xref: /openbmc/linux/drivers/nvme/host/nvme.h (revision 54adc010)
157dacad5SJay Sternberg /*
257dacad5SJay Sternberg  * Copyright (c) 2011-2014, Intel Corporation.
357dacad5SJay Sternberg  *
457dacad5SJay Sternberg  * This program is free software; you can redistribute it and/or modify it
557dacad5SJay Sternberg  * under the terms and conditions of the GNU General Public License,
657dacad5SJay Sternberg  * version 2, as published by the Free Software Foundation.
757dacad5SJay Sternberg  *
857dacad5SJay Sternberg  * This program is distributed in the hope it will be useful, but WITHOUT
957dacad5SJay Sternberg  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1057dacad5SJay Sternberg  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
1157dacad5SJay Sternberg  * more details.
1257dacad5SJay Sternberg  */
1357dacad5SJay Sternberg 
1457dacad5SJay Sternberg #ifndef _NVME_H
1557dacad5SJay Sternberg #define _NVME_H
1657dacad5SJay Sternberg 
1757dacad5SJay Sternberg #include <linux/nvme.h>
1857dacad5SJay Sternberg #include <linux/pci.h>
1957dacad5SJay Sternberg #include <linux/kref.h>
2057dacad5SJay Sternberg #include <linux/blk-mq.h>
2157dacad5SJay Sternberg 
22297465c8SChristoph Hellwig enum {
23297465c8SChristoph Hellwig 	/*
24297465c8SChristoph Hellwig 	 * Driver internal status code for commands that were cancelled due
25297465c8SChristoph Hellwig 	 * to timeouts or controller shutdown.  The value is negative so
26297465c8SChristoph Hellwig 	 * that it a) doesn't overlap with the unsigned hardware error codes,
27297465c8SChristoph Hellwig 	 * and b) can easily be tested for.
28297465c8SChristoph Hellwig 	 */
29297465c8SChristoph Hellwig 	NVME_SC_CANCELLED		= -EINTR,
30297465c8SChristoph Hellwig };
31297465c8SChristoph Hellwig 
3257dacad5SJay Sternberg extern unsigned char nvme_io_timeout;
3357dacad5SJay Sternberg #define NVME_IO_TIMEOUT	(nvme_io_timeout * HZ)
3457dacad5SJay Sternberg 
3521d34711SChristoph Hellwig extern unsigned char admin_timeout;
3621d34711SChristoph Hellwig #define ADMIN_TIMEOUT	(admin_timeout * HZ)
3721d34711SChristoph Hellwig 
385fd4ce1bSChristoph Hellwig extern unsigned char shutdown_timeout;
395fd4ce1bSChristoph Hellwig #define SHUTDOWN_TIMEOUT	(shutdown_timeout * HZ)
405fd4ce1bSChristoph Hellwig 
41038bd4cbSSagi Grimberg #define NVME_DEFAULT_KATO	5
42038bd4cbSSagi Grimberg #define NVME_KATO_GRACE		10
43038bd4cbSSagi Grimberg 
44ca064085SMatias Bjørling enum {
45ca064085SMatias Bjørling 	NVME_NS_LBA		= 0,
46ca064085SMatias Bjørling 	NVME_NS_LIGHTNVM	= 1,
47ca064085SMatias Bjørling };
48ca064085SMatias Bjørling 
4957dacad5SJay Sternberg /*
50106198edSChristoph Hellwig  * List of workarounds for devices that required behavior not specified in
51106198edSChristoph Hellwig  * the standard.
5257dacad5SJay Sternberg  */
53106198edSChristoph Hellwig enum nvme_quirks {
54106198edSChristoph Hellwig 	/*
55106198edSChristoph Hellwig 	 * Prefers I/O aligned to a stripe size specified in a vendor
56106198edSChristoph Hellwig 	 * specific Identify field.
57106198edSChristoph Hellwig 	 */
58106198edSChristoph Hellwig 	NVME_QUIRK_STRIPE_SIZE			= (1 << 0),
59540c801cSKeith Busch 
60540c801cSKeith Busch 	/*
61540c801cSKeith Busch 	 * The controller doesn't handle Identify value others than 0 or 1
62540c801cSKeith Busch 	 * correctly.
63540c801cSKeith Busch 	 */
64540c801cSKeith Busch 	NVME_QUIRK_IDENTIFY_CNS			= (1 << 1),
6508095e70SKeith Busch 
6608095e70SKeith Busch 	/*
6708095e70SKeith Busch 	 * The controller deterministically returns O's on reads to discarded
6808095e70SKeith Busch 	 * logical blocks.
6908095e70SKeith Busch 	 */
7008095e70SKeith Busch 	NVME_QUIRK_DISCARD_ZEROES		= (1 << 2),
71*54adc010SGuilherme G. Piccoli 
72*54adc010SGuilherme G. Piccoli 	/*
73*54adc010SGuilherme G. Piccoli 	 * The controller needs a delay before starts checking the device
74*54adc010SGuilherme G. Piccoli 	 * readiness, which is done by reading the NVME_CSTS_RDY bit.
75*54adc010SGuilherme G. Piccoli 	 */
76*54adc010SGuilherme G. Piccoli 	NVME_QUIRK_DELAY_BEFORE_CHK_RDY		= (1 << 3),
77106198edSChristoph Hellwig };
78106198edSChristoph Hellwig 
79*54adc010SGuilherme G. Piccoli /* The below value is the specific amount of delay needed before checking
80*54adc010SGuilherme G. Piccoli  * readiness in case of the PCI_DEVICE(0x1c58, 0x0003), which needs the
81*54adc010SGuilherme G. Piccoli  * NVME_QUIRK_DELAY_BEFORE_CHK_RDY quirk enabled. The value (in ms) was
82*54adc010SGuilherme G. Piccoli  * found empirically.
83*54adc010SGuilherme G. Piccoli  */
84*54adc010SGuilherme G. Piccoli #define NVME_QUIRK_DELAY_AMOUNT		2000
85*54adc010SGuilherme G. Piccoli 
86bb8d261eSChristoph Hellwig enum nvme_ctrl_state {
87bb8d261eSChristoph Hellwig 	NVME_CTRL_NEW,
88bb8d261eSChristoph Hellwig 	NVME_CTRL_LIVE,
89bb8d261eSChristoph Hellwig 	NVME_CTRL_RESETTING,
90def61ecaSChristoph Hellwig 	NVME_CTRL_RECONNECTING,
91bb8d261eSChristoph Hellwig 	NVME_CTRL_DELETING,
920ff9d4e1SKeith Busch 	NVME_CTRL_DEAD,
93bb8d261eSChristoph Hellwig };
94bb8d261eSChristoph Hellwig 
951c63dc66SChristoph Hellwig struct nvme_ctrl {
96bb8d261eSChristoph Hellwig 	enum nvme_ctrl_state state;
97bb8d261eSChristoph Hellwig 	spinlock_t lock;
981c63dc66SChristoph Hellwig 	const struct nvme_ctrl_ops *ops;
9957dacad5SJay Sternberg 	struct request_queue *admin_q;
10007bfcd09SChristoph Hellwig 	struct request_queue *connect_q;
10157dacad5SJay Sternberg 	struct device *dev;
10257dacad5SJay Sternberg 	struct kref kref;
10357dacad5SJay Sternberg 	int instance;
1045bae7f73SChristoph Hellwig 	struct blk_mq_tag_set *tagset;
1055bae7f73SChristoph Hellwig 	struct list_head namespaces;
10669d3b8acSChristoph Hellwig 	struct mutex namespaces_mutex;
1075bae7f73SChristoph Hellwig 	struct device *device;	/* char device */
108f3ca80fcSChristoph Hellwig 	struct list_head node;
109075790ebSKeith Busch 	struct ida ns_ida;
1101c63dc66SChristoph Hellwig 
11157dacad5SJay Sternberg 	char name[12];
11257dacad5SJay Sternberg 	char serial[20];
11357dacad5SJay Sternberg 	char model[40];
11457dacad5SJay Sternberg 	char firmware_rev[8];
11576e3914aSChristoph Hellwig 	u16 cntlid;
1165fd4ce1bSChristoph Hellwig 
1175fd4ce1bSChristoph Hellwig 	u32 ctrl_config;
1185fd4ce1bSChristoph Hellwig 
1195fd4ce1bSChristoph Hellwig 	u32 page_size;
12057dacad5SJay Sternberg 	u32 max_hw_sectors;
12157dacad5SJay Sternberg 	u32 stripe_size;
12257dacad5SJay Sternberg 	u16 oncs;
123118472abSKeith Busch 	u16 vid;
1246bf25d16SChristoph Hellwig 	atomic_t abort_limit;
12557dacad5SJay Sternberg 	u8 event_limit;
12657dacad5SJay Sternberg 	u8 vwc;
127f3ca80fcSChristoph Hellwig 	u32 vs;
12807bfcd09SChristoph Hellwig 	u32 sgls;
129038bd4cbSSagi Grimberg 	u16 kas;
130038bd4cbSSagi Grimberg 	unsigned int kato;
131f3ca80fcSChristoph Hellwig 	bool subsystem;
132106198edSChristoph Hellwig 	unsigned long quirks;
1335955be21SChristoph Hellwig 	struct work_struct scan_work;
134f866fc42SChristoph Hellwig 	struct work_struct async_event_work;
135038bd4cbSSagi Grimberg 	struct delayed_work ka_work;
13607bfcd09SChristoph Hellwig 
13707bfcd09SChristoph Hellwig 	/* Fabrics only */
13807bfcd09SChristoph Hellwig 	u16 sqsize;
13907bfcd09SChristoph Hellwig 	u32 ioccsz;
14007bfcd09SChristoph Hellwig 	u32 iorcsz;
14107bfcd09SChristoph Hellwig 	u16 icdoff;
14207bfcd09SChristoph Hellwig 	u16 maxcmd;
14307bfcd09SChristoph Hellwig 	struct nvmf_ctrl_options *opts;
14457dacad5SJay Sternberg };
14557dacad5SJay Sternberg 
14657dacad5SJay Sternberg /*
14757dacad5SJay Sternberg  * An NVM Express namespace is equivalent to a SCSI LUN
14857dacad5SJay Sternberg  */
14957dacad5SJay Sternberg struct nvme_ns {
15057dacad5SJay Sternberg 	struct list_head list;
15157dacad5SJay Sternberg 
1521c63dc66SChristoph Hellwig 	struct nvme_ctrl *ctrl;
15357dacad5SJay Sternberg 	struct request_queue *queue;
15457dacad5SJay Sternberg 	struct gendisk *disk;
15557dacad5SJay Sternberg 	struct kref kref;
156075790ebSKeith Busch 	int instance;
15757dacad5SJay Sternberg 
1582b9b6e86SKeith Busch 	u8 eui[8];
1592b9b6e86SKeith Busch 	u8 uuid[16];
1602b9b6e86SKeith Busch 
16157dacad5SJay Sternberg 	unsigned ns_id;
16257dacad5SJay Sternberg 	int lba_shift;
16357dacad5SJay Sternberg 	u16 ms;
16457dacad5SJay Sternberg 	bool ext;
16557dacad5SJay Sternberg 	u8 pi_type;
166ca064085SMatias Bjørling 	int type;
167646017a6SKeith Busch 	unsigned long flags;
168646017a6SKeith Busch 
169646017a6SKeith Busch #define NVME_NS_REMOVING 0
17069d9a99cSKeith Busch #define NVME_NS_DEAD     1
171646017a6SKeith Busch 
17257dacad5SJay Sternberg 	u64 mode_select_num_blocks;
17357dacad5SJay Sternberg 	u32 mode_select_block_len;
17457dacad5SJay Sternberg };
17557dacad5SJay Sternberg 
1761c63dc66SChristoph Hellwig struct nvme_ctrl_ops {
1771a353d85SMing Lin 	const char *name;
178e439bb12SSagi Grimberg 	struct module *module;
17907bfcd09SChristoph Hellwig 	bool is_fabrics;
1801c63dc66SChristoph Hellwig 	int (*reg_read32)(struct nvme_ctrl *ctrl, u32 off, u32 *val);
1815fd4ce1bSChristoph Hellwig 	int (*reg_write32)(struct nvme_ctrl *ctrl, u32 off, u32 val);
1827fd8930fSChristoph Hellwig 	int (*reg_read64)(struct nvme_ctrl *ctrl, u32 off, u64 *val);
183f3ca80fcSChristoph Hellwig 	int (*reset_ctrl)(struct nvme_ctrl *ctrl);
1841673f1f0SChristoph Hellwig 	void (*free_ctrl)(struct nvme_ctrl *ctrl);
1855955be21SChristoph Hellwig 	void (*post_scan)(struct nvme_ctrl *ctrl);
186f866fc42SChristoph Hellwig 	void (*submit_async_event)(struct nvme_ctrl *ctrl, int aer_idx);
1871a353d85SMing Lin 	int (*delete_ctrl)(struct nvme_ctrl *ctrl);
1881a353d85SMing Lin 	const char *(*get_subsysnqn)(struct nvme_ctrl *ctrl);
1891a353d85SMing Lin 	int (*get_address)(struct nvme_ctrl *ctrl, char *buf, int size);
19057dacad5SJay Sternberg };
19157dacad5SJay Sternberg 
1921c63dc66SChristoph Hellwig static inline bool nvme_ctrl_ready(struct nvme_ctrl *ctrl)
1931c63dc66SChristoph Hellwig {
1941c63dc66SChristoph Hellwig 	u32 val = 0;
1951c63dc66SChristoph Hellwig 
1961c63dc66SChristoph Hellwig 	if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &val))
1971c63dc66SChristoph Hellwig 		return false;
1981c63dc66SChristoph Hellwig 	return val & NVME_CSTS_RDY;
1991c63dc66SChristoph Hellwig }
2001c63dc66SChristoph Hellwig 
201f3ca80fcSChristoph Hellwig static inline int nvme_reset_subsystem(struct nvme_ctrl *ctrl)
202f3ca80fcSChristoph Hellwig {
203f3ca80fcSChristoph Hellwig 	if (!ctrl->subsystem)
204f3ca80fcSChristoph Hellwig 		return -ENOTTY;
205f3ca80fcSChristoph Hellwig 	return ctrl->ops->reg_write32(ctrl, NVME_REG_NSSR, 0x4E564D65);
206f3ca80fcSChristoph Hellwig }
207f3ca80fcSChristoph Hellwig 
20857dacad5SJay Sternberg static inline u64 nvme_block_nr(struct nvme_ns *ns, sector_t sector)
20957dacad5SJay Sternberg {
21057dacad5SJay Sternberg 	return (sector >> (ns->lba_shift - 9));
21157dacad5SJay Sternberg }
21257dacad5SJay Sternberg 
21358b45602SMing Lin static inline unsigned nvme_map_len(struct request *rq)
21458b45602SMing Lin {
215c2df40dfSMike Christie 	if (req_op(rq) == REQ_OP_DISCARD)
21658b45602SMing Lin 		return sizeof(struct nvme_dsm_range);
21758b45602SMing Lin 	else
21858b45602SMing Lin 		return blk_rq_bytes(rq);
21958b45602SMing Lin }
22058b45602SMing Lin 
2216904242dSMing Lin static inline void nvme_cleanup_cmd(struct request *req)
2226904242dSMing Lin {
223c2df40dfSMike Christie 	if (req_op(req) == REQ_OP_DISCARD)
2246904242dSMing Lin 		kfree(req->completion_data);
2256904242dSMing Lin }
2266904242dSMing Lin 
22715a190f7SChristoph Hellwig static inline int nvme_error_status(u16 status)
22815a190f7SChristoph Hellwig {
22915a190f7SChristoph Hellwig 	switch (status & 0x7ff) {
23015a190f7SChristoph Hellwig 	case NVME_SC_SUCCESS:
23115a190f7SChristoph Hellwig 		return 0;
23215a190f7SChristoph Hellwig 	case NVME_SC_CAP_EXCEEDED:
23315a190f7SChristoph Hellwig 		return -ENOSPC;
23415a190f7SChristoph Hellwig 	default:
23515a190f7SChristoph Hellwig 		return -EIO;
23615a190f7SChristoph Hellwig 	}
23715a190f7SChristoph Hellwig }
23815a190f7SChristoph Hellwig 
2397688faa6SChristoph Hellwig static inline bool nvme_req_needs_retry(struct request *req, u16 status)
2407688faa6SChristoph Hellwig {
2417688faa6SChristoph Hellwig 	return !(status & NVME_SC_DNR || blk_noretry_request(req)) &&
2427688faa6SChristoph Hellwig 		(jiffies - req->start_time) < req->timeout;
2437688faa6SChristoph Hellwig }
2447688faa6SChristoph Hellwig 
245c55a2fd4SMing Lin void nvme_cancel_request(struct request *req, void *data, bool reserved);
246bb8d261eSChristoph Hellwig bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
247bb8d261eSChristoph Hellwig 		enum nvme_ctrl_state new_state);
2485fd4ce1bSChristoph Hellwig int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap);
2495fd4ce1bSChristoph Hellwig int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap);
2505fd4ce1bSChristoph Hellwig int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl);
251f3ca80fcSChristoph Hellwig int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
252f3ca80fcSChristoph Hellwig 		const struct nvme_ctrl_ops *ops, unsigned long quirks);
25353029b04SKeith Busch void nvme_uninit_ctrl(struct nvme_ctrl *ctrl);
2541673f1f0SChristoph Hellwig void nvme_put_ctrl(struct nvme_ctrl *ctrl);
2557fd8930fSChristoph Hellwig int nvme_init_identify(struct nvme_ctrl *ctrl);
2565bae7f73SChristoph Hellwig 
2575955be21SChristoph Hellwig void nvme_queue_scan(struct nvme_ctrl *ctrl);
2585bae7f73SChristoph Hellwig void nvme_remove_namespaces(struct nvme_ctrl *ctrl);
2591673f1f0SChristoph Hellwig 
260f866fc42SChristoph Hellwig #define NVME_NR_AERS	1
261f866fc42SChristoph Hellwig void nvme_complete_async_event(struct nvme_ctrl *ctrl,
262f866fc42SChristoph Hellwig 		struct nvme_completion *cqe);
263f866fc42SChristoph Hellwig void nvme_queue_async_events(struct nvme_ctrl *ctrl);
264f866fc42SChristoph Hellwig 
26525646264SKeith Busch void nvme_stop_queues(struct nvme_ctrl *ctrl);
26625646264SKeith Busch void nvme_start_queues(struct nvme_ctrl *ctrl);
26769d9a99cSKeith Busch void nvme_kill_queues(struct nvme_ctrl *ctrl);
268363c9aacSSagi Grimberg 
269eb71f435SChristoph Hellwig #define NVME_QID_ANY -1
2704160982eSChristoph Hellwig struct request *nvme_alloc_request(struct request_queue *q,
271eb71f435SChristoph Hellwig 		struct nvme_command *cmd, unsigned int flags, int qid);
2727688faa6SChristoph Hellwig void nvme_requeue_req(struct request *req);
2738093f7caSMing Lin int nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
2748093f7caSMing Lin 		struct nvme_command *cmd);
27557dacad5SJay Sternberg int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
27657dacad5SJay Sternberg 		void *buf, unsigned bufflen);
27757dacad5SJay Sternberg int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
2781cb3cce5SChristoph Hellwig 		struct nvme_completion *cqe, void *buffer, unsigned bufflen,
279eb71f435SChristoph Hellwig 		unsigned timeout, int qid, int at_head, int flags);
2804160982eSChristoph Hellwig int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
2814160982eSChristoph Hellwig 		void __user *ubuffer, unsigned bufflen, u32 *result,
2824160982eSChristoph Hellwig 		unsigned timeout);
2830b7f1f26SKeith Busch int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
2840b7f1f26SKeith Busch 		void __user *ubuffer, unsigned bufflen,
2850b7f1f26SKeith Busch 		void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
28657dacad5SJay Sternberg 		u32 *result, unsigned timeout);
2871c63dc66SChristoph Hellwig int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id);
2881c63dc66SChristoph Hellwig int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
28957dacad5SJay Sternberg 		struct nvme_id_ns **id);
2901c63dc66SChristoph Hellwig int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log);
2911c63dc66SChristoph Hellwig int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
29257dacad5SJay Sternberg 			dma_addr_t dma_addr, u32 *result);
2931c63dc66SChristoph Hellwig int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
29457dacad5SJay Sternberg 			dma_addr_t dma_addr, u32 *result);
2959a0be7abSChristoph Hellwig int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count);
296038bd4cbSSagi Grimberg void nvme_start_keep_alive(struct nvme_ctrl *ctrl);
297038bd4cbSSagi Grimberg void nvme_stop_keep_alive(struct nvme_ctrl *ctrl);
29857dacad5SJay Sternberg 
29957dacad5SJay Sternberg struct sg_io_hdr;
30057dacad5SJay Sternberg 
30157dacad5SJay Sternberg int nvme_sg_io(struct nvme_ns *ns, struct sg_io_hdr __user *u_hdr);
30257dacad5SJay Sternberg int nvme_sg_io32(struct nvme_ns *ns, unsigned long arg);
30357dacad5SJay Sternberg int nvme_sg_get_version_num(int __user *ip);
30457dacad5SJay Sternberg 
305c4699e70SKeith Busch #ifdef CONFIG_NVM
306ca064085SMatias Bjørling int nvme_nvm_ns_supported(struct nvme_ns *ns, struct nvme_id_ns *id);
307ca064085SMatias Bjørling int nvme_nvm_register(struct request_queue *q, char *disk_name);
308ca064085SMatias Bjørling void nvme_nvm_unregister(struct request_queue *q, char *disk_name);
309c4699e70SKeith Busch #else
310c4699e70SKeith Busch static inline int nvme_nvm_register(struct request_queue *q, char *disk_name)
311c4699e70SKeith Busch {
312c4699e70SKeith Busch 	return 0;
313c4699e70SKeith Busch }
314c4699e70SKeith Busch 
315c4699e70SKeith Busch static inline void nvme_nvm_unregister(struct request_queue *q, char *disk_name) {};
316c4699e70SKeith Busch 
317c4699e70SKeith Busch static inline int nvme_nvm_ns_supported(struct nvme_ns *ns, struct nvme_id_ns *id)
318c4699e70SKeith Busch {
319c4699e70SKeith Busch 	return 0;
320c4699e70SKeith Busch }
321c4699e70SKeith Busch #endif /* CONFIG_NVM */
322ca064085SMatias Bjørling 
3235bae7f73SChristoph Hellwig int __init nvme_core_init(void);
3245bae7f73SChristoph Hellwig void nvme_core_exit(void);
3255bae7f73SChristoph Hellwig 
32657dacad5SJay Sternberg #endif /* _NVME_H */
327