net: add macro for MAC address print
[dpdk.git] / drivers / net / i40e / i40e_ethdev_vf.c
index c26b036..dcc2b64 100644 (file)
@@ -25,8 +25,8 @@
 #include <rte_eal.h>
 #include <rte_alarm.h>
 #include <rte_ether.h>
-#include <rte_ethdev_driver.h>
-#include <rte_ethdev_pci.h>
+#include <ethdev_driver.h>
+#include <ethdev_pci.h>
 #include <rte_malloc.h>
 #include <rte_dev.h>
 
@@ -106,6 +106,9 @@ static int i40evf_dev_tx_queue_start(struct rte_eth_dev *dev,
                                     uint16_t tx_queue_id);
 static int i40evf_dev_tx_queue_stop(struct rte_eth_dev *dev,
                                    uint16_t tx_queue_id);
+static int i40evf_add_del_eth_addr(struct rte_eth_dev *dev,
+                                  struct rte_ether_addr *addr,
+                                  bool add, uint8_t type);
 static int i40evf_add_mac_addr(struct rte_eth_dev *dev,
                               struct rte_ether_addr *addr,
                               uint32_t index,
@@ -213,6 +216,7 @@ static const struct eth_dev_ops i40evf_eth_dev_ops = {
        .mtu_set              = i40evf_dev_mtu_set,
        .mac_addr_set         = i40evf_set_default_mac_addr,
        .tx_done_cleanup      = i40e_tx_done_cleanup,
+       .get_monitor_addr     = i40e_get_monitor_addr
 };
 
 /*
@@ -823,10 +827,9 @@ i40evf_stop_queues(struct rte_eth_dev *dev)
 }
 
 static int
-i40evf_add_mac_addr(struct rte_eth_dev *dev,
-                   struct rte_ether_addr *addr,
-                   __rte_unused uint32_t index,
-                   __rte_unused uint32_t pool)
+i40evf_add_del_eth_addr(struct rte_eth_dev *dev,
+                       struct rte_ether_addr *addr,
+                       bool add, uint8_t type)
 {
        struct virtchnl_ether_addr_list *list;
        struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
@@ -835,83 +838,70 @@ i40evf_add_mac_addr(struct rte_eth_dev *dev,
        int err;
        struct vf_cmd_info args;
 
-       if (rte_is_zero_ether_addr(addr)) {
-               PMD_DRV_LOG(ERR, "Invalid mac:%x:%x:%x:%x:%x:%x",
-                           addr->addr_bytes[0], addr->addr_bytes[1],
-                           addr->addr_bytes[2], addr->addr_bytes[3],
-                           addr->addr_bytes[4], addr->addr_bytes[5]);
-               return I40E_ERR_INVALID_MAC_ADDR;
-       }
-
        list = (struct virtchnl_ether_addr_list *)cmd_buffer;
        list->vsi_id = vf->vsi_res->vsi_id;
        list->num_elements = 1;
+       list->list[0].type = type;
        rte_memcpy(list->list[0].addr, addr->addr_bytes,
                                        sizeof(addr->addr_bytes));
 
-       args.ops = VIRTCHNL_OP_ADD_ETH_ADDR;
+       args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR : VIRTCHNL_OP_DEL_ETH_ADDR;
        args.in_args = cmd_buffer;
        args.in_args_size = sizeof(cmd_buffer);
        args.out_buffer = vf->aq_resp;
        args.out_size = I40E_AQ_BUF_SZ;
        err = i40evf_execute_vf_cmd(dev, &args);
        if (err)
-               PMD_DRV_LOG(ERR, "fail to execute command "
-                           "OP_ADD_ETHER_ADDRESS");
-       else
-               vf->vsi.mac_num++;
-
+               PMD_DRV_LOG(ERR, "fail to execute command %s",
+                           add ? "OP_ADD_ETH_ADDR" :  "OP_DEL_ETH_ADDR");
        return err;
 }
 
-static void
-i40evf_del_mac_addr_by_addr(struct rte_eth_dev *dev,
-                           struct rte_ether_addr *addr)
+static int
+i40evf_add_mac_addr(struct rte_eth_dev *dev,
+                   struct rte_ether_addr *addr,
+                   __rte_unused uint32_t index,
+                   __rte_unused uint32_t pool)
 {
-       struct virtchnl_ether_addr_list *list;
        struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
-       uint8_t cmd_buffer[sizeof(struct virtchnl_ether_addr_list) + \
-                       sizeof(struct virtchnl_ether_addr)];
        int err;
-       struct vf_cmd_info args;
 
-       if (i40e_validate_mac_addr(addr->addr_bytes) != I40E_SUCCESS) {
-               PMD_DRV_LOG(ERR, "Invalid mac:%x-%x-%x-%x-%x-%x",
+       if (rte_is_zero_ether_addr(addr)) {
+               PMD_DRV_LOG(ERR, "Invalid mac:" RTE_ETHER_ADDR_PRT_FMT,
                            addr->addr_bytes[0], addr->addr_bytes[1],
                            addr->addr_bytes[2], addr->addr_bytes[3],
                            addr->addr_bytes[4], addr->addr_bytes[5]);
-               return;
+               return I40E_ERR_INVALID_MAC_ADDR;
        }
 
-       list = (struct virtchnl_ether_addr_list *)cmd_buffer;
-       list->vsi_id = vf->vsi_res->vsi_id;
-       list->num_elements = 1;
-       rte_memcpy(list->list[0].addr, addr->addr_bytes,
-                       sizeof(addr->addr_bytes));
+       err = i40evf_add_del_eth_addr(dev, addr, TRUE, VIRTCHNL_ETHER_ADDR_EXTRA);
 
-       args.ops = VIRTCHNL_OP_DEL_ETH_ADDR;
-       args.in_args = cmd_buffer;
-       args.in_args_size = sizeof(cmd_buffer);
-       args.out_buffer = vf->aq_resp;
-       args.out_size = I40E_AQ_BUF_SZ;
-       err = i40evf_execute_vf_cmd(dev, &args);
        if (err)
-               PMD_DRV_LOG(ERR, "fail to execute command "
-                           "OP_DEL_ETHER_ADDRESS");
+               PMD_DRV_LOG(ERR, "fail to add MAC address");
        else
-               vf->vsi.mac_num--;
-       return;
+               vf->vsi.mac_num++;
+
+       return err;
 }
 
 static void
 i40evf_del_mac_addr(struct rte_eth_dev *dev, uint32_t index)
 {
+       struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
        struct rte_eth_dev_data *data = dev->data;
        struct rte_ether_addr *addr;
+       int err;
 
        addr = &data->mac_addrs[index];
 
-       i40evf_del_mac_addr_by_addr(dev, addr);
+       err = i40evf_add_del_eth_addr(dev, addr, FALSE, VIRTCHNL_ETHER_ADDR_EXTRA);
+
+       if (err)
+               PMD_DRV_LOG(ERR, "fail to delete MAC address");
+       else
+               vf->vsi.mac_num--;
+
+       return;
 }
 
 static int
@@ -1078,8 +1068,18 @@ i40evf_add_vlan(struct rte_eth_dev *dev, uint16_t vlanid)
        args.out_buffer = vf->aq_resp;
        args.out_size = I40E_AQ_BUF_SZ;
        err = i40evf_execute_vf_cmd(dev, &args);
-       if (err)
+       if (err) {
                PMD_DRV_LOG(ERR, "fail to execute command OP_ADD_VLAN");
+               return err;
+       }
+       /**
+        * In linux kernel driver on receiving ADD_VLAN it enables
+        * VLAN_STRIP by default. So reconfigure the vlan_offload
+        * as it was done by the app earlier.
+        */
+       err = i40evf_vlan_offload_set(dev, ETH_VLAN_STRIP_MASK);
+       if (err)
+               PMD_DRV_LOG(ERR, "fail to set vlan_strip");
 
        return err;
 }
