nvme: move OPAL setup from PCIe to core
authorChristoph Hellwig <hch@lst.de>
Tue, 8 Nov 2022 14:48:27 +0000 (15:48 +0100)
committerChristoph Hellwig <hch@lst.de>
Tue, 15 Nov 2022 09:55:53 +0000 (10:55 +0100)
Nothing about the TCG Opal support is PCIe transport specific, so move it
to the core code.  For this nvme_init_ctrl_finish grows a new
was_suspended argument that allows the transport driver to tell the OPAL
code if the controller came out of a suspend cycle.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Sagi Grimberg <sagi@grimberg.me>
Reviewed-by: Chaitanya Kulkarni <kch@nvidia.com>
Reviewed-by: James Smart <jsmart2021@gmail.com>
Tested-by Gerd Bayer <gbayer@linxu.ibm.com>

drivers/nvme/host/apple.c
drivers/nvme/host/core.c
drivers/nvme/host/fc.c
drivers/nvme/host/nvme.h
drivers/nvme/host/pci.c
drivers/nvme/host/rdma.c
drivers/nvme/host/tcp.c
drivers/nvme/target/loop.c

index 24e224c..a85349a 100644 (file)
@@ -1102,7 +1102,7 @@ static void apple_nvme_reset_work(struct work_struct *work)
                goto out;
        }
 
-       ret = nvme_init_ctrl_finish(&anv->ctrl);
+       ret = nvme_init_ctrl_finish(&anv->ctrl, false);
        if (ret)
                goto out;
 
index ce8314a..aedacf2 100644 (file)
@@ -2192,7 +2192,7 @@ const struct pr_ops nvme_pr_ops = {
 };
 
 #ifdef CONFIG_BLK_SED_OPAL
-int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
+static int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
                bool send)
 {
        struct nvme_ctrl *ctrl = data;
@@ -2209,7 +2209,23 @@ int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
        return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
                        NVME_QID_ANY, 1, 0);
 }
-EXPORT_SYMBOL_GPL(nvme_sec_submit);
+
+static void nvme_configure_opal(struct nvme_ctrl *ctrl, bool was_suspended)
+{
+       if (ctrl->oacs & NVME_CTRL_OACS_SEC_SUPP) {
+               if (!ctrl->opal_dev)
+                       ctrl->opal_dev = init_opal_dev(ctrl, &nvme_sec_submit);
+               else if (was_suspended)
+                       opal_unlock_from_suspend(ctrl->opal_dev);
+       } else {
+               free_opal_dev(ctrl->opal_dev);
+               ctrl->opal_dev = NULL;
+       }
+}
+#else
+static void nvme_configure_opal(struct nvme_ctrl *ctrl, bool was_suspended)
+{
+}
 #endif /* CONFIG_BLK_SED_OPAL */
 
 #ifdef CONFIG_BLK_DEV_ZONED
@@ -3242,7 +3258,7 @@ out_free:
  * register in our nvme_ctrl structure.  This should be called as soon as
  * the admin queue is fully up and running.
  */
