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"
73 * Retrieve integer value from environment variable.
76 * Environment variable name.
79 * Integer value, 0 if the variable is not set.
82 mlx5_getenv_int(const char *name)
84 const char *val = getenv(name);
92 * DPDK callback to close the device.
94 * Destroy all queues and objects, free memory.
97 * Pointer to Ethernet device structure.
100 mlx5_dev_close(struct rte_eth_dev *dev)
102 struct priv *priv = mlx5_get_priv(dev);
106 DEBUG("%p: closing device \"%s\"",
108 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
109 /* In case mlx5_dev_stop() has not been called. */
110 priv_dev_interrupt_handler_uninstall(priv, dev);
111 priv_special_flow_disable_all(priv);
112 priv_mac_addrs_disable(priv);
113 priv_destroy_hash_rxqs(priv);
115 /* Remove flow director elements. */
116 priv_fdir_disable(priv);
117 priv_fdir_delete_filters_list(priv);
119 /* Prevent crashes when queues are still in use. */
120 dev->rx_pkt_burst = removed_rx_burst;
121 dev->tx_pkt_burst = removed_tx_burst;
122 if (priv->rxqs != NULL) {
123 /* XXX race condition if mlx5_rx_burst() is still running. */
125 for (i = 0; (i != priv->rxqs_n); ++i) {
126 struct rxq *rxq = (*priv->rxqs)[i];
127 struct rxq_ctrl *rxq_ctrl;
131 rxq_ctrl = container_of(rxq, struct rxq_ctrl, rxq);
132 (*priv->rxqs)[i] = NULL;
133 rxq_cleanup(rxq_ctrl);
139 if (priv->txqs != NULL) {
140 /* XXX race condition if mlx5_tx_burst() is still running. */
142 for (i = 0; (i != priv->txqs_n); ++i) {
143 struct txq *txq = (*priv->txqs)[i];
144 struct txq_ctrl *txq_ctrl;
148 txq_ctrl = container_of(txq, struct txq_ctrl, txq);
149 (*priv->txqs)[i] = NULL;
150 txq_cleanup(txq_ctrl);
156 if (priv->pd != NULL) {
157 assert(priv->ctx != NULL);
158 claim_zero(ibv_dealloc_pd(priv->pd));
159 claim_zero(ibv_close_device(priv->ctx));
161 assert(priv->ctx == NULL);
162 if (priv->rss_conf != NULL) {
163 for (i = 0; (i != hash_rxq_init_n); ++i)
164 rte_free((*priv->rss_conf)[i]);
165 rte_free(priv->rss_conf);
167 if (priv->reta_idx != NULL)
168 rte_free(priv->reta_idx);
170 memset(priv, 0, sizeof(*priv));
173 static const struct eth_dev_ops mlx5_dev_ops = {
174 .dev_configure = mlx5_dev_configure,
175 .dev_start = mlx5_dev_start,
176 .dev_stop = mlx5_dev_stop,
177 .dev_set_link_down = mlx5_set_link_down,
178 .dev_set_link_up = mlx5_set_link_up,
179 .dev_close = mlx5_dev_close,
180 .promiscuous_enable = mlx5_promiscuous_enable,
181 .promiscuous_disable = mlx5_promiscuous_disable,
182 .allmulticast_enable = mlx5_allmulticast_enable,
183 .allmulticast_disable = mlx5_allmulticast_disable,
184 .link_update = mlx5_link_update,
185 .stats_get = mlx5_stats_get,
186 .stats_reset = mlx5_stats_reset,
187 .dev_infos_get = mlx5_dev_infos_get,
188 .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
189 .vlan_filter_set = mlx5_vlan_filter_set,
190 .rx_queue_setup = mlx5_rx_queue_setup,
191 .tx_queue_setup = mlx5_tx_queue_setup,
192 .rx_queue_release = mlx5_rx_queue_release,
193 .tx_queue_release = mlx5_tx_queue_release,
194 .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
195 .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
196 .mac_addr_remove = mlx5_mac_addr_remove,
197 .mac_addr_add = mlx5_mac_addr_add,
198 .mac_addr_set = mlx5_mac_addr_set,
199 .mtu_set = mlx5_dev_set_mtu,
200 .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
201 .vlan_offload_set = mlx5_vlan_offload_set,
202 .reta_update = mlx5_dev_rss_reta_update,
203 .reta_query = mlx5_dev_rss_reta_query,
204 .rss_hash_update = mlx5_rss_hash_update,
205 .rss_hash_conf_get = mlx5_rss_hash_conf_get,
206 .filter_ctrl = mlx5_dev_filter_ctrl,
210 struct rte_pci_addr pci_addr; /* associated PCI address */
211 uint32_t ports; /* physical ports bitfield. */
215 * Get device index in mlx5_dev[] from PCI bus address.
217 * @param[in] pci_addr
218 * PCI bus address to look for.
221 * mlx5_dev[] index on success, -1 on failure.
224 mlx5_dev_idx(struct rte_pci_addr *pci_addr)
229 assert(pci_addr != NULL);
230 for (i = 0; (i != RTE_DIM(mlx5_dev)); ++i) {
231 if ((mlx5_dev[i].pci_addr.domain == pci_addr->domain) &&
232 (mlx5_dev[i].pci_addr.bus == pci_addr->bus) &&
233 (mlx5_dev[i].pci_addr.devid == pci_addr->devid) &&
234 (mlx5_dev[i].pci_addr.function == pci_addr->function))
236 if ((mlx5_dev[i].ports == 0) && (ret == -1))
243 * Verify and store value for device argument.
246 * Key argument to verify.
248 * Value associated with key.
253 * 0 on success, negative errno value on failure.
256 mlx5_args_check(const char *key, const char *val, void *opaque)
258 struct priv *priv = opaque;
260 /* No parameters are expected at the moment. */
263 WARN("%s: unknown parameter", key);
268 * Parse device parameters.
271 * Pointer to private structure.
273 * Device arguments structure.
276 * 0 on success, errno value on failure.
279 mlx5_args(struct priv *priv, struct rte_devargs *devargs)
281 const char **params = (const char *[]){
284 struct rte_kvargs *kvlist;
290 /* Following UGLY cast is done to pass checkpatch. */
291 kvlist = rte_kvargs_parse(devargs->args, params);
294 /* Process parameters. */
295 for (i = 0; (params[i] != NULL); ++i) {
296 if (rte_kvargs_count(kvlist, params[i])) {
297 ret = rte_kvargs_process(kvlist, params[i],
298 mlx5_args_check, priv);
303 rte_kvargs_free(kvlist);
307 static struct eth_driver mlx5_driver;
310 * DPDK callback to register a PCI device.
312 * This function creates an Ethernet device for each port of a given
316 * PCI driver structure (mlx5_driver).
318 * PCI device information.
321 * 0 on success, negative errno value on failure.
324 mlx5_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
326 struct ibv_device **list;
327 struct ibv_device *ibv_dev;
329 struct ibv_context *attr_ctx = NULL;
330 struct ibv_device_attr device_attr;
337 assert(pci_drv == &mlx5_driver.pci_drv);
338 /* Get mlx5_dev[] index. */
339 idx = mlx5_dev_idx(&pci_dev->addr);
341 ERROR("this driver cannot support any more adapters");
344 DEBUG("using driver device index %d", idx);
346 /* Save PCI address. */
347 mlx5_dev[idx].pci_addr = pci_dev->addr;
348 list = ibv_get_device_list(&i);
351 if (errno == ENOSYS) {
352 WARN("cannot list devices, is ib_uverbs loaded?");
359 * For each listed device, check related sysfs entry against
360 * the provided PCI ID.
363 struct rte_pci_addr pci_addr;
366 DEBUG("checking device \"%s\"", list[i]->name);
367 if (mlx5_ibv_device_to_pci_addr(list[i], &pci_addr))
369 if ((pci_dev->addr.domain != pci_addr.domain) ||
370 (pci_dev->addr.bus != pci_addr.bus) ||
371 (pci_dev->addr.devid != pci_addr.devid) ||
372 (pci_dev->addr.function != pci_addr.function))
374 sriov = ((pci_dev->id.device_id ==
375 PCI_DEVICE_ID_MELLANOX_CONNECTX4VF) ||
376 (pci_dev->id.device_id ==
377 PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF));
378 /* Multi-packet send is only supported by ConnectX-4 Lx PF. */
379 mps = (pci_dev->id.device_id ==
380 PCI_DEVICE_ID_MELLANOX_CONNECTX4LX);
381 INFO("PCI information matches, using device \"%s\""
382 " (SR-IOV: %s, MPS: %s)",
384 sriov ? "true" : "false",
385 mps ? "true" : "false");
386 attr_ctx = ibv_open_device(list[i]);
390 if (attr_ctx == NULL) {
391 ibv_free_device_list(list);
394 WARN("cannot access device, is mlx5_ib loaded?");
397 WARN("cannot use device, are drivers up to date?");
405 DEBUG("device opened");
406 if (ibv_query_device(attr_ctx, &device_attr))
408 INFO("%u port(s) detected", device_attr.phys_port_cnt);
410 for (i = 0; i < device_attr.phys_port_cnt; i++) {
411 uint32_t port = i + 1; /* ports are indexed from one */
412 uint32_t test = (1 << i);
413 struct ibv_context *ctx = NULL;
414 struct ibv_port_attr port_attr;
415 struct ibv_pd *pd = NULL;
416 struct priv *priv = NULL;
417 struct rte_eth_dev *eth_dev;
418 struct ibv_exp_device_attr exp_device_attr;
419 struct ether_addr mac;
420 uint16_t num_vfs = 0;
422 exp_device_attr.comp_mask =
423 IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS |
424 IBV_EXP_DEVICE_ATTR_RX_HASH |
425 IBV_EXP_DEVICE_ATTR_VLAN_OFFLOADS |
426 IBV_EXP_DEVICE_ATTR_RX_PAD_END_ALIGN |
429 DEBUG("using port %u (%08" PRIx32 ")", port, test);
431 ctx = ibv_open_device(ibv_dev);
435 /* Check port status. */
436 err = ibv_query_port(ctx, port, &port_attr);
438 ERROR("port query failed: %s", strerror(err));
442 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
443 ERROR("port %d is not configured in Ethernet mode",
448 if (port_attr.state != IBV_PORT_ACTIVE)
449 DEBUG("port %d is not active: \"%s\" (%d)",
450 port, ibv_port_state_str(port_attr.state),
453 /* Allocate protection domain. */
454 pd = ibv_alloc_pd(ctx);
456 ERROR("PD allocation failure");
461 mlx5_dev[idx].ports |= test;
463 /* from rte_ethdev.c */
464 priv = rte_zmalloc("ethdev private structure",
466 RTE_CACHE_LINE_SIZE);
468 ERROR("priv allocation failure");
474 priv->device_attr = device_attr;
477 priv->mtu = ETHER_MTU;
478 err = mlx5_args(priv, pci_dev->devargs);
480 ERROR("failed to process device arguments: %s",
484 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
485 ERROR("ibv_exp_query_device() failed");
490 ((exp_device_attr.exp_device_cap_flags &
491 IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
492 (exp_device_attr.exp_device_cap_flags &
493 IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
494 DEBUG("checksum offloading is %ssupported",
495 (priv->hw_csum ? "" : "not "));
497 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
498 IBV_EXP_DEVICE_VXLAN_SUPPORT);
499 DEBUG("L2 tunnel checksum offloads are %ssupported",
500 (priv->hw_csum_l2tun ? "" : "not "));
502 priv->ind_table_max_size = exp_device_attr.rx_hash_caps.max_rwq_indirection_table_size;
503 /* Remove this check once DPDK supports larger/variable
504 * indirection tables. */
505 if (priv->ind_table_max_size > (unsigned int)RSS_INDIRECTION_TABLE_SIZE)
506 priv->ind_table_max_size = RSS_INDIRECTION_TABLE_SIZE;
507 DEBUG("maximum RX indirection table size is %u",
508 priv->ind_table_max_size);
509 priv->hw_vlan_strip = !!(exp_device_attr.wq_vlan_offloads_cap &
510 IBV_EXP_RECEIVE_WQ_CVLAN_STRIP);
511 DEBUG("VLAN stripping is %ssupported",
512 (priv->hw_vlan_strip ? "" : "not "));
514 priv->hw_fcs_strip = !!(exp_device_attr.exp_device_cap_flags &
515 IBV_EXP_DEVICE_SCATTER_FCS);
516 DEBUG("FCS stripping configuration is %ssupported",
517 (priv->hw_fcs_strip ? "" : "not "));
519 priv->hw_padding = !!exp_device_attr.rx_pad_end_addr_align;
520 DEBUG("hardware RX end alignment padding is %ssupported",
521 (priv->hw_padding ? "" : "not "));
523 priv_get_num_vfs(priv, &num_vfs);
524 priv->sriov = (num_vfs || sriov);
526 /* Allocate and register default RSS hash keys. */
527 priv->rss_conf = rte_calloc(__func__, hash_rxq_init_n,
528 sizeof((*priv->rss_conf)[0]), 0);
529 if (priv->rss_conf == NULL) {
533 err = rss_hash_rss_conf_new_key(priv,
534 rss_hash_default_key,
535 rss_hash_default_key_len,
539 /* Configure the first MAC address by default. */
540 if (priv_get_mac(priv, &mac.addr_bytes)) {
541 ERROR("cannot get MAC address, is mlx5_en loaded?"
542 " (errno: %s)", strerror(errno));
545 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
547 mac.addr_bytes[0], mac.addr_bytes[1],
548 mac.addr_bytes[2], mac.addr_bytes[3],
549 mac.addr_bytes[4], mac.addr_bytes[5]);
550 /* Register MAC address. */
551 claim_zero(priv_mac_addr_add(priv, 0,
552 (const uint8_t (*)[ETHER_ADDR_LEN])
554 /* Initialize FD filters list. */
555 err = fdir_init_filters_list(priv);
560 char ifname[IF_NAMESIZE];
562 if (priv_get_ifname(priv, &ifname) == 0)
563 DEBUG("port %u ifname is \"%s\"",
566 DEBUG("port %u ifname is unknown", priv->port);
569 /* Get actual MTU if possible. */
570 priv_get_mtu(priv, &priv->mtu);
571 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
573 /* from rte_ethdev.c */
575 char name[RTE_ETH_NAME_MAX_LEN];
577 snprintf(name, sizeof(name), "%s port %u",
578 ibv_get_device_name(ibv_dev), port);
579 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
581 if (eth_dev == NULL) {
582 ERROR("can not allocate rte ethdev");
587 /* Secondary processes have to use local storage for their
588 * private data as well as a copy of eth_dev->data, but this
589 * pointer must not be modified before burst functions are
590 * actually called. */
591 if (mlx5_is_secondary()) {
592 struct mlx5_secondary_data *sd =
593 &mlx5_secondary_data[eth_dev->data->port_id];
594 sd->primary_priv = eth_dev->data->dev_private;
595 if (sd->primary_priv == NULL) {
596 ERROR("no private data for port %u",
597 eth_dev->data->port_id);
601 sd->shared_dev_data = eth_dev->data;
602 rte_spinlock_init(&sd->lock);
603 memcpy(sd->data.name, sd->shared_dev_data->name,
604 sizeof(sd->data.name));
605 sd->data.dev_private = priv;
606 sd->data.rx_mbuf_alloc_failed = 0;
607 sd->data.mtu = ETHER_MTU;
608 sd->data.port_id = sd->shared_dev_data->port_id;
609 sd->data.mac_addrs = priv->mac;
610 eth_dev->tx_pkt_burst = mlx5_tx_burst_secondary_setup;
611 eth_dev->rx_pkt_burst = mlx5_rx_burst_secondary_setup;
613 eth_dev->data->dev_private = priv;
614 eth_dev->data->rx_mbuf_alloc_failed = 0;
615 eth_dev->data->mtu = ETHER_MTU;
616 eth_dev->data->mac_addrs = priv->mac;
619 eth_dev->pci_dev = pci_dev;
620 rte_eth_copy_pci_info(eth_dev, pci_dev);
621 eth_dev->driver = &mlx5_driver;
623 eth_dev->dev_ops = &mlx5_dev_ops;
625 TAILQ_INIT(ð_dev->link_intr_cbs);
627 /* Bring Ethernet device up. */
628 DEBUG("forcing Ethernet interface up");
629 priv_set_flags(priv, ~IFF_UP, IFF_UP);
634 rte_free(priv->rss_conf);
638 claim_zero(ibv_dealloc_pd(pd));
640 claim_zero(ibv_close_device(ctx));
645 * XXX if something went wrong in the loop above, there is a resource
646 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
647 * long as the dpdk does not provide a way to deallocate a ethdev and a
648 * way to enumerate the registered ethdevs to free the previous ones.
651 /* no port found, complain */
652 if (!mlx5_dev[idx].ports) {
659 claim_zero(ibv_close_device(attr_ctx));
661 ibv_free_device_list(list);
666 static const struct rte_pci_id mlx5_pci_id_map[] = {
668 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
669 PCI_DEVICE_ID_MELLANOX_CONNECTX4)
672 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
673 PCI_DEVICE_ID_MELLANOX_CONNECTX4VF)
676 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
677 PCI_DEVICE_ID_MELLANOX_CONNECTX4LX)
680 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
681 PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF)
688 static struct eth_driver mlx5_driver = {
690 .name = MLX5_DRIVER_NAME,
691 .id_table = mlx5_pci_id_map,
692 .devinit = mlx5_pci_devinit,
693 .drv_flags = RTE_PCI_DRV_INTR_LSC,
695 .dev_private_size = sizeof(struct priv)
699 * Driver initialization routine.
702 rte_mlx5_pmd_init(const char *name, const char *args)
707 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
708 * huge pages. Calling ibv_fork_init() during init allows
709 * applications to use fork() safely for purposes other than
710 * using this PMD, which is not supported in forked processes.
712 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
714 rte_eal_pci_register(&mlx5_driver.pci_drv);
718 static struct rte_driver rte_mlx5_driver = {
720 .name = MLX5_DRIVER_NAME,
721 .init = rte_mlx5_pmd_init,
724 PMD_REGISTER_DRIVER(rte_mlx5_driver)