nvme.h 5.6 KB
Newer Older
M
Matthew Wilcox 已提交
1 2
/*
 * Definitions for the NVM Express interface
M
Matthew Wilcox 已提交
3
 * Copyright (c) 2011-2014, Intel Corporation.
M
Matthew Wilcox 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 */

#ifndef _LINUX_NVME_H
#define _LINUX_NVME_H

18 19 20
#include <uapi/linux/nvme.h>
#include <linux/pci.h>
#include <linux/kref.h>
M
Matias Bjørling 已提交
21
#include <linux/blk-mq.h>
M
Matthew Wilcox 已提交
22 23 24 25

struct nvme_bar {
	__u64			cap;	/* Controller Capabilities */
	__u32			vs;	/* Version */
26 27
	__u32			intms;	/* Interrupt Mask Set */
	__u32			intmc;	/* Interrupt Mask Clear */
M
Matthew Wilcox 已提交
28
	__u32			cc;	/* Controller Configuration */
29
	__u32			rsvd1;	/* Reserved */
M
Matthew Wilcox 已提交
30
	__u32			csts;	/* Controller Status */
31
	__u32			rsvd2;	/* Reserved */
M
Matthew Wilcox 已提交
32 33 34
	__u32			aqa;	/* Admin Queue Attributes */
	__u64			asq;	/* Admin SQ Base Address */
	__u64			acq;	/* Admin CQ Base Address */
35 36
	__u32			cmbloc; /* Controller Memory Buffer Location */
	__u32			cmbsz;  /* Controller Memory Buffer Size */
M
Matthew Wilcox 已提交
37 38
};

39
#define NVME_CAP_MQES(cap)	((cap) & 0xffff)
40
#define NVME_CAP_TIMEOUT(cap)	(((cap) >> 24) & 0xff)
41
#define NVME_CAP_STRIDE(cap)	(((cap) >> 32) & 0xf)
42
#define NVME_CAP_NSSRC(cap)	(((cap) >> 36) & 0x1)
K
Keith Busch 已提交
43
#define NVME_CAP_MPSMIN(cap)	(((cap) >> 48) & 0xf)
44
#define NVME_CAP_MPSMAX(cap)	(((cap) >> 52) & 0xf)
45

46 47 48 49 50 51 52 53 54 55 56
#define NVME_CMB_BIR(cmbloc)	((cmbloc) & 0x7)
#define NVME_CMB_OFST(cmbloc)	(((cmbloc) >> 12) & 0xfffff)
#define NVME_CMB_SZ(cmbsz)	(((cmbsz) >> 12) & 0xfffff)
#define NVME_CMB_SZU(cmbsz)	(((cmbsz) >> 8) & 0xf)

#define NVME_CMB_WDS(cmbsz)	((cmbsz) & 0x10)
#define NVME_CMB_RDS(cmbsz)	((cmbsz) & 0x8)
#define NVME_CMB_LISTS(cmbsz)	((cmbsz) & 0x4)
#define NVME_CMB_CQS(cmbsz)	((cmbsz) & 0x2)
#define NVME_CMB_SQS(cmbsz)	((cmbsz) & 0x1)

M
Matthew Wilcox 已提交
57 58 59 60 61 62
enum {
	NVME_CC_ENABLE		= 1 << 0,
	NVME_CC_CSS_NVM		= 0 << 4,
	NVME_CC_MPS_SHIFT	= 7,
	NVME_CC_ARB_RR		= 0 << 11,
	NVME_CC_ARB_WRRU	= 1 << 11,
63 64 65 66
	NVME_CC_ARB_VS		= 7 << 11,
	NVME_CC_SHN_NONE	= 0 << 14,
	NVME_CC_SHN_NORMAL	= 1 << 14,
	NVME_CC_SHN_ABRUPT	= 2 << 14,
K
Keith Busch 已提交
67
	NVME_CC_SHN_MASK	= 3 << 14,
68 69
	NVME_CC_IOSQES		= 6 << 16,
	NVME_CC_IOCQES		= 4 << 20,
M
Matthew Wilcox 已提交
70 71
	NVME_CSTS_RDY		= 1 << 0,
	NVME_CSTS_CFS		= 1 << 1,
72
	NVME_CSTS_NSSRO		= 1 << 4,
M
Matthew Wilcox 已提交
73 74 75
	NVME_CSTS_SHST_NORMAL	= 0 << 2,
	NVME_CSTS_SHST_OCCUR	= 1 << 2,
	NVME_CSTS_SHST_CMPLT	= 2 << 2,
K
Keith Busch 已提交
76
	NVME_CSTS_SHST_MASK	= 3 << 2,
M
Matthew Wilcox 已提交
77 78
};

