b75d0ac155666510a709691bef85c671db0837e6
[dpdk.git] / drivers / net / mlx5 / mlx5.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 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
16 #include <rte_malloc.h>
17 #include <rte_ethdev_driver.h>
18 #include <rte_ethdev_pci.h>
19 #include <rte_pci.h>
20 #include <rte_bus_pci.h>
21 #include <rte_common.h>
22 #include <rte_kvargs.h>
23 #include <rte_rwlock.h>
24 #include <rte_spinlock.h>
25 #include <rte_string_fns.h>
26 #include <rte_alarm.h>
27
28 #include <mlx5_glue.h>
29 #include <mlx5_devx_cmds.h>
30 #include <mlx5_common.h>
31 #include <mlx5_common_os.h>
32 #include <mlx5_common_mp.h>
33 #include <mlx5_malloc.h>
34
35 #include "mlx5_defs.h"
36 #include "mlx5.h"
37 #include "mlx5_utils.h"
38 #include "mlx5_rxtx.h"
39 #include "mlx5_autoconf.h"
40 #include "mlx5_mr.h"
41 #include "mlx5_flow.h"
42 #include "rte_pmd_mlx5.h"
43
44 /* Device parameter to enable RX completion queue compression. */
45 #define MLX5_RXQ_CQE_COMP_EN "rxq_cqe_comp_en"
46
47 /* Device parameter to enable RX completion entry padding to 128B. */
48 #define MLX5_RXQ_CQE_PAD_EN "rxq_cqe_pad_en"
49
50 /* Device parameter to enable padding Rx packet to cacheline size. */
51 #define MLX5_RXQ_PKT_PAD_EN "rxq_pkt_pad_en"
52
53 /* Device parameter to enable Multi-Packet Rx queue. */
54 #define MLX5_RX_MPRQ_EN "mprq_en"
55
56 /* Device parameter to configure log 2 of the number of strides for MPRQ. */
57 #define MLX5_RX_MPRQ_LOG_STRIDE_NUM "mprq_log_stride_num"
58
59 /* Device parameter to configure log 2 of the stride size for MPRQ. */
60 #define MLX5_RX_MPRQ_LOG_STRIDE_SIZE "mprq_log_stride_size"
61
62 /* Device parameter to limit the size of memcpy'd packet for MPRQ. */
63 #define MLX5_RX_MPRQ_MAX_MEMCPY_LEN "mprq_max_memcpy_len"
64
65 /* Device parameter to set the minimum number of Rx queues to enable MPRQ. */
66 #define MLX5_RXQS_MIN_MPRQ "rxqs_min_mprq"
67
68 /* Device parameter to configure inline send. Deprecated, ignored.*/
69 #define MLX5_TXQ_INLINE "txq_inline"
70
71 /* Device parameter to limit packet size to inline with ordinary SEND. */
72 #define MLX5_TXQ_INLINE_MAX "txq_inline_max"
73
74 /* Device parameter to configure minimal data size to inline. */
75 #define MLX5_TXQ_INLINE_MIN "txq_inline_min"
76
77 /* Device parameter to limit packet size to inline with Enhanced MPW. */
78 #define MLX5_TXQ_INLINE_MPW "txq_inline_mpw"
79
80 /*
81  * Device parameter to configure the number of TX queues threshold for
82  * enabling inline send.
83  */
84 #define MLX5_TXQS_MIN_INLINE "txqs_min_inline"
85
86 /*
87  * Device parameter to configure the number of TX queues threshold for
88  * enabling vectorized Tx, deprecated, ignored (no vectorized Tx routines).
89  */
90 #define MLX5_TXQS_MAX_VEC "txqs_max_vec"
91
92 /* Device parameter to enable multi-packet send WQEs. */
93 #define MLX5_TXQ_MPW_EN "txq_mpw_en"
94
95 /*
96  * Device parameter to force doorbell register mapping
97  * to non-cahed region eliminating the extra write memory barrier.
98  */
99 #define MLX5_TX_DB_NC "tx_db_nc"
100
101 /*
102  * Device parameter to include 2 dsegs in the title WQEBB.
103  * Deprecated, ignored.
104  */
105 #define MLX5_TXQ_MPW_HDR_DSEG_EN "txq_mpw_hdr_dseg_en"
106
107 /*
108  * Device parameter to limit the size of inlining packet.
109  * Deprecated, ignored.
110  */
111 #define MLX5_TXQ_MAX_INLINE_LEN "txq_max_inline_len"
112
113 /*
114  * Device parameter to enable Tx scheduling on timestamps
115  * and specify the packet pacing granularity in nanoseconds.
116  */
117 #define MLX5_TX_PP "tx_pp"
118
119 /*
120  * Device parameter to specify skew in nanoseconds on Tx datapath,
121  * it represents the time between SQ start WQE processing and
122  * appearing actual packet data on the wire.
123  */
124 #define MLX5_TX_SKEW "tx_skew"
125
126 /*
127  * Device parameter to enable hardware Tx vector.
128  * Deprecated, ignored (no vectorized Tx routines anymore).
129  */
130 #define MLX5_TX_VEC_EN "tx_vec_en"
131
132 /* Device parameter to enable hardware Rx vector. */
133 #define MLX5_RX_VEC_EN "rx_vec_en"
134
135 /* Allow L3 VXLAN flow creation. */
136 #define MLX5_L3_VXLAN_EN "l3_vxlan_en"
137
138 /* Activate DV E-Switch flow steering. */
139 #define MLX5_DV_ESW_EN "dv_esw_en"
140
141 /* Activate DV flow steering. */
142 #define MLX5_DV_FLOW_EN "dv_flow_en"
143
144 /* Enable extensive flow metadata support. */
145 #define MLX5_DV_XMETA_EN "dv_xmeta_en"
146
147 /* Device parameter to let the user manage the lacp traffic of bonded device */
148 #define MLX5_LACP_BY_USER "lacp_by_user"
149
150 /* Activate Netlink support in VF mode. */
151 #define MLX5_VF_NL_EN "vf_nl_en"
152
153 /* Enable extending memsegs when creating a MR. */
154 #define MLX5_MR_EXT_MEMSEG_EN "mr_ext_memseg_en"
155
156 /* Select port representors to instantiate. */
157 #define MLX5_REPRESENTOR "representor"
158
159 /* Device parameter to configure the maximum number of dump files per queue. */
160 #define MLX5_MAX_DUMP_FILES_NUM "max_dump_files_num"
161
162 /* Configure timeout of LRO session (in microseconds). */
163 #define MLX5_LRO_TIMEOUT_USEC "lro_timeout_usec"
164
165 /*
166  * Device parameter to configure the total data buffer size for a single
167  * hairpin queue (logarithm value).
168  */
169 #define MLX5_HP_BUF_SIZE "hp_buf_log_sz"
170
171 /* Flow memory reclaim mode. */
172 #define MLX5_RECLAIM_MEM "reclaim_mem_mode"
173
174 /* The default memory allocator used in PMD. */
175 #define MLX5_SYS_MEM_EN "sys_mem_en"
176 /* Decap will be used or not. */
177 #define MLX5_DECAP_EN "decap_en"
178
179 /* Shared memory between primary and secondary processes. */
180 struct mlx5_shared_data *mlx5_shared_data;
181
182 /** Driver-specific log messages type. */
183 int mlx5_logtype;
184
185 static LIST_HEAD(, mlx5_dev_ctx_shared) mlx5_dev_ctx_list =
186                                                 LIST_HEAD_INITIALIZER();
187 static pthread_mutex_t mlx5_dev_ctx_list_mutex = PTHREAD_MUTEX_INITIALIZER;
188
189 static const struct mlx5_indexed_pool_config mlx5_ipool_cfg[] = {
190 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
191         {
192                 .size = sizeof(struct mlx5_flow_dv_encap_decap_resource),
193                 .trunk_size = 64,
194                 .grow_trunk = 3,
195                 .grow_shift = 2,
196                 .need_lock = 0,
197                 .release_mem_en = 1,
198                 .malloc = mlx5_malloc,
199                 .free = mlx5_free,
200                 .type = "mlx5_encap_decap_ipool",
201         },
202         {
203                 .size = sizeof(struct mlx5_flow_dv_push_vlan_action_resource),
204                 .trunk_size = 64,
205                 .grow_trunk = 3,
206                 .grow_shift = 2,
207                 .need_lock = 0,
208                 .release_mem_en = 1,
209                 .malloc = mlx5_malloc,
210                 .free = mlx5_free,
211                 .type = "mlx5_push_vlan_ipool",
212         },
213         {
214                 .size = sizeof(struct mlx5_flow_dv_tag_resource),
215                 .trunk_size = 64,
216                 .grow_trunk = 3,
217                 .grow_shift = 2,
218                 .need_lock = 0,
219                 .release_mem_en = 1,
220                 .malloc = mlx5_malloc,
221                 .free = mlx5_free,
222                 .type = "mlx5_tag_ipool",
223         },
224         {
225                 .size = sizeof(struct mlx5_flow_dv_port_id_action_resource),
226                 .trunk_size = 64,
227                 .grow_trunk = 3,
228                 .grow_shift = 2,
229                 .need_lock = 0,
230                 .release_mem_en = 1,
231                 .malloc = mlx5_malloc,
232                 .free = mlx5_free,
233                 .type = "mlx5_port_id_ipool",
234         },
235         {
236                 .size = sizeof(struct mlx5_flow_tbl_data_entry),
237                 .trunk_size = 64,
238                 .grow_trunk = 3,
239                 .grow_shift = 2,
240                 .need_lock = 0,
241                 .release_mem_en = 1,
242                 .malloc = mlx5_malloc,
243                 .free = mlx5_free,
244                 .type = "mlx5_jump_ipool",
245         },
246 #endif
247         {
248                 .size = sizeof(struct mlx5_flow_meter),
249                 .trunk_size = 64,
250                 .grow_trunk = 3,
251                 .grow_shift = 2,
252                 .need_lock = 0,
253                 .release_mem_en = 1,
254                 .malloc = mlx5_malloc,
255                 .free = mlx5_free,
256                 .type = "mlx5_meter_ipool",
257         },
258         {
259                 .size = sizeof(struct mlx5_flow_mreg_copy_resource),
260                 .trunk_size = 64,
261                 .grow_trunk = 3,
262                 .grow_shift = 2,
263                 .need_lock = 0,
264                 .release_mem_en = 1,
265                 .malloc = mlx5_malloc,
266                 .free = mlx5_free,
267                 .type = "mlx5_mcp_ipool",
268         },
269         {
270                 .size = (sizeof(struct mlx5_hrxq) + MLX5_RSS_HASH_KEY_LEN),
271                 .trunk_size = 64,
272                 .grow_trunk = 3,
273                 .grow_shift = 2,
274                 .need_lock = 0,
275                 .release_mem_en = 1,
276                 .malloc = mlx5_malloc,
277                 .free = mlx5_free,
278                 .type = "mlx5_hrxq_ipool",
279         },
280         {
281                 /*
282                  * MLX5_IPOOL_MLX5_FLOW size varies for DV and VERBS flows.
283                  * It set in run time according to PCI function configuration.
284                  */
285                 .size = 0,
286                 .trunk_size = 64,
287                 .grow_trunk = 3,
288                 .grow_shift = 2,
289                 .need_lock = 0,
290                 .release_mem_en = 1,
291                 .malloc = mlx5_malloc,
292                 .free = mlx5_free,
293                 .type = "mlx5_flow_handle_ipool",
294         },
295         {
296                 .size = sizeof(struct rte_flow),
297                 .trunk_size = 4096,
298                 .need_lock = 1,
299                 .release_mem_en = 1,
300                 .malloc = mlx5_malloc,
301                 .free = mlx5_free,
302                 .type = "rte_flow_ipool",
303         },
304 };
305
306
307 #define MLX5_FLOW_MIN_ID_POOL_SIZE 512
308 #define MLX5_ID_GENERATION_ARRAY_FACTOR 16
309
310 #define MLX5_FLOW_TABLE_HLIST_ARRAY_SIZE 4096
311
312 /**
313  * Allocate ID pool structure.
314  *
315  * @param[in] max_id
316  *   The maximum id can be allocated from the pool.
317  *
318  * @return
319  *   Pointer to pool object, NULL value otherwise.
320  */
321 struct mlx5_flow_id_pool *
322 mlx5_flow_id_pool_alloc(uint32_t max_id)
323 {
324         struct mlx5_flow_id_pool *pool;
325         void *mem;
326
327         pool = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*pool),
328                            RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
329         if (!pool) {
330                 DRV_LOG(ERR, "can't allocate id pool");
331                 rte_errno  = ENOMEM;
332                 return NULL;
333         }
334         mem = mlx5_malloc(MLX5_MEM_ZERO,
335                           MLX5_FLOW_MIN_ID_POOL_SIZE * sizeof(uint32_t),
336                           RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
337         if (!mem) {
338                 DRV_LOG(ERR, "can't allocate mem for id pool");
339                 rte_errno  = ENOMEM;
340                 goto error;
341         }
342         pool->free_arr = mem;
343         pool->curr = pool->free_arr;
344         pool->last = pool->free_arr + MLX5_FLOW_MIN_ID_POOL_SIZE;
345         pool->base_index = 0;
346         pool->max_id = max_id;
347         return pool;
348 error:
349         mlx5_free(pool);
350         return NULL;
351 }
352
353 /**
354  * Release ID pool structure.
355  *
356  * @param[in] pool
357  *   Pointer to flow id pool object to free.
358  */
359 void
360 mlx5_flow_id_pool_release(struct mlx5_flow_id_pool *pool)
361 {
362         mlx5_free(pool->free_arr);
363         mlx5_free(pool);
364 }
365
366 /**
367  * Generate ID.
368  *
369  * @param[in] pool
370  *   Pointer to flow id pool.
371  * @param[out] id
372  *   The generated ID.
373  *
374  * @return
375  *   0 on success, error value otherwise.
376  */
377 uint32_t
378 mlx5_flow_id_get(struct mlx5_flow_id_pool *pool, uint32_t *id)
379 {
380         if (pool->curr == pool->free_arr) {
381                 if (pool->base_index == pool->max_id) {
382                         rte_errno  = ENOMEM;
383                         DRV_LOG(ERR, "no free id");
384                         return -rte_errno;
385                 }
386                 *id = ++pool->base_index;
387                 return 0;
388         }
389         *id = *(--pool->curr);
390         return 0;
391 }
392
393 /**
394  * Release ID.
395  *
396  * @param[in] pool
397  *   Pointer to flow id pool.
398  * @param[out] id
399  *   The generated ID.
400  *
401  * @return
402  *   0 on success, error value otherwise.
403  */
404 uint32_t
405 mlx5_flow_id_release(struct mlx5_flow_id_pool *pool, uint32_t id)
406 {
407         uint32_t size;
408         uint32_t size2;
409         void *mem;
410
411         if (pool->curr == pool->last) {
412                 size = pool->curr - pool->free_arr;
413                 size2 = size * MLX5_ID_GENERATION_ARRAY_FACTOR;
414                 MLX5_ASSERT(size2 > size);
415                 mem = mlx5_malloc(0, size2 * sizeof(uint32_t), 0,
416                                   SOCKET_ID_ANY);
417                 if (!mem) {
418                         DRV_LOG(ERR, "can't allocate mem for id pool");
419                         rte_errno  = ENOMEM;
420                         return -rte_errno;
421                 }
422                 memcpy(mem, pool->free_arr, size * sizeof(uint32_t));
423                 mlx5_free(pool->free_arr);
424                 pool->free_arr = mem;
425                 pool->curr = pool->free_arr + size;
426                 pool->last = pool->free_arr + size2;
427         }
428         *pool->curr = id;
429         pool->curr++;
430         return 0;
431 }
432
433 /**
434  * Initialize the shared aging list information per port.
435  *
436  * @param[in] sh
437  *   Pointer to mlx5_dev_ctx_shared object.
438  */
439 static void
440 mlx5_flow_aging_init(struct mlx5_dev_ctx_shared *sh)
441 {
442         uint32_t i;
443         struct mlx5_age_info *age_info;
444
445         for (i = 0; i < sh->max_port; i++) {
446                 age_info = &sh->port[i].age_info;
447                 age_info->flags = 0;
448                 TAILQ_INIT(&age_info->aged_counters);
449                 rte_spinlock_init(&age_info->aged_sl);
450                 MLX5_AGE_SET(age_info, MLX5_AGE_TRIGGER);
451         }
452 }
453
454 /**
455  * Initialize the counters management structure.
456  *
457  * @param[in] sh
458  *   Pointer to mlx5_dev_ctx_shared object to free
459  */
460 static void
461 mlx5_flow_counters_mng_init(struct mlx5_dev_ctx_shared *sh)
462 {
463         int i;
464
465         memset(&sh->cmng, 0, sizeof(sh->cmng));
466         TAILQ_INIT(&sh->cmng.flow_counters);
467         for (i = 0; i < MLX5_CCONT_TYPE_MAX; ++i) {
468                 sh->cmng.ccont[i].min_id = MLX5_CNT_BATCH_OFFSET;
469                 sh->cmng.ccont[i].max_id = -1;
470                 sh->cmng.ccont[i].last_pool_idx = POOL_IDX_INVALID;
471                 TAILQ_INIT(&sh->cmng.ccont[i].pool_list);
472                 rte_spinlock_init(&sh->cmng.ccont[i].resize_sl);
473                 TAILQ_INIT(&sh->cmng.ccont[i].counters);
474                 rte_spinlock_init(&sh->cmng.ccont[i].csl);
475         }
476 }
477
478 /**
479  * Destroy all the resources allocated for a counter memory management.
480  *
481  * @param[in] mng
482  *   Pointer to the memory management structure.
483  */
484 static void
485 mlx5_flow_destroy_counter_stat_mem_mng(struct mlx5_counter_stats_mem_mng *mng)
486 {
487         uint8_t *mem = (uint8_t *)(uintptr_t)mng->raws[0].data;
488
489         LIST_REMOVE(mng, next);
490         claim_zero(mlx5_devx_cmd_destroy(mng->dm));
491         claim_zero(mlx5_glue->devx_umem_dereg(mng->umem));
492         mlx5_free(mem);
493 }
494
495 /**
496  * Close and release all the resources of the counters management.
497  *
498  * @param[in] sh
499  *   Pointer to mlx5_dev_ctx_shared object to free.
500  */
501 static void
502 mlx5_flow_counters_mng_close(struct mlx5_dev_ctx_shared *sh)
503 {
504         struct mlx5_counter_stats_mem_mng *mng;
505         int i;
506         int j;
507         int retries = 1024;
508
509         rte_errno = 0;
510         while (--retries) {
511                 rte_eal_alarm_cancel(mlx5_flow_query_alarm, sh);
512                 if (rte_errno != EINPROGRESS)
513                         break;
514                 rte_pause();
515         }
516         for (i = 0; i < MLX5_CCONT_TYPE_MAX; ++i) {
517                 struct mlx5_flow_counter_pool *pool;
518                 uint32_t batch = !!(i > 1);
519
520                 if (!sh->cmng.ccont[i].pools)
521                         continue;
522                 pool = TAILQ_FIRST(&sh->cmng.ccont[i].pool_list);
523                 while (pool) {
524                         if (batch && pool->min_dcs)
525                                 claim_zero(mlx5_devx_cmd_destroy
526                                                                (pool->min_dcs));
527                         for (j = 0; j < MLX5_COUNTERS_PER_POOL; ++j) {
528                                 if (MLX5_POOL_GET_CNT(pool, j)->action)
529                                         claim_zero
530                                          (mlx5_glue->destroy_flow_action
531                                           (MLX5_POOL_GET_CNT
532                                           (pool, j)->action));
533                                 if (!batch && MLX5_GET_POOL_CNT_EXT
534                                     (pool, j)->dcs)
535                                         claim_zero(mlx5_devx_cmd_destroy
536                                                    (MLX5_GET_POOL_CNT_EXT
537                                                     (pool, j)->dcs));
538                         }
539                         TAILQ_REMOVE(&sh->cmng.ccont[i].pool_list, pool, next);
540                         mlx5_free(pool);
541                         pool = TAILQ_FIRST(&sh->cmng.ccont[i].pool_list);
542                 }
543                 mlx5_free(sh->cmng.ccont[i].pools);
544         }
545         mng = LIST_FIRST(&sh->cmng.mem_mngs);
546         while (mng) {
547                 mlx5_flow_destroy_counter_stat_mem_mng(mng);
548                 mng = LIST_FIRST(&sh->cmng.mem_mngs);
549         }
550         memset(&sh->cmng, 0, sizeof(sh->cmng));
551 }
552
553 /**
554  * Initialize the flow resources' indexed mempool.
555  *
556  * @param[in] sh
557  *   Pointer to mlx5_dev_ctx_shared object.
558  * @param[in] sh
559  *   Pointer to user dev config.
560  */
561 static void
562 mlx5_flow_ipool_create(struct mlx5_dev_ctx_shared *sh,
563                        const struct mlx5_dev_config *config)
564 {
565         uint8_t i;
566         struct mlx5_indexed_pool_config cfg;
567
568         for (i = 0; i < MLX5_IPOOL_MAX; ++i) {
569                 cfg = mlx5_ipool_cfg[i];
570                 switch (i) {
571                 default:
572                         break;
573                 /*
574                  * Set MLX5_IPOOL_MLX5_FLOW ipool size
575                  * according to PCI function flow configuration.
576                  */
577                 case MLX5_IPOOL_MLX5_FLOW:
578                         cfg.size = config->dv_flow_en ?
579                                 sizeof(struct mlx5_flow_handle) :
580                                 MLX5_FLOW_HANDLE_VERBS_SIZE;
581                         break;
582                 }
583                 if (config->reclaim_mode)
584                         cfg.release_mem_en = 1;
585                 sh->ipool[i] = mlx5_ipool_create(&cfg);
586         }
587 }
588
589 /**
590  * Release the flow resources' indexed mempool.
591  *
592  * @param[in] sh
593  *   Pointer to mlx5_dev_ctx_shared object.
594  */
595 static void
596 mlx5_flow_ipool_destroy(struct mlx5_dev_ctx_shared *sh)
597 {
598         uint8_t i;
599
600         for (i = 0; i < MLX5_IPOOL_MAX; ++i)
601                 mlx5_ipool_destroy(sh->ipool[i]);
602 }
603
604 /*
605  * Check if dynamic flex parser for eCPRI already exists.
606  *
607  * @param dev
608  *   Pointer to Ethernet device structure.
609  *
610  * @return
611  *   true on exists, false on not.
612  */
613 bool
614 mlx5_flex_parser_ecpri_exist(struct rte_eth_dev *dev)
615 {
616         struct mlx5_priv *priv = dev->data->dev_private;
617         struct mlx5_flex_parser_profiles *prf =
618                                 &priv->sh->fp[MLX5_FLEX_PARSER_ECPRI_0];
619
620         return !!prf->obj;
621 }
622
623 /*
624  * Allocation of a flex parser for eCPRI. Once created, this parser related
625  * resources will be held until the device is closed.
626  *
627  * @param dev
628  *   Pointer to Ethernet device structure.
629  *
630  * @return
631  *   0 on success, a negative errno value otherwise and rte_errno is set.
632  */
633 int
634 mlx5_flex_parser_ecpri_alloc(struct rte_eth_dev *dev)
635 {
636         struct mlx5_priv *priv = dev->data->dev_private;
637         struct mlx5_flex_parser_profiles *prf =
638                                 &priv->sh->fp[MLX5_FLEX_PARSER_ECPRI_0];
639         struct mlx5_devx_graph_node_attr node = {
640                 .modify_field_select = 0,
641         };
642         uint32_t ids[8];
643         int ret;
644
645         if (!priv->config.hca_attr.parse_graph_flex_node) {
646                 DRV_LOG(ERR, "Dynamic flex parser is not supported "
647                         "for device %s.", priv->dev_data->name);
648                 return -ENOTSUP;
649         }
650         node.header_length_mode = MLX5_GRAPH_NODE_LEN_FIXED;
651         /* 8 bytes now: 4B common header + 4B message body header. */
652         node.header_length_base_value = 0x8;
653         /* After MAC layer: Ether / VLAN. */
654         node.in[0].arc_parse_graph_node = MLX5_GRAPH_ARC_NODE_MAC;
655         /* Type of compared condition should be 0xAEFE in the L2 layer. */
656         node.in[0].compare_condition_value = RTE_ETHER_TYPE_ECPRI;
657         /* Sample #0: type in common header. */
658         node.sample[0].flow_match_sample_en = 1;
659         /* Fixed offset. */
660         node.sample[0].flow_match_sample_offset_mode = 0x0;
661         /* Only the 2nd byte will be used. */
662         node.sample[0].flow_match_sample_field_base_offset = 0x0;
663         /* Sample #1: message payload. */
664         node.sample[1].flow_match_sample_en = 1;
665         /* Fixed offset. */
666         node.sample[1].flow_match_sample_offset_mode = 0x0;
667         /*
668          * Only the first two bytes will be used right now, and its offset will
669          * start after the common header that with the length of a DW(u32).
670          */
671         node.sample[1].flow_match_sample_field_base_offset = sizeof(uint32_t);
672         prf->obj = mlx5_devx_cmd_create_flex_parser(priv->sh->ctx, &node);
673         if (!prf->obj) {
674                 DRV_LOG(ERR, "Failed to create flex parser node object.");
675                 return (rte_errno == 0) ? -ENODEV : -rte_errno;
676         }
677         prf->num = 2;
678         ret = mlx5_devx_cmd_query_parse_samples(prf->obj, ids, prf->num);
679         if (ret) {
680                 DRV_LOG(ERR, "Failed to query sample IDs.");
681                 return (rte_errno == 0) ? -ENODEV : -rte_errno;
682         }
683         prf->offset[0] = 0x0;
684         prf->offset[1] = sizeof(uint32_t);
685         prf->ids[0] = ids[0];
686         prf->ids[1] = ids[1];
687         return 0;
688 }
689
690 /*
691  * Destroy the flex parser node, including the parser itself, input / output
692  * arcs and DW samples. Resources could be reused then.
693  *
694  * @param dev
695  *   Pointer to Ethernet device structure.
696  */
697 static void
698 mlx5_flex_parser_ecpri_release(struct rte_eth_dev *dev)
699 {
700         struct mlx5_priv *priv = dev->data->dev_private;
701         struct mlx5_flex_parser_profiles *prf =
702                                 &priv->sh->fp[MLX5_FLEX_PARSER_ECPRI_0];
703
704         if (prf->obj)
705                 mlx5_devx_cmd_destroy(prf->obj);
706         prf->obj = NULL;
707 }
708
709 /**
710  * Allocate shared device context. If there is multiport device the
711  * master and representors will share this context, if there is single
712  * port dedicated device, the context will be used by only given
713  * port due to unification.
714  *
715  * Routine first searches the context for the specified device name,
716  * if found the shared context assumed and reference counter is incremented.
717  * If no context found the new one is created and initialized with specified
718  * device context and parameters.
719  *
720  * @param[in] spawn
721  *   Pointer to the device attributes (name, port, etc).
722  * @param[in] config
723  *   Pointer to device configuration structure.
724  *
725  * @return
726  *   Pointer to mlx5_dev_ctx_shared object on success,
727  *   otherwise NULL and rte_errno is set.
728  */
729 struct mlx5_dev_ctx_shared *
730 mlx5_alloc_shared_dev_ctx(const struct mlx5_dev_spawn_data *spawn,
731                            const struct mlx5_dev_config *config)
732 {
733         struct mlx5_dev_ctx_shared *sh;
734         int err = 0;
735         uint32_t i;
736         struct mlx5_devx_tis_attr tis_attr = { 0 };
737
738         MLX5_ASSERT(spawn);
739         /* Secondary process should not create the shared context. */
740         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
741         pthread_mutex_lock(&mlx5_dev_ctx_list_mutex);
742         /* Search for IB context by device name. */
743         LIST_FOREACH(sh, &mlx5_dev_ctx_list, next) {
744                 if (!strcmp(sh->ibdev_name,
745                         mlx5_os_get_dev_device_name(spawn->phys_dev))) {
746                         sh->refcnt++;
747                         goto exit;
748                 }
749         }
750         /* No device found, we have to create new shared context. */
751         MLX5_ASSERT(spawn->max_port);
752         sh = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE,
753                          sizeof(struct mlx5_dev_ctx_shared) +
754                          spawn->max_port *
755                          sizeof(struct mlx5_dev_shared_port),
756                          RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
757         if (!sh) {
758                 DRV_LOG(ERR, "shared context allocation failure");
759                 rte_errno  = ENOMEM;
760                 goto exit;
761         }
762         err = mlx5_os_open_device(spawn, config, sh);
763         if (!sh->ctx)
764                 goto error;
765         err = mlx5_os_get_dev_attr(sh->ctx, &sh->device_attr);
766         if (err) {
767                 DRV_LOG(DEBUG, "mlx5_os_get_dev_attr() failed");
768                 goto error;
769         }
770         sh->refcnt = 1;
771         sh->max_port = spawn->max_port;
772         strncpy(sh->ibdev_name, mlx5_os_get_ctx_device_name(sh->ctx),
773                 sizeof(sh->ibdev_name) - 1);
774         strncpy(sh->ibdev_path, mlx5_os_get_ctx_device_path(sh->ctx),
775                 sizeof(sh->ibdev_path) - 1);
776         /*
777          * Setting port_id to max unallowed value means
778          * there is no interrupt subhandler installed for
779          * the given port index i.
780          */
781         for (i = 0; i < sh->max_port; i++) {
782                 sh->port[i].ih_port_id = RTE_MAX_ETHPORTS;
783                 sh->port[i].devx_ih_port_id = RTE_MAX_ETHPORTS;
784         }
785         sh->pd = mlx5_glue->alloc_pd(sh->ctx);
786         if (sh->pd == NULL) {
787                 DRV_LOG(ERR, "PD allocation failure");
788                 err = ENOMEM;
789                 goto error;
790         }
791         if (sh->devx) {
792                 err = mlx5_os_get_pdn(sh->pd, &sh->pdn);
793                 if (err) {
794                         DRV_LOG(ERR, "Fail to extract pdn from PD");
795                         goto error;
796                 }
797                 sh->td = mlx5_devx_cmd_create_td(sh->ctx);
798                 if (!sh->td) {
799                         DRV_LOG(ERR, "TD allocation failure");
800                         err = ENOMEM;
801                         goto error;
802                 }
803                 tis_attr.transport_domain = sh->td->id;
804                 sh->tis = mlx5_devx_cmd_create_tis(sh->ctx, &tis_attr);
805                 if (!sh->tis) {
806                         DRV_LOG(ERR, "TIS allocation failure");
807                         err = ENOMEM;
808                         goto error;
809                 }
810                 sh->tx_uar = mlx5_glue->devx_alloc_uar(sh->ctx, 0);
811                 if (!sh->tx_uar) {
812                         DRV_LOG(ERR, "Failed to allocate DevX UAR.");
813                         err = ENOMEM;
814                         goto error;
815                 }
816         }
817         sh->flow_id_pool = mlx5_flow_id_pool_alloc
818                                         ((1 << HAIRPIN_FLOW_ID_BITS) - 1);
819         if (!sh->flow_id_pool) {
820                 DRV_LOG(ERR, "can't create flow id pool");
821                 err = ENOMEM;
822                 goto error;
823         }
824 #ifndef RTE_ARCH_64
825         /* Initialize UAR access locks for 32bit implementations. */
826         rte_spinlock_init(&sh->uar_lock_cq);
827         for (i = 0; i < MLX5_UAR_PAGE_NUM_MAX; i++)
828                 rte_spinlock_init(&sh->uar_lock[i]);
829 #endif
830         /*
831          * Once the device is added to the list of memory event
832          * callback, its global MR cache table cannot be expanded
833          * on the fly because of deadlock. If it overflows, lookup
834          * should be done by searching MR list linearly, which is slow.
835          *
836          * At this point the device is not added to the memory
837          * event list yet, context is just being created.
838          */
839         err = mlx5_mr_btree_init(&sh->share_cache.cache,
840                                  MLX5_MR_BTREE_CACHE_N * 2,
841                                  spawn->pci_dev->device.numa_node);
842         if (err) {
843                 err = rte_errno;
844                 goto error;
845         }
846         mlx5_os_set_reg_mr_cb(&sh->share_cache.reg_mr_cb,
847                               &sh->share_cache.dereg_mr_cb);
848         mlx5_os_dev_shared_handler_install(sh);
849         sh->cnt_id_tbl = mlx5_l3t_create(MLX5_L3T_TYPE_DWORD);
850         if (!sh->cnt_id_tbl) {
851                 err = rte_errno;
852                 goto error;
853         }
854         mlx5_flow_aging_init(sh);
855         mlx5_flow_counters_mng_init(sh);
856         mlx5_flow_ipool_create(sh, config);
857         /* Add device to memory callback list. */
858         rte_rwlock_write_lock(&mlx5_shared_data->mem_event_rwlock);
859         LIST_INSERT_HEAD(&mlx5_shared_data->mem_event_cb_list,
860                          sh, mem_event_cb);
861         rte_rwlock_write_unlock(&mlx5_shared_data->mem_event_rwlock);
862         /* Add context to the global device list. */
863         LIST_INSERT_HEAD(&mlx5_dev_ctx_list, sh, next);
864 exit:
865         pthread_mutex_unlock(&mlx5_dev_ctx_list_mutex);
866         return sh;
867 error:
868         pthread_mutex_destroy(&sh->txpp.mutex);
869         pthread_mutex_unlock(&mlx5_dev_ctx_list_mutex);
870         MLX5_ASSERT(sh);
871         if (sh->cnt_id_tbl) {
872                 mlx5_l3t_destroy(sh->cnt_id_tbl);
873                 sh->cnt_id_tbl = NULL;
874         }
875         if (sh->tx_uar) {
876                 mlx5_glue->devx_free_uar(sh->tx_uar);
877                 sh->tx_uar = NULL;
878         }
879         if (sh->tis)
880                 claim_zero(mlx5_devx_cmd_destroy(sh->tis));
881         if (sh->td)
882                 claim_zero(mlx5_devx_cmd_destroy(sh->td));
883         if (sh->pd)
884                 claim_zero(mlx5_glue->dealloc_pd(sh->pd));
885         if (sh->ctx)
886                 claim_zero(mlx5_glue->close_device(sh->ctx));
887         if (sh->flow_id_pool)
888                 mlx5_flow_id_pool_release(sh->flow_id_pool);
889         mlx5_free(sh);
890         MLX5_ASSERT(err > 0);
891         rte_errno = err;
892         return NULL;
893 }
894
895 /**
896  * Free shared IB device context. Decrement counter and if zero free
897  * all allocated resources and close handles.
898  *
899  * @param[in] sh
900  *   Pointer to mlx5_dev_ctx_shared object to free
901  */
902 void
903 mlx5_free_shared_dev_ctx(struct mlx5_dev_ctx_shared *sh)
904 {
905         pthread_mutex_lock(&mlx5_dev_ctx_list_mutex);
906 #ifdef RTE_LIBRTE_MLX5_DEBUG
907         /* Check the object presence in the list. */
908         struct mlx5_dev_ctx_shared *lctx;
909
910         LIST_FOREACH(lctx, &mlx5_dev_ctx_list, next)
911                 if (lctx == sh)
912                         break;
913         MLX5_ASSERT(lctx);
914         if (lctx != sh) {
915                 DRV_LOG(ERR, "Freeing non-existing shared IB context");
916                 goto exit;
917         }
918 #endif
919         MLX5_ASSERT(sh);
920         MLX5_ASSERT(sh->refcnt);
921         /* Secondary process should not free the shared context. */
922         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
923         if (--sh->refcnt)
924                 goto exit;
925         /* Remove from memory callback device list. */
926         rte_rwlock_write_lock(&mlx5_shared_data->mem_event_rwlock);
927         LIST_REMOVE(sh, mem_event_cb);
928         rte_rwlock_write_unlock(&mlx5_shared_data->mem_event_rwlock);
929         /* Release created Memory Regions. */
930         mlx5_mr_release_cache(&sh->share_cache);
931         /* Remove context from the global device list. */
932         LIST_REMOVE(sh, next);
933         /*
934          *  Ensure there is no async event handler installed.
935          *  Only primary process handles async device events.
936          **/
937         mlx5_flow_counters_mng_close(sh);
938         mlx5_flow_ipool_destroy(sh);
939         mlx5_os_dev_shared_handler_uninstall(sh);
940         if (sh->cnt_id_tbl) {
941                 mlx5_l3t_destroy(sh->cnt_id_tbl);
942                 sh->cnt_id_tbl = NULL;
943         }
944         if (sh->tx_uar) {
945                 mlx5_glue->devx_free_uar(sh->tx_uar);
946                 sh->tx_uar = NULL;
947         }
948         if (sh->pd)
949                 claim_zero(mlx5_glue->dealloc_pd(sh->pd));
950         if (sh->tis)
951                 claim_zero(mlx5_devx_cmd_destroy(sh->tis));
952         if (sh->td)
953                 claim_zero(mlx5_devx_cmd_destroy(sh->td));
954         if (sh->ctx)
955                 claim_zero(mlx5_glue->close_device(sh->ctx));
956         if (sh->flow_id_pool)
957                 mlx5_flow_id_pool_release(sh->flow_id_pool);
958         pthread_mutex_destroy(&sh->txpp.mutex);
959         mlx5_free(sh);
960 exit:
961         pthread_mutex_unlock(&mlx5_dev_ctx_list_mutex);
962 }
963
964 /**
965  * Destroy table hash list and all the root entries per domain.
966  *
967  * @param[in] priv
968  *   Pointer to the private device data structure.
969  */
970 void
971 mlx5_free_table_hash_list(struct mlx5_priv *priv)
972 {
973         struct mlx5_dev_ctx_shared *sh = priv->sh;
974         struct mlx5_flow_tbl_data_entry *tbl_data;
975         union mlx5_flow_tbl_key table_key = {
976                 {
977                         .table_id = 0,
978                         .reserved = 0,
979                         .domain = 0,
980                         .direction = 0,
981                 }
982         };
983         struct mlx5_hlist_entry *pos;
984
985         if (!sh->flow_tbls)
986                 return;
987         pos = mlx5_hlist_lookup(sh->flow_tbls, table_key.v64);
988         if (pos) {
989                 tbl_data = container_of(pos, struct mlx5_flow_tbl_data_entry,
990                                         entry);
991                 MLX5_ASSERT(tbl_data);
992                 mlx5_hlist_remove(sh->flow_tbls, pos);
993                 mlx5_free(tbl_data);
994         }
995         table_key.direction = 1;
996         pos = mlx5_hlist_lookup(sh->flow_tbls, table_key.v64);
997         if (pos) {
998                 tbl_data = container_of(pos, struct mlx5_flow_tbl_data_entry,
999                                         entry);
1000                 MLX5_ASSERT(tbl_data);
1001                 mlx5_hlist_remove(sh->flow_tbls, pos);
1002                 mlx5_free(tbl_data);
1003         }
1004         table_key.direction = 0;
1005         table_key.domain = 1;
1006         pos = mlx5_hlist_lookup(sh->flow_tbls, table_key.v64);
1007         if (pos) {
1008                 tbl_data = container_of(pos, struct mlx5_flow_tbl_data_entry,
1009                                         entry);
1010                 MLX5_ASSERT(tbl_data);
1011                 mlx5_hlist_remove(sh->flow_tbls, pos);
1012                 mlx5_free(tbl_data);
1013         }
1014         mlx5_hlist_destroy(sh->flow_tbls, NULL, NULL);
1015 }
1016
1017 /**
1018  * Initialize flow table hash list and create the root tables entry
1019  * for each domain.
1020  *
1021  * @param[in] priv
1022  *   Pointer to the private device data structure.
1023  *
1024  * @return
1025  *   Zero on success, positive error code otherwise.
1026  */
1027 int
1028 mlx5_alloc_table_hash_list(struct mlx5_priv *priv)
1029 {
1030         struct mlx5_dev_ctx_shared *sh = priv->sh;
1031         char s[MLX5_HLIST_NAMESIZE];
1032         int err = 0;
1033
1034         MLX5_ASSERT(sh);
1035         snprintf(s, sizeof(s), "%s_flow_table", priv->sh->ibdev_name);
1036         sh->flow_tbls = mlx5_hlist_create(s, MLX5_FLOW_TABLE_HLIST_ARRAY_SIZE);
1037         if (!sh->flow_tbls) {
1038                 DRV_LOG(ERR, "flow tables with hash creation failed.");
1039                 err = ENOMEM;
1040                 return err;
1041         }
1042 #ifndef HAVE_MLX5DV_DR
1043         /*
1044          * In case we have not DR support, the zero tables should be created
1045          * because DV expect to see them even if they cannot be created by
1046          * RDMA-CORE.
1047          */
1048         union mlx5_flow_tbl_key table_key = {
1049                 {
1050                         .table_id = 0,
1051                         .reserved = 0,
1052                         .domain = 0,
1053                         .direction = 0,
1054                 }
1055         };
1056         struct mlx5_flow_tbl_data_entry *tbl_data = mlx5_malloc(MLX5_MEM_ZERO,
1057                                                           sizeof(*tbl_data), 0,
1058                                                           SOCKET_ID_ANY);
1059
1060         if (!tbl_data) {
1061                 err = ENOMEM;
1062                 goto error;
1063         }
1064         tbl_data->entry.key = table_key.v64;
1065         err = mlx5_hlist_insert(sh->flow_tbls, &tbl_data->entry);
1066         if (err)
1067                 goto error;
1068         rte_atomic32_init(&tbl_data->tbl.refcnt);
1069         rte_atomic32_inc(&tbl_data->tbl.refcnt);
1070         table_key.direction = 1;
1071         tbl_data = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*tbl_data), 0,
1072                                SOCKET_ID_ANY);
1073         if (!tbl_data) {
1074                 err = ENOMEM;
1075                 goto error;
1076         }
1077         tbl_data->entry.key = table_key.v64;
1078         err = mlx5_hlist_insert(sh->flow_tbls, &tbl_data->entry);
1079         if (err)
1080                 goto error;
1081         rte_atomic32_init(&tbl_data->tbl.refcnt);
1082         rte_atomic32_inc(&tbl_data->tbl.refcnt);
1083         table_key.direction = 0;
1084         table_key.domain = 1;
1085         tbl_data = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*tbl_data), 0,
1086                                SOCKET_ID_ANY);
1087         if (!tbl_data) {
1088                 err = ENOMEM;
1089                 goto error;
1090         }
1091         tbl_data->entry.key = table_key.v64;
1092         err = mlx5_hlist_insert(sh->flow_tbls, &tbl_data->entry);
1093         if (err)
1094                 goto error;
1095         rte_atomic32_init(&tbl_data->tbl.refcnt);
1096         rte_atomic32_inc(&tbl_data->tbl.refcnt);
1097         return err;
1098 error:
1099         mlx5_free_table_hash_list(priv);
1100 #endif /* HAVE_MLX5DV_DR */
1101         return err;
1102 }
1103
1104 /**
1105  * Retrieve integer value from environment variable.
1106  *
1107  * @param[in] name
1108  *   Environment variable name.
1109  *
1110  * @return
1111  *   Integer value, 0 if the variable is not set.
1112  */
1113 int
1114 mlx5_getenv_int(const char *name)
1115 {
1116         const char *val = getenv(name);
1117
1118         if (val == NULL)
1119                 return 0;
1120         return atoi(val);
1121 }
1122
1123 /**
1124  * DPDK callback to add udp tunnel port
1125  *
1126  * @param[in] dev
1127  *   A pointer to eth_dev
1128  * @param[in] udp_tunnel
1129  *   A pointer to udp tunnel
1130  *
1131  * @return
1132  *   0 on valid udp ports and tunnels, -ENOTSUP otherwise.
1133  */
1134 int
1135 mlx5_udp_tunnel_port_add(struct rte_eth_dev *dev __rte_unused,
1136                          struct rte_eth_udp_tunnel *udp_tunnel)
1137 {
1138         MLX5_ASSERT(udp_tunnel != NULL);
1139         if (udp_tunnel->prot_type == RTE_TUNNEL_TYPE_VXLAN &&
1140             udp_tunnel->udp_port == 4789)
1141                 return 0;
1142         if (udp_tunnel->prot_type == RTE_TUNNEL_TYPE_VXLAN_GPE &&
1143             udp_tunnel->udp_port == 4790)
1144                 return 0;
1145         return -ENOTSUP;
1146 }
1147
1148 /**
1149  * Initialize process private data structure.
1150  *
1151  * @param dev
1152  *   Pointer to Ethernet device structure.
1153  *
1154  * @return
1155  *   0 on success, a negative errno value otherwise and rte_errno is set.
1156  */
1157 int
1158 mlx5_proc_priv_init(struct rte_eth_dev *dev)
1159 {
1160         struct mlx5_priv *priv = dev->data->dev_private;
1161         struct mlx5_proc_priv *ppriv;
1162         size_t ppriv_size;
1163
1164         /*
1165          * UAR register table follows the process private structure. BlueFlame
1166          * registers for Tx queues are stored in the table.
1167          */
1168         ppriv_size =
1169                 sizeof(struct mlx5_proc_priv) + priv->txqs_n * sizeof(void *);
1170         ppriv = mlx5_malloc(MLX5_MEM_RTE, ppriv_size, RTE_CACHE_LINE_SIZE,
1171                             dev->device->numa_node);
1172         if (!ppriv) {
1173                 rte_errno = ENOMEM;
1174                 return -rte_errno;
1175         }
1176         ppriv->uar_table_sz = ppriv_size;
1177         dev->process_private = ppriv;
1178         return 0;
1179 }
1180
1181 /**
1182  * Un-initialize process private data structure.
1183  *
1184  * @param dev
1185  *   Pointer to Ethernet device structure.
1186  */
1187 static void
1188 mlx5_proc_priv_uninit(struct rte_eth_dev *dev)
1189 {
1190         if (!dev->process_private)
1191                 return;
1192         mlx5_free(dev->process_private);
1193         dev->process_private = NULL;
1194 }
1195
1196 /**
1197  * DPDK callback to close the device.
1198  *
1199  * Destroy all queues and objects, free memory.
1200  *
1201  * @param dev
1202  *   Pointer to Ethernet device structure.
1203  */
1204 void
1205 mlx5_dev_close(struct rte_eth_dev *dev)
1206 {
1207         struct mlx5_priv *priv = dev->data->dev_private;
1208         unsigned int i;
1209         int ret;
1210
1211         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1212                 /* Check if process_private released. */
1213                 if (!dev->process_private)
1214                         return;
1215                 mlx5_tx_uar_uninit_secondary(dev);
1216                 mlx5_proc_priv_uninit(dev);
1217                 rte_eth_dev_release_port(dev);
1218                 return;
1219         }
1220         if (!priv->sh)
1221                 return;
1222         DRV_LOG(DEBUG, "port %u closing device \"%s\"",
1223                 dev->data->port_id,
1224                 ((priv->sh->ctx != NULL) ?
1225                 mlx5_os_get_ctx_device_name(priv->sh->ctx) : ""));
1226         /*
1227          * If default mreg copy action is removed at the stop stage,
1228          * the search will return none and nothing will be done anymore.
1229          */
1230         mlx5_flow_stop_default(dev);
1231         mlx5_traffic_disable(dev);
1232         /*
1233          * If all the flows are already flushed in the device stop stage,
1234          * then this will return directly without any action.
1235          */
1236         mlx5_flow_list_flush(dev, &priv->flows, true);
1237         mlx5_flow_meter_flush(dev, NULL);
1238         /* Free the intermediate buffers for flow creation. */
1239         mlx5_flow_free_intermediate(dev);
1240         /* Prevent crashes when queues are still in use. */
1241         dev->rx_pkt_burst = removed_rx_burst;
1242         dev->tx_pkt_burst = removed_tx_burst;
1243         rte_wmb();
1244         /* Disable datapath on secondary process. */
1245         mlx5_mp_os_req_stop_rxtx(dev);
1246         /* Free the eCPRI flex parser resource. */
1247         mlx5_flex_parser_ecpri_release(dev);
1248         if (priv->rxqs != NULL) {
1249                 /* XXX race condition if mlx5_rx_burst() is still running. */
1250                 usleep(1000);
1251                 for (i = 0; (i != priv->rxqs_n); ++i)
1252                         mlx5_rxq_release(dev, i);
1253                 priv->rxqs_n = 0;
1254                 priv->rxqs = NULL;
1255         }
1256         if (priv->txqs != NULL) {
1257                 /* XXX race condition if mlx5_tx_burst() is still running. */
1258                 usleep(1000);
1259                 for (i = 0; (i != priv->txqs_n); ++i)
1260                         mlx5_txq_release(dev, i);
1261                 priv->txqs_n = 0;
1262                 priv->txqs = NULL;
1263         }
1264         mlx5_proc_priv_uninit(dev);
1265         if (priv->mreg_cp_tbl)
1266                 mlx5_hlist_destroy(priv->mreg_cp_tbl, NULL, NULL);
1267         mlx5_mprq_free_mp(dev);
1268         mlx5_os_free_shared_dr(priv);
1269         if (priv->rss_conf.rss_key != NULL)
1270                 mlx5_free(priv->rss_conf.rss_key);
1271         if (priv->reta_idx != NULL)
1272                 mlx5_free(priv->reta_idx);
1273         if (priv->config.vf)
1274                 mlx5_nl_mac_addr_flush(priv->nl_socket_route, mlx5_ifindex(dev),
1275                                        dev->data->mac_addrs,
1276                                        MLX5_MAX_MAC_ADDRESSES, priv->mac_own);
1277         if (priv->nl_socket_route >= 0)
1278                 close(priv->nl_socket_route);
1279         if (priv->nl_socket_rdma >= 0)
1280                 close(priv->nl_socket_rdma);
1281         if (priv->vmwa_context)
1282                 mlx5_vlan_vmwa_exit(priv->vmwa_context);
1283         ret = mlx5_hrxq_verify(dev);
1284         if (ret)
1285                 DRV_LOG(WARNING, "port %u some hash Rx queue still remain",
1286                         dev->data->port_id);
1287         ret = mlx5_ind_table_obj_verify(dev);
1288         if (ret)
1289                 DRV_LOG(WARNING, "port %u some indirection table still remain",
1290                         dev->data->port_id);
1291         ret = mlx5_rxq_obj_verify(dev);
1292         if (ret)
1293                 DRV_LOG(WARNING, "port %u some Rx queue objects still remain",
1294                         dev->data->port_id);
1295         ret = mlx5_rxq_verify(dev);
1296         if (ret)
1297                 DRV_LOG(WARNING, "port %u some Rx queues still remain",
1298                         dev->data->port_id);
1299         ret = mlx5_txq_obj_verify(dev);
1300         if (ret)
1301                 DRV_LOG(WARNING, "port %u some Verbs Tx queue still remain",
1302                         dev->data->port_id);
1303         ret = mlx5_txq_verify(dev);
1304         if (ret)
1305                 DRV_LOG(WARNING, "port %u some Tx queues still remain",
1306                         dev->data->port_id);
1307         ret = mlx5_flow_verify(dev);
1308         if (ret)
1309                 DRV_LOG(WARNING, "port %u some flows still remain",
1310                         dev->data->port_id);
1311         /*
1312          * Free the shared context in last turn, because the cleanup
1313          * routines above may use some shared fields, like
1314          * mlx5_nl_mac_addr_flush() uses ibdev_path for retrieveing
1315          * ifindex if Netlink fails.
1316          */
1317         mlx5_free_shared_dev_ctx(priv->sh);
1318         if (priv->domain_id != RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
1319                 unsigned int c = 0;
1320                 uint16_t port_id;
1321
1322                 MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
1323                         struct mlx5_priv *opriv =
1324                                 rte_eth_devices[port_id].data->dev_private;
1325
1326                         if (!opriv ||
1327                             opriv->domain_id != priv->domain_id ||
1328                             &rte_eth_devices[port_id] == dev)
1329                                 continue;
1330                         ++c;
1331                         break;
1332                 }
1333                 if (!c)
1334                         claim_zero(rte_eth_switch_domain_free(priv->domain_id));
1335         }
1336         memset(priv, 0, sizeof(*priv));
1337         priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
1338         /*
1339          * Reset mac_addrs to NULL such that it is not freed as part of
1340          * rte_eth_dev_release_port(). mac_addrs is part of dev_private so
1341          * it is freed when dev_private is freed.
1342          */
1343         dev->data->mac_addrs = NULL;
1344 }
1345
1346 /**
1347  * Verify and store value for device argument.
1348  *
1349  * @param[in] key
1350  *   Key argument to verify.
1351  * @param[in] val
1352  *   Value associated with key.
1353  * @param opaque
1354  *   User data.
1355  *
1356  * @return
1357  *   0 on success, a negative errno value otherwise and rte_errno is set.
1358  */
1359 static int
1360 mlx5_args_check(const char *key, const char *val, void *opaque)
1361 {
1362         struct mlx5_dev_config *config = opaque;
1363         unsigned long mod;
1364         signed long tmp;
1365
1366         /* No-op, port representors are processed in mlx5_dev_spawn(). */
1367         if (!strcmp(MLX5_REPRESENTOR, key))
1368                 return 0;
1369         errno = 0;
1370         tmp = strtol(val, NULL, 0);
1371         if (errno) {
1372                 rte_errno = errno;
1373                 DRV_LOG(WARNING, "%s: \"%s\" is not a valid integer", key, val);
1374                 return -rte_errno;
1375         }
1376         if (tmp < 0 && strcmp(MLX5_TX_PP, key) && strcmp(MLX5_TX_SKEW, key)) {
1377                 /* Negative values are acceptable for some keys only. */
1378                 rte_errno = EINVAL;
1379                 DRV_LOG(WARNING, "%s: invalid negative value \"%s\"", key, val);
1380                 return -rte_errno;
1381         }
1382         mod = tmp >= 0 ? tmp : -tmp;
1383         if (strcmp(MLX5_RXQ_CQE_COMP_EN, key) == 0) {
1384                 config->cqe_comp = !!tmp;
1385         } else if (strcmp(MLX5_RXQ_CQE_PAD_EN, key) == 0) {
1386                 config->cqe_pad = !!tmp;
1387         } else if (strcmp(MLX5_RXQ_PKT_PAD_EN, key) == 0) {
1388                 config->hw_padding = !!tmp;
1389         } else if (strcmp(MLX5_RX_MPRQ_EN, key) == 0) {
1390                 config->mprq.enabled = !!tmp;
1391         } else if (strcmp(MLX5_RX_MPRQ_LOG_STRIDE_NUM, key) == 0) {
1392                 config->mprq.stride_num_n = tmp;
1393         } else if (strcmp(MLX5_RX_MPRQ_LOG_STRIDE_SIZE, key) == 0) {
1394                 config->mprq.stride_size_n = tmp;
1395         } else if (strcmp(MLX5_RX_MPRQ_MAX_MEMCPY_LEN, key) == 0) {
1396                 config->mprq.max_memcpy_len = tmp;
1397         } else if (strcmp(MLX5_RXQS_MIN_MPRQ, key) == 0) {
1398                 config->mprq.min_rxqs_num = tmp;
1399         } else if (strcmp(MLX5_TXQ_INLINE, key) == 0) {
1400                 DRV_LOG(WARNING, "%s: deprecated parameter,"
1401                                  " converted to txq_inline_max", key);
1402                 config->txq_inline_max = tmp;
1403         } else if (strcmp(MLX5_TXQ_INLINE_MAX, key) == 0) {
1404                 config->txq_inline_max = tmp;
1405         } else if (strcmp(MLX5_TXQ_INLINE_MIN, key) == 0) {
1406                 config->txq_inline_min = tmp;
1407         } else if (strcmp(MLX5_TXQ_INLINE_MPW, key) == 0) {
1408                 config->txq_inline_mpw = tmp;
1409         } else if (strcmp(MLX5_TXQS_MIN_INLINE, key) == 0) {
1410                 config->txqs_inline = tmp;
1411         } else if (strcmp(MLX5_TXQS_MAX_VEC, key) == 0) {
1412                 DRV_LOG(WARNING, "%s: deprecated parameter, ignored", key);
1413         } else if (strcmp(MLX5_TXQ_MPW_EN, key) == 0) {
1414                 config->mps = !!tmp;
1415         } else if (strcmp(MLX5_TX_DB_NC, key) == 0) {
1416                 if (tmp != MLX5_TXDB_CACHED &&
1417                     tmp != MLX5_TXDB_NCACHED &&
1418                     tmp != MLX5_TXDB_HEURISTIC) {
1419                         DRV_LOG(ERR, "invalid Tx doorbell "
1420                                      "mapping parameter");
1421                         rte_errno = EINVAL;
1422                         return -rte_errno;
1423                 }
1424                 config->dbnc = tmp;
1425         } else if (strcmp(MLX5_TXQ_MPW_HDR_DSEG_EN, key) == 0) {
1426                 DRV_LOG(WARNING, "%s: deprecated parameter, ignored", key);
1427         } else if (strcmp(MLX5_TXQ_MAX_INLINE_LEN, key) == 0) {
1428                 DRV_LOG(WARNING, "%s: deprecated parameter,"
1429                                  " converted to txq_inline_mpw", key);
1430                 config->txq_inline_mpw = tmp;
1431         } else if (strcmp(MLX5_TX_VEC_EN, key) == 0) {
1432                 DRV_LOG(WARNING, "%s: deprecated parameter, ignored", key);
1433         } else if (strcmp(MLX5_TX_PP, key) == 0) {
1434                 if (!mod) {
1435                         DRV_LOG(ERR, "Zero Tx packet pacing parameter");
1436                         rte_errno = EINVAL;
1437                         return -rte_errno;
1438                 }
1439                 config->tx_pp = tmp;
1440         } else if (strcmp(MLX5_TX_SKEW, key) == 0) {
1441                 config->tx_skew = tmp;
1442         } else if (strcmp(MLX5_RX_VEC_EN, key) == 0) {
1443                 config->rx_vec_en = !!tmp;
1444         } else if (strcmp(MLX5_L3_VXLAN_EN, key) == 0) {
1445                 config->l3_vxlan_en = !!tmp;
1446         } else if (strcmp(MLX5_VF_NL_EN, key) == 0) {
1447                 config->vf_nl_en = !!tmp;
1448         } else if (strcmp(MLX5_DV_ESW_EN, key) == 0) {
1449                 config->dv_esw_en = !!tmp;
1450         } else if (strcmp(MLX5_DV_FLOW_EN, key) == 0) {
1451                 config->dv_flow_en = !!tmp;
1452         } else if (strcmp(MLX5_DV_XMETA_EN, key) == 0) {
1453                 if (tmp != MLX5_XMETA_MODE_LEGACY &&
1454                     tmp != MLX5_XMETA_MODE_META16 &&
1455                     tmp != MLX5_XMETA_MODE_META32) {
1456                         DRV_LOG(ERR, "invalid extensive "
1457                                      "metadata parameter");
1458                         rte_errno = EINVAL;
1459                         return -rte_errno;
1460                 }
1461                 config->dv_xmeta_en = tmp;
1462         } else if (strcmp(MLX5_LACP_BY_USER, key) == 0) {
1463                 config->lacp_by_user = !!tmp;
1464         } else if (strcmp(MLX5_MR_EXT_MEMSEG_EN, key) == 0) {
1465                 config->mr_ext_memseg_en = !!tmp;
1466         } else if (strcmp(MLX5_MAX_DUMP_FILES_NUM, key) == 0) {
1467                 config->max_dump_files_num = tmp;
1468         } else if (strcmp(MLX5_LRO_TIMEOUT_USEC, key) == 0) {
1469                 config->lro.timeout = tmp;
1470         } else if (strcmp(MLX5_CLASS_ARG_NAME, key) == 0) {
1471                 DRV_LOG(DEBUG, "class argument is %s.", val);
1472         } else if (strcmp(MLX5_HP_BUF_SIZE, key) == 0) {
1473                 config->log_hp_size = tmp;
1474         } else if (strcmp(MLX5_RECLAIM_MEM, key) == 0) {
1475                 if (tmp != MLX5_RCM_NONE &&
1476                     tmp != MLX5_RCM_LIGHT &&
1477                     tmp != MLX5_RCM_AGGR) {
1478                         DRV_LOG(ERR, "Unrecognize %s: \"%s\"", key, val);
1479                         rte_errno = EINVAL;
1480                         return -rte_errno;
1481                 }
1482                 config->reclaim_mode = tmp;
1483         } else if (strcmp(MLX5_SYS_MEM_EN, key) == 0) {
1484                 config->sys_mem_en = !!tmp;
1485         } else if (strcmp(MLX5_DECAP_EN, key) == 0) {
1486                 config->decap_en = !!tmp;
1487         } else {
1488                 DRV_LOG(WARNING, "%s: unknown parameter", key);
1489                 rte_errno = EINVAL;
1490                 return -rte_errno;
1491         }
1492         return 0;
1493 }
1494
1495 /**
1496  * Parse device parameters.
1497  *
1498  * @param config
1499  *   Pointer to device configuration structure.
1500  * @param devargs
1501  *   Device arguments structure.
1502  *
1503  * @return
1504  *   0 on success, a negative errno value otherwise and rte_errno is set.
1505  */
1506 int
1507 mlx5_args(struct mlx5_dev_config *config, struct rte_devargs *devargs)
1508 {
1509         const char **params = (const char *[]){
1510                 MLX5_RXQ_CQE_COMP_EN,
1511                 MLX5_RXQ_CQE_PAD_EN,
1512                 MLX5_RXQ_PKT_PAD_EN,
1513                 MLX5_RX_MPRQ_EN,
1514                 MLX5_RX_MPRQ_LOG_STRIDE_NUM,
1515                 MLX5_RX_MPRQ_LOG_STRIDE_SIZE,
1516                 MLX5_RX_MPRQ_MAX_MEMCPY_LEN,
1517                 MLX5_RXQS_MIN_MPRQ,
1518                 MLX5_TXQ_INLINE,
1519                 MLX5_TXQ_INLINE_MIN,
1520                 MLX5_TXQ_INLINE_MAX,
1521                 MLX5_TXQ_INLINE_MPW,
1522                 MLX5_TXQS_MIN_INLINE,
1523                 MLX5_TXQS_MAX_VEC,
1524                 MLX5_TXQ_MPW_EN,
1525                 MLX5_TXQ_MPW_HDR_DSEG_EN,
1526                 MLX5_TXQ_MAX_INLINE_LEN,
1527                 MLX5_TX_DB_NC,
1528                 MLX5_TX_PP,
1529                 MLX5_TX_SKEW,
1530                 MLX5_TX_VEC_EN,
1531                 MLX5_RX_VEC_EN,
1532                 MLX5_L3_VXLAN_EN,
1533                 MLX5_VF_NL_EN,
1534                 MLX5_DV_ESW_EN,
1535                 MLX5_DV_FLOW_EN,
1536                 MLX5_DV_XMETA_EN,
1537                 MLX5_LACP_BY_USER,
1538                 MLX5_MR_EXT_MEMSEG_EN,
1539                 MLX5_REPRESENTOR,
1540                 MLX5_MAX_DUMP_FILES_NUM,
1541                 MLX5_LRO_TIMEOUT_USEC,
1542                 MLX5_CLASS_ARG_NAME,
1543                 MLX5_HP_BUF_SIZE,
1544                 MLX5_RECLAIM_MEM,
1545                 MLX5_SYS_MEM_EN,
1546                 MLX5_DECAP_EN,
1547                 NULL,
1548         };
1549         struct rte_kvargs *kvlist;
1550         int ret = 0;
1551         int i;
1552
1553         if (devargs == NULL)
1554                 return 0;
1555         /* Following UGLY cast is done to pass checkpatch. */
1556         kvlist = rte_kvargs_parse(devargs->args, params);
1557         if (kvlist == NULL) {
1558                 rte_errno = EINVAL;
1559                 return -rte_errno;
1560         }
1561         /* Process parameters. */
1562         for (i = 0; (params[i] != NULL); ++i) {
1563                 if (rte_kvargs_count(kvlist, params[i])) {
1564                         ret = rte_kvargs_process(kvlist, params[i],
1565                                                  mlx5_args_check, config);
1566                         if (ret) {
1567                                 rte_errno = EINVAL;
1568                                 rte_kvargs_free(kvlist);
1569                                 return -rte_errno;
1570                         }
1571                 }
1572         }
1573         rte_kvargs_free(kvlist);
1574         return 0;
1575 }
1576
1577 /**
1578  * Configures the minimal amount of data to inline into WQE
1579  * while sending packets.
1580  *
1581  * - the txq_inline_min has the maximal priority, if this
1582  *   key is specified in devargs
1583  * - if DevX is enabled the inline mode is queried from the
1584  *   device (HCA attributes and NIC vport context if needed).
1585  * - otherwise L2 mode (18 bytes) is assumed for ConnectX-4/4 Lx
1586  *   and none (0 bytes) for other NICs
1587  *
1588  * @param spawn
1589  *   Verbs device parameters (name, port, switch_info) to spawn.
1590  * @param config
1591  *   Device configuration parameters.
1592  */
1593 void
1594 mlx5_set_min_inline(struct mlx5_dev_spawn_data *spawn,
1595                     struct mlx5_dev_config *config)
1596 {
1597         if (config->txq_inline_min != MLX5_ARG_UNSET) {
1598                 /* Application defines size of inlined data explicitly. */
1599                 switch (spawn->pci_dev->id.device_id) {
1600                 case PCI_DEVICE_ID_MELLANOX_CONNECTX4:
1601                 case PCI_DEVICE_ID_MELLANOX_CONNECTX4VF:
1602                         if (config->txq_inline_min <
1603                                        (int)MLX5_INLINE_HSIZE_L2) {
1604                                 DRV_LOG(DEBUG,
1605                                         "txq_inline_mix aligned to minimal"
1606                                         " ConnectX-4 required value %d",
1607                                         (int)MLX5_INLINE_HSIZE_L2);
1608                                 config->txq_inline_min = MLX5_INLINE_HSIZE_L2;
1609                         }
1610                         break;
1611                 }
1612                 goto exit;
1613         }
1614         if (config->hca_attr.eth_net_offloads) {
1615                 /* We have DevX enabled, inline mode queried successfully. */
1616                 switch (config->hca_attr.wqe_inline_mode) {
1617                 case MLX5_CAP_INLINE_MODE_L2:
1618                         /* outer L2 header must be inlined. */
1619                         config->txq_inline_min = MLX5_INLINE_HSIZE_L2;
1620                         goto exit;
1621                 case MLX5_CAP_INLINE_MODE_NOT_REQUIRED:
1622                         /* No inline data are required by NIC. */
1623                         config->txq_inline_min = MLX5_INLINE_HSIZE_NONE;
1624                         config->hw_vlan_insert =
1625                                 config->hca_attr.wqe_vlan_insert;
1626                         DRV_LOG(DEBUG, "Tx VLAN insertion is supported");
1627                         goto exit;
1628                 case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT:
1629                         /* inline mode is defined by NIC vport context. */
1630                         if (!config->hca_attr.eth_virt)
1631                                 break;
1632                         switch (config->hca_attr.vport_inline_mode) {
1633                         case MLX5_INLINE_MODE_NONE:
1634                                 config->txq_inline_min =
1635                                         MLX5_INLINE_HSIZE_NONE;
1636                                 goto exit;
1637                         case MLX5_INLINE_MODE_L2:
1638                                 config->txq_inline_min =
1639                                         MLX5_INLINE_HSIZE_L2;
1640                                 goto exit;
1641                         case MLX5_INLINE_MODE_IP:
1642                                 config->txq_inline_min =
1643                                         MLX5_INLINE_HSIZE_L3;
1644                                 goto exit;
1645                         case MLX5_INLINE_MODE_TCP_UDP:
1646                                 config->txq_inline_min =
1647                                         MLX5_INLINE_HSIZE_L4;
1648                                 goto exit;
1649                         case MLX5_INLINE_MODE_INNER_L2:
1650                                 config->txq_inline_min =
1651                                         MLX5_INLINE_HSIZE_INNER_L2;
1652                                 goto exit;
1653                         case MLX5_INLINE_MODE_INNER_IP:
1654                                 config->txq_inline_min =
1655                                         MLX5_INLINE_HSIZE_INNER_L3;
1656                                 goto exit;
1657                         case MLX5_INLINE_MODE_INNER_TCP_UDP:
1658                                 config->txq_inline_min =
1659                                         MLX5_INLINE_HSIZE_INNER_L4;
1660                                 goto exit;
1661                         }
1662                 }
1663         }
1664         /*
1665          * We get here if we are unable to deduce
1666          * inline data size with DevX. Try PCI ID
1667          * to determine old NICs.
1668          */
1669         switch (spawn->pci_dev->id.device_id) {
1670         case PCI_DEVICE_ID_MELLANOX_CONNECTX4:
1671         case PCI_DEVICE_ID_MELLANOX_CONNECTX4VF:
1672         case PCI_DEVICE_ID_MELLANOX_CONNECTX4LX:
1673         case PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF:
1674                 config->txq_inline_min = MLX5_INLINE_HSIZE_L2;
1675                 config->hw_vlan_insert = 0;
1676                 break;
1677         case PCI_DEVICE_ID_MELLANOX_CONNECTX5:
1678         case PCI_DEVICE_ID_MELLANOX_CONNECTX5VF:
1679         case PCI_DEVICE_ID_MELLANOX_CONNECTX5EX:
1680         case PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF:
1681                 /*
1682                  * These NICs support VLAN insertion from WQE and
1683                  * report the wqe_vlan_insert flag. But there is the bug
1684                  * and PFC control may be broken, so disable feature.
1685                  */
1686                 config->hw_vlan_insert = 0;
1687                 config->txq_inline_min = MLX5_INLINE_HSIZE_NONE;
1688                 break;
1689         default:
1690                 config->txq_inline_min = MLX5_INLINE_HSIZE_NONE;
1691                 break;
1692         }
1693 exit:
1694         DRV_LOG(DEBUG, "min tx inline configured: %d", config->txq_inline_min);
1695 }
1696
1697 /**
1698  * Configures the metadata mask fields in the shared context.
1699  *
1700  * @param [in] dev
1701  *   Pointer to Ethernet device.
1702  */
1703 void
1704 mlx5_set_metadata_mask(struct rte_eth_dev *dev)
1705 {
1706         struct mlx5_priv *priv = dev->data->dev_private;
1707         struct mlx5_dev_ctx_shared *sh = priv->sh;
1708         uint32_t meta, mark, reg_c0;
1709
1710         reg_c0 = ~priv->vport_meta_mask;
1711         switch (priv->config.dv_xmeta_en) {
1712         case MLX5_XMETA_MODE_LEGACY:
1713                 meta = UINT32_MAX;
1714                 mark = MLX5_FLOW_MARK_MASK;
1715                 break;
1716         case MLX5_XMETA_MODE_META16:
1717                 meta = reg_c0 >> rte_bsf32(reg_c0);
1718                 mark = MLX5_FLOW_MARK_MASK;
1719                 break;
1720         case MLX5_XMETA_MODE_META32:
1721                 meta = UINT32_MAX;
1722                 mark = (reg_c0 >> rte_bsf32(reg_c0)) & MLX5_FLOW_MARK_MASK;
1723                 break;
1724         default:
1725                 meta = 0;
1726                 mark = 0;
1727                 MLX5_ASSERT(false);
1728                 break;
1729         }
1730         if (sh->dv_mark_mask && sh->dv_mark_mask != mark)
1731                 DRV_LOG(WARNING, "metadata MARK mask mismatche %08X:%08X",
1732                                  sh->dv_mark_mask, mark);
1733         else
1734                 sh->dv_mark_mask = mark;
1735         if (sh->dv_meta_mask && sh->dv_meta_mask != meta)
1736                 DRV_LOG(WARNING, "metadata META mask mismatche %08X:%08X",
1737                                  sh->dv_meta_mask, meta);
1738         else
1739                 sh->dv_meta_mask = meta;
1740         if (sh->dv_regc0_mask && sh->dv_regc0_mask != reg_c0)
1741                 DRV_LOG(WARNING, "metadata reg_c0 mask mismatche %08X:%08X",
1742                                  sh->dv_meta_mask, reg_c0);
1743         else
1744                 sh->dv_regc0_mask = reg_c0;
1745         DRV_LOG(DEBUG, "metadata mode %u", priv->config.dv_xmeta_en);
1746         DRV_LOG(DEBUG, "metadata MARK mask %08X", sh->dv_mark_mask);
1747         DRV_LOG(DEBUG, "metadata META mask %08X", sh->dv_meta_mask);
1748         DRV_LOG(DEBUG, "metadata reg_c0 mask %08X", sh->dv_regc0_mask);
1749 }
1750
1751 int
1752 rte_pmd_mlx5_get_dyn_flag_names(char *names[], unsigned int n)
1753 {
1754         static const char *const dynf_names[] = {
1755                 RTE_PMD_MLX5_FINE_GRANULARITY_INLINE,
1756                 RTE_MBUF_DYNFLAG_METADATA_NAME,
1757                 RTE_MBUF_DYNFLAG_TX_TIMESTAMP_NAME
1758         };
1759         unsigned int i;
1760
1761         if (n < RTE_DIM(dynf_names))
1762                 return -ENOMEM;
1763         for (i = 0; i < RTE_DIM(dynf_names); i++) {
1764                 if (names[i] == NULL)
1765                         return -EINVAL;
1766                 strcpy(names[i], dynf_names[i]);
1767         }
1768         return RTE_DIM(dynf_names);
1769 }
1770
1771 /**
1772  * Comparison callback to sort device data.
1773  *
1774  * This is meant to be used with qsort().
1775  *
1776  * @param a[in]
1777  *   Pointer to pointer to first data object.
1778  * @param b[in]
1779  *   Pointer to pointer to second data object.
1780  *
1781  * @return
1782  *   0 if both objects are equal, less than 0 if the first argument is less
1783  *   than the second, greater than 0 otherwise.
1784  */
1785 int
1786 mlx5_dev_check_sibling_config(struct mlx5_priv *priv,
1787                               struct mlx5_dev_config *config)
1788 {
1789         struct mlx5_dev_ctx_shared *sh = priv->sh;
1790         struct mlx5_dev_config *sh_conf = NULL;
1791         uint16_t port_id;
1792
1793         MLX5_ASSERT(sh);
1794         /* Nothing to compare for the single/first device. */
1795         if (sh->refcnt == 1)
1796                 return 0;
1797         /* Find the device with shared context. */
1798         MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
1799                 struct mlx5_priv *opriv =
1800                         rte_eth_devices[port_id].data->dev_private;
1801
1802                 if (opriv && opriv != priv && opriv->sh == sh) {
1803                         sh_conf = &opriv->config;
1804                         break;
1805                 }
1806         }
1807         if (!sh_conf)
1808                 return 0;
1809         if (sh_conf->dv_flow_en ^ config->dv_flow_en) {
1810                 DRV_LOG(ERR, "\"dv_flow_en\" configuration mismatch"
1811                              " for shared %s context", sh->ibdev_name);
1812                 rte_errno = EINVAL;
1813                 return rte_errno;
1814         }
1815         if (sh_conf->dv_xmeta_en ^ config->dv_xmeta_en) {
1816                 DRV_LOG(ERR, "\"dv_xmeta_en\" configuration mismatch"
1817                              " for shared %s context", sh->ibdev_name);
1818                 rte_errno = EINVAL;
1819                 return rte_errno;
1820         }
1821         return 0;
1822 }
1823
1824 /**
1825  * Look for the ethernet device belonging to mlx5 driver.
1826  *
1827  * @param[in] port_id
1828  *   port_id to start looking for device.
1829  * @param[in] pci_dev
1830  *   Pointer to the hint PCI device. When device is being probed
1831  *   the its siblings (master and preceding representors might
1832  *   not have assigned driver yet (because the mlx5_os_pci_probe()
1833  *   is not completed yet, for this case match on hint PCI
1834  *   device may be used to detect sibling device.
1835  *
1836  * @return
1837  *   port_id of found device, RTE_MAX_ETHPORT if not found.
1838  */
1839 uint16_t
1840 mlx5_eth_find_next(uint16_t port_id, struct rte_pci_device *pci_dev)
1841 {
1842         while (port_id < RTE_MAX_ETHPORTS) {
1843                 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1844
1845                 if (dev->state != RTE_ETH_DEV_UNUSED &&
1846                     dev->device &&
1847                     (dev->device == &pci_dev->device ||
1848                      (dev->device->driver &&
1849                      dev->device->driver->name &&
1850                      !strcmp(dev->device->driver->name, MLX5_DRIVER_NAME))))
1851                         break;
1852                 port_id++;
1853         }
1854         if (port_id >= RTE_MAX_ETHPORTS)
1855                 return RTE_MAX_ETHPORTS;
1856         return port_id;
1857 }
1858
1859 /**
1860  * DPDK callback to remove a PCI device.
1861  *
1862  * This function removes all Ethernet devices belong to a given PCI device.
1863  *
1864  * @param[in] pci_dev
1865  *   Pointer to the PCI device.
1866  *
1867  * @return
1868  *   0 on success, the function cannot fail.
1869  */
1870 static int
1871 mlx5_pci_remove(struct rte_pci_device *pci_dev)
1872 {
1873         uint16_t port_id;
1874
1875         RTE_ETH_FOREACH_DEV_OF(port_id, &pci_dev->device) {
1876                 /*
1877                  * mlx5_dev_close() is not registered to secondary process,
1878                  * call the close function explicitly for secondary process.
1879                  */
1880                 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1881                         mlx5_dev_close(&rte_eth_devices[port_id]);
1882                 else
1883                         rte_eth_dev_close(port_id);
1884         }
1885         return 0;
1886 }
1887
1888 static const struct rte_pci_id mlx5_pci_id_map[] = {
1889         {
1890                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1891                                PCI_DEVICE_ID_MELLANOX_CONNECTX4)
1892         },
1893         {
1894                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1895                                PCI_DEVICE_ID_MELLANOX_CONNECTX4VF)
1896         },
1897         {
1898                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1899                                PCI_DEVICE_ID_MELLANOX_CONNECTX4LX)
1900         },
1901         {
1902                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1903                                PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF)
1904         },
1905         {
1906                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1907                                PCI_DEVICE_ID_MELLANOX_CONNECTX5)
1908         },
1909         {
1910                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1911                                PCI_DEVICE_ID_MELLANOX_CONNECTX5VF)
1912         },
1913         {
1914                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1915                                PCI_DEVICE_ID_MELLANOX_CONNECTX5EX)
1916         },
1917         {
1918                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1919                                PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF)
1920         },
1921         {
1922                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1923                                PCI_DEVICE_ID_MELLANOX_CONNECTX5BF)
1924         },
1925         {
1926                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1927                                PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF)
1928         },
1929         {
1930                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1931                                 PCI_DEVICE_ID_MELLANOX_CONNECTX6)
1932         },
1933         {
1934                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1935                                 PCI_DEVICE_ID_MELLANOX_CONNECTX6VF)
1936         },
1937         {
1938                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1939                                 PCI_DEVICE_ID_MELLANOX_CONNECTX6DX)
1940         },
1941         {
1942                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1943                                 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXVF)
1944         },
1945         {
1946                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1947                                 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXBF)
1948         },
1949         {
1950                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1951                                 PCI_DEVICE_ID_MELLANOX_CONNECTX6LX)
1952         },
1953         {
1954                 .vendor_id = 0
1955         }
1956 };
1957
1958 struct rte_pci_driver mlx5_driver = {
1959         .driver = {
1960                 .name = MLX5_DRIVER_NAME
1961         },
1962         .id_table = mlx5_pci_id_map,
1963         .probe = mlx5_os_pci_probe,
1964         .remove = mlx5_pci_remove,
1965         .dma_map = mlx5_dma_map,
1966         .dma_unmap = mlx5_dma_unmap,
1967         .drv_flags = PCI_DRV_FLAGS,
1968 };
1969
1970 /* Initialize driver log type. */
1971 RTE_LOG_REGISTER(mlx5_logtype, pmd.net.mlx5, NOTICE)
1972
1973 /**
1974  * Driver initialization routine.
1975  */
1976 RTE_INIT(rte_mlx5_pmd_init)
1977 {
1978         /* Build the static tables for Verbs conversion. */
1979         mlx5_set_ptype_table();
1980         mlx5_set_cksum_table();
1981         mlx5_set_swp_types_table();
1982         if (mlx5_glue)
1983                 rte_pci_register(&mlx5_driver);
1984 }
1985
1986 RTE_PMD_EXPORT_NAME(net_mlx5, __COUNTER__);
1987 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5, mlx5_pci_id_map);
1988 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5, "* ib_uverbs & mlx5_core & mlx5_ib");