35 #include <linux/module.h>
37 #include <linux/kernel.h>
42 #include <linux/errno.h>
52 #include <linux/slab.h>
54 #include <asm/uaccess.h>
55 #include <asm/unaligned.h>
57 #include <scsi/scsi.h>
95 #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
101 static void sd_config_discard(
struct scsi_disk *,
unsigned int);
102 static void sd_config_write_same(
struct scsi_disk *);
103 static int sd_revalidate_disk(
struct gendisk *);
104 static void sd_unlock_native_capacity(
struct gendisk *disk);
105 static int sd_probe(
struct device *);
106 static int sd_remove(
struct device *);
107 static void sd_shutdown(
struct device *);
109 static int sd_resume(
struct device *);
110 static void sd_rescan(
struct device *);
112 static int sd_eh_action(
struct scsi_cmnd *,
unsigned char *,
int,
int);
113 static void sd_read_capacity(
struct scsi_disk *sdkp,
unsigned char *
buffer);
114 static void scsi_disk_release(
struct device *
cdev);
116 static void sd_print_result(
struct scsi_disk *,
int);
129 static const char *sd_cache_types[] = {
130 "write through",
"none",
"write back",
131 "write back, no read (daft)"
153 for (i = 0; i <
ARRAY_SIZE(sd_cache_types); i++) {
154 len =
strlen(sd_cache_types[i]);
155 if (
strncmp(sd_cache_types[i], buf, len) == 0 &&
163 rcd = ct & 0x01 ? 1 : 0;
164 wce = ct & 0x02 ? 1 : 0;
168 len =
min_t(
size_t,
sizeof(buffer),
data.length -
data.header_length -
169 data.block_descriptor_length);
170 buffer_data = buffer +
data.header_length +
171 data.block_descriptor_length;
172 buffer_data[2] &= ~0x05;
173 buffer_data[2] |= wce << 2 | rcd;
174 sp = buffer_data[0] & 0x80 ? 1 : 0;
178 if (scsi_sense_valid(&sshdr))
179 sd_print_sense_hdr(sdkp, &sshdr);
188 const char *buf,
size_t count)
203 const char *buf,
size_t count)
224 int ct = sdkp->
RCD + 2*sdkp->
WCE;
226 return snprintf(buf, 40,
"%s\n", sd_cache_types[ct]);
267 const char *buf,
size_t count)
293 unsigned int dif, dix;
306 return snprintf(buf, 20,
"%s%u\n", dix ?
"dix" :
"dif", dif);
327 static const char *lbp_mode[] = {
347 const char *buf,
size_t count)
359 sd_config_discard(sdkp, SD_LBP_UNMAP);
361 sd_config_discard(sdkp, SD_LBP_WS16);
363 sd_config_discard(sdkp, SD_LBP_WS10);
365 sd_config_discard(sdkp, SD_LBP_ZERO);
367 sd_config_discard(sdkp, SD_LBP_DISABLE);
375 sd_show_max_medium_access_timeouts(
struct device *dev,
384 sd_store_max_medium_access_timeouts(
struct device *dev,
386 const char *buf,
size_t count)
396 return err ? err :
count;
410 const char *buf,
size_t count)
423 err = kstrtoul(buf, 10, &max);
433 sd_config_write_same(sdkp);
440 sd_store_cache_type),
443 sd_store_allow_restart),
445 sd_store_manage_start_stop),
447 sd_store_protection_type),
452 sd_store_provisioning_mode),
454 sd_show_write_same_blocks, sd_store_write_same_blocks),
456 sd_show_max_medium_access_timeouts,
457 sd_store_max_medium_access_timeouts),
461 static struct class sd_disk_class = {
464 .dev_release = scsi_disk_release,
465 .dev_attrs = sd_disk_attrs,
474 .suspend = sd_suspend,
476 .shutdown = sd_shutdown,
480 .eh_action = sd_eh_action,
497 static int sd_major(
int major_idx)
512 static struct scsi_disk *__scsi_disk_get(
struct gendisk *
disk)
516 if (disk->private_data) {
526 static struct scsi_disk *scsi_disk_get(
struct gendisk *disk)
531 sdkp = __scsi_disk_get(disk);
543 sdkp = __scsi_disk_get(sdkp->
disk);
548 static void scsi_disk_put(
struct scsi_disk *sdkp)
558 static void sd_prot_op(
struct scsi_cmnd *scmd,
unsigned int dif)
561 unsigned int dix = scsi_prot_sg_count(scmd);
566 else if (dif && !dix)
568 else if (!dif && dix)
573 else if (dif && !dix)
575 else if (!dif && dix)
579 scsi_set_prot_op(scmd, prot_op);
580 scsi_set_prot_type(scmd, dif);
583 static void sd_config_discard(
struct scsi_disk *sdkp,
unsigned int mode)
586 unsigned int logical_block_size = sdkp->
device->sector_size;
587 unsigned int max_blocks = 0;
589 q->limits.discard_zeroes_data = sdkp->
lbprz;
592 q->limits.discard_granularity =
601 q->limits.max_discard_sectors = 0;
602 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
623 q->limits.discard_zeroes_data = 1;
627 q->limits.max_discard_sectors = max_blocks * (logical_block_size >> 9);
628 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
644 unsigned int nr_bytes = blk_rq_bytes(rq);
654 memset(rq->cmd, 0, rq->cmd_len);
658 return BLKPREP_DEFER;
668 put_unaligned_be16(6 + 16, &buf[0]);
669 put_unaligned_be16(16, &buf[2]);
683 len = sdkp->
device->sector_size;
693 put_unaligned_be16(nr_sectors, &rq->cmd[7]);
695 len = sdkp->
device->sector_size;
706 rq->__data_len = nr_bytes;
709 if (ret != BLKPREP_OK) {
716 static void sd_config_write_same(
struct scsi_disk *sdkp)
719 unsigned int logical_block_size = sdkp->
device->sector_size;
720 unsigned int blocks = 0;
722 if (sdkp->
device->no_write_same) {
757 struct bio *bio = rq->bio;
759 unsigned int nr_sectors = blk_rq_sectors(rq);
760 unsigned int nr_bytes = blk_rq_bytes(rq);
763 if (sdkp->
device->no_write_same)
773 memset(rq->cmd, 0, rq->cmd_len);
775 if (sdkp->
ws16 || sector > 0xffffffff || nr_sectors > 0xffff) {
784 put_unaligned_be16(nr_sectors, &rq->cmd[7]);
788 rq->__data_len = nr_bytes;
823 struct gendisk *disk = rq->rq_disk;
827 unsigned int this_count = blk_rq_sectors(rq);
836 ret = sd_setup_discard_cmnd(sdp, rq);
839 ret = sd_setup_write_same_cmnd(sdp, rq);
842 ret = scsi_setup_flush_cmnd(sdp, rq);
844 }
else if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
847 }
else if (rq->cmd_type != REQ_TYPE_FS) {
852 if (ret != BLKPREP_OK)
862 "sd_prep_fn: block=%llu, "
864 (
unsigned long long)block,
867 if (!sdp || !scsi_device_online(sdp) ||
868 block + blk_rq_sectors(rq) > get_capacity(disk)) {
870 "Finishing %u sectors\n",
871 blk_rq_sectors(rq)));
873 "Retry with 0x%p\n", SCpnt));
894 if (block < threshold) {
896 this_count = threshold -
block;
904 (
unsigned long long)block));
918 if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
920 "Bad block number requested\n");
924 this_count = this_count >> 1;
928 if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
930 "Bad block number requested\n");
934 this_count = this_count >> 2;
938 if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
940 "Bad block number requested\n");
944 this_count = this_count >> 3;
947 if (rq_data_dir(rq) ==
WRITE) {
954 if (blk_integrity_rq(rq))
957 }
else if (rq_data_dir(rq) ==
READ) {
966 "%s %d/%u 512 byte blocks.\n",
967 (rq_data_dir(rq) ==
WRITE) ?
968 "writing" :
"reading", this_count,
969 blk_rq_sectors(rq)));
989 SCpnt->
cmnd[7] = 0x18;
991 SCpnt->
cmnd[10] = protect | ((rq->cmd_flags &
REQ_FUA) ? 0x8 : 0);
994 SCpnt->
cmnd[12] =
sizeof(
block) > 4 ? (
unsigned char) (block >> 56) & 0xff : 0;
995 SCpnt->
cmnd[13] =
sizeof(
block) > 4 ? (
unsigned char) (block >> 48) & 0xff : 0;
996 SCpnt->
cmnd[14] =
sizeof(
block) > 4 ? (
unsigned char) (block >> 40) & 0xff : 0;
997 SCpnt->
cmnd[15] =
sizeof(
block) > 4 ? (
unsigned char) (block >> 32) & 0xff : 0;
998 SCpnt->
cmnd[16] = (
unsigned char) (block >> 24) & 0xff;
999 SCpnt->
cmnd[17] = (
unsigned char) (block >> 16) & 0xff;
1000 SCpnt->
cmnd[18] = (
unsigned char) (block >> 8) & 0xff;
1001 SCpnt->
cmnd[19] = (
unsigned char) block & 0xff;
1004 SCpnt->
cmnd[20] = (
unsigned char) (block >> 24) & 0xff;
1005 SCpnt->
cmnd[21] = (
unsigned char) (block >> 16) & 0xff;
1006 SCpnt->
cmnd[22] = (
unsigned char) (block >> 8) & 0xff;
1007 SCpnt->
cmnd[23] = (
unsigned char) block & 0xff;
1010 SCpnt->
cmnd[28] = (
unsigned char) (this_count >> 24) & 0xff;
1011 SCpnt->
cmnd[29] = (
unsigned char) (this_count >> 16) & 0xff;
1012 SCpnt->
cmnd[30] = (
unsigned char) (this_count >> 8) & 0xff;
1013 SCpnt->
cmnd[31] = (
unsigned char) this_count & 0xff;
1014 }
else if (block > 0xffffffff) {
1016 SCpnt->
cmnd[1] = protect | ((rq->cmd_flags &
REQ_FUA) ? 0x8 : 0);
1017 SCpnt->
cmnd[2] =
sizeof(
block) > 4 ? (
unsigned char) (block >> 56) & 0xff : 0;
1018 SCpnt->
cmnd[3] =
sizeof(
block) > 4 ? (
unsigned char) (block >> 48) & 0xff : 0;
1019 SCpnt->
cmnd[4] =
sizeof(
block) > 4 ? (
unsigned char) (block >> 40) & 0xff : 0;
1020 SCpnt->
cmnd[5] =
sizeof(
block) > 4 ? (
unsigned char) (block >> 32) & 0xff : 0;
1021 SCpnt->
cmnd[6] = (
unsigned char) (block >> 24) & 0xff;
1022 SCpnt->
cmnd[7] = (
unsigned char) (block >> 16) & 0xff;
1023 SCpnt->
cmnd[8] = (
unsigned char) (block >> 8) & 0xff;
1024 SCpnt->
cmnd[9] = (
unsigned char) block & 0xff;
1025 SCpnt->
cmnd[10] = (
unsigned char) (this_count >> 24) & 0xff;
1026 SCpnt->
cmnd[11] = (
unsigned char) (this_count >> 16) & 0xff;
1027 SCpnt->
cmnd[12] = (
unsigned char) (this_count >> 8) & 0xff;
1028 SCpnt->
cmnd[13] = (
unsigned char) this_count & 0xff;
1029 SCpnt->
cmnd[14] = SCpnt->
cmnd[15] = 0;
1030 }
else if ((this_count > 0xff) || (block > 0x1fffff) ||
1031 scsi_device_protection(SCpnt->
device) ||
1032 SCpnt->
device->use_10_for_rw) {
1033 if (this_count > 0xffff)
1034 this_count = 0xffff;
1037 SCpnt->
cmnd[1] = protect | ((rq->cmd_flags &
REQ_FUA) ? 0x8 : 0);
1038 SCpnt->
cmnd[2] = (
unsigned char) (block >> 24) & 0xff;
1039 SCpnt->
cmnd[3] = (
unsigned char) (block >> 16) & 0xff;
1040 SCpnt->
cmnd[4] = (
unsigned char) (block >> 8) & 0xff;
1041 SCpnt->
cmnd[5] = (
unsigned char) block & 0xff;
1042 SCpnt->
cmnd[6] = SCpnt->
cmnd[9] = 0;
1043 SCpnt->
cmnd[7] = (
unsigned char) (this_count >> 8) & 0xff;
1044 SCpnt->
cmnd[8] = (
unsigned char) this_count & 0xff;
1054 "FUA write on READ/WRITE(6) drive\n");
1058 SCpnt->
cmnd[1] |= (
unsigned char) ((block >> 16) & 0x1f);
1059 SCpnt->
cmnd[2] = (
unsigned char) ((block >> 8) & 0xff);
1060 SCpnt->
cmnd[3] = (
unsigned char) block & 0xff;
1061 SCpnt->
cmnd[4] = (
unsigned char) this_count;
1067 if (host_dif || scsi_prot_sg_count(SCpnt))
1068 sd_prot_op(SCpnt, host_dif);
1116 retval = scsi_autopm_get_device(sdev);
1153 if (!scsi_device_online(sdev))
1164 scsi_autopm_put_device(sdev);
1166 scsi_disk_put(sdkp);
1183 static int sd_release(
struct gendisk *disk,
fmode_t mode)
1200 scsi_autopm_put_device(sdev);
1201 scsi_disk_put(sdkp);
1215 diskinfo[2] = sdkp->
capacity >> 11;
1218 if (host->
hostt->bios_param)
1219 host->
hostt->bios_param(sdp, bdev, sdkp->
capacity, diskinfo);
1223 geo->
heads = diskinfo[0];
1244 unsigned int cmd,
unsigned long arg)
1246 struct gendisk *disk = bdev->
bd_disk;
1253 "cmd=0x%x\n", disk->disk_name, cmd));
1266 (mode & FMODE_NDELAY) != 0);
1291 static void set_media_not_present(
struct scsi_disk *sdkp)
1294 sdkp->
device->changed = 1;
1296 if (sdkp->
device->removable) {
1305 if (!scsi_sense_valid(sshdr))
1313 if (sshdr->
asc == 0x3A) {
1314 set_media_not_present(sdkp);
1330 static unsigned int sd_check_events(
struct gendisk *disk,
unsigned int clearing)
1345 if (!scsi_device_online(sdp)) {
1346 set_media_not_present(sdkp);
1362 retval = scsi_autopm_get_device(sdp);
1369 scsi_autopm_put_device(sdp);
1374 set_media_not_present(sdkp);
1396 retval = sdp->
changed ? DISK_EVENT_MEDIA_CHANGE : 0;
1401 static int sd_sync_cache(
struct scsi_disk *sdkp)
1407 if (!scsi_device_online(sdp))
1411 for (retries = 3; retries > 0; --
retries) {
1412 unsigned char cmd[10] = { 0 };
1426 sd_print_result(sdkp, res);
1428 sd_print_sense_hdr(sdkp, &sshdr);
1436 static void sd_rescan(
struct device *dev)
1438 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1442 scsi_disk_put(sdkp);
1447 #ifdef CONFIG_COMPAT
1453 unsigned int cmd,
unsigned long arg)
1471 if (sdev->
host->hostt->compat_ioctl) {
1472 ret = sdev->
host->hostt->compat_ioctl(sdev, cmd, (
void __user *)arg);
1484 static const struct block_device_operations sd_fops = {
1487 .release = sd_release,
1489 .getgeo = sd_getgeo,
1490 #ifdef CONFIG_COMPAT
1491 .compat_ioctl = sd_compat_ioctl,
1493 .check_events = sd_check_events,
1494 .revalidate_disk = sd_revalidate_disk,
1495 .unlock_native_capacity = sd_unlock_native_capacity,
1511 static int sd_eh_action(
struct scsi_cmnd *scmd,
unsigned char *eh_cmnd,
1512 int eh_cmnd_len,
int eh_disp)
1516 if (!scsi_device_online(scmd->
device) ||
1517 !scsi_medium_access_command(scmd))
1538 "Medium access timeout failure. Offlining disk!\n");
1547 static unsigned int sd_completed_bytes(
struct scsi_cmnd *scmd)
1550 u64 end_lba = blk_rq_pos(scmd->
request) + (scsi_bufflen(scmd) / 512);
1557 unsigned int transferred = scsi_bufflen(scmd) - scsi_get_resid(scmd);
1558 unsigned int good_bytes;
1560 if (scmd->
request->cmd_type != REQ_TYPE_FS)
1569 if (scsi_bufflen(scmd) <= scmd->
device->sector_size)
1572 if (scmd->
device->sector_size < 512) {
1578 u64 factor = scmd->
device->sector_size / 512;
1579 do_div(start_lba, factor);
1586 if (bad_lba < start_lba || bad_lba >= end_lba)
1592 good_bytes = (bad_lba - start_lba) * scmd->
device->sector_size;
1593 return min(good_bytes, transferred);
1603 static int sd_done(
struct scsi_cmnd *SCpnt)
1606 unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
1611 int sense_deferred = 0;
1612 unsigned char op = SCpnt->
cmnd[0];
1613 unsigned char unmap = SCpnt->
cmnd[1] & 8;
1617 good_bytes = blk_rq_bytes(req);
1618 scsi_set_resid(SCpnt, 0);
1621 scsi_set_resid(SCpnt, blk_rq_bytes(req));
1628 sense_deferred = scsi_sense_is_deferred(&sshdr);
1630 #ifdef CONFIG_SCSI_LOGGING
1634 "sd_done: sb[respc,sk,asc,"
1635 "ascq]=%x,%x,%x,%x\n",
1642 (!sense_valid || sense_deferred))
1650 good_bytes = sd_completed_bytes(SCpnt);
1653 good_bytes = scsi_bufflen(SCpnt);
1665 if (sshdr.
asc == 0x10)
1666 good_bytes = sd_completed_bytes(SCpnt);
1669 if (sshdr.
asc == 0x10)
1670 good_bytes = sd_completed_bytes(SCpnt);
1672 if (sshdr.
asc == 0x20 || sshdr.
asc == 0x24) {
1682 sdkp->
device->no_write_same = 1;
1683 sd_config_write_same(sdkp);
1686 req->__data_len = blk_rq_bytes(req);
1696 if (rq_data_dir(SCpnt->
request) ==
READ && scsi_prot_sg_count(SCpnt))
1723 unsigned char cmd[10];
1724 unsigned long spintime_expire = 0;
1726 unsigned int the_result;
1728 int sense_valid = 0;
1739 memset((
void *) &cmd[1], 0, 9);
1755 sense_valid = scsi_sense_valid(&sshdr);
1757 }
while (retries < 3 &&
1758 (!scsi_status_is_good(the_result) ||
1762 if ((
driver_byte(the_result) & DRIVER_SENSE) == 0) {
1765 if(!spintime && !scsi_status_is_good(the_result)) {
1767 sd_print_result(sdkp, the_result);
1775 if (sdkp->
device->no_start_on_add)
1779 if (sshdr.
asc == 4 && sshdr.
ascq == 3)
1781 if (sshdr.
asc == 4 && sshdr.
ascq == 0
xb)
1783 if (sshdr.
asc == 4 && sshdr.
ascq == 0
xc)
1792 memset((
void *) &cmd[2], 0, 8);
1794 if (sdkp->
device->start_stop_pwr_cond)
1812 }
else if (sense_valid &&
1814 sshdr.
asc == 0x28) {
1826 sd_print_sense_hdr(sdkp, &sshdr);
1834 if (scsi_status_is_good(the_result))
1837 printk(
"not responding...\n");
1845 static int sd_read_protection_type(
struct scsi_disk *sdkp,
unsigned char *buffer)
1851 if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
1854 type = ((buffer[12] >> 1) & 7) + 1;
1858 else if (scsi_host_dif_capable(sdp->
host, type))
1865 " protection type %u. Disabling disk!\n",
1870 "Enabling DIF Type %u protection\n", type);
1874 "Disabling DIF Type %u protection\n", type);
1887 sd_print_result(sdkp, the_result);
1889 sd_print_sense_hdr(sdkp, sshdr);
1899 set_media_not_present(sdkp);
1910 #if RC16_LEN > SD_BUF_SIZE
1911 #error RC16_LEN must not be more than SD_BUF_SIZE
1914 #define READ_CAPACITY_RETRIES_ON_RESET 10
1917 unsigned char *buffer)
1919 unsigned char cmd[16];
1921 int sense_valid = 0;
1925 unsigned long long lba;
1946 sense_valid = scsi_sense_valid(&sshdr);
1949 (sshdr.
asc == 0x20 || sshdr.
asc == 0x24) &&
1957 sshdr.
asc == 0x29 && sshdr.
ascq == 0x00)
1960 if (--reset_retries > 0)
1965 }
while (the_result && retries);
1969 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
1976 if (sd_read_protection_type(sdkp, buffer) < 0) {
1981 if ((
sizeof(sdkp->
capacity) == 4) && (lba >= 0xffffffffULL)) {
1983 "kernel compiled with support for large block "
1993 alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) *
sector_size;
1997 "physical block alignment offset: %u\n", alignment);
1999 if (buffer[14] & 0x80) {
2002 if (buffer[14] & 0x40)
2005 sd_config_discard(sdkp, SD_LBP_WS16);
2013 unsigned char *buffer)
2015 unsigned char cmd[16];
2017 int sense_valid = 0;
2036 sense_valid = scsi_sense_valid(&sshdr);
2039 sshdr.
asc == 0x29 && sshdr.
ascq == 0x00)
2042 if (--reset_retries > 0)
2047 }
while (the_result && retries);
2051 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
2067 if ((
sizeof(sdkp->
capacity) == 4) && (lba == 0xffffffff)) {
2069 "kernel compiled with support for large block "
2080 static int sd_try_rc16_first(
struct scsi_device *sdp)
2082 if (sdp->
host->max_cmd_len < 16)
2088 if (scsi_device_protection(sdp))
2097 sd_read_capacity(
struct scsi_disk *sdkp,
unsigned char *buffer)
2103 if (sd_try_rc16_first(sdp)) {
2104 sector_size = read_capacity_16(sdkp, sdp, buffer);
2107 if (sector_size == -
ENODEV)
2109 if (sector_size < 0)
2110 sector_size = read_capacity_10(sdkp, sdp, buffer);
2111 if (sector_size < 0)
2114 sector_size = read_capacity_10(sdkp, sdp, buffer);
2117 if (sector_size < 0)
2119 if ((
sizeof(sdkp->
capacity) > 4) &&
2120 (sdkp->
capacity > 0xffffffffULL)) {
2123 "Trying to use READ CAPACITY(16).\n");
2124 sector_size = read_capacity_16(sdkp, sdp, buffer);
2125 if (sector_size < 0) {
2127 "Using 0xffffffff as device size\n");
2129 sector_size = old_sector_size;
2148 "from its reported value: %llu\n",
2149 (
unsigned long long) sdkp->
capacity);
2154 if (sector_size == 0) {
2160 if (sector_size != 512 &&
2161 sector_size != 1024 &&
2162 sector_size != 2048 &&
2163 sector_size != 4096 &&
2164 sector_size != 256) {
2185 char cap_str_2[10], cap_str_10[10];
2191 sizeof(cap_str_10));
2195 "%llu %d-byte logical blocks: (%s/%s)\n",
2196 (
unsigned long long)sdkp->
capacity,
2197 sector_size, cap_str_10, cap_str_2);
2201 "%u-byte physical blocks\n",
2207 if (sector_size == 4096)
2209 else if (sector_size == 2048)
2211 else if (sector_size == 1024)
2213 else if (sector_size == 256)
2223 sd_do_mode_sense(
struct scsi_device *sdp,
int dbd,
int modepage,
2237 sd_read_write_protect_flag(
struct scsi_disk *sdkp,
unsigned char *buffer)
2251 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &
data,
NULL);
2258 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &
data,
NULL);
2266 if (!scsi_status_is_good(res))
2267 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &
data,
NULL);
2272 if (!scsi_status_is_good(res))
2273 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
2277 if (!scsi_status_is_good(res)) {
2279 "Test WP failed, assume Write Enabled\n");
2287 "Mode Sense: %02x %02x %02x %02x\n",
2288 buffer[0], buffer[1], buffer[2], buffer[3]);
2298 sd_read_cache_type(
struct scsi_disk *sdkp,
unsigned char *buffer)
2308 int old_wce = sdkp->
WCE;
2309 int old_rcd = sdkp->
RCD;
2310 int old_dpofua = sdkp->
DPOFUA;
2333 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, first_len,
2336 if (!scsi_status_is_good(res))
2339 if (!
data.header_length) {
2363 if (len > first_len)
2364 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len,
2367 if (scsi_status_is_good(res)) {
2370 while (offset < len) {
2374 if (page_code == 8 || page_code == 6) {
2377 if (len - offset <= 2) {
2379 "mode parameter data\n");
2387 if (spf && len - offset > 3)
2388 offset += 4 + (buffer[offset+2] << 8) +
2390 else if (!spf && len - offset > 1)
2391 offset += 2 + buffer[offset+1];
2394 "mode parameter data\n");
2400 if (modepage == 0x3F) {
2404 }
else if ((buffer[offset] & 0x3f) != modepage) {
2409 if (modepage == 8) {
2410 sdkp->
WCE = ((buffer[offset + 2] & 0x04) != 0);
2411 sdkp->
RCD = ((buffer[offset + 2] & 0x01) != 0);
2413 sdkp->
WCE = ((buffer[offset + 2] & 0x01) == 0);
2417 sdkp->
DPOFUA = (
data.device_specific & 0x10) != 0;
2420 "Uses READ/WRITE(6), disabling FUA\n");
2425 old_rcd != sdkp->
RCD || old_dpofua != sdkp->
DPOFUA)
2427 "Write cache: %s, read cache: %s, %s\n",
2428 sdkp->
WCE ?
"enabled" :
"disabled",
2429 sdkp->
RCD ?
"disabled" :
"enabled",
2430 sdkp->
DPOFUA ?
"supports DPO and FUA"
2431 :
"doesn't support DPO or FUA");
2437 if (scsi_sense_valid(&sshdr) &&
2439 sshdr.
asc == 0x24 && sshdr.
ascq == 0x0)
2461 static void sd_read_app_tag_own(
struct scsi_disk *sdkp,
unsigned char *buffer)
2477 if (!scsi_status_is_good(res) || !
data.header_length ||
2480 "getting Control mode page failed, assume no ATO\n");
2482 if (scsi_sense_valid(&sshdr))
2483 sd_print_sense_hdr(sdkp, &sshdr);
2488 offset =
data.header_length +
data.block_descriptor_length;
2490 if ((buffer[offset] & 0x3f) != 0x0a) {
2495 if ((buffer[offset + 5] & 0x80) == 0)
2507 static void sd_read_block_limits(
struct scsi_disk *sdkp)
2509 unsigned int sector_sz = sdkp->
device->sector_size;
2510 const int vpd_len = 64;
2519 get_unaligned_be16(&buffer[6]) * sector_sz);
2523 if (buffer[3] == 0x3c) {
2534 if (lba_count && desc_count)
2539 if (buffer[32] & 0x80)
2546 sd_config_discard(sdkp, SD_LBP_UNMAP);
2548 sd_config_discard(sdkp, SD_LBP_WS16);
2553 sd_config_discard(sdkp, SD_LBP_UNMAP);
2554 else if (sdkp->
lbpws)
2555 sd_config_discard(sdkp, SD_LBP_WS16);
2557 sd_config_discard(sdkp, SD_LBP_WS10);
2571 static void sd_read_block_characteristics(
struct scsi_disk *sdkp)
2575 const int vpd_len = 64;
2584 rot = get_unaligned_be16(&buffer[4]);
2587 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, sdkp->
disk->queue);
2597 static void sd_read_block_provisioning(
struct scsi_disk *sdkp)
2600 const int vpd_len = 8;
2602 if (sdkp->
lbpme == 0)
2611 sdkp->
lbpu = (buffer[5] >> 7) & 1;
2612 sdkp->
lbpws = (buffer[5] >> 6) & 1;
2613 sdkp->
lbpws10 = (buffer[5] >> 5) & 1;
2619 static void sd_read_write_same(
struct scsi_disk *sdkp,
unsigned char *buffer)
2626 static int sd_try_extended_inquiry(
struct scsi_device *sdp)
2643 static int sd_revalidate_disk(
struct gendisk *disk)
2651 "sd_revalidate_disk\n"));
2657 if (!scsi_device_online(sdp))
2663 "allocation failure.\n");
2667 sd_spinup_disk(sdkp);
2674 sd_read_capacity(sdkp, buffer);
2676 if (sd_try_extended_inquiry(sdp)) {
2677 sd_read_block_provisioning(sdkp);
2678 sd_read_block_limits(sdkp);
2679 sd_read_block_characteristics(sdkp);
2682 sd_read_write_protect_flag(sdkp, buffer);
2683 sd_read_cache_type(sdkp, buffer);
2684 sd_read_app_tag_own(sdkp, buffer);
2685 sd_read_write_same(sdkp, buffer);
2702 set_capacity(disk, sdkp->
capacity);
2703 sd_config_write_same(sdkp);
2722 static void sd_unlock_native_capacity(
struct gendisk *disk)
2726 if (sdev->
host->hostt->unlock_native_capacity)
2727 sdev->
host->hostt->unlock_native_capacity(sdev);
2754 const int base =
'z' -
'a' + 1;
2755 char *begin = buf +
strlen(prefix);
2766 *--p =
'a' + (index %
unit);
2767 index = (index /
unit) - 1;
2768 }
while (index >= 0);
2789 index = sdkp->
index;
2790 dev = &sdp->sdev_gendev;
2792 gd->major = sd_major((index & 0xf0) >> 4);
2793 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
2796 gd->fops = &sd_fops;
2797 gd->private_data = &sdkp->
driver;
2798 gd->queue = sdkp->
device->request_queue;
2811 sd_revalidate_disk(gd);
2816 gd->driverfs_dev = &sdp->sdev_gendev;
2817 gd->flags = GENHD_FL_EXT_DEVT;
2819 gd->flags |= GENHD_FL_REMOVABLE;
2820 gd->events |= DISK_EVENT_MEDIA_CHANGE;
2827 sd_revalidate_disk(gd);
2831 scsi_autopm_put_device(sdp);
2853 static int sd_probe(
struct device *dev)
2881 spin_lock(&sd_index_lock);
2883 spin_unlock(&sd_index_lock);
2884 }
while (error == -
EAGAIN);
2891 error = sd_format_disk_name(
"sd", index, gd->disk_name, DISK_NAME_LEN);
2894 goto out_free_index;
2898 sdkp->
driver = &sd_template;
2914 sdkp->
dev.class = &sd_disk_class;
2918 goto out_free_index;
2929 spin_lock(&sd_index_lock);
2931 spin_unlock(&sd_index_lock);
2951 static int sd_remove(
struct device *dev)
2956 scsi_autopm_get_device(sdkp->
device);
2982 static void scsi_disk_release(
struct device *dev)
2985 struct gendisk *disk = sdkp->
disk;
2987 spin_lock(&sd_index_lock);
2989 spin_unlock(&sd_index_lock);
2991 disk->private_data =
NULL;
2998 static int sd_start_stop_device(
struct scsi_disk *sdkp,
int start)
3009 cmd[4] |= start ? 1 << 4 : 3 << 4;
3011 if (!scsi_device_online(sdp))
3018 sd_print_result(sdkp, res);
3020 sd_print_sense_hdr(sdkp, &sshdr);
3031 static void sd_shutdown(
struct device *dev)
3033 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
3038 if (pm_runtime_suspended(dev))
3043 sd_sync_cache(sdkp);
3048 sd_start_stop_device(sdkp, 0);
3052 scsi_disk_put(sdkp);
3057 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
3065 ret = sd_sync_cache(sdkp);
3072 ret = sd_start_stop_device(sdkp, 0);
3076 scsi_disk_put(sdkp);
3080 static int sd_resume(
struct device *dev)
3082 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
3085 if (!sdkp->
device->manage_start_stop)
3089 ret = sd_start_stop_device(sdkp, 1);
3092 scsi_disk_put(sdkp);
3102 static int __init init_sd(
void)
3104 int majors = 0,
i,
err;
3125 if (!sd_cdb_cache) {
3130 sd_cdb_pool = mempool_create_slab_pool(
SD_MEMPOOL_SIZE, sd_cdb_cache);
3154 static void __exit exit_sd(
void)
3173 static void sd_print_sense_hdr(
struct scsi_disk *sdkp,
3182 static void sd_print_result(
struct scsi_disk *sdkp,
int result)