xref: /openbmc/linux/drivers/nvme/target/nvmet.h (revision f9362ac1)
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 	bool			cmd_seen;
167 
168 	struct mutex		lock;
169 	u64			cap;
170 	u32			cc;
171 	u32			csts;
172 
173 	uuid_t			hostid;
174 	u16			cntlid;
175 	u32			kato;
176 
177 	struct nvmet_port	*port;
178 
179 	u32			aen_enabled;
180 	unsigned long		aen_masked;
181 	struct nvmet_req	*async_event_cmds[NVMET_ASYNC_EVENTS];
182 	unsigned int		nr_async_event_cmds;
183 	struct list_head	async_events;
184 	struct work_struct	async_event_work;
185 
186 	struct list_head	subsys_entry;
187 	struct kref		ref;
188 	struct delayed_work	ka_work;
189 	struct work_struct	fatal_err_work;
190 
191 	const struct nvmet_fabrics_ops *ops;
192 
193 	__le32			*changed_ns_list;
194 	u32			nr_changed_ns;
195 
196 	char			subsysnqn[NVMF_NQN_FIELD_LEN];
197 	char			hostnqn[NVMF_NQN_FIELD_LEN];
198 
199 	struct device *p2p_client;
200 	struct radix_tree_root p2p_ns_map;
201 };
202 
203 struct nvmet_subsys {
204 	enum nvme_subsys_type	type;
205 
206 	struct mutex		lock;
207 	struct kref		ref;
208 
209 	struct list_head	namespaces;
210 	unsigned int		nr_namespaces;
211 	unsigned int		max_nsid;
212 
213 	struct list_head	ctrls;
214 
215 	struct list_head	hosts;
216 	bool			allow_any_host;
217 
218 	u16			max_qid;
219 
220 	u64			ver;
221 	u64			serial;
222 	char			*subsysnqn;
223 
224 	struct config_group	group;
225 
226 	struct config_group	namespaces_group;
227 	struct config_group	allowed_hosts_group;
228 };
229 
230 static inline struct nvmet_subsys *to_subsys(struct config_item *item)
231 {
232 	return container_of(to_config_group(item), struct nvmet_subsys, group);
233 }
234 
235 static inline struct nvmet_subsys *namespaces_to_subsys(
236 		struct config_item *item)
237 {
238 	return container_of(to_config_group(item), struct nvmet_subsys,
239 			namespaces_group);
240 }
241 
242 struct nvmet_host {
243 	struct config_group	group;
244 };
245 
246 static inline struct nvmet_host *to_host(struct config_item *item)
247 {
248 	return container_of(to_config_group(item), struct nvmet_host, group);
249 }
250 
251 static inline char *nvmet_host_name(struct nvmet_host *host)
252 {
253 	return config_item_name(&host->group.cg_item);
254 }
255 
256 struct nvmet_host_link {
257 	struct list_head	entry;
258 	struct nvmet_host	*host;
259 };
260 
261 struct nvmet_subsys_link {
262 	struct list_head	entry;
263 	struct nvmet_subsys	*subsys;
264 };
265 
266 struct nvmet_req;
267 struct nvmet_fabrics_ops {
268 	struct module *owner;
269 	unsigned int type;
270 	unsigned int msdbd;
271 	bool has_keyed_sgls : 1;
272 	void (*queue_response)(struct nvmet_req *req);
273 	int (*add_port)(struct nvmet_port *port);
274 	void (*remove_port)(struct nvmet_port *port);
275 	void (*delete_ctrl)(struct nvmet_ctrl *ctrl);
276 	void (*disc_traddr)(struct nvmet_req *req,
277 			struct nvmet_port *port, char *traddr);
278 };
279 
280 #define NVMET_MAX_INLINE_BIOVEC	8
281 #define NVMET_MAX_INLINE_DATA_LEN NVMET_MAX_INLINE_BIOVEC * PAGE_SIZE
282 
283 struct nvmet_req {
284 	struct nvme_command	*cmd;
285 	struct nvme_completion	*rsp;
286 	struct nvmet_sq		*sq;
287 	struct nvmet_cq		*cq;
288 	struct nvmet_ns		*ns;
289 	struct scatterlist	*sg;
290 	struct bio_vec		inline_bvec[NVMET_MAX_INLINE_BIOVEC];
291 	union {
292 		struct {
293 			struct bio      inline_bio;
294 		} b;
295 		struct {
296 			bool			mpool_alloc;
297 			struct kiocb            iocb;
298 			struct bio_vec          *bvec;
299 			struct work_struct      work;
300 		} f;
301 	};
302 	int			sg_cnt;
303 	/* data length as parsed from the command: */
304 	size_t			data_len;
305 	/* data length as parsed from the SGL descriptor: */
306 	size_t			transfer_len;
307 
308 	struct nvmet_port	*port;
309 
310 	void (*execute)(struct nvmet_req *req);
311 	const struct nvmet_fabrics_ops *ops;
312 
313 	struct pci_dev *p2p_dev;
314 	struct device *p2p_client;
315 };
316 
317 extern struct workqueue_struct *buffered_io_wq;
318 
319 static inline void nvmet_set_status(struct nvmet_req *req, u16 status)
320 {
321 	req->rsp->status = cpu_to_le16(status << 1);
322 }
323 
324 static inline void nvmet_set_result(struct nvmet_req *req, u32 result)
325 {
326 	req->rsp->result.u32 = cpu_to_le32(result);
327 }
328 
329 /*
330  * NVMe command writes actually are DMA reads for us on the target side.
331  */
332 static inline enum dma_data_direction
333 nvmet_data_dir(struct nvmet_req *req)
334 {
335 	return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
336 }
337 
338 struct nvmet_async_event {
339 	struct list_head	entry;
340 	u8			event_type;
341 	u8			event_info;
342 	u8			log_page;
343 };
344 
345 static inline void nvmet_clear_aen_bit(struct nvmet_req *req, u32 bn)
346 {
347 	int rae = le32_to_cpu(req->cmd->common.cdw10[0]) & 1 << 15;
348 
349 	if (!rae)
350 		clear_bit(bn, &req->sq->ctrl->aen_masked);
351 }
352 
353 static inline bool nvmet_aen_bit_disabled(struct nvmet_ctrl *ctrl, u32 bn)
354 {
355 	if (!(READ_ONCE(ctrl->aen_enabled) & (1 << bn)))
356 		return true;
357 	return test_and_set_bit(bn, &ctrl->aen_masked);
358 }
359 
360 u16 nvmet_parse_connect_cmd(struct nvmet_req *req);
361 u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req);
362 u16 nvmet_file_parse_io_cmd(struct nvmet_req *req);
363 u16 nvmet_parse_admin_cmd(struct nvmet_req *req);
364 u16 nvmet_parse_discovery_cmd(struct nvmet_req *req);
365 u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req);
366 
367 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
368 		struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops);
369 void nvmet_req_uninit(struct nvmet_req *req);
370 void nvmet_req_execute(struct nvmet_req *req);
371 void nvmet_req_complete(struct nvmet_req *req, u16 status);
372 int nvmet_req_alloc_sgl(struct nvmet_req *req);
373 void nvmet_req_free_sgl(struct nvmet_req *req);
374 
375 void nvmet_execute_keep_alive(struct nvmet_req *req);
376 
377 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid,
378 		u16 size);
379 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid,
380 		u16 size);
381 void nvmet_sq_destroy(struct nvmet_sq *sq);
382 int nvmet_sq_init(struct nvmet_sq *sq);
383 
384 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl);
385 
386 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new);
387 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
388 		struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp);
389 u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
390 		struct nvmet_req *req, struct nvmet_ctrl **ret);
391 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl);
392 u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd);
393 
394 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
395 		enum nvme_subsys_type type);
396 void nvmet_subsys_put(struct nvmet_subsys *subsys);
397 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys);
398 
399 struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid);
400 void nvmet_put_namespace(struct nvmet_ns *ns);
401 int nvmet_ns_enable(struct nvmet_ns *ns);
402 void nvmet_ns_disable(struct nvmet_ns *ns);
403 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
404 void nvmet_ns_free(struct nvmet_ns *ns);
405 
406 void nvmet_send_ana_event(struct nvmet_subsys *subsys,
407 		struct nvmet_port *port);
408 void nvmet_port_send_ana_event(struct nvmet_port *port);
409 
410 int nvmet_register_transport(const struct nvmet_fabrics_ops *ops);
411 void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops);
412 
413 int nvmet_enable_port(struct nvmet_port *port);
414 void nvmet_disable_port(struct nvmet_port *port);
415 
416 void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port);
417 void nvmet_referral_disable(struct nvmet_port *port);
418 
419 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
420 		size_t len);
421 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf,
422 		size_t len);
423 u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len);
424 
425 u32 nvmet_get_log_page_len(struct nvme_command *cmd);
426 
427 #define NVMET_QUEUE_SIZE	1024
428 #define NVMET_NR_QUEUES		128
429 #define NVMET_MAX_CMD		NVMET_QUEUE_SIZE
430 
431 /*
432  * Nice round number that makes a list of nsids fit into a page.
433  * Should become tunable at some point in the future.
434  */
435 #define NVMET_MAX_NAMESPACES	1024
436 
437 /*
438  * 0 is not a valid ANA group ID, so we start numbering at 1.
439  *
440  * ANA Group 1 exists without manual intervention, has namespaces assigned to it
441  * by default, and is available in an optimized state through all ports.
442  */
443 #define NVMET_MAX_ANAGRPS	128
444 #define NVMET_DEFAULT_ANA_GRPID	1
445 
446 #define NVMET_KAS		10
447 #define NVMET_DISC_KATO_MS		120000
448 
449 int __init nvmet_init_configfs(void);
450 void __exit nvmet_exit_configfs(void);
451 
452 int __init nvmet_init_discovery(void);
453 void nvmet_exit_discovery(void);
454 
455 extern struct nvmet_subsys *nvmet_disc_subsys;
456 extern u64 nvmet_genctr;
457 extern struct rw_semaphore nvmet_config_sem;
458 
459 extern u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
460 extern u64 nvmet_ana_chgcnt;
461 extern struct rw_semaphore nvmet_ana_sem;
462 
463 bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys,
464 		const char *hostnqn);
465 
466 int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
467 int nvmet_file_ns_enable(struct nvmet_ns *ns);
468 void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
469 void nvmet_file_ns_disable(struct nvmet_ns *ns);
470 u16 nvmet_bdev_flush(struct nvmet_req *req);
471 u16 nvmet_file_flush(struct nvmet_req *req);
472 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
473 
474 static inline u32 nvmet_rw_len(struct nvmet_req *req)
475 {
476 	return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) <<
477 			req->ns->blksize_shift;
478 }
479 #endif /* _NVMET_H */
480