xref: /freebsd/sys/dev/irdma/irdma_verbs.h (revision 4d3fc8b0)
1 /*-
2  * SPDX-License-Identifier: GPL-2.0 or Linux-OpenIB
3  *
4  * Copyright (c) 2015 - 2022 Intel Corporation
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenFabrics.org BSD license below:
11  *
12  *   Redistribution and use in source and binary forms, with or
13  *   without modification, are permitted provided that the following
14  *   conditions are met:
15  *
16  *    - Redistributions of source code must retain the above
17  *	copyright notice, this list of conditions and the following
18  *	disclaimer.
19  *
20  *    - Redistributions in binary form must reproduce the above
21  *	copyright notice, this list of conditions and the following
22  *	disclaimer in the documentation and/or other materials
23  *	provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 /*$FreeBSD$*/
35 
36 #ifndef IRDMA_VERBS_H
37 #define IRDMA_VERBS_H
38 
39 #define IRDMA_MAX_SAVED_PHY_PGADDR	4
40 #define IRDMA_FLUSH_DELAY_MS		20
41 
42 #define IRDMA_PKEY_TBL_SZ		1
43 #define IRDMA_DEFAULT_PKEY		0xFFFF
44 
45 #define iwdev_to_idev(iwdev)	(&(iwdev)->rf->sc_dev)
46 
47 struct irdma_ucontext {
48 	struct ib_ucontext ibucontext;
49 	struct irdma_device *iwdev;
50 #if __FreeBSD_version >= 1400026
51 	struct rdma_user_mmap_entry *db_mmap_entry;
52 #else
53 	struct irdma_user_mmap_entry *db_mmap_entry;
54 	DECLARE_HASHTABLE(mmap_hash_tbl, 6);
55 	spinlock_t mmap_tbl_lock; /* protect mmap hash table entries */
56 #endif
57 	struct list_head cq_reg_mem_list;
58 	spinlock_t cq_reg_mem_list_lock; /* protect CQ memory list */
59 	struct list_head qp_reg_mem_list;
60 	spinlock_t qp_reg_mem_list_lock; /* protect QP memory list */
61 	/* FIXME: Move to kcompat ideally. Used < 4.20.0 for old diassasscoaite flow */
62 	struct list_head vma_list;
63 	struct mutex vma_list_mutex; /* protect the vma_list */
64 	int abi_ver;
65 	bool legacy_mode;
66 };
67 
68 struct irdma_pd {
69 	struct ib_pd ibpd;
70 	struct irdma_sc_pd sc_pd;
71 };
72 
73 struct irdma_av {
74 	u8 macaddr[16];
75 	struct ib_ah_attr attrs;
76 	union {
77 		struct sockaddr saddr;
78 		struct sockaddr_in saddr_in;
79 		struct sockaddr_in6 saddr_in6;
80 	} sgid_addr, dgid_addr;
81 	u8 net_type;
82 };
83 
84 struct irdma_ah {
85 	struct ib_ah ibah;
86 	struct irdma_sc_ah sc_ah;
87 	struct irdma_pd *pd;
88 	struct irdma_av av;
89 	u8 sgid_index;
90 	union ib_gid dgid;
91 };
92 
93 struct irdma_hmc_pble {
94 	union {
95 		u32 idx;
96 		dma_addr_t addr;
97 	};
98 };
99 
100 struct irdma_cq_mr {
101 	struct irdma_hmc_pble cq_pbl;
102 	dma_addr_t shadow;
103 	bool split;
104 };
105 
106 struct irdma_qp_mr {
107 	struct irdma_hmc_pble sq_pbl;
108 	struct irdma_hmc_pble rq_pbl;
109 	dma_addr_t shadow;
110 	struct page *sq_page;
111 };
112 
113 struct irdma_cq_buf {
114 	struct irdma_dma_mem kmem_buf;
115 	struct irdma_cq_uk cq_uk;
116 	struct irdma_hw *hw;
117 	struct list_head list;
118 	struct work_struct work;
119 };
120 
121 struct irdma_pbl {
122 	struct list_head list;
123 	union {
124 		struct irdma_qp_mr qp_mr;
125 		struct irdma_cq_mr cq_mr;
126 	};
127 
128 	bool pbl_allocated:1;
129 	bool on_list:1;
130 	u64 user_base;
131 	struct irdma_pble_alloc pble_alloc;
132 	struct irdma_mr *iwmr;
133 };
134 
135 struct irdma_mr {
136 	union {
137 		struct ib_mr ibmr;
138 		struct ib_mw ibmw;
139 	};
140 	struct ib_umem *region;
141 	int access;
142 	u8 is_hwreg;
143 	u16 type;
144 	u32 page_cnt;
145 	u64 page_size;
146 	u64 page_msk;
147 	u32 npages;
148 	u32 stag;
149 	u64 len;
150 	u64 pgaddrmem[IRDMA_MAX_SAVED_PHY_PGADDR];
151 	struct irdma_pbl iwpbl;
152 };
153 
154 struct irdma_cq {
155 	struct ib_cq ibcq;
156 	struct irdma_sc_cq sc_cq;
157 	u16 cq_head;
158 	u16 cq_size;
159 	u16 cq_num;
160 	bool user_mode;
161 	atomic_t armed;
162 	enum irdma_cmpl_notify last_notify;
163 	u32 polled_cmpls;
164 	u32 cq_mem_size;
165 	struct irdma_dma_mem kmem;
166 	struct irdma_dma_mem kmem_shadow;
167 	struct completion free_cq;
168 	atomic_t refcnt;
169 	spinlock_t lock; /* for poll cq */
170 	struct irdma_pbl *iwpbl;
171 	struct irdma_pbl *iwpbl_shadow;
172 	struct list_head resize_list;
173 	struct irdma_cq_poll_info cur_cqe;
174 	struct list_head cmpl_generated;
175 };
176 
177 struct irdma_cmpl_gen {
178 	struct list_head list;
179 	struct irdma_cq_poll_info cpi;
180 };
181 
182 struct disconn_work {
183 	struct work_struct work;
184 	struct irdma_qp *iwqp;
185 };
186 
187 struct iw_cm_id;
188 
189 struct irdma_qp_kmode {
190 	struct irdma_dma_mem dma_mem;
191 	u32 *sig_trk_mem;
192 	struct irdma_sq_uk_wr_trk_info *sq_wrid_mem;
193 	u64 *rq_wrid_mem;
194 };
195 
196 struct irdma_qp {
197 	struct ib_qp ibqp;
198 	struct irdma_sc_qp sc_qp;
199 	struct irdma_device *iwdev;
200 	struct irdma_cq *iwscq;
201 	struct irdma_cq *iwrcq;
202 	struct irdma_pd *iwpd;
203 #if __FreeBSD_version >= 1400026
204 	struct rdma_user_mmap_entry *push_wqe_mmap_entry;
205 	struct rdma_user_mmap_entry *push_db_mmap_entry;
206 #else
207 	struct irdma_user_mmap_entry *push_wqe_mmap_entry;
208 	struct irdma_user_mmap_entry *push_db_mmap_entry;
209 #endif
210 	struct irdma_qp_host_ctx_info ctx_info;
211 	union {
212 		struct irdma_iwarp_offload_info iwarp_info;
213 		struct irdma_roce_offload_info roce_info;
214 	};
215 
216 	union {
217 		struct irdma_tcp_offload_info tcp_info;
218 		struct irdma_udp_offload_info udp_info;
219 	};
220 
221 	struct irdma_ah roce_ah;
222 	struct list_head teardown_entry;
223 	atomic_t refcnt;
224 	struct iw_cm_id *cm_id;
225 	struct irdma_cm_node *cm_node;
226 	struct delayed_work dwork_flush;
227 	struct ib_mr *lsmm_mr;
228 	atomic_t hw_mod_qp_pend;
229 	enum ib_qp_state ibqp_state;
230 	u32 qp_mem_size;
231 	u32 last_aeq;
232 	int max_send_wr;
233 	int max_recv_wr;
234 	atomic_t close_timer_started;
235 	spinlock_t lock; /* serialize posting WRs to SQ/RQ */
236 	struct irdma_qp_context *iwqp_context;
237 	void *pbl_vbase;
238 	dma_addr_t pbl_pbase;
239 	struct page *page;
240 	u8 iwarp_state;
241 	u16 term_sq_flush_code;
242 	u16 term_rq_flush_code;
243 	u8 hw_iwarp_state;
244 	u8 hw_tcp_state;
245 	struct irdma_qp_kmode kqp;
246 	struct irdma_dma_mem host_ctx;
247 	struct timer_list terminate_timer;
248 	struct irdma_pbl *iwpbl;
249 	struct irdma_sge *sg_list;
250 	struct irdma_dma_mem q2_ctx_mem;
251 	struct irdma_dma_mem ietf_mem;
252 	struct completion free_qp;
253 	wait_queue_head_t waitq;
254 	wait_queue_head_t mod_qp_waitq;
255 	u8 rts_ae_rcvd;
256 	u8 active_conn : 1;
257 	u8 user_mode : 1;
258 	u8 hte_added : 1;
259 	u8 flush_issued : 1;
260 	u8 sig_all : 1;
261 	u8 pau_mode : 1;
262 };
263 
264 enum irdma_mmap_flag {
265 	IRDMA_MMAP_IO_NC,
266 	IRDMA_MMAP_IO_WC,
267 };
268 
269 struct irdma_user_mmap_entry {
270 #if __FreeBSD_version >= 1400026
271 	struct rdma_user_mmap_entry rdma_entry;
272 #else
273 	struct irdma_ucontext *ucontext;
274 	struct hlist_node hlist;
275 	u64 pgoff_key; /* Used to compute offset (in bytes) returned to user libc's mmap */
276 #endif
277 	u64 bar_offset;
278 	u8 mmap_flag;
279 };
280 
281 static inline u16 irdma_fw_major_ver(struct irdma_sc_dev *dev)
282 {
283 	return (u16)FIELD_GET(IRDMA_FW_VER_MAJOR, dev->feature_info[IRDMA_FEATURE_FW_INFO]);
284 }
285 
286 static inline u16 irdma_fw_minor_ver(struct irdma_sc_dev *dev)
287 {
288 	return (u16)FIELD_GET(IRDMA_FW_VER_MINOR, dev->feature_info[IRDMA_FEATURE_FW_INFO]);
289 }
290 
291 /**
292  * irdma_mcast_mac_v4 - Get the multicast MAC for an IP address
293  * @ip_addr: IPv4 address
294  * @mac: pointer to result MAC address
295  *
296  */
297 static inline void irdma_mcast_mac_v4(u32 *ip_addr, u8 *mac)
298 {
299 	u8 *ip = (u8 *)ip_addr;
300 	unsigned char mac4[ETH_ALEN] = {0x01, 0x00, 0x5E, ip[2] & 0x7F, ip[1],
301 					ip[0]};
302 
303 	ether_addr_copy(mac, mac4);
304 }
305 
306 /**
307  * irdma_mcast_mac_v6 - Get the multicast MAC for an IP address
308  * @ip_addr: IPv6 address
309  * @mac: pointer to result MAC address
310  *
311  */
312 static inline void irdma_mcast_mac_v6(u32 *ip_addr, u8 *mac)
313 {
314 	u8 *ip = (u8 *)ip_addr;
315 	unsigned char mac6[ETH_ALEN] = {0x33, 0x33, ip[3], ip[2], ip[1], ip[0]};
316 
317 	ether_addr_copy(mac, mac6);
318 }
319 
320 #if __FreeBSD_version >= 1400026
321 struct rdma_user_mmap_entry*
322 irdma_user_mmap_entry_insert(struct irdma_ucontext *ucontext, u64 bar_offset,
323 			     enum irdma_mmap_flag mmap_flag, u64 *mmap_offset);
324 #else
325 struct irdma_user_mmap_entry *
326 irdma_user_mmap_entry_add_hash(struct irdma_ucontext *ucontext, u64 bar_offset,
327 			       enum irdma_mmap_flag mmap_flag, u64 *mmap_offset);
328 void irdma_user_mmap_entry_del_hash(struct irdma_user_mmap_entry *entry);
329 #endif
330 int irdma_ib_register_device(struct irdma_device *iwdev);
331 void irdma_ib_unregister_device(struct irdma_device *iwdev);
332 void irdma_ib_qp_event(struct irdma_qp *iwqp, enum irdma_qp_event_type event);
333 void irdma_generate_flush_completions(struct irdma_qp *iwqp);
334 void irdma_remove_cmpls_list(struct irdma_cq *iwcq);
335 int irdma_generated_cmpls(struct irdma_cq *iwcq, struct irdma_cq_poll_info *cq_poll_info);
336 void irdma_sched_qp_flush_work(struct irdma_qp *iwqp);
337 void irdma_flush_worker(struct work_struct *work);
338 #endif /* IRDMA_VERBS_H */
339