net/mlx4: implement isolated mode from flow API
[dpdk.git] / drivers / net / mlx4 / mlx4.c
index ec4419a..cd828cb 100644 (file)
@@ -75,6 +75,7 @@
 #include <rte_memory.h>
 #include <rte_flow.h>
 #include <rte_kvargs.h>
+#include <rte_interrupts.h>
 
 /* Generated configuration header. */
 #include "mlx4_autoconf.h"
@@ -127,6 +128,18 @@ const char *pmd_mlx4_init_params[] = {
        NULL,
 };
 
+static int
+mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx);
+
+static int
+mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx);
+
+static int
+priv_rx_intr_vec_enable(struct priv *priv);
+
+static void
+priv_rx_intr_vec_disable(struct priv *priv);
+
 /**
  * Check if running as a secondary process.
  *
@@ -569,7 +582,7 @@ dev_configure(struct rte_eth_dev *dev)
        }
        if (rxqs_n == priv->rxqs_n)
                return 0;
-       if (!rte_is_power_of_2(rxqs_n)) {
+       if (!rte_is_power_of_2(rxqs_n) && !priv->isolated) {
                unsigned n_active;
 
                n_active = rte_align32pow2(rxqs_n + 1) >> 1;
@@ -1098,7 +1111,7 @@ static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
 
 /* For best performance, this function should not be inlined. */
 static struct ibv_mr *mlx4_mp2mr(struct ibv_pd *, struct rte_mempool *)
-       __attribute__((noinline));
+       __rte_noinline;
 
 /**
  * Register mempool as a memory region.
@@ -2499,6 +2512,7 @@ priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
 {
        unsigned int i;
 
+       assert(!priv->isolated);
        assert(mac_index < elemof(priv->mac));
        if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
                return;
@@ -2748,14 +2762,17 @@ rxq_cleanup(struct rxq *rxq)
                                                rxq->if_cq,
                                                &params));
        }
-       if (rxq->qp != NULL) {
+       if (rxq->qp != NULL && !rxq->priv->isolated) {
                rxq_promiscuous_disable(rxq);
                rxq_allmulticast_disable(rxq);
                rxq_mac_addrs_del(rxq);
-               claim_zero(ibv_destroy_qp(rxq->qp));
        }
+       if (rxq->qp != NULL)
+               claim_zero(ibv_destroy_qp(rxq->qp));
        if (rxq->cq != NULL)
                claim_zero(ibv_destroy_cq(rxq->cq));
+       if (rxq->channel != NULL)
+               claim_zero(ibv_destroy_comp_channel(rxq->channel));
        if (rxq->rd != NULL) {
                struct ibv_exp_destroy_res_domain_attr attr = {
                        .comp_mask = 0,
@@ -2987,6 +3004,13 @@ mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
                        NB_SEGS(rep) = 0x2a;
                        PORT(rep) = 0x2a;
                        rep->ol_flags = -1;
+                       /*
+                        * Clear special flags in mbuf to avoid
+                        * crashing while freeing.
+                        */
+                       rep->ol_flags &=
+                               ~(uint64_t)(IND_ATTACHED_MBUF |
+                                           CTRL_MBUF_FLAG);
 #endif
                        assert(rep->buf_len == seg->buf_len);
                        /* Reconfigure sge to use rep instead of seg. */
@@ -3451,7 +3475,7 @@ rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
                return 0;
        }
        /* Remove attached flows if RSS is disabled (no parent queue). */
