4 * Copyright 2012 6WIND S.A.
5 * Copyright 2012 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.
36 * mlx4 driver initialization.
48 /* Verbs headers do not support -pedantic. */
50 #pragma GCC diagnostic ignored "-Wpedantic"
52 #include <infiniband/verbs.h>
54 #pragma GCC diagnostic error "-Wpedantic"
57 #include <rte_common.h>
59 #include <rte_errno.h>
60 #include <rte_ethdev.h>
61 #include <rte_ethdev_pci.h>
62 #include <rte_ether.h>
64 #include <rte_interrupts.h>
65 #include <rte_kvargs.h>
66 #include <rte_malloc.h>
70 #include "mlx4_flow.h"
71 #include "mlx4_rxtx.h"
72 #include "mlx4_utils.h"
74 /** Configuration structure for device arguments. */
77 uint32_t present; /**< Bit-field for existing ports. */
78 uint32_t enabled; /**< Bit-field for user-enabled ports. */
82 /* Available parameters list. */
83 const char *pmd_mlx4_init_params[] = {
89 * DPDK callback for Ethernet device configuration.
92 * Pointer to Ethernet device structure.
95 * 0 on success, negative errno value otherwise and rte_errno is set.
98 mlx4_dev_configure(struct rte_eth_dev *dev)
100 struct priv *priv = dev->data->dev_private;
101 struct rte_flow_error error;
104 /* Prepare internal flow rules. */
105 ret = mlx4_flow_sync(priv, &error);
107 ERROR("cannot set up internal flow rules (code %d, \"%s\"),"
108 " flow error type %d, cause %p, message: %s",
109 -ret, strerror(-ret), error.type, error.cause,
110 error.message ? error.message : "(unspecified)");
116 * DPDK callback to start the device.
118 * Simulate device start by attaching all configured flows.
121 * Pointer to Ethernet device structure.
124 * 0 on success, negative errno value otherwise and rte_errno is set.
127 mlx4_dev_start(struct rte_eth_dev *dev)
129 struct priv *priv = dev->data->dev_private;
130 struct rte_flow_error error;
135 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
137 ret = mlx4_intr_install(priv);
139 ERROR("%p: interrupt handler installation failed",
143 ret = mlx4_flow_sync(priv, &error);
145 ERROR("%p: cannot attach flow rules (code %d, \"%s\"),"
146 " flow error type %d, cause %p, message: %s",
148 -ret, strerror(-ret), error.type, error.cause,
149 error.message ? error.message : "(unspecified)");
160 * DPDK callback to stop the device.
162 * Simulate device stop by detaching all configured flows.
165 * Pointer to Ethernet device structure.
168 mlx4_dev_stop(struct rte_eth_dev *dev)
170 struct priv *priv = dev->data->dev_private;
174 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
176 mlx4_flow_sync(priv, NULL);
177 mlx4_intr_uninstall(priv);
181 * DPDK callback to close the device.
183 * Destroy all queues and objects, free memory.
186 * Pointer to Ethernet device structure.
189 mlx4_dev_close(struct rte_eth_dev *dev)
191 struct priv *priv = dev->data->dev_private;
196 DEBUG("%p: closing device \"%s\"",
198 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
199 mlx4_flow_clean(priv);
200 dev->rx_pkt_burst = mlx4_rx_burst_removed;
201 dev->tx_pkt_burst = mlx4_tx_burst_removed;
202 for (i = 0; i != dev->data->nb_rx_queues; ++i)
203 mlx4_rx_queue_release(dev->data->rx_queues[i]);
204 for (i = 0; i != dev->data->nb_tx_queues; ++i)
205 mlx4_tx_queue_release(dev->data->tx_queues[i]);
206 if (priv->pd != NULL) {
207 assert(priv->ctx != NULL);
208 claim_zero(ibv_dealloc_pd(priv->pd));
209 claim_zero(ibv_close_device(priv->ctx));
211 assert(priv->ctx == NULL);
212 mlx4_intr_uninstall(priv);
213 memset(priv, 0, sizeof(*priv));
216 static const struct eth_dev_ops mlx4_dev_ops = {
217 .dev_configure = mlx4_dev_configure,
218 .dev_start = mlx4_dev_start,
219 .dev_stop = mlx4_dev_stop,
220 .dev_set_link_down = mlx4_dev_set_link_down,
221 .dev_set_link_up = mlx4_dev_set_link_up,
222 .dev_close = mlx4_dev_close,
223 .link_update = mlx4_link_update,
224 .mac_addr_remove = mlx4_mac_addr_remove,
225 .mac_addr_add = mlx4_mac_addr_add,
226 .mac_addr_set = mlx4_mac_addr_set,
227 .stats_get = mlx4_stats_get,
228 .stats_reset = mlx4_stats_reset,
229 .dev_infos_get = mlx4_dev_infos_get,
230 .rx_queue_setup = mlx4_rx_queue_setup,
231 .tx_queue_setup = mlx4_tx_queue_setup,
232 .rx_queue_release = mlx4_rx_queue_release,
233 .tx_queue_release = mlx4_tx_queue_release,
234 .flow_ctrl_get = mlx4_flow_ctrl_get,
235 .flow_ctrl_set = mlx4_flow_ctrl_set,
236 .mtu_set = mlx4_mtu_set,
237 .filter_ctrl = mlx4_filter_ctrl,
238 .rx_queue_intr_enable = mlx4_rx_intr_enable,
239 .rx_queue_intr_disable = mlx4_rx_intr_disable,
243 * Get PCI information from struct ibv_device.
246 * Pointer to Ethernet device structure.
247 * @param[out] pci_addr
248 * PCI bus address output buffer.
251 * 0 on success, negative errno value otherwise and rte_errno is set.
254 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
255 struct rte_pci_addr *pci_addr)
259 MKSTR(path, "%s/device/uevent", device->ibdev_path);
261 file = fopen(path, "rb");
266 while (fgets(line, sizeof(line), file) == line) {
267 size_t len = strlen(line);
270 /* Truncate long lines. */
271 if (len == (sizeof(line) - 1))
272 while (line[(len - 1)] != '\n') {
276 line[(len - 1)] = ret;
278 /* Extract information. */
281 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
285 &pci_addr->function) == 4) {
295 * Verify and store value for device argument.
298 * Key argument to verify.
300 * Value associated with key.
301 * @param[in, out] conf
302 * Shared configuration data.
305 * 0 on success, negative errno value otherwise and rte_errno is set.
308 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
313 tmp = strtoul(val, NULL, 0);
316 WARN("%s: \"%s\" is not a valid integer", key, val);
319 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
320 uint32_t ports = rte_log2_u32(conf->ports.present);
323 ERROR("port index %lu outside range [0,%" PRIu32 ")",
327 if (!(conf->ports.present & (1 << tmp))) {
329 ERROR("invalid port index %lu", tmp);
332 conf->ports.enabled |= 1 << tmp;
335 WARN("%s: unknown parameter", key);
342 * Parse device parameters.
345 * Device arguments structure.
348 * 0 on success, negative errno value otherwise and rte_errno is set.
351 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
353 struct rte_kvargs *kvlist;
354 unsigned int arg_count;
360 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
361 if (kvlist == NULL) {
363 ERROR("failed to parse kvargs");
366 /* Process parameters. */
367 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
368 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
369 while (arg_count-- > 0) {
370 ret = rte_kvargs_process(kvlist,
372 (int (*)(const char *,
382 rte_kvargs_free(kvlist);
386 static struct rte_pci_driver mlx4_driver;
389 * DPDK callback to register a PCI device.
391 * This function creates an Ethernet device for each port of a given
395 * PCI driver structure (mlx4_driver).
397 * PCI device information.
400 * 0 on success, negative errno value otherwise and rte_errno is set.
403 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
405 struct ibv_device **list;
406 struct ibv_device *ibv_dev;
408 struct ibv_context *attr_ctx = NULL;
409 struct ibv_device_attr device_attr;
410 struct mlx4_conf conf = {
417 assert(pci_drv == &mlx4_driver);
418 list = ibv_get_device_list(&i);
422 if (rte_errno == ENOSYS)
423 ERROR("cannot list devices, is ib_uverbs loaded?");
428 * For each listed device, check related sysfs entry against
429 * the provided PCI ID.
432 struct rte_pci_addr pci_addr;
435 DEBUG("checking device \"%s\"", list[i]->name);
436 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
438 if ((pci_dev->addr.domain != pci_addr.domain) ||
439 (pci_dev->addr.bus != pci_addr.bus) ||
440 (pci_dev->addr.devid != pci_addr.devid) ||
441 (pci_dev->addr.function != pci_addr.function))
443 vf = (pci_dev->id.device_id ==
444 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
445 INFO("PCI information matches, using device \"%s\" (VF: %s)",
446 list[i]->name, (vf ? "true" : "false"));
447 attr_ctx = ibv_open_device(list[i]);
451 if (attr_ctx == NULL) {
452 ibv_free_device_list(list);
456 ERROR("cannot access device, is mlx4_ib loaded?");
460 ERROR("cannot use device, are drivers up to date?");
468 DEBUG("device opened");
469 if (ibv_query_device(attr_ctx, &device_attr)) {
473 INFO("%u port(s) detected", device_attr.phys_port_cnt);
474 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
475 if (mlx4_args(pci_dev->device.devargs, &conf)) {
476 ERROR("failed to process device arguments");
480 /* Use all ports when none are defined */
481 if (!conf.ports.enabled)
482 conf.ports.enabled = conf.ports.present;
483 for (i = 0; i < device_attr.phys_port_cnt; i++) {
484 uint32_t port = i + 1; /* ports are indexed from one */
485 struct ibv_context *ctx = NULL;
486 struct ibv_port_attr port_attr;
487 struct ibv_pd *pd = NULL;
488 struct priv *priv = NULL;
489 struct rte_eth_dev *eth_dev = NULL;
490 struct ether_addr mac;
492 /* If port is not enabled, skip. */
493 if (!(conf.ports.enabled & (1 << i)))
495 DEBUG("using port %u", port);
496 ctx = ibv_open_device(ibv_dev);
501 /* Check port status. */
502 err = ibv_query_port(ctx, port, &port_attr);
505 ERROR("port query failed: %s", strerror(rte_errno));
508 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
510 ERROR("port %d is not configured in Ethernet mode",
514 if (port_attr.state != IBV_PORT_ACTIVE)
515 DEBUG("port %d is not active: \"%s\" (%d)",
516 port, ibv_port_state_str(port_attr.state),
518 /* Make asynchronous FD non-blocking to handle interrupts. */
519 if (mlx4_fd_set_non_blocking(ctx->async_fd) < 0) {
520 ERROR("cannot make asynchronous FD non-blocking: %s",
521 strerror(rte_errno));
524 /* Allocate protection domain. */
525 pd = ibv_alloc_pd(ctx);
528 ERROR("PD allocation failure");
531 /* from rte_ethdev.c */
532 priv = rte_zmalloc("ethdev private structure",
534 RTE_CACHE_LINE_SIZE);
537 ERROR("priv allocation failure");
541 priv->device_attr = device_attr;
544 priv->mtu = ETHER_MTU;
546 /* Configure the first MAC address by default. */
547 if (mlx4_get_mac(priv, &mac.addr_bytes)) {
548 ERROR("cannot get MAC address, is mlx4_en loaded?"
549 " (rte_errno: %s)", strerror(rte_errno));
552 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
554 mac.addr_bytes[0], mac.addr_bytes[1],
555 mac.addr_bytes[2], mac.addr_bytes[3],
556 mac.addr_bytes[4], mac.addr_bytes[5]);
557 /* Register MAC address. */
561 char ifname[IF_NAMESIZE];
563 if (mlx4_get_ifname(priv, &ifname) == 0)
564 DEBUG("port %u ifname is \"%s\"",
567 DEBUG("port %u ifname is unknown", priv->port);
570 /* Get actual MTU if possible. */
571 mlx4_mtu_get(priv, &priv->mtu);
572 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);
581 if (eth_dev == NULL) {
582 ERROR("can not allocate rte ethdev");
586 eth_dev->data->dev_private = priv;
587 eth_dev->data->mac_addrs = priv->mac;
588 eth_dev->device = &pci_dev->device;
589 rte_eth_copy_pci_info(eth_dev, pci_dev);
590 eth_dev->device->driver = &mlx4_driver.driver;
591 /* Initialize local interrupt handle for current port. */
592 priv->intr_handle = (struct rte_intr_handle){
594 .type = RTE_INTR_HANDLE_EXT,
597 * Override ethdev interrupt handle pointer with private
598 * handle instead of that of the parent PCI device used by
599 * default. This prevents it from being shared between all
600 * ports of the same PCI device since each of them is
601 * associated its own Verbs context.
603 * Rx interrupts in particular require this as the PMD has
604 * no control over the registration of queue interrupts
605 * besides setting up eth_dev->intr_handle, the rest is
606 * handled by rte_intr_rx_ctl().
608 eth_dev->intr_handle = &priv->intr_handle;
610 eth_dev->dev_ops = &mlx4_dev_ops;
611 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
612 /* Bring Ethernet device up. */
613 DEBUG("forcing Ethernet interface up");
614 mlx4_dev_set_link_up(priv->dev);
615 /* Update link status once if waiting for LSC. */
616 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
617 mlx4_link_update(eth_dev, 0);
622 claim_zero(ibv_dealloc_pd(pd));
624 claim_zero(ibv_close_device(ctx));
626 rte_eth_dev_release_port(eth_dev);
629 if (i == device_attr.phys_port_cnt)
632 * XXX if something went wrong in the loop above, there is a resource
633 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
634 * long as the dpdk does not provide a way to deallocate a ethdev and a
635 * way to enumerate the registered ethdevs to free the previous ones.
639 claim_zero(ibv_close_device(attr_ctx));
641 ibv_free_device_list(list);
642 assert(rte_errno >= 0);
646 static const struct rte_pci_id mlx4_pci_id_map[] = {
648 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
649 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
652 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
653 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
656 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
657 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
664 static struct rte_pci_driver mlx4_driver = {
666 .name = MLX4_DRIVER_NAME
668 .id_table = mlx4_pci_id_map,
669 .probe = mlx4_pci_probe,
670 .drv_flags = RTE_PCI_DRV_INTR_LSC |
671 RTE_PCI_DRV_INTR_RMV,
675 * Driver initialization routine.
677 RTE_INIT(rte_mlx4_pmd_init);
679 rte_mlx4_pmd_init(void)
682 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
683 * huge pages. Calling ibv_fork_init() during init allows
684 * applications to use fork() safely for purposes other than
685 * using this PMD, which is not supported in forked processes.
687 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
689 rte_pci_register(&mlx4_driver);
692 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
693 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
694 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
695 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");