sd.c (848ade90ba9c1e2bc2f7869fbe5d08bb32f3db09) sd.c (9c2b9dbafc067e173db30c4fd0636392d27944e8)
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

--- 96 unchanged lines hidden (view full) ---

105static int sd_revalidate_disk(struct gendisk *);
106static void sd_unlock_native_capacity(struct gendisk *disk);
107static int sd_probe(struct device *);
108static int sd_remove(struct device *);
109static void sd_shutdown(struct device *);
110static int sd_suspend_system(struct device *);
111static int sd_suspend_runtime(struct device *);
112static int sd_resume(struct device *);
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

--- 96 unchanged lines hidden (view full) ---

105static int sd_revalidate_disk(struct gendisk *);
106static void sd_unlock_native_capacity(struct gendisk *disk);
107static int sd_probe(struct device *);
108static int sd_remove(struct device *);
109static void sd_shutdown(struct device *);
110static int sd_suspend_system(struct device *);
111static int sd_suspend_runtime(struct device *);
112static int sd_resume(struct device *);
113static int sd_resume_runtime(struct device *);
114static void sd_rescan(struct device *);
115static blk_status_t sd_init_command(struct scsi_cmnd *SCpnt);
116static void sd_uninit_command(struct scsi_cmnd *SCpnt);
117static int sd_done(struct scsi_cmnd *);
118static void sd_eh_reset(struct scsi_cmnd *);
119static int sd_eh_action(struct scsi_cmnd *, int);
120static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
121static void scsi_disk_release(struct device *cdev);
122
123static DEFINE_IDA(sd_index_ida);
124
125/* This semaphore is used to mediate the 0->1 reference get in the
126 * face of object destruction (i.e. we can't allow a get on an
127 * object after last put) */
128static DEFINE_MUTEX(sd_ref_mutex);
129
130static struct kmem_cache *sd_cdb_cache;
131static mempool_t *sd_cdb_pool;
132static mempool_t *sd_page_pool;
113static void sd_rescan(struct device *);
114static blk_status_t sd_init_command(struct scsi_cmnd *SCpnt);
115static void sd_uninit_command(struct scsi_cmnd *SCpnt);
116static int sd_done(struct scsi_cmnd *);
117static void sd_eh_reset(struct scsi_cmnd *);
118static int sd_eh_action(struct scsi_cmnd *, int);
119static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
120static void scsi_disk_release(struct device *cdev);
121
122static DEFINE_IDA(sd_index_ida);
123
124/* This semaphore is used to mediate the 0->1 reference get in the
125 * face of object destruction (i.e. we can't allow a get on an
126 * object after last put) */
127static DEFINE_MUTEX(sd_ref_mutex);
128
129static struct kmem_cache *sd_cdb_cache;
130static mempool_t *sd_cdb_pool;
131static mempool_t *sd_page_pool;
132static struct lock_class_key sd_bio_compl_lkclass;
133
134static const char *sd_cache_types[] = {
135 "write through", "none", "write back",
136 "write back, no read (daft)"
137};
138
139static void sd_set_flush_flag(struct scsi_disk *sdkp)
140{

--- 459 unchanged lines hidden (view full) ---

600};
601
602static const struct dev_pm_ops sd_pm_ops = {
603 .suspend = sd_suspend_system,
604 .resume = sd_resume,
605 .poweroff = sd_suspend_system,
606 .restore = sd_resume,
607 .runtime_suspend = sd_suspend_runtime,
133
134static const char *sd_cache_types[] = {
135 "write through", "none", "write back",
136 "write back, no read (daft)"
137};
138
139static void sd_set_flush_flag(struct scsi_disk *sdkp)
140{

--- 459 unchanged lines hidden (view full) ---

600};
601
602static const struct dev_pm_ops sd_pm_ops = {
603 .suspend = sd_suspend_system,
604 .resume = sd_resume,
605 .poweroff = sd_suspend_system,
606 .restore = sd_resume,
607 .runtime_suspend = sd_suspend_runtime,
608 .runtime_resume = sd_resume_runtime,
608 .runtime_resume = sd_resume,
609};
610
611static struct scsi_driver sd_template = {
612 .gendrv = {
613 .name = "sd",
614 .owner = THIS_MODULE,
615 .probe = sd_probe,
616 .probe_type = PROBE_PREFER_ASYNCHRONOUS,

--- 154 unchanged lines hidden (view full) ---

771 };
772
773 return flag_mask[prot_op];
774}
775
776static unsigned char sd_setup_protect_cmnd(struct scsi_cmnd *scmd,
777 unsigned int dix, unsigned int dif)
778{
609};
610
611static struct scsi_driver sd_template = {
612 .gendrv = {
613 .name = "sd",
614 .owner = THIS_MODULE,
615 .probe = sd_probe,
616 .probe_type = PROBE_PREFER_ASYNCHRONOUS,

--- 154 unchanged lines hidden (view full) ---

771 };
772
773 return flag_mask[prot_op];
774}
775
776static unsigned char sd_setup_protect_cmnd(struct scsi_cmnd *scmd,
777 unsigned int dix, unsigned int dif)
778{
779 struct request *rq = scsi_cmd_to_rq(scmd);
780 struct bio *bio = rq->bio;
781 unsigned int prot_op = sd_prot_op(rq_data_dir(rq), dix, dif);
779 struct bio *bio = scmd->request->bio;
780 unsigned int prot_op = sd_prot_op(rq_data_dir(scmd->request), dix, dif);
782 unsigned int protect = 0;
783
784 if (dix) { /* DIX Type 0, 1, 2, 3 */
785 if (bio_integrity_flagged(bio, BIP_IP_CHECKSUM))
786 scmd->prot_flags |= SCSI_PROT_IP_CHECKSUM;
787
788 if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
789 scmd->prot_flags |= SCSI_PROT_GUARD_CHECK;

--- 74 unchanged lines hidden (view full) ---

864
865 blk_queue_max_discard_sectors(q, max_blocks * (logical_block_size >> 9));
866 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
867}
868
869static blk_status_t sd_setup_unmap_cmnd(struct scsi_cmnd *cmd)
870{
871 struct scsi_device *sdp = cmd->device;
781 unsigned int protect = 0;
782
783 if (dix) { /* DIX Type 0, 1, 2, 3 */
784 if (bio_integrity_flagged(bio, BIP_IP_CHECKSUM))
785 scmd->prot_flags |= SCSI_PROT_IP_CHECKSUM;
786
787 if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
788 scmd->prot_flags |= SCSI_PROT_GUARD_CHECK;

--- 74 unchanged lines hidden (view full) ---

863
864 blk_queue_max_discard_sectors(q, max_blocks * (logical_block_size >> 9));
865 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
866}
867
868static blk_status_t sd_setup_unmap_cmnd(struct scsi_cmnd *cmd)
869{
870 struct scsi_device *sdp = cmd->device;
872 struct request *rq = scsi_cmd_to_rq(cmd);
871 struct request *rq = cmd->request;
873 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
874 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
875 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
876 unsigned int data_len = 24;
877 char *buf;
878
879 rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
880 if (!rq->special_vec.bv_page)
881 return BLK_STS_RESOURCE;
882 clear_highpage(rq->special_vec.bv_page);
883 rq->special_vec.bv_offset = 0;
884 rq->special_vec.bv_len = data_len;
885 rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
886
887 cmd->cmd_len = 10;
888 cmd->cmnd[0] = UNMAP;
889 cmd->cmnd[8] = 24;
890
872 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
873 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
874 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
875 unsigned int data_len = 24;
876 char *buf;
877
878 rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
879 if (!rq->special_vec.bv_page)
880 return BLK_STS_RESOURCE;
881 clear_highpage(rq->special_vec.bv_page);
882 rq->special_vec.bv_offset = 0;
883 rq->special_vec.bv_len = data_len;
884 rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
885
886 cmd->cmd_len = 10;
887 cmd->cmnd[0] = UNMAP;
888 cmd->cmnd[8] = 24;
889
891 buf = page_address(rq->special_vec.bv_page);
890 buf = bvec_virt(&rq->special_vec);
892 put_unaligned_be16(6 + 16, &buf[0]);
893 put_unaligned_be16(16, &buf[2]);
894 put_unaligned_be64(lba, &buf[8]);
895 put_unaligned_be32(nr_blocks, &buf[16]);
896
897 cmd->allowed = sdkp->max_retries;
898 cmd->transfersize = data_len;
899 rq->timeout = SD_TIMEOUT;
900
901 return scsi_alloc_sgtables(cmd);
902}
903
904static blk_status_t sd_setup_write_same16_cmnd(struct scsi_cmnd *cmd,
905 bool unmap)
906{
907 struct scsi_device *sdp = cmd->device;
891 put_unaligned_be16(6 + 16, &buf[0]);
892 put_unaligned_be16(16, &buf[2]);
893 put_unaligned_be64(lba, &buf[8]);
894 put_unaligned_be32(nr_blocks, &buf[16]);
895
896 cmd->allowed = sdkp->max_retries;
897 cmd->transfersize = data_len;
898 rq->timeout = SD_TIMEOUT;
899
900 return scsi_alloc_sgtables(cmd);
901}
902
903static blk_status_t sd_setup_write_same16_cmnd(struct scsi_cmnd *cmd,
904 bool unmap)
905{
906 struct scsi_device *sdp = cmd->device;
908 struct request *rq = scsi_cmd_to_rq(cmd);
907 struct request *rq = cmd->request;
909 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
910 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
911 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
912 u32 data_len = sdp->sector_size;
913
914 rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
915 if (!rq->special_vec.bv_page)
916 return BLK_STS_RESOURCE;

--- 15 unchanged lines hidden (view full) ---

932
933 return scsi_alloc_sgtables(cmd);
934}
935
936static blk_status_t sd_setup_write_same10_cmnd(struct scsi_cmnd *cmd,
937 bool unmap)
938{
939 struct scsi_device *sdp = cmd->device;
908 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
909 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
910 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
911 u32 data_len = sdp->sector_size;
912
913 rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
914 if (!rq->special_vec.bv_page)
915 return BLK_STS_RESOURCE;

--- 15 unchanged lines hidden (view full) ---

931
932 return scsi_alloc_sgtables(cmd);
933}
934
935static blk_status_t sd_setup_write_same10_cmnd(struct scsi_cmnd *cmd,
936 bool unmap)
937{
938 struct scsi_device *sdp = cmd->device;
940 struct request *rq = scsi_cmd_to_rq(cmd);
939 struct request *rq = cmd->request;
941 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
942 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
943 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
944 u32 data_len = sdp->sector_size;
945
946 rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
947 if (!rq->special_vec.bv_page)
948 return BLK_STS_RESOURCE;

--- 13 unchanged lines hidden (view full) ---

962 cmd->transfersize = data_len;
963 rq->timeout = unmap ? SD_TIMEOUT : SD_WRITE_SAME_TIMEOUT;
964
965 return scsi_alloc_sgtables(cmd);
966}
967
968static blk_status_t sd_setup_write_zeroes_cmnd(struct scsi_cmnd *cmd)
969{
940 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
941 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
942 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
943 u32 data_len = sdp->sector_size;
944
945 rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
946 if (!rq->special_vec.bv_page)
947 return BLK_STS_RESOURCE;

--- 13 unchanged lines hidden (view full) ---

961 cmd->transfersize = data_len;
962 rq->timeout = unmap ? SD_TIMEOUT : SD_WRITE_SAME_TIMEOUT;
963
964 return scsi_alloc_sgtables(cmd);
965}
966
967static blk_status_t sd_setup_write_zeroes_cmnd(struct scsi_cmnd *cmd)
968{
970 struct request *rq = scsi_cmd_to_rq(cmd);
969 struct request *rq = cmd->request;
971 struct scsi_device *sdp = cmd->device;
972 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
973 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
974 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
975
976 if (!(rq->cmd_flags & REQ_NOUNMAP)) {
977 switch (sdkp->zeroing_mode) {
978 case SD_ZERO_WS16_UNMAP:

--- 80 unchanged lines hidden (view full) ---

1059 * sd_setup_write_same_cmnd - write the same data to multiple blocks
1060 * @cmd: command to prepare
1061 *
1062 * Will set up either WRITE SAME(10) or WRITE SAME(16) depending on
1063 * the preference indicated by the target device.
1064 **/
1065static blk_status_t sd_setup_write_same_cmnd(struct scsi_cmnd *cmd)
1066{
970 struct scsi_device *sdp = cmd->device;
971 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
972 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
973 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
974
975 if (!(rq->cmd_flags & REQ_NOUNMAP)) {
976 switch (sdkp->zeroing_mode) {
977 case SD_ZERO_WS16_UNMAP:

--- 80 unchanged lines hidden (view full) ---

1058 * sd_setup_write_same_cmnd - write the same data to multiple blocks
1059 * @cmd: command to prepare
1060 *
1061 * Will set up either WRITE SAME(10) or WRITE SAME(16) depending on
1062 * the preference indicated by the target device.
1063 **/
1064static blk_status_t sd_setup_write_same_cmnd(struct scsi_cmnd *cmd)
1065{
1067 struct request *rq = scsi_cmd_to_rq(cmd);
1066 struct request *rq = cmd->request;
1068 struct scsi_device *sdp = cmd->device;
1069 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
1070 struct bio *bio = rq->bio;
1071 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
1072 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
1073 blk_status_t ret;
1074
1075 if (sdkp->device->no_write_same)

--- 32 unchanged lines hidden (view full) ---

1108 ret = scsi_alloc_sgtables(cmd);
1109 rq->__data_len = blk_rq_bytes(rq);
1110
1111 return ret;
1112}
1113
1114static blk_status_t sd_setup_flush_cmnd(struct scsi_cmnd *cmd)
1115{
1067 struct scsi_device *sdp = cmd->device;
1068 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
1069 struct bio *bio = rq->bio;
1070 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
1071 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
1072 blk_status_t ret;
1073
1074 if (sdkp->device->no_write_same)

--- 32 unchanged lines hidden (view full) ---

1107 ret = scsi_alloc_sgtables(cmd);
1108 rq->__data_len = blk_rq_bytes(rq);
1109
1110 return ret;
1111}
1112
1113static blk_status_t sd_setup_flush_cmnd(struct scsi_cmnd *cmd)
1114{
1116 struct request *rq = scsi_cmd_to_rq(cmd);
1115 struct request *rq = cmd->request;
1117 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
1118
1119 /* flush requests don't perform I/O, zero the S/G table */
1120 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
1121
1122 cmd->cmnd[0] = SYNCHRONIZE_CACHE;
1123 cmd->cmd_len = 10;
1124 cmd->transfersize = 0;

--- 81 unchanged lines hidden (view full) ---

1206 cmd->cmnd[4] = nr_blocks;
1207 cmd->cmnd[5] = 0;
1208
1209 return BLK_STS_OK;
1210}
1211
1212static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)
1213{
1116 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
1117
1118 /* flush requests don't perform I/O, zero the S/G table */
1119 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
1120
1121 cmd->cmnd[0] = SYNCHRONIZE_CACHE;
1122 cmd->cmd_len = 10;
1123 cmd->transfersize = 0;

--- 81 unchanged lines hidden (view full) ---

1205 cmd->cmnd[4] = nr_blocks;
1206 cmd->cmnd[5] = 0;
1207
1208 return BLK_STS_OK;
1209}
1210
1211static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)
1212{
1214 struct request *rq = scsi_cmd_to_rq(cmd);
1213 struct request *rq = cmd->request;
1215 struct scsi_device *sdp = cmd->device;
1216 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
1217 sector_t lba = sectors_to_logical(sdp, blk_rq_pos(rq));
1218 sector_t threshold;
1219 unsigned int nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
1220 unsigned int mask = logical_to_sectors(sdp, 1) - 1;
1221 bool write = rq_data_dir(rq) == WRITE;
1222 unsigned char protect, fua;

--- 97 unchanged lines hidden (view full) ---

1320 return BLK_STS_OK;
1321fail:
1322 scsi_free_sgtables(cmd);
1323 return ret;
1324}
1325
1326static blk_status_t sd_init_command(struct scsi_cmnd *cmd)
1327{
1214 struct scsi_device *sdp = cmd->device;
1215 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
1216 sector_t lba = sectors_to_logical(sdp, blk_rq_pos(rq));
1217 sector_t threshold;
1218 unsigned int nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
1219 unsigned int mask = logical_to_sectors(sdp, 1) - 1;
1220 bool write = rq_data_dir(rq) == WRITE;
1221 unsigned char protect, fua;

--- 97 unchanged lines hidden (view full) ---

1319 return BLK_STS_OK;
1320fail:
1321 scsi_free_sgtables(cmd);
1322 return ret;
1323}
1324
1325static blk_status_t sd_init_command(struct scsi_cmnd *cmd)
1326{
1328 struct request *rq = scsi_cmd_to_rq(cmd);
1327 struct request *rq = cmd->request;
1329
1330 switch (req_op(rq)) {
1331 case REQ_OP_DISCARD:
1332 switch (scsi_disk(rq->rq_disk)->provisioning_mode) {
1333 case SD_LBP_UNMAP:
1334 return sd_setup_unmap_cmnd(cmd);
1335 case SD_LBP_WS16:
1336 return sd_setup_write_same16_cmnd(cmd, true);

--- 29 unchanged lines hidden (view full) ---

1366 default:
1367 WARN_ON_ONCE(1);
1368 return BLK_STS_NOTSUPP;
1369 }
1370}
1371
1372static void sd_uninit_command(struct scsi_cmnd *SCpnt)
1373{
1328
1329 switch (req_op(rq)) {
1330 case REQ_OP_DISCARD:
1331 switch (scsi_disk(rq->rq_disk)->provisioning_mode) {
1332 case SD_LBP_UNMAP:
1333 return sd_setup_unmap_cmnd(cmd);
1334 case SD_LBP_WS16:
1335 return sd_setup_write_same16_cmnd(cmd, true);

--- 29 unchanged lines hidden (view full) ---

1365 default:
1366 WARN_ON_ONCE(1);
1367 return BLK_STS_NOTSUPP;
1368 }
1369}
1370
1371static void sd_uninit_command(struct scsi_cmnd *SCpnt)
1372{
1374 struct request *rq = scsi_cmd_to_rq(SCpnt);
1373 struct request *rq = SCpnt->request;
1375 u8 *cmnd;
1376
1377 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1378 mempool_free(rq->special_vec.bv_page, sd_page_pool);
1379
1380 if (SCpnt->cmnd != scsi_req(rq)->cmd) {
1381 cmnd = SCpnt->cmnd;
1382 SCpnt->cmnd = NULL;

--- 143 unchanged lines hidden (view full) ---

1526
1527 geo->heads = diskinfo[0];
1528 geo->sectors = diskinfo[1];
1529 geo->cylinders = diskinfo[2];
1530 return 0;
1531}
1532
1533/**
1374 u8 *cmnd;
1375
1376 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1377 mempool_free(rq->special_vec.bv_page, sd_page_pool);
1378
1379 if (SCpnt->cmnd != scsi_req(rq)->cmd) {
1380 cmnd = SCpnt->cmnd;
1381 SCpnt->cmnd = NULL;

--- 143 unchanged lines hidden (view full) ---

1525
1526 geo->heads = diskinfo[0];
1527 geo->sectors = diskinfo[1];
1528 geo->cylinders = diskinfo[2];
1529 return 0;
1530}
1531
1532/**
1534 * sd_ioctl - process an ioctl
1533 * sd_ioctl_common - process an ioctl
1535 * @bdev: target block device
1536 * @mode: FMODE_* mask
1537 * @cmd: ioctl command number
1534 * @bdev: target block device
1535 * @mode: FMODE_* mask
1536 * @cmd: ioctl command number
1538 * @arg: this is third argument given to ioctl(2) system call.
1537 * @p: this is third argument given to ioctl(2) system call.
1539 * Often contains a pointer.
1540 *
1541 * Returns 0 if successful (some ioctls return positive numbers on
1542 * success as well). Returns a negated errno value in case of error.
1543 *
1544 * Note: most ioctls are forward onto the block subsystem or further
1545 * down in the scsi subsystem.
1546 **/
1538 * Often contains a pointer.
1539 *
1540 * Returns 0 if successful (some ioctls return positive numbers on
1541 * success as well). Returns a negated errno value in case of error.
1542 *
1543 * Note: most ioctls are forward onto the block subsystem or further
1544 * down in the scsi subsystem.
1545 **/
1547static int sd_ioctl(struct block_device *bdev, fmode_t mode,
1548 unsigned int cmd, unsigned long arg)
1546static int sd_ioctl_common(struct block_device *bdev, fmode_t mode,
1547 unsigned int cmd, void __user *p)
1549{
1550 struct gendisk *disk = bdev->bd_disk;
1551 struct scsi_disk *sdkp = scsi_disk(disk);
1552 struct scsi_device *sdp = sdkp->device;
1548{
1549 struct gendisk *disk = bdev->bd_disk;
1550 struct scsi_disk *sdkp = scsi_disk(disk);
1551 struct scsi_device *sdp = sdkp->device;
1553 void __user *p = (void __user *)arg;
1554 int error;
1555
1556 SCSI_LOG_IOCTL(1, sd_printk(KERN_INFO, sdkp, "sd_ioctl: disk=%s, "
1557 "cmd=0x%x\n", disk->disk_name, cmd));
1558
1552 int error;
1553
1554 SCSI_LOG_IOCTL(1, sd_printk(KERN_INFO, sdkp, "sd_ioctl: disk=%s, "
1555 "cmd=0x%x\n", disk->disk_name, cmd));
1556
1559 if (bdev_is_partition(bdev) && !capable(CAP_SYS_RAWIO))
1560 return -ENOIOCTLCMD;
1557 error = scsi_verify_blk_ioctl(bdev, cmd);
1558 if (error < 0)
1559 return error;
1561
1562 /*
1563 * If we are in the middle of error recovery, don't let anyone
1564 * else try and use this device. Also, if error recovery fails, it
1565 * may try and take the device offline, in which case all further
1566 * access to the device is prohibited.
1567 */
1568 error = scsi_ioctl_block_when_processing_errors(sdp, cmd,
1569 (mode & FMODE_NDELAY) != 0);
1570 if (error)
1560
1561 /*
1562 * If we are in the middle of error recovery, don't let anyone
1563 * else try and use this device. Also, if error recovery fails, it
1564 * may try and take the device offline, in which case all further
1565 * access to the device is prohibited.
1566 */
1567 error = scsi_ioctl_block_when_processing_errors(sdp, cmd,
1568 (mode & FMODE_NDELAY) != 0);
1569 if (error)
1571 return error;
1570 goto out;
1572
1573 if (is_sed_ioctl(cmd))
1574 return sed_ioctl(sdkp->opal_dev, cmd, p);
1571
1572 if (is_sed_ioctl(cmd))
1573 return sed_ioctl(sdkp->opal_dev, cmd, p);
1575 return scsi_ioctl(sdp, disk, mode, cmd, p);
1574
1575 /*
1576 * Send SCSI addressing ioctls directly to mid level, send other
1577 * ioctls to block level and then onto mid level if they can't be
1578 * resolved.
1579 */
1580 switch (cmd) {
1581 case SCSI_IOCTL_GET_IDLUN:
1582 case SCSI_IOCTL_GET_BUS_NUMBER:
1583 error = scsi_ioctl(sdp, cmd, p);
1584 break;
1585 default:
1586 error = scsi_cmd_blk_ioctl(bdev, mode, cmd, p);
1587 break;
1588 }
1589out:
1590 return error;
1576}
1577
1578static void set_media_not_present(struct scsi_disk *sdkp)
1579{
1580 if (sdkp->media_present)
1581 sdkp->device->changed = 1;
1582
1583 if (sdkp->device->removable) {

--- 166 unchanged lines hidden (view full) ---

1750
1751static void sd_rescan(struct device *dev)
1752{
1753 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1754
1755 sd_revalidate_disk(sdkp->disk);
1756}
1757
1591}
1592
1593static void set_media_not_present(struct scsi_disk *sdkp)
1594{
1595 if (sdkp->media_present)
1596 sdkp->device->changed = 1;
1597
1598 if (sdkp->device->removable) {

--- 166 unchanged lines hidden (view full) ---

1765
1766static void sd_rescan(struct device *dev)
1767{
1768 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1769
1770 sd_revalidate_disk(sdkp->disk);
1771}
1772
1773static int sd_ioctl(struct block_device *bdev, fmode_t mode,
1774 unsigned int cmd, unsigned long arg)
1775{
1776 void __user *p = (void __user *)arg;
1777 int ret;
1778
1779 ret = sd_ioctl_common(bdev, mode, cmd, p);
1780 if (ret != -ENOTTY)
1781 return ret;
1782
1783 return scsi_ioctl(scsi_disk(bdev->bd_disk)->device, cmd, p);
1784}
1785
1786#ifdef CONFIG_COMPAT
1787static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
1788 unsigned int cmd, unsigned long arg)
1789{
1790 void __user *p = compat_ptr(arg);
1791 int ret;
1792
1793 ret = sd_ioctl_common(bdev, mode, cmd, p);
1794 if (ret != -ENOTTY)
1795 return ret;
1796
1797 return scsi_compat_ioctl(scsi_disk(bdev->bd_disk)->device, cmd, p);
1798}
1799#endif
1800
1758static char sd_pr_type(enum pr_type type)
1759{
1760 switch (type) {
1761 case PR_WRITE_EXCLUSIVE:
1762 return 0x01;
1763 case PR_EXCLUSIVE_ACCESS:
1764 return 0x03;
1765 case PR_WRITE_EXCLUSIVE_REG_ONLY:

--- 84 unchanged lines hidden (view full) ---

1850};
1851
1852static const struct block_device_operations sd_fops = {
1853 .owner = THIS_MODULE,
1854 .open = sd_open,
1855 .release = sd_release,
1856 .ioctl = sd_ioctl,
1857 .getgeo = sd_getgeo,
1801static char sd_pr_type(enum pr_type type)
1802{
1803 switch (type) {
1804 case PR_WRITE_EXCLUSIVE:
1805 return 0x01;
1806 case PR_EXCLUSIVE_ACCESS:
1807 return 0x03;
1808 case PR_WRITE_EXCLUSIVE_REG_ONLY:

--- 84 unchanged lines hidden (view full) ---

1893};
1894
1895static const struct block_device_operations sd_fops = {
1896 .owner = THIS_MODULE,
1897 .open = sd_open,
1898 .release = sd_release,
1899 .ioctl = sd_ioctl,
1900 .getgeo = sd_getgeo,
1858 .compat_ioctl = blkdev_compat_ptr_ioctl,
1901#ifdef CONFIG_COMPAT
1902 .compat_ioctl = sd_compat_ioctl,
1903#endif
1859 .check_events = sd_check_events,
1860 .unlock_native_capacity = sd_unlock_native_capacity,
1861 .report_zones = sd_zbc_report_zones,
1862 .pr_ops = &sd_pr_ops,
1863};
1864
1865/**
1866 * sd_eh_reset - reset error handling callback

--- 4 unchanged lines hidden (view full) ---

1871 * careful to register it only only once per device and SCSI EH run;
1872 * there might be several timed out commands which will cause the
1873 * 'max_medium_access_timeouts' counter to trigger after the first
1874 * SCSI EH run already and set the device to offline.
1875 * So this function resets the internal counter before starting SCSI EH.
1876 **/
1877static void sd_eh_reset(struct scsi_cmnd *scmd)
1878{
1904 .check_events = sd_check_events,
1905 .unlock_native_capacity = sd_unlock_native_capacity,
1906 .report_zones = sd_zbc_report_zones,
1907 .pr_ops = &sd_pr_ops,
1908};
1909
1910/**
1911 * sd_eh_reset - reset error handling callback

--- 4 unchanged lines hidden (view full) ---

1916 * careful to register it only only once per device and SCSI EH run;
1917 * there might be several timed out commands which will cause the
1918 * 'max_medium_access_timeouts' counter to trigger after the first
1919 * SCSI EH run already and set the device to offline.
1920 * So this function resets the internal counter before starting SCSI EH.
1921 **/
1922static void sd_eh_reset(struct scsi_cmnd *scmd)
1923{
1879 struct scsi_disk *sdkp = scsi_disk(scsi_cmd_to_rq(scmd)->rq_disk);
1924 struct scsi_disk *sdkp = scsi_disk(scmd->request->rq_disk);
1880
1881 /* New SCSI EH run, reset gate variable */
1882 sdkp->ignore_medium_access_errors = false;
1883}
1884
1885/**
1886 * sd_eh_action - error handling callback
1887 * @scmd: sd-issued command that has failed
1888 * @eh_disp: The recovery disposition suggested by the midlayer
1889 *
1890 * This function is called by the SCSI midlayer upon completion of an
1891 * error test command (currently TEST UNIT READY). The result of sending
1892 * the eh command is passed in eh_disp. We're looking for devices that
1893 * fail medium access commands but are OK with non access commands like
1894 * test unit ready (so wrongly see the device as having a successful
1895 * recovery)
1896 **/
1897static int sd_eh_action(struct scsi_cmnd *scmd, int eh_disp)
1898{
1925
1926 /* New SCSI EH run, reset gate variable */
1927 sdkp->ignore_medium_access_errors = false;
1928}
1929
1930/**
1931 * sd_eh_action - error handling callback
1932 * @scmd: sd-issued command that has failed
1933 * @eh_disp: The recovery disposition suggested by the midlayer
1934 *
1935 * This function is called by the SCSI midlayer upon completion of an
1936 * error test command (currently TEST UNIT READY). The result of sending
1937 * the eh command is passed in eh_disp. We're looking for devices that
1938 * fail medium access commands but are OK with non access commands like
1939 * test unit ready (so wrongly see the device as having a successful
1940 * recovery)
1941 **/
1942static int sd_eh_action(struct scsi_cmnd *scmd, int eh_disp)
1943{
1899 struct scsi_disk *sdkp = scsi_disk(scsi_cmd_to_rq(scmd)->rq_disk);
1944 struct scsi_disk *sdkp = scsi_disk(scmd->request->rq_disk);
1900 struct scsi_device *sdev = scmd->device;
1901
1902 if (!scsi_device_online(sdev) ||
1903 !scsi_medium_access_command(scmd) ||
1904 host_byte(scmd->result) != DID_TIME_OUT ||
1905 eh_disp != SUCCESS)
1906 return eh_disp;
1907

--- 24 unchanged lines hidden (view full) ---

1932 return SUCCESS;
1933 }
1934
1935 return eh_disp;
1936}
1937
1938static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
1939{
1945 struct scsi_device *sdev = scmd->device;
1946
1947 if (!scsi_device_online(sdev) ||
1948 !scsi_medium_access_command(scmd) ||
1949 host_byte(scmd->result) != DID_TIME_OUT ||
1950 eh_disp != SUCCESS)
1951 return eh_disp;
1952

--- 24 unchanged lines hidden (view full) ---

1977 return SUCCESS;
1978 }
1979
1980 return eh_disp;
1981}
1982
1983static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
1984{
1940 struct request *req = scsi_cmd_to_rq(scmd);
1985 struct request *req = scmd->request;
1941 struct scsi_device *sdev = scmd->device;
1942 unsigned int transferred, good_bytes;
1943 u64 start_lba, end_lba, bad_lba;
1944
1945 /*
1946 * Some commands have a payload smaller than the device logical
1947 * block size (e.g. INQUIRY on a 4K disk).
1948 */

--- 38 unchanged lines hidden (view full) ---

1987 **/
1988static int sd_done(struct scsi_cmnd *SCpnt)
1989{
1990 int result = SCpnt->result;
1991 unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
1992 unsigned int sector_size = SCpnt->device->sector_size;
1993 unsigned int resid;
1994 struct scsi_sense_hdr sshdr;
1986 struct scsi_device *sdev = scmd->device;
1987 unsigned int transferred, good_bytes;
1988 u64 start_lba, end_lba, bad_lba;
1989
1990 /*
1991 * Some commands have a payload smaller than the device logical
1992 * block size (e.g. INQUIRY on a 4K disk).
1993 */

--- 38 unchanged lines hidden (view full) ---

2032 **/
2033static int sd_done(struct scsi_cmnd *SCpnt)
2034{
2035 int result = SCpnt->result;
2036 unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
2037 unsigned int sector_size = SCpnt->device->sector_size;
2038 unsigned int resid;
2039 struct scsi_sense_hdr sshdr;
1995 struct request *req = scsi_cmd_to_rq(SCpnt);
1996 struct scsi_disk *sdkp = scsi_disk(req->rq_disk);
2040 struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk);
2041 struct request *req = SCpnt->request;
1997 int sense_valid = 0;
1998 int sense_deferred = 0;
1999
2000 switch (req_op(req)) {
2001 case REQ_OP_DISCARD:
2002 case REQ_OP_WRITE_ZEROES:
2003 case REQ_OP_WRITE_SAME:
2004 case REQ_OP_ZONE_RESET:

--- 126 unchanged lines hidden (view full) ---

2131 &sshdr, SD_TIMEOUT,
2132 sdkp->max_retries, NULL);
2133
2134 /*
2135 * If the drive has indicated to us that it
2136 * doesn't have any media in it, don't bother
2137 * with any more polling.
2138 */
2042 int sense_valid = 0;
2043 int sense_deferred = 0;
2044
2045 switch (req_op(req)) {
2046 case REQ_OP_DISCARD:
2047 case REQ_OP_WRITE_ZEROES:
2048 case REQ_OP_WRITE_SAME:
2049 case REQ_OP_ZONE_RESET:

--- 126 unchanged lines hidden (view full) ---

2176 &sshdr, SD_TIMEOUT,
2177 sdkp->max_retries, NULL);
2178
2179 /*
2180 * If the drive has indicated to us that it
2181 * doesn't have any media in it, don't bother
2182 * with any more polling.
2183 */
2139 if (media_not_present(sdkp, &sshdr)) {
2140 sd_printk(KERN_NOTICE, sdkp, "Media removed, stopped polling\n");
2184 if (media_not_present(sdkp, &sshdr))
2141 return;
2185 return;
2142 }
2143
2144 if (the_result)
2145 sense_valid = scsi_sense_valid(&sshdr);
2146 retries++;
2147 } while (retries < 3 &&
2148 (!scsi_status_is_good(the_result) ||
2149 (scsi_status_is_check_condition(the_result) &&
2150 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));

--- 1210 unchanged lines hidden (view full) ---

3361 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
3362 "sd_probe\n"));
3363
3364 error = -ENOMEM;
3365 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
3366 if (!sdkp)
3367 goto out;
3368
2186
2187 if (the_result)
2188 sense_valid = scsi_sense_valid(&sshdr);
2189 retries++;
2190 } while (retries < 3 &&
2191 (!scsi_status_is_good(the_result) ||
2192 (scsi_status_is_check_condition(the_result) &&
2193 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));

--- 1210 unchanged lines hidden (view full) ---

3404 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
3405 "sd_probe\n"));
3406
3407 error = -ENOMEM;
3408 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
3409 if (!sdkp)
3410 goto out;
3411
3369 gd = alloc_disk(SD_MINORS);
3412 gd = __alloc_disk_node(SD_MINORS, NUMA_NO_NODE, &sd_bio_compl_lkclass);
3370 if (!gd)
3371 goto out_free;
3372
3373 index = ida_alloc(&sd_index_ida, GFP_KERNEL);
3374 if (index < 0) {
3375 sdev_printk(KERN_WARNING, sdp, "sd_probe: memory exhausted.\n");
3376 goto out_put;
3377 }

--- 291 unchanged lines hidden (view full) ---

3669
3670 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
3671 ret = sd_start_stop_device(sdkp, 1);
3672 if (!ret)
3673 opal_unlock_from_suspend(sdkp->opal_dev);
3674 return ret;
3675}
3676
3413 if (!gd)
3414 goto out_free;
3415
3416 index = ida_alloc(&sd_index_ida, GFP_KERNEL);
3417 if (index < 0) {
3418 sdev_printk(KERN_WARNING, sdp, "sd_probe: memory exhausted.\n");
3419 goto out_put;
3420 }

--- 291 unchanged lines hidden (view full) ---

3712
3713 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
3714 ret = sd_start_stop_device(sdkp, 1);
3715 if (!ret)
3716 opal_unlock_from_suspend(sdkp->opal_dev);
3717 return ret;
3718}
3719
3677static int sd_resume_runtime(struct device *dev)
3678{
3679 struct scsi_disk *sdkp = dev_get_drvdata(dev);
3680 struct scsi_device *sdp = sdkp->device;
3681
3682 if (sdp->ignore_media_change) {
3683 /* clear the device's sense data */
3684 static const u8 cmd[10] = { REQUEST_SENSE };
3685
3686 if (scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL,
3687 NULL, sdp->request_queue->rq_timeout, 1, 0,
3688 RQF_PM, NULL))
3689 sd_printk(KERN_NOTICE, sdkp,
3690 "Failed to clear sense data\n");
3691 }
3692
3693 return sd_resume(dev);
3694}
3695
3696/**
3697 * init_sd - entry point for this driver (both when built in or when
3698 * a module).
3699 *
3700 * Note: this function registers this driver with the scsi mid-level.
3701 **/
3702static int __init init_sd(void)
3703{

--- 107 unchanged lines hidden ---
3720/**
3721 * init_sd - entry point for this driver (both when built in or when
3722 * a module).
3723 *
3724 * Note: this function registers this driver with the scsi mid-level.
3725 **/
3726static int __init init_sd(void)
3727{

--- 107 unchanged lines hidden ---