net/mlx5: probe port representors in natural order
[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 <assert.h>
10 #include <dlfcn.h>
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <errno.h>
14 #include <net/if.h>
15 #include <sys/mman.h>
16 #include <linux/netlink.h>
17 #include <linux/rtnetlink.h>
18
19 /* Verbs header. */
20 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
21 #ifdef PEDANTIC
22 #pragma GCC diagnostic ignored "-Wpedantic"
23 #endif
24 #include <infiniband/verbs.h>
25 #ifdef PEDANTIC
26 #pragma GCC diagnostic error "-Wpedantic"
27 #endif
28
29 #include <rte_malloc.h>
30 #include <rte_ethdev_driver.h>
31 #include <rte_ethdev_pci.h>
32 #include <rte_pci.h>
33 #include <rte_bus_pci.h>
34 #include <rte_common.h>
35 #include <rte_config.h>
36 #include <rte_eal_memconfig.h>
37 #include <rte_kvargs.h>
38 #include <rte_rwlock.h>
39 #include <rte_spinlock.h>
40 #include <rte_string_fns.h>
41
42 #include "mlx5.h"
43 #include "mlx5_utils.h"
44 #include "mlx5_rxtx.h"
45 #include "mlx5_autoconf.h"
46 #include "mlx5_defs.h"
47 #include "mlx5_glue.h"
48 #include "mlx5_mr.h"
49
50 /* Device parameter to enable RX completion queue compression. */
51 #define MLX5_RXQ_CQE_COMP_EN "rxq_cqe_comp_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 limit the size of memcpy'd packet for MPRQ. */
60 #define MLX5_RX_MPRQ_MAX_MEMCPY_LEN "mprq_max_memcpy_len"
61
62 /* Device parameter to set the minimum number of Rx queues to enable MPRQ. */
63 #define MLX5_RXQS_MIN_MPRQ "rxqs_min_mprq"
64
65 /* Device parameter to configure inline send. */
66 #define MLX5_TXQ_INLINE "txq_inline"
67
68 /*
69  * Device parameter to configure the number of TX queues threshold for
70  * enabling inline send.
71  */
72 #define MLX5_TXQS_MIN_INLINE "txqs_min_inline"
73
74 /* Device parameter to enable multi-packet send WQEs. */
75 #define MLX5_TXQ_MPW_EN "txq_mpw_en"
76
77 /* Device parameter to include 2 dsegs in the title WQEBB. */
78 #define MLX5_TXQ_MPW_HDR_DSEG_EN "txq_mpw_hdr_dseg_en"
79
80 /* Device parameter to limit the size of inlining packet. */
81 #define MLX5_TXQ_MAX_INLINE_LEN "txq_max_inline_len"
82
83 /* Device parameter to enable hardware Tx vector. */
84 #define MLX5_TX_VEC_EN "tx_vec_en"
85
86 /* Device parameter to enable hardware Rx vector. */
87 #define MLX5_RX_VEC_EN "rx_vec_en"
88
89 /* Allow L3 VXLAN flow creation. */
90 #define MLX5_L3_VXLAN_EN "l3_vxlan_en"
91
92 /* Activate Netlink support in VF mode. */
93 #define MLX5_VF_NL_EN "vf_nl_en"
94
95 #ifndef HAVE_IBV_MLX5_MOD_MPW
96 #define MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED (1 << 2)
97 #define MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW (1 << 3)
98 #endif
99
100 #ifndef HAVE_IBV_MLX5_MOD_CQE_128B_COMP
101 #define MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP (1 << 4)
102 #endif
103
104 static const char *MZ_MLX5_PMD_SHARED_DATA = "mlx5_pmd_shared_data";
105
106 /* Shared memory between primary and secondary processes. */
107 struct mlx5_shared_data *mlx5_shared_data;
108
109 /* Spinlock for mlx5_shared_data allocation. */
110 static rte_spinlock_t mlx5_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
111
112 /** Driver-specific log messages type. */
113 int mlx5_logtype;
114
115 /**
116  * Prepare shared data between primary and secondary process.
117  */
118 static void
119 mlx5_prepare_shared_data(void)
120 {
121         const struct rte_memzone *mz;
122
123         rte_spinlock_lock(&mlx5_shared_data_lock);
124         if (mlx5_shared_data == NULL) {
125                 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
126                         /* Allocate shared memory. */
127                         mz = rte_memzone_reserve(MZ_MLX5_PMD_SHARED_DATA,
128                                                  sizeof(*mlx5_shared_data),
129                                                  SOCKET_ID_ANY, 0);
130                 } else {
131                         /* Lookup allocated shared memory. */
132                         mz = rte_memzone_lookup(MZ_MLX5_PMD_SHARED_DATA);
133                 }
134                 if (mz == NULL)
135                         rte_panic("Cannot allocate mlx5 shared data\n");
136                 mlx5_shared_data = mz->addr;
137                 /* Initialize shared data. */
138                 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
139                         LIST_INIT(&mlx5_shared_data->mem_event_cb_list);
140                         rte_rwlock_init(&mlx5_shared_data->mem_event_rwlock);
141                 }
142                 rte_mem_event_callback_register("MLX5_MEM_EVENT_CB",
143                                                 mlx5_mr_mem_event_cb, NULL);
144         }
145         rte_spinlock_unlock(&mlx5_shared_data_lock);
146 }
147
148 /**
149  * Retrieve integer value from environment variable.
150  *
151  * @param[in] name
152  *   Environment variable name.
153  *
154  * @return
155  *   Integer value, 0 if the variable is not set.
156  */
157 int
158 mlx5_getenv_int(const char *name)
159 {
160         const char *val = getenv(name);
161
162         if (val == NULL)
163                 return 0;
164         return atoi(val);
165 }
166
167 /**
168  * Verbs callback to allocate a memory. This function should allocate the space
169  * according to the size provided residing inside a huge page.
170  * Please note that all allocation must respect the alignment from libmlx5
171  * (i.e. currently sysconf(_SC_PAGESIZE)).
172  *
173  * @param[in] size
174  *   The size in bytes of the memory to allocate.
175  * @param[in] data
176  *   A pointer to the callback data.
177  *
178  * @return
179  *   Allocated buffer, NULL otherwise and rte_errno is set.
180  */
181 static void *
182 mlx5_alloc_verbs_buf(size_t size, void *data)
183 {
184         struct priv *priv = data;
185         void *ret;
186         size_t alignment = sysconf(_SC_PAGESIZE);
187         unsigned int socket = SOCKET_ID_ANY;
188
189         if (priv->verbs_alloc_ctx.type == MLX5_VERBS_ALLOC_TYPE_TX_QUEUE) {
190                 const struct mlx5_txq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
191
192                 socket = ctrl->socket;
193         } else if (priv->verbs_alloc_ctx.type ==
194                    MLX5_VERBS_ALLOC_TYPE_RX_QUEUE) {
195                 const struct mlx5_rxq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
196
197                 socket = ctrl->socket;
198         }
199         assert(data != NULL);
200         ret = rte_malloc_socket(__func__, size, alignment, socket);
201         if (!ret && size)
202                 rte_errno = ENOMEM;
203         return ret;
204 }
205
206 /**
207  * Verbs callback to free a memory.
208  *
209  * @param[in] ptr
210  *   A pointer to the memory to free.
211  * @param[in] data
212  *   A pointer to the callback data.
213  */
214 static void
215 mlx5_free_verbs_buf(void *ptr, void *data __rte_unused)
216 {
217         assert(data != NULL);
218         rte_free(ptr);
219 }
220
221 /**
222  * DPDK callback to close the device.
223  *
224  * Destroy all queues and objects, free memory.
225  *
226  * @param dev
227  *   Pointer to Ethernet device structure.
228  */
229 static void
230 mlx5_dev_close(struct rte_eth_dev *dev)
231 {
232         struct priv *priv = dev->data->dev_private;
233         unsigned int i;
234         int ret;
235
236         DRV_LOG(DEBUG, "port %u closing device \"%s\"",
237                 dev->data->port_id,
238                 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
239         /* In case mlx5_dev_stop() has not been called. */
240         mlx5_dev_interrupt_handler_uninstall(dev);
241         mlx5_traffic_disable(dev);
242         /* Prevent crashes when queues are still in use. */
243         dev->rx_pkt_burst = removed_rx_burst;
244         dev->tx_pkt_burst = removed_tx_burst;
245         if (priv->rxqs != NULL) {
246                 /* XXX race condition if mlx5_rx_burst() is still running. */
247                 usleep(1000);
248                 for (i = 0; (i != priv->rxqs_n); ++i)
249                         mlx5_rxq_release(dev, i);
250                 priv->rxqs_n = 0;
251                 priv->rxqs = NULL;
252         }
253         if (priv->txqs != NULL) {
254                 /* XXX race condition if mlx5_tx_burst() is still running. */
255                 usleep(1000);
256                 for (i = 0; (i != priv->txqs_n); ++i)
257                         mlx5_txq_release(dev, i);
258                 priv->txqs_n = 0;
259                 priv->txqs = NULL;
260         }
261         mlx5_flow_delete_drop_queue(dev);
262         mlx5_mprq_free_mp(dev);
263         mlx5_mr_release(dev);
264         if (priv->pd != NULL) {
265                 assert(priv->ctx != NULL);
266                 claim_zero(mlx5_glue->dealloc_pd(priv->pd));
267                 claim_zero(mlx5_glue->close_device(priv->ctx));
268         } else
269                 assert(priv->ctx == NULL);
270         if (priv->rss_conf.rss_key != NULL)
271                 rte_free(priv->rss_conf.rss_key);
272         if (priv->reta_idx != NULL)
273                 rte_free(priv->reta_idx);
274         if (priv->primary_socket)
275                 mlx5_socket_uninit(dev);
276         if (priv->config.vf)
277                 mlx5_nl_mac_addr_flush(dev);
278         if (priv->nl_socket_route >= 0)
279                 close(priv->nl_socket_route);
280         if (priv->nl_socket_rdma >= 0)
281                 close(priv->nl_socket_rdma);
282         ret = mlx5_hrxq_ibv_verify(dev);
283         if (ret)
284                 DRV_LOG(WARNING, "port %u some hash Rx queue still remain",
285                         dev->data->port_id);
286         ret = mlx5_ind_table_ibv_verify(dev);
287         if (ret)
288                 DRV_LOG(WARNING, "port %u some indirection table still remain",
289                         dev->data->port_id);
290         ret = mlx5_rxq_ibv_verify(dev);
291         if (ret)
292                 DRV_LOG(WARNING, "port %u some Verbs Rx queue still remain",
293                         dev->data->port_id);
294         ret = mlx5_rxq_verify(dev);
295         if (ret)
296                 DRV_LOG(WARNING, "port %u some Rx queues still remain",
297                         dev->data->port_id);
298         ret = mlx5_txq_ibv_verify(dev);
299         if (ret)
300                 DRV_LOG(WARNING, "port %u some Verbs Tx queue still remain",
301                         dev->data->port_id);
302         ret = mlx5_txq_verify(dev);
303         if (ret)
304                 DRV_LOG(WARNING, "port %u some Tx queues still remain",
305                         dev->data->port_id);
306         ret = mlx5_flow_verify(dev);
307         if (ret)
308                 DRV_LOG(WARNING, "port %u some flows still remain",
309                         dev->data->port_id);
310         if (priv->domain_id != RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
311                 unsigned int c = 0;
312                 unsigned int i = mlx5_dev_to_port_id(dev->device, NULL, 0);
313                 uint16_t port_id[i];
314
315                 i = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, i), i);
316                 while (i--) {
317                         struct priv *opriv =
318                                 rte_eth_devices[port_id[i]].data->dev_private;
319
320                         if (!opriv ||
321                             opriv->domain_id != priv->domain_id ||
322                             &rte_eth_devices[port_id[i]] == dev)
323                                 continue;
324                         ++c;
325                 }
326                 if (!c)
327                         claim_zero(rte_eth_switch_domain_free(priv->domain_id));
328         }
329         memset(priv, 0, sizeof(*priv));
330         priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
331 }
332
333 const struct eth_dev_ops mlx5_dev_ops = {
334         .dev_configure = mlx5_dev_configure,
335         .dev_start = mlx5_dev_start,
336         .dev_stop = mlx5_dev_stop,
337         .dev_set_link_down = mlx5_set_link_down,
338         .dev_set_link_up = mlx5_set_link_up,
339         .dev_close = mlx5_dev_close,
340         .promiscuous_enable = mlx5_promiscuous_enable,
341         .promiscuous_disable = mlx5_promiscuous_disable,
342         .allmulticast_enable = mlx5_allmulticast_enable,
343         .allmulticast_disable = mlx5_allmulticast_disable,
344         .link_update = mlx5_link_update,
345         .stats_get = mlx5_stats_get,
346         .stats_reset = mlx5_stats_reset,
347         .xstats_get = mlx5_xstats_get,
348         .xstats_reset = mlx5_xstats_reset,
349         .xstats_get_names = mlx5_xstats_get_names,
350         .dev_infos_get = mlx5_dev_infos_get,
351         .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
352         .vlan_filter_set = mlx5_vlan_filter_set,
353         .rx_queue_setup = mlx5_rx_queue_setup,
354         .tx_queue_setup = mlx5_tx_queue_setup,
355         .rx_queue_release = mlx5_rx_queue_release,
356         .tx_queue_release = mlx5_tx_queue_release,
357         .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
358         .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
359         .mac_addr_remove = mlx5_mac_addr_remove,
360         .mac_addr_add = mlx5_mac_addr_add,
361         .mac_addr_set = mlx5_mac_addr_set,
362         .set_mc_addr_list = mlx5_set_mc_addr_list,
363         .mtu_set = mlx5_dev_set_mtu,
364         .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
365         .vlan_offload_set = mlx5_vlan_offload_set,
366         .reta_update = mlx5_dev_rss_reta_update,
367         .reta_query = mlx5_dev_rss_reta_query,
368         .rss_hash_update = mlx5_rss_hash_update,
369         .rss_hash_conf_get = mlx5_rss_hash_conf_get,
370         .filter_ctrl = mlx5_dev_filter_ctrl,
371         .rx_descriptor_status = mlx5_rx_descriptor_status,
372         .tx_descriptor_status = mlx5_tx_descriptor_status,
373         .rx_queue_intr_enable = mlx5_rx_intr_enable,
374         .rx_queue_intr_disable = mlx5_rx_intr_disable,
375         .is_removed = mlx5_is_removed,
376 };
377
378 static const struct eth_dev_ops mlx5_dev_sec_ops = {
379         .stats_get = mlx5_stats_get,
380         .stats_reset = mlx5_stats_reset,
381         .xstats_get = mlx5_xstats_get,
382         .xstats_reset = mlx5_xstats_reset,
383         .xstats_get_names = mlx5_xstats_get_names,
384         .dev_infos_get = mlx5_dev_infos_get,
385         .rx_descriptor_status = mlx5_rx_descriptor_status,
386         .tx_descriptor_status = mlx5_tx_descriptor_status,
387 };
388
389 /* Available operators in flow isolated mode. */
390 const struct eth_dev_ops mlx5_dev_ops_isolate = {
391         .dev_configure = mlx5_dev_configure,
392         .dev_start = mlx5_dev_start,
393         .dev_stop = mlx5_dev_stop,
394         .dev_set_link_down = mlx5_set_link_down,
395         .dev_set_link_up = mlx5_set_link_up,
396         .dev_close = mlx5_dev_close,
397         .link_update = mlx5_link_update,
398         .stats_get = mlx5_stats_get,
399         .stats_reset = mlx5_stats_reset,
400         .xstats_get = mlx5_xstats_get,
401         .xstats_reset = mlx5_xstats_reset,
402         .xstats_get_names = mlx5_xstats_get_names,
403         .dev_infos_get = mlx5_dev_infos_get,
404         .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
405         .vlan_filter_set = mlx5_vlan_filter_set,
406         .rx_queue_setup = mlx5_rx_queue_setup,
407         .tx_queue_setup = mlx5_tx_queue_setup,
408         .rx_queue_release = mlx5_rx_queue_release,
409         .tx_queue_release = mlx5_tx_queue_release,
410         .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
411         .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
412         .mac_addr_remove = mlx5_mac_addr_remove,
413         .mac_addr_add = mlx5_mac_addr_add,
414         .mac_addr_set = mlx5_mac_addr_set,
415         .set_mc_addr_list = mlx5_set_mc_addr_list,
416         .mtu_set = mlx5_dev_set_mtu,
417         .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
418         .vlan_offload_set = mlx5_vlan_offload_set,
419         .filter_ctrl = mlx5_dev_filter_ctrl,
420         .rx_descriptor_status = mlx5_rx_descriptor_status,
421         .tx_descriptor_status = mlx5_tx_descriptor_status,
422         .rx_queue_intr_enable = mlx5_rx_intr_enable,
423         .rx_queue_intr_disable = mlx5_rx_intr_disable,
424         .is_removed = mlx5_is_removed,
425 };
426
427 /**
428  * Verify and store value for device argument.
429  *
430  * @param[in] key
431  *   Key argument to verify.
432  * @param[in] val
433  *   Value associated with key.
434  * @param opaque
435  *   User data.
436  *
437  * @return
438  *   0 on success, a negative errno value otherwise and rte_errno is set.
439  */
440 static int
441 mlx5_args_check(const char *key, const char *val, void *opaque)
442 {
443         struct mlx5_dev_config *config = opaque;
444         unsigned long tmp;
445
446         errno = 0;
447         tmp = strtoul(val, NULL, 0);
448         if (errno) {
449                 rte_errno = errno;
450                 DRV_LOG(WARNING, "%s: \"%s\" is not a valid integer", key, val);
451                 return -rte_errno;
452         }
453         if (strcmp(MLX5_RXQ_CQE_COMP_EN, key) == 0) {
454                 config->cqe_comp = !!tmp;
455         } else if (strcmp(MLX5_RX_MPRQ_EN, key) == 0) {
456                 config->mprq.enabled = !!tmp;
457         } else if (strcmp(MLX5_RX_MPRQ_LOG_STRIDE_NUM, key) == 0) {
458                 config->mprq.stride_num_n = tmp;
459         } else if (strcmp(MLX5_RX_MPRQ_MAX_MEMCPY_LEN, key) == 0) {
460                 config->mprq.max_memcpy_len = tmp;
461         } else if (strcmp(MLX5_RXQS_MIN_MPRQ, key) == 0) {
462                 config->mprq.min_rxqs_num = tmp;
463         } else if (strcmp(MLX5_TXQ_INLINE, key) == 0) {
464                 config->txq_inline = tmp;
465         } else if (strcmp(MLX5_TXQS_MIN_INLINE, key) == 0) {
466                 config->txqs_inline = tmp;
467         } else if (strcmp(MLX5_TXQ_MPW_EN, key) == 0) {
468                 config->mps = !!tmp ? config->mps : 0;
469         } else if (strcmp(MLX5_TXQ_MPW_HDR_DSEG_EN, key) == 0) {
470                 config->mpw_hdr_dseg = !!tmp;
471         } else if (strcmp(MLX5_TXQ_MAX_INLINE_LEN, key) == 0) {
472                 config->inline_max_packet_sz = tmp;
473         } else if (strcmp(MLX5_TX_VEC_EN, key) == 0) {
474                 config->tx_vec_en = !!tmp;
475         } else if (strcmp(MLX5_RX_VEC_EN, key) == 0) {
476                 config->rx_vec_en = !!tmp;
477         } else if (strcmp(MLX5_L3_VXLAN_EN, key) == 0) {
478                 config->l3_vxlan_en = !!tmp;
479         } else if (strcmp(MLX5_VF_NL_EN, key) == 0) {
480                 config->vf_nl_en = !!tmp;
481         } else {
482                 DRV_LOG(WARNING, "%s: unknown parameter", key);
483                 rte_errno = EINVAL;
484                 return -rte_errno;
485         }
486         return 0;
487 }
488
489 /**
490  * Parse device parameters.
491  *
492  * @param config
493  *   Pointer to device configuration structure.
494  * @param devargs
495  *   Device arguments structure.
496  *
497  * @return
498  *   0 on success, a negative errno value otherwise and rte_errno is set.
499  */
500 static int
501 mlx5_args(struct mlx5_dev_config *config, struct rte_devargs *devargs)
502 {
503         const char **params = (const char *[]){
504                 MLX5_RXQ_CQE_COMP_EN,
505                 MLX5_RX_MPRQ_EN,
506                 MLX5_RX_MPRQ_LOG_STRIDE_NUM,
507                 MLX5_RX_MPRQ_MAX_MEMCPY_LEN,
508                 MLX5_RXQS_MIN_MPRQ,
509                 MLX5_TXQ_INLINE,
510                 MLX5_TXQS_MIN_INLINE,
511                 MLX5_TXQ_MPW_EN,
512                 MLX5_TXQ_MPW_HDR_DSEG_EN,
513                 MLX5_TXQ_MAX_INLINE_LEN,
514                 MLX5_TX_VEC_EN,
515                 MLX5_RX_VEC_EN,
516                 MLX5_L3_VXLAN_EN,
517                 MLX5_VF_NL_EN,
518                 NULL,
519         };
520         struct rte_kvargs *kvlist;
521         int ret = 0;
522         int i;
523
524         if (devargs == NULL)
525                 return 0;
526         /* Following UGLY cast is done to pass checkpatch. */
527         kvlist = rte_kvargs_parse(devargs->args, params);
528         if (kvlist == NULL)
529                 return 0;
530         /* Process parameters. */
531         for (i = 0; (params[i] != NULL); ++i) {
532                 if (rte_kvargs_count(kvlist, params[i])) {
533                         ret = rte_kvargs_process(kvlist, params[i],
534                                                  mlx5_args_check, config);
535                         if (ret) {
536                                 rte_errno = EINVAL;
537                                 rte_kvargs_free(kvlist);
538                                 return -rte_errno;
539                         }
540                 }
541         }
542         rte_kvargs_free(kvlist);
543         return 0;
544 }
545
546 static struct rte_pci_driver mlx5_driver;
547
548 /*
549  * Reserved UAR address space for TXQ UAR(hw doorbell) mapping, process
550  * local resource used by both primary and secondary to avoid duplicate
551  * reservation.
552  * The space has to be available on both primary and secondary process,
553  * TXQ UAR maps to this area using fixed mmap w/o double check.
554  */
555 static void *uar_base;
556
557 static int
558 find_lower_va_bound(const struct rte_memseg_list *msl __rte_unused,
559                 const struct rte_memseg *ms, void *arg)
560 {
561         void **addr = arg;
562
563         if (*addr == NULL)
564                 *addr = ms->addr;
565         else
566                 *addr = RTE_MIN(*addr, ms->addr);
567
568         return 0;
569 }
570
571 /**
572  * Reserve UAR address space for primary process.
573  *
574  * @param[in] dev
575  *   Pointer to Ethernet device.
576  *
577  * @return
578  *   0 on success, a negative errno value otherwise and rte_errno is set.
579  */
580 static int
581 mlx5_uar_init_primary(struct rte_eth_dev *dev)
582 {
583         struct priv *priv = dev->data->dev_private;
584         void *addr = (void *)0;
585
586         if (uar_base) { /* UAR address space mapped. */
587                 priv->uar_base = uar_base;
588                 return 0;
589         }
590         /* find out lower bound of hugepage segments */
591         rte_memseg_walk(find_lower_va_bound, &addr);
592
593         /* keep distance to hugepages to minimize potential conflicts. */
594         addr = RTE_PTR_SUB(addr, MLX5_UAR_OFFSET + MLX5_UAR_SIZE);
595         /* anonymous mmap, no real memory consumption. */
596         addr = mmap(addr, MLX5_UAR_SIZE,
597                     PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
598         if (addr == MAP_FAILED) {
599                 DRV_LOG(ERR,
600                         "port %u failed to reserve UAR address space, please"
601                         " adjust MLX5_UAR_SIZE or try --base-virtaddr",
602                         dev->data->port_id);
603                 rte_errno = ENOMEM;
604                 return -rte_errno;
605         }
606         /* Accept either same addr or a new addr returned from mmap if target
607          * range occupied.
608          */
609         DRV_LOG(INFO, "port %u reserved UAR address space: %p",
610                 dev->data->port_id, addr);
611         priv->uar_base = addr; /* for primary and secondary UAR re-mmap. */
612         uar_base = addr; /* process local, don't reserve again. */
613         return 0;
614 }
615
616 /**
617  * Reserve UAR address space for secondary process, align with
618  * primary process.
619  *
620  * @param[in] dev
621  *   Pointer to Ethernet device.
622  *
623  * @return
624  *   0 on success, a negative errno value otherwise and rte_errno is set.
625  */
626 static int
627 mlx5_uar_init_secondary(struct rte_eth_dev *dev)
628 {
629         struct priv *priv = dev->data->dev_private;
630         void *addr;
631
632         assert(priv->uar_base);
633         if (uar_base) { /* already reserved. */
634                 assert(uar_base == priv->uar_base);
635                 return 0;
636         }
637         /* anonymous mmap, no real memory consumption. */
638         addr = mmap(priv->uar_base, MLX5_UAR_SIZE,
639                     PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
640         if (addr == MAP_FAILED) {
641                 DRV_LOG(ERR, "port %u UAR mmap failed: %p size: %llu",
642                         dev->data->port_id, priv->uar_base, MLX5_UAR_SIZE);
643                 rte_errno = ENXIO;
644                 return -rte_errno;
645         }
646         if (priv->uar_base != addr) {
647                 DRV_LOG(ERR,
648                         "port %u UAR address %p size %llu occupied, please"
649                         " adjust MLX5_UAR_OFFSET or try EAL parameter"
650                         " --base-virtaddr",
651                         dev->data->port_id, priv->uar_base, MLX5_UAR_SIZE);
652                 rte_errno = ENXIO;
653                 return -rte_errno;
654         }
655         uar_base = addr; /* process local, don't reserve again */
656         DRV_LOG(INFO, "port %u reserved UAR address space: %p",
657                 dev->data->port_id, addr);
658         return 0;
659 }
660
661 /**
662  * Spawn an Ethernet device from Verbs information.
663  *
664  * @param dpdk_dev
665  *   Backing DPDK device.
666  * @param ibv_dev
667  *   Verbs device.
668  * @param vf
669  *   If nonzero, enable VF-specific features.
670  * @param[in] switch_info
671  *   Switch properties of Ethernet device.
672  *
673  * @return
674  *   A valid Ethernet device object on success, NULL otherwise and rte_errno
675  *   is set.
676  */
677 static struct rte_eth_dev *
678 mlx5_dev_spawn(struct rte_device *dpdk_dev,
679                struct ibv_device *ibv_dev,
680                int vf,
681                const struct mlx5_switch_info *switch_info)
682 {
683         struct ibv_context *ctx;
684         struct ibv_device_attr_ex attr;
685         struct ibv_port_attr port_attr;
686         struct ibv_pd *pd = NULL;
687         struct mlx5dv_context dv_attr = { .comp_mask = 0 };
688         struct mlx5_dev_config config = {
689                 .vf = !!vf,
690                 .tx_vec_en = 1,
691                 .rx_vec_en = 1,
692                 .mpw_hdr_dseg = 0,
693                 .txq_inline = MLX5_ARG_UNSET,
694                 .txqs_inline = MLX5_ARG_UNSET,
695                 .inline_max_packet_sz = MLX5_ARG_UNSET,
696                 .vf_nl_en = 1,
697                 .mprq = {
698                         .enabled = 0,
699                         .stride_num_n = MLX5_MPRQ_STRIDE_NUM_N,
700                         .max_memcpy_len = MLX5_MPRQ_MEMCPY_DEFAULT_LEN,
701                         .min_rxqs_num = MLX5_MPRQ_MIN_RXQS,
702                 },
703         };
704         struct rte_eth_dev *eth_dev = NULL;
705         struct priv *priv = NULL;
706         int err = 0;
707         unsigned int mps;
708         unsigned int cqe_comp;
709         unsigned int tunnel_en = 0;
710         unsigned int mpls_en = 0;
711         unsigned int swp = 0;
712         unsigned int verb_priorities = 0;
713         unsigned int mprq = 0;
714         unsigned int mprq_min_stride_size_n = 0;
715         unsigned int mprq_max_stride_size_n = 0;
716         unsigned int mprq_min_stride_num_n = 0;
717         unsigned int mprq_max_stride_num_n = 0;
718 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
719         struct ibv_counter_set_description cs_desc = { .counter_type = 0 };
720 #endif
721         struct ether_addr mac;
722         char name[RTE_ETH_NAME_MAX_LEN];
723         int own_domain_id = 0;
724         unsigned int i;
725
726         /* Prepare shared data between primary and secondary process. */
727         mlx5_prepare_shared_data();
728         errno = 0;
729         ctx = mlx5_glue->open_device(ibv_dev);
730         if (!ctx) {
731                 rte_errno = errno ? errno : ENODEV;
732                 return NULL;
733         }
734 #ifdef HAVE_IBV_MLX5_MOD_SWP
735         dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_SWP;
736 #endif
737         /*
738          * Multi-packet send is supported by ConnectX-4 Lx PF as well
739          * as all ConnectX-5 devices.
740          */
741 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
742         dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS;
743 #endif
744 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
745         dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_STRIDING_RQ;
746 #endif
747         mlx5_glue->dv_query_device(ctx, &dv_attr);
748         if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED) {
749                 if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW) {
750                         DRV_LOG(DEBUG, "enhanced MPW is supported");
751                         mps = MLX5_MPW_ENHANCED;
752                 } else {
753                         DRV_LOG(DEBUG, "MPW is supported");
754                         mps = MLX5_MPW;
755                 }
756         } else {
757                 DRV_LOG(DEBUG, "MPW isn't supported");
758                 mps = MLX5_MPW_DISABLED;
759         }
760         config.mps = mps;
761 #ifdef HAVE_IBV_MLX5_MOD_SWP
762         if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_SWP)
763                 swp = dv_attr.sw_parsing_caps.sw_parsing_offloads;
764         DRV_LOG(DEBUG, "SWP support: %u", swp);
765 #endif
766         config.swp = !!swp;
767 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
768         if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_STRIDING_RQ) {
769                 struct mlx5dv_striding_rq_caps mprq_caps =
770                         dv_attr.striding_rq_caps;
771
772                 DRV_LOG(DEBUG, "\tmin_single_stride_log_num_of_bytes: %d",
773                         mprq_caps.min_single_stride_log_num_of_bytes);
774                 DRV_LOG(DEBUG, "\tmax_single_stride_log_num_of_bytes: %d",
775                         mprq_caps.max_single_stride_log_num_of_bytes);
776                 DRV_LOG(DEBUG, "\tmin_single_wqe_log_num_of_strides: %d",
777                         mprq_caps.min_single_wqe_log_num_of_strides);
778                 DRV_LOG(DEBUG, "\tmax_single_wqe_log_num_of_strides: %d",
779                         mprq_caps.max_single_wqe_log_num_of_strides);
780                 DRV_LOG(DEBUG, "\tsupported_qpts: %d",
781                         mprq_caps.supported_qpts);
782                 DRV_LOG(DEBUG, "device supports Multi-Packet RQ");
783                 mprq = 1;
784                 mprq_min_stride_size_n =
785                         mprq_caps.min_single_stride_log_num_of_bytes;
786                 mprq_max_stride_size_n =
787                         mprq_caps.max_single_stride_log_num_of_bytes;
788                 mprq_min_stride_num_n =
789                         mprq_caps.min_single_wqe_log_num_of_strides;
790                 mprq_max_stride_num_n =
791                         mprq_caps.max_single_wqe_log_num_of_strides;
792                 config.mprq.stride_num_n = RTE_MAX(MLX5_MPRQ_STRIDE_NUM_N,
793                                                    mprq_min_stride_num_n);
794         }
795 #endif
796         if (RTE_CACHE_LINE_SIZE == 128 &&
797             !(dv_attr.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP))
798                 cqe_comp = 0;
799         else
800                 cqe_comp = 1;
801         config.cqe_comp = cqe_comp;
802 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
803         if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS) {
804                 tunnel_en = ((dv_attr.tunnel_offloads_caps &
805                               MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_VXLAN) &&
806                              (dv_attr.tunnel_offloads_caps &
807                               MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_GRE));
808         }
809         DRV_LOG(DEBUG, "tunnel offloading is %ssupported",
810                 tunnel_en ? "" : "not ");
811 #else
812         DRV_LOG(WARNING,
813                 "tunnel offloading disabled due to old OFED/rdma-core version");
814 #endif
815         config.tunnel_en = tunnel_en;
816 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
817         mpls_en = ((dv_attr.tunnel_offloads_caps &
818                     MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_GRE) &&
819                    (dv_attr.tunnel_offloads_caps &
820                     MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_UDP));
821         DRV_LOG(DEBUG, "MPLS over GRE/UDP tunnel offloading is %ssupported",
822                 mpls_en ? "" : "not ");
823 #else
824         DRV_LOG(WARNING, "MPLS over GRE/UDP tunnel offloading disabled due to"
825                 " old OFED/rdma-core version or firmware configuration");
826 #endif
827         config.mpls_en = mpls_en;
828         err = mlx5_glue->query_device_ex(ctx, NULL, &attr);
829         if (err) {
830                 DEBUG("ibv_query_device_ex() failed");
831                 goto error;
832         }
833         if (!switch_info->representor)
834                 rte_strlcpy(name, dpdk_dev->name, sizeof(name));
835         else
836                 snprintf(name, sizeof(name), "%s_representor_%u",
837                          dpdk_dev->name, switch_info->port_name);
838         DRV_LOG(DEBUG, "naming Ethernet device \"%s\"", name);
839         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
840                 eth_dev = rte_eth_dev_attach_secondary(name);
841                 if (eth_dev == NULL) {
842                         DRV_LOG(ERR, "can not attach rte ethdev");
843                         rte_errno = ENOMEM;
844                         err = rte_errno;
845                         goto error;
846                 }
847                 eth_dev->device = dpdk_dev;
848                 eth_dev->dev_ops = &mlx5_dev_sec_ops;
849                 err = mlx5_uar_init_secondary(eth_dev);
850                 if (err) {
851                         err = rte_errno;
852                         goto error;
853                 }
854                 /* Receive command fd from primary process */
855                 err = mlx5_socket_connect(eth_dev);
856                 if (err < 0) {
857                         err = rte_errno;
858                         goto error;
859                 }
860                 /* Remap UAR for Tx queues. */
861                 err = mlx5_tx_uar_remap(eth_dev, err);
862                 if (err) {
863                         err = rte_errno;
864                         goto error;
865                 }
866                 /*
867                  * Ethdev pointer is still required as input since
868                  * the primary device is not accessible from the
869                  * secondary process.
870                  */
871                 eth_dev->rx_pkt_burst = mlx5_select_rx_function(eth_dev);
872                 eth_dev->tx_pkt_burst = mlx5_select_tx_function(eth_dev);
873                 claim_zero(mlx5_glue->close_device(ctx));
874                 return eth_dev;
875         }
876         /* Check port status. */
877         err = mlx5_glue->query_port(ctx, 1, &port_attr);
878         if (err) {
879                 DRV_LOG(ERR, "port query failed: %s", strerror(err));
880                 goto error;
881         }
882         if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
883                 DRV_LOG(ERR, "port is not configured in Ethernet mode");
884                 err = EINVAL;
885                 goto error;
886         }
887         if (port_attr.state != IBV_PORT_ACTIVE)
888                 DRV_LOG(DEBUG, "port is not active: \"%s\" (%d)",
889                         mlx5_glue->port_state_str(port_attr.state),
890                         port_attr.state);
891         /* Allocate protection domain. */
892         pd = mlx5_glue->alloc_pd(ctx);
893         if (pd == NULL) {
894                 DRV_LOG(ERR, "PD allocation failure");
895                 err = ENOMEM;
896                 goto error;
897         }
898         priv = rte_zmalloc("ethdev private structure",
899                            sizeof(*priv),
900                            RTE_CACHE_LINE_SIZE);
901         if (priv == NULL) {
902                 DRV_LOG(ERR, "priv allocation failure");
903                 err = ENOMEM;
904                 goto error;
905         }
906         priv->ctx = ctx;
907         strncpy(priv->ibdev_name, priv->ctx->device->name,
908                 sizeof(priv->ibdev_name));
909         strncpy(priv->ibdev_path, priv->ctx->device->ibdev_path,
910                 sizeof(priv->ibdev_path));
911         priv->device_attr = attr;
912         priv->pd = pd;
913         priv->mtu = ETHER_MTU;
914         /* Some internal functions rely on Netlink sockets, open them now. */
915         priv->nl_socket_rdma = mlx5_nl_init(0, NETLINK_RDMA);
916         priv->nl_socket_route = mlx5_nl_init(RTMGRP_LINK, NETLINK_ROUTE);
917         priv->nl_sn = 0;
918         priv->representor = !!switch_info->representor;
919         priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
920         priv->representor_id =
921                 switch_info->representor ? switch_info->port_name : -1;
922         /*
923          * Look for sibling devices in order to reuse their switch domain
924          * if any, otherwise allocate one.
925          */
926         i = mlx5_dev_to_port_id(dpdk_dev, NULL, 0);
927         if (i > 0) {
928                 uint16_t port_id[i];
929
930                 i = RTE_MIN(mlx5_dev_to_port_id(dpdk_dev, port_id, i), i);
931                 while (i--) {
932                         const struct priv *opriv =
933                                 rte_eth_devices[port_id[i]].data->dev_private;
934
935                         if (!opriv ||
936                             opriv->domain_id ==
937                             RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID)
938                                 continue;
939                         priv->domain_id = opriv->domain_id;
940                         break;
941                 }
942         }
943         if (priv->domain_id == RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
944                 err = rte_eth_switch_domain_alloc(&priv->domain_id);
945                 if (err) {
946                         err = rte_errno;
947                         DRV_LOG(ERR, "unable to allocate switch domain: %s",
948                                 strerror(rte_errno));
949                         goto error;
950                 }
951                 own_domain_id = 1;
952         }
953         err = mlx5_args(&config, dpdk_dev->devargs);
954         if (err) {
955                 err = rte_errno;
956                 DRV_LOG(ERR, "failed to process device arguments: %s",
957                         strerror(rte_errno));
958                 goto error;
959         }
960         config.hw_csum = !!(attr.device_cap_flags_ex & IBV_DEVICE_RAW_IP_CSUM);
961         DRV_LOG(DEBUG, "checksum offloading is %ssupported",
962                 (config.hw_csum ? "" : "not "));
963 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
964         config.flow_counter_en = !!attr.max_counter_sets;
965         mlx5_glue->describe_counter_set(ctx, 0, &cs_desc);
966         DRV_LOG(DEBUG, "counter type = %d, num of cs = %ld, attributes = %d",
967                 cs_desc.counter_type, cs_desc.num_of_cs,
968                 cs_desc.attributes);
969 #endif
970         config.ind_table_max_size =
971                 attr.rss_caps.max_rwq_indirection_table_size;
972         /*
973          * Remove this check once DPDK supports larger/variable
974          * indirection tables.
975          */
976         if (config.ind_table_max_size > (unsigned int)ETH_RSS_RETA_SIZE_512)
977                 config.ind_table_max_size = ETH_RSS_RETA_SIZE_512;
978         DRV_LOG(DEBUG, "maximum Rx indirection table size is %u",
979                 config.ind_table_max_size);
980         config.hw_vlan_strip = !!(attr.raw_packet_caps &
981                                   IBV_RAW_PACKET_CAP_CVLAN_STRIPPING);
982         DRV_LOG(DEBUG, "VLAN stripping is %ssupported",
983                 (config.hw_vlan_strip ? "" : "not "));
984         config.hw_fcs_strip = !!(attr.raw_packet_caps &
985                                  IBV_RAW_PACKET_CAP_SCATTER_FCS);
986         DRV_LOG(DEBUG, "FCS stripping configuration is %ssupported",
987                 (config.hw_fcs_strip ? "" : "not "));
988 #ifdef HAVE_IBV_WQ_FLAG_RX_END_PADDING
989         config.hw_padding = !!attr.rx_pad_end_addr_align;
990 #endif
991         DRV_LOG(DEBUG, "hardware Rx end alignment padding is %ssupported",
992                 (config.hw_padding ? "" : "not "));
993         config.tso = (attr.tso_caps.max_tso > 0 &&
994                       (attr.tso_caps.supported_qpts &
995                        (1 << IBV_QPT_RAW_PACKET)));
996         if (config.tso)
997                 config.tso_max_payload_sz = attr.tso_caps.max_tso;
998         if (config.mps && !mps) {
999                 DRV_LOG(ERR,
1000                         "multi-packet send not supported on this device"
1001                         " (" MLX5_TXQ_MPW_EN ")");
1002                 err = ENOTSUP;
1003                 goto error;
1004         }
1005         DRV_LOG(INFO, "%sMPS is %s",
1006                 config.mps == MLX5_MPW_ENHANCED ? "enhanced " : "",
1007                 config.mps != MLX5_MPW_DISABLED ? "enabled" : "disabled");
1008         if (config.cqe_comp && !cqe_comp) {
1009                 DRV_LOG(WARNING, "Rx CQE compression isn't supported");
1010                 config.cqe_comp = 0;
1011         }
1012         if (config.mprq.enabled && mprq) {
1013                 if (config.mprq.stride_num_n > mprq_max_stride_num_n ||
1014                     config.mprq.stride_num_n < mprq_min_stride_num_n) {
1015                         config.mprq.stride_num_n =
1016                                 RTE_MAX(MLX5_MPRQ_STRIDE_NUM_N,
1017                                         mprq_min_stride_num_n);
1018                         DRV_LOG(WARNING,
1019                                 "the number of strides"
1020                                 " for Multi-Packet RQ is out of range,"
1021                                 " setting default value (%u)",
1022                                 1 << config.mprq.stride_num_n);
1023                 }
1024                 config.mprq.min_stride_size_n = mprq_min_stride_size_n;
1025                 config.mprq.max_stride_size_n = mprq_max_stride_size_n;
1026         } else if (config.mprq.enabled && !mprq) {
1027                 DRV_LOG(WARNING, "Multi-Packet RQ isn't supported");
1028                 config.mprq.enabled = 0;
1029         }
1030         eth_dev = rte_eth_dev_allocate(name);
1031         if (eth_dev == NULL) {
1032                 DRV_LOG(ERR, "can not allocate rte ethdev");
1033                 err = ENOMEM;
1034                 goto error;
1035         }
1036         if (priv->representor)
1037                 eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
1038         eth_dev->data->dev_private = priv;
1039         priv->dev_data = eth_dev->data;
1040         eth_dev->data->mac_addrs = priv->mac;
1041         eth_dev->device = dpdk_dev;
1042         eth_dev->device->driver = &mlx5_driver.driver;
1043         err = mlx5_uar_init_primary(eth_dev);
1044         if (err) {
1045                 err = rte_errno;
1046                 goto error;
1047         }
1048         /* Configure the first MAC address by default. */
1049         if (mlx5_get_mac(eth_dev, &mac.addr_bytes)) {
1050                 DRV_LOG(ERR,
1051                         "port %u cannot get MAC address, is mlx5_en"
1052                         " loaded? (errno: %s)",
1053                         eth_dev->data->port_id, strerror(rte_errno));
1054                 err = ENODEV;
1055                 goto error;
1056         }
1057         DRV_LOG(INFO,
1058                 "port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
1059                 eth_dev->data->port_id,
1060                 mac.addr_bytes[0], mac.addr_bytes[1],
1061                 mac.addr_bytes[2], mac.addr_bytes[3],
1062                 mac.addr_bytes[4], mac.addr_bytes[5]);
1063 #ifndef NDEBUG
1064         {
1065                 char ifname[IF_NAMESIZE];
1066
1067                 if (mlx5_get_ifname(eth_dev, &ifname) == 0)
1068                         DRV_LOG(DEBUG, "port %u ifname is \"%s\"",
1069                                 eth_dev->data->port_id, ifname);
1070                 else
1071                         DRV_LOG(DEBUG, "port %u ifname is unknown",
1072                                 eth_dev->data->port_id);
1073         }
1074 #endif
1075         /* Get actual MTU if possible. */
1076         err = mlx5_get_mtu(eth_dev, &priv->mtu);
1077         if (err) {
1078                 err = rte_errno;
1079                 goto error;
1080         }
1081         DRV_LOG(DEBUG, "port %u MTU is %u", eth_dev->data->port_id,
1082                 priv->mtu);
1083         /* Initialize burst functions to prevent crashes before link-up. */
1084         eth_dev->rx_pkt_burst = removed_rx_burst;
1085         eth_dev->tx_pkt_burst = removed_tx_burst;
1086         eth_dev->dev_ops = &mlx5_dev_ops;
1087         /* Register MAC address. */
1088         claim_zero(mlx5_mac_addr_add(eth_dev, &mac, 0, 0));
1089         if (vf && config.vf_nl_en)
1090                 mlx5_nl_mac_addr_sync(eth_dev);
1091         TAILQ_INIT(&priv->flows);
1092         TAILQ_INIT(&priv->ctrl_flows);
1093         /* Hint libmlx5 to use PMD allocator for data plane resources */
1094         struct mlx5dv_ctx_allocators alctr = {
1095                 .alloc = &mlx5_alloc_verbs_buf,
1096                 .free = &mlx5_free_verbs_buf,
1097                 .data = priv,
1098         };
1099         mlx5_glue->dv_set_context_attr(ctx, MLX5DV_CTX_ATTR_BUF_ALLOCATORS,
1100                                        (void *)((uintptr_t)&alctr));
1101         /* Bring Ethernet device up. */
1102         DRV_LOG(DEBUG, "port %u forcing Ethernet interface up",
1103                 eth_dev->data->port_id);
1104         mlx5_set_link_up(eth_dev);
1105         /*
1106          * Even though the interrupt handler is not installed yet,
1107          * interrupts will still trigger on the asyn_fd from
1108          * Verbs context returned by ibv_open_device().
1109          */
1110         mlx5_link_update(eth_dev, 0);
1111         /* Store device configuration on private structure. */
1112         priv->config = config;
1113         /* Create drop queue. */
1114         err = mlx5_flow_create_drop_queue(eth_dev);
1115         if (err) {
1116                 DRV_LOG(ERR, "port %u drop queue allocation failed: %s",
1117                         eth_dev->data->port_id, strerror(rte_errno));
1118                 err = rte_errno;
1119                 goto error;
1120         }
1121         /* Supported Verbs flow priority number detection. */
1122         if (verb_priorities == 0)
1123                 verb_priorities = mlx5_get_max_verbs_prio(eth_dev);
1124         if (verb_priorities < MLX5_VERBS_FLOW_PRIO_8) {
1125                 DRV_LOG(ERR, "port %u wrong Verbs flow priorities: %u",
1126                         eth_dev->data->port_id, verb_priorities);
1127                 err = ENOTSUP;
1128                 goto error;
1129         }
1130         priv->config.max_verbs_prio = verb_priorities;
1131         /*
1132          * Once the device is added to the list of memory event
1133          * callback, its global MR cache table cannot be expanded
1134          * on the fly because of deadlock. If it overflows, lookup
1135          * should be done by searching MR list linearly, which is slow.
1136          */
1137         err = mlx5_mr_btree_init(&priv->mr.cache,
1138                                  MLX5_MR_BTREE_CACHE_N * 2,
1139                                  eth_dev->device->numa_node);
1140         if (err) {
1141                 err = rte_errno;
1142                 goto error;
1143         }
1144         /* Add device to memory callback list. */
1145         rte_rwlock_write_lock(&mlx5_shared_data->mem_event_rwlock);
1146         LIST_INSERT_HEAD(&mlx5_shared_data->mem_event_cb_list,
1147                          priv, mem_event_cb);
1148         rte_rwlock_write_unlock(&mlx5_shared_data->mem_event_rwlock);
1149         return eth_dev;
1150 error:
1151         if (priv) {
1152                 if (priv->nl_socket_route >= 0)
1153                         close(priv->nl_socket_route);
1154                 if (priv->nl_socket_rdma >= 0)
1155                         close(priv->nl_socket_rdma);
1156                 if (own_domain_id)
1157                         claim_zero(rte_eth_switch_domain_free(priv->domain_id));
1158                 rte_free(priv);
1159         }
1160         if (pd)
1161                 claim_zero(mlx5_glue->dealloc_pd(pd));
1162         if (eth_dev)
1163                 rte_eth_dev_release_port(eth_dev);
1164         if (ctx)
1165                 claim_zero(mlx5_glue->close_device(ctx));
1166         assert(err > 0);
1167         rte_errno = err;
1168         return NULL;
1169 }
1170
1171 /** Data associated with devices to spawn. */
1172 struct mlx5_dev_spawn_data {
1173         unsigned int ifindex; /**< Network interface index. */
1174         struct mlx5_switch_info info; /**< Switch information. */
1175         struct ibv_device *ibv_dev; /**< Associated IB device. */
1176         struct rte_eth_dev *eth_dev; /**< Associated Ethernet device. */
1177 };
1178
1179 /**
1180  * Comparison callback to sort device data.
1181  *
1182  * This is meant to be used with qsort().
1183  *
1184  * @param a[in]
1185  *   Pointer to pointer to first data object.
1186  * @param b[in]
1187  *   Pointer to pointer to second data object.
1188  *
1189  * @return
1190  *   0 if both objects are equal, less than 0 if the first argument is less
1191  *   than the second, greater than 0 otherwise.
1192  */
1193 static int
1194 mlx5_dev_spawn_data_cmp(const void *a, const void *b)
1195 {
1196         const struct mlx5_switch_info *si_a =
1197                 &((const struct mlx5_dev_spawn_data *)a)->info;
1198         const struct mlx5_switch_info *si_b =
1199                 &((const struct mlx5_dev_spawn_data *)b)->info;
1200         int ret;
1201
1202         /* Master device first. */
1203         ret = si_b->master - si_a->master;
1204         if (ret)
1205                 return ret;
1206         /* Then representor devices. */
1207         ret = si_b->representor - si_a->representor;
1208         if (ret)
1209                 return ret;
1210         /* Unidentified devices come last in no specific order. */
1211         if (!si_a->representor)
1212                 return 0;
1213         /* Order representors by name. */
1214         return si_a->port_name - si_b->port_name;
1215 }
1216
1217 /**
1218  * DPDK callback to register a PCI device.
1219  *
1220  * This function spawns Ethernet devices out of a given PCI device.
1221  *
1222  * @param[in] pci_drv
1223  *   PCI driver structure (mlx5_driver).
1224  * @param[in] pci_dev
1225  *   PCI device information.
1226  *
1227  * @return
1228  *   0 on success, a negative errno value otherwise and rte_errno is set.
1229  */
1230 static int
1231 mlx5_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1232                struct rte_pci_device *pci_dev)
1233 {
1234         struct ibv_device **ibv_list;
1235         unsigned int n = 0;
1236         int vf;
1237         int ret;
1238
1239         assert(pci_drv == &mlx5_driver);
1240         errno = 0;
1241         ibv_list = mlx5_glue->get_device_list(&ret);
1242         if (!ibv_list) {
1243                 rte_errno = errno ? errno : ENOSYS;
1244                 DRV_LOG(ERR, "cannot list devices, is ib_uverbs loaded?");
1245                 return -rte_errno;
1246         }
1247
1248         struct ibv_device *ibv_match[ret + 1];
1249
1250         while (ret-- > 0) {
1251                 struct rte_pci_addr pci_addr;
1252
1253                 DRV_LOG(DEBUG, "checking device \"%s\"", ibv_list[ret]->name);
1254                 if (mlx5_ibv_device_to_pci_addr(ibv_list[ret], &pci_addr))
1255                         continue;
1256                 if (pci_dev->addr.domain != pci_addr.domain ||
1257                     pci_dev->addr.bus != pci_addr.bus ||
1258                     pci_dev->addr.devid != pci_addr.devid ||
1259                     pci_dev->addr.function != pci_addr.function)
1260                         continue;
1261                 DRV_LOG(INFO, "PCI information matches for device \"%s\"",
1262                         ibv_list[ret]->name);
1263                 ibv_match[n++] = ibv_list[ret];
1264         }
1265         ibv_match[n] = NULL;
1266
1267         struct mlx5_dev_spawn_data list[n];
1268         int nl_route = n ? mlx5_nl_init(0, NETLINK_ROUTE) : -1;
1269         int nl_rdma = n ? mlx5_nl_init(0, NETLINK_RDMA) : -1;
1270         unsigned int i;
1271         unsigned int u;
1272
1273         /*
1274          * The existence of several matching entries (n > 1) means port
1275          * representors have been instantiated. No existing Verbs call nor
1276          * /sys entries can tell them apart, this can only be done through
1277          * Netlink calls assuming kernel drivers are recent enough to
1278          * support them.
1279          *
1280          * In the event of identification failure through Netlink, either:
1281          *
1282          * 1. No device matches (n == 0), complain and bail out.
1283          * 2. A single IB device matches (n == 1) and is not a representor,
1284          *    assume no switch support.
1285          * 3. Otherwise no safe assumptions can be made; complain louder and
1286          *    bail out.
1287          */
1288         for (i = 0; i != n; ++i) {
1289                 list[i].ibv_dev = ibv_match[i];
1290                 list[i].eth_dev = NULL;
1291                 if (nl_rdma < 0)
1292                         list[i].ifindex = 0;
1293                 else
1294                         list[i].ifindex = mlx5_nl_ifindex
1295                                 (nl_rdma, list[i].ibv_dev->name);
1296                 if (nl_route < 0 ||
1297                     !list[i].ifindex ||
1298                     mlx5_nl_switch_info(nl_route, list[i].ifindex,
1299                                         &list[i].info)) {
1300                         list[i].ifindex = 0;
1301                         memset(&list[i].info, 0, sizeof(list[i].info));
1302                         continue;
1303                 }
1304         }
1305         if (nl_rdma >= 0)
1306                 close(nl_rdma);
1307         if (nl_route >= 0)
1308                 close(nl_route);
1309         /* Count unidentified devices. */
1310         for (u = 0, i = 0; i != n; ++i)
1311                 if (!list[i].info.master && !list[i].info.representor)
1312                         ++u;
1313         if (u) {
1314                 if (n == 1 && u == 1) {
1315                         /* Case #2. */
1316                         DRV_LOG(INFO, "no switch support detected");
1317                 } else {
1318                         /* Case #3. */
1319                         DRV_LOG(ERR,
1320                                 "unable to tell which of the matching devices"
1321                                 " is the master (lack of kernel support?)");
1322                         n = 0;
1323                 }
1324         }
1325         /*
1326          * Sort list to probe devices in natural order for users convenience
1327          * (i.e. master first, then representors from lowest to highest ID).
1328          */
1329         if (n)
1330                 qsort(list, n, sizeof(*list), mlx5_dev_spawn_data_cmp);
1331         switch (pci_dev->id.device_id) {
1332         case PCI_DEVICE_ID_MELLANOX_CONNECTX4VF:
1333         case PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF:
1334         case PCI_DEVICE_ID_MELLANOX_CONNECTX5VF:
1335         case PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF:
1336                 vf = 1;
1337                 break;
1338         default:
1339                 vf = 0;
1340         }
1341         for (i = 0; i != n; ++i) {
1342                 uint32_t restore;
1343
1344                 list[i].eth_dev = mlx5_dev_spawn
1345                         (&pci_dev->device, list[i].ibv_dev, vf, &list[i].info);
1346                 if (!list[i].eth_dev)
1347                         break;
1348                 restore = list[i].eth_dev->data->dev_flags;
1349                 rte_eth_copy_pci_info(list[i].eth_dev, pci_dev);
1350                 /* Restore non-PCI flags cleared by the above call. */
1351                 list[i].eth_dev->data->dev_flags |= restore;
1352                 rte_eth_dev_probing_finish(list[i].eth_dev);
1353         }
1354         mlx5_glue->free_device_list(ibv_list);
1355         if (!n) {
1356                 DRV_LOG(WARNING,
1357                         "no Verbs device matches PCI device " PCI_PRI_FMT ","
1358                         " are kernel drivers loaded?",
1359                         pci_dev->addr.domain, pci_dev->addr.bus,
1360                         pci_dev->addr.devid, pci_dev->addr.function);
1361                 rte_errno = ENOENT;
1362                 ret = -rte_errno;
1363         } else if (i != n) {
1364                 DRV_LOG(ERR,
1365                         "probe of PCI device " PCI_PRI_FMT " aborted after"
1366                         " encountering an error: %s",
1367                         pci_dev->addr.domain, pci_dev->addr.bus,
1368                         pci_dev->addr.devid, pci_dev->addr.function,
1369                         strerror(rte_errno));
1370                 ret = -rte_errno;
1371                 /* Roll back. */
1372                 while (i--) {
1373                         mlx5_dev_close(list[i].eth_dev);
1374                         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1375                                 rte_free(list[i].eth_dev->data->dev_private);
1376                         claim_zero(rte_eth_dev_release_port(list[i].eth_dev));
1377                 }
1378                 /* Restore original error. */
1379                 rte_errno = -ret;
1380         } else {
1381                 ret = 0;
1382         }
1383         return ret;
1384 }
1385
1386 static const struct rte_pci_id mlx5_pci_id_map[] = {
1387         {
1388                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1389                                PCI_DEVICE_ID_MELLANOX_CONNECTX4)
1390         },
1391         {
1392                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1393                                PCI_DEVICE_ID_MELLANOX_CONNECTX4VF)
1394         },
1395         {
1396                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1397                                PCI_DEVICE_ID_MELLANOX_CONNECTX4LX)
1398         },
1399         {
1400                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1401                                PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF)
1402         },
1403         {
1404                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1405                                PCI_DEVICE_ID_MELLANOX_CONNECTX5)
1406         },
1407         {
1408                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1409                                PCI_DEVICE_ID_MELLANOX_CONNECTX5VF)
1410         },
1411         {
1412                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1413                                PCI_DEVICE_ID_MELLANOX_CONNECTX5EX)
1414         },
1415         {
1416                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1417                                PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF)
1418         },
1419         {
1420                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1421                                PCI_DEVICE_ID_MELLANOX_CONNECTX5BF)
1422         },
1423         {
1424                 .vendor_id = 0
1425         }
1426 };
1427
1428 static struct rte_pci_driver mlx5_driver = {
1429         .driver = {
1430                 .name = MLX5_DRIVER_NAME
1431         },
1432         .id_table = mlx5_pci_id_map,
1433         .probe = mlx5_pci_probe,
1434         .drv_flags = RTE_PCI_DRV_INTR_LSC | RTE_PCI_DRV_INTR_RMV,
1435 };
1436
1437 #ifdef RTE_LIBRTE_MLX5_DLOPEN_DEPS
1438
1439 /**
1440  * Suffix RTE_EAL_PMD_PATH with "-glue".
1441  *
1442  * This function performs a sanity check on RTE_EAL_PMD_PATH before
1443  * suffixing its last component.
1444  *
1445  * @param buf[out]
1446  *   Output buffer, should be large enough otherwise NULL is returned.
1447  * @param size
1448  *   Size of @p out.
1449  *
1450  * @return
1451  *   Pointer to @p buf or @p NULL in case suffix cannot be appended.
1452  */
1453 static char *
1454 mlx5_glue_path(char *buf, size_t size)
1455 {
1456         static const char *const bad[] = { "/", ".", "..", NULL };
1457         const char *path = RTE_EAL_PMD_PATH;
1458         size_t len = strlen(path);
1459         size_t off;
1460         int i;
1461
1462         while (len && path[len - 1] == '/')
1463                 --len;
1464         for (off = len; off && path[off - 1] != '/'; --off)
1465                 ;
1466         for (i = 0; bad[i]; ++i)
1467                 if (!strncmp(path + off, bad[i], (int)(len - off)))
1468                         goto error;
1469         i = snprintf(buf, size, "%.*s-glue", (int)len, path);
1470         if (i == -1 || (size_t)i >= size)
1471                 goto error;
1472         return buf;
1473 error:
1474         DRV_LOG(ERR,
1475                 "unable to append \"-glue\" to last component of"
1476                 " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"),"
1477                 " please re-configure DPDK");
1478         return NULL;
1479 }
1480
1481 /**
1482  * Initialization routine for run-time dependency on rdma-core.
1483  */
1484 static int
1485 mlx5_glue_init(void)
1486 {
1487         char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
1488         const char *path[] = {
1489                 /*
1490                  * A basic security check is necessary before trusting
1491                  * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
1492                  */
1493                 (geteuid() == getuid() && getegid() == getgid() ?
1494                  getenv("MLX5_GLUE_PATH") : NULL),
1495                 /*
1496                  * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
1497                  * variant, otherwise let dlopen() look up libraries on its
1498                  * own.
1499                  */
1500                 (*RTE_EAL_PMD_PATH ?
1501                  mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
1502         };
1503         unsigned int i = 0;
1504         void *handle = NULL;
1505         void **sym;
1506         const char *dlmsg;
1507
1508         while (!handle && i != RTE_DIM(path)) {
1509                 const char *end;
1510                 size_t len;
1511                 int ret;
1512
1513                 if (!path[i]) {
1514                         ++i;
1515                         continue;
1516                 }
1517                 end = strpbrk(path[i], ":;");
1518                 if (!end)
1519                         end = path[i] + strlen(path[i]);
1520                 len = end - path[i];
1521                 ret = 0;
1522                 do {
1523                         char name[ret + 1];
1524
1525                         ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
1526                                        (int)len, path[i],
1527                                        (!len || *(end - 1) == '/') ? "" : "/");
1528                         if (ret == -1)
1529                                 break;
1530                         if (sizeof(name) != (size_t)ret + 1)
1531                                 continue;
1532                         DRV_LOG(DEBUG, "looking for rdma-core glue as \"%s\"",
1533                                 name);
1534                         handle = dlopen(name, RTLD_LAZY);
1535                         break;
1536                 } while (1);
1537                 path[i] = end + 1;
1538                 if (!*end)
1539                         ++i;
1540         }
1541         if (!handle) {
1542                 rte_errno = EINVAL;
1543                 dlmsg = dlerror();
1544                 if (dlmsg)
1545                         DRV_LOG(WARNING, "cannot load glue library: %s", dlmsg);
1546                 goto glue_error;
1547         }
1548         sym = dlsym(handle, "mlx5_glue");
1549         if (!sym || !*sym) {
1550                 rte_errno = EINVAL;
1551                 dlmsg = dlerror();
1552                 if (dlmsg)
1553                         DRV_LOG(ERR, "cannot resolve glue symbol: %s", dlmsg);
1554                 goto glue_error;
1555         }
1556         mlx5_glue = *sym;
1557         return 0;
1558 glue_error:
1559         if (handle)
1560                 dlclose(handle);
1561         DRV_LOG(WARNING,
1562                 "cannot initialize PMD due to missing run-time dependency on"
1563                 " rdma-core libraries (libibverbs, libmlx5)");
1564         return -rte_errno;
1565 }
1566
1567 #endif
1568
1569 /**
1570  * Driver initialization routine.
1571  */
1572 RTE_INIT(rte_mlx5_pmd_init)
1573 {
1574         /* Initialize driver log type. */
1575         mlx5_logtype = rte_log_register("pmd.net.mlx5");
1576         if (mlx5_logtype >= 0)
1577                 rte_log_set_level(mlx5_logtype, RTE_LOG_NOTICE);
1578
1579         /* Build the static tables for Verbs conversion. */
1580         mlx5_set_ptype_table();
1581         mlx5_set_cksum_table();
1582         mlx5_set_swp_types_table();
1583         /*
1584          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
1585          * huge pages. Calling ibv_fork_init() during init allows
1586          * applications to use fork() safely for purposes other than
1587          * using this PMD, which is not supported in forked processes.
1588          */
1589         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
1590         /* Match the size of Rx completion entry to the size of a cacheline. */
1591         if (RTE_CACHE_LINE_SIZE == 128)
1592                 setenv("MLX5_CQE_SIZE", "128", 0);
1593         /*
1594          * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
1595          * cleanup all the Verbs resources even when the device was removed.
1596          */
1597         setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
1598 #ifdef RTE_LIBRTE_MLX5_DLOPEN_DEPS
1599         if (mlx5_glue_init())
1600                 return;
1601         assert(mlx5_glue);
1602 #endif
1603 #ifndef NDEBUG
1604         /* Glue structure must not contain any NULL pointers. */
1605         {
1606                 unsigned int i;
1607
1608                 for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
1609                         assert(((const void *const *)mlx5_glue)[i]);
1610         }
1611 #endif
1612         if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
1613                 DRV_LOG(ERR,
1614                         "rdma-core glue \"%s\" mismatch: \"%s\" is required",
1615                         mlx5_glue->version, MLX5_GLUE_VERSION);
1616                 return;
1617         }
1618         mlx5_glue->fork_init();
1619         rte_pci_register(&mlx5_driver);
1620 }
1621
1622 RTE_PMD_EXPORT_NAME(net_mlx5, __COUNTER__);
1623 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5, mlx5_pci_id_map);
1624 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5, "* ib_uverbs & mlx5_core & mlx5_ib");