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 /* Prevent crashes when queues are still in use. */
89 dev->rx_pkt_burst = removed_rx_burst;
90 dev->tx_pkt_burst = removed_tx_burst;
91 if (priv->rxqs != NULL) {
92 /* XXX race condition if mlx5_rx_burst() is still running. */
94 for (i = 0; (i != priv->rxqs_n); ++i) {
95 tmp = (*priv->rxqs)[i];
98 (*priv->rxqs)[i] = NULL;
105 if (priv->txqs != NULL) {
106 /* XXX race condition if mlx5_tx_burst() is still running. */
108 for (i = 0; (i != priv->txqs_n); ++i) {
109 tmp = (*priv->txqs)[i];
112 (*priv->txqs)[i] = NULL;
120 rxq_cleanup(&priv->rxq_parent);
121 if (priv->pd != NULL) {
122 assert(priv->ctx != NULL);
123 claim_zero(ibv_dealloc_pd(priv->pd));
124 claim_zero(ibv_close_device(priv->ctx));
126 assert(priv->ctx == NULL);
128 memset(priv, 0, sizeof(*priv));
131 static const struct eth_dev_ops mlx5_dev_ops = {
132 .dev_configure = mlx5_dev_configure,
133 .dev_start = mlx5_dev_start,
134 .dev_stop = mlx5_dev_stop,
135 .dev_close = mlx5_dev_close,
136 .promiscuous_enable = mlx5_promiscuous_enable,
137 .promiscuous_disable = mlx5_promiscuous_disable,
138 .allmulticast_enable = mlx5_allmulticast_enable,
139 .allmulticast_disable = mlx5_allmulticast_disable,
140 .stats_get = mlx5_stats_get,
141 .stats_reset = mlx5_stats_reset,
142 .dev_infos_get = mlx5_dev_infos_get,
143 .rx_queue_setup = mlx5_rx_queue_setup,
144 .tx_queue_setup = mlx5_tx_queue_setup,
145 .rx_queue_release = mlx5_rx_queue_release,
146 .tx_queue_release = mlx5_tx_queue_release,
147 .mac_addr_remove = mlx5_mac_addr_remove,
148 .mac_addr_add = mlx5_mac_addr_add,
149 .mtu_set = mlx5_dev_set_mtu,
153 struct rte_pci_addr pci_addr; /* associated PCI address */
154 uint32_t ports; /* physical ports bitfield. */
158 * Get device index in mlx5_dev[] from PCI bus address.
160 * @param[in] pci_addr
161 * PCI bus address to look for.
164 * mlx5_dev[] index on success, -1 on failure.
167 mlx5_dev_idx(struct rte_pci_addr *pci_addr)
172 assert(pci_addr != NULL);
173 for (i = 0; (i != RTE_DIM(mlx5_dev)); ++i) {
174 if ((mlx5_dev[i].pci_addr.domain == pci_addr->domain) &&
175 (mlx5_dev[i].pci_addr.bus == pci_addr->bus) &&
176 (mlx5_dev[i].pci_addr.devid == pci_addr->devid) &&
177 (mlx5_dev[i].pci_addr.function == pci_addr->function))
179 if ((mlx5_dev[i].ports == 0) && (ret == -1))
185 static struct eth_driver mlx5_driver;
188 * DPDK callback to register a PCI device.
190 * This function creates an Ethernet device for each port of a given
194 * PCI driver structure (mlx5_driver).
196 * PCI device information.
199 * 0 on success, negative errno value on failure.
202 mlx5_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
204 struct ibv_device **list;
205 struct ibv_device *ibv_dev;
207 struct ibv_context *attr_ctx = NULL;
208 struct ibv_device_attr device_attr;
214 assert(pci_drv == &mlx5_driver.pci_drv);
215 /* Get mlx5_dev[] index. */
216 idx = mlx5_dev_idx(&pci_dev->addr);
218 ERROR("this driver cannot support any more adapters");
221 DEBUG("using driver device index %d", idx);
223 /* Save PCI address. */
224 mlx5_dev[idx].pci_addr = pci_dev->addr;
225 list = ibv_get_device_list(&i);
228 if (errno == ENOSYS) {
229 WARN("cannot list devices, is ib_uverbs loaded?");
236 * For each listed device, check related sysfs entry against
237 * the provided PCI ID.
240 struct rte_pci_addr pci_addr;
243 DEBUG("checking device \"%s\"", list[i]->name);
244 if (mlx5_ibv_device_to_pci_addr(list[i], &pci_addr))
246 if ((pci_dev->addr.domain != pci_addr.domain) ||
247 (pci_dev->addr.bus != pci_addr.bus) ||
248 (pci_dev->addr.devid != pci_addr.devid) ||
249 (pci_dev->addr.function != pci_addr.function))
251 vf = ((pci_dev->id.device_id ==
252 PCI_DEVICE_ID_MELLANOX_CONNECTX4VF) ||
253 (pci_dev->id.device_id ==
254 PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF));
255 INFO("PCI information matches, using device \"%s\" (VF: %s)",
256 list[i]->name, (vf ? "true" : "false"));
257 attr_ctx = ibv_open_device(list[i]);
261 if (attr_ctx == NULL) {
262 ibv_free_device_list(list);
265 WARN("cannot access device, is mlx5_ib loaded?");
268 WARN("cannot use device, are drivers up to date?");
276 DEBUG("device opened");
277 if (ibv_query_device(attr_ctx, &device_attr))
279 INFO("%u port(s) detected", device_attr.phys_port_cnt);
281 for (i = 0; i < device_attr.phys_port_cnt; i++) {
282 uint32_t port = i + 1; /* ports are indexed from one */
283 uint32_t test = (1 << i);
284 struct ibv_context *ctx = NULL;
285 struct ibv_port_attr port_attr;
286 struct ibv_pd *pd = NULL;
287 struct priv *priv = NULL;
288 struct rte_eth_dev *eth_dev;
289 #ifdef HAVE_EXP_QUERY_DEVICE
290 struct ibv_exp_device_attr exp_device_attr;
291 #endif /* HAVE_EXP_QUERY_DEVICE */
292 struct ether_addr mac;
294 #ifdef HAVE_EXP_QUERY_DEVICE
295 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
297 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
298 #endif /* RSS_SUPPORT */
299 #endif /* HAVE_EXP_QUERY_DEVICE */
301 DEBUG("using port %u (%08" PRIx32 ")", port, test);
303 ctx = ibv_open_device(ibv_dev);
307 /* Check port status. */
308 err = ibv_query_port(ctx, port, &port_attr);
310 ERROR("port query failed: %s", strerror(err));
313 if (port_attr.state != IBV_PORT_ACTIVE)
314 DEBUG("port %d is not active: \"%s\" (%d)",
315 port, ibv_port_state_str(port_attr.state),
318 /* Allocate protection domain. */
319 pd = ibv_alloc_pd(ctx);
321 ERROR("PD allocation failure");
326 mlx5_dev[idx].ports |= test;
328 /* from rte_ethdev.c */
329 priv = rte_zmalloc("ethdev private structure",
331 RTE_CACHE_LINE_SIZE);
333 ERROR("priv allocation failure");
339 priv->device_attr = device_attr;
342 priv->mtu = ETHER_MTU;
343 #ifdef HAVE_EXP_QUERY_DEVICE
344 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
345 ERROR("ibv_exp_query_device() failed");
349 if ((exp_device_attr.exp_device_cap_flags &
350 IBV_EXP_DEVICE_QPG) &&
351 (exp_device_attr.exp_device_cap_flags &
352 IBV_EXP_DEVICE_UD_RSS) &&
353 (exp_device_attr.comp_mask &
354 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
355 (exp_device_attr.max_rss_tbl_sz > 0)) {
358 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
362 priv->max_rss_tbl_sz = 0;
364 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
365 IBV_EXP_DEVICE_UD_TSS);
366 DEBUG("device flags: %s%s%s",
367 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
368 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
369 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
371 DEBUG("maximum RSS indirection table size: %u",
372 exp_device_attr.max_rss_tbl_sz);
373 #endif /* RSS_SUPPORT */
376 ((exp_device_attr.exp_device_cap_flags &
377 IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
378 (exp_device_attr.exp_device_cap_flags &
379 IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
380 DEBUG("checksum offloading is %ssupported",
381 (priv->hw_csum ? "" : "not "));
383 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
384 IBV_EXP_DEVICE_VXLAN_SUPPORT);
385 DEBUG("L2 tunnel checksum offloads are %ssupported",
386 (priv->hw_csum_l2tun ? "" : "not "));
388 #endif /* HAVE_EXP_QUERY_DEVICE */
391 /* Configure the first MAC address by default. */
392 if (priv_get_mac(priv, &mac.addr_bytes)) {
393 ERROR("cannot get MAC address, is mlx5_en loaded?"
394 " (errno: %s)", strerror(errno));
397 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
399 mac.addr_bytes[0], mac.addr_bytes[1],
400 mac.addr_bytes[2], mac.addr_bytes[3],
401 mac.addr_bytes[4], mac.addr_bytes[5]);
402 /* Register MAC and broadcast addresses. */
403 claim_zero(priv_mac_addr_add(priv, 0,
404 (const uint8_t (*)[ETHER_ADDR_LEN])
406 claim_zero(priv_mac_addr_add(priv, (RTE_DIM(priv->mac) - 1),
407 &(const uint8_t [ETHER_ADDR_LEN])
408 { "\xff\xff\xff\xff\xff\xff" }));
411 char ifname[IF_NAMESIZE];
413 if (priv_get_ifname(priv, &ifname) == 0)
414 DEBUG("port %u ifname is \"%s\"",
417 DEBUG("port %u ifname is unknown", priv->port);
420 /* Get actual MTU if possible. */
421 priv_get_mtu(priv, &priv->mtu);
422 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
424 /* from rte_ethdev.c */
426 char name[RTE_ETH_NAME_MAX_LEN];
428 snprintf(name, sizeof(name), "%s port %u",
429 ibv_get_device_name(ibv_dev), port);
430 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
432 if (eth_dev == NULL) {
433 ERROR("can not allocate rte ethdev");
438 eth_dev->data->dev_private = priv;
439 eth_dev->pci_dev = pci_dev;
440 eth_dev->driver = &mlx5_driver;
441 eth_dev->data->rx_mbuf_alloc_failed = 0;
442 eth_dev->data->mtu = ETHER_MTU;
445 eth_dev->dev_ops = &mlx5_dev_ops;
446 eth_dev->data->mac_addrs = priv->mac;
448 /* Bring Ethernet device up. */
449 DEBUG("forcing Ethernet interface up");
450 priv_set_flags(priv, ~IFF_UP, IFF_UP);
456 claim_zero(ibv_dealloc_pd(pd));
458 claim_zero(ibv_close_device(ctx));
463 * XXX if something went wrong in the loop above, there is a resource
464 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
465 * long as the dpdk does not provide a way to deallocate a ethdev and a
466 * way to enumerate the registered ethdevs to free the previous ones.
469 /* no port found, complain */
470 if (!mlx5_dev[idx].ports) {
477 claim_zero(ibv_close_device(attr_ctx));
479 ibv_free_device_list(list);
484 static const struct rte_pci_id mlx5_pci_id_map[] = {
486 .vendor_id = PCI_VENDOR_ID_MELLANOX,
487 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4,
488 .subsystem_vendor_id = PCI_ANY_ID,
489 .subsystem_device_id = PCI_ANY_ID
492 .vendor_id = PCI_VENDOR_ID_MELLANOX,
493 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4VF,
494 .subsystem_vendor_id = PCI_ANY_ID,
495 .subsystem_device_id = PCI_ANY_ID
498 .vendor_id = PCI_VENDOR_ID_MELLANOX,
499 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4LX,
500 .subsystem_vendor_id = PCI_ANY_ID,
501 .subsystem_device_id = PCI_ANY_ID
504 .vendor_id = PCI_VENDOR_ID_MELLANOX,
505 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF,
506 .subsystem_vendor_id = PCI_ANY_ID,
507 .subsystem_device_id = PCI_ANY_ID
514 static struct eth_driver mlx5_driver = {
516 .name = MLX5_DRIVER_NAME,
517 .id_table = mlx5_pci_id_map,
518 .devinit = mlx5_pci_devinit,
520 .dev_private_size = sizeof(struct priv)
524 * Driver initialization routine.
527 rte_mlx5_pmd_init(const char *name, const char *args)
532 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
533 * huge pages. Calling ibv_fork_init() during init allows
534 * applications to use fork() safely for purposes other than
535 * using this PMD, which is not supported in forked processes.
537 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
539 rte_eal_pci_register(&mlx5_driver.pci_drv);
543 static struct rte_driver rte_mlx5_driver = {
545 .name = MLX5_DRIVER_NAME,
546 .init = rte_mlx5_pmd_init,
549 PMD_REGISTER_DRIVER(rte_mlx5_driver)