-       if (!priv->rss) {
+       if (!priv->rss && !priv->isolated) {
                rxq_allmulticast_disable(&tmpl);
                rxq_promiscuous_disable(&tmpl);
                rxq_mac_addrs_del(&tmpl);
@@ -3496,7 +3520,7 @@ rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
                return err;
        };
        /* Reconfigure flows. Do not care for errors. */
-       if (!priv->rss) {
+       if (!priv->rss && !priv->isolated) {
                rxq_mac_addrs_add(&tmpl);
                if (priv->promisc)
                        rxq_promiscuous_enable(&tmpl);
@@ -3696,11 +3720,22 @@ skip_mr:
                      (void *)dev, strerror(ret));
                goto error;
        }
+       if (dev->data->dev_conf.intr_conf.rxq) {
+               tmpl.channel = ibv_create_comp_channel(priv->ctx);
+               if (tmpl.channel == NULL) {
+                       ret = ENOMEM;
+                       ERROR("%p: Rx interrupt completion channel creation"
+                             " failure: %s",
+                             (void *)dev, strerror(ret));
+                       goto error;
+               }
+       }
        attr.cq = (struct ibv_exp_cq_init_attr){
                .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
                .res_domain = tmpl.rd,
        };
-       tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, NULL, 0, &attr.cq);
+       tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0,
+                                   &attr.cq);
        if (tmpl.cq == NULL) {
                ret = ENOMEM;
                ERROR("%p: CQ creation failure: %s",
@@ -3741,7 +3776,7 @@ skip_mr:
                      (void *)dev, strerror(ret));
                goto error;
        }
