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.
43 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
45 #pragma GCC diagnostic ignored "-pedantic"
47 #include <infiniband/verbs.h>
49 #pragma GCC diagnostic error "-pedantic"
52 /* DPDK headers don't like -pedantic. */
54 #pragma GCC diagnostic ignored "-pedantic"
56 #include <rte_malloc.h>
57 #include <rte_ethdev.h>
59 #include <rte_common.h>
61 #pragma GCC diagnostic error "-pedantic"
65 #include "mlx5_utils.h"
66 #include "mlx5_rxtx.h"
67 #include "mlx5_autoconf.h"
70 * DPDK callback to close the device.
72 * Destroy all queues and objects, free memory.
75 * Pointer to Ethernet device structure.
78 mlx5_dev_close(struct rte_eth_dev *dev)
80 struct priv *priv = dev->data->dev_private;
85 DEBUG("%p: closing device \"%s\"",
87 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
88 /* In case mlx5_dev_stop() has not been called. */
89 priv_dev_interrupt_handler_uninstall(priv, dev);
90 priv_allmulticast_disable(priv);
91 priv_promiscuous_disable(priv);
92 priv_mac_addrs_disable(priv);
93 priv_destroy_hash_rxqs(priv);
94 /* Prevent crashes when queues are still in use. */
95 dev->rx_pkt_burst = removed_rx_burst;
96 dev->tx_pkt_burst = removed_tx_burst;
97 if (priv->rxqs != NULL) {
98 /* XXX race condition if mlx5_rx_burst() is still running. */
100 for (i = 0; (i != priv->rxqs_n); ++i) {
101 tmp = (*priv->rxqs)[i];
104 (*priv->rxqs)[i] = NULL;
111 if (priv->txqs != NULL) {
112 /* XXX race condition if mlx5_tx_burst() is still running. */
114 for (i = 0; (i != priv->txqs_n); ++i) {
115 tmp = (*priv->txqs)[i];
118 (*priv->txqs)[i] = NULL;
125 if (priv->pd != NULL) {
126 assert(priv->ctx != NULL);
127 claim_zero(ibv_dealloc_pd(priv->pd));
128 claim_zero(ibv_close_device(priv->ctx));
130 assert(priv->ctx == NULL);
131 if (priv->rss_conf != NULL) {
132 for (i = 0; (i != hash_rxq_init_n); ++i)
133 rte_free((*priv->rss_conf)[i]);
134 rte_free(priv->rss_conf);
136 if (priv->reta_idx != NULL)
137 rte_free(priv->reta_idx);
139 memset(priv, 0, sizeof(*priv));
142 static const struct eth_dev_ops mlx5_dev_ops = {
143 .dev_configure = mlx5_dev_configure,
144 .dev_start = mlx5_dev_start,
145 .dev_stop = mlx5_dev_stop,
146 .dev_close = mlx5_dev_close,
147 .promiscuous_enable = mlx5_promiscuous_enable,
148 .promiscuous_disable = mlx5_promiscuous_disable,
149 .allmulticast_enable = mlx5_allmulticast_enable,
150 .allmulticast_disable = mlx5_allmulticast_disable,
151 .link_update = mlx5_link_update,
152 .stats_get = mlx5_stats_get,
153 .stats_reset = mlx5_stats_reset,
154 .dev_infos_get = mlx5_dev_infos_get,
155 .vlan_filter_set = mlx5_vlan_filter_set,
156 .rx_queue_setup = mlx5_rx_queue_setup,
157 .tx_queue_setup = mlx5_tx_queue_setup,
158 .rx_queue_release = mlx5_rx_queue_release,
159 .tx_queue_release = mlx5_tx_queue_release,
160 .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
161 .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
162 .mac_addr_remove = mlx5_mac_addr_remove,
163 .mac_addr_add = mlx5_mac_addr_add,
164 .mtu_set = mlx5_dev_set_mtu,
165 .reta_update = mlx5_dev_rss_reta_update,
166 .reta_query = mlx5_dev_rss_reta_query,
167 .rss_hash_update = mlx5_rss_hash_update,
168 .rss_hash_conf_get = mlx5_rss_hash_conf_get,
172 struct rte_pci_addr pci_addr; /* associated PCI address */
173 uint32_t ports; /* physical ports bitfield. */
177 * Get device index in mlx5_dev[] from PCI bus address.
179 * @param[in] pci_addr
180 * PCI bus address to look for.
183 * mlx5_dev[] index on success, -1 on failure.
186 mlx5_dev_idx(struct rte_pci_addr *pci_addr)
191 assert(pci_addr != NULL);
192 for (i = 0; (i != RTE_DIM(mlx5_dev)); ++i) {
193 if ((mlx5_dev[i].pci_addr.domain == pci_addr->domain) &&
194 (mlx5_dev[i].pci_addr.bus == pci_addr->bus) &&
195 (mlx5_dev[i].pci_addr.devid == pci_addr->devid) &&
196 (mlx5_dev[i].pci_addr.function == pci_addr->function))
198 if ((mlx5_dev[i].ports == 0) && (ret == -1))
204 static struct eth_driver mlx5_driver;
207 * DPDK callback to register a PCI device.
209 * This function creates an Ethernet device for each port of a given
213 * PCI driver structure (mlx5_driver).
215 * PCI device information.
218 * 0 on success, negative errno value on failure.
221 mlx5_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
223 struct ibv_device **list;
224 struct ibv_device *ibv_dev;
226 struct ibv_context *attr_ctx = NULL;
227 struct ibv_device_attr device_attr;
233 assert(pci_drv == &mlx5_driver.pci_drv);
234 /* Get mlx5_dev[] index. */
235 idx = mlx5_dev_idx(&pci_dev->addr);
237 ERROR("this driver cannot support any more adapters");
240 DEBUG("using driver device index %d", idx);
242 /* Save PCI address. */
243 mlx5_dev[idx].pci_addr = pci_dev->addr;
244 list = ibv_get_device_list(&i);
247 if (errno == ENOSYS) {
248 WARN("cannot list devices, is ib_uverbs loaded?");
255 * For each listed device, check related sysfs entry against
256 * the provided PCI ID.
259 struct rte_pci_addr pci_addr;
262 DEBUG("checking device \"%s\"", list[i]->name);
263 if (mlx5_ibv_device_to_pci_addr(list[i], &pci_addr))
265 if ((pci_dev->addr.domain != pci_addr.domain) ||
266 (pci_dev->addr.bus != pci_addr.bus) ||
267 (pci_dev->addr.devid != pci_addr.devid) ||
268 (pci_dev->addr.function != pci_addr.function))
270 vf = ((pci_dev->id.device_id ==
271 PCI_DEVICE_ID_MELLANOX_CONNECTX4VF) ||
272 (pci_dev->id.device_id ==
273 PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF));
274 INFO("PCI information matches, using device \"%s\" (VF: %s)",
275 list[i]->name, (vf ? "true" : "false"));
276 attr_ctx = ibv_open_device(list[i]);
280 if (attr_ctx == NULL) {
281 ibv_free_device_list(list);
284 WARN("cannot access device, is mlx5_ib loaded?");
287 WARN("cannot use device, are drivers up to date?");
295 DEBUG("device opened");
296 if (ibv_query_device(attr_ctx, &device_attr))
298 INFO("%u port(s) detected", device_attr.phys_port_cnt);
300 for (i = 0; i < device_attr.phys_port_cnt; i++) {
301 uint32_t port = i + 1; /* ports are indexed from one */
302 uint32_t test = (1 << i);
303 struct ibv_context *ctx = NULL;
304 struct ibv_port_attr port_attr;
305 struct ibv_pd *pd = NULL;
306 struct priv *priv = NULL;
307 struct rte_eth_dev *eth_dev;
308 #ifdef HAVE_EXP_QUERY_DEVICE
309 struct ibv_exp_device_attr exp_device_attr;
310 #endif /* HAVE_EXP_QUERY_DEVICE */
311 struct ether_addr mac;
313 #ifdef HAVE_EXP_QUERY_DEVICE
314 exp_device_attr.comp_mask =
315 IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS |
316 IBV_EXP_DEVICE_ATTR_RX_HASH;
317 #endif /* HAVE_EXP_QUERY_DEVICE */
319 DEBUG("using port %u (%08" PRIx32 ")", port, test);
321 ctx = ibv_open_device(ibv_dev);
325 /* Check port status. */
326 err = ibv_query_port(ctx, port, &port_attr);
328 ERROR("port query failed: %s", strerror(err));
331 if (port_attr.state != IBV_PORT_ACTIVE)
332 DEBUG("port %d is not active: \"%s\" (%d)",
333 port, ibv_port_state_str(port_attr.state),
336 /* Allocate protection domain. */
337 pd = ibv_alloc_pd(ctx);
339 ERROR("PD allocation failure");
344 mlx5_dev[idx].ports |= test;
346 /* from rte_ethdev.c */
347 priv = rte_zmalloc("ethdev private structure",
349 RTE_CACHE_LINE_SIZE);
351 ERROR("priv allocation failure");
357 priv->device_attr = device_attr;
360 priv->mtu = ETHER_MTU;
361 #ifdef HAVE_EXP_QUERY_DEVICE
362 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
363 ERROR("ibv_exp_query_device() failed");
368 ((exp_device_attr.exp_device_cap_flags &
369 IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
370 (exp_device_attr.exp_device_cap_flags &
371 IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
372 DEBUG("checksum offloading is %ssupported",
373 (priv->hw_csum ? "" : "not "));
375 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
376 IBV_EXP_DEVICE_VXLAN_SUPPORT);
377 DEBUG("L2 tunnel checksum offloads are %ssupported",
378 (priv->hw_csum_l2tun ? "" : "not "));
380 priv->ind_table_max_size =
381 RTE_MIN((unsigned int)RSS_INDIRECTION_TABLE_SIZE,
382 exp_device_attr.rx_hash_caps.max_rwq_indirection_table_size);
383 DEBUG("maximum RX indirection table size is %u",
384 priv->ind_table_max_size);
386 #else /* HAVE_EXP_QUERY_DEVICE */
387 priv->ind_table_max_size = RSS_INDIRECTION_TABLE_SIZE;
388 #endif /* HAVE_EXP_QUERY_DEVICE */
391 /* Allocate and register default RSS hash keys. */
392 priv->rss_conf = rte_calloc(__func__, hash_rxq_init_n,
393 sizeof((*priv->rss_conf)[0]), 0);
394 if (priv->rss_conf == NULL) {
398 err = rss_hash_rss_conf_new_key(priv,
399 rss_hash_default_key,
400 rss_hash_default_key_len,
404 /* Configure the first MAC address by default. */
405 if (priv_get_mac(priv, &mac.addr_bytes)) {
406 ERROR("cannot get MAC address, is mlx5_en loaded?"
407 " (errno: %s)", strerror(errno));
410 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
412 mac.addr_bytes[0], mac.addr_bytes[1],
413 mac.addr_bytes[2], mac.addr_bytes[3],
414 mac.addr_bytes[4], mac.addr_bytes[5]);
415 /* Register MAC and broadcast addresses. */
416 claim_zero(priv_mac_addr_add(priv, 0,
417 (const uint8_t (*)[ETHER_ADDR_LEN])
419 claim_zero(priv_mac_addr_add(priv, (RTE_DIM(priv->mac) - 1),
420 &(const uint8_t [ETHER_ADDR_LEN])
421 { "\xff\xff\xff\xff\xff\xff" }));
424 char ifname[IF_NAMESIZE];
426 if (priv_get_ifname(priv, &ifname) == 0)
427 DEBUG("port %u ifname is \"%s\"",
430 DEBUG("port %u ifname is unknown", priv->port);
433 /* Get actual MTU if possible. */
434 priv_get_mtu(priv, &priv->mtu);
435 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
437 /* from rte_ethdev.c */
439 char name[RTE_ETH_NAME_MAX_LEN];
441 snprintf(name, sizeof(name), "%s port %u",
442 ibv_get_device_name(ibv_dev), port);
443 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
445 if (eth_dev == NULL) {
446 ERROR("can not allocate rte ethdev");
451 eth_dev->data->dev_private = priv;
452 eth_dev->pci_dev = pci_dev;
454 rte_eth_copy_pci_info(eth_dev, pci_dev);
456 eth_dev->driver = &mlx5_driver;
457 eth_dev->data->rx_mbuf_alloc_failed = 0;
458 eth_dev->data->mtu = ETHER_MTU;
461 eth_dev->dev_ops = &mlx5_dev_ops;
462 eth_dev->data->mac_addrs = priv->mac;
463 TAILQ_INIT(ð_dev->link_intr_cbs);
465 /* Bring Ethernet device up. */
466 DEBUG("forcing Ethernet interface up");
467 priv_set_flags(priv, ~IFF_UP, IFF_UP);
471 rte_free(priv->rss_conf);
474 claim_zero(ibv_dealloc_pd(pd));
476 claim_zero(ibv_close_device(ctx));
481 * XXX if something went wrong in the loop above, there is a resource
482 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
483 * long as the dpdk does not provide a way to deallocate a ethdev and a
484 * way to enumerate the registered ethdevs to free the previous ones.
487 /* no port found, complain */
488 if (!mlx5_dev[idx].ports) {
495 claim_zero(ibv_close_device(attr_ctx));
497 ibv_free_device_list(list);
502 static const struct rte_pci_id mlx5_pci_id_map[] = {
504 .vendor_id = PCI_VENDOR_ID_MELLANOX,
505 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4,
506 .subsystem_vendor_id = PCI_ANY_ID,
507 .subsystem_device_id = PCI_ANY_ID
510 .vendor_id = PCI_VENDOR_ID_MELLANOX,
511 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4VF,
512 .subsystem_vendor_id = PCI_ANY_ID,
513 .subsystem_device_id = PCI_ANY_ID
516 .vendor_id = PCI_VENDOR_ID_MELLANOX,
517 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4LX,
518 .subsystem_vendor_id = PCI_ANY_ID,
519 .subsystem_device_id = PCI_ANY_ID
522 .vendor_id = PCI_VENDOR_ID_MELLANOX,
523 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF,
524 .subsystem_vendor_id = PCI_ANY_ID,
525 .subsystem_device_id = PCI_ANY_ID
532 static struct eth_driver mlx5_driver = {
534 .name = MLX5_DRIVER_NAME,
535 .id_table = mlx5_pci_id_map,
536 .devinit = mlx5_pci_devinit,
537 .drv_flags = RTE_PCI_DRV_INTR_LSC,
539 .dev_private_size = sizeof(struct priv)
543 * Driver initialization routine.
546 rte_mlx5_pmd_init(const char *name, const char *args)
551 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
552 * huge pages. Calling ibv_fork_init() during init allows
553 * applications to use fork() safely for purposes other than
554 * using this PMD, which is not supported in forked processes.
556 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
558 rte_eal_pci_register(&mlx5_driver.pci_drv);
562 static struct rte_driver rte_mlx5_driver = {
564 .name = MLX5_DRIVER_NAME,
565 .init = rte_mlx5_pmd_init,
568 PMD_REGISTER_DRIVER(rte_mlx5_driver)