common/mlx5: introduce common library
[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 int
230 mlx5_glue_dereg_mr(struct ibv_mr *mr)
231 {
232         return ibv_dereg_mr(mr);
233 }
234
235 static struct ibv_counter_set *
236 mlx5_glue_create_counter_set(struct ibv_context *context,
237                              struct ibv_counter_set_init_attr *init_attr)
238 {
239 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V42
240         (void)context;
241         (void)init_attr;
242         return NULL;
243 #else
244         return ibv_create_counter_set(context, init_attr);
245 #endif
246 }
247
248 static int
249 mlx5_glue_destroy_counter_set(struct ibv_counter_set *cs)
250 {
251 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V42
252         (void)cs;
253         return ENOTSUP;
254 #else
255         return ibv_destroy_counter_set(cs);
256 #endif
257 }
258
259 static int
260 mlx5_glue_describe_counter_set(struct ibv_context *context,
261                                uint16_t counter_set_id,
262                                struct ibv_counter_set_description *cs_desc)
263 {
264 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V42
265         (void)context;
266         (void)counter_set_id;
267         (void)cs_desc;
268         return ENOTSUP;
269 #else
270         return ibv_describe_counter_set(context, counter_set_id, cs_desc);
271 #endif
272 }
273
274 static int
275 mlx5_glue_query_counter_set(struct ibv_query_counter_set_attr *query_attr,
276                             struct ibv_counter_set_data *cs_data)
277 {
278 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V42
279         (void)query_attr;
280         (void)cs_data;
281         return ENOTSUP;
282 #else
283         return ibv_query_counter_set(query_attr, cs_data);
284 #endif
285 }
286
287 static struct ibv_counters *
288 mlx5_glue_create_counters(struct ibv_context *context,
289                           struct ibv_counters_init_attr *init_attr)
290 {
291 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V45
292         (void)context;
293         (void)init_attr;
294         errno = ENOTSUP;
295         return NULL;
296 #else
297         return ibv_create_counters(context, init_attr);
298 #endif
299 }
300
301 static int
302 mlx5_glue_destroy_counters(struct ibv_counters *counters)
303 {
304 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V45
305         (void)counters;
306         return ENOTSUP;
307 #else
308         return ibv_destroy_counters(counters);
309 #endif
310 }
311
312 static int
313 mlx5_glue_attach_counters(struct ibv_counters *counters,
314                           struct ibv_counter_attach_attr *attr,
315                           struct ibv_flow *flow)
316 {
317 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V45
318         (void)counters;
319         (void)attr;
320         (void)flow;
321         return ENOTSUP;
322 #else
323         return ibv_attach_counters_point_flow(counters, attr, flow);
324 #endif
325 }
326
327 static int
328 mlx5_glue_query_counters(struct ibv_counters *counters,
329                          uint64_t *counters_value,
330                          uint32_t ncounters,
331                          uint32_t flags)
332 {
333 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_V45
334         (void)counters;
335         (void)counters_value;
336         (void)ncounters;
337         (void)flags;
338         return ENOTSUP;
339 #else
340         return ibv_read_counters(counters, counters_value, ncounters, flags);
341 #endif
342 }
343
344 static void
345 mlx5_glue_ack_async_event(struct ibv_async_event *event)
346 {
347         ibv_ack_async_event(event);
348 }
349
350 static int
351 mlx5_glue_get_async_event(struct ibv_context *context,
352                           struct ibv_async_event *event)
353 {
354         return ibv_get_async_event(context, event);
355 }
356
357 static const char *
358 mlx5_glue_port_state_str(enum ibv_port_state port_state)
359 {
360         return ibv_port_state_str(port_state);
361 }
362
363 static struct ibv_cq *
364 mlx5_glue_cq_ex_to_cq(struct ibv_cq_ex *cq)
365 {
366         return ibv_cq_ex_to_cq(cq);
367 }
368
369 static void *
370 mlx5_glue_dr_create_flow_action_dest_flow_tbl(void *tbl)
371 {
372 #ifdef HAVE_MLX5DV_DR
373         return mlx5dv_dr_action_create_dest_table(tbl);
374 #else
375         (void)tbl;
376         errno = ENOTSUP;
377         return NULL;
378 #endif
379 }
380
381 static void *
382 mlx5_glue_dr_create_flow_action_dest_port(void *domain, uint32_t port)
383 {
384 #ifdef HAVE_MLX5DV_DR_DEVX_PORT
385         return mlx5dv_dr_action_create_dest_ib_port(domain, port);
386 #else
387 #ifdef HAVE_MLX5DV_DR_ESWITCH
388         return mlx5dv_dr_action_create_dest_vport(domain, port);
389 #else
390         (void)domain;
391         (void)port;
392         errno = ENOTSUP;
393         return NULL;
394 #endif
395 #endif
396 }
397
398 static void *
399 mlx5_glue_dr_create_flow_action_drop(void)
400 {
401 #ifdef HAVE_MLX5DV_DR_ESWITCH
402         return mlx5dv_dr_action_create_drop();
403 #else
404         errno = ENOTSUP;
405         return NULL;
406 #endif
407 }
408
409 static void *
410 mlx5_glue_dr_create_flow_action_push_vlan(struct mlx5dv_dr_domain *domain,
411                                           rte_be32_t vlan_tag)
412 {
413 #ifdef HAVE_MLX5DV_DR_VLAN
414         return mlx5dv_dr_action_create_push_vlan(domain, vlan_tag);
415 #else
416         (void)domain;
417         (void)vlan_tag;
418         errno = ENOTSUP;
419         return NULL;
420 #endif
421 }
422
423 static void *
424 mlx5_glue_dr_create_flow_action_pop_vlan(void)
425 {
426 #ifdef HAVE_MLX5DV_DR_VLAN
427         return mlx5dv_dr_action_create_pop_vlan();
428 #else
429         errno = ENOTSUP;
430         return NULL;
431 #endif
432 }
433
434 static void *
435 mlx5_glue_dr_create_flow_tbl(void *domain, uint32_t level)
436 {
437 #ifdef HAVE_MLX5DV_DR
438         return mlx5dv_dr_table_create(domain, level);
439 #else
440         (void)domain;
441         (void)level;
442         errno = ENOTSUP;
443         return NULL;
444 #endif
445 }
446
447 static int
448 mlx5_glue_dr_destroy_flow_tbl(void *tbl)
449 {
450 #ifdef HAVE_MLX5DV_DR
451         return mlx5dv_dr_table_destroy(tbl);
452 #else
453         (void)tbl;
454         errno = ENOTSUP;
455         return errno;
456 #endif
457 }
458
459 static void *
460 mlx5_glue_dr_create_domain(struct ibv_context *ctx,
461                            enum  mlx5dv_dr_domain_type domain)
462 {
463 #ifdef HAVE_MLX5DV_DR
464         return mlx5dv_dr_domain_create(ctx, domain);
465 #else
466         (void)ctx;
467         (void)domain;
468         errno = ENOTSUP;
469         return NULL;
470 #endif
471 }
472
473 static int
474 mlx5_glue_dr_destroy_domain(void *domain)
475 {
476 #ifdef HAVE_MLX5DV_DR
477         return mlx5dv_dr_domain_destroy(domain);
478 #else
479         (void)domain;
480         errno = ENOTSUP;
481         return errno;
482 #endif
483 }
484
485 static struct ibv_cq_ex *
486 mlx5_glue_dv_create_cq(struct ibv_context *context,
487                        struct ibv_cq_init_attr_ex *cq_attr,
488                        struct mlx5dv_cq_init_attr *mlx5_cq_attr)
489 {
490         return mlx5dv_create_cq(context, cq_attr, mlx5_cq_attr);
491 }
492
493 static struct ibv_wq *
494 mlx5_glue_dv_create_wq(struct ibv_context *context,
495                        struct ibv_wq_init_attr *wq_attr,
496                        struct mlx5dv_wq_init_attr *mlx5_wq_attr)
497 {
498 #ifndef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
499         (void)context;
500         (void)wq_attr;
501         (void)mlx5_wq_attr;
502         errno = ENOTSUP;
503         return NULL;
504 #else
505         return mlx5dv_create_wq(context, wq_attr, mlx5_wq_attr);
506 #endif
507 }
508
509 static int
510 mlx5_glue_dv_query_device(struct ibv_context *ctx,
511                           struct mlx5dv_context *attrs_out)
512 {
513         return mlx5dv_query_device(ctx, attrs_out);
514 }
515
516 static int
517 mlx5_glue_dv_set_context_attr(struct ibv_context *ibv_ctx,
518                               enum mlx5dv_set_ctx_attr_type type, void *attr)
519 {
520         return mlx5dv_set_context_attr(ibv_ctx, type, attr);
521 }
522
523 static int
524 mlx5_glue_dv_init_obj(struct mlx5dv_obj *obj, uint64_t obj_type)
525 {
526         return mlx5dv_init_obj(obj, obj_type);
527 }
528
529 static struct ibv_qp *
530 mlx5_glue_dv_create_qp(struct ibv_context *context,
531                        struct ibv_qp_init_attr_ex *qp_init_attr_ex,
532                        struct mlx5dv_qp_init_attr *dv_qp_init_attr)
533 {
534 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
535         return mlx5dv_create_qp(context, qp_init_attr_ex, dv_qp_init_attr);
536 #else
537         (void)context;
538         (void)qp_init_attr_ex;
539         (void)dv_qp_init_attr;
540         errno = ENOTSUP;
541         return NULL;
542 #endif
543 }
544
545 static void *
546 mlx5_glue_dv_create_flow_matcher(struct ibv_context *context,
547                                  struct mlx5dv_flow_matcher_attr *matcher_attr,
548                                  void *tbl)
549 {
550 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
551 #ifdef HAVE_MLX5DV_DR
552         (void)context;
553         return mlx5dv_dr_matcher_create(tbl, matcher_attr->priority,
554                                         matcher_attr->match_criteria_enable,
555                                         matcher_attr->match_mask);
556 #else
557         (void)tbl;
558         return mlx5dv_create_flow_matcher(context, matcher_attr);
559 #endif
560 #else
561         (void)context;
562         (void)matcher_attr;
563         (void)tbl;
564         errno = ENOTSUP;
565         return NULL;
566 #endif
567 }
568
569 static void *
570 mlx5_glue_dv_create_flow(void *matcher,
571                          void *match_value,
572                          size_t num_actions,
573                          void *actions[])
574 {
575 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
576 #ifdef HAVE_MLX5DV_DR
577         return mlx5dv_dr_rule_create(matcher, match_value, num_actions,
578                                      (struct mlx5dv_dr_action **)actions);
579 #else
580         struct mlx5dv_flow_action_attr actions_attr[8];
581
582         if (num_actions > 8)
583                 return NULL;
584         for (size_t i = 0; i < num_actions; i++)
585                 actions_attr[i] =
586                         *((struct mlx5dv_flow_action_attr *)(actions[i]));
587         return mlx5dv_create_flow(matcher, match_value,
588                                   num_actions, actions_attr);
589 #endif
590 #else
591         (void)matcher;
592         (void)match_value;
593         (void)num_actions;
594         (void)actions;
595         return NULL;
596 #endif
597 }
598
599 static void *
600 mlx5_glue_dv_create_flow_action_counter(void *counter_obj, uint32_t offset)
601 {
602 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
603 #ifdef HAVE_MLX5DV_DR
604         return mlx5dv_dr_action_create_flow_counter(counter_obj, offset);
605 #else
606         struct mlx5dv_flow_action_attr *action;
607
608         (void)offset;
609         action = malloc(sizeof(*action));
610         if (!action)
611                 return NULL;
612         action->type = MLX5DV_FLOW_ACTION_COUNTERS_DEVX;
613         action->obj = counter_obj;
614         return action;
615 #endif
616 #else
617         (void)counter_obj;
618         (void)offset;
619         errno = ENOTSUP;
620         return NULL;
621 #endif
622 }
623
624 static void *
625 mlx5_glue_dv_create_flow_action_dest_ibv_qp(void *qp)
626 {
627 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
628 #ifdef HAVE_MLX5DV_DR
629         return mlx5dv_dr_action_create_dest_ibv_qp(qp);
630 #else
631         struct mlx5dv_flow_action_attr *action;
632
633         action = malloc(sizeof(*action));
634         if (!action)
635                 return NULL;
636         action->type = MLX5DV_FLOW_ACTION_DEST_IBV_QP;
637         action->obj = qp;
638         return action;
639 #endif
640 #else
641         (void)qp;
642         errno = ENOTSUP;
643         return NULL;
644 #endif
645 }
646
647 static void *
648 mlx5_glue_dv_create_flow_action_dest_devx_tir(void *tir)
649 {
650 #ifdef HAVE_MLX5DV_DR_ACTION_DEST_DEVX_TIR
651         return mlx5dv_dr_action_create_dest_devx_tir(tir);
652 #else
653         (void)tir;
654         errno = ENOTSUP;
655         return NULL;
656 #endif
657 }
658
659 static void *
660 mlx5_glue_dv_create_flow_action_modify_header
661                                         (struct ibv_context *ctx,
662                                          enum mlx5dv_flow_table_type ft_type,
663                                          void *domain, uint64_t flags,
664                                          size_t actions_sz,
665                                          uint64_t actions[])
666 {
667 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
668 #ifdef HAVE_MLX5DV_DR
669         (void)ctx;
670         (void)ft_type;
671         return mlx5dv_dr_action_create_modify_header(domain, flags, actions_sz,
672                                                      (__be64 *)actions);
673 #else
674         struct mlx5dv_flow_action_attr *action;
675
676         (void)domain;
677         (void)flags;
678         action = malloc(sizeof(*action));
679         if (!action)
680                 return NULL;
681         action->type = MLX5DV_FLOW_ACTION_IBV_FLOW_ACTION;
682         action->action = mlx5dv_create_flow_action_modify_header
683                 (ctx, actions_sz, actions, ft_type);
684         return action;
685 #endif
686 #else
687         (void)ctx;
688         (void)ft_type;
689         (void)domain;
690         (void)flags;
691         (void)actions_sz;
692         (void)actions;
693         errno = ENOTSUP;
694         return NULL;
695 #endif
696 }
697
698 static void *
699 mlx5_glue_dv_create_flow_action_packet_reformat
700                 (struct ibv_context *ctx,
701                  enum mlx5dv_flow_action_packet_reformat_type reformat_type,
702                  enum mlx5dv_flow_table_type ft_type,
703                  struct mlx5dv_dr_domain *domain,
704                  uint32_t flags, size_t data_sz, void *data)
705 {
706 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
707 #ifdef HAVE_MLX5DV_DR
708         (void)ctx;
709         (void)ft_type;
710         return mlx5dv_dr_action_create_packet_reformat(domain, flags,
711                                                        reformat_type, data_sz,
712                                                        data);
713 #else
714         (void)domain;
715         (void)flags;
716         struct mlx5dv_flow_action_attr *action;
717
718         action = malloc(sizeof(*action));
719         if (!action)
720                 return NULL;
721         action->type = MLX5DV_FLOW_ACTION_IBV_FLOW_ACTION;
722         action->action = mlx5dv_create_flow_action_packet_reformat
723                 (ctx, data_sz, data, reformat_type, ft_type);
724         return action;
725 #endif
726 #else
727         (void)ctx;
728         (void)reformat_type;
729         (void)ft_type;
730         (void)domain;
731         (void)flags;
732         (void)data_sz;
733         (void)data;
734         errno = ENOTSUP;
735         return NULL;
736 #endif
737 }
738
739 static void *
740 mlx5_glue_dv_create_flow_action_tag(uint32_t tag)
741 {
742 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
743 #ifdef HAVE_MLX5DV_DR
744         return mlx5dv_dr_action_create_tag(tag);
745 #else
746         struct mlx5dv_flow_action_attr *action;
747         action = malloc(sizeof(*action));
748         if (!action)
749                 return NULL;
750         action->type = MLX5DV_FLOW_ACTION_TAG;
751         action->tag_value = tag;
752         return action;
753 #endif
754 #endif
755         (void)tag;
756         errno = ENOTSUP;
757         return NULL;
758 }
759
760 static void *
761 mlx5_glue_dv_create_flow_action_meter(struct mlx5dv_dr_flow_meter_attr *attr)
762 {
763 #if defined(HAVE_MLX5DV_DR) && defined(HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER)
764         return mlx5dv_dr_action_create_flow_meter(attr);
765 #else
766         (void)attr;
767         errno = ENOTSUP;
768         return NULL;
769 #endif
770 }
771
772 static int
773 mlx5_glue_dv_modify_flow_action_meter(void *action,
774                                       struct mlx5dv_dr_flow_meter_attr *attr,
775                                       uint64_t modify_bits)
776 {
777 #if defined(HAVE_MLX5DV_DR) && defined(HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER)
778         return mlx5dv_dr_action_modify_flow_meter(action, attr, modify_bits);
779 #else
780         (void)action;
781         (void)attr;
782         (void)modify_bits;
783         errno = ENOTSUP;
784         return errno;
785 #endif
786 }
787
788 static int
789 mlx5_glue_dv_destroy_flow(void *flow_id)
790 {
791 #ifdef HAVE_MLX5DV_DR
792         return mlx5dv_dr_rule_destroy(flow_id);
793 #else
794         return ibv_destroy_flow(flow_id);
795 #endif
796 }
797
798 static int
799 mlx5_glue_dv_destroy_flow_matcher(void *matcher)
800 {
801 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
802 #ifdef HAVE_MLX5DV_DR
803         return mlx5dv_dr_matcher_destroy(matcher);
804 #else
805         return mlx5dv_destroy_flow_matcher(matcher);
806 #endif
807 #else
808         (void)matcher;
809         errno = ENOTSUP;
810         return errno;
811 #endif
812 }
813
814 static struct ibv_context *
815 mlx5_glue_dv_open_device(struct ibv_device *device)
816 {
817 #ifdef HAVE_IBV_DEVX_OBJ
818         return mlx5dv_open_device(device,
819                                   &(struct mlx5dv_context_attr){
820                                         .flags = MLX5DV_CONTEXT_FLAGS_DEVX,
821                                   });
822 #else
823         (void)device;
824         errno = ENOTSUP;
825         return NULL;
826 #endif
827 }
828
829 static struct mlx5dv_devx_obj *
830 mlx5_glue_devx_obj_create(struct ibv_context *ctx,
831                           const void *in, size_t inlen,
832                           void *out, size_t outlen)
833 {
834 #ifdef HAVE_IBV_DEVX_OBJ
835         return mlx5dv_devx_obj_create(ctx, in, inlen, out, outlen);
836 #else
837         (void)ctx;
838         (void)in;
839         (void)inlen;
840         (void)out;
841         (void)outlen;
842         errno = ENOTSUP;
843         return NULL;
844 #endif
845 }
846
847 static int
848 mlx5_glue_devx_obj_destroy(struct mlx5dv_devx_obj *obj)
849 {
850 #ifdef HAVE_IBV_DEVX_OBJ
851         return mlx5dv_devx_obj_destroy(obj);
852 #else
853         (void)obj;
854         return -ENOTSUP;
855 #endif
856 }
857
858 static int
859 mlx5_glue_devx_obj_query(struct mlx5dv_devx_obj *obj,
860                          const void *in, size_t inlen,
861                          void *out, size_t outlen)
862 {
863 #ifdef HAVE_IBV_DEVX_OBJ
864         return mlx5dv_devx_obj_query(obj, in, inlen, out, outlen);
865 #else
866         (void)obj;
867         (void)in;
868         (void)inlen;
869         (void)out;
870         (void)outlen;
871         return -ENOTSUP;
872 #endif
873 }
874
875 static int
876 mlx5_glue_devx_obj_modify(struct mlx5dv_devx_obj *obj,
877                           const void *in, size_t inlen,
878                           void *out, size_t outlen)
879 {
880 #ifdef HAVE_IBV_DEVX_OBJ
881         return mlx5dv_devx_obj_modify(obj, in, inlen, out, outlen);
882 #else
883         (void)obj;
884         (void)in;
885         (void)inlen;
886         (void)out;
887         (void)outlen;
888         return -ENOTSUP;
889 #endif
890 }
891
892 static int
893 mlx5_glue_devx_general_cmd(struct ibv_context *ctx,
894                            const void *in, size_t inlen,
895                            void *out, size_t outlen)
896 {
897 #ifdef HAVE_IBV_DEVX_OBJ
898         return mlx5dv_devx_general_cmd(ctx, in, inlen, out, outlen);
899 #else
900         (void)ctx;
901         (void)in;
902         (void)inlen;
903         (void)out;
904         (void)outlen;
905         return -ENOTSUP;
906 #endif
907 }
908
909 static struct mlx5dv_devx_cmd_comp *
910 mlx5_glue_devx_create_cmd_comp(struct ibv_context *ctx)
911 {
912 #ifdef HAVE_IBV_DEVX_ASYNC
913         return mlx5dv_devx_create_cmd_comp(ctx);
914 #else
915         (void)ctx;
916         errno = -ENOTSUP;
917         return NULL;
918 #endif
919 }
920
921 static void
922 mlx5_glue_devx_destroy_cmd_comp(struct mlx5dv_devx_cmd_comp *cmd_comp)
923 {
924 #ifdef HAVE_IBV_DEVX_ASYNC
925         mlx5dv_devx_destroy_cmd_comp(cmd_comp);
926 #else
927         (void)cmd_comp;
928         errno = -ENOTSUP;
929 #endif
930 }
931
932 static int
933 mlx5_glue_devx_obj_query_async(struct mlx5dv_devx_obj *obj, const void *in,
934                                size_t inlen, size_t outlen, uint64_t wr_id,
935                                struct mlx5dv_devx_cmd_comp *cmd_comp)
936 {
937 #ifdef HAVE_IBV_DEVX_ASYNC
938         return mlx5dv_devx_obj_query_async(obj, in, inlen, outlen, wr_id,
939                                            cmd_comp);
940 #else
941         (void)obj;
942         (void)in;
943         (void)inlen;
944         (void)outlen;
945         (void)wr_id;
946         (void)cmd_comp;
947         return -ENOTSUP;
948 #endif
949 }
950
951 static int
952 mlx5_glue_devx_get_async_cmd_comp(struct mlx5dv_devx_cmd_comp *cmd_comp,
953                                   struct mlx5dv_devx_async_cmd_hdr *cmd_resp,
954                                   size_t cmd_resp_len)
955 {
956 #ifdef HAVE_IBV_DEVX_ASYNC
957         return mlx5dv_devx_get_async_cmd_comp(cmd_comp, cmd_resp,
958                                               cmd_resp_len);
959 #else
960         (void)cmd_comp;
961         (void)cmd_resp;
962         (void)cmd_resp_len;
963         return -ENOTSUP;
964 #endif
965 }
966
967 static struct mlx5dv_devx_umem *
968 mlx5_glue_devx_umem_reg(struct ibv_context *context, void *addr, size_t size,
969                         uint32_t access)
970 {
971 #ifdef HAVE_IBV_DEVX_OBJ
972         return mlx5dv_devx_umem_reg(context, addr, size, access);
973 #else
974         (void)context;
975         (void)addr;
976         (void)size;
977         (void)access;
978         errno = -ENOTSUP;
979         return NULL;
980 #endif
981 }
982
983 static int
984 mlx5_glue_devx_umem_dereg(struct mlx5dv_devx_umem *dv_devx_umem)
985 {
986 #ifdef HAVE_IBV_DEVX_OBJ
987         return mlx5dv_devx_umem_dereg(dv_devx_umem);
988 #else
989         (void)dv_devx_umem;
990         return -ENOTSUP;
991 #endif
992 }
993
994 static int
995 mlx5_glue_devx_qp_query(struct ibv_qp *qp,
996                         const void *in, size_t inlen,
997                         void *out, size_t outlen)
998 {
999 #ifdef HAVE_IBV_DEVX_OBJ
1000         return mlx5dv_devx_qp_query(qp, in, inlen, out, outlen);
1001 #else
1002         (void)qp;
1003         (void)in;
1004         (void)inlen;
1005         (void)out;
1006         (void)outlen;
1007         errno = ENOTSUP;
1008         return errno;
1009 #endif
1010 }
1011
1012 static int
1013 mlx5_glue_devx_port_query(struct ibv_context *ctx,
1014                           uint32_t port_num,
1015                           struct mlx5dv_devx_port *mlx5_devx_port)
1016 {
1017 #ifdef HAVE_MLX5DV_DR_DEVX_PORT
1018         return mlx5dv_query_devx_port(ctx, port_num, mlx5_devx_port);
1019 #else
1020         (void)ctx;
1021         (void)port_num;
1022         (void)mlx5_devx_port;
1023         errno = ENOTSUP;
1024         return errno;
1025 #endif
1026 }
1027
1028 static int
1029 mlx5_glue_dr_dump_domain(FILE *file, void *domain)
1030 {
1031 #ifdef HAVE_MLX5_DR_FLOW_DUMP
1032         return mlx5dv_dump_dr_domain(file, domain);
1033 #else
1034         RTE_SET_USED(file);
1035         RTE_SET_USED(domain);
1036         return -ENOTSUP;
1037 #endif
1038 }
1039
1040 alignas(RTE_CACHE_LINE_SIZE)
1041 const struct mlx5_glue *mlx5_glue = &(const struct mlx5_glue){
1042         .version = MLX5_GLUE_VERSION,
1043         .fork_init = mlx5_glue_fork_init,
1044         .alloc_pd = mlx5_glue_alloc_pd,
1045         .dealloc_pd = mlx5_glue_dealloc_pd,
1046         .get_device_list = mlx5_glue_get_device_list,
1047         .free_device_list = mlx5_glue_free_device_list,
1048         .open_device = mlx5_glue_open_device,
1049         .close_device = mlx5_glue_close_device,
1050         .query_device = mlx5_glue_query_device,
1051         .query_device_ex = mlx5_glue_query_device_ex,
1052         .query_rt_values_ex = mlx5_glue_query_rt_values_ex,
1053         .query_port = mlx5_glue_query_port,
1054         .create_comp_channel = mlx5_glue_create_comp_channel,
1055         .destroy_comp_channel = mlx5_glue_destroy_comp_channel,
1056         .create_cq = mlx5_glue_create_cq,
1057         .destroy_cq = mlx5_glue_destroy_cq,
1058         .get_cq_event = mlx5_glue_get_cq_event,
1059         .ack_cq_events = mlx5_glue_ack_cq_events,
1060         .create_rwq_ind_table = mlx5_glue_create_rwq_ind_table,
1061         .destroy_rwq_ind_table = mlx5_glue_destroy_rwq_ind_table,
1062         .create_wq = mlx5_glue_create_wq,
1063         .destroy_wq = mlx5_glue_destroy_wq,
1064         .modify_wq = mlx5_glue_modify_wq,
1065         .create_flow = mlx5_glue_create_flow,
1066         .destroy_flow = mlx5_glue_destroy_flow,
1067         .destroy_flow_action = mlx5_glue_destroy_flow_action,
1068         .create_qp = mlx5_glue_create_qp,
1069         .create_qp_ex = mlx5_glue_create_qp_ex,
1070         .destroy_qp = mlx5_glue_destroy_qp,
1071         .modify_qp = mlx5_glue_modify_qp,
1072         .reg_mr = mlx5_glue_reg_mr,
1073         .dereg_mr = mlx5_glue_dereg_mr,
1074         .create_counter_set = mlx5_glue_create_counter_set,
1075         .destroy_counter_set = mlx5_glue_destroy_counter_set,
1076         .describe_counter_set = mlx5_glue_describe_counter_set,
1077         .query_counter_set = mlx5_glue_query_counter_set,
1078         .create_counters = mlx5_glue_create_counters,
1079         .destroy_counters = mlx5_glue_destroy_counters,
1080         .attach_counters = mlx5_glue_attach_counters,
1081         .query_counters = mlx5_glue_query_counters,
1082         .ack_async_event = mlx5_glue_ack_async_event,
1083         .get_async_event = mlx5_glue_get_async_event,
1084         .port_state_str = mlx5_glue_port_state_str,
1085         .cq_ex_to_cq = mlx5_glue_cq_ex_to_cq,
1086         .dr_create_flow_action_dest_flow_tbl =
1087                 mlx5_glue_dr_create_flow_action_dest_flow_tbl,
1088         .dr_create_flow_action_dest_port =
1089                 mlx5_glue_dr_create_flow_action_dest_port,
1090         .dr_create_flow_action_drop =
1091                 mlx5_glue_dr_create_flow_action_drop,
1092         .dr_create_flow_action_push_vlan =
1093                 mlx5_glue_dr_create_flow_action_push_vlan,
1094         .dr_create_flow_action_pop_vlan =
1095                 mlx5_glue_dr_create_flow_action_pop_vlan,
1096         .dr_create_flow_tbl = mlx5_glue_dr_create_flow_tbl,
1097         .dr_destroy_flow_tbl = mlx5_glue_dr_destroy_flow_tbl,
1098         .dr_create_domain = mlx5_glue_dr_create_domain,
1099         .dr_destroy_domain = mlx5_glue_dr_destroy_domain,
1100         .dv_create_cq = mlx5_glue_dv_create_cq,
1101         .dv_create_wq = mlx5_glue_dv_create_wq,
1102         .dv_query_device = mlx5_glue_dv_query_device,
1103         .dv_set_context_attr = mlx5_glue_dv_set_context_attr,
1104         .dv_init_obj = mlx5_glue_dv_init_obj,
1105         .dv_create_qp = mlx5_glue_dv_create_qp,
1106         .dv_create_flow_matcher = mlx5_glue_dv_create_flow_matcher,
1107         .dv_create_flow = mlx5_glue_dv_create_flow,
1108         .dv_create_flow_action_counter =
1109                 mlx5_glue_dv_create_flow_action_counter,
1110         .dv_create_flow_action_dest_ibv_qp =
1111                 mlx5_glue_dv_create_flow_action_dest_ibv_qp,
1112         .dv_create_flow_action_dest_devx_tir =
1113                 mlx5_glue_dv_create_flow_action_dest_devx_tir,
1114         .dv_create_flow_action_modify_header =
1115                 mlx5_glue_dv_create_flow_action_modify_header,
1116         .dv_create_flow_action_packet_reformat =
1117                 mlx5_glue_dv_create_flow_action_packet_reformat,
1118         .dv_create_flow_action_tag =  mlx5_glue_dv_create_flow_action_tag,
1119         .dv_create_flow_action_meter = mlx5_glue_dv_create_flow_action_meter,
1120         .dv_modify_flow_action_meter = mlx5_glue_dv_modify_flow_action_meter,
1121         .dv_destroy_flow = mlx5_glue_dv_destroy_flow,
1122         .dv_destroy_flow_matcher = mlx5_glue_dv_destroy_flow_matcher,
1123         .dv_open_device = mlx5_glue_dv_open_device,
1124         .devx_obj_create = mlx5_glue_devx_obj_create,
1125         .devx_obj_destroy = mlx5_glue_devx_obj_destroy,
1126         .devx_obj_query = mlx5_glue_devx_obj_query,
1127         .devx_obj_modify = mlx5_glue_devx_obj_modify,
1128         .devx_general_cmd = mlx5_glue_devx_general_cmd,
1129         .devx_create_cmd_comp = mlx5_glue_devx_create_cmd_comp,
1130         .devx_destroy_cmd_comp = mlx5_glue_devx_destroy_cmd_comp,
1131         .devx_obj_query_async = mlx5_glue_devx_obj_query_async,
1132         .devx_get_async_cmd_comp = mlx5_glue_devx_get_async_cmd_comp,
1133         .devx_umem_reg = mlx5_glue_devx_umem_reg,
1134         .devx_umem_dereg = mlx5_glue_devx_umem_dereg,
1135         .devx_qp_query = mlx5_glue_devx_qp_query,
1136         .devx_port_query = mlx5_glue_devx_port_query,
1137         .dr_dump_domain = mlx5_glue_dr_dump_domain,
1138 };