Skip to contentSkip to conversation

NVMe: Add support for SG_IO

PATCH New Vishal Verma 2 participants 5 messages Mar 5, 2013 – Mar 28, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover NVMe: Add support for SG_IO

These patches allow the nvme driver to handle the SG_IO ioctl. Applications may send any supported SCSI Command through the SG_IO framework. The supported command list as well as the translation reference is available on www.nvmexpress.org

Vishal Verma (3):
NVMe: Rename nvme.c to nvme-core.c
NVMe: Move dev, ns, iod structs to nvme.c
NVMe: Add nvme-scsi.c

 drivers/block/Makefile    |    1 +
 drivers/block/nvme-core.c | 1760 +++++++++++++++++++++++++++
 drivers/block/nvme-scsi.c | 2941 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/block/nvme.c      | 1808 ----------------------------
 include/linux/nvme.h      |  107 ++
 5 files changed, 4809 insertions(+), 1808 deletions(-)
 create mode 100644 drivers/block/nvme-core.c
 create mode 100644 drivers/block/nvme-scsi.c
 delete mode 100644 drivers/block/nvme.c
#1 lore raw

PATCH v3 1/3 NVMe: Rename nvme.c to nvme-core.c

In preparation for adding nvme-scsi.c
It is preferable to retain the module name 'nvme'

Signed-off-by Vishal Verma <vishal.l.verma at intel.com>
---
 drivers/block/Makefile    |    1 +
 drivers/block/nvme-core.c | 1808 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/block/nvme.c      | 1808 ---------------------------------------------
 3 files changed, 1809 insertions(+), 1808 deletions(-)
 create mode 100644 drivers/block/nvme-core.c
 delete mode 100644 drivers/block/nvme.c
drivers/block/Makefile +1 -0
index 17e82df..dd192b6 100644
@@ -41,4 +41,5 @@ obj-$(CONFIG_BLK_DEV_DRBD)     += drbd/
 obj-$(CONFIG_BLK_DEV_RBD)     += rbd.o
 obj-$(CONFIG_BLK_DEV_PCIESSD_MTIP32XX)	+= mtip32xx/
 
+nvme-y 		:= nvme-core.o
 swim_mod-y	:= swim.o swim_asm.o
drivers/block/nvme-core.c new file +1808 -0

1,808 lines not on this page · raw message

drivers/block/nvme.c deleted +0 -1808

1,187 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.7.0.4

Part of the diff is not shown here. View the raw message.

#2 lore raw

PATCH v3 2/3 NVMe: Move dev, ns, iod structs to nvme.c

In reply to Vishal Verma

nvme-scsi.c uses nvme_dev, nvme_ns, and nvme_iod.
Move the structure definitions to nvme.h

Signed-off-by Vishal Verma <vishal.l.verma at intel.com>
---
 drivers/block/nvme-core.c |   53 -----------------------------------
 include/linux/nvme.h      |   67 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 53 deletions(-)
drivers/block/nvme-core.c +0 -53
index 993c014..310b6f2 100644
@@ -60,43 +60,6 @@ static LIST_HEAD(dev_list);
 static struct task_struct *nvme_thread;
 
 /*
- * Represents an NVM Express device.  Each nvme_dev is a PCI function.
- */
-struct nvme_dev {
-	struct list_head node;
-	struct nvme_queue **queues;
-	u32 __iomem *dbs;
-	struct pci_dev *pci_dev;
-	struct dma_pool *prp_page_pool;
-	struct dma_pool *prp_small_pool;
-	int instance;
-	int queue_count;
-	int db_stride;
-	u32 ctrl_config;
-	struct msix_entry *entry;
-	struct nvme_bar __iomem *bar;
-	struct list_head namespaces;
-	char serial[20];
-	char model[40];
-	char firmware_rev[8];
-	u32 max_hw_sectors;
-};
-
-/*
- * An NVM Express namespace is equivalent to a SCSI LUN
- */
-struct nvme_ns {
-	struct list_head list;
-
-	struct nvme_dev *dev;
-	struct request_queue *queue;
-	struct gendisk *disk;
-
-	int ns_id;
-	int lba_shift;
-};
-
-/*
  * An NVM Express queue.  Each device has at least two (one for admin
  * commands and one for I/O commands).
  */
