1 /*
2  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
3  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *	   Redistribution and use in source and binary forms, with or
12  *	   without modification, are permitted provided that the following
13  *	   conditions are met:
14  *
15  *	- Redistributions of source code must retain the above
16  *	  copyright notice, this list of conditions and the following
17  *	  disclaimer.
18  *
19  *	- Redistributions in binary form must reproduce the above
20  *	  copyright notice, this list of conditions and the following
21  *	  disclaimer in the documentation and/or other materials
22  *	  provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #ifndef RXE_VERBS_H
35 #define RXE_VERBS_H
36 
37 #include <linux/interrupt.h>
38 #include <rdma/rdma_user_rxe.h>
39 #include "rxe_pool.h"
40 #include "rxe_task.h"
41 #include "rxe_hw_counters.h"
42 
43 static inline int pkey_match(u16 key1, u16 key2)
44 {
45 	return (((key1 & 0x7fff) != 0) &&
46 		((key1 & 0x7fff) == (key2 & 0x7fff)) &&
47 		((key1 & 0x8000) || (key2 & 0x8000))) ? 1 : 0;
48 }
49 
50 /* Return >0 if psn_a > psn_b
51  *	   0 if psn_a == psn_b
52  *	  <0 if psn_a < psn_b
53  */
54 static inline int psn_compare(u32 psn_a, u32 psn_b)
55 {
56 	s32 diff;
57 
58 	diff = (psn_a - psn_b) << 8;
59 	return diff;
60 }
61 
62 struct rxe_ucontext {
63 	struct rxe_pool_entry	pelem;
64 	struct ib_ucontext	ibuc;
65 };
66 
67 struct rxe_pd {
68 	struct rxe_pool_entry	pelem;
69 	struct ib_pd		ibpd;
70 };
71 
72 struct rxe_ah {
73 	struct rxe_pool_entry	pelem;
74 	struct ib_ah		ibah;
75 	struct rxe_pd		*pd;
76 	struct rxe_av		av;
77 };
78 
79 struct rxe_cqe {
80 	union {
81 		struct ib_wc		ibwc;
82 		struct ib_uverbs_wc	uibwc;
83 	};
84 };
85 
86 struct rxe_cq {
87 	struct rxe_pool_entry	pelem;
88 	struct ib_cq		ibcq;
89 	struct rxe_queue	*queue;
90 	spinlock_t		cq_lock;
91 	u8			notify;
92 	int			is_user;
93 	struct tasklet_struct	comp_task;
94 };
95 
96 enum wqe_state {
97 	wqe_state_posted,
98 	wqe_state_processing,
99 	wqe_state_pending,
100 	wqe_state_done,
101 	wqe_state_error,
102 };
103 
104 struct rxe_sq {
105 	int			max_wr;
106 	int			max_sge;
107 	int			max_inline;
108 	spinlock_t		sq_lock; /* guard queue */
109 	struct rxe_queue	*queue;
110 };
111 
112 struct rxe_rq {
113 	int			max_wr;
114 	int			max_sge;
115 	spinlock_t		producer_lock; /* guard queue producer */
116 	spinlock_t		consumer_lock; /* guard queue consumer */
117 	struct rxe_queue	*queue;
118 };
119 
120 struct rxe_srq {
121 	struct rxe_pool_entry	pelem;
122 	struct ib_srq		ibsrq;
123 	struct rxe_pd		*pd;
124 	struct rxe_rq		rq;
125 	u32			srq_num;
126 
127 	int			limit;
128 	int			error;
129 };
130 
131 enum rxe_qp_state {
132 	QP_STATE_RESET,
133 	QP_STATE_INIT,
134 	QP_STATE_READY,
135 	QP_STATE_DRAIN,		/* req only */
136 	QP_STATE_DRAINED,	/* req only */
137 	QP_STATE_ERROR
138 };
139 
140 extern char *rxe_qp_state_name[];
141 
142 struct rxe_req_info {
143 	enum rxe_qp_state	state;
144 	int			wqe_index;
145 	u32			psn;
146 	int			opcode;
147 	atomic_t		rd_atomic;
148 	int			wait_fence;
149 	int			need_rd_atomic;
150 	int			wait_psn;
151 	int			need_retry;
152 	int			noack_pkts;
153 	struct rxe_task		task;
154 };
155 
156 struct rxe_comp_info {
157 	u32			psn;
158 	int			opcode;
159 	int			timeout;
160 	int			timeout_retry;
161 	u32			retry_cnt;
162 	u32			rnr_retry;
163 	struct rxe_task		task;
164 };
165 
166 enum rdatm_res_state {
167 	rdatm_res_state_next,
168 	rdatm_res_state_new,
169 	rdatm_res_state_replay,
170 };
171 
172 struct resp_res {
173 	int			type;
174 	u32			first_psn;
175 	u32			last_psn;
176 	u32			cur_psn;
177 	enum rdatm_res_state	state;
178 
179 	union {
180 		struct {
181 			struct sk_buff	*skb;
182 		} atomic;
183 		struct {
184 			struct rxe_mem	*mr;
185 			u64		va_org;
186 			u32		rkey;
187 			u32		length;
188 			u64		va;
189 			u32		resid;
190 		} read;
191 	};
192 };
193 
194 struct rxe_resp_info {
195 	enum rxe_qp_state	state;
196 	u32			msn;
197 	u32			psn;
198 	int			opcode;
199 	int			drop_msg;
200 	int			goto_error;
201 	int			sent_psn_nak;
202 	enum ib_wc_status	status;
203 	u8			aeth_syndrome;
204 
205 	/* Receive only */
206 	struct rxe_recv_wqe	*wqe;
207 
208 	/* RDMA read / atomic only */
209 	u64			va;
210 	struct rxe_mem		*mr;
211 	u32			resid;
212 	u32			rkey;
213 	u64			atomic_orig;
214 
215 	/* SRQ only */
216 	struct {
217 		struct rxe_recv_wqe	wqe;
218 		struct ib_sge		sge[RXE_MAX_SGE];
219 	} srq_wqe;
220 
221 	/* Responder resources. It's a circular list where the oldest
222 	 * resource is dropped first.
223 	 */
224 	struct resp_res		*resources;
225 	unsigned int		res_head;
226 	unsigned int		res_tail;
227 	struct resp_res		*res;
228 	struct rxe_task		task;
229 };
230 
231 struct rxe_qp {
232 	struct rxe_pool_entry	pelem;
233 	struct ib_qp		ibqp;
234 	struct ib_qp_attr	attr;
235 	unsigned int		valid;
236 	unsigned int		mtu;
237 	int			is_user;
238 
239 	struct rxe_pd		*pd;
240 	struct rxe_srq		*srq;
241 	struct rxe_cq		*scq;
242 	struct rxe_cq		*rcq;
243 
244 	enum ib_sig_type	sq_sig_type;
245 
246 	struct rxe_sq		sq;
247 	struct rxe_rq		rq;
248 
249 	struct socket		*sk;
250 
251 	struct rxe_av		pri_av;
252 	struct rxe_av		alt_av;
253 
254 	/* list of mcast groups qp has joined (for cleanup) */
255 	struct list_head	grp_list;
256 	spinlock_t		grp_lock; /* guard grp_list */
257 
258 	struct sk_buff_head	req_pkts;
259 	struct sk_buff_head	resp_pkts;
260 	struct sk_buff_head	send_pkts;
261 
262 	struct rxe_req_info	req;
263 	struct rxe_comp_info	comp;
264 	struct rxe_resp_info	resp;
265 
266 	atomic_t		ssn;
267 	atomic_t		skb_out;
268 	int			need_req_skb;
269 
270 	/* Timer for retranmitting packet when ACKs have been lost. RC
271 	 * only. The requester sets it when it is not already
272 	 * started. The responder resets it whenever an ack is
273 	 * received.
274 	 */
275 	struct timer_list retrans_timer;
276 	u64 qp_timeout_jiffies;
277 
278 	/* Timer for handling RNR NAKS. */
279 	struct timer_list rnr_nak_timer;
280 
281 	spinlock_t		state_lock; /* guard requester and completer */
282 };
283 
284 enum rxe_mem_state {
285 	RXE_MEM_STATE_ZOMBIE,
286 	RXE_MEM_STATE_INVALID,
287 	RXE_MEM_STATE_FREE,
288 	RXE_MEM_STATE_VALID,
289 };
290 
291 enum rxe_mem_type {
292 	RXE_MEM_TYPE_NONE,
293 	RXE_MEM_TYPE_DMA,
294 	RXE_MEM_TYPE_MR,
295 	RXE_MEM_TYPE_FMR,
296 	RXE_MEM_TYPE_MW,
297 };
298 
299 #define RXE_BUF_PER_MAP		(PAGE_SIZE / sizeof(struct rxe_phys_buf))
300 
301 struct rxe_phys_buf {
302 	u64      addr;
303 	u64      size;
304 };
305 
306 struct rxe_map {
307 	struct rxe_phys_buf	buf[RXE_BUF_PER_MAP];
308 };
309 
310 struct rxe_mem {
311 	struct rxe_pool_entry	pelem;
312 	union {
313 		struct ib_mr		ibmr;
314 		struct ib_mw		ibmw;
315 	};
316 
317 	struct rxe_pd		*pd;
318 	struct ib_umem		*umem;
319 
320 	u32			lkey;
321 	u32			rkey;
322 
323 	enum rxe_mem_state	state;
324 	enum rxe_mem_type	type;
325 	u64			va;
326 	u64			iova;
327 	size_t			length;
328 	u32			offset;
329 	int			access;
330 
331 	int			page_shift;
332 	int			page_mask;
333 	int			map_shift;
334 	int			map_mask;
335 
336 	u32			num_buf;
337 	u32			nbuf;
338 
339 	u32			max_buf;
340 	u32			num_map;
341 
342 	struct rxe_map		**map;
343 };
344 
345 struct rxe_mc_grp {
346 	struct rxe_pool_entry	pelem;
347 	spinlock_t		mcg_lock; /* guard group */
348 	struct rxe_dev		*rxe;
349 	struct list_head	qp_list;
350 	union ib_gid		mgid;
351 	int			num_qp;
352 	u32			qkey;
353 	u16			pkey;
354 };
355 
356 struct rxe_mc_elem {
357 	struct rxe_pool_entry	pelem;
358 	struct list_head	qp_list;
359 	struct list_head	grp_list;
360 	struct rxe_qp		*qp;
361 	struct rxe_mc_grp	*grp;
362 };
363 
364 struct rxe_port {
365 	struct ib_port_attr	attr;
366 	u16			*pkey_tbl;
367 	__be64			port_guid;
368 	__be64			subnet_prefix;
369 	spinlock_t		port_lock; /* guard port */
370 	unsigned int		mtu_cap;
371 	/* special QPs */
372 	u32			qp_smi_index;
373 	u32			qp_gsi_index;
374 };
375 
376 struct rxe_dev {
377 	struct ib_device	ib_dev;
378 	struct ib_device_attr	attr;
379 	int			max_ucontext;
380 	int			max_inline_data;
381 	struct kref		ref_cnt;
382 	struct mutex	usdev_lock;
383 
384 	struct net_device	*ndev;
385 
386 	int			xmit_errors;
387 
388 	struct rxe_pool		uc_pool;
389 	struct rxe_pool		pd_pool;
390 	struct rxe_pool		ah_pool;
391 	struct rxe_pool		srq_pool;
392 	struct rxe_pool		qp_pool;
393 	struct rxe_pool		cq_pool;
394 	struct rxe_pool		mr_pool;
395 	struct rxe_pool		mw_pool;
396 	struct rxe_pool		mc_grp_pool;
397 	struct rxe_pool		mc_elem_pool;
398 
399 	spinlock_t		pending_lock; /* guard pending_mmaps */
400 	struct list_head	pending_mmaps;
401 
402 	spinlock_t		mmap_offset_lock; /* guard mmap_offset */
403 	int			mmap_offset;
404 
405 	u64			stats_counters[RXE_NUM_OF_COUNTERS];
406 
407 	struct rxe_port		port;
408 	struct list_head	list;
409 	struct crypto_shash	*tfm;
410 };
411 
412 static inline void rxe_counter_inc(struct rxe_dev *rxe, enum rxe_counters cnt)
413 {
414 	rxe->stats_counters[cnt]++;
415 }
416 
417 static inline struct rxe_dev *to_rdev(struct ib_device *dev)
418 {
419 	return dev ? container_of(dev, struct rxe_dev, ib_dev) : NULL;
420 }
421 
422 static inline struct rxe_ucontext *to_ruc(struct ib_ucontext *uc)
423 {
424 	return uc ? container_of(uc, struct rxe_ucontext, ibuc) : NULL;
425 }
426 
427 static inline struct rxe_pd *to_rpd(struct ib_pd *pd)
428 {
429 	return pd ? container_of(pd, struct rxe_pd, ibpd) : NULL;
430 }
431 
432 static inline struct rxe_ah *to_rah(struct ib_ah *ah)
433 {
434 	return ah ? container_of(ah, struct rxe_ah, ibah) : NULL;
435 }
436 
437 static inline struct rxe_srq *to_rsrq(struct ib_srq *srq)
438 {
439 	return srq ? container_of(srq, struct rxe_srq, ibsrq) : NULL;
440 }
441 
442 static inline struct rxe_qp *to_rqp(struct ib_qp *qp)
443 {
444 	return qp ? container_of(qp, struct rxe_qp, ibqp) : NULL;
445 }
446 
447 static inline struct rxe_cq *to_rcq(struct ib_cq *cq)
448 {
449 	return cq ? container_of(cq, struct rxe_cq, ibcq) : NULL;
450 }
451 
452 static inline struct rxe_mem *to_rmr(struct ib_mr *mr)
453 {
454 	return mr ? container_of(mr, struct rxe_mem, ibmr) : NULL;
455 }
456 
457 static inline struct rxe_mem *to_rmw(struct ib_mw *mw)
458 {
459 	return mw ? container_of(mw, struct rxe_mem, ibmw) : NULL;
460 }
461 
462 int rxe_register_device(struct rxe_dev *rxe);
463 int rxe_unregister_device(struct rxe_dev *rxe);
464 
465 void rxe_mc_cleanup(struct rxe_pool_entry *arg);
466 
467 #endif /* RXE_VERBS_H */
468