net/i40e: set VF MAC from VF
[dpdk.git] / drivers / net / i40e / i40e_ethdev_vf.c
index 29b31b5..a9a8bb4 100644 (file)
@@ -151,6 +151,9 @@ static int i40evf_dev_rss_hash_update(struct rte_eth_dev *dev,
                                      struct rte_eth_rss_conf *rss_conf);
 static int i40evf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
                                        struct rte_eth_rss_conf *rss_conf);
+static int i40evf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
+static void i40evf_set_default_mac_addr(struct rte_eth_dev *dev,
+                                       struct ether_addr *mac_addr);
 static int
 i40evf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id);
 static int
@@ -176,11 +179,11 @@ static const struct rte_i40evf_xstats_name_off rte_i40evf_stats_strings[] = {
        {"rx_unknown_protocol_packets", offsetof(struct i40e_eth_stats,
                rx_unknown_protocol)},
        {"tx_bytes", offsetof(struct i40e_eth_stats, tx_bytes)},
-       {"tx_unicast_packets", offsetof(struct i40e_eth_stats, tx_bytes)},
-       {"tx_multicast_packets", offsetof(struct i40e_eth_stats, tx_bytes)},
-       {"tx_broadcast_packets", offsetof(struct i40e_eth_stats, tx_bytes)},
-       {"tx_dropped_packets", offsetof(struct i40e_eth_stats, tx_bytes)},
-       {"tx_error_packets", offsetof(struct i40e_eth_stats, tx_bytes)},
+       {"tx_unicast_packets", offsetof(struct i40e_eth_stats, tx_unicast)},
+       {"tx_multicast_packets", offsetof(struct i40e_eth_stats, tx_multicast)},
+       {"tx_broadcast_packets", offsetof(struct i40e_eth_stats, tx_broadcast)},
+       {"tx_dropped_packets", offsetof(struct i40e_eth_stats, tx_discards)},
+       {"tx_error_packets", offsetof(struct i40e_eth_stats, tx_errors)},
 };
 
 #define I40EVF_NB_XSTATS (sizeof(rte_i40evf_stats_strings) / \
@@ -225,6 +228,8 @@ static const struct eth_dev_ops i40evf_eth_dev_ops = {
        .reta_query           = i40evf_dev_rss_reta_query,
        .rss_hash_update      = i40evf_dev_rss_hash_update,
        .rss_hash_conf_get    = i40evf_dev_rss_hash_conf_get,
+       .mtu_set              = i40evf_dev_mtu_set,
+       .mac_addr_set         = i40evf_set_default_mac_addr,
 };
 
 /*
@@ -361,6 +366,7 @@ i40evf_execute_vf_cmd(struct rte_eth_dev *dev, struct vf_cmd_info *args)
                err = -1;
                do {
                        ret = i40evf_read_pfmsg(dev, &info);
+                       vf->cmd_retval = info.result;
                        if (ret == I40EVF_MSG_CMD) {
                                err = 0;
                                break;
@@ -887,19 +893,16 @@ i40evf_add_mac_addr(struct rte_eth_dev *dev,
 }
 
 static void
-i40evf_del_mac_addr(struct rte_eth_dev *dev, uint32_t index)
+i40evf_del_mac_addr_by_addr(struct rte_eth_dev *dev,
+                           struct ether_addr *addr)
 {
        struct i40e_virtchnl_ether_addr_list *list;
        struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
-       struct rte_eth_dev_data *data = dev->data;
-       struct ether_addr *addr;
        uint8_t cmd_buffer[sizeof(struct i40e_virtchnl_ether_addr_list) + \
                        sizeof(struct i40e_virtchnl_ether_addr)];
        int err;
        struct vf_cmd_info args;
 
-       addr = &(data->mac_addrs[index]);
-
        if (i40e_validate_mac_addr(addr->addr_bytes) != I40E_SUCCESS) {
                PMD_DRV_LOG(ERR, "Invalid mac:%x-%x-%x-%x-%x-%x",
                            addr->addr_bytes[0], addr->addr_bytes[1],
@@ -926,6 +929,17 @@ i40evf_del_mac_addr(struct rte_eth_dev *dev, uint32_t index)
        return;
 }
 
+static void
+i40evf_del_mac_addr(struct rte_eth_dev *dev, uint32_t index)
+{
+       struct rte_eth_dev_data *data = dev->data;
+       struct ether_addr *addr;
+
+       addr = &data->mac_addrs[index];
+
+       i40evf_del_mac_addr_by_addr(dev, addr);
+}
+
 static int
 i40evf_update_stats(struct rte_eth_dev *dev, struct i40e_eth_stats **pstats)
 {
@@ -953,7 +967,7 @@ i40evf_update_stats(struct rte_eth_dev *dev, struct i40e_eth_stats **pstats)
 }
 
 static int
-i40evf_get_statics(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
+i40evf_get_statistics(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
 {
        int ret;
        struct i40e_eth_stats *pstats = NULL;
@@ -1088,7 +1102,6 @@ static const struct rte_pci_id pci_id_i40evf_map[] = {
        { RTE_PCI_DEVICE(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_VF_HV) },
        { RTE_PCI_DEVICE(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_X722_A0_VF) },
        { RTE_PCI_DEVICE(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_X722_VF) },
-       { RTE_PCI_DEVICE(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_X722_VF_HV) },
        { .vendor_id = 0, /* sentinel */ },
 };
 
