xref: /openbmc/linux/drivers/nvme/target/nvmet.h (revision d2d1c454)
177141dc6SChristoph Hellwig /* SPDX-License-Identifier: GPL-2.0 */
2a07b4970SChristoph Hellwig /*
3a07b4970SChristoph Hellwig  * Copyright (c) 2015-2016 HGST, a Western Digital Company.
4a07b4970SChristoph Hellwig  */
5a07b4970SChristoph Hellwig 
6a07b4970SChristoph Hellwig #ifndef _NVMET_H
7a07b4970SChristoph Hellwig #define _NVMET_H
8a07b4970SChristoph Hellwig 
9a07b4970SChristoph Hellwig #include <linux/dma-mapping.h>
10a07b4970SChristoph Hellwig #include <linux/types.h>
11a07b4970SChristoph Hellwig #include <linux/device.h>
12a07b4970SChristoph Hellwig #include <linux/kref.h>
13a07b4970SChristoph Hellwig #include <linux/percpu-refcount.h>
14a07b4970SChristoph Hellwig #include <linux/list.h>
15a07b4970SChristoph Hellwig #include <linux/mutex.h>
168e412263SChristoph Hellwig #include <linux/uuid.h>
17a07b4970SChristoph Hellwig #include <linux/nvme.h>
18a07b4970SChristoph Hellwig #include <linux/configfs.h>
19a07b4970SChristoph Hellwig #include <linux/rcupdate.h>
20a07b4970SChristoph Hellwig #include <linux/blkdev.h>
21c6925093SLogan Gunthorpe #include <linux/radix-tree.h>
22d2d1c454SIsrael Rukshin #include <linux/t10-pi.h>
23a07b4970SChristoph Hellwig 
24a07b4970SChristoph Hellwig #define NVMET_ASYNC_EVENTS		4
25a07b4970SChristoph Hellwig #define NVMET_ERROR_LOG_SLOTS		128
265698b805SChaitanya Kulkarni #define NVMET_NO_ERROR_LOC		((u16)-1)
27013b7ebeSMark Ruijter #define NVMET_DEFAULT_CTRL_MODEL	"Linux"
28a07b4970SChristoph Hellwig 
29c86b8f7bSChristoph Hellwig /*
30c86b8f7bSChristoph Hellwig  * Supported optional AENs:
31c86b8f7bSChristoph Hellwig  */
32c86b8f7bSChristoph Hellwig #define NVMET_AEN_CFG_OPTIONAL \
3362ac0d32SChristoph Hellwig 	(NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_ANA_CHANGE)
34f301c2b1SJay Sternberg #define NVMET_DISC_AEN_CFG_OPTIONAL \
35f301c2b1SJay Sternberg 	(NVME_AEN_CFG_DISC_CHANGE)
36c86b8f7bSChristoph Hellwig 
37c86b8f7bSChristoph Hellwig /*
38c86b8f7bSChristoph Hellwig  * Plus mandatory SMART AENs (we'll never send them, but allow enabling them):
39c86b8f7bSChristoph Hellwig  */
40c86b8f7bSChristoph Hellwig #define NVMET_AEN_CFG_ALL \
41c86b8f7bSChristoph Hellwig 	(NVME_SMART_CRIT_SPARE | NVME_SMART_CRIT_TEMPERATURE | \
42c86b8f7bSChristoph Hellwig 	 NVME_SMART_CRIT_RELIABILITY | NVME_SMART_CRIT_MEDIA | \
43c86b8f7bSChristoph Hellwig 	 NVME_SMART_CRIT_VOLATILE_MEMORY | NVMET_AEN_CFG_OPTIONAL)
44c86b8f7bSChristoph Hellwig 
45a07b4970SChristoph Hellwig /* Helper Macros when NVMe error is NVME_SC_CONNECT_INVALID_PARAM
46a07b4970SChristoph Hellwig  * The 16 bit shift is to set IATTR bit to 1, which means offending
47a07b4970SChristoph Hellwig  * offset starts in the data section of connect()
48a07b4970SChristoph Hellwig  */
49a07b4970SChristoph Hellwig #define IPO_IATTR_CONNECT_DATA(x)	\
50a07b4970SChristoph Hellwig 	(cpu_to_le32((1 << 16) | (offsetof(struct nvmf_connect_data, x))))
51a07b4970SChristoph Hellwig #define IPO_IATTR_CONNECT_SQE(x)	\
52a07b4970SChristoph Hellwig 	(cpu_to_le32(offsetof(struct nvmf_connect_command, x)))
53a07b4970SChristoph Hellwig 
54a07b4970SChristoph Hellwig struct nvmet_ns {
55a07b4970SChristoph Hellwig 	struct list_head	dev_link;
56a07b4970SChristoph Hellwig 	struct percpu_ref	ref;
57a07b4970SChristoph Hellwig 	struct block_device	*bdev;
58d5eff33eSChaitanya Kulkarni 	struct file		*file;
59dedf0be5SChaitanya Kulkarni 	bool			readonly;
60a07b4970SChristoph Hellwig 	u32			nsid;
61a07b4970SChristoph Hellwig 	u32			blksize_shift;
62a07b4970SChristoph Hellwig 	loff_t			size;
63a07b4970SChristoph Hellwig 	u8			nguid[16];
64637dc0f3SJohannes Thumshirn 	uuid_t			uuid;
6572efd25dSChristoph Hellwig 	u32			anagrpid;
66a07b4970SChristoph Hellwig 
6755eb942eSChaitanya Kulkarni 	bool			buffered_io;
68e4fcf07cSSolganik Alexander 	bool			enabled;
69a07b4970SChristoph Hellwig 	struct nvmet_subsys	*subsys;
70a07b4970SChristoph Hellwig 	const char		*device_path;
71a07b4970SChristoph Hellwig 
72a07b4970SChristoph Hellwig 	struct config_group	device_group;
73a07b4970SChristoph Hellwig 	struct config_group	group;
74a07b4970SChristoph Hellwig 
75a07b4970SChristoph Hellwig 	struct completion	disable_done;
76d5eff33eSChaitanya Kulkarni 	mempool_t		*bvec_pool;
77d5eff33eSChaitanya Kulkarni 	struct kmem_cache	*bvec_cache;
78c6925093SLogan Gunthorpe 
79c6925093SLogan Gunthorpe 	int			use_p2pmem;
80c6925093SLogan Gunthorpe 	struct pci_dev		*p2p_dev;
81d2d1c454SIsrael Rukshin 	int			pi_type;
82d2d1c454SIsrael Rukshin 	int			metadata_size;
83a07b4970SChristoph Hellwig };
84a07b4970SChristoph Hellwig 
85a07b4970SChristoph Hellwig static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
86a07b4970SChristoph Hellwig {
87a07b4970SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_ns, group);
88a07b4970SChristoph Hellwig }
89a07b4970SChristoph Hellwig 
90c6925093SLogan Gunthorpe static inline struct device *nvmet_ns_dev(struct nvmet_ns *ns)
91c6925093SLogan Gunthorpe {
92c6925093SLogan Gunthorpe 	return ns->bdev ? disk_to_dev(ns->bdev->bd_disk) : NULL;
93c6925093SLogan Gunthorpe }
94c6925093SLogan Gunthorpe 
95a07b4970SChristoph Hellwig struct nvmet_cq {
96a07b4970SChristoph Hellwig 	u16			qid;
97a07b4970SChristoph Hellwig 	u16			size;
98a07b4970SChristoph Hellwig };
99a07b4970SChristoph Hellwig 
100a07b4970SChristoph Hellwig struct nvmet_sq {
101a07b4970SChristoph Hellwig 	struct nvmet_ctrl	*ctrl;
102a07b4970SChristoph Hellwig 	struct percpu_ref	ref;
103a07b4970SChristoph Hellwig 	u16			qid;
104a07b4970SChristoph Hellwig 	u16			size;
105f9cf2a64SJames Smart 	u32			sqhd;
106e6a622fdSSagi Grimberg 	bool			sqhd_disabled;
107a07b4970SChristoph Hellwig 	struct completion	free_done;
108427242ceSSagi Grimberg 	struct completion	confirm_done;
109a07b4970SChristoph Hellwig };
110a07b4970SChristoph Hellwig 
11162ac0d32SChristoph Hellwig struct nvmet_ana_group {
11262ac0d32SChristoph Hellwig 	struct config_group	group;
11362ac0d32SChristoph Hellwig 	struct nvmet_port	*port;
11462ac0d32SChristoph Hellwig 	u32			grpid;
11562ac0d32SChristoph Hellwig };
11662ac0d32SChristoph Hellwig 
11762ac0d32SChristoph Hellwig static inline struct nvmet_ana_group *to_ana_group(struct config_item *item)
11862ac0d32SChristoph Hellwig {
11962ac0d32SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_ana_group,
12062ac0d32SChristoph Hellwig 			group);
12162ac0d32SChristoph Hellwig }
12262ac0d32SChristoph Hellwig 
123a07b4970SChristoph Hellwig /**
124a07b4970SChristoph Hellwig  * struct nvmet_port -	Common structure to keep port
125a07b4970SChristoph Hellwig  *				information for the target.
126fe4a9791SChristoph Hellwig  * @entry:		Entry into referrals or transport list.
127a07b4970SChristoph Hellwig  * @disc_addr:		Address information is stored in a format defined
128a07b4970SChristoph Hellwig  *				for a discovery log page entry.
129a07b4970SChristoph Hellwig  * @group:		ConfigFS group for this element's folder.
130a07b4970SChristoph Hellwig  * @priv:		Private data for the transport.
131a07b4970SChristoph Hellwig  */
132a07b4970SChristoph Hellwig struct nvmet_port {
133a07b4970SChristoph Hellwig 	struct list_head		entry;
134a07b4970SChristoph Hellwig 	struct nvmf_disc_rsp_page_entry	disc_addr;
135a07b4970SChristoph Hellwig 	struct config_group		group;
136a07b4970SChristoph Hellwig 	struct config_group		subsys_group;
137a07b4970SChristoph Hellwig 	struct list_head		subsystems;
138a07b4970SChristoph Hellwig 	struct config_group		referrals_group;
139a07b4970SChristoph Hellwig 	struct list_head		referrals;
140b662a078SJay Sternberg 	struct list_head		global_entry;
14162ac0d32SChristoph Hellwig 	struct config_group		ana_groups_group;
14262ac0d32SChristoph Hellwig 	struct nvmet_ana_group		ana_default_group;
14372efd25dSChristoph Hellwig 	enum nvme_ana_state		*ana_state;
144a07b4970SChristoph Hellwig 	void				*priv;
145a07b4970SChristoph Hellwig 	bool				enabled;
1460d5ee2b2SSteve Wise 	int				inline_data_size;
1479d09dd8dSJames Smart 	const struct nvmet_fabrics_ops	*tr_ops;
148a07b4970SChristoph Hellwig };
149a07b4970SChristoph Hellwig 
150a07b4970SChristoph Hellwig static inline struct nvmet_port *to_nvmet_port(struct config_item *item)
151a07b4970SChristoph Hellwig {
152a07b4970SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_port,
153a07b4970SChristoph Hellwig 			group);
154a07b4970SChristoph Hellwig }
155a07b4970SChristoph Hellwig 
15662ac0d32SChristoph Hellwig static inline struct nvmet_port *ana_groups_to_port(
15762ac0d32SChristoph Hellwig 		struct config_item *item)
15862ac0d32SChristoph Hellwig {
15962ac0d32SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_port,
16062ac0d32SChristoph Hellwig 			ana_groups_group);
16162ac0d32SChristoph Hellwig }
16262ac0d32SChristoph Hellwig 
163a07b4970SChristoph Hellwig struct nvmet_ctrl {
164a07b4970SChristoph Hellwig 	struct nvmet_subsys	*subsys;
165a07b4970SChristoph Hellwig 	struct nvmet_cq		**cqs;
166a07b4970SChristoph Hellwig 	struct nvmet_sq		**sqs;
167a07b4970SChristoph Hellwig 
168c09305aeSSagi Grimberg 	bool			cmd_seen;
169c09305aeSSagi Grimberg 
170a07b4970SChristoph Hellwig 	struct mutex		lock;
171a07b4970SChristoph Hellwig 	u64			cap;
172a07b4970SChristoph Hellwig 	u32			cc;
173a07b4970SChristoph Hellwig 	u32			csts;
174a07b4970SChristoph Hellwig 
17528dd5cf7SOmri Mann 	uuid_t			hostid;
176a07b4970SChristoph Hellwig 	u16			cntlid;
177a07b4970SChristoph Hellwig 	u32			kato;
178a07b4970SChristoph Hellwig 
1794ee43280SChristoph Hellwig 	struct nvmet_port	*port;
1804ee43280SChristoph Hellwig 
181c86b8f7bSChristoph Hellwig 	u32			aen_enabled;
18255fdd6b6SChristoph Hellwig 	unsigned long		aen_masked;
183a07b4970SChristoph Hellwig 	struct nvmet_req	*async_event_cmds[NVMET_ASYNC_EVENTS];
184a07b4970SChristoph Hellwig 	unsigned int		nr_async_event_cmds;
185a07b4970SChristoph Hellwig 	struct list_head	async_events;
186a07b4970SChristoph Hellwig 	struct work_struct	async_event_work;
187a07b4970SChristoph Hellwig 
188a07b4970SChristoph Hellwig 	struct list_head	subsys_entry;
189a07b4970SChristoph Hellwig 	struct kref		ref;
190a07b4970SChristoph Hellwig 	struct delayed_work	ka_work;
191a07b4970SChristoph Hellwig 	struct work_struct	fatal_err_work;
192a07b4970SChristoph Hellwig 
193e929f06dSChristoph Hellwig 	const struct nvmet_fabrics_ops *ops;
194a07b4970SChristoph Hellwig 
195c16734eaSChristoph Hellwig 	__le32			*changed_ns_list;
196c16734eaSChristoph Hellwig 	u32			nr_changed_ns;
197c16734eaSChristoph Hellwig 
198a07b4970SChristoph Hellwig 	char			subsysnqn[NVMF_NQN_FIELD_LEN];
199a07b4970SChristoph Hellwig 	char			hostnqn[NVMF_NQN_FIELD_LEN];
200c6925093SLogan Gunthorpe 
201c6925093SLogan Gunthorpe 	struct device		*p2p_client;
202c6925093SLogan Gunthorpe 	struct radix_tree_root	p2p_ns_map;
203e4a97625SChaitanya Kulkarni 
204e4a97625SChaitanya Kulkarni 	spinlock_t		error_lock;
205e4a97625SChaitanya Kulkarni 	u64			err_counter;
206e4a97625SChaitanya Kulkarni 	struct nvme_error_slot	slots[NVMET_ERROR_LOG_SLOTS];
207a07b4970SChristoph Hellwig };
208a07b4970SChristoph Hellwig 
209013b7ebeSMark Ruijter struct nvmet_subsys_model {
210013b7ebeSMark Ruijter 	struct rcu_head		rcuhead;
211013b7ebeSMark Ruijter 	char			number[];
212013b7ebeSMark Ruijter };
213013b7ebeSMark Ruijter 
214a07b4970SChristoph Hellwig struct nvmet_subsys {
215a07b4970SChristoph Hellwig 	enum nvme_subsys_type	type;
216a07b4970SChristoph Hellwig 
217a07b4970SChristoph Hellwig 	struct mutex		lock;
218a07b4970SChristoph Hellwig 	struct kref		ref;
219a07b4970SChristoph Hellwig 
220a07b4970SChristoph Hellwig 	struct list_head	namespaces;
221793c7cfcSChristoph Hellwig 	unsigned int		nr_namespaces;
222a07b4970SChristoph Hellwig 	unsigned int		max_nsid;
22394a39d61SChaitanya Kulkarni 	u16			cntlid_min;
22494a39d61SChaitanya Kulkarni 	u16			cntlid_max;
225a07b4970SChristoph Hellwig 
226a07b4970SChristoph Hellwig 	struct list_head	ctrls;
227a07b4970SChristoph Hellwig 
228a07b4970SChristoph Hellwig 	struct list_head	hosts;
229a07b4970SChristoph Hellwig 	bool			allow_any_host;
230a07b4970SChristoph Hellwig 
231a07b4970SChristoph Hellwig 	u16			max_qid;
232a07b4970SChristoph Hellwig 
233a07b4970SChristoph Hellwig 	u64			ver;
2342e7f5d2aSJohannes Thumshirn 	u64			serial;
235a07b4970SChristoph Hellwig 	char			*subsysnqn;
236a07b4970SChristoph Hellwig 
237a07b4970SChristoph Hellwig 	struct config_group	group;
238a07b4970SChristoph Hellwig 
239a07b4970SChristoph Hellwig 	struct config_group	namespaces_group;
240a07b4970SChristoph Hellwig 	struct config_group	allowed_hosts_group;
241013b7ebeSMark Ruijter 
242013b7ebeSMark Ruijter 	struct nvmet_subsys_model	__rcu *model;
243a07b4970SChristoph Hellwig };
244a07b4970SChristoph Hellwig 
245a07b4970SChristoph Hellwig static inline struct nvmet_subsys *to_subsys(struct config_item *item)
246a07b4970SChristoph Hellwig {
247a07b4970SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_subsys, group);
248a07b4970SChristoph Hellwig }
249a07b4970SChristoph Hellwig 
250a07b4970SChristoph Hellwig static inline struct nvmet_subsys *namespaces_to_subsys(
251a07b4970SChristoph Hellwig 		struct config_item *item)
252a07b4970SChristoph Hellwig {
253a07b4970SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_subsys,
254a07b4970SChristoph Hellwig 			namespaces_group);
255a07b4970SChristoph Hellwig }
256a07b4970SChristoph Hellwig 
257a07b4970SChristoph Hellwig struct nvmet_host {
258a07b4970SChristoph Hellwig 	struct config_group	group;
259a07b4970SChristoph Hellwig };
260a07b4970SChristoph Hellwig 
261a07b4970SChristoph Hellwig static inline struct nvmet_host *to_host(struct config_item *item)
262a07b4970SChristoph Hellwig {
263a07b4970SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_host, group);
264a07b4970SChristoph Hellwig }
265a07b4970SChristoph Hellwig 
266a07b4970SChristoph Hellwig static inline char *nvmet_host_name(struct nvmet_host *host)
267a07b4970SChristoph Hellwig {
268a07b4970SChristoph Hellwig 	return config_item_name(&host->group.cg_item);
269a07b4970SChristoph Hellwig }
270a07b4970SChristoph Hellwig 
271a07b4970SChristoph Hellwig struct nvmet_host_link {
272a07b4970SChristoph Hellwig 	struct list_head	entry;
273a07b4970SChristoph Hellwig 	struct nvmet_host	*host;
274a07b4970SChristoph Hellwig };
275a07b4970SChristoph Hellwig 
276a07b4970SChristoph Hellwig struct nvmet_subsys_link {
277a07b4970SChristoph Hellwig 	struct list_head	entry;
278a07b4970SChristoph Hellwig 	struct nvmet_subsys	*subsys;
279a07b4970SChristoph Hellwig };
280a07b4970SChristoph Hellwig 
281a07b4970SChristoph Hellwig struct nvmet_req;
282a07b4970SChristoph Hellwig struct nvmet_fabrics_ops {
283a07b4970SChristoph Hellwig 	struct module *owner;
284a07b4970SChristoph Hellwig 	unsigned int type;
285a07b4970SChristoph Hellwig 	unsigned int msdbd;
286a07b4970SChristoph Hellwig 	bool has_keyed_sgls : 1;
287a07b4970SChristoph Hellwig 	void (*queue_response)(struct nvmet_req *req);
288a07b4970SChristoph Hellwig 	int (*add_port)(struct nvmet_port *port);
289a07b4970SChristoph Hellwig 	void (*remove_port)(struct nvmet_port *port);
290a07b4970SChristoph Hellwig 	void (*delete_ctrl)(struct nvmet_ctrl *ctrl);
2914c652685SSagi Grimberg 	void (*disc_traddr)(struct nvmet_req *req,
2924c652685SSagi Grimberg 			struct nvmet_port *port, char *traddr);
2931672ddb8SSagi Grimberg 	u16 (*install_queue)(struct nvmet_sq *nvme_sq);
2949d09dd8dSJames Smart 	void (*discovery_chg)(struct nvmet_port *port);
29502cb00e2SMax Gurtovoy 	u8 (*get_mdts)(const struct nvmet_ctrl *ctrl);
296a07b4970SChristoph Hellwig };
297a07b4970SChristoph Hellwig 
298a07b4970SChristoph Hellwig #define NVMET_MAX_INLINE_BIOVEC	8
29973383adfSSagi Grimberg #define NVMET_MAX_INLINE_DATA_LEN NVMET_MAX_INLINE_BIOVEC * PAGE_SIZE
300a07b4970SChristoph Hellwig 
301a07b4970SChristoph Hellwig struct nvmet_req {
302a07b4970SChristoph Hellwig 	struct nvme_command	*cmd;
303fc6c9730SMax Gurtovoy 	struct nvme_completion	*cqe;
304a07b4970SChristoph Hellwig 	struct nvmet_sq		*sq;
305a07b4970SChristoph Hellwig 	struct nvmet_cq		*cq;
306a07b4970SChristoph Hellwig 	struct nvmet_ns		*ns;
307a07b4970SChristoph Hellwig 	struct scatterlist	*sg;
308a07b4970SChristoph Hellwig 	struct bio_vec		inline_bvec[NVMET_MAX_INLINE_BIOVEC];
309d5eff33eSChaitanya Kulkarni 	union {
310d5eff33eSChaitanya Kulkarni 		struct {
311d5eff33eSChaitanya Kulkarni 			struct bio      inline_bio;
312d5eff33eSChaitanya Kulkarni 		} b;
313d5eff33eSChaitanya Kulkarni 		struct {
314d5eff33eSChaitanya Kulkarni 			bool			mpool_alloc;
315d5eff33eSChaitanya Kulkarni 			struct kiocb            iocb;
316d5eff33eSChaitanya Kulkarni 			struct bio_vec          *bvec;
317d5eff33eSChaitanya Kulkarni 			struct work_struct      work;
318d5eff33eSChaitanya Kulkarni 		} f;
319d5eff33eSChaitanya Kulkarni 	};
320a07b4970SChristoph Hellwig 	int			sg_cnt;
3215e62d5c9SChristoph Hellwig 	/* data length as parsed from the SGL descriptor: */
3225e62d5c9SChristoph Hellwig 	size_t			transfer_len;
323a07b4970SChristoph Hellwig 
324a07b4970SChristoph Hellwig 	struct nvmet_port	*port;
325a07b4970SChristoph Hellwig 
326a07b4970SChristoph Hellwig 	void (*execute)(struct nvmet_req *req);
327e929f06dSChristoph Hellwig 	const struct nvmet_fabrics_ops *ops;
328c6925093SLogan Gunthorpe 
329c6925093SLogan Gunthorpe 	struct pci_dev		*p2p_dev;
330c6925093SLogan Gunthorpe 	struct device		*p2p_client;
331e4a97625SChaitanya Kulkarni 	u16			error_loc;
332e4a97625SChaitanya Kulkarni 	u64			error_slba;
333a07b4970SChristoph Hellwig };
334a07b4970SChristoph Hellwig 
33555eb942eSChaitanya Kulkarni extern struct workqueue_struct *buffered_io_wq;
33655eb942eSChaitanya Kulkarni 
337a07b4970SChristoph Hellwig static inline void nvmet_set_result(struct nvmet_req *req, u32 result)
338a07b4970SChristoph Hellwig {
339fc6c9730SMax Gurtovoy 	req->cqe->result.u32 = cpu_to_le32(result);
340a07b4970SChristoph Hellwig }
341a07b4970SChristoph Hellwig 
342a07b4970SChristoph Hellwig /*
343a07b4970SChristoph Hellwig  * NVMe command writes actually are DMA reads for us on the target side.
344a07b4970SChristoph Hellwig  */
345a07b4970SChristoph Hellwig static inline enum dma_data_direction
346a07b4970SChristoph Hellwig nvmet_data_dir(struct nvmet_req *req)
347a07b4970SChristoph Hellwig {
348a07b4970SChristoph Hellwig 	return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
349a07b4970SChristoph Hellwig }
350a07b4970SChristoph Hellwig 
351a07b4970SChristoph Hellwig struct nvmet_async_event {
352a07b4970SChristoph Hellwig 	struct list_head	entry;
353a07b4970SChristoph Hellwig 	u8			event_type;
354a07b4970SChristoph Hellwig 	u8			event_info;
355a07b4970SChristoph Hellwig 	u8			log_page;
356a07b4970SChristoph Hellwig };
357a07b4970SChristoph Hellwig 
3587114ddebSJay Sternberg static inline void nvmet_clear_aen_bit(struct nvmet_req *req, u32 bn)
3596c8312adSJay Sternberg {
360b7c8f366SChaitanya Kulkarni 	int rae = le32_to_cpu(req->cmd->common.cdw10) & 1 << 15;
3616c8312adSJay Sternberg 
3626c8312adSJay Sternberg 	if (!rae)
3637114ddebSJay Sternberg 		clear_bit(bn, &req->sq->ctrl->aen_masked);
3646c8312adSJay Sternberg }
3656c8312adSJay Sternberg 
3667114ddebSJay Sternberg static inline bool nvmet_aen_bit_disabled(struct nvmet_ctrl *ctrl, u32 bn)
3676c8312adSJay Sternberg {
3687114ddebSJay Sternberg 	if (!(READ_ONCE(ctrl->aen_enabled) & (1 << bn)))
3696c8312adSJay Sternberg 		return true;
3707114ddebSJay Sternberg 	return test_and_set_bit(bn, &ctrl->aen_masked);
3716c8312adSJay Sternberg }
3726c8312adSJay Sternberg 
37390107455SJay Sternberg void nvmet_get_feat_kato(struct nvmet_req *req);
37490107455SJay Sternberg void nvmet_get_feat_async_event(struct nvmet_req *req);
37590107455SJay Sternberg u16 nvmet_set_feat_kato(struct nvmet_req *req);
37690107455SJay Sternberg u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask);
37790107455SJay Sternberg void nvmet_execute_async_event(struct nvmet_req *req);
37890107455SJay Sternberg 
37964a0ca88SParav Pandit u16 nvmet_parse_connect_cmd(struct nvmet_req *req);
3809d05a96eSBart Van Assche void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id);
381d5eff33eSChaitanya Kulkarni u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req);
382d5eff33eSChaitanya Kulkarni u16 nvmet_file_parse_io_cmd(struct nvmet_req *req);
38364a0ca88SParav Pandit u16 nvmet_parse_admin_cmd(struct nvmet_req *req);
38464a0ca88SParav Pandit u16 nvmet_parse_discovery_cmd(struct nvmet_req *req);
38564a0ca88SParav Pandit u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req);
386a07b4970SChristoph Hellwig 
387a07b4970SChristoph Hellwig bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
388e929f06dSChristoph Hellwig 		struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops);
389549f01aeSVijay Immanuel void nvmet_req_uninit(struct nvmet_req *req);
390e9061c39SChristoph Hellwig bool nvmet_check_data_len(struct nvmet_req *req, size_t data_len);
391b716e688SSagi Grimberg bool nvmet_check_data_len_lte(struct nvmet_req *req, size_t data_len);
392a07b4970SChristoph Hellwig void nvmet_req_complete(struct nvmet_req *req, u16 status);
3935b2322e4SLogan Gunthorpe int nvmet_req_alloc_sgl(struct nvmet_req *req);
3945b2322e4SLogan Gunthorpe void nvmet_req_free_sgl(struct nvmet_req *req);
395a07b4970SChristoph Hellwig 
396f9362ac1SJay Sternberg void nvmet_execute_keep_alive(struct nvmet_req *req);
397f9362ac1SJay Sternberg 
398a07b4970SChristoph Hellwig void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid,
399a07b4970SChristoph Hellwig 		u16 size);
400a07b4970SChristoph Hellwig void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid,
401a07b4970SChristoph Hellwig 		u16 size);
402a07b4970SChristoph Hellwig void nvmet_sq_destroy(struct nvmet_sq *sq);
403a07b4970SChristoph Hellwig int nvmet_sq_init(struct nvmet_sq *sq);
404a07b4970SChristoph Hellwig 
405a07b4970SChristoph Hellwig void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl);
406a07b4970SChristoph Hellwig 
407a07b4970SChristoph Hellwig void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new);
408a07b4970SChristoph Hellwig u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
409a07b4970SChristoph Hellwig 		struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp);
410a07b4970SChristoph Hellwig u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
411a07b4970SChristoph Hellwig 		struct nvmet_req *req, struct nvmet_ctrl **ret);
412a07b4970SChristoph Hellwig void nvmet_ctrl_put(struct nvmet_ctrl *ctrl);
41364a0ca88SParav Pandit u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd);
414a07b4970SChristoph Hellwig 
415a07b4970SChristoph Hellwig struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
416a07b4970SChristoph Hellwig 		enum nvme_subsys_type type);
417a07b4970SChristoph Hellwig void nvmet_subsys_put(struct nvmet_subsys *subsys);
418344770b0SSagi Grimberg void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys);
419a07b4970SChristoph Hellwig 
420a07b4970SChristoph Hellwig struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid);
421a07b4970SChristoph Hellwig void nvmet_put_namespace(struct nvmet_ns *ns);
422a07b4970SChristoph Hellwig int nvmet_ns_enable(struct nvmet_ns *ns);
423a07b4970SChristoph Hellwig void nvmet_ns_disable(struct nvmet_ns *ns);
424a07b4970SChristoph Hellwig struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
425a07b4970SChristoph Hellwig void nvmet_ns_free(struct nvmet_ns *ns);
426a07b4970SChristoph Hellwig 
42762ac0d32SChristoph Hellwig void nvmet_send_ana_event(struct nvmet_subsys *subsys,
42862ac0d32SChristoph Hellwig 		struct nvmet_port *port);
42962ac0d32SChristoph Hellwig void nvmet_port_send_ana_event(struct nvmet_port *port);
43062ac0d32SChristoph Hellwig 
431e929f06dSChristoph Hellwig int nvmet_register_transport(const struct nvmet_fabrics_ops *ops);
432e929f06dSChristoph Hellwig void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops);
433a07b4970SChristoph Hellwig 
4343aed8673SLogan Gunthorpe void nvmet_port_del_ctrls(struct nvmet_port *port,
4353aed8673SLogan Gunthorpe 			  struct nvmet_subsys *subsys);
4363aed8673SLogan Gunthorpe 
437a07b4970SChristoph Hellwig int nvmet_enable_port(struct nvmet_port *port);
438a07b4970SChristoph Hellwig void nvmet_disable_port(struct nvmet_port *port);
439a07b4970SChristoph Hellwig 
440a07b4970SChristoph Hellwig void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port);
441b662a078SJay Sternberg void nvmet_referral_disable(struct nvmet_port *parent, struct nvmet_port *port);
442a07b4970SChristoph Hellwig 
443a07b4970SChristoph Hellwig u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
444a07b4970SChristoph Hellwig 		size_t len);
445a07b4970SChristoph Hellwig u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf,
446a07b4970SChristoph Hellwig 		size_t len);
447c7759fffSChristoph Hellwig u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len);
448a07b4970SChristoph Hellwig 
449a07b4970SChristoph Hellwig u32 nvmet_get_log_page_len(struct nvme_command *cmd);
450d808b7f7SKeith Busch u64 nvmet_get_log_page_offset(struct nvme_command *cmd);
451a07b4970SChristoph Hellwig 
452b662a078SJay Sternberg extern struct list_head *nvmet_ports;
453b662a078SJay Sternberg void nvmet_port_disc_changed(struct nvmet_port *port,
454b662a078SJay Sternberg 		struct nvmet_subsys *subsys);
455b662a078SJay Sternberg void nvmet_subsys_disc_changed(struct nvmet_subsys *subsys,
456b662a078SJay Sternberg 		struct nvmet_host *host);
457b662a078SJay Sternberg void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
458b662a078SJay Sternberg 		u8 event_info, u8 log_page);
459b662a078SJay Sternberg 
460a07b4970SChristoph Hellwig #define NVMET_QUEUE_SIZE	1024
4613375e29cSJames Smart #define NVMET_NR_QUEUES		128
462a07b4970SChristoph Hellwig #define NVMET_MAX_CMD		NVMET_QUEUE_SIZE
463793c7cfcSChristoph Hellwig 
464793c7cfcSChristoph Hellwig /*
465793c7cfcSChristoph Hellwig  * Nice round number that makes a list of nsids fit into a page.
466793c7cfcSChristoph Hellwig  * Should become tunable at some point in the future.
467793c7cfcSChristoph Hellwig  */
468793c7cfcSChristoph Hellwig #define NVMET_MAX_NAMESPACES	1024
469793c7cfcSChristoph Hellwig 
47072efd25dSChristoph Hellwig /*
47172efd25dSChristoph Hellwig  * 0 is not a valid ANA group ID, so we start numbering at 1.
47272efd25dSChristoph Hellwig  *
47372efd25dSChristoph Hellwig  * ANA Group 1 exists without manual intervention, has namespaces assigned to it
47472efd25dSChristoph Hellwig  * by default, and is available in an optimized state through all ports.
47572efd25dSChristoph Hellwig  */
47662ac0d32SChristoph Hellwig #define NVMET_MAX_ANAGRPS	128
47772efd25dSChristoph Hellwig #define NVMET_DEFAULT_ANA_GRPID	1
47872efd25dSChristoph Hellwig 
479a07b4970SChristoph Hellwig #define NVMET_KAS		10
480f9362ac1SJay Sternberg #define NVMET_DISC_KATO_MS		120000
481a07b4970SChristoph Hellwig 
482a07b4970SChristoph Hellwig int __init nvmet_init_configfs(void);
483a07b4970SChristoph Hellwig void __exit nvmet_exit_configfs(void);
484a07b4970SChristoph Hellwig 
485a07b4970SChristoph Hellwig int __init nvmet_init_discovery(void);
486a07b4970SChristoph Hellwig void nvmet_exit_discovery(void);
487a07b4970SChristoph Hellwig 
488a07b4970SChristoph Hellwig extern struct nvmet_subsys *nvmet_disc_subsys;
489a07b4970SChristoph Hellwig extern struct rw_semaphore nvmet_config_sem;
490a07b4970SChristoph Hellwig 
49172efd25dSChristoph Hellwig extern u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
49272efd25dSChristoph Hellwig extern u64 nvmet_ana_chgcnt;
49372efd25dSChristoph Hellwig extern struct rw_semaphore nvmet_ana_sem;
49472efd25dSChristoph Hellwig 
495253928eeSSagi Grimberg bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn);
496a07b4970SChristoph Hellwig 
497d5eff33eSChaitanya Kulkarni int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
498d5eff33eSChaitanya Kulkarni int nvmet_file_ns_enable(struct nvmet_ns *ns);
499d5eff33eSChaitanya Kulkarni void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
500d5eff33eSChaitanya Kulkarni void nvmet_file_ns_disable(struct nvmet_ns *ns);
501dedf0be5SChaitanya Kulkarni u16 nvmet_bdev_flush(struct nvmet_req *req);
502dedf0be5SChaitanya Kulkarni u16 nvmet_file_flush(struct nvmet_req *req);
503dedf0be5SChaitanya Kulkarni void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
504e8cd1ff1SAnthony Iliopoulos void nvmet_bdev_ns_revalidate(struct nvmet_ns *ns);
505e8cd1ff1SAnthony Iliopoulos int nvmet_file_ns_revalidate(struct nvmet_ns *ns);
506463c5fabSChaitanya Kulkarni void nvmet_ns_revalidate(struct nvmet_ns *ns);
507d5eff33eSChaitanya Kulkarni 
508d5eff33eSChaitanya Kulkarni static inline u32 nvmet_rw_len(struct nvmet_req *req)
509d5eff33eSChaitanya Kulkarni {
510d5eff33eSChaitanya Kulkarni 	return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) <<
511d5eff33eSChaitanya Kulkarni 			req->ns->blksize_shift;
512d5eff33eSChaitanya Kulkarni }
513c6aa3542SChaitanya Kulkarni 
51459ef0eaaSChristoph Hellwig static inline u32 nvmet_dsm_len(struct nvmet_req *req)
51559ef0eaaSChristoph Hellwig {
51659ef0eaaSChristoph Hellwig 	return (le32_to_cpu(req->cmd->dsm.nr) + 1) *
51759ef0eaaSChristoph Hellwig 		sizeof(struct nvme_dsm_range);
51859ef0eaaSChristoph Hellwig }
51959ef0eaaSChristoph Hellwig 
520c6aa3542SChaitanya Kulkarni u16 errno_to_nvme_status(struct nvmet_req *req, int errno);
5219d05a96eSBart Van Assche 
5229d05a96eSBart Van Assche /* Convert a 32-bit number to a 16-bit 0's based number */
5239d05a96eSBart Van Assche static inline __le16 to0based(u32 a)
5249d05a96eSBart Van Assche {
5259d05a96eSBart Van Assche 	return cpu_to_le16(max(1U, min(1U << 16, a)) - 1);
5269d05a96eSBart Van Assche }
5279d05a96eSBart Van Assche 
528a07b4970SChristoph Hellwig #endif /* _NVMET_H */
529