1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox.
18 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
20 #pragma GCC diagnostic ignored "-Wpedantic"
22 #include <infiniband/verbs.h>
24 #pragma GCC diagnostic error "-Wpedantic"
27 #include <rte_malloc.h>
28 #include <rte_ethdev_driver.h>
29 #include <rte_ethdev_pci.h>
31 #include <rte_bus_pci.h>
32 #include <rte_common.h>
33 #include <rte_config.h>
34 #include <rte_eal_memconfig.h>
35 #include <rte_kvargs.h>
38 #include "mlx5_utils.h"
39 #include "mlx5_rxtx.h"
40 #include "mlx5_autoconf.h"
41 #include "mlx5_defs.h"
42 #include "mlx5_glue.h"
44 /* Device parameter to enable RX completion queue compression. */
45 #define MLX5_RXQ_CQE_COMP_EN "rxq_cqe_comp_en"
47 /* Device parameter to configure inline send. */
48 #define MLX5_TXQ_INLINE "txq_inline"
51 * Device parameter to configure the number of TX queues threshold for
52 * enabling inline send.
54 #define MLX5_TXQS_MIN_INLINE "txqs_min_inline"
56 /* Device parameter to enable multi-packet send WQEs. */
57 #define MLX5_TXQ_MPW_EN "txq_mpw_en"
59 /* Device parameter to include 2 dsegs in the title WQEBB. */
60 #define MLX5_TXQ_MPW_HDR_DSEG_EN "txq_mpw_hdr_dseg_en"
62 /* Device parameter to limit the size of inlining packet. */
63 #define MLX5_TXQ_MAX_INLINE_LEN "txq_max_inline_len"
65 /* Device parameter to enable hardware Tx vector. */
66 #define MLX5_TX_VEC_EN "tx_vec_en"
68 /* Device parameter to enable hardware Rx vector. */
69 #define MLX5_RX_VEC_EN "rx_vec_en"
71 #ifndef HAVE_IBV_MLX5_MOD_MPW
72 #define MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED (1 << 2)
73 #define MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW (1 << 3)
76 #ifndef HAVE_IBV_MLX5_MOD_CQE_128B_COMP
77 #define MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP (1 << 4)
81 * Retrieve integer value from environment variable.
84 * Environment variable name.
87 * Integer value, 0 if the variable is not set.
90 mlx5_getenv_int(const char *name)
92 const char *val = getenv(name);
100 * Verbs callback to allocate a memory. This function should allocate the space
101 * according to the size provided residing inside a huge page.
102 * Please note that all allocation must respect the alignment from libmlx5
103 * (i.e. currently sysconf(_SC_PAGESIZE)).
106 * The size in bytes of the memory to allocate.
108 * A pointer to the callback data.
111 * a pointer to the allocate space.
114 mlx5_alloc_verbs_buf(size_t size, void *data)
116 struct priv *priv = data;
118 size_t alignment = sysconf(_SC_PAGESIZE);
119 unsigned int socket = SOCKET_ID_ANY;
121 if (priv->verbs_alloc_ctx.type == MLX5_VERBS_ALLOC_TYPE_TX_QUEUE) {
122 const struct mlx5_txq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
124 socket = ctrl->socket;
125 } else if (priv->verbs_alloc_ctx.type ==
126 MLX5_VERBS_ALLOC_TYPE_RX_QUEUE) {
127 const struct mlx5_rxq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
129 socket = ctrl->socket;
131 assert(data != NULL);
132 ret = rte_malloc_socket(__func__, size, alignment, socket);
133 DEBUG("Extern alloc size: %lu, align: %lu: %p", size, alignment, ret);
138 * Verbs callback to free a memory.
141 * A pointer to the memory to free.
143 * A pointer to the callback data.
146 mlx5_free_verbs_buf(void *ptr, void *data __rte_unused)
148 assert(data != NULL);
149 DEBUG("Extern free request: %p", ptr);
154 * DPDK callback to close the device.
156 * Destroy all queues and objects, free memory.
159 * Pointer to Ethernet device structure.
162 mlx5_dev_close(struct rte_eth_dev *dev)
164 struct priv *priv = dev->data->dev_private;
168 DEBUG("%p: closing device \"%s\"",
170 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
171 /* In case mlx5_dev_stop() has not been called. */
172 mlx5_dev_interrupt_handler_uninstall(dev);
173 mlx5_traffic_disable(dev);
174 /* Prevent crashes when queues are still in use. */
175 dev->rx_pkt_burst = removed_rx_burst;
176 dev->tx_pkt_burst = removed_tx_burst;
177 if (priv->rxqs != NULL) {
178 /* XXX race condition if mlx5_rx_burst() is still running. */
180 for (i = 0; (i != priv->rxqs_n); ++i)
181 mlx5_rxq_release(dev, i);
185 if (priv->txqs != NULL) {
186 /* XXX race condition if mlx5_tx_burst() is still running. */
188 for (i = 0; (i != priv->txqs_n); ++i)
189 mlx5_txq_release(dev, i);
193 if (priv->pd != NULL) {
194 assert(priv->ctx != NULL);
195 claim_zero(mlx5_glue->dealloc_pd(priv->pd));
196 claim_zero(mlx5_glue->close_device(priv->ctx));
198 assert(priv->ctx == NULL);
199 if (priv->rss_conf.rss_key != NULL)
200 rte_free(priv->rss_conf.rss_key);
201 if (priv->reta_idx != NULL)
202 rte_free(priv->reta_idx);
203 if (priv->primary_socket)
204 mlx5_socket_uninit(dev);
205 ret = mlx5_hrxq_ibv_verify(dev);
207 WARN("%p: some Hash Rx queue still remain", (void *)dev);
208 ret = mlx5_ind_table_ibv_verify(dev);
210 WARN("%p: some Indirection table still remain", (void *)dev);
211 ret = mlx5_rxq_ibv_verify(dev);
213 WARN("%p: some Verbs Rx queue still remain", (void *)dev);
214 ret = mlx5_rxq_verify(dev);
216 WARN("%p: some Rx Queues still remain", (void *)dev);
217 ret = mlx5_txq_ibv_verify(dev);
219 WARN("%p: some Verbs Tx queue still remain", (void *)dev);
220 ret = mlx5_txq_verify(dev);
222 WARN("%p: some Tx Queues still remain", (void *)dev);
223 ret = mlx5_flow_verify(dev);
225 WARN("%p: some flows still remain", (void *)dev);
226 ret = mlx5_mr_verify(dev);
228 WARN("%p: some Memory Region still remain", (void *)dev);
229 memset(priv, 0, sizeof(*priv));
232 const struct eth_dev_ops mlx5_dev_ops = {
233 .dev_configure = mlx5_dev_configure,
234 .dev_start = mlx5_dev_start,
235 .dev_stop = mlx5_dev_stop,
236 .dev_set_link_down = mlx5_set_link_down,
237 .dev_set_link_up = mlx5_set_link_up,
238 .dev_close = mlx5_dev_close,
239 .promiscuous_enable = mlx5_promiscuous_enable,
240 .promiscuous_disable = mlx5_promiscuous_disable,
241 .allmulticast_enable = mlx5_allmulticast_enable,
242 .allmulticast_disable = mlx5_allmulticast_disable,
243 .link_update = mlx5_link_update,
244 .stats_get = mlx5_stats_get,
245 .stats_reset = mlx5_stats_reset,
246 .xstats_get = mlx5_xstats_get,
247 .xstats_reset = mlx5_xstats_reset,
248 .xstats_get_names = mlx5_xstats_get_names,
249 .dev_infos_get = mlx5_dev_infos_get,
250 .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
251 .vlan_filter_set = mlx5_vlan_filter_set,
252 .rx_queue_setup = mlx5_rx_queue_setup,
253 .tx_queue_setup = mlx5_tx_queue_setup,
254 .rx_queue_release = mlx5_rx_queue_release,
255 .tx_queue_release = mlx5_tx_queue_release,
256 .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
257 .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
258 .mac_addr_remove = mlx5_mac_addr_remove,
259 .mac_addr_add = mlx5_mac_addr_add,
260 .mac_addr_set = mlx5_mac_addr_set,
261 .mtu_set = mlx5_dev_set_mtu,
262 .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
263 .vlan_offload_set = mlx5_vlan_offload_set,
264 .reta_update = mlx5_dev_rss_reta_update,
265 .reta_query = mlx5_dev_rss_reta_query,
266 .rss_hash_update = mlx5_rss_hash_update,
267 .rss_hash_conf_get = mlx5_rss_hash_conf_get,
268 .filter_ctrl = mlx5_dev_filter_ctrl,
269 .rx_descriptor_status = mlx5_rx_descriptor_status,
270 .tx_descriptor_status = mlx5_tx_descriptor_status,
271 .rx_queue_intr_enable = mlx5_rx_intr_enable,
272 .rx_queue_intr_disable = mlx5_rx_intr_disable,
273 .is_removed = mlx5_is_removed,
276 static const struct eth_dev_ops mlx5_dev_sec_ops = {
277 .stats_get = mlx5_stats_get,
278 .stats_reset = mlx5_stats_reset,
279 .xstats_get = mlx5_xstats_get,
280 .xstats_reset = mlx5_xstats_reset,
281 .xstats_get_names = mlx5_xstats_get_names,
282 .dev_infos_get = mlx5_dev_infos_get,
283 .rx_descriptor_status = mlx5_rx_descriptor_status,
284 .tx_descriptor_status = mlx5_tx_descriptor_status,
287 /* Available operators in flow isolated mode. */
288 const struct eth_dev_ops mlx5_dev_ops_isolate = {
289 .dev_configure = mlx5_dev_configure,
290 .dev_start = mlx5_dev_start,
291 .dev_stop = mlx5_dev_stop,
292 .dev_set_link_down = mlx5_set_link_down,
293 .dev_set_link_up = mlx5_set_link_up,
294 .dev_close = mlx5_dev_close,
295 .link_update = mlx5_link_update,
296 .stats_get = mlx5_stats_get,
297 .stats_reset = mlx5_stats_reset,
298 .xstats_get = mlx5_xstats_get,
299 .xstats_reset = mlx5_xstats_reset,
300 .xstats_get_names = mlx5_xstats_get_names,
301 .dev_infos_get = mlx5_dev_infos_get,
302 .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
303 .vlan_filter_set = mlx5_vlan_filter_set,
304 .rx_queue_setup = mlx5_rx_queue_setup,
305 .tx_queue_setup = mlx5_tx_queue_setup,
306 .rx_queue_release = mlx5_rx_queue_release,
307 .tx_queue_release = mlx5_tx_queue_release,
308 .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
309 .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
310 .mac_addr_remove = mlx5_mac_addr_remove,
311 .mac_addr_add = mlx5_mac_addr_add,
312 .mac_addr_set = mlx5_mac_addr_set,
313 .mtu_set = mlx5_dev_set_mtu,
314 .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
315 .vlan_offload_set = mlx5_vlan_offload_set,
316 .filter_ctrl = mlx5_dev_filter_ctrl,
317 .rx_descriptor_status = mlx5_rx_descriptor_status,
318 .tx_descriptor_status = mlx5_tx_descriptor_status,
319 .rx_queue_intr_enable = mlx5_rx_intr_enable,
320 .rx_queue_intr_disable = mlx5_rx_intr_disable,
321 .is_removed = mlx5_is_removed,
325 struct rte_pci_addr pci_addr; /* associated PCI address */
326 uint32_t ports; /* physical ports bitfield. */
330 * Get device index in mlx5_dev[] from PCI bus address.
332 * @param[in] pci_addr
333 * PCI bus address to look for.
336 * mlx5_dev[] index on success, -1 on failure.
339 mlx5_dev_idx(struct rte_pci_addr *pci_addr)
344 assert(pci_addr != NULL);
345 for (i = 0; (i != RTE_DIM(mlx5_dev)); ++i) {
346 if ((mlx5_dev[i].pci_addr.domain == pci_addr->domain) &&
347 (mlx5_dev[i].pci_addr.bus == pci_addr->bus) &&
348 (mlx5_dev[i].pci_addr.devid == pci_addr->devid) &&
349 (mlx5_dev[i].pci_addr.function == pci_addr->function))
351 if ((mlx5_dev[i].ports == 0) && (ret == -1))
358 * Verify and store value for device argument.
361 * Key argument to verify.
363 * Value associated with key.
368 * 0 on success, negative errno value on failure.
371 mlx5_args_check(const char *key, const char *val, void *opaque)
373 struct mlx5_dev_config *config = opaque;
377 tmp = strtoul(val, NULL, 0);
379 WARN("%s: \"%s\" is not a valid integer", key, val);
382 if (strcmp(MLX5_RXQ_CQE_COMP_EN, key) == 0) {
383 config->cqe_comp = !!tmp;
384 } else if (strcmp(MLX5_TXQ_INLINE, key) == 0) {
385 config->txq_inline = tmp;
386 } else if (strcmp(MLX5_TXQS_MIN_INLINE, key) == 0) {
387 config->txqs_inline = tmp;
388 } else if (strcmp(MLX5_TXQ_MPW_EN, key) == 0) {
389 config->mps = !!tmp ? config->mps : 0;
390 } else if (strcmp(MLX5_TXQ_MPW_HDR_DSEG_EN, key) == 0) {
391 config->mpw_hdr_dseg = !!tmp;
392 } else if (strcmp(MLX5_TXQ_MAX_INLINE_LEN, key) == 0) {
393 config->inline_max_packet_sz = tmp;
394 } else if (strcmp(MLX5_TX_VEC_EN, key) == 0) {
395 config->tx_vec_en = !!tmp;
396 } else if (strcmp(MLX5_RX_VEC_EN, key) == 0) {
397 config->rx_vec_en = !!tmp;
399 WARN("%s: unknown parameter", key);
406 * Parse device parameters.
409 * Pointer to device configuration structure.
411 * Device arguments structure.
414 * 0 on success, errno value on failure.
417 mlx5_args(struct mlx5_dev_config *config, struct rte_devargs *devargs)
419 const char **params = (const char *[]){
420 MLX5_RXQ_CQE_COMP_EN,
422 MLX5_TXQS_MIN_INLINE,
424 MLX5_TXQ_MPW_HDR_DSEG_EN,
425 MLX5_TXQ_MAX_INLINE_LEN,
430 struct rte_kvargs *kvlist;
436 /* Following UGLY cast is done to pass checkpatch. */
437 kvlist = rte_kvargs_parse(devargs->args, params);
440 /* Process parameters. */
441 for (i = 0; (params[i] != NULL); ++i) {
442 if (rte_kvargs_count(kvlist, params[i])) {
443 ret = rte_kvargs_process(kvlist, params[i],
444 mlx5_args_check, config);
446 rte_kvargs_free(kvlist);
451 rte_kvargs_free(kvlist);
455 static struct rte_pci_driver mlx5_driver;
458 * Reserved UAR address space for TXQ UAR(hw doorbell) mapping, process
459 * local resource used by both primary and secondary to avoid duplicate
461 * The space has to be available on both primary and secondary process,
462 * TXQ UAR maps to this area using fixed mmap w/o double check.
464 static void *uar_base;
467 * Reserve UAR address space for primary process.
470 * Pointer to Ethernet device.
473 * 0 on success, errno value on failure.
476 mlx5_uar_init_primary(struct rte_eth_dev *dev)
478 struct priv *priv = dev->data->dev_private;
479 void *addr = (void *)0;
481 const struct rte_mem_config *mcfg;
484 if (uar_base) { /* UAR address space mapped. */
485 priv->uar_base = uar_base;
488 /* find out lower bound of hugepage segments */
489 mcfg = rte_eal_get_configuration()->mem_config;
490 for (i = 0; i < RTE_MAX_MEMSEG && mcfg->memseg[i].addr; i++) {
492 addr = RTE_MIN(addr, mcfg->memseg[i].addr);
494 addr = mcfg->memseg[i].addr;
496 /* keep distance to hugepages to minimize potential conflicts. */
497 addr = RTE_PTR_SUB(addr, MLX5_UAR_OFFSET + MLX5_UAR_SIZE);
498 /* anonymous mmap, no real memory consumption. */
499 addr = mmap(addr, MLX5_UAR_SIZE,
500 PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
501 if (addr == MAP_FAILED) {
502 ERROR("Failed to reserve UAR address space, please adjust "
503 "MLX5_UAR_SIZE or try --base-virtaddr");
507 /* Accept either same addr or a new addr returned from mmap if target
510 INFO("Reserved UAR address space: %p", addr);
511 priv->uar_base = addr; /* for primary and secondary UAR re-mmap. */
512 uar_base = addr; /* process local, don't reserve again. */
517 * Reserve UAR address space for secondary process, align with
521 * Pointer to Ethernet device.
524 * 0 on success, errno value on failure.
527 mlx5_uar_init_secondary(struct rte_eth_dev *dev)
529 struct priv *priv = dev->data->dev_private;
533 assert(priv->uar_base);
534 if (uar_base) { /* already reserved. */
535 assert(uar_base == priv->uar_base);
538 /* anonymous mmap, no real memory consumption. */
539 addr = mmap(priv->uar_base, MLX5_UAR_SIZE,
540 PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
541 if (addr == MAP_FAILED) {
542 ERROR("UAR mmap failed: %p size: %llu",
543 priv->uar_base, MLX5_UAR_SIZE);
547 if (priv->uar_base != addr) {
548 ERROR("UAR address %p size %llu occupied, please adjust "
549 "MLX5_UAR_OFFSET or try EAL parameter --base-virtaddr",
550 priv->uar_base, MLX5_UAR_SIZE);
554 uar_base = addr; /* process local, don't reserve again */
555 INFO("Reserved UAR address space: %p", addr);
560 * DPDK callback to register a PCI device.
562 * This function creates an Ethernet device for each port of a given
566 * PCI driver structure (mlx5_driver).
568 * PCI device information.
571 * 0 on success, negative errno value on failure.
574 mlx5_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
575 struct rte_pci_device *pci_dev)
577 struct ibv_device **list;
578 struct ibv_device *ibv_dev;
580 struct ibv_context *attr_ctx = NULL;
581 struct ibv_device_attr_ex device_attr;
583 unsigned int cqe_comp;
584 unsigned int tunnel_en = 0;
587 struct mlx5dv_context attrs_out = {0};
588 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
589 struct ibv_counter_set_description cs_desc;
592 assert(pci_drv == &mlx5_driver);
593 /* Get mlx5_dev[] index. */
594 idx = mlx5_dev_idx(&pci_dev->addr);
596 ERROR("this driver cannot support any more adapters");
599 DEBUG("using driver device index %d", idx);
600 /* Save PCI address. */
601 mlx5_dev[idx].pci_addr = pci_dev->addr;
602 list = mlx5_glue->get_device_list(&i);
606 ERROR("cannot list devices, is ib_uverbs loaded?");
611 * For each listed device, check related sysfs entry against
612 * the provided PCI ID.
615 struct rte_pci_addr pci_addr;
618 DEBUG("checking device \"%s\"", list[i]->name);
619 if (mlx5_ibv_device_to_pci_addr(list[i], &pci_addr))
621 if ((pci_dev->addr.domain != pci_addr.domain) ||
622 (pci_dev->addr.bus != pci_addr.bus) ||
623 (pci_dev->addr.devid != pci_addr.devid) ||
624 (pci_dev->addr.function != pci_addr.function))
626 INFO("PCI information matches, using device \"%s\"",
628 attr_ctx = mlx5_glue->open_device(list[i]);
632 if (attr_ctx == NULL) {
633 mlx5_glue->free_device_list(list);
636 ERROR("cannot access device, is mlx5_ib loaded?");
639 ERROR("cannot use device, are drivers up to date?");
646 DEBUG("device opened");
648 * Multi-packet send is supported by ConnectX-4 Lx PF as well
649 * as all ConnectX-5 devices.
651 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
652 attrs_out.comp_mask |= MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS;
654 mlx5_glue->dv_query_device(attr_ctx, &attrs_out);
655 if (attrs_out.flags & MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED) {
656 if (attrs_out.flags & MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW) {
657 DEBUG("Enhanced MPW is supported");
658 mps = MLX5_MPW_ENHANCED;
660 DEBUG("MPW is supported");
664 DEBUG("MPW isn't supported");
665 mps = MLX5_MPW_DISABLED;
667 if (RTE_CACHE_LINE_SIZE == 128 &&
668 !(attrs_out.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP))
672 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
673 if (attrs_out.comp_mask & MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS) {
674 tunnel_en = ((attrs_out.tunnel_offloads_caps &
675 MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_VXLAN) &&
676 (attrs_out.tunnel_offloads_caps &
677 MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_GRE));
679 DEBUG("Tunnel offloading is %ssupported", tunnel_en ? "" : "not ");
681 WARN("Tunnel offloading disabled due to old OFED/rdma-core version");
683 if (mlx5_glue->query_device_ex(attr_ctx, NULL, &device_attr))
685 INFO("%u port(s) detected", device_attr.orig_attr.phys_port_cnt);
686 for (i = 0; i < device_attr.orig_attr.phys_port_cnt; i++) {
687 char name[RTE_ETH_NAME_MAX_LEN];
689 uint32_t port = i + 1; /* ports are indexed from one */
690 uint32_t test = (1 << i);
691 struct ibv_context *ctx = NULL;
692 struct ibv_port_attr port_attr;
693 struct ibv_pd *pd = NULL;
694 struct priv *priv = NULL;
695 struct rte_eth_dev *eth_dev = NULL;
696 struct ibv_device_attr_ex device_attr_ex;
697 struct ether_addr mac;
698 struct ibv_device_attr_ex device_attr;
699 struct mlx5_dev_config config = {
700 .cqe_comp = cqe_comp,
702 .tunnel_en = tunnel_en,
706 .txq_inline = MLX5_ARG_UNSET,
707 .txqs_inline = MLX5_ARG_UNSET,
708 .inline_max_packet_sz = MLX5_ARG_UNSET,
711 len = snprintf(name, sizeof(name), PCI_PRI_FMT,
712 pci_dev->addr.domain, pci_dev->addr.bus,
713 pci_dev->addr.devid, pci_dev->addr.function);
714 if (device_attr.orig_attr.phys_port_cnt > 1)
715 snprintf(name + len, sizeof(name), " port %u", i);
716 mlx5_dev[idx].ports |= test;
717 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
718 eth_dev = rte_eth_dev_attach_secondary(name);
719 if (eth_dev == NULL) {
720 ERROR("can not attach rte ethdev");
724 eth_dev->device = &pci_dev->device;
725 eth_dev->dev_ops = &mlx5_dev_sec_ops;
726 err = mlx5_uar_init_secondary(eth_dev);
731 /* Receive command fd from primary process */
732 err = mlx5_socket_connect(eth_dev);
737 /* Remap UAR for Tx queues. */
738 err = mlx5_tx_uar_remap(eth_dev, err);
742 * Ethdev pointer is still required as input since
743 * the primary device is not accessible from the
746 eth_dev->rx_pkt_burst =
747 mlx5_select_rx_function(eth_dev);
748 eth_dev->tx_pkt_burst =
749 mlx5_select_tx_function(eth_dev);
752 DEBUG("using port %u (%08" PRIx32 ")", port, test);
753 ctx = mlx5_glue->open_device(ibv_dev);
758 mlx5_glue->query_device_ex(ctx, NULL, &device_attr);
759 /* Check port status. */
760 err = mlx5_glue->query_port(ctx, port, &port_attr);
762 ERROR("port query failed: %s", strerror(err));
765 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
766 ERROR("port %d is not configured in Ethernet mode",
771 if (port_attr.state != IBV_PORT_ACTIVE)
772 DEBUG("port %d is not active: \"%s\" (%d)",
773 port, mlx5_glue->port_state_str(port_attr.state),
775 /* Allocate protection domain. */
776 pd = mlx5_glue->alloc_pd(ctx);
778 ERROR("PD allocation failure");
782 mlx5_dev[idx].ports |= test;
783 /* from rte_ethdev.c */
784 priv = rte_zmalloc("ethdev private structure",
786 RTE_CACHE_LINE_SIZE);
788 ERROR("priv allocation failure");
793 strncpy(priv->ibdev_path, priv->ctx->device->ibdev_path,
794 sizeof(priv->ibdev_path));
795 priv->device_attr = device_attr;
798 priv->mtu = ETHER_MTU;
799 err = mlx5_args(&config, pci_dev->device.devargs);
801 ERROR("failed to process device arguments: %s",
805 if (mlx5_glue->query_device_ex(ctx, NULL, &device_attr_ex)) {
806 ERROR("ibv_query_device_ex() failed");
809 config.hw_csum = !!(device_attr_ex.device_cap_flags_ex &
810 IBV_DEVICE_RAW_IP_CSUM);
811 DEBUG("checksum offloading is %ssupported",
812 (config.hw_csum ? "" : "not "));
813 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
814 config.flow_counter_en = !!(device_attr.max_counter_sets);
815 mlx5_glue->describe_counter_set(ctx, 0, &cs_desc);
816 DEBUG("counter type = %d, num of cs = %ld, attributes = %d",
817 cs_desc.counter_type, cs_desc.num_of_cs,
820 config.ind_table_max_size =
821 device_attr_ex.rss_caps.max_rwq_indirection_table_size;
822 /* Remove this check once DPDK supports larger/variable
823 * indirection tables. */
824 if (config.ind_table_max_size >
825 (unsigned int)ETH_RSS_RETA_SIZE_512)
826 config.ind_table_max_size = ETH_RSS_RETA_SIZE_512;
827 DEBUG("maximum RX indirection table size is %u",
828 config.ind_table_max_size);
829 config.hw_vlan_strip = !!(device_attr_ex.raw_packet_caps &
830 IBV_RAW_PACKET_CAP_CVLAN_STRIPPING);
831 DEBUG("VLAN stripping is %ssupported",
832 (config.hw_vlan_strip ? "" : "not "));
834 config.hw_fcs_strip = !!(device_attr_ex.raw_packet_caps &
835 IBV_RAW_PACKET_CAP_SCATTER_FCS);
836 DEBUG("FCS stripping configuration is %ssupported",
837 (config.hw_fcs_strip ? "" : "not "));
839 #ifdef HAVE_IBV_WQ_FLAG_RX_END_PADDING
840 config.hw_padding = !!device_attr_ex.rx_pad_end_addr_align;
842 DEBUG("hardware RX end alignment padding is %ssupported",
843 (config.hw_padding ? "" : "not "));
844 config.tso = ((device_attr_ex.tso_caps.max_tso > 0) &&
845 (device_attr_ex.tso_caps.supported_qpts &
846 (1 << IBV_QPT_RAW_PACKET)));
848 config.tso_max_payload_sz =
849 device_attr_ex.tso_caps.max_tso;
850 if (config.mps && !mps) {
851 ERROR("multi-packet send not supported on this device"
852 " (" MLX5_TXQ_MPW_EN ")");
857 config.mps == MLX5_MPW_ENHANCED ? "Enhanced " : "",
858 config.mps != MLX5_MPW_DISABLED ? "enabled" : "disabled");
859 if (config.cqe_comp && !cqe_comp) {
860 WARN("Rx CQE compression isn't supported");
863 eth_dev = rte_eth_dev_allocate(name);
864 if (eth_dev == NULL) {
865 ERROR("can not allocate rte ethdev");
869 eth_dev->data->dev_private = priv;
871 eth_dev->data->mac_addrs = priv->mac;
872 eth_dev->device = &pci_dev->device;
873 rte_eth_copy_pci_info(eth_dev, pci_dev);
874 eth_dev->device->driver = &mlx5_driver.driver;
875 err = mlx5_uar_init_primary(eth_dev);
878 /* Configure the first MAC address by default. */
879 if (mlx5_get_mac(eth_dev, &mac.addr_bytes)) {
880 ERROR("cannot get MAC address, is mlx5_en loaded?"
881 " (errno: %s)", strerror(errno));
885 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
887 mac.addr_bytes[0], mac.addr_bytes[1],
888 mac.addr_bytes[2], mac.addr_bytes[3],
889 mac.addr_bytes[4], mac.addr_bytes[5]);
892 char ifname[IF_NAMESIZE];
894 if (mlx5_get_ifname(eth_dev, &ifname) == 0)
895 DEBUG("port %u ifname is \"%s\"",
898 DEBUG("port %u ifname is unknown", priv->port);
901 /* Get actual MTU if possible. */
902 mlx5_get_mtu(eth_dev, &priv->mtu);
903 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
905 * Initialize burst functions to prevent crashes before link-up.
907 eth_dev->rx_pkt_burst = removed_rx_burst;
908 eth_dev->tx_pkt_burst = removed_tx_burst;
909 eth_dev->dev_ops = &mlx5_dev_ops;
910 /* Register MAC address. */
911 claim_zero(mlx5_mac_addr_add(eth_dev, &mac, 0, 0));
912 TAILQ_INIT(&priv->flows);
913 TAILQ_INIT(&priv->ctrl_flows);
914 /* Hint libmlx5 to use PMD allocator for data plane resources */
915 struct mlx5dv_ctx_allocators alctr = {
916 .alloc = &mlx5_alloc_verbs_buf,
917 .free = &mlx5_free_verbs_buf,
920 mlx5_glue->dv_set_context_attr(ctx,
921 MLX5DV_CTX_ATTR_BUF_ALLOCATORS,
922 (void *)((uintptr_t)&alctr));
923 /* Bring Ethernet device up. */
924 DEBUG("forcing Ethernet interface up");
925 mlx5_set_flags(eth_dev, ~IFF_UP, IFF_UP);
926 /* Store device configuration on private structure. */
927 priv->config = config;
933 claim_zero(mlx5_glue->dealloc_pd(pd));
935 claim_zero(mlx5_glue->close_device(ctx));
939 * XXX if something went wrong in the loop above, there is a resource
940 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
941 * long as the dpdk does not provide a way to deallocate a ethdev and a
942 * way to enumerate the registered ethdevs to free the previous ones.
944 /* no port found, complain */
945 if (!mlx5_dev[idx].ports) {
951 claim_zero(mlx5_glue->close_device(attr_ctx));
953 mlx5_glue->free_device_list(list);
958 static const struct rte_pci_id mlx5_pci_id_map[] = {
960 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
961 PCI_DEVICE_ID_MELLANOX_CONNECTX4)
964 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
965 PCI_DEVICE_ID_MELLANOX_CONNECTX4VF)
968 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
969 PCI_DEVICE_ID_MELLANOX_CONNECTX4LX)
972 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
973 PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF)
976 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
977 PCI_DEVICE_ID_MELLANOX_CONNECTX5)
980 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
981 PCI_DEVICE_ID_MELLANOX_CONNECTX5VF)
984 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
985 PCI_DEVICE_ID_MELLANOX_CONNECTX5EX)
988 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
989 PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF)
996 static struct rte_pci_driver mlx5_driver = {
998 .name = MLX5_DRIVER_NAME
1000 .id_table = mlx5_pci_id_map,
1001 .probe = mlx5_pci_probe,
1002 .drv_flags = RTE_PCI_DRV_INTR_LSC | RTE_PCI_DRV_INTR_RMV,
1005 #ifdef RTE_LIBRTE_MLX5_DLOPEN_DEPS
1008 * Suffix RTE_EAL_PMD_PATH with "-glue".
1010 * This function performs a sanity check on RTE_EAL_PMD_PATH before
1011 * suffixing its last component.
1014 * Output buffer, should be large enough otherwise NULL is returned.
1019 * Pointer to @p buf or @p NULL in case suffix cannot be appended.
1022 mlx5_glue_path(char *buf, size_t size)
1024 static const char *const bad[] = { "/", ".", "..", NULL };
1025 const char *path = RTE_EAL_PMD_PATH;
1026 size_t len = strlen(path);
1030 while (len && path[len - 1] == '/')
1032 for (off = len; off && path[off - 1] != '/'; --off)
1034 for (i = 0; bad[i]; ++i)
1035 if (!strncmp(path + off, bad[i], (int)(len - off)))
1037 i = snprintf(buf, size, "%.*s-glue", (int)len, path);
1038 if (i == -1 || (size_t)i >= size)
1042 ERROR("unable to append \"-glue\" to last component of"
1043 " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"),"
1044 " please re-configure DPDK");
1049 * Initialization routine for run-time dependency on rdma-core.
1052 mlx5_glue_init(void)
1054 char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
1055 const char *path[] = {
1057 * A basic security check is necessary before trusting
1058 * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
1060 (geteuid() == getuid() && getegid() == getgid() ?
1061 getenv("MLX5_GLUE_PATH") : NULL),
1063 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
1064 * variant, otherwise let dlopen() look up libraries on its
1067 (*RTE_EAL_PMD_PATH ?
1068 mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
1071 void *handle = NULL;
1075 while (!handle && i != RTE_DIM(path)) {
1084 end = strpbrk(path[i], ":;");
1086 end = path[i] + strlen(path[i]);
1087 len = end - path[i];
1092 ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
1094 (!len || *(end - 1) == '/') ? "" : "/");
1097 if (sizeof(name) != (size_t)ret + 1)
1099 DEBUG("looking for rdma-core glue as \"%s\"", name);
1100 handle = dlopen(name, RTLD_LAZY);
1111 WARN("cannot load glue library: %s", dlmsg);
1114 sym = dlsym(handle, "mlx5_glue");
1115 if (!sym || !*sym) {
1119 ERROR("cannot resolve glue symbol: %s", dlmsg);
1127 WARN("cannot initialize PMD due to missing run-time"
1128 " dependency on rdma-core libraries (libibverbs,"
1136 * Driver initialization routine.
1138 RTE_INIT(rte_mlx5_pmd_init);
1140 rte_mlx5_pmd_init(void)
1142 /* Build the static table for ptype conversion. */
1143 mlx5_set_ptype_table();
1145 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
1146 * huge pages. Calling ibv_fork_init() during init allows
1147 * applications to use fork() safely for purposes other than
1148 * using this PMD, which is not supported in forked processes.
1150 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
1151 /* Match the size of Rx completion entry to the size of a cacheline. */
1152 if (RTE_CACHE_LINE_SIZE == 128)
1153 setenv("MLX5_CQE_SIZE", "128", 0);
1154 #ifdef RTE_LIBRTE_MLX5_DLOPEN_DEPS
1155 if (mlx5_glue_init())
1160 /* Glue structure must not contain any NULL pointers. */
1164 for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
1165 assert(((const void *const *)mlx5_glue)[i]);
1168 if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
1169 ERROR("rdma-core glue \"%s\" mismatch: \"%s\" is required",
1170 mlx5_glue->version, MLX5_GLUE_VERSION);
1173 mlx5_glue->fork_init();
1174 rte_pci_register(&mlx5_driver);
1177 RTE_PMD_EXPORT_NAME(net_mlx5, __COUNTER__);
1178 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5, mlx5_pci_id_map);
1179 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5, "* ib_uverbs & mlx5_core & mlx5_ib");