xref: /openbmc/linux/drivers/nvme/target/nvmet.h (revision 136cc1ff)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2015-2016 HGST, a Western Digital Company.
4  */
5 
6 #ifndef _NVMET_H
7 #define _NVMET_H
8 
9 #include <linux/dma-mapping.h>
10 #include <linux/types.h>
11 #include <linux/device.h>
12 #include <linux/kref.h>
13 #include <linux/percpu-refcount.h>
14 #include <linux/list.h>
15 #include <linux/mutex.h>
16 #include <linux/uuid.h>
17 #include <linux/nvme.h>
18 #include <linux/configfs.h>
19 #include <linux/rcupdate.h>
20 #include <linux/blkdev.h>
21 #include <linux/radix-tree.h>
22 #include <linux/t10-pi.h>
23 
24 #define NVMET_ASYNC_EVENTS		4
25 #define NVMET_ERROR_LOG_SLOTS		128
26 #define NVMET_NO_ERROR_LOC		((u16)-1)
27 #define NVMET_DEFAULT_CTRL_MODEL	"Linux"
28 
29 /*
30  * Supported optional AENs:
31  */
32 #define NVMET_AEN_CFG_OPTIONAL \
33 	(NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_ANA_CHANGE)
34 #define NVMET_DISC_AEN_CFG_OPTIONAL \
35 	(NVME_AEN_CFG_DISC_CHANGE)
36 
37 /*
38  * Plus mandatory SMART AENs (we'll never send them, but allow enabling them):
39  */
40 #define NVMET_AEN_CFG_ALL \
41 	(NVME_SMART_CRIT_SPARE | NVME_SMART_CRIT_TEMPERATURE | \
42 	 NVME_SMART_CRIT_RELIABILITY | NVME_SMART_CRIT_MEDIA | \
43 	 NVME_SMART_CRIT_VOLATILE_MEMORY | NVMET_AEN_CFG_OPTIONAL)
44 
45 /* Helper Macros when NVMe error is NVME_SC_CONNECT_INVALID_PARAM
46  * The 16 bit shift is to set IATTR bit to 1, which means offending
47  * offset starts in the data section of connect()
48  */
49 #define IPO_IATTR_CONNECT_DATA(x)	\
50 	(cpu_to_le32((1 << 16) | (offsetof(struct nvmf_connect_data, x))))
51 #define IPO_IATTR_CONNECT_SQE(x)	\
52 	(cpu_to_le32(offsetof(struct nvmf_connect_command, x)))
53 
54 struct nvmet_ns {
55 	struct list_head	dev_link;
56 	struct percpu_ref	ref;
57 	struct block_device	*bdev;
58 	struct file		*file;
59 	bool			readonly;
60 	u32			nsid;
61 	u32			blksize_shift;
62 	loff_t			size;
63 	u8			nguid[16];
64 	uuid_t			uuid;
65 	u32			anagrpid;
66 
67 	bool			buffered_io;
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 	int			use_p2pmem;
80 	struct pci_dev		*p2p_dev;
81 	int			pi_type;
82 	int			metadata_size;
83 };
84 
85 static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
86 {
87 	return container_of(to_config_group(item), struct nvmet_ns, group);
88 }
89 
90 static inline struct device *nvmet_ns_dev(struct nvmet_ns *ns)
91 {
92 	return ns->bdev ? disk_to_dev(ns->bdev->bd_disk) : NULL;
93 }
94 
95 struct nvmet_cq {
96 	u16			qid;
97 	u16			size;
98 };
99 
100 struct nvmet_sq {
101 	struct nvmet_ctrl	*ctrl;
102 	struct percpu_ref	ref;
103 	u16			qid;
104 	u16			size;
105 	u32			sqhd;
106 	bool			sqhd_disabled;
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 list_head		global_entry;
141 	struct config_group		ana_groups_group;
142 	struct nvmet_ana_group		ana_default_group;
143 	enum nvme_ana_state		*ana_state;
144 	void				*priv;
145 	bool				enabled;
146 	int				inline_data_size;
147 	const struct nvmet_fabrics_ops	*tr_ops;
148 };
149 
150 static inline struct nvmet_port *to_nvmet_port(struct config_item *item)
151 {
152 	return container_of(to_config_group(item), struct nvmet_port,
153 			group);
154 }
155 
156 static inline struct nvmet_port *ana_groups_to_port(
157 		struct config_item *item)
158 {
159 	return container_of(to_config_group(item), struct nvmet_port,
160 			ana_groups_group);
161 }
162 
163 struct nvmet_ctrl {
164 	struct nvmet_subsys	*subsys;
165 	struct nvmet_cq		**cqs;
166 	struct nvmet_sq		**sqs;
167 
168 	bool			cmd_seen;
169 
170 	struct mutex		lock;
171 	u64			cap;
172 	u32			cc;
173 	u32			csts;
174 
175 	uuid_t			hostid;
176 	u16			cntlid;
177 	u32			kato;
178 
179 	struct nvmet_port	*port;
180 
181 	u32			aen_enabled;
182 	unsigned long		aen_masked;
183 	struct nvmet_req	*async_event_cmds[NVMET_ASYNC_EVENTS];
184 	unsigned int		nr_async_event_cmds;
185 	struct list_head	async_events;
186 	struct work_struct	async_event_work;
187 
188 	struct list_head	subsys_entry;
189 	struct kref		ref;
190 	struct delayed_work	ka_work;
191 	struct work_struct	fatal_err_work;
192 
193 	const struct nvmet_fabrics_ops *ops;
194 
195 	__le32			*changed_ns_list;
196 	u32			nr_changed_ns;
197 
198 	char			subsysnqn[NVMF_NQN_FIELD_LEN];
199 	char			hostnqn[NVMF_NQN_FIELD_LEN];
200 
201 	struct device		*p2p_client;
202 	struct radix_tree_root	p2p_ns_map;
203 
204 	spinlock_t		error_lock;
205 	u64			err_counter;
206 	struct nvme_error_slot	slots[NVMET_ERROR_LOG_SLOTS];
207 };
208 
209 struct nvmet_subsys_model {
210 	struct rcu_head		rcuhead;
211 	char			number[];
212 };
213 
214 struct nvmet_subsys {
215 	enum nvme_subsys_type	type;
216 
217 	struct mutex		lock;
218 	struct kref		ref;
219 
220 	struct list_head	namespaces;
221 	unsigned int		nr_namespaces;
222 	unsigned int		max_nsid;
223 	u16			cntlid_min;
224 	u16			cntlid_max;
225 
226 	struct list_head	ctrls;
227 
228 	struct list_head	hosts;
229 	bool			allow_any_host;
230 
231 	u16			max_qid;
232 
233 	u64			ver;
234 	u64			serial;
235 	char			*subsysnqn;
236 
237 	struct config_group	group;
238 
239 	struct config_group	namespaces_group;
240 	struct config_group	allowed_hosts_group;
241 
242 	struct nvmet_subsys_model	__rcu *model;
243 };
244 
245 static inline struct nvmet_subsys *to_subsys(struct config_item *item)
246 {
247 	return container_of(to_config_group(item), struct nvmet_subsys, group);
248 }
249 
250 static inline struct nvmet_subsys *namespaces_to_subsys(
251 		struct config_item *item)
252 {
253 	return container_of(to_config_group(item), struct nvmet_subsys,
254 			namespaces_group);
255 }
256 
257 struct nvmet_host {
258 	struct config_group	group;
259 };
260 
261 static inline struct nvmet_host *to_host(struct config_item *item)
262 {
263 	return container_of(to_config_group(item), struct nvmet_host, group);
264 }
265 
266 static inline char *nvmet_host_name(struct nvmet_host *host)
267 {
268 	return config_item_name(&host->group.cg_item);
269 }
270 
271 struct nvmet_host_link {
272 	struct list_head	entry;
273 	struct nvmet_host	*host;
274 };
275 
276 struct nvmet_subsys_link {
277 	struct list_head	entry;
278 	struct nvmet_subsys	*subsys;
279 };
280 
281 struct nvmet_req;
282 struct nvmet_fabrics_ops {
283 	struct module *owner;
284 	unsigned int type;
285 	unsigned int msdbd;
286 	bool has_keyed_sgls : 1;
287 	void (*queue_response)(struct nvmet_req *req);
288 	int (*add_port)(struct nvmet_port *port);
289 	void (*remove_port)(struct nvmet_port *port);
290 	void (*delete_ctrl)(struct nvmet_ctrl *ctrl);
291 	void (*disc_traddr)(struct nvmet_req *req,
292 			struct nvmet_port *port, char *traddr);
293 	u16 (*install_queue)(struct nvmet_sq *nvme_sq);
294 	void (*discovery_chg)(struct nvmet_port *port);
295 	u8 (*get_mdts)(const struct nvmet_ctrl *ctrl);
296 };
297 
298 #define NVMET_MAX_INLINE_BIOVEC	8
299 #define NVMET_MAX_INLINE_DATA_LEN NVMET_MAX_INLINE_BIOVEC * PAGE_SIZE
300 
301 struct nvmet_req {
302 	struct nvme_command	*cmd;
303 	struct nvme_completion	*cqe;
304 	struct nvmet_sq		*sq;
305 	struct nvmet_cq		*cq;
306 	struct nvmet_ns		*ns;
307 	struct scatterlist	*sg;
308 	struct bio_vec		inline_bvec[NVMET_MAX_INLINE_BIOVEC];
309 	union {
310 		struct {
311 			struct bio      inline_bio;
312 		} b;
313 		struct {
314 			bool			mpool_alloc;
315 			struct kiocb            iocb;
316 			struct bio_vec          *bvec;
317 			struct work_struct      work;
318 		} f;
319 	};
320 	int			sg_cnt;
321 	/* data length as parsed from the SGL descriptor: */
322 	size_t			transfer_len;
323 
324 	struct nvmet_port	*port;
325 
326 	void (*execute)(struct nvmet_req *req);
327 	const struct nvmet_fabrics_ops *ops;
328 
329 	struct pci_dev		*p2p_dev;
330 	struct device		*p2p_client;
331 	u16			error_loc;
332 	u64			error_slba;
333 };
334 
335 extern struct workqueue_struct *buffered_io_wq;
336 
337 static inline void nvmet_set_result(struct nvmet_req *req, u32 result)
338 {
339 	req->cqe->result.u32 = cpu_to_le32(result);
340 }
341 
342 /*
343  * NVMe command writes actually are DMA reads for us on the target side.
344  */
345 static inline enum dma_data_direction
346 nvmet_data_dir(struct nvmet_req *req)
347 {
348 	return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
349 }
350 
351 struct nvmet_async_event {
352 	struct list_head	entry;
353 	u8			event_type;
354 	u8			event_info;
355 	u8			log_page;
356 };
357 
358 static inline void nvmet_clear_aen_bit(struct nvmet_req *req, u32 bn)
359 {
360 	int rae = le32_to_cpu(req->cmd->common.cdw10) & 1 << 15;
361 
362 	if (!rae)
363 		clear_bit(bn, &req->sq->ctrl->aen_masked);
364 }
365 
366 static inline bool nvmet_aen_bit_disabled(struct nvmet_ctrl *ctrl, u32 bn)
367 {
368 	if (!(READ_ONCE(ctrl->aen_enabled) & (1 << bn)))
369 		return true;
370 	return test_and_set_bit(bn, &ctrl->aen_masked);
371 }
372 
373 void nvmet_get_feat_kato(struct nvmet_req *req);
374 void nvmet_get_feat_async_event(struct nvmet_req *req);
375 u16 nvmet_set_feat_kato(struct nvmet_req *req);
376 u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask);
377 void nvmet_execute_async_event(struct nvmet_req *req);
378 
379 u16 nvmet_parse_connect_cmd(struct nvmet_req *req);
380 void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id);
381 u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req);
382 u16 nvmet_file_parse_io_cmd(struct nvmet_req *req);
383 u16 nvmet_parse_admin_cmd(struct nvmet_req *req);
384 u16 nvmet_parse_discovery_cmd(struct nvmet_req *req);
385 u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req);
386 
387 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
388 		struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops);
389 void nvmet_req_uninit(struct nvmet_req *req);
390 bool nvmet_check_transfer_len(struct nvmet_req *req, size_t len);
391 bool nvmet_check_data_len_lte(struct nvmet_req *req, size_t data_len);
392 void nvmet_req_complete(struct nvmet_req *req, u16 status);
393 int nvmet_req_alloc_sgl(struct nvmet_req *req);
394 void nvmet_req_free_sgl(struct nvmet_req *req);
395 
396 void nvmet_execute_keep_alive(struct nvmet_req *req);
397 
398 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid,
399 		u16 size);
400 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid,
401 		u16 size);
402 void nvmet_sq_destroy(struct nvmet_sq *sq);
403 int nvmet_sq_init(struct nvmet_sq *sq);
404 
405 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl);
406 
407 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new);
408 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
409 		struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp);
410 u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
411 		struct nvmet_req *req, struct nvmet_ctrl **ret);
412 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl);
413 u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd);
414 
415 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
416 		enum nvme_subsys_type type);
417 void nvmet_subsys_put(struct nvmet_subsys *subsys);
418 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys);
419 
420 struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid);
421 void nvmet_put_namespace(struct nvmet_ns *ns);
422 int nvmet_ns_enable(struct nvmet_ns *ns);
423 void nvmet_ns_disable(struct nvmet_ns *ns);
424 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
425 void nvmet_ns_free(struct nvmet_ns *ns);
426 
427 void nvmet_send_ana_event(struct nvmet_subsys *subsys,
428 		struct nvmet_port *port);
429 void nvmet_port_send_ana_event(struct nvmet_port *port);
430 
431 int nvmet_register_transport(const struct nvmet_fabrics_ops *ops);
432 void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops);
433 
434 void nvmet_port_del_ctrls(struct nvmet_port *port,
435 			  struct nvmet_subsys *subsys);
436 
437 int nvmet_enable_port(struct nvmet_port *port);
438 void nvmet_disable_port(struct nvmet_port *port);
439 
440 void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port);
441 void nvmet_referral_disable(struct nvmet_port *parent, struct nvmet_port *port);
442 
443 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
444 		size_t len);
445 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf,
446 		size_t len);
447 u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len);
448 
449 u32 nvmet_get_log_page_len(struct nvme_command *cmd);
450 u64 nvmet_get_log_page_offset(struct nvme_command *cmd);
451 
452 extern struct list_head *nvmet_ports;
453 void nvmet_port_disc_changed(struct nvmet_port *port,
454 		struct nvmet_subsys *subsys);
455 void nvmet_subsys_disc_changed(struct nvmet_subsys *subsys,
456 		struct nvmet_host *host);
457 void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
458 		u8 event_info, u8 log_page);
459 
460 #define NVMET_QUEUE_SIZE	1024
461 #define NVMET_NR_QUEUES		128
462 #define NVMET_MAX_CMD		NVMET_QUEUE_SIZE
463 
464 /*
465  * Nice round number that makes a list of nsids fit into a page.
466  * Should become tunable at some point in the future.
467  */
468 #define NVMET_MAX_NAMESPACES	1024
469 
470 /*
471  * 0 is not a valid ANA group ID, so we start numbering at 1.
472  *
473  * ANA Group 1 exists without manual intervention, has namespaces assigned to it
474  * by default, and is available in an optimized state through all ports.
475  */
476 #define NVMET_MAX_ANAGRPS	128
477 #define NVMET_DEFAULT_ANA_GRPID	1
478 
479 #define NVMET_KAS		10
480 #define NVMET_DISC_KATO_MS		120000
481 
482 int __init nvmet_init_configfs(void);
483 void __exit nvmet_exit_configfs(void);
484 
485 int __init nvmet_init_discovery(void);
486 void nvmet_exit_discovery(void);
487 
488 extern struct nvmet_subsys *nvmet_disc_subsys;
489 extern struct rw_semaphore nvmet_config_sem;
490 
491 extern u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
492 extern u64 nvmet_ana_chgcnt;
493 extern struct rw_semaphore nvmet_ana_sem;
494 
495 bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn);
496 
497 int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
498 int nvmet_file_ns_enable(struct nvmet_ns *ns);
499 void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
500 void nvmet_file_ns_disable(struct nvmet_ns *ns);
501 u16 nvmet_bdev_flush(struct nvmet_req *req);
502 u16 nvmet_file_flush(struct nvmet_req *req);
503 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
504 void nvmet_bdev_ns_revalidate(struct nvmet_ns *ns);
505 int nvmet_file_ns_revalidate(struct nvmet_ns *ns);
506 void nvmet_ns_revalidate(struct nvmet_ns *ns);
507 
508 static inline u32 nvmet_rw_data_len(struct nvmet_req *req)
509 {
510 	return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) <<
511 			req->ns->blksize_shift;
512 }
513 
514 static inline u32 nvmet_dsm_len(struct nvmet_req *req)
515 {
516 	return (le32_to_cpu(req->cmd->dsm.nr) + 1) *
517 		sizeof(struct nvme_dsm_range);
518 }
519 
520 u16 errno_to_nvme_status(struct nvmet_req *req, int errno);
521 
522 /* Convert a 32-bit number to a 16-bit 0's based number */
523 static inline __le16 to0based(u32 a)
524 {
525 	return cpu_to_le16(max(1U, min(1U << 16, a)) - 1);
526 }
527 
528 #endif /* _NVMET_H */
529