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