@@ -1213,7 +1213,6 @@ i40evf_check_vf_reset_done(struct rte_eth_dev *dev)
        if (i >= MAX_RESET_WAIT_CNT)
                return -1;
 
-       vf->vf_reset = false;
        vf->pend_msg &= ~PFMSG_RESET_IMPENDING;
 
        return 0;
@@ -1237,7 +1236,7 @@ i40evf_reset_vf(struct rte_eth_dev *dev)
          * it to ACTIVE. In this duration, vf may not catch the moment that
          * COMPLETE is set. So, for vf, we'll try to wait a long time.
          */
-       rte_delay_ms(200);
+       rte_delay_ms(500);
 
        ret = i40evf_check_vf_reset_done(dev);
        if (ret) {
@@ -1392,6 +1391,7 @@ i40evf_handle_pf_event(struct rte_eth_dev *dev, uint8_t *msg,
        switch (pf_msg->event) {
        case VIRTCHNL_EVENT_RESET_IMPENDING:
                PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_RESET_IMPENDING event");
+               vf->vf_reset = true;
                rte_eth_dev_callback_process(dev,
                                RTE_ETH_EVENT_INTR_RESET, NULL);
                break;
@@ -1495,7 +1495,8 @@ i40evf_handle_aq_msg(struct rte_eth_dev *dev)
                                                       info.msg_len);
                        else {
                                /* read message and it's expected one */
-                               if (msg_opc == vf->pend_cmd) {
+                               if ((volatile uint32_t)msg_opc ==
+                                   vf->pend_cmd) {
                                        vf->cmd_retval = msg_ret;
                                        /* prevent compiler reordering */
                                        rte_compiler_barrier();
@@ -1645,9 +1646,52 @@ i40evf_dev_uninit(struct rte_eth_dev *eth_dev)
        return 0;
 }
 
+static int
+i40evf_check_driver_handler(__rte_unused const char *key,
+                           const char *value, __rte_unused void *opaque)
+{
+       if (strcmp(value, "i40evf"))
+               return -1;
+
+       return 0;
+}
+
+static int
+i40evf_driver_selected(struct rte_devargs *devargs)
+{
+       struct rte_kvargs *kvlist;
+       int ret = 0;
+
+       if (devargs == NULL)
+               return 0;
+
+       kvlist = rte_kvargs_parse(devargs->args, NULL);
+       if (kvlist == NULL)
+               return 0;
+
+       if (!rte_kvargs_count(kvlist, RTE_DEVARGS_KEY_DRIVER))
+               goto exit;
+
+       /* i40evf driver selected when there's a key-value pair:
+        * driver=i40evf
+        */
+       if (rte_kvargs_process(kvlist, RTE_DEVARGS_KEY_DRIVER,
+                              i40evf_check_driver_handler, NULL) < 0)
+               goto exit;
+
+       ret = 1;
+
+exit:
+       rte_kvargs_free(kvlist);
+       return ret;
+}
+
 static int eth_i40evf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
        struct rte_pci_device *pci_dev)
 {
+       if (!i40evf_driver_selected(pci_dev->device.devargs))
+               return 1;
+
        return rte_eth_dev_pci_generic_probe(pci_dev,
                sizeof(struct i40e_adapter), i40evf_dev_init);
 }
@@ -1670,6 +1714,7 @@ static struct rte_pci_driver rte_i40evf_pmd = {
 RTE_PMD_REGISTER_PCI(net_i40e_vf, rte_i40evf_pmd);
 RTE_PMD_REGISTER_PCI_TABLE(net_i40e_vf, pci_id_i40evf_map);
 RTE_PMD_REGISTER_KMOD_DEP(net_i40e_vf, "* igb_uio | vfio-pci");
+RTE_PMD_REGISTER_PARAM_STRING(net_i40e_vf, "driver=i40evf");
 
 static int
 i40evf_dev_configure(struct rte_eth_dev *dev)
@@ -1889,22 +1934,22 @@ i40evf_rxq_init(struct rte_eth_dev *dev, struct i40e_rx_queue *rxq)
         * Check if the jumbo frame and maximum packet length are set correctly
         */
        if (dev_data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
-               if (rxq->max_pkt_len <= RTE_ETHER_MAX_LEN ||
+               if (rxq->max_pkt_len <= I40E_ETH_MAX_LEN ||
                    rxq->max_pkt_len > I40E_FRAME_SIZE_MAX) {
                        PMD_DRV_LOG(ERR, "maximum packet length must be "
                                "larger than %u and smaller than %u, as jumbo "
-                               "frame is enabled", (uint32_t)RTE_ETHER_MAX_LEN,
+                               "frame is enabled", (uint32_t)I40E_ETH_MAX_LEN,
                                        (uint32_t)I40E_FRAME_SIZE_MAX);
                        return I40E_ERR_CONFIG;
                }
        } else {
                if (rxq->max_pkt_len < RTE_ETHER_MIN_LEN ||
-                   rxq->max_pkt_len > RTE_ETHER_MAX_LEN) {
+                   rxq->max_pkt_len > I40E_ETH_MAX_LEN) {
                        PMD_DRV_LOG(ERR, "maximum packet length must be "
                                "larger than %u and smaller than %u, as jumbo "
                                "frame is disabled",
                                (uint32_t)RTE_ETHER_MIN_LEN,
-                               (uint32_t)RTE_ETHER_MAX_LEN);
+                               (uint32_t)I40E_ETH_MAX_LEN);
                        return I40E_ERR_CONFIG;
                }
        }
@@ -2082,7 +2127,10 @@ i40evf_add_del_all_mac_addr(struct rte_eth_dev *dev, bool add)
                                continue;
                        rte_memcpy(list->list[j].addr, addr->addr_bytes,
                                         sizeof(addr->addr_bytes));
-                       PMD_DRV_LOG(DEBUG, "add/rm mac:%x:%x:%x:%x:%x:%x",
+                       list->list[j].type = (j == 0 ?
+                                             VIRTCHNL_ETHER_ADDR_PRIMARY :
+                                             VIRTCHNL_ETHER_ADDR_EXTRA);
+                       PMD_DRV_LOG(DEBUG, "add/rm mac:" RTE_ETHER_ADDR_PRT_FMT,
                                    addr->addr_bytes[0], addr->addr_bytes[1],
                                    addr->addr_bytes[2], addr->addr_bytes[3],
                                    addr->addr_bytes[4], addr->addr_bytes[5]);
@@ -2406,6 +2454,7 @@ i40evf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
                stats->imissed = pstats->rx_discards;
                stats->oerrors = pstats->tx_errors + pstats->tx_discards;
                stats->ibytes = pstats->rx_bytes;
+               stats->ibytes -= stats->ipackets * RTE_ETHER_CRC_LEN;
                stats->obytes = pstats->tx_bytes;
        } else {
                PMD_DRV_LOG(ERR, "Get statistics failed");
@@ -2418,6 +2467,7 @@ i40evf_dev_close(struct rte_eth_dev *dev)
 {
        struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
        struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
+       struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
        int ret;
 
        if (rte_eal_process_type() != RTE_PROC_PRIMARY)
@@ -2440,6 +2490,16 @@ i40evf_dev_close(struct rte_eth_dev *dev)
        i40e_shutdown_adminq(hw);
        i40evf_disable_irq0(hw);
 
+       /*
+        * If the VF is reset via VFLR, the device will be knocked out of bus
+        * master mode, and the driver will fail to recover from the reset. Fix
+        * this by enabling bus mastering after every reset. In a non-VFLR case,
+        * the bus master bit will not be disabled, and this call will have no
+        * effect.
+        */
+       if (vf->vf_reset && !rte_pci_set_bus_master(pci_dev, true))
+               vf->vf_reset = false;
+
        rte_free(vf->vf_res);
        vf->vf_res = NULL;
        rte_free(vf->aq_resp);
@@ -2734,7 +2794,7 @@ i40evf_config_rss(struct i40e_vf *vf)
                }
 
                for (i = 0; i < rss_lut_size; i++)
-                       lut_info[i] = i % vf->num_queue_pairs;
+                       lut_info[i] = i % num;
 
                ret = i40evf_set_rss_lut(&vf->vsi, lut_info,
                                         rss_lut_size);
@@ -2825,7 +2885,7 @@ i40evf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
                return -EBUSY;
        }
 
-       if (frame_size > RTE_ETHER_MAX_LEN)
+       if (frame_size > I40E_ETH_MAX_LEN)
                dev_data->dev_conf.rxmode.offloads |=
                        DEV_RX_OFFLOAD_JUMBO_FRAME;
        else
@@ -2841,15 +2901,23 @@ i40evf_set_default_mac_addr(struct rte_eth_dev *dev,
                            struct rte_ether_addr *mac_addr)
 {
        struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
+       struct rte_ether_addr *old_addr;
+       int ret;
+
+       old_addr = (struct rte_ether_addr *)hw->mac.addr;
 
        if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
                PMD_DRV_LOG(ERR, "Tried to set invalid MAC address.");
                return -EINVAL;
        }
 
-       i40evf_del_mac_addr_by_addr(dev, (struct rte_ether_addr *)hw->mac.addr);
+       if (rte_is_same_ether_addr(old_addr, mac_addr))
+               return 0;
 
-       if (i40evf_add_mac_addr(dev, mac_addr, 0, 0) != 0)
+       i40evf_add_del_eth_addr(dev, old_addr, FALSE, VIRTCHNL_ETHER_ADDR_PRIMARY);
+
+       ret = i40evf_add_del_eth_addr(dev, mac_addr, TRUE, VIRTCHNL_ETHER_ADDR_PRIMARY);
+       if (ret)
                return -EIO;
 
        rte_ether_addr_copy(mac_addr, (struct rte_ether_addr *)hw->mac.addr);
@@ -2881,7 +2949,7 @@ i40evf_add_del_mc_addr_list(struct rte_eth_dev *dev,
 
        for (i = 0; i < mc_addrs_num; i++) {
                if (!I40E_IS_MULTICAST(mc_addrs[i].addr_bytes)) {
-                       PMD_DRV_LOG(ERR, "Invalid mac:%x:%x:%x:%x:%x:%x",
+                       PMD_DRV_LOG(ERR, "Invalid mac:" RTE_ETHER_ADDR_PRT_FMT,
                                    mc_addrs[i].addr_bytes[0],
                                    mc_addrs[i].addr_bytes[1],
                                    mc_addrs[i].addr_bytes[2],
@@ -2893,6 +2961,7 @@ i40evf_add_del_mc_addr_list(struct rte_eth_dev *dev,
 
                memcpy(list->list[i].addr, mc_addrs[i].addr_bytes,
                        sizeof(list->list[i].addr));
+               list->list[i].type = VIRTCHNL_ETHER_ADDR_EXTRA;
        }
 
        args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR : VIRTCHNL_OP_DEL_ETH_ADDR;