@@ -294,22 +257,6 @@ static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
 	return 0;
 }
 
-/*
- * The nvme_iod describes the data in an I/O, including the list of PRP
- * entries.  You can't see it in this data structure because C doesn't let
- * me express that.  Use nvme_alloc_iod to ensure there's enough space
- * allocated to store the PRP list.
- */
-struct nvme_iod {
-	void *private;		/* For the use of the submitter of the I/O */
-	int npages;		/* In the PRP list. 0 means small pool in use */
-	int offset;		/* Of PRP list */
-	int nents;		/* Used in scatterlist */
-	int length;		/* Of data, in bytes */
-	dma_addr_t first_dma;
-	struct scatterlist sg[0];
-};
-
 static __le64 **iod_list(struct nvme_iod *iod)
 {
 	return ((void *)iod) + iod->offset;
include/linux/nvme.h +67 -0

71 lines not on this page · raw message

Signature · 1 line
1.7.0.4
#3 lore raw

PATCH v3 3/3 NVMe: Add nvme-scsi.c

In reply to Vishal Verma

Translates SCSI commands in SG_IO ioctl to NVMe commands. Uses the scsi-nvme translation spec from nvmexpress.org as reference.

Signed-off-by Vishal Verma <vishal.l.verma at intel.com>
---
 drivers/block/Makefile    |    2 +-
 drivers/block/nvme-core.c |   39 +-
 drivers/block/nvme-scsi.c | 2941 +++++++++++++++++++++++++++++++++++++++++++++
 include/linux/nvme.h      |   40 +
 4 files changed, 3004 insertions(+), 18 deletions(-)
 create mode 100644 drivers/block/nvme-scsi.c
drivers/block/Makefile +1 -1
index dd192b6..8b678fc 100644
@@ -41,5 +41,5 @@ obj-$(CONFIG_BLK_DEV_DRBD)     += drbd/
 obj-$(CONFIG_BLK_DEV_RBD)     += rbd.o
 obj-$(CONFIG_BLK_DEV_PCIESSD_MTIP32XX)	+= mtip32xx/
 
-nvme-y 		:= nvme-core.o
+nvme-y 		:= nvme-core.o nvme-scsi.o
 swim_mod-y	:= swim.o swim_asm.o
drivers/block/nvme-core.c +22 -17
index 310b6f2..26e4a76 100644
@@ -39,6 +39,7 @@
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/types.h>
+#include <scsi/sg.h>
 
 #include <asm-generic/io-64-nonatomic-lo-hi.h>
 
@@ -46,7 +47,6 @@
 #define SQ_SIZE(depth)		(depth * sizeof(struct nvme_command))
 #define CQ_SIZE(depth)		(depth * sizeof(struct nvme_completion))
 #define NVME_MINORS 64
-#define NVME_IO_TIMEOUT	(5 * HZ)
 #define ADMIN_TIMEOUT	(60 * HZ)
 
 static int nvme_major;
@@ -94,6 +94,7 @@ static inline void _nvme_check_size(void)
 	BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
 	BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
 	BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
+	BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
 	BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
 	BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
 	BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
@@ -224,12 +225,12 @@ static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
 	return ctx;
 }
 
-static struct nvme_queue *get_nvmeq(struct nvme_dev *dev)
+struct nvme_queue *get_nvmeq(struct nvme_dev *dev)
 {
 	return dev->queues[get_cpu() + 1];
 }
 
-static void put_nvmeq(struct nvme_queue *nvmeq)
+void put_nvmeq(struct nvme_queue *nvmeq)
 {
 	put_cpu();
 }
@@ -290,7 +291,7 @@ nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp)
 	return iod;
 }
 
