xref: /openbmc/linux/drivers/nvme/target/nvmet.h (revision 4c9ca2fd)
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/uuid.h>
25 #include <linux/nvme.h>
26 #include <linux/configfs.h>
27 #include <linux/rcupdate.h>
28 #include <linux/blkdev.h>
29 
30 #define NVMET_ASYNC_EVENTS		4
31 #define NVMET_ERROR_LOG_SLOTS		128
32 
33 
34 /*
35  * Supported optional AENs:
36  */
37 #define NVMET_AEN_CFG_OPTIONAL \
38 	NVME_AEN_CFG_NS_ATTR
39 
40 /*
41  * Plus mandatory SMART AENs (we'll never send them, but allow enabling them):
42  */
43 #define NVMET_AEN_CFG_ALL \
44 	(NVME_SMART_CRIT_SPARE | NVME_SMART_CRIT_TEMPERATURE | \
45 	 NVME_SMART_CRIT_RELIABILITY | NVME_SMART_CRIT_MEDIA | \
46 	 NVME_SMART_CRIT_VOLATILE_MEMORY | NVMET_AEN_CFG_OPTIONAL)
47 
48 /* Helper Macros when NVMe error is NVME_SC_CONNECT_INVALID_PARAM
49  * The 16 bit shift is to set IATTR bit to 1, which means offending
50  * offset starts in the data section of connect()
51  */
52 #define IPO_IATTR_CONNECT_DATA(x)	\
53 	(cpu_to_le32((1 << 16) | (offsetof(struct nvmf_connect_data, x))))
54 #define IPO_IATTR_CONNECT_SQE(x)	\
55 	(cpu_to_le32(offsetof(struct nvmf_connect_command, x)))
56 
57 struct nvmet_ns {
58 	struct list_head	dev_link;
59 	struct percpu_ref	ref;
60 	struct block_device	*bdev;
61 	struct file		*file;
62 	u32			nsid;
63 	u32			blksize_shift;
64 	loff_t			size;
65 	u8			nguid[16];
66 	uuid_t			uuid;
67 
68 	bool			enabled;
69 	struct nvmet_subsys	*subsys;
70 	const char		*device_path;
71 
72 	struct config_group	device_group;
73 	struct config_group	group;
74 
75 	struct completion	disable_done;
76 	mempool_t		*bvec_pool;
77 	struct kmem_cache	*bvec_cache;
78 };
79 
80 static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
81 {
82 	return container_of(to_config_group(item), struct nvmet_ns, group);
83 }
84 
85 struct nvmet_cq {
86 	u16			qid;
87 	u16			size;
88 };
89 
90 struct nvmet_sq {
91 	struct nvmet_ctrl	*ctrl;
92 	struct percpu_ref	ref;
93 	u16			qid;
94 	u16			size;
95 	u32			sqhd;
96 	struct completion	free_done;
97 	struct completion	confirm_done;
98 };
99 
100 /**
101  * struct nvmet_port -	Common structure to keep port
102  *				information for the target.
103  * @entry:		Entry into referrals or transport list.
104  * @disc_addr:		Address information is stored in a format defined
105  *				for a discovery log page entry.
106  * @group:		ConfigFS group for this element's folder.
107  * @priv:		Private data for the transport.
108  */
109 struct nvmet_port {
110 	struct list_head		entry;
111 	struct nvmf_disc_rsp_page_entry	disc_addr;
112 	struct config_group		group;
113 	struct config_group		subsys_group;
114 	struct list_head		subsystems;
115 	struct config_group		referrals_group;
116 	struct list_head		referrals;
117 	void				*priv;
118 	bool				enabled;
119 };
120 
121 static inline struct nvmet_port *to_nvmet_port(struct config_item *item)
122 {
123 	return container_of(to_config_group(item), struct nvmet_port,
124 			group);
125 }
126 
127 struct nvmet_ctrl {
128 	struct nvmet_subsys	*subsys;
129 	struct nvmet_cq		**cqs;
130 	struct nvmet_sq		**sqs;
131 
132 	struct mutex		lock;
133 	u64			cap;
134 	u32			cc;
135 	u32			csts;
136 
137 	uuid_t			hostid;
138 	u16			cntlid;
139 	u32			kato;
140 
141 	u32			aen_enabled;
142 	unsigned long		aen_masked;
143 	struct nvmet_req	*async_event_cmds[NVMET_ASYNC_EVENTS];
144 	unsigned int		nr_async_event_cmds;
145 	struct list_head	async_events;
146 	struct work_struct	async_event_work;
147 
148 	struct list_head	subsys_entry;
149 	struct kref		ref;
150 	struct delayed_work	ka_work;
151 	struct work_struct	fatal_err_work;
152 
153 	const struct nvmet_fabrics_ops *ops;
154 
155 	__le32			*changed_ns_list;
156 	u32			nr_changed_ns;
157 
158 	char			subsysnqn[NVMF_NQN_FIELD_LEN];
159 	char			hostnqn[NVMF_NQN_FIELD_LEN];
160 };
161 
162 struct nvmet_subsys {
163 	enum nvme_subsys_type	type;
164 
165 	struct mutex		lock;
166 	struct kref		ref;
167 
168 	struct list_head	namespaces;
169 	unsigned int		max_nsid;
170 
171 	struct list_head	ctrls;
172 
173 	struct list_head	hosts;
174 	bool			allow_any_host;
175 
176 	u16			max_qid;
177 
178 	u64			ver;
179 	u64			serial;
180 	char			*subsysnqn;
181 
182 	struct config_group	group;
183 
184 	struct config_group	namespaces_group;
185 	struct config_group	allowed_hosts_group;
186 };
187 
188 static inline struct nvmet_subsys *to_subsys(struct config_item *item)
189 {
190 	return container_of(to_config_group(item), struct nvmet_subsys, group);
191 }
192 
193 static inline struct nvmet_subsys *namespaces_to_subsys(
194 		struct config_item *item)
195 {
196 	return container_of(to_config_group(item), struct nvmet_subsys,
197 			namespaces_group);
198 }
199 
200 struct nvmet_host {
201 	struct config_group	group;
202 };
203 
204 static inline struct nvmet_host *to_host(struct config_item *item)
205 {
206 	return container_of(to_config_group(item), struct nvmet_host, group);
207 }
208 
209 static inline char *nvmet_host_name(struct nvmet_host *host)
210 {
211 	return config_item_name(&host->group.cg_item);
212 }
213 
214 struct nvmet_host_link {
215 	struct list_head	entry;
216 	struct nvmet_host	*host;
217 };
218 
219 struct nvmet_subsys_link {
220 	struct list_head	entry;
221 	struct nvmet_subsys	*subsys;
222 };
223 
224 struct nvmet_req;
225 struct nvmet_fabrics_ops {
226 	struct module *owner;
227 	unsigned int type;
228 	unsigned int sqe_inline_size;
229 	unsigned int msdbd;
230 	bool has_keyed_sgls : 1;
231 	void (*queue_response)(struct nvmet_req *req);
232 	int (*add_port)(struct nvmet_port *port);
233 	void (*remove_port)(struct nvmet_port *port);
234 	void (*delete_ctrl)(struct nvmet_ctrl *ctrl);
235 	void (*disc_traddr)(struct nvmet_req *req,
236 			struct nvmet_port *port, char *traddr);
237 };
238 
239 #define NVMET_MAX_INLINE_BIOVEC	8
240 
241 struct nvmet_req {
242 	struct nvme_command	*cmd;
243 	struct nvme_completion	*rsp;
244 	struct nvmet_sq		*sq;
245 	struct nvmet_cq		*cq;
246 	struct nvmet_ns		*ns;
247 	struct scatterlist	*sg;
248 	struct bio_vec		inline_bvec[NVMET_MAX_INLINE_BIOVEC];
249 	union {
250 		struct {
251 			struct bio      inline_bio;
252 		} b;
253 		struct {
254 			bool			mpool_alloc;
255 			struct kiocb            iocb;
256 			struct bio_vec          *bvec;
257 			struct work_struct      work;
258 		} f;
259 	};
260 	int			sg_cnt;
261 	/* data length as parsed from the command: */
262 	size_t			data_len;
263 	/* data length as parsed from the SGL descriptor: */
264 	size_t			transfer_len;
265 
266 	struct nvmet_port	*port;
267 
268 	void (*execute)(struct nvmet_req *req);
269 	const struct nvmet_fabrics_ops *ops;
270 };
271 
272 static inline void nvmet_set_status(struct nvmet_req *req, u16 status)
273 {
274 	req->rsp->status = cpu_to_le16(status << 1);
275 }
276 
277 static inline void nvmet_set_result(struct nvmet_req *req, u32 result)
278 {
279 	req->rsp->result.u32 = cpu_to_le32(result);
280 }
281 
282 /*
283  * NVMe command writes actually are DMA reads for us on the target side.
284  */
285 static inline enum dma_data_direction
286 nvmet_data_dir(struct nvmet_req *req)
287 {
288 	return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
289 }
290 
291 struct nvmet_async_event {
292 	struct list_head	entry;
293 	u8			event_type;
294 	u8			event_info;
295 	u8			log_page;
296 };
297 
298 u16 nvmet_parse_connect_cmd(struct nvmet_req *req);
299 u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req);
300 u16 nvmet_file_parse_io_cmd(struct nvmet_req *req);
301 u16 nvmet_parse_admin_cmd(struct nvmet_req *req);
302 u16 nvmet_parse_discovery_cmd(struct nvmet_req *req);
303 u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req);
304 
305 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
306 		struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops);
307 void nvmet_req_uninit(struct nvmet_req *req);
308 void nvmet_req_execute(struct nvmet_req *req);
309 void nvmet_req_complete(struct nvmet_req *req, u16 status);
310 
311 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid,
312 		u16 size);
313 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid,
314 		u16 size);
315 void nvmet_sq_destroy(struct nvmet_sq *sq);
316 int nvmet_sq_init(struct nvmet_sq *sq);
317 
318 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl);
319 
320 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new);
321 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
322 		struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp);
323 u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
324 		struct nvmet_req *req, struct nvmet_ctrl **ret);
325 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl);
326 u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd);
327 
328 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
329 		enum nvme_subsys_type type);
330 void nvmet_subsys_put(struct nvmet_subsys *subsys);
331 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys);
332 
333 struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid);
334 void nvmet_put_namespace(struct nvmet_ns *ns);
335 int nvmet_ns_enable(struct nvmet_ns *ns);
336 void nvmet_ns_disable(struct nvmet_ns *ns);
337 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
338 void nvmet_ns_free(struct nvmet_ns *ns);
339 
340 int nvmet_register_transport(const struct nvmet_fabrics_ops *ops);
341 void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops);
342 
343 int nvmet_enable_port(struct nvmet_port *port);
344 void nvmet_disable_port(struct nvmet_port *port);
345 
346 void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port);
347 void nvmet_referral_disable(struct nvmet_port *port);
348 
349 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
350 		size_t len);
351 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf,
352 		size_t len);
353 u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len);
354 
355 u32 nvmet_get_log_page_len(struct nvme_command *cmd);
356 
357 #define NVMET_QUEUE_SIZE	1024
358 #define NVMET_NR_QUEUES		128
359 #define NVMET_MAX_CMD		NVMET_QUEUE_SIZE
360 #define NVMET_KAS		10
361 #define NVMET_DISC_KATO		120
362 
363 int __init nvmet_init_configfs(void);
364 void __exit nvmet_exit_configfs(void);
365 
366 int __init nvmet_init_discovery(void);
367 void nvmet_exit_discovery(void);
368 
369 extern struct nvmet_subsys *nvmet_disc_subsys;
370 extern u64 nvmet_genctr;
371 extern struct rw_semaphore nvmet_config_sem;
372 
373 bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys,
374 		const char *hostnqn);
375 
376 int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
377 int nvmet_file_ns_enable(struct nvmet_ns *ns);
378 void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
379 void nvmet_file_ns_disable(struct nvmet_ns *ns);
380 
381 static inline u32 nvmet_rw_len(struct nvmet_req *req)
382 {
383 	return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) <<
384 			req->ns->blksize_shift;
385 }
386 #endif /* _NVMET_H */
387