xref: /openbmc/linux/drivers/nvme/target/nvmet.h (revision 6d99a79c)
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 #include <linux/radix-tree.h>
30 
31 #define NVMET_ASYNC_EVENTS		4
32 #define NVMET_ERROR_LOG_SLOTS		128
33 
34 /*
35  * Supported optional AENs:
36  */
37 #define NVMET_AEN_CFG_OPTIONAL \
38 	(NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_ANA_CHANGE)
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 	bool			readonly;
63 	u32			nsid;
64 	u32			blksize_shift;
65 	loff_t			size;
66 	u8			nguid[16];
67 	uuid_t			uuid;
68 	u32			anagrpid;
69 
70 	bool			buffered_io;
71 	bool			enabled;
72 	struct nvmet_subsys	*subsys;
73 	const char		*device_path;
74 
75 	struct config_group	device_group;
76 	struct config_group	group;
77 
78 	struct completion	disable_done;
79 	mempool_t		*bvec_pool;
80 	struct kmem_cache	*bvec_cache;
81 
82 	int			use_p2pmem;
83 	struct pci_dev		*p2p_dev;
84 };
85 
86 static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
87 {
88 	return container_of(to_config_group(item), struct nvmet_ns, group);
89 }
90 
91 static inline struct device *nvmet_ns_dev(struct nvmet_ns *ns)
92 {
93 	return ns->bdev ? disk_to_dev(ns->bdev->bd_disk) : NULL;
94 }
95 
96 struct nvmet_cq {
97 	u16			qid;
98 	u16			size;
99 };
100 
101 struct nvmet_sq {
102 	struct nvmet_ctrl	*ctrl;
103 	struct percpu_ref	ref;
104 	u16			qid;
105 	u16			size;
106 	u32			sqhd;
107 	struct completion	free_done;
108 	struct completion	confirm_done;
109 };
110 
111 struct nvmet_ana_group {
112 	struct config_group	group;
113 	struct nvmet_port	*port;
114 	u32			grpid;
115 };
116 
117 static inline struct nvmet_ana_group *to_ana_group(struct config_item *item)
118 {
119 	return container_of(to_config_group(item), struct nvmet_ana_group,
120 			group);
121 }
122 
123 /**
124  * struct nvmet_port -	Common structure to keep port
125  *				information for the target.
126  * @entry:		Entry into referrals or transport list.
127  * @disc_addr:		Address information is stored in a format defined
128  *				for a discovery log page entry.
129  * @group:		ConfigFS group for this element's folder.
130  * @priv:		Private data for the transport.
131  */
132 struct nvmet_port {
133 	struct list_head		entry;
134 	struct nvmf_disc_rsp_page_entry	disc_addr;
135 	struct config_group		group;
136 	struct config_group		subsys_group;
137 	struct list_head		subsystems;
138 	struct config_group		referrals_group;
139 	struct list_head		referrals;
140 	struct config_group		ana_groups_group;
141 	struct nvmet_ana_group		ana_default_group;
142 	enum nvme_ana_state		*ana_state;
143 	void				*priv;
144 	bool				enabled;
145 	int				inline_data_size;
146 };
147 
148 static inline struct nvmet_port *to_nvmet_port(struct config_item *item)
149 {
150 	return container_of(to_config_group(item), struct nvmet_port,
151 			group);
152 }
153 
154 static inline struct nvmet_port *ana_groups_to_port(
155 		struct config_item *item)
156 {
157 	return container_of(to_config_group(item), struct nvmet_port,
158 			ana_groups_group);
159 }
160 
161 struct nvmet_ctrl {
162 	struct nvmet_subsys	*subsys;
163 	struct nvmet_cq		**cqs;
164 	struct nvmet_sq		**sqs;
165 
166 	struct mutex		lock;
167 	u64			cap;
168 	u32			cc;
169 	u32			csts;
170 
171 	uuid_t			hostid;
172 	u16			cntlid;
173 	u32			kato;
174 
175 	struct nvmet_port	*port;
176 
177 	u32			aen_enabled;
178 	unsigned long		aen_masked;
179 	struct nvmet_req	*async_event_cmds[NVMET_ASYNC_EVENTS];
180 	unsigned int		nr_async_event_cmds;
181 	struct list_head	async_events;
182 	struct work_struct	async_event_work;
183 
184 	struct list_head	subsys_entry;
185 	struct kref		ref;
186 	struct delayed_work	ka_work;
187 	struct work_struct	fatal_err_work;
188 
189 	const struct nvmet_fabrics_ops *ops;
190 
191 	__le32			*changed_ns_list;
192 	u32			nr_changed_ns;
193 
194 	char			subsysnqn[NVMF_NQN_FIELD_LEN];
195 	char			hostnqn[NVMF_NQN_FIELD_LEN];
196 
197 	struct device *p2p_client;
198 	struct radix_tree_root p2p_ns_map;
199 };
200 
201 struct nvmet_subsys {
202 	enum nvme_subsys_type	type;
203 
204 	struct mutex		lock;
205 	struct kref		ref;
206 
207 	struct list_head	namespaces;
208 	unsigned int		nr_namespaces;
209 	unsigned int		max_nsid;
210 
211 	struct list_head	ctrls;
212 
213 	struct list_head	hosts;
214 	bool			allow_any_host;
215 
216 	u16			max_qid;
217 
218 	u64			ver;
219 	u64			serial;
220 	char			*subsysnqn;
221 
222 	struct config_group	group;
223 
224 	struct config_group	namespaces_group;
225 	struct config_group	allowed_hosts_group;
226 };
227 
228 static inline struct nvmet_subsys *to_subsys(struct config_item *item)
229 {
230 	return container_of(to_config_group(item), struct nvmet_subsys, group);
231 }
232 
233 static inline struct nvmet_subsys *namespaces_to_subsys(
234 		struct config_item *item)
235 {
236 	return container_of(to_config_group(item), struct nvmet_subsys,
237 			namespaces_group);
238 }
239 
240 struct nvmet_host {
241 	struct config_group	group;
242 };
243 
244 static inline struct nvmet_host *to_host(struct config_item *item)
245 {
246 	return container_of(to_config_group(item), struct nvmet_host, group);
247 }
248 
249 static inline char *nvmet_host_name(struct nvmet_host *host)
250 {
251 	return config_item_name(&host->group.cg_item);
252 }
253 
254 struct nvmet_host_link {
255 	struct list_head	entry;
256 	struct nvmet_host	*host;
257 };
258 
259 struct nvmet_subsys_link {
260 	struct list_head	entry;
261 	struct nvmet_subsys	*subsys;
262 };
263 
264 struct nvmet_req;
265 struct nvmet_fabrics_ops {
266 	struct module *owner;
267 	unsigned int type;
268 	unsigned int msdbd;
269 	bool has_keyed_sgls : 1;
270 	void (*queue_response)(struct nvmet_req *req);
271 	int (*add_port)(struct nvmet_port *port);
272 	void (*remove_port)(struct nvmet_port *port);
273 	void (*delete_ctrl)(struct nvmet_ctrl *ctrl);
274 	void (*disc_traddr)(struct nvmet_req *req,
275 			struct nvmet_port *port, char *traddr);
276 };
277 
278 #define NVMET_MAX_INLINE_BIOVEC	8
279 #define NVMET_MAX_INLINE_DATA_LEN NVMET_MAX_INLINE_BIOVEC * PAGE_SIZE
280 
281 struct nvmet_req {
282 	struct nvme_command	*cmd;
283 	struct nvme_completion	*rsp;
284 	struct nvmet_sq		*sq;
285 	struct nvmet_cq		*cq;
286 	struct nvmet_ns		*ns;
287 	struct scatterlist	*sg;
288 	struct bio_vec		inline_bvec[NVMET_MAX_INLINE_BIOVEC];
289 	union {
290 		struct {
291 			struct bio      inline_bio;
292 		} b;
293 		struct {
294 			bool			mpool_alloc;
295 			struct kiocb            iocb;
296 			struct bio_vec          *bvec;
297 			struct work_struct      work;
298 		} f;
299 	};
300 	int			sg_cnt;
301 	/* data length as parsed from the command: */
302 	size_t			data_len;
303 	/* data length as parsed from the SGL descriptor: */
304 	size_t			transfer_len;
305 
306 	struct nvmet_port	*port;
307 
308 	void (*execute)(struct nvmet_req *req);
309 	const struct nvmet_fabrics_ops *ops;
310 
311 	struct pci_dev *p2p_dev;
312 	struct device *p2p_client;
313 };
314 
315 extern struct workqueue_struct *buffered_io_wq;
316 
317 static inline void nvmet_set_status(struct nvmet_req *req, u16 status)
318 {
319 	req->rsp->status = cpu_to_le16(status << 1);
320 }
321 
322 static inline void nvmet_set_result(struct nvmet_req *req, u32 result)
323 {
324 	req->rsp->result.u32 = cpu_to_le32(result);
325 }
326 
327 /*
328  * NVMe command writes actually are DMA reads for us on the target side.
329  */
330 static inline enum dma_data_direction
331 nvmet_data_dir(struct nvmet_req *req)
332 {
333 	return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
334 }
335 
336 struct nvmet_async_event {
337 	struct list_head	entry;
338 	u8			event_type;
339 	u8			event_info;
340 	u8			log_page;
341 };
342 
343 u16 nvmet_parse_connect_cmd(struct nvmet_req *req);
344 u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req);
345 u16 nvmet_file_parse_io_cmd(struct nvmet_req *req);
346 u16 nvmet_parse_admin_cmd(struct nvmet_req *req);
347 u16 nvmet_parse_discovery_cmd(struct nvmet_req *req);
348 u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req);
349 
350 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
351 		struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops);
352 void nvmet_req_uninit(struct nvmet_req *req);
353 void nvmet_req_execute(struct nvmet_req *req);
354 void nvmet_req_complete(struct nvmet_req *req, u16 status);
355 int nvmet_req_alloc_sgl(struct nvmet_req *req);
356 void nvmet_req_free_sgl(struct nvmet_req *req);
357 
358 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid,
359 		u16 size);
360 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid,
361 		u16 size);
362 void nvmet_sq_destroy(struct nvmet_sq *sq);
363 int nvmet_sq_init(struct nvmet_sq *sq);
364 
365 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl);
366 
367 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new);
368 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
369 		struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp);
370 u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
371 		struct nvmet_req *req, struct nvmet_ctrl **ret);
372 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl);
373 u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd);
374 
375 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
376 		enum nvme_subsys_type type);
377 void nvmet_subsys_put(struct nvmet_subsys *subsys);
378 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys);
379 
380 struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid);
381 void nvmet_put_namespace(struct nvmet_ns *ns);
382 int nvmet_ns_enable(struct nvmet_ns *ns);
383 void nvmet_ns_disable(struct nvmet_ns *ns);
384 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
385 void nvmet_ns_free(struct nvmet_ns *ns);
386 
387 void nvmet_send_ana_event(struct nvmet_subsys *subsys,
388 		struct nvmet_port *port);
389 void nvmet_port_send_ana_event(struct nvmet_port *port);
390 
391 int nvmet_register_transport(const struct nvmet_fabrics_ops *ops);
392 void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops);
393 
394 int nvmet_enable_port(struct nvmet_port *port);
395 void nvmet_disable_port(struct nvmet_port *port);
396 
397 void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port);
398 void nvmet_referral_disable(struct nvmet_port *port);
399 
400 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
401 		size_t len);
402 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf,
403 		size_t len);
404 u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len);
405 
406 u32 nvmet_get_log_page_len(struct nvme_command *cmd);
407 
408 #define NVMET_QUEUE_SIZE	1024
409 #define NVMET_NR_QUEUES		128
410 #define NVMET_MAX_CMD		NVMET_QUEUE_SIZE
411 
412 /*
413  * Nice round number that makes a list of nsids fit into a page.
414  * Should become tunable at some point in the future.
415  */
416 #define NVMET_MAX_NAMESPACES	1024
417 
418 /*
419  * 0 is not a valid ANA group ID, so we start numbering at 1.
420  *
421  * ANA Group 1 exists without manual intervention, has namespaces assigned to it
422  * by default, and is available in an optimized state through all ports.
423  */
424 #define NVMET_MAX_ANAGRPS	128
425 #define NVMET_DEFAULT_ANA_GRPID	1
426 
427 #define NVMET_KAS		10
428 #define NVMET_DISC_KATO		120
429 
430 int __init nvmet_init_configfs(void);
431 void __exit nvmet_exit_configfs(void);
432 
433 int __init nvmet_init_discovery(void);
434 void nvmet_exit_discovery(void);
435 
436 extern struct nvmet_subsys *nvmet_disc_subsys;
437 extern u64 nvmet_genctr;
438 extern struct rw_semaphore nvmet_config_sem;
439 
440 extern u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
441 extern u64 nvmet_ana_chgcnt;
442 extern struct rw_semaphore nvmet_ana_sem;
443 
444 bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys,
445 		const char *hostnqn);
446 
447 int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
448 int nvmet_file_ns_enable(struct nvmet_ns *ns);
449 void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
450 void nvmet_file_ns_disable(struct nvmet_ns *ns);
451 u16 nvmet_bdev_flush(struct nvmet_req *req);
452 u16 nvmet_file_flush(struct nvmet_req *req);
453 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
454 
455 static inline u32 nvmet_rw_len(struct nvmet_req *req)
456 {
457 	return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) <<
458 			req->ns->blksize_shift;
459 }
460 #endif /* _NVMET_H */
461