New upstream version 18.11-rc1
[deb_dpdk.git] / drivers / net / mlx5 / mlx5_glue.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018 6WIND S.A.
3  * Copyright 2018 Mellanox Technologies, Ltd
4  */
5
6 #include <errno.h>
7 #include <stdalign.h>
8 #include <stddef.h>
9 #include <stdint.h>
10
11 /*
12  * Not needed by this file; included to work around the lack of off_t
13  * definition for mlx5dv.h with unpatched rdma-core versions.
14  */
15 #include <sys/types.h>
16
17 /* Verbs headers do not support -pedantic. */
18 #ifdef PEDANTIC
19 #pragma GCC diagnostic ignored "-Wpedantic"
20 #endif
21 #include <infiniband/mlx5dv.h>
22 #include <infiniband/verbs.h>
23 #ifdef PEDANTIC
24 #pragma GCC diagnostic error "-Wpedantic"
25 #endif
26
27 #include <rte_config.h>
28
29 #include "mlx5_autoconf.h"
30 #include "mlx5_glue.h"
31
32 static int
33 mlx5_glue_fork_init(void)
34 {
35         return ibv_fork_init();
36 }
37
38 static struct ibv_pd *
39 mlx5_glue_alloc_pd(struct ibv_context *context)
40 {
41         return ibv_alloc_pd(context);
42 }
43
44 static int
45 mlx5_glue_dealloc_pd(struct ibv_pd *pd)
46 {
47         return ibv_dealloc_pd(pd);
48 }
49
50 static struct ibv_device **
51 mlx5_glue_get_device_list(int *num_devices)
52 {
53         return ibv_get_device_list(num_devices);
54 }
55
56 static void
57 mlx5_glue_free_device_list(struct ibv_device **list)
58 {
59         ibv_free_device_list(list);
60 }
61
62 static struct ibv_context *
63 mlx5_glue_open_device(struct ibv_device *device)
64 {
65         return ibv_open_device(device);
66 }
67
68 static int
69 mlx5_glue_close_device(struct ibv_context *context)
70 {
71         return ibv_close_device(context);
72 }
73
74 static int
75 mlx5_glue_query_device(struct ibv_context *context,
76                        struct ibv_device_attr *device_attr)
77 {
78         return ibv_query_device(context, device_attr);
79 }
80
81 static int
82 mlx5_glue_query_device_ex(struct ibv_context *context,
83                           const struct ibv_query_device_ex_input *input,
84                           struct ibv_device_attr_ex *attr)
85 {
86         return ibv_query_device_ex(context, input, attr);
87 }
88
89 static int
90 mlx5_glue_query_port(struct ibv_context *context, uint8_t port_num,
91                      struct ibv_port_attr *port_attr)
92 {
93         return ibv_query_port(context, port_num, port_attr);
94 }
95
96 static struct ibv_comp_channel *
97 mlx5_glue_create_comp_channel(struct ibv_context *context)
98 {
99         return ibv_create_comp_channel(context);
100 }
101
102 static int
103 mlx5_glue_destroy_comp_channel(struct ibv_comp_channel *channel)
104 {
105         return ibv_destroy_comp_channel(channel);
106 }
107
108 static struct ibv_cq *
109 mlx5_glue_create_cq(struct ibv_context *context, int cqe, void *cq_context,
110                     struct ibv_comp_channel *channel, int comp_vector)
111 {
112         return ibv_create_cq(context, cqe, cq_context, channel, comp_vector);
113 }
114
115 static int
116 mlx5_glue_destroy_cq(struct ibv_cq *cq)
117 {
118         return ibv_destroy_cq(cq);
119 }
120
121 static int
122 mlx5_glue_get_cq_event(struct ibv_comp_channel *channel, struct ibv_cq **cq,
123                        void **cq_context)
124 {
125         return ibv_get_cq_event(channel, cq, cq_context);
126 }
127
128 static void
129 mlx5_glue_ack_cq_events(struct ibv_cq *cq, unsigned int nevents)
130 {
131         ibv_ack_cq_events(cq, nevents);
132 }
133
134 static struct ibv_rwq_ind_table *
135 mlx5_glue_create_rwq_ind_table(struct ibv_context *context,
136                                struct ibv_rwq_ind_table_init_attr *init_attr)
137 {
138         return ibv_create_rwq_ind_table(context, init_attr);
139 }
140
141 static int
142 mlx5_glue_destroy_rwq_ind_table(struct ibv_rwq_ind_table *rwq_ind_table)
143 {
144         return ibv_destroy_rwq_ind_table(rwq_ind_table);
145 }
146
147 static struct ibv_wq *
148 mlx5_glue_create_wq(struct ibv_context *context,
149                     struct ibv_wq_init_attr *wq_init_attr)
150 {
151         return ibv_create_wq(context, wq_init_attr);
152 }
153
154 static int
155 mlx5_glue_destroy_wq(struct ibv_wq *wq)
156 {
157         return ibv_destroy_wq(wq);
158 }
159 static int
160 mlx5_glue_modify_wq(struct ibv_wq *wq, struct ibv_wq_attr *wq_attr)
161 {
162         return ibv_modify_wq(wq, wq_attr);
163 }
164
165 static struct ibv_flow *
166 mlx5_glue_create_flow(struct ibv_qp *qp, struct ibv_flow_attr *flow)
167 {
168         return ibv_create_flow(qp, flow);
169 }
170
171 static int
172 mlx5_glue_destroy_flow(struct ibv_flow *flow_id)
173 {
174         return ibv_destroy_flow(flow_id);
175 }
176
177 static struct ibv_qp *
178 mlx5_glue_create_qp(struct ibv_pd *pd, struct ibv_qp_init_attr *qp_init_attr)
179 {
180         return ibv_create_qp(pd, qp_init_attr);
181 }
182
183 static struct ibv_qp *
184 mlx5_glue_create_qp_ex(struct ibv_context *context,
185                        struct ibv_qp_init_attr_ex *qp_init_attr_ex)
186 {
187         return ibv_create_qp_ex(context, qp_init_attr_ex);
188 }
189
190 static int
191 mlx5_glue_destroy_qp(struct ibv_qp *qp)
192 {
193         return ibv_destroy_qp(qp);
194 }
195
196 static int
197 mlx5_glue_modify_qp(struct ibv_qp *qp, struct ibv_qp_attr *attr, int attr_mask)
198 {
199         return ibv_modify_qp(qp, attr, attr_mask);
200 }
201
202 static struct ibv_mr *
203 mlx5_glue_reg_mr(struct ibv_pd *pd, void *addr, size_t length, int access)
204 {
205         return ibv_reg_mr(pd, addr, length, access);
206 }
207
208 static int
209 mlx5_glue_dereg_mr(struct ibv_mr *mr)
210 {
211         return ibv_dereg_mr(mr);
212 }
213
214 static struct ibv_counter_set *
215 mlx5_glue_create_counter_set(struct ibv_context *context,
216                              struct ibv_counter_set_init_attr *init_attr)
217 {
218 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V42
219         (void)context;
220         (void)init_attr;
221         return NULL;
222 #else
223         return ibv_create_counter_set(context, init_attr);
224 #endif
225 }
226
227 static int
228 mlx5_glue_destroy_counter_set(struct ibv_counter_set *cs)
229 {
230 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V42
231         (void)cs;
232         return ENOTSUP;
233 #else
234         return ibv_destroy_counter_set(cs);
235 #endif
236 }
237
238 static int
239 mlx5_glue_describe_counter_set(struct ibv_context *context,
240                                uint16_t counter_set_id,
241                                struct ibv_counter_set_description *cs_desc)
242 {
243 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V42
244         (void)context;
245         (void)counter_set_id;
246         (void)cs_desc;
247         return ENOTSUP;
248 #else
249         return ibv_describe_counter_set(context, counter_set_id, cs_desc);
250 #endif
251 }
252
253 static int
254 mlx5_glue_query_counter_set(struct ibv_query_counter_set_attr *query_attr,
255                             struct ibv_counter_set_data *cs_data)
256 {
257 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V42
258         (void)query_attr;
259         (void)cs_data;
260         return ENOTSUP;
261 #else
262         return ibv_query_counter_set(query_attr, cs_data);
263 #endif
264 }
265
266 static struct ibv_counters *
267 mlx5_glue_create_counters(struct ibv_context *context,
268                           struct ibv_counters_init_attr *init_attr)
269 {
270 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V45
271         (void)context;
272         (void)init_attr;
273         return NULL;
274 #else
275         return ibv_create_counters(context, init_attr);
276 #endif
277 }
278
279 static int
280 mlx5_glue_destroy_counters(struct ibv_counters *counters)
281 {
282 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V45
283         (void)counters;
284         return ENOTSUP;
285 #else
286         return ibv_destroy_counters(counters);
287 #endif
288 }
289
290 static int
291 mlx5_glue_attach_counters(struct ibv_counters *counters,
292                           struct ibv_counter_attach_attr *attr,
293                           struct ibv_flow *flow)
294 {
295 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V45
296         (void)counters;
297         (void)attr;
298         (void)flow;
299         return ENOTSUP;
300 #else
301         return ibv_attach_counters_point_flow(counters, attr, flow);
302 #endif
303 }
304
305 static int
306 mlx5_glue_query_counters(struct ibv_counters *counters,
307                          uint64_t *counters_value,
308                          uint32_t ncounters,
309                          uint32_t flags)
310 {
311 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V45
312         (void)counters;
313         (void)counters_value;
314         (void)ncounters;
315         (void)flags;
316         return ENOTSUP;
317 #else
318         return ibv_read_counters(counters, counters_value, ncounters, flags);
319 #endif
320 }
321
322 static void
323 mlx5_glue_ack_async_event(struct ibv_async_event *event)
324 {
325         ibv_ack_async_event(event);
326 }
327
328 static int
329 mlx5_glue_get_async_event(struct ibv_context *context,
330                           struct ibv_async_event *event)
331 {
332         return ibv_get_async_event(context, event);
333 }
334
335 static const char *
336 mlx5_glue_port_state_str(enum ibv_port_state port_state)
337 {
338         return ibv_port_state_str(port_state);
339 }
340
341 static struct ibv_cq *
342 mlx5_glue_cq_ex_to_cq(struct ibv_cq_ex *cq)
343 {
344         return ibv_cq_ex_to_cq(cq);
345 }
346
347 static struct ibv_cq_ex *
348 mlx5_glue_dv_create_cq(struct ibv_context *context,
349                        struct ibv_cq_init_attr_ex *cq_attr,
350                        struct mlx5dv_cq_init_attr *mlx5_cq_attr)
351 {
352         return mlx5dv_create_cq(context, cq_attr, mlx5_cq_attr);
353 }
354
355 static struct ibv_wq *
356 mlx5_glue_dv_create_wq(struct ibv_context *context,
357                        struct ibv_wq_init_attr *wq_attr,
358                        struct mlx5dv_wq_init_attr *mlx5_wq_attr)
359 {
360 #ifndef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
361         (void)context;
362         (void)wq_attr;
363         (void)mlx5_wq_attr;
364         return NULL;
365 #else
366         return mlx5dv_create_wq(context, wq_attr, mlx5_wq_attr);
367 #endif
368 }
369
370 static int
371 mlx5_glue_dv_query_device(struct ibv_context *ctx,
372                           struct mlx5dv_context *attrs_out)
373 {
374         return mlx5dv_query_device(ctx, attrs_out);
375 }
376
377 static int
378 mlx5_glue_dv_set_context_attr(struct ibv_context *ibv_ctx,
379                               enum mlx5dv_set_ctx_attr_type type, void *attr)
380 {
381         return mlx5dv_set_context_attr(ibv_ctx, type, attr);
382 }
383
384 static int
385 mlx5_glue_dv_init_obj(struct mlx5dv_obj *obj, uint64_t obj_type)
386 {
387         return mlx5dv_init_obj(obj, obj_type);
388 }
389
390 static struct ibv_qp *
391 mlx5_glue_dv_create_qp(struct ibv_context *context,
392                        struct ibv_qp_init_attr_ex *qp_init_attr_ex,
393                        struct mlx5dv_qp_init_attr *dv_qp_init_attr)
394 {
395 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
396         return mlx5dv_create_qp(context, qp_init_attr_ex, dv_qp_init_attr);
397 #else
398         (void)context;
399         (void)qp_init_attr_ex;
400         (void)dv_qp_init_attr;
401         return NULL;
402 #endif
403 }
404
405 static struct mlx5dv_flow_matcher *
406 mlx5_glue_dv_create_flow_matcher(struct ibv_context *context,
407                                  struct mlx5dv_flow_matcher_attr *matcher_attr)
408 {
409 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
410         return mlx5dv_create_flow_matcher(context, matcher_attr);
411 #else
412         (void)context;
413         (void)matcher_attr;
414         return NULL;
415 #endif
416 }
417
418 static struct ibv_flow *
419 mlx5_glue_dv_create_flow(struct mlx5dv_flow_matcher *matcher,
420                          struct mlx5dv_flow_match_parameters *match_value,
421                          size_t num_actions,
422                          struct mlx5dv_flow_action_attr *actions_attr)
423 {
424 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
425         return mlx5dv_create_flow(matcher, match_value,
426                                   num_actions, actions_attr);
427 #else
428         (void)matcher;
429         (void)match_value;
430         (void)num_actions;
431         (void)actions_attr;
432         return NULL;
433 #endif
434 }
435
436 static int
437 mlx5_glue_dv_destroy_flow_matcher(struct mlx5dv_flow_matcher *matcher)
438 {
439 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
440         return mlx5dv_destroy_flow_matcher(matcher);
441 #else
442         (void)matcher;
443         return 0;
444 #endif
445 }
446
447 alignas(RTE_CACHE_LINE_SIZE)
448 const struct mlx5_glue *mlx5_glue = &(const struct mlx5_glue){
449         .version = MLX5_GLUE_VERSION,
450         .fork_init = mlx5_glue_fork_init,
451         .alloc_pd = mlx5_glue_alloc_pd,
452         .dealloc_pd = mlx5_glue_dealloc_pd,
453         .get_device_list = mlx5_glue_get_device_list,
454         .free_device_list = mlx5_glue_free_device_list,
455         .open_device = mlx5_glue_open_device,
456         .close_device = mlx5_glue_close_device,
457         .query_device = mlx5_glue_query_device,
458         .query_device_ex = mlx5_glue_query_device_ex,
459         .query_port = mlx5_glue_query_port,
460         .create_comp_channel = mlx5_glue_create_comp_channel,
461         .destroy_comp_channel = mlx5_glue_destroy_comp_channel,
462         .create_cq = mlx5_glue_create_cq,
463         .destroy_cq = mlx5_glue_destroy_cq,
464         .get_cq_event = mlx5_glue_get_cq_event,
465         .ack_cq_events = mlx5_glue_ack_cq_events,
466         .create_rwq_ind_table = mlx5_glue_create_rwq_ind_table,
467         .destroy_rwq_ind_table = mlx5_glue_destroy_rwq_ind_table,
468         .create_wq = mlx5_glue_create_wq,
469         .destroy_wq = mlx5_glue_destroy_wq,
470         .modify_wq = mlx5_glue_modify_wq,
471         .create_flow = mlx5_glue_create_flow,
472         .destroy_flow = mlx5_glue_destroy_flow,
473         .create_qp = mlx5_glue_create_qp,
474         .create_qp_ex = mlx5_glue_create_qp_ex,
475         .destroy_qp = mlx5_glue_destroy_qp,
476         .modify_qp = mlx5_glue_modify_qp,
477         .reg_mr = mlx5_glue_reg_mr,
478         .dereg_mr = mlx5_glue_dereg_mr,
479         .create_counter_set = mlx5_glue_create_counter_set,
480         .destroy_counter_set = mlx5_glue_destroy_counter_set,
481         .describe_counter_set = mlx5_glue_describe_counter_set,
482         .query_counter_set = mlx5_glue_query_counter_set,
483         .create_counters = mlx5_glue_create_counters,
484         .destroy_counters = mlx5_glue_destroy_counters,
485         .attach_counters = mlx5_glue_attach_counters,
486         .query_counters = mlx5_glue_query_counters,
487         .ack_async_event = mlx5_glue_ack_async_event,
488         .get_async_event = mlx5_glue_get_async_event,
489         .port_state_str = mlx5_glue_port_state_str,
490         .cq_ex_to_cq = mlx5_glue_cq_ex_to_cq,
491         .dv_create_cq = mlx5_glue_dv_create_cq,
492         .dv_create_wq = mlx5_glue_dv_create_wq,
493         .dv_query_device = mlx5_glue_dv_query_device,
494         .dv_set_context_attr = mlx5_glue_dv_set_context_attr,
495         .dv_init_obj = mlx5_glue_dv_init_obj,
496         .dv_create_qp = mlx5_glue_dv_create_qp,
497         .dv_create_flow_matcher = mlx5_glue_dv_create_flow_matcher,
498         .dv_destroy_flow_matcher = mlx5_glue_dv_destroy_flow_matcher,
499         .dv_create_flow = mlx5_glue_dv_create_flow,
500 };