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