4 * Copyright 2015 6WIND S.A.
5 * Copyright 2015 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
46 #pragma GCC diagnostic ignored "-pedantic"
48 #include <infiniband/verbs.h>
50 #pragma GCC diagnostic error "-pedantic"
53 /* DPDK headers don't like -pedantic. */
55 #pragma GCC diagnostic ignored "-pedantic"
57 #include <rte_malloc.h>
58 #include <rte_ethdev.h>
60 #include <rte_common.h>
61 #include <rte_kvargs.h>
63 #pragma GCC diagnostic error "-pedantic"
67 #include "mlx5_utils.h"
68 #include "mlx5_rxtx.h"
69 #include "mlx5_autoconf.h"
70 #include "mlx5_defs.h"
72 /* Device parameter to enable RX completion queue compression. */
73 #define MLX5_RXQ_CQE_COMP_EN "rxq_cqe_comp_en"
76 * Retrieve integer value from environment variable.
79 * Environment variable name.
82 * Integer value, 0 if the variable is not set.
85 mlx5_getenv_int(const char *name)
87 const char *val = getenv(name);
95 * DPDK callback to close the device.
97 * Destroy all queues and objects, free memory.
100 * Pointer to Ethernet device structure.
103 mlx5_dev_close(struct rte_eth_dev *dev)
105 struct priv *priv = mlx5_get_priv(dev);
109 DEBUG("%p: closing device \"%s\"",
111 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
112 /* In case mlx5_dev_stop() has not been called. */
113 priv_dev_interrupt_handler_uninstall(priv, dev);
114 priv_special_flow_disable_all(priv);
115 priv_mac_addrs_disable(priv);
116 priv_destroy_hash_rxqs(priv);
118 /* Remove flow director elements. */
119 priv_fdir_disable(priv);
120 priv_fdir_delete_filters_list(priv);
122 /* Prevent crashes when queues are still in use. */
123 dev->rx_pkt_burst = removed_rx_burst;
124 dev->tx_pkt_burst = removed_tx_burst;
125 if (priv->rxqs != NULL) {
126 /* XXX race condition if mlx5_rx_burst() is still running. */
128 for (i = 0; (i != priv->rxqs_n); ++i) {
129 struct rxq *rxq = (*priv->rxqs)[i];
130 struct rxq_ctrl *rxq_ctrl;
134 rxq_ctrl = container_of(rxq, struct rxq_ctrl, rxq);
135 (*priv->rxqs)[i] = NULL;
136 rxq_cleanup(rxq_ctrl);
142 if (priv->txqs != NULL) {
143 /* XXX race condition if mlx5_tx_burst() is still running. */
145 for (i = 0; (i != priv->txqs_n); ++i) {
146 struct txq *txq = (*priv->txqs)[i];
147 struct txq_ctrl *txq_ctrl;
151 txq_ctrl = container_of(txq, struct txq_ctrl, txq);
152 (*priv->txqs)[i] = NULL;
153 txq_cleanup(txq_ctrl);
159 if (priv->pd != NULL) {
160 assert(priv->ctx != NULL);
161 claim_zero(ibv_dealloc_pd(priv->pd));
162 claim_zero(ibv_close_device(priv->ctx));
164 assert(priv->ctx == NULL);
165 if (priv->rss_conf != NULL) {
166 for (i = 0; (i != hash_rxq_init_n); ++i)
167 rte_free((*priv->rss_conf)[i]);
168 rte_free(priv->rss_conf);
170 if (priv->reta_idx != NULL)
171 rte_free(priv->reta_idx);
173 memset(priv, 0, sizeof(*priv));
176 static const struct eth_dev_ops mlx5_dev_ops = {
177 .dev_configure = mlx5_dev_configure,
178 .dev_start = mlx5_dev_start,
179 .dev_stop = mlx5_dev_stop,
180 .dev_set_link_down = mlx5_set_link_down,
181 .dev_set_link_up = mlx5_set_link_up,
182 .dev_close = mlx5_dev_close,
183 .promiscuous_enable = mlx5_promiscuous_enable,
184 .promiscuous_disable = mlx5_promiscuous_disable,
185 .allmulticast_enable = mlx5_allmulticast_enable,
186 .allmulticast_disable = mlx5_allmulticast_disable,
187 .link_update = mlx5_link_update,
188 .stats_get = mlx5_stats_get,
189 .stats_reset = mlx5_stats_reset,
190 .dev_infos_get = mlx5_dev_infos_get,
191 .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
192 .vlan_filter_set = mlx5_vlan_filter_set,
193 .rx_queue_setup = mlx5_rx_queue_setup,
194 .tx_queue_setup = mlx5_tx_queue_setup,
195 .rx_queue_release = mlx5_rx_queue_release,
196 .tx_queue_release = mlx5_tx_queue_release,
197 .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
198 .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
199 .mac_addr_remove = mlx5_mac_addr_remove,
200 .mac_addr_add = mlx5_mac_addr_add,
201 .mac_addr_set = mlx5_mac_addr_set,
202 .mtu_set = mlx5_dev_set_mtu,
203 .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
204 .vlan_offload_set = mlx5_vlan_offload_set,
205 .reta_update = mlx5_dev_rss_reta_update,
206 .reta_query = mlx5_dev_rss_reta_query,
207 .rss_hash_update = mlx5_rss_hash_update,
208 .rss_hash_conf_get = mlx5_rss_hash_conf_get,
209 .filter_ctrl = mlx5_dev_filter_ctrl,
213 struct rte_pci_addr pci_addr; /* associated PCI address */
214 uint32_t ports; /* physical ports bitfield. */
218 * Get device index in mlx5_dev[] from PCI bus address.
220 * @param[in] pci_addr
221 * PCI bus address to look for.
224 * mlx5_dev[] index on success, -1 on failure.
227 mlx5_dev_idx(struct rte_pci_addr *pci_addr)
232 assert(pci_addr != NULL);
233 for (i = 0; (i != RTE_DIM(mlx5_dev)); ++i) {
234 if ((mlx5_dev[i].pci_addr.domain == pci_addr->domain) &&
235 (mlx5_dev[i].pci_addr.bus == pci_addr->bus) &&
236 (mlx5_dev[i].pci_addr.devid == pci_addr->devid) &&
237 (mlx5_dev[i].pci_addr.function == pci_addr->function))
239 if ((mlx5_dev[i].ports == 0) && (ret == -1))
246 * Verify and store value for device argument.
249 * Key argument to verify.
251 * Value associated with key.
256 * 0 on success, negative errno value on failure.
259 mlx5_args_check(const char *key, const char *val, void *opaque)
261 struct priv *priv = opaque;
265 tmp = strtoul(val, NULL, 0);
267 WARN("%s: \"%s\" is not a valid integer", key, val);
270 if (strcmp(MLX5_RXQ_CQE_COMP_EN, key) == 0) {
271 priv->cqe_comp = !!tmp;
273 WARN("%s: unknown parameter", key);
280 * Parse device parameters.
283 * Pointer to private structure.
285 * Device arguments structure.
288 * 0 on success, errno value on failure.
291 mlx5_args(struct priv *priv, struct rte_devargs *devargs)
293 const char **params = (const char *[]){
294 MLX5_RXQ_CQE_COMP_EN,
297 struct rte_kvargs *kvlist;
303 /* Following UGLY cast is done to pass checkpatch. */
304 kvlist = rte_kvargs_parse(devargs->args, params);
307 /* Process parameters. */
308 for (i = 0; (params[i] != NULL); ++i) {
309 if (rte_kvargs_count(kvlist, params[i])) {
310 ret = rte_kvargs_process(kvlist, params[i],
311 mlx5_args_check, priv);
316 rte_kvargs_free(kvlist);
320 static struct eth_driver mlx5_driver;
323 * DPDK callback to register a PCI device.
325 * This function creates an Ethernet device for each port of a given
329 * PCI driver structure (mlx5_driver).
331 * PCI device information.
334 * 0 on success, negative errno value on failure.
337 mlx5_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
339 struct ibv_device **list;
340 struct ibv_device *ibv_dev;
342 struct ibv_context *attr_ctx = NULL;
343 struct ibv_device_attr device_attr;
350 assert(pci_drv == &mlx5_driver.pci_drv);
351 /* Get mlx5_dev[] index. */
352 idx = mlx5_dev_idx(&pci_dev->addr);
354 ERROR("this driver cannot support any more adapters");
357 DEBUG("using driver device index %d", idx);
359 /* Save PCI address. */
360 mlx5_dev[idx].pci_addr = pci_dev->addr;
361 list = ibv_get_device_list(&i);
364 if (errno == ENOSYS) {
365 WARN("cannot list devices, is ib_uverbs loaded?");
372 * For each listed device, check related sysfs entry against
373 * the provided PCI ID.
376 struct rte_pci_addr pci_addr;
379 DEBUG("checking device \"%s\"", list[i]->name);
380 if (mlx5_ibv_device_to_pci_addr(list[i], &pci_addr))
382 if ((pci_dev->addr.domain != pci_addr.domain) ||
383 (pci_dev->addr.bus != pci_addr.bus) ||
384 (pci_dev->addr.devid != pci_addr.devid) ||
385 (pci_dev->addr.function != pci_addr.function))
387 sriov = ((pci_dev->id.device_id ==
388 PCI_DEVICE_ID_MELLANOX_CONNECTX4VF) ||
389 (pci_dev->id.device_id ==
390 PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF));
391 /* Multi-packet send is only supported by ConnectX-4 Lx PF. */
392 mps = (pci_dev->id.device_id ==
393 PCI_DEVICE_ID_MELLANOX_CONNECTX4LX);
394 INFO("PCI information matches, using device \"%s\""
395 " (SR-IOV: %s, MPS: %s)",
397 sriov ? "true" : "false",
398 mps ? "true" : "false");
399 attr_ctx = ibv_open_device(list[i]);
403 if (attr_ctx == NULL) {
404 ibv_free_device_list(list);
407 WARN("cannot access device, is mlx5_ib loaded?");
410 WARN("cannot use device, are drivers up to date?");
418 DEBUG("device opened");
419 if (ibv_query_device(attr_ctx, &device_attr))
421 INFO("%u port(s) detected", device_attr.phys_port_cnt);
423 for (i = 0; i < device_attr.phys_port_cnt; i++) {
424 uint32_t port = i + 1; /* ports are indexed from one */
425 uint32_t test = (1 << i);
426 struct ibv_context *ctx = NULL;
427 struct ibv_port_attr port_attr;
428 struct ibv_pd *pd = NULL;
429 struct priv *priv = NULL;
430 struct rte_eth_dev *eth_dev;
431 struct ibv_exp_device_attr exp_device_attr;
432 struct ether_addr mac;
433 uint16_t num_vfs = 0;
435 exp_device_attr.comp_mask =
436 IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS |
437 IBV_EXP_DEVICE_ATTR_RX_HASH |
438 IBV_EXP_DEVICE_ATTR_VLAN_OFFLOADS |
439 IBV_EXP_DEVICE_ATTR_RX_PAD_END_ALIGN |
442 DEBUG("using port %u (%08" PRIx32 ")", port, test);
444 ctx = ibv_open_device(ibv_dev);
448 /* Check port status. */
449 err = ibv_query_port(ctx, port, &port_attr);
451 ERROR("port query failed: %s", strerror(err));
455 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
456 ERROR("port %d is not configured in Ethernet mode",
461 if (port_attr.state != IBV_PORT_ACTIVE)
462 DEBUG("port %d is not active: \"%s\" (%d)",
463 port, ibv_port_state_str(port_attr.state),
466 /* Allocate protection domain. */
467 pd = ibv_alloc_pd(ctx);
469 ERROR("PD allocation failure");
474 mlx5_dev[idx].ports |= test;
476 /* from rte_ethdev.c */
477 priv = rte_zmalloc("ethdev private structure",
479 RTE_CACHE_LINE_SIZE);
481 ERROR("priv allocation failure");
487 priv->device_attr = device_attr;
490 priv->mtu = ETHER_MTU;
491 priv->cqe_comp = 1; /* Enable compression by default. */
492 err = mlx5_args(priv, pci_dev->devargs);
494 ERROR("failed to process device arguments: %s",
498 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
499 ERROR("ibv_exp_query_device() failed");
504 ((exp_device_attr.exp_device_cap_flags &
505 IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
506 (exp_device_attr.exp_device_cap_flags &
507 IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
508 DEBUG("checksum offloading is %ssupported",
509 (priv->hw_csum ? "" : "not "));
511 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
512 IBV_EXP_DEVICE_VXLAN_SUPPORT);
513 DEBUG("L2 tunnel checksum offloads are %ssupported",
514 (priv->hw_csum_l2tun ? "" : "not "));
516 priv->ind_table_max_size = exp_device_attr.rx_hash_caps.max_rwq_indirection_table_size;
517 /* Remove this check once DPDK supports larger/variable
518 * indirection tables. */
519 if (priv->ind_table_max_size > (unsigned int)RSS_INDIRECTION_TABLE_SIZE)
520 priv->ind_table_max_size = RSS_INDIRECTION_TABLE_SIZE;
521 DEBUG("maximum RX indirection table size is %u",
522 priv->ind_table_max_size);
523 priv->hw_vlan_strip = !!(exp_device_attr.wq_vlan_offloads_cap &
524 IBV_EXP_RECEIVE_WQ_CVLAN_STRIP);
525 DEBUG("VLAN stripping is %ssupported",
526 (priv->hw_vlan_strip ? "" : "not "));
528 priv->hw_fcs_strip = !!(exp_device_attr.exp_device_cap_flags &
529 IBV_EXP_DEVICE_SCATTER_FCS);
530 DEBUG("FCS stripping configuration is %ssupported",
531 (priv->hw_fcs_strip ? "" : "not "));
533 priv->hw_padding = !!exp_device_attr.rx_pad_end_addr_align;
534 DEBUG("hardware RX end alignment padding is %ssupported",
535 (priv->hw_padding ? "" : "not "));
537 priv_get_num_vfs(priv, &num_vfs);
538 priv->sriov = (num_vfs || sriov);
540 /* Allocate and register default RSS hash keys. */
541 priv->rss_conf = rte_calloc(__func__, hash_rxq_init_n,
542 sizeof((*priv->rss_conf)[0]), 0);
543 if (priv->rss_conf == NULL) {
547 err = rss_hash_rss_conf_new_key(priv,
548 rss_hash_default_key,
549 rss_hash_default_key_len,
553 /* Configure the first MAC address by default. */
554 if (priv_get_mac(priv, &mac.addr_bytes)) {
555 ERROR("cannot get MAC address, is mlx5_en loaded?"
556 " (errno: %s)", strerror(errno));
559 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
561 mac.addr_bytes[0], mac.addr_bytes[1],
562 mac.addr_bytes[2], mac.addr_bytes[3],
563 mac.addr_bytes[4], mac.addr_bytes[5]);
564 /* Register MAC address. */
565 claim_zero(priv_mac_addr_add(priv, 0,
566 (const uint8_t (*)[ETHER_ADDR_LEN])
568 /* Initialize FD filters list. */
569 err = fdir_init_filters_list(priv);
574 char ifname[IF_NAMESIZE];
576 if (priv_get_ifname(priv, &ifname) == 0)
577 DEBUG("port %u ifname is \"%s\"",
580 DEBUG("port %u ifname is unknown", priv->port);
583 /* Get actual MTU if possible. */
584 priv_get_mtu(priv, &priv->mtu);
585 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
587 /* from rte_ethdev.c */
589 char name[RTE_ETH_NAME_MAX_LEN];
591 snprintf(name, sizeof(name), "%s port %u",
592 ibv_get_device_name(ibv_dev), port);
593 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
595 if (eth_dev == NULL) {
596 ERROR("can not allocate rte ethdev");
601 /* Secondary processes have to use local storage for their
602 * private data as well as a copy of eth_dev->data, but this
603 * pointer must not be modified before burst functions are
604 * actually called. */
605 if (mlx5_is_secondary()) {
606 struct mlx5_secondary_data *sd =
607 &mlx5_secondary_data[eth_dev->data->port_id];
608 sd->primary_priv = eth_dev->data->dev_private;
609 if (sd->primary_priv == NULL) {
610 ERROR("no private data for port %u",
611 eth_dev->data->port_id);
615 sd->shared_dev_data = eth_dev->data;
616 rte_spinlock_init(&sd->lock);
617 memcpy(sd->data.name, sd->shared_dev_data->name,
618 sizeof(sd->data.name));
619 sd->data.dev_private = priv;
620 sd->data.rx_mbuf_alloc_failed = 0;
621 sd->data.mtu = ETHER_MTU;
622 sd->data.port_id = sd->shared_dev_data->port_id;
623 sd->data.mac_addrs = priv->mac;
624 eth_dev->tx_pkt_burst = mlx5_tx_burst_secondary_setup;
625 eth_dev->rx_pkt_burst = mlx5_rx_burst_secondary_setup;
627 eth_dev->data->dev_private = priv;
628 eth_dev->data->rx_mbuf_alloc_failed = 0;
629 eth_dev->data->mtu = ETHER_MTU;
630 eth_dev->data->mac_addrs = priv->mac;
633 eth_dev->pci_dev = pci_dev;
634 rte_eth_copy_pci_info(eth_dev, pci_dev);
635 eth_dev->driver = &mlx5_driver;
637 eth_dev->dev_ops = &mlx5_dev_ops;
639 TAILQ_INIT(ð_dev->link_intr_cbs);
641 /* Bring Ethernet device up. */
642 DEBUG("forcing Ethernet interface up");
643 priv_set_flags(priv, ~IFF_UP, IFF_UP);
648 rte_free(priv->rss_conf);
652 claim_zero(ibv_dealloc_pd(pd));
654 claim_zero(ibv_close_device(ctx));
659 * XXX if something went wrong in the loop above, there is a resource
660 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
661 * long as the dpdk does not provide a way to deallocate a ethdev and a
662 * way to enumerate the registered ethdevs to free the previous ones.
665 /* no port found, complain */
666 if (!mlx5_dev[idx].ports) {
673 claim_zero(ibv_close_device(attr_ctx));
675 ibv_free_device_list(list);
680 static const struct rte_pci_id mlx5_pci_id_map[] = {
682 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
683 PCI_DEVICE_ID_MELLANOX_CONNECTX4)
686 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
687 PCI_DEVICE_ID_MELLANOX_CONNECTX4VF)
690 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
691 PCI_DEVICE_ID_MELLANOX_CONNECTX4LX)
694 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
695 PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF)
702 static struct eth_driver mlx5_driver = {
704 .name = MLX5_DRIVER_NAME,
705 .id_table = mlx5_pci_id_map,
706 .devinit = mlx5_pci_devinit,
707 .drv_flags = RTE_PCI_DRV_INTR_LSC,
709 .dev_private_size = sizeof(struct priv)
713 * Driver initialization routine.
716 rte_mlx5_pmd_init(const char *name, const char *args)
721 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
722 * huge pages. Calling ibv_fork_init() during init allows
723 * applications to use fork() safely for purposes other than
724 * using this PMD, which is not supported in forked processes.
726 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
728 rte_eal_pci_register(&mlx5_driver.pci_drv);
732 static struct rte_driver rte_mlx5_driver = {
734 .name = MLX5_DRIVER_NAME,
735 .init = rte_mlx5_pmd_init,
738 PMD_REGISTER_DRIVER(rte_mlx5_driver)