27cf33cdb94969d3d7f6a47c5fe6c5346bb268e5
[dpdk.git] / drivers / common / 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 #include <stdlib.h>
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 #include <rte_config.h>
18
19 #include "mlx5_glue.h"
20
21 static int
22 mlx5_glue_fork_init(void)
23 {
24         return ibv_fork_init();
25 }
26
27 static struct ibv_pd *
28 mlx5_glue_alloc_pd(struct ibv_context *context)
29 {
30         return ibv_alloc_pd(context);
31 }
32
33 static int
34 mlx5_glue_dealloc_pd(struct ibv_pd *pd)
35 {
36         return ibv_dealloc_pd(pd);
37 }
38
39 static struct ibv_device **
40 mlx5_glue_get_device_list(int *num_devices)
41 {
42         return ibv_get_device_list(num_devices);
43 }
44
45 static void
46 mlx5_glue_free_device_list(struct ibv_device **list)
47 {
48         ibv_free_device_list(list);
49 }
50
51 static struct ibv_context *
52 mlx5_glue_open_device(struct ibv_device *device)
53 {
54         return ibv_open_device(device);
55 }
56
57 static int
58 mlx5_glue_close_device(struct ibv_context *context)
59 {
60         return ibv_close_device(context);
61 }
62
63 static int
64 mlx5_glue_query_device(struct ibv_context *context,
65                        struct ibv_device_attr *device_attr)
66 {
67         return ibv_query_device(context, device_attr);
68 }
69
70 static int
71 mlx5_glue_query_device_ex(struct ibv_context *context,
72                           const struct ibv_query_device_ex_input *input,
73                           struct ibv_device_attr_ex *attr)
74 {
75         return ibv_query_device_ex(context, input, attr);
76 }
77
78 static int
79 mlx5_glue_query_rt_values_ex(struct ibv_context *context,
80                           struct ibv_values_ex *values)
81 {
82         return ibv_query_rt_values_ex(context, values);
83 }
84
85 static int
86 mlx5_glue_query_port(struct ibv_context *context, uint8_t port_num,
87                      struct ibv_port_attr *port_attr)
88 {
89         return ibv_query_port(context, port_num, port_attr);
90 }
91
92 static struct ibv_comp_channel *
93 mlx5_glue_create_comp_channel(struct ibv_context *context)
94 {
95         return ibv_create_comp_channel(context);
96 }
97
98 static int
99 mlx5_glue_destroy_comp_channel(struct ibv_comp_channel *channel)
100 {
101         return ibv_destroy_comp_channel(channel);
102 }
103
104 static struct ibv_cq *
105 mlx5_glue_create_cq(struct ibv_context *context, int cqe, void *cq_context,
106                     struct ibv_comp_channel *channel, int comp_vector)
107 {
108         return ibv_create_cq(context, cqe, cq_context, channel, comp_vector);
109 }
110
111 static int
112 mlx5_glue_destroy_cq(struct ibv_cq *cq)
113 {
114         return ibv_destroy_cq(cq);
115 }
116
117 static int
118 mlx5_glue_get_cq_event(struct ibv_comp_channel *channel, struct ibv_cq **cq,
119                        void **cq_context)
120 {
121         return ibv_get_cq_event(channel, cq, cq_context);
122 }
123
124 static void
125 mlx5_glue_ack_cq_events(struct ibv_cq *cq, unsigned int nevents)
126 {
127         ibv_ack_cq_events(cq, nevents);
128 }
129
130 static struct ibv_rwq_ind_table *
131 mlx5_glue_create_rwq_ind_table(struct ibv_context *context,
132                                struct ibv_rwq_ind_table_init_attr *init_attr)
133 {
134         return ibv_create_rwq_ind_table(context, init_attr);
135 }
136
137 static int
138 mlx5_glue_destroy_rwq_ind_table(struct ibv_rwq_ind_table *rwq_ind_table)
139 {
140         return ibv_destroy_rwq_ind_table(rwq_ind_table);
141 }
142
143 static struct ibv_wq *
144 mlx5_glue_create_wq(struct ibv_context *context,
145                     struct ibv_wq_init_attr *wq_init_attr)
146 {
147         return ibv_create_wq(context, wq_init_attr);
148 }
149
150 static int
151 mlx5_glue_destroy_wq(struct ibv_wq *wq)
152 {
153         return ibv_destroy_wq(wq);
154 }
155 static int
156 mlx5_glue_modify_wq(struct ibv_wq *wq, struct ibv_wq_attr *wq_attr)
157 {
158         return ibv_modify_wq(wq, wq_attr);
159 }
160
161 static struct ibv_flow *
162 mlx5_glue_create_flow(struct ibv_qp *qp, struct ibv_flow_attr *flow)
163 {
164         return ibv_create_flow(qp, flow);
165 }
166
167 static int
168 mlx5_glue_destroy_flow(struct ibv_flow *flow_id)
169 {
170         return ibv_destroy_flow(flow_id);
171 }
172
173 static int
174 mlx5_glue_destroy_flow_action(void *action)
175 {
176 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
177 #ifdef HAVE_MLX5DV_DR
178         return mlx5dv_dr_action_destroy(action);
179 #else
180         struct mlx5dv_flow_action_attr *attr = action;
181         int res = 0;
182         switch (attr->type) {
183         case MLX5DV_FLOW_ACTION_TAG:
184                 break;
185         default:
186                 res = ibv_destroy_flow_action(attr->action);
187                 break;
188         }
189         free(action);
190         return res;
191 #endif
192 #else
193         (void)action;
194         return ENOTSUP;
195 #endif
196 }
197
198 static struct ibv_qp *
199 mlx5_glue_create_qp(struct ibv_pd *pd, struct ibv_qp_init_attr *qp_init_attr)
200 {
201         return ibv_create_qp(pd, qp_init_attr);
202 }
203
204 static struct ibv_qp *
205 mlx5_glue_create_qp_ex(struct ibv_context *context,
206                        struct ibv_qp_init_attr_ex *qp_init_attr_ex)
207 {
208         return ibv_create_qp_ex(context, qp_init_attr_ex);
209 }
210
211 static int
212 mlx5_glue_destroy_qp(struct ibv_qp *qp)
213 {
214         return ibv_destroy_qp(qp);
215 }
216
217 static int
218 mlx5_glue_modify_qp(struct ibv_qp *qp, struct ibv_qp_attr *attr, int attr_mask)
219 {
220         return ibv_modify_qp(qp, attr, attr_mask);
221 }
222
223 static struct ibv_mr *
224 mlx5_glue_reg_mr(struct ibv_pd *pd, void *addr, size_t length, int access)
225 {
226         return ibv_reg_mr(pd, addr, length, access);
227 }
228
229 static struct ibv_mr *
230 mlx5_glue_alloc_null_mr(struct ibv_pd *pd)
231 {
232 #ifdef HAVE_IBV_DEVX_OBJ
233         return ibv_alloc_null_mr(pd);
234 #else
235         (void)pd;
236         errno = ENOTSUP;
237         return NULL;
238 #endif
239 }
240
241 static int
242 mlx5_glue_dereg_mr(struct ibv_mr *mr)
243 {
244         return ibv_dereg_mr(mr);
245 }
246
247 static struct ibv_counter_set *
248 mlx5_glue_create_counter_set(struct ibv_context *context,
249                              struct ibv_counter_set_init_attr *init_attr)
250 {
251 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V42
252         (void)context;
253         (void)init_attr;
254         return NULL;
255 #else
256         return ibv_create_counter_set(context, init_attr);
257 #endif
258 }
259
260 static int
261 mlx5_glue_destroy_counter_set(struct ibv_counter_set *cs)
262 {
263 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V42
264         (void)cs;
265         return ENOTSUP;
266 #else
267         return ibv_destroy_counter_set(cs);
268 #endif
269 }
270
271 static int
272 mlx5_glue_describe_counter_set(struct ibv_context *context,
273                                uint16_t counter_set_id,
274                                struct ibv_counter_set_description *cs_desc)
275 {
276 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V42
277         (void)context;
278         (void)counter_set_id;
279         (void)cs_desc;
280         return ENOTSUP;
281 #else
282         return ibv_describe_counter_set(context, counter_set_id, cs_desc);
283 #endif
284 }
285
286 static int
287 mlx5_glue_query_counter_set(struct ibv_query_counter_set_attr *query_attr,
288                             struct ibv_counter_set_data *cs_data)
289 {
290 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V42
291         (void)query_attr;
292         (void)cs_data;
293         return ENOTSUP;
294 #else
295         return ibv_query_counter_set(query_attr, cs_data);
296 #endif
297 }
298
299 static struct ibv_counters *
300 mlx5_glue_create_counters(struct ibv_context *context,
301                           struct ibv_counters_init_attr *init_attr)
302 {
303 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V45
304         (void)context;
305         (void)init_attr;
306         errno = ENOTSUP;
307         return NULL;
308 #else
309         return ibv_create_counters(context, init_attr);
310 #endif
311 }
312
313 static int
314 mlx5_glue_destroy_counters(struct ibv_counters *counters)
315 {
316 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V45
317         (void)counters;
318         return ENOTSUP;
319 #else
320         return ibv_destroy_counters(counters);
321 #endif
322 }
323
324 static int
325 mlx5_glue_attach_counters(struct ibv_counters *counters,
326                           struct ibv_counter_attach_attr *attr,
327                           struct ibv_flow *flow)
328 {
329 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V45
330         (void)counters;
331         (void)attr;
332         (void)flow;
333         return ENOTSUP;
334 #else
335         return ibv_attach_counters_point_flow(counters, attr, flow);
336 #endif
337 }
338
339 static int
340 mlx5_glue_query_counters(struct ibv_counters *counters,
341                          uint64_t *counters_value,
342                          uint32_t ncounters,
343                          uint32_t flags)
344 {
345 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V45
346         (void)counters;
347         (void)counters_value;
348         (void)ncounters;
349         (void)flags;
350         return ENOTSUP;
351 #else
352         return ibv_read_counters(counters, counters_value, ncounters, flags);
353 #endif
354 }
355
356 static void
357 mlx5_glue_ack_async_event(struct ibv_async_event *event)
358 {
359         ibv_ack_async_event(event);
360 }
361
362 static int
363 mlx5_glue_get_async_event(struct ibv_context *context,
364                           struct ibv_async_event *event)
365 {
366         return ibv_get_async_event(context, event);
367 }
368
369 static const char *
370 mlx5_glue_port_state_str(enum ibv_port_state port_state)
371 {
372         return ibv_port_state_str(port_state);
373 }
374
375 static struct ibv_cq *
376 mlx5_glue_cq_ex_to_cq(struct ibv_cq_ex *cq)
377 {
378         return ibv_cq_ex_to_cq(cq);
379 }
380
381 static void *
382 mlx5_glue_dr_create_flow_action_dest_flow_tbl(void *tbl)
383 {
384 #ifdef HAVE_MLX5DV_DR
385         return mlx5dv_dr_action_create_dest_table(tbl);
386 #else
387         (void)tbl;
388         errno = ENOTSUP;
389         return NULL;
390 #endif
391 }
392
393 static void *
394 mlx5_glue_dr_create_flow_action_dest_port(void *domain, uint32_t port)
395 {
396 #ifdef HAVE_MLX5DV_DR_DEVX_PORT
397         return mlx5dv_dr_action_create_dest_ib_port(domain, port);
398 #else
399 #ifdef HAVE_MLX5DV_DR_ESWITCH
400         return mlx5dv_dr_action_create_dest_vport(domain, port);
401 #else
402         (void)domain;
403         (void)port;
404         errno = ENOTSUP;
405         return NULL;
406 #endif
407 #endif
408 }
409
410 static void *
411 mlx5_glue_dr_create_flow_action_drop(void)
412 {
413 #ifdef HAVE_MLX5DV_DR_ESWITCH
414         return mlx5dv_dr_action_create_drop();
415 #else
416         errno = ENOTSUP;
417         return NULL;
418 #endif
419 }
420
421 static void *
422 mlx5_glue_dr_create_flow_action_push_vlan(struct mlx5dv_dr_domain *domain,
423                                           rte_be32_t vlan_tag)
424 {
425 #ifdef HAVE_MLX5DV_DR_VLAN
426         return mlx5dv_dr_action_create_push_vlan(domain, vlan_tag);
427 #else
428         (void)domain;
429         (void)vlan_tag;
430         errno = ENOTSUP;
431         return NULL;
432 #endif
433 }
434
435 static void *
436 mlx5_glue_dr_create_flow_action_pop_vlan(void)
437 {
438 #ifdef HAVE_MLX5DV_DR_VLAN
439         return mlx5dv_dr_action_create_pop_vlan();
440 #else
441         errno = ENOTSUP;
442         return NULL;
443 #endif
444 }
445
446 static void *
447 mlx5_glue_dr_create_flow_tbl(void *domain, uint32_t level)
448 {
449 #ifdef HAVE_MLX5DV_DR
450         return mlx5dv_dr_table_create(domain, level);
451 #else
452         (void)domain;
453         (void)level;
454         errno = ENOTSUP;
455         return NULL;
456 #endif
457 }
458
459 static int
460 mlx5_glue_dr_destroy_flow_tbl(void *tbl)
461 {
462 #ifdef HAVE_MLX5DV_DR
463         return mlx5dv_dr_table_destroy(tbl);
464 #else
465         (void)tbl;
466         errno = ENOTSUP;
467         return errno;
468 #endif
469 }
470
471 static void *
472 mlx5_glue_dr_create_domain(struct ibv_context *ctx,
473                            enum  mlx5dv_dr_domain_type domain)
474 {
475 #ifdef HAVE_MLX5DV_DR
476         return mlx5dv_dr_domain_create(ctx, domain);
477 #else
478         (void)ctx;
479         (void)domain;
480         errno = ENOTSUP;
481         return NULL;
482 #endif
483 }
484
485 static int
486 mlx5_glue_dr_destroy_domain(void *domain)
487 {
488 #ifdef HAVE_MLX5DV_DR
489         return mlx5dv_dr_domain_destroy(domain);
490 #else
491         (void)domain;
492         errno = ENOTSUP;
493         return errno;
494 #endif
495 }
496
497 static struct ibv_cq_ex *
498 mlx5_glue_dv_create_cq(struct ibv_context *context,
499                        struct ibv_cq_init_attr_ex *cq_attr,
500                        struct mlx5dv_cq_init_attr *mlx5_cq_attr)
501 {
502         return mlx5dv_create_cq(context, cq_attr, mlx5_cq_attr);
503 }
504
505 static struct ibv_wq *
506 mlx5_glue_dv_create_wq(struct ibv_context *context,
507                        struct ibv_wq_init_attr *wq_attr,
508                        struct mlx5dv_wq_init_attr *mlx5_wq_attr)
509 {
510 #ifndef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
511         (void)context;
512         (void)wq_attr;
513         (void)mlx5_wq_attr;
514         errno = ENOTSUP;
515         return NULL;
516 #else
517         return mlx5dv_create_wq(context, wq_attr, mlx5_wq_attr);
518 #endif
519 }
520
521 static int
522 mlx5_glue_dv_query_device(struct ibv_context *ctx,
523                           struct mlx5dv_context *attrs_out)
524 {
525         return mlx5dv_query_device(ctx, attrs_out);
526 }
527
528 static int
529 mlx5_glue_dv_set_context_attr(struct ibv_context *ibv_ctx,
530                               enum mlx5dv_set_ctx_attr_type type, void *attr)
531 {
532         return mlx5dv_set_context_attr(ibv_ctx, type, attr);
533 }
534
535 static int
536 mlx5_glue_dv_init_obj(struct mlx5dv_obj *obj, uint64_t obj_type)
537 {
538         return mlx5dv_init_obj(obj, obj_type);
539 }
540
541 static struct ibv_qp *
542 mlx5_glue_dv_create_qp(struct ibv_context *context,
543                        struct ibv_qp_init_attr_ex *qp_init_attr_ex,
544                        struct mlx5dv_qp_init_attr *dv_qp_init_attr)
545 {
546 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
547         return mlx5dv_create_qp(context, qp_init_attr_ex, dv_qp_init_attr);
548 #else
549         (void)context;
550         (void)qp_init_attr_ex;
551         (void)dv_qp_init_attr;
552         errno = ENOTSUP;
553         return NULL;
554 #endif
555 }
556
557 static void *
558 mlx5_glue_dv_create_flow_matcher(struct ibv_context *context,
559                                  struct mlx5dv_flow_matcher_attr *matcher_attr,
560                                  void *tbl)
561 {
562 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
563 #ifdef HAVE_MLX5DV_DR
564         (void)context;
565         return mlx5dv_dr_matcher_create(tbl, matcher_attr->priority,
566                                         matcher_attr->match_criteria_enable,
567                                         matcher_attr->match_mask);
568 #else
569         (void)tbl;
570         return mlx5dv_create_flow_matcher(context, matcher_attr);
571 #endif
572 #else
573         (void)context;
574         (void)matcher_attr;
575         (void)tbl;
576         errno = ENOTSUP;
577         return NULL;
578 #endif
579 }
580
581 static void *
582 mlx5_glue_dv_create_flow(void *matcher,
583                          void *match_value,
584                          size_t num_actions,
585                          void *actions[])
586 {
587 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
588 #ifdef HAVE_MLX5DV_DR
589         return mlx5dv_dr_rule_create(matcher, match_value, num_actions,
590                                      (struct mlx5dv_dr_action **)actions);
591 #else
592         struct mlx5dv_flow_action_attr actions_attr[8];
593
594         if (num_actions > 8)
595                 return NULL;
596         for (size_t i = 0; i < num_actions; i++)
597                 actions_attr[i] =
598                         *((struct mlx5dv_flow_action_attr *)(actions[i]));
599         return mlx5dv_create_flow(matcher, match_value,
600                                   num_actions, actions_attr);
601 #endif
602 #else
603         (void)matcher;
604         (void)match_value;
605         (void)num_actions;
606         (void)actions;
607         return NULL;
608 #endif
609 }
610
611 static void *
612 mlx5_glue_dv_create_flow_action_counter(void *counter_obj, uint32_t offset)
613 {
614 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
615 #ifdef HAVE_MLX5DV_DR
616         return mlx5dv_dr_action_create_flow_counter(counter_obj, offset);
617 #else
618         struct mlx5dv_flow_action_attr *action;
619
620         (void)offset;
621         action = malloc(sizeof(*action));
622         if (!action)
623                 return NULL;
624         action->type = MLX5DV_FLOW_ACTION_COUNTERS_DEVX;
625         action->obj = counter_obj;
626         return action;
627 #endif
628 #else
629         (void)counter_obj;
630         (void)offset;
631         errno = ENOTSUP;
632         return NULL;
633 #endif
634 }
635
636 static void *
637 mlx5_glue_dv_create_flow_action_dest_ibv_qp(void *qp)
638 {
639 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
640 #ifdef HAVE_MLX5DV_DR
641         return mlx5dv_dr_action_create_dest_ibv_qp(qp);
642 #else
643         struct mlx5dv_flow_action_attr *action;
644
645         action = malloc(sizeof(*action));
646         if (!action)
647                 return NULL;
648         action->type = MLX5DV_FLOW_ACTION_DEST_IBV_QP;
649         action->obj = qp;
650         return action;
651 #endif
652 #else
653         (void)qp;
654         errno = ENOTSUP;
655         return NULL;
656 #endif
657 }
658
659 static void *
660 mlx5_glue_dv_create_flow_action_dest_devx_tir(void *tir)
661 {
662 #ifdef HAVE_MLX5DV_DR_ACTION_DEST_DEVX_TIR
663         return mlx5dv_dr_action_create_dest_devx_tir(tir);
664 #else
665         (void)tir;
666         errno = ENOTSUP;
667         return NULL;
668 #endif
669 }
670
671 static void *
672 mlx5_glue_dv_create_flow_action_modify_header
673                                         (struct ibv_context *ctx,
674                                          enum mlx5dv_flow_table_type ft_type,
675                                          void *domain, uint64_t flags,
676                                          size_t actions_sz,
677                                          uint64_t actions[])
678 {
679 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
680 #ifdef HAVE_MLX5DV_DR
681         (void)ctx;
682         (void)ft_type;
683         return mlx5dv_dr_action_create_modify_header(domain, flags, actions_sz,
684                                                      (__be64 *)actions);
685 #else
686         struct mlx5dv_flow_action_attr *action;
687
688         (void)domain;
689         (void)flags;
690         action = malloc(sizeof(*action));
691         if (!action)
692                 return NULL;
693         action->type = MLX5DV_FLOW_ACTION_IBV_FLOW_ACTION;
694         action->action = mlx5dv_create_flow_action_modify_header
695                 (ctx, actions_sz, actions, ft_type);
696         return action;
697 #endif
698 #else
699         (void)ctx;
700         (void)ft_type;
701         (void)domain;
702         (void)flags;
703         (void)actions_sz;
704         (void)actions;
705         errno = ENOTSUP;
706         return NULL;
707 #endif
708 }
709
710 static void *
711 mlx5_glue_dv_create_flow_action_packet_reformat
712                 (struct ibv_context *ctx,
713                  enum mlx5dv_flow_action_packet_reformat_type reformat_type,
714                  enum mlx5dv_flow_table_type ft_type,
715                  struct mlx5dv_dr_domain *domain,
716                  uint32_t flags, size_t data_sz, void *data)
717 {
718 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
719 #ifdef HAVE_MLX5DV_DR
720         (void)ctx;
721         (void)ft_type;
722         return mlx5dv_dr_action_create_packet_reformat(domain, flags,
723                                                        reformat_type, data_sz,
724                                                        data);
725 #else
726         (void)domain;
727         (void)flags;
728         struct mlx5dv_flow_action_attr *action;
729
730         action = malloc(sizeof(*action));
731         if (!action)
732                 return NULL;
733         action->type = MLX5DV_FLOW_ACTION_IBV_FLOW_ACTION;
734         action->action = mlx5dv_create_flow_action_packet_reformat
735                 (ctx, data_sz, data, reformat_type, ft_type);
736         return action;
737 #endif
738 #else
739         (void)ctx;
740         (void)reformat_type;
741         (void)ft_type;
742         (void)domain;
743         (void)flags;
744         (void)data_sz;
745         (void)data;
746         errno = ENOTSUP;
747         return NULL;
748 #endif
749 }
750
751 static void *
752 mlx5_glue_dv_create_flow_action_tag(uint32_t tag)
753 {
754 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
755 #ifdef HAVE_MLX5DV_DR
756         return mlx5dv_dr_action_create_tag(tag);
757 #else
758         struct mlx5dv_flow_action_attr *action;
759         action = malloc(sizeof(*action));
760         if (!action)
761                 return NULL;
762         action->type = MLX5DV_FLOW_ACTION_TAG;
763         action->tag_value = tag;
764         return action;
765 #endif
766 #endif
767         (void)tag;
768         errno = ENOTSUP;
769         return NULL;
770 }
771
772 static void *
773 mlx5_glue_dv_create_flow_action_meter(struct mlx5dv_dr_flow_meter_attr *attr)
774 {
775 #if defined(HAVE_MLX5DV_DR) && defined(HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER)
776         return mlx5dv_dr_action_create_flow_meter(attr);
777 #else
778         (void)attr;
779         errno = ENOTSUP;
780         return NULL;
781 #endif
782 }
783
784 static int
785 mlx5_glue_dv_modify_flow_action_meter(void *action,
786                                       struct mlx5dv_dr_flow_meter_attr *attr,
787                                       uint64_t modify_bits)
788 {
789 #if defined(HAVE_MLX5DV_DR) && defined(HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER)
790         return mlx5dv_dr_action_modify_flow_meter(action, attr, modify_bits);
791 #else
792         (void)action;
793         (void)attr;
794         (void)modify_bits;
795         errno = ENOTSUP;
796         return errno;
797 #endif
798 }
799
800 static int
801 mlx5_glue_dv_destroy_flow(void *flow_id)
802 {
803 #ifdef HAVE_MLX5DV_DR
804         return mlx5dv_dr_rule_destroy(flow_id);
805 #else
806         return ibv_destroy_flow(flow_id);
807 #endif
808 }
809
810 static int
811 mlx5_glue_dv_destroy_flow_matcher(void *matcher)
812 {
813 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
814 #ifdef HAVE_MLX5DV_DR
815         return mlx5dv_dr_matcher_destroy(matcher);
816 #else
817         return mlx5dv_destroy_flow_matcher(matcher);
818 #endif
819 #else
820         (void)matcher;
821         errno = ENOTSUP;
822         return errno;
823 #endif
824 }
825
826 static struct ibv_context *
827 mlx5_glue_dv_open_device(struct ibv_device *device)
828 {
829 #ifdef HAVE_IBV_DEVX_OBJ
830         return mlx5dv_open_device(device,
831                                   &(struct mlx5dv_context_attr){
832                                         .flags = MLX5DV_CONTEXT_FLAGS_DEVX,
833                                   });
834 #else
835         (void)device;
836         errno = ENOTSUP;
837         return NULL;
838 #endif
839 }
840
841 static struct mlx5dv_devx_obj *
842 mlx5_glue_devx_obj_create(struct ibv_context *ctx,
843                           const void *in, size_t inlen,
844                           void *out, size_t outlen)
845 {
846 #ifdef HAVE_IBV_DEVX_OBJ
847         return mlx5dv_devx_obj_create(ctx, in, inlen, out, outlen);
848 #else
849         (void)ctx;
850         (void)in;
851         (void)inlen;
852         (void)out;
853         (void)outlen;
854         errno = ENOTSUP;
855         return NULL;
856 #endif
857 }
858
859 static int
860 mlx5_glue_devx_obj_destroy(struct mlx5dv_devx_obj *obj)
861 {
862 #ifdef HAVE_IBV_DEVX_OBJ
863         return mlx5dv_devx_obj_destroy(obj);
864 #else
865         (void)obj;
866         return -ENOTSUP;
867 #endif
868 }
869
870 static int
871 mlx5_glue_devx_obj_query(struct mlx5dv_devx_obj *obj,
872                          const void *in, size_t inlen,
873                          void *out, size_t outlen)
874 {
875 #ifdef HAVE_IBV_DEVX_OBJ
876         return mlx5dv_devx_obj_query(obj, in, inlen, out, outlen);
877 #else
878         (void)obj;
879         (void)in;
880         (void)inlen;
881         (void)out;
882         (void)outlen;
883         return -ENOTSUP;
884 #endif
885 }
886
887 static int
888 mlx5_glue_devx_obj_modify(struct mlx5dv_devx_obj *obj,
889                           const void *in, size_t inlen,
890                           void *out, size_t outlen)
891 {
892 #ifdef HAVE_IBV_DEVX_OBJ
893         return mlx5dv_devx_obj_modify(obj, in, inlen, out, outlen);
894 #else
895         (void)obj;
896         (void)in;
897         (void)inlen;
898         (void)out;
899         (void)outlen;
900         return -ENOTSUP;
901 #endif
902 }
903
904 static int
905 mlx5_glue_devx_general_cmd(struct ibv_context *ctx,
906                            const void *in, size_t inlen,
907                            void *out, size_t outlen)
908 {
909 #ifdef HAVE_IBV_DEVX_OBJ
910         return mlx5dv_devx_general_cmd(ctx, in, inlen, out, outlen);
911 #else
912         (void)ctx;
913         (void)in;
914         (void)inlen;
915         (void)out;
916         (void)outlen;
917         return -ENOTSUP;
918 #endif
919 }
920
921 static struct mlx5dv_devx_cmd_comp *
922 mlx5_glue_devx_create_cmd_comp(struct ibv_context *ctx)
923 {
924 #ifdef HAVE_IBV_DEVX_ASYNC
925         return mlx5dv_devx_create_cmd_comp(ctx);
926 #else
927         (void)ctx;
928         errno = -ENOTSUP;
929         return NULL;
930 #endif
931 }
932
933 static void
934 mlx5_glue_devx_destroy_cmd_comp(struct mlx5dv_devx_cmd_comp *cmd_comp)
935 {
936 #ifdef HAVE_IBV_DEVX_ASYNC
937         mlx5dv_devx_destroy_cmd_comp(cmd_comp);
938 #else
939         (void)cmd_comp;
940         errno = -ENOTSUP;
941 #endif
942 }
943
944 static int
945 mlx5_glue_devx_obj_query_async(struct mlx5dv_devx_obj *obj, const void *in,
946                                size_t inlen, size_t outlen, uint64_t wr_id,
947                                struct mlx5dv_devx_cmd_comp *cmd_comp)
948 {
949 #ifdef HAVE_IBV_DEVX_ASYNC
950         return mlx5dv_devx_obj_query_async(obj, in, inlen, outlen, wr_id,
951                                            cmd_comp);
952 #else
953         (void)obj;
954         (void)in;
955         (void)inlen;
956         (void)outlen;
957         (void)wr_id;
958         (void)cmd_comp;
959         return -ENOTSUP;
960 #endif
961 }
962
963 static int
964 mlx5_glue_devx_get_async_cmd_comp(struct mlx5dv_devx_cmd_comp *cmd_comp,
965                                   struct mlx5dv_devx_async_cmd_hdr *cmd_resp,
966                                   size_t cmd_resp_len)
967 {
968 #ifdef HAVE_IBV_DEVX_ASYNC
969         return mlx5dv_devx_get_async_cmd_comp(cmd_comp, cmd_resp,
970                                               cmd_resp_len);
971 #else
972         (void)cmd_comp;
973         (void)cmd_resp;
974         (void)cmd_resp_len;
975         return -ENOTSUP;
976 #endif
977 }
978
979 static struct mlx5dv_devx_umem *
980 mlx5_glue_devx_umem_reg(struct ibv_context *context, void *addr, size_t size,
981                         uint32_t access)
982 {
983 #ifdef HAVE_IBV_DEVX_OBJ
984         return mlx5dv_devx_umem_reg(context, addr, size, access);
985 #else
986         (void)context;
987         (void)addr;
988         (void)size;
989         (void)access;
990         errno = -ENOTSUP;
991         return NULL;
992 #endif
993 }
994
995 static int
996 mlx5_glue_devx_umem_dereg(struct mlx5dv_devx_umem *dv_devx_umem)
997 {
998 #ifdef HAVE_IBV_DEVX_OBJ
999         return mlx5dv_devx_umem_dereg(dv_devx_umem);
1000 #else
1001         (void)dv_devx_umem;
1002         return -ENOTSUP;
1003 #endif
1004 }
1005
1006 static int
1007 mlx5_glue_devx_qp_query(struct ibv_qp *qp,
1008                         const void *in, size_t inlen,
1009                         void *out, size_t outlen)
1010 {
1011 #ifdef HAVE_IBV_DEVX_OBJ
1012         return mlx5dv_devx_qp_query(qp, in, inlen, out, outlen);
1013 #else
1014         (void)qp;
1015         (void)in;
1016         (void)inlen;
1017         (void)out;
1018         (void)outlen;
1019         errno = ENOTSUP;
1020         return errno;
1021 #endif
1022 }
1023
1024 static int
1025 mlx5_glue_devx_port_query(struct ibv_context *ctx,
1026                           uint32_t port_num,
1027                           struct mlx5dv_devx_port *mlx5_devx_port)
1028 {
1029 #ifdef HAVE_MLX5DV_DR_DEVX_PORT
1030         return mlx5dv_query_devx_port(ctx, port_num, mlx5_devx_port);
1031 #else
1032         (void)ctx;
1033         (void)port_num;
1034         (void)mlx5_devx_port;
1035         errno = ENOTSUP;
1036         return errno;
1037 #endif
1038 }
1039
1040 static int
1041 mlx5_glue_dr_dump_domain(FILE *file, void *domain)
1042 {
1043 #ifdef HAVE_MLX5_DR_FLOW_DUMP
1044         return mlx5dv_dump_dr_domain(file, domain);
1045 #else
1046         RTE_SET_USED(file);
1047         RTE_SET_USED(domain);
1048         return -ENOTSUP;
1049 #endif
1050 }
1051
1052 static int
1053 mlx5_glue_devx_query_eqn(struct ibv_context *ctx, uint32_t cpus,
1054                          uint32_t *eqn)
1055 {
1056 #ifdef HAVE_IBV_DEVX_OBJ
1057         return mlx5dv_devx_query_eqn(ctx, cpus, eqn);
1058 #else
1059         (void)ctx;
1060         (void)cpus;
1061         (void)eqn;
1062         return -ENOTSUP;
1063 #endif
1064 }
1065
1066 static struct mlx5dv_devx_event_channel *
1067 mlx5_glue_devx_create_event_channel(struct ibv_context *ctx, int flags)
1068 {
1069 #ifdef HAVE_IBV_DEVX_EVENT
1070         return mlx5dv_devx_create_event_channel(ctx, flags);
1071 #else
1072         (void)ctx;
1073         (void)flags;
1074         errno = ENOTSUP;
1075         return NULL;
1076 #endif
1077 }
1078
1079 static void
1080 mlx5_glue_devx_destroy_event_channel(struct mlx5dv_devx_event_channel *eventc)
1081 {
1082 #ifdef HAVE_IBV_DEVX_EVENT
1083         mlx5dv_devx_destroy_event_channel(eventc);
1084 #else
1085         (void)eventc;
1086 #endif
1087 }
1088
1089 static int
1090 mlx5_glue_devx_subscribe_devx_event(struct mlx5dv_devx_event_channel *eventc,
1091                                     struct mlx5dv_devx_obj *obj,
1092                                     uint16_t events_sz, uint16_t events_num[],
1093                                     uint64_t cookie)
1094 {
1095 #ifdef HAVE_IBV_DEVX_EVENT
1096         return mlx5dv_devx_subscribe_devx_event(eventc, obj, events_sz,
1097                                                 events_num, cookie);
1098 #else
1099         (void)eventc;
1100         (void)obj;
1101         (void)events_sz;
1102         (void)events_num;
1103         (void)cookie;
1104         return -ENOTSUP;
1105 #endif
1106 }
1107
1108 static int
1109 mlx5_glue_devx_subscribe_devx_event_fd(struct mlx5dv_devx_event_channel *eventc,
1110                                        int fd, struct mlx5dv_devx_obj *obj,
1111                                        uint16_t event_num)
1112 {
1113 #ifdef HAVE_IBV_DEVX_EVENT
1114         return mlx5dv_devx_subscribe_devx_event_fd(eventc, fd, obj, event_num);
1115 #else
1116         (void)eventc;
1117         (void)fd;
1118         (void)obj;
1119         (void)event_num;
1120         return -ENOTSUP;
1121 #endif
1122 }
1123
1124 static ssize_t
1125 mlx5_glue_devx_get_event(struct mlx5dv_devx_event_channel *eventc,
1126                          struct mlx5dv_devx_async_event_hdr *event_data,
1127                          size_t event_resp_len)
1128 {
1129 #ifdef HAVE_IBV_DEVX_EVENT
1130         return mlx5dv_devx_get_event(eventc, event_data, event_resp_len);
1131 #else
1132         (void)eventc;
1133         (void)event_data;
1134         (void)event_resp_len;
1135         errno = ENOTSUP;
1136         return -1;
1137 #endif
1138 }
1139
1140 static struct mlx5dv_devx_uar *
1141 mlx5_glue_devx_alloc_uar(struct ibv_context *context, uint32_t flags)
1142 {
1143 #ifdef HAVE_IBV_DEVX_OBJ
1144         return mlx5dv_devx_alloc_uar(context, flags);
1145 #else
1146         (void)context;
1147         (void)flags;
1148         errno = ENOTSUP;
1149         return NULL;
1150 #endif
1151 }
1152
1153 static void
1154 mlx5_glue_devx_free_uar(struct mlx5dv_devx_uar *devx_uar)
1155 {
1156 #ifdef HAVE_IBV_DEVX_OBJ
1157         mlx5dv_devx_free_uar(devx_uar);
1158 #else
1159         (void)devx_uar;
1160 #endif
1161 }
1162
1163 static struct mlx5dv_var *
1164 mlx5_glue_dv_alloc_var(struct ibv_context *context, uint32_t flags)
1165 {
1166 #ifdef HAVE_IBV_VAR
1167         return mlx5dv_alloc_var(context, flags);
1168 #else
1169         (void)context;
1170         (void)flags;
1171         errno = ENOTSUP;
1172         return NULL;
1173 #endif
1174 }
1175
1176 static void
1177 mlx5_glue_dv_free_var(struct mlx5dv_var *var)
1178 {
1179 #ifdef HAVE_IBV_VAR
1180         mlx5dv_free_var(var);
1181 #else
1182         (void)var;
1183         errno = ENOTSUP;
1184 #endif
1185 }
1186
1187 alignas(RTE_CACHE_LINE_SIZE)
1188 const struct mlx5_glue *mlx5_glue = &(const struct mlx5_glue){
1189         .version = MLX5_GLUE_VERSION,
1190         .fork_init = mlx5_glue_fork_init,
1191         .alloc_pd = mlx5_glue_alloc_pd,
1192         .dealloc_pd = mlx5_glue_dealloc_pd,
1193         .get_device_list = mlx5_glue_get_device_list,
1194         .free_device_list = mlx5_glue_free_device_list,
1195         .open_device = mlx5_glue_open_device,
1196         .close_device = mlx5_glue_close_device,
1197         .query_device = mlx5_glue_query_device,
1198         .query_device_ex = mlx5_glue_query_device_ex,
1199         .query_rt_values_ex = mlx5_glue_query_rt_values_ex,
1200         .query_port = mlx5_glue_query_port,
1201         .create_comp_channel = mlx5_glue_create_comp_channel,
1202         .destroy_comp_channel = mlx5_glue_destroy_comp_channel,
1203         .create_cq = mlx5_glue_create_cq,
1204         .destroy_cq = mlx5_glue_destroy_cq,
1205         .get_cq_event = mlx5_glue_get_cq_event,
1206         .ack_cq_events = mlx5_glue_ack_cq_events,
1207         .create_rwq_ind_table = mlx5_glue_create_rwq_ind_table,
1208         .destroy_rwq_ind_table = mlx5_glue_destroy_rwq_ind_table,
1209         .create_wq = mlx5_glue_create_wq,
1210         .destroy_wq = mlx5_glue_destroy_wq,
1211         .modify_wq = mlx5_glue_modify_wq,
1212         .create_flow = mlx5_glue_create_flow,
1213         .destroy_flow = mlx5_glue_destroy_flow,
1214         .destroy_flow_action = mlx5_glue_destroy_flow_action,
1215         .create_qp = mlx5_glue_create_qp,
1216         .create_qp_ex = mlx5_glue_create_qp_ex,
1217         .destroy_qp = mlx5_glue_destroy_qp,
1218         .modify_qp = mlx5_glue_modify_qp,
1219         .reg_mr = mlx5_glue_reg_mr,
1220         .alloc_null_mr = mlx5_glue_alloc_null_mr,
1221         .dereg_mr = mlx5_glue_dereg_mr,
1222         .create_counter_set = mlx5_glue_create_counter_set,
1223         .destroy_counter_set = mlx5_glue_destroy_counter_set,
1224         .describe_counter_set = mlx5_glue_describe_counter_set,
1225         .query_counter_set = mlx5_glue_query_counter_set,
1226         .create_counters = mlx5_glue_create_counters,
1227         .destroy_counters = mlx5_glue_destroy_counters,
1228         .attach_counters = mlx5_glue_attach_counters,
1229         .query_counters = mlx5_glue_query_counters,
1230         .ack_async_event = mlx5_glue_ack_async_event,
1231         .get_async_event = mlx5_glue_get_async_event,
1232         .port_state_str = mlx5_glue_port_state_str,
1233         .cq_ex_to_cq = mlx5_glue_cq_ex_to_cq,
1234         .dr_create_flow_action_dest_flow_tbl =
1235                 mlx5_glue_dr_create_flow_action_dest_flow_tbl,
1236         .dr_create_flow_action_dest_port =
1237                 mlx5_glue_dr_create_flow_action_dest_port,
1238         .dr_create_flow_action_drop =
1239                 mlx5_glue_dr_create_flow_action_drop,
1240         .dr_create_flow_action_push_vlan =
1241                 mlx5_glue_dr_create_flow_action_push_vlan,
1242         .dr_create_flow_action_pop_vlan =
1243                 mlx5_glue_dr_create_flow_action_pop_vlan,
1244         .dr_create_flow_tbl = mlx5_glue_dr_create_flow_tbl,
1245         .dr_destroy_flow_tbl = mlx5_glue_dr_destroy_flow_tbl,
1246         .dr_create_domain = mlx5_glue_dr_create_domain,
1247         .dr_destroy_domain = mlx5_glue_dr_destroy_domain,
1248         .dv_create_cq = mlx5_glue_dv_create_cq,
1249         .dv_create_wq = mlx5_glue_dv_create_wq,
1250         .dv_query_device = mlx5_glue_dv_query_device,
1251         .dv_set_context_attr = mlx5_glue_dv_set_context_attr,
1252         .dv_init_obj = mlx5_glue_dv_init_obj,
1253         .dv_create_qp = mlx5_glue_dv_create_qp,
1254         .dv_create_flow_matcher = mlx5_glue_dv_create_flow_matcher,
1255         .dv_create_flow = mlx5_glue_dv_create_flow,
1256         .dv_create_flow_action_counter =
1257                 mlx5_glue_dv_create_flow_action_counter,
1258         .dv_create_flow_action_dest_ibv_qp =
1259                 mlx5_glue_dv_create_flow_action_dest_ibv_qp,
1260         .dv_create_flow_action_dest_devx_tir =
1261                 mlx5_glue_dv_create_flow_action_dest_devx_tir,
1262         .dv_create_flow_action_modify_header =
1263                 mlx5_glue_dv_create_flow_action_modify_header,
1264         .dv_create_flow_action_packet_reformat =
1265                 mlx5_glue_dv_create_flow_action_packet_reformat,
1266         .dv_create_flow_action_tag =  mlx5_glue_dv_create_flow_action_tag,
1267         .dv_create_flow_action_meter = mlx5_glue_dv_create_flow_action_meter,
1268         .dv_modify_flow_action_meter = mlx5_glue_dv_modify_flow_action_meter,
1269         .dv_destroy_flow = mlx5_glue_dv_destroy_flow,
1270         .dv_destroy_flow_matcher = mlx5_glue_dv_destroy_flow_matcher,
1271         .dv_open_device = mlx5_glue_dv_open_device,
1272         .devx_obj_create = mlx5_glue_devx_obj_create,
1273         .devx_obj_destroy = mlx5_glue_devx_obj_destroy,
1274         .devx_obj_query = mlx5_glue_devx_obj_query,
1275         .devx_obj_modify = mlx5_glue_devx_obj_modify,
1276         .devx_general_cmd = mlx5_glue_devx_general_cmd,
1277         .devx_create_cmd_comp = mlx5_glue_devx_create_cmd_comp,
1278         .devx_destroy_cmd_comp = mlx5_glue_devx_destroy_cmd_comp,
1279         .devx_obj_query_async = mlx5_glue_devx_obj_query_async,
1280         .devx_get_async_cmd_comp = mlx5_glue_devx_get_async_cmd_comp,
1281         .devx_umem_reg = mlx5_glue_devx_umem_reg,
1282         .devx_umem_dereg = mlx5_glue_devx_umem_dereg,
1283         .devx_qp_query = mlx5_glue_devx_qp_query,
1284         .devx_port_query = mlx5_glue_devx_port_query,
1285         .dr_dump_domain = mlx5_glue_dr_dump_domain,
1286         .devx_query_eqn = mlx5_glue_devx_query_eqn,
1287         .devx_create_event_channel = mlx5_glue_devx_create_event_channel,
1288         .devx_destroy_event_channel = mlx5_glue_devx_destroy_event_channel,
1289         .devx_subscribe_devx_event = mlx5_glue_devx_subscribe_devx_event,
1290         .devx_subscribe_devx_event_fd = mlx5_glue_devx_subscribe_devx_event_fd,
1291         .devx_get_event = mlx5_glue_devx_get_event,
1292         .devx_alloc_uar = mlx5_glue_devx_alloc_uar,
1293         .devx_free_uar = mlx5_glue_devx_free_uar,
1294         .dv_alloc_var = mlx5_glue_dv_alloc_var,
1295         .dv_free_var = mlx5_glue_dv_free_var,
1296 };