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"
68 #include "mlx5_defs.h"
71 * DPDK callback to close the device.
73 * Destroy all queues and objects, free memory.
76 * Pointer to Ethernet device structure.
79 mlx5_dev_close(struct rte_eth_dev *dev)
81 struct priv *priv = dev->data->dev_private;
86 DEBUG("%p: closing device \"%s\"",
88 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
89 /* In case mlx5_dev_stop() has not been called. */
90 priv_dev_interrupt_handler_uninstall(priv, dev);
91 priv_special_flow_disable(priv, HASH_RXQ_FLOW_TYPE_ALLMULTI);
92 priv_special_flow_disable(priv, HASH_RXQ_FLOW_TYPE_PROMISC);
93 priv_special_flow_disable(priv, HASH_RXQ_FLOW_TYPE_BROADCAST);
94 priv_special_flow_disable(priv, HASH_RXQ_FLOW_TYPE_IPV6MULTI);
95 priv_mac_addrs_disable(priv);
96 priv_destroy_hash_rxqs(priv);
98 /* Remove flow director elements. */
99 priv_fdir_disable(priv);
100 priv_fdir_delete_filters_list(priv);
102 /* Prevent crashes when queues are still in use. */
103 dev->rx_pkt_burst = removed_rx_burst;
104 dev->tx_pkt_burst = removed_tx_burst;
105 if (priv->rxqs != NULL) {
106 /* XXX race condition if mlx5_rx_burst() is still running. */
108 for (i = 0; (i != priv->rxqs_n); ++i) {
109 tmp = (*priv->rxqs)[i];
112 (*priv->rxqs)[i] = NULL;
119 if (priv->txqs != NULL) {
120 /* XXX race condition if mlx5_tx_burst() is still running. */
122 for (i = 0; (i != priv->txqs_n); ++i) {
123 tmp = (*priv->txqs)[i];
126 (*priv->txqs)[i] = NULL;
133 if (priv->pd != NULL) {
134 assert(priv->ctx != NULL);
135 claim_zero(ibv_dealloc_pd(priv->pd));
136 claim_zero(ibv_close_device(priv->ctx));
138 assert(priv->ctx == NULL);
139 if (priv->rss_conf != NULL) {
140 for (i = 0; (i != hash_rxq_init_n); ++i)
141 rte_free((*priv->rss_conf)[i]);
142 rte_free(priv->rss_conf);
144 if (priv->reta_idx != NULL)
145 rte_free(priv->reta_idx);
147 memset(priv, 0, sizeof(*priv));
150 static const struct eth_dev_ops mlx5_dev_ops = {
151 .dev_configure = mlx5_dev_configure,
152 .dev_start = mlx5_dev_start,
153 .dev_stop = mlx5_dev_stop,
154 .dev_close = mlx5_dev_close,
155 .promiscuous_enable = mlx5_promiscuous_enable,
156 .promiscuous_disable = mlx5_promiscuous_disable,
157 .allmulticast_enable = mlx5_allmulticast_enable,
158 .allmulticast_disable = mlx5_allmulticast_disable,
159 .link_update = mlx5_link_update,
160 .stats_get = mlx5_stats_get,
161 .stats_reset = mlx5_stats_reset,
162 .dev_infos_get = mlx5_dev_infos_get,
163 .vlan_filter_set = mlx5_vlan_filter_set,
164 .rx_queue_setup = mlx5_rx_queue_setup,
165 .tx_queue_setup = mlx5_tx_queue_setup,
166 .rx_queue_release = mlx5_rx_queue_release,
167 .tx_queue_release = mlx5_tx_queue_release,
168 .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
169 .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
170 .mac_addr_remove = mlx5_mac_addr_remove,
171 .mac_addr_add = mlx5_mac_addr_add,
172 .mac_addr_set = mlx5_mac_addr_set,
173 .mtu_set = mlx5_dev_set_mtu,
174 #ifdef HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS
175 .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
176 .vlan_offload_set = mlx5_vlan_offload_set,
177 #endif /* HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS */
178 .reta_update = mlx5_dev_rss_reta_update,
179 .reta_query = mlx5_dev_rss_reta_query,
180 .rss_hash_update = mlx5_rss_hash_update,
181 .rss_hash_conf_get = mlx5_rss_hash_conf_get,
182 #ifdef MLX5_FDIR_SUPPORT
183 .filter_ctrl = mlx5_dev_filter_ctrl,
184 #endif /* MLX5_FDIR_SUPPORT */
188 struct rte_pci_addr pci_addr; /* associated PCI address */
189 uint32_t ports; /* physical ports bitfield. */
193 * Get device index in mlx5_dev[] from PCI bus address.
195 * @param[in] pci_addr
196 * PCI bus address to look for.
199 * mlx5_dev[] index on success, -1 on failure.
202 mlx5_dev_idx(struct rte_pci_addr *pci_addr)
207 assert(pci_addr != NULL);
208 for (i = 0; (i != RTE_DIM(mlx5_dev)); ++i) {
209 if ((mlx5_dev[i].pci_addr.domain == pci_addr->domain) &&
210 (mlx5_dev[i].pci_addr.bus == pci_addr->bus) &&
211 (mlx5_dev[i].pci_addr.devid == pci_addr->devid) &&
212 (mlx5_dev[i].pci_addr.function == pci_addr->function))
214 if ((mlx5_dev[i].ports == 0) && (ret == -1))
220 static struct eth_driver mlx5_driver;
223 * DPDK callback to register a PCI device.
225 * This function creates an Ethernet device for each port of a given
229 * PCI driver structure (mlx5_driver).
231 * PCI device information.
234 * 0 on success, negative errno value on failure.
237 mlx5_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
239 struct ibv_device **list;
240 struct ibv_device *ibv_dev;
242 struct ibv_context *attr_ctx = NULL;
243 struct ibv_device_attr device_attr;
249 assert(pci_drv == &mlx5_driver.pci_drv);
250 /* Get mlx5_dev[] index. */
251 idx = mlx5_dev_idx(&pci_dev->addr);
253 ERROR("this driver cannot support any more adapters");
256 DEBUG("using driver device index %d", idx);
258 /* Save PCI address. */
259 mlx5_dev[idx].pci_addr = pci_dev->addr;
260 list = ibv_get_device_list(&i);
263 if (errno == ENOSYS) {
264 WARN("cannot list devices, is ib_uverbs loaded?");
271 * For each listed device, check related sysfs entry against
272 * the provided PCI ID.
275 struct rte_pci_addr pci_addr;
278 DEBUG("checking device \"%s\"", list[i]->name);
279 if (mlx5_ibv_device_to_pci_addr(list[i], &pci_addr))
281 if ((pci_dev->addr.domain != pci_addr.domain) ||
282 (pci_dev->addr.bus != pci_addr.bus) ||
283 (pci_dev->addr.devid != pci_addr.devid) ||
284 (pci_dev->addr.function != pci_addr.function))
286 vf = ((pci_dev->id.device_id ==
287 PCI_DEVICE_ID_MELLANOX_CONNECTX4VF) ||
288 (pci_dev->id.device_id ==
289 PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF));
290 INFO("PCI information matches, using device \"%s\" (VF: %s)",
291 list[i]->name, (vf ? "true" : "false"));
292 attr_ctx = ibv_open_device(list[i]);
296 if (attr_ctx == NULL) {
297 ibv_free_device_list(list);
300 WARN("cannot access device, is mlx5_ib loaded?");
303 WARN("cannot use device, are drivers up to date?");
311 DEBUG("device opened");
312 if (ibv_query_device(attr_ctx, &device_attr))
314 INFO("%u port(s) detected", device_attr.phys_port_cnt);
316 for (i = 0; i < device_attr.phys_port_cnt; i++) {
317 uint32_t port = i + 1; /* ports are indexed from one */
318 uint32_t test = (1 << i);
319 struct ibv_context *ctx = NULL;
320 struct ibv_port_attr port_attr;
321 struct ibv_pd *pd = NULL;
322 struct priv *priv = NULL;
323 struct rte_eth_dev *eth_dev;
324 #ifdef HAVE_EXP_QUERY_DEVICE
325 struct ibv_exp_device_attr exp_device_attr;
326 #endif /* HAVE_EXP_QUERY_DEVICE */
327 struct ether_addr mac;
329 #ifdef HAVE_EXP_QUERY_DEVICE
330 exp_device_attr.comp_mask =
331 IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS |
332 IBV_EXP_DEVICE_ATTR_RX_HASH |
333 #ifdef HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS
334 IBV_EXP_DEVICE_ATTR_VLAN_OFFLOADS |
335 #endif /* HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS */
337 #endif /* HAVE_EXP_QUERY_DEVICE */
339 DEBUG("using port %u (%08" PRIx32 ")", port, test);
341 ctx = ibv_open_device(ibv_dev);
345 /* Check port status. */
346 err = ibv_query_port(ctx, port, &port_attr);
348 ERROR("port query failed: %s", strerror(err));
351 if (port_attr.state != IBV_PORT_ACTIVE)
352 DEBUG("port %d is not active: \"%s\" (%d)",
353 port, ibv_port_state_str(port_attr.state),
356 /* Allocate protection domain. */
357 pd = ibv_alloc_pd(ctx);
359 ERROR("PD allocation failure");
364 mlx5_dev[idx].ports |= test;
366 /* from rte_ethdev.c */
367 priv = rte_zmalloc("ethdev private structure",
369 RTE_CACHE_LINE_SIZE);
371 ERROR("priv allocation failure");
377 priv->device_attr = device_attr;
380 priv->mtu = ETHER_MTU;
381 #ifdef HAVE_EXP_QUERY_DEVICE
382 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
383 ERROR("ibv_exp_query_device() failed");
388 ((exp_device_attr.exp_device_cap_flags &
389 IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
390 (exp_device_attr.exp_device_cap_flags &
391 IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
392 DEBUG("checksum offloading is %ssupported",
393 (priv->hw_csum ? "" : "not "));
395 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
396 IBV_EXP_DEVICE_VXLAN_SUPPORT);
397 DEBUG("L2 tunnel checksum offloads are %ssupported",
398 (priv->hw_csum_l2tun ? "" : "not "));
400 priv->ind_table_max_size = exp_device_attr.rx_hash_caps.max_rwq_indirection_table_size;
401 /* Remove this check once DPDK supports larger/variable
402 * indirection tables. */
403 if (priv->ind_table_max_size > (unsigned int)RSS_INDIRECTION_TABLE_SIZE)
404 priv->ind_table_max_size = RSS_INDIRECTION_TABLE_SIZE;
405 DEBUG("maximum RX indirection table size is %u",
406 priv->ind_table_max_size);
407 #ifdef HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS
408 priv->hw_vlan_strip = !!(exp_device_attr.wq_vlan_offloads_cap &
409 IBV_EXP_RECEIVE_WQ_CVLAN_STRIP);
410 #endif /* HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS */
411 DEBUG("VLAN stripping is %ssupported",
412 (priv->hw_vlan_strip ? "" : "not "));
414 #else /* HAVE_EXP_QUERY_DEVICE */
415 priv->ind_table_max_size = RSS_INDIRECTION_TABLE_SIZE;
416 #endif /* HAVE_EXP_QUERY_DEVICE */
419 /* Allocate and register default RSS hash keys. */
420 priv->rss_conf = rte_calloc(__func__, hash_rxq_init_n,
421 sizeof((*priv->rss_conf)[0]), 0);
422 if (priv->rss_conf == NULL) {
426 err = rss_hash_rss_conf_new_key(priv,
427 rss_hash_default_key,
428 rss_hash_default_key_len,
432 /* Configure the first MAC address by default. */
433 if (priv_get_mac(priv, &mac.addr_bytes)) {
434 ERROR("cannot get MAC address, is mlx5_en loaded?"
435 " (errno: %s)", strerror(errno));
438 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
440 mac.addr_bytes[0], mac.addr_bytes[1],
441 mac.addr_bytes[2], mac.addr_bytes[3],
442 mac.addr_bytes[4], mac.addr_bytes[5]);
443 /* Register MAC address. */
444 claim_zero(priv_mac_addr_add(priv, 0,
445 (const uint8_t (*)[ETHER_ADDR_LEN])
447 /* Initialize FD filters list. */
448 err = fdir_init_filters_list(priv);
453 char ifname[IF_NAMESIZE];
455 if (priv_get_ifname(priv, &ifname) == 0)
456 DEBUG("port %u ifname is \"%s\"",
459 DEBUG("port %u ifname is unknown", priv->port);
462 /* Get actual MTU if possible. */
463 priv_get_mtu(priv, &priv->mtu);
464 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
466 /* from rte_ethdev.c */
468 char name[RTE_ETH_NAME_MAX_LEN];
470 snprintf(name, sizeof(name), "%s port %u",
471 ibv_get_device_name(ibv_dev), port);
472 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
474 if (eth_dev == NULL) {
475 ERROR("can not allocate rte ethdev");
480 eth_dev->data->dev_private = priv;
481 eth_dev->pci_dev = pci_dev;
483 rte_eth_copy_pci_info(eth_dev, pci_dev);
485 eth_dev->driver = &mlx5_driver;
486 eth_dev->data->rx_mbuf_alloc_failed = 0;
487 eth_dev->data->mtu = ETHER_MTU;
490 eth_dev->dev_ops = &mlx5_dev_ops;
491 eth_dev->data->mac_addrs = priv->mac;
492 TAILQ_INIT(ð_dev->link_intr_cbs);
494 /* Bring Ethernet device up. */
495 DEBUG("forcing Ethernet interface up");
496 priv_set_flags(priv, ~IFF_UP, IFF_UP);
501 rte_free(priv->rss_conf);
505 claim_zero(ibv_dealloc_pd(pd));
507 claim_zero(ibv_close_device(ctx));
512 * XXX if something went wrong in the loop above, there is a resource
513 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
514 * long as the dpdk does not provide a way to deallocate a ethdev and a
515 * way to enumerate the registered ethdevs to free the previous ones.
518 /* no port found, complain */
519 if (!mlx5_dev[idx].ports) {
526 claim_zero(ibv_close_device(attr_ctx));
528 ibv_free_device_list(list);
533 static const struct rte_pci_id mlx5_pci_id_map[] = {
535 .vendor_id = PCI_VENDOR_ID_MELLANOX,
536 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4,
537 .subsystem_vendor_id = PCI_ANY_ID,
538 .subsystem_device_id = PCI_ANY_ID
541 .vendor_id = PCI_VENDOR_ID_MELLANOX,
542 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4VF,
543 .subsystem_vendor_id = PCI_ANY_ID,
544 .subsystem_device_id = PCI_ANY_ID
547 .vendor_id = PCI_VENDOR_ID_MELLANOX,
548 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4LX,
549 .subsystem_vendor_id = PCI_ANY_ID,
550 .subsystem_device_id = PCI_ANY_ID
553 .vendor_id = PCI_VENDOR_ID_MELLANOX,
554 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF,
555 .subsystem_vendor_id = PCI_ANY_ID,
556 .subsystem_device_id = PCI_ANY_ID
563 static struct eth_driver mlx5_driver = {
565 .name = MLX5_DRIVER_NAME,
566 .id_table = mlx5_pci_id_map,
567 .devinit = mlx5_pci_devinit,
568 .drv_flags = RTE_PCI_DRV_INTR_LSC,
570 .dev_private_size = sizeof(struct priv)
574 * Driver initialization routine.
577 rte_mlx5_pmd_init(const char *name, const char *args)
582 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
583 * huge pages. Calling ibv_fork_init() during init allows
584 * applications to use fork() safely for purposes other than
585 * using this PMD, which is not supported in forked processes.
587 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
589 rte_eal_pci_register(&mlx5_driver.pci_drv);
593 static struct rte_driver rte_mlx5_driver = {
595 .name = MLX5_DRIVER_NAME,
596 .init = rte_mlx5_pmd_init,
599 PMD_REGISTER_DRIVER(rte_mlx5_driver)