const struct mlx5_eswitch_rep_ops *rep_ops[NUM_REP_TYPES];
        u8 inline_mode;
        atomic64_t num_flows;
+       u64 num_block_encap;
        enum devlink_eswitch_encap_mode encap;
        struct ida vport_metadata_ida;
        unsigned int host_number; /* ECPF supports one external host */
                                              struct mlx5_eswitch *slave_esw);
 int mlx5_eswitch_reload_reps(struct mlx5_eswitch *esw);
 
+bool mlx5_eswitch_block_encap(struct mlx5_core_dev *dev);
+void mlx5_eswitch_unblock_encap(struct mlx5_core_dev *dev);
+
 static inline int mlx5_eswitch_num_vfs(struct mlx5_eswitch *esw)
 {
        if (mlx5_esw_allowed(esw))
 {
        return esw->fdb_table.offloads.slow_fdb;
 }
+
 #else  /* CONFIG_MLX5_ESWITCH */
 /* eswitch API stubs */
 static inline int  mlx5_eswitch_init(struct mlx5_core_dev *dev) { return 0; }
 {
        return 0;
 }
+
+static inline bool mlx5_eswitch_block_encap(struct mlx5_core_dev *dev)
+{
+       return true;
+}
+
+static inline void mlx5_eswitch_unblock_encap(struct mlx5_core_dev *dev)
+{
+}
 #endif /* CONFIG_MLX5_ESWITCH */
 
 #endif /* __MLX5_ESWITCH_H__ */
 
        return err;
 }
 
+bool mlx5_eswitch_block_encap(struct mlx5_core_dev *dev)
+{
+       struct devlink *devlink = priv_to_devlink(dev);
+       struct mlx5_eswitch *esw;
+
+       devl_lock(devlink);
+       esw = mlx5_devlink_eswitch_get(devlink);
+       if (IS_ERR(esw)) {
+               devl_unlock(devlink);
+               /* Failure means no eswitch => not possible to change encap */
+               return true;
+       }
+
+       down_write(&esw->mode_lock);
+       if (esw->mode != MLX5_ESWITCH_LEGACY &&
+           esw->offloads.encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE) {
+               up_write(&esw->mode_lock);
+               devl_unlock(devlink);
+               return false;
+       }
+
+       esw->offloads.num_block_encap++;
+       up_write(&esw->mode_lock);
+       devl_unlock(devlink);
+       return true;
+}
+
+void mlx5_eswitch_unblock_encap(struct mlx5_core_dev *dev)
+{
+       struct devlink *devlink = priv_to_devlink(dev);
+       struct mlx5_eswitch *esw;
+
+       esw = mlx5_devlink_eswitch_get(devlink);
+       if (IS_ERR(esw))
+               return;
+
+       down_write(&esw->mode_lock);
+       esw->offloads.num_block_encap--;
+       up_write(&esw->mode_lock);
+}
+
 int mlx5_devlink_eswitch_encap_mode_set(struct devlink *devlink,
                                        enum devlink_eswitch_encap_mode encap,
                                        struct netlink_ext_ack *extack)
                goto unlock;
        }
 
+       if (esw->offloads.num_block_encap) {
+               NL_SET_ERR_MSG_MOD(extack,
+                                  "Can't set encapsulation when IPsec SA and/or policies are configured");
+               err = -EOPNOTSUPP;
+               goto unlock;
+       }
+
        esw_destroy_offloads_fdb_tables(esw);
 
        esw->offloads.encap = encap;