[Lkw] [PATCH] nvme-cmb: allocate a device index
Marco AR
marcoalrey at gmail.com
Fri Sep 15 18:54:21 EDT 2023
So far we've just added a 0 postfix to the device name unconditionally.
This does not work if there are multiple devices. Use the Linux
IDA data structure to allocate and index, and use that for the device
name.
root at localhost:~/linux# sh ./smake.sh
cp driver to block dir
make ko
CALL scripts/checksyscalls.sh
DESCEND objtool
INSTALL libsubcmd_headers
CC [M] drivers/block/nvme-cmb.o
MODPOST Module.symvers
LD [M] drivers/block/nvme-cmb.ko
done
root at localhost:~/linux# insmod drivers/block/nvme-cmb.ko
[ 780.160151] loading nvme-cmb module
[ 780.161242] nvme_cmb 0000:00:04.0: found NVMe device
[ 780.188920] nc0: p1
[ 780.189611] nvme_cmb 0000:00:04.0: added 16 MiB ramdisk
root at localhost:~/linux# rmmod nvme_cmb
[ 788.614129] unloading nvme-cmb module
[ 788.616552] nvme_cmb 0000:00:04.0: unbinding NVMe device
Signed-off-by: Marco AR <marcoalrey at gmail.com>
---
drivers/block/nvme-cmb.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/drivers/block/nvme-cmb.c b/drivers/block/nvme-cmb.c
index fb44e191fe5f..1b74364d8ff2 100644
--- a/drivers/block/nvme-cmb.c
+++ b/drivers/block/nvme-cmb.c
@@ -1,5 +1,6 @@
#include <linux/blkdev.h>
#include <linux/delay.h>
+#include <linux/idr.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
@@ -17,6 +18,7 @@ struct nvme_cmb_dev {
size_t buffer_size;
struct gendisk *disk;
+ int index;
struct nvme_command *admin_sqes;
dma_addr_t admin_sq_dma_addr;
@@ -28,6 +30,8 @@ struct nvme_cmb_dev {
#define QUEUE_SIZE 2
+static DEFINE_IDA(nvme_cmb_index_ida);
+
static void nvme_cmb_submit_bio(struct bio *bio)
{
struct nvme_cmb_dev *dev = bio->bi_bdev->bd_disk->private_data;
@@ -189,11 +193,15 @@ static int nvme_cmb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
dev->disk->private_data = dev;
dev->disk->fops = &nvme_cmb_ops;
set_capacity(dev->disk, SZ_16M >> SECTOR_SHIFT);
- snprintf(dev->disk->disk_name, DISK_NAME_LEN, "nc0");
+
+ error = ida_alloc(&nvme_cmb_index_ida, GFP_KERNEL);
+ if (error < 0)
+ goto out_put_disk;
+ snprintf(dev->disk->disk_name, DISK_NAME_LEN, "nc%d", error);
error = device_add_disk(&pdev->dev, dev->disk, NULL);
if (error)
- goto out_put_disk;
+ goto out_ida_free;
dev_set_drvdata(&pdev->dev, dev);
dev_info(&pdev->dev, "added %zu MiB ramdisk\n",
@@ -201,6 +209,9 @@ static int nvme_cmb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
return 0;
+out_ida_free:
+ ida_free(&nvme_cmb_index_ida, dev->index);
+
out_put_disk:
put_disk(dev->disk);
--
2.39.2
More information about the LKW
mailing list