From: Dean Balandin <dbalan...@marvell.com>

This patch implements the create_ctrl skeleton, specifcially to
demonstrate how the claim_dev op will be used.

The driver scans the registered devices and calls the claim_dev op on
each of them, to find the first devices that matches the connection
params. Once the correct devices is found (claim_dev returns true), we
raise the refcnt of that device and return that device as the device to
be used for ctrl currently being created.

Signed-off-by: Dean Balandin <dbalan...@marvell.com>
Signed-off-by: Michal Kalderon <mkalde...@marvell.com>
Signed-off-by: Ariel Elior <ael...@marvell.com>
Signed-off-by: Shai Malin <sma...@marvell.com>
---
 drivers/nvme/host/tcp-offload.c | 82 +++++++++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)

diff --git a/drivers/nvme/host/tcp-offload.c b/drivers/nvme/host/tcp-offload.c
index ee3800250e47..49e11638b4fd 100644
--- a/drivers/nvme/host/tcp-offload.c
+++ b/drivers/nvme/host/tcp-offload.c
@@ -13,6 +13,12 @@
 static LIST_HEAD(nvme_tcp_ofld_devices);
 static DECLARE_RWSEM(nvme_tcp_ofld_devices_rwsem);
 
+static inline struct nvme_tcp_ofld_ctrl *
+to_tcp_ofld_ctrl(struct nvme_ctrl *nctrl)
+{
+       return container_of(nctrl, struct nvme_tcp_ofld_ctrl, nctrl);
+}
+
 /**
  * nvme_tcp_ofld_register_dev() - NVMeTCP Offload Library registration
  * function.
@@ -96,6 +102,81 @@ nvme_tcp_ofld_req_done(struct nvme_tcp_ofld_req *req,
        /* Placeholder - complete request with/without error */
 }
 
+struct nvme_tcp_ofld_dev *
+nvme_tcp_ofld_lookup_dev(struct nvme_tcp_ofld_ctrl *ctrl)
+{
+       struct nvme_tcp_ofld_dev *dev;
+
+       down_read(&nvme_tcp_ofld_devices_rwsem);
+       list_for_each_entry(dev, &nvme_tcp_ofld_devices, entry) {
+               if (dev->ops->claim_dev(dev, &ctrl->conn_params)) {
+                       /* Increase driver refcnt */
+                       if (!try_module_get(dev->ops->module)) {
+                               pr_err("try_module_get failed\n");
+                               dev = NULL;
+                       }
+
+                       goto out;
+               }
+       }
+
+       dev = NULL;
+out:
+       up_read(&nvme_tcp_ofld_devices_rwsem);
+
+       return dev;
+}
+
+static int nvme_tcp_ofld_setup_ctrl(struct nvme_ctrl *nctrl, bool new)
+{
+       /* Placeholder - validates inputs and creates admin and IO queues */
+
+       return 0;
+}
+
+static struct nvme_ctrl *
+nvme_tcp_ofld_create_ctrl(struct device *ndev, struct nvmf_ctrl_options *opts)
+{
+       struct nvme_tcp_ofld_ctrl *ctrl;
+       struct nvme_tcp_ofld_dev *dev;
+       struct nvme_ctrl *nctrl;
+       int rc = 0;
+
+       ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
+       if (!ctrl)
+               return ERR_PTR(-ENOMEM);
+
+       /* Init nvme_tcp_ofld_ctrl and nvme_ctrl params based on received opts 
*/
+
+       /* Find device that can reach the dest addr */
+       dev = nvme_tcp_ofld_lookup_dev(ctrl);
+       if (!dev) {
+               pr_info("no device found for addr %s:%s.\n",
+                       opts->traddr, opts->trsvcid);
+               rc = -EINVAL;
+               goto out_free_ctrl;
+       }
+
+       ctrl->dev = dev;
+
+       /* Init queues */
+
+       /* Call nvme_init_ctrl */
+
+       rc = nvme_tcp_ofld_setup_ctrl(nctrl, true);
+       if (rc)
+               goto out_module_put;
+
+       return nctrl;
+
+out_module_put:
+       module_put(dev->ops->module);
+out_free_ctrl:
+       kfree(ctrl);
+
+       return ERR_PTR(rc);
+}
+
 static struct nvmf_transport_ops nvme_tcp_ofld_transport = {
        .name           = "tcp_offload",
        .module         = THIS_MODULE,
@@ -105,6 +186,7 @@ static struct nvmf_transport_ops nvme_tcp_ofld_transport = {
                          NVMF_OPT_CTRL_LOSS_TMO | NVMF_OPT_RECONNECT_DELAY |
                          NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST |
                          NVMF_OPT_NR_POLL_QUEUES | NVMF_OPT_TOS,
+       .create_ctrl    = nvme_tcp_ofld_create_ctrl,
 };
 
 static int __init nvme_tcp_ofld_init_module(void)
-- 
2.22.0

Reply via email to