xref: /openbmc/linux/drivers/nvme/target/nvmet.h (revision 174cd4b1)
1 /*
2  * Copyright (c) 2015-2016 HGST, a Western Digital Company.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  */
13 
14 #ifndef _NVMET_H
15 #define _NVMET_H
16 
17 #include <linux/dma-mapping.h>
18 #include <linux/types.h>
19 #include <linux/device.h>
20 #include <linux/kref.h>
21 #include <linux/percpu-refcount.h>
22 #include <linux/list.h>
23 #include <linux/mutex.h>
24 #include <linux/nvme.h>
25 #include <linux/configfs.h>
26 #include <linux/rcupdate.h>
27 #include <linux/blkdev.h>
28 
29 #define NVMET_ASYNC_EVENTS		4
30 #define NVMET_ERROR_LOG_SLOTS		128
31 
32 /* Helper Macros when NVMe error is NVME_SC_CONNECT_INVALID_PARAM
33  * The 16 bit shift is to set IATTR bit to 1, which means offending
34  * offset starts in the data section of connect()
35  */
36 #define IPO_IATTR_CONNECT_DATA(x)	\
37 	(cpu_to_le32((1 << 16) | (offsetof(struct nvmf_connect_data, x))))
38 #define IPO_IATTR_CONNECT_SQE(x)	\
39 	(cpu_to_le32(offsetof(struct nvmf_connect_command, x)))
40 
41 struct nvmet_ns {
42 	struct list_head	dev_link;
43 	struct percpu_ref	ref;
44 	struct block_device	*bdev;
45 	u32			nsid;
46 	u32			blksize_shift;
47 	loff_t			size;
48 	u8			nguid[16];
49 
50 	bool			enabled;
51 	struct nvmet_subsys	*subsys;
52 	const char		*device_path;
53 
54 	struct config_group	device_group;
55 	struct config_group	group;
56 
57 	struct completion	disable_done;
58 };
59 
60 static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
61 {
62 	return container_of(to_config_group(item), struct nvmet_ns, group);
63 }
64 
65 struct nvmet_cq {
66 	u16			qid;
67 	u16			size;
68 };
69 
70 struct nvmet_sq {
71 	struct nvmet_ctrl	*ctrl;
72 	struct percpu_ref	ref;
73 	u16			qid;
74 	u16			size;
75 	struct completion	free_done;
76 };
77 
78 /**
79  * struct nvmet_port -	Common structure to keep port
80  *				information for the target.
81  * @entry:		List head for holding a list of these elements.
82  * @disc_addr:		Address information is stored in a format defined
83  *				for a discovery log page entry.
84  * @group:		ConfigFS group for this element's folder.
85  * @priv:		Private data for the transport.
86  */
87 struct nvmet_port {
88 	struct list_head		entry;
89 	struct nvmf_disc_rsp_page_entry	disc_addr;
90 	struct config_group		group;
91 	struct config_group		subsys_group;
92 	struct list_head		subsystems;
93 	struct config_group		referrals_group;
94 	struct list_head		referrals;
95 	void				*priv;
96 	bool				enabled;
97 };
98 
99 static inline struct nvmet_port *to_nvmet_port(struct config_item *item)
100 {
101 	return container_of(to_config_group(item), struct nvmet_port,
102 			group);
103 }
104 
105 struct nvmet_ctrl {
106 	struct nvmet_subsys	*subsys;
107 	struct nvmet_cq		**cqs;
108 	struct nvmet_sq		**sqs;
109 
110 	struct mutex		lock;
111 	u64			cap;
112 	u64			serial;
113 	u32			cc;
114 	u32			csts;
115 
116 	u16			cntlid;
117 	u32			kato;
118 
119 	struct nvmet_req	*async_event_cmds[NVMET_ASYNC_EVENTS];
120 	unsigned int		nr_async_event_cmds;
121 	struct list_head	async_events;
122 	struct work_struct	async_event_work;
123 
124 	struct list_head	subsys_entry;
125 	struct kref		ref;
126 	struct delayed_work	ka_work;
127 	struct work_struct	fatal_err_work;
128 
129 	struct nvmet_fabrics_ops *ops;
130 
131 	char			subsysnqn[NVMF_NQN_FIELD_LEN];
132 	char			hostnqn[NVMF_NQN_FIELD_LEN];
133 };
134 
135 struct nvmet_subsys {
136 	enum nvme_subsys_type	type;
137 
138 	struct mutex		lock;
139 	struct kref		ref;
140 
141 	struct list_head	namespaces;
142 	unsigned int		max_nsid;
143 
144 	struct list_head	ctrls;
145 
146 	struct list_head	hosts;
147 	bool			allow_any_host;
148 
149 	u16			max_qid;
150 
151 	u64			ver;
152 	char			*subsysnqn;
153 
154 	struct config_group	group;
155 
156 	struct config_group	namespaces_group;
157 	struct config_group	allowed_hosts_group;
158 };
159 
160 static inline struct nvmet_subsys *to_subsys(struct config_item *item)
161 {
162 	return container_of(to_config_group(item), struct nvmet_subsys, group);
163 }
164 
165 static inline struct nvmet_subsys *namespaces_to_subsys(
166 		struct config_item *item)
167 {
168 	return container_of(to_config_group(item), struct nvmet_subsys,
169 			namespaces_group);
170 }
171 
172 struct nvmet_host {
173 	struct config_group	group;
174 };
175 
176 static inline struct nvmet_host *to_host(struct config_item *item)
177 {
178 	return container_of(to_config_group(item), struct nvmet_host, group);
179 }
180 
181 static inline char *nvmet_host_name(struct nvmet_host *host)
182 {
183 	return config_item_name(&host->group.cg_item);
184 }
185 
186 struct nvmet_host_link {
187 	struct list_head	entry;
188 	struct nvmet_host	*host;
189 };
190 
191 struct nvmet_subsys_link {
192 	struct list_head	entry;
193 	struct nvmet_subsys	*subsys;
194 };
195 
196 struct nvmet_req;
197 struct nvmet_fabrics_ops {
198 	struct module *owner;
199 	unsigned int type;
200 	unsigned int sqe_inline_size;
201 	unsigned int msdbd;
202 	bool has_keyed_sgls : 1;
203 	void (*queue_response)(struct nvmet_req *req);
204 	int (*add_port)(struct nvmet_port *port);
205 	void (*remove_port)(struct nvmet_port *port);
206 	void (*delete_ctrl)(struct nvmet_ctrl *ctrl);
207 };
208 
209 #define NVMET_MAX_INLINE_BIOVEC	8
210 
211 struct nvmet_req {
212 	struct nvme_command	*cmd;
213 	struct nvme_completion	*rsp;
214 	struct nvmet_sq		*sq;
215 	struct nvmet_cq		*cq;
216 	struct nvmet_ns		*ns;
217 	struct scatterlist	*sg;
218 	struct bio		inline_bio;
219 	struct bio_vec		inline_bvec[NVMET_MAX_INLINE_BIOVEC];
220 	int			sg_cnt;
221 	size_t			data_len;
222 
223 	struct nvmet_port	*port;
224 
225 	void (*execute)(struct nvmet_req *req);
226 	struct nvmet_fabrics_ops *ops;
227 };
228 
229 static inline void nvmet_set_status(struct nvmet_req *req, u16 status)
230 {
231 	req->rsp->status = cpu_to_le16(status << 1);
232 }
233 
234 static inline void nvmet_set_result(struct nvmet_req *req, u32 result)
235 {
236 	req->rsp->result.u32 = cpu_to_le32(result);
237 }
238 
239 /*
240  * NVMe command writes actually are DMA reads for us on the target side.
241  */
242 static inline enum dma_data_direction
243 nvmet_data_dir(struct nvmet_req *req)
244 {
245 	return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
246 }
247 
248 struct nvmet_async_event {
249 	struct list_head	entry;
250 	u8			event_type;
251 	u8			event_info;
252 	u8			log_page;
253 };
254 
255 int nvmet_parse_connect_cmd(struct nvmet_req *req);
256 int nvmet_parse_io_cmd(struct nvmet_req *req);
257 int nvmet_parse_admin_cmd(struct nvmet_req *req);
258 int nvmet_parse_discovery_cmd(struct nvmet_req *req);
259 int nvmet_parse_fabrics_cmd(struct nvmet_req *req);
260 
261 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
262 		struct nvmet_sq *sq, struct nvmet_fabrics_ops *ops);
263 void nvmet_req_complete(struct nvmet_req *req, u16 status);
264 
265 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid,
266 		u16 size);
267 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid,
268 		u16 size);
269 void nvmet_sq_destroy(struct nvmet_sq *sq);
270 int nvmet_sq_init(struct nvmet_sq *sq);
271 
272 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl);
273 
274 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new);
275 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
276 		struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp);
277 u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
278 		struct nvmet_req *req, struct nvmet_ctrl **ret);
279 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl);
280 
281 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
282 		enum nvme_subsys_type type);
283 void nvmet_subsys_put(struct nvmet_subsys *subsys);
284 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys);
285 
286 struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid);
287 void nvmet_put_namespace(struct nvmet_ns *ns);
288 int nvmet_ns_enable(struct nvmet_ns *ns);
289 void nvmet_ns_disable(struct nvmet_ns *ns);
290 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
291 void nvmet_ns_free(struct nvmet_ns *ns);
292 
293 int nvmet_register_transport(struct nvmet_fabrics_ops *ops);
294 void nvmet_unregister_transport(struct nvmet_fabrics_ops *ops);
295 
296 int nvmet_enable_port(struct nvmet_port *port);
297 void nvmet_disable_port(struct nvmet_port *port);
298 
299 void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port);
300 void nvmet_referral_disable(struct nvmet_port *port);
301 
302 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
303 		size_t len);
304 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf,
305 		size_t len);
306 
307 u32 nvmet_get_log_page_len(struct nvme_command *cmd);
308 
309 #define NVMET_QUEUE_SIZE	1024
310 #define NVMET_NR_QUEUES		64
311 #define NVMET_MAX_CMD		NVMET_QUEUE_SIZE
312 #define NVMET_KAS		10
313 #define NVMET_DISC_KATO		120
314 
315 int __init nvmet_init_configfs(void);
316 void __exit nvmet_exit_configfs(void);
317 
318 int __init nvmet_init_discovery(void);
319 void nvmet_exit_discovery(void);
320 
321 extern struct nvmet_subsys *nvmet_disc_subsys;
322 extern u64 nvmet_genctr;
323 extern struct rw_semaphore nvmet_config_sem;
324 
325 bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys,
326 		const char *hostnqn);
327 
328 #endif /* _NVMET_H */
329