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.
45 #include <rte_ether.h>
46 #include <rte_ethdev.h>
47 #include <rte_ethdev_pci.h>
50 #include <rte_errno.h>
51 #include <rte_malloc.h>
52 #include <rte_kvargs.h>
53 #include <rte_interrupts.h>
54 #include <rte_common.h>
58 #include "mlx4_flow.h"
59 #include "mlx4_rxtx.h"
60 #include "mlx4_utils.h"
62 /** Configuration structure for device arguments. */
65 uint32_t present; /**< Bit-field for existing ports. */
66 uint32_t enabled; /**< Bit-field for user-enabled ports. */
70 /* Available parameters list. */
71 const char *pmd_mlx4_init_params[] = {
76 /* Device configuration. */
79 * DPDK callback for Ethernet device configuration.
81 * Prepare the driver for a given number of TX and RX queues.
84 * Pointer to Ethernet device structure.
87 * 0 on success, negative errno value otherwise and rte_errno is set.
90 mlx4_dev_configure(struct rte_eth_dev *dev)
92 struct priv *priv = dev->data->dev_private;
93 unsigned int rxqs_n = dev->data->nb_rx_queues;
94 unsigned int txqs_n = dev->data->nb_tx_queues;
96 priv->rxqs = (void *)dev->data->rx_queues;
97 priv->txqs = (void *)dev->data->tx_queues;
98 if (txqs_n != priv->txqs_n) {
99 INFO("%p: TX queues number update: %u -> %u",
100 (void *)dev, priv->txqs_n, txqs_n);
101 priv->txqs_n = txqs_n;
103 if (rxqs_n != priv->rxqs_n) {
104 INFO("%p: Rx queues number update: %u -> %u",
105 (void *)dev, priv->rxqs_n, rxqs_n);
106 priv->rxqs_n = rxqs_n;
112 * DPDK callback to start the device.
114 * Simulate device start by attaching all configured flows.
117 * Pointer to Ethernet device structure.
120 * 0 on success, negative errno value otherwise and rte_errno is set.
123 mlx4_dev_start(struct rte_eth_dev *dev)
125 struct priv *priv = dev->data->dev_private;
130 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
132 ret = mlx4_mac_addr_add(priv);
135 ret = mlx4_intr_install(priv);
137 ERROR("%p: interrupt handler installation failed",
141 ret = mlx4_flow_start(priv);
143 ERROR("%p: flow start failed: %s",
144 (void *)dev, strerror(ret));
150 mlx4_mac_addr_del(priv);
156 * DPDK callback to stop the device.
158 * Simulate device stop by detaching all configured flows.
161 * Pointer to Ethernet device structure.
164 mlx4_dev_stop(struct rte_eth_dev *dev)
166 struct priv *priv = dev->data->dev_private;
170 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
172 mlx4_flow_stop(priv);
173 mlx4_intr_uninstall(priv);
174 mlx4_mac_addr_del(priv);
178 * DPDK callback to close the device.
180 * Destroy all queues and objects, free memory.
183 * Pointer to Ethernet device structure.
186 mlx4_dev_close(struct rte_eth_dev *dev)
188 struct priv *priv = dev->data->dev_private;
194 DEBUG("%p: closing device \"%s\"",
196 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
197 mlx4_mac_addr_del(priv);
199 * Prevent crashes when queues are still in use. This is unfortunately
200 * still required for DPDK 1.3 because some programs (such as testpmd)
201 * never release them before closing the device.
203 dev->rx_pkt_burst = mlx4_rx_burst_removed;
204 dev->tx_pkt_burst = mlx4_tx_burst_removed;
205 if (priv->rxqs != NULL) {
206 /* XXX race condition if mlx4_rx_burst() is still running. */
208 for (i = 0; (i != priv->rxqs_n); ++i) {
209 tmp = (*priv->rxqs)[i];
212 (*priv->rxqs)[i] = NULL;
213 mlx4_rxq_cleanup(tmp);
219 if (priv->txqs != NULL) {
220 /* XXX race condition if mlx4_tx_burst() is still running. */
222 for (i = 0; (i != priv->txqs_n); ++i) {
223 tmp = (*priv->txqs)[i];
226 (*priv->txqs)[i] = NULL;
227 mlx4_txq_cleanup(tmp);
233 if (priv->pd != NULL) {
234 assert(priv->ctx != NULL);
235 claim_zero(ibv_dealloc_pd(priv->pd));
236 claim_zero(ibv_close_device(priv->ctx));
238 assert(priv->ctx == NULL);
239 mlx4_intr_uninstall(priv);
240 memset(priv, 0, sizeof(*priv));
243 static const struct eth_dev_ops mlx4_dev_ops = {
244 .dev_configure = mlx4_dev_configure,
245 .dev_start = mlx4_dev_start,
246 .dev_stop = mlx4_dev_stop,
247 .dev_set_link_down = mlx4_dev_set_link_down,
248 .dev_set_link_up = mlx4_dev_set_link_up,
249 .dev_close = mlx4_dev_close,
250 .link_update = mlx4_link_update,
251 .stats_get = mlx4_stats_get,
252 .stats_reset = mlx4_stats_reset,
253 .dev_infos_get = mlx4_dev_infos_get,
254 .rx_queue_setup = mlx4_rx_queue_setup,
255 .tx_queue_setup = mlx4_tx_queue_setup,
256 .rx_queue_release = mlx4_rx_queue_release,
257 .tx_queue_release = mlx4_tx_queue_release,
258 .flow_ctrl_get = mlx4_flow_ctrl_get,
259 .flow_ctrl_set = mlx4_flow_ctrl_set,
260 .mtu_set = mlx4_mtu_set,
261 .filter_ctrl = mlx4_filter_ctrl,
262 .rx_queue_intr_enable = mlx4_rx_intr_enable,
263 .rx_queue_intr_disable = mlx4_rx_intr_disable,
267 * Get PCI information from struct ibv_device.
270 * Pointer to Ethernet device structure.
271 * @param[out] pci_addr
272 * PCI bus address output buffer.
275 * 0 on success, negative errno value otherwise and rte_errno is set.
278 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
279 struct rte_pci_addr *pci_addr)
283 MKSTR(path, "%s/device/uevent", device->ibdev_path);
285 file = fopen(path, "rb");
290 while (fgets(line, sizeof(line), file) == line) {
291 size_t len = strlen(line);
294 /* Truncate long lines. */
295 if (len == (sizeof(line) - 1))
296 while (line[(len - 1)] != '\n') {
300 line[(len - 1)] = ret;
302 /* Extract information. */
305 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
309 &pci_addr->function) == 4) {
319 * Verify and store value for device argument.
322 * Key argument to verify.
324 * Value associated with key.
325 * @param[in, out] conf
326 * Shared configuration data.
329 * 0 on success, negative errno value otherwise and rte_errno is set.
332 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
337 tmp = strtoul(val, NULL, 0);
340 WARN("%s: \"%s\" is not a valid integer", key, val);
343 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
344 uint32_t ports = rte_log2_u32(conf->ports.present);
347 ERROR("port index %lu outside range [0,%" PRIu32 ")",
351 if (!(conf->ports.present & (1 << tmp))) {
353 ERROR("invalid port index %lu", tmp);
356 conf->ports.enabled |= 1 << tmp;
359 WARN("%s: unknown parameter", key);
366 * Parse device parameters.
369 * Device arguments structure.
372 * 0 on success, negative errno value otherwise and rte_errno is set.
375 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
377 struct rte_kvargs *kvlist;
378 unsigned int arg_count;
384 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
385 if (kvlist == NULL) {
387 ERROR("failed to parse kvargs");
390 /* Process parameters. */
391 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
392 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
393 while (arg_count-- > 0) {
394 ret = rte_kvargs_process(kvlist,
396 (int (*)(const char *,
406 rte_kvargs_free(kvlist);
410 static struct rte_pci_driver mlx4_driver;
413 * DPDK callback to register a PCI device.
415 * This function creates an Ethernet device for each port of a given
419 * PCI driver structure (mlx4_driver).
421 * PCI device information.
424 * 0 on success, negative errno value otherwise and rte_errno is set.
427 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
429 struct ibv_device **list;
430 struct ibv_device *ibv_dev;
432 struct ibv_context *attr_ctx = NULL;
433 struct ibv_device_attr device_attr;
434 struct mlx4_conf conf = {
441 assert(pci_drv == &mlx4_driver);
442 list = ibv_get_device_list(&i);
446 if (rte_errno == ENOSYS)
447 ERROR("cannot list devices, is ib_uverbs loaded?");
452 * For each listed device, check related sysfs entry against
453 * the provided PCI ID.
456 struct rte_pci_addr pci_addr;
459 DEBUG("checking device \"%s\"", list[i]->name);
460 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
462 if ((pci_dev->addr.domain != pci_addr.domain) ||
463 (pci_dev->addr.bus != pci_addr.bus) ||
464 (pci_dev->addr.devid != pci_addr.devid) ||
465 (pci_dev->addr.function != pci_addr.function))
467 vf = (pci_dev->id.device_id ==
468 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
469 INFO("PCI information matches, using device \"%s\" (VF: %s)",
470 list[i]->name, (vf ? "true" : "false"));
471 attr_ctx = ibv_open_device(list[i]);
475 if (attr_ctx == NULL) {
476 ibv_free_device_list(list);
480 ERROR("cannot access device, is mlx4_ib loaded?");
484 ERROR("cannot use device, are drivers up to date?");
492 DEBUG("device opened");
493 if (ibv_query_device(attr_ctx, &device_attr)) {
497 INFO("%u port(s) detected", device_attr.phys_port_cnt);
498 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
499 if (mlx4_args(pci_dev->device.devargs, &conf)) {
500 ERROR("failed to process device arguments");
504 /* Use all ports when none are defined */
505 if (!conf.ports.enabled)
506 conf.ports.enabled = conf.ports.present;
507 for (i = 0; i < device_attr.phys_port_cnt; i++) {
508 uint32_t port = i + 1; /* ports are indexed from one */
509 struct ibv_context *ctx = NULL;
510 struct ibv_port_attr port_attr;
511 struct ibv_pd *pd = NULL;
512 struct priv *priv = NULL;
513 struct rte_eth_dev *eth_dev = NULL;
514 struct ether_addr mac;
516 /* If port is not enabled, skip. */
517 if (!(conf.ports.enabled & (1 << i)))
519 DEBUG("using port %u", port);
520 ctx = ibv_open_device(ibv_dev);
525 /* Check port status. */
526 err = ibv_query_port(ctx, port, &port_attr);
529 ERROR("port query failed: %s", strerror(rte_errno));
532 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
534 ERROR("port %d is not configured in Ethernet mode",
538 if (port_attr.state != IBV_PORT_ACTIVE)
539 DEBUG("port %d is not active: \"%s\" (%d)",
540 port, ibv_port_state_str(port_attr.state),
542 /* Make asynchronous FD non-blocking to handle interrupts. */
543 if (mlx4_fd_set_non_blocking(ctx->async_fd) < 0) {
544 ERROR("cannot make asynchronous FD non-blocking: %s",
545 strerror(rte_errno));
548 /* Allocate protection domain. */
549 pd = ibv_alloc_pd(ctx);
552 ERROR("PD allocation failure");
555 /* from rte_ethdev.c */
556 priv = rte_zmalloc("ethdev private structure",
558 RTE_CACHE_LINE_SIZE);
561 ERROR("priv allocation failure");
565 priv->device_attr = device_attr;
568 priv->mtu = ETHER_MTU;
570 /* Configure the first MAC address by default. */
571 if (mlx4_get_mac(priv, &mac.addr_bytes)) {
572 ERROR("cannot get MAC address, is mlx4_en loaded?"
573 " (rte_errno: %s)", strerror(rte_errno));
576 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
578 mac.addr_bytes[0], mac.addr_bytes[1],
579 mac.addr_bytes[2], mac.addr_bytes[3],
580 mac.addr_bytes[4], mac.addr_bytes[5]);
581 /* Register MAC address. */
583 if (mlx4_mac_addr_add(priv))
587 char ifname[IF_NAMESIZE];
589 if (mlx4_get_ifname(priv, &ifname) == 0)
590 DEBUG("port %u ifname is \"%s\"",
593 DEBUG("port %u ifname is unknown", priv->port);
596 /* Get actual MTU if possible. */
597 mlx4_mtu_get(priv, &priv->mtu);
598 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
599 /* from rte_ethdev.c */
601 char name[RTE_ETH_NAME_MAX_LEN];
603 snprintf(name, sizeof(name), "%s port %u",
604 ibv_get_device_name(ibv_dev), port);
605 eth_dev = rte_eth_dev_allocate(name);
607 if (eth_dev == NULL) {
608 ERROR("can not allocate rte ethdev");
612 eth_dev->data->dev_private = priv;
613 eth_dev->data->mac_addrs = &priv->mac;
614 eth_dev->device = &pci_dev->device;
615 rte_eth_copy_pci_info(eth_dev, pci_dev);
616 eth_dev->device->driver = &mlx4_driver.driver;
617 /* Initialize local interrupt handle for current port. */
618 priv->intr_handle = (struct rte_intr_handle){
620 .type = RTE_INTR_HANDLE_EXT,
623 * Override ethdev interrupt handle pointer with private
624 * handle instead of that of the parent PCI device used by
625 * default. This prevents it from being shared between all
626 * ports of the same PCI device since each of them is
627 * associated its own Verbs context.
629 * Rx interrupts in particular require this as the PMD has
630 * no control over the registration of queue interrupts
631 * besides setting up eth_dev->intr_handle, the rest is
632 * handled by rte_intr_rx_ctl().
634 eth_dev->intr_handle = &priv->intr_handle;
636 eth_dev->dev_ops = &mlx4_dev_ops;
637 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
638 /* Bring Ethernet device up. */
639 DEBUG("forcing Ethernet interface up");
640 mlx4_dev_set_link_up(priv->dev);
641 /* Update link status once if waiting for LSC. */
642 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
643 mlx4_link_update(eth_dev, 0);
648 claim_zero(ibv_dealloc_pd(pd));
650 claim_zero(ibv_close_device(ctx));
652 rte_eth_dev_release_port(eth_dev);
655 if (i == device_attr.phys_port_cnt)
658 * XXX if something went wrong in the loop above, there is a resource
659 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
660 * long as the dpdk does not provide a way to deallocate a ethdev and a
661 * way to enumerate the registered ethdevs to free the previous ones.
665 claim_zero(ibv_close_device(attr_ctx));
667 ibv_free_device_list(list);
668 assert(rte_errno >= 0);
672 static const struct rte_pci_id mlx4_pci_id_map[] = {
674 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
675 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
678 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
679 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
682 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
683 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
690 static struct rte_pci_driver mlx4_driver = {
692 .name = MLX4_DRIVER_NAME
694 .id_table = mlx4_pci_id_map,
695 .probe = mlx4_pci_probe,
696 .drv_flags = RTE_PCI_DRV_INTR_LSC |
697 RTE_PCI_DRV_INTR_RMV,
701 * Driver initialization routine.
703 RTE_INIT(rte_mlx4_pmd_init);
705 rte_mlx4_pmd_init(void)
708 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
709 * huge pages. Calling ibv_fork_init() during init allows
710 * applications to use fork() safely for purposes other than
711 * using this PMD, which is not supported in forked processes.
713 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
715 rte_pci_register(&mlx4_driver);
718 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
719 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
720 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
721 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");