net/mlx5: separate DevX commands interface
[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 #include <stdlib.h>
11
12 /*
13  * Not needed by this file; included to work around the lack of off_t
14  * definition for mlx5dv.h with unpatched rdma-core versions.
15  */
16 #include <sys/types.h>
17
18 /* Verbs headers do not support -pedantic. */
19 #ifdef PEDANTIC
20 #pragma GCC diagnostic ignored "-Wpedantic"
21 #endif
22 #include <infiniband/mlx5dv.h>
23 #include <infiniband/verbs.h>
24 #ifdef PEDANTIC
25 #pragma GCC diagnostic error "-Wpedantic"
26 #endif
27
28 #include <rte_config.h>
29
30 #include "mlx5_autoconf.h"
31 #include "mlx5_glue.h"
32
33 static int
34 mlx5_glue_fork_init(void)
35 {
36         return ibv_fork_init();
37 }
38
39 static struct ibv_pd *
40 mlx5_glue_alloc_pd(struct ibv_context *context)
41 {
42         return ibv_alloc_pd(context);
43 }
44
45 static int
46 mlx5_glue_dealloc_pd(struct ibv_pd *pd)
47 {
48         return ibv_dealloc_pd(pd);
49 }
50
51 static struct ibv_device **
52 mlx5_glue_get_device_list(int *num_devices)
53 {
54         return ibv_get_device_list(num_devices);
55 }
56
57 static void
58 mlx5_glue_free_device_list(struct ibv_device **list)
59 {
60         ibv_free_device_list(list);
61 }
62
63 static struct ibv_context *
64 mlx5_glue_open_device(struct ibv_device *device)
65 {
66         return ibv_open_device(device);
67 }
68
69 static int
70 mlx5_glue_close_device(struct ibv_context *context)
71 {
72         return ibv_close_device(context);
73 }
74
75 static int
76 mlx5_glue_query_device(struct ibv_context *context,
77                        struct ibv_device_attr *device_attr)
78 {
79         return ibv_query_device(context, device_attr);
80 }
81
82 static int
83 mlx5_glue_query_device_ex(struct ibv_context *context,
84                           const struct ibv_query_device_ex_input *input,
85                           struct ibv_device_attr_ex *attr)
86 {
87         return ibv_query_device_ex(context, input, attr);
88 }
89
90 static int
91 mlx5_glue_query_rt_values_ex(struct ibv_context *context,
92                           struct ibv_values_ex *values)
93 {
94         return ibv_query_rt_values_ex(context, values);
95 }
96
97 static int
98 mlx5_glue_query_port(struct ibv_context *context, uint8_t port_num,
99                      struct ibv_port_attr *port_attr)
100 {
101         return ibv_query_port(context, port_num, port_attr);
102 }
103
104 static struct ibv_comp_channel *
105 mlx5_glue_create_comp_channel(struct ibv_context *context)
106 {
107         return ibv_create_comp_channel(context);
108 }
109
110 static int
111 mlx5_glue_destroy_comp_channel(struct ibv_comp_channel *channel)
112 {
113         return ibv_destroy_comp_channel(channel);
114 }
115
116 static struct ibv_cq *
117 mlx5_glue_create_cq(struct ibv_context *context, int cqe, void *cq_context,
118                     struct ibv_comp_channel *channel, int comp_vector)
119 {
120         return ibv_create_cq(context, cqe, cq_context, channel, comp_vector);
121 }
122
123 static int
124 mlx5_glue_destroy_cq(struct ibv_cq *cq)
125 {
126         return ibv_destroy_cq(cq);
127 }
128
129 static int
130 mlx5_glue_get_cq_event(struct ibv_comp_channel *channel, struct ibv_cq **cq,
131                        void **cq_context)
132 {
133         return ibv_get_cq_event(channel, cq, cq_context);
134 }
135
136 static void
137 mlx5_glue_ack_cq_events(struct ibv_cq *cq, unsigned int nevents)
138 {
139         ibv_ack_cq_events(cq, nevents);
140 }
141
142 static struct ibv_rwq_ind_table *
143 mlx5_glue_create_rwq_ind_table(struct ibv_context *context,
144                                struct ibv_rwq_ind_table_init_attr *init_attr)
145 {
146         return ibv_create_rwq_ind_table(context, init_attr);
147 }
148
149 static int
150 mlx5_glue_destroy_rwq_ind_table(struct ibv_rwq_ind_table *rwq_ind_table)
151 {
152         return ibv_destroy_rwq_ind_table(rwq_ind_table);
153 }
154
155 static struct ibv_wq *
156 mlx5_glue_create_wq(struct ibv_context *context,
157                     struct ibv_wq_init_attr *wq_init_attr)
158 {
159         return ibv_create_wq(context, wq_init_attr);
160 }
161
162 static int
163 mlx5_glue_destroy_wq(struct ibv_wq *wq)
164 {
165         return ibv_destroy_wq(wq);
166 }
167 static int
168 mlx5_glue_modify_wq(struct ibv_wq *wq, struct ibv_wq_attr *wq_attr)
169 {
170         return ibv_modify_wq(wq, wq_attr);
171 }
172
173 static struct ibv_flow *
174 mlx5_glue_create_flow(struct ibv_qp *qp, struct ibv_flow_attr *flow)
175 {
176         return ibv_create_flow(qp, flow);
177 }
178
179 static int
180 mlx5_glue_destroy_flow(struct ibv_flow *flow_id)
181 {
182         return ibv_destroy_flow(flow_id);
183 }
184
185 static int
186 mlx5_glue_destroy_flow_action(void *action)
187 {
188 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
189 #ifdef HAVE_MLX5DV_DR
190         return mlx5dv_dr_action_destroy(action);
191 #else
192         struct mlx5dv_flow_action_attr *attr = action;
193         int res = 0;
194         switch (attr->type) {
195         case MLX5DV_FLOW_ACTION_TAG:
196                 break;
197         default:
198                 res = ibv_destroy_flow_action(attr->action);
199                 break;
200         }
201         free(action);
202         return res;
203 #endif
204 #else
205         (void)action;
206         return ENOTSUP;
207 #endif
208 }
209
210 static struct ibv_qp *
211 mlx5_glue_create_qp(struct ibv_pd *pd, struct ibv_qp_init_attr *qp_init_attr)
212 {
213         return ibv_create_qp(pd, qp_init_attr);
214 }
215
216 static struct ibv_qp *
217 mlx5_glue_create_qp_ex(struct ibv_context *context,
218                        struct ibv_qp_init_attr_ex *qp_init_attr_ex)
219 {
220         return ibv_create_qp_ex(context, qp_init_attr_ex);
221 }
222
223 static int
224 mlx5_glue_destroy_qp(struct ibv_qp *qp)
225 {
226         return ibv_destroy_qp(qp);
227 }
228
229 static int
230 mlx5_glue_modify_qp(struct ibv_qp *qp, struct ibv_qp_attr *attr, int attr_mask)
231 {
232         return ibv_modify_qp(qp, attr, attr_mask);
233 }
234
235 static struct ibv_mr *
236 mlx5_glue_reg_mr(struct ibv_pd *pd, void *addr, size_t length, int access)
237 {
238         return ibv_reg_mr(pd, addr, length, access);
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 alignas(RTE_CACHE_LINE_SIZE)
1053 const struct mlx5_glue *mlx5_glue = &(const struct mlx5_glue){
1054         .version = MLX5_GLUE_VERSION,
1055         .fork_init = mlx5_glue_fork_init,
1056         .alloc_pd = mlx5_glue_alloc_pd,
1057         .dealloc_pd = mlx5_glue_dealloc_pd,
1058         .get_device_list = mlx5_glue_get_device_list,
1059         .free_device_list = mlx5_glue_free_device_list,
1060         .open_device = mlx5_glue_open_device,
1061         .close_device = mlx5_glue_close_device,
1062         .query_device = mlx5_glue_query_device,
1063         .query_device_ex = mlx5_glue_query_device_ex,
1064         .query_rt_values_ex = mlx5_glue_query_rt_values_ex,
1065         .query_port = mlx5_glue_query_port,
1066         .create_comp_channel = mlx5_glue_create_comp_channel,
1067         .destroy_comp_channel = mlx5_glue_destroy_comp_channel,
1068         .create_cq = mlx5_glue_create_cq,
1069         .destroy_cq = mlx5_glue_destroy_cq,
1070         .get_cq_event = mlx5_glue_get_cq_event,
1071         .ack_cq_events = mlx5_glue_ack_cq_events,
1072         .create_rwq_ind_table = mlx5_glue_create_rwq_ind_table,
1073         .destroy_rwq_ind_table = mlx5_glue_destroy_rwq_ind_table,
1074         .create_wq = mlx5_glue_create_wq,
1075         .destroy_wq = mlx5_glue_destroy_wq,
1076         .modify_wq = mlx5_glue_modify_wq,
1077         .create_flow = mlx5_glue_create_flow,
1078         .destroy_flow = mlx5_glue_destroy_flow,
1079         .destroy_flow_action = mlx5_glue_destroy_flow_action,
1080         .create_qp = mlx5_glue_create_qp,
1081         .create_qp_ex = mlx5_glue_create_qp_ex,
1082         .destroy_qp = mlx5_glue_destroy_qp,
1083         .modify_qp = mlx5_glue_modify_qp,
1084         .reg_mr = mlx5_glue_reg_mr,
1085         .dereg_mr = mlx5_glue_dereg_mr,
1086         .create_counter_set = mlx5_glue_create_counter_set,
1087         .destroy_counter_set = mlx5_glue_destroy_counter_set,
1088         .describe_counter_set = mlx5_glue_describe_counter_set,
1089         .query_counter_set = mlx5_glue_query_counter_set,
1090         .create_counters = mlx5_glue_create_counters,
1091         .destroy_counters = mlx5_glue_destroy_counters,
1092         .attach_counters = mlx5_glue_attach_counters,
1093         .query_counters = mlx5_glue_query_counters,
1094         .ack_async_event = mlx5_glue_ack_async_event,
1095         .get_async_event = mlx5_glue_get_async_event,
1096         .port_state_str = mlx5_glue_port_state_str,
1097         .cq_ex_to_cq = mlx5_glue_cq_ex_to_cq,
1098         .dr_create_flow_action_dest_flow_tbl =
1099                 mlx5_glue_dr_create_flow_action_dest_flow_tbl,
1100         .dr_create_flow_action_dest_port =
1101                 mlx5_glue_dr_create_flow_action_dest_port,
1102         .dr_create_flow_action_drop =
1103                 mlx5_glue_dr_create_flow_action_drop,
1104         .dr_create_flow_action_push_vlan =
1105                 mlx5_glue_dr_create_flow_action_push_vlan,
1106         .dr_create_flow_action_pop_vlan =
1107                 mlx5_glue_dr_create_flow_action_pop_vlan,
1108         .dr_create_flow_tbl = mlx5_glue_dr_create_flow_tbl,
1109         .dr_destroy_flow_tbl = mlx5_glue_dr_destroy_flow_tbl,
1110         .dr_create_domain = mlx5_glue_dr_create_domain,
1111         .dr_destroy_domain = mlx5_glue_dr_destroy_domain,
1112         .dv_create_cq = mlx5_glue_dv_create_cq,
1113         .dv_create_wq = mlx5_glue_dv_create_wq,
1114         .dv_query_device = mlx5_glue_dv_query_device,
1115         .dv_set_context_attr = mlx5_glue_dv_set_context_attr,
1116         .dv_init_obj = mlx5_glue_dv_init_obj,
1117         .dv_create_qp = mlx5_glue_dv_create_qp,
1118         .dv_create_flow_matcher = mlx5_glue_dv_create_flow_matcher,
1119         .dv_create_flow = mlx5_glue_dv_create_flow,
1120         .dv_create_flow_action_counter =
1121                 mlx5_glue_dv_create_flow_action_counter,
1122         .dv_create_flow_action_dest_ibv_qp =
1123                 mlx5_glue_dv_create_flow_action_dest_ibv_qp,
1124         .dv_create_flow_action_dest_devx_tir =
1125                 mlx5_glue_dv_create_flow_action_dest_devx_tir,
1126         .dv_create_flow_action_modify_header =
1127                 mlx5_glue_dv_create_flow_action_modify_header,
1128         .dv_create_flow_action_packet_reformat =
1129                 mlx5_glue_dv_create_flow_action_packet_reformat,
1130         .dv_create_flow_action_tag =  mlx5_glue_dv_create_flow_action_tag,
1131         .dv_create_flow_action_meter = mlx5_glue_dv_create_flow_action_meter,
1132         .dv_modify_flow_action_meter = mlx5_glue_dv_modify_flow_action_meter,
1133         .dv_destroy_flow = mlx5_glue_dv_destroy_flow,
1134         .dv_destroy_flow_matcher = mlx5_glue_dv_destroy_flow_matcher,
1135         .dv_open_device = mlx5_glue_dv_open_device,
1136         .devx_obj_create = mlx5_glue_devx_obj_create,
1137         .devx_obj_destroy = mlx5_glue_devx_obj_destroy,
1138         .devx_obj_query = mlx5_glue_devx_obj_query,
1139         .devx_obj_modify = mlx5_glue_devx_obj_modify,
1140         .devx_general_cmd = mlx5_glue_devx_general_cmd,
1141         .devx_create_cmd_comp = mlx5_glue_devx_create_cmd_comp,
1142         .devx_destroy_cmd_comp = mlx5_glue_devx_destroy_cmd_comp,
1143         .devx_obj_query_async = mlx5_glue_devx_obj_query_async,
1144         .devx_get_async_cmd_comp = mlx5_glue_devx_get_async_cmd_comp,
1145         .devx_umem_reg = mlx5_glue_devx_umem_reg,
1146         .devx_umem_dereg = mlx5_glue_devx_umem_dereg,
1147         .devx_qp_query = mlx5_glue_devx_qp_query,
1148         .devx_port_query = mlx5_glue_devx_port_query,
1149         .dr_dump_domain = mlx5_glue_dr_dump_domain,
1150 };