1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * sd.c Copyright (C) 1992 Drew Eckhardt 4 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale 5 * 6 * Linux scsi disk driver 7 * Initial versions: Drew Eckhardt 8 * Subsequent revisions: Eric Youngdale 9 * Modification history: 10 * - Drew Eckhardt <drew@colorado.edu> original 11 * - Eric Youngdale <eric@andante.org> add scatter-gather, multiple 12 * outstanding request, and other enhancements. 13 * Support loadable low-level scsi drivers. 14 * - Jirka Hanika <geo@ff.cuni.cz> support more scsi disks using 15 * eight major numbers. 16 * - Richard Gooch <rgooch@atnf.csiro.au> support devfs. 17 * - Torben Mathiasen <tmm@image.dk> Resource allocation fixes in 18 * sd_init and cleanups. 19 * - Alex Davis <letmein@erols.com> Fix problem where partition info 20 * not being read in sd_open. Fix problem where removable media 21 * could be ejected after sd_open. 22 * - Douglas Gilbert <dgilbert@interlog.com> cleanup for lk 2.5.x 23 * - Badari Pulavarty <pbadari@us.ibm.com>, Matthew Wilcox 24 * <willy@debian.org>, Kurt Garloff <garloff@suse.de>: 25 * Support 32k/1M disks. 26 * 27 * Logging policy (needs CONFIG_SCSI_LOGGING defined): 28 * - setting up transfer: SCSI_LOG_HLQUEUE levels 1 and 2 29 * - end of transfer (bh + scsi_lib): SCSI_LOG_HLCOMPLETE level 1 30 * - entering sd_ioctl: SCSI_LOG_IOCTL level 1 31 * - entering other commands: SCSI_LOG_HLQUEUE level 3 32 * Note: when the logging level is set by the user, it must be greater 33 * than the level indicated above to trigger output. 34 */ 35 36 #include <linux/bio-integrity.h> 37 #include <linux/module.h> 38 #include <linux/fs.h> 39 #include <linux/kernel.h> 40 #include <linux/mm.h> 41 #include <linux/bio-integrity.h> 42 #include <linux/hdreg.h> 43 #include <linux/errno.h> 44 #include <linux/idr.h> 45 #include <linux/interrupt.h> 46 #include <linux/init.h> 47 #include <linux/blkdev.h> 48 #include <linux/blkpg.h> 49 #include <linux/blk-pm.h> 50 #include <linux/delay.h> 51 #include <linux/rw_hint.h> 52 #include <linux/major.h> 53 #include <linux/mutex.h> 54 #include <linux/string_helpers.h> 55 #include <linux/slab.h> 56 #include <linux/sed-opal.h> 57 #include <linux/pm_runtime.h> 58 #include <linux/pr.h> 59 #include <linux/t10-pi.h> 60 #include <linux/uaccess.h> 61 #include <asm/unaligned.h> 62 63 #include <scsi/scsi.h> 64 #include <scsi/scsi_cmnd.h> 65 #include <scsi/scsi_dbg.h> 66 #include <scsi/scsi_device.h> 67 #include <scsi/scsi_devinfo.h> 68 #include <scsi/scsi_driver.h> 69 #include <scsi/scsi_eh.h> 70 #include <scsi/scsi_host.h> 71 #include <scsi/scsi_ioctl.h> 72 #include <scsi/scsicam.h> 73 #include <scsi/scsi_common.h> 74 75 #include "sd.h" 76 #include "scsi_priv.h" 77 #include "scsi_logging.h" 78 79 MODULE_AUTHOR("Eric Youngdale"); 80 MODULE_DESCRIPTION("SCSI disk (sd) driver"); 81 MODULE_LICENSE("GPL"); 82 83 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR); 84 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR); 85 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR); 86 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR); 87 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR); 88 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR); 89 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR); 90 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR); 91 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR); 92 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR); 93 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR); 94 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR); 95 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR); 96 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR); 97 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR); 98 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR); 99 MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK); 100 MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD); 101 MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC); 102 MODULE_ALIAS_SCSI_DEVICE(TYPE_ZBC); 103 104 #define SD_MINORS 16 105 106 static void sd_config_discard(struct scsi_disk *sdkp, struct queue_limits *lim, 107 unsigned int mode); 108 static void sd_config_write_same(struct scsi_disk *sdkp, 109 struct queue_limits *lim); 110 static int sd_revalidate_disk(struct gendisk *); 111 static void sd_unlock_native_capacity(struct gendisk *disk); 112 static void sd_shutdown(struct device *); 113 static void scsi_disk_release(struct device *cdev); 114 115 static DEFINE_IDA(sd_index_ida); 116 117 static mempool_t *sd_page_pool; 118 static struct lock_class_key sd_bio_compl_lkclass; 119 120 static const char *sd_cache_types[] = { 121 "write through", "none", "write back", 122 "write back, no read (daft)" 123 }; 124 125 static void sd_set_flush_flag(struct scsi_disk *sdkp, 126 struct queue_limits *lim) 127 { 128 if (sdkp->WCE) { 129 lim->features |= BLK_FEAT_WRITE_CACHE; 130 if (sdkp->DPOFUA) 131 lim->features |= BLK_FEAT_FUA; 132 else 133 lim->features &= ~BLK_FEAT_FUA; 134 } else { 135 lim->features &= ~(BLK_FEAT_WRITE_CACHE | BLK_FEAT_FUA); 136 } 137 } 138 139 static ssize_t 140 cache_type_store(struct device *dev, struct device_attribute *attr, 141 const char *buf, size_t count) 142 { 143 int ct, rcd, wce, sp; 144 struct scsi_disk *sdkp = to_scsi_disk(dev); 145 struct scsi_device *sdp = sdkp->device; 146 char buffer[64]; 147 char *buffer_data; 148 struct scsi_mode_data data; 149 struct scsi_sense_hdr sshdr; 150 static const char temp[] = "temporary "; 151 int len, ret; 152 153 if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC) 154 /* no cache control on RBC devices; theoretically they 155 * can do it, but there's probably so many exceptions 156 * it's not worth the risk */ 157 return -EINVAL; 158 159 if (strncmp(buf, temp, sizeof(temp) - 1) == 0) { 160 buf += sizeof(temp) - 1; 161 sdkp->cache_override = 1; 162 } else { 163 sdkp->cache_override = 0; 164 } 165 166 ct = sysfs_match_string(sd_cache_types, buf); 167 if (ct < 0) 168 return -EINVAL; 169 170 rcd = ct & 0x01 ? 1 : 0; 171 wce = (ct & 0x02) && !sdkp->write_prot ? 1 : 0; 172 173 if (sdkp->cache_override) { 174 struct queue_limits lim; 175 176 sdkp->WCE = wce; 177 sdkp->RCD = rcd; 178 179 lim = queue_limits_start_update(sdkp->disk->queue); 180 sd_set_flush_flag(sdkp, &lim); 181 blk_mq_freeze_queue(sdkp->disk->queue); 182 ret = queue_limits_commit_update(sdkp->disk->queue, &lim); 183 blk_mq_unfreeze_queue(sdkp->disk->queue); 184 if (ret) 185 return ret; 186 return count; 187 } 188 189 if (scsi_mode_sense(sdp, 0x08, 8, 0, buffer, sizeof(buffer), SD_TIMEOUT, 190 sdkp->max_retries, &data, NULL)) 191 return -EINVAL; 192 len = min_t(size_t, sizeof(buffer), data.length - data.header_length - 193 data.block_descriptor_length); 194 buffer_data = buffer + data.header_length + 195 data.block_descriptor_length; 196 buffer_data[2] &= ~0x05; 197 buffer_data[2] |= wce << 2 | rcd; 198 sp = buffer_data[0] & 0x80 ? 1 : 0; 199 buffer_data[0] &= ~0x80; 200 201 /* 202 * Ensure WP, DPOFUA, and RESERVED fields are cleared in 203 * received mode parameter buffer before doing MODE SELECT. 204 */ 205 data.device_specific = 0; 206 207 ret = scsi_mode_select(sdp, 1, sp, buffer_data, len, SD_TIMEOUT, 208 sdkp->max_retries, &data, &sshdr); 209 if (ret) { 210 if (ret > 0 && scsi_sense_valid(&sshdr)) 211 sd_print_sense_hdr(sdkp, &sshdr); 212 return -EINVAL; 213 } 214 sd_revalidate_disk(sdkp->disk); 215 return count; 216 } 217 218 static ssize_t 219 manage_start_stop_show(struct device *dev, 220 struct device_attribute *attr, char *buf) 221 { 222 struct scsi_disk *sdkp = to_scsi_disk(dev); 223 struct scsi_device *sdp = sdkp->device; 224 225 return sysfs_emit(buf, "%u\n", 226 sdp->manage_system_start_stop && 227 sdp->manage_runtime_start_stop && 228 sdp->manage_shutdown); 229 } 230 static DEVICE_ATTR_RO(manage_start_stop); 231 232 static ssize_t 233 manage_system_start_stop_show(struct device *dev, 234 struct device_attribute *attr, char *buf) 235 { 236 struct scsi_disk *sdkp = to_scsi_disk(dev); 237 struct scsi_device *sdp = sdkp->device; 238 239 return sysfs_emit(buf, "%u\n", sdp->manage_system_start_stop); 240 } 241 242 static ssize_t 243 manage_system_start_stop_store(struct device *dev, 244 struct device_attribute *attr, 245 const char *buf, size_t count) 246 { 247 struct scsi_disk *sdkp = to_scsi_disk(dev); 248 struct scsi_device *sdp = sdkp->device; 249 bool v; 250 251 if (!capable(CAP_SYS_ADMIN)) 252 return -EACCES; 253 254 if (kstrtobool(buf, &v)) 255 return -EINVAL; 256 257 sdp->manage_system_start_stop = v; 258 259 return count; 260 } 261 static DEVICE_ATTR_RW(manage_system_start_stop); 262 263 static ssize_t 264 manage_runtime_start_stop_show(struct device *dev, 265 struct device_attribute *attr, char *buf) 266 { 267 struct scsi_disk *sdkp = to_scsi_disk(dev); 268 struct scsi_device *sdp = sdkp->device; 269 270 return sysfs_emit(buf, "%u\n", sdp->manage_runtime_start_stop); 271 } 272 273 static ssize_t 274 manage_runtime_start_stop_store(struct device *dev, 275 struct device_attribute *attr, 276 const char *buf, size_t count) 277 { 278 struct scsi_disk *sdkp = to_scsi_disk(dev); 279 struct scsi_device *sdp = sdkp->device; 280 bool v; 281 282 if (!capable(CAP_SYS_ADMIN)) 283 return -EACCES; 284 285 if (kstrtobool(buf, &v)) 286 return -EINVAL; 287 288 sdp->manage_runtime_start_stop = v; 289 290 return count; 291 } 292 static DEVICE_ATTR_RW(manage_runtime_start_stop); 293 294 static ssize_t manage_shutdown_show(struct device *dev, 295 struct device_attribute *attr, char *buf) 296 { 297 struct scsi_disk *sdkp = to_scsi_disk(dev); 298 struct scsi_device *sdp = sdkp->device; 299 300 return sysfs_emit(buf, "%u\n", sdp->manage_shutdown); 301 } 302 303 static ssize_t manage_shutdown_store(struct device *dev, 304 struct device_attribute *attr, 305 const char *buf, size_t count) 306 { 307 struct scsi_disk *sdkp = to_scsi_disk(dev); 308 struct scsi_device *sdp = sdkp->device; 309 bool v; 310 311 if (!capable(CAP_SYS_ADMIN)) 312 return -EACCES; 313 314 if (kstrtobool(buf, &v)) 315 return -EINVAL; 316 317 sdp->manage_shutdown = v; 318 319 return count; 320 } 321 static DEVICE_ATTR_RW(manage_shutdown); 322 323 static ssize_t 324 allow_restart_show(struct device *dev, struct device_attribute *attr, char *buf) 325 { 326 struct scsi_disk *sdkp = to_scsi_disk(dev); 327 328 return sprintf(buf, "%u\n", sdkp->device->allow_restart); 329 } 330 331 static ssize_t 332 allow_restart_store(struct device *dev, struct device_attribute *attr, 333 const char *buf, size_t count) 334 { 335 bool v; 336 struct scsi_disk *sdkp = to_scsi_disk(dev); 337 struct scsi_device *sdp = sdkp->device; 338 339 if (!capable(CAP_SYS_ADMIN)) 340 return -EACCES; 341 342 if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC) 343 return -EINVAL; 344 345 if (kstrtobool(buf, &v)) 346 return -EINVAL; 347 348 sdp->allow_restart = v; 349 350 return count; 351 } 352 static DEVICE_ATTR_RW(allow_restart); 353 354 static ssize_t 355 cache_type_show(struct device *dev, struct device_attribute *attr, char *buf) 356 { 357 struct scsi_disk *sdkp = to_scsi_disk(dev); 358 int ct = sdkp->RCD + 2*sdkp->WCE; 359 360 return sprintf(buf, "%s\n", sd_cache_types[ct]); 361 } 362 static DEVICE_ATTR_RW(cache_type); 363 364 static ssize_t 365 FUA_show(struct device *dev, struct device_attribute *attr, char *buf) 366 { 367 struct scsi_disk *sdkp = to_scsi_disk(dev); 368 369 return sprintf(buf, "%u\n", sdkp->DPOFUA); 370 } 371 static DEVICE_ATTR_RO(FUA); 372 373 static ssize_t 374 protection_type_show(struct device *dev, struct device_attribute *attr, 375 char *buf) 376 { 377 struct scsi_disk *sdkp = to_scsi_disk(dev); 378 379 return sprintf(buf, "%u\n", sdkp->protection_type); 380 } 381 382 static ssize_t 383 protection_type_store(struct device *dev, struct device_attribute *attr, 384 const char *buf, size_t count) 385 { 386 struct scsi_disk *sdkp = to_scsi_disk(dev); 387 unsigned int val; 388 int err; 389 390 if (!capable(CAP_SYS_ADMIN)) 391 return -EACCES; 392 393 err = kstrtouint(buf, 10, &val); 394 395 if (err) 396 return err; 397 398 if (val <= T10_PI_TYPE3_PROTECTION) 399 sdkp->protection_type = val; 400 401 return count; 402 } 403 static DEVICE_ATTR_RW(protection_type); 404 405 static ssize_t 406 protection_mode_show(struct device *dev, struct device_attribute *attr, 407 char *buf) 408 { 409 struct scsi_disk *sdkp = to_scsi_disk(dev); 410 struct scsi_device *sdp = sdkp->device; 411 unsigned int dif, dix; 412 413 dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type); 414 dix = scsi_host_dix_capable(sdp->host, sdkp->protection_type); 415 416 if (!dix && scsi_host_dix_capable(sdp->host, T10_PI_TYPE0_PROTECTION)) { 417 dif = 0; 418 dix = 1; 419 } 420 421 if (!dif && !dix) 422 return sprintf(buf, "none\n"); 423 424 return sprintf(buf, "%s%u\n", dix ? "dix" : "dif", dif); 425 } 426 static DEVICE_ATTR_RO(protection_mode); 427 428 static ssize_t 429 app_tag_own_show(struct device *dev, struct device_attribute *attr, char *buf) 430 { 431 struct scsi_disk *sdkp = to_scsi_disk(dev); 432 433 return sprintf(buf, "%u\n", sdkp->ATO); 434 } 435 static DEVICE_ATTR_RO(app_tag_own); 436 437 static ssize_t 438 thin_provisioning_show(struct device *dev, struct device_attribute *attr, 439 char *buf) 440 { 441 struct scsi_disk *sdkp = to_scsi_disk(dev); 442 443 return sprintf(buf, "%u\n", sdkp->lbpme); 444 } 445 static DEVICE_ATTR_RO(thin_provisioning); 446 447 /* sysfs_match_string() requires dense arrays */ 448 static const char *lbp_mode[] = { 449 [SD_LBP_FULL] = "full", 450 [SD_LBP_UNMAP] = "unmap", 451 [SD_LBP_WS16] = "writesame_16", 452 [SD_LBP_WS10] = "writesame_10", 453 [SD_LBP_ZERO] = "writesame_zero", 454 [SD_LBP_DISABLE] = "disabled", 455 }; 456 457 static ssize_t 458 provisioning_mode_show(struct device *dev, struct device_attribute *attr, 459 char *buf) 460 { 461 struct scsi_disk *sdkp = to_scsi_disk(dev); 462 463 return sprintf(buf, "%s\n", lbp_mode[sdkp->provisioning_mode]); 464 } 465 466 static ssize_t 467 provisioning_mode_store(struct device *dev, struct device_attribute *attr, 468 const char *buf, size_t count) 469 { 470 struct scsi_disk *sdkp = to_scsi_disk(dev); 471 struct scsi_device *sdp = sdkp->device; 472 struct queue_limits lim; 473 int mode, err; 474 475 if (!capable(CAP_SYS_ADMIN)) 476 return -EACCES; 477 478 if (sdp->type != TYPE_DISK) 479 return -EINVAL; 480 481 mode = sysfs_match_string(lbp_mode, buf); 482 if (mode < 0) 483 return -EINVAL; 484 485 lim = queue_limits_start_update(sdkp->disk->queue); 486 sd_config_discard(sdkp, &lim, mode); 487 blk_mq_freeze_queue(sdkp->disk->queue); 488 err = queue_limits_commit_update(sdkp->disk->queue, &lim); 489 blk_mq_unfreeze_queue(sdkp->disk->queue); 490 if (err) 491 return err; 492 return count; 493 } 494 static DEVICE_ATTR_RW(provisioning_mode); 495 496 /* sysfs_match_string() requires dense arrays */ 497 static const char *zeroing_mode[] = { 498 [SD_ZERO_WRITE] = "write", 499 [SD_ZERO_WS] = "writesame", 500 [SD_ZERO_WS16_UNMAP] = "writesame_16_unmap", 501 [SD_ZERO_WS10_UNMAP] = "writesame_10_unmap", 502 }; 503 504 static ssize_t 505 zeroing_mode_show(struct device *dev, struct device_attribute *attr, 506 char *buf) 507 { 508 struct scsi_disk *sdkp = to_scsi_disk(dev); 509 510 return sprintf(buf, "%s\n", zeroing_mode[sdkp->zeroing_mode]); 511 } 512 513 static ssize_t 514 zeroing_mode_store(struct device *dev, struct device_attribute *attr, 515 const char *buf, size_t count) 516 { 517 struct scsi_disk *sdkp = to_scsi_disk(dev); 518 int mode; 519 520 if (!capable(CAP_SYS_ADMIN)) 521 return -EACCES; 522 523 mode = sysfs_match_string(zeroing_mode, buf); 524 if (mode < 0) 525 return -EINVAL; 526 527 sdkp->zeroing_mode = mode; 528 529 return count; 530 } 531 static DEVICE_ATTR_RW(zeroing_mode); 532 533 static ssize_t 534 max_medium_access_timeouts_show(struct device *dev, 535 struct device_attribute *attr, char *buf) 536 { 537 struct scsi_disk *sdkp = to_scsi_disk(dev); 538 539 return sprintf(buf, "%u\n", sdkp->max_medium_access_timeouts); 540 } 541 542 static ssize_t 543 max_medium_access_timeouts_store(struct device *dev, 544 struct device_attribute *attr, const char *buf, 545 size_t count) 546 { 547 struct scsi_disk *sdkp = to_scsi_disk(dev); 548 int err; 549 550 if (!capable(CAP_SYS_ADMIN)) 551 return -EACCES; 552 553 err = kstrtouint(buf, 10, &sdkp->max_medium_access_timeouts); 554 555 return err ? err : count; 556 } 557 static DEVICE_ATTR_RW(max_medium_access_timeouts); 558 559 static ssize_t 560 max_write_same_blocks_show(struct device *dev, struct device_attribute *attr, 561 char *buf) 562 { 563 struct scsi_disk *sdkp = to_scsi_disk(dev); 564 565 return sprintf(buf, "%u\n", sdkp->max_ws_blocks); 566 } 567 568 static ssize_t 569 max_write_same_blocks_store(struct device *dev, struct device_attribute *attr, 570 const char *buf, size_t count) 571 { 572 struct scsi_disk *sdkp = to_scsi_disk(dev); 573 struct scsi_device *sdp = sdkp->device; 574 struct queue_limits lim; 575 unsigned long max; 576 int err; 577 578 if (!capable(CAP_SYS_ADMIN)) 579 return -EACCES; 580 581 if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC) 582 return -EINVAL; 583 584 err = kstrtoul(buf, 10, &max); 585 586 if (err) 587 return err; 588 589 if (max == 0) 590 sdp->no_write_same = 1; 591 else if (max <= SD_MAX_WS16_BLOCKS) { 592 sdp->no_write_same = 0; 593 sdkp->max_ws_blocks = max; 594 } 595 596 lim = queue_limits_start_update(sdkp->disk->queue); 597 sd_config_write_same(sdkp, &lim); 598 blk_mq_freeze_queue(sdkp->disk->queue); 599 err = queue_limits_commit_update(sdkp->disk->queue, &lim); 600 blk_mq_unfreeze_queue(sdkp->disk->queue); 601 if (err) 602 return err; 603 return count; 604 } 605 static DEVICE_ATTR_RW(max_write_same_blocks); 606 607 static ssize_t 608 zoned_cap_show(struct device *dev, struct device_attribute *attr, char *buf) 609 { 610 struct scsi_disk *sdkp = to_scsi_disk(dev); 611 612 if (sdkp->device->type == TYPE_ZBC) 613 return sprintf(buf, "host-managed\n"); 614 if (sdkp->zoned == 1) 615 return sprintf(buf, "host-aware\n"); 616 if (sdkp->zoned == 2) 617 return sprintf(buf, "drive-managed\n"); 618 return sprintf(buf, "none\n"); 619 } 620 static DEVICE_ATTR_RO(zoned_cap); 621 622 static ssize_t 623 max_retries_store(struct device *dev, struct device_attribute *attr, 624 const char *buf, size_t count) 625 { 626 struct scsi_disk *sdkp = to_scsi_disk(dev); 627 struct scsi_device *sdev = sdkp->device; 628 int retries, err; 629 630 err = kstrtoint(buf, 10, &retries); 631 if (err) 632 return err; 633 634 if (retries == SCSI_CMD_RETRIES_NO_LIMIT || retries <= SD_MAX_RETRIES) { 635 sdkp->max_retries = retries; 636 return count; 637 } 638 639 sdev_printk(KERN_ERR, sdev, "max_retries must be between -1 and %d\n", 640 SD_MAX_RETRIES); 641 return -EINVAL; 642 } 643 644 static ssize_t 645 max_retries_show(struct device *dev, struct device_attribute *attr, 646 char *buf) 647 { 648 struct scsi_disk *sdkp = to_scsi_disk(dev); 649 650 return sprintf(buf, "%d\n", sdkp->max_retries); 651 } 652 653 static DEVICE_ATTR_RW(max_retries); 654 655 static struct attribute *sd_disk_attrs[] = { 656 &dev_attr_cache_type.attr, 657 &dev_attr_FUA.attr, 658 &dev_attr_allow_restart.attr, 659 &dev_attr_manage_start_stop.attr, 660 &dev_attr_manage_system_start_stop.attr, 661 &dev_attr_manage_runtime_start_stop.attr, 662 &dev_attr_manage_shutdown.attr, 663 &dev_attr_protection_type.attr, 664 &dev_attr_protection_mode.attr, 665 &dev_attr_app_tag_own.attr, 666 &dev_attr_thin_provisioning.attr, 667 &dev_attr_provisioning_mode.attr, 668 &dev_attr_zeroing_mode.attr, 669 &dev_attr_max_write_same_blocks.attr, 670 &dev_attr_max_medium_access_timeouts.attr, 671 &dev_attr_zoned_cap.attr, 672 &dev_attr_max_retries.attr, 673 NULL, 674 }; 675 ATTRIBUTE_GROUPS(sd_disk); 676 677 static struct class sd_disk_class = { 678 .name = "scsi_disk", 679 .dev_release = scsi_disk_release, 680 .dev_groups = sd_disk_groups, 681 }; 682 683 /* 684 * Don't request a new module, as that could deadlock in multipath 685 * environment. 686 */ 687 static void sd_default_probe(dev_t devt) 688 { 689 } 690 691 /* 692 * Device no to disk mapping: 693 * 694 * major disc2 disc p1 695 * |............|.............|....|....| <- dev_t 696 * 31 20 19 8 7 4 3 0 697 * 698 * Inside a major, we have 16k disks, however mapped non- 699 * contiguously. The first 16 disks are for major0, the next 700 * ones with major1, ... Disk 256 is for major0 again, disk 272 701 * for major1, ... 702 * As we stay compatible with our numbering scheme, we can reuse 703 * the well-know SCSI majors 8, 65--71, 136--143. 704 */ 705 static int sd_major(int major_idx) 706 { 707 switch (major_idx) { 708 case 0: 709 return SCSI_DISK0_MAJOR; 710 case 1 ... 7: 711 return SCSI_DISK1_MAJOR + major_idx - 1; 712 case 8 ... 15: 713 return SCSI_DISK8_MAJOR + major_idx - 8; 714 default: 715 BUG(); 716 return 0; /* shut up gcc */ 717 } 718 } 719 720 #ifdef CONFIG_BLK_SED_OPAL 721 static int sd_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, 722 size_t len, bool send) 723 { 724 struct scsi_disk *sdkp = data; 725 struct scsi_device *sdev = sdkp->device; 726 u8 cdb[12] = { 0, }; 727 const struct scsi_exec_args exec_args = { 728 .req_flags = BLK_MQ_REQ_PM, 729 }; 730 int ret; 731 732 cdb[0] = send ? SECURITY_PROTOCOL_OUT : SECURITY_PROTOCOL_IN; 733 cdb[1] = secp; 734 put_unaligned_be16(spsp, &cdb[2]); 735 put_unaligned_be32(len, &cdb[6]); 736 737 ret = scsi_execute_cmd(sdev, cdb, send ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 738 buffer, len, SD_TIMEOUT, sdkp->max_retries, 739 &exec_args); 740 return ret <= 0 ? ret : -EIO; 741 } 742 #endif /* CONFIG_BLK_SED_OPAL */ 743 744 /* 745 * Look up the DIX operation based on whether the command is read or 746 * write and whether dix and dif are enabled. 747 */ 748 static unsigned int sd_prot_op(bool write, bool dix, bool dif) 749 { 750 /* Lookup table: bit 2 (write), bit 1 (dix), bit 0 (dif) */ 751 static const unsigned int ops[] = { /* wrt dix dif */ 752 SCSI_PROT_NORMAL, /* 0 0 0 */ 753 SCSI_PROT_READ_STRIP, /* 0 0 1 */ 754 SCSI_PROT_READ_INSERT, /* 0 1 0 */ 755 SCSI_PROT_READ_PASS, /* 0 1 1 */ 756 SCSI_PROT_NORMAL, /* 1 0 0 */ 757 SCSI_PROT_WRITE_INSERT, /* 1 0 1 */ 758 SCSI_PROT_WRITE_STRIP, /* 1 1 0 */ 759 SCSI_PROT_WRITE_PASS, /* 1 1 1 */ 760 }; 761 762 return ops[write << 2 | dix << 1 | dif]; 763 } 764 765 /* 766 * Returns a mask of the protection flags that are valid for a given DIX 767 * operation. 768 */ 769 static unsigned int sd_prot_flag_mask(unsigned int prot_op) 770 { 771 static const unsigned int flag_mask[] = { 772 [SCSI_PROT_NORMAL] = 0, 773 774 [SCSI_PROT_READ_STRIP] = SCSI_PROT_TRANSFER_PI | 775 SCSI_PROT_GUARD_CHECK | 776 SCSI_PROT_REF_CHECK | 777 SCSI_PROT_REF_INCREMENT, 778 779 [SCSI_PROT_READ_INSERT] = SCSI_PROT_REF_INCREMENT | 780 SCSI_PROT_IP_CHECKSUM, 781 782 [SCSI_PROT_READ_PASS] = SCSI_PROT_TRANSFER_PI | 783 SCSI_PROT_GUARD_CHECK | 784 SCSI_PROT_REF_CHECK | 785 SCSI_PROT_REF_INCREMENT | 786 SCSI_PROT_IP_CHECKSUM, 787 788 [SCSI_PROT_WRITE_INSERT] = SCSI_PROT_TRANSFER_PI | 789 SCSI_PROT_REF_INCREMENT, 790 791 [SCSI_PROT_WRITE_STRIP] = SCSI_PROT_GUARD_CHECK | 792 SCSI_PROT_REF_CHECK | 793 SCSI_PROT_REF_INCREMENT | 794 SCSI_PROT_IP_CHECKSUM, 795 796 [SCSI_PROT_WRITE_PASS] = SCSI_PROT_TRANSFER_PI | 797 SCSI_PROT_GUARD_CHECK | 798 SCSI_PROT_REF_CHECK | 799 SCSI_PROT_REF_INCREMENT | 800 SCSI_PROT_IP_CHECKSUM, 801 }; 802 803 return flag_mask[prot_op]; 804 } 805 806 static unsigned char sd_setup_protect_cmnd(struct scsi_cmnd *scmd, 807 unsigned int dix, unsigned int dif) 808 { 809 struct request *rq = scsi_cmd_to_rq(scmd); 810 struct bio *bio = rq->bio; 811 unsigned int prot_op = sd_prot_op(rq_data_dir(rq), dix, dif); 812 unsigned int protect = 0; 813 814 if (dix) { /* DIX Type 0, 1, 2, 3 */ 815 if (bio_integrity_flagged(bio, BIP_IP_CHECKSUM)) 816 scmd->prot_flags |= SCSI_PROT_IP_CHECKSUM; 817 818 if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false) 819 scmd->prot_flags |= SCSI_PROT_GUARD_CHECK; 820 } 821 822 if (dif != T10_PI_TYPE3_PROTECTION) { /* DIX/DIF Type 0, 1, 2 */ 823 scmd->prot_flags |= SCSI_PROT_REF_INCREMENT; 824 825 if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false) 826 scmd->prot_flags |= SCSI_PROT_REF_CHECK; 827 } 828 829 if (dif) { /* DIX/DIF Type 1, 2, 3 */ 830 scmd->prot_flags |= SCSI_PROT_TRANSFER_PI; 831 832 if (bio_integrity_flagged(bio, BIP_DISK_NOCHECK)) 833 protect = 3 << 5; /* Disable target PI checking */ 834 else 835 protect = 1 << 5; /* Enable target PI checking */ 836 } 837 838 scsi_set_prot_op(scmd, prot_op); 839 scsi_set_prot_type(scmd, dif); 840 scmd->prot_flags &= sd_prot_flag_mask(prot_op); 841 842 return protect; 843 } 844 845 static void sd_disable_discard(struct scsi_disk *sdkp) 846 { 847 sdkp->provisioning_mode = SD_LBP_DISABLE; 848 blk_queue_disable_discard(sdkp->disk->queue); 849 } 850 851 static void sd_config_discard(struct scsi_disk *sdkp, struct queue_limits *lim, 852 unsigned int mode) 853 { 854 unsigned int logical_block_size = sdkp->device->sector_size; 855 unsigned int max_blocks = 0; 856 857 lim->discard_alignment = sdkp->unmap_alignment * logical_block_size; 858 lim->discard_granularity = max(sdkp->physical_block_size, 859 sdkp->unmap_granularity * logical_block_size); 860 sdkp->provisioning_mode = mode; 861 862 switch (mode) { 863 864 case SD_LBP_FULL: 865 case SD_LBP_DISABLE: 866 break; 867 868 case SD_LBP_UNMAP: 869 max_blocks = min_not_zero(sdkp->max_unmap_blocks, 870 (u32)SD_MAX_WS16_BLOCKS); 871 break; 872 873 case SD_LBP_WS16: 874 if (sdkp->device->unmap_limit_for_ws) 875 max_blocks = sdkp->max_unmap_blocks; 876 else 877 max_blocks = sdkp->max_ws_blocks; 878 879 max_blocks = min_not_zero(max_blocks, (u32)SD_MAX_WS16_BLOCKS); 880 break; 881 882 case SD_LBP_WS10: 883 if (sdkp->device->unmap_limit_for_ws) 884 max_blocks = sdkp->max_unmap_blocks; 885 else 886 max_blocks = sdkp->max_ws_blocks; 887 888 max_blocks = min_not_zero(max_blocks, (u32)SD_MAX_WS10_BLOCKS); 889 break; 890 891 case SD_LBP_ZERO: 892 max_blocks = min_not_zero(sdkp->max_ws_blocks, 893 (u32)SD_MAX_WS10_BLOCKS); 894 break; 895 } 896 897 lim->max_hw_discard_sectors = max_blocks * 898 (logical_block_size >> SECTOR_SHIFT); 899 } 900 901 static void *sd_set_special_bvec(struct request *rq, unsigned int data_len) 902 { 903 struct page *page; 904 905 page = mempool_alloc(sd_page_pool, GFP_ATOMIC); 906 if (!page) 907 return NULL; 908 clear_highpage(page); 909 bvec_set_page(&rq->special_vec, page, data_len, 0); 910 rq->rq_flags |= RQF_SPECIAL_PAYLOAD; 911 return bvec_virt(&rq->special_vec); 912 } 913 914 static blk_status_t sd_setup_unmap_cmnd(struct scsi_cmnd *cmd) 915 { 916 struct scsi_device *sdp = cmd->device; 917 struct request *rq = scsi_cmd_to_rq(cmd); 918 struct scsi_disk *sdkp = scsi_disk(rq->q->disk); 919 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq)); 920 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq)); 921 unsigned int data_len = 24; 922 char *buf; 923 924 buf = sd_set_special_bvec(rq, data_len); 925 if (!buf) 926 return BLK_STS_RESOURCE; 927 928 cmd->cmd_len = 10; 929 cmd->cmnd[0] = UNMAP; 930 cmd->cmnd[8] = 24; 931 932 put_unaligned_be16(6 + 16, &buf[0]); 933 put_unaligned_be16(16, &buf[2]); 934 put_unaligned_be64(lba, &buf[8]); 935 put_unaligned_be32(nr_blocks, &buf[16]); 936 937 cmd->allowed = sdkp->max_retries; 938 cmd->transfersize = data_len; 939 rq->timeout = SD_TIMEOUT; 940 941 return scsi_alloc_sgtables(cmd); 942 } 943 944 static void sd_config_atomic(struct scsi_disk *sdkp, struct queue_limits *lim) 945 { 946 unsigned int logical_block_size = sdkp->device->sector_size, 947 physical_block_size_sectors, max_atomic, unit_min, unit_max; 948 949 if ((!sdkp->max_atomic && !sdkp->max_atomic_with_boundary) || 950 sdkp->protection_type == T10_PI_TYPE2_PROTECTION) 951 return; 952 953 physical_block_size_sectors = sdkp->physical_block_size / 954 sdkp->device->sector_size; 955 956 unit_min = rounddown_pow_of_two(sdkp->atomic_granularity ? 957 sdkp->atomic_granularity : 958 physical_block_size_sectors); 959 960 /* 961 * Only use atomic boundary when we have the odd scenario of 962 * sdkp->max_atomic == 0, which the spec does permit. 963 */ 964 if (sdkp->max_atomic) { 965 max_atomic = sdkp->max_atomic; 966 unit_max = rounddown_pow_of_two(sdkp->max_atomic); 967 sdkp->use_atomic_write_boundary = 0; 968 } else { 969 max_atomic = sdkp->max_atomic_with_boundary; 970 unit_max = rounddown_pow_of_two(sdkp->max_atomic_boundary); 971 sdkp->use_atomic_write_boundary = 1; 972 } 973 974 /* 975 * Ensure compliance with granularity and alignment. For now, keep it 976 * simple and just don't support atomic writes for values mismatched 977 * with max_{boundary}atomic, physical block size, and 978 * atomic_granularity itself. 979 * 980 * We're really being distrustful by checking unit_max also... 981 */ 982 if (sdkp->atomic_granularity > 1) { 983 if (unit_min > 1 && unit_min % sdkp->atomic_granularity) 984 return; 985 if (unit_max > 1 && unit_max % sdkp->atomic_granularity) 986 return; 987 } 988 989 if (sdkp->atomic_alignment > 1) { 990 if (unit_min > 1 && unit_min % sdkp->atomic_alignment) 991 return; 992 if (unit_max > 1 && unit_max % sdkp->atomic_alignment) 993 return; 994 } 995 996 lim->atomic_write_hw_max = max_atomic * logical_block_size; 997 lim->atomic_write_hw_boundary = 0; 998 lim->atomic_write_hw_unit_min = unit_min * logical_block_size; 999 lim->atomic_write_hw_unit_max = unit_max * logical_block_size; 1000 } 1001 1002 static blk_status_t sd_setup_write_same16_cmnd(struct scsi_cmnd *cmd, 1003 bool unmap) 1004 { 1005 struct scsi_device *sdp = cmd->device; 1006 struct request *rq = scsi_cmd_to_rq(cmd); 1007 struct scsi_disk *sdkp = scsi_disk(rq->q->disk); 1008 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq)); 1009 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq)); 1010 u32 data_len = sdp->sector_size; 1011 1012 if (!sd_set_special_bvec(rq, data_len)) 1013 return BLK_STS_RESOURCE; 1014 1015 cmd->cmd_len = 16; 1016 cmd->cmnd[0] = WRITE_SAME_16; 1017 if (unmap) 1018 cmd->cmnd[1] = 0x8; /* UNMAP */ 1019 put_unaligned_be64(lba, &cmd->cmnd[2]); 1020 put_unaligned_be32(nr_blocks, &cmd->cmnd[10]); 1021 1022 cmd->allowed = sdkp->max_retries; 1023 cmd->transfersize = data_len; 1024 rq->timeout = unmap ? SD_TIMEOUT : SD_WRITE_SAME_TIMEOUT; 1025 1026 return scsi_alloc_sgtables(cmd); 1027 } 1028 1029 static blk_status_t sd_setup_write_same10_cmnd(struct scsi_cmnd *cmd, 1030 bool unmap) 1031 { 1032 struct scsi_device *sdp = cmd->device; 1033 struct request *rq = scsi_cmd_to_rq(cmd); 1034 struct scsi_disk *sdkp = scsi_disk(rq->q->disk); 1035 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq)); 1036 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq)); 1037 u32 data_len = sdp->sector_size; 1038 1039 if (!sd_set_special_bvec(rq, data_len)) 1040 return BLK_STS_RESOURCE; 1041 1042 cmd->cmd_len = 10; 1043 cmd->cmnd[0] = WRITE_SAME; 1044 if (unmap) 1045 cmd->cmnd[1] = 0x8; /* UNMAP */ 1046 put_unaligned_be32(lba, &cmd->cmnd[2]); 1047 put_unaligned_be16(nr_blocks, &cmd->cmnd[7]); 1048 1049 cmd->allowed = sdkp->max_retries; 1050 cmd->transfersize = data_len; 1051 rq->timeout = unmap ? SD_TIMEOUT : SD_WRITE_SAME_TIMEOUT; 1052 1053 return scsi_alloc_sgtables(cmd); 1054 } 1055 1056 static blk_status_t sd_setup_write_zeroes_cmnd(struct scsi_cmnd *cmd) 1057 { 1058 struct request *rq = scsi_cmd_to_rq(cmd); 1059 struct scsi_device *sdp = cmd->device; 1060 struct scsi_disk *sdkp = scsi_disk(rq->q->disk); 1061 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq)); 1062 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq)); 1063 1064 if (!(rq->cmd_flags & REQ_NOUNMAP)) { 1065 switch (sdkp->zeroing_mode) { 1066 case SD_ZERO_WS16_UNMAP: 1067 return sd_setup_write_same16_cmnd(cmd, true); 1068 case SD_ZERO_WS10_UNMAP: 1069 return sd_setup_write_same10_cmnd(cmd, true); 1070 } 1071 } 1072 1073 if (sdp->no_write_same) { 1074 rq->rq_flags |= RQF_QUIET; 1075 return BLK_STS_TARGET; 1076 } 1077 1078 if (sdkp->ws16 || lba > 0xffffffff || nr_blocks > 0xffff) 1079 return sd_setup_write_same16_cmnd(cmd, false); 1080 1081 return sd_setup_write_same10_cmnd(cmd, false); 1082 } 1083 1084 static void sd_disable_write_same(struct scsi_disk *sdkp) 1085 { 1086 sdkp->device->no_write_same = 1; 1087 sdkp->max_ws_blocks = 0; 1088 blk_queue_disable_write_zeroes(sdkp->disk->queue); 1089 } 1090 1091 static void sd_config_write_same(struct scsi_disk *sdkp, 1092 struct queue_limits *lim) 1093 { 1094 unsigned int logical_block_size = sdkp->device->sector_size; 1095 1096 if (sdkp->device->no_write_same) { 1097 sdkp->max_ws_blocks = 0; 1098 goto out; 1099 } 1100 1101 /* Some devices can not handle block counts above 0xffff despite 1102 * supporting WRITE SAME(16). Consequently we default to 64k 1103 * blocks per I/O unless the device explicitly advertises a 1104 * bigger limit. 1105 */ 1106 if (sdkp->max_ws_blocks > SD_MAX_WS10_BLOCKS) 1107 sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks, 1108 (u32)SD_MAX_WS16_BLOCKS); 1109 else if (sdkp->ws16 || sdkp->ws10 || sdkp->device->no_report_opcodes) 1110 sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks, 1111 (u32)SD_MAX_WS10_BLOCKS); 1112 else { 1113 sdkp->device->no_write_same = 1; 1114 sdkp->max_ws_blocks = 0; 1115 } 1116 1117 if (sdkp->lbprz && sdkp->lbpws) 1118 sdkp->zeroing_mode = SD_ZERO_WS16_UNMAP; 1119 else if (sdkp->lbprz && sdkp->lbpws10) 1120 sdkp->zeroing_mode = SD_ZERO_WS10_UNMAP; 1121 else if (sdkp->max_ws_blocks) 1122 sdkp->zeroing_mode = SD_ZERO_WS; 1123 else 1124 sdkp->zeroing_mode = SD_ZERO_WRITE; 1125 1126 if (sdkp->max_ws_blocks && 1127 sdkp->physical_block_size > logical_block_size) { 1128 /* 1129 * Reporting a maximum number of blocks that is not aligned 1130 * on the device physical size would cause a large write same 1131 * request to be split into physically unaligned chunks by 1132 * __blkdev_issue_write_zeroes() even if the caller of this 1133 * functions took care to align the large request. So make sure 1134 * the maximum reported is aligned to the device physical block 1135 * size. This is only an optional optimization for regular 1136 * disks, but this is mandatory to avoid failure of large write 1137 * same requests directed at sequential write required zones of 1138 * host-managed ZBC disks. 1139 */ 1140 sdkp->max_ws_blocks = 1141 round_down(sdkp->max_ws_blocks, 1142 bytes_to_logical(sdkp->device, 1143 sdkp->physical_block_size)); 1144 } 1145 1146 out: 1147 lim->max_write_zeroes_sectors = 1148 sdkp->max_ws_blocks * (logical_block_size >> SECTOR_SHIFT); 1149 } 1150 1151 static blk_status_t sd_setup_flush_cmnd(struct scsi_cmnd *cmd) 1152 { 1153 struct request *rq = scsi_cmd_to_rq(cmd); 1154 struct scsi_disk *sdkp = scsi_disk(rq->q->disk); 1155 1156 /* flush requests don't perform I/O, zero the S/G table */ 1157 memset(&cmd->sdb, 0, sizeof(cmd->sdb)); 1158 1159 if (cmd->device->use_16_for_sync) { 1160 cmd->cmnd[0] = SYNCHRONIZE_CACHE_16; 1161 cmd->cmd_len = 16; 1162 } else { 1163 cmd->cmnd[0] = SYNCHRONIZE_CACHE; 1164 cmd->cmd_len = 10; 1165 } 1166 cmd->transfersize = 0; 1167 cmd->allowed = sdkp->max_retries; 1168 1169 rq->timeout = rq->q->rq_timeout * SD_FLUSH_TIMEOUT_MULTIPLIER; 1170 return BLK_STS_OK; 1171 } 1172 1173 /** 1174 * sd_group_number() - Compute the GROUP NUMBER field 1175 * @cmd: SCSI command for which to compute the value of the six-bit GROUP NUMBER 1176 * field. 1177 * 1178 * From SBC-5 r05 (https://www.t10.org/cgi-bin/ac.pl?t=f&f=sbc5r05.pdf): 1179 * 0: no relative lifetime. 1180 * 1: shortest relative lifetime. 1181 * 2: second shortest relative lifetime. 1182 * 3 - 0x3d: intermediate relative lifetimes. 1183 * 0x3e: second longest relative lifetime. 1184 * 0x3f: longest relative lifetime. 1185 */ 1186 static u8 sd_group_number(struct scsi_cmnd *cmd) 1187 { 1188 const struct request *rq = scsi_cmd_to_rq(cmd); 1189 struct scsi_disk *sdkp = scsi_disk(rq->q->disk); 1190 1191 if (!sdkp->rscs) 1192 return 0; 1193 1194 return min3((u32)rq->write_hint, (u32)sdkp->permanent_stream_count, 1195 0x3fu); 1196 } 1197 1198 static blk_status_t sd_setup_rw32_cmnd(struct scsi_cmnd *cmd, bool write, 1199 sector_t lba, unsigned int nr_blocks, 1200 unsigned char flags, unsigned int dld) 1201 { 1202 cmd->cmd_len = SD_EXT_CDB_SIZE; 1203 cmd->cmnd[0] = VARIABLE_LENGTH_CMD; 1204 cmd->cmnd[6] = sd_group_number(cmd); 1205 cmd->cmnd[7] = 0x18; /* Additional CDB len */ 1206 cmd->cmnd[9] = write ? WRITE_32 : READ_32; 1207 cmd->cmnd[10] = flags; 1208 cmd->cmnd[11] = dld & 0x07; 1209 put_unaligned_be64(lba, &cmd->cmnd[12]); 1210 put_unaligned_be32(lba, &cmd->cmnd[20]); /* Expected Indirect LBA */ 1211 put_unaligned_be32(nr_blocks, &cmd->cmnd[28]); 1212 1213 return BLK_STS_OK; 1214 } 1215 1216 static blk_status_t sd_setup_rw16_cmnd(struct scsi_cmnd *cmd, bool write, 1217 sector_t lba, unsigned int nr_blocks, 1218 unsigned char flags, unsigned int dld) 1219 { 1220 cmd->cmd_len = 16; 1221 cmd->cmnd[0] = write ? WRITE_16 : READ_16; 1222 cmd->cmnd[1] = flags | ((dld >> 2) & 0x01); 1223 cmd->cmnd[14] = ((dld & 0x03) << 6) | sd_group_number(cmd); 1224 cmd->cmnd[15] = 0; 1225 put_unaligned_be64(lba, &cmd->cmnd[2]); 1226 put_unaligned_be32(nr_blocks, &cmd->cmnd[10]); 1227 1228 return BLK_STS_OK; 1229 } 1230 1231 static blk_status_t sd_setup_rw10_cmnd(struct scsi_cmnd *cmd, bool write, 1232 sector_t lba, unsigned int nr_blocks, 1233 unsigned char flags) 1234 { 1235 cmd->cmd_len = 10; 1236 cmd->cmnd[0] = write ? WRITE_10 : READ_10; 1237 cmd->cmnd[1] = flags; 1238 cmd->cmnd[6] = sd_group_number(cmd); 1239 cmd->cmnd[9] = 0; 1240 put_unaligned_be32(lba, &cmd->cmnd[2]); 1241 put_unaligned_be16(nr_blocks, &cmd->cmnd[7]); 1242 1243 return BLK_STS_OK; 1244 } 1245 1246 static blk_status_t sd_setup_rw6_cmnd(struct scsi_cmnd *cmd, bool write, 1247 sector_t lba, unsigned int nr_blocks, 1248 unsigned char flags) 1249 { 1250 /* Avoid that 0 blocks gets translated into 256 blocks. */ 1251 if (WARN_ON_ONCE(nr_blocks == 0)) 1252 return BLK_STS_IOERR; 1253 1254 if (unlikely(flags & 0x8)) { 1255 /* 1256 * This happens only if this drive failed 10byte rw 1257 * command with ILLEGAL_REQUEST during operation and 1258 * thus turned off use_10_for_rw. 1259 */ 1260 scmd_printk(KERN_ERR, cmd, "FUA write on READ/WRITE(6) drive\n"); 1261 return BLK_STS_IOERR; 1262 } 1263 1264 cmd->cmd_len = 6; 1265 cmd->cmnd[0] = write ? WRITE_6 : READ_6; 1266 cmd->cmnd[1] = (lba >> 16) & 0x1f; 1267 cmd->cmnd[2] = (lba >> 8) & 0xff; 1268 cmd->cmnd[3] = lba & 0xff; 1269 cmd->cmnd[4] = nr_blocks; 1270 cmd->cmnd[5] = 0; 1271 1272 return BLK_STS_OK; 1273 } 1274 1275 /* 1276 * Check if a command has a duration limit set. If it does, and the target 1277 * device supports CDL and the feature is enabled, return the limit 1278 * descriptor index to use. Return 0 (no limit) otherwise. 1279 */ 1280 static int sd_cdl_dld(struct scsi_disk *sdkp, struct scsi_cmnd *scmd) 1281 { 1282 struct scsi_device *sdp = sdkp->device; 1283 int hint; 1284 1285 if (!sdp->cdl_supported || !sdp->cdl_enable) 1286 return 0; 1287 1288 /* 1289 * Use "no limit" if the request ioprio does not specify a duration 1290 * limit hint. 1291 */ 1292 hint = IOPRIO_PRIO_HINT(req_get_ioprio(scsi_cmd_to_rq(scmd))); 1293 if (hint < IOPRIO_HINT_DEV_DURATION_LIMIT_1 || 1294 hint > IOPRIO_HINT_DEV_DURATION_LIMIT_7) 1295 return 0; 1296 1297 return (hint - IOPRIO_HINT_DEV_DURATION_LIMIT_1) + 1; 1298 } 1299 1300 static blk_status_t sd_setup_atomic_cmnd(struct scsi_cmnd *cmd, 1301 sector_t lba, unsigned int nr_blocks, 1302 bool boundary, unsigned char flags) 1303 { 1304 cmd->cmd_len = 16; 1305 cmd->cmnd[0] = WRITE_ATOMIC_16; 1306 cmd->cmnd[1] = flags; 1307 put_unaligned_be64(lba, &cmd->cmnd[2]); 1308 put_unaligned_be16(nr_blocks, &cmd->cmnd[12]); 1309 if (boundary) 1310 put_unaligned_be16(nr_blocks, &cmd->cmnd[10]); 1311 else 1312 put_unaligned_be16(0, &cmd->cmnd[10]); 1313 put_unaligned_be16(nr_blocks, &cmd->cmnd[12]); 1314 cmd->cmnd[14] = 0; 1315 cmd->cmnd[15] = 0; 1316 1317 return BLK_STS_OK; 1318 } 1319 1320 static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd) 1321 { 1322 struct request *rq = scsi_cmd_to_rq(cmd); 1323 struct scsi_device *sdp = cmd->device; 1324 struct scsi_disk *sdkp = scsi_disk(rq->q->disk); 1325 sector_t lba = sectors_to_logical(sdp, blk_rq_pos(rq)); 1326 sector_t threshold; 1327 unsigned int nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq)); 1328 unsigned int mask = logical_to_sectors(sdp, 1) - 1; 1329 bool write = rq_data_dir(rq) == WRITE; 1330 unsigned char protect, fua; 1331 unsigned int dld; 1332 blk_status_t ret; 1333 unsigned int dif; 1334 bool dix; 1335 1336 ret = scsi_alloc_sgtables(cmd); 1337 if (ret != BLK_STS_OK) 1338 return ret; 1339 1340 ret = BLK_STS_IOERR; 1341 if (!scsi_device_online(sdp) || sdp->changed) { 1342 scmd_printk(KERN_ERR, cmd, "device offline or changed\n"); 1343 goto fail; 1344 } 1345 1346 if (blk_rq_pos(rq) + blk_rq_sectors(rq) > get_capacity(rq->q->disk)) { 1347 scmd_printk(KERN_ERR, cmd, "access beyond end of device\n"); 1348 goto fail; 1349 } 1350 1351 if ((blk_rq_pos(rq) & mask) || (blk_rq_sectors(rq) & mask)) { 1352 scmd_printk(KERN_ERR, cmd, "request not aligned to the logical block size\n"); 1353 goto fail; 1354 } 1355 1356 /* 1357 * Some SD card readers can't handle accesses which touch the 1358 * last one or two logical blocks. Split accesses as needed. 1359 */ 1360 threshold = sdkp->capacity - SD_LAST_BUGGY_SECTORS; 1361 1362 if (unlikely(sdp->last_sector_bug && lba + nr_blocks > threshold)) { 1363 if (lba < threshold) { 1364 /* Access up to the threshold but not beyond */ 1365 nr_blocks = threshold - lba; 1366 } else { 1367 /* Access only a single logical block */ 1368 nr_blocks = 1; 1369 } 1370 } 1371 1372 fua = rq->cmd_flags & REQ_FUA ? 0x8 : 0; 1373 dix = scsi_prot_sg_count(cmd); 1374 dif = scsi_host_dif_capable(cmd->device->host, sdkp->protection_type); 1375 dld = sd_cdl_dld(sdkp, cmd); 1376 1377 if (dif || dix) 1378 protect = sd_setup_protect_cmnd(cmd, dix, dif); 1379 else 1380 protect = 0; 1381 1382 if (protect && sdkp->protection_type == T10_PI_TYPE2_PROTECTION) { 1383 ret = sd_setup_rw32_cmnd(cmd, write, lba, nr_blocks, 1384 protect | fua, dld); 1385 } else if (rq->cmd_flags & REQ_ATOMIC && write) { 1386 ret = sd_setup_atomic_cmnd(cmd, lba, nr_blocks, 1387 sdkp->use_atomic_write_boundary, 1388 protect | fua); 1389 } else if (sdp->use_16_for_rw || (nr_blocks > 0xffff)) { 1390 ret = sd_setup_rw16_cmnd(cmd, write, lba, nr_blocks, 1391 protect | fua, dld); 1392 } else if ((nr_blocks > 0xff) || (lba > 0x1fffff) || 1393 sdp->use_10_for_rw || protect || rq->write_hint) { 1394 ret = sd_setup_rw10_cmnd(cmd, write, lba, nr_blocks, 1395 protect | fua); 1396 } else { 1397 ret = sd_setup_rw6_cmnd(cmd, write, lba, nr_blocks, 1398 protect | fua); 1399 } 1400 1401 if (unlikely(ret != BLK_STS_OK)) 1402 goto fail; 1403 1404 /* 1405 * We shouldn't disconnect in the middle of a sector, so with a dumb 1406 * host adapter, it's safe to assume that we can at least transfer 1407 * this many bytes between each connect / disconnect. 1408 */ 1409 cmd->transfersize = sdp->sector_size; 1410 cmd->underflow = nr_blocks << 9; 1411 cmd->allowed = sdkp->max_retries; 1412 cmd->sdb.length = nr_blocks * sdp->sector_size; 1413 1414 SCSI_LOG_HLQUEUE(1, 1415 scmd_printk(KERN_INFO, cmd, 1416 "%s: block=%llu, count=%d\n", __func__, 1417 (unsigned long long)blk_rq_pos(rq), 1418 blk_rq_sectors(rq))); 1419 SCSI_LOG_HLQUEUE(2, 1420 scmd_printk(KERN_INFO, cmd, 1421 "%s %d/%u 512 byte blocks.\n", 1422 write ? "writing" : "reading", nr_blocks, 1423 blk_rq_sectors(rq))); 1424 1425 /* 1426 * This indicates that the command is ready from our end to be queued. 1427 */ 1428 return BLK_STS_OK; 1429 fail: 1430 scsi_free_sgtables(cmd); 1431 return ret; 1432 } 1433 1434 static blk_status_t sd_init_command(struct scsi_cmnd *cmd) 1435 { 1436 struct request *rq = scsi_cmd_to_rq(cmd); 1437 1438 switch (req_op(rq)) { 1439 case REQ_OP_DISCARD: 1440 switch (scsi_disk(rq->q->disk)->provisioning_mode) { 1441 case SD_LBP_UNMAP: 1442 return sd_setup_unmap_cmnd(cmd); 1443 case SD_LBP_WS16: 1444 return sd_setup_write_same16_cmnd(cmd, true); 1445 case SD_LBP_WS10: 1446 return sd_setup_write_same10_cmnd(cmd, true); 1447 case SD_LBP_ZERO: 1448 return sd_setup_write_same10_cmnd(cmd, false); 1449 default: 1450 return BLK_STS_TARGET; 1451 } 1452 case REQ_OP_WRITE_ZEROES: 1453 return sd_setup_write_zeroes_cmnd(cmd); 1454 case REQ_OP_FLUSH: 1455 return sd_setup_flush_cmnd(cmd); 1456 case REQ_OP_READ: 1457 case REQ_OP_WRITE: 1458 return sd_setup_read_write_cmnd(cmd); 1459 case REQ_OP_ZONE_RESET: 1460 return sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_RESET_WRITE_POINTER, 1461 false); 1462 case REQ_OP_ZONE_RESET_ALL: 1463 return sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_RESET_WRITE_POINTER, 1464 true); 1465 case REQ_OP_ZONE_OPEN: 1466 return sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_OPEN_ZONE, false); 1467 case REQ_OP_ZONE_CLOSE: 1468 return sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_CLOSE_ZONE, false); 1469 case REQ_OP_ZONE_FINISH: 1470 return sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_FINISH_ZONE, false); 1471 default: 1472 WARN_ON_ONCE(1); 1473 return BLK_STS_NOTSUPP; 1474 } 1475 } 1476 1477 static void sd_uninit_command(struct scsi_cmnd *SCpnt) 1478 { 1479 struct request *rq = scsi_cmd_to_rq(SCpnt); 1480 1481 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) 1482 mempool_free(rq->special_vec.bv_page, sd_page_pool); 1483 } 1484 1485 static bool sd_need_revalidate(struct gendisk *disk, struct scsi_disk *sdkp) 1486 { 1487 if (sdkp->device->removable || sdkp->write_prot) { 1488 if (disk_check_media_change(disk)) 1489 return true; 1490 } 1491 1492 /* 1493 * Force a full rescan after ioctl(BLKRRPART). While the disk state has 1494 * nothing to do with partitions, BLKRRPART is used to force a full 1495 * revalidate after things like a format for historical reasons. 1496 */ 1497 return test_bit(GD_NEED_PART_SCAN, &disk->state); 1498 } 1499 1500 /** 1501 * sd_open - open a scsi disk device 1502 * @disk: disk to open 1503 * @mode: open mode 1504 * 1505 * Returns 0 if successful. Returns a negated errno value in case 1506 * of error. 1507 * 1508 * Note: This can be called from a user context (e.g. fsck(1) ) 1509 * or from within the kernel (e.g. as a result of a mount(1) ). 1510 * In the latter case @inode and @filp carry an abridged amount 1511 * of information as noted above. 1512 * 1513 * Locking: called with disk->open_mutex held. 1514 **/ 1515 static int sd_open(struct gendisk *disk, blk_mode_t mode) 1516 { 1517 struct scsi_disk *sdkp = scsi_disk(disk); 1518 struct scsi_device *sdev = sdkp->device; 1519 int retval; 1520 1521 if (scsi_device_get(sdev)) 1522 return -ENXIO; 1523 1524 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n")); 1525 1526 /* 1527 * If the device is in error recovery, wait until it is done. 1528 * If the device is offline, then disallow any access to it. 1529 */ 1530 retval = -ENXIO; 1531 if (!scsi_block_when_processing_errors(sdev)) 1532 goto error_out; 1533 1534 if (sd_need_revalidate(disk, sdkp)) 1535 sd_revalidate_disk(disk); 1536 1537 /* 1538 * If the drive is empty, just let the open fail. 1539 */ 1540 retval = -ENOMEDIUM; 1541 if (sdev->removable && !sdkp->media_present && 1542 !(mode & BLK_OPEN_NDELAY)) 1543 goto error_out; 1544 1545 /* 1546 * If the device has the write protect tab set, have the open fail 1547 * if the user expects to be able to write to the thing. 1548 */ 1549 retval = -EROFS; 1550 if (sdkp->write_prot && (mode & BLK_OPEN_WRITE)) 1551 goto error_out; 1552 1553 /* 1554 * It is possible that the disk changing stuff resulted in 1555 * the device being taken offline. If this is the case, 1556 * report this to the user, and don't pretend that the 1557 * open actually succeeded. 1558 */ 1559 retval = -ENXIO; 1560 if (!scsi_device_online(sdev)) 1561 goto error_out; 1562 1563 if ((atomic_inc_return(&sdkp->openers) == 1) && sdev->removable) { 1564 if (scsi_block_when_processing_errors(sdev)) 1565 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT); 1566 } 1567 1568 return 0; 1569 1570 error_out: 1571 scsi_device_put(sdev); 1572 return retval; 1573 } 1574 1575 /** 1576 * sd_release - invoked when the (last) close(2) is called on this 1577 * scsi disk. 1578 * @disk: disk to release 1579 * 1580 * Returns 0. 1581 * 1582 * Note: may block (uninterruptible) if error recovery is underway 1583 * on this disk. 1584 * 1585 * Locking: called with disk->open_mutex held. 1586 **/ 1587 static void sd_release(struct gendisk *disk) 1588 { 1589 struct scsi_disk *sdkp = scsi_disk(disk); 1590 struct scsi_device *sdev = sdkp->device; 1591 1592 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n")); 1593 1594 if (atomic_dec_return(&sdkp->openers) == 0 && sdev->removable) { 1595 if (scsi_block_when_processing_errors(sdev)) 1596 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW); 1597 } 1598 1599 scsi_device_put(sdev); 1600 } 1601 1602 static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo) 1603 { 1604 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk); 1605 struct scsi_device *sdp = sdkp->device; 1606 struct Scsi_Host *host = sdp->host; 1607 sector_t capacity = logical_to_sectors(sdp, sdkp->capacity); 1608 int diskinfo[4]; 1609 1610 /* default to most commonly used values */ 1611 diskinfo[0] = 0x40; /* 1 << 6 */ 1612 diskinfo[1] = 0x20; /* 1 << 5 */ 1613 diskinfo[2] = capacity >> 11; 1614 1615 /* override with calculated, extended default, or driver values */ 1616 if (host->hostt->bios_param) 1617 host->hostt->bios_param(sdp, bdev, capacity, diskinfo); 1618 else 1619 scsicam_bios_param(bdev, capacity, diskinfo); 1620 1621 geo->heads = diskinfo[0]; 1622 geo->sectors = diskinfo[1]; 1623 geo->cylinders = diskinfo[2]; 1624 return 0; 1625 } 1626 1627 /** 1628 * sd_ioctl - process an ioctl 1629 * @bdev: target block device 1630 * @mode: open mode 1631 * @cmd: ioctl command number 1632 * @arg: this is third argument given to ioctl(2) system call. 1633 * Often contains a pointer. 1634 * 1635 * Returns 0 if successful (some ioctls return positive numbers on 1636 * success as well). Returns a negated errno value in case of error. 1637 * 1638 * Note: most ioctls are forward onto the block subsystem or further 1639 * down in the scsi subsystem. 1640 **/ 1641 static int sd_ioctl(struct block_device *bdev, blk_mode_t mode, 1642 unsigned int cmd, unsigned long arg) 1643 { 1644 struct gendisk *disk = bdev->bd_disk; 1645 struct scsi_disk *sdkp = scsi_disk(disk); 1646 struct scsi_device *sdp = sdkp->device; 1647 void __user *p = (void __user *)arg; 1648 int error; 1649 1650 SCSI_LOG_IOCTL(1, sd_printk(KERN_INFO, sdkp, "sd_ioctl: disk=%s, " 1651 "cmd=0x%x\n", disk->disk_name, cmd)); 1652 1653 if (bdev_is_partition(bdev) && !capable(CAP_SYS_RAWIO)) 1654 return -ENOIOCTLCMD; 1655 1656 /* 1657 * If we are in the middle of error recovery, don't let anyone 1658 * else try and use this device. Also, if error recovery fails, it 1659 * may try and take the device offline, in which case all further 1660 * access to the device is prohibited. 1661 */ 1662 error = scsi_ioctl_block_when_processing_errors(sdp, cmd, 1663 (mode & BLK_OPEN_NDELAY)); 1664 if (error) 1665 return error; 1666 1667 if (is_sed_ioctl(cmd)) 1668 return sed_ioctl(sdkp->opal_dev, cmd, p); 1669 return scsi_ioctl(sdp, mode & BLK_OPEN_WRITE, cmd, p); 1670 } 1671 1672 static void set_media_not_present(struct scsi_disk *sdkp) 1673 { 1674 if (sdkp->media_present) 1675 sdkp->device->changed = 1; 1676 1677 if (sdkp->device->removable) { 1678 sdkp->media_present = 0; 1679 sdkp->capacity = 0; 1680 } 1681 } 1682 1683 static int media_not_present(struct scsi_disk *sdkp, 1684 struct scsi_sense_hdr *sshdr) 1685 { 1686 if (!scsi_sense_valid(sshdr)) 1687 return 0; 1688 1689 /* not invoked for commands that could return deferred errors */ 1690 switch (sshdr->sense_key) { 1691 case UNIT_ATTENTION: 1692 case NOT_READY: 1693 /* medium not present */ 1694 if (sshdr->asc == 0x3A) { 1695 set_media_not_present(sdkp); 1696 return 1; 1697 } 1698 } 1699 return 0; 1700 } 1701 1702 /** 1703 * sd_check_events - check media events 1704 * @disk: kernel device descriptor 1705 * @clearing: disk events currently being cleared 1706 * 1707 * Returns mask of DISK_EVENT_*. 1708 * 1709 * Note: this function is invoked from the block subsystem. 1710 **/ 1711 static unsigned int sd_check_events(struct gendisk *disk, unsigned int clearing) 1712 { 1713 struct scsi_disk *sdkp = disk->private_data; 1714 struct scsi_device *sdp; 1715 int retval; 1716 bool disk_changed; 1717 1718 if (!sdkp) 1719 return 0; 1720 1721 sdp = sdkp->device; 1722 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_check_events\n")); 1723 1724 /* 1725 * If the device is offline, don't send any commands - just pretend as 1726 * if the command failed. If the device ever comes back online, we 1727 * can deal with it then. It is only because of unrecoverable errors 1728 * that we would ever take a device offline in the first place. 1729 */ 1730 if (!scsi_device_online(sdp)) { 1731 set_media_not_present(sdkp); 1732 goto out; 1733 } 1734 1735 /* 1736 * Using TEST_UNIT_READY enables differentiation between drive with 1737 * no cartridge loaded - NOT READY, drive with changed cartridge - 1738 * UNIT ATTENTION, or with same cartridge - GOOD STATUS. 1739 * 1740 * Drives that auto spin down. eg iomega jaz 1G, will be started 1741 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever 1742 * sd_revalidate() is called. 1743 */ 1744 if (scsi_block_when_processing_errors(sdp)) { 1745 struct scsi_sense_hdr sshdr = { 0, }; 1746 1747 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, sdkp->max_retries, 1748 &sshdr); 1749 1750 /* failed to execute TUR, assume media not present */ 1751 if (retval < 0 || host_byte(retval)) { 1752 set_media_not_present(sdkp); 1753 goto out; 1754 } 1755 1756 if (media_not_present(sdkp, &sshdr)) 1757 goto out; 1758 } 1759 1760 /* 1761 * For removable scsi disk we have to recognise the presence 1762 * of a disk in the drive. 1763 */ 1764 if (!sdkp->media_present) 1765 sdp->changed = 1; 1766 sdkp->media_present = 1; 1767 out: 1768 /* 1769 * sdp->changed is set under the following conditions: 1770 * 1771 * Medium present state has changed in either direction. 1772 * Device has indicated UNIT_ATTENTION. 1773 */ 1774 disk_changed = sdp->changed; 1775 sdp->changed = 0; 1776 return disk_changed ? DISK_EVENT_MEDIA_CHANGE : 0; 1777 } 1778 1779 static int sd_sync_cache(struct scsi_disk *sdkp) 1780 { 1781 int res; 1782 struct scsi_device *sdp = sdkp->device; 1783 const int timeout = sdp->request_queue->rq_timeout 1784 * SD_FLUSH_TIMEOUT_MULTIPLIER; 1785 /* Leave the rest of the command zero to indicate flush everything. */ 1786 const unsigned char cmd[16] = { sdp->use_16_for_sync ? 1787 SYNCHRONIZE_CACHE_16 : SYNCHRONIZE_CACHE }; 1788 struct scsi_sense_hdr sshdr; 1789 struct scsi_failure failure_defs[] = { 1790 { 1791 .allowed = 3, 1792 .result = SCMD_FAILURE_RESULT_ANY, 1793 }, 1794 {} 1795 }; 1796 struct scsi_failures failures = { 1797 .failure_definitions = failure_defs, 1798 }; 1799 const struct scsi_exec_args exec_args = { 1800 .req_flags = BLK_MQ_REQ_PM, 1801 .sshdr = &sshdr, 1802 .failures = &failures, 1803 }; 1804 1805 if (!scsi_device_online(sdp)) 1806 return -ENODEV; 1807 1808 res = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, NULL, 0, timeout, 1809 sdkp->max_retries, &exec_args); 1810 if (res) { 1811 sd_print_result(sdkp, "Synchronize Cache(10) failed", res); 1812 1813 if (res < 0) 1814 return res; 1815 1816 if (scsi_status_is_check_condition(res) && 1817 scsi_sense_valid(&sshdr)) { 1818 sd_print_sense_hdr(sdkp, &sshdr); 1819 1820 /* we need to evaluate the error return */ 1821 if (sshdr.asc == 0x3a || /* medium not present */ 1822 sshdr.asc == 0x20 || /* invalid command */ 1823 (sshdr.asc == 0x74 && sshdr.ascq == 0x71)) /* drive is password locked */ 1824 /* this is no error here */ 1825 return 0; 1826 /* 1827 * This drive doesn't support sync and there's not much 1828 * we can do because this is called during shutdown 1829 * or suspend so just return success so those operations 1830 * can proceed. 1831 */ 1832 if (sshdr.sense_key == ILLEGAL_REQUEST) 1833 return 0; 1834 } 1835 1836 switch (host_byte(res)) { 1837 /* ignore errors due to racing a disconnection */ 1838 case DID_BAD_TARGET: 1839 case DID_NO_CONNECT: 1840 return 0; 1841 /* signal the upper layer it might try again */ 1842 case DID_BUS_BUSY: 1843 case DID_IMM_RETRY: 1844 case DID_REQUEUE: 1845 case DID_SOFT_ERROR: 1846 return -EBUSY; 1847 default: 1848 return -EIO; 1849 } 1850 } 1851 return 0; 1852 } 1853 1854 static void sd_rescan(struct device *dev) 1855 { 1856 struct scsi_disk *sdkp = dev_get_drvdata(dev); 1857 1858 sd_revalidate_disk(sdkp->disk); 1859 } 1860 1861 static int sd_get_unique_id(struct gendisk *disk, u8 id[16], 1862 enum blk_unique_id type) 1863 { 1864 struct scsi_device *sdev = scsi_disk(disk)->device; 1865 const struct scsi_vpd *vpd; 1866 const unsigned char *d; 1867 int ret = -ENXIO, len; 1868 1869 rcu_read_lock(); 1870 vpd = rcu_dereference(sdev->vpd_pg83); 1871 if (!vpd) 1872 goto out_unlock; 1873 1874 ret = -EINVAL; 1875 for (d = vpd->data + 4; d < vpd->data + vpd->len; d += d[3] + 4) { 1876 /* we only care about designators with LU association */ 1877 if (((d[1] >> 4) & 0x3) != 0x00) 1878 continue; 1879 if ((d[1] & 0xf) != type) 1880 continue; 1881 1882 /* 1883 * Only exit early if a 16-byte descriptor was found. Otherwise 1884 * keep looking as one with more entropy might still show up. 1885 */ 1886 len = d[3]; 1887 if (len != 8 && len != 12 && len != 16) 1888 continue; 1889 ret = len; 1890 memcpy(id, d + 4, len); 1891 if (len == 16) 1892 break; 1893 } 1894 out_unlock: 1895 rcu_read_unlock(); 1896 return ret; 1897 } 1898 1899 static int sd_scsi_to_pr_err(struct scsi_sense_hdr *sshdr, int result) 1900 { 1901 switch (host_byte(result)) { 1902 case DID_TRANSPORT_MARGINAL: 1903 case DID_TRANSPORT_DISRUPTED: 1904 case DID_BUS_BUSY: 1905 return PR_STS_RETRY_PATH_FAILURE; 1906 case DID_NO_CONNECT: 1907 return PR_STS_PATH_FAILED; 1908 case DID_TRANSPORT_FAILFAST: 1909 return PR_STS_PATH_FAST_FAILED; 1910 } 1911 1912 switch (status_byte(result)) { 1913 case SAM_STAT_RESERVATION_CONFLICT: 1914 return PR_STS_RESERVATION_CONFLICT; 1915 case SAM_STAT_CHECK_CONDITION: 1916 if (!scsi_sense_valid(sshdr)) 1917 return PR_STS_IOERR; 1918 1919 if (sshdr->sense_key == ILLEGAL_REQUEST && 1920 (sshdr->asc == 0x26 || sshdr->asc == 0x24)) 1921 return -EINVAL; 1922 1923 fallthrough; 1924 default: 1925 return PR_STS_IOERR; 1926 } 1927 } 1928 1929 static int sd_pr_in_command(struct block_device *bdev, u8 sa, 1930 unsigned char *data, int data_len) 1931 { 1932 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk); 1933 struct scsi_device *sdev = sdkp->device; 1934 struct scsi_sense_hdr sshdr; 1935 u8 cmd[10] = { PERSISTENT_RESERVE_IN, sa }; 1936 struct scsi_failure failure_defs[] = { 1937 { 1938 .sense = UNIT_ATTENTION, 1939 .asc = SCMD_FAILURE_ASC_ANY, 1940 .ascq = SCMD_FAILURE_ASCQ_ANY, 1941 .allowed = 5, 1942 .result = SAM_STAT_CHECK_CONDITION, 1943 }, 1944 {} 1945 }; 1946 struct scsi_failures failures = { 1947 .failure_definitions = failure_defs, 1948 }; 1949 const struct scsi_exec_args exec_args = { 1950 .sshdr = &sshdr, 1951 .failures = &failures, 1952 }; 1953 int result; 1954 1955 put_unaligned_be16(data_len, &cmd[7]); 1956 1957 result = scsi_execute_cmd(sdev, cmd, REQ_OP_DRV_IN, data, data_len, 1958 SD_TIMEOUT, sdkp->max_retries, &exec_args); 1959 if (scsi_status_is_check_condition(result) && 1960 scsi_sense_valid(&sshdr)) { 1961 sdev_printk(KERN_INFO, sdev, "PR command failed: %d\n", result); 1962 scsi_print_sense_hdr(sdev, NULL, &sshdr); 1963 } 1964 1965 if (result <= 0) 1966 return result; 1967 1968 return sd_scsi_to_pr_err(&sshdr, result); 1969 } 1970 1971 static int sd_pr_read_keys(struct block_device *bdev, struct pr_keys *keys_info) 1972 { 1973 int result, i, data_offset, num_copy_keys; 1974 u32 num_keys = keys_info->num_keys; 1975 int data_len = num_keys * 8 + 8; 1976 u8 *data; 1977 1978 data = kzalloc(data_len, GFP_KERNEL); 1979 if (!data) 1980 return -ENOMEM; 1981 1982 result = sd_pr_in_command(bdev, READ_KEYS, data, data_len); 1983 if (result) 1984 goto free_data; 1985 1986 keys_info->generation = get_unaligned_be32(&data[0]); 1987 keys_info->num_keys = get_unaligned_be32(&data[4]) / 8; 1988 1989 data_offset = 8; 1990 num_copy_keys = min(num_keys, keys_info->num_keys); 1991 1992 for (i = 0; i < num_copy_keys; i++) { 1993 keys_info->keys[i] = get_unaligned_be64(&data[data_offset]); 1994 data_offset += 8; 1995 } 1996 1997 free_data: 1998 kfree(data); 1999 return result; 2000 } 2001 2002 static int sd_pr_read_reservation(struct block_device *bdev, 2003 struct pr_held_reservation *rsv) 2004 { 2005 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk); 2006 struct scsi_device *sdev = sdkp->device; 2007 u8 data[24] = { }; 2008 int result, len; 2009 2010 result = sd_pr_in_command(bdev, READ_RESERVATION, data, sizeof(data)); 2011 if (result) 2012 return result; 2013 2014 len = get_unaligned_be32(&data[4]); 2015 if (!len) 2016 return 0; 2017 2018 /* Make sure we have at least the key and type */ 2019 if (len < 14) { 2020 sdev_printk(KERN_INFO, sdev, 2021 "READ RESERVATION failed due to short return buffer of %d bytes\n", 2022 len); 2023 return -EINVAL; 2024 } 2025 2026 rsv->generation = get_unaligned_be32(&data[0]); 2027 rsv->key = get_unaligned_be64(&data[8]); 2028 rsv->type = scsi_pr_type_to_block(data[21] & 0x0f); 2029 return 0; 2030 } 2031 2032 static int sd_pr_out_command(struct block_device *bdev, u8 sa, u64 key, 2033 u64 sa_key, enum scsi_pr_type type, u8 flags) 2034 { 2035 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk); 2036 struct scsi_device *sdev = sdkp->device; 2037 struct scsi_sense_hdr sshdr; 2038 struct scsi_failure failure_defs[] = { 2039 { 2040 .sense = UNIT_ATTENTION, 2041 .asc = SCMD_FAILURE_ASC_ANY, 2042 .ascq = SCMD_FAILURE_ASCQ_ANY, 2043 .allowed = 5, 2044 .result = SAM_STAT_CHECK_CONDITION, 2045 }, 2046 {} 2047 }; 2048 struct scsi_failures failures = { 2049 .failure_definitions = failure_defs, 2050 }; 2051 const struct scsi_exec_args exec_args = { 2052 .sshdr = &sshdr, 2053 .failures = &failures, 2054 }; 2055 int result; 2056 u8 cmd[16] = { 0, }; 2057 u8 data[24] = { 0, }; 2058 2059 cmd[0] = PERSISTENT_RESERVE_OUT; 2060 cmd[1] = sa; 2061 cmd[2] = type; 2062 put_unaligned_be32(sizeof(data), &cmd[5]); 2063 2064 put_unaligned_be64(key, &data[0]); 2065 put_unaligned_be64(sa_key, &data[8]); 2066 data[20] = flags; 2067 2068 result = scsi_execute_cmd(sdev, cmd, REQ_OP_DRV_OUT, &data, 2069 sizeof(data), SD_TIMEOUT, sdkp->max_retries, 2070 &exec_args); 2071 2072 if (scsi_status_is_check_condition(result) && 2073 scsi_sense_valid(&sshdr)) { 2074 sdev_printk(KERN_INFO, sdev, "PR command failed: %d\n", result); 2075 scsi_print_sense_hdr(sdev, NULL, &sshdr); 2076 } 2077 2078 if (result <= 0) 2079 return result; 2080 2081 return sd_scsi_to_pr_err(&sshdr, result); 2082 } 2083 2084 static int sd_pr_register(struct block_device *bdev, u64 old_key, u64 new_key, 2085 u32 flags) 2086 { 2087 if (flags & ~PR_FL_IGNORE_KEY) 2088 return -EOPNOTSUPP; 2089 return sd_pr_out_command(bdev, (flags & PR_FL_IGNORE_KEY) ? 0x06 : 0x00, 2090 old_key, new_key, 0, 2091 (1 << 0) /* APTPL */); 2092 } 2093 2094 static int sd_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type, 2095 u32 flags) 2096 { 2097 if (flags) 2098 return -EOPNOTSUPP; 2099 return sd_pr_out_command(bdev, 0x01, key, 0, 2100 block_pr_type_to_scsi(type), 0); 2101 } 2102 2103 static int sd_pr_release(struct block_device *bdev, u64 key, enum pr_type type) 2104 { 2105 return sd_pr_out_command(bdev, 0x02, key, 0, 2106 block_pr_type_to_scsi(type), 0); 2107 } 2108 2109 static int sd_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key, 2110 enum pr_type type, bool abort) 2111 { 2112 return sd_pr_out_command(bdev, abort ? 0x05 : 0x04, old_key, new_key, 2113 block_pr_type_to_scsi(type), 0); 2114 } 2115 2116 static int sd_pr_clear(struct block_device *bdev, u64 key) 2117 { 2118 return sd_pr_out_command(bdev, 0x03, key, 0, 0, 0); 2119 } 2120 2121 static const struct pr_ops sd_pr_ops = { 2122 .pr_register = sd_pr_register, 2123 .pr_reserve = sd_pr_reserve, 2124 .pr_release = sd_pr_release, 2125 .pr_preempt = sd_pr_preempt, 2126 .pr_clear = sd_pr_clear, 2127 .pr_read_keys = sd_pr_read_keys, 2128 .pr_read_reservation = sd_pr_read_reservation, 2129 }; 2130 2131 static void scsi_disk_free_disk(struct gendisk *disk) 2132 { 2133 struct scsi_disk *sdkp = scsi_disk(disk); 2134 2135 put_device(&sdkp->disk_dev); 2136 } 2137 2138 static const struct block_device_operations sd_fops = { 2139 .owner = THIS_MODULE, 2140 .open = sd_open, 2141 .release = sd_release, 2142 .ioctl = sd_ioctl, 2143 .getgeo = sd_getgeo, 2144 .compat_ioctl = blkdev_compat_ptr_ioctl, 2145 .check_events = sd_check_events, 2146 .unlock_native_capacity = sd_unlock_native_capacity, 2147 .report_zones = sd_zbc_report_zones, 2148 .get_unique_id = sd_get_unique_id, 2149 .free_disk = scsi_disk_free_disk, 2150 .pr_ops = &sd_pr_ops, 2151 }; 2152 2153 /** 2154 * sd_eh_reset - reset error handling callback 2155 * @scmd: sd-issued command that has failed 2156 * 2157 * This function is called by the SCSI midlayer before starting 2158 * SCSI EH. When counting medium access failures we have to be 2159 * careful to register it only only once per device and SCSI EH run; 2160 * there might be several timed out commands which will cause the 2161 * 'max_medium_access_timeouts' counter to trigger after the first 2162 * SCSI EH run already and set the device to offline. 2163 * So this function resets the internal counter before starting SCSI EH. 2164 **/ 2165 static void sd_eh_reset(struct scsi_cmnd *scmd) 2166 { 2167 struct scsi_disk *sdkp = scsi_disk(scsi_cmd_to_rq(scmd)->q->disk); 2168 2169 /* New SCSI EH run, reset gate variable */ 2170 sdkp->ignore_medium_access_errors = false; 2171 } 2172 2173 /** 2174 * sd_eh_action - error handling callback 2175 * @scmd: sd-issued command that has failed 2176 * @eh_disp: The recovery disposition suggested by the midlayer 2177 * 2178 * This function is called by the SCSI midlayer upon completion of an 2179 * error test command (currently TEST UNIT READY). The result of sending 2180 * the eh command is passed in eh_disp. We're looking for devices that 2181 * fail medium access commands but are OK with non access commands like 2182 * test unit ready (so wrongly see the device as having a successful 2183 * recovery) 2184 **/ 2185 static int sd_eh_action(struct scsi_cmnd *scmd, int eh_disp) 2186 { 2187 struct scsi_disk *sdkp = scsi_disk(scsi_cmd_to_rq(scmd)->q->disk); 2188 struct scsi_device *sdev = scmd->device; 2189 2190 if (!scsi_device_online(sdev) || 2191 !scsi_medium_access_command(scmd) || 2192 host_byte(scmd->result) != DID_TIME_OUT || 2193 eh_disp != SUCCESS) 2194 return eh_disp; 2195 2196 /* 2197 * The device has timed out executing a medium access command. 2198 * However, the TEST UNIT READY command sent during error 2199 * handling completed successfully. Either the device is in the 2200 * process of recovering or has it suffered an internal failure 2201 * that prevents access to the storage medium. 2202 */ 2203 if (!sdkp->ignore_medium_access_errors) { 2204 sdkp->medium_access_timed_out++; 2205 sdkp->ignore_medium_access_errors = true; 2206 } 2207 2208 /* 2209 * If the device keeps failing read/write commands but TEST UNIT 2210 * READY always completes successfully we assume that medium 2211 * access is no longer possible and take the device offline. 2212 */ 2213 if (sdkp->medium_access_timed_out >= sdkp->max_medium_access_timeouts) { 2214 scmd_printk(KERN_ERR, scmd, 2215 "Medium access timeout failure. Offlining disk!\n"); 2216 mutex_lock(&sdev->state_mutex); 2217 scsi_device_set_state(sdev, SDEV_OFFLINE); 2218 mutex_unlock(&sdev->state_mutex); 2219 2220 return SUCCESS; 2221 } 2222 2223 return eh_disp; 2224 } 2225 2226 static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd) 2227 { 2228 struct request *req = scsi_cmd_to_rq(scmd); 2229 struct scsi_device *sdev = scmd->device; 2230 unsigned int transferred, good_bytes; 2231 u64 start_lba, end_lba, bad_lba; 2232 2233 /* 2234 * Some commands have a payload smaller than the device logical 2235 * block size (e.g. INQUIRY on a 4K disk). 2236 */ 2237 if (scsi_bufflen(scmd) <= sdev->sector_size) 2238 return 0; 2239 2240 /* Check if we have a 'bad_lba' information */ 2241 if (!scsi_get_sense_info_fld(scmd->sense_buffer, 2242 SCSI_SENSE_BUFFERSIZE, 2243 &bad_lba)) 2244 return 0; 2245 2246 /* 2247 * If the bad lba was reported incorrectly, we have no idea where 2248 * the error is. 2249 */ 2250 start_lba = sectors_to_logical(sdev, blk_rq_pos(req)); 2251 end_lba = start_lba + bytes_to_logical(sdev, scsi_bufflen(scmd)); 2252 if (bad_lba < start_lba || bad_lba >= end_lba) 2253 return 0; 2254 2255 /* 2256 * resid is optional but mostly filled in. When it's unused, 2257 * its value is zero, so we assume the whole buffer transferred 2258 */ 2259 transferred = scsi_bufflen(scmd) - scsi_get_resid(scmd); 2260 2261 /* This computation should always be done in terms of the 2262 * resolution of the device's medium. 2263 */ 2264 good_bytes = logical_to_bytes(sdev, bad_lba - start_lba); 2265 2266 return min(good_bytes, transferred); 2267 } 2268 2269 /** 2270 * sd_done - bottom half handler: called when the lower level 2271 * driver has completed (successfully or otherwise) a scsi command. 2272 * @SCpnt: mid-level's per command structure. 2273 * 2274 * Note: potentially run from within an ISR. Must not block. 2275 **/ 2276 static int sd_done(struct scsi_cmnd *SCpnt) 2277 { 2278 int result = SCpnt->result; 2279 unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt); 2280 unsigned int sector_size = SCpnt->device->sector_size; 2281 unsigned int resid; 2282 struct scsi_sense_hdr sshdr; 2283 struct request *req = scsi_cmd_to_rq(SCpnt); 2284 struct scsi_disk *sdkp = scsi_disk(req->q->disk); 2285 int sense_valid = 0; 2286 int sense_deferred = 0; 2287 2288 switch (req_op(req)) { 2289 case REQ_OP_DISCARD: 2290 case REQ_OP_WRITE_ZEROES: 2291 case REQ_OP_ZONE_RESET: 2292 case REQ_OP_ZONE_RESET_ALL: 2293 case REQ_OP_ZONE_OPEN: 2294 case REQ_OP_ZONE_CLOSE: 2295 case REQ_OP_ZONE_FINISH: 2296 if (!result) { 2297 good_bytes = blk_rq_bytes(req); 2298 scsi_set_resid(SCpnt, 0); 2299 } else { 2300 good_bytes = 0; 2301 scsi_set_resid(SCpnt, blk_rq_bytes(req)); 2302 } 2303 break; 2304 default: 2305 /* 2306 * In case of bogus fw or device, we could end up having 2307 * an unaligned partial completion. Check this here and force 2308 * alignment. 2309 */ 2310 resid = scsi_get_resid(SCpnt); 2311 if (resid & (sector_size - 1)) { 2312 sd_printk(KERN_INFO, sdkp, 2313 "Unaligned partial completion (resid=%u, sector_sz=%u)\n", 2314 resid, sector_size); 2315 scsi_print_command(SCpnt); 2316 resid = min(scsi_bufflen(SCpnt), 2317 round_up(resid, sector_size)); 2318 scsi_set_resid(SCpnt, resid); 2319 } 2320 } 2321 2322 if (result) { 2323 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr); 2324 if (sense_valid) 2325 sense_deferred = scsi_sense_is_deferred(&sshdr); 2326 } 2327 sdkp->medium_access_timed_out = 0; 2328 2329 if (!scsi_status_is_check_condition(result) && 2330 (!sense_valid || sense_deferred)) 2331 goto out; 2332 2333 switch (sshdr.sense_key) { 2334 case HARDWARE_ERROR: 2335 case MEDIUM_ERROR: 2336 good_bytes = sd_completed_bytes(SCpnt); 2337 break; 2338 case RECOVERED_ERROR: 2339 good_bytes = scsi_bufflen(SCpnt); 2340 break; 2341 case NO_SENSE: 2342 /* This indicates a false check condition, so ignore it. An 2343 * unknown amount of data was transferred so treat it as an 2344 * error. 2345 */ 2346 SCpnt->result = 0; 2347 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); 2348 break; 2349 case ABORTED_COMMAND: 2350 if (sshdr.asc == 0x10) /* DIF: Target detected corruption */ 2351 good_bytes = sd_completed_bytes(SCpnt); 2352 break; 2353 case ILLEGAL_REQUEST: 2354 switch (sshdr.asc) { 2355 case 0x10: /* DIX: Host detected corruption */ 2356 good_bytes = sd_completed_bytes(SCpnt); 2357 break; 2358 case 0x20: /* INVALID COMMAND OPCODE */ 2359 case 0x24: /* INVALID FIELD IN CDB */ 2360 switch (SCpnt->cmnd[0]) { 2361 case UNMAP: 2362 sd_disable_discard(sdkp); 2363 break; 2364 case WRITE_SAME_16: 2365 case WRITE_SAME: 2366 if (SCpnt->cmnd[1] & 8) { /* UNMAP */ 2367 sd_disable_discard(sdkp); 2368 } else { 2369 sd_disable_write_same(sdkp); 2370 req->rq_flags |= RQF_QUIET; 2371 } 2372 break; 2373 } 2374 } 2375 break; 2376 default: 2377 break; 2378 } 2379 2380 out: 2381 if (sdkp->device->type == TYPE_ZBC) 2382 good_bytes = sd_zbc_complete(SCpnt, good_bytes, &sshdr); 2383 2384 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt, 2385 "sd_done: completed %d of %d bytes\n", 2386 good_bytes, scsi_bufflen(SCpnt))); 2387 2388 return good_bytes; 2389 } 2390 2391 /* 2392 * spinup disk - called only in sd_revalidate_disk() 2393 */ 2394 static void 2395 sd_spinup_disk(struct scsi_disk *sdkp) 2396 { 2397 static const u8 cmd[10] = { TEST_UNIT_READY }; 2398 unsigned long spintime_expire = 0; 2399 int spintime, sense_valid = 0; 2400 unsigned int the_result; 2401 struct scsi_sense_hdr sshdr; 2402 struct scsi_failure failure_defs[] = { 2403 /* Do not retry Medium Not Present */ 2404 { 2405 .sense = UNIT_ATTENTION, 2406 .asc = 0x3A, 2407 .ascq = SCMD_FAILURE_ASCQ_ANY, 2408 .result = SAM_STAT_CHECK_CONDITION, 2409 }, 2410 { 2411 .sense = NOT_READY, 2412 .asc = 0x3A, 2413 .ascq = SCMD_FAILURE_ASCQ_ANY, 2414 .result = SAM_STAT_CHECK_CONDITION, 2415 }, 2416 /* Retry when scsi_status_is_good would return false 3 times */ 2417 { 2418 .result = SCMD_FAILURE_STAT_ANY, 2419 .allowed = 3, 2420 }, 2421 {} 2422 }; 2423 struct scsi_failures failures = { 2424 .failure_definitions = failure_defs, 2425 }; 2426 const struct scsi_exec_args exec_args = { 2427 .sshdr = &sshdr, 2428 .failures = &failures, 2429 }; 2430 2431 spintime = 0; 2432 2433 /* Spin up drives, as required. Only do this at boot time */ 2434 /* Spinup needs to be done for module loads too. */ 2435 do { 2436 bool media_was_present = sdkp->media_present; 2437 2438 scsi_failures_reset_retries(&failures); 2439 2440 the_result = scsi_execute_cmd(sdkp->device, cmd, REQ_OP_DRV_IN, 2441 NULL, 0, SD_TIMEOUT, 2442 sdkp->max_retries, &exec_args); 2443 2444 2445 if (the_result > 0) { 2446 /* 2447 * If the drive has indicated to us that it doesn't 2448 * have any media in it, don't bother with any more 2449 * polling. 2450 */ 2451 if (media_not_present(sdkp, &sshdr)) { 2452 if (media_was_present) 2453 sd_printk(KERN_NOTICE, sdkp, 2454 "Media removed, stopped polling\n"); 2455 return; 2456 } 2457 sense_valid = scsi_sense_valid(&sshdr); 2458 } 2459 2460 if (!scsi_status_is_check_condition(the_result)) { 2461 /* no sense, TUR either succeeded or failed 2462 * with a status error */ 2463 if(!spintime && !scsi_status_is_good(the_result)) { 2464 sd_print_result(sdkp, "Test Unit Ready failed", 2465 the_result); 2466 } 2467 break; 2468 } 2469 2470 /* 2471 * The device does not want the automatic start to be issued. 2472 */ 2473 if (sdkp->device->no_start_on_add) 2474 break; 2475 2476 if (sense_valid && sshdr.sense_key == NOT_READY) { 2477 if (sshdr.asc == 4 && sshdr.ascq == 3) 2478 break; /* manual intervention required */ 2479 if (sshdr.asc == 4 && sshdr.ascq == 0xb) 2480 break; /* standby */ 2481 if (sshdr.asc == 4 && sshdr.ascq == 0xc) 2482 break; /* unavailable */ 2483 if (sshdr.asc == 4 && sshdr.ascq == 0x1b) 2484 break; /* sanitize in progress */ 2485 if (sshdr.asc == 4 && sshdr.ascq == 0x24) 2486 break; /* depopulation in progress */ 2487 if (sshdr.asc == 4 && sshdr.ascq == 0x25) 2488 break; /* depopulation restoration in progress */ 2489 /* 2490 * Issue command to spin up drive when not ready 2491 */ 2492 if (!spintime) { 2493 /* Return immediately and start spin cycle */ 2494 const u8 start_cmd[10] = { 2495 [0] = START_STOP, 2496 [1] = 1, 2497 [4] = sdkp->device->start_stop_pwr_cond ? 2498 0x11 : 1, 2499 }; 2500 2501 sd_printk(KERN_NOTICE, sdkp, "Spinning up disk..."); 2502 scsi_execute_cmd(sdkp->device, start_cmd, 2503 REQ_OP_DRV_IN, NULL, 0, 2504 SD_TIMEOUT, sdkp->max_retries, 2505 &exec_args); 2506 spintime_expire = jiffies + 100 * HZ; 2507 spintime = 1; 2508 } 2509 /* Wait 1 second for next try */ 2510 msleep(1000); 2511 printk(KERN_CONT "."); 2512 2513 /* 2514 * Wait for USB flash devices with slow firmware. 2515 * Yes, this sense key/ASC combination shouldn't 2516 * occur here. It's characteristic of these devices. 2517 */ 2518 } else if (sense_valid && 2519 sshdr.sense_key == UNIT_ATTENTION && 2520 sshdr.asc == 0x28) { 2521 if (!spintime) { 2522 spintime_expire = jiffies + 5 * HZ; 2523 spintime = 1; 2524 } 2525 /* Wait 1 second for next try */ 2526 msleep(1000); 2527 } else { 2528 /* we don't understand the sense code, so it's 2529 * probably pointless to loop */ 2530 if(!spintime) { 2531 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n"); 2532 sd_print_sense_hdr(sdkp, &sshdr); 2533 } 2534 break; 2535 } 2536 2537 } while (spintime && time_before_eq(jiffies, spintime_expire)); 2538 2539 if (spintime) { 2540 if (scsi_status_is_good(the_result)) 2541 printk(KERN_CONT "ready\n"); 2542 else 2543 printk(KERN_CONT "not responding...\n"); 2544 } 2545 } 2546 2547 /* 2548 * Determine whether disk supports Data Integrity Field. 2549 */ 2550 static int sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer) 2551 { 2552 struct scsi_device *sdp = sdkp->device; 2553 u8 type; 2554 2555 if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0) { 2556 sdkp->protection_type = 0; 2557 return 0; 2558 } 2559 2560 type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */ 2561 2562 if (type > T10_PI_TYPE3_PROTECTION) { 2563 sd_printk(KERN_ERR, sdkp, "formatted with unsupported" \ 2564 " protection type %u. Disabling disk!\n", 2565 type); 2566 sdkp->protection_type = 0; 2567 return -ENODEV; 2568 } 2569 2570 sdkp->protection_type = type; 2571 2572 return 0; 2573 } 2574 2575 static void sd_config_protection(struct scsi_disk *sdkp, 2576 struct queue_limits *lim) 2577 { 2578 struct scsi_device *sdp = sdkp->device; 2579 2580 if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)) 2581 sd_dif_config_host(sdkp, lim); 2582 2583 if (!sdkp->protection_type) 2584 return; 2585 2586 if (!scsi_host_dif_capable(sdp->host, sdkp->protection_type)) { 2587 sd_first_printk(KERN_NOTICE, sdkp, 2588 "Disabling DIF Type %u protection\n", 2589 sdkp->protection_type); 2590 sdkp->protection_type = 0; 2591 } 2592 2593 sd_first_printk(KERN_NOTICE, sdkp, "Enabling DIF Type %u protection\n", 2594 sdkp->protection_type); 2595 } 2596 2597 static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp, 2598 struct scsi_sense_hdr *sshdr, int sense_valid, 2599 int the_result) 2600 { 2601 if (sense_valid) 2602 sd_print_sense_hdr(sdkp, sshdr); 2603 else 2604 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n"); 2605 2606 /* 2607 * Set dirty bit for removable devices if not ready - 2608 * sometimes drives will not report this properly. 2609 */ 2610 if (sdp->removable && 2611 sense_valid && sshdr->sense_key == NOT_READY) 2612 set_media_not_present(sdkp); 2613 2614 /* 2615 * We used to set media_present to 0 here to indicate no media 2616 * in the drive, but some drives fail read capacity even with 2617 * media present, so we can't do that. 2618 */ 2619 sdkp->capacity = 0; /* unknown mapped to zero - as usual */ 2620 } 2621 2622 #define RC16_LEN 32 2623 #if RC16_LEN > SD_BUF_SIZE 2624 #error RC16_LEN must not be more than SD_BUF_SIZE 2625 #endif 2626 2627 #define READ_CAPACITY_RETRIES_ON_RESET 10 2628 2629 static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp, 2630 struct queue_limits *lim, unsigned char *buffer) 2631 { 2632 unsigned char cmd[16]; 2633 struct scsi_sense_hdr sshdr; 2634 const struct scsi_exec_args exec_args = { 2635 .sshdr = &sshdr, 2636 }; 2637 int sense_valid = 0; 2638 int the_result; 2639 int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET; 2640 unsigned int alignment; 2641 unsigned long long lba; 2642 unsigned sector_size; 2643 2644 if (sdp->no_read_capacity_16) 2645 return -EINVAL; 2646 2647 do { 2648 memset(cmd, 0, 16); 2649 cmd[0] = SERVICE_ACTION_IN_16; 2650 cmd[1] = SAI_READ_CAPACITY_16; 2651 cmd[13] = RC16_LEN; 2652 memset(buffer, 0, RC16_LEN); 2653 2654 the_result = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, 2655 buffer, RC16_LEN, SD_TIMEOUT, 2656 sdkp->max_retries, &exec_args); 2657 if (the_result > 0) { 2658 if (media_not_present(sdkp, &sshdr)) 2659 return -ENODEV; 2660 2661 sense_valid = scsi_sense_valid(&sshdr); 2662 if (sense_valid && 2663 sshdr.sense_key == ILLEGAL_REQUEST && 2664 (sshdr.asc == 0x20 || sshdr.asc == 0x24) && 2665 sshdr.ascq == 0x00) 2666 /* Invalid Command Operation Code or 2667 * Invalid Field in CDB, just retry 2668 * silently with RC10 */ 2669 return -EINVAL; 2670 if (sense_valid && 2671 sshdr.sense_key == UNIT_ATTENTION && 2672 sshdr.asc == 0x29 && sshdr.ascq == 0x00) 2673 /* Device reset might occur several times, 2674 * give it one more chance */ 2675 if (--reset_retries > 0) 2676 continue; 2677 } 2678 retries--; 2679 2680 } while (the_result && retries); 2681 2682 if (the_result) { 2683 sd_print_result(sdkp, "Read Capacity(16) failed", the_result); 2684 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result); 2685 return -EINVAL; 2686 } 2687 2688 sector_size = get_unaligned_be32(&buffer[8]); 2689 lba = get_unaligned_be64(&buffer[0]); 2690 2691 if (sd_read_protection_type(sdkp, buffer) < 0) { 2692 sdkp->capacity = 0; 2693 return -ENODEV; 2694 } 2695 2696 /* Logical blocks per physical block exponent */ 2697 sdkp->physical_block_size = (1 << (buffer[13] & 0xf)) * sector_size; 2698 2699 /* RC basis */ 2700 sdkp->rc_basis = (buffer[12] >> 4) & 0x3; 2701 2702 /* Lowest aligned logical block */ 2703 alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size; 2704 lim->alignment_offset = alignment; 2705 if (alignment && sdkp->first_scan) 2706 sd_printk(KERN_NOTICE, sdkp, 2707 "physical block alignment offset: %u\n", alignment); 2708 2709 if (buffer[14] & 0x80) { /* LBPME */ 2710 sdkp->lbpme = 1; 2711 2712 if (buffer[14] & 0x40) /* LBPRZ */ 2713 sdkp->lbprz = 1; 2714 2715 sd_config_discard(sdkp, lim, SD_LBP_WS16); 2716 } 2717 2718 sdkp->capacity = lba + 1; 2719 return sector_size; 2720 } 2721 2722 static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp, 2723 unsigned char *buffer) 2724 { 2725 static const u8 cmd[10] = { READ_CAPACITY }; 2726 struct scsi_sense_hdr sshdr; 2727 struct scsi_failure failure_defs[] = { 2728 /* Do not retry Medium Not Present */ 2729 { 2730 .sense = UNIT_ATTENTION, 2731 .asc = 0x3A, 2732 .result = SAM_STAT_CHECK_CONDITION, 2733 }, 2734 { 2735 .sense = NOT_READY, 2736 .asc = 0x3A, 2737 .result = SAM_STAT_CHECK_CONDITION, 2738 }, 2739 /* Device reset might occur several times so retry a lot */ 2740 { 2741 .sense = UNIT_ATTENTION, 2742 .asc = 0x29, 2743 .allowed = READ_CAPACITY_RETRIES_ON_RESET, 2744 .result = SAM_STAT_CHECK_CONDITION, 2745 }, 2746 /* Any other error not listed above retry 3 times */ 2747 { 2748 .result = SCMD_FAILURE_RESULT_ANY, 2749 .allowed = 3, 2750 }, 2751 {} 2752 }; 2753 struct scsi_failures failures = { 2754 .failure_definitions = failure_defs, 2755 }; 2756 const struct scsi_exec_args exec_args = { 2757 .sshdr = &sshdr, 2758 .failures = &failures, 2759 }; 2760 int sense_valid = 0; 2761 int the_result; 2762 sector_t lba; 2763 unsigned sector_size; 2764 2765 memset(buffer, 0, 8); 2766 2767 the_result = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, buffer, 2768 8, SD_TIMEOUT, sdkp->max_retries, 2769 &exec_args); 2770 2771 if (the_result > 0) { 2772 sense_valid = scsi_sense_valid(&sshdr); 2773 2774 if (media_not_present(sdkp, &sshdr)) 2775 return -ENODEV; 2776 } 2777 2778 if (the_result) { 2779 sd_print_result(sdkp, "Read Capacity(10) failed", the_result); 2780 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result); 2781 return -EINVAL; 2782 } 2783 2784 sector_size = get_unaligned_be32(&buffer[4]); 2785 lba = get_unaligned_be32(&buffer[0]); 2786 2787 if (sdp->no_read_capacity_16 && (lba == 0xffffffff)) { 2788 /* Some buggy (usb cardreader) devices return an lba of 2789 0xffffffff when the want to report a size of 0 (with 2790 which they really mean no media is present) */ 2791 sdkp->capacity = 0; 2792 sdkp->physical_block_size = sector_size; 2793 return sector_size; 2794 } 2795 2796 sdkp->capacity = lba + 1; 2797 sdkp->physical_block_size = sector_size; 2798 return sector_size; 2799 } 2800 2801 static int sd_try_rc16_first(struct scsi_device *sdp) 2802 { 2803 if (sdp->host->max_cmd_len < 16) 2804 return 0; 2805 if (sdp->try_rc_10_first) 2806 return 0; 2807 if (sdp->scsi_level > SCSI_SPC_2) 2808 return 1; 2809 if (scsi_device_protection(sdp)) 2810 return 1; 2811 return 0; 2812 } 2813 2814 /* 2815 * read disk capacity 2816 */ 2817 static void 2818 sd_read_capacity(struct scsi_disk *sdkp, struct queue_limits *lim, 2819 unsigned char *buffer) 2820 { 2821 int sector_size; 2822 struct scsi_device *sdp = sdkp->device; 2823 2824 if (sd_try_rc16_first(sdp)) { 2825 sector_size = read_capacity_16(sdkp, sdp, lim, buffer); 2826 if (sector_size == -EOVERFLOW) 2827 goto got_data; 2828 if (sector_size == -ENODEV) 2829 return; 2830 if (sector_size < 0) 2831 sector_size = read_capacity_10(sdkp, sdp, buffer); 2832 if (sector_size < 0) 2833 return; 2834 } else { 2835 sector_size = read_capacity_10(sdkp, sdp, buffer); 2836 if (sector_size == -EOVERFLOW) 2837 goto got_data; 2838 if (sector_size < 0) 2839 return; 2840 if ((sizeof(sdkp->capacity) > 4) && 2841 (sdkp->capacity > 0xffffffffULL)) { 2842 int old_sector_size = sector_size; 2843 sd_printk(KERN_NOTICE, sdkp, "Very big device. " 2844 "Trying to use READ CAPACITY(16).\n"); 2845 sector_size = read_capacity_16(sdkp, sdp, lim, buffer); 2846 if (sector_size < 0) { 2847 sd_printk(KERN_NOTICE, sdkp, 2848 "Using 0xffffffff as device size\n"); 2849 sdkp->capacity = 1 + (sector_t) 0xffffffff; 2850 sector_size = old_sector_size; 2851 goto got_data; 2852 } 2853 /* Remember that READ CAPACITY(16) succeeded */ 2854 sdp->try_rc_10_first = 0; 2855 } 2856 } 2857 2858 /* Some devices are known to return the total number of blocks, 2859 * not the highest block number. Some devices have versions 2860 * which do this and others which do not. Some devices we might 2861 * suspect of doing this but we don't know for certain. 2862 * 2863 * If we know the reported capacity is wrong, decrement it. If 2864 * we can only guess, then assume the number of blocks is even 2865 * (usually true but not always) and err on the side of lowering 2866 * the capacity. 2867 */ 2868 if (sdp->fix_capacity || 2869 (sdp->guess_capacity && (sdkp->capacity & 0x01))) { 2870 sd_printk(KERN_INFO, sdkp, "Adjusting the sector count " 2871 "from its reported value: %llu\n", 2872 (unsigned long long) sdkp->capacity); 2873 --sdkp->capacity; 2874 } 2875 2876 got_data: 2877 if (sector_size == 0) { 2878 sector_size = 512; 2879 sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, " 2880 "assuming 512.\n"); 2881 } 2882 2883 if (sector_size != 512 && 2884 sector_size != 1024 && 2885 sector_size != 2048 && 2886 sector_size != 4096) { 2887 sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n", 2888 sector_size); 2889 /* 2890 * The user might want to re-format the drive with 2891 * a supported sectorsize. Once this happens, it 2892 * would be relatively trivial to set the thing up. 2893 * For this reason, we leave the thing in the table. 2894 */ 2895 sdkp->capacity = 0; 2896 /* 2897 * set a bogus sector size so the normal read/write 2898 * logic in the block layer will eventually refuse any 2899 * request on this device without tripping over power 2900 * of two sector size assumptions 2901 */ 2902 sector_size = 512; 2903 } 2904 lim->logical_block_size = sector_size; 2905 lim->physical_block_size = sdkp->physical_block_size; 2906 sdkp->device->sector_size = sector_size; 2907 2908 if (sdkp->capacity > 0xffffffff) 2909 sdp->use_16_for_rw = 1; 2910 2911 } 2912 2913 /* 2914 * Print disk capacity 2915 */ 2916 static void 2917 sd_print_capacity(struct scsi_disk *sdkp, 2918 sector_t old_capacity) 2919 { 2920 int sector_size = sdkp->device->sector_size; 2921 char cap_str_2[10], cap_str_10[10]; 2922 2923 if (!sdkp->first_scan && old_capacity == sdkp->capacity) 2924 return; 2925 2926 string_get_size(sdkp->capacity, sector_size, 2927 STRING_UNITS_2, cap_str_2, sizeof(cap_str_2)); 2928 string_get_size(sdkp->capacity, sector_size, 2929 STRING_UNITS_10, cap_str_10, sizeof(cap_str_10)); 2930 2931 sd_printk(KERN_NOTICE, sdkp, 2932 "%llu %d-byte logical blocks: (%s/%s)\n", 2933 (unsigned long long)sdkp->capacity, 2934 sector_size, cap_str_10, cap_str_2); 2935 2936 if (sdkp->physical_block_size != sector_size) 2937 sd_printk(KERN_NOTICE, sdkp, 2938 "%u-byte physical blocks\n", 2939 sdkp->physical_block_size); 2940 } 2941 2942 /* called with buffer of length 512 */ 2943 static inline int 2944 sd_do_mode_sense(struct scsi_disk *sdkp, int dbd, int modepage, 2945 unsigned char *buffer, int len, struct scsi_mode_data *data, 2946 struct scsi_sense_hdr *sshdr) 2947 { 2948 /* 2949 * If we must use MODE SENSE(10), make sure that the buffer length 2950 * is at least 8 bytes so that the mode sense header fits. 2951 */ 2952 if (sdkp->device->use_10_for_ms && len < 8) 2953 len = 8; 2954 2955 return scsi_mode_sense(sdkp->device, dbd, modepage, 0, buffer, len, 2956 SD_TIMEOUT, sdkp->max_retries, data, sshdr); 2957 } 2958 2959 /* 2960 * read write protect setting, if possible - called only in sd_revalidate_disk() 2961 * called with buffer of length SD_BUF_SIZE 2962 */ 2963 static void 2964 sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer) 2965 { 2966 int res; 2967 struct scsi_device *sdp = sdkp->device; 2968 struct scsi_mode_data data; 2969 int old_wp = sdkp->write_prot; 2970 2971 set_disk_ro(sdkp->disk, 0); 2972 if (sdp->skip_ms_page_3f) { 2973 sd_first_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n"); 2974 return; 2975 } 2976 2977 if (sdp->use_192_bytes_for_3f) { 2978 res = sd_do_mode_sense(sdkp, 0, 0x3F, buffer, 192, &data, NULL); 2979 } else { 2980 /* 2981 * First attempt: ask for all pages (0x3F), but only 4 bytes. 2982 * We have to start carefully: some devices hang if we ask 2983 * for more than is available. 2984 */ 2985 res = sd_do_mode_sense(sdkp, 0, 0x3F, buffer, 4, &data, NULL); 2986 2987 /* 2988 * Second attempt: ask for page 0 When only page 0 is 2989 * implemented, a request for page 3F may return Sense Key 2990 * 5: Illegal Request, Sense Code 24: Invalid field in 2991 * CDB. 2992 */ 2993 if (res < 0) 2994 res = sd_do_mode_sense(sdkp, 0, 0, buffer, 4, &data, NULL); 2995 2996 /* 2997 * Third attempt: ask 255 bytes, as we did earlier. 2998 */ 2999 if (res < 0) 3000 res = sd_do_mode_sense(sdkp, 0, 0x3F, buffer, 255, 3001 &data, NULL); 3002 } 3003 3004 if (res < 0) { 3005 sd_first_printk(KERN_WARNING, sdkp, 3006 "Test WP failed, assume Write Enabled\n"); 3007 } else { 3008 sdkp->write_prot = ((data.device_specific & 0x80) != 0); 3009 set_disk_ro(sdkp->disk, sdkp->write_prot); 3010 if (sdkp->first_scan || old_wp != sdkp->write_prot) { 3011 sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n", 3012 sdkp->write_prot ? "on" : "off"); 3013 sd_printk(KERN_DEBUG, sdkp, "Mode Sense: %4ph\n", buffer); 3014 } 3015 } 3016 } 3017 3018 /* 3019 * sd_read_cache_type - called only from sd_revalidate_disk() 3020 * called with buffer of length SD_BUF_SIZE 3021 */ 3022 static void 3023 sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer) 3024 { 3025 int len = 0, res; 3026 struct scsi_device *sdp = sdkp->device; 3027 3028 int dbd; 3029 int modepage; 3030 int first_len; 3031 struct scsi_mode_data data; 3032 struct scsi_sense_hdr sshdr; 3033 int old_wce = sdkp->WCE; 3034 int old_rcd = sdkp->RCD; 3035 int old_dpofua = sdkp->DPOFUA; 3036 3037 3038 if (sdkp->cache_override) 3039 return; 3040 3041 first_len = 4; 3042 if (sdp->skip_ms_page_8) { 3043 if (sdp->type == TYPE_RBC) 3044 goto defaults; 3045 else { 3046 if (sdp->skip_ms_page_3f) 3047 goto defaults; 3048 modepage = 0x3F; 3049 if (sdp->use_192_bytes_for_3f) 3050 first_len = 192; 3051 dbd = 0; 3052 } 3053 } else if (sdp->type == TYPE_RBC) { 3054 modepage = 6; 3055 dbd = 8; 3056 } else { 3057 modepage = 8; 3058 dbd = 0; 3059 } 3060 3061 /* cautiously ask */ 3062 res = sd_do_mode_sense(sdkp, dbd, modepage, buffer, first_len, 3063 &data, &sshdr); 3064 3065 if (res < 0) 3066 goto bad_sense; 3067 3068 if (!data.header_length) { 3069 modepage = 6; 3070 first_len = 0; 3071 sd_first_printk(KERN_ERR, sdkp, 3072 "Missing header in MODE_SENSE response\n"); 3073 } 3074 3075 /* that went OK, now ask for the proper length */ 3076 len = data.length; 3077 3078 /* 3079 * We're only interested in the first three bytes, actually. 3080 * But the data cache page is defined for the first 20. 3081 */ 3082 if (len < 3) 3083 goto bad_sense; 3084 else if (len > SD_BUF_SIZE) { 3085 sd_first_printk(KERN_NOTICE, sdkp, "Truncating mode parameter " 3086 "data from %d to %d bytes\n", len, SD_BUF_SIZE); 3087 len = SD_BUF_SIZE; 3088 } 3089 if (modepage == 0x3F && sdp->use_192_bytes_for_3f) 3090 len = 192; 3091 3092 /* Get the data */ 3093 if (len > first_len) 3094 res = sd_do_mode_sense(sdkp, dbd, modepage, buffer, len, 3095 &data, &sshdr); 3096 3097 if (!res) { 3098 int offset = data.header_length + data.block_descriptor_length; 3099 3100 while (offset < len) { 3101 u8 page_code = buffer[offset] & 0x3F; 3102 u8 spf = buffer[offset] & 0x40; 3103 3104 if (page_code == 8 || page_code == 6) { 3105 /* We're interested only in the first 3 bytes. 3106 */ 3107 if (len - offset <= 2) { 3108 sd_first_printk(KERN_ERR, sdkp, 3109 "Incomplete mode parameter " 3110 "data\n"); 3111 goto defaults; 3112 } else { 3113 modepage = page_code; 3114 goto Page_found; 3115 } 3116 } else { 3117 /* Go to the next page */ 3118 if (spf && len - offset > 3) 3119 offset += 4 + (buffer[offset+2] << 8) + 3120 buffer[offset+3]; 3121 else if (!spf && len - offset > 1) 3122 offset += 2 + buffer[offset+1]; 3123 else { 3124 sd_first_printk(KERN_ERR, sdkp, 3125 "Incomplete mode " 3126 "parameter data\n"); 3127 goto defaults; 3128 } 3129 } 3130 } 3131 3132 sd_first_printk(KERN_WARNING, sdkp, 3133 "No Caching mode page found\n"); 3134 goto defaults; 3135 3136 Page_found: 3137 if (modepage == 8) { 3138 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0); 3139 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0); 3140 } else { 3141 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0); 3142 sdkp->RCD = 0; 3143 } 3144 3145 sdkp->DPOFUA = (data.device_specific & 0x10) != 0; 3146 if (sdp->broken_fua) { 3147 sd_first_printk(KERN_NOTICE, sdkp, "Disabling FUA\n"); 3148 sdkp->DPOFUA = 0; 3149 } else if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw && 3150 !sdkp->device->use_16_for_rw) { 3151 sd_first_printk(KERN_NOTICE, sdkp, 3152 "Uses READ/WRITE(6), disabling FUA\n"); 3153 sdkp->DPOFUA = 0; 3154 } 3155 3156 /* No cache flush allowed for write protected devices */ 3157 if (sdkp->WCE && sdkp->write_prot) 3158 sdkp->WCE = 0; 3159 3160 if (sdkp->first_scan || old_wce != sdkp->WCE || 3161 old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA) 3162 sd_printk(KERN_NOTICE, sdkp, 3163 "Write cache: %s, read cache: %s, %s\n", 3164 sdkp->WCE ? "enabled" : "disabled", 3165 sdkp->RCD ? "disabled" : "enabled", 3166 sdkp->DPOFUA ? "supports DPO and FUA" 3167 : "doesn't support DPO or FUA"); 3168 3169 return; 3170 } 3171 3172 bad_sense: 3173 if (res == -EIO && scsi_sense_valid(&sshdr) && 3174 sshdr.sense_key == ILLEGAL_REQUEST && 3175 sshdr.asc == 0x24 && sshdr.ascq == 0x0) 3176 /* Invalid field in CDB */ 3177 sd_first_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n"); 3178 else 3179 sd_first_printk(KERN_ERR, sdkp, 3180 "Asking for cache data failed\n"); 3181 3182 defaults: 3183 if (sdp->wce_default_on) { 3184 sd_first_printk(KERN_NOTICE, sdkp, 3185 "Assuming drive cache: write back\n"); 3186 sdkp->WCE = 1; 3187 } else { 3188 sd_first_printk(KERN_WARNING, sdkp, 3189 "Assuming drive cache: write through\n"); 3190 sdkp->WCE = 0; 3191 } 3192 sdkp->RCD = 0; 3193 sdkp->DPOFUA = 0; 3194 } 3195 3196 static bool sd_is_perm_stream(struct scsi_disk *sdkp, unsigned int stream_id) 3197 { 3198 u8 cdb[16] = { SERVICE_ACTION_IN_16, SAI_GET_STREAM_STATUS }; 3199 struct { 3200 struct scsi_stream_status_header h; 3201 struct scsi_stream_status s; 3202 } buf; 3203 struct scsi_device *sdev = sdkp->device; 3204 struct scsi_sense_hdr sshdr; 3205 const struct scsi_exec_args exec_args = { 3206 .sshdr = &sshdr, 3207 }; 3208 int res; 3209 3210 put_unaligned_be16(stream_id, &cdb[4]); 3211 put_unaligned_be32(sizeof(buf), &cdb[10]); 3212 3213 res = scsi_execute_cmd(sdev, cdb, REQ_OP_DRV_IN, &buf, sizeof(buf), 3214 SD_TIMEOUT, sdkp->max_retries, &exec_args); 3215 if (res < 0) 3216 return false; 3217 if (scsi_status_is_check_condition(res) && scsi_sense_valid(&sshdr)) 3218 sd_print_sense_hdr(sdkp, &sshdr); 3219 if (res) 3220 return false; 3221 if (get_unaligned_be32(&buf.h.len) < sizeof(struct scsi_stream_status)) 3222 return false; 3223 return buf.h.stream_status[0].perm; 3224 } 3225 3226 static void sd_read_io_hints(struct scsi_disk *sdkp, unsigned char *buffer) 3227 { 3228 struct scsi_device *sdp = sdkp->device; 3229 const struct scsi_io_group_descriptor *desc, *start, *end; 3230 u16 permanent_stream_count_old; 3231 struct scsi_sense_hdr sshdr; 3232 struct scsi_mode_data data; 3233 int res; 3234 3235 if (sdp->sdev_bflags & BLIST_SKIP_IO_HINTS) 3236 return; 3237 3238 res = scsi_mode_sense(sdp, /*dbd=*/0x8, /*modepage=*/0x0a, 3239 /*subpage=*/0x05, buffer, SD_BUF_SIZE, SD_TIMEOUT, 3240 sdkp->max_retries, &data, &sshdr); 3241 if (res < 0) 3242 return; 3243 start = (void *)buffer + data.header_length + 16; 3244 end = (void *)buffer + ALIGN_DOWN(data.header_length + data.length, 3245 sizeof(*end)); 3246 /* 3247 * From "SBC-5 Constrained Streams with Data Lifetimes": Device severs 3248 * should assign the lowest numbered stream identifiers to permanent 3249 * streams. 3250 */ 3251 for (desc = start; desc < end; desc++) 3252 if (!desc->st_enble || !sd_is_perm_stream(sdkp, desc - start)) 3253 break; 3254 permanent_stream_count_old = sdkp->permanent_stream_count; 3255 sdkp->permanent_stream_count = desc - start; 3256 if (sdkp->rscs && sdkp->permanent_stream_count < 2) 3257 sd_printk(KERN_INFO, sdkp, 3258 "Unexpected: RSCS has been set and the permanent stream count is %u\n", 3259 sdkp->permanent_stream_count); 3260 else if (sdkp->permanent_stream_count != permanent_stream_count_old) 3261 sd_printk(KERN_INFO, sdkp, "permanent stream count = %d\n", 3262 sdkp->permanent_stream_count); 3263 } 3264 3265 /* 3266 * The ATO bit indicates whether the DIF application tag is available 3267 * for use by the operating system. 3268 */ 3269 static void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer) 3270 { 3271 int res, offset; 3272 struct scsi_device *sdp = sdkp->device; 3273 struct scsi_mode_data data; 3274 struct scsi_sense_hdr sshdr; 3275 3276 if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC) 3277 return; 3278 3279 if (sdkp->protection_type == 0) 3280 return; 3281 3282 res = scsi_mode_sense(sdp, 1, 0x0a, 0, buffer, 36, SD_TIMEOUT, 3283 sdkp->max_retries, &data, &sshdr); 3284 3285 if (res < 0 || !data.header_length || 3286 data.length < 6) { 3287 sd_first_printk(KERN_WARNING, sdkp, 3288 "getting Control mode page failed, assume no ATO\n"); 3289 3290 if (res == -EIO && scsi_sense_valid(&sshdr)) 3291 sd_print_sense_hdr(sdkp, &sshdr); 3292 3293 return; 3294 } 3295 3296 offset = data.header_length + data.block_descriptor_length; 3297 3298 if ((buffer[offset] & 0x3f) != 0x0a) { 3299 sd_first_printk(KERN_ERR, sdkp, "ATO Got wrong page\n"); 3300 return; 3301 } 3302 3303 if ((buffer[offset + 5] & 0x80) == 0) 3304 return; 3305 3306 sdkp->ATO = 1; 3307 3308 return; 3309 } 3310 3311 static unsigned int sd_discard_mode(struct scsi_disk *sdkp) 3312 { 3313 if (!sdkp->lbpvpd) { 3314 /* LBP VPD page not provided */ 3315 if (sdkp->max_unmap_blocks) 3316 return SD_LBP_UNMAP; 3317 return SD_LBP_WS16; 3318 } 3319 3320 /* LBP VPD page tells us what to use */ 3321 if (sdkp->lbpu && sdkp->max_unmap_blocks) 3322 return SD_LBP_UNMAP; 3323 if (sdkp->lbpws) 3324 return SD_LBP_WS16; 3325 if (sdkp->lbpws10) 3326 return SD_LBP_WS10; 3327 return SD_LBP_DISABLE; 3328 } 3329 3330 /* 3331 * Query disk device for preferred I/O sizes. 3332 */ 3333 static void sd_read_block_limits(struct scsi_disk *sdkp, 3334 struct queue_limits *lim) 3335 { 3336 struct scsi_vpd *vpd; 3337 3338 rcu_read_lock(); 3339 3340 vpd = rcu_dereference(sdkp->device->vpd_pgb0); 3341 if (!vpd || vpd->len < 16) 3342 goto out; 3343 3344 sdkp->min_xfer_blocks = get_unaligned_be16(&vpd->data[6]); 3345 sdkp->max_xfer_blocks = get_unaligned_be32(&vpd->data[8]); 3346 sdkp->opt_xfer_blocks = get_unaligned_be32(&vpd->data[12]); 3347 3348 if (vpd->len >= 64) { 3349 unsigned int lba_count, desc_count; 3350 3351 sdkp->max_ws_blocks = (u32)get_unaligned_be64(&vpd->data[36]); 3352 3353 if (!sdkp->lbpme) 3354 goto config_atomic; 3355 3356 lba_count = get_unaligned_be32(&vpd->data[20]); 3357 desc_count = get_unaligned_be32(&vpd->data[24]); 3358 3359 if (lba_count && desc_count) 3360 sdkp->max_unmap_blocks = lba_count; 3361 3362 sdkp->unmap_granularity = get_unaligned_be32(&vpd->data[28]); 3363 3364 if (vpd->data[32] & 0x80) 3365 sdkp->unmap_alignment = 3366 get_unaligned_be32(&vpd->data[32]) & ~(1 << 31); 3367 3368 sd_config_discard(sdkp, lim, sd_discard_mode(sdkp)); 3369 3370 config_atomic: 3371 sdkp->max_atomic = get_unaligned_be32(&vpd->data[44]); 3372 sdkp->atomic_alignment = get_unaligned_be32(&vpd->data[48]); 3373 sdkp->atomic_granularity = get_unaligned_be32(&vpd->data[52]); 3374 sdkp->max_atomic_with_boundary = get_unaligned_be32(&vpd->data[56]); 3375 sdkp->max_atomic_boundary = get_unaligned_be32(&vpd->data[60]); 3376 3377 sd_config_atomic(sdkp, lim); 3378 } 3379 3380 out: 3381 rcu_read_unlock(); 3382 } 3383 3384 /* Parse the Block Limits Extension VPD page (0xb7) */ 3385 static void sd_read_block_limits_ext(struct scsi_disk *sdkp) 3386 { 3387 struct scsi_vpd *vpd; 3388 3389 rcu_read_lock(); 3390 vpd = rcu_dereference(sdkp->device->vpd_pgb7); 3391 if (vpd && vpd->len >= 2) 3392 sdkp->rscs = vpd->data[5] & 1; 3393 rcu_read_unlock(); 3394 } 3395 3396 /* Query block device characteristics */ 3397 static void sd_read_block_characteristics(struct scsi_disk *sdkp, 3398 struct queue_limits *lim) 3399 { 3400 struct scsi_vpd *vpd; 3401 u16 rot; 3402 3403 rcu_read_lock(); 3404 vpd = rcu_dereference(sdkp->device->vpd_pgb1); 3405 3406 if (!vpd || vpd->len < 8) { 3407 rcu_read_unlock(); 3408 return; 3409 } 3410 3411 rot = get_unaligned_be16(&vpd->data[4]); 3412 sdkp->zoned = (vpd->data[8] >> 4) & 3; 3413 rcu_read_unlock(); 3414 3415 if (rot == 1) 3416 lim->features &= ~(BLK_FEAT_ROTATIONAL | BLK_FEAT_ADD_RANDOM); 3417 3418 if (!sdkp->first_scan) 3419 return; 3420 3421 if (sdkp->device->type == TYPE_ZBC) 3422 sd_printk(KERN_NOTICE, sdkp, "Host-managed zoned block device\n"); 3423 else if (sdkp->zoned == 1) 3424 sd_printk(KERN_NOTICE, sdkp, "Host-aware SMR disk used as regular disk\n"); 3425 else if (sdkp->zoned == 2) 3426 sd_printk(KERN_NOTICE, sdkp, "Drive-managed SMR disk\n"); 3427 } 3428 3429 /** 3430 * sd_read_block_provisioning - Query provisioning VPD page 3431 * @sdkp: disk to query 3432 */ 3433 static void sd_read_block_provisioning(struct scsi_disk *sdkp) 3434 { 3435 struct scsi_vpd *vpd; 3436 3437 if (sdkp->lbpme == 0) 3438 return; 3439 3440 rcu_read_lock(); 3441 vpd = rcu_dereference(sdkp->device->vpd_pgb2); 3442 3443 if (!vpd || vpd->len < 8) { 3444 rcu_read_unlock(); 3445 return; 3446 } 3447 3448 sdkp->lbpvpd = 1; 3449 sdkp->lbpu = (vpd->data[5] >> 7) & 1; /* UNMAP */ 3450 sdkp->lbpws = (vpd->data[5] >> 6) & 1; /* WRITE SAME(16) w/ UNMAP */ 3451 sdkp->lbpws10 = (vpd->data[5] >> 5) & 1; /* WRITE SAME(10) w/ UNMAP */ 3452 rcu_read_unlock(); 3453 } 3454 3455 static void sd_read_write_same(struct scsi_disk *sdkp, unsigned char *buffer) 3456 { 3457 struct scsi_device *sdev = sdkp->device; 3458 3459 if (sdev->host->no_write_same) { 3460 sdev->no_write_same = 1; 3461 3462 return; 3463 } 3464 3465 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, INQUIRY, 0) < 0) { 3466 struct scsi_vpd *vpd; 3467 3468 sdev->no_report_opcodes = 1; 3469 3470 /* Disable WRITE SAME if REPORT SUPPORTED OPERATION 3471 * CODES is unsupported and the device has an ATA 3472 * Information VPD page (SAT). 3473 */ 3474 rcu_read_lock(); 3475 vpd = rcu_dereference(sdev->vpd_pg89); 3476 if (vpd) 3477 sdev->no_write_same = 1; 3478 rcu_read_unlock(); 3479 } 3480 3481 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME_16, 0) == 1) 3482 sdkp->ws16 = 1; 3483 3484 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME, 0) == 1) 3485 sdkp->ws10 = 1; 3486 } 3487 3488 static void sd_read_security(struct scsi_disk *sdkp, unsigned char *buffer) 3489 { 3490 struct scsi_device *sdev = sdkp->device; 3491 3492 if (!sdev->security_supported) 3493 return; 3494 3495 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, 3496 SECURITY_PROTOCOL_IN, 0) == 1 && 3497 scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, 3498 SECURITY_PROTOCOL_OUT, 0) == 1) 3499 sdkp->security = 1; 3500 } 3501 3502 static inline sector_t sd64_to_sectors(struct scsi_disk *sdkp, u8 *buf) 3503 { 3504 return logical_to_sectors(sdkp->device, get_unaligned_be64(buf)); 3505 } 3506 3507 /** 3508 * sd_read_cpr - Query concurrent positioning ranges 3509 * @sdkp: disk to query 3510 */ 3511 static void sd_read_cpr(struct scsi_disk *sdkp) 3512 { 3513 struct blk_independent_access_ranges *iars = NULL; 3514 unsigned char *buffer = NULL; 3515 unsigned int nr_cpr = 0; 3516 int i, vpd_len, buf_len = SD_BUF_SIZE; 3517 u8 *desc; 3518 3519 /* 3520 * We need to have the capacity set first for the block layer to be 3521 * able to check the ranges. 3522 */ 3523 if (sdkp->first_scan) 3524 return; 3525 3526 if (!sdkp->capacity) 3527 goto out; 3528 3529 /* 3530 * Concurrent Positioning Ranges VPD: there can be at most 256 ranges, 3531 * leading to a maximum page size of 64 + 256*32 bytes. 3532 */ 3533 buf_len = 64 + 256*32; 3534 buffer = kmalloc(buf_len, GFP_KERNEL); 3535 if (!buffer || scsi_get_vpd_page(sdkp->device, 0xb9, buffer, buf_len)) 3536 goto out; 3537 3538 /* We must have at least a 64B header and one 32B range descriptor */ 3539 vpd_len = get_unaligned_be16(&buffer[2]) + 4; 3540 if (vpd_len > buf_len || vpd_len < 64 + 32 || (vpd_len & 31)) { 3541 sd_printk(KERN_ERR, sdkp, 3542 "Invalid Concurrent Positioning Ranges VPD page\n"); 3543 goto out; 3544 } 3545 3546 nr_cpr = (vpd_len - 64) / 32; 3547 if (nr_cpr == 1) { 3548 nr_cpr = 0; 3549 goto out; 3550 } 3551 3552 iars = disk_alloc_independent_access_ranges(sdkp->disk, nr_cpr); 3553 if (!iars) { 3554 nr_cpr = 0; 3555 goto out; 3556 } 3557 3558 desc = &buffer[64]; 3559 for (i = 0; i < nr_cpr; i++, desc += 32) { 3560 if (desc[0] != i) { 3561 sd_printk(KERN_ERR, sdkp, 3562 "Invalid Concurrent Positioning Range number\n"); 3563 nr_cpr = 0; 3564 break; 3565 } 3566 3567 iars->ia_range[i].sector = sd64_to_sectors(sdkp, desc + 8); 3568 iars->ia_range[i].nr_sectors = sd64_to_sectors(sdkp, desc + 16); 3569 } 3570 3571 out: 3572 disk_set_independent_access_ranges(sdkp->disk, iars); 3573 if (nr_cpr && sdkp->nr_actuators != nr_cpr) { 3574 sd_printk(KERN_NOTICE, sdkp, 3575 "%u concurrent positioning ranges\n", nr_cpr); 3576 sdkp->nr_actuators = nr_cpr; 3577 } 3578 3579 kfree(buffer); 3580 } 3581 3582 static bool sd_validate_min_xfer_size(struct scsi_disk *sdkp) 3583 { 3584 struct scsi_device *sdp = sdkp->device; 3585 unsigned int min_xfer_bytes = 3586 logical_to_bytes(sdp, sdkp->min_xfer_blocks); 3587 3588 if (sdkp->min_xfer_blocks == 0) 3589 return false; 3590 3591 if (min_xfer_bytes & (sdkp->physical_block_size - 1)) { 3592 sd_first_printk(KERN_WARNING, sdkp, 3593 "Preferred minimum I/O size %u bytes not a " \ 3594 "multiple of physical block size (%u bytes)\n", 3595 min_xfer_bytes, sdkp->physical_block_size); 3596 sdkp->min_xfer_blocks = 0; 3597 return false; 3598 } 3599 3600 sd_first_printk(KERN_INFO, sdkp, "Preferred minimum I/O size %u bytes\n", 3601 min_xfer_bytes); 3602 return true; 3603 } 3604 3605 /* 3606 * Determine the device's preferred I/O size for reads and writes 3607 * unless the reported value is unreasonably small, large, not a 3608 * multiple of the physical block size, or simply garbage. 3609 */ 3610 static bool sd_validate_opt_xfer_size(struct scsi_disk *sdkp, 3611 unsigned int dev_max) 3612 { 3613 struct scsi_device *sdp = sdkp->device; 3614 unsigned int opt_xfer_bytes = 3615 logical_to_bytes(sdp, sdkp->opt_xfer_blocks); 3616 unsigned int min_xfer_bytes = 3617 logical_to_bytes(sdp, sdkp->min_xfer_blocks); 3618 3619 if (sdkp->opt_xfer_blocks == 0) 3620 return false; 3621 3622 if (sdkp->opt_xfer_blocks > dev_max) { 3623 sd_first_printk(KERN_WARNING, sdkp, 3624 "Optimal transfer size %u logical blocks " \ 3625 "> dev_max (%u logical blocks)\n", 3626 sdkp->opt_xfer_blocks, dev_max); 3627 return false; 3628 } 3629 3630 if (sdkp->opt_xfer_blocks > SD_DEF_XFER_BLOCKS) { 3631 sd_first_printk(KERN_WARNING, sdkp, 3632 "Optimal transfer size %u logical blocks " \ 3633 "> sd driver limit (%u logical blocks)\n", 3634 sdkp->opt_xfer_blocks, SD_DEF_XFER_BLOCKS); 3635 return false; 3636 } 3637 3638 if (opt_xfer_bytes < PAGE_SIZE) { 3639 sd_first_printk(KERN_WARNING, sdkp, 3640 "Optimal transfer size %u bytes < " \ 3641 "PAGE_SIZE (%u bytes)\n", 3642 opt_xfer_bytes, (unsigned int)PAGE_SIZE); 3643 return false; 3644 } 3645 3646 if (min_xfer_bytes && opt_xfer_bytes % min_xfer_bytes) { 3647 sd_first_printk(KERN_WARNING, sdkp, 3648 "Optimal transfer size %u bytes not a " \ 3649 "multiple of preferred minimum block " \ 3650 "size (%u bytes)\n", 3651 opt_xfer_bytes, min_xfer_bytes); 3652 return false; 3653 } 3654 3655 if (opt_xfer_bytes & (sdkp->physical_block_size - 1)) { 3656 sd_first_printk(KERN_WARNING, sdkp, 3657 "Optimal transfer size %u bytes not a " \ 3658 "multiple of physical block size (%u bytes)\n", 3659 opt_xfer_bytes, sdkp->physical_block_size); 3660 return false; 3661 } 3662 3663 sd_first_printk(KERN_INFO, sdkp, "Optimal transfer size %u bytes\n", 3664 opt_xfer_bytes); 3665 return true; 3666 } 3667 3668 static void sd_read_block_zero(struct scsi_disk *sdkp) 3669 { 3670 struct scsi_device *sdev = sdkp->device; 3671 unsigned int buf_len = sdev->sector_size; 3672 u8 *buffer, cmd[16] = { }; 3673 3674 buffer = kmalloc(buf_len, GFP_KERNEL); 3675 if (!buffer) 3676 return; 3677 3678 if (sdev->use_16_for_rw) { 3679 cmd[0] = READ_16; 3680 put_unaligned_be64(0, &cmd[2]); /* Logical block address 0 */ 3681 put_unaligned_be32(1, &cmd[10]);/* Transfer 1 logical block */ 3682 } else { 3683 cmd[0] = READ_10; 3684 put_unaligned_be32(0, &cmd[2]); /* Logical block address 0 */ 3685 put_unaligned_be16(1, &cmd[7]); /* Transfer 1 logical block */ 3686 } 3687 3688 scsi_execute_cmd(sdkp->device, cmd, REQ_OP_DRV_IN, buffer, buf_len, 3689 SD_TIMEOUT, sdkp->max_retries, NULL); 3690 kfree(buffer); 3691 } 3692 3693 /** 3694 * sd_revalidate_disk - called the first time a new disk is seen, 3695 * performs disk spin up, read_capacity, etc. 3696 * @disk: struct gendisk we care about 3697 **/ 3698 static int sd_revalidate_disk(struct gendisk *disk) 3699 { 3700 struct scsi_disk *sdkp = scsi_disk(disk); 3701 struct scsi_device *sdp = sdkp->device; 3702 sector_t old_capacity = sdkp->capacity; 3703 struct queue_limits lim; 3704 unsigned char *buffer; 3705 unsigned int dev_max; 3706 int err; 3707 3708 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, 3709 "sd_revalidate_disk\n")); 3710 3711 /* 3712 * If the device is offline, don't try and read capacity or any 3713 * of the other niceties. 3714 */ 3715 if (!scsi_device_online(sdp)) 3716 goto out; 3717 3718 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL); 3719 if (!buffer) { 3720 sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory " 3721 "allocation failure.\n"); 3722 goto out; 3723 } 3724 3725 sd_spinup_disk(sdkp); 3726 3727 lim = queue_limits_start_update(sdkp->disk->queue); 3728 3729 /* 3730 * Without media there is no reason to ask; moreover, some devices 3731 * react badly if we do. 3732 */ 3733 if (sdkp->media_present) { 3734 sd_read_capacity(sdkp, &lim, buffer); 3735 /* 3736 * Some USB/UAS devices return generic values for mode pages 3737 * until the media has been accessed. Trigger a READ operation 3738 * to force the device to populate mode pages. 3739 */ 3740 if (sdp->read_before_ms) 3741 sd_read_block_zero(sdkp); 3742 /* 3743 * set the default to rotational. All non-rotational devices 3744 * support the block characteristics VPD page, which will 3745 * cause this to be updated correctly and any device which 3746 * doesn't support it should be treated as rotational. 3747 */ 3748 lim.features |= (BLK_FEAT_ROTATIONAL | BLK_FEAT_ADD_RANDOM); 3749 3750 if (scsi_device_supports_vpd(sdp)) { 3751 sd_read_block_provisioning(sdkp); 3752 sd_read_block_limits(sdkp, &lim); 3753 sd_read_block_limits_ext(sdkp); 3754 sd_read_block_characteristics(sdkp, &lim); 3755 sd_zbc_read_zones(sdkp, &lim, buffer); 3756 sd_read_cpr(sdkp); 3757 } 3758 3759 sd_print_capacity(sdkp, old_capacity); 3760 3761 sd_read_write_protect_flag(sdkp, buffer); 3762 sd_read_cache_type(sdkp, buffer); 3763 sd_read_io_hints(sdkp, buffer); 3764 sd_read_app_tag_own(sdkp, buffer); 3765 sd_read_write_same(sdkp, buffer); 3766 sd_read_security(sdkp, buffer); 3767 sd_config_protection(sdkp, &lim); 3768 } 3769 3770 /* 3771 * We now have all cache related info, determine how we deal 3772 * with flush requests. 3773 */ 3774 sd_set_flush_flag(sdkp, &lim); 3775 3776 /* Initial block count limit based on CDB TRANSFER LENGTH field size. */ 3777 dev_max = sdp->use_16_for_rw ? SD_MAX_XFER_BLOCKS : SD_DEF_XFER_BLOCKS; 3778 3779 /* Some devices report a maximum block count for READ/WRITE requests. */ 3780 dev_max = min_not_zero(dev_max, sdkp->max_xfer_blocks); 3781 lim.max_dev_sectors = logical_to_sectors(sdp, dev_max); 3782 3783 if (sd_validate_min_xfer_size(sdkp)) 3784 lim.io_min = logical_to_bytes(sdp, sdkp->min_xfer_blocks); 3785 else 3786 lim.io_min = 0; 3787 3788 /* 3789 * Limit default to SCSI host optimal sector limit if set. There may be 3790 * an impact on performance for when the size of a request exceeds this 3791 * host limit. 3792 */ 3793 lim.io_opt = sdp->host->opt_sectors << SECTOR_SHIFT; 3794 if (sd_validate_opt_xfer_size(sdkp, dev_max)) { 3795 lim.io_opt = min_not_zero(lim.io_opt, 3796 logical_to_bytes(sdp, sdkp->opt_xfer_blocks)); 3797 } 3798 3799 sdkp->first_scan = 0; 3800 3801 set_capacity_and_notify(disk, logical_to_sectors(sdp, sdkp->capacity)); 3802 sd_config_write_same(sdkp, &lim); 3803 kfree(buffer); 3804 3805 blk_mq_freeze_queue(sdkp->disk->queue); 3806 err = queue_limits_commit_update(sdkp->disk->queue, &lim); 3807 blk_mq_unfreeze_queue(sdkp->disk->queue); 3808 if (err) 3809 return err; 3810 3811 /* 3812 * For a zoned drive, revalidating the zones can be done only once 3813 * the gendisk capacity is set. So if this fails, set back the gendisk 3814 * capacity to 0. 3815 */ 3816 if (sd_zbc_revalidate_zones(sdkp)) 3817 set_capacity_and_notify(disk, 0); 3818 3819 out: 3820 return 0; 3821 } 3822 3823 /** 3824 * sd_unlock_native_capacity - unlock native capacity 3825 * @disk: struct gendisk to set capacity for 3826 * 3827 * Block layer calls this function if it detects that partitions 3828 * on @disk reach beyond the end of the device. If the SCSI host 3829 * implements ->unlock_native_capacity() method, it's invoked to 3830 * give it a chance to adjust the device capacity. 3831 * 3832 * CONTEXT: 3833 * Defined by block layer. Might sleep. 3834 */ 3835 static void sd_unlock_native_capacity(struct gendisk *disk) 3836 { 3837 struct scsi_device *sdev = scsi_disk(disk)->device; 3838 3839 if (sdev->host->hostt->unlock_native_capacity) 3840 sdev->host->hostt->unlock_native_capacity(sdev); 3841 } 3842 3843 /** 3844 * sd_format_disk_name - format disk name 3845 * @prefix: name prefix - ie. "sd" for SCSI disks 3846 * @index: index of the disk to format name for 3847 * @buf: output buffer 3848 * @buflen: length of the output buffer 3849 * 3850 * SCSI disk names starts at sda. The 26th device is sdz and the 3851 * 27th is sdaa. The last one for two lettered suffix is sdzz 3852 * which is followed by sdaaa. 3853 * 3854 * This is basically 26 base counting with one extra 'nil' entry 3855 * at the beginning from the second digit on and can be 3856 * determined using similar method as 26 base conversion with the 3857 * index shifted -1 after each digit is computed. 3858 * 3859 * CONTEXT: 3860 * Don't care. 3861 * 3862 * RETURNS: 3863 * 0 on success, -errno on failure. 3864 */ 3865 static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen) 3866 { 3867 const int base = 'z' - 'a' + 1; 3868 char *begin = buf + strlen(prefix); 3869 char *end = buf + buflen; 3870 char *p; 3871 int unit; 3872 3873 p = end - 1; 3874 *p = '\0'; 3875 unit = base; 3876 do { 3877 if (p == begin) 3878 return -EINVAL; 3879 *--p = 'a' + (index % unit); 3880 index = (index / unit) - 1; 3881 } while (index >= 0); 3882 3883 memmove(begin, p, end - p); 3884 memcpy(buf, prefix, strlen(prefix)); 3885 3886 return 0; 3887 } 3888 3889 /** 3890 * sd_probe - called during driver initialization and whenever a 3891 * new scsi device is attached to the system. It is called once 3892 * for each scsi device (not just disks) present. 3893 * @dev: pointer to device object 3894 * 3895 * Returns 0 if successful (or not interested in this scsi device 3896 * (e.g. scanner)); 1 when there is an error. 3897 * 3898 * Note: this function is invoked from the scsi mid-level. 3899 * This function sets up the mapping between a given 3900 * <host,channel,id,lun> (found in sdp) and new device name 3901 * (e.g. /dev/sda). More precisely it is the block device major 3902 * and minor number that is chosen here. 3903 * 3904 * Assume sd_probe is not re-entrant (for time being) 3905 * Also think about sd_probe() and sd_remove() running coincidentally. 3906 **/ 3907 static int sd_probe(struct device *dev) 3908 { 3909 struct scsi_device *sdp = to_scsi_device(dev); 3910 struct scsi_disk *sdkp; 3911 struct gendisk *gd; 3912 int index; 3913 int error; 3914 3915 scsi_autopm_get_device(sdp); 3916 error = -ENODEV; 3917 if (sdp->type != TYPE_DISK && 3918 sdp->type != TYPE_ZBC && 3919 sdp->type != TYPE_MOD && 3920 sdp->type != TYPE_RBC) 3921 goto out; 3922 3923 if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED) && sdp->type == TYPE_ZBC) { 3924 sdev_printk(KERN_WARNING, sdp, 3925 "Unsupported ZBC host-managed device.\n"); 3926 goto out; 3927 } 3928 3929 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp, 3930 "sd_probe\n")); 3931 3932 error = -ENOMEM; 3933 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL); 3934 if (!sdkp) 3935 goto out; 3936 3937 gd = blk_mq_alloc_disk_for_queue(sdp->request_queue, 3938 &sd_bio_compl_lkclass); 3939 if (!gd) 3940 goto out_free; 3941 3942 index = ida_alloc(&sd_index_ida, GFP_KERNEL); 3943 if (index < 0) { 3944 sdev_printk(KERN_WARNING, sdp, "sd_probe: memory exhausted.\n"); 3945 goto out_put; 3946 } 3947 3948 error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN); 3949 if (error) { 3950 sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name length exceeded.\n"); 3951 goto out_free_index; 3952 } 3953 3954 sdkp->device = sdp; 3955 sdkp->disk = gd; 3956 sdkp->index = index; 3957 sdkp->max_retries = SD_MAX_RETRIES; 3958 atomic_set(&sdkp->openers, 0); 3959 atomic_set(&sdkp->device->ioerr_cnt, 0); 3960 3961 if (!sdp->request_queue->rq_timeout) { 3962 if (sdp->type != TYPE_MOD) 3963 blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT); 3964 else 3965 blk_queue_rq_timeout(sdp->request_queue, 3966 SD_MOD_TIMEOUT); 3967 } 3968 3969 device_initialize(&sdkp->disk_dev); 3970 sdkp->disk_dev.parent = get_device(dev); 3971 sdkp->disk_dev.class = &sd_disk_class; 3972 dev_set_name(&sdkp->disk_dev, "%s", dev_name(dev)); 3973 3974 error = device_add(&sdkp->disk_dev); 3975 if (error) { 3976 put_device(&sdkp->disk_dev); 3977 goto out; 3978 } 3979 3980 dev_set_drvdata(dev, sdkp); 3981 3982 gd->major = sd_major((index & 0xf0) >> 4); 3983 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00); 3984 gd->minors = SD_MINORS; 3985 3986 gd->fops = &sd_fops; 3987 gd->private_data = sdkp; 3988 3989 /* defaults, until the device tells us otherwise */ 3990 sdp->sector_size = 512; 3991 sdkp->capacity = 0; 3992 sdkp->media_present = 1; 3993 sdkp->write_prot = 0; 3994 sdkp->cache_override = 0; 3995 sdkp->WCE = 0; 3996 sdkp->RCD = 0; 3997 sdkp->ATO = 0; 3998 sdkp->first_scan = 1; 3999 sdkp->max_medium_access_timeouts = SD_MAX_MEDIUM_TIMEOUTS; 4000 4001 sd_revalidate_disk(gd); 4002 4003 if (sdp->removable) { 4004 gd->flags |= GENHD_FL_REMOVABLE; 4005 gd->events |= DISK_EVENT_MEDIA_CHANGE; 4006 gd->event_flags = DISK_EVENT_FLAG_POLL | DISK_EVENT_FLAG_UEVENT; 4007 } 4008 4009 blk_pm_runtime_init(sdp->request_queue, dev); 4010 if (sdp->rpm_autosuspend) { 4011 pm_runtime_set_autosuspend_delay(dev, 4012 sdp->host->rpm_autosuspend_delay); 4013 } 4014 4015 error = device_add_disk(dev, gd, NULL); 4016 if (error) { 4017 device_unregister(&sdkp->disk_dev); 4018 put_disk(gd); 4019 goto out; 4020 } 4021 4022 if (sdkp->security) { 4023 sdkp->opal_dev = init_opal_dev(sdkp, &sd_sec_submit); 4024 if (sdkp->opal_dev) 4025 sd_printk(KERN_NOTICE, sdkp, "supports TCG Opal\n"); 4026 } 4027 4028 sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n", 4029 sdp->removable ? "removable " : ""); 4030 scsi_autopm_put_device(sdp); 4031 4032 return 0; 4033 4034 out_free_index: 4035 ida_free(&sd_index_ida, index); 4036 out_put: 4037 put_disk(gd); 4038 out_free: 4039 kfree(sdkp); 4040 out: 4041 scsi_autopm_put_device(sdp); 4042 return error; 4043 } 4044 4045 /** 4046 * sd_remove - called whenever a scsi disk (previously recognized by 4047 * sd_probe) is detached from the system. It is called (potentially 4048 * multiple times) during sd module unload. 4049 * @dev: pointer to device object 4050 * 4051 * Note: this function is invoked from the scsi mid-level. 4052 * This function potentially frees up a device name (e.g. /dev/sdc) 4053 * that could be re-used by a subsequent sd_probe(). 4054 * This function is not called when the built-in sd driver is "exit-ed". 4055 **/ 4056 static int sd_remove(struct device *dev) 4057 { 4058 struct scsi_disk *sdkp = dev_get_drvdata(dev); 4059 4060 scsi_autopm_get_device(sdkp->device); 4061 4062 device_del(&sdkp->disk_dev); 4063 del_gendisk(sdkp->disk); 4064 if (!sdkp->suspended) 4065 sd_shutdown(dev); 4066 4067 put_disk(sdkp->disk); 4068 return 0; 4069 } 4070 4071 static void scsi_disk_release(struct device *dev) 4072 { 4073 struct scsi_disk *sdkp = to_scsi_disk(dev); 4074 4075 ida_free(&sd_index_ida, sdkp->index); 4076 put_device(&sdkp->device->sdev_gendev); 4077 free_opal_dev(sdkp->opal_dev); 4078 4079 kfree(sdkp); 4080 } 4081 4082 static int sd_start_stop_device(struct scsi_disk *sdkp, int start) 4083 { 4084 unsigned char cmd[6] = { START_STOP }; /* START_VALID */ 4085 struct scsi_sense_hdr sshdr; 4086 const struct scsi_exec_args exec_args = { 4087 .sshdr = &sshdr, 4088 .req_flags = BLK_MQ_REQ_PM, 4089 }; 4090 struct scsi_device *sdp = sdkp->device; 4091 int res; 4092 4093 if (start) 4094 cmd[4] |= 1; /* START */ 4095 4096 if (sdp->start_stop_pwr_cond) 4097 cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */ 4098 4099 if (!scsi_device_online(sdp)) 4100 return -ENODEV; 4101 4102 res = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, NULL, 0, SD_TIMEOUT, 4103 sdkp->max_retries, &exec_args); 4104 if (res) { 4105 sd_print_result(sdkp, "Start/Stop Unit failed", res); 4106 if (res > 0 && scsi_sense_valid(&sshdr)) { 4107 sd_print_sense_hdr(sdkp, &sshdr); 4108 /* 0x3a is medium not present */ 4109 if (sshdr.asc == 0x3a) 4110 res = 0; 4111 } 4112 } 4113 4114 /* SCSI error codes must not go to the generic layer */ 4115 if (res) 4116 return -EIO; 4117 4118 return 0; 4119 } 4120 4121 /* 4122 * Send a SYNCHRONIZE CACHE instruction down to the device through 4123 * the normal SCSI command structure. Wait for the command to 4124 * complete. 4125 */ 4126 static void sd_shutdown(struct device *dev) 4127 { 4128 struct scsi_disk *sdkp = dev_get_drvdata(dev); 4129 4130 if (!sdkp) 4131 return; /* this can happen */ 4132 4133 if (pm_runtime_suspended(dev)) 4134 return; 4135 4136 if (sdkp->WCE && sdkp->media_present) { 4137 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n"); 4138 sd_sync_cache(sdkp); 4139 } 4140 4141 if ((system_state != SYSTEM_RESTART && 4142 sdkp->device->manage_system_start_stop) || 4143 (system_state == SYSTEM_POWER_OFF && 4144 sdkp->device->manage_shutdown)) { 4145 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n"); 4146 sd_start_stop_device(sdkp, 0); 4147 } 4148 } 4149 4150 static inline bool sd_do_start_stop(struct scsi_device *sdev, bool runtime) 4151 { 4152 return (sdev->manage_system_start_stop && !runtime) || 4153 (sdev->manage_runtime_start_stop && runtime); 4154 } 4155 4156 static int sd_suspend_common(struct device *dev, bool runtime) 4157 { 4158 struct scsi_disk *sdkp = dev_get_drvdata(dev); 4159 int ret = 0; 4160 4161 if (!sdkp) /* E.g.: runtime suspend following sd_remove() */ 4162 return 0; 4163 4164 if (sdkp->WCE && sdkp->media_present) { 4165 if (!sdkp->device->silence_suspend) 4166 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n"); 4167 ret = sd_sync_cache(sdkp); 4168 /* ignore OFFLINE device */ 4169 if (ret == -ENODEV) 4170 return 0; 4171 4172 if (ret) 4173 return ret; 4174 } 4175 4176 if (sd_do_start_stop(sdkp->device, runtime)) { 4177 if (!sdkp->device->silence_suspend) 4178 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n"); 4179 /* an error is not worth aborting a system sleep */ 4180 ret = sd_start_stop_device(sdkp, 0); 4181 if (!runtime) 4182 ret = 0; 4183 } 4184 4185 if (!ret) 4186 sdkp->suspended = true; 4187 4188 return ret; 4189 } 4190 4191 static int sd_suspend_system(struct device *dev) 4192 { 4193 if (pm_runtime_suspended(dev)) 4194 return 0; 4195 4196 return sd_suspend_common(dev, false); 4197 } 4198 4199 static int sd_suspend_runtime(struct device *dev) 4200 { 4201 return sd_suspend_common(dev, true); 4202 } 4203 4204 static int sd_resume(struct device *dev) 4205 { 4206 struct scsi_disk *sdkp = dev_get_drvdata(dev); 4207 4208 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n"); 4209 4210 if (opal_unlock_from_suspend(sdkp->opal_dev)) { 4211 sd_printk(KERN_NOTICE, sdkp, "OPAL unlock failed\n"); 4212 return -EIO; 4213 } 4214 4215 return 0; 4216 } 4217 4218 static int sd_resume_common(struct device *dev, bool runtime) 4219 { 4220 struct scsi_disk *sdkp = dev_get_drvdata(dev); 4221 int ret; 4222 4223 if (!sdkp) /* E.g.: runtime resume at the start of sd_probe() */ 4224 return 0; 4225 4226 if (!sd_do_start_stop(sdkp->device, runtime)) { 4227 sdkp->suspended = false; 4228 return 0; 4229 } 4230 4231 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n"); 4232 ret = sd_start_stop_device(sdkp, 1); 4233 if (!ret) { 4234 sd_resume(dev); 4235 sdkp->suspended = false; 4236 } 4237 4238 return ret; 4239 } 4240 4241 static int sd_resume_system(struct device *dev) 4242 { 4243 if (pm_runtime_suspended(dev)) { 4244 struct scsi_disk *sdkp = dev_get_drvdata(dev); 4245 struct scsi_device *sdp = sdkp ? sdkp->device : NULL; 4246 4247 if (sdp && sdp->force_runtime_start_on_system_start) 4248 pm_request_resume(dev); 4249 4250 return 0; 4251 } 4252 4253 return sd_resume_common(dev, false); 4254 } 4255 4256 static int sd_resume_runtime(struct device *dev) 4257 { 4258 struct scsi_disk *sdkp = dev_get_drvdata(dev); 4259 struct scsi_device *sdp; 4260 4261 if (!sdkp) /* E.g.: runtime resume at the start of sd_probe() */ 4262 return 0; 4263 4264 sdp = sdkp->device; 4265 4266 if (sdp->ignore_media_change) { 4267 /* clear the device's sense data */ 4268 static const u8 cmd[10] = { REQUEST_SENSE }; 4269 const struct scsi_exec_args exec_args = { 4270 .req_flags = BLK_MQ_REQ_PM, 4271 }; 4272 4273 if (scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, NULL, 0, 4274 sdp->request_queue->rq_timeout, 1, 4275 &exec_args)) 4276 sd_printk(KERN_NOTICE, sdkp, 4277 "Failed to clear sense data\n"); 4278 } 4279 4280 return sd_resume_common(dev, true); 4281 } 4282 4283 static const struct dev_pm_ops sd_pm_ops = { 4284 .suspend = sd_suspend_system, 4285 .resume = sd_resume_system, 4286 .poweroff = sd_suspend_system, 4287 .restore = sd_resume_system, 4288 .runtime_suspend = sd_suspend_runtime, 4289 .runtime_resume = sd_resume_runtime, 4290 }; 4291 4292 static struct scsi_driver sd_template = { 4293 .gendrv = { 4294 .name = "sd", 4295 .probe = sd_probe, 4296 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 4297 .remove = sd_remove, 4298 .shutdown = sd_shutdown, 4299 .pm = &sd_pm_ops, 4300 }, 4301 .rescan = sd_rescan, 4302 .resume = sd_resume, 4303 .init_command = sd_init_command, 4304 .uninit_command = sd_uninit_command, 4305 .done = sd_done, 4306 .eh_action = sd_eh_action, 4307 .eh_reset = sd_eh_reset, 4308 }; 4309 4310 /** 4311 * init_sd - entry point for this driver (both when built in or when 4312 * a module). 4313 * 4314 * Note: this function registers this driver with the scsi mid-level. 4315 **/ 4316 static int __init init_sd(void) 4317 { 4318 int majors = 0, i, err; 4319 4320 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n")); 4321 4322 for (i = 0; i < SD_MAJORS; i++) { 4323 if (__register_blkdev(sd_major(i), "sd", sd_default_probe)) 4324 continue; 4325 majors++; 4326 } 4327 4328 if (!majors) 4329 return -ENODEV; 4330 4331 err = class_register(&sd_disk_class); 4332 if (err) 4333 goto err_out; 4334 4335 sd_page_pool = mempool_create_page_pool(SD_MEMPOOL_SIZE, 0); 4336 if (!sd_page_pool) { 4337 printk(KERN_ERR "sd: can't init discard page pool\n"); 4338 err = -ENOMEM; 4339 goto err_out_class; 4340 } 4341 4342 err = scsi_register_driver(&sd_template.gendrv); 4343 if (err) 4344 goto err_out_driver; 4345 4346 return 0; 4347 4348 err_out_driver: 4349 mempool_destroy(sd_page_pool); 4350 err_out_class: 4351 class_unregister(&sd_disk_class); 4352 err_out: 4353 for (i = 0; i < SD_MAJORS; i++) 4354 unregister_blkdev(sd_major(i), "sd"); 4355 return err; 4356 } 4357 4358 /** 4359 * exit_sd - exit point for this driver (when it is a module). 4360 * 4361 * Note: this function unregisters this driver from the scsi mid-level. 4362 **/ 4363 static void __exit exit_sd(void) 4364 { 4365 int i; 4366 4367 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n")); 4368 4369 scsi_unregister_driver(&sd_template.gendrv); 4370 mempool_destroy(sd_page_pool); 4371 4372 class_unregister(&sd_disk_class); 4373 4374 for (i = 0; i < SD_MAJORS; i++) 4375 unregister_blkdev(sd_major(i), "sd"); 4376 } 4377 4378 module_init(init_sd); 4379 module_exit(exit_sd); 4380 4381 void sd_print_sense_hdr(struct scsi_disk *sdkp, struct scsi_sense_hdr *sshdr) 4382 { 4383 scsi_print_sense_hdr(sdkp->device, 4384 sdkp->disk ? sdkp->disk->disk_name : NULL, sshdr); 4385 } 4386 4387 void sd_print_result(const struct scsi_disk *sdkp, const char *msg, int result) 4388 { 4389 const char *hb_string = scsi_hostbyte_string(result); 4390 4391 if (hb_string) 4392 sd_printk(KERN_INFO, sdkp, 4393 "%s: Result: hostbyte=%s driverbyte=%s\n", msg, 4394 hb_string ? hb_string : "invalid", 4395 "DRIVER_OK"); 4396 else 4397 sd_printk(KERN_INFO, sdkp, 4398 "%s: Result: hostbyte=0x%02x driverbyte=%s\n", 4399 msg, host_byte(result), "DRIVER_OK"); 4400 } 4401