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 .dev_infos_get = mlx5_dev_infos_get,
137 .rx_queue_setup = mlx5_rx_queue_setup,
138 .tx_queue_setup = mlx5_tx_queue_setup,
139 .rx_queue_release = mlx5_rx_queue_release,
140 .tx_queue_release = mlx5_tx_queue_release,
141 .mac_addr_remove = mlx5_mac_addr_remove,
142 .mac_addr_add = mlx5_mac_addr_add,
146 struct rte_pci_addr pci_addr; /* associated PCI address */
147 uint32_t ports; /* physical ports bitfield. */
151 * Get device index in mlx5_dev[] from PCI bus address.
153 * @param[in] pci_addr
154 * PCI bus address to look for.
157 * mlx5_dev[] index on success, -1 on failure.
160 mlx5_dev_idx(struct rte_pci_addr *pci_addr)
165 assert(pci_addr != NULL);
166 for (i = 0; (i != RTE_DIM(mlx5_dev)); ++i) {
167 if ((mlx5_dev[i].pci_addr.domain == pci_addr->domain) &&
168 (mlx5_dev[i].pci_addr.bus == pci_addr->bus) &&
169 (mlx5_dev[i].pci_addr.devid == pci_addr->devid) &&
170 (mlx5_dev[i].pci_addr.function == pci_addr->function))
172 if ((mlx5_dev[i].ports == 0) && (ret == -1))
178 static struct eth_driver mlx5_driver;
181 * DPDK callback to register a PCI device.
183 * This function creates an Ethernet device for each port of a given
187 * PCI driver structure (mlx5_driver).
189 * PCI device information.
192 * 0 on success, negative errno value on failure.
195 mlx5_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
197 struct ibv_device **list;
198 struct ibv_device *ibv_dev;
200 struct ibv_context *attr_ctx = NULL;
201 struct ibv_device_attr device_attr;
207 assert(pci_drv == &mlx5_driver.pci_drv);
208 /* Get mlx5_dev[] index. */
209 idx = mlx5_dev_idx(&pci_dev->addr);
211 ERROR("this driver cannot support any more adapters");
214 DEBUG("using driver device index %d", idx);
216 /* Save PCI address. */
217 mlx5_dev[idx].pci_addr = pci_dev->addr;
218 list = ibv_get_device_list(&i);
221 if (errno == ENOSYS) {
222 WARN("cannot list devices, is ib_uverbs loaded?");
229 * For each listed device, check related sysfs entry against
230 * the provided PCI ID.
233 struct rte_pci_addr pci_addr;
236 DEBUG("checking device \"%s\"", list[i]->name);
237 if (mlx5_ibv_device_to_pci_addr(list[i], &pci_addr))
239 if ((pci_dev->addr.domain != pci_addr.domain) ||
240 (pci_dev->addr.bus != pci_addr.bus) ||
241 (pci_dev->addr.devid != pci_addr.devid) ||
242 (pci_dev->addr.function != pci_addr.function))
244 vf = ((pci_dev->id.device_id ==
245 PCI_DEVICE_ID_MELLANOX_CONNECTX4VF) ||
246 (pci_dev->id.device_id ==
247 PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF));
248 INFO("PCI information matches, using device \"%s\" (VF: %s)",
249 list[i]->name, (vf ? "true" : "false"));
250 attr_ctx = ibv_open_device(list[i]);
254 if (attr_ctx == NULL) {
255 ibv_free_device_list(list);
258 WARN("cannot access device, is mlx5_ib loaded?");
261 WARN("cannot use device, are drivers up to date?");
269 DEBUG("device opened");
270 if (ibv_query_device(attr_ctx, &device_attr))
272 INFO("%u port(s) detected", device_attr.phys_port_cnt);
274 for (i = 0; i < device_attr.phys_port_cnt; i++) {
275 uint32_t port = i + 1; /* ports are indexed from one */
276 uint32_t test = (1 << i);
277 struct ibv_context *ctx = NULL;
278 struct ibv_port_attr port_attr;
279 struct ibv_pd *pd = NULL;
280 struct priv *priv = NULL;
281 struct rte_eth_dev *eth_dev;
282 #ifdef HAVE_EXP_QUERY_DEVICE
283 struct ibv_exp_device_attr exp_device_attr;
284 #endif /* HAVE_EXP_QUERY_DEVICE */
285 struct ether_addr mac;
287 #ifdef HAVE_EXP_QUERY_DEVICE
288 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
290 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
291 #endif /* RSS_SUPPORT */
292 #endif /* HAVE_EXP_QUERY_DEVICE */
294 DEBUG("using port %u (%08" PRIx32 ")", port, test);
296 ctx = ibv_open_device(ibv_dev);
300 /* Check port status. */
301 err = ibv_query_port(ctx, port, &port_attr);
303 ERROR("port query failed: %s", strerror(err));
306 if (port_attr.state != IBV_PORT_ACTIVE)
307 DEBUG("port %d is not active: \"%s\" (%d)",
308 port, ibv_port_state_str(port_attr.state),
311 /* Allocate protection domain. */
312 pd = ibv_alloc_pd(ctx);
314 ERROR("PD allocation failure");
319 mlx5_dev[idx].ports |= test;
321 /* from rte_ethdev.c */
322 priv = rte_zmalloc("ethdev private structure",
324 RTE_CACHE_LINE_SIZE);
326 ERROR("priv allocation failure");
332 priv->device_attr = device_attr;
335 priv->mtu = ETHER_MTU;
336 #ifdef HAVE_EXP_QUERY_DEVICE
337 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
338 ERROR("ibv_exp_query_device() failed");
342 if ((exp_device_attr.exp_device_cap_flags &
343 IBV_EXP_DEVICE_QPG) &&
344 (exp_device_attr.exp_device_cap_flags &
345 IBV_EXP_DEVICE_UD_RSS) &&
346 (exp_device_attr.comp_mask &
347 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
348 (exp_device_attr.max_rss_tbl_sz > 0)) {
351 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
355 priv->max_rss_tbl_sz = 0;
357 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
358 IBV_EXP_DEVICE_UD_TSS);
359 DEBUG("device flags: %s%s%s",
360 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
361 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
362 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
364 DEBUG("maximum RSS indirection table size: %u",
365 exp_device_attr.max_rss_tbl_sz);
366 #endif /* RSS_SUPPORT */
369 ((exp_device_attr.exp_device_cap_flags &
370 IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
371 (exp_device_attr.exp_device_cap_flags &
372 IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
373 DEBUG("checksum offloading is %ssupported",
374 (priv->hw_csum ? "" : "not "));
376 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
377 IBV_EXP_DEVICE_VXLAN_SUPPORT);
378 DEBUG("L2 tunnel checksum offloads are %ssupported",
379 (priv->hw_csum_l2tun ? "" : "not "));
381 #endif /* HAVE_EXP_QUERY_DEVICE */
384 /* Configure the first MAC address by default. */
385 if (priv_get_mac(priv, &mac.addr_bytes)) {
386 ERROR("cannot get MAC address, is mlx5_en loaded?"
387 " (errno: %s)", strerror(errno));
390 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
392 mac.addr_bytes[0], mac.addr_bytes[1],
393 mac.addr_bytes[2], mac.addr_bytes[3],
394 mac.addr_bytes[4], mac.addr_bytes[5]);
395 /* Register MAC and broadcast addresses. */
396 claim_zero(priv_mac_addr_add(priv, 0,
397 (const uint8_t (*)[ETHER_ADDR_LEN])
399 claim_zero(priv_mac_addr_add(priv, (RTE_DIM(priv->mac) - 1),
400 &(const uint8_t [ETHER_ADDR_LEN])
401 { "\xff\xff\xff\xff\xff\xff" }));
404 char ifname[IF_NAMESIZE];
406 if (priv_get_ifname(priv, &ifname) == 0)
407 DEBUG("port %u ifname is \"%s\"",
410 DEBUG("port %u ifname is unknown", priv->port);
413 /* Get actual MTU if possible. */
414 priv_get_mtu(priv, &priv->mtu);
415 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
417 /* from rte_ethdev.c */
419 char name[RTE_ETH_NAME_MAX_LEN];
421 snprintf(name, sizeof(name), "%s port %u",
422 ibv_get_device_name(ibv_dev), port);
423 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
425 if (eth_dev == NULL) {
426 ERROR("can not allocate rte ethdev");
431 eth_dev->data->dev_private = priv;
432 eth_dev->pci_dev = pci_dev;
433 eth_dev->driver = &mlx5_driver;
434 eth_dev->data->rx_mbuf_alloc_failed = 0;
435 eth_dev->data->mtu = ETHER_MTU;
438 eth_dev->dev_ops = &mlx5_dev_ops;
439 eth_dev->data->mac_addrs = priv->mac;
441 /* Bring Ethernet device up. */
442 DEBUG("forcing Ethernet interface up");
443 priv_set_flags(priv, ~IFF_UP, IFF_UP);
449 claim_zero(ibv_dealloc_pd(pd));
451 claim_zero(ibv_close_device(ctx));
456 * XXX if something went wrong in the loop above, there is a resource
457 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
458 * long as the dpdk does not provide a way to deallocate a ethdev and a
459 * way to enumerate the registered ethdevs to free the previous ones.
462 /* no port found, complain */
463 if (!mlx5_dev[idx].ports) {
470 claim_zero(ibv_close_device(attr_ctx));
472 ibv_free_device_list(list);
477 static const struct rte_pci_id mlx5_pci_id_map[] = {
479 .vendor_id = PCI_VENDOR_ID_MELLANOX,
480 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4,
481 .subsystem_vendor_id = PCI_ANY_ID,
482 .subsystem_device_id = PCI_ANY_ID
485 .vendor_id = PCI_VENDOR_ID_MELLANOX,
486 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4VF,
487 .subsystem_vendor_id = PCI_ANY_ID,
488 .subsystem_device_id = PCI_ANY_ID
491 .vendor_id = PCI_VENDOR_ID_MELLANOX,
492 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4LX,
493 .subsystem_vendor_id = PCI_ANY_ID,
494 .subsystem_device_id = PCI_ANY_ID
497 .vendor_id = PCI_VENDOR_ID_MELLANOX,
498 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF,
499 .subsystem_vendor_id = PCI_ANY_ID,
500 .subsystem_device_id = PCI_ANY_ID
507 static struct eth_driver mlx5_driver = {
509 .name = MLX5_DRIVER_NAME,
510 .id_table = mlx5_pci_id_map,
511 .devinit = mlx5_pci_devinit,
513 .dev_private_size = sizeof(struct priv)
517 * Driver initialization routine.
520 rte_mlx5_pmd_init(const char *name, const char *args)
525 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
526 * huge pages. Calling ibv_fork_init() during init allows
527 * applications to use fork() safely for purposes other than
528 * using this PMD, which is not supported in forked processes.
530 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
532 rte_eal_pci_register(&mlx5_driver.pci_drv);
536 static struct rte_driver rte_mlx5_driver = {
538 .name = MLX5_DRIVER_NAME,
539 .init = rte_mlx5_pmd_init,
542 PMD_REGISTER_DRIVER(rte_mlx5_driver)