xref: /openbmc/linux/drivers/nvme/target/nvmet.h (revision aaf2e048af2704da5869f27b508b288f36d5c7b7)
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;
111a07b4970SChristoph Hellwig 	struct completion	free_done;
112427242ceSSagi Grimberg 	struct completion	confirm_done;
113a07b4970SChristoph Hellwig };
114a07b4970SChristoph Hellwig 
11562ac0d32SChristoph Hellwig struct nvmet_ana_group {
11662ac0d32SChristoph Hellwig 	struct config_group	group;
11762ac0d32SChristoph Hellwig 	struct nvmet_port	*port;
11862ac0d32SChristoph Hellwig 	u32			grpid;
11962ac0d32SChristoph Hellwig };
12062ac0d32SChristoph Hellwig 
12162ac0d32SChristoph Hellwig static inline struct nvmet_ana_group *to_ana_group(struct config_item *item)
12262ac0d32SChristoph Hellwig {
12362ac0d32SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_ana_group,
12462ac0d32SChristoph Hellwig 			group);
12562ac0d32SChristoph Hellwig }
12662ac0d32SChristoph Hellwig 
127a07b4970SChristoph Hellwig /**
128a07b4970SChristoph Hellwig  * struct nvmet_port -	Common structure to keep port
129a07b4970SChristoph Hellwig  *				information for the target.
130fe4a9791SChristoph Hellwig  * @entry:		Entry into referrals or transport list.
131a07b4970SChristoph Hellwig  * @disc_addr:		Address information is stored in a format defined
132a07b4970SChristoph Hellwig  *				for a discovery log page entry.
133a07b4970SChristoph Hellwig  * @group:		ConfigFS group for this element's folder.
134a07b4970SChristoph Hellwig  * @priv:		Private data for the transport.
135a07b4970SChristoph Hellwig  */
136a07b4970SChristoph Hellwig struct nvmet_port {
137a07b4970SChristoph Hellwig 	struct list_head		entry;
138a07b4970SChristoph Hellwig 	struct nvmf_disc_rsp_page_entry	disc_addr;
139a07b4970SChristoph Hellwig 	struct config_group		group;
140a07b4970SChristoph Hellwig 	struct config_group		subsys_group;
141a07b4970SChristoph Hellwig 	struct list_head		subsystems;
142a07b4970SChristoph Hellwig 	struct config_group		referrals_group;
143a07b4970SChristoph Hellwig 	struct list_head		referrals;
144b662a078SJay Sternberg 	struct list_head		global_entry;
14562ac0d32SChristoph Hellwig 	struct config_group		ana_groups_group;
14662ac0d32SChristoph Hellwig 	struct nvmet_ana_group		ana_default_group;
14772efd25dSChristoph Hellwig 	enum nvme_ana_state		*ana_state;
148a07b4970SChristoph Hellwig 	void				*priv;
149a07b4970SChristoph Hellwig 	bool				enabled;
1500d5ee2b2SSteve Wise 	int				inline_data_size;
1519d09dd8dSJames Smart 	const struct nvmet_fabrics_ops	*tr_ops;
152ea52ac1cSIsrael Rukshin 	bool				pi_enable;
153a07b4970SChristoph Hellwig };
154a07b4970SChristoph Hellwig 
155a07b4970SChristoph Hellwig static inline struct nvmet_port *to_nvmet_port(struct config_item *item)
156a07b4970SChristoph Hellwig {
157a07b4970SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_port,
158a07b4970SChristoph Hellwig 			group);
159a07b4970SChristoph Hellwig }
160a07b4970SChristoph Hellwig 
16162ac0d32SChristoph Hellwig static inline struct nvmet_port *ana_groups_to_port(
16262ac0d32SChristoph Hellwig 		struct config_item *item)
16362ac0d32SChristoph Hellwig {
16462ac0d32SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_port,
16562ac0d32SChristoph Hellwig 			ana_groups_group);
16662ac0d32SChristoph Hellwig }
16762ac0d32SChristoph Hellwig 
168a07b4970SChristoph Hellwig struct nvmet_ctrl {
169a07b4970SChristoph Hellwig 	struct nvmet_subsys	*subsys;
170a07b4970SChristoph Hellwig 	struct nvmet_sq		**sqs;
171a07b4970SChristoph Hellwig 
172c09305aeSSagi Grimberg 	bool			cmd_seen;
173c09305aeSSagi Grimberg 
174a07b4970SChristoph Hellwig 	struct mutex		lock;
175a07b4970SChristoph Hellwig 	u64			cap;
176a07b4970SChristoph Hellwig 	u32			cc;
177a07b4970SChristoph Hellwig 	u32			csts;
178a07b4970SChristoph Hellwig 
17928dd5cf7SOmri Mann 	uuid_t			hostid;
180a07b4970SChristoph Hellwig 	u16			cntlid;
181a07b4970SChristoph Hellwig 	u32			kato;
182a07b4970SChristoph Hellwig 
1834ee43280SChristoph Hellwig 	struct nvmet_port	*port;
1844ee43280SChristoph Hellwig 
185c86b8f7bSChristoph Hellwig 	u32			aen_enabled;
18655fdd6b6SChristoph Hellwig 	unsigned long		aen_masked;
187a07b4970SChristoph Hellwig 	struct nvmet_req	*async_event_cmds[NVMET_ASYNC_EVENTS];
188a07b4970SChristoph Hellwig 	unsigned int		nr_async_event_cmds;
189a07b4970SChristoph Hellwig 	struct list_head	async_events;
190a07b4970SChristoph Hellwig 	struct work_struct	async_event_work;
191a07b4970SChristoph Hellwig 
192a07b4970SChristoph Hellwig 	struct list_head	subsys_entry;
193a07b4970SChristoph Hellwig 	struct kref		ref;
194a07b4970SChristoph Hellwig 	struct delayed_work	ka_work;
195a07b4970SChristoph Hellwig 	struct work_struct	fatal_err_work;
196a07b4970SChristoph Hellwig 
197e929f06dSChristoph Hellwig 	const struct nvmet_fabrics_ops *ops;
198a07b4970SChristoph Hellwig 
199c16734eaSChristoph Hellwig 	__le32			*changed_ns_list;
200c16734eaSChristoph Hellwig 	u32			nr_changed_ns;
201c16734eaSChristoph Hellwig 
202a07b4970SChristoph Hellwig 	char			subsysnqn[NVMF_NQN_FIELD_LEN];
203a07b4970SChristoph Hellwig 	char			hostnqn[NVMF_NQN_FIELD_LEN];
204c6925093SLogan Gunthorpe 
205c6925093SLogan Gunthorpe 	struct device		*p2p_client;
206c6925093SLogan Gunthorpe 	struct radix_tree_root	p2p_ns_map;
207e4a97625SChaitanya Kulkarni 
208e4a97625SChaitanya Kulkarni 	spinlock_t		error_lock;
209e4a97625SChaitanya Kulkarni 	u64			err_counter;
210e4a97625SChaitanya Kulkarni 	struct nvme_error_slot	slots[NVMET_ERROR_LOG_SLOTS];
211ea52ac1cSIsrael Rukshin 	bool			pi_support;
212a07b4970SChristoph Hellwig };
213a07b4970SChristoph Hellwig 
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 
2207774e77eSChaitanya Kulkarni 	struct xarray		namespaces;
221793c7cfcSChristoph Hellwig 	unsigned int		nr_namespaces;
22286693c43SChaitanya Kulkarni 	u32			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;
234e13b0615SNoam Gottlieb 	char			serial[NVMET_SN_MAX_SIZE];
2357ae023c5SNoam Gottlieb 	bool			subsys_discovered;
236a07b4970SChristoph Hellwig 	char			*subsysnqn;
237ea52ac1cSIsrael Rukshin 	bool			pi_support;
238a07b4970SChristoph Hellwig 
239a07b4970SChristoph Hellwig 	struct config_group	group;
240a07b4970SChristoph Hellwig 
241a07b4970SChristoph Hellwig 	struct config_group	namespaces_group;
242a07b4970SChristoph Hellwig 	struct config_group	allowed_hosts_group;
243013b7ebeSMark Ruijter 
244d9f273b7SMax Gurtovoy 	char			*model_number;
245c1fef73fSLogan Gunthorpe 
246c1fef73fSLogan Gunthorpe #ifdef CONFIG_NVME_TARGET_PASSTHRU
247c1fef73fSLogan Gunthorpe 	struct nvme_ctrl	*passthru_ctrl;
248ba76af67SLogan Gunthorpe 	char			*passthru_ctrl_path;
249cae5b01aSLogan Gunthorpe 	struct config_group	passthru_group;
250a2f6a2b8SChaitanya Kulkarni 	unsigned int		admin_timeout;
25147e9730cSChaitanya Kulkarni 	unsigned int		io_timeout;
252c1fef73fSLogan Gunthorpe #endif /* CONFIG_NVME_TARGET_PASSTHRU */
253*aaf2e048SChaitanya Kulkarni 
254*aaf2e048SChaitanya Kulkarni #ifdef CONFIG_BLK_DEV_ZONED
255*aaf2e048SChaitanya Kulkarni 	u8			zasl;
256*aaf2e048SChaitanya Kulkarni #endif /* CONFIG_BLK_DEV_ZONED */
257a07b4970SChristoph Hellwig };
258a07b4970SChristoph Hellwig 
259a07b4970SChristoph Hellwig static inline struct nvmet_subsys *to_subsys(struct config_item *item)
260a07b4970SChristoph Hellwig {
261a07b4970SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_subsys, group);
262a07b4970SChristoph Hellwig }
263a07b4970SChristoph Hellwig 
264a07b4970SChristoph Hellwig static inline struct nvmet_subsys *namespaces_to_subsys(
265a07b4970SChristoph Hellwig 		struct config_item *item)
266a07b4970SChristoph Hellwig {
267a07b4970SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_subsys,
268a07b4970SChristoph Hellwig 			namespaces_group);
269a07b4970SChristoph Hellwig }
270a07b4970SChristoph Hellwig 
271a07b4970SChristoph Hellwig struct nvmet_host {
272a07b4970SChristoph Hellwig 	struct config_group	group;
273a07b4970SChristoph Hellwig };
274a07b4970SChristoph Hellwig 
275a07b4970SChristoph Hellwig static inline struct nvmet_host *to_host(struct config_item *item)
276a07b4970SChristoph Hellwig {
277a07b4970SChristoph Hellwig 	return container_of(to_config_group(item), struct nvmet_host, group);
278a07b4970SChristoph Hellwig }
279a07b4970SChristoph Hellwig 
280a07b4970SChristoph Hellwig static inline char *nvmet_host_name(struct nvmet_host *host)
281a07b4970SChristoph Hellwig {
282a07b4970SChristoph Hellwig 	return config_item_name(&host->group.cg_item);
283a07b4970SChristoph Hellwig }
284a07b4970SChristoph Hellwig 
285a07b4970SChristoph Hellwig struct nvmet_host_link {
286a07b4970SChristoph Hellwig 	struct list_head	entry;
287a07b4970SChristoph Hellwig 	struct nvmet_host	*host;
288a07b4970SChristoph Hellwig };
289a07b4970SChristoph Hellwig 
290a07b4970SChristoph Hellwig struct nvmet_subsys_link {
291a07b4970SChristoph Hellwig 	struct list_head	entry;
292a07b4970SChristoph Hellwig 	struct nvmet_subsys	*subsys;
293a07b4970SChristoph Hellwig };
294a07b4970SChristoph Hellwig 
295a07b4970SChristoph Hellwig struct nvmet_req;
296a07b4970SChristoph Hellwig struct nvmet_fabrics_ops {
297a07b4970SChristoph Hellwig 	struct module *owner;
298a07b4970SChristoph Hellwig 	unsigned int type;
299a07b4970SChristoph Hellwig 	unsigned int msdbd;
3006fa350f7SMax Gurtovoy 	unsigned int flags;
3016fa350f7SMax Gurtovoy #define NVMF_KEYED_SGLS			(1 << 0)
3026fa350f7SMax Gurtovoy #define NVMF_METADATA_SUPPORTED		(1 << 1)
303a07b4970SChristoph Hellwig 	void (*queue_response)(struct nvmet_req *req);
304a07b4970SChristoph Hellwig 	int (*add_port)(struct nvmet_port *port);
305a07b4970SChristoph Hellwig 	void (*remove_port)(struct nvmet_port *port);
306a07b4970SChristoph Hellwig 	void (*delete_ctrl)(struct nvmet_ctrl *ctrl);
3074c652685SSagi Grimberg 	void (*disc_traddr)(struct nvmet_req *req,
3084c652685SSagi Grimberg 			struct nvmet_port *port, char *traddr);
3091672ddb8SSagi Grimberg 	u16 (*install_queue)(struct nvmet_sq *nvme_sq);
3109d09dd8dSJames Smart 	void (*discovery_chg)(struct nvmet_port *port);
31102cb00e2SMax Gurtovoy 	u8 (*get_mdts)(const struct nvmet_ctrl *ctrl);
312a07b4970SChristoph Hellwig };
313a07b4970SChristoph Hellwig 
314a07b4970SChristoph Hellwig #define NVMET_MAX_INLINE_BIOVEC	8
31573383adfSSagi Grimberg #define NVMET_MAX_INLINE_DATA_LEN NVMET_MAX_INLINE_BIOVEC * PAGE_SIZE
316a07b4970SChristoph Hellwig 
317a07b4970SChristoph Hellwig struct nvmet_req {
318a07b4970SChristoph Hellwig 	struct nvme_command	*cmd;
319fc6c9730SMax Gurtovoy 	struct nvme_completion	*cqe;
320a07b4970SChristoph Hellwig 	struct nvmet_sq		*sq;
321a07b4970SChristoph Hellwig 	struct nvmet_cq		*cq;
322a07b4970SChristoph Hellwig 	struct nvmet_ns		*ns;
323a07b4970SChristoph Hellwig 	struct scatterlist	*sg;
324c6e3f133SIsrael Rukshin 	struct scatterlist	*metadata_sg;
325a07b4970SChristoph Hellwig 	struct bio_vec		inline_bvec[NVMET_MAX_INLINE_BIOVEC];
326d5eff33eSChaitanya Kulkarni 	union {
327d5eff33eSChaitanya Kulkarni 		struct {
328d5eff33eSChaitanya Kulkarni 			struct bio      inline_bio;
329d5eff33eSChaitanya Kulkarni 		} b;
330d5eff33eSChaitanya Kulkarni 		struct {
331d5eff33eSChaitanya Kulkarni 			bool			mpool_alloc;
332d5eff33eSChaitanya Kulkarni 			struct kiocb            iocb;
333d5eff33eSChaitanya Kulkarni 			struct bio_vec          *bvec;
334d5eff33eSChaitanya Kulkarni 			struct work_struct      work;
335d5eff33eSChaitanya Kulkarni 		} f;
336c1fef73fSLogan Gunthorpe 		struct {
337dab3902bSChaitanya Kulkarni 			struct bio		inline_bio;
338c1fef73fSLogan Gunthorpe 			struct request		*rq;
339c1fef73fSLogan Gunthorpe 			struct work_struct      work;
340c1fef73fSLogan Gunthorpe 			bool			use_workqueue;
341c1fef73fSLogan Gunthorpe 		} p;
342*aaf2e048SChaitanya Kulkarni #ifdef CONFIG_BLK_DEV_ZONED
343*aaf2e048SChaitanya Kulkarni 		struct {
344*aaf2e048SChaitanya Kulkarni 			struct bio		inline_bio;
345*aaf2e048SChaitanya Kulkarni 			struct work_struct	zmgmt_work;
346*aaf2e048SChaitanya Kulkarni 		} z;
347*aaf2e048SChaitanya Kulkarni #endif /* CONFIG_BLK_DEV_ZONED */
348d5eff33eSChaitanya Kulkarni 	};
349a07b4970SChristoph Hellwig 	int			sg_cnt;
350c6e3f133SIsrael Rukshin 	int			metadata_sg_cnt;
3515e62d5c9SChristoph Hellwig 	/* data length as parsed from the SGL descriptor: */
3525e62d5c9SChristoph Hellwig 	size_t			transfer_len;
353c6e3f133SIsrael Rukshin 	size_t			metadata_len;
354a07b4970SChristoph Hellwig 
355a07b4970SChristoph Hellwig 	struct nvmet_port	*port;
356a07b4970SChristoph Hellwig 
357a07b4970SChristoph Hellwig 	void (*execute)(struct nvmet_req *req);
358e929f06dSChristoph Hellwig 	const struct nvmet_fabrics_ops *ops;
359c6925093SLogan Gunthorpe 
360c6925093SLogan Gunthorpe 	struct pci_dev		*p2p_dev;
361c6925093SLogan Gunthorpe 	struct device		*p2p_client;
362e4a97625SChaitanya Kulkarni 	u16			error_loc;
363e4a97625SChaitanya Kulkarni 	u64			error_slba;
364a07b4970SChristoph Hellwig };
365a07b4970SChristoph Hellwig 
36655eb942eSChaitanya Kulkarni extern struct workqueue_struct *buffered_io_wq;
367*aaf2e048SChaitanya Kulkarni extern struct workqueue_struct *zbd_wq;
36855eb942eSChaitanya Kulkarni 
369a07b4970SChristoph Hellwig static inline void nvmet_set_result(struct nvmet_req *req, u32 result)
370a07b4970SChristoph Hellwig {
371fc6c9730SMax Gurtovoy 	req->cqe->result.u32 = cpu_to_le32(result);
372a07b4970SChristoph Hellwig }
373a07b4970SChristoph Hellwig 
374a07b4970SChristoph Hellwig /*
375a07b4970SChristoph Hellwig  * NVMe command writes actually are DMA reads for us on the target side.
376a07b4970SChristoph Hellwig  */
377a07b4970SChristoph Hellwig static inline enum dma_data_direction
378a07b4970SChristoph Hellwig nvmet_data_dir(struct nvmet_req *req)
379a07b4970SChristoph Hellwig {
380a07b4970SChristoph Hellwig 	return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
381a07b4970SChristoph Hellwig }
382a07b4970SChristoph Hellwig 
383a07b4970SChristoph Hellwig struct nvmet_async_event {
384a07b4970SChristoph Hellwig 	struct list_head	entry;
385a07b4970SChristoph Hellwig 	u8			event_type;
386a07b4970SChristoph Hellwig 	u8			event_info;
387a07b4970SChristoph Hellwig 	u8			log_page;
388a07b4970SChristoph Hellwig };
389a07b4970SChristoph Hellwig 
3907114ddebSJay Sternberg static inline void nvmet_clear_aen_bit(struct nvmet_req *req, u32 bn)
3916c8312adSJay Sternberg {
392b7c8f366SChaitanya Kulkarni 	int rae = le32_to_cpu(req->cmd->common.cdw10) & 1 << 15;
3936c8312adSJay Sternberg 
3946c8312adSJay Sternberg 	if (!rae)
3957114ddebSJay Sternberg 		clear_bit(bn, &req->sq->ctrl->aen_masked);
3966c8312adSJay Sternberg }
3976c8312adSJay Sternberg 
3987114ddebSJay Sternberg static inline bool nvmet_aen_bit_disabled(struct nvmet_ctrl *ctrl, u32 bn)
3996c8312adSJay Sternberg {
4007114ddebSJay Sternberg 	if (!(READ_ONCE(ctrl->aen_enabled) & (1 << bn)))
4016c8312adSJay Sternberg 		return true;
4027114ddebSJay Sternberg 	return test_and_set_bit(bn, &ctrl->aen_masked);
4036c8312adSJay Sternberg }
4046c8312adSJay Sternberg 
40590107455SJay Sternberg void nvmet_get_feat_kato(struct nvmet_req *req);
40690107455SJay Sternberg void nvmet_get_feat_async_event(struct nvmet_req *req);
40790107455SJay Sternberg u16 nvmet_set_feat_kato(struct nvmet_req *req);
40890107455SJay Sternberg u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask);
40990107455SJay Sternberg void nvmet_execute_async_event(struct nvmet_req *req);
4104e683c48SAmit Engel void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl);
4114e683c48SAmit Engel void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl);
41290107455SJay Sternberg 
41364a0ca88SParav Pandit u16 nvmet_parse_connect_cmd(struct nvmet_req *req);
4149d05a96eSBart Van Assche void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id);
415d5eff33eSChaitanya Kulkarni u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req);
416d5eff33eSChaitanya Kulkarni u16 nvmet_file_parse_io_cmd(struct nvmet_req *req);
417*aaf2e048SChaitanya Kulkarni u16 nvmet_bdev_zns_parse_io_cmd(struct nvmet_req *req);
41864a0ca88SParav Pandit u16 nvmet_parse_admin_cmd(struct nvmet_req *req);
41964a0ca88SParav Pandit u16 nvmet_parse_discovery_cmd(struct nvmet_req *req);
42064a0ca88SParav Pandit u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req);
421a07b4970SChristoph Hellwig 
422a07b4970SChristoph Hellwig bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
423e929f06dSChristoph Hellwig 		struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops);
424549f01aeSVijay Immanuel void nvmet_req_uninit(struct nvmet_req *req);
425136cc1ffSIsrael Rukshin bool nvmet_check_transfer_len(struct nvmet_req *req, size_t len);
426b716e688SSagi Grimberg bool nvmet_check_data_len_lte(struct nvmet_req *req, size_t data_len);
427a07b4970SChristoph Hellwig void nvmet_req_complete(struct nvmet_req *req, u16 status);
428c6e3f133SIsrael Rukshin int nvmet_req_alloc_sgls(struct nvmet_req *req);
429c6e3f133SIsrael Rukshin void nvmet_req_free_sgls(struct nvmet_req *req);
430a07b4970SChristoph Hellwig 
431c1fef73fSLogan Gunthorpe void nvmet_execute_set_features(struct nvmet_req *req);
432c1fef73fSLogan Gunthorpe void nvmet_execute_get_features(struct nvmet_req *req);
433f9362ac1SJay Sternberg void nvmet_execute_keep_alive(struct nvmet_req *req);
434f9362ac1SJay Sternberg 
435a07b4970SChristoph Hellwig void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid,
436a07b4970SChristoph Hellwig 		u16 size);
437a07b4970SChristoph Hellwig void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid,
438a07b4970SChristoph Hellwig 		u16 size);
439a07b4970SChristoph Hellwig void nvmet_sq_destroy(struct nvmet_sq *sq);
440a07b4970SChristoph Hellwig int nvmet_sq_init(struct nvmet_sq *sq);
441a07b4970SChristoph Hellwig 
442a07b4970SChristoph Hellwig void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl);
443a07b4970SChristoph Hellwig 
444a07b4970SChristoph Hellwig void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new);
445a07b4970SChristoph Hellwig u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
446a07b4970SChristoph Hellwig 		struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp);
447de587804SChaitanya Kulkarni struct nvmet_ctrl *nvmet_ctrl_find_get(const char *subsysnqn,
448de587804SChaitanya Kulkarni 				       const char *hostnqn, u16 cntlid,
449de587804SChaitanya Kulkarni 				       struct nvmet_req *req);
450a07b4970SChristoph Hellwig void nvmet_ctrl_put(struct nvmet_ctrl *ctrl);
4517798df6fSChaitanya Kulkarni u16 nvmet_check_ctrl_status(struct nvmet_req *req);
452a07b4970SChristoph Hellwig 
453a07b4970SChristoph Hellwig struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
454a07b4970SChristoph Hellwig 		enum nvme_subsys_type type);
455a07b4970SChristoph Hellwig void nvmet_subsys_put(struct nvmet_subsys *subsys);
456344770b0SSagi Grimberg void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys);
457a07b4970SChristoph Hellwig 
4583a1f7c79SChaitanya Kulkarni u16 nvmet_req_find_ns(struct nvmet_req *req);
459a07b4970SChristoph Hellwig void nvmet_put_namespace(struct nvmet_ns *ns);
460a07b4970SChristoph Hellwig int nvmet_ns_enable(struct nvmet_ns *ns);
461a07b4970SChristoph Hellwig void nvmet_ns_disable(struct nvmet_ns *ns);
462a07b4970SChristoph Hellwig struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
463a07b4970SChristoph Hellwig void nvmet_ns_free(struct nvmet_ns *ns);
464a07b4970SChristoph Hellwig 
46562ac0d32SChristoph Hellwig void nvmet_send_ana_event(struct nvmet_subsys *subsys,
46662ac0d32SChristoph Hellwig 		struct nvmet_port *port);
46762ac0d32SChristoph Hellwig void nvmet_port_send_ana_event(struct nvmet_port *port);
46862ac0d32SChristoph Hellwig 
469e929f06dSChristoph Hellwig int nvmet_register_transport(const struct nvmet_fabrics_ops *ops);
470e929f06dSChristoph Hellwig void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops);
471a07b4970SChristoph Hellwig 
4723aed8673SLogan Gunthorpe void nvmet_port_del_ctrls(struct nvmet_port *port,
4733aed8673SLogan Gunthorpe 			  struct nvmet_subsys *subsys);
4743aed8673SLogan Gunthorpe 
475a07b4970SChristoph Hellwig int nvmet_enable_port(struct nvmet_port *port);
476a07b4970SChristoph Hellwig void nvmet_disable_port(struct nvmet_port *port);
477a07b4970SChristoph Hellwig 
478a07b4970SChristoph Hellwig void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port);
479b662a078SJay Sternberg void nvmet_referral_disable(struct nvmet_port *parent, struct nvmet_port *port);
480a07b4970SChristoph Hellwig 
481a07b4970SChristoph Hellwig u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
482a07b4970SChristoph Hellwig 		size_t len);
483a07b4970SChristoph Hellwig u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf,
484a07b4970SChristoph Hellwig 		size_t len);
485c7759fffSChristoph Hellwig u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len);
486a07b4970SChristoph Hellwig 
487a07b4970SChristoph Hellwig u32 nvmet_get_log_page_len(struct nvme_command *cmd);
488d808b7f7SKeith Busch u64 nvmet_get_log_page_offset(struct nvme_command *cmd);
489a07b4970SChristoph Hellwig 
490b662a078SJay Sternberg extern struct list_head *nvmet_ports;
491b662a078SJay Sternberg void nvmet_port_disc_changed(struct nvmet_port *port,
492b662a078SJay Sternberg 		struct nvmet_subsys *subsys);
493b662a078SJay Sternberg void nvmet_subsys_disc_changed(struct nvmet_subsys *subsys,
494b662a078SJay Sternberg 		struct nvmet_host *host);
495b662a078SJay Sternberg void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
496b662a078SJay Sternberg 		u8 event_info, u8 log_page);
497b662a078SJay Sternberg 
498a07b4970SChristoph Hellwig #define NVMET_QUEUE_SIZE	1024
4993375e29cSJames Smart #define NVMET_NR_QUEUES		128
500a07b4970SChristoph Hellwig #define NVMET_MAX_CMD		NVMET_QUEUE_SIZE
501793c7cfcSChristoph Hellwig 
502793c7cfcSChristoph Hellwig /*
503793c7cfcSChristoph Hellwig  * Nice round number that makes a list of nsids fit into a page.
504793c7cfcSChristoph Hellwig  * Should become tunable at some point in the future.
505793c7cfcSChristoph Hellwig  */
506793c7cfcSChristoph Hellwig #define NVMET_MAX_NAMESPACES	1024
507793c7cfcSChristoph Hellwig 
50872efd25dSChristoph Hellwig /*
50972efd25dSChristoph Hellwig  * 0 is not a valid ANA group ID, so we start numbering at 1.
51072efd25dSChristoph Hellwig  *
51172efd25dSChristoph Hellwig  * ANA Group 1 exists without manual intervention, has namespaces assigned to it
51272efd25dSChristoph Hellwig  * by default, and is available in an optimized state through all ports.
51372efd25dSChristoph Hellwig  */
51462ac0d32SChristoph Hellwig #define NVMET_MAX_ANAGRPS	128
51572efd25dSChristoph Hellwig #define NVMET_DEFAULT_ANA_GRPID	1
51672efd25dSChristoph Hellwig 
517a07b4970SChristoph Hellwig #define NVMET_KAS		10
518f9362ac1SJay Sternberg #define NVMET_DISC_KATO_MS		120000
519a07b4970SChristoph Hellwig 
520a07b4970SChristoph Hellwig int __init nvmet_init_configfs(void);
521a07b4970SChristoph Hellwig void __exit nvmet_exit_configfs(void);
522a07b4970SChristoph Hellwig 
523a07b4970SChristoph Hellwig int __init nvmet_init_discovery(void);
524a07b4970SChristoph Hellwig void nvmet_exit_discovery(void);
525a07b4970SChristoph Hellwig 
526a07b4970SChristoph Hellwig extern struct nvmet_subsys *nvmet_disc_subsys;
527a07b4970SChristoph Hellwig extern struct rw_semaphore nvmet_config_sem;
528a07b4970SChristoph Hellwig 
52972efd25dSChristoph Hellwig extern u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
53072efd25dSChristoph Hellwig extern u64 nvmet_ana_chgcnt;
53172efd25dSChristoph Hellwig extern struct rw_semaphore nvmet_ana_sem;
53272efd25dSChristoph Hellwig 
533253928eeSSagi Grimberg bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn);
534a07b4970SChristoph Hellwig 
535d5eff33eSChaitanya Kulkarni int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
536d5eff33eSChaitanya Kulkarni int nvmet_file_ns_enable(struct nvmet_ns *ns);
537d5eff33eSChaitanya Kulkarni void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
538d5eff33eSChaitanya Kulkarni void nvmet_file_ns_disable(struct nvmet_ns *ns);
539dedf0be5SChaitanya Kulkarni u16 nvmet_bdev_flush(struct nvmet_req *req);
540dedf0be5SChaitanya Kulkarni u16 nvmet_file_flush(struct nvmet_req *req);
541dedf0be5SChaitanya Kulkarni void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
542e8cd1ff1SAnthony Iliopoulos void nvmet_bdev_ns_revalidate(struct nvmet_ns *ns);
543e8cd1ff1SAnthony Iliopoulos int nvmet_file_ns_revalidate(struct nvmet_ns *ns);
544463c5fabSChaitanya Kulkarni void nvmet_ns_revalidate(struct nvmet_ns *ns);
545*aaf2e048SChaitanya Kulkarni u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts);
546*aaf2e048SChaitanya Kulkarni 
547*aaf2e048SChaitanya Kulkarni bool nvmet_bdev_zns_enable(struct nvmet_ns *ns);
548*aaf2e048SChaitanya Kulkarni void nvmet_execute_identify_cns_cs_ctrl(struct nvmet_req *req);
549*aaf2e048SChaitanya Kulkarni void nvmet_execute_identify_cns_cs_ns(struct nvmet_req *req);
550*aaf2e048SChaitanya Kulkarni void nvmet_bdev_execute_zone_mgmt_recv(struct nvmet_req *req);
551*aaf2e048SChaitanya Kulkarni void nvmet_bdev_execute_zone_mgmt_send(struct nvmet_req *req);
552*aaf2e048SChaitanya Kulkarni void nvmet_bdev_execute_zone_append(struct nvmet_req *req);
553d5eff33eSChaitanya Kulkarni 
55426af180cSIsrael Rukshin static inline u32 nvmet_rw_data_len(struct nvmet_req *req)
555d5eff33eSChaitanya Kulkarni {
556d5eff33eSChaitanya Kulkarni 	return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) <<
557d5eff33eSChaitanya Kulkarni 			req->ns->blksize_shift;
558d5eff33eSChaitanya Kulkarni }
559c6aa3542SChaitanya Kulkarni 
560ea52ac1cSIsrael Rukshin static inline u32 nvmet_rw_metadata_len(struct nvmet_req *req)
561ea52ac1cSIsrael Rukshin {
562ea52ac1cSIsrael Rukshin 	if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
563ea52ac1cSIsrael Rukshin 		return 0;
564ea52ac1cSIsrael Rukshin 	return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) *
565ea52ac1cSIsrael Rukshin 			req->ns->metadata_size;
566ea52ac1cSIsrael Rukshin }
567ea52ac1cSIsrael Rukshin 
56859ef0eaaSChristoph Hellwig static inline u32 nvmet_dsm_len(struct nvmet_req *req)
56959ef0eaaSChristoph Hellwig {
57059ef0eaaSChristoph Hellwig 	return (le32_to_cpu(req->cmd->dsm.nr) + 1) *
57159ef0eaaSChristoph Hellwig 		sizeof(struct nvme_dsm_range);
57259ef0eaaSChristoph Hellwig }
57359ef0eaaSChristoph Hellwig 
57420c2c3bbSChaitanya Kulkarni static inline struct nvmet_subsys *nvmet_req_subsys(struct nvmet_req *req)
57520c2c3bbSChaitanya Kulkarni {
57620c2c3bbSChaitanya Kulkarni 	return req->sq->ctrl->subsys;
57720c2c3bbSChaitanya Kulkarni }
57820c2c3bbSChaitanya Kulkarni 
579c1fef73fSLogan Gunthorpe #ifdef CONFIG_NVME_TARGET_PASSTHRU
580ba76af67SLogan Gunthorpe void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys);
581ba76af67SLogan Gunthorpe int nvmet_passthru_ctrl_enable(struct nvmet_subsys *subsys);
582ba76af67SLogan Gunthorpe void nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys);
583c1fef73fSLogan Gunthorpe u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req);
584c1fef73fSLogan Gunthorpe u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req);
585c1fef73fSLogan Gunthorpe static inline struct nvme_ctrl *nvmet_passthru_ctrl(struct nvmet_subsys *subsys)
586c1fef73fSLogan Gunthorpe {
587c1fef73fSLogan Gunthorpe 	return subsys->passthru_ctrl;
588c1fef73fSLogan Gunthorpe }
589c1fef73fSLogan Gunthorpe #else /* CONFIG_NVME_TARGET_PASSTHRU */
590ba76af67SLogan Gunthorpe static inline void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys)
591ba76af67SLogan Gunthorpe {
592ba76af67SLogan Gunthorpe }
593ba76af67SLogan Gunthorpe static inline void nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys)
594ba76af67SLogan Gunthorpe {
595ba76af67SLogan Gunthorpe }
596c1fef73fSLogan Gunthorpe static inline u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req)
597c1fef73fSLogan Gunthorpe {
598c1fef73fSLogan Gunthorpe 	return 0;
599c1fef73fSLogan Gunthorpe }
600c1fef73fSLogan Gunthorpe static inline u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req)
601c1fef73fSLogan Gunthorpe {
602c1fef73fSLogan Gunthorpe 	return 0;
603c1fef73fSLogan Gunthorpe }
604c1fef73fSLogan Gunthorpe static inline struct nvme_ctrl *nvmet_passthru_ctrl(struct nvmet_subsys *subsys)
605c1fef73fSLogan Gunthorpe {
606c1fef73fSLogan Gunthorpe 	return NULL;
607c1fef73fSLogan Gunthorpe }
608c1fef73fSLogan Gunthorpe #endif /* CONFIG_NVME_TARGET_PASSTHRU */
609c1fef73fSLogan Gunthorpe 
610c1fef73fSLogan Gunthorpe static inline struct nvme_ctrl *
611c1fef73fSLogan Gunthorpe nvmet_req_passthru_ctrl(struct nvmet_req *req)
612c1fef73fSLogan Gunthorpe {
61320c2c3bbSChaitanya Kulkarni 	return nvmet_passthru_ctrl(nvmet_req_subsys(req));
614c1fef73fSLogan Gunthorpe }
615c1fef73fSLogan Gunthorpe 
616c6aa3542SChaitanya Kulkarni u16 errno_to_nvme_status(struct nvmet_req *req, int errno);
617d81d57cfSChaitanya Kulkarni u16 nvmet_report_invalid_opcode(struct nvmet_req *req);
6189d05a96eSBart Van Assche 
6199d05a96eSBart Van Assche /* Convert a 32-bit number to a 16-bit 0's based number */
6209d05a96eSBart Van Assche static inline __le16 to0based(u32 a)
6219d05a96eSBart Van Assche {
6229d05a96eSBart Van Assche 	return cpu_to_le16(max(1U, min(1U << 16, a)) - 1);
6239d05a96eSBart Van Assche }
6249d05a96eSBart Van Assche 
625ea52ac1cSIsrael Rukshin static inline bool nvmet_ns_has_pi(struct nvmet_ns *ns)
626ea52ac1cSIsrael Rukshin {
627ea52ac1cSIsrael Rukshin 	if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
628ea52ac1cSIsrael Rukshin 		return false;
629ea52ac1cSIsrael Rukshin 	return ns->pi_type && ns->metadata_size == sizeof(struct t10_pi_tuple);
630ea52ac1cSIsrael Rukshin }
631ea52ac1cSIsrael Rukshin 
632193fcf37SChaitanya Kulkarni static inline __le64 nvmet_sect_to_lba(struct nvmet_ns *ns, sector_t sect)
633193fcf37SChaitanya Kulkarni {
634193fcf37SChaitanya Kulkarni 	return cpu_to_le64(sect >> (ns->blksize_shift - SECTOR_SHIFT));
635193fcf37SChaitanya Kulkarni }
636193fcf37SChaitanya Kulkarni 
637193fcf37SChaitanya Kulkarni static inline sector_t nvmet_lba_to_sect(struct nvmet_ns *ns, __le64 lba)
638193fcf37SChaitanya Kulkarni {
639193fcf37SChaitanya Kulkarni 	return le64_to_cpu(lba) << (ns->blksize_shift - SECTOR_SHIFT);
640193fcf37SChaitanya Kulkarni }
641193fcf37SChaitanya Kulkarni 
642608a9690SChaitanya Kulkarni static inline bool nvmet_use_inline_bvec(struct nvmet_req *req)
643608a9690SChaitanya Kulkarni {
644608a9690SChaitanya Kulkarni 	return req->transfer_len <= NVMET_MAX_INLINE_DATA_LEN &&
645608a9690SChaitanya Kulkarni 	       req->sg_cnt <= NVMET_MAX_INLINE_BIOVEC;
646608a9690SChaitanya Kulkarni }
647608a9690SChaitanya Kulkarni 
6486e597263SChaitanya Kulkarni static inline void nvmet_req_cns_error_complete(struct nvmet_req *req)
6496e597263SChaitanya Kulkarni {
6506e597263SChaitanya Kulkarni 	pr_debug("unhandled identify cns %d on qid %d\n",
6516e597263SChaitanya Kulkarni 	       req->cmd->identify.cns, req->sq->qid);
6526e597263SChaitanya Kulkarni 	req->error_loc = offsetof(struct nvme_identify, cns);
6536e597263SChaitanya Kulkarni 	nvmet_req_complete(req, NVME_SC_INVALID_FIELD | NVME_SC_DNR);
6546e597263SChaitanya Kulkarni }
6556e597263SChaitanya Kulkarni 
6569a01b58cSChaitanya Kulkarni static inline void nvmet_req_bio_put(struct nvmet_req *req, struct bio *bio)
6579a01b58cSChaitanya Kulkarni {
6589a01b58cSChaitanya Kulkarni 	if (bio != &req->b.inline_bio)
6599a01b58cSChaitanya Kulkarni 		bio_put(bio);
6609a01b58cSChaitanya Kulkarni }
6619a01b58cSChaitanya Kulkarni 
662a07b4970SChristoph Hellwig #endif /* _NVMET_H */
663