@@ -72,6 +72,9 @@ static DEFINE_IDA(nvme_instance_ida);
72
72
static dev_t nvme_chr_devt ;
73
73
static struct class * nvme_class ;
74
74
75
+ static void nvme_ns_remove (struct nvme_ns * ns );
76
+ static int nvme_revalidate_disk (struct gendisk * disk );
77
+
75
78
static __le32 nvme_get_log_dw10 (u8 lid , size_t size )
76
79
{
77
80
return cpu_to_le32 ((((size / 4 ) - 1 ) << 16 ) | lid );
@@ -992,12 +995,87 @@ static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
992
995
metadata , meta_len , io .slba , NULL , 0 );
993
996
}
994
997
998
+ static u32 nvme_known_admin_effects (u8 opcode )
999
+ {
1000
+ switch (opcode ) {
1001
+ case nvme_admin_format_nvm :
1002
+ return NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC |
1003
+ NVME_CMD_EFFECTS_CSE_MASK ;
1004
+ case nvme_admin_sanitize_nvm :
1005
+ return NVME_CMD_EFFECTS_CSE_MASK ;
1006
+ default :
1007
+ break ;
1008
+ }
1009
+ return 0 ;
1010
+ }
1011
+
1012
+ static u32 nvme_passthru_start (struct nvme_ctrl * ctrl , struct nvme_ns * ns ,
1013
+ u8 opcode )
1014
+ {
1015
+ u32 effects = 0 ;
1016
+
1017
+ if (ns ) {
1018
+ if (ctrl -> effects )
1019
+ effects = le32_to_cpu (ctrl -> effects -> iocs [opcode ]);
1020
+ if (effects & ~NVME_CMD_EFFECTS_CSUPP )
1021
+ dev_warn (ctrl -> device ,
1022
+ "IO command:%02x has unhandled effects:%08x\n" ,
1023
+ opcode , effects );
1024
+ return 0 ;
1025
+ }
1026
+
1027
+ if (ctrl -> effects )
1028
+ effects = le32_to_cpu (ctrl -> effects -> iocs [opcode ]);
1029
+ else
1030
+ effects = nvme_known_admin_effects (opcode );
1031
+
1032
+ /*
1033
+ * For simplicity, IO to all namespaces is quiesced even if the command
1034
+ * effects say only one namespace is affected.
1035
+ */
1036
+ if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK )) {
1037
+ nvme_start_freeze (ctrl );
1038
+ nvme_wait_freeze (ctrl );
1039
+ }
1040
+ return effects ;
1041
+ }
1042
+
1043
+ static void nvme_update_formats (struct nvme_ctrl * ctrl )
1044
+ {
1045
+ struct nvme_ns * ns ;
1046
+
1047
+ mutex_lock (& ctrl -> namespaces_mutex );
1048
+ list_for_each_entry (ns , & ctrl -> namespaces , list ) {
1049
+ if (ns -> disk && nvme_revalidate_disk (ns -> disk ))
1050
+ nvme_ns_remove (ns );
1051
+ }
1052
+ mutex_unlock (& ctrl -> namespaces_mutex );
1053
+ }
1054
+
1055
+ static void nvme_passthru_end (struct nvme_ctrl * ctrl , u32 effects )
1056
+ {
1057
+ /*
1058
+ * Revalidate LBA changes prior to unfreezing. This is necessary to
1059
+ * prevent memory corruption if a logical block size was changed by
1060
+ * this command.
1061
+ */
1062
+ if (effects & NVME_CMD_EFFECTS_LBCC )
1063
+ nvme_update_formats (ctrl );
1064
+ if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK ))
1065
+ nvme_unfreeze (ctrl );
1066
+ if (effects & NVME_CMD_EFFECTS_CCC )
1067
+ nvme_init_identify (ctrl );
1068
+ if (effects & (NVME_CMD_EFFECTS_NIC | NVME_CMD_EFFECTS_NCC ))
1069
+ nvme_queue_scan (ctrl );
1070
+ }
1071
+
995
1072
static int nvme_user_cmd (struct nvme_ctrl * ctrl , struct nvme_ns * ns ,
996
1073
struct nvme_passthru_cmd __user * ucmd )
997
1074
{
998
1075
struct nvme_passthru_cmd cmd ;
999
1076
struct nvme_command c ;
1000
1077
unsigned timeout = 0 ;
1078
+ u32 effects ;
1001
1079
int status ;
1002
1080
1003
1081
if (!capable (CAP_SYS_ADMIN ))
@@ -1023,10 +1101,13 @@ static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
1023
1101
if (cmd .timeout_ms )
1024
1102
timeout = msecs_to_jiffies (cmd .timeout_ms );
1025
1103
1104
+ effects = nvme_passthru_start (ctrl , ns , cmd .opcode );
1026
1105
status = nvme_submit_user_cmd (ns ? ns -> queue : ctrl -> admin_q , & c ,
1027
1106
(void __user * )(uintptr_t )cmd .addr , cmd .data_len ,
1028
1107
(void __user * )(uintptr_t )cmd .metadata , cmd .metadata ,
1029
1108
0 , & cmd .result , timeout );
1109
+ nvme_passthru_end (ctrl , effects );
1110
+
1030
1111
if (status >= 0 ) {
1031
1112
if (put_user (cmd .result , & ucmd -> result ))
1032
1113
return - EFAULT ;
@@ -1759,6 +1840,25 @@ static int nvme_get_log(struct nvme_ctrl *ctrl, u8 log_page, void *log,
1759
1840
return nvme_submit_sync_cmd (ctrl -> admin_q , & c , log , size );
1760
1841
}
1761
1842
1843
+ static int nvme_get_effects_log (struct nvme_ctrl * ctrl )
1844
+ {
1845
+ int ret ;
1846
+
1847
+ if (!ctrl -> effects )
1848
+ ctrl -> effects = kzalloc (sizeof (* ctrl -> effects ), GFP_KERNEL );
1849
+
1850
+ if (!ctrl -> effects )
1851
+ return 0 ;
1852
+
1853
+ ret = nvme_get_log (ctrl , NVME_LOG_CMD_EFFECTS , ctrl -> effects ,
1854
+ sizeof (* ctrl -> effects ));
1855
+ if (ret ) {
1856
+ kfree (ctrl -> effects );
1857
+ ctrl -> effects = NULL ;
1858
+ }
1859
+ return ret ;
1860
+ }
1861
+
1762
1862
/*
1763
1863
* Initialize the cached copies of the Identify data and various controller
1764
1864
* register in our nvme_ctrl structure. This should be called as soon as
@@ -1794,6 +1894,12 @@ int nvme_init_identify(struct nvme_ctrl *ctrl)
1794
1894
return - EIO ;
1795
1895
}
1796
1896
1897
+ if (id -> lpa & NVME_CTRL_LPA_CMD_EFFECTS_LOG ) {
1898
+ ret = nvme_get_effects_log (ctrl );
1899
+ if (ret < 0 )
1900
+ return ret ;
1901
+ }
1902
+
1797
1903
nvme_init_subnqn (ctrl , id );
1798
1904
1799
1905
if (!ctrl -> identified ) {
@@ -2713,6 +2819,7 @@ static void nvme_free_ctrl(struct device *dev)
2713
2819
2714
2820
ida_simple_remove (& nvme_instance_ida , ctrl -> instance );
2715
2821
ida_destroy (& ctrl -> ns_ida );
2822
+ kfree (ctrl -> effects );
2716
2823
2717
2824
ctrl -> ops -> free_ctrl (ctrl );
2718
2825
}
0 commit comments