xref: /openbmc/linux/drivers/nvme/host/nvme.h (revision bd83fe6f)
1bc50ad75SChristoph Hellwig /* SPDX-License-Identifier: GPL-2.0 */
257dacad5SJay Sternberg /*
357dacad5SJay Sternberg  * Copyright (c) 2011-2014, Intel Corporation.
457dacad5SJay Sternberg  */
557dacad5SJay Sternberg 
657dacad5SJay Sternberg #ifndef _NVME_H
757dacad5SJay Sternberg #define _NVME_H
857dacad5SJay Sternberg 
957dacad5SJay Sternberg #include <linux/nvme.h>
10a6a5149bSChristoph Hellwig #include <linux/cdev.h>
1157dacad5SJay Sternberg #include <linux/pci.h>
1257dacad5SJay Sternberg #include <linux/kref.h>
1357dacad5SJay Sternberg #include <linux/blk-mq.h>
14a98e58e5SScott Bauer #include <linux/sed-opal.h>
15b9e03857SThomas Tai #include <linux/fault-inject.h>
16978628ecSJohannes Thumshirn #include <linux/rcupdate.h>
17c1ac9a4bSKeith Busch #include <linux/wait.h>
184d2ce688SJames Smart #include <linux/t10-pi.h>
1957dacad5SJay Sternberg 
2035fe0d12SHannes Reinecke #include <trace/events/block.h>
2135fe0d12SHannes Reinecke 
228ae4e447SMarc Olson extern unsigned int nvme_io_timeout;
2357dacad5SJay Sternberg #define NVME_IO_TIMEOUT	(nvme_io_timeout * HZ)
2457dacad5SJay Sternberg 
258ae4e447SMarc Olson extern unsigned int admin_timeout;
26dc96f938SChaitanya Kulkarni #define NVME_ADMIN_TIMEOUT	(admin_timeout * HZ)
2721d34711SChristoph Hellwig 
28038bd4cbSSagi Grimberg #define NVME_DEFAULT_KATO	5
29038bd4cbSSagi Grimberg 
3038e18002SIsrael Rukshin #ifdef CONFIG_ARCH_NO_SG_CHAIN
3138e18002SIsrael Rukshin #define  NVME_INLINE_SG_CNT  0
32ba7ca2aeSIsrael Rukshin #define  NVME_INLINE_METADATA_SG_CNT  0
3338e18002SIsrael Rukshin #else
3438e18002SIsrael Rukshin #define  NVME_INLINE_SG_CNT  2
35ba7ca2aeSIsrael Rukshin #define  NVME_INLINE_METADATA_SG_CNT  1
3638e18002SIsrael Rukshin #endif
3738e18002SIsrael Rukshin 
386c3c05b0SChaitanya Kulkarni /*
396c3c05b0SChaitanya Kulkarni  * Default to a 4K page size, with the intention to update this
406c3c05b0SChaitanya Kulkarni  * path in the future to accommodate architectures with differing
416c3c05b0SChaitanya Kulkarni  * kernel and IO page sizes.
426c3c05b0SChaitanya Kulkarni  */
436c3c05b0SChaitanya Kulkarni #define NVME_CTRL_PAGE_SHIFT	12
446c3c05b0SChaitanya Kulkarni #define NVME_CTRL_PAGE_SIZE	(1 << NVME_CTRL_PAGE_SHIFT)
456c3c05b0SChaitanya Kulkarni 
469a6327d2SSagi Grimberg extern struct workqueue_struct *nvme_wq;
47b227c59bSRoy Shterman extern struct workqueue_struct *nvme_reset_wq;
48b227c59bSRoy Shterman extern struct workqueue_struct *nvme_delete_wq;
499a6327d2SSagi Grimberg 
5057dacad5SJay Sternberg /*
51106198edSChristoph Hellwig  * List of workarounds for devices that required behavior not specified in
52106198edSChristoph Hellwig  * the standard.
5357dacad5SJay Sternberg  */
54106198edSChristoph Hellwig enum nvme_quirks {
55106198edSChristoph Hellwig 	/*
56106198edSChristoph Hellwig 	 * Prefers I/O aligned to a stripe size specified in a vendor
57106198edSChristoph Hellwig 	 * specific Identify field.
58106198edSChristoph Hellwig 	 */
59106198edSChristoph Hellwig 	NVME_QUIRK_STRIPE_SIZE			= (1 << 0),
60540c801cSKeith Busch 
61540c801cSKeith Busch 	/*
62540c801cSKeith Busch 	 * The controller doesn't handle Identify value others than 0 or 1
63540c801cSKeith Busch 	 * correctly.
64540c801cSKeith Busch 	 */
65540c801cSKeith Busch 	NVME_QUIRK_IDENTIFY_CNS			= (1 << 1),
6608095e70SKeith Busch 
6708095e70SKeith Busch 	/*
68e850fd16SChristoph Hellwig 	 * The controller deterministically returns O's on reads to
69e850fd16SChristoph Hellwig 	 * logical blocks that deallocate was called on.
7008095e70SKeith Busch 	 */
71e850fd16SChristoph Hellwig 	NVME_QUIRK_DEALLOCATE_ZEROES		= (1 << 2),
7254adc010SGuilherme G. Piccoli 
7354adc010SGuilherme G. Piccoli 	/*
7454adc010SGuilherme G. Piccoli 	 * The controller needs a delay before starts checking the device
7554adc010SGuilherme G. Piccoli 	 * readiness, which is done by reading the NVME_CSTS_RDY bit.
7654adc010SGuilherme G. Piccoli 	 */
7754adc010SGuilherme G. Piccoli 	NVME_QUIRK_DELAY_BEFORE_CHK_RDY		= (1 << 3),
78c5552fdeSAndy Lutomirski 
79c5552fdeSAndy Lutomirski 	/*
80c5552fdeSAndy Lutomirski 	 * APST should not be used.
81c5552fdeSAndy Lutomirski 	 */
82c5552fdeSAndy Lutomirski 	NVME_QUIRK_NO_APST			= (1 << 4),
83ff5350a8SAndy Lutomirski 
84ff5350a8SAndy Lutomirski 	/*
85ff5350a8SAndy Lutomirski 	 * The deepest sleep state should not be used.
86ff5350a8SAndy Lutomirski 	 */
87ff5350a8SAndy Lutomirski 	NVME_QUIRK_NO_DEEPEST_PS		= (1 << 5),
88608cc4b1SChristoph Hellwig 
89608cc4b1SChristoph Hellwig 	/*
909abd68efSJens Axboe 	 * Set MEDIUM priority on SQ creation
919abd68efSJens Axboe 	 */
929abd68efSJens Axboe 	NVME_QUIRK_MEDIUM_PRIO_SQ		= (1 << 7),
936299358dSJames Dingwall 
946299358dSJames Dingwall 	/*
956299358dSJames Dingwall 	 * Ignore device provided subnqn.
966299358dSJames Dingwall 	 */
976299358dSJames Dingwall 	NVME_QUIRK_IGNORE_DEV_SUBNQN		= (1 << 8),
987b210e4eSChristoph Hellwig 
997b210e4eSChristoph Hellwig 	/*
1007b210e4eSChristoph Hellwig 	 * Broken Write Zeroes.
1017b210e4eSChristoph Hellwig 	 */
1027b210e4eSChristoph Hellwig 	NVME_QUIRK_DISABLE_WRITE_ZEROES		= (1 << 9),
103cb32de1bSMario Limonciello 
104cb32de1bSMario Limonciello 	/*
105cb32de1bSMario Limonciello 	 * Force simple suspend/resume path.
106cb32de1bSMario Limonciello 	 */
107cb32de1bSMario Limonciello 	NVME_QUIRK_SIMPLE_SUSPEND		= (1 << 10),
1087ad67ca5SLinus Torvalds 
1097ad67ca5SLinus Torvalds 	/*
11066341331SBenjamin Herrenschmidt 	 * Use only one interrupt vector for all queues
11166341331SBenjamin Herrenschmidt 	 */
1127ad67ca5SLinus Torvalds 	NVME_QUIRK_SINGLE_VECTOR		= (1 << 11),
11366341331SBenjamin Herrenschmidt 
11466341331SBenjamin Herrenschmidt 	/*
11566341331SBenjamin Herrenschmidt 	 * Use non-standard 128 bytes SQEs.
11666341331SBenjamin Herrenschmidt 	 */
1177ad67ca5SLinus Torvalds 	NVME_QUIRK_128_BYTES_SQES		= (1 << 12),
118d38e9f04SBenjamin Herrenschmidt 
119d38e9f04SBenjamin Herrenschmidt 	/*
120d38e9f04SBenjamin Herrenschmidt 	 * Prevent tag overlap between queues
121d38e9f04SBenjamin Herrenschmidt 	 */
1227ad67ca5SLinus Torvalds 	NVME_QUIRK_SHARED_TAGS                  = (1 << 13),
1236c6aa2f2SAkinobu Mita 
1246c6aa2f2SAkinobu Mita 	/*
1256c6aa2f2SAkinobu Mita 	 * Don't change the value of the temperature threshold feature
1266c6aa2f2SAkinobu Mita 	 */
1276c6aa2f2SAkinobu Mita 	NVME_QUIRK_NO_TEMP_THRESH_CHANGE	= (1 << 14),
1285bedd3afSChristoph Hellwig 
1295bedd3afSChristoph Hellwig 	/*
1305bedd3afSChristoph Hellwig 	 * The controller doesn't handle the Identify Namespace
1315bedd3afSChristoph Hellwig 	 * Identification Descriptor list subcommand despite claiming
1325bedd3afSChristoph Hellwig 	 * NVMe 1.3 compliance.
1335bedd3afSChristoph Hellwig 	 */
1345bedd3afSChristoph Hellwig 	NVME_QUIRK_NO_NS_DESC_LIST		= (1 << 15),
1354bdf2603SFilippo Sironi 
1364bdf2603SFilippo Sironi 	/*
1374bdf2603SFilippo Sironi 	 * The controller does not properly handle DMA addresses over
1384bdf2603SFilippo Sironi 	 * 48 bits.
1394bdf2603SFilippo Sironi 	 */
1404bdf2603SFilippo Sironi 	NVME_QUIRK_DMA_ADDRESS_BITS_48		= (1 << 16),
141a2941f6aSKeith Busch 
142a2941f6aSKeith Busch 	/*
143a2941f6aSKeith Busch 	 * The controller requires the command_id value be be limited, so skip
144a2941f6aSKeith Busch 	 * encoding the generation sequence number.
145a2941f6aSKeith Busch 	 */
146a2941f6aSKeith Busch 	NVME_QUIRK_SKIP_CID_GEN			= (1 << 17),
147106198edSChristoph Hellwig };
148106198edSChristoph Hellwig 
149d49187e9SChristoph Hellwig /*
150d49187e9SChristoph Hellwig  * Common request structure for NVMe passthrough.  All drivers must have
151d49187e9SChristoph Hellwig  * this structure as the first member of their request-private data.
152d49187e9SChristoph Hellwig  */
153d49187e9SChristoph Hellwig struct nvme_request {
154d49187e9SChristoph Hellwig 	struct nvme_command	*cmd;
155d49187e9SChristoph Hellwig 	union nvme_result	result;
156e7006de6SSagi Grimberg 	u8			genctr;
15744e44b29SChristoph Hellwig 	u8			retries;
15827fa9bc5SChristoph Hellwig 	u8			flags;
15927fa9bc5SChristoph Hellwig 	u16			status;
16059e29ce6SSagi Grimberg 	struct nvme_ctrl	*ctrl;
16127fa9bc5SChristoph Hellwig };
16227fa9bc5SChristoph Hellwig 
16332acab31SChristoph Hellwig /*
16432acab31SChristoph Hellwig  * Mark a bio as coming in through the mpath node.
16532acab31SChristoph Hellwig  */
16632acab31SChristoph Hellwig #define REQ_NVME_MPATH		REQ_DRV
16732acab31SChristoph Hellwig 
16827fa9bc5SChristoph Hellwig enum {
16927fa9bc5SChristoph Hellwig 	NVME_REQ_CANCELLED		= (1 << 0),
170bb06ec31SJames Smart 	NVME_REQ_USERCMD		= (1 << 1),
171d49187e9SChristoph Hellwig };
172d49187e9SChristoph Hellwig 
173d49187e9SChristoph Hellwig static inline struct nvme_request *nvme_req(struct request *req)
174d49187e9SChristoph Hellwig {
175d49187e9SChristoph Hellwig 	return blk_mq_rq_to_pdu(req);
176d49187e9SChristoph Hellwig }
177d49187e9SChristoph Hellwig 
1785d87eb94SKeith Busch static inline u16 nvme_req_qid(struct request *req)
1795d87eb94SKeith Busch {
180643c476dSKeith Busch 	if (!req->q->queuedata)
1815d87eb94SKeith Busch 		return 0;
18284115d6dSBaolin Wang 
18384115d6dSBaolin Wang 	return req->mq_hctx->queue_num + 1;
1845d87eb94SKeith Busch }
1855d87eb94SKeith Busch 
18654adc010SGuilherme G. Piccoli /* The below value is the specific amount of delay needed before checking
18754adc010SGuilherme G. Piccoli  * readiness in case of the PCI_DEVICE(0x1c58, 0x0003), which needs the
18854adc010SGuilherme G. Piccoli  * NVME_QUIRK_DELAY_BEFORE_CHK_RDY quirk enabled. The value (in ms) was
18954adc010SGuilherme G. Piccoli  * found empirically.
19054adc010SGuilherme G. Piccoli  */
1918c97eeccSJeff Lien #define NVME_QUIRK_DELAY_AMOUNT		2300
19254adc010SGuilherme G. Piccoli 
1934212f4e9SSagi Grimberg /*
1944212f4e9SSagi Grimberg  * enum nvme_ctrl_state: Controller state
1954212f4e9SSagi Grimberg  *
1964212f4e9SSagi Grimberg  * @NVME_CTRL_NEW:		New controller just allocated, initial state
1974212f4e9SSagi Grimberg  * @NVME_CTRL_LIVE:		Controller is connected and I/O capable
1984212f4e9SSagi Grimberg  * @NVME_CTRL_RESETTING:	Controller is resetting (or scheduled reset)
1994212f4e9SSagi Grimberg  * @NVME_CTRL_CONNECTING:	Controller is disconnected, now connecting the
2004212f4e9SSagi Grimberg  *				transport
2014212f4e9SSagi Grimberg  * @NVME_CTRL_DELETING:		Controller is deleting (or scheduled deletion)
202ecca390eSSagi Grimberg  * @NVME_CTRL_DELETING_NOIO:	Controller is deleting and I/O is not
203ecca390eSSagi Grimberg  *				disabled/failed immediately. This state comes
204ecca390eSSagi Grimberg  * 				after all async event processing took place and
205ecca390eSSagi Grimberg  * 				before ns removal and the controller deletion
206ecca390eSSagi Grimberg  * 				progress
2074212f4e9SSagi Grimberg  * @NVME_CTRL_DEAD:		Controller is non-present/unresponsive during
2084212f4e9SSagi Grimberg  *				shutdown or removal. In this case we forcibly
2094212f4e9SSagi Grimberg  *				kill all inflight I/O as they have no chance to
2104212f4e9SSagi Grimberg  *				complete
2114212f4e9SSagi Grimberg  */
212bb8d261eSChristoph Hellwig enum nvme_ctrl_state {
213bb8d261eSChristoph Hellwig 	NVME_CTRL_NEW,
214bb8d261eSChristoph Hellwig 	NVME_CTRL_LIVE,
215bb8d261eSChristoph Hellwig 	NVME_CTRL_RESETTING,
216ad6a0a52SMax Gurtovoy 	NVME_CTRL_CONNECTING,
217bb8d261eSChristoph Hellwig 	NVME_CTRL_DELETING,
218ecca390eSSagi Grimberg 	NVME_CTRL_DELETING_NOIO,
2190ff9d4e1SKeith Busch 	NVME_CTRL_DEAD,
220bb8d261eSChristoph Hellwig };
221bb8d261eSChristoph Hellwig 
222a3646451SAkinobu Mita struct nvme_fault_inject {
223a3646451SAkinobu Mita #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
224a3646451SAkinobu Mita 	struct fault_attr attr;
225a3646451SAkinobu Mita 	struct dentry *parent;
226a3646451SAkinobu Mita 	bool dont_retry;	/* DNR, do not retry */
227a3646451SAkinobu Mita 	u16 status;		/* status code */
228a3646451SAkinobu Mita #endif
229a3646451SAkinobu Mita };
230a3646451SAkinobu Mita 
2311c63dc66SChristoph Hellwig struct nvme_ctrl {
2326e3ca03eSSagi Grimberg 	bool comp_seen;
233bb8d261eSChristoph Hellwig 	enum nvme_ctrl_state state;
234bd4da3abSAndy Lutomirski 	bool identified;
235bb8d261eSChristoph Hellwig 	spinlock_t lock;
236e7ad43c3SKeith Busch 	struct mutex scan_lock;
2371c63dc66SChristoph Hellwig 	const struct nvme_ctrl_ops *ops;
23857dacad5SJay Sternberg 	struct request_queue *admin_q;
23907bfcd09SChristoph Hellwig 	struct request_queue *connect_q;
240e7832cb4SSagi Grimberg 	struct request_queue *fabrics_q;
24157dacad5SJay Sternberg 	struct device *dev;
24257dacad5SJay Sternberg 	int instance;
243103e515eSHannes Reinecke 	int numa_node;
2445bae7f73SChristoph Hellwig 	struct blk_mq_tag_set *tagset;
24534b6c231SSagi Grimberg 	struct blk_mq_tag_set *admin_tagset;
2465bae7f73SChristoph Hellwig 	struct list_head namespaces;
247765cc031SJianchao Wang 	struct rw_semaphore namespaces_rwsem;
248d22524a4SChristoph Hellwig 	struct device ctrl_device;
2495bae7f73SChristoph Hellwig 	struct device *device;	/* char device */
250ed7770f6SHannes Reinecke #ifdef CONFIG_NVME_HWMON
251ed7770f6SHannes Reinecke 	struct device *hwmon_device;
252ed7770f6SHannes Reinecke #endif
253a6a5149bSChristoph Hellwig 	struct cdev cdev;
254d86c4d8eSChristoph Hellwig 	struct work_struct reset_work;
255c5017e85SChristoph Hellwig 	struct work_struct delete_work;
256c1ac9a4bSKeith Busch 	wait_queue_head_t state_wq;
2571c63dc66SChristoph Hellwig 
258ab9e00ccSChristoph Hellwig 	struct nvme_subsystem *subsys;
259ab9e00ccSChristoph Hellwig 	struct list_head subsys_entry;
260ab9e00ccSChristoph Hellwig 
2614f1244c8SChristoph Hellwig 	struct opal_dev *opal_dev;
262a98e58e5SScott Bauer 
26357dacad5SJay Sternberg 	char name[12];
26476e3914aSChristoph Hellwig 	u16 cntlid;
2655fd4ce1bSChristoph Hellwig 
2665fd4ce1bSChristoph Hellwig 	u32 ctrl_config;
267b6dccf7fSArnav Dawn 	u16 mtfa;
268d858e5f0SSagi Grimberg 	u32 queue_count;
2695fd4ce1bSChristoph Hellwig 
27020d0dfe6SSagi Grimberg 	u64 cap;
27157dacad5SJay Sternberg 	u32 max_hw_sectors;
272943e942eSJens Axboe 	u32 max_segments;
27395093350SMax Gurtovoy 	u32 max_integrity_segments;
2745befc7c2SKeith Busch 	u32 max_discard_sectors;
2755befc7c2SKeith Busch 	u32 max_discard_segments;
2765befc7c2SKeith Busch 	u32 max_zeroes_sectors;
277240e6ee2SKeith Busch #ifdef CONFIG_BLK_DEV_ZONED
278240e6ee2SKeith Busch 	u32 max_zone_append;
279240e6ee2SKeith Busch #endif
28049cd84b6SKeith Busch 	u16 crdt[3];
28157dacad5SJay Sternberg 	u16 oncs;
2828a9ae523SScott Bauer 	u16 oacs;
283f5d11840SJens Axboe 	u16 nssa;
284f5d11840SJens Axboe 	u16 nr_streams;
285f968688fSKeith Busch 	u16 sqsize;
2860d0b660fSChristoph Hellwig 	u32 max_namespaces;
2876bf25d16SChristoph Hellwig 	atomic_t abort_limit;
28857dacad5SJay Sternberg 	u8 vwc;
289f3ca80fcSChristoph Hellwig 	u32 vs;
29007bfcd09SChristoph Hellwig 	u32 sgls;
291038bd4cbSSagi Grimberg 	u16 kas;
292c5552fdeSAndy Lutomirski 	u8 npss;
293c5552fdeSAndy Lutomirski 	u8 apsta;
294400b6a7bSGuenter Roeck 	u16 wctemp;
295400b6a7bSGuenter Roeck 	u16 cctemp;
296c0561f82SHannes Reinecke 	u32 oaes;
297e3d7874dSKeith Busch 	u32 aen_result;
2983e53ba38SSagi Grimberg 	u32 ctratt;
29907fbd32aSMartin K. Petersen 	unsigned int shutdown_timeout;
300038bd4cbSSagi Grimberg 	unsigned int kato;
301f3ca80fcSChristoph Hellwig 	bool subsystem;
302106198edSChristoph Hellwig 	unsigned long quirks;
303c5552fdeSAndy Lutomirski 	struct nvme_id_power_state psd[32];
30484fef62dSKeith Busch 	struct nvme_effects_log *effects;
3051cf7a12eSChaitanya Kulkarni 	struct xarray cels;
3065955be21SChristoph Hellwig 	struct work_struct scan_work;
307f866fc42SChristoph Hellwig 	struct work_struct async_event_work;
308038bd4cbSSagi Grimberg 	struct delayed_work ka_work;
3098c4dfea9SVictor Gladkov 	struct delayed_work failfast_work;
3100a34e466SRoland Dreier 	struct nvme_command ka_cmd;
311b6dccf7fSArnav Dawn 	struct work_struct fw_act_work;
31230d90964SChristoph Hellwig 	unsigned long events;
31307bfcd09SChristoph Hellwig 
3140d0b660fSChristoph Hellwig #ifdef CONFIG_NVME_MULTIPATH
3150d0b660fSChristoph Hellwig 	/* asymmetric namespace access: */
3160d0b660fSChristoph Hellwig 	u8 anacap;
3170d0b660fSChristoph Hellwig 	u8 anatt;
3180d0b660fSChristoph Hellwig 	u32 anagrpmax;
3190d0b660fSChristoph Hellwig 	u32 nanagrpid;
3200d0b660fSChristoph Hellwig 	struct mutex ana_lock;
3210d0b660fSChristoph Hellwig 	struct nvme_ana_rsp_hdr *ana_log_buf;
3220d0b660fSChristoph Hellwig 	size_t ana_log_size;
3230d0b660fSChristoph Hellwig 	struct timer_list anatt_timer;
3240d0b660fSChristoph Hellwig 	struct work_struct ana_work;
3250d0b660fSChristoph Hellwig #endif
3260d0b660fSChristoph Hellwig 
327c5552fdeSAndy Lutomirski 	/* Power saving configuration */
328c5552fdeSAndy Lutomirski 	u64 ps_max_latency_us;
32976a5af84SKai-Heng Feng 	bool apst_enabled;
330c5552fdeSAndy Lutomirski 
331044a9df1SChristoph Hellwig 	/* PCIe only: */
332fe6d53c9SChristoph Hellwig 	u32 hmpre;
333fe6d53c9SChristoph Hellwig 	u32 hmmin;
334044a9df1SChristoph Hellwig 	u32 hmminds;
335044a9df1SChristoph Hellwig 	u16 hmmaxd;
336fe6d53c9SChristoph Hellwig 
33707bfcd09SChristoph Hellwig 	/* Fabrics only */
33807bfcd09SChristoph Hellwig 	u32 ioccsz;
33907bfcd09SChristoph Hellwig 	u32 iorcsz;
34007bfcd09SChristoph Hellwig 	u16 icdoff;
34107bfcd09SChristoph Hellwig 	u16 maxcmd;
342fdf9dfa8SSagi Grimberg 	int nr_reconnects;
3438c4dfea9SVictor Gladkov 	unsigned long flags;
3448c4dfea9SVictor Gladkov #define NVME_CTRL_FAILFAST_EXPIRED	0
3459e6a6b12SMing Lei #define NVME_CTRL_ADMIN_Q_STOPPED	1
34607bfcd09SChristoph Hellwig 	struct nvmf_ctrl_options *opts;
347cb5b7262SJens Axboe 
348cb5b7262SJens Axboe 	struct page *discard_page;
349cb5b7262SJens Axboe 	unsigned long discard_page_busy;
350f79d5fdaSAkinobu Mita 
351f79d5fdaSAkinobu Mita 	struct nvme_fault_inject fault_inject;
35257dacad5SJay Sternberg };
35357dacad5SJay Sternberg 
35475c10e73SHannes Reinecke enum nvme_iopolicy {
35575c10e73SHannes Reinecke 	NVME_IOPOLICY_NUMA,
35675c10e73SHannes Reinecke 	NVME_IOPOLICY_RR,
35775c10e73SHannes Reinecke };
35875c10e73SHannes Reinecke 
359ab9e00ccSChristoph Hellwig struct nvme_subsystem {
360ab9e00ccSChristoph Hellwig 	int			instance;
361ab9e00ccSChristoph Hellwig 	struct device		dev;
362ab9e00ccSChristoph Hellwig 	/*
363ab9e00ccSChristoph Hellwig 	 * Because we unregister the device on the last put we need
364ab9e00ccSChristoph Hellwig 	 * a separate refcount.
365ab9e00ccSChristoph Hellwig 	 */
366ab9e00ccSChristoph Hellwig 	struct kref		ref;
367ab9e00ccSChristoph Hellwig 	struct list_head	entry;
368ab9e00ccSChristoph Hellwig 	struct mutex		lock;
369ab9e00ccSChristoph Hellwig 	struct list_head	ctrls;
370ed754e5dSChristoph Hellwig 	struct list_head	nsheads;
371ab9e00ccSChristoph Hellwig 	char			subnqn[NVMF_NQN_SIZE];
372ab9e00ccSChristoph Hellwig 	char			serial[20];
373ab9e00ccSChristoph Hellwig 	char			model[40];
374ab9e00ccSChristoph Hellwig 	char			firmware_rev[8];
375ab9e00ccSChristoph Hellwig 	u8			cmic;
376954ae166SHannes Reinecke 	enum nvme_subsys_type	subtype;
377ab9e00ccSChristoph Hellwig 	u16			vendor_id;
37881adb863SBart Van Assche 	u16			awupf;	/* 0's based awupf value. */
379ed754e5dSChristoph Hellwig 	struct ida		ns_ida;
38075c10e73SHannes Reinecke #ifdef CONFIG_NVME_MULTIPATH
38175c10e73SHannes Reinecke 	enum nvme_iopolicy	iopolicy;
38275c10e73SHannes Reinecke #endif
383ab9e00ccSChristoph Hellwig };
384ab9e00ccSChristoph Hellwig 
385002fab04SChristoph Hellwig /*
386002fab04SChristoph Hellwig  * Container structure for uniqueue namespace identifiers.
387002fab04SChristoph Hellwig  */
388002fab04SChristoph Hellwig struct nvme_ns_ids {
389002fab04SChristoph Hellwig 	u8	eui64[8];
390002fab04SChristoph Hellwig 	u8	nguid[16];
391002fab04SChristoph Hellwig 	uuid_t	uuid;
39271010c30SNiklas Cassel 	u8	csi;
393002fab04SChristoph Hellwig };
394002fab04SChristoph Hellwig 
395ed754e5dSChristoph Hellwig /*
396ed754e5dSChristoph Hellwig  * Anchor structure for namespaces.  There is one for each namespace in a
397ed754e5dSChristoph Hellwig  * NVMe subsystem that any of our controllers can see, and the namespace
398ed754e5dSChristoph Hellwig  * structure for each controller is chained of it.  For private namespaces
399ed754e5dSChristoph Hellwig  * there is a 1:1 relation to our namespace structures, that is ->list
400ed754e5dSChristoph Hellwig  * only ever has a single entry for private namespaces.
401ed754e5dSChristoph Hellwig  */
402ed754e5dSChristoph Hellwig struct nvme_ns_head {
403ed754e5dSChristoph Hellwig 	struct list_head	list;
404ed754e5dSChristoph Hellwig 	struct srcu_struct      srcu;
405ed754e5dSChristoph Hellwig 	struct nvme_subsystem	*subsys;
406ed754e5dSChristoph Hellwig 	unsigned		ns_id;
407ed754e5dSChristoph Hellwig 	struct nvme_ns_ids	ids;
408ed754e5dSChristoph Hellwig 	struct list_head	entry;
409ed754e5dSChristoph Hellwig 	struct kref		ref;
4100c284db7SKeith Busch 	bool			shared;
411ed754e5dSChristoph Hellwig 	int			instance;
412be93e87eSKeith Busch 	struct nvme_effects_log *effects;
4132637baedSMinwoo Im 
4142637baedSMinwoo Im 	struct cdev		cdev;
4152637baedSMinwoo Im 	struct device		cdev_device;
4162637baedSMinwoo Im 
417f3334447SChristoph Hellwig 	struct gendisk		*disk;
41830897388SMinwoo Im #ifdef CONFIG_NVME_MULTIPATH
419f3334447SChristoph Hellwig 	struct bio_list		requeue_list;
420f3334447SChristoph Hellwig 	spinlock_t		requeue_lock;
421f3334447SChristoph Hellwig 	struct work_struct	requeue_work;
422f3334447SChristoph Hellwig 	struct mutex		lock;
423d8a22f85SAnton Eidelman 	unsigned long		flags;
424d8a22f85SAnton Eidelman #define NVME_NSHEAD_DISK_LIVE	0
425f3334447SChristoph Hellwig 	struct nvme_ns __rcu	*current_path[];
426f3334447SChristoph Hellwig #endif
427ed754e5dSChristoph Hellwig };
428ed754e5dSChristoph Hellwig 
42930897388SMinwoo Im static inline bool nvme_ns_head_multipath(struct nvme_ns_head *head)
43030897388SMinwoo Im {
43130897388SMinwoo Im 	return IS_ENABLED(CONFIG_NVME_MULTIPATH) && head->disk;
43230897388SMinwoo Im }
43330897388SMinwoo Im 
434ffc89b1dSMax Gurtovoy enum nvme_ns_features {
435ffc89b1dSMax Gurtovoy 	NVME_NS_EXT_LBAS = 1 << 0, /* support extended LBA format */
436b29f8485SMax Gurtovoy 	NVME_NS_METADATA_SUPPORTED = 1 << 1, /* support getting generated md */
437ffc89b1dSMax Gurtovoy };
438ffc89b1dSMax Gurtovoy 
43957dacad5SJay Sternberg struct nvme_ns {
44057dacad5SJay Sternberg 	struct list_head list;
44157dacad5SJay Sternberg 
4421c63dc66SChristoph Hellwig 	struct nvme_ctrl *ctrl;
44357dacad5SJay Sternberg 	struct request_queue *queue;
44457dacad5SJay Sternberg 	struct gendisk *disk;
4450d0b660fSChristoph Hellwig #ifdef CONFIG_NVME_MULTIPATH
4460d0b660fSChristoph Hellwig 	enum nvme_ana_state ana_state;
4470d0b660fSChristoph Hellwig 	u32 ana_grpid;
4480d0b660fSChristoph Hellwig #endif
449ed754e5dSChristoph Hellwig 	struct list_head siblings;
45057dacad5SJay Sternberg 	struct kref kref;
451ed754e5dSChristoph Hellwig 	struct nvme_ns_head *head;
45257dacad5SJay Sternberg 
45357dacad5SJay Sternberg 	int lba_shift;
45457dacad5SJay Sternberg 	u16 ms;
455f5d11840SJens Axboe 	u16 sgs;
456f5d11840SJens Axboe 	u32 sws;
45757dacad5SJay Sternberg 	u8 pi_type;
458240e6ee2SKeith Busch #ifdef CONFIG_BLK_DEV_ZONED
459240e6ee2SKeith Busch 	u64 zsze;
460240e6ee2SKeith Busch #endif
461ffc89b1dSMax Gurtovoy 	unsigned long features;
462646017a6SKeith Busch 	unsigned long flags;
463646017a6SKeith Busch #define NVME_NS_REMOVING	0
46469d9a99cSKeith Busch #define NVME_NS_DEAD     	1
4650d0b660fSChristoph Hellwig #define NVME_NS_ANA_PENDING	2
4662f4c9ba2SJavier González #define NVME_NS_FORCE_RO	3
467e7d65803SHannes Reinecke #define NVME_NS_READY		4
4689e6a6b12SMing Lei #define NVME_NS_STOPPED		5
469b9e03857SThomas Tai 
4702637baedSMinwoo Im 	struct cdev		cdev;
4712637baedSMinwoo Im 	struct device		cdev_device;
4722637baedSMinwoo Im 
473b9e03857SThomas Tai 	struct nvme_fault_inject fault_inject;
474b9e03857SThomas Tai 
47557dacad5SJay Sternberg };
47657dacad5SJay Sternberg 
4774d2ce688SJames Smart /* NVMe ns supports metadata actions by the controller (generate/strip) */
4784d2ce688SJames Smart static inline bool nvme_ns_has_pi(struct nvme_ns *ns)
4794d2ce688SJames Smart {
4804d2ce688SJames Smart 	return ns->pi_type && ns->ms == sizeof(struct t10_pi_tuple);
4814d2ce688SJames Smart }
4824d2ce688SJames Smart 
4831c63dc66SChristoph Hellwig struct nvme_ctrl_ops {
4841a353d85SMing Lin 	const char *name;
485e439bb12SSagi Grimberg 	struct module *module;
486d3d5b87dSChristoph Hellwig 	unsigned int flags;
487d3d5b87dSChristoph Hellwig #define NVME_F_FABRICS			(1 << 0)
488c81bfba9SChristoph Hellwig #define NVME_F_METADATA_SUPPORTED	(1 << 1)
489e0596ab2SLogan Gunthorpe #define NVME_F_PCI_P2PDMA		(1 << 2)
4901c63dc66SChristoph Hellwig 	int (*reg_read32)(struct nvme_ctrl *ctrl, u32 off, u32 *val);
4915fd4ce1bSChristoph Hellwig 	int (*reg_write32)(struct nvme_ctrl *ctrl, u32 off, u32 val);
4927fd8930fSChristoph Hellwig 	int (*reg_read64)(struct nvme_ctrl *ctrl, u32 off, u64 *val);
4931673f1f0SChristoph Hellwig 	void (*free_ctrl)(struct nvme_ctrl *ctrl);
494ad22c355SKeith Busch 	void (*submit_async_event)(struct nvme_ctrl *ctrl);
495c5017e85SChristoph Hellwig 	void (*delete_ctrl)(struct nvme_ctrl *ctrl);
4961a353d85SMing Lin 	int (*get_address)(struct nvme_ctrl *ctrl, char *buf, int size);
49757dacad5SJay Sternberg };
49857dacad5SJay Sternberg 
499e7006de6SSagi Grimberg /*
500e7006de6SSagi Grimberg  * nvme command_id is constructed as such:
501e7006de6SSagi Grimberg  * | xxxx | xxxxxxxxxxxx |
502e7006de6SSagi Grimberg  *   gen    request tag
503e7006de6SSagi Grimberg  */
504e7006de6SSagi Grimberg #define nvme_genctr_mask(gen)			(gen & 0xf)
505e7006de6SSagi Grimberg #define nvme_cid_install_genctr(gen)		(nvme_genctr_mask(gen) << 12)
506e7006de6SSagi Grimberg #define nvme_genctr_from_cid(cid)		((cid & 0xf000) >> 12)
507e7006de6SSagi Grimberg #define nvme_tag_from_cid(cid)			(cid & 0xfff)
508e7006de6SSagi Grimberg 
509e7006de6SSagi Grimberg static inline u16 nvme_cid(struct request *rq)
510e7006de6SSagi Grimberg {
511e7006de6SSagi Grimberg 	return nvme_cid_install_genctr(nvme_req(rq)->genctr) | rq->tag;
512e7006de6SSagi Grimberg }
513e7006de6SSagi Grimberg 
514e7006de6SSagi Grimberg static inline struct request *nvme_find_rq(struct blk_mq_tags *tags,
515e7006de6SSagi Grimberg 		u16 command_id)
516e7006de6SSagi Grimberg {
517e7006de6SSagi Grimberg 	u8 genctr = nvme_genctr_from_cid(command_id);
518e7006de6SSagi Grimberg 	u16 tag = nvme_tag_from_cid(command_id);
519e7006de6SSagi Grimberg 	struct request *rq;
520e7006de6SSagi Grimberg 
521e7006de6SSagi Grimberg 	rq = blk_mq_tag_to_rq(tags, tag);
522e7006de6SSagi Grimberg 	if (unlikely(!rq)) {
523e7006de6SSagi Grimberg 		pr_err("could not locate request for tag %#x\n",
524e7006de6SSagi Grimberg 			tag);
525e7006de6SSagi Grimberg 		return NULL;
526e7006de6SSagi Grimberg 	}
527e7006de6SSagi Grimberg 	if (unlikely(nvme_genctr_mask(nvme_req(rq)->genctr) != genctr)) {
528e7006de6SSagi Grimberg 		dev_err(nvme_req(rq)->ctrl->device,
529e7006de6SSagi Grimberg 			"request %#x genctr mismatch (got %#x expected %#x)\n",
530e7006de6SSagi Grimberg 			tag, genctr, nvme_genctr_mask(nvme_req(rq)->genctr));
531e7006de6SSagi Grimberg 		return NULL;
532e7006de6SSagi Grimberg 	}
533e7006de6SSagi Grimberg 	return rq;
534e7006de6SSagi Grimberg }
535e7006de6SSagi Grimberg 
536e7006de6SSagi Grimberg static inline struct request *nvme_cid_to_rq(struct blk_mq_tags *tags,
537e7006de6SSagi Grimberg                 u16 command_id)
538e7006de6SSagi Grimberg {
539e7006de6SSagi Grimberg 	return blk_mq_tag_to_rq(tags, nvme_tag_from_cid(command_id));
540e7006de6SSagi Grimberg }
541e7006de6SSagi Grimberg 
542b9e03857SThomas Tai #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
543a3646451SAkinobu Mita void nvme_fault_inject_init(struct nvme_fault_inject *fault_inj,
544a3646451SAkinobu Mita 			    const char *dev_name);
545a3646451SAkinobu Mita void nvme_fault_inject_fini(struct nvme_fault_inject *fault_inject);
546b9e03857SThomas Tai void nvme_should_fail(struct request *req);
547b9e03857SThomas Tai #else
548a3646451SAkinobu Mita static inline void nvme_fault_inject_init(struct nvme_fault_inject *fault_inj,
549a3646451SAkinobu Mita 					  const char *dev_name)
550a3646451SAkinobu Mita {
551a3646451SAkinobu Mita }
552a3646451SAkinobu Mita static inline void nvme_fault_inject_fini(struct nvme_fault_inject *fault_inj)
553a3646451SAkinobu Mita {
554a3646451SAkinobu Mita }
555b9e03857SThomas Tai static inline void nvme_should_fail(struct request *req) {}
556b9e03857SThomas Tai #endif
557b9e03857SThomas Tai 
558f3ca80fcSChristoph Hellwig static inline int nvme_reset_subsystem(struct nvme_ctrl *ctrl)
559f3ca80fcSChristoph Hellwig {
560f3ca80fcSChristoph Hellwig 	if (!ctrl->subsystem)
561f3ca80fcSChristoph Hellwig 		return -ENOTTY;
562f3ca80fcSChristoph Hellwig 	return ctrl->ops->reg_write32(ctrl, NVME_REG_NSSR, 0x4E564D65);
563f3ca80fcSChristoph Hellwig }
564f3ca80fcSChristoph Hellwig 
565314d48ddSDamien Le Moal /*
566314d48ddSDamien Le Moal  * Convert a 512B sector number to a device logical block number.
567314d48ddSDamien Le Moal  */
568314d48ddSDamien Le Moal static inline u64 nvme_sect_to_lba(struct nvme_ns *ns, sector_t sector)
56957dacad5SJay Sternberg {
570314d48ddSDamien Le Moal 	return sector >> (ns->lba_shift - SECTOR_SHIFT);
57157dacad5SJay Sternberg }
57257dacad5SJay Sternberg 
573e08f2ae8SDamien Le Moal /*
574e08f2ae8SDamien Le Moal  * Convert a device logical block number to a 512B sector number.
575e08f2ae8SDamien Le Moal  */
576e08f2ae8SDamien Le Moal static inline sector_t nvme_lba_to_sect(struct nvme_ns *ns, u64 lba)
577e08f2ae8SDamien Le Moal {
578e08f2ae8SDamien Le Moal 	return lba << (ns->lba_shift - SECTOR_SHIFT);
57957dacad5SJay Sternberg }
58057dacad5SJay Sternberg 
58171fb90ebSKeith Busch /*
58271fb90ebSKeith Busch  * Convert byte length to nvme's 0-based num dwords
58371fb90ebSKeith Busch  */
58471fb90ebSKeith Busch static inline u32 nvme_bytes_to_numd(size_t len)
58571fb90ebSKeith Busch {
58671fb90ebSKeith Busch 	return (len >> 2) - 1;
58771fb90ebSKeith Busch }
58871fb90ebSKeith Busch 
5895ddaabe8SChristoph Hellwig static inline bool nvme_is_ana_error(u16 status)
5905ddaabe8SChristoph Hellwig {
5915ddaabe8SChristoph Hellwig 	switch (status & 0x7ff) {
5925ddaabe8SChristoph Hellwig 	case NVME_SC_ANA_TRANSITION:
5935ddaabe8SChristoph Hellwig 	case NVME_SC_ANA_INACCESSIBLE:
5945ddaabe8SChristoph Hellwig 	case NVME_SC_ANA_PERSISTENT_LOSS:
5955ddaabe8SChristoph Hellwig 		return true;
5965ddaabe8SChristoph Hellwig 	default:
5975ddaabe8SChristoph Hellwig 		return false;
5985ddaabe8SChristoph Hellwig 	}
5995ddaabe8SChristoph Hellwig }
6005ddaabe8SChristoph Hellwig 
6015ddaabe8SChristoph Hellwig static inline bool nvme_is_path_error(u16 status)
6025ddaabe8SChristoph Hellwig {
6031e41f3bdSChristoph Hellwig 	/* check for a status code type of 'path related status' */
6041e41f3bdSChristoph Hellwig 	return (status & 0x700) == 0x300;
6055ddaabe8SChristoph Hellwig }
6065ddaabe8SChristoph Hellwig 
6072eb81a33SChristoph Hellwig /*
6082eb81a33SChristoph Hellwig  * Fill in the status and result information from the CQE, and then figure out
6092eb81a33SChristoph Hellwig  * if blk-mq will need to use IPI magic to complete the request, and if yes do
6102eb81a33SChristoph Hellwig  * so.  If not let the caller complete the request without an indirect function
6112eb81a33SChristoph Hellwig  * call.
6122eb81a33SChristoph Hellwig  */
6132eb81a33SChristoph Hellwig static inline bool nvme_try_complete_req(struct request *req, __le16 status,
61427fa9bc5SChristoph Hellwig 		union nvme_result result)
61515a190f7SChristoph Hellwig {
61627fa9bc5SChristoph Hellwig 	struct nvme_request *rq = nvme_req(req);
617e4fdb2b1SKeith Busch 	struct nvme_ctrl *ctrl = rq->ctrl;
618e4fdb2b1SKeith Busch 
619e4fdb2b1SKeith Busch 	if (!(ctrl->quirks & NVME_QUIRK_SKIP_CID_GEN))
620e4fdb2b1SKeith Busch 		rq->genctr++;
62127fa9bc5SChristoph Hellwig 
62227fa9bc5SChristoph Hellwig 	rq->status = le16_to_cpu(status) >> 1;
62327fa9bc5SChristoph Hellwig 	rq->result = result;
624b9e03857SThomas Tai 	/* inject error when permitted by fault injection framework */
625b9e03857SThomas Tai 	nvme_should_fail(req);
626ff029451SChristoph Hellwig 	if (unlikely(blk_should_fake_timeout(req->q)))
627ff029451SChristoph Hellwig 		return true;
628ff029451SChristoph Hellwig 	return blk_mq_complete_request_remote(req);
62915a190f7SChristoph Hellwig }
63015a190f7SChristoph Hellwig 
631d22524a4SChristoph Hellwig static inline void nvme_get_ctrl(struct nvme_ctrl *ctrl)
632d22524a4SChristoph Hellwig {
633d22524a4SChristoph Hellwig 	get_device(ctrl->device);
634d22524a4SChristoph Hellwig }
635d22524a4SChristoph Hellwig 
636d22524a4SChristoph Hellwig static inline void nvme_put_ctrl(struct nvme_ctrl *ctrl)
637d22524a4SChristoph Hellwig {
638d22524a4SChristoph Hellwig 	put_device(ctrl->device);
639d22524a4SChristoph Hellwig }
640d22524a4SChristoph Hellwig 
64158a8df67SIsrael Rukshin static inline bool nvme_is_aen_req(u16 qid, __u16 command_id)
64258a8df67SIsrael Rukshin {
643e7006de6SSagi Grimberg 	return !qid &&
644e7006de6SSagi Grimberg 		nvme_tag_from_cid(command_id) >= NVME_AQ_BLK_MQ_DEPTH;
64558a8df67SIsrael Rukshin }
64658a8df67SIsrael Rukshin 
64777f02a7aSChristoph Hellwig void nvme_complete_rq(struct request *req);
648c234a653SJens Axboe void nvme_complete_batch_req(struct request *req);
649c234a653SJens Axboe 
650c234a653SJens Axboe static __always_inline void nvme_complete_batch(struct io_comp_batch *iob,
651c234a653SJens Axboe 						void (*fn)(struct request *rq))
652c234a653SJens Axboe {
653c234a653SJens Axboe 	struct request *req;
654c234a653SJens Axboe 
655c234a653SJens Axboe 	rq_list_for_each(&iob->req_list, req) {
656c234a653SJens Axboe 		fn(req);
657c234a653SJens Axboe 		nvme_complete_batch_req(req);
658c234a653SJens Axboe 	}
659c234a653SJens Axboe 	blk_mq_end_request_batch(iob);
660c234a653SJens Axboe }
661c234a653SJens Axboe 
662dda3248eSChao Leng blk_status_t nvme_host_path_error(struct request *req);
6637baa8572SJens Axboe bool nvme_cancel_request(struct request *req, void *data, bool reserved);
66425479069SChao Leng void nvme_cancel_tagset(struct nvme_ctrl *ctrl);
66525479069SChao Leng void nvme_cancel_admin_tagset(struct nvme_ctrl *ctrl);
666bb8d261eSChristoph Hellwig bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
667bb8d261eSChristoph Hellwig 		enum nvme_ctrl_state new_state);
668c1ac9a4bSKeith Busch bool nvme_wait_reset(struct nvme_ctrl *ctrl);
669b5b05048SSagi Grimberg int nvme_disable_ctrl(struct nvme_ctrl *ctrl);
670c0f2f45bSSagi Grimberg int nvme_enable_ctrl(struct nvme_ctrl *ctrl);
6715fd4ce1bSChristoph Hellwig int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl);
672f3ca80fcSChristoph Hellwig int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
673f3ca80fcSChristoph Hellwig 		const struct nvme_ctrl_ops *ops, unsigned long quirks);
67453029b04SKeith Busch void nvme_uninit_ctrl(struct nvme_ctrl *ctrl);
675d09f2b45SSagi Grimberg void nvme_start_ctrl(struct nvme_ctrl *ctrl);
676d09f2b45SSagi Grimberg void nvme_stop_ctrl(struct nvme_ctrl *ctrl);
677f21c4769SChaitanya Kulkarni int nvme_init_ctrl_finish(struct nvme_ctrl *ctrl);
6785bae7f73SChristoph Hellwig 
6795bae7f73SChristoph Hellwig void nvme_remove_namespaces(struct nvme_ctrl *ctrl);
6801673f1f0SChristoph Hellwig 
6814f1244c8SChristoph Hellwig int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
6824f1244c8SChristoph Hellwig 		bool send);
683a98e58e5SScott Bauer 
6847bf58533SChristoph Hellwig void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
685287a63ebSChristoph Hellwig 		volatile union nvme_result *res);
686f866fc42SChristoph Hellwig 
68725646264SKeith Busch void nvme_stop_queues(struct nvme_ctrl *ctrl);
68825646264SKeith Busch void nvme_start_queues(struct nvme_ctrl *ctrl);
689a277654bSMing Lei void nvme_stop_admin_queue(struct nvme_ctrl *ctrl);
690a277654bSMing Lei void nvme_start_admin_queue(struct nvme_ctrl *ctrl);
69169d9a99cSKeith Busch void nvme_kill_queues(struct nvme_ctrl *ctrl);
692d6135c3aSKeith Busch void nvme_sync_queues(struct nvme_ctrl *ctrl);
69304800fbfSChao Leng void nvme_sync_io_queues(struct nvme_ctrl *ctrl);
694302ad8ccSKeith Busch void nvme_unfreeze(struct nvme_ctrl *ctrl);
695302ad8ccSKeith Busch void nvme_wait_freeze(struct nvme_ctrl *ctrl);
6967cf0d7c0SSagi Grimberg int nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout);
697302ad8ccSKeith Busch void nvme_start_freeze(struct nvme_ctrl *ctrl);
698363c9aacSSagi Grimberg 
699eb71f435SChristoph Hellwig #define NVME_QID_ANY -1
7004160982eSChristoph Hellwig struct request *nvme_alloc_request(struct request_queue *q,
70139dfe844SChaitanya Kulkarni 		struct nvme_command *cmd, blk_mq_req_flags_t flags);
702f7f1fc36SMax Gurtovoy void nvme_cleanup_cmd(struct request *req);
703f4b9e6c9SKeith Busch blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req);
704a9715744STao Chiu blk_status_t nvme_fail_nonready_command(struct nvme_ctrl *ctrl,
705a9715744STao Chiu 		struct request *req);
706a9715744STao Chiu bool __nvme_check_ready(struct nvme_ctrl *ctrl, struct request *rq,
707a9715744STao Chiu 		bool queue_live);
708a9715744STao Chiu 
709a9715744STao Chiu static inline bool nvme_check_ready(struct nvme_ctrl *ctrl, struct request *rq,
710a9715744STao Chiu 		bool queue_live)
711a9715744STao Chiu {
712a9715744STao Chiu 	if (likely(ctrl->state == NVME_CTRL_LIVE))
713a9715744STao Chiu 		return true;
714a9715744STao Chiu 	if (ctrl->ops->flags & NVME_F_FABRICS &&
715a9715744STao Chiu 	    ctrl->state == NVME_CTRL_DELETING)
7168b77fa6fSRuozhu Li 		return queue_live;
717a9715744STao Chiu 	return __nvme_check_ready(ctrl, rq, queue_live);
718a9715744STao Chiu }
71957dacad5SJay Sternberg int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
72057dacad5SJay Sternberg 		void *buf, unsigned bufflen);
72157dacad5SJay Sternberg int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
722d49187e9SChristoph Hellwig 		union nvme_result *result, void *buffer, unsigned bufflen,
7239a95e4efSBart Van Assche 		unsigned timeout, int qid, int at_head,
724be42a33bSKeith Busch 		blk_mq_req_flags_t flags);
7251a87ee65SKeith Busch int nvme_set_features(struct nvme_ctrl *dev, unsigned int fid,
7261a87ee65SKeith Busch 		      unsigned int dword11, void *buffer, size_t buflen,
7271a87ee65SKeith Busch 		      u32 *result);
7281a87ee65SKeith Busch int nvme_get_features(struct nvme_ctrl *dev, unsigned int fid,
7291a87ee65SKeith Busch 		      unsigned int dword11, void *buffer, size_t buflen,
7301a87ee65SKeith Busch 		      u32 *result);
7319a0be7abSChristoph Hellwig int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count);
732038bd4cbSSagi Grimberg void nvme_stop_keep_alive(struct nvme_ctrl *ctrl);
733d86c4d8eSChristoph Hellwig int nvme_reset_ctrl(struct nvme_ctrl *ctrl);
7342405252aSChristoph Hellwig int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl);
735c1ac9a4bSKeith Busch int nvme_try_sched_reset(struct nvme_ctrl *ctrl);
736c5017e85SChristoph Hellwig int nvme_delete_ctrl(struct nvme_ctrl *ctrl);
7372405252aSChristoph Hellwig void nvme_queue_scan(struct nvme_ctrl *ctrl);
738be93e87eSKeith Busch int nvme_get_log(struct nvme_ctrl *ctrl, u32 nsid, u8 log_page, u8 lsp, u8 csi,
7390e98719bSChristoph Hellwig 		void *log, size_t size, u64 offset);
7401496bd49SChristoph Hellwig bool nvme_tryget_ns_head(struct nvme_ns_head *head);
7411496bd49SChristoph Hellwig void nvme_put_ns_head(struct nvme_ns_head *head);
7422637baedSMinwoo Im int nvme_cdev_add(struct cdev *cdev, struct device *cdev_device,
7432637baedSMinwoo Im 		const struct file_operations *fops, struct module *owner);
7442637baedSMinwoo Im void nvme_cdev_del(struct cdev *cdev, struct device *cdev_device);
7452405252aSChristoph Hellwig int nvme_ioctl(struct block_device *bdev, fmode_t mode,
7462405252aSChristoph Hellwig 		unsigned int cmd, unsigned long arg);
7472637baedSMinwoo Im long nvme_ns_chr_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
7482405252aSChristoph Hellwig int nvme_ns_head_ioctl(struct block_device *bdev, fmode_t mode,
7492405252aSChristoph Hellwig 		unsigned int cmd, unsigned long arg);
7502637baedSMinwoo Im long nvme_ns_head_chr_ioctl(struct file *file, unsigned int cmd,
7512637baedSMinwoo Im 		unsigned long arg);
7522405252aSChristoph Hellwig long nvme_dev_ioctl(struct file *file, unsigned int cmd,
7532405252aSChristoph Hellwig 		unsigned long arg);
7541496bd49SChristoph Hellwig int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo);
755d558fb51SMatias Bjørling 
75633b14f67SHannes Reinecke extern const struct attribute_group *nvme_ns_id_attr_groups[];
7571496bd49SChristoph Hellwig extern const struct pr_ops nvme_pr_ops;
75832acab31SChristoph Hellwig extern const struct block_device_operations nvme_ns_head_ops;
75932acab31SChristoph Hellwig 
760f1cf35e1SChristoph Hellwig struct nvme_ns *nvme_find_path(struct nvme_ns_head *head);
76132acab31SChristoph Hellwig #ifdef CONFIG_NVME_MULTIPATH
76266b20ac0SMarta Rybczynska static inline bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl)
76366b20ac0SMarta Rybczynska {
76466b20ac0SMarta Rybczynska 	return ctrl->ana_log_buf != NULL;
76566b20ac0SMarta Rybczynska }
76666b20ac0SMarta Rybczynska 
767b9156daeSSagi Grimberg void nvme_mpath_unfreeze(struct nvme_subsystem *subsys);
768b9156daeSSagi Grimberg void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys);
769b9156daeSSagi Grimberg void nvme_mpath_start_freeze(struct nvme_subsystem *subsys);
770e3d34794SHannes Reinecke void nvme_mpath_default_iopolicy(struct nvme_subsystem *subsys);
7719953ab0cSChristoph Hellwig bool nvme_mpath_set_disk_name(struct nvme_ns *ns, char *disk_name, int *flags);
7725ddaabe8SChristoph Hellwig void nvme_failover_req(struct request *req);
77332acab31SChristoph Hellwig void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl);
77432acab31SChristoph Hellwig int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl,struct nvme_ns_head *head);
7750d0b660fSChristoph Hellwig void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id);
77632acab31SChristoph Hellwig void nvme_mpath_remove_disk(struct nvme_ns_head *head);
7775e1f6899SChristoph Hellwig int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id);
7785e1f6899SChristoph Hellwig void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl);
7790d0b660fSChristoph Hellwig void nvme_mpath_uninit(struct nvme_ctrl *ctrl);
7800d0b660fSChristoph Hellwig void nvme_mpath_stop(struct nvme_ctrl *ctrl);
7810157ec8dSSagi Grimberg bool nvme_mpath_clear_current_path(struct nvme_ns *ns);
782e7d65803SHannes Reinecke void nvme_mpath_revalidate_paths(struct nvme_ns *ns);
7830157ec8dSSagi Grimberg void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl);
7845396fdacSHannes Reinecke void nvme_mpath_shutdown_disk(struct nvme_ns_head *head);
785479a322fSSagi Grimberg 
7862b59787aSMax Gurtovoy static inline void nvme_trace_bio_complete(struct request *req)
78735fe0d12SHannes Reinecke {
78835fe0d12SHannes Reinecke 	struct nvme_ns *ns = req->q->queuedata;
78935fe0d12SHannes Reinecke 
79035fe0d12SHannes Reinecke 	if (req->cmd_flags & REQ_NVME_MPATH)
791d24de76aSChristoph Hellwig 		trace_block_bio_complete(ns->head->disk->queue, req->bio);
79235fe0d12SHannes Reinecke }
79335fe0d12SHannes Reinecke 
7940d0b660fSChristoph Hellwig extern struct device_attribute dev_attr_ana_grpid;
7950d0b660fSChristoph Hellwig extern struct device_attribute dev_attr_ana_state;
79675c10e73SHannes Reinecke extern struct device_attribute subsys_attr_iopolicy;
7970d0b660fSChristoph Hellwig 
79832acab31SChristoph Hellwig #else
7990d0b660fSChristoph Hellwig static inline bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl)
8000d0b660fSChristoph Hellwig {
8010d0b660fSChristoph Hellwig 	return false;
8020d0b660fSChristoph Hellwig }
8039953ab0cSChristoph Hellwig static inline bool nvme_mpath_set_disk_name(struct nvme_ns *ns, char *disk_name,
8049953ab0cSChristoph Hellwig 		int *flags)
805a785dbccSKeith Busch {
8069953ab0cSChristoph Hellwig 	return false;
807a785dbccSKeith Busch }
8085ddaabe8SChristoph Hellwig static inline void nvme_failover_req(struct request *req)
80932acab31SChristoph Hellwig {
81032acab31SChristoph Hellwig }
81132acab31SChristoph Hellwig static inline void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
81232acab31SChristoph Hellwig {
81332acab31SChristoph Hellwig }
81432acab31SChristoph Hellwig static inline int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl,
81532acab31SChristoph Hellwig 		struct nvme_ns_head *head)
81632acab31SChristoph Hellwig {
81732acab31SChristoph Hellwig 	return 0;
81832acab31SChristoph Hellwig }
8190d0b660fSChristoph Hellwig static inline void nvme_mpath_add_disk(struct nvme_ns *ns,
8200d0b660fSChristoph Hellwig 		struct nvme_id_ns *id)
82132acab31SChristoph Hellwig {
82232acab31SChristoph Hellwig }
82332acab31SChristoph Hellwig static inline void nvme_mpath_remove_disk(struct nvme_ns_head *head)
82432acab31SChristoph Hellwig {
82532acab31SChristoph Hellwig }
8260157ec8dSSagi Grimberg static inline bool nvme_mpath_clear_current_path(struct nvme_ns *ns)
8270157ec8dSSagi Grimberg {
8280157ec8dSSagi Grimberg 	return false;
8290157ec8dSSagi Grimberg }
830e7d65803SHannes Reinecke static inline void nvme_mpath_revalidate_paths(struct nvme_ns *ns)
831e7d65803SHannes Reinecke {
832e7d65803SHannes Reinecke }
8330157ec8dSSagi Grimberg static inline void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
83432acab31SChristoph Hellwig {
83532acab31SChristoph Hellwig }
8365396fdacSHannes Reinecke static inline void nvme_mpath_shutdown_disk(struct nvme_ns_head *head)
837479a322fSSagi Grimberg {
838479a322fSSagi Grimberg }
8392b59787aSMax Gurtovoy static inline void nvme_trace_bio_complete(struct request *req)
84035fe0d12SHannes Reinecke {
84135fe0d12SHannes Reinecke }
8425e1f6899SChristoph Hellwig static inline void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl)
8435e1f6899SChristoph Hellwig {
8445e1f6899SChristoph Hellwig }
8455e1f6899SChristoph Hellwig static inline int nvme_mpath_init_identify(struct nvme_ctrl *ctrl,
8460d0b660fSChristoph Hellwig 		struct nvme_id_ctrl *id)
8470d0b660fSChristoph Hellwig {
8482bd64307SKanchan Joshi 	if (ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA)
84914a1336eSChristoph Hellwig 		dev_warn(ctrl->device,
85014a1336eSChristoph Hellwig "Please enable CONFIG_NVME_MULTIPATH for full support of multi-port devices.\n");
8510d0b660fSChristoph Hellwig 	return 0;
8520d0b660fSChristoph Hellwig }
8530d0b660fSChristoph Hellwig static inline void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
8540d0b660fSChristoph Hellwig {
8550d0b660fSChristoph Hellwig }
8560d0b660fSChristoph Hellwig static inline void nvme_mpath_stop(struct nvme_ctrl *ctrl)
8570d0b660fSChristoph Hellwig {
8580d0b660fSChristoph Hellwig }
859b9156daeSSagi Grimberg static inline void nvme_mpath_unfreeze(struct nvme_subsystem *subsys)
860b9156daeSSagi Grimberg {
861b9156daeSSagi Grimberg }
862b9156daeSSagi Grimberg static inline void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys)
863b9156daeSSagi Grimberg {
864b9156daeSSagi Grimberg }
865b9156daeSSagi Grimberg static inline void nvme_mpath_start_freeze(struct nvme_subsystem *subsys)
866b9156daeSSagi Grimberg {
867b9156daeSSagi Grimberg }
868e3d34794SHannes Reinecke static inline void nvme_mpath_default_iopolicy(struct nvme_subsystem *subsys)
869e3d34794SHannes Reinecke {
870e3d34794SHannes Reinecke }
87132acab31SChristoph Hellwig #endif /* CONFIG_NVME_MULTIPATH */
87232acab31SChristoph Hellwig 
8737fad20ddSChristoph Hellwig int nvme_revalidate_zones(struct nvme_ns *ns);
8748b4fb0f9SChristoph Hellwig int nvme_ns_report_zones(struct nvme_ns *ns, sector_t sector,
8758b4fb0f9SChristoph Hellwig 		unsigned int nr_zones, report_zones_cb cb, void *data);
876240e6ee2SKeith Busch #ifdef CONFIG_BLK_DEV_ZONED
877d525c3c0SChristoph Hellwig int nvme_update_zone_info(struct nvme_ns *ns, unsigned lbaf);
878240e6ee2SKeith Busch blk_status_t nvme_setup_zone_mgmt_send(struct nvme_ns *ns, struct request *req,
879240e6ee2SKeith Busch 				       struct nvme_command *cmnd,
880240e6ee2SKeith Busch 				       enum nvme_zone_mgmt_action action);
881240e6ee2SKeith Busch #else
882240e6ee2SKeith Busch static inline blk_status_t nvme_setup_zone_mgmt_send(struct nvme_ns *ns,
883240e6ee2SKeith Busch 		struct request *req, struct nvme_command *cmnd,
884240e6ee2SKeith Busch 		enum nvme_zone_mgmt_action action)
885240e6ee2SKeith Busch {
886240e6ee2SKeith Busch 	return BLK_STS_NOTSUPP;
887240e6ee2SKeith Busch }
888240e6ee2SKeith Busch 
889d525c3c0SChristoph Hellwig static inline int nvme_update_zone_info(struct nvme_ns *ns, unsigned lbaf)
890240e6ee2SKeith Busch {
891240e6ee2SKeith Busch 	dev_warn(ns->ctrl->device,
892240e6ee2SKeith Busch 		 "Please enable CONFIG_BLK_DEV_ZONED to support ZNS devices\n");
893240e6ee2SKeith Busch 	return -EPROTONOSUPPORT;
894240e6ee2SKeith Busch }
895240e6ee2SKeith Busch #endif
896240e6ee2SKeith Busch 
89772e8b5cdSChaitanya Kulkarni static inline int nvme_ctrl_init_connect_q(struct nvme_ctrl *ctrl)
89872e8b5cdSChaitanya Kulkarni {
89972e8b5cdSChaitanya Kulkarni 	ctrl->connect_q = blk_mq_init_queue(ctrl->tagset);
90072e8b5cdSChaitanya Kulkarni 	if (IS_ERR(ctrl->connect_q))
90172e8b5cdSChaitanya Kulkarni 		return PTR_ERR(ctrl->connect_q);
90272e8b5cdSChaitanya Kulkarni 	return 0;
90372e8b5cdSChaitanya Kulkarni }
90472e8b5cdSChaitanya Kulkarni 
90540267efdSSimon A. F. Lund static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev)
90640267efdSSimon A. F. Lund {
90740267efdSSimon A. F. Lund 	return dev_to_disk(dev)->private_data;
90840267efdSSimon A. F. Lund }
909ca064085SMatias Bjørling 
910400b6a7bSGuenter Roeck #ifdef CONFIG_NVME_HWMON
91159e330f8SKeith Busch int nvme_hwmon_init(struct nvme_ctrl *ctrl);
912ed7770f6SHannes Reinecke void nvme_hwmon_exit(struct nvme_ctrl *ctrl);
913400b6a7bSGuenter Roeck #else
91459e330f8SKeith Busch static inline int nvme_hwmon_init(struct nvme_ctrl *ctrl)
91559e330f8SKeith Busch {
91659e330f8SKeith Busch 	return 0;
91759e330f8SKeith Busch }
918ed7770f6SHannes Reinecke 
919ed7770f6SHannes Reinecke static inline void nvme_hwmon_exit(struct nvme_ctrl *ctrl)
920ed7770f6SHannes Reinecke {
921ed7770f6SHannes Reinecke }
922400b6a7bSGuenter Roeck #endif
923400b6a7bSGuenter Roeck 
92473eefc27SChaitanya Kulkarni static inline bool nvme_ctrl_sgl_supported(struct nvme_ctrl *ctrl)
92573eefc27SChaitanya Kulkarni {
92673eefc27SChaitanya Kulkarni 	return ctrl->sgls & ((1 << 0) | (1 << 1));
92773eefc27SChaitanya Kulkarni }
92873eefc27SChaitanya Kulkarni 
929df21b6b1SLogan Gunthorpe u32 nvme_command_effects(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
930df21b6b1SLogan Gunthorpe 			 u8 opcode);
931ae5e6886SKeith Busch int nvme_execute_passthru_rq(struct request *rq);
932b2702aaaSChaitanya Kulkarni struct nvme_ctrl *nvme_ctrl_from_file(struct file *file);
93324493b8bSLogan Gunthorpe struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid);
93424493b8bSLogan Gunthorpe void nvme_put_ns(struct nvme_ns *ns);
935df21b6b1SLogan Gunthorpe 
93643dc9878SAdam Manzanares static inline bool nvme_multi_css(struct nvme_ctrl *ctrl)
93743dc9878SAdam Manzanares {
93843dc9878SAdam Manzanares 	return (ctrl->ctrl_config & NVME_CC_CSS_MASK) == NVME_CC_CSS_CSI;
93943dc9878SAdam Manzanares }
94043dc9878SAdam Manzanares 
941*bd83fe6fSAlan Adamson #ifdef CONFIG_NVME_VERBOSE_ERRORS
942*bd83fe6fSAlan Adamson const unsigned char *nvme_get_error_status_str(u16 status);
943*bd83fe6fSAlan Adamson const unsigned char *nvme_get_opcode_str(u8 opcode);
944*bd83fe6fSAlan Adamson const unsigned char *nvme_get_admin_opcode_str(u8 opcode);
945*bd83fe6fSAlan Adamson #else /* CONFIG_NVME_VERBOSE_ERRORS */
946*bd83fe6fSAlan Adamson static inline const unsigned char *nvme_get_error_status_str(u16 status)
947*bd83fe6fSAlan Adamson {
948*bd83fe6fSAlan Adamson 	return "I/O Error";
949*bd83fe6fSAlan Adamson }
950*bd83fe6fSAlan Adamson static inline const unsigned char *nvme_get_opcode_str(u8 opcode)
951*bd83fe6fSAlan Adamson {
952*bd83fe6fSAlan Adamson 	return "I/O Cmd";
953*bd83fe6fSAlan Adamson }
954*bd83fe6fSAlan Adamson static inline const unsigned char *nvme_get_admin_opcode_str(u8 opcode)
955*bd83fe6fSAlan Adamson {
956*bd83fe6fSAlan Adamson 	return "Admin Cmd";
957*bd83fe6fSAlan Adamson }
958*bd83fe6fSAlan Adamson #endif /* CONFIG_NVME_VERBOSE_ERRORS */
959*bd83fe6fSAlan Adamson 
96057dacad5SJay Sternberg #endif /* _NVME_H */
961