xref: /freebsd/contrib/ofed/libbnxtre/verbs.h (revision e9ac41698b2f322d55ccf9da50a3596edb2c1800)
1 /*
2  * Copyright (c) 2024, Broadcom. All rights reserved.  The term
3  * Broadcom refers to Broadcom Limited and/or its subsidiaries.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef __BNXT_RE_VERBS_H__
30 #define __BNXT_RE_VERBS_H__
31 
32 #include <sys/mman.h>
33 
34 #include <netinet/in.h>
35 #include <infiniband/verbs.h>
36 
37 #include <assert.h>
38 #include <errno.h>
39 #include <malloc.h>
40 #include <pthread.h>
41 #include <signal.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 int bnxt_re_query_device(struct ibv_context *ibvctx,
48 			 struct ibv_device_attr *dev_attr);
49 
50 int bnxt_re_query_device_compat(struct ibv_context *ibvctx,
51 				struct ibv_device_attr *dev_attr);
52 
53 int bnxt_re_query_port(struct ibv_context *, uint8_t, struct ibv_port_attr *);
54 
55 struct ibv_pd *bnxt_re_alloc_pd(struct ibv_context *);
56 int bnxt_re_free_pd(struct ibv_pd *);
57 
58 typedef struct ibv_mr VERBS_MR;
59 
60 struct ibv_mr *bnxt_re_reg_mr(struct ibv_pd *, void *, size_t,
61 			      int ibv_access_flags);
62 int bnxt_re_dereg_mr(VERBS_MR*);
63 
64 struct ibv_cq *bnxt_re_create_cq(struct ibv_context *, int,
65 				 struct ibv_comp_channel *, int);
66 int bnxt_re_resize_cq(struct ibv_cq *, int);
67 int bnxt_re_destroy_cq(struct ibv_cq *);
68 int bnxt_re_poll_cq(struct ibv_cq *, int, struct ibv_wc *);
69 void bnxt_re_cq_event(struct ibv_cq *);
70 int bnxt_re_arm_cq(struct ibv_cq *, int);
71 
72 struct ibv_qp *bnxt_re_create_qp(struct ibv_pd *, struct ibv_qp_init_attr *);
73 int bnxt_re_modify_qp(struct ibv_qp *, struct ibv_qp_attr *,
74 		      int ibv_qp_attr_mask);
75 int bnxt_re_query_qp(struct ibv_qp *qp, struct ibv_qp_attr *attr,
76 		     int attr_mask, struct ibv_qp_init_attr *init_attr);
77 int bnxt_re_destroy_qp(struct ibv_qp *);
78 int bnxt_re_post_send(struct ibv_qp *, struct ibv_send_wr *,
79 		      struct ibv_send_wr **);
80 int bnxt_re_post_recv(struct ibv_qp *, struct ibv_recv_wr *,
81 		      struct ibv_recv_wr **);
82 
83 struct ibv_srq *bnxt_re_create_srq(struct ibv_pd *,
84 				   struct ibv_srq_init_attr *);
85 int bnxt_re_modify_srq(struct ibv_srq *, struct ibv_srq_attr *, int);
86 int bnxt_re_destroy_srq(struct ibv_srq *);
87 int bnxt_re_query_srq(struct ibv_srq *ibsrq, struct ibv_srq_attr *attr);
88 int bnxt_re_post_srq_recv(struct ibv_srq *, struct ibv_recv_wr *,
89 			  struct ibv_recv_wr **);
90 
91 struct ibv_ah *bnxt_re_create_ah(struct ibv_pd *, struct ibv_ah_attr *);
92 int bnxt_re_destroy_ah(struct ibv_ah *);
93 
94 int bnxt_re_attach_mcast(struct ibv_qp *, const union ibv_gid *, uint16_t);
95 int bnxt_re_detach_mcast(struct ibv_qp *, const union ibv_gid *, uint16_t);
96 
97 void bnxt_re_async_event(struct ibv_async_event *event);
98 
99 struct bnxt_re_work_compl {
100 	struct bnxt_re_list_node cnode;
101 	struct ibv_wc wc;
102 };
103 
104 static inline uint8_t bnxt_re_get_psne_size(struct bnxt_re_context *cntx)
105 {
106 	return (BNXT_RE_HW_RETX(cntx)) ? sizeof(struct bnxt_re_msns) :
107 					      (cntx->cctx->chip_is_gen_p5_thor2) ?
108 					      sizeof(struct bnxt_re_psns_ext) :
109 					      sizeof(struct bnxt_re_psns);
110 }
111 
112 static inline uint32_t bnxt_re_get_npsn(uint8_t mode, uint32_t nwr,
113 					uint32_t slots)
114 {
115 	return mode == BNXT_RE_WQE_MODE_VARIABLE ? slots : nwr;
116 }
117 
118 static inline bool bnxt_re_is_mqp_ex_supported(struct bnxt_re_context *cntx)
119 {
120 	return cntx->comp_mask & BNXT_RE_COMP_MASK_UCNTX_MQP_EX_SUPPORTED;
121 }
122 
123 static inline bool can_request_ppp(struct bnxt_re_qp *re_qp,
124 				   struct ibv_qp_attr *attr, int attr_mask)
125 {
126 	struct bnxt_re_context *cntx;
127 	struct bnxt_re_qp *qp;
128 	bool request = false;
129 
130 	qp = re_qp;
131 	cntx = qp->cntx;
132 	if (!qp->push_st_en && cntx->udpi.wcdpi && (attr_mask & IBV_QP_STATE) &&
133 	    qp->qpst == IBV_QPS_RESET && attr->qp_state == IBV_QPS_INIT) {
134 		request = true;
135 	}
136 	return request;
137 }
138 
139 static inline uint64_t bnxt_re_update_msn_tbl(uint32_t st_idx, uint32_t npsn, uint32_t start_psn)
140 {
141 	return htole64((((uint64_t)(st_idx) << BNXT_RE_SQ_MSN_SEARCH_START_IDX_SHIFT) &
142 		 BNXT_RE_SQ_MSN_SEARCH_START_IDX_MASK) |
143 		 (((uint64_t)(npsn) << BNXT_RE_SQ_MSN_SEARCH_NEXT_PSN_SHIFT) &
144 		 BNXT_RE_SQ_MSN_SEARCH_NEXT_PSN_MASK) |
145 		 (((start_psn) << BNXT_RE_SQ_MSN_SEARCH_START_PSN_SHIFT) &
146 		 BNXT_RE_SQ_MSN_SEARCH_START_PSN_MASK));
147 }
148 
149 static inline int ibv_cmd_modify_qp_compat(struct ibv_qp *ibvqp,
150 					   struct ibv_qp_attr *attr,
151 					   int attr_mask, bool issue_mqp_ex,
152 					   struct bnxt_re_modify_ex_req *mreq,
153 					   struct bnxt_re_modify_ex_resp *mresp)
154 {
155 	int rc;
156 
157 	if (issue_mqp_ex) {
158 		struct bnxt_re_modify_ex_resp *resp;
159 		struct bnxt_re_modify_ex_req *req;
160 
161 		req = mreq;
162 		resp = mresp;
163 		rc = ibv_cmd_modify_qp_ex(ibvqp, attr, attr_mask, &req->cmd,
164 					  sizeof(req->cmd), sizeof(*req),
165 					  &resp->resp, sizeof(resp->resp),
166 					  sizeof(*resp));
167 	} else {
168 		struct ibv_modify_qp cmd = {};
169 
170 		rc = ibv_cmd_modify_qp(ibvqp, attr, attr_mask,
171 				       &cmd, sizeof(cmd));
172 	}
173 	return rc;
174 }
175 
176 #define bnxt_re_is_zero_len_pkt(len, opcd)	(len == 0)
177 #define BNXT_RE_MSN_IDX(m) (((m) & BNXT_RE_SQ_MSN_SEARCH_START_IDX_MASK) >> \
178 		BNXT_RE_SQ_MSN_SEARCH_START_IDX_SHIFT)
179 #define BNXT_RE_MSN_NPSN(m) (((m) & BNXT_RE_SQ_MSN_SEARCH_NEXT_PSN_MASK) >> \
180 		BNXT_RE_SQ_MSN_SEARCH_NEXT_PSN_SHIFT)
181 #define BNXT_RE_MSN_SPSN(m) (((m) & BNXT_RE_SQ_MSN_SEARCH_START_PSN_MASK) >> \
182 		BNXT_RE_SQ_MSN_SEARCH_START_PSN_SHIFT)
183 
184 #endif /* __BNXT_RE_VERBS_H__ */
185