xref: /openbmc/linux/drivers/block/rnbd/rnbd-clt.h (revision 208012f0)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * RDMA Network Block Driver
4  *
5  * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
6  * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
7  * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
8  */
9 
10 #ifndef RNBD_CLT_H
11 #define RNBD_CLT_H
12 
13 #include <linux/wait.h>
14 #include <linux/in.h>
15 #include <linux/inet.h>
16 #include <linux/blk-mq.h>
17 #include <linux/refcount.h>
18 
19 #include <rtrs.h>
20 #include "rnbd-proto.h"
21 #include "rnbd-log.h"
22 
23 /* Max. number of segments per IO request, Mellanox Connect X ~ Connect X5,
24  * choose minimial 30 for all, minus 1 for internal protocol, so 29.
25  */
26 #define BMAX_SEGMENTS 29
27 /*  time in seconds between reconnect tries, default to 30 s */
28 #define RECONNECT_DELAY 30
29 /*
30  * Number of times to reconnect on error before giving up, 0 for * disabled,
31  * -1 for forever
32  */
33 #define MAX_RECONNECTS -1
34 
35 enum rnbd_clt_dev_state {
36 	DEV_STATE_INIT,
37 	DEV_STATE_MAPPED,
38 	DEV_STATE_MAPPED_DISCONNECTED,
39 	DEV_STATE_UNMAPPED,
40 };
41 
42 struct rnbd_iu_comp {
43 	wait_queue_head_t wait;
44 	int errno;
45 };
46 
47 #ifdef CONFIG_ARCH_NO_SG_CHAIN
48 #define RNBD_INLINE_SG_CNT 0
49 #else
50 #define RNBD_INLINE_SG_CNT 2
51 #endif
52 #define RNBD_RDMA_SGL_SIZE (sizeof(struct scatterlist) * RNBD_INLINE_SG_CNT)
53 
54 struct rnbd_iu {
55 	union {
56 		struct request *rq; /* for block io */
57 		void *buf; /* for user messages */
58 	};
59 	struct rtrs_permit	*permit;
60 	union {
61 		/* use to send msg associated with a dev */
62 		struct rnbd_clt_dev *dev;
63 		/* use to send msg associated with a sess */
64 		struct rnbd_clt_session *sess;
65 	};
66 	struct sg_table		sgt;
67 	struct work_struct	work;
68 	int			errno;
69 	struct rnbd_iu_comp	comp;
70 	atomic_t		refcount;
71 	struct scatterlist	first_sgl[]; /* must be the last one */
72 };
73 
74 struct rnbd_cpu_qlist {
75 	struct list_head	requeue_list;
76 	spinlock_t		requeue_lock;
77 	unsigned int		cpu;
78 };
79 
80 struct rnbd_clt_session {
81 	struct list_head        list;
82 	struct rtrs_clt        *rtrs;
83 	wait_queue_head_t       rtrs_waitq;
84 	bool                    rtrs_ready;
85 	struct rnbd_cpu_qlist	__percpu
86 				*cpu_queues;
87 	DECLARE_BITMAP(cpu_queues_bm, NR_CPUS);
88 	int	__percpu	*cpu_rr; /* per-cpu var for CPU round-robin */
89 	atomic_t		busy;
90 	size_t			queue_depth;
91 	u32			max_io_size;
92 	struct blk_mq_tag_set	tag_set;
93 	u32			nr_poll_queues;
94 	struct mutex		lock; /* protects state and devs_list */
95 	struct list_head        devs_list; /* list of struct rnbd_clt_dev */
96 	refcount_t		refcount;
97 	char			sessname[NAME_MAX];
98 	u8			ver; /* protocol version */
99 };
100 
101 /**
102  * Submission queues.
103  */
104 struct rnbd_queue {
105 	struct list_head	requeue_list;
106 	unsigned long		in_list;
107 	struct rnbd_clt_dev	*dev;
108 	struct blk_mq_hw_ctx	*hctx;
109 };
110 
111 struct rnbd_clt_dev {
112 	struct rnbd_clt_session	*sess;
113 	struct request_queue	*queue;
114 	struct rnbd_queue	*hw_queues;
115 	u32			device_id;
116 	/* local Idr index - used to track minor number allocations. */
117 	u32			clt_device_id;
118 	struct mutex		lock;
119 	enum rnbd_clt_dev_state	dev_state;
120 	char			*pathname;
121 	enum rnbd_access_mode	access_mode;
122 	u32			nr_poll_queues;
123 	bool			read_only;
124 	bool			rotational;
125 	bool			wc;
126 	bool			fua;
127 	u32			max_hw_sectors;
128 	u32			max_write_same_sectors;
129 	u32			max_discard_sectors;
130 	u32			discard_granularity;
131 	u32			discard_alignment;
132 	u16			secure_discard;
133 	u16			physical_block_size;
134 	u16			logical_block_size;
135 	u16			max_segments;
136 	size_t			nsectors;
137 	u64			size;		/* device size in bytes */
138 	struct list_head        list;
139 	struct gendisk		*gd;
140 	struct kobject		kobj;
141 	char			*blk_symlink_name;
142 	refcount_t		refcount;
143 	struct work_struct	unmap_on_rmmod_work;
144 };
145 
146 /* rnbd-clt.c */
147 
148 struct rnbd_clt_dev *rnbd_clt_map_device(const char *sessname,
149 					   struct rtrs_addr *paths,
150 					   size_t path_cnt, u16 port_nr,
151 					   const char *pathname,
152 					   enum rnbd_access_mode access_mode,
153 					   u32 nr_poll_queues);
154 int rnbd_clt_unmap_device(struct rnbd_clt_dev *dev, bool force,
155 			   const struct attribute *sysfs_self);
156 
157 int rnbd_clt_remap_device(struct rnbd_clt_dev *dev);
158 int rnbd_clt_resize_disk(struct rnbd_clt_dev *dev, size_t newsize);
159 
160 /* rnbd-clt-sysfs.c */
161 
162 int rnbd_clt_create_sysfs_files(void);
163 
164 void rnbd_clt_destroy_sysfs_files(void);
165 
166 void rnbd_clt_remove_dev_symlink(struct rnbd_clt_dev *dev);
167 
168 #endif /* RNBD_CLT_H */
169