#include "virtchnl.h"
#include "queues.h"
#include "rss.h"
#include "ice_vf_lib_private.h"
#include "ice.h"
#include "ice_base.h"
#include "ice_lib.h"
#include "ice_fltr.h"
#include "allowlist.h"
#include "ice_vf_vsi_vlan_ops.h"
#include "ice_vlan.h"
#include "ice_flex_pipe.h"
#include "ice_dcb_lib.h"
static void
ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
{
struct ice_hw *hw = &pf->hw;
struct ice_vf *vf;
unsigned int bkt;
mutex_lock(&pf->vfs.table_lock);
ice_for_each_vf(pf, bkt, vf) {
if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
continue;
ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
msglen, NULL);
}
mutex_unlock(&pf->vfs.table_lock);
}
static void
ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
int ice_link_speed, bool link_up)
{
if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
pfe->event_data.link_event_adv.link_status = link_up;
pfe->event_data.link_event_adv.link_speed =
ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
} else {
pfe->event_data.link_event.link_status = link_up;
pfe->event_data.link_event.link_speed =
(enum virtchnl_link_speed)
ice_conv_link_speed_to_virtchnl(false, ice_link_speed);
}
}
void ice_vc_notify_vf_link_state(struct ice_vf *vf)
{
struct virtchnl_pf_event pfe = { 0 };
struct ice_hw *hw = &vf->pf->hw;
pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
pfe.severity = PF_EVENT_SEVERITY_INFO;
if (ice_is_vf_link_up(vf))
ice_set_pfe_link(vf, &pfe,
hw->port_info->phy.link_info.link_speed, true);
else
ice_set_pfe_link(vf, &pfe, ICE_AQ_LINK_SPEED_UNKNOWN, false);
ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,
sizeof(pfe), NULL);
}
void ice_vc_notify_link_state(struct ice_pf *pf)
{
struct ice_vf *vf;
unsigned int bkt;
mutex_lock(&pf->vfs.table_lock);
ice_for_each_vf(pf, bkt, vf)
ice_vc_notify_vf_link_state(vf);
mutex_unlock(&pf->vfs.table_lock);
}
void ice_vc_notify_reset(struct ice_pf *pf)
{
struct virtchnl_pf_event pfe;
if (!ice_has_vfs(pf))
return;
pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
(u8 *)&pfe, sizeof(struct virtchnl_pf_event));
}
int
ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
{
struct device *dev;
struct ice_pf *pf;
int aq_ret;
pf = vf->pf;
dev = ice_pf_to_dev(pf);
aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
msg, msglen, NULL);
if (aq_ret && pf->hw.mailboxq.sq_last_status != LIBIE_AQ_RC_ENOSYS) {
dev_info(dev, "Unable to send the message to VF %d ret %d aq_err %s\n",
vf->vf_id, aq_ret,
libie_aq_str(pf->hw.mailboxq.sq_last_status));
return -EIO;
}
return 0;
}
static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
{
struct virtchnl_version_info info = {
VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
};
vf->vf_ver = *(struct virtchnl_version_info *)msg;
if (VF_IS_V10(&vf->vf_ver))
info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
sizeof(struct virtchnl_version_info));
}
static u32
ice_vc_get_vlan_caps(struct ice_hw *hw, struct ice_vf *vf, struct ice_vsi *vsi,
u32 driver_caps)
{
if (ice_is_eswitch_mode_switchdev(vf->pf))
return 0;
if (driver_caps & VIRTCHNL_VF_OFFLOAD_VLAN_V2) {
return VIRTCHNL_VF_OFFLOAD_VLAN_V2;
} else if (driver_caps & VIRTCHNL_VF_OFFLOAD_VLAN) {
if (ice_is_dvm_ena(hw) && ice_vf_is_port_vlan_ena(vf)) {
return VIRTCHNL_VF_OFFLOAD_VLAN;
} else if (!ice_is_dvm_ena(hw) &&
!ice_vf_is_port_vlan_ena(vf)) {
ice_vf_vsi_cfg_svm_legacy_vlan_mode(vsi);
return VIRTCHNL_VF_OFFLOAD_VLAN;
} else if (ice_is_dvm_ena(hw)) {
ice_vf_vsi_cfg_dvm_legacy_vlan_mode(vsi);
}
}
return 0;
}
static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
{
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
struct virtchnl_vf_resource *vfres = NULL;
struct ice_hw *hw = &vf->pf->hw;
struct ice_vsi *vsi;
int len = 0;
int ret;
if (ice_check_vf_init(vf)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto err;
}
len = virtchnl_struct_size(vfres, vsi_res, 0);
vfres = kzalloc(len, GFP_KERNEL);
if (!vfres) {
v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
len = 0;
goto err;
}
if (VF_IS_V11(&vf->vf_ver))
vf->driver_caps = *(u32 *)msg;
else
vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
VIRTCHNL_VF_OFFLOAD_VLAN;
vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
vsi = ice_get_vf_vsi(vf);
if (!vsi) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto err;
}
vfres->vf_cap_flags |= ice_vc_get_vlan_caps(hw, vf, vsi,
vf->driver_caps);
if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF)
vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC)
vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC;
if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_FDIR_PF)
vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_FDIR_PF;
if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_TC_U32 &&
vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_FDIR_PF)
vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_TC_U32;
if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_CRC)
vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_CRC;
if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF)
vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF;
if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_USO)
vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_USO;
if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_QOS)
vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_QOS;
if (vf->driver_caps & VIRTCHNL_VF_CAP_PTP)
vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_PTP;
vfres->num_vsis = 1;
vfres->num_queue_pairs = vsi->num_txq;
vfres->max_vectors = vf->num_msix;
vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
vfres->rss_lut_size = ICE_LUT_VSI_SIZE;
vfres->max_mtu = ice_vc_get_max_frame_size(vf);
vfres->vsi_res[0].vsi_id = ICE_VF_VSI_ID;
vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
vf->hw_lan_addr);
vf->driver_caps = vfres->vf_cap_flags;
ice_vc_set_caps_allowlist(vf);
ice_vc_set_working_allowlist(vf);
set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
err:
ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
(u8 *)vfres, len);
kfree(vfres);
return ret;
}
static void ice_vc_reset_vf_msg(struct ice_vf *vf)
{
if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
ice_reset_vf(vf, 0);
}
bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
{
return vsi_id == ICE_VF_VSI_ID;
}
static int ice_vc_get_qos_caps(struct ice_vf *vf)
{
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
struct virtchnl_qos_cap_list *cap_list = NULL;
u8 tc_prio[ICE_MAX_TRAFFIC_CLASS] = { 0 };
struct virtchnl_qos_cap_elem *cfg = NULL;
struct ice_vsi_ctx *vsi_ctx;
struct ice_pf *pf = vf->pf;
struct ice_port_info *pi;
struct ice_vsi *vsi;
u8 numtc, tc;
u16 len = 0;
int ret, i;
if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto err;
}
vsi = ice_get_vf_vsi(vf);
if (!vsi) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto err;
}
pi = pf->hw.port_info;
numtc = vsi->tc_cfg.numtc;
vsi_ctx = ice_get_vsi_ctx(pi->hw, vf->lan_vsi_idx);
if (!vsi_ctx) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto err;
}
len = struct_size(cap_list, cap, numtc);
cap_list = kzalloc(len, GFP_KERNEL);
if (!cap_list) {
v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
len = 0;
goto err;
}
cap_list->vsi_id = vsi->vsi_num;
cap_list->num_elem = numtc;
for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
tc = pi->qos_cfg.local_dcbx_cfg.etscfg.prio_table[i];
tc_prio[tc] |= BIT(i);
}
for (i = 0; i < numtc; i++) {
cfg = &cap_list->cap[i];
cfg->tc_num = i;
cfg->tc_prio = tc_prio[i];
cfg->arbiter = pi->qos_cfg.local_dcbx_cfg.etscfg.tsatable[i];
cfg->weight = VIRTCHNL_STRICT_WEIGHT;
cfg->type = VIRTCHNL_BW_SHAPER;
cfg->shaper.committed = vsi_ctx->sched.bw_t_info[i].cir_bw.bw;
cfg->shaper.peak = vsi_ctx->sched.bw_t_info[i].eir_bw.bw;
}
err:
ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_QOS_CAPS, v_ret,
(u8 *)cap_list, len);
kfree(cap_list);
return ret;
}
static int ice_vc_cfg_promiscuous_mode_msg(struct ice_vf *vf, u8 *msg)
{
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
bool rm_promisc, alluni = false, allmulti = false;
struct virtchnl_promisc_info *info =
(struct virtchnl_promisc_info *)msg;
struct ice_vsi_vlan_ops *vlan_ops;
int mcast_err = 0, ucast_err = 0;
struct ice_pf *pf = vf->pf;
struct ice_vsi *vsi;
u8 mcast_m, ucast_m;
struct device *dev;
int ret = 0;
if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
if (!ice_vc_isvalid_vsi_id(vf, info->vsi_id)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
vsi = ice_get_vf_vsi(vf);
if (!vsi) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
dev = ice_pf_to_dev(pf);
if (!ice_is_vf_trusted(vf)) {
dev_err(dev, "Unprivileged VF %d is attempting to configure promiscuous mode\n",
vf->vf_id);
goto error_param;
}
if (info->flags & FLAG_VF_UNICAST_PROMISC)
alluni = true;
if (info->flags & FLAG_VF_MULTICAST_PROMISC)
allmulti = true;
rm_promisc = !allmulti && !alluni;
vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
if (rm_promisc)
ret = vlan_ops->ena_rx_filtering(vsi);
else
ret = vlan_ops->dis_rx_filtering(vsi);
if (ret) {
dev_err(dev, "Failed to configure VLAN pruning in promiscuous mode\n");
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
ice_vf_get_promisc_masks(vf, vsi, &ucast_m, &mcast_m);
if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
if (alluni) {
ret = ice_set_dflt_vsi(vsi);
} else {
if (ice_is_dflt_vsi_in_use(vsi->port_info))
ret = ice_clear_dflt_vsi(vsi);
}
if (allmulti)
mcast_err = ice_vf_set_vsi_promisc(vf, vsi, mcast_m);
else
mcast_err = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
if (ret) {
dev_err(dev, "Turning on/off promiscuous mode for VF %d failed, error: %d\n",
vf->vf_id, ret);
v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
goto error_param;
}
} else {
if (alluni)
ucast_err = ice_vf_set_vsi_promisc(vf, vsi, ucast_m);
else
ucast_err = ice_vf_clear_vsi_promisc(vf, vsi, ucast_m);
if (allmulti)
mcast_err = ice_vf_set_vsi_promisc(vf, vsi, mcast_m);
else
mcast_err = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
if (ucast_err || mcast_err)
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
}
if (!mcast_err) {
if (allmulti &&
!test_and_set_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
dev_info(dev, "VF %u successfully set multicast promiscuous mode\n",
vf->vf_id);
else if (!allmulti &&
test_and_clear_bit(ICE_VF_STATE_MC_PROMISC,
vf->vf_states))
dev_info(dev, "VF %u successfully unset multicast promiscuous mode\n",
vf->vf_id);
} else {
dev_err(dev, "Error while modifying multicast promiscuous mode for VF %u, error: %d\n",
vf->vf_id, mcast_err);
}
if (!ucast_err) {
if (alluni &&
!test_and_set_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
dev_info(dev, "VF %u successfully set unicast promiscuous mode\n",
vf->vf_id);
else if (!alluni &&
test_and_clear_bit(ICE_VF_STATE_UC_PROMISC,
vf->vf_states))
dev_info(dev, "VF %u successfully unset unicast promiscuous mode\n",
vf->vf_id);
} else {
dev_err(dev, "Error while modifying unicast promiscuous mode for VF %u, error: %d\n",
vf->vf_id, ucast_err);
}
error_param:
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
v_ret, NULL, 0);
}
static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
{
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
struct virtchnl_queue_select *vqs =
(struct virtchnl_queue_select *)msg;
struct ice_eth_stats stats = { 0 };
struct ice_vsi *vsi;
if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
vsi = ice_get_vf_vsi(vf);
if (!vsi) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
ice_update_eth_stats(vsi);
stats = vsi->eth_stats;
error_param:
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
(u8 *)&stats, sizeof(stats));
}
static bool ice_can_vf_change_mac(struct ice_vf *vf)
{
if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
return false;
return true;
}
static u8
ice_vc_ether_addr_type(struct virtchnl_ether_addr *vc_ether_addr)
{
return (vc_ether_addr->type & VIRTCHNL_ETHER_ADDR_TYPE_MASK);
}
static bool
ice_is_vc_addr_legacy(struct virtchnl_ether_addr *vc_ether_addr)
{
u8 type = ice_vc_ether_addr_type(vc_ether_addr);
return (type == VIRTCHNL_ETHER_ADDR_LEGACY);
}
static bool
ice_is_vc_addr_primary(struct virtchnl_ether_addr __maybe_unused *vc_ether_addr)
{
u8 type = ice_vc_ether_addr_type(vc_ether_addr);
return (type == VIRTCHNL_ETHER_ADDR_PRIMARY);
}
static void
ice_vfhw_mac_add(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
{
u8 *mac_addr = vc_ether_addr->addr;
if (!is_valid_ether_addr(mac_addr))
return;
if ((ice_is_vc_addr_legacy(vc_ether_addr) &&
is_zero_ether_addr(vf->hw_lan_addr)) ||
ice_is_vc_addr_primary(vc_ether_addr)) {
ether_addr_copy(vf->dev_lan_addr, mac_addr);
ether_addr_copy(vf->hw_lan_addr, mac_addr);
}
if (ice_is_vc_addr_legacy(vc_ether_addr)) {
ether_addr_copy(vf->legacy_last_added_umac.addr,
mac_addr);
vf->legacy_last_added_umac.time_modified = jiffies;
}
}
static bool ice_is_mc_lldp_eth_addr(const u8 *mac)
{
const u8 lldp_mac_base[] = {0x01, 0x80, 0xc2, 0x00, 0x00};
if (memcmp(mac, lldp_mac_base, sizeof(lldp_mac_base)))
return false;
return (mac[5] == 0x0e || mac[5] == 0x03 || mac[5] == 0x00);
}
static bool ice_vc_can_add_mac(const struct ice_vf *vf, const u8 *mac)
{
struct device *dev = ice_pf_to_dev(vf->pf);
if (is_unicast_ether_addr(mac) &&
!ice_can_vf_change_mac((struct ice_vf *)vf)) {
dev_err(dev,
"VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
return false;
}
if (!vf->trusted && ice_is_mc_lldp_eth_addr(mac)) {
dev_warn(dev,
"An untrusted VF %u is attempting to configure an LLDP multicast address\n",
vf->vf_id);
return false;
}
return true;
}
static int
ice_vc_add_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
struct virtchnl_ether_addr *vc_ether_addr)
{
struct device *dev = ice_pf_to_dev(vf->pf);
u8 *mac_addr = vc_ether_addr->addr;
int ret;
if (ether_addr_equal(mac_addr, vf->dev_lan_addr))
return 0;
if (!ice_vc_can_add_mac(vf, mac_addr))
return -EPERM;
ret = ice_fltr_add_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
if (ret == -EEXIST) {
dev_dbg(dev, "MAC %pM already exists for VF %d\n", mac_addr,
vf->vf_id);
} else if (ret) {
dev_err(dev, "Failed to add MAC %pM for VF %d\n, error %d\n",
mac_addr, vf->vf_id, ret);
return ret;
} else {
vf->num_mac++;
if (ice_is_mc_lldp_eth_addr(mac_addr))
ice_vf_update_mac_lldp_num(vf, vsi, true);
}
ice_vfhw_mac_add(vf, vc_ether_addr);
return ret;
}
static bool ice_is_legacy_umac_expired(struct ice_time_mac *last_added_umac)
{
#define ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME msecs_to_jiffies(3000)
return time_is_before_jiffies(last_added_umac->time_modified +
ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME);
}
static void
ice_update_legacy_cached_mac(struct ice_vf *vf,
struct virtchnl_ether_addr *vc_ether_addr)
{
if (!ice_is_vc_addr_legacy(vc_ether_addr) ||
ice_is_legacy_umac_expired(&vf->legacy_last_added_umac))
return;
ether_addr_copy(vf->dev_lan_addr, vf->legacy_last_added_umac.addr);
ether_addr_copy(vf->hw_lan_addr, vf->legacy_last_added_umac.addr);
}
static void
ice_vfhw_mac_del(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
{
u8 *mac_addr = vc_ether_addr->addr;
if (!is_valid_ether_addr(mac_addr) ||
!ether_addr_equal(vf->dev_lan_addr, mac_addr))
return;
eth_zero_addr(vf->dev_lan_addr);
ice_update_legacy_cached_mac(vf, vc_ether_addr);
}
static int
ice_vc_del_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
struct virtchnl_ether_addr *vc_ether_addr)
{
struct device *dev = ice_pf_to_dev(vf->pf);
u8 *mac_addr = vc_ether_addr->addr;
int status;
if (!ice_can_vf_change_mac(vf) &&
ether_addr_equal(vf->dev_lan_addr, mac_addr))
return 0;
status = ice_fltr_remove_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
if (status == -ENOENT) {
dev_err(dev, "MAC %pM does not exist for VF %d\n", mac_addr,
vf->vf_id);
return -ENOENT;
} else if (status) {
dev_err(dev, "Failed to delete MAC %pM for VF %d, error %d\n",
mac_addr, vf->vf_id, status);
return -EIO;
}
ice_vfhw_mac_del(vf, vc_ether_addr);
vf->num_mac--;
if (ice_is_mc_lldp_eth_addr(mac_addr))
ice_vf_update_mac_lldp_num(vf, vsi, false);
return 0;
}
static int
ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
{
int (*ice_vc_cfg_mac)
(struct ice_vf *vf, struct ice_vsi *vsi,
struct virtchnl_ether_addr *virtchnl_ether_addr);
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
struct virtchnl_ether_addr_list *al =
(struct virtchnl_ether_addr_list *)msg;
struct ice_pf *pf = vf->pf;
enum virtchnl_ops vc_op;
struct ice_vsi *vsi;
int i;
if (set) {
vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
ice_vc_cfg_mac = ice_vc_add_mac_addr;
} else {
vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
ice_vc_cfg_mac = ice_vc_del_mac_addr;
}
if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
!ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto handle_mac_exit;
}
if (set && !ice_is_vf_trusted(vf) &&
(vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
dev_err(ice_pf_to_dev(pf), "Can't add more MAC addresses, because VF-%d is not trusted, switch the VF to trusted mode in order to add more functionalities\n",
vf->vf_id);
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto handle_mac_exit;
}
vsi = ice_get_vf_vsi(vf);
if (!vsi) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto handle_mac_exit;
}
for (i = 0; i < al->num_elements; i++) {
u8 *mac_addr = al->list[i].addr;
int result;
if (is_broadcast_ether_addr(mac_addr) ||
is_zero_ether_addr(mac_addr))
continue;
result = ice_vc_cfg_mac(vf, vsi, &al->list[i]);
if (result == -EEXIST || result == -ENOENT) {
continue;
} else if (result) {
v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
goto handle_mac_exit;
}
}
handle_mac_exit:
return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
}
static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
{
return ice_vc_handle_mac_addr_msg(vf, msg, true);
}
static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
{
return ice_vc_handle_mac_addr_msg(vf, msg, false);
}
static bool ice_vf_vlan_offload_ena(u32 caps)
{
return !!(caps & VIRTCHNL_VF_OFFLOAD_VLAN);
}
bool ice_is_vlan_promisc_allowed(struct ice_vf *vf)
{
if ((test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) &&
test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, vf->pf->flags))
return true;
return false;
}
int ice_vf_ena_vlan_promisc(struct ice_vf *vf, struct ice_vsi *vsi,
struct ice_vlan *vlan)
{
u8 promisc_m = 0;
int status;
if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
promisc_m |= ICE_UCAST_VLAN_PROMISC_BITS;
if (test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
promisc_m |= ICE_MCAST_VLAN_PROMISC_BITS;
if (!promisc_m)
return 0;
status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m,
vlan->vid);
if (status && status != -EEXIST)
return status;
return 0;
}
static int ice_vf_dis_vlan_promisc(struct ice_vsi *vsi, struct ice_vlan *vlan)
{
u8 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS | ICE_MCAST_VLAN_PROMISC_BITS;
int status;
status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m,
vlan->vid);
if (status && status != -ENOENT)
return status;
return 0;
}
static bool ice_vf_has_max_vlans(struct ice_vf *vf, struct ice_vsi *vsi)
{
if (ice_is_vf_trusted(vf))
return false;
#define ICE_VF_ADDED_VLAN_ZERO_FLTRS 1
return ((ice_vsi_num_non_zero_vlans(vsi) +
ICE_VF_ADDED_VLAN_ZERO_FLTRS) >= ICE_MAX_VLAN_PER_VF);
}
static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
{
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
struct virtchnl_vlan_filter_list *vfl =
(struct virtchnl_vlan_filter_list *)msg;
struct ice_pf *pf = vf->pf;
bool vlan_promisc = false;
struct ice_vsi *vsi;
struct device *dev;
int status = 0;
int i;
dev = ice_pf_to_dev(pf);
if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
for (i = 0; i < vfl->num_elements; i++) {
if (vfl->vlan_id[i] >= VLAN_N_VID) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
dev_err(dev, "invalid VF VLAN id %d\n",
vfl->vlan_id[i]);
goto error_param;
}
}
vsi = ice_get_vf_vsi(vf);
if (!vsi) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
if (add_v && ice_vf_has_max_vlans(vf, vsi)) {
dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
vf->vf_id);
goto error_param;
}
if (ice_vf_is_port_vlan_ena(vf) && !ice_is_dvm_ena(&pf->hw)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
vlan_promisc = ice_is_vlan_promisc_allowed(vf) &&
!ice_is_dvm_ena(&pf->hw) &&
!ice_vf_is_port_vlan_ena(vf);
if (add_v) {
for (i = 0; i < vfl->num_elements; i++) {
u16 vid = vfl->vlan_id[i];
struct ice_vlan vlan;
if (ice_vf_has_max_vlans(vf, vsi)) {
dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
vf->vf_id);
goto error_param;
}
if (!vid)
continue;
vlan = ICE_VLAN(ETH_P_8021Q, vid, 0);
status = vsi->inner_vlan_ops.add_vlan(vsi, &vlan);
if (status) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
if (!vlan_promisc && vid && !ice_is_dvm_ena(&pf->hw)) {
if (vf->spoofchk) {
status = vsi->inner_vlan_ops.ena_tx_filtering(vsi);
if (status) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
dev_err(dev, "Enable VLAN anti-spoofing on VLAN ID: %d failed error-%d\n",
vid, status);
goto error_param;
}
}
if (vsi->inner_vlan_ops.ena_rx_filtering(vsi)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
dev_err(dev, "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
vid, status);
goto error_param;
}
} else if (vlan_promisc) {
status = ice_vf_ena_vlan_promisc(vf, vsi, &vlan);
if (status) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
dev_err(dev, "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
vid, status);
}
}
}
} else {
int num_vf_vlan;
num_vf_vlan = vsi->num_vlan;
for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
u16 vid = vfl->vlan_id[i];
struct ice_vlan vlan;
if (!vid)
continue;
vlan = ICE_VLAN(ETH_P_8021Q, vid, 0);
status = vsi->inner_vlan_ops.del_vlan(vsi, &vlan);
if (status) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
if (!ice_vsi_has_non_zero_vlans(vsi)) {
vsi->inner_vlan_ops.dis_tx_filtering(vsi);
vsi->inner_vlan_ops.dis_rx_filtering(vsi);
}
if (vlan_promisc)
ice_vf_dis_vlan_promisc(vsi, &vlan);
}
}
error_param:
if (add_v)
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
NULL, 0);
else
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
NULL, 0);
}
static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
{
return ice_vc_process_vlan_msg(vf, msg, true);
}
static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
{
return ice_vc_process_vlan_msg(vf, msg, false);
}
static bool ice_vsi_is_rxq_crc_strip_dis(struct ice_vsi *vsi)
{
unsigned int i;
ice_for_each_alloc_rxq(vsi, i)
if (vsi->rx_rings[i]->flags & ICE_RX_FLAGS_CRC_STRIP_DIS)
return true;
return false;
}
static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
{
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
struct ice_vsi *vsi;
if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
vsi = ice_get_vf_vsi(vf);
if (!vsi) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
if (vsi->inner_vlan_ops.ena_stripping(vsi, ETH_P_8021Q))
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
else
vf->vlan_strip_ena |= ICE_INNER_VLAN_STRIP_ENA;
error_param:
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
v_ret, NULL, 0);
}
static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
{
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
struct ice_vsi *vsi;
if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
vsi = ice_get_vf_vsi(vf);
if (!vsi) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto error_param;
}
if (vsi->inner_vlan_ops.dis_stripping(vsi))
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
else
vf->vlan_strip_ena &= ~ICE_INNER_VLAN_STRIP_ENA;
error_param:
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
v_ret, NULL, 0);
}
static int ice_vc_query_rxdid(struct ice_vf *vf)
{
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
struct ice_pf *pf = vf->pf;
u64 rxdid;
if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto err;
}
if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto err;
}
rxdid = pf->supported_rxdids;
err:
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_SUPPORTED_RXDIDS,
v_ret, (u8 *)&rxdid, sizeof(rxdid));
}
static int ice_vf_init_vlan_stripping(struct ice_vf *vf)
{
struct ice_vsi *vsi = ice_get_vf_vsi(vf);
vf->vlan_strip_ena = 0;
if (!vsi)
return -EINVAL;
if (ice_vf_is_port_vlan_ena(vf) && !ice_is_dvm_ena(&vsi->back->hw))
return 0;
if (ice_vf_vlan_offload_ena(vf->driver_caps)) {
int err;
err = vsi->inner_vlan_ops.ena_stripping(vsi, ETH_P_8021Q);
if (!err)
vf->vlan_strip_ena |= ICE_INNER_VLAN_STRIP_ENA;
return err;
}
return vsi->inner_vlan_ops.dis_stripping(vsi);
}
static u16 ice_vc_get_max_vlan_fltrs(struct ice_vf *vf)
{
if (vf->trusted)
return VLAN_N_VID;
else
return ICE_MAX_VLAN_PER_VF;
}
static bool ice_vf_outer_vlan_not_allowed(struct ice_vf *vf)
{
if (ice_vf_is_port_vlan_ena(vf))
return true;
return false;
}
static void
ice_vc_set_dvm_caps(struct ice_vf *vf, struct virtchnl_vlan_caps *caps)
{
struct virtchnl_vlan_supported_caps *supported_caps;
if (ice_vf_outer_vlan_not_allowed(vf)) {
supported_caps = &caps->filtering.filtering_support;
supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
supported_caps = &caps->offloads.stripping_support;
supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
VIRTCHNL_VLAN_TOGGLE |
VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
supported_caps = &caps->offloads.insertion_support;
supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
VIRTCHNL_VLAN_TOGGLE |
VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
caps->offloads.ethertype_match =
VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
} else {
supported_caps = &caps->filtering.filtering_support;
supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
supported_caps->outer = VIRTCHNL_VLAN_ETHERTYPE_8100 |
VIRTCHNL_VLAN_ETHERTYPE_88A8 |
VIRTCHNL_VLAN_ETHERTYPE_9100 |
VIRTCHNL_VLAN_ETHERTYPE_AND;
caps->filtering.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100 |
VIRTCHNL_VLAN_ETHERTYPE_88A8 |
VIRTCHNL_VLAN_ETHERTYPE_9100;
supported_caps = &caps->offloads.stripping_support;
supported_caps->inner = VIRTCHNL_VLAN_TOGGLE |
VIRTCHNL_VLAN_ETHERTYPE_8100 |
VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
supported_caps->outer = VIRTCHNL_VLAN_TOGGLE |
VIRTCHNL_VLAN_ETHERTYPE_8100 |
VIRTCHNL_VLAN_ETHERTYPE_88A8 |
VIRTCHNL_VLAN_ETHERTYPE_9100 |
VIRTCHNL_VLAN_ETHERTYPE_XOR |
VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2;
supported_caps = &caps->offloads.insertion_support;
supported_caps->inner = VIRTCHNL_VLAN_TOGGLE |
VIRTCHNL_VLAN_ETHERTYPE_8100 |
VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
supported_caps->outer = VIRTCHNL_VLAN_TOGGLE |
VIRTCHNL_VLAN_ETHERTYPE_8100 |
VIRTCHNL_VLAN_ETHERTYPE_88A8 |
VIRTCHNL_VLAN_ETHERTYPE_9100 |
VIRTCHNL_VLAN_ETHERTYPE_XOR |
VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2;
caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
caps->offloads.ethertype_match =
VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
}
caps->filtering.max_filters = ice_vc_get_max_vlan_fltrs(vf);
}
static void
ice_vc_set_svm_caps(struct ice_vf *vf, struct virtchnl_vlan_caps *caps)
{
struct virtchnl_vlan_supported_caps *supported_caps;
if (ice_vf_is_port_vlan_ena(vf)) {
supported_caps = &caps->filtering.filtering_support;
supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
supported_caps = &caps->offloads.stripping_support;
supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
supported_caps = &caps->offloads.insertion_support;
supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
caps->offloads.ethertype_init = VIRTCHNL_VLAN_UNSUPPORTED;
caps->offloads.ethertype_match = VIRTCHNL_VLAN_UNSUPPORTED;
caps->filtering.max_filters = 0;
} else {
supported_caps = &caps->filtering.filtering_support;
supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100;
supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
caps->filtering.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
supported_caps = &caps->offloads.stripping_support;
supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
VIRTCHNL_VLAN_TOGGLE |
VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
supported_caps = &caps->offloads.insertion_support;
supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
VIRTCHNL_VLAN_TOGGLE |
VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
caps->offloads.ethertype_match =
VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
caps->filtering.max_filters = ice_vc_get_max_vlan_fltrs(vf);
}
}
static int ice_vc_get_offload_vlan_v2_caps(struct ice_vf *vf)
{
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
struct virtchnl_vlan_caps *caps = NULL;
int err, len = 0;
if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
caps = kzalloc_obj(*caps);
if (!caps) {
v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
goto out;
}
len = sizeof(*caps);
if (ice_is_dvm_ena(&vf->pf->hw))
ice_vc_set_dvm_caps(vf, caps);
else
ice_vc_set_svm_caps(vf, caps);
memcpy(&vf->vlan_v2_caps, caps, sizeof(*caps));
out:
err = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS,
v_ret, (u8 *)caps, len);
kfree(caps);
return err;
}
static bool ice_vc_validate_vlan_tpid(u16 filtering_caps, u16 tpid)
{
enum virtchnl_vlan_support vlan_ethertype = VIRTCHNL_VLAN_UNSUPPORTED;
switch (tpid) {
case ETH_P_8021Q:
vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_8100;
break;
case ETH_P_8021AD:
vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_88A8;
break;
case ETH_P_QINQ1:
vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_9100;
break;
}
if (!(filtering_caps & vlan_ethertype))
return false;
return true;
}
static bool ice_vc_is_valid_vlan(struct virtchnl_vlan *vc_vlan)
{
if (!vc_vlan->tci || !vc_vlan->tpid)
return false;
return true;
}
static bool
ice_vc_validate_vlan_filter_list(struct virtchnl_vlan_filtering_caps *vfc,
struct virtchnl_vlan_filter_list_v2 *vfl)
{
u16 i;
if (!vfl->num_elements)
return false;
for (i = 0; i < vfl->num_elements; i++) {
struct virtchnl_vlan_supported_caps *filtering_support =
&vfc->filtering_support;
struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
struct virtchnl_vlan *outer = &vlan_fltr->outer;
struct virtchnl_vlan *inner = &vlan_fltr->inner;
if ((ice_vc_is_valid_vlan(outer) &&
filtering_support->outer == VIRTCHNL_VLAN_UNSUPPORTED) ||
(ice_vc_is_valid_vlan(inner) &&
filtering_support->inner == VIRTCHNL_VLAN_UNSUPPORTED))
return false;
if ((outer->tci_mask &&
!(filtering_support->outer & VIRTCHNL_VLAN_FILTER_MASK)) ||
(inner->tci_mask &&
!(filtering_support->inner & VIRTCHNL_VLAN_FILTER_MASK)))
return false;
if (((outer->tci & VLAN_PRIO_MASK) &&
!(filtering_support->outer & VIRTCHNL_VLAN_PRIO)) ||
((inner->tci & VLAN_PRIO_MASK) &&
!(filtering_support->inner & VIRTCHNL_VLAN_PRIO)))
return false;
if ((ice_vc_is_valid_vlan(outer) &&
!ice_vc_validate_vlan_tpid(filtering_support->outer,
outer->tpid)) ||
(ice_vc_is_valid_vlan(inner) &&
!ice_vc_validate_vlan_tpid(filtering_support->inner,
inner->tpid)))
return false;
}
return true;
}
static struct ice_vlan ice_vc_to_vlan(struct virtchnl_vlan *vc_vlan)
{
struct ice_vlan vlan = { 0 };
vlan.prio = FIELD_GET(VLAN_PRIO_MASK, vc_vlan->tci);
vlan.vid = vc_vlan->tci & VLAN_VID_MASK;
vlan.tpid = vc_vlan->tpid;
return vlan;
}
static int
ice_vc_vlan_action(struct ice_vsi *vsi,
int (*vlan_action)(struct ice_vsi *, struct ice_vlan *),
struct ice_vlan *vlan)
{
int err;
err = vlan_action(vsi, vlan);
if (err)
return err;
return 0;
}
static int
ice_vc_del_vlans(struct ice_vf *vf, struct ice_vsi *vsi,
struct virtchnl_vlan_filter_list_v2 *vfl)
{
bool vlan_promisc = ice_is_vlan_promisc_allowed(vf);
int err;
u16 i;
for (i = 0; i < vfl->num_elements; i++) {
struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
struct virtchnl_vlan *vc_vlan;
vc_vlan = &vlan_fltr->outer;
if (ice_vc_is_valid_vlan(vc_vlan)) {
struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
err = ice_vc_vlan_action(vsi,
vsi->outer_vlan_ops.del_vlan,
&vlan);
if (err)
return err;
if (vlan_promisc)
ice_vf_dis_vlan_promisc(vsi, &vlan);
if (!ice_vsi_has_non_zero_vlans(vsi) && ice_is_dvm_ena(&vsi->back->hw)) {
err = vsi->outer_vlan_ops.dis_tx_filtering(vsi);
if (err)
return err;
}
}
vc_vlan = &vlan_fltr->inner;
if (ice_vc_is_valid_vlan(vc_vlan)) {
struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
err = ice_vc_vlan_action(vsi,
vsi->inner_vlan_ops.del_vlan,
&vlan);
if (err)
return err;
if (!ice_is_dvm_ena(&vsi->back->hw)) {
if (vlan_promisc)
ice_vf_dis_vlan_promisc(vsi, &vlan);
if (!ice_vsi_has_non_zero_vlans(vsi)) {
err = vsi->inner_vlan_ops.dis_tx_filtering(vsi);
if (err)
return err;
}
}
}
}
return 0;
}
static int ice_vc_remove_vlan_v2_msg(struct ice_vf *vf, u8 *msg)
{
struct virtchnl_vlan_filter_list_v2 *vfl =
(struct virtchnl_vlan_filter_list_v2 *)msg;
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
struct ice_vsi *vsi;
if (!ice_vc_validate_vlan_filter_list(&vf->vlan_v2_caps.filtering,
vfl)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
if (!ice_vc_isvalid_vsi_id(vf, vfl->vport_id)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
vsi = ice_get_vf_vsi(vf);
if (!vsi) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
if (ice_vc_del_vlans(vf, vsi, vfl))
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
out:
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN_V2, v_ret, NULL,
0);
}
static int
ice_vc_add_vlans(struct ice_vf *vf, struct ice_vsi *vsi,
struct virtchnl_vlan_filter_list_v2 *vfl)
{
bool vlan_promisc = ice_is_vlan_promisc_allowed(vf);
int err;
u16 i;
for (i = 0; i < vfl->num_elements; i++) {
struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
struct virtchnl_vlan *vc_vlan;
vc_vlan = &vlan_fltr->outer;
if (ice_vc_is_valid_vlan(vc_vlan)) {
struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
err = ice_vc_vlan_action(vsi,
vsi->outer_vlan_ops.add_vlan,
&vlan);
if (err)
return err;
if (vlan_promisc) {
err = ice_vf_ena_vlan_promisc(vf, vsi, &vlan);
if (err)
return err;
}
if (vf->spoofchk && vlan.vid && ice_is_dvm_ena(&vsi->back->hw)) {
err = vsi->outer_vlan_ops.ena_tx_filtering(vsi);
if (err)
return err;
}
}
vc_vlan = &vlan_fltr->inner;
if (ice_vc_is_valid_vlan(vc_vlan)) {
struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
err = ice_vc_vlan_action(vsi,
vsi->inner_vlan_ops.add_vlan,
&vlan);
if (err)
return err;
if (!ice_is_dvm_ena(&vsi->back->hw)) {
if (vlan_promisc) {
err = ice_vf_ena_vlan_promisc(vf, vsi,
&vlan);
if (err)
return err;
}
if (vf->spoofchk && vlan.vid) {
err = vsi->inner_vlan_ops.ena_tx_filtering(vsi);
if (err)
return err;
}
}
}
}
return 0;
}
static bool
ice_vc_validate_add_vlan_filter_list(struct ice_vsi *vsi,
struct virtchnl_vlan_filtering_caps *vfc,
struct virtchnl_vlan_filter_list_v2 *vfl)
{
u16 num_requested_filters = ice_vsi_num_non_zero_vlans(vsi) +
vfl->num_elements;
if (num_requested_filters > vfc->max_filters)
return false;
return ice_vc_validate_vlan_filter_list(vfc, vfl);
}
static int ice_vc_add_vlan_v2_msg(struct ice_vf *vf, u8 *msg)
{
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
struct virtchnl_vlan_filter_list_v2 *vfl =
(struct virtchnl_vlan_filter_list_v2 *)msg;
struct ice_vsi *vsi;
if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
if (!ice_vc_isvalid_vsi_id(vf, vfl->vport_id)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
vsi = ice_get_vf_vsi(vf);
if (!vsi) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
if (!ice_vc_validate_add_vlan_filter_list(vsi,
&vf->vlan_v2_caps.filtering,
vfl)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
if (ice_vc_add_vlans(vf, vsi, vfl))
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
out:
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN_V2, v_ret, NULL,
0);
}
static bool
ice_vc_valid_vlan_setting(u32 negotiated_settings, u32 ethertype_setting)
{
if (ethertype_setting && !(negotiated_settings & ethertype_setting))
return false;
if (!(negotiated_settings & VIRTCHNL_VLAN_ETHERTYPE_AND) &&
hweight32(ethertype_setting) > 1)
return false;
if (!(negotiated_settings & VIRTCHNL_VLAN_TOGGLE))
return false;
return true;
}
static bool
ice_vc_valid_vlan_setting_msg(struct virtchnl_vlan_supported_caps *caps,
struct virtchnl_vlan_setting *msg)
{
if ((!msg->outer_ethertype_setting &&
!msg->inner_ethertype_setting) ||
(!caps->outer && !caps->inner))
return false;
if (msg->outer_ethertype_setting &&
!ice_vc_valid_vlan_setting(caps->outer,
msg->outer_ethertype_setting))
return false;
if (msg->inner_ethertype_setting &&
!ice_vc_valid_vlan_setting(caps->inner,
msg->inner_ethertype_setting))
return false;
return true;
}
static int ice_vc_get_tpid(u32 ethertype_setting, u16 *tpid)
{
switch (ethertype_setting) {
case VIRTCHNL_VLAN_ETHERTYPE_8100:
*tpid = ETH_P_8021Q;
break;
case VIRTCHNL_VLAN_ETHERTYPE_88A8:
*tpid = ETH_P_8021AD;
break;
case VIRTCHNL_VLAN_ETHERTYPE_9100:
*tpid = ETH_P_QINQ1;
break;
default:
*tpid = 0;
return -EINVAL;
}
return 0;
}
static int
ice_vc_ena_vlan_offload(struct ice_vsi *vsi,
int (*ena_offload)(struct ice_vsi *vsi, u16 tpid),
u32 ethertype_setting)
{
u16 tpid;
int err;
err = ice_vc_get_tpid(ethertype_setting, &tpid);
if (err)
return err;
err = ena_offload(vsi, tpid);
if (err)
return err;
return 0;
}
static int ice_vc_ena_vlan_stripping_v2_msg(struct ice_vf *vf, u8 *msg)
{
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
struct virtchnl_vlan_supported_caps *stripping_support;
struct virtchnl_vlan_setting *strip_msg =
(struct virtchnl_vlan_setting *)msg;
u32 ethertype_setting;
struct ice_vsi *vsi;
if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
if (!ice_vc_isvalid_vsi_id(vf, strip_msg->vport_id)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
vsi = ice_get_vf_vsi(vf);
if (!vsi) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
stripping_support = &vf->vlan_v2_caps.offloads.stripping_support;
if (!ice_vc_valid_vlan_setting_msg(stripping_support, strip_msg)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
if (ice_vsi_is_rxq_crc_strip_dis(vsi)) {
v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
goto out;
}
ethertype_setting = strip_msg->outer_ethertype_setting;
if (ethertype_setting) {
if (ice_vc_ena_vlan_offload(vsi,
vsi->outer_vlan_ops.ena_stripping,
ethertype_setting)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
} else {
enum ice_l2tsel l2tsel =
ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND;
ice_vsi_update_l2tsel(vsi, l2tsel);
vf->vlan_strip_ena |= ICE_OUTER_VLAN_STRIP_ENA;
}
}
ethertype_setting = strip_msg->inner_ethertype_setting;
if (ethertype_setting &&
ice_vc_ena_vlan_offload(vsi, vsi->inner_vlan_ops.ena_stripping,
ethertype_setting)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
if (ethertype_setting)
vf->vlan_strip_ena |= ICE_INNER_VLAN_STRIP_ENA;
out:
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2,
v_ret, NULL, 0);
}
static int ice_vc_dis_vlan_stripping_v2_msg(struct ice_vf *vf, u8 *msg)
{
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
struct virtchnl_vlan_supported_caps *stripping_support;
struct virtchnl_vlan_setting *strip_msg =
(struct virtchnl_vlan_setting *)msg;
u32 ethertype_setting;
struct ice_vsi *vsi;
if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
if (!ice_vc_isvalid_vsi_id(vf, strip_msg->vport_id)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
vsi = ice_get_vf_vsi(vf);
if (!vsi) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
stripping_support = &vf->vlan_v2_caps.offloads.stripping_support;
if (!ice_vc_valid_vlan_setting_msg(stripping_support, strip_msg)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
ethertype_setting = strip_msg->outer_ethertype_setting;
if (ethertype_setting) {
if (vsi->outer_vlan_ops.dis_stripping(vsi)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
} else {
enum ice_l2tsel l2tsel =
ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG1;
ice_vsi_update_l2tsel(vsi, l2tsel);
vf->vlan_strip_ena &= ~ICE_OUTER_VLAN_STRIP_ENA;
}
}
ethertype_setting = strip_msg->inner_ethertype_setting;
if (ethertype_setting && vsi->inner_vlan_ops.dis_stripping(vsi)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
if (ethertype_setting)
vf->vlan_strip_ena &= ~ICE_INNER_VLAN_STRIP_ENA;
out:
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2,
v_ret, NULL, 0);
}
static int ice_vc_ena_vlan_insertion_v2_msg(struct ice_vf *vf, u8 *msg)
{
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
struct virtchnl_vlan_supported_caps *insertion_support;
struct virtchnl_vlan_setting *insertion_msg =
(struct virtchnl_vlan_setting *)msg;
u32 ethertype_setting;
struct ice_vsi *vsi;
if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
if (!ice_vc_isvalid_vsi_id(vf, insertion_msg->vport_id)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
vsi = ice_get_vf_vsi(vf);
if (!vsi) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
insertion_support = &vf->vlan_v2_caps.offloads.insertion_support;
if (!ice_vc_valid_vlan_setting_msg(insertion_support, insertion_msg)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
ethertype_setting = insertion_msg->outer_ethertype_setting;
if (ethertype_setting &&
ice_vc_ena_vlan_offload(vsi, vsi->outer_vlan_ops.ena_insertion,
ethertype_setting)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
ethertype_setting = insertion_msg->inner_ethertype_setting;
if (ethertype_setting &&
ice_vc_ena_vlan_offload(vsi, vsi->inner_vlan_ops.ena_insertion,
ethertype_setting)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
out:
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2,
v_ret, NULL, 0);
}
static int ice_vc_dis_vlan_insertion_v2_msg(struct ice_vf *vf, u8 *msg)
{
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
struct virtchnl_vlan_supported_caps *insertion_support;
struct virtchnl_vlan_setting *insertion_msg =
(struct virtchnl_vlan_setting *)msg;
u32 ethertype_setting;
struct ice_vsi *vsi;
if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
if (!ice_vc_isvalid_vsi_id(vf, insertion_msg->vport_id)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
vsi = ice_get_vf_vsi(vf);
if (!vsi) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
insertion_support = &vf->vlan_v2_caps.offloads.insertion_support;
if (!ice_vc_valid_vlan_setting_msg(insertion_support, insertion_msg)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
ethertype_setting = insertion_msg->outer_ethertype_setting;
if (ethertype_setting && vsi->outer_vlan_ops.dis_insertion(vsi)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
ethertype_setting = insertion_msg->inner_ethertype_setting;
if (ethertype_setting && vsi->inner_vlan_ops.dis_insertion(vsi)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto out;
}
out:
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2,
v_ret, NULL, 0);
}
static int ice_vc_get_ptp_cap(struct ice_vf *vf,
const struct virtchnl_ptp_caps *msg)
{
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_ERR_PARAM;
u32 caps = VIRTCHNL_1588_PTP_CAP_RX_TSTAMP |
VIRTCHNL_1588_PTP_CAP_READ_PHC;
if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
goto err;
v_ret = VIRTCHNL_STATUS_SUCCESS;
if (msg->caps & caps)
vf->ptp_caps = caps;
err:
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_1588_PTP_GET_CAPS, v_ret,
(u8 *)&vf->ptp_caps,
sizeof(struct virtchnl_ptp_caps));
}
static int ice_vc_get_phc_time(struct ice_vf *vf)
{
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_ERR_PARAM;
struct virtchnl_phc_time *phc_time = NULL;
struct ice_pf *pf = vf->pf;
u32 len = 0;
int ret;
if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
goto err;
v_ret = VIRTCHNL_STATUS_SUCCESS;
phc_time = kzalloc_obj(*phc_time);
if (!phc_time) {
v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
goto err;
}
len = sizeof(*phc_time);
phc_time->time = ice_ptp_read_src_clk_reg(pf, NULL);
err:
ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_1588_PTP_GET_TIME, v_ret,
(u8 *)phc_time, len);
kfree(phc_time);
return ret;
}
static const struct ice_virtchnl_ops ice_virtchnl_dflt_ops = {
.get_ver_msg = ice_vc_get_ver_msg,
.get_vf_res_msg = ice_vc_get_vf_res_msg,
.reset_vf = ice_vc_reset_vf_msg,
.add_mac_addr_msg = ice_vc_add_mac_addr_msg,
.del_mac_addr_msg = ice_vc_del_mac_addr_msg,
.cfg_qs_msg = ice_vc_cfg_qs_msg,
.ena_qs_msg = ice_vc_ena_qs_msg,
.dis_qs_msg = ice_vc_dis_qs_msg,
.request_qs_msg = ice_vc_request_qs_msg,
.cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
.config_rss_key = ice_vc_config_rss_key,
.config_rss_lut = ice_vc_config_rss_lut,
.config_rss_hfunc = ice_vc_config_rss_hfunc,
.get_stats_msg = ice_vc_get_stats_msg,
.cfg_promiscuous_mode_msg = ice_vc_cfg_promiscuous_mode_msg,
.add_vlan_msg = ice_vc_add_vlan_msg,
.remove_vlan_msg = ice_vc_remove_vlan_msg,
.query_rxdid = ice_vc_query_rxdid,
.get_rss_hashcfg = ice_vc_get_rss_hashcfg,
.set_rss_hashcfg = ice_vc_set_rss_hashcfg,
.ena_vlan_stripping = ice_vc_ena_vlan_stripping,
.dis_vlan_stripping = ice_vc_dis_vlan_stripping,
.handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
.add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
.del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
.get_offload_vlan_v2_caps = ice_vc_get_offload_vlan_v2_caps,
.add_vlan_v2_msg = ice_vc_add_vlan_v2_msg,
.remove_vlan_v2_msg = ice_vc_remove_vlan_v2_msg,
.ena_vlan_stripping_v2_msg = ice_vc_ena_vlan_stripping_v2_msg,
.dis_vlan_stripping_v2_msg = ice_vc_dis_vlan_stripping_v2_msg,
.ena_vlan_insertion_v2_msg = ice_vc_ena_vlan_insertion_v2_msg,
.dis_vlan_insertion_v2_msg = ice_vc_dis_vlan_insertion_v2_msg,
.get_qos_caps = ice_vc_get_qos_caps,
.cfg_q_bw = ice_vc_cfg_q_bw,
.cfg_q_quanta = ice_vc_cfg_q_quanta,
.get_ptp_cap = ice_vc_get_ptp_cap,
.get_phc_time = ice_vc_get_phc_time,
};
void ice_virtchnl_set_dflt_ops(struct ice_vf *vf)
{
vf->virtchnl_ops = &ice_virtchnl_dflt_ops;
}
static int ice_vc_repr_add_mac(struct ice_vf *vf, u8 *msg)
{
enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
struct virtchnl_ether_addr_list *al =
(struct virtchnl_ether_addr_list *)msg;
struct ice_vsi *vsi;
struct ice_pf *pf;
int i;
if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
!ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto handle_mac_exit;
}
pf = vf->pf;
vsi = ice_get_vf_vsi(vf);
if (!vsi) {
v_ret = VIRTCHNL_STATUS_ERR_PARAM;
goto handle_mac_exit;
}
for (i = 0; i < al->num_elements; i++) {
u8 *mac_addr = al->list[i].addr;
if (!is_unicast_ether_addr(mac_addr) ||
ether_addr_equal(mac_addr, vf->hw_lan_addr))
continue;
if (vf->pf_set_mac) {
dev_err(ice_pf_to_dev(pf), "VF attempting to override administratively set MAC address\n");
v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
goto handle_mac_exit;
}
ice_vfhw_mac_add(vf, &al->list[i]);
break;
}
handle_mac_exit:
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
v_ret, NULL, 0);
}
static int
ice_vc_repr_del_mac(struct ice_vf __always_unused *vf, u8 __always_unused *msg)
{
struct virtchnl_ether_addr_list *al =
(struct virtchnl_ether_addr_list *)msg;
ice_update_legacy_cached_mac(vf, &al->list[0]);
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
VIRTCHNL_STATUS_SUCCESS, NULL, 0);
}
static int
ice_vc_repr_cfg_promiscuous_mode(struct ice_vf *vf, u8 __always_unused *msg)
{
dev_dbg(ice_pf_to_dev(vf->pf),
"Can't config promiscuous mode in switchdev mode for VF %d\n",
vf->vf_id);
return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
NULL, 0);
}
static const struct ice_virtchnl_ops ice_virtchnl_repr_ops = {
.get_ver_msg = ice_vc_get_ver_msg,
.get_vf_res_msg = ice_vc_get_vf_res_msg,
.reset_vf = ice_vc_reset_vf_msg,
.add_mac_addr_msg = ice_vc_repr_add_mac,
.del_mac_addr_msg = ice_vc_repr_del_mac,
.cfg_qs_msg = ice_vc_cfg_qs_msg,
.ena_qs_msg = ice_vc_ena_qs_msg,
.dis_qs_msg = ice_vc_dis_qs_msg,
.request_qs_msg = ice_vc_request_qs_msg,
.cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
.config_rss_key = ice_vc_config_rss_key,
.config_rss_lut = ice_vc_config_rss_lut,
.config_rss_hfunc = ice_vc_config_rss_hfunc,
.get_stats_msg = ice_vc_get_stats_msg,
.cfg_promiscuous_mode_msg = ice_vc_repr_cfg_promiscuous_mode,
.add_vlan_msg = ice_vc_add_vlan_msg,
.remove_vlan_msg = ice_vc_remove_vlan_msg,
.query_rxdid = ice_vc_query_rxdid,
.get_rss_hashcfg = ice_vc_get_rss_hashcfg,
.set_rss_hashcfg = ice_vc_set_rss_hashcfg,
.ena_vlan_stripping = ice_vc_ena_vlan_stripping,
.dis_vlan_stripping = ice_vc_dis_vlan_stripping,
.handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
.add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
.del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
.get_offload_vlan_v2_caps = ice_vc_get_offload_vlan_v2_caps,
.add_vlan_v2_msg = ice_vc_add_vlan_v2_msg,
.remove_vlan_v2_msg = ice_vc_remove_vlan_v2_msg,
.ena_vlan_stripping_v2_msg = ice_vc_ena_vlan_stripping_v2_msg,
.dis_vlan_stripping_v2_msg = ice_vc_dis_vlan_stripping_v2_msg,
.ena_vlan_insertion_v2_msg = ice_vc_ena_vlan_insertion_v2_msg,
.dis_vlan_insertion_v2_msg = ice_vc_dis_vlan_insertion_v2_msg,
.get_qos_caps = ice_vc_get_qos_caps,
.cfg_q_bw = ice_vc_cfg_q_bw,
.cfg_q_quanta = ice_vc_cfg_q_quanta,
.get_ptp_cap = ice_vc_get_ptp_cap,
.get_phc_time = ice_vc_get_phc_time,
};
void ice_virtchnl_set_repr_ops(struct ice_vf *vf)
{
vf->virtchnl_ops = &ice_virtchnl_repr_ops;
}
static bool
ice_is_malicious_vf(struct ice_vf *vf, struct ice_mbx_data *mbxdata)
{
bool report_malvf = false;
struct device *dev;
struct ice_pf *pf;
int status;
pf = vf->pf;
dev = ice_pf_to_dev(pf);
if (test_bit(ICE_VF_STATE_DIS, vf->vf_states))
return vf->mbx_info.malicious;
status = ice_mbx_vf_state_handler(&pf->hw, mbxdata, &vf->mbx_info,
&report_malvf);
if (status)
dev_warn_ratelimited(dev, "Unable to check status of mailbox overflow for VF %u MAC %pM, status %d\n",
vf->vf_id, vf->dev_lan_addr, status);
if (report_malvf) {
struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
u8 zero_addr[ETH_ALEN] = {};
dev_warn(dev, "VF MAC %pM on PF MAC %pM is generating asynchronous messages and may be overflowing the PF message queue. Please see the Adapter User Guide for more information\n",
vf->dev_lan_addr,
pf_vsi ? pf_vsi->netdev->dev_addr : zero_addr);
}
return vf->mbx_info.malicious;
}
void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event,
struct ice_mbx_data *mbxdata)
{
u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
s16 vf_id = le16_to_cpu(event->desc.retval);
const struct ice_virtchnl_ops *ops;
u16 msglen = event->msg_len;
u8 *msg = event->msg_buf;
struct ice_vf *vf = NULL;
struct device *dev;
int err = 0;
dev = ice_pf_to_dev(pf);
vf = ice_get_vf_by_id(pf, vf_id);
if (!vf) {
dev_err(dev, "Unable to locate VF for message from VF ID %d, opcode %d, len %d\n",
vf_id, v_opcode, msglen);
return;
}
mutex_lock(&vf->cfg_lock);
if (mbxdata && ice_is_malicious_vf(vf, mbxdata))
goto finish;
if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
err = -EPERM;
goto error_handler;
}
ops = vf->virtchnl_ops;
err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
if (err) {
if (err == VIRTCHNL_STATUS_ERR_PARAM)
err = -EPERM;
else
err = -EINVAL;
}
error_handler:
if (err) {
ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
NULL, 0);
dev_err(dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
vf_id, v_opcode, msglen, err);
goto finish;
}
if (!ice_vc_is_opcode_allowed(vf, v_opcode)) {
ice_vc_send_msg_to_vf(vf, v_opcode,
VIRTCHNL_STATUS_ERR_NOT_SUPPORTED, NULL,
0);
goto finish;
}
switch (v_opcode) {
case VIRTCHNL_OP_VERSION:
err = ops->get_ver_msg(vf, msg);
break;
case VIRTCHNL_OP_GET_VF_RESOURCES:
err = ops->get_vf_res_msg(vf, msg);
if (ice_vf_init_vlan_stripping(vf))
dev_dbg(dev, "Failed to initialize VLAN stripping for VF %d\n",
vf->vf_id);
ice_vc_notify_vf_link_state(vf);
break;
case VIRTCHNL_OP_RESET_VF:
ops->reset_vf(vf);
break;
case VIRTCHNL_OP_ADD_ETH_ADDR:
err = ops->add_mac_addr_msg(vf, msg);
break;
case VIRTCHNL_OP_DEL_ETH_ADDR:
err = ops->del_mac_addr_msg(vf, msg);
break;
case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
err = ops->cfg_qs_msg(vf, msg);
break;
case VIRTCHNL_OP_ENABLE_QUEUES:
err = ops->ena_qs_msg(vf, msg);
ice_vc_notify_vf_link_state(vf);
break;
case VIRTCHNL_OP_DISABLE_QUEUES:
err = ops->dis_qs_msg(vf, msg);
break;
case VIRTCHNL_OP_REQUEST_QUEUES:
err = ops->request_qs_msg(vf, msg);
break;
case VIRTCHNL_OP_CONFIG_IRQ_MAP:
err = ops->cfg_irq_map_msg(vf, msg);
break;
case VIRTCHNL_OP_CONFIG_RSS_KEY:
err = ops->config_rss_key(vf, msg);
break;
case VIRTCHNL_OP_CONFIG_RSS_LUT:
err = ops->config_rss_lut(vf, msg);
break;
case VIRTCHNL_OP_CONFIG_RSS_HFUNC:
err = ops->config_rss_hfunc(vf, msg);
break;
case VIRTCHNL_OP_GET_STATS:
err = ops->get_stats_msg(vf, msg);
break;
case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
err = ops->cfg_promiscuous_mode_msg(vf, msg);
break;
case VIRTCHNL_OP_ADD_VLAN:
err = ops->add_vlan_msg(vf, msg);
break;
case VIRTCHNL_OP_DEL_VLAN:
err = ops->remove_vlan_msg(vf, msg);
break;
case VIRTCHNL_OP_GET_SUPPORTED_RXDIDS:
err = ops->query_rxdid(vf);
break;
case VIRTCHNL_OP_GET_RSS_HASHCFG_CAPS:
err = ops->get_rss_hashcfg(vf);
break;
case VIRTCHNL_OP_SET_RSS_HASHCFG:
err = ops->set_rss_hashcfg(vf, msg);
break;
case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
err = ops->ena_vlan_stripping(vf);
break;
case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
err = ops->dis_vlan_stripping(vf);
break;
case VIRTCHNL_OP_ADD_FDIR_FILTER:
err = ops->add_fdir_fltr_msg(vf, msg);
break;
case VIRTCHNL_OP_DEL_FDIR_FILTER:
err = ops->del_fdir_fltr_msg(vf, msg);
break;
case VIRTCHNL_OP_ADD_RSS_CFG:
err = ops->handle_rss_cfg_msg(vf, msg, true);
break;
case VIRTCHNL_OP_DEL_RSS_CFG:
err = ops->handle_rss_cfg_msg(vf, msg, false);
break;
case VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS:
err = ops->get_offload_vlan_v2_caps(vf);
break;
case VIRTCHNL_OP_ADD_VLAN_V2:
err = ops->add_vlan_v2_msg(vf, msg);
break;
case VIRTCHNL_OP_DEL_VLAN_V2:
err = ops->remove_vlan_v2_msg(vf, msg);
break;
case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2:
err = ops->ena_vlan_stripping_v2_msg(vf, msg);
break;
case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2:
err = ops->dis_vlan_stripping_v2_msg(vf, msg);
break;
case VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2:
err = ops->ena_vlan_insertion_v2_msg(vf, msg);
break;
case VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2:
err = ops->dis_vlan_insertion_v2_msg(vf, msg);
break;
case VIRTCHNL_OP_GET_QOS_CAPS:
err = ops->get_qos_caps(vf);
break;
case VIRTCHNL_OP_CONFIG_QUEUE_BW:
err = ops->cfg_q_bw(vf, msg);
break;
case VIRTCHNL_OP_CONFIG_QUANTA:
err = ops->cfg_q_quanta(vf, msg);
break;
case VIRTCHNL_OP_1588_PTP_GET_CAPS:
err = ops->get_ptp_cap(vf, (const void *)msg);
break;
case VIRTCHNL_OP_1588_PTP_GET_TIME:
err = ops->get_phc_time(vf);
break;
case VIRTCHNL_OP_UNKNOWN:
default:
dev_err(dev, "Unsupported opcode %d from VF %d\n", v_opcode,
vf_id);
err = ice_vc_send_msg_to_vf(vf, v_opcode,
VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
NULL, 0);
break;
}
if (err) {
dev_info(dev, "PF failed to honor VF %d, opcode %d, error %d\n",
vf_id, v_opcode, err);
}
finish:
mutex_unlock(&vf->cfg_lock);
ice_put_vf(vf);
}