xref: /linux/drivers/infiniband/ulp/rtrs/rtrs-pri.h (revision ec8a42e7343234802b9054874fe01810880289ce)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * RDMA Transport Layer
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 RTRS_PRI_H
11 #define RTRS_PRI_H
12 
13 #include <linux/uuid.h>
14 #include <rdma/rdma_cm.h>
15 #include <rdma/ib_verbs.h>
16 #include <rdma/ib.h>
17 
18 #include "rtrs.h"
19 
20 #define RTRS_PROTO_VER_MAJOR 2
21 #define RTRS_PROTO_VER_MINOR 0
22 
23 #define RTRS_PROTO_VER_STRING __stringify(RTRS_PROTO_VER_MAJOR) "." \
24 			       __stringify(RTRS_PROTO_VER_MINOR)
25 
26 enum rtrs_imm_const {
27 	MAX_IMM_TYPE_BITS = 4,
28 	MAX_IMM_TYPE_MASK = ((1 << MAX_IMM_TYPE_BITS) - 1),
29 	MAX_IMM_PAYL_BITS = 28,
30 	MAX_IMM_PAYL_MASK = ((1 << MAX_IMM_PAYL_BITS) - 1),
31 };
32 
33 enum rtrs_imm_type {
34 	RTRS_IO_REQ_IMM       = 0, /* client to server */
35 	RTRS_IO_RSP_IMM       = 1, /* server to client */
36 	RTRS_IO_RSP_W_INV_IMM = 2, /* server to client */
37 
38 	RTRS_HB_MSG_IMM = 8, /* HB: HeartBeat */
39 	RTRS_HB_ACK_IMM = 9,
40 
41 	RTRS_LAST_IMM,
42 };
43 
44 enum {
45 	SERVICE_CON_QUEUE_DEPTH = 512,
46 
47 	MAX_PATHS_NUM = 128,
48 
49 	/*
50 	 * With the size of struct rtrs_permit allocated on the client, 4K
51 	 * is the maximum number of rtrs_permits we can allocate. This number is
52 	 * also used on the client to allocate the IU for the user connection
53 	 * to receive the RDMA addresses from the server.
54 	 */
55 	MAX_SESS_QUEUE_DEPTH = 4096,
56 
57 	RTRS_HB_INTERVAL_MS = 5000,
58 	RTRS_HB_MISSED_MAX = 5,
59 
60 	RTRS_MAGIC = 0x1BBD,
61 	RTRS_PROTO_VER = (RTRS_PROTO_VER_MAJOR << 8) | RTRS_PROTO_VER_MINOR,
62 };
63 
64 struct rtrs_ib_dev;
65 
66 struct rtrs_rdma_dev_pd_ops {
67 	struct rtrs_ib_dev *(*alloc)(void);
68 	void (*free)(struct rtrs_ib_dev *dev);
69 	int (*init)(struct rtrs_ib_dev *dev);
70 	void (*deinit)(struct rtrs_ib_dev *dev);
71 };
72 
73 struct rtrs_rdma_dev_pd {
74 	struct mutex		mutex;
75 	struct list_head	list;
76 	enum ib_pd_flags	pd_flags;
77 	const struct rtrs_rdma_dev_pd_ops *ops;
78 };
79 
80 struct rtrs_ib_dev {
81 	struct ib_device	 *ib_dev;
82 	struct ib_pd		 *ib_pd;
83 	struct kref		 ref;
84 	struct list_head	 entry;
85 	struct rtrs_rdma_dev_pd *pool;
86 };
87 
88 struct rtrs_con {
89 	struct rtrs_sess	*sess;
90 	struct ib_qp		*qp;
91 	struct ib_cq		*cq;
92 	struct rdma_cm_id	*cm_id;
93 	unsigned int		cid;
94 };
95 
96 struct rtrs_sess {
97 	struct list_head	entry;
98 	struct sockaddr_storage dst_addr;
99 	struct sockaddr_storage src_addr;
100 	char			sessname[NAME_MAX];
101 	uuid_t			uuid;
102 	struct rtrs_con	**con;
103 	unsigned int		con_num;
104 	unsigned int		recon_cnt;
105 	struct rtrs_ib_dev	*dev;
106 	int			dev_ref;
107 	struct ib_cqe		*hb_cqe;
108 	void			(*hb_err_handler)(struct rtrs_con *con);
109 	struct workqueue_struct *hb_wq;
110 	struct delayed_work	hb_dwork;
111 	unsigned int		hb_interval_ms;
112 	unsigned int		hb_missed_cnt;
113 	unsigned int		hb_missed_max;
114 };
115 
116 /* rtrs information unit */
117 struct rtrs_iu {
118 	struct ib_cqe           cqe;
119 	dma_addr_t              dma_addr;
120 	void                    *buf;
121 	size_t                  size;
122 	enum dma_data_direction direction;
123 };
124 
125 /**
126  * enum rtrs_msg_types - RTRS message types, see also rtrs/README
127  * @RTRS_MSG_INFO_REQ:		Client additional info request to the server
128  * @RTRS_MSG_INFO_RSP:		Server additional info response to the client
129  * @RTRS_MSG_WRITE:		Client writes data per RDMA to server
130  * @RTRS_MSG_READ:		Client requests data transfer from server
131  * @RTRS_MSG_RKEY_RSP:		Server refreshed rkey for rbuf
132  */
133 enum rtrs_msg_types {
134 	RTRS_MSG_INFO_REQ,
135 	RTRS_MSG_INFO_RSP,
136 	RTRS_MSG_WRITE,
137 	RTRS_MSG_READ,
138 	RTRS_MSG_RKEY_RSP,
139 };
140 
141 /**
142  * enum rtrs_msg_flags - RTRS message flags.
143  * @RTRS_NEED_INVAL:	Send invalidation in response.
144  * @RTRS_MSG_NEW_RKEY_F: Send refreshed rkey in response.
145  */
146 enum rtrs_msg_flags {
147 	RTRS_MSG_NEED_INVAL_F = 1 << 0,
148 	RTRS_MSG_NEW_RKEY_F = 1 << 1,
149 };
150 
151 /**
152  * struct rtrs_sg_desc - RDMA-Buffer entry description
153  * @addr:	Address of RDMA destination buffer
154  * @key:	Authorization rkey to write to the buffer
155  * @len:	Size of the buffer
156  */
157 struct rtrs_sg_desc {
158 	__le64			addr;
159 	__le32			key;
160 	__le32			len;
161 };
162 
163 /**
164  * struct rtrs_msg_conn_req - Client connection request to the server
165  * @magic:	   RTRS magic
166  * @version:	   RTRS protocol version
167  * @cid:	   Current connection id
168  * @cid_num:	   Number of connections per session
169  * @recon_cnt:	   Reconnections counter
170  * @sess_uuid:	   UUID of a session (path)
171  * @paths_uuid:	   UUID of a group of sessions (paths)
172  *
173  * NOTE: max size 56 bytes, see man rdma_connect().
174  */
175 struct rtrs_msg_conn_req {
176 	/* Is set to 0 by cma.c in case of AF_IB, do not touch that.
177 	 * see https://www.spinics.net/lists/linux-rdma/msg22397.html
178 	 */
179 	u8		__cma_version;
180 	/* On sender side that should be set to 0, or cma_save_ip_info()
181 	 * extract garbage and will fail.
182 	 */
183 	u8		__ip_version;
184 	__le16		magic;
185 	__le16		version;
186 	__le16		cid;
187 	__le16		cid_num;
188 	__le16		recon_cnt;
189 	uuid_t		sess_uuid;
190 	uuid_t		paths_uuid;
191 	u8		reserved[12];
192 };
193 
194 /**
195  * struct rtrs_msg_conn_rsp - Server connection response to the client
196  * @magic:	   RTRS magic
197  * @version:	   RTRS protocol version
198  * @errno:	   If rdma_accept() then 0, if rdma_reject() indicates error
199  * @queue_depth:   max inflight messages (queue-depth) in this session
200  * @max_io_size:   max io size server supports
201  * @max_hdr_size:  max msg header size server supports
202  *
203  * NOTE: size is 56 bytes, max possible is 136 bytes, see man rdma_accept().
204  */
205 struct rtrs_msg_conn_rsp {
206 	__le16		magic;
207 	__le16		version;
208 	__le16		errno;
209 	__le16		queue_depth;
210 	__le32		max_io_size;
211 	__le32		max_hdr_size;
212 	__le32		flags;
213 	u8		reserved[36];
214 };
215 
216 /**
217  * struct rtrs_msg_info_req
218  * @type:		@RTRS_MSG_INFO_REQ
219  * @sessname:		Session name chosen by client
220  */
221 struct rtrs_msg_info_req {
222 	__le16		type;
223 	u8		sessname[NAME_MAX];
224 	u8		reserved[15];
225 };
226 
227 /**
228  * struct rtrs_msg_info_rsp
229  * @type:		@RTRS_MSG_INFO_RSP
230  * @sg_cnt:		Number of @desc entries
231  * @desc:		RDMA buffers where the client can write to server
232  */
233 struct rtrs_msg_info_rsp {
234 	__le16		type;
235 	__le16          sg_cnt;
236 	u8              reserved[4];
237 	struct rtrs_sg_desc desc[];
238 };
239 
240 /**
241  * struct rtrs_msg_rkey_rsp
242  * @type:		@RTRS_MSG_RKEY_RSP
243  * @buf_id:		RDMA buf_id of the new rkey
244  * @rkey:		new remote key for RDMA buffers id from server
245  */
246 struct rtrs_msg_rkey_rsp {
247 	__le16		type;
248 	__le16          buf_id;
249 	__le32		rkey;
250 };
251 
252 /**
253  * struct rtrs_msg_rdma_read - RDMA data transfer request from client
254  * @type:		always @RTRS_MSG_READ
255  * @usr_len:		length of user payload
256  * @sg_cnt:		number of @desc entries
257  * @desc:		RDMA buffers where the server can write the result to
258  */
259 struct rtrs_msg_rdma_read {
260 	__le16			type;
261 	__le16			usr_len;
262 	__le16			flags;
263 	__le16			sg_cnt;
264 	struct rtrs_sg_desc    desc[];
265 };
266 
267 /**
268  * struct_msg_rdma_write - Message transferred to server with RDMA-Write
269  * @type:		always @RTRS_MSG_WRITE
270  * @usr_len:		length of user payload
271  */
272 struct rtrs_msg_rdma_write {
273 	__le16			type;
274 	__le16			usr_len;
275 };
276 
277 /**
278  * struct_msg_rdma_hdr - header for read or write request
279  * @type:		@RTRS_MSG_WRITE | @RTRS_MSG_READ
280  */
281 struct rtrs_msg_rdma_hdr {
282 	__le16			type;
283 };
284 
285 /* rtrs.c */
286 
287 struct rtrs_iu *rtrs_iu_alloc(u32 queue_size, size_t size, gfp_t t,
288 			      struct ib_device *dev, enum dma_data_direction,
289 			      void (*done)(struct ib_cq *cq, struct ib_wc *wc));
290 void rtrs_iu_free(struct rtrs_iu *iu, struct ib_device *dev, u32 queue_size);
291 int rtrs_iu_post_recv(struct rtrs_con *con, struct rtrs_iu *iu);
292 int rtrs_iu_post_send(struct rtrs_con *con, struct rtrs_iu *iu, size_t size,
293 		      struct ib_send_wr *head);
294 int rtrs_iu_post_rdma_write_imm(struct rtrs_con *con, struct rtrs_iu *iu,
295 				struct ib_sge *sge, unsigned int num_sge,
296 				u32 rkey, u64 rdma_addr, u32 imm_data,
297 				enum ib_send_flags flags,
298 				struct ib_send_wr *head);
299 
300 int rtrs_post_recv_empty(struct rtrs_con *con, struct ib_cqe *cqe);
301 int rtrs_post_rdma_write_imm_empty(struct rtrs_con *con, struct ib_cqe *cqe,
302 				   u32 imm_data, enum ib_send_flags flags,
303 				   struct ib_send_wr *head);
304 
305 int rtrs_cq_qp_create(struct rtrs_sess *rtrs_sess, struct rtrs_con *con,
306 		      u32 max_send_sge, int cq_vector, u16 cq_size,
307 		      u16 wr_queue_size, enum ib_poll_context poll_ctx);
308 void rtrs_cq_qp_destroy(struct rtrs_con *con);
309 
310 void rtrs_init_hb(struct rtrs_sess *sess, struct ib_cqe *cqe,
311 		  unsigned int interval_ms, unsigned int missed_max,
312 		  void (*err_handler)(struct rtrs_con *con),
313 		  struct workqueue_struct *wq);
314 void rtrs_start_hb(struct rtrs_sess *sess);
315 void rtrs_stop_hb(struct rtrs_sess *sess);
316 void rtrs_send_hb_ack(struct rtrs_sess *sess);
317 
318 void rtrs_rdma_dev_pd_init(enum ib_pd_flags pd_flags,
319 			   struct rtrs_rdma_dev_pd *pool);
320 void rtrs_rdma_dev_pd_deinit(struct rtrs_rdma_dev_pd *pool);
321 
322 struct rtrs_ib_dev *rtrs_ib_dev_find_or_add(struct ib_device *ib_dev,
323 					    struct rtrs_rdma_dev_pd *pool);
324 int rtrs_ib_dev_put(struct rtrs_ib_dev *dev);
325 
326 static inline u32 rtrs_to_imm(u32 type, u32 payload)
327 {
328 	BUILD_BUG_ON(MAX_IMM_PAYL_BITS + MAX_IMM_TYPE_BITS != 32);
329 	BUILD_BUG_ON(RTRS_LAST_IMM > (1<<MAX_IMM_TYPE_BITS));
330 	return ((type & MAX_IMM_TYPE_MASK) << MAX_IMM_PAYL_BITS) |
331 		(payload & MAX_IMM_PAYL_MASK);
332 }
333 
334 static inline void rtrs_from_imm(u32 imm, u32 *type, u32 *payload)
335 {
336 	*payload = imm & MAX_IMM_PAYL_MASK;
337 	*type = imm >> MAX_IMM_PAYL_BITS;
338 }
339 
340 static inline u32 rtrs_to_io_req_imm(u32 addr)
341 {
342 	return rtrs_to_imm(RTRS_IO_REQ_IMM, addr);
343 }
344 
345 static inline u32 rtrs_to_io_rsp_imm(u32 msg_id, int errno, bool w_inval)
346 {
347 	enum rtrs_imm_type type;
348 	u32 payload;
349 
350 	/* 9 bits for errno, 19 bits for msg_id */
351 	payload = (abs(errno) & 0x1ff) << 19 | (msg_id & 0x7ffff);
352 	type = w_inval ? RTRS_IO_RSP_W_INV_IMM : RTRS_IO_RSP_IMM;
353 
354 	return rtrs_to_imm(type, payload);
355 }
356 
357 static inline void rtrs_from_io_rsp_imm(u32 payload, u32 *msg_id, int *errno)
358 {
359 	/* 9 bits for errno, 19 bits for msg_id */
360 	*msg_id = payload & 0x7ffff;
361 	*errno = -(int)((payload >> 19) & 0x1ff);
362 }
363 
364 #define STAT_STORE_FUNC(type, set_value, reset)				\
365 static ssize_t set_value##_store(struct kobject *kobj,			\
366 			     struct kobj_attribute *attr,		\
367 			     const char *buf, size_t count)		\
368 {									\
369 	int ret = -EINVAL;						\
370 	type *stats = container_of(kobj, type, kobj_stats);		\
371 									\
372 	if (sysfs_streq(buf, "1"))					\
373 		ret = reset(stats, true);			\
374 	else if (sysfs_streq(buf, "0"))					\
375 		ret = reset(stats, false);			\
376 	if (ret)							\
377 		return ret;						\
378 									\
379 	return count;							\
380 }
381 
382 #define STAT_SHOW_FUNC(type, get_value, print)				\
383 static ssize_t get_value##_show(struct kobject *kobj,			\
384 			   struct kobj_attribute *attr,			\
385 			   char *page)					\
386 {									\
387 	type *stats = container_of(kobj, type, kobj_stats);		\
388 									\
389 	return print(stats, page, PAGE_SIZE);			\
390 }
391 
392 #define STAT_ATTR(type, stat, print, reset)				\
393 STAT_STORE_FUNC(type, stat, reset)					\
394 STAT_SHOW_FUNC(type, stat, print)					\
395 static struct kobj_attribute stat##_attr = __ATTR_RW(stat)
396 
397 #endif /* RTRS_PRI_H */
398