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