-       if ((parent) || (!priv->rss))  {
+       if (!priv->isolated && (parent || !priv->rss)) {
                /* Configure MAC and broadcast addresses. */
                ret = rxq_mac_addrs_add(&tmpl);
                if (ret) {
@@ -3970,7 +4005,10 @@ mlx4_dev_start(struct rte_eth_dev *dev)
        }
        DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
        priv->started = 1;
-       if (priv->rss) {
+       if (priv->isolated) {
+               rxq = NULL;
+               r = 1;
+       } else if (priv->rss) {
                rxq = &priv->rxq_parent;
                r = 1;
        } else {
@@ -4005,6 +4043,12 @@ mlx4_dev_start(struct rte_eth_dev *dev)
                     (void *)dev);
                goto err;
        }
+       ret = priv_rx_intr_vec_enable(priv);
+       if (ret) {
+               ERROR("%p: Rx interrupt vector creation failed",
+                     (void *)dev);
+               goto err;
+       }
        ret = mlx4_priv_flow_start(priv);
        if (ret) {
                ERROR("%p: flow start failed: %s",
@@ -4053,7 +4097,10 @@ mlx4_dev_stop(struct rte_eth_dev *dev)
        }
        DEBUG("%p: detaching flows from all RX queues", (void *)dev);
        priv->started = 0;
-       if (priv->rss) {
+       if (priv->isolated) {
+               rxq = NULL;
+               r = 1;
+       } else if (priv->rss) {
                rxq = &priv->rxq_parent;
                r = 1;
        } else {
@@ -4197,6 +4244,7 @@ mlx4_dev_close(struct rte_eth_dev *dev)
                assert(priv->ctx == NULL);
        priv_dev_removal_interrupt_handler_uninstall(priv, dev);
        priv_dev_link_interrupt_handler_uninstall(priv, dev);
+       priv_rx_intr_vec_disable(priv);
        priv_unlock(priv);
        memset(priv, 0, sizeof(*priv));
 }
@@ -4300,7 +4348,7 @@ mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
        unsigned int max;
        char ifname[IF_NAMESIZE];
 
-       info->pci_dev = RTE_DEV_TO_PCI(dev->device);
+       info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
 
        if (priv == NULL)
                return;
@@ -4481,6 +4529,8 @@ mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
        if (mlx4_is_secondary())
                return;
        priv_lock(priv);
+       if (priv->isolated)
+               goto end;
        DEBUG("%p: removing MAC address from index %" PRIu32,
              (void *)dev, index);
        /* Last array entry is reserved for broadcast. */
@@ -4514,6 +4564,12 @@ mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
                return -ENOTSUP;
        (void)vmdq;
        priv_lock(priv);
+       if (priv->isolated) {
+               DEBUG("%p: cannot add MAC address, "
+                     "device is in isolated mode", (void *)dev);
+               re = EPERM;
+               goto end;
+       }
        DEBUG("%p: adding MAC address at index %" PRIu32,
              (void *)dev, index);
        /* Last array entry is reserved for broadcast. */
@@ -4561,6 +4617,12 @@ mlx4_promiscuous_enable(struct rte_eth_dev *dev)
        if (mlx4_is_secondary())
                return;
        priv_lock(priv);
+       if (priv->isolated) {
+               DEBUG("%p: cannot enable promiscuous, "
+                     "device is in isolated mode", (void *)dev);
+               priv_unlock(priv);
+               return;
+       }
        if (priv->promisc) {
                priv_unlock(priv);
                return;
@@ -4609,7 +4671,7 @@ mlx4_promiscuous_disable(struct rte_eth_dev *dev)
        if (mlx4_is_secondary())
                return;
        priv_lock(priv);
-       if (!priv->promisc) {
+       if (!priv->promisc || priv->isolated) {
                priv_unlock(priv);
                return;
        }
@@ -4641,6 +4703,12 @@ mlx4_allmulticast_enable(struct rte_eth_dev *dev)
        if (mlx4_is_secondary())
                return;
        priv_lock(priv);
+       if (priv->isolated) {
+               DEBUG("%p: cannot enable allmulticast, "
+                     "device is in isolated mode", (void *)dev);
+               priv_unlock(priv);
+               return;
+       }
        if (priv->allmulti) {
                priv_unlock(priv);
                return;
@@ -4689,7 +4757,7 @@ mlx4_allmulticast_disable(struct rte_eth_dev *dev)
        if (mlx4_is_secondary())
                return;
        priv_lock(priv);
-       if (!priv->allmulti) {
+       if (!priv->allmulti || priv->isolated) {
                priv_unlock(priv);
                return;
        }
@@ -4832,7 +4900,7 @@ mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
                }
                /* Reenable non-RSS queue attributes. No need to check
                 * for errors at this stage. */
-               if (!priv->rss) {
+               if (!priv->rss && !priv->isolated) {
                        rxq_mac_addrs_add(rxq);
                        if (priv->promisc)
                                rxq_promiscuous_enable(rxq);
@@ -5067,6 +5135,12 @@ mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
        if (mlx4_is_secondary())
                return -E_RTE_SECONDARY;
        priv_lock(priv);
+       if (priv->isolated) {
+               DEBUG("%p: cannot set vlan filter, "
+                     "device is in isolated mode", (void *)dev);
+               priv_unlock(priv);
+               return -EINVAL;
+       }
        ret = vlan_filter_set(dev, vlan_id, on);
        priv_unlock(priv);
        assert(ret >= 0);
@@ -5079,6 +5153,7 @@ const struct rte_flow_ops mlx4_flow_ops = {
        .destroy = mlx4_flow_destroy,
        .flush = mlx4_flow_flush,
        .query = NULL,
+       .isolate = mlx4_flow_isolate,
 };
 
 /**
@@ -5157,6 +5232,8 @@ static const struct eth_dev_ops mlx4_dev_ops = {
        .mac_addr_set = mlx4_mac_addr_set,
        .mtu_set = mlx4_dev_set_mtu,
        .filter_ctrl = mlx4_dev_filter_ctrl,
+       .rx_queue_intr_enable = mlx4_rx_intr_enable,
+       .rx_queue_intr_disable = mlx4_rx_intr_disable,
 };
 
 /**
@@ -5196,7 +5273,7 @@ mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
                /* Extract information. */
                if (sscanf(line,
                           "PCI_SLOT_NAME="
-                          "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
+                          "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
                           &pci_addr->domain,
                           &pci_addr->bus,
                           &pci_addr->devid,
@@ -5308,6 +5385,7 @@ priv_dev_status_handler(struct priv *priv, struct rte_eth_dev *dev,
 {
        struct ibv_async_event event;
        int port_change = 0;
+       struct rte_eth_link *link = &dev->data->dev_link;
        int ret = 0;
 
        *events = 0;
@@ -5329,22 +5407,20 @@ priv_dev_status_handler(struct priv *priv, struct rte_eth_dev *dev,
                              event.event_type, event.element.port_num);
                ibv_ack_async_event(&event);
        }
-
-       if (port_change ^ priv->pending_alarm) {
-               struct rte_eth_link *link = &dev->data->dev_link;
-
-               priv->pending_alarm = 0;
-               mlx4_link_update(dev, 0);
-               if (((link->link_speed == 0) && link->link_status) ||
-                   ((link->link_speed != 0) && !link->link_status)) {
+       if (!port_change)
+               return ret;
+       mlx4_link_update(dev, 0);
+       if (((link->link_speed == 0) && link->link_status) ||
+           ((link->link_speed != 0) && !link->link_status)) {
+               if (!priv->pending_alarm) {
                        /* Inconsistent status, check again later. */
                        priv->pending_alarm = 1;
                        rte_eal_alarm_set(MLX4_ALARM_TIMEOUT_US,
                                          mlx4_dev_link_status_handler,
                                          dev);
-               } else {
-                       *events |= (1 << RTE_ETH_EVENT_INTR_LSC);
                }
+       } else {
+               *events |= (1 << RTE_ETH_EVENT_INTR_LSC);
        }
        return ret;
 }
@@ -5365,10 +5441,12 @@ mlx4_dev_link_status_handler(void *arg)
 
        priv_lock(priv);
        assert(priv->pending_alarm == 1);
+       priv->pending_alarm = 0;
        ret = priv_dev_status_handler(priv, dev, &events);
        priv_unlock(priv);
        if (ret > 0 && events & (1 << RTE_ETH_EVENT_INTR_LSC))
-               _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
+               _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
+                                             NULL);
 }
 
 /**
@@ -5397,7 +5475,8 @@ mlx4_dev_interrupt_handler(void *cb_arg)
                     i++) {
                        if (ev & (1 << i)) {
                                ev &= ~(1 << i);
-                               _rte_eth_dev_callback_process(dev, i, NULL);
+                               _rte_eth_dev_callback_process(dev, i, NULL,
+                                                             NULL);
                                ret--;
                        }
                }
@@ -5591,6 +5670,154 @@ priv_dev_removal_interrupt_handler_install(struct priv *priv,
        return 0;
 }
 
+/**
+ * Allocate queue vector and fill epoll fd list for Rx interrupts.
+ *
+ * @param priv
+ *   Pointer to private structure.
+ *
+ * @return
+ *   0 on success, negative on failure.
+ */
+static int
+priv_rx_intr_vec_enable(struct priv *priv)
+{
+       unsigned int i;
+       unsigned int rxqs_n = priv->rxqs_n;
+       unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
+       unsigned int count = 0;
+       struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
+
+       if (!priv->dev->data->dev_conf.intr_conf.rxq)
+               return 0;
+       priv_rx_intr_vec_disable(priv);
+       intr_handle->intr_vec = malloc(sizeof(intr_handle->intr_vec[rxqs_n]));
+       if (intr_handle->intr_vec == NULL) {
+               ERROR("failed to allocate memory for interrupt vector,"
+                     " Rx interrupts will not be supported");
+               return -ENOMEM;
+       }
+       intr_handle->type = RTE_INTR_HANDLE_EXT;
+       for (i = 0; i != n; ++i) {
+               struct rxq *rxq = (*priv->rxqs)[i];
+               int fd;
+               int flags;
+               int rc;
+
+               /* Skip queues that cannot request interrupts. */
+               if (!rxq || !rxq->channel) {
+                       /* Use invalid intr_vec[] index to disable entry. */
+                       intr_handle->intr_vec[i] =
+                               RTE_INTR_VEC_RXTX_OFFSET +
+                               RTE_MAX_RXTX_INTR_VEC_ID;
+                       continue;
+               }
+               if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
+                       ERROR("too many Rx queues for interrupt vector size"
+                             " (%d), Rx interrupts cannot be enabled",
+                             RTE_MAX_RXTX_INTR_VEC_ID);
+                       priv_rx_intr_vec_disable(priv);
+                       return -1;
+               }
+               fd = rxq->channel->fd;
+               flags = fcntl(fd, F_GETFL);
+               rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
+               if (rc < 0) {
+                       ERROR("failed to make Rx interrupt file descriptor"
+                             " %d non-blocking for queue index %d", fd, i);
+                       priv_rx_intr_vec_disable(priv);
+                       return rc;
+               }
+               intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
+               intr_handle->efds[count] = fd;
+               count++;
+       }
+       if (!count)
+               priv_rx_intr_vec_disable(priv);
+       else
+               intr_handle->nb_efd = count;
+       return 0;
+}
+
+/**
+ * Clean up Rx interrupts handler.
+ *
+ * @param priv
+ *   Pointer to private structure.
+ */
+static void
+priv_rx_intr_vec_disable(struct priv *priv)
+{
+       struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
+
+       rte_intr_free_epoll_fd(intr_handle);
+       free(intr_handle->intr_vec);
+       intr_handle->nb_efd = 0;
+       intr_handle->intr_vec = NULL;
+}
+
+/**
+ * DPDK callback for Rx queue interrupt enable.
+ *
+ * @param dev
+ *   Pointer to Ethernet device structure.
+ * @param idx
+ *   Rx queue index.
+ *
+ * @return
+ *   0 on success, negative on failure.
+ */
+static int
+mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx)
+{
+       struct priv *priv = dev->data->dev_private;
+       struct rxq *rxq = (*priv->rxqs)[idx];
+       int ret;
+
+       if (!rxq || !rxq->channel)
+               ret = EINVAL;
+       else
+               ret = ibv_req_notify_cq(rxq->cq, 0);
+       if (ret)
+               WARN("unable to arm interrupt on rx queue %d", idx);
+       return -ret;
+}
+
+/**
+ * DPDK callback for Rx queue interrupt disable.
+ *
+ * @param dev
+ *   Pointer to Ethernet device structure.
+ * @param idx
+ *   Rx queue index.
+ *
+ * @return
+ *   0 on success, negative on failure.
+ */
+static int
+mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx)
+{
+       struct priv *priv = dev->data->dev_private;
+       struct rxq *rxq = (*priv->rxqs)[idx];
+       struct ibv_cq *ev_cq;
+       void *ev_ctx;
+       int ret;
+
+       if (!rxq || !rxq->channel) {
+               ret = EINVAL;
+       } else {
+               ret = ibv_get_cq_event(rxq->cq->channel, &ev_cq, &ev_ctx);
+               if (ret || ev_cq != rxq->cq)
+                       ret = EINVAL;
+       }
+       if (ret)
+               WARN("unable to disable interrupt on rx queue %d",
+                    idx);
+       else
+               ibv_ack_cq_events(rxq->cq, 1);
+       return -ret;
+}
+
 /**
  * Verify and store value for device argument.
  *
@@ -6002,6 +6229,15 @@ mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
 
                eth_dev->device->driver = &mlx4_driver.driver;
 
+               /*
+                * Copy and override interrupt handle to prevent it from
+                * being shared between all ethdev instances of a given PCI
+                * device. This is required to properly handle Rx interrupts
+                * on all ports.
+                */
+               priv->intr_handle_dev = *eth_dev->intr_handle;
+               eth_dev->intr_handle = &priv->intr_handle_dev;
+
                priv->dev = eth_dev;
                eth_dev->dev_ops = &mlx4_dev_ops;