@@ -72,6 +72,9 @@ static DEFINE_IDA(nvme_instance_ida);
7272static dev_t nvme_chr_devt ;
7373static struct class * nvme_class ;
7474
75+ static void nvme_ns_remove (struct nvme_ns * ns );
76+ static int nvme_revalidate_disk (struct gendisk * disk );
77+
7578static __le32 nvme_get_log_dw10 (u8 lid , size_t size )
7679{
7780 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)
992995 metadata , meta_len , io .slba , NULL , 0 );
993996}
994997
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+
9951072static int nvme_user_cmd (struct nvme_ctrl * ctrl , struct nvme_ns * ns ,
9961073 struct nvme_passthru_cmd __user * ucmd )
9971074{
9981075 struct nvme_passthru_cmd cmd ;
9991076 struct nvme_command c ;
10001077 unsigned timeout = 0 ;
1078+ u32 effects ;
10011079 int status ;
10021080
10031081 if (!capable (CAP_SYS_ADMIN ))
@@ -1023,10 +1101,13 @@ static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
10231101 if (cmd .timeout_ms )
10241102 timeout = msecs_to_jiffies (cmd .timeout_ms );
10251103
1104+ effects = nvme_passthru_start (ctrl , ns , cmd .opcode );
10261105 status = nvme_submit_user_cmd (ns ? ns -> queue : ctrl -> admin_q , & c ,
10271106 (void __user * )(uintptr_t )cmd .addr , cmd .data_len ,
10281107 (void __user * )(uintptr_t )cmd .metadata , cmd .metadata ,
10291108 0 , & cmd .result , timeout );
1109+ nvme_passthru_end (ctrl , effects );
1110+
10301111 if (status >= 0 ) {
10311112 if (put_user (cmd .result , & ucmd -> result ))
10321113 return - EFAULT ;
@@ -1759,6 +1840,25 @@ static int nvme_get_log(struct nvme_ctrl *ctrl, u8 log_page, void *log,
17591840 return nvme_submit_sync_cmd (ctrl -> admin_q , & c , log , size );
17601841}
17611842
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+
17621862/*
17631863 * Initialize the cached copies of the Identify data and various controller
17641864 * 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)
17941894 return - EIO ;
17951895 }
17961896
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+
17971903 nvme_init_subnqn (ctrl , id );
17981904
17991905 if (!ctrl -> identified ) {
@@ -2713,6 +2819,7 @@ static void nvme_free_ctrl(struct device *dev)
27132819
27142820 ida_simple_remove (& nvme_instance_ida , ctrl -> instance );
27152821 ida_destroy (& ctrl -> ns_ida );
2822+ kfree (ctrl -> effects );
27162823
27172824 ctrl -> ops -> free_ctrl (ctrl );
27182825}
0 commit comments