1*d6b92ffaSHans Petter Selasky /*
2*d6b92ffaSHans Petter Selasky * Copyright (c) 2006-2016 Chelsio, Inc. All rights reserved.
3*d6b92ffaSHans Petter Selasky *
4*d6b92ffaSHans Petter Selasky * This software is available to you under a choice of one of two
5*d6b92ffaSHans Petter Selasky * licenses. You may choose to be licensed under the terms of the GNU
6*d6b92ffaSHans Petter Selasky * General Public License (GPL) Version 2, available from the file
7*d6b92ffaSHans Petter Selasky * COPYING in the main directory of this source tree, or the
8*d6b92ffaSHans Petter Selasky * OpenIB.org BSD license below:
9*d6b92ffaSHans Petter Selasky *
10*d6b92ffaSHans Petter Selasky * Redistribution and use in source and binary forms, with or
11*d6b92ffaSHans Petter Selasky * without modification, are permitted provided that the following
12*d6b92ffaSHans Petter Selasky * conditions are met:
13*d6b92ffaSHans Petter Selasky *
14*d6b92ffaSHans Petter Selasky * - Redistributions of source code must retain the above
15*d6b92ffaSHans Petter Selasky * copyright notice, this list of conditions and the following
16*d6b92ffaSHans Petter Selasky * disclaimer.
17*d6b92ffaSHans Petter Selasky *
18*d6b92ffaSHans Petter Selasky * - Redistributions in binary form must reproduce the above
19*d6b92ffaSHans Petter Selasky * copyright notice, this list of conditions and the following
20*d6b92ffaSHans Petter Selasky * disclaimer in the documentation and/or other materials
21*d6b92ffaSHans Petter Selasky * provided with the distribution.
22*d6b92ffaSHans Petter Selasky *
23*d6b92ffaSHans Petter Selasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24*d6b92ffaSHans Petter Selasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25*d6b92ffaSHans Petter Selasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26*d6b92ffaSHans Petter Selasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27*d6b92ffaSHans Petter Selasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28*d6b92ffaSHans Petter Selasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29*d6b92ffaSHans Petter Selasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30*d6b92ffaSHans Petter Selasky * SOFTWARE.
31*d6b92ffaSHans Petter Selasky */
32*d6b92ffaSHans Petter Selasky #ifndef IWCH_H
33*d6b92ffaSHans Petter Selasky #define IWCH_H
34*d6b92ffaSHans Petter Selasky
35*d6b92ffaSHans Petter Selasky #include <pthread.h>
36*d6b92ffaSHans Petter Selasky #include <inttypes.h>
37*d6b92ffaSHans Petter Selasky #include <stddef.h>
38*d6b92ffaSHans Petter Selasky #include <string.h>
39*d6b92ffaSHans Petter Selasky #include <syslog.h>
40*d6b92ffaSHans Petter Selasky #include <sys/errno.h>
41*d6b92ffaSHans Petter Selasky #include <sys/time.h>
42*d6b92ffaSHans Petter Selasky #include <infiniband/driver.h>
43*d6b92ffaSHans Petter Selasky #include <infiniband/udma_barrier.h>
44*d6b92ffaSHans Petter Selasky #include <sys/queue.h>
45*d6b92ffaSHans Petter Selasky #include "t4.h"
46*d6b92ffaSHans Petter Selasky
47*d6b92ffaSHans Petter Selasky extern unsigned long c4iw_page_size;
48*d6b92ffaSHans Petter Selasky extern unsigned long c4iw_page_shift;
49*d6b92ffaSHans Petter Selasky extern unsigned long c4iw_page_mask;
50*d6b92ffaSHans Petter Selasky
51*d6b92ffaSHans Petter Selasky struct c4iw_mr;
52*d6b92ffaSHans Petter Selasky
53*d6b92ffaSHans Petter Selasky struct c4iw_dev {
54*d6b92ffaSHans Petter Selasky struct verbs_device ibv_dev;
55*d6b92ffaSHans Petter Selasky unsigned chip_version;
56*d6b92ffaSHans Petter Selasky int max_mr;
57*d6b92ffaSHans Petter Selasky struct c4iw_mr **mmid2ptr;
58*d6b92ffaSHans Petter Selasky int max_qp;
59*d6b92ffaSHans Petter Selasky struct c4iw_qp **qpid2ptr;
60*d6b92ffaSHans Petter Selasky int max_cq;
61*d6b92ffaSHans Petter Selasky struct c4iw_cq **cqid2ptr;
62*d6b92ffaSHans Petter Selasky pthread_spinlock_t lock;
63*d6b92ffaSHans Petter Selasky TAILQ_ENTRY(c4iw_dev) list;
64*d6b92ffaSHans Petter Selasky int abi_version;
65*d6b92ffaSHans Petter Selasky };
66*d6b92ffaSHans Petter Selasky
dev_is_t6(struct c4iw_dev * dev)67*d6b92ffaSHans Petter Selasky static inline int dev_is_t6(struct c4iw_dev *dev)
68*d6b92ffaSHans Petter Selasky {
69*d6b92ffaSHans Petter Selasky return dev->chip_version == CHELSIO_T6;
70*d6b92ffaSHans Petter Selasky }
71*d6b92ffaSHans Petter Selasky
dev_is_t5(struct c4iw_dev * dev)72*d6b92ffaSHans Petter Selasky static inline int dev_is_t5(struct c4iw_dev *dev)
73*d6b92ffaSHans Petter Selasky {
74*d6b92ffaSHans Petter Selasky return dev->chip_version == CHELSIO_T5;
75*d6b92ffaSHans Petter Selasky }
76*d6b92ffaSHans Petter Selasky
dev_is_t4(struct c4iw_dev * dev)77*d6b92ffaSHans Petter Selasky static inline int dev_is_t4(struct c4iw_dev *dev)
78*d6b92ffaSHans Petter Selasky {
79*d6b92ffaSHans Petter Selasky return dev->chip_version == CHELSIO_T4;
80*d6b92ffaSHans Petter Selasky }
81*d6b92ffaSHans Petter Selasky
82*d6b92ffaSHans Petter Selasky struct c4iw_context {
83*d6b92ffaSHans Petter Selasky struct ibv_context ibv_ctx;
84*d6b92ffaSHans Petter Selasky struct t4_dev_status_page *status_page;
85*d6b92ffaSHans Petter Selasky int status_page_size;
86*d6b92ffaSHans Petter Selasky };
87*d6b92ffaSHans Petter Selasky
88*d6b92ffaSHans Petter Selasky struct c4iw_pd {
89*d6b92ffaSHans Petter Selasky struct ibv_pd ibv_pd;
90*d6b92ffaSHans Petter Selasky };
91*d6b92ffaSHans Petter Selasky
92*d6b92ffaSHans Petter Selasky struct c4iw_mr {
93*d6b92ffaSHans Petter Selasky struct ibv_mr ibv_mr;
94*d6b92ffaSHans Petter Selasky uint64_t va_fbo;
95*d6b92ffaSHans Petter Selasky uint32_t len;
96*d6b92ffaSHans Petter Selasky };
97*d6b92ffaSHans Petter Selasky
c4iw_mmid(u32 stag)98*d6b92ffaSHans Petter Selasky static inline u32 c4iw_mmid(u32 stag)
99*d6b92ffaSHans Petter Selasky {
100*d6b92ffaSHans Petter Selasky return (stag >> 8);
101*d6b92ffaSHans Petter Selasky }
102*d6b92ffaSHans Petter Selasky
103*d6b92ffaSHans Petter Selasky struct c4iw_cq {
104*d6b92ffaSHans Petter Selasky struct ibv_cq ibv_cq;
105*d6b92ffaSHans Petter Selasky struct c4iw_dev *rhp;
106*d6b92ffaSHans Petter Selasky struct t4_cq cq;
107*d6b92ffaSHans Petter Selasky pthread_spinlock_t lock;
108*d6b92ffaSHans Petter Selasky #ifdef STALL_DETECTION
109*d6b92ffaSHans Petter Selasky struct timeval time;
110*d6b92ffaSHans Petter Selasky int dumped;
111*d6b92ffaSHans Petter Selasky #endif
112*d6b92ffaSHans Petter Selasky };
113*d6b92ffaSHans Petter Selasky
114*d6b92ffaSHans Petter Selasky struct c4iw_qp {
115*d6b92ffaSHans Petter Selasky struct ibv_qp ibv_qp;
116*d6b92ffaSHans Petter Selasky struct c4iw_dev *rhp;
117*d6b92ffaSHans Petter Selasky struct t4_wq wq;
118*d6b92ffaSHans Petter Selasky pthread_spinlock_t lock;
119*d6b92ffaSHans Petter Selasky int sq_sig_all;
120*d6b92ffaSHans Petter Selasky };
121*d6b92ffaSHans Petter Selasky
122*d6b92ffaSHans Petter Selasky #define to_c4iw_xxx(xxx, type) \
123*d6b92ffaSHans Petter Selasky ((struct c4iw_##type *) \
124*d6b92ffaSHans Petter Selasky ((void *) ib##xxx - offsetof(struct c4iw_##type, ibv_##xxx)))
125*d6b92ffaSHans Petter Selasky
to_c4iw_dev(struct ibv_device * ibdev)126*d6b92ffaSHans Petter Selasky static inline struct c4iw_dev *to_c4iw_dev(struct ibv_device *ibdev)
127*d6b92ffaSHans Petter Selasky {
128*d6b92ffaSHans Petter Selasky return to_c4iw_xxx(dev, dev);
129*d6b92ffaSHans Petter Selasky }
130*d6b92ffaSHans Petter Selasky
to_c4iw_context(struct ibv_context * ibctx)131*d6b92ffaSHans Petter Selasky static inline struct c4iw_context *to_c4iw_context(struct ibv_context *ibctx)
132*d6b92ffaSHans Petter Selasky {
133*d6b92ffaSHans Petter Selasky return to_c4iw_xxx(ctx, context);
134*d6b92ffaSHans Petter Selasky }
135*d6b92ffaSHans Petter Selasky
to_c4iw_pd(struct ibv_pd * ibpd)136*d6b92ffaSHans Petter Selasky static inline struct c4iw_pd *to_c4iw_pd(struct ibv_pd *ibpd)
137*d6b92ffaSHans Petter Selasky {
138*d6b92ffaSHans Petter Selasky return to_c4iw_xxx(pd, pd);
139*d6b92ffaSHans Petter Selasky }
140*d6b92ffaSHans Petter Selasky
to_c4iw_cq(struct ibv_cq * ibcq)141*d6b92ffaSHans Petter Selasky static inline struct c4iw_cq *to_c4iw_cq(struct ibv_cq *ibcq)
142*d6b92ffaSHans Petter Selasky {
143*d6b92ffaSHans Petter Selasky return to_c4iw_xxx(cq, cq);
144*d6b92ffaSHans Petter Selasky }
145*d6b92ffaSHans Petter Selasky
to_c4iw_qp(struct ibv_qp * ibqp)146*d6b92ffaSHans Petter Selasky static inline struct c4iw_qp *to_c4iw_qp(struct ibv_qp *ibqp)
147*d6b92ffaSHans Petter Selasky {
148*d6b92ffaSHans Petter Selasky return to_c4iw_xxx(qp, qp);
149*d6b92ffaSHans Petter Selasky }
150*d6b92ffaSHans Petter Selasky
to_c4iw_mr(struct ibv_mr * ibmr)151*d6b92ffaSHans Petter Selasky static inline struct c4iw_mr *to_c4iw_mr(struct ibv_mr *ibmr)
152*d6b92ffaSHans Petter Selasky {
153*d6b92ffaSHans Petter Selasky return to_c4iw_xxx(mr, mr);
154*d6b92ffaSHans Petter Selasky }
155*d6b92ffaSHans Petter Selasky
get_qhp(struct c4iw_dev * rhp,u32 qid)156*d6b92ffaSHans Petter Selasky static inline struct c4iw_qp *get_qhp(struct c4iw_dev *rhp, u32 qid)
157*d6b92ffaSHans Petter Selasky {
158*d6b92ffaSHans Petter Selasky return rhp->qpid2ptr[qid];
159*d6b92ffaSHans Petter Selasky }
160*d6b92ffaSHans Petter Selasky
get_chp(struct c4iw_dev * rhp,u32 qid)161*d6b92ffaSHans Petter Selasky static inline struct c4iw_cq *get_chp(struct c4iw_dev *rhp, u32 qid)
162*d6b92ffaSHans Petter Selasky {
163*d6b92ffaSHans Petter Selasky return rhp->cqid2ptr[qid];
164*d6b92ffaSHans Petter Selasky }
165*d6b92ffaSHans Petter Selasky
long_log2(unsigned long x)166*d6b92ffaSHans Petter Selasky static inline unsigned long_log2(unsigned long x)
167*d6b92ffaSHans Petter Selasky {
168*d6b92ffaSHans Petter Selasky unsigned r = 0;
169*d6b92ffaSHans Petter Selasky for (x >>= 1; x > 0; x >>= 1)
170*d6b92ffaSHans Petter Selasky r++;
171*d6b92ffaSHans Petter Selasky return r;
172*d6b92ffaSHans Petter Selasky }
173*d6b92ffaSHans Petter Selasky
174*d6b92ffaSHans Petter Selasky int c4iw_query_device(struct ibv_context *context,
175*d6b92ffaSHans Petter Selasky struct ibv_device_attr *attr);
176*d6b92ffaSHans Petter Selasky int c4iw_query_port(struct ibv_context *context, uint8_t port,
177*d6b92ffaSHans Petter Selasky struct ibv_port_attr *attr);
178*d6b92ffaSHans Petter Selasky
179*d6b92ffaSHans Petter Selasky struct ibv_pd *c4iw_alloc_pd(struct ibv_context *context);
180*d6b92ffaSHans Petter Selasky int c4iw_free_pd(struct ibv_pd *pd);
181*d6b92ffaSHans Petter Selasky
182*d6b92ffaSHans Petter Selasky struct ibv_mr *c4iw_reg_mr(struct ibv_pd *pd, void *addr,
183*d6b92ffaSHans Petter Selasky size_t length, int access);
184*d6b92ffaSHans Petter Selasky int c4iw_dereg_mr(struct ibv_mr *mr);
185*d6b92ffaSHans Petter Selasky
186*d6b92ffaSHans Petter Selasky struct ibv_cq *c4iw_create_cq(struct ibv_context *context, int cqe,
187*d6b92ffaSHans Petter Selasky struct ibv_comp_channel *channel,
188*d6b92ffaSHans Petter Selasky int comp_vector);
189*d6b92ffaSHans Petter Selasky int c4iw_resize_cq(struct ibv_cq *cq, int cqe);
190*d6b92ffaSHans Petter Selasky int c4iw_destroy_cq(struct ibv_cq *cq);
191*d6b92ffaSHans Petter Selasky int c4iw_poll_cq(struct ibv_cq *cq, int ne, struct ibv_wc *wc);
192*d6b92ffaSHans Petter Selasky int c4iw_arm_cq(struct ibv_cq *cq, int solicited);
193*d6b92ffaSHans Petter Selasky void c4iw_cq_event(struct ibv_cq *cq);
194*d6b92ffaSHans Petter Selasky void c4iw_init_cq_buf(struct c4iw_cq *cq, int nent);
195*d6b92ffaSHans Petter Selasky
196*d6b92ffaSHans Petter Selasky struct ibv_srq *c4iw_create_srq(struct ibv_pd *pd,
197*d6b92ffaSHans Petter Selasky struct ibv_srq_init_attr *attr);
198*d6b92ffaSHans Petter Selasky int c4iw_modify_srq(struct ibv_srq *srq,
199*d6b92ffaSHans Petter Selasky struct ibv_srq_attr *attr,
200*d6b92ffaSHans Petter Selasky int mask);
201*d6b92ffaSHans Petter Selasky int c4iw_destroy_srq(struct ibv_srq *srq);
202*d6b92ffaSHans Petter Selasky int c4iw_post_srq_recv(struct ibv_srq *ibsrq,
203*d6b92ffaSHans Petter Selasky struct ibv_recv_wr *wr,
204*d6b92ffaSHans Petter Selasky struct ibv_recv_wr **bad_wr);
205*d6b92ffaSHans Petter Selasky
206*d6b92ffaSHans Petter Selasky struct ibv_qp *c4iw_create_qp(struct ibv_pd *pd,
207*d6b92ffaSHans Petter Selasky struct ibv_qp_init_attr *attr);
208*d6b92ffaSHans Petter Selasky int c4iw_modify_qp(struct ibv_qp *qp, struct ibv_qp_attr *attr,
209*d6b92ffaSHans Petter Selasky int attr_mask);
210*d6b92ffaSHans Petter Selasky int c4iw_destroy_qp(struct ibv_qp *qp);
211*d6b92ffaSHans Petter Selasky int c4iw_query_qp(struct ibv_qp *qp,
212*d6b92ffaSHans Petter Selasky struct ibv_qp_attr *attr,
213*d6b92ffaSHans Petter Selasky int attr_mask,
214*d6b92ffaSHans Petter Selasky struct ibv_qp_init_attr *init_attr);
215*d6b92ffaSHans Petter Selasky void c4iw_flush_qp(struct c4iw_qp *qhp);
216*d6b92ffaSHans Petter Selasky void c4iw_flush_qps(struct c4iw_dev *dev);
217*d6b92ffaSHans Petter Selasky int c4iw_post_send(struct ibv_qp *ibqp, struct ibv_send_wr *wr,
218*d6b92ffaSHans Petter Selasky struct ibv_send_wr **bad_wr);
219*d6b92ffaSHans Petter Selasky int c4iw_post_receive(struct ibv_qp *ibqp, struct ibv_recv_wr *wr,
220*d6b92ffaSHans Petter Selasky struct ibv_recv_wr **bad_wr);
221*d6b92ffaSHans Petter Selasky struct ibv_ah *c4iw_create_ah(struct ibv_pd *pd,
222*d6b92ffaSHans Petter Selasky struct ibv_ah_attr *ah_attr);
223*d6b92ffaSHans Petter Selasky int c4iw_destroy_ah(struct ibv_ah *ah);
224*d6b92ffaSHans Petter Selasky int c4iw_attach_mcast(struct ibv_qp *qp, const union ibv_gid *gid,
225*d6b92ffaSHans Petter Selasky uint16_t lid);
226*d6b92ffaSHans Petter Selasky int c4iw_detach_mcast(struct ibv_qp *qp, const union ibv_gid *gid,
227*d6b92ffaSHans Petter Selasky uint16_t lid);
228*d6b92ffaSHans Petter Selasky void c4iw_async_event(struct ibv_async_event *event);
229*d6b92ffaSHans Petter Selasky void c4iw_flush_hw_cq(struct c4iw_cq *chp);
230*d6b92ffaSHans Petter Selasky int c4iw_flush_rq(struct t4_wq *wq, struct t4_cq *cq, int count);
231*d6b92ffaSHans Petter Selasky void c4iw_flush_sq(struct c4iw_qp *qhp);
232*d6b92ffaSHans Petter Selasky void c4iw_count_rcqes(struct t4_cq *cq, struct t4_wq *wq, int *count);
233*d6b92ffaSHans Petter Selasky
234*d6b92ffaSHans Petter Selasky #define FW_MAJ 0
235*d6b92ffaSHans Petter Selasky #define FW_MIN 0
236*d6b92ffaSHans Petter Selasky
align(unsigned long val,unsigned long align)237*d6b92ffaSHans Petter Selasky static inline unsigned long align(unsigned long val, unsigned long align)
238*d6b92ffaSHans Petter Selasky {
239*d6b92ffaSHans Petter Selasky return (val + align - 1) & ~(align - 1);
240*d6b92ffaSHans Petter Selasky }
241*d6b92ffaSHans Petter Selasky
242*d6b92ffaSHans Petter Selasky #ifdef STATS
243*d6b92ffaSHans Petter Selasky
244*d6b92ffaSHans Petter Selasky #define INC_STAT(a) { c4iw_stats.a++; }
245*d6b92ffaSHans Petter Selasky
246*d6b92ffaSHans Petter Selasky struct c4iw_stats {
247*d6b92ffaSHans Petter Selasky unsigned long send;
248*d6b92ffaSHans Petter Selasky unsigned long recv;
249*d6b92ffaSHans Petter Selasky unsigned long read;
250*d6b92ffaSHans Petter Selasky unsigned long write;
251*d6b92ffaSHans Petter Selasky unsigned long arm;
252*d6b92ffaSHans Petter Selasky unsigned long cqe;
253*d6b92ffaSHans Petter Selasky unsigned long mr;
254*d6b92ffaSHans Petter Selasky unsigned long qp;
255*d6b92ffaSHans Petter Selasky unsigned long cq;
256*d6b92ffaSHans Petter Selasky };
257*d6b92ffaSHans Petter Selasky extern struct c4iw_stats c4iw_stats;
258*d6b92ffaSHans Petter Selasky #else
259*d6b92ffaSHans Petter Selasky #define INC_STAT(a)
260*d6b92ffaSHans Petter Selasky #endif
261*d6b92ffaSHans Petter Selasky
262*d6b92ffaSHans Petter Selasky #ifdef STALL_DETECTION
263*d6b92ffaSHans Petter Selasky void dump_state(void);
264*d6b92ffaSHans Petter Selasky extern int stall_to;
265*d6b92ffaSHans Petter Selasky #endif
266*d6b92ffaSHans Petter Selasky
267*d6b92ffaSHans Petter Selasky #endif /* IWCH_H */
268