-int nvme_init_ctrl_finish(struct nvme_ctrl *ctrl)
+int nvme_init_ctrl_finish(struct nvme_ctrl *ctrl, bool was_suspended)
 {
        int ret;
 
@@ -3273,6 +3289,8 @@ int nvme_init_ctrl_finish(struct nvme_ctrl *ctrl)
        if (ret < 0)
                return ret;
 
+       nvme_configure_opal(ctrl, was_suspended);
+
        if (!ctrl->identified && !nvme_discovery_ctrl(ctrl)) {
                /*
                 * Do not return errors unless we are in a controller reset,
@@ -5007,6 +5025,7 @@ static void nvme_free_ctrl(struct device *dev)
        nvme_auth_stop(ctrl);
        nvme_auth_free(ctrl);
        __free_page(ctrl->discard_page);
+       free_opal_dev(ctrl->opal_dev);
 
        if (subsys) {
                mutex_lock(&nvme_subsystems_lock);
index 2d3c548..1f9f407 100644 (file)
@@ -3107,7 +3107,7 @@ nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
 
        nvme_start_admin_queue(&ctrl->ctrl);
 
-       ret = nvme_init_ctrl_finish(&ctrl->ctrl);
+       ret = nvme_init_ctrl_finish(&ctrl->ctrl, false);
        if (ret || test_bit(ASSOC_FAILED, &ctrl->flags))
                goto out_disconnect_admin_queue;
 
index 16b34a4..306a120 100644 (file)
@@ -736,7 +736,7 @@ int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
 void nvme_uninit_ctrl(struct nvme_ctrl *ctrl);
 void nvme_start_ctrl(struct nvme_ctrl *ctrl);
 void nvme_stop_ctrl(struct nvme_ctrl *ctrl);
-int nvme_init_ctrl_finish(struct nvme_ctrl *ctrl);
+int nvme_init_ctrl_finish(struct nvme_ctrl *ctrl, bool was_suspended);
 int nvme_alloc_admin_tag_set(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set,
                const struct blk_mq_ops *ops, unsigned int flags,
                unsigned int cmd_size);
@@ -748,9 +748,6 @@ void nvme_remove_io_tag_set(struct nvme_ctrl *ctrl);
 
 void nvme_remove_namespaces(struct nvme_ctrl *ctrl);
 
-int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
-               bool send);
-
 void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
                volatile union nvme_result *res);
 
index 208c387..e4f084e 100644 (file)
@@ -2772,7 +2772,6 @@ static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
        nvme_free_tagset(dev);
        if (dev->ctrl.admin_q)
                blk_put_queue(dev->ctrl.admin_q);
-       free_opal_dev(dev->ctrl.opal_dev);
        mempool_destroy(dev->iod_mempool);
        put_device(dev->dev);
        kfree(dev->queues);
@@ -2866,21 +2865,10 @@ static void nvme_reset_work(struct work_struct *work)
         */
        dev->ctrl.max_integrity_segments = 1;
 
-       result = nvme_init_ctrl_finish(&dev->ctrl);
+       result = nvme_init_ctrl_finish(&dev->ctrl, was_suspend);
        if (result)
                goto out;
 
-       if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) {
-               if (!dev->ctrl.opal_dev)
-                       dev->ctrl.opal_dev =
-                               init_opal_dev(&dev->ctrl, &nvme_sec_submit);
-               else if (was_suspend)
-                       opal_unlock_from_suspend(dev->ctrl.opal_dev);
-       } else {
-               free_opal_dev(dev->ctrl.opal_dev);
-               dev->ctrl.opal_dev = NULL;
-       }
-
        if (dev->ctrl.oacs & NVME_CTRL_OACS_DBBUF_SUPP) {
                result = nvme_dbbuf_dma_alloc(dev);
                if (result)
index 6e079ab..ccd45e5 100644 (file)
@@ -871,7 +871,7 @@ static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
 
        nvme_start_admin_queue(&ctrl->ctrl);
 
-       error = nvme_init_ctrl_finish(&ctrl->ctrl);
+       error = nvme_init_ctrl_finish(&ctrl->ctrl, false);
        if (error)
                goto out_quiesce_queue;
 
index 1eed0fc..4f85846 100644 (file)
@@ -1949,7 +1949,7 @@ static int nvme_tcp_configure_admin_queue(struct nvme_ctrl *ctrl, bool new)
 
        nvme_start_admin_queue(ctrl);
 
-       error = nvme_init_ctrl_finish(ctrl);
+       error = nvme_init_ctrl_finish(ctrl, false);
        if (error)
                goto out_quiesce_queue;
 
index b45fe3a..893c50f 100644 (file)
@@ -377,7 +377,7 @@ static int nvme_loop_configure_admin_queue(struct nvme_loop_ctrl *ctrl)
 
        nvme_start_admin_queue(&ctrl->ctrl);
 
-       error = nvme_init_ctrl_finish(&ctrl->ctrl);
+       error = nvme_init_ctrl_finish(&ctrl->ctrl, false);
        if (error)
                goto out_cleanup_tagset;