-static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
+void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
 {
 	const int last_prp = PAGE_SIZE / 8 - 1;
 	int i;
@@ -339,9 +340,8 @@ static void bio_completion(struct nvme_dev *dev, void *ctx,
 }
 
 /* length is in bytes.  gfp flags indicates whether we may sleep. */
-static int nvme_setup_prps(struct nvme_dev *dev,
-			struct nvme_common_command *cmd, struct nvme_iod *iod,
-			int total_len, gfp_t gfp)
+int nvme_setup_prps(struct nvme_dev *dev, struct nvme_common_command *cmd,
+			struct nvme_iod *iod, int total_len, gfp_t gfp)
 {
 	struct dma_pool *pool;
 	int length = total_len;
@@ -474,7 +474,7 @@ static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
 	return 0;
 }
 
-static int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
+int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
 {
 	int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH,
 					special_completion, NVME_IO_TIMEOUT);
@@ -671,8 +671,8 @@ static void sync_completion(struct nvme_dev *dev, void *ctx,
  * Returns 0 on success.  If the result is negative, it's a Linux error code;
  * if the result is positive, it's an NVM Express status code
  */
-static int nvme_submit_sync_cmd(struct nvme_queue *nvmeq,
-			struct nvme_command *cmd, u32 *result, unsigned timeout)
+int nvme_submit_sync_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd,
+						u32 *result, unsigned timeout)
 {
 	int cmdid;
 	struct sync_cmd_info cmdinfo;
@@ -701,7 +701,7 @@ static int nvme_submit_sync_cmd(struct nvme_queue *nvmeq,
 	return cmdinfo.status;
 }
 
-static int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
+int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
 								u32 *result)
 {
 	return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT);
@@ -774,7 +774,7 @@ static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
 	return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
 }
 
-static int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
+int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
 							dma_addr_t dma_addr)
 {
 	struct nvme_command c;
@@ -788,7 +788,7 @@ static int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
 	return nvme_submit_admin_cmd(dev, &c, NULL);
 }
 
-static int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
+int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
 					dma_addr_t dma_addr, u32 *result)
 {
 	struct nvme_command c;
@@ -802,8 +802,8 @@ static int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
 	return nvme_submit_admin_cmd(dev, &c, result);
 }
 
-static int nvme_set_features(struct nvme_dev *dev, unsigned fid,
-			unsigned dword11, dma_addr_t dma_addr, u32 *result)
+int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
+					dma_addr_t dma_addr, u32 *result)
 {
 	struct nvme_command c;
 
@@ -1021,7 +1021,7 @@ static int __devinit nvme_configure_admin_queue(struct nvme_dev *dev)
 	return result;
 }
 
-static struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
+struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
 				unsigned long addr, unsigned length)
 {
 	int i, err, count, nents, offset;
@@ -1077,7 +1077,7 @@ static struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
 	return ERR_PTR(err);
 }
 
-static void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
+void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
 			struct nvme_iod *iod)
 {
 	int i;
@@ -1213,6 +1213,10 @@ static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
 		return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
 	case NVME_IOCTL_SUBMIT_IO:
 		return nvme_submit_io(ns, (void __user *)arg);
+	case SG_GET_VERSION_NUM:
+		return nvme_sg_get_version_num((void __user *)arg);
+	case SG_IO:
+		return nvme_sg_io(ns, (void __user *)arg);
 	default:
 		return -ENOTTY;
 	}
@@ -1470,6 +1474,7 @@ static int __devinit nvme_dev_add(struct nvme_dev *dev)
 	memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
 	memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
 	memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
+	dev->vendor_id = le16_to_cpup(&ctrl->vid);
 	if (ctrl->mdts) {
 		int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
 		dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
drivers/block/nvme-scsi.c new file +2941 -0

2,855 lines not on this page · raw message

include/linux/nvme.h +40 -0
index 968d175..152c134 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.7.0.4

Part of the diff is not shown here. View the raw message.

#4 lore raw

v3 cover NVMe: Add support for SG_IO

In reply to Vishal Verma
Quoted 9 lines · Vishal Verma

These patches allow the nvme driver to handle the SG_IO ioctl. Applications may send any supported SCSI Command through the SG_IO framework. The supported command list as well as the translation reference is available on www.nvmexpress.org

Vishal Verma (3):
NVMe: Rename nvme.c to nvme-core.c
NVMe: Move dev, ns, iod structs to nvme.c
NVMe: Add nvme-scsi.c

I ended up modifying these patche before committing them.

- I split out the addition of the format command into its own patch. It's significant enough to be worth doing separately.
- The comment on nvme_free_iod() was part of the second patch instead of the final patch, for some reason.
- I moved the additions to the nvme_ns data structure to the final patch (was part of the second patch).
- Instead of storing the vendor_id from the identify result, just use the pci_dev's ->vendor element.

Hope I didn't break anything in the process :-)