1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * SCSI disk target driver. 31 */ 32 33 #include <sys/scsi/scsi.h> 34 #include <sys/dkbad.h> 35 #include <sys/dklabel.h> 36 #include <sys/dkio.h> 37 #include <sys/fdio.h> 38 #include <sys/cdio.h> 39 #include <sys/mhd.h> 40 #include <sys/vtoc.h> 41 #include <sys/dktp/fdisk.h> 42 #include <sys/file.h> 43 #include <sys/stat.h> 44 #include <sys/kstat.h> 45 #include <sys/vtrace.h> 46 #include <sys/note.h> 47 #include <sys/thread.h> 48 #include <sys/proc.h> 49 #include <sys/efi_partition.h> 50 #include <sys/var.h> 51 #include <sys/aio_req.h> 52 #if (defined(__fibre)) 53 /* Note: is there a leadville version of the following? */ 54 #include <sys/fc4/fcal_linkapp.h> 55 #endif 56 #include <sys/taskq.h> 57 #include <sys/uuid.h> 58 #include <sys/byteorder.h> 59 #include <sys/sdt.h> 60 61 #include "sd_xbuf.h" 62 63 #include <sys/scsi/targets/sddef.h> 64 65 66 /* 67 * Loadable module info. 68 */ 69 #if (defined(__fibre)) 70 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver %I%" 71 char _depends_on[] = "misc/scsi drv/fcp"; 72 #else 73 #define SD_MODULE_NAME "SCSI Disk Driver %I%" 74 char _depends_on[] = "misc/scsi"; 75 #endif 76 77 /* 78 * Define the interconnect type, to allow the driver to distinguish 79 * between parallel SCSI (sd) and fibre channel (ssd) behaviors. 80 * 81 * This is really for backward compatability. In the future, the driver 82 * should actually check the "interconnect-type" property as reported by 83 * the HBA; however at present this property is not defined by all HBAs, 84 * so we will use this #define (1) to permit the driver to run in 85 * backward-compatability mode; and (2) to print a notification message 86 * if an FC HBA does not support the "interconnect-type" property. The 87 * behavior of the driver will be to assume parallel SCSI behaviors unless 88 * the "interconnect-type" property is defined by the HBA **AND** has a 89 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or 90 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre 91 * Channel behaviors (as per the old ssd). (Note that the 92 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and 93 * will result in the driver assuming parallel SCSI behaviors.) 94 * 95 * (see common/sys/scsi/impl/services.h) 96 * 97 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default 98 * since some FC HBAs may already support that, and there is some code in 99 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the 100 * default would confuse that code, and besides things should work fine 101 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the 102 * "interconnect_type" property. 103 */ 104 #if (defined(__fibre)) 105 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE 106 #else 107 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL 108 #endif 109 110 /* 111 * The name of the driver, established from the module name in _init. 112 */ 113 static char *sd_label = NULL; 114 115 /* 116 * Driver name is unfortunately prefixed on some driver.conf properties. 117 */ 118 #if (defined(__fibre)) 119 #define sd_max_xfer_size ssd_max_xfer_size 120 #define sd_config_list ssd_config_list 121 static char *sd_max_xfer_size = "ssd_max_xfer_size"; 122 static char *sd_config_list = "ssd-config-list"; 123 #else 124 static char *sd_max_xfer_size = "sd_max_xfer_size"; 125 static char *sd_config_list = "sd-config-list"; 126 #endif 127 128 /* 129 * Driver global variables 130 */ 131 132 #if (defined(__fibre)) 133 /* 134 * These #defines are to avoid namespace collisions that occur because this 135 * code is currently used to compile two seperate driver modules: sd and ssd. 136 * All global variables need to be treated this way (even if declared static) 137 * in order to allow the debugger to resolve the names properly. 138 * It is anticipated that in the near future the ssd module will be obsoleted, 139 * at which time this namespace issue should go away. 140 */ 141 #define sd_state ssd_state 142 #define sd_io_time ssd_io_time 143 #define sd_failfast_enable ssd_failfast_enable 144 #define sd_ua_retry_count ssd_ua_retry_count 145 #define sd_report_pfa ssd_report_pfa 146 #define sd_max_throttle ssd_max_throttle 147 #define sd_min_throttle ssd_min_throttle 148 #define sd_rot_delay ssd_rot_delay 149 150 #define sd_retry_on_reservation_conflict \ 151 ssd_retry_on_reservation_conflict 152 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay 153 #define sd_resv_conflict_name ssd_resv_conflict_name 154 155 #define sd_component_mask ssd_component_mask 156 #define sd_level_mask ssd_level_mask 157 #define sd_debug_un ssd_debug_un 158 #define sd_error_level ssd_error_level 159 160 #define sd_xbuf_active_limit ssd_xbuf_active_limit 161 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit 162 163 #define sd_tr ssd_tr 164 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout 165 #define sd_check_media_time ssd_check_media_time 166 #define sd_wait_cmds_complete ssd_wait_cmds_complete 167 #define sd_label_mutex ssd_label_mutex 168 #define sd_detach_mutex ssd_detach_mutex 169 #define sd_log_buf ssd_log_buf 170 #define sd_log_mutex ssd_log_mutex 171 172 #define sd_disk_table ssd_disk_table 173 #define sd_disk_table_size ssd_disk_table_size 174 #define sd_sense_mutex ssd_sense_mutex 175 #define sd_cdbtab ssd_cdbtab 176 177 #define sd_cb_ops ssd_cb_ops 178 #define sd_ops ssd_ops 179 #define sd_additional_codes ssd_additional_codes 180 181 #define sd_minor_data ssd_minor_data 182 #define sd_minor_data_efi ssd_minor_data_efi 183 184 #define sd_tq ssd_tq 185 #define sd_wmr_tq ssd_wmr_tq 186 #define sd_taskq_name ssd_taskq_name 187 #define sd_wmr_taskq_name ssd_wmr_taskq_name 188 #define sd_taskq_minalloc ssd_taskq_minalloc 189 #define sd_taskq_maxalloc ssd_taskq_maxalloc 190 191 #define sd_dump_format_string ssd_dump_format_string 192 193 #define sd_iostart_chain ssd_iostart_chain 194 #define sd_iodone_chain ssd_iodone_chain 195 196 #define sd_pm_idletime ssd_pm_idletime 197 198 #define sd_force_pm_supported ssd_force_pm_supported 199 200 #define sd_dtype_optical_bind ssd_dtype_optical_bind 201 #endif 202 203 204 #ifdef SDDEBUG 205 int sd_force_pm_supported = 0; 206 #endif /* SDDEBUG */ 207 208 void *sd_state = NULL; 209 int sd_io_time = SD_IO_TIME; 210 int sd_failfast_enable = 1; 211 int sd_ua_retry_count = SD_UA_RETRY_COUNT; 212 int sd_report_pfa = 1; 213 int sd_max_throttle = SD_MAX_THROTTLE; 214 int sd_min_throttle = SD_MIN_THROTTLE; 215 int sd_rot_delay = 4; /* Default 4ms Rotation delay */ 216 217 int sd_retry_on_reservation_conflict = 1; 218 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 219 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay)) 220 221 static int sd_dtype_optical_bind = -1; 222 223 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */ 224 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict"; 225 226 /* 227 * Global data for debug logging. To enable debug printing, sd_component_mask 228 * and sd_level_mask should be set to the desired bit patterns as outlined in 229 * sddef.h. 230 */ 231 uint_t sd_component_mask = 0x0; 232 uint_t sd_level_mask = 0x0; 233 struct sd_lun *sd_debug_un = NULL; 234 uint_t sd_error_level = SCSI_ERR_RETRYABLE; 235 236 /* Note: these may go away in the future... */ 237 static uint32_t sd_xbuf_active_limit = 512; 238 static uint32_t sd_xbuf_reserve_limit = 16; 239 240 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 }; 241 242 /* 243 * Timer value used to reset the throttle after it has been reduced 244 * (typically in response to TRAN_BUSY) 245 */ 246 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT; 247 248 /* 249 * Interval value associated with the media change scsi watch. 250 */ 251 static int sd_check_media_time = 3000000; 252 253 /* 254 * Wait value used for in progress operations during a DDI_SUSPEND 255 */ 256 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE; 257 258 /* 259 * sd_label_mutex protects a static buffer used in the disk label 260 * component of the driver 261 */ 262 static kmutex_t sd_label_mutex; 263 264 /* 265 * sd_detach_mutex protects un_layer_count, un_detach_count, and 266 * un_opens_in_progress in the sd_lun structure. 267 */ 268 static kmutex_t sd_detach_mutex; 269 270 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex, 271 sd_lun::{un_layer_count un_detach_count un_opens_in_progress})) 272 273 /* 274 * Global buffer and mutex for debug logging 275 */ 276 static char sd_log_buf[1024]; 277 static kmutex_t sd_log_mutex; 278 279 280 /* 281 * "Smart" Probe Caching structs, globals, #defines, etc. 282 * For parallel scsi and non-self-identify device only. 283 */ 284 285 /* 286 * The following resources and routines are implemented to support 287 * "smart" probing, which caches the scsi_probe() results in an array, 288 * in order to help avoid long probe times. 289 */ 290 struct sd_scsi_probe_cache { 291 struct sd_scsi_probe_cache *next; 292 dev_info_t *pdip; 293 int cache[NTARGETS_WIDE]; 294 }; 295 296 static kmutex_t sd_scsi_probe_cache_mutex; 297 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL; 298 299 /* 300 * Really we only need protection on the head of the linked list, but 301 * better safe than sorry. 302 */ 303 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 304 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip)) 305 306 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 307 sd_scsi_probe_cache_head)) 308 309 310 /* 311 * Vendor specific data name property declarations 312 */ 313 314 #if defined(__fibre) || defined(__i386) ||defined(__amd64) 315 316 static sd_tunables seagate_properties = { 317 SEAGATE_THROTTLE_VALUE, 318 0, 319 0, 320 0, 321 0, 322 0, 323 0, 324 0, 325 0 326 }; 327 328 static sd_tunables lsi_properties = { 329 0, 330 0, 331 LSI_NOTREADY_RETRIES, 332 0, 333 0, 334 0, 335 0, 336 0, 337 0 338 }; 339 340 static sd_tunables lsi_oem_properties = { 341 0, 342 0, 343 LSI_OEM_NOTREADY_RETRIES, 344 0, 345 0, 346 0, 347 0, 348 0, 349 0 350 }; 351 352 static sd_tunables fujitsu_properties = { 353 FUJITSU_THROTTLE_VALUE, 354 0, 355 0, 356 0, 357 0, 358 0, 359 0, 360 0, 361 0 362 }; 363 364 static sd_tunables ibm_properties = { 365 IBM_THROTTLE_VALUE, 366 0, 367 0, 368 0, 369 0, 370 0, 371 0, 372 0, 373 0 374 }; 375 376 static sd_tunables purple_properties = { 377 PURPLE_THROTTLE_VALUE, 378 0, 379 0, 380 PURPLE_BUSY_RETRIES, 381 PURPLE_RESET_RETRY_COUNT, 382 PURPLE_RESERVE_RELEASE_TIME, 383 0, 384 0, 385 0 386 }; 387 388 static sd_tunables sve_properties = { 389 SVE_THROTTLE_VALUE, 390 0, 391 0, 392 SVE_BUSY_RETRIES, 393 SVE_RESET_RETRY_COUNT, 394 SVE_RESERVE_RELEASE_TIME, 395 SVE_MIN_THROTTLE_VALUE, 396 SVE_DISKSORT_DISABLED_FLAG, 397 0 398 }; 399 400 static sd_tunables maserati_properties = { 401 0, 402 0, 403 0, 404 0, 405 0, 406 0, 407 0, 408 MASERATI_DISKSORT_DISABLED_FLAG, 409 MASERATI_LUN_RESET_ENABLED_FLAG 410 }; 411 412 static sd_tunables pirus_properties = { 413 PIRUS_THROTTLE_VALUE, 414 0, 415 PIRUS_NRR_COUNT, 416 PIRUS_BUSY_RETRIES, 417 PIRUS_RESET_RETRY_COUNT, 418 0, 419 PIRUS_MIN_THROTTLE_VALUE, 420 PIRUS_DISKSORT_DISABLED_FLAG, 421 PIRUS_LUN_RESET_ENABLED_FLAG 422 }; 423 424 #endif 425 #if (defined(__sparc) && !defined(__fibre)) || \ 426 (defined(__i386) || defined(__amd64)) 427 428 static sd_tunables lsi_properties_scsi = { 429 LSI_THROTTLE_VALUE, 430 0, 431 LSI_NOTREADY_RETRIES, 432 0, 433 0, 434 0, 435 0, 436 0, 437 0 438 }; 439 440 static sd_tunables elite_properties = { 441 ELITE_THROTTLE_VALUE, 442 0, 443 0, 444 0, 445 0, 446 0, 447 0, 448 0, 449 0 450 }; 451 452 static sd_tunables st31200n_properties = { 453 ST31200N_THROTTLE_VALUE, 454 0, 455 0, 456 0, 457 0, 458 0, 459 0, 460 0, 461 0 462 }; 463 464 #endif /* Fibre or not */ 465 466 static sd_tunables symbios_properties = { 467 SYMBIOS_THROTTLE_VALUE, 468 0, 469 SYMBIOS_NOTREADY_RETRIES, 470 0, 471 0, 472 0, 473 0, 474 0, 475 0 476 }; 477 478 479 480 481 #if (defined(SD_PROP_TST)) 482 483 #define SD_TST_CTYPE_VAL CTYPE_CDROM 484 #define SD_TST_THROTTLE_VAL 16 485 #define SD_TST_NOTREADY_VAL 12 486 #define SD_TST_BUSY_VAL 60 487 #define SD_TST_RST_RETRY_VAL 36 488 #define SD_TST_RSV_REL_TIME 60 489 490 static sd_tunables tst_properties = { 491 SD_TST_THROTTLE_VAL, 492 SD_TST_CTYPE_VAL, 493 SD_TST_NOTREADY_VAL, 494 SD_TST_BUSY_VAL, 495 SD_TST_RST_RETRY_VAL, 496 SD_TST_RSV_REL_TIME, 497 0, 498 0, 499 0 500 }; 501 #endif 502 503 /* This is similiar to the ANSI toupper implementation */ 504 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C)) 505 506 /* 507 * Static Driver Configuration Table 508 * 509 * This is the table of disks which need throttle adjustment (or, perhaps 510 * something else as defined by the flags at a future time.) device_id 511 * is a string consisting of concatenated vid (vendor), pid (product/model) 512 * and revision strings as defined in the scsi_inquiry structure. Offsets of 513 * the parts of the string are as defined by the sizes in the scsi_inquiry 514 * structure. Device type is searched as far as the device_id string is 515 * defined. Flags defines which values are to be set in the driver from the 516 * properties list. 517 * 518 * Entries below which begin and end with a "*" are a special case. 519 * These do not have a specific vendor, and the string which follows 520 * can appear anywhere in the 16 byte PID portion of the inquiry data. 521 * 522 * Entries below which begin and end with a " " (blank) are a special 523 * case. The comparison function will treat multiple consecutive blanks 524 * as equivalent to a single blank. For example, this causes a 525 * sd_disk_table entry of " NEC CDROM " to match a device's id string 526 * of "NEC CDROM". 527 * 528 * Note: The MD21 controller type has been obsoleted. 529 * ST318202F is a Legacy device 530 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been 531 * made with an FC connection. The entries here are a legacy. 532 */ 533 static sd_disk_config_t sd_disk_table[] = { 534 #if defined(__fibre) || defined(__i386) || defined(__amd64) 535 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 536 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 537 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 538 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 539 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 540 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 541 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 542 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 543 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 544 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 545 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 546 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 547 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 548 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 549 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 550 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 551 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 552 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 553 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 554 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 555 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 556 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 557 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 558 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties }, 559 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties }, 560 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties }, 561 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties }, 562 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 563 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 564 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 565 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 566 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 567 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 568 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 569 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 570 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 571 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 572 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 573 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 574 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 575 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties }, 576 { "SUN T3", SD_CONF_BSET_THROTTLE | 577 SD_CONF_BSET_BSY_RETRY_COUNT| 578 SD_CONF_BSET_RST_RETRIES| 579 SD_CONF_BSET_RSV_REL_TIME, 580 &purple_properties }, 581 { "SUN SESS01", SD_CONF_BSET_THROTTLE | 582 SD_CONF_BSET_BSY_RETRY_COUNT| 583 SD_CONF_BSET_RST_RETRIES| 584 SD_CONF_BSET_RSV_REL_TIME| 585 SD_CONF_BSET_MIN_THROTTLE| 586 SD_CONF_BSET_DISKSORT_DISABLED, 587 &sve_properties }, 588 { "SUN T4", SD_CONF_BSET_THROTTLE | 589 SD_CONF_BSET_BSY_RETRY_COUNT| 590 SD_CONF_BSET_RST_RETRIES| 591 SD_CONF_BSET_RSV_REL_TIME, 592 &purple_properties }, 593 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED | 594 SD_CONF_BSET_LUN_RESET_ENABLED, 595 &maserati_properties }, 596 { "SUN SE6920", SD_CONF_BSET_THROTTLE | 597 SD_CONF_BSET_NRR_COUNT| 598 SD_CONF_BSET_BSY_RETRY_COUNT| 599 SD_CONF_BSET_RST_RETRIES| 600 SD_CONF_BSET_MIN_THROTTLE| 601 SD_CONF_BSET_DISKSORT_DISABLED| 602 SD_CONF_BSET_LUN_RESET_ENABLED, 603 &pirus_properties }, 604 { "SUN PSX1000", SD_CONF_BSET_THROTTLE | 605 SD_CONF_BSET_NRR_COUNT| 606 SD_CONF_BSET_BSY_RETRY_COUNT| 607 SD_CONF_BSET_RST_RETRIES| 608 SD_CONF_BSET_MIN_THROTTLE| 609 SD_CONF_BSET_DISKSORT_DISABLED| 610 SD_CONF_BSET_LUN_RESET_ENABLED, 611 &pirus_properties }, 612 { "SUN SE6330", SD_CONF_BSET_THROTTLE | 613 SD_CONF_BSET_NRR_COUNT| 614 SD_CONF_BSET_BSY_RETRY_COUNT| 615 SD_CONF_BSET_RST_RETRIES| 616 SD_CONF_BSET_MIN_THROTTLE| 617 SD_CONF_BSET_DISKSORT_DISABLED| 618 SD_CONF_BSET_LUN_RESET_ENABLED, 619 &pirus_properties }, 620 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 621 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 622 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 623 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 624 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties }, 625 #endif /* fibre or NON-sparc platforms */ 626 #if ((defined(__sparc) && !defined(__fibre)) ||\ 627 (defined(__i386) || defined(__amd64))) 628 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties }, 629 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties }, 630 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL }, 631 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL }, 632 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL }, 633 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL }, 634 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL }, 635 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL }, 636 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL }, 637 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL }, 638 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL }, 639 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL }, 640 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT, 641 &symbios_properties }, 642 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT, 643 &lsi_properties_scsi }, 644 #if defined(__i386) || defined(__amd64) 645 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD 646 | SD_CONF_BSET_READSUB_BCD 647 | SD_CONF_BSET_READ_TOC_ADDR_BCD 648 | SD_CONF_BSET_NO_READ_HEADER 649 | SD_CONF_BSET_READ_CD_XD4), NULL }, 650 651 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD 652 | SD_CONF_BSET_READSUB_BCD 653 | SD_CONF_BSET_READ_TOC_ADDR_BCD 654 | SD_CONF_BSET_NO_READ_HEADER 655 | SD_CONF_BSET_READ_CD_XD4), NULL }, 656 #endif /* __i386 || __amd64 */ 657 #endif /* sparc NON-fibre or NON-sparc platforms */ 658 659 #if (defined(SD_PROP_TST)) 660 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE 661 | SD_CONF_BSET_CTYPE 662 | SD_CONF_BSET_NRR_COUNT 663 | SD_CONF_BSET_FAB_DEVID 664 | SD_CONF_BSET_NOCACHE 665 | SD_CONF_BSET_BSY_RETRY_COUNT 666 | SD_CONF_BSET_PLAYMSF_BCD 667 | SD_CONF_BSET_READSUB_BCD 668 | SD_CONF_BSET_READ_TOC_TRK_BCD 669 | SD_CONF_BSET_READ_TOC_ADDR_BCD 670 | SD_CONF_BSET_NO_READ_HEADER 671 | SD_CONF_BSET_READ_CD_XD4 672 | SD_CONF_BSET_RST_RETRIES 673 | SD_CONF_BSET_RSV_REL_TIME 674 | SD_CONF_BSET_TUR_CHECK), &tst_properties}, 675 #endif 676 }; 677 678 static const int sd_disk_table_size = 679 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t); 680 681 682 /* 683 * Return codes of sd_uselabel(). 684 */ 685 #define SD_LABEL_IS_VALID 0 686 #define SD_LABEL_IS_INVALID 1 687 688 #define SD_INTERCONNECT_PARALLEL 0 689 #define SD_INTERCONNECT_FABRIC 1 690 #define SD_INTERCONNECT_FIBRE 2 691 #define SD_INTERCONNECT_SSA 3 692 #define SD_IS_PARALLEL_SCSI(un) \ 693 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL) 694 695 /* 696 * Definitions used by device id registration routines 697 */ 698 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */ 699 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */ 700 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */ 701 #define WD_NODE 7 /* the whole disk minor */ 702 703 static kmutex_t sd_sense_mutex = {0}; 704 705 /* 706 * Macros for updates of the driver state 707 */ 708 #define New_state(un, s) \ 709 (un)->un_last_state = (un)->un_state, (un)->un_state = (s) 710 #define Restore_state(un) \ 711 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); } 712 713 static struct sd_cdbinfo sd_cdbtab[] = { 714 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, }, 715 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, }, 716 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, }, 717 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, }, 718 }; 719 720 /* 721 * Specifies the number of seconds that must have elapsed since the last 722 * cmd. has completed for a device to be declared idle to the PM framework. 723 */ 724 static int sd_pm_idletime = 1; 725 726 /* 727 * Internal function prototypes 728 */ 729 730 #if (defined(__fibre)) 731 /* 732 * These #defines are to avoid namespace collisions that occur because this 733 * code is currently used to compile two seperate driver modules: sd and ssd. 734 * All function names need to be treated this way (even if declared static) 735 * in order to allow the debugger to resolve the names properly. 736 * It is anticipated that in the near future the ssd module will be obsoleted, 737 * at which time this ugliness should go away. 738 */ 739 #define sd_log_trace ssd_log_trace 740 #define sd_log_info ssd_log_info 741 #define sd_log_err ssd_log_err 742 #define sdprobe ssdprobe 743 #define sdinfo ssdinfo 744 #define sd_prop_op ssd_prop_op 745 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init 746 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini 747 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache 748 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache 749 #define sd_spin_up_unit ssd_spin_up_unit 750 #define sd_enable_descr_sense ssd_enable_descr_sense 751 #define sd_set_mmc_caps ssd_set_mmc_caps 752 #define sd_read_unit_properties ssd_read_unit_properties 753 #define sd_process_sdconf_file ssd_process_sdconf_file 754 #define sd_process_sdconf_table ssd_process_sdconf_table 755 #define sd_sdconf_id_match ssd_sdconf_id_match 756 #define sd_blank_cmp ssd_blank_cmp 757 #define sd_chk_vers1_data ssd_chk_vers1_data 758 #define sd_set_vers1_properties ssd_set_vers1_properties 759 #define sd_validate_geometry ssd_validate_geometry 760 761 #if defined(_SUNOS_VTOC_16) 762 #define sd_convert_geometry ssd_convert_geometry 763 #endif 764 765 #define sd_resync_geom_caches ssd_resync_geom_caches 766 #define sd_read_fdisk ssd_read_fdisk 767 #define sd_get_physical_geometry ssd_get_physical_geometry 768 #define sd_get_virtual_geometry ssd_get_virtual_geometry 769 #define sd_update_block_info ssd_update_block_info 770 #define sd_swap_efi_gpt ssd_swap_efi_gpt 771 #define sd_swap_efi_gpe ssd_swap_efi_gpe 772 #define sd_validate_efi ssd_validate_efi 773 #define sd_use_efi ssd_use_efi 774 #define sd_uselabel ssd_uselabel 775 #define sd_build_default_label ssd_build_default_label 776 #define sd_has_max_chs_vals ssd_has_max_chs_vals 777 #define sd_inq_fill ssd_inq_fill 778 #define sd_register_devid ssd_register_devid 779 #define sd_get_devid_block ssd_get_devid_block 780 #define sd_get_devid ssd_get_devid 781 #define sd_create_devid ssd_create_devid 782 #define sd_write_deviceid ssd_write_deviceid 783 #define sd_check_vpd_page_support ssd_check_vpd_page_support 784 #define sd_setup_pm ssd_setup_pm 785 #define sd_create_pm_components ssd_create_pm_components 786 #define sd_ddi_suspend ssd_ddi_suspend 787 #define sd_ddi_pm_suspend ssd_ddi_pm_suspend 788 #define sd_ddi_resume ssd_ddi_resume 789 #define sd_ddi_pm_resume ssd_ddi_pm_resume 790 #define sdpower ssdpower 791 #define sdattach ssdattach 792 #define sddetach ssddetach 793 #define sd_unit_attach ssd_unit_attach 794 #define sd_unit_detach ssd_unit_detach 795 #define sd_create_minor_nodes ssd_create_minor_nodes 796 #define sd_create_errstats ssd_create_errstats 797 #define sd_set_errstats ssd_set_errstats 798 #define sd_set_pstats ssd_set_pstats 799 #define sddump ssddump 800 #define sd_scsi_poll ssd_scsi_poll 801 #define sd_send_polled_RQS ssd_send_polled_RQS 802 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll 803 #define sd_init_event_callbacks ssd_init_event_callbacks 804 #define sd_event_callback ssd_event_callback 805 #define sd_disable_caching ssd_disable_caching 806 #define sd_make_device ssd_make_device 807 #define sdopen ssdopen 808 #define sdclose ssdclose 809 #define sd_ready_and_valid ssd_ready_and_valid 810 #define sdmin ssdmin 811 #define sdread ssdread 812 #define sdwrite ssdwrite 813 #define sdaread ssdaread 814 #define sdawrite ssdawrite 815 #define sdstrategy ssdstrategy 816 #define sdioctl ssdioctl 817 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart 818 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart 819 #define sd_checksum_iostart ssd_checksum_iostart 820 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart 821 #define sd_pm_iostart ssd_pm_iostart 822 #define sd_core_iostart ssd_core_iostart 823 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone 824 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone 825 #define sd_checksum_iodone ssd_checksum_iodone 826 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone 827 #define sd_pm_iodone ssd_pm_iodone 828 #define sd_initpkt_for_buf ssd_initpkt_for_buf 829 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf 830 #define sd_setup_rw_pkt ssd_setup_rw_pkt 831 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt 832 #define sd_buf_iodone ssd_buf_iodone 833 #define sd_uscsi_strategy ssd_uscsi_strategy 834 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi 835 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi 836 #define sd_uscsi_iodone ssd_uscsi_iodone 837 #define sd_xbuf_strategy ssd_xbuf_strategy 838 #define sd_xbuf_init ssd_xbuf_init 839 #define sd_pm_entry ssd_pm_entry 840 #define sd_pm_exit ssd_pm_exit 841 842 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler 843 #define sd_pm_timeout_handler ssd_pm_timeout_handler 844 845 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq 846 #define sdintr ssdintr 847 #define sd_start_cmds ssd_start_cmds 848 #define sd_send_scsi_cmd ssd_send_scsi_cmd 849 #define sd_bioclone_alloc ssd_bioclone_alloc 850 #define sd_bioclone_free ssd_bioclone_free 851 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc 852 #define sd_shadow_buf_free ssd_shadow_buf_free 853 #define sd_print_transport_rejected_message \ 854 ssd_print_transport_rejected_message 855 #define sd_retry_command ssd_retry_command 856 #define sd_set_retry_bp ssd_set_retry_bp 857 #define sd_send_request_sense_command ssd_send_request_sense_command 858 #define sd_start_retry_command ssd_start_retry_command 859 #define sd_start_direct_priority_command \ 860 ssd_start_direct_priority_command 861 #define sd_return_failed_command ssd_return_failed_command 862 #define sd_return_failed_command_no_restart \ 863 ssd_return_failed_command_no_restart 864 #define sd_return_command ssd_return_command 865 #define sd_sync_with_callback ssd_sync_with_callback 866 #define sdrunout ssdrunout 867 #define sd_mark_rqs_busy ssd_mark_rqs_busy 868 #define sd_mark_rqs_idle ssd_mark_rqs_idle 869 #define sd_reduce_throttle ssd_reduce_throttle 870 #define sd_restore_throttle ssd_restore_throttle 871 #define sd_print_incomplete_msg ssd_print_incomplete_msg 872 #define sd_init_cdb_limits ssd_init_cdb_limits 873 #define sd_pkt_status_good ssd_pkt_status_good 874 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition 875 #define sd_pkt_status_busy ssd_pkt_status_busy 876 #define sd_pkt_status_reservation_conflict \ 877 ssd_pkt_status_reservation_conflict 878 #define sd_pkt_status_qfull ssd_pkt_status_qfull 879 #define sd_handle_request_sense ssd_handle_request_sense 880 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense 881 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg 882 #define sd_validate_sense_data ssd_validate_sense_data 883 #define sd_decode_sense ssd_decode_sense 884 #define sd_print_sense_msg ssd_print_sense_msg 885 #define sd_extract_sense_info_descr ssd_extract_sense_info_descr 886 #define sd_sense_key_no_sense ssd_sense_key_no_sense 887 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error 888 #define sd_sense_key_not_ready ssd_sense_key_not_ready 889 #define sd_sense_key_medium_or_hardware_error \ 890 ssd_sense_key_medium_or_hardware_error 891 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request 892 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention 893 #define sd_sense_key_fail_command ssd_sense_key_fail_command 894 #define sd_sense_key_blank_check ssd_sense_key_blank_check 895 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command 896 #define sd_sense_key_default ssd_sense_key_default 897 #define sd_print_retry_msg ssd_print_retry_msg 898 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg 899 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete 900 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err 901 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset 902 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted 903 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout 904 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free 905 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject 906 #define sd_pkt_reason_default ssd_pkt_reason_default 907 #define sd_reset_target ssd_reset_target 908 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback 909 #define sd_start_stop_unit_task ssd_start_stop_unit_task 910 #define sd_taskq_create ssd_taskq_create 911 #define sd_taskq_delete ssd_taskq_delete 912 #define sd_media_change_task ssd_media_change_task 913 #define sd_handle_mchange ssd_handle_mchange 914 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK 915 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY 916 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16 917 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION 918 #define sd_send_scsi_feature_GET_CONFIGURATION \ 919 sd_send_scsi_feature_GET_CONFIGURATION 920 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT 921 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY 922 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY 923 #define sd_send_scsi_PERSISTENT_RESERVE_IN \ 924 ssd_send_scsi_PERSISTENT_RESERVE_IN 925 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \ 926 ssd_send_scsi_PERSISTENT_RESERVE_OUT 927 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE 928 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE 929 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT 930 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR 931 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE 932 #define sd_alloc_rqs ssd_alloc_rqs 933 #define sd_free_rqs ssd_free_rqs 934 #define sd_dump_memory ssd_dump_memory 935 #define sd_uscsi_ioctl ssd_uscsi_ioctl 936 #define sd_get_media_info ssd_get_media_info 937 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info 938 #define sd_dkio_get_geometry ssd_dkio_get_geometry 939 #define sd_dkio_set_geometry ssd_dkio_set_geometry 940 #define sd_dkio_get_partition ssd_dkio_get_partition 941 #define sd_dkio_set_partition ssd_dkio_set_partition 942 #define sd_dkio_partition ssd_dkio_partition 943 #define sd_dkio_get_vtoc ssd_dkio_get_vtoc 944 #define sd_dkio_get_efi ssd_dkio_get_efi 945 #define sd_build_user_vtoc ssd_build_user_vtoc 946 #define sd_dkio_set_vtoc ssd_dkio_set_vtoc 947 #define sd_dkio_set_efi ssd_dkio_set_efi 948 #define sd_build_label_vtoc ssd_build_label_vtoc 949 #define sd_write_label ssd_write_label 950 #define sd_clear_vtoc ssd_clear_vtoc 951 #define sd_clear_efi ssd_clear_efi 952 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf 953 #define sd_setup_next_xfer ssd_setup_next_xfer 954 #define sd_dkio_get_temp ssd_dkio_get_temp 955 #define sd_dkio_get_mboot ssd_dkio_get_mboot 956 #define sd_dkio_set_mboot ssd_dkio_set_mboot 957 #define sd_setup_default_geometry ssd_setup_default_geometry 958 #define sd_update_fdisk_and_vtoc ssd_update_fdisk_and_vtoc 959 #define sd_check_mhd ssd_check_mhd 960 #define sd_mhd_watch_cb ssd_mhd_watch_cb 961 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete 962 #define sd_sname ssd_sname 963 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover 964 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread 965 #define sd_take_ownership ssd_take_ownership 966 #define sd_reserve_release ssd_reserve_release 967 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req 968 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb 969 #define sd_persistent_reservation_in_read_keys \ 970 ssd_persistent_reservation_in_read_keys 971 #define sd_persistent_reservation_in_read_resv \ 972 ssd_persistent_reservation_in_read_resv 973 #define sd_mhdioc_takeown ssd_mhdioc_takeown 974 #define sd_mhdioc_failfast ssd_mhdioc_failfast 975 #define sd_mhdioc_release ssd_mhdioc_release 976 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid 977 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys 978 #define sd_mhdioc_inresv ssd_mhdioc_inresv 979 #define sr_change_blkmode ssr_change_blkmode 980 #define sr_change_speed ssr_change_speed 981 #define sr_atapi_change_speed ssr_atapi_change_speed 982 #define sr_pause_resume ssr_pause_resume 983 #define sr_play_msf ssr_play_msf 984 #define sr_play_trkind ssr_play_trkind 985 #define sr_read_all_subcodes ssr_read_all_subcodes 986 #define sr_read_subchannel ssr_read_subchannel 987 #define sr_read_tocentry ssr_read_tocentry 988 #define sr_read_tochdr ssr_read_tochdr 989 #define sr_read_cdda ssr_read_cdda 990 #define sr_read_cdxa ssr_read_cdxa 991 #define sr_read_mode1 ssr_read_mode1 992 #define sr_read_mode2 ssr_read_mode2 993 #define sr_read_cd_mode2 ssr_read_cd_mode2 994 #define sr_sector_mode ssr_sector_mode 995 #define sr_eject ssr_eject 996 #define sr_ejected ssr_ejected 997 #define sr_check_wp ssr_check_wp 998 #define sd_check_media ssd_check_media 999 #define sd_media_watch_cb ssd_media_watch_cb 1000 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast 1001 #define sr_volume_ctrl ssr_volume_ctrl 1002 #define sr_read_sony_session_offset ssr_read_sony_session_offset 1003 #define sd_log_page_supported ssd_log_page_supported 1004 #define sd_check_for_writable_cd ssd_check_for_writable_cd 1005 #define sd_wm_cache_constructor ssd_wm_cache_constructor 1006 #define sd_wm_cache_destructor ssd_wm_cache_destructor 1007 #define sd_range_lock ssd_range_lock 1008 #define sd_get_range ssd_get_range 1009 #define sd_free_inlist_wmap ssd_free_inlist_wmap 1010 #define sd_range_unlock ssd_range_unlock 1011 #define sd_read_modify_write_task ssd_read_modify_write_task 1012 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw 1013 1014 #define sd_iostart_chain ssd_iostart_chain 1015 #define sd_iodone_chain ssd_iodone_chain 1016 #define sd_initpkt_map ssd_initpkt_map 1017 #define sd_destroypkt_map ssd_destroypkt_map 1018 #define sd_chain_type_map ssd_chain_type_map 1019 #define sd_chain_index_map ssd_chain_index_map 1020 1021 #define sd_failfast_flushctl ssd_failfast_flushctl 1022 #define sd_failfast_flushq ssd_failfast_flushq 1023 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback 1024 1025 #endif /* #if (defined(__fibre)) */ 1026 1027 1028 int _init(void); 1029 int _fini(void); 1030 int _info(struct modinfo *modinfop); 1031 1032 /*PRINTFLIKE3*/ 1033 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1034 /*PRINTFLIKE3*/ 1035 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1036 /*PRINTFLIKE3*/ 1037 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1038 1039 static int sdprobe(dev_info_t *devi); 1040 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1041 void **result); 1042 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1043 int mod_flags, char *name, caddr_t valuep, int *lengthp); 1044 1045 /* 1046 * Smart probe for parallel scsi 1047 */ 1048 static void sd_scsi_probe_cache_init(void); 1049 static void sd_scsi_probe_cache_fini(void); 1050 static void sd_scsi_clear_probe_cache(void); 1051 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)()); 1052 1053 static int sd_spin_up_unit(struct sd_lun *un); 1054 static void sd_enable_descr_sense(struct sd_lun *un); 1055 static void sd_set_mmc_caps(struct sd_lun *un); 1056 1057 static void sd_read_unit_properties(struct sd_lun *un); 1058 static int sd_process_sdconf_file(struct sd_lun *un); 1059 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, 1060 int *data_list, sd_tunables *values); 1061 static void sd_process_sdconf_table(struct sd_lun *un); 1062 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen); 1063 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen); 1064 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 1065 int list_len, char *dataname_ptr); 1066 static void sd_set_vers1_properties(struct sd_lun *un, int flags, 1067 sd_tunables *prop_list); 1068 static int sd_validate_geometry(struct sd_lun *un, int path_flag); 1069 1070 #if defined(_SUNOS_VTOC_16) 1071 static void sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g); 1072 #endif 1073 1074 static void sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 1075 int path_flag); 1076 static int sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, 1077 int path_flag); 1078 static void sd_get_physical_geometry(struct sd_lun *un, 1079 struct geom_cache *pgeom_p, int capacity, int lbasize, int path_flag); 1080 static void sd_get_virtual_geometry(struct sd_lun *un, int capacity, 1081 int lbasize); 1082 static int sd_uselabel(struct sd_lun *un, struct dk_label *l, int path_flag); 1083 static void sd_swap_efi_gpt(efi_gpt_t *); 1084 static void sd_swap_efi_gpe(int nparts, efi_gpe_t *); 1085 static int sd_validate_efi(efi_gpt_t *); 1086 static int sd_use_efi(struct sd_lun *, int); 1087 static void sd_build_default_label(struct sd_lun *un); 1088 1089 #if defined(_FIRMWARE_NEEDS_FDISK) 1090 static int sd_has_max_chs_vals(struct ipart *fdp); 1091 #endif 1092 static void sd_inq_fill(char *p, int l, char *s); 1093 1094 1095 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi, 1096 int reservation_flag); 1097 static daddr_t sd_get_devid_block(struct sd_lun *un); 1098 static int sd_get_devid(struct sd_lun *un); 1099 static int sd_get_serialnum(struct sd_lun *un, uchar_t *wwn, int *len); 1100 static ddi_devid_t sd_create_devid(struct sd_lun *un); 1101 static int sd_write_deviceid(struct sd_lun *un); 1102 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len); 1103 static int sd_check_vpd_page_support(struct sd_lun *un); 1104 1105 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi); 1106 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un); 1107 1108 static int sd_ddi_suspend(dev_info_t *devi); 1109 static int sd_ddi_pm_suspend(struct sd_lun *un); 1110 static int sd_ddi_resume(dev_info_t *devi); 1111 static int sd_ddi_pm_resume(struct sd_lun *un); 1112 static int sdpower(dev_info_t *devi, int component, int level); 1113 1114 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1115 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1116 static int sd_unit_attach(dev_info_t *devi); 1117 static int sd_unit_detach(dev_info_t *devi); 1118 1119 static int sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi); 1120 static void sd_create_errstats(struct sd_lun *un, int instance); 1121 static void sd_set_errstats(struct sd_lun *un); 1122 static void sd_set_pstats(struct sd_lun *un); 1123 1124 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1125 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt); 1126 static int sd_send_polled_RQS(struct sd_lun *un); 1127 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt); 1128 1129 #if (defined(__fibre)) 1130 /* 1131 * Event callbacks (photon) 1132 */ 1133 static void sd_init_event_callbacks(struct sd_lun *un); 1134 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *); 1135 #endif 1136 1137 1138 static int sd_disable_caching(struct sd_lun *un); 1139 static dev_t sd_make_device(dev_info_t *devi); 1140 1141 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize, 1142 uint64_t capacity); 1143 1144 /* 1145 * Driver entry point functions. 1146 */ 1147 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p); 1148 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p); 1149 static int sd_ready_and_valid(struct sd_lun *un); 1150 1151 static void sdmin(struct buf *bp); 1152 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p); 1153 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p); 1154 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1155 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1156 1157 static int sdstrategy(struct buf *bp); 1158 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1159 1160 /* 1161 * Function prototypes for layering functions in the iostart chain. 1162 */ 1163 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un, 1164 struct buf *bp); 1165 static void sd_mapblocksize_iostart(int index, struct sd_lun *un, 1166 struct buf *bp); 1167 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp); 1168 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un, 1169 struct buf *bp); 1170 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp); 1171 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp); 1172 1173 /* 1174 * Function prototypes for layering functions in the iodone chain. 1175 */ 1176 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp); 1177 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp); 1178 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un, 1179 struct buf *bp); 1180 static void sd_mapblocksize_iodone(int index, struct sd_lun *un, 1181 struct buf *bp); 1182 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp); 1183 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un, 1184 struct buf *bp); 1185 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp); 1186 1187 /* 1188 * Prototypes for functions to support buf(9S) based IO. 1189 */ 1190 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg); 1191 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **); 1192 static void sd_destroypkt_for_buf(struct buf *); 1193 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp, 1194 struct buf *bp, int flags, 1195 int (*callback)(caddr_t), caddr_t callback_arg, 1196 diskaddr_t lba, uint32_t blockcount); 1197 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp, 1198 struct buf *bp, diskaddr_t lba, uint32_t blockcount); 1199 1200 /* 1201 * Prototypes for functions to support USCSI IO. 1202 */ 1203 static int sd_uscsi_strategy(struct buf *bp); 1204 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **); 1205 static void sd_destroypkt_for_uscsi(struct buf *); 1206 1207 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 1208 uchar_t chain_type, void *pktinfop); 1209 1210 static int sd_pm_entry(struct sd_lun *un); 1211 static void sd_pm_exit(struct sd_lun *un); 1212 1213 static void sd_pm_idletimeout_handler(void *arg); 1214 1215 /* 1216 * sd_core internal functions (used at the sd_core_io layer). 1217 */ 1218 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp); 1219 static void sdintr(struct scsi_pkt *pktp); 1220 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp); 1221 1222 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 1223 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 1224 int path_flag); 1225 1226 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen, 1227 daddr_t blkno, int (*func)(struct buf *)); 1228 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen, 1229 uint_t bflags, daddr_t blkno, int (*func)(struct buf *)); 1230 static void sd_bioclone_free(struct buf *bp); 1231 static void sd_shadow_buf_free(struct buf *bp); 1232 1233 static void sd_print_transport_rejected_message(struct sd_lun *un, 1234 struct sd_xbuf *xp, int code); 1235 1236 static void sd_retry_command(struct sd_lun *un, struct buf *bp, 1237 int retry_check_flag, 1238 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, 1239 int c), 1240 void *user_arg, int failure_code, clock_t retry_delay, 1241 void (*statp)(kstat_io_t *)); 1242 1243 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp, 1244 clock_t retry_delay, void (*statp)(kstat_io_t *)); 1245 1246 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 1247 struct scsi_pkt *pktp); 1248 static void sd_start_retry_command(void *arg); 1249 static void sd_start_direct_priority_command(void *arg); 1250 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp, 1251 int errcode); 1252 static void sd_return_failed_command_no_restart(struct sd_lun *un, 1253 struct buf *bp, int errcode); 1254 static void sd_return_command(struct sd_lun *un, struct buf *bp); 1255 static void sd_sync_with_callback(struct sd_lun *un); 1256 static int sdrunout(caddr_t arg); 1257 1258 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp); 1259 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp); 1260 1261 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type); 1262 static void sd_restore_throttle(void *arg); 1263 1264 static void sd_init_cdb_limits(struct sd_lun *un); 1265 1266 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 1267 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1268 1269 /* 1270 * Error handling functions 1271 */ 1272 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 1273 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1274 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, 1275 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1276 static void sd_pkt_status_reservation_conflict(struct sd_lun *un, 1277 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1278 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 1279 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1280 1281 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp, 1282 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1283 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 1284 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1285 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp, 1286 struct sd_xbuf *xp); 1287 static void sd_decode_sense(struct sd_lun *un, struct buf *bp, 1288 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1289 1290 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp, 1291 void *arg, int code); 1292 static diskaddr_t sd_extract_sense_info_descr( 1293 struct scsi_descr_sense_hdr *sdsp); 1294 1295 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 1296 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1297 static void sd_sense_key_recoverable_error(struct sd_lun *un, 1298 uint8_t asc, 1299 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1300 static void sd_sense_key_not_ready(struct sd_lun *un, 1301 uint8_t asc, uint8_t ascq, 1302 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1303 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 1304 int sense_key, uint8_t asc, 1305 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1306 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 1307 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1308 static void sd_sense_key_unit_attention(struct sd_lun *un, 1309 uint8_t asc, 1310 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1311 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 1312 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1313 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 1314 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1315 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 1316 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1317 static void sd_sense_key_default(struct sd_lun *un, 1318 int sense_key, 1319 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1320 1321 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp, 1322 void *arg, int flag); 1323 1324 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 1325 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1326 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 1327 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1328 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 1329 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1330 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 1331 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1332 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 1333 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1334 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 1335 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1336 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 1337 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1338 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 1339 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1340 1341 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp); 1342 1343 static void sd_start_stop_unit_callback(void *arg); 1344 static void sd_start_stop_unit_task(void *arg); 1345 1346 static void sd_taskq_create(void); 1347 static void sd_taskq_delete(void); 1348 static void sd_media_change_task(void *arg); 1349 1350 static int sd_handle_mchange(struct sd_lun *un); 1351 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag); 1352 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, 1353 uint32_t *lbap, int path_flag); 1354 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 1355 uint32_t *lbap, int path_flag); 1356 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, 1357 int path_flag); 1358 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, 1359 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp); 1360 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag); 1361 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, 1362 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp); 1363 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, 1364 uchar_t usr_cmd, uchar_t *usr_bufp); 1365 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un); 1366 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, 1367 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1368 uchar_t *bufaddr, uint_t buflen); 1369 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 1370 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1371 uchar_t *bufaddr, uint_t buflen, char feature); 1372 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, 1373 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag); 1374 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, 1375 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag); 1376 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 1377 size_t buflen, daddr_t start_block, int path_flag); 1378 #define sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag) \ 1379 sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \ 1380 path_flag) 1381 #define sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag) \ 1382 sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\ 1383 path_flag) 1384 1385 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, 1386 uint16_t buflen, uchar_t page_code, uchar_t page_control, 1387 uint16_t param_ptr, int path_flag); 1388 1389 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un); 1390 static void sd_free_rqs(struct sd_lun *un); 1391 1392 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, 1393 uchar_t *data, int len, int fmt); 1394 1395 /* 1396 * Disk Ioctl Function Prototypes 1397 */ 1398 static int sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag); 1399 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag); 1400 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag); 1401 static int sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, 1402 int geom_validated); 1403 static int sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag); 1404 static int sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, 1405 int geom_validated); 1406 static int sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag); 1407 static int sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, 1408 int geom_validated); 1409 static int sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag); 1410 static int sd_dkio_partition(dev_t dev, caddr_t arg, int flag); 1411 static void sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1412 static int sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag); 1413 static int sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag); 1414 static int sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1415 static int sd_write_label(dev_t dev); 1416 static int sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl); 1417 static void sd_clear_vtoc(struct sd_lun *un); 1418 static void sd_clear_efi(struct sd_lun *un); 1419 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag); 1420 static int sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag); 1421 static int sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag); 1422 static void sd_setup_default_geometry(struct sd_lun *un); 1423 #if defined(__i386) || defined(__amd64) 1424 static int sd_update_fdisk_and_vtoc(struct sd_lun *un); 1425 #endif 1426 1427 /* 1428 * Multi-host Ioctl Prototypes 1429 */ 1430 static int sd_check_mhd(dev_t dev, int interval); 1431 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1432 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt); 1433 static char *sd_sname(uchar_t status); 1434 static void sd_mhd_resvd_recover(void *arg); 1435 static void sd_resv_reclaim_thread(); 1436 static int sd_take_ownership(dev_t dev, struct mhioctkown *p); 1437 static int sd_reserve_release(dev_t dev, int cmd); 1438 static void sd_rmv_resv_reclaim_req(dev_t dev); 1439 static void sd_mhd_reset_notify_cb(caddr_t arg); 1440 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un, 1441 mhioc_inkeys_t *usrp, int flag); 1442 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un, 1443 mhioc_inresvs_t *usrp, int flag); 1444 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag); 1445 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag); 1446 static int sd_mhdioc_release(dev_t dev); 1447 static int sd_mhdioc_register_devid(dev_t dev); 1448 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag); 1449 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag); 1450 1451 /* 1452 * SCSI removable prototypes 1453 */ 1454 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag); 1455 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1456 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1457 static int sr_pause_resume(dev_t dev, int mode); 1458 static int sr_play_msf(dev_t dev, caddr_t data, int flag); 1459 static int sr_play_trkind(dev_t dev, caddr_t data, int flag); 1460 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag); 1461 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag); 1462 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag); 1463 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag); 1464 static int sr_read_cdda(dev_t dev, caddr_t data, int flag); 1465 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag); 1466 static int sr_read_mode1(dev_t dev, caddr_t data, int flag); 1467 static int sr_read_mode2(dev_t dev, caddr_t data, int flag); 1468 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag); 1469 static int sr_sector_mode(dev_t dev, uint32_t blksize); 1470 static int sr_eject(dev_t dev); 1471 static void sr_ejected(register struct sd_lun *un); 1472 static int sr_check_wp(dev_t dev); 1473 static int sd_check_media(dev_t dev, enum dkio_state state); 1474 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1475 static void sd_delayed_cv_broadcast(void *arg); 1476 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag); 1477 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag); 1478 1479 static int sd_log_page_supported(struct sd_lun *un, int log_page); 1480 1481 /* 1482 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions. 1483 */ 1484 static void sd_check_for_writable_cd(struct sd_lun *un); 1485 static int sd_wm_cache_constructor(void *wm, void *un, int flags); 1486 static void sd_wm_cache_destructor(void *wm, void *un); 1487 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb, 1488 daddr_t endb, ushort_t typ); 1489 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb, 1490 daddr_t endb); 1491 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp); 1492 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm); 1493 static void sd_read_modify_write_task(void * arg); 1494 static int 1495 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 1496 struct buf **bpp); 1497 1498 1499 /* 1500 * Function prototypes for failfast support. 1501 */ 1502 static void sd_failfast_flushq(struct sd_lun *un); 1503 static int sd_failfast_flushq_callback(struct buf *bp); 1504 1505 /* 1506 * Function prototypes for x86 support 1507 */ 1508 #if defined(__i386) || defined(__amd64) 1509 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 1510 struct scsi_pkt *pkt, struct sd_xbuf *xp); 1511 #endif 1512 1513 /* 1514 * Constants for failfast support: 1515 * 1516 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO 1517 * failfast processing being performed. 1518 * 1519 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing 1520 * failfast processing on all bufs with B_FAILFAST set. 1521 */ 1522 1523 #define SD_FAILFAST_INACTIVE 0 1524 #define SD_FAILFAST_ACTIVE 1 1525 1526 /* 1527 * Bitmask to control behavior of buf(9S) flushes when a transition to 1528 * the failfast state occurs. Optional bits include: 1529 * 1530 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that 1531 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will 1532 * be flushed. 1533 * 1534 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the 1535 * driver, in addition to the regular wait queue. This includes the xbuf 1536 * queues. When clear, only the driver's wait queue will be flushed. 1537 */ 1538 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01 1539 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02 1540 1541 /* 1542 * The default behavior is to only flush bufs that have B_FAILFAST set, but 1543 * to flush all queues within the driver. 1544 */ 1545 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES; 1546 1547 1548 /* 1549 * SD Testing Fault Injection 1550 */ 1551 #ifdef SD_FAULT_INJECTION 1552 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un); 1553 static void sd_faultinjection(struct scsi_pkt *pktp); 1554 static void sd_injection_log(char *buf, struct sd_lun *un); 1555 #endif 1556 1557 /* 1558 * Device driver ops vector 1559 */ 1560 static struct cb_ops sd_cb_ops = { 1561 sdopen, /* open */ 1562 sdclose, /* close */ 1563 sdstrategy, /* strategy */ 1564 nodev, /* print */ 1565 sddump, /* dump */ 1566 sdread, /* read */ 1567 sdwrite, /* write */ 1568 sdioctl, /* ioctl */ 1569 nodev, /* devmap */ 1570 nodev, /* mmap */ 1571 nodev, /* segmap */ 1572 nochpoll, /* poll */ 1573 sd_prop_op, /* cb_prop_op */ 1574 0, /* streamtab */ 1575 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */ 1576 CB_REV, /* cb_rev */ 1577 sdaread, /* async I/O read entry point */ 1578 sdawrite /* async I/O write entry point */ 1579 }; 1580 1581 static struct dev_ops sd_ops = { 1582 DEVO_REV, /* devo_rev, */ 1583 0, /* refcnt */ 1584 sdinfo, /* info */ 1585 nulldev, /* identify */ 1586 sdprobe, /* probe */ 1587 sdattach, /* attach */ 1588 sddetach, /* detach */ 1589 nodev, /* reset */ 1590 &sd_cb_ops, /* driver operations */ 1591 NULL, /* bus operations */ 1592 sdpower /* power */ 1593 }; 1594 1595 1596 /* 1597 * This is the loadable module wrapper. 1598 */ 1599 #include <sys/modctl.h> 1600 1601 static struct modldrv modldrv = { 1602 &mod_driverops, /* Type of module. This one is a driver */ 1603 SD_MODULE_NAME, /* Module name. */ 1604 &sd_ops /* driver ops */ 1605 }; 1606 1607 1608 static struct modlinkage modlinkage = { 1609 MODREV_1, 1610 &modldrv, 1611 NULL 1612 }; 1613 1614 1615 static struct scsi_asq_key_strings sd_additional_codes[] = { 1616 0x81, 0, "Logical Unit is Reserved", 1617 0x85, 0, "Audio Address Not Valid", 1618 0xb6, 0, "Media Load Mechanism Failed", 1619 0xB9, 0, "Audio Play Operation Aborted", 1620 0xbf, 0, "Buffer Overflow for Read All Subcodes Command", 1621 0x53, 2, "Medium removal prevented", 1622 0x6f, 0, "Authentication failed during key exchange", 1623 0x6f, 1, "Key not present", 1624 0x6f, 2, "Key not established", 1625 0x6f, 3, "Read without proper authentication", 1626 0x6f, 4, "Mismatched region to this logical unit", 1627 0x6f, 5, "Region reset count error", 1628 0xffff, 0x0, NULL 1629 }; 1630 1631 1632 /* 1633 * Struct for passing printing information for sense data messages 1634 */ 1635 struct sd_sense_info { 1636 int ssi_severity; 1637 int ssi_pfa_flag; 1638 }; 1639 1640 /* 1641 * Table of function pointers for iostart-side routines. Seperate "chains" 1642 * of layered function calls are formed by placing the function pointers 1643 * sequentially in the desired order. Functions are called according to an 1644 * incrementing table index ordering. The last function in each chain must 1645 * be sd_core_iostart(). The corresponding iodone-side routines are expected 1646 * in the sd_iodone_chain[] array. 1647 * 1648 * Note: It may seem more natural to organize both the iostart and iodone 1649 * functions together, into an array of structures (or some similar 1650 * organization) with a common index, rather than two seperate arrays which 1651 * must be maintained in synchronization. The purpose of this division is 1652 * to achiece improved performance: individual arrays allows for more 1653 * effective cache line utilization on certain platforms. 1654 */ 1655 1656 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp); 1657 1658 1659 static sd_chain_t sd_iostart_chain[] = { 1660 1661 /* Chain for buf IO for disk drive targets (PM enabled) */ 1662 sd_mapblockaddr_iostart, /* Index: 0 */ 1663 sd_pm_iostart, /* Index: 1 */ 1664 sd_core_iostart, /* Index: 2 */ 1665 1666 /* Chain for buf IO for disk drive targets (PM disabled) */ 1667 sd_mapblockaddr_iostart, /* Index: 3 */ 1668 sd_core_iostart, /* Index: 4 */ 1669 1670 /* Chain for buf IO for removable-media targets (PM enabled) */ 1671 sd_mapblockaddr_iostart, /* Index: 5 */ 1672 sd_mapblocksize_iostart, /* Index: 6 */ 1673 sd_pm_iostart, /* Index: 7 */ 1674 sd_core_iostart, /* Index: 8 */ 1675 1676 /* Chain for buf IO for removable-media targets (PM disabled) */ 1677 sd_mapblockaddr_iostart, /* Index: 9 */ 1678 sd_mapblocksize_iostart, /* Index: 10 */ 1679 sd_core_iostart, /* Index: 11 */ 1680 1681 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1682 sd_mapblockaddr_iostart, /* Index: 12 */ 1683 sd_checksum_iostart, /* Index: 13 */ 1684 sd_pm_iostart, /* Index: 14 */ 1685 sd_core_iostart, /* Index: 15 */ 1686 1687 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1688 sd_mapblockaddr_iostart, /* Index: 16 */ 1689 sd_checksum_iostart, /* Index: 17 */ 1690 sd_core_iostart, /* Index: 18 */ 1691 1692 /* Chain for USCSI commands (all targets) */ 1693 sd_pm_iostart, /* Index: 19 */ 1694 sd_core_iostart, /* Index: 20 */ 1695 1696 /* Chain for checksumming USCSI commands (all targets) */ 1697 sd_checksum_uscsi_iostart, /* Index: 21 */ 1698 sd_pm_iostart, /* Index: 22 */ 1699 sd_core_iostart, /* Index: 23 */ 1700 1701 /* Chain for "direct" USCSI commands (all targets) */ 1702 sd_core_iostart, /* Index: 24 */ 1703 1704 /* Chain for "direct priority" USCSI commands (all targets) */ 1705 sd_core_iostart, /* Index: 25 */ 1706 }; 1707 1708 /* 1709 * Macros to locate the first function of each iostart chain in the 1710 * sd_iostart_chain[] array. These are located by the index in the array. 1711 */ 1712 #define SD_CHAIN_DISK_IOSTART 0 1713 #define SD_CHAIN_DISK_IOSTART_NO_PM 3 1714 #define SD_CHAIN_RMMEDIA_IOSTART 5 1715 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9 1716 #define SD_CHAIN_CHKSUM_IOSTART 12 1717 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16 1718 #define SD_CHAIN_USCSI_CMD_IOSTART 19 1719 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21 1720 #define SD_CHAIN_DIRECT_CMD_IOSTART 24 1721 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25 1722 1723 1724 /* 1725 * Table of function pointers for the iodone-side routines for the driver- 1726 * internal layering mechanism. The calling sequence for iodone routines 1727 * uses a decrementing table index, so the last routine called in a chain 1728 * must be at the lowest array index location for that chain. The last 1729 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs) 1730 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering 1731 * of the functions in an iodone side chain must correspond to the ordering 1732 * of the iostart routines for that chain. Note that there is no iodone 1733 * side routine that corresponds to sd_core_iostart(), so there is no 1734 * entry in the table for this. 1735 */ 1736 1737 static sd_chain_t sd_iodone_chain[] = { 1738 1739 /* Chain for buf IO for disk drive targets (PM enabled) */ 1740 sd_buf_iodone, /* Index: 0 */ 1741 sd_mapblockaddr_iodone, /* Index: 1 */ 1742 sd_pm_iodone, /* Index: 2 */ 1743 1744 /* Chain for buf IO for disk drive targets (PM disabled) */ 1745 sd_buf_iodone, /* Index: 3 */ 1746 sd_mapblockaddr_iodone, /* Index: 4 */ 1747 1748 /* Chain for buf IO for removable-media targets (PM enabled) */ 1749 sd_buf_iodone, /* Index: 5 */ 1750 sd_mapblockaddr_iodone, /* Index: 6 */ 1751 sd_mapblocksize_iodone, /* Index: 7 */ 1752 sd_pm_iodone, /* Index: 8 */ 1753 1754 /* Chain for buf IO for removable-media targets (PM disabled) */ 1755 sd_buf_iodone, /* Index: 9 */ 1756 sd_mapblockaddr_iodone, /* Index: 10 */ 1757 sd_mapblocksize_iodone, /* Index: 11 */ 1758 1759 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1760 sd_buf_iodone, /* Index: 12 */ 1761 sd_mapblockaddr_iodone, /* Index: 13 */ 1762 sd_checksum_iodone, /* Index: 14 */ 1763 sd_pm_iodone, /* Index: 15 */ 1764 1765 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1766 sd_buf_iodone, /* Index: 16 */ 1767 sd_mapblockaddr_iodone, /* Index: 17 */ 1768 sd_checksum_iodone, /* Index: 18 */ 1769 1770 /* Chain for USCSI commands (non-checksum targets) */ 1771 sd_uscsi_iodone, /* Index: 19 */ 1772 sd_pm_iodone, /* Index: 20 */ 1773 1774 /* Chain for USCSI commands (checksum targets) */ 1775 sd_uscsi_iodone, /* Index: 21 */ 1776 sd_checksum_uscsi_iodone, /* Index: 22 */ 1777 sd_pm_iodone, /* Index: 22 */ 1778 1779 /* Chain for "direct" USCSI commands (all targets) */ 1780 sd_uscsi_iodone, /* Index: 24 */ 1781 1782 /* Chain for "direct priority" USCSI commands (all targets) */ 1783 sd_uscsi_iodone, /* Index: 25 */ 1784 }; 1785 1786 1787 /* 1788 * Macros to locate the "first" function in the sd_iodone_chain[] array for 1789 * each iodone-side chain. These are located by the array index, but as the 1790 * iodone side functions are called in a decrementing-index order, the 1791 * highest index number in each chain must be specified (as these correspond 1792 * to the first function in the iodone chain that will be called by the core 1793 * at IO completion time). 1794 */ 1795 1796 #define SD_CHAIN_DISK_IODONE 2 1797 #define SD_CHAIN_DISK_IODONE_NO_PM 4 1798 #define SD_CHAIN_RMMEDIA_IODONE 8 1799 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11 1800 #define SD_CHAIN_CHKSUM_IODONE 15 1801 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18 1802 #define SD_CHAIN_USCSI_CMD_IODONE 20 1803 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22 1804 #define SD_CHAIN_DIRECT_CMD_IODONE 24 1805 #define SD_CHAIN_PRIORITY_CMD_IODONE 25 1806 1807 1808 1809 1810 /* 1811 * Array to map a layering chain index to the appropriate initpkt routine. 1812 * The redundant entries are present so that the index used for accessing 1813 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1814 * with this table as well. 1815 */ 1816 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **); 1817 1818 static sd_initpkt_t sd_initpkt_map[] = { 1819 1820 /* Chain for buf IO for disk drive targets (PM enabled) */ 1821 sd_initpkt_for_buf, /* Index: 0 */ 1822 sd_initpkt_for_buf, /* Index: 1 */ 1823 sd_initpkt_for_buf, /* Index: 2 */ 1824 1825 /* Chain for buf IO for disk drive targets (PM disabled) */ 1826 sd_initpkt_for_buf, /* Index: 3 */ 1827 sd_initpkt_for_buf, /* Index: 4 */ 1828 1829 /* Chain for buf IO for removable-media targets (PM enabled) */ 1830 sd_initpkt_for_buf, /* Index: 5 */ 1831 sd_initpkt_for_buf, /* Index: 6 */ 1832 sd_initpkt_for_buf, /* Index: 7 */ 1833 sd_initpkt_for_buf, /* Index: 8 */ 1834 1835 /* Chain for buf IO for removable-media targets (PM disabled) */ 1836 sd_initpkt_for_buf, /* Index: 9 */ 1837 sd_initpkt_for_buf, /* Index: 10 */ 1838 sd_initpkt_for_buf, /* Index: 11 */ 1839 1840 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1841 sd_initpkt_for_buf, /* Index: 12 */ 1842 sd_initpkt_for_buf, /* Index: 13 */ 1843 sd_initpkt_for_buf, /* Index: 14 */ 1844 sd_initpkt_for_buf, /* Index: 15 */ 1845 1846 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1847 sd_initpkt_for_buf, /* Index: 16 */ 1848 sd_initpkt_for_buf, /* Index: 17 */ 1849 sd_initpkt_for_buf, /* Index: 18 */ 1850 1851 /* Chain for USCSI commands (non-checksum targets) */ 1852 sd_initpkt_for_uscsi, /* Index: 19 */ 1853 sd_initpkt_for_uscsi, /* Index: 20 */ 1854 1855 /* Chain for USCSI commands (checksum targets) */ 1856 sd_initpkt_for_uscsi, /* Index: 21 */ 1857 sd_initpkt_for_uscsi, /* Index: 22 */ 1858 sd_initpkt_for_uscsi, /* Index: 22 */ 1859 1860 /* Chain for "direct" USCSI commands (all targets) */ 1861 sd_initpkt_for_uscsi, /* Index: 24 */ 1862 1863 /* Chain for "direct priority" USCSI commands (all targets) */ 1864 sd_initpkt_for_uscsi, /* Index: 25 */ 1865 1866 }; 1867 1868 1869 /* 1870 * Array to map a layering chain index to the appropriate destroypktpkt routine. 1871 * The redundant entries are present so that the index used for accessing 1872 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1873 * with this table as well. 1874 */ 1875 typedef void (*sd_destroypkt_t)(struct buf *); 1876 1877 static sd_destroypkt_t sd_destroypkt_map[] = { 1878 1879 /* Chain for buf IO for disk drive targets (PM enabled) */ 1880 sd_destroypkt_for_buf, /* Index: 0 */ 1881 sd_destroypkt_for_buf, /* Index: 1 */ 1882 sd_destroypkt_for_buf, /* Index: 2 */ 1883 1884 /* Chain for buf IO for disk drive targets (PM disabled) */ 1885 sd_destroypkt_for_buf, /* Index: 3 */ 1886 sd_destroypkt_for_buf, /* Index: 4 */ 1887 1888 /* Chain for buf IO for removable-media targets (PM enabled) */ 1889 sd_destroypkt_for_buf, /* Index: 5 */ 1890 sd_destroypkt_for_buf, /* Index: 6 */ 1891 sd_destroypkt_for_buf, /* Index: 7 */ 1892 sd_destroypkt_for_buf, /* Index: 8 */ 1893 1894 /* Chain for buf IO for removable-media targets (PM disabled) */ 1895 sd_destroypkt_for_buf, /* Index: 9 */ 1896 sd_destroypkt_for_buf, /* Index: 10 */ 1897 sd_destroypkt_for_buf, /* Index: 11 */ 1898 1899 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1900 sd_destroypkt_for_buf, /* Index: 12 */ 1901 sd_destroypkt_for_buf, /* Index: 13 */ 1902 sd_destroypkt_for_buf, /* Index: 14 */ 1903 sd_destroypkt_for_buf, /* Index: 15 */ 1904 1905 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1906 sd_destroypkt_for_buf, /* Index: 16 */ 1907 sd_destroypkt_for_buf, /* Index: 17 */ 1908 sd_destroypkt_for_buf, /* Index: 18 */ 1909 1910 /* Chain for USCSI commands (non-checksum targets) */ 1911 sd_destroypkt_for_uscsi, /* Index: 19 */ 1912 sd_destroypkt_for_uscsi, /* Index: 20 */ 1913 1914 /* Chain for USCSI commands (checksum targets) */ 1915 sd_destroypkt_for_uscsi, /* Index: 21 */ 1916 sd_destroypkt_for_uscsi, /* Index: 22 */ 1917 sd_destroypkt_for_uscsi, /* Index: 22 */ 1918 1919 /* Chain for "direct" USCSI commands (all targets) */ 1920 sd_destroypkt_for_uscsi, /* Index: 24 */ 1921 1922 /* Chain for "direct priority" USCSI commands (all targets) */ 1923 sd_destroypkt_for_uscsi, /* Index: 25 */ 1924 1925 }; 1926 1927 1928 1929 /* 1930 * Array to map a layering chain index to the appropriate chain "type". 1931 * The chain type indicates a specific property/usage of the chain. 1932 * The redundant entries are present so that the index used for accessing 1933 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1934 * with this table as well. 1935 */ 1936 1937 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */ 1938 #define SD_CHAIN_BUFIO 1 /* regular buf IO */ 1939 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */ 1940 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */ 1941 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */ 1942 /* (for error recovery) */ 1943 1944 static int sd_chain_type_map[] = { 1945 1946 /* Chain for buf IO for disk drive targets (PM enabled) */ 1947 SD_CHAIN_BUFIO, /* Index: 0 */ 1948 SD_CHAIN_BUFIO, /* Index: 1 */ 1949 SD_CHAIN_BUFIO, /* Index: 2 */ 1950 1951 /* Chain for buf IO for disk drive targets (PM disabled) */ 1952 SD_CHAIN_BUFIO, /* Index: 3 */ 1953 SD_CHAIN_BUFIO, /* Index: 4 */ 1954 1955 /* Chain for buf IO for removable-media targets (PM enabled) */ 1956 SD_CHAIN_BUFIO, /* Index: 5 */ 1957 SD_CHAIN_BUFIO, /* Index: 6 */ 1958 SD_CHAIN_BUFIO, /* Index: 7 */ 1959 SD_CHAIN_BUFIO, /* Index: 8 */ 1960 1961 /* Chain for buf IO for removable-media targets (PM disabled) */ 1962 SD_CHAIN_BUFIO, /* Index: 9 */ 1963 SD_CHAIN_BUFIO, /* Index: 10 */ 1964 SD_CHAIN_BUFIO, /* Index: 11 */ 1965 1966 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1967 SD_CHAIN_BUFIO, /* Index: 12 */ 1968 SD_CHAIN_BUFIO, /* Index: 13 */ 1969 SD_CHAIN_BUFIO, /* Index: 14 */ 1970 SD_CHAIN_BUFIO, /* Index: 15 */ 1971 1972 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1973 SD_CHAIN_BUFIO, /* Index: 16 */ 1974 SD_CHAIN_BUFIO, /* Index: 17 */ 1975 SD_CHAIN_BUFIO, /* Index: 18 */ 1976 1977 /* Chain for USCSI commands (non-checksum targets) */ 1978 SD_CHAIN_USCSI, /* Index: 19 */ 1979 SD_CHAIN_USCSI, /* Index: 20 */ 1980 1981 /* Chain for USCSI commands (checksum targets) */ 1982 SD_CHAIN_USCSI, /* Index: 21 */ 1983 SD_CHAIN_USCSI, /* Index: 22 */ 1984 SD_CHAIN_USCSI, /* Index: 22 */ 1985 1986 /* Chain for "direct" USCSI commands (all targets) */ 1987 SD_CHAIN_DIRECT, /* Index: 24 */ 1988 1989 /* Chain for "direct priority" USCSI commands (all targets) */ 1990 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */ 1991 }; 1992 1993 1994 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */ 1995 #define SD_IS_BUFIO(xp) \ 1996 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO) 1997 1998 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */ 1999 #define SD_IS_DIRECT_PRIORITY(xp) \ 2000 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY) 2001 2002 2003 2004 /* 2005 * Struct, array, and macros to map a specific chain to the appropriate 2006 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays. 2007 * 2008 * The sd_chain_index_map[] array is used at attach time to set the various 2009 * un_xxx_chain type members of the sd_lun softstate to the specific layering 2010 * chain to be used with the instance. This allows different instances to use 2011 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart 2012 * and xb_chain_iodone index values in the sd_xbuf are initialized to these 2013 * values at sd_xbuf init time, this allows (1) layering chains may be changed 2014 * dynamically & without the use of locking; and (2) a layer may update the 2015 * xb_chain_io[start|done] member in a given xbuf with its current index value, 2016 * to allow for deferred processing of an IO within the same chain from a 2017 * different execution context. 2018 */ 2019 2020 struct sd_chain_index { 2021 int sci_iostart_index; 2022 int sci_iodone_index; 2023 }; 2024 2025 static struct sd_chain_index sd_chain_index_map[] = { 2026 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE }, 2027 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM }, 2028 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE }, 2029 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM }, 2030 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE }, 2031 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM }, 2032 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE }, 2033 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE }, 2034 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE }, 2035 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE }, 2036 }; 2037 2038 2039 /* 2040 * The following are indexes into the sd_chain_index_map[] array. 2041 */ 2042 2043 /* un->un_buf_chain_type must be set to one of these */ 2044 #define SD_CHAIN_INFO_DISK 0 2045 #define SD_CHAIN_INFO_DISK_NO_PM 1 2046 #define SD_CHAIN_INFO_RMMEDIA 2 2047 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3 2048 #define SD_CHAIN_INFO_CHKSUM 4 2049 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5 2050 2051 /* un->un_uscsi_chain_type must be set to one of these */ 2052 #define SD_CHAIN_INFO_USCSI_CMD 6 2053 /* USCSI with PM disabled is the same as DIRECT */ 2054 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8 2055 #define SD_CHAIN_INFO_USCSI_CHKSUM 7 2056 2057 /* un->un_direct_chain_type must be set to one of these */ 2058 #define SD_CHAIN_INFO_DIRECT_CMD 8 2059 2060 /* un->un_priority_chain_type must be set to one of these */ 2061 #define SD_CHAIN_INFO_PRIORITY_CMD 9 2062 2063 /* size for devid inquiries */ 2064 #define MAX_INQUIRY_SIZE 0xF0 2065 2066 /* 2067 * Macros used by functions to pass a given buf(9S) struct along to the 2068 * next function in the layering chain for further processing. 2069 * 2070 * In the following macros, passing more than three arguments to the called 2071 * routines causes the optimizer for the SPARC compiler to stop doing tail 2072 * call elimination which results in significant performance degradation. 2073 */ 2074 #define SD_BEGIN_IOSTART(index, un, bp) \ 2075 ((*(sd_iostart_chain[index]))(index, un, bp)) 2076 2077 #define SD_BEGIN_IODONE(index, un, bp) \ 2078 ((*(sd_iodone_chain[index]))(index, un, bp)) 2079 2080 #define SD_NEXT_IOSTART(index, un, bp) \ 2081 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp)) 2082 2083 #define SD_NEXT_IODONE(index, un, bp) \ 2084 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp)) 2085 2086 2087 /* 2088 * Function: _init 2089 * 2090 * Description: This is the driver _init(9E) entry point. 2091 * 2092 * Return Code: Returns the value from mod_install(9F) or 2093 * ddi_soft_state_init(9F) as appropriate. 2094 * 2095 * Context: Called when driver module loaded. 2096 */ 2097 2098 int 2099 _init(void) 2100 { 2101 int err; 2102 2103 /* establish driver name from module name */ 2104 sd_label = mod_modname(&modlinkage); 2105 2106 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun), 2107 SD_MAXUNIT); 2108 2109 if (err != 0) { 2110 return (err); 2111 } 2112 2113 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL); 2114 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL); 2115 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL); 2116 2117 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL); 2118 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL); 2119 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL); 2120 2121 /* 2122 * it's ok to init here even for fibre device 2123 */ 2124 sd_scsi_probe_cache_init(); 2125 2126 /* 2127 * Creating taskq before mod_install ensures that all callers (threads) 2128 * that enter the module after a successfull mod_install encounter 2129 * a valid taskq. 2130 */ 2131 sd_taskq_create(); 2132 2133 err = mod_install(&modlinkage); 2134 if (err != 0) { 2135 /* delete taskq if install fails */ 2136 sd_taskq_delete(); 2137 2138 mutex_destroy(&sd_detach_mutex); 2139 mutex_destroy(&sd_log_mutex); 2140 mutex_destroy(&sd_label_mutex); 2141 2142 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2143 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2144 cv_destroy(&sd_tr.srq_inprocess_cv); 2145 2146 sd_scsi_probe_cache_fini(); 2147 2148 ddi_soft_state_fini(&sd_state); 2149 return (err); 2150 } 2151 2152 return (err); 2153 } 2154 2155 2156 /* 2157 * Function: _fini 2158 * 2159 * Description: This is the driver _fini(9E) entry point. 2160 * 2161 * Return Code: Returns the value from mod_remove(9F) 2162 * 2163 * Context: Called when driver module is unloaded. 2164 */ 2165 2166 int 2167 _fini(void) 2168 { 2169 int err; 2170 2171 if ((err = mod_remove(&modlinkage)) != 0) { 2172 return (err); 2173 } 2174 2175 sd_taskq_delete(); 2176 2177 mutex_destroy(&sd_detach_mutex); 2178 mutex_destroy(&sd_log_mutex); 2179 mutex_destroy(&sd_label_mutex); 2180 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2181 2182 sd_scsi_probe_cache_fini(); 2183 2184 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2185 cv_destroy(&sd_tr.srq_inprocess_cv); 2186 2187 ddi_soft_state_fini(&sd_state); 2188 2189 return (err); 2190 } 2191 2192 2193 /* 2194 * Function: _info 2195 * 2196 * Description: This is the driver _info(9E) entry point. 2197 * 2198 * Arguments: modinfop - pointer to the driver modinfo structure 2199 * 2200 * Return Code: Returns the value from mod_info(9F). 2201 * 2202 * Context: Kernel thread context 2203 */ 2204 2205 int 2206 _info(struct modinfo *modinfop) 2207 { 2208 return (mod_info(&modlinkage, modinfop)); 2209 } 2210 2211 2212 /* 2213 * The following routines implement the driver message logging facility. 2214 * They provide component- and level- based debug output filtering. 2215 * Output may also be restricted to messages for a single instance by 2216 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set 2217 * to NULL, then messages for all instances are printed. 2218 * 2219 * These routines have been cloned from each other due to the language 2220 * constraints of macros and variable argument list processing. 2221 */ 2222 2223 2224 /* 2225 * Function: sd_log_err 2226 * 2227 * Description: This routine is called by the SD_ERROR macro for debug 2228 * logging of error conditions. 2229 * 2230 * Arguments: comp - driver component being logged 2231 * dev - pointer to driver info structure 2232 * fmt - error string and format to be logged 2233 */ 2234 2235 static void 2236 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...) 2237 { 2238 va_list ap; 2239 dev_info_t *dev; 2240 2241 ASSERT(un != NULL); 2242 dev = SD_DEVINFO(un); 2243 ASSERT(dev != NULL); 2244 2245 /* 2246 * Filter messages based on the global component and level masks. 2247 * Also print if un matches the value of sd_debug_un, or if 2248 * sd_debug_un is set to NULL. 2249 */ 2250 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) && 2251 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2252 mutex_enter(&sd_log_mutex); 2253 va_start(ap, fmt); 2254 (void) vsprintf(sd_log_buf, fmt, ap); 2255 va_end(ap); 2256 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2257 mutex_exit(&sd_log_mutex); 2258 } 2259 #ifdef SD_FAULT_INJECTION 2260 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2261 if (un->sd_injection_mask & comp) { 2262 mutex_enter(&sd_log_mutex); 2263 va_start(ap, fmt); 2264 (void) vsprintf(sd_log_buf, fmt, ap); 2265 va_end(ap); 2266 sd_injection_log(sd_log_buf, un); 2267 mutex_exit(&sd_log_mutex); 2268 } 2269 #endif 2270 } 2271 2272 2273 /* 2274 * Function: sd_log_info 2275 * 2276 * Description: This routine is called by the SD_INFO macro for debug 2277 * logging of general purpose informational conditions. 2278 * 2279 * Arguments: comp - driver component being logged 2280 * dev - pointer to driver info structure 2281 * fmt - info string and format to be logged 2282 */ 2283 2284 static void 2285 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...) 2286 { 2287 va_list ap; 2288 dev_info_t *dev; 2289 2290 ASSERT(un != NULL); 2291 dev = SD_DEVINFO(un); 2292 ASSERT(dev != NULL); 2293 2294 /* 2295 * Filter messages based on the global component and level masks. 2296 * Also print if un matches the value of sd_debug_un, or if 2297 * sd_debug_un is set to NULL. 2298 */ 2299 if ((sd_component_mask & component) && 2300 (sd_level_mask & SD_LOGMASK_INFO) && 2301 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2302 mutex_enter(&sd_log_mutex); 2303 va_start(ap, fmt); 2304 (void) vsprintf(sd_log_buf, fmt, ap); 2305 va_end(ap); 2306 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2307 mutex_exit(&sd_log_mutex); 2308 } 2309 #ifdef SD_FAULT_INJECTION 2310 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2311 if (un->sd_injection_mask & component) { 2312 mutex_enter(&sd_log_mutex); 2313 va_start(ap, fmt); 2314 (void) vsprintf(sd_log_buf, fmt, ap); 2315 va_end(ap); 2316 sd_injection_log(sd_log_buf, un); 2317 mutex_exit(&sd_log_mutex); 2318 } 2319 #endif 2320 } 2321 2322 2323 /* 2324 * Function: sd_log_trace 2325 * 2326 * Description: This routine is called by the SD_TRACE macro for debug 2327 * logging of trace conditions (i.e. function entry/exit). 2328 * 2329 * Arguments: comp - driver component being logged 2330 * dev - pointer to driver info structure 2331 * fmt - trace string and format to be logged 2332 */ 2333 2334 static void 2335 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...) 2336 { 2337 va_list ap; 2338 dev_info_t *dev; 2339 2340 ASSERT(un != NULL); 2341 dev = SD_DEVINFO(un); 2342 ASSERT(dev != NULL); 2343 2344 /* 2345 * Filter messages based on the global component and level masks. 2346 * Also print if un matches the value of sd_debug_un, or if 2347 * sd_debug_un is set to NULL. 2348 */ 2349 if ((sd_component_mask & component) && 2350 (sd_level_mask & SD_LOGMASK_TRACE) && 2351 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2352 mutex_enter(&sd_log_mutex); 2353 va_start(ap, fmt); 2354 (void) vsprintf(sd_log_buf, fmt, ap); 2355 va_end(ap); 2356 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2357 mutex_exit(&sd_log_mutex); 2358 } 2359 #ifdef SD_FAULT_INJECTION 2360 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2361 if (un->sd_injection_mask & component) { 2362 mutex_enter(&sd_log_mutex); 2363 va_start(ap, fmt); 2364 (void) vsprintf(sd_log_buf, fmt, ap); 2365 va_end(ap); 2366 sd_injection_log(sd_log_buf, un); 2367 mutex_exit(&sd_log_mutex); 2368 } 2369 #endif 2370 } 2371 2372 2373 /* 2374 * Function: sdprobe 2375 * 2376 * Description: This is the driver probe(9e) entry point function. 2377 * 2378 * Arguments: devi - opaque device info handle 2379 * 2380 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful. 2381 * DDI_PROBE_FAILURE: If the probe failed. 2382 * DDI_PROBE_PARTIAL: If the instance is not present now, 2383 * but may be present in the future. 2384 */ 2385 2386 static int 2387 sdprobe(dev_info_t *devi) 2388 { 2389 struct scsi_device *devp; 2390 int rval; 2391 int instance; 2392 2393 /* 2394 * if it wasn't for pln, sdprobe could actually be nulldev 2395 * in the "__fibre" case. 2396 */ 2397 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 2398 return (DDI_PROBE_DONTCARE); 2399 } 2400 2401 devp = ddi_get_driver_private(devi); 2402 2403 if (devp == NULL) { 2404 /* Ooops... nexus driver is mis-configured... */ 2405 return (DDI_PROBE_FAILURE); 2406 } 2407 2408 instance = ddi_get_instance(devi); 2409 2410 if (ddi_get_soft_state(sd_state, instance) != NULL) { 2411 return (DDI_PROBE_PARTIAL); 2412 } 2413 2414 /* 2415 * Call the SCSA utility probe routine to see if we actually 2416 * have a target at this SCSI nexus. 2417 */ 2418 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) { 2419 case SCSIPROBE_EXISTS: 2420 switch (devp->sd_inq->inq_dtype) { 2421 case DTYPE_DIRECT: 2422 rval = DDI_PROBE_SUCCESS; 2423 break; 2424 case DTYPE_RODIRECT: 2425 /* CDs etc. Can be removable media */ 2426 rval = DDI_PROBE_SUCCESS; 2427 break; 2428 case DTYPE_OPTICAL: 2429 /* 2430 * Rewritable optical driver HP115AA 2431 * Can also be removable media 2432 */ 2433 2434 /* 2435 * Do not attempt to bind to DTYPE_OPTICAL if 2436 * pre solaris 9 sparc sd behavior is required 2437 * 2438 * If first time through and sd_dtype_optical_bind 2439 * has not been set in /etc/system check properties 2440 */ 2441 2442 if (sd_dtype_optical_bind < 0) { 2443 sd_dtype_optical_bind = ddi_prop_get_int 2444 (DDI_DEV_T_ANY, devi, 0, 2445 "optical-device-bind", 1); 2446 } 2447 2448 if (sd_dtype_optical_bind == 0) { 2449 rval = DDI_PROBE_FAILURE; 2450 } else { 2451 rval = DDI_PROBE_SUCCESS; 2452 } 2453 break; 2454 2455 case DTYPE_NOTPRESENT: 2456 default: 2457 rval = DDI_PROBE_FAILURE; 2458 break; 2459 } 2460 break; 2461 default: 2462 rval = DDI_PROBE_PARTIAL; 2463 break; 2464 } 2465 2466 /* 2467 * This routine checks for resource allocation prior to freeing, 2468 * so it will take care of the "smart probing" case where a 2469 * scsi_probe() may or may not have been issued and will *not* 2470 * free previously-freed resources. 2471 */ 2472 scsi_unprobe(devp); 2473 return (rval); 2474 } 2475 2476 2477 /* 2478 * Function: sdinfo 2479 * 2480 * Description: This is the driver getinfo(9e) entry point function. 2481 * Given the device number, return the devinfo pointer from 2482 * the scsi_device structure or the instance number 2483 * associated with the dev_t. 2484 * 2485 * Arguments: dip - pointer to device info structure 2486 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO, 2487 * DDI_INFO_DEVT2INSTANCE) 2488 * arg - driver dev_t 2489 * resultp - user buffer for request response 2490 * 2491 * Return Code: DDI_SUCCESS 2492 * DDI_FAILURE 2493 */ 2494 /* ARGSUSED */ 2495 static int 2496 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2497 { 2498 struct sd_lun *un; 2499 dev_t dev; 2500 int instance; 2501 int error; 2502 2503 switch (infocmd) { 2504 case DDI_INFO_DEVT2DEVINFO: 2505 dev = (dev_t)arg; 2506 instance = SDUNIT(dev); 2507 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 2508 return (DDI_FAILURE); 2509 } 2510 *result = (void *) SD_DEVINFO(un); 2511 error = DDI_SUCCESS; 2512 break; 2513 case DDI_INFO_DEVT2INSTANCE: 2514 dev = (dev_t)arg; 2515 instance = SDUNIT(dev); 2516 *result = (void *)(uintptr_t)instance; 2517 error = DDI_SUCCESS; 2518 break; 2519 default: 2520 error = DDI_FAILURE; 2521 } 2522 return (error); 2523 } 2524 2525 /* 2526 * Function: sd_prop_op 2527 * 2528 * Description: This is the driver prop_op(9e) entry point function. 2529 * Return the number of blocks for the partition in question 2530 * or forward the request to the property facilities. 2531 * 2532 * Arguments: dev - device number 2533 * dip - pointer to device info structure 2534 * prop_op - property operator 2535 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent 2536 * name - pointer to property name 2537 * valuep - pointer or address of the user buffer 2538 * lengthp - property length 2539 * 2540 * Return Code: DDI_PROP_SUCCESS 2541 * DDI_PROP_NOT_FOUND 2542 * DDI_PROP_UNDEFINED 2543 * DDI_PROP_NO_MEMORY 2544 * DDI_PROP_BUF_TOO_SMALL 2545 */ 2546 2547 static int 2548 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 2549 char *name, caddr_t valuep, int *lengthp) 2550 { 2551 int instance = ddi_get_instance(dip); 2552 struct sd_lun *un; 2553 uint64_t nblocks64; 2554 2555 /* 2556 * Our dynamic properties are all device specific and size oriented. 2557 * Requests issued under conditions where size is valid are passed 2558 * to ddi_prop_op_nblocks with the size information, otherwise the 2559 * request is passed to ddi_prop_op. Size depends on valid geometry. 2560 */ 2561 un = ddi_get_soft_state(sd_state, instance); 2562 if ((dev == DDI_DEV_T_ANY) || (un == NULL) || 2563 (un->un_f_geometry_is_valid == FALSE)) { 2564 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 2565 name, valuep, lengthp)); 2566 } else { 2567 /* get nblocks value */ 2568 ASSERT(!mutex_owned(SD_MUTEX(un))); 2569 mutex_enter(SD_MUTEX(un)); 2570 nblocks64 = (ulong_t)un->un_map[SDPART(dev)].dkl_nblk; 2571 mutex_exit(SD_MUTEX(un)); 2572 2573 return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags, 2574 name, valuep, lengthp, nblocks64)); 2575 } 2576 } 2577 2578 /* 2579 * The following functions are for smart probing: 2580 * sd_scsi_probe_cache_init() 2581 * sd_scsi_probe_cache_fini() 2582 * sd_scsi_clear_probe_cache() 2583 * sd_scsi_probe_with_cache() 2584 */ 2585 2586 /* 2587 * Function: sd_scsi_probe_cache_init 2588 * 2589 * Description: Initializes the probe response cache mutex and head pointer. 2590 * 2591 * Context: Kernel thread context 2592 */ 2593 2594 static void 2595 sd_scsi_probe_cache_init(void) 2596 { 2597 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL); 2598 sd_scsi_probe_cache_head = NULL; 2599 } 2600 2601 2602 /* 2603 * Function: sd_scsi_probe_cache_fini 2604 * 2605 * Description: Frees all resources associated with the probe response cache. 2606 * 2607 * Context: Kernel thread context 2608 */ 2609 2610 static void 2611 sd_scsi_probe_cache_fini(void) 2612 { 2613 struct sd_scsi_probe_cache *cp; 2614 struct sd_scsi_probe_cache *ncp; 2615 2616 /* Clean up our smart probing linked list */ 2617 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) { 2618 ncp = cp->next; 2619 kmem_free(cp, sizeof (struct sd_scsi_probe_cache)); 2620 } 2621 sd_scsi_probe_cache_head = NULL; 2622 mutex_destroy(&sd_scsi_probe_cache_mutex); 2623 } 2624 2625 2626 /* 2627 * Function: sd_scsi_clear_probe_cache 2628 * 2629 * Description: This routine clears the probe response cache. This is 2630 * done when open() returns ENXIO so that when deferred 2631 * attach is attempted (possibly after a device has been 2632 * turned on) we will retry the probe. Since we don't know 2633 * which target we failed to open, we just clear the 2634 * entire cache. 2635 * 2636 * Context: Kernel thread context 2637 */ 2638 2639 static void 2640 sd_scsi_clear_probe_cache(void) 2641 { 2642 struct sd_scsi_probe_cache *cp; 2643 int i; 2644 2645 mutex_enter(&sd_scsi_probe_cache_mutex); 2646 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2647 /* 2648 * Reset all entries to SCSIPROBE_EXISTS. This will 2649 * force probing to be performed the next time 2650 * sd_scsi_probe_with_cache is called. 2651 */ 2652 for (i = 0; i < NTARGETS_WIDE; i++) { 2653 cp->cache[i] = SCSIPROBE_EXISTS; 2654 } 2655 } 2656 mutex_exit(&sd_scsi_probe_cache_mutex); 2657 } 2658 2659 2660 /* 2661 * Function: sd_scsi_probe_with_cache 2662 * 2663 * Description: This routine implements support for a scsi device probe 2664 * with cache. The driver maintains a cache of the target 2665 * responses to scsi probes. If we get no response from a 2666 * target during a probe inquiry, we remember that, and we 2667 * avoid additional calls to scsi_probe on non-zero LUNs 2668 * on the same target until the cache is cleared. By doing 2669 * so we avoid the 1/4 sec selection timeout for nonzero 2670 * LUNs. lun0 of a target is always probed. 2671 * 2672 * Arguments: devp - Pointer to a scsi_device(9S) structure 2673 * waitfunc - indicates what the allocator routines should 2674 * do when resources are not available. This value 2675 * is passed on to scsi_probe() when that routine 2676 * is called. 2677 * 2678 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache; 2679 * otherwise the value returned by scsi_probe(9F). 2680 * 2681 * Context: Kernel thread context 2682 */ 2683 2684 static int 2685 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)()) 2686 { 2687 struct sd_scsi_probe_cache *cp; 2688 dev_info_t *pdip = ddi_get_parent(devp->sd_dev); 2689 int lun, tgt; 2690 2691 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2692 SCSI_ADDR_PROP_LUN, 0); 2693 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2694 SCSI_ADDR_PROP_TARGET, -1); 2695 2696 /* Make sure caching enabled and target in range */ 2697 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) { 2698 /* do it the old way (no cache) */ 2699 return (scsi_probe(devp, waitfn)); 2700 } 2701 2702 mutex_enter(&sd_scsi_probe_cache_mutex); 2703 2704 /* Find the cache for this scsi bus instance */ 2705 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2706 if (cp->pdip == pdip) { 2707 break; 2708 } 2709 } 2710 2711 /* If we can't find a cache for this pdip, create one */ 2712 if (cp == NULL) { 2713 int i; 2714 2715 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache), 2716 KM_SLEEP); 2717 cp->pdip = pdip; 2718 cp->next = sd_scsi_probe_cache_head; 2719 sd_scsi_probe_cache_head = cp; 2720 for (i = 0; i < NTARGETS_WIDE; i++) { 2721 cp->cache[i] = SCSIPROBE_EXISTS; 2722 } 2723 } 2724 2725 mutex_exit(&sd_scsi_probe_cache_mutex); 2726 2727 /* Recompute the cache for this target if LUN zero */ 2728 if (lun == 0) { 2729 cp->cache[tgt] = SCSIPROBE_EXISTS; 2730 } 2731 2732 /* Don't probe if cache remembers a NORESP from a previous LUN. */ 2733 if (cp->cache[tgt] != SCSIPROBE_EXISTS) { 2734 return (SCSIPROBE_NORESP); 2735 } 2736 2737 /* Do the actual probe; save & return the result */ 2738 return (cp->cache[tgt] = scsi_probe(devp, waitfn)); 2739 } 2740 2741 2742 /* 2743 * Function: sd_spin_up_unit 2744 * 2745 * Description: Issues the following commands to spin-up the device: 2746 * START STOP UNIT, and INQUIRY. 2747 * 2748 * Arguments: un - driver soft state (unit) structure 2749 * 2750 * Return Code: 0 - success 2751 * EIO - failure 2752 * EACCES - reservation conflict 2753 * 2754 * Context: Kernel thread context 2755 */ 2756 2757 static int 2758 sd_spin_up_unit(struct sd_lun *un) 2759 { 2760 size_t resid = 0; 2761 int has_conflict = FALSE; 2762 uchar_t *bufaddr; 2763 2764 ASSERT(un != NULL); 2765 2766 /* 2767 * Send a throwaway START UNIT command. 2768 * 2769 * If we fail on this, we don't care presently what precisely 2770 * is wrong. EMC's arrays will also fail this with a check 2771 * condition (0x2/0x4/0x3) if the device is "inactive," but 2772 * we don't want to fail the attach because it may become 2773 * "active" later. 2774 */ 2775 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT) 2776 == EACCES) 2777 has_conflict = TRUE; 2778 2779 /* 2780 * Send another INQUIRY command to the target. This is necessary for 2781 * non-removable media direct access devices because their INQUIRY data 2782 * may not be fully qualified until they are spun up (perhaps via the 2783 * START command above). Note: This seems to be needed for some 2784 * legacy devices only.) The INQUIRY command should succeed even if a 2785 * Reservation Conflict is present. 2786 */ 2787 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP); 2788 if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) { 2789 kmem_free(bufaddr, SUN_INQSIZE); 2790 return (EIO); 2791 } 2792 2793 /* 2794 * If we got enough INQUIRY data, copy it over the old INQUIRY data. 2795 * Note that this routine does not return a failure here even if the 2796 * INQUIRY command did not return any data. This is a legacy behavior. 2797 */ 2798 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) { 2799 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE); 2800 } 2801 2802 kmem_free(bufaddr, SUN_INQSIZE); 2803 2804 /* If we hit a reservation conflict above, tell the caller. */ 2805 if (has_conflict == TRUE) { 2806 return (EACCES); 2807 } 2808 2809 return (0); 2810 } 2811 2812 /* 2813 * Function: sd_enable_descr_sense 2814 * 2815 * Description: This routine attempts to select descriptor sense format 2816 * using the Control mode page. Devices that support 64 bit 2817 * LBAs (for >2TB luns) should also implement descriptor 2818 * sense data so we will call this function whenever we see 2819 * a lun larger than 2TB. If for some reason the device 2820 * supports 64 bit LBAs but doesn't support descriptor sense 2821 * presumably the mode select will fail. Everything will 2822 * continue to work normally except that we will not get 2823 * complete sense data for commands that fail with an LBA 2824 * larger than 32 bits. 2825 * 2826 * Arguments: un - driver soft state (unit) structure 2827 * 2828 * Context: Kernel thread context only 2829 */ 2830 2831 static void 2832 sd_enable_descr_sense(struct sd_lun *un) 2833 { 2834 uchar_t *header; 2835 struct mode_control_scsi3 *ctrl_bufp; 2836 size_t buflen; 2837 size_t bd_len; 2838 2839 /* 2840 * Read MODE SENSE page 0xA, Control Mode Page 2841 */ 2842 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH + 2843 sizeof (struct mode_control_scsi3); 2844 header = kmem_zalloc(buflen, KM_SLEEP); 2845 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 2846 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) { 2847 SD_ERROR(SD_LOG_COMMON, un, 2848 "sd_enable_descr_sense: mode sense ctrl page failed\n"); 2849 goto eds_exit; 2850 } 2851 2852 /* 2853 * Determine size of Block Descriptors in order to locate 2854 * the mode page data. ATAPI devices return 0, SCSI devices 2855 * should return MODE_BLK_DESC_LENGTH. 2856 */ 2857 bd_len = ((struct mode_header *)header)->bdesc_length; 2858 2859 ctrl_bufp = (struct mode_control_scsi3 *) 2860 (header + MODE_HEADER_LENGTH + bd_len); 2861 2862 /* 2863 * Clear PS bit for MODE SELECT 2864 */ 2865 ctrl_bufp->mode_page.ps = 0; 2866 2867 /* 2868 * Set D_SENSE to enable descriptor sense format. 2869 */ 2870 ctrl_bufp->d_sense = 1; 2871 2872 /* 2873 * Use MODE SELECT to commit the change to the D_SENSE bit 2874 */ 2875 if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 2876 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) { 2877 SD_INFO(SD_LOG_COMMON, un, 2878 "sd_enable_descr_sense: mode select ctrl page failed\n"); 2879 goto eds_exit; 2880 } 2881 2882 eds_exit: 2883 kmem_free(header, buflen); 2884 } 2885 2886 2887 /* 2888 * Function: sd_set_mmc_caps 2889 * 2890 * Description: This routine determines if the device is MMC compliant and if 2891 * the device supports CDDA via a mode sense of the CDVD 2892 * capabilities mode page. Also checks if the device is a 2893 * dvdram writable device. 2894 * 2895 * Arguments: un - driver soft state (unit) structure 2896 * 2897 * Context: Kernel thread context only 2898 */ 2899 2900 static void 2901 sd_set_mmc_caps(struct sd_lun *un) 2902 { 2903 struct mode_header_grp2 *sense_mhp; 2904 uchar_t *sense_page; 2905 caddr_t buf; 2906 int bd_len; 2907 int status; 2908 struct uscsi_cmd com; 2909 int rtn; 2910 uchar_t *out_data_rw, *out_data_hd; 2911 uchar_t *rqbuf_rw, *rqbuf_hd; 2912 2913 ASSERT(un != NULL); 2914 2915 /* 2916 * The flags which will be set in this function are - mmc compliant, 2917 * dvdram writable device, cdda support. Initialize them to FALSE 2918 * and if a capability is detected - it will be set to TRUE. 2919 */ 2920 un->un_f_mmc_cap = FALSE; 2921 un->un_f_dvdram_writable_device = FALSE; 2922 un->un_f_cfg_cdda = FALSE; 2923 2924 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 2925 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 2926 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 2927 2928 if (status != 0) { 2929 /* command failed; just return */ 2930 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 2931 return; 2932 } 2933 /* 2934 * If the mode sense request for the CDROM CAPABILITIES 2935 * page (0x2A) succeeds the device is assumed to be MMC. 2936 */ 2937 un->un_f_mmc_cap = TRUE; 2938 2939 /* Get to the page data */ 2940 sense_mhp = (struct mode_header_grp2 *)buf; 2941 bd_len = (sense_mhp->bdesc_length_hi << 8) | 2942 sense_mhp->bdesc_length_lo; 2943 if (bd_len > MODE_BLK_DESC_LENGTH) { 2944 /* 2945 * We did not get back the expected block descriptor 2946 * length so we cannot determine if the device supports 2947 * CDDA. However, we still indicate the device is MMC 2948 * according to the successful response to the page 2949 * 0x2A mode sense request. 2950 */ 2951 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 2952 "sd_set_mmc_caps: Mode Sense returned " 2953 "invalid block descriptor length\n"); 2954 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 2955 return; 2956 } 2957 2958 /* See if read CDDA is supported */ 2959 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + 2960 bd_len); 2961 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE; 2962 2963 /* See if writing DVD RAM is supported. */ 2964 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE; 2965 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 2966 if (un->un_f_dvdram_writable_device == TRUE) { 2967 return; 2968 } 2969 2970 /* 2971 * If un->un_f_dvdram_writable_device is still FALSE, 2972 * check for Iomega RRD type device. Iomega is identifying 2973 * their RRD type devices by the features RANDOM_WRITABLE and 2974 * HARDWARE_DEFECT_MANAGEMENT. 2975 */ 2976 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 2977 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 2978 2979 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 2980 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 2981 RANDOM_WRITABLE); 2982 if (rtn != 0) { 2983 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 2984 kmem_free(rqbuf_rw, SENSE_LENGTH); 2985 return; 2986 } 2987 2988 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 2989 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 2990 2991 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 2992 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 2993 HARDWARE_DEFECT_MANAGEMENT); 2994 if (rtn == 0) { 2995 /* 2996 * We have good information, check for random writable 2997 * and hardware defect features. 2998 */ 2999 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3000 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) { 3001 un->un_f_dvdram_writable_device = TRUE; 3002 } 3003 } 3004 3005 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3006 kmem_free(rqbuf_rw, SENSE_LENGTH); 3007 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3008 kmem_free(rqbuf_hd, SENSE_LENGTH); 3009 } 3010 3011 /* 3012 * Function: sd_check_for_writable_cd 3013 * 3014 * Description: This routine determines if the media in the device is 3015 * writable or not. It uses the get configuration command (0x46) 3016 * to determine if the media is writable 3017 * 3018 * Arguments: un - driver soft state (unit) structure 3019 * 3020 * Context: Never called at interrupt context. 3021 */ 3022 3023 static void 3024 sd_check_for_writable_cd(struct sd_lun *un) 3025 { 3026 struct uscsi_cmd com; 3027 uchar_t *out_data; 3028 uchar_t *rqbuf; 3029 int rtn; 3030 uchar_t *out_data_rw, *out_data_hd; 3031 uchar_t *rqbuf_rw, *rqbuf_hd; 3032 3033 ASSERT(un != NULL); 3034 ASSERT(mutex_owned(SD_MUTEX(un))); 3035 3036 /* 3037 * Initialize the writable media to false, if configuration info. 3038 * tells us otherwise then only we will set it. 3039 */ 3040 un->un_f_mmc_writable_media = FALSE; 3041 mutex_exit(SD_MUTEX(un)); 3042 3043 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 3044 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3045 3046 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH, 3047 out_data, SD_PROFILE_HEADER_LEN); 3048 3049 mutex_enter(SD_MUTEX(un)); 3050 if (rtn == 0) { 3051 /* 3052 * We have good information, check for writable DVD. 3053 */ 3054 if ((out_data[6] == 0) && (out_data[7] == 0x12)) { 3055 un->un_f_mmc_writable_media = TRUE; 3056 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3057 kmem_free(rqbuf, SENSE_LENGTH); 3058 return; 3059 } 3060 } 3061 3062 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3063 kmem_free(rqbuf, SENSE_LENGTH); 3064 3065 /* 3066 * If un->un_f_mmc_writable_media is still FALSE, 3067 * check for Iomega RRD type media. Iomega is identifying 3068 * their RRD type devices by the features RANDOM_WRITABLE and 3069 * HARDWARE_DEFECT_MANAGEMENT. 3070 */ 3071 mutex_exit(SD_MUTEX(un)); 3072 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3073 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3074 3075 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3076 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3077 RANDOM_WRITABLE); 3078 if (rtn != 0) { 3079 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3080 kmem_free(rqbuf_rw, SENSE_LENGTH); 3081 mutex_enter(SD_MUTEX(un)); 3082 return; 3083 } 3084 3085 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3086 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3087 3088 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3089 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3090 HARDWARE_DEFECT_MANAGEMENT); 3091 mutex_enter(SD_MUTEX(un)); 3092 if (rtn == 0) { 3093 /* 3094 * We have good information, check for random writable 3095 * and hardware defect features as current. 3096 */ 3097 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3098 (out_data_rw[10] & 0x1) && 3099 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) && 3100 (out_data_hd[10] & 0x1)) { 3101 un->un_f_mmc_writable_media = TRUE; 3102 } 3103 } 3104 3105 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3106 kmem_free(rqbuf_rw, SENSE_LENGTH); 3107 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3108 kmem_free(rqbuf_hd, SENSE_LENGTH); 3109 } 3110 3111 /* 3112 * Function: sd_read_unit_properties 3113 * 3114 * Description: The following implements a property lookup mechanism. 3115 * Properties for particular disks (keyed on vendor, model 3116 * and rev numbers) are sought in the sd.conf file via 3117 * sd_process_sdconf_file(), and if not found there, are 3118 * looked for in a list hardcoded in this driver via 3119 * sd_process_sdconf_table() Once located the properties 3120 * are used to update the driver unit structure. 3121 * 3122 * Arguments: un - driver soft state (unit) structure 3123 */ 3124 3125 static void 3126 sd_read_unit_properties(struct sd_lun *un) 3127 { 3128 /* 3129 * sd_process_sdconf_file returns SD_FAILURE if it cannot find 3130 * the "sd-config-list" property (from the sd.conf file) or if 3131 * there was not a match for the inquiry vid/pid. If this event 3132 * occurs the static driver configuration table is searched for 3133 * a match. 3134 */ 3135 ASSERT(un != NULL); 3136 if (sd_process_sdconf_file(un) == SD_FAILURE) { 3137 sd_process_sdconf_table(un); 3138 } 3139 3140 /* 3141 * Set this in sd.conf to 0 in order to disable kstats. The default 3142 * is 1, so they are enabled by default. 3143 */ 3144 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 3145 SD_DEVINFO(un), DDI_PROP_DONTPASS, "enable-partition-kstats", 1)); 3146 } 3147 3148 3149 /* 3150 * Function: sd_process_sdconf_file 3151 * 3152 * Description: Use ddi_getlongprop to obtain the properties from the 3153 * driver's config file (ie, sd.conf) and update the driver 3154 * soft state structure accordingly. 3155 * 3156 * Arguments: un - driver soft state (unit) structure 3157 * 3158 * Return Code: SD_SUCCESS - The properties were successfully set according 3159 * to the driver configuration file. 3160 * SD_FAILURE - The driver config list was not obtained or 3161 * there was no vid/pid match. This indicates that 3162 * the static config table should be used. 3163 * 3164 * The config file has a property, "sd-config-list", which consists of 3165 * one or more duplets as follows: 3166 * 3167 * sd-config-list= 3168 * <duplet>, 3169 * [<duplet>,] 3170 * [<duplet>]; 3171 * 3172 * The structure of each duplet is as follows: 3173 * 3174 * <duplet>:= <vid+pid>,<data-property-name_list> 3175 * 3176 * The first entry of the duplet is the device ID string (the concatenated 3177 * vid & pid; not to be confused with a device_id). This is defined in 3178 * the same way as in the sd_disk_table. 3179 * 3180 * The second part of the duplet is a string that identifies a 3181 * data-property-name-list. The data-property-name-list is defined as 3182 * follows: 3183 * 3184 * <data-property-name-list>:=<data-property-name> [<data-property-name>] 3185 * 3186 * The syntax of <data-property-name> depends on the <version> field. 3187 * 3188 * If version = SD_CONF_VERSION_1 we have the following syntax: 3189 * 3190 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3191 * 3192 * where the prop0 value will be used to set prop0 if bit0 set in the 3193 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1 3194 * 3195 * If version = SD_CONF_VERSION_10 we have the following syntax: 3196 * 3197 * <data-property-name>:=<version>,<prop0>,<prop1>,<prop2>,<prop3> 3198 */ 3199 3200 static int 3201 sd_process_sdconf_file(struct sd_lun *un) 3202 { 3203 char *config_list = NULL; 3204 int config_list_len; 3205 int len; 3206 int dupletlen = 0; 3207 char *vidptr; 3208 int vidlen; 3209 char *dnlist_ptr; 3210 char *dataname_ptr; 3211 int dnlist_len; 3212 int dataname_len; 3213 int *data_list; 3214 int data_list_len; 3215 int rval = SD_FAILURE; 3216 int i; 3217 3218 ASSERT(un != NULL); 3219 3220 /* Obtain the configuration list associated with the .conf file */ 3221 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS, 3222 sd_config_list, (caddr_t)&config_list, &config_list_len) 3223 != DDI_PROP_SUCCESS) { 3224 return (SD_FAILURE); 3225 } 3226 3227 /* 3228 * Compare vids in each duplet to the inquiry vid - if a match is 3229 * made, get the data value and update the soft state structure 3230 * accordingly. 3231 * 3232 * Note: This algorithm is complex and difficult to maintain. It should 3233 * be replaced with a more robust implementation. 3234 */ 3235 for (len = config_list_len, vidptr = config_list; len > 0; 3236 vidptr += dupletlen, len -= dupletlen) { 3237 /* 3238 * Note: The assumption here is that each vid entry is on 3239 * a unique line from its associated duplet. 3240 */ 3241 vidlen = dupletlen = (int)strlen(vidptr); 3242 if ((vidlen == 0) || 3243 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) { 3244 dupletlen++; 3245 continue; 3246 } 3247 3248 /* 3249 * dnlist contains 1 or more blank separated 3250 * data-property-name entries 3251 */ 3252 dnlist_ptr = vidptr + vidlen + 1; 3253 dnlist_len = (int)strlen(dnlist_ptr); 3254 dupletlen += dnlist_len + 2; 3255 3256 /* 3257 * Set a pointer for the first data-property-name 3258 * entry in the list 3259 */ 3260 dataname_ptr = dnlist_ptr; 3261 dataname_len = 0; 3262 3263 /* 3264 * Loop through all data-property-name entries in the 3265 * data-property-name-list setting the properties for each. 3266 */ 3267 while (dataname_len < dnlist_len) { 3268 int version; 3269 3270 /* 3271 * Determine the length of the current 3272 * data-property-name entry by indexing until a 3273 * blank or NULL is encountered. When the space is 3274 * encountered reset it to a NULL for compliance 3275 * with ddi_getlongprop(). 3276 */ 3277 for (i = 0; ((dataname_ptr[i] != ' ') && 3278 (dataname_ptr[i] != '\0')); i++) { 3279 ; 3280 } 3281 3282 dataname_len += i; 3283 /* If not null terminated, Make it so */ 3284 if (dataname_ptr[i] == ' ') { 3285 dataname_ptr[i] = '\0'; 3286 } 3287 dataname_len++; 3288 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3289 "sd_process_sdconf_file: disk:%s, data:%s\n", 3290 vidptr, dataname_ptr); 3291 3292 /* Get the data list */ 3293 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0, 3294 dataname_ptr, (caddr_t)&data_list, &data_list_len) 3295 != DDI_PROP_SUCCESS) { 3296 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3297 "sd_process_sdconf_file: data property (%s)" 3298 " has no value\n", dataname_ptr); 3299 dataname_ptr = dnlist_ptr + dataname_len; 3300 continue; 3301 } 3302 3303 version = data_list[0]; 3304 3305 if (version == SD_CONF_VERSION_1) { 3306 sd_tunables values; 3307 3308 /* Set the properties */ 3309 if (sd_chk_vers1_data(un, data_list[1], 3310 &data_list[2], data_list_len, dataname_ptr) 3311 == SD_SUCCESS) { 3312 sd_get_tunables_from_conf(un, 3313 data_list[1], &data_list[2], 3314 &values); 3315 sd_set_vers1_properties(un, 3316 data_list[1], &values); 3317 rval = SD_SUCCESS; 3318 } else { 3319 rval = SD_FAILURE; 3320 } 3321 } else { 3322 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3323 "data property %s version 0x%x is invalid.", 3324 dataname_ptr, version); 3325 rval = SD_FAILURE; 3326 } 3327 kmem_free(data_list, data_list_len); 3328 dataname_ptr = dnlist_ptr + dataname_len; 3329 } 3330 } 3331 3332 /* free up the memory allocated by ddi_getlongprop */ 3333 if (config_list) { 3334 kmem_free(config_list, config_list_len); 3335 } 3336 3337 return (rval); 3338 } 3339 3340 /* 3341 * Function: sd_get_tunables_from_conf() 3342 * 3343 * 3344 * This function reads the data list from the sd.conf file and pulls 3345 * the values that can have numeric values as arguments and places 3346 * the values in the apropriate sd_tunables member. 3347 * Since the order of the data list members varies across platforms 3348 * This function reads them from the data list in a platform specific 3349 * order and places them into the correct sd_tunable member that is 3350 * a consistant across all platforms. 3351 */ 3352 static void 3353 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 3354 sd_tunables *values) 3355 { 3356 int i; 3357 int mask; 3358 3359 bzero(values, sizeof (sd_tunables)); 3360 3361 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3362 3363 mask = 1 << i; 3364 if (mask > flags) { 3365 break; 3366 } 3367 3368 switch (mask & flags) { 3369 case 0: /* This mask bit not set in flags */ 3370 continue; 3371 case SD_CONF_BSET_THROTTLE: 3372 values->sdt_throttle = data_list[i]; 3373 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3374 "sd_get_tunables_from_conf: throttle = %d\n", 3375 values->sdt_throttle); 3376 break; 3377 case SD_CONF_BSET_CTYPE: 3378 values->sdt_ctype = data_list[i]; 3379 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3380 "sd_get_tunables_from_conf: ctype = %d\n", 3381 values->sdt_ctype); 3382 break; 3383 case SD_CONF_BSET_NRR_COUNT: 3384 values->sdt_not_rdy_retries = data_list[i]; 3385 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3386 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 3387 values->sdt_not_rdy_retries); 3388 break; 3389 case SD_CONF_BSET_BSY_RETRY_COUNT: 3390 values->sdt_busy_retries = data_list[i]; 3391 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3392 "sd_get_tunables_from_conf: busy_retries = %d\n", 3393 values->sdt_busy_retries); 3394 break; 3395 case SD_CONF_BSET_RST_RETRIES: 3396 values->sdt_reset_retries = data_list[i]; 3397 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3398 "sd_get_tunables_from_conf: reset_retries = %d\n", 3399 values->sdt_reset_retries); 3400 break; 3401 case SD_CONF_BSET_RSV_REL_TIME: 3402 values->sdt_reserv_rel_time = data_list[i]; 3403 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3404 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 3405 values->sdt_reserv_rel_time); 3406 break; 3407 case SD_CONF_BSET_MIN_THROTTLE: 3408 values->sdt_min_throttle = data_list[i]; 3409 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3410 "sd_get_tunables_from_conf: min_throttle = %d\n", 3411 values->sdt_min_throttle); 3412 break; 3413 case SD_CONF_BSET_DISKSORT_DISABLED: 3414 values->sdt_disk_sort_dis = data_list[i]; 3415 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3416 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 3417 values->sdt_disk_sort_dis); 3418 break; 3419 case SD_CONF_BSET_LUN_RESET_ENABLED: 3420 values->sdt_lun_reset_enable = data_list[i]; 3421 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3422 "sd_get_tunables_from_conf: lun_reset_enable = %d" 3423 "\n", values->sdt_lun_reset_enable); 3424 break; 3425 } 3426 } 3427 } 3428 3429 /* 3430 * Function: sd_process_sdconf_table 3431 * 3432 * Description: Search the static configuration table for a match on the 3433 * inquiry vid/pid and update the driver soft state structure 3434 * according to the table property values for the device. 3435 * 3436 * The form of a configuration table entry is: 3437 * <vid+pid>,<flags>,<property-data> 3438 * "SEAGATE ST42400N",1,63,0,0 (Fibre) 3439 * "SEAGATE ST42400N",1,63,0,0,0,0 (Sparc) 3440 * "SEAGATE ST42400N",1,63,0,0,0,0,0,0,0,0,0,0 (Intel) 3441 * 3442 * Arguments: un - driver soft state (unit) structure 3443 */ 3444 3445 static void 3446 sd_process_sdconf_table(struct sd_lun *un) 3447 { 3448 char *id = NULL; 3449 int table_index; 3450 int idlen; 3451 3452 ASSERT(un != NULL); 3453 for (table_index = 0; table_index < sd_disk_table_size; 3454 table_index++) { 3455 id = sd_disk_table[table_index].device_id; 3456 idlen = strlen(id); 3457 if (idlen == 0) { 3458 continue; 3459 } 3460 3461 /* 3462 * The static configuration table currently does not 3463 * implement version 10 properties. Additionally, 3464 * multiple data-property-name entries are not 3465 * implemented in the static configuration table. 3466 */ 3467 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 3468 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3469 "sd_process_sdconf_table: disk %s\n", id); 3470 sd_set_vers1_properties(un, 3471 sd_disk_table[table_index].flags, 3472 sd_disk_table[table_index].properties); 3473 break; 3474 } 3475 } 3476 } 3477 3478 3479 /* 3480 * Function: sd_sdconf_id_match 3481 * 3482 * Description: This local function implements a case sensitive vid/pid 3483 * comparison as well as the boundary cases of wild card and 3484 * multiple blanks. 3485 * 3486 * Note: An implicit assumption made here is that the scsi 3487 * inquiry structure will always keep the vid, pid and 3488 * revision strings in consecutive sequence, so they can be 3489 * read as a single string. If this assumption is not the 3490 * case, a separate string, to be used for the check, needs 3491 * to be built with these strings concatenated. 3492 * 3493 * Arguments: un - driver soft state (unit) structure 3494 * id - table or config file vid/pid 3495 * idlen - length of the vid/pid (bytes) 3496 * 3497 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3498 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3499 */ 3500 3501 static int 3502 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 3503 { 3504 struct scsi_inquiry *sd_inq; 3505 int rval = SD_SUCCESS; 3506 3507 ASSERT(un != NULL); 3508 sd_inq = un->un_sd->sd_inq; 3509 ASSERT(id != NULL); 3510 3511 /* 3512 * We use the inq_vid as a pointer to a buffer containing the 3513 * vid and pid and use the entire vid/pid length of the table 3514 * entry for the comparison. This works because the inq_pid 3515 * data member follows inq_vid in the scsi_inquiry structure. 3516 */ 3517 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 3518 /* 3519 * The user id string is compared to the inquiry vid/pid 3520 * using a case insensitive comparison and ignoring 3521 * multiple spaces. 3522 */ 3523 rval = sd_blank_cmp(un, id, idlen); 3524 if (rval != SD_SUCCESS) { 3525 /* 3526 * User id strings that start and end with a "*" 3527 * are a special case. These do not have a 3528 * specific vendor, and the product string can 3529 * appear anywhere in the 16 byte PID portion of 3530 * the inquiry data. This is a simple strstr() 3531 * type search for the user id in the inquiry data. 3532 */ 3533 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 3534 char *pidptr = &id[1]; 3535 int i; 3536 int j; 3537 int pidstrlen = idlen - 2; 3538 j = sizeof (SD_INQUIRY(un)->inq_pid) - 3539 pidstrlen; 3540 3541 if (j < 0) { 3542 return (SD_FAILURE); 3543 } 3544 for (i = 0; i < j; i++) { 3545 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 3546 pidptr, pidstrlen) == 0) { 3547 rval = SD_SUCCESS; 3548 break; 3549 } 3550 } 3551 } 3552 } 3553 } 3554 return (rval); 3555 } 3556 3557 3558 /* 3559 * Function: sd_blank_cmp 3560 * 3561 * Description: If the id string starts and ends with a space, treat 3562 * multiple consecutive spaces as equivalent to a single 3563 * space. For example, this causes a sd_disk_table entry 3564 * of " NEC CDROM " to match a device's id string of 3565 * "NEC CDROM". 3566 * 3567 * Note: The success exit condition for this routine is if 3568 * the pointer to the table entry is '\0' and the cnt of 3569 * the inquiry length is zero. This will happen if the inquiry 3570 * string returned by the device is padded with spaces to be 3571 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 3572 * SCSI spec states that the inquiry string is to be padded with 3573 * spaces. 3574 * 3575 * Arguments: un - driver soft state (unit) structure 3576 * id - table or config file vid/pid 3577 * idlen - length of the vid/pid (bytes) 3578 * 3579 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3580 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3581 */ 3582 3583 static int 3584 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 3585 { 3586 char *p1; 3587 char *p2; 3588 int cnt; 3589 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 3590 sizeof (SD_INQUIRY(un)->inq_pid); 3591 3592 ASSERT(un != NULL); 3593 p2 = un->un_sd->sd_inq->inq_vid; 3594 ASSERT(id != NULL); 3595 p1 = id; 3596 3597 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 3598 /* 3599 * Note: string p1 is terminated by a NUL but string p2 3600 * isn't. The end of p2 is determined by cnt. 3601 */ 3602 for (;;) { 3603 /* skip over any extra blanks in both strings */ 3604 while ((*p1 != '\0') && (*p1 == ' ')) { 3605 p1++; 3606 } 3607 while ((cnt != 0) && (*p2 == ' ')) { 3608 p2++; 3609 cnt--; 3610 } 3611 3612 /* compare the two strings */ 3613 if ((cnt == 0) || 3614 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 3615 break; 3616 } 3617 while ((cnt > 0) && 3618 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 3619 p1++; 3620 p2++; 3621 cnt--; 3622 } 3623 } 3624 } 3625 3626 /* return SD_SUCCESS if both strings match */ 3627 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 3628 } 3629 3630 3631 /* 3632 * Function: sd_chk_vers1_data 3633 * 3634 * Description: Verify the version 1 device properties provided by the 3635 * user via the configuration file 3636 * 3637 * Arguments: un - driver soft state (unit) structure 3638 * flags - integer mask indicating properties to be set 3639 * prop_list - integer list of property values 3640 * list_len - length of user provided data 3641 * 3642 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 3643 * SD_FAILURE - Indicates the user provided data is invalid 3644 */ 3645 3646 static int 3647 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 3648 int list_len, char *dataname_ptr) 3649 { 3650 int i; 3651 int mask = 1; 3652 int index = 0; 3653 3654 ASSERT(un != NULL); 3655 3656 /* Check for a NULL property name and list */ 3657 if (dataname_ptr == NULL) { 3658 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3659 "sd_chk_vers1_data: NULL data property name."); 3660 return (SD_FAILURE); 3661 } 3662 if (prop_list == NULL) { 3663 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3664 "sd_chk_vers1_data: %s NULL data property list.", 3665 dataname_ptr); 3666 return (SD_FAILURE); 3667 } 3668 3669 /* Display a warning if undefined bits are set in the flags */ 3670 if (flags & ~SD_CONF_BIT_MASK) { 3671 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3672 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 3673 "Properties not set.", 3674 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 3675 return (SD_FAILURE); 3676 } 3677 3678 /* 3679 * Verify the length of the list by identifying the highest bit set 3680 * in the flags and validating that the property list has a length 3681 * up to the index of this bit. 3682 */ 3683 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3684 if (flags & mask) { 3685 index++; 3686 } 3687 mask = 1 << i; 3688 } 3689 if ((list_len / sizeof (int)) < (index + 2)) { 3690 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3691 "sd_chk_vers1_data: " 3692 "Data property list %s size is incorrect. " 3693 "Properties not set.", dataname_ptr); 3694 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 3695 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 3696 return (SD_FAILURE); 3697 } 3698 return (SD_SUCCESS); 3699 } 3700 3701 3702 /* 3703 * Function: sd_set_vers1_properties 3704 * 3705 * Description: Set version 1 device properties based on a property list 3706 * retrieved from the driver configuration file or static 3707 * configuration table. Version 1 properties have the format: 3708 * 3709 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3710 * 3711 * where the prop0 value will be used to set prop0 if bit0 3712 * is set in the flags 3713 * 3714 * Arguments: un - driver soft state (unit) structure 3715 * flags - integer mask indicating properties to be set 3716 * prop_list - integer list of property values 3717 */ 3718 3719 static void 3720 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 3721 { 3722 ASSERT(un != NULL); 3723 3724 /* 3725 * Set the flag to indicate cache is to be disabled. An attempt 3726 * to disable the cache via sd_disable_caching() will be made 3727 * later during attach once the basic initialization is complete. 3728 */ 3729 if (flags & SD_CONF_BSET_NOCACHE) { 3730 un->un_f_opt_disable_cache = TRUE; 3731 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3732 "sd_set_vers1_properties: caching disabled flag set\n"); 3733 } 3734 3735 /* CD-specific configuration parameters */ 3736 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 3737 un->un_f_cfg_playmsf_bcd = TRUE; 3738 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3739 "sd_set_vers1_properties: playmsf_bcd set\n"); 3740 } 3741 if (flags & SD_CONF_BSET_READSUB_BCD) { 3742 un->un_f_cfg_readsub_bcd = TRUE; 3743 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3744 "sd_set_vers1_properties: readsub_bcd set\n"); 3745 } 3746 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 3747 un->un_f_cfg_read_toc_trk_bcd = TRUE; 3748 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3749 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 3750 } 3751 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 3752 un->un_f_cfg_read_toc_addr_bcd = TRUE; 3753 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3754 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 3755 } 3756 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 3757 un->un_f_cfg_no_read_header = TRUE; 3758 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3759 "sd_set_vers1_properties: no_read_header set\n"); 3760 } 3761 if (flags & SD_CONF_BSET_READ_CD_XD4) { 3762 un->un_f_cfg_read_cd_xd4 = TRUE; 3763 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3764 "sd_set_vers1_properties: read_cd_xd4 set\n"); 3765 } 3766 3767 /* Support for devices which do not have valid/unique serial numbers */ 3768 if (flags & SD_CONF_BSET_FAB_DEVID) { 3769 un->un_f_opt_fab_devid = TRUE; 3770 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3771 "sd_set_vers1_properties: fab_devid bit set\n"); 3772 } 3773 3774 /* Support for user throttle configuration */ 3775 if (flags & SD_CONF_BSET_THROTTLE) { 3776 ASSERT(prop_list != NULL); 3777 un->un_saved_throttle = un->un_throttle = 3778 prop_list->sdt_throttle; 3779 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3780 "sd_set_vers1_properties: throttle set to %d\n", 3781 prop_list->sdt_throttle); 3782 } 3783 3784 /* Set the per disk retry count according to the conf file or table. */ 3785 if (flags & SD_CONF_BSET_NRR_COUNT) { 3786 ASSERT(prop_list != NULL); 3787 if (prop_list->sdt_not_rdy_retries) { 3788 un->un_notready_retry_count = 3789 prop_list->sdt_not_rdy_retries; 3790 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3791 "sd_set_vers1_properties: not ready retry count" 3792 " set to %d\n", un->un_notready_retry_count); 3793 } 3794 } 3795 3796 /* The controller type is reported for generic disk driver ioctls */ 3797 if (flags & SD_CONF_BSET_CTYPE) { 3798 ASSERT(prop_list != NULL); 3799 switch (prop_list->sdt_ctype) { 3800 case CTYPE_CDROM: 3801 un->un_ctype = prop_list->sdt_ctype; 3802 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3803 "sd_set_vers1_properties: ctype set to " 3804 "CTYPE_CDROM\n"); 3805 break; 3806 case CTYPE_CCS: 3807 un->un_ctype = prop_list->sdt_ctype; 3808 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3809 "sd_set_vers1_properties: ctype set to " 3810 "CTYPE_CCS\n"); 3811 break; 3812 case CTYPE_ROD: /* RW optical */ 3813 un->un_ctype = prop_list->sdt_ctype; 3814 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3815 "sd_set_vers1_properties: ctype set to " 3816 "CTYPE_ROD\n"); 3817 break; 3818 default: 3819 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3820 "sd_set_vers1_properties: Could not set " 3821 "invalid ctype value (%d)", 3822 prop_list->sdt_ctype); 3823 } 3824 } 3825 3826 /* Purple failover timeout */ 3827 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 3828 ASSERT(prop_list != NULL); 3829 un->un_busy_retry_count = 3830 prop_list->sdt_busy_retries; 3831 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3832 "sd_set_vers1_properties: " 3833 "busy retry count set to %d\n", 3834 un->un_busy_retry_count); 3835 } 3836 3837 /* Purple reset retry count */ 3838 if (flags & SD_CONF_BSET_RST_RETRIES) { 3839 ASSERT(prop_list != NULL); 3840 un->un_reset_retry_count = 3841 prop_list->sdt_reset_retries; 3842 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3843 "sd_set_vers1_properties: " 3844 "reset retry count set to %d\n", 3845 un->un_reset_retry_count); 3846 } 3847 3848 /* Purple reservation release timeout */ 3849 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 3850 ASSERT(prop_list != NULL); 3851 un->un_reserve_release_time = 3852 prop_list->sdt_reserv_rel_time; 3853 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3854 "sd_set_vers1_properties: " 3855 "reservation release timeout set to %d\n", 3856 un->un_reserve_release_time); 3857 } 3858 3859 /* 3860 * Driver flag telling the driver to verify that no commands are pending 3861 * for a device before issuing a Test Unit Ready. This is a workaround 3862 * for a firmware bug in some Seagate eliteI drives. 3863 */ 3864 if (flags & SD_CONF_BSET_TUR_CHECK) { 3865 un->un_f_cfg_tur_check = TRUE; 3866 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3867 "sd_set_vers1_properties: tur queue check set\n"); 3868 } 3869 3870 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 3871 un->un_min_throttle = prop_list->sdt_min_throttle; 3872 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3873 "sd_set_vers1_properties: min throttle set to %d\n", 3874 un->un_min_throttle); 3875 } 3876 3877 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 3878 un->un_f_disksort_disabled = 3879 (prop_list->sdt_disk_sort_dis != 0) ? 3880 TRUE : FALSE; 3881 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3882 "sd_set_vers1_properties: disksort disabled " 3883 "flag set to %d\n", 3884 prop_list->sdt_disk_sort_dis); 3885 } 3886 3887 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 3888 un->un_f_lun_reset_enabled = 3889 (prop_list->sdt_lun_reset_enable != 0) ? 3890 TRUE : FALSE; 3891 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3892 "sd_set_vers1_properties: lun reset enabled " 3893 "flag set to %d\n", 3894 prop_list->sdt_lun_reset_enable); 3895 } 3896 3897 /* 3898 * Validate the throttle values. 3899 * If any of the numbers are invalid, set everything to defaults. 3900 */ 3901 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 3902 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 3903 (un->un_min_throttle > un->un_throttle)) { 3904 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 3905 un->un_min_throttle = sd_min_throttle; 3906 } 3907 } 3908 3909 /* 3910 * The following routines support reading and interpretation of disk labels, 3911 * including Solaris BE (8-slice) vtoc's, Solaris LE (16-slice) vtoc's, and 3912 * fdisk tables. 3913 */ 3914 3915 /* 3916 * Function: sd_validate_geometry 3917 * 3918 * Description: Read the label from the disk (if present). Update the unit's 3919 * geometry and vtoc information from the data in the label. 3920 * Verify that the label is valid. 3921 * 3922 * Arguments: un - driver soft state (unit) structure 3923 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 3924 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 3925 * to use the USCSI "direct" chain and bypass the normal 3926 * command waitq. 3927 * 3928 * Return Code: 0 - Successful completion 3929 * EINVAL - Invalid value in un->un_tgt_blocksize or 3930 * un->un_blockcount; or label on disk is corrupted 3931 * or unreadable. 3932 * EACCES - Reservation conflict at the device. 3933 * ENOMEM - Resource allocation error 3934 * ENOTSUP - geometry not applicable 3935 * 3936 * Context: Kernel thread only (can sleep). 3937 */ 3938 3939 static int 3940 sd_validate_geometry(struct sd_lun *un, int path_flag) 3941 { 3942 static char labelstring[128]; 3943 static char buf[256]; 3944 char *label = NULL; 3945 int label_error = 0; 3946 int gvalid = un->un_f_geometry_is_valid; 3947 int lbasize; 3948 uint_t capacity; 3949 int count; 3950 3951 ASSERT(un != NULL); 3952 ASSERT(mutex_owned(SD_MUTEX(un))); 3953 3954 /* 3955 * If the required values are not valid, then try getting them 3956 * once via read capacity. If that fails, then fail this call. 3957 * This is necessary with the new mpxio failover behavior in 3958 * the T300 where we can get an attach for the inactive path 3959 * before the active path. The inactive path fails commands with 3960 * sense data of 02,04,88 which happens to the read capacity 3961 * before mpxio has had sufficient knowledge to know if it should 3962 * force a fail over or not. (Which it won't do at attach anyhow). 3963 * If the read capacity at attach time fails, un_tgt_blocksize and 3964 * un_blockcount won't be valid. 3965 */ 3966 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 3967 (un->un_f_blockcount_is_valid != TRUE)) { 3968 uint64_t cap; 3969 uint32_t lbasz; 3970 int rval; 3971 3972 mutex_exit(SD_MUTEX(un)); 3973 rval = sd_send_scsi_READ_CAPACITY(un, &cap, 3974 &lbasz, SD_PATH_DIRECT); 3975 mutex_enter(SD_MUTEX(un)); 3976 if (rval == 0) { 3977 /* 3978 * The following relies on 3979 * sd_send_scsi_READ_CAPACITY never 3980 * returning 0 for capacity and/or lbasize. 3981 */ 3982 sd_update_block_info(un, lbasz, cap); 3983 } 3984 3985 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 3986 (un->un_f_blockcount_is_valid != TRUE)) { 3987 return (EINVAL); 3988 } 3989 } 3990 3991 /* 3992 * Copy the lbasize and capacity so that if they're reset while we're 3993 * not holding the SD_MUTEX, we will continue to use valid values 3994 * after the SD_MUTEX is reacquired. (4119659) 3995 */ 3996 lbasize = un->un_tgt_blocksize; 3997 capacity = un->un_blockcount; 3998 3999 #if defined(_SUNOS_VTOC_16) 4000 /* 4001 * Set up the "whole disk" fdisk partition; this should always 4002 * exist, regardless of whether the disk contains an fdisk table 4003 * or vtoc. 4004 */ 4005 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 4006 un->un_map[P0_RAW_DISK].dkl_nblk = capacity; 4007 #endif 4008 4009 /* 4010 * Refresh the logical and physical geometry caches. 4011 * (data from MODE SENSE format/rigid disk geometry pages, 4012 * and scsi_ifgetcap("geometry"). 4013 */ 4014 sd_resync_geom_caches(un, capacity, lbasize, path_flag); 4015 4016 label_error = sd_use_efi(un, path_flag); 4017 if (label_error == 0) { 4018 /* found a valid EFI label */ 4019 SD_TRACE(SD_LOG_IO_PARTITION, un, 4020 "sd_validate_geometry: found EFI label\n"); 4021 un->un_solaris_offset = 0; 4022 un->un_solaris_size = capacity; 4023 return (ENOTSUP); 4024 } 4025 if (un->un_blockcount > DK_MAX_BLOCKS) { 4026 if (label_error == ESRCH) { 4027 /* 4028 * they've configured a LUN over 1TB, but used 4029 * format.dat to restrict format's view of the 4030 * capacity to be under 1TB 4031 */ 4032 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4033 "is >1TB and has a VTOC label: use format(1M) to either decrease the"); 4034 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 4035 "size to be < 1TB or relabel the disk with an EFI label"); 4036 } else { 4037 /* unlabeled disk over 1TB */ 4038 return (ENOTSUP); 4039 } 4040 } 4041 label_error = 0; 4042 4043 /* 4044 * at this point it is either labeled with a VTOC or it is 4045 * under 1TB 4046 */ 4047 4048 /* 4049 * Only DIRECT ACCESS devices will have Sun labels. 4050 * CD's supposedly have a Sun label, too 4051 */ 4052 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT || ISREMOVABLE(un)) { 4053 struct dk_label *dkl; 4054 offset_t dkl1; 4055 offset_t label_addr, real_addr; 4056 int rval; 4057 size_t buffer_size; 4058 4059 /* 4060 * Note: This will set up un->un_solaris_size and 4061 * un->un_solaris_offset. 4062 */ 4063 switch (sd_read_fdisk(un, capacity, lbasize, path_flag)) { 4064 case SD_CMD_RESERVATION_CONFLICT: 4065 ASSERT(mutex_owned(SD_MUTEX(un))); 4066 return (EACCES); 4067 case SD_CMD_FAILURE: 4068 ASSERT(mutex_owned(SD_MUTEX(un))); 4069 return (ENOMEM); 4070 } 4071 4072 if (un->un_solaris_size <= DK_LABEL_LOC) { 4073 /* 4074 * Found fdisk table but no Solaris partition entry, 4075 * so don't call sd_uselabel() and don't create 4076 * a default label. 4077 */ 4078 label_error = 0; 4079 un->un_f_geometry_is_valid = TRUE; 4080 goto no_solaris_partition; 4081 } 4082 label_addr = (daddr_t)(un->un_solaris_offset + DK_LABEL_LOC); 4083 4084 /* 4085 * sys_blocksize != tgt_blocksize, need to re-adjust 4086 * blkno and save the index to beginning of dk_label 4087 */ 4088 real_addr = SD_SYS2TGTBLOCK(un, label_addr); 4089 buffer_size = SD_REQBYTES2TGTBYTES(un, 4090 sizeof (struct dk_label)); 4091 4092 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: " 4093 "label_addr: 0x%x allocation size: 0x%x\n", 4094 label_addr, buffer_size); 4095 dkl = kmem_zalloc(buffer_size, KM_NOSLEEP); 4096 if (dkl == NULL) { 4097 return (ENOMEM); 4098 } 4099 4100 mutex_exit(SD_MUTEX(un)); 4101 rval = sd_send_scsi_READ(un, dkl, buffer_size, real_addr, 4102 path_flag); 4103 mutex_enter(SD_MUTEX(un)); 4104 4105 switch (rval) { 4106 case 0: 4107 /* 4108 * sd_uselabel will establish that the geometry 4109 * is valid. 4110 * For sys_blocksize != tgt_blocksize, need 4111 * to index into the beginning of dk_label 4112 */ 4113 dkl1 = (daddr_t)dkl 4114 + SD_TGTBYTEOFFSET(un, label_addr, real_addr); 4115 if (sd_uselabel(un, (struct dk_label *)(uintptr_t)dkl1, 4116 path_flag) != SD_LABEL_IS_VALID) { 4117 label_error = EINVAL; 4118 } 4119 break; 4120 case EACCES: 4121 label_error = EACCES; 4122 break; 4123 default: 4124 label_error = EINVAL; 4125 break; 4126 } 4127 4128 kmem_free(dkl, buffer_size); 4129 4130 #if defined(_SUNOS_VTOC_8) 4131 label = (char *)un->un_asciilabel; 4132 #elif defined(_SUNOS_VTOC_16) 4133 label = (char *)un->un_vtoc.v_asciilabel; 4134 #else 4135 #error "No VTOC format defined." 4136 #endif 4137 } 4138 4139 /* 4140 * If a valid label was not found, AND if no reservation conflict 4141 * was detected, then go ahead and create a default label (4069506). 4142 * 4143 * Note: currently, for VTOC_8 devices, the default label is created 4144 * for removables only. For VTOC_16 devices, the default label will 4145 * be created for both removables and non-removables alike. 4146 * (see sd_build_default_label) 4147 */ 4148 #if defined(_SUNOS_VTOC_8) 4149 if (ISREMOVABLE(un) && (label_error != EACCES)) { 4150 #elif defined(_SUNOS_VTOC_16) 4151 if (label_error != EACCES) { 4152 #endif 4153 if (un->un_f_geometry_is_valid == FALSE) { 4154 sd_build_default_label(un); 4155 } 4156 label_error = 0; 4157 } 4158 4159 no_solaris_partition: 4160 if ((!ISREMOVABLE(un) || 4161 (ISREMOVABLE(un) && un->un_mediastate == DKIO_EJECTED)) && 4162 (un->un_state == SD_STATE_NORMAL && gvalid == FALSE)) { 4163 /* 4164 * Print out a message indicating who and what we are. 4165 * We do this only when we happen to really validate the 4166 * geometry. We may call sd_validate_geometry() at other 4167 * times, e.g., ioctl()'s like Get VTOC in which case we 4168 * don't want to print the label. 4169 * If the geometry is valid, print the label string, 4170 * else print vendor and product info, if available 4171 */ 4172 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 4173 SD_INFO(SD_LOG_ATTACH_DETACH, un, "?<%s>\n", label); 4174 } else { 4175 mutex_enter(&sd_label_mutex); 4176 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 4177 labelstring); 4178 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 4179 &labelstring[64]); 4180 (void) sprintf(buf, "?Vendor '%s', product '%s'", 4181 labelstring, &labelstring[64]); 4182 if (un->un_f_blockcount_is_valid == TRUE) { 4183 (void) sprintf(&buf[strlen(buf)], 4184 ", %llu %u byte blocks\n", 4185 (longlong_t)un->un_blockcount, 4186 un->un_tgt_blocksize); 4187 } else { 4188 (void) sprintf(&buf[strlen(buf)], 4189 ", (unknown capacity)\n"); 4190 } 4191 SD_INFO(SD_LOG_ATTACH_DETACH, un, buf); 4192 mutex_exit(&sd_label_mutex); 4193 } 4194 } 4195 4196 #if defined(_SUNOS_VTOC_16) 4197 /* 4198 * If we have valid geometry, set up the remaining fdisk partitions. 4199 * Note that dkl_cylno is not used for the fdisk map entries, so 4200 * we set it to an entirely bogus value. 4201 */ 4202 for (count = 0; count < FD_NUMPART; count++) { 4203 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 4204 un->un_map[FDISK_P1 + count].dkl_nblk = 4205 un->un_fmap[count].fmap_nblk; 4206 4207 un->un_offset[FDISK_P1 + count] = 4208 un->un_fmap[count].fmap_start; 4209 } 4210 #endif 4211 4212 for (count = 0; count < NDKMAP; count++) { 4213 #if defined(_SUNOS_VTOC_8) 4214 struct dk_map *lp = &un->un_map[count]; 4215 un->un_offset[count] = 4216 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 4217 #elif defined(_SUNOS_VTOC_16) 4218 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 4219 4220 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 4221 #else 4222 #error "No VTOC format defined." 4223 #endif 4224 } 4225 4226 return (label_error); 4227 } 4228 4229 4230 #if defined(_SUNOS_VTOC_16) 4231 /* 4232 * Macro: MAX_BLKS 4233 * 4234 * This macro is used for table entries where we need to have the largest 4235 * possible sector value for that head & SPT (sectors per track) 4236 * combination. Other entries for some smaller disk sizes are set by 4237 * convention to match those used by X86 BIOS usage. 4238 */ 4239 #define MAX_BLKS(heads, spt) UINT16_MAX * heads * spt, heads, spt 4240 4241 /* 4242 * Function: sd_convert_geometry 4243 * 4244 * Description: Convert physical geometry into a dk_geom structure. In 4245 * other words, make sure we don't wrap 16-bit values. 4246 * e.g. converting from geom_cache to dk_geom 4247 * 4248 * Context: Kernel thread only 4249 */ 4250 static void 4251 sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g) 4252 { 4253 int i; 4254 static const struct chs_values { 4255 uint_t max_cap; /* Max Capacity for this HS. */ 4256 uint_t nhead; /* Heads to use. */ 4257 uint_t nsect; /* SPT to use. */ 4258 } CHS_values[] = { 4259 {0x00200000, 64, 32}, /* 1GB or smaller disk. */ 4260 {0x01000000, 128, 32}, /* 8GB or smaller disk. */ 4261 {MAX_BLKS(255, 63)}, /* 502.02GB or smaller disk. */ 4262 {MAX_BLKS(255, 126)}, /* .98TB or smaller disk. */ 4263 {DK_MAX_BLOCKS, 255, 189} /* Max size is just under 1TB */ 4264 }; 4265 4266 /* Unlabeled SCSI floppy device */ 4267 if (capacity <= 0x1000) { 4268 un_g->dkg_nhead = 2; 4269 un_g->dkg_ncyl = 80; 4270 un_g->dkg_nsect = capacity / (un_g->dkg_nhead * un_g->dkg_ncyl); 4271 return; 4272 } 4273 4274 /* 4275 * For all devices we calculate cylinders using the 4276 * heads and sectors we assign based on capacity of the 4277 * device. The table is designed to be compatible with the 4278 * way other operating systems lay out fdisk tables for X86 4279 * and to insure that the cylinders never exceed 65535 to 4280 * prevent problems with X86 ioctls that report geometry. 4281 * We use SPT that are multiples of 63, since other OSes that 4282 * are not limited to 16-bits for cylinders stop at 63 SPT 4283 * we make do by using multiples of 63 SPT. 4284 * 4285 * Note than capacities greater than or equal to 1TB will simply 4286 * get the largest geometry from the table. This should be okay 4287 * since disks this large shouldn't be using CHS values anyway. 4288 */ 4289 for (i = 0; CHS_values[i].max_cap < capacity && 4290 CHS_values[i].max_cap != DK_MAX_BLOCKS; i++) 4291 ; 4292 4293 un_g->dkg_nhead = CHS_values[i].nhead; 4294 un_g->dkg_nsect = CHS_values[i].nsect; 4295 } 4296 #endif 4297 4298 4299 /* 4300 * Function: sd_resync_geom_caches 4301 * 4302 * Description: (Re)initialize both geometry caches: the virtual geometry 4303 * information is extracted from the HBA (the "geometry" 4304 * capability), and the physical geometry cache data is 4305 * generated by issuing MODE SENSE commands. 4306 * 4307 * Arguments: un - driver soft state (unit) structure 4308 * capacity - disk capacity in #blocks 4309 * lbasize - disk block size in bytes 4310 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4311 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4312 * to use the USCSI "direct" chain and bypass the normal 4313 * command waitq. 4314 * 4315 * Context: Kernel thread only (can sleep). 4316 */ 4317 4318 static void 4319 sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 4320 int path_flag) 4321 { 4322 struct geom_cache pgeom; 4323 struct geom_cache *pgeom_p = &pgeom; 4324 int spc; 4325 unsigned short nhead; 4326 unsigned short nsect; 4327 4328 ASSERT(un != NULL); 4329 ASSERT(mutex_owned(SD_MUTEX(un))); 4330 4331 /* 4332 * Ask the controller for its logical geometry. 4333 * Note: if the HBA does not support scsi_ifgetcap("geometry"), 4334 * then the lgeom cache will be invalid. 4335 */ 4336 sd_get_virtual_geometry(un, capacity, lbasize); 4337 4338 /* 4339 * Initialize the pgeom cache from lgeom, so that if MODE SENSE 4340 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values. 4341 */ 4342 if (un->un_lgeom.g_nsect == 0 || un->un_lgeom.g_nhead == 0) { 4343 /* 4344 * Note: Perhaps this needs to be more adaptive? The rationale 4345 * is that, if there's no HBA geometry from the HBA driver, any 4346 * guess is good, since this is the physical geometry. If MODE 4347 * SENSE fails this gives a max cylinder size for non-LBA access 4348 */ 4349 nhead = 255; 4350 nsect = 63; 4351 } else { 4352 nhead = un->un_lgeom.g_nhead; 4353 nsect = un->un_lgeom.g_nsect; 4354 } 4355 4356 if (ISCD(un)) { 4357 pgeom_p->g_nhead = 1; 4358 pgeom_p->g_nsect = nsect * nhead; 4359 } else { 4360 pgeom_p->g_nhead = nhead; 4361 pgeom_p->g_nsect = nsect; 4362 } 4363 4364 spc = pgeom_p->g_nhead * pgeom_p->g_nsect; 4365 pgeom_p->g_capacity = capacity; 4366 pgeom_p->g_ncyl = pgeom_p->g_capacity / spc; 4367 pgeom_p->g_acyl = 0; 4368 4369 /* 4370 * Retrieve fresh geometry data from the hardware, stash it 4371 * here temporarily before we rebuild the incore label. 4372 * 4373 * We want to use the MODE SENSE commands to derive the 4374 * physical geometry of the device, but if either command 4375 * fails, the logical geometry is used as the fallback for 4376 * disk label geometry. 4377 */ 4378 mutex_exit(SD_MUTEX(un)); 4379 sd_get_physical_geometry(un, pgeom_p, capacity, lbasize, path_flag); 4380 mutex_enter(SD_MUTEX(un)); 4381 4382 /* 4383 * Now update the real copy while holding the mutex. This 4384 * way the global copy is never in an inconsistent state. 4385 */ 4386 bcopy(pgeom_p, &un->un_pgeom, sizeof (un->un_pgeom)); 4387 4388 SD_INFO(SD_LOG_COMMON, un, "sd_resync_geom_caches: " 4389 "(cached from lgeom)\n"); 4390 SD_INFO(SD_LOG_COMMON, un, 4391 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 4392 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 4393 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 4394 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 4395 "intrlv: %d; rpm: %d\n", un->un_pgeom.g_secsize, 4396 un->un_pgeom.g_capacity, un->un_pgeom.g_intrlv, 4397 un->un_pgeom.g_rpm); 4398 } 4399 4400 4401 /* 4402 * Function: sd_read_fdisk 4403 * 4404 * Description: utility routine to read the fdisk table. 4405 * 4406 * Arguments: un - driver soft state (unit) structure 4407 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4408 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4409 * to use the USCSI "direct" chain and bypass the normal 4410 * command waitq. 4411 * 4412 * Return Code: SD_CMD_SUCCESS 4413 * SD_CMD_FAILURE 4414 * 4415 * Context: Kernel thread only (can sleep). 4416 */ 4417 /* ARGSUSED */ 4418 static int 4419 sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, int path_flag) 4420 { 4421 #if defined(_NO_FDISK_PRESENT) 4422 4423 un->un_solaris_offset = 0; 4424 un->un_solaris_size = capacity; 4425 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4426 return (SD_CMD_SUCCESS); 4427 4428 #elif defined(_FIRMWARE_NEEDS_FDISK) 4429 4430 struct ipart *fdp; 4431 struct mboot *mbp; 4432 struct ipart fdisk[FD_NUMPART]; 4433 int i; 4434 char sigbuf[2]; 4435 caddr_t bufp; 4436 int uidx; 4437 int rval; 4438 int lba = 0; 4439 uint_t solaris_offset; /* offset to solaris part. */ 4440 daddr_t solaris_size; /* size of solaris partition */ 4441 uint32_t blocksize; 4442 4443 ASSERT(un != NULL); 4444 ASSERT(mutex_owned(SD_MUTEX(un))); 4445 ASSERT(un->un_f_tgt_blocksize_is_valid == TRUE); 4446 4447 blocksize = un->un_tgt_blocksize; 4448 4449 /* 4450 * Start off assuming no fdisk table 4451 */ 4452 solaris_offset = 0; 4453 solaris_size = capacity; 4454 4455 mutex_exit(SD_MUTEX(un)); 4456 bufp = kmem_zalloc(blocksize, KM_SLEEP); 4457 rval = sd_send_scsi_READ(un, bufp, blocksize, 0, path_flag); 4458 mutex_enter(SD_MUTEX(un)); 4459 4460 if (rval != 0) { 4461 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4462 "sd_read_fdisk: fdisk read err\n"); 4463 kmem_free(bufp, blocksize); 4464 return (SD_CMD_FAILURE); 4465 } 4466 4467 mbp = (struct mboot *)bufp; 4468 4469 /* 4470 * The fdisk table does not begin on a 4-byte boundary within the 4471 * master boot record, so we copy it to an aligned structure to avoid 4472 * alignment exceptions on some processors. 4473 */ 4474 bcopy(&mbp->parts[0], fdisk, sizeof (fdisk)); 4475 4476 /* 4477 * Check for lba support before verifying sig; sig might not be 4478 * there, say on a blank disk, but the max_chs mark may still 4479 * be present. 4480 * 4481 * Note: LBA support and BEFs are an x86-only concept but this 4482 * code should work OK on SPARC as well. 4483 */ 4484 4485 /* 4486 * First, check for lba-access-ok on root node (or prom root node) 4487 * if present there, don't need to search fdisk table. 4488 */ 4489 if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0, 4490 "lba-access-ok", 0) != 0) { 4491 /* All drives do LBA; don't search fdisk table */ 4492 lba = 1; 4493 } else { 4494 /* Okay, look for mark in fdisk table */ 4495 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4496 /* accumulate "lba" value from all partitions */ 4497 lba = (lba || sd_has_max_chs_vals(fdp)); 4498 } 4499 } 4500 4501 /* 4502 * Next, look for 'no-bef-lba-access' prop on parent. 4503 * Its presence means the realmode driver doesn't support 4504 * LBA, so the target driver shouldn't advertise it as ok. 4505 * This should be a temporary condition; one day all 4506 * BEFs should support the LBA access functions. 4507 */ 4508 if ((lba != 0) && (ddi_getprop(DDI_DEV_T_ANY, 4509 ddi_get_parent(SD_DEVINFO(un)), DDI_PROP_DONTPASS, 4510 "no-bef-lba-access", 0) != 0)) { 4511 /* BEF doesn't support LBA; don't advertise it as ok */ 4512 lba = 0; 4513 } 4514 4515 if (lba != 0) { 4516 dev_t dev = sd_make_device(SD_DEVINFO(un)); 4517 4518 if (ddi_getprop(dev, SD_DEVINFO(un), DDI_PROP_DONTPASS, 4519 "lba-access-ok", 0) == 0) { 4520 /* not found; create it */ 4521 if (ddi_prop_create(dev, SD_DEVINFO(un), 0, 4522 "lba-access-ok", (caddr_t)NULL, 0) != 4523 DDI_PROP_SUCCESS) { 4524 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4525 "sd_read_fdisk: Can't create lba property " 4526 "for instance %d\n", 4527 ddi_get_instance(SD_DEVINFO(un))); 4528 } 4529 } 4530 } 4531 4532 bcopy(&mbp->signature, sigbuf, sizeof (sigbuf)); 4533 4534 /* 4535 * Endian-independent signature check 4536 */ 4537 if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) || 4538 (sigbuf[0] != (MBB_MAGIC & 0xFF))) { 4539 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4540 "sd_read_fdisk: no fdisk\n"); 4541 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4542 rval = SD_CMD_SUCCESS; 4543 goto done; 4544 } 4545 4546 #ifdef SDDEBUG 4547 if (sd_level_mask & SD_LOGMASK_INFO) { 4548 fdp = fdisk; 4549 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_read_fdisk:\n"); 4550 SD_INFO(SD_LOG_ATTACH_DETACH, un, " relsect " 4551 "numsect sysid bootid\n"); 4552 for (i = 0; i < FD_NUMPART; i++, fdp++) { 4553 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4554 " %d: %8d %8d 0x%08x 0x%08x\n", 4555 i, fdp->relsect, fdp->numsect, 4556 fdp->systid, fdp->bootid); 4557 } 4558 } 4559 #endif 4560 4561 /* 4562 * Try to find the unix partition 4563 */ 4564 uidx = -1; 4565 solaris_offset = 0; 4566 solaris_size = 0; 4567 4568 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4569 int relsect; 4570 int numsect; 4571 4572 if (fdp->numsect == 0) { 4573 un->un_fmap[i].fmap_start = 0; 4574 un->un_fmap[i].fmap_nblk = 0; 4575 continue; 4576 } 4577 4578 /* 4579 * Data in the fdisk table is little-endian. 4580 */ 4581 relsect = LE_32(fdp->relsect); 4582 numsect = LE_32(fdp->numsect); 4583 4584 un->un_fmap[i].fmap_start = relsect; 4585 un->un_fmap[i].fmap_nblk = numsect; 4586 4587 if (fdp->systid != SUNIXOS && 4588 fdp->systid != SUNIXOS2 && 4589 fdp->systid != EFI_PMBR) { 4590 continue; 4591 } 4592 4593 /* 4594 * use the last active solaris partition id found 4595 * (there should only be 1 active partition id) 4596 * 4597 * if there are no active solaris partition id 4598 * then use the first inactive solaris partition id 4599 */ 4600 if ((uidx == -1) || (fdp->bootid == ACTIVE)) { 4601 uidx = i; 4602 solaris_offset = relsect; 4603 solaris_size = numsect; 4604 } 4605 } 4606 4607 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk 0x%x 0x%lx", 4608 un->un_solaris_offset, un->un_solaris_size); 4609 4610 rval = SD_CMD_SUCCESS; 4611 4612 done: 4613 4614 /* 4615 * Clear the VTOC info, only if the Solaris partition entry 4616 * has moved, changed size, been deleted, or if the size of 4617 * the partition is too small to even fit the label sector. 4618 */ 4619 if ((un->un_solaris_offset != solaris_offset) || 4620 (un->un_solaris_size != solaris_size) || 4621 solaris_size <= DK_LABEL_LOC) { 4622 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk moved 0x%x 0x%lx", 4623 solaris_offset, solaris_size); 4624 bzero(&un->un_g, sizeof (struct dk_geom)); 4625 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 4626 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 4627 un->un_f_geometry_is_valid = FALSE; 4628 } 4629 un->un_solaris_offset = solaris_offset; 4630 un->un_solaris_size = solaris_size; 4631 kmem_free(bufp, blocksize); 4632 return (rval); 4633 4634 #else /* #elif defined(_FIRMWARE_NEEDS_FDISK) */ 4635 #error "fdisk table presence undetermined for this platform." 4636 #endif /* #if defined(_NO_FDISK_PRESENT) */ 4637 } 4638 4639 4640 /* 4641 * Function: sd_get_physical_geometry 4642 * 4643 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 4644 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 4645 * target, and use this information to initialize the physical 4646 * geometry cache specified by pgeom_p. 4647 * 4648 * MODE SENSE is an optional command, so failure in this case 4649 * does not necessarily denote an error. We want to use the 4650 * MODE SENSE commands to derive the physical geometry of the 4651 * device, but if either command fails, the logical geometry is 4652 * used as the fallback for disk label geometry. 4653 * 4654 * This requires that un->un_blockcount and un->un_tgt_blocksize 4655 * have already been initialized for the current target and 4656 * that the current values be passed as args so that we don't 4657 * end up ever trying to use -1 as a valid value. This could 4658 * happen if either value is reset while we're not holding 4659 * the mutex. 4660 * 4661 * Arguments: un - driver soft state (unit) structure 4662 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4663 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4664 * to use the USCSI "direct" chain and bypass the normal 4665 * command waitq. 4666 * 4667 * Context: Kernel thread only (can sleep). 4668 */ 4669 4670 static void 4671 sd_get_physical_geometry(struct sd_lun *un, struct geom_cache *pgeom_p, 4672 int capacity, int lbasize, int path_flag) 4673 { 4674 struct mode_format *page3p; 4675 struct mode_geometry *page4p; 4676 struct mode_header *headerp; 4677 int sector_size; 4678 int nsect; 4679 int nhead; 4680 int ncyl; 4681 int intrlv; 4682 int spc; 4683 int modesense_capacity; 4684 int rpm; 4685 int bd_len; 4686 int mode_header_length; 4687 uchar_t *p3bufp; 4688 uchar_t *p4bufp; 4689 int cdbsize; 4690 4691 ASSERT(un != NULL); 4692 ASSERT(!(mutex_owned(SD_MUTEX(un)))); 4693 4694 if (un->un_f_blockcount_is_valid != TRUE) { 4695 return; 4696 } 4697 4698 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 4699 return; 4700 } 4701 4702 if (lbasize == 0) { 4703 if (ISCD(un)) { 4704 lbasize = 2048; 4705 } else { 4706 lbasize = un->un_sys_blocksize; 4707 } 4708 } 4709 pgeom_p->g_secsize = (unsigned short)lbasize; 4710 4711 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 4712 4713 /* 4714 * Retrieve MODE SENSE page 3 - Format Device Page 4715 */ 4716 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 4717 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp, 4718 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag) 4719 != 0) { 4720 SD_ERROR(SD_LOG_COMMON, un, 4721 "sd_get_physical_geometry: mode sense page 3 failed\n"); 4722 goto page3_exit; 4723 } 4724 4725 /* 4726 * Determine size of Block Descriptors in order to locate the mode 4727 * page data. ATAPI devices return 0, SCSI devices should return 4728 * MODE_BLK_DESC_LENGTH. 4729 */ 4730 headerp = (struct mode_header *)p3bufp; 4731 if (un->un_f_cfg_is_atapi == TRUE) { 4732 struct mode_header_grp2 *mhp = 4733 (struct mode_header_grp2 *)headerp; 4734 mode_header_length = MODE_HEADER_LENGTH_GRP2; 4735 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4736 } else { 4737 mode_header_length = MODE_HEADER_LENGTH; 4738 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4739 } 4740 4741 if (bd_len > MODE_BLK_DESC_LENGTH) { 4742 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4743 "received unexpected bd_len of %d, page3\n", bd_len); 4744 goto page3_exit; 4745 } 4746 4747 page3p = (struct mode_format *) 4748 ((caddr_t)headerp + mode_header_length + bd_len); 4749 4750 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 4751 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4752 "mode sense pg3 code mismatch %d\n", 4753 page3p->mode_page.code); 4754 goto page3_exit; 4755 } 4756 4757 /* 4758 * Use this physical geometry data only if BOTH MODE SENSE commands 4759 * complete successfully; otherwise, revert to the logical geometry. 4760 * So, we need to save everything in temporary variables. 4761 */ 4762 sector_size = BE_16(page3p->data_bytes_sect); 4763 4764 /* 4765 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 4766 */ 4767 if (sector_size == 0) { 4768 sector_size = (ISCD(un)) ? 2048 : un->un_sys_blocksize; 4769 } else { 4770 sector_size &= ~(un->un_sys_blocksize - 1); 4771 } 4772 4773 nsect = BE_16(page3p->sect_track); 4774 intrlv = BE_16(page3p->interleave); 4775 4776 SD_INFO(SD_LOG_COMMON, un, 4777 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 4778 SD_INFO(SD_LOG_COMMON, un, 4779 " mode page: %d; nsect: %d; sector size: %d;\n", 4780 page3p->mode_page.code, nsect, sector_size); 4781 SD_INFO(SD_LOG_COMMON, un, 4782 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 4783 BE_16(page3p->track_skew), 4784 BE_16(page3p->cylinder_skew)); 4785 4786 4787 /* 4788 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 4789 */ 4790 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 4791 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp, 4792 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag) 4793 != 0) { 4794 SD_ERROR(SD_LOG_COMMON, un, 4795 "sd_get_physical_geometry: mode sense page 4 failed\n"); 4796 goto page4_exit; 4797 } 4798 4799 /* 4800 * Determine size of Block Descriptors in order to locate the mode 4801 * page data. ATAPI devices return 0, SCSI devices should return 4802 * MODE_BLK_DESC_LENGTH. 4803 */ 4804 headerp = (struct mode_header *)p4bufp; 4805 if (un->un_f_cfg_is_atapi == TRUE) { 4806 struct mode_header_grp2 *mhp = 4807 (struct mode_header_grp2 *)headerp; 4808 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4809 } else { 4810 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4811 } 4812 4813 if (bd_len > MODE_BLK_DESC_LENGTH) { 4814 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4815 "received unexpected bd_len of %d, page4\n", bd_len); 4816 goto page4_exit; 4817 } 4818 4819 page4p = (struct mode_geometry *) 4820 ((caddr_t)headerp + mode_header_length + bd_len); 4821 4822 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 4823 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4824 "mode sense pg4 code mismatch %d\n", 4825 page4p->mode_page.code); 4826 goto page4_exit; 4827 } 4828 4829 /* 4830 * Stash the data now, after we know that both commands completed. 4831 */ 4832 4833 mutex_enter(SD_MUTEX(un)); 4834 4835 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 4836 spc = nhead * nsect; 4837 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 4838 rpm = BE_16(page4p->rpm); 4839 4840 modesense_capacity = spc * ncyl; 4841 4842 SD_INFO(SD_LOG_COMMON, un, 4843 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 4844 SD_INFO(SD_LOG_COMMON, un, 4845 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 4846 SD_INFO(SD_LOG_COMMON, un, 4847 " computed capacity(h*s*c): %d;\n", modesense_capacity); 4848 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 4849 (void *)pgeom_p, capacity); 4850 4851 /* 4852 * Compensate if the drive's geometry is not rectangular, i.e., 4853 * the product of C * H * S returned by MODE SENSE >= that returned 4854 * by read capacity. This is an idiosyncrasy of the original x86 4855 * disk subsystem. 4856 */ 4857 if (modesense_capacity >= capacity) { 4858 SD_INFO(SD_LOG_COMMON, un, 4859 "sd_get_physical_geometry: adjusting acyl; " 4860 "old: %d; new: %d\n", pgeom_p->g_acyl, 4861 (modesense_capacity - capacity + spc - 1) / spc); 4862 if (sector_size != 0) { 4863 /* 1243403: NEC D38x7 drives don't support sec size */ 4864 pgeom_p->g_secsize = (unsigned short)sector_size; 4865 } 4866 pgeom_p->g_nsect = (unsigned short)nsect; 4867 pgeom_p->g_nhead = (unsigned short)nhead; 4868 pgeom_p->g_capacity = capacity; 4869 pgeom_p->g_acyl = 4870 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 4871 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 4872 } 4873 4874 pgeom_p->g_rpm = (unsigned short)rpm; 4875 pgeom_p->g_intrlv = (unsigned short)intrlv; 4876 4877 SD_INFO(SD_LOG_COMMON, un, 4878 "sd_get_physical_geometry: mode sense geometry:\n"); 4879 SD_INFO(SD_LOG_COMMON, un, 4880 " nsect: %d; sector size: %d; interlv: %d\n", 4881 nsect, sector_size, intrlv); 4882 SD_INFO(SD_LOG_COMMON, un, 4883 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 4884 nhead, ncyl, rpm, modesense_capacity); 4885 SD_INFO(SD_LOG_COMMON, un, 4886 "sd_get_physical_geometry: (cached)\n"); 4887 SD_INFO(SD_LOG_COMMON, un, 4888 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 4889 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 4890 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 4891 SD_INFO(SD_LOG_COMMON, un, 4892 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 4893 un->un_pgeom.g_secsize, un->un_pgeom.g_capacity, 4894 un->un_pgeom.g_intrlv, un->un_pgeom.g_rpm); 4895 4896 mutex_exit(SD_MUTEX(un)); 4897 4898 page4_exit: 4899 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 4900 page3_exit: 4901 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 4902 } 4903 4904 4905 /* 4906 * Function: sd_get_virtual_geometry 4907 * 4908 * Description: Ask the controller to tell us about the target device. 4909 * 4910 * Arguments: un - pointer to softstate 4911 * capacity - disk capacity in #blocks 4912 * lbasize - disk block size in bytes 4913 * 4914 * Context: Kernel thread only 4915 */ 4916 4917 static void 4918 sd_get_virtual_geometry(struct sd_lun *un, int capacity, int lbasize) 4919 { 4920 struct geom_cache *lgeom_p = &un->un_lgeom; 4921 uint_t geombuf; 4922 int spc; 4923 4924 ASSERT(un != NULL); 4925 ASSERT(mutex_owned(SD_MUTEX(un))); 4926 4927 mutex_exit(SD_MUTEX(un)); 4928 4929 /* Set sector size, and total number of sectors */ 4930 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 4931 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 4932 4933 /* Let the HBA tell us its geometry */ 4934 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 4935 4936 mutex_enter(SD_MUTEX(un)); 4937 4938 /* A value of -1 indicates an undefined "geometry" property */ 4939 if (geombuf == (-1)) { 4940 return; 4941 } 4942 4943 /* Initialize the logical geometry cache. */ 4944 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 4945 lgeom_p->g_nsect = geombuf & 0xffff; 4946 lgeom_p->g_secsize = un->un_sys_blocksize; 4947 4948 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 4949 4950 /* 4951 * Note: The driver originally converted the capacity value from 4952 * target blocks to system blocks. However, the capacity value passed 4953 * to this routine is already in terms of system blocks (this scaling 4954 * is done when the READ CAPACITY command is issued and processed). 4955 * This 'error' may have gone undetected because the usage of g_ncyl 4956 * (which is based upon g_capacity) is very limited within the driver 4957 */ 4958 lgeom_p->g_capacity = capacity; 4959 4960 /* 4961 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 4962 * hba may return zero values if the device has been removed. 4963 */ 4964 if (spc == 0) { 4965 lgeom_p->g_ncyl = 0; 4966 } else { 4967 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 4968 } 4969 lgeom_p->g_acyl = 0; 4970 4971 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 4972 SD_INFO(SD_LOG_COMMON, un, 4973 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 4974 un->un_lgeom.g_ncyl, un->un_lgeom.g_acyl, 4975 un->un_lgeom.g_nhead, un->un_lgeom.g_nsect); 4976 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 4977 "intrlv: %d; rpm: %d\n", un->un_lgeom.g_secsize, 4978 un->un_lgeom.g_capacity, un->un_lgeom.g_intrlv, un->un_lgeom.g_rpm); 4979 } 4980 4981 4982 /* 4983 * Function: sd_update_block_info 4984 * 4985 * Description: Calculate a byte count to sector count bitshift value 4986 * from sector size. 4987 * 4988 * Arguments: un: unit struct. 4989 * lbasize: new target sector size 4990 * capacity: new target capacity, ie. block count 4991 * 4992 * Context: Kernel thread context 4993 */ 4994 4995 static void 4996 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 4997 { 4998 if (lbasize != 0) { 4999 un->un_tgt_blocksize = lbasize; 5000 un->un_f_tgt_blocksize_is_valid = TRUE; 5001 } 5002 5003 if (capacity != 0) { 5004 un->un_blockcount = capacity; 5005 un->un_f_blockcount_is_valid = TRUE; 5006 } 5007 } 5008 5009 5010 static void 5011 sd_swap_efi_gpt(efi_gpt_t *e) 5012 { 5013 _NOTE(ASSUMING_PROTECTED(*e)) 5014 e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature); 5015 e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision); 5016 e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize); 5017 e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32); 5018 e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA); 5019 e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA); 5020 e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA); 5021 e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA); 5022 UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID); 5023 e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA); 5024 e->efi_gpt_NumberOfPartitionEntries = 5025 LE_32(e->efi_gpt_NumberOfPartitionEntries); 5026 e->efi_gpt_SizeOfPartitionEntry = 5027 LE_32(e->efi_gpt_SizeOfPartitionEntry); 5028 e->efi_gpt_PartitionEntryArrayCRC32 = 5029 LE_32(e->efi_gpt_PartitionEntryArrayCRC32); 5030 } 5031 5032 static void 5033 sd_swap_efi_gpe(int nparts, efi_gpe_t *p) 5034 { 5035 int i; 5036 5037 _NOTE(ASSUMING_PROTECTED(*p)) 5038 for (i = 0; i < nparts; i++) { 5039 UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID, 5040 p[i].efi_gpe_PartitionTypeGUID); 5041 p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA); 5042 p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA); 5043 /* PartitionAttrs */ 5044 } 5045 } 5046 5047 static int 5048 sd_validate_efi(efi_gpt_t *labp) 5049 { 5050 if (labp->efi_gpt_Signature != EFI_SIGNATURE) 5051 return (EINVAL); 5052 /* at least 96 bytes in this version of the spec. */ 5053 if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) > 5054 labp->efi_gpt_HeaderSize) 5055 return (EINVAL); 5056 /* this should be 128 bytes */ 5057 if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t)) 5058 return (EINVAL); 5059 return (0); 5060 } 5061 5062 static int 5063 sd_use_efi(struct sd_lun *un, int path_flag) 5064 { 5065 int i; 5066 int rval = 0; 5067 efi_gpe_t *partitions; 5068 uchar_t *buf; 5069 uint_t lbasize; 5070 uint64_t cap; 5071 uint_t nparts; 5072 diskaddr_t gpe_lba; 5073 5074 ASSERT(mutex_owned(SD_MUTEX(un))); 5075 lbasize = un->un_tgt_blocksize; 5076 5077 mutex_exit(SD_MUTEX(un)); 5078 5079 buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 5080 5081 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 5082 rval = EINVAL; 5083 goto done_err; 5084 } 5085 5086 rval = sd_send_scsi_READ(un, buf, lbasize, 0, path_flag); 5087 if (rval) { 5088 goto done_err; 5089 } 5090 if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) { 5091 /* not ours */ 5092 rval = ESRCH; 5093 goto done_err; 5094 } 5095 5096 rval = sd_send_scsi_READ(un, buf, lbasize, 1, path_flag); 5097 if (rval) { 5098 goto done_err; 5099 } 5100 sd_swap_efi_gpt((efi_gpt_t *)buf); 5101 5102 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) { 5103 /* 5104 * Couldn't read the primary, try the backup. Our 5105 * capacity at this point could be based on CHS, so 5106 * check what the device reports. 5107 */ 5108 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 5109 path_flag); 5110 if (rval) { 5111 goto done_err; 5112 } 5113 if ((rval = sd_send_scsi_READ(un, buf, lbasize, 5114 cap - 1, path_flag)) != 0) { 5115 goto done_err; 5116 } 5117 sd_swap_efi_gpt((efi_gpt_t *)buf); 5118 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) 5119 goto done_err; 5120 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5121 "primary label corrupt; using backup\n"); 5122 } 5123 5124 nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries; 5125 gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA; 5126 5127 rval = sd_send_scsi_READ(un, buf, EFI_MIN_ARRAY_SIZE, gpe_lba, 5128 path_flag); 5129 if (rval) { 5130 goto done_err; 5131 } 5132 partitions = (efi_gpe_t *)buf; 5133 5134 if (nparts > MAXPART) { 5135 nparts = MAXPART; 5136 } 5137 sd_swap_efi_gpe(nparts, partitions); 5138 5139 mutex_enter(SD_MUTEX(un)); 5140 5141 /* Fill in partition table. */ 5142 for (i = 0; i < nparts; i++) { 5143 if (partitions->efi_gpe_StartingLBA != 0 || 5144 partitions->efi_gpe_EndingLBA != 0) { 5145 un->un_map[i].dkl_cylno = 5146 partitions->efi_gpe_StartingLBA; 5147 un->un_map[i].dkl_nblk = 5148 partitions->efi_gpe_EndingLBA - 5149 partitions->efi_gpe_StartingLBA + 1; 5150 un->un_offset[i] = 5151 partitions->efi_gpe_StartingLBA; 5152 } 5153 if (i == WD_NODE) { 5154 /* 5155 * minor number 7 corresponds to the whole disk 5156 */ 5157 un->un_map[i].dkl_cylno = 0; 5158 un->un_map[i].dkl_nblk = un->un_blockcount; 5159 un->un_offset[i] = 0; 5160 } 5161 partitions++; 5162 } 5163 un->un_solaris_offset = 0; 5164 un->un_solaris_size = cap; 5165 un->un_f_geometry_is_valid = TRUE; 5166 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5167 return (0); 5168 5169 done_err: 5170 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5171 mutex_enter(SD_MUTEX(un)); 5172 /* 5173 * if we didn't find something that could look like a VTOC 5174 * and the disk is over 1TB, we know there isn't a valid label. 5175 * Otherwise let sd_uselabel decide what to do. We only 5176 * want to invalidate this if we're certain the label isn't 5177 * valid because sd_prop_op will now fail, which in turn 5178 * causes things like opens and stats on the partition to fail. 5179 */ 5180 if ((un->un_blockcount > DK_MAX_BLOCKS) && (rval != ESRCH)) { 5181 un->un_f_geometry_is_valid = FALSE; 5182 } 5183 return (rval); 5184 } 5185 5186 5187 /* 5188 * Function: sd_uselabel 5189 * 5190 * Description: Validate the disk label and update the relevant data (geometry, 5191 * partition, vtoc, and capacity data) in the sd_lun struct. 5192 * Marks the geometry of the unit as being valid. 5193 * 5194 * Arguments: un: unit struct. 5195 * dk_label: disk label 5196 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 5197 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 5198 * to use the USCSI "direct" chain and bypass the normal 5199 * command waitq. 5200 * 5201 * Return Code: SD_LABEL_IS_VALID: Label read from disk is OK; geometry, 5202 * partition, vtoc, and capacity data are good. 5203 * 5204 * SD_LABEL_IS_INVALID: Magic number or checksum error in the 5205 * label; or computed capacity does not jibe with capacity 5206 * reported from the READ CAPACITY command. 5207 * 5208 * Context: Kernel thread only (can sleep). 5209 */ 5210 5211 static int 5212 sd_uselabel(struct sd_lun *un, struct dk_label *labp, int path_flag) 5213 { 5214 short *sp; 5215 short sum; 5216 short count; 5217 int label_error = SD_LABEL_IS_VALID; 5218 int i; 5219 int capacity; 5220 int part_end; 5221 int track_capacity; 5222 int err; 5223 #if defined(_SUNOS_VTOC_16) 5224 struct dkl_partition *vpartp; 5225 #endif 5226 ASSERT(un != NULL); 5227 ASSERT(mutex_owned(SD_MUTEX(un))); 5228 5229 /* Validate the magic number of the label. */ 5230 if (labp->dkl_magic != DKL_MAGIC) { 5231 #if defined(__sparc) 5232 if ((un->un_state == SD_STATE_NORMAL) && 5233 !ISREMOVABLE(un)) { 5234 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5235 "Corrupt label; wrong magic number\n"); 5236 } 5237 #endif 5238 return (SD_LABEL_IS_INVALID); 5239 } 5240 5241 /* Validate the checksum of the label. */ 5242 sp = (short *)labp; 5243 sum = 0; 5244 count = sizeof (struct dk_label) / sizeof (short); 5245 while (count--) { 5246 sum ^= *sp++; 5247 } 5248 5249 if (sum != 0) { 5250 #if defined(_SUNOS_VTOC_16) 5251 if (un->un_state == SD_STATE_NORMAL && !ISCD(un)) { 5252 #elif defined(_SUNOS_VTOC_8) 5253 if (un->un_state == SD_STATE_NORMAL && !ISREMOVABLE(un)) { 5254 #endif 5255 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5256 "Corrupt label - label checksum failed\n"); 5257 } 5258 return (SD_LABEL_IS_INVALID); 5259 } 5260 5261 5262 /* 5263 * Fill in geometry structure with data from label. 5264 */ 5265 bzero(&un->un_g, sizeof (struct dk_geom)); 5266 un->un_g.dkg_ncyl = labp->dkl_ncyl; 5267 un->un_g.dkg_acyl = labp->dkl_acyl; 5268 un->un_g.dkg_bcyl = 0; 5269 un->un_g.dkg_nhead = labp->dkl_nhead; 5270 un->un_g.dkg_nsect = labp->dkl_nsect; 5271 un->un_g.dkg_intrlv = labp->dkl_intrlv; 5272 5273 #if defined(_SUNOS_VTOC_8) 5274 un->un_g.dkg_gap1 = labp->dkl_gap1; 5275 un->un_g.dkg_gap2 = labp->dkl_gap2; 5276 un->un_g.dkg_bhead = labp->dkl_bhead; 5277 #endif 5278 #if defined(_SUNOS_VTOC_16) 5279 un->un_dkg_skew = labp->dkl_skew; 5280 #endif 5281 5282 #if defined(__i386) || defined(__amd64) 5283 un->un_g.dkg_apc = labp->dkl_apc; 5284 #endif 5285 5286 /* 5287 * Currently we rely on the values in the label being accurate. If 5288 * dlk_rpm or dlk_pcly are zero in the label, use a default value. 5289 * 5290 * Note: In the future a MODE SENSE may be used to retrieve this data, 5291 * although this command is optional in SCSI-2. 5292 */ 5293 un->un_g.dkg_rpm = (labp->dkl_rpm != 0) ? labp->dkl_rpm : 3600; 5294 un->un_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl : 5295 (un->un_g.dkg_ncyl + un->un_g.dkg_acyl); 5296 5297 /* 5298 * The Read and Write reinstruct values may not be valid 5299 * for older disks. 5300 */ 5301 un->un_g.dkg_read_reinstruct = labp->dkl_read_reinstruct; 5302 un->un_g.dkg_write_reinstruct = labp->dkl_write_reinstruct; 5303 5304 /* Fill in partition table. */ 5305 #if defined(_SUNOS_VTOC_8) 5306 for (i = 0; i < NDKMAP; i++) { 5307 un->un_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno; 5308 un->un_map[i].dkl_nblk = labp->dkl_map[i].dkl_nblk; 5309 } 5310 #endif 5311 #if defined(_SUNOS_VTOC_16) 5312 vpartp = labp->dkl_vtoc.v_part; 5313 track_capacity = labp->dkl_nhead * labp->dkl_nsect; 5314 5315 for (i = 0; i < NDKMAP; i++, vpartp++) { 5316 un->un_map[i].dkl_cylno = vpartp->p_start / track_capacity; 5317 un->un_map[i].dkl_nblk = vpartp->p_size; 5318 } 5319 #endif 5320 5321 /* Fill in VTOC Structure. */ 5322 bcopy(&labp->dkl_vtoc, &un->un_vtoc, sizeof (struct dk_vtoc)); 5323 #if defined(_SUNOS_VTOC_8) 5324 /* 5325 * The 8-slice vtoc does not include the ascii label; save it into 5326 * the device's soft state structure here. 5327 */ 5328 bcopy(labp->dkl_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 5329 #endif 5330 5331 /* Mark the geometry as valid. */ 5332 un->un_f_geometry_is_valid = TRUE; 5333 5334 /* Now look for a valid capacity. */ 5335 track_capacity = (un->un_g.dkg_nhead * un->un_g.dkg_nsect); 5336 capacity = (un->un_g.dkg_ncyl * track_capacity); 5337 5338 if (un->un_g.dkg_acyl) { 5339 #if defined(__i386) || defined(__amd64) 5340 /* we may have > 1 alts cylinder */ 5341 capacity += (track_capacity * un->un_g.dkg_acyl); 5342 #else 5343 capacity += track_capacity; 5344 #endif 5345 } 5346 5347 /* 5348 * At this point, un->un_blockcount should contain valid data from 5349 * the READ CAPACITY command. 5350 */ 5351 if (un->un_f_blockcount_is_valid != TRUE) { 5352 /* 5353 * We have a situation where the target didn't give us a good 5354 * READ CAPACITY value, yet there appears to be a valid label. 5355 * In this case, we'll fake the capacity. 5356 */ 5357 un->un_blockcount = capacity; 5358 un->un_f_blockcount_is_valid = TRUE; 5359 goto done; 5360 } 5361 5362 5363 if ((capacity <= un->un_blockcount) || 5364 (un->un_state != SD_STATE_NORMAL)) { 5365 #if defined(_SUNOS_VTOC_8) 5366 /* 5367 * We can't let this happen on drives that are subdivided 5368 * into logical disks (i.e., that have an fdisk table). 5369 * The un_blockcount field should always hold the full media 5370 * size in sectors, period. This code would overwrite 5371 * un_blockcount with the size of the Solaris fdisk partition. 5372 */ 5373 SD_ERROR(SD_LOG_COMMON, un, 5374 "sd_uselabel: Label %d blocks; Drive %d blocks\n", 5375 capacity, un->un_blockcount); 5376 un->un_blockcount = capacity; 5377 un->un_f_blockcount_is_valid = TRUE; 5378 #endif /* defined(_SUNOS_VTOC_8) */ 5379 goto done; 5380 } 5381 5382 if (ISCD(un)) { 5383 /* For CDROMs, we trust that the data in the label is OK. */ 5384 #if defined(_SUNOS_VTOC_8) 5385 for (i = 0; i < NDKMAP; i++) { 5386 part_end = labp->dkl_nhead * labp->dkl_nsect * 5387 labp->dkl_map[i].dkl_cylno + 5388 labp->dkl_map[i].dkl_nblk - 1; 5389 5390 if ((labp->dkl_map[i].dkl_nblk) && 5391 (part_end > un->un_blockcount)) { 5392 un->un_f_geometry_is_valid = FALSE; 5393 break; 5394 } 5395 } 5396 #endif 5397 #if defined(_SUNOS_VTOC_16) 5398 vpartp = &(labp->dkl_vtoc.v_part[0]); 5399 for (i = 0; i < NDKMAP; i++, vpartp++) { 5400 part_end = vpartp->p_start + vpartp->p_size; 5401 if ((vpartp->p_size > 0) && 5402 (part_end > un->un_blockcount)) { 5403 un->un_f_geometry_is_valid = FALSE; 5404 break; 5405 } 5406 } 5407 #endif 5408 } else { 5409 uint64_t t_capacity; 5410 uint32_t t_lbasize; 5411 5412 mutex_exit(SD_MUTEX(un)); 5413 err = sd_send_scsi_READ_CAPACITY(un, &t_capacity, &t_lbasize, 5414 path_flag); 5415 ASSERT(t_capacity <= DK_MAX_BLOCKS); 5416 mutex_enter(SD_MUTEX(un)); 5417 5418 if (err == 0) { 5419 sd_update_block_info(un, t_lbasize, t_capacity); 5420 } 5421 5422 if (capacity > un->un_blockcount) { 5423 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5424 "Corrupt label - bad geometry\n"); 5425 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 5426 "Label says %u blocks; Drive says %llu blocks\n", 5427 capacity, (unsigned long long)un->un_blockcount); 5428 un->un_f_geometry_is_valid = FALSE; 5429 label_error = SD_LABEL_IS_INVALID; 5430 } 5431 } 5432 5433 done: 5434 5435 SD_INFO(SD_LOG_COMMON, un, "sd_uselabel: (label geometry)\n"); 5436 SD_INFO(SD_LOG_COMMON, un, 5437 " ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n", 5438 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 5439 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 5440 SD_INFO(SD_LOG_COMMON, un, 5441 " lbasize: %d; capacity: %d; intrlv: %d; rpm: %d\n", 5442 un->un_tgt_blocksize, un->un_blockcount, 5443 un->un_g.dkg_intrlv, un->un_g.dkg_rpm); 5444 SD_INFO(SD_LOG_COMMON, un, " wrt_reinstr: %d; rd_reinstr: %d\n", 5445 un->un_g.dkg_write_reinstruct, un->un_g.dkg_read_reinstruct); 5446 5447 ASSERT(mutex_owned(SD_MUTEX(un))); 5448 5449 return (label_error); 5450 } 5451 5452 5453 /* 5454 * Function: sd_build_default_label 5455 * 5456 * Description: Generate a default label for those devices that do not have 5457 * one, e.g., new media, removable cartridges, etc.. 5458 * 5459 * Context: Kernel thread only 5460 */ 5461 5462 static void 5463 sd_build_default_label(struct sd_lun *un) 5464 { 5465 #if defined(_SUNOS_VTOC_16) 5466 uint_t phys_spc; 5467 uint_t disksize; 5468 struct dk_geom un_g; 5469 #endif 5470 5471 ASSERT(un != NULL); 5472 ASSERT(mutex_owned(SD_MUTEX(un))); 5473 5474 #if defined(_SUNOS_VTOC_8) 5475 /* 5476 * Note: This is a legacy check for non-removable devices on VTOC_8 5477 * only. This may be a valid check for VTOC_16 as well. 5478 */ 5479 if (!ISREMOVABLE(un)) { 5480 return; 5481 } 5482 #endif 5483 5484 bzero(&un->un_g, sizeof (struct dk_geom)); 5485 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5486 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 5487 5488 #if defined(_SUNOS_VTOC_8) 5489 5490 /* 5491 * It's a REMOVABLE media, therefore no label (on sparc, anyway). 5492 * But it is still necessary to set up various geometry information, 5493 * and we are doing this here. 5494 */ 5495 5496 /* 5497 * For the rpm, we use the minimum for the disk. For the head, cyl, 5498 * and number of sector per track, if the capacity <= 1GB, head = 64, 5499 * sect = 32. else head = 255, sect 63 Note: the capacity should be 5500 * equal to C*H*S values. This will cause some truncation of size due 5501 * to round off errors. For CD-ROMs, this truncation can have adverse 5502 * side effects, so returning ncyl and nhead as 1. The nsect will 5503 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569) 5504 */ 5505 if (ISCD(un)) { 5506 /* 5507 * Preserve the old behavior for non-writable 5508 * medias. Since dkg_nsect is a ushort, it 5509 * will lose bits as cdroms have more than 5510 * 65536 sectors. So if we recalculate 5511 * capacity, it will become much shorter. 5512 * But the dkg_* information is not 5513 * used for CDROMs so it is OK. But for 5514 * Writable CDs we need this information 5515 * to be valid (for newfs say). So we 5516 * make nsect and nhead > 1 that way 5517 * nsect can still stay within ushort limit 5518 * without losing any bits. 5519 */ 5520 if (un->un_f_mmc_writable_media == TRUE) { 5521 un->un_g.dkg_nhead = 64; 5522 un->un_g.dkg_nsect = 32; 5523 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 5524 un->un_blockcount = un->un_g.dkg_ncyl * 5525 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 5526 } else { 5527 un->un_g.dkg_ncyl = 1; 5528 un->un_g.dkg_nhead = 1; 5529 un->un_g.dkg_nsect = un->un_blockcount; 5530 } 5531 } else { 5532 if (un->un_blockcount <= 0x1000) { 5533 /* unlabeled SCSI floppy device */ 5534 un->un_g.dkg_nhead = 2; 5535 un->un_g.dkg_ncyl = 80; 5536 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 5537 } else if (un->un_blockcount <= 0x200000) { 5538 un->un_g.dkg_nhead = 64; 5539 un->un_g.dkg_nsect = 32; 5540 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 5541 } else { 5542 un->un_g.dkg_nhead = 255; 5543 un->un_g.dkg_nsect = 63; 5544 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 5545 } 5546 un->un_blockcount = 5547 un->un_g.dkg_ncyl * un->un_g.dkg_nhead * un->un_g.dkg_nsect; 5548 } 5549 5550 un->un_g.dkg_acyl = 0; 5551 un->un_g.dkg_bcyl = 0; 5552 un->un_g.dkg_rpm = 200; 5553 un->un_asciilabel[0] = '\0'; 5554 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl; 5555 5556 un->un_map[0].dkl_cylno = 0; 5557 un->un_map[0].dkl_nblk = un->un_blockcount; 5558 un->un_map[2].dkl_cylno = 0; 5559 un->un_map[2].dkl_nblk = un->un_blockcount; 5560 5561 #elif defined(_SUNOS_VTOC_16) 5562 5563 if (un->un_solaris_size == 0) { 5564 /* 5565 * Got fdisk table but no solaris entry therefore 5566 * don't create a default label 5567 */ 5568 un->un_f_geometry_is_valid = TRUE; 5569 return; 5570 } 5571 5572 /* 5573 * For CDs we continue to use the physical geometry to calculate 5574 * number of cylinders. All other devices must convert the 5575 * physical geometry (geom_cache) to values that will fit 5576 * in a dk_geom structure. 5577 */ 5578 if (ISCD(un)) { 5579 phys_spc = un->un_pgeom.g_nhead * un->un_pgeom.g_nsect; 5580 } else { 5581 /* Convert physical geometry to disk geometry */ 5582 bzero(&un_g, sizeof (struct dk_geom)); 5583 sd_convert_geometry(un->un_blockcount, &un_g); 5584 bcopy(&un_g, &un->un_g, sizeof (un->un_g)); 5585 phys_spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 5586 } 5587 5588 un->un_g.dkg_pcyl = un->un_solaris_size / phys_spc; 5589 un->un_g.dkg_acyl = DK_ACYL; 5590 un->un_g.dkg_ncyl = un->un_g.dkg_pcyl - DK_ACYL; 5591 disksize = un->un_g.dkg_ncyl * phys_spc; 5592 5593 if (ISCD(un)) { 5594 /* 5595 * CD's don't use the "heads * sectors * cyls"-type of 5596 * geometry, but instead use the entire capacity of the media. 5597 */ 5598 disksize = un->un_solaris_size; 5599 un->un_g.dkg_nhead = 1; 5600 un->un_g.dkg_nsect = 1; 5601 un->un_g.dkg_rpm = 5602 (un->un_pgeom.g_rpm == 0) ? 200 : un->un_pgeom.g_rpm; 5603 5604 un->un_vtoc.v_part[0].p_start = 0; 5605 un->un_vtoc.v_part[0].p_size = disksize; 5606 un->un_vtoc.v_part[0].p_tag = V_BACKUP; 5607 un->un_vtoc.v_part[0].p_flag = V_UNMNT; 5608 5609 un->un_map[0].dkl_cylno = 0; 5610 un->un_map[0].dkl_nblk = disksize; 5611 un->un_offset[0] = 0; 5612 5613 } else { 5614 /* 5615 * Hard disks and removable media cartridges 5616 */ 5617 un->un_g.dkg_rpm = 5618 (un->un_pgeom.g_rpm == 0) ? 3600: un->un_pgeom.g_rpm; 5619 un->un_vtoc.v_sectorsz = un->un_sys_blocksize; 5620 5621 /* Add boot slice */ 5622 un->un_vtoc.v_part[8].p_start = 0; 5623 un->un_vtoc.v_part[8].p_size = phys_spc; 5624 un->un_vtoc.v_part[8].p_tag = V_BOOT; 5625 un->un_vtoc.v_part[8].p_flag = V_UNMNT; 5626 5627 un->un_map[8].dkl_cylno = 0; 5628 un->un_map[8].dkl_nblk = phys_spc; 5629 un->un_offset[8] = 0; 5630 } 5631 5632 un->un_g.dkg_apc = 0; 5633 un->un_vtoc.v_nparts = V_NUMPAR; 5634 un->un_vtoc.v_version = V_VERSION; 5635 5636 /* Add backup slice */ 5637 un->un_vtoc.v_part[2].p_start = 0; 5638 un->un_vtoc.v_part[2].p_size = disksize; 5639 un->un_vtoc.v_part[2].p_tag = V_BACKUP; 5640 un->un_vtoc.v_part[2].p_flag = V_UNMNT; 5641 5642 un->un_map[2].dkl_cylno = 0; 5643 un->un_map[2].dkl_nblk = disksize; 5644 un->un_offset[2] = 0; 5645 5646 (void) sprintf(un->un_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d" 5647 " hd %d sec %d", un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 5648 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 5649 5650 #else 5651 #error "No VTOC format defined." 5652 #endif 5653 5654 un->un_g.dkg_read_reinstruct = 0; 5655 un->un_g.dkg_write_reinstruct = 0; 5656 5657 un->un_g.dkg_intrlv = 1; 5658 5659 un->un_vtoc.v_sanity = VTOC_SANE; 5660 5661 un->un_f_geometry_is_valid = TRUE; 5662 5663 SD_INFO(SD_LOG_COMMON, un, 5664 "sd_build_default_label: Default label created: " 5665 "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n", 5666 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, un->un_g.dkg_nhead, 5667 un->un_g.dkg_nsect, un->un_blockcount); 5668 } 5669 5670 5671 #if defined(_FIRMWARE_NEEDS_FDISK) 5672 /* 5673 * Max CHS values, as they are encoded into bytes, for 1022/254/63 5674 */ 5675 #define LBA_MAX_SECT (63 | ((1022 & 0x300) >> 2)) 5676 #define LBA_MAX_CYL (1022 & 0xFF) 5677 #define LBA_MAX_HEAD (254) 5678 5679 5680 /* 5681 * Function: sd_has_max_chs_vals 5682 * 5683 * Description: Return TRUE if Cylinder-Head-Sector values are all at maximum. 5684 * 5685 * Arguments: fdp - ptr to CHS info 5686 * 5687 * Return Code: True or false 5688 * 5689 * Context: Any. 5690 */ 5691 5692 static int 5693 sd_has_max_chs_vals(struct ipart *fdp) 5694 { 5695 return ((fdp->begcyl == LBA_MAX_CYL) && 5696 (fdp->beghead == LBA_MAX_HEAD) && 5697 (fdp->begsect == LBA_MAX_SECT) && 5698 (fdp->endcyl == LBA_MAX_CYL) && 5699 (fdp->endhead == LBA_MAX_HEAD) && 5700 (fdp->endsect == LBA_MAX_SECT)); 5701 } 5702 #endif 5703 5704 5705 /* 5706 * Function: sd_inq_fill 5707 * 5708 * Description: Print a piece of inquiry data, cleaned up for non-printable 5709 * characters and stopping at the first space character after 5710 * the beginning of the passed string; 5711 * 5712 * Arguments: p - source string 5713 * l - maximum length to copy 5714 * s - destination string 5715 * 5716 * Context: Any. 5717 */ 5718 5719 static void 5720 sd_inq_fill(char *p, int l, char *s) 5721 { 5722 unsigned i = 0; 5723 char c; 5724 5725 while (i++ < l) { 5726 if ((c = *p++) < ' ' || c >= 0x7F) { 5727 c = '*'; 5728 } else if (i != 1 && c == ' ') { 5729 break; 5730 } 5731 *s++ = c; 5732 } 5733 *s++ = 0; 5734 } 5735 5736 5737 /* 5738 * Function: sd_register_devid 5739 * 5740 * Description: This routine will obtain the device id information from the 5741 * target, obtain the serial number, and register the device 5742 * id with the ddi framework. 5743 * 5744 * Arguments: devi - the system's dev_info_t for the device. 5745 * un - driver soft state (unit) structure 5746 * reservation_flag - indicates if a reservation conflict 5747 * occurred during attach 5748 * 5749 * Context: Kernel Thread 5750 */ 5751 static void 5752 sd_register_devid(struct sd_lun *un, dev_info_t *devi, int reservation_flag) 5753 { 5754 int rval = 0; 5755 uchar_t *inq80 = NULL; 5756 size_t inq80_len = MAX_INQUIRY_SIZE; 5757 size_t inq80_resid = 0; 5758 uchar_t *inq83 = NULL; 5759 size_t inq83_len = MAX_INQUIRY_SIZE; 5760 size_t inq83_resid = 0; 5761 5762 ASSERT(un != NULL); 5763 ASSERT(mutex_owned(SD_MUTEX(un))); 5764 ASSERT((SD_DEVINFO(un)) == devi); 5765 5766 /* 5767 * This is the case of antiquated Sun disk drives that have the 5768 * FAB_DEVID property set in the disk_table. These drives 5769 * manage the devid's by storing them in last 2 available sectors 5770 * on the drive and have them fabricated by the ddi layer by calling 5771 * ddi_devid_init and passing the DEVID_FAB flag. 5772 */ 5773 if (un->un_f_opt_fab_devid == TRUE) { 5774 /* 5775 * Depending on EINVAL isn't reliable, since a reserved disk 5776 * may result in invalid geometry, so check to make sure a 5777 * reservation conflict did not occur during attach. 5778 */ 5779 if ((sd_get_devid(un) == EINVAL) && 5780 (reservation_flag != SD_TARGET_IS_RESERVED)) { 5781 /* 5782 * The devid is invalid AND there is no reservation 5783 * conflict. Fabricate a new devid. 5784 */ 5785 (void) sd_create_devid(un); 5786 } 5787 5788 /* Register the devid if it exists */ 5789 if (un->un_devid != NULL) { 5790 (void) ddi_devid_register(SD_DEVINFO(un), 5791 un->un_devid); 5792 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5793 "sd_register_devid: Devid Fabricated\n"); 5794 } 5795 return; 5796 } 5797 5798 /* 5799 * We check the availibility of the World Wide Name (0x83) and Unit 5800 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 5801 * un_vpd_page_mask from them, we decide which way to get the WWN. If 5802 * 0x83 is availible, that is the best choice. Our next choice is 5803 * 0x80. If neither are availible, we munge the devid from the device 5804 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 5805 * to fabricate a devid for non-Sun qualified disks. 5806 */ 5807 if (sd_check_vpd_page_support(un) == 0) { 5808 /* collect page 80 data if available */ 5809 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 5810 5811 mutex_exit(SD_MUTEX(un)); 5812 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 5813 rval = sd_send_scsi_INQUIRY(un, inq80, inq80_len, 5814 0x01, 0x80, &inq80_resid); 5815 5816 if (rval != 0) { 5817 kmem_free(inq80, inq80_len); 5818 inq80 = NULL; 5819 inq80_len = 0; 5820 } 5821 mutex_enter(SD_MUTEX(un)); 5822 } 5823 5824 /* collect page 83 data if available */ 5825 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 5826 5827 mutex_exit(SD_MUTEX(un)); 5828 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 5829 rval = sd_send_scsi_INQUIRY(un, inq83, inq83_len, 5830 0x01, 0x83, &inq83_resid); 5831 5832 if (rval != 0) { 5833 kmem_free(inq83, inq83_len); 5834 inq83 = NULL; 5835 inq83_len = 0; 5836 } 5837 mutex_enter(SD_MUTEX(un)); 5838 } 5839 } 5840 5841 /* encode best devid possible based on data available */ 5842 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 5843 (char *)ddi_driver_name(SD_DEVINFO(un)), 5844 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 5845 inq80, inq80_len - inq80_resid, inq83, inq83_len - 5846 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 5847 5848 /* devid successfully encoded, register devid */ 5849 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 5850 5851 } else { 5852 /* 5853 * Unable to encode a devid based on data available. 5854 * This is not a Sun qualified disk. Older Sun disk 5855 * drives that have the SD_FAB_DEVID property 5856 * set in the disk_table and non Sun qualified 5857 * disks are treated in the same manner. These 5858 * drives manage the devid's by storing them in 5859 * last 2 available sectors on the drive and 5860 * have them fabricated by the ddi layer by 5861 * calling ddi_devid_init and passing the 5862 * DEVID_FAB flag. 5863 * Create a fabricate devid only if there's no 5864 * fabricate devid existed. 5865 */ 5866 if (sd_get_devid(un) == EINVAL) { 5867 (void) sd_create_devid(un); 5868 un->un_f_opt_fab_devid = TRUE; 5869 } 5870 5871 /* Register the devid if it exists */ 5872 if (un->un_devid != NULL) { 5873 (void) ddi_devid_register(SD_DEVINFO(un), 5874 un->un_devid); 5875 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5876 "sd_register_devid: devid fabricated using " 5877 "ddi framework\n"); 5878 } 5879 } 5880 5881 /* clean up resources */ 5882 if (inq80 != NULL) { 5883 kmem_free(inq80, inq80_len); 5884 } 5885 if (inq83 != NULL) { 5886 kmem_free(inq83, inq83_len); 5887 } 5888 } 5889 5890 static daddr_t 5891 sd_get_devid_block(struct sd_lun *un) 5892 { 5893 daddr_t spc, blk, head, cyl; 5894 5895 if (un->un_blockcount <= DK_MAX_BLOCKS) { 5896 /* this geometry doesn't allow us to write a devid */ 5897 if (un->un_g.dkg_acyl < 2) { 5898 return (-1); 5899 } 5900 5901 /* 5902 * Subtract 2 guarantees that the next to last cylinder 5903 * is used 5904 */ 5905 cyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl - 2; 5906 spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 5907 head = un->un_g.dkg_nhead - 1; 5908 blk = (cyl * (spc - un->un_g.dkg_apc)) + 5909 (head * un->un_g.dkg_nsect) + 1; 5910 } else { 5911 if (un->un_reserved != -1) { 5912 blk = un->un_map[un->un_reserved].dkl_cylno + 1; 5913 } else { 5914 return (-1); 5915 } 5916 } 5917 return (blk); 5918 } 5919 5920 /* 5921 * Function: sd_get_devid 5922 * 5923 * Description: This routine will return 0 if a valid device id has been 5924 * obtained from the target and stored in the soft state. If a 5925 * valid device id has not been previously read and stored, a 5926 * read attempt will be made. 5927 * 5928 * Arguments: un - driver soft state (unit) structure 5929 * 5930 * Return Code: 0 if we successfully get the device id 5931 * 5932 * Context: Kernel Thread 5933 */ 5934 5935 static int 5936 sd_get_devid(struct sd_lun *un) 5937 { 5938 struct dk_devid *dkdevid; 5939 ddi_devid_t tmpid; 5940 uint_t *ip; 5941 size_t sz; 5942 daddr_t blk; 5943 int status; 5944 int chksum; 5945 int i; 5946 size_t buffer_size; 5947 5948 ASSERT(un != NULL); 5949 ASSERT(mutex_owned(SD_MUTEX(un))); 5950 5951 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 5952 un); 5953 5954 if (un->un_devid != NULL) { 5955 return (0); 5956 } 5957 5958 blk = sd_get_devid_block(un); 5959 if (blk < 0) 5960 return (EINVAL); 5961 5962 /* 5963 * Read and verify device id, stored in the reserved cylinders at the 5964 * end of the disk. Backup label is on the odd sectors of the last 5965 * track of the last cylinder. Device id will be on track of the next 5966 * to last cylinder. 5967 */ 5968 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 5969 mutex_exit(SD_MUTEX(un)); 5970 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 5971 status = sd_send_scsi_READ(un, dkdevid, buffer_size, blk, 5972 SD_PATH_DIRECT); 5973 if (status != 0) { 5974 goto error; 5975 } 5976 5977 /* Validate the revision */ 5978 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 5979 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 5980 status = EINVAL; 5981 goto error; 5982 } 5983 5984 /* Calculate the checksum */ 5985 chksum = 0; 5986 ip = (uint_t *)dkdevid; 5987 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 5988 i++) { 5989 chksum ^= ip[i]; 5990 } 5991 5992 /* Compare the checksums */ 5993 if (DKD_GETCHKSUM(dkdevid) != chksum) { 5994 status = EINVAL; 5995 goto error; 5996 } 5997 5998 /* Validate the device id */ 5999 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 6000 status = EINVAL; 6001 goto error; 6002 } 6003 6004 /* 6005 * Store the device id in the driver soft state 6006 */ 6007 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 6008 tmpid = kmem_alloc(sz, KM_SLEEP); 6009 6010 mutex_enter(SD_MUTEX(un)); 6011 6012 un->un_devid = tmpid; 6013 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 6014 6015 kmem_free(dkdevid, buffer_size); 6016 6017 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 6018 6019 return (status); 6020 error: 6021 mutex_enter(SD_MUTEX(un)); 6022 kmem_free(dkdevid, buffer_size); 6023 return (status); 6024 } 6025 6026 6027 /* 6028 * Function: sd_create_devid 6029 * 6030 * Description: This routine will fabricate the device id and write it 6031 * to the disk. 6032 * 6033 * Arguments: un - driver soft state (unit) structure 6034 * 6035 * Return Code: value of the fabricated device id 6036 * 6037 * Context: Kernel Thread 6038 */ 6039 6040 static ddi_devid_t 6041 sd_create_devid(struct sd_lun *un) 6042 { 6043 ASSERT(un != NULL); 6044 6045 /* Fabricate the devid */ 6046 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 6047 == DDI_FAILURE) { 6048 return (NULL); 6049 } 6050 6051 /* Write the devid to disk */ 6052 if (sd_write_deviceid(un) != 0) { 6053 ddi_devid_free(un->un_devid); 6054 un->un_devid = NULL; 6055 } 6056 6057 return (un->un_devid); 6058 } 6059 6060 6061 /* 6062 * Function: sd_write_deviceid 6063 * 6064 * Description: This routine will write the device id to the disk 6065 * reserved sector. 6066 * 6067 * Arguments: un - driver soft state (unit) structure 6068 * 6069 * Return Code: EINVAL 6070 * value returned by sd_send_scsi_cmd 6071 * 6072 * Context: Kernel Thread 6073 */ 6074 6075 static int 6076 sd_write_deviceid(struct sd_lun *un) 6077 { 6078 struct dk_devid *dkdevid; 6079 daddr_t blk; 6080 uint_t *ip, chksum; 6081 int status; 6082 int i; 6083 6084 ASSERT(mutex_owned(SD_MUTEX(un))); 6085 6086 blk = sd_get_devid_block(un); 6087 if (blk < 0) 6088 return (-1); 6089 mutex_exit(SD_MUTEX(un)); 6090 6091 /* Allocate the buffer */ 6092 dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 6093 6094 /* Fill in the revision */ 6095 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 6096 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 6097 6098 /* Copy in the device id */ 6099 mutex_enter(SD_MUTEX(un)); 6100 bcopy(un->un_devid, &dkdevid->dkd_devid, 6101 ddi_devid_sizeof(un->un_devid)); 6102 mutex_exit(SD_MUTEX(un)); 6103 6104 /* Calculate the checksum */ 6105 chksum = 0; 6106 ip = (uint_t *)dkdevid; 6107 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6108 i++) { 6109 chksum ^= ip[i]; 6110 } 6111 6112 /* Fill-in checksum */ 6113 DKD_FORMCHKSUM(chksum, dkdevid); 6114 6115 /* Write the reserved sector */ 6116 status = sd_send_scsi_WRITE(un, dkdevid, un->un_sys_blocksize, blk, 6117 SD_PATH_DIRECT); 6118 6119 kmem_free(dkdevid, un->un_sys_blocksize); 6120 6121 mutex_enter(SD_MUTEX(un)); 6122 return (status); 6123 } 6124 6125 6126 /* 6127 * Function: sd_check_vpd_page_support 6128 * 6129 * Description: This routine sends an inquiry command with the EVPD bit set and 6130 * a page code of 0x00 to the device. It is used to determine which 6131 * vital product pages are availible to find the devid. We are 6132 * looking for pages 0x83 or 0x80. If we return a negative 1, the 6133 * device does not support that command. 6134 * 6135 * Arguments: un - driver soft state (unit) structure 6136 * 6137 * Return Code: 0 - success 6138 * 1 - check condition 6139 * 6140 * Context: This routine can sleep. 6141 */ 6142 6143 static int 6144 sd_check_vpd_page_support(struct sd_lun *un) 6145 { 6146 uchar_t *page_list = NULL; 6147 uchar_t page_length = 0xff; /* Use max possible length */ 6148 uchar_t evpd = 0x01; /* Set the EVPD bit */ 6149 uchar_t page_code = 0x00; /* Supported VPD Pages */ 6150 int rval = 0; 6151 int counter; 6152 6153 ASSERT(un != NULL); 6154 ASSERT(mutex_owned(SD_MUTEX(un))); 6155 6156 mutex_exit(SD_MUTEX(un)); 6157 6158 /* 6159 * We'll set the page length to the maximum to save figuring it out 6160 * with an additional call. 6161 */ 6162 page_list = kmem_zalloc(page_length, KM_SLEEP); 6163 6164 rval = sd_send_scsi_INQUIRY(un, page_list, page_length, evpd, 6165 page_code, NULL); 6166 6167 mutex_enter(SD_MUTEX(un)); 6168 6169 /* 6170 * Now we must validate that the device accepted the command, as some 6171 * drives do not support it. If the drive does support it, we will 6172 * return 0, and the supported pages will be in un_vpd_page_mask. If 6173 * not, we return -1. 6174 */ 6175 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 6176 /* Loop to find one of the 2 pages we need */ 6177 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 6178 6179 /* 6180 * Pages are returned in ascending order, and 0x83 is what we 6181 * are hoping for. 6182 */ 6183 while ((page_list[counter] <= 0x83) && 6184 (counter <= (page_list[VPD_PAGE_LENGTH] + 6185 VPD_HEAD_OFFSET))) { 6186 /* 6187 * Add 3 because page_list[3] is the number of 6188 * pages minus 3 6189 */ 6190 6191 switch (page_list[counter]) { 6192 case 0x00: 6193 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 6194 break; 6195 case 0x80: 6196 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 6197 break; 6198 case 0x81: 6199 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 6200 break; 6201 case 0x82: 6202 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 6203 break; 6204 case 0x83: 6205 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 6206 break; 6207 } 6208 counter++; 6209 } 6210 6211 } else { 6212 rval = -1; 6213 6214 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6215 "sd_check_vpd_page_support: This drive does not implement " 6216 "VPD pages.\n"); 6217 } 6218 6219 kmem_free(page_list, page_length); 6220 6221 return (rval); 6222 } 6223 6224 6225 /* 6226 * Function: sd_setup_pm 6227 * 6228 * Description: Initialize Power Management on the device 6229 * 6230 * Context: Kernel Thread 6231 */ 6232 6233 static void 6234 sd_setup_pm(struct sd_lun *un, dev_info_t *devi) 6235 { 6236 uint_t log_page_size; 6237 uchar_t *log_page_data; 6238 int rval; 6239 6240 /* 6241 * Since we are called from attach, holding a mutex for 6242 * un is unnecessary. Because some of the routines called 6243 * from here require SD_MUTEX to not be held, assert this 6244 * right up front. 6245 */ 6246 ASSERT(!mutex_owned(SD_MUTEX(un))); 6247 /* 6248 * Since the sd device does not have the 'reg' property, 6249 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 6250 * The following code is to tell cpr that this device 6251 * DOES need to be suspended and resumed. 6252 */ 6253 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 6254 "pm-hardware-state", "needs-suspend-resume"); 6255 6256 /* 6257 * Check if HBA has set the "pm-capable" property. 6258 * If "pm-capable" exists and is non-zero then we can 6259 * power manage the device without checking the start/stop 6260 * cycle count log sense page. 6261 * 6262 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0) 6263 * then we should not power manage the device. 6264 * 6265 * If "pm-capable" doesn't exist then un->un_pm_capable_prop will 6266 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, sd will 6267 * check the start/stop cycle count log sense page and power manage 6268 * the device if the cycle count limit has not been exceeded. 6269 */ 6270 un->un_pm_capable_prop = 6271 ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 6272 "pm-capable", SD_PM_CAPABLE_UNDEFINED); 6273 if (un->un_pm_capable_prop != SD_PM_CAPABLE_UNDEFINED) { 6274 /* 6275 * pm-capable property exists. 6276 * 6277 * Convert "TRUE" values for un_pm_capable_prop to 6278 * SD_PM_CAPABLE_TRUE (1) to make it easier to check later. 6279 * "TRUE" values are any values except SD_PM_CAPABLE_FALSE (0) 6280 * and SD_PM_CAPABLE_UNDEFINED (-1) 6281 */ 6282 if (un->un_pm_capable_prop != SD_PM_CAPABLE_FALSE) { 6283 un->un_pm_capable_prop = SD_PM_CAPABLE_TRUE; 6284 } 6285 6286 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6287 "sd_unit_attach: un:0x%p pm-capable " 6288 "property set to %d.\n", un, un->un_pm_capable_prop); 6289 } 6290 6291 /* 6292 * This complies with the new power management framework 6293 * for certain desktop machines. Create the pm_components 6294 * property as a string array property. 6295 * 6296 * If this is a removable device or if the pm-capable property 6297 * is SD_PM_CAPABLE_TRUE (1) then we should create the 6298 * pm_components property without checking for the existance of 6299 * the start-stop cycle counter log page 6300 */ 6301 if (ISREMOVABLE(un) || 6302 un->un_pm_capable_prop == SD_PM_CAPABLE_TRUE) { 6303 /* 6304 * not all devices have a motor, try it first. 6305 * some devices may return ILLEGAL REQUEST, some 6306 * will hang 6307 */ 6308 un->un_f_start_stop_supported = TRUE; 6309 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 6310 SD_PATH_DIRECT) != 0) { 6311 un->un_f_start_stop_supported = FALSE; 6312 } 6313 6314 /* 6315 * create pm properties anyways otherwise the parent can't 6316 * go to sleep 6317 */ 6318 (void) sd_create_pm_components(devi, un); 6319 un->un_f_pm_is_enabled = TRUE; 6320 6321 /* 6322 * Need to create a zero length (Boolean) property 6323 * removable-media for the removable media devices. 6324 * Note that the return value of the property is not being 6325 * checked, since if unable to create the property 6326 * then do not want the attach to fail altogether. Consistent 6327 * with other property creation in attach. 6328 */ 6329 if (ISREMOVABLE(un)) { 6330 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 6331 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 6332 } 6333 return; 6334 } 6335 6336 rval = sd_log_page_supported(un, START_STOP_CYCLE_PAGE); 6337 6338 #ifdef SDDEBUG 6339 if (sd_force_pm_supported) { 6340 /* Force a successful result */ 6341 rval = 1; 6342 } 6343 #endif 6344 6345 /* 6346 * If the start-stop cycle counter log page is not supported 6347 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0) 6348 * then we should not create the pm_components property. 6349 */ 6350 if (rval == -1 || un->un_pm_capable_prop == SD_PM_CAPABLE_FALSE) { 6351 /* 6352 * Error. 6353 * Reading log sense failed, most likely this is 6354 * an older drive that does not support log sense. 6355 * If this fails auto-pm is not supported. 6356 */ 6357 un->un_power_level = SD_SPINDLE_ON; 6358 un->un_f_pm_is_enabled = FALSE; 6359 6360 } else if (rval == 0) { 6361 /* 6362 * Page not found. 6363 * The start stop cycle counter is implemented as page 6364 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 6365 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 6366 */ 6367 if (sd_log_page_supported(un, START_STOP_CYCLE_VU_PAGE) == 1) { 6368 /* 6369 * Page found, use this one. 6370 */ 6371 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 6372 un->un_f_pm_is_enabled = TRUE; 6373 } else { 6374 /* 6375 * Error or page not found. 6376 * auto-pm is not supported for this device. 6377 */ 6378 un->un_power_level = SD_SPINDLE_ON; 6379 un->un_f_pm_is_enabled = FALSE; 6380 } 6381 } else { 6382 /* 6383 * Page found, use it. 6384 */ 6385 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 6386 un->un_f_pm_is_enabled = TRUE; 6387 } 6388 6389 6390 if (un->un_f_pm_is_enabled == TRUE) { 6391 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6392 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6393 6394 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 6395 log_page_size, un->un_start_stop_cycle_page, 6396 0x01, 0, SD_PATH_DIRECT); 6397 #ifdef SDDEBUG 6398 if (sd_force_pm_supported) { 6399 /* Force a successful result */ 6400 rval = 0; 6401 } 6402 #endif 6403 6404 /* 6405 * If the Log sense for Page( Start/stop cycle counter page) 6406 * succeeds, then power managment is supported and we can 6407 * enable auto-pm. 6408 */ 6409 if (rval == 0) { 6410 (void) sd_create_pm_components(devi, un); 6411 } else { 6412 un->un_power_level = SD_SPINDLE_ON; 6413 un->un_f_pm_is_enabled = FALSE; 6414 } 6415 6416 kmem_free(log_page_data, log_page_size); 6417 } 6418 } 6419 6420 6421 /* 6422 * Function: sd_create_pm_components 6423 * 6424 * Description: Initialize PM property. 6425 * 6426 * Context: Kernel thread context 6427 */ 6428 6429 static void 6430 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 6431 { 6432 char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL }; 6433 6434 ASSERT(!mutex_owned(SD_MUTEX(un))); 6435 6436 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6437 "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) { 6438 /* 6439 * When components are initially created they are idle, 6440 * power up any non-removables. 6441 * Note: the return value of pm_raise_power can't be used 6442 * for determining if PM should be enabled for this device. 6443 * Even if you check the return values and remove this 6444 * property created above, the PM framework will not honor the 6445 * change after the first call to pm_raise_power. Hence, 6446 * removal of that property does not help if pm_raise_power 6447 * fails. In the case of removable media, the start/stop 6448 * will fail if the media is not present. 6449 */ 6450 if ((!ISREMOVABLE(un)) && (pm_raise_power(SD_DEVINFO(un), 0, 6451 SD_SPINDLE_ON) == DDI_SUCCESS)) { 6452 mutex_enter(SD_MUTEX(un)); 6453 un->un_power_level = SD_SPINDLE_ON; 6454 mutex_enter(&un->un_pm_mutex); 6455 /* Set to on and not busy. */ 6456 un->un_pm_count = 0; 6457 } else { 6458 mutex_enter(SD_MUTEX(un)); 6459 un->un_power_level = SD_SPINDLE_OFF; 6460 mutex_enter(&un->un_pm_mutex); 6461 /* Set to off. */ 6462 un->un_pm_count = -1; 6463 } 6464 mutex_exit(&un->un_pm_mutex); 6465 mutex_exit(SD_MUTEX(un)); 6466 } else { 6467 un->un_power_level = SD_SPINDLE_ON; 6468 un->un_f_pm_is_enabled = FALSE; 6469 } 6470 } 6471 6472 6473 /* 6474 * Function: sd_ddi_suspend 6475 * 6476 * Description: Performs system power-down operations. This includes 6477 * setting the drive state to indicate its suspended so 6478 * that no new commands will be accepted. Also, wait for 6479 * all commands that are in transport or queued to a timer 6480 * for retry to complete. All timeout threads are cancelled. 6481 * 6482 * Return Code: DDI_FAILURE or DDI_SUCCESS 6483 * 6484 * Context: Kernel thread context 6485 */ 6486 6487 static int 6488 sd_ddi_suspend(dev_info_t *devi) 6489 { 6490 struct sd_lun *un; 6491 clock_t wait_cmds_complete; 6492 6493 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6494 if (un == NULL) { 6495 return (DDI_FAILURE); 6496 } 6497 6498 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 6499 6500 mutex_enter(SD_MUTEX(un)); 6501 6502 /* Return success if the device is already suspended. */ 6503 if (un->un_state == SD_STATE_SUSPENDED) { 6504 mutex_exit(SD_MUTEX(un)); 6505 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6506 "device already suspended, exiting\n"); 6507 return (DDI_SUCCESS); 6508 } 6509 6510 /* Return failure if the device is being used by HA */ 6511 if (un->un_resvd_status & 6512 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 6513 mutex_exit(SD_MUTEX(un)); 6514 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6515 "device in use by HA, exiting\n"); 6516 return (DDI_FAILURE); 6517 } 6518 6519 /* 6520 * Return failure if the device is in a resource wait 6521 * or power changing state. 6522 */ 6523 if ((un->un_state == SD_STATE_RWAIT) || 6524 (un->un_state == SD_STATE_PM_CHANGING)) { 6525 mutex_exit(SD_MUTEX(un)); 6526 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6527 "device in resource wait state, exiting\n"); 6528 return (DDI_FAILURE); 6529 } 6530 6531 6532 un->un_save_state = un->un_last_state; 6533 New_state(un, SD_STATE_SUSPENDED); 6534 6535 /* 6536 * Wait for all commands that are in transport or queued to a timer 6537 * for retry to complete. 6538 * 6539 * While waiting, no new commands will be accepted or sent because of 6540 * the new state we set above. 6541 * 6542 * Wait till current operation has completed. If we are in the resource 6543 * wait state (with an intr outstanding) then we need to wait till the 6544 * intr completes and starts the next cmd. We want to wait for 6545 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 6546 */ 6547 wait_cmds_complete = ddi_get_lbolt() + 6548 (sd_wait_cmds_complete * drv_usectohz(1000000)); 6549 6550 while (un->un_ncmds_in_transport != 0) { 6551 /* 6552 * Fail if commands do not finish in the specified time. 6553 */ 6554 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 6555 wait_cmds_complete) == -1) { 6556 /* 6557 * Undo the state changes made above. Everything 6558 * must go back to it's original value. 6559 */ 6560 Restore_state(un); 6561 un->un_last_state = un->un_save_state; 6562 /* Wake up any threads that might be waiting. */ 6563 cv_broadcast(&un->un_suspend_cv); 6564 mutex_exit(SD_MUTEX(un)); 6565 SD_ERROR(SD_LOG_IO_PM, un, 6566 "sd_ddi_suspend: failed due to outstanding cmds\n"); 6567 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 6568 return (DDI_FAILURE); 6569 } 6570 } 6571 6572 /* 6573 * Cancel SCSI watch thread and timeouts, if any are active 6574 */ 6575 6576 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 6577 opaque_t temp_token = un->un_swr_token; 6578 mutex_exit(SD_MUTEX(un)); 6579 scsi_watch_suspend(temp_token); 6580 mutex_enter(SD_MUTEX(un)); 6581 } 6582 6583 if (un->un_reset_throttle_timeid != NULL) { 6584 timeout_id_t temp_id = un->un_reset_throttle_timeid; 6585 un->un_reset_throttle_timeid = NULL; 6586 mutex_exit(SD_MUTEX(un)); 6587 (void) untimeout(temp_id); 6588 mutex_enter(SD_MUTEX(un)); 6589 } 6590 6591 if (un->un_dcvb_timeid != NULL) { 6592 timeout_id_t temp_id = un->un_dcvb_timeid; 6593 un->un_dcvb_timeid = NULL; 6594 mutex_exit(SD_MUTEX(un)); 6595 (void) untimeout(temp_id); 6596 mutex_enter(SD_MUTEX(un)); 6597 } 6598 6599 mutex_enter(&un->un_pm_mutex); 6600 if (un->un_pm_timeid != NULL) { 6601 timeout_id_t temp_id = un->un_pm_timeid; 6602 un->un_pm_timeid = NULL; 6603 mutex_exit(&un->un_pm_mutex); 6604 mutex_exit(SD_MUTEX(un)); 6605 (void) untimeout(temp_id); 6606 mutex_enter(SD_MUTEX(un)); 6607 } else { 6608 mutex_exit(&un->un_pm_mutex); 6609 } 6610 6611 if (un->un_retry_timeid != NULL) { 6612 timeout_id_t temp_id = un->un_retry_timeid; 6613 un->un_retry_timeid = NULL; 6614 mutex_exit(SD_MUTEX(un)); 6615 (void) untimeout(temp_id); 6616 mutex_enter(SD_MUTEX(un)); 6617 } 6618 6619 if (un->un_direct_priority_timeid != NULL) { 6620 timeout_id_t temp_id = un->un_direct_priority_timeid; 6621 un->un_direct_priority_timeid = NULL; 6622 mutex_exit(SD_MUTEX(un)); 6623 (void) untimeout(temp_id); 6624 mutex_enter(SD_MUTEX(un)); 6625 } 6626 6627 if (un->un_f_is_fibre == TRUE) { 6628 /* 6629 * Remove callbacks for insert and remove events 6630 */ 6631 if (un->un_insert_event != NULL) { 6632 mutex_exit(SD_MUTEX(un)); 6633 (void) ddi_remove_event_handler(un->un_insert_cb_id); 6634 mutex_enter(SD_MUTEX(un)); 6635 un->un_insert_event = NULL; 6636 } 6637 6638 if (un->un_remove_event != NULL) { 6639 mutex_exit(SD_MUTEX(un)); 6640 (void) ddi_remove_event_handler(un->un_remove_cb_id); 6641 mutex_enter(SD_MUTEX(un)); 6642 un->un_remove_event = NULL; 6643 } 6644 } 6645 6646 mutex_exit(SD_MUTEX(un)); 6647 6648 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 6649 6650 return (DDI_SUCCESS); 6651 } 6652 6653 6654 /* 6655 * Function: sd_ddi_pm_suspend 6656 * 6657 * Description: Set the drive state to low power. 6658 * Someone else is required to actually change the drive 6659 * power level. 6660 * 6661 * Arguments: un - driver soft state (unit) structure 6662 * 6663 * Return Code: DDI_FAILURE or DDI_SUCCESS 6664 * 6665 * Context: Kernel thread context 6666 */ 6667 6668 static int 6669 sd_ddi_pm_suspend(struct sd_lun *un) 6670 { 6671 ASSERT(un != NULL); 6672 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n"); 6673 6674 ASSERT(!mutex_owned(SD_MUTEX(un))); 6675 mutex_enter(SD_MUTEX(un)); 6676 6677 /* 6678 * Exit if power management is not enabled for this device, or if 6679 * the device is being used by HA. 6680 */ 6681 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 6682 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 6683 mutex_exit(SD_MUTEX(un)); 6684 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n"); 6685 return (DDI_SUCCESS); 6686 } 6687 6688 SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n", 6689 un->un_ncmds_in_driver); 6690 6691 /* 6692 * See if the device is not busy, ie.: 6693 * - we have no commands in the driver for this device 6694 * - not waiting for resources 6695 */ 6696 if ((un->un_ncmds_in_driver == 0) && 6697 (un->un_state != SD_STATE_RWAIT)) { 6698 /* 6699 * The device is not busy, so it is OK to go to low power state. 6700 * Indicate low power, but rely on someone else to actually 6701 * change it. 6702 */ 6703 mutex_enter(&un->un_pm_mutex); 6704 un->un_pm_count = -1; 6705 mutex_exit(&un->un_pm_mutex); 6706 un->un_power_level = SD_SPINDLE_OFF; 6707 } 6708 6709 mutex_exit(SD_MUTEX(un)); 6710 6711 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n"); 6712 6713 return (DDI_SUCCESS); 6714 } 6715 6716 6717 /* 6718 * Function: sd_ddi_resume 6719 * 6720 * Description: Performs system power-up operations.. 6721 * 6722 * Return Code: DDI_SUCCESS 6723 * DDI_FAILURE 6724 * 6725 * Context: Kernel thread context 6726 */ 6727 6728 static int 6729 sd_ddi_resume(dev_info_t *devi) 6730 { 6731 struct sd_lun *un; 6732 6733 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6734 if (un == NULL) { 6735 return (DDI_FAILURE); 6736 } 6737 6738 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 6739 6740 mutex_enter(SD_MUTEX(un)); 6741 Restore_state(un); 6742 6743 /* 6744 * Restore the state which was saved to give the 6745 * the right state in un_last_state 6746 */ 6747 un->un_last_state = un->un_save_state; 6748 /* 6749 * Note: throttle comes back at full. 6750 * Also note: this MUST be done before calling pm_raise_power 6751 * otherwise the system can get hung in biowait. The scenario where 6752 * this'll happen is under cpr suspend. Writing of the system 6753 * state goes through sddump, which writes 0 to un_throttle. If 6754 * writing the system state then fails, example if the partition is 6755 * too small, then cpr attempts a resume. If throttle isn't restored 6756 * from the saved value until after calling pm_raise_power then 6757 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 6758 * in biowait. 6759 */ 6760 un->un_throttle = un->un_saved_throttle; 6761 6762 /* 6763 * The chance of failure is very rare as the only command done in power 6764 * entry point is START command when you transition from 0->1 or 6765 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 6766 * which suspend was done. Ignore the return value as the resume should 6767 * not be failed. In the case of removable media the media need not be 6768 * inserted and hence there is a chance that raise power will fail with 6769 * media not present. 6770 */ 6771 if (!ISREMOVABLE(un)) { 6772 mutex_exit(SD_MUTEX(un)); 6773 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 6774 mutex_enter(SD_MUTEX(un)); 6775 } 6776 6777 /* 6778 * Don't broadcast to the suspend cv and therefore possibly 6779 * start I/O until after power has been restored. 6780 */ 6781 cv_broadcast(&un->un_suspend_cv); 6782 cv_broadcast(&un->un_state_cv); 6783 6784 /* restart thread */ 6785 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 6786 scsi_watch_resume(un->un_swr_token); 6787 } 6788 6789 #if (defined(__fibre)) 6790 if (un->un_f_is_fibre == TRUE) { 6791 /* 6792 * Add callbacks for insert and remove events 6793 */ 6794 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 6795 sd_init_event_callbacks(un); 6796 } 6797 } 6798 #endif 6799 6800 /* 6801 * Transport any pending commands to the target. 6802 * 6803 * If this is a low-activity device commands in queue will have to wait 6804 * until new commands come in, which may take awhile. Also, we 6805 * specifically don't check un_ncmds_in_transport because we know that 6806 * there really are no commands in progress after the unit was 6807 * suspended and we could have reached the throttle level, been 6808 * suspended, and have no new commands coming in for awhile. Highly 6809 * unlikely, but so is the low-activity disk scenario. 6810 */ 6811 ddi_xbuf_dispatch(un->un_xbuf_attr); 6812 6813 sd_start_cmds(un, NULL); 6814 mutex_exit(SD_MUTEX(un)); 6815 6816 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 6817 6818 return (DDI_SUCCESS); 6819 } 6820 6821 6822 /* 6823 * Function: sd_ddi_pm_resume 6824 * 6825 * Description: Set the drive state to powered on. 6826 * Someone else is required to actually change the drive 6827 * power level. 6828 * 6829 * Arguments: un - driver soft state (unit) structure 6830 * 6831 * Return Code: DDI_SUCCESS 6832 * 6833 * Context: Kernel thread context 6834 */ 6835 6836 static int 6837 sd_ddi_pm_resume(struct sd_lun *un) 6838 { 6839 ASSERT(un != NULL); 6840 6841 ASSERT(!mutex_owned(SD_MUTEX(un))); 6842 mutex_enter(SD_MUTEX(un)); 6843 un->un_power_level = SD_SPINDLE_ON; 6844 6845 ASSERT(!mutex_owned(&un->un_pm_mutex)); 6846 mutex_enter(&un->un_pm_mutex); 6847 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 6848 un->un_pm_count++; 6849 ASSERT(un->un_pm_count == 0); 6850 /* 6851 * Note: no longer do the cv_broadcast on un_suspend_cv. The 6852 * un_suspend_cv is for a system resume, not a power management 6853 * device resume. (4297749) 6854 * cv_broadcast(&un->un_suspend_cv); 6855 */ 6856 } 6857 mutex_exit(&un->un_pm_mutex); 6858 mutex_exit(SD_MUTEX(un)); 6859 6860 return (DDI_SUCCESS); 6861 } 6862 6863 6864 /* 6865 * Function: sd_pm_idletimeout_handler 6866 * 6867 * Description: A timer routine that's active only while a device is busy. 6868 * The purpose is to extend slightly the pm framework's busy 6869 * view of the device to prevent busy/idle thrashing for 6870 * back-to-back commands. Do this by comparing the current time 6871 * to the time at which the last command completed and when the 6872 * difference is greater than sd_pm_idletime, call 6873 * pm_idle_component. In addition to indicating idle to the pm 6874 * framework, update the chain type to again use the internal pm 6875 * layers of the driver. 6876 * 6877 * Arguments: arg - driver soft state (unit) structure 6878 * 6879 * Context: Executes in a timeout(9F) thread context 6880 */ 6881 6882 static void 6883 sd_pm_idletimeout_handler(void *arg) 6884 { 6885 struct sd_lun *un = arg; 6886 6887 time_t now; 6888 6889 mutex_enter(&sd_detach_mutex); 6890 if (un->un_detach_count != 0) { 6891 /* Abort if the instance is detaching */ 6892 mutex_exit(&sd_detach_mutex); 6893 return; 6894 } 6895 mutex_exit(&sd_detach_mutex); 6896 6897 now = ddi_get_time(); 6898 /* 6899 * Grab both mutexes, in the proper order, since we're accessing 6900 * both PM and softstate variables. 6901 */ 6902 mutex_enter(SD_MUTEX(un)); 6903 mutex_enter(&un->un_pm_mutex); 6904 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 6905 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 6906 /* 6907 * Update the chain types. 6908 * This takes affect on the next new command received. 6909 */ 6910 if (ISREMOVABLE(un)) { 6911 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 6912 } else { 6913 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 6914 } 6915 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 6916 6917 SD_TRACE(SD_LOG_IO_PM, un, 6918 "sd_pm_idletimeout_handler: idling device\n"); 6919 (void) pm_idle_component(SD_DEVINFO(un), 0); 6920 un->un_pm_idle_timeid = NULL; 6921 } else { 6922 un->un_pm_idle_timeid = 6923 timeout(sd_pm_idletimeout_handler, un, 6924 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 6925 } 6926 mutex_exit(&un->un_pm_mutex); 6927 mutex_exit(SD_MUTEX(un)); 6928 } 6929 6930 6931 /* 6932 * Function: sd_pm_timeout_handler 6933 * 6934 * Description: Callback to tell framework we are idle. 6935 * 6936 * Context: timeout(9f) thread context. 6937 */ 6938 6939 static void 6940 sd_pm_timeout_handler(void *arg) 6941 { 6942 struct sd_lun *un = arg; 6943 6944 (void) pm_idle_component(SD_DEVINFO(un), 0); 6945 mutex_enter(&un->un_pm_mutex); 6946 un->un_pm_timeid = NULL; 6947 mutex_exit(&un->un_pm_mutex); 6948 } 6949 6950 6951 /* 6952 * Function: sdpower 6953 * 6954 * Description: PM entry point. 6955 * 6956 * Return Code: DDI_SUCCESS 6957 * DDI_FAILURE 6958 * 6959 * Context: Kernel thread context 6960 */ 6961 6962 static int 6963 sdpower(dev_info_t *devi, int component, int level) 6964 { 6965 struct sd_lun *un; 6966 int instance; 6967 int rval = DDI_SUCCESS; 6968 uint_t i, log_page_size, maxcycles, ncycles; 6969 uchar_t *log_page_data; 6970 int log_sense_page; 6971 int medium_present; 6972 time_t intvlp; 6973 dev_t dev; 6974 struct pm_trans_data sd_pm_tran_data; 6975 uchar_t save_state; 6976 int sval; 6977 uchar_t state_before_pm; 6978 int got_semaphore_here; 6979 6980 instance = ddi_get_instance(devi); 6981 6982 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 6983 (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) || 6984 component != 0) { 6985 return (DDI_FAILURE); 6986 } 6987 6988 dev = sd_make_device(SD_DEVINFO(un)); 6989 6990 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 6991 6992 /* 6993 * Must synchronize power down with close. 6994 * Attempt to decrement/acquire the open/close semaphore, 6995 * but do NOT wait on it. If it's not greater than zero, 6996 * ie. it can't be decremented without waiting, then 6997 * someone else, either open or close, already has it 6998 * and the try returns 0. Use that knowledge here to determine 6999 * if it's OK to change the device power level. 7000 * Also, only increment it on exit if it was decremented, ie. gotten, 7001 * here. 7002 */ 7003 got_semaphore_here = sema_tryp(&un->un_semoclose); 7004 7005 mutex_enter(SD_MUTEX(un)); 7006 7007 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 7008 un->un_ncmds_in_driver); 7009 7010 /* 7011 * If un_ncmds_in_driver is non-zero it indicates commands are 7012 * already being processed in the driver, or if the semaphore was 7013 * not gotten here it indicates an open or close is being processed. 7014 * At the same time somebody is requesting to go low power which 7015 * can't happen, therefore we need to return failure. 7016 */ 7017 if ((level == SD_SPINDLE_OFF) && 7018 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 7019 mutex_exit(SD_MUTEX(un)); 7020 7021 if (got_semaphore_here != 0) { 7022 sema_v(&un->un_semoclose); 7023 } 7024 SD_TRACE(SD_LOG_IO_PM, un, 7025 "sdpower: exit, device has queued cmds.\n"); 7026 return (DDI_FAILURE); 7027 } 7028 7029 /* 7030 * if it is OFFLINE that means the disk is completely dead 7031 * in our case we have to put the disk in on or off by sending commands 7032 * Of course that will fail anyway so return back here. 7033 * 7034 * Power changes to a device that's OFFLINE or SUSPENDED 7035 * are not allowed. 7036 */ 7037 if ((un->un_state == SD_STATE_OFFLINE) || 7038 (un->un_state == SD_STATE_SUSPENDED)) { 7039 mutex_exit(SD_MUTEX(un)); 7040 7041 if (got_semaphore_here != 0) { 7042 sema_v(&un->un_semoclose); 7043 } 7044 SD_TRACE(SD_LOG_IO_PM, un, 7045 "sdpower: exit, device is off-line.\n"); 7046 return (DDI_FAILURE); 7047 } 7048 7049 /* 7050 * Change the device's state to indicate it's power level 7051 * is being changed. Do this to prevent a power off in the 7052 * middle of commands, which is especially bad on devices 7053 * that are really powered off instead of just spun down. 7054 */ 7055 state_before_pm = un->un_state; 7056 un->un_state = SD_STATE_PM_CHANGING; 7057 7058 mutex_exit(SD_MUTEX(un)); 7059 7060 /* 7061 * Bypass checking the log sense information for removables 7062 * and devices for which the HBA set the pm-capable property. 7063 * If un->un_pm_capable_prop is SD_PM_CAPABLE_UNDEFINED (-1) 7064 * then the HBA did not create the property. 7065 */ 7066 if ((level == SD_SPINDLE_OFF) && (!ISREMOVABLE(un)) && 7067 un->un_pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) { 7068 /* 7069 * Get the log sense information to understand whether the 7070 * the powercycle counts have gone beyond the threshhold. 7071 */ 7072 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 7073 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 7074 7075 mutex_enter(SD_MUTEX(un)); 7076 log_sense_page = un->un_start_stop_cycle_page; 7077 mutex_exit(SD_MUTEX(un)); 7078 7079 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 7080 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 7081 #ifdef SDDEBUG 7082 if (sd_force_pm_supported) { 7083 /* Force a successful result */ 7084 rval = 0; 7085 } 7086 #endif 7087 if (rval != 0) { 7088 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 7089 "Log Sense Failed\n"); 7090 kmem_free(log_page_data, log_page_size); 7091 /* Cannot support power management on those drives */ 7092 7093 if (got_semaphore_here != 0) { 7094 sema_v(&un->un_semoclose); 7095 } 7096 /* 7097 * On exit put the state back to it's original value 7098 * and broadcast to anyone waiting for the power 7099 * change completion. 7100 */ 7101 mutex_enter(SD_MUTEX(un)); 7102 un->un_state = state_before_pm; 7103 cv_broadcast(&un->un_suspend_cv); 7104 mutex_exit(SD_MUTEX(un)); 7105 SD_TRACE(SD_LOG_IO_PM, un, 7106 "sdpower: exit, Log Sense Failed.\n"); 7107 return (DDI_FAILURE); 7108 } 7109 7110 /* 7111 * From the page data - Convert the essential information to 7112 * pm_trans_data 7113 */ 7114 maxcycles = 7115 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 7116 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 7117 7118 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 7119 7120 ncycles = 7121 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 7122 (log_page_data[0x26] << 8) | log_page_data[0x27]; 7123 7124 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 7125 7126 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 7127 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 7128 log_page_data[8+i]; 7129 } 7130 7131 kmem_free(log_page_data, log_page_size); 7132 7133 /* 7134 * Call pm_trans_check routine to get the Ok from 7135 * the global policy 7136 */ 7137 7138 sd_pm_tran_data.format = DC_SCSI_FORMAT; 7139 sd_pm_tran_data.un.scsi_cycles.flag = 0; 7140 7141 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 7142 #ifdef SDDEBUG 7143 if (sd_force_pm_supported) { 7144 /* Force a successful result */ 7145 rval = 1; 7146 } 7147 #endif 7148 switch (rval) { 7149 case 0: 7150 /* 7151 * Not Ok to Power cycle or error in parameters passed 7152 * Would have given the advised time to consider power 7153 * cycle. Based on the new intvlp parameter we are 7154 * supposed to pretend we are busy so that pm framework 7155 * will never call our power entry point. Because of 7156 * that install a timeout handler and wait for the 7157 * recommended time to elapse so that power management 7158 * can be effective again. 7159 * 7160 * To effect this behavior, call pm_busy_component to 7161 * indicate to the framework this device is busy. 7162 * By not adjusting un_pm_count the rest of PM in 7163 * the driver will function normally, and independant 7164 * of this but because the framework is told the device 7165 * is busy it won't attempt powering down until it gets 7166 * a matching idle. The timeout handler sends this. 7167 * Note: sd_pm_entry can't be called here to do this 7168 * because sdpower may have been called as a result 7169 * of a call to pm_raise_power from within sd_pm_entry. 7170 * 7171 * If a timeout handler is already active then 7172 * don't install another. 7173 */ 7174 mutex_enter(&un->un_pm_mutex); 7175 if (un->un_pm_timeid == NULL) { 7176 un->un_pm_timeid = 7177 timeout(sd_pm_timeout_handler, 7178 un, intvlp * drv_usectohz(1000000)); 7179 mutex_exit(&un->un_pm_mutex); 7180 (void) pm_busy_component(SD_DEVINFO(un), 0); 7181 } else { 7182 mutex_exit(&un->un_pm_mutex); 7183 } 7184 if (got_semaphore_here != 0) { 7185 sema_v(&un->un_semoclose); 7186 } 7187 /* 7188 * On exit put the state back to it's original value 7189 * and broadcast to anyone waiting for the power 7190 * change completion. 7191 */ 7192 mutex_enter(SD_MUTEX(un)); 7193 un->un_state = state_before_pm; 7194 cv_broadcast(&un->un_suspend_cv); 7195 mutex_exit(SD_MUTEX(un)); 7196 7197 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 7198 "trans check Failed, not ok to power cycle.\n"); 7199 return (DDI_FAILURE); 7200 7201 case -1: 7202 if (got_semaphore_here != 0) { 7203 sema_v(&un->un_semoclose); 7204 } 7205 /* 7206 * On exit put the state back to it's original value 7207 * and broadcast to anyone waiting for the power 7208 * change completion. 7209 */ 7210 mutex_enter(SD_MUTEX(un)); 7211 un->un_state = state_before_pm; 7212 cv_broadcast(&un->un_suspend_cv); 7213 mutex_exit(SD_MUTEX(un)); 7214 SD_TRACE(SD_LOG_IO_PM, un, 7215 "sdpower: exit, trans check command Failed.\n"); 7216 return (DDI_FAILURE); 7217 } 7218 } 7219 7220 if (level == SD_SPINDLE_OFF) { 7221 /* 7222 * Save the last state... if the STOP FAILS we need it 7223 * for restoring 7224 */ 7225 mutex_enter(SD_MUTEX(un)); 7226 save_state = un->un_last_state; 7227 /* 7228 * There must not be any cmds. getting processed 7229 * in the driver when we get here. Power to the 7230 * device is potentially going off. 7231 */ 7232 ASSERT(un->un_ncmds_in_driver == 0); 7233 mutex_exit(SD_MUTEX(un)); 7234 7235 /* 7236 * For now suspend the device completely before spindle is 7237 * turned off 7238 */ 7239 if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) { 7240 if (got_semaphore_here != 0) { 7241 sema_v(&un->un_semoclose); 7242 } 7243 /* 7244 * On exit put the state back to it's original value 7245 * and broadcast to anyone waiting for the power 7246 * change completion. 7247 */ 7248 mutex_enter(SD_MUTEX(un)); 7249 un->un_state = state_before_pm; 7250 cv_broadcast(&un->un_suspend_cv); 7251 mutex_exit(SD_MUTEX(un)); 7252 SD_TRACE(SD_LOG_IO_PM, un, 7253 "sdpower: exit, PM suspend Failed.\n"); 7254 return (DDI_FAILURE); 7255 } 7256 } 7257 7258 /* 7259 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 7260 * close, or strategy. Dump no long uses this routine, it uses it's 7261 * own code so it can be done in polled mode. 7262 */ 7263 7264 medium_present = TRUE; 7265 7266 /* 7267 * When powering up, issue a TUR in case the device is at unit 7268 * attention. Don't do retries. Bypass the PM layer, otherwise 7269 * a deadlock on un_pm_busy_cv will occur. 7270 */ 7271 if (level == SD_SPINDLE_ON) { 7272 (void) sd_send_scsi_TEST_UNIT_READY(un, 7273 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 7274 } 7275 7276 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 7277 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 7278 7279 sval = sd_send_scsi_START_STOP_UNIT(un, 7280 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP), 7281 SD_PATH_DIRECT); 7282 /* Command failed, check for media present. */ 7283 if ((sval == ENXIO) && ISREMOVABLE(un)) { 7284 medium_present = FALSE; 7285 } 7286 7287 /* 7288 * The conditions of interest here are: 7289 * if a spindle off with media present fails, 7290 * then restore the state and return an error. 7291 * else if a spindle on fails, 7292 * then return an error (there's no state to restore). 7293 * In all other cases we setup for the new state 7294 * and return success. 7295 */ 7296 switch (level) { 7297 case SD_SPINDLE_OFF: 7298 if ((medium_present == TRUE) && (sval != 0)) { 7299 /* The stop command from above failed */ 7300 rval = DDI_FAILURE; 7301 /* 7302 * The stop command failed, and we have media 7303 * present. Put the level back by calling the 7304 * sd_pm_resume() and set the state back to 7305 * it's previous value. 7306 */ 7307 (void) sd_ddi_pm_resume(un); 7308 mutex_enter(SD_MUTEX(un)); 7309 un->un_last_state = save_state; 7310 mutex_exit(SD_MUTEX(un)); 7311 break; 7312 } 7313 /* 7314 * The stop command from above succeeded. 7315 */ 7316 if (ISREMOVABLE(un)) { 7317 /* 7318 * Terminate watch thread in case of removable media 7319 * devices going into low power state. This is as per 7320 * the requirements of pm framework, otherwise commands 7321 * will be generated for the device (through watch 7322 * thread), even when the device is in low power state. 7323 */ 7324 mutex_enter(SD_MUTEX(un)); 7325 un->un_f_watcht_stopped = FALSE; 7326 if (un->un_swr_token != NULL) { 7327 opaque_t temp_token = un->un_swr_token; 7328 un->un_f_watcht_stopped = TRUE; 7329 un->un_swr_token = NULL; 7330 mutex_exit(SD_MUTEX(un)); 7331 (void) scsi_watch_request_terminate(temp_token, 7332 SCSI_WATCH_TERMINATE_WAIT); 7333 } else { 7334 mutex_exit(SD_MUTEX(un)); 7335 } 7336 } 7337 break; 7338 7339 default: /* The level requested is spindle on... */ 7340 /* 7341 * Legacy behavior: return success on a failed spinup 7342 * if there is no media in the drive. 7343 * Do this by looking at medium_present here. 7344 */ 7345 if ((sval != 0) && medium_present) { 7346 /* The start command from above failed */ 7347 rval = DDI_FAILURE; 7348 break; 7349 } 7350 /* 7351 * The start command from above succeeded 7352 * Resume the devices now that we have 7353 * started the disks 7354 */ 7355 (void) sd_ddi_pm_resume(un); 7356 7357 /* 7358 * Resume the watch thread since it was suspended 7359 * when the device went into low power mode. 7360 */ 7361 if (ISREMOVABLE(un)) { 7362 mutex_enter(SD_MUTEX(un)); 7363 if (un->un_f_watcht_stopped == TRUE) { 7364 opaque_t temp_token; 7365 7366 un->un_f_watcht_stopped = FALSE; 7367 mutex_exit(SD_MUTEX(un)); 7368 temp_token = scsi_watch_request_submit( 7369 SD_SCSI_DEVP(un), 7370 sd_check_media_time, 7371 SENSE_LENGTH, sd_media_watch_cb, 7372 (caddr_t)dev); 7373 mutex_enter(SD_MUTEX(un)); 7374 un->un_swr_token = temp_token; 7375 } 7376 mutex_exit(SD_MUTEX(un)); 7377 } 7378 } 7379 if (got_semaphore_here != 0) { 7380 sema_v(&un->un_semoclose); 7381 } 7382 /* 7383 * On exit put the state back to it's original value 7384 * and broadcast to anyone waiting for the power 7385 * change completion. 7386 */ 7387 mutex_enter(SD_MUTEX(un)); 7388 un->un_state = state_before_pm; 7389 cv_broadcast(&un->un_suspend_cv); 7390 mutex_exit(SD_MUTEX(un)); 7391 7392 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 7393 7394 return (rval); 7395 } 7396 7397 7398 7399 /* 7400 * Function: sdattach 7401 * 7402 * Description: Driver's attach(9e) entry point function. 7403 * 7404 * Arguments: devi - opaque device info handle 7405 * cmd - attach type 7406 * 7407 * Return Code: DDI_SUCCESS 7408 * DDI_FAILURE 7409 * 7410 * Context: Kernel thread context 7411 */ 7412 7413 static int 7414 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 7415 { 7416 switch (cmd) { 7417 case DDI_ATTACH: 7418 return (sd_unit_attach(devi)); 7419 case DDI_RESUME: 7420 return (sd_ddi_resume(devi)); 7421 default: 7422 break; 7423 } 7424 return (DDI_FAILURE); 7425 } 7426 7427 7428 /* 7429 * Function: sddetach 7430 * 7431 * Description: Driver's detach(9E) entry point function. 7432 * 7433 * Arguments: devi - opaque device info handle 7434 * cmd - detach type 7435 * 7436 * Return Code: DDI_SUCCESS 7437 * DDI_FAILURE 7438 * 7439 * Context: Kernel thread context 7440 */ 7441 7442 static int 7443 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 7444 { 7445 switch (cmd) { 7446 case DDI_DETACH: 7447 return (sd_unit_detach(devi)); 7448 case DDI_SUSPEND: 7449 return (sd_ddi_suspend(devi)); 7450 default: 7451 break; 7452 } 7453 return (DDI_FAILURE); 7454 } 7455 7456 7457 /* 7458 * Function: sd_sync_with_callback 7459 * 7460 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 7461 * state while the callback routine is active. 7462 * 7463 * Arguments: un: softstate structure for the instance 7464 * 7465 * Context: Kernel thread context 7466 */ 7467 7468 static void 7469 sd_sync_with_callback(struct sd_lun *un) 7470 { 7471 ASSERT(un != NULL); 7472 7473 mutex_enter(SD_MUTEX(un)); 7474 7475 ASSERT(un->un_in_callback >= 0); 7476 7477 while (un->un_in_callback > 0) { 7478 mutex_exit(SD_MUTEX(un)); 7479 delay(2); 7480 mutex_enter(SD_MUTEX(un)); 7481 } 7482 7483 mutex_exit(SD_MUTEX(un)); 7484 } 7485 7486 /* 7487 * Function: sd_unit_attach 7488 * 7489 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 7490 * the soft state structure for the device and performs 7491 * all necessary structure and device initializations. 7492 * 7493 * Arguments: devi: the system's dev_info_t for the device. 7494 * 7495 * Return Code: DDI_SUCCESS if attach is successful. 7496 * DDI_FAILURE if any part of the attach fails. 7497 * 7498 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 7499 * Kernel thread context only. Can sleep. 7500 */ 7501 7502 static int 7503 sd_unit_attach(dev_info_t *devi) 7504 { 7505 struct scsi_device *devp; 7506 struct sd_lun *un; 7507 char *variantp; 7508 int reservation_flag = SD_TARGET_IS_UNRESERVED; 7509 int instance; 7510 int rval; 7511 uint64_t capacity; 7512 uint_t lbasize; 7513 7514 /* 7515 * Retrieve the target driver's private data area. This was set 7516 * up by the HBA. 7517 */ 7518 devp = ddi_get_driver_private(devi); 7519 7520 /* 7521 * Since we have no idea what state things were left in by the last 7522 * user of the device, set up some 'default' settings, ie. turn 'em 7523 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 7524 * Do this before the scsi_probe, which sends an inquiry. 7525 * This is a fix for bug (4430280). 7526 * Of special importance is wide-xfer. The drive could have been left 7527 * in wide transfer mode by the last driver to communicate with it, 7528 * this includes us. If that's the case, and if the following is not 7529 * setup properly or we don't re-negotiate with the drive prior to 7530 * transferring data to/from the drive, it causes bus parity errors, 7531 * data overruns, and unexpected interrupts. This first occurred when 7532 * the fix for bug (4378686) was made. 7533 */ 7534 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 7535 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 7536 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 7537 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 7538 7539 /* 7540 * Use scsi_probe() to issue an INQUIRY command to the device. 7541 * This call will allocate and fill in the scsi_inquiry structure 7542 * and point the sd_inq member of the scsi_device structure to it. 7543 * If the attach succeeds, then this memory will not be de-allocated 7544 * (via scsi_unprobe()) until the instance is detached. 7545 */ 7546 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 7547 goto probe_failed; 7548 } 7549 7550 /* 7551 * Check the device type as specified in the inquiry data and 7552 * claim it if it is of a type that we support. 7553 */ 7554 switch (devp->sd_inq->inq_dtype) { 7555 case DTYPE_DIRECT: 7556 break; 7557 case DTYPE_RODIRECT: 7558 break; 7559 case DTYPE_OPTICAL: 7560 break; 7561 case DTYPE_NOTPRESENT: 7562 default: 7563 /* Unsupported device type; fail the attach. */ 7564 goto probe_failed; 7565 } 7566 7567 /* 7568 * Allocate the soft state structure for this unit. 7569 * 7570 * We rely upon this memory being set to all zeroes by 7571 * ddi_soft_state_zalloc(). We assume that any member of the 7572 * soft state structure that is not explicitly initialized by 7573 * this routine will have a value of zero. 7574 */ 7575 instance = ddi_get_instance(devp->sd_dev); 7576 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 7577 goto probe_failed; 7578 } 7579 7580 /* 7581 * Retrieve a pointer to the newly-allocated soft state. 7582 * 7583 * This should NEVER fail if the ddi_soft_state_zalloc() call above 7584 * was successful, unless something has gone horribly wrong and the 7585 * ddi's soft state internals are corrupt (in which case it is 7586 * probably better to halt here than just fail the attach....) 7587 */ 7588 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 7589 panic("sd_unit_attach: NULL soft state on instance:0x%x", 7590 instance); 7591 /*NOTREACHED*/ 7592 } 7593 7594 /* 7595 * Link the back ptr of the driver soft state to the scsi_device 7596 * struct for this lun. 7597 * Save a pointer to the softstate in the driver-private area of 7598 * the scsi_device struct. 7599 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 7600 * we first set un->un_sd below. 7601 */ 7602 un->un_sd = devp; 7603 devp->sd_private = (opaque_t)un; 7604 7605 /* 7606 * The following must be after devp is stored in the soft state struct. 7607 */ 7608 #ifdef SDDEBUG 7609 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7610 "%s_unit_attach: un:0x%p instance:%d\n", 7611 ddi_driver_name(devi), un, instance); 7612 #endif 7613 7614 /* 7615 * Set up the device type and node type (for the minor nodes). 7616 * By default we assume that the device can at least support the 7617 * Common Command Set. Call it a CD-ROM if it reports itself 7618 * as a RODIRECT device. 7619 */ 7620 switch (devp->sd_inq->inq_dtype) { 7621 case DTYPE_RODIRECT: 7622 un->un_node_type = DDI_NT_CD_CHAN; 7623 un->un_ctype = CTYPE_CDROM; 7624 break; 7625 case DTYPE_OPTICAL: 7626 un->un_node_type = DDI_NT_BLOCK_CHAN; 7627 un->un_ctype = CTYPE_ROD; 7628 break; 7629 default: 7630 un->un_node_type = DDI_NT_BLOCK_CHAN; 7631 un->un_ctype = CTYPE_CCS; 7632 break; 7633 } 7634 7635 /* 7636 * Try to read the interconnect type from the HBA. 7637 * 7638 * Note: This driver is currently compiled as two binaries, a parallel 7639 * scsi version (sd) and a fibre channel version (ssd). All functional 7640 * differences are determined at compile time. In the future a single 7641 * binary will be provided and the inteconnect type will be used to 7642 * differentiate between fibre and parallel scsi behaviors. At that time 7643 * it will be necessary for all fibre channel HBAs to support this 7644 * property. 7645 * 7646 * set un_f_is_fiber to TRUE ( default fiber ) 7647 */ 7648 un->un_f_is_fibre = TRUE; 7649 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 7650 case INTERCONNECT_SSA: 7651 un->un_interconnect_type = SD_INTERCONNECT_SSA; 7652 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7653 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 7654 break; 7655 case INTERCONNECT_PARALLEL: 7656 un->un_f_is_fibre = FALSE; 7657 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7658 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7659 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 7660 break; 7661 case INTERCONNECT_FIBRE: 7662 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 7663 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7664 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 7665 break; 7666 case INTERCONNECT_FABRIC: 7667 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 7668 un->un_node_type = DDI_NT_BLOCK_FABRIC; 7669 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7670 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 7671 break; 7672 default: 7673 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 7674 /* 7675 * The HBA does not support the "interconnect-type" property 7676 * (or did not provide a recognized type). 7677 * 7678 * Note: This will be obsoleted when a single fibre channel 7679 * and parallel scsi driver is delivered. In the meantime the 7680 * interconnect type will be set to the platform default.If that 7681 * type is not parallel SCSI, it means that we should be 7682 * assuming "ssd" semantics. However, here this also means that 7683 * the FC HBA is not supporting the "interconnect-type" property 7684 * like we expect it to, so log this occurrence. 7685 */ 7686 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 7687 if (!SD_IS_PARALLEL_SCSI(un)) { 7688 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7689 "sd_unit_attach: un:0x%p Assuming " 7690 "INTERCONNECT_FIBRE\n", un); 7691 } else { 7692 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7693 "sd_unit_attach: un:0x%p Assuming " 7694 "INTERCONNECT_PARALLEL\n", un); 7695 un->un_f_is_fibre = FALSE; 7696 } 7697 #else 7698 /* 7699 * Note: This source will be implemented when a single fibre 7700 * channel and parallel scsi driver is delivered. The default 7701 * will be to assume that if a device does not support the 7702 * "interconnect-type" property it is a parallel SCSI HBA and 7703 * we will set the interconnect type for parallel scsi. 7704 */ 7705 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7706 un->un_f_is_fibre = FALSE; 7707 #endif 7708 break; 7709 } 7710 7711 if (un->un_f_is_fibre == TRUE) { 7712 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 7713 SCSI_VERSION_3) { 7714 switch (un->un_interconnect_type) { 7715 case SD_INTERCONNECT_FIBRE: 7716 case SD_INTERCONNECT_SSA: 7717 un->un_node_type = DDI_NT_BLOCK_WWN; 7718 break; 7719 default: 7720 break; 7721 } 7722 } 7723 } 7724 7725 /* 7726 * Initialize the Request Sense command for the target 7727 */ 7728 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 7729 goto alloc_rqs_failed; 7730 } 7731 7732 /* 7733 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 7734 * with seperate binary for sd and ssd. 7735 * 7736 * x86 has 1 binary, un_retry_count is set base on connection type. 7737 * The hardcoded values will go away when Sparc uses 1 binary 7738 * for sd and ssd. This hardcoded values need to match 7739 * SD_RETRY_COUNT in sddef.h 7740 * The value used is base on interconnect type. 7741 * fibre = 3, parallel = 5 7742 */ 7743 #if defined(__i386) || defined(__amd64) 7744 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 7745 #else 7746 un->un_retry_count = SD_RETRY_COUNT; 7747 #endif 7748 7749 /* 7750 * Set the per disk retry count to the default number of retries 7751 * for disks and CDROMs. This value can be overridden by the 7752 * disk property list or an entry in sd.conf. 7753 */ 7754 un->un_notready_retry_count = 7755 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 7756 : DISK_NOT_READY_RETRY_COUNT(un); 7757 7758 /* 7759 * Set the busy retry count to the default value of un_retry_count. 7760 * This can be overridden by entries in sd.conf or the device 7761 * config table. 7762 */ 7763 un->un_busy_retry_count = un->un_retry_count; 7764 7765 /* 7766 * Init the reset threshold for retries. This number determines 7767 * how many retries must be performed before a reset can be issued 7768 * (for certain error conditions). This can be overridden by entries 7769 * in sd.conf or the device config table. 7770 */ 7771 un->un_reset_retry_count = (un->un_retry_count / 2); 7772 7773 /* 7774 * Set the victim_retry_count to the default un_retry_count 7775 */ 7776 un->un_victim_retry_count = (2 * un->un_retry_count); 7777 7778 /* 7779 * Set the reservation release timeout to the default value of 7780 * 5 seconds. This can be overridden by entries in ssd.conf or the 7781 * device config table. 7782 */ 7783 un->un_reserve_release_time = 5; 7784 7785 /* 7786 * Set up the default maximum transfer size. Note that this may 7787 * get updated later in the attach, when setting up default wide 7788 * operations for disks. 7789 */ 7790 #if defined(__i386) || defined(__amd64) 7791 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 7792 #else 7793 un->un_max_xfer_size = (uint_t)maxphys; 7794 #endif 7795 7796 /* 7797 * Get "allow bus device reset" property (defaults to "enabled" if 7798 * the property was not defined). This is to disable bus resets for 7799 * certain kinds of error recovery. Note: In the future when a run-time 7800 * fibre check is available the soft state flag should default to 7801 * enabled. 7802 */ 7803 if (un->un_f_is_fibre == TRUE) { 7804 un->un_f_allow_bus_device_reset = TRUE; 7805 } else { 7806 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 7807 "allow-bus-device-reset", 1) != 0) { 7808 un->un_f_allow_bus_device_reset = TRUE; 7809 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7810 "sd_unit_attach: un:0x%p Bus device reset enabled\n", 7811 un); 7812 } else { 7813 un->un_f_allow_bus_device_reset = FALSE; 7814 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7815 "sd_unit_attach: un:0x%p Bus device reset disabled\n", 7816 un); 7817 } 7818 } 7819 7820 /* 7821 * Check if this is an ATAPI device. ATAPI devices use Group 1 7822 * Read/Write commands and Group 2 Mode Sense/Select commands. 7823 * 7824 * Note: The "obsolete" way of doing this is to check for the "atapi" 7825 * property. The new "variant" property with a value of "atapi" has been 7826 * introduced so that future 'variants' of standard SCSI behavior (like 7827 * atapi) could be specified by the underlying HBA drivers by supplying 7828 * a new value for the "variant" property, instead of having to define a 7829 * new property. 7830 */ 7831 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 7832 un->un_f_cfg_is_atapi = TRUE; 7833 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7834 "sd_unit_attach: un:0x%p Atapi device\n", un); 7835 } 7836 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 7837 &variantp) == DDI_PROP_SUCCESS) { 7838 if (strcmp(variantp, "atapi") == 0) { 7839 un->un_f_cfg_is_atapi = TRUE; 7840 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7841 "sd_unit_attach: un:0x%p Atapi device\n", un); 7842 } 7843 ddi_prop_free(variantp); 7844 } 7845 7846 /* 7847 * Assume doorlock commands are supported. If not, the first 7848 * call to sd_send_scsi_DOORLOCK() will set to FALSE 7849 */ 7850 un->un_f_doorlock_supported = TRUE; 7851 7852 un->un_cmd_timeout = SD_IO_TIME; 7853 7854 /* Info on current states, statuses, etc. (Updated frequently) */ 7855 un->un_state = SD_STATE_NORMAL; 7856 un->un_last_state = SD_STATE_NORMAL; 7857 7858 /* Control & status info for command throttling */ 7859 un->un_throttle = sd_max_throttle; 7860 un->un_saved_throttle = sd_max_throttle; 7861 un->un_min_throttle = sd_min_throttle; 7862 7863 if (un->un_f_is_fibre == TRUE) { 7864 un->un_f_use_adaptive_throttle = TRUE; 7865 } else { 7866 un->un_f_use_adaptive_throttle = FALSE; 7867 } 7868 7869 /* Removable media support. */ 7870 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 7871 un->un_mediastate = DKIO_NONE; 7872 un->un_specified_mediastate = DKIO_NONE; 7873 7874 /* CVs for suspend/resume (PM or DR) */ 7875 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 7876 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 7877 7878 /* Power management support. */ 7879 un->un_power_level = SD_SPINDLE_UNINIT; 7880 7881 /* 7882 * The open/close semaphore is used to serialize threads executing 7883 * in the driver's open & close entry point routines for a given 7884 * instance. 7885 */ 7886 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 7887 7888 /* 7889 * The conf file entry and softstate variable is a forceful override, 7890 * meaning a non-zero value must be entered to change the default. 7891 */ 7892 un->un_f_disksort_disabled = FALSE; 7893 7894 /* 7895 * Retrieve the properties from the static driver table or the driver 7896 * configuration file (.conf) for this unit and update the soft state 7897 * for the device as needed for the indicated properties. 7898 * Note: the property configuration needs to occur here as some of the 7899 * following routines may have dependancies on soft state flags set 7900 * as part of the driver property configuration. 7901 */ 7902 sd_read_unit_properties(un); 7903 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7904 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 7905 7906 /* 7907 * By default, we mark the capacity, lbazize, and geometry 7908 * as invalid. Only if we successfully read a valid capacity 7909 * will we update the un_blockcount and un_tgt_blocksize with the 7910 * valid values (the geometry will be validated later). 7911 */ 7912 un->un_f_blockcount_is_valid = FALSE; 7913 un->un_f_tgt_blocksize_is_valid = FALSE; 7914 un->un_f_geometry_is_valid = FALSE; 7915 7916 /* 7917 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 7918 * otherwise. 7919 */ 7920 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 7921 un->un_blockcount = 0; 7922 7923 /* 7924 * Set up the per-instance info needed to determine the correct 7925 * CDBs and other info for issuing commands to the target. 7926 */ 7927 sd_init_cdb_limits(un); 7928 7929 /* 7930 * Set up the IO chains to use, based upon the target type. 7931 */ 7932 if (ISREMOVABLE(un)) { 7933 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 7934 } else { 7935 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 7936 } 7937 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 7938 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 7939 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 7940 7941 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 7942 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 7943 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 7944 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 7945 7946 7947 if (ISCD(un)) { 7948 un->un_additional_codes = sd_additional_codes; 7949 } else { 7950 un->un_additional_codes = NULL; 7951 } 7952 7953 /* 7954 * Create the kstats here so they can be available for attach-time 7955 * routines that send commands to the unit (either polled or via 7956 * sd_send_scsi_cmd). 7957 * 7958 * Note: This is a critical sequence that needs to be maintained: 7959 * 1) Instantiate the kstats here, before any routines using the 7960 * iopath (i.e. sd_send_scsi_cmd). 7961 * 2) Initialize the error stats (sd_set_errstats) and partition 7962 * stats (sd_set_pstats), following sd_validate_geometry(), 7963 * sd_register_devid(), and sd_disable_caching(). 7964 */ 7965 7966 un->un_stats = kstat_create(sd_label, instance, 7967 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 7968 if (un->un_stats != NULL) { 7969 un->un_stats->ks_lock = SD_MUTEX(un); 7970 kstat_install(un->un_stats); 7971 } 7972 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7973 "sd_unit_attach: un:0x%p un_stats created\n", un); 7974 7975 sd_create_errstats(un, instance); 7976 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7977 "sd_unit_attach: un:0x%p errstats created\n", un); 7978 7979 /* 7980 * The following if/else code was relocated here from below as part 7981 * of the fix for bug (4430280). However with the default setup added 7982 * on entry to this routine, it's no longer absolutely necessary for 7983 * this to be before the call to sd_spin_up_unit. 7984 */ 7985 if (SD_IS_PARALLEL_SCSI(un)) { 7986 /* 7987 * If SCSI-2 tagged queueing is supported by the target 7988 * and by the host adapter then we will enable it. 7989 */ 7990 un->un_tagflags = 0; 7991 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 7992 (devp->sd_inq->inq_cmdque) && 7993 (un->un_f_arq_enabled == TRUE)) { 7994 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 7995 1, 1) == 1) { 7996 un->un_tagflags = FLAG_STAG; 7997 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7998 "sd_unit_attach: un:0x%p tag queueing " 7999 "enabled\n", un); 8000 } else if (scsi_ifgetcap(SD_ADDRESS(un), 8001 "untagged-qing", 0) == 1) { 8002 un->un_f_opt_queueing = TRUE; 8003 un->un_saved_throttle = un->un_throttle = 8004 min(un->un_throttle, 3); 8005 } else { 8006 un->un_f_opt_queueing = FALSE; 8007 un->un_saved_throttle = un->un_throttle = 1; 8008 } 8009 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 8010 == 1) && (un->un_f_arq_enabled == TRUE)) { 8011 /* The Host Adapter supports internal queueing. */ 8012 un->un_f_opt_queueing = TRUE; 8013 un->un_saved_throttle = un->un_throttle = 8014 min(un->un_throttle, 3); 8015 } else { 8016 un->un_f_opt_queueing = FALSE; 8017 un->un_saved_throttle = un->un_throttle = 1; 8018 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8019 "sd_unit_attach: un:0x%p no tag queueing\n", un); 8020 } 8021 8022 8023 /* Setup or tear down default wide operations for disks */ 8024 8025 /* 8026 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 8027 * and "ssd_max_xfer_size" to exist simultaneously on the same 8028 * system and be set to different values. In the future this 8029 * code may need to be updated when the ssd module is 8030 * obsoleted and removed from the system. (4299588) 8031 */ 8032 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 8033 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 8034 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8035 1, 1) == 1) { 8036 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8037 "sd_unit_attach: un:0x%p Wide Transfer " 8038 "enabled\n", un); 8039 } 8040 8041 /* 8042 * If tagged queuing has also been enabled, then 8043 * enable large xfers 8044 */ 8045 if (un->un_saved_throttle == sd_max_throttle) { 8046 un->un_max_xfer_size = 8047 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8048 sd_max_xfer_size, SD_MAX_XFER_SIZE); 8049 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8050 "sd_unit_attach: un:0x%p max transfer " 8051 "size=0x%x\n", un, un->un_max_xfer_size); 8052 } 8053 } else { 8054 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8055 0, 1) == 1) { 8056 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8057 "sd_unit_attach: un:0x%p " 8058 "Wide Transfer disabled\n", un); 8059 } 8060 } 8061 } else { 8062 un->un_tagflags = FLAG_STAG; 8063 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 8064 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 8065 } 8066 8067 /* 8068 * If this target supports LUN reset, try to enable it. 8069 */ 8070 if (un->un_f_lun_reset_enabled) { 8071 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 8072 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8073 "un:0x%p lun_reset capability set\n", un); 8074 } else { 8075 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8076 "un:0x%p lun-reset capability not set\n", un); 8077 } 8078 } 8079 8080 /* 8081 * At this point in the attach, we have enough info in the 8082 * soft state to be able to issue commands to the target. 8083 * 8084 * All command paths used below MUST issue their commands as 8085 * SD_PATH_DIRECT. This is important as intermediate layers 8086 * are not all initialized yet (such as PM). 8087 */ 8088 8089 /* 8090 * Send a TEST UNIT READY command to the device. This should clear 8091 * any outstanding UNIT ATTENTION that may be present. 8092 * 8093 * Note: Don't check for success, just track if there is a reservation, 8094 * this is a throw away command to clear any unit attentions. 8095 * 8096 * Note: This MUST be the first command issued to the target during 8097 * attach to ensure power on UNIT ATTENTIONS are cleared. 8098 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 8099 * with attempts at spinning up a device with no media. 8100 */ 8101 if (sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR) == EACCES) { 8102 reservation_flag = SD_TARGET_IS_RESERVED; 8103 } 8104 8105 /* 8106 * If the device is NOT a removable media device, attempt to spin 8107 * it up (using the START_STOP_UNIT command) and read its capacity 8108 * (using the READ CAPACITY command). Note, however, that either 8109 * of these could fail and in some cases we would continue with 8110 * the attach despite the failure (see below). 8111 */ 8112 if (devp->sd_inq->inq_dtype == DTYPE_DIRECT && !ISREMOVABLE(un)) { 8113 switch (sd_spin_up_unit(un)) { 8114 case 0: 8115 /* 8116 * Spin-up was successful; now try to read the 8117 * capacity. If successful then save the results 8118 * and mark the capacity & lbasize as valid. 8119 */ 8120 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8121 "sd_unit_attach: un:0x%p spin-up successful\n", un); 8122 8123 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, 8124 &lbasize, SD_PATH_DIRECT)) { 8125 case 0: { 8126 if (capacity > DK_MAX_BLOCKS) { 8127 #ifdef _LP64 8128 /* 8129 * Enable descriptor format sense data 8130 * so that we can get 64 bit sense 8131 * data fields. 8132 */ 8133 sd_enable_descr_sense(un); 8134 #else 8135 /* 32-bit kernels can't handle this */ 8136 scsi_log(SD_DEVINFO(un), 8137 sd_label, CE_WARN, 8138 "disk has %llu blocks, which " 8139 "is too large for a 32-bit " 8140 "kernel", capacity); 8141 goto spinup_failed; 8142 #endif 8143 } 8144 /* 8145 * The following relies on 8146 * sd_send_scsi_READ_CAPACITY never 8147 * returning 0 for capacity and/or lbasize. 8148 */ 8149 sd_update_block_info(un, lbasize, capacity); 8150 8151 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8152 "sd_unit_attach: un:0x%p capacity = %ld " 8153 "blocks; lbasize= %ld.\n", un, 8154 un->un_blockcount, un->un_tgt_blocksize); 8155 8156 break; 8157 } 8158 case EACCES: 8159 /* 8160 * Should never get here if the spin-up 8161 * succeeded, but code it in anyway. 8162 * From here, just continue with the attach... 8163 */ 8164 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8165 "sd_unit_attach: un:0x%p " 8166 "sd_send_scsi_READ_CAPACITY " 8167 "returned reservation conflict\n", un); 8168 reservation_flag = SD_TARGET_IS_RESERVED; 8169 break; 8170 default: 8171 /* 8172 * Likewise, should never get here if the 8173 * spin-up succeeded. Just continue with 8174 * the attach... 8175 */ 8176 break; 8177 } 8178 break; 8179 case EACCES: 8180 /* 8181 * Device is reserved by another host. In this case 8182 * we could not spin it up or read the capacity, but 8183 * we continue with the attach anyway. 8184 */ 8185 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8186 "sd_unit_attach: un:0x%p spin-up reservation " 8187 "conflict.\n", un); 8188 reservation_flag = SD_TARGET_IS_RESERVED; 8189 break; 8190 default: 8191 /* Fail the attach if the spin-up failed. */ 8192 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8193 "sd_unit_attach: un:0x%p spin-up failed.", un); 8194 goto spinup_failed; 8195 } 8196 } 8197 8198 /* 8199 * Check to see if this is a MMC drive 8200 */ 8201 if (ISCD(un)) { 8202 sd_set_mmc_caps(un); 8203 } 8204 8205 /* 8206 * Create the minor nodes for the device. 8207 * Note: If we want to support fdisk on both sparc and intel, this will 8208 * have to separate out the notion that VTOC8 is always sparc, and 8209 * VTOC16 is always intel (tho these can be the defaults). The vtoc 8210 * type will have to be determined at run-time, and the fdisk 8211 * partitioning will have to have been read & set up before we 8212 * create the minor nodes. (any other inits (such as kstats) that 8213 * also ought to be done before creating the minor nodes?) (Doesn't 8214 * setting up the minor nodes kind of imply that we're ready to 8215 * handle an open from userland?) 8216 */ 8217 if (sd_create_minor_nodes(un, devi) != DDI_SUCCESS) { 8218 goto create_minor_nodes_failed; 8219 } 8220 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8221 "sd_unit_attach: un:0x%p minor nodes created\n", un); 8222 8223 /* 8224 * Add a zero-length attribute to tell the world we support 8225 * kernel ioctls (for layered drivers) 8226 */ 8227 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8228 DDI_KERNEL_IOCTL, NULL, 0); 8229 8230 /* 8231 * Add a boolean property to tell the world we support 8232 * the B_FAILFAST flag (for layered drivers) 8233 */ 8234 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8235 "ddi-failfast-supported", NULL, 0); 8236 8237 /* 8238 * Initialize power management 8239 */ 8240 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 8241 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 8242 sd_setup_pm(un, devi); 8243 if (un->un_f_pm_is_enabled == FALSE) { 8244 /* 8245 * For performance, point to a jump table that does 8246 * not include pm. 8247 * The direct and priority chains don't change with PM. 8248 * 8249 * Note: this is currently done based on individual device 8250 * capabilities. When an interface for determining system 8251 * power enabled state becomes available, or when additional 8252 * layers are added to the command chain, these values will 8253 * have to be re-evaluated for correctness. 8254 */ 8255 if (ISREMOVABLE(un)) { 8256 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 8257 } else { 8258 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 8259 } 8260 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8261 } 8262 8263 /* 8264 * This property is set to 0 by HA software to avoid retries 8265 * on a reserved disk. (The preferred property name is 8266 * "retry-on-reservation-conflict") (1189689) 8267 * 8268 * Note: The use of a global here can have unintended consequences. A 8269 * per instance variable is preferrable to match the capabilities of 8270 * different underlying hba's (4402600) 8271 */ 8272 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 8273 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 8274 sd_retry_on_reservation_conflict); 8275 if (sd_retry_on_reservation_conflict != 0) { 8276 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 8277 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 8278 sd_retry_on_reservation_conflict); 8279 } 8280 8281 /* Set up options for QFULL handling. */ 8282 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8283 "qfull-retries", -1)) != -1) { 8284 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 8285 rval, 1); 8286 } 8287 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8288 "qfull-retry-interval", -1)) != -1) { 8289 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 8290 rval, 1); 8291 } 8292 8293 /* 8294 * This just prints a message that announces the existence of the 8295 * device. The message is always printed in the system logfile, but 8296 * only appears on the console if the system is booted with the 8297 * -v (verbose) argument. 8298 */ 8299 ddi_report_dev(devi); 8300 8301 /* 8302 * The framework calls driver attach routines single-threaded 8303 * for a given instance. However we still acquire SD_MUTEX here 8304 * because this required for calling the sd_validate_geometry() 8305 * and sd_register_devid() functions. 8306 */ 8307 mutex_enter(SD_MUTEX(un)); 8308 un->un_f_geometry_is_valid = FALSE; 8309 un->un_mediastate = DKIO_NONE; 8310 un->un_reserved = -1; 8311 if (!ISREMOVABLE(un)) { 8312 /* 8313 * Read and validate the device's geometry (ie, disk label) 8314 * A new unformatted drive will not have a valid geometry, but 8315 * the driver needs to successfully attach to this device so 8316 * the drive can be formatted via ioctls. 8317 */ 8318 if (((sd_validate_geometry(un, SD_PATH_DIRECT) == 8319 ENOTSUP)) && 8320 (un->un_blockcount < DK_MAX_BLOCKS)) { 8321 /* 8322 * We found a small disk with an EFI label on it; 8323 * we need to fix up the minor nodes accordingly. 8324 */ 8325 ddi_remove_minor_node(devi, "h"); 8326 ddi_remove_minor_node(devi, "h,raw"); 8327 (void) ddi_create_minor_node(devi, "wd", 8328 S_IFBLK, 8329 (instance << SDUNIT_SHIFT) | WD_NODE, 8330 un->un_node_type, NULL); 8331 (void) ddi_create_minor_node(devi, "wd,raw", 8332 S_IFCHR, 8333 (instance << SDUNIT_SHIFT) | WD_NODE, 8334 un->un_node_type, NULL); 8335 } 8336 } 8337 8338 /* 8339 * Read and initialize the devid for the unit. 8340 */ 8341 ASSERT(un->un_errstats != NULL); 8342 if (!ISREMOVABLE(un)) { 8343 sd_register_devid(un, devi, reservation_flag); 8344 } 8345 mutex_exit(SD_MUTEX(un)); 8346 8347 #if (defined(__fibre)) 8348 /* 8349 * Register callbacks for fibre only. You can't do this soley 8350 * on the basis of the devid_type because this is hba specific. 8351 * We need to query our hba capabilities to find out whether to 8352 * register or not. 8353 */ 8354 if (un->un_f_is_fibre) { 8355 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 8356 sd_init_event_callbacks(un); 8357 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8358 "sd_unit_attach: un:0x%p event callbacks inserted", un); 8359 } 8360 } 8361 #endif 8362 8363 if (un->un_f_opt_disable_cache == TRUE) { 8364 if (sd_disable_caching(un) != 0) { 8365 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8366 "sd_unit_attach: un:0x%p Could not disable " 8367 "caching", un); 8368 goto devid_failed; 8369 } 8370 } 8371 8372 /* 8373 * Set the pstat and error stat values here, so data obtained during the 8374 * previous attach-time routines is available. 8375 * 8376 * Note: This is a critical sequence that needs to be maintained: 8377 * 1) Instantiate the kstats before any routines using the iopath 8378 * (i.e. sd_send_scsi_cmd). 8379 * 2) Initialize the error stats (sd_set_errstats) and partition 8380 * stats (sd_set_pstats)here, following sd_validate_geometry(), 8381 * sd_register_devid(), and sd_disable_caching(). 8382 */ 8383 if (!ISREMOVABLE(un) && (un->un_f_pkstats_enabled == TRUE)) { 8384 sd_set_pstats(un); 8385 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8386 "sd_unit_attach: un:0x%p pstats created and set\n", un); 8387 } 8388 8389 sd_set_errstats(un); 8390 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8391 "sd_unit_attach: un:0x%p errstats set\n", un); 8392 8393 /* 8394 * Find out what type of reservation this disk supports. 8395 */ 8396 switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) { 8397 case 0: 8398 /* 8399 * SCSI-3 reservations are supported. 8400 */ 8401 un->un_reservation_type = SD_SCSI3_RESERVATION; 8402 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8403 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 8404 break; 8405 case ENOTSUP: 8406 /* 8407 * The PERSISTENT RESERVE IN command would not be recognized by 8408 * a SCSI-2 device, so assume the reservation type is SCSI-2. 8409 */ 8410 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8411 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 8412 un->un_reservation_type = SD_SCSI2_RESERVATION; 8413 break; 8414 default: 8415 /* 8416 * default to SCSI-3 reservations 8417 */ 8418 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8419 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 8420 un->un_reservation_type = SD_SCSI3_RESERVATION; 8421 break; 8422 } 8423 8424 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8425 "sd_unit_attach: un:0x%p exit success\n", un); 8426 8427 return (DDI_SUCCESS); 8428 8429 /* 8430 * An error occurred during the attach; clean up & return failure. 8431 */ 8432 8433 devid_failed: 8434 8435 setup_pm_failed: 8436 ddi_remove_minor_node(devi, NULL); 8437 8438 create_minor_nodes_failed: 8439 /* 8440 * Cleanup from the scsi_ifsetcap() calls (437868) 8441 */ 8442 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8443 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8444 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8445 8446 if (un->un_f_is_fibre == FALSE) { 8447 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8448 } 8449 8450 spinup_failed: 8451 8452 mutex_enter(SD_MUTEX(un)); 8453 8454 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 8455 if (un->un_direct_priority_timeid != NULL) { 8456 timeout_id_t temp_id = un->un_direct_priority_timeid; 8457 un->un_direct_priority_timeid = NULL; 8458 mutex_exit(SD_MUTEX(un)); 8459 (void) untimeout(temp_id); 8460 mutex_enter(SD_MUTEX(un)); 8461 } 8462 8463 /* Cancel any pending start/stop timeouts */ 8464 if (un->un_startstop_timeid != NULL) { 8465 timeout_id_t temp_id = un->un_startstop_timeid; 8466 un->un_startstop_timeid = NULL; 8467 mutex_exit(SD_MUTEX(un)); 8468 (void) untimeout(temp_id); 8469 mutex_enter(SD_MUTEX(un)); 8470 } 8471 8472 mutex_exit(SD_MUTEX(un)); 8473 8474 /* There should not be any in-progress I/O so ASSERT this check */ 8475 ASSERT(un->un_ncmds_in_transport == 0); 8476 ASSERT(un->un_ncmds_in_driver == 0); 8477 8478 /* Do not free the softstate if the callback routine is active */ 8479 sd_sync_with_callback(un); 8480 8481 /* 8482 * Partition stats apparently are not used with removables. These would 8483 * not have been created during attach, so no need to clean them up... 8484 */ 8485 if (un->un_stats != NULL) { 8486 kstat_delete(un->un_stats); 8487 un->un_stats = NULL; 8488 } 8489 if (un->un_errstats != NULL) { 8490 kstat_delete(un->un_errstats); 8491 un->un_errstats = NULL; 8492 } 8493 8494 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 8495 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 8496 8497 ddi_prop_remove_all(devi); 8498 sema_destroy(&un->un_semoclose); 8499 cv_destroy(&un->un_state_cv); 8500 8501 getrbuf_failed: 8502 8503 sd_free_rqs(un); 8504 8505 alloc_rqs_failed: 8506 8507 devp->sd_private = NULL; 8508 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 8509 8510 get_softstate_failed: 8511 /* 8512 * Note: the man pages are unclear as to whether or not doing a 8513 * ddi_soft_state_free(sd_state, instance) is the right way to 8514 * clean up after the ddi_soft_state_zalloc() if the subsequent 8515 * ddi_get_soft_state() fails. The implication seems to be 8516 * that the get_soft_state cannot fail if the zalloc succeeds. 8517 */ 8518 ddi_soft_state_free(sd_state, instance); 8519 8520 probe_failed: 8521 scsi_unprobe(devp); 8522 #ifdef SDDEBUG 8523 if ((sd_component_mask & SD_LOG_ATTACH_DETACH) && 8524 (sd_level_mask & SD_LOGMASK_TRACE)) { 8525 cmn_err(CE_CONT, "sd_unit_attach: un:0x%p exit failure\n", 8526 (void *)un); 8527 } 8528 #endif 8529 return (DDI_FAILURE); 8530 } 8531 8532 8533 /* 8534 * Function: sd_unit_detach 8535 * 8536 * Description: Performs DDI_DETACH processing for sddetach(). 8537 * 8538 * Return Code: DDI_SUCCESS 8539 * DDI_FAILURE 8540 * 8541 * Context: Kernel thread context 8542 */ 8543 8544 static int 8545 sd_unit_detach(dev_info_t *devi) 8546 { 8547 struct scsi_device *devp; 8548 struct sd_lun *un; 8549 int i; 8550 dev_t dev; 8551 #if !(defined(__i386) || defined(__amd64)) && !defined(__fibre) 8552 int reset_retval; 8553 #endif 8554 int instance = ddi_get_instance(devi); 8555 8556 mutex_enter(&sd_detach_mutex); 8557 8558 /* 8559 * Fail the detach for any of the following: 8560 * - Unable to get the sd_lun struct for the instance 8561 * - A layered driver has an outstanding open on the instance 8562 * - Another thread is already detaching this instance 8563 * - Another thread is currently performing an open 8564 */ 8565 devp = ddi_get_driver_private(devi); 8566 if ((devp == NULL) || 8567 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 8568 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 8569 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 8570 mutex_exit(&sd_detach_mutex); 8571 return (DDI_FAILURE); 8572 } 8573 8574 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 8575 8576 /* 8577 * Mark this instance as currently in a detach, to inhibit any 8578 * opens from a layered driver. 8579 */ 8580 un->un_detach_count++; 8581 mutex_exit(&sd_detach_mutex); 8582 8583 dev = sd_make_device(SD_DEVINFO(un)); 8584 8585 _NOTE(COMPETING_THREADS_NOW); 8586 8587 mutex_enter(SD_MUTEX(un)); 8588 8589 /* 8590 * Fail the detach if there are any outstanding layered 8591 * opens on this device. 8592 */ 8593 for (i = 0; i < NDKMAP; i++) { 8594 if (un->un_ocmap.lyropen[i] != 0) { 8595 goto err_notclosed; 8596 } 8597 } 8598 8599 /* 8600 * Verify there are NO outstanding commands issued to this device. 8601 * ie, un_ncmds_in_transport == 0. 8602 * It's possible to have outstanding commands through the physio 8603 * code path, even though everything's closed. 8604 */ 8605 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 8606 (un->un_direct_priority_timeid != NULL) || 8607 (un->un_state == SD_STATE_RWAIT)) { 8608 mutex_exit(SD_MUTEX(un)); 8609 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8610 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 8611 goto err_stillbusy; 8612 } 8613 8614 /* 8615 * If we have the device reserved, release the reservation. 8616 */ 8617 if ((un->un_resvd_status & SD_RESERVE) && 8618 !(un->un_resvd_status & SD_LOST_RESERVE)) { 8619 mutex_exit(SD_MUTEX(un)); 8620 /* 8621 * Note: sd_reserve_release sends a command to the device 8622 * via the sd_ioctlcmd() path, and can sleep. 8623 */ 8624 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 8625 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8626 "sd_dr_detach: Cannot release reservation \n"); 8627 } 8628 } else { 8629 mutex_exit(SD_MUTEX(un)); 8630 } 8631 8632 /* 8633 * Untimeout any reserve recover, throttle reset, restart unit 8634 * and delayed broadcast timeout threads. Protect the timeout pointer 8635 * from getting nulled by their callback functions. 8636 */ 8637 mutex_enter(SD_MUTEX(un)); 8638 if (un->un_resvd_timeid != NULL) { 8639 timeout_id_t temp_id = un->un_resvd_timeid; 8640 un->un_resvd_timeid = NULL; 8641 mutex_exit(SD_MUTEX(un)); 8642 (void) untimeout(temp_id); 8643 mutex_enter(SD_MUTEX(un)); 8644 } 8645 8646 if (un->un_reset_throttle_timeid != NULL) { 8647 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8648 un->un_reset_throttle_timeid = NULL; 8649 mutex_exit(SD_MUTEX(un)); 8650 (void) untimeout(temp_id); 8651 mutex_enter(SD_MUTEX(un)); 8652 } 8653 8654 if (un->un_startstop_timeid != NULL) { 8655 timeout_id_t temp_id = un->un_startstop_timeid; 8656 un->un_startstop_timeid = NULL; 8657 mutex_exit(SD_MUTEX(un)); 8658 (void) untimeout(temp_id); 8659 mutex_enter(SD_MUTEX(un)); 8660 } 8661 8662 if (un->un_dcvb_timeid != NULL) { 8663 timeout_id_t temp_id = un->un_dcvb_timeid; 8664 un->un_dcvb_timeid = NULL; 8665 mutex_exit(SD_MUTEX(un)); 8666 (void) untimeout(temp_id); 8667 } else { 8668 mutex_exit(SD_MUTEX(un)); 8669 } 8670 8671 /* Remove any pending reservation reclaim requests for this device */ 8672 sd_rmv_resv_reclaim_req(dev); 8673 8674 mutex_enter(SD_MUTEX(un)); 8675 8676 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 8677 if (un->un_direct_priority_timeid != NULL) { 8678 timeout_id_t temp_id = un->un_direct_priority_timeid; 8679 un->un_direct_priority_timeid = NULL; 8680 mutex_exit(SD_MUTEX(un)); 8681 (void) untimeout(temp_id); 8682 mutex_enter(SD_MUTEX(un)); 8683 } 8684 8685 /* Cancel any active multi-host disk watch thread requests */ 8686 if (un->un_mhd_token != NULL) { 8687 mutex_exit(SD_MUTEX(un)); 8688 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 8689 if (scsi_watch_request_terminate(un->un_mhd_token, 8690 SCSI_WATCH_TERMINATE_NOWAIT)) { 8691 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8692 "sd_dr_detach: Cannot cancel mhd watch request\n"); 8693 /* 8694 * Note: We are returning here after having removed 8695 * some driver timeouts above. This is consistent with 8696 * the legacy implementation but perhaps the watch 8697 * terminate call should be made with the wait flag set. 8698 */ 8699 goto err_stillbusy; 8700 } 8701 mutex_enter(SD_MUTEX(un)); 8702 un->un_mhd_token = NULL; 8703 } 8704 8705 if (un->un_swr_token != NULL) { 8706 mutex_exit(SD_MUTEX(un)); 8707 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 8708 if (scsi_watch_request_terminate(un->un_swr_token, 8709 SCSI_WATCH_TERMINATE_NOWAIT)) { 8710 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8711 "sd_dr_detach: Cannot cancel swr watch request\n"); 8712 /* 8713 * Note: We are returning here after having removed 8714 * some driver timeouts above. This is consistent with 8715 * the legacy implementation but perhaps the watch 8716 * terminate call should be made with the wait flag set. 8717 */ 8718 goto err_stillbusy; 8719 } 8720 mutex_enter(SD_MUTEX(un)); 8721 un->un_swr_token = NULL; 8722 } 8723 8724 mutex_exit(SD_MUTEX(un)); 8725 8726 /* 8727 * Clear any scsi_reset_notifies. We clear the reset notifies 8728 * if we have not registered one. 8729 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 8730 */ 8731 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 8732 sd_mhd_reset_notify_cb, (caddr_t)un); 8733 8734 8735 8736 #if defined(__i386) || defined(__amd64) 8737 /* 8738 * Gratuitous bus resets sometimes cause an otherwise 8739 * okay ATA/ATAPI bus to hang. This is due the lack of 8740 * a clear spec of how resets should be implemented by ATA 8741 * disk drives. 8742 */ 8743 #elif !defined(__fibre) /* "#else if" does NOT work! */ 8744 /* 8745 * Reset target/bus. 8746 * 8747 * Note: This is a legacy workaround for Elite III dual-port drives that 8748 * will not come online after an aborted detach and subsequent re-attach 8749 * It should be removed when the Elite III FW is fixed, or the drives 8750 * are no longer supported. 8751 */ 8752 if (un->un_f_cfg_is_atapi == FALSE) { 8753 reset_retval = 0; 8754 8755 /* If the device is in low power mode don't reset it */ 8756 8757 mutex_enter(&un->un_pm_mutex); 8758 if (!SD_DEVICE_IS_IN_LOW_POWER(un)) { 8759 /* 8760 * First try a LUN reset if we can, then move on to a 8761 * target reset if needed; swat the bus as a last 8762 * resort. 8763 */ 8764 mutex_exit(&un->un_pm_mutex); 8765 if (un->un_f_allow_bus_device_reset == TRUE) { 8766 if (un->un_f_lun_reset_enabled == TRUE) { 8767 reset_retval = 8768 scsi_reset(SD_ADDRESS(un), 8769 RESET_LUN); 8770 } 8771 if (reset_retval == 0) { 8772 reset_retval = 8773 scsi_reset(SD_ADDRESS(un), 8774 RESET_TARGET); 8775 } 8776 } 8777 if (reset_retval == 0) { 8778 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 8779 } 8780 } else { 8781 mutex_exit(&un->un_pm_mutex); 8782 } 8783 } 8784 #endif 8785 8786 /* 8787 * protect the timeout pointers from getting nulled by 8788 * their callback functions during the cancellation process. 8789 * In such a scenario untimeout can be invoked with a null value. 8790 */ 8791 _NOTE(NO_COMPETING_THREADS_NOW); 8792 8793 mutex_enter(&un->un_pm_mutex); 8794 if (un->un_pm_idle_timeid != NULL) { 8795 timeout_id_t temp_id = un->un_pm_idle_timeid; 8796 un->un_pm_idle_timeid = NULL; 8797 mutex_exit(&un->un_pm_mutex); 8798 8799 /* 8800 * Timeout is active; cancel it. 8801 * Note that it'll never be active on a device 8802 * that does not support PM therefore we don't 8803 * have to check before calling pm_idle_component. 8804 */ 8805 (void) untimeout(temp_id); 8806 (void) pm_idle_component(SD_DEVINFO(un), 0); 8807 mutex_enter(&un->un_pm_mutex); 8808 } 8809 8810 /* 8811 * Check whether there is already a timeout scheduled for power 8812 * management. If yes then don't lower the power here, that's. 8813 * the timeout handler's job. 8814 */ 8815 if (un->un_pm_timeid != NULL) { 8816 timeout_id_t temp_id = un->un_pm_timeid; 8817 un->un_pm_timeid = NULL; 8818 mutex_exit(&un->un_pm_mutex); 8819 /* 8820 * Timeout is active; cancel it. 8821 * Note that it'll never be active on a device 8822 * that does not support PM therefore we don't 8823 * have to check before calling pm_idle_component. 8824 */ 8825 (void) untimeout(temp_id); 8826 (void) pm_idle_component(SD_DEVINFO(un), 0); 8827 8828 } else { 8829 mutex_exit(&un->un_pm_mutex); 8830 if ((un->un_f_pm_is_enabled == TRUE) && 8831 (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) != 8832 DDI_SUCCESS)) { 8833 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8834 "sd_dr_detach: Lower power request failed, ignoring.\n"); 8835 /* 8836 * Fix for bug: 4297749, item # 13 8837 * The above test now includes a check to see if PM is 8838 * supported by this device before call 8839 * pm_lower_power(). 8840 * Note, the following is not dead code. The call to 8841 * pm_lower_power above will generate a call back into 8842 * our sdpower routine which might result in a timeout 8843 * handler getting activated. Therefore the following 8844 * code is valid and necessary. 8845 */ 8846 mutex_enter(&un->un_pm_mutex); 8847 if (un->un_pm_timeid != NULL) { 8848 timeout_id_t temp_id = un->un_pm_timeid; 8849 un->un_pm_timeid = NULL; 8850 mutex_exit(&un->un_pm_mutex); 8851 (void) untimeout(temp_id); 8852 (void) pm_idle_component(SD_DEVINFO(un), 0); 8853 } else { 8854 mutex_exit(&un->un_pm_mutex); 8855 } 8856 } 8857 } 8858 8859 /* 8860 * Cleanup from the scsi_ifsetcap() calls (437868) 8861 * Relocated here from above to be after the call to 8862 * pm_lower_power, which was getting errors. 8863 */ 8864 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8865 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8866 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8867 8868 if (un->un_f_is_fibre == FALSE) { 8869 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8870 } 8871 8872 /* 8873 * Remove any event callbacks, fibre only 8874 */ 8875 if (un->un_f_is_fibre == TRUE) { 8876 if ((un->un_insert_event != NULL) && 8877 (ddi_remove_event_handler(un->un_insert_cb_id) != 8878 DDI_SUCCESS)) { 8879 /* 8880 * Note: We are returning here after having done 8881 * substantial cleanup above. This is consistent 8882 * with the legacy implementation but this may not 8883 * be the right thing to do. 8884 */ 8885 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8886 "sd_dr_detach: Cannot cancel insert event\n"); 8887 goto err_remove_event; 8888 } 8889 un->un_insert_event = NULL; 8890 8891 if ((un->un_remove_event != NULL) && 8892 (ddi_remove_event_handler(un->un_remove_cb_id) != 8893 DDI_SUCCESS)) { 8894 /* 8895 * Note: We are returning here after having done 8896 * substantial cleanup above. This is consistent 8897 * with the legacy implementation but this may not 8898 * be the right thing to do. 8899 */ 8900 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8901 "sd_dr_detach: Cannot cancel remove event\n"); 8902 goto err_remove_event; 8903 } 8904 un->un_remove_event = NULL; 8905 } 8906 8907 /* Do not free the softstate if the callback routine is active */ 8908 sd_sync_with_callback(un); 8909 8910 /* 8911 * Hold the detach mutex here, to make sure that no other threads ever 8912 * can access a (partially) freed soft state structure. 8913 */ 8914 mutex_enter(&sd_detach_mutex); 8915 8916 /* 8917 * Clean up the soft state struct. 8918 * Cleanup is done in reverse order of allocs/inits. 8919 * At this point there should be no competing threads anymore. 8920 */ 8921 8922 /* Unregister and free device id. */ 8923 ddi_devid_unregister(devi); 8924 if (un->un_devid) { 8925 ddi_devid_free(un->un_devid); 8926 un->un_devid = NULL; 8927 } 8928 8929 /* 8930 * Destroy wmap cache if it exists. 8931 */ 8932 if (un->un_wm_cache != NULL) { 8933 kmem_cache_destroy(un->un_wm_cache); 8934 un->un_wm_cache = NULL; 8935 } 8936 8937 /* Remove minor nodes */ 8938 ddi_remove_minor_node(devi, NULL); 8939 8940 /* 8941 * kstat cleanup is done in detach for all device types (4363169). 8942 * We do not want to fail detach if the device kstats are not deleted 8943 * since there is a confusion about the devo_refcnt for the device. 8944 * We just delete the kstats and let detach complete successfully. 8945 */ 8946 if (un->un_stats != NULL) { 8947 kstat_delete(un->un_stats); 8948 un->un_stats = NULL; 8949 } 8950 if (un->un_errstats != NULL) { 8951 kstat_delete(un->un_errstats); 8952 un->un_errstats = NULL; 8953 } 8954 8955 /* Remove partition stats (not created for removables) */ 8956 if (!ISREMOVABLE(un)) { 8957 for (i = 0; i < NSDMAP; i++) { 8958 if (un->un_pstats[i] != NULL) { 8959 kstat_delete(un->un_pstats[i]); 8960 un->un_pstats[i] = NULL; 8961 } 8962 } 8963 } 8964 8965 /* Remove xbuf registration */ 8966 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 8967 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 8968 8969 /* Remove driver properties */ 8970 ddi_prop_remove_all(devi); 8971 8972 mutex_destroy(&un->un_pm_mutex); 8973 cv_destroy(&un->un_pm_busy_cv); 8974 8975 /* Open/close semaphore */ 8976 sema_destroy(&un->un_semoclose); 8977 8978 /* Removable media condvar. */ 8979 cv_destroy(&un->un_state_cv); 8980 8981 /* Suspend/resume condvar. */ 8982 cv_destroy(&un->un_suspend_cv); 8983 cv_destroy(&un->un_disk_busy_cv); 8984 8985 sd_free_rqs(un); 8986 8987 /* Free up soft state */ 8988 devp->sd_private = NULL; 8989 bzero(un, sizeof (struct sd_lun)); 8990 ddi_soft_state_free(sd_state, instance); 8991 8992 mutex_exit(&sd_detach_mutex); 8993 8994 /* This frees up the INQUIRY data associated with the device. */ 8995 scsi_unprobe(devp); 8996 8997 return (DDI_SUCCESS); 8998 8999 err_notclosed: 9000 mutex_exit(SD_MUTEX(un)); 9001 9002 err_stillbusy: 9003 _NOTE(NO_COMPETING_THREADS_NOW); 9004 9005 err_remove_event: 9006 mutex_enter(&sd_detach_mutex); 9007 un->un_detach_count--; 9008 mutex_exit(&sd_detach_mutex); 9009 9010 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 9011 return (DDI_FAILURE); 9012 } 9013 9014 9015 /* 9016 * Driver minor node structure and data table 9017 */ 9018 struct driver_minor_data { 9019 char *name; 9020 minor_t minor; 9021 int type; 9022 }; 9023 9024 static struct driver_minor_data sd_minor_data[] = { 9025 {"a", 0, S_IFBLK}, 9026 {"b", 1, S_IFBLK}, 9027 {"c", 2, S_IFBLK}, 9028 {"d", 3, S_IFBLK}, 9029 {"e", 4, S_IFBLK}, 9030 {"f", 5, S_IFBLK}, 9031 {"g", 6, S_IFBLK}, 9032 {"h", 7, S_IFBLK}, 9033 #if defined(_SUNOS_VTOC_16) 9034 {"i", 8, S_IFBLK}, 9035 {"j", 9, S_IFBLK}, 9036 {"k", 10, S_IFBLK}, 9037 {"l", 11, S_IFBLK}, 9038 {"m", 12, S_IFBLK}, 9039 {"n", 13, S_IFBLK}, 9040 {"o", 14, S_IFBLK}, 9041 {"p", 15, S_IFBLK}, 9042 #endif /* defined(_SUNOS_VTOC_16) */ 9043 #if defined(_FIRMWARE_NEEDS_FDISK) 9044 {"q", 16, S_IFBLK}, 9045 {"r", 17, S_IFBLK}, 9046 {"s", 18, S_IFBLK}, 9047 {"t", 19, S_IFBLK}, 9048 {"u", 20, S_IFBLK}, 9049 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9050 {"a,raw", 0, S_IFCHR}, 9051 {"b,raw", 1, S_IFCHR}, 9052 {"c,raw", 2, S_IFCHR}, 9053 {"d,raw", 3, S_IFCHR}, 9054 {"e,raw", 4, S_IFCHR}, 9055 {"f,raw", 5, S_IFCHR}, 9056 {"g,raw", 6, S_IFCHR}, 9057 {"h,raw", 7, S_IFCHR}, 9058 #if defined(_SUNOS_VTOC_16) 9059 {"i,raw", 8, S_IFCHR}, 9060 {"j,raw", 9, S_IFCHR}, 9061 {"k,raw", 10, S_IFCHR}, 9062 {"l,raw", 11, S_IFCHR}, 9063 {"m,raw", 12, S_IFCHR}, 9064 {"n,raw", 13, S_IFCHR}, 9065 {"o,raw", 14, S_IFCHR}, 9066 {"p,raw", 15, S_IFCHR}, 9067 #endif /* defined(_SUNOS_VTOC_16) */ 9068 #if defined(_FIRMWARE_NEEDS_FDISK) 9069 {"q,raw", 16, S_IFCHR}, 9070 {"r,raw", 17, S_IFCHR}, 9071 {"s,raw", 18, S_IFCHR}, 9072 {"t,raw", 19, S_IFCHR}, 9073 {"u,raw", 20, S_IFCHR}, 9074 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9075 {0} 9076 }; 9077 9078 static struct driver_minor_data sd_minor_data_efi[] = { 9079 {"a", 0, S_IFBLK}, 9080 {"b", 1, S_IFBLK}, 9081 {"c", 2, S_IFBLK}, 9082 {"d", 3, S_IFBLK}, 9083 {"e", 4, S_IFBLK}, 9084 {"f", 5, S_IFBLK}, 9085 {"g", 6, S_IFBLK}, 9086 {"wd", 7, S_IFBLK}, 9087 #if defined(_FIRMWARE_NEEDS_FDISK) 9088 {"q", 16, S_IFBLK}, 9089 {"r", 17, S_IFBLK}, 9090 {"s", 18, S_IFBLK}, 9091 {"t", 19, S_IFBLK}, 9092 {"u", 20, S_IFBLK}, 9093 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9094 {"a,raw", 0, S_IFCHR}, 9095 {"b,raw", 1, S_IFCHR}, 9096 {"c,raw", 2, S_IFCHR}, 9097 {"d,raw", 3, S_IFCHR}, 9098 {"e,raw", 4, S_IFCHR}, 9099 {"f,raw", 5, S_IFCHR}, 9100 {"g,raw", 6, S_IFCHR}, 9101 {"wd,raw", 7, S_IFCHR}, 9102 #if defined(_FIRMWARE_NEEDS_FDISK) 9103 {"q,raw", 16, S_IFCHR}, 9104 {"r,raw", 17, S_IFCHR}, 9105 {"s,raw", 18, S_IFCHR}, 9106 {"t,raw", 19, S_IFCHR}, 9107 {"u,raw", 20, S_IFCHR}, 9108 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9109 {0} 9110 }; 9111 9112 9113 /* 9114 * Function: sd_create_minor_nodes 9115 * 9116 * Description: Create the minor device nodes for the instance. 9117 * 9118 * Arguments: un - driver soft state (unit) structure 9119 * devi - pointer to device info structure 9120 * 9121 * Return Code: DDI_SUCCESS 9122 * DDI_FAILURE 9123 * 9124 * Context: Kernel thread context 9125 */ 9126 9127 static int 9128 sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi) 9129 { 9130 struct driver_minor_data *dmdp; 9131 struct scsi_device *devp; 9132 int instance; 9133 char name[48]; 9134 9135 ASSERT(un != NULL); 9136 devp = ddi_get_driver_private(devi); 9137 instance = ddi_get_instance(devp->sd_dev); 9138 9139 /* 9140 * Create all the minor nodes for this target. 9141 */ 9142 if (un->un_blockcount > DK_MAX_BLOCKS) 9143 dmdp = sd_minor_data_efi; 9144 else 9145 dmdp = sd_minor_data; 9146 while (dmdp->name != NULL) { 9147 9148 (void) sprintf(name, "%s", dmdp->name); 9149 9150 if (ddi_create_minor_node(devi, name, dmdp->type, 9151 (instance << SDUNIT_SHIFT) | dmdp->minor, 9152 un->un_node_type, NULL) == DDI_FAILURE) { 9153 /* 9154 * Clean up any nodes that may have been created, in 9155 * case this fails in the middle of the loop. 9156 */ 9157 ddi_remove_minor_node(devi, NULL); 9158 return (DDI_FAILURE); 9159 } 9160 dmdp++; 9161 } 9162 9163 return (DDI_SUCCESS); 9164 } 9165 9166 9167 /* 9168 * Function: sd_create_errstats 9169 * 9170 * Description: This routine instantiates the device error stats. 9171 * 9172 * Note: During attach the stats are instantiated first so they are 9173 * available for attach-time routines that utilize the driver 9174 * iopath to send commands to the device. The stats are initialized 9175 * separately so data obtained during some attach-time routines is 9176 * available. (4362483) 9177 * 9178 * Arguments: un - driver soft state (unit) structure 9179 * instance - driver instance 9180 * 9181 * Context: Kernel thread context 9182 */ 9183 9184 static void 9185 sd_create_errstats(struct sd_lun *un, int instance) 9186 { 9187 struct sd_errstats *stp; 9188 char kstatmodule_err[KSTAT_STRLEN]; 9189 char kstatname[KSTAT_STRLEN]; 9190 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 9191 9192 ASSERT(un != NULL); 9193 9194 if (un->un_errstats != NULL) { 9195 return; 9196 } 9197 9198 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 9199 "%serr", sd_label); 9200 (void) snprintf(kstatname, sizeof (kstatname), 9201 "%s%d,err", sd_label, instance); 9202 9203 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 9204 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 9205 9206 if (un->un_errstats == NULL) { 9207 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9208 "sd_create_errstats: Failed kstat_create\n"); 9209 return; 9210 } 9211 9212 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9213 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 9214 KSTAT_DATA_UINT32); 9215 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 9216 KSTAT_DATA_UINT32); 9217 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 9218 KSTAT_DATA_UINT32); 9219 kstat_named_init(&stp->sd_vid, "Vendor", 9220 KSTAT_DATA_CHAR); 9221 kstat_named_init(&stp->sd_pid, "Product", 9222 KSTAT_DATA_CHAR); 9223 kstat_named_init(&stp->sd_revision, "Revision", 9224 KSTAT_DATA_CHAR); 9225 kstat_named_init(&stp->sd_serial, "Serial No", 9226 KSTAT_DATA_CHAR); 9227 kstat_named_init(&stp->sd_capacity, "Size", 9228 KSTAT_DATA_ULONGLONG); 9229 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 9230 KSTAT_DATA_UINT32); 9231 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 9232 KSTAT_DATA_UINT32); 9233 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 9234 KSTAT_DATA_UINT32); 9235 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 9236 KSTAT_DATA_UINT32); 9237 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 9238 KSTAT_DATA_UINT32); 9239 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 9240 KSTAT_DATA_UINT32); 9241 9242 un->un_errstats->ks_private = un; 9243 un->un_errstats->ks_update = nulldev; 9244 9245 kstat_install(un->un_errstats); 9246 } 9247 9248 9249 /* 9250 * Function: sd_set_errstats 9251 * 9252 * Description: This routine sets the value of the vendor id, product id, 9253 * revision, serial number, and capacity device error stats. 9254 * 9255 * Note: During attach the stats are instantiated first so they are 9256 * available for attach-time routines that utilize the driver 9257 * iopath to send commands to the device. The stats are initialized 9258 * separately so data obtained during some attach-time routines is 9259 * available. (4362483) 9260 * 9261 * Arguments: un - driver soft state (unit) structure 9262 * 9263 * Context: Kernel thread context 9264 */ 9265 9266 static void 9267 sd_set_errstats(struct sd_lun *un) 9268 { 9269 struct sd_errstats *stp; 9270 9271 ASSERT(un != NULL); 9272 ASSERT(un->un_errstats != NULL); 9273 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9274 ASSERT(stp != NULL); 9275 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 9276 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 9277 (void) strncpy(stp->sd_revision.value.c, 9278 un->un_sd->sd_inq->inq_revision, 4); 9279 9280 /* 9281 * Set the "Serial No" kstat for Sun qualified drives (indicated by 9282 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 9283 * (4376302)) 9284 */ 9285 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 9286 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9287 sizeof (SD_INQUIRY(un)->inq_serial)); 9288 } 9289 9290 if (un->un_f_blockcount_is_valid != TRUE) { 9291 /* 9292 * Set capacity error stat to 0 for no media. This ensures 9293 * a valid capacity is displayed in response to 'iostat -E' 9294 * when no media is present in the device. 9295 */ 9296 stp->sd_capacity.value.ui64 = 0; 9297 } else { 9298 /* 9299 * Multiply un_blockcount by un->un_sys_blocksize to get 9300 * capacity. 9301 * 9302 * Note: for non-512 blocksize devices "un_blockcount" has been 9303 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 9304 * (un_tgt_blocksize / un->un_sys_blocksize). 9305 */ 9306 stp->sd_capacity.value.ui64 = (uint64_t) 9307 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 9308 } 9309 } 9310 9311 9312 /* 9313 * Function: sd_set_pstats 9314 * 9315 * Description: This routine instantiates and initializes the partition 9316 * stats for each partition with more than zero blocks. 9317 * (4363169) 9318 * 9319 * Arguments: un - driver soft state (unit) structure 9320 * 9321 * Context: Kernel thread context 9322 */ 9323 9324 static void 9325 sd_set_pstats(struct sd_lun *un) 9326 { 9327 char kstatname[KSTAT_STRLEN]; 9328 int instance; 9329 int i; 9330 9331 ASSERT(un != NULL); 9332 9333 instance = ddi_get_instance(SD_DEVINFO(un)); 9334 9335 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 9336 for (i = 0; i < NSDMAP; i++) { 9337 if ((un->un_pstats[i] == NULL) && 9338 (un->un_map[i].dkl_nblk != 0)) { 9339 (void) snprintf(kstatname, sizeof (kstatname), 9340 "%s%d,%s", sd_label, instance, 9341 sd_minor_data[i].name); 9342 un->un_pstats[i] = kstat_create(sd_label, 9343 instance, kstatname, "partition", KSTAT_TYPE_IO, 9344 1, KSTAT_FLAG_PERSISTENT); 9345 if (un->un_pstats[i] != NULL) { 9346 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 9347 kstat_install(un->un_pstats[i]); 9348 } 9349 } 9350 } 9351 } 9352 9353 9354 #if (defined(__fibre)) 9355 /* 9356 * Function: sd_init_event_callbacks 9357 * 9358 * Description: This routine initializes the insertion and removal event 9359 * callbacks. (fibre only) 9360 * 9361 * Arguments: un - driver soft state (unit) structure 9362 * 9363 * Context: Kernel thread context 9364 */ 9365 9366 static void 9367 sd_init_event_callbacks(struct sd_lun *un) 9368 { 9369 ASSERT(un != NULL); 9370 9371 if ((un->un_insert_event == NULL) && 9372 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 9373 &un->un_insert_event) == DDI_SUCCESS)) { 9374 /* 9375 * Add the callback for an insertion event 9376 */ 9377 (void) ddi_add_event_handler(SD_DEVINFO(un), 9378 un->un_insert_event, sd_event_callback, (void *)un, 9379 &(un->un_insert_cb_id)); 9380 } 9381 9382 if ((un->un_remove_event == NULL) && 9383 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 9384 &un->un_remove_event) == DDI_SUCCESS)) { 9385 /* 9386 * Add the callback for a removal event 9387 */ 9388 (void) ddi_add_event_handler(SD_DEVINFO(un), 9389 un->un_remove_event, sd_event_callback, (void *)un, 9390 &(un->un_remove_cb_id)); 9391 } 9392 } 9393 9394 9395 /* 9396 * Function: sd_event_callback 9397 * 9398 * Description: This routine handles insert/remove events (photon). The 9399 * state is changed to OFFLINE which can be used to supress 9400 * error msgs. (fibre only) 9401 * 9402 * Arguments: un - driver soft state (unit) structure 9403 * 9404 * Context: Callout thread context 9405 */ 9406 /* ARGSUSED */ 9407 static void 9408 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 9409 void *bus_impldata) 9410 { 9411 struct sd_lun *un = (struct sd_lun *)arg; 9412 9413 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 9414 if (event == un->un_insert_event) { 9415 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 9416 mutex_enter(SD_MUTEX(un)); 9417 if (un->un_state == SD_STATE_OFFLINE) { 9418 if (un->un_last_state != SD_STATE_SUSPENDED) { 9419 un->un_state = un->un_last_state; 9420 } else { 9421 /* 9422 * We have gone through SUSPEND/RESUME while 9423 * we were offline. Restore the last state 9424 */ 9425 un->un_state = un->un_save_state; 9426 } 9427 } 9428 mutex_exit(SD_MUTEX(un)); 9429 9430 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 9431 } else if (event == un->un_remove_event) { 9432 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 9433 mutex_enter(SD_MUTEX(un)); 9434 /* 9435 * We need to handle an event callback that occurs during 9436 * the suspend operation, since we don't prevent it. 9437 */ 9438 if (un->un_state != SD_STATE_OFFLINE) { 9439 if (un->un_state != SD_STATE_SUSPENDED) { 9440 New_state(un, SD_STATE_OFFLINE); 9441 } else { 9442 un->un_last_state = SD_STATE_OFFLINE; 9443 } 9444 } 9445 mutex_exit(SD_MUTEX(un)); 9446 } else { 9447 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 9448 "!Unknown event\n"); 9449 } 9450 9451 } 9452 #endif 9453 9454 9455 /* 9456 * Function: sd_disable_caching() 9457 * 9458 * Description: This routine is the driver entry point for disabling 9459 * read and write caching by modifying the WCE (write cache 9460 * enable) and RCD (read cache disable) bits of mode 9461 * page 8 (MODEPAGE_CACHING). 9462 * 9463 * Arguments: un - driver soft state (unit) structure 9464 * 9465 * Return Code: EIO 9466 * code returned by sd_send_scsi_MODE_SENSE and 9467 * sd_send_scsi_MODE_SELECT 9468 * 9469 * Context: Kernel Thread 9470 */ 9471 9472 static int 9473 sd_disable_caching(struct sd_lun *un) 9474 { 9475 struct mode_caching *mode_caching_page; 9476 uchar_t *header; 9477 size_t buflen; 9478 int hdrlen; 9479 int bd_len; 9480 int rval = 0; 9481 9482 ASSERT(un != NULL); 9483 9484 /* 9485 * Do a test unit ready, otherwise a mode sense may not work if this 9486 * is the first command sent to the device after boot. 9487 */ 9488 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 9489 9490 if (un->un_f_cfg_is_atapi == TRUE) { 9491 hdrlen = MODE_HEADER_LENGTH_GRP2; 9492 } else { 9493 hdrlen = MODE_HEADER_LENGTH; 9494 } 9495 9496 /* 9497 * Allocate memory for the retrieved mode page and its headers. Set 9498 * a pointer to the page itself. 9499 */ 9500 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 9501 header = kmem_zalloc(buflen, KM_SLEEP); 9502 9503 /* Get the information from the device. */ 9504 if (un->un_f_cfg_is_atapi == TRUE) { 9505 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 9506 MODEPAGE_CACHING, SD_PATH_DIRECT); 9507 } else { 9508 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 9509 MODEPAGE_CACHING, SD_PATH_DIRECT); 9510 } 9511 if (rval != 0) { 9512 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9513 "sd_disable_caching: Mode Sense Failed\n"); 9514 kmem_free(header, buflen); 9515 return (rval); 9516 } 9517 9518 /* 9519 * Determine size of Block Descriptors in order to locate 9520 * the mode page data. ATAPI devices return 0, SCSI devices 9521 * should return MODE_BLK_DESC_LENGTH. 9522 */ 9523 if (un->un_f_cfg_is_atapi == TRUE) { 9524 struct mode_header_grp2 *mhp; 9525 mhp = (struct mode_header_grp2 *)header; 9526 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9527 } else { 9528 bd_len = ((struct mode_header *)header)->bdesc_length; 9529 } 9530 9531 if (bd_len > MODE_BLK_DESC_LENGTH) { 9532 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 9533 "sd_disable_caching: Mode Sense returned invalid " 9534 "block descriptor length\n"); 9535 kmem_free(header, buflen); 9536 return (EIO); 9537 } 9538 9539 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9540 9541 /* Check the relevant bits on successful mode sense. */ 9542 if ((mode_caching_page->wce) || !(mode_caching_page->rcd)) { 9543 /* 9544 * Read or write caching is enabled. Disable both of them. 9545 */ 9546 mode_caching_page->wce = 0; 9547 mode_caching_page->rcd = 1; 9548 9549 /* Clear reserved bits before mode select. */ 9550 mode_caching_page->mode_page.ps = 0; 9551 9552 /* 9553 * Clear out mode header for mode select. 9554 * The rest of the retrieved page will be reused. 9555 */ 9556 bzero(header, hdrlen); 9557 9558 /* Change the cache page to disable all caching. */ 9559 if (un->un_f_cfg_is_atapi == TRUE) { 9560 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header, 9561 buflen, SD_SAVE_PAGE, SD_PATH_DIRECT); 9562 } else { 9563 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 9564 buflen, SD_SAVE_PAGE, SD_PATH_DIRECT); 9565 } 9566 } 9567 9568 kmem_free(header, buflen); 9569 return (rval); 9570 } 9571 9572 9573 /* 9574 * Function: sd_make_device 9575 * 9576 * Description: Utility routine to return the Solaris device number from 9577 * the data in the device's dev_info structure. 9578 * 9579 * Return Code: The Solaris device number 9580 * 9581 * Context: Any 9582 */ 9583 9584 static dev_t 9585 sd_make_device(dev_info_t *devi) 9586 { 9587 return (makedevice(ddi_name_to_major(ddi_get_name(devi)), 9588 ddi_get_instance(devi) << SDUNIT_SHIFT)); 9589 } 9590 9591 9592 /* 9593 * Function: sd_pm_entry 9594 * 9595 * Description: Called at the start of a new command to manage power 9596 * and busy status of a device. This includes determining whether 9597 * the current power state of the device is sufficient for 9598 * performing the command or whether it must be changed. 9599 * The PM framework is notified appropriately. 9600 * Only with a return status of DDI_SUCCESS will the 9601 * component be busy to the framework. 9602 * 9603 * All callers of sd_pm_entry must check the return status 9604 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 9605 * of DDI_FAILURE indicates the device failed to power up. 9606 * In this case un_pm_count has been adjusted so the result 9607 * on exit is still powered down, ie. count is less than 0. 9608 * Calling sd_pm_exit with this count value hits an ASSERT. 9609 * 9610 * Return Code: DDI_SUCCESS or DDI_FAILURE 9611 * 9612 * Context: Kernel thread context. 9613 */ 9614 9615 static int 9616 sd_pm_entry(struct sd_lun *un) 9617 { 9618 int return_status = DDI_SUCCESS; 9619 9620 ASSERT(!mutex_owned(SD_MUTEX(un))); 9621 ASSERT(!mutex_owned(&un->un_pm_mutex)); 9622 9623 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 9624 9625 if (un->un_f_pm_is_enabled == FALSE) { 9626 SD_TRACE(SD_LOG_IO_PM, un, 9627 "sd_pm_entry: exiting, PM not enabled\n"); 9628 return (return_status); 9629 } 9630 9631 /* 9632 * Just increment a counter if PM is enabled. On the transition from 9633 * 0 ==> 1, mark the device as busy. The iodone side will decrement 9634 * the count with each IO and mark the device as idle when the count 9635 * hits 0. 9636 * 9637 * If the count is less than 0 the device is powered down. If a powered 9638 * down device is successfully powered up then the count must be 9639 * incremented to reflect the power up. Note that it'll get incremented 9640 * a second time to become busy. 9641 * 9642 * Because the following has the potential to change the device state 9643 * and must release the un_pm_mutex to do so, only one thread can be 9644 * allowed through at a time. 9645 */ 9646 9647 mutex_enter(&un->un_pm_mutex); 9648 while (un->un_pm_busy == TRUE) { 9649 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 9650 } 9651 un->un_pm_busy = TRUE; 9652 9653 if (un->un_pm_count < 1) { 9654 9655 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 9656 9657 /* 9658 * Indicate we are now busy so the framework won't attempt to 9659 * power down the device. This call will only fail if either 9660 * we passed a bad component number or the device has no 9661 * components. Neither of these should ever happen. 9662 */ 9663 mutex_exit(&un->un_pm_mutex); 9664 return_status = pm_busy_component(SD_DEVINFO(un), 0); 9665 ASSERT(return_status == DDI_SUCCESS); 9666 9667 mutex_enter(&un->un_pm_mutex); 9668 9669 if (un->un_pm_count < 0) { 9670 mutex_exit(&un->un_pm_mutex); 9671 9672 SD_TRACE(SD_LOG_IO_PM, un, 9673 "sd_pm_entry: power up component\n"); 9674 9675 /* 9676 * pm_raise_power will cause sdpower to be called 9677 * which brings the device power level to the 9678 * desired state, ON in this case. If successful, 9679 * un_pm_count and un_power_level will be updated 9680 * appropriately. 9681 */ 9682 return_status = pm_raise_power(SD_DEVINFO(un), 0, 9683 SD_SPINDLE_ON); 9684 9685 mutex_enter(&un->un_pm_mutex); 9686 9687 if (return_status != DDI_SUCCESS) { 9688 /* 9689 * Power up failed. 9690 * Idle the device and adjust the count 9691 * so the result on exit is that we're 9692 * still powered down, ie. count is less than 0. 9693 */ 9694 SD_TRACE(SD_LOG_IO_PM, un, 9695 "sd_pm_entry: power up failed," 9696 " idle the component\n"); 9697 9698 (void) pm_idle_component(SD_DEVINFO(un), 0); 9699 un->un_pm_count--; 9700 } else { 9701 /* 9702 * Device is powered up, verify the 9703 * count is non-negative. 9704 * This is debug only. 9705 */ 9706 ASSERT(un->un_pm_count == 0); 9707 } 9708 } 9709 9710 if (return_status == DDI_SUCCESS) { 9711 /* 9712 * For performance, now that the device has been tagged 9713 * as busy, and it's known to be powered up, update the 9714 * chain types to use jump tables that do not include 9715 * pm. This significantly lowers the overhead and 9716 * therefore improves performance. 9717 */ 9718 9719 mutex_exit(&un->un_pm_mutex); 9720 mutex_enter(SD_MUTEX(un)); 9721 SD_TRACE(SD_LOG_IO_PM, un, 9722 "sd_pm_entry: changing uscsi_chain_type from %d\n", 9723 un->un_uscsi_chain_type); 9724 9725 if (ISREMOVABLE(un)) { 9726 un->un_buf_chain_type = 9727 SD_CHAIN_INFO_RMMEDIA_NO_PM; 9728 } else { 9729 un->un_buf_chain_type = 9730 SD_CHAIN_INFO_DISK_NO_PM; 9731 } 9732 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 9733 9734 SD_TRACE(SD_LOG_IO_PM, un, 9735 " changed uscsi_chain_type to %d\n", 9736 un->un_uscsi_chain_type); 9737 mutex_exit(SD_MUTEX(un)); 9738 mutex_enter(&un->un_pm_mutex); 9739 9740 if (un->un_pm_idle_timeid == NULL) { 9741 /* 300 ms. */ 9742 un->un_pm_idle_timeid = 9743 timeout(sd_pm_idletimeout_handler, un, 9744 (drv_usectohz((clock_t)300000))); 9745 /* 9746 * Include an extra call to busy which keeps the 9747 * device busy with-respect-to the PM layer 9748 * until the timer fires, at which time it'll 9749 * get the extra idle call. 9750 */ 9751 (void) pm_busy_component(SD_DEVINFO(un), 0); 9752 } 9753 } 9754 } 9755 un->un_pm_busy = FALSE; 9756 /* Next... */ 9757 cv_signal(&un->un_pm_busy_cv); 9758 9759 un->un_pm_count++; 9760 9761 SD_TRACE(SD_LOG_IO_PM, un, 9762 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 9763 9764 mutex_exit(&un->un_pm_mutex); 9765 9766 return (return_status); 9767 } 9768 9769 9770 /* 9771 * Function: sd_pm_exit 9772 * 9773 * Description: Called at the completion of a command to manage busy 9774 * status for the device. If the device becomes idle the 9775 * PM framework is notified. 9776 * 9777 * Context: Kernel thread context 9778 */ 9779 9780 static void 9781 sd_pm_exit(struct sd_lun *un) 9782 { 9783 ASSERT(!mutex_owned(SD_MUTEX(un))); 9784 ASSERT(!mutex_owned(&un->un_pm_mutex)); 9785 9786 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 9787 9788 /* 9789 * After attach the following flag is only read, so don't 9790 * take the penalty of acquiring a mutex for it. 9791 */ 9792 if (un->un_f_pm_is_enabled == TRUE) { 9793 9794 mutex_enter(&un->un_pm_mutex); 9795 un->un_pm_count--; 9796 9797 SD_TRACE(SD_LOG_IO_PM, un, 9798 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 9799 9800 ASSERT(un->un_pm_count >= 0); 9801 if (un->un_pm_count == 0) { 9802 mutex_exit(&un->un_pm_mutex); 9803 9804 SD_TRACE(SD_LOG_IO_PM, un, 9805 "sd_pm_exit: idle component\n"); 9806 9807 (void) pm_idle_component(SD_DEVINFO(un), 0); 9808 9809 } else { 9810 mutex_exit(&un->un_pm_mutex); 9811 } 9812 } 9813 9814 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 9815 } 9816 9817 9818 /* 9819 * Function: sdopen 9820 * 9821 * Description: Driver's open(9e) entry point function. 9822 * 9823 * Arguments: dev_i - pointer to device number 9824 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 9825 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 9826 * cred_p - user credential pointer 9827 * 9828 * Return Code: EINVAL 9829 * ENXIO 9830 * EIO 9831 * EROFS 9832 * EBUSY 9833 * 9834 * Context: Kernel thread context 9835 */ 9836 /* ARGSUSED */ 9837 static int 9838 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 9839 { 9840 struct sd_lun *un; 9841 int nodelay; 9842 int part; 9843 int partmask; 9844 int instance; 9845 dev_t dev; 9846 int rval = EIO; 9847 9848 /* Validate the open type */ 9849 if (otyp >= OTYPCNT) { 9850 return (EINVAL); 9851 } 9852 9853 dev = *dev_p; 9854 instance = SDUNIT(dev); 9855 mutex_enter(&sd_detach_mutex); 9856 9857 /* 9858 * Fail the open if there is no softstate for the instance, or 9859 * if another thread somewhere is trying to detach the instance. 9860 */ 9861 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 9862 (un->un_detach_count != 0)) { 9863 mutex_exit(&sd_detach_mutex); 9864 /* 9865 * The probe cache only needs to be cleared when open (9e) fails 9866 * with ENXIO (4238046). 9867 */ 9868 /* 9869 * un-conditionally clearing probe cache is ok with 9870 * separate sd/ssd binaries 9871 * x86 platform can be an issue with both parallel 9872 * and fibre in 1 binary 9873 */ 9874 sd_scsi_clear_probe_cache(); 9875 return (ENXIO); 9876 } 9877 9878 /* 9879 * The un_layer_count is to prevent another thread in specfs from 9880 * trying to detach the instance, which can happen when we are 9881 * called from a higher-layer driver instead of thru specfs. 9882 * This will not be needed when DDI provides a layered driver 9883 * interface that allows specfs to know that an instance is in 9884 * use by a layered driver & should not be detached. 9885 * 9886 * Note: the semantics for layered driver opens are exactly one 9887 * close for every open. 9888 */ 9889 if (otyp == OTYP_LYR) { 9890 un->un_layer_count++; 9891 } 9892 9893 /* 9894 * Keep a count of the current # of opens in progress. This is because 9895 * some layered drivers try to call us as a regular open. This can 9896 * cause problems that we cannot prevent, however by keeping this count 9897 * we can at least keep our open and detach routines from racing against 9898 * each other under such conditions. 9899 */ 9900 un->un_opens_in_progress++; 9901 mutex_exit(&sd_detach_mutex); 9902 9903 nodelay = (flag & (FNDELAY | FNONBLOCK)); 9904 part = SDPART(dev); 9905 partmask = 1 << part; 9906 9907 /* 9908 * We use a semaphore here in order to serialize 9909 * open and close requests on the device. 9910 */ 9911 sema_p(&un->un_semoclose); 9912 9913 mutex_enter(SD_MUTEX(un)); 9914 9915 /* 9916 * All device accesses go thru sdstrategy() where we check 9917 * on suspend status but there could be a scsi_poll command, 9918 * which bypasses sdstrategy(), so we need to check pm 9919 * status. 9920 */ 9921 9922 if (!nodelay) { 9923 while ((un->un_state == SD_STATE_SUSPENDED) || 9924 (un->un_state == SD_STATE_PM_CHANGING)) { 9925 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9926 } 9927 9928 mutex_exit(SD_MUTEX(un)); 9929 if (sd_pm_entry(un) != DDI_SUCCESS) { 9930 rval = EIO; 9931 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 9932 "sdopen: sd_pm_entry failed\n"); 9933 goto open_failed_with_pm; 9934 } 9935 mutex_enter(SD_MUTEX(un)); 9936 } 9937 9938 /* check for previous exclusive open */ 9939 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 9940 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 9941 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 9942 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 9943 9944 if (un->un_exclopen & (partmask)) { 9945 goto excl_open_fail; 9946 } 9947 9948 if (flag & FEXCL) { 9949 int i; 9950 if (un->un_ocmap.lyropen[part]) { 9951 goto excl_open_fail; 9952 } 9953 for (i = 0; i < (OTYPCNT - 1); i++) { 9954 if (un->un_ocmap.regopen[i] & (partmask)) { 9955 goto excl_open_fail; 9956 } 9957 } 9958 } 9959 9960 /* 9961 * Check the write permission if this is a removable media device, 9962 * NDELAY has not been set, and writable permission is requested. 9963 * 9964 * Note: If NDELAY was set and this is write-protected media the WRITE 9965 * attempt will fail with EIO as part of the I/O processing. This is a 9966 * more permissive implementation that allows the open to succeed and 9967 * WRITE attempts to fail when appropriate. 9968 */ 9969 if (ISREMOVABLE(un)) { 9970 if ((flag & FWRITE) && (!nodelay)) { 9971 mutex_exit(SD_MUTEX(un)); 9972 /* 9973 * Defer the check for write permission on writable 9974 * DVD drive till sdstrategy and will not fail open even 9975 * if FWRITE is set as the device can be writable 9976 * depending upon the media and the media can change 9977 * after the call to open(). 9978 */ 9979 if (un->un_f_dvdram_writable_device == FALSE) { 9980 if (ISCD(un) || sr_check_wp(dev)) { 9981 rval = EROFS; 9982 mutex_enter(SD_MUTEX(un)); 9983 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 9984 "write to cd or write protected media\n"); 9985 goto open_fail; 9986 } 9987 } 9988 mutex_enter(SD_MUTEX(un)); 9989 } 9990 } 9991 9992 /* 9993 * If opening in NDELAY/NONBLOCK mode, just return. 9994 * Check if disk is ready and has a valid geometry later. 9995 */ 9996 if (!nodelay) { 9997 mutex_exit(SD_MUTEX(un)); 9998 rval = sd_ready_and_valid(un); 9999 mutex_enter(SD_MUTEX(un)); 10000 /* 10001 * Fail if device is not ready or if the number of disk 10002 * blocks is zero or negative for non CD devices. 10003 */ 10004 if ((rval != SD_READY_VALID) || 10005 (!ISCD(un) && un->un_map[part].dkl_nblk <= 0)) { 10006 if (ISREMOVABLE(un)) { 10007 rval = ENXIO; 10008 } else { 10009 rval = EIO; 10010 } 10011 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10012 "device not ready or invalid disk block value\n"); 10013 goto open_fail; 10014 } 10015 #if defined(__i386) || defined(__amd64) 10016 } else { 10017 uchar_t *cp; 10018 /* 10019 * x86 requires special nodelay handling, so that p0 is 10020 * always defined and accessible. 10021 * Invalidate geometry only if device is not already open. 10022 */ 10023 cp = &un->un_ocmap.chkd[0]; 10024 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10025 if (*cp != (uchar_t)0) { 10026 break; 10027 } 10028 cp++; 10029 } 10030 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10031 un->un_f_geometry_is_valid = FALSE; 10032 } 10033 10034 #endif 10035 } 10036 10037 if (otyp == OTYP_LYR) { 10038 un->un_ocmap.lyropen[part]++; 10039 } else { 10040 un->un_ocmap.regopen[otyp] |= partmask; 10041 } 10042 10043 /* Set up open and exclusive open flags */ 10044 if (flag & FEXCL) { 10045 un->un_exclopen |= (partmask); 10046 } 10047 10048 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10049 "open of part %d type %d\n", part, otyp); 10050 10051 mutex_exit(SD_MUTEX(un)); 10052 if (!nodelay) { 10053 sd_pm_exit(un); 10054 } 10055 10056 sema_v(&un->un_semoclose); 10057 10058 mutex_enter(&sd_detach_mutex); 10059 un->un_opens_in_progress--; 10060 mutex_exit(&sd_detach_mutex); 10061 10062 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 10063 return (DDI_SUCCESS); 10064 10065 excl_open_fail: 10066 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 10067 rval = EBUSY; 10068 10069 open_fail: 10070 mutex_exit(SD_MUTEX(un)); 10071 10072 /* 10073 * On a failed open we must exit the pm management. 10074 */ 10075 if (!nodelay) { 10076 sd_pm_exit(un); 10077 } 10078 open_failed_with_pm: 10079 sema_v(&un->un_semoclose); 10080 10081 mutex_enter(&sd_detach_mutex); 10082 un->un_opens_in_progress--; 10083 if (otyp == OTYP_LYR) { 10084 un->un_layer_count--; 10085 } 10086 mutex_exit(&sd_detach_mutex); 10087 10088 return (rval); 10089 } 10090 10091 10092 /* 10093 * Function: sdclose 10094 * 10095 * Description: Driver's close(9e) entry point function. 10096 * 10097 * Arguments: dev - device number 10098 * flag - file status flag, informational only 10099 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10100 * cred_p - user credential pointer 10101 * 10102 * Return Code: ENXIO 10103 * 10104 * Context: Kernel thread context 10105 */ 10106 /* ARGSUSED */ 10107 static int 10108 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 10109 { 10110 struct sd_lun *un; 10111 uchar_t *cp; 10112 int part; 10113 int nodelay; 10114 int rval = 0; 10115 10116 /* Validate the open type */ 10117 if (otyp >= OTYPCNT) { 10118 return (ENXIO); 10119 } 10120 10121 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10122 return (ENXIO); 10123 } 10124 10125 part = SDPART(dev); 10126 nodelay = flag & (FNDELAY | FNONBLOCK); 10127 10128 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10129 "sdclose: close of part %d type %d\n", part, otyp); 10130 10131 /* 10132 * We use a semaphore here in order to serialize 10133 * open and close requests on the device. 10134 */ 10135 sema_p(&un->un_semoclose); 10136 10137 mutex_enter(SD_MUTEX(un)); 10138 10139 /* Don't proceed if power is being changed. */ 10140 while (un->un_state == SD_STATE_PM_CHANGING) { 10141 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10142 } 10143 10144 if (un->un_exclopen & (1 << part)) { 10145 un->un_exclopen &= ~(1 << part); 10146 } 10147 10148 /* Update the open partition map */ 10149 if (otyp == OTYP_LYR) { 10150 un->un_ocmap.lyropen[part] -= 1; 10151 } else { 10152 un->un_ocmap.regopen[otyp] &= ~(1 << part); 10153 } 10154 10155 cp = &un->un_ocmap.chkd[0]; 10156 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10157 if (*cp != NULL) { 10158 break; 10159 } 10160 cp++; 10161 } 10162 10163 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10164 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 10165 10166 /* 10167 * We avoid persistance upon the last close, and set 10168 * the throttle back to the maximum. 10169 */ 10170 un->un_throttle = un->un_saved_throttle; 10171 10172 if (un->un_state == SD_STATE_OFFLINE) { 10173 if (un->un_f_is_fibre == FALSE) { 10174 scsi_log(SD_DEVINFO(un), sd_label, 10175 CE_WARN, "offline\n"); 10176 } 10177 un->un_f_geometry_is_valid = FALSE; 10178 10179 } else { 10180 /* 10181 * Flush any outstanding writes in NVRAM cache. 10182 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 10183 * cmd, it may not work for non-Pluto devices. 10184 * SYNCHRONIZE CACHE is not required for removables, 10185 * except DVD-RAM drives. 10186 * 10187 * Also note: because SYNCHRONIZE CACHE is currently 10188 * the only command issued here that requires the 10189 * drive be powered up, only do the power up before 10190 * sending the Sync Cache command. If additional 10191 * commands are added which require a powered up 10192 * drive, the following sequence may have to change. 10193 * 10194 * And finally, note that parallel SCSI on SPARC 10195 * only issues a Sync Cache to DVD-RAM, a newly 10196 * supported device. 10197 */ 10198 #if defined(__i386) || defined(__amd64) 10199 if (!ISREMOVABLE(un) || 10200 un->un_f_dvdram_writable_device == TRUE) { 10201 #else 10202 if (un->un_f_dvdram_writable_device == TRUE) { 10203 #endif 10204 mutex_exit(SD_MUTEX(un)); 10205 if (sd_pm_entry(un) == DDI_SUCCESS) { 10206 if (sd_send_scsi_SYNCHRONIZE_CACHE(un) 10207 != 0) { 10208 rval = EIO; 10209 } 10210 sd_pm_exit(un); 10211 } else { 10212 rval = EIO; 10213 } 10214 mutex_enter(SD_MUTEX(un)); 10215 } 10216 10217 /* 10218 * For removable media devices, send an ALLOW MEDIA 10219 * REMOVAL command, but don't get upset if it fails. 10220 * Also invalidate the geometry. We need to raise 10221 * the power of the drive before we can call 10222 * sd_send_scsi_DOORLOCK() 10223 */ 10224 if (ISREMOVABLE(un)) { 10225 mutex_exit(SD_MUTEX(un)); 10226 if (sd_pm_entry(un) == DDI_SUCCESS) { 10227 rval = sd_send_scsi_DOORLOCK(un, 10228 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 10229 10230 sd_pm_exit(un); 10231 if (ISCD(un) && (rval != 0) && 10232 (nodelay != 0)) { 10233 rval = ENXIO; 10234 } 10235 } else { 10236 rval = EIO; 10237 } 10238 mutex_enter(SD_MUTEX(un)); 10239 10240 sr_ejected(un); 10241 /* 10242 * Destroy the cache (if it exists) which was 10243 * allocated for the write maps since this is 10244 * the last close for this media. 10245 */ 10246 if (un->un_wm_cache) { 10247 /* 10248 * Check if there are pending commands. 10249 * and if there are give a warning and 10250 * do not destroy the cache. 10251 */ 10252 if (un->un_ncmds_in_driver > 0) { 10253 scsi_log(SD_DEVINFO(un), 10254 sd_label, CE_WARN, 10255 "Unable to clean up memory " 10256 "because of pending I/O\n"); 10257 } else { 10258 kmem_cache_destroy( 10259 un->un_wm_cache); 10260 un->un_wm_cache = NULL; 10261 } 10262 } 10263 } 10264 } 10265 } 10266 10267 mutex_exit(SD_MUTEX(un)); 10268 sema_v(&un->un_semoclose); 10269 10270 if (otyp == OTYP_LYR) { 10271 mutex_enter(&sd_detach_mutex); 10272 /* 10273 * The detach routine may run when the layer count 10274 * drops to zero. 10275 */ 10276 un->un_layer_count--; 10277 mutex_exit(&sd_detach_mutex); 10278 } 10279 10280 return (rval); 10281 } 10282 10283 10284 /* 10285 * Function: sd_ready_and_valid 10286 * 10287 * Description: Test if device is ready and has a valid geometry. 10288 * 10289 * Arguments: dev - device number 10290 * un - driver soft state (unit) structure 10291 * 10292 * Return Code: SD_READY_VALID ready and valid label 10293 * SD_READY_NOT_VALID ready, geom ops never applicable 10294 * SD_NOT_READY_VALID not ready, no label 10295 * 10296 * Context: Never called at interrupt context. 10297 */ 10298 10299 static int 10300 sd_ready_and_valid(struct sd_lun *un) 10301 { 10302 struct sd_errstats *stp; 10303 uint64_t capacity; 10304 uint_t lbasize; 10305 int rval = SD_READY_VALID; 10306 char name_str[48]; 10307 10308 ASSERT(un != NULL); 10309 ASSERT(!mutex_owned(SD_MUTEX(un))); 10310 10311 mutex_enter(SD_MUTEX(un)); 10312 if (ISREMOVABLE(un)) { 10313 mutex_exit(SD_MUTEX(un)); 10314 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 10315 rval = SD_NOT_READY_VALID; 10316 mutex_enter(SD_MUTEX(un)); 10317 goto done; 10318 } 10319 10320 mutex_enter(SD_MUTEX(un)); 10321 if ((un->un_f_geometry_is_valid == FALSE) || 10322 (un->un_f_blockcount_is_valid == FALSE) || 10323 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 10324 10325 /* capacity has to be read every open. */ 10326 mutex_exit(SD_MUTEX(un)); 10327 if (sd_send_scsi_READ_CAPACITY(un, &capacity, 10328 &lbasize, SD_PATH_DIRECT) != 0) { 10329 mutex_enter(SD_MUTEX(un)); 10330 un->un_f_geometry_is_valid = FALSE; 10331 rval = SD_NOT_READY_VALID; 10332 goto done; 10333 } else { 10334 mutex_enter(SD_MUTEX(un)); 10335 sd_update_block_info(un, lbasize, capacity); 10336 } 10337 } 10338 10339 /* 10340 * If this is a non 512 block device, allocate space for 10341 * the wmap cache. This is being done here since every time 10342 * a media is changed this routine will be called and the 10343 * block size is a function of media rather than device. 10344 */ 10345 if (NOT_DEVBSIZE(un)) { 10346 if (!(un->un_wm_cache)) { 10347 (void) snprintf(name_str, sizeof (name_str), 10348 "%s%d_cache", 10349 ddi_driver_name(SD_DEVINFO(un)), 10350 ddi_get_instance(SD_DEVINFO(un))); 10351 un->un_wm_cache = kmem_cache_create( 10352 name_str, sizeof (struct sd_w_map), 10353 8, sd_wm_cache_constructor, 10354 sd_wm_cache_destructor, NULL, 10355 (void *)un, NULL, 0); 10356 if (!(un->un_wm_cache)) { 10357 rval = ENOMEM; 10358 goto done; 10359 } 10360 } 10361 } 10362 10363 /* 10364 * Check if the media in the device is writable or not. 10365 */ 10366 if ((un->un_f_geometry_is_valid == FALSE) && ISCD(un)) { 10367 sd_check_for_writable_cd(un); 10368 } 10369 10370 } else { 10371 /* 10372 * Do a test unit ready to clear any unit attention from non-cd 10373 * devices. 10374 */ 10375 mutex_exit(SD_MUTEX(un)); 10376 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 10377 mutex_enter(SD_MUTEX(un)); 10378 } 10379 10380 10381 if (un->un_state == SD_STATE_NORMAL) { 10382 /* 10383 * If the target is not yet ready here (defined by a TUR 10384 * failure), invalidate the geometry and print an 'offline' 10385 * message. This is a legacy message, as the state of the 10386 * target is not actually changed to SD_STATE_OFFLINE. 10387 * 10388 * If the TUR fails for EACCES (Reservation Conflict), it 10389 * means there actually is nothing wrong with the target that 10390 * would require invalidating the geometry, so continue in 10391 * that case as if the TUR was successful. 10392 */ 10393 int err; 10394 10395 mutex_exit(SD_MUTEX(un)); 10396 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 10397 mutex_enter(SD_MUTEX(un)); 10398 10399 if ((err != 0) && (err != EACCES)) { 10400 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10401 "offline\n"); 10402 un->un_f_geometry_is_valid = FALSE; 10403 rval = SD_NOT_READY_VALID; 10404 goto done; 10405 } 10406 } 10407 10408 if (un->un_f_format_in_progress == FALSE) { 10409 /* 10410 * Note: sd_validate_geometry may return TRUE, but that does 10411 * not necessarily mean un_f_geometry_is_valid == TRUE! 10412 */ 10413 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 10414 if (rval == ENOTSUP) { 10415 if (un->un_f_geometry_is_valid == TRUE) 10416 rval = 0; 10417 else { 10418 rval = SD_READY_NOT_VALID; 10419 goto done; 10420 } 10421 } 10422 if (rval != 0) { 10423 /* 10424 * We don't check the validity of geometry for 10425 * CDROMs. Also we assume we have a good label 10426 * even if sd_validate_geometry returned ENOMEM. 10427 */ 10428 if (!ISCD(un) && rval != ENOMEM) { 10429 rval = SD_NOT_READY_VALID; 10430 goto done; 10431 } 10432 } 10433 } 10434 10435 #ifdef DOESNTWORK /* on eliteII, see 1118607 */ 10436 /* 10437 * check to see if this disk is write protected, if it is and we have 10438 * not set read-only, then fail 10439 */ 10440 if ((flag & FWRITE) && (sr_check_wp(dev))) { 10441 New_state(un, SD_STATE_CLOSED); 10442 goto done; 10443 } 10444 #endif 10445 10446 /* 10447 * If this is a removable media device, try and send 10448 * a PREVENT MEDIA REMOVAL command, but don't get upset 10449 * if it fails. For a CD, however, it is an error 10450 */ 10451 if (ISREMOVABLE(un)) { 10452 mutex_exit(SD_MUTEX(un)); 10453 if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 10454 SD_PATH_DIRECT) != 0) && ISCD(un)) { 10455 rval = SD_NOT_READY_VALID; 10456 mutex_enter(SD_MUTEX(un)); 10457 goto done; 10458 } 10459 mutex_enter(SD_MUTEX(un)); 10460 } 10461 10462 /* The state has changed, inform the media watch routines */ 10463 un->un_mediastate = DKIO_INSERTED; 10464 cv_broadcast(&un->un_state_cv); 10465 rval = SD_READY_VALID; 10466 10467 done: 10468 10469 /* 10470 * Initialize the capacity kstat value, if no media previously 10471 * (capacity kstat is 0) and a media has been inserted 10472 * (un_blockcount > 0). 10473 * This is a more generic way then checking for ISREMOVABLE. 10474 */ 10475 if (un->un_errstats != NULL) { 10476 stp = (struct sd_errstats *)un->un_errstats->ks_data; 10477 if ((stp->sd_capacity.value.ui64 == 0) && 10478 (un->un_f_blockcount_is_valid == TRUE)) { 10479 stp->sd_capacity.value.ui64 = 10480 (uint64_t)((uint64_t)un->un_blockcount * 10481 un->un_sys_blocksize); 10482 } 10483 } 10484 10485 mutex_exit(SD_MUTEX(un)); 10486 return (rval); 10487 } 10488 10489 10490 /* 10491 * Function: sdmin 10492 * 10493 * Description: Routine to limit the size of a data transfer. Used in 10494 * conjunction with physio(9F). 10495 * 10496 * Arguments: bp - pointer to the indicated buf(9S) struct. 10497 * 10498 * Context: Kernel thread context. 10499 */ 10500 10501 static void 10502 sdmin(struct buf *bp) 10503 { 10504 struct sd_lun *un; 10505 int instance; 10506 10507 instance = SDUNIT(bp->b_edev); 10508 10509 un = ddi_get_soft_state(sd_state, instance); 10510 ASSERT(un != NULL); 10511 10512 if (bp->b_bcount > un->un_max_xfer_size) { 10513 bp->b_bcount = un->un_max_xfer_size; 10514 } 10515 } 10516 10517 10518 /* 10519 * Function: sdread 10520 * 10521 * Description: Driver's read(9e) entry point function. 10522 * 10523 * Arguments: dev - device number 10524 * uio - structure pointer describing where data is to be stored 10525 * in user's space 10526 * cred_p - user credential pointer 10527 * 10528 * Return Code: ENXIO 10529 * EIO 10530 * EINVAL 10531 * value returned by physio 10532 * 10533 * Context: Kernel thread context. 10534 */ 10535 /* ARGSUSED */ 10536 static int 10537 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 10538 { 10539 struct sd_lun *un = NULL; 10540 int secmask; 10541 int err; 10542 10543 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10544 return (ENXIO); 10545 } 10546 10547 ASSERT(!mutex_owned(SD_MUTEX(un))); 10548 10549 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 10550 mutex_enter(SD_MUTEX(un)); 10551 /* 10552 * Because the call to sd_ready_and_valid will issue I/O we 10553 * must wait here if either the device is suspended or 10554 * if it's power level is changing. 10555 */ 10556 while ((un->un_state == SD_STATE_SUSPENDED) || 10557 (un->un_state == SD_STATE_PM_CHANGING)) { 10558 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10559 } 10560 un->un_ncmds_in_driver++; 10561 mutex_exit(SD_MUTEX(un)); 10562 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 10563 mutex_enter(SD_MUTEX(un)); 10564 un->un_ncmds_in_driver--; 10565 ASSERT(un->un_ncmds_in_driver >= 0); 10566 mutex_exit(SD_MUTEX(un)); 10567 return (EIO); 10568 } 10569 mutex_enter(SD_MUTEX(un)); 10570 un->un_ncmds_in_driver--; 10571 ASSERT(un->un_ncmds_in_driver >= 0); 10572 mutex_exit(SD_MUTEX(un)); 10573 } 10574 10575 /* 10576 * Read requests are restricted to multiples of the system block size. 10577 */ 10578 secmask = un->un_sys_blocksize - 1; 10579 10580 if (uio->uio_loffset & ((offset_t)(secmask))) { 10581 SD_ERROR(SD_LOG_READ_WRITE, un, 10582 "sdread: file offset not modulo %d\n", 10583 un->un_sys_blocksize); 10584 err = EINVAL; 10585 } else if (uio->uio_iov->iov_len & (secmask)) { 10586 SD_ERROR(SD_LOG_READ_WRITE, un, 10587 "sdread: transfer length not modulo %d\n", 10588 un->un_sys_blocksize); 10589 err = EINVAL; 10590 } else { 10591 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 10592 } 10593 return (err); 10594 } 10595 10596 10597 /* 10598 * Function: sdwrite 10599 * 10600 * Description: Driver's write(9e) entry point function. 10601 * 10602 * Arguments: dev - device number 10603 * uio - structure pointer describing where data is stored in 10604 * user's space 10605 * cred_p - user credential pointer 10606 * 10607 * Return Code: ENXIO 10608 * EIO 10609 * EINVAL 10610 * value returned by physio 10611 * 10612 * Context: Kernel thread context. 10613 */ 10614 /* ARGSUSED */ 10615 static int 10616 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 10617 { 10618 struct sd_lun *un = NULL; 10619 int secmask; 10620 int err; 10621 10622 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10623 return (ENXIO); 10624 } 10625 10626 ASSERT(!mutex_owned(SD_MUTEX(un))); 10627 10628 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 10629 mutex_enter(SD_MUTEX(un)); 10630 /* 10631 * Because the call to sd_ready_and_valid will issue I/O we 10632 * must wait here if either the device is suspended or 10633 * if it's power level is changing. 10634 */ 10635 while ((un->un_state == SD_STATE_SUSPENDED) || 10636 (un->un_state == SD_STATE_PM_CHANGING)) { 10637 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10638 } 10639 un->un_ncmds_in_driver++; 10640 mutex_exit(SD_MUTEX(un)); 10641 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 10642 mutex_enter(SD_MUTEX(un)); 10643 un->un_ncmds_in_driver--; 10644 ASSERT(un->un_ncmds_in_driver >= 0); 10645 mutex_exit(SD_MUTEX(un)); 10646 return (EIO); 10647 } 10648 mutex_enter(SD_MUTEX(un)); 10649 un->un_ncmds_in_driver--; 10650 ASSERT(un->un_ncmds_in_driver >= 0); 10651 mutex_exit(SD_MUTEX(un)); 10652 } 10653 10654 /* 10655 * Write requests are restricted to multiples of the system block size. 10656 */ 10657 secmask = un->un_sys_blocksize - 1; 10658 10659 if (uio->uio_loffset & ((offset_t)(secmask))) { 10660 SD_ERROR(SD_LOG_READ_WRITE, un, 10661 "sdwrite: file offset not modulo %d\n", 10662 un->un_sys_blocksize); 10663 err = EINVAL; 10664 } else if (uio->uio_iov->iov_len & (secmask)) { 10665 SD_ERROR(SD_LOG_READ_WRITE, un, 10666 "sdwrite: transfer length not modulo %d\n", 10667 un->un_sys_blocksize); 10668 err = EINVAL; 10669 } else { 10670 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 10671 } 10672 return (err); 10673 } 10674 10675 10676 /* 10677 * Function: sdaread 10678 * 10679 * Description: Driver's aread(9e) entry point function. 10680 * 10681 * Arguments: dev - device number 10682 * aio - structure pointer describing where data is to be stored 10683 * cred_p - user credential pointer 10684 * 10685 * Return Code: ENXIO 10686 * EIO 10687 * EINVAL 10688 * value returned by aphysio 10689 * 10690 * Context: Kernel thread context. 10691 */ 10692 /* ARGSUSED */ 10693 static int 10694 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 10695 { 10696 struct sd_lun *un = NULL; 10697 struct uio *uio = aio->aio_uio; 10698 int secmask; 10699 int err; 10700 10701 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10702 return (ENXIO); 10703 } 10704 10705 ASSERT(!mutex_owned(SD_MUTEX(un))); 10706 10707 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 10708 mutex_enter(SD_MUTEX(un)); 10709 /* 10710 * Because the call to sd_ready_and_valid will issue I/O we 10711 * must wait here if either the device is suspended or 10712 * if it's power level is changing. 10713 */ 10714 while ((un->un_state == SD_STATE_SUSPENDED) || 10715 (un->un_state == SD_STATE_PM_CHANGING)) { 10716 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10717 } 10718 un->un_ncmds_in_driver++; 10719 mutex_exit(SD_MUTEX(un)); 10720 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 10721 mutex_enter(SD_MUTEX(un)); 10722 un->un_ncmds_in_driver--; 10723 ASSERT(un->un_ncmds_in_driver >= 0); 10724 mutex_exit(SD_MUTEX(un)); 10725 return (EIO); 10726 } 10727 mutex_enter(SD_MUTEX(un)); 10728 un->un_ncmds_in_driver--; 10729 ASSERT(un->un_ncmds_in_driver >= 0); 10730 mutex_exit(SD_MUTEX(un)); 10731 } 10732 10733 /* 10734 * Read requests are restricted to multiples of the system block size. 10735 */ 10736 secmask = un->un_sys_blocksize - 1; 10737 10738 if (uio->uio_loffset & ((offset_t)(secmask))) { 10739 SD_ERROR(SD_LOG_READ_WRITE, un, 10740 "sdaread: file offset not modulo %d\n", 10741 un->un_sys_blocksize); 10742 err = EINVAL; 10743 } else if (uio->uio_iov->iov_len & (secmask)) { 10744 SD_ERROR(SD_LOG_READ_WRITE, un, 10745 "sdaread: transfer length not modulo %d\n", 10746 un->un_sys_blocksize); 10747 err = EINVAL; 10748 } else { 10749 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 10750 } 10751 return (err); 10752 } 10753 10754 10755 /* 10756 * Function: sdawrite 10757 * 10758 * Description: Driver's awrite(9e) entry point function. 10759 * 10760 * Arguments: dev - device number 10761 * aio - structure pointer describing where data is stored 10762 * cred_p - user credential pointer 10763 * 10764 * Return Code: ENXIO 10765 * EIO 10766 * EINVAL 10767 * value returned by aphysio 10768 * 10769 * Context: Kernel thread context. 10770 */ 10771 /* ARGSUSED */ 10772 static int 10773 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 10774 { 10775 struct sd_lun *un = NULL; 10776 struct uio *uio = aio->aio_uio; 10777 int secmask; 10778 int err; 10779 10780 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10781 return (ENXIO); 10782 } 10783 10784 ASSERT(!mutex_owned(SD_MUTEX(un))); 10785 10786 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 10787 mutex_enter(SD_MUTEX(un)); 10788 /* 10789 * Because the call to sd_ready_and_valid will issue I/O we 10790 * must wait here if either the device is suspended or 10791 * if it's power level is changing. 10792 */ 10793 while ((un->un_state == SD_STATE_SUSPENDED) || 10794 (un->un_state == SD_STATE_PM_CHANGING)) { 10795 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10796 } 10797 un->un_ncmds_in_driver++; 10798 mutex_exit(SD_MUTEX(un)); 10799 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 10800 mutex_enter(SD_MUTEX(un)); 10801 un->un_ncmds_in_driver--; 10802 ASSERT(un->un_ncmds_in_driver >= 0); 10803 mutex_exit(SD_MUTEX(un)); 10804 return (EIO); 10805 } 10806 mutex_enter(SD_MUTEX(un)); 10807 un->un_ncmds_in_driver--; 10808 ASSERT(un->un_ncmds_in_driver >= 0); 10809 mutex_exit(SD_MUTEX(un)); 10810 } 10811 10812 /* 10813 * Write requests are restricted to multiples of the system block size. 10814 */ 10815 secmask = un->un_sys_blocksize - 1; 10816 10817 if (uio->uio_loffset & ((offset_t)(secmask))) { 10818 SD_ERROR(SD_LOG_READ_WRITE, un, 10819 "sdawrite: file offset not modulo %d\n", 10820 un->un_sys_blocksize); 10821 err = EINVAL; 10822 } else if (uio->uio_iov->iov_len & (secmask)) { 10823 SD_ERROR(SD_LOG_READ_WRITE, un, 10824 "sdawrite: transfer length not modulo %d\n", 10825 un->un_sys_blocksize); 10826 err = EINVAL; 10827 } else { 10828 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 10829 } 10830 return (err); 10831 } 10832 10833 10834 10835 10836 10837 /* 10838 * Driver IO processing follows the following sequence: 10839 * 10840 * sdioctl(9E) sdstrategy(9E) biodone(9F) 10841 * | | ^ 10842 * v v | 10843 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 10844 * | | | | 10845 * v | | | 10846 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 10847 * | | ^ ^ 10848 * v v | | 10849 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 10850 * | | | | 10851 * +---+ | +------------+ +-------+ 10852 * | | | | 10853 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 10854 * | v | | 10855 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 10856 * | | ^ | 10857 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 10858 * | v | | 10859 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 10860 * | | ^ | 10861 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 10862 * | v | | 10863 * | sd_checksum_iostart() sd_checksum_iodone() | 10864 * | | ^ | 10865 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 10866 * | v | | 10867 * | sd_pm_iostart() sd_pm_iodone() | 10868 * | | ^ | 10869 * | | | | 10870 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 10871 * | ^ 10872 * v | 10873 * sd_core_iostart() | 10874 * | | 10875 * | +------>(*destroypkt)() 10876 * +-> sd_start_cmds() <-+ | | 10877 * | | | v 10878 * | | | scsi_destroy_pkt(9F) 10879 * | | | 10880 * +->(*initpkt)() +- sdintr() 10881 * | | | | 10882 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 10883 * | +-> scsi_setup_cdb(9F) | 10884 * | | 10885 * +--> scsi_transport(9F) | 10886 * | | 10887 * +----> SCSA ---->+ 10888 * 10889 * 10890 * This code is based upon the following presumtions: 10891 * 10892 * - iostart and iodone functions operate on buf(9S) structures. These 10893 * functions perform the necessary operations on the buf(9S) and pass 10894 * them along to the next function in the chain by using the macros 10895 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 10896 * (for iodone side functions). 10897 * 10898 * - The iostart side functions may sleep. The iodone side functions 10899 * are called under interrupt context and may NOT sleep. Therefore 10900 * iodone side functions also may not call iostart side functions. 10901 * (NOTE: iostart side functions should NOT sleep for memory, as 10902 * this could result in deadlock.) 10903 * 10904 * - An iostart side function may call its corresponding iodone side 10905 * function directly (if necessary). 10906 * 10907 * - In the event of an error, an iostart side function can return a buf(9S) 10908 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 10909 * b_error in the usual way of course). 10910 * 10911 * - The taskq mechanism may be used by the iodone side functions to dispatch 10912 * requests to the iostart side functions. The iostart side functions in 10913 * this case would be called under the context of a taskq thread, so it's 10914 * OK for them to block/sleep/spin in this case. 10915 * 10916 * - iostart side functions may allocate "shadow" buf(9S) structs and 10917 * pass them along to the next function in the chain. The corresponding 10918 * iodone side functions must coalesce the "shadow" bufs and return 10919 * the "original" buf to the next higher layer. 10920 * 10921 * - The b_private field of the buf(9S) struct holds a pointer to 10922 * an sd_xbuf struct, which contains information needed to 10923 * construct the scsi_pkt for the command. 10924 * 10925 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 10926 * layer must acquire & release the SD_MUTEX(un) as needed. 10927 */ 10928 10929 10930 /* 10931 * Create taskq for all targets in the system. This is created at 10932 * _init(9E) and destroyed at _fini(9E). 10933 * 10934 * Note: here we set the minalloc to a reasonably high number to ensure that 10935 * we will have an adequate supply of task entries available at interrupt time. 10936 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 10937 * sd_create_taskq(). Since we do not want to sleep for allocations at 10938 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 10939 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 10940 * requests any one instant in time. 10941 */ 10942 #define SD_TASKQ_NUMTHREADS 8 10943 #define SD_TASKQ_MINALLOC 256 10944 #define SD_TASKQ_MAXALLOC 256 10945 10946 static taskq_t *sd_tq = NULL; 10947 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 10948 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 10949 10950 /* 10951 * The following task queue is being created for the write part of 10952 * read-modify-write of non-512 block size devices. 10953 * Limit the number of threads to 1 for now. This number has been choosen 10954 * considering the fact that it applies only to dvd ram drives/MO drives 10955 * currently. Performance for which is not main criteria at this stage. 10956 * Note: It needs to be explored if we can use a single taskq in future 10957 */ 10958 #define SD_WMR_TASKQ_NUMTHREADS 1 10959 static taskq_t *sd_wmr_tq = NULL; 10960 10961 /* 10962 * Function: sd_taskq_create 10963 * 10964 * Description: Create taskq thread(s) and preallocate task entries 10965 * 10966 * Return Code: Returns a pointer to the allocated taskq_t. 10967 * 10968 * Context: Can sleep. Requires blockable context. 10969 * 10970 * Notes: - The taskq() facility currently is NOT part of the DDI. 10971 * (definitely NOT recommeded for 3rd-party drivers!) :-) 10972 * - taskq_create() will block for memory, also it will panic 10973 * if it cannot create the requested number of threads. 10974 * - Currently taskq_create() creates threads that cannot be 10975 * swapped. 10976 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 10977 * supply of taskq entries at interrupt time (ie, so that we 10978 * do not have to sleep for memory) 10979 */ 10980 10981 static void 10982 sd_taskq_create(void) 10983 { 10984 char taskq_name[TASKQ_NAMELEN]; 10985 10986 ASSERT(sd_tq == NULL); 10987 ASSERT(sd_wmr_tq == NULL); 10988 10989 (void) snprintf(taskq_name, sizeof (taskq_name), 10990 "%s_drv_taskq", sd_label); 10991 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 10992 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 10993 TASKQ_PREPOPULATE)); 10994 10995 (void) snprintf(taskq_name, sizeof (taskq_name), 10996 "%s_rmw_taskq", sd_label); 10997 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 10998 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 10999 TASKQ_PREPOPULATE)); 11000 } 11001 11002 11003 /* 11004 * Function: sd_taskq_delete 11005 * 11006 * Description: Complementary cleanup routine for sd_taskq_create(). 11007 * 11008 * Context: Kernel thread context. 11009 */ 11010 11011 static void 11012 sd_taskq_delete(void) 11013 { 11014 ASSERT(sd_tq != NULL); 11015 ASSERT(sd_wmr_tq != NULL); 11016 taskq_destroy(sd_tq); 11017 taskq_destroy(sd_wmr_tq); 11018 sd_tq = NULL; 11019 sd_wmr_tq = NULL; 11020 } 11021 11022 11023 /* 11024 * Function: sdstrategy 11025 * 11026 * Description: Driver's strategy (9E) entry point function. 11027 * 11028 * Arguments: bp - pointer to buf(9S) 11029 * 11030 * Return Code: Always returns zero 11031 * 11032 * Context: Kernel thread context. 11033 */ 11034 11035 static int 11036 sdstrategy(struct buf *bp) 11037 { 11038 struct sd_lun *un; 11039 11040 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11041 if (un == NULL) { 11042 bioerror(bp, EIO); 11043 bp->b_resid = bp->b_bcount; 11044 biodone(bp); 11045 return (0); 11046 } 11047 /* As was done in the past, fail new cmds. if state is dumping. */ 11048 if (un->un_state == SD_STATE_DUMPING) { 11049 bioerror(bp, ENXIO); 11050 bp->b_resid = bp->b_bcount; 11051 biodone(bp); 11052 return (0); 11053 } 11054 11055 ASSERT(!mutex_owned(SD_MUTEX(un))); 11056 11057 /* 11058 * Commands may sneak in while we released the mutex in 11059 * DDI_SUSPEND, we should block new commands. However, old 11060 * commands that are still in the driver at this point should 11061 * still be allowed to drain. 11062 */ 11063 mutex_enter(SD_MUTEX(un)); 11064 /* 11065 * Must wait here if either the device is suspended or 11066 * if it's power level is changing. 11067 */ 11068 while ((un->un_state == SD_STATE_SUSPENDED) || 11069 (un->un_state == SD_STATE_PM_CHANGING)) { 11070 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11071 } 11072 11073 un->un_ncmds_in_driver++; 11074 11075 /* 11076 * atapi: Since we are running the CD for now in PIO mode we need to 11077 * call bp_mapin here to avoid bp_mapin called interrupt context under 11078 * the HBA's init_pkt routine. 11079 */ 11080 if (un->un_f_cfg_is_atapi == TRUE) { 11081 mutex_exit(SD_MUTEX(un)); 11082 bp_mapin(bp); 11083 mutex_enter(SD_MUTEX(un)); 11084 } 11085 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 11086 un->un_ncmds_in_driver); 11087 11088 mutex_exit(SD_MUTEX(un)); 11089 11090 /* 11091 * This will (eventually) allocate the sd_xbuf area and 11092 * call sd_xbuf_strategy(). We just want to return the 11093 * result of ddi_xbuf_qstrategy so that we have an opt- 11094 * imized tail call which saves us a stack frame. 11095 */ 11096 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 11097 } 11098 11099 11100 /* 11101 * Function: sd_xbuf_strategy 11102 * 11103 * Description: Function for initiating IO operations via the 11104 * ddi_xbuf_qstrategy() mechanism. 11105 * 11106 * Context: Kernel thread context. 11107 */ 11108 11109 static void 11110 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 11111 { 11112 struct sd_lun *un = arg; 11113 11114 ASSERT(bp != NULL); 11115 ASSERT(xp != NULL); 11116 ASSERT(un != NULL); 11117 ASSERT(!mutex_owned(SD_MUTEX(un))); 11118 11119 /* 11120 * Initialize the fields in the xbuf and save a pointer to the 11121 * xbuf in bp->b_private. 11122 */ 11123 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 11124 11125 /* Send the buf down the iostart chain */ 11126 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 11127 } 11128 11129 11130 /* 11131 * Function: sd_xbuf_init 11132 * 11133 * Description: Prepare the given sd_xbuf struct for use. 11134 * 11135 * Arguments: un - ptr to softstate 11136 * bp - ptr to associated buf(9S) 11137 * xp - ptr to associated sd_xbuf 11138 * chain_type - IO chain type to use: 11139 * SD_CHAIN_NULL 11140 * SD_CHAIN_BUFIO 11141 * SD_CHAIN_USCSI 11142 * SD_CHAIN_DIRECT 11143 * SD_CHAIN_DIRECT_PRIORITY 11144 * pktinfop - ptr to private data struct for scsi_pkt(9S) 11145 * initialization; may be NULL if none. 11146 * 11147 * Context: Kernel thread context 11148 */ 11149 11150 static void 11151 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 11152 uchar_t chain_type, void *pktinfop) 11153 { 11154 int index; 11155 11156 ASSERT(un != NULL); 11157 ASSERT(bp != NULL); 11158 ASSERT(xp != NULL); 11159 11160 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 11161 bp, chain_type); 11162 11163 xp->xb_un = un; 11164 xp->xb_pktp = NULL; 11165 xp->xb_pktinfo = pktinfop; 11166 xp->xb_private = bp->b_private; 11167 xp->xb_blkno = (daddr_t)bp->b_blkno; 11168 11169 /* 11170 * Set up the iostart and iodone chain indexes in the xbuf, based 11171 * upon the specified chain type to use. 11172 */ 11173 switch (chain_type) { 11174 case SD_CHAIN_NULL: 11175 /* 11176 * Fall thru to just use the values for the buf type, even 11177 * tho for the NULL chain these values will never be used. 11178 */ 11179 /* FALLTHRU */ 11180 case SD_CHAIN_BUFIO: 11181 index = un->un_buf_chain_type; 11182 break; 11183 case SD_CHAIN_USCSI: 11184 index = un->un_uscsi_chain_type; 11185 break; 11186 case SD_CHAIN_DIRECT: 11187 index = un->un_direct_chain_type; 11188 break; 11189 case SD_CHAIN_DIRECT_PRIORITY: 11190 index = un->un_priority_chain_type; 11191 break; 11192 default: 11193 /* We're really broken if we ever get here... */ 11194 panic("sd_xbuf_init: illegal chain type!"); 11195 /*NOTREACHED*/ 11196 } 11197 11198 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 11199 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 11200 11201 /* 11202 * It might be a bit easier to simply bzero the entire xbuf above, 11203 * but it turns out that since we init a fair number of members anyway, 11204 * we save a fair number cycles by doing explicit assignment of zero. 11205 */ 11206 xp->xb_pkt_flags = 0; 11207 xp->xb_dma_resid = 0; 11208 xp->xb_retry_count = 0; 11209 xp->xb_victim_retry_count = 0; 11210 xp->xb_ua_retry_count = 0; 11211 xp->xb_sense_bp = NULL; 11212 xp->xb_sense_status = 0; 11213 xp->xb_sense_state = 0; 11214 xp->xb_sense_resid = 0; 11215 11216 bp->b_private = xp; 11217 bp->b_flags &= ~(B_DONE | B_ERROR); 11218 bp->b_resid = 0; 11219 bp->av_forw = NULL; 11220 bp->av_back = NULL; 11221 bioerror(bp, 0); 11222 11223 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 11224 } 11225 11226 11227 /* 11228 * Function: sd_uscsi_strategy 11229 * 11230 * Description: Wrapper for calling into the USCSI chain via physio(9F) 11231 * 11232 * Arguments: bp - buf struct ptr 11233 * 11234 * Return Code: Always returns 0 11235 * 11236 * Context: Kernel thread context 11237 */ 11238 11239 static int 11240 sd_uscsi_strategy(struct buf *bp) 11241 { 11242 struct sd_lun *un; 11243 struct sd_uscsi_info *uip; 11244 struct sd_xbuf *xp; 11245 uchar_t chain_type; 11246 11247 ASSERT(bp != NULL); 11248 11249 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11250 if (un == NULL) { 11251 bioerror(bp, EIO); 11252 bp->b_resid = bp->b_bcount; 11253 biodone(bp); 11254 return (0); 11255 } 11256 11257 ASSERT(!mutex_owned(SD_MUTEX(un))); 11258 11259 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 11260 11261 mutex_enter(SD_MUTEX(un)); 11262 /* 11263 * atapi: Since we are running the CD for now in PIO mode we need to 11264 * call bp_mapin here to avoid bp_mapin called interrupt context under 11265 * the HBA's init_pkt routine. 11266 */ 11267 if (un->un_f_cfg_is_atapi == TRUE) { 11268 mutex_exit(SD_MUTEX(un)); 11269 bp_mapin(bp); 11270 mutex_enter(SD_MUTEX(un)); 11271 } 11272 un->un_ncmds_in_driver++; 11273 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 11274 un->un_ncmds_in_driver); 11275 mutex_exit(SD_MUTEX(un)); 11276 11277 /* 11278 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 11279 */ 11280 ASSERT(bp->b_private != NULL); 11281 uip = (struct sd_uscsi_info *)bp->b_private; 11282 11283 switch (uip->ui_flags) { 11284 case SD_PATH_DIRECT: 11285 chain_type = SD_CHAIN_DIRECT; 11286 break; 11287 case SD_PATH_DIRECT_PRIORITY: 11288 chain_type = SD_CHAIN_DIRECT_PRIORITY; 11289 break; 11290 default: 11291 chain_type = SD_CHAIN_USCSI; 11292 break; 11293 } 11294 11295 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 11296 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 11297 11298 /* Use the index obtained within xbuf_init */ 11299 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 11300 11301 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 11302 11303 return (0); 11304 } 11305 11306 11307 /* 11308 * These routines perform raw i/o operations. 11309 */ 11310 /*ARGSUSED*/ 11311 static void 11312 sduscsimin(struct buf *bp) 11313 { 11314 /* 11315 * do not break up because the CDB count would then 11316 * be incorrect and data underruns would result (incomplete 11317 * read/writes which would be retried and then failed, see 11318 * sdintr(). 11319 */ 11320 } 11321 11322 11323 11324 /* 11325 * Function: sd_send_scsi_cmd 11326 * 11327 * Description: Runs a USCSI command for user (when called thru sdioctl), 11328 * or for the driver 11329 * 11330 * Arguments: dev - the dev_t for the device 11331 * incmd - ptr to a valid uscsi_cmd struct 11332 * cdbspace - UIO_USERSPACE or UIO_SYSSPACE 11333 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 11334 * rqbufspace - UIO_USERSPACE or UIO_SYSSPACE 11335 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 11336 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 11337 * to use the USCSI "direct" chain and bypass the normal 11338 * command waitq. 11339 * 11340 * Return Code: 0 - successful completion of the given command 11341 * EIO - scsi_reset() failed, or see biowait()/physio() codes. 11342 * ENXIO - soft state not found for specified dev 11343 * EINVAL 11344 * EFAULT - copyin/copyout error 11345 * return code of biowait(9F) or physio(9F): 11346 * EIO - IO error, caller may check incmd->uscsi_status 11347 * ENXIO 11348 * EACCES - reservation conflict 11349 * 11350 * Context: Waits for command to complete. Can sleep. 11351 */ 11352 11353 static int 11354 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 11355 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 11356 int path_flag) 11357 { 11358 struct sd_uscsi_info *uip; 11359 struct uscsi_cmd *uscmd; 11360 struct sd_lun *un; 11361 struct buf *bp; 11362 int rval; 11363 int flags; 11364 11365 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 11366 if (un == NULL) { 11367 return (ENXIO); 11368 } 11369 11370 ASSERT(!mutex_owned(SD_MUTEX(un))); 11371 11372 #ifdef SDDEBUG 11373 switch (dataspace) { 11374 case UIO_USERSPACE: 11375 SD_TRACE(SD_LOG_IO, un, 11376 "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un); 11377 break; 11378 case UIO_SYSSPACE: 11379 SD_TRACE(SD_LOG_IO, un, 11380 "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un); 11381 break; 11382 default: 11383 SD_TRACE(SD_LOG_IO, un, 11384 "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un); 11385 break; 11386 } 11387 #endif 11388 11389 /* 11390 * Perform resets directly; no need to generate a command to do it. 11391 */ 11392 if (incmd->uscsi_flags & (USCSI_RESET | USCSI_RESET_ALL)) { 11393 flags = ((incmd->uscsi_flags & USCSI_RESET_ALL) != 0) ? 11394 RESET_ALL : RESET_TARGET; 11395 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: Issuing reset\n"); 11396 if (scsi_reset(SD_ADDRESS(un), flags) == 0) { 11397 /* Reset attempt was unsuccessful */ 11398 SD_TRACE(SD_LOG_IO, un, 11399 "sd_send_scsi_cmd: reset: failure\n"); 11400 return (EIO); 11401 } 11402 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: reset: success\n"); 11403 return (0); 11404 } 11405 11406 /* Perfunctory sanity check... */ 11407 if (incmd->uscsi_cdblen <= 0) { 11408 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11409 "invalid uscsi_cdblen, returning EINVAL\n"); 11410 return (EINVAL); 11411 } 11412 11413 /* 11414 * In order to not worry about where the uscsi structure came from 11415 * (or where the cdb it points to came from) we're going to make 11416 * kmem_alloc'd copies of them here. This will also allow reference 11417 * to the data they contain long after this process has gone to 11418 * sleep and its kernel stack has been unmapped, etc. 11419 * 11420 * First get some memory for the uscsi_cmd struct and copy the 11421 * contents of the given uscsi_cmd struct into it. 11422 */ 11423 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 11424 bcopy(incmd, uscmd, sizeof (struct uscsi_cmd)); 11425 11426 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: uscsi_cmd", 11427 (uchar_t *)uscmd, sizeof (struct uscsi_cmd), SD_LOG_HEX); 11428 11429 /* 11430 * Now get some space for the CDB, and copy the given CDB into 11431 * it. Use ddi_copyin() in case the data is in user space. 11432 */ 11433 uscmd->uscsi_cdb = kmem_zalloc((size_t)incmd->uscsi_cdblen, KM_SLEEP); 11434 flags = (cdbspace == UIO_SYSSPACE) ? FKIOCTL : 0; 11435 if (ddi_copyin(incmd->uscsi_cdb, uscmd->uscsi_cdb, 11436 (uint_t)incmd->uscsi_cdblen, flags) != 0) { 11437 kmem_free(uscmd->uscsi_cdb, (size_t)incmd->uscsi_cdblen); 11438 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 11439 return (EFAULT); 11440 } 11441 11442 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: CDB", 11443 (uchar_t *)uscmd->uscsi_cdb, incmd->uscsi_cdblen, SD_LOG_HEX); 11444 11445 bp = getrbuf(KM_SLEEP); 11446 11447 /* 11448 * Allocate an sd_uscsi_info struct and fill it with the info 11449 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 11450 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 11451 * since we allocate the buf here in this function, we do not 11452 * need to preserve the prior contents of b_private. 11453 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 11454 */ 11455 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 11456 uip->ui_flags = path_flag; 11457 uip->ui_cmdp = uscmd; 11458 bp->b_private = uip; 11459 11460 /* 11461 * Initialize Request Sense buffering, if requested. 11462 */ 11463 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 11464 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 11465 /* 11466 * Here uscmd->uscsi_rqbuf currently points to the caller's 11467 * buffer, but we replace this with a kernel buffer that 11468 * we allocate to use with the sense data. The sense data 11469 * (if present) gets copied into this new buffer before the 11470 * command is completed. Then we copy the sense data from 11471 * our allocated buf into the caller's buffer below. Note 11472 * that incmd->uscsi_rqbuf and incmd->uscsi_rqlen are used 11473 * below to perform the copy back to the caller's buf. 11474 */ 11475 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 11476 if (rqbufspace == UIO_USERSPACE) { 11477 uscmd->uscsi_rqlen = SENSE_LENGTH; 11478 uscmd->uscsi_rqresid = SENSE_LENGTH; 11479 } else { 11480 uchar_t rlen = min(SENSE_LENGTH, uscmd->uscsi_rqlen); 11481 uscmd->uscsi_rqlen = rlen; 11482 uscmd->uscsi_rqresid = rlen; 11483 } 11484 } else { 11485 uscmd->uscsi_rqbuf = NULL; 11486 uscmd->uscsi_rqlen = 0; 11487 uscmd->uscsi_rqresid = 0; 11488 } 11489 11490 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: rqbuf:0x%p rqlen:%d\n", 11491 uscmd->uscsi_rqbuf, uscmd->uscsi_rqlen); 11492 11493 if (un->un_f_is_fibre == FALSE) { 11494 /* 11495 * Force asynchronous mode, if necessary. Doing this here 11496 * has the unfortunate effect of running other queued 11497 * commands async also, but since the main purpose of this 11498 * capability is downloading new drive firmware, we can 11499 * probably live with it. 11500 */ 11501 if ((uscmd->uscsi_flags & USCSI_ASYNC) != 0) { 11502 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 11503 == 1) { 11504 if (scsi_ifsetcap(SD_ADDRESS(un), 11505 "synchronous", 0, 1) == 1) { 11506 SD_TRACE(SD_LOG_IO, un, 11507 "sd_send_scsi_cmd: forced async ok\n"); 11508 } else { 11509 SD_TRACE(SD_LOG_IO, un, 11510 "sd_send_scsi_cmd:\ 11511 forced async failed\n"); 11512 rval = EINVAL; 11513 goto done; 11514 } 11515 } 11516 } 11517 11518 /* 11519 * Re-enable synchronous mode, if requested 11520 */ 11521 if (uscmd->uscsi_flags & USCSI_SYNC) { 11522 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 11523 == 0) { 11524 int i = scsi_ifsetcap(SD_ADDRESS(un), 11525 "synchronous", 1, 1); 11526 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11527 "re-enabled sync %s\n", 11528 (i == 1) ? "ok" : "failed"); 11529 } 11530 } 11531 } 11532 11533 /* 11534 * Commands sent with priority are intended for error recovery 11535 * situations, and do not have retries performed. 11536 */ 11537 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 11538 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 11539 } 11540 11541 /* 11542 * If we're going to do actual I/O, let physio do all the right things 11543 */ 11544 if (uscmd->uscsi_buflen != 0) { 11545 struct iovec aiov; 11546 struct uio auio; 11547 struct uio *uio = &auio; 11548 11549 bzero(&auio, sizeof (struct uio)); 11550 bzero(&aiov, sizeof (struct iovec)); 11551 aiov.iov_base = uscmd->uscsi_bufaddr; 11552 aiov.iov_len = uscmd->uscsi_buflen; 11553 uio->uio_iov = &aiov; 11554 11555 uio->uio_iovcnt = 1; 11556 uio->uio_resid = uscmd->uscsi_buflen; 11557 uio->uio_segflg = dataspace; 11558 11559 /* 11560 * physio() will block here until the command completes.... 11561 */ 11562 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling physio.\n"); 11563 11564 rval = physio(sd_uscsi_strategy, bp, dev, 11565 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE), 11566 sduscsimin, uio); 11567 11568 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11569 "returned from physio with 0x%x\n", rval); 11570 11571 } else { 11572 /* 11573 * We have to mimic what physio would do here! Argh! 11574 */ 11575 bp->b_flags = B_BUSY | 11576 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE); 11577 bp->b_edev = dev; 11578 bp->b_dev = cmpdev(dev); /* maybe unnecessary? */ 11579 bp->b_bcount = 0; 11580 bp->b_blkno = 0; 11581 11582 SD_TRACE(SD_LOG_IO, un, 11583 "sd_send_scsi_cmd: calling sd_uscsi_strategy...\n"); 11584 11585 (void) sd_uscsi_strategy(bp); 11586 11587 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling biowait\n"); 11588 11589 rval = biowait(bp); 11590 11591 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11592 "returned from biowait with 0x%x\n", rval); 11593 } 11594 11595 done: 11596 11597 #ifdef SDDEBUG 11598 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11599 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 11600 uscmd->uscsi_status, uscmd->uscsi_resid); 11601 if (uscmd->uscsi_bufaddr != NULL) { 11602 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11603 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 11604 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 11605 if (dataspace == UIO_SYSSPACE) { 11606 SD_DUMP_MEMORY(un, SD_LOG_IO, 11607 "data", (uchar_t *)uscmd->uscsi_bufaddr, 11608 uscmd->uscsi_buflen, SD_LOG_HEX); 11609 } 11610 } 11611 #endif 11612 11613 /* 11614 * Get the status and residual to return to the caller. 11615 */ 11616 incmd->uscsi_status = uscmd->uscsi_status; 11617 incmd->uscsi_resid = uscmd->uscsi_resid; 11618 11619 /* 11620 * If the caller wants sense data, copy back whatever sense data 11621 * we may have gotten, and update the relevant rqsense info. 11622 */ 11623 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 11624 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 11625 11626 int rqlen = uscmd->uscsi_rqlen - uscmd->uscsi_rqresid; 11627 rqlen = min(((int)incmd->uscsi_rqlen), rqlen); 11628 11629 /* Update the Request Sense status and resid */ 11630 incmd->uscsi_rqresid = incmd->uscsi_rqlen - rqlen; 11631 incmd->uscsi_rqstatus = uscmd->uscsi_rqstatus; 11632 11633 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11634 "uscsi_rqstatus: 0x%02x uscsi_rqresid:0x%x\n", 11635 incmd->uscsi_rqstatus, incmd->uscsi_rqresid); 11636 11637 /* Copy out the sense data for user processes */ 11638 if ((incmd->uscsi_rqbuf != NULL) && (rqlen != 0)) { 11639 int flags = 11640 (rqbufspace == UIO_USERSPACE) ? 0 : FKIOCTL; 11641 if (ddi_copyout(uscmd->uscsi_rqbuf, incmd->uscsi_rqbuf, 11642 rqlen, flags) != 0) { 11643 rval = EFAULT; 11644 } 11645 /* 11646 * Note: Can't touch incmd->uscsi_rqbuf so use 11647 * uscmd->uscsi_rqbuf instead. They're the same. 11648 */ 11649 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11650 "incmd->uscsi_rqbuf: 0x%p rqlen:%d\n", 11651 incmd->uscsi_rqbuf, rqlen); 11652 SD_DUMP_MEMORY(un, SD_LOG_IO, "rq", 11653 (uchar_t *)uscmd->uscsi_rqbuf, rqlen, SD_LOG_HEX); 11654 } 11655 } 11656 11657 /* 11658 * Free allocated resources and return; mapout the buf in case it was 11659 * mapped in by a lower layer. 11660 */ 11661 bp_mapout(bp); 11662 freerbuf(bp); 11663 kmem_free(uip, sizeof (struct sd_uscsi_info)); 11664 if (uscmd->uscsi_rqbuf != NULL) { 11665 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 11666 } 11667 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 11668 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 11669 11670 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: exit\n"); 11671 11672 return (rval); 11673 } 11674 11675 11676 /* 11677 * Function: sd_buf_iodone 11678 * 11679 * Description: Frees the sd_xbuf & returns the buf to its originator. 11680 * 11681 * Context: May be called from interrupt context. 11682 */ 11683 /* ARGSUSED */ 11684 static void 11685 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 11686 { 11687 struct sd_xbuf *xp; 11688 11689 ASSERT(un != NULL); 11690 ASSERT(bp != NULL); 11691 ASSERT(!mutex_owned(SD_MUTEX(un))); 11692 11693 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 11694 11695 xp = SD_GET_XBUF(bp); 11696 ASSERT(xp != NULL); 11697 11698 mutex_enter(SD_MUTEX(un)); 11699 11700 /* 11701 * Grab time when the cmd completed. 11702 * This is used for determining if the system has been 11703 * idle long enough to make it idle to the PM framework. 11704 * This is for lowering the overhead, and therefore improving 11705 * performance per I/O operation. 11706 */ 11707 un->un_pm_idle_time = ddi_get_time(); 11708 11709 un->un_ncmds_in_driver--; 11710 ASSERT(un->un_ncmds_in_driver >= 0); 11711 SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 11712 un->un_ncmds_in_driver); 11713 11714 mutex_exit(SD_MUTEX(un)); 11715 11716 ddi_xbuf_done(bp, un->un_xbuf_attr); /* xbuf is gone after this */ 11717 biodone(bp); /* bp is gone after this */ 11718 11719 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 11720 } 11721 11722 11723 /* 11724 * Function: sd_uscsi_iodone 11725 * 11726 * Description: Frees the sd_xbuf & returns the buf to its originator. 11727 * 11728 * Context: May be called from interrupt context. 11729 */ 11730 /* ARGSUSED */ 11731 static void 11732 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 11733 { 11734 struct sd_xbuf *xp; 11735 11736 ASSERT(un != NULL); 11737 ASSERT(bp != NULL); 11738 11739 xp = SD_GET_XBUF(bp); 11740 ASSERT(xp != NULL); 11741 ASSERT(!mutex_owned(SD_MUTEX(un))); 11742 11743 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 11744 11745 mutex_enter(SD_MUTEX(un)); 11746 11747 /* 11748 * Grab time when the cmd completed. 11749 * This is used for determining if the system has been 11750 * idle long enough to make it idle to the PM framework. 11751 * This is for lowering the overhead, and therefore improving 11752 * performance per I/O operation. 11753 */ 11754 un->un_pm_idle_time = ddi_get_time(); 11755 11756 un->un_ncmds_in_driver--; 11757 ASSERT(un->un_ncmds_in_driver >= 0); 11758 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 11759 un->un_ncmds_in_driver); 11760 11761 mutex_exit(SD_MUTEX(un)); 11762 11763 kmem_free(xp, sizeof (struct sd_xbuf)); 11764 biodone(bp); 11765 11766 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 11767 } 11768 11769 11770 /* 11771 * Function: sd_mapblockaddr_iostart 11772 * 11773 * Description: Verify request lies withing the partition limits for 11774 * the indicated minor device. Issue "overrun" buf if 11775 * request would exceed partition range. Converts 11776 * partition-relative block address to absolute. 11777 * 11778 * Context: Can sleep 11779 * 11780 * Issues: This follows what the old code did, in terms of accessing 11781 * some of the partition info in the unit struct without holding 11782 * the mutext. This is a general issue, if the partition info 11783 * can be altered while IO is in progress... as soon as we send 11784 * a buf, its partitioning can be invalid before it gets to the 11785 * device. Probably the right fix is to move partitioning out 11786 * of the driver entirely. 11787 */ 11788 11789 static void 11790 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 11791 { 11792 daddr_t nblocks; /* #blocks in the given partition */ 11793 daddr_t blocknum; /* Block number specified by the buf */ 11794 size_t requested_nblocks; 11795 size_t available_nblocks; 11796 int partition; 11797 diskaddr_t partition_offset; 11798 struct sd_xbuf *xp; 11799 11800 11801 ASSERT(un != NULL); 11802 ASSERT(bp != NULL); 11803 ASSERT(!mutex_owned(SD_MUTEX(un))); 11804 11805 SD_TRACE(SD_LOG_IO_PARTITION, un, 11806 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 11807 11808 xp = SD_GET_XBUF(bp); 11809 ASSERT(xp != NULL); 11810 11811 /* 11812 * If the geometry is not indicated as valid, attempt to access 11813 * the unit & verify the geometry/label. This can be the case for 11814 * removable-media devices, of if the device was opened in 11815 * NDELAY/NONBLOCK mode. 11816 */ 11817 if ((un->un_f_geometry_is_valid != TRUE) && 11818 (sd_ready_and_valid(un) != SD_READY_VALID)) { 11819 /* 11820 * For removable devices it is possible to start an I/O 11821 * without a media by opening the device in nodelay mode. 11822 * Also for writable CDs there can be many scenarios where 11823 * there is no geometry yet but volume manager is trying to 11824 * issue a read() just because it can see TOC on the CD. So 11825 * do not print a message for removables. 11826 */ 11827 if (!ISREMOVABLE(un)) { 11828 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 11829 "i/o to invalid geometry\n"); 11830 } 11831 bioerror(bp, EIO); 11832 bp->b_resid = bp->b_bcount; 11833 SD_BEGIN_IODONE(index, un, bp); 11834 return; 11835 } 11836 11837 partition = SDPART(bp->b_edev); 11838 11839 /* #blocks in partition */ 11840 nblocks = un->un_map[partition].dkl_nblk; /* #blocks in partition */ 11841 11842 /* Use of a local variable potentially improves performance slightly */ 11843 partition_offset = un->un_offset[partition]; 11844 11845 /* 11846 * blocknum is the starting block number of the request. At this 11847 * point it is still relative to the start of the minor device. 11848 */ 11849 blocknum = xp->xb_blkno; 11850 11851 /* 11852 * Legacy: If the starting block number is one past the last block 11853 * in the partition, do not set B_ERROR in the buf. 11854 */ 11855 if (blocknum == nblocks) { 11856 goto error_exit; 11857 } 11858 11859 /* 11860 * Confirm that the first block of the request lies within the 11861 * partition limits. Also the requested number of bytes must be 11862 * a multiple of the system block size. 11863 */ 11864 if ((blocknum < 0) || (blocknum >= nblocks) || 11865 ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) { 11866 bp->b_flags |= B_ERROR; 11867 goto error_exit; 11868 } 11869 11870 /* 11871 * If the requsted # blocks exceeds the available # blocks, that 11872 * is an overrun of the partition. 11873 */ 11874 requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount); 11875 available_nblocks = (size_t)(nblocks - blocknum); 11876 ASSERT(nblocks >= blocknum); 11877 11878 if (requested_nblocks > available_nblocks) { 11879 /* 11880 * Allocate an "overrun" buf to allow the request to proceed 11881 * for the amount of space available in the partition. The 11882 * amount not transferred will be added into the b_resid 11883 * when the operation is complete. The overrun buf 11884 * replaces the original buf here, and the original buf 11885 * is saved inside the overrun buf, for later use. 11886 */ 11887 size_t resid = SD_SYSBLOCKS2BYTES(un, 11888 (offset_t)(requested_nblocks - available_nblocks)); 11889 size_t count = bp->b_bcount - resid; 11890 /* 11891 * Note: count is an unsigned entity thus it'll NEVER 11892 * be less than 0 so ASSERT the original values are 11893 * correct. 11894 */ 11895 ASSERT(bp->b_bcount >= resid); 11896 11897 bp = sd_bioclone_alloc(bp, count, blocknum, 11898 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 11899 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 11900 ASSERT(xp != NULL); 11901 } 11902 11903 /* At this point there should be no residual for this buf. */ 11904 ASSERT(bp->b_resid == 0); 11905 11906 /* Convert the block number to an absolute address. */ 11907 xp->xb_blkno += partition_offset; 11908 11909 SD_NEXT_IOSTART(index, un, bp); 11910 11911 SD_TRACE(SD_LOG_IO_PARTITION, un, 11912 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 11913 11914 return; 11915 11916 error_exit: 11917 bp->b_resid = bp->b_bcount; 11918 SD_BEGIN_IODONE(index, un, bp); 11919 SD_TRACE(SD_LOG_IO_PARTITION, un, 11920 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 11921 } 11922 11923 11924 /* 11925 * Function: sd_mapblockaddr_iodone 11926 * 11927 * Description: Completion-side processing for partition management. 11928 * 11929 * Context: May be called under interrupt context 11930 */ 11931 11932 static void 11933 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 11934 { 11935 /* int partition; */ /* Not used, see below. */ 11936 ASSERT(un != NULL); 11937 ASSERT(bp != NULL); 11938 ASSERT(!mutex_owned(SD_MUTEX(un))); 11939 11940 SD_TRACE(SD_LOG_IO_PARTITION, un, 11941 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 11942 11943 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 11944 /* 11945 * We have an "overrun" buf to deal with... 11946 */ 11947 struct sd_xbuf *xp; 11948 struct buf *obp; /* ptr to the original buf */ 11949 11950 xp = SD_GET_XBUF(bp); 11951 ASSERT(xp != NULL); 11952 11953 /* Retrieve the pointer to the original buf */ 11954 obp = (struct buf *)xp->xb_private; 11955 ASSERT(obp != NULL); 11956 11957 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 11958 bioerror(obp, bp->b_error); 11959 11960 sd_bioclone_free(bp); 11961 11962 /* 11963 * Get back the original buf. 11964 * Note that since the restoration of xb_blkno below 11965 * was removed, the sd_xbuf is not needed. 11966 */ 11967 bp = obp; 11968 /* 11969 * xp = SD_GET_XBUF(bp); 11970 * ASSERT(xp != NULL); 11971 */ 11972 } 11973 11974 /* 11975 * Convert sd->xb_blkno back to a minor-device relative value. 11976 * Note: this has been commented out, as it is not needed in the 11977 * current implementation of the driver (ie, since this function 11978 * is at the top of the layering chains, so the info will be 11979 * discarded) and it is in the "hot" IO path. 11980 * 11981 * partition = getminor(bp->b_edev) & SDPART_MASK; 11982 * xp->xb_blkno -= un->un_offset[partition]; 11983 */ 11984 11985 SD_NEXT_IODONE(index, un, bp); 11986 11987 SD_TRACE(SD_LOG_IO_PARTITION, un, 11988 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 11989 } 11990 11991 11992 /* 11993 * Function: sd_mapblocksize_iostart 11994 * 11995 * Description: Convert between system block size (un->un_sys_blocksize) 11996 * and target block size (un->un_tgt_blocksize). 11997 * 11998 * Context: Can sleep to allocate resources. 11999 * 12000 * Assumptions: A higher layer has already performed any partition validation, 12001 * and converted the xp->xb_blkno to an absolute value relative 12002 * to the start of the device. 12003 * 12004 * It is also assumed that the higher layer has implemented 12005 * an "overrun" mechanism for the case where the request would 12006 * read/write beyond the end of a partition. In this case we 12007 * assume (and ASSERT) that bp->b_resid == 0. 12008 * 12009 * Note: The implementation for this routine assumes the target 12010 * block size remains constant between allocation and transport. 12011 */ 12012 12013 static void 12014 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 12015 { 12016 struct sd_mapblocksize_info *bsp; 12017 struct sd_xbuf *xp; 12018 offset_t first_byte; 12019 daddr_t start_block, end_block; 12020 daddr_t request_bytes; 12021 ushort_t is_aligned = FALSE; 12022 12023 ASSERT(un != NULL); 12024 ASSERT(bp != NULL); 12025 ASSERT(!mutex_owned(SD_MUTEX(un))); 12026 ASSERT(bp->b_resid == 0); 12027 12028 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12029 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 12030 12031 /* 12032 * For a non-writable CD, a write request is an error 12033 */ 12034 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 12035 (un->un_f_mmc_writable_media == FALSE)) { 12036 bioerror(bp, EIO); 12037 bp->b_resid = bp->b_bcount; 12038 SD_BEGIN_IODONE(index, un, bp); 12039 return; 12040 } 12041 12042 /* 12043 * We do not need a shadow buf if the device is using 12044 * un->un_sys_blocksize as its block size or if bcount == 0. 12045 * In this case there is no layer-private data block allocated. 12046 */ 12047 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12048 (bp->b_bcount == 0)) { 12049 goto done; 12050 } 12051 12052 #if defined(__i386) || defined(__amd64) 12053 /* We do not support non-block-aligned transfers for ROD devices */ 12054 ASSERT(!ISROD(un)); 12055 #endif 12056 12057 xp = SD_GET_XBUF(bp); 12058 ASSERT(xp != NULL); 12059 12060 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12061 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 12062 un->un_tgt_blocksize, un->un_sys_blocksize); 12063 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12064 "request start block:0x%x\n", xp->xb_blkno); 12065 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12066 "request len:0x%x\n", bp->b_bcount); 12067 12068 /* 12069 * Allocate the layer-private data area for the mapblocksize layer. 12070 * Layers are allowed to use the xp_private member of the sd_xbuf 12071 * struct to store the pointer to their layer-private data block, but 12072 * each layer also has the responsibility of restoring the prior 12073 * contents of xb_private before returning the buf/xbuf to the 12074 * higher layer that sent it. 12075 * 12076 * Here we save the prior contents of xp->xb_private into the 12077 * bsp->mbs_oprivate field of our layer-private data area. This value 12078 * is restored by sd_mapblocksize_iodone() just prior to freeing up 12079 * the layer-private area and returning the buf/xbuf to the layer 12080 * that sent it. 12081 * 12082 * Note that here we use kmem_zalloc for the allocation as there are 12083 * parts of the mapblocksize code that expect certain fields to be 12084 * zero unless explicitly set to a required value. 12085 */ 12086 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12087 bsp->mbs_oprivate = xp->xb_private; 12088 xp->xb_private = bsp; 12089 12090 /* 12091 * This treats the data on the disk (target) as an array of bytes. 12092 * first_byte is the byte offset, from the beginning of the device, 12093 * to the location of the request. This is converted from a 12094 * un->un_sys_blocksize block address to a byte offset, and then back 12095 * to a block address based upon a un->un_tgt_blocksize block size. 12096 * 12097 * xp->xb_blkno should be absolute upon entry into this function, 12098 * but, but it is based upon partitions that use the "system" 12099 * block size. It must be adjusted to reflect the block size of 12100 * the target. 12101 * 12102 * Note that end_block is actually the block that follows the last 12103 * block of the request, but that's what is needed for the computation. 12104 */ 12105 first_byte = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12106 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 12107 end_block = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) / 12108 un->un_tgt_blocksize; 12109 12110 /* request_bytes is rounded up to a multiple of the target block size */ 12111 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 12112 12113 /* 12114 * See if the starting address of the request and the request 12115 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 12116 * then we do not need to allocate a shadow buf to handle the request. 12117 */ 12118 if (((first_byte % un->un_tgt_blocksize) == 0) && 12119 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 12120 is_aligned = TRUE; 12121 } 12122 12123 if ((bp->b_flags & B_READ) == 0) { 12124 /* 12125 * Lock the range for a write operation. An aligned request is 12126 * considered a simple write; otherwise the request must be a 12127 * read-modify-write. 12128 */ 12129 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 12130 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 12131 } 12132 12133 /* 12134 * Alloc a shadow buf if the request is not aligned. Also, this is 12135 * where the READ command is generated for a read-modify-write. (The 12136 * write phase is deferred until after the read completes.) 12137 */ 12138 if (is_aligned == FALSE) { 12139 12140 struct sd_mapblocksize_info *shadow_bsp; 12141 struct sd_xbuf *shadow_xp; 12142 struct buf *shadow_bp; 12143 12144 /* 12145 * Allocate the shadow buf and it associated xbuf. Note that 12146 * after this call the xb_blkno value in both the original 12147 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 12148 * same: absolute relative to the start of the device, and 12149 * adjusted for the target block size. The b_blkno in the 12150 * shadow buf will also be set to this value. We should never 12151 * change b_blkno in the original bp however. 12152 * 12153 * Note also that the shadow buf will always need to be a 12154 * READ command, regardless of whether the incoming command 12155 * is a READ or a WRITE. 12156 */ 12157 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 12158 xp->xb_blkno, 12159 (int (*)(struct buf *)) sd_mapblocksize_iodone); 12160 12161 shadow_xp = SD_GET_XBUF(shadow_bp); 12162 12163 /* 12164 * Allocate the layer-private data for the shadow buf. 12165 * (No need to preserve xb_private in the shadow xbuf.) 12166 */ 12167 shadow_xp->xb_private = shadow_bsp = 12168 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12169 12170 /* 12171 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 12172 * to figure out where the start of the user data is (based upon 12173 * the system block size) in the data returned by the READ 12174 * command (which will be based upon the target blocksize). Note 12175 * that this is only really used if the request is unaligned. 12176 */ 12177 bsp->mbs_copy_offset = (ssize_t)(first_byte - 12178 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 12179 ASSERT((bsp->mbs_copy_offset >= 0) && 12180 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 12181 12182 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 12183 12184 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 12185 12186 /* Transfer the wmap (if any) to the shadow buf */ 12187 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 12188 bsp->mbs_wmp = NULL; 12189 12190 /* 12191 * The shadow buf goes on from here in place of the 12192 * original buf. 12193 */ 12194 shadow_bsp->mbs_orig_bp = bp; 12195 bp = shadow_bp; 12196 } 12197 12198 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12199 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 12200 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12201 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 12202 request_bytes); 12203 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12204 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 12205 12206 done: 12207 SD_NEXT_IOSTART(index, un, bp); 12208 12209 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12210 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 12211 } 12212 12213 12214 /* 12215 * Function: sd_mapblocksize_iodone 12216 * 12217 * Description: Completion side processing for block-size mapping. 12218 * 12219 * Context: May be called under interrupt context 12220 */ 12221 12222 static void 12223 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 12224 { 12225 struct sd_mapblocksize_info *bsp; 12226 struct sd_xbuf *xp; 12227 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 12228 struct buf *orig_bp; /* ptr to the original buf */ 12229 offset_t shadow_end; 12230 offset_t request_end; 12231 offset_t shadow_start; 12232 ssize_t copy_offset; 12233 size_t copy_length; 12234 size_t shortfall; 12235 uint_t is_write; /* TRUE if this bp is a WRITE */ 12236 uint_t has_wmap; /* TRUE is this bp has a wmap */ 12237 12238 ASSERT(un != NULL); 12239 ASSERT(bp != NULL); 12240 12241 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12242 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 12243 12244 /* 12245 * There is no shadow buf or layer-private data if the target is 12246 * using un->un_sys_blocksize as its block size or if bcount == 0. 12247 */ 12248 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12249 (bp->b_bcount == 0)) { 12250 goto exit; 12251 } 12252 12253 xp = SD_GET_XBUF(bp); 12254 ASSERT(xp != NULL); 12255 12256 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 12257 bsp = xp->xb_private; 12258 12259 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 12260 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 12261 12262 if (is_write) { 12263 /* 12264 * For a WRITE request we must free up the block range that 12265 * we have locked up. This holds regardless of whether this is 12266 * an aligned write request or a read-modify-write request. 12267 */ 12268 sd_range_unlock(un, bsp->mbs_wmp); 12269 bsp->mbs_wmp = NULL; 12270 } 12271 12272 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 12273 /* 12274 * An aligned read or write command will have no shadow buf; 12275 * there is not much else to do with it. 12276 */ 12277 goto done; 12278 } 12279 12280 orig_bp = bsp->mbs_orig_bp; 12281 ASSERT(orig_bp != NULL); 12282 orig_xp = SD_GET_XBUF(orig_bp); 12283 ASSERT(orig_xp != NULL); 12284 ASSERT(!mutex_owned(SD_MUTEX(un))); 12285 12286 if (!is_write && has_wmap) { 12287 /* 12288 * A READ with a wmap means this is the READ phase of a 12289 * read-modify-write. If an error occurred on the READ then 12290 * we do not proceed with the WRITE phase or copy any data. 12291 * Just release the write maps and return with an error. 12292 */ 12293 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 12294 orig_bp->b_resid = orig_bp->b_bcount; 12295 bioerror(orig_bp, bp->b_error); 12296 sd_range_unlock(un, bsp->mbs_wmp); 12297 goto freebuf_done; 12298 } 12299 } 12300 12301 /* 12302 * Here is where we set up to copy the data from the shadow buf 12303 * into the space associated with the original buf. 12304 * 12305 * To deal with the conversion between block sizes, these 12306 * computations treat the data as an array of bytes, with the 12307 * first byte (byte 0) corresponding to the first byte in the 12308 * first block on the disk. 12309 */ 12310 12311 /* 12312 * shadow_start and shadow_len indicate the location and size of 12313 * the data returned with the shadow IO request. 12314 */ 12315 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12316 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 12317 12318 /* 12319 * copy_offset gives the offset (in bytes) from the start of the first 12320 * block of the READ request to the beginning of the data. We retrieve 12321 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 12322 * there by sd_mapblockize_iostart(). copy_length gives the amount of 12323 * data to be copied (in bytes). 12324 */ 12325 copy_offset = bsp->mbs_copy_offset; 12326 ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize)); 12327 copy_length = orig_bp->b_bcount; 12328 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 12329 12330 /* 12331 * Set up the resid and error fields of orig_bp as appropriate. 12332 */ 12333 if (shadow_end >= request_end) { 12334 /* We got all the requested data; set resid to zero */ 12335 orig_bp->b_resid = 0; 12336 } else { 12337 /* 12338 * We failed to get enough data to fully satisfy the original 12339 * request. Just copy back whatever data we got and set 12340 * up the residual and error code as required. 12341 * 12342 * 'shortfall' is the amount by which the data received with the 12343 * shadow buf has "fallen short" of the requested amount. 12344 */ 12345 shortfall = (size_t)(request_end - shadow_end); 12346 12347 if (shortfall > orig_bp->b_bcount) { 12348 /* 12349 * We did not get enough data to even partially 12350 * fulfill the original request. The residual is 12351 * equal to the amount requested. 12352 */ 12353 orig_bp->b_resid = orig_bp->b_bcount; 12354 } else { 12355 /* 12356 * We did not get all the data that we requested 12357 * from the device, but we will try to return what 12358 * portion we did get. 12359 */ 12360 orig_bp->b_resid = shortfall; 12361 } 12362 ASSERT(copy_length >= orig_bp->b_resid); 12363 copy_length -= orig_bp->b_resid; 12364 } 12365 12366 /* Propagate the error code from the shadow buf to the original buf */ 12367 bioerror(orig_bp, bp->b_error); 12368 12369 if (is_write) { 12370 goto freebuf_done; /* No data copying for a WRITE */ 12371 } 12372 12373 if (has_wmap) { 12374 /* 12375 * This is a READ command from the READ phase of a 12376 * read-modify-write request. We have to copy the data given 12377 * by the user OVER the data returned by the READ command, 12378 * then convert the command from a READ to a WRITE and send 12379 * it back to the target. 12380 */ 12381 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 12382 copy_length); 12383 12384 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 12385 12386 /* 12387 * Dispatch the WRITE command to the taskq thread, which 12388 * will in turn send the command to the target. When the 12389 * WRITE command completes, we (sd_mapblocksize_iodone()) 12390 * will get called again as part of the iodone chain 12391 * processing for it. Note that we will still be dealing 12392 * with the shadow buf at that point. 12393 */ 12394 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 12395 KM_NOSLEEP) != 0) { 12396 /* 12397 * Dispatch was successful so we are done. Return 12398 * without going any higher up the iodone chain. Do 12399 * not free up any layer-private data until after the 12400 * WRITE completes. 12401 */ 12402 return; 12403 } 12404 12405 /* 12406 * Dispatch of the WRITE command failed; set up the error 12407 * condition and send this IO back up the iodone chain. 12408 */ 12409 bioerror(orig_bp, EIO); 12410 orig_bp->b_resid = orig_bp->b_bcount; 12411 12412 } else { 12413 /* 12414 * This is a regular READ request (ie, not a RMW). Copy the 12415 * data from the shadow buf into the original buf. The 12416 * copy_offset compensates for any "misalignment" between the 12417 * shadow buf (with its un->un_tgt_blocksize blocks) and the 12418 * original buf (with its un->un_sys_blocksize blocks). 12419 */ 12420 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 12421 copy_length); 12422 } 12423 12424 freebuf_done: 12425 12426 /* 12427 * At this point we still have both the shadow buf AND the original 12428 * buf to deal with, as well as the layer-private data area in each. 12429 * Local variables are as follows: 12430 * 12431 * bp -- points to shadow buf 12432 * xp -- points to xbuf of shadow buf 12433 * bsp -- points to layer-private data area of shadow buf 12434 * orig_bp -- points to original buf 12435 * 12436 * First free the shadow buf and its associated xbuf, then free the 12437 * layer-private data area from the shadow buf. There is no need to 12438 * restore xb_private in the shadow xbuf. 12439 */ 12440 sd_shadow_buf_free(bp); 12441 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 12442 12443 /* 12444 * Now update the local variables to point to the original buf, xbuf, 12445 * and layer-private area. 12446 */ 12447 bp = orig_bp; 12448 xp = SD_GET_XBUF(bp); 12449 ASSERT(xp != NULL); 12450 ASSERT(xp == orig_xp); 12451 bsp = xp->xb_private; 12452 ASSERT(bsp != NULL); 12453 12454 done: 12455 /* 12456 * Restore xb_private to whatever it was set to by the next higher 12457 * layer in the chain, then free the layer-private data area. 12458 */ 12459 xp->xb_private = bsp->mbs_oprivate; 12460 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 12461 12462 exit: 12463 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 12464 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 12465 12466 SD_NEXT_IODONE(index, un, bp); 12467 } 12468 12469 12470 /* 12471 * Function: sd_checksum_iostart 12472 * 12473 * Description: A stub function for a layer that's currently not used. 12474 * For now just a placeholder. 12475 * 12476 * Context: Kernel thread context 12477 */ 12478 12479 static void 12480 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 12481 { 12482 ASSERT(un != NULL); 12483 ASSERT(bp != NULL); 12484 ASSERT(!mutex_owned(SD_MUTEX(un))); 12485 SD_NEXT_IOSTART(index, un, bp); 12486 } 12487 12488 12489 /* 12490 * Function: sd_checksum_iodone 12491 * 12492 * Description: A stub function for a layer that's currently not used. 12493 * For now just a placeholder. 12494 * 12495 * Context: May be called under interrupt context 12496 */ 12497 12498 static void 12499 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 12500 { 12501 ASSERT(un != NULL); 12502 ASSERT(bp != NULL); 12503 ASSERT(!mutex_owned(SD_MUTEX(un))); 12504 SD_NEXT_IODONE(index, un, bp); 12505 } 12506 12507 12508 /* 12509 * Function: sd_checksum_uscsi_iostart 12510 * 12511 * Description: A stub function for a layer that's currently not used. 12512 * For now just a placeholder. 12513 * 12514 * Context: Kernel thread context 12515 */ 12516 12517 static void 12518 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 12519 { 12520 ASSERT(un != NULL); 12521 ASSERT(bp != NULL); 12522 ASSERT(!mutex_owned(SD_MUTEX(un))); 12523 SD_NEXT_IOSTART(index, un, bp); 12524 } 12525 12526 12527 /* 12528 * Function: sd_checksum_uscsi_iodone 12529 * 12530 * Description: A stub function for a layer that's currently not used. 12531 * For now just a placeholder. 12532 * 12533 * Context: May be called under interrupt context 12534 */ 12535 12536 static void 12537 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12538 { 12539 ASSERT(un != NULL); 12540 ASSERT(bp != NULL); 12541 ASSERT(!mutex_owned(SD_MUTEX(un))); 12542 SD_NEXT_IODONE(index, un, bp); 12543 } 12544 12545 12546 /* 12547 * Function: sd_pm_iostart 12548 * 12549 * Description: iostart-side routine for Power mangement. 12550 * 12551 * Context: Kernel thread context 12552 */ 12553 12554 static void 12555 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 12556 { 12557 ASSERT(un != NULL); 12558 ASSERT(bp != NULL); 12559 ASSERT(!mutex_owned(SD_MUTEX(un))); 12560 ASSERT(!mutex_owned(&un->un_pm_mutex)); 12561 12562 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 12563 12564 if (sd_pm_entry(un) != DDI_SUCCESS) { 12565 /* 12566 * Set up to return the failed buf back up the 'iodone' 12567 * side of the calling chain. 12568 */ 12569 bioerror(bp, EIO); 12570 bp->b_resid = bp->b_bcount; 12571 12572 SD_BEGIN_IODONE(index, un, bp); 12573 12574 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 12575 return; 12576 } 12577 12578 SD_NEXT_IOSTART(index, un, bp); 12579 12580 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 12581 } 12582 12583 12584 /* 12585 * Function: sd_pm_iodone 12586 * 12587 * Description: iodone-side routine for power mangement. 12588 * 12589 * Context: may be called from interrupt context 12590 */ 12591 12592 static void 12593 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 12594 { 12595 ASSERT(un != NULL); 12596 ASSERT(bp != NULL); 12597 ASSERT(!mutex_owned(&un->un_pm_mutex)); 12598 12599 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 12600 12601 /* 12602 * After attach the following flag is only read, so don't 12603 * take the penalty of acquiring a mutex for it. 12604 */ 12605 if (un->un_f_pm_is_enabled == TRUE) { 12606 sd_pm_exit(un); 12607 } 12608 12609 SD_NEXT_IODONE(index, un, bp); 12610 12611 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 12612 } 12613 12614 12615 /* 12616 * Function: sd_core_iostart 12617 * 12618 * Description: Primary driver function for enqueuing buf(9S) structs from 12619 * the system and initiating IO to the target device 12620 * 12621 * Context: Kernel thread context. Can sleep. 12622 * 12623 * Assumptions: - The given xp->xb_blkno is absolute 12624 * (ie, relative to the start of the device). 12625 * - The IO is to be done using the native blocksize of 12626 * the device, as specified in un->un_tgt_blocksize. 12627 */ 12628 /* ARGSUSED */ 12629 static void 12630 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 12631 { 12632 struct sd_xbuf *xp; 12633 12634 ASSERT(un != NULL); 12635 ASSERT(bp != NULL); 12636 ASSERT(!mutex_owned(SD_MUTEX(un))); 12637 ASSERT(bp->b_resid == 0); 12638 12639 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 12640 12641 xp = SD_GET_XBUF(bp); 12642 ASSERT(xp != NULL); 12643 12644 mutex_enter(SD_MUTEX(un)); 12645 12646 /* 12647 * If we are currently in the failfast state, fail any new IO 12648 * that has B_FAILFAST set, then return. 12649 */ 12650 if ((bp->b_flags & B_FAILFAST) && 12651 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 12652 mutex_exit(SD_MUTEX(un)); 12653 bioerror(bp, EIO); 12654 bp->b_resid = bp->b_bcount; 12655 SD_BEGIN_IODONE(index, un, bp); 12656 return; 12657 } 12658 12659 if (SD_IS_DIRECT_PRIORITY(xp)) { 12660 /* 12661 * Priority command -- transport it immediately. 12662 * 12663 * Note: We may want to assert that USCSI_DIAGNOSE is set, 12664 * because all direct priority commands should be associated 12665 * with error recovery actions which we don't want to retry. 12666 */ 12667 sd_start_cmds(un, bp); 12668 } else { 12669 /* 12670 * Normal command -- add it to the wait queue, then start 12671 * transporting commands from the wait queue. 12672 */ 12673 sd_add_buf_to_waitq(un, bp); 12674 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 12675 sd_start_cmds(un, NULL); 12676 } 12677 12678 mutex_exit(SD_MUTEX(un)); 12679 12680 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 12681 } 12682 12683 12684 /* 12685 * Function: sd_init_cdb_limits 12686 * 12687 * Description: This is to handle scsi_pkt initialization differences 12688 * between the driver platforms. 12689 * 12690 * Legacy behaviors: 12691 * 12692 * If the block number or the sector count exceeds the 12693 * capabilities of a Group 0 command, shift over to a 12694 * Group 1 command. We don't blindly use Group 1 12695 * commands because a) some drives (CDC Wren IVs) get a 12696 * bit confused, and b) there is probably a fair amount 12697 * of speed difference for a target to receive and decode 12698 * a 10 byte command instead of a 6 byte command. 12699 * 12700 * The xfer time difference of 6 vs 10 byte CDBs is 12701 * still significant so this code is still worthwhile. 12702 * 10 byte CDBs are very inefficient with the fas HBA driver 12703 * and older disks. Each CDB byte took 1 usec with some 12704 * popular disks. 12705 * 12706 * Context: Must be called at attach time 12707 */ 12708 12709 static void 12710 sd_init_cdb_limits(struct sd_lun *un) 12711 { 12712 /* 12713 * Use CDB_GROUP1 commands for most devices except for 12714 * parallel SCSI fixed drives in which case we get better 12715 * performance using CDB_GROUP0 commands (where applicable). 12716 */ 12717 un->un_mincdb = SD_CDB_GROUP1; 12718 #if !defined(__fibre) 12719 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 12720 !ISREMOVABLE(un)) { 12721 un->un_mincdb = SD_CDB_GROUP0; 12722 } 12723 #endif 12724 12725 /* 12726 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 12727 * commands for fixed disks unless we are building for a 32 bit 12728 * kernel. 12729 */ 12730 #ifdef _LP64 12731 un->un_maxcdb = (ISREMOVABLE(un)) ? SD_CDB_GROUP5 : SD_CDB_GROUP4; 12732 #else 12733 un->un_maxcdb = (ISREMOVABLE(un)) ? SD_CDB_GROUP5 : SD_CDB_GROUP1; 12734 #endif 12735 12736 /* 12737 * x86 systems require the PKT_DMA_PARTIAL flag 12738 */ 12739 #if defined(__x86) 12740 un->un_pkt_flags = PKT_DMA_PARTIAL; 12741 #else 12742 un->un_pkt_flags = 0; 12743 #endif 12744 12745 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 12746 ? sizeof (struct scsi_arq_status) : 1); 12747 un->un_cmd_timeout = (ushort_t)sd_io_time; 12748 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 12749 } 12750 12751 12752 /* 12753 * Function: sd_initpkt_for_buf 12754 * 12755 * Description: Allocate and initialize for transport a scsi_pkt struct, 12756 * based upon the info specified in the given buf struct. 12757 * 12758 * Assumes the xb_blkno in the request is absolute (ie, 12759 * relative to the start of the device (NOT partition!). 12760 * Also assumes that the request is using the native block 12761 * size of the device (as returned by the READ CAPACITY 12762 * command). 12763 * 12764 * Return Code: SD_PKT_ALLOC_SUCCESS 12765 * SD_PKT_ALLOC_FAILURE 12766 * SD_PKT_ALLOC_FAILURE_NO_DMA 12767 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 12768 * 12769 * Context: Kernel thread and may be called from software interrupt context 12770 * as part of a sdrunout callback. This function may not block or 12771 * call routines that block 12772 */ 12773 12774 static int 12775 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 12776 { 12777 struct sd_xbuf *xp; 12778 struct scsi_pkt *pktp = NULL; 12779 struct sd_lun *un; 12780 size_t blockcount; 12781 daddr_t startblock; 12782 int rval; 12783 int cmd_flags; 12784 12785 ASSERT(bp != NULL); 12786 ASSERT(pktpp != NULL); 12787 xp = SD_GET_XBUF(bp); 12788 ASSERT(xp != NULL); 12789 un = SD_GET_UN(bp); 12790 ASSERT(un != NULL); 12791 ASSERT(mutex_owned(SD_MUTEX(un))); 12792 ASSERT(bp->b_resid == 0); 12793 12794 SD_TRACE(SD_LOG_IO_CORE, un, 12795 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 12796 12797 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 12798 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 12799 /* 12800 * Already have a scsi_pkt -- just need DMA resources. 12801 * We must recompute the CDB in case the mapping returns 12802 * a nonzero pkt_resid. 12803 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 12804 * that is being retried, the unmap/remap of the DMA resouces 12805 * will result in the entire transfer starting over again 12806 * from the very first block. 12807 */ 12808 ASSERT(xp->xb_pktp != NULL); 12809 pktp = xp->xb_pktp; 12810 } else { 12811 pktp = NULL; 12812 } 12813 #endif /* __i386 || __amd64 */ 12814 12815 startblock = xp->xb_blkno; /* Absolute block num. */ 12816 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 12817 12818 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 12819 12820 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 12821 12822 #else 12823 12824 cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags; 12825 12826 #endif 12827 12828 /* 12829 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 12830 * call scsi_init_pkt, and build the CDB. 12831 */ 12832 rval = sd_setup_rw_pkt(un, &pktp, bp, 12833 cmd_flags, sdrunout, (caddr_t)un, 12834 startblock, blockcount); 12835 12836 if (rval == 0) { 12837 /* 12838 * Success. 12839 * 12840 * If partial DMA is being used and required for this transfer. 12841 * set it up here. 12842 */ 12843 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 12844 (pktp->pkt_resid != 0)) { 12845 12846 /* 12847 * Save the CDB length and pkt_resid for the 12848 * next xfer 12849 */ 12850 xp->xb_dma_resid = pktp->pkt_resid; 12851 12852 /* rezero resid */ 12853 pktp->pkt_resid = 0; 12854 12855 } else { 12856 xp->xb_dma_resid = 0; 12857 } 12858 12859 pktp->pkt_flags = un->un_tagflags; 12860 pktp->pkt_time = un->un_cmd_timeout; 12861 pktp->pkt_comp = sdintr; 12862 12863 pktp->pkt_private = bp; 12864 *pktpp = pktp; 12865 12866 SD_TRACE(SD_LOG_IO_CORE, un, 12867 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 12868 12869 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 12870 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 12871 #endif 12872 12873 return (SD_PKT_ALLOC_SUCCESS); 12874 12875 } 12876 12877 /* 12878 * SD_PKT_ALLOC_FAILURE is the only expected failure code 12879 * from sd_setup_rw_pkt. 12880 */ 12881 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 12882 12883 if (rval == SD_PKT_ALLOC_FAILURE) { 12884 *pktpp = NULL; 12885 /* 12886 * Set the driver state to RWAIT to indicate the driver 12887 * is waiting on resource allocations. The driver will not 12888 * suspend, pm_suspend, or detatch while the state is RWAIT. 12889 */ 12890 New_state(un, SD_STATE_RWAIT); 12891 12892 SD_ERROR(SD_LOG_IO_CORE, un, 12893 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 12894 12895 if ((bp->b_flags & B_ERROR) != 0) { 12896 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 12897 } 12898 return (SD_PKT_ALLOC_FAILURE); 12899 } else { 12900 /* 12901 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 12902 * 12903 * This should never happen. Maybe someone messed with the 12904 * kernel's minphys? 12905 */ 12906 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12907 "Request rejected: too large for CDB: " 12908 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 12909 SD_ERROR(SD_LOG_IO_CORE, un, 12910 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 12911 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 12912 12913 } 12914 } 12915 12916 12917 /* 12918 * Function: sd_destroypkt_for_buf 12919 * 12920 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 12921 * 12922 * Context: Kernel thread or interrupt context 12923 */ 12924 12925 static void 12926 sd_destroypkt_for_buf(struct buf *bp) 12927 { 12928 ASSERT(bp != NULL); 12929 ASSERT(SD_GET_UN(bp) != NULL); 12930 12931 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 12932 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 12933 12934 ASSERT(SD_GET_PKTP(bp) != NULL); 12935 scsi_destroy_pkt(SD_GET_PKTP(bp)); 12936 12937 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 12938 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 12939 } 12940 12941 /* 12942 * Function: sd_setup_rw_pkt 12943 * 12944 * Description: Determines appropriate CDB group for the requested LBA 12945 * and transfer length, calls scsi_init_pkt, and builds 12946 * the CDB. Do not use for partial DMA transfers except 12947 * for the initial transfer since the CDB size must 12948 * remain constant. 12949 * 12950 * Context: Kernel thread and may be called from software interrupt 12951 * context as part of a sdrunout callback. This function may not 12952 * block or call routines that block 12953 */ 12954 12955 12956 int 12957 sd_setup_rw_pkt(struct sd_lun *un, 12958 struct scsi_pkt **pktpp, struct buf *bp, int flags, 12959 int (*callback)(caddr_t), caddr_t callback_arg, 12960 diskaddr_t lba, uint32_t blockcount) 12961 { 12962 struct scsi_pkt *return_pktp; 12963 union scsi_cdb *cdbp; 12964 struct sd_cdbinfo *cp = NULL; 12965 int i; 12966 12967 /* 12968 * See which size CDB to use, based upon the request. 12969 */ 12970 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 12971 12972 /* 12973 * Check lba and block count against sd_cdbtab limits. 12974 * In the partial DMA case, we have to use the same size 12975 * CDB for all the transfers. Check lba + blockcount 12976 * against the max LBA so we know that segment of the 12977 * transfer can use the CDB we select. 12978 */ 12979 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 12980 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 12981 12982 /* 12983 * The command will fit into the CDB type 12984 * specified by sd_cdbtab[i]. 12985 */ 12986 cp = sd_cdbtab + i; 12987 12988 /* 12989 * Call scsi_init_pkt so we can fill in the 12990 * CDB. 12991 */ 12992 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 12993 bp, cp->sc_grpcode, un->un_status_len, 0, 12994 flags, callback, callback_arg); 12995 12996 if (return_pktp != NULL) { 12997 12998 /* 12999 * Return new value of pkt 13000 */ 13001 *pktpp = return_pktp; 13002 13003 /* 13004 * To be safe, zero the CDB insuring there is 13005 * no leftover data from a previous command. 13006 */ 13007 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 13008 13009 /* 13010 * Handle partial DMA mapping 13011 */ 13012 if (return_pktp->pkt_resid != 0) { 13013 13014 /* 13015 * Not going to xfer as many blocks as 13016 * originally expected 13017 */ 13018 blockcount -= 13019 SD_BYTES2TGTBLOCKS(un, 13020 return_pktp->pkt_resid); 13021 } 13022 13023 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 13024 13025 /* 13026 * Set command byte based on the CDB 13027 * type we matched. 13028 */ 13029 cdbp->scc_cmd = cp->sc_grpmask | 13030 ((bp->b_flags & B_READ) ? 13031 SCMD_READ : SCMD_WRITE); 13032 13033 SD_FILL_SCSI1_LUN(un, return_pktp); 13034 13035 /* 13036 * Fill in LBA and length 13037 */ 13038 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 13039 (cp->sc_grpcode == CDB_GROUP4) || 13040 (cp->sc_grpcode == CDB_GROUP0) || 13041 (cp->sc_grpcode == CDB_GROUP5)); 13042 13043 if (cp->sc_grpcode == CDB_GROUP1) { 13044 FORMG1ADDR(cdbp, lba); 13045 FORMG1COUNT(cdbp, blockcount); 13046 return (0); 13047 } else if (cp->sc_grpcode == CDB_GROUP4) { 13048 FORMG4LONGADDR(cdbp, lba); 13049 FORMG4COUNT(cdbp, blockcount); 13050 return (0); 13051 } else if (cp->sc_grpcode == CDB_GROUP0) { 13052 FORMG0ADDR(cdbp, lba); 13053 FORMG0COUNT(cdbp, blockcount); 13054 return (0); 13055 } else if (cp->sc_grpcode == CDB_GROUP5) { 13056 FORMG5ADDR(cdbp, lba); 13057 FORMG5COUNT(cdbp, blockcount); 13058 return (0); 13059 } 13060 13061 /* 13062 * It should be impossible to not match one 13063 * of the CDB types above, so we should never 13064 * reach this point. Set the CDB command byte 13065 * to test-unit-ready to avoid writing 13066 * to somewhere we don't intend. 13067 */ 13068 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 13069 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13070 } else { 13071 /* 13072 * Couldn't get scsi_pkt 13073 */ 13074 return (SD_PKT_ALLOC_FAILURE); 13075 } 13076 } 13077 } 13078 13079 /* 13080 * None of the available CDB types were suitable. This really 13081 * should never happen: on a 64 bit system we support 13082 * READ16/WRITE16 which will hold an entire 64 bit disk address 13083 * and on a 32 bit system we will refuse to bind to a device 13084 * larger than 2TB so addresses will never be larger than 32 bits. 13085 */ 13086 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13087 } 13088 13089 /* 13090 * Function: sd_setup_next_rw_pkt 13091 * 13092 * Description: Setup packet for partial DMA transfers, except for the 13093 * initial transfer. sd_setup_rw_pkt should be used for 13094 * the initial transfer. 13095 * 13096 * Context: Kernel thread and may be called from interrupt context. 13097 */ 13098 13099 int 13100 sd_setup_next_rw_pkt(struct sd_lun *un, 13101 struct scsi_pkt *pktp, struct buf *bp, 13102 diskaddr_t lba, uint32_t blockcount) 13103 { 13104 uchar_t com; 13105 union scsi_cdb *cdbp; 13106 uchar_t cdb_group_id; 13107 13108 ASSERT(pktp != NULL); 13109 ASSERT(pktp->pkt_cdbp != NULL); 13110 13111 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 13112 com = cdbp->scc_cmd; 13113 cdb_group_id = CDB_GROUPID(com); 13114 13115 ASSERT((cdb_group_id == CDB_GROUPID_0) || 13116 (cdb_group_id == CDB_GROUPID_1) || 13117 (cdb_group_id == CDB_GROUPID_4) || 13118 (cdb_group_id == CDB_GROUPID_5)); 13119 13120 /* 13121 * Move pkt to the next portion of the xfer. 13122 * func is NULL_FUNC so we do not have to release 13123 * the disk mutex here. 13124 */ 13125 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 13126 NULL_FUNC, NULL) == pktp) { 13127 /* Success. Handle partial DMA */ 13128 if (pktp->pkt_resid != 0) { 13129 blockcount -= 13130 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 13131 } 13132 13133 cdbp->scc_cmd = com; 13134 SD_FILL_SCSI1_LUN(un, pktp); 13135 if (cdb_group_id == CDB_GROUPID_1) { 13136 FORMG1ADDR(cdbp, lba); 13137 FORMG1COUNT(cdbp, blockcount); 13138 return (0); 13139 } else if (cdb_group_id == CDB_GROUPID_4) { 13140 FORMG4LONGADDR(cdbp, lba); 13141 FORMG4COUNT(cdbp, blockcount); 13142 return (0); 13143 } else if (cdb_group_id == CDB_GROUPID_0) { 13144 FORMG0ADDR(cdbp, lba); 13145 FORMG0COUNT(cdbp, blockcount); 13146 return (0); 13147 } else if (cdb_group_id == CDB_GROUPID_5) { 13148 FORMG5ADDR(cdbp, lba); 13149 FORMG5COUNT(cdbp, blockcount); 13150 return (0); 13151 } 13152 13153 /* Unreachable */ 13154 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13155 } 13156 13157 /* 13158 * Error setting up next portion of cmd transfer. 13159 * Something is definitely very wrong and this 13160 * should not happen. 13161 */ 13162 return (SD_PKT_ALLOC_FAILURE); 13163 } 13164 13165 /* 13166 * Function: sd_initpkt_for_uscsi 13167 * 13168 * Description: Allocate and initialize for transport a scsi_pkt struct, 13169 * based upon the info specified in the given uscsi_cmd struct. 13170 * 13171 * Return Code: SD_PKT_ALLOC_SUCCESS 13172 * SD_PKT_ALLOC_FAILURE 13173 * SD_PKT_ALLOC_FAILURE_NO_DMA 13174 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13175 * 13176 * Context: Kernel thread and may be called from software interrupt context 13177 * as part of a sdrunout callback. This function may not block or 13178 * call routines that block 13179 */ 13180 13181 static int 13182 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 13183 { 13184 struct uscsi_cmd *uscmd; 13185 struct sd_xbuf *xp; 13186 struct scsi_pkt *pktp; 13187 struct sd_lun *un; 13188 uint32_t flags = 0; 13189 13190 ASSERT(bp != NULL); 13191 ASSERT(pktpp != NULL); 13192 xp = SD_GET_XBUF(bp); 13193 ASSERT(xp != NULL); 13194 un = SD_GET_UN(bp); 13195 ASSERT(un != NULL); 13196 ASSERT(mutex_owned(SD_MUTEX(un))); 13197 13198 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 13199 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 13200 ASSERT(uscmd != NULL); 13201 13202 SD_TRACE(SD_LOG_IO_CORE, un, 13203 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 13204 13205 /* Allocate the scsi_pkt for the command. */ 13206 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 13207 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 13208 sizeof (struct scsi_arq_status), 0, un->un_pkt_flags, 13209 sdrunout, (caddr_t)un); 13210 13211 if (pktp == NULL) { 13212 *pktpp = NULL; 13213 /* 13214 * Set the driver state to RWAIT to indicate the driver 13215 * is waiting on resource allocations. The driver will not 13216 * suspend, pm_suspend, or detatch while the state is RWAIT. 13217 */ 13218 New_state(un, SD_STATE_RWAIT); 13219 13220 SD_ERROR(SD_LOG_IO_CORE, un, 13221 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 13222 13223 if ((bp->b_flags & B_ERROR) != 0) { 13224 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13225 } 13226 return (SD_PKT_ALLOC_FAILURE); 13227 } 13228 13229 /* 13230 * We do not do DMA breakup for USCSI commands, so return failure 13231 * here if all the needed DMA resources were not allocated. 13232 */ 13233 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 13234 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 13235 scsi_destroy_pkt(pktp); 13236 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 13237 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 13238 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 13239 } 13240 13241 /* Init the cdb from the given uscsi struct */ 13242 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 13243 uscmd->uscsi_cdb[0], 0, 0, 0); 13244 13245 SD_FILL_SCSI1_LUN(un, pktp); 13246 13247 /* 13248 * Set up the optional USCSI flags. See the uscsi (7I) man page 13249 * for listing of the supported flags. 13250 */ 13251 13252 if (uscmd->uscsi_flags & USCSI_SILENT) { 13253 flags |= FLAG_SILENT; 13254 } 13255 13256 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 13257 flags |= FLAG_DIAGNOSE; 13258 } 13259 13260 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 13261 flags |= FLAG_ISOLATE; 13262 } 13263 13264 if (un->un_f_is_fibre == FALSE) { 13265 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 13266 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 13267 } 13268 } 13269 13270 /* 13271 * Set the pkt flags here so we save time later. 13272 * Note: These flags are NOT in the uscsi man page!!! 13273 */ 13274 if (uscmd->uscsi_flags & USCSI_HEAD) { 13275 flags |= FLAG_HEAD; 13276 } 13277 13278 if (uscmd->uscsi_flags & USCSI_NOINTR) { 13279 flags |= FLAG_NOINTR; 13280 } 13281 13282 /* 13283 * For tagged queueing, things get a bit complicated. 13284 * Check first for head of queue and last for ordered queue. 13285 * If neither head nor order, use the default driver tag flags. 13286 */ 13287 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 13288 if (uscmd->uscsi_flags & USCSI_HTAG) { 13289 flags |= FLAG_HTAG; 13290 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 13291 flags |= FLAG_OTAG; 13292 } else { 13293 flags |= un->un_tagflags & FLAG_TAGMASK; 13294 } 13295 } 13296 13297 if (uscmd->uscsi_flags & USCSI_NODISCON) { 13298 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 13299 } 13300 13301 pktp->pkt_flags = flags; 13302 13303 /* Copy the caller's CDB into the pkt... */ 13304 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 13305 13306 if (uscmd->uscsi_timeout == 0) { 13307 pktp->pkt_time = un->un_uscsi_timeout; 13308 } else { 13309 pktp->pkt_time = uscmd->uscsi_timeout; 13310 } 13311 13312 /* need it later to identify USCSI request in sdintr */ 13313 xp->xb_pkt_flags |= SD_XB_USCSICMD; 13314 13315 xp->xb_sense_resid = uscmd->uscsi_rqresid; 13316 13317 pktp->pkt_private = bp; 13318 pktp->pkt_comp = sdintr; 13319 *pktpp = pktp; 13320 13321 SD_TRACE(SD_LOG_IO_CORE, un, 13322 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 13323 13324 return (SD_PKT_ALLOC_SUCCESS); 13325 } 13326 13327 13328 /* 13329 * Function: sd_destroypkt_for_uscsi 13330 * 13331 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 13332 * IOs.. Also saves relevant info into the associated uscsi_cmd 13333 * struct. 13334 * 13335 * Context: May be called under interrupt context 13336 */ 13337 13338 static void 13339 sd_destroypkt_for_uscsi(struct buf *bp) 13340 { 13341 struct uscsi_cmd *uscmd; 13342 struct sd_xbuf *xp; 13343 struct scsi_pkt *pktp; 13344 struct sd_lun *un; 13345 13346 ASSERT(bp != NULL); 13347 xp = SD_GET_XBUF(bp); 13348 ASSERT(xp != NULL); 13349 un = SD_GET_UN(bp); 13350 ASSERT(un != NULL); 13351 ASSERT(!mutex_owned(SD_MUTEX(un))); 13352 pktp = SD_GET_PKTP(bp); 13353 ASSERT(pktp != NULL); 13354 13355 SD_TRACE(SD_LOG_IO_CORE, un, 13356 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 13357 13358 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 13359 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 13360 ASSERT(uscmd != NULL); 13361 13362 /* Save the status and the residual into the uscsi_cmd struct */ 13363 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 13364 uscmd->uscsi_resid = bp->b_resid; 13365 13366 /* 13367 * If enabled, copy any saved sense data into the area specified 13368 * by the uscsi command. 13369 */ 13370 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 13371 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 13372 /* 13373 * Note: uscmd->uscsi_rqbuf should always point to a buffer 13374 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 13375 */ 13376 uscmd->uscsi_rqstatus = xp->xb_sense_status; 13377 uscmd->uscsi_rqresid = xp->xb_sense_resid; 13378 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, SENSE_LENGTH); 13379 } 13380 13381 /* We are done with the scsi_pkt; free it now */ 13382 ASSERT(SD_GET_PKTP(bp) != NULL); 13383 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13384 13385 SD_TRACE(SD_LOG_IO_CORE, un, 13386 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 13387 } 13388 13389 13390 /* 13391 * Function: sd_bioclone_alloc 13392 * 13393 * Description: Allocate a buf(9S) and init it as per the given buf 13394 * and the various arguments. The associated sd_xbuf 13395 * struct is (nearly) duplicated. The struct buf *bp 13396 * argument is saved in new_xp->xb_private. 13397 * 13398 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 13399 * datalen - size of data area for the shadow bp 13400 * blkno - starting LBA 13401 * func - function pointer for b_iodone in the shadow buf. (May 13402 * be NULL if none.) 13403 * 13404 * Return Code: Pointer to allocates buf(9S) struct 13405 * 13406 * Context: Can sleep. 13407 */ 13408 13409 static struct buf * 13410 sd_bioclone_alloc(struct buf *bp, size_t datalen, 13411 daddr_t blkno, int (*func)(struct buf *)) 13412 { 13413 struct sd_lun *un; 13414 struct sd_xbuf *xp; 13415 struct sd_xbuf *new_xp; 13416 struct buf *new_bp; 13417 13418 ASSERT(bp != NULL); 13419 xp = SD_GET_XBUF(bp); 13420 ASSERT(xp != NULL); 13421 un = SD_GET_UN(bp); 13422 ASSERT(un != NULL); 13423 ASSERT(!mutex_owned(SD_MUTEX(un))); 13424 13425 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 13426 NULL, KM_SLEEP); 13427 13428 new_bp->b_lblkno = blkno; 13429 13430 /* 13431 * Allocate an xbuf for the shadow bp and copy the contents of the 13432 * original xbuf into it. 13433 */ 13434 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 13435 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 13436 13437 /* 13438 * The given bp is automatically saved in the xb_private member 13439 * of the new xbuf. Callers are allowed to depend on this. 13440 */ 13441 new_xp->xb_private = bp; 13442 13443 new_bp->b_private = new_xp; 13444 13445 return (new_bp); 13446 } 13447 13448 /* 13449 * Function: sd_shadow_buf_alloc 13450 * 13451 * Description: Allocate a buf(9S) and init it as per the given buf 13452 * and the various arguments. The associated sd_xbuf 13453 * struct is (nearly) duplicated. The struct buf *bp 13454 * argument is saved in new_xp->xb_private. 13455 * 13456 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 13457 * datalen - size of data area for the shadow bp 13458 * bflags - B_READ or B_WRITE (pseudo flag) 13459 * blkno - starting LBA 13460 * func - function pointer for b_iodone in the shadow buf. (May 13461 * be NULL if none.) 13462 * 13463 * Return Code: Pointer to allocates buf(9S) struct 13464 * 13465 * Context: Can sleep. 13466 */ 13467 13468 static struct buf * 13469 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 13470 daddr_t blkno, int (*func)(struct buf *)) 13471 { 13472 struct sd_lun *un; 13473 struct sd_xbuf *xp; 13474 struct sd_xbuf *new_xp; 13475 struct buf *new_bp; 13476 13477 ASSERT(bp != NULL); 13478 xp = SD_GET_XBUF(bp); 13479 ASSERT(xp != NULL); 13480 un = SD_GET_UN(bp); 13481 ASSERT(un != NULL); 13482 ASSERT(!mutex_owned(SD_MUTEX(un))); 13483 13484 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 13485 bp_mapin(bp); 13486 } 13487 13488 bflags &= (B_READ | B_WRITE); 13489 #if defined(__i386) || defined(__amd64) 13490 new_bp = getrbuf(KM_SLEEP); 13491 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 13492 new_bp->b_bcount = datalen; 13493 new_bp->b_flags = bp->b_flags | bflags; 13494 #else 13495 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 13496 datalen, bflags, SLEEP_FUNC, NULL); 13497 #endif 13498 new_bp->av_forw = NULL; 13499 new_bp->av_back = NULL; 13500 new_bp->b_dev = bp->b_dev; 13501 new_bp->b_blkno = blkno; 13502 new_bp->b_iodone = func; 13503 new_bp->b_edev = bp->b_edev; 13504 new_bp->b_resid = 0; 13505 13506 /* We need to preserve the B_FAILFAST flag */ 13507 if (bp->b_flags & B_FAILFAST) { 13508 new_bp->b_flags |= B_FAILFAST; 13509 } 13510 13511 /* 13512 * Allocate an xbuf for the shadow bp and copy the contents of the 13513 * original xbuf into it. 13514 */ 13515 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 13516 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 13517 13518 /* Need later to copy data between the shadow buf & original buf! */ 13519 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 13520 13521 /* 13522 * The given bp is automatically saved in the xb_private member 13523 * of the new xbuf. Callers are allowed to depend on this. 13524 */ 13525 new_xp->xb_private = bp; 13526 13527 new_bp->b_private = new_xp; 13528 13529 return (new_bp); 13530 } 13531 13532 /* 13533 * Function: sd_bioclone_free 13534 * 13535 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 13536 * in the larger than partition operation. 13537 * 13538 * Context: May be called under interrupt context 13539 */ 13540 13541 static void 13542 sd_bioclone_free(struct buf *bp) 13543 { 13544 struct sd_xbuf *xp; 13545 13546 ASSERT(bp != NULL); 13547 xp = SD_GET_XBUF(bp); 13548 ASSERT(xp != NULL); 13549 13550 /* 13551 * Call bp_mapout() before freeing the buf, in case a lower 13552 * layer or HBA had done a bp_mapin(). we must do this here 13553 * as we are the "originator" of the shadow buf. 13554 */ 13555 bp_mapout(bp); 13556 13557 /* 13558 * Null out b_iodone before freeing the bp, to ensure that the driver 13559 * never gets confused by a stale value in this field. (Just a little 13560 * extra defensiveness here.) 13561 */ 13562 bp->b_iodone = NULL; 13563 13564 freerbuf(bp); 13565 13566 kmem_free(xp, sizeof (struct sd_xbuf)); 13567 } 13568 13569 /* 13570 * Function: sd_shadow_buf_free 13571 * 13572 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 13573 * 13574 * Context: May be called under interrupt context 13575 */ 13576 13577 static void 13578 sd_shadow_buf_free(struct buf *bp) 13579 { 13580 struct sd_xbuf *xp; 13581 13582 ASSERT(bp != NULL); 13583 xp = SD_GET_XBUF(bp); 13584 ASSERT(xp != NULL); 13585 13586 #if defined(__sparc) 13587 /* 13588 * Call bp_mapout() before freeing the buf, in case a lower 13589 * layer or HBA had done a bp_mapin(). we must do this here 13590 * as we are the "originator" of the shadow buf. 13591 */ 13592 bp_mapout(bp); 13593 #endif 13594 13595 /* 13596 * Null out b_iodone before freeing the bp, to ensure that the driver 13597 * never gets confused by a stale value in this field. (Just a little 13598 * extra defensiveness here.) 13599 */ 13600 bp->b_iodone = NULL; 13601 13602 #if defined(__i386) || defined(__amd64) 13603 kmem_free(bp->b_un.b_addr, bp->b_bcount); 13604 freerbuf(bp); 13605 #else 13606 scsi_free_consistent_buf(bp); 13607 #endif 13608 13609 kmem_free(xp, sizeof (struct sd_xbuf)); 13610 } 13611 13612 13613 /* 13614 * Function: sd_print_transport_rejected_message 13615 * 13616 * Description: This implements the ludicrously complex rules for printing 13617 * a "transport rejected" message. This is to address the 13618 * specific problem of having a flood of this error message 13619 * produced when a failover occurs. 13620 * 13621 * Context: Any. 13622 */ 13623 13624 static void 13625 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 13626 int code) 13627 { 13628 ASSERT(un != NULL); 13629 ASSERT(mutex_owned(SD_MUTEX(un))); 13630 ASSERT(xp != NULL); 13631 13632 /* 13633 * Print the "transport rejected" message under the following 13634 * conditions: 13635 * 13636 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 13637 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 13638 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 13639 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 13640 * scsi_transport(9F) (which indicates that the target might have 13641 * gone off-line). This uses the un->un_tran_fatal_count 13642 * count, which is incremented whenever a TRAN_FATAL_ERROR is 13643 * received, and reset to zero whenver a TRAN_ACCEPT is returned 13644 * from scsi_transport(). 13645 * 13646 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 13647 * the preceeding cases in order for the message to be printed. 13648 */ 13649 if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) { 13650 if ((sd_level_mask & SD_LOGMASK_DIAG) || 13651 (code != TRAN_FATAL_ERROR) || 13652 (un->un_tran_fatal_count == 1)) { 13653 switch (code) { 13654 case TRAN_BADPKT: 13655 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13656 "transport rejected bad packet\n"); 13657 break; 13658 case TRAN_FATAL_ERROR: 13659 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13660 "transport rejected fatal error\n"); 13661 break; 13662 default: 13663 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13664 "transport rejected (%d)\n", code); 13665 break; 13666 } 13667 } 13668 } 13669 } 13670 13671 13672 /* 13673 * Function: sd_add_buf_to_waitq 13674 * 13675 * Description: Add the given buf(9S) struct to the wait queue for the 13676 * instance. If sorting is enabled, then the buf is added 13677 * to the queue via an elevator sort algorithm (a la 13678 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 13679 * If sorting is not enabled, then the buf is just added 13680 * to the end of the wait queue. 13681 * 13682 * Return Code: void 13683 * 13684 * Context: Does not sleep/block, therefore technically can be called 13685 * from any context. However if sorting is enabled then the 13686 * execution time is indeterminate, and may take long if 13687 * the wait queue grows large. 13688 */ 13689 13690 static void 13691 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 13692 { 13693 struct buf *ap; 13694 13695 ASSERT(bp != NULL); 13696 ASSERT(un != NULL); 13697 ASSERT(mutex_owned(SD_MUTEX(un))); 13698 13699 /* If the queue is empty, add the buf as the only entry & return. */ 13700 if (un->un_waitq_headp == NULL) { 13701 ASSERT(un->un_waitq_tailp == NULL); 13702 un->un_waitq_headp = un->un_waitq_tailp = bp; 13703 bp->av_forw = NULL; 13704 return; 13705 } 13706 13707 ASSERT(un->un_waitq_tailp != NULL); 13708 13709 /* 13710 * If sorting is disabled, just add the buf to the tail end of 13711 * the wait queue and return. 13712 */ 13713 if (un->un_f_disksort_disabled) { 13714 un->un_waitq_tailp->av_forw = bp; 13715 un->un_waitq_tailp = bp; 13716 bp->av_forw = NULL; 13717 return; 13718 } 13719 13720 /* 13721 * Sort thru the list of requests currently on the wait queue 13722 * and add the new buf request at the appropriate position. 13723 * 13724 * The un->un_waitq_headp is an activity chain pointer on which 13725 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 13726 * first queue holds those requests which are positioned after 13727 * the current SD_GET_BLKNO() (in the first request); the second holds 13728 * requests which came in after their SD_GET_BLKNO() number was passed. 13729 * Thus we implement a one way scan, retracting after reaching 13730 * the end of the drive to the first request on the second 13731 * queue, at which time it becomes the first queue. 13732 * A one-way scan is natural because of the way UNIX read-ahead 13733 * blocks are allocated. 13734 * 13735 * If we lie after the first request, then we must locate the 13736 * second request list and add ourselves to it. 13737 */ 13738 ap = un->un_waitq_headp; 13739 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 13740 while (ap->av_forw != NULL) { 13741 /* 13742 * Look for an "inversion" in the (normally 13743 * ascending) block numbers. This indicates 13744 * the start of the second request list. 13745 */ 13746 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 13747 /* 13748 * Search the second request list for the 13749 * first request at a larger block number. 13750 * We go before that; however if there is 13751 * no such request, we go at the end. 13752 */ 13753 do { 13754 if (SD_GET_BLKNO(bp) < 13755 SD_GET_BLKNO(ap->av_forw)) { 13756 goto insert; 13757 } 13758 ap = ap->av_forw; 13759 } while (ap->av_forw != NULL); 13760 goto insert; /* after last */ 13761 } 13762 ap = ap->av_forw; 13763 } 13764 13765 /* 13766 * No inversions... we will go after the last, and 13767 * be the first request in the second request list. 13768 */ 13769 goto insert; 13770 } 13771 13772 /* 13773 * Request is at/after the current request... 13774 * sort in the first request list. 13775 */ 13776 while (ap->av_forw != NULL) { 13777 /* 13778 * We want to go after the current request (1) if 13779 * there is an inversion after it (i.e. it is the end 13780 * of the first request list), or (2) if the next 13781 * request is a larger block no. than our request. 13782 */ 13783 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 13784 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 13785 goto insert; 13786 } 13787 ap = ap->av_forw; 13788 } 13789 13790 /* 13791 * Neither a second list nor a larger request, therefore 13792 * we go at the end of the first list (which is the same 13793 * as the end of the whole schebang). 13794 */ 13795 insert: 13796 bp->av_forw = ap->av_forw; 13797 ap->av_forw = bp; 13798 13799 /* 13800 * If we inserted onto the tail end of the waitq, make sure the 13801 * tail pointer is updated. 13802 */ 13803 if (ap == un->un_waitq_tailp) { 13804 un->un_waitq_tailp = bp; 13805 } 13806 } 13807 13808 13809 /* 13810 * Function: sd_start_cmds 13811 * 13812 * Description: Remove and transport cmds from the driver queues. 13813 * 13814 * Arguments: un - pointer to the unit (soft state) struct for the target. 13815 * 13816 * immed_bp - ptr to a buf to be transported immediately. Only 13817 * the immed_bp is transported; bufs on the waitq are not 13818 * processed and the un_retry_bp is not checked. If immed_bp is 13819 * NULL, then normal queue processing is performed. 13820 * 13821 * Context: May be called from kernel thread context, interrupt context, 13822 * or runout callback context. This function may not block or 13823 * call routines that block. 13824 */ 13825 13826 static void 13827 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 13828 { 13829 struct sd_xbuf *xp; 13830 struct buf *bp; 13831 void (*statp)(kstat_io_t *); 13832 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13833 void (*saved_statp)(kstat_io_t *); 13834 #endif 13835 int rval; 13836 13837 ASSERT(un != NULL); 13838 ASSERT(mutex_owned(SD_MUTEX(un))); 13839 ASSERT(un->un_ncmds_in_transport >= 0); 13840 ASSERT(un->un_throttle >= 0); 13841 13842 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 13843 13844 do { 13845 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13846 saved_statp = NULL; 13847 #endif 13848 13849 /* 13850 * If we are syncing or dumping, fail the command to 13851 * avoid recursively calling back into scsi_transport(). 13852 */ 13853 if (ddi_in_panic()) { 13854 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13855 "sd_start_cmds: panicking\n"); 13856 goto exit; 13857 } 13858 13859 if ((bp = immed_bp) != NULL) { 13860 /* 13861 * We have a bp that must be transported immediately. 13862 * It's OK to transport the immed_bp here without doing 13863 * the throttle limit check because the immed_bp is 13864 * always used in a retry/recovery case. This means 13865 * that we know we are not at the throttle limit by 13866 * virtue of the fact that to get here we must have 13867 * already gotten a command back via sdintr(). This also 13868 * relies on (1) the command on un_retry_bp preventing 13869 * further commands from the waitq from being issued; 13870 * and (2) the code in sd_retry_command checking the 13871 * throttle limit before issuing a delayed or immediate 13872 * retry. This holds even if the throttle limit is 13873 * currently ratcheted down from its maximum value. 13874 */ 13875 statp = kstat_runq_enter; 13876 if (bp == un->un_retry_bp) { 13877 ASSERT((un->un_retry_statp == NULL) || 13878 (un->un_retry_statp == kstat_waitq_enter) || 13879 (un->un_retry_statp == 13880 kstat_runq_back_to_waitq)); 13881 /* 13882 * If the waitq kstat was incremented when 13883 * sd_set_retry_bp() queued this bp for a retry, 13884 * then we must set up statp so that the waitq 13885 * count will get decremented correctly below. 13886 * Also we must clear un->un_retry_statp to 13887 * ensure that we do not act on a stale value 13888 * in this field. 13889 */ 13890 if ((un->un_retry_statp == kstat_waitq_enter) || 13891 (un->un_retry_statp == 13892 kstat_runq_back_to_waitq)) { 13893 statp = kstat_waitq_to_runq; 13894 } 13895 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13896 saved_statp = un->un_retry_statp; 13897 #endif 13898 un->un_retry_statp = NULL; 13899 13900 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 13901 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 13902 "un_throttle:%d un_ncmds_in_transport:%d\n", 13903 un, un->un_retry_bp, un->un_throttle, 13904 un->un_ncmds_in_transport); 13905 } else { 13906 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 13907 "processing priority bp:0x%p\n", bp); 13908 } 13909 13910 } else if ((bp = un->un_waitq_headp) != NULL) { 13911 /* 13912 * A command on the waitq is ready to go, but do not 13913 * send it if: 13914 * 13915 * (1) the throttle limit has been reached, or 13916 * (2) a retry is pending, or 13917 * (3) a START_STOP_UNIT callback pending, or 13918 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 13919 * command is pending. 13920 * 13921 * For all of these conditions, IO processing will 13922 * restart after the condition is cleared. 13923 */ 13924 if (un->un_ncmds_in_transport >= un->un_throttle) { 13925 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13926 "sd_start_cmds: exiting, " 13927 "throttle limit reached!\n"); 13928 goto exit; 13929 } 13930 if (un->un_retry_bp != NULL) { 13931 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13932 "sd_start_cmds: exiting, retry pending!\n"); 13933 goto exit; 13934 } 13935 if (un->un_startstop_timeid != NULL) { 13936 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13937 "sd_start_cmds: exiting, " 13938 "START_STOP pending!\n"); 13939 goto exit; 13940 } 13941 if (un->un_direct_priority_timeid != NULL) { 13942 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13943 "sd_start_cmds: exiting, " 13944 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 13945 goto exit; 13946 } 13947 13948 /* Dequeue the command */ 13949 un->un_waitq_headp = bp->av_forw; 13950 if (un->un_waitq_headp == NULL) { 13951 un->un_waitq_tailp = NULL; 13952 } 13953 bp->av_forw = NULL; 13954 statp = kstat_waitq_to_runq; 13955 SD_TRACE(SD_LOG_IO_CORE, un, 13956 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 13957 13958 } else { 13959 /* No work to do so bail out now */ 13960 SD_TRACE(SD_LOG_IO_CORE, un, 13961 "sd_start_cmds: no more work, exiting!\n"); 13962 goto exit; 13963 } 13964 13965 /* 13966 * Reset the state to normal. This is the mechanism by which 13967 * the state transitions from either SD_STATE_RWAIT or 13968 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 13969 * If state is SD_STATE_PM_CHANGING then this command is 13970 * part of the device power control and the state must 13971 * not be put back to normal. Doing so would would 13972 * allow new commands to proceed when they shouldn't, 13973 * the device may be going off. 13974 */ 13975 if ((un->un_state != SD_STATE_SUSPENDED) && 13976 (un->un_state != SD_STATE_PM_CHANGING)) { 13977 New_state(un, SD_STATE_NORMAL); 13978 } 13979 13980 xp = SD_GET_XBUF(bp); 13981 ASSERT(xp != NULL); 13982 13983 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13984 /* 13985 * Allocate the scsi_pkt if we need one, or attach DMA 13986 * resources if we have a scsi_pkt that needs them. The 13987 * latter should only occur for commands that are being 13988 * retried. 13989 */ 13990 if ((xp->xb_pktp == NULL) || 13991 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 13992 #else 13993 if (xp->xb_pktp == NULL) { 13994 #endif 13995 /* 13996 * There is no scsi_pkt allocated for this buf. Call 13997 * the initpkt function to allocate & init one. 13998 * 13999 * The scsi_init_pkt runout callback functionality is 14000 * implemented as follows: 14001 * 14002 * 1) The initpkt function always calls 14003 * scsi_init_pkt(9F) with sdrunout specified as the 14004 * callback routine. 14005 * 2) A successful packet allocation is initialized and 14006 * the I/O is transported. 14007 * 3) The I/O associated with an allocation resource 14008 * failure is left on its queue to be retried via 14009 * runout or the next I/O. 14010 * 4) The I/O associated with a DMA error is removed 14011 * from the queue and failed with EIO. Processing of 14012 * the transport queues is also halted to be 14013 * restarted via runout or the next I/O. 14014 * 5) The I/O associated with a CDB size or packet 14015 * size error is removed from the queue and failed 14016 * with EIO. Processing of the transport queues is 14017 * continued. 14018 * 14019 * Note: there is no interface for canceling a runout 14020 * callback. To prevent the driver from detaching or 14021 * suspending while a runout is pending the driver 14022 * state is set to SD_STATE_RWAIT 14023 * 14024 * Note: using the scsi_init_pkt callback facility can 14025 * result in an I/O request persisting at the head of 14026 * the list which cannot be satisfied even after 14027 * multiple retries. In the future the driver may 14028 * implement some kind of maximum runout count before 14029 * failing an I/O. 14030 * 14031 * Note: the use of funcp below may seem superfluous, 14032 * but it helps warlock figure out the correct 14033 * initpkt function calls (see [s]sd.wlcmd). 14034 */ 14035 struct scsi_pkt *pktp; 14036 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 14037 14038 ASSERT(bp != un->un_rqs_bp); 14039 14040 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 14041 switch ((*funcp)(bp, &pktp)) { 14042 case SD_PKT_ALLOC_SUCCESS: 14043 xp->xb_pktp = pktp; 14044 SD_TRACE(SD_LOG_IO_CORE, un, 14045 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 14046 pktp); 14047 goto got_pkt; 14048 14049 case SD_PKT_ALLOC_FAILURE: 14050 /* 14051 * Temporary (hopefully) resource depletion. 14052 * Since retries and RQS commands always have a 14053 * scsi_pkt allocated, these cases should never 14054 * get here. So the only cases this needs to 14055 * handle is a bp from the waitq (which we put 14056 * back onto the waitq for sdrunout), or a bp 14057 * sent as an immed_bp (which we just fail). 14058 */ 14059 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14060 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 14061 14062 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14063 14064 if (bp == immed_bp) { 14065 /* 14066 * If SD_XB_DMA_FREED is clear, then 14067 * this is a failure to allocate a 14068 * scsi_pkt, and we must fail the 14069 * command. 14070 */ 14071 if ((xp->xb_pkt_flags & 14072 SD_XB_DMA_FREED) == 0) { 14073 break; 14074 } 14075 14076 /* 14077 * If this immediate command is NOT our 14078 * un_retry_bp, then we must fail it. 14079 */ 14080 if (bp != un->un_retry_bp) { 14081 break; 14082 } 14083 14084 /* 14085 * We get here if this cmd is our 14086 * un_retry_bp that was DMAFREED, but 14087 * scsi_init_pkt() failed to reallocate 14088 * DMA resources when we attempted to 14089 * retry it. This can happen when an 14090 * mpxio failover is in progress, but 14091 * we don't want to just fail the 14092 * command in this case. 14093 * 14094 * Use timeout(9F) to restart it after 14095 * a 100ms delay. We don't want to 14096 * let sdrunout() restart it, because 14097 * sdrunout() is just supposed to start 14098 * commands that are sitting on the 14099 * wait queue. The un_retry_bp stays 14100 * set until the command completes, but 14101 * sdrunout can be called many times 14102 * before that happens. Since sdrunout 14103 * cannot tell if the un_retry_bp is 14104 * already in the transport, it could 14105 * end up calling scsi_transport() for 14106 * the un_retry_bp multiple times. 14107 * 14108 * Also: don't schedule the callback 14109 * if some other callback is already 14110 * pending. 14111 */ 14112 if (un->un_retry_statp == NULL) { 14113 /* 14114 * restore the kstat pointer to 14115 * keep kstat counts coherent 14116 * when we do retry the command. 14117 */ 14118 un->un_retry_statp = 14119 saved_statp; 14120 } 14121 14122 if ((un->un_startstop_timeid == NULL) && 14123 (un->un_retry_timeid == NULL) && 14124 (un->un_direct_priority_timeid == 14125 NULL)) { 14126 14127 un->un_retry_timeid = 14128 timeout( 14129 sd_start_retry_command, 14130 un, SD_RESTART_TIMEOUT); 14131 } 14132 goto exit; 14133 } 14134 14135 #else 14136 if (bp == immed_bp) { 14137 break; /* Just fail the command */ 14138 } 14139 #endif 14140 14141 /* Add the buf back to the head of the waitq */ 14142 bp->av_forw = un->un_waitq_headp; 14143 un->un_waitq_headp = bp; 14144 if (un->un_waitq_tailp == NULL) { 14145 un->un_waitq_tailp = bp; 14146 } 14147 goto exit; 14148 14149 case SD_PKT_ALLOC_FAILURE_NO_DMA: 14150 /* 14151 * HBA DMA resource failure. Fail the command 14152 * and continue processing of the queues. 14153 */ 14154 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14155 "sd_start_cmds: " 14156 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 14157 break; 14158 14159 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 14160 /* 14161 * Note:x86: Partial DMA mapping not supported 14162 * for USCSI commands, and all the needed DMA 14163 * resources were not allocated. 14164 */ 14165 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14166 "sd_start_cmds: " 14167 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 14168 break; 14169 14170 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 14171 /* 14172 * Note:x86: Request cannot fit into CDB based 14173 * on lba and len. 14174 */ 14175 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14176 "sd_start_cmds: " 14177 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 14178 break; 14179 14180 default: 14181 /* Should NEVER get here! */ 14182 panic("scsi_initpkt error"); 14183 /*NOTREACHED*/ 14184 } 14185 14186 /* 14187 * Fatal error in allocating a scsi_pkt for this buf. 14188 * Update kstats & return the buf with an error code. 14189 * We must use sd_return_failed_command_no_restart() to 14190 * avoid a recursive call back into sd_start_cmds(). 14191 * However this also means that we must keep processing 14192 * the waitq here in order to avoid stalling. 14193 */ 14194 if (statp == kstat_waitq_to_runq) { 14195 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 14196 } 14197 sd_return_failed_command_no_restart(un, bp, EIO); 14198 if (bp == immed_bp) { 14199 /* immed_bp is gone by now, so clear this */ 14200 immed_bp = NULL; 14201 } 14202 continue; 14203 } 14204 got_pkt: 14205 if (bp == immed_bp) { 14206 /* goto the head of the class.... */ 14207 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 14208 } 14209 14210 un->un_ncmds_in_transport++; 14211 SD_UPDATE_KSTATS(un, statp, bp); 14212 14213 /* 14214 * Call scsi_transport() to send the command to the target. 14215 * According to SCSA architecture, we must drop the mutex here 14216 * before calling scsi_transport() in order to avoid deadlock. 14217 * Note that the scsi_pkt's completion routine can be executed 14218 * (from interrupt context) even before the call to 14219 * scsi_transport() returns. 14220 */ 14221 SD_TRACE(SD_LOG_IO_CORE, un, 14222 "sd_start_cmds: calling scsi_transport()\n"); 14223 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 14224 14225 mutex_exit(SD_MUTEX(un)); 14226 rval = scsi_transport(xp->xb_pktp); 14227 mutex_enter(SD_MUTEX(un)); 14228 14229 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14230 "sd_start_cmds: scsi_transport() returned %d\n", rval); 14231 14232 switch (rval) { 14233 case TRAN_ACCEPT: 14234 /* Clear this with every pkt accepted by the HBA */ 14235 un->un_tran_fatal_count = 0; 14236 break; /* Success; try the next cmd (if any) */ 14237 14238 case TRAN_BUSY: 14239 un->un_ncmds_in_transport--; 14240 ASSERT(un->un_ncmds_in_transport >= 0); 14241 14242 /* 14243 * Don't retry request sense, the sense data 14244 * is lost when another request is sent. 14245 * Free up the rqs buf and retry 14246 * the original failed cmd. Update kstat. 14247 */ 14248 if (bp == un->un_rqs_bp) { 14249 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14250 bp = sd_mark_rqs_idle(un, xp); 14251 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 14252 NULL, NULL, EIO, SD_BSY_TIMEOUT / 500, 14253 kstat_waitq_enter); 14254 goto exit; 14255 } 14256 14257 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14258 /* 14259 * Free the DMA resources for the scsi_pkt. This will 14260 * allow mpxio to select another path the next time 14261 * we call scsi_transport() with this scsi_pkt. 14262 * See sdintr() for the rationalization behind this. 14263 */ 14264 if ((un->un_f_is_fibre == TRUE) && 14265 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 14266 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 14267 scsi_dmafree(xp->xb_pktp); 14268 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 14269 } 14270 #endif 14271 14272 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 14273 /* 14274 * Commands that are SD_PATH_DIRECT_PRIORITY 14275 * are for error recovery situations. These do 14276 * not use the normal command waitq, so if they 14277 * get a TRAN_BUSY we cannot put them back onto 14278 * the waitq for later retry. One possible 14279 * problem is that there could already be some 14280 * other command on un_retry_bp that is waiting 14281 * for this one to complete, so we would be 14282 * deadlocked if we put this command back onto 14283 * the waitq for later retry (since un_retry_bp 14284 * must complete before the driver gets back to 14285 * commands on the waitq). 14286 * 14287 * To avoid deadlock we must schedule a callback 14288 * that will restart this command after a set 14289 * interval. This should keep retrying for as 14290 * long as the underlying transport keeps 14291 * returning TRAN_BUSY (just like for other 14292 * commands). Use the same timeout interval as 14293 * for the ordinary TRAN_BUSY retry. 14294 */ 14295 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14296 "sd_start_cmds: scsi_transport() returned " 14297 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 14298 14299 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14300 un->un_direct_priority_timeid = 14301 timeout(sd_start_direct_priority_command, 14302 bp, SD_BSY_TIMEOUT / 500); 14303 14304 goto exit; 14305 } 14306 14307 /* 14308 * For TRAN_BUSY, we want to reduce the throttle value, 14309 * unless we are retrying a command. 14310 */ 14311 if (bp != un->un_retry_bp) { 14312 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 14313 } 14314 14315 /* 14316 * Set up the bp to be tried again 10 ms later. 14317 * Note:x86: Is there a timeout value in the sd_lun 14318 * for this condition? 14319 */ 14320 sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500, 14321 kstat_runq_back_to_waitq); 14322 goto exit; 14323 14324 case TRAN_FATAL_ERROR: 14325 un->un_tran_fatal_count++; 14326 /* FALLTHRU */ 14327 14328 case TRAN_BADPKT: 14329 default: 14330 un->un_ncmds_in_transport--; 14331 ASSERT(un->un_ncmds_in_transport >= 0); 14332 14333 /* 14334 * If this is our REQUEST SENSE command with a 14335 * transport error, we must get back the pointers 14336 * to the original buf, and mark the REQUEST 14337 * SENSE command as "available". 14338 */ 14339 if (bp == un->un_rqs_bp) { 14340 bp = sd_mark_rqs_idle(un, xp); 14341 xp = SD_GET_XBUF(bp); 14342 } else { 14343 /* 14344 * Legacy behavior: do not update transport 14345 * error count for request sense commands. 14346 */ 14347 SD_UPDATE_ERRSTATS(un, sd_transerrs); 14348 } 14349 14350 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14351 sd_print_transport_rejected_message(un, xp, rval); 14352 14353 /* 14354 * We must use sd_return_failed_command_no_restart() to 14355 * avoid a recursive call back into sd_start_cmds(). 14356 * However this also means that we must keep processing 14357 * the waitq here in order to avoid stalling. 14358 */ 14359 sd_return_failed_command_no_restart(un, bp, EIO); 14360 14361 /* 14362 * Notify any threads waiting in sd_ddi_suspend() that 14363 * a command completion has occurred. 14364 */ 14365 if (un->un_state == SD_STATE_SUSPENDED) { 14366 cv_broadcast(&un->un_disk_busy_cv); 14367 } 14368 14369 if (bp == immed_bp) { 14370 /* immed_bp is gone by now, so clear this */ 14371 immed_bp = NULL; 14372 } 14373 break; 14374 } 14375 14376 } while (immed_bp == NULL); 14377 14378 exit: 14379 ASSERT(mutex_owned(SD_MUTEX(un))); 14380 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 14381 } 14382 14383 14384 /* 14385 * Function: sd_return_command 14386 * 14387 * Description: Returns a command to its originator (with or without an 14388 * error). Also starts commands waiting to be transported 14389 * to the target. 14390 * 14391 * Context: May be called from interrupt, kernel, or timeout context 14392 */ 14393 14394 static void 14395 sd_return_command(struct sd_lun *un, struct buf *bp) 14396 { 14397 struct sd_xbuf *xp; 14398 #if defined(__i386) || defined(__amd64) 14399 struct scsi_pkt *pktp; 14400 #endif 14401 14402 ASSERT(bp != NULL); 14403 ASSERT(un != NULL); 14404 ASSERT(mutex_owned(SD_MUTEX(un))); 14405 ASSERT(bp != un->un_rqs_bp); 14406 xp = SD_GET_XBUF(bp); 14407 ASSERT(xp != NULL); 14408 14409 #if defined(__i386) || defined(__amd64) 14410 pktp = SD_GET_PKTP(bp); 14411 #endif 14412 14413 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 14414 14415 #if defined(__i386) || defined(__amd64) 14416 /* 14417 * Note:x86: check for the "sdrestart failed" case. 14418 */ 14419 if (((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 14420 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 14421 (xp->xb_pktp->pkt_resid == 0)) { 14422 14423 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 14424 /* 14425 * Successfully set up next portion of cmd 14426 * transfer, try sending it 14427 */ 14428 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 14429 NULL, NULL, 0, (clock_t)0, NULL); 14430 sd_start_cmds(un, NULL); 14431 return; /* Note:x86: need a return here? */ 14432 } 14433 } 14434 #endif 14435 14436 /* 14437 * If this is the failfast bp, clear it from un_failfast_bp. This 14438 * can happen if upon being re-tried the failfast bp either 14439 * succeeded or encountered another error (possibly even a different 14440 * error than the one that precipitated the failfast state, but in 14441 * that case it would have had to exhaust retries as well). Regardless, 14442 * this should not occur whenever the instance is in the active 14443 * failfast state. 14444 */ 14445 if (bp == un->un_failfast_bp) { 14446 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 14447 un->un_failfast_bp = NULL; 14448 } 14449 14450 /* 14451 * Clear the failfast state upon successful completion of ANY cmd. 14452 */ 14453 if (bp->b_error == 0) { 14454 un->un_failfast_state = SD_FAILFAST_INACTIVE; 14455 } 14456 14457 /* 14458 * This is used if the command was retried one or more times. Show that 14459 * we are done with it, and allow processing of the waitq to resume. 14460 */ 14461 if (bp == un->un_retry_bp) { 14462 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14463 "sd_return_command: un:0x%p: " 14464 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 14465 un->un_retry_bp = NULL; 14466 un->un_retry_statp = NULL; 14467 } 14468 14469 SD_UPDATE_RDWR_STATS(un, bp); 14470 SD_UPDATE_PARTITION_STATS(un, bp); 14471 14472 switch (un->un_state) { 14473 case SD_STATE_SUSPENDED: 14474 /* 14475 * Notify any threads waiting in sd_ddi_suspend() that 14476 * a command completion has occurred. 14477 */ 14478 cv_broadcast(&un->un_disk_busy_cv); 14479 break; 14480 default: 14481 sd_start_cmds(un, NULL); 14482 break; 14483 } 14484 14485 /* Return this command up the iodone chain to its originator. */ 14486 mutex_exit(SD_MUTEX(un)); 14487 14488 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 14489 xp->xb_pktp = NULL; 14490 14491 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 14492 14493 ASSERT(!mutex_owned(SD_MUTEX(un))); 14494 mutex_enter(SD_MUTEX(un)); 14495 14496 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 14497 } 14498 14499 14500 /* 14501 * Function: sd_return_failed_command 14502 * 14503 * Description: Command completion when an error occurred. 14504 * 14505 * Context: May be called from interrupt context 14506 */ 14507 14508 static void 14509 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 14510 { 14511 ASSERT(bp != NULL); 14512 ASSERT(un != NULL); 14513 ASSERT(mutex_owned(SD_MUTEX(un))); 14514 14515 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14516 "sd_return_failed_command: entry\n"); 14517 14518 /* 14519 * b_resid could already be nonzero due to a partial data 14520 * transfer, so do not change it here. 14521 */ 14522 SD_BIOERROR(bp, errcode); 14523 14524 sd_return_command(un, bp); 14525 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14526 "sd_return_failed_command: exit\n"); 14527 } 14528 14529 14530 /* 14531 * Function: sd_return_failed_command_no_restart 14532 * 14533 * Description: Same as sd_return_failed_command, but ensures that no 14534 * call back into sd_start_cmds will be issued. 14535 * 14536 * Context: May be called from interrupt context 14537 */ 14538 14539 static void 14540 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 14541 int errcode) 14542 { 14543 struct sd_xbuf *xp; 14544 14545 ASSERT(bp != NULL); 14546 ASSERT(un != NULL); 14547 ASSERT(mutex_owned(SD_MUTEX(un))); 14548 xp = SD_GET_XBUF(bp); 14549 ASSERT(xp != NULL); 14550 ASSERT(errcode != 0); 14551 14552 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14553 "sd_return_failed_command_no_restart: entry\n"); 14554 14555 /* 14556 * b_resid could already be nonzero due to a partial data 14557 * transfer, so do not change it here. 14558 */ 14559 SD_BIOERROR(bp, errcode); 14560 14561 /* 14562 * If this is the failfast bp, clear it. This can happen if the 14563 * failfast bp encounterd a fatal error when we attempted to 14564 * re-try it (such as a scsi_transport(9F) failure). However 14565 * we should NOT be in an active failfast state if the failfast 14566 * bp is not NULL. 14567 */ 14568 if (bp == un->un_failfast_bp) { 14569 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 14570 un->un_failfast_bp = NULL; 14571 } 14572 14573 if (bp == un->un_retry_bp) { 14574 /* 14575 * This command was retried one or more times. Show that we are 14576 * done with it, and allow processing of the waitq to resume. 14577 */ 14578 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14579 "sd_return_failed_command_no_restart: " 14580 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 14581 un->un_retry_bp = NULL; 14582 un->un_retry_statp = NULL; 14583 } 14584 14585 SD_UPDATE_RDWR_STATS(un, bp); 14586 SD_UPDATE_PARTITION_STATS(un, bp); 14587 14588 mutex_exit(SD_MUTEX(un)); 14589 14590 if (xp->xb_pktp != NULL) { 14591 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 14592 xp->xb_pktp = NULL; 14593 } 14594 14595 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 14596 14597 mutex_enter(SD_MUTEX(un)); 14598 14599 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14600 "sd_return_failed_command_no_restart: exit\n"); 14601 } 14602 14603 14604 /* 14605 * Function: sd_retry_command 14606 * 14607 * Description: queue up a command for retry, or (optionally) fail it 14608 * if retry counts are exhausted. 14609 * 14610 * Arguments: un - Pointer to the sd_lun struct for the target. 14611 * 14612 * bp - Pointer to the buf for the command to be retried. 14613 * 14614 * retry_check_flag - Flag to see which (if any) of the retry 14615 * counts should be decremented/checked. If the indicated 14616 * retry count is exhausted, then the command will not be 14617 * retried; it will be failed instead. This should use a 14618 * value equal to one of the following: 14619 * 14620 * SD_RETRIES_NOCHECK 14621 * SD_RESD_RETRIES_STANDARD 14622 * SD_RETRIES_VICTIM 14623 * 14624 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 14625 * if the check should be made to see of FLAG_ISOLATE is set 14626 * in the pkt. If FLAG_ISOLATE is set, then the command is 14627 * not retried, it is simply failed. 14628 * 14629 * user_funcp - Ptr to function to call before dispatching the 14630 * command. May be NULL if no action needs to be performed. 14631 * (Primarily intended for printing messages.) 14632 * 14633 * user_arg - Optional argument to be passed along to 14634 * the user_funcp call. 14635 * 14636 * failure_code - errno return code to set in the bp if the 14637 * command is going to be failed. 14638 * 14639 * retry_delay - Retry delay interval in (clock_t) units. May 14640 * be zero which indicates that the retry should be retried 14641 * immediately (ie, without an intervening delay). 14642 * 14643 * statp - Ptr to kstat function to be updated if the command 14644 * is queued for a delayed retry. May be NULL if no kstat 14645 * update is desired. 14646 * 14647 * Context: May be called from interupt context. 14648 */ 14649 14650 static void 14651 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 14652 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 14653 code), void *user_arg, int failure_code, clock_t retry_delay, 14654 void (*statp)(kstat_io_t *)) 14655 { 14656 struct sd_xbuf *xp; 14657 struct scsi_pkt *pktp; 14658 14659 ASSERT(un != NULL); 14660 ASSERT(mutex_owned(SD_MUTEX(un))); 14661 ASSERT(bp != NULL); 14662 xp = SD_GET_XBUF(bp); 14663 ASSERT(xp != NULL); 14664 pktp = SD_GET_PKTP(bp); 14665 ASSERT(pktp != NULL); 14666 14667 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14668 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 14669 14670 /* 14671 * If we are syncing or dumping, fail the command to avoid 14672 * recursively calling back into scsi_transport(). 14673 */ 14674 if (ddi_in_panic()) { 14675 goto fail_command_no_log; 14676 } 14677 14678 /* 14679 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 14680 * log an error and fail the command. 14681 */ 14682 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 14683 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 14684 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 14685 sd_dump_memory(un, SD_LOG_IO, "CDB", 14686 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 14687 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 14688 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 14689 goto fail_command; 14690 } 14691 14692 /* 14693 * If we are suspended, then put the command onto head of the 14694 * wait queue since we don't want to start more commands. 14695 */ 14696 switch (un->un_state) { 14697 case SD_STATE_SUSPENDED: 14698 case SD_STATE_DUMPING: 14699 bp->av_forw = un->un_waitq_headp; 14700 un->un_waitq_headp = bp; 14701 if (un->un_waitq_tailp == NULL) { 14702 un->un_waitq_tailp = bp; 14703 } 14704 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 14705 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 14706 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 14707 return; 14708 default: 14709 break; 14710 } 14711 14712 /* 14713 * If the caller wants us to check FLAG_ISOLATE, then see if that 14714 * is set; if it is then we do not want to retry the command. 14715 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 14716 */ 14717 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 14718 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 14719 goto fail_command; 14720 } 14721 } 14722 14723 14724 /* 14725 * If SD_RETRIES_FAILFAST is set, it indicates that either a 14726 * command timeout or a selection timeout has occurred. This means 14727 * that we were unable to establish an kind of communication with 14728 * the target, and subsequent retries and/or commands are likely 14729 * to encounter similar results and take a long time to complete. 14730 * 14731 * If this is a failfast error condition, we need to update the 14732 * failfast state, even if this bp does not have B_FAILFAST set. 14733 */ 14734 if (retry_check_flag & SD_RETRIES_FAILFAST) { 14735 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 14736 ASSERT(un->un_failfast_bp == NULL); 14737 /* 14738 * If we are already in the active failfast state, and 14739 * another failfast error condition has been detected, 14740 * then fail this command if it has B_FAILFAST set. 14741 * If B_FAILFAST is clear, then maintain the legacy 14742 * behavior of retrying heroically, even tho this will 14743 * take a lot more time to fail the command. 14744 */ 14745 if (bp->b_flags & B_FAILFAST) { 14746 goto fail_command; 14747 } 14748 } else { 14749 /* 14750 * We're not in the active failfast state, but we 14751 * have a failfast error condition, so we must begin 14752 * transition to the next state. We do this regardless 14753 * of whether or not this bp has B_FAILFAST set. 14754 */ 14755 if (un->un_failfast_bp == NULL) { 14756 /* 14757 * This is the first bp to meet a failfast 14758 * condition so save it on un_failfast_bp & 14759 * do normal retry processing. Do not enter 14760 * active failfast state yet. This marks 14761 * entry into the "failfast pending" state. 14762 */ 14763 un->un_failfast_bp = bp; 14764 14765 } else if (un->un_failfast_bp == bp) { 14766 /* 14767 * This is the second time *this* bp has 14768 * encountered a failfast error condition, 14769 * so enter active failfast state & flush 14770 * queues as appropriate. 14771 */ 14772 un->un_failfast_state = SD_FAILFAST_ACTIVE; 14773 un->un_failfast_bp = NULL; 14774 sd_failfast_flushq(un); 14775 14776 /* 14777 * Fail this bp now if B_FAILFAST set; 14778 * otherwise continue with retries. (It would 14779 * be pretty ironic if this bp succeeded on a 14780 * subsequent retry after we just flushed all 14781 * the queues). 14782 */ 14783 if (bp->b_flags & B_FAILFAST) { 14784 goto fail_command; 14785 } 14786 14787 #if !defined(lint) && !defined(__lint) 14788 } else { 14789 /* 14790 * If neither of the preceeding conditionals 14791 * was true, it means that there is some 14792 * *other* bp that has met an inital failfast 14793 * condition and is currently either being 14794 * retried or is waiting to be retried. In 14795 * that case we should perform normal retry 14796 * processing on *this* bp, since there is a 14797 * chance that the current failfast condition 14798 * is transient and recoverable. If that does 14799 * not turn out to be the case, then retries 14800 * will be cleared when the wait queue is 14801 * flushed anyway. 14802 */ 14803 #endif 14804 } 14805 } 14806 } else { 14807 /* 14808 * SD_RETRIES_FAILFAST is clear, which indicates that we 14809 * likely were able to at least establish some level of 14810 * communication with the target and subsequent commands 14811 * and/or retries are likely to get through to the target, 14812 * In this case we want to be aggressive about clearing 14813 * the failfast state. Note that this does not affect 14814 * the "failfast pending" condition. 14815 */ 14816 un->un_failfast_state = SD_FAILFAST_INACTIVE; 14817 } 14818 14819 14820 /* 14821 * Check the specified retry count to see if we can still do 14822 * any retries with this pkt before we should fail it. 14823 */ 14824 switch (retry_check_flag & SD_RETRIES_MASK) { 14825 case SD_RETRIES_VICTIM: 14826 /* 14827 * Check the victim retry count. If exhausted, then fall 14828 * thru & check against the standard retry count. 14829 */ 14830 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 14831 /* Increment count & proceed with the retry */ 14832 xp->xb_victim_retry_count++; 14833 break; 14834 } 14835 /* Victim retries exhausted, fall back to std. retries... */ 14836 /* FALLTHRU */ 14837 14838 case SD_RETRIES_STANDARD: 14839 if (xp->xb_retry_count >= un->un_retry_count) { 14840 /* Retries exhausted, fail the command */ 14841 SD_TRACE(SD_LOG_IO_CORE, un, 14842 "sd_retry_command: retries exhausted!\n"); 14843 /* 14844 * update b_resid for failed SCMD_READ & SCMD_WRITE 14845 * commands with nonzero pkt_resid. 14846 */ 14847 if ((pktp->pkt_reason == CMD_CMPLT) && 14848 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 14849 (pktp->pkt_resid != 0)) { 14850 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 14851 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 14852 SD_UPDATE_B_RESID(bp, pktp); 14853 } 14854 } 14855 goto fail_command; 14856 } 14857 xp->xb_retry_count++; 14858 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14859 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 14860 break; 14861 14862 case SD_RETRIES_UA: 14863 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 14864 /* Retries exhausted, fail the command */ 14865 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14866 "Unit Attention retries exhausted. " 14867 "Check the target.\n"); 14868 goto fail_command; 14869 } 14870 xp->xb_ua_retry_count++; 14871 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14872 "sd_retry_command: retry count:%d\n", 14873 xp->xb_ua_retry_count); 14874 break; 14875 14876 case SD_RETRIES_BUSY: 14877 if (xp->xb_retry_count >= un->un_busy_retry_count) { 14878 /* Retries exhausted, fail the command */ 14879 SD_TRACE(SD_LOG_IO_CORE, un, 14880 "sd_retry_command: retries exhausted!\n"); 14881 goto fail_command; 14882 } 14883 xp->xb_retry_count++; 14884 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14885 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 14886 break; 14887 14888 case SD_RETRIES_NOCHECK: 14889 default: 14890 /* No retry count to check. Just proceed with the retry */ 14891 break; 14892 } 14893 14894 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 14895 14896 /* 14897 * If we were given a zero timeout, we must attempt to retry the 14898 * command immediately (ie, without a delay). 14899 */ 14900 if (retry_delay == 0) { 14901 /* 14902 * Check some limiting conditions to see if we can actually 14903 * do the immediate retry. If we cannot, then we must 14904 * fall back to queueing up a delayed retry. 14905 */ 14906 if (un->un_ncmds_in_transport >= un->un_throttle) { 14907 /* 14908 * We are at the throttle limit for the target, 14909 * fall back to delayed retry. 14910 */ 14911 retry_delay = SD_BSY_TIMEOUT; 14912 statp = kstat_waitq_enter; 14913 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14914 "sd_retry_command: immed. retry hit throttle!\n"); 14915 } else { 14916 /* 14917 * We're clear to proceed with the immediate retry. 14918 * First call the user-provided function (if any) 14919 */ 14920 if (user_funcp != NULL) { 14921 (*user_funcp)(un, bp, user_arg, 14922 SD_IMMEDIATE_RETRY_ISSUED); 14923 } 14924 14925 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14926 "sd_retry_command: issuing immediate retry\n"); 14927 14928 /* 14929 * Call sd_start_cmds() to transport the command to 14930 * the target. 14931 */ 14932 sd_start_cmds(un, bp); 14933 14934 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14935 "sd_retry_command exit\n"); 14936 return; 14937 } 14938 } 14939 14940 /* 14941 * Set up to retry the command after a delay. 14942 * First call the user-provided function (if any) 14943 */ 14944 if (user_funcp != NULL) { 14945 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 14946 } 14947 14948 sd_set_retry_bp(un, bp, retry_delay, statp); 14949 14950 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 14951 return; 14952 14953 fail_command: 14954 14955 if (user_funcp != NULL) { 14956 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 14957 } 14958 14959 fail_command_no_log: 14960 14961 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14962 "sd_retry_command: returning failed command\n"); 14963 14964 sd_return_failed_command(un, bp, failure_code); 14965 14966 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 14967 } 14968 14969 14970 /* 14971 * Function: sd_set_retry_bp 14972 * 14973 * Description: Set up the given bp for retry. 14974 * 14975 * Arguments: un - ptr to associated softstate 14976 * bp - ptr to buf(9S) for the command 14977 * retry_delay - time interval before issuing retry (may be 0) 14978 * statp - optional pointer to kstat function 14979 * 14980 * Context: May be called under interrupt context 14981 */ 14982 14983 static void 14984 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 14985 void (*statp)(kstat_io_t *)) 14986 { 14987 ASSERT(un != NULL); 14988 ASSERT(mutex_owned(SD_MUTEX(un))); 14989 ASSERT(bp != NULL); 14990 14991 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14992 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 14993 14994 /* 14995 * Indicate that the command is being retried. This will not allow any 14996 * other commands on the wait queue to be transported to the target 14997 * until this command has been completed (success or failure). The 14998 * "retry command" is not transported to the target until the given 14999 * time delay expires, unless the user specified a 0 retry_delay. 15000 * 15001 * Note: the timeout(9F) callback routine is what actually calls 15002 * sd_start_cmds() to transport the command, with the exception of a 15003 * zero retry_delay. The only current implementor of a zero retry delay 15004 * is the case where a START_STOP_UNIT is sent to spin-up a device. 15005 */ 15006 if (un->un_retry_bp == NULL) { 15007 ASSERT(un->un_retry_statp == NULL); 15008 un->un_retry_bp = bp; 15009 15010 /* 15011 * If the user has not specified a delay the command should 15012 * be queued and no timeout should be scheduled. 15013 */ 15014 if (retry_delay == 0) { 15015 /* 15016 * Save the kstat pointer that will be used in the 15017 * call to SD_UPDATE_KSTATS() below, so that 15018 * sd_start_cmds() can correctly decrement the waitq 15019 * count when it is time to transport this command. 15020 */ 15021 un->un_retry_statp = statp; 15022 goto done; 15023 } 15024 } 15025 15026 if (un->un_retry_bp == bp) { 15027 /* 15028 * Save the kstat pointer that will be used in the call to 15029 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 15030 * correctly decrement the waitq count when it is time to 15031 * transport this command. 15032 */ 15033 un->un_retry_statp = statp; 15034 15035 /* 15036 * Schedule a timeout if: 15037 * 1) The user has specified a delay. 15038 * 2) There is not a START_STOP_UNIT callback pending. 15039 * 15040 * If no delay has been specified, then it is up to the caller 15041 * to ensure that IO processing continues without stalling. 15042 * Effectively, this means that the caller will issue the 15043 * required call to sd_start_cmds(). The START_STOP_UNIT 15044 * callback does this after the START STOP UNIT command has 15045 * completed. In either of these cases we should not schedule 15046 * a timeout callback here. Also don't schedule the timeout if 15047 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 15048 */ 15049 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 15050 (un->un_direct_priority_timeid == NULL)) { 15051 un->un_retry_timeid = 15052 timeout(sd_start_retry_command, un, retry_delay); 15053 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15054 "sd_set_retry_bp: setting timeout: un: 0x%p" 15055 " bp:0x%p un_retry_timeid:0x%p\n", 15056 un, bp, un->un_retry_timeid); 15057 } 15058 } else { 15059 /* 15060 * We only get in here if there is already another command 15061 * waiting to be retried. In this case, we just put the 15062 * given command onto the wait queue, so it can be transported 15063 * after the current retry command has completed. 15064 * 15065 * Also we have to make sure that if the command at the head 15066 * of the wait queue is the un_failfast_bp, that we do not 15067 * put ahead of it any other commands that are to be retried. 15068 */ 15069 if ((un->un_failfast_bp != NULL) && 15070 (un->un_failfast_bp == un->un_waitq_headp)) { 15071 /* 15072 * Enqueue this command AFTER the first command on 15073 * the wait queue (which is also un_failfast_bp). 15074 */ 15075 bp->av_forw = un->un_waitq_headp->av_forw; 15076 un->un_waitq_headp->av_forw = bp; 15077 if (un->un_waitq_headp == un->un_waitq_tailp) { 15078 un->un_waitq_tailp = bp; 15079 } 15080 } else { 15081 /* Enqueue this command at the head of the waitq. */ 15082 bp->av_forw = un->un_waitq_headp; 15083 un->un_waitq_headp = bp; 15084 if (un->un_waitq_tailp == NULL) { 15085 un->un_waitq_tailp = bp; 15086 } 15087 } 15088 15089 if (statp == NULL) { 15090 statp = kstat_waitq_enter; 15091 } 15092 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15093 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 15094 } 15095 15096 done: 15097 if (statp != NULL) { 15098 SD_UPDATE_KSTATS(un, statp, bp); 15099 } 15100 15101 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15102 "sd_set_retry_bp: exit un:0x%p\n", un); 15103 } 15104 15105 15106 /* 15107 * Function: sd_start_retry_command 15108 * 15109 * Description: Start the command that has been waiting on the target's 15110 * retry queue. Called from timeout(9F) context after the 15111 * retry delay interval has expired. 15112 * 15113 * Arguments: arg - pointer to associated softstate for the device. 15114 * 15115 * Context: timeout(9F) thread context. May not sleep. 15116 */ 15117 15118 static void 15119 sd_start_retry_command(void *arg) 15120 { 15121 struct sd_lun *un = arg; 15122 15123 ASSERT(un != NULL); 15124 ASSERT(!mutex_owned(SD_MUTEX(un))); 15125 15126 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15127 "sd_start_retry_command: entry\n"); 15128 15129 mutex_enter(SD_MUTEX(un)); 15130 15131 un->un_retry_timeid = NULL; 15132 15133 if (un->un_retry_bp != NULL) { 15134 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15135 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 15136 un, un->un_retry_bp); 15137 sd_start_cmds(un, un->un_retry_bp); 15138 } 15139 15140 mutex_exit(SD_MUTEX(un)); 15141 15142 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15143 "sd_start_retry_command: exit\n"); 15144 } 15145 15146 15147 /* 15148 * Function: sd_start_direct_priority_command 15149 * 15150 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 15151 * received TRAN_BUSY when we called scsi_transport() to send it 15152 * to the underlying HBA. This function is called from timeout(9F) 15153 * context after the delay interval has expired. 15154 * 15155 * Arguments: arg - pointer to associated buf(9S) to be restarted. 15156 * 15157 * Context: timeout(9F) thread context. May not sleep. 15158 */ 15159 15160 static void 15161 sd_start_direct_priority_command(void *arg) 15162 { 15163 struct buf *priority_bp = arg; 15164 struct sd_lun *un; 15165 15166 ASSERT(priority_bp != NULL); 15167 un = SD_GET_UN(priority_bp); 15168 ASSERT(un != NULL); 15169 ASSERT(!mutex_owned(SD_MUTEX(un))); 15170 15171 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15172 "sd_start_direct_priority_command: entry\n"); 15173 15174 mutex_enter(SD_MUTEX(un)); 15175 un->un_direct_priority_timeid = NULL; 15176 sd_start_cmds(un, priority_bp); 15177 mutex_exit(SD_MUTEX(un)); 15178 15179 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15180 "sd_start_direct_priority_command: exit\n"); 15181 } 15182 15183 15184 /* 15185 * Function: sd_send_request_sense_command 15186 * 15187 * Description: Sends a REQUEST SENSE command to the target 15188 * 15189 * Context: May be called from interrupt context. 15190 */ 15191 15192 static void 15193 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 15194 struct scsi_pkt *pktp) 15195 { 15196 ASSERT(bp != NULL); 15197 ASSERT(un != NULL); 15198 ASSERT(mutex_owned(SD_MUTEX(un))); 15199 15200 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 15201 "entry: buf:0x%p\n", bp); 15202 15203 /* 15204 * If we are syncing or dumping, then fail the command to avoid a 15205 * recursive callback into scsi_transport(). Also fail the command 15206 * if we are suspended (legacy behavior). 15207 */ 15208 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 15209 (un->un_state == SD_STATE_DUMPING)) { 15210 sd_return_failed_command(un, bp, EIO); 15211 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15212 "sd_send_request_sense_command: syncing/dumping, exit\n"); 15213 return; 15214 } 15215 15216 /* 15217 * Retry the failed command and don't issue the request sense if: 15218 * 1) the sense buf is busy 15219 * 2) we have 1 or more outstanding commands on the target 15220 * (the sense data will be cleared or invalidated any way) 15221 * 15222 * Note: There could be an issue with not checking a retry limit here, 15223 * the problem is determining which retry limit to check. 15224 */ 15225 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 15226 /* Don't retry if the command is flagged as non-retryable */ 15227 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 15228 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 15229 NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter); 15230 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15231 "sd_send_request_sense_command: " 15232 "at full throttle, retrying exit\n"); 15233 } else { 15234 sd_return_failed_command(un, bp, EIO); 15235 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15236 "sd_send_request_sense_command: " 15237 "at full throttle, non-retryable exit\n"); 15238 } 15239 return; 15240 } 15241 15242 sd_mark_rqs_busy(un, bp); 15243 sd_start_cmds(un, un->un_rqs_bp); 15244 15245 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15246 "sd_send_request_sense_command: exit\n"); 15247 } 15248 15249 15250 /* 15251 * Function: sd_mark_rqs_busy 15252 * 15253 * Description: Indicate that the request sense bp for this instance is 15254 * in use. 15255 * 15256 * Context: May be called under interrupt context 15257 */ 15258 15259 static void 15260 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 15261 { 15262 struct sd_xbuf *sense_xp; 15263 15264 ASSERT(un != NULL); 15265 ASSERT(bp != NULL); 15266 ASSERT(mutex_owned(SD_MUTEX(un))); 15267 ASSERT(un->un_sense_isbusy == 0); 15268 15269 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 15270 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 15271 15272 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 15273 ASSERT(sense_xp != NULL); 15274 15275 SD_INFO(SD_LOG_IO, un, 15276 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 15277 15278 ASSERT(sense_xp->xb_pktp != NULL); 15279 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 15280 == (FLAG_SENSING | FLAG_HEAD)); 15281 15282 un->un_sense_isbusy = 1; 15283 un->un_rqs_bp->b_resid = 0; 15284 sense_xp->xb_pktp->pkt_resid = 0; 15285 sense_xp->xb_pktp->pkt_reason = 0; 15286 15287 /* So we can get back the bp at interrupt time! */ 15288 sense_xp->xb_sense_bp = bp; 15289 15290 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 15291 15292 /* 15293 * Mark this buf as awaiting sense data. (This is already set in 15294 * the pkt_flags for the RQS packet.) 15295 */ 15296 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 15297 15298 sense_xp->xb_retry_count = 0; 15299 sense_xp->xb_victim_retry_count = 0; 15300 sense_xp->xb_ua_retry_count = 0; 15301 sense_xp->xb_dma_resid = 0; 15302 15303 /* Clean up the fields for auto-request sense */ 15304 sense_xp->xb_sense_status = 0; 15305 sense_xp->xb_sense_state = 0; 15306 sense_xp->xb_sense_resid = 0; 15307 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 15308 15309 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 15310 } 15311 15312 15313 /* 15314 * Function: sd_mark_rqs_idle 15315 * 15316 * Description: SD_MUTEX must be held continuously through this routine 15317 * to prevent reuse of the rqs struct before the caller can 15318 * complete it's processing. 15319 * 15320 * Return Code: Pointer to the RQS buf 15321 * 15322 * Context: May be called under interrupt context 15323 */ 15324 15325 static struct buf * 15326 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 15327 { 15328 struct buf *bp; 15329 ASSERT(un != NULL); 15330 ASSERT(sense_xp != NULL); 15331 ASSERT(mutex_owned(SD_MUTEX(un))); 15332 ASSERT(un->un_sense_isbusy != 0); 15333 15334 un->un_sense_isbusy = 0; 15335 bp = sense_xp->xb_sense_bp; 15336 sense_xp->xb_sense_bp = NULL; 15337 15338 /* This pkt is no longer interested in getting sense data */ 15339 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 15340 15341 return (bp); 15342 } 15343 15344 15345 15346 /* 15347 * Function: sd_alloc_rqs 15348 * 15349 * Description: Set up the unit to receive auto request sense data 15350 * 15351 * Return Code: DDI_SUCCESS or DDI_FAILURE 15352 * 15353 * Context: Called under attach(9E) context 15354 */ 15355 15356 static int 15357 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 15358 { 15359 struct sd_xbuf *xp; 15360 15361 ASSERT(un != NULL); 15362 ASSERT(!mutex_owned(SD_MUTEX(un))); 15363 ASSERT(un->un_rqs_bp == NULL); 15364 ASSERT(un->un_rqs_pktp == NULL); 15365 15366 /* 15367 * First allocate the required buf and scsi_pkt structs, then set up 15368 * the CDB in the scsi_pkt for a REQUEST SENSE command. 15369 */ 15370 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 15371 SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 15372 if (un->un_rqs_bp == NULL) { 15373 return (DDI_FAILURE); 15374 } 15375 15376 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 15377 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 15378 15379 if (un->un_rqs_pktp == NULL) { 15380 sd_free_rqs(un); 15381 return (DDI_FAILURE); 15382 } 15383 15384 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 15385 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 15386 SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0); 15387 15388 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 15389 15390 /* Set up the other needed members in the ARQ scsi_pkt. */ 15391 un->un_rqs_pktp->pkt_comp = sdintr; 15392 un->un_rqs_pktp->pkt_time = sd_io_time; 15393 un->un_rqs_pktp->pkt_flags |= 15394 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 15395 15396 /* 15397 * Allocate & init the sd_xbuf struct for the RQS command. Do not 15398 * provide any intpkt, destroypkt routines as we take care of 15399 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 15400 */ 15401 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 15402 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 15403 xp->xb_pktp = un->un_rqs_pktp; 15404 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15405 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 15406 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 15407 15408 /* 15409 * Save the pointer to the request sense private bp so it can 15410 * be retrieved in sdintr. 15411 */ 15412 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 15413 ASSERT(un->un_rqs_bp->b_private == xp); 15414 15415 /* 15416 * See if the HBA supports auto-request sense for the specified 15417 * target/lun. If it does, then try to enable it (if not already 15418 * enabled). 15419 * 15420 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 15421 * failure, while for other HBAs (pln) scsi_ifsetcap will always 15422 * return success. However, in both of these cases ARQ is always 15423 * enabled and scsi_ifgetcap will always return true. The best approach 15424 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 15425 * 15426 * The 3rd case is the HBA (adp) always return enabled on 15427 * scsi_ifgetgetcap even when it's not enable, the best approach 15428 * is issue a scsi_ifsetcap then a scsi_ifgetcap 15429 * Note: this case is to circumvent the Adaptec bug. (x86 only) 15430 */ 15431 15432 if (un->un_f_is_fibre == TRUE) { 15433 un->un_f_arq_enabled = TRUE; 15434 } else { 15435 #if defined(__i386) || defined(__amd64) 15436 /* 15437 * Circumvent the Adaptec bug, remove this code when 15438 * the bug is fixed 15439 */ 15440 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 15441 #endif 15442 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 15443 case 0: 15444 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15445 "sd_alloc_rqs: HBA supports ARQ\n"); 15446 /* 15447 * ARQ is supported by this HBA but currently is not 15448 * enabled. Attempt to enable it and if successful then 15449 * mark this instance as ARQ enabled. 15450 */ 15451 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 15452 == 1) { 15453 /* Successfully enabled ARQ in the HBA */ 15454 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15455 "sd_alloc_rqs: ARQ enabled\n"); 15456 un->un_f_arq_enabled = TRUE; 15457 } else { 15458 /* Could not enable ARQ in the HBA */ 15459 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15460 "sd_alloc_rqs: failed ARQ enable\n"); 15461 un->un_f_arq_enabled = FALSE; 15462 } 15463 break; 15464 case 1: 15465 /* 15466 * ARQ is supported by this HBA and is already enabled. 15467 * Just mark ARQ as enabled for this instance. 15468 */ 15469 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15470 "sd_alloc_rqs: ARQ already enabled\n"); 15471 un->un_f_arq_enabled = TRUE; 15472 break; 15473 default: 15474 /* 15475 * ARQ is not supported by this HBA; disable it for this 15476 * instance. 15477 */ 15478 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15479 "sd_alloc_rqs: HBA does not support ARQ\n"); 15480 un->un_f_arq_enabled = FALSE; 15481 break; 15482 } 15483 } 15484 15485 return (DDI_SUCCESS); 15486 } 15487 15488 15489 /* 15490 * Function: sd_free_rqs 15491 * 15492 * Description: Cleanup for the pre-instance RQS command. 15493 * 15494 * Context: Kernel thread context 15495 */ 15496 15497 static void 15498 sd_free_rqs(struct sd_lun *un) 15499 { 15500 ASSERT(un != NULL); 15501 15502 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 15503 15504 /* 15505 * If consistent memory is bound to a scsi_pkt, the pkt 15506 * has to be destroyed *before* freeing the consistent memory. 15507 * Don't change the sequence of this operations. 15508 * scsi_destroy_pkt() might access memory, which isn't allowed, 15509 * after it was freed in scsi_free_consistent_buf(). 15510 */ 15511 if (un->un_rqs_pktp != NULL) { 15512 scsi_destroy_pkt(un->un_rqs_pktp); 15513 un->un_rqs_pktp = NULL; 15514 } 15515 15516 if (un->un_rqs_bp != NULL) { 15517 kmem_free(SD_GET_XBUF(un->un_rqs_bp), sizeof (struct sd_xbuf)); 15518 scsi_free_consistent_buf(un->un_rqs_bp); 15519 un->un_rqs_bp = NULL; 15520 } 15521 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 15522 } 15523 15524 15525 15526 /* 15527 * Function: sd_reduce_throttle 15528 * 15529 * Description: Reduces the maximun # of outstanding commands on a 15530 * target to the current number of outstanding commands. 15531 * Queues a tiemout(9F) callback to restore the limit 15532 * after a specified interval has elapsed. 15533 * Typically used when we get a TRAN_BUSY return code 15534 * back from scsi_transport(). 15535 * 15536 * Arguments: un - ptr to the sd_lun softstate struct 15537 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 15538 * 15539 * Context: May be called from interrupt context 15540 */ 15541 15542 static void 15543 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 15544 { 15545 ASSERT(un != NULL); 15546 ASSERT(mutex_owned(SD_MUTEX(un))); 15547 ASSERT(un->un_ncmds_in_transport >= 0); 15548 15549 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 15550 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 15551 un, un->un_throttle, un->un_ncmds_in_transport); 15552 15553 if (un->un_throttle > 1) { 15554 if (un->un_f_use_adaptive_throttle == TRUE) { 15555 switch (throttle_type) { 15556 case SD_THROTTLE_TRAN_BUSY: 15557 if (un->un_busy_throttle == 0) { 15558 un->un_busy_throttle = un->un_throttle; 15559 } 15560 break; 15561 case SD_THROTTLE_QFULL: 15562 un->un_busy_throttle = 0; 15563 break; 15564 default: 15565 ASSERT(FALSE); 15566 } 15567 15568 if (un->un_ncmds_in_transport > 0) { 15569 un->un_throttle = un->un_ncmds_in_transport; 15570 } 15571 } else { 15572 if (un->un_ncmds_in_transport == 0) { 15573 un->un_throttle = 1; 15574 } else { 15575 un->un_throttle = un->un_ncmds_in_transport; 15576 } 15577 } 15578 } 15579 15580 /* Reschedule the timeout if none is currently active */ 15581 if (un->un_reset_throttle_timeid == NULL) { 15582 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 15583 un, sd_reset_throttle_timeout); 15584 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15585 "sd_reduce_throttle: timeout scheduled!\n"); 15586 } 15587 15588 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 15589 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 15590 } 15591 15592 15593 15594 /* 15595 * Function: sd_restore_throttle 15596 * 15597 * Description: Callback function for timeout(9F). Resets the current 15598 * value of un->un_throttle to its default. 15599 * 15600 * Arguments: arg - pointer to associated softstate for the device. 15601 * 15602 * Context: May be called from interrupt context 15603 */ 15604 15605 static void 15606 sd_restore_throttle(void *arg) 15607 { 15608 struct sd_lun *un = arg; 15609 15610 ASSERT(un != NULL); 15611 ASSERT(!mutex_owned(SD_MUTEX(un))); 15612 15613 mutex_enter(SD_MUTEX(un)); 15614 15615 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 15616 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 15617 15618 un->un_reset_throttle_timeid = NULL; 15619 15620 if (un->un_f_use_adaptive_throttle == TRUE) { 15621 /* 15622 * If un_busy_throttle is nonzero, then it contains the 15623 * value that un_throttle was when we got a TRAN_BUSY back 15624 * from scsi_transport(). We want to revert back to this 15625 * value. 15626 */ 15627 if (un->un_busy_throttle > 0) { 15628 un->un_throttle = un->un_busy_throttle; 15629 un->un_busy_throttle = 0; 15630 } 15631 15632 /* 15633 * If un_throttle has fallen below the low-water mark, we 15634 * restore the maximum value here (and allow it to ratchet 15635 * down again if necessary). 15636 */ 15637 if (un->un_throttle < un->un_min_throttle) { 15638 un->un_throttle = un->un_saved_throttle; 15639 } 15640 } else { 15641 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 15642 "restoring limit from 0x%x to 0x%x\n", 15643 un->un_throttle, un->un_saved_throttle); 15644 un->un_throttle = un->un_saved_throttle; 15645 } 15646 15647 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15648 "sd_restore_throttle: calling sd_start_cmds!\n"); 15649 15650 sd_start_cmds(un, NULL); 15651 15652 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15653 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 15654 un, un->un_throttle); 15655 15656 mutex_exit(SD_MUTEX(un)); 15657 15658 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 15659 } 15660 15661 /* 15662 * Function: sdrunout 15663 * 15664 * Description: Callback routine for scsi_init_pkt when a resource allocation 15665 * fails. 15666 * 15667 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 15668 * soft state instance. 15669 * 15670 * Return Code: The scsi_init_pkt routine allows for the callback function to 15671 * return a 0 indicating the callback should be rescheduled or a 1 15672 * indicating not to reschedule. This routine always returns 1 15673 * because the driver always provides a callback function to 15674 * scsi_init_pkt. This results in a callback always being scheduled 15675 * (via the scsi_init_pkt callback implementation) if a resource 15676 * failure occurs. 15677 * 15678 * Context: This callback function may not block or call routines that block 15679 * 15680 * Note: Using the scsi_init_pkt callback facility can result in an I/O 15681 * request persisting at the head of the list which cannot be 15682 * satisfied even after multiple retries. In the future the driver 15683 * may implement some time of maximum runout count before failing 15684 * an I/O. 15685 */ 15686 15687 static int 15688 sdrunout(caddr_t arg) 15689 { 15690 struct sd_lun *un = (struct sd_lun *)arg; 15691 15692 ASSERT(un != NULL); 15693 ASSERT(!mutex_owned(SD_MUTEX(un))); 15694 15695 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 15696 15697 mutex_enter(SD_MUTEX(un)); 15698 sd_start_cmds(un, NULL); 15699 mutex_exit(SD_MUTEX(un)); 15700 /* 15701 * This callback routine always returns 1 (i.e. do not reschedule) 15702 * because we always specify sdrunout as the callback handler for 15703 * scsi_init_pkt inside the call to sd_start_cmds. 15704 */ 15705 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 15706 return (1); 15707 } 15708 15709 15710 /* 15711 * Function: sdintr 15712 * 15713 * Description: Completion callback routine for scsi_pkt(9S) structs 15714 * sent to the HBA driver via scsi_transport(9F). 15715 * 15716 * Context: Interrupt context 15717 */ 15718 15719 static void 15720 sdintr(struct scsi_pkt *pktp) 15721 { 15722 struct buf *bp; 15723 struct sd_xbuf *xp; 15724 struct sd_lun *un; 15725 15726 ASSERT(pktp != NULL); 15727 bp = (struct buf *)pktp->pkt_private; 15728 ASSERT(bp != NULL); 15729 xp = SD_GET_XBUF(bp); 15730 ASSERT(xp != NULL); 15731 ASSERT(xp->xb_pktp != NULL); 15732 un = SD_GET_UN(bp); 15733 ASSERT(un != NULL); 15734 ASSERT(!mutex_owned(SD_MUTEX(un))); 15735 15736 #ifdef SD_FAULT_INJECTION 15737 15738 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 15739 /* SD FaultInjection */ 15740 sd_faultinjection(pktp); 15741 15742 #endif /* SD_FAULT_INJECTION */ 15743 15744 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 15745 " xp:0x%p, un:0x%p\n", bp, xp, un); 15746 15747 mutex_enter(SD_MUTEX(un)); 15748 15749 /* Reduce the count of the #commands currently in transport */ 15750 un->un_ncmds_in_transport--; 15751 ASSERT(un->un_ncmds_in_transport >= 0); 15752 15753 /* Increment counter to indicate that the callback routine is active */ 15754 un->un_in_callback++; 15755 15756 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15757 15758 #ifdef SDDEBUG 15759 if (bp == un->un_retry_bp) { 15760 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 15761 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 15762 un, un->un_retry_bp, un->un_ncmds_in_transport); 15763 } 15764 #endif 15765 15766 /* 15767 * If pkt_reason is CMD_DEV_GONE, just fail the command 15768 */ 15769 if (pktp->pkt_reason == CMD_DEV_GONE) { 15770 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 15771 "Device is gone\n"); 15772 sd_return_failed_command(un, bp, EIO); 15773 goto exit; 15774 } 15775 15776 /* 15777 * First see if the pkt has auto-request sense data with it.... 15778 * Look at the packet state first so we don't take a performance 15779 * hit looking at the arq enabled flag unless absolutely necessary. 15780 */ 15781 if ((pktp->pkt_state & STATE_ARQ_DONE) && 15782 (un->un_f_arq_enabled == TRUE)) { 15783 /* 15784 * The HBA did an auto request sense for this command so check 15785 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 15786 * driver command that should not be retried. 15787 */ 15788 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 15789 /* 15790 * Save the relevant sense info into the xp for the 15791 * original cmd. 15792 */ 15793 struct scsi_arq_status *asp; 15794 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 15795 xp->xb_sense_status = 15796 *((uchar_t *)(&(asp->sts_rqpkt_status))); 15797 xp->xb_sense_state = asp->sts_rqpkt_state; 15798 xp->xb_sense_resid = asp->sts_rqpkt_resid; 15799 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 15800 min(sizeof (struct scsi_extended_sense), 15801 SENSE_LENGTH)); 15802 15803 /* fail the command */ 15804 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15805 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 15806 sd_return_failed_command(un, bp, EIO); 15807 goto exit; 15808 } 15809 15810 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 15811 /* 15812 * We want to either retry or fail this command, so free 15813 * the DMA resources here. If we retry the command then 15814 * the DMA resources will be reallocated in sd_start_cmds(). 15815 * Note that when PKT_DMA_PARTIAL is used, this reallocation 15816 * causes the *entire* transfer to start over again from the 15817 * beginning of the request, even for PARTIAL chunks that 15818 * have already transferred successfully. 15819 */ 15820 if ((un->un_f_is_fibre == TRUE) && 15821 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 15822 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 15823 scsi_dmafree(pktp); 15824 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 15825 } 15826 #endif 15827 15828 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15829 "sdintr: arq done, sd_handle_auto_request_sense\n"); 15830 15831 sd_handle_auto_request_sense(un, bp, xp, pktp); 15832 goto exit; 15833 } 15834 15835 /* Next see if this is the REQUEST SENSE pkt for the instance */ 15836 if (pktp->pkt_flags & FLAG_SENSING) { 15837 /* This pktp is from the unit's REQUEST_SENSE command */ 15838 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15839 "sdintr: sd_handle_request_sense\n"); 15840 sd_handle_request_sense(un, bp, xp, pktp); 15841 goto exit; 15842 } 15843 15844 /* 15845 * Check to see if the command successfully completed as requested; 15846 * this is the most common case (and also the hot performance path). 15847 * 15848 * Requirements for successful completion are: 15849 * pkt_reason is CMD_CMPLT and packet status is status good. 15850 * In addition: 15851 * - A residual of zero indicates successful completion no matter what 15852 * the command is. 15853 * - If the residual is not zero and the command is not a read or 15854 * write, then it's still defined as successful completion. In other 15855 * words, if the command is a read or write the residual must be 15856 * zero for successful completion. 15857 * - If the residual is not zero and the command is a read or 15858 * write, and it's a USCSICMD, then it's still defined as 15859 * successful completion. 15860 */ 15861 if ((pktp->pkt_reason == CMD_CMPLT) && 15862 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 15863 15864 /* 15865 * Since this command is returned with a good status, we 15866 * can reset the count for Sonoma failover. 15867 */ 15868 un->un_sonoma_failure_count = 0; 15869 15870 /* 15871 * Return all USCSI commands on good status 15872 */ 15873 if (pktp->pkt_resid == 0) { 15874 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15875 "sdintr: returning command for resid == 0\n"); 15876 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 15877 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 15878 SD_UPDATE_B_RESID(bp, pktp); 15879 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15880 "sdintr: returning command for resid != 0\n"); 15881 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 15882 SD_UPDATE_B_RESID(bp, pktp); 15883 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15884 "sdintr: returning uscsi command\n"); 15885 } else { 15886 goto not_successful; 15887 } 15888 sd_return_command(un, bp); 15889 15890 /* 15891 * Decrement counter to indicate that the callback routine 15892 * is done. 15893 */ 15894 un->un_in_callback--; 15895 ASSERT(un->un_in_callback >= 0); 15896 mutex_exit(SD_MUTEX(un)); 15897 15898 return; 15899 } 15900 15901 not_successful: 15902 15903 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 15904 /* 15905 * The following is based upon knowledge of the underlying transport 15906 * and its use of DMA resources. This code should be removed when 15907 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 15908 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 15909 * and sd_start_cmds(). 15910 * 15911 * Free any DMA resources associated with this command if there 15912 * is a chance it could be retried or enqueued for later retry. 15913 * If we keep the DMA binding then mpxio cannot reissue the 15914 * command on another path whenever a path failure occurs. 15915 * 15916 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 15917 * causes the *entire* transfer to start over again from the 15918 * beginning of the request, even for PARTIAL chunks that 15919 * have already transferred successfully. 15920 * 15921 * This is only done for non-uscsi commands (and also skipped for the 15922 * driver's internal RQS command). Also just do this for Fibre Channel 15923 * devices as these are the only ones that support mpxio. 15924 */ 15925 if ((un->un_f_is_fibre == TRUE) && 15926 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 15927 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 15928 scsi_dmafree(pktp); 15929 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 15930 } 15931 #endif 15932 15933 /* 15934 * The command did not successfully complete as requested so check 15935 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 15936 * driver command that should not be retried so just return. If 15937 * FLAG_DIAGNOSE is not set the error will be processed below. 15938 */ 15939 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 15940 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15941 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 15942 /* 15943 * Issue a request sense if a check condition caused the error 15944 * (we handle the auto request sense case above), otherwise 15945 * just fail the command. 15946 */ 15947 if ((pktp->pkt_reason == CMD_CMPLT) && 15948 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 15949 sd_send_request_sense_command(un, bp, pktp); 15950 } else { 15951 sd_return_failed_command(un, bp, EIO); 15952 } 15953 goto exit; 15954 } 15955 15956 /* 15957 * The command did not successfully complete as requested so process 15958 * the error, retry, and/or attempt recovery. 15959 */ 15960 switch (pktp->pkt_reason) { 15961 case CMD_CMPLT: 15962 switch (SD_GET_PKT_STATUS(pktp)) { 15963 case STATUS_GOOD: 15964 /* 15965 * The command completed successfully with a non-zero 15966 * residual 15967 */ 15968 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15969 "sdintr: STATUS_GOOD \n"); 15970 sd_pkt_status_good(un, bp, xp, pktp); 15971 break; 15972 15973 case STATUS_CHECK: 15974 case STATUS_TERMINATED: 15975 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15976 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 15977 sd_pkt_status_check_condition(un, bp, xp, pktp); 15978 break; 15979 15980 case STATUS_BUSY: 15981 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15982 "sdintr: STATUS_BUSY\n"); 15983 sd_pkt_status_busy(un, bp, xp, pktp); 15984 break; 15985 15986 case STATUS_RESERVATION_CONFLICT: 15987 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15988 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 15989 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 15990 break; 15991 15992 case STATUS_QFULL: 15993 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15994 "sdintr: STATUS_QFULL\n"); 15995 sd_pkt_status_qfull(un, bp, xp, pktp); 15996 break; 15997 15998 case STATUS_MET: 15999 case STATUS_INTERMEDIATE: 16000 case STATUS_SCSI2: 16001 case STATUS_INTERMEDIATE_MET: 16002 case STATUS_ACA_ACTIVE: 16003 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16004 "Unexpected SCSI status received: 0x%x\n", 16005 SD_GET_PKT_STATUS(pktp)); 16006 sd_return_failed_command(un, bp, EIO); 16007 break; 16008 16009 default: 16010 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16011 "Invalid SCSI status received: 0x%x\n", 16012 SD_GET_PKT_STATUS(pktp)); 16013 sd_return_failed_command(un, bp, EIO); 16014 break; 16015 16016 } 16017 break; 16018 16019 case CMD_INCOMPLETE: 16020 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16021 "sdintr: CMD_INCOMPLETE\n"); 16022 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 16023 break; 16024 case CMD_TRAN_ERR: 16025 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16026 "sdintr: CMD_TRAN_ERR\n"); 16027 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 16028 break; 16029 case CMD_RESET: 16030 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16031 "sdintr: CMD_RESET \n"); 16032 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 16033 break; 16034 case CMD_ABORTED: 16035 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16036 "sdintr: CMD_ABORTED \n"); 16037 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 16038 break; 16039 case CMD_TIMEOUT: 16040 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16041 "sdintr: CMD_TIMEOUT\n"); 16042 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 16043 break; 16044 case CMD_UNX_BUS_FREE: 16045 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16046 "sdintr: CMD_UNX_BUS_FREE \n"); 16047 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 16048 break; 16049 case CMD_TAG_REJECT: 16050 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16051 "sdintr: CMD_TAG_REJECT\n"); 16052 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 16053 break; 16054 default: 16055 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16056 "sdintr: default\n"); 16057 sd_pkt_reason_default(un, bp, xp, pktp); 16058 break; 16059 } 16060 16061 exit: 16062 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 16063 16064 /* Decrement counter to indicate that the callback routine is done. */ 16065 un->un_in_callback--; 16066 ASSERT(un->un_in_callback >= 0); 16067 16068 /* 16069 * At this point, the pkt has been dispatched, ie, it is either 16070 * being re-tried or has been returned to its caller and should 16071 * not be referenced. 16072 */ 16073 16074 mutex_exit(SD_MUTEX(un)); 16075 } 16076 16077 16078 /* 16079 * Function: sd_print_incomplete_msg 16080 * 16081 * Description: Prints the error message for a CMD_INCOMPLETE error. 16082 * 16083 * Arguments: un - ptr to associated softstate for the device. 16084 * bp - ptr to the buf(9S) for the command. 16085 * arg - message string ptr 16086 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 16087 * or SD_NO_RETRY_ISSUED. 16088 * 16089 * Context: May be called under interrupt context 16090 */ 16091 16092 static void 16093 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 16094 { 16095 struct scsi_pkt *pktp; 16096 char *msgp; 16097 char *cmdp = arg; 16098 16099 ASSERT(un != NULL); 16100 ASSERT(mutex_owned(SD_MUTEX(un))); 16101 ASSERT(bp != NULL); 16102 ASSERT(arg != NULL); 16103 pktp = SD_GET_PKTP(bp); 16104 ASSERT(pktp != NULL); 16105 16106 switch (code) { 16107 case SD_DELAYED_RETRY_ISSUED: 16108 case SD_IMMEDIATE_RETRY_ISSUED: 16109 msgp = "retrying"; 16110 break; 16111 case SD_NO_RETRY_ISSUED: 16112 default: 16113 msgp = "giving up"; 16114 break; 16115 } 16116 16117 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16118 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16119 "incomplete %s- %s\n", cmdp, msgp); 16120 } 16121 } 16122 16123 16124 16125 /* 16126 * Function: sd_pkt_status_good 16127 * 16128 * Description: Processing for a STATUS_GOOD code in pkt_status. 16129 * 16130 * Context: May be called under interrupt context 16131 */ 16132 16133 static void 16134 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 16135 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16136 { 16137 char *cmdp; 16138 16139 ASSERT(un != NULL); 16140 ASSERT(mutex_owned(SD_MUTEX(un))); 16141 ASSERT(bp != NULL); 16142 ASSERT(xp != NULL); 16143 ASSERT(pktp != NULL); 16144 ASSERT(pktp->pkt_reason == CMD_CMPLT); 16145 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 16146 ASSERT(pktp->pkt_resid != 0); 16147 16148 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 16149 16150 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16151 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 16152 case SCMD_READ: 16153 cmdp = "read"; 16154 break; 16155 case SCMD_WRITE: 16156 cmdp = "write"; 16157 break; 16158 default: 16159 SD_UPDATE_B_RESID(bp, pktp); 16160 sd_return_command(un, bp); 16161 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 16162 return; 16163 } 16164 16165 /* 16166 * See if we can retry the read/write, preferrably immediately. 16167 * If retries are exhaused, then sd_retry_command() will update 16168 * the b_resid count. 16169 */ 16170 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 16171 cmdp, EIO, (clock_t)0, NULL); 16172 16173 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 16174 } 16175 16176 16177 16178 16179 16180 /* 16181 * Function: sd_handle_request_sense 16182 * 16183 * Description: Processing for non-auto Request Sense command. 16184 * 16185 * Arguments: un - ptr to associated softstate 16186 * sense_bp - ptr to buf(9S) for the RQS command 16187 * sense_xp - ptr to the sd_xbuf for the RQS command 16188 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 16189 * 16190 * Context: May be called under interrupt context 16191 */ 16192 16193 static void 16194 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 16195 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 16196 { 16197 struct buf *cmd_bp; /* buf for the original command */ 16198 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 16199 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 16200 16201 ASSERT(un != NULL); 16202 ASSERT(mutex_owned(SD_MUTEX(un))); 16203 ASSERT(sense_bp != NULL); 16204 ASSERT(sense_xp != NULL); 16205 ASSERT(sense_pktp != NULL); 16206 16207 /* 16208 * Note the sense_bp, sense_xp, and sense_pktp here are for the 16209 * RQS command and not the original command. 16210 */ 16211 ASSERT(sense_pktp == un->un_rqs_pktp); 16212 ASSERT(sense_bp == un->un_rqs_bp); 16213 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 16214 (FLAG_SENSING | FLAG_HEAD)); 16215 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 16216 FLAG_SENSING) == FLAG_SENSING); 16217 16218 /* These are the bp, xp, and pktp for the original command */ 16219 cmd_bp = sense_xp->xb_sense_bp; 16220 cmd_xp = SD_GET_XBUF(cmd_bp); 16221 cmd_pktp = SD_GET_PKTP(cmd_bp); 16222 16223 if (sense_pktp->pkt_reason != CMD_CMPLT) { 16224 /* 16225 * The REQUEST SENSE command failed. Release the REQUEST 16226 * SENSE command for re-use, get back the bp for the original 16227 * command, and attempt to re-try the original command if 16228 * FLAG_DIAGNOSE is not set in the original packet. 16229 */ 16230 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16231 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16232 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 16233 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 16234 NULL, NULL, EIO, (clock_t)0, NULL); 16235 return; 16236 } 16237 } 16238 16239 /* 16240 * Save the relevant sense info into the xp for the original cmd. 16241 * 16242 * Note: if the request sense failed the state info will be zero 16243 * as set in sd_mark_rqs_busy() 16244 */ 16245 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 16246 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 16247 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 16248 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, SENSE_LENGTH); 16249 16250 /* 16251 * Free up the RQS command.... 16252 * NOTE: 16253 * Must do this BEFORE calling sd_validate_sense_data! 16254 * sd_validate_sense_data may return the original command in 16255 * which case the pkt will be freed and the flags can no 16256 * longer be touched. 16257 * SD_MUTEX is held through this process until the command 16258 * is dispatched based upon the sense data, so there are 16259 * no race conditions. 16260 */ 16261 (void) sd_mark_rqs_idle(un, sense_xp); 16262 16263 /* 16264 * For a retryable command see if we have valid sense data, if so then 16265 * turn it over to sd_decode_sense() to figure out the right course of 16266 * action. Just fail a non-retryable command. 16267 */ 16268 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16269 if (sd_validate_sense_data(un, cmd_bp, cmd_xp) == 16270 SD_SENSE_DATA_IS_VALID) { 16271 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 16272 } 16273 } else { 16274 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 16275 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 16276 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 16277 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 16278 sd_return_failed_command(un, cmd_bp, EIO); 16279 } 16280 } 16281 16282 16283 16284 16285 /* 16286 * Function: sd_handle_auto_request_sense 16287 * 16288 * Description: Processing for auto-request sense information. 16289 * 16290 * Arguments: un - ptr to associated softstate 16291 * bp - ptr to buf(9S) for the command 16292 * xp - ptr to the sd_xbuf for the command 16293 * pktp - ptr to the scsi_pkt(9S) for the command 16294 * 16295 * Context: May be called under interrupt context 16296 */ 16297 16298 static void 16299 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 16300 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16301 { 16302 struct scsi_arq_status *asp; 16303 16304 ASSERT(un != NULL); 16305 ASSERT(mutex_owned(SD_MUTEX(un))); 16306 ASSERT(bp != NULL); 16307 ASSERT(xp != NULL); 16308 ASSERT(pktp != NULL); 16309 ASSERT(pktp != un->un_rqs_pktp); 16310 ASSERT(bp != un->un_rqs_bp); 16311 16312 /* 16313 * For auto-request sense, we get a scsi_arq_status back from 16314 * the HBA, with the sense data in the sts_sensedata member. 16315 * The pkt_scbp of the packet points to this scsi_arq_status. 16316 */ 16317 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16318 16319 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 16320 /* 16321 * The auto REQUEST SENSE failed; see if we can re-try 16322 * the original command. 16323 */ 16324 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16325 "auto request sense failed (reason=%s)\n", 16326 scsi_rname(asp->sts_rqpkt_reason)); 16327 16328 sd_reset_target(un, pktp); 16329 16330 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 16331 NULL, NULL, EIO, (clock_t)0, NULL); 16332 return; 16333 } 16334 16335 /* Save the relevant sense info into the xp for the original cmd. */ 16336 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 16337 xp->xb_sense_state = asp->sts_rqpkt_state; 16338 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16339 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16340 min(sizeof (struct scsi_extended_sense), SENSE_LENGTH)); 16341 16342 /* 16343 * See if we have valid sense data, if so then turn it over to 16344 * sd_decode_sense() to figure out the right course of action. 16345 */ 16346 if (sd_validate_sense_data(un, bp, xp) == SD_SENSE_DATA_IS_VALID) { 16347 sd_decode_sense(un, bp, xp, pktp); 16348 } 16349 } 16350 16351 16352 /* 16353 * Function: sd_print_sense_failed_msg 16354 * 16355 * Description: Print log message when RQS has failed. 16356 * 16357 * Arguments: un - ptr to associated softstate 16358 * bp - ptr to buf(9S) for the command 16359 * arg - generic message string ptr 16360 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 16361 * or SD_NO_RETRY_ISSUED 16362 * 16363 * Context: May be called from interrupt context 16364 */ 16365 16366 static void 16367 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 16368 int code) 16369 { 16370 char *msgp = arg; 16371 16372 ASSERT(un != NULL); 16373 ASSERT(mutex_owned(SD_MUTEX(un))); 16374 ASSERT(bp != NULL); 16375 16376 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 16377 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 16378 } 16379 } 16380 16381 16382 /* 16383 * Function: sd_validate_sense_data 16384 * 16385 * Description: Check the given sense data for validity. 16386 * If the sense data is not valid, the command will 16387 * be either failed or retried! 16388 * 16389 * Return Code: SD_SENSE_DATA_IS_INVALID 16390 * SD_SENSE_DATA_IS_VALID 16391 * 16392 * Context: May be called from interrupt context 16393 */ 16394 16395 static int 16396 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp) 16397 { 16398 struct scsi_extended_sense *esp; 16399 struct scsi_pkt *pktp; 16400 size_t actual_len; 16401 char *msgp = NULL; 16402 16403 ASSERT(un != NULL); 16404 ASSERT(mutex_owned(SD_MUTEX(un))); 16405 ASSERT(bp != NULL); 16406 ASSERT(bp != un->un_rqs_bp); 16407 ASSERT(xp != NULL); 16408 16409 pktp = SD_GET_PKTP(bp); 16410 ASSERT(pktp != NULL); 16411 16412 /* 16413 * Check the status of the RQS command (auto or manual). 16414 */ 16415 switch (xp->xb_sense_status & STATUS_MASK) { 16416 case STATUS_GOOD: 16417 break; 16418 16419 case STATUS_RESERVATION_CONFLICT: 16420 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16421 return (SD_SENSE_DATA_IS_INVALID); 16422 16423 case STATUS_BUSY: 16424 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16425 "Busy Status on REQUEST SENSE\n"); 16426 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 16427 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 16428 return (SD_SENSE_DATA_IS_INVALID); 16429 16430 case STATUS_QFULL: 16431 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16432 "QFULL Status on REQUEST SENSE\n"); 16433 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 16434 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 16435 return (SD_SENSE_DATA_IS_INVALID); 16436 16437 case STATUS_CHECK: 16438 case STATUS_TERMINATED: 16439 msgp = "Check Condition on REQUEST SENSE\n"; 16440 goto sense_failed; 16441 16442 default: 16443 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 16444 goto sense_failed; 16445 } 16446 16447 /* 16448 * See if we got the minimum required amount of sense data. 16449 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 16450 * or less. 16451 */ 16452 actual_len = (int)(SENSE_LENGTH - xp->xb_sense_resid); 16453 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 16454 (actual_len == 0)) { 16455 msgp = "Request Sense couldn't get sense data\n"; 16456 goto sense_failed; 16457 } 16458 16459 if (actual_len < SUN_MIN_SENSE_LENGTH) { 16460 msgp = "Not enough sense information\n"; 16461 goto sense_failed; 16462 } 16463 16464 /* 16465 * We require the extended sense data 16466 */ 16467 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 16468 if (esp->es_class != CLASS_EXTENDED_SENSE) { 16469 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16470 static char tmp[8]; 16471 static char buf[148]; 16472 char *p = (char *)(xp->xb_sense_data); 16473 int i; 16474 16475 mutex_enter(&sd_sense_mutex); 16476 (void) strcpy(buf, "undecodable sense information:"); 16477 for (i = 0; i < actual_len; i++) { 16478 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 16479 (void) strcpy(&buf[strlen(buf)], tmp); 16480 } 16481 i = strlen(buf); 16482 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 16483 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf); 16484 mutex_exit(&sd_sense_mutex); 16485 } 16486 /* Note: Legacy behavior, fail the command with no retry */ 16487 sd_return_failed_command(un, bp, EIO); 16488 return (SD_SENSE_DATA_IS_INVALID); 16489 } 16490 16491 /* 16492 * Check that es_code is valid (es_class concatenated with es_code 16493 * make up the "response code" field. es_class will always be 7, so 16494 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 16495 * format. 16496 */ 16497 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 16498 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 16499 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 16500 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 16501 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 16502 goto sense_failed; 16503 } 16504 16505 return (SD_SENSE_DATA_IS_VALID); 16506 16507 sense_failed: 16508 /* 16509 * If the request sense failed (for whatever reason), attempt 16510 * to retry the original command. 16511 */ 16512 #if defined(__i386) || defined(__amd64) 16513 /* 16514 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 16515 * sddef.h for Sparc platform, and x86 uses 1 binary 16516 * for both SCSI/FC. 16517 * The SD_RETRY_DELAY value need to be adjusted here 16518 * when SD_RETRY_DELAY change in sddef.h 16519 */ 16520 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 16521 sd_print_sense_failed_msg, msgp, EIO, 16522 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 16523 #else 16524 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 16525 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 16526 #endif 16527 16528 return (SD_SENSE_DATA_IS_INVALID); 16529 } 16530 16531 16532 16533 /* 16534 * Function: sd_decode_sense 16535 * 16536 * Description: Take recovery action(s) when SCSI Sense Data is received. 16537 * 16538 * Context: Interrupt context. 16539 */ 16540 16541 static void 16542 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 16543 struct scsi_pkt *pktp) 16544 { 16545 struct scsi_extended_sense *esp; 16546 struct scsi_descr_sense_hdr *sdsp; 16547 uint8_t asc, ascq, sense_key; 16548 16549 ASSERT(un != NULL); 16550 ASSERT(mutex_owned(SD_MUTEX(un))); 16551 ASSERT(bp != NULL); 16552 ASSERT(bp != un->un_rqs_bp); 16553 ASSERT(xp != NULL); 16554 ASSERT(pktp != NULL); 16555 16556 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 16557 16558 switch (esp->es_code) { 16559 case CODE_FMT_DESCR_CURRENT: 16560 case CODE_FMT_DESCR_DEFERRED: 16561 sdsp = (struct scsi_descr_sense_hdr *)xp->xb_sense_data; 16562 sense_key = sdsp->ds_key; 16563 asc = sdsp->ds_add_code; 16564 ascq = sdsp->ds_qual_code; 16565 break; 16566 case CODE_FMT_VENDOR_SPECIFIC: 16567 case CODE_FMT_FIXED_CURRENT: 16568 case CODE_FMT_FIXED_DEFERRED: 16569 default: 16570 sense_key = esp->es_key; 16571 asc = esp->es_add_code; 16572 ascq = esp->es_qual_code; 16573 break; 16574 } 16575 16576 switch (sense_key) { 16577 case KEY_NO_SENSE: 16578 sd_sense_key_no_sense(un, bp, xp, pktp); 16579 break; 16580 case KEY_RECOVERABLE_ERROR: 16581 sd_sense_key_recoverable_error(un, asc, bp, xp, pktp); 16582 break; 16583 case KEY_NOT_READY: 16584 sd_sense_key_not_ready(un, asc, ascq, bp, xp, pktp); 16585 break; 16586 case KEY_MEDIUM_ERROR: 16587 case KEY_HARDWARE_ERROR: 16588 sd_sense_key_medium_or_hardware_error(un, 16589 sense_key, asc, bp, xp, pktp); 16590 break; 16591 case KEY_ILLEGAL_REQUEST: 16592 sd_sense_key_illegal_request(un, bp, xp, pktp); 16593 break; 16594 case KEY_UNIT_ATTENTION: 16595 sd_sense_key_unit_attention(un, asc, bp, xp, pktp); 16596 break; 16597 case KEY_WRITE_PROTECT: 16598 case KEY_VOLUME_OVERFLOW: 16599 case KEY_MISCOMPARE: 16600 sd_sense_key_fail_command(un, bp, xp, pktp); 16601 break; 16602 case KEY_BLANK_CHECK: 16603 sd_sense_key_blank_check(un, bp, xp, pktp); 16604 break; 16605 case KEY_ABORTED_COMMAND: 16606 sd_sense_key_aborted_command(un, bp, xp, pktp); 16607 break; 16608 case KEY_VENDOR_UNIQUE: 16609 case KEY_COPY_ABORTED: 16610 case KEY_EQUAL: 16611 case KEY_RESERVED: 16612 default: 16613 sd_sense_key_default(un, sense_key, bp, xp, pktp); 16614 break; 16615 } 16616 } 16617 16618 16619 /* 16620 * Function: sd_dump_memory 16621 * 16622 * Description: Debug logging routine to print the contents of a user provided 16623 * buffer. The output of the buffer is broken up into 256 byte 16624 * segments due to a size constraint of the scsi_log. 16625 * implementation. 16626 * 16627 * Arguments: un - ptr to softstate 16628 * comp - component mask 16629 * title - "title" string to preceed data when printed 16630 * data - ptr to data block to be printed 16631 * len - size of data block to be printed 16632 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 16633 * 16634 * Context: May be called from interrupt context 16635 */ 16636 16637 #define SD_DUMP_MEMORY_BUF_SIZE 256 16638 16639 static char *sd_dump_format_string[] = { 16640 " 0x%02x", 16641 " %c" 16642 }; 16643 16644 static void 16645 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 16646 int len, int fmt) 16647 { 16648 int i, j; 16649 int avail_count; 16650 int start_offset; 16651 int end_offset; 16652 size_t entry_len; 16653 char *bufp; 16654 char *local_buf; 16655 char *format_string; 16656 16657 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 16658 16659 /* 16660 * In the debug version of the driver, this function is called from a 16661 * number of places which are NOPs in the release driver. 16662 * The debug driver therefore has additional methods of filtering 16663 * debug output. 16664 */ 16665 #ifdef SDDEBUG 16666 /* 16667 * In the debug version of the driver we can reduce the amount of debug 16668 * messages by setting sd_error_level to something other than 16669 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 16670 * sd_component_mask. 16671 */ 16672 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 16673 (sd_error_level != SCSI_ERR_ALL)) { 16674 return; 16675 } 16676 if (((sd_component_mask & comp) == 0) || 16677 (sd_error_level != SCSI_ERR_ALL)) { 16678 return; 16679 } 16680 #else 16681 if (sd_error_level != SCSI_ERR_ALL) { 16682 return; 16683 } 16684 #endif 16685 16686 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 16687 bufp = local_buf; 16688 /* 16689 * Available length is the length of local_buf[], minus the 16690 * length of the title string, minus one for the ":", minus 16691 * one for the newline, minus one for the NULL terminator. 16692 * This gives the #bytes available for holding the printed 16693 * values from the given data buffer. 16694 */ 16695 if (fmt == SD_LOG_HEX) { 16696 format_string = sd_dump_format_string[0]; 16697 } else /* SD_LOG_CHAR */ { 16698 format_string = sd_dump_format_string[1]; 16699 } 16700 /* 16701 * Available count is the number of elements from the given 16702 * data buffer that we can fit into the available length. 16703 * This is based upon the size of the format string used. 16704 * Make one entry and find it's size. 16705 */ 16706 (void) sprintf(bufp, format_string, data[0]); 16707 entry_len = strlen(bufp); 16708 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 16709 16710 j = 0; 16711 while (j < len) { 16712 bufp = local_buf; 16713 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 16714 start_offset = j; 16715 16716 end_offset = start_offset + avail_count; 16717 16718 (void) sprintf(bufp, "%s:", title); 16719 bufp += strlen(bufp); 16720 for (i = start_offset; ((i < end_offset) && (j < len)); 16721 i++, j++) { 16722 (void) sprintf(bufp, format_string, data[i]); 16723 bufp += entry_len; 16724 } 16725 (void) sprintf(bufp, "\n"); 16726 16727 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 16728 } 16729 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 16730 } 16731 16732 /* 16733 * Function: sd_print_sense_msg 16734 * 16735 * Description: Log a message based upon the given sense data. 16736 * 16737 * Arguments: un - ptr to associated softstate 16738 * bp - ptr to buf(9S) for the command 16739 * arg - ptr to associate sd_sense_info struct 16740 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 16741 * or SD_NO_RETRY_ISSUED 16742 * 16743 * Context: May be called from interrupt context 16744 */ 16745 16746 static void 16747 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 16748 { 16749 struct sd_xbuf *xp; 16750 struct scsi_pkt *pktp; 16751 struct scsi_extended_sense *sensep; 16752 daddr_t request_blkno; 16753 diskaddr_t err_blkno; 16754 int severity; 16755 int pfa_flag; 16756 int fixed_format = TRUE; 16757 extern struct scsi_key_strings scsi_cmds[]; 16758 16759 ASSERT(un != NULL); 16760 ASSERT(mutex_owned(SD_MUTEX(un))); 16761 ASSERT(bp != NULL); 16762 xp = SD_GET_XBUF(bp); 16763 ASSERT(xp != NULL); 16764 pktp = SD_GET_PKTP(bp); 16765 ASSERT(pktp != NULL); 16766 ASSERT(arg != NULL); 16767 16768 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 16769 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 16770 16771 if ((code == SD_DELAYED_RETRY_ISSUED) || 16772 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 16773 severity = SCSI_ERR_RETRYABLE; 16774 } 16775 16776 /* Use absolute block number for the request block number */ 16777 request_blkno = xp->xb_blkno; 16778 16779 /* 16780 * Now try to get the error block number from the sense data 16781 */ 16782 sensep = (struct scsi_extended_sense *)xp->xb_sense_data; 16783 switch (sensep->es_code) { 16784 case CODE_FMT_DESCR_CURRENT: 16785 case CODE_FMT_DESCR_DEFERRED: 16786 err_blkno = 16787 sd_extract_sense_info_descr( 16788 (struct scsi_descr_sense_hdr *)sensep); 16789 fixed_format = FALSE; 16790 break; 16791 case CODE_FMT_FIXED_CURRENT: 16792 case CODE_FMT_FIXED_DEFERRED: 16793 case CODE_FMT_VENDOR_SPECIFIC: 16794 default: 16795 /* 16796 * With the es_valid bit set, we assume that the error 16797 * blkno is in the sense data. Also, if xp->xb_blkno is 16798 * greater than 0xffffffff then the target *should* have used 16799 * a descriptor sense format (or it shouldn't have set 16800 * the es_valid bit), and we may as well ignore the 16801 * 32-bit value. 16802 */ 16803 if ((sensep->es_valid != 0) && (xp->xb_blkno <= 0xffffffff)) { 16804 err_blkno = (diskaddr_t) 16805 ((sensep->es_info_1 << 24) | 16806 (sensep->es_info_2 << 16) | 16807 (sensep->es_info_3 << 8) | 16808 (sensep->es_info_4)); 16809 } else { 16810 err_blkno = (diskaddr_t)-1; 16811 } 16812 break; 16813 } 16814 16815 if (err_blkno == (diskaddr_t)-1) { 16816 /* 16817 * Without the es_valid bit set (for fixed format) or an 16818 * information descriptor (for descriptor format) we cannot 16819 * be certain of the error blkno, so just use the 16820 * request_blkno. 16821 */ 16822 err_blkno = (diskaddr_t)request_blkno; 16823 } else { 16824 /* 16825 * We retrieved the error block number from the information 16826 * portion of the sense data. 16827 * 16828 * For USCSI commands we are better off using the error 16829 * block no. as the requested block no. (This is the best 16830 * we can estimate.) 16831 */ 16832 if ((SD_IS_BUFIO(xp) == FALSE) && 16833 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 16834 request_blkno = err_blkno; 16835 } 16836 } 16837 16838 /* 16839 * The following will log the buffer contents for the release driver 16840 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 16841 * level is set to verbose. 16842 */ 16843 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 16844 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 16845 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 16846 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 16847 16848 if (pfa_flag == FALSE) { 16849 /* This is normally only set for USCSI */ 16850 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 16851 return; 16852 } 16853 16854 if ((SD_IS_BUFIO(xp) == TRUE) && 16855 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 16856 (severity < sd_error_level))) { 16857 return; 16858 } 16859 } 16860 16861 /* 16862 * If the data is fixed format then check for Sonoma Failover, 16863 * and keep a count of how many failed I/O's. We should not have 16864 * to worry about Sonoma returning descriptor format sense data, 16865 * and asc/ascq are in a different location in descriptor format. 16866 */ 16867 if (fixed_format && 16868 (SD_IS_LSI(un)) && (sensep->es_key == KEY_ILLEGAL_REQUEST) && 16869 (sensep->es_add_code == 0x94) && (sensep->es_qual_code == 0x01)) { 16870 un->un_sonoma_failure_count++; 16871 if (un->un_sonoma_failure_count > 1) { 16872 return; 16873 } 16874 } 16875 16876 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 16877 request_blkno, err_blkno, scsi_cmds, sensep, 16878 un->un_additional_codes, NULL); 16879 } 16880 16881 /* 16882 * Function: sd_extract_sense_info_descr 16883 * 16884 * Description: Retrieve "information" field from descriptor format 16885 * sense data. Iterates through each sense descriptor 16886 * looking for the information descriptor and returns 16887 * the information field from that descriptor. 16888 * 16889 * Context: May be called from interrupt context 16890 */ 16891 16892 static diskaddr_t 16893 sd_extract_sense_info_descr(struct scsi_descr_sense_hdr *sdsp) 16894 { 16895 diskaddr_t result; 16896 uint8_t *descr_offset; 16897 int valid_sense_length; 16898 struct scsi_information_sense_descr *isd; 16899 16900 /* 16901 * Initialize result to -1 indicating there is no information 16902 * descriptor 16903 */ 16904 result = (diskaddr_t)-1; 16905 16906 /* 16907 * The first descriptor will immediately follow the header 16908 */ 16909 descr_offset = (uint8_t *)(sdsp+1); /* Pointer arithmetic */ 16910 16911 /* 16912 * Calculate the amount of valid sense data 16913 */ 16914 valid_sense_length = 16915 min((sizeof (struct scsi_descr_sense_hdr) + 16916 sdsp->ds_addl_sense_length), 16917 SENSE_LENGTH); 16918 16919 /* 16920 * Iterate through the list of descriptors, stopping when we 16921 * run out of sense data 16922 */ 16923 while ((descr_offset + sizeof (struct scsi_information_sense_descr)) <= 16924 (uint8_t *)sdsp + valid_sense_length) { 16925 /* 16926 * Check if this is an information descriptor. We can 16927 * use the scsi_information_sense_descr structure as a 16928 * template sense the first two fields are always the 16929 * same 16930 */ 16931 isd = (struct scsi_information_sense_descr *)descr_offset; 16932 if (isd->isd_descr_type == DESCR_INFORMATION) { 16933 /* 16934 * Found an information descriptor. Copy the 16935 * information field. There will only be one 16936 * information descriptor so we can stop looking. 16937 */ 16938 result = 16939 (((diskaddr_t)isd->isd_information[0] << 56) | 16940 ((diskaddr_t)isd->isd_information[1] << 48) | 16941 ((diskaddr_t)isd->isd_information[2] << 40) | 16942 ((diskaddr_t)isd->isd_information[3] << 32) | 16943 ((diskaddr_t)isd->isd_information[4] << 24) | 16944 ((diskaddr_t)isd->isd_information[5] << 16) | 16945 ((diskaddr_t)isd->isd_information[6] << 8) | 16946 ((diskaddr_t)isd->isd_information[7])); 16947 break; 16948 } 16949 16950 /* 16951 * Get pointer to the next descriptor. The "additional 16952 * length" field holds the length of the descriptor except 16953 * for the "type" and "additional length" fields, so 16954 * we need to add 2 to get the total length. 16955 */ 16956 descr_offset += (isd->isd_addl_length + 2); 16957 } 16958 16959 return (result); 16960 } 16961 16962 /* 16963 * Function: sd_sense_key_no_sense 16964 * 16965 * Description: Recovery action when sense data was not received. 16966 * 16967 * Context: May be called from interrupt context 16968 */ 16969 16970 static void 16971 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 16972 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16973 { 16974 struct sd_sense_info si; 16975 16976 ASSERT(un != NULL); 16977 ASSERT(mutex_owned(SD_MUTEX(un))); 16978 ASSERT(bp != NULL); 16979 ASSERT(xp != NULL); 16980 ASSERT(pktp != NULL); 16981 16982 si.ssi_severity = SCSI_ERR_FATAL; 16983 si.ssi_pfa_flag = FALSE; 16984 16985 SD_UPDATE_ERRSTATS(un, sd_softerrs); 16986 16987 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 16988 &si, EIO, (clock_t)0, NULL); 16989 } 16990 16991 16992 /* 16993 * Function: sd_sense_key_recoverable_error 16994 * 16995 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 16996 * 16997 * Context: May be called from interrupt context 16998 */ 16999 17000 static void 17001 sd_sense_key_recoverable_error(struct sd_lun *un, 17002 uint8_t asc, 17003 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17004 { 17005 struct sd_sense_info si; 17006 17007 ASSERT(un != NULL); 17008 ASSERT(mutex_owned(SD_MUTEX(un))); 17009 ASSERT(bp != NULL); 17010 ASSERT(xp != NULL); 17011 ASSERT(pktp != NULL); 17012 17013 /* 17014 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 17015 */ 17016 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 17017 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17018 si.ssi_severity = SCSI_ERR_INFO; 17019 si.ssi_pfa_flag = TRUE; 17020 } else { 17021 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17022 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 17023 si.ssi_severity = SCSI_ERR_RECOVERED; 17024 si.ssi_pfa_flag = FALSE; 17025 } 17026 17027 if (pktp->pkt_resid == 0) { 17028 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17029 sd_return_command(un, bp); 17030 return; 17031 } 17032 17033 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17034 &si, EIO, (clock_t)0, NULL); 17035 } 17036 17037 17038 17039 17040 /* 17041 * Function: sd_sense_key_not_ready 17042 * 17043 * Description: Recovery actions for a SCSI "Not Ready" sense key. 17044 * 17045 * Context: May be called from interrupt context 17046 */ 17047 17048 static void 17049 sd_sense_key_not_ready(struct sd_lun *un, 17050 uint8_t asc, uint8_t ascq, 17051 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17052 { 17053 struct sd_sense_info si; 17054 17055 ASSERT(un != NULL); 17056 ASSERT(mutex_owned(SD_MUTEX(un))); 17057 ASSERT(bp != NULL); 17058 ASSERT(xp != NULL); 17059 ASSERT(pktp != NULL); 17060 17061 si.ssi_severity = SCSI_ERR_FATAL; 17062 si.ssi_pfa_flag = FALSE; 17063 17064 /* 17065 * Update error stats after first NOT READY error. Disks may have 17066 * been powered down and may need to be restarted. For CDROMs, 17067 * report NOT READY errors only if media is present. 17068 */ 17069 if ((ISCD(un) && (un->un_f_geometry_is_valid == TRUE)) || 17070 (xp->xb_retry_count > 0)) { 17071 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17072 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 17073 } 17074 17075 /* 17076 * Just fail if the "not ready" retry limit has been reached. 17077 */ 17078 if (xp->xb_retry_count >= un->un_notready_retry_count) { 17079 /* Special check for error message printing for removables. */ 17080 if ((ISREMOVABLE(un)) && (asc == 0x04) && 17081 (ascq >= 0x04)) { 17082 si.ssi_severity = SCSI_ERR_ALL; 17083 } 17084 goto fail_command; 17085 } 17086 17087 /* 17088 * Check the ASC and ASCQ in the sense data as needed, to determine 17089 * what to do. 17090 */ 17091 switch (asc) { 17092 case 0x04: /* LOGICAL UNIT NOT READY */ 17093 /* 17094 * disk drives that don't spin up result in a very long delay 17095 * in format without warning messages. We will log a message 17096 * if the error level is set to verbose. 17097 */ 17098 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17099 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17100 "logical unit not ready, resetting disk\n"); 17101 } 17102 17103 /* 17104 * There are different requirements for CDROMs and disks for 17105 * the number of retries. If a CD-ROM is giving this, it is 17106 * probably reading TOC and is in the process of getting 17107 * ready, so we should keep on trying for a long time to make 17108 * sure that all types of media are taken in account (for 17109 * some media the drive takes a long time to read TOC). For 17110 * disks we do not want to retry this too many times as this 17111 * can cause a long hang in format when the drive refuses to 17112 * spin up (a very common failure). 17113 */ 17114 switch (ascq) { 17115 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 17116 /* 17117 * Disk drives frequently refuse to spin up which 17118 * results in a very long hang in format without 17119 * warning messages. 17120 * 17121 * Note: This code preserves the legacy behavior of 17122 * comparing xb_retry_count against zero for fibre 17123 * channel targets instead of comparing against the 17124 * un_reset_retry_count value. The reason for this 17125 * discrepancy has been so utterly lost beneath the 17126 * Sands of Time that even Indiana Jones could not 17127 * find it. 17128 */ 17129 if (un->un_f_is_fibre == TRUE) { 17130 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17131 (xp->xb_retry_count > 0)) && 17132 (un->un_startstop_timeid == NULL)) { 17133 scsi_log(SD_DEVINFO(un), sd_label, 17134 CE_WARN, "logical unit not ready, " 17135 "resetting disk\n"); 17136 sd_reset_target(un, pktp); 17137 } 17138 } else { 17139 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17140 (xp->xb_retry_count > 17141 un->un_reset_retry_count)) && 17142 (un->un_startstop_timeid == NULL)) { 17143 scsi_log(SD_DEVINFO(un), sd_label, 17144 CE_WARN, "logical unit not ready, " 17145 "resetting disk\n"); 17146 sd_reset_target(un, pktp); 17147 } 17148 } 17149 break; 17150 17151 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 17152 /* 17153 * If the target is in the process of becoming 17154 * ready, just proceed with the retry. This can 17155 * happen with CD-ROMs that take a long time to 17156 * read TOC after a power cycle or reset. 17157 */ 17158 goto do_retry; 17159 17160 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 17161 break; 17162 17163 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 17164 /* 17165 * Retries cannot help here so just fail right away. 17166 */ 17167 goto fail_command; 17168 17169 case 0x88: 17170 /* 17171 * Vendor-unique code for T3/T4: it indicates a 17172 * path problem in a mutipathed config, but as far as 17173 * the target driver is concerned it equates to a fatal 17174 * error, so we should just fail the command right away 17175 * (without printing anything to the console). If this 17176 * is not a T3/T4, fall thru to the default recovery 17177 * action. 17178 * T3/T4 is FC only, don't need to check is_fibre 17179 */ 17180 if (SD_IS_T3(un) || SD_IS_T4(un)) { 17181 sd_return_failed_command(un, bp, EIO); 17182 return; 17183 } 17184 /* FALLTHRU */ 17185 17186 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 17187 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 17188 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 17189 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 17190 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 17191 default: /* Possible future codes in SCSI spec? */ 17192 /* 17193 * For removable-media devices, do not retry if 17194 * ASCQ > 2 as these result mostly from USCSI commands 17195 * on MMC devices issued to check status of an 17196 * operation initiated in immediate mode. Also for 17197 * ASCQ >= 4 do not print console messages as these 17198 * mainly represent a user-initiated operation 17199 * instead of a system failure. 17200 */ 17201 if (ISREMOVABLE(un)) { 17202 si.ssi_severity = SCSI_ERR_ALL; 17203 goto fail_command; 17204 } 17205 break; 17206 } 17207 17208 /* 17209 * As part of our recovery attempt for the NOT READY 17210 * condition, we issue a START STOP UNIT command. However 17211 * we want to wait for a short delay before attempting this 17212 * as there may still be more commands coming back from the 17213 * target with the check condition. To do this we use 17214 * timeout(9F) to call sd_start_stop_unit_callback() after 17215 * the delay interval expires. (sd_start_stop_unit_callback() 17216 * dispatches sd_start_stop_unit_task(), which will issue 17217 * the actual START STOP UNIT command. The delay interval 17218 * is one-half of the delay that we will use to retry the 17219 * command that generated the NOT READY condition. 17220 * 17221 * Note that we could just dispatch sd_start_stop_unit_task() 17222 * from here and allow it to sleep for the delay interval, 17223 * but then we would be tying up the taskq thread 17224 * uncesessarily for the duration of the delay. 17225 * 17226 * Do not issue the START STOP UNIT if the current command 17227 * is already a START STOP UNIT. 17228 */ 17229 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 17230 break; 17231 } 17232 17233 /* 17234 * Do not schedule the timeout if one is already pending. 17235 */ 17236 if (un->un_startstop_timeid != NULL) { 17237 SD_INFO(SD_LOG_ERROR, un, 17238 "sd_sense_key_not_ready: restart already issued to" 17239 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 17240 ddi_get_instance(SD_DEVINFO(un))); 17241 break; 17242 } 17243 17244 /* 17245 * Schedule the START STOP UNIT command, then queue the command 17246 * for a retry. 17247 * 17248 * Note: A timeout is not scheduled for this retry because we 17249 * want the retry to be serial with the START_STOP_UNIT. The 17250 * retry will be started when the START_STOP_UNIT is completed 17251 * in sd_start_stop_unit_task. 17252 */ 17253 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 17254 un, SD_BSY_TIMEOUT / 2); 17255 xp->xb_retry_count++; 17256 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 17257 return; 17258 17259 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 17260 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17261 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17262 "unit does not respond to selection\n"); 17263 } 17264 break; 17265 17266 case 0x3A: /* MEDIUM NOT PRESENT */ 17267 if (sd_error_level >= SCSI_ERR_FATAL) { 17268 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17269 "Caddy not inserted in drive\n"); 17270 } 17271 17272 sr_ejected(un); 17273 un->un_mediastate = DKIO_EJECTED; 17274 /* The state has changed, inform the media watch routines */ 17275 cv_broadcast(&un->un_state_cv); 17276 /* Just fail if no media is present in the drive. */ 17277 goto fail_command; 17278 17279 default: 17280 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17281 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 17282 "Unit not Ready. Additional sense code 0x%x\n", 17283 asc); 17284 } 17285 break; 17286 } 17287 17288 do_retry: 17289 17290 /* 17291 * Retry the command, as some targets may report NOT READY for 17292 * several seconds after being reset. 17293 */ 17294 xp->xb_retry_count++; 17295 si.ssi_severity = SCSI_ERR_RETRYABLE; 17296 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 17297 &si, EIO, SD_BSY_TIMEOUT, NULL); 17298 17299 return; 17300 17301 fail_command: 17302 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17303 sd_return_failed_command(un, bp, EIO); 17304 } 17305 17306 17307 17308 /* 17309 * Function: sd_sense_key_medium_or_hardware_error 17310 * 17311 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 17312 * sense key. 17313 * 17314 * Context: May be called from interrupt context 17315 */ 17316 17317 static void 17318 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 17319 int sense_key, uint8_t asc, 17320 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17321 { 17322 struct sd_sense_info si; 17323 17324 ASSERT(un != NULL); 17325 ASSERT(mutex_owned(SD_MUTEX(un))); 17326 ASSERT(bp != NULL); 17327 ASSERT(xp != NULL); 17328 ASSERT(pktp != NULL); 17329 17330 si.ssi_severity = SCSI_ERR_FATAL; 17331 si.ssi_pfa_flag = FALSE; 17332 17333 if (sense_key == KEY_MEDIUM_ERROR) { 17334 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 17335 } 17336 17337 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17338 17339 if ((un->un_reset_retry_count != 0) && 17340 (xp->xb_retry_count == un->un_reset_retry_count)) { 17341 mutex_exit(SD_MUTEX(un)); 17342 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 17343 if (un->un_f_allow_bus_device_reset == TRUE) { 17344 17345 boolean_t try_resetting_target = B_TRUE; 17346 17347 /* 17348 * We need to be able to handle specific ASC when we are 17349 * handling a KEY_HARDWARE_ERROR. In particular 17350 * taking the default action of resetting the target may 17351 * not be the appropriate way to attempt recovery. 17352 * Resetting a target because of a single LUN failure 17353 * victimizes all LUNs on that target. 17354 * 17355 * This is true for the LSI arrays, if an LSI 17356 * array controller returns an ASC of 0x84 (LUN Dead) we 17357 * should trust it. 17358 */ 17359 17360 if (sense_key == KEY_HARDWARE_ERROR) { 17361 switch (asc) { 17362 case 0x84: 17363 if (SD_IS_LSI(un)) { 17364 try_resetting_target = B_FALSE; 17365 } 17366 break; 17367 default: 17368 break; 17369 } 17370 } 17371 17372 if (try_resetting_target == B_TRUE) { 17373 int reset_retval = 0; 17374 if (un->un_f_lun_reset_enabled == TRUE) { 17375 SD_TRACE(SD_LOG_IO_CORE, un, 17376 "sd_sense_key_medium_or_hardware_" 17377 "error: issuing RESET_LUN\n"); 17378 reset_retval = 17379 scsi_reset(SD_ADDRESS(un), 17380 RESET_LUN); 17381 } 17382 if (reset_retval == 0) { 17383 SD_TRACE(SD_LOG_IO_CORE, un, 17384 "sd_sense_key_medium_or_hardware_" 17385 "error: issuing RESET_TARGET\n"); 17386 (void) scsi_reset(SD_ADDRESS(un), 17387 RESET_TARGET); 17388 } 17389 } 17390 } 17391 mutex_enter(SD_MUTEX(un)); 17392 } 17393 17394 /* 17395 * This really ought to be a fatal error, but we will retry anyway 17396 * as some drives report this as a spurious error. 17397 */ 17398 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17399 &si, EIO, (clock_t)0, NULL); 17400 } 17401 17402 17403 17404 /* 17405 * Function: sd_sense_key_illegal_request 17406 * 17407 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 17408 * 17409 * Context: May be called from interrupt context 17410 */ 17411 17412 static void 17413 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 17414 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17415 { 17416 struct sd_sense_info si; 17417 17418 ASSERT(un != NULL); 17419 ASSERT(mutex_owned(SD_MUTEX(un))); 17420 ASSERT(bp != NULL); 17421 ASSERT(xp != NULL); 17422 ASSERT(pktp != NULL); 17423 17424 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17425 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 17426 17427 si.ssi_severity = SCSI_ERR_INFO; 17428 si.ssi_pfa_flag = FALSE; 17429 17430 /* Pointless to retry if the target thinks it's an illegal request */ 17431 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17432 sd_return_failed_command(un, bp, EIO); 17433 } 17434 17435 17436 17437 17438 /* 17439 * Function: sd_sense_key_unit_attention 17440 * 17441 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 17442 * 17443 * Context: May be called from interrupt context 17444 */ 17445 17446 static void 17447 sd_sense_key_unit_attention(struct sd_lun *un, 17448 uint8_t asc, 17449 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17450 { 17451 /* 17452 * For UNIT ATTENTION we allow retries for one minute. Devices 17453 * like Sonoma can return UNIT ATTENTION close to a minute 17454 * under certain conditions. 17455 */ 17456 int retry_check_flag = SD_RETRIES_UA; 17457 struct sd_sense_info si; 17458 17459 ASSERT(un != NULL); 17460 ASSERT(mutex_owned(SD_MUTEX(un))); 17461 ASSERT(bp != NULL); 17462 ASSERT(xp != NULL); 17463 ASSERT(pktp != NULL); 17464 17465 si.ssi_severity = SCSI_ERR_INFO; 17466 si.ssi_pfa_flag = FALSE; 17467 17468 17469 switch (asc) { 17470 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 17471 if (sd_report_pfa != 0) { 17472 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17473 si.ssi_pfa_flag = TRUE; 17474 retry_check_flag = SD_RETRIES_STANDARD; 17475 goto do_retry; 17476 } 17477 break; 17478 17479 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 17480 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 17481 un->un_resvd_status |= 17482 (SD_LOST_RESERVE | SD_WANT_RESERVE); 17483 } 17484 /* FALLTHRU */ 17485 17486 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 17487 if (!ISREMOVABLE(un)) { 17488 break; 17489 } 17490 17491 /* 17492 * When we get a unit attention from a removable-media device, 17493 * it may be in a state that will take a long time to recover 17494 * (e.g., from a reset). Since we are executing in interrupt 17495 * context here, we cannot wait around for the device to come 17496 * back. So hand this command off to sd_media_change_task() 17497 * for deferred processing under taskq thread context. (Note 17498 * that the command still may be failed if a problem is 17499 * encountered at a later time.) 17500 */ 17501 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 17502 KM_NOSLEEP) == 0) { 17503 /* 17504 * Cannot dispatch the request so fail the command. 17505 */ 17506 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17507 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17508 si.ssi_severity = SCSI_ERR_FATAL; 17509 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17510 sd_return_failed_command(un, bp, EIO); 17511 } 17512 /* 17513 * Either the command has been successfully dispatched to a 17514 * task Q for retrying, or the dispatch failed. In either case 17515 * do NOT retry again by calling sd_retry_command. This sets up 17516 * two retries of the same command and when one completes and 17517 * frees the resources the other will access freed memory, 17518 * a bad thing. 17519 */ 17520 return; 17521 17522 default: 17523 break; 17524 } 17525 17526 if (!ISREMOVABLE(un)) { 17527 /* 17528 * Do not update these here for removables. For removables 17529 * these stats are updated (1) above if we failed to dispatch 17530 * sd_media_change_task(), or (2) sd_media_change_task() may 17531 * update these later if it encounters an error. 17532 */ 17533 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17534 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17535 } 17536 17537 do_retry: 17538 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 17539 EIO, SD_UA_RETRY_DELAY, NULL); 17540 } 17541 17542 17543 17544 /* 17545 * Function: sd_sense_key_fail_command 17546 * 17547 * Description: Use to fail a command when we don't like the sense key that 17548 * was returned. 17549 * 17550 * Context: May be called from interrupt context 17551 */ 17552 17553 static void 17554 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 17555 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17556 { 17557 struct sd_sense_info si; 17558 17559 ASSERT(un != NULL); 17560 ASSERT(mutex_owned(SD_MUTEX(un))); 17561 ASSERT(bp != NULL); 17562 ASSERT(xp != NULL); 17563 ASSERT(pktp != NULL); 17564 17565 si.ssi_severity = SCSI_ERR_FATAL; 17566 si.ssi_pfa_flag = FALSE; 17567 17568 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17569 sd_return_failed_command(un, bp, EIO); 17570 } 17571 17572 17573 17574 /* 17575 * Function: sd_sense_key_blank_check 17576 * 17577 * Description: Recovery actions for a SCSI "Blank Check" sense key. 17578 * Has no monetary connotation. 17579 * 17580 * Context: May be called from interrupt context 17581 */ 17582 17583 static void 17584 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 17585 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17586 { 17587 struct sd_sense_info si; 17588 17589 ASSERT(un != NULL); 17590 ASSERT(mutex_owned(SD_MUTEX(un))); 17591 ASSERT(bp != NULL); 17592 ASSERT(xp != NULL); 17593 ASSERT(pktp != NULL); 17594 17595 /* 17596 * Blank check is not fatal for removable devices, therefore 17597 * it does not require a console message. 17598 */ 17599 si.ssi_severity = (ISREMOVABLE(un)) ? SCSI_ERR_ALL : SCSI_ERR_FATAL; 17600 si.ssi_pfa_flag = FALSE; 17601 17602 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17603 sd_return_failed_command(un, bp, EIO); 17604 } 17605 17606 17607 17608 17609 /* 17610 * Function: sd_sense_key_aborted_command 17611 * 17612 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 17613 * 17614 * Context: May be called from interrupt context 17615 */ 17616 17617 static void 17618 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 17619 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17620 { 17621 struct sd_sense_info si; 17622 17623 ASSERT(un != NULL); 17624 ASSERT(mutex_owned(SD_MUTEX(un))); 17625 ASSERT(bp != NULL); 17626 ASSERT(xp != NULL); 17627 ASSERT(pktp != NULL); 17628 17629 si.ssi_severity = SCSI_ERR_FATAL; 17630 si.ssi_pfa_flag = FALSE; 17631 17632 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17633 17634 /* 17635 * This really ought to be a fatal error, but we will retry anyway 17636 * as some drives report this as a spurious error. 17637 */ 17638 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17639 &si, EIO, (clock_t)0, NULL); 17640 } 17641 17642 17643 17644 /* 17645 * Function: sd_sense_key_default 17646 * 17647 * Description: Default recovery action for several SCSI sense keys (basically 17648 * attempts a retry). 17649 * 17650 * Context: May be called from interrupt context 17651 */ 17652 17653 static void 17654 sd_sense_key_default(struct sd_lun *un, 17655 int sense_key, 17656 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17657 { 17658 struct sd_sense_info si; 17659 17660 ASSERT(un != NULL); 17661 ASSERT(mutex_owned(SD_MUTEX(un))); 17662 ASSERT(bp != NULL); 17663 ASSERT(xp != NULL); 17664 ASSERT(pktp != NULL); 17665 17666 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17667 17668 /* 17669 * Undecoded sense key. Attempt retries and hope that will fix 17670 * the problem. Otherwise, we're dead. 17671 */ 17672 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17673 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17674 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 17675 } 17676 17677 si.ssi_severity = SCSI_ERR_FATAL; 17678 si.ssi_pfa_flag = FALSE; 17679 17680 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17681 &si, EIO, (clock_t)0, NULL); 17682 } 17683 17684 17685 17686 /* 17687 * Function: sd_print_retry_msg 17688 * 17689 * Description: Print a message indicating the retry action being taken. 17690 * 17691 * Arguments: un - ptr to associated softstate 17692 * bp - ptr to buf(9S) for the command 17693 * arg - not used. 17694 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17695 * or SD_NO_RETRY_ISSUED 17696 * 17697 * Context: May be called from interrupt context 17698 */ 17699 /* ARGSUSED */ 17700 static void 17701 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 17702 { 17703 struct sd_xbuf *xp; 17704 struct scsi_pkt *pktp; 17705 char *reasonp; 17706 char *msgp; 17707 17708 ASSERT(un != NULL); 17709 ASSERT(mutex_owned(SD_MUTEX(un))); 17710 ASSERT(bp != NULL); 17711 pktp = SD_GET_PKTP(bp); 17712 ASSERT(pktp != NULL); 17713 xp = SD_GET_XBUF(bp); 17714 ASSERT(xp != NULL); 17715 17716 ASSERT(!mutex_owned(&un->un_pm_mutex)); 17717 mutex_enter(&un->un_pm_mutex); 17718 if ((un->un_state == SD_STATE_SUSPENDED) || 17719 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 17720 (pktp->pkt_flags & FLAG_SILENT)) { 17721 mutex_exit(&un->un_pm_mutex); 17722 goto update_pkt_reason; 17723 } 17724 mutex_exit(&un->un_pm_mutex); 17725 17726 /* 17727 * Suppress messages if they are all the same pkt_reason; with 17728 * TQ, many (up to 256) are returned with the same pkt_reason. 17729 * If we are in panic, then suppress the retry messages. 17730 */ 17731 switch (flag) { 17732 case SD_NO_RETRY_ISSUED: 17733 msgp = "giving up"; 17734 break; 17735 case SD_IMMEDIATE_RETRY_ISSUED: 17736 case SD_DELAYED_RETRY_ISSUED: 17737 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 17738 ((pktp->pkt_reason == un->un_last_pkt_reason) && 17739 (sd_error_level != SCSI_ERR_ALL))) { 17740 return; 17741 } 17742 msgp = "retrying command"; 17743 break; 17744 default: 17745 goto update_pkt_reason; 17746 } 17747 17748 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 17749 scsi_rname(pktp->pkt_reason)); 17750 17751 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17752 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 17753 17754 update_pkt_reason: 17755 /* 17756 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 17757 * This is to prevent multiple console messages for the same failure 17758 * condition. Note that un->un_last_pkt_reason is NOT restored if & 17759 * when the command is retried successfully because there still may be 17760 * more commands coming back with the same value of pktp->pkt_reason. 17761 */ 17762 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 17763 un->un_last_pkt_reason = pktp->pkt_reason; 17764 } 17765 } 17766 17767 17768 /* 17769 * Function: sd_print_cmd_incomplete_msg 17770 * 17771 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 17772 * 17773 * Arguments: un - ptr to associated softstate 17774 * bp - ptr to buf(9S) for the command 17775 * arg - passed to sd_print_retry_msg() 17776 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17777 * or SD_NO_RETRY_ISSUED 17778 * 17779 * Context: May be called from interrupt context 17780 */ 17781 17782 static void 17783 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 17784 int code) 17785 { 17786 dev_info_t *dip; 17787 17788 ASSERT(un != NULL); 17789 ASSERT(mutex_owned(SD_MUTEX(un))); 17790 ASSERT(bp != NULL); 17791 17792 switch (code) { 17793 case SD_NO_RETRY_ISSUED: 17794 /* Command was failed. Someone turned off this target? */ 17795 if (un->un_state != SD_STATE_OFFLINE) { 17796 /* 17797 * Suppress message if we are detaching and 17798 * device has been disconnected 17799 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 17800 * private interface and not part of the DDI 17801 */ 17802 dip = un->un_sd->sd_dev; 17803 if (!(DEVI_IS_DETACHING(dip) && 17804 DEVI_IS_DEVICE_REMOVED(dip))) { 17805 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17806 "disk not responding to selection\n"); 17807 } 17808 New_state(un, SD_STATE_OFFLINE); 17809 } 17810 break; 17811 17812 case SD_DELAYED_RETRY_ISSUED: 17813 case SD_IMMEDIATE_RETRY_ISSUED: 17814 default: 17815 /* Command was successfully queued for retry */ 17816 sd_print_retry_msg(un, bp, arg, code); 17817 break; 17818 } 17819 } 17820 17821 17822 /* 17823 * Function: sd_pkt_reason_cmd_incomplete 17824 * 17825 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 17826 * 17827 * Context: May be called from interrupt context 17828 */ 17829 17830 static void 17831 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 17832 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17833 { 17834 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 17835 17836 ASSERT(un != NULL); 17837 ASSERT(mutex_owned(SD_MUTEX(un))); 17838 ASSERT(bp != NULL); 17839 ASSERT(xp != NULL); 17840 ASSERT(pktp != NULL); 17841 17842 /* Do not do a reset if selection did not complete */ 17843 /* Note: Should this not just check the bit? */ 17844 if (pktp->pkt_state != STATE_GOT_BUS) { 17845 SD_UPDATE_ERRSTATS(un, sd_transerrs); 17846 sd_reset_target(un, pktp); 17847 } 17848 17849 /* 17850 * If the target was not successfully selected, then set 17851 * SD_RETRIES_FAILFAST to indicate that we lost communication 17852 * with the target, and further retries and/or commands are 17853 * likely to take a long time. 17854 */ 17855 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 17856 flag |= SD_RETRIES_FAILFAST; 17857 } 17858 17859 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17860 17861 sd_retry_command(un, bp, flag, 17862 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17863 } 17864 17865 17866 17867 /* 17868 * Function: sd_pkt_reason_cmd_tran_err 17869 * 17870 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 17871 * 17872 * Context: May be called from interrupt context 17873 */ 17874 17875 static void 17876 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 17877 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17878 { 17879 ASSERT(un != NULL); 17880 ASSERT(mutex_owned(SD_MUTEX(un))); 17881 ASSERT(bp != NULL); 17882 ASSERT(xp != NULL); 17883 ASSERT(pktp != NULL); 17884 17885 /* 17886 * Do not reset if we got a parity error, or if 17887 * selection did not complete. 17888 */ 17889 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17890 /* Note: Should this not just check the bit for pkt_state? */ 17891 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 17892 (pktp->pkt_state != STATE_GOT_BUS)) { 17893 SD_UPDATE_ERRSTATS(un, sd_transerrs); 17894 sd_reset_target(un, pktp); 17895 } 17896 17897 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17898 17899 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 17900 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17901 } 17902 17903 17904 17905 /* 17906 * Function: sd_pkt_reason_cmd_reset 17907 * 17908 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 17909 * 17910 * Context: May be called from interrupt context 17911 */ 17912 17913 static void 17914 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 17915 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17916 { 17917 ASSERT(un != NULL); 17918 ASSERT(mutex_owned(SD_MUTEX(un))); 17919 ASSERT(bp != NULL); 17920 ASSERT(xp != NULL); 17921 ASSERT(pktp != NULL); 17922 17923 /* The target may still be running the command, so try to reset. */ 17924 SD_UPDATE_ERRSTATS(un, sd_transerrs); 17925 sd_reset_target(un, pktp); 17926 17927 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17928 17929 /* 17930 * If pkt_reason is CMD_RESET chances are that this pkt got 17931 * reset because another target on this bus caused it. The target 17932 * that caused it should get CMD_TIMEOUT with pkt_statistics 17933 * of STAT_TIMEOUT/STAT_DEV_RESET. 17934 */ 17935 17936 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 17937 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17938 } 17939 17940 17941 17942 17943 /* 17944 * Function: sd_pkt_reason_cmd_aborted 17945 * 17946 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 17947 * 17948 * Context: May be called from interrupt context 17949 */ 17950 17951 static void 17952 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 17953 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17954 { 17955 ASSERT(un != NULL); 17956 ASSERT(mutex_owned(SD_MUTEX(un))); 17957 ASSERT(bp != NULL); 17958 ASSERT(xp != NULL); 17959 ASSERT(pktp != NULL); 17960 17961 /* The target may still be running the command, so try to reset. */ 17962 SD_UPDATE_ERRSTATS(un, sd_transerrs); 17963 sd_reset_target(un, pktp); 17964 17965 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17966 17967 /* 17968 * If pkt_reason is CMD_ABORTED chances are that this pkt got 17969 * aborted because another target on this bus caused it. The target 17970 * that caused it should get CMD_TIMEOUT with pkt_statistics 17971 * of STAT_TIMEOUT/STAT_DEV_RESET. 17972 */ 17973 17974 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 17975 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17976 } 17977 17978 17979 17980 /* 17981 * Function: sd_pkt_reason_cmd_timeout 17982 * 17983 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 17984 * 17985 * Context: May be called from interrupt context 17986 */ 17987 17988 static void 17989 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 17990 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17991 { 17992 ASSERT(un != NULL); 17993 ASSERT(mutex_owned(SD_MUTEX(un))); 17994 ASSERT(bp != NULL); 17995 ASSERT(xp != NULL); 17996 ASSERT(pktp != NULL); 17997 17998 17999 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18000 sd_reset_target(un, pktp); 18001 18002 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18003 18004 /* 18005 * A command timeout indicates that we could not establish 18006 * communication with the target, so set SD_RETRIES_FAILFAST 18007 * as further retries/commands are likely to take a long time. 18008 */ 18009 sd_retry_command(un, bp, 18010 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 18011 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18012 } 18013 18014 18015 18016 /* 18017 * Function: sd_pkt_reason_cmd_unx_bus_free 18018 * 18019 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 18020 * 18021 * Context: May be called from interrupt context 18022 */ 18023 18024 static void 18025 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 18026 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18027 { 18028 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 18029 18030 ASSERT(un != NULL); 18031 ASSERT(mutex_owned(SD_MUTEX(un))); 18032 ASSERT(bp != NULL); 18033 ASSERT(xp != NULL); 18034 ASSERT(pktp != NULL); 18035 18036 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18037 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18038 18039 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 18040 sd_print_retry_msg : NULL; 18041 18042 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18043 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18044 } 18045 18046 18047 /* 18048 * Function: sd_pkt_reason_cmd_tag_reject 18049 * 18050 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 18051 * 18052 * Context: May be called from interrupt context 18053 */ 18054 18055 static void 18056 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 18057 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18058 { 18059 ASSERT(un != NULL); 18060 ASSERT(mutex_owned(SD_MUTEX(un))); 18061 ASSERT(bp != NULL); 18062 ASSERT(xp != NULL); 18063 ASSERT(pktp != NULL); 18064 18065 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18066 pktp->pkt_flags = 0; 18067 un->un_tagflags = 0; 18068 if (un->un_f_opt_queueing == TRUE) { 18069 un->un_throttle = min(un->un_throttle, 3); 18070 } else { 18071 un->un_throttle = 1; 18072 } 18073 mutex_exit(SD_MUTEX(un)); 18074 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 18075 mutex_enter(SD_MUTEX(un)); 18076 18077 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18078 18079 /* Legacy behavior not to check retry counts here. */ 18080 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 18081 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18082 } 18083 18084 18085 /* 18086 * Function: sd_pkt_reason_default 18087 * 18088 * Description: Default recovery actions for SCSA pkt_reason values that 18089 * do not have more explicit recovery actions. 18090 * 18091 * Context: May be called from interrupt context 18092 */ 18093 18094 static void 18095 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 18096 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18097 { 18098 ASSERT(un != NULL); 18099 ASSERT(mutex_owned(SD_MUTEX(un))); 18100 ASSERT(bp != NULL); 18101 ASSERT(xp != NULL); 18102 ASSERT(pktp != NULL); 18103 18104 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18105 sd_reset_target(un, pktp); 18106 18107 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18108 18109 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18110 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18111 } 18112 18113 18114 18115 /* 18116 * Function: sd_pkt_status_check_condition 18117 * 18118 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 18119 * 18120 * Context: May be called from interrupt context 18121 */ 18122 18123 static void 18124 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 18125 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18126 { 18127 ASSERT(un != NULL); 18128 ASSERT(mutex_owned(SD_MUTEX(un))); 18129 ASSERT(bp != NULL); 18130 ASSERT(xp != NULL); 18131 ASSERT(pktp != NULL); 18132 18133 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 18134 "entry: buf:0x%p xp:0x%p\n", bp, xp); 18135 18136 /* 18137 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 18138 * command will be retried after the request sense). Otherwise, retry 18139 * the command. Note: we are issuing the request sense even though the 18140 * retry limit may have been reached for the failed command. 18141 */ 18142 if (un->un_f_arq_enabled == FALSE) { 18143 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18144 "no ARQ, sending request sense command\n"); 18145 sd_send_request_sense_command(un, bp, pktp); 18146 } else { 18147 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18148 "ARQ,retrying request sense command\n"); 18149 #if defined(__i386) || defined(__amd64) 18150 /* 18151 * The SD_RETRY_DELAY value need to be adjusted here 18152 * when SD_RETRY_DELAY change in sddef.h 18153 */ 18154 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 0, 18155 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 18156 NULL); 18157 #else 18158 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 18159 0, SD_RETRY_DELAY, NULL); 18160 #endif 18161 } 18162 18163 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 18164 } 18165 18166 18167 /* 18168 * Function: sd_pkt_status_busy 18169 * 18170 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 18171 * 18172 * Context: May be called from interrupt context 18173 */ 18174 18175 static void 18176 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 18177 struct scsi_pkt *pktp) 18178 { 18179 ASSERT(un != NULL); 18180 ASSERT(mutex_owned(SD_MUTEX(un))); 18181 ASSERT(bp != NULL); 18182 ASSERT(xp != NULL); 18183 ASSERT(pktp != NULL); 18184 18185 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18186 "sd_pkt_status_busy: entry\n"); 18187 18188 /* If retries are exhausted, just fail the command. */ 18189 if (xp->xb_retry_count >= un->un_busy_retry_count) { 18190 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18191 "device busy too long\n"); 18192 sd_return_failed_command(un, bp, EIO); 18193 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18194 "sd_pkt_status_busy: exit\n"); 18195 return; 18196 } 18197 xp->xb_retry_count++; 18198 18199 /* 18200 * Try to reset the target. However, we do not want to perform 18201 * more than one reset if the device continues to fail. The reset 18202 * will be performed when the retry count reaches the reset 18203 * threshold. This threshold should be set such that at least 18204 * one retry is issued before the reset is performed. 18205 */ 18206 if (xp->xb_retry_count == 18207 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 18208 int rval = 0; 18209 mutex_exit(SD_MUTEX(un)); 18210 if (un->un_f_allow_bus_device_reset == TRUE) { 18211 /* 18212 * First try to reset the LUN; if we cannot then 18213 * try to reset the target. 18214 */ 18215 if (un->un_f_lun_reset_enabled == TRUE) { 18216 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18217 "sd_pkt_status_busy: RESET_LUN\n"); 18218 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 18219 } 18220 if (rval == 0) { 18221 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18222 "sd_pkt_status_busy: RESET_TARGET\n"); 18223 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 18224 } 18225 } 18226 if (rval == 0) { 18227 /* 18228 * If the RESET_LUN and/or RESET_TARGET failed, 18229 * try RESET_ALL 18230 */ 18231 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18232 "sd_pkt_status_busy: RESET_ALL\n"); 18233 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 18234 } 18235 mutex_enter(SD_MUTEX(un)); 18236 if (rval == 0) { 18237 /* 18238 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 18239 * At this point we give up & fail the command. 18240 */ 18241 sd_return_failed_command(un, bp, EIO); 18242 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18243 "sd_pkt_status_busy: exit (failed cmd)\n"); 18244 return; 18245 } 18246 } 18247 18248 /* 18249 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 18250 * we have already checked the retry counts above. 18251 */ 18252 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 18253 EIO, SD_BSY_TIMEOUT, NULL); 18254 18255 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18256 "sd_pkt_status_busy: exit\n"); 18257 } 18258 18259 18260 /* 18261 * Function: sd_pkt_status_reservation_conflict 18262 * 18263 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 18264 * command status. 18265 * 18266 * Context: May be called from interrupt context 18267 */ 18268 18269 static void 18270 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 18271 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18272 { 18273 ASSERT(un != NULL); 18274 ASSERT(mutex_owned(SD_MUTEX(un))); 18275 ASSERT(bp != NULL); 18276 ASSERT(xp != NULL); 18277 ASSERT(pktp != NULL); 18278 18279 /* 18280 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 18281 * conflict could be due to various reasons like incorrect keys, not 18282 * registered or not reserved etc. So, we return EACCES to the caller. 18283 */ 18284 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 18285 int cmd = SD_GET_PKT_OPCODE(pktp); 18286 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 18287 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 18288 sd_return_failed_command(un, bp, EACCES); 18289 return; 18290 } 18291 } 18292 18293 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 18294 18295 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 18296 if (sd_failfast_enable != 0) { 18297 /* By definition, we must panic here.... */ 18298 panic("Reservation Conflict"); 18299 /*NOTREACHED*/ 18300 } 18301 SD_ERROR(SD_LOG_IO, un, 18302 "sd_handle_resv_conflict: Disk Reserved\n"); 18303 sd_return_failed_command(un, bp, EACCES); 18304 return; 18305 } 18306 18307 /* 18308 * 1147670: retry only if sd_retry_on_reservation_conflict 18309 * property is set (default is 1). Retries will not succeed 18310 * on a disk reserved by another initiator. HA systems 18311 * may reset this via sd.conf to avoid these retries. 18312 * 18313 * Note: The legacy return code for this failure is EIO, however EACCES 18314 * seems more appropriate for a reservation conflict. 18315 */ 18316 if (sd_retry_on_reservation_conflict == 0) { 18317 SD_ERROR(SD_LOG_IO, un, 18318 "sd_handle_resv_conflict: Device Reserved\n"); 18319 sd_return_failed_command(un, bp, EIO); 18320 return; 18321 } 18322 18323 /* 18324 * Retry the command if we can. 18325 * 18326 * Note: The legacy return code for this failure is EIO, however EACCES 18327 * seems more appropriate for a reservation conflict. 18328 */ 18329 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18330 (clock_t)2, NULL); 18331 } 18332 18333 18334 18335 /* 18336 * Function: sd_pkt_status_qfull 18337 * 18338 * Description: Handle a QUEUE FULL condition from the target. This can 18339 * occur if the HBA does not handle the queue full condition. 18340 * (Basically this means third-party HBAs as Sun HBAs will 18341 * handle the queue full condition.) Note that if there are 18342 * some commands already in the transport, then the queue full 18343 * has occurred because the queue for this nexus is actually 18344 * full. If there are no commands in the transport, then the 18345 * queue full is resulting from some other initiator or lun 18346 * consuming all the resources at the target. 18347 * 18348 * Context: May be called from interrupt context 18349 */ 18350 18351 static void 18352 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 18353 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18354 { 18355 ASSERT(un != NULL); 18356 ASSERT(mutex_owned(SD_MUTEX(un))); 18357 ASSERT(bp != NULL); 18358 ASSERT(xp != NULL); 18359 ASSERT(pktp != NULL); 18360 18361 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18362 "sd_pkt_status_qfull: entry\n"); 18363 18364 /* 18365 * Just lower the QFULL throttle and retry the command. Note that 18366 * we do not limit the number of retries here. 18367 */ 18368 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 18369 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 18370 SD_RESTART_TIMEOUT, NULL); 18371 18372 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18373 "sd_pkt_status_qfull: exit\n"); 18374 } 18375 18376 18377 /* 18378 * Function: sd_reset_target 18379 * 18380 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 18381 * RESET_TARGET, or RESET_ALL. 18382 * 18383 * Context: May be called under interrupt context. 18384 */ 18385 18386 static void 18387 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 18388 { 18389 int rval = 0; 18390 18391 ASSERT(un != NULL); 18392 ASSERT(mutex_owned(SD_MUTEX(un))); 18393 ASSERT(pktp != NULL); 18394 18395 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 18396 18397 /* 18398 * No need to reset if the transport layer has already done so. 18399 */ 18400 if ((pktp->pkt_statistics & 18401 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 18402 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18403 "sd_reset_target: no reset\n"); 18404 return; 18405 } 18406 18407 mutex_exit(SD_MUTEX(un)); 18408 18409 if (un->un_f_allow_bus_device_reset == TRUE) { 18410 if (un->un_f_lun_reset_enabled == TRUE) { 18411 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18412 "sd_reset_target: RESET_LUN\n"); 18413 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 18414 } 18415 if (rval == 0) { 18416 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18417 "sd_reset_target: RESET_TARGET\n"); 18418 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 18419 } 18420 } 18421 18422 if (rval == 0) { 18423 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18424 "sd_reset_target: RESET_ALL\n"); 18425 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 18426 } 18427 18428 mutex_enter(SD_MUTEX(un)); 18429 18430 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 18431 } 18432 18433 18434 /* 18435 * Function: sd_media_change_task 18436 * 18437 * Description: Recovery action for CDROM to become available. 18438 * 18439 * Context: Executes in a taskq() thread context 18440 */ 18441 18442 static void 18443 sd_media_change_task(void *arg) 18444 { 18445 struct scsi_pkt *pktp = arg; 18446 struct sd_lun *un; 18447 struct buf *bp; 18448 struct sd_xbuf *xp; 18449 int err = 0; 18450 int retry_count = 0; 18451 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 18452 struct sd_sense_info si; 18453 18454 ASSERT(pktp != NULL); 18455 bp = (struct buf *)pktp->pkt_private; 18456 ASSERT(bp != NULL); 18457 xp = SD_GET_XBUF(bp); 18458 ASSERT(xp != NULL); 18459 un = SD_GET_UN(bp); 18460 ASSERT(un != NULL); 18461 ASSERT(!mutex_owned(SD_MUTEX(un))); 18462 ASSERT(ISREMOVABLE(un)); 18463 18464 si.ssi_severity = SCSI_ERR_INFO; 18465 si.ssi_pfa_flag = FALSE; 18466 18467 /* 18468 * When a reset is issued on a CDROM, it takes a long time to 18469 * recover. First few attempts to read capacity and other things 18470 * related to handling unit attention fail (with a ASC 0x4 and 18471 * ASCQ 0x1). In that case we want to do enough retries and we want 18472 * to limit the retries in other cases of genuine failures like 18473 * no media in drive. 18474 */ 18475 while (retry_count++ < retry_limit) { 18476 if ((err = sd_handle_mchange(un)) == 0) { 18477 break; 18478 } 18479 if (err == EAGAIN) { 18480 retry_limit = SD_UNIT_ATTENTION_RETRY; 18481 } 18482 /* Sleep for 0.5 sec. & try again */ 18483 delay(drv_usectohz(500000)); 18484 } 18485 18486 /* 18487 * Dispatch (retry or fail) the original command here, 18488 * along with appropriate console messages.... 18489 * 18490 * Must grab the mutex before calling sd_retry_command, 18491 * sd_print_sense_msg and sd_return_failed_command. 18492 */ 18493 mutex_enter(SD_MUTEX(un)); 18494 if (err != SD_CMD_SUCCESS) { 18495 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18496 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18497 si.ssi_severity = SCSI_ERR_FATAL; 18498 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18499 sd_return_failed_command(un, bp, EIO); 18500 } else { 18501 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 18502 &si, EIO, (clock_t)0, NULL); 18503 } 18504 mutex_exit(SD_MUTEX(un)); 18505 } 18506 18507 18508 18509 /* 18510 * Function: sd_handle_mchange 18511 * 18512 * Description: Perform geometry validation & other recovery when CDROM 18513 * has been removed from drive. 18514 * 18515 * Return Code: 0 for success 18516 * errno-type return code of either sd_send_scsi_DOORLOCK() or 18517 * sd_send_scsi_READ_CAPACITY() 18518 * 18519 * Context: Executes in a taskq() thread context 18520 */ 18521 18522 static int 18523 sd_handle_mchange(struct sd_lun *un) 18524 { 18525 uint64_t capacity; 18526 uint32_t lbasize; 18527 int rval; 18528 18529 ASSERT(!mutex_owned(SD_MUTEX(un))); 18530 ASSERT(ISREMOVABLE(un)); 18531 18532 if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 18533 SD_PATH_DIRECT_PRIORITY)) != 0) { 18534 return (rval); 18535 } 18536 18537 mutex_enter(SD_MUTEX(un)); 18538 sd_update_block_info(un, lbasize, capacity); 18539 18540 if (un->un_errstats != NULL) { 18541 struct sd_errstats *stp = 18542 (struct sd_errstats *)un->un_errstats->ks_data; 18543 stp->sd_capacity.value.ui64 = (uint64_t) 18544 ((uint64_t)un->un_blockcount * 18545 (uint64_t)un->un_tgt_blocksize); 18546 } 18547 18548 /* 18549 * Note: Maybe let the strategy/partitioning chain worry about getting 18550 * valid geometry. 18551 */ 18552 un->un_f_geometry_is_valid = FALSE; 18553 (void) sd_validate_geometry(un, SD_PATH_DIRECT_PRIORITY); 18554 if (un->un_f_geometry_is_valid == FALSE) { 18555 mutex_exit(SD_MUTEX(un)); 18556 return (EIO); 18557 } 18558 18559 mutex_exit(SD_MUTEX(un)); 18560 18561 /* 18562 * Try to lock the door 18563 */ 18564 return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 18565 SD_PATH_DIRECT_PRIORITY)); 18566 } 18567 18568 18569 /* 18570 * Function: sd_send_scsi_DOORLOCK 18571 * 18572 * Description: Issue the scsi DOOR LOCK command 18573 * 18574 * Arguments: un - pointer to driver soft state (unit) structure for 18575 * this target. 18576 * flag - SD_REMOVAL_ALLOW 18577 * SD_REMOVAL_PREVENT 18578 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 18579 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 18580 * to use the USCSI "direct" chain and bypass the normal 18581 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 18582 * command is issued as part of an error recovery action. 18583 * 18584 * Return Code: 0 - Success 18585 * errno return code from sd_send_scsi_cmd() 18586 * 18587 * Context: Can sleep. 18588 */ 18589 18590 static int 18591 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag) 18592 { 18593 union scsi_cdb cdb; 18594 struct uscsi_cmd ucmd_buf; 18595 struct scsi_extended_sense sense_buf; 18596 int status; 18597 18598 ASSERT(un != NULL); 18599 ASSERT(!mutex_owned(SD_MUTEX(un))); 18600 18601 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 18602 18603 /* already determined doorlock is not supported, fake success */ 18604 if (un->un_f_doorlock_supported == FALSE) { 18605 return (0); 18606 } 18607 18608 bzero(&cdb, sizeof (cdb)); 18609 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18610 18611 cdb.scc_cmd = SCMD_DOORLOCK; 18612 cdb.cdb_opaque[4] = (uchar_t)flag; 18613 18614 ucmd_buf.uscsi_cdb = (char *)&cdb; 18615 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 18616 ucmd_buf.uscsi_bufaddr = NULL; 18617 ucmd_buf.uscsi_buflen = 0; 18618 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18619 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 18620 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 18621 ucmd_buf.uscsi_timeout = 15; 18622 18623 SD_TRACE(SD_LOG_IO, un, 18624 "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n"); 18625 18626 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 18627 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 18628 18629 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 18630 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18631 (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) { 18632 /* fake success and skip subsequent doorlock commands */ 18633 un->un_f_doorlock_supported = FALSE; 18634 return (0); 18635 } 18636 18637 return (status); 18638 } 18639 18640 18641 /* 18642 * Function: sd_send_scsi_READ_CAPACITY 18643 * 18644 * Description: This routine uses the scsi READ CAPACITY command to determine 18645 * the device capacity in number of blocks and the device native 18646 * block size. If this function returns a failure, then the 18647 * values in *capp and *lbap are undefined. If the capacity 18648 * returned is 0xffffffff then the lun is too large for a 18649 * normal READ CAPACITY command and the results of a 18650 * READ CAPACITY 16 will be used instead. 18651 * 18652 * Arguments: un - ptr to soft state struct for the target 18653 * capp - ptr to unsigned 64-bit variable to receive the 18654 * capacity value from the command. 18655 * lbap - ptr to unsigned 32-bit varaible to receive the 18656 * block size value from the command 18657 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 18658 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 18659 * to use the USCSI "direct" chain and bypass the normal 18660 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 18661 * command is issued as part of an error recovery action. 18662 * 18663 * Return Code: 0 - Success 18664 * EIO - IO error 18665 * EACCES - Reservation conflict detected 18666 * EAGAIN - Device is becoming ready 18667 * errno return code from sd_send_scsi_cmd() 18668 * 18669 * Context: Can sleep. Blocks until command completes. 18670 */ 18671 18672 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 18673 18674 static int 18675 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap, 18676 int path_flag) 18677 { 18678 struct scsi_extended_sense sense_buf; 18679 struct uscsi_cmd ucmd_buf; 18680 union scsi_cdb cdb; 18681 uint32_t *capacity_buf; 18682 uint64_t capacity; 18683 uint32_t lbasize; 18684 int status; 18685 18686 ASSERT(un != NULL); 18687 ASSERT(!mutex_owned(SD_MUTEX(un))); 18688 ASSERT(capp != NULL); 18689 ASSERT(lbap != NULL); 18690 18691 SD_TRACE(SD_LOG_IO, un, 18692 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 18693 18694 /* 18695 * First send a READ_CAPACITY command to the target. 18696 * (This command is mandatory under SCSI-2.) 18697 * 18698 * Set up the CDB for the READ_CAPACITY command. The Partial 18699 * Medium Indicator bit is cleared. The address field must be 18700 * zero if the PMI bit is zero. 18701 */ 18702 bzero(&cdb, sizeof (cdb)); 18703 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18704 18705 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 18706 18707 cdb.scc_cmd = SCMD_READ_CAPACITY; 18708 18709 ucmd_buf.uscsi_cdb = (char *)&cdb; 18710 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 18711 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 18712 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 18713 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18714 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 18715 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 18716 ucmd_buf.uscsi_timeout = 60; 18717 18718 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 18719 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 18720 18721 switch (status) { 18722 case 0: 18723 /* Return failure if we did not get valid capacity data. */ 18724 if (ucmd_buf.uscsi_resid != 0) { 18725 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 18726 return (EIO); 18727 } 18728 18729 /* 18730 * Read capacity and block size from the READ CAPACITY 10 data. 18731 * This data may be adjusted later due to device specific 18732 * issues. 18733 * 18734 * According to the SCSI spec, the READ CAPACITY 10 18735 * command returns the following: 18736 * 18737 * bytes 0-3: Maximum logical block address available. 18738 * (MSB in byte:0 & LSB in byte:3) 18739 * 18740 * bytes 4-7: Block length in bytes 18741 * (MSB in byte:4 & LSB in byte:7) 18742 * 18743 */ 18744 capacity = BE_32(capacity_buf[0]); 18745 lbasize = BE_32(capacity_buf[1]); 18746 18747 /* 18748 * Done with capacity_buf 18749 */ 18750 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 18751 18752 /* 18753 * if the reported capacity is set to all 0xf's, then 18754 * this disk is too large and requires SBC-2 commands. 18755 * Reissue the request using READ CAPACITY 16. 18756 */ 18757 if (capacity == 0xffffffff) { 18758 status = sd_send_scsi_READ_CAPACITY_16(un, &capacity, 18759 &lbasize, path_flag); 18760 if (status != 0) { 18761 return (status); 18762 } 18763 } 18764 break; /* Success! */ 18765 case EIO: 18766 switch (ucmd_buf.uscsi_status) { 18767 case STATUS_RESERVATION_CONFLICT: 18768 status = EACCES; 18769 break; 18770 case STATUS_CHECK: 18771 /* 18772 * Check condition; look for ASC/ASCQ of 0x04/0x01 18773 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 18774 */ 18775 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18776 (sense_buf.es_add_code == 0x04) && 18777 (sense_buf.es_qual_code == 0x01)) { 18778 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 18779 return (EAGAIN); 18780 } 18781 break; 18782 default: 18783 break; 18784 } 18785 /* FALLTHRU */ 18786 default: 18787 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 18788 return (status); 18789 } 18790 18791 /* 18792 * Some ATAPI CD-ROM drives report inaccurate LBA size values 18793 * (2352 and 0 are common) so for these devices always force the value 18794 * to 2048 as required by the ATAPI specs. 18795 */ 18796 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 18797 lbasize = 2048; 18798 } 18799 18800 /* 18801 * Get the maximum LBA value from the READ CAPACITY data. 18802 * Here we assume that the Partial Medium Indicator (PMI) bit 18803 * was cleared when issuing the command. This means that the LBA 18804 * returned from the device is the LBA of the last logical block 18805 * on the logical unit. The actual logical block count will be 18806 * this value plus one. 18807 * 18808 * Currently the capacity is saved in terms of un->un_sys_blocksize, 18809 * so scale the capacity value to reflect this. 18810 */ 18811 capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize); 18812 18813 #if defined(__i386) || defined(__amd64) 18814 /* 18815 * On x86, compensate for off-by-1 error (number of sectors on 18816 * media) (1175930) 18817 */ 18818 if (!ISREMOVABLE(un) && (lbasize == un->un_sys_blocksize)) { 18819 capacity -= 1; 18820 } 18821 #endif 18822 18823 /* 18824 * Copy the values from the READ CAPACITY command into the space 18825 * provided by the caller. 18826 */ 18827 *capp = capacity; 18828 *lbap = lbasize; 18829 18830 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 18831 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 18832 18833 /* 18834 * Both the lbasize and capacity from the device must be nonzero, 18835 * otherwise we assume that the values are not valid and return 18836 * failure to the caller. (4203735) 18837 */ 18838 if ((capacity == 0) || (lbasize == 0)) { 18839 return (EIO); 18840 } 18841 18842 return (0); 18843 } 18844 18845 /* 18846 * Function: sd_send_scsi_READ_CAPACITY_16 18847 * 18848 * Description: This routine uses the scsi READ CAPACITY 16 command to 18849 * determine the device capacity in number of blocks and the 18850 * device native block size. If this function returns a failure, 18851 * then the values in *capp and *lbap are undefined. 18852 * This routine should always be called by 18853 * sd_send_scsi_READ_CAPACITY which will appy any device 18854 * specific adjustments to capacity and lbasize. 18855 * 18856 * Arguments: un - ptr to soft state struct for the target 18857 * capp - ptr to unsigned 64-bit variable to receive the 18858 * capacity value from the command. 18859 * lbap - ptr to unsigned 32-bit varaible to receive the 18860 * block size value from the command 18861 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 18862 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 18863 * to use the USCSI "direct" chain and bypass the normal 18864 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 18865 * this command is issued as part of an error recovery 18866 * action. 18867 * 18868 * Return Code: 0 - Success 18869 * EIO - IO error 18870 * EACCES - Reservation conflict detected 18871 * EAGAIN - Device is becoming ready 18872 * errno return code from sd_send_scsi_cmd() 18873 * 18874 * Context: Can sleep. Blocks until command completes. 18875 */ 18876 18877 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 18878 18879 static int 18880 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 18881 uint32_t *lbap, int path_flag) 18882 { 18883 struct scsi_extended_sense sense_buf; 18884 struct uscsi_cmd ucmd_buf; 18885 union scsi_cdb cdb; 18886 uint64_t *capacity16_buf; 18887 uint64_t capacity; 18888 uint32_t lbasize; 18889 int status; 18890 18891 ASSERT(un != NULL); 18892 ASSERT(!mutex_owned(SD_MUTEX(un))); 18893 ASSERT(capp != NULL); 18894 ASSERT(lbap != NULL); 18895 18896 SD_TRACE(SD_LOG_IO, un, 18897 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 18898 18899 /* 18900 * First send a READ_CAPACITY_16 command to the target. 18901 * 18902 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 18903 * Medium Indicator bit is cleared. The address field must be 18904 * zero if the PMI bit is zero. 18905 */ 18906 bzero(&cdb, sizeof (cdb)); 18907 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18908 18909 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 18910 18911 ucmd_buf.uscsi_cdb = (char *)&cdb; 18912 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 18913 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 18914 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 18915 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18916 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 18917 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 18918 ucmd_buf.uscsi_timeout = 60; 18919 18920 /* 18921 * Read Capacity (16) is a Service Action In command. One 18922 * command byte (0x9E) is overloaded for multiple operations, 18923 * with the second CDB byte specifying the desired operation 18924 */ 18925 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 18926 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 18927 18928 /* 18929 * Fill in allocation length field 18930 */ 18931 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 18932 18933 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 18934 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 18935 18936 switch (status) { 18937 case 0: 18938 /* Return failure if we did not get valid capacity data. */ 18939 if (ucmd_buf.uscsi_resid > 20) { 18940 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 18941 return (EIO); 18942 } 18943 18944 /* 18945 * Read capacity and block size from the READ CAPACITY 10 data. 18946 * This data may be adjusted later due to device specific 18947 * issues. 18948 * 18949 * According to the SCSI spec, the READ CAPACITY 10 18950 * command returns the following: 18951 * 18952 * bytes 0-7: Maximum logical block address available. 18953 * (MSB in byte:0 & LSB in byte:7) 18954 * 18955 * bytes 8-11: Block length in bytes 18956 * (MSB in byte:8 & LSB in byte:11) 18957 * 18958 */ 18959 capacity = BE_64(capacity16_buf[0]); 18960 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 18961 18962 /* 18963 * Done with capacity16_buf 18964 */ 18965 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 18966 18967 /* 18968 * if the reported capacity is set to all 0xf's, then 18969 * this disk is too large. This could only happen with 18970 * a device that supports LBAs larger than 64 bits which 18971 * are not defined by any current T10 standards. 18972 */ 18973 if (capacity == 0xffffffffffffffff) { 18974 return (EIO); 18975 } 18976 break; /* Success! */ 18977 case EIO: 18978 switch (ucmd_buf.uscsi_status) { 18979 case STATUS_RESERVATION_CONFLICT: 18980 status = EACCES; 18981 break; 18982 case STATUS_CHECK: 18983 /* 18984 * Check condition; look for ASC/ASCQ of 0x04/0x01 18985 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 18986 */ 18987 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18988 (sense_buf.es_add_code == 0x04) && 18989 (sense_buf.es_qual_code == 0x01)) { 18990 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 18991 return (EAGAIN); 18992 } 18993 break; 18994 default: 18995 break; 18996 } 18997 /* FALLTHRU */ 18998 default: 18999 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19000 return (status); 19001 } 19002 19003 *capp = capacity; 19004 *lbap = lbasize; 19005 19006 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 19007 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19008 19009 return (0); 19010 } 19011 19012 19013 /* 19014 * Function: sd_send_scsi_START_STOP_UNIT 19015 * 19016 * Description: Issue a scsi START STOP UNIT command to the target. 19017 * 19018 * Arguments: un - pointer to driver soft state (unit) structure for 19019 * this target. 19020 * flag - SD_TARGET_START 19021 * SD_TARGET_STOP 19022 * SD_TARGET_EJECT 19023 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19024 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19025 * to use the USCSI "direct" chain and bypass the normal 19026 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19027 * command is issued as part of an error recovery action. 19028 * 19029 * Return Code: 0 - Success 19030 * EIO - IO error 19031 * EACCES - Reservation conflict detected 19032 * ENXIO - Not Ready, medium not present 19033 * errno return code from sd_send_scsi_cmd() 19034 * 19035 * Context: Can sleep. 19036 */ 19037 19038 static int 19039 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag) 19040 { 19041 struct scsi_extended_sense sense_buf; 19042 union scsi_cdb cdb; 19043 struct uscsi_cmd ucmd_buf; 19044 int status; 19045 19046 ASSERT(un != NULL); 19047 ASSERT(!mutex_owned(SD_MUTEX(un))); 19048 19049 SD_TRACE(SD_LOG_IO, un, 19050 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 19051 19052 if (ISREMOVABLE(un) && 19053 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 19054 (un->un_f_start_stop_supported != TRUE)) { 19055 return (0); 19056 } 19057 19058 bzero(&cdb, sizeof (cdb)); 19059 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19060 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19061 19062 cdb.scc_cmd = SCMD_START_STOP; 19063 cdb.cdb_opaque[4] = (uchar_t)flag; 19064 19065 ucmd_buf.uscsi_cdb = (char *)&cdb; 19066 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19067 ucmd_buf.uscsi_bufaddr = NULL; 19068 ucmd_buf.uscsi_buflen = 0; 19069 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19070 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19071 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19072 ucmd_buf.uscsi_timeout = 200; 19073 19074 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19075 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19076 19077 switch (status) { 19078 case 0: 19079 break; /* Success! */ 19080 case EIO: 19081 switch (ucmd_buf.uscsi_status) { 19082 case STATUS_RESERVATION_CONFLICT: 19083 status = EACCES; 19084 break; 19085 case STATUS_CHECK: 19086 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 19087 switch (sense_buf.es_key) { 19088 case KEY_ILLEGAL_REQUEST: 19089 status = ENOTSUP; 19090 break; 19091 case KEY_NOT_READY: 19092 if (sense_buf.es_add_code == 0x3A) { 19093 status = ENXIO; 19094 } 19095 break; 19096 default: 19097 break; 19098 } 19099 } 19100 break; 19101 default: 19102 break; 19103 } 19104 break; 19105 default: 19106 break; 19107 } 19108 19109 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 19110 19111 return (status); 19112 } 19113 19114 19115 /* 19116 * Function: sd_start_stop_unit_callback 19117 * 19118 * Description: timeout(9F) callback to begin recovery process for a 19119 * device that has spun down. 19120 * 19121 * Arguments: arg - pointer to associated softstate struct. 19122 * 19123 * Context: Executes in a timeout(9F) thread context 19124 */ 19125 19126 static void 19127 sd_start_stop_unit_callback(void *arg) 19128 { 19129 struct sd_lun *un = arg; 19130 ASSERT(un != NULL); 19131 ASSERT(!mutex_owned(SD_MUTEX(un))); 19132 19133 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 19134 19135 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 19136 } 19137 19138 19139 /* 19140 * Function: sd_start_stop_unit_task 19141 * 19142 * Description: Recovery procedure when a drive is spun down. 19143 * 19144 * Arguments: arg - pointer to associated softstate struct. 19145 * 19146 * Context: Executes in a taskq() thread context 19147 */ 19148 19149 static void 19150 sd_start_stop_unit_task(void *arg) 19151 { 19152 struct sd_lun *un = arg; 19153 19154 ASSERT(un != NULL); 19155 ASSERT(!mutex_owned(SD_MUTEX(un))); 19156 19157 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 19158 19159 /* 19160 * Some unformatted drives report not ready error, no need to 19161 * restart if format has been initiated. 19162 */ 19163 mutex_enter(SD_MUTEX(un)); 19164 if (un->un_f_format_in_progress == TRUE) { 19165 mutex_exit(SD_MUTEX(un)); 19166 return; 19167 } 19168 mutex_exit(SD_MUTEX(un)); 19169 19170 /* 19171 * When a START STOP command is issued from here, it is part of a 19172 * failure recovery operation and must be issued before any other 19173 * commands, including any pending retries. Thus it must be sent 19174 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 19175 * succeeds or not, we will start I/O after the attempt. 19176 */ 19177 (void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 19178 SD_PATH_DIRECT_PRIORITY); 19179 19180 /* 19181 * The above call blocks until the START_STOP_UNIT command completes. 19182 * Now that it has completed, we must re-try the original IO that 19183 * received the NOT READY condition in the first place. There are 19184 * three possible conditions here: 19185 * 19186 * (1) The original IO is on un_retry_bp. 19187 * (2) The original IO is on the regular wait queue, and un_retry_bp 19188 * is NULL. 19189 * (3) The original IO is on the regular wait queue, and un_retry_bp 19190 * points to some other, unrelated bp. 19191 * 19192 * For each case, we must call sd_start_cmds() with un_retry_bp 19193 * as the argument. If un_retry_bp is NULL, this will initiate 19194 * processing of the regular wait queue. If un_retry_bp is not NULL, 19195 * then this will process the bp on un_retry_bp. That may or may not 19196 * be the original IO, but that does not matter: the important thing 19197 * is to keep the IO processing going at this point. 19198 * 19199 * Note: This is a very specific error recovery sequence associated 19200 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 19201 * serialize the I/O with completion of the spin-up. 19202 */ 19203 mutex_enter(SD_MUTEX(un)); 19204 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19205 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 19206 un, un->un_retry_bp); 19207 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 19208 sd_start_cmds(un, un->un_retry_bp); 19209 mutex_exit(SD_MUTEX(un)); 19210 19211 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 19212 } 19213 19214 19215 /* 19216 * Function: sd_send_scsi_INQUIRY 19217 * 19218 * Description: Issue the scsi INQUIRY command. 19219 * 19220 * Arguments: un 19221 * bufaddr 19222 * buflen 19223 * evpd 19224 * page_code 19225 * page_length 19226 * 19227 * Return Code: 0 - Success 19228 * errno return code from sd_send_scsi_cmd() 19229 * 19230 * Context: Can sleep. Does not return until command is completed. 19231 */ 19232 19233 static int 19234 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen, 19235 uchar_t evpd, uchar_t page_code, size_t *residp) 19236 { 19237 union scsi_cdb cdb; 19238 struct uscsi_cmd ucmd_buf; 19239 int status; 19240 19241 ASSERT(un != NULL); 19242 ASSERT(!mutex_owned(SD_MUTEX(un))); 19243 ASSERT(bufaddr != NULL); 19244 19245 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 19246 19247 bzero(&cdb, sizeof (cdb)); 19248 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19249 bzero(bufaddr, buflen); 19250 19251 cdb.scc_cmd = SCMD_INQUIRY; 19252 cdb.cdb_opaque[1] = evpd; 19253 cdb.cdb_opaque[2] = page_code; 19254 FORMG0COUNT(&cdb, buflen); 19255 19256 ucmd_buf.uscsi_cdb = (char *)&cdb; 19257 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19258 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19259 ucmd_buf.uscsi_buflen = buflen; 19260 ucmd_buf.uscsi_rqbuf = NULL; 19261 ucmd_buf.uscsi_rqlen = 0; 19262 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 19263 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 19264 19265 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19266 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_DIRECT); 19267 19268 if ((status == 0) && (residp != NULL)) { 19269 *residp = ucmd_buf.uscsi_resid; 19270 } 19271 19272 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 19273 19274 return (status); 19275 } 19276 19277 19278 /* 19279 * Function: sd_send_scsi_TEST_UNIT_READY 19280 * 19281 * Description: Issue the scsi TEST UNIT READY command. 19282 * This routine can be told to set the flag USCSI_DIAGNOSE to 19283 * prevent retrying failed commands. Use this when the intent 19284 * is either to check for device readiness, to clear a Unit 19285 * Attention, or to clear any outstanding sense data. 19286 * However under specific conditions the expected behavior 19287 * is for retries to bring a device ready, so use the flag 19288 * with caution. 19289 * 19290 * Arguments: un 19291 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 19292 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 19293 * 0: dont check for media present, do retries on cmd. 19294 * 19295 * Return Code: 0 - Success 19296 * EIO - IO error 19297 * EACCES - Reservation conflict detected 19298 * ENXIO - Not Ready, medium not present 19299 * errno return code from sd_send_scsi_cmd() 19300 * 19301 * Context: Can sleep. Does not return until command is completed. 19302 */ 19303 19304 static int 19305 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag) 19306 { 19307 struct scsi_extended_sense sense_buf; 19308 union scsi_cdb cdb; 19309 struct uscsi_cmd ucmd_buf; 19310 int status; 19311 19312 ASSERT(un != NULL); 19313 ASSERT(!mutex_owned(SD_MUTEX(un))); 19314 19315 SD_TRACE(SD_LOG_IO, un, 19316 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 19317 19318 /* 19319 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 19320 * timeouts when they receive a TUR and the queue is not empty. Check 19321 * the configuration flag set during attach (indicating the drive has 19322 * this firmware bug) and un_ncmds_in_transport before issuing the 19323 * TUR. If there are 19324 * pending commands return success, this is a bit arbitrary but is ok 19325 * for non-removables (i.e. the eliteI disks) and non-clustering 19326 * configurations. 19327 */ 19328 if (un->un_f_cfg_tur_check == TRUE) { 19329 mutex_enter(SD_MUTEX(un)); 19330 if (un->un_ncmds_in_transport != 0) { 19331 mutex_exit(SD_MUTEX(un)); 19332 return (0); 19333 } 19334 mutex_exit(SD_MUTEX(un)); 19335 } 19336 19337 bzero(&cdb, sizeof (cdb)); 19338 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19339 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19340 19341 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 19342 19343 ucmd_buf.uscsi_cdb = (char *)&cdb; 19344 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19345 ucmd_buf.uscsi_bufaddr = NULL; 19346 ucmd_buf.uscsi_buflen = 0; 19347 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19348 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19349 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19350 19351 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 19352 if ((flag & SD_DONT_RETRY_TUR) != 0) { 19353 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 19354 } 19355 ucmd_buf.uscsi_timeout = 60; 19356 19357 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19358 UIO_SYSSPACE, UIO_SYSSPACE, 19359 ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : SD_PATH_STANDARD)); 19360 19361 switch (status) { 19362 case 0: 19363 break; /* Success! */ 19364 case EIO: 19365 switch (ucmd_buf.uscsi_status) { 19366 case STATUS_RESERVATION_CONFLICT: 19367 status = EACCES; 19368 break; 19369 case STATUS_CHECK: 19370 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 19371 break; 19372 } 19373 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19374 (sense_buf.es_key == KEY_NOT_READY) && 19375 (sense_buf.es_add_code == 0x3A)) { 19376 status = ENXIO; 19377 } 19378 break; 19379 default: 19380 break; 19381 } 19382 break; 19383 default: 19384 break; 19385 } 19386 19387 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 19388 19389 return (status); 19390 } 19391 19392 19393 /* 19394 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 19395 * 19396 * Description: Issue the scsi PERSISTENT RESERVE IN command. 19397 * 19398 * Arguments: un 19399 * 19400 * Return Code: 0 - Success 19401 * EACCES 19402 * ENOTSUP 19403 * errno return code from sd_send_scsi_cmd() 19404 * 19405 * Context: Can sleep. Does not return until command is completed. 19406 */ 19407 19408 static int 19409 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t usr_cmd, 19410 uint16_t data_len, uchar_t *data_bufp) 19411 { 19412 struct scsi_extended_sense sense_buf; 19413 union scsi_cdb cdb; 19414 struct uscsi_cmd ucmd_buf; 19415 int status; 19416 int no_caller_buf = FALSE; 19417 19418 ASSERT(un != NULL); 19419 ASSERT(!mutex_owned(SD_MUTEX(un))); 19420 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 19421 19422 SD_TRACE(SD_LOG_IO, un, 19423 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 19424 19425 bzero(&cdb, sizeof (cdb)); 19426 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19427 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19428 if (data_bufp == NULL) { 19429 /* Allocate a default buf if the caller did not give one */ 19430 ASSERT(data_len == 0); 19431 data_len = MHIOC_RESV_KEY_SIZE; 19432 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 19433 no_caller_buf = TRUE; 19434 } 19435 19436 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 19437 cdb.cdb_opaque[1] = usr_cmd; 19438 FORMG1COUNT(&cdb, data_len); 19439 19440 ucmd_buf.uscsi_cdb = (char *)&cdb; 19441 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19442 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 19443 ucmd_buf.uscsi_buflen = data_len; 19444 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19445 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19446 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19447 ucmd_buf.uscsi_timeout = 60; 19448 19449 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19450 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 19451 19452 switch (status) { 19453 case 0: 19454 break; /* Success! */ 19455 case EIO: 19456 switch (ucmd_buf.uscsi_status) { 19457 case STATUS_RESERVATION_CONFLICT: 19458 status = EACCES; 19459 break; 19460 case STATUS_CHECK: 19461 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19462 (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) { 19463 status = ENOTSUP; 19464 } 19465 break; 19466 default: 19467 break; 19468 } 19469 break; 19470 default: 19471 break; 19472 } 19473 19474 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 19475 19476 if (no_caller_buf == TRUE) { 19477 kmem_free(data_bufp, data_len); 19478 } 19479 19480 return (status); 19481 } 19482 19483 19484 /* 19485 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 19486 * 19487 * Description: This routine is the driver entry point for handling CD-ROM 19488 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 19489 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 19490 * device. 19491 * 19492 * Arguments: un - Pointer to soft state struct for the target. 19493 * usr_cmd SCSI-3 reservation facility command (one of 19494 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 19495 * SD_SCSI3_PREEMPTANDABORT) 19496 * usr_bufp - user provided pointer register, reserve descriptor or 19497 * preempt and abort structure (mhioc_register_t, 19498 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 19499 * 19500 * Return Code: 0 - Success 19501 * EACCES 19502 * ENOTSUP 19503 * errno return code from sd_send_scsi_cmd() 19504 * 19505 * Context: Can sleep. Does not return until command is completed. 19506 */ 19507 19508 static int 19509 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd, 19510 uchar_t *usr_bufp) 19511 { 19512 struct scsi_extended_sense sense_buf; 19513 union scsi_cdb cdb; 19514 struct uscsi_cmd ucmd_buf; 19515 int status; 19516 uchar_t data_len = sizeof (sd_prout_t); 19517 sd_prout_t *prp; 19518 19519 ASSERT(un != NULL); 19520 ASSERT(!mutex_owned(SD_MUTEX(un))); 19521 ASSERT(data_len == 24); /* required by scsi spec */ 19522 19523 SD_TRACE(SD_LOG_IO, un, 19524 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 19525 19526 if (usr_bufp == NULL) { 19527 return (EINVAL); 19528 } 19529 19530 bzero(&cdb, sizeof (cdb)); 19531 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19532 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19533 prp = kmem_zalloc(data_len, KM_SLEEP); 19534 19535 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 19536 cdb.cdb_opaque[1] = usr_cmd; 19537 FORMG1COUNT(&cdb, data_len); 19538 19539 ucmd_buf.uscsi_cdb = (char *)&cdb; 19540 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19541 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 19542 ucmd_buf.uscsi_buflen = data_len; 19543 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19544 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19545 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 19546 ucmd_buf.uscsi_timeout = 60; 19547 19548 switch (usr_cmd) { 19549 case SD_SCSI3_REGISTER: { 19550 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 19551 19552 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 19553 bcopy(ptr->newkey.key, prp->service_key, 19554 MHIOC_RESV_KEY_SIZE); 19555 prp->aptpl = ptr->aptpl; 19556 break; 19557 } 19558 case SD_SCSI3_RESERVE: 19559 case SD_SCSI3_RELEASE: { 19560 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 19561 19562 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 19563 prp->scope_address = BE_32(ptr->scope_specific_addr); 19564 cdb.cdb_opaque[2] = ptr->type; 19565 break; 19566 } 19567 case SD_SCSI3_PREEMPTANDABORT: { 19568 mhioc_preemptandabort_t *ptr = 19569 (mhioc_preemptandabort_t *)usr_bufp; 19570 19571 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 19572 bcopy(ptr->victim_key.key, prp->service_key, 19573 MHIOC_RESV_KEY_SIZE); 19574 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 19575 cdb.cdb_opaque[2] = ptr->resvdesc.type; 19576 ucmd_buf.uscsi_flags |= USCSI_HEAD; 19577 break; 19578 } 19579 case SD_SCSI3_REGISTERANDIGNOREKEY: 19580 { 19581 mhioc_registerandignorekey_t *ptr; 19582 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 19583 bcopy(ptr->newkey.key, 19584 prp->service_key, MHIOC_RESV_KEY_SIZE); 19585 prp->aptpl = ptr->aptpl; 19586 break; 19587 } 19588 default: 19589 ASSERT(FALSE); 19590 break; 19591 } 19592 19593 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19594 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 19595 19596 switch (status) { 19597 case 0: 19598 break; /* Success! */ 19599 case EIO: 19600 switch (ucmd_buf.uscsi_status) { 19601 case STATUS_RESERVATION_CONFLICT: 19602 status = EACCES; 19603 break; 19604 case STATUS_CHECK: 19605 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19606 (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) { 19607 status = ENOTSUP; 19608 } 19609 break; 19610 default: 19611 break; 19612 } 19613 break; 19614 default: 19615 break; 19616 } 19617 19618 kmem_free(prp, data_len); 19619 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 19620 return (status); 19621 } 19622 19623 19624 /* 19625 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 19626 * 19627 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 19628 * 19629 * Arguments: un - pointer to the target's soft state struct 19630 * 19631 * Return Code: 0 - success 19632 * errno-type error code 19633 * 19634 * Context: kernel thread context only. 19635 */ 19636 19637 static int 19638 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un) 19639 { 19640 struct scsi_extended_sense sense_buf; 19641 union scsi_cdb cdb; 19642 struct uscsi_cmd ucmd_buf; 19643 int status; 19644 19645 ASSERT(un != NULL); 19646 ASSERT(!mutex_owned(SD_MUTEX(un))); 19647 19648 SD_TRACE(SD_LOG_IO, un, 19649 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 19650 19651 bzero(&cdb, sizeof (cdb)); 19652 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19653 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19654 19655 cdb.scc_cmd = SCMD_SYNCHRONIZE_CACHE; 19656 19657 ucmd_buf.uscsi_cdb = (char *)&cdb; 19658 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19659 ucmd_buf.uscsi_bufaddr = NULL; 19660 ucmd_buf.uscsi_buflen = 0; 19661 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19662 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19663 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19664 ucmd_buf.uscsi_timeout = 240; 19665 19666 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19667 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_DIRECT); 19668 19669 switch (status) { 19670 case 0: 19671 break; /* Success! */ 19672 case EIO: 19673 switch (ucmd_buf.uscsi_status) { 19674 case STATUS_RESERVATION_CONFLICT: 19675 /* Ignore reservation conflict */ 19676 status = 0; 19677 goto done; 19678 19679 case STATUS_CHECK: 19680 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19681 (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) { 19682 /* Ignore Illegal Request error */ 19683 status = 0; 19684 goto done; 19685 } 19686 break; 19687 default: 19688 break; 19689 } 19690 /* FALLTHRU */ 19691 default: 19692 /* Ignore error if the media is not present. */ 19693 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 19694 status = 0; 19695 goto done; 19696 } 19697 /* If we reach this, we had an error */ 19698 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 19699 "SYNCHRONIZE CACHE command failed (%d)\n", status); 19700 break; 19701 } 19702 19703 done: 19704 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: exit\n"); 19705 19706 return (status); 19707 } 19708 19709 19710 /* 19711 * Function: sd_send_scsi_GET_CONFIGURATION 19712 * 19713 * Description: Issues the get configuration command to the device. 19714 * Called from sd_check_for_writable_cd & sd_get_media_info 19715 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 19716 * Arguments: un 19717 * ucmdbuf 19718 * rqbuf 19719 * rqbuflen 19720 * bufaddr 19721 * buflen 19722 * 19723 * Return Code: 0 - Success 19724 * errno return code from sd_send_scsi_cmd() 19725 * 19726 * Context: Can sleep. Does not return until command is completed. 19727 * 19728 */ 19729 19730 static int 19731 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf, 19732 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen) 19733 { 19734 char cdb[CDB_GROUP1]; 19735 int status; 19736 19737 ASSERT(un != NULL); 19738 ASSERT(!mutex_owned(SD_MUTEX(un))); 19739 ASSERT(bufaddr != NULL); 19740 ASSERT(ucmdbuf != NULL); 19741 ASSERT(rqbuf != NULL); 19742 19743 SD_TRACE(SD_LOG_IO, un, 19744 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 19745 19746 bzero(cdb, sizeof (cdb)); 19747 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 19748 bzero(rqbuf, rqbuflen); 19749 bzero(bufaddr, buflen); 19750 19751 /* 19752 * Set up cdb field for the get configuration command. 19753 */ 19754 cdb[0] = SCMD_GET_CONFIGURATION; 19755 cdb[1] = 0x02; /* Requested Type */ 19756 cdb[8] = SD_PROFILE_HEADER_LEN; 19757 ucmdbuf->uscsi_cdb = cdb; 19758 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 19759 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 19760 ucmdbuf->uscsi_buflen = buflen; 19761 ucmdbuf->uscsi_timeout = sd_io_time; 19762 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 19763 ucmdbuf->uscsi_rqlen = rqbuflen; 19764 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 19765 19766 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 19767 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 19768 19769 switch (status) { 19770 case 0: 19771 break; /* Success! */ 19772 case EIO: 19773 switch (ucmdbuf->uscsi_status) { 19774 case STATUS_RESERVATION_CONFLICT: 19775 status = EACCES; 19776 break; 19777 default: 19778 break; 19779 } 19780 break; 19781 default: 19782 break; 19783 } 19784 19785 if (status == 0) { 19786 SD_DUMP_MEMORY(un, SD_LOG_IO, 19787 "sd_send_scsi_GET_CONFIGURATION: data", 19788 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 19789 } 19790 19791 SD_TRACE(SD_LOG_IO, un, 19792 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 19793 19794 return (status); 19795 } 19796 19797 /* 19798 * Function: sd_send_scsi_feature_GET_CONFIGURATION 19799 * 19800 * Description: Issues the get configuration command to the device to 19801 * retrieve a specfic feature. Called from 19802 * sd_check_for_writable_cd & sd_set_mmc_caps. 19803 * Arguments: un 19804 * ucmdbuf 19805 * rqbuf 19806 * rqbuflen 19807 * bufaddr 19808 * buflen 19809 * feature 19810 * 19811 * Return Code: 0 - Success 19812 * errno return code from sd_send_scsi_cmd() 19813 * 19814 * Context: Can sleep. Does not return until command is completed. 19815 * 19816 */ 19817 static int 19818 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 19819 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 19820 uchar_t *bufaddr, uint_t buflen, char feature) 19821 { 19822 char cdb[CDB_GROUP1]; 19823 int status; 19824 19825 ASSERT(un != NULL); 19826 ASSERT(!mutex_owned(SD_MUTEX(un))); 19827 ASSERT(bufaddr != NULL); 19828 ASSERT(ucmdbuf != NULL); 19829 ASSERT(rqbuf != NULL); 19830 19831 SD_TRACE(SD_LOG_IO, un, 19832 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 19833 19834 bzero(cdb, sizeof (cdb)); 19835 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 19836 bzero(rqbuf, rqbuflen); 19837 bzero(bufaddr, buflen); 19838 19839 /* 19840 * Set up cdb field for the get configuration command. 19841 */ 19842 cdb[0] = SCMD_GET_CONFIGURATION; 19843 cdb[1] = 0x02; /* Requested Type */ 19844 cdb[3] = feature; 19845 cdb[8] = buflen; 19846 ucmdbuf->uscsi_cdb = cdb; 19847 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 19848 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 19849 ucmdbuf->uscsi_buflen = buflen; 19850 ucmdbuf->uscsi_timeout = sd_io_time; 19851 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 19852 ucmdbuf->uscsi_rqlen = rqbuflen; 19853 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 19854 19855 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 19856 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 19857 19858 switch (status) { 19859 case 0: 19860 break; /* Success! */ 19861 case EIO: 19862 switch (ucmdbuf->uscsi_status) { 19863 case STATUS_RESERVATION_CONFLICT: 19864 status = EACCES; 19865 break; 19866 default: 19867 break; 19868 } 19869 break; 19870 default: 19871 break; 19872 } 19873 19874 if (status == 0) { 19875 SD_DUMP_MEMORY(un, SD_LOG_IO, 19876 "sd_send_scsi_feature_GET_CONFIGURATION: data", 19877 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 19878 } 19879 19880 SD_TRACE(SD_LOG_IO, un, 19881 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 19882 19883 return (status); 19884 } 19885 19886 19887 /* 19888 * Function: sd_send_scsi_MODE_SENSE 19889 * 19890 * Description: Utility function for issuing a scsi MODE SENSE command. 19891 * Note: This routine uses a consistent implementation for Group0, 19892 * Group1, and Group2 commands across all platforms. ATAPI devices 19893 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 19894 * 19895 * Arguments: un - pointer to the softstate struct for the target. 19896 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 19897 * CDB_GROUP[1|2] (10 byte). 19898 * bufaddr - buffer for page data retrieved from the target. 19899 * buflen - size of page to be retrieved. 19900 * page_code - page code of data to be retrieved from the target. 19901 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19902 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19903 * to use the USCSI "direct" chain and bypass the normal 19904 * command waitq. 19905 * 19906 * Return Code: 0 - Success 19907 * errno return code from sd_send_scsi_cmd() 19908 * 19909 * Context: Can sleep. Does not return until command is completed. 19910 */ 19911 19912 static int 19913 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 19914 size_t buflen, uchar_t page_code, int path_flag) 19915 { 19916 struct scsi_extended_sense sense_buf; 19917 union scsi_cdb cdb; 19918 struct uscsi_cmd ucmd_buf; 19919 int status; 19920 19921 ASSERT(un != NULL); 19922 ASSERT(!mutex_owned(SD_MUTEX(un))); 19923 ASSERT(bufaddr != NULL); 19924 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 19925 (cdbsize == CDB_GROUP2)); 19926 19927 SD_TRACE(SD_LOG_IO, un, 19928 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 19929 19930 bzero(&cdb, sizeof (cdb)); 19931 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19932 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19933 bzero(bufaddr, buflen); 19934 19935 if (cdbsize == CDB_GROUP0) { 19936 cdb.scc_cmd = SCMD_MODE_SENSE; 19937 cdb.cdb_opaque[2] = page_code; 19938 FORMG0COUNT(&cdb, buflen); 19939 } else { 19940 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 19941 cdb.cdb_opaque[2] = page_code; 19942 FORMG1COUNT(&cdb, buflen); 19943 } 19944 19945 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 19946 19947 ucmd_buf.uscsi_cdb = (char *)&cdb; 19948 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 19949 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19950 ucmd_buf.uscsi_buflen = buflen; 19951 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19952 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19953 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19954 ucmd_buf.uscsi_timeout = 60; 19955 19956 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19957 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19958 19959 switch (status) { 19960 case 0: 19961 break; /* Success! */ 19962 case EIO: 19963 switch (ucmd_buf.uscsi_status) { 19964 case STATUS_RESERVATION_CONFLICT: 19965 status = EACCES; 19966 break; 19967 default: 19968 break; 19969 } 19970 break; 19971 default: 19972 break; 19973 } 19974 19975 if (status == 0) { 19976 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 19977 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 19978 } 19979 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 19980 19981 return (status); 19982 } 19983 19984 19985 /* 19986 * Function: sd_send_scsi_MODE_SELECT 19987 * 19988 * Description: Utility function for issuing a scsi MODE SELECT command. 19989 * Note: This routine uses a consistent implementation for Group0, 19990 * Group1, and Group2 commands across all platforms. ATAPI devices 19991 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 19992 * 19993 * Arguments: un - pointer to the softstate struct for the target. 19994 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 19995 * CDB_GROUP[1|2] (10 byte). 19996 * bufaddr - buffer for page data retrieved from the target. 19997 * buflen - size of page to be retrieved. 19998 * save_page - boolean to determin if SP bit should be set. 19999 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20000 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20001 * to use the USCSI "direct" chain and bypass the normal 20002 * command waitq. 20003 * 20004 * Return Code: 0 - Success 20005 * errno return code from sd_send_scsi_cmd() 20006 * 20007 * Context: Can sleep. Does not return until command is completed. 20008 */ 20009 20010 static int 20011 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20012 size_t buflen, uchar_t save_page, int path_flag) 20013 { 20014 struct scsi_extended_sense sense_buf; 20015 union scsi_cdb cdb; 20016 struct uscsi_cmd ucmd_buf; 20017 int status; 20018 20019 ASSERT(un != NULL); 20020 ASSERT(!mutex_owned(SD_MUTEX(un))); 20021 ASSERT(bufaddr != NULL); 20022 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20023 (cdbsize == CDB_GROUP2)); 20024 20025 SD_TRACE(SD_LOG_IO, un, 20026 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 20027 20028 bzero(&cdb, sizeof (cdb)); 20029 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20030 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20031 20032 /* Set the PF bit for many third party drives */ 20033 cdb.cdb_opaque[1] = 0x10; 20034 20035 /* Set the savepage(SP) bit if given */ 20036 if (save_page == SD_SAVE_PAGE) { 20037 cdb.cdb_opaque[1] |= 0x01; 20038 } 20039 20040 if (cdbsize == CDB_GROUP0) { 20041 cdb.scc_cmd = SCMD_MODE_SELECT; 20042 FORMG0COUNT(&cdb, buflen); 20043 } else { 20044 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 20045 FORMG1COUNT(&cdb, buflen); 20046 } 20047 20048 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20049 20050 ucmd_buf.uscsi_cdb = (char *)&cdb; 20051 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20052 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20053 ucmd_buf.uscsi_buflen = buflen; 20054 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20055 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20056 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20057 ucmd_buf.uscsi_timeout = 60; 20058 20059 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20060 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20061 20062 switch (status) { 20063 case 0: 20064 break; /* Success! */ 20065 case EIO: 20066 switch (ucmd_buf.uscsi_status) { 20067 case STATUS_RESERVATION_CONFLICT: 20068 status = EACCES; 20069 break; 20070 default: 20071 break; 20072 } 20073 break; 20074 default: 20075 break; 20076 } 20077 20078 if (status == 0) { 20079 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 20080 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20081 } 20082 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 20083 20084 return (status); 20085 } 20086 20087 20088 /* 20089 * Function: sd_send_scsi_RDWR 20090 * 20091 * Description: Issue a scsi READ or WRITE command with the given parameters. 20092 * 20093 * Arguments: un: Pointer to the sd_lun struct for the target. 20094 * cmd: SCMD_READ or SCMD_WRITE 20095 * bufaddr: Address of caller's buffer to receive the RDWR data 20096 * buflen: Length of caller's buffer receive the RDWR data. 20097 * start_block: Block number for the start of the RDWR operation. 20098 * (Assumes target-native block size.) 20099 * residp: Pointer to variable to receive the redisual of the 20100 * RDWR operation (may be NULL of no residual requested). 20101 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20102 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20103 * to use the USCSI "direct" chain and bypass the normal 20104 * command waitq. 20105 * 20106 * Return Code: 0 - Success 20107 * errno return code from sd_send_scsi_cmd() 20108 * 20109 * Context: Can sleep. Does not return until command is completed. 20110 */ 20111 20112 static int 20113 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 20114 size_t buflen, daddr_t start_block, int path_flag) 20115 { 20116 struct scsi_extended_sense sense_buf; 20117 union scsi_cdb cdb; 20118 struct uscsi_cmd ucmd_buf; 20119 uint32_t block_count; 20120 int status; 20121 int cdbsize; 20122 uchar_t flag; 20123 20124 ASSERT(un != NULL); 20125 ASSERT(!mutex_owned(SD_MUTEX(un))); 20126 ASSERT(bufaddr != NULL); 20127 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 20128 20129 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 20130 20131 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 20132 return (EINVAL); 20133 } 20134 20135 mutex_enter(SD_MUTEX(un)); 20136 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 20137 mutex_exit(SD_MUTEX(un)); 20138 20139 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 20140 20141 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 20142 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 20143 bufaddr, buflen, start_block, block_count); 20144 20145 bzero(&cdb, sizeof (cdb)); 20146 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20147 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20148 20149 /* Compute CDB size to use */ 20150 if (start_block > 0xffffffff) 20151 cdbsize = CDB_GROUP4; 20152 else if ((start_block & 0xFFE00000) || 20153 (un->un_f_cfg_is_atapi == TRUE)) 20154 cdbsize = CDB_GROUP1; 20155 else 20156 cdbsize = CDB_GROUP0; 20157 20158 switch (cdbsize) { 20159 case CDB_GROUP0: /* 6-byte CDBs */ 20160 cdb.scc_cmd = cmd; 20161 FORMG0ADDR(&cdb, start_block); 20162 FORMG0COUNT(&cdb, block_count); 20163 break; 20164 case CDB_GROUP1: /* 10-byte CDBs */ 20165 cdb.scc_cmd = cmd | SCMD_GROUP1; 20166 FORMG1ADDR(&cdb, start_block); 20167 FORMG1COUNT(&cdb, block_count); 20168 break; 20169 case CDB_GROUP4: /* 16-byte CDBs */ 20170 cdb.scc_cmd = cmd | SCMD_GROUP4; 20171 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 20172 FORMG4COUNT(&cdb, block_count); 20173 break; 20174 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 20175 default: 20176 /* All others reserved */ 20177 return (EINVAL); 20178 } 20179 20180 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 20181 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20182 20183 ucmd_buf.uscsi_cdb = (char *)&cdb; 20184 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20185 ucmd_buf.uscsi_bufaddr = bufaddr; 20186 ucmd_buf.uscsi_buflen = buflen; 20187 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20188 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20189 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 20190 ucmd_buf.uscsi_timeout = 60; 20191 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20192 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20193 switch (status) { 20194 case 0: 20195 break; /* Success! */ 20196 case EIO: 20197 switch (ucmd_buf.uscsi_status) { 20198 case STATUS_RESERVATION_CONFLICT: 20199 status = EACCES; 20200 break; 20201 default: 20202 break; 20203 } 20204 break; 20205 default: 20206 break; 20207 } 20208 20209 if (status == 0) { 20210 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 20211 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20212 } 20213 20214 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 20215 20216 return (status); 20217 } 20218 20219 20220 /* 20221 * Function: sd_send_scsi_LOG_SENSE 20222 * 20223 * Description: Issue a scsi LOG_SENSE command with the given parameters. 20224 * 20225 * Arguments: un: Pointer to the sd_lun struct for the target. 20226 * 20227 * Return Code: 0 - Success 20228 * errno return code from sd_send_scsi_cmd() 20229 * 20230 * Context: Can sleep. Does not return until command is completed. 20231 */ 20232 20233 static int 20234 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen, 20235 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 20236 int path_flag) 20237 20238 { 20239 struct scsi_extended_sense sense_buf; 20240 union scsi_cdb cdb; 20241 struct uscsi_cmd ucmd_buf; 20242 int status; 20243 20244 ASSERT(un != NULL); 20245 ASSERT(!mutex_owned(SD_MUTEX(un))); 20246 20247 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 20248 20249 bzero(&cdb, sizeof (cdb)); 20250 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20251 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20252 20253 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 20254 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 20255 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 20256 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 20257 FORMG1COUNT(&cdb, buflen); 20258 20259 ucmd_buf.uscsi_cdb = (char *)&cdb; 20260 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20261 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20262 ucmd_buf.uscsi_buflen = buflen; 20263 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20264 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20265 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20266 ucmd_buf.uscsi_timeout = 60; 20267 20268 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20269 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20270 20271 switch (status) { 20272 case 0: 20273 break; 20274 case EIO: 20275 switch (ucmd_buf.uscsi_status) { 20276 case STATUS_RESERVATION_CONFLICT: 20277 status = EACCES; 20278 break; 20279 case STATUS_CHECK: 20280 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20281 (sense_buf.es_key == KEY_ILLEGAL_REQUEST) && 20282 (sense_buf.es_add_code == 0x24)) { 20283 /* 20284 * ASC 0x24: INVALID FIELD IN CDB 20285 */ 20286 switch (page_code) { 20287 case START_STOP_CYCLE_PAGE: 20288 /* 20289 * The start stop cycle counter is 20290 * implemented as page 0x31 in earlier 20291 * generation disks. In new generation 20292 * disks the start stop cycle counter is 20293 * implemented as page 0xE. To properly 20294 * handle this case if an attempt for 20295 * log page 0xE is made and fails we 20296 * will try again using page 0x31. 20297 * 20298 * Network storage BU committed to 20299 * maintain the page 0x31 for this 20300 * purpose and will not have any other 20301 * page implemented with page code 0x31 20302 * until all disks transition to the 20303 * standard page. 20304 */ 20305 mutex_enter(SD_MUTEX(un)); 20306 un->un_start_stop_cycle_page = 20307 START_STOP_CYCLE_VU_PAGE; 20308 cdb.cdb_opaque[2] = 20309 (char)(page_control << 6) | 20310 un->un_start_stop_cycle_page; 20311 mutex_exit(SD_MUTEX(un)); 20312 status = sd_send_scsi_cmd( 20313 SD_GET_DEV(un), &ucmd_buf, 20314 UIO_SYSSPACE, UIO_SYSSPACE, 20315 UIO_SYSSPACE, path_flag); 20316 20317 break; 20318 case TEMPERATURE_PAGE: 20319 status = ENOTTY; 20320 break; 20321 default: 20322 break; 20323 } 20324 } 20325 break; 20326 default: 20327 break; 20328 } 20329 break; 20330 default: 20331 break; 20332 } 20333 20334 if (status == 0) { 20335 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 20336 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20337 } 20338 20339 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 20340 20341 return (status); 20342 } 20343 20344 20345 /* 20346 * Function: sdioctl 20347 * 20348 * Description: Driver's ioctl(9e) entry point function. 20349 * 20350 * Arguments: dev - device number 20351 * cmd - ioctl operation to be performed 20352 * arg - user argument, contains data to be set or reference 20353 * parameter for get 20354 * flag - bit flag, indicating open settings, 32/64 bit type 20355 * cred_p - user credential pointer 20356 * rval_p - calling process return value (OPT) 20357 * 20358 * Return Code: EINVAL 20359 * ENOTTY 20360 * ENXIO 20361 * EIO 20362 * EFAULT 20363 * ENOTSUP 20364 * EPERM 20365 * 20366 * Context: Called from the device switch at normal priority. 20367 */ 20368 20369 static int 20370 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 20371 { 20372 struct sd_lun *un = NULL; 20373 int geom_validated = FALSE; 20374 int err = 0; 20375 int i = 0; 20376 cred_t *cr; 20377 20378 /* 20379 * All device accesses go thru sdstrategy where we check on suspend 20380 * status 20381 */ 20382 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 20383 return (ENXIO); 20384 } 20385 20386 ASSERT(!mutex_owned(SD_MUTEX(un))); 20387 20388 /* 20389 * Moved this wait from sd_uscsi_strategy to here for 20390 * reasons of deadlock prevention. Internal driver commands, 20391 * specifically those to change a devices power level, result 20392 * in a call to sd_uscsi_strategy. 20393 */ 20394 mutex_enter(SD_MUTEX(un)); 20395 while ((un->un_state == SD_STATE_SUSPENDED) || 20396 (un->un_state == SD_STATE_PM_CHANGING)) { 20397 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 20398 } 20399 /* 20400 * Twiddling the counter here protects commands from now 20401 * through to the top of sd_uscsi_strategy. Without the 20402 * counter inc. a power down, for example, could get in 20403 * after the above check for state is made and before 20404 * execution gets to the top of sd_uscsi_strategy. 20405 * That would cause problems. 20406 */ 20407 un->un_ncmds_in_driver++; 20408 20409 if ((un->un_f_geometry_is_valid == FALSE) && 20410 (flag & (FNDELAY | FNONBLOCK))) { 20411 switch (cmd) { 20412 case CDROMPAUSE: 20413 case CDROMRESUME: 20414 case CDROMPLAYMSF: 20415 case CDROMPLAYTRKIND: 20416 case CDROMREADTOCHDR: 20417 case CDROMREADTOCENTRY: 20418 case CDROMSTOP: 20419 case CDROMSTART: 20420 case CDROMVOLCTRL: 20421 case CDROMSUBCHNL: 20422 case CDROMREADMODE2: 20423 case CDROMREADMODE1: 20424 case CDROMREADOFFSET: 20425 case CDROMSBLKMODE: 20426 case CDROMGBLKMODE: 20427 case CDROMGDRVSPEED: 20428 case CDROMSDRVSPEED: 20429 case CDROMCDDA: 20430 case CDROMCDXA: 20431 case CDROMSUBCODE: 20432 if (!ISCD(un)) { 20433 un->un_ncmds_in_driver--; 20434 ASSERT(un->un_ncmds_in_driver >= 0); 20435 mutex_exit(SD_MUTEX(un)); 20436 return (ENOTTY); 20437 } 20438 break; 20439 case FDEJECT: 20440 case DKIOCEJECT: 20441 case CDROMEJECT: 20442 if (!ISREMOVABLE(un)) { 20443 un->un_ncmds_in_driver--; 20444 ASSERT(un->un_ncmds_in_driver >= 0); 20445 mutex_exit(SD_MUTEX(un)); 20446 return (ENOTTY); 20447 } 20448 break; 20449 case DKIOCSVTOC: 20450 case DKIOCSETEFI: 20451 case DKIOCSMBOOT: 20452 mutex_exit(SD_MUTEX(un)); 20453 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 20454 if (err != 0) { 20455 mutex_enter(SD_MUTEX(un)); 20456 un->un_ncmds_in_driver--; 20457 ASSERT(un->un_ncmds_in_driver >= 0); 20458 mutex_exit(SD_MUTEX(un)); 20459 return (EIO); 20460 } 20461 mutex_enter(SD_MUTEX(un)); 20462 /* FALLTHROUGH */ 20463 case DKIOCREMOVABLE: 20464 case DKIOCINFO: 20465 case DKIOCGMEDIAINFO: 20466 case MHIOCENFAILFAST: 20467 case MHIOCSTATUS: 20468 case MHIOCTKOWN: 20469 case MHIOCRELEASE: 20470 case MHIOCGRP_INKEYS: 20471 case MHIOCGRP_INRESV: 20472 case MHIOCGRP_REGISTER: 20473 case MHIOCGRP_RESERVE: 20474 case MHIOCGRP_PREEMPTANDABORT: 20475 case MHIOCGRP_REGISTERANDIGNOREKEY: 20476 case CDROMCLOSETRAY: 20477 case USCSICMD: 20478 goto skip_ready_valid; 20479 default: 20480 break; 20481 } 20482 20483 mutex_exit(SD_MUTEX(un)); 20484 err = sd_ready_and_valid(un); 20485 mutex_enter(SD_MUTEX(un)); 20486 if (err == SD_READY_NOT_VALID) { 20487 switch (cmd) { 20488 case DKIOCGAPART: 20489 case DKIOCGGEOM: 20490 case DKIOCSGEOM: 20491 case DKIOCGVTOC: 20492 case DKIOCSVTOC: 20493 case DKIOCSAPART: 20494 case DKIOCG_PHYGEOM: 20495 case DKIOCG_VIRTGEOM: 20496 err = ENOTSUP; 20497 un->un_ncmds_in_driver--; 20498 ASSERT(un->un_ncmds_in_driver >= 0); 20499 mutex_exit(SD_MUTEX(un)); 20500 return (err); 20501 } 20502 } 20503 if (err != SD_READY_VALID) { 20504 switch (cmd) { 20505 case DKIOCSTATE: 20506 case CDROMGDRVSPEED: 20507 case CDROMSDRVSPEED: 20508 case FDEJECT: /* for eject command */ 20509 case DKIOCEJECT: 20510 case CDROMEJECT: 20511 case DKIOCGETEFI: 20512 case DKIOCSGEOM: 20513 case DKIOCREMOVABLE: 20514 case DKIOCSAPART: 20515 case DKIOCSETEFI: 20516 break; 20517 default: 20518 if (ISREMOVABLE(un)) { 20519 err = ENXIO; 20520 } else { 20521 /* Do not map EACCES to EIO */ 20522 if (err != EACCES) 20523 err = EIO; 20524 } 20525 un->un_ncmds_in_driver--; 20526 ASSERT(un->un_ncmds_in_driver >= 0); 20527 mutex_exit(SD_MUTEX(un)); 20528 return (err); 20529 } 20530 } 20531 geom_validated = TRUE; 20532 } 20533 if ((un->un_f_geometry_is_valid == TRUE) && 20534 (un->un_solaris_size > 0)) { 20535 /* 20536 * the "geometry_is_valid" flag could be true if we 20537 * have an fdisk table but no Solaris partition 20538 */ 20539 if (un->un_vtoc.v_sanity != VTOC_SANE) { 20540 /* it is EFI, so return ENOTSUP for these */ 20541 switch (cmd) { 20542 case DKIOCGAPART: 20543 case DKIOCGGEOM: 20544 case DKIOCGVTOC: 20545 case DKIOCSVTOC: 20546 case DKIOCSAPART: 20547 err = ENOTSUP; 20548 un->un_ncmds_in_driver--; 20549 ASSERT(un->un_ncmds_in_driver >= 0); 20550 mutex_exit(SD_MUTEX(un)); 20551 return (err); 20552 } 20553 } 20554 } 20555 20556 skip_ready_valid: 20557 mutex_exit(SD_MUTEX(un)); 20558 20559 switch (cmd) { 20560 case DKIOCINFO: 20561 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 20562 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 20563 break; 20564 20565 case DKIOCGMEDIAINFO: 20566 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 20567 err = sd_get_media_info(dev, (caddr_t)arg, flag); 20568 break; 20569 20570 case DKIOCGGEOM: 20571 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGGEOM\n"); 20572 err = sd_dkio_get_geometry(dev, (caddr_t)arg, flag, 20573 geom_validated); 20574 break; 20575 20576 case DKIOCSGEOM: 20577 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSGEOM\n"); 20578 err = sd_dkio_set_geometry(dev, (caddr_t)arg, flag); 20579 break; 20580 20581 case DKIOCGAPART: 20582 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGAPART\n"); 20583 err = sd_dkio_get_partition(dev, (caddr_t)arg, flag, 20584 geom_validated); 20585 break; 20586 20587 case DKIOCSAPART: 20588 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSAPART\n"); 20589 err = sd_dkio_set_partition(dev, (caddr_t)arg, flag); 20590 break; 20591 20592 case DKIOCGVTOC: 20593 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGVTOC\n"); 20594 err = sd_dkio_get_vtoc(dev, (caddr_t)arg, flag, 20595 geom_validated); 20596 break; 20597 20598 case DKIOCGETEFI: 20599 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGETEFI\n"); 20600 err = sd_dkio_get_efi(dev, (caddr_t)arg, flag); 20601 break; 20602 20603 case DKIOCPARTITION: 20604 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTITION\n"); 20605 err = sd_dkio_partition(dev, (caddr_t)arg, flag); 20606 break; 20607 20608 case DKIOCSVTOC: 20609 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSVTOC\n"); 20610 err = sd_dkio_set_vtoc(dev, (caddr_t)arg, flag); 20611 break; 20612 20613 case DKIOCSETEFI: 20614 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSETEFI\n"); 20615 err = sd_dkio_set_efi(dev, (caddr_t)arg, flag); 20616 break; 20617 20618 case DKIOCGMBOOT: 20619 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMBOOT\n"); 20620 err = sd_dkio_get_mboot(dev, (caddr_t)arg, flag); 20621 break; 20622 20623 case DKIOCSMBOOT: 20624 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSMBOOT\n"); 20625 err = sd_dkio_set_mboot(dev, (caddr_t)arg, flag); 20626 break; 20627 20628 case DKIOCLOCK: 20629 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 20630 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 20631 SD_PATH_STANDARD); 20632 break; 20633 20634 case DKIOCUNLOCK: 20635 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 20636 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 20637 SD_PATH_STANDARD); 20638 break; 20639 20640 case DKIOCSTATE: { 20641 enum dkio_state state; 20642 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 20643 20644 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 20645 err = EFAULT; 20646 } else { 20647 err = sd_check_media(dev, state); 20648 if (err == 0) { 20649 if (ddi_copyout(&un->un_mediastate, (void *)arg, 20650 sizeof (int), flag) != 0) 20651 err = EFAULT; 20652 } 20653 } 20654 break; 20655 } 20656 20657 case DKIOCREMOVABLE: 20658 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 20659 if (ISREMOVABLE(un)) { 20660 i = 1; 20661 } else { 20662 i = 0; 20663 } 20664 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 20665 err = EFAULT; 20666 } else { 20667 err = 0; 20668 } 20669 break; 20670 20671 case DKIOCGTEMPERATURE: 20672 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 20673 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 20674 break; 20675 20676 case MHIOCENFAILFAST: 20677 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 20678 if ((err = drv_priv(cred_p)) == 0) { 20679 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 20680 } 20681 break; 20682 20683 case MHIOCTKOWN: 20684 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 20685 if ((err = drv_priv(cred_p)) == 0) { 20686 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 20687 } 20688 break; 20689 20690 case MHIOCRELEASE: 20691 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 20692 if ((err = drv_priv(cred_p)) == 0) { 20693 err = sd_mhdioc_release(dev); 20694 } 20695 break; 20696 20697 case MHIOCSTATUS: 20698 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 20699 if ((err = drv_priv(cred_p)) == 0) { 20700 switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) { 20701 case 0: 20702 err = 0; 20703 break; 20704 case EACCES: 20705 *rval_p = 1; 20706 err = 0; 20707 break; 20708 default: 20709 err = EIO; 20710 break; 20711 } 20712 } 20713 break; 20714 20715 case MHIOCQRESERVE: 20716 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 20717 if ((err = drv_priv(cred_p)) == 0) { 20718 err = sd_reserve_release(dev, SD_RESERVE); 20719 } 20720 break; 20721 20722 case MHIOCREREGISTERDEVID: 20723 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 20724 if (drv_priv(cred_p) == EPERM) { 20725 err = EPERM; 20726 } else if (ISREMOVABLE(un) || ISCD(un)) { 20727 err = ENOTTY; 20728 } else { 20729 err = sd_mhdioc_register_devid(dev); 20730 } 20731 break; 20732 20733 case MHIOCGRP_INKEYS: 20734 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 20735 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 20736 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20737 err = ENOTSUP; 20738 } else { 20739 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 20740 flag); 20741 } 20742 } 20743 break; 20744 20745 case MHIOCGRP_INRESV: 20746 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 20747 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 20748 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20749 err = ENOTSUP; 20750 } else { 20751 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 20752 } 20753 } 20754 break; 20755 20756 case MHIOCGRP_REGISTER: 20757 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 20758 if ((err = drv_priv(cred_p)) != EPERM) { 20759 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20760 err = ENOTSUP; 20761 } else if (arg != NULL) { 20762 mhioc_register_t reg; 20763 if (ddi_copyin((void *)arg, ®, 20764 sizeof (mhioc_register_t), flag) != 0) { 20765 err = EFAULT; 20766 } else { 20767 err = 20768 sd_send_scsi_PERSISTENT_RESERVE_OUT( 20769 un, SD_SCSI3_REGISTER, 20770 (uchar_t *)®); 20771 } 20772 } 20773 } 20774 break; 20775 20776 case MHIOCGRP_RESERVE: 20777 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 20778 if ((err = drv_priv(cred_p)) != EPERM) { 20779 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20780 err = ENOTSUP; 20781 } else if (arg != NULL) { 20782 mhioc_resv_desc_t resv_desc; 20783 if (ddi_copyin((void *)arg, &resv_desc, 20784 sizeof (mhioc_resv_desc_t), flag) != 0) { 20785 err = EFAULT; 20786 } else { 20787 err = 20788 sd_send_scsi_PERSISTENT_RESERVE_OUT( 20789 un, SD_SCSI3_RESERVE, 20790 (uchar_t *)&resv_desc); 20791 } 20792 } 20793 } 20794 break; 20795 20796 case MHIOCGRP_PREEMPTANDABORT: 20797 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 20798 if ((err = drv_priv(cred_p)) != EPERM) { 20799 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20800 err = ENOTSUP; 20801 } else if (arg != NULL) { 20802 mhioc_preemptandabort_t preempt_abort; 20803 if (ddi_copyin((void *)arg, &preempt_abort, 20804 sizeof (mhioc_preemptandabort_t), 20805 flag) != 0) { 20806 err = EFAULT; 20807 } else { 20808 err = 20809 sd_send_scsi_PERSISTENT_RESERVE_OUT( 20810 un, SD_SCSI3_PREEMPTANDABORT, 20811 (uchar_t *)&preempt_abort); 20812 } 20813 } 20814 } 20815 break; 20816 20817 case MHIOCGRP_REGISTERANDIGNOREKEY: 20818 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 20819 if ((err = drv_priv(cred_p)) != EPERM) { 20820 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20821 err = ENOTSUP; 20822 } else if (arg != NULL) { 20823 mhioc_registerandignorekey_t r_and_i; 20824 if (ddi_copyin((void *)arg, (void *)&r_and_i, 20825 sizeof (mhioc_registerandignorekey_t), 20826 flag) != 0) { 20827 err = EFAULT; 20828 } else { 20829 err = 20830 sd_send_scsi_PERSISTENT_RESERVE_OUT( 20831 un, SD_SCSI3_REGISTERANDIGNOREKEY, 20832 (uchar_t *)&r_and_i); 20833 } 20834 } 20835 } 20836 break; 20837 20838 case USCSICMD: 20839 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 20840 cr = ddi_get_cred(); 20841 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 20842 err = EPERM; 20843 } else { 20844 err = sd_uscsi_ioctl(dev, (caddr_t)arg, flag); 20845 } 20846 break; 20847 20848 case CDROMPAUSE: 20849 case CDROMRESUME: 20850 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 20851 if (!ISCD(un)) { 20852 err = ENOTTY; 20853 } else { 20854 err = sr_pause_resume(dev, cmd); 20855 } 20856 break; 20857 20858 case CDROMPLAYMSF: 20859 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 20860 if (!ISCD(un)) { 20861 err = ENOTTY; 20862 } else { 20863 err = sr_play_msf(dev, (caddr_t)arg, flag); 20864 } 20865 break; 20866 20867 case CDROMPLAYTRKIND: 20868 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 20869 #if defined(__i386) || defined(__amd64) 20870 /* 20871 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 20872 */ 20873 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 20874 #else 20875 if (!ISCD(un)) { 20876 #endif 20877 err = ENOTTY; 20878 } else { 20879 err = sr_play_trkind(dev, (caddr_t)arg, flag); 20880 } 20881 break; 20882 20883 case CDROMREADTOCHDR: 20884 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 20885 if (!ISCD(un)) { 20886 err = ENOTTY; 20887 } else { 20888 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 20889 } 20890 break; 20891 20892 case CDROMREADTOCENTRY: 20893 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 20894 if (!ISCD(un)) { 20895 err = ENOTTY; 20896 } else { 20897 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 20898 } 20899 break; 20900 20901 case CDROMSTOP: 20902 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 20903 if (!ISCD(un)) { 20904 err = ENOTTY; 20905 } else { 20906 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP, 20907 SD_PATH_STANDARD); 20908 } 20909 break; 20910 20911 case CDROMSTART: 20912 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 20913 if (!ISCD(un)) { 20914 err = ENOTTY; 20915 } else { 20916 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 20917 SD_PATH_STANDARD); 20918 } 20919 break; 20920 20921 case CDROMCLOSETRAY: 20922 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 20923 if (!ISCD(un)) { 20924 err = ENOTTY; 20925 } else { 20926 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE, 20927 SD_PATH_STANDARD); 20928 } 20929 break; 20930 20931 case FDEJECT: /* for eject command */ 20932 case DKIOCEJECT: 20933 case CDROMEJECT: 20934 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 20935 if (!ISREMOVABLE(un)) { 20936 err = ENOTTY; 20937 } else { 20938 err = sr_eject(dev); 20939 } 20940 break; 20941 20942 case CDROMVOLCTRL: 20943 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 20944 if (!ISCD(un)) { 20945 err = ENOTTY; 20946 } else { 20947 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 20948 } 20949 break; 20950 20951 case CDROMSUBCHNL: 20952 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 20953 if (!ISCD(un)) { 20954 err = ENOTTY; 20955 } else { 20956 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 20957 } 20958 break; 20959 20960 case CDROMREADMODE2: 20961 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 20962 if (!ISCD(un)) { 20963 err = ENOTTY; 20964 } else if (un->un_f_cfg_is_atapi == TRUE) { 20965 /* 20966 * If the drive supports READ CD, use that instead of 20967 * switching the LBA size via a MODE SELECT 20968 * Block Descriptor 20969 */ 20970 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 20971 } else { 20972 err = sr_read_mode2(dev, (caddr_t)arg, flag); 20973 } 20974 break; 20975 20976 case CDROMREADMODE1: 20977 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 20978 if (!ISCD(un)) { 20979 err = ENOTTY; 20980 } else { 20981 err = sr_read_mode1(dev, (caddr_t)arg, flag); 20982 } 20983 break; 20984 20985 case CDROMREADOFFSET: 20986 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 20987 if (!ISCD(un)) { 20988 err = ENOTTY; 20989 } else { 20990 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 20991 flag); 20992 } 20993 break; 20994 20995 case CDROMSBLKMODE: 20996 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 20997 /* 20998 * There is no means of changing block size in case of atapi 20999 * drives, thus return ENOTTY if drive type is atapi 21000 */ 21001 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21002 err = ENOTTY; 21003 } else if (un->un_f_mmc_cap == TRUE) { 21004 21005 /* 21006 * MMC Devices do not support changing the 21007 * logical block size 21008 * 21009 * Note: EINVAL is being returned instead of ENOTTY to 21010 * maintain consistancy with the original mmc 21011 * driver update. 21012 */ 21013 err = EINVAL; 21014 } else { 21015 mutex_enter(SD_MUTEX(un)); 21016 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 21017 (un->un_ncmds_in_transport > 0)) { 21018 mutex_exit(SD_MUTEX(un)); 21019 err = EINVAL; 21020 } else { 21021 mutex_exit(SD_MUTEX(un)); 21022 err = sr_change_blkmode(dev, cmd, arg, flag); 21023 } 21024 } 21025 break; 21026 21027 case CDROMGBLKMODE: 21028 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 21029 if (!ISCD(un)) { 21030 err = ENOTTY; 21031 } else if ((un->un_f_cfg_is_atapi != FALSE) && 21032 (un->un_f_blockcount_is_valid != FALSE)) { 21033 /* 21034 * Drive is an ATAPI drive so return target block 21035 * size for ATAPI drives since we cannot change the 21036 * blocksize on ATAPI drives. Used primarily to detect 21037 * if an ATAPI cdrom is present. 21038 */ 21039 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 21040 sizeof (int), flag) != 0) { 21041 err = EFAULT; 21042 } else { 21043 err = 0; 21044 } 21045 21046 } else { 21047 /* 21048 * Drive supports changing block sizes via a Mode 21049 * Select. 21050 */ 21051 err = sr_change_blkmode(dev, cmd, arg, flag); 21052 } 21053 break; 21054 21055 case CDROMGDRVSPEED: 21056 case CDROMSDRVSPEED: 21057 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 21058 if (!ISCD(un)) { 21059 err = ENOTTY; 21060 } else if (un->un_f_mmc_cap == TRUE) { 21061 /* 21062 * Note: In the future the driver implementation 21063 * for getting and 21064 * setting cd speed should entail: 21065 * 1) If non-mmc try the Toshiba mode page 21066 * (sr_change_speed) 21067 * 2) If mmc but no support for Real Time Streaming try 21068 * the SET CD SPEED (0xBB) command 21069 * (sr_atapi_change_speed) 21070 * 3) If mmc and support for Real Time Streaming 21071 * try the GET PERFORMANCE and SET STREAMING 21072 * commands (not yet implemented, 4380808) 21073 */ 21074 /* 21075 * As per recent MMC spec, CD-ROM speed is variable 21076 * and changes with LBA. Since there is no such 21077 * things as drive speed now, fail this ioctl. 21078 * 21079 * Note: EINVAL is returned for consistancy of original 21080 * implementation which included support for getting 21081 * the drive speed of mmc devices but not setting 21082 * the drive speed. Thus EINVAL would be returned 21083 * if a set request was made for an mmc device. 21084 * We no longer support get or set speed for 21085 * mmc but need to remain consistant with regard 21086 * to the error code returned. 21087 */ 21088 err = EINVAL; 21089 } else if (un->un_f_cfg_is_atapi == TRUE) { 21090 err = sr_atapi_change_speed(dev, cmd, arg, flag); 21091 } else { 21092 err = sr_change_speed(dev, cmd, arg, flag); 21093 } 21094 break; 21095 21096 case CDROMCDDA: 21097 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 21098 if (!ISCD(un)) { 21099 err = ENOTTY; 21100 } else { 21101 err = sr_read_cdda(dev, (void *)arg, flag); 21102 } 21103 break; 21104 21105 case CDROMCDXA: 21106 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 21107 if (!ISCD(un)) { 21108 err = ENOTTY; 21109 } else { 21110 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 21111 } 21112 break; 21113 21114 case CDROMSUBCODE: 21115 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 21116 if (!ISCD(un)) { 21117 err = ENOTTY; 21118 } else { 21119 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 21120 } 21121 break; 21122 21123 case DKIOCPARTINFO: { 21124 /* 21125 * Return parameters describing the selected disk slice. 21126 * Note: this ioctl is for the intel platform only 21127 */ 21128 #if defined(__i386) || defined(__amd64) 21129 int part; 21130 21131 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 21132 part = SDPART(dev); 21133 21134 /* don't check un_solaris_size for pN */ 21135 if (part < P0_RAW_DISK && un->un_solaris_size == 0) { 21136 err = EIO; 21137 } else { 21138 struct part_info p; 21139 21140 p.p_start = (daddr_t)un->un_offset[part]; 21141 p.p_length = (int)un->un_map[part].dkl_nblk; 21142 #ifdef _MULTI_DATAMODEL 21143 switch (ddi_model_convert_from(flag & FMODELS)) { 21144 case DDI_MODEL_ILP32: 21145 { 21146 struct part_info32 p32; 21147 21148 p32.p_start = (daddr32_t)p.p_start; 21149 p32.p_length = p.p_length; 21150 if (ddi_copyout(&p32, (void *)arg, 21151 sizeof (p32), flag)) 21152 err = EFAULT; 21153 break; 21154 } 21155 21156 case DDI_MODEL_NONE: 21157 { 21158 if (ddi_copyout(&p, (void *)arg, sizeof (p), 21159 flag)) 21160 err = EFAULT; 21161 break; 21162 } 21163 } 21164 #else /* ! _MULTI_DATAMODEL */ 21165 if (ddi_copyout(&p, (void *)arg, sizeof (p), flag)) 21166 err = EFAULT; 21167 #endif /* _MULTI_DATAMODEL */ 21168 } 21169 #else 21170 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 21171 err = ENOTTY; 21172 #endif 21173 break; 21174 } 21175 21176 case DKIOCG_PHYGEOM: { 21177 /* Return the driver's notion of the media physical geometry */ 21178 #if defined(__i386) || defined(__amd64) 21179 struct dk_geom disk_geom; 21180 struct dk_geom *dkgp = &disk_geom; 21181 21182 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 21183 mutex_enter(SD_MUTEX(un)); 21184 21185 if (un->un_g.dkg_nhead != 0 && 21186 un->un_g.dkg_nsect != 0) { 21187 /* 21188 * We succeeded in getting a geometry, but 21189 * right now it is being reported as just the 21190 * Solaris fdisk partition, just like for 21191 * DKIOCGGEOM. We need to change that to be 21192 * correct for the entire disk now. 21193 */ 21194 bcopy(&un->un_g, dkgp, sizeof (*dkgp)); 21195 dkgp->dkg_acyl = 0; 21196 dkgp->dkg_ncyl = un->un_blockcount / 21197 (dkgp->dkg_nhead * dkgp->dkg_nsect); 21198 } else { 21199 bzero(dkgp, sizeof (struct dk_geom)); 21200 /* 21201 * This disk does not have a Solaris VTOC 21202 * so we must present a physical geometry 21203 * that will remain consistent regardless 21204 * of how the disk is used. This will ensure 21205 * that the geometry does not change regardless 21206 * of the fdisk partition type (ie. EFI, FAT32, 21207 * Solaris, etc). 21208 */ 21209 if (ISCD(un)) { 21210 dkgp->dkg_nhead = un->un_pgeom.g_nhead; 21211 dkgp->dkg_nsect = un->un_pgeom.g_nsect; 21212 dkgp->dkg_ncyl = un->un_pgeom.g_ncyl; 21213 dkgp->dkg_acyl = un->un_pgeom.g_acyl; 21214 } else { 21215 sd_convert_geometry(un->un_blockcount, dkgp); 21216 dkgp->dkg_acyl = 0; 21217 dkgp->dkg_ncyl = un->un_blockcount / 21218 (dkgp->dkg_nhead * dkgp->dkg_nsect); 21219 } 21220 } 21221 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 21222 21223 if (ddi_copyout(dkgp, (void *)arg, 21224 sizeof (struct dk_geom), flag)) { 21225 mutex_exit(SD_MUTEX(un)); 21226 err = EFAULT; 21227 } else { 21228 mutex_exit(SD_MUTEX(un)); 21229 err = 0; 21230 } 21231 #else 21232 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 21233 err = ENOTTY; 21234 #endif 21235 break; 21236 } 21237 21238 case DKIOCG_VIRTGEOM: { 21239 /* Return the driver's notion of the media's logical geometry */ 21240 #if defined(__i386) || defined(__amd64) 21241 struct dk_geom disk_geom; 21242 struct dk_geom *dkgp = &disk_geom; 21243 21244 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 21245 mutex_enter(SD_MUTEX(un)); 21246 /* 21247 * If there is no HBA geometry available, or 21248 * if the HBA returned us something that doesn't 21249 * really fit into an Int 13/function 8 geometry 21250 * result, just fail the ioctl. See PSARC 1998/313. 21251 */ 21252 if (un->un_lgeom.g_nhead == 0 || 21253 un->un_lgeom.g_nsect == 0 || 21254 un->un_lgeom.g_ncyl > 1024) { 21255 mutex_exit(SD_MUTEX(un)); 21256 err = EINVAL; 21257 } else { 21258 dkgp->dkg_ncyl = un->un_lgeom.g_ncyl; 21259 dkgp->dkg_acyl = un->un_lgeom.g_acyl; 21260 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 21261 dkgp->dkg_nhead = un->un_lgeom.g_nhead; 21262 dkgp->dkg_nsect = un->un_lgeom.g_nsect; 21263 21264 if (ddi_copyout(dkgp, (void *)arg, 21265 sizeof (struct dk_geom), flag)) { 21266 mutex_exit(SD_MUTEX(un)); 21267 err = EFAULT; 21268 } else { 21269 mutex_exit(SD_MUTEX(un)); 21270 err = 0; 21271 } 21272 } 21273 #else 21274 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 21275 err = ENOTTY; 21276 #endif 21277 break; 21278 } 21279 #ifdef SDDEBUG 21280 /* RESET/ABORTS testing ioctls */ 21281 case DKIOCRESET: { 21282 int reset_level; 21283 21284 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 21285 err = EFAULT; 21286 } else { 21287 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 21288 "reset_level = 0x%lx\n", reset_level); 21289 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 21290 err = 0; 21291 } else { 21292 err = EIO; 21293 } 21294 } 21295 break; 21296 } 21297 21298 case DKIOCABORT: 21299 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 21300 if (scsi_abort(SD_ADDRESS(un), NULL)) { 21301 err = 0; 21302 } else { 21303 err = EIO; 21304 } 21305 break; 21306 #endif 21307 21308 #ifdef SD_FAULT_INJECTION 21309 /* SDIOC FaultInjection testing ioctls */ 21310 case SDIOCSTART: 21311 case SDIOCSTOP: 21312 case SDIOCINSERTPKT: 21313 case SDIOCINSERTXB: 21314 case SDIOCINSERTUN: 21315 case SDIOCINSERTARQ: 21316 case SDIOCPUSH: 21317 case SDIOCRETRIEVE: 21318 case SDIOCRUN: 21319 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 21320 "SDIOC detected cmd:0x%X:\n", cmd); 21321 /* call error generator */ 21322 sd_faultinjection_ioctl(cmd, arg, un); 21323 err = 0; 21324 break; 21325 21326 #endif /* SD_FAULT_INJECTION */ 21327 21328 default: 21329 err = ENOTTY; 21330 break; 21331 } 21332 mutex_enter(SD_MUTEX(un)); 21333 un->un_ncmds_in_driver--; 21334 ASSERT(un->un_ncmds_in_driver >= 0); 21335 mutex_exit(SD_MUTEX(un)); 21336 21337 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 21338 return (err); 21339 } 21340 21341 21342 /* 21343 * Function: sd_uscsi_ioctl 21344 * 21345 * Description: This routine is the driver entry point for handling USCSI ioctl 21346 * requests (USCSICMD). 21347 * 21348 * Arguments: dev - the device number 21349 * arg - user provided scsi command 21350 * flag - this argument is a pass through to ddi_copyxxx() 21351 * directly from the mode argument of ioctl(). 21352 * 21353 * Return Code: code returned by sd_send_scsi_cmd 21354 * ENXIO 21355 * EFAULT 21356 * EAGAIN 21357 */ 21358 21359 static int 21360 sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag) 21361 { 21362 #ifdef _MULTI_DATAMODEL 21363 /* 21364 * For use when a 32 bit app makes a call into a 21365 * 64 bit ioctl 21366 */ 21367 struct uscsi_cmd32 uscsi_cmd_32_for_64; 21368 struct uscsi_cmd32 *ucmd32 = &uscsi_cmd_32_for_64; 21369 model_t model; 21370 #endif /* _MULTI_DATAMODEL */ 21371 struct uscsi_cmd *scmd = NULL; 21372 struct sd_lun *un = NULL; 21373 enum uio_seg uioseg; 21374 char cdb[CDB_GROUP0]; 21375 int rval = 0; 21376 21377 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21378 return (ENXIO); 21379 } 21380 21381 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: entry: un:0x%p\n", un); 21382 21383 scmd = (struct uscsi_cmd *) 21384 kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 21385 21386 #ifdef _MULTI_DATAMODEL 21387 switch (model = ddi_model_convert_from(flag & FMODELS)) { 21388 case DDI_MODEL_ILP32: 21389 { 21390 if (ddi_copyin((void *)arg, ucmd32, sizeof (*ucmd32), flag)) { 21391 rval = EFAULT; 21392 goto done; 21393 } 21394 /* 21395 * Convert the ILP32 uscsi data from the 21396 * application to LP64 for internal use. 21397 */ 21398 uscsi_cmd32touscsi_cmd(ucmd32, scmd); 21399 break; 21400 } 21401 case DDI_MODEL_NONE: 21402 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 21403 rval = EFAULT; 21404 goto done; 21405 } 21406 break; 21407 } 21408 #else /* ! _MULTI_DATAMODEL */ 21409 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 21410 rval = EFAULT; 21411 goto done; 21412 } 21413 #endif /* _MULTI_DATAMODEL */ 21414 21415 scmd->uscsi_flags &= ~USCSI_NOINTR; 21416 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE; 21417 if (un->un_f_format_in_progress == TRUE) { 21418 rval = EAGAIN; 21419 goto done; 21420 } 21421 21422 /* 21423 * Gotta do the ddi_copyin() here on the uscsi_cdb so that 21424 * we will have a valid cdb[0] to test. 21425 */ 21426 if ((ddi_copyin(scmd->uscsi_cdb, cdb, CDB_GROUP0, flag) == 0) && 21427 (cdb[0] == SCMD_FORMAT)) { 21428 SD_TRACE(SD_LOG_IOCTL, un, 21429 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 21430 mutex_enter(SD_MUTEX(un)); 21431 un->un_f_format_in_progress = TRUE; 21432 mutex_exit(SD_MUTEX(un)); 21433 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 21434 SD_PATH_STANDARD); 21435 mutex_enter(SD_MUTEX(un)); 21436 un->un_f_format_in_progress = FALSE; 21437 mutex_exit(SD_MUTEX(un)); 21438 } else { 21439 SD_TRACE(SD_LOG_IOCTL, un, 21440 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 21441 /* 21442 * It's OK to fall into here even if the ddi_copyin() 21443 * on the uscsi_cdb above fails, because sd_send_scsi_cmd() 21444 * does this same copyin and will return the EFAULT 21445 * if it fails. 21446 */ 21447 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 21448 SD_PATH_STANDARD); 21449 } 21450 #ifdef _MULTI_DATAMODEL 21451 switch (model) { 21452 case DDI_MODEL_ILP32: 21453 /* 21454 * Convert back to ILP32 before copyout to the 21455 * application 21456 */ 21457 uscsi_cmdtouscsi_cmd32(scmd, ucmd32); 21458 if (ddi_copyout(ucmd32, (void *)arg, sizeof (*ucmd32), flag)) { 21459 if (rval != 0) { 21460 rval = EFAULT; 21461 } 21462 } 21463 break; 21464 case DDI_MODEL_NONE: 21465 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 21466 if (rval != 0) { 21467 rval = EFAULT; 21468 } 21469 } 21470 break; 21471 } 21472 #else /* ! _MULTI_DATAMODE */ 21473 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 21474 if (rval != 0) { 21475 rval = EFAULT; 21476 } 21477 } 21478 #endif /* _MULTI_DATAMODE */ 21479 done: 21480 kmem_free(scmd, sizeof (struct uscsi_cmd)); 21481 21482 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: exit: un:0x%p\n", un); 21483 21484 return (rval); 21485 } 21486 21487 21488 /* 21489 * Function: sd_dkio_ctrl_info 21490 * 21491 * Description: This routine is the driver entry point for handling controller 21492 * information ioctl requests (DKIOCINFO). 21493 * 21494 * Arguments: dev - the device number 21495 * arg - pointer to user provided dk_cinfo structure 21496 * specifying the controller type and attributes. 21497 * flag - this argument is a pass through to ddi_copyxxx() 21498 * directly from the mode argument of ioctl(). 21499 * 21500 * Return Code: 0 21501 * EFAULT 21502 * ENXIO 21503 */ 21504 21505 static int 21506 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 21507 { 21508 struct sd_lun *un = NULL; 21509 struct dk_cinfo *info; 21510 dev_info_t *pdip; 21511 int lun, tgt; 21512 21513 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21514 return (ENXIO); 21515 } 21516 21517 info = (struct dk_cinfo *) 21518 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 21519 21520 switch (un->un_ctype) { 21521 case CTYPE_CDROM: 21522 info->dki_ctype = DKC_CDROM; 21523 break; 21524 default: 21525 info->dki_ctype = DKC_SCSI_CCS; 21526 break; 21527 } 21528 pdip = ddi_get_parent(SD_DEVINFO(un)); 21529 info->dki_cnum = ddi_get_instance(pdip); 21530 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 21531 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 21532 } else { 21533 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 21534 DK_DEVLEN - 1); 21535 } 21536 21537 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 21538 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 21539 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 21540 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 21541 21542 /* Unit Information */ 21543 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 21544 info->dki_slave = ((tgt << 3) | lun); 21545 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 21546 DK_DEVLEN - 1); 21547 info->dki_flags = DKI_FMTVOL; 21548 info->dki_partition = SDPART(dev); 21549 21550 /* Max Transfer size of this device in blocks */ 21551 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 21552 info->dki_addr = 0; 21553 info->dki_space = 0; 21554 info->dki_prio = 0; 21555 info->dki_vec = 0; 21556 21557 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 21558 kmem_free(info, sizeof (struct dk_cinfo)); 21559 return (EFAULT); 21560 } else { 21561 kmem_free(info, sizeof (struct dk_cinfo)); 21562 return (0); 21563 } 21564 } 21565 21566 21567 /* 21568 * Function: sd_get_media_info 21569 * 21570 * Description: This routine is the driver entry point for handling ioctl 21571 * requests for the media type or command set profile used by the 21572 * drive to operate on the media (DKIOCGMEDIAINFO). 21573 * 21574 * Arguments: dev - the device number 21575 * arg - pointer to user provided dk_minfo structure 21576 * specifying the media type, logical block size and 21577 * drive capacity. 21578 * flag - this argument is a pass through to ddi_copyxxx() 21579 * directly from the mode argument of ioctl(). 21580 * 21581 * Return Code: 0 21582 * EACCESS 21583 * EFAULT 21584 * ENXIO 21585 * EIO 21586 */ 21587 21588 static int 21589 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 21590 { 21591 struct sd_lun *un = NULL; 21592 struct uscsi_cmd com; 21593 struct scsi_inquiry *sinq; 21594 struct dk_minfo media_info; 21595 u_longlong_t media_capacity; 21596 uint64_t capacity; 21597 uint_t lbasize; 21598 uchar_t *out_data; 21599 uchar_t *rqbuf; 21600 int rval = 0; 21601 int rtn; 21602 21603 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 21604 (un->un_state == SD_STATE_OFFLINE)) { 21605 return (ENXIO); 21606 } 21607 21608 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n"); 21609 21610 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 21611 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 21612 21613 /* Issue a TUR to determine if the drive is ready with media present */ 21614 rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA); 21615 if (rval == ENXIO) { 21616 goto done; 21617 } 21618 21619 /* Now get configuration data */ 21620 if (ISCD(un)) { 21621 media_info.dki_media_type = DK_CDROM; 21622 21623 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 21624 if (un->un_f_mmc_cap == TRUE) { 21625 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, 21626 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN); 21627 21628 if (rtn) { 21629 /* 21630 * Failed for other than an illegal request 21631 * or command not supported 21632 */ 21633 if ((com.uscsi_status == STATUS_CHECK) && 21634 (com.uscsi_rqstatus == STATUS_GOOD)) { 21635 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 21636 (rqbuf[12] != 0x20)) { 21637 rval = EIO; 21638 goto done; 21639 } 21640 } 21641 } else { 21642 /* 21643 * The GET CONFIGURATION command succeeded 21644 * so set the media type according to the 21645 * returned data 21646 */ 21647 media_info.dki_media_type = out_data[6]; 21648 media_info.dki_media_type <<= 8; 21649 media_info.dki_media_type |= out_data[7]; 21650 } 21651 } 21652 } else { 21653 /* 21654 * The profile list is not available, so we attempt to identify 21655 * the media type based on the inquiry data 21656 */ 21657 sinq = un->un_sd->sd_inq; 21658 if (sinq->inq_qual == 0) { 21659 /* This is a direct access device */ 21660 media_info.dki_media_type = DK_FIXED_DISK; 21661 21662 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 21663 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 21664 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 21665 media_info.dki_media_type = DK_ZIP; 21666 } else if ( 21667 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 21668 media_info.dki_media_type = DK_JAZ; 21669 } 21670 } 21671 } else { 21672 /* Not a CD or direct access so return unknown media */ 21673 media_info.dki_media_type = DK_UNKNOWN; 21674 } 21675 } 21676 21677 /* Now read the capacity so we can provide the lbasize and capacity */ 21678 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 21679 SD_PATH_DIRECT)) { 21680 case 0: 21681 break; 21682 case EACCES: 21683 rval = EACCES; 21684 goto done; 21685 default: 21686 rval = EIO; 21687 goto done; 21688 } 21689 21690 media_info.dki_lbsize = lbasize; 21691 media_capacity = capacity; 21692 21693 /* 21694 * sd_send_scsi_READ_CAPACITY() reports capacity in 21695 * un->un_sys_blocksize chunks. So we need to convert it into 21696 * cap.lbasize chunks. 21697 */ 21698 media_capacity *= un->un_sys_blocksize; 21699 media_capacity /= lbasize; 21700 media_info.dki_capacity = media_capacity; 21701 21702 if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) { 21703 rval = EFAULT; 21704 /* Put goto. Anybody might add some code below in future */ 21705 goto done; 21706 } 21707 done: 21708 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 21709 kmem_free(rqbuf, SENSE_LENGTH); 21710 return (rval); 21711 } 21712 21713 21714 /* 21715 * Function: sd_dkio_get_geometry 21716 * 21717 * Description: This routine is the driver entry point for handling user 21718 * requests to get the device geometry (DKIOCGGEOM). 21719 * 21720 * Arguments: dev - the device number 21721 * arg - pointer to user provided dk_geom structure specifying 21722 * the controller's notion of the current geometry. 21723 * flag - this argument is a pass through to ddi_copyxxx() 21724 * directly from the mode argument of ioctl(). 21725 * geom_validated - flag indicating if the device geometry has been 21726 * previously validated in the sdioctl routine. 21727 * 21728 * Return Code: 0 21729 * EFAULT 21730 * ENXIO 21731 * EIO 21732 */ 21733 21734 static int 21735 sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, int geom_validated) 21736 { 21737 struct sd_lun *un = NULL; 21738 struct dk_geom *tmp_geom = NULL; 21739 int rval = 0; 21740 21741 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21742 return (ENXIO); 21743 } 21744 21745 #if defined(__i386) || defined(__amd64) 21746 if (un->un_solaris_size == 0) { 21747 return (EIO); 21748 } 21749 #endif 21750 if (geom_validated == FALSE) { 21751 /* 21752 * sd_validate_geometry does not spin a disk up 21753 * if it was spun down. We need to make sure it 21754 * is ready. 21755 */ 21756 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 21757 return (rval); 21758 } 21759 mutex_enter(SD_MUTEX(un)); 21760 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 21761 mutex_exit(SD_MUTEX(un)); 21762 } 21763 if (rval) 21764 return (rval); 21765 21766 /* 21767 * Make a local copy of the soft state geometry to avoid some potential 21768 * race conditions associated with holding the mutex and updating the 21769 * write_reinstruct value 21770 */ 21771 tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 21772 mutex_enter(SD_MUTEX(un)); 21773 bcopy(&un->un_g, tmp_geom, sizeof (struct dk_geom)); 21774 mutex_exit(SD_MUTEX(un)); 21775 21776 if (tmp_geom->dkg_write_reinstruct == 0) { 21777 tmp_geom->dkg_write_reinstruct = 21778 (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm * 21779 sd_rot_delay) / (int)60000); 21780 } 21781 21782 rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom), 21783 flag); 21784 if (rval != 0) { 21785 rval = EFAULT; 21786 } 21787 21788 kmem_free(tmp_geom, sizeof (struct dk_geom)); 21789 return (rval); 21790 21791 } 21792 21793 21794 /* 21795 * Function: sd_dkio_set_geometry 21796 * 21797 * Description: This routine is the driver entry point for handling user 21798 * requests to set the device geometry (DKIOCSGEOM). The actual 21799 * device geometry is not updated, just the driver "notion" of it. 21800 * 21801 * Arguments: dev - the device number 21802 * arg - pointer to user provided dk_geom structure used to set 21803 * the controller's notion of the current geometry. 21804 * flag - this argument is a pass through to ddi_copyxxx() 21805 * directly from the mode argument of ioctl(). 21806 * 21807 * Return Code: 0 21808 * EFAULT 21809 * ENXIO 21810 * EIO 21811 */ 21812 21813 static int 21814 sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag) 21815 { 21816 struct sd_lun *un = NULL; 21817 struct dk_geom *tmp_geom; 21818 struct dk_map *lp; 21819 int rval = 0; 21820 int i; 21821 21822 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21823 return (ENXIO); 21824 } 21825 21826 #if defined(__i386) || defined(__amd64) 21827 if (un->un_solaris_size == 0) { 21828 return (EIO); 21829 } 21830 #endif 21831 /* 21832 * We need to copy the user specified geometry into local 21833 * storage and then update the softstate. We don't want to hold 21834 * the mutex and copyin directly from the user to the soft state 21835 */ 21836 tmp_geom = (struct dk_geom *) 21837 kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 21838 rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag); 21839 if (rval != 0) { 21840 kmem_free(tmp_geom, sizeof (struct dk_geom)); 21841 return (EFAULT); 21842 } 21843 21844 mutex_enter(SD_MUTEX(un)); 21845 bcopy(tmp_geom, &un->un_g, sizeof (struct dk_geom)); 21846 for (i = 0; i < NDKMAP; i++) { 21847 lp = &un->un_map[i]; 21848 un->un_offset[i] = 21849 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 21850 #if defined(__i386) || defined(__amd64) 21851 un->un_offset[i] += un->un_solaris_offset; 21852 #endif 21853 } 21854 un->un_f_geometry_is_valid = FALSE; 21855 mutex_exit(SD_MUTEX(un)); 21856 kmem_free(tmp_geom, sizeof (struct dk_geom)); 21857 21858 return (rval); 21859 } 21860 21861 21862 /* 21863 * Function: sd_dkio_get_partition 21864 * 21865 * Description: This routine is the driver entry point for handling user 21866 * requests to get the partition table (DKIOCGAPART). 21867 * 21868 * Arguments: dev - the device number 21869 * arg - pointer to user provided dk_allmap structure specifying 21870 * the controller's notion of the current partition table. 21871 * flag - this argument is a pass through to ddi_copyxxx() 21872 * directly from the mode argument of ioctl(). 21873 * geom_validated - flag indicating if the device geometry has been 21874 * previously validated in the sdioctl routine. 21875 * 21876 * Return Code: 0 21877 * EFAULT 21878 * ENXIO 21879 * EIO 21880 */ 21881 21882 static int 21883 sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, int geom_validated) 21884 { 21885 struct sd_lun *un = NULL; 21886 int rval = 0; 21887 int size; 21888 21889 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21890 return (ENXIO); 21891 } 21892 21893 #if defined(__i386) || defined(__amd64) 21894 if (un->un_solaris_size == 0) { 21895 return (EIO); 21896 } 21897 #endif 21898 /* 21899 * Make sure the geometry is valid before getting the partition 21900 * information. 21901 */ 21902 mutex_enter(SD_MUTEX(un)); 21903 if (geom_validated == FALSE) { 21904 /* 21905 * sd_validate_geometry does not spin a disk up 21906 * if it was spun down. We need to make sure it 21907 * is ready before validating the geometry. 21908 */ 21909 mutex_exit(SD_MUTEX(un)); 21910 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 21911 return (rval); 21912 } 21913 mutex_enter(SD_MUTEX(un)); 21914 21915 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 21916 mutex_exit(SD_MUTEX(un)); 21917 return (rval); 21918 } 21919 } 21920 mutex_exit(SD_MUTEX(un)); 21921 21922 #ifdef _MULTI_DATAMODEL 21923 switch (ddi_model_convert_from(flag & FMODELS)) { 21924 case DDI_MODEL_ILP32: { 21925 struct dk_map32 dk_map32[NDKMAP]; 21926 int i; 21927 21928 for (i = 0; i < NDKMAP; i++) { 21929 dk_map32[i].dkl_cylno = un->un_map[i].dkl_cylno; 21930 dk_map32[i].dkl_nblk = un->un_map[i].dkl_nblk; 21931 } 21932 size = NDKMAP * sizeof (struct dk_map32); 21933 rval = ddi_copyout(dk_map32, (void *)arg, size, flag); 21934 if (rval != 0) { 21935 rval = EFAULT; 21936 } 21937 break; 21938 } 21939 case DDI_MODEL_NONE: 21940 size = NDKMAP * sizeof (struct dk_map); 21941 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 21942 if (rval != 0) { 21943 rval = EFAULT; 21944 } 21945 break; 21946 } 21947 #else /* ! _MULTI_DATAMODEL */ 21948 size = NDKMAP * sizeof (struct dk_map); 21949 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 21950 if (rval != 0) { 21951 rval = EFAULT; 21952 } 21953 #endif /* _MULTI_DATAMODEL */ 21954 return (rval); 21955 } 21956 21957 21958 /* 21959 * Function: sd_dkio_set_partition 21960 * 21961 * Description: This routine is the driver entry point for handling user 21962 * requests to set the partition table (DKIOCSAPART). The actual 21963 * device partition is not updated. 21964 * 21965 * Arguments: dev - the device number 21966 * arg - pointer to user provided dk_allmap structure used to set 21967 * the controller's notion of the partition table. 21968 * flag - this argument is a pass through to ddi_copyxxx() 21969 * directly from the mode argument of ioctl(). 21970 * 21971 * Return Code: 0 21972 * EINVAL 21973 * EFAULT 21974 * ENXIO 21975 * EIO 21976 */ 21977 21978 static int 21979 sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag) 21980 { 21981 struct sd_lun *un = NULL; 21982 struct dk_map dk_map[NDKMAP]; 21983 struct dk_map *lp; 21984 int rval = 0; 21985 int size; 21986 int i; 21987 #if defined(_SUNOS_VTOC_16) 21988 struct dkl_partition *vp; 21989 #endif 21990 21991 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21992 return (ENXIO); 21993 } 21994 21995 /* 21996 * Set the map for all logical partitions. We lock 21997 * the priority just to make sure an interrupt doesn't 21998 * come in while the map is half updated. 21999 */ 22000 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_solaris_size)) 22001 mutex_enter(SD_MUTEX(un)); 22002 if (un->un_blockcount > DK_MAX_BLOCKS) { 22003 mutex_exit(SD_MUTEX(un)); 22004 return (ENOTSUP); 22005 } 22006 mutex_exit(SD_MUTEX(un)); 22007 if (un->un_solaris_size == 0) { 22008 return (EIO); 22009 } 22010 22011 #ifdef _MULTI_DATAMODEL 22012 switch (ddi_model_convert_from(flag & FMODELS)) { 22013 case DDI_MODEL_ILP32: { 22014 struct dk_map32 dk_map32[NDKMAP]; 22015 22016 size = NDKMAP * sizeof (struct dk_map32); 22017 rval = ddi_copyin((void *)arg, dk_map32, size, flag); 22018 if (rval != 0) { 22019 return (EFAULT); 22020 } 22021 for (i = 0; i < NDKMAP; i++) { 22022 dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno; 22023 dk_map[i].dkl_nblk = dk_map32[i].dkl_nblk; 22024 } 22025 break; 22026 } 22027 case DDI_MODEL_NONE: 22028 size = NDKMAP * sizeof (struct dk_map); 22029 rval = ddi_copyin((void *)arg, dk_map, size, flag); 22030 if (rval != 0) { 22031 return (EFAULT); 22032 } 22033 break; 22034 } 22035 #else /* ! _MULTI_DATAMODEL */ 22036 size = NDKMAP * sizeof (struct dk_map); 22037 rval = ddi_copyin((void *)arg, dk_map, size, flag); 22038 if (rval != 0) { 22039 return (EFAULT); 22040 } 22041 #endif /* _MULTI_DATAMODEL */ 22042 22043 mutex_enter(SD_MUTEX(un)); 22044 /* Note: The size used in this bcopy is set based upon the data model */ 22045 bcopy(dk_map, un->un_map, size); 22046 #if defined(_SUNOS_VTOC_16) 22047 vp = (struct dkl_partition *)&(un->un_vtoc); 22048 #endif /* defined(_SUNOS_VTOC_16) */ 22049 for (i = 0; i < NDKMAP; i++) { 22050 lp = &un->un_map[i]; 22051 un->un_offset[i] = 22052 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 22053 #if defined(_SUNOS_VTOC_16) 22054 vp->p_start = un->un_offset[i]; 22055 vp->p_size = lp->dkl_nblk; 22056 vp++; 22057 #endif /* defined(_SUNOS_VTOC_16) */ 22058 #if defined(__i386) || defined(__amd64) 22059 un->un_offset[i] += un->un_solaris_offset; 22060 #endif 22061 } 22062 mutex_exit(SD_MUTEX(un)); 22063 return (rval); 22064 } 22065 22066 22067 /* 22068 * Function: sd_dkio_get_vtoc 22069 * 22070 * Description: This routine is the driver entry point for handling user 22071 * requests to get the current volume table of contents 22072 * (DKIOCGVTOC). 22073 * 22074 * Arguments: dev - the device number 22075 * arg - pointer to user provided vtoc structure specifying 22076 * the current vtoc. 22077 * flag - this argument is a pass through to ddi_copyxxx() 22078 * directly from the mode argument of ioctl(). 22079 * geom_validated - flag indicating if the device geometry has been 22080 * previously validated in the sdioctl routine. 22081 * 22082 * Return Code: 0 22083 * EFAULT 22084 * ENXIO 22085 * EIO 22086 */ 22087 22088 static int 22089 sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, int geom_validated) 22090 { 22091 struct sd_lun *un = NULL; 22092 #if defined(_SUNOS_VTOC_8) 22093 struct vtoc user_vtoc; 22094 #endif /* defined(_SUNOS_VTOC_8) */ 22095 int rval = 0; 22096 22097 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22098 return (ENXIO); 22099 } 22100 22101 mutex_enter(SD_MUTEX(un)); 22102 if (geom_validated == FALSE) { 22103 /* 22104 * sd_validate_geometry does not spin a disk up 22105 * if it was spun down. We need to make sure it 22106 * is ready. 22107 */ 22108 mutex_exit(SD_MUTEX(un)); 22109 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22110 return (rval); 22111 } 22112 mutex_enter(SD_MUTEX(un)); 22113 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 22114 mutex_exit(SD_MUTEX(un)); 22115 return (rval); 22116 } 22117 } 22118 22119 #if defined(_SUNOS_VTOC_8) 22120 sd_build_user_vtoc(un, &user_vtoc); 22121 mutex_exit(SD_MUTEX(un)); 22122 22123 #ifdef _MULTI_DATAMODEL 22124 switch (ddi_model_convert_from(flag & FMODELS)) { 22125 case DDI_MODEL_ILP32: { 22126 struct vtoc32 user_vtoc32; 22127 22128 vtoctovtoc32(user_vtoc, user_vtoc32); 22129 if (ddi_copyout(&user_vtoc32, (void *)arg, 22130 sizeof (struct vtoc32), flag)) { 22131 return (EFAULT); 22132 } 22133 break; 22134 } 22135 22136 case DDI_MODEL_NONE: 22137 if (ddi_copyout(&user_vtoc, (void *)arg, 22138 sizeof (struct vtoc), flag)) { 22139 return (EFAULT); 22140 } 22141 break; 22142 } 22143 #else /* ! _MULTI_DATAMODEL */ 22144 if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) { 22145 return (EFAULT); 22146 } 22147 #endif /* _MULTI_DATAMODEL */ 22148 22149 #elif defined(_SUNOS_VTOC_16) 22150 mutex_exit(SD_MUTEX(un)); 22151 22152 #ifdef _MULTI_DATAMODEL 22153 /* 22154 * The un_vtoc structure is a "struct dk_vtoc" which is always 22155 * 32-bit to maintain compatibility with existing on-disk 22156 * structures. Thus, we need to convert the structure when copying 22157 * it out to a datamodel-dependent "struct vtoc" in a 64-bit 22158 * program. If the target is a 32-bit program, then no conversion 22159 * is necessary. 22160 */ 22161 /* LINTED: logical expression always true: op "||" */ 22162 ASSERT(sizeof (un->un_vtoc) == sizeof (struct vtoc32)); 22163 switch (ddi_model_convert_from(flag & FMODELS)) { 22164 case DDI_MODEL_ILP32: 22165 if (ddi_copyout(&(un->un_vtoc), (void *)arg, 22166 sizeof (un->un_vtoc), flag)) { 22167 return (EFAULT); 22168 } 22169 break; 22170 22171 case DDI_MODEL_NONE: { 22172 struct vtoc user_vtoc; 22173 22174 vtoc32tovtoc(un->un_vtoc, user_vtoc); 22175 if (ddi_copyout(&user_vtoc, (void *)arg, 22176 sizeof (struct vtoc), flag)) { 22177 return (EFAULT); 22178 } 22179 break; 22180 } 22181 } 22182 #else /* ! _MULTI_DATAMODEL */ 22183 if (ddi_copyout(&(un->un_vtoc), (void *)arg, sizeof (un->un_vtoc), 22184 flag)) { 22185 return (EFAULT); 22186 } 22187 #endif /* _MULTI_DATAMODEL */ 22188 #else 22189 #error "No VTOC format defined." 22190 #endif 22191 22192 return (rval); 22193 } 22194 22195 static int 22196 sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag) 22197 { 22198 struct sd_lun *un = NULL; 22199 dk_efi_t user_efi; 22200 int rval = 0; 22201 void *buffer; 22202 22203 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 22204 return (ENXIO); 22205 22206 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 22207 return (EFAULT); 22208 22209 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 22210 22211 if ((user_efi.dki_length % un->un_tgt_blocksize) || 22212 (user_efi.dki_length > un->un_max_xfer_size)) 22213 return (EINVAL); 22214 22215 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 22216 rval = sd_send_scsi_READ(un, buffer, user_efi.dki_length, 22217 user_efi.dki_lba, SD_PATH_DIRECT); 22218 if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data, 22219 user_efi.dki_length, flag) != 0) 22220 rval = EFAULT; 22221 22222 kmem_free(buffer, user_efi.dki_length); 22223 return (rval); 22224 } 22225 22226 /* 22227 * Function: sd_build_user_vtoc 22228 * 22229 * Description: This routine populates a pass by reference variable with the 22230 * current volume table of contents. 22231 * 22232 * Arguments: un - driver soft state (unit) structure 22233 * user_vtoc - pointer to vtoc structure to be populated 22234 */ 22235 22236 static void 22237 sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 22238 { 22239 struct dk_map2 *lpart; 22240 struct dk_map *lmap; 22241 struct partition *vpart; 22242 int nblks; 22243 int i; 22244 22245 ASSERT(mutex_owned(SD_MUTEX(un))); 22246 22247 /* 22248 * Return vtoc structure fields in the provided VTOC area, addressed 22249 * by *vtoc. 22250 */ 22251 bzero(user_vtoc, sizeof (struct vtoc)); 22252 user_vtoc->v_bootinfo[0] = un->un_vtoc.v_bootinfo[0]; 22253 user_vtoc->v_bootinfo[1] = un->un_vtoc.v_bootinfo[1]; 22254 user_vtoc->v_bootinfo[2] = un->un_vtoc.v_bootinfo[2]; 22255 user_vtoc->v_sanity = VTOC_SANE; 22256 user_vtoc->v_version = un->un_vtoc.v_version; 22257 bcopy(un->un_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL); 22258 user_vtoc->v_sectorsz = un->un_sys_blocksize; 22259 user_vtoc->v_nparts = un->un_vtoc.v_nparts; 22260 bcopy(un->un_vtoc.v_reserved, user_vtoc->v_reserved, 22261 sizeof (un->un_vtoc.v_reserved)); 22262 /* 22263 * Convert partitioning information. 22264 * 22265 * Note the conversion from starting cylinder number 22266 * to starting sector number. 22267 */ 22268 lmap = un->un_map; 22269 lpart = (struct dk_map2 *)un->un_vtoc.v_part; 22270 vpart = user_vtoc->v_part; 22271 22272 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 22273 22274 for (i = 0; i < V_NUMPAR; i++) { 22275 vpart->p_tag = lpart->p_tag; 22276 vpart->p_flag = lpart->p_flag; 22277 vpart->p_start = lmap->dkl_cylno * nblks; 22278 vpart->p_size = lmap->dkl_nblk; 22279 lmap++; 22280 lpart++; 22281 vpart++; 22282 22283 /* (4364927) */ 22284 user_vtoc->timestamp[i] = (time_t)un->un_vtoc.v_timestamp[i]; 22285 } 22286 22287 bcopy(un->un_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII); 22288 } 22289 22290 static int 22291 sd_dkio_partition(dev_t dev, caddr_t arg, int flag) 22292 { 22293 struct sd_lun *un = NULL; 22294 struct partition64 p64; 22295 int rval = 0; 22296 uint_t nparts; 22297 efi_gpe_t *partitions; 22298 efi_gpt_t *buffer; 22299 diskaddr_t gpe_lba; 22300 22301 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22302 return (ENXIO); 22303 } 22304 22305 if (ddi_copyin((const void *)arg, &p64, 22306 sizeof (struct partition64), flag)) { 22307 return (EFAULT); 22308 } 22309 22310 buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 22311 rval = sd_send_scsi_READ(un, buffer, DEV_BSIZE, 22312 1, SD_PATH_DIRECT); 22313 if (rval != 0) 22314 goto done_error; 22315 22316 sd_swap_efi_gpt(buffer); 22317 22318 if ((rval = sd_validate_efi(buffer)) != 0) 22319 goto done_error; 22320 22321 nparts = buffer->efi_gpt_NumberOfPartitionEntries; 22322 gpe_lba = buffer->efi_gpt_PartitionEntryLBA; 22323 if (p64.p_partno > nparts) { 22324 /* couldn't find it */ 22325 rval = ESRCH; 22326 goto done_error; 22327 } 22328 /* 22329 * if we're dealing with a partition that's out of the normal 22330 * 16K block, adjust accordingly 22331 */ 22332 gpe_lba += p64.p_partno / sizeof (efi_gpe_t); 22333 rval = sd_send_scsi_READ(un, buffer, EFI_MIN_ARRAY_SIZE, 22334 gpe_lba, SD_PATH_DIRECT); 22335 if (rval) { 22336 goto done_error; 22337 } 22338 partitions = (efi_gpe_t *)buffer; 22339 22340 sd_swap_efi_gpe(nparts, partitions); 22341 22342 partitions += p64.p_partno; 22343 bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type, 22344 sizeof (struct uuid)); 22345 p64.p_start = partitions->efi_gpe_StartingLBA; 22346 p64.p_size = partitions->efi_gpe_EndingLBA - 22347 p64.p_start + 1; 22348 22349 if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag)) 22350 rval = EFAULT; 22351 22352 done_error: 22353 kmem_free(buffer, EFI_MIN_ARRAY_SIZE); 22354 return (rval); 22355 } 22356 22357 22358 /* 22359 * Function: sd_dkio_set_vtoc 22360 * 22361 * Description: This routine is the driver entry point for handling user 22362 * requests to set the current volume table of contents 22363 * (DKIOCSVTOC). 22364 * 22365 * Arguments: dev - the device number 22366 * arg - pointer to user provided vtoc structure used to set the 22367 * current vtoc. 22368 * flag - this argument is a pass through to ddi_copyxxx() 22369 * directly from the mode argument of ioctl(). 22370 * 22371 * Return Code: 0 22372 * EFAULT 22373 * ENXIO 22374 * EINVAL 22375 * ENOTSUP 22376 */ 22377 22378 static int 22379 sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag) 22380 { 22381 struct sd_lun *un = NULL; 22382 struct vtoc user_vtoc; 22383 int rval = 0; 22384 22385 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22386 return (ENXIO); 22387 } 22388 22389 #if defined(__i386) || defined(__amd64) 22390 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 22391 return (EINVAL); 22392 } 22393 #endif 22394 22395 #ifdef _MULTI_DATAMODEL 22396 switch (ddi_model_convert_from(flag & FMODELS)) { 22397 case DDI_MODEL_ILP32: { 22398 struct vtoc32 user_vtoc32; 22399 22400 if (ddi_copyin((const void *)arg, &user_vtoc32, 22401 sizeof (struct vtoc32), flag)) { 22402 return (EFAULT); 22403 } 22404 vtoc32tovtoc(user_vtoc32, user_vtoc); 22405 break; 22406 } 22407 22408 case DDI_MODEL_NONE: 22409 if (ddi_copyin((const void *)arg, &user_vtoc, 22410 sizeof (struct vtoc), flag)) { 22411 return (EFAULT); 22412 } 22413 break; 22414 } 22415 #else /* ! _MULTI_DATAMODEL */ 22416 if (ddi_copyin((const void *)arg, &user_vtoc, 22417 sizeof (struct vtoc), flag)) { 22418 return (EFAULT); 22419 } 22420 #endif /* _MULTI_DATAMODEL */ 22421 22422 mutex_enter(SD_MUTEX(un)); 22423 if (un->un_blockcount > DK_MAX_BLOCKS) { 22424 mutex_exit(SD_MUTEX(un)); 22425 return (ENOTSUP); 22426 } 22427 if (un->un_g.dkg_ncyl == 0) { 22428 mutex_exit(SD_MUTEX(un)); 22429 return (EINVAL); 22430 } 22431 22432 mutex_exit(SD_MUTEX(un)); 22433 sd_clear_efi(un); 22434 ddi_remove_minor_node(SD_DEVINFO(un), "wd"); 22435 ddi_remove_minor_node(SD_DEVINFO(un), "wd,raw"); 22436 (void) ddi_create_minor_node(SD_DEVINFO(un), "h", 22437 S_IFBLK, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 22438 un->un_node_type, NULL); 22439 (void) ddi_create_minor_node(SD_DEVINFO(un), "h,raw", 22440 S_IFCHR, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 22441 un->un_node_type, NULL); 22442 mutex_enter(SD_MUTEX(un)); 22443 22444 if ((rval = sd_build_label_vtoc(un, &user_vtoc)) == 0) { 22445 if ((rval = sd_write_label(dev)) == 0) { 22446 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) 22447 != 0) { 22448 SD_ERROR(SD_LOG_IOCTL_DKIO, un, 22449 "sd_dkio_set_vtoc: " 22450 "Failed validate geometry\n"); 22451 } 22452 } 22453 } 22454 22455 /* 22456 * If sd_build_label_vtoc, or sd_write_label failed above write the 22457 * devid anyway, what can it hurt? Also preserve the device id by 22458 * writing to the disk acyl for the case where a devid has been 22459 * fabricated. 22460 */ 22461 if (!ISREMOVABLE(un) && !ISCD(un) && 22462 (un->un_f_opt_fab_devid == TRUE)) { 22463 if (un->un_devid == NULL) { 22464 sd_register_devid(un, SD_DEVINFO(un), 22465 SD_TARGET_IS_UNRESERVED); 22466 } else { 22467 /* 22468 * The device id for this disk has been 22469 * fabricated. Fabricated device id's are 22470 * managed by storing them in the last 2 22471 * available sectors on the drive. The device 22472 * id must be preserved by writing it back out 22473 * to this location. 22474 */ 22475 if (sd_write_deviceid(un) != 0) { 22476 ddi_devid_free(un->un_devid); 22477 un->un_devid = NULL; 22478 } 22479 } 22480 } 22481 mutex_exit(SD_MUTEX(un)); 22482 return (rval); 22483 } 22484 22485 22486 /* 22487 * Function: sd_build_label_vtoc 22488 * 22489 * Description: This routine updates the driver soft state current volume table 22490 * of contents based on a user specified vtoc. 22491 * 22492 * Arguments: un - driver soft state (unit) structure 22493 * user_vtoc - pointer to vtoc structure specifying vtoc to be used 22494 * to update the driver soft state. 22495 * 22496 * Return Code: 0 22497 * EINVAL 22498 */ 22499 22500 static int 22501 sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 22502 { 22503 struct dk_map *lmap; 22504 struct partition *vpart; 22505 int nblks; 22506 #if defined(_SUNOS_VTOC_8) 22507 int ncyl; 22508 struct dk_map2 *lpart; 22509 #endif /* defined(_SUNOS_VTOC_8) */ 22510 int i; 22511 22512 ASSERT(mutex_owned(SD_MUTEX(un))); 22513 22514 /* Sanity-check the vtoc */ 22515 if (user_vtoc->v_sanity != VTOC_SANE || 22516 user_vtoc->v_sectorsz != un->un_sys_blocksize || 22517 user_vtoc->v_nparts != V_NUMPAR) { 22518 return (EINVAL); 22519 } 22520 22521 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 22522 if (nblks == 0) { 22523 return (EINVAL); 22524 } 22525 22526 #if defined(_SUNOS_VTOC_8) 22527 vpart = user_vtoc->v_part; 22528 for (i = 0; i < V_NUMPAR; i++) { 22529 if ((vpart->p_start % nblks) != 0) { 22530 return (EINVAL); 22531 } 22532 ncyl = vpart->p_start / nblks; 22533 ncyl += vpart->p_size / nblks; 22534 if ((vpart->p_size % nblks) != 0) { 22535 ncyl++; 22536 } 22537 if (ncyl > (int)un->un_g.dkg_ncyl) { 22538 return (EINVAL); 22539 } 22540 vpart++; 22541 } 22542 #endif /* defined(_SUNOS_VTOC_8) */ 22543 22544 /* Put appropriate vtoc structure fields into the disk label */ 22545 #if defined(_SUNOS_VTOC_16) 22546 /* 22547 * The vtoc is always a 32bit data structure to maintain the 22548 * on-disk format. Convert "in place" instead of bcopying it. 22549 */ 22550 vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(un->un_vtoc)))); 22551 22552 /* 22553 * in the 16-slice vtoc, starting sectors are expressed in 22554 * numbers *relative* to the start of the Solaris fdisk partition. 22555 */ 22556 lmap = un->un_map; 22557 vpart = user_vtoc->v_part; 22558 22559 for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) { 22560 lmap->dkl_cylno = vpart->p_start / nblks; 22561 lmap->dkl_nblk = vpart->p_size; 22562 } 22563 22564 #elif defined(_SUNOS_VTOC_8) 22565 22566 un->un_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0]; 22567 un->un_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1]; 22568 un->un_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2]; 22569 22570 un->un_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity; 22571 un->un_vtoc.v_version = (uint32_t)user_vtoc->v_version; 22572 22573 bcopy(user_vtoc->v_volume, un->un_vtoc.v_volume, LEN_DKL_VVOL); 22574 22575 un->un_vtoc.v_nparts = user_vtoc->v_nparts; 22576 22577 bcopy(user_vtoc->v_reserved, un->un_vtoc.v_reserved, 22578 sizeof (un->un_vtoc.v_reserved)); 22579 22580 /* 22581 * Note the conversion from starting sector number 22582 * to starting cylinder number. 22583 * Return error if division results in a remainder. 22584 */ 22585 lmap = un->un_map; 22586 lpart = un->un_vtoc.v_part; 22587 vpart = user_vtoc->v_part; 22588 22589 for (i = 0; i < (int)user_vtoc->v_nparts; i++) { 22590 lpart->p_tag = vpart->p_tag; 22591 lpart->p_flag = vpart->p_flag; 22592 lmap->dkl_cylno = vpart->p_start / nblks; 22593 lmap->dkl_nblk = vpart->p_size; 22594 22595 lmap++; 22596 lpart++; 22597 vpart++; 22598 22599 /* (4387723) */ 22600 #ifdef _LP64 22601 if (user_vtoc->timestamp[i] > TIME32_MAX) { 22602 un->un_vtoc.v_timestamp[i] = TIME32_MAX; 22603 } else { 22604 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 22605 } 22606 #else 22607 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 22608 #endif 22609 } 22610 22611 bcopy(user_vtoc->v_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 22612 #else 22613 #error "No VTOC format defined." 22614 #endif 22615 return (0); 22616 } 22617 22618 /* 22619 * Function: sd_clear_efi 22620 * 22621 * Description: This routine clears all EFI labels. 22622 * 22623 * Arguments: un - driver soft state (unit) structure 22624 * 22625 * Return Code: void 22626 */ 22627 22628 static void 22629 sd_clear_efi(struct sd_lun *un) 22630 { 22631 efi_gpt_t *gpt; 22632 uint_t lbasize; 22633 uint64_t cap; 22634 int rval; 22635 22636 ASSERT(!mutex_owned(SD_MUTEX(un))); 22637 22638 gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP); 22639 22640 if (sd_send_scsi_READ(un, gpt, DEV_BSIZE, 1, SD_PATH_DIRECT) != 0) { 22641 goto done; 22642 } 22643 22644 sd_swap_efi_gpt(gpt); 22645 rval = sd_validate_efi(gpt); 22646 if (rval == 0) { 22647 /* clear primary */ 22648 bzero(gpt, sizeof (efi_gpt_t)); 22649 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 1, 22650 SD_PATH_DIRECT))) { 22651 SD_INFO(SD_LOG_IO_PARTITION, un, 22652 "sd_clear_efi: clear primary label failed\n"); 22653 } 22654 } 22655 /* the backup */ 22656 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 22657 SD_PATH_DIRECT); 22658 if (rval) { 22659 goto done; 22660 } 22661 if ((rval = sd_send_scsi_READ(un, gpt, lbasize, 22662 cap - 1, SD_PATH_DIRECT)) != 0) { 22663 goto done; 22664 } 22665 sd_swap_efi_gpt(gpt); 22666 rval = sd_validate_efi(gpt); 22667 if (rval == 0) { 22668 /* clear backup */ 22669 SD_TRACE(SD_LOG_IOCTL, un, "sd_clear_efi clear backup@%lu\n", 22670 cap-1); 22671 bzero(gpt, sizeof (efi_gpt_t)); 22672 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 22673 cap-1, SD_PATH_DIRECT))) { 22674 SD_INFO(SD_LOG_IO_PARTITION, un, 22675 "sd_clear_efi: clear backup label failed\n"); 22676 } 22677 } 22678 22679 done: 22680 kmem_free(gpt, sizeof (efi_gpt_t)); 22681 } 22682 22683 /* 22684 * Function: sd_set_vtoc 22685 * 22686 * Description: This routine writes data to the appropriate positions 22687 * 22688 * Arguments: un - driver soft state (unit) structure 22689 * dkl - the data to be written 22690 * 22691 * Return: void 22692 */ 22693 22694 static int 22695 sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl) 22696 { 22697 void *shadow_buf; 22698 uint_t label_addr; 22699 int sec; 22700 int blk; 22701 int head; 22702 int cyl; 22703 int rval; 22704 22705 #if defined(__i386) || defined(__amd64) 22706 label_addr = un->un_solaris_offset + DK_LABEL_LOC; 22707 #else 22708 /* Write the primary label at block 0 of the solaris partition. */ 22709 label_addr = 0; 22710 #endif 22711 22712 if (NOT_DEVBSIZE(un)) { 22713 shadow_buf = kmem_zalloc(un->un_tgt_blocksize, KM_SLEEP); 22714 /* 22715 * Read the target's first block. 22716 */ 22717 if ((rval = sd_send_scsi_READ(un, shadow_buf, 22718 un->un_tgt_blocksize, label_addr, 22719 SD_PATH_STANDARD)) != 0) { 22720 goto exit; 22721 } 22722 /* 22723 * Copy the contents of the label into the shadow buffer 22724 * which is of the size of target block size. 22725 */ 22726 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 22727 } 22728 22729 /* Write the primary label */ 22730 if (NOT_DEVBSIZE(un)) { 22731 rval = sd_send_scsi_WRITE(un, shadow_buf, un->un_tgt_blocksize, 22732 label_addr, SD_PATH_STANDARD); 22733 } else { 22734 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 22735 label_addr, SD_PATH_STANDARD); 22736 } 22737 if (rval != 0) { 22738 return (rval); 22739 } 22740 22741 /* 22742 * Calculate where the backup labels go. They are always on 22743 * the last alternate cylinder, but some older drives put them 22744 * on head 2 instead of the last head. They are always on the 22745 * first 5 odd sectors of the appropriate track. 22746 * 22747 * We have no choice at this point, but to believe that the 22748 * disk label is valid. Use the geometry of the disk 22749 * as described in the label. 22750 */ 22751 cyl = dkl->dkl_ncyl + dkl->dkl_acyl - 1; 22752 head = dkl->dkl_nhead - 1; 22753 22754 /* 22755 * Write and verify the backup labels. Make sure we don't try to 22756 * write past the last cylinder. 22757 */ 22758 for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) { 22759 blk = (daddr_t)( 22760 (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) + 22761 (head * dkl->dkl_nsect) + sec); 22762 #if defined(__i386) || defined(__amd64) 22763 blk += un->un_solaris_offset; 22764 #endif 22765 if (NOT_DEVBSIZE(un)) { 22766 uint64_t tblk; 22767 /* 22768 * Need to read the block first for read modify write. 22769 */ 22770 tblk = (uint64_t)blk; 22771 blk = (int)((tblk * un->un_sys_blocksize) / 22772 un->un_tgt_blocksize); 22773 if ((rval = sd_send_scsi_READ(un, shadow_buf, 22774 un->un_tgt_blocksize, blk, 22775 SD_PATH_STANDARD)) != 0) { 22776 goto exit; 22777 } 22778 /* 22779 * Modify the shadow buffer with the label. 22780 */ 22781 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 22782 rval = sd_send_scsi_WRITE(un, shadow_buf, 22783 un->un_tgt_blocksize, blk, SD_PATH_STANDARD); 22784 } else { 22785 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 22786 blk, SD_PATH_STANDARD); 22787 SD_INFO(SD_LOG_IO_PARTITION, un, 22788 "sd_set_vtoc: wrote backup label %d\n", blk); 22789 } 22790 if (rval != 0) { 22791 goto exit; 22792 } 22793 } 22794 exit: 22795 if (NOT_DEVBSIZE(un)) { 22796 kmem_free(shadow_buf, un->un_tgt_blocksize); 22797 } 22798 return (rval); 22799 } 22800 22801 /* 22802 * Function: sd_clear_vtoc 22803 * 22804 * Description: This routine clears out the VTOC labels. 22805 * 22806 * Arguments: un - driver soft state (unit) structure 22807 * 22808 * Return: void 22809 */ 22810 22811 static void 22812 sd_clear_vtoc(struct sd_lun *un) 22813 { 22814 struct dk_label *dkl; 22815 22816 mutex_exit(SD_MUTEX(un)); 22817 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 22818 mutex_enter(SD_MUTEX(un)); 22819 /* 22820 * sd_set_vtoc uses these fields in order to figure out 22821 * where to overwrite the backup labels 22822 */ 22823 dkl->dkl_apc = un->un_g.dkg_apc; 22824 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 22825 dkl->dkl_acyl = un->un_g.dkg_acyl; 22826 dkl->dkl_nhead = un->un_g.dkg_nhead; 22827 dkl->dkl_nsect = un->un_g.dkg_nsect; 22828 mutex_exit(SD_MUTEX(un)); 22829 (void) sd_set_vtoc(un, dkl); 22830 kmem_free(dkl, sizeof (struct dk_label)); 22831 22832 mutex_enter(SD_MUTEX(un)); 22833 } 22834 22835 /* 22836 * Function: sd_write_label 22837 * 22838 * Description: This routine will validate and write the driver soft state vtoc 22839 * contents to the device. 22840 * 22841 * Arguments: dev - the device number 22842 * 22843 * Return Code: the code returned by sd_send_scsi_cmd() 22844 * 0 22845 * EINVAL 22846 * ENXIO 22847 * ENOMEM 22848 */ 22849 22850 static int 22851 sd_write_label(dev_t dev) 22852 { 22853 struct sd_lun *un; 22854 struct dk_label *dkl; 22855 short sum; 22856 short *sp; 22857 int i; 22858 int rval; 22859 22860 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 22861 (un->un_state == SD_STATE_OFFLINE)) { 22862 return (ENXIO); 22863 } 22864 ASSERT(mutex_owned(SD_MUTEX(un))); 22865 mutex_exit(SD_MUTEX(un)); 22866 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 22867 mutex_enter(SD_MUTEX(un)); 22868 22869 bcopy(&un->un_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc)); 22870 dkl->dkl_rpm = un->un_g.dkg_rpm; 22871 dkl->dkl_pcyl = un->un_g.dkg_pcyl; 22872 dkl->dkl_apc = un->un_g.dkg_apc; 22873 dkl->dkl_intrlv = un->un_g.dkg_intrlv; 22874 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 22875 dkl->dkl_acyl = un->un_g.dkg_acyl; 22876 dkl->dkl_nhead = un->un_g.dkg_nhead; 22877 dkl->dkl_nsect = un->un_g.dkg_nsect; 22878 22879 #if defined(_SUNOS_VTOC_8) 22880 dkl->dkl_obs1 = un->un_g.dkg_obs1; 22881 dkl->dkl_obs2 = un->un_g.dkg_obs2; 22882 dkl->dkl_obs3 = un->un_g.dkg_obs3; 22883 for (i = 0; i < NDKMAP; i++) { 22884 dkl->dkl_map[i].dkl_cylno = un->un_map[i].dkl_cylno; 22885 dkl->dkl_map[i].dkl_nblk = un->un_map[i].dkl_nblk; 22886 } 22887 bcopy(un->un_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII); 22888 #elif defined(_SUNOS_VTOC_16) 22889 dkl->dkl_skew = un->un_dkg_skew; 22890 #else 22891 #error "No VTOC format defined." 22892 #endif 22893 22894 dkl->dkl_magic = DKL_MAGIC; 22895 dkl->dkl_write_reinstruct = un->un_g.dkg_write_reinstruct; 22896 dkl->dkl_read_reinstruct = un->un_g.dkg_read_reinstruct; 22897 22898 /* Construct checksum for the new disk label */ 22899 sum = 0; 22900 sp = (short *)dkl; 22901 i = sizeof (struct dk_label) / sizeof (short); 22902 while (i--) { 22903 sum ^= *sp++; 22904 } 22905 dkl->dkl_cksum = sum; 22906 22907 mutex_exit(SD_MUTEX(un)); 22908 22909 rval = sd_set_vtoc(un, dkl); 22910 exit: 22911 kmem_free(dkl, sizeof (struct dk_label)); 22912 mutex_enter(SD_MUTEX(un)); 22913 return (rval); 22914 } 22915 22916 static int 22917 sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag) 22918 { 22919 struct sd_lun *un = NULL; 22920 dk_efi_t user_efi; 22921 int rval = 0; 22922 void *buffer; 22923 22924 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 22925 return (ENXIO); 22926 22927 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 22928 return (EFAULT); 22929 22930 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 22931 22932 if ((user_efi.dki_length % un->un_tgt_blocksize) || 22933 (user_efi.dki_length > un->un_max_xfer_size)) 22934 return (EINVAL); 22935 22936 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 22937 if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) { 22938 rval = EFAULT; 22939 } else { 22940 /* 22941 * let's clear the vtoc labels and clear the softstate 22942 * vtoc. 22943 */ 22944 mutex_enter(SD_MUTEX(un)); 22945 if (un->un_vtoc.v_sanity == VTOC_SANE) { 22946 SD_TRACE(SD_LOG_IO_PARTITION, un, 22947 "sd_dkio_set_efi: CLEAR VTOC\n"); 22948 sd_clear_vtoc(un); 22949 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 22950 mutex_exit(SD_MUTEX(un)); 22951 ddi_remove_minor_node(SD_DEVINFO(un), "h"); 22952 ddi_remove_minor_node(SD_DEVINFO(un), "h,raw"); 22953 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd", 22954 S_IFBLK, 22955 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 22956 un->un_node_type, NULL); 22957 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd,raw", 22958 S_IFCHR, 22959 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 22960 un->un_node_type, NULL); 22961 } else 22962 mutex_exit(SD_MUTEX(un)); 22963 rval = sd_send_scsi_WRITE(un, buffer, user_efi.dki_length, 22964 user_efi.dki_lba, SD_PATH_DIRECT); 22965 if (rval == 0) { 22966 mutex_enter(SD_MUTEX(un)); 22967 un->un_f_geometry_is_valid = FALSE; 22968 mutex_exit(SD_MUTEX(un)); 22969 } 22970 } 22971 kmem_free(buffer, user_efi.dki_length); 22972 return (rval); 22973 } 22974 22975 /* 22976 * Function: sd_dkio_get_mboot 22977 * 22978 * Description: This routine is the driver entry point for handling user 22979 * requests to get the current device mboot (DKIOCGMBOOT) 22980 * 22981 * Arguments: dev - the device number 22982 * arg - pointer to user provided mboot structure specifying 22983 * the current mboot. 22984 * flag - this argument is a pass through to ddi_copyxxx() 22985 * directly from the mode argument of ioctl(). 22986 * 22987 * Return Code: 0 22988 * EINVAL 22989 * EFAULT 22990 * ENXIO 22991 */ 22992 22993 static int 22994 sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag) 22995 { 22996 struct sd_lun *un; 22997 struct mboot *mboot; 22998 int rval; 22999 size_t buffer_size; 23000 23001 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 23002 (un->un_state == SD_STATE_OFFLINE)) { 23003 return (ENXIO); 23004 } 23005 23006 #if defined(_SUNOS_VTOC_8) 23007 if ((!ISREMOVABLE(un)) || (arg == NULL)) { 23008 #elif defined(_SUNOS_VTOC_16) 23009 if (arg == NULL) { 23010 #endif 23011 return (EINVAL); 23012 } 23013 23014 /* 23015 * Read the mboot block, located at absolute block 0 on the target. 23016 */ 23017 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct mboot)); 23018 23019 SD_TRACE(SD_LOG_IO_PARTITION, un, 23020 "sd_dkio_get_mboot: allocation size: 0x%x\n", buffer_size); 23021 23022 mboot = kmem_zalloc(buffer_size, KM_SLEEP); 23023 if ((rval = sd_send_scsi_READ(un, mboot, buffer_size, 0, 23024 SD_PATH_STANDARD)) == 0) { 23025 if (ddi_copyout(mboot, (void *)arg, 23026 sizeof (struct mboot), flag) != 0) { 23027 rval = EFAULT; 23028 } 23029 } 23030 kmem_free(mboot, buffer_size); 23031 return (rval); 23032 } 23033 23034 23035 /* 23036 * Function: sd_dkio_set_mboot 23037 * 23038 * Description: This routine is the driver entry point for handling user 23039 * requests to validate and set the device master boot 23040 * (DKIOCSMBOOT). 23041 * 23042 * Arguments: dev - the device number 23043 * arg - pointer to user provided mboot structure used to set the 23044 * master boot. 23045 * flag - this argument is a pass through to ddi_copyxxx() 23046 * directly from the mode argument of ioctl(). 23047 * 23048 * Return Code: 0 23049 * EINVAL 23050 * EFAULT 23051 * ENXIO 23052 */ 23053 23054 static int 23055 sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag) 23056 { 23057 struct sd_lun *un = NULL; 23058 struct mboot *mboot = NULL; 23059 int rval; 23060 ushort_t magic; 23061 23062 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23063 return (ENXIO); 23064 } 23065 23066 ASSERT(!mutex_owned(SD_MUTEX(un))); 23067 23068 #if defined(_SUNOS_VTOC_8) 23069 if (!ISREMOVABLE(un)) { 23070 return (EINVAL); 23071 } 23072 #endif 23073 23074 if (arg == NULL) { 23075 return (EINVAL); 23076 } 23077 23078 mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP); 23079 23080 if (ddi_copyin((const void *)arg, mboot, 23081 sizeof (struct mboot), flag) != 0) { 23082 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23083 return (EFAULT); 23084 } 23085 23086 /* Is this really a master boot record? */ 23087 magic = LE_16(mboot->signature); 23088 if (magic != MBB_MAGIC) { 23089 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23090 return (EINVAL); 23091 } 23092 23093 rval = sd_send_scsi_WRITE(un, mboot, un->un_sys_blocksize, 0, 23094 SD_PATH_STANDARD); 23095 23096 mutex_enter(SD_MUTEX(un)); 23097 #if defined(__i386) || defined(__amd64) 23098 if (rval == 0) { 23099 /* 23100 * mboot has been written successfully. 23101 * update the fdisk and vtoc tables in memory 23102 */ 23103 rval = sd_update_fdisk_and_vtoc(un); 23104 if ((un->un_f_geometry_is_valid == FALSE) || (rval != 0)) { 23105 mutex_exit(SD_MUTEX(un)); 23106 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23107 return (rval); 23108 } 23109 } 23110 23111 /* 23112 * If the mboot write fails, write the devid anyway, what can it hurt? 23113 * Also preserve the device id by writing to the disk acyl for the case 23114 * where a devid has been fabricated. 23115 */ 23116 if (!ISREMOVABLE(un) && !ISCD(un) && 23117 (un->un_f_opt_fab_devid == TRUE)) { 23118 if (un->un_devid == NULL) { 23119 sd_register_devid(un, SD_DEVINFO(un), 23120 SD_TARGET_IS_UNRESERVED); 23121 } else { 23122 /* 23123 * The device id for this disk has been 23124 * fabricated. Fabricated device id's are 23125 * managed by storing them in the last 2 23126 * available sectors on the drive. The device 23127 * id must be preserved by writing it back out 23128 * to this location. 23129 */ 23130 if (sd_write_deviceid(un) != 0) { 23131 ddi_devid_free(un->un_devid); 23132 un->un_devid = NULL; 23133 } 23134 } 23135 } 23136 #else 23137 if (rval == 0) { 23138 /* 23139 * mboot has been written successfully. 23140 * set up the default geometry and VTOC 23141 */ 23142 if (un->un_blockcount <= DK_MAX_BLOCKS) 23143 sd_setup_default_geometry(un); 23144 } 23145 #endif 23146 mutex_exit(SD_MUTEX(un)); 23147 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23148 return (rval); 23149 } 23150 23151 23152 /* 23153 * Function: sd_setup_default_geometry 23154 * 23155 * Description: This local utility routine sets the default geometry as part of 23156 * setting the device mboot. 23157 * 23158 * Arguments: un - driver soft state (unit) structure 23159 * 23160 * Note: This may be redundant with sd_build_default_label. 23161 */ 23162 23163 static void 23164 sd_setup_default_geometry(struct sd_lun *un) 23165 { 23166 /* zero out the soft state geometry and partition table. */ 23167 bzero(&un->un_g, sizeof (struct dk_geom)); 23168 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 23169 bzero(un->un_map, NDKMAP * (sizeof (struct dk_map))); 23170 un->un_asciilabel[0] = '\0'; 23171 23172 /* 23173 * For the rpm, we use the minimum for the disk. 23174 * For the head, cyl and number of sector per track, 23175 * if the capacity <= 1GB, head = 64, sect = 32. 23176 * else head = 255, sect 63 23177 * Note: the capacity should be equal to C*H*S values. 23178 * This will cause some truncation of size due to 23179 * round off errors. For CD-ROMs, this truncation can 23180 * have adverse side effects, so returning ncyl and 23181 * nhead as 1. The nsect will overflow for most of 23182 * CD-ROMs as nsect is of type ushort. 23183 */ 23184 if (ISCD(un)) { 23185 un->un_g.dkg_ncyl = 1; 23186 un->un_g.dkg_nhead = 1; 23187 un->un_g.dkg_nsect = un->un_blockcount; 23188 } else { 23189 if (un->un_blockcount <= 0x1000) { 23190 /* Needed for unlabeled SCSI floppies. */ 23191 un->un_g.dkg_nhead = 2; 23192 un->un_g.dkg_ncyl = 80; 23193 un->un_g.dkg_pcyl = 80; 23194 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 23195 } else if (un->un_blockcount <= 0x200000) { 23196 un->un_g.dkg_nhead = 64; 23197 un->un_g.dkg_nsect = 32; 23198 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 23199 } else { 23200 un->un_g.dkg_nhead = 255; 23201 un->un_g.dkg_nsect = 63; 23202 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 23203 } 23204 un->un_blockcount = un->un_g.dkg_ncyl * 23205 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 23206 } 23207 un->un_g.dkg_acyl = 0; 23208 un->un_g.dkg_bcyl = 0; 23209 un->un_g.dkg_intrlv = 1; 23210 un->un_g.dkg_rpm = 200; 23211 un->un_g.dkg_read_reinstruct = 0; 23212 un->un_g.dkg_write_reinstruct = 0; 23213 if (un->un_g.dkg_pcyl == 0) { 23214 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl; 23215 } 23216 23217 un->un_map['a'-'a'].dkl_cylno = 0; 23218 un->un_map['a'-'a'].dkl_nblk = un->un_blockcount; 23219 un->un_map['c'-'a'].dkl_cylno = 0; 23220 un->un_map['c'-'a'].dkl_nblk = un->un_blockcount; 23221 un->un_f_geometry_is_valid = FALSE; 23222 } 23223 23224 23225 #if defined(__i386) || defined(__amd64) 23226 /* 23227 * Function: sd_update_fdisk_and_vtoc 23228 * 23229 * Description: This local utility routine updates the device fdisk and vtoc 23230 * as part of setting the device mboot. 23231 * 23232 * Arguments: un - driver soft state (unit) structure 23233 * 23234 * Return Code: 0 for success or errno-type return code. 23235 * 23236 * Note:x86: This looks like a duplicate of sd_validate_geometry(), but 23237 * these did exist seperately in x86 sd.c!!! 23238 */ 23239 23240 static int 23241 sd_update_fdisk_and_vtoc(struct sd_lun *un) 23242 { 23243 static char labelstring[128]; 23244 static char buf[256]; 23245 char *label = 0; 23246 int count; 23247 int label_rc = 0; 23248 int gvalid = un->un_f_geometry_is_valid; 23249 int fdisk_rval; 23250 int lbasize; 23251 int capacity; 23252 23253 ASSERT(mutex_owned(SD_MUTEX(un))); 23254 23255 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 23256 return (EINVAL); 23257 } 23258 23259 if (un->un_f_blockcount_is_valid == FALSE) { 23260 return (EINVAL); 23261 } 23262 23263 #if defined(_SUNOS_VTOC_16) 23264 /* 23265 * Set up the "whole disk" fdisk partition; this should always 23266 * exist, regardless of whether the disk contains an fdisk table 23267 * or vtoc. 23268 */ 23269 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 23270 un->un_map[P0_RAW_DISK].dkl_nblk = un->un_blockcount; 23271 #endif /* defined(_SUNOS_VTOC_16) */ 23272 23273 /* 23274 * copy the lbasize and capacity so that if they're 23275 * reset while we're not holding the SD_MUTEX(un), we will 23276 * continue to use valid values after the SD_MUTEX(un) is 23277 * reacquired. 23278 */ 23279 lbasize = un->un_tgt_blocksize; 23280 capacity = un->un_blockcount; 23281 23282 /* 23283 * refresh the logical and physical geometry caches. 23284 * (data from mode sense format/rigid disk geometry pages, 23285 * and scsi_ifgetcap("geometry"). 23286 */ 23287 sd_resync_geom_caches(un, capacity, lbasize, SD_PATH_DIRECT); 23288 23289 /* 23290 * Only DIRECT ACCESS devices will have Sun labels. 23291 * CD's supposedly have a Sun label, too 23292 */ 23293 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT || ISREMOVABLE(un)) { 23294 fdisk_rval = sd_read_fdisk(un, capacity, lbasize, 23295 SD_PATH_DIRECT); 23296 if (fdisk_rval == SD_CMD_FAILURE) { 23297 ASSERT(mutex_owned(SD_MUTEX(un))); 23298 return (EIO); 23299 } 23300 23301 if (fdisk_rval == SD_CMD_RESERVATION_CONFLICT) { 23302 ASSERT(mutex_owned(SD_MUTEX(un))); 23303 return (EACCES); 23304 } 23305 23306 if (un->un_solaris_size <= DK_LABEL_LOC) { 23307 /* 23308 * Found fdisk table but no Solaris partition entry, 23309 * so don't call sd_uselabel() and don't create 23310 * a default label. 23311 */ 23312 label_rc = 0; 23313 un->un_f_geometry_is_valid = TRUE; 23314 goto no_solaris_partition; 23315 } 23316 23317 #if defined(_SUNOS_VTOC_8) 23318 label = (char *)un->un_asciilabel; 23319 #elif defined(_SUNOS_VTOC_16) 23320 label = (char *)un->un_vtoc.v_asciilabel; 23321 #else 23322 #error "No VTOC format defined." 23323 #endif 23324 } else if (capacity < 0) { 23325 ASSERT(mutex_owned(SD_MUTEX(un))); 23326 return (EINVAL); 23327 } 23328 23329 /* 23330 * For Removable media We reach here if we have found a 23331 * SOLARIS PARTITION. 23332 * If un_f_geometry_is_valid is FALSE it indicates that the SOLARIS 23333 * PARTITION has changed from the previous one, hence we will setup a 23334 * default VTOC in this case. 23335 */ 23336 if (un->un_f_geometry_is_valid == FALSE) { 23337 sd_build_default_label(un); 23338 label_rc = 0; 23339 } 23340 23341 no_solaris_partition: 23342 if ((!ISREMOVABLE(un) || 23343 (ISREMOVABLE(un) && un->un_mediastate == DKIO_EJECTED)) && 23344 (un->un_state == SD_STATE_NORMAL && gvalid == FALSE)) { 23345 /* 23346 * Print out a message indicating who and what we are. 23347 * We do this only when we happen to really validate the 23348 * geometry. We may call sd_validate_geometry() at other 23349 * times, ioctl()'s like Get VTOC in which case we 23350 * don't want to print the label. 23351 * If the geometry is valid, print the label string, 23352 * else print vendor and product info, if available 23353 */ 23354 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 23355 SD_INFO(SD_LOG_IOCTL_DKIO, un, "?<%s>\n", label); 23356 } else { 23357 mutex_enter(&sd_label_mutex); 23358 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 23359 labelstring); 23360 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 23361 &labelstring[64]); 23362 (void) sprintf(buf, "?Vendor '%s', product '%s'", 23363 labelstring, &labelstring[64]); 23364 if (un->un_f_blockcount_is_valid == TRUE) { 23365 (void) sprintf(&buf[strlen(buf)], 23366 ", %" PRIu64 " %u byte blocks\n", 23367 un->un_blockcount, 23368 un->un_tgt_blocksize); 23369 } else { 23370 (void) sprintf(&buf[strlen(buf)], 23371 ", (unknown capacity)\n"); 23372 } 23373 SD_INFO(SD_LOG_IOCTL_DKIO, un, buf); 23374 mutex_exit(&sd_label_mutex); 23375 } 23376 } 23377 23378 #if defined(_SUNOS_VTOC_16) 23379 /* 23380 * If we have valid geometry, set up the remaining fdisk partitions. 23381 * Note that dkl_cylno is not used for the fdisk map entries, so 23382 * we set it to an entirely bogus value. 23383 */ 23384 for (count = 0; count < FD_NUMPART; count++) { 23385 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 23386 un->un_map[FDISK_P1 + count].dkl_nblk = 23387 un->un_fmap[count].fmap_nblk; 23388 un->un_offset[FDISK_P1 + count] = 23389 un->un_fmap[count].fmap_start; 23390 } 23391 #endif 23392 23393 for (count = 0; count < NDKMAP; count++) { 23394 #if defined(_SUNOS_VTOC_8) 23395 struct dk_map *lp = &un->un_map[count]; 23396 un->un_offset[count] = 23397 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 23398 #elif defined(_SUNOS_VTOC_16) 23399 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 23400 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 23401 #else 23402 #error "No VTOC format defined." 23403 #endif 23404 } 23405 23406 ASSERT(mutex_owned(SD_MUTEX(un))); 23407 return (label_rc); 23408 } 23409 #endif 23410 23411 23412 /* 23413 * Function: sd_check_media 23414 * 23415 * Description: This utility routine implements the functionality for the 23416 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 23417 * driver state changes from that specified by the user 23418 * (inserted or ejected). For example, if the user specifies 23419 * DKIO_EJECTED and the current media state is inserted this 23420 * routine will immediately return DKIO_INSERTED. However, if the 23421 * current media state is not inserted the user thread will be 23422 * blocked until the drive state changes. If DKIO_NONE is specified 23423 * the user thread will block until a drive state change occurs. 23424 * 23425 * Arguments: dev - the device number 23426 * state - user pointer to a dkio_state, updated with the current 23427 * drive state at return. 23428 * 23429 * Return Code: ENXIO 23430 * EIO 23431 * EAGAIN 23432 * EINTR 23433 */ 23434 23435 static int 23436 sd_check_media(dev_t dev, enum dkio_state state) 23437 { 23438 struct sd_lun *un = NULL; 23439 enum dkio_state prev_state; 23440 opaque_t token = NULL; 23441 int rval = 0; 23442 23443 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23444 return (ENXIO); 23445 } 23446 23447 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 23448 23449 mutex_enter(SD_MUTEX(un)); 23450 23451 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 23452 "state=%x, mediastate=%x\n", state, un->un_mediastate); 23453 23454 prev_state = un->un_mediastate; 23455 23456 /* is there anything to do? */ 23457 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 23458 /* 23459 * submit the request to the scsi_watch service; 23460 * scsi_media_watch_cb() does the real work 23461 */ 23462 mutex_exit(SD_MUTEX(un)); 23463 23464 /* 23465 * This change handles the case where a scsi watch request is 23466 * added to a device that is powered down. To accomplish this 23467 * we power up the device before adding the scsi watch request, 23468 * since the scsi watch sends a TUR directly to the device 23469 * which the device cannot handle if it is powered down. 23470 */ 23471 if (sd_pm_entry(un) != DDI_SUCCESS) { 23472 mutex_enter(SD_MUTEX(un)); 23473 goto done; 23474 } 23475 23476 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), 23477 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 23478 (caddr_t)dev); 23479 23480 sd_pm_exit(un); 23481 23482 mutex_enter(SD_MUTEX(un)); 23483 if (token == NULL) { 23484 rval = EAGAIN; 23485 goto done; 23486 } 23487 23488 /* 23489 * This is a special case IOCTL that doesn't return 23490 * until the media state changes. Routine sdpower 23491 * knows about and handles this so don't count it 23492 * as an active cmd in the driver, which would 23493 * keep the device busy to the pm framework. 23494 * If the count isn't decremented the device can't 23495 * be powered down. 23496 */ 23497 un->un_ncmds_in_driver--; 23498 ASSERT(un->un_ncmds_in_driver >= 0); 23499 23500 /* 23501 * if a prior request had been made, this will be the same 23502 * token, as scsi_watch was designed that way. 23503 */ 23504 un->un_swr_token = token; 23505 un->un_specified_mediastate = state; 23506 23507 /* 23508 * now wait for media change 23509 * we will not be signalled unless mediastate == state but it is 23510 * still better to test for this condition, since there is a 23511 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 23512 */ 23513 SD_TRACE(SD_LOG_COMMON, un, 23514 "sd_check_media: waiting for media state change\n"); 23515 while (un->un_mediastate == state) { 23516 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 23517 SD_TRACE(SD_LOG_COMMON, un, 23518 "sd_check_media: waiting for media state " 23519 "was interrupted\n"); 23520 un->un_ncmds_in_driver++; 23521 rval = EINTR; 23522 goto done; 23523 } 23524 SD_TRACE(SD_LOG_COMMON, un, 23525 "sd_check_media: received signal, state=%x\n", 23526 un->un_mediastate); 23527 } 23528 /* 23529 * Inc the counter to indicate the device once again 23530 * has an active outstanding cmd. 23531 */ 23532 un->un_ncmds_in_driver++; 23533 } 23534 23535 /* invalidate geometry */ 23536 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 23537 sr_ejected(un); 23538 } 23539 23540 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 23541 uint64_t capacity; 23542 uint_t lbasize; 23543 23544 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 23545 mutex_exit(SD_MUTEX(un)); 23546 /* 23547 * Since the following routines use SD_PATH_DIRECT, we must 23548 * call PM directly before the upcoming disk accesses. This 23549 * may cause the disk to be power/spin up. 23550 */ 23551 23552 if (sd_pm_entry(un) == DDI_SUCCESS) { 23553 rval = sd_send_scsi_READ_CAPACITY(un, 23554 &capacity, 23555 &lbasize, SD_PATH_DIRECT); 23556 if (rval != 0) { 23557 sd_pm_exit(un); 23558 mutex_enter(SD_MUTEX(un)); 23559 goto done; 23560 } 23561 } else { 23562 rval = EIO; 23563 mutex_enter(SD_MUTEX(un)); 23564 goto done; 23565 } 23566 mutex_enter(SD_MUTEX(un)); 23567 23568 sd_update_block_info(un, lbasize, capacity); 23569 23570 un->un_f_geometry_is_valid = FALSE; 23571 (void) sd_validate_geometry(un, SD_PATH_DIRECT); 23572 23573 mutex_exit(SD_MUTEX(un)); 23574 rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 23575 SD_PATH_DIRECT); 23576 sd_pm_exit(un); 23577 23578 mutex_enter(SD_MUTEX(un)); 23579 } 23580 done: 23581 un->un_f_watcht_stopped = FALSE; 23582 if (un->un_swr_token) { 23583 /* 23584 * Use of this local token and the mutex ensures that we avoid 23585 * some race conditions associated with terminating the 23586 * scsi watch. 23587 */ 23588 token = un->un_swr_token; 23589 un->un_swr_token = (opaque_t)NULL; 23590 mutex_exit(SD_MUTEX(un)); 23591 (void) scsi_watch_request_terminate(token, 23592 SCSI_WATCH_TERMINATE_WAIT); 23593 mutex_enter(SD_MUTEX(un)); 23594 } 23595 23596 /* 23597 * Update the capacity kstat value, if no media previously 23598 * (capacity kstat is 0) and a media has been inserted 23599 * (un_f_blockcount_is_valid == TRUE) 23600 * This is a more generic way then checking for ISREMOVABLE. 23601 */ 23602 if (un->un_errstats) { 23603 struct sd_errstats *stp = NULL; 23604 23605 stp = (struct sd_errstats *)un->un_errstats->ks_data; 23606 if ((stp->sd_capacity.value.ui64 == 0) && 23607 (un->un_f_blockcount_is_valid == TRUE)) { 23608 stp->sd_capacity.value.ui64 = 23609 (uint64_t)((uint64_t)un->un_blockcount * 23610 un->un_sys_blocksize); 23611 } 23612 } 23613 mutex_exit(SD_MUTEX(un)); 23614 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 23615 return (rval); 23616 } 23617 23618 23619 /* 23620 * Function: sd_delayed_cv_broadcast 23621 * 23622 * Description: Delayed cv_broadcast to allow for target to recover from media 23623 * insertion. 23624 * 23625 * Arguments: arg - driver soft state (unit) structure 23626 */ 23627 23628 static void 23629 sd_delayed_cv_broadcast(void *arg) 23630 { 23631 struct sd_lun *un = arg; 23632 23633 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 23634 23635 mutex_enter(SD_MUTEX(un)); 23636 un->un_dcvb_timeid = NULL; 23637 cv_broadcast(&un->un_state_cv); 23638 mutex_exit(SD_MUTEX(un)); 23639 } 23640 23641 23642 /* 23643 * Function: sd_media_watch_cb 23644 * 23645 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 23646 * routine processes the TUR sense data and updates the driver 23647 * state if a transition has occurred. The user thread 23648 * (sd_check_media) is then signalled. 23649 * 23650 * Arguments: arg - the device 'dev_t' is used for context to discriminate 23651 * among multiple watches that share this callback function 23652 * resultp - scsi watch facility result packet containing scsi 23653 * packet, status byte and sense data 23654 * 23655 * Return Code: 0 for success, -1 for failure 23656 */ 23657 23658 static int 23659 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 23660 { 23661 struct sd_lun *un; 23662 struct scsi_status *statusp = resultp->statusp; 23663 struct scsi_extended_sense *sensep = resultp->sensep; 23664 enum dkio_state state = DKIO_NONE; 23665 dev_t dev = (dev_t)arg; 23666 uchar_t actual_sense_length; 23667 23668 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23669 return (-1); 23670 } 23671 actual_sense_length = resultp->actual_sense_length; 23672 23673 mutex_enter(SD_MUTEX(un)); 23674 SD_TRACE(SD_LOG_COMMON, un, 23675 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 23676 *((char *)statusp), (void *)sensep, actual_sense_length); 23677 23678 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 23679 un->un_mediastate = DKIO_DEV_GONE; 23680 printf("sd_media_watch_cb: dev gone\n"); 23681 cv_broadcast(&un->un_state_cv); 23682 mutex_exit(SD_MUTEX(un)); 23683 23684 return (0); 23685 } 23686 23687 /* 23688 * If there was a check condition then sensep points to valid sense data 23689 * If status was not a check condition but a reservation or busy status 23690 * then the new state is DKIO_NONE 23691 */ 23692 if (sensep != NULL) { 23693 SD_INFO(SD_LOG_COMMON, un, 23694 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 23695 sensep->es_key, sensep->es_add_code, sensep->es_qual_code); 23696 /* This routine only uses up to 13 bytes of sense data. */ 23697 if (actual_sense_length >= 13) { 23698 if (sensep->es_key == KEY_UNIT_ATTENTION) { 23699 if (sensep->es_add_code == 0x28) { 23700 state = DKIO_INSERTED; 23701 } 23702 } else { 23703 /* 23704 * if 02/04/02 means that the host 23705 * should send start command. Explicitly 23706 * leave the media state as is 23707 * (inserted) as the media is inserted 23708 * and host has stopped device for PM 23709 * reasons. Upon next true read/write 23710 * to this media will bring the 23711 * device to the right state good for 23712 * media access. 23713 */ 23714 if ((sensep->es_key == KEY_NOT_READY) && 23715 (sensep->es_add_code == 0x3a)) { 23716 state = DKIO_EJECTED; 23717 } 23718 23719 /* 23720 * If the drivge is busy with an operation 23721 * or long write, keep the media in an 23722 * inserted state. 23723 */ 23724 23725 if ((sensep->es_key == KEY_NOT_READY) && 23726 (sensep->es_add_code == 0x04) && 23727 ((sensep->es_qual_code == 0x02) || 23728 (sensep->es_qual_code == 0x07) || 23729 (sensep->es_qual_code == 0x08))) { 23730 state = DKIO_INSERTED; 23731 } 23732 } 23733 } 23734 } else if ((*((char *)statusp) == STATUS_GOOD) && 23735 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 23736 state = DKIO_INSERTED; 23737 } 23738 23739 SD_TRACE(SD_LOG_COMMON, un, 23740 "sd_media_watch_cb: state=%x, specified=%x\n", 23741 state, un->un_specified_mediastate); 23742 23743 /* 23744 * now signal the waiting thread if this is *not* the specified state; 23745 * delay the signal if the state is DKIO_INSERTED to allow the target 23746 * to recover 23747 */ 23748 if (state != un->un_specified_mediastate) { 23749 un->un_mediastate = state; 23750 if (state == DKIO_INSERTED) { 23751 /* 23752 * delay the signal to give the drive a chance 23753 * to do what it apparently needs to do 23754 */ 23755 SD_TRACE(SD_LOG_COMMON, un, 23756 "sd_media_watch_cb: delayed cv_broadcast\n"); 23757 if (un->un_dcvb_timeid == NULL) { 23758 un->un_dcvb_timeid = 23759 timeout(sd_delayed_cv_broadcast, un, 23760 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 23761 } 23762 } else { 23763 SD_TRACE(SD_LOG_COMMON, un, 23764 "sd_media_watch_cb: immediate cv_broadcast\n"); 23765 cv_broadcast(&un->un_state_cv); 23766 } 23767 } 23768 mutex_exit(SD_MUTEX(un)); 23769 return (0); 23770 } 23771 23772 23773 /* 23774 * Function: sd_dkio_get_temp 23775 * 23776 * Description: This routine is the driver entry point for handling ioctl 23777 * requests to get the disk temperature. 23778 * 23779 * Arguments: dev - the device number 23780 * arg - pointer to user provided dk_temperature structure. 23781 * flag - this argument is a pass through to ddi_copyxxx() 23782 * directly from the mode argument of ioctl(). 23783 * 23784 * Return Code: 0 23785 * EFAULT 23786 * ENXIO 23787 * EAGAIN 23788 */ 23789 23790 static int 23791 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 23792 { 23793 struct sd_lun *un = NULL; 23794 struct dk_temperature *dktemp = NULL; 23795 uchar_t *temperature_page; 23796 int rval = 0; 23797 int path_flag = SD_PATH_STANDARD; 23798 23799 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23800 return (ENXIO); 23801 } 23802 23803 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 23804 23805 /* copyin the disk temp argument to get the user flags */ 23806 if (ddi_copyin((void *)arg, dktemp, 23807 sizeof (struct dk_temperature), flag) != 0) { 23808 rval = EFAULT; 23809 goto done; 23810 } 23811 23812 /* Initialize the temperature to invalid. */ 23813 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 23814 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 23815 23816 /* 23817 * Note: Investigate removing the "bypass pm" semantic. 23818 * Can we just bypass PM always? 23819 */ 23820 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 23821 path_flag = SD_PATH_DIRECT; 23822 ASSERT(!mutex_owned(&un->un_pm_mutex)); 23823 mutex_enter(&un->un_pm_mutex); 23824 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 23825 /* 23826 * If DKT_BYPASS_PM is set, and the drive happens to be 23827 * in low power mode, we can not wake it up, Need to 23828 * return EAGAIN. 23829 */ 23830 mutex_exit(&un->un_pm_mutex); 23831 rval = EAGAIN; 23832 goto done; 23833 } else { 23834 /* 23835 * Indicate to PM the device is busy. This is required 23836 * to avoid a race - i.e. the ioctl is issuing a 23837 * command and the pm framework brings down the device 23838 * to low power mode (possible power cut-off on some 23839 * platforms). 23840 */ 23841 mutex_exit(&un->un_pm_mutex); 23842 if (sd_pm_entry(un) != DDI_SUCCESS) { 23843 rval = EAGAIN; 23844 goto done; 23845 } 23846 } 23847 } 23848 23849 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 23850 23851 if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page, 23852 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) { 23853 goto done2; 23854 } 23855 23856 /* 23857 * For the current temperature verify that the parameter length is 0x02 23858 * and the parameter code is 0x00 23859 */ 23860 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 23861 (temperature_page[5] == 0x00)) { 23862 if (temperature_page[9] == 0xFF) { 23863 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 23864 } else { 23865 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 23866 } 23867 } 23868 23869 /* 23870 * For the reference temperature verify that the parameter 23871 * length is 0x02 and the parameter code is 0x01 23872 */ 23873 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 23874 (temperature_page[11] == 0x01)) { 23875 if (temperature_page[15] == 0xFF) { 23876 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 23877 } else { 23878 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 23879 } 23880 } 23881 23882 /* Do the copyout regardless of the temperature commands status. */ 23883 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 23884 flag) != 0) { 23885 rval = EFAULT; 23886 } 23887 23888 done2: 23889 if (path_flag == SD_PATH_DIRECT) { 23890 sd_pm_exit(un); 23891 } 23892 23893 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 23894 done: 23895 if (dktemp != NULL) { 23896 kmem_free(dktemp, sizeof (struct dk_temperature)); 23897 } 23898 23899 return (rval); 23900 } 23901 23902 23903 /* 23904 * Function: sd_log_page_supported 23905 * 23906 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 23907 * supported log pages. 23908 * 23909 * Arguments: un - 23910 * log_page - 23911 * 23912 * Return Code: -1 - on error (log sense is optional and may not be supported). 23913 * 0 - log page not found. 23914 * 1 - log page found. 23915 */ 23916 23917 static int 23918 sd_log_page_supported(struct sd_lun *un, int log_page) 23919 { 23920 uchar_t *log_page_data; 23921 int i; 23922 int match = 0; 23923 int log_size; 23924 23925 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 23926 23927 if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0, 23928 SD_PATH_DIRECT) != 0) { 23929 SD_ERROR(SD_LOG_COMMON, un, 23930 "sd_log_page_supported: failed log page retrieval\n"); 23931 kmem_free(log_page_data, 0xFF); 23932 return (-1); 23933 } 23934 log_size = log_page_data[3]; 23935 23936 /* 23937 * The list of supported log pages start from the fourth byte. Check 23938 * until we run out of log pages or a match is found. 23939 */ 23940 for (i = 4; (i < (log_size + 4)) && !match; i++) { 23941 if (log_page_data[i] == log_page) { 23942 match++; 23943 } 23944 } 23945 kmem_free(log_page_data, 0xFF); 23946 return (match); 23947 } 23948 23949 23950 /* 23951 * Function: sd_mhdioc_failfast 23952 * 23953 * Description: This routine is the driver entry point for handling ioctl 23954 * requests to enable/disable the multihost failfast option. 23955 * (MHIOCENFAILFAST) 23956 * 23957 * Arguments: dev - the device number 23958 * arg - user specified probing interval. 23959 * flag - this argument is a pass through to ddi_copyxxx() 23960 * directly from the mode argument of ioctl(). 23961 * 23962 * Return Code: 0 23963 * EFAULT 23964 * ENXIO 23965 */ 23966 23967 static int 23968 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 23969 { 23970 struct sd_lun *un = NULL; 23971 int mh_time; 23972 int rval = 0; 23973 23974 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23975 return (ENXIO); 23976 } 23977 23978 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 23979 return (EFAULT); 23980 23981 if (mh_time) { 23982 mutex_enter(SD_MUTEX(un)); 23983 un->un_resvd_status |= SD_FAILFAST; 23984 mutex_exit(SD_MUTEX(un)); 23985 /* 23986 * If mh_time is INT_MAX, then this ioctl is being used for 23987 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 23988 */ 23989 if (mh_time != INT_MAX) { 23990 rval = sd_check_mhd(dev, mh_time); 23991 } 23992 } else { 23993 (void) sd_check_mhd(dev, 0); 23994 mutex_enter(SD_MUTEX(un)); 23995 un->un_resvd_status &= ~SD_FAILFAST; 23996 mutex_exit(SD_MUTEX(un)); 23997 } 23998 return (rval); 23999 } 24000 24001 24002 /* 24003 * Function: sd_mhdioc_takeown 24004 * 24005 * Description: This routine is the driver entry point for handling ioctl 24006 * requests to forcefully acquire exclusive access rights to the 24007 * multihost disk (MHIOCTKOWN). 24008 * 24009 * Arguments: dev - the device number 24010 * arg - user provided structure specifying the delay 24011 * parameters in milliseconds 24012 * flag - this argument is a pass through to ddi_copyxxx() 24013 * directly from the mode argument of ioctl(). 24014 * 24015 * Return Code: 0 24016 * EFAULT 24017 * ENXIO 24018 */ 24019 24020 static int 24021 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 24022 { 24023 struct sd_lun *un = NULL; 24024 struct mhioctkown *tkown = NULL; 24025 int rval = 0; 24026 24027 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24028 return (ENXIO); 24029 } 24030 24031 if (arg != NULL) { 24032 tkown = (struct mhioctkown *) 24033 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 24034 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 24035 if (rval != 0) { 24036 rval = EFAULT; 24037 goto error; 24038 } 24039 } 24040 24041 rval = sd_take_ownership(dev, tkown); 24042 mutex_enter(SD_MUTEX(un)); 24043 if (rval == 0) { 24044 un->un_resvd_status |= SD_RESERVE; 24045 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 24046 sd_reinstate_resv_delay = 24047 tkown->reinstate_resv_delay * 1000; 24048 } else { 24049 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 24050 } 24051 /* 24052 * Give the scsi_watch routine interval set by 24053 * the MHIOCENFAILFAST ioctl precedence here. 24054 */ 24055 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 24056 mutex_exit(SD_MUTEX(un)); 24057 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 24058 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24059 "sd_mhdioc_takeown : %d\n", 24060 sd_reinstate_resv_delay); 24061 } else { 24062 mutex_exit(SD_MUTEX(un)); 24063 } 24064 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 24065 sd_mhd_reset_notify_cb, (caddr_t)un); 24066 } else { 24067 un->un_resvd_status &= ~SD_RESERVE; 24068 mutex_exit(SD_MUTEX(un)); 24069 } 24070 24071 error: 24072 if (tkown != NULL) { 24073 kmem_free(tkown, sizeof (struct mhioctkown)); 24074 } 24075 return (rval); 24076 } 24077 24078 24079 /* 24080 * Function: sd_mhdioc_release 24081 * 24082 * Description: This routine is the driver entry point for handling ioctl 24083 * requests to release exclusive access rights to the multihost 24084 * disk (MHIOCRELEASE). 24085 * 24086 * Arguments: dev - the device number 24087 * 24088 * Return Code: 0 24089 * ENXIO 24090 */ 24091 24092 static int 24093 sd_mhdioc_release(dev_t dev) 24094 { 24095 struct sd_lun *un = NULL; 24096 timeout_id_t resvd_timeid_save; 24097 int resvd_status_save; 24098 int rval = 0; 24099 24100 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24101 return (ENXIO); 24102 } 24103 24104 mutex_enter(SD_MUTEX(un)); 24105 resvd_status_save = un->un_resvd_status; 24106 un->un_resvd_status &= 24107 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 24108 if (un->un_resvd_timeid) { 24109 resvd_timeid_save = un->un_resvd_timeid; 24110 un->un_resvd_timeid = NULL; 24111 mutex_exit(SD_MUTEX(un)); 24112 (void) untimeout(resvd_timeid_save); 24113 } else { 24114 mutex_exit(SD_MUTEX(un)); 24115 } 24116 24117 /* 24118 * destroy any pending timeout thread that may be attempting to 24119 * reinstate reservation on this device. 24120 */ 24121 sd_rmv_resv_reclaim_req(dev); 24122 24123 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 24124 mutex_enter(SD_MUTEX(un)); 24125 if ((un->un_mhd_token) && 24126 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 24127 mutex_exit(SD_MUTEX(un)); 24128 (void) sd_check_mhd(dev, 0); 24129 } else { 24130 mutex_exit(SD_MUTEX(un)); 24131 } 24132 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 24133 sd_mhd_reset_notify_cb, (caddr_t)un); 24134 } else { 24135 /* 24136 * sd_mhd_watch_cb will restart the resvd recover timeout thread 24137 */ 24138 mutex_enter(SD_MUTEX(un)); 24139 un->un_resvd_status = resvd_status_save; 24140 mutex_exit(SD_MUTEX(un)); 24141 } 24142 return (rval); 24143 } 24144 24145 24146 /* 24147 * Function: sd_mhdioc_register_devid 24148 * 24149 * Description: This routine is the driver entry point for handling ioctl 24150 * requests to register the device id (MHIOCREREGISTERDEVID). 24151 * 24152 * Note: The implementation for this ioctl has been updated to 24153 * be consistent with the original PSARC case (1999/357) 24154 * (4375899, 4241671, 4220005) 24155 * 24156 * Arguments: dev - the device number 24157 * 24158 * Return Code: 0 24159 * ENXIO 24160 */ 24161 24162 static int 24163 sd_mhdioc_register_devid(dev_t dev) 24164 { 24165 struct sd_lun *un = NULL; 24166 int rval = 0; 24167 24168 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24169 return (ENXIO); 24170 } 24171 24172 ASSERT(!mutex_owned(SD_MUTEX(un))); 24173 24174 mutex_enter(SD_MUTEX(un)); 24175 24176 /* If a devid already exists, de-register it */ 24177 if (un->un_devid != NULL) { 24178 ddi_devid_unregister(SD_DEVINFO(un)); 24179 /* 24180 * After unregister devid, needs to free devid memory 24181 */ 24182 ddi_devid_free(un->un_devid); 24183 un->un_devid = NULL; 24184 } 24185 24186 /* Check for reservation conflict */ 24187 mutex_exit(SD_MUTEX(un)); 24188 rval = sd_send_scsi_TEST_UNIT_READY(un, 0); 24189 mutex_enter(SD_MUTEX(un)); 24190 24191 switch (rval) { 24192 case 0: 24193 sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 24194 break; 24195 case EACCES: 24196 break; 24197 default: 24198 rval = EIO; 24199 } 24200 24201 mutex_exit(SD_MUTEX(un)); 24202 return (rval); 24203 } 24204 24205 24206 /* 24207 * Function: sd_mhdioc_inkeys 24208 * 24209 * Description: This routine is the driver entry point for handling ioctl 24210 * requests to issue the SCSI-3 Persistent In Read Keys command 24211 * to the device (MHIOCGRP_INKEYS). 24212 * 24213 * Arguments: dev - the device number 24214 * arg - user provided in_keys structure 24215 * flag - this argument is a pass through to ddi_copyxxx() 24216 * directly from the mode argument of ioctl(). 24217 * 24218 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 24219 * ENXIO 24220 * EFAULT 24221 */ 24222 24223 static int 24224 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 24225 { 24226 struct sd_lun *un; 24227 mhioc_inkeys_t inkeys; 24228 int rval = 0; 24229 24230 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24231 return (ENXIO); 24232 } 24233 24234 #ifdef _MULTI_DATAMODEL 24235 switch (ddi_model_convert_from(flag & FMODELS)) { 24236 case DDI_MODEL_ILP32: { 24237 struct mhioc_inkeys32 inkeys32; 24238 24239 if (ddi_copyin(arg, &inkeys32, 24240 sizeof (struct mhioc_inkeys32), flag) != 0) { 24241 return (EFAULT); 24242 } 24243 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 24244 if ((rval = sd_persistent_reservation_in_read_keys(un, 24245 &inkeys, flag)) != 0) { 24246 return (rval); 24247 } 24248 inkeys32.generation = inkeys.generation; 24249 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 24250 flag) != 0) { 24251 return (EFAULT); 24252 } 24253 break; 24254 } 24255 case DDI_MODEL_NONE: 24256 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 24257 flag) != 0) { 24258 return (EFAULT); 24259 } 24260 if ((rval = sd_persistent_reservation_in_read_keys(un, 24261 &inkeys, flag)) != 0) { 24262 return (rval); 24263 } 24264 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 24265 flag) != 0) { 24266 return (EFAULT); 24267 } 24268 break; 24269 } 24270 24271 #else /* ! _MULTI_DATAMODEL */ 24272 24273 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 24274 return (EFAULT); 24275 } 24276 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 24277 if (rval != 0) { 24278 return (rval); 24279 } 24280 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 24281 return (EFAULT); 24282 } 24283 24284 #endif /* _MULTI_DATAMODEL */ 24285 24286 return (rval); 24287 } 24288 24289 24290 /* 24291 * Function: sd_mhdioc_inresv 24292 * 24293 * Description: This routine is the driver entry point for handling ioctl 24294 * requests to issue the SCSI-3 Persistent In Read Reservations 24295 * command to the device (MHIOCGRP_INKEYS). 24296 * 24297 * Arguments: dev - the device number 24298 * arg - user provided in_resv structure 24299 * flag - this argument is a pass through to ddi_copyxxx() 24300 * directly from the mode argument of ioctl(). 24301 * 24302 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 24303 * ENXIO 24304 * EFAULT 24305 */ 24306 24307 static int 24308 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 24309 { 24310 struct sd_lun *un; 24311 mhioc_inresvs_t inresvs; 24312 int rval = 0; 24313 24314 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24315 return (ENXIO); 24316 } 24317 24318 #ifdef _MULTI_DATAMODEL 24319 24320 switch (ddi_model_convert_from(flag & FMODELS)) { 24321 case DDI_MODEL_ILP32: { 24322 struct mhioc_inresvs32 inresvs32; 24323 24324 if (ddi_copyin(arg, &inresvs32, 24325 sizeof (struct mhioc_inresvs32), flag) != 0) { 24326 return (EFAULT); 24327 } 24328 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 24329 if ((rval = sd_persistent_reservation_in_read_resv(un, 24330 &inresvs, flag)) != 0) { 24331 return (rval); 24332 } 24333 inresvs32.generation = inresvs.generation; 24334 if (ddi_copyout(&inresvs32, arg, 24335 sizeof (struct mhioc_inresvs32), flag) != 0) { 24336 return (EFAULT); 24337 } 24338 break; 24339 } 24340 case DDI_MODEL_NONE: 24341 if (ddi_copyin(arg, &inresvs, 24342 sizeof (mhioc_inresvs_t), flag) != 0) { 24343 return (EFAULT); 24344 } 24345 if ((rval = sd_persistent_reservation_in_read_resv(un, 24346 &inresvs, flag)) != 0) { 24347 return (rval); 24348 } 24349 if (ddi_copyout(&inresvs, arg, 24350 sizeof (mhioc_inresvs_t), flag) != 0) { 24351 return (EFAULT); 24352 } 24353 break; 24354 } 24355 24356 #else /* ! _MULTI_DATAMODEL */ 24357 24358 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 24359 return (EFAULT); 24360 } 24361 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 24362 if (rval != 0) { 24363 return (rval); 24364 } 24365 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 24366 return (EFAULT); 24367 } 24368 24369 #endif /* ! _MULTI_DATAMODEL */ 24370 24371 return (rval); 24372 } 24373 24374 24375 /* 24376 * The following routines support the clustering functionality described below 24377 * and implement lost reservation reclaim functionality. 24378 * 24379 * Clustering 24380 * ---------- 24381 * The clustering code uses two different, independent forms of SCSI 24382 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 24383 * Persistent Group Reservations. For any particular disk, it will use either 24384 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 24385 * 24386 * SCSI-2 24387 * The cluster software takes ownership of a multi-hosted disk by issuing the 24388 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 24389 * MHIOCRELEASE ioctl.Closely related is the MHIOCENFAILFAST ioctl -- a cluster, 24390 * just after taking ownership of the disk with the MHIOCTKOWN ioctl then issues 24391 * the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the driver. The 24392 * meaning of failfast is that if the driver (on this host) ever encounters the 24393 * scsi error return code RESERVATION_CONFLICT from the device, it should 24394 * immediately panic the host. The motivation for this ioctl is that if this 24395 * host does encounter reservation conflict, the underlying cause is that some 24396 * other host of the cluster has decided that this host is no longer in the 24397 * cluster and has seized control of the disks for itself. Since this host is no 24398 * longer in the cluster, it ought to panic itself. The MHIOCENFAILFAST ioctl 24399 * does two things: 24400 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 24401 * error to panic the host 24402 * (b) it sets up a periodic timer to test whether this host still has 24403 * "access" (in that no other host has reserved the device): if the 24404 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 24405 * purpose of that periodic timer is to handle scenarios where the host is 24406 * otherwise temporarily quiescent, temporarily doing no real i/o. 24407 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 24408 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 24409 * the device itself. 24410 * 24411 * SCSI-3 PGR 24412 * A direct semantic implementation of the SCSI-3 Persistent Reservation 24413 * facility is supported through the shared multihost disk ioctls 24414 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 24415 * MHIOCGRP_PREEMPTANDABORT) 24416 * 24417 * Reservation Reclaim: 24418 * -------------------- 24419 * To support the lost reservation reclaim operations this driver creates a 24420 * single thread to handle reinstating reservations on all devices that have 24421 * lost reservations sd_resv_reclaim_requests are logged for all devices that 24422 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 24423 * and the reservation reclaim thread loops through the requests to regain the 24424 * lost reservations. 24425 */ 24426 24427 /* 24428 * Function: sd_check_mhd() 24429 * 24430 * Description: This function sets up and submits a scsi watch request or 24431 * terminates an existing watch request. This routine is used in 24432 * support of reservation reclaim. 24433 * 24434 * Arguments: dev - the device 'dev_t' is used for context to discriminate 24435 * among multiple watches that share the callback function 24436 * interval - the number of microseconds specifying the watch 24437 * interval for issuing TEST UNIT READY commands. If 24438 * set to 0 the watch should be terminated. If the 24439 * interval is set to 0 and if the device is required 24440 * to hold reservation while disabling failfast, the 24441 * watch is restarted with an interval of 24442 * reinstate_resv_delay. 24443 * 24444 * Return Code: 0 - Successful submit/terminate of scsi watch request 24445 * ENXIO - Indicates an invalid device was specified 24446 * EAGAIN - Unable to submit the scsi watch request 24447 */ 24448 24449 static int 24450 sd_check_mhd(dev_t dev, int interval) 24451 { 24452 struct sd_lun *un; 24453 opaque_t token; 24454 24455 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24456 return (ENXIO); 24457 } 24458 24459 /* is this a watch termination request? */ 24460 if (interval == 0) { 24461 mutex_enter(SD_MUTEX(un)); 24462 /* if there is an existing watch task then terminate it */ 24463 if (un->un_mhd_token) { 24464 token = un->un_mhd_token; 24465 un->un_mhd_token = NULL; 24466 mutex_exit(SD_MUTEX(un)); 24467 (void) scsi_watch_request_terminate(token, 24468 SCSI_WATCH_TERMINATE_WAIT); 24469 mutex_enter(SD_MUTEX(un)); 24470 } else { 24471 mutex_exit(SD_MUTEX(un)); 24472 /* 24473 * Note: If we return here we don't check for the 24474 * failfast case. This is the original legacy 24475 * implementation but perhaps we should be checking 24476 * the failfast case. 24477 */ 24478 return (0); 24479 } 24480 /* 24481 * If the device is required to hold reservation while 24482 * disabling failfast, we need to restart the scsi_watch 24483 * routine with an interval of reinstate_resv_delay. 24484 */ 24485 if (un->un_resvd_status & SD_RESERVE) { 24486 interval = sd_reinstate_resv_delay/1000; 24487 } else { 24488 /* no failfast so bail */ 24489 mutex_exit(SD_MUTEX(un)); 24490 return (0); 24491 } 24492 mutex_exit(SD_MUTEX(un)); 24493 } 24494 24495 /* 24496 * adjust minimum time interval to 1 second, 24497 * and convert from msecs to usecs 24498 */ 24499 if (interval > 0 && interval < 1000) { 24500 interval = 1000; 24501 } 24502 interval *= 1000; 24503 24504 /* 24505 * submit the request to the scsi_watch service 24506 */ 24507 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 24508 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 24509 if (token == NULL) { 24510 return (EAGAIN); 24511 } 24512 24513 /* 24514 * save token for termination later on 24515 */ 24516 mutex_enter(SD_MUTEX(un)); 24517 un->un_mhd_token = token; 24518 mutex_exit(SD_MUTEX(un)); 24519 return (0); 24520 } 24521 24522 24523 /* 24524 * Function: sd_mhd_watch_cb() 24525 * 24526 * Description: This function is the call back function used by the scsi watch 24527 * facility. The scsi watch facility sends the "Test Unit Ready" 24528 * and processes the status. If applicable (i.e. a "Unit Attention" 24529 * status and automatic "Request Sense" not used) the scsi watch 24530 * facility will send a "Request Sense" and retrieve the sense data 24531 * to be passed to this callback function. In either case the 24532 * automatic "Request Sense" or the facility submitting one, this 24533 * callback is passed the status and sense data. 24534 * 24535 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24536 * among multiple watches that share this callback function 24537 * resultp - scsi watch facility result packet containing scsi 24538 * packet, status byte and sense data 24539 * 24540 * Return Code: 0 - continue the watch task 24541 * non-zero - terminate the watch task 24542 */ 24543 24544 static int 24545 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 24546 { 24547 struct sd_lun *un; 24548 struct scsi_status *statusp; 24549 struct scsi_extended_sense *sensep; 24550 struct scsi_pkt *pkt; 24551 uchar_t actual_sense_length; 24552 dev_t dev = (dev_t)arg; 24553 24554 ASSERT(resultp != NULL); 24555 statusp = resultp->statusp; 24556 sensep = resultp->sensep; 24557 pkt = resultp->pkt; 24558 actual_sense_length = resultp->actual_sense_length; 24559 24560 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24561 return (ENXIO); 24562 } 24563 24564 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24565 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 24566 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 24567 24568 /* Begin processing of the status and/or sense data */ 24569 if (pkt->pkt_reason != CMD_CMPLT) { 24570 /* Handle the incomplete packet */ 24571 sd_mhd_watch_incomplete(un, pkt); 24572 return (0); 24573 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 24574 if (*((unsigned char *)statusp) 24575 == STATUS_RESERVATION_CONFLICT) { 24576 /* 24577 * Handle a reservation conflict by panicking if 24578 * configured for failfast or by logging the conflict 24579 * and updating the reservation status 24580 */ 24581 mutex_enter(SD_MUTEX(un)); 24582 if ((un->un_resvd_status & SD_FAILFAST) && 24583 (sd_failfast_enable)) { 24584 panic("Reservation Conflict"); 24585 /*NOTREACHED*/ 24586 } 24587 SD_INFO(SD_LOG_IOCTL_MHD, un, 24588 "sd_mhd_watch_cb: Reservation Conflict\n"); 24589 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 24590 mutex_exit(SD_MUTEX(un)); 24591 } 24592 } 24593 24594 if (sensep != NULL) { 24595 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 24596 mutex_enter(SD_MUTEX(un)); 24597 if ((sensep->es_add_code == SD_SCSI_RESET_SENSE_CODE) && 24598 (un->un_resvd_status & SD_RESERVE)) { 24599 /* 24600 * The additional sense code indicates a power 24601 * on or bus device reset has occurred; update 24602 * the reservation status. 24603 */ 24604 un->un_resvd_status |= 24605 (SD_LOST_RESERVE | SD_WANT_RESERVE); 24606 SD_INFO(SD_LOG_IOCTL_MHD, un, 24607 "sd_mhd_watch_cb: Lost Reservation\n"); 24608 } 24609 } else { 24610 return (0); 24611 } 24612 } else { 24613 mutex_enter(SD_MUTEX(un)); 24614 } 24615 24616 if ((un->un_resvd_status & SD_RESERVE) && 24617 (un->un_resvd_status & SD_LOST_RESERVE)) { 24618 if (un->un_resvd_status & SD_WANT_RESERVE) { 24619 /* 24620 * A reset occurred in between the last probe and this 24621 * one so if a timeout is pending cancel it. 24622 */ 24623 if (un->un_resvd_timeid) { 24624 timeout_id_t temp_id = un->un_resvd_timeid; 24625 un->un_resvd_timeid = NULL; 24626 mutex_exit(SD_MUTEX(un)); 24627 (void) untimeout(temp_id); 24628 mutex_enter(SD_MUTEX(un)); 24629 } 24630 un->un_resvd_status &= ~SD_WANT_RESERVE; 24631 } 24632 if (un->un_resvd_timeid == 0) { 24633 /* Schedule a timeout to handle the lost reservation */ 24634 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 24635 (void *)dev, 24636 drv_usectohz(sd_reinstate_resv_delay)); 24637 } 24638 } 24639 mutex_exit(SD_MUTEX(un)); 24640 return (0); 24641 } 24642 24643 24644 /* 24645 * Function: sd_mhd_watch_incomplete() 24646 * 24647 * Description: This function is used to find out why a scsi pkt sent by the 24648 * scsi watch facility was not completed. Under some scenarios this 24649 * routine will return. Otherwise it will send a bus reset to see 24650 * if the drive is still online. 24651 * 24652 * Arguments: un - driver soft state (unit) structure 24653 * pkt - incomplete scsi pkt 24654 */ 24655 24656 static void 24657 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 24658 { 24659 int be_chatty; 24660 int perr; 24661 24662 ASSERT(pkt != NULL); 24663 ASSERT(un != NULL); 24664 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 24665 perr = (pkt->pkt_statistics & STAT_PERR); 24666 24667 mutex_enter(SD_MUTEX(un)); 24668 if (un->un_state == SD_STATE_DUMPING) { 24669 mutex_exit(SD_MUTEX(un)); 24670 return; 24671 } 24672 24673 switch (pkt->pkt_reason) { 24674 case CMD_UNX_BUS_FREE: 24675 /* 24676 * If we had a parity error that caused the target to drop BSY*, 24677 * don't be chatty about it. 24678 */ 24679 if (perr && be_chatty) { 24680 be_chatty = 0; 24681 } 24682 break; 24683 case CMD_TAG_REJECT: 24684 /* 24685 * The SCSI-2 spec states that a tag reject will be sent by the 24686 * target if tagged queuing is not supported. A tag reject may 24687 * also be sent during certain initialization periods or to 24688 * control internal resources. For the latter case the target 24689 * may also return Queue Full. 24690 * 24691 * If this driver receives a tag reject from a target that is 24692 * going through an init period or controlling internal 24693 * resources tagged queuing will be disabled. This is a less 24694 * than optimal behavior but the driver is unable to determine 24695 * the target state and assumes tagged queueing is not supported 24696 */ 24697 pkt->pkt_flags = 0; 24698 un->un_tagflags = 0; 24699 24700 if (un->un_f_opt_queueing == TRUE) { 24701 un->un_throttle = min(un->un_throttle, 3); 24702 } else { 24703 un->un_throttle = 1; 24704 } 24705 mutex_exit(SD_MUTEX(un)); 24706 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 24707 mutex_enter(SD_MUTEX(un)); 24708 break; 24709 case CMD_INCOMPLETE: 24710 /* 24711 * The transport stopped with an abnormal state, fallthrough and 24712 * reset the target and/or bus unless selection did not complete 24713 * (indicated by STATE_GOT_BUS) in which case we don't want to 24714 * go through a target/bus reset 24715 */ 24716 if (pkt->pkt_state == STATE_GOT_BUS) { 24717 break; 24718 } 24719 /*FALLTHROUGH*/ 24720 24721 case CMD_TIMEOUT: 24722 default: 24723 /* 24724 * The lun may still be running the command, so a lun reset 24725 * should be attempted. If the lun reset fails or cannot be 24726 * issued, than try a target reset. Lastly try a bus reset. 24727 */ 24728 if ((pkt->pkt_statistics & 24729 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 24730 int reset_retval = 0; 24731 mutex_exit(SD_MUTEX(un)); 24732 if (un->un_f_allow_bus_device_reset == TRUE) { 24733 if (un->un_f_lun_reset_enabled == TRUE) { 24734 reset_retval = 24735 scsi_reset(SD_ADDRESS(un), 24736 RESET_LUN); 24737 } 24738 if (reset_retval == 0) { 24739 reset_retval = 24740 scsi_reset(SD_ADDRESS(un), 24741 RESET_TARGET); 24742 } 24743 } 24744 if (reset_retval == 0) { 24745 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 24746 } 24747 mutex_enter(SD_MUTEX(un)); 24748 } 24749 break; 24750 } 24751 24752 /* A device/bus reset has occurred; update the reservation status. */ 24753 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 24754 (STAT_BUS_RESET | STAT_DEV_RESET))) { 24755 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 24756 un->un_resvd_status |= 24757 (SD_LOST_RESERVE | SD_WANT_RESERVE); 24758 SD_INFO(SD_LOG_IOCTL_MHD, un, 24759 "sd_mhd_watch_incomplete: Lost Reservation\n"); 24760 } 24761 } 24762 24763 /* 24764 * The disk has been turned off; Update the device state. 24765 * 24766 * Note: Should we be offlining the disk here? 24767 */ 24768 if (pkt->pkt_state == STATE_GOT_BUS) { 24769 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 24770 "Disk not responding to selection\n"); 24771 if (un->un_state != SD_STATE_OFFLINE) { 24772 New_state(un, SD_STATE_OFFLINE); 24773 } 24774 } else if (be_chatty) { 24775 /* 24776 * suppress messages if they are all the same pkt reason; 24777 * with TQ, many (up to 256) are returned with the same 24778 * pkt_reason 24779 */ 24780 if (pkt->pkt_reason != un->un_last_pkt_reason) { 24781 SD_ERROR(SD_LOG_IOCTL_MHD, un, 24782 "sd_mhd_watch_incomplete: " 24783 "SCSI transport failed: reason '%s'\n", 24784 scsi_rname(pkt->pkt_reason)); 24785 } 24786 } 24787 un->un_last_pkt_reason = pkt->pkt_reason; 24788 mutex_exit(SD_MUTEX(un)); 24789 } 24790 24791 24792 /* 24793 * Function: sd_sname() 24794 * 24795 * Description: This is a simple little routine to return a string containing 24796 * a printable description of command status byte for use in 24797 * logging. 24798 * 24799 * Arguments: status - pointer to a status byte 24800 * 24801 * Return Code: char * - string containing status description. 24802 */ 24803 24804 static char * 24805 sd_sname(uchar_t status) 24806 { 24807 switch (status & STATUS_MASK) { 24808 case STATUS_GOOD: 24809 return ("good status"); 24810 case STATUS_CHECK: 24811 return ("check condition"); 24812 case STATUS_MET: 24813 return ("condition met"); 24814 case STATUS_BUSY: 24815 return ("busy"); 24816 case STATUS_INTERMEDIATE: 24817 return ("intermediate"); 24818 case STATUS_INTERMEDIATE_MET: 24819 return ("intermediate - condition met"); 24820 case STATUS_RESERVATION_CONFLICT: 24821 return ("reservation_conflict"); 24822 case STATUS_TERMINATED: 24823 return ("command terminated"); 24824 case STATUS_QFULL: 24825 return ("queue full"); 24826 default: 24827 return ("<unknown status>"); 24828 } 24829 } 24830 24831 24832 /* 24833 * Function: sd_mhd_resvd_recover() 24834 * 24835 * Description: This function adds a reservation entry to the 24836 * sd_resv_reclaim_request list and signals the reservation 24837 * reclaim thread that there is work pending. If the reservation 24838 * reclaim thread has not been previously created this function 24839 * will kick it off. 24840 * 24841 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24842 * among multiple watches that share this callback function 24843 * 24844 * Context: This routine is called by timeout() and is run in interrupt 24845 * context. It must not sleep or call other functions which may 24846 * sleep. 24847 */ 24848 24849 static void 24850 sd_mhd_resvd_recover(void *arg) 24851 { 24852 dev_t dev = (dev_t)arg; 24853 struct sd_lun *un; 24854 struct sd_thr_request *sd_treq = NULL; 24855 struct sd_thr_request *sd_cur = NULL; 24856 struct sd_thr_request *sd_prev = NULL; 24857 int already_there = 0; 24858 24859 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24860 return; 24861 } 24862 24863 mutex_enter(SD_MUTEX(un)); 24864 un->un_resvd_timeid = NULL; 24865 if (un->un_resvd_status & SD_WANT_RESERVE) { 24866 /* 24867 * There was a reset so don't issue the reserve, allow the 24868 * sd_mhd_watch_cb callback function to notice this and 24869 * reschedule the timeout for reservation. 24870 */ 24871 mutex_exit(SD_MUTEX(un)); 24872 return; 24873 } 24874 mutex_exit(SD_MUTEX(un)); 24875 24876 /* 24877 * Add this device to the sd_resv_reclaim_request list and the 24878 * sd_resv_reclaim_thread should take care of the rest. 24879 * 24880 * Note: We can't sleep in this context so if the memory allocation 24881 * fails allow the sd_mhd_watch_cb callback function to notice this and 24882 * reschedule the timeout for reservation. (4378460) 24883 */ 24884 sd_treq = (struct sd_thr_request *) 24885 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 24886 if (sd_treq == NULL) { 24887 return; 24888 } 24889 24890 sd_treq->sd_thr_req_next = NULL; 24891 sd_treq->dev = dev; 24892 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 24893 if (sd_tr.srq_thr_req_head == NULL) { 24894 sd_tr.srq_thr_req_head = sd_treq; 24895 } else { 24896 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 24897 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 24898 if (sd_cur->dev == dev) { 24899 /* 24900 * already in Queue so don't log 24901 * another request for the device 24902 */ 24903 already_there = 1; 24904 break; 24905 } 24906 sd_prev = sd_cur; 24907 } 24908 if (!already_there) { 24909 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 24910 "logging request for %lx\n", dev); 24911 sd_prev->sd_thr_req_next = sd_treq; 24912 } else { 24913 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 24914 } 24915 } 24916 24917 /* 24918 * Create a kernel thread to do the reservation reclaim and free up this 24919 * thread. We cannot block this thread while we go away to do the 24920 * reservation reclaim 24921 */ 24922 if (sd_tr.srq_resv_reclaim_thread == NULL) 24923 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 24924 sd_resv_reclaim_thread, NULL, 24925 0, &p0, TS_RUN, v.v_maxsyspri - 2); 24926 24927 /* Tell the reservation reclaim thread that it has work to do */ 24928 cv_signal(&sd_tr.srq_resv_reclaim_cv); 24929 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 24930 } 24931 24932 /* 24933 * Function: sd_resv_reclaim_thread() 24934 * 24935 * Description: This function implements the reservation reclaim operations 24936 * 24937 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24938 * among multiple watches that share this callback function 24939 */ 24940 24941 static void 24942 sd_resv_reclaim_thread() 24943 { 24944 struct sd_lun *un; 24945 struct sd_thr_request *sd_mhreq; 24946 24947 /* Wait for work */ 24948 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 24949 if (sd_tr.srq_thr_req_head == NULL) { 24950 cv_wait(&sd_tr.srq_resv_reclaim_cv, 24951 &sd_tr.srq_resv_reclaim_mutex); 24952 } 24953 24954 /* Loop while we have work */ 24955 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 24956 un = ddi_get_soft_state(sd_state, 24957 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 24958 if (un == NULL) { 24959 /* 24960 * softstate structure is NULL so just 24961 * dequeue the request and continue 24962 */ 24963 sd_tr.srq_thr_req_head = 24964 sd_tr.srq_thr_cur_req->sd_thr_req_next; 24965 kmem_free(sd_tr.srq_thr_cur_req, 24966 sizeof (struct sd_thr_request)); 24967 continue; 24968 } 24969 24970 /* dequeue the request */ 24971 sd_mhreq = sd_tr.srq_thr_cur_req; 24972 sd_tr.srq_thr_req_head = 24973 sd_tr.srq_thr_cur_req->sd_thr_req_next; 24974 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 24975 24976 /* 24977 * Reclaim reservation only if SD_RESERVE is still set. There 24978 * may have been a call to MHIOCRELEASE before we got here. 24979 */ 24980 mutex_enter(SD_MUTEX(un)); 24981 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 24982 /* 24983 * Note: The SD_LOST_RESERVE flag is cleared before 24984 * reclaiming the reservation. If this is done after the 24985 * call to sd_reserve_release a reservation loss in the 24986 * window between pkt completion of reserve cmd and 24987 * mutex_enter below may not be recognized 24988 */ 24989 un->un_resvd_status &= ~SD_LOST_RESERVE; 24990 mutex_exit(SD_MUTEX(un)); 24991 24992 if (sd_reserve_release(sd_mhreq->dev, 24993 SD_RESERVE) == 0) { 24994 mutex_enter(SD_MUTEX(un)); 24995 un->un_resvd_status |= SD_RESERVE; 24996 mutex_exit(SD_MUTEX(un)); 24997 SD_INFO(SD_LOG_IOCTL_MHD, un, 24998 "sd_resv_reclaim_thread: " 24999 "Reservation Recovered\n"); 25000 } else { 25001 mutex_enter(SD_MUTEX(un)); 25002 un->un_resvd_status |= SD_LOST_RESERVE; 25003 mutex_exit(SD_MUTEX(un)); 25004 SD_INFO(SD_LOG_IOCTL_MHD, un, 25005 "sd_resv_reclaim_thread: Failed " 25006 "Reservation Recovery\n"); 25007 } 25008 } else { 25009 mutex_exit(SD_MUTEX(un)); 25010 } 25011 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25012 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 25013 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25014 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 25015 /* 25016 * wakeup the destroy thread if anyone is waiting on 25017 * us to complete. 25018 */ 25019 cv_signal(&sd_tr.srq_inprocess_cv); 25020 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25021 "sd_resv_reclaim_thread: cv_signalling current request \n"); 25022 } 25023 25024 /* 25025 * cleanup the sd_tr structure now that this thread will not exist 25026 */ 25027 ASSERT(sd_tr.srq_thr_req_head == NULL); 25028 ASSERT(sd_tr.srq_thr_cur_req == NULL); 25029 sd_tr.srq_resv_reclaim_thread = NULL; 25030 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25031 thread_exit(); 25032 } 25033 25034 25035 /* 25036 * Function: sd_rmv_resv_reclaim_req() 25037 * 25038 * Description: This function removes any pending reservation reclaim requests 25039 * for the specified device. 25040 * 25041 * Arguments: dev - the device 'dev_t' 25042 */ 25043 25044 static void 25045 sd_rmv_resv_reclaim_req(dev_t dev) 25046 { 25047 struct sd_thr_request *sd_mhreq; 25048 struct sd_thr_request *sd_prev; 25049 25050 /* Remove a reservation reclaim request from the list */ 25051 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25052 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 25053 /* 25054 * We are attempting to reinstate reservation for 25055 * this device. We wait for sd_reserve_release() 25056 * to return before we return. 25057 */ 25058 cv_wait(&sd_tr.srq_inprocess_cv, 25059 &sd_tr.srq_resv_reclaim_mutex); 25060 } else { 25061 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 25062 if (sd_mhreq && sd_mhreq->dev == dev) { 25063 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 25064 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25065 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25066 return; 25067 } 25068 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 25069 if (sd_mhreq && sd_mhreq->dev == dev) { 25070 break; 25071 } 25072 sd_prev = sd_mhreq; 25073 } 25074 if (sd_mhreq != NULL) { 25075 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 25076 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25077 } 25078 } 25079 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25080 } 25081 25082 25083 /* 25084 * Function: sd_mhd_reset_notify_cb() 25085 * 25086 * Description: This is a call back function for scsi_reset_notify. This 25087 * function updates the softstate reserved status and logs the 25088 * reset. The driver scsi watch facility callback function 25089 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 25090 * will reclaim the reservation. 25091 * 25092 * Arguments: arg - driver soft state (unit) structure 25093 */ 25094 25095 static void 25096 sd_mhd_reset_notify_cb(caddr_t arg) 25097 { 25098 struct sd_lun *un = (struct sd_lun *)arg; 25099 25100 mutex_enter(SD_MUTEX(un)); 25101 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25102 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 25103 SD_INFO(SD_LOG_IOCTL_MHD, un, 25104 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 25105 } 25106 mutex_exit(SD_MUTEX(un)); 25107 } 25108 25109 25110 /* 25111 * Function: sd_take_ownership() 25112 * 25113 * Description: This routine implements an algorithm to achieve a stable 25114 * reservation on disks which don't implement priority reserve, 25115 * and makes sure that other host lose re-reservation attempts. 25116 * This algorithm contains of a loop that keeps issuing the RESERVE 25117 * for some period of time (min_ownership_delay, default 6 seconds) 25118 * During that loop, it looks to see if there has been a bus device 25119 * reset or bus reset (both of which cause an existing reservation 25120 * to be lost). If the reservation is lost issue RESERVE until a 25121 * period of min_ownership_delay with no resets has gone by, or 25122 * until max_ownership_delay has expired. This loop ensures that 25123 * the host really did manage to reserve the device, in spite of 25124 * resets. The looping for min_ownership_delay (default six 25125 * seconds) is important to early generation clustering products, 25126 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 25127 * MHIOCENFAILFAST periodic timer of two seconds. By having 25128 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 25129 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 25130 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 25131 * have already noticed, via the MHIOCENFAILFAST polling, that it 25132 * no longer "owns" the disk and will have panicked itself. Thus, 25133 * the host issuing the MHIOCTKOWN is assured (with timing 25134 * dependencies) that by the time it actually starts to use the 25135 * disk for real work, the old owner is no longer accessing it. 25136 * 25137 * min_ownership_delay is the minimum amount of time for which the 25138 * disk must be reserved continuously devoid of resets before the 25139 * MHIOCTKOWN ioctl will return success. 25140 * 25141 * max_ownership_delay indicates the amount of time by which the 25142 * take ownership should succeed or timeout with an error. 25143 * 25144 * Arguments: dev - the device 'dev_t' 25145 * *p - struct containing timing info. 25146 * 25147 * Return Code: 0 for success or error code 25148 */ 25149 25150 static int 25151 sd_take_ownership(dev_t dev, struct mhioctkown *p) 25152 { 25153 struct sd_lun *un; 25154 int rval; 25155 int err; 25156 int reservation_count = 0; 25157 int min_ownership_delay = 6000000; /* in usec */ 25158 int max_ownership_delay = 30000000; /* in usec */ 25159 clock_t start_time; /* starting time of this algorithm */ 25160 clock_t end_time; /* time limit for giving up */ 25161 clock_t ownership_time; /* time limit for stable ownership */ 25162 clock_t current_time; 25163 clock_t previous_current_time; 25164 25165 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25166 return (ENXIO); 25167 } 25168 25169 /* 25170 * Attempt a device reservation. A priority reservation is requested. 25171 */ 25172 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 25173 != SD_SUCCESS) { 25174 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25175 "sd_take_ownership: return(1)=%d\n", rval); 25176 return (rval); 25177 } 25178 25179 /* Update the softstate reserved status to indicate the reservation */ 25180 mutex_enter(SD_MUTEX(un)); 25181 un->un_resvd_status |= SD_RESERVE; 25182 un->un_resvd_status &= 25183 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 25184 mutex_exit(SD_MUTEX(un)); 25185 25186 if (p != NULL) { 25187 if (p->min_ownership_delay != 0) { 25188 min_ownership_delay = p->min_ownership_delay * 1000; 25189 } 25190 if (p->max_ownership_delay != 0) { 25191 max_ownership_delay = p->max_ownership_delay * 1000; 25192 } 25193 } 25194 SD_INFO(SD_LOG_IOCTL_MHD, un, 25195 "sd_take_ownership: min, max delays: %d, %d\n", 25196 min_ownership_delay, max_ownership_delay); 25197 25198 start_time = ddi_get_lbolt(); 25199 current_time = start_time; 25200 ownership_time = current_time + drv_usectohz(min_ownership_delay); 25201 end_time = start_time + drv_usectohz(max_ownership_delay); 25202 25203 while (current_time - end_time < 0) { 25204 delay(drv_usectohz(500000)); 25205 25206 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 25207 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 25208 mutex_enter(SD_MUTEX(un)); 25209 rval = (un->un_resvd_status & 25210 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 25211 mutex_exit(SD_MUTEX(un)); 25212 break; 25213 } 25214 } 25215 previous_current_time = current_time; 25216 current_time = ddi_get_lbolt(); 25217 mutex_enter(SD_MUTEX(un)); 25218 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 25219 ownership_time = ddi_get_lbolt() + 25220 drv_usectohz(min_ownership_delay); 25221 reservation_count = 0; 25222 } else { 25223 reservation_count++; 25224 } 25225 un->un_resvd_status |= SD_RESERVE; 25226 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 25227 mutex_exit(SD_MUTEX(un)); 25228 25229 SD_INFO(SD_LOG_IOCTL_MHD, un, 25230 "sd_take_ownership: ticks for loop iteration=%ld, " 25231 "reservation=%s\n", (current_time - previous_current_time), 25232 reservation_count ? "ok" : "reclaimed"); 25233 25234 if (current_time - ownership_time >= 0 && 25235 reservation_count >= 4) { 25236 rval = 0; /* Achieved a stable ownership */ 25237 break; 25238 } 25239 if (current_time - end_time >= 0) { 25240 rval = EACCES; /* No ownership in max possible time */ 25241 break; 25242 } 25243 } 25244 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25245 "sd_take_ownership: return(2)=%d\n", rval); 25246 return (rval); 25247 } 25248 25249 25250 /* 25251 * Function: sd_reserve_release() 25252 * 25253 * Description: This function builds and sends scsi RESERVE, RELEASE, and 25254 * PRIORITY RESERVE commands based on a user specified command type 25255 * 25256 * Arguments: dev - the device 'dev_t' 25257 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 25258 * SD_RESERVE, SD_RELEASE 25259 * 25260 * Return Code: 0 or Error Code 25261 */ 25262 25263 static int 25264 sd_reserve_release(dev_t dev, int cmd) 25265 { 25266 struct uscsi_cmd *com = NULL; 25267 struct sd_lun *un = NULL; 25268 char cdb[CDB_GROUP0]; 25269 int rval; 25270 25271 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 25272 (cmd == SD_PRIORITY_RESERVE)); 25273 25274 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25275 return (ENXIO); 25276 } 25277 25278 /* instantiate and initialize the command and cdb */ 25279 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25280 bzero(cdb, CDB_GROUP0); 25281 com->uscsi_flags = USCSI_SILENT; 25282 com->uscsi_timeout = un->un_reserve_release_time; 25283 com->uscsi_cdblen = CDB_GROUP0; 25284 com->uscsi_cdb = cdb; 25285 if (cmd == SD_RELEASE) { 25286 cdb[0] = SCMD_RELEASE; 25287 } else { 25288 cdb[0] = SCMD_RESERVE; 25289 } 25290 25291 /* Send the command. */ 25292 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 25293 UIO_SYSSPACE, SD_PATH_STANDARD); 25294 25295 /* 25296 * "break" a reservation that is held by another host, by issuing a 25297 * reset if priority reserve is desired, and we could not get the 25298 * device. 25299 */ 25300 if ((cmd == SD_PRIORITY_RESERVE) && 25301 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 25302 /* 25303 * First try to reset the LUN. If we cannot, then try a target 25304 * reset, followed by a bus reset if the target reset fails. 25305 */ 25306 int reset_retval = 0; 25307 if (un->un_f_lun_reset_enabled == TRUE) { 25308 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 25309 } 25310 if (reset_retval == 0) { 25311 /* The LUN reset either failed or was not issued */ 25312 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 25313 } 25314 if ((reset_retval == 0) && 25315 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 25316 rval = EIO; 25317 kmem_free(com, sizeof (*com)); 25318 return (rval); 25319 } 25320 25321 bzero(com, sizeof (struct uscsi_cmd)); 25322 com->uscsi_flags = USCSI_SILENT; 25323 com->uscsi_cdb = cdb; 25324 com->uscsi_cdblen = CDB_GROUP0; 25325 com->uscsi_timeout = 5; 25326 25327 /* 25328 * Reissue the last reserve command, this time without request 25329 * sense. Assume that it is just a regular reserve command. 25330 */ 25331 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 25332 UIO_SYSSPACE, SD_PATH_STANDARD); 25333 } 25334 25335 /* Return an error if still getting a reservation conflict. */ 25336 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 25337 rval = EACCES; 25338 } 25339 25340 kmem_free(com, sizeof (*com)); 25341 return (rval); 25342 } 25343 25344 25345 #define SD_NDUMP_RETRIES 12 25346 /* 25347 * System Crash Dump routine 25348 */ 25349 25350 static int 25351 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 25352 { 25353 int instance; 25354 int partition; 25355 int i; 25356 int err; 25357 struct sd_lun *un; 25358 struct dk_map *lp; 25359 struct scsi_pkt *wr_pktp; 25360 struct buf *wr_bp; 25361 struct buf wr_buf; 25362 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 25363 daddr_t tgt_blkno; /* rmw - blkno for target */ 25364 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 25365 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 25366 size_t io_start_offset; 25367 int doing_rmw = FALSE; 25368 int rval; 25369 #if defined(__i386) || defined(__amd64) 25370 ssize_t dma_resid; 25371 daddr_t oblkno; 25372 #endif 25373 25374 instance = SDUNIT(dev); 25375 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 25376 (!un->un_f_geometry_is_valid) || ISCD(un)) { 25377 return (ENXIO); 25378 } 25379 25380 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 25381 25382 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 25383 25384 partition = SDPART(dev); 25385 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 25386 25387 /* Validate blocks to dump at against partition size. */ 25388 lp = &un->un_map[partition]; 25389 if ((blkno + nblk) > lp->dkl_nblk) { 25390 SD_TRACE(SD_LOG_DUMP, un, 25391 "sddump: dump range larger than partition: " 25392 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 25393 blkno, nblk, lp->dkl_nblk); 25394 return (EINVAL); 25395 } 25396 25397 mutex_enter(&un->un_pm_mutex); 25398 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 25399 struct scsi_pkt *start_pktp; 25400 25401 mutex_exit(&un->un_pm_mutex); 25402 25403 /* 25404 * use pm framework to power on HBA 1st 25405 */ 25406 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 25407 25408 /* 25409 * Dump no long uses sdpower to power on a device, it's 25410 * in-line here so it can be done in polled mode. 25411 */ 25412 25413 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 25414 25415 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 25416 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 25417 25418 if (start_pktp == NULL) { 25419 /* We were not given a SCSI packet, fail. */ 25420 return (EIO); 25421 } 25422 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 25423 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 25424 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 25425 start_pktp->pkt_flags = FLAG_NOINTR; 25426 25427 mutex_enter(SD_MUTEX(un)); 25428 SD_FILL_SCSI1_LUN(un, start_pktp); 25429 mutex_exit(SD_MUTEX(un)); 25430 /* 25431 * Scsi_poll returns 0 (success) if the command completes and 25432 * the status block is STATUS_GOOD. 25433 */ 25434 if (sd_scsi_poll(un, start_pktp) != 0) { 25435 scsi_destroy_pkt(start_pktp); 25436 return (EIO); 25437 } 25438 scsi_destroy_pkt(start_pktp); 25439 (void) sd_ddi_pm_resume(un); 25440 } else { 25441 mutex_exit(&un->un_pm_mutex); 25442 } 25443 25444 mutex_enter(SD_MUTEX(un)); 25445 un->un_throttle = 0; 25446 25447 /* 25448 * The first time through, reset the specific target device. 25449 * However, when cpr calls sddump we know that sd is in a 25450 * a good state so no bus reset is required. 25451 * Clear sense data via Request Sense cmd. 25452 * In sddump we don't care about allow_bus_device_reset anymore 25453 */ 25454 25455 if ((un->un_state != SD_STATE_SUSPENDED) && 25456 (un->un_state != SD_STATE_DUMPING)) { 25457 25458 New_state(un, SD_STATE_DUMPING); 25459 25460 if (un->un_f_is_fibre == FALSE) { 25461 mutex_exit(SD_MUTEX(un)); 25462 /* 25463 * Attempt a bus reset for parallel scsi. 25464 * 25465 * Note: A bus reset is required because on some host 25466 * systems (i.e. E420R) a bus device reset is 25467 * insufficient to reset the state of the target. 25468 * 25469 * Note: Don't issue the reset for fibre-channel, 25470 * because this tends to hang the bus (loop) for 25471 * too long while everyone is logging out and in 25472 * and the deadman timer for dumping will fire 25473 * before the dump is complete. 25474 */ 25475 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 25476 mutex_enter(SD_MUTEX(un)); 25477 Restore_state(un); 25478 mutex_exit(SD_MUTEX(un)); 25479 return (EIO); 25480 } 25481 25482 /* Delay to give the device some recovery time. */ 25483 drv_usecwait(10000); 25484 25485 if (sd_send_polled_RQS(un) == SD_FAILURE) { 25486 SD_INFO(SD_LOG_DUMP, un, 25487 "sddump: sd_send_polled_RQS failed\n"); 25488 } 25489 mutex_enter(SD_MUTEX(un)); 25490 } 25491 } 25492 25493 /* 25494 * Convert the partition-relative block number to a 25495 * disk physical block number. 25496 */ 25497 blkno += un->un_offset[partition]; 25498 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 25499 25500 25501 /* 25502 * Check if the device has a non-512 block size. 25503 */ 25504 wr_bp = NULL; 25505 if (NOT_DEVBSIZE(un)) { 25506 tgt_byte_offset = blkno * un->un_sys_blocksize; 25507 tgt_byte_count = nblk * un->un_sys_blocksize; 25508 if ((tgt_byte_offset % un->un_tgt_blocksize) || 25509 (tgt_byte_count % un->un_tgt_blocksize)) { 25510 doing_rmw = TRUE; 25511 /* 25512 * Calculate the block number and number of block 25513 * in terms of the media block size. 25514 */ 25515 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 25516 tgt_nblk = 25517 ((tgt_byte_offset + tgt_byte_count + 25518 (un->un_tgt_blocksize - 1)) / 25519 un->un_tgt_blocksize) - tgt_blkno; 25520 25521 /* 25522 * Invoke the routine which is going to do read part 25523 * of read-modify-write. 25524 * Note that this routine returns a pointer to 25525 * a valid bp in wr_bp. 25526 */ 25527 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 25528 &wr_bp); 25529 if (err) { 25530 mutex_exit(SD_MUTEX(un)); 25531 return (err); 25532 } 25533 /* 25534 * Offset is being calculated as - 25535 * (original block # * system block size) - 25536 * (new block # * target block size) 25537 */ 25538 io_start_offset = 25539 ((uint64_t)(blkno * un->un_sys_blocksize)) - 25540 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 25541 25542 ASSERT((io_start_offset >= 0) && 25543 (io_start_offset < un->un_tgt_blocksize)); 25544 /* 25545 * Do the modify portion of read modify write. 25546 */ 25547 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 25548 (size_t)nblk * un->un_sys_blocksize); 25549 } else { 25550 doing_rmw = FALSE; 25551 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 25552 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 25553 } 25554 25555 /* Convert blkno and nblk to target blocks */ 25556 blkno = tgt_blkno; 25557 nblk = tgt_nblk; 25558 } else { 25559 wr_bp = &wr_buf; 25560 bzero(wr_bp, sizeof (struct buf)); 25561 wr_bp->b_flags = B_BUSY; 25562 wr_bp->b_un.b_addr = addr; 25563 wr_bp->b_bcount = nblk << DEV_BSHIFT; 25564 wr_bp->b_resid = 0; 25565 } 25566 25567 mutex_exit(SD_MUTEX(un)); 25568 25569 /* 25570 * Obtain a SCSI packet for the write command. 25571 * It should be safe to call the allocator here without 25572 * worrying about being locked for DVMA mapping because 25573 * the address we're passed is already a DVMA mapping 25574 * 25575 * We are also not going to worry about semaphore ownership 25576 * in the dump buffer. Dumping is single threaded at present. 25577 */ 25578 25579 wr_pktp = NULL; 25580 25581 #if defined(__i386) || defined(__amd64) 25582 dma_resid = wr_bp->b_bcount; 25583 oblkno = blkno; 25584 while (dma_resid != 0) { 25585 #endif 25586 25587 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 25588 wr_bp->b_flags &= ~B_ERROR; 25589 25590 #if defined(__i386) || defined(__amd64) 25591 blkno = oblkno + 25592 ((wr_bp->b_bcount - dma_resid) / 25593 un->un_tgt_blocksize); 25594 nblk = dma_resid / un->un_tgt_blocksize; 25595 25596 if (wr_pktp) { 25597 /* Partial DMA transfers after initial transfer */ 25598 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 25599 blkno, nblk); 25600 } else { 25601 /* Initial transfer */ 25602 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 25603 un->un_pkt_flags, NULL_FUNC, NULL, 25604 blkno, nblk); 25605 } 25606 #else 25607 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 25608 0, NULL_FUNC, NULL, blkno, nblk); 25609 #endif 25610 25611 if (rval == 0) { 25612 /* We were given a SCSI packet, continue. */ 25613 break; 25614 } 25615 25616 if (i == 0) { 25617 if (wr_bp->b_flags & B_ERROR) { 25618 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25619 "no resources for dumping; " 25620 "error code: 0x%x, retrying", 25621 geterror(wr_bp)); 25622 } else { 25623 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25624 "no resources for dumping; retrying"); 25625 } 25626 } else if (i != (SD_NDUMP_RETRIES - 1)) { 25627 if (wr_bp->b_flags & B_ERROR) { 25628 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 25629 "no resources for dumping; error code: " 25630 "0x%x, retrying\n", geterror(wr_bp)); 25631 } 25632 } else { 25633 if (wr_bp->b_flags & B_ERROR) { 25634 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 25635 "no resources for dumping; " 25636 "error code: 0x%x, retries failed, " 25637 "giving up.\n", geterror(wr_bp)); 25638 } else { 25639 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 25640 "no resources for dumping; " 25641 "retries failed, giving up.\n"); 25642 } 25643 mutex_enter(SD_MUTEX(un)); 25644 Restore_state(un); 25645 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 25646 mutex_exit(SD_MUTEX(un)); 25647 scsi_free_consistent_buf(wr_bp); 25648 } else { 25649 mutex_exit(SD_MUTEX(un)); 25650 } 25651 return (EIO); 25652 } 25653 drv_usecwait(10000); 25654 } 25655 25656 #if defined(__i386) || defined(__amd64) 25657 /* 25658 * save the resid from PARTIAL_DMA 25659 */ 25660 dma_resid = wr_pktp->pkt_resid; 25661 if (dma_resid != 0) 25662 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 25663 wr_pktp->pkt_resid = 0; 25664 #endif 25665 25666 /* SunBug 1222170 */ 25667 wr_pktp->pkt_flags = FLAG_NOINTR; 25668 25669 err = EIO; 25670 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 25671 25672 /* 25673 * Scsi_poll returns 0 (success) if the command completes and 25674 * the status block is STATUS_GOOD. We should only check 25675 * errors if this condition is not true. Even then we should 25676 * send our own request sense packet only if we have a check 25677 * condition and auto request sense has not been performed by 25678 * the hba. 25679 */ 25680 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 25681 25682 if ((sd_scsi_poll(un, wr_pktp) == 0) && 25683 (wr_pktp->pkt_resid == 0)) { 25684 err = SD_SUCCESS; 25685 break; 25686 } 25687 25688 /* 25689 * Check CMD_DEV_GONE 1st, give up if device is gone. 25690 */ 25691 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 25692 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 25693 "Device is gone\n"); 25694 break; 25695 } 25696 25697 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 25698 SD_INFO(SD_LOG_DUMP, un, 25699 "sddump: write failed with CHECK, try # %d\n", i); 25700 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 25701 (void) sd_send_polled_RQS(un); 25702 } 25703 25704 continue; 25705 } 25706 25707 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 25708 int reset_retval = 0; 25709 25710 SD_INFO(SD_LOG_DUMP, un, 25711 "sddump: write failed with BUSY, try # %d\n", i); 25712 25713 if (un->un_f_lun_reset_enabled == TRUE) { 25714 reset_retval = scsi_reset(SD_ADDRESS(un), 25715 RESET_LUN); 25716 } 25717 if (reset_retval == 0) { 25718 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 25719 } 25720 (void) sd_send_polled_RQS(un); 25721 25722 } else { 25723 SD_INFO(SD_LOG_DUMP, un, 25724 "sddump: write failed with 0x%x, try # %d\n", 25725 SD_GET_PKT_STATUS(wr_pktp), i); 25726 mutex_enter(SD_MUTEX(un)); 25727 sd_reset_target(un, wr_pktp); 25728 mutex_exit(SD_MUTEX(un)); 25729 } 25730 25731 /* 25732 * If we are not getting anywhere with lun/target resets, 25733 * let's reset the bus. 25734 */ 25735 if (i == SD_NDUMP_RETRIES/2) { 25736 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 25737 (void) sd_send_polled_RQS(un); 25738 } 25739 25740 } 25741 #if defined(__i386) || defined(__amd64) 25742 } /* dma_resid */ 25743 #endif 25744 25745 scsi_destroy_pkt(wr_pktp); 25746 mutex_enter(SD_MUTEX(un)); 25747 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 25748 mutex_exit(SD_MUTEX(un)); 25749 scsi_free_consistent_buf(wr_bp); 25750 } else { 25751 mutex_exit(SD_MUTEX(un)); 25752 } 25753 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 25754 return (err); 25755 } 25756 25757 /* 25758 * Function: sd_scsi_poll() 25759 * 25760 * Description: This is a wrapper for the scsi_poll call. 25761 * 25762 * Arguments: sd_lun - The unit structure 25763 * scsi_pkt - The scsi packet being sent to the device. 25764 * 25765 * Return Code: 0 - Command completed successfully with good status 25766 * -1 - Command failed. This could indicate a check condition 25767 * or other status value requiring recovery action. 25768 * 25769 */ 25770 25771 static int 25772 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 25773 { 25774 int status; 25775 25776 ASSERT(un != NULL); 25777 ASSERT(!mutex_owned(SD_MUTEX(un))); 25778 ASSERT(pktp != NULL); 25779 25780 status = SD_SUCCESS; 25781 25782 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 25783 pktp->pkt_flags |= un->un_tagflags; 25784 pktp->pkt_flags &= ~FLAG_NODISCON; 25785 } 25786 25787 status = sd_ddi_scsi_poll(pktp); 25788 /* 25789 * Scsi_poll returns 0 (success) if the command completes and the 25790 * status block is STATUS_GOOD. We should only check errors if this 25791 * condition is not true. Even then we should send our own request 25792 * sense packet only if we have a check condition and auto 25793 * request sense has not been performed by the hba. 25794 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 25795 */ 25796 if ((status != SD_SUCCESS) && 25797 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 25798 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 25799 (pktp->pkt_reason != CMD_DEV_GONE)) 25800 (void) sd_send_polled_RQS(un); 25801 25802 return (status); 25803 } 25804 25805 /* 25806 * Function: sd_send_polled_RQS() 25807 * 25808 * Description: This sends the request sense command to a device. 25809 * 25810 * Arguments: sd_lun - The unit structure 25811 * 25812 * Return Code: 0 - Command completed successfully with good status 25813 * -1 - Command failed. 25814 * 25815 */ 25816 25817 static int 25818 sd_send_polled_RQS(struct sd_lun *un) 25819 { 25820 int ret_val; 25821 struct scsi_pkt *rqs_pktp; 25822 struct buf *rqs_bp; 25823 25824 ASSERT(un != NULL); 25825 ASSERT(!mutex_owned(SD_MUTEX(un))); 25826 25827 ret_val = SD_SUCCESS; 25828 25829 rqs_pktp = un->un_rqs_pktp; 25830 rqs_bp = un->un_rqs_bp; 25831 25832 mutex_enter(SD_MUTEX(un)); 25833 25834 if (un->un_sense_isbusy) { 25835 ret_val = SD_FAILURE; 25836 mutex_exit(SD_MUTEX(un)); 25837 return (ret_val); 25838 } 25839 25840 /* 25841 * If the request sense buffer (and packet) is not in use, 25842 * let's set the un_sense_isbusy and send our packet 25843 */ 25844 un->un_sense_isbusy = 1; 25845 rqs_pktp->pkt_resid = 0; 25846 rqs_pktp->pkt_reason = 0; 25847 rqs_pktp->pkt_flags |= FLAG_NOINTR; 25848 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 25849 25850 mutex_exit(SD_MUTEX(un)); 25851 25852 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 25853 " 0x%p\n", rqs_bp->b_un.b_addr); 25854 25855 /* 25856 * Can't send this to sd_scsi_poll, we wrap ourselves around the 25857 * axle - it has a call into us! 25858 */ 25859 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 25860 SD_INFO(SD_LOG_COMMON, un, 25861 "sd_send_polled_RQS: RQS failed\n"); 25862 } 25863 25864 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 25865 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 25866 25867 mutex_enter(SD_MUTEX(un)); 25868 un->un_sense_isbusy = 0; 25869 mutex_exit(SD_MUTEX(un)); 25870 25871 return (ret_val); 25872 } 25873 25874 /* 25875 * Defines needed for localized version of the scsi_poll routine. 25876 */ 25877 #define SD_CSEC 10000 /* usecs */ 25878 #define SD_SEC_TO_CSEC (1000000/SD_CSEC) 25879 25880 25881 /* 25882 * Function: sd_ddi_scsi_poll() 25883 * 25884 * Description: Localized version of the scsi_poll routine. The purpose is to 25885 * send a scsi_pkt to a device as a polled command. This version 25886 * is to ensure more robust handling of transport errors. 25887 * Specifically this routine cures not ready, coming ready 25888 * transition for power up and reset of sonoma's. This can take 25889 * up to 45 seconds for power-on and 20 seconds for reset of a 25890 * sonoma lun. 25891 * 25892 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 25893 * 25894 * Return Code: 0 - Command completed successfully with good status 25895 * -1 - Command failed. 25896 * 25897 */ 25898 25899 static int 25900 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 25901 { 25902 int busy_count; 25903 int timeout; 25904 int rval = SD_FAILURE; 25905 int savef; 25906 struct scsi_extended_sense *sensep; 25907 long savet; 25908 void (*savec)(); 25909 /* 25910 * The following is defined in machdep.c and is used in determining if 25911 * the scsi transport system will do polled I/O instead of interrupt 25912 * I/O when called from xx_dump(). 25913 */ 25914 extern int do_polled_io; 25915 25916 /* 25917 * save old flags in pkt, to restore at end 25918 */ 25919 savef = pkt->pkt_flags; 25920 savec = pkt->pkt_comp; 25921 savet = pkt->pkt_time; 25922 25923 pkt->pkt_flags |= FLAG_NOINTR; 25924 25925 /* 25926 * XXX there is nothing in the SCSA spec that states that we should not 25927 * do a callback for polled cmds; however, removing this will break sd 25928 * and probably other target drivers 25929 */ 25930 pkt->pkt_comp = NULL; 25931 25932 /* 25933 * we don't like a polled command without timeout. 25934 * 60 seconds seems long enough. 25935 */ 25936 if (pkt->pkt_time == 0) { 25937 pkt->pkt_time = SCSI_POLL_TIMEOUT; 25938 } 25939 25940 /* 25941 * Send polled cmd. 25942 * 25943 * We do some error recovery for various errors. Tran_busy, 25944 * queue full, and non-dispatched commands are retried every 10 msec. 25945 * as they are typically transient failures. Busy status and Not 25946 * Ready are retried every second as this status takes a while to 25947 * change. Unit attention is retried for pkt_time (60) times 25948 * with no delay. 25949 */ 25950 timeout = pkt->pkt_time * SD_SEC_TO_CSEC; 25951 25952 for (busy_count = 0; busy_count < timeout; busy_count++) { 25953 int rc; 25954 int poll_delay; 25955 25956 /* 25957 * Initialize pkt status variables. 25958 */ 25959 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 25960 25961 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 25962 if (rc != TRAN_BUSY) { 25963 /* Transport failed - give up. */ 25964 break; 25965 } else { 25966 /* Transport busy - try again. */ 25967 poll_delay = 1 * SD_CSEC; /* 10 msec */ 25968 } 25969 } else { 25970 /* 25971 * Transport accepted - check pkt status. 25972 */ 25973 rc = (*pkt->pkt_scbp) & STATUS_MASK; 25974 if (pkt->pkt_reason == CMD_CMPLT && 25975 rc == STATUS_CHECK && 25976 pkt->pkt_state & STATE_ARQ_DONE) { 25977 struct scsi_arq_status *arqstat = 25978 (struct scsi_arq_status *)(pkt->pkt_scbp); 25979 25980 sensep = &arqstat->sts_sensedata; 25981 } else { 25982 sensep = NULL; 25983 } 25984 25985 if ((pkt->pkt_reason == CMD_CMPLT) && 25986 (rc == STATUS_GOOD)) { 25987 /* No error - we're done */ 25988 rval = SD_SUCCESS; 25989 break; 25990 25991 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 25992 /* Lost connection - give up */ 25993 break; 25994 25995 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 25996 (pkt->pkt_state == 0)) { 25997 /* Pkt not dispatched - try again. */ 25998 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 25999 26000 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26001 (rc == STATUS_QFULL)) { 26002 /* Queue full - try again. */ 26003 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 26004 26005 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26006 (rc == STATUS_BUSY)) { 26007 /* Busy - try again. */ 26008 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 26009 busy_count += (SD_SEC_TO_CSEC - 1); 26010 26011 } else if ((sensep != NULL) && 26012 (sensep->es_key == KEY_UNIT_ATTENTION)) { 26013 /* Unit Attention - try again */ 26014 busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */ 26015 continue; 26016 26017 } else if ((sensep != NULL) && 26018 (sensep->es_key == KEY_NOT_READY) && 26019 (sensep->es_add_code == 0x04) && 26020 (sensep->es_qual_code == 0x01)) { 26021 /* Not ready -> ready - try again. */ 26022 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 26023 busy_count += (SD_SEC_TO_CSEC - 1); 26024 26025 } else { 26026 /* BAD status - give up. */ 26027 break; 26028 } 26029 } 26030 26031 if ((curthread->t_flag & T_INTR_THREAD) == 0 && 26032 !do_polled_io) { 26033 delay(drv_usectohz(poll_delay)); 26034 } else { 26035 /* we busy wait during cpr_dump or interrupt threads */ 26036 drv_usecwait(poll_delay); 26037 } 26038 } 26039 26040 pkt->pkt_flags = savef; 26041 pkt->pkt_comp = savec; 26042 pkt->pkt_time = savet; 26043 return (rval); 26044 } 26045 26046 26047 /* 26048 * Function: sd_persistent_reservation_in_read_keys 26049 * 26050 * Description: This routine is the driver entry point for handling CD-ROM 26051 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 26052 * by sending the SCSI-3 PRIN commands to the device. 26053 * Processes the read keys command response by copying the 26054 * reservation key information into the user provided buffer. 26055 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 26056 * 26057 * Arguments: un - Pointer to soft state struct for the target. 26058 * usrp - user provided pointer to multihost Persistent In Read 26059 * Keys structure (mhioc_inkeys_t) 26060 * flag - this argument is a pass through to ddi_copyxxx() 26061 * directly from the mode argument of ioctl(). 26062 * 26063 * Return Code: 0 - Success 26064 * EACCES 26065 * ENOTSUP 26066 * errno return code from sd_send_scsi_cmd() 26067 * 26068 * Context: Can sleep. Does not return until command is completed. 26069 */ 26070 26071 static int 26072 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 26073 mhioc_inkeys_t *usrp, int flag) 26074 { 26075 #ifdef _MULTI_DATAMODEL 26076 struct mhioc_key_list32 li32; 26077 #endif 26078 sd_prin_readkeys_t *in; 26079 mhioc_inkeys_t *ptr; 26080 mhioc_key_list_t li; 26081 uchar_t *data_bufp; 26082 int data_len; 26083 int rval; 26084 size_t copysz; 26085 26086 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 26087 return (EINVAL); 26088 } 26089 bzero(&li, sizeof (mhioc_key_list_t)); 26090 26091 /* 26092 * Get the listsize from user 26093 */ 26094 #ifdef _MULTI_DATAMODEL 26095 26096 switch (ddi_model_convert_from(flag & FMODELS)) { 26097 case DDI_MODEL_ILP32: 26098 copysz = sizeof (struct mhioc_key_list32); 26099 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 26100 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26101 "sd_persistent_reservation_in_read_keys: " 26102 "failed ddi_copyin: mhioc_key_list32_t\n"); 26103 rval = EFAULT; 26104 goto done; 26105 } 26106 li.listsize = li32.listsize; 26107 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 26108 break; 26109 26110 case DDI_MODEL_NONE: 26111 copysz = sizeof (mhioc_key_list_t); 26112 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26113 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26114 "sd_persistent_reservation_in_read_keys: " 26115 "failed ddi_copyin: mhioc_key_list_t\n"); 26116 rval = EFAULT; 26117 goto done; 26118 } 26119 break; 26120 } 26121 26122 #else /* ! _MULTI_DATAMODEL */ 26123 copysz = sizeof (mhioc_key_list_t); 26124 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26125 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26126 "sd_persistent_reservation_in_read_keys: " 26127 "failed ddi_copyin: mhioc_key_list_t\n"); 26128 rval = EFAULT; 26129 goto done; 26130 } 26131 #endif 26132 26133 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 26134 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 26135 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26136 26137 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 26138 data_len, data_bufp)) != 0) { 26139 goto done; 26140 } 26141 in = (sd_prin_readkeys_t *)data_bufp; 26142 ptr->generation = BE_32(in->generation); 26143 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 26144 26145 /* 26146 * Return the min(listsize, listlen) keys 26147 */ 26148 #ifdef _MULTI_DATAMODEL 26149 26150 switch (ddi_model_convert_from(flag & FMODELS)) { 26151 case DDI_MODEL_ILP32: 26152 li32.listlen = li.listlen; 26153 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 26154 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26155 "sd_persistent_reservation_in_read_keys: " 26156 "failed ddi_copyout: mhioc_key_list32_t\n"); 26157 rval = EFAULT; 26158 goto done; 26159 } 26160 break; 26161 26162 case DDI_MODEL_NONE: 26163 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26164 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26165 "sd_persistent_reservation_in_read_keys: " 26166 "failed ddi_copyout: mhioc_key_list_t\n"); 26167 rval = EFAULT; 26168 goto done; 26169 } 26170 break; 26171 } 26172 26173 #else /* ! _MULTI_DATAMODEL */ 26174 26175 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26176 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26177 "sd_persistent_reservation_in_read_keys: " 26178 "failed ddi_copyout: mhioc_key_list_t\n"); 26179 rval = EFAULT; 26180 goto done; 26181 } 26182 26183 #endif /* _MULTI_DATAMODEL */ 26184 26185 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 26186 li.listsize * MHIOC_RESV_KEY_SIZE); 26187 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 26188 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26189 "sd_persistent_reservation_in_read_keys: " 26190 "failed ddi_copyout: keylist\n"); 26191 rval = EFAULT; 26192 } 26193 done: 26194 kmem_free(data_bufp, data_len); 26195 return (rval); 26196 } 26197 26198 26199 /* 26200 * Function: sd_persistent_reservation_in_read_resv 26201 * 26202 * Description: This routine is the driver entry point for handling CD-ROM 26203 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 26204 * by sending the SCSI-3 PRIN commands to the device. 26205 * Process the read persistent reservations command response by 26206 * copying the reservation information into the user provided 26207 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 26208 * 26209 * Arguments: un - Pointer to soft state struct for the target. 26210 * usrp - user provided pointer to multihost Persistent In Read 26211 * Keys structure (mhioc_inkeys_t) 26212 * flag - this argument is a pass through to ddi_copyxxx() 26213 * directly from the mode argument of ioctl(). 26214 * 26215 * Return Code: 0 - Success 26216 * EACCES 26217 * ENOTSUP 26218 * errno return code from sd_send_scsi_cmd() 26219 * 26220 * Context: Can sleep. Does not return until command is completed. 26221 */ 26222 26223 static int 26224 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 26225 mhioc_inresvs_t *usrp, int flag) 26226 { 26227 #ifdef _MULTI_DATAMODEL 26228 struct mhioc_resv_desc_list32 resvlist32; 26229 #endif 26230 sd_prin_readresv_t *in; 26231 mhioc_inresvs_t *ptr; 26232 sd_readresv_desc_t *readresv_ptr; 26233 mhioc_resv_desc_list_t resvlist; 26234 mhioc_resv_desc_t resvdesc; 26235 uchar_t *data_bufp; 26236 int data_len; 26237 int rval; 26238 int i; 26239 size_t copysz; 26240 mhioc_resv_desc_t *bufp; 26241 26242 if ((ptr = usrp) == NULL) { 26243 return (EINVAL); 26244 } 26245 26246 /* 26247 * Get the listsize from user 26248 */ 26249 #ifdef _MULTI_DATAMODEL 26250 switch (ddi_model_convert_from(flag & FMODELS)) { 26251 case DDI_MODEL_ILP32: 26252 copysz = sizeof (struct mhioc_resv_desc_list32); 26253 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 26254 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26255 "sd_persistent_reservation_in_read_resv: " 26256 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26257 rval = EFAULT; 26258 goto done; 26259 } 26260 resvlist.listsize = resvlist32.listsize; 26261 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 26262 break; 26263 26264 case DDI_MODEL_NONE: 26265 copysz = sizeof (mhioc_resv_desc_list_t); 26266 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26267 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26268 "sd_persistent_reservation_in_read_resv: " 26269 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26270 rval = EFAULT; 26271 goto done; 26272 } 26273 break; 26274 } 26275 #else /* ! _MULTI_DATAMODEL */ 26276 copysz = sizeof (mhioc_resv_desc_list_t); 26277 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26278 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26279 "sd_persistent_reservation_in_read_resv: " 26280 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26281 rval = EFAULT; 26282 goto done; 26283 } 26284 #endif /* ! _MULTI_DATAMODEL */ 26285 26286 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 26287 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 26288 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26289 26290 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV, 26291 data_len, data_bufp)) != 0) { 26292 goto done; 26293 } 26294 in = (sd_prin_readresv_t *)data_bufp; 26295 ptr->generation = BE_32(in->generation); 26296 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 26297 26298 /* 26299 * Return the min(listsize, listlen( keys 26300 */ 26301 #ifdef _MULTI_DATAMODEL 26302 26303 switch (ddi_model_convert_from(flag & FMODELS)) { 26304 case DDI_MODEL_ILP32: 26305 resvlist32.listlen = resvlist.listlen; 26306 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 26307 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26308 "sd_persistent_reservation_in_read_resv: " 26309 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26310 rval = EFAULT; 26311 goto done; 26312 } 26313 break; 26314 26315 case DDI_MODEL_NONE: 26316 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 26317 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26318 "sd_persistent_reservation_in_read_resv: " 26319 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26320 rval = EFAULT; 26321 goto done; 26322 } 26323 break; 26324 } 26325 26326 #else /* ! _MULTI_DATAMODEL */ 26327 26328 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 26329 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26330 "sd_persistent_reservation_in_read_resv: " 26331 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26332 rval = EFAULT; 26333 goto done; 26334 } 26335 26336 #endif /* ! _MULTI_DATAMODEL */ 26337 26338 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 26339 bufp = resvlist.list; 26340 copysz = sizeof (mhioc_resv_desc_t); 26341 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 26342 i++, readresv_ptr++, bufp++) { 26343 26344 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 26345 MHIOC_RESV_KEY_SIZE); 26346 resvdesc.type = readresv_ptr->type; 26347 resvdesc.scope = readresv_ptr->scope; 26348 resvdesc.scope_specific_addr = 26349 BE_32(readresv_ptr->scope_specific_addr); 26350 26351 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 26352 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26353 "sd_persistent_reservation_in_read_resv: " 26354 "failed ddi_copyout: resvlist\n"); 26355 rval = EFAULT; 26356 goto done; 26357 } 26358 } 26359 done: 26360 kmem_free(data_bufp, data_len); 26361 return (rval); 26362 } 26363 26364 26365 /* 26366 * Function: sr_change_blkmode() 26367 * 26368 * Description: This routine is the driver entry point for handling CD-ROM 26369 * block mode ioctl requests. Support for returning and changing 26370 * the current block size in use by the device is implemented. The 26371 * LBA size is changed via a MODE SELECT Block Descriptor. 26372 * 26373 * This routine issues a mode sense with an allocation length of 26374 * 12 bytes for the mode page header and a single block descriptor. 26375 * 26376 * Arguments: dev - the device 'dev_t' 26377 * cmd - the request type; one of CDROMGBLKMODE (get) or 26378 * CDROMSBLKMODE (set) 26379 * data - current block size or requested block size 26380 * flag - this argument is a pass through to ddi_copyxxx() directly 26381 * from the mode argument of ioctl(). 26382 * 26383 * Return Code: the code returned by sd_send_scsi_cmd() 26384 * EINVAL if invalid arguments are provided 26385 * EFAULT if ddi_copyxxx() fails 26386 * ENXIO if fail ddi_get_soft_state 26387 * EIO if invalid mode sense block descriptor length 26388 * 26389 */ 26390 26391 static int 26392 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 26393 { 26394 struct sd_lun *un = NULL; 26395 struct mode_header *sense_mhp, *select_mhp; 26396 struct block_descriptor *sense_desc, *select_desc; 26397 int current_bsize; 26398 int rval = EINVAL; 26399 uchar_t *sense = NULL; 26400 uchar_t *select = NULL; 26401 26402 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 26403 26404 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26405 return (ENXIO); 26406 } 26407 26408 /* 26409 * The block length is changed via the Mode Select block descriptor, the 26410 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 26411 * required as part of this routine. Therefore the mode sense allocation 26412 * length is specified to be the length of a mode page header and a 26413 * block descriptor. 26414 */ 26415 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 26416 26417 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 26418 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) { 26419 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26420 "sr_change_blkmode: Mode Sense Failed\n"); 26421 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26422 return (rval); 26423 } 26424 26425 /* Check the block descriptor len to handle only 1 block descriptor */ 26426 sense_mhp = (struct mode_header *)sense; 26427 if ((sense_mhp->bdesc_length == 0) || 26428 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 26429 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26430 "sr_change_blkmode: Mode Sense returned invalid block" 26431 " descriptor length\n"); 26432 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26433 return (EIO); 26434 } 26435 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 26436 current_bsize = ((sense_desc->blksize_hi << 16) | 26437 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 26438 26439 /* Process command */ 26440 switch (cmd) { 26441 case CDROMGBLKMODE: 26442 /* Return the block size obtained during the mode sense */ 26443 if (ddi_copyout(¤t_bsize, (void *)data, 26444 sizeof (int), flag) != 0) 26445 rval = EFAULT; 26446 break; 26447 case CDROMSBLKMODE: 26448 /* Validate the requested block size */ 26449 switch (data) { 26450 case CDROM_BLK_512: 26451 case CDROM_BLK_1024: 26452 case CDROM_BLK_2048: 26453 case CDROM_BLK_2056: 26454 case CDROM_BLK_2336: 26455 case CDROM_BLK_2340: 26456 case CDROM_BLK_2352: 26457 case CDROM_BLK_2368: 26458 case CDROM_BLK_2448: 26459 case CDROM_BLK_2646: 26460 case CDROM_BLK_2647: 26461 break; 26462 default: 26463 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26464 "sr_change_blkmode: " 26465 "Block Size '%ld' Not Supported\n", data); 26466 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26467 return (EINVAL); 26468 } 26469 26470 /* 26471 * The current block size matches the requested block size so 26472 * there is no need to send the mode select to change the size 26473 */ 26474 if (current_bsize == data) { 26475 break; 26476 } 26477 26478 /* Build the select data for the requested block size */ 26479 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 26480 select_mhp = (struct mode_header *)select; 26481 select_desc = 26482 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 26483 /* 26484 * The LBA size is changed via the block descriptor, so the 26485 * descriptor is built according to the user data 26486 */ 26487 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 26488 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 26489 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 26490 select_desc->blksize_lo = (char)((data) & 0x000000ff); 26491 26492 /* Send the mode select for the requested block size */ 26493 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 26494 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 26495 SD_PATH_STANDARD)) != 0) { 26496 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26497 "sr_change_blkmode: Mode Select Failed\n"); 26498 /* 26499 * The mode select failed for the requested block size, 26500 * so reset the data for the original block size and 26501 * send it to the target. The error is indicated by the 26502 * return value for the failed mode select. 26503 */ 26504 select_desc->blksize_hi = sense_desc->blksize_hi; 26505 select_desc->blksize_mid = sense_desc->blksize_mid; 26506 select_desc->blksize_lo = sense_desc->blksize_lo; 26507 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 26508 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 26509 SD_PATH_STANDARD); 26510 } else { 26511 ASSERT(!mutex_owned(SD_MUTEX(un))); 26512 mutex_enter(SD_MUTEX(un)); 26513 sd_update_block_info(un, (uint32_t)data, 0); 26514 26515 mutex_exit(SD_MUTEX(un)); 26516 } 26517 break; 26518 default: 26519 /* should not reach here, but check anyway */ 26520 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26521 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 26522 rval = EINVAL; 26523 break; 26524 } 26525 26526 if (select) { 26527 kmem_free(select, BUFLEN_CHG_BLK_MODE); 26528 } 26529 if (sense) { 26530 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26531 } 26532 return (rval); 26533 } 26534 26535 26536 /* 26537 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 26538 * implement driver support for getting and setting the CD speed. The command 26539 * set used will be based on the device type. If the device has not been 26540 * identified as MMC the Toshiba vendor specific mode page will be used. If 26541 * the device is MMC but does not support the Real Time Streaming feature 26542 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 26543 * be used to read the speed. 26544 */ 26545 26546 /* 26547 * Function: sr_change_speed() 26548 * 26549 * Description: This routine is the driver entry point for handling CD-ROM 26550 * drive speed ioctl requests for devices supporting the Toshiba 26551 * vendor specific drive speed mode page. Support for returning 26552 * and changing the current drive speed in use by the device is 26553 * implemented. 26554 * 26555 * Arguments: dev - the device 'dev_t' 26556 * cmd - the request type; one of CDROMGDRVSPEED (get) or 26557 * CDROMSDRVSPEED (set) 26558 * data - current drive speed or requested drive speed 26559 * flag - this argument is a pass through to ddi_copyxxx() directly 26560 * from the mode argument of ioctl(). 26561 * 26562 * Return Code: the code returned by sd_send_scsi_cmd() 26563 * EINVAL if invalid arguments are provided 26564 * EFAULT if ddi_copyxxx() fails 26565 * ENXIO if fail ddi_get_soft_state 26566 * EIO if invalid mode sense block descriptor length 26567 */ 26568 26569 static int 26570 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 26571 { 26572 struct sd_lun *un = NULL; 26573 struct mode_header *sense_mhp, *select_mhp; 26574 struct mode_speed *sense_page, *select_page; 26575 int current_speed; 26576 int rval = EINVAL; 26577 int bd_len; 26578 uchar_t *sense = NULL; 26579 uchar_t *select = NULL; 26580 26581 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 26582 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26583 return (ENXIO); 26584 } 26585 26586 /* 26587 * Note: The drive speed is being modified here according to a Toshiba 26588 * vendor specific mode page (0x31). 26589 */ 26590 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 26591 26592 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 26593 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 26594 SD_PATH_STANDARD)) != 0) { 26595 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26596 "sr_change_speed: Mode Sense Failed\n"); 26597 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 26598 return (rval); 26599 } 26600 sense_mhp = (struct mode_header *)sense; 26601 26602 /* Check the block descriptor len to handle only 1 block descriptor */ 26603 bd_len = sense_mhp->bdesc_length; 26604 if (bd_len > MODE_BLK_DESC_LENGTH) { 26605 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26606 "sr_change_speed: Mode Sense returned invalid block " 26607 "descriptor length\n"); 26608 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 26609 return (EIO); 26610 } 26611 26612 sense_page = (struct mode_speed *) 26613 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 26614 current_speed = sense_page->speed; 26615 26616 /* Process command */ 26617 switch (cmd) { 26618 case CDROMGDRVSPEED: 26619 /* Return the drive speed obtained during the mode sense */ 26620 if (current_speed == 0x2) { 26621 current_speed = CDROM_TWELVE_SPEED; 26622 } 26623 if (ddi_copyout(¤t_speed, (void *)data, 26624 sizeof (int), flag) != 0) { 26625 rval = EFAULT; 26626 } 26627 break; 26628 case CDROMSDRVSPEED: 26629 /* Validate the requested drive speed */ 26630 switch ((uchar_t)data) { 26631 case CDROM_TWELVE_SPEED: 26632 data = 0x2; 26633 /*FALLTHROUGH*/ 26634 case CDROM_NORMAL_SPEED: 26635 case CDROM_DOUBLE_SPEED: 26636 case CDROM_QUAD_SPEED: 26637 case CDROM_MAXIMUM_SPEED: 26638 break; 26639 default: 26640 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26641 "sr_change_speed: " 26642 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 26643 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 26644 return (EINVAL); 26645 } 26646 26647 /* 26648 * The current drive speed matches the requested drive speed so 26649 * there is no need to send the mode select to change the speed 26650 */ 26651 if (current_speed == data) { 26652 break; 26653 } 26654 26655 /* Build the select data for the requested drive speed */ 26656 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 26657 select_mhp = (struct mode_header *)select; 26658 select_mhp->bdesc_length = 0; 26659 select_page = 26660 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 26661 select_page = 26662 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 26663 select_page->mode_page.code = CDROM_MODE_SPEED; 26664 select_page->mode_page.length = 2; 26665 select_page->speed = (uchar_t)data; 26666 26667 /* Send the mode select for the requested block size */ 26668 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 26669 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 26670 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 26671 /* 26672 * The mode select failed for the requested drive speed, 26673 * so reset the data for the original drive speed and 26674 * send it to the target. The error is indicated by the 26675 * return value for the failed mode select. 26676 */ 26677 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26678 "sr_drive_speed: Mode Select Failed\n"); 26679 select_page->speed = sense_page->speed; 26680 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 26681 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 26682 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 26683 } 26684 break; 26685 default: 26686 /* should not reach here, but check anyway */ 26687 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26688 "sr_change_speed: Command '%x' Not Supported\n", cmd); 26689 rval = EINVAL; 26690 break; 26691 } 26692 26693 if (select) { 26694 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 26695 } 26696 if (sense) { 26697 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 26698 } 26699 26700 return (rval); 26701 } 26702 26703 26704 /* 26705 * Function: sr_atapi_change_speed() 26706 * 26707 * Description: This routine is the driver entry point for handling CD-ROM 26708 * drive speed ioctl requests for MMC devices that do not support 26709 * the Real Time Streaming feature (0x107). 26710 * 26711 * Note: This routine will use the SET SPEED command which may not 26712 * be supported by all devices. 26713 * 26714 * Arguments: dev- the device 'dev_t' 26715 * cmd- the request type; one of CDROMGDRVSPEED (get) or 26716 * CDROMSDRVSPEED (set) 26717 * data- current drive speed or requested drive speed 26718 * flag- this argument is a pass through to ddi_copyxxx() directly 26719 * from the mode argument of ioctl(). 26720 * 26721 * Return Code: the code returned by sd_send_scsi_cmd() 26722 * EINVAL if invalid arguments are provided 26723 * EFAULT if ddi_copyxxx() fails 26724 * ENXIO if fail ddi_get_soft_state 26725 * EIO if invalid mode sense block descriptor length 26726 */ 26727 26728 static int 26729 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 26730 { 26731 struct sd_lun *un; 26732 struct uscsi_cmd *com = NULL; 26733 struct mode_header_grp2 *sense_mhp; 26734 uchar_t *sense_page; 26735 uchar_t *sense = NULL; 26736 char cdb[CDB_GROUP5]; 26737 int bd_len; 26738 int current_speed = 0; 26739 int max_speed = 0; 26740 int rval; 26741 26742 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 26743 26744 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26745 return (ENXIO); 26746 } 26747 26748 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 26749 26750 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 26751 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 26752 SD_PATH_STANDARD)) != 0) { 26753 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26754 "sr_atapi_change_speed: Mode Sense Failed\n"); 26755 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 26756 return (rval); 26757 } 26758 26759 /* Check the block descriptor len to handle only 1 block descriptor */ 26760 sense_mhp = (struct mode_header_grp2 *)sense; 26761 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 26762 if (bd_len > MODE_BLK_DESC_LENGTH) { 26763 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26764 "sr_atapi_change_speed: Mode Sense returned invalid " 26765 "block descriptor length\n"); 26766 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 26767 return (EIO); 26768 } 26769 26770 /* Calculate the current and maximum drive speeds */ 26771 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 26772 current_speed = (sense_page[14] << 8) | sense_page[15]; 26773 max_speed = (sense_page[8] << 8) | sense_page[9]; 26774 26775 /* Process the command */ 26776 switch (cmd) { 26777 case CDROMGDRVSPEED: 26778 current_speed /= SD_SPEED_1X; 26779 if (ddi_copyout(¤t_speed, (void *)data, 26780 sizeof (int), flag) != 0) 26781 rval = EFAULT; 26782 break; 26783 case CDROMSDRVSPEED: 26784 /* Convert the speed code to KB/sec */ 26785 switch ((uchar_t)data) { 26786 case CDROM_NORMAL_SPEED: 26787 current_speed = SD_SPEED_1X; 26788 break; 26789 case CDROM_DOUBLE_SPEED: 26790 current_speed = 2 * SD_SPEED_1X; 26791 break; 26792 case CDROM_QUAD_SPEED: 26793 current_speed = 4 * SD_SPEED_1X; 26794 break; 26795 case CDROM_TWELVE_SPEED: 26796 current_speed = 12 * SD_SPEED_1X; 26797 break; 26798 case CDROM_MAXIMUM_SPEED: 26799 current_speed = 0xffff; 26800 break; 26801 default: 26802 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26803 "sr_atapi_change_speed: invalid drive speed %d\n", 26804 (uchar_t)data); 26805 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 26806 return (EINVAL); 26807 } 26808 26809 /* Check the request against the drive's max speed. */ 26810 if (current_speed != 0xffff) { 26811 if (current_speed > max_speed) { 26812 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 26813 return (EINVAL); 26814 } 26815 } 26816 26817 /* 26818 * Build and send the SET SPEED command 26819 * 26820 * Note: The SET SPEED (0xBB) command used in this routine is 26821 * obsolete per the SCSI MMC spec but still supported in the 26822 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 26823 * therefore the command is still implemented in this routine. 26824 */ 26825 bzero(cdb, sizeof (cdb)); 26826 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 26827 cdb[2] = (uchar_t)(current_speed >> 8); 26828 cdb[3] = (uchar_t)current_speed; 26829 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26830 com->uscsi_cdb = (caddr_t)cdb; 26831 com->uscsi_cdblen = CDB_GROUP5; 26832 com->uscsi_bufaddr = NULL; 26833 com->uscsi_buflen = 0; 26834 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 26835 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, 0, 26836 UIO_SYSSPACE, SD_PATH_STANDARD); 26837 break; 26838 default: 26839 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26840 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 26841 rval = EINVAL; 26842 } 26843 26844 if (sense) { 26845 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 26846 } 26847 if (com) { 26848 kmem_free(com, sizeof (*com)); 26849 } 26850 return (rval); 26851 } 26852 26853 26854 /* 26855 * Function: sr_pause_resume() 26856 * 26857 * Description: This routine is the driver entry point for handling CD-ROM 26858 * pause/resume ioctl requests. This only affects the audio play 26859 * operation. 26860 * 26861 * Arguments: dev - the device 'dev_t' 26862 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 26863 * for setting the resume bit of the cdb. 26864 * 26865 * Return Code: the code returned by sd_send_scsi_cmd() 26866 * EINVAL if invalid mode specified 26867 * 26868 */ 26869 26870 static int 26871 sr_pause_resume(dev_t dev, int cmd) 26872 { 26873 struct sd_lun *un; 26874 struct uscsi_cmd *com; 26875 char cdb[CDB_GROUP1]; 26876 int rval; 26877 26878 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26879 return (ENXIO); 26880 } 26881 26882 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26883 bzero(cdb, CDB_GROUP1); 26884 cdb[0] = SCMD_PAUSE_RESUME; 26885 switch (cmd) { 26886 case CDROMRESUME: 26887 cdb[8] = 1; 26888 break; 26889 case CDROMPAUSE: 26890 cdb[8] = 0; 26891 break; 26892 default: 26893 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 26894 " Command '%x' Not Supported\n", cmd); 26895 rval = EINVAL; 26896 goto done; 26897 } 26898 26899 com->uscsi_cdb = cdb; 26900 com->uscsi_cdblen = CDB_GROUP1; 26901 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 26902 26903 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 26904 UIO_SYSSPACE, SD_PATH_STANDARD); 26905 26906 done: 26907 kmem_free(com, sizeof (*com)); 26908 return (rval); 26909 } 26910 26911 26912 /* 26913 * Function: sr_play_msf() 26914 * 26915 * Description: This routine is the driver entry point for handling CD-ROM 26916 * ioctl requests to output the audio signals at the specified 26917 * starting address and continue the audio play until the specified 26918 * ending address (CDROMPLAYMSF) The address is in Minute Second 26919 * Frame (MSF) format. 26920 * 26921 * Arguments: dev - the device 'dev_t' 26922 * data - pointer to user provided audio msf structure, 26923 * specifying start/end addresses. 26924 * flag - this argument is a pass through to ddi_copyxxx() 26925 * directly from the mode argument of ioctl(). 26926 * 26927 * Return Code: the code returned by sd_send_scsi_cmd() 26928 * EFAULT if ddi_copyxxx() fails 26929 * ENXIO if fail ddi_get_soft_state 26930 * EINVAL if data pointer is NULL 26931 */ 26932 26933 static int 26934 sr_play_msf(dev_t dev, caddr_t data, int flag) 26935 { 26936 struct sd_lun *un; 26937 struct uscsi_cmd *com; 26938 struct cdrom_msf msf_struct; 26939 struct cdrom_msf *msf = &msf_struct; 26940 char cdb[CDB_GROUP1]; 26941 int rval; 26942 26943 if (data == NULL) { 26944 return (EINVAL); 26945 } 26946 26947 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26948 return (ENXIO); 26949 } 26950 26951 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 26952 return (EFAULT); 26953 } 26954 26955 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26956 bzero(cdb, CDB_GROUP1); 26957 cdb[0] = SCMD_PLAYAUDIO_MSF; 26958 if (un->un_f_cfg_playmsf_bcd == TRUE) { 26959 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 26960 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 26961 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 26962 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 26963 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 26964 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 26965 } else { 26966 cdb[3] = msf->cdmsf_min0; 26967 cdb[4] = msf->cdmsf_sec0; 26968 cdb[5] = msf->cdmsf_frame0; 26969 cdb[6] = msf->cdmsf_min1; 26970 cdb[7] = msf->cdmsf_sec1; 26971 cdb[8] = msf->cdmsf_frame1; 26972 } 26973 com->uscsi_cdb = cdb; 26974 com->uscsi_cdblen = CDB_GROUP1; 26975 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 26976 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 26977 UIO_SYSSPACE, SD_PATH_STANDARD); 26978 kmem_free(com, sizeof (*com)); 26979 return (rval); 26980 } 26981 26982 26983 /* 26984 * Function: sr_play_trkind() 26985 * 26986 * Description: This routine is the driver entry point for handling CD-ROM 26987 * ioctl requests to output the audio signals at the specified 26988 * starting address and continue the audio play until the specified 26989 * ending address (CDROMPLAYTRKIND). The address is in Track Index 26990 * format. 26991 * 26992 * Arguments: dev - the device 'dev_t' 26993 * data - pointer to user provided audio track/index structure, 26994 * specifying start/end addresses. 26995 * flag - this argument is a pass through to ddi_copyxxx() 26996 * directly from the mode argument of ioctl(). 26997 * 26998 * Return Code: the code returned by sd_send_scsi_cmd() 26999 * EFAULT if ddi_copyxxx() fails 27000 * ENXIO if fail ddi_get_soft_state 27001 * EINVAL if data pointer is NULL 27002 */ 27003 27004 static int 27005 sr_play_trkind(dev_t dev, caddr_t data, int flag) 27006 { 27007 struct cdrom_ti ti_struct; 27008 struct cdrom_ti *ti = &ti_struct; 27009 struct uscsi_cmd *com = NULL; 27010 char cdb[CDB_GROUP1]; 27011 int rval; 27012 27013 if (data == NULL) { 27014 return (EINVAL); 27015 } 27016 27017 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 27018 return (EFAULT); 27019 } 27020 27021 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27022 bzero(cdb, CDB_GROUP1); 27023 cdb[0] = SCMD_PLAYAUDIO_TI; 27024 cdb[4] = ti->cdti_trk0; 27025 cdb[5] = ti->cdti_ind0; 27026 cdb[7] = ti->cdti_trk1; 27027 cdb[8] = ti->cdti_ind1; 27028 com->uscsi_cdb = cdb; 27029 com->uscsi_cdblen = CDB_GROUP1; 27030 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27031 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27032 UIO_SYSSPACE, SD_PATH_STANDARD); 27033 kmem_free(com, sizeof (*com)); 27034 return (rval); 27035 } 27036 27037 27038 /* 27039 * Function: sr_read_all_subcodes() 27040 * 27041 * Description: This routine is the driver entry point for handling CD-ROM 27042 * ioctl requests to return raw subcode data while the target is 27043 * playing audio (CDROMSUBCODE). 27044 * 27045 * Arguments: dev - the device 'dev_t' 27046 * data - pointer to user provided cdrom subcode structure, 27047 * specifying the transfer length and address. 27048 * flag - this argument is a pass through to ddi_copyxxx() 27049 * directly from the mode argument of ioctl(). 27050 * 27051 * Return Code: the code returned by sd_send_scsi_cmd() 27052 * EFAULT if ddi_copyxxx() fails 27053 * ENXIO if fail ddi_get_soft_state 27054 * EINVAL if data pointer is NULL 27055 */ 27056 27057 static int 27058 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 27059 { 27060 struct sd_lun *un = NULL; 27061 struct uscsi_cmd *com = NULL; 27062 struct cdrom_subcode *subcode = NULL; 27063 int rval; 27064 size_t buflen; 27065 char cdb[CDB_GROUP5]; 27066 27067 #ifdef _MULTI_DATAMODEL 27068 /* To support ILP32 applications in an LP64 world */ 27069 struct cdrom_subcode32 cdrom_subcode32; 27070 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 27071 #endif 27072 if (data == NULL) { 27073 return (EINVAL); 27074 } 27075 27076 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27077 return (ENXIO); 27078 } 27079 27080 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 27081 27082 #ifdef _MULTI_DATAMODEL 27083 switch (ddi_model_convert_from(flag & FMODELS)) { 27084 case DDI_MODEL_ILP32: 27085 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 27086 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27087 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27088 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27089 return (EFAULT); 27090 } 27091 /* Convert the ILP32 uscsi data from the application to LP64 */ 27092 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 27093 break; 27094 case DDI_MODEL_NONE: 27095 if (ddi_copyin(data, subcode, 27096 sizeof (struct cdrom_subcode), flag)) { 27097 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27098 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27099 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27100 return (EFAULT); 27101 } 27102 break; 27103 } 27104 #else /* ! _MULTI_DATAMODEL */ 27105 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 27106 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27107 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27108 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27109 return (EFAULT); 27110 } 27111 #endif /* _MULTI_DATAMODEL */ 27112 27113 /* 27114 * Since MMC-2 expects max 3 bytes for length, check if the 27115 * length input is greater than 3 bytes 27116 */ 27117 if ((subcode->cdsc_length & 0xFF000000) != 0) { 27118 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27119 "sr_read_all_subcodes: " 27120 "cdrom transfer length too large: %d (limit %d)\n", 27121 subcode->cdsc_length, 0xFFFFFF); 27122 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27123 return (EINVAL); 27124 } 27125 27126 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 27127 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27128 bzero(cdb, CDB_GROUP5); 27129 27130 if (un->un_f_mmc_cap == TRUE) { 27131 cdb[0] = (char)SCMD_READ_CD; 27132 cdb[2] = (char)0xff; 27133 cdb[3] = (char)0xff; 27134 cdb[4] = (char)0xff; 27135 cdb[5] = (char)0xff; 27136 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27137 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27138 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 27139 cdb[10] = 1; 27140 } else { 27141 /* 27142 * Note: A vendor specific command (0xDF) is being used her to 27143 * request a read of all subcodes. 27144 */ 27145 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 27146 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 27147 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27148 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27149 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 27150 } 27151 com->uscsi_cdb = cdb; 27152 com->uscsi_cdblen = CDB_GROUP5; 27153 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 27154 com->uscsi_buflen = buflen; 27155 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27156 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 27157 UIO_SYSSPACE, SD_PATH_STANDARD); 27158 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27159 kmem_free(com, sizeof (*com)); 27160 return (rval); 27161 } 27162 27163 27164 /* 27165 * Function: sr_read_subchannel() 27166 * 27167 * Description: This routine is the driver entry point for handling CD-ROM 27168 * ioctl requests to return the Q sub-channel data of the CD 27169 * current position block. (CDROMSUBCHNL) The data includes the 27170 * track number, index number, absolute CD-ROM address (LBA or MSF 27171 * format per the user) , track relative CD-ROM address (LBA or MSF 27172 * format per the user), control data and audio status. 27173 * 27174 * Arguments: dev - the device 'dev_t' 27175 * data - pointer to user provided cdrom sub-channel structure 27176 * flag - this argument is a pass through to ddi_copyxxx() 27177 * directly from the mode argument of ioctl(). 27178 * 27179 * Return Code: the code returned by sd_send_scsi_cmd() 27180 * EFAULT if ddi_copyxxx() fails 27181 * ENXIO if fail ddi_get_soft_state 27182 * EINVAL if data pointer is NULL 27183 */ 27184 27185 static int 27186 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 27187 { 27188 struct sd_lun *un; 27189 struct uscsi_cmd *com; 27190 struct cdrom_subchnl subchanel; 27191 struct cdrom_subchnl *subchnl = &subchanel; 27192 char cdb[CDB_GROUP1]; 27193 caddr_t buffer; 27194 int rval; 27195 27196 if (data == NULL) { 27197 return (EINVAL); 27198 } 27199 27200 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27201 (un->un_state == SD_STATE_OFFLINE)) { 27202 return (ENXIO); 27203 } 27204 27205 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 27206 return (EFAULT); 27207 } 27208 27209 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 27210 bzero(cdb, CDB_GROUP1); 27211 cdb[0] = SCMD_READ_SUBCHANNEL; 27212 /* Set the MSF bit based on the user requested address format */ 27213 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 27214 /* 27215 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 27216 * returned 27217 */ 27218 cdb[2] = 0x40; 27219 /* 27220 * Set byte 3 to specify the return data format. A value of 0x01 27221 * indicates that the CD-ROM current position should be returned. 27222 */ 27223 cdb[3] = 0x01; 27224 cdb[8] = 0x10; 27225 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27226 com->uscsi_cdb = cdb; 27227 com->uscsi_cdblen = CDB_GROUP1; 27228 com->uscsi_bufaddr = buffer; 27229 com->uscsi_buflen = 16; 27230 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27231 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27232 UIO_SYSSPACE, SD_PATH_STANDARD); 27233 if (rval != 0) { 27234 kmem_free(buffer, 16); 27235 kmem_free(com, sizeof (*com)); 27236 return (rval); 27237 } 27238 27239 /* Process the returned Q sub-channel data */ 27240 subchnl->cdsc_audiostatus = buffer[1]; 27241 subchnl->cdsc_adr = (buffer[5] & 0xF0); 27242 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 27243 subchnl->cdsc_trk = buffer[6]; 27244 subchnl->cdsc_ind = buffer[7]; 27245 if (subchnl->cdsc_format & CDROM_LBA) { 27246 subchnl->cdsc_absaddr.lba = 27247 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 27248 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 27249 subchnl->cdsc_reladdr.lba = 27250 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 27251 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 27252 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 27253 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 27254 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 27255 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 27256 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 27257 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 27258 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 27259 } else { 27260 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 27261 subchnl->cdsc_absaddr.msf.second = buffer[10]; 27262 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 27263 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 27264 subchnl->cdsc_reladdr.msf.second = buffer[14]; 27265 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 27266 } 27267 kmem_free(buffer, 16); 27268 kmem_free(com, sizeof (*com)); 27269 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 27270 != 0) { 27271 return (EFAULT); 27272 } 27273 return (rval); 27274 } 27275 27276 27277 /* 27278 * Function: sr_read_tocentry() 27279 * 27280 * Description: This routine is the driver entry point for handling CD-ROM 27281 * ioctl requests to read from the Table of Contents (TOC) 27282 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 27283 * fields, the starting address (LBA or MSF format per the user) 27284 * and the data mode if the user specified track is a data track. 27285 * 27286 * Note: The READ HEADER (0x44) command used in this routine is 27287 * obsolete per the SCSI MMC spec but still supported in the 27288 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27289 * therefore the command is still implemented in this routine. 27290 * 27291 * Arguments: dev - the device 'dev_t' 27292 * data - pointer to user provided toc entry structure, 27293 * specifying the track # and the address format 27294 * (LBA or MSF). 27295 * flag - this argument is a pass through to ddi_copyxxx() 27296 * directly from the mode argument of ioctl(). 27297 * 27298 * Return Code: the code returned by sd_send_scsi_cmd() 27299 * EFAULT if ddi_copyxxx() fails 27300 * ENXIO if fail ddi_get_soft_state 27301 * EINVAL if data pointer is NULL 27302 */ 27303 27304 static int 27305 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 27306 { 27307 struct sd_lun *un = NULL; 27308 struct uscsi_cmd *com; 27309 struct cdrom_tocentry toc_entry; 27310 struct cdrom_tocentry *entry = &toc_entry; 27311 caddr_t buffer; 27312 int rval; 27313 char cdb[CDB_GROUP1]; 27314 27315 if (data == NULL) { 27316 return (EINVAL); 27317 } 27318 27319 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27320 (un->un_state == SD_STATE_OFFLINE)) { 27321 return (ENXIO); 27322 } 27323 27324 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 27325 return (EFAULT); 27326 } 27327 27328 /* Validate the requested track and address format */ 27329 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 27330 return (EINVAL); 27331 } 27332 27333 if (entry->cdte_track == 0) { 27334 return (EINVAL); 27335 } 27336 27337 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 27338 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27339 bzero(cdb, CDB_GROUP1); 27340 27341 cdb[0] = SCMD_READ_TOC; 27342 /* Set the MSF bit based on the user requested address format */ 27343 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 27344 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 27345 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 27346 } else { 27347 cdb[6] = entry->cdte_track; 27348 } 27349 27350 /* 27351 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 27352 * (4 byte TOC response header + 8 byte track descriptor) 27353 */ 27354 cdb[8] = 12; 27355 com->uscsi_cdb = cdb; 27356 com->uscsi_cdblen = CDB_GROUP1; 27357 com->uscsi_bufaddr = buffer; 27358 com->uscsi_buflen = 0x0C; 27359 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 27360 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27361 UIO_SYSSPACE, SD_PATH_STANDARD); 27362 if (rval != 0) { 27363 kmem_free(buffer, 12); 27364 kmem_free(com, sizeof (*com)); 27365 return (rval); 27366 } 27367 27368 /* Process the toc entry */ 27369 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 27370 entry->cdte_ctrl = (buffer[5] & 0x0F); 27371 if (entry->cdte_format & CDROM_LBA) { 27372 entry->cdte_addr.lba = 27373 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 27374 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 27375 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 27376 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 27377 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 27378 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 27379 /* 27380 * Send a READ TOC command using the LBA address format to get 27381 * the LBA for the track requested so it can be used in the 27382 * READ HEADER request 27383 * 27384 * Note: The MSF bit of the READ HEADER command specifies the 27385 * output format. The block address specified in that command 27386 * must be in LBA format. 27387 */ 27388 cdb[1] = 0; 27389 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27390 UIO_SYSSPACE, SD_PATH_STANDARD); 27391 if (rval != 0) { 27392 kmem_free(buffer, 12); 27393 kmem_free(com, sizeof (*com)); 27394 return (rval); 27395 } 27396 } else { 27397 entry->cdte_addr.msf.minute = buffer[9]; 27398 entry->cdte_addr.msf.second = buffer[10]; 27399 entry->cdte_addr.msf.frame = buffer[11]; 27400 /* 27401 * Send a READ TOC command using the LBA address format to get 27402 * the LBA for the track requested so it can be used in the 27403 * READ HEADER request 27404 * 27405 * Note: The MSF bit of the READ HEADER command specifies the 27406 * output format. The block address specified in that command 27407 * must be in LBA format. 27408 */ 27409 cdb[1] = 0; 27410 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27411 UIO_SYSSPACE, SD_PATH_STANDARD); 27412 if (rval != 0) { 27413 kmem_free(buffer, 12); 27414 kmem_free(com, sizeof (*com)); 27415 return (rval); 27416 } 27417 } 27418 27419 /* 27420 * Build and send the READ HEADER command to determine the data mode of 27421 * the user specified track. 27422 */ 27423 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 27424 (entry->cdte_track != CDROM_LEADOUT)) { 27425 bzero(cdb, CDB_GROUP1); 27426 cdb[0] = SCMD_READ_HEADER; 27427 cdb[2] = buffer[8]; 27428 cdb[3] = buffer[9]; 27429 cdb[4] = buffer[10]; 27430 cdb[5] = buffer[11]; 27431 cdb[8] = 0x08; 27432 com->uscsi_buflen = 0x08; 27433 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27434 UIO_SYSSPACE, SD_PATH_STANDARD); 27435 if (rval == 0) { 27436 entry->cdte_datamode = buffer[0]; 27437 } else { 27438 /* 27439 * READ HEADER command failed, since this is 27440 * obsoleted in one spec, its better to return 27441 * -1 for an invlid track so that we can still 27442 * recieve the rest of the TOC data. 27443 */ 27444 entry->cdte_datamode = (uchar_t)-1; 27445 } 27446 } else { 27447 entry->cdte_datamode = (uchar_t)-1; 27448 } 27449 27450 kmem_free(buffer, 12); 27451 kmem_free(com, sizeof (*com)); 27452 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 27453 return (EFAULT); 27454 27455 return (rval); 27456 } 27457 27458 27459 /* 27460 * Function: sr_read_tochdr() 27461 * 27462 * Description: This routine is the driver entry point for handling CD-ROM 27463 * ioctl requests to read the Table of Contents (TOC) header 27464 * (CDROMREADTOHDR). The TOC header consists of the disk starting 27465 * and ending track numbers 27466 * 27467 * Arguments: dev - the device 'dev_t' 27468 * data - pointer to user provided toc header structure, 27469 * specifying the starting and ending track numbers. 27470 * flag - this argument is a pass through to ddi_copyxxx() 27471 * directly from the mode argument of ioctl(). 27472 * 27473 * Return Code: the code returned by sd_send_scsi_cmd() 27474 * EFAULT if ddi_copyxxx() fails 27475 * ENXIO if fail ddi_get_soft_state 27476 * EINVAL if data pointer is NULL 27477 */ 27478 27479 static int 27480 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 27481 { 27482 struct sd_lun *un; 27483 struct uscsi_cmd *com; 27484 struct cdrom_tochdr toc_header; 27485 struct cdrom_tochdr *hdr = &toc_header; 27486 char cdb[CDB_GROUP1]; 27487 int rval; 27488 caddr_t buffer; 27489 27490 if (data == NULL) { 27491 return (EINVAL); 27492 } 27493 27494 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27495 (un->un_state == SD_STATE_OFFLINE)) { 27496 return (ENXIO); 27497 } 27498 27499 buffer = kmem_zalloc(4, KM_SLEEP); 27500 bzero(cdb, CDB_GROUP1); 27501 cdb[0] = SCMD_READ_TOC; 27502 /* 27503 * Specifying a track number of 0x00 in the READ TOC command indicates 27504 * that the TOC header should be returned 27505 */ 27506 cdb[6] = 0x00; 27507 /* 27508 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 27509 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 27510 */ 27511 cdb[8] = 0x04; 27512 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27513 com->uscsi_cdb = cdb; 27514 com->uscsi_cdblen = CDB_GROUP1; 27515 com->uscsi_bufaddr = buffer; 27516 com->uscsi_buflen = 0x04; 27517 com->uscsi_timeout = 300; 27518 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27519 27520 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27521 UIO_SYSSPACE, SD_PATH_STANDARD); 27522 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 27523 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 27524 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 27525 } else { 27526 hdr->cdth_trk0 = buffer[2]; 27527 hdr->cdth_trk1 = buffer[3]; 27528 } 27529 kmem_free(buffer, 4); 27530 kmem_free(com, sizeof (*com)); 27531 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 27532 return (EFAULT); 27533 } 27534 return (rval); 27535 } 27536 27537 27538 /* 27539 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 27540 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 27541 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 27542 * digital audio and extended architecture digital audio. These modes are 27543 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 27544 * MMC specs. 27545 * 27546 * In addition to support for the various data formats these routines also 27547 * include support for devices that implement only the direct access READ 27548 * commands (0x08, 0x28), devices that implement the READ_CD commands 27549 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 27550 * READ CDXA commands (0xD8, 0xDB) 27551 */ 27552 27553 /* 27554 * Function: sr_read_mode1() 27555 * 27556 * Description: This routine is the driver entry point for handling CD-ROM 27557 * ioctl read mode1 requests (CDROMREADMODE1). 27558 * 27559 * Arguments: dev - the device 'dev_t' 27560 * data - pointer to user provided cd read structure specifying 27561 * the lba buffer address and length. 27562 * flag - this argument is a pass through to ddi_copyxxx() 27563 * directly from the mode argument of ioctl(). 27564 * 27565 * Return Code: the code returned by sd_send_scsi_cmd() 27566 * EFAULT if ddi_copyxxx() fails 27567 * ENXIO if fail ddi_get_soft_state 27568 * EINVAL if data pointer is NULL 27569 */ 27570 27571 static int 27572 sr_read_mode1(dev_t dev, caddr_t data, int flag) 27573 { 27574 struct sd_lun *un; 27575 struct cdrom_read mode1_struct; 27576 struct cdrom_read *mode1 = &mode1_struct; 27577 int rval; 27578 #ifdef _MULTI_DATAMODEL 27579 /* To support ILP32 applications in an LP64 world */ 27580 struct cdrom_read32 cdrom_read32; 27581 struct cdrom_read32 *cdrd32 = &cdrom_read32; 27582 #endif /* _MULTI_DATAMODEL */ 27583 27584 if (data == NULL) { 27585 return (EINVAL); 27586 } 27587 27588 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27589 (un->un_state == SD_STATE_OFFLINE)) { 27590 return (ENXIO); 27591 } 27592 27593 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 27594 "sd_read_mode1: entry: un:0x%p\n", un); 27595 27596 #ifdef _MULTI_DATAMODEL 27597 switch (ddi_model_convert_from(flag & FMODELS)) { 27598 case DDI_MODEL_ILP32: 27599 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 27600 return (EFAULT); 27601 } 27602 /* Convert the ILP32 uscsi data from the application to LP64 */ 27603 cdrom_read32tocdrom_read(cdrd32, mode1); 27604 break; 27605 case DDI_MODEL_NONE: 27606 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 27607 return (EFAULT); 27608 } 27609 } 27610 #else /* ! _MULTI_DATAMODEL */ 27611 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 27612 return (EFAULT); 27613 } 27614 #endif /* _MULTI_DATAMODEL */ 27615 27616 rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr, 27617 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 27618 27619 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 27620 "sd_read_mode1: exit: un:0x%p\n", un); 27621 27622 return (rval); 27623 } 27624 27625 27626 /* 27627 * Function: sr_read_cd_mode2() 27628 * 27629 * Description: This routine is the driver entry point for handling CD-ROM 27630 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 27631 * support the READ CD (0xBE) command or the 1st generation 27632 * READ CD (0xD4) command. 27633 * 27634 * Arguments: dev - the device 'dev_t' 27635 * data - pointer to user provided cd read structure specifying 27636 * the lba buffer address and length. 27637 * flag - this argument is a pass through to ddi_copyxxx() 27638 * directly from the mode argument of ioctl(). 27639 * 27640 * Return Code: the code returned by sd_send_scsi_cmd() 27641 * EFAULT if ddi_copyxxx() fails 27642 * ENXIO if fail ddi_get_soft_state 27643 * EINVAL if data pointer is NULL 27644 */ 27645 27646 static int 27647 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 27648 { 27649 struct sd_lun *un; 27650 struct uscsi_cmd *com; 27651 struct cdrom_read mode2_struct; 27652 struct cdrom_read *mode2 = &mode2_struct; 27653 uchar_t cdb[CDB_GROUP5]; 27654 int nblocks; 27655 int rval; 27656 #ifdef _MULTI_DATAMODEL 27657 /* To support ILP32 applications in an LP64 world */ 27658 struct cdrom_read32 cdrom_read32; 27659 struct cdrom_read32 *cdrd32 = &cdrom_read32; 27660 #endif /* _MULTI_DATAMODEL */ 27661 27662 if (data == NULL) { 27663 return (EINVAL); 27664 } 27665 27666 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27667 (un->un_state == SD_STATE_OFFLINE)) { 27668 return (ENXIO); 27669 } 27670 27671 #ifdef _MULTI_DATAMODEL 27672 switch (ddi_model_convert_from(flag & FMODELS)) { 27673 case DDI_MODEL_ILP32: 27674 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 27675 return (EFAULT); 27676 } 27677 /* Convert the ILP32 uscsi data from the application to LP64 */ 27678 cdrom_read32tocdrom_read(cdrd32, mode2); 27679 break; 27680 case DDI_MODEL_NONE: 27681 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 27682 return (EFAULT); 27683 } 27684 break; 27685 } 27686 27687 #else /* ! _MULTI_DATAMODEL */ 27688 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 27689 return (EFAULT); 27690 } 27691 #endif /* _MULTI_DATAMODEL */ 27692 27693 bzero(cdb, sizeof (cdb)); 27694 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 27695 /* Read command supported by 1st generation atapi drives */ 27696 cdb[0] = SCMD_READ_CDD4; 27697 } else { 27698 /* Universal CD Access Command */ 27699 cdb[0] = SCMD_READ_CD; 27700 } 27701 27702 /* 27703 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 27704 */ 27705 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 27706 27707 /* set the start address */ 27708 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 27709 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 27710 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 27711 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 27712 27713 /* set the transfer length */ 27714 nblocks = mode2->cdread_buflen / 2336; 27715 cdb[6] = (uchar_t)(nblocks >> 16); 27716 cdb[7] = (uchar_t)(nblocks >> 8); 27717 cdb[8] = (uchar_t)nblocks; 27718 27719 /* set the filter bits */ 27720 cdb[9] = CDROM_READ_CD_USERDATA; 27721 27722 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27723 com->uscsi_cdb = (caddr_t)cdb; 27724 com->uscsi_cdblen = sizeof (cdb); 27725 com->uscsi_bufaddr = mode2->cdread_bufaddr; 27726 com->uscsi_buflen = mode2->cdread_buflen; 27727 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27728 27729 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 27730 UIO_SYSSPACE, SD_PATH_STANDARD); 27731 kmem_free(com, sizeof (*com)); 27732 return (rval); 27733 } 27734 27735 27736 /* 27737 * Function: sr_read_mode2() 27738 * 27739 * Description: This routine is the driver entry point for handling CD-ROM 27740 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 27741 * do not support the READ CD (0xBE) command. 27742 * 27743 * Arguments: dev - the device 'dev_t' 27744 * data - pointer to user provided cd read structure specifying 27745 * the lba buffer address and length. 27746 * flag - this argument is a pass through to ddi_copyxxx() 27747 * directly from the mode argument of ioctl(). 27748 * 27749 * Return Code: the code returned by sd_send_scsi_cmd() 27750 * EFAULT if ddi_copyxxx() fails 27751 * ENXIO if fail ddi_get_soft_state 27752 * EINVAL if data pointer is NULL 27753 * EIO if fail to reset block size 27754 * EAGAIN if commands are in progress in the driver 27755 */ 27756 27757 static int 27758 sr_read_mode2(dev_t dev, caddr_t data, int flag) 27759 { 27760 struct sd_lun *un; 27761 struct cdrom_read mode2_struct; 27762 struct cdrom_read *mode2 = &mode2_struct; 27763 int rval; 27764 uint32_t restore_blksize; 27765 struct uscsi_cmd *com; 27766 uchar_t cdb[CDB_GROUP0]; 27767 int nblocks; 27768 27769 #ifdef _MULTI_DATAMODEL 27770 /* To support ILP32 applications in an LP64 world */ 27771 struct cdrom_read32 cdrom_read32; 27772 struct cdrom_read32 *cdrd32 = &cdrom_read32; 27773 #endif /* _MULTI_DATAMODEL */ 27774 27775 if (data == NULL) { 27776 return (EINVAL); 27777 } 27778 27779 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27780 (un->un_state == SD_STATE_OFFLINE)) { 27781 return (ENXIO); 27782 } 27783 27784 /* 27785 * Because this routine will update the device and driver block size 27786 * being used we want to make sure there are no commands in progress. 27787 * If commands are in progress the user will have to try again. 27788 * 27789 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 27790 * in sdioctl to protect commands from sdioctl through to the top of 27791 * sd_uscsi_strategy. See sdioctl for details. 27792 */ 27793 mutex_enter(SD_MUTEX(un)); 27794 if (un->un_ncmds_in_driver != 1) { 27795 mutex_exit(SD_MUTEX(un)); 27796 return (EAGAIN); 27797 } 27798 mutex_exit(SD_MUTEX(un)); 27799 27800 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 27801 "sd_read_mode2: entry: un:0x%p\n", un); 27802 27803 #ifdef _MULTI_DATAMODEL 27804 switch (ddi_model_convert_from(flag & FMODELS)) { 27805 case DDI_MODEL_ILP32: 27806 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 27807 return (EFAULT); 27808 } 27809 /* Convert the ILP32 uscsi data from the application to LP64 */ 27810 cdrom_read32tocdrom_read(cdrd32, mode2); 27811 break; 27812 case DDI_MODEL_NONE: 27813 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 27814 return (EFAULT); 27815 } 27816 break; 27817 } 27818 #else /* ! _MULTI_DATAMODEL */ 27819 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 27820 return (EFAULT); 27821 } 27822 #endif /* _MULTI_DATAMODEL */ 27823 27824 /* Store the current target block size for restoration later */ 27825 restore_blksize = un->un_tgt_blocksize; 27826 27827 /* Change the device and soft state target block size to 2336 */ 27828 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 27829 rval = EIO; 27830 goto done; 27831 } 27832 27833 27834 bzero(cdb, sizeof (cdb)); 27835 27836 /* set READ operation */ 27837 cdb[0] = SCMD_READ; 27838 27839 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 27840 mode2->cdread_lba >>= 2; 27841 27842 /* set the start address */ 27843 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 27844 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 27845 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 27846 27847 /* set the transfer length */ 27848 nblocks = mode2->cdread_buflen / 2336; 27849 cdb[4] = (uchar_t)nblocks & 0xFF; 27850 27851 /* build command */ 27852 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27853 com->uscsi_cdb = (caddr_t)cdb; 27854 com->uscsi_cdblen = sizeof (cdb); 27855 com->uscsi_bufaddr = mode2->cdread_bufaddr; 27856 com->uscsi_buflen = mode2->cdread_buflen; 27857 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27858 27859 /* 27860 * Issue SCSI command with user space address for read buffer. 27861 * 27862 * This sends the command through main channel in the driver. 27863 * 27864 * Since this is accessed via an IOCTL call, we go through the 27865 * standard path, so that if the device was powered down, then 27866 * it would be 'awakened' to handle the command. 27867 */ 27868 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 27869 UIO_SYSSPACE, SD_PATH_STANDARD); 27870 27871 kmem_free(com, sizeof (*com)); 27872 27873 /* Restore the device and soft state target block size */ 27874 if (sr_sector_mode(dev, restore_blksize) != 0) { 27875 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27876 "can't do switch back to mode 1\n"); 27877 /* 27878 * If sd_send_scsi_READ succeeded we still need to report 27879 * an error because we failed to reset the block size 27880 */ 27881 if (rval == 0) { 27882 rval = EIO; 27883 } 27884 } 27885 27886 done: 27887 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 27888 "sd_read_mode2: exit: un:0x%p\n", un); 27889 27890 return (rval); 27891 } 27892 27893 27894 /* 27895 * Function: sr_sector_mode() 27896 * 27897 * Description: This utility function is used by sr_read_mode2 to set the target 27898 * block size based on the user specified size. This is a legacy 27899 * implementation based upon a vendor specific mode page 27900 * 27901 * Arguments: dev - the device 'dev_t' 27902 * data - flag indicating if block size is being set to 2336 or 27903 * 512. 27904 * 27905 * Return Code: the code returned by sd_send_scsi_cmd() 27906 * EFAULT if ddi_copyxxx() fails 27907 * ENXIO if fail ddi_get_soft_state 27908 * EINVAL if data pointer is NULL 27909 */ 27910 27911 static int 27912 sr_sector_mode(dev_t dev, uint32_t blksize) 27913 { 27914 struct sd_lun *un; 27915 uchar_t *sense; 27916 uchar_t *select; 27917 int rval; 27918 27919 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27920 (un->un_state == SD_STATE_OFFLINE)) { 27921 return (ENXIO); 27922 } 27923 27924 sense = kmem_zalloc(20, KM_SLEEP); 27925 27926 /* Note: This is a vendor specific mode page (0x81) */ 27927 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81, 27928 SD_PATH_STANDARD)) != 0) { 27929 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 27930 "sr_sector_mode: Mode Sense failed\n"); 27931 kmem_free(sense, 20); 27932 return (rval); 27933 } 27934 select = kmem_zalloc(20, KM_SLEEP); 27935 select[3] = 0x08; 27936 select[10] = ((blksize >> 8) & 0xff); 27937 select[11] = (blksize & 0xff); 27938 select[12] = 0x01; 27939 select[13] = 0x06; 27940 select[14] = sense[14]; 27941 select[15] = sense[15]; 27942 if (blksize == SD_MODE2_BLKSIZE) { 27943 select[14] |= 0x01; 27944 } 27945 27946 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20, 27947 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 27948 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 27949 "sr_sector_mode: Mode Select failed\n"); 27950 } else { 27951 /* 27952 * Only update the softstate block size if we successfully 27953 * changed the device block mode. 27954 */ 27955 mutex_enter(SD_MUTEX(un)); 27956 sd_update_block_info(un, blksize, 0); 27957 mutex_exit(SD_MUTEX(un)); 27958 } 27959 kmem_free(sense, 20); 27960 kmem_free(select, 20); 27961 return (rval); 27962 } 27963 27964 27965 /* 27966 * Function: sr_read_cdda() 27967 * 27968 * Description: This routine is the driver entry point for handling CD-ROM 27969 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 27970 * the target supports CDDA these requests are handled via a vendor 27971 * specific command (0xD8) If the target does not support CDDA 27972 * these requests are handled via the READ CD command (0xBE). 27973 * 27974 * Arguments: dev - the device 'dev_t' 27975 * data - pointer to user provided CD-DA structure specifying 27976 * the track starting address, transfer length, and 27977 * subcode options. 27978 * flag - this argument is a pass through to ddi_copyxxx() 27979 * directly from the mode argument of ioctl(). 27980 * 27981 * Return Code: the code returned by sd_send_scsi_cmd() 27982 * EFAULT if ddi_copyxxx() fails 27983 * ENXIO if fail ddi_get_soft_state 27984 * EINVAL if invalid arguments are provided 27985 * ENOTTY 27986 */ 27987 27988 static int 27989 sr_read_cdda(dev_t dev, caddr_t data, int flag) 27990 { 27991 struct sd_lun *un; 27992 struct uscsi_cmd *com; 27993 struct cdrom_cdda *cdda; 27994 int rval; 27995 size_t buflen; 27996 char cdb[CDB_GROUP5]; 27997 27998 #ifdef _MULTI_DATAMODEL 27999 /* To support ILP32 applications in an LP64 world */ 28000 struct cdrom_cdda32 cdrom_cdda32; 28001 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 28002 #endif /* _MULTI_DATAMODEL */ 28003 28004 if (data == NULL) { 28005 return (EINVAL); 28006 } 28007 28008 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28009 return (ENXIO); 28010 } 28011 28012 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 28013 28014 #ifdef _MULTI_DATAMODEL 28015 switch (ddi_model_convert_from(flag & FMODELS)) { 28016 case DDI_MODEL_ILP32: 28017 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 28018 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28019 "sr_read_cdda: ddi_copyin Failed\n"); 28020 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28021 return (EFAULT); 28022 } 28023 /* Convert the ILP32 uscsi data from the application to LP64 */ 28024 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 28025 break; 28026 case DDI_MODEL_NONE: 28027 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28028 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28029 "sr_read_cdda: ddi_copyin Failed\n"); 28030 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28031 return (EFAULT); 28032 } 28033 break; 28034 } 28035 #else /* ! _MULTI_DATAMODEL */ 28036 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28037 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28038 "sr_read_cdda: ddi_copyin Failed\n"); 28039 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28040 return (EFAULT); 28041 } 28042 #endif /* _MULTI_DATAMODEL */ 28043 28044 /* 28045 * Since MMC-2 expects max 3 bytes for length, check if the 28046 * length input is greater than 3 bytes 28047 */ 28048 if ((cdda->cdda_length & 0xFF000000) != 0) { 28049 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 28050 "cdrom transfer length too large: %d (limit %d)\n", 28051 cdda->cdda_length, 0xFFFFFF); 28052 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28053 return (EINVAL); 28054 } 28055 28056 switch (cdda->cdda_subcode) { 28057 case CDROM_DA_NO_SUBCODE: 28058 buflen = CDROM_BLK_2352 * cdda->cdda_length; 28059 break; 28060 case CDROM_DA_SUBQ: 28061 buflen = CDROM_BLK_2368 * cdda->cdda_length; 28062 break; 28063 case CDROM_DA_ALL_SUBCODE: 28064 buflen = CDROM_BLK_2448 * cdda->cdda_length; 28065 break; 28066 case CDROM_DA_SUBCODE_ONLY: 28067 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 28068 break; 28069 default: 28070 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28071 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 28072 cdda->cdda_subcode); 28073 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28074 return (EINVAL); 28075 } 28076 28077 /* Build and send the command */ 28078 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28079 bzero(cdb, CDB_GROUP5); 28080 28081 if (un->un_f_cfg_cdda == TRUE) { 28082 cdb[0] = (char)SCMD_READ_CD; 28083 cdb[1] = 0x04; 28084 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28085 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28086 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28087 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28088 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28089 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28090 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 28091 cdb[9] = 0x10; 28092 switch (cdda->cdda_subcode) { 28093 case CDROM_DA_NO_SUBCODE : 28094 cdb[10] = 0x0; 28095 break; 28096 case CDROM_DA_SUBQ : 28097 cdb[10] = 0x2; 28098 break; 28099 case CDROM_DA_ALL_SUBCODE : 28100 cdb[10] = 0x1; 28101 break; 28102 case CDROM_DA_SUBCODE_ONLY : 28103 /* FALLTHROUGH */ 28104 default : 28105 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28106 kmem_free(com, sizeof (*com)); 28107 return (ENOTTY); 28108 } 28109 } else { 28110 cdb[0] = (char)SCMD_READ_CDDA; 28111 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28112 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28113 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28114 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28115 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 28116 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28117 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28118 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 28119 cdb[10] = cdda->cdda_subcode; 28120 } 28121 28122 com->uscsi_cdb = cdb; 28123 com->uscsi_cdblen = CDB_GROUP5; 28124 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 28125 com->uscsi_buflen = buflen; 28126 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28127 28128 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28129 UIO_SYSSPACE, SD_PATH_STANDARD); 28130 28131 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28132 kmem_free(com, sizeof (*com)); 28133 return (rval); 28134 } 28135 28136 28137 /* 28138 * Function: sr_read_cdxa() 28139 * 28140 * Description: This routine is the driver entry point for handling CD-ROM 28141 * ioctl requests to return CD-XA (Extended Architecture) data. 28142 * (CDROMCDXA). 28143 * 28144 * Arguments: dev - the device 'dev_t' 28145 * data - pointer to user provided CD-XA structure specifying 28146 * the data starting address, transfer length, and format 28147 * flag - this argument is a pass through to ddi_copyxxx() 28148 * directly from the mode argument of ioctl(). 28149 * 28150 * Return Code: the code returned by sd_send_scsi_cmd() 28151 * EFAULT if ddi_copyxxx() fails 28152 * ENXIO if fail ddi_get_soft_state 28153 * EINVAL if data pointer is NULL 28154 */ 28155 28156 static int 28157 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 28158 { 28159 struct sd_lun *un; 28160 struct uscsi_cmd *com; 28161 struct cdrom_cdxa *cdxa; 28162 int rval; 28163 size_t buflen; 28164 char cdb[CDB_GROUP5]; 28165 uchar_t read_flags; 28166 28167 #ifdef _MULTI_DATAMODEL 28168 /* To support ILP32 applications in an LP64 world */ 28169 struct cdrom_cdxa32 cdrom_cdxa32; 28170 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 28171 #endif /* _MULTI_DATAMODEL */ 28172 28173 if (data == NULL) { 28174 return (EINVAL); 28175 } 28176 28177 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28178 return (ENXIO); 28179 } 28180 28181 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 28182 28183 #ifdef _MULTI_DATAMODEL 28184 switch (ddi_model_convert_from(flag & FMODELS)) { 28185 case DDI_MODEL_ILP32: 28186 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 28187 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28188 return (EFAULT); 28189 } 28190 /* 28191 * Convert the ILP32 uscsi data from the 28192 * application to LP64 for internal use. 28193 */ 28194 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 28195 break; 28196 case DDI_MODEL_NONE: 28197 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28198 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28199 return (EFAULT); 28200 } 28201 break; 28202 } 28203 #else /* ! _MULTI_DATAMODEL */ 28204 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28205 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28206 return (EFAULT); 28207 } 28208 #endif /* _MULTI_DATAMODEL */ 28209 28210 /* 28211 * Since MMC-2 expects max 3 bytes for length, check if the 28212 * length input is greater than 3 bytes 28213 */ 28214 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 28215 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 28216 "cdrom transfer length too large: %d (limit %d)\n", 28217 cdxa->cdxa_length, 0xFFFFFF); 28218 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28219 return (EINVAL); 28220 } 28221 28222 switch (cdxa->cdxa_format) { 28223 case CDROM_XA_DATA: 28224 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 28225 read_flags = 0x10; 28226 break; 28227 case CDROM_XA_SECTOR_DATA: 28228 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 28229 read_flags = 0xf8; 28230 break; 28231 case CDROM_XA_DATA_W_ERROR: 28232 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 28233 read_flags = 0xfc; 28234 break; 28235 default: 28236 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28237 "sr_read_cdxa: Format '0x%x' Not Supported\n", 28238 cdxa->cdxa_format); 28239 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28240 return (EINVAL); 28241 } 28242 28243 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28244 bzero(cdb, CDB_GROUP5); 28245 if (un->un_f_mmc_cap == TRUE) { 28246 cdb[0] = (char)SCMD_READ_CD; 28247 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28248 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28249 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28250 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28251 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28252 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28253 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 28254 cdb[9] = (char)read_flags; 28255 } else { 28256 /* 28257 * Note: A vendor specific command (0xDB) is being used her to 28258 * request a read of all subcodes. 28259 */ 28260 cdb[0] = (char)SCMD_READ_CDXA; 28261 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28262 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28263 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28264 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28265 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 28266 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28267 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28268 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 28269 cdb[10] = cdxa->cdxa_format; 28270 } 28271 com->uscsi_cdb = cdb; 28272 com->uscsi_cdblen = CDB_GROUP5; 28273 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 28274 com->uscsi_buflen = buflen; 28275 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28276 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28277 UIO_SYSSPACE, SD_PATH_STANDARD); 28278 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28279 kmem_free(com, sizeof (*com)); 28280 return (rval); 28281 } 28282 28283 28284 /* 28285 * Function: sr_eject() 28286 * 28287 * Description: This routine is the driver entry point for handling CD-ROM 28288 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 28289 * 28290 * Arguments: dev - the device 'dev_t' 28291 * 28292 * Return Code: the code returned by sd_send_scsi_cmd() 28293 */ 28294 28295 static int 28296 sr_eject(dev_t dev) 28297 { 28298 struct sd_lun *un; 28299 int rval; 28300 28301 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28302 (un->un_state == SD_STATE_OFFLINE)) { 28303 return (ENXIO); 28304 } 28305 if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 28306 SD_PATH_STANDARD)) != 0) { 28307 return (rval); 28308 } 28309 28310 rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT, 28311 SD_PATH_STANDARD); 28312 28313 if (rval == 0) { 28314 mutex_enter(SD_MUTEX(un)); 28315 sr_ejected(un); 28316 un->un_mediastate = DKIO_EJECTED; 28317 cv_broadcast(&un->un_state_cv); 28318 mutex_exit(SD_MUTEX(un)); 28319 } 28320 return (rval); 28321 } 28322 28323 28324 /* 28325 * Function: sr_ejected() 28326 * 28327 * Description: This routine updates the soft state structure to invalidate the 28328 * geometry information after the media has been ejected or a 28329 * media eject has been detected. 28330 * 28331 * Arguments: un - driver soft state (unit) structure 28332 */ 28333 28334 static void 28335 sr_ejected(struct sd_lun *un) 28336 { 28337 struct sd_errstats *stp; 28338 28339 ASSERT(un != NULL); 28340 ASSERT(mutex_owned(SD_MUTEX(un))); 28341 28342 un->un_f_blockcount_is_valid = FALSE; 28343 un->un_f_tgt_blocksize_is_valid = FALSE; 28344 un->un_f_geometry_is_valid = FALSE; 28345 28346 if (un->un_errstats != NULL) { 28347 stp = (struct sd_errstats *)un->un_errstats->ks_data; 28348 stp->sd_capacity.value.ui64 = 0; 28349 } 28350 } 28351 28352 28353 /* 28354 * Function: sr_check_wp() 28355 * 28356 * Description: This routine checks the write protection of a removable media 28357 * disk via the write protect bit of the Mode Page Header device 28358 * specific field. This routine has been implemented to use the 28359 * error recovery mode page for all device types. 28360 * Note: In the future use a sd_send_scsi_MODE_SENSE() routine 28361 * 28362 * Arguments: dev - the device 'dev_t' 28363 * 28364 * Return Code: int indicating if the device is write protected (1) or not (0) 28365 * 28366 * Context: Kernel thread. 28367 * 28368 */ 28369 28370 static int 28371 sr_check_wp(dev_t dev) 28372 { 28373 struct sd_lun *un; 28374 uchar_t device_specific; 28375 uchar_t *sense; 28376 int hdrlen; 28377 int rval; 28378 int retry_flag = FALSE; 28379 28380 /* 28381 * Note: The return codes for this routine should be reworked to 28382 * properly handle the case of a NULL softstate. 28383 */ 28384 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28385 return (FALSE); 28386 } 28387 28388 if (un->un_f_cfg_is_atapi == TRUE) { 28389 retry_flag = TRUE; 28390 } 28391 28392 retry: 28393 if (un->un_f_cfg_is_atapi == TRUE) { 28394 /* 28395 * The mode page contents are not required; set the allocation 28396 * length for the mode page header only 28397 */ 28398 hdrlen = MODE_HEADER_LENGTH_GRP2; 28399 sense = kmem_zalloc(hdrlen, KM_SLEEP); 28400 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen, 28401 MODEPAGE_ERR_RECOV, SD_PATH_STANDARD); 28402 device_specific = 28403 ((struct mode_header_grp2 *)sense)->device_specific; 28404 } else { 28405 hdrlen = MODE_HEADER_LENGTH; 28406 sense = kmem_zalloc(hdrlen, KM_SLEEP); 28407 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen, 28408 MODEPAGE_ERR_RECOV, SD_PATH_STANDARD); 28409 device_specific = 28410 ((struct mode_header *)sense)->device_specific; 28411 } 28412 28413 if (rval != 0) { 28414 if ((un->un_f_cfg_is_atapi == TRUE) && (retry_flag)) { 28415 /* 28416 * For an Atapi Zip drive, observed the drive 28417 * reporting check condition for the first attempt. 28418 * Sense data indicating power on or bus device/reset. 28419 * Hence in case of failure need to try at least once 28420 * for Atapi devices. 28421 */ 28422 retry_flag = FALSE; 28423 kmem_free(sense, hdrlen); 28424 goto retry; 28425 } else { 28426 /* 28427 * Write protect mode sense failed; not all disks 28428 * understand this query. Return FALSE assuming that 28429 * these devices are not writable. 28430 */ 28431 rval = FALSE; 28432 } 28433 } else { 28434 if (device_specific & WRITE_PROTECT) { 28435 rval = TRUE; 28436 } else { 28437 rval = FALSE; 28438 } 28439 } 28440 kmem_free(sense, hdrlen); 28441 return (rval); 28442 } 28443 28444 28445 /* 28446 * Function: sr_volume_ctrl() 28447 * 28448 * Description: This routine is the driver entry point for handling CD-ROM 28449 * audio output volume ioctl requests. (CDROMVOLCTRL) 28450 * 28451 * Arguments: dev - the device 'dev_t' 28452 * data - pointer to user audio volume control structure 28453 * flag - this argument is a pass through to ddi_copyxxx() 28454 * directly from the mode argument of ioctl(). 28455 * 28456 * Return Code: the code returned by sd_send_scsi_cmd() 28457 * EFAULT if ddi_copyxxx() fails 28458 * ENXIO if fail ddi_get_soft_state 28459 * EINVAL if data pointer is NULL 28460 * 28461 */ 28462 28463 static int 28464 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 28465 { 28466 struct sd_lun *un; 28467 struct cdrom_volctrl volume; 28468 struct cdrom_volctrl *vol = &volume; 28469 uchar_t *sense_page; 28470 uchar_t *select_page; 28471 uchar_t *sense; 28472 uchar_t *select; 28473 int sense_buflen; 28474 int select_buflen; 28475 int rval; 28476 28477 if (data == NULL) { 28478 return (EINVAL); 28479 } 28480 28481 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28482 (un->un_state == SD_STATE_OFFLINE)) { 28483 return (ENXIO); 28484 } 28485 28486 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 28487 return (EFAULT); 28488 } 28489 28490 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 28491 struct mode_header_grp2 *sense_mhp; 28492 struct mode_header_grp2 *select_mhp; 28493 int bd_len; 28494 28495 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 28496 select_buflen = MODE_HEADER_LENGTH_GRP2 + 28497 MODEPAGE_AUDIO_CTRL_LEN; 28498 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 28499 select = kmem_zalloc(select_buflen, KM_SLEEP); 28500 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 28501 sense_buflen, MODEPAGE_AUDIO_CTRL, 28502 SD_PATH_STANDARD)) != 0) { 28503 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28504 "sr_volume_ctrl: Mode Sense Failed\n"); 28505 kmem_free(sense, sense_buflen); 28506 kmem_free(select, select_buflen); 28507 return (rval); 28508 } 28509 sense_mhp = (struct mode_header_grp2 *)sense; 28510 select_mhp = (struct mode_header_grp2 *)select; 28511 bd_len = (sense_mhp->bdesc_length_hi << 8) | 28512 sense_mhp->bdesc_length_lo; 28513 if (bd_len > MODE_BLK_DESC_LENGTH) { 28514 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28515 "sr_volume_ctrl: Mode Sense returned invalid " 28516 "block descriptor length\n"); 28517 kmem_free(sense, sense_buflen); 28518 kmem_free(select, select_buflen); 28519 return (EIO); 28520 } 28521 sense_page = (uchar_t *) 28522 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 28523 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 28524 select_mhp->length_msb = 0; 28525 select_mhp->length_lsb = 0; 28526 select_mhp->bdesc_length_hi = 0; 28527 select_mhp->bdesc_length_lo = 0; 28528 } else { 28529 struct mode_header *sense_mhp, *select_mhp; 28530 28531 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 28532 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 28533 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 28534 select = kmem_zalloc(select_buflen, KM_SLEEP); 28535 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 28536 sense_buflen, MODEPAGE_AUDIO_CTRL, 28537 SD_PATH_STANDARD)) != 0) { 28538 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28539 "sr_volume_ctrl: Mode Sense Failed\n"); 28540 kmem_free(sense, sense_buflen); 28541 kmem_free(select, select_buflen); 28542 return (rval); 28543 } 28544 sense_mhp = (struct mode_header *)sense; 28545 select_mhp = (struct mode_header *)select; 28546 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 28547 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28548 "sr_volume_ctrl: Mode Sense returned invalid " 28549 "block descriptor length\n"); 28550 kmem_free(sense, sense_buflen); 28551 kmem_free(select, select_buflen); 28552 return (EIO); 28553 } 28554 sense_page = (uchar_t *) 28555 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 28556 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 28557 select_mhp->length = 0; 28558 select_mhp->bdesc_length = 0; 28559 } 28560 /* 28561 * Note: An audio control data structure could be created and overlayed 28562 * on the following in place of the array indexing method implemented. 28563 */ 28564 28565 /* Build the select data for the user volume data */ 28566 select_page[0] = MODEPAGE_AUDIO_CTRL; 28567 select_page[1] = 0xE; 28568 /* Set the immediate bit */ 28569 select_page[2] = 0x04; 28570 /* Zero out reserved fields */ 28571 select_page[3] = 0x00; 28572 select_page[4] = 0x00; 28573 /* Return sense data for fields not to be modified */ 28574 select_page[5] = sense_page[5]; 28575 select_page[6] = sense_page[6]; 28576 select_page[7] = sense_page[7]; 28577 /* Set the user specified volume levels for channel 0 and 1 */ 28578 select_page[8] = 0x01; 28579 select_page[9] = vol->channel0; 28580 select_page[10] = 0x02; 28581 select_page[11] = vol->channel1; 28582 /* Channel 2 and 3 are currently unsupported so return the sense data */ 28583 select_page[12] = sense_page[12]; 28584 select_page[13] = sense_page[13]; 28585 select_page[14] = sense_page[14]; 28586 select_page[15] = sense_page[15]; 28587 28588 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 28589 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select, 28590 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 28591 } else { 28592 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 28593 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 28594 } 28595 28596 kmem_free(sense, sense_buflen); 28597 kmem_free(select, select_buflen); 28598 return (rval); 28599 } 28600 28601 28602 /* 28603 * Function: sr_read_sony_session_offset() 28604 * 28605 * Description: This routine is the driver entry point for handling CD-ROM 28606 * ioctl requests for session offset information. (CDROMREADOFFSET) 28607 * The address of the first track in the last session of a 28608 * multi-session CD-ROM is returned 28609 * 28610 * Note: This routine uses a vendor specific key value in the 28611 * command control field without implementing any vendor check here 28612 * or in the ioctl routine. 28613 * 28614 * Arguments: dev - the device 'dev_t' 28615 * data - pointer to an int to hold the requested address 28616 * flag - this argument is a pass through to ddi_copyxxx() 28617 * directly from the mode argument of ioctl(). 28618 * 28619 * Return Code: the code returned by sd_send_scsi_cmd() 28620 * EFAULT if ddi_copyxxx() fails 28621 * ENXIO if fail ddi_get_soft_state 28622 * EINVAL if data pointer is NULL 28623 */ 28624 28625 static int 28626 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 28627 { 28628 struct sd_lun *un; 28629 struct uscsi_cmd *com; 28630 caddr_t buffer; 28631 char cdb[CDB_GROUP1]; 28632 int session_offset = 0; 28633 int rval; 28634 28635 if (data == NULL) { 28636 return (EINVAL); 28637 } 28638 28639 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28640 (un->un_state == SD_STATE_OFFLINE)) { 28641 return (ENXIO); 28642 } 28643 28644 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 28645 bzero(cdb, CDB_GROUP1); 28646 cdb[0] = SCMD_READ_TOC; 28647 /* 28648 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 28649 * (4 byte TOC response header + 8 byte response data) 28650 */ 28651 cdb[8] = SONY_SESSION_OFFSET_LEN; 28652 /* Byte 9 is the control byte. A vendor specific value is used */ 28653 cdb[9] = SONY_SESSION_OFFSET_KEY; 28654 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28655 com->uscsi_cdb = cdb; 28656 com->uscsi_cdblen = CDB_GROUP1; 28657 com->uscsi_bufaddr = buffer; 28658 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 28659 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28660 28661 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28662 UIO_SYSSPACE, SD_PATH_STANDARD); 28663 if (rval != 0) { 28664 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 28665 kmem_free(com, sizeof (*com)); 28666 return (rval); 28667 } 28668 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 28669 session_offset = 28670 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 28671 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 28672 /* 28673 * Offset returned offset in current lbasize block's. Convert to 28674 * 2k block's to return to the user 28675 */ 28676 if (un->un_tgt_blocksize == CDROM_BLK_512) { 28677 session_offset >>= 2; 28678 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 28679 session_offset >>= 1; 28680 } 28681 } 28682 28683 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 28684 rval = EFAULT; 28685 } 28686 28687 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 28688 kmem_free(com, sizeof (*com)); 28689 return (rval); 28690 } 28691 28692 28693 /* 28694 * Function: sd_wm_cache_constructor() 28695 * 28696 * Description: Cache Constructor for the wmap cache for the read/modify/write 28697 * devices. 28698 * 28699 * Arguments: wm - A pointer to the sd_w_map to be initialized. 28700 * un - sd_lun structure for the device. 28701 * flag - the km flags passed to constructor 28702 * 28703 * Return Code: 0 on success. 28704 * -1 on failure. 28705 */ 28706 28707 /*ARGSUSED*/ 28708 static int 28709 sd_wm_cache_constructor(void *wm, void *un, int flags) 28710 { 28711 bzero(wm, sizeof (struct sd_w_map)); 28712 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 28713 return (0); 28714 } 28715 28716 28717 /* 28718 * Function: sd_wm_cache_destructor() 28719 * 28720 * Description: Cache destructor for the wmap cache for the read/modify/write 28721 * devices. 28722 * 28723 * Arguments: wm - A pointer to the sd_w_map to be initialized. 28724 * un - sd_lun structure for the device. 28725 */ 28726 /*ARGSUSED*/ 28727 static void 28728 sd_wm_cache_destructor(void *wm, void *un) 28729 { 28730 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 28731 } 28732 28733 28734 /* 28735 * Function: sd_range_lock() 28736 * 28737 * Description: Lock the range of blocks specified as parameter to ensure 28738 * that read, modify write is atomic and no other i/o writes 28739 * to the same location. The range is specified in terms 28740 * of start and end blocks. Block numbers are the actual 28741 * media block numbers and not system. 28742 * 28743 * Arguments: un - sd_lun structure for the device. 28744 * startb - The starting block number 28745 * endb - The end block number 28746 * typ - type of i/o - simple/read_modify_write 28747 * 28748 * Return Code: wm - pointer to the wmap structure. 28749 * 28750 * Context: This routine can sleep. 28751 */ 28752 28753 static struct sd_w_map * 28754 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 28755 { 28756 struct sd_w_map *wmp = NULL; 28757 struct sd_w_map *sl_wmp = NULL; 28758 struct sd_w_map *tmp_wmp; 28759 wm_state state = SD_WM_CHK_LIST; 28760 28761 28762 ASSERT(un != NULL); 28763 ASSERT(!mutex_owned(SD_MUTEX(un))); 28764 28765 mutex_enter(SD_MUTEX(un)); 28766 28767 while (state != SD_WM_DONE) { 28768 28769 switch (state) { 28770 case SD_WM_CHK_LIST: 28771 /* 28772 * This is the starting state. Check the wmap list 28773 * to see if the range is currently available. 28774 */ 28775 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 28776 /* 28777 * If this is a simple write and no rmw 28778 * i/o is pending then try to lock the 28779 * range as the range should be available. 28780 */ 28781 state = SD_WM_LOCK_RANGE; 28782 } else { 28783 tmp_wmp = sd_get_range(un, startb, endb); 28784 if (tmp_wmp != NULL) { 28785 if ((wmp != NULL) && ONLIST(un, wmp)) { 28786 /* 28787 * Should not keep onlist wmps 28788 * while waiting this macro 28789 * will also do wmp = NULL; 28790 */ 28791 FREE_ONLIST_WMAP(un, wmp); 28792 } 28793 /* 28794 * sl_wmp is the wmap on which wait 28795 * is done, since the tmp_wmp points 28796 * to the inuse wmap, set sl_wmp to 28797 * tmp_wmp and change the state to sleep 28798 */ 28799 sl_wmp = tmp_wmp; 28800 state = SD_WM_WAIT_MAP; 28801 } else { 28802 state = SD_WM_LOCK_RANGE; 28803 } 28804 28805 } 28806 break; 28807 28808 case SD_WM_LOCK_RANGE: 28809 ASSERT(un->un_wm_cache); 28810 /* 28811 * The range need to be locked, try to get a wmap. 28812 * First attempt it with NO_SLEEP, want to avoid a sleep 28813 * if possible as we will have to release the sd mutex 28814 * if we have to sleep. 28815 */ 28816 if (wmp == NULL) 28817 wmp = kmem_cache_alloc(un->un_wm_cache, 28818 KM_NOSLEEP); 28819 if (wmp == NULL) { 28820 mutex_exit(SD_MUTEX(un)); 28821 _NOTE(DATA_READABLE_WITHOUT_LOCK 28822 (sd_lun::un_wm_cache)) 28823 wmp = kmem_cache_alloc(un->un_wm_cache, 28824 KM_SLEEP); 28825 mutex_enter(SD_MUTEX(un)); 28826 /* 28827 * we released the mutex so recheck and go to 28828 * check list state. 28829 */ 28830 state = SD_WM_CHK_LIST; 28831 } else { 28832 /* 28833 * We exit out of state machine since we 28834 * have the wmap. Do the housekeeping first. 28835 * place the wmap on the wmap list if it is not 28836 * on it already and then set the state to done. 28837 */ 28838 wmp->wm_start = startb; 28839 wmp->wm_end = endb; 28840 wmp->wm_flags = typ | SD_WM_BUSY; 28841 if (typ & SD_WTYPE_RMW) { 28842 un->un_rmw_count++; 28843 } 28844 /* 28845 * If not already on the list then link 28846 */ 28847 if (!ONLIST(un, wmp)) { 28848 wmp->wm_next = un->un_wm; 28849 wmp->wm_prev = NULL; 28850 if (wmp->wm_next) 28851 wmp->wm_next->wm_prev = wmp; 28852 un->un_wm = wmp; 28853 } 28854 state = SD_WM_DONE; 28855 } 28856 break; 28857 28858 case SD_WM_WAIT_MAP: 28859 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 28860 /* 28861 * Wait is done on sl_wmp, which is set in the 28862 * check_list state. 28863 */ 28864 sl_wmp->wm_wanted_count++; 28865 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 28866 sl_wmp->wm_wanted_count--; 28867 if (!(sl_wmp->wm_flags & SD_WM_BUSY)) { 28868 if (wmp != NULL) 28869 CHK_N_FREEWMP(un, wmp); 28870 wmp = sl_wmp; 28871 } 28872 sl_wmp = NULL; 28873 /* 28874 * After waking up, need to recheck for availability of 28875 * range. 28876 */ 28877 state = SD_WM_CHK_LIST; 28878 break; 28879 28880 default: 28881 panic("sd_range_lock: " 28882 "Unknown state %d in sd_range_lock", state); 28883 /*NOTREACHED*/ 28884 } /* switch(state) */ 28885 28886 } /* while(state != SD_WM_DONE) */ 28887 28888 mutex_exit(SD_MUTEX(un)); 28889 28890 ASSERT(wmp != NULL); 28891 28892 return (wmp); 28893 } 28894 28895 28896 /* 28897 * Function: sd_get_range() 28898 * 28899 * Description: Find if there any overlapping I/O to this one 28900 * Returns the write-map of 1st such I/O, NULL otherwise. 28901 * 28902 * Arguments: un - sd_lun structure for the device. 28903 * startb - The starting block number 28904 * endb - The end block number 28905 * 28906 * Return Code: wm - pointer to the wmap structure. 28907 */ 28908 28909 static struct sd_w_map * 28910 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 28911 { 28912 struct sd_w_map *wmp; 28913 28914 ASSERT(un != NULL); 28915 28916 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 28917 if (!(wmp->wm_flags & SD_WM_BUSY)) { 28918 continue; 28919 } 28920 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 28921 break; 28922 } 28923 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 28924 break; 28925 } 28926 } 28927 28928 return (wmp); 28929 } 28930 28931 28932 /* 28933 * Function: sd_free_inlist_wmap() 28934 * 28935 * Description: Unlink and free a write map struct. 28936 * 28937 * Arguments: un - sd_lun structure for the device. 28938 * wmp - sd_w_map which needs to be unlinked. 28939 */ 28940 28941 static void 28942 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 28943 { 28944 ASSERT(un != NULL); 28945 28946 if (un->un_wm == wmp) { 28947 un->un_wm = wmp->wm_next; 28948 } else { 28949 wmp->wm_prev->wm_next = wmp->wm_next; 28950 } 28951 28952 if (wmp->wm_next) { 28953 wmp->wm_next->wm_prev = wmp->wm_prev; 28954 } 28955 28956 wmp->wm_next = wmp->wm_prev = NULL; 28957 28958 kmem_cache_free(un->un_wm_cache, wmp); 28959 } 28960 28961 28962 /* 28963 * Function: sd_range_unlock() 28964 * 28965 * Description: Unlock the range locked by wm. 28966 * Free write map if nobody else is waiting on it. 28967 * 28968 * Arguments: un - sd_lun structure for the device. 28969 * wmp - sd_w_map which needs to be unlinked. 28970 */ 28971 28972 static void 28973 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 28974 { 28975 ASSERT(un != NULL); 28976 ASSERT(wm != NULL); 28977 ASSERT(!mutex_owned(SD_MUTEX(un))); 28978 28979 mutex_enter(SD_MUTEX(un)); 28980 28981 if (wm->wm_flags & SD_WTYPE_RMW) { 28982 un->un_rmw_count--; 28983 } 28984 28985 if (wm->wm_wanted_count) { 28986 wm->wm_flags = 0; 28987 /* 28988 * Broadcast that the wmap is available now. 28989 */ 28990 cv_broadcast(&wm->wm_avail); 28991 } else { 28992 /* 28993 * If no one is waiting on the map, it should be free'ed. 28994 */ 28995 sd_free_inlist_wmap(un, wm); 28996 } 28997 28998 mutex_exit(SD_MUTEX(un)); 28999 } 29000 29001 29002 /* 29003 * Function: sd_read_modify_write_task 29004 * 29005 * Description: Called from a taskq thread to initiate the write phase of 29006 * a read-modify-write request. This is used for targets where 29007 * un->un_sys_blocksize != un->un_tgt_blocksize. 29008 * 29009 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 29010 * 29011 * Context: Called under taskq thread context. 29012 */ 29013 29014 static void 29015 sd_read_modify_write_task(void *arg) 29016 { 29017 struct sd_mapblocksize_info *bsp; 29018 struct buf *bp; 29019 struct sd_xbuf *xp; 29020 struct sd_lun *un; 29021 29022 bp = arg; /* The bp is given in arg */ 29023 ASSERT(bp != NULL); 29024 29025 /* Get the pointer to the layer-private data struct */ 29026 xp = SD_GET_XBUF(bp); 29027 ASSERT(xp != NULL); 29028 bsp = xp->xb_private; 29029 ASSERT(bsp != NULL); 29030 29031 un = SD_GET_UN(bp); 29032 ASSERT(un != NULL); 29033 ASSERT(!mutex_owned(SD_MUTEX(un))); 29034 29035 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29036 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 29037 29038 /* 29039 * This is the write phase of a read-modify-write request, called 29040 * under the context of a taskq thread in response to the completion 29041 * of the read portion of the rmw request completing under interrupt 29042 * context. The write request must be sent from here down the iostart 29043 * chain as if it were being sent from sd_mapblocksize_iostart(), so 29044 * we use the layer index saved in the layer-private data area. 29045 */ 29046 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 29047 29048 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29049 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 29050 } 29051 29052 29053 /* 29054 * Function: sddump_do_read_of_rmw() 29055 * 29056 * Description: This routine will be called from sddump, If sddump is called 29057 * with an I/O which not aligned on device blocksize boundary 29058 * then the write has to be converted to read-modify-write. 29059 * Do the read part here in order to keep sddump simple. 29060 * Note - That the sd_mutex is held across the call to this 29061 * routine. 29062 * 29063 * Arguments: un - sd_lun 29064 * blkno - block number in terms of media block size. 29065 * nblk - number of blocks. 29066 * bpp - pointer to pointer to the buf structure. On return 29067 * from this function, *bpp points to the valid buffer 29068 * to which the write has to be done. 29069 * 29070 * Return Code: 0 for success or errno-type return code 29071 */ 29072 29073 static int 29074 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 29075 struct buf **bpp) 29076 { 29077 int err; 29078 int i; 29079 int rval; 29080 struct buf *bp; 29081 struct scsi_pkt *pkt = NULL; 29082 uint32_t target_blocksize; 29083 29084 ASSERT(un != NULL); 29085 ASSERT(mutex_owned(SD_MUTEX(un))); 29086 29087 target_blocksize = un->un_tgt_blocksize; 29088 29089 mutex_exit(SD_MUTEX(un)); 29090 29091 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 29092 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 29093 if (bp == NULL) { 29094 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29095 "no resources for dumping; giving up"); 29096 err = ENOMEM; 29097 goto done; 29098 } 29099 29100 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 29101 blkno, nblk); 29102 if (rval != 0) { 29103 scsi_free_consistent_buf(bp); 29104 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29105 "no resources for dumping; giving up"); 29106 err = ENOMEM; 29107 goto done; 29108 } 29109 29110 pkt->pkt_flags |= FLAG_NOINTR; 29111 29112 err = EIO; 29113 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 29114 29115 /* 29116 * Scsi_poll returns 0 (success) if the command completes and 29117 * the status block is STATUS_GOOD. We should only check 29118 * errors if this condition is not true. Even then we should 29119 * send our own request sense packet only if we have a check 29120 * condition and auto request sense has not been performed by 29121 * the hba. 29122 */ 29123 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 29124 29125 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 29126 err = 0; 29127 break; 29128 } 29129 29130 /* 29131 * Check CMD_DEV_GONE 1st, give up if device is gone, 29132 * no need to read RQS data. 29133 */ 29134 if (pkt->pkt_reason == CMD_DEV_GONE) { 29135 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29136 "Device is gone\n"); 29137 break; 29138 } 29139 29140 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 29141 SD_INFO(SD_LOG_DUMP, un, 29142 "sddump: read failed with CHECK, try # %d\n", i); 29143 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 29144 (void) sd_send_polled_RQS(un); 29145 } 29146 29147 continue; 29148 } 29149 29150 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 29151 int reset_retval = 0; 29152 29153 SD_INFO(SD_LOG_DUMP, un, 29154 "sddump: read failed with BUSY, try # %d\n", i); 29155 29156 if (un->un_f_lun_reset_enabled == TRUE) { 29157 reset_retval = scsi_reset(SD_ADDRESS(un), 29158 RESET_LUN); 29159 } 29160 if (reset_retval == 0) { 29161 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 29162 } 29163 (void) sd_send_polled_RQS(un); 29164 29165 } else { 29166 SD_INFO(SD_LOG_DUMP, un, 29167 "sddump: read failed with 0x%x, try # %d\n", 29168 SD_GET_PKT_STATUS(pkt), i); 29169 mutex_enter(SD_MUTEX(un)); 29170 sd_reset_target(un, pkt); 29171 mutex_exit(SD_MUTEX(un)); 29172 } 29173 29174 /* 29175 * If we are not getting anywhere with lun/target resets, 29176 * let's reset the bus. 29177 */ 29178 if (i > SD_NDUMP_RETRIES/2) { 29179 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 29180 (void) sd_send_polled_RQS(un); 29181 } 29182 29183 } 29184 scsi_destroy_pkt(pkt); 29185 29186 if (err != 0) { 29187 scsi_free_consistent_buf(bp); 29188 *bpp = NULL; 29189 } else { 29190 *bpp = bp; 29191 } 29192 29193 done: 29194 mutex_enter(SD_MUTEX(un)); 29195 return (err); 29196 } 29197 29198 29199 /* 29200 * Function: sd_failfast_flushq 29201 * 29202 * Description: Take all bp's on the wait queue that have B_FAILFAST set 29203 * in b_flags and move them onto the failfast queue, then kick 29204 * off a thread to return all bp's on the failfast queue to 29205 * their owners with an error set. 29206 * 29207 * Arguments: un - pointer to the soft state struct for the instance. 29208 * 29209 * Context: may execute in interrupt context. 29210 */ 29211 29212 static void 29213 sd_failfast_flushq(struct sd_lun *un) 29214 { 29215 struct buf *bp; 29216 struct buf *next_waitq_bp; 29217 struct buf *prev_waitq_bp = NULL; 29218 29219 ASSERT(un != NULL); 29220 ASSERT(mutex_owned(SD_MUTEX(un))); 29221 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 29222 ASSERT(un->un_failfast_bp == NULL); 29223 29224 SD_TRACE(SD_LOG_IO_FAILFAST, un, 29225 "sd_failfast_flushq: entry: un:0x%p\n", un); 29226 29227 /* 29228 * Check if we should flush all bufs when entering failfast state, or 29229 * just those with B_FAILFAST set. 29230 */ 29231 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 29232 /* 29233 * Move *all* bp's on the wait queue to the failfast flush 29234 * queue, including those that do NOT have B_FAILFAST set. 29235 */ 29236 if (un->un_failfast_headp == NULL) { 29237 ASSERT(un->un_failfast_tailp == NULL); 29238 un->un_failfast_headp = un->un_waitq_headp; 29239 } else { 29240 ASSERT(un->un_failfast_tailp != NULL); 29241 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 29242 } 29243 29244 un->un_failfast_tailp = un->un_waitq_tailp; 29245 un->un_waitq_headp = un->un_waitq_tailp = NULL; 29246 29247 } else { 29248 /* 29249 * Go thru the wait queue, pick off all entries with 29250 * B_FAILFAST set, and move these onto the failfast queue. 29251 */ 29252 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 29253 /* 29254 * Save the pointer to the next bp on the wait queue, 29255 * so we get to it on the next iteration of this loop. 29256 */ 29257 next_waitq_bp = bp->av_forw; 29258 29259 /* 29260 * If this bp from the wait queue does NOT have 29261 * B_FAILFAST set, just move on to the next element 29262 * in the wait queue. Note, this is the only place 29263 * where it is correct to set prev_waitq_bp. 29264 */ 29265 if ((bp->b_flags & B_FAILFAST) == 0) { 29266 prev_waitq_bp = bp; 29267 continue; 29268 } 29269 29270 /* 29271 * Remove the bp from the wait queue. 29272 */ 29273 if (bp == un->un_waitq_headp) { 29274 /* The bp is the first element of the waitq. */ 29275 un->un_waitq_headp = next_waitq_bp; 29276 if (un->un_waitq_headp == NULL) { 29277 /* The wait queue is now empty */ 29278 un->un_waitq_tailp = NULL; 29279 } 29280 } else { 29281 /* 29282 * The bp is either somewhere in the middle 29283 * or at the end of the wait queue. 29284 */ 29285 ASSERT(un->un_waitq_headp != NULL); 29286 ASSERT(prev_waitq_bp != NULL); 29287 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 29288 == 0); 29289 if (bp == un->un_waitq_tailp) { 29290 /* bp is the last entry on the waitq. */ 29291 ASSERT(next_waitq_bp == NULL); 29292 un->un_waitq_tailp = prev_waitq_bp; 29293 } 29294 prev_waitq_bp->av_forw = next_waitq_bp; 29295 } 29296 bp->av_forw = NULL; 29297 29298 /* 29299 * Now put the bp onto the failfast queue. 29300 */ 29301 if (un->un_failfast_headp == NULL) { 29302 /* failfast queue is currently empty */ 29303 ASSERT(un->un_failfast_tailp == NULL); 29304 un->un_failfast_headp = 29305 un->un_failfast_tailp = bp; 29306 } else { 29307 /* Add the bp to the end of the failfast q */ 29308 ASSERT(un->un_failfast_tailp != NULL); 29309 ASSERT(un->un_failfast_tailp->b_flags & 29310 B_FAILFAST); 29311 un->un_failfast_tailp->av_forw = bp; 29312 un->un_failfast_tailp = bp; 29313 } 29314 } 29315 } 29316 29317 /* 29318 * Now return all bp's on the failfast queue to their owners. 29319 */ 29320 while ((bp = un->un_failfast_headp) != NULL) { 29321 29322 un->un_failfast_headp = bp->av_forw; 29323 if (un->un_failfast_headp == NULL) { 29324 un->un_failfast_tailp = NULL; 29325 } 29326 29327 /* 29328 * We want to return the bp with a failure error code, but 29329 * we do not want a call to sd_start_cmds() to occur here, 29330 * so use sd_return_failed_command_no_restart() instead of 29331 * sd_return_failed_command(). 29332 */ 29333 sd_return_failed_command_no_restart(un, bp, EIO); 29334 } 29335 29336 /* Flush the xbuf queues if required. */ 29337 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 29338 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 29339 } 29340 29341 SD_TRACE(SD_LOG_IO_FAILFAST, un, 29342 "sd_failfast_flushq: exit: un:0x%p\n", un); 29343 } 29344 29345 29346 /* 29347 * Function: sd_failfast_flushq_callback 29348 * 29349 * Description: Return TRUE if the given bp meets the criteria for failfast 29350 * flushing. Used with ddi_xbuf_flushq(9F). 29351 * 29352 * Arguments: bp - ptr to buf struct to be examined. 29353 * 29354 * Context: Any 29355 */ 29356 29357 static int 29358 sd_failfast_flushq_callback(struct buf *bp) 29359 { 29360 /* 29361 * Return TRUE if (1) we want to flush ALL bufs when the failfast 29362 * state is entered; OR (2) the given bp has B_FAILFAST set. 29363 */ 29364 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 29365 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 29366 } 29367 29368 29369 #if defined(__i386) || defined(__amd64) 29370 /* 29371 * Function: sd_setup_next_xfer 29372 * 29373 * Description: Prepare next I/O operation using DMA_PARTIAL 29374 * 29375 */ 29376 29377 static int 29378 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 29379 struct scsi_pkt *pkt, struct sd_xbuf *xp) 29380 { 29381 ssize_t num_blks_not_xfered; 29382 daddr_t strt_blk_num; 29383 ssize_t bytes_not_xfered; 29384 int rval; 29385 29386 ASSERT(pkt->pkt_resid == 0); 29387 29388 /* 29389 * Calculate next block number and amount to be transferred. 29390 * 29391 * How much data NOT transfered to the HBA yet. 29392 */ 29393 bytes_not_xfered = xp->xb_dma_resid; 29394 29395 /* 29396 * figure how many blocks NOT transfered to the HBA yet. 29397 */ 29398 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 29399 29400 /* 29401 * set starting block number to the end of what WAS transfered. 29402 */ 29403 strt_blk_num = xp->xb_blkno + 29404 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 29405 29406 /* 29407 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 29408 * will call scsi_initpkt with NULL_FUNC so we do not have to release 29409 * the disk mutex here. 29410 */ 29411 rval = sd_setup_next_rw_pkt(un, pkt, bp, 29412 strt_blk_num, num_blks_not_xfered); 29413 29414 if (rval == 0) { 29415 29416 /* 29417 * Success. 29418 * 29419 * Adjust things if there are still more blocks to be 29420 * transfered. 29421 */ 29422 xp->xb_dma_resid = pkt->pkt_resid; 29423 pkt->pkt_resid = 0; 29424 29425 return (1); 29426 } 29427 29428 /* 29429 * There's really only one possible return value from 29430 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 29431 * returns NULL. 29432 */ 29433 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 29434 29435 bp->b_resid = bp->b_bcount; 29436 bp->b_flags |= B_ERROR; 29437 29438 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29439 "Error setting up next portion of DMA transfer\n"); 29440 29441 return (0); 29442 } 29443 #endif 29444 29445 /* 29446 * Note: The following sd_faultinjection_ioctl( ) routines implement 29447 * driver support for handling fault injection for error analysis 29448 * causing faults in multiple layers of the driver. 29449 * 29450 */ 29451 29452 #ifdef SD_FAULT_INJECTION 29453 static uint_t sd_fault_injection_on = 0; 29454 29455 /* 29456 * Function: sd_faultinjection_ioctl() 29457 * 29458 * Description: This routine is the driver entry point for handling 29459 * faultinjection ioctls to inject errors into the 29460 * layer model 29461 * 29462 * Arguments: cmd - the ioctl cmd recieved 29463 * arg - the arguments from user and returns 29464 */ 29465 29466 static void 29467 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 29468 29469 uint_t i; 29470 uint_t rval; 29471 29472 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 29473 29474 mutex_enter(SD_MUTEX(un)); 29475 29476 switch (cmd) { 29477 case SDIOCRUN: 29478 /* Allow pushed faults to be injected */ 29479 SD_INFO(SD_LOG_SDTEST, un, 29480 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 29481 29482 sd_fault_injection_on = 1; 29483 29484 SD_INFO(SD_LOG_IOERR, un, 29485 "sd_faultinjection_ioctl: run finished\n"); 29486 break; 29487 29488 case SDIOCSTART: 29489 /* Start Injection Session */ 29490 SD_INFO(SD_LOG_SDTEST, un, 29491 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 29492 29493 sd_fault_injection_on = 0; 29494 un->sd_injection_mask = 0xFFFFFFFF; 29495 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 29496 un->sd_fi_fifo_pkt[i] = NULL; 29497 un->sd_fi_fifo_xb[i] = NULL; 29498 un->sd_fi_fifo_un[i] = NULL; 29499 un->sd_fi_fifo_arq[i] = NULL; 29500 } 29501 un->sd_fi_fifo_start = 0; 29502 un->sd_fi_fifo_end = 0; 29503 29504 mutex_enter(&(un->un_fi_mutex)); 29505 un->sd_fi_log[0] = '\0'; 29506 un->sd_fi_buf_len = 0; 29507 mutex_exit(&(un->un_fi_mutex)); 29508 29509 SD_INFO(SD_LOG_IOERR, un, 29510 "sd_faultinjection_ioctl: start finished\n"); 29511 break; 29512 29513 case SDIOCSTOP: 29514 /* Stop Injection Session */ 29515 SD_INFO(SD_LOG_SDTEST, un, 29516 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 29517 sd_fault_injection_on = 0; 29518 un->sd_injection_mask = 0x0; 29519 29520 /* Empty stray or unuseds structs from fifo */ 29521 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 29522 if (un->sd_fi_fifo_pkt[i] != NULL) { 29523 kmem_free(un->sd_fi_fifo_pkt[i], 29524 sizeof (struct sd_fi_pkt)); 29525 } 29526 if (un->sd_fi_fifo_xb[i] != NULL) { 29527 kmem_free(un->sd_fi_fifo_xb[i], 29528 sizeof (struct sd_fi_xb)); 29529 } 29530 if (un->sd_fi_fifo_un[i] != NULL) { 29531 kmem_free(un->sd_fi_fifo_un[i], 29532 sizeof (struct sd_fi_un)); 29533 } 29534 if (un->sd_fi_fifo_arq[i] != NULL) { 29535 kmem_free(un->sd_fi_fifo_arq[i], 29536 sizeof (struct sd_fi_arq)); 29537 } 29538 un->sd_fi_fifo_pkt[i] = NULL; 29539 un->sd_fi_fifo_un[i] = NULL; 29540 un->sd_fi_fifo_xb[i] = NULL; 29541 un->sd_fi_fifo_arq[i] = NULL; 29542 } 29543 un->sd_fi_fifo_start = 0; 29544 un->sd_fi_fifo_end = 0; 29545 29546 SD_INFO(SD_LOG_IOERR, un, 29547 "sd_faultinjection_ioctl: stop finished\n"); 29548 break; 29549 29550 case SDIOCINSERTPKT: 29551 /* Store a packet struct to be pushed onto fifo */ 29552 SD_INFO(SD_LOG_SDTEST, un, 29553 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 29554 29555 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 29556 29557 sd_fault_injection_on = 0; 29558 29559 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 29560 if (un->sd_fi_fifo_pkt[i] != NULL) { 29561 kmem_free(un->sd_fi_fifo_pkt[i], 29562 sizeof (struct sd_fi_pkt)); 29563 } 29564 if (arg != NULL) { 29565 un->sd_fi_fifo_pkt[i] = 29566 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 29567 if (un->sd_fi_fifo_pkt[i] == NULL) { 29568 /* Alloc failed don't store anything */ 29569 break; 29570 } 29571 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 29572 sizeof (struct sd_fi_pkt), 0); 29573 if (rval == -1) { 29574 kmem_free(un->sd_fi_fifo_pkt[i], 29575 sizeof (struct sd_fi_pkt)); 29576 un->sd_fi_fifo_pkt[i] = NULL; 29577 } 29578 } else { 29579 SD_INFO(SD_LOG_IOERR, un, 29580 "sd_faultinjection_ioctl: pkt null\n"); 29581 } 29582 break; 29583 29584 case SDIOCINSERTXB: 29585 /* Store a xb struct to be pushed onto fifo */ 29586 SD_INFO(SD_LOG_SDTEST, un, 29587 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 29588 29589 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 29590 29591 sd_fault_injection_on = 0; 29592 29593 if (un->sd_fi_fifo_xb[i] != NULL) { 29594 kmem_free(un->sd_fi_fifo_xb[i], 29595 sizeof (struct sd_fi_xb)); 29596 un->sd_fi_fifo_xb[i] = NULL; 29597 } 29598 if (arg != NULL) { 29599 un->sd_fi_fifo_xb[i] = 29600 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 29601 if (un->sd_fi_fifo_xb[i] == NULL) { 29602 /* Alloc failed don't store anything */ 29603 break; 29604 } 29605 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 29606 sizeof (struct sd_fi_xb), 0); 29607 29608 if (rval == -1) { 29609 kmem_free(un->sd_fi_fifo_xb[i], 29610 sizeof (struct sd_fi_xb)); 29611 un->sd_fi_fifo_xb[i] = NULL; 29612 } 29613 } else { 29614 SD_INFO(SD_LOG_IOERR, un, 29615 "sd_faultinjection_ioctl: xb null\n"); 29616 } 29617 break; 29618 29619 case SDIOCINSERTUN: 29620 /* Store a un struct to be pushed onto fifo */ 29621 SD_INFO(SD_LOG_SDTEST, un, 29622 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 29623 29624 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 29625 29626 sd_fault_injection_on = 0; 29627 29628 if (un->sd_fi_fifo_un[i] != NULL) { 29629 kmem_free(un->sd_fi_fifo_un[i], 29630 sizeof (struct sd_fi_un)); 29631 un->sd_fi_fifo_un[i] = NULL; 29632 } 29633 if (arg != NULL) { 29634 un->sd_fi_fifo_un[i] = 29635 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 29636 if (un->sd_fi_fifo_un[i] == NULL) { 29637 /* Alloc failed don't store anything */ 29638 break; 29639 } 29640 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 29641 sizeof (struct sd_fi_un), 0); 29642 if (rval == -1) { 29643 kmem_free(un->sd_fi_fifo_un[i], 29644 sizeof (struct sd_fi_un)); 29645 un->sd_fi_fifo_un[i] = NULL; 29646 } 29647 29648 } else { 29649 SD_INFO(SD_LOG_IOERR, un, 29650 "sd_faultinjection_ioctl: un null\n"); 29651 } 29652 29653 break; 29654 29655 case SDIOCINSERTARQ: 29656 /* Store a arq struct to be pushed onto fifo */ 29657 SD_INFO(SD_LOG_SDTEST, un, 29658 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 29659 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 29660 29661 sd_fault_injection_on = 0; 29662 29663 if (un->sd_fi_fifo_arq[i] != NULL) { 29664 kmem_free(un->sd_fi_fifo_arq[i], 29665 sizeof (struct sd_fi_arq)); 29666 un->sd_fi_fifo_arq[i] = NULL; 29667 } 29668 if (arg != NULL) { 29669 un->sd_fi_fifo_arq[i] = 29670 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 29671 if (un->sd_fi_fifo_arq[i] == NULL) { 29672 /* Alloc failed don't store anything */ 29673 break; 29674 } 29675 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 29676 sizeof (struct sd_fi_arq), 0); 29677 if (rval == -1) { 29678 kmem_free(un->sd_fi_fifo_arq[i], 29679 sizeof (struct sd_fi_arq)); 29680 un->sd_fi_fifo_arq[i] = NULL; 29681 } 29682 29683 } else { 29684 SD_INFO(SD_LOG_IOERR, un, 29685 "sd_faultinjection_ioctl: arq null\n"); 29686 } 29687 29688 break; 29689 29690 case SDIOCPUSH: 29691 /* Push stored xb, pkt, un, and arq onto fifo */ 29692 sd_fault_injection_on = 0; 29693 29694 if (arg != NULL) { 29695 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 29696 if (rval != -1 && 29697 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 29698 un->sd_fi_fifo_end += i; 29699 } 29700 } else { 29701 SD_INFO(SD_LOG_IOERR, un, 29702 "sd_faultinjection_ioctl: push arg null\n"); 29703 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 29704 un->sd_fi_fifo_end++; 29705 } 29706 } 29707 SD_INFO(SD_LOG_IOERR, un, 29708 "sd_faultinjection_ioctl: push to end=%d\n", 29709 un->sd_fi_fifo_end); 29710 break; 29711 29712 case SDIOCRETRIEVE: 29713 /* Return buffer of log from Injection session */ 29714 SD_INFO(SD_LOG_SDTEST, un, 29715 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 29716 29717 sd_fault_injection_on = 0; 29718 29719 mutex_enter(&(un->un_fi_mutex)); 29720 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 29721 un->sd_fi_buf_len+1, 0); 29722 mutex_exit(&(un->un_fi_mutex)); 29723 29724 if (rval == -1) { 29725 /* 29726 * arg is possibly invalid setting 29727 * it to NULL for return 29728 */ 29729 arg = NULL; 29730 } 29731 break; 29732 } 29733 29734 mutex_exit(SD_MUTEX(un)); 29735 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 29736 " exit\n"); 29737 } 29738 29739 29740 /* 29741 * Function: sd_injection_log() 29742 * 29743 * Description: This routine adds buff to the already existing injection log 29744 * for retrieval via faultinjection_ioctl for use in fault 29745 * detection and recovery 29746 * 29747 * Arguments: buf - the string to add to the log 29748 */ 29749 29750 static void 29751 sd_injection_log(char *buf, struct sd_lun *un) 29752 { 29753 uint_t len; 29754 29755 ASSERT(un != NULL); 29756 ASSERT(buf != NULL); 29757 29758 mutex_enter(&(un->un_fi_mutex)); 29759 29760 len = min(strlen(buf), 255); 29761 /* Add logged value to Injection log to be returned later */ 29762 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 29763 uint_t offset = strlen((char *)un->sd_fi_log); 29764 char *destp = (char *)un->sd_fi_log + offset; 29765 int i; 29766 for (i = 0; i < len; i++) { 29767 *destp++ = *buf++; 29768 } 29769 un->sd_fi_buf_len += len; 29770 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 29771 } 29772 29773 mutex_exit(&(un->un_fi_mutex)); 29774 } 29775 29776 29777 /* 29778 * Function: sd_faultinjection() 29779 * 29780 * Description: This routine takes the pkt and changes its 29781 * content based on error injection scenerio. 29782 * 29783 * Arguments: pktp - packet to be changed 29784 */ 29785 29786 static void 29787 sd_faultinjection(struct scsi_pkt *pktp) 29788 { 29789 uint_t i; 29790 struct sd_fi_pkt *fi_pkt; 29791 struct sd_fi_xb *fi_xb; 29792 struct sd_fi_un *fi_un; 29793 struct sd_fi_arq *fi_arq; 29794 struct buf *bp; 29795 struct sd_xbuf *xb; 29796 struct sd_lun *un; 29797 29798 ASSERT(pktp != NULL); 29799 29800 /* pull bp xb and un from pktp */ 29801 bp = (struct buf *)pktp->pkt_private; 29802 xb = SD_GET_XBUF(bp); 29803 un = SD_GET_UN(bp); 29804 29805 ASSERT(un != NULL); 29806 29807 mutex_enter(SD_MUTEX(un)); 29808 29809 SD_TRACE(SD_LOG_SDTEST, un, 29810 "sd_faultinjection: entry Injection from sdintr\n"); 29811 29812 /* if injection is off return */ 29813 if (sd_fault_injection_on == 0 || 29814 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 29815 mutex_exit(SD_MUTEX(un)); 29816 return; 29817 } 29818 29819 29820 /* take next set off fifo */ 29821 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 29822 29823 fi_pkt = un->sd_fi_fifo_pkt[i]; 29824 fi_xb = un->sd_fi_fifo_xb[i]; 29825 fi_un = un->sd_fi_fifo_un[i]; 29826 fi_arq = un->sd_fi_fifo_arq[i]; 29827 29828 29829 /* set variables accordingly */ 29830 /* set pkt if it was on fifo */ 29831 if (fi_pkt != NULL) { 29832 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 29833 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 29834 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 29835 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 29836 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 29837 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 29838 29839 } 29840 29841 /* set xb if it was on fifo */ 29842 if (fi_xb != NULL) { 29843 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 29844 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 29845 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 29846 SD_CONDSET(xb, xb, xb_victim_retry_count, 29847 "xb_victim_retry_count"); 29848 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 29849 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 29850 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 29851 29852 /* copy in block data from sense */ 29853 if (fi_xb->xb_sense_data[0] != -1) { 29854 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 29855 SENSE_LENGTH); 29856 } 29857 29858 /* copy in extended sense codes */ 29859 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code, 29860 "es_code"); 29861 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key, 29862 "es_key"); 29863 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code, 29864 "es_add_code"); 29865 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, 29866 es_qual_code, "es_qual_code"); 29867 } 29868 29869 /* set un if it was on fifo */ 29870 if (fi_un != NULL) { 29871 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 29872 SD_CONDSET(un, un, un_ctype, "un_ctype"); 29873 SD_CONDSET(un, un, un_reset_retry_count, 29874 "un_reset_retry_count"); 29875 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 29876 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 29877 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 29878 SD_CONDSET(un, un, un_f_geometry_is_valid, 29879 "un_f_geometry_is_valid"); 29880 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 29881 "un_f_allow_bus_device_reset"); 29882 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 29883 29884 } 29885 29886 /* copy in auto request sense if it was on fifo */ 29887 if (fi_arq != NULL) { 29888 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 29889 } 29890 29891 /* free structs */ 29892 if (un->sd_fi_fifo_pkt[i] != NULL) { 29893 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 29894 } 29895 if (un->sd_fi_fifo_xb[i] != NULL) { 29896 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 29897 } 29898 if (un->sd_fi_fifo_un[i] != NULL) { 29899 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 29900 } 29901 if (un->sd_fi_fifo_arq[i] != NULL) { 29902 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 29903 } 29904 29905 /* 29906 * kmem_free does not gurantee to set to NULL 29907 * since we uses these to determine if we set 29908 * values or not lets confirm they are always 29909 * NULL after free 29910 */ 29911 un->sd_fi_fifo_pkt[i] = NULL; 29912 un->sd_fi_fifo_un[i] = NULL; 29913 un->sd_fi_fifo_xb[i] = NULL; 29914 un->sd_fi_fifo_arq[i] = NULL; 29915 29916 un->sd_fi_fifo_start++; 29917 29918 mutex_exit(SD_MUTEX(un)); 29919 29920 SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 29921 } 29922 29923 #endif /* SD_FAULT_INJECTION */ 29924