-
Notifications
You must be signed in to change notification settings - Fork 653
/
hns_roce_u.h
359 lines (296 loc) · 9.56 KB
/
hns_roce_u.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Copyright (c) 2016-2017 Hisilicon Limited.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* OpenIB.org BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef _HNS_ROCE_U_H
#define _HNS_ROCE_U_H
#include <stddef.h>
#include <endian.h>
#include <util/compiler.h>
#include <infiniband/driver.h>
#include <util/udma_barrier.h>
#include <util/util.h>
#include <infiniband/verbs.h>
#include <ccan/array_size.h>
#include <ccan/bitmap.h>
#include <ccan/container_of.h>
#define HNS_ROCE_HW_VER1 ('h' << 24 | 'i' << 16 | '0' << 8 | '6')
#define HNS_ROCE_HW_VER2 ('h' << 24 | 'i' << 16 | '0' << 8 | '8')
#define PFX "hns: "
/* The minimum page size is 4K for hardware */
#define HNS_HW_PAGE_SHIFT 12
#define HNS_HW_PAGE_SIZE (1 << HNS_HW_PAGE_SHIFT)
#define HNS_ROCE_MAX_INLINE_DATA_LEN 32
#define HNS_ROCE_MAX_CQ_NUM 0x10000
#define HNS_ROCE_MAX_SRQWQE_NUM 0x8000
#define HNS_ROCE_MAX_SRQSGE_NUM 0x100
#define HNS_ROCE_MIN_CQE_NUM 0x40
#define HNS_ROCE_V1_MIN_WQE_NUM 0x20
#define HNS_ROCE_V2_MIN_WQE_NUM 0x40
#define HNS_ROCE_CQE_ENTRY_SIZE 0x20
#define HNS_ROCE_SQWQE_SHIFT 6
#define HNS_ROCE_SGE_IN_WQE 2
#define HNS_ROCE_SGE_SIZE 16
#define HNS_ROCE_SGE_SHIFT 4
#define HNS_ROCE_GID_SIZE 16
#define HNS_ROCE_CQ_DB_BUF_SIZE ((HNS_ROCE_MAX_CQ_NUM >> 11) << 12)
#define HNS_ROCE_STATIC_RATE 3 /* Gbps */
#define HNS_ROCE_ADDRESS_MASK 0xFFFFFFFF
#define HNS_ROCE_ADDRESS_SHIFT 32
#define roce_get_field(origin, mask, shift) \
(((le32toh(origin)) & (mask)) >> (shift))
#define roce_get_bit(origin, shift) \
roce_get_field((origin), (1ul << (shift)), (shift))
#define roce_set_field(origin, mask, shift, val) \
do { \
(origin) &= ~htole32(mask); \
(origin) |= htole32(((unsigned int)(val) << (shift)) & (mask)); \
} while (0)
#define roce_set_bit(origin, shift, val) \
roce_set_field((origin), (1ul << (shift)), (shift), (val))
#define hr_ilog32(n) ilog32((n) - 1)
enum {
HNS_ROCE_QP_TABLE_BITS = 8,
HNS_ROCE_QP_TABLE_SIZE = 1 << HNS_ROCE_QP_TABLE_BITS,
};
/* operation type list */
enum {
/* rq&srq operation */
HNS_ROCE_OPCODE_SEND_DATA_RECEIVE = 0x06,
HNS_ROCE_OPCODE_RDMA_WITH_IMM_RECEIVE = 0x07,
};
struct hns_roce_device {
struct verbs_device ibv_dev;
int page_size;
const struct hns_roce_u_hw *u_hw;
int hw_version;
};
struct hns_roce_buf {
void *buf;
unsigned int length;
};
#define BIT_CNT_PER_BYTE 8
#define BIT_CNT_PER_LONG (BIT_CNT_PER_BYTE * sizeof(unsigned long))
/* the sw doorbell type; */
enum hns_roce_db_type {
HNS_ROCE_QP_TYPE_DB,
HNS_ROCE_CQ_TYPE_DB,
HNS_ROCE_DB_TYPE_NUM
};
struct hns_roce_db_page {
struct hns_roce_db_page *prev, *next;
struct hns_roce_buf buf;
unsigned int num_db;
unsigned int use_cnt;
bitmap *bitmap;
};
struct hns_roce_context {
struct verbs_context ibv_ctx;
void *uar;
pthread_spinlock_t uar_lock;
void *cq_tptr_base;
struct {
struct hns_roce_qp **table;
int refcnt;
} qp_table[HNS_ROCE_QP_TABLE_SIZE];
pthread_mutex_t qp_table_mutex;
int num_qps;
int qp_table_shift;
int qp_table_mask;
struct hns_roce_db_page *db_list[HNS_ROCE_DB_TYPE_NUM];
pthread_mutex_t db_list_mutex;
unsigned int max_qp_wr;
unsigned int max_sge;
int max_cqe;
};
struct hns_roce_pd {
struct ibv_pd ibv_pd;
unsigned int pdn;
};
struct hns_roce_cq {
struct ibv_cq ibv_cq;
struct hns_roce_buf buf;
pthread_spinlock_t lock;
unsigned int cqn;
unsigned int cq_depth;
unsigned int cons_index;
unsigned int *set_ci_db;
unsigned int *arm_db;
int arm_sn;
unsigned long flags;
};
struct hns_roce_idx_que {
struct hns_roce_buf buf;
int entry_shift;
unsigned long *bitmap;
int bitmap_cnt;
};
struct hns_roce_srq {
struct verbs_srq verbs_srq;
struct hns_roce_buf buf;
pthread_spinlock_t lock;
unsigned long *wrid;
unsigned int srqn;
unsigned int wqe_cnt;
unsigned int max_gs;
unsigned int wqe_shift;
int head;
int tail;
unsigned int *db;
unsigned short counter;
struct hns_roce_idx_que idx_que;
};
struct hns_roce_wq {
unsigned long *wrid;
pthread_spinlock_t lock;
unsigned int wqe_cnt;
int max_post;
unsigned int head;
unsigned int tail;
unsigned int max_gs;
unsigned int wqe_shift;
unsigned int shift; /* wq size is 2^shift */
int offset;
};
/* record the result of sge process */
struct hns_roce_sge_info {
unsigned int valid_num; /* sge length is not 0 */
unsigned int start_idx; /* start position of extend sge */
unsigned int total_len; /* total length of valid sges */
};
struct hns_roce_sge_ex {
int offset;
unsigned int sge_cnt;
int sge_shift;
};
struct hns_roce_rinl_sge {
void *addr;
unsigned int len;
};
struct hns_roce_rinl_wqe {
struct hns_roce_rinl_sge *sg_list;
unsigned int sge_cnt;
};
struct hns_roce_rinl_buf {
struct hns_roce_rinl_wqe *wqe_list;
unsigned int wqe_cnt;
};
struct hns_roce_qp {
struct ibv_qp ibv_qp;
struct hns_roce_buf buf;
int max_inline_data;
int buf_size;
unsigned int sq_signal_bits;
struct hns_roce_wq sq;
struct hns_roce_wq rq;
unsigned int *rdb;
unsigned int *sdb;
struct hns_roce_sge_ex ex_sge;
unsigned int next_sge;
int port_num;
int sl;
struct hns_roce_rinl_buf rq_rinl_buf;
unsigned long flags;
};
struct hns_roce_u_hw {
uint32_t hw_version;
struct verbs_context_ops hw_ops;
};
/*
* The entries's buffer should be aligned to a multiple of the hardware's
* minimum page size.
*/
static inline unsigned int to_hr_hem_entries_size(int count, int buf_shift)
{
return align(count << buf_shift, HNS_HW_PAGE_SIZE);
}
static inline struct hns_roce_device *to_hr_dev(struct ibv_device *ibv_dev)
{
return container_of(ibv_dev, struct hns_roce_device, ibv_dev.device);
}
static inline struct hns_roce_context *to_hr_ctx(struct ibv_context *ibv_ctx)
{
return container_of(ibv_ctx, struct hns_roce_context, ibv_ctx.context);
}
static inline struct hns_roce_pd *to_hr_pd(struct ibv_pd *ibv_pd)
{
return container_of(ibv_pd, struct hns_roce_pd, ibv_pd);
}
static inline struct hns_roce_cq *to_hr_cq(struct ibv_cq *ibv_cq)
{
return container_of(ibv_cq, struct hns_roce_cq, ibv_cq);
}
static inline struct hns_roce_srq *to_hr_srq(struct ibv_srq *ibv_srq)
{
return container_of(container_of(ibv_srq, struct verbs_srq, srq),
struct hns_roce_srq, verbs_srq);
}
static inline struct hns_roce_qp *to_hr_qp(struct ibv_qp *ibv_qp)
{
return container_of(ibv_qp, struct hns_roce_qp, ibv_qp);
}
int hns_roce_u_query_device(struct ibv_context *context,
struct ibv_device_attr *attr);
int hns_roce_u_query_port(struct ibv_context *context, uint8_t port,
struct ibv_port_attr *attr);
struct ibv_pd *hns_roce_u_alloc_pd(struct ibv_context *context);
int hns_roce_u_free_pd(struct ibv_pd *pd);
struct ibv_mr *hns_roce_u_reg_mr(struct ibv_pd *pd, void *addr, size_t length,
uint64_t hca_va, int access);
int hns_roce_u_rereg_mr(struct verbs_mr *mr, int flags, struct ibv_pd *pd,
void *addr, size_t length, int access);
int hns_roce_u_dereg_mr(struct verbs_mr *mr);
struct ibv_mw *hns_roce_u_alloc_mw(struct ibv_pd *pd, enum ibv_mw_type type);
int hns_roce_u_dealloc_mw(struct ibv_mw *mw);
int hns_roce_u_bind_mw(struct ibv_qp *qp, struct ibv_mw *mw,
struct ibv_mw_bind *mw_bind);
struct ibv_cq *hns_roce_u_create_cq(struct ibv_context *context, int cqe,
struct ibv_comp_channel *channel,
int comp_vector);
int hns_roce_u_modify_cq(struct ibv_cq *cq, struct ibv_modify_cq_attr *attr);
int hns_roce_u_destroy_cq(struct ibv_cq *cq);
void hns_roce_u_cq_event(struct ibv_cq *cq);
struct ibv_srq *hns_roce_u_create_srq(struct ibv_pd *pd,
struct ibv_srq_init_attr *srq_init_attr);
int hns_roce_u_modify_srq(struct ibv_srq *srq, struct ibv_srq_attr *srq_attr,
int srq_attr_mask);
int hns_roce_u_query_srq(struct ibv_srq *srq, struct ibv_srq_attr *srq_attr);
int hns_roce_u_destroy_srq(struct ibv_srq *srq);
struct ibv_qp *hns_roce_u_create_qp(struct ibv_pd *pd,
struct ibv_qp_init_attr *attr);
int hns_roce_u_query_qp(struct ibv_qp *ibqp, struct ibv_qp_attr *attr,
int attr_mask, struct ibv_qp_init_attr *init_attr);
int hns_roce_alloc_buf(struct hns_roce_buf *buf, unsigned int size,
int page_size);
void hns_roce_free_buf(struct hns_roce_buf *buf);
void hns_roce_init_qp_indices(struct hns_roce_qp *qp);
extern const struct hns_roce_u_hw hns_roce_u_hw_v1;
extern const struct hns_roce_u_hw hns_roce_u_hw_v2;
#endif /* _HNS_ROCE_U_H */