162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2015-2016 HGST, a Western Digital Company.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef _NVMET_H
762306a36Sopenharmony_ci#define _NVMET_H
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/dma-mapping.h>
1062306a36Sopenharmony_ci#include <linux/types.h>
1162306a36Sopenharmony_ci#include <linux/device.h>
1262306a36Sopenharmony_ci#include <linux/kref.h>
1362306a36Sopenharmony_ci#include <linux/percpu-refcount.h>
1462306a36Sopenharmony_ci#include <linux/list.h>
1562306a36Sopenharmony_ci#include <linux/mutex.h>
1662306a36Sopenharmony_ci#include <linux/uuid.h>
1762306a36Sopenharmony_ci#include <linux/nvme.h>
1862306a36Sopenharmony_ci#include <linux/configfs.h>
1962306a36Sopenharmony_ci#include <linux/rcupdate.h>
2062306a36Sopenharmony_ci#include <linux/blkdev.h>
2162306a36Sopenharmony_ci#include <linux/radix-tree.h>
2262306a36Sopenharmony_ci#include <linux/t10-pi.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#define NVMET_DEFAULT_VS		NVME_VS(1, 3, 0)
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define NVMET_ASYNC_EVENTS		4
2762306a36Sopenharmony_ci#define NVMET_ERROR_LOG_SLOTS		128
2862306a36Sopenharmony_ci#define NVMET_NO_ERROR_LOC		((u16)-1)
2962306a36Sopenharmony_ci#define NVMET_DEFAULT_CTRL_MODEL	"Linux"
3062306a36Sopenharmony_ci#define NVMET_MN_MAX_SIZE		40
3162306a36Sopenharmony_ci#define NVMET_SN_MAX_SIZE		20
3262306a36Sopenharmony_ci#define NVMET_FR_MAX_SIZE		8
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci/*
3562306a36Sopenharmony_ci * Supported optional AENs:
3662306a36Sopenharmony_ci */
3762306a36Sopenharmony_ci#define NVMET_AEN_CFG_OPTIONAL \
3862306a36Sopenharmony_ci	(NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_ANA_CHANGE)
3962306a36Sopenharmony_ci#define NVMET_DISC_AEN_CFG_OPTIONAL \
4062306a36Sopenharmony_ci	(NVME_AEN_CFG_DISC_CHANGE)
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci/*
4362306a36Sopenharmony_ci * Plus mandatory SMART AENs (we'll never send them, but allow enabling them):
4462306a36Sopenharmony_ci */
4562306a36Sopenharmony_ci#define NVMET_AEN_CFG_ALL \
4662306a36Sopenharmony_ci	(NVME_SMART_CRIT_SPARE | NVME_SMART_CRIT_TEMPERATURE | \
4762306a36Sopenharmony_ci	 NVME_SMART_CRIT_RELIABILITY | NVME_SMART_CRIT_MEDIA | \
4862306a36Sopenharmony_ci	 NVME_SMART_CRIT_VOLATILE_MEMORY | NVMET_AEN_CFG_OPTIONAL)
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci/* Helper Macros when NVMe error is NVME_SC_CONNECT_INVALID_PARAM
5162306a36Sopenharmony_ci * The 16 bit shift is to set IATTR bit to 1, which means offending
5262306a36Sopenharmony_ci * offset starts in the data section of connect()
5362306a36Sopenharmony_ci */
5462306a36Sopenharmony_ci#define IPO_IATTR_CONNECT_DATA(x)	\
5562306a36Sopenharmony_ci	(cpu_to_le32((1 << 16) | (offsetof(struct nvmf_connect_data, x))))
5662306a36Sopenharmony_ci#define IPO_IATTR_CONNECT_SQE(x)	\
5762306a36Sopenharmony_ci	(cpu_to_le32(offsetof(struct nvmf_connect_command, x)))
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_cistruct nvmet_ns {
6062306a36Sopenharmony_ci	struct percpu_ref	ref;
6162306a36Sopenharmony_ci	struct block_device	*bdev;
6262306a36Sopenharmony_ci	struct file		*file;
6362306a36Sopenharmony_ci	bool			readonly;
6462306a36Sopenharmony_ci	u32			nsid;
6562306a36Sopenharmony_ci	u32			blksize_shift;
6662306a36Sopenharmony_ci	loff_t			size;
6762306a36Sopenharmony_ci	u8			nguid[16];
6862306a36Sopenharmony_ci	uuid_t			uuid;
6962306a36Sopenharmony_ci	u32			anagrpid;
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	bool			buffered_io;
7262306a36Sopenharmony_ci	bool			enabled;
7362306a36Sopenharmony_ci	struct nvmet_subsys	*subsys;
7462306a36Sopenharmony_ci	const char		*device_path;
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	struct config_group	device_group;
7762306a36Sopenharmony_ci	struct config_group	group;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	struct completion	disable_done;
8062306a36Sopenharmony_ci	mempool_t		*bvec_pool;
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	struct pci_dev		*p2p_dev;
8362306a36Sopenharmony_ci	int			use_p2pmem;
8462306a36Sopenharmony_ci	int			pi_type;
8562306a36Sopenharmony_ci	int			metadata_size;
8662306a36Sopenharmony_ci	u8			csi;
8762306a36Sopenharmony_ci};
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_cistatic inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	return container_of(to_config_group(item), struct nvmet_ns, group);
9262306a36Sopenharmony_ci}
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_cistatic inline struct device *nvmet_ns_dev(struct nvmet_ns *ns)
9562306a36Sopenharmony_ci{
9662306a36Sopenharmony_ci	return ns->bdev ? disk_to_dev(ns->bdev->bd_disk) : NULL;
9762306a36Sopenharmony_ci}
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_cistruct nvmet_cq {
10062306a36Sopenharmony_ci	u16			qid;
10162306a36Sopenharmony_ci	u16			size;
10262306a36Sopenharmony_ci};
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_cistruct nvmet_sq {
10562306a36Sopenharmony_ci	struct nvmet_ctrl	*ctrl;
10662306a36Sopenharmony_ci	struct percpu_ref	ref;
10762306a36Sopenharmony_ci	u16			qid;
10862306a36Sopenharmony_ci	u16			size;
10962306a36Sopenharmony_ci	u32			sqhd;
11062306a36Sopenharmony_ci	bool			sqhd_disabled;
11162306a36Sopenharmony_ci#ifdef CONFIG_NVME_TARGET_AUTH
11262306a36Sopenharmony_ci	bool			authenticated;
11362306a36Sopenharmony_ci	struct delayed_work	auth_expired_work;
11462306a36Sopenharmony_ci	u16			dhchap_tid;
11562306a36Sopenharmony_ci	u16			dhchap_status;
11662306a36Sopenharmony_ci	int			dhchap_step;
11762306a36Sopenharmony_ci	u8			*dhchap_c1;
11862306a36Sopenharmony_ci	u8			*dhchap_c2;
11962306a36Sopenharmony_ci	u32			dhchap_s1;
12062306a36Sopenharmony_ci	u32			dhchap_s2;
12162306a36Sopenharmony_ci	u8			*dhchap_skey;
12262306a36Sopenharmony_ci	int			dhchap_skey_len;
12362306a36Sopenharmony_ci#endif
12462306a36Sopenharmony_ci	struct completion	free_done;
12562306a36Sopenharmony_ci	struct completion	confirm_done;
12662306a36Sopenharmony_ci};
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_cistruct nvmet_ana_group {
12962306a36Sopenharmony_ci	struct config_group	group;
13062306a36Sopenharmony_ci	struct nvmet_port	*port;
13162306a36Sopenharmony_ci	u32			grpid;
13262306a36Sopenharmony_ci};
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_cistatic inline struct nvmet_ana_group *to_ana_group(struct config_item *item)
13562306a36Sopenharmony_ci{
13662306a36Sopenharmony_ci	return container_of(to_config_group(item), struct nvmet_ana_group,
13762306a36Sopenharmony_ci			group);
13862306a36Sopenharmony_ci}
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci/**
14162306a36Sopenharmony_ci * struct nvmet_port -	Common structure to keep port
14262306a36Sopenharmony_ci *				information for the target.
14362306a36Sopenharmony_ci * @entry:		Entry into referrals or transport list.
14462306a36Sopenharmony_ci * @disc_addr:		Address information is stored in a format defined
14562306a36Sopenharmony_ci *				for a discovery log page entry.
14662306a36Sopenharmony_ci * @group:		ConfigFS group for this element's folder.
14762306a36Sopenharmony_ci * @priv:		Private data for the transport.
14862306a36Sopenharmony_ci */
14962306a36Sopenharmony_cistruct nvmet_port {
15062306a36Sopenharmony_ci	struct list_head		entry;
15162306a36Sopenharmony_ci	struct nvmf_disc_rsp_page_entry	disc_addr;
15262306a36Sopenharmony_ci	struct config_group		group;
15362306a36Sopenharmony_ci	struct config_group		subsys_group;
15462306a36Sopenharmony_ci	struct list_head		subsystems;
15562306a36Sopenharmony_ci	struct config_group		referrals_group;
15662306a36Sopenharmony_ci	struct list_head		referrals;
15762306a36Sopenharmony_ci	struct list_head		global_entry;
15862306a36Sopenharmony_ci	struct config_group		ana_groups_group;
15962306a36Sopenharmony_ci	struct nvmet_ana_group		ana_default_group;
16062306a36Sopenharmony_ci	enum nvme_ana_state		*ana_state;
16162306a36Sopenharmony_ci	void				*priv;
16262306a36Sopenharmony_ci	bool				enabled;
16362306a36Sopenharmony_ci	int				inline_data_size;
16462306a36Sopenharmony_ci	const struct nvmet_fabrics_ops	*tr_ops;
16562306a36Sopenharmony_ci	bool				pi_enable;
16662306a36Sopenharmony_ci};
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_cistatic inline struct nvmet_port *to_nvmet_port(struct config_item *item)
16962306a36Sopenharmony_ci{
17062306a36Sopenharmony_ci	return container_of(to_config_group(item), struct nvmet_port,
17162306a36Sopenharmony_ci			group);
17262306a36Sopenharmony_ci}
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_cistatic inline struct nvmet_port *ana_groups_to_port(
17562306a36Sopenharmony_ci		struct config_item *item)
17662306a36Sopenharmony_ci{
17762306a36Sopenharmony_ci	return container_of(to_config_group(item), struct nvmet_port,
17862306a36Sopenharmony_ci			ana_groups_group);
17962306a36Sopenharmony_ci}
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_cistruct nvmet_ctrl {
18262306a36Sopenharmony_ci	struct nvmet_subsys	*subsys;
18362306a36Sopenharmony_ci	struct nvmet_sq		**sqs;
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci	bool			reset_tbkas;
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	struct mutex		lock;
18862306a36Sopenharmony_ci	u64			cap;
18962306a36Sopenharmony_ci	u32			cc;
19062306a36Sopenharmony_ci	u32			csts;
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci	uuid_t			hostid;
19362306a36Sopenharmony_ci	u16			cntlid;
19462306a36Sopenharmony_ci	u32			kato;
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	struct nvmet_port	*port;
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci	u32			aen_enabled;
19962306a36Sopenharmony_ci	unsigned long		aen_masked;
20062306a36Sopenharmony_ci	struct nvmet_req	*async_event_cmds[NVMET_ASYNC_EVENTS];
20162306a36Sopenharmony_ci	unsigned int		nr_async_event_cmds;
20262306a36Sopenharmony_ci	struct list_head	async_events;
20362306a36Sopenharmony_ci	struct work_struct	async_event_work;
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	struct list_head	subsys_entry;
20662306a36Sopenharmony_ci	struct kref		ref;
20762306a36Sopenharmony_ci	struct delayed_work	ka_work;
20862306a36Sopenharmony_ci	struct work_struct	fatal_err_work;
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci	const struct nvmet_fabrics_ops *ops;
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci	__le32			*changed_ns_list;
21362306a36Sopenharmony_ci	u32			nr_changed_ns;
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	char			subsysnqn[NVMF_NQN_FIELD_LEN];
21662306a36Sopenharmony_ci	char			hostnqn[NVMF_NQN_FIELD_LEN];
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	struct device		*p2p_client;
21962306a36Sopenharmony_ci	struct radix_tree_root	p2p_ns_map;
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	spinlock_t		error_lock;
22262306a36Sopenharmony_ci	u64			err_counter;
22362306a36Sopenharmony_ci	struct nvme_error_slot	slots[NVMET_ERROR_LOG_SLOTS];
22462306a36Sopenharmony_ci	bool			pi_support;
22562306a36Sopenharmony_ci#ifdef CONFIG_NVME_TARGET_AUTH
22662306a36Sopenharmony_ci	struct nvme_dhchap_key	*host_key;
22762306a36Sopenharmony_ci	struct nvme_dhchap_key	*ctrl_key;
22862306a36Sopenharmony_ci	u8			shash_id;
22962306a36Sopenharmony_ci	struct crypto_kpp	*dh_tfm;
23062306a36Sopenharmony_ci	u8			dh_gid;
23162306a36Sopenharmony_ci	u8			*dh_key;
23262306a36Sopenharmony_ci	size_t			dh_keysize;
23362306a36Sopenharmony_ci#endif
23462306a36Sopenharmony_ci};
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_cistruct nvmet_subsys {
23762306a36Sopenharmony_ci	enum nvme_subsys_type	type;
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	struct mutex		lock;
24062306a36Sopenharmony_ci	struct kref		ref;
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci	struct xarray		namespaces;
24362306a36Sopenharmony_ci	unsigned int		nr_namespaces;
24462306a36Sopenharmony_ci	u32			max_nsid;
24562306a36Sopenharmony_ci	u16			cntlid_min;
24662306a36Sopenharmony_ci	u16			cntlid_max;
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci	struct list_head	ctrls;
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci	struct list_head	hosts;
25162306a36Sopenharmony_ci	bool			allow_any_host;
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	u16			max_qid;
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci	u64			ver;
25662306a36Sopenharmony_ci	char			serial[NVMET_SN_MAX_SIZE];
25762306a36Sopenharmony_ci	bool			subsys_discovered;
25862306a36Sopenharmony_ci	char			*subsysnqn;
25962306a36Sopenharmony_ci	bool			pi_support;
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci	struct config_group	group;
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci	struct config_group	namespaces_group;
26462306a36Sopenharmony_ci	struct config_group	allowed_hosts_group;
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	char			*model_number;
26762306a36Sopenharmony_ci	u32			ieee_oui;
26862306a36Sopenharmony_ci	char			*firmware_rev;
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci#ifdef CONFIG_NVME_TARGET_PASSTHRU
27162306a36Sopenharmony_ci	struct nvme_ctrl	*passthru_ctrl;
27262306a36Sopenharmony_ci	char			*passthru_ctrl_path;
27362306a36Sopenharmony_ci	struct config_group	passthru_group;
27462306a36Sopenharmony_ci	unsigned int		admin_timeout;
27562306a36Sopenharmony_ci	unsigned int		io_timeout;
27662306a36Sopenharmony_ci	unsigned int		clear_ids;
27762306a36Sopenharmony_ci#endif /* CONFIG_NVME_TARGET_PASSTHRU */
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci#ifdef CONFIG_BLK_DEV_ZONED
28062306a36Sopenharmony_ci	u8			zasl;
28162306a36Sopenharmony_ci#endif /* CONFIG_BLK_DEV_ZONED */
28262306a36Sopenharmony_ci};
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_cistatic inline struct nvmet_subsys *to_subsys(struct config_item *item)
28562306a36Sopenharmony_ci{
28662306a36Sopenharmony_ci	return container_of(to_config_group(item), struct nvmet_subsys, group);
28762306a36Sopenharmony_ci}
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_cistatic inline struct nvmet_subsys *namespaces_to_subsys(
29062306a36Sopenharmony_ci		struct config_item *item)
29162306a36Sopenharmony_ci{
29262306a36Sopenharmony_ci	return container_of(to_config_group(item), struct nvmet_subsys,
29362306a36Sopenharmony_ci			namespaces_group);
29462306a36Sopenharmony_ci}
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_cistruct nvmet_host {
29762306a36Sopenharmony_ci	struct config_group	group;
29862306a36Sopenharmony_ci	u8			*dhchap_secret;
29962306a36Sopenharmony_ci	u8			*dhchap_ctrl_secret;
30062306a36Sopenharmony_ci	u8			dhchap_key_hash;
30162306a36Sopenharmony_ci	u8			dhchap_ctrl_key_hash;
30262306a36Sopenharmony_ci	u8			dhchap_hash_id;
30362306a36Sopenharmony_ci	u8			dhchap_dhgroup_id;
30462306a36Sopenharmony_ci};
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_cistatic inline struct nvmet_host *to_host(struct config_item *item)
30762306a36Sopenharmony_ci{
30862306a36Sopenharmony_ci	return container_of(to_config_group(item), struct nvmet_host, group);
30962306a36Sopenharmony_ci}
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_cistatic inline char *nvmet_host_name(struct nvmet_host *host)
31262306a36Sopenharmony_ci{
31362306a36Sopenharmony_ci	return config_item_name(&host->group.cg_item);
31462306a36Sopenharmony_ci}
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_cistruct nvmet_host_link {
31762306a36Sopenharmony_ci	struct list_head	entry;
31862306a36Sopenharmony_ci	struct nvmet_host	*host;
31962306a36Sopenharmony_ci};
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_cistruct nvmet_subsys_link {
32262306a36Sopenharmony_ci	struct list_head	entry;
32362306a36Sopenharmony_ci	struct nvmet_subsys	*subsys;
32462306a36Sopenharmony_ci};
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_cistruct nvmet_req;
32762306a36Sopenharmony_cistruct nvmet_fabrics_ops {
32862306a36Sopenharmony_ci	struct module *owner;
32962306a36Sopenharmony_ci	unsigned int type;
33062306a36Sopenharmony_ci	unsigned int msdbd;
33162306a36Sopenharmony_ci	unsigned int flags;
33262306a36Sopenharmony_ci#define NVMF_KEYED_SGLS			(1 << 0)
33362306a36Sopenharmony_ci#define NVMF_METADATA_SUPPORTED		(1 << 1)
33462306a36Sopenharmony_ci	void (*queue_response)(struct nvmet_req *req);
33562306a36Sopenharmony_ci	int (*add_port)(struct nvmet_port *port);
33662306a36Sopenharmony_ci	void (*remove_port)(struct nvmet_port *port);
33762306a36Sopenharmony_ci	void (*delete_ctrl)(struct nvmet_ctrl *ctrl);
33862306a36Sopenharmony_ci	void (*disc_traddr)(struct nvmet_req *req,
33962306a36Sopenharmony_ci			struct nvmet_port *port, char *traddr);
34062306a36Sopenharmony_ci	u16 (*install_queue)(struct nvmet_sq *nvme_sq);
34162306a36Sopenharmony_ci	void (*discovery_chg)(struct nvmet_port *port);
34262306a36Sopenharmony_ci	u8 (*get_mdts)(const struct nvmet_ctrl *ctrl);
34362306a36Sopenharmony_ci	u16 (*get_max_queue_size)(const struct nvmet_ctrl *ctrl);
34462306a36Sopenharmony_ci};
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci#define NVMET_MAX_INLINE_BIOVEC	8
34762306a36Sopenharmony_ci#define NVMET_MAX_INLINE_DATA_LEN NVMET_MAX_INLINE_BIOVEC * PAGE_SIZE
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_cistruct nvmet_req {
35062306a36Sopenharmony_ci	struct nvme_command	*cmd;
35162306a36Sopenharmony_ci	struct nvme_completion	*cqe;
35262306a36Sopenharmony_ci	struct nvmet_sq		*sq;
35362306a36Sopenharmony_ci	struct nvmet_cq		*cq;
35462306a36Sopenharmony_ci	struct nvmet_ns		*ns;
35562306a36Sopenharmony_ci	struct scatterlist	*sg;
35662306a36Sopenharmony_ci	struct scatterlist	*metadata_sg;
35762306a36Sopenharmony_ci	struct bio_vec		inline_bvec[NVMET_MAX_INLINE_BIOVEC];
35862306a36Sopenharmony_ci	union {
35962306a36Sopenharmony_ci		struct {
36062306a36Sopenharmony_ci			struct bio      inline_bio;
36162306a36Sopenharmony_ci		} b;
36262306a36Sopenharmony_ci		struct {
36362306a36Sopenharmony_ci			bool			mpool_alloc;
36462306a36Sopenharmony_ci			struct kiocb            iocb;
36562306a36Sopenharmony_ci			struct bio_vec          *bvec;
36662306a36Sopenharmony_ci			struct work_struct      work;
36762306a36Sopenharmony_ci		} f;
36862306a36Sopenharmony_ci		struct {
36962306a36Sopenharmony_ci			struct bio		inline_bio;
37062306a36Sopenharmony_ci			struct request		*rq;
37162306a36Sopenharmony_ci			struct work_struct      work;
37262306a36Sopenharmony_ci			bool			use_workqueue;
37362306a36Sopenharmony_ci		} p;
37462306a36Sopenharmony_ci#ifdef CONFIG_BLK_DEV_ZONED
37562306a36Sopenharmony_ci		struct {
37662306a36Sopenharmony_ci			struct bio		inline_bio;
37762306a36Sopenharmony_ci			struct work_struct	zmgmt_work;
37862306a36Sopenharmony_ci		} z;
37962306a36Sopenharmony_ci#endif /* CONFIG_BLK_DEV_ZONED */
38062306a36Sopenharmony_ci	};
38162306a36Sopenharmony_ci	int			sg_cnt;
38262306a36Sopenharmony_ci	int			metadata_sg_cnt;
38362306a36Sopenharmony_ci	/* data length as parsed from the SGL descriptor: */
38462306a36Sopenharmony_ci	size_t			transfer_len;
38562306a36Sopenharmony_ci	size_t			metadata_len;
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci	struct nvmet_port	*port;
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ci	void (*execute)(struct nvmet_req *req);
39062306a36Sopenharmony_ci	const struct nvmet_fabrics_ops *ops;
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci	struct pci_dev		*p2p_dev;
39362306a36Sopenharmony_ci	struct device		*p2p_client;
39462306a36Sopenharmony_ci	u16			error_loc;
39562306a36Sopenharmony_ci	u64			error_slba;
39662306a36Sopenharmony_ci};
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_ci#define NVMET_MAX_MPOOL_BVEC		16
39962306a36Sopenharmony_ciextern struct kmem_cache *nvmet_bvec_cache;
40062306a36Sopenharmony_ciextern struct workqueue_struct *buffered_io_wq;
40162306a36Sopenharmony_ciextern struct workqueue_struct *zbd_wq;
40262306a36Sopenharmony_ciextern struct workqueue_struct *nvmet_wq;
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_cistatic inline void nvmet_set_result(struct nvmet_req *req, u32 result)
40562306a36Sopenharmony_ci{
40662306a36Sopenharmony_ci	req->cqe->result.u32 = cpu_to_le32(result);
40762306a36Sopenharmony_ci}
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ci/*
41062306a36Sopenharmony_ci * NVMe command writes actually are DMA reads for us on the target side.
41162306a36Sopenharmony_ci */
41262306a36Sopenharmony_cistatic inline enum dma_data_direction
41362306a36Sopenharmony_cinvmet_data_dir(struct nvmet_req *req)
41462306a36Sopenharmony_ci{
41562306a36Sopenharmony_ci	return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
41662306a36Sopenharmony_ci}
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_cistruct nvmet_async_event {
41962306a36Sopenharmony_ci	struct list_head	entry;
42062306a36Sopenharmony_ci	u8			event_type;
42162306a36Sopenharmony_ci	u8			event_info;
42262306a36Sopenharmony_ci	u8			log_page;
42362306a36Sopenharmony_ci};
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_cistatic inline void nvmet_clear_aen_bit(struct nvmet_req *req, u32 bn)
42662306a36Sopenharmony_ci{
42762306a36Sopenharmony_ci	int rae = le32_to_cpu(req->cmd->common.cdw10) & 1 << 15;
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci	if (!rae)
43062306a36Sopenharmony_ci		clear_bit(bn, &req->sq->ctrl->aen_masked);
43162306a36Sopenharmony_ci}
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_cistatic inline bool nvmet_aen_bit_disabled(struct nvmet_ctrl *ctrl, u32 bn)
43462306a36Sopenharmony_ci{
43562306a36Sopenharmony_ci	if (!(READ_ONCE(ctrl->aen_enabled) & (1 << bn)))
43662306a36Sopenharmony_ci		return true;
43762306a36Sopenharmony_ci	return test_and_set_bit(bn, &ctrl->aen_masked);
43862306a36Sopenharmony_ci}
43962306a36Sopenharmony_ci
44062306a36Sopenharmony_civoid nvmet_get_feat_kato(struct nvmet_req *req);
44162306a36Sopenharmony_civoid nvmet_get_feat_async_event(struct nvmet_req *req);
44262306a36Sopenharmony_ciu16 nvmet_set_feat_kato(struct nvmet_req *req);
44362306a36Sopenharmony_ciu16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask);
44462306a36Sopenharmony_civoid nvmet_execute_async_event(struct nvmet_req *req);
44562306a36Sopenharmony_civoid nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl);
44662306a36Sopenharmony_civoid nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl);
44762306a36Sopenharmony_ci
44862306a36Sopenharmony_ciu16 nvmet_parse_connect_cmd(struct nvmet_req *req);
44962306a36Sopenharmony_civoid nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id);
45062306a36Sopenharmony_ciu16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req);
45162306a36Sopenharmony_ciu16 nvmet_file_parse_io_cmd(struct nvmet_req *req);
45262306a36Sopenharmony_ciu16 nvmet_bdev_zns_parse_io_cmd(struct nvmet_req *req);
45362306a36Sopenharmony_ciu16 nvmet_parse_admin_cmd(struct nvmet_req *req);
45462306a36Sopenharmony_ciu16 nvmet_parse_discovery_cmd(struct nvmet_req *req);
45562306a36Sopenharmony_ciu16 nvmet_parse_fabrics_admin_cmd(struct nvmet_req *req);
45662306a36Sopenharmony_ciu16 nvmet_parse_fabrics_io_cmd(struct nvmet_req *req);
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_cibool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
45962306a36Sopenharmony_ci		struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops);
46062306a36Sopenharmony_civoid nvmet_req_uninit(struct nvmet_req *req);
46162306a36Sopenharmony_cibool nvmet_check_transfer_len(struct nvmet_req *req, size_t len);
46262306a36Sopenharmony_cibool nvmet_check_data_len_lte(struct nvmet_req *req, size_t data_len);
46362306a36Sopenharmony_civoid nvmet_req_complete(struct nvmet_req *req, u16 status);
46462306a36Sopenharmony_ciint nvmet_req_alloc_sgls(struct nvmet_req *req);
46562306a36Sopenharmony_civoid nvmet_req_free_sgls(struct nvmet_req *req);
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_civoid nvmet_execute_set_features(struct nvmet_req *req);
46862306a36Sopenharmony_civoid nvmet_execute_get_features(struct nvmet_req *req);
46962306a36Sopenharmony_civoid nvmet_execute_keep_alive(struct nvmet_req *req);
47062306a36Sopenharmony_ci
47162306a36Sopenharmony_civoid nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid,
47262306a36Sopenharmony_ci		u16 size);
47362306a36Sopenharmony_civoid nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid,
47462306a36Sopenharmony_ci		u16 size);
47562306a36Sopenharmony_civoid nvmet_sq_destroy(struct nvmet_sq *sq);
47662306a36Sopenharmony_ciint nvmet_sq_init(struct nvmet_sq *sq);
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_civoid nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl);
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_civoid nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new);
48162306a36Sopenharmony_ciu16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
48262306a36Sopenharmony_ci		struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp);
48362306a36Sopenharmony_cistruct nvmet_ctrl *nvmet_ctrl_find_get(const char *subsysnqn,
48462306a36Sopenharmony_ci				       const char *hostnqn, u16 cntlid,
48562306a36Sopenharmony_ci				       struct nvmet_req *req);
48662306a36Sopenharmony_civoid nvmet_ctrl_put(struct nvmet_ctrl *ctrl);
48762306a36Sopenharmony_ciu16 nvmet_check_ctrl_status(struct nvmet_req *req);
48862306a36Sopenharmony_ci
48962306a36Sopenharmony_cistruct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
49062306a36Sopenharmony_ci		enum nvme_subsys_type type);
49162306a36Sopenharmony_civoid nvmet_subsys_put(struct nvmet_subsys *subsys);
49262306a36Sopenharmony_civoid nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys);
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_ciu16 nvmet_req_find_ns(struct nvmet_req *req);
49562306a36Sopenharmony_civoid nvmet_put_namespace(struct nvmet_ns *ns);
49662306a36Sopenharmony_ciint nvmet_ns_enable(struct nvmet_ns *ns);
49762306a36Sopenharmony_civoid nvmet_ns_disable(struct nvmet_ns *ns);
49862306a36Sopenharmony_cistruct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
49962306a36Sopenharmony_civoid nvmet_ns_free(struct nvmet_ns *ns);
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_civoid nvmet_send_ana_event(struct nvmet_subsys *subsys,
50262306a36Sopenharmony_ci		struct nvmet_port *port);
50362306a36Sopenharmony_civoid nvmet_port_send_ana_event(struct nvmet_port *port);
50462306a36Sopenharmony_ci
50562306a36Sopenharmony_ciint nvmet_register_transport(const struct nvmet_fabrics_ops *ops);
50662306a36Sopenharmony_civoid nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops);
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_civoid nvmet_port_del_ctrls(struct nvmet_port *port,
50962306a36Sopenharmony_ci			  struct nvmet_subsys *subsys);
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ciint nvmet_enable_port(struct nvmet_port *port);
51262306a36Sopenharmony_civoid nvmet_disable_port(struct nvmet_port *port);
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_civoid nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port);
51562306a36Sopenharmony_civoid nvmet_referral_disable(struct nvmet_port *parent, struct nvmet_port *port);
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_ciu16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
51862306a36Sopenharmony_ci		size_t len);
51962306a36Sopenharmony_ciu16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf,
52062306a36Sopenharmony_ci		size_t len);
52162306a36Sopenharmony_ciu16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len);
52262306a36Sopenharmony_ci
52362306a36Sopenharmony_ciu32 nvmet_get_log_page_len(struct nvme_command *cmd);
52462306a36Sopenharmony_ciu64 nvmet_get_log_page_offset(struct nvme_command *cmd);
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ciextern struct list_head *nvmet_ports;
52762306a36Sopenharmony_civoid nvmet_port_disc_changed(struct nvmet_port *port,
52862306a36Sopenharmony_ci		struct nvmet_subsys *subsys);
52962306a36Sopenharmony_civoid nvmet_subsys_disc_changed(struct nvmet_subsys *subsys,
53062306a36Sopenharmony_ci		struct nvmet_host *host);
53162306a36Sopenharmony_civoid nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
53262306a36Sopenharmony_ci		u8 event_info, u8 log_page);
53362306a36Sopenharmony_ci
53462306a36Sopenharmony_ci#define NVMET_QUEUE_SIZE	1024
53562306a36Sopenharmony_ci#define NVMET_NR_QUEUES		128
53662306a36Sopenharmony_ci#define NVMET_MAX_CMD		NVMET_QUEUE_SIZE
53762306a36Sopenharmony_ci
53862306a36Sopenharmony_ci/*
53962306a36Sopenharmony_ci * Nice round number that makes a list of nsids fit into a page.
54062306a36Sopenharmony_ci * Should become tunable at some point in the future.
54162306a36Sopenharmony_ci */
54262306a36Sopenharmony_ci#define NVMET_MAX_NAMESPACES	1024
54362306a36Sopenharmony_ci
54462306a36Sopenharmony_ci/*
54562306a36Sopenharmony_ci * 0 is not a valid ANA group ID, so we start numbering at 1.
54662306a36Sopenharmony_ci *
54762306a36Sopenharmony_ci * ANA Group 1 exists without manual intervention, has namespaces assigned to it
54862306a36Sopenharmony_ci * by default, and is available in an optimized state through all ports.
54962306a36Sopenharmony_ci */
55062306a36Sopenharmony_ci#define NVMET_MAX_ANAGRPS	128
55162306a36Sopenharmony_ci#define NVMET_DEFAULT_ANA_GRPID	1
55262306a36Sopenharmony_ci
55362306a36Sopenharmony_ci#define NVMET_KAS		10
55462306a36Sopenharmony_ci#define NVMET_DISC_KATO_MS		120000
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ciint __init nvmet_init_configfs(void);
55762306a36Sopenharmony_civoid __exit nvmet_exit_configfs(void);
55862306a36Sopenharmony_ci
55962306a36Sopenharmony_ciint __init nvmet_init_discovery(void);
56062306a36Sopenharmony_civoid nvmet_exit_discovery(void);
56162306a36Sopenharmony_ci
56262306a36Sopenharmony_ciextern struct nvmet_subsys *nvmet_disc_subsys;
56362306a36Sopenharmony_ciextern struct rw_semaphore nvmet_config_sem;
56462306a36Sopenharmony_ci
56562306a36Sopenharmony_ciextern u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
56662306a36Sopenharmony_ciextern u64 nvmet_ana_chgcnt;
56762306a36Sopenharmony_ciextern struct rw_semaphore nvmet_ana_sem;
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_cibool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn);
57062306a36Sopenharmony_ci
57162306a36Sopenharmony_ciint nvmet_bdev_ns_enable(struct nvmet_ns *ns);
57262306a36Sopenharmony_ciint nvmet_file_ns_enable(struct nvmet_ns *ns);
57362306a36Sopenharmony_civoid nvmet_bdev_ns_disable(struct nvmet_ns *ns);
57462306a36Sopenharmony_civoid nvmet_file_ns_disable(struct nvmet_ns *ns);
57562306a36Sopenharmony_ciu16 nvmet_bdev_flush(struct nvmet_req *req);
57662306a36Sopenharmony_ciu16 nvmet_file_flush(struct nvmet_req *req);
57762306a36Sopenharmony_civoid nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
57862306a36Sopenharmony_civoid nvmet_bdev_ns_revalidate(struct nvmet_ns *ns);
57962306a36Sopenharmony_civoid nvmet_file_ns_revalidate(struct nvmet_ns *ns);
58062306a36Sopenharmony_cibool nvmet_ns_revalidate(struct nvmet_ns *ns);
58162306a36Sopenharmony_ciu16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts);
58262306a36Sopenharmony_ci
58362306a36Sopenharmony_cibool nvmet_bdev_zns_enable(struct nvmet_ns *ns);
58462306a36Sopenharmony_civoid nvmet_execute_identify_ctrl_zns(struct nvmet_req *req);
58562306a36Sopenharmony_civoid nvmet_execute_identify_ns_zns(struct nvmet_req *req);
58662306a36Sopenharmony_civoid nvmet_bdev_execute_zone_mgmt_recv(struct nvmet_req *req);
58762306a36Sopenharmony_civoid nvmet_bdev_execute_zone_mgmt_send(struct nvmet_req *req);
58862306a36Sopenharmony_civoid nvmet_bdev_execute_zone_append(struct nvmet_req *req);
58962306a36Sopenharmony_ci
59062306a36Sopenharmony_cistatic inline u32 nvmet_rw_data_len(struct nvmet_req *req)
59162306a36Sopenharmony_ci{
59262306a36Sopenharmony_ci	return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) <<
59362306a36Sopenharmony_ci			req->ns->blksize_shift;
59462306a36Sopenharmony_ci}
59562306a36Sopenharmony_ci
59662306a36Sopenharmony_cistatic inline u32 nvmet_rw_metadata_len(struct nvmet_req *req)
59762306a36Sopenharmony_ci{
59862306a36Sopenharmony_ci	if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
59962306a36Sopenharmony_ci		return 0;
60062306a36Sopenharmony_ci	return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) *
60162306a36Sopenharmony_ci			req->ns->metadata_size;
60262306a36Sopenharmony_ci}
60362306a36Sopenharmony_ci
60462306a36Sopenharmony_cistatic inline u32 nvmet_dsm_len(struct nvmet_req *req)
60562306a36Sopenharmony_ci{
60662306a36Sopenharmony_ci	return (le32_to_cpu(req->cmd->dsm.nr) + 1) *
60762306a36Sopenharmony_ci		sizeof(struct nvme_dsm_range);
60862306a36Sopenharmony_ci}
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_cistatic inline struct nvmet_subsys *nvmet_req_subsys(struct nvmet_req *req)
61162306a36Sopenharmony_ci{
61262306a36Sopenharmony_ci	return req->sq->ctrl->subsys;
61362306a36Sopenharmony_ci}
61462306a36Sopenharmony_ci
61562306a36Sopenharmony_cistatic inline bool nvmet_is_disc_subsys(struct nvmet_subsys *subsys)
61662306a36Sopenharmony_ci{
61762306a36Sopenharmony_ci    return subsys->type != NVME_NQN_NVME;
61862306a36Sopenharmony_ci}
61962306a36Sopenharmony_ci
62062306a36Sopenharmony_ci#ifdef CONFIG_NVME_TARGET_PASSTHRU
62162306a36Sopenharmony_civoid nvmet_passthru_subsys_free(struct nvmet_subsys *subsys);
62262306a36Sopenharmony_ciint nvmet_passthru_ctrl_enable(struct nvmet_subsys *subsys);
62362306a36Sopenharmony_civoid nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys);
62462306a36Sopenharmony_ciu16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req);
62562306a36Sopenharmony_ciu16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req);
62662306a36Sopenharmony_cistatic inline bool nvmet_is_passthru_subsys(struct nvmet_subsys *subsys)
62762306a36Sopenharmony_ci{
62862306a36Sopenharmony_ci	return subsys->passthru_ctrl;
62962306a36Sopenharmony_ci}
63062306a36Sopenharmony_ci#else /* CONFIG_NVME_TARGET_PASSTHRU */
63162306a36Sopenharmony_cistatic inline void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys)
63262306a36Sopenharmony_ci{
63362306a36Sopenharmony_ci}
63462306a36Sopenharmony_cistatic inline void nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys)
63562306a36Sopenharmony_ci{
63662306a36Sopenharmony_ci}
63762306a36Sopenharmony_cistatic inline u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req)
63862306a36Sopenharmony_ci{
63962306a36Sopenharmony_ci	return 0;
64062306a36Sopenharmony_ci}
64162306a36Sopenharmony_cistatic inline u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req)
64262306a36Sopenharmony_ci{
64362306a36Sopenharmony_ci	return 0;
64462306a36Sopenharmony_ci}
64562306a36Sopenharmony_cistatic inline bool nvmet_is_passthru_subsys(struct nvmet_subsys *subsys)
64662306a36Sopenharmony_ci{
64762306a36Sopenharmony_ci	return NULL;
64862306a36Sopenharmony_ci}
64962306a36Sopenharmony_ci#endif /* CONFIG_NVME_TARGET_PASSTHRU */
65062306a36Sopenharmony_ci
65162306a36Sopenharmony_cistatic inline bool nvmet_is_passthru_req(struct nvmet_req *req)
65262306a36Sopenharmony_ci{
65362306a36Sopenharmony_ci	return nvmet_is_passthru_subsys(nvmet_req_subsys(req));
65462306a36Sopenharmony_ci}
65562306a36Sopenharmony_ci
65662306a36Sopenharmony_civoid nvmet_passthrough_override_cap(struct nvmet_ctrl *ctrl);
65762306a36Sopenharmony_ci
65862306a36Sopenharmony_ciu16 errno_to_nvme_status(struct nvmet_req *req, int errno);
65962306a36Sopenharmony_ciu16 nvmet_report_invalid_opcode(struct nvmet_req *req);
66062306a36Sopenharmony_ci
66162306a36Sopenharmony_ci/* Convert a 32-bit number to a 16-bit 0's based number */
66262306a36Sopenharmony_cistatic inline __le16 to0based(u32 a)
66362306a36Sopenharmony_ci{
66462306a36Sopenharmony_ci	return cpu_to_le16(max(1U, min(1U << 16, a)) - 1);
66562306a36Sopenharmony_ci}
66662306a36Sopenharmony_ci
66762306a36Sopenharmony_cistatic inline bool nvmet_ns_has_pi(struct nvmet_ns *ns)
66862306a36Sopenharmony_ci{
66962306a36Sopenharmony_ci	if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
67062306a36Sopenharmony_ci		return false;
67162306a36Sopenharmony_ci	return ns->pi_type && ns->metadata_size == sizeof(struct t10_pi_tuple);
67262306a36Sopenharmony_ci}
67362306a36Sopenharmony_ci
67462306a36Sopenharmony_cistatic inline __le64 nvmet_sect_to_lba(struct nvmet_ns *ns, sector_t sect)
67562306a36Sopenharmony_ci{
67662306a36Sopenharmony_ci	return cpu_to_le64(sect >> (ns->blksize_shift - SECTOR_SHIFT));
67762306a36Sopenharmony_ci}
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_cistatic inline sector_t nvmet_lba_to_sect(struct nvmet_ns *ns, __le64 lba)
68062306a36Sopenharmony_ci{
68162306a36Sopenharmony_ci	return le64_to_cpu(lba) << (ns->blksize_shift - SECTOR_SHIFT);
68262306a36Sopenharmony_ci}
68362306a36Sopenharmony_ci
68462306a36Sopenharmony_cistatic inline bool nvmet_use_inline_bvec(struct nvmet_req *req)
68562306a36Sopenharmony_ci{
68662306a36Sopenharmony_ci	return req->transfer_len <= NVMET_MAX_INLINE_DATA_LEN &&
68762306a36Sopenharmony_ci	       req->sg_cnt <= NVMET_MAX_INLINE_BIOVEC;
68862306a36Sopenharmony_ci}
68962306a36Sopenharmony_ci
69062306a36Sopenharmony_cistatic inline void nvmet_req_bio_put(struct nvmet_req *req, struct bio *bio)
69162306a36Sopenharmony_ci{
69262306a36Sopenharmony_ci	if (bio != &req->b.inline_bio)
69362306a36Sopenharmony_ci		bio_put(bio);
69462306a36Sopenharmony_ci}
69562306a36Sopenharmony_ci
69662306a36Sopenharmony_ci#ifdef CONFIG_NVME_TARGET_AUTH
69762306a36Sopenharmony_civoid nvmet_execute_auth_send(struct nvmet_req *req);
69862306a36Sopenharmony_civoid nvmet_execute_auth_receive(struct nvmet_req *req);
69962306a36Sopenharmony_ciint nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
70062306a36Sopenharmony_ci		       bool set_ctrl);
70162306a36Sopenharmony_ciint nvmet_auth_set_host_hash(struct nvmet_host *host, const char *hash);
70262306a36Sopenharmony_ciint nvmet_setup_auth(struct nvmet_ctrl *ctrl);
70362306a36Sopenharmony_civoid nvmet_auth_sq_init(struct nvmet_sq *sq);
70462306a36Sopenharmony_civoid nvmet_destroy_auth(struct nvmet_ctrl *ctrl);
70562306a36Sopenharmony_civoid nvmet_auth_sq_free(struct nvmet_sq *sq);
70662306a36Sopenharmony_ciint nvmet_setup_dhgroup(struct nvmet_ctrl *ctrl, u8 dhgroup_id);
70762306a36Sopenharmony_cibool nvmet_check_auth_status(struct nvmet_req *req);
70862306a36Sopenharmony_ciint nvmet_auth_host_hash(struct nvmet_req *req, u8 *response,
70962306a36Sopenharmony_ci			 unsigned int hash_len);
71062306a36Sopenharmony_ciint nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response,
71162306a36Sopenharmony_ci			 unsigned int hash_len);
71262306a36Sopenharmony_cistatic inline bool nvmet_has_auth(struct nvmet_ctrl *ctrl)
71362306a36Sopenharmony_ci{
71462306a36Sopenharmony_ci	return ctrl->host_key != NULL;
71562306a36Sopenharmony_ci}
71662306a36Sopenharmony_ciint nvmet_auth_ctrl_exponential(struct nvmet_req *req,
71762306a36Sopenharmony_ci				u8 *buf, int buf_size);
71862306a36Sopenharmony_ciint nvmet_auth_ctrl_sesskey(struct nvmet_req *req,
71962306a36Sopenharmony_ci			    u8 *buf, int buf_size);
72062306a36Sopenharmony_ci#else
72162306a36Sopenharmony_cistatic inline int nvmet_setup_auth(struct nvmet_ctrl *ctrl)
72262306a36Sopenharmony_ci{
72362306a36Sopenharmony_ci	return 0;
72462306a36Sopenharmony_ci}
72562306a36Sopenharmony_cistatic inline void nvmet_auth_sq_init(struct nvmet_sq *sq)
72662306a36Sopenharmony_ci{
72762306a36Sopenharmony_ci}
72862306a36Sopenharmony_cistatic inline void nvmet_destroy_auth(struct nvmet_ctrl *ctrl) {};
72962306a36Sopenharmony_cistatic inline void nvmet_auth_sq_free(struct nvmet_sq *sq) {};
73062306a36Sopenharmony_cistatic inline bool nvmet_check_auth_status(struct nvmet_req *req)
73162306a36Sopenharmony_ci{
73262306a36Sopenharmony_ci	return true;
73362306a36Sopenharmony_ci}
73462306a36Sopenharmony_cistatic inline bool nvmet_has_auth(struct nvmet_ctrl *ctrl)
73562306a36Sopenharmony_ci{
73662306a36Sopenharmony_ci	return false;
73762306a36Sopenharmony_ci}
73862306a36Sopenharmony_cistatic inline const char *nvmet_dhchap_dhgroup_name(u8 dhgid) { return NULL; }
73962306a36Sopenharmony_ci#endif
74062306a36Sopenharmony_ci
74162306a36Sopenharmony_ci#endif /* _NVMET_H */
742