@@ -1260,10 +1273,12 @@ i40evf_init_vf(struct rte_eth_dev *dev)
 
        /* Store the MAC address configured by host, or generate random one */
        p_mac_addr = (struct ether_addr *)(vf->vsi_res->default_mac_addr);
-       if (is_valid_assigned_ether_addr(p_mac_addr)) /* Configured by host */
+       if (is_valid_assigned_ether_addr(p_mac_addr)) /* Configured by host */
                ether_addr_copy(p_mac_addr, (struct ether_addr *)hw->mac.addr);
-       else
+               vf->flags |= I40E_FLAG_VF_MAC_BY_PF;
+       } else {
                eth_random_addr(hw->mac.addr); /* Generate a random one */
+       }
 
        /* If the PF host is not DPDK, set the interval of ITR0 to max*/
        if (vf->version_major != I40E_DPDK_VERSION_MAJOR) {
@@ -1337,8 +1352,9 @@ i40evf_handle_aq_msg(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 i40e_arq_event_info info;
-       struct i40e_virtchnl_msg *v_msg;
-       uint16_t pending, opcode;
+       uint16_t pending, aq_opc;
+       enum i40e_virtchnl_ops msg_opc;
+       enum i40e_status_code msg_ret;
        int ret;
 
        info.buf_len = I40E_AQ_BUF_SZ;
@@ -1347,7 +1363,6 @@ i40evf_handle_aq_msg(struct rte_eth_dev *dev)
                return;
        }
        info.msg_buf = vf->aq_resp;
-       v_msg = (struct i40e_virtchnl_msg *)&info.desc;
 
        pending = 1;
        while (pending) {
@@ -1358,32 +1373,39 @@ i40evf_handle_aq_msg(struct rte_eth_dev *dev)
                                    "ret: %d", ret);
                        break;
                }
-               opcode = rte_le_to_cpu_16(info.desc.opcode);
-
-               switch (opcode) {
+               aq_opc = rte_le_to_cpu_16(info.desc.opcode);
+               /* For the message sent from pf to vf, opcode is stored in
+                * cookie_high of struct i40e_aq_desc, while return error code
+                * are stored in cookie_low, Which is done by
+                * i40e_aq_send_msg_to_vf in PF driver.*/
+               msg_opc = (enum i40e_virtchnl_ops)rte_le_to_cpu_32(
+                                                 info.desc.cookie_high);
+               msg_ret = (enum i40e_status_code)rte_le_to_cpu_32(
+                                                 info.desc.cookie_low);
+               switch (aq_opc) {
                case i40e_aqc_opc_send_msg_to_vf:
-                       if (v_msg->v_opcode == I40E_VIRTCHNL_OP_EVENT)
+                       if (msg_opc == I40E_VIRTCHNL_OP_EVENT)
                                /* process event*/
                                i40evf_handle_pf_event(dev, info.msg_buf,
                                                       info.msg_len);
                        else {
                                /* read message and it's expected one */
-                               if (v_msg->v_opcode == vf->pend_cmd) {
-                                       vf->cmd_retval = v_msg->v_retval;
+                               if (msg_opc == vf->pend_cmd) {
+                                       vf->cmd_retval = msg_ret;
                                        /* prevent compiler reordering */
                                        rte_compiler_barrier();
                                        _clear_cmd(vf);
                                } else
                                        PMD_DRV_LOG(ERR, "command mismatch,"
                                                "expect %u, get %u",
-                                               vf->pend_cmd, v_msg->v_opcode);
+                                               vf->pend_cmd, msg_opc);
                                PMD_DRV_LOG(DEBUG, "adminq response is received,"
-                                            " opcode = %d\n", v_msg->v_opcode);
+                                            " opcode = %d\n", msg_opc);
                        }
                        break;
                default:
                        PMD_DRV_LOG(ERR, "Request %u is not supported yet",
-                                   opcode);
+                                   aq_opc);
                        break;
                }
        }
