xref: /linux/drivers/infiniband/hw/erdma/erdma_verbs.h (revision f9bff0e31881d03badf191d3b0005839391f5f2b)
1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 
3 /* Authors: Cheng Xu <chengyou@linux.alibaba.com> */
4 /*          Kai Shen <kaishen@linux.alibaba.com> */
5 /* Copyright (c) 2020-2022, Alibaba Group. */
6 
7 #ifndef __ERDMA_VERBS_H__
8 #define __ERDMA_VERBS_H__
9 
10 #include "erdma.h"
11 
12 /* RDMA Capability. */
13 #define ERDMA_MAX_PD (128 * 1024)
14 #define ERDMA_MAX_SEND_WR 8192
15 #define ERDMA_MAX_ORD 128
16 #define ERDMA_MAX_IRD 128
17 #define ERDMA_MAX_SGE_RD 1
18 #define ERDMA_MAX_CONTEXT (128 * 1024)
19 #define ERDMA_MAX_SEND_SGE 6
20 #define ERDMA_MAX_RECV_SGE 1
21 #define ERDMA_MAX_INLINE (sizeof(struct erdma_sge) * (ERDMA_MAX_SEND_SGE))
22 #define ERDMA_MAX_FRMR_PA 512
23 
24 enum {
25 	ERDMA_MMAP_IO_NC = 0, /* no cache */
26 };
27 
28 struct erdma_user_mmap_entry {
29 	struct rdma_user_mmap_entry rdma_entry;
30 	u64 address;
31 	u8 mmap_flag;
32 };
33 
34 struct erdma_ext_db_info {
35 	bool enable;
36 	u16 sdb_off;
37 	u16 rdb_off;
38 	u16 cdb_off;
39 };
40 
41 struct erdma_ucontext {
42 	struct ib_ucontext ibucontext;
43 
44 	struct erdma_ext_db_info ext_db;
45 
46 	u64 sdb;
47 	u64 rdb;
48 	u64 cdb;
49 
50 	struct rdma_user_mmap_entry *sq_db_mmap_entry;
51 	struct rdma_user_mmap_entry *rq_db_mmap_entry;
52 	struct rdma_user_mmap_entry *cq_db_mmap_entry;
53 
54 	/* doorbell records */
55 	struct list_head dbrecords_page_list;
56 	struct mutex dbrecords_page_mutex;
57 };
58 
59 struct erdma_pd {
60 	struct ib_pd ibpd;
61 	u32 pdn;
62 };
63 
64 /*
65  * MemoryRegion definition.
66  */
67 #define ERDMA_MAX_INLINE_MTT_ENTRIES 4
68 #define MTT_SIZE(mtt_cnt) (mtt_cnt << 3) /* per mtt takes 8 Bytes. */
69 #define ERDMA_MR_MAX_MTT_CNT 524288
70 #define ERDMA_MTT_ENTRY_SIZE 8
71 
72 #define ERDMA_MR_TYPE_NORMAL 0
73 #define ERDMA_MR_TYPE_FRMR 1
74 #define ERDMA_MR_TYPE_DMA 2
75 
76 #define ERDMA_MR_INLINE_MTT 0
77 #define ERDMA_MR_INDIRECT_MTT 1
78 
79 #define ERDMA_MR_ACC_RA BIT(0)
80 #define ERDMA_MR_ACC_LR BIT(1)
81 #define ERDMA_MR_ACC_LW BIT(2)
82 #define ERDMA_MR_ACC_RR BIT(3)
83 #define ERDMA_MR_ACC_RW BIT(4)
84 
85 static inline u8 to_erdma_access_flags(int access)
86 {
87 	return (access & IB_ACCESS_REMOTE_READ ? ERDMA_MR_ACC_RR : 0) |
88 	       (access & IB_ACCESS_LOCAL_WRITE ? ERDMA_MR_ACC_LW : 0) |
89 	       (access & IB_ACCESS_REMOTE_WRITE ? ERDMA_MR_ACC_RW : 0) |
90 	       (access & IB_ACCESS_REMOTE_ATOMIC ? ERDMA_MR_ACC_RA : 0);
91 }
92 
93 struct erdma_mem {
94 	struct ib_umem *umem;
95 	void *mtt_buf;
96 	u32 mtt_type;
97 	u32 page_size;
98 	u32 page_offset;
99 	u32 page_cnt;
100 	u32 mtt_nents;
101 
102 	u64 va;
103 	u64 len;
104 
105 	u64 mtt_entry[ERDMA_MAX_INLINE_MTT_ENTRIES];
106 };
107 
108 struct erdma_mr {
109 	struct ib_mr ibmr;
110 	struct erdma_mem mem;
111 	u8 type;
112 	u8 access;
113 	u8 valid;
114 };
115 
116 struct erdma_user_dbrecords_page {
117 	struct list_head list;
118 	struct ib_umem *umem;
119 	u64 va;
120 	int refcnt;
121 };
122 
123 struct erdma_uqp {
124 	struct erdma_mem sq_mtt;
125 	struct erdma_mem rq_mtt;
126 
127 	dma_addr_t sq_db_info_dma_addr;
128 	dma_addr_t rq_db_info_dma_addr;
129 
130 	struct erdma_user_dbrecords_page *user_dbr_page;
131 
132 	u32 rq_offset;
133 };
134 
135 struct erdma_kqp {
136 	u16 sq_pi;
137 	u16 sq_ci;
138 
139 	u16 rq_pi;
140 	u16 rq_ci;
141 
142 	u64 *swr_tbl;
143 	u64 *rwr_tbl;
144 
145 	void __iomem *hw_sq_db;
146 	void __iomem *hw_rq_db;
147 
148 	void *sq_buf;
149 	dma_addr_t sq_buf_dma_addr;
150 
151 	void *rq_buf;
152 	dma_addr_t rq_buf_dma_addr;
153 
154 	void *sq_db_info;
155 	void *rq_db_info;
156 
157 	u8 sig_all;
158 };
159 
160 enum erdma_qp_state {
161 	ERDMA_QP_STATE_IDLE = 0,
162 	ERDMA_QP_STATE_RTR = 1,
163 	ERDMA_QP_STATE_RTS = 2,
164 	ERDMA_QP_STATE_CLOSING = 3,
165 	ERDMA_QP_STATE_TERMINATE = 4,
166 	ERDMA_QP_STATE_ERROR = 5,
167 	ERDMA_QP_STATE_UNDEF = 7,
168 	ERDMA_QP_STATE_COUNT = 8
169 };
170 
171 enum erdma_qp_attr_mask {
172 	ERDMA_QP_ATTR_STATE = (1 << 0),
173 	ERDMA_QP_ATTR_LLP_HANDLE = (1 << 2),
174 	ERDMA_QP_ATTR_ORD = (1 << 3),
175 	ERDMA_QP_ATTR_IRD = (1 << 4),
176 	ERDMA_QP_ATTR_SQ_SIZE = (1 << 5),
177 	ERDMA_QP_ATTR_RQ_SIZE = (1 << 6),
178 	ERDMA_QP_ATTR_MPA = (1 << 7)
179 };
180 
181 enum erdma_qp_flags {
182 	ERDMA_QP_IN_FLUSHING = (1 << 0),
183 };
184 
185 struct erdma_qp_attrs {
186 	enum erdma_qp_state state;
187 	enum erdma_cc_alg cc; /* Congestion control algorithm */
188 	u32 sq_size;
189 	u32 rq_size;
190 	u32 orq_size;
191 	u32 irq_size;
192 	u32 max_send_sge;
193 	u32 max_recv_sge;
194 	u32 cookie;
195 #define ERDMA_QP_ACTIVE 0
196 #define ERDMA_QP_PASSIVE 1
197 	u8 qp_type;
198 	u8 pd_len;
199 };
200 
201 struct erdma_qp {
202 	struct ib_qp ibqp;
203 	struct kref ref;
204 	struct completion safe_free;
205 	struct erdma_dev *dev;
206 	struct erdma_cep *cep;
207 	struct rw_semaphore state_lock;
208 
209 	unsigned long flags;
210 	struct delayed_work reflush_dwork;
211 
212 	union {
213 		struct erdma_kqp kern_qp;
214 		struct erdma_uqp user_qp;
215 	};
216 
217 	struct erdma_cq *scq;
218 	struct erdma_cq *rcq;
219 
220 	struct erdma_qp_attrs attrs;
221 	spinlock_t lock;
222 };
223 
224 struct erdma_kcq_info {
225 	void *qbuf;
226 	dma_addr_t qbuf_dma_addr;
227 	u32 ci;
228 	u32 cmdsn;
229 	u32 notify_cnt;
230 
231 	spinlock_t lock;
232 	u8 __iomem *db;
233 	u64 *db_record;
234 };
235 
236 struct erdma_ucq_info {
237 	struct erdma_mem qbuf_mtt;
238 	struct erdma_user_dbrecords_page *user_dbr_page;
239 	dma_addr_t db_info_dma_addr;
240 };
241 
242 struct erdma_cq {
243 	struct ib_cq ibcq;
244 	u32 cqn;
245 
246 	u32 depth;
247 	u32 assoc_eqn;
248 
249 	union {
250 		struct erdma_kcq_info kern_cq;
251 		struct erdma_ucq_info user_cq;
252 	};
253 };
254 
255 #define QP_ID(qp) ((qp)->ibqp.qp_num)
256 
257 static inline struct erdma_qp *find_qp_by_qpn(struct erdma_dev *dev, int id)
258 {
259 	return (struct erdma_qp *)xa_load(&dev->qp_xa, id);
260 }
261 
262 static inline struct erdma_cq *find_cq_by_cqn(struct erdma_dev *dev, int id)
263 {
264 	return (struct erdma_cq *)xa_load(&dev->cq_xa, id);
265 }
266 
267 void erdma_qp_get(struct erdma_qp *qp);
268 void erdma_qp_put(struct erdma_qp *qp);
269 int erdma_modify_qp_internal(struct erdma_qp *qp, struct erdma_qp_attrs *attrs,
270 			     enum erdma_qp_attr_mask mask);
271 void erdma_qp_llp_close(struct erdma_qp *qp);
272 void erdma_qp_cm_drop(struct erdma_qp *qp);
273 
274 static inline struct erdma_ucontext *to_ectx(struct ib_ucontext *ibctx)
275 {
276 	return container_of(ibctx, struct erdma_ucontext, ibucontext);
277 }
278 
279 static inline struct erdma_pd *to_epd(struct ib_pd *pd)
280 {
281 	return container_of(pd, struct erdma_pd, ibpd);
282 }
283 
284 static inline struct erdma_mr *to_emr(struct ib_mr *ibmr)
285 {
286 	return container_of(ibmr, struct erdma_mr, ibmr);
287 }
288 
289 static inline struct erdma_qp *to_eqp(struct ib_qp *qp)
290 {
291 	return container_of(qp, struct erdma_qp, ibqp);
292 }
293 
294 static inline struct erdma_cq *to_ecq(struct ib_cq *ibcq)
295 {
296 	return container_of(ibcq, struct erdma_cq, ibcq);
297 }
298 
299 static inline struct erdma_user_mmap_entry *
300 to_emmap(struct rdma_user_mmap_entry *ibmmap)
301 {
302 	return container_of(ibmmap, struct erdma_user_mmap_entry, rdma_entry);
303 }
304 
305 int erdma_alloc_ucontext(struct ib_ucontext *ibctx, struct ib_udata *data);
306 void erdma_dealloc_ucontext(struct ib_ucontext *ibctx);
307 int erdma_query_device(struct ib_device *dev, struct ib_device_attr *attr,
308 		       struct ib_udata *data);
309 int erdma_get_port_immutable(struct ib_device *dev, u32 port,
310 			     struct ib_port_immutable *ib_port_immutable);
311 int erdma_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
312 		    struct ib_udata *data);
313 int erdma_query_port(struct ib_device *dev, u32 port,
314 		     struct ib_port_attr *attr);
315 int erdma_query_gid(struct ib_device *dev, u32 port, int idx,
316 		    union ib_gid *gid);
317 int erdma_alloc_pd(struct ib_pd *ibpd, struct ib_udata *data);
318 int erdma_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata);
319 int erdma_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *attr,
320 		    struct ib_udata *data);
321 int erdma_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int mask,
322 		   struct ib_qp_init_attr *init_attr);
323 int erdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int mask,
324 		    struct ib_udata *data);
325 int erdma_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata);
326 int erdma_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata);
327 int erdma_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags);
328 struct ib_mr *erdma_reg_user_mr(struct ib_pd *ibpd, u64 start, u64 len,
329 				u64 virt, int access, struct ib_udata *udata);
330 struct ib_mr *erdma_get_dma_mr(struct ib_pd *ibpd, int rights);
331 int erdma_dereg_mr(struct ib_mr *ibmr, struct ib_udata *data);
332 int erdma_mmap(struct ib_ucontext *ctx, struct vm_area_struct *vma);
333 void erdma_mmap_free(struct rdma_user_mmap_entry *rdma_entry);
334 void erdma_qp_get_ref(struct ib_qp *ibqp);
335 void erdma_qp_put_ref(struct ib_qp *ibqp);
336 struct ib_qp *erdma_get_ibqp(struct ib_device *dev, int id);
337 int erdma_post_send(struct ib_qp *ibqp, const struct ib_send_wr *send_wr,
338 		    const struct ib_send_wr **bad_send_wr);
339 int erdma_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *recv_wr,
340 		    const struct ib_recv_wr **bad_recv_wr);
341 int erdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc);
342 struct ib_mr *erdma_ib_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type,
343 				u32 max_num_sg);
344 int erdma_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
345 		    unsigned int *sg_offset);
346 void erdma_port_event(struct erdma_dev *dev, enum ib_event_type reason);
347 void erdma_set_mtu(struct erdma_dev *dev, u32 mtu);
348 
349 #endif
350