hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/scsi/sd.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * sd.c Copyright (C) 1992 Drew Eckhardt
34 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
....@@ -45,6 +46,7 @@
4546 #include <linux/init.h>
4647 #include <linux/blkdev.h>
4748 #include <linux/blkpg.h>
49
+#include <linux/blk-pm.h>
4850 #include <linux/delay.h>
4951 #include <linux/mutex.h>
5052 #include <linux/string_helpers.h>
....@@ -113,15 +115,13 @@
113115 static int sd_suspend_runtime(struct device *);
114116 static int sd_resume(struct device *);
115117 static void sd_rescan(struct device *);
116
-static int sd_init_command(struct scsi_cmnd *SCpnt);
118
+static blk_status_t sd_init_command(struct scsi_cmnd *SCpnt);
117119 static void sd_uninit_command(struct scsi_cmnd *SCpnt);
118120 static int sd_done(struct scsi_cmnd *);
119121 static void sd_eh_reset(struct scsi_cmnd *);
120122 static int sd_eh_action(struct scsi_cmnd *, int);
121123 static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
122124 static void scsi_disk_release(struct device *cdev);
123
-static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
124
-static void sd_print_result(const struct scsi_disk *, const char *, int);
125125
126126 static DEFINE_IDA(sd_index_ida);
127127
....@@ -194,7 +194,7 @@
194194 }
195195
196196 if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
197
- SD_MAX_RETRIES, &data, NULL))
197
+ sdkp->max_retries, &data, NULL))
198198 return -EINVAL;
199199 len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
200200 data.block_descriptor_length);
....@@ -212,12 +212,12 @@
212212 data.device_specific = 0;
213213
214214 if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
215
- SD_MAX_RETRIES, &data, &sshdr)) {
215
+ sdkp->max_retries, &data, &sshdr)) {
216216 if (scsi_sense_valid(&sshdr))
217217 sd_print_sense_hdr(sdkp, &sshdr);
218218 return -EINVAL;
219219 }
220
- revalidate_disk(sdkp->disk);
220
+ sd_revalidate_disk(sdkp->disk);
221221 return count;
222222 }
223223
....@@ -528,6 +528,54 @@
528528 }
529529 static DEVICE_ATTR_RW(max_write_same_blocks);
530530
531
+static ssize_t
532
+zoned_cap_show(struct device *dev, struct device_attribute *attr, char *buf)
533
+{
534
+ struct scsi_disk *sdkp = to_scsi_disk(dev);
535
+
536
+ if (sdkp->device->type == TYPE_ZBC)
537
+ return sprintf(buf, "host-managed\n");
538
+ if (sdkp->zoned == 1)
539
+ return sprintf(buf, "host-aware\n");
540
+ if (sdkp->zoned == 2)
541
+ return sprintf(buf, "drive-managed\n");
542
+ return sprintf(buf, "none\n");
543
+}
544
+static DEVICE_ATTR_RO(zoned_cap);
545
+
546
+static ssize_t
547
+max_retries_store(struct device *dev, struct device_attribute *attr,
548
+ const char *buf, size_t count)
549
+{
550
+ struct scsi_disk *sdkp = to_scsi_disk(dev);
551
+ struct scsi_device *sdev = sdkp->device;
552
+ int retries, err;
553
+
554
+ err = kstrtoint(buf, 10, &retries);
555
+ if (err)
556
+ return err;
557
+
558
+ if (retries == SCSI_CMD_RETRIES_NO_LIMIT || retries <= SD_MAX_RETRIES) {
559
+ sdkp->max_retries = retries;
560
+ return count;
561
+ }
562
+
563
+ sdev_printk(KERN_ERR, sdev, "max_retries must be between -1 and %d\n",
564
+ SD_MAX_RETRIES);
565
+ return -EINVAL;
566
+}
567
+
568
+static ssize_t
569
+max_retries_show(struct device *dev, struct device_attribute *attr,
570
+ char *buf)
571
+{
572
+ struct scsi_disk *sdkp = to_scsi_disk(dev);
573
+
574
+ return sprintf(buf, "%d\n", sdkp->max_retries);
575
+}
576
+
577
+static DEVICE_ATTR_RW(max_retries);
578
+
531579 static struct attribute *sd_disk_attrs[] = {
532580 &dev_attr_cache_type.attr,
533581 &dev_attr_FUA.attr,
....@@ -541,6 +589,8 @@
541589 &dev_attr_zeroing_mode.attr,
542590 &dev_attr_max_write_same_blocks.attr,
543591 &dev_attr_max_medium_access_timeouts.attr,
592
+ &dev_attr_zoned_cap.attr,
593
+ &dev_attr_max_retries.attr,
544594 NULL,
545595 };
546596 ATTRIBUTE_GROUPS(sd_disk);
....@@ -566,6 +616,7 @@
566616 .name = "sd",
567617 .owner = THIS_MODULE,
568618 .probe = sd_probe,
619
+ .probe_type = PROBE_PREFER_ASYNCHRONOUS,
569620 .remove = sd_remove,
570621 .shutdown = sd_shutdown,
571622 .pm = &sd_pm_ops,
....@@ -648,7 +699,8 @@
648699 static int sd_sec_submit(void *data, u16 spsp, u8 secp, void *buffer,
649700 size_t len, bool send)
650701 {
651
- struct scsi_device *sdev = data;
702
+ struct scsi_disk *sdkp = data;
703
+ struct scsi_device *sdev = sdkp->device;
652704 u8 cdb[12] = { 0, };
653705 int ret;
654706
....@@ -657,12 +709,74 @@
657709 put_unaligned_be16(spsp, &cdb[2]);
658710 put_unaligned_be32(len, &cdb[6]);
659711
660
- ret = scsi_execute_req(sdev, cdb,
661
- send ? DMA_TO_DEVICE : DMA_FROM_DEVICE,
662
- buffer, len, NULL, SD_TIMEOUT, SD_MAX_RETRIES, NULL);
712
+ ret = scsi_execute(sdev, cdb, send ? DMA_TO_DEVICE : DMA_FROM_DEVICE,
713
+ buffer, len, NULL, NULL, SD_TIMEOUT, sdkp->max_retries, 0,
714
+ RQF_PM, NULL);
663715 return ret <= 0 ? ret : -EIO;
664716 }
665717 #endif /* CONFIG_BLK_SED_OPAL */
718
+
719
+/*
720
+ * Look up the DIX operation based on whether the command is read or
721
+ * write and whether dix and dif are enabled.
722
+ */
723
+static unsigned int sd_prot_op(bool write, bool dix, bool dif)
724
+{
725
+ /* Lookup table: bit 2 (write), bit 1 (dix), bit 0 (dif) */
726
+ static const unsigned int ops[] = { /* wrt dix dif */
727
+ SCSI_PROT_NORMAL, /* 0 0 0 */
728
+ SCSI_PROT_READ_STRIP, /* 0 0 1 */
729
+ SCSI_PROT_READ_INSERT, /* 0 1 0 */
730
+ SCSI_PROT_READ_PASS, /* 0 1 1 */
731
+ SCSI_PROT_NORMAL, /* 1 0 0 */
732
+ SCSI_PROT_WRITE_INSERT, /* 1 0 1 */
733
+ SCSI_PROT_WRITE_STRIP, /* 1 1 0 */
734
+ SCSI_PROT_WRITE_PASS, /* 1 1 1 */
735
+ };
736
+
737
+ return ops[write << 2 | dix << 1 | dif];
738
+}
739
+
740
+/*
741
+ * Returns a mask of the protection flags that are valid for a given DIX
742
+ * operation.
743
+ */
744
+static unsigned int sd_prot_flag_mask(unsigned int prot_op)
745
+{
746
+ static const unsigned int flag_mask[] = {
747
+ [SCSI_PROT_NORMAL] = 0,
748
+
749
+ [SCSI_PROT_READ_STRIP] = SCSI_PROT_TRANSFER_PI |
750
+ SCSI_PROT_GUARD_CHECK |
751
+ SCSI_PROT_REF_CHECK |
752
+ SCSI_PROT_REF_INCREMENT,
753
+
754
+ [SCSI_PROT_READ_INSERT] = SCSI_PROT_REF_INCREMENT |
755
+ SCSI_PROT_IP_CHECKSUM,
756
+
757
+ [SCSI_PROT_READ_PASS] = SCSI_PROT_TRANSFER_PI |
758
+ SCSI_PROT_GUARD_CHECK |
759
+ SCSI_PROT_REF_CHECK |
760
+ SCSI_PROT_REF_INCREMENT |
761
+ SCSI_PROT_IP_CHECKSUM,
762
+
763
+ [SCSI_PROT_WRITE_INSERT] = SCSI_PROT_TRANSFER_PI |
764
+ SCSI_PROT_REF_INCREMENT,
765
+
766
+ [SCSI_PROT_WRITE_STRIP] = SCSI_PROT_GUARD_CHECK |
767
+ SCSI_PROT_REF_CHECK |
768
+ SCSI_PROT_REF_INCREMENT |
769
+ SCSI_PROT_IP_CHECKSUM,
770
+
771
+ [SCSI_PROT_WRITE_PASS] = SCSI_PROT_TRANSFER_PI |
772
+ SCSI_PROT_GUARD_CHECK |
773
+ SCSI_PROT_REF_CHECK |
774
+ SCSI_PROT_REF_INCREMENT |
775
+ SCSI_PROT_IP_CHECKSUM,
776
+ };
777
+
778
+ return flag_mask[prot_op];
779
+}
666780
667781 static unsigned char sd_setup_protect_cmnd(struct scsi_cmnd *scmd,
668782 unsigned int dix, unsigned int dif)
....@@ -756,18 +870,19 @@
756870 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
757871 }
758872
759
-static int sd_setup_unmap_cmnd(struct scsi_cmnd *cmd)
873
+static blk_status_t sd_setup_unmap_cmnd(struct scsi_cmnd *cmd)
760874 {
761875 struct scsi_device *sdp = cmd->device;
762876 struct request *rq = cmd->request;
763
- u64 sector = blk_rq_pos(rq) >> (ilog2(sdp->sector_size) - 9);
764
- u32 nr_sectors = blk_rq_sectors(rq) >> (ilog2(sdp->sector_size) - 9);
877
+ struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
878
+ u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
879
+ u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
765880 unsigned int data_len = 24;
766881 char *buf;
767882
768883 rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
769884 if (!rq->special_vec.bv_page)
770
- return BLKPREP_DEFER;
885
+ return BLK_STS_RESOURCE;
771886 clear_highpage(rq->special_vec.bv_page);
772887 rq->special_vec.bv_offset = 0;
773888 rq->special_vec.bv_len = data_len;
....@@ -780,28 +895,29 @@
780895 buf = page_address(rq->special_vec.bv_page);
781896 put_unaligned_be16(6 + 16, &buf[0]);
782897 put_unaligned_be16(16, &buf[2]);
783
- put_unaligned_be64(sector, &buf[8]);
784
- put_unaligned_be32(nr_sectors, &buf[16]);
898
+ put_unaligned_be64(lba, &buf[8]);
899
+ put_unaligned_be32(nr_blocks, &buf[16]);
785900
786
- cmd->allowed = SD_MAX_RETRIES;
901
+ cmd->allowed = sdkp->max_retries;
787902 cmd->transfersize = data_len;
788903 rq->timeout = SD_TIMEOUT;
789
- scsi_req(rq)->resid_len = data_len;
790904
791
- return scsi_init_io(cmd);
905
+ return scsi_alloc_sgtables(cmd);
792906 }
793907
794
-static int sd_setup_write_same16_cmnd(struct scsi_cmnd *cmd, bool unmap)
908
+static blk_status_t sd_setup_write_same16_cmnd(struct scsi_cmnd *cmd,
909
+ bool unmap)
795910 {
796911 struct scsi_device *sdp = cmd->device;
797912 struct request *rq = cmd->request;
798
- u64 sector = blk_rq_pos(rq) >> (ilog2(sdp->sector_size) - 9);
799
- u32 nr_sectors = blk_rq_sectors(rq) >> (ilog2(sdp->sector_size) - 9);
913
+ struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
914
+ u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
915
+ u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
800916 u32 data_len = sdp->sector_size;
801917
802918 rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
803919 if (!rq->special_vec.bv_page)
804
- return BLKPREP_DEFER;
920
+ return BLK_STS_RESOURCE;
805921 clear_highpage(rq->special_vec.bv_page);
806922 rq->special_vec.bv_offset = 0;
807923 rq->special_vec.bv_len = data_len;
....@@ -811,28 +927,29 @@
811927 cmd->cmnd[0] = WRITE_SAME_16;
812928 if (unmap)
813929 cmd->cmnd[1] = 0x8; /* UNMAP */
814
- put_unaligned_be64(sector, &cmd->cmnd[2]);
815
- put_unaligned_be32(nr_sectors, &cmd->cmnd[10]);
930
+ put_unaligned_be64(lba, &cmd->cmnd[2]);
931
+ put_unaligned_be32(nr_blocks, &cmd->cmnd[10]);
816932
817
- cmd->allowed = SD_MAX_RETRIES;
933
+ cmd->allowed = sdkp->max_retries;
818934 cmd->transfersize = data_len;
819935 rq->timeout = unmap ? SD_TIMEOUT : SD_WRITE_SAME_TIMEOUT;
820
- scsi_req(rq)->resid_len = data_len;
821936
822
- return scsi_init_io(cmd);
937
+ return scsi_alloc_sgtables(cmd);
823938 }
824939
825
-static int sd_setup_write_same10_cmnd(struct scsi_cmnd *cmd, bool unmap)
940
+static blk_status_t sd_setup_write_same10_cmnd(struct scsi_cmnd *cmd,
941
+ bool unmap)
826942 {
827943 struct scsi_device *sdp = cmd->device;
828944 struct request *rq = cmd->request;
829
- u64 sector = blk_rq_pos(rq) >> (ilog2(sdp->sector_size) - 9);
830
- u32 nr_sectors = blk_rq_sectors(rq) >> (ilog2(sdp->sector_size) - 9);
945
+ struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
946
+ u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
947
+ u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
831948 u32 data_len = sdp->sector_size;
832949
833950 rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
834951 if (!rq->special_vec.bv_page)
835
- return BLKPREP_DEFER;
952
+ return BLK_STS_RESOURCE;
836953 clear_highpage(rq->special_vec.bv_page);
837954 rq->special_vec.bv_offset = 0;
838955 rq->special_vec.bv_len = data_len;
....@@ -842,24 +959,23 @@
842959 cmd->cmnd[0] = WRITE_SAME;
843960 if (unmap)
844961 cmd->cmnd[1] = 0x8; /* UNMAP */
845
- put_unaligned_be32(sector, &cmd->cmnd[2]);
846
- put_unaligned_be16(nr_sectors, &cmd->cmnd[7]);
962
+ put_unaligned_be32(lba, &cmd->cmnd[2]);
963
+ put_unaligned_be16(nr_blocks, &cmd->cmnd[7]);
847964
848
- cmd->allowed = SD_MAX_RETRIES;
965
+ cmd->allowed = sdkp->max_retries;
849966 cmd->transfersize = data_len;
850967 rq->timeout = unmap ? SD_TIMEOUT : SD_WRITE_SAME_TIMEOUT;
851
- scsi_req(rq)->resid_len = data_len;
852968
853
- return scsi_init_io(cmd);
969
+ return scsi_alloc_sgtables(cmd);
854970 }
855971
856
-static int sd_setup_write_zeroes_cmnd(struct scsi_cmnd *cmd)
972
+static blk_status_t sd_setup_write_zeroes_cmnd(struct scsi_cmnd *cmd)
857973 {
858974 struct request *rq = cmd->request;
859975 struct scsi_device *sdp = cmd->device;
860976 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
861
- u64 sector = blk_rq_pos(rq) >> (ilog2(sdp->sector_size) - 9);
862
- u32 nr_sectors = blk_rq_sectors(rq) >> (ilog2(sdp->sector_size) - 9);
977
+ u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
978
+ u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
863979
864980 if (!(rq->cmd_flags & REQ_NOUNMAP)) {
865981 switch (sdkp->zeroing_mode) {
....@@ -870,10 +986,12 @@
870986 }
871987 }
872988
873
- if (sdp->no_write_same)
874
- return BLKPREP_INVALID;
989
+ if (sdp->no_write_same) {
990
+ rq->rq_flags |= RQF_QUIET;
991
+ return BLK_STS_TARGET;
992
+ }
875993
876
- if (sdkp->ws16 || sector > 0xffffffff || nr_sectors > 0xffff)
994
+ if (sdkp->ws16 || lba > 0xffffffff || nr_blocks > 0xffff)
877995 return sd_setup_write_same16_cmnd(cmd, false);
878996
879997 return sd_setup_write_same10_cmnd(cmd, false);
....@@ -948,44 +1066,38 @@
9481066 * Will set up either WRITE SAME(10) or WRITE SAME(16) depending on
9491067 * the preference indicated by the target device.
9501068 **/
951
-static int sd_setup_write_same_cmnd(struct scsi_cmnd *cmd)
1069
+static blk_status_t sd_setup_write_same_cmnd(struct scsi_cmnd *cmd)
9521070 {
9531071 struct request *rq = cmd->request;
9541072 struct scsi_device *sdp = cmd->device;
9551073 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
9561074 struct bio *bio = rq->bio;
957
- sector_t sector = blk_rq_pos(rq);
958
- unsigned int nr_sectors = blk_rq_sectors(rq);
1075
+ u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
1076
+ u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
9591077 unsigned int nr_bytes = blk_rq_bytes(rq);
960
- int ret;
1078
+ blk_status_t ret;
9611079
9621080 if (sdkp->device->no_write_same)
963
- return BLKPREP_INVALID;
1081
+ return BLK_STS_TARGET;
9641082
9651083 BUG_ON(bio_offset(bio) || bio_iovec(bio).bv_len != sdp->sector_size);
9661084
967
- sector >>= ilog2(sdp->sector_size) - 9;
968
- nr_sectors >>= ilog2(sdp->sector_size) - 9;
1085
+ rq->timeout = SD_WRITE_SAME_TIMEOUT;
9691086
970
- if (likely(!sdp->timeout_override))
971
- rq->timeout = SD_WRITE_SAME_TIMEOUT;
972
- else
973
- rq->timeout = sdp->timeout_override;
974
-
975
- if (sdkp->ws16 || sector > 0xffffffff || nr_sectors > 0xffff) {
1087
+ if (sdkp->ws16 || lba > 0xffffffff || nr_blocks > 0xffff) {
9761088 cmd->cmd_len = 16;
9771089 cmd->cmnd[0] = WRITE_SAME_16;
978
- put_unaligned_be64(sector, &cmd->cmnd[2]);
979
- put_unaligned_be32(nr_sectors, &cmd->cmnd[10]);
1090
+ put_unaligned_be64(lba, &cmd->cmnd[2]);
1091
+ put_unaligned_be32(nr_blocks, &cmd->cmnd[10]);
9801092 } else {
9811093 cmd->cmd_len = 10;
9821094 cmd->cmnd[0] = WRITE_SAME;
983
- put_unaligned_be32(sector, &cmd->cmnd[2]);
984
- put_unaligned_be16(nr_sectors, &cmd->cmnd[7]);
1095
+ put_unaligned_be32(lba, &cmd->cmnd[2]);
1096
+ put_unaligned_be16(nr_blocks, &cmd->cmnd[7]);
9851097 }
9861098
9871099 cmd->transfersize = sdp->sector_size;
988
- cmd->allowed = SD_MAX_RETRIES;
1100
+ cmd->allowed = sdkp->max_retries;
9891101
9901102 /*
9911103 * For WRITE SAME the data transferred via the DATA OUT buffer is
....@@ -998,15 +1110,16 @@
9981110 * knows how much to actually write.
9991111 */
10001112 rq->__data_len = sdp->sector_size;
1001
- ret = scsi_init_io(cmd);
1113
+ ret = scsi_alloc_sgtables(cmd);
10021114 rq->__data_len = nr_bytes;
10031115
10041116 return ret;
10051117 }
10061118
1007
-static int sd_setup_flush_cmnd(struct scsi_cmnd *cmd)
1119
+static blk_status_t sd_setup_flush_cmnd(struct scsi_cmnd *cmd)
10081120 {
10091121 struct request *rq = cmd->request;
1122
+ struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
10101123
10111124 /* flush requests don't perform I/O, zero the S/G table */
10121125 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
....@@ -1014,250 +1127,208 @@
10141127 cmd->cmnd[0] = SYNCHRONIZE_CACHE;
10151128 cmd->cmd_len = 10;
10161129 cmd->transfersize = 0;
1017
- cmd->allowed = SD_MAX_RETRIES;
1130
+ cmd->allowed = sdkp->max_retries;
10181131
10191132 rq->timeout = rq->q->rq_timeout * SD_FLUSH_TIMEOUT_MULTIPLIER;
1020
- return BLKPREP_OK;
1133
+ return BLK_STS_OK;
10211134 }
10221135
1023
-static int sd_setup_read_write_cmnd(struct scsi_cmnd *SCpnt)
1136
+static blk_status_t sd_setup_rw32_cmnd(struct scsi_cmnd *cmd, bool write,
1137
+ sector_t lba, unsigned int nr_blocks,
1138
+ unsigned char flags)
10241139 {
1025
- struct request *rq = SCpnt->request;
1026
- struct scsi_device *sdp = SCpnt->device;
1027
- struct gendisk *disk = rq->rq_disk;
1028
- struct scsi_disk *sdkp = scsi_disk(disk);
1029
- sector_t block = blk_rq_pos(rq);
1030
- sector_t threshold;
1031
- unsigned int this_count = blk_rq_sectors(rq);
1032
- unsigned int dif, dix;
1033
- int ret;
1034
- unsigned char protect;
1140
+ cmd->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
1141
+ if (unlikely(cmd->cmnd == NULL))
1142
+ return BLK_STS_RESOURCE;
10351143
1036
- ret = scsi_init_io(SCpnt);
1037
- if (ret != BLKPREP_OK)
1038
- return ret;
1039
- WARN_ON_ONCE(SCpnt != rq->special);
1144
+ cmd->cmd_len = SD_EXT_CDB_SIZE;
1145
+ memset(cmd->cmnd, 0, cmd->cmd_len);
10401146
1041
- /* from here on until we're complete, any goto out
1042
- * is used for a killable error condition */
1043
- ret = BLKPREP_KILL;
1147
+ cmd->cmnd[0] = VARIABLE_LENGTH_CMD;
1148
+ cmd->cmnd[7] = 0x18; /* Additional CDB len */
1149
+ cmd->cmnd[9] = write ? WRITE_32 : READ_32;
1150
+ cmd->cmnd[10] = flags;
1151
+ put_unaligned_be64(lba, &cmd->cmnd[12]);
1152
+ put_unaligned_be32(lba, &cmd->cmnd[20]); /* Expected Indirect LBA */
1153
+ put_unaligned_be32(nr_blocks, &cmd->cmnd[28]);
10441154
1045
- SCSI_LOG_HLQUEUE(1,
1046
- scmd_printk(KERN_INFO, SCpnt,
1047
- "%s: block=%llu, count=%d\n",
1048
- __func__, (unsigned long long)block, this_count));
1155
+ return BLK_STS_OK;
1156
+}
10491157
1050
- if (!sdp || !scsi_device_online(sdp) ||
1051
- block + blk_rq_sectors(rq) > get_capacity(disk)) {
1052
- SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
1053
- "Finishing %u sectors\n",
1054
- blk_rq_sectors(rq)));
1055
- SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
1056
- "Retry with 0x%p\n", SCpnt));
1057
- goto out;
1058
- }
1158
+static blk_status_t sd_setup_rw16_cmnd(struct scsi_cmnd *cmd, bool write,
1159
+ sector_t lba, unsigned int nr_blocks,
1160
+ unsigned char flags)
1161
+{
1162
+ cmd->cmd_len = 16;
1163
+ cmd->cmnd[0] = write ? WRITE_16 : READ_16;
1164
+ cmd->cmnd[1] = flags;
1165
+ cmd->cmnd[14] = 0;
1166
+ cmd->cmnd[15] = 0;
1167
+ put_unaligned_be64(lba, &cmd->cmnd[2]);
1168
+ put_unaligned_be32(nr_blocks, &cmd->cmnd[10]);
10591169
1060
- if (sdp->changed) {
1170
+ return BLK_STS_OK;
1171
+}
1172
+
1173
+static blk_status_t sd_setup_rw10_cmnd(struct scsi_cmnd *cmd, bool write,
1174
+ sector_t lba, unsigned int nr_blocks,
1175
+ unsigned char flags)
1176
+{
1177
+ cmd->cmd_len = 10;
1178
+ cmd->cmnd[0] = write ? WRITE_10 : READ_10;
1179
+ cmd->cmnd[1] = flags;
1180
+ cmd->cmnd[6] = 0;
1181
+ cmd->cmnd[9] = 0;
1182
+ put_unaligned_be32(lba, &cmd->cmnd[2]);
1183
+ put_unaligned_be16(nr_blocks, &cmd->cmnd[7]);
1184
+
1185
+ return BLK_STS_OK;
1186
+}
1187
+
1188
+static blk_status_t sd_setup_rw6_cmnd(struct scsi_cmnd *cmd, bool write,
1189
+ sector_t lba, unsigned int nr_blocks,
1190
+ unsigned char flags)
1191
+{
1192
+ /* Avoid that 0 blocks gets translated into 256 blocks. */
1193
+ if (WARN_ON_ONCE(nr_blocks == 0))
1194
+ return BLK_STS_IOERR;
1195
+
1196
+ if (unlikely(flags & 0x8)) {
10611197 /*
1062
- * quietly refuse to do anything to a changed disc until
1063
- * the changed bit has been reset
1198
+ * This happens only if this drive failed 10byte rw
1199
+ * command with ILLEGAL_REQUEST during operation and
1200
+ * thus turned off use_10_for_rw.
10641201 */
1065
- /* printk("SCSI disk has been changed or is not present. Prohibiting further I/O.\n"); */
1066
- goto out;
1202
+ scmd_printk(KERN_ERR, cmd, "FUA write on READ/WRITE(6) drive\n");
1203
+ return BLK_STS_IOERR;
1204
+ }
1205
+
1206
+ cmd->cmd_len = 6;
1207
+ cmd->cmnd[0] = write ? WRITE_6 : READ_6;
1208
+ cmd->cmnd[1] = (lba >> 16) & 0x1f;
1209
+ cmd->cmnd[2] = (lba >> 8) & 0xff;
1210
+ cmd->cmnd[3] = lba & 0xff;
1211
+ cmd->cmnd[4] = nr_blocks;
1212
+ cmd->cmnd[5] = 0;
1213
+
1214
+ return BLK_STS_OK;
1215
+}
1216
+
1217
+static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)
1218
+{
1219
+ struct request *rq = cmd->request;
1220
+ struct scsi_device *sdp = cmd->device;
1221
+ struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
1222
+ sector_t lba = sectors_to_logical(sdp, blk_rq_pos(rq));
1223
+ sector_t threshold;
1224
+ unsigned int nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
1225
+ unsigned int mask = logical_to_sectors(sdp, 1) - 1;
1226
+ bool write = rq_data_dir(rq) == WRITE;
1227
+ unsigned char protect, fua;
1228
+ blk_status_t ret;
1229
+ unsigned int dif;
1230
+ bool dix;
1231
+
1232
+ ret = scsi_alloc_sgtables(cmd);
1233
+ if (ret != BLK_STS_OK)
1234
+ return ret;
1235
+
1236
+ ret = BLK_STS_IOERR;
1237
+ if (!scsi_device_online(sdp) || sdp->changed) {
1238
+ scmd_printk(KERN_ERR, cmd, "device offline or changed\n");
1239
+ goto fail;
1240
+ }
1241
+
1242
+ if (blk_rq_pos(rq) + blk_rq_sectors(rq) > get_capacity(rq->rq_disk)) {
1243
+ scmd_printk(KERN_ERR, cmd, "access beyond end of device\n");
1244
+ goto fail;
1245
+ }
1246
+
1247
+ if ((blk_rq_pos(rq) & mask) || (blk_rq_sectors(rq) & mask)) {
1248
+ scmd_printk(KERN_ERR, cmd, "request not aligned to the logical block size\n");
1249
+ goto fail;
10671250 }
10681251
10691252 /*
1070
- * Some SD card readers can't handle multi-sector accesses which touch
1071
- * the last one or two hardware sectors. Split accesses as needed.
1253
+ * Some SD card readers can't handle accesses which touch the
1254
+ * last one or two logical blocks. Split accesses as needed.
10721255 */
1073
- threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
1074
- (sdp->sector_size / 512);
1256
+ threshold = sdkp->capacity - SD_LAST_BUGGY_SECTORS;
10751257
1076
- if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
1077
- if (block < threshold) {
1258
+ if (unlikely(sdp->last_sector_bug && lba + nr_blocks > threshold)) {
1259
+ if (lba < threshold) {
10781260 /* Access up to the threshold but not beyond */
1079
- this_count = threshold - block;
1261
+ nr_blocks = threshold - lba;
10801262 } else {
1081
- /* Access only a single hardware sector */
1082
- this_count = sdp->sector_size / 512;
1263
+ /* Access only a single logical block */
1264
+ nr_blocks = 1;
10831265 }
10841266 }
10851267
1086
- SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
1087
- (unsigned long long)block));
1088
-
1089
- /*
1090
- * If we have a 1K hardware sectorsize, prevent access to single
1091
- * 512 byte sectors. In theory we could handle this - in fact
1092
- * the scsi cdrom driver must be able to handle this because
1093
- * we typically use 1K blocksizes, and cdroms typically have
1094
- * 2K hardware sectorsizes. Of course, things are simpler
1095
- * with the cdrom, since it is read-only. For performance
1096
- * reasons, the filesystems should be able to handle this
1097
- * and not force the scsi disk driver to use bounce buffers
1098
- * for this.
1099
- */
1100
- if (sdp->sector_size == 1024) {
1101
- if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
1102
- scmd_printk(KERN_ERR, SCpnt,
1103
- "Bad block number requested\n");
1104
- goto out;
1105
- } else {
1106
- block = block >> 1;
1107
- this_count = this_count >> 1;
1108
- }
1109
- }
1110
- if (sdp->sector_size == 2048) {
1111
- if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
1112
- scmd_printk(KERN_ERR, SCpnt,
1113
- "Bad block number requested\n");
1114
- goto out;
1115
- } else {
1116
- block = block >> 2;
1117
- this_count = this_count >> 2;
1118
- }
1119
- }
1120
- if (sdp->sector_size == 4096) {
1121
- if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
1122
- scmd_printk(KERN_ERR, SCpnt,
1123
- "Bad block number requested\n");
1124
- goto out;
1125
- } else {
1126
- block = block >> 3;
1127
- this_count = this_count >> 3;
1128
- }
1129
- }
1130
- if (rq_data_dir(rq) == WRITE) {
1131
- SCpnt->cmnd[0] = WRITE_6;
1132
-
1133
- if (blk_integrity_rq(rq))
1134
- t10_pi_prepare(SCpnt->request, sdkp->protection_type);
1135
-
1136
- } else if (rq_data_dir(rq) == READ) {
1137
- SCpnt->cmnd[0] = READ_6;
1138
- } else {
1139
- scmd_printk(KERN_ERR, SCpnt, "Unknown command %d\n", req_op(rq));
1140
- goto out;
1268
+ if (req_op(rq) == REQ_OP_ZONE_APPEND) {
1269
+ ret = sd_zbc_prepare_zone_append(cmd, &lba, nr_blocks);
1270
+ if (ret)
1271
+ goto fail;
11411272 }
11421273
1143
- SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
1144
- "%s %d/%u 512 byte blocks.\n",
1145
- (rq_data_dir(rq) == WRITE) ?
1146
- "writing" : "reading", this_count,
1147
- blk_rq_sectors(rq)));
1148
-
1149
- dix = scsi_prot_sg_count(SCpnt);
1150
- dif = scsi_host_dif_capable(SCpnt->device->host, sdkp->protection_type);
1274
+ fua = rq->cmd_flags & REQ_FUA ? 0x8 : 0;
1275
+ dix = scsi_prot_sg_count(cmd);
1276
+ dif = scsi_host_dif_capable(cmd->device->host, sdkp->protection_type);
11511277
11521278 if (dif || dix)
1153
- protect = sd_setup_protect_cmnd(SCpnt, dix, dif);
1279
+ protect = sd_setup_protect_cmnd(cmd, dix, dif);
11541280 else
11551281 protect = 0;
11561282
11571283 if (protect && sdkp->protection_type == T10_PI_TYPE2_PROTECTION) {
1158
- SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
1159
-
1160
- if (unlikely(SCpnt->cmnd == NULL)) {
1161
- ret = BLKPREP_DEFER;
1162
- goto out;
1163
- }
1164
-
1165
- SCpnt->cmd_len = SD_EXT_CDB_SIZE;
1166
- memset(SCpnt->cmnd, 0, SCpnt->cmd_len);
1167
- SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
1168
- SCpnt->cmnd[7] = 0x18;
1169
- SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
1170
- SCpnt->cmnd[10] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
1171
-
1172
- /* LBA */
1173
- SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
1174
- SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
1175
- SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
1176
- SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
1177
- SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff;
1178
- SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff;
1179
- SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff;
1180
- SCpnt->cmnd[19] = (unsigned char) block & 0xff;
1181
-
1182
- /* Expected Indirect LBA */
1183
- SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff;
1184
- SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff;
1185
- SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff;
1186
- SCpnt->cmnd[23] = (unsigned char) block & 0xff;
1187
-
1188
- /* Transfer length */
1189
- SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff;
1190
- SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff;
1191
- SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff;
1192
- SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
1193
- } else if (sdp->use_16_for_rw || (this_count > 0xffff)) {
1194
- SCpnt->cmnd[0] += READ_16 - READ_6;
1195
- SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
1196
- SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
1197
- SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
1198
- SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
1199
- SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
1200
- SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
1201
- SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
1202
- SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
1203
- SCpnt->cmnd[9] = (unsigned char) block & 0xff;
1204
- SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
1205
- SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
1206
- SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
1207
- SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
1208
- SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
1209
- } else if ((this_count > 0xff) || (block > 0x1fffff) ||
1210
- scsi_device_protection(SCpnt->device) ||
1211
- SCpnt->device->use_10_for_rw) {
1212
- SCpnt->cmnd[0] += READ_10 - READ_6;
1213
- SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
1214
- SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
1215
- SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
1216
- SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
1217
- SCpnt->cmnd[5] = (unsigned char) block & 0xff;
1218
- SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
1219
- SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
1220
- SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
1284
+ ret = sd_setup_rw32_cmnd(cmd, write, lba, nr_blocks,
1285
+ protect | fua);
1286
+ } else if (sdp->use_16_for_rw || (nr_blocks > 0xffff)) {
1287
+ ret = sd_setup_rw16_cmnd(cmd, write, lba, nr_blocks,
1288
+ protect | fua);
1289
+ } else if ((nr_blocks > 0xff) || (lba > 0x1fffff) ||
1290
+ sdp->use_10_for_rw || protect) {
1291
+ ret = sd_setup_rw10_cmnd(cmd, write, lba, nr_blocks,
1292
+ protect | fua);
12211293 } else {
1222
- if (unlikely(rq->cmd_flags & REQ_FUA)) {
1223
- /*
1224
- * This happens only if this drive failed
1225
- * 10byte rw command with ILLEGAL_REQUEST
1226
- * during operation and thus turned off
1227
- * use_10_for_rw.
1228
- */
1229
- scmd_printk(KERN_ERR, SCpnt,
1230
- "FUA write on READ/WRITE(6) drive\n");
1231
- goto out;
1232
- }
1233
-
1234
- SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
1235
- SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
1236
- SCpnt->cmnd[3] = (unsigned char) block & 0xff;
1237
- SCpnt->cmnd[4] = (unsigned char) this_count;
1238
- SCpnt->cmnd[5] = 0;
1294
+ ret = sd_setup_rw6_cmnd(cmd, write, lba, nr_blocks,
1295
+ protect | fua);
12391296 }
1240
- SCpnt->sdb.length = this_count * sdp->sector_size;
1297
+
1298
+ if (unlikely(ret != BLK_STS_OK))
1299
+ goto fail;
12411300
12421301 /*
12431302 * We shouldn't disconnect in the middle of a sector, so with a dumb
12441303 * host adapter, it's safe to assume that we can at least transfer
12451304 * this many bytes between each connect / disconnect.
12461305 */
1247
- SCpnt->transfersize = sdp->sector_size;
1248
- SCpnt->underflow = this_count << 9;
1249
- SCpnt->allowed = SD_MAX_RETRIES;
1306
+ cmd->transfersize = sdp->sector_size;
1307
+ cmd->underflow = nr_blocks << 9;
1308
+ cmd->allowed = sdkp->max_retries;
1309
+ cmd->sdb.length = nr_blocks * sdp->sector_size;
1310
+
1311
+ SCSI_LOG_HLQUEUE(1,
1312
+ scmd_printk(KERN_INFO, cmd,
1313
+ "%s: block=%llu, count=%d\n", __func__,
1314
+ (unsigned long long)blk_rq_pos(rq),
1315
+ blk_rq_sectors(rq)));
1316
+ SCSI_LOG_HLQUEUE(2,
1317
+ scmd_printk(KERN_INFO, cmd,
1318
+ "%s %d/%u 512 byte blocks.\n",
1319
+ write ? "writing" : "reading", nr_blocks,
1320
+ blk_rq_sectors(rq)));
12501321
12511322 /*
1252
- * This indicates that the command is ready from our end to be
1253
- * queued.
1323
+ * This indicates that the command is ready from our end to be queued.
12541324 */
1255
- ret = BLKPREP_OK;
1256
- out:
1325
+ return BLK_STS_OK;
1326
+fail:
1327
+ scsi_free_sgtables(cmd);
12571328 return ret;
12581329 }
12591330
1260
-static int sd_init_command(struct scsi_cmnd *cmd)
1331
+static blk_status_t sd_init_command(struct scsi_cmnd *cmd)
12611332 {
12621333 struct request *rq = cmd->request;
12631334
....@@ -1273,7 +1344,7 @@
12731344 case SD_LBP_ZERO:
12741345 return sd_setup_write_same10_cmnd(cmd, false);
12751346 default:
1276
- return BLKPREP_INVALID;
1347
+ return BLK_STS_TARGET;
12771348 }
12781349 case REQ_OP_WRITE_ZEROES:
12791350 return sd_setup_write_zeroes_cmnd(cmd);
....@@ -1283,14 +1354,23 @@
12831354 return sd_setup_flush_cmnd(cmd);
12841355 case REQ_OP_READ:
12851356 case REQ_OP_WRITE:
1357
+ case REQ_OP_ZONE_APPEND:
12861358 return sd_setup_read_write_cmnd(cmd);
1287
- case REQ_OP_ZONE_REPORT:
1288
- return sd_zbc_setup_report_cmnd(cmd);
12891359 case REQ_OP_ZONE_RESET:
1290
- return sd_zbc_setup_reset_cmnd(cmd);
1360
+ return sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_RESET_WRITE_POINTER,
1361
+ false);
1362
+ case REQ_OP_ZONE_RESET_ALL:
1363
+ return sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_RESET_WRITE_POINTER,
1364
+ true);
1365
+ case REQ_OP_ZONE_OPEN:
1366
+ return sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_OPEN_ZONE, false);
1367
+ case REQ_OP_ZONE_CLOSE:
1368
+ return sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_CLOSE_ZONE, false);
1369
+ case REQ_OP_ZONE_FINISH:
1370
+ return sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_FINISH_ZONE, false);
12911371 default:
12921372 WARN_ON_ONCE(1);
1293
- return BLKPREP_KILL;
1373
+ return BLK_STS_NOTSUPP;
12941374 }
12951375 }
12961376
....@@ -1308,6 +1388,22 @@
13081388 SCpnt->cmd_len = 0;
13091389 mempool_free(cmnd, sd_cdb_pool);
13101390 }
1391
+}
1392
+
1393
+static bool sd_need_revalidate(struct block_device *bdev,
1394
+ struct scsi_disk *sdkp)
1395
+{
1396
+ if (sdkp->device->removable || sdkp->write_prot) {
1397
+ if (bdev_check_media_change(bdev))
1398
+ return true;
1399
+ }
1400
+
1401
+ /*
1402
+ * Force a full rescan after ioctl(BLKRRPART). While the disk state has
1403
+ * nothing to do with partitions, BLKRRPART is used to force a full
1404
+ * revalidate after things like a format for historical reasons.
1405
+ */
1406
+ return test_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
13111407 }
13121408
13131409 /**
....@@ -1346,8 +1442,8 @@
13461442 if (!scsi_block_when_processing_errors(sdev))
13471443 goto error_out;
13481444
1349
- if (sdev->removable || sdkp->write_prot)
1350
- check_disk_change(bdev);
1445
+ if (sd_need_revalidate(bdev, sdkp))
1446
+ sd_revalidate_disk(bdev->bd_disk);
13511447
13521448 /*
13531449 * If the drive is empty, just let the open fail.
....@@ -1444,7 +1540,7 @@
14441540 * @bdev: target block device
14451541 * @mode: FMODE_* mask
14461542 * @cmd: ioctl command number
1447
- * @arg: this is third argument given to ioctl(2) system call.
1543
+ * @p: this is third argument given to ioctl(2) system call.
14481544 * Often contains a pointer.
14491545 *
14501546 * Returns 0 if successful (some ioctls return positive numbers on
....@@ -1453,13 +1549,12 @@
14531549 * Note: most ioctls are forward onto the block subsystem or further
14541550 * down in the scsi subsystem.
14551551 **/
1456
-static int sd_ioctl(struct block_device *bdev, fmode_t mode,
1457
- unsigned int cmd, unsigned long arg)
1552
+static int sd_ioctl_common(struct block_device *bdev, fmode_t mode,
1553
+ unsigned int cmd, void __user *p)
14581554 {
14591555 struct gendisk *disk = bdev->bd_disk;
14601556 struct scsi_disk *sdkp = scsi_disk(disk);
14611557 struct scsi_device *sdp = sdkp->device;
1462
- void __user *p = (void __user *)arg;
14631558 int error;
14641559
14651560 SCSI_LOG_IOCTL(1, sd_printk(KERN_INFO, sdkp, "sd_ioctl: disk=%s, "
....@@ -1495,9 +1590,6 @@
14951590 break;
14961591 default:
14971592 error = scsi_cmd_blk_ioctl(bdev, mode, cmd, p);
1498
- if (error != -ENOTTY)
1499
- break;
1500
- error = scsi_ioctl(sdp, cmd, p);
15011593 break;
15021594 }
15031595 out:
....@@ -1578,7 +1670,7 @@
15781670 if (scsi_block_when_processing_errors(sdp)) {
15791671 struct scsi_sense_hdr sshdr = { 0, };
15801672
1581
- retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
1673
+ retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, sdkp->max_retries,
15821674 &sshdr);
15831675
15841676 /* failed to execute TUR, assume media not present */
....@@ -1635,7 +1727,7 @@
16351727 * flush everything.
16361728 */
16371729 res = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, sshdr,
1638
- timeout, SD_MAX_RETRIES, 0, RQF_PM, NULL);
1730
+ timeout, sdkp->max_retries, 0, RQF_PM, NULL);
16391731 if (res == 0)
16401732 break;
16411733 }
....@@ -1675,43 +1767,37 @@
16751767 static void sd_rescan(struct device *dev)
16761768 {
16771769 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1770
+ int ret;
16781771
1679
- revalidate_disk(sdkp->disk);
1772
+ ret = sd_revalidate_disk(sdkp->disk);
1773
+ revalidate_disk_size(sdkp->disk, ret == 0);
16801774 }
16811775
1776
+static int sd_ioctl(struct block_device *bdev, fmode_t mode,
1777
+ unsigned int cmd, unsigned long arg)
1778
+{
1779
+ void __user *p = (void __user *)arg;
1780
+ int ret;
1781
+
1782
+ ret = sd_ioctl_common(bdev, mode, cmd, p);
1783
+ if (ret != -ENOTTY)
1784
+ return ret;
1785
+
1786
+ return scsi_ioctl(scsi_disk(bdev->bd_disk)->device, cmd, p);
1787
+}
16821788
16831789 #ifdef CONFIG_COMPAT
1684
-/*
1685
- * This gets directly called from VFS. When the ioctl
1686
- * is not recognized we go back to the other translation paths.
1687
- */
16881790 static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
16891791 unsigned int cmd, unsigned long arg)
16901792 {
1691
- struct gendisk *disk = bdev->bd_disk;
1692
- struct scsi_disk *sdkp = scsi_disk(disk);
1693
- struct scsi_device *sdev = sdkp->device;
16941793 void __user *p = compat_ptr(arg);
1695
- int error;
1794
+ int ret;
16961795
1697
- error = scsi_verify_blk_ioctl(bdev, cmd);
1698
- if (error < 0)
1699
- return error;
1796
+ ret = sd_ioctl_common(bdev, mode, cmd, p);
1797
+ if (ret != -ENOTTY)
1798
+ return ret;
17001799
1701
- error = scsi_ioctl_block_when_processing_errors(sdev, cmd,
1702
- (mode & FMODE_NDELAY) != 0);
1703
- if (error)
1704
- return error;
1705
-
1706
- if (is_sed_ioctl(cmd))
1707
- return sed_ioctl(sdkp->opal_dev, cmd, p);
1708
-
1709
- /*
1710
- * Let the static ioctl translation table take care of it.
1711
- */
1712
- if (!sdev->host->hostt->compat_ioctl)
1713
- return -ENOIOCTLCMD;
1714
- return sdev->host->hostt->compat_ioctl(sdev, cmd, p);
1800
+ return scsi_compat_ioctl(scsi_disk(bdev->bd_disk)->device, cmd, p);
17151801 }
17161802 #endif
17171803
....@@ -1738,7 +1824,8 @@
17381824 static int sd_pr_command(struct block_device *bdev, u8 sa,
17391825 u64 key, u64 sa_key, u8 type, u8 flags)
17401826 {
1741
- struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
1827
+ struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
1828
+ struct scsi_device *sdev = sdkp->device;
17421829 struct scsi_sense_hdr sshdr;
17431830 int result;
17441831 u8 cmd[16] = { 0, };
....@@ -1754,7 +1841,7 @@
17541841 data[20] = flags;
17551842
17561843 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, &data, sizeof(data),
1757
- &sshdr, SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1844
+ &sshdr, SD_TIMEOUT, sdkp->max_retries, NULL);
17581845
17591846 if (driver_byte(result) == DRIVER_SENSE &&
17601847 scsi_sense_valid(&sshdr)) {
....@@ -1818,8 +1905,8 @@
18181905 .compat_ioctl = sd_compat_ioctl,
18191906 #endif
18201907 .check_events = sd_check_events,
1821
- .revalidate_disk = sd_revalidate_disk,
18221908 .unlock_native_capacity = sd_unlock_native_capacity,
1909
+ .report_zones = sd_zbc_report_zones,
18231910 .pr_ops = &sd_pr_ops,
18241911 };
18251912
....@@ -1963,22 +2050,12 @@
19632050 case REQ_OP_WRITE_ZEROES:
19642051 case REQ_OP_WRITE_SAME:
19652052 case REQ_OP_ZONE_RESET:
2053
+ case REQ_OP_ZONE_RESET_ALL:
2054
+ case REQ_OP_ZONE_OPEN:
2055
+ case REQ_OP_ZONE_CLOSE:
2056
+ case REQ_OP_ZONE_FINISH:
19662057 if (!result) {
19672058 good_bytes = blk_rq_bytes(req);
1968
- scsi_set_resid(SCpnt, 0);
1969
- } else {
1970
- good_bytes = 0;
1971
- scsi_set_resid(SCpnt, blk_rq_bytes(req));
1972
- }
1973
- break;
1974
- case REQ_OP_ZONE_REPORT:
1975
- /* To avoid that the block layer performs an incorrect
1976
- * bio_advance() call and restart of the remainder of
1977
- * incomplete report zone BIOs, always indicate a full
1978
- * completion of REQ_OP_ZONE_REPORT.
1979
- */
1980
- if (!result) {
1981
- good_bytes = scsi_bufflen(SCpnt);
19822059 scsi_set_resid(SCpnt, 0);
19832060 } else {
19842061 good_bytes = 0;
....@@ -1996,6 +2073,7 @@
19962073 sd_printk(KERN_INFO, sdkp,
19972074 "Unaligned partial completion (resid=%u, sector_sz=%u)\n",
19982075 resid, sector_size);
2076
+ scsi_print_command(SCpnt);
19992077 resid = min(scsi_bufflen(SCpnt),
20002078 round_up(resid, sector_size));
20012079 scsi_set_resid(SCpnt, resid);
....@@ -2063,16 +2141,11 @@
20632141
20642142 out:
20652143 if (sd_is_zoned(sdkp))
2066
- sd_zbc_complete(SCpnt, good_bytes, &sshdr);
2144
+ good_bytes = sd_zbc_complete(SCpnt, good_bytes, &sshdr);
20672145
20682146 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
20692147 "sd_done: completed %d of %d bytes\n",
20702148 good_bytes, scsi_bufflen(SCpnt)));
2071
-
2072
- if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt) &&
2073
- good_bytes)
2074
- t10_pi_complete(SCpnt->request, sdkp->protection_type,
2075
- good_bytes / scsi_prot_interval(SCpnt));
20762149
20772150 return good_bytes;
20782151 }
....@@ -2104,7 +2177,7 @@
21042177 the_result = scsi_execute_req(sdkp->device, cmd,
21052178 DMA_NONE, NULL, 0,
21062179 &sshdr, SD_TIMEOUT,
2107
- SD_MAX_RETRIES, NULL);
2180
+ sdkp->max_retries, NULL);
21082181
21092182 /*
21102183 * If the drive has indicated to us that it
....@@ -2160,7 +2233,7 @@
21602233 cmd[4] |= 1 << 4;
21612234 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
21622235 NULL, 0, &sshdr,
2163
- SD_TIMEOUT, SD_MAX_RETRIES,
2236
+ SD_TIMEOUT, sdkp->max_retries,
21642237 NULL);
21652238 spintime_expire = jiffies + 100 * HZ;
21662239 spintime = 1;
....@@ -2278,22 +2351,6 @@
22782351
22792352 #define READ_CAPACITY_RETRIES_ON_RESET 10
22802353
2281
-/*
2282
- * Ensure that we don't overflow sector_t when CONFIG_LBDAF is not set
2283
- * and the reported logical block size is bigger than 512 bytes. Note
2284
- * that last_sector is a u64 and therefore logical_to_sectors() is not
2285
- * applicable.
2286
- */
2287
-static bool sd_addressable_capacity(u64 lba, unsigned int sector_size)
2288
-{
2289
- u64 last_sector = (lba + 1ULL) << (ilog2(sector_size) - 9);
2290
-
2291
- if (sizeof(sector_t) == 4 && last_sector > U32_MAX)
2292
- return false;
2293
-
2294
- return true;
2295
-}
2296
-
22972354 static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
22982355 unsigned char *buffer)
22992356 {
....@@ -2318,7 +2375,7 @@
23182375
23192376 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
23202377 buffer, RC16_LEN, &sshdr,
2321
- SD_TIMEOUT, SD_MAX_RETRIES, NULL);
2378
+ SD_TIMEOUT, sdkp->max_retries, NULL);
23222379
23232380 if (media_not_present(sdkp, &sshdr))
23242381 return -ENODEV;
....@@ -2357,14 +2414,6 @@
23572414 if (sd_read_protection_type(sdkp, buffer) < 0) {
23582415 sdkp->capacity = 0;
23592416 return -ENODEV;
2360
- }
2361
-
2362
- if (!sd_addressable_capacity(lba, sector_size)) {
2363
- sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
2364
- "kernel compiled with support for large block "
2365
- "devices.\n");
2366
- sdkp->capacity = 0;
2367
- return -EOVERFLOW;
23682417 }
23692418
23702419 /* Logical blocks per physical block exponent */
....@@ -2411,7 +2460,7 @@
24112460
24122461 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
24132462 buffer, 8, &sshdr,
2414
- SD_TIMEOUT, SD_MAX_RETRIES, NULL);
2463
+ SD_TIMEOUT, sdkp->max_retries, NULL);
24152464
24162465 if (media_not_present(sdkp, &sshdr))
24172466 return -ENODEV;
....@@ -2446,14 +2495,6 @@
24462495 sdkp->capacity = 0;
24472496 sdkp->physical_block_size = sector_size;
24482497 return sector_size;
2449
- }
2450
-
2451
- if (!sd_addressable_capacity(lba, sector_size)) {
2452
- sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
2453
- "kernel compiled with support for large block "
2454
- "devices.\n");
2455
- sdkp->capacity = 0;
2456
- return -EOVERFLOW;
24572498 }
24582499
24592500 sdkp->capacity = lba + 1;
....@@ -2583,35 +2624,33 @@
25832624 int sector_size = sdkp->device->sector_size;
25842625 char cap_str_2[10], cap_str_10[10];
25852626
2627
+ if (!sdkp->first_scan && old_capacity == sdkp->capacity)
2628
+ return;
2629
+
25862630 string_get_size(sdkp->capacity, sector_size,
25872631 STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
25882632 string_get_size(sdkp->capacity, sector_size,
2589
- STRING_UNITS_10, cap_str_10,
2590
- sizeof(cap_str_10));
2633
+ STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
25912634
2592
- if (sdkp->first_scan || old_capacity != sdkp->capacity) {
2635
+ sd_printk(KERN_NOTICE, sdkp,
2636
+ "%llu %d-byte logical blocks: (%s/%s)\n",
2637
+ (unsigned long long)sdkp->capacity,
2638
+ sector_size, cap_str_10, cap_str_2);
2639
+
2640
+ if (sdkp->physical_block_size != sector_size)
25932641 sd_printk(KERN_NOTICE, sdkp,
2594
- "%llu %d-byte logical blocks: (%s/%s)\n",
2595
- (unsigned long long)sdkp->capacity,
2596
- sector_size, cap_str_10, cap_str_2);
2597
-
2598
- if (sdkp->physical_block_size != sector_size)
2599
- sd_printk(KERN_NOTICE, sdkp,
2600
- "%u-byte physical blocks\n",
2601
- sdkp->physical_block_size);
2602
-
2603
- sd_zbc_print_zones(sdkp);
2604
- }
2642
+ "%u-byte physical blocks\n",
2643
+ sdkp->physical_block_size);
26052644 }
26062645
26072646 /* called with buffer of length 512 */
26082647 static inline int
2609
-sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
2648
+sd_do_mode_sense(struct scsi_disk *sdkp, int dbd, int modepage,
26102649 unsigned char *buffer, int len, struct scsi_mode_data *data,
26112650 struct scsi_sense_hdr *sshdr)
26122651 {
2613
- return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
2614
- SD_TIMEOUT, SD_MAX_RETRIES, data,
2652
+ return scsi_mode_sense(sdkp->device, dbd, modepage, buffer, len,
2653
+ SD_TIMEOUT, sdkp->max_retries, data,
26152654 sshdr);
26162655 }
26172656
....@@ -2634,14 +2673,14 @@
26342673 }
26352674
26362675 if (sdp->use_192_bytes_for_3f) {
2637
- res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
2676
+ res = sd_do_mode_sense(sdkp, 0, 0x3F, buffer, 192, &data, NULL);
26382677 } else {
26392678 /*
26402679 * First attempt: ask for all pages (0x3F), but only 4 bytes.
26412680 * We have to start carefully: some devices hang if we ask
26422681 * for more than is available.
26432682 */
2644
- res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
2683
+ res = sd_do_mode_sense(sdkp, 0, 0x3F, buffer, 4, &data, NULL);
26452684
26462685 /*
26472686 * Second attempt: ask for page 0 When only page 0 is
....@@ -2649,18 +2688,18 @@
26492688 * 5: Illegal Request, Sense Code 24: Invalid field in
26502689 * CDB.
26512690 */
2652
- if (!scsi_status_is_good(res))
2653
- res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
2691
+ if (res < 0)
2692
+ res = sd_do_mode_sense(sdkp, 0, 0, buffer, 4, &data, NULL);
26542693
26552694 /*
26562695 * Third attempt: ask 255 bytes, as we did earlier.
26572696 */
2658
- if (!scsi_status_is_good(res))
2659
- res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
2697
+ if (res < 0)
2698
+ res = sd_do_mode_sense(sdkp, 0, 0x3F, buffer, 255,
26602699 &data, NULL);
26612700 }
26622701
2663
- if (!scsi_status_is_good(res)) {
2702
+ if (res < 0) {
26642703 sd_first_printk(KERN_WARNING, sdkp,
26652704 "Test WP failed, assume Write Enabled\n");
26662705 } else {
....@@ -2683,7 +2722,6 @@
26832722 {
26842723 int len = 0, res;
26852724 struct scsi_device *sdp = sdkp->device;
2686
- struct Scsi_Host *host = sdp->host;
26872725
26882726 int dbd;
26892727 int modepage;
....@@ -2715,17 +2753,14 @@
27152753 dbd = 8;
27162754 } else {
27172755 modepage = 8;
2718
- if (host->set_dbd_for_caching)
2719
- dbd = 8;
2720
- else
2721
- dbd = 0;
2756
+ dbd = 0;
27222757 }
27232758
27242759 /* cautiously ask */
2725
- res = sd_do_mode_sense(sdp, dbd, modepage, buffer, first_len,
2760
+ res = sd_do_mode_sense(sdkp, dbd, modepage, buffer, first_len,
27262761 &data, &sshdr);
27272762
2728
- if (!scsi_status_is_good(res))
2763
+ if (res < 0)
27292764 goto bad_sense;
27302765
27312766 if (!data.header_length) {
....@@ -2754,10 +2789,10 @@
27542789
27552790 /* Get the data */
27562791 if (len > first_len)
2757
- res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len,
2792
+ res = sd_do_mode_sense(sdkp, dbd, modepage, buffer, len,
27582793 &data, &sshdr);
27592794
2760
- if (scsi_status_is_good(res)) {
2795
+ if (!res) {
27612796 int offset = data.header_length + data.block_descriptor_length;
27622797
27632798 while (offset < len) {
....@@ -2873,9 +2908,9 @@
28732908 return;
28742909
28752910 res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
2876
- SD_MAX_RETRIES, &data, &sshdr);
2911
+ sdkp->max_retries, &data, &sshdr);
28772912
2878
- if (!scsi_status_is_good(res) || !data.header_length ||
2913
+ if (res < 0 || !data.header_length ||
28792914 data.length < 6) {
28802915 sd_first_printk(KERN_WARNING, sdkp,
28812916 "getting Control mode page failed, assume no ATO\n");
....@@ -2992,22 +3027,32 @@
29923027
29933028 if (sdkp->device->type == TYPE_ZBC) {
29943029 /* Host-managed */
2995
- q->limits.zoned = BLK_ZONED_HM;
3030
+ blk_queue_set_zoned(sdkp->disk, BLK_ZONED_HM);
29963031 } else {
29973032 sdkp->zoned = (buffer[8] >> 4) & 3;
2998
- if (sdkp->zoned == 1)
3033
+ if (sdkp->zoned == 1) {
29993034 /* Host-aware */
3000
- q->limits.zoned = BLK_ZONED_HA;
3001
- else
3002
- /*
3003
- * Treat drive-managed devices as
3004
- * regular block devices.
3005
- */
3006
- q->limits.zoned = BLK_ZONED_NONE;
3035
+ blk_queue_set_zoned(sdkp->disk, BLK_ZONED_HA);
3036
+ } else {
3037
+ /* Regular disk or drive managed disk */
3038
+ blk_queue_set_zoned(sdkp->disk, BLK_ZONED_NONE);
3039
+ }
30073040 }
3008
- if (blk_queue_is_zoned(q) && sdkp->first_scan)
3041
+
3042
+ if (!sdkp->first_scan)
3043
+ goto out;
3044
+
3045
+ if (blk_queue_is_zoned(q)) {
30093046 sd_printk(KERN_NOTICE, sdkp, "Host-%s zoned block device\n",
30103047 q->limits.zoned == BLK_ZONED_HM ? "managed" : "aware");
3048
+ } else {
3049
+ if (sdkp->zoned == 1)
3050
+ sd_printk(KERN_NOTICE, sdkp,
3051
+ "Host-aware SMR disk used as regular disk\n");
3052
+ else if (sdkp->zoned == 2)
3053
+ sd_printk(KERN_NOTICE, sdkp,
3054
+ "Drive-managed SMR disk\n");
3055
+ }
30113056
30123057 out:
30133058 kfree(buffer);
....@@ -3237,9 +3282,18 @@
32373282
32383283 sdkp->first_scan = 0;
32393284
3240
- set_capacity(disk, logical_to_sectors(sdp, sdkp->capacity));
3285
+ set_capacity_revalidate_and_notify(disk,
3286
+ logical_to_sectors(sdp, sdkp->capacity), false);
32413287 sd_config_write_same(sdkp);
32423288 kfree(buffer);
3289
+
3290
+ /*
3291
+ * For a zoned drive, revalidating the zones can be done only once
3292
+ * the gendisk capacity is set. So if this fails, set back the gendisk
3293
+ * capacity to 0.
3294
+ */
3295
+ if (sd_zbc_revalidate_zones(sdkp))
3296
+ set_capacity_revalidate_and_notify(disk, 0, false);
32433297
32443298 out:
32453299 return 0;
....@@ -3311,72 +3365,6 @@
33113365 return 0;
33123366 }
33133367
3314
-/*
3315
- * The asynchronous part of sd_probe
3316
- */
3317
-static void sd_probe_async(void *data, async_cookie_t cookie)
3318
-{
3319
- struct scsi_disk *sdkp = data;
3320
- struct scsi_device *sdp;
3321
- struct gendisk *gd;
3322
- u32 index;
3323
- struct device *dev;
3324
-
3325
- sdp = sdkp->device;
3326
- gd = sdkp->disk;
3327
- index = sdkp->index;
3328
- dev = &sdp->sdev_gendev;
3329
-
3330
- gd->major = sd_major((index & 0xf0) >> 4);
3331
- gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
3332
-
3333
- gd->fops = &sd_fops;
3334
- gd->private_data = &sdkp->driver;
3335
- gd->queue = sdkp->device->request_queue;
3336
-
3337
- /* defaults, until the device tells us otherwise */
3338
- sdp->sector_size = 512;
3339
- sdkp->capacity = 0;
3340
- sdkp->media_present = 1;
3341
- sdkp->write_prot = 0;
3342
- sdkp->cache_override = 0;
3343
- sdkp->WCE = 0;
3344
- sdkp->RCD = 0;
3345
- sdkp->ATO = 0;
3346
- sdkp->first_scan = 1;
3347
- sdkp->max_medium_access_timeouts = SD_MAX_MEDIUM_TIMEOUTS;
3348
-
3349
- sd_revalidate_disk(gd);
3350
-
3351
- gd->flags = GENHD_FL_EXT_DEVT;
3352
- if (sdp->removable) {
3353
- gd->flags |= GENHD_FL_REMOVABLE;
3354
- gd->events |= DISK_EVENT_MEDIA_CHANGE;
3355
- }
3356
-
3357
- blk_pm_runtime_init(sdp->request_queue, dev);
3358
- if (sdp->rpm_autosuspend) {
3359
- pm_runtime_set_autosuspend_delay(dev,
3360
- sdp->host->hostt->rpm_autosuspend_delay);
3361
- }
3362
- device_add_disk(dev, gd);
3363
- if (sdkp->capacity)
3364
- sd_dif_config_host(sdkp);
3365
-
3366
- sd_revalidate_disk(gd);
3367
-
3368
- if (sdkp->security) {
3369
- sdkp->opal_dev = init_opal_dev(sdp, &sd_sec_submit);
3370
- if (sdkp->opal_dev)
3371
- sd_printk(KERN_NOTICE, sdkp, "supports TCG Opal\n");
3372
- }
3373
-
3374
- sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
3375
- sdp->removable ? "removable " : "");
3376
- scsi_autopm_put_device(sdp);
3377
- put_device(&sdkp->dev);
3378
-}
3379
-
33803368 /**
33813369 * sd_probe - called during driver initialization and whenever a
33823370 * new scsi device is attached to the system. It is called once
....@@ -3443,6 +3431,7 @@
34433431 sdkp->driver = &sd_template;
34443432 sdkp->disk = gd;
34453433 sdkp->index = index;
3434
+ sdkp->max_retries = SD_MAX_RETRIES;
34463435 atomic_set(&sdkp->openers, 0);
34473436 atomic_set(&sdkp->device->ioerr_cnt, 0);
34483437
....@@ -3466,9 +3455,56 @@
34663455 }
34673456
34683457 dev_set_drvdata(dev, sdkp);
3458
+ device_init_wakeup(dev, true);
34693459
3470
- get_device(&sdkp->dev); /* prevent release before async_schedule */
3471
- async_schedule_domain(sd_probe_async, sdkp, &scsi_sd_probe_domain);
3460
+ gd->major = sd_major((index & 0xf0) >> 4);
3461
+ gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
3462
+
3463
+ gd->fops = &sd_fops;
3464
+ gd->private_data = &sdkp->driver;
3465
+ gd->queue = sdkp->device->request_queue;
3466
+
3467
+ /* defaults, until the device tells us otherwise */
3468
+ sdp->sector_size = 512;
3469
+ sdkp->capacity = 0;
3470
+ sdkp->media_present = 1;
3471
+ sdkp->write_prot = 0;
3472
+ sdkp->cache_override = 0;
3473
+ sdkp->WCE = 0;
3474
+ sdkp->RCD = 0;
3475
+ sdkp->ATO = 0;
3476
+ sdkp->first_scan = 1;
3477
+ sdkp->max_medium_access_timeouts = SD_MAX_MEDIUM_TIMEOUTS;
3478
+
3479
+ sd_revalidate_disk(gd);
3480
+
3481
+ gd->flags = GENHD_FL_EXT_DEVT;
3482
+ if (sdp->removable) {
3483
+ gd->flags |= GENHD_FL_REMOVABLE;
3484
+ gd->events |= DISK_EVENT_MEDIA_CHANGE;
3485
+ gd->event_flags = DISK_EVENT_FLAG_POLL | DISK_EVENT_FLAG_UEVENT;
3486
+ }
3487
+
3488
+ blk_pm_runtime_init(sdp->request_queue, dev);
3489
+ if (sdp->rpm_autosuspend) {
3490
+ pm_runtime_set_autosuspend_delay(dev,
3491
+ sdp->host->hostt->rpm_autosuspend_delay);
3492
+ }
3493
+ device_add_disk(dev, gd, NULL);
3494
+ if (sdkp->capacity)
3495
+ sd_dif_config_host(sdkp);
3496
+
3497
+ sd_revalidate_disk(gd);
3498
+
3499
+ if (sdkp->security) {
3500
+ sdkp->opal_dev = init_opal_dev(sdkp, &sd_sec_submit);
3501
+ if (sdkp->opal_dev)
3502
+ sd_printk(KERN_NOTICE, sdkp, "supports TCG Opal\n");
3503
+ }
3504
+
3505
+ sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
3506
+ sdp->removable ? "removable " : "");
3507
+ scsi_autopm_put_device(sdp);
34723508
34733509 return 0;
34743510
....@@ -3504,12 +3540,9 @@
35043540 scsi_autopm_get_device(sdkp->device);
35053541
35063542 async_synchronize_full_domain(&scsi_sd_pm_domain);
3507
- async_synchronize_full_domain(&scsi_sd_probe_domain);
35083543 device_del(&sdkp->dev);
35093544 del_gendisk(sdkp->disk);
35103545 sd_shutdown(dev);
3511
-
3512
- sd_zbc_remove(sdkp);
35133546
35143547 free_opal_dev(sdkp->opal_dev);
35153548
....@@ -3556,6 +3589,8 @@
35563589 put_disk(disk);
35573590 put_device(&sdkp->device->sdev_gendev);
35583591
3592
+ sd_zbc_release_disk(sdkp);
3593
+
35593594 kfree(sdkp);
35603595 }
35613596
....@@ -3576,7 +3611,7 @@
35763611 return -ENODEV;
35773612
35783613 res = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
3579
- SD_TIMEOUT, SD_MAX_RETRIES, 0, RQF_PM, NULL);
3614
+ SD_TIMEOUT, sdkp->max_retries, 0, RQF_PM, NULL);
35803615 if (res) {
35813616 sd_print_result(sdkp, "Start/Stop Unit failed", res);
35823617 if (driver_byte(res) == DRIVER_SENSE)
....@@ -3683,10 +3718,16 @@
36833718 if (!sdkp->device->manage_start_stop)
36843719 return 0;
36853720
3721
+ /* The wake-up process cannot allow the PM to enter sleep */
3722
+ pm_stay_awake(dev);
3723
+
36863724 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
36873725 ret = sd_start_stop_device(sdkp, 1);
36883726 if (!ret)
36893727 opal_unlock_from_suspend(sdkp->opal_dev);
3728
+
3729
+ pm_relax(dev);
3730
+
36903731 return ret;
36913732 }
36923733
....@@ -3789,15 +3830,13 @@
37893830 module_init(init_sd);
37903831 module_exit(exit_sd);
37913832
3792
-static void sd_print_sense_hdr(struct scsi_disk *sdkp,
3793
- struct scsi_sense_hdr *sshdr)
3833
+void sd_print_sense_hdr(struct scsi_disk *sdkp, struct scsi_sense_hdr *sshdr)
37943834 {
37953835 scsi_print_sense_hdr(sdkp->device,
37963836 sdkp->disk ? sdkp->disk->disk_name : NULL, sshdr);
37973837 }
37983838
3799
-static void sd_print_result(const struct scsi_disk *sdkp, const char *msg,
3800
- int result)
3839
+void sd_print_result(const struct scsi_disk *sdkp, const char *msg, int result)
38013840 {
38023841 const char *hb_string = scsi_hostbyte_string(result);
38033842 const char *db_string = scsi_driverbyte_string(result);
....@@ -3812,4 +3851,3 @@
38123851 "%s: Result: hostbyte=0x%02x driverbyte=0x%02x\n",
38133852 msg, host_byte(result), driver_byte(result));
38143853 }
3815
-