xref: /openbmc/linux/drivers/nvme/target/nvmet.h (revision db1312dd95488b5e6ff362ff66fcf953a46b1821)
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 
24ba76af67SLogan Gunthorpe #define NVMET_DEFAULT_VS		NVME_VS(1, 3, 0)
25ba76af67SLogan Gunthorpe 
26a07b4970SChristoph Hellwig #define NVMET_ASYNC_EVENTS		4
27a07b4970SChristoph Hellwig #define NVMET_ERROR_LOG_SLOTS		128
285698b805SChaitanya Kulkarni #define NVMET_NO_ERROR_LOC		((u16)-1)
29013b7ebeSMark Ruijter #define NVMET_DEFAULT_CTRL_MODEL	"Linux"
3048b4c010SNoam Gottlieb #define NVMET_MN_MAX_SIZE		40
31e13b0615SNoam Gottlieb #define NVMET_SN_MAX_SIZE		20
32a07b4970SChristoph Hellwig 
33c86b8f7bSChristoph Hellwig /*
34c86b8f7bSChristoph Hellwig  * Supported optional AENs:
35c86b8f7bSChristoph Hellwig  */
36c86b8f7bSChristoph Hellwig #define NVMET_AEN_CFG_OPTIONAL \
3762ac0d32SChristoph Hellwig 	(NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_ANA_CHANGE)
38f301c2b1SJay Sternberg #define NVMET_DISC_AEN_CFG_OPTIONAL \
39f301c2b1SJay Sternberg 	(NVME_AEN_CFG_DISC_CHANGE)
40c86b8f7bSChristoph Hellwig 
41c86b8f7bSChristoph Hellwig /*
42c86b8f7bSChristoph Hellwig  * Plus mandatory SMART AENs (we'll never send them, but allow enabling them):
43c86b8f7bSChristoph Hellwig  */
44c86b8f7bSChristoph Hellwig #define NVMET_AEN_CFG_ALL \
45c86b8f7bSChristoph Hellwig 	(NVME_SMART_CRIT_SPARE | NVME_SMART_CRIT_TEMPERATURE | \
46c86b8f7bSChristoph Hellwig 	 NVME_SMART_CRIT_RELIABILITY | NVME_SMART_CRIT_MEDIA | \
47c86b8f7bSChristoph Hellwig 	 NVME_SMART_CRIT_VOLATILE_MEMORY | NVMET_AEN_CFG_OPTIONAL)
48c86b8f7bSChristoph Hellwig 
49a07b4970SChristoph Hellwig /* Helper Macros when NVMe error is NVME_SC_CONNECT_INVALID_PARAM
50a07b4970SChristoph Hellwig  * The 16 bit shift is to set IATTR bit to 1, which means offending
51a07b4970SChristoph Hellwig  * offset starts in the data section of connect()
52a07b4970SChristoph Hellwig  */
53a07b4970SChristoph Hellwig #define IPO_IATTR_CONNECT_DATA(x)	\
54a07b4970SChristoph Hellwig 	(cpu_to_le32((1 << 16) | (offsetof(struct nvmf_connect_data, x))))
55a07b4970SChristoph Hellwig #define IPO_IATTR_CONNECT_SQE(x)	\
56a07b4970SChristoph Hellwig 	(cpu_to_le32(offsetof(struct nvmf_connect_command, x)))
57a07b4970SChristoph Hellwig 
58a07b4970SChristoph Hellwig struct nvmet_ns {
59a07b4970SChristoph Hellwig 	struct percpu_ref	ref;
60a07b4970SChristoph Hellwig 	struct block_device	*bdev;
61d5eff33eSChaitanya Kulkarni 	struct file		*file;
62dedf0be5SChaitanya Kulkarni 	bool			readonly;
63a07b4970SChristoph Hellwig 	u32			nsid;
64a07b4970SChristoph Hellwig 	u32			blksize_shift;
65a07b4970SChristoph Hellwig 	loff_t			size;
66a07b4970SChristoph Hellwig 	u8			nguid[16];
67637dc0f3SJohannes Thumshirn 	uuid_t			uuid;
6872efd25dSChristoph Hellwig 	u32			anagrpid;
69a07b4970SChristoph Hellwig 
7055eb942eSChaitanya Kulkarni 	bool			buffered_io;
71e4fcf07cSSolganik Alexander 	bool			enabled;
72a07b4970SChristoph Hellwig 	struct nvmet_subsys	*subsys;
73a07b4970SChristoph Hellwig 	const char		*device_path;
74a07b4970SChristoph Hellwig 
75a07b4970SChristoph Hellwig 	struct config_group	device_group;
76a07b4970SChristoph Hellwig 	struct config_group	group;
77a07b4970SChristoph Hellwig 
78a07b4970SChristoph Hellwig 	struct completion	disable_done;
79d5eff33eSChaitanya Kulkarni 	mempool_t		*bvec_pool;
80d5eff33eSChaitanya Kulkarni 	struct kmem_cache	*bvec_cache;
81c6925093SLogan Gunthorpe 
82c6925093SLogan Gunthorpe 	int			use_p2pmem;
83c6925093SLogan Gunthorpe 	struct pci_dev		*p2p_dev;
84d2d1c454SIsrael Rukshin 	int			pi_type;
85d2d1c454SIsrael Rukshin 	int			metadata_size;
86ab5d0b38SChaitanya Kulkarni 	u8			csi;
87a07b4970SChristoph Hellwig };
88a07b4970SChristoph Hellwig 
89a07b4970SChristoph Hellwig static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
90a07b4970SChristoph Hellwig {
91a07b4970SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_ns, group);
92a07b4970SChristoph Hellwig }
93a07b4970SChristoph Hellwig 
94c6925093SLogan Gunthorpe static inline struct device *nvmet_ns_dev(struct nvmet_ns *ns)
95c6925093SLogan Gunthorpe {
96c6925093SLogan Gunthorpe 	return ns->bdev ? disk_to_dev(ns->bdev->bd_disk) : NULL;
97c6925093SLogan Gunthorpe }
98c6925093SLogan Gunthorpe 
99a07b4970SChristoph Hellwig struct nvmet_cq {
100a07b4970SChristoph Hellwig 	u16			qid;
101a07b4970SChristoph Hellwig 	u16			size;
102a07b4970SChristoph Hellwig };
103a07b4970SChristoph Hellwig 
104a07b4970SChristoph Hellwig struct nvmet_sq {
105a07b4970SChristoph Hellwig 	struct nvmet_ctrl	*ctrl;
106a07b4970SChristoph Hellwig 	struct percpu_ref	ref;
107a07b4970SChristoph Hellwig 	u16			qid;
108a07b4970SChristoph Hellwig 	u16			size;
109f9cf2a64SJames Smart 	u32			sqhd;
110e6a622fdSSagi Grimberg 	bool			sqhd_disabled;
111*db1312ddSHannes Reinecke #ifdef CONFIG_NVME_TARGET_AUTH
112*db1312ddSHannes Reinecke 	bool			authenticated;
113*db1312ddSHannes Reinecke 	u16			dhchap_tid;
114*db1312ddSHannes Reinecke 	u16			dhchap_status;
115*db1312ddSHannes Reinecke 	int			dhchap_step;
116*db1312ddSHannes Reinecke 	u8			*dhchap_c1;
117*db1312ddSHannes Reinecke 	u8			*dhchap_c2;
118*db1312ddSHannes Reinecke 	u32			dhchap_s1;
119*db1312ddSHannes Reinecke 	u32			dhchap_s2;
120*db1312ddSHannes Reinecke 	u8			*dhchap_skey;
121*db1312ddSHannes Reinecke 	int			dhchap_skey_len;
122*db1312ddSHannes Reinecke #endif
123a07b4970SChristoph Hellwig 	struct completion	free_done;
124427242ceSSagi Grimberg 	struct completion	confirm_done;
125a07b4970SChristoph Hellwig };
126a07b4970SChristoph Hellwig 
12762ac0d32SChristoph Hellwig struct nvmet_ana_group {
12862ac0d32SChristoph Hellwig 	struct config_group	group;
12962ac0d32SChristoph Hellwig 	struct nvmet_port	*port;
13062ac0d32SChristoph Hellwig 	u32			grpid;
13162ac0d32SChristoph Hellwig };
13262ac0d32SChristoph Hellwig 
13362ac0d32SChristoph Hellwig static inline struct nvmet_ana_group *to_ana_group(struct config_item *item)
13462ac0d32SChristoph Hellwig {
13562ac0d32SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_ana_group,
13662ac0d32SChristoph Hellwig 			group);
13762ac0d32SChristoph Hellwig }
13862ac0d32SChristoph Hellwig 
139a07b4970SChristoph Hellwig /**
140a07b4970SChristoph Hellwig  * struct nvmet_port -	Common structure to keep port
141a07b4970SChristoph Hellwig  *				information for the target.
142fe4a9791SChristoph Hellwig  * @entry:		Entry into referrals or transport list.
143a07b4970SChristoph Hellwig  * @disc_addr:		Address information is stored in a format defined
144a07b4970SChristoph Hellwig  *				for a discovery log page entry.
145a07b4970SChristoph Hellwig  * @group:		ConfigFS group for this element's folder.
146a07b4970SChristoph Hellwig  * @priv:		Private data for the transport.
147a07b4970SChristoph Hellwig  */
148a07b4970SChristoph Hellwig struct nvmet_port {
149a07b4970SChristoph Hellwig 	struct list_head		entry;
150a07b4970SChristoph Hellwig 	struct nvmf_disc_rsp_page_entry	disc_addr;
151a07b4970SChristoph Hellwig 	struct config_group		group;
152a07b4970SChristoph Hellwig 	struct config_group		subsys_group;
153a07b4970SChristoph Hellwig 	struct list_head		subsystems;
154a07b4970SChristoph Hellwig 	struct config_group		referrals_group;
155a07b4970SChristoph Hellwig 	struct list_head		referrals;
156b662a078SJay Sternberg 	struct list_head		global_entry;
15762ac0d32SChristoph Hellwig 	struct config_group		ana_groups_group;
15862ac0d32SChristoph Hellwig 	struct nvmet_ana_group		ana_default_group;
15972efd25dSChristoph Hellwig 	enum nvme_ana_state		*ana_state;
160a07b4970SChristoph Hellwig 	void				*priv;
161a07b4970SChristoph Hellwig 	bool				enabled;
1620d5ee2b2SSteve Wise 	int				inline_data_size;
1639d09dd8dSJames Smart 	const struct nvmet_fabrics_ops	*tr_ops;
164ea52ac1cSIsrael Rukshin 	bool				pi_enable;
165a07b4970SChristoph Hellwig };
166a07b4970SChristoph Hellwig 
167a07b4970SChristoph Hellwig static inline struct nvmet_port *to_nvmet_port(struct config_item *item)
168a07b4970SChristoph Hellwig {
169a07b4970SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_port,
170a07b4970SChristoph Hellwig 			group);
171a07b4970SChristoph Hellwig }
172a07b4970SChristoph Hellwig 
17362ac0d32SChristoph Hellwig static inline struct nvmet_port *ana_groups_to_port(
17462ac0d32SChristoph Hellwig 		struct config_item *item)
17562ac0d32SChristoph Hellwig {
17662ac0d32SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_port,
17762ac0d32SChristoph Hellwig 			ana_groups_group);
17862ac0d32SChristoph Hellwig }
17962ac0d32SChristoph Hellwig 
180a07b4970SChristoph Hellwig struct nvmet_ctrl {
181a07b4970SChristoph Hellwig 	struct nvmet_subsys	*subsys;
182a07b4970SChristoph Hellwig 	struct nvmet_sq		**sqs;
183a07b4970SChristoph Hellwig 
184aaeadd70SSagi Grimberg 	bool			reset_tbkas;
185c09305aeSSagi Grimberg 
186a07b4970SChristoph Hellwig 	struct mutex		lock;
187a07b4970SChristoph Hellwig 	u64			cap;
188a07b4970SChristoph Hellwig 	u32			cc;
189a07b4970SChristoph Hellwig 	u32			csts;
190a07b4970SChristoph Hellwig 
19128dd5cf7SOmri Mann 	uuid_t			hostid;
192a07b4970SChristoph Hellwig 	u16			cntlid;
193a07b4970SChristoph Hellwig 	u32			kato;
194a07b4970SChristoph Hellwig 
1954ee43280SChristoph Hellwig 	struct nvmet_port	*port;
1964ee43280SChristoph Hellwig 
197c86b8f7bSChristoph Hellwig 	u32			aen_enabled;
19855fdd6b6SChristoph Hellwig 	unsigned long		aen_masked;
199a07b4970SChristoph Hellwig 	struct nvmet_req	*async_event_cmds[NVMET_ASYNC_EVENTS];
200a07b4970SChristoph Hellwig 	unsigned int		nr_async_event_cmds;
201a07b4970SChristoph Hellwig 	struct list_head	async_events;
202a07b4970SChristoph Hellwig 	struct work_struct	async_event_work;
203a07b4970SChristoph Hellwig 
204a07b4970SChristoph Hellwig 	struct list_head	subsys_entry;
205a07b4970SChristoph Hellwig 	struct kref		ref;
206a07b4970SChristoph Hellwig 	struct delayed_work	ka_work;
207a07b4970SChristoph Hellwig 	struct work_struct	fatal_err_work;
208a07b4970SChristoph Hellwig 
209e929f06dSChristoph Hellwig 	const struct nvmet_fabrics_ops *ops;
210a07b4970SChristoph Hellwig 
211c16734eaSChristoph Hellwig 	__le32			*changed_ns_list;
212c16734eaSChristoph Hellwig 	u32			nr_changed_ns;
213c16734eaSChristoph Hellwig 
214a07b4970SChristoph Hellwig 	char			subsysnqn[NVMF_NQN_FIELD_LEN];
215a07b4970SChristoph Hellwig 	char			hostnqn[NVMF_NQN_FIELD_LEN];
216c6925093SLogan Gunthorpe 
217c6925093SLogan Gunthorpe 	struct device		*p2p_client;
218c6925093SLogan Gunthorpe 	struct radix_tree_root	p2p_ns_map;
219e4a97625SChaitanya Kulkarni 
220e4a97625SChaitanya Kulkarni 	spinlock_t		error_lock;
221e4a97625SChaitanya Kulkarni 	u64			err_counter;
222e4a97625SChaitanya Kulkarni 	struct nvme_error_slot	slots[NVMET_ERROR_LOG_SLOTS];
223ea52ac1cSIsrael Rukshin 	bool			pi_support;
224*db1312ddSHannes Reinecke #ifdef CONFIG_NVME_TARGET_AUTH
225*db1312ddSHannes Reinecke 	struct nvme_dhchap_key	*host_key;
226*db1312ddSHannes Reinecke 	struct nvme_dhchap_key	*ctrl_key;
227*db1312ddSHannes Reinecke 	u8			shash_id;
228*db1312ddSHannes Reinecke #endif
229a07b4970SChristoph Hellwig };
230a07b4970SChristoph Hellwig 
231a07b4970SChristoph Hellwig struct nvmet_subsys {
232a07b4970SChristoph Hellwig 	enum nvme_subsys_type	type;
233a07b4970SChristoph Hellwig 
234a07b4970SChristoph Hellwig 	struct mutex		lock;
235a07b4970SChristoph Hellwig 	struct kref		ref;
236a07b4970SChristoph Hellwig 
2377774e77eSChaitanya Kulkarni 	struct xarray		namespaces;
238793c7cfcSChristoph Hellwig 	unsigned int		nr_namespaces;
23986693c43SChaitanya Kulkarni 	u32			max_nsid;
24094a39d61SChaitanya Kulkarni 	u16			cntlid_min;
24194a39d61SChaitanya Kulkarni 	u16			cntlid_max;
242a07b4970SChristoph Hellwig 
243a07b4970SChristoph Hellwig 	struct list_head	ctrls;
244a07b4970SChristoph Hellwig 
245a07b4970SChristoph Hellwig 	struct list_head	hosts;
246a07b4970SChristoph Hellwig 	bool			allow_any_host;
247a07b4970SChristoph Hellwig 
248a07b4970SChristoph Hellwig 	u16			max_qid;
249a07b4970SChristoph Hellwig 
250a07b4970SChristoph Hellwig 	u64			ver;
251e13b0615SNoam Gottlieb 	char			serial[NVMET_SN_MAX_SIZE];
2527ae023c5SNoam Gottlieb 	bool			subsys_discovered;
253a07b4970SChristoph Hellwig 	char			*subsysnqn;
254ea52ac1cSIsrael Rukshin 	bool			pi_support;
255a07b4970SChristoph Hellwig 
256a07b4970SChristoph Hellwig 	struct config_group	group;
257a07b4970SChristoph Hellwig 
258a07b4970SChristoph Hellwig 	struct config_group	namespaces_group;
259a07b4970SChristoph Hellwig 	struct config_group	allowed_hosts_group;
260013b7ebeSMark Ruijter 
261d9f273b7SMax Gurtovoy 	char			*model_number;
262c1fef73fSLogan Gunthorpe 
263c1fef73fSLogan Gunthorpe #ifdef CONFIG_NVME_TARGET_PASSTHRU
264c1fef73fSLogan Gunthorpe 	struct nvme_ctrl	*passthru_ctrl;
265ba76af67SLogan Gunthorpe 	char			*passthru_ctrl_path;
266cae5b01aSLogan Gunthorpe 	struct config_group	passthru_group;
267a2f6a2b8SChaitanya Kulkarni 	unsigned int		admin_timeout;
26847e9730cSChaitanya Kulkarni 	unsigned int		io_timeout;
26934ad6151SAlan Adamson 	unsigned int		clear_ids;
270c1fef73fSLogan Gunthorpe #endif /* CONFIG_NVME_TARGET_PASSTHRU */
271aaf2e048SChaitanya Kulkarni 
272aaf2e048SChaitanya Kulkarni #ifdef CONFIG_BLK_DEV_ZONED
273aaf2e048SChaitanya Kulkarni 	u8			zasl;
274aaf2e048SChaitanya Kulkarni #endif /* CONFIG_BLK_DEV_ZONED */
275a07b4970SChristoph Hellwig };
276a07b4970SChristoph Hellwig 
277a07b4970SChristoph Hellwig static inline struct nvmet_subsys *to_subsys(struct config_item *item)
278a07b4970SChristoph Hellwig {
279a07b4970SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_subsys, group);
280a07b4970SChristoph Hellwig }
281a07b4970SChristoph Hellwig 
282a07b4970SChristoph Hellwig static inline struct nvmet_subsys *namespaces_to_subsys(
283a07b4970SChristoph Hellwig 		struct config_item *item)
284a07b4970SChristoph Hellwig {
285a07b4970SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_subsys,
286a07b4970SChristoph Hellwig 			namespaces_group);
287a07b4970SChristoph Hellwig }
288a07b4970SChristoph Hellwig 
289a07b4970SChristoph Hellwig struct nvmet_host {
290a07b4970SChristoph Hellwig 	struct config_group	group;
291*db1312ddSHannes Reinecke 	u8			*dhchap_secret;
292*db1312ddSHannes Reinecke 	u8			*dhchap_ctrl_secret;
293*db1312ddSHannes Reinecke 	u8			dhchap_key_hash;
294*db1312ddSHannes Reinecke 	u8			dhchap_ctrl_key_hash;
295*db1312ddSHannes Reinecke 	u8			dhchap_hash_id;
296*db1312ddSHannes Reinecke 	u8			dhchap_dhgroup_id;
297a07b4970SChristoph Hellwig };
298a07b4970SChristoph Hellwig 
299a07b4970SChristoph Hellwig static inline struct nvmet_host *to_host(struct config_item *item)
300a07b4970SChristoph Hellwig {
301a07b4970SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_host, group);
302a07b4970SChristoph Hellwig }
303a07b4970SChristoph Hellwig 
304a07b4970SChristoph Hellwig static inline char *nvmet_host_name(struct nvmet_host *host)
305a07b4970SChristoph Hellwig {
306a07b4970SChristoph Hellwig 	return config_item_name(&host->group.cg_item);
307a07b4970SChristoph Hellwig }
308a07b4970SChristoph Hellwig 
309a07b4970SChristoph Hellwig struct nvmet_host_link {
310a07b4970SChristoph Hellwig 	struct list_head	entry;
311a07b4970SChristoph Hellwig 	struct nvmet_host	*host;
312a07b4970SChristoph Hellwig };
313a07b4970SChristoph Hellwig 
314a07b4970SChristoph Hellwig struct nvmet_subsys_link {
315a07b4970SChristoph Hellwig 	struct list_head	entry;
316a07b4970SChristoph Hellwig 	struct nvmet_subsys	*subsys;
317a07b4970SChristoph Hellwig };
318a07b4970SChristoph Hellwig 
319a07b4970SChristoph Hellwig struct nvmet_req;
320a07b4970SChristoph Hellwig struct nvmet_fabrics_ops {
321a07b4970SChristoph Hellwig 	struct module *owner;
322a07b4970SChristoph Hellwig 	unsigned int type;
323a07b4970SChristoph Hellwig 	unsigned int msdbd;
3246fa350f7SMax Gurtovoy 	unsigned int flags;
3256fa350f7SMax Gurtovoy #define NVMF_KEYED_SGLS			(1 << 0)
3266fa350f7SMax Gurtovoy #define NVMF_METADATA_SUPPORTED		(1 << 1)
327a07b4970SChristoph Hellwig 	void (*queue_response)(struct nvmet_req *req);
328a07b4970SChristoph Hellwig 	int (*add_port)(struct nvmet_port *port);
329a07b4970SChristoph Hellwig 	void (*remove_port)(struct nvmet_port *port);
330a07b4970SChristoph Hellwig 	void (*delete_ctrl)(struct nvmet_ctrl *ctrl);
3314c652685SSagi Grimberg 	void (*disc_traddr)(struct nvmet_req *req,
3324c652685SSagi Grimberg 			struct nvmet_port *port, char *traddr);
3331672ddb8SSagi Grimberg 	u16 (*install_queue)(struct nvmet_sq *nvme_sq);
3349d09dd8dSJames Smart 	void (*discovery_chg)(struct nvmet_port *port);
33502cb00e2SMax Gurtovoy 	u8 (*get_mdts)(const struct nvmet_ctrl *ctrl);
3366d1555ccSMax Gurtovoy 	u16 (*get_max_queue_size)(const struct nvmet_ctrl *ctrl);
337a07b4970SChristoph Hellwig };
338a07b4970SChristoph Hellwig 
339a07b4970SChristoph Hellwig #define NVMET_MAX_INLINE_BIOVEC	8
34073383adfSSagi Grimberg #define NVMET_MAX_INLINE_DATA_LEN NVMET_MAX_INLINE_BIOVEC * PAGE_SIZE
341a07b4970SChristoph Hellwig 
342a07b4970SChristoph Hellwig struct nvmet_req {
343a07b4970SChristoph Hellwig 	struct nvme_command	*cmd;
344fc6c9730SMax Gurtovoy 	struct nvme_completion	*cqe;
345a07b4970SChristoph Hellwig 	struct nvmet_sq		*sq;
346a07b4970SChristoph Hellwig 	struct nvmet_cq		*cq;
347a07b4970SChristoph Hellwig 	struct nvmet_ns		*ns;
348a07b4970SChristoph Hellwig 	struct scatterlist	*sg;
349c6e3f133SIsrael Rukshin 	struct scatterlist	*metadata_sg;
350a07b4970SChristoph Hellwig 	struct bio_vec		inline_bvec[NVMET_MAX_INLINE_BIOVEC];
351d5eff33eSChaitanya Kulkarni 	union {
352d5eff33eSChaitanya Kulkarni 		struct {
353d5eff33eSChaitanya Kulkarni 			struct bio      inline_bio;
354d5eff33eSChaitanya Kulkarni 		} b;
355d5eff33eSChaitanya Kulkarni 		struct {
356d5eff33eSChaitanya Kulkarni 			bool			mpool_alloc;
357d5eff33eSChaitanya Kulkarni 			struct kiocb            iocb;
358d5eff33eSChaitanya Kulkarni 			struct bio_vec          *bvec;
359d5eff33eSChaitanya Kulkarni 			struct work_struct      work;
360d5eff33eSChaitanya Kulkarni 		} f;
361c1fef73fSLogan Gunthorpe 		struct {
362dab3902bSChaitanya Kulkarni 			struct bio		inline_bio;
363c1fef73fSLogan Gunthorpe 			struct request		*rq;
364c1fef73fSLogan Gunthorpe 			struct work_struct      work;
365c1fef73fSLogan Gunthorpe 			bool			use_workqueue;
366c1fef73fSLogan Gunthorpe 		} p;
367aaf2e048SChaitanya Kulkarni #ifdef CONFIG_BLK_DEV_ZONED
368aaf2e048SChaitanya Kulkarni 		struct {
369aaf2e048SChaitanya Kulkarni 			struct bio		inline_bio;
370aaf2e048SChaitanya Kulkarni 			struct work_struct	zmgmt_work;
371aaf2e048SChaitanya Kulkarni 		} z;
372aaf2e048SChaitanya Kulkarni #endif /* CONFIG_BLK_DEV_ZONED */
373d5eff33eSChaitanya Kulkarni 	};
374a07b4970SChristoph Hellwig 	int			sg_cnt;
375c6e3f133SIsrael Rukshin 	int			metadata_sg_cnt;
3765e62d5c9SChristoph Hellwig 	/* data length as parsed from the SGL descriptor: */
3775e62d5c9SChristoph Hellwig 	size_t			transfer_len;
378c6e3f133SIsrael Rukshin 	size_t			metadata_len;
379a07b4970SChristoph Hellwig 
380a07b4970SChristoph Hellwig 	struct nvmet_port	*port;
381a07b4970SChristoph Hellwig 
382a07b4970SChristoph Hellwig 	void (*execute)(struct nvmet_req *req);
383e929f06dSChristoph Hellwig 	const struct nvmet_fabrics_ops *ops;
384c6925093SLogan Gunthorpe 
385c6925093SLogan Gunthorpe 	struct pci_dev		*p2p_dev;
386c6925093SLogan Gunthorpe 	struct device		*p2p_client;
387e4a97625SChaitanya Kulkarni 	u16			error_loc;
388e4a97625SChaitanya Kulkarni 	u64			error_slba;
389a07b4970SChristoph Hellwig };
390a07b4970SChristoph Hellwig 
39155eb942eSChaitanya Kulkarni extern struct workqueue_struct *buffered_io_wq;
392aaf2e048SChaitanya Kulkarni extern struct workqueue_struct *zbd_wq;
3938832cf92SSagi Grimberg extern struct workqueue_struct *nvmet_wq;
39455eb942eSChaitanya Kulkarni 
395a07b4970SChristoph Hellwig static inline void nvmet_set_result(struct nvmet_req *req, u32 result)
396a07b4970SChristoph Hellwig {
397fc6c9730SMax Gurtovoy 	req->cqe->result.u32 = cpu_to_le32(result);
398a07b4970SChristoph Hellwig }
399a07b4970SChristoph Hellwig 
400a07b4970SChristoph Hellwig /*
401a07b4970SChristoph Hellwig  * NVMe command writes actually are DMA reads for us on the target side.
402a07b4970SChristoph Hellwig  */
403a07b4970SChristoph Hellwig static inline enum dma_data_direction
404a07b4970SChristoph Hellwig nvmet_data_dir(struct nvmet_req *req)
405a07b4970SChristoph Hellwig {
406a07b4970SChristoph Hellwig 	return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
407a07b4970SChristoph Hellwig }
408a07b4970SChristoph Hellwig 
409a07b4970SChristoph Hellwig struct nvmet_async_event {
410a07b4970SChristoph Hellwig 	struct list_head	entry;
411a07b4970SChristoph Hellwig 	u8			event_type;
412a07b4970SChristoph Hellwig 	u8			event_info;
413a07b4970SChristoph Hellwig 	u8			log_page;
414a07b4970SChristoph Hellwig };
415a07b4970SChristoph Hellwig 
4167114ddebSJay Sternberg static inline void nvmet_clear_aen_bit(struct nvmet_req *req, u32 bn)
4176c8312adSJay Sternberg {
418b7c8f366SChaitanya Kulkarni 	int rae = le32_to_cpu(req->cmd->common.cdw10) & 1 << 15;
4196c8312adSJay Sternberg 
4206c8312adSJay Sternberg 	if (!rae)
4217114ddebSJay Sternberg 		clear_bit(bn, &req->sq->ctrl->aen_masked);
4226c8312adSJay Sternberg }
4236c8312adSJay Sternberg 
4247114ddebSJay Sternberg static inline bool nvmet_aen_bit_disabled(struct nvmet_ctrl *ctrl, u32 bn)
4256c8312adSJay Sternberg {
4267114ddebSJay Sternberg 	if (!(READ_ONCE(ctrl->aen_enabled) & (1 << bn)))
4276c8312adSJay Sternberg 		return true;
4287114ddebSJay Sternberg 	return test_and_set_bit(bn, &ctrl->aen_masked);
4296c8312adSJay Sternberg }
4306c8312adSJay Sternberg 
43190107455SJay Sternberg void nvmet_get_feat_kato(struct nvmet_req *req);
43290107455SJay Sternberg void nvmet_get_feat_async_event(struct nvmet_req *req);
43390107455SJay Sternberg u16 nvmet_set_feat_kato(struct nvmet_req *req);
43490107455SJay Sternberg u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask);
43590107455SJay Sternberg void nvmet_execute_async_event(struct nvmet_req *req);
4364e683c48SAmit Engel void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl);
4374e683c48SAmit Engel void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl);
43890107455SJay Sternberg 
43964a0ca88SParav Pandit u16 nvmet_parse_connect_cmd(struct nvmet_req *req);
4409d05a96eSBart Van Assche void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id);
441d5eff33eSChaitanya Kulkarni u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req);
442d5eff33eSChaitanya Kulkarni u16 nvmet_file_parse_io_cmd(struct nvmet_req *req);
443aaf2e048SChaitanya Kulkarni u16 nvmet_bdev_zns_parse_io_cmd(struct nvmet_req *req);
44464a0ca88SParav Pandit u16 nvmet_parse_admin_cmd(struct nvmet_req *req);
44564a0ca88SParav Pandit u16 nvmet_parse_discovery_cmd(struct nvmet_req *req);
4466490c9edSHannes Reinecke u16 nvmet_parse_fabrics_admin_cmd(struct nvmet_req *req);
4476490c9edSHannes Reinecke u16 nvmet_parse_fabrics_io_cmd(struct nvmet_req *req);
448a07b4970SChristoph Hellwig 
449a07b4970SChristoph Hellwig bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
450e929f06dSChristoph Hellwig 		struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops);
451549f01aeSVijay Immanuel void nvmet_req_uninit(struct nvmet_req *req);
452136cc1ffSIsrael Rukshin bool nvmet_check_transfer_len(struct nvmet_req *req, size_t len);
453b716e688SSagi Grimberg bool nvmet_check_data_len_lte(struct nvmet_req *req, size_t data_len);
454a07b4970SChristoph Hellwig void nvmet_req_complete(struct nvmet_req *req, u16 status);
455c6e3f133SIsrael Rukshin int nvmet_req_alloc_sgls(struct nvmet_req *req);
456c6e3f133SIsrael Rukshin void nvmet_req_free_sgls(struct nvmet_req *req);
457a07b4970SChristoph Hellwig 
458c1fef73fSLogan Gunthorpe void nvmet_execute_set_features(struct nvmet_req *req);
459c1fef73fSLogan Gunthorpe void nvmet_execute_get_features(struct nvmet_req *req);
460f9362ac1SJay Sternberg void nvmet_execute_keep_alive(struct nvmet_req *req);
461f9362ac1SJay Sternberg 
462a07b4970SChristoph Hellwig void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid,
463a07b4970SChristoph Hellwig 		u16 size);
464a07b4970SChristoph Hellwig void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid,
465a07b4970SChristoph Hellwig 		u16 size);
466a07b4970SChristoph Hellwig void nvmet_sq_destroy(struct nvmet_sq *sq);
467a07b4970SChristoph Hellwig int nvmet_sq_init(struct nvmet_sq *sq);
468a07b4970SChristoph Hellwig 
469a07b4970SChristoph Hellwig void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl);
470a07b4970SChristoph Hellwig 
471a07b4970SChristoph Hellwig void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new);
472a07b4970SChristoph Hellwig u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
473a07b4970SChristoph Hellwig 		struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp);
474de587804SChaitanya Kulkarni struct nvmet_ctrl *nvmet_ctrl_find_get(const char *subsysnqn,
475de587804SChaitanya Kulkarni 				       const char *hostnqn, u16 cntlid,
476de587804SChaitanya Kulkarni 				       struct nvmet_req *req);
477a07b4970SChristoph Hellwig void nvmet_ctrl_put(struct nvmet_ctrl *ctrl);
4787798df6fSChaitanya Kulkarni u16 nvmet_check_ctrl_status(struct nvmet_req *req);
479a07b4970SChristoph Hellwig 
480a07b4970SChristoph Hellwig struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
481a07b4970SChristoph Hellwig 		enum nvme_subsys_type type);
482a07b4970SChristoph Hellwig void nvmet_subsys_put(struct nvmet_subsys *subsys);
483344770b0SSagi Grimberg void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys);
484a07b4970SChristoph Hellwig 
4853a1f7c79SChaitanya Kulkarni u16 nvmet_req_find_ns(struct nvmet_req *req);
486a07b4970SChristoph Hellwig void nvmet_put_namespace(struct nvmet_ns *ns);
487a07b4970SChristoph Hellwig int nvmet_ns_enable(struct nvmet_ns *ns);
488a07b4970SChristoph Hellwig void nvmet_ns_disable(struct nvmet_ns *ns);
489a07b4970SChristoph Hellwig struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
490a07b4970SChristoph Hellwig void nvmet_ns_free(struct nvmet_ns *ns);
491a07b4970SChristoph Hellwig 
49262ac0d32SChristoph Hellwig void nvmet_send_ana_event(struct nvmet_subsys *subsys,
49362ac0d32SChristoph Hellwig 		struct nvmet_port *port);
49462ac0d32SChristoph Hellwig void nvmet_port_send_ana_event(struct nvmet_port *port);
49562ac0d32SChristoph Hellwig 
496e929f06dSChristoph Hellwig int nvmet_register_transport(const struct nvmet_fabrics_ops *ops);
497e929f06dSChristoph Hellwig void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops);
498a07b4970SChristoph Hellwig 
4993aed8673SLogan Gunthorpe void nvmet_port_del_ctrls(struct nvmet_port *port,
5003aed8673SLogan Gunthorpe 			  struct nvmet_subsys *subsys);
5013aed8673SLogan Gunthorpe 
502a07b4970SChristoph Hellwig int nvmet_enable_port(struct nvmet_port *port);
503a07b4970SChristoph Hellwig void nvmet_disable_port(struct nvmet_port *port);
504a07b4970SChristoph Hellwig 
505a07b4970SChristoph Hellwig void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port);
506b662a078SJay Sternberg void nvmet_referral_disable(struct nvmet_port *parent, struct nvmet_port *port);
507a07b4970SChristoph Hellwig 
508a07b4970SChristoph Hellwig u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
509a07b4970SChristoph Hellwig 		size_t len);
510a07b4970SChristoph Hellwig u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf,
511a07b4970SChristoph Hellwig 		size_t len);
512c7759fffSChristoph Hellwig u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len);
513a07b4970SChristoph Hellwig 
514a07b4970SChristoph Hellwig u32 nvmet_get_log_page_len(struct nvme_command *cmd);
515d808b7f7SKeith Busch u64 nvmet_get_log_page_offset(struct nvme_command *cmd);
516a07b4970SChristoph Hellwig 
517b662a078SJay Sternberg extern struct list_head *nvmet_ports;
518b662a078SJay Sternberg void nvmet_port_disc_changed(struct nvmet_port *port,
519b662a078SJay Sternberg 		struct nvmet_subsys *subsys);
520b662a078SJay Sternberg void nvmet_subsys_disc_changed(struct nvmet_subsys *subsys,
521b662a078SJay Sternberg 		struct nvmet_host *host);
522b662a078SJay Sternberg void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
523b662a078SJay Sternberg 		u8 event_info, u8 log_page);
524b662a078SJay Sternberg 
525a07b4970SChristoph Hellwig #define NVMET_QUEUE_SIZE	1024
5263375e29cSJames Smart #define NVMET_NR_QUEUES		128
527a07b4970SChristoph Hellwig #define NVMET_MAX_CMD		NVMET_QUEUE_SIZE
528793c7cfcSChristoph Hellwig 
529793c7cfcSChristoph Hellwig /*
530793c7cfcSChristoph Hellwig  * Nice round number that makes a list of nsids fit into a page.
531793c7cfcSChristoph Hellwig  * Should become tunable at some point in the future.
532793c7cfcSChristoph Hellwig  */
533793c7cfcSChristoph Hellwig #define NVMET_MAX_NAMESPACES	1024
534793c7cfcSChristoph Hellwig 
53572efd25dSChristoph Hellwig /*
53672efd25dSChristoph Hellwig  * 0 is not a valid ANA group ID, so we start numbering at 1.
53772efd25dSChristoph Hellwig  *
53872efd25dSChristoph Hellwig  * ANA Group 1 exists without manual intervention, has namespaces assigned to it
53972efd25dSChristoph Hellwig  * by default, and is available in an optimized state through all ports.
54072efd25dSChristoph Hellwig  */
54162ac0d32SChristoph Hellwig #define NVMET_MAX_ANAGRPS	128
54272efd25dSChristoph Hellwig #define NVMET_DEFAULT_ANA_GRPID	1
54372efd25dSChristoph Hellwig 
544a07b4970SChristoph Hellwig #define NVMET_KAS		10
545f9362ac1SJay Sternberg #define NVMET_DISC_KATO_MS		120000
546a07b4970SChristoph Hellwig 
547a07b4970SChristoph Hellwig int __init nvmet_init_configfs(void);
548a07b4970SChristoph Hellwig void __exit nvmet_exit_configfs(void);
549a07b4970SChristoph Hellwig 
550a07b4970SChristoph Hellwig int __init nvmet_init_discovery(void);
551a07b4970SChristoph Hellwig void nvmet_exit_discovery(void);
552a07b4970SChristoph Hellwig 
553a07b4970SChristoph Hellwig extern struct nvmet_subsys *nvmet_disc_subsys;
554a07b4970SChristoph Hellwig extern struct rw_semaphore nvmet_config_sem;
555a07b4970SChristoph Hellwig 
55672efd25dSChristoph Hellwig extern u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
55772efd25dSChristoph Hellwig extern u64 nvmet_ana_chgcnt;
55872efd25dSChristoph Hellwig extern struct rw_semaphore nvmet_ana_sem;
55972efd25dSChristoph Hellwig 
560253928eeSSagi Grimberg bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn);
561a07b4970SChristoph Hellwig 
562d5eff33eSChaitanya Kulkarni int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
563d5eff33eSChaitanya Kulkarni int nvmet_file_ns_enable(struct nvmet_ns *ns);
564d5eff33eSChaitanya Kulkarni void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
565d5eff33eSChaitanya Kulkarni void nvmet_file_ns_disable(struct nvmet_ns *ns);
566dedf0be5SChaitanya Kulkarni u16 nvmet_bdev_flush(struct nvmet_req *req);
567dedf0be5SChaitanya Kulkarni u16 nvmet_file_flush(struct nvmet_req *req);
568dedf0be5SChaitanya Kulkarni void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
569e8cd1ff1SAnthony Iliopoulos void nvmet_bdev_ns_revalidate(struct nvmet_ns *ns);
5702caecd62SChaitanya Kulkarni void nvmet_file_ns_revalidate(struct nvmet_ns *ns);
571da783733SChristoph Hellwig bool nvmet_ns_revalidate(struct nvmet_ns *ns);
572aaf2e048SChaitanya Kulkarni u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts);
573aaf2e048SChaitanya Kulkarni 
574aaf2e048SChaitanya Kulkarni bool nvmet_bdev_zns_enable(struct nvmet_ns *ns);
575aaf2e048SChaitanya Kulkarni void nvmet_execute_identify_cns_cs_ctrl(struct nvmet_req *req);
576aaf2e048SChaitanya Kulkarni void nvmet_execute_identify_cns_cs_ns(struct nvmet_req *req);
577aaf2e048SChaitanya Kulkarni void nvmet_bdev_execute_zone_mgmt_recv(struct nvmet_req *req);
578aaf2e048SChaitanya Kulkarni void nvmet_bdev_execute_zone_mgmt_send(struct nvmet_req *req);
579aaf2e048SChaitanya Kulkarni void nvmet_bdev_execute_zone_append(struct nvmet_req *req);
580d5eff33eSChaitanya Kulkarni 
58126af180cSIsrael Rukshin static inline u32 nvmet_rw_data_len(struct nvmet_req *req)
582d5eff33eSChaitanya Kulkarni {
583d5eff33eSChaitanya Kulkarni 	return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) <<
584d5eff33eSChaitanya Kulkarni 			req->ns->blksize_shift;
585d5eff33eSChaitanya Kulkarni }
586c6aa3542SChaitanya Kulkarni 
587ea52ac1cSIsrael Rukshin static inline u32 nvmet_rw_metadata_len(struct nvmet_req *req)
588ea52ac1cSIsrael Rukshin {
589ea52ac1cSIsrael Rukshin 	if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
590ea52ac1cSIsrael Rukshin 		return 0;
591ea52ac1cSIsrael Rukshin 	return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) *
592ea52ac1cSIsrael Rukshin 			req->ns->metadata_size;
593ea52ac1cSIsrael Rukshin }
594ea52ac1cSIsrael Rukshin 
59559ef0eaaSChristoph Hellwig static inline u32 nvmet_dsm_len(struct nvmet_req *req)
59659ef0eaaSChristoph Hellwig {
59759ef0eaaSChristoph Hellwig 	return (le32_to_cpu(req->cmd->dsm.nr) + 1) *
59859ef0eaaSChristoph Hellwig 		sizeof(struct nvme_dsm_range);
59959ef0eaaSChristoph Hellwig }
60059ef0eaaSChristoph Hellwig 
60120c2c3bbSChaitanya Kulkarni static inline struct nvmet_subsys *nvmet_req_subsys(struct nvmet_req *req)
60220c2c3bbSChaitanya Kulkarni {
60320c2c3bbSChaitanya Kulkarni 	return req->sq->ctrl->subsys;
60420c2c3bbSChaitanya Kulkarni }
60520c2c3bbSChaitanya Kulkarni 
606a294711eSHannes Reinecke static inline bool nvmet_is_disc_subsys(struct nvmet_subsys *subsys)
607a294711eSHannes Reinecke {
608598e7593SHannes Reinecke     return subsys->type != NVME_NQN_NVME;
609a294711eSHannes Reinecke }
610a294711eSHannes Reinecke 
611c1fef73fSLogan Gunthorpe #ifdef CONFIG_NVME_TARGET_PASSTHRU
612ba76af67SLogan Gunthorpe void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys);
613ba76af67SLogan Gunthorpe int nvmet_passthru_ctrl_enable(struct nvmet_subsys *subsys);
614ba76af67SLogan Gunthorpe void nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys);
615c1fef73fSLogan Gunthorpe u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req);
616c1fef73fSLogan Gunthorpe u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req);
617ab7a2737SChristoph Hellwig static inline bool nvmet_is_passthru_subsys(struct nvmet_subsys *subsys)
618c1fef73fSLogan Gunthorpe {
619c1fef73fSLogan Gunthorpe 	return subsys->passthru_ctrl;
620c1fef73fSLogan Gunthorpe }
621c1fef73fSLogan Gunthorpe #else /* CONFIG_NVME_TARGET_PASSTHRU */
622ba76af67SLogan Gunthorpe static inline void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys)
623ba76af67SLogan Gunthorpe {
624ba76af67SLogan Gunthorpe }
625ba76af67SLogan Gunthorpe static inline void nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys)
626ba76af67SLogan Gunthorpe {
627ba76af67SLogan Gunthorpe }
628c1fef73fSLogan Gunthorpe static inline u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req)
629c1fef73fSLogan Gunthorpe {
630c1fef73fSLogan Gunthorpe 	return 0;
631c1fef73fSLogan Gunthorpe }
632c1fef73fSLogan Gunthorpe static inline u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req)
633c1fef73fSLogan Gunthorpe {
634c1fef73fSLogan Gunthorpe 	return 0;
635c1fef73fSLogan Gunthorpe }
636ab7a2737SChristoph Hellwig static inline bool nvmet_is_passthru_subsys(struct nvmet_subsys *subsys)
637c1fef73fSLogan Gunthorpe {
638c1fef73fSLogan Gunthorpe 	return NULL;
639c1fef73fSLogan Gunthorpe }
640c1fef73fSLogan Gunthorpe #endif /* CONFIG_NVME_TARGET_PASSTHRU */
641c1fef73fSLogan Gunthorpe 
642ab7a2737SChristoph Hellwig static inline bool nvmet_is_passthru_req(struct nvmet_req *req)
643c1fef73fSLogan Gunthorpe {
644ab7a2737SChristoph Hellwig 	return nvmet_is_passthru_subsys(nvmet_req_subsys(req));
645c1fef73fSLogan Gunthorpe }
646c1fef73fSLogan Gunthorpe 
64777d651a6SAdam Manzanares void nvmet_passthrough_override_cap(struct nvmet_ctrl *ctrl);
64877d651a6SAdam Manzanares 
649c6aa3542SChaitanya Kulkarni u16 errno_to_nvme_status(struct nvmet_req *req, int errno);
650d81d57cfSChaitanya Kulkarni u16 nvmet_report_invalid_opcode(struct nvmet_req *req);
6519d05a96eSBart Van Assche 
6529d05a96eSBart Van Assche /* Convert a 32-bit number to a 16-bit 0's based number */
6539d05a96eSBart Van Assche static inline __le16 to0based(u32 a)
6549d05a96eSBart Van Assche {
6559d05a96eSBart Van Assche 	return cpu_to_le16(max(1U, min(1U << 16, a)) - 1);
6569d05a96eSBart Van Assche }
6579d05a96eSBart Van Assche 
658ea52ac1cSIsrael Rukshin static inline bool nvmet_ns_has_pi(struct nvmet_ns *ns)
659ea52ac1cSIsrael Rukshin {
660ea52ac1cSIsrael Rukshin 	if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
661ea52ac1cSIsrael Rukshin 		return false;
662ea52ac1cSIsrael Rukshin 	return ns->pi_type && ns->metadata_size == sizeof(struct t10_pi_tuple);
663ea52ac1cSIsrael Rukshin }
664ea52ac1cSIsrael Rukshin 
665193fcf37SChaitanya Kulkarni static inline __le64 nvmet_sect_to_lba(struct nvmet_ns *ns, sector_t sect)
666193fcf37SChaitanya Kulkarni {
667193fcf37SChaitanya Kulkarni 	return cpu_to_le64(sect >> (ns->blksize_shift - SECTOR_SHIFT));
668193fcf37SChaitanya Kulkarni }
669193fcf37SChaitanya Kulkarni 
670193fcf37SChaitanya Kulkarni static inline sector_t nvmet_lba_to_sect(struct nvmet_ns *ns, __le64 lba)
671193fcf37SChaitanya Kulkarni {
672193fcf37SChaitanya Kulkarni 	return le64_to_cpu(lba) << (ns->blksize_shift - SECTOR_SHIFT);
673193fcf37SChaitanya Kulkarni }
674193fcf37SChaitanya Kulkarni 
675608a9690SChaitanya Kulkarni static inline bool nvmet_use_inline_bvec(struct nvmet_req *req)
676608a9690SChaitanya Kulkarni {
677608a9690SChaitanya Kulkarni 	return req->transfer_len <= NVMET_MAX_INLINE_DATA_LEN &&
678608a9690SChaitanya Kulkarni 	       req->sg_cnt <= NVMET_MAX_INLINE_BIOVEC;
679608a9690SChaitanya Kulkarni }
680608a9690SChaitanya Kulkarni 
6816e597263SChaitanya Kulkarni static inline void nvmet_req_cns_error_complete(struct nvmet_req *req)
6826e597263SChaitanya Kulkarni {
6836e597263SChaitanya Kulkarni 	pr_debug("unhandled identify cns %d on qid %d\n",
6846e597263SChaitanya Kulkarni 	       req->cmd->identify.cns, req->sq->qid);
6856e597263SChaitanya Kulkarni 	req->error_loc = offsetof(struct nvme_identify, cns);
6866e597263SChaitanya Kulkarni 	nvmet_req_complete(req, NVME_SC_INVALID_FIELD | NVME_SC_DNR);
6876e597263SChaitanya Kulkarni }
6886e597263SChaitanya Kulkarni 
6899a01b58cSChaitanya Kulkarni static inline void nvmet_req_bio_put(struct nvmet_req *req, struct bio *bio)
6909a01b58cSChaitanya Kulkarni {
6919a01b58cSChaitanya Kulkarni 	if (bio != &req->b.inline_bio)
6929a01b58cSChaitanya Kulkarni 		bio_put(bio);
6939a01b58cSChaitanya Kulkarni }
6949a01b58cSChaitanya Kulkarni 
695*db1312ddSHannes Reinecke #ifdef CONFIG_NVME_TARGET_AUTH
696*db1312ddSHannes Reinecke void nvmet_execute_auth_send(struct nvmet_req *req);
697*db1312ddSHannes Reinecke void nvmet_execute_auth_receive(struct nvmet_req *req);
698*db1312ddSHannes Reinecke int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
699*db1312ddSHannes Reinecke 		       bool set_ctrl);
700*db1312ddSHannes Reinecke int nvmet_auth_set_host_hash(struct nvmet_host *host, const char *hash);
701*db1312ddSHannes Reinecke int nvmet_setup_auth(struct nvmet_ctrl *ctrl);
702*db1312ddSHannes Reinecke void nvmet_init_auth(struct nvmet_ctrl *ctrl, struct nvmet_req *req);
703*db1312ddSHannes Reinecke void nvmet_destroy_auth(struct nvmet_ctrl *ctrl);
704*db1312ddSHannes Reinecke void nvmet_auth_sq_free(struct nvmet_sq *sq);
705*db1312ddSHannes Reinecke bool nvmet_check_auth_status(struct nvmet_req *req);
706*db1312ddSHannes Reinecke int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response,
707*db1312ddSHannes Reinecke 			 unsigned int hash_len);
708*db1312ddSHannes Reinecke int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response,
709*db1312ddSHannes Reinecke 			 unsigned int hash_len);
710*db1312ddSHannes Reinecke static inline bool nvmet_has_auth(struct nvmet_ctrl *ctrl)
711*db1312ddSHannes Reinecke {
712*db1312ddSHannes Reinecke 	return ctrl->host_key != NULL;
713*db1312ddSHannes Reinecke }
714*db1312ddSHannes Reinecke #else
715*db1312ddSHannes Reinecke static inline int nvmet_setup_auth(struct nvmet_ctrl *ctrl)
716*db1312ddSHannes Reinecke {
717*db1312ddSHannes Reinecke 	return 0;
718*db1312ddSHannes Reinecke }
719*db1312ddSHannes Reinecke static inline void nvmet_init_auth(struct nvmet_ctrl *ctrl,
720*db1312ddSHannes Reinecke 				   struct nvmet_req *req) {};
721*db1312ddSHannes Reinecke static inline void nvmet_destroy_auth(struct nvmet_ctrl *ctrl) {};
722*db1312ddSHannes Reinecke static inline void nvmet_auth_sq_free(struct nvmet_sq *sq) {};
723*db1312ddSHannes Reinecke static inline bool nvmet_check_auth_status(struct nvmet_req *req)
724*db1312ddSHannes Reinecke {
725*db1312ddSHannes Reinecke 	return true;
726*db1312ddSHannes Reinecke }
727*db1312ddSHannes Reinecke static inline bool nvmet_has_auth(struct nvmet_ctrl *ctrl)
728*db1312ddSHannes Reinecke {
729*db1312ddSHannes Reinecke 	return false;
730*db1312ddSHannes Reinecke }
731*db1312ddSHannes Reinecke static inline const char *nvmet_dhchap_dhgroup_name(u8 dhgid) { return NULL; }
732*db1312ddSHannes Reinecke #endif
733*db1312ddSHannes Reinecke 
734a07b4970SChristoph Hellwig #endif /* _NVMET_H */
735