@@ -1460,6 +1482,7 @@ i40evf_dev_init(struct rte_eth_dev *eth_dev)
        }
 
        rte_eth_copy_pci_info(eth_dev, pci_dev);
+       eth_dev->data->dev_flags = RTE_ETH_DEV_DETACHABLE;
 
        hw->vendor_id = pci_dev->id.vendor_id;
        hw->device_id = pci_dev->id.device_id;
@@ -1529,7 +1552,7 @@ i40evf_dev_uninit(struct rte_eth_dev *eth_dev)
 static struct eth_driver rte_i40evf_pmd = {
        .pci_drv = {
                .id_table = pci_id_i40evf_map,
-               .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_DETACHABLE,
+               .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
                .probe = rte_eth_dev_pci_probe,
                .remove = rte_eth_dev_pci_remove,
        },
@@ -2225,6 +2248,7 @@ i40evf_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
        struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
 
        memset(dev_info, 0, sizeof(*dev_info));
+       dev_info->pci_dev = RTE_DEV_TO_PCI(dev->device);
        dev_info->max_rx_queues = vf->vsi_res->num_queue_pairs;
        dev_info->max_tx_queues = vf->vsi_res->num_queue_pairs;
        dev_info->min_rx_bufsize = I40E_BUF_SIZE_MIN;
@@ -2285,8 +2309,8 @@ i40evf_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
 static void
 i40evf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
 {
-       if (i40evf_get_statics(dev, stats))
-               PMD_DRV_LOG(ERR, "Get statics failed");
+       if (i40evf_get_statistics(dev, stats))
+               PMD_DRV_LOG(ERR, "Get statistics failed");
 }
 
 static void
@@ -2644,3 +2668,56 @@ i40evf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
 
        return 0;
 }
+
+static int
+i40evf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
+{
+       struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
+       struct rte_eth_dev_data *dev_data = vf->dev_data;
+       uint32_t frame_size = mtu + ETHER_HDR_LEN
+                             + ETHER_CRC_LEN + I40E_VLAN_TAG_SIZE;
+       int ret = 0;
+
+       /* check if mtu is within the allowed range */
+       if ((mtu < ETHER_MIN_MTU) || (frame_size > I40E_FRAME_SIZE_MAX))
+               return -EINVAL;
+
+       /* mtu setting is forbidden if port is start */
+       if (dev_data->dev_started) {
+               PMD_DRV_LOG(ERR,
+                           "port %d must be stopped before configuration\n",
+                           dev_data->port_id);
+               return -EBUSY;
+       }
+
+       if (frame_size > ETHER_MAX_LEN)
+               dev_data->dev_conf.rxmode.jumbo_frame = 1;
+       else
+               dev_data->dev_conf.rxmode.jumbo_frame = 0;
+
+       dev_data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
+
+       return ret;
+}
+
+static void
+i40evf_set_default_mac_addr(struct rte_eth_dev *dev,
+                           struct ether_addr *mac_addr)
+{
+       struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
+
+       if (!is_valid_assigned_ether_addr(mac_addr)) {
+               PMD_DRV_LOG(ERR, "Tried to set invalid MAC address.");
+               return;
+       }
+
+       if (is_same_ether_addr(mac_addr, dev->data->mac_addrs))
+               return;
+
+       if (vf->flags & I40E_FLAG_VF_MAC_BY_PF)
+               return;
+
+       i40evf_del_mac_addr_by_addr(dev, dev->data->mac_addrs);
+
+       i40evf_add_mac_addr(dev, mac_addr, 0, 0);
+}