net/mlx5: refactor statistics
[dpdk.git] / drivers / net / mlx5 / linux / mlx5_os.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2020 Mellanox Technologies, Ltd
4  */
5
6 #include <stddef.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <errno.h>
12 #include <net/if.h>
13 #include <sys/mman.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/sockios.h>
16 #include <linux/ethtool.h>
17 #include <fcntl.h>
18
19 /* Verbs header. */
20 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
21 #ifdef PEDANTIC
22 #pragma GCC diagnostic ignored "-Wpedantic"
23 #endif
24 #include <infiniband/verbs.h>
25 #ifdef PEDANTIC
26 #pragma GCC diagnostic error "-Wpedantic"
27 #endif
28
29 #include <rte_malloc.h>
30 #include <rte_ethdev_driver.h>
31 #include <rte_ethdev_pci.h>
32 #include <rte_pci.h>
33 #include <rte_bus_pci.h>
34 #include <rte_common.h>
35 #include <rte_kvargs.h>
36 #include <rte_rwlock.h>
37 #include <rte_spinlock.h>
38 #include <rte_string_fns.h>
39 #include <rte_alarm.h>
40
41 #include <mlx5_glue.h>
42 #include <mlx5_devx_cmds.h>
43 #include <mlx5_common.h>
44 #include <mlx5_common_mp.h>
45
46 #include "mlx5_defs.h"
47 #include "mlx5.h"
48 #include "mlx5_utils.h"
49 #include "mlx5_rxtx.h"
50 #include "mlx5_autoconf.h"
51 #include "mlx5_mr.h"
52 #include "mlx5_flow.h"
53 #include "rte_pmd_mlx5.h"
54
55 #define MLX5_TAGS_HLIST_ARRAY_SIZE 8192
56
57 #ifndef HAVE_IBV_MLX5_MOD_MPW
58 #define MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED (1 << 2)
59 #define MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW (1 << 3)
60 #endif
61
62 #ifndef HAVE_IBV_MLX5_MOD_CQE_128B_COMP
63 #define MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP (1 << 4)
64 #endif
65
66 /**
67  * Get device name. Given an ibv_device pointer - return a
68  * pointer to the corresponding device name.
69  *
70  * @param[in] dev
71  *   Pointer to ibv device.
72  *
73  * @return
74  *   Pointer to device name if dev is valid, NULL otherwise.
75  */
76 const char *
77 mlx5_os_get_dev_device_name(void *dev)
78 {
79         if (!dev)
80                 return NULL;
81         return ((struct ibv_device *)dev)->name;
82 }
83
84 /**
85  * Get ibv device name. Given an ibv_context pointer - return a
86  * pointer to the corresponding device name.
87  *
88  * @param[in] ctx
89  *   Pointer to ibv context.
90  *
91  * @return
92  *   Pointer to device name if ctx is valid, NULL otherwise.
93  */
94 const char *
95 mlx5_os_get_ctx_device_name(void *ctx)
96 {
97         if (!ctx)
98                 return NULL;
99         return ((struct ibv_context *)ctx)->device->name;
100 }
101
102 /**
103  * Get ibv device path name. Given an ibv_context pointer - return a
104  * pointer to the corresponding device path name.
105  *
106  * @param[in] ctx
107  *   Pointer to ibv context.
108  *
109  * @return
110  *   Pointer to device path name if ctx is valid, NULL otherwise.
111  */
112 const char *
113 mlx5_os_get_ctx_device_path(void *ctx)
114 {
115         if (!ctx)
116                 return NULL;
117
118         return ((struct ibv_context *)ctx)->device->ibdev_path;
119 }
120
121 /**
122  * Get umem id. Given a pointer to umem object of type
123  * 'struct mlx5dv_devx_umem *' - return its id.
124  *
125  * @param[in] umem
126  *   Pointer to umem object.
127  *
128  * @return
129  *   The umem id if umem is valid, 0 otherwise.
130  */
131 uint32_t
132 mlx5_os_get_umem_id(void *umem)
133 {
134         if (!umem)
135                 return 0;
136         return ((struct mlx5dv_devx_umem *)umem)->umem_id;
137 }
138
139 /**
140  * Get mlx5 device attributes. The glue function query_device_ex() is called
141  * with out parameter of type 'struct ibv_device_attr_ex *'. Then fill in mlx5
142  * device attributes from the glue out parameter.
143  *
144  * @param dev
145  *   Pointer to ibv context.
146  *
147  * @param device_attr
148  *   Pointer to mlx5 device attributes.
149  *
150  * @return
151  *   0 on success, non zero error number otherwise
152  */
153 int
154 mlx5_os_get_dev_attr(void *ctx, struct mlx5_dev_attr *device_attr)
155 {
156         int err;
157         struct ibv_device_attr_ex attr_ex;
158         memset(device_attr, 0, sizeof(*device_attr));
159         err = mlx5_glue->query_device_ex(ctx, NULL, &attr_ex);
160         if (err)
161                 return err;
162
163         device_attr->device_cap_flags_ex = attr_ex.device_cap_flags_ex;
164         device_attr->max_qp_wr = attr_ex.orig_attr.max_qp_wr;
165         device_attr->max_sge = attr_ex.orig_attr.max_sge;
166         device_attr->max_cq = attr_ex.orig_attr.max_cq;
167         device_attr->max_qp = attr_ex.orig_attr.max_qp;
168         device_attr->raw_packet_caps = attr_ex.raw_packet_caps;
169         device_attr->max_rwq_indirection_table_size =
170                 attr_ex.rss_caps.max_rwq_indirection_table_size;
171         device_attr->max_tso = attr_ex.tso_caps.max_tso;
172         device_attr->tso_supported_qpts = attr_ex.tso_caps.supported_qpts;
173
174         struct mlx5dv_context dv_attr = { .comp_mask = 0 };
175         err = mlx5_glue->dv_query_device(ctx, &dv_attr);
176         if (err)
177                 return err;
178
179         device_attr->flags = dv_attr.flags;
180         device_attr->comp_mask = dv_attr.comp_mask;
181 #ifdef HAVE_IBV_MLX5_MOD_SWP
182         device_attr->sw_parsing_offloads =
183                 dv_attr.sw_parsing_caps.sw_parsing_offloads;
184 #endif
185         device_attr->min_single_stride_log_num_of_bytes =
186                 dv_attr.striding_rq_caps.min_single_stride_log_num_of_bytes;
187         device_attr->max_single_stride_log_num_of_bytes =
188                 dv_attr.striding_rq_caps.max_single_stride_log_num_of_bytes;
189         device_attr->min_single_wqe_log_num_of_strides =
190                 dv_attr.striding_rq_caps.min_single_wqe_log_num_of_strides;
191         device_attr->max_single_wqe_log_num_of_strides =
192                 dv_attr.striding_rq_caps.max_single_wqe_log_num_of_strides;
193         device_attr->stride_supported_qpts =
194                 dv_attr.striding_rq_caps.supported_qpts;
195 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
196         device_attr->tunnel_offloads_caps = dv_attr.tunnel_offloads_caps;
197 #endif
198
199         return err;
200 }
201
202 /**
203  * Verbs callback to allocate a memory. This function should allocate the space
204  * according to the size provided residing inside a huge page.
205  * Please note that all allocation must respect the alignment from libmlx5
206  * (i.e. currently sysconf(_SC_PAGESIZE)).
207  *
208  * @param[in] size
209  *   The size in bytes of the memory to allocate.
210  * @param[in] data
211  *   A pointer to the callback data.
212  *
213  * @return
214  *   Allocated buffer, NULL otherwise and rte_errno is set.
215  */
216 static void *
217 mlx5_alloc_verbs_buf(size_t size, void *data)
218 {
219         struct mlx5_priv *priv = data;
220         void *ret;
221         size_t alignment = sysconf(_SC_PAGESIZE);
222         unsigned int socket = SOCKET_ID_ANY;
223
224         if (priv->verbs_alloc_ctx.type == MLX5_VERBS_ALLOC_TYPE_TX_QUEUE) {
225                 const struct mlx5_txq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
226
227                 socket = ctrl->socket;
228         } else if (priv->verbs_alloc_ctx.type ==
229                    MLX5_VERBS_ALLOC_TYPE_RX_QUEUE) {
230                 const struct mlx5_rxq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
231
232                 socket = ctrl->socket;
233         }
234         MLX5_ASSERT(data != NULL);
235         ret = rte_malloc_socket(__func__, size, alignment, socket);
236         if (!ret && size)
237                 rte_errno = ENOMEM;
238         return ret;
239 }
240
241 /**
242  * Verbs callback to free a memory.
243  *
244  * @param[in] ptr
245  *   A pointer to the memory to free.
246  * @param[in] data
247  *   A pointer to the callback data.
248  */
249 static void
250 mlx5_free_verbs_buf(void *ptr, void *data __rte_unused)
251 {
252         MLX5_ASSERT(data != NULL);
253         rte_free(ptr);
254 }
255
256 /**
257  * Initialize DR related data within private structure.
258  * Routine checks the reference counter and does actual
259  * resources creation/initialization only if counter is zero.
260  *
261  * @param[in] priv
262  *   Pointer to the private device data structure.
263  *
264  * @return
265  *   Zero on success, positive error code otherwise.
266  */
267 static int
268 mlx5_alloc_shared_dr(struct mlx5_priv *priv)
269 {
270         struct mlx5_dev_ctx_shared *sh = priv->sh;
271         char s[MLX5_HLIST_NAMESIZE];
272         int err = 0;
273
274         if (!sh->flow_tbls)
275                 err = mlx5_alloc_table_hash_list(priv);
276         else
277                 DRV_LOG(DEBUG, "sh->flow_tbls[%p] already created, reuse\n",
278                         (void *)sh->flow_tbls);
279         if (err)
280                 return err;
281         /* Create tags hash list table. */
282         snprintf(s, sizeof(s), "%s_tags", sh->ibdev_name);
283         sh->tag_table = mlx5_hlist_create(s, MLX5_TAGS_HLIST_ARRAY_SIZE);
284         if (!sh->tag_table) {
285                 DRV_LOG(ERR, "tags with hash creation failed.\n");
286                 err = ENOMEM;
287                 goto error;
288         }
289 #ifdef HAVE_MLX5DV_DR
290         void *domain;
291
292         if (sh->dv_refcnt) {
293                 /* Shared DV/DR structures is already initialized. */
294                 sh->dv_refcnt++;
295                 priv->dr_shared = 1;
296                 return 0;
297         }
298         /* Reference counter is zero, we should initialize structures. */
299         domain = mlx5_glue->dr_create_domain(sh->ctx,
300                                              MLX5DV_DR_DOMAIN_TYPE_NIC_RX);
301         if (!domain) {
302                 DRV_LOG(ERR, "ingress mlx5dv_dr_create_domain failed");
303                 err = errno;
304                 goto error;
305         }
306         sh->rx_domain = domain;
307         domain = mlx5_glue->dr_create_domain(sh->ctx,
308                                              MLX5DV_DR_DOMAIN_TYPE_NIC_TX);
309         if (!domain) {
310                 DRV_LOG(ERR, "egress mlx5dv_dr_create_domain failed");
311                 err = errno;
312                 goto error;
313         }
314         pthread_mutex_init(&sh->dv_mutex, NULL);
315         sh->tx_domain = domain;
316 #ifdef HAVE_MLX5DV_DR_ESWITCH
317         if (priv->config.dv_esw_en) {
318                 domain  = mlx5_glue->dr_create_domain
319                         (sh->ctx, MLX5DV_DR_DOMAIN_TYPE_FDB);
320                 if (!domain) {
321                         DRV_LOG(ERR, "FDB mlx5dv_dr_create_domain failed");
322                         err = errno;
323                         goto error;
324                 }
325                 sh->fdb_domain = domain;
326                 sh->esw_drop_action = mlx5_glue->dr_create_flow_action_drop();
327         }
328 #endif
329         if (priv->config.reclaim_mode == MLX5_RCM_AGGR) {
330                 mlx5_glue->dr_reclaim_domain_memory(sh->rx_domain, 1);
331                 mlx5_glue->dr_reclaim_domain_memory(sh->tx_domain, 1);
332                 if (sh->fdb_domain)
333                         mlx5_glue->dr_reclaim_domain_memory(sh->fdb_domain, 1);
334         }
335         sh->pop_vlan_action = mlx5_glue->dr_create_flow_action_pop_vlan();
336 #endif /* HAVE_MLX5DV_DR */
337         sh->dv_refcnt++;
338         priv->dr_shared = 1;
339         return 0;
340 error:
341         /* Rollback the created objects. */
342         if (sh->rx_domain) {
343                 mlx5_glue->dr_destroy_domain(sh->rx_domain);
344                 sh->rx_domain = NULL;
345         }
346         if (sh->tx_domain) {
347                 mlx5_glue->dr_destroy_domain(sh->tx_domain);
348                 sh->tx_domain = NULL;
349         }
350         if (sh->fdb_domain) {
351                 mlx5_glue->dr_destroy_domain(sh->fdb_domain);
352                 sh->fdb_domain = NULL;
353         }
354         if (sh->esw_drop_action) {
355                 mlx5_glue->destroy_flow_action(sh->esw_drop_action);
356                 sh->esw_drop_action = NULL;
357         }
358         if (sh->pop_vlan_action) {
359                 mlx5_glue->destroy_flow_action(sh->pop_vlan_action);
360                 sh->pop_vlan_action = NULL;
361         }
362         if (sh->tag_table) {
363                 /* tags should be destroyed with flow before. */
364                 mlx5_hlist_destroy(sh->tag_table, NULL, NULL);
365                 sh->tag_table = NULL;
366         }
367         mlx5_free_table_hash_list(priv);
368         return err;
369 }
370
371 /**
372  * Destroy DR related data within private structure.
373  *
374  * @param[in] priv
375  *   Pointer to the private device data structure.
376  */
377 void
378 mlx5_os_free_shared_dr(struct mlx5_priv *priv)
379 {
380         struct mlx5_dev_ctx_shared *sh;
381
382         if (!priv->dr_shared)
383                 return;
384         priv->dr_shared = 0;
385         sh = priv->sh;
386         MLX5_ASSERT(sh);
387 #ifdef HAVE_MLX5DV_DR
388         MLX5_ASSERT(sh->dv_refcnt);
389         if (sh->dv_refcnt && --sh->dv_refcnt)
390                 return;
391         if (sh->rx_domain) {
392                 mlx5_glue->dr_destroy_domain(sh->rx_domain);
393                 sh->rx_domain = NULL;
394         }
395         if (sh->tx_domain) {
396                 mlx5_glue->dr_destroy_domain(sh->tx_domain);
397                 sh->tx_domain = NULL;
398         }
399 #ifdef HAVE_MLX5DV_DR_ESWITCH
400         if (sh->fdb_domain) {
401                 mlx5_glue->dr_destroy_domain(sh->fdb_domain);
402                 sh->fdb_domain = NULL;
403         }
404         if (sh->esw_drop_action) {
405                 mlx5_glue->destroy_flow_action(sh->esw_drop_action);
406                 sh->esw_drop_action = NULL;
407         }
408 #endif
409         if (sh->pop_vlan_action) {
410                 mlx5_glue->destroy_flow_action(sh->pop_vlan_action);
411                 sh->pop_vlan_action = NULL;
412         }
413         pthread_mutex_destroy(&sh->dv_mutex);
414 #endif /* HAVE_MLX5DV_DR */
415         if (sh->tag_table) {
416                 /* tags should be destroyed with flow before. */
417                 mlx5_hlist_destroy(sh->tag_table, NULL, NULL);
418                 sh->tag_table = NULL;
419         }
420         mlx5_free_table_hash_list(priv);
421 }
422
423 /**
424  * Spawn an Ethernet device from Verbs information.
425  *
426  * @param dpdk_dev
427  *   Backing DPDK device.
428  * @param spawn
429  *   Verbs device parameters (name, port, switch_info) to spawn.
430  * @param config
431  *   Device configuration parameters.
432  *
433  * @return
434  *   A valid Ethernet device object on success, NULL otherwise and rte_errno
435  *   is set. The following errors are defined:
436  *
437  *   EBUSY: device is not supposed to be spawned.
438  *   EEXIST: device is already spawned
439  */
440 static struct rte_eth_dev *
441 mlx5_dev_spawn(struct rte_device *dpdk_dev,
442                struct mlx5_dev_spawn_data *spawn,
443                struct mlx5_dev_config config)
444 {
445         const struct mlx5_switch_info *switch_info = &spawn->info;
446         struct mlx5_dev_ctx_shared *sh = NULL;
447         struct ibv_port_attr port_attr;
448         struct mlx5dv_context dv_attr = { .comp_mask = 0 };
449         struct rte_eth_dev *eth_dev = NULL;
450         struct mlx5_priv *priv = NULL;
451         int err = 0;
452         unsigned int hw_padding = 0;
453         unsigned int mps;
454         unsigned int cqe_comp;
455         unsigned int cqe_pad = 0;
456         unsigned int tunnel_en = 0;
457         unsigned int mpls_en = 0;
458         unsigned int swp = 0;
459         unsigned int mprq = 0;
460         unsigned int mprq_min_stride_size_n = 0;
461         unsigned int mprq_max_stride_size_n = 0;
462         unsigned int mprq_min_stride_num_n = 0;
463         unsigned int mprq_max_stride_num_n = 0;
464         struct rte_ether_addr mac;
465         char name[RTE_ETH_NAME_MAX_LEN];
466         int own_domain_id = 0;
467         uint16_t port_id;
468         unsigned int i;
469 #ifdef HAVE_MLX5DV_DR_DEVX_PORT
470         struct mlx5dv_devx_port devx_port = { .comp_mask = 0 };
471 #endif
472
473         /* Determine if this port representor is supposed to be spawned. */
474         if (switch_info->representor && dpdk_dev->devargs) {
475                 struct rte_eth_devargs eth_da;
476
477                 err = rte_eth_devargs_parse(dpdk_dev->devargs->args, &eth_da);
478                 if (err) {
479                         rte_errno = -err;
480                         DRV_LOG(ERR, "failed to process device arguments: %s",
481                                 strerror(rte_errno));
482                         return NULL;
483                 }
484                 for (i = 0; i < eth_da.nb_representor_ports; ++i)
485                         if (eth_da.representor_ports[i] ==
486                             (uint16_t)switch_info->port_name)
487                                 break;
488                 if (i == eth_da.nb_representor_ports) {
489                         rte_errno = EBUSY;
490                         return NULL;
491                 }
492         }
493         /* Build device name. */
494         if (spawn->pf_bond <  0) {
495                 /* Single device. */
496                 if (!switch_info->representor)
497                         strlcpy(name, dpdk_dev->name, sizeof(name));
498                 else
499                         snprintf(name, sizeof(name), "%s_representor_%u",
500                                  dpdk_dev->name, switch_info->port_name);
501         } else {
502                 /* Bonding device. */
503                 if (!switch_info->representor)
504                         snprintf(name, sizeof(name), "%s_%s",
505                                  dpdk_dev->name,
506                                  mlx5_os_get_dev_device_name(spawn->phys_dev));
507                 else
508                         snprintf(name, sizeof(name), "%s_%s_representor_%u",
509                                  dpdk_dev->name,
510                                  mlx5_os_get_dev_device_name(spawn->phys_dev),
511                                  switch_info->port_name);
512         }
513         /* check if the device is already spawned */
514         if (rte_eth_dev_get_port_by_name(name, &port_id) == 0) {
515                 rte_errno = EEXIST;
516                 return NULL;
517         }
518         DRV_LOG(DEBUG, "naming Ethernet device \"%s\"", name);
519         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
520                 struct mlx5_mp_id mp_id;
521
522                 eth_dev = rte_eth_dev_attach_secondary(name);
523                 if (eth_dev == NULL) {
524                         DRV_LOG(ERR, "can not attach rte ethdev");
525                         rte_errno = ENOMEM;
526                         return NULL;
527                 }
528                 eth_dev->device = dpdk_dev;
529                 eth_dev->dev_ops = &mlx5_os_dev_sec_ops;
530                 err = mlx5_proc_priv_init(eth_dev);
531                 if (err)
532                         return NULL;
533                 mp_id.port_id = eth_dev->data->port_id;
534                 strlcpy(mp_id.name, MLX5_MP_NAME, RTE_MP_MAX_NAME_LEN);
535                 /* Receive command fd from primary process */
536                 err = mlx5_mp_req_verbs_cmd_fd(&mp_id);
537                 if (err < 0)
538                         goto err_secondary;
539                 /* Remap UAR for Tx queues. */
540                 err = mlx5_tx_uar_init_secondary(eth_dev, err);
541                 if (err)
542                         goto err_secondary;
543                 /*
544                  * Ethdev pointer is still required as input since
545                  * the primary device is not accessible from the
546                  * secondary process.
547                  */
548                 eth_dev->rx_pkt_burst = mlx5_select_rx_function(eth_dev);
549                 eth_dev->tx_pkt_burst = mlx5_select_tx_function(eth_dev);
550                 return eth_dev;
551 err_secondary:
552                 mlx5_dev_close(eth_dev);
553                 return NULL;
554         }
555         /*
556          * Some parameters ("tx_db_nc" in particularly) are needed in
557          * advance to create dv/verbs device context. We proceed the
558          * devargs here to get ones, and later proceed devargs again
559          * to override some hardware settings.
560          */
561         err = mlx5_args(&config, dpdk_dev->devargs);
562         if (err) {
563                 err = rte_errno;
564                 DRV_LOG(ERR, "failed to process device arguments: %s",
565                         strerror(rte_errno));
566                 goto error;
567         }
568         sh = mlx5_alloc_shared_dev_ctx(spawn, &config);
569         if (!sh)
570                 return NULL;
571         config.devx = sh->devx;
572 #ifdef HAVE_MLX5DV_DR_ACTION_DEST_DEVX_TIR
573         config.dest_tir = 1;
574 #endif
575 #ifdef HAVE_IBV_MLX5_MOD_SWP
576         dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_SWP;
577 #endif
578         /*
579          * Multi-packet send is supported by ConnectX-4 Lx PF as well
580          * as all ConnectX-5 devices.
581          */
582 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
583         dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS;
584 #endif
585 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
586         dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_STRIDING_RQ;
587 #endif
588         mlx5_glue->dv_query_device(sh->ctx, &dv_attr);
589         if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED) {
590                 if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW) {
591                         DRV_LOG(DEBUG, "enhanced MPW is supported");
592                         mps = MLX5_MPW_ENHANCED;
593                 } else {
594                         DRV_LOG(DEBUG, "MPW is supported");
595                         mps = MLX5_MPW;
596                 }
597         } else {
598                 DRV_LOG(DEBUG, "MPW isn't supported");
599                 mps = MLX5_MPW_DISABLED;
600         }
601 #ifdef HAVE_IBV_MLX5_MOD_SWP
602         if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_SWP)
603                 swp = dv_attr.sw_parsing_caps.sw_parsing_offloads;
604         DRV_LOG(DEBUG, "SWP support: %u", swp);
605 #endif
606         config.swp = !!swp;
607 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
608         if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_STRIDING_RQ) {
609                 struct mlx5dv_striding_rq_caps mprq_caps =
610                         dv_attr.striding_rq_caps;
611
612                 DRV_LOG(DEBUG, "\tmin_single_stride_log_num_of_bytes: %d",
613                         mprq_caps.min_single_stride_log_num_of_bytes);
614                 DRV_LOG(DEBUG, "\tmax_single_stride_log_num_of_bytes: %d",
615                         mprq_caps.max_single_stride_log_num_of_bytes);
616                 DRV_LOG(DEBUG, "\tmin_single_wqe_log_num_of_strides: %d",
617                         mprq_caps.min_single_wqe_log_num_of_strides);
618                 DRV_LOG(DEBUG, "\tmax_single_wqe_log_num_of_strides: %d",
619                         mprq_caps.max_single_wqe_log_num_of_strides);
620                 DRV_LOG(DEBUG, "\tsupported_qpts: %d",
621                         mprq_caps.supported_qpts);
622                 DRV_LOG(DEBUG, "device supports Multi-Packet RQ");
623                 mprq = 1;
624                 mprq_min_stride_size_n =
625                         mprq_caps.min_single_stride_log_num_of_bytes;
626                 mprq_max_stride_size_n =
627                         mprq_caps.max_single_stride_log_num_of_bytes;
628                 mprq_min_stride_num_n =
629                         mprq_caps.min_single_wqe_log_num_of_strides;
630                 mprq_max_stride_num_n =
631                         mprq_caps.max_single_wqe_log_num_of_strides;
632         }
633 #endif
634         if (RTE_CACHE_LINE_SIZE == 128 &&
635             !(dv_attr.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP))
636                 cqe_comp = 0;
637         else
638                 cqe_comp = 1;
639         config.cqe_comp = cqe_comp;
640 #ifdef HAVE_IBV_MLX5_MOD_CQE_128B_PAD
641         /* Whether device supports 128B Rx CQE padding. */
642         cqe_pad = RTE_CACHE_LINE_SIZE == 128 &&
643                   (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_PAD);
644 #endif
645 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
646         if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS) {
647                 tunnel_en = ((dv_attr.tunnel_offloads_caps &
648                               MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_VXLAN) &&
649                              (dv_attr.tunnel_offloads_caps &
650                               MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_GRE) &&
651                              (dv_attr.tunnel_offloads_caps &
652                               MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_GENEVE));
653         }
654         DRV_LOG(DEBUG, "tunnel offloading is %ssupported",
655                 tunnel_en ? "" : "not ");
656 #else
657         DRV_LOG(WARNING,
658                 "tunnel offloading disabled due to old OFED/rdma-core version");
659 #endif
660         config.tunnel_en = tunnel_en;
661 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
662         mpls_en = ((dv_attr.tunnel_offloads_caps &
663                     MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_GRE) &&
664                    (dv_attr.tunnel_offloads_caps &
665                     MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_UDP));
666         DRV_LOG(DEBUG, "MPLS over GRE/UDP tunnel offloading is %ssupported",
667                 mpls_en ? "" : "not ");
668 #else
669         DRV_LOG(WARNING, "MPLS over GRE/UDP tunnel offloading disabled due to"
670                 " old OFED/rdma-core version or firmware configuration");
671 #endif
672         config.mpls_en = mpls_en;
673         /* Check port status. */
674         err = mlx5_glue->query_port(sh->ctx, spawn->phys_port, &port_attr);
675         if (err) {
676                 DRV_LOG(ERR, "port query failed: %s", strerror(err));
677                 goto error;
678         }
679         if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
680                 DRV_LOG(ERR, "port is not configured in Ethernet mode");
681                 err = EINVAL;
682                 goto error;
683         }
684         if (port_attr.state != IBV_PORT_ACTIVE)
685                 DRV_LOG(DEBUG, "port is not active: \"%s\" (%d)",
686                         mlx5_glue->port_state_str(port_attr.state),
687                         port_attr.state);
688         /* Allocate private eth device data. */
689         priv = rte_zmalloc("ethdev private structure",
690                            sizeof(*priv),
691                            RTE_CACHE_LINE_SIZE);
692         if (priv == NULL) {
693                 DRV_LOG(ERR, "priv allocation failure");
694                 err = ENOMEM;
695                 goto error;
696         }
697         priv->sh = sh;
698         priv->dev_port = spawn->phys_port;
699         priv->pci_dev = spawn->pci_dev;
700         priv->mtu = RTE_ETHER_MTU;
701         priv->mp_id.port_id = port_id;
702         strlcpy(priv->mp_id.name, MLX5_MP_NAME, RTE_MP_MAX_NAME_LEN);
703 #ifndef RTE_ARCH_64
704         /* Initialize UAR access locks for 32bit implementations. */
705         rte_spinlock_init(&priv->uar_lock_cq);
706         for (i = 0; i < MLX5_UAR_PAGE_NUM_MAX; i++)
707                 rte_spinlock_init(&priv->uar_lock[i]);
708 #endif
709         /* Some internal functions rely on Netlink sockets, open them now. */
710         priv->nl_socket_rdma = mlx5_nl_init(NETLINK_RDMA);
711         priv->nl_socket_route = mlx5_nl_init(NETLINK_ROUTE);
712         priv->representor = !!switch_info->representor;
713         priv->master = !!switch_info->master;
714         priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
715         priv->vport_meta_tag = 0;
716         priv->vport_meta_mask = 0;
717         priv->pf_bond = spawn->pf_bond;
718 #ifdef HAVE_MLX5DV_DR_DEVX_PORT
719         /*
720          * The DevX port query API is implemented. E-Switch may use
721          * either vport or reg_c[0] metadata register to match on
722          * vport index. The engaged part of metadata register is
723          * defined by mask.
724          */
725         if (switch_info->representor || switch_info->master) {
726                 devx_port.comp_mask = MLX5DV_DEVX_PORT_VPORT |
727                                       MLX5DV_DEVX_PORT_MATCH_REG_C_0;
728                 err = mlx5_glue->devx_port_query(sh->ctx, spawn->phys_port,
729                                                  &devx_port);
730                 if (err) {
731                         DRV_LOG(WARNING,
732                                 "can't query devx port %d on device %s",
733                                 spawn->phys_port,
734                                 mlx5_os_get_dev_device_name(spawn->phys_dev));
735                         devx_port.comp_mask = 0;
736                 }
737         }
738         if (devx_port.comp_mask & MLX5DV_DEVX_PORT_MATCH_REG_C_0) {
739                 priv->vport_meta_tag = devx_port.reg_c_0.value;
740                 priv->vport_meta_mask = devx_port.reg_c_0.mask;
741                 if (!priv->vport_meta_mask) {
742                         DRV_LOG(ERR, "vport zero mask for port %d"
743                                      " on bonding device %s",
744                                      spawn->phys_port,
745                                      mlx5_os_get_dev_device_name
746                                                         (spawn->phys_dev));
747                         err = ENOTSUP;
748                         goto error;
749                 }
750                 if (priv->vport_meta_tag & ~priv->vport_meta_mask) {
751                         DRV_LOG(ERR, "invalid vport tag for port %d"
752                                      " on bonding device %s",
753                                      spawn->phys_port,
754                                      mlx5_os_get_dev_device_name
755                                                         (spawn->phys_dev));
756                         err = ENOTSUP;
757                         goto error;
758                 }
759         }
760         if (devx_port.comp_mask & MLX5DV_DEVX_PORT_VPORT) {
761                 priv->vport_id = devx_port.vport_num;
762         } else if (spawn->pf_bond >= 0) {
763                 DRV_LOG(ERR, "can't deduce vport index for port %d"
764                              " on bonding device %s",
765                              spawn->phys_port,
766                              mlx5_os_get_dev_device_name(spawn->phys_dev));
767                 err = ENOTSUP;
768                 goto error;
769         } else {
770                 /* Suppose vport index in compatible way. */
771                 priv->vport_id = switch_info->representor ?
772                                  switch_info->port_name + 1 : -1;
773         }
774 #else
775         /*
776          * Kernel/rdma_core support single E-Switch per PF configurations
777          * only and vport_id field contains the vport index for
778          * associated VF, which is deduced from representor port name.
779          * For example, let's have the IB device port 10, it has
780          * attached network device eth0, which has port name attribute
781          * pf0vf2, we can deduce the VF number as 2, and set vport index
782          * as 3 (2+1). This assigning schema should be changed if the
783          * multiple E-Switch instances per PF configurations or/and PCI
784          * subfunctions are added.
785          */
786         priv->vport_id = switch_info->representor ?
787                          switch_info->port_name + 1 : -1;
788 #endif
789         /* representor_id field keeps the unmodified VF index. */
790         priv->representor_id = switch_info->representor ?
791                                switch_info->port_name : -1;
792         /*
793          * Look for sibling devices in order to reuse their switch domain
794          * if any, otherwise allocate one.
795          */
796         MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
797                 const struct mlx5_priv *opriv =
798                         rte_eth_devices[port_id].data->dev_private;
799
800                 if (!opriv ||
801                     opriv->sh != priv->sh ||
802                         opriv->domain_id ==
803                         RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID)
804                         continue;
805                 priv->domain_id = opriv->domain_id;
806                 break;
807         }
808         if (priv->domain_id == RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
809                 err = rte_eth_switch_domain_alloc(&priv->domain_id);
810                 if (err) {
811                         err = rte_errno;
812                         DRV_LOG(ERR, "unable to allocate switch domain: %s",
813                                 strerror(rte_errno));
814                         goto error;
815                 }
816                 own_domain_id = 1;
817         }
818         /* Override some values set by hardware configuration. */
819         mlx5_args(&config, dpdk_dev->devargs);
820         err = mlx5_dev_check_sibling_config(priv, &config);
821         if (err)
822                 goto error;
823         config.hw_csum = !!(sh->device_attr.device_cap_flags_ex &
824                             IBV_DEVICE_RAW_IP_CSUM);
825         DRV_LOG(DEBUG, "checksum offloading is %ssupported",
826                 (config.hw_csum ? "" : "not "));
827 #if !defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) && \
828         !defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
829         DRV_LOG(DEBUG, "counters are not supported");
830 #endif
831 #if !defined(HAVE_IBV_FLOW_DV_SUPPORT) || !defined(HAVE_MLX5DV_DR)
832         if (config.dv_flow_en) {
833                 DRV_LOG(WARNING, "DV flow is not supported");
834                 config.dv_flow_en = 0;
835         }
836 #endif
837         config.ind_table_max_size =
838                 sh->device_attr.max_rwq_indirection_table_size;
839         /*
840          * Remove this check once DPDK supports larger/variable
841          * indirection tables.
842          */
843         if (config.ind_table_max_size > (unsigned int)ETH_RSS_RETA_SIZE_512)
844                 config.ind_table_max_size = ETH_RSS_RETA_SIZE_512;
845         DRV_LOG(DEBUG, "maximum Rx indirection table size is %u",
846                 config.ind_table_max_size);
847         config.hw_vlan_strip = !!(sh->device_attr.raw_packet_caps &
848                                   IBV_RAW_PACKET_CAP_CVLAN_STRIPPING);
849         DRV_LOG(DEBUG, "VLAN stripping is %ssupported",
850                 (config.hw_vlan_strip ? "" : "not "));
851         config.hw_fcs_strip = !!(sh->device_attr.raw_packet_caps &
852                                  IBV_RAW_PACKET_CAP_SCATTER_FCS);
853         DRV_LOG(DEBUG, "FCS stripping configuration is %ssupported",
854                 (config.hw_fcs_strip ? "" : "not "));
855 #if defined(HAVE_IBV_WQ_FLAG_RX_END_PADDING)
856         hw_padding = !!sh->device_attr.rx_pad_end_addr_align;
857 #elif defined(HAVE_IBV_WQ_FLAGS_PCI_WRITE_END_PADDING)
858         hw_padding = !!(sh->device_attr.device_cap_flags_ex &
859                         IBV_DEVICE_PCI_WRITE_END_PADDING);
860 #endif
861         if (config.hw_padding && !hw_padding) {
862                 DRV_LOG(DEBUG, "Rx end alignment padding isn't supported");
863                 config.hw_padding = 0;
864         } else if (config.hw_padding) {
865                 DRV_LOG(DEBUG, "Rx end alignment padding is enabled");
866         }
867         config.tso = (sh->device_attr.max_tso > 0 &&
868                       (sh->device_attr.tso_supported_qpts &
869                        (1 << IBV_QPT_RAW_PACKET)));
870         if (config.tso)
871                 config.tso_max_payload_sz = sh->device_attr.max_tso;
872         /*
873          * MPW is disabled by default, while the Enhanced MPW is enabled
874          * by default.
875          */
876         if (config.mps == MLX5_ARG_UNSET)
877                 config.mps = (mps == MLX5_MPW_ENHANCED) ? MLX5_MPW_ENHANCED :
878                                                           MLX5_MPW_DISABLED;
879         else
880                 config.mps = config.mps ? mps : MLX5_MPW_DISABLED;
881         DRV_LOG(INFO, "%sMPS is %s",
882                 config.mps == MLX5_MPW_ENHANCED ? "enhanced " :
883                 config.mps == MLX5_MPW ? "legacy " : "",
884                 config.mps != MLX5_MPW_DISABLED ? "enabled" : "disabled");
885         if (config.cqe_comp && !cqe_comp) {
886                 DRV_LOG(WARNING, "Rx CQE compression isn't supported");
887                 config.cqe_comp = 0;
888         }
889         if (config.cqe_pad && !cqe_pad) {
890                 DRV_LOG(WARNING, "Rx CQE padding isn't supported");
891                 config.cqe_pad = 0;
892         } else if (config.cqe_pad) {
893                 DRV_LOG(INFO, "Rx CQE padding is enabled");
894         }
895         if (config.devx) {
896                 priv->counter_fallback = 0;
897                 err = mlx5_devx_cmd_query_hca_attr(sh->ctx, &config.hca_attr);
898                 if (err) {
899                         err = -err;
900                         goto error;
901                 }
902                 if (!config.hca_attr.flow_counters_dump)
903                         priv->counter_fallback = 1;
904 #ifndef HAVE_IBV_DEVX_ASYNC
905                 priv->counter_fallback = 1;
906 #endif
907                 if (priv->counter_fallback)
908                         DRV_LOG(INFO, "Use fall-back DV counter management");
909                 /* Check for LRO support. */
910                 if (config.dest_tir && config.hca_attr.lro_cap &&
911                     config.dv_flow_en) {
912                         /* TBD check tunnel lro caps. */
913                         config.lro.supported = config.hca_attr.lro_cap;
914                         DRV_LOG(DEBUG, "Device supports LRO");
915                         /*
916                          * If LRO timeout is not configured by application,
917                          * use the minimal supported value.
918                          */
919                         if (!config.lro.timeout)
920                                 config.lro.timeout =
921                                 config.hca_attr.lro_timer_supported_periods[0];
922                         DRV_LOG(DEBUG, "LRO session timeout set to %d usec",
923                                 config.lro.timeout);
924                 }
925 #if defined(HAVE_MLX5DV_DR) && defined(HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER)
926                 if (config.hca_attr.qos.sup && config.hca_attr.qos.srtcm_sup &&
927                     config.dv_flow_en) {
928                         uint8_t reg_c_mask =
929                                 config.hca_attr.qos.flow_meter_reg_c_ids;
930                         /*
931                          * Meter needs two REG_C's for color match and pre-sfx
932                          * flow match. Here get the REG_C for color match.
933                          * REG_C_0 and REG_C_1 is reserved for metadata feature.
934                          */
935                         reg_c_mask &= 0xfc;
936                         if (__builtin_popcount(reg_c_mask) < 1) {
937                                 priv->mtr_en = 0;
938                                 DRV_LOG(WARNING, "No available register for"
939                                         " meter.");
940                         } else {
941                                 priv->mtr_color_reg = ffs(reg_c_mask) - 1 +
942                                                       REG_C_0;
943                                 priv->mtr_en = 1;
944                                 priv->mtr_reg_share =
945                                       config.hca_attr.qos.flow_meter_reg_share;
946                                 DRV_LOG(DEBUG, "The REG_C meter uses is %d",
947                                         priv->mtr_color_reg);
948                         }
949                 }
950 #endif
951         }
952         if (config.mprq.enabled && mprq) {
953                 if (config.mprq.stride_num_n &&
954                     (config.mprq.stride_num_n > mprq_max_stride_num_n ||
955                      config.mprq.stride_num_n < mprq_min_stride_num_n)) {
956                         config.mprq.stride_num_n =
957                                 RTE_MIN(RTE_MAX(MLX5_MPRQ_STRIDE_NUM_N,
958                                                 mprq_min_stride_num_n),
959                                         mprq_max_stride_num_n);
960                         DRV_LOG(WARNING,
961                                 "the number of strides"
962                                 " for Multi-Packet RQ is out of range,"
963                                 " setting default value (%u)",
964                                 1 << config.mprq.stride_num_n);
965                 }
966                 if (config.mprq.stride_size_n &&
967                     (config.mprq.stride_size_n > mprq_max_stride_size_n ||
968                      config.mprq.stride_size_n < mprq_min_stride_size_n)) {
969                         config.mprq.stride_size_n =
970                                 RTE_MIN(RTE_MAX(MLX5_MPRQ_STRIDE_SIZE_N,
971                                                 mprq_min_stride_size_n),
972                                         mprq_max_stride_size_n);
973                         DRV_LOG(WARNING,
974                                 "the size of a stride"
975                                 " for Multi-Packet RQ is out of range,"
976                                 " setting default value (%u)",
977                                 1 << config.mprq.stride_size_n);
978                 }
979                 config.mprq.min_stride_size_n = mprq_min_stride_size_n;
980                 config.mprq.max_stride_size_n = mprq_max_stride_size_n;
981         } else if (config.mprq.enabled && !mprq) {
982                 DRV_LOG(WARNING, "Multi-Packet RQ isn't supported");
983                 config.mprq.enabled = 0;
984         }
985         if (config.max_dump_files_num == 0)
986                 config.max_dump_files_num = 128;
987         eth_dev = rte_eth_dev_allocate(name);
988         if (eth_dev == NULL) {
989                 DRV_LOG(ERR, "can not allocate rte ethdev");
990                 err = ENOMEM;
991                 goto error;
992         }
993         /* Flag to call rte_eth_dev_release_port() in rte_eth_dev_close(). */
994         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
995         if (priv->representor) {
996                 eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
997                 eth_dev->data->representor_id = priv->representor_id;
998         }
999         /*
1000          * Store associated network device interface index. This index
1001          * is permanent throughout the lifetime of device. So, we may store
1002          * the ifindex here and use the cached value further.
1003          */
1004         MLX5_ASSERT(spawn->ifindex);
1005         priv->if_index = spawn->ifindex;
1006         eth_dev->data->dev_private = priv;
1007         priv->dev_data = eth_dev->data;
1008         eth_dev->data->mac_addrs = priv->mac;
1009         eth_dev->device = dpdk_dev;
1010         /* Configure the first MAC address by default. */
1011         if (mlx5_get_mac(eth_dev, &mac.addr_bytes)) {
1012                 DRV_LOG(ERR,
1013                         "port %u cannot get MAC address, is mlx5_en"
1014                         " loaded? (errno: %s)",
1015                         eth_dev->data->port_id, strerror(rte_errno));
1016                 err = ENODEV;
1017                 goto error;
1018         }
1019         DRV_LOG(INFO,
1020                 "port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
1021                 eth_dev->data->port_id,
1022                 mac.addr_bytes[0], mac.addr_bytes[1],
1023                 mac.addr_bytes[2], mac.addr_bytes[3],
1024                 mac.addr_bytes[4], mac.addr_bytes[5]);
1025 #ifdef RTE_LIBRTE_MLX5_DEBUG
1026         {
1027                 char ifname[IF_NAMESIZE];
1028
1029                 if (mlx5_get_ifname(eth_dev, &ifname) == 0)
1030                         DRV_LOG(DEBUG, "port %u ifname is \"%s\"",
1031                                 eth_dev->data->port_id, ifname);
1032                 else
1033                         DRV_LOG(DEBUG, "port %u ifname is unknown",
1034                                 eth_dev->data->port_id);
1035         }
1036 #endif
1037         /* Get actual MTU if possible. */
1038         err = mlx5_get_mtu(eth_dev, &priv->mtu);
1039         if (err) {
1040                 err = rte_errno;
1041                 goto error;
1042         }
1043         DRV_LOG(DEBUG, "port %u MTU is %u", eth_dev->data->port_id,
1044                 priv->mtu);
1045         /* Initialize burst functions to prevent crashes before link-up. */
1046         eth_dev->rx_pkt_burst = removed_rx_burst;
1047         eth_dev->tx_pkt_burst = removed_tx_burst;
1048         eth_dev->dev_ops = &mlx5_os_dev_ops;
1049         /* Register MAC address. */
1050         claim_zero(mlx5_mac_addr_add(eth_dev, &mac, 0, 0));
1051         if (config.vf && config.vf_nl_en)
1052                 mlx5_nl_mac_addr_sync(priv->nl_socket_route,
1053                                       mlx5_ifindex(eth_dev),
1054                                       eth_dev->data->mac_addrs,
1055                                       MLX5_MAX_MAC_ADDRESSES);
1056         priv->flows = 0;
1057         priv->ctrl_flows = 0;
1058         TAILQ_INIT(&priv->flow_meters);
1059         TAILQ_INIT(&priv->flow_meter_profiles);
1060         /* Hint libmlx5 to use PMD allocator for data plane resources */
1061         struct mlx5dv_ctx_allocators alctr = {
1062                 .alloc = &mlx5_alloc_verbs_buf,
1063                 .free = &mlx5_free_verbs_buf,
1064                 .data = priv,
1065         };
1066         mlx5_glue->dv_set_context_attr(sh->ctx,
1067                                        MLX5DV_CTX_ATTR_BUF_ALLOCATORS,
1068                                        (void *)((uintptr_t)&alctr));
1069         /* Bring Ethernet device up. */
1070         DRV_LOG(DEBUG, "port %u forcing Ethernet interface up",
1071                 eth_dev->data->port_id);
1072         mlx5_set_link_up(eth_dev);
1073         /*
1074          * Even though the interrupt handler is not installed yet,
1075          * interrupts will still trigger on the async_fd from
1076          * Verbs context returned by ibv_open_device().
1077          */
1078         mlx5_link_update(eth_dev, 0);
1079 #ifdef HAVE_MLX5DV_DR_ESWITCH
1080         if (!(config.hca_attr.eswitch_manager && config.dv_flow_en &&
1081               (switch_info->representor || switch_info->master)))
1082                 config.dv_esw_en = 0;
1083 #else
1084         config.dv_esw_en = 0;
1085 #endif
1086         /* Detect minimal data bytes to inline. */
1087         mlx5_set_min_inline(spawn, &config);
1088         /* Store device configuration on private structure. */
1089         priv->config = config;
1090         /* Create context for virtual machine VLAN workaround. */
1091         priv->vmwa_context = mlx5_vlan_vmwa_init(eth_dev, spawn->ifindex);
1092         if (config.dv_flow_en) {
1093                 err = mlx5_alloc_shared_dr(priv);
1094                 if (err)
1095                         goto error;
1096                 /*
1097                  * RSS id is shared with meter flow id. Meter flow id can only
1098                  * use the 24 MSB of the register.
1099                  */
1100                 priv->qrss_id_pool = mlx5_flow_id_pool_alloc(UINT32_MAX >>
1101                                      MLX5_MTR_COLOR_BITS);
1102                 if (!priv->qrss_id_pool) {
1103                         DRV_LOG(ERR, "can't create flow id pool");
1104                         err = ENOMEM;
1105                         goto error;
1106                 }
1107         }
1108         /* Supported Verbs flow priority number detection. */
1109         err = mlx5_flow_discover_priorities(eth_dev);
1110         if (err < 0) {
1111                 err = -err;
1112                 goto error;
1113         }
1114         priv->config.flow_prio = err;
1115         if (!priv->config.dv_esw_en &&
1116             priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
1117                 DRV_LOG(WARNING, "metadata mode %u is not supported "
1118                                  "(no E-Switch)", priv->config.dv_xmeta_en);
1119                 priv->config.dv_xmeta_en = MLX5_XMETA_MODE_LEGACY;
1120         }
1121         mlx5_set_metadata_mask(eth_dev);
1122         if (priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
1123             !priv->sh->dv_regc0_mask) {
1124                 DRV_LOG(ERR, "metadata mode %u is not supported "
1125                              "(no metadata reg_c[0] is available)",
1126                              priv->config.dv_xmeta_en);
1127                         err = ENOTSUP;
1128                         goto error;
1129         }
1130         /*
1131          * Allocate the buffer for flow creating, just once.
1132          * The allocation must be done before any flow creating.
1133          */
1134         mlx5_flow_alloc_intermediate(eth_dev);
1135         /* Query availability of metadata reg_c's. */
1136         err = mlx5_flow_discover_mreg_c(eth_dev);
1137         if (err < 0) {
1138                 err = -err;
1139                 goto error;
1140         }
1141         if (!mlx5_flow_ext_mreg_supported(eth_dev)) {
1142                 DRV_LOG(DEBUG,
1143                         "port %u extensive metadata register is not supported",
1144                         eth_dev->data->port_id);
1145                 if (priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
1146                         DRV_LOG(ERR, "metadata mode %u is not supported "
1147                                      "(no metadata registers available)",
1148                                      priv->config.dv_xmeta_en);
1149                         err = ENOTSUP;
1150                         goto error;
1151                 }
1152         }
1153         if (priv->config.dv_flow_en &&
1154             priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
1155             mlx5_flow_ext_mreg_supported(eth_dev) &&
1156             priv->sh->dv_regc0_mask) {
1157                 priv->mreg_cp_tbl = mlx5_hlist_create(MLX5_FLOW_MREG_HNAME,
1158                                                       MLX5_FLOW_MREG_HTABLE_SZ);
1159                 if (!priv->mreg_cp_tbl) {
1160                         err = ENOMEM;
1161                         goto error;
1162                 }
1163         }
1164         return eth_dev;
1165 error:
1166         if (priv) {
1167                 if (priv->mreg_cp_tbl)
1168                         mlx5_hlist_destroy(priv->mreg_cp_tbl, NULL, NULL);
1169                 if (priv->sh)
1170                         mlx5_os_free_shared_dr(priv);
1171                 if (priv->nl_socket_route >= 0)
1172                         close(priv->nl_socket_route);
1173                 if (priv->nl_socket_rdma >= 0)
1174                         close(priv->nl_socket_rdma);
1175                 if (priv->vmwa_context)
1176                         mlx5_vlan_vmwa_exit(priv->vmwa_context);
1177                 if (priv->qrss_id_pool)
1178                         mlx5_flow_id_pool_release(priv->qrss_id_pool);
1179                 if (own_domain_id)
1180                         claim_zero(rte_eth_switch_domain_free(priv->domain_id));
1181                 rte_free(priv);
1182                 if (eth_dev != NULL)
1183                         eth_dev->data->dev_private = NULL;
1184         }
1185         if (eth_dev != NULL) {
1186                 /* mac_addrs must not be freed alone because part of
1187                  * dev_private
1188                  **/
1189                 eth_dev->data->mac_addrs = NULL;
1190                 rte_eth_dev_release_port(eth_dev);
1191         }
1192         if (sh)
1193                 mlx5_free_shared_dev_ctx(sh);
1194         MLX5_ASSERT(err > 0);
1195         rte_errno = err;
1196         return NULL;
1197 }
1198
1199 /**
1200  * Comparison callback to sort device data.
1201  *
1202  * This is meant to be used with qsort().
1203  *
1204  * @param a[in]
1205  *   Pointer to pointer to first data object.
1206  * @param b[in]
1207  *   Pointer to pointer to second data object.
1208  *
1209  * @return
1210  *   0 if both objects are equal, less than 0 if the first argument is less
1211  *   than the second, greater than 0 otherwise.
1212  */
1213 static int
1214 mlx5_dev_spawn_data_cmp(const void *a, const void *b)
1215 {
1216         const struct mlx5_switch_info *si_a =
1217                 &((const struct mlx5_dev_spawn_data *)a)->info;
1218         const struct mlx5_switch_info *si_b =
1219                 &((const struct mlx5_dev_spawn_data *)b)->info;
1220         int ret;
1221
1222         /* Master device first. */
1223         ret = si_b->master - si_a->master;
1224         if (ret)
1225                 return ret;
1226         /* Then representor devices. */
1227         ret = si_b->representor - si_a->representor;
1228         if (ret)
1229                 return ret;
1230         /* Unidentified devices come last in no specific order. */
1231         if (!si_a->representor)
1232                 return 0;
1233         /* Order representors by name. */
1234         return si_a->port_name - si_b->port_name;
1235 }
1236
1237 /**
1238  * Match PCI information for possible slaves of bonding device.
1239  *
1240  * @param[in] ibv_dev
1241  *   Pointer to Infiniband device structure.
1242  * @param[in] pci_dev
1243  *   Pointer to PCI device structure to match PCI address.
1244  * @param[in] nl_rdma
1245  *   Netlink RDMA group socket handle.
1246  *
1247  * @return
1248  *   negative value if no bonding device found, otherwise
1249  *   positive index of slave PF in bonding.
1250  */
1251 static int
1252 mlx5_device_bond_pci_match(const struct ibv_device *ibv_dev,
1253                            const struct rte_pci_device *pci_dev,
1254                            int nl_rdma)
1255 {
1256         char ifname[IF_NAMESIZE + 1];
1257         unsigned int ifindex;
1258         unsigned int np, i;
1259         FILE *file = NULL;
1260         int pf = -1;
1261
1262         /*
1263          * Try to get master device name. If something goes
1264          * wrong suppose the lack of kernel support and no
1265          * bonding devices.
1266          */
1267         if (nl_rdma < 0)
1268                 return -1;
1269         if (!strstr(ibv_dev->name, "bond"))
1270                 return -1;
1271         np = mlx5_nl_portnum(nl_rdma, ibv_dev->name);
1272         if (!np)
1273                 return -1;
1274         /*
1275          * The Master device might not be on the predefined
1276          * port (not on port index 1, it is not garanted),
1277          * we have to scan all Infiniband device port and
1278          * find master.
1279          */
1280         for (i = 1; i <= np; ++i) {
1281                 /* Check whether Infiniband port is populated. */
1282                 ifindex = mlx5_nl_ifindex(nl_rdma, ibv_dev->name, i);
1283                 if (!ifindex)
1284                         continue;
1285                 if (!if_indextoname(ifindex, ifname))
1286                         continue;
1287                 /* Try to read bonding slave names from sysfs. */
1288                 MKSTR(slaves,
1289                       "/sys/class/net/%s/master/bonding/slaves", ifname);
1290                 file = fopen(slaves, "r");
1291                 if (file)
1292                         break;
1293         }
1294         if (!file)
1295                 return -1;
1296         /* Use safe format to check maximal buffer length. */
1297         MLX5_ASSERT(atol(RTE_STR(IF_NAMESIZE)) == IF_NAMESIZE);
1298         while (fscanf(file, "%" RTE_STR(IF_NAMESIZE) "s", ifname) == 1) {
1299                 char tmp_str[IF_NAMESIZE + 32];
1300                 struct rte_pci_addr pci_addr;
1301                 struct mlx5_switch_info info;
1302
1303                 /* Process slave interface names in the loop. */
1304                 snprintf(tmp_str, sizeof(tmp_str),
1305                          "/sys/class/net/%s", ifname);
1306                 if (mlx5_dev_to_pci_addr(tmp_str, &pci_addr)) {
1307                         DRV_LOG(WARNING, "can not get PCI address"
1308                                          " for netdev \"%s\"", ifname);
1309                         continue;
1310                 }
1311                 if (pci_dev->addr.domain != pci_addr.domain ||
1312                     pci_dev->addr.bus != pci_addr.bus ||
1313                     pci_dev->addr.devid != pci_addr.devid ||
1314                     pci_dev->addr.function != pci_addr.function)
1315                         continue;
1316                 /* Slave interface PCI address match found. */
1317                 fclose(file);
1318                 snprintf(tmp_str, sizeof(tmp_str),
1319                          "/sys/class/net/%s/phys_port_name", ifname);
1320                 file = fopen(tmp_str, "rb");
1321                 if (!file)
1322                         break;
1323                 info.name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET;
1324                 if (fscanf(file, "%32s", tmp_str) == 1)
1325                         mlx5_translate_port_name(tmp_str, &info);
1326                 if (info.name_type == MLX5_PHYS_PORT_NAME_TYPE_LEGACY ||
1327                     info.name_type == MLX5_PHYS_PORT_NAME_TYPE_UPLINK)
1328                         pf = info.port_name;
1329                 break;
1330         }
1331         if (file)
1332                 fclose(file);
1333         return pf;
1334 }
1335
1336 /**
1337  * DPDK callback to register a PCI device.
1338  *
1339  * This function spawns Ethernet devices out of a given PCI device.
1340  *
1341  * @param[in] pci_drv
1342  *   PCI driver structure (mlx5_driver).
1343  * @param[in] pci_dev
1344  *   PCI device information.
1345  *
1346  * @return
1347  *   0 on success, a negative errno value otherwise and rte_errno is set.
1348  */
1349 int
1350 mlx5_os_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1351                   struct rte_pci_device *pci_dev)
1352 {
1353         struct ibv_device **ibv_list;
1354         /*
1355          * Number of found IB Devices matching with requested PCI BDF.
1356          * nd != 1 means there are multiple IB devices over the same
1357          * PCI device and we have representors and master.
1358          */
1359         unsigned int nd = 0;
1360         /*
1361          * Number of found IB device Ports. nd = 1 and np = 1..n means
1362          * we have the single multiport IB device, and there may be
1363          * representors attached to some of found ports.
1364          */
1365         unsigned int np = 0;
1366         /*
1367          * Number of DPDK ethernet devices to Spawn - either over
1368          * multiple IB devices or multiple ports of single IB device.
1369          * Actually this is the number of iterations to spawn.
1370          */
1371         unsigned int ns = 0;
1372         /*
1373          * Bonding device
1374          *   < 0 - no bonding device (single one)
1375          *  >= 0 - bonding device (value is slave PF index)
1376          */
1377         int bd = -1;
1378         struct mlx5_dev_spawn_data *list = NULL;
1379         struct mlx5_dev_config dev_config;
1380         int ret;
1381
1382         if (mlx5_class_get(pci_dev->device.devargs) != MLX5_CLASS_NET) {
1383                 DRV_LOG(DEBUG, "Skip probing - should be probed by other mlx5"
1384                         " driver.");
1385                 return 1;
1386         }
1387         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1388                 mlx5_pmd_socket_init();
1389         ret = mlx5_init_once();
1390         if (ret) {
1391                 DRV_LOG(ERR, "unable to init PMD global data: %s",
1392                         strerror(rte_errno));
1393                 return -rte_errno;
1394         }
1395         MLX5_ASSERT(pci_drv == &mlx5_driver);
1396         errno = 0;
1397         ibv_list = mlx5_glue->get_device_list(&ret);
1398         if (!ibv_list) {
1399                 rte_errno = errno ? errno : ENOSYS;
1400                 DRV_LOG(ERR, "cannot list devices, is ib_uverbs loaded?");
1401                 return -rte_errno;
1402         }
1403         /*
1404          * First scan the list of all Infiniband devices to find
1405          * matching ones, gathering into the list.
1406          */
1407         struct ibv_device *ibv_match[ret + 1];
1408         int nl_route = mlx5_nl_init(NETLINK_ROUTE);
1409         int nl_rdma = mlx5_nl_init(NETLINK_RDMA);
1410         unsigned int i;
1411
1412         while (ret-- > 0) {
1413                 struct rte_pci_addr pci_addr;
1414
1415                 DRV_LOG(DEBUG, "checking device \"%s\"", ibv_list[ret]->name);
1416                 bd = mlx5_device_bond_pci_match
1417                                 (ibv_list[ret], pci_dev, nl_rdma);
1418                 if (bd >= 0) {
1419                         /*
1420                          * Bonding device detected. Only one match is allowed,
1421                          * the bonding is supported over multi-port IB device,
1422                          * there should be no matches on representor PCI
1423                          * functions or non VF LAG bonding devices with
1424                          * specified address.
1425                          */
1426                         if (nd) {
1427                                 DRV_LOG(ERR,
1428                                         "multiple PCI match on bonding device"
1429                                         "\"%s\" found", ibv_list[ret]->name);
1430                                 rte_errno = ENOENT;
1431                                 ret = -rte_errno;
1432                                 goto exit;
1433                         }
1434                         DRV_LOG(INFO, "PCI information matches for"
1435                                       " slave %d bonding device \"%s\"",
1436                                       bd, ibv_list[ret]->name);
1437                         ibv_match[nd++] = ibv_list[ret];
1438                         break;
1439                 }
1440                 if (mlx5_dev_to_pci_addr
1441                         (ibv_list[ret]->ibdev_path, &pci_addr))
1442                         continue;
1443                 if (pci_dev->addr.domain != pci_addr.domain ||
1444                     pci_dev->addr.bus != pci_addr.bus ||
1445                     pci_dev->addr.devid != pci_addr.devid ||
1446                     pci_dev->addr.function != pci_addr.function)
1447                         continue;
1448                 DRV_LOG(INFO, "PCI information matches for device \"%s\"",
1449                         ibv_list[ret]->name);
1450                 ibv_match[nd++] = ibv_list[ret];
1451         }
1452         ibv_match[nd] = NULL;
1453         if (!nd) {
1454                 /* No device matches, just complain and bail out. */
1455                 DRV_LOG(WARNING,
1456                         "no Verbs device matches PCI device " PCI_PRI_FMT ","
1457                         " are kernel drivers loaded?",
1458                         pci_dev->addr.domain, pci_dev->addr.bus,
1459                         pci_dev->addr.devid, pci_dev->addr.function);
1460                 rte_errno = ENOENT;
1461                 ret = -rte_errno;
1462                 goto exit;
1463         }
1464         if (nd == 1) {
1465                 /*
1466                  * Found single matching device may have multiple ports.
1467                  * Each port may be representor, we have to check the port
1468                  * number and check the representors existence.
1469                  */
1470                 if (nl_rdma >= 0)
1471                         np = mlx5_nl_portnum(nl_rdma, ibv_match[0]->name);
1472                 if (!np)
1473                         DRV_LOG(WARNING, "can not get IB device \"%s\""
1474                                          " ports number", ibv_match[0]->name);
1475                 if (bd >= 0 && !np) {
1476                         DRV_LOG(ERR, "can not get ports"
1477                                      " for bonding device");
1478                         rte_errno = ENOENT;
1479                         ret = -rte_errno;
1480                         goto exit;
1481                 }
1482         }
1483 #ifndef HAVE_MLX5DV_DR_DEVX_PORT
1484         if (bd >= 0) {
1485                 /*
1486                  * This may happen if there is VF LAG kernel support and
1487                  * application is compiled with older rdma_core library.
1488                  */
1489                 DRV_LOG(ERR,
1490                         "No kernel/verbs support for VF LAG bonding found.");
1491                 rte_errno = ENOTSUP;
1492                 ret = -rte_errno;
1493                 goto exit;
1494         }
1495 #endif
1496         /*
1497          * Now we can determine the maximal
1498          * amount of devices to be spawned.
1499          */
1500         list = rte_zmalloc("device spawn data",
1501                          sizeof(struct mlx5_dev_spawn_data) *
1502                          (np ? np : nd),
1503                          RTE_CACHE_LINE_SIZE);
1504         if (!list) {
1505                 DRV_LOG(ERR, "spawn data array allocation failure");
1506                 rte_errno = ENOMEM;
1507                 ret = -rte_errno;
1508                 goto exit;
1509         }
1510         if (bd >= 0 || np > 1) {
1511                 /*
1512                  * Single IB device with multiple ports found,
1513                  * it may be E-Switch master device and representors.
1514                  * We have to perform identification through the ports.
1515                  */
1516                 MLX5_ASSERT(nl_rdma >= 0);
1517                 MLX5_ASSERT(ns == 0);
1518                 MLX5_ASSERT(nd == 1);
1519                 MLX5_ASSERT(np);
1520                 for (i = 1; i <= np; ++i) {
1521                         list[ns].max_port = np;
1522                         list[ns].phys_port = i;
1523                         list[ns].phys_dev = ibv_match[0];
1524                         list[ns].eth_dev = NULL;
1525                         list[ns].pci_dev = pci_dev;
1526                         list[ns].pf_bond = bd;
1527                         list[ns].ifindex = mlx5_nl_ifindex
1528                                 (nl_rdma,
1529                                 mlx5_os_get_dev_device_name
1530                                                 (list[ns].phys_dev), i);
1531                         if (!list[ns].ifindex) {
1532                                 /*
1533                                  * No network interface index found for the
1534                                  * specified port, it means there is no
1535                                  * representor on this port. It's OK,
1536                                  * there can be disabled ports, for example
1537                                  * if sriov_numvfs < sriov_totalvfs.
1538                                  */
1539                                 continue;
1540                         }
1541                         ret = -1;
1542                         if (nl_route >= 0)
1543                                 ret = mlx5_nl_switch_info
1544                                                (nl_route,
1545                                                 list[ns].ifindex,
1546                                                 &list[ns].info);
1547                         if (ret || (!list[ns].info.representor &&
1548                                     !list[ns].info.master)) {
1549                                 /*
1550                                  * We failed to recognize representors with
1551                                  * Netlink, let's try to perform the task
1552                                  * with sysfs.
1553                                  */
1554                                 ret =  mlx5_sysfs_switch_info
1555                                                 (list[ns].ifindex,
1556                                                  &list[ns].info);
1557                         }
1558                         if (!ret && bd >= 0) {
1559                                 switch (list[ns].info.name_type) {
1560                                 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1561                                         if (list[ns].info.port_name == bd)
1562                                                 ns++;
1563                                         break;
1564                                 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1565                                         if (list[ns].info.pf_num == bd)
1566                                                 ns++;
1567                                         break;
1568                                 default:
1569                                         break;
1570                                 }
1571                                 continue;
1572                         }
1573                         if (!ret && (list[ns].info.representor ^
1574                                      list[ns].info.master))
1575                                 ns++;
1576                 }
1577                 if (!ns) {
1578                         DRV_LOG(ERR,
1579                                 "unable to recognize master/representors"
1580                                 " on the IB device with multiple ports");
1581                         rte_errno = ENOENT;
1582                         ret = -rte_errno;
1583                         goto exit;
1584                 }
1585         } else {
1586                 /*
1587                  * The existence of several matching entries (nd > 1) means
1588                  * port representors have been instantiated. No existing Verbs
1589                  * call nor sysfs entries can tell them apart, this can only
1590                  * be done through Netlink calls assuming kernel drivers are
1591                  * recent enough to support them.
1592                  *
1593                  * In the event of identification failure through Netlink,
1594                  * try again through sysfs, then:
1595                  *
1596                  * 1. A single IB device matches (nd == 1) with single
1597                  *    port (np=0/1) and is not a representor, assume
1598                  *    no switch support.
1599                  *
1600                  * 2. Otherwise no safe assumptions can be made;
1601                  *    complain louder and bail out.
1602                  */
1603                 for (i = 0; i != nd; ++i) {
1604                         memset(&list[ns].info, 0, sizeof(list[ns].info));
1605                         list[ns].max_port = 1;
1606                         list[ns].phys_port = 1;
1607                         list[ns].phys_dev = ibv_match[i];
1608                         list[ns].eth_dev = NULL;
1609                         list[ns].pci_dev = pci_dev;
1610                         list[ns].pf_bond = -1;
1611                         list[ns].ifindex = 0;
1612                         if (nl_rdma >= 0)
1613                                 list[ns].ifindex = mlx5_nl_ifindex
1614                                 (nl_rdma,
1615                                 mlx5_os_get_dev_device_name
1616                                                 (list[ns].phys_dev), 1);
1617                         if (!list[ns].ifindex) {
1618                                 char ifname[IF_NAMESIZE];
1619
1620                                 /*
1621                                  * Netlink failed, it may happen with old
1622                                  * ib_core kernel driver (before 4.16).
1623                                  * We can assume there is old driver because
1624                                  * here we are processing single ports IB
1625                                  * devices. Let's try sysfs to retrieve
1626                                  * the ifindex. The method works for
1627                                  * master device only.
1628                                  */
1629                                 if (nd > 1) {
1630                                         /*
1631                                          * Multiple devices found, assume
1632                                          * representors, can not distinguish
1633                                          * master/representor and retrieve
1634                                          * ifindex via sysfs.
1635                                          */
1636                                         continue;
1637                                 }
1638                                 ret = mlx5_get_master_ifname
1639                                         (ibv_match[i]->ibdev_path, &ifname);
1640                                 if (!ret)
1641                                         list[ns].ifindex =
1642                                                 if_nametoindex(ifname);
1643                                 if (!list[ns].ifindex) {
1644                                         /*
1645                                          * No network interface index found
1646                                          * for the specified device, it means
1647                                          * there it is neither representor
1648                                          * nor master.
1649                                          */
1650                                         continue;
1651                                 }
1652                         }
1653                         ret = -1;
1654                         if (nl_route >= 0)
1655                                 ret = mlx5_nl_switch_info
1656                                                (nl_route,
1657                                                 list[ns].ifindex,
1658                                                 &list[ns].info);
1659                         if (ret || (!list[ns].info.representor &&
1660                                     !list[ns].info.master)) {
1661                                 /*
1662                                  * We failed to recognize representors with
1663                                  * Netlink, let's try to perform the task
1664                                  * with sysfs.
1665                                  */
1666                                 ret =  mlx5_sysfs_switch_info
1667                                                 (list[ns].ifindex,
1668                                                  &list[ns].info);
1669                         }
1670                         if (!ret && (list[ns].info.representor ^
1671                                      list[ns].info.master)) {
1672                                 ns++;
1673                         } else if ((nd == 1) &&
1674                                    !list[ns].info.representor &&
1675                                    !list[ns].info.master) {
1676                                 /*
1677                                  * Single IB device with
1678                                  * one physical port and
1679                                  * attached network device.
1680                                  * May be SRIOV is not enabled
1681                                  * or there is no representors.
1682                                  */
1683                                 DRV_LOG(INFO, "no E-Switch support detected");
1684                                 ns++;
1685                                 break;
1686                         }
1687                 }
1688                 if (!ns) {
1689                         DRV_LOG(ERR,
1690                                 "unable to recognize master/representors"
1691                                 " on the multiple IB devices");
1692                         rte_errno = ENOENT;
1693                         ret = -rte_errno;
1694                         goto exit;
1695                 }
1696         }
1697         MLX5_ASSERT(ns);
1698         /*
1699          * Sort list to probe devices in natural order for users convenience
1700          * (i.e. master first, then representors from lowest to highest ID).
1701          */
1702         qsort(list, ns, sizeof(*list), mlx5_dev_spawn_data_cmp);
1703         /* Default configuration. */
1704         dev_config = (struct mlx5_dev_config){
1705                 .hw_padding = 0,
1706                 .mps = MLX5_ARG_UNSET,
1707                 .dbnc = MLX5_ARG_UNSET,
1708                 .rx_vec_en = 1,
1709                 .txq_inline_max = MLX5_ARG_UNSET,
1710                 .txq_inline_min = MLX5_ARG_UNSET,
1711                 .txq_inline_mpw = MLX5_ARG_UNSET,
1712                 .txqs_inline = MLX5_ARG_UNSET,
1713                 .vf_nl_en = 1,
1714                 .mr_ext_memseg_en = 1,
1715                 .mprq = {
1716                         .enabled = 0, /* Disabled by default. */
1717                         .stride_num_n = 0,
1718                         .stride_size_n = 0,
1719                         .max_memcpy_len = MLX5_MPRQ_MEMCPY_DEFAULT_LEN,
1720                         .min_rxqs_num = MLX5_MPRQ_MIN_RXQS,
1721                 },
1722                 .dv_esw_en = 1,
1723                 .dv_flow_en = 1,
1724                 .log_hp_size = MLX5_ARG_UNSET,
1725         };
1726         /* Device specific configuration. */
1727         switch (pci_dev->id.device_id) {
1728         case PCI_DEVICE_ID_MELLANOX_CONNECTX4VF:
1729         case PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF:
1730         case PCI_DEVICE_ID_MELLANOX_CONNECTX5VF:
1731         case PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF:
1732         case PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF:
1733         case PCI_DEVICE_ID_MELLANOX_CONNECTX6VF:
1734         case PCI_DEVICE_ID_MELLANOX_CONNECTX6DXVF:
1735                 dev_config.vf = 1;
1736                 break;
1737         default:
1738                 break;
1739         }
1740         for (i = 0; i != ns; ++i) {
1741                 uint32_t restore;
1742
1743                 list[i].eth_dev = mlx5_dev_spawn(&pci_dev->device,
1744                                                  &list[i],
1745                                                  dev_config);
1746                 if (!list[i].eth_dev) {
1747                         if (rte_errno != EBUSY && rte_errno != EEXIST)
1748                                 break;
1749                         /* Device is disabled or already spawned. Ignore it. */
1750                         continue;
1751                 }
1752                 restore = list[i].eth_dev->data->dev_flags;
1753                 rte_eth_copy_pci_info(list[i].eth_dev, pci_dev);
1754                 /* Restore non-PCI flags cleared by the above call. */
1755                 list[i].eth_dev->data->dev_flags |= restore;
1756                 rte_eth_dev_probing_finish(list[i].eth_dev);
1757         }
1758         if (i != ns) {
1759                 DRV_LOG(ERR,
1760                         "probe of PCI device " PCI_PRI_FMT " aborted after"
1761                         " encountering an error: %s",
1762                         pci_dev->addr.domain, pci_dev->addr.bus,
1763                         pci_dev->addr.devid, pci_dev->addr.function,
1764                         strerror(rte_errno));
1765                 ret = -rte_errno;
1766                 /* Roll back. */
1767                 while (i--) {
1768                         if (!list[i].eth_dev)
1769                                 continue;
1770                         mlx5_dev_close(list[i].eth_dev);
1771                         /* mac_addrs must not be freed because in dev_private */
1772                         list[i].eth_dev->data->mac_addrs = NULL;
1773                         claim_zero(rte_eth_dev_release_port(list[i].eth_dev));
1774                 }
1775                 /* Restore original error. */
1776                 rte_errno = -ret;
1777         } else {
1778                 ret = 0;
1779         }
1780 exit:
1781         /*
1782          * Do the routine cleanup:
1783          * - close opened Netlink sockets
1784          * - free allocated spawn data array
1785          * - free the Infiniband device list
1786          */
1787         if (nl_rdma >= 0)
1788                 close(nl_rdma);
1789         if (nl_route >= 0)
1790                 close(nl_route);
1791         if (list)
1792                 rte_free(list);
1793         MLX5_ASSERT(ibv_list);
1794         mlx5_glue->free_device_list(ibv_list);
1795         return ret;
1796 }
1797
1798 static int
1799 mlx5_config_doorbell_mapping_env(const struct mlx5_dev_config *config)
1800 {
1801         char *env;
1802         int value;
1803
1804         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
1805         /* Get environment variable to store. */
1806         env = getenv(MLX5_SHUT_UP_BF);
1807         value = env ? !!strcmp(env, "0") : MLX5_ARG_UNSET;
1808         if (config->dbnc == MLX5_ARG_UNSET)
1809                 setenv(MLX5_SHUT_UP_BF, MLX5_SHUT_UP_BF_DEFAULT, 1);
1810         else
1811                 setenv(MLX5_SHUT_UP_BF,
1812                        config->dbnc == MLX5_TXDB_NCACHED ? "1" : "0", 1);
1813         return value;
1814 }
1815
1816 static void
1817 mlx5_restore_doorbell_mapping_env(int value)
1818 {
1819         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
1820         /* Restore the original environment variable state. */
1821         if (value == MLX5_ARG_UNSET)
1822                 unsetenv(MLX5_SHUT_UP_BF);
1823         else
1824                 setenv(MLX5_SHUT_UP_BF, value ? "1" : "0", 1);
1825 }
1826
1827 /**
1828  * Extract pdn of PD object using DV API.
1829  *
1830  * @param[in] pd
1831  *   Pointer to the verbs PD object.
1832  * @param[out] pdn
1833  *   Pointer to the PD object number variable.
1834  *
1835  * @return
1836  *   0 on success, error value otherwise.
1837  */
1838 int
1839 mlx5_os_get_pdn(void *pd, uint32_t *pdn)
1840 {
1841 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
1842         struct mlx5dv_obj obj;
1843         struct mlx5dv_pd pd_info;
1844         int ret = 0;
1845
1846         obj.pd.in = pd;
1847         obj.pd.out = &pd_info;
1848         ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_PD);
1849         if (ret) {
1850                 DRV_LOG(DEBUG, "Fail to get PD object info");
1851                 return ret;
1852         }
1853         *pdn = pd_info.pdn;
1854         return 0;
1855 #else
1856         (void)pd;
1857         (void)pdn;
1858         return -ENOTSUP;
1859 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */
1860 }
1861
1862 /**
1863  * Function API to open IB device.
1864  *
1865  * This function calls the Linux glue APIs to open a device.
1866  *
1867  * @param[in] spawn
1868  *   Pointer to the IB device attributes (name, port, etc).
1869  * @param[out] config
1870  *   Pointer to device configuration structure.
1871  * @param[out] sh
1872  *   Pointer to shared context structure.
1873  *
1874  * @return
1875  *   0 on success, a positive error value otherwise.
1876  */
1877 int
1878 mlx5_os_open_device(const struct mlx5_dev_spawn_data *spawn,
1879                      const struct mlx5_dev_config *config,
1880                      struct mlx5_dev_ctx_shared *sh)
1881 {
1882         int dbmap_env;
1883         int err = 0;
1884         /*
1885          * Configure environment variable "MLX5_BF_SHUT_UP"
1886          * before the device creation. The rdma_core library
1887          * checks the variable at device creation and
1888          * stores the result internally.
1889          */
1890         dbmap_env = mlx5_config_doorbell_mapping_env(config);
1891         /* Try to open IB device with DV first, then usual Verbs. */
1892         errno = 0;
1893         sh->ctx = mlx5_glue->dv_open_device(spawn->phys_dev);
1894         if (sh->ctx) {
1895                 sh->devx = 1;
1896                 DRV_LOG(DEBUG, "DevX is supported");
1897                 /* The device is created, no need for environment. */
1898                 mlx5_restore_doorbell_mapping_env(dbmap_env);
1899         } else {
1900                 /* The environment variable is still configured. */
1901                 sh->ctx = mlx5_glue->open_device(spawn->phys_dev);
1902                 err = errno ? errno : ENODEV;
1903                 /*
1904                  * The environment variable is not needed anymore,
1905                  * all device creation attempts are completed.
1906                  */
1907                 mlx5_restore_doorbell_mapping_env(dbmap_env);
1908                 if (!sh->ctx)
1909                         return err;
1910                 DRV_LOG(DEBUG, "DevX is NOT supported");
1911                 err = 0;
1912         }
1913         return err;
1914 }
1915
1916 /**
1917  * Install shared asynchronous device events handler.
1918  * This function is implemented to support event sharing
1919  * between multiple ports of single IB device.
1920  *
1921  * @param sh
1922  *   Pointer to mlx5_dev_ctx_shared object.
1923  */
1924 void
1925 mlx5_os_dev_shared_handler_install(struct mlx5_dev_ctx_shared *sh)
1926 {
1927         int ret;
1928         int flags;
1929
1930         sh->intr_handle.fd = -1;
1931         flags = fcntl(((struct ibv_context *)sh->ctx)->async_fd, F_GETFL);
1932         ret = fcntl(((struct ibv_context *)sh->ctx)->async_fd,
1933                     F_SETFL, flags | O_NONBLOCK);
1934         if (ret) {
1935                 DRV_LOG(INFO, "failed to change file descriptor async event"
1936                         " queue");
1937         } else {
1938                 sh->intr_handle.fd = ((struct ibv_context *)sh->ctx)->async_fd;
1939                 sh->intr_handle.type = RTE_INTR_HANDLE_EXT;
1940                 if (rte_intr_callback_register(&sh->intr_handle,
1941                                         mlx5_dev_interrupt_handler, sh)) {
1942                         DRV_LOG(INFO, "Fail to install the shared interrupt.");
1943                         sh->intr_handle.fd = -1;
1944                 }
1945         }
1946         if (sh->devx) {
1947 #ifdef HAVE_IBV_DEVX_ASYNC
1948                 sh->intr_handle_devx.fd = -1;
1949                 sh->devx_comp =
1950                         (void *)mlx5_glue->devx_create_cmd_comp(sh->ctx);
1951                 struct mlx5dv_devx_cmd_comp *devx_comp = sh->devx_comp;
1952                 if (!devx_comp) {
1953                         DRV_LOG(INFO, "failed to allocate devx_comp.");
1954                         return;
1955                 }
1956                 flags = fcntl(devx_comp->fd, F_GETFL);
1957                 ret = fcntl(devx_comp->fd, F_SETFL, flags | O_NONBLOCK);
1958                 if (ret) {
1959                         DRV_LOG(INFO, "failed to change file descriptor"
1960                                 " devx comp");
1961                         return;
1962                 }
1963                 sh->intr_handle_devx.fd = devx_comp->fd;
1964                 sh->intr_handle_devx.type = RTE_INTR_HANDLE_EXT;
1965                 if (rte_intr_callback_register(&sh->intr_handle_devx,
1966                                         mlx5_dev_interrupt_handler_devx, sh)) {
1967                         DRV_LOG(INFO, "Fail to install the devx shared"
1968                                 " interrupt.");
1969                         sh->intr_handle_devx.fd = -1;
1970                 }
1971 #endif /* HAVE_IBV_DEVX_ASYNC */
1972         }
1973 }
1974
1975 /**
1976  * Uninstall shared asynchronous device events handler.
1977  * This function is implemented to support event sharing
1978  * between multiple ports of single IB device.
1979  *
1980  * @param dev
1981  *   Pointer to mlx5_dev_ctx_shared object.
1982  */
1983 void
1984 mlx5_os_dev_shared_handler_uninstall(struct mlx5_dev_ctx_shared *sh)
1985 {
1986         if (sh->intr_handle.fd >= 0)
1987                 mlx5_intr_callback_unregister(&sh->intr_handle,
1988                                               mlx5_dev_interrupt_handler, sh);
1989 #ifdef HAVE_IBV_DEVX_ASYNC
1990         if (sh->intr_handle_devx.fd >= 0)
1991                 rte_intr_callback_unregister(&sh->intr_handle_devx,
1992                                   mlx5_dev_interrupt_handler_devx, sh);
1993         if (sh->devx_comp)
1994                 mlx5_glue->devx_destroy_cmd_comp(sh->devx_comp);
1995 #endif
1996 }
1997
1998 /**
1999  * Read statistics by a named counter.
2000  *
2001  * @param[in] priv
2002  *   Pointer to the private device data structure.
2003  * @param[in] ctr_name
2004  *   Pointer to the name of the statistic counter to read
2005  * @param[out] stat
2006  *   Pointer to read statistic value.
2007  * @return
2008  *   0 on success and stat is valud, 1 if failed to read the value
2009  *   rte_errno is set.
2010  *
2011  */
2012 int
2013 mlx5_os_read_dev_stat(struct mlx5_priv *priv, const char *ctr_name,
2014                       uint64_t *stat)
2015 {
2016         int fd;
2017
2018         if (priv->sh) {
2019                 MKSTR(path, "%s/ports/%d/hw_counters/%s",
2020                           priv->sh->ibdev_path,
2021                           priv->dev_port,
2022                           ctr_name);
2023                 fd = open(path, O_RDONLY);
2024                 if (fd != -1) {
2025                         char buf[21] = {'\0'};
2026                         ssize_t n = read(fd, buf, sizeof(buf));
2027
2028                         close(fd);
2029                         if (n != -1) {
2030                                 *stat = strtoull(buf, NULL, 10);
2031                                 return 0;
2032                         }
2033                 }
2034         }
2035         *stat = 0;
2036         return 1;
2037 }
2038
2039 /**
2040  * Read device counters table.
2041  *
2042  * @param dev
2043  *   Pointer to Ethernet device.
2044  * @param[out] stats
2045  *   Counters table output buffer.
2046  *
2047  * @return
2048  *   0 on success and stats is filled, negative errno value otherwise and
2049  *   rte_errno is set.
2050  */
2051 int
2052 mlx5_os_read_dev_counters(struct rte_eth_dev *dev, uint64_t *stats)
2053 {
2054         struct mlx5_priv *priv = dev->data->dev_private;
2055         struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
2056         unsigned int i;
2057         struct ifreq ifr;
2058         unsigned int stats_sz = xstats_ctrl->stats_n * sizeof(uint64_t);
2059         unsigned char et_stat_buf[sizeof(struct ethtool_stats) + stats_sz];
2060         struct ethtool_stats *et_stats = (struct ethtool_stats *)et_stat_buf;
2061         int ret;
2062
2063         et_stats->cmd = ETHTOOL_GSTATS;
2064         et_stats->n_stats = xstats_ctrl->stats_n;
2065         ifr.ifr_data = (caddr_t)et_stats;
2066         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2067         if (ret) {
2068                 DRV_LOG(WARNING,
2069                         "port %u unable to read statistic values from device",
2070                         dev->data->port_id);
2071                 return ret;
2072         }
2073         for (i = 0; i != xstats_ctrl->mlx5_stats_n; ++i) {
2074                 if (xstats_ctrl->info[i].dev) {
2075                         ret = mlx5_os_read_dev_stat(priv,
2076                                             xstats_ctrl->info[i].ctr_name,
2077                                             &stats[i]);
2078                         /* return last xstats counter if fail to read. */
2079                         if (ret == 0)
2080                                 xstats_ctrl->xstats[i] = stats[i];
2081                         else
2082                                 stats[i] = xstats_ctrl->xstats[i];
2083                 } else {
2084                         stats[i] = (uint64_t)
2085                                 et_stats->data[xstats_ctrl->dev_table_idx[i]];
2086                 }
2087         }
2088         return 0;
2089 }
2090
2091 /**
2092  * Query the number of statistics provided by ETHTOOL.
2093  *
2094  * @param dev
2095  *   Pointer to Ethernet device.
2096  *
2097  * @return
2098  *   Number of statistics on success, negative errno value otherwise and
2099  *   rte_errno is set.
2100  */
2101 int
2102 mlx5_os_get_stats_n(struct rte_eth_dev *dev)
2103 {
2104         struct ethtool_drvinfo drvinfo;
2105         struct ifreq ifr;
2106         int ret;
2107
2108         drvinfo.cmd = ETHTOOL_GDRVINFO;
2109         ifr.ifr_data = (caddr_t)&drvinfo;
2110         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2111         if (ret) {
2112                 DRV_LOG(WARNING, "port %u unable to query number of statistics",
2113                         dev->data->port_id);
2114                 return ret;
2115         }
2116         return drvinfo.n_stats;
2117 }
2118
2119 static const struct mlx5_counter_ctrl mlx5_counters_init[] = {
2120         {
2121                 .dpdk_name = "rx_port_unicast_bytes",
2122                 .ctr_name = "rx_vport_unicast_bytes",
2123         },
2124         {
2125                 .dpdk_name = "rx_port_multicast_bytes",
2126                 .ctr_name = "rx_vport_multicast_bytes",
2127         },
2128         {
2129                 .dpdk_name = "rx_port_broadcast_bytes",
2130                 .ctr_name = "rx_vport_broadcast_bytes",
2131         },
2132         {
2133                 .dpdk_name = "rx_port_unicast_packets",
2134                 .ctr_name = "rx_vport_unicast_packets",
2135         },
2136         {
2137                 .dpdk_name = "rx_port_multicast_packets",
2138                 .ctr_name = "rx_vport_multicast_packets",
2139         },
2140         {
2141                 .dpdk_name = "rx_port_broadcast_packets",
2142                 .ctr_name = "rx_vport_broadcast_packets",
2143         },
2144         {
2145                 .dpdk_name = "tx_port_unicast_bytes",
2146                 .ctr_name = "tx_vport_unicast_bytes",
2147         },
2148         {
2149                 .dpdk_name = "tx_port_multicast_bytes",
2150                 .ctr_name = "tx_vport_multicast_bytes",
2151         },
2152         {
2153                 .dpdk_name = "tx_port_broadcast_bytes",
2154                 .ctr_name = "tx_vport_broadcast_bytes",
2155         },
2156         {
2157                 .dpdk_name = "tx_port_unicast_packets",
2158                 .ctr_name = "tx_vport_unicast_packets",
2159         },
2160         {
2161                 .dpdk_name = "tx_port_multicast_packets",
2162                 .ctr_name = "tx_vport_multicast_packets",
2163         },
2164         {
2165                 .dpdk_name = "tx_port_broadcast_packets",
2166                 .ctr_name = "tx_vport_broadcast_packets",
2167         },
2168         {
2169                 .dpdk_name = "rx_wqe_err",
2170                 .ctr_name = "rx_wqe_err",
2171         },
2172         {
2173                 .dpdk_name = "rx_crc_errors_phy",
2174                 .ctr_name = "rx_crc_errors_phy",
2175         },
2176         {
2177                 .dpdk_name = "rx_in_range_len_errors_phy",
2178                 .ctr_name = "rx_in_range_len_errors_phy",
2179         },
2180         {
2181                 .dpdk_name = "rx_symbol_err_phy",
2182                 .ctr_name = "rx_symbol_err_phy",
2183         },
2184         {
2185                 .dpdk_name = "tx_errors_phy",
2186                 .ctr_name = "tx_errors_phy",
2187         },
2188         {
2189                 .dpdk_name = "rx_out_of_buffer",
2190                 .ctr_name = "out_of_buffer",
2191                 .dev = 1,
2192         },
2193         {
2194                 .dpdk_name = "tx_packets_phy",
2195                 .ctr_name = "tx_packets_phy",
2196         },
2197         {
2198                 .dpdk_name = "rx_packets_phy",
2199                 .ctr_name = "rx_packets_phy",
2200         },
2201         {
2202                 .dpdk_name = "tx_discards_phy",
2203                 .ctr_name = "tx_discards_phy",
2204         },
2205         {
2206                 .dpdk_name = "rx_discards_phy",
2207                 .ctr_name = "rx_discards_phy",
2208         },
2209         {
2210                 .dpdk_name = "tx_bytes_phy",
2211                 .ctr_name = "tx_bytes_phy",
2212         },
2213         {
2214                 .dpdk_name = "rx_bytes_phy",
2215                 .ctr_name = "rx_bytes_phy",
2216         },
2217         /* Representor only */
2218         {
2219                 .dpdk_name = "rx_packets",
2220                 .ctr_name = "vport_rx_packets",
2221         },
2222         {
2223                 .dpdk_name = "rx_bytes",
2224                 .ctr_name = "vport_rx_bytes",
2225         },
2226         {
2227                 .dpdk_name = "tx_packets",
2228                 .ctr_name = "vport_tx_packets",
2229         },
2230         {
2231                 .dpdk_name = "tx_bytes",
2232                 .ctr_name = "vport_tx_bytes",
2233         },
2234 };
2235
2236 static const unsigned int xstats_n = RTE_DIM(mlx5_counters_init);
2237
2238 /**
2239  * Init the structures to read device counters.
2240  *
2241  * @param dev
2242  *   Pointer to Ethernet device.
2243  */
2244 void
2245 mlx5_os_stats_init(struct rte_eth_dev *dev)
2246 {
2247         struct mlx5_priv *priv = dev->data->dev_private;
2248         struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
2249         struct mlx5_stats_ctrl *stats_ctrl = &priv->stats_ctrl;
2250         unsigned int i;
2251         unsigned int j;
2252         struct ifreq ifr;
2253         struct ethtool_gstrings *strings = NULL;
2254         unsigned int dev_stats_n;
2255         unsigned int str_sz;
2256         int ret;
2257
2258         /* So that it won't aggregate for each init. */
2259         xstats_ctrl->mlx5_stats_n = 0;
2260         ret = mlx5_os_get_stats_n(dev);
2261         if (ret < 0) {
2262                 DRV_LOG(WARNING, "port %u no extended statistics available",
2263                         dev->data->port_id);
2264                 return;
2265         }
2266         dev_stats_n = ret;
2267         /* Allocate memory to grab stat names and values. */
2268         str_sz = dev_stats_n * ETH_GSTRING_LEN;
2269         strings = (struct ethtool_gstrings *)
2270                   rte_malloc("xstats_strings",
2271                              str_sz + sizeof(struct ethtool_gstrings), 0);
2272         if (!strings) {
2273                 DRV_LOG(WARNING, "port %u unable to allocate memory for xstats",
2274                      dev->data->port_id);
2275                 return;
2276         }
2277         strings->cmd = ETHTOOL_GSTRINGS;
2278         strings->string_set = ETH_SS_STATS;
2279         strings->len = dev_stats_n;
2280         ifr.ifr_data = (caddr_t)strings;
2281         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2282         if (ret) {
2283                 DRV_LOG(WARNING, "port %u unable to get statistic names",
2284                         dev->data->port_id);
2285                 goto free;
2286         }
2287         for (i = 0; i != dev_stats_n; ++i) {
2288                 const char *curr_string = (const char *)
2289                         &strings->data[i * ETH_GSTRING_LEN];
2290
2291                 for (j = 0; j != xstats_n; ++j) {
2292                         if (!strcmp(mlx5_counters_init[j].ctr_name,
2293                                     curr_string)) {
2294                                 unsigned int idx = xstats_ctrl->mlx5_stats_n++;
2295
2296                                 xstats_ctrl->dev_table_idx[idx] = i;
2297                                 xstats_ctrl->info[idx] = mlx5_counters_init[j];
2298                                 break;
2299                         }
2300                 }
2301         }
2302         /* Add dev counters. */
2303         for (i = 0; i != xstats_n; ++i) {
2304                 if (mlx5_counters_init[i].dev) {
2305                         unsigned int idx = xstats_ctrl->mlx5_stats_n++;
2306
2307                         xstats_ctrl->info[idx] = mlx5_counters_init[i];
2308                         xstats_ctrl->hw_stats[idx] = 0;
2309                 }
2310         }
2311         MLX5_ASSERT(xstats_ctrl->mlx5_stats_n <= MLX5_MAX_XSTATS);
2312         xstats_ctrl->stats_n = dev_stats_n;
2313         /* Copy to base at first time. */
2314         ret = mlx5_os_read_dev_counters(dev, xstats_ctrl->base);
2315         if (ret)
2316                 DRV_LOG(ERR, "port %u cannot read device counters: %s",
2317                         dev->data->port_id, strerror(rte_errno));
2318         mlx5_os_read_dev_stat(priv, "out_of_buffer", &stats_ctrl->imissed_base);
2319         stats_ctrl->imissed = 0;
2320 free:
2321         rte_free(strings);
2322 }
2323
2324 const struct eth_dev_ops mlx5_os_dev_ops = {
2325         .dev_configure = mlx5_dev_configure,
2326         .dev_start = mlx5_dev_start,
2327         .dev_stop = mlx5_dev_stop,
2328         .dev_set_link_down = mlx5_set_link_down,
2329         .dev_set_link_up = mlx5_set_link_up,
2330         .dev_close = mlx5_dev_close,
2331         .promiscuous_enable = mlx5_promiscuous_enable,
2332         .promiscuous_disable = mlx5_promiscuous_disable,
2333         .allmulticast_enable = mlx5_allmulticast_enable,
2334         .allmulticast_disable = mlx5_allmulticast_disable,
2335         .link_update = mlx5_link_update,
2336         .stats_get = mlx5_stats_get,
2337         .stats_reset = mlx5_stats_reset,
2338         .xstats_get = mlx5_xstats_get,
2339         .xstats_reset = mlx5_xstats_reset,
2340         .xstats_get_names = mlx5_xstats_get_names,
2341         .fw_version_get = mlx5_fw_version_get,
2342         .dev_infos_get = mlx5_dev_infos_get,
2343         .read_clock = mlx5_read_clock,
2344         .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
2345         .vlan_filter_set = mlx5_vlan_filter_set,
2346         .rx_queue_setup = mlx5_rx_queue_setup,
2347         .rx_hairpin_queue_setup = mlx5_rx_hairpin_queue_setup,
2348         .tx_queue_setup = mlx5_tx_queue_setup,
2349         .tx_hairpin_queue_setup = mlx5_tx_hairpin_queue_setup,
2350         .rx_queue_release = mlx5_rx_queue_release,
2351         .tx_queue_release = mlx5_tx_queue_release,
2352         .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
2353         .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
2354         .mac_addr_remove = mlx5_mac_addr_remove,
2355         .mac_addr_add = mlx5_mac_addr_add,
2356         .mac_addr_set = mlx5_mac_addr_set,
2357         .set_mc_addr_list = mlx5_set_mc_addr_list,
2358         .mtu_set = mlx5_dev_set_mtu,
2359         .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
2360         .vlan_offload_set = mlx5_vlan_offload_set,
2361         .reta_update = mlx5_dev_rss_reta_update,
2362         .reta_query = mlx5_dev_rss_reta_query,
2363         .rss_hash_update = mlx5_rss_hash_update,
2364         .rss_hash_conf_get = mlx5_rss_hash_conf_get,
2365         .filter_ctrl = mlx5_dev_filter_ctrl,
2366         .rx_descriptor_status = mlx5_rx_descriptor_status,
2367         .tx_descriptor_status = mlx5_tx_descriptor_status,
2368         .rxq_info_get = mlx5_rxq_info_get,
2369         .txq_info_get = mlx5_txq_info_get,
2370         .rx_burst_mode_get = mlx5_rx_burst_mode_get,
2371         .tx_burst_mode_get = mlx5_tx_burst_mode_get,
2372         .rx_queue_count = mlx5_rx_queue_count,
2373         .rx_queue_intr_enable = mlx5_rx_intr_enable,
2374         .rx_queue_intr_disable = mlx5_rx_intr_disable,
2375         .is_removed = mlx5_is_removed,
2376         .udp_tunnel_port_add  = mlx5_udp_tunnel_port_add,
2377         .get_module_info = mlx5_get_module_info,
2378         .get_module_eeprom = mlx5_get_module_eeprom,
2379         .hairpin_cap_get = mlx5_hairpin_cap_get,
2380         .mtr_ops_get = mlx5_flow_meter_ops_get,
2381 };
2382
2383 /* Available operations from secondary process. */
2384 const struct eth_dev_ops mlx5_os_dev_sec_ops = {
2385         .stats_get = mlx5_stats_get,
2386         .stats_reset = mlx5_stats_reset,
2387         .xstats_get = mlx5_xstats_get,
2388         .xstats_reset = mlx5_xstats_reset,
2389         .xstats_get_names = mlx5_xstats_get_names,
2390         .fw_version_get = mlx5_fw_version_get,
2391         .dev_infos_get = mlx5_dev_infos_get,
2392         .rx_descriptor_status = mlx5_rx_descriptor_status,
2393         .tx_descriptor_status = mlx5_tx_descriptor_status,
2394         .rxq_info_get = mlx5_rxq_info_get,
2395         .txq_info_get = mlx5_txq_info_get,
2396         .rx_burst_mode_get = mlx5_rx_burst_mode_get,
2397         .tx_burst_mode_get = mlx5_tx_burst_mode_get,
2398         .get_module_info = mlx5_get_module_info,
2399         .get_module_eeprom = mlx5_get_module_eeprom,
2400 };
2401
2402 /* Available operations in flow isolated mode. */
2403 const struct eth_dev_ops mlx5_os_dev_ops_isolate = {
2404         .dev_configure = mlx5_dev_configure,
2405         .dev_start = mlx5_dev_start,
2406         .dev_stop = mlx5_dev_stop,
2407         .dev_set_link_down = mlx5_set_link_down,
2408         .dev_set_link_up = mlx5_set_link_up,
2409         .dev_close = mlx5_dev_close,
2410         .promiscuous_enable = mlx5_promiscuous_enable,
2411         .promiscuous_disable = mlx5_promiscuous_disable,
2412         .allmulticast_enable = mlx5_allmulticast_enable,
2413         .allmulticast_disable = mlx5_allmulticast_disable,
2414         .link_update = mlx5_link_update,
2415         .stats_get = mlx5_stats_get,
2416         .stats_reset = mlx5_stats_reset,
2417         .xstats_get = mlx5_xstats_get,
2418         .xstats_reset = mlx5_xstats_reset,
2419         .xstats_get_names = mlx5_xstats_get_names,
2420         .fw_version_get = mlx5_fw_version_get,
2421         .dev_infos_get = mlx5_dev_infos_get,
2422         .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
2423         .vlan_filter_set = mlx5_vlan_filter_set,
2424         .rx_queue_setup = mlx5_rx_queue_setup,
2425         .rx_hairpin_queue_setup = mlx5_rx_hairpin_queue_setup,
2426         .tx_queue_setup = mlx5_tx_queue_setup,
2427         .tx_hairpin_queue_setup = mlx5_tx_hairpin_queue_setup,
2428         .rx_queue_release = mlx5_rx_queue_release,
2429         .tx_queue_release = mlx5_tx_queue_release,
2430         .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
2431         .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
2432         .mac_addr_remove = mlx5_mac_addr_remove,
2433         .mac_addr_add = mlx5_mac_addr_add,
2434         .mac_addr_set = mlx5_mac_addr_set,
2435         .set_mc_addr_list = mlx5_set_mc_addr_list,
2436         .mtu_set = mlx5_dev_set_mtu,
2437         .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
2438         .vlan_offload_set = mlx5_vlan_offload_set,
2439         .filter_ctrl = mlx5_dev_filter_ctrl,
2440         .rx_descriptor_status = mlx5_rx_descriptor_status,
2441         .tx_descriptor_status = mlx5_tx_descriptor_status,
2442         .rxq_info_get = mlx5_rxq_info_get,
2443         .txq_info_get = mlx5_txq_info_get,
2444         .rx_burst_mode_get = mlx5_rx_burst_mode_get,
2445         .tx_burst_mode_get = mlx5_tx_burst_mode_get,
2446         .rx_queue_intr_enable = mlx5_rx_intr_enable,
2447         .rx_queue_intr_disable = mlx5_rx_intr_disable,
2448         .is_removed = mlx5_is_removed,
2449         .get_module_info = mlx5_get_module_info,
2450         .get_module_eeprom = mlx5_get_module_eeprom,
2451         .hairpin_cap_get = mlx5_hairpin_cap_get,
2452         .mtr_ops_get = mlx5_flow_meter_ops_get,
2453 };