diff mbox series

[RFC,04/11] nvmet: Add function to get nvmet_fabrics_ops from trtype

Message ID 20250313052222.178524-5-michael.christie@oracle.com (mailing list archive)
State New
Headers show
Series nvmet: Add NVMe target mdev/vfio driver | expand

Commit Message

Mike Christie March 13, 2025, 5:18 a.m. UTC
In the next patches we allow users to create static controllers if the
driver supports it. To get this info we need the nvmet_fabrics_ops
a little sooner then port enablement so this creates a function to go
from trtype to nvmet_fabrics_ops.

Signed-off-by: Mike Christie <michael.christie@oracle.com>
---
 drivers/nvme/target/core.c  | 41 +++++++++++++++++++++++--------------
 drivers/nvme/target/nvmet.h |  1 +
 2 files changed, 27 insertions(+), 15 deletions(-)

Comments

Damien Le Moal March 13, 2025, 9:03 a.m. UTC | #1
On 3/13/25 14:18, Mike Christie wrote:
> In the next patches we allow users to create static controllers if the
> driver supports it. To get this info we need the nvmet_fabrics_ops
> a little sooner then port enablement so this creates a function to go
> from trtype to nvmet_fabrics_ops.
> 
> Signed-off-by: Mike Christie <michael.christie@oracle.com>
> ---
>  drivers/nvme/target/core.c  | 41 +++++++++++++++++++++++--------------
>  drivers/nvme/target/nvmet.h |  1 +
>  2 files changed, 27 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
> index 4de534eafd89..06967c00e9a2 100644
> --- a/drivers/nvme/target/core.c
> +++ b/drivers/nvme/target/core.c
> @@ -306,6 +306,30 @@ void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops)
>  }
>  EXPORT_SYMBOL_GPL(nvmet_unregister_transport);
>  
> +const struct nvmet_fabrics_ops *nvmet_get_ops_by_transport(int trtype)
> +{
> +	const struct nvmet_fabrics_ops *ops;
> +
> +	lockdep_assert_held(&nvmet_config_sem);
> +
> +	ops = nvmet_transports[trtype];
> +	if (!ops) {
> +		up_write(&nvmet_config_sem);
> +		request_module("nvmet-transport-%d", trtype);
> +		down_write(&nvmet_config_sem);
> +		ops = nvmet_transports[trtype];
> +		if (!ops) {
> +			pr_err("transport type %d not supported\n", trtype);
> +			return NULL;
> +		}
> +	}
> +
> +	if (!try_module_get(ops->owner))
> +		return NULL;
> +
> +	return ops;
> +}
> +
>  void nvmet_port_del_ctrls(struct nvmet_port *port, struct nvmet_subsys *subsys)
>  {
>  	struct nvmet_ctrl *ctrl;
> @@ -325,22 +349,9 @@ int nvmet_enable_port(struct nvmet_port *port)
>  
>  	lockdep_assert_held(&nvmet_config_sem);
>  
> -	ops = nvmet_transports[port->disc_addr.trtype];
> -	if (!ops) {
> -		up_write(&nvmet_config_sem);
> -		request_module("nvmet-transport-%d", port->disc_addr.trtype);
> -		down_write(&nvmet_config_sem);
> -		ops = nvmet_transports[port->disc_addr.trtype];
> -		if (!ops) {
> -			pr_err("transport type %d not supported\n",
> -				port->disc_addr.trtype);
> -			return -EINVAL;
> -		}
> -	}
> -
> -	if (!try_module_get(ops->owner))
> +	ops = nvmet_get_ops_by_transport(port->disc_addr.trtype);
> +	if (!ops)
>  		return -EINVAL;
> -

whiteline change.

>  	/*
>  	 * If the user requested PI support and the transport isn't pi capable,
>  	 * don't enable the port.
> diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
> index ec3d10eb316a..052ea4a105fc 100644
> --- a/drivers/nvme/target/nvmet.h
> +++ b/drivers/nvme/target/nvmet.h
> @@ -622,6 +622,7 @@ void nvmet_port_send_ana_event(struct nvmet_port *port);
>  
>  int nvmet_register_transport(const struct nvmet_fabrics_ops *ops);
>  void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops);
> +const struct nvmet_fabrics_ops *nvmet_get_ops_by_transport(int trtype);
>  
>  void nvmet_port_del_ctrls(struct nvmet_port *port,
>  			  struct nvmet_subsys *subsys);
diff mbox series

Patch

diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
index 4de534eafd89..06967c00e9a2 100644
--- a/drivers/nvme/target/core.c
+++ b/drivers/nvme/target/core.c
@@ -306,6 +306,30 @@  void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops)
 }
 EXPORT_SYMBOL_GPL(nvmet_unregister_transport);
 
+const struct nvmet_fabrics_ops *nvmet_get_ops_by_transport(int trtype)
+{
+	const struct nvmet_fabrics_ops *ops;
+
+	lockdep_assert_held(&nvmet_config_sem);
+
+	ops = nvmet_transports[trtype];
+	if (!ops) {
+		up_write(&nvmet_config_sem);
+		request_module("nvmet-transport-%d", trtype);
+		down_write(&nvmet_config_sem);
+		ops = nvmet_transports[trtype];
+		if (!ops) {
+			pr_err("transport type %d not supported\n", trtype);
+			return NULL;
+		}
+	}
+
+	if (!try_module_get(ops->owner))
+		return NULL;
+
+	return ops;
+}
+
 void nvmet_port_del_ctrls(struct nvmet_port *port, struct nvmet_subsys *subsys)
 {
 	struct nvmet_ctrl *ctrl;
@@ -325,22 +349,9 @@  int nvmet_enable_port(struct nvmet_port *port)
 
 	lockdep_assert_held(&nvmet_config_sem);
 
-	ops = nvmet_transports[port->disc_addr.trtype];
-	if (!ops) {
-		up_write(&nvmet_config_sem);
-		request_module("nvmet-transport-%d", port->disc_addr.trtype);
-		down_write(&nvmet_config_sem);
-		ops = nvmet_transports[port->disc_addr.trtype];
-		if (!ops) {
-			pr_err("transport type %d not supported\n",
-				port->disc_addr.trtype);
-			return -EINVAL;
-		}
-	}
-
-	if (!try_module_get(ops->owner))
+	ops = nvmet_get_ops_by_transport(port->disc_addr.trtype);
+	if (!ops)
 		return -EINVAL;
-
 	/*
 	 * If the user requested PI support and the transport isn't pi capable,
 	 * don't enable the port.
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index ec3d10eb316a..052ea4a105fc 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -622,6 +622,7 @@  void nvmet_port_send_ana_event(struct nvmet_port *port);
 
 int nvmet_register_transport(const struct nvmet_fabrics_ops *ops);
 void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops);
+const struct nvmet_fabrics_ops *nvmet_get_ops_by_transport(int trtype);
 
 void nvmet_port_del_ctrls(struct nvmet_port *port,
 			  struct nvmet_subsys *subsys);