xref: /freebsd/sys/dev/irdma/irdma_verbs.h (revision e3aa18ad)
1 /*-
2  * SPDX-License-Identifier: GPL-2.0 or Linux-OpenIB
3  *
4  * Copyright (c) 2015 - 2021 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 1500
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 	struct rdma_user_mmap_entry *db_mmap_entry;
51 	struct list_head cq_reg_mem_list;
52 	spinlock_t cq_reg_mem_list_lock; /* protect CQ memory list */
53 	struct list_head qp_reg_mem_list;
54 	spinlock_t qp_reg_mem_list_lock; /* protect QP memory list */
55 	/* FIXME: Move to kcompat ideally. Used < 4.20.0 for old diassasscoaite flow */
56 	struct list_head vma_list;
57 	struct mutex vma_list_mutex; /* protect the vma_list */
58 	int abi_ver;
59 	bool legacy_mode;
60 };
61 
62 struct irdma_pd {
63 	struct ib_pd ibpd;
64 	struct irdma_sc_pd sc_pd;
65 };
66 
67 struct irdma_av {
68 	u8 macaddr[16];
69 	struct ib_ah_attr attrs;
70 	union {
71 		struct sockaddr saddr;
72 		struct sockaddr_in saddr_in;
73 		struct sockaddr_in6 saddr_in6;
74 	} sgid_addr, dgid_addr;
75 	u8 net_type;
76 };
77 
78 struct irdma_ah {
79 	struct ib_ah ibah;
80 	struct irdma_sc_ah sc_ah;
81 	struct irdma_pd *pd;
82 	struct irdma_av av;
83 	u8 sgid_index;
84 	union ib_gid dgid;
85 };
86 
87 struct irdma_hmc_pble {
88 	union {
89 		u32 idx;
90 		dma_addr_t addr;
91 	};
92 };
93 
94 struct irdma_cq_mr {
95 	struct irdma_hmc_pble cq_pbl;
96 	dma_addr_t shadow;
97 	bool split;
98 };
99 
100 struct irdma_qp_mr {
101 	struct irdma_hmc_pble sq_pbl;
102 	struct irdma_hmc_pble rq_pbl;
103 	dma_addr_t shadow;
104 	struct page *sq_page;
105 };
106 
107 struct irdma_cq_buf {
108 	struct irdma_dma_mem kmem_buf;
109 	struct irdma_cq_uk cq_uk;
110 	struct irdma_hw *hw;
111 	struct list_head list;
112 	struct work_struct work;
113 };
114 
115 struct irdma_pbl {
116 	struct list_head list;
117 	union {
118 		struct irdma_qp_mr qp_mr;
119 		struct irdma_cq_mr cq_mr;
120 	};
121 
122 	bool pbl_allocated:1;
123 	bool on_list:1;
124 	u64 user_base;
125 	struct irdma_pble_alloc pble_alloc;
126 	struct irdma_mr *iwmr;
127 };
128 
129 struct irdma_mr {
130 	union {
131 		struct ib_mr ibmr;
132 		struct ib_mw ibmw;
133 	};
134 	struct ib_umem *region;
135 	u16 type;
136 	u32 page_cnt;
137 	u64 page_size;
138 	u64 page_msk;
139 	u32 npages;
140 	u32 stag;
141 	u64 len;
142 	u64 pgaddrmem[IRDMA_MAX_SAVED_PHY_PGADDR];
143 	struct irdma_pbl iwpbl;
144 };
145 
146 struct irdma_cq {
147 	struct ib_cq ibcq;
148 	struct irdma_sc_cq sc_cq;
149 	u16 cq_head;
150 	u16 cq_size;
151 	u16 cq_num;
152 	bool user_mode;
153 	bool armed;
154 	enum irdma_cmpl_notify last_notify;
155 	u32 polled_cmpls;
156 	u32 cq_mem_size;
157 	struct irdma_dma_mem kmem;
158 	struct irdma_dma_mem kmem_shadow;
159 	struct completion free_cq;
160 	atomic_t refcnt;
161 	spinlock_t lock; /* for poll cq */
162 	struct irdma_pbl *iwpbl;
163 	struct irdma_pbl *iwpbl_shadow;
164 	struct list_head resize_list;
165 	struct irdma_cq_poll_info cur_cqe;
166 	struct list_head cmpl_generated;
167 };
168 
169 struct irdma_cmpl_gen {
170 	struct list_head list;
171 	struct irdma_cq_poll_info cpi;
172 };
173 
174 struct disconn_work {
175 	struct work_struct work;
176 	struct irdma_qp *iwqp;
177 };
178 
179 struct iw_cm_id;
180 
181 struct irdma_qp_kmode {
182 	struct irdma_dma_mem dma_mem;
183 	u32 *sig_trk_mem;
184 	struct irdma_sq_uk_wr_trk_info *sq_wrid_mem;
185 	u64 *rq_wrid_mem;
186 };
187 
188 struct irdma_qp {
189 	struct ib_qp ibqp;
190 	struct irdma_sc_qp sc_qp;
191 	struct irdma_device *iwdev;
192 	struct irdma_cq *iwscq;
193 	struct irdma_cq *iwrcq;
194 	struct irdma_pd *iwpd;
195 	struct rdma_user_mmap_entry *push_wqe_mmap_entry;
196 	struct rdma_user_mmap_entry *push_db_mmap_entry;
197 	struct irdma_qp_host_ctx_info ctx_info;
198 	union {
199 		struct irdma_iwarp_offload_info iwarp_info;
200 		struct irdma_roce_offload_info roce_info;
201 	};
202 
203 	union {
204 		struct irdma_tcp_offload_info tcp_info;
205 		struct irdma_udp_offload_info udp_info;
206 	};
207 
208 	struct irdma_ah roce_ah;
209 	struct list_head teardown_entry;
210 	atomic_t refcnt;
211 	struct iw_cm_id *cm_id;
212 	struct irdma_cm_node *cm_node;
213 	struct delayed_work dwork_flush;
214 	struct ib_mr *lsmm_mr;
215 	atomic_t hw_mod_qp_pend;
216 	enum ib_qp_state ibqp_state;
217 	u32 qp_mem_size;
218 	u32 last_aeq;
219 	int max_send_wr;
220 	int max_recv_wr;
221 	atomic_t close_timer_started;
222 	spinlock_t lock; /* serialize posting WRs to SQ/RQ */
223 	struct irdma_qp_context *iwqp_context;
224 	void *pbl_vbase;
225 	dma_addr_t pbl_pbase;
226 	struct page *page;
227 	u8 active_conn : 1;
228 	u8 user_mode : 1;
229 	u8 hte_added : 1;
230 	u8 flush_issued : 1;
231 	u8 sig_all : 1;
232 	u8 pau_mode : 1;
233 	u8 rsvd : 1;
234 	u8 iwarp_state;
235 	u16 term_sq_flush_code;
236 	u16 term_rq_flush_code;
237 	u8 hw_iwarp_state;
238 	u8 hw_tcp_state;
239 	struct irdma_qp_kmode kqp;
240 	struct irdma_dma_mem host_ctx;
241 	struct timer_list terminate_timer;
242 	struct irdma_pbl *iwpbl;
243 	struct irdma_sge *sg_list;
244 	struct irdma_dma_mem q2_ctx_mem;
245 	struct irdma_dma_mem ietf_mem;
246 	struct completion free_qp;
247 	wait_queue_head_t waitq;
248 	wait_queue_head_t mod_qp_waitq;
249 	u8 rts_ae_rcvd;
250 };
251 
252 enum irdma_mmap_flag {
253 	IRDMA_MMAP_IO_NC,
254 	IRDMA_MMAP_IO_WC,
255 };
256 
257 struct irdma_user_mmap_entry {
258 	struct rdma_user_mmap_entry rdma_entry;
259 	u64 bar_offset;
260 	u8 mmap_flag;
261 };
262 
263 static inline u16 irdma_fw_major_ver(struct irdma_sc_dev *dev)
264 {
265 	return (u16)RS_64(dev->feature_info[IRDMA_FEATURE_FW_INFO], IRDMA_FW_VER_MAJOR);
266 }
267 
268 static inline u16 irdma_fw_minor_ver(struct irdma_sc_dev *dev)
269 {
270 	return (u16)RS_64(dev->feature_info[IRDMA_FEATURE_FW_INFO], IRDMA_FW_VER_MINOR);
271 }
272 
273 /**
274  * irdma_mcast_mac_v4 - Get the multicast MAC for an IP address
275  * @ip_addr: IPv4 address
276  * @mac: pointer to result MAC address
277  *
278  */
279 static inline void irdma_mcast_mac_v4(u32 *ip_addr, u8 *mac)
280 {
281 	u8 *ip = (u8 *)ip_addr;
282 	unsigned char mac4[ETH_ALEN] = {0x01, 0x00, 0x5E, ip[2] & 0x7F, ip[1],
283 					ip[0]};
284 
285 	ether_addr_copy(mac, mac4);
286 }
287 
288 /**
289  * irdma_mcast_mac_v6 - Get the multicast MAC for an IP address
290  * @ip_addr: IPv6 address
291  * @mac: pointer to result MAC address
292  *
293  */
294 static inline void irdma_mcast_mac_v6(u32 *ip_addr, u8 *mac)
295 {
296 	u8 *ip = (u8 *)ip_addr;
297 	unsigned char mac6[ETH_ALEN] = {0x33, 0x33, ip[3], ip[2], ip[1], ip[0]};
298 
299 	ether_addr_copy(mac, mac6);
300 }
301 
302 struct rdma_user_mmap_entry*
303 irdma_user_mmap_entry_insert(struct irdma_ucontext *ucontext, u64 bar_offset,
304 			     enum irdma_mmap_flag mmap_flag, u64 *mmap_offset);
305 int irdma_ib_register_device(struct irdma_device *iwdev);
306 void irdma_ib_unregister_device(struct irdma_device *iwdev);
307 void irdma_ib_dealloc_device(struct ib_device *ibdev);
308 void irdma_ib_qp_event(struct irdma_qp *iwqp, enum irdma_qp_event_type event);
309 void irdma_generate_flush_completions(struct irdma_qp *iwqp);
310 void irdma_remove_cmpls_list(struct irdma_cq *iwcq);
311 int irdma_generated_cmpls(struct irdma_cq *iwcq, struct irdma_cq_poll_info *cq_poll_info);
312 void irdma_flush_worker(struct work_struct *work);
313 #endif /* IRDMA_VERBS_H */
314