79 80
extern unsigned char nvme_io_timeout;
#define NVME_IO_TIMEOUT	(nvme_io_timeout * HZ)
81 82 83 84 85 86

/*
 * Represents an NVM Express device.  Each nvme_dev is a PCI function.
 */
struct nvme_dev {
	struct list_head node;
M
Matias Bjørling 已提交
87 88 89 90
	struct nvme_queue **queues;
	struct request_queue *admin_q;
	struct blk_mq_tag_set tagset;
	struct blk_mq_tag_set admin_tagset;
91
	u32 __iomem *dbs;
92
	struct device *dev;
93 94 95
	struct dma_pool *prp_page_pool;
	struct dma_pool *prp_small_pool;
	int instance;
K
Keith Busch 已提交
96 97 98 99
	unsigned queue_count;
	unsigned online_queues;
	unsigned max_qid;
	int q_depth;
100
	u32 db_stride;
101 102 103 104
	u32 ctrl_config;
	struct msix_entry *entry;
	struct nvme_bar __iomem *bar;
	struct list_head namespaces;
105
	struct kref kref;
106
	struct device *device;
T
Tejun Heo 已提交
107
	work_func_t reset_workfn;
K
Keith Busch 已提交
108
	struct work_struct reset_work;
109
	struct work_struct probe_work;
K
Keith Busch 已提交
110
	struct work_struct scan_work;
111
	char name[12];
112 113 114
	char serial[20];
	char model[40];
	char firmware_rev[8];
115
	bool subsystem;
116
	u32 max_hw_sectors;
117
	u32 stripe_size;
118
	u32 page_size;
119 120 121 122
	void __iomem *cmb;
	dma_addr_t cmb_dma_addr;
	u64 cmb_size;
	u32 cmbsz;
123
	u16 oncs;
K
Keith Busch 已提交
124
	u16 abort_limit;
K
Keith Busch 已提交
125
	u8 event_limit;
126
	u8 vwc;
127 128 129 130 131 132 133 134 135 136 137 138
};

/*
 * 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;

139
	unsigned ns_id;
140
	int lba_shift;
141 142 143
	u16 ms;
	bool ext;
	u8 pi_type;
V
Vishal Verma 已提交
144 145
	u64 mode_select_num_blocks;
	u32 mode_select_block_len;
146 147 148 149 150 151 152 153 154
};

/*
 * 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 {
155
	unsigned long private;	/* For the use of the submitter of the I/O */
156 157 158 159 160
	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;
K
Keith Busch 已提交
161
	struct scatterlist meta_sg[1]; /* metadata requires single contiguous buffer */
162 163
	struct scatterlist sg[0];
};
V
Vishal Verma 已提交
164

165 166 167 168 169
static inline u64 nvme_block_nr(struct nvme_ns *ns, sector_t sector)
{
	return (sector >> (ns->lba_shift - 9));
}

170 171 172 173 174 175 176 177 178
int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
		void *buf, unsigned bufflen);
int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
		void *buffer, void __user *ubuffer, unsigned bufflen,
		u32 *result, unsigned timeout);
int nvme_identify_ctrl(struct nvme_dev *dev, struct nvme_id_ctrl **id);
int nvme_identify_ns(struct nvme_dev *dev, unsigned nsid,
		struct nvme_id_ns **id);
int nvme_get_log_page(struct nvme_dev *dev, struct nvme_smart_log **log);
V
Vishal Verma 已提交
179 180 181 182 183 184 185 186
int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
			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 sg_io_hdr;

int nvme_sg_io(struct nvme_ns *ns, struct sg_io_hdr __user *u_hdr);
K
Keith Busch 已提交
187
int nvme_sg_io32(struct nvme_ns *ns, unsigned long arg);
V
Vishal Verma 已提交
188 189
int nvme_sg_get_version_num(int __user *ip);

M
Matthew Wilcox 已提交
190
#endif /* _LINUX_NVME_H */