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