[Lkw] [PATCH 3/6] nvme-pci: map the BAR and check the device is present

Emmanuel Arias eamanu at riseup.net
Fri Sep 15 18:18:38 EDT 2023


part 4

Signed-off-by: Emmanuel Arias <eamanu at riseup.net>
---
 drivers/block/nvme-cmb.c | 39 +++++++++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 4 deletions(-)

diff --git a/drivers/block/nvme-cmb.c b/drivers/block/nvme-cmb.c
index ea2993c06..442695690 100644
--- a/drivers/block/nvme-cmb.c
+++ b/drivers/block/nvme-cmb.c
@@ -2,36 +2,68 @@
 #include <linux/init.h>
 #include <linux/kernel.h>
 #include <linux/pci.h>
+#include <linux/nvme.h>
 
 MODULE_LICENSE("GPL");
 
+struct nvme_cmb_dev {
+	void __iomem *bar;
+};
+
 static int nvme_cmb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 {
+	struct nvme_cmb_dev *dev;
 	int error;
 
 	dev_info(&pdev->dev, "found NVMe device\n");
-	
+
+	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+	if (!dev)
+		return -ENOMEM;
+
 	pci_set_master(pdev);
 	error = pci_enable_device_mem(pdev);
 	if (error)
-		return error;
+		goto out_free_dev;
 	error = pci_request_mem_regions(pdev, "nvme-cmb");
 	if (error)
 		goto out_disable_device;
-		
+
+	dev->bar = pci_ioremap_bar(pdev, 0);
+	if (!dev->bar) {
+		error = -ENOMEM;
+		goto out_release_regions;
+	}
+
+	if (readl(dev->bar + NVME_REG_CSTS) == ~0) {
+		error = -EIO;
+		goto out_iounmap;
+	}
+
+	dev_set_drvdata(&pdev->dev, dev);
 	return 0;
 
+out_iounmap:
+	iounmap(dev->bar);
+out_release_regions:
+	pci_release_mem_regions(dev);
 out_disable_device:
 	pci_disable_device(pdev);
+out_free_dev:
+	kfree(dev);
 	return error;
 }
 
 static void nvme_cmb_remove(struct pci_dev *pdev)
 {
+	struct nvme_cmb_dev *dev = dev_get_drvdata(&pdev->dev);
+
 	dev_info(&pdev->dev, "unbinding NVMe device\n");
 
+	iounmap(dev->bar);
 	pci_release_mem_regions(pdev);
 	pci_disable_device(pdev);
+	kfree(dev);
 }
 
 static const struct pci_device_id nvme_cmb_id_table[] = {
@@ -46,7 +78,6 @@ static struct pci_driver nvme_cmb_driver = {
 	.remove = nvme_cmb_remove,
 };
 
-
 static int __init nvme_cmb_init(void)
 {
 	pr_info("loading nvme-cmb module\n");
-- 
2.39.2




More information about the LKW mailing list