Linux Audio

Check our new training course

Embedded Linux training

Mar 31-Apr 8, 2025
Register
Loading...
v6.9.4
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (C) 2022, Intel Corporation. */
   3
   4#include "ice_virtchnl.h"
   5#include "ice_vf_lib_private.h"
   6#include "ice.h"
   7#include "ice_base.h"
   8#include "ice_lib.h"
   9#include "ice_fltr.h"
  10#include "ice_virtchnl_allowlist.h"
  11#include "ice_vf_vsi_vlan_ops.h"
  12#include "ice_vlan.h"
  13#include "ice_flex_pipe.h"
  14#include "ice_dcb_lib.h"
  15
  16#define FIELD_SELECTOR(proto_hdr_field) \
  17		BIT((proto_hdr_field) & PROTO_HDR_FIELD_MASK)
  18
  19struct ice_vc_hdr_match_type {
  20	u32 vc_hdr;	/* virtchnl headers (VIRTCHNL_PROTO_HDR_XXX) */
  21	u32 ice_hdr;	/* ice headers (ICE_FLOW_SEG_HDR_XXX) */
  22};
  23
  24static const struct ice_vc_hdr_match_type ice_vc_hdr_list[] = {
  25	{VIRTCHNL_PROTO_HDR_NONE,	ICE_FLOW_SEG_HDR_NONE},
  26	{VIRTCHNL_PROTO_HDR_ETH,	ICE_FLOW_SEG_HDR_ETH},
  27	{VIRTCHNL_PROTO_HDR_S_VLAN,	ICE_FLOW_SEG_HDR_VLAN},
  28	{VIRTCHNL_PROTO_HDR_C_VLAN,	ICE_FLOW_SEG_HDR_VLAN},
  29	{VIRTCHNL_PROTO_HDR_IPV4,	ICE_FLOW_SEG_HDR_IPV4 |
  30					ICE_FLOW_SEG_HDR_IPV_OTHER},
  31	{VIRTCHNL_PROTO_HDR_IPV6,	ICE_FLOW_SEG_HDR_IPV6 |
  32					ICE_FLOW_SEG_HDR_IPV_OTHER},
  33	{VIRTCHNL_PROTO_HDR_TCP,	ICE_FLOW_SEG_HDR_TCP},
  34	{VIRTCHNL_PROTO_HDR_UDP,	ICE_FLOW_SEG_HDR_UDP},
  35	{VIRTCHNL_PROTO_HDR_SCTP,	ICE_FLOW_SEG_HDR_SCTP},
  36	{VIRTCHNL_PROTO_HDR_PPPOE,	ICE_FLOW_SEG_HDR_PPPOE},
  37	{VIRTCHNL_PROTO_HDR_GTPU_IP,	ICE_FLOW_SEG_HDR_GTPU_IP},
  38	{VIRTCHNL_PROTO_HDR_GTPU_EH,	ICE_FLOW_SEG_HDR_GTPU_EH},
  39	{VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_DWN,
  40					ICE_FLOW_SEG_HDR_GTPU_DWN},
  41	{VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_UP,
  42					ICE_FLOW_SEG_HDR_GTPU_UP},
  43	{VIRTCHNL_PROTO_HDR_L2TPV3,	ICE_FLOW_SEG_HDR_L2TPV3},
  44	{VIRTCHNL_PROTO_HDR_ESP,	ICE_FLOW_SEG_HDR_ESP},
  45	{VIRTCHNL_PROTO_HDR_AH,		ICE_FLOW_SEG_HDR_AH},
  46	{VIRTCHNL_PROTO_HDR_PFCP,	ICE_FLOW_SEG_HDR_PFCP_SESSION},
  47};
  48
  49struct ice_vc_hash_field_match_type {
  50	u32 vc_hdr;		/* virtchnl headers
  51				 * (VIRTCHNL_PROTO_HDR_XXX)
  52				 */
  53	u32 vc_hash_field;	/* virtchnl hash fields selector
  54				 * FIELD_SELECTOR((VIRTCHNL_PROTO_HDR_ETH_XXX))
  55				 */
  56	u64 ice_hash_field;	/* ice hash fields
  57				 * (BIT_ULL(ICE_FLOW_FIELD_IDX_XXX))
  58				 */
  59};
  60
  61static const struct
  62ice_vc_hash_field_match_type ice_vc_hash_field_list[] = {
  63	{VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC),
  64		BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_SA)},
  65	{VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
  66		BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_DA)},
  67	{VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC) |
  68		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
  69		ICE_FLOW_HASH_ETH},
  70	{VIRTCHNL_PROTO_HDR_ETH,
  71		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_ETHERTYPE),
  72		BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_TYPE)},
  73	{VIRTCHNL_PROTO_HDR_S_VLAN,
  74		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_S_VLAN_ID),
  75		BIT_ULL(ICE_FLOW_FIELD_IDX_S_VLAN)},
  76	{VIRTCHNL_PROTO_HDR_C_VLAN,
  77		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_C_VLAN_ID),
  78		BIT_ULL(ICE_FLOW_FIELD_IDX_C_VLAN)},
  79	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC),
  80		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA)},
  81	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
  82		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA)},
  83	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
  84		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
  85		ICE_FLOW_HASH_IPV4},
  86	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
  87		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
  88		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA) |
  89		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
  90	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
  91		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
  92		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA) |
  93		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
  94	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
  95		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
  96		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
  97		ICE_FLOW_HASH_IPV4 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
  98	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
  99		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
 100	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC),
 101		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA)},
 102	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
 103		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA)},
 104	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
 105		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
 106		ICE_FLOW_HASH_IPV6},
 107	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
 108		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
 109		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA) |
 110		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
 111	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
 112		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
 113		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA) |
 114		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
 115	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
 116		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
 117		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
 118		ICE_FLOW_HASH_IPV6 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
 119	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
 120		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
 121	{VIRTCHNL_PROTO_HDR_TCP,
 122		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT),
 123		BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_SRC_PORT)},
 124	{VIRTCHNL_PROTO_HDR_TCP,
 125		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
 126		BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_DST_PORT)},
 127	{VIRTCHNL_PROTO_HDR_TCP,
 128		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT) |
 129		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
 130		ICE_FLOW_HASH_TCP_PORT},
 131	{VIRTCHNL_PROTO_HDR_UDP,
 132		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT),
 133		BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_SRC_PORT)},
 134	{VIRTCHNL_PROTO_HDR_UDP,
 135		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
 136		BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_DST_PORT)},
 137	{VIRTCHNL_PROTO_HDR_UDP,
 138		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT) |
 139		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
 140		ICE_FLOW_HASH_UDP_PORT},
 141	{VIRTCHNL_PROTO_HDR_SCTP,
 142		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT),
 143		BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT)},
 144	{VIRTCHNL_PROTO_HDR_SCTP,
 145		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
 146		BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_DST_PORT)},
 147	{VIRTCHNL_PROTO_HDR_SCTP,
 148		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT) |
 149		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
 150		ICE_FLOW_HASH_SCTP_PORT},
 151	{VIRTCHNL_PROTO_HDR_PPPOE,
 152		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PPPOE_SESS_ID),
 153		BIT_ULL(ICE_FLOW_FIELD_IDX_PPPOE_SESS_ID)},
 154	{VIRTCHNL_PROTO_HDR_GTPU_IP,
 155		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_GTPU_IP_TEID),
 156		BIT_ULL(ICE_FLOW_FIELD_IDX_GTPU_IP_TEID)},
 157	{VIRTCHNL_PROTO_HDR_L2TPV3,
 158		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_L2TPV3_SESS_ID),
 159		BIT_ULL(ICE_FLOW_FIELD_IDX_L2TPV3_SESS_ID)},
 160	{VIRTCHNL_PROTO_HDR_ESP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ESP_SPI),
 161		BIT_ULL(ICE_FLOW_FIELD_IDX_ESP_SPI)},
 162	{VIRTCHNL_PROTO_HDR_AH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_AH_SPI),
 163		BIT_ULL(ICE_FLOW_FIELD_IDX_AH_SPI)},
 164	{VIRTCHNL_PROTO_HDR_PFCP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PFCP_SEID),
 165		BIT_ULL(ICE_FLOW_FIELD_IDX_PFCP_SEID)},
 166};
 167
 168/**
 169 * ice_vc_vf_broadcast - Broadcast a message to all VFs on PF
 170 * @pf: pointer to the PF structure
 171 * @v_opcode: operation code
 172 * @v_retval: return value
 173 * @msg: pointer to the msg buffer
 174 * @msglen: msg length
 175 */
 176static void
 177ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
 178		    enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
 179{
 180	struct ice_hw *hw = &pf->hw;
 181	struct ice_vf *vf;
 182	unsigned int bkt;
 183
 184	mutex_lock(&pf->vfs.table_lock);
 185	ice_for_each_vf(pf, bkt, vf) {
 186		/* Not all vfs are enabled so skip the ones that are not */
 187		if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
 188		    !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
 189			continue;
 190
 191		/* Ignore return value on purpose - a given VF may fail, but
 192		 * we need to keep going and send to all of them
 193		 */
 194		ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
 195				      msglen, NULL);
 196	}
 197	mutex_unlock(&pf->vfs.table_lock);
 198}
 199
 200/**
 201 * ice_set_pfe_link - Set the link speed/status of the virtchnl_pf_event
 202 * @vf: pointer to the VF structure
 203 * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
 204 * @ice_link_speed: link speed specified by ICE_AQ_LINK_SPEED_*
 205 * @link_up: whether or not to set the link up/down
 206 */
 207static void
 208ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
 209		 int ice_link_speed, bool link_up)
 210{
 211	if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
 212		pfe->event_data.link_event_adv.link_status = link_up;
 213		/* Speed in Mbps */
 214		pfe->event_data.link_event_adv.link_speed =
 215			ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
 216	} else {
 217		pfe->event_data.link_event.link_status = link_up;
 218		/* Legacy method for virtchnl link speeds */
 219		pfe->event_data.link_event.link_speed =
 220			(enum virtchnl_link_speed)
 221			ice_conv_link_speed_to_virtchnl(false, ice_link_speed);
 222	}
 223}
 224
 225/**
 226 * ice_vc_notify_vf_link_state - Inform a VF of link status
 227 * @vf: pointer to the VF structure
 228 *
 229 * send a link status message to a single VF
 230 */
 231void ice_vc_notify_vf_link_state(struct ice_vf *vf)
 232{
 233	struct virtchnl_pf_event pfe = { 0 };
 234	struct ice_hw *hw = &vf->pf->hw;
 235
 236	pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
 237	pfe.severity = PF_EVENT_SEVERITY_INFO;
 238
 239	if (ice_is_vf_link_up(vf))
 240		ice_set_pfe_link(vf, &pfe,
 241				 hw->port_info->phy.link_info.link_speed, true);
 242	else
 243		ice_set_pfe_link(vf, &pfe, ICE_AQ_LINK_SPEED_UNKNOWN, false);
 244
 245	ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
 246			      VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,
 247			      sizeof(pfe), NULL);
 248}
 249
 250/**
 251 * ice_vc_notify_link_state - Inform all VFs on a PF of link status
 252 * @pf: pointer to the PF structure
 253 */
 254void ice_vc_notify_link_state(struct ice_pf *pf)
 255{
 256	struct ice_vf *vf;
 257	unsigned int bkt;
 258
 259	mutex_lock(&pf->vfs.table_lock);
 260	ice_for_each_vf(pf, bkt, vf)
 261		ice_vc_notify_vf_link_state(vf);
 262	mutex_unlock(&pf->vfs.table_lock);
 263}
 264
 265/**
 266 * ice_vc_notify_reset - Send pending reset message to all VFs
 267 * @pf: pointer to the PF structure
 268 *
 269 * indicate a pending reset to all VFs on a given PF
 270 */
 271void ice_vc_notify_reset(struct ice_pf *pf)
 272{
 273	struct virtchnl_pf_event pfe;
 274
 275	if (!ice_has_vfs(pf))
 276		return;
 277
 278	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
 279	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
 280	ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
 281			    (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
 282}
 283
 284/**
 285 * ice_vc_send_msg_to_vf - Send message to VF
 286 * @vf: pointer to the VF info
 287 * @v_opcode: virtual channel opcode
 288 * @v_retval: virtual channel return value
 289 * @msg: pointer to the msg buffer
 290 * @msglen: msg length
 291 *
 292 * send msg to VF
 293 */
 294int
 295ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
 296		      enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
 297{
 298	struct device *dev;
 299	struct ice_pf *pf;
 300	int aq_ret;
 301
 302	pf = vf->pf;
 303	dev = ice_pf_to_dev(pf);
 304
 305	aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
 306				       msg, msglen, NULL);
 307	if (aq_ret && pf->hw.mailboxq.sq_last_status != ICE_AQ_RC_ENOSYS) {
 308		dev_info(dev, "Unable to send the message to VF %d ret %d aq_err %s\n",
 309			 vf->vf_id, aq_ret,
 310			 ice_aq_str(pf->hw.mailboxq.sq_last_status));
 311		return -EIO;
 312	}
 313
 314	return 0;
 315}
 316
 317/**
 318 * ice_vc_get_ver_msg
 319 * @vf: pointer to the VF info
 320 * @msg: pointer to the msg buffer
 321 *
 322 * called from the VF to request the API version used by the PF
 323 */
 324static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
 325{
 326	struct virtchnl_version_info info = {
 327		VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
 328	};
 329
 330	vf->vf_ver = *(struct virtchnl_version_info *)msg;
 331	/* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
 332	if (VF_IS_V10(&vf->vf_ver))
 333		info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
 334
 335	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
 336				     VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
 337				     sizeof(struct virtchnl_version_info));
 338}
 339
 340/**
 341 * ice_vc_get_max_frame_size - get max frame size allowed for VF
 342 * @vf: VF used to determine max frame size
 343 *
 344 * Max frame size is determined based on the current port's max frame size and
 345 * whether a port VLAN is configured on this VF. The VF is not aware whether
 346 * it's in a port VLAN so the PF needs to account for this in max frame size
 347 * checks and sending the max frame size to the VF.
 348 */
 349static u16 ice_vc_get_max_frame_size(struct ice_vf *vf)
 350{
 351	struct ice_port_info *pi = ice_vf_get_port_info(vf);
 352	u16 max_frame_size;
 353
 354	max_frame_size = pi->phy.link_info.max_frame_size;
 355
 356	if (ice_vf_is_port_vlan_ena(vf))
 357		max_frame_size -= VLAN_HLEN;
 358
 359	return max_frame_size;
 360}
 361
 362/**
 363 * ice_vc_get_vlan_caps
 364 * @hw: pointer to the hw
 365 * @vf: pointer to the VF info
 366 * @vsi: pointer to the VSI
 367 * @driver_caps: current driver caps
 368 *
 369 * Return 0 if there is no VLAN caps supported, or VLAN caps value
 370 */
 371static u32
 372ice_vc_get_vlan_caps(struct ice_hw *hw, struct ice_vf *vf, struct ice_vsi *vsi,
 373		     u32 driver_caps)
 374{
 375	if (ice_is_eswitch_mode_switchdev(vf->pf))
 376		/* In switchdev setting VLAN from VF isn't supported */
 377		return 0;
 378
 379	if (driver_caps & VIRTCHNL_VF_OFFLOAD_VLAN_V2) {
 380		/* VLAN offloads based on current device configuration */
 381		return VIRTCHNL_VF_OFFLOAD_VLAN_V2;
 382	} else if (driver_caps & VIRTCHNL_VF_OFFLOAD_VLAN) {
 383		/* allow VF to negotiate VIRTCHNL_VF_OFFLOAD explicitly for
 384		 * these two conditions, which amounts to guest VLAN filtering
 385		 * and offloads being based on the inner VLAN or the
 386		 * inner/single VLAN respectively and don't allow VF to
 387		 * negotiate VIRTCHNL_VF_OFFLOAD in any other cases
 388		 */
 389		if (ice_is_dvm_ena(hw) && ice_vf_is_port_vlan_ena(vf)) {
 390			return VIRTCHNL_VF_OFFLOAD_VLAN;
 391		} else if (!ice_is_dvm_ena(hw) &&
 392			   !ice_vf_is_port_vlan_ena(vf)) {
 393			/* configure backward compatible support for VFs that
 394			 * only support VIRTCHNL_VF_OFFLOAD_VLAN, the PF is
 395			 * configured in SVM, and no port VLAN is configured
 396			 */
 397			ice_vf_vsi_cfg_svm_legacy_vlan_mode(vsi);
 398			return VIRTCHNL_VF_OFFLOAD_VLAN;
 399		} else if (ice_is_dvm_ena(hw)) {
 400			/* configure software offloaded VLAN support when DVM
 401			 * is enabled, but no port VLAN is enabled
 402			 */
 403			ice_vf_vsi_cfg_dvm_legacy_vlan_mode(vsi);
 404		}
 405	}
 406
 407	return 0;
 408}
 409
 410/**
 411 * ice_vc_get_vf_res_msg
 412 * @vf: pointer to the VF info
 413 * @msg: pointer to the msg buffer
 414 *
 415 * called from the VF to request its resources
 416 */
 417static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
 418{
 419	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
 420	struct virtchnl_vf_resource *vfres = NULL;
 421	struct ice_hw *hw = &vf->pf->hw;
 422	struct ice_vsi *vsi;
 423	int len = 0;
 424	int ret;
 425
 426	if (ice_check_vf_init(vf)) {
 427		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 428		goto err;
 429	}
 430
 431	len = virtchnl_struct_size(vfres, vsi_res, 0);
 432
 433	vfres = kzalloc(len, GFP_KERNEL);
 434	if (!vfres) {
 435		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
 436		len = 0;
 437		goto err;
 438	}
 439	if (VF_IS_V11(&vf->vf_ver))
 440		vf->driver_caps = *(u32 *)msg;
 441	else
 442		vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
 
 443				  VIRTCHNL_VF_OFFLOAD_VLAN;
 444
 445	vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
 446	vsi = ice_get_vf_vsi(vf);
 447	if (!vsi) {
 448		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 449		goto err;
 450	}
 451
 452	vfres->vf_cap_flags |= ice_vc_get_vlan_caps(hw, vf, vsi,
 453						    vf->driver_caps);
 454
 455	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF)
 456		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
 
 
 
 
 
 
 457
 458	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC)
 459		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC;
 460
 461	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_FDIR_PF)
 462		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_FDIR_PF;
 463
 464	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
 465		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
 466
 467	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
 468		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
 469
 470	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
 471		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
 472
 473	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
 474		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
 475
 476	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
 477		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
 478
 479	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
 480		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
 481
 482	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_CRC)
 483		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_CRC;
 484
 485	if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
 486		vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
 487
 488	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF)
 489		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF;
 490
 491	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_USO)
 492		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_USO;
 493
 494	vfres->num_vsis = 1;
 495	/* Tx and Rx queue are equal for VF */
 496	vfres->num_queue_pairs = vsi->num_txq;
 497	vfres->max_vectors = vf->num_msix;
 498	vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
 499	vfres->rss_lut_size = ICE_LUT_VSI_SIZE;
 500	vfres->max_mtu = ice_vc_get_max_frame_size(vf);
 501
 502	vfres->vsi_res[0].vsi_id = ICE_VF_VSI_ID;
 503	vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
 504	vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
 505	ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
 506			vf->hw_lan_addr);
 507
 508	/* match guest capabilities */
 509	vf->driver_caps = vfres->vf_cap_flags;
 510
 511	ice_vc_set_caps_allowlist(vf);
 512	ice_vc_set_working_allowlist(vf);
 513
 514	set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
 515
 516err:
 517	/* send the response back to the VF */
 518	ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
 519				    (u8 *)vfres, len);
 520
 521	kfree(vfres);
 522	return ret;
 523}
 524
 525/**
 526 * ice_vc_reset_vf_msg
 527 * @vf: pointer to the VF info
 528 *
 529 * called from the VF to reset itself,
 530 * unlike other virtchnl messages, PF driver
 531 * doesn't send the response back to the VF
 532 */
 533static void ice_vc_reset_vf_msg(struct ice_vf *vf)
 534{
 535	if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
 536		ice_reset_vf(vf, 0);
 537}
 538
 539/**
 540 * ice_vc_isvalid_vsi_id
 541 * @vf: pointer to the VF info
 542 * @vsi_id: VF relative VSI ID
 543 *
 544 * check for the valid VSI ID
 545 */
 546bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
 547{
 548	return vsi_id == ICE_VF_VSI_ID;
 
 
 
 
 
 549}
 550
 551/**
 552 * ice_vc_isvalid_q_id
 553 * @vsi: VSI to check queue ID against
 
 554 * @qid: VSI relative queue ID
 555 *
 556 * check for the valid queue ID
 557 */
 558static bool ice_vc_isvalid_q_id(struct ice_vsi *vsi, u8 qid)
 559{
 
 560	/* allocated Tx and Rx queues should be always equal for VF VSI */
 561	return qid < vsi->alloc_txq;
 562}
 563
 564/**
 565 * ice_vc_isvalid_ring_len
 566 * @ring_len: length of ring
 567 *
 568 * check for the valid ring count, should be multiple of ICE_REQ_DESC_MULTIPLE
 569 * or zero
 570 */
 571static bool ice_vc_isvalid_ring_len(u16 ring_len)
 572{
 573	return ring_len == 0 ||
 574	       (ring_len >= ICE_MIN_NUM_DESC &&
 575		ring_len <= ICE_MAX_NUM_DESC &&
 576		!(ring_len % ICE_REQ_DESC_MULTIPLE));
 577}
 578
 579/**
 580 * ice_vc_validate_pattern
 581 * @vf: pointer to the VF info
 582 * @proto: virtchnl protocol headers
 583 *
 584 * validate the pattern is supported or not.
 585 *
 586 * Return: true on success, false on error.
 587 */
 588bool
 589ice_vc_validate_pattern(struct ice_vf *vf, struct virtchnl_proto_hdrs *proto)
 590{
 591	bool is_ipv4 = false;
 592	bool is_ipv6 = false;
 593	bool is_udp = false;
 594	u16 ptype = -1;
 595	int i = 0;
 596
 597	while (i < proto->count &&
 598	       proto->proto_hdr[i].type != VIRTCHNL_PROTO_HDR_NONE) {
 599		switch (proto->proto_hdr[i].type) {
 600		case VIRTCHNL_PROTO_HDR_ETH:
 601			ptype = ICE_PTYPE_MAC_PAY;
 602			break;
 603		case VIRTCHNL_PROTO_HDR_IPV4:
 604			ptype = ICE_PTYPE_IPV4_PAY;
 605			is_ipv4 = true;
 606			break;
 607		case VIRTCHNL_PROTO_HDR_IPV6:
 608			ptype = ICE_PTYPE_IPV6_PAY;
 609			is_ipv6 = true;
 610			break;
 611		case VIRTCHNL_PROTO_HDR_UDP:
 612			if (is_ipv4)
 613				ptype = ICE_PTYPE_IPV4_UDP_PAY;
 614			else if (is_ipv6)
 615				ptype = ICE_PTYPE_IPV6_UDP_PAY;
 616			is_udp = true;
 617			break;
 618		case VIRTCHNL_PROTO_HDR_TCP:
 619			if (is_ipv4)
 620				ptype = ICE_PTYPE_IPV4_TCP_PAY;
 621			else if (is_ipv6)
 622				ptype = ICE_PTYPE_IPV6_TCP_PAY;
 623			break;
 624		case VIRTCHNL_PROTO_HDR_SCTP:
 625			if (is_ipv4)
 626				ptype = ICE_PTYPE_IPV4_SCTP_PAY;
 627			else if (is_ipv6)
 628				ptype = ICE_PTYPE_IPV6_SCTP_PAY;
 629			break;
 630		case VIRTCHNL_PROTO_HDR_GTPU_IP:
 631		case VIRTCHNL_PROTO_HDR_GTPU_EH:
 632			if (is_ipv4)
 633				ptype = ICE_MAC_IPV4_GTPU;
 634			else if (is_ipv6)
 635				ptype = ICE_MAC_IPV6_GTPU;
 636			goto out;
 637		case VIRTCHNL_PROTO_HDR_L2TPV3:
 638			if (is_ipv4)
 639				ptype = ICE_MAC_IPV4_L2TPV3;
 640			else if (is_ipv6)
 641				ptype = ICE_MAC_IPV6_L2TPV3;
 642			goto out;
 643		case VIRTCHNL_PROTO_HDR_ESP:
 644			if (is_ipv4)
 645				ptype = is_udp ? ICE_MAC_IPV4_NAT_T_ESP :
 646						ICE_MAC_IPV4_ESP;
 647			else if (is_ipv6)
 648				ptype = is_udp ? ICE_MAC_IPV6_NAT_T_ESP :
 649						ICE_MAC_IPV6_ESP;
 650			goto out;
 651		case VIRTCHNL_PROTO_HDR_AH:
 652			if (is_ipv4)
 653				ptype = ICE_MAC_IPV4_AH;
 654			else if (is_ipv6)
 655				ptype = ICE_MAC_IPV6_AH;
 656			goto out;
 657		case VIRTCHNL_PROTO_HDR_PFCP:
 658			if (is_ipv4)
 659				ptype = ICE_MAC_IPV4_PFCP_SESSION;
 660			else if (is_ipv6)
 661				ptype = ICE_MAC_IPV6_PFCP_SESSION;
 662			goto out;
 663		default:
 664			break;
 665		}
 666		i++;
 667	}
 668
 669out:
 670	return ice_hw_ptype_ena(&vf->pf->hw, ptype);
 671}
 672
 673/**
 674 * ice_vc_parse_rss_cfg - parses hash fields and headers from
 675 * a specific virtchnl RSS cfg
 676 * @hw: pointer to the hardware
 677 * @rss_cfg: pointer to the virtchnl RSS cfg
 678 * @hash_cfg: pointer to the HW hash configuration
 
 
 679 *
 680 * Return true if all the protocol header and hash fields in the RSS cfg could
 681 * be parsed, else return false
 682 *
 683 * This function parses the virtchnl RSS cfg to be the intended
 684 * hash fields and the intended header for RSS configuration
 685 */
 686static bool ice_vc_parse_rss_cfg(struct ice_hw *hw,
 687				 struct virtchnl_rss_cfg *rss_cfg,
 688				 struct ice_rss_hash_cfg *hash_cfg)
 689{
 690	const struct ice_vc_hash_field_match_type *hf_list;
 691	const struct ice_vc_hdr_match_type *hdr_list;
 692	int i, hf_list_len, hdr_list_len;
 693	u32 *addl_hdrs = &hash_cfg->addl_hdrs;
 694	u64 *hash_flds = &hash_cfg->hash_flds;
 695
 696	/* set outer layer RSS as default */
 697	hash_cfg->hdr_type = ICE_RSS_OUTER_HEADERS;
 698
 699	if (rss_cfg->rss_algorithm == VIRTCHNL_RSS_ALG_TOEPLITZ_SYMMETRIC)
 700		hash_cfg->symm = true;
 701	else
 702		hash_cfg->symm = false;
 703
 704	hf_list = ice_vc_hash_field_list;
 705	hf_list_len = ARRAY_SIZE(ice_vc_hash_field_list);
 706	hdr_list = ice_vc_hdr_list;
 707	hdr_list_len = ARRAY_SIZE(ice_vc_hdr_list);
 708
 709	for (i = 0; i < rss_cfg->proto_hdrs.count; i++) {
 710		struct virtchnl_proto_hdr *proto_hdr =
 711					&rss_cfg->proto_hdrs.proto_hdr[i];
 712		bool hdr_found = false;
 713		int j;
 714
 715		/* Find matched ice headers according to virtchnl headers. */
 716		for (j = 0; j < hdr_list_len; j++) {
 717			struct ice_vc_hdr_match_type hdr_map = hdr_list[j];
 718
 719			if (proto_hdr->type == hdr_map.vc_hdr) {
 720				*addl_hdrs |= hdr_map.ice_hdr;
 721				hdr_found = true;
 722			}
 723		}
 724
 725		if (!hdr_found)
 726			return false;
 727
 728		/* Find matched ice hash fields according to
 729		 * virtchnl hash fields.
 730		 */
 731		for (j = 0; j < hf_list_len; j++) {
 732			struct ice_vc_hash_field_match_type hf_map = hf_list[j];
 733
 734			if (proto_hdr->type == hf_map.vc_hdr &&
 735			    proto_hdr->field_selector == hf_map.vc_hash_field) {
 736				*hash_flds |= hf_map.ice_hash_field;
 737				break;
 738			}
 739		}
 740	}
 741
 742	return true;
 743}
 744
 745/**
 746 * ice_vf_adv_rss_offload_ena - determine if capabilities support advanced
 747 * RSS offloads
 748 * @caps: VF driver negotiated capabilities
 749 *
 750 * Return true if VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF capability is set,
 751 * else return false
 752 */
 753static bool ice_vf_adv_rss_offload_ena(u32 caps)
 754{
 755	return !!(caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF);
 756}
 757
 758/**
 759 * ice_vc_handle_rss_cfg
 760 * @vf: pointer to the VF info
 761 * @msg: pointer to the message buffer
 762 * @add: add a RSS config if true, otherwise delete a RSS config
 763 *
 764 * This function adds/deletes a RSS config
 765 */
 766static int ice_vc_handle_rss_cfg(struct ice_vf *vf, u8 *msg, bool add)
 767{
 768	u32 v_opcode = add ? VIRTCHNL_OP_ADD_RSS_CFG : VIRTCHNL_OP_DEL_RSS_CFG;
 769	struct virtchnl_rss_cfg *rss_cfg = (struct virtchnl_rss_cfg *)msg;
 770	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
 771	struct device *dev = ice_pf_to_dev(vf->pf);
 772	struct ice_hw *hw = &vf->pf->hw;
 773	struct ice_vsi *vsi;
 774
 775	if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
 776		dev_dbg(dev, "VF %d attempting to configure RSS, but RSS is not supported by the PF\n",
 777			vf->vf_id);
 778		v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
 779		goto error_param;
 780	}
 781
 782	if (!ice_vf_adv_rss_offload_ena(vf->driver_caps)) {
 783		dev_dbg(dev, "VF %d attempting to configure RSS, but Advanced RSS offload is not supported\n",
 784			vf->vf_id);
 785		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 786		goto error_param;
 787	}
 788
 789	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
 790		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 791		goto error_param;
 792	}
 793
 794	if (rss_cfg->proto_hdrs.count > VIRTCHNL_MAX_NUM_PROTO_HDRS ||
 795	    rss_cfg->rss_algorithm < VIRTCHNL_RSS_ALG_TOEPLITZ_ASYMMETRIC ||
 796	    rss_cfg->rss_algorithm > VIRTCHNL_RSS_ALG_XOR_SYMMETRIC) {
 797		dev_dbg(dev, "VF %d attempting to configure RSS, but RSS configuration is not valid\n",
 798			vf->vf_id);
 799		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 800		goto error_param;
 801	}
 802
 803	vsi = ice_get_vf_vsi(vf);
 804	if (!vsi) {
 805		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 806		goto error_param;
 807	}
 808
 809	if (!ice_vc_validate_pattern(vf, &rss_cfg->proto_hdrs)) {
 810		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 811		goto error_param;
 812	}
 813
 814	if (rss_cfg->rss_algorithm == VIRTCHNL_RSS_ALG_R_ASYMMETRIC) {
 815		struct ice_vsi_ctx *ctx;
 816		u8 lut_type, hash_type;
 817		int status;
 818
 819		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
 820		hash_type = add ? ICE_AQ_VSI_Q_OPT_RSS_HASH_XOR :
 821				ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ;
 822
 823		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
 824		if (!ctx) {
 825			v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
 826			goto error_param;
 827		}
 828
 829		ctx->info.q_opt_rss =
 830			FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_LUT_M, lut_type) |
 831			FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_HASH_M, hash_type);
 
 
 832
 833		/* Preserve existing queueing option setting */
 834		ctx->info.q_opt_rss |= (vsi->info.q_opt_rss &
 835					  ICE_AQ_VSI_Q_OPT_RSS_GBL_LUT_M);
 836		ctx->info.q_opt_tc = vsi->info.q_opt_tc;
 837		ctx->info.q_opt_flags = vsi->info.q_opt_rss;
 838
 839		ctx->info.valid_sections =
 840				cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
 841
 842		status = ice_update_vsi(hw, vsi->idx, ctx, NULL);
 843		if (status) {
 844			dev_err(dev, "update VSI for RSS failed, err %d aq_err %s\n",
 845				status, ice_aq_str(hw->adminq.sq_last_status));
 846			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 847		} else {
 848			vsi->info.q_opt_rss = ctx->info.q_opt_rss;
 849		}
 850
 851		kfree(ctx);
 852	} else {
 853		struct ice_rss_hash_cfg cfg;
 
 854
 855		/* Only check for none raw pattern case */
 856		if (!ice_vc_validate_pattern(vf, &rss_cfg->proto_hdrs)) {
 857			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 858			goto error_param;
 859		}
 860		cfg.addl_hdrs = ICE_FLOW_SEG_HDR_NONE;
 861		cfg.hash_flds = ICE_HASH_INVALID;
 862		cfg.hdr_type = ICE_RSS_ANY_HEADERS;
 863
 864		if (!ice_vc_parse_rss_cfg(hw, rss_cfg, &cfg)) {
 865			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 866			goto error_param;
 867		}
 868
 869		if (add) {
 870			if (ice_add_rss_cfg(hw, vsi, &cfg)) {
 
 871				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 872				dev_err(dev, "ice_add_rss_cfg failed for vsi = %d, v_ret = %d\n",
 873					vsi->vsi_num, v_ret);
 874			}
 875		} else {
 876			int status;
 877
 878			status = ice_rem_rss_cfg(hw, vsi->idx, &cfg);
 
 879			/* We just ignore -ENOENT, because if two configurations
 880			 * share the same profile remove one of them actually
 881			 * removes both, since the profile is deleted.
 882			 */
 883			if (status && status != -ENOENT) {
 884				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 885				dev_err(dev, "ice_rem_rss_cfg failed for VF ID:%d, error:%d\n",
 886					vf->vf_id, status);
 887			}
 888		}
 889	}
 890
 891error_param:
 892	return ice_vc_send_msg_to_vf(vf, v_opcode, v_ret, NULL, 0);
 893}
 894
 895/**
 896 * ice_vc_config_rss_key
 897 * @vf: pointer to the VF info
 898 * @msg: pointer to the msg buffer
 899 *
 900 * Configure the VF's RSS key
 901 */
 902static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
 903{
 904	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
 905	struct virtchnl_rss_key *vrk =
 906		(struct virtchnl_rss_key *)msg;
 907	struct ice_vsi *vsi;
 908
 909	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
 910		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 911		goto error_param;
 912	}
 913
 914	if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
 915		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 916		goto error_param;
 917	}
 918
 919	if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
 920		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 921		goto error_param;
 922	}
 923
 924	if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
 925		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 926		goto error_param;
 927	}
 928
 929	vsi = ice_get_vf_vsi(vf);
 930	if (!vsi) {
 931		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 932		goto error_param;
 933	}
 934
 935	if (ice_set_rss_key(vsi, vrk->key))
 936		v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
 937error_param:
 938	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
 939				     NULL, 0);
 940}
 941
 942/**
 943 * ice_vc_config_rss_lut
 944 * @vf: pointer to the VF info
 945 * @msg: pointer to the msg buffer
 946 *
 947 * Configure the VF's RSS LUT
 948 */
 949static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
 950{
 951	struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
 952	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
 953	struct ice_vsi *vsi;
 954
 955	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
 956		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 957		goto error_param;
 958	}
 959
 960	if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
 961		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 962		goto error_param;
 963	}
 964
 965	if (vrl->lut_entries != ICE_LUT_VSI_SIZE) {
 966		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 967		goto error_param;
 968	}
 969
 970	if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
 971		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 972		goto error_param;
 973	}
 974
 975	vsi = ice_get_vf_vsi(vf);
 976	if (!vsi) {
 977		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 978		goto error_param;
 979	}
 980
 981	if (ice_set_rss_lut(vsi, vrl->lut, ICE_LUT_VSI_SIZE))
 982		v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
 983error_param:
 984	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
 985				     NULL, 0);
 986}
 987
 988/**
 989 * ice_vc_config_rss_hfunc
 990 * @vf: pointer to the VF info
 991 * @msg: pointer to the msg buffer
 992 *
 993 * Configure the VF's RSS Hash function
 994 */
 995static int ice_vc_config_rss_hfunc(struct ice_vf *vf, u8 *msg)
 996{
 997	struct virtchnl_rss_hfunc *vrh = (struct virtchnl_rss_hfunc *)msg;
 998	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
 999	u8 hfunc = ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ;
1000	struct ice_vsi *vsi;
1001
1002	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1003		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1004		goto error_param;
1005	}
1006
1007	if (!ice_vc_isvalid_vsi_id(vf, vrh->vsi_id)) {
1008		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1009		goto error_param;
1010	}
1011
1012	if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
1013		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1014		goto error_param;
1015	}
1016
1017	vsi = ice_get_vf_vsi(vf);
1018	if (!vsi) {
1019		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1020		goto error_param;
1021	}
1022
1023	if (vrh->rss_algorithm == VIRTCHNL_RSS_ALG_TOEPLITZ_SYMMETRIC)
1024		hfunc = ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ;
1025
1026	if (ice_set_rss_hfunc(vsi, hfunc))
1027		v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1028error_param:
1029	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_HFUNC, v_ret,
1030				     NULL, 0);
1031}
1032
1033/**
1034 * ice_vc_cfg_promiscuous_mode_msg
1035 * @vf: pointer to the VF info
1036 * @msg: pointer to the msg buffer
1037 *
1038 * called from the VF to configure VF VSIs promiscuous mode
1039 */
1040static int ice_vc_cfg_promiscuous_mode_msg(struct ice_vf *vf, u8 *msg)
1041{
1042	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1043	bool rm_promisc, alluni = false, allmulti = false;
1044	struct virtchnl_promisc_info *info =
1045	    (struct virtchnl_promisc_info *)msg;
1046	struct ice_vsi_vlan_ops *vlan_ops;
1047	int mcast_err = 0, ucast_err = 0;
1048	struct ice_pf *pf = vf->pf;
1049	struct ice_vsi *vsi;
1050	u8 mcast_m, ucast_m;
1051	struct device *dev;
1052	int ret = 0;
1053
1054	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1055		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1056		goto error_param;
1057	}
1058
1059	if (!ice_vc_isvalid_vsi_id(vf, info->vsi_id)) {
1060		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1061		goto error_param;
1062	}
1063
1064	vsi = ice_get_vf_vsi(vf);
1065	if (!vsi) {
1066		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1067		goto error_param;
1068	}
1069
1070	dev = ice_pf_to_dev(pf);
1071	if (!ice_is_vf_trusted(vf)) {
1072		dev_err(dev, "Unprivileged VF %d is attempting to configure promiscuous mode\n",
1073			vf->vf_id);
1074		/* Leave v_ret alone, lie to the VF on purpose. */
1075		goto error_param;
1076	}
1077
1078	if (info->flags & FLAG_VF_UNICAST_PROMISC)
1079		alluni = true;
1080
1081	if (info->flags & FLAG_VF_MULTICAST_PROMISC)
1082		allmulti = true;
1083
1084	rm_promisc = !allmulti && !alluni;
1085
1086	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
1087	if (rm_promisc)
1088		ret = vlan_ops->ena_rx_filtering(vsi);
1089	else
1090		ret = vlan_ops->dis_rx_filtering(vsi);
1091	if (ret) {
1092		dev_err(dev, "Failed to configure VLAN pruning in promiscuous mode\n");
1093		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1094		goto error_param;
1095	}
1096
1097	ice_vf_get_promisc_masks(vf, vsi, &ucast_m, &mcast_m);
1098
1099	if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
1100		if (alluni) {
1101			/* in this case we're turning on promiscuous mode */
1102			ret = ice_set_dflt_vsi(vsi);
1103		} else {
1104			/* in this case we're turning off promiscuous mode */
1105			if (ice_is_dflt_vsi_in_use(vsi->port_info))
1106				ret = ice_clear_dflt_vsi(vsi);
1107		}
1108
1109		/* in this case we're turning on/off only
1110		 * allmulticast
1111		 */
1112		if (allmulti)
1113			mcast_err = ice_vf_set_vsi_promisc(vf, vsi, mcast_m);
1114		else
1115			mcast_err = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
1116
1117		if (ret) {
1118			dev_err(dev, "Turning on/off promiscuous mode for VF %d failed, error: %d\n",
1119				vf->vf_id, ret);
1120			v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1121			goto error_param;
1122		}
1123	} else {
1124		if (alluni)
1125			ucast_err = ice_vf_set_vsi_promisc(vf, vsi, ucast_m);
1126		else
1127			ucast_err = ice_vf_clear_vsi_promisc(vf, vsi, ucast_m);
1128
1129		if (allmulti)
1130			mcast_err = ice_vf_set_vsi_promisc(vf, vsi, mcast_m);
1131		else
1132			mcast_err = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
1133
1134		if (ucast_err || mcast_err)
1135			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1136	}
1137
1138	if (!mcast_err) {
1139		if (allmulti &&
1140		    !test_and_set_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
1141			dev_info(dev, "VF %u successfully set multicast promiscuous mode\n",
1142				 vf->vf_id);
1143		else if (!allmulti &&
1144			 test_and_clear_bit(ICE_VF_STATE_MC_PROMISC,
1145					    vf->vf_states))
1146			dev_info(dev, "VF %u successfully unset multicast promiscuous mode\n",
1147				 vf->vf_id);
1148	} else {
1149		dev_err(dev, "Error while modifying multicast promiscuous mode for VF %u, error: %d\n",
1150			vf->vf_id, mcast_err);
1151	}
1152
1153	if (!ucast_err) {
1154		if (alluni &&
1155		    !test_and_set_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
1156			dev_info(dev, "VF %u successfully set unicast promiscuous mode\n",
1157				 vf->vf_id);
1158		else if (!alluni &&
1159			 test_and_clear_bit(ICE_VF_STATE_UC_PROMISC,
1160					    vf->vf_states))
1161			dev_info(dev, "VF %u successfully unset unicast promiscuous mode\n",
1162				 vf->vf_id);
1163	} else {
1164		dev_err(dev, "Error while modifying unicast promiscuous mode for VF %u, error: %d\n",
1165			vf->vf_id, ucast_err);
1166	}
1167
1168error_param:
1169	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1170				     v_ret, NULL, 0);
1171}
1172
1173/**
1174 * ice_vc_get_stats_msg
1175 * @vf: pointer to the VF info
1176 * @msg: pointer to the msg buffer
1177 *
1178 * called from the VF to get VSI stats
1179 */
1180static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
1181{
1182	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1183	struct virtchnl_queue_select *vqs =
1184		(struct virtchnl_queue_select *)msg;
1185	struct ice_eth_stats stats = { 0 };
1186	struct ice_vsi *vsi;
1187
1188	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1189		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1190		goto error_param;
1191	}
1192
1193	if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1194		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1195		goto error_param;
1196	}
1197
1198	vsi = ice_get_vf_vsi(vf);
1199	if (!vsi) {
1200		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1201		goto error_param;
1202	}
1203
1204	ice_update_eth_stats(vsi);
1205
1206	stats = vsi->eth_stats;
1207
1208error_param:
1209	/* send the response to the VF */
1210	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
1211				     (u8 *)&stats, sizeof(stats));
1212}
1213
1214/**
1215 * ice_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTCHNL
1216 * @vqs: virtchnl_queue_select structure containing bitmaps to validate
1217 *
1218 * Return true on successful validation, else false
1219 */
1220static bool ice_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
1221{
1222	if ((!vqs->rx_queues && !vqs->tx_queues) ||
1223	    vqs->rx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF) ||
1224	    vqs->tx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF))
1225		return false;
1226
1227	return true;
1228}
1229
1230/**
1231 * ice_vf_ena_txq_interrupt - enable Tx queue interrupt via QINT_TQCTL
1232 * @vsi: VSI of the VF to configure
1233 * @q_idx: VF queue index used to determine the queue in the PF's space
1234 */
1235static void ice_vf_ena_txq_interrupt(struct ice_vsi *vsi, u32 q_idx)
1236{
1237	struct ice_hw *hw = &vsi->back->hw;
1238	u32 pfq = vsi->txq_map[q_idx];
1239	u32 reg;
1240
1241	reg = rd32(hw, QINT_TQCTL(pfq));
1242
1243	/* MSI-X index 0 in the VF's space is always for the OICR, which means
1244	 * this is most likely a poll mode VF driver, so don't enable an
1245	 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
1246	 */
1247	if (!(reg & QINT_TQCTL_MSIX_INDX_M))
1248		return;
1249
1250	wr32(hw, QINT_TQCTL(pfq), reg | QINT_TQCTL_CAUSE_ENA_M);
1251}
1252
1253/**
1254 * ice_vf_ena_rxq_interrupt - enable Tx queue interrupt via QINT_RQCTL
1255 * @vsi: VSI of the VF to configure
1256 * @q_idx: VF queue index used to determine the queue in the PF's space
1257 */
1258static void ice_vf_ena_rxq_interrupt(struct ice_vsi *vsi, u32 q_idx)
1259{
1260	struct ice_hw *hw = &vsi->back->hw;
1261	u32 pfq = vsi->rxq_map[q_idx];
1262	u32 reg;
1263
1264	reg = rd32(hw, QINT_RQCTL(pfq));
1265
1266	/* MSI-X index 0 in the VF's space is always for the OICR, which means
1267	 * this is most likely a poll mode VF driver, so don't enable an
1268	 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
1269	 */
1270	if (!(reg & QINT_RQCTL_MSIX_INDX_M))
1271		return;
1272
1273	wr32(hw, QINT_RQCTL(pfq), reg | QINT_RQCTL_CAUSE_ENA_M);
1274}
1275
1276/**
1277 * ice_vc_ena_qs_msg
1278 * @vf: pointer to the VF info
1279 * @msg: pointer to the msg buffer
1280 *
1281 * called from the VF to enable all or specific queue(s)
1282 */
1283static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
1284{
1285	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1286	struct virtchnl_queue_select *vqs =
1287	    (struct virtchnl_queue_select *)msg;
1288	struct ice_vsi *vsi;
1289	unsigned long q_map;
1290	u16 vf_q_id;
1291
1292	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1293		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1294		goto error_param;
1295	}
1296
1297	if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1298		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1299		goto error_param;
1300	}
1301
1302	if (!ice_vc_validate_vqs_bitmaps(vqs)) {
1303		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1304		goto error_param;
1305	}
1306
1307	vsi = ice_get_vf_vsi(vf);
1308	if (!vsi) {
1309		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1310		goto error_param;
1311	}
1312
1313	/* Enable only Rx rings, Tx rings were enabled by the FW when the
1314	 * Tx queue group list was configured and the context bits were
1315	 * programmed using ice_vsi_cfg_txqs
1316	 */
1317	q_map = vqs->rx_queues;
1318	for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1319		if (!ice_vc_isvalid_q_id(vsi, vf_q_id)) {
1320			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1321			goto error_param;
1322		}
1323
1324		/* Skip queue if enabled */
1325		if (test_bit(vf_q_id, vf->rxq_ena))
1326			continue;
1327
1328		if (ice_vsi_ctrl_one_rx_ring(vsi, true, vf_q_id, true)) {
1329			dev_err(ice_pf_to_dev(vsi->back), "Failed to enable Rx ring %d on VSI %d\n",
1330				vf_q_id, vsi->vsi_num);
1331			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1332			goto error_param;
1333		}
1334
1335		ice_vf_ena_rxq_interrupt(vsi, vf_q_id);
1336		set_bit(vf_q_id, vf->rxq_ena);
1337	}
1338
1339	q_map = vqs->tx_queues;
1340	for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1341		if (!ice_vc_isvalid_q_id(vsi, vf_q_id)) {
1342			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1343			goto error_param;
1344		}
1345
1346		/* Skip queue if enabled */
1347		if (test_bit(vf_q_id, vf->txq_ena))
1348			continue;
1349
1350		ice_vf_ena_txq_interrupt(vsi, vf_q_id);
1351		set_bit(vf_q_id, vf->txq_ena);
1352	}
1353
1354	/* Set flag to indicate that queues are enabled */
1355	if (v_ret == VIRTCHNL_STATUS_SUCCESS)
1356		set_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
1357
1358error_param:
1359	/* send the response to the VF */
1360	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
1361				     NULL, 0);
1362}
1363
1364/**
1365 * ice_vf_vsi_dis_single_txq - disable a single Tx queue
1366 * @vf: VF to disable queue for
1367 * @vsi: VSI for the VF
1368 * @q_id: VF relative (0-based) queue ID
1369 *
1370 * Attempt to disable the Tx queue passed in. If the Tx queue was successfully
1371 * disabled then clear q_id bit in the enabled queues bitmap and return
1372 * success. Otherwise return error.
1373 */
1374static int
1375ice_vf_vsi_dis_single_txq(struct ice_vf *vf, struct ice_vsi *vsi, u16 q_id)
1376{
1377	struct ice_txq_meta txq_meta = { 0 };
1378	struct ice_tx_ring *ring;
1379	int err;
1380
1381	if (!test_bit(q_id, vf->txq_ena))
1382		dev_dbg(ice_pf_to_dev(vsi->back), "Queue %u on VSI %u is not enabled, but stopping it anyway\n",
1383			q_id, vsi->vsi_num);
1384
1385	ring = vsi->tx_rings[q_id];
1386	if (!ring)
1387		return -EINVAL;
1388
1389	ice_fill_txq_meta(vsi, ring, &txq_meta);
1390
1391	err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, vf->vf_id, ring, &txq_meta);
1392	if (err) {
1393		dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Tx ring %d on VSI %d\n",
1394			q_id, vsi->vsi_num);
1395		return err;
1396	}
1397
1398	/* Clear enabled queues flag */
1399	clear_bit(q_id, vf->txq_ena);
1400
1401	return 0;
1402}
1403
1404/**
1405 * ice_vc_dis_qs_msg
1406 * @vf: pointer to the VF info
1407 * @msg: pointer to the msg buffer
1408 *
1409 * called from the VF to disable all or specific queue(s)
1410 */
1411static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
1412{
1413	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1414	struct virtchnl_queue_select *vqs =
1415	    (struct virtchnl_queue_select *)msg;
1416	struct ice_vsi *vsi;
1417	unsigned long q_map;
1418	u16 vf_q_id;
1419
1420	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
1421	    !test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states)) {
1422		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1423		goto error_param;
1424	}
1425
1426	if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1427		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1428		goto error_param;
1429	}
1430
1431	if (!ice_vc_validate_vqs_bitmaps(vqs)) {
1432		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1433		goto error_param;
1434	}
1435
1436	vsi = ice_get_vf_vsi(vf);
1437	if (!vsi) {
1438		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1439		goto error_param;
1440	}
1441
1442	if (vqs->tx_queues) {
1443		q_map = vqs->tx_queues;
1444
1445		for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1446			if (!ice_vc_isvalid_q_id(vsi, vf_q_id)) {
1447				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1448				goto error_param;
1449			}
1450
1451			if (ice_vf_vsi_dis_single_txq(vf, vsi, vf_q_id)) {
1452				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1453				goto error_param;
1454			}
1455		}
1456	}
1457
1458	q_map = vqs->rx_queues;
1459	/* speed up Rx queue disable by batching them if possible */
1460	if (q_map &&
1461	    bitmap_equal(&q_map, vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF)) {
1462		if (ice_vsi_stop_all_rx_rings(vsi)) {
1463			dev_err(ice_pf_to_dev(vsi->back), "Failed to stop all Rx rings on VSI %d\n",
1464				vsi->vsi_num);
1465			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1466			goto error_param;
1467		}
1468
1469		bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
1470	} else if (q_map) {
1471		for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1472			if (!ice_vc_isvalid_q_id(vsi, vf_q_id)) {
1473				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1474				goto error_param;
1475			}
1476
1477			/* Skip queue if not enabled */
1478			if (!test_bit(vf_q_id, vf->rxq_ena))
1479				continue;
1480
1481			if (ice_vsi_ctrl_one_rx_ring(vsi, false, vf_q_id,
1482						     true)) {
1483				dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Rx ring %d on VSI %d\n",
1484					vf_q_id, vsi->vsi_num);
1485				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1486				goto error_param;
1487			}
1488
1489			/* Clear enabled queues flag */
1490			clear_bit(vf_q_id, vf->rxq_ena);
1491		}
1492	}
1493
1494	/* Clear enabled queues flag */
1495	if (v_ret == VIRTCHNL_STATUS_SUCCESS && ice_vf_has_no_qs_ena(vf))
1496		clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
1497
1498error_param:
1499	/* send the response to the VF */
1500	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
1501				     NULL, 0);
1502}
1503
1504/**
1505 * ice_cfg_interrupt
1506 * @vf: pointer to the VF info
1507 * @vsi: the VSI being configured
1508 * @vector_id: vector ID
1509 * @map: vector map for mapping vectors to queues
1510 * @q_vector: structure for interrupt vector
1511 * configure the IRQ to queue map
1512 */
1513static int
1514ice_cfg_interrupt(struct ice_vf *vf, struct ice_vsi *vsi, u16 vector_id,
1515		  struct virtchnl_vector_map *map,
1516		  struct ice_q_vector *q_vector)
1517{
1518	u16 vsi_q_id, vsi_q_id_idx;
1519	unsigned long qmap;
1520
1521	q_vector->num_ring_rx = 0;
1522	q_vector->num_ring_tx = 0;
1523
1524	qmap = map->rxq_map;
1525	for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
1526		vsi_q_id = vsi_q_id_idx;
1527
1528		if (!ice_vc_isvalid_q_id(vsi, vsi_q_id))
1529			return VIRTCHNL_STATUS_ERR_PARAM;
1530
1531		q_vector->num_ring_rx++;
1532		q_vector->rx.itr_idx = map->rxitr_idx;
1533		vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
1534		ice_cfg_rxq_interrupt(vsi, vsi_q_id, vector_id,
1535				      q_vector->rx.itr_idx);
1536	}
1537
1538	qmap = map->txq_map;
1539	for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
1540		vsi_q_id = vsi_q_id_idx;
1541
1542		if (!ice_vc_isvalid_q_id(vsi, vsi_q_id))
1543			return VIRTCHNL_STATUS_ERR_PARAM;
1544
1545		q_vector->num_ring_tx++;
1546		q_vector->tx.itr_idx = map->txitr_idx;
1547		vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
1548		ice_cfg_txq_interrupt(vsi, vsi_q_id, vector_id,
1549				      q_vector->tx.itr_idx);
1550	}
1551
1552	return VIRTCHNL_STATUS_SUCCESS;
1553}
1554
1555/**
1556 * ice_vc_cfg_irq_map_msg
1557 * @vf: pointer to the VF info
1558 * @msg: pointer to the msg buffer
1559 *
1560 * called from the VF to configure the IRQ to queue map
1561 */
1562static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
1563{
1564	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1565	u16 num_q_vectors_mapped, vsi_id, vector_id;
1566	struct virtchnl_irq_map_info *irqmap_info;
1567	struct virtchnl_vector_map *map;
 
1568	struct ice_vsi *vsi;
1569	int i;
1570
1571	irqmap_info = (struct virtchnl_irq_map_info *)msg;
1572	num_q_vectors_mapped = irqmap_info->num_vectors;
1573
1574	/* Check to make sure number of VF vectors mapped is not greater than
1575	 * number of VF vectors originally allocated, and check that
1576	 * there is actually at least a single VF queue vector mapped
1577	 */
1578	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
1579	    vf->num_msix < num_q_vectors_mapped ||
1580	    !num_q_vectors_mapped) {
1581		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1582		goto error_param;
1583	}
1584
1585	vsi = ice_get_vf_vsi(vf);
1586	if (!vsi) {
1587		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1588		goto error_param;
1589	}
1590
1591	for (i = 0; i < num_q_vectors_mapped; i++) {
1592		struct ice_q_vector *q_vector;
1593
1594		map = &irqmap_info->vecmap[i];
1595
1596		vector_id = map->vector_id;
1597		vsi_id = map->vsi_id;
1598		/* vector_id is always 0-based for each VF, and can never be
1599		 * larger than or equal to the max allowed interrupts per VF
1600		 */
1601		if (!(vector_id < vf->num_msix) ||
1602		    !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
1603		    (!vector_id && (map->rxq_map || map->txq_map))) {
1604			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1605			goto error_param;
1606		}
1607
1608		/* No need to map VF miscellaneous or rogue vector */
1609		if (!vector_id)
1610			continue;
1611
1612		/* Subtract non queue vector from vector_id passed by VF
1613		 * to get actual number of VSI queue vector array index
1614		 */
1615		q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
1616		if (!q_vector) {
1617			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1618			goto error_param;
1619		}
1620
1621		/* lookout for the invalid queue index */
1622		v_ret = (enum virtchnl_status_code)
1623			ice_cfg_interrupt(vf, vsi, vector_id, map, q_vector);
1624		if (v_ret)
1625			goto error_param;
1626	}
1627
1628error_param:
1629	/* send the response to the VF */
1630	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
1631				     NULL, 0);
1632}
1633
1634/**
1635 * ice_vc_cfg_qs_msg
1636 * @vf: pointer to the VF info
1637 * @msg: pointer to the msg buffer
1638 *
1639 * called from the VF to configure the Rx/Tx queues
1640 */
1641static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
1642{
1643	struct virtchnl_vsi_queue_config_info *qci =
1644	    (struct virtchnl_vsi_queue_config_info *)msg;
1645	struct virtchnl_queue_pair_info *qpi;
1646	struct ice_pf *pf = vf->pf;
1647	struct ice_lag *lag;
1648	struct ice_vsi *vsi;
1649	u8 act_prt, pri_prt;
1650	int i = -1, q_idx;
1651
1652	lag = pf->lag;
1653	mutex_lock(&pf->lag_mutex);
1654	act_prt = ICE_LAG_INVALID_PORT;
1655	pri_prt = pf->hw.port_info->lport;
1656	if (lag && lag->bonded && lag->primary) {
1657		act_prt = lag->active_port;
1658		if (act_prt != pri_prt && act_prt != ICE_LAG_INVALID_PORT &&
1659		    lag->upper_netdev)
1660			ice_lag_move_vf_nodes_cfg(lag, act_prt, pri_prt);
1661		else
1662			act_prt = ICE_LAG_INVALID_PORT;
1663	}
1664
1665	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
1666		goto error_param;
1667
1668	if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id))
1669		goto error_param;
1670
1671	vsi = ice_get_vf_vsi(vf);
1672	if (!vsi)
1673		goto error_param;
1674
1675	if (qci->num_queue_pairs > ICE_MAX_RSS_QS_PER_VF ||
1676	    qci->num_queue_pairs > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
1677		dev_err(ice_pf_to_dev(pf), "VF-%d requesting more than supported number of queues: %d\n",
1678			vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
1679		goto error_param;
1680	}
1681
1682	for (i = 0; i < qci->num_queue_pairs; i++) {
1683		if (!qci->qpair[i].rxq.crc_disable)
1684			continue;
1685
1686		if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_CRC) ||
1687		    vf->vlan_strip_ena)
1688			goto error_param;
1689	}
1690
1691	for (i = 0; i < qci->num_queue_pairs; i++) {
1692		qpi = &qci->qpair[i];
1693		if (qpi->txq.vsi_id != qci->vsi_id ||
1694		    qpi->rxq.vsi_id != qci->vsi_id ||
1695		    qpi->rxq.queue_id != qpi->txq.queue_id ||
1696		    qpi->txq.headwb_enabled ||
1697		    !ice_vc_isvalid_ring_len(qpi->txq.ring_len) ||
1698		    !ice_vc_isvalid_ring_len(qpi->rxq.ring_len) ||
1699		    !ice_vc_isvalid_q_id(vsi, qpi->txq.queue_id)) {
1700			goto error_param;
1701		}
1702
1703		q_idx = qpi->rxq.queue_id;
1704
1705		/* make sure selected "q_idx" is in valid range of queues
1706		 * for selected "vsi"
1707		 */
1708		if (q_idx >= vsi->alloc_txq || q_idx >= vsi->alloc_rxq) {
1709			goto error_param;
1710		}
1711
1712		/* copy Tx queue info from VF into VSI */
1713		if (qpi->txq.ring_len > 0) {
1714			vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
1715			vsi->tx_rings[i]->count = qpi->txq.ring_len;
1716
1717			/* Disable any existing queue first */
1718			if (ice_vf_vsi_dis_single_txq(vf, vsi, q_idx))
1719				goto error_param;
1720
1721			/* Configure a queue with the requested settings */
1722			if (ice_vsi_cfg_single_txq(vsi, vsi->tx_rings, q_idx)) {
1723				dev_warn(ice_pf_to_dev(pf), "VF-%d failed to configure TX queue %d\n",
1724					 vf->vf_id, i);
1725				goto error_param;
1726			}
1727		}
1728
1729		/* copy Rx queue info from VF into VSI */
1730		if (qpi->rxq.ring_len > 0) {
1731			u16 max_frame_size = ice_vc_get_max_frame_size(vf);
1732			u32 rxdid;
1733
1734			vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
1735			vsi->rx_rings[i]->count = qpi->rxq.ring_len;
1736
1737			if (qpi->rxq.crc_disable)
1738				vsi->rx_rings[q_idx]->flags |=
1739					ICE_RX_FLAGS_CRC_STRIP_DIS;
1740			else
1741				vsi->rx_rings[q_idx]->flags &=
1742					~ICE_RX_FLAGS_CRC_STRIP_DIS;
1743
1744			if (qpi->rxq.databuffer_size != 0 &&
1745			    (qpi->rxq.databuffer_size > ((16 * 1024) - 128) ||
1746			     qpi->rxq.databuffer_size < 1024))
1747				goto error_param;
1748			vsi->rx_buf_len = qpi->rxq.databuffer_size;
1749			vsi->rx_rings[i]->rx_buf_len = vsi->rx_buf_len;
1750			if (qpi->rxq.max_pkt_size > max_frame_size ||
1751			    qpi->rxq.max_pkt_size < 64)
1752				goto error_param;
1753
1754			vsi->max_frame = qpi->rxq.max_pkt_size;
1755			/* add space for the port VLAN since the VF driver is
1756			 * not expected to account for it in the MTU
1757			 * calculation
1758			 */
1759			if (ice_vf_is_port_vlan_ena(vf))
1760				vsi->max_frame += VLAN_HLEN;
1761
1762			if (ice_vsi_cfg_single_rxq(vsi, q_idx)) {
1763				dev_warn(ice_pf_to_dev(pf), "VF-%d failed to configure RX queue %d\n",
1764					 vf->vf_id, i);
1765				goto error_param;
1766			}
1767
1768			/* If Rx flex desc is supported, select RXDID for Rx
1769			 * queues. Otherwise, use legacy 32byte descriptor
1770			 * format. Legacy 16byte descriptor is not supported.
1771			 * If this RXDID is selected, return error.
1772			 */
1773			if (vf->driver_caps &
1774			    VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC) {
1775				rxdid = qpi->rxq.rxdid;
1776				if (!(BIT(rxdid) & pf->supported_rxdids))
1777					goto error_param;
1778			} else {
1779				rxdid = ICE_RXDID_LEGACY_1;
1780			}
1781
1782			ice_write_qrxflxp_cntxt(&vsi->back->hw,
1783						vsi->rxq_map[q_idx],
1784						rxdid, 0x03, false);
1785		}
1786	}
1787
1788	if (lag && lag->bonded && lag->primary &&
1789	    act_prt != ICE_LAG_INVALID_PORT)
1790		ice_lag_move_vf_nodes_cfg(lag, pri_prt, act_prt);
1791	mutex_unlock(&pf->lag_mutex);
1792
1793	/* send the response to the VF */
1794	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1795				     VIRTCHNL_STATUS_SUCCESS, NULL, 0);
1796error_param:
1797	/* disable whatever we can */
1798	for (; i >= 0; i--) {
1799		if (ice_vsi_ctrl_one_rx_ring(vsi, false, i, true))
1800			dev_err(ice_pf_to_dev(pf), "VF-%d could not disable RX queue %d\n",
1801				vf->vf_id, i);
1802		if (ice_vf_vsi_dis_single_txq(vf, vsi, i))
1803			dev_err(ice_pf_to_dev(pf), "VF-%d could not disable TX queue %d\n",
1804				vf->vf_id, i);
1805	}
1806
1807	if (lag && lag->bonded && lag->primary &&
1808	    act_prt != ICE_LAG_INVALID_PORT)
1809		ice_lag_move_vf_nodes_cfg(lag, pri_prt, act_prt);
1810	mutex_unlock(&pf->lag_mutex);
1811
1812	ice_lag_move_new_vf_nodes(vf);
1813
1814	/* send the response to the VF */
1815	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1816				     VIRTCHNL_STATUS_ERR_PARAM, NULL, 0);
1817}
1818
1819/**
1820 * ice_can_vf_change_mac
1821 * @vf: pointer to the VF info
1822 *
1823 * Return true if the VF is allowed to change its MAC filters, false otherwise
1824 */
1825static bool ice_can_vf_change_mac(struct ice_vf *vf)
1826{
1827	/* If the VF MAC address has been set administratively (via the
1828	 * ndo_set_vf_mac command), then deny permission to the VF to
1829	 * add/delete unicast MAC addresses, unless the VF is trusted
1830	 */
1831	if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
1832		return false;
1833
1834	return true;
1835}
1836
1837/**
1838 * ice_vc_ether_addr_type - get type of virtchnl_ether_addr
1839 * @vc_ether_addr: used to extract the type
1840 */
1841static u8
1842ice_vc_ether_addr_type(struct virtchnl_ether_addr *vc_ether_addr)
1843{
1844	return (vc_ether_addr->type & VIRTCHNL_ETHER_ADDR_TYPE_MASK);
1845}
1846
1847/**
1848 * ice_is_vc_addr_legacy - check if the MAC address is from an older VF
1849 * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
1850 */
1851static bool
1852ice_is_vc_addr_legacy(struct virtchnl_ether_addr *vc_ether_addr)
1853{
1854	u8 type = ice_vc_ether_addr_type(vc_ether_addr);
1855
1856	return (type == VIRTCHNL_ETHER_ADDR_LEGACY);
1857}
1858
1859/**
1860 * ice_is_vc_addr_primary - check if the MAC address is the VF's primary MAC
1861 * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
1862 *
1863 * This function should only be called when the MAC address in
1864 * virtchnl_ether_addr is a valid unicast MAC
1865 */
1866static bool
1867ice_is_vc_addr_primary(struct virtchnl_ether_addr __maybe_unused *vc_ether_addr)
1868{
1869	u8 type = ice_vc_ether_addr_type(vc_ether_addr);
1870
1871	return (type == VIRTCHNL_ETHER_ADDR_PRIMARY);
1872}
1873
1874/**
1875 * ice_vfhw_mac_add - update the VF's cached hardware MAC if allowed
1876 * @vf: VF to update
1877 * @vc_ether_addr: structure from VIRTCHNL with MAC to add
1878 */
1879static void
1880ice_vfhw_mac_add(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
1881{
1882	u8 *mac_addr = vc_ether_addr->addr;
1883
1884	if (!is_valid_ether_addr(mac_addr))
1885		return;
1886
1887	/* only allow legacy VF drivers to set the device and hardware MAC if it
1888	 * is zero and allow new VF drivers to set the hardware MAC if the type
1889	 * was correctly specified over VIRTCHNL
1890	 */
1891	if ((ice_is_vc_addr_legacy(vc_ether_addr) &&
1892	     is_zero_ether_addr(vf->hw_lan_addr)) ||
1893	    ice_is_vc_addr_primary(vc_ether_addr)) {
1894		ether_addr_copy(vf->dev_lan_addr, mac_addr);
1895		ether_addr_copy(vf->hw_lan_addr, mac_addr);
1896	}
1897
1898	/* hardware and device MACs are already set, but its possible that the
1899	 * VF driver sent the VIRTCHNL_OP_ADD_ETH_ADDR message before the
1900	 * VIRTCHNL_OP_DEL_ETH_ADDR when trying to update its MAC, so save it
1901	 * away for the legacy VF driver case as it will be updated in the
1902	 * delete flow for this case
1903	 */
1904	if (ice_is_vc_addr_legacy(vc_ether_addr)) {
1905		ether_addr_copy(vf->legacy_last_added_umac.addr,
1906				mac_addr);
1907		vf->legacy_last_added_umac.time_modified = jiffies;
1908	}
1909}
1910
1911/**
1912 * ice_vc_add_mac_addr - attempt to add the MAC address passed in
1913 * @vf: pointer to the VF info
1914 * @vsi: pointer to the VF's VSI
1915 * @vc_ether_addr: VIRTCHNL MAC address structure used to add MAC
1916 */
1917static int
1918ice_vc_add_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
1919		    struct virtchnl_ether_addr *vc_ether_addr)
1920{
1921	struct device *dev = ice_pf_to_dev(vf->pf);
1922	u8 *mac_addr = vc_ether_addr->addr;
1923	int ret;
1924
1925	/* device MAC already added */
1926	if (ether_addr_equal(mac_addr, vf->dev_lan_addr))
1927		return 0;
1928
1929	if (is_unicast_ether_addr(mac_addr) && !ice_can_vf_change_mac(vf)) {
1930		dev_err(dev, "VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
1931		return -EPERM;
1932	}
1933
1934	ret = ice_fltr_add_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
1935	if (ret == -EEXIST) {
1936		dev_dbg(dev, "MAC %pM already exists for VF %d\n", mac_addr,
1937			vf->vf_id);
1938		/* don't return since we might need to update
1939		 * the primary MAC in ice_vfhw_mac_add() below
1940		 */
1941	} else if (ret) {
1942		dev_err(dev, "Failed to add MAC %pM for VF %d\n, error %d\n",
1943			mac_addr, vf->vf_id, ret);
1944		return ret;
1945	} else {
1946		vf->num_mac++;
1947	}
1948
1949	ice_vfhw_mac_add(vf, vc_ether_addr);
1950
1951	return ret;
1952}
1953
1954/**
1955 * ice_is_legacy_umac_expired - check if last added legacy unicast MAC expired
1956 * @last_added_umac: structure used to check expiration
1957 */
1958static bool ice_is_legacy_umac_expired(struct ice_time_mac *last_added_umac)
1959{
1960#define ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME	msecs_to_jiffies(3000)
1961	return time_is_before_jiffies(last_added_umac->time_modified +
1962				      ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME);
1963}
1964
1965/**
1966 * ice_update_legacy_cached_mac - update cached hardware MAC for legacy VF
1967 * @vf: VF to update
1968 * @vc_ether_addr: structure from VIRTCHNL with MAC to check
1969 *
1970 * only update cached hardware MAC for legacy VF drivers on delete
1971 * because we cannot guarantee order/type of MAC from the VF driver
1972 */
1973static void
1974ice_update_legacy_cached_mac(struct ice_vf *vf,
1975			     struct virtchnl_ether_addr *vc_ether_addr)
1976{
1977	if (!ice_is_vc_addr_legacy(vc_ether_addr) ||
1978	    ice_is_legacy_umac_expired(&vf->legacy_last_added_umac))
1979		return;
1980
1981	ether_addr_copy(vf->dev_lan_addr, vf->legacy_last_added_umac.addr);
1982	ether_addr_copy(vf->hw_lan_addr, vf->legacy_last_added_umac.addr);
1983}
1984
1985/**
1986 * ice_vfhw_mac_del - update the VF's cached hardware MAC if allowed
1987 * @vf: VF to update
1988 * @vc_ether_addr: structure from VIRTCHNL with MAC to delete
1989 */
1990static void
1991ice_vfhw_mac_del(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
1992{
1993	u8 *mac_addr = vc_ether_addr->addr;
1994
1995	if (!is_valid_ether_addr(mac_addr) ||
1996	    !ether_addr_equal(vf->dev_lan_addr, mac_addr))
1997		return;
1998
1999	/* allow the device MAC to be repopulated in the add flow and don't
2000	 * clear the hardware MAC (i.e. hw_lan_addr) here as that is meant
2001	 * to be persistent on VM reboot and across driver unload/load, which
2002	 * won't work if we clear the hardware MAC here
2003	 */
2004	eth_zero_addr(vf->dev_lan_addr);
2005
2006	ice_update_legacy_cached_mac(vf, vc_ether_addr);
2007}
2008
2009/**
2010 * ice_vc_del_mac_addr - attempt to delete the MAC address passed in
2011 * @vf: pointer to the VF info
2012 * @vsi: pointer to the VF's VSI
2013 * @vc_ether_addr: VIRTCHNL MAC address structure used to delete MAC
2014 */
2015static int
2016ice_vc_del_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
2017		    struct virtchnl_ether_addr *vc_ether_addr)
2018{
2019	struct device *dev = ice_pf_to_dev(vf->pf);
2020	u8 *mac_addr = vc_ether_addr->addr;
2021	int status;
2022
2023	if (!ice_can_vf_change_mac(vf) &&
2024	    ether_addr_equal(vf->dev_lan_addr, mac_addr))
2025		return 0;
2026
2027	status = ice_fltr_remove_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
2028	if (status == -ENOENT) {
2029		dev_err(dev, "MAC %pM does not exist for VF %d\n", mac_addr,
2030			vf->vf_id);
2031		return -ENOENT;
2032	} else if (status) {
2033		dev_err(dev, "Failed to delete MAC %pM for VF %d, error %d\n",
2034			mac_addr, vf->vf_id, status);
2035		return -EIO;
2036	}
2037
2038	ice_vfhw_mac_del(vf, vc_ether_addr);
2039
2040	vf->num_mac--;
2041
2042	return 0;
2043}
2044
2045/**
2046 * ice_vc_handle_mac_addr_msg
2047 * @vf: pointer to the VF info
2048 * @msg: pointer to the msg buffer
2049 * @set: true if MAC filters are being set, false otherwise
2050 *
2051 * add guest MAC address filter
2052 */
2053static int
2054ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
2055{
2056	int (*ice_vc_cfg_mac)
2057		(struct ice_vf *vf, struct ice_vsi *vsi,
2058		 struct virtchnl_ether_addr *virtchnl_ether_addr);
2059	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2060	struct virtchnl_ether_addr_list *al =
2061	    (struct virtchnl_ether_addr_list *)msg;
2062	struct ice_pf *pf = vf->pf;
2063	enum virtchnl_ops vc_op;
2064	struct ice_vsi *vsi;
2065	int i;
2066
2067	if (set) {
2068		vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
2069		ice_vc_cfg_mac = ice_vc_add_mac_addr;
2070	} else {
2071		vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
2072		ice_vc_cfg_mac = ice_vc_del_mac_addr;
2073	}
2074
2075	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
2076	    !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
2077		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2078		goto handle_mac_exit;
2079	}
2080
2081	/* If this VF is not privileged, then we can't add more than a
2082	 * limited number of addresses. Check to make sure that the
2083	 * additions do not push us over the limit.
2084	 */
2085	if (set && !ice_is_vf_trusted(vf) &&
2086	    (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
2087		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",
2088			vf->vf_id);
2089		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2090		goto handle_mac_exit;
2091	}
2092
2093	vsi = ice_get_vf_vsi(vf);
2094	if (!vsi) {
2095		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2096		goto handle_mac_exit;
2097	}
2098
2099	for (i = 0; i < al->num_elements; i++) {
2100		u8 *mac_addr = al->list[i].addr;
2101		int result;
2102
2103		if (is_broadcast_ether_addr(mac_addr) ||
2104		    is_zero_ether_addr(mac_addr))
2105			continue;
2106
2107		result = ice_vc_cfg_mac(vf, vsi, &al->list[i]);
2108		if (result == -EEXIST || result == -ENOENT) {
2109			continue;
2110		} else if (result) {
2111			v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2112			goto handle_mac_exit;
2113		}
2114	}
2115
2116handle_mac_exit:
2117	/* send the response to the VF */
2118	return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
2119}
2120
2121/**
2122 * ice_vc_add_mac_addr_msg
2123 * @vf: pointer to the VF info
2124 * @msg: pointer to the msg buffer
2125 *
2126 * add guest MAC address filter
2127 */
2128static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2129{
2130	return ice_vc_handle_mac_addr_msg(vf, msg, true);
2131}
2132
2133/**
2134 * ice_vc_del_mac_addr_msg
2135 * @vf: pointer to the VF info
2136 * @msg: pointer to the msg buffer
2137 *
2138 * remove guest MAC address filter
2139 */
2140static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2141{
2142	return ice_vc_handle_mac_addr_msg(vf, msg, false);
2143}
2144
2145/**
2146 * ice_vc_request_qs_msg
2147 * @vf: pointer to the VF info
2148 * @msg: pointer to the msg buffer
2149 *
2150 * VFs get a default number of queues but can use this message to request a
2151 * different number. If the request is successful, PF will reset the VF and
2152 * return 0. If unsuccessful, PF will send message informing VF of number of
2153 * available queue pairs via virtchnl message response to VF.
2154 */
2155static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
2156{
2157	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2158	struct virtchnl_vf_res_request *vfres =
2159		(struct virtchnl_vf_res_request *)msg;
2160	u16 req_queues = vfres->num_queue_pairs;
2161	struct ice_pf *pf = vf->pf;
2162	u16 max_allowed_vf_queues;
2163	u16 tx_rx_queue_left;
2164	struct device *dev;
2165	u16 cur_queues;
2166
2167	dev = ice_pf_to_dev(pf);
2168	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2169		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2170		goto error_param;
2171	}
2172
2173	cur_queues = vf->num_vf_qs;
2174	tx_rx_queue_left = min_t(u16, ice_get_avail_txq_count(pf),
2175				 ice_get_avail_rxq_count(pf));
2176	max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
2177	if (!req_queues) {
2178		dev_err(dev, "VF %d tried to request 0 queues. Ignoring.\n",
2179			vf->vf_id);
2180	} else if (req_queues > ICE_MAX_RSS_QS_PER_VF) {
2181		dev_err(dev, "VF %d tried to request more than %d queues.\n",
2182			vf->vf_id, ICE_MAX_RSS_QS_PER_VF);
2183		vfres->num_queue_pairs = ICE_MAX_RSS_QS_PER_VF;
2184	} else if (req_queues > cur_queues &&
2185		   req_queues - cur_queues > tx_rx_queue_left) {
2186		dev_warn(dev, "VF %d requested %u more queues, but only %u left.\n",
2187			 vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
2188		vfres->num_queue_pairs = min_t(u16, max_allowed_vf_queues,
2189					       ICE_MAX_RSS_QS_PER_VF);
2190	} else {
2191		/* request is successful, then reset VF */
2192		vf->num_req_qs = req_queues;
2193		ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
2194		dev_info(dev, "VF %d granted request of %u queues.\n",
2195			 vf->vf_id, req_queues);
2196		return 0;
2197	}
2198
2199error_param:
2200	/* send the response to the VF */
2201	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
2202				     v_ret, (u8 *)vfres, sizeof(*vfres));
2203}
2204
2205/**
2206 * ice_vf_vlan_offload_ena - determine if capabilities support VLAN offloads
2207 * @caps: VF driver negotiated capabilities
2208 *
2209 * Return true if VIRTCHNL_VF_OFFLOAD_VLAN capability is set, else return false
2210 */
2211static bool ice_vf_vlan_offload_ena(u32 caps)
2212{
2213	return !!(caps & VIRTCHNL_VF_OFFLOAD_VLAN);
2214}
2215
2216/**
2217 * ice_is_vlan_promisc_allowed - check if VLAN promiscuous config is allowed
2218 * @vf: VF used to determine if VLAN promiscuous config is allowed
2219 */
2220static bool ice_is_vlan_promisc_allowed(struct ice_vf *vf)
2221{
2222	if ((test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2223	     test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) &&
2224	    test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, vf->pf->flags))
2225		return true;
2226
2227	return false;
2228}
2229
2230/**
2231 * ice_vf_ena_vlan_promisc - Enable Tx/Rx VLAN promiscuous for the VLAN
2232 * @vsi: VF's VSI used to enable VLAN promiscuous mode
2233 * @vlan: VLAN used to enable VLAN promiscuous
2234 *
2235 * This function should only be called if VLAN promiscuous mode is allowed,
2236 * which can be determined via ice_is_vlan_promisc_allowed().
2237 */
2238static int ice_vf_ena_vlan_promisc(struct ice_vsi *vsi, struct ice_vlan *vlan)
2239{
2240	u8 promisc_m = ICE_PROMISC_VLAN_TX | ICE_PROMISC_VLAN_RX;
2241	int status;
2242
2243	status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m,
2244					  vlan->vid);
2245	if (status && status != -EEXIST)
2246		return status;
2247
2248	return 0;
2249}
2250
2251/**
2252 * ice_vf_dis_vlan_promisc - Disable Tx/Rx VLAN promiscuous for the VLAN
2253 * @vsi: VF's VSI used to disable VLAN promiscuous mode for
2254 * @vlan: VLAN used to disable VLAN promiscuous
2255 *
2256 * This function should only be called if VLAN promiscuous mode is allowed,
2257 * which can be determined via ice_is_vlan_promisc_allowed().
2258 */
2259static int ice_vf_dis_vlan_promisc(struct ice_vsi *vsi, struct ice_vlan *vlan)
2260{
2261	u8 promisc_m = ICE_PROMISC_VLAN_TX | ICE_PROMISC_VLAN_RX;
2262	int status;
2263
2264	status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m,
2265					    vlan->vid);
2266	if (status && status != -ENOENT)
2267		return status;
2268
2269	return 0;
2270}
2271
2272/**
2273 * ice_vf_has_max_vlans - check if VF already has the max allowed VLAN filters
2274 * @vf: VF to check against
2275 * @vsi: VF's VSI
2276 *
2277 * If the VF is trusted then the VF is allowed to add as many VLANs as it
2278 * wants to, so return false.
2279 *
2280 * When the VF is untrusted compare the number of non-zero VLANs + 1 to the max
2281 * allowed VLANs for an untrusted VF. Return the result of this comparison.
2282 */
2283static bool ice_vf_has_max_vlans(struct ice_vf *vf, struct ice_vsi *vsi)
2284{
2285	if (ice_is_vf_trusted(vf))
2286		return false;
2287
2288#define ICE_VF_ADDED_VLAN_ZERO_FLTRS	1
2289	return ((ice_vsi_num_non_zero_vlans(vsi) +
2290		ICE_VF_ADDED_VLAN_ZERO_FLTRS) >= ICE_MAX_VLAN_PER_VF);
2291}
2292
2293/**
2294 * ice_vc_process_vlan_msg
2295 * @vf: pointer to the VF info
2296 * @msg: pointer to the msg buffer
2297 * @add_v: Add VLAN if true, otherwise delete VLAN
2298 *
2299 * Process virtchnl op to add or remove programmed guest VLAN ID
2300 */
2301static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
2302{
2303	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2304	struct virtchnl_vlan_filter_list *vfl =
2305	    (struct virtchnl_vlan_filter_list *)msg;
2306	struct ice_pf *pf = vf->pf;
2307	bool vlan_promisc = false;
2308	struct ice_vsi *vsi;
2309	struct device *dev;
2310	int status = 0;
2311	int i;
2312
2313	dev = ice_pf_to_dev(pf);
2314	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2315		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2316		goto error_param;
2317	}
2318
2319	if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2320		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2321		goto error_param;
2322	}
2323
2324	if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
2325		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2326		goto error_param;
2327	}
2328
2329	for (i = 0; i < vfl->num_elements; i++) {
2330		if (vfl->vlan_id[i] >= VLAN_N_VID) {
2331			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2332			dev_err(dev, "invalid VF VLAN id %d\n",
2333				vfl->vlan_id[i]);
2334			goto error_param;
2335		}
2336	}
2337
2338	vsi = ice_get_vf_vsi(vf);
2339	if (!vsi) {
2340		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2341		goto error_param;
2342	}
2343
2344	if (add_v && ice_vf_has_max_vlans(vf, vsi)) {
2345		dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2346			 vf->vf_id);
2347		/* There is no need to let VF know about being not trusted,
2348		 * so we can just return success message here
2349		 */
2350		goto error_param;
2351	}
2352
2353	/* in DVM a VF can add/delete inner VLAN filters when
2354	 * VIRTCHNL_VF_OFFLOAD_VLAN is negotiated, so only reject in SVM
2355	 */
2356	if (ice_vf_is_port_vlan_ena(vf) && !ice_is_dvm_ena(&pf->hw)) {
2357		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2358		goto error_param;
2359	}
2360
2361	/* in DVM VLAN promiscuous is based on the outer VLAN, which would be
2362	 * the port VLAN if VIRTCHNL_VF_OFFLOAD_VLAN was negotiated, so only
2363	 * allow vlan_promisc = true in SVM and if no port VLAN is configured
2364	 */
2365	vlan_promisc = ice_is_vlan_promisc_allowed(vf) &&
2366		!ice_is_dvm_ena(&pf->hw) &&
2367		!ice_vf_is_port_vlan_ena(vf);
2368
2369	if (add_v) {
2370		for (i = 0; i < vfl->num_elements; i++) {
2371			u16 vid = vfl->vlan_id[i];
2372			struct ice_vlan vlan;
2373
2374			if (ice_vf_has_max_vlans(vf, vsi)) {
2375				dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2376					 vf->vf_id);
2377				/* There is no need to let VF know about being
2378				 * not trusted, so we can just return success
2379				 * message here as well.
2380				 */
2381				goto error_param;
2382			}
2383
2384			/* we add VLAN 0 by default for each VF so we can enable
2385			 * Tx VLAN anti-spoof without triggering MDD events so
2386			 * we don't need to add it again here
2387			 */
2388			if (!vid)
2389				continue;
2390
2391			vlan = ICE_VLAN(ETH_P_8021Q, vid, 0);
2392			status = vsi->inner_vlan_ops.add_vlan(vsi, &vlan);
2393			if (status) {
2394				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2395				goto error_param;
2396			}
2397
2398			/* Enable VLAN filtering on first non-zero VLAN */
2399			if (!vlan_promisc && vid && !ice_is_dvm_ena(&pf->hw)) {
2400				if (vf->spoofchk) {
2401					status = vsi->inner_vlan_ops.ena_tx_filtering(vsi);
2402					if (status) {
2403						v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2404						dev_err(dev, "Enable VLAN anti-spoofing on VLAN ID: %d failed error-%d\n",
2405							vid, status);
2406						goto error_param;
2407					}
2408				}
2409				if (vsi->inner_vlan_ops.ena_rx_filtering(vsi)) {
2410					v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2411					dev_err(dev, "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
2412						vid, status);
2413					goto error_param;
2414				}
2415			} else if (vlan_promisc) {
2416				status = ice_vf_ena_vlan_promisc(vsi, &vlan);
2417				if (status) {
2418					v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2419					dev_err(dev, "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
2420						vid, status);
2421				}
2422			}
2423		}
2424	} else {
2425		/* In case of non_trusted VF, number of VLAN elements passed
2426		 * to PF for removal might be greater than number of VLANs
2427		 * filter programmed for that VF - So, use actual number of
2428		 * VLANS added earlier with add VLAN opcode. In order to avoid
2429		 * removing VLAN that doesn't exist, which result to sending
2430		 * erroneous failed message back to the VF
2431		 */
2432		int num_vf_vlan;
2433
2434		num_vf_vlan = vsi->num_vlan;
2435		for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
2436			u16 vid = vfl->vlan_id[i];
2437			struct ice_vlan vlan;
2438
2439			/* we add VLAN 0 by default for each VF so we can enable
2440			 * Tx VLAN anti-spoof without triggering MDD events so
2441			 * we don't want a VIRTCHNL request to remove it
2442			 */
2443			if (!vid)
2444				continue;
2445
2446			vlan = ICE_VLAN(ETH_P_8021Q, vid, 0);
2447			status = vsi->inner_vlan_ops.del_vlan(vsi, &vlan);
2448			if (status) {
2449				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2450				goto error_param;
2451			}
2452
2453			/* Disable VLAN filtering when only VLAN 0 is left */
2454			if (!ice_vsi_has_non_zero_vlans(vsi)) {
2455				vsi->inner_vlan_ops.dis_tx_filtering(vsi);
2456				vsi->inner_vlan_ops.dis_rx_filtering(vsi);
2457			}
2458
2459			if (vlan_promisc)
2460				ice_vf_dis_vlan_promisc(vsi, &vlan);
2461		}
2462	}
2463
2464error_param:
2465	/* send the response to the VF */
2466	if (add_v)
2467		return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
2468					     NULL, 0);
2469	else
2470		return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
2471					     NULL, 0);
2472}
2473
2474/**
2475 * ice_vc_add_vlan_msg
2476 * @vf: pointer to the VF info
2477 * @msg: pointer to the msg buffer
2478 *
2479 * Add and program guest VLAN ID
2480 */
2481static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
2482{
2483	return ice_vc_process_vlan_msg(vf, msg, true);
2484}
2485
2486/**
2487 * ice_vc_remove_vlan_msg
2488 * @vf: pointer to the VF info
2489 * @msg: pointer to the msg buffer
2490 *
2491 * remove programmed guest VLAN ID
2492 */
2493static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
2494{
2495	return ice_vc_process_vlan_msg(vf, msg, false);
2496}
2497
2498/**
2499 * ice_vsi_is_rxq_crc_strip_dis - check if Rx queue CRC strip is disabled or not
2500 * @vsi: pointer to the VF VSI info
2501 */
2502static bool ice_vsi_is_rxq_crc_strip_dis(struct ice_vsi *vsi)
2503{
2504	unsigned int i;
2505
2506	ice_for_each_alloc_rxq(vsi, i)
2507		if (vsi->rx_rings[i]->flags & ICE_RX_FLAGS_CRC_STRIP_DIS)
2508			return true;
2509
2510	return false;
2511}
2512
2513/**
2514 * ice_vc_ena_vlan_stripping
2515 * @vf: pointer to the VF info
2516 *
2517 * Enable VLAN header stripping for a given VF
2518 */
2519static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
2520{
2521	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2522	struct ice_vsi *vsi;
2523
2524	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2525		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2526		goto error_param;
2527	}
2528
2529	if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2530		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2531		goto error_param;
2532	}
2533
2534	vsi = ice_get_vf_vsi(vf);
2535	if (!vsi) {
2536		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2537		goto error_param;
2538	}
2539
2540	if (vsi->inner_vlan_ops.ena_stripping(vsi, ETH_P_8021Q))
2541		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2542	else
2543		vf->vlan_strip_ena |= ICE_INNER_VLAN_STRIP_ENA;
2544
2545error_param:
2546	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
2547				     v_ret, NULL, 0);
2548}
2549
2550/**
2551 * ice_vc_dis_vlan_stripping
2552 * @vf: pointer to the VF info
2553 *
2554 * Disable VLAN header stripping for a given VF
2555 */
2556static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
2557{
2558	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2559	struct ice_vsi *vsi;
2560
2561	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2562		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2563		goto error_param;
2564	}
2565
2566	if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2567		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2568		goto error_param;
2569	}
2570
2571	vsi = ice_get_vf_vsi(vf);
2572	if (!vsi) {
2573		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2574		goto error_param;
2575	}
2576
2577	if (vsi->inner_vlan_ops.dis_stripping(vsi))
2578		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2579	else
2580		vf->vlan_strip_ena &= ~ICE_INNER_VLAN_STRIP_ENA;
2581
2582error_param:
2583	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
2584				     v_ret, NULL, 0);
2585}
2586
2587/**
2588 * ice_vc_get_rss_hena - return the RSS HENA bits allowed by the hardware
2589 * @vf: pointer to the VF info
2590 */
2591static int ice_vc_get_rss_hena(struct ice_vf *vf)
2592{
2593	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2594	struct virtchnl_rss_hena *vrh = NULL;
2595	int len = 0, ret;
2596
2597	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2598		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2599		goto err;
2600	}
2601
2602	if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2603		dev_err(ice_pf_to_dev(vf->pf), "RSS not supported by PF\n");
2604		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2605		goto err;
2606	}
2607
2608	len = sizeof(struct virtchnl_rss_hena);
2609	vrh = kzalloc(len, GFP_KERNEL);
2610	if (!vrh) {
2611		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2612		len = 0;
2613		goto err;
2614	}
2615
2616	vrh->hena = ICE_DEFAULT_RSS_HENA;
2617err:
2618	/* send the response back to the VF */
2619	ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS, v_ret,
2620				    (u8 *)vrh, len);
2621	kfree(vrh);
2622	return ret;
2623}
2624
2625/**
2626 * ice_vc_set_rss_hena - set RSS HENA bits for the VF
2627 * @vf: pointer to the VF info
2628 * @msg: pointer to the msg buffer
2629 */
2630static int ice_vc_set_rss_hena(struct ice_vf *vf, u8 *msg)
2631{
2632	struct virtchnl_rss_hena *vrh = (struct virtchnl_rss_hena *)msg;
2633	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2634	struct ice_pf *pf = vf->pf;
2635	struct ice_vsi *vsi;
2636	struct device *dev;
2637	int status;
2638
2639	dev = ice_pf_to_dev(pf);
2640
2641	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2642		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2643		goto err;
2644	}
2645
2646	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2647		dev_err(dev, "RSS not supported by PF\n");
2648		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2649		goto err;
2650	}
2651
2652	vsi = ice_get_vf_vsi(vf);
2653	if (!vsi) {
2654		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2655		goto err;
2656	}
2657
2658	/* clear all previously programmed RSS configuration to allow VF drivers
2659	 * the ability to customize the RSS configuration and/or completely
2660	 * disable RSS
2661	 */
2662	status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
2663	if (status && !vrh->hena) {
2664		/* only report failure to clear the current RSS configuration if
2665		 * that was clearly the VF's intention (i.e. vrh->hena = 0)
2666		 */
2667		v_ret = ice_err_to_virt_err(status);
2668		goto err;
2669	} else if (status) {
2670		/* allow the VF to update the RSS configuration even on failure
2671		 * to clear the current RSS confguration in an attempt to keep
2672		 * RSS in a working state
2673		 */
2674		dev_warn(dev, "Failed to clear the RSS configuration for VF %u\n",
2675			 vf->vf_id);
2676	}
2677
2678	if (vrh->hena) {
2679		status = ice_add_avf_rss_cfg(&pf->hw, vsi, vrh->hena);
2680		v_ret = ice_err_to_virt_err(status);
2681	}
2682
2683	/* send the response to the VF */
2684err:
2685	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, v_ret,
2686				     NULL, 0);
2687}
2688
2689/**
2690 * ice_vc_query_rxdid - query RXDID supported by DDP package
2691 * @vf: pointer to VF info
2692 *
2693 * Called from VF to query a bitmap of supported flexible
2694 * descriptor RXDIDs of a DDP package.
2695 */
2696static int ice_vc_query_rxdid(struct ice_vf *vf)
2697{
2698	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2699	struct virtchnl_supported_rxdids *rxdid = NULL;
2700	struct ice_hw *hw = &vf->pf->hw;
2701	struct ice_pf *pf = vf->pf;
2702	int len = 0;
2703	int ret, i;
2704	u32 regval;
2705
2706	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2707		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2708		goto err;
2709	}
2710
2711	if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC)) {
2712		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2713		goto err;
2714	}
2715
2716	len = sizeof(struct virtchnl_supported_rxdids);
2717	rxdid = kzalloc(len, GFP_KERNEL);
2718	if (!rxdid) {
2719		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2720		len = 0;
2721		goto err;
2722	}
2723
2724	/* RXDIDs supported by DDP package can be read from the register
2725	 * to get the supported RXDID bitmap. But the legacy 32byte RXDID
2726	 * is not listed in DDP package, add it in the bitmap manually.
2727	 * Legacy 16byte descriptor is not supported.
2728	 */
2729	rxdid->supported_rxdids |= BIT(ICE_RXDID_LEGACY_1);
2730
2731	for (i = ICE_RXDID_FLEX_NIC; i < ICE_FLEX_DESC_RXDID_MAX_NUM; i++) {
2732		regval = rd32(hw, GLFLXP_RXDID_FLAGS(i, 0));
2733		if ((regval >> GLFLXP_RXDID_FLAGS_FLEXIFLAG_4N_S)
2734			& GLFLXP_RXDID_FLAGS_FLEXIFLAG_4N_M)
2735			rxdid->supported_rxdids |= BIT(i);
2736	}
2737
2738	pf->supported_rxdids = rxdid->supported_rxdids;
2739
2740err:
2741	ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_SUPPORTED_RXDIDS,
2742				    v_ret, (u8 *)rxdid, len);
2743	kfree(rxdid);
2744	return ret;
2745}
2746
2747/**
2748 * ice_vf_init_vlan_stripping - enable/disable VLAN stripping on initialization
2749 * @vf: VF to enable/disable VLAN stripping for on initialization
2750 *
2751 * Set the default for VLAN stripping based on whether a port VLAN is configured
2752 * and the current VLAN mode of the device.
2753 */
2754static int ice_vf_init_vlan_stripping(struct ice_vf *vf)
2755{
2756	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
2757
2758	vf->vlan_strip_ena = 0;
2759
2760	if (!vsi)
2761		return -EINVAL;
2762
2763	/* don't modify stripping if port VLAN is configured in SVM since the
2764	 * port VLAN is based on the inner/single VLAN in SVM
2765	 */
2766	if (ice_vf_is_port_vlan_ena(vf) && !ice_is_dvm_ena(&vsi->back->hw))
2767		return 0;
2768
2769	if (ice_vf_vlan_offload_ena(vf->driver_caps)) {
2770		int err;
2771
2772		err = vsi->inner_vlan_ops.ena_stripping(vsi, ETH_P_8021Q);
2773		if (!err)
2774			vf->vlan_strip_ena |= ICE_INNER_VLAN_STRIP_ENA;
2775		return err;
2776	}
2777
2778	return vsi->inner_vlan_ops.dis_stripping(vsi);
2779}
2780
2781static u16 ice_vc_get_max_vlan_fltrs(struct ice_vf *vf)
2782{
2783	if (vf->trusted)
2784		return VLAN_N_VID;
2785	else
2786		return ICE_MAX_VLAN_PER_VF;
2787}
2788
2789/**
2790 * ice_vf_outer_vlan_not_allowed - check if outer VLAN can be used
2791 * @vf: VF that being checked for
2792 *
2793 * When the device is in double VLAN mode, check whether or not the outer VLAN
2794 * is allowed.
2795 */
2796static bool ice_vf_outer_vlan_not_allowed(struct ice_vf *vf)
2797{
2798	if (ice_vf_is_port_vlan_ena(vf))
2799		return true;
2800
2801	return false;
2802}
2803
2804/**
2805 * ice_vc_set_dvm_caps - set VLAN capabilities when the device is in DVM
2806 * @vf: VF that capabilities are being set for
2807 * @caps: VLAN capabilities to populate
2808 *
2809 * Determine VLAN capabilities support based on whether a port VLAN is
2810 * configured. If a port VLAN is configured then the VF should use the inner
2811 * filtering/offload capabilities since the port VLAN is using the outer VLAN
2812 * capabilies.
2813 */
2814static void
2815ice_vc_set_dvm_caps(struct ice_vf *vf, struct virtchnl_vlan_caps *caps)
2816{
2817	struct virtchnl_vlan_supported_caps *supported_caps;
2818
2819	if (ice_vf_outer_vlan_not_allowed(vf)) {
2820		/* until support for inner VLAN filtering is added when a port
2821		 * VLAN is configured, only support software offloaded inner
2822		 * VLANs when a port VLAN is confgured in DVM
2823		 */
2824		supported_caps = &caps->filtering.filtering_support;
2825		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2826
2827		supported_caps = &caps->offloads.stripping_support;
2828		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2829					VIRTCHNL_VLAN_TOGGLE |
2830					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2831		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2832
2833		supported_caps = &caps->offloads.insertion_support;
2834		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2835					VIRTCHNL_VLAN_TOGGLE |
2836					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2837		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2838
2839		caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
2840		caps->offloads.ethertype_match =
2841			VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
2842	} else {
2843		supported_caps = &caps->filtering.filtering_support;
2844		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2845		supported_caps->outer = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2846					VIRTCHNL_VLAN_ETHERTYPE_88A8 |
2847					VIRTCHNL_VLAN_ETHERTYPE_9100 |
2848					VIRTCHNL_VLAN_ETHERTYPE_AND;
2849		caps->filtering.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2850						 VIRTCHNL_VLAN_ETHERTYPE_88A8 |
2851						 VIRTCHNL_VLAN_ETHERTYPE_9100;
2852
2853		supported_caps = &caps->offloads.stripping_support;
2854		supported_caps->inner = VIRTCHNL_VLAN_TOGGLE |
2855					VIRTCHNL_VLAN_ETHERTYPE_8100 |
2856					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2857		supported_caps->outer = VIRTCHNL_VLAN_TOGGLE |
2858					VIRTCHNL_VLAN_ETHERTYPE_8100 |
2859					VIRTCHNL_VLAN_ETHERTYPE_88A8 |
2860					VIRTCHNL_VLAN_ETHERTYPE_9100 |
2861					VIRTCHNL_VLAN_ETHERTYPE_XOR |
2862					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2;
2863
2864		supported_caps = &caps->offloads.insertion_support;
2865		supported_caps->inner = VIRTCHNL_VLAN_TOGGLE |
2866					VIRTCHNL_VLAN_ETHERTYPE_8100 |
2867					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2868		supported_caps->outer = VIRTCHNL_VLAN_TOGGLE |
2869					VIRTCHNL_VLAN_ETHERTYPE_8100 |
2870					VIRTCHNL_VLAN_ETHERTYPE_88A8 |
2871					VIRTCHNL_VLAN_ETHERTYPE_9100 |
2872					VIRTCHNL_VLAN_ETHERTYPE_XOR |
2873					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2;
2874
2875		caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
2876
2877		caps->offloads.ethertype_match =
2878			VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
2879	}
2880
2881	caps->filtering.max_filters = ice_vc_get_max_vlan_fltrs(vf);
2882}
2883
2884/**
2885 * ice_vc_set_svm_caps - set VLAN capabilities when the device is in SVM
2886 * @vf: VF that capabilities are being set for
2887 * @caps: VLAN capabilities to populate
2888 *
2889 * Determine VLAN capabilities support based on whether a port VLAN is
2890 * configured. If a port VLAN is configured then the VF does not have any VLAN
2891 * filtering or offload capabilities since the port VLAN is using the inner VLAN
2892 * capabilities in single VLAN mode (SVM). Otherwise allow the VF to use inner
2893 * VLAN fitlering and offload capabilities.
2894 */
2895static void
2896ice_vc_set_svm_caps(struct ice_vf *vf, struct virtchnl_vlan_caps *caps)
2897{
2898	struct virtchnl_vlan_supported_caps *supported_caps;
2899
2900	if (ice_vf_is_port_vlan_ena(vf)) {
2901		supported_caps = &caps->filtering.filtering_support;
2902		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2903		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2904
2905		supported_caps = &caps->offloads.stripping_support;
2906		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2907		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2908
2909		supported_caps = &caps->offloads.insertion_support;
2910		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2911		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2912
2913		caps->offloads.ethertype_init = VIRTCHNL_VLAN_UNSUPPORTED;
2914		caps->offloads.ethertype_match = VIRTCHNL_VLAN_UNSUPPORTED;
2915		caps->filtering.max_filters = 0;
2916	} else {
2917		supported_caps = &caps->filtering.filtering_support;
2918		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100;
2919		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2920		caps->filtering.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
2921
2922		supported_caps = &caps->offloads.stripping_support;
2923		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2924					VIRTCHNL_VLAN_TOGGLE |
2925					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2926		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2927
2928		supported_caps = &caps->offloads.insertion_support;
2929		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2930					VIRTCHNL_VLAN_TOGGLE |
2931					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2932		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2933
2934		caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
2935		caps->offloads.ethertype_match =
2936			VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
2937		caps->filtering.max_filters = ice_vc_get_max_vlan_fltrs(vf);
2938	}
2939}
2940
2941/**
2942 * ice_vc_get_offload_vlan_v2_caps - determine VF's VLAN capabilities
2943 * @vf: VF to determine VLAN capabilities for
2944 *
2945 * This will only be called if the VF and PF successfully negotiated
2946 * VIRTCHNL_VF_OFFLOAD_VLAN_V2.
2947 *
2948 * Set VLAN capabilities based on the current VLAN mode and whether a port VLAN
2949 * is configured or not.
2950 */
2951static int ice_vc_get_offload_vlan_v2_caps(struct ice_vf *vf)
2952{
2953	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2954	struct virtchnl_vlan_caps *caps = NULL;
2955	int err, len = 0;
2956
2957	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2958		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2959		goto out;
2960	}
2961
2962	caps = kzalloc(sizeof(*caps), GFP_KERNEL);
2963	if (!caps) {
2964		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2965		goto out;
2966	}
2967	len = sizeof(*caps);
2968
2969	if (ice_is_dvm_ena(&vf->pf->hw))
2970		ice_vc_set_dvm_caps(vf, caps);
2971	else
2972		ice_vc_set_svm_caps(vf, caps);
2973
2974	/* store negotiated caps to prevent invalid VF messages */
2975	memcpy(&vf->vlan_v2_caps, caps, sizeof(*caps));
2976
2977out:
2978	err = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS,
2979				    v_ret, (u8 *)caps, len);
2980	kfree(caps);
2981	return err;
2982}
2983
2984/**
2985 * ice_vc_validate_vlan_tpid - validate VLAN TPID
2986 * @filtering_caps: negotiated/supported VLAN filtering capabilities
2987 * @tpid: VLAN TPID used for validation
2988 *
2989 * Convert the VLAN TPID to a VIRTCHNL_VLAN_ETHERTYPE_* and then compare against
2990 * the negotiated/supported filtering caps to see if the VLAN TPID is valid.
2991 */
2992static bool ice_vc_validate_vlan_tpid(u16 filtering_caps, u16 tpid)
2993{
2994	enum virtchnl_vlan_support vlan_ethertype = VIRTCHNL_VLAN_UNSUPPORTED;
2995
2996	switch (tpid) {
2997	case ETH_P_8021Q:
2998		vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_8100;
2999		break;
3000	case ETH_P_8021AD:
3001		vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_88A8;
3002		break;
3003	case ETH_P_QINQ1:
3004		vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_9100;
3005		break;
3006	}
3007
3008	if (!(filtering_caps & vlan_ethertype))
3009		return false;
3010
3011	return true;
3012}
3013
3014/**
3015 * ice_vc_is_valid_vlan - validate the virtchnl_vlan
3016 * @vc_vlan: virtchnl_vlan to validate
3017 *
3018 * If the VLAN TCI and VLAN TPID are 0, then this filter is invalid, so return
3019 * false. Otherwise return true.
3020 */
3021static bool ice_vc_is_valid_vlan(struct virtchnl_vlan *vc_vlan)
3022{
3023	if (!vc_vlan->tci || !vc_vlan->tpid)
3024		return false;
3025
3026	return true;
3027}
3028
3029/**
3030 * ice_vc_validate_vlan_filter_list - validate the filter list from the VF
3031 * @vfc: negotiated/supported VLAN filtering capabilities
3032 * @vfl: VLAN filter list from VF to validate
3033 *
3034 * Validate all of the filters in the VLAN filter list from the VF. If any of
3035 * the checks fail then return false. Otherwise return true.
3036 */
3037static bool
3038ice_vc_validate_vlan_filter_list(struct virtchnl_vlan_filtering_caps *vfc,
3039				 struct virtchnl_vlan_filter_list_v2 *vfl)
3040{
3041	u16 i;
3042
3043	if (!vfl->num_elements)
3044		return false;
3045
3046	for (i = 0; i < vfl->num_elements; i++) {
3047		struct virtchnl_vlan_supported_caps *filtering_support =
3048			&vfc->filtering_support;
3049		struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
3050		struct virtchnl_vlan *outer = &vlan_fltr->outer;
3051		struct virtchnl_vlan *inner = &vlan_fltr->inner;
3052
3053		if ((ice_vc_is_valid_vlan(outer) &&
3054		     filtering_support->outer == VIRTCHNL_VLAN_UNSUPPORTED) ||
3055		    (ice_vc_is_valid_vlan(inner) &&
3056		     filtering_support->inner == VIRTCHNL_VLAN_UNSUPPORTED))
3057			return false;
3058
3059		if ((outer->tci_mask &&
3060		     !(filtering_support->outer & VIRTCHNL_VLAN_FILTER_MASK)) ||
3061		    (inner->tci_mask &&
3062		     !(filtering_support->inner & VIRTCHNL_VLAN_FILTER_MASK)))
3063			return false;
3064
3065		if (((outer->tci & VLAN_PRIO_MASK) &&
3066		     !(filtering_support->outer & VIRTCHNL_VLAN_PRIO)) ||
3067		    ((inner->tci & VLAN_PRIO_MASK) &&
3068		     !(filtering_support->inner & VIRTCHNL_VLAN_PRIO)))
3069			return false;
3070
3071		if ((ice_vc_is_valid_vlan(outer) &&
3072		     !ice_vc_validate_vlan_tpid(filtering_support->outer,
3073						outer->tpid)) ||
3074		    (ice_vc_is_valid_vlan(inner) &&
3075		     !ice_vc_validate_vlan_tpid(filtering_support->inner,
3076						inner->tpid)))
3077			return false;
3078	}
3079
3080	return true;
3081}
3082
3083/**
3084 * ice_vc_to_vlan - transform from struct virtchnl_vlan to struct ice_vlan
3085 * @vc_vlan: struct virtchnl_vlan to transform
3086 */
3087static struct ice_vlan ice_vc_to_vlan(struct virtchnl_vlan *vc_vlan)
3088{
3089	struct ice_vlan vlan = { 0 };
3090
3091	vlan.prio = FIELD_GET(VLAN_PRIO_MASK, vc_vlan->tci);
3092	vlan.vid = vc_vlan->tci & VLAN_VID_MASK;
3093	vlan.tpid = vc_vlan->tpid;
3094
3095	return vlan;
3096}
3097
3098/**
3099 * ice_vc_vlan_action - action to perform on the virthcnl_vlan
3100 * @vsi: VF's VSI used to perform the action
3101 * @vlan_action: function to perform the action with (i.e. add/del)
3102 * @vlan: VLAN filter to perform the action with
3103 */
3104static int
3105ice_vc_vlan_action(struct ice_vsi *vsi,
3106		   int (*vlan_action)(struct ice_vsi *, struct ice_vlan *),
3107		   struct ice_vlan *vlan)
3108{
3109	int err;
3110
3111	err = vlan_action(vsi, vlan);
3112	if (err)
3113		return err;
3114
3115	return 0;
3116}
3117
3118/**
3119 * ice_vc_del_vlans - delete VLAN(s) from the virtchnl filter list
3120 * @vf: VF used to delete the VLAN(s)
3121 * @vsi: VF's VSI used to delete the VLAN(s)
3122 * @vfl: virthchnl filter list used to delete the filters
3123 */
3124static int
3125ice_vc_del_vlans(struct ice_vf *vf, struct ice_vsi *vsi,
3126		 struct virtchnl_vlan_filter_list_v2 *vfl)
3127{
3128	bool vlan_promisc = ice_is_vlan_promisc_allowed(vf);
3129	int err;
3130	u16 i;
3131
3132	for (i = 0; i < vfl->num_elements; i++) {
3133		struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
3134		struct virtchnl_vlan *vc_vlan;
3135
3136		vc_vlan = &vlan_fltr->outer;
3137		if (ice_vc_is_valid_vlan(vc_vlan)) {
3138			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3139
3140			err = ice_vc_vlan_action(vsi,
3141						 vsi->outer_vlan_ops.del_vlan,
3142						 &vlan);
3143			if (err)
3144				return err;
3145
3146			if (vlan_promisc)
3147				ice_vf_dis_vlan_promisc(vsi, &vlan);
3148
3149			/* Disable VLAN filtering when only VLAN 0 is left */
3150			if (!ice_vsi_has_non_zero_vlans(vsi) && ice_is_dvm_ena(&vsi->back->hw)) {
3151				err = vsi->outer_vlan_ops.dis_tx_filtering(vsi);
3152				if (err)
3153					return err;
3154			}
3155		}
3156
3157		vc_vlan = &vlan_fltr->inner;
3158		if (ice_vc_is_valid_vlan(vc_vlan)) {
3159			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3160
3161			err = ice_vc_vlan_action(vsi,
3162						 vsi->inner_vlan_ops.del_vlan,
3163						 &vlan);
3164			if (err)
3165				return err;
3166
3167			/* no support for VLAN promiscuous on inner VLAN unless
3168			 * we are in Single VLAN Mode (SVM)
3169			 */
3170			if (!ice_is_dvm_ena(&vsi->back->hw)) {
3171				if (vlan_promisc)
3172					ice_vf_dis_vlan_promisc(vsi, &vlan);
3173
3174				/* Disable VLAN filtering when only VLAN 0 is left */
3175				if (!ice_vsi_has_non_zero_vlans(vsi)) {
3176					err = vsi->inner_vlan_ops.dis_tx_filtering(vsi);
3177					if (err)
3178						return err;
3179				}
3180			}
3181		}
3182	}
3183
3184	return 0;
3185}
3186
3187/**
3188 * ice_vc_remove_vlan_v2_msg - virtchnl handler for VIRTCHNL_OP_DEL_VLAN_V2
3189 * @vf: VF the message was received from
3190 * @msg: message received from the VF
3191 */
3192static int ice_vc_remove_vlan_v2_msg(struct ice_vf *vf, u8 *msg)
3193{
3194	struct virtchnl_vlan_filter_list_v2 *vfl =
3195		(struct virtchnl_vlan_filter_list_v2 *)msg;
3196	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3197	struct ice_vsi *vsi;
3198
3199	if (!ice_vc_validate_vlan_filter_list(&vf->vlan_v2_caps.filtering,
3200					      vfl)) {
3201		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3202		goto out;
3203	}
3204
3205	if (!ice_vc_isvalid_vsi_id(vf, vfl->vport_id)) {
3206		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3207		goto out;
3208	}
3209
3210	vsi = ice_get_vf_vsi(vf);
3211	if (!vsi) {
3212		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3213		goto out;
3214	}
3215
3216	if (ice_vc_del_vlans(vf, vsi, vfl))
3217		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3218
3219out:
3220	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN_V2, v_ret, NULL,
3221				     0);
3222}
3223
3224/**
3225 * ice_vc_add_vlans - add VLAN(s) from the virtchnl filter list
3226 * @vf: VF used to add the VLAN(s)
3227 * @vsi: VF's VSI used to add the VLAN(s)
3228 * @vfl: virthchnl filter list used to add the filters
3229 */
3230static int
3231ice_vc_add_vlans(struct ice_vf *vf, struct ice_vsi *vsi,
3232		 struct virtchnl_vlan_filter_list_v2 *vfl)
3233{
3234	bool vlan_promisc = ice_is_vlan_promisc_allowed(vf);
3235	int err;
3236	u16 i;
3237
3238	for (i = 0; i < vfl->num_elements; i++) {
3239		struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
3240		struct virtchnl_vlan *vc_vlan;
3241
3242		vc_vlan = &vlan_fltr->outer;
3243		if (ice_vc_is_valid_vlan(vc_vlan)) {
3244			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3245
3246			err = ice_vc_vlan_action(vsi,
3247						 vsi->outer_vlan_ops.add_vlan,
3248						 &vlan);
3249			if (err)
3250				return err;
3251
3252			if (vlan_promisc) {
3253				err = ice_vf_ena_vlan_promisc(vsi, &vlan);
3254				if (err)
3255					return err;
3256			}
3257
3258			/* Enable VLAN filtering on first non-zero VLAN */
3259			if (vf->spoofchk && vlan.vid && ice_is_dvm_ena(&vsi->back->hw)) {
3260				err = vsi->outer_vlan_ops.ena_tx_filtering(vsi);
3261				if (err)
3262					return err;
3263			}
3264		}
3265
3266		vc_vlan = &vlan_fltr->inner;
3267		if (ice_vc_is_valid_vlan(vc_vlan)) {
3268			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3269
3270			err = ice_vc_vlan_action(vsi,
3271						 vsi->inner_vlan_ops.add_vlan,
3272						 &vlan);
3273			if (err)
3274				return err;
3275
3276			/* no support for VLAN promiscuous on inner VLAN unless
3277			 * we are in Single VLAN Mode (SVM)
3278			 */
3279			if (!ice_is_dvm_ena(&vsi->back->hw)) {
3280				if (vlan_promisc) {
3281					err = ice_vf_ena_vlan_promisc(vsi, &vlan);
3282					if (err)
3283						return err;
3284				}
3285
3286				/* Enable VLAN filtering on first non-zero VLAN */
3287				if (vf->spoofchk && vlan.vid) {
3288					err = vsi->inner_vlan_ops.ena_tx_filtering(vsi);
3289					if (err)
3290						return err;
3291				}
3292			}
3293		}
3294	}
3295
3296	return 0;
3297}
3298
3299/**
3300 * ice_vc_validate_add_vlan_filter_list - validate add filter list from the VF
3301 * @vsi: VF VSI used to get number of existing VLAN filters
3302 * @vfc: negotiated/supported VLAN filtering capabilities
3303 * @vfl: VLAN filter list from VF to validate
3304 *
3305 * Validate all of the filters in the VLAN filter list from the VF during the
3306 * VIRTCHNL_OP_ADD_VLAN_V2 opcode. If any of the checks fail then return false.
3307 * Otherwise return true.
3308 */
3309static bool
3310ice_vc_validate_add_vlan_filter_list(struct ice_vsi *vsi,
3311				     struct virtchnl_vlan_filtering_caps *vfc,
3312				     struct virtchnl_vlan_filter_list_v2 *vfl)
3313{
3314	u16 num_requested_filters = ice_vsi_num_non_zero_vlans(vsi) +
3315		vfl->num_elements;
3316
3317	if (num_requested_filters > vfc->max_filters)
3318		return false;
3319
3320	return ice_vc_validate_vlan_filter_list(vfc, vfl);
3321}
3322
3323/**
3324 * ice_vc_add_vlan_v2_msg - virtchnl handler for VIRTCHNL_OP_ADD_VLAN_V2
3325 * @vf: VF the message was received from
3326 * @msg: message received from the VF
3327 */
3328static int ice_vc_add_vlan_v2_msg(struct ice_vf *vf, u8 *msg)
3329{
3330	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3331	struct virtchnl_vlan_filter_list_v2 *vfl =
3332		(struct virtchnl_vlan_filter_list_v2 *)msg;
3333	struct ice_vsi *vsi;
3334
3335	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3336		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3337		goto out;
3338	}
3339
3340	if (!ice_vc_isvalid_vsi_id(vf, vfl->vport_id)) {
3341		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3342		goto out;
3343	}
3344
3345	vsi = ice_get_vf_vsi(vf);
3346	if (!vsi) {
3347		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3348		goto out;
3349	}
3350
3351	if (!ice_vc_validate_add_vlan_filter_list(vsi,
3352						  &vf->vlan_v2_caps.filtering,
3353						  vfl)) {
3354		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3355		goto out;
3356	}
3357
3358	if (ice_vc_add_vlans(vf, vsi, vfl))
3359		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3360
3361out:
3362	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN_V2, v_ret, NULL,
3363				     0);
3364}
3365
3366/**
3367 * ice_vc_valid_vlan_setting - validate VLAN setting
3368 * @negotiated_settings: negotiated VLAN settings during VF init
3369 * @ethertype_setting: ethertype(s) requested for the VLAN setting
3370 */
3371static bool
3372ice_vc_valid_vlan_setting(u32 negotiated_settings, u32 ethertype_setting)
3373{
3374	if (ethertype_setting && !(negotiated_settings & ethertype_setting))
3375		return false;
3376
3377	/* only allow a single VIRTCHNL_VLAN_ETHERTYPE if
3378	 * VIRTHCNL_VLAN_ETHERTYPE_AND is not negotiated/supported
3379	 */
3380	if (!(negotiated_settings & VIRTCHNL_VLAN_ETHERTYPE_AND) &&
3381	    hweight32(ethertype_setting) > 1)
3382		return false;
3383
3384	/* ability to modify the VLAN setting was not negotiated */
3385	if (!(negotiated_settings & VIRTCHNL_VLAN_TOGGLE))
3386		return false;
3387
3388	return true;
3389}
3390
3391/**
3392 * ice_vc_valid_vlan_setting_msg - validate the VLAN setting message
3393 * @caps: negotiated VLAN settings during VF init
3394 * @msg: message to validate
3395 *
3396 * Used to validate any VLAN virtchnl message sent as a
3397 * virtchnl_vlan_setting structure. Validates the message against the
3398 * negotiated/supported caps during VF driver init.
3399 */
3400static bool
3401ice_vc_valid_vlan_setting_msg(struct virtchnl_vlan_supported_caps *caps,
3402			      struct virtchnl_vlan_setting *msg)
3403{
3404	if ((!msg->outer_ethertype_setting &&
3405	     !msg->inner_ethertype_setting) ||
3406	    (!caps->outer && !caps->inner))
3407		return false;
3408
3409	if (msg->outer_ethertype_setting &&
3410	    !ice_vc_valid_vlan_setting(caps->outer,
3411				       msg->outer_ethertype_setting))
3412		return false;
3413
3414	if (msg->inner_ethertype_setting &&
3415	    !ice_vc_valid_vlan_setting(caps->inner,
3416				       msg->inner_ethertype_setting))
3417		return false;
3418
3419	return true;
3420}
3421
3422/**
3423 * ice_vc_get_tpid - transform from VIRTCHNL_VLAN_ETHERTYPE_* to VLAN TPID
3424 * @ethertype_setting: VIRTCHNL_VLAN_ETHERTYPE_* used to get VLAN TPID
3425 * @tpid: VLAN TPID to populate
3426 */
3427static int ice_vc_get_tpid(u32 ethertype_setting, u16 *tpid)
3428{
3429	switch (ethertype_setting) {
3430	case VIRTCHNL_VLAN_ETHERTYPE_8100:
3431		*tpid = ETH_P_8021Q;
3432		break;
3433	case VIRTCHNL_VLAN_ETHERTYPE_88A8:
3434		*tpid = ETH_P_8021AD;
3435		break;
3436	case VIRTCHNL_VLAN_ETHERTYPE_9100:
3437		*tpid = ETH_P_QINQ1;
3438		break;
3439	default:
3440		*tpid = 0;
3441		return -EINVAL;
3442	}
3443
3444	return 0;
3445}
3446
3447/**
3448 * ice_vc_ena_vlan_offload - enable VLAN offload based on the ethertype_setting
3449 * @vsi: VF's VSI used to enable the VLAN offload
3450 * @ena_offload: function used to enable the VLAN offload
3451 * @ethertype_setting: VIRTCHNL_VLAN_ETHERTYPE_* to enable offloads for
3452 */
3453static int
3454ice_vc_ena_vlan_offload(struct ice_vsi *vsi,
3455			int (*ena_offload)(struct ice_vsi *vsi, u16 tpid),
3456			u32 ethertype_setting)
3457{
3458	u16 tpid;
3459	int err;
3460
3461	err = ice_vc_get_tpid(ethertype_setting, &tpid);
3462	if (err)
3463		return err;
3464
3465	err = ena_offload(vsi, tpid);
3466	if (err)
3467		return err;
3468
3469	return 0;
3470}
3471
3472#define ICE_L2TSEL_QRX_CONTEXT_REG_IDX	3
3473#define ICE_L2TSEL_BIT_OFFSET		23
3474enum ice_l2tsel {
3475	ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND,
3476	ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG1,
3477};
3478
3479/**
3480 * ice_vsi_update_l2tsel - update l2tsel field for all Rx rings on this VSI
3481 * @vsi: VSI used to update l2tsel on
3482 * @l2tsel: l2tsel setting requested
3483 *
3484 * Use the l2tsel setting to update all of the Rx queue context bits for l2tsel.
3485 * This will modify which descriptor field the first offloaded VLAN will be
3486 * stripped into.
3487 */
3488static void ice_vsi_update_l2tsel(struct ice_vsi *vsi, enum ice_l2tsel l2tsel)
3489{
3490	struct ice_hw *hw = &vsi->back->hw;
3491	u32 l2tsel_bit;
3492	int i;
3493
3494	if (l2tsel == ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND)
3495		l2tsel_bit = 0;
3496	else
3497		l2tsel_bit = BIT(ICE_L2TSEL_BIT_OFFSET);
3498
3499	for (i = 0; i < vsi->alloc_rxq; i++) {
3500		u16 pfq = vsi->rxq_map[i];
3501		u32 qrx_context_offset;
3502		u32 regval;
3503
3504		qrx_context_offset =
3505			QRX_CONTEXT(ICE_L2TSEL_QRX_CONTEXT_REG_IDX, pfq);
3506
3507		regval = rd32(hw, qrx_context_offset);
3508		regval &= ~BIT(ICE_L2TSEL_BIT_OFFSET);
3509		regval |= l2tsel_bit;
3510		wr32(hw, qrx_context_offset, regval);
3511	}
3512}
3513
3514/**
3515 * ice_vc_ena_vlan_stripping_v2_msg
3516 * @vf: VF the message was received from
3517 * @msg: message received from the VF
3518 *
3519 * virthcnl handler for VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2
3520 */
3521static int ice_vc_ena_vlan_stripping_v2_msg(struct ice_vf *vf, u8 *msg)
3522{
3523	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3524	struct virtchnl_vlan_supported_caps *stripping_support;
3525	struct virtchnl_vlan_setting *strip_msg =
3526		(struct virtchnl_vlan_setting *)msg;
3527	u32 ethertype_setting;
3528	struct ice_vsi *vsi;
3529
3530	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3531		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3532		goto out;
3533	}
3534
3535	if (!ice_vc_isvalid_vsi_id(vf, strip_msg->vport_id)) {
3536		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3537		goto out;
3538	}
3539
3540	vsi = ice_get_vf_vsi(vf);
3541	if (!vsi) {
3542		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3543		goto out;
3544	}
3545
3546	stripping_support = &vf->vlan_v2_caps.offloads.stripping_support;
3547	if (!ice_vc_valid_vlan_setting_msg(stripping_support, strip_msg)) {
3548		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3549		goto out;
3550	}
3551
3552	if (ice_vsi_is_rxq_crc_strip_dis(vsi)) {
3553		v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
3554		goto out;
3555	}
3556
3557	ethertype_setting = strip_msg->outer_ethertype_setting;
3558	if (ethertype_setting) {
3559		if (ice_vc_ena_vlan_offload(vsi,
3560					    vsi->outer_vlan_ops.ena_stripping,
3561					    ethertype_setting)) {
3562			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3563			goto out;
3564		} else {
3565			enum ice_l2tsel l2tsel =
3566				ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND;
3567
3568			/* PF tells the VF that the outer VLAN tag is always
3569			 * extracted to VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2 and
3570			 * inner is always extracted to
3571			 * VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1. This is needed to
3572			 * support outer stripping so the first tag always ends
3573			 * up in L2TAG2_2ND and the second/inner tag, if
3574			 * enabled, is extracted in L2TAG1.
3575			 */
3576			ice_vsi_update_l2tsel(vsi, l2tsel);
3577
3578			vf->vlan_strip_ena |= ICE_OUTER_VLAN_STRIP_ENA;
3579		}
3580	}
3581
3582	ethertype_setting = strip_msg->inner_ethertype_setting;
3583	if (ethertype_setting &&
3584	    ice_vc_ena_vlan_offload(vsi, vsi->inner_vlan_ops.ena_stripping,
3585				    ethertype_setting)) {
3586		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3587		goto out;
3588	}
3589
3590	if (ethertype_setting)
3591		vf->vlan_strip_ena |= ICE_INNER_VLAN_STRIP_ENA;
3592
3593out:
3594	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2,
3595				     v_ret, NULL, 0);
3596}
3597
3598/**
3599 * ice_vc_dis_vlan_stripping_v2_msg
3600 * @vf: VF the message was received from
3601 * @msg: message received from the VF
3602 *
3603 * virthcnl handler for VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2
3604 */
3605static int ice_vc_dis_vlan_stripping_v2_msg(struct ice_vf *vf, u8 *msg)
3606{
3607	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3608	struct virtchnl_vlan_supported_caps *stripping_support;
3609	struct virtchnl_vlan_setting *strip_msg =
3610		(struct virtchnl_vlan_setting *)msg;
3611	u32 ethertype_setting;
3612	struct ice_vsi *vsi;
3613
3614	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3615		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3616		goto out;
3617	}
3618
3619	if (!ice_vc_isvalid_vsi_id(vf, strip_msg->vport_id)) {
3620		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3621		goto out;
3622	}
3623
3624	vsi = ice_get_vf_vsi(vf);
3625	if (!vsi) {
3626		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3627		goto out;
3628	}
3629
3630	stripping_support = &vf->vlan_v2_caps.offloads.stripping_support;
3631	if (!ice_vc_valid_vlan_setting_msg(stripping_support, strip_msg)) {
3632		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3633		goto out;
3634	}
3635
3636	ethertype_setting = strip_msg->outer_ethertype_setting;
3637	if (ethertype_setting) {
3638		if (vsi->outer_vlan_ops.dis_stripping(vsi)) {
3639			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3640			goto out;
3641		} else {
3642			enum ice_l2tsel l2tsel =
3643				ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG1;
3644
3645			/* PF tells the VF that the outer VLAN tag is always
3646			 * extracted to VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2 and
3647			 * inner is always extracted to
3648			 * VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1. This is needed to
3649			 * support inner stripping while outer stripping is
3650			 * disabled so that the first and only tag is extracted
3651			 * in L2TAG1.
3652			 */
3653			ice_vsi_update_l2tsel(vsi, l2tsel);
3654
3655			vf->vlan_strip_ena &= ~ICE_OUTER_VLAN_STRIP_ENA;
3656		}
3657	}
3658
3659	ethertype_setting = strip_msg->inner_ethertype_setting;
3660	if (ethertype_setting && vsi->inner_vlan_ops.dis_stripping(vsi)) {
3661		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3662		goto out;
3663	}
3664
3665	if (ethertype_setting)
3666		vf->vlan_strip_ena &= ~ICE_INNER_VLAN_STRIP_ENA;
3667
3668out:
3669	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2,
3670				     v_ret, NULL, 0);
3671}
3672
3673/**
3674 * ice_vc_ena_vlan_insertion_v2_msg
3675 * @vf: VF the message was received from
3676 * @msg: message received from the VF
3677 *
3678 * virthcnl handler for VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2
3679 */
3680static int ice_vc_ena_vlan_insertion_v2_msg(struct ice_vf *vf, u8 *msg)
3681{
3682	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3683	struct virtchnl_vlan_supported_caps *insertion_support;
3684	struct virtchnl_vlan_setting *insertion_msg =
3685		(struct virtchnl_vlan_setting *)msg;
3686	u32 ethertype_setting;
3687	struct ice_vsi *vsi;
3688
3689	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3690		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3691		goto out;
3692	}
3693
3694	if (!ice_vc_isvalid_vsi_id(vf, insertion_msg->vport_id)) {
3695		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3696		goto out;
3697	}
3698
3699	vsi = ice_get_vf_vsi(vf);
3700	if (!vsi) {
3701		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3702		goto out;
3703	}
3704
3705	insertion_support = &vf->vlan_v2_caps.offloads.insertion_support;
3706	if (!ice_vc_valid_vlan_setting_msg(insertion_support, insertion_msg)) {
3707		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3708		goto out;
3709	}
3710
3711	ethertype_setting = insertion_msg->outer_ethertype_setting;
3712	if (ethertype_setting &&
3713	    ice_vc_ena_vlan_offload(vsi, vsi->outer_vlan_ops.ena_insertion,
3714				    ethertype_setting)) {
3715		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3716		goto out;
3717	}
3718
3719	ethertype_setting = insertion_msg->inner_ethertype_setting;
3720	if (ethertype_setting &&
3721	    ice_vc_ena_vlan_offload(vsi, vsi->inner_vlan_ops.ena_insertion,
3722				    ethertype_setting)) {
3723		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3724		goto out;
3725	}
3726
3727out:
3728	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2,
3729				     v_ret, NULL, 0);
3730}
3731
3732/**
3733 * ice_vc_dis_vlan_insertion_v2_msg
3734 * @vf: VF the message was received from
3735 * @msg: message received from the VF
3736 *
3737 * virthcnl handler for VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2
3738 */
3739static int ice_vc_dis_vlan_insertion_v2_msg(struct ice_vf *vf, u8 *msg)
3740{
3741	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3742	struct virtchnl_vlan_supported_caps *insertion_support;
3743	struct virtchnl_vlan_setting *insertion_msg =
3744		(struct virtchnl_vlan_setting *)msg;
3745	u32 ethertype_setting;
3746	struct ice_vsi *vsi;
3747
3748	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3749		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3750		goto out;
3751	}
3752
3753	if (!ice_vc_isvalid_vsi_id(vf, insertion_msg->vport_id)) {
3754		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3755		goto out;
3756	}
3757
3758	vsi = ice_get_vf_vsi(vf);
3759	if (!vsi) {
3760		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3761		goto out;
3762	}
3763
3764	insertion_support = &vf->vlan_v2_caps.offloads.insertion_support;
3765	if (!ice_vc_valid_vlan_setting_msg(insertion_support, insertion_msg)) {
3766		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3767		goto out;
3768	}
3769
3770	ethertype_setting = insertion_msg->outer_ethertype_setting;
3771	if (ethertype_setting && vsi->outer_vlan_ops.dis_insertion(vsi)) {
3772		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3773		goto out;
3774	}
3775
3776	ethertype_setting = insertion_msg->inner_ethertype_setting;
3777	if (ethertype_setting && vsi->inner_vlan_ops.dis_insertion(vsi)) {
3778		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3779		goto out;
3780	}
3781
3782out:
3783	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2,
3784				     v_ret, NULL, 0);
3785}
3786
3787static const struct ice_virtchnl_ops ice_virtchnl_dflt_ops = {
3788	.get_ver_msg = ice_vc_get_ver_msg,
3789	.get_vf_res_msg = ice_vc_get_vf_res_msg,
3790	.reset_vf = ice_vc_reset_vf_msg,
3791	.add_mac_addr_msg = ice_vc_add_mac_addr_msg,
3792	.del_mac_addr_msg = ice_vc_del_mac_addr_msg,
3793	.cfg_qs_msg = ice_vc_cfg_qs_msg,
3794	.ena_qs_msg = ice_vc_ena_qs_msg,
3795	.dis_qs_msg = ice_vc_dis_qs_msg,
3796	.request_qs_msg = ice_vc_request_qs_msg,
3797	.cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
3798	.config_rss_key = ice_vc_config_rss_key,
3799	.config_rss_lut = ice_vc_config_rss_lut,
3800	.config_rss_hfunc = ice_vc_config_rss_hfunc,
3801	.get_stats_msg = ice_vc_get_stats_msg,
3802	.cfg_promiscuous_mode_msg = ice_vc_cfg_promiscuous_mode_msg,
3803	.add_vlan_msg = ice_vc_add_vlan_msg,
3804	.remove_vlan_msg = ice_vc_remove_vlan_msg,
3805	.query_rxdid = ice_vc_query_rxdid,
3806	.get_rss_hena = ice_vc_get_rss_hena,
3807	.set_rss_hena_msg = ice_vc_set_rss_hena,
3808	.ena_vlan_stripping = ice_vc_ena_vlan_stripping,
3809	.dis_vlan_stripping = ice_vc_dis_vlan_stripping,
3810	.handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
3811	.add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
3812	.del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
3813	.get_offload_vlan_v2_caps = ice_vc_get_offload_vlan_v2_caps,
3814	.add_vlan_v2_msg = ice_vc_add_vlan_v2_msg,
3815	.remove_vlan_v2_msg = ice_vc_remove_vlan_v2_msg,
3816	.ena_vlan_stripping_v2_msg = ice_vc_ena_vlan_stripping_v2_msg,
3817	.dis_vlan_stripping_v2_msg = ice_vc_dis_vlan_stripping_v2_msg,
3818	.ena_vlan_insertion_v2_msg = ice_vc_ena_vlan_insertion_v2_msg,
3819	.dis_vlan_insertion_v2_msg = ice_vc_dis_vlan_insertion_v2_msg,
3820};
3821
3822/**
3823 * ice_virtchnl_set_dflt_ops - Switch to default virtchnl ops
3824 * @vf: the VF to switch ops
3825 */
3826void ice_virtchnl_set_dflt_ops(struct ice_vf *vf)
3827{
3828	vf->virtchnl_ops = &ice_virtchnl_dflt_ops;
3829}
3830
3831/**
3832 * ice_vc_repr_add_mac
3833 * @vf: pointer to VF
3834 * @msg: virtchannel message
3835 *
3836 * When port representors are created, we do not add MAC rule
3837 * to firmware, we store it so that PF could report same
3838 * MAC as VF.
3839 */
3840static int ice_vc_repr_add_mac(struct ice_vf *vf, u8 *msg)
3841{
3842	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3843	struct virtchnl_ether_addr_list *al =
3844	    (struct virtchnl_ether_addr_list *)msg;
3845	struct ice_vsi *vsi;
3846	struct ice_pf *pf;
3847	int i;
3848
3849	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
3850	    !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
3851		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3852		goto handle_mac_exit;
3853	}
3854
3855	pf = vf->pf;
3856
3857	vsi = ice_get_vf_vsi(vf);
3858	if (!vsi) {
3859		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3860		goto handle_mac_exit;
3861	}
3862
3863	for (i = 0; i < al->num_elements; i++) {
3864		u8 *mac_addr = al->list[i].addr;
 
3865
3866		if (!is_unicast_ether_addr(mac_addr) ||
3867		    ether_addr_equal(mac_addr, vf->hw_lan_addr))
3868			continue;
3869
3870		if (vf->pf_set_mac) {
3871			dev_err(ice_pf_to_dev(pf), "VF attempting to override administratively set MAC address\n");
3872			v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
3873			goto handle_mac_exit;
3874		}
3875
 
 
 
 
 
 
 
3876		ice_vfhw_mac_add(vf, &al->list[i]);
3877		vf->num_mac++;
3878		break;
3879	}
3880
3881handle_mac_exit:
3882	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
3883				     v_ret, NULL, 0);
3884}
3885
3886/**
3887 * ice_vc_repr_del_mac - response with success for deleting MAC
3888 * @vf: pointer to VF
3889 * @msg: virtchannel message
3890 *
3891 * Respond with success to not break normal VF flow.
3892 * For legacy VF driver try to update cached MAC address.
3893 */
3894static int
3895ice_vc_repr_del_mac(struct ice_vf __always_unused *vf, u8 __always_unused *msg)
3896{
3897	struct virtchnl_ether_addr_list *al =
3898		(struct virtchnl_ether_addr_list *)msg;
3899
3900	ice_update_legacy_cached_mac(vf, &al->list[0]);
3901
3902	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
3903				     VIRTCHNL_STATUS_SUCCESS, NULL, 0);
3904}
3905
3906static int
3907ice_vc_repr_cfg_promiscuous_mode(struct ice_vf *vf, u8 __always_unused *msg)
3908{
3909	dev_dbg(ice_pf_to_dev(vf->pf),
3910		"Can't config promiscuous mode in switchdev mode for VF %d\n",
3911		vf->vf_id);
3912	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
3913				     VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
3914				     NULL, 0);
3915}
3916
3917static const struct ice_virtchnl_ops ice_virtchnl_repr_ops = {
3918	.get_ver_msg = ice_vc_get_ver_msg,
3919	.get_vf_res_msg = ice_vc_get_vf_res_msg,
3920	.reset_vf = ice_vc_reset_vf_msg,
3921	.add_mac_addr_msg = ice_vc_repr_add_mac,
3922	.del_mac_addr_msg = ice_vc_repr_del_mac,
3923	.cfg_qs_msg = ice_vc_cfg_qs_msg,
3924	.ena_qs_msg = ice_vc_ena_qs_msg,
3925	.dis_qs_msg = ice_vc_dis_qs_msg,
3926	.request_qs_msg = ice_vc_request_qs_msg,
3927	.cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
3928	.config_rss_key = ice_vc_config_rss_key,
3929	.config_rss_lut = ice_vc_config_rss_lut,
3930	.config_rss_hfunc = ice_vc_config_rss_hfunc,
3931	.get_stats_msg = ice_vc_get_stats_msg,
3932	.cfg_promiscuous_mode_msg = ice_vc_repr_cfg_promiscuous_mode,
3933	.add_vlan_msg = ice_vc_add_vlan_msg,
3934	.remove_vlan_msg = ice_vc_remove_vlan_msg,
3935	.query_rxdid = ice_vc_query_rxdid,
3936	.get_rss_hena = ice_vc_get_rss_hena,
3937	.set_rss_hena_msg = ice_vc_set_rss_hena,
3938	.ena_vlan_stripping = ice_vc_ena_vlan_stripping,
3939	.dis_vlan_stripping = ice_vc_dis_vlan_stripping,
3940	.handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
3941	.add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
3942	.del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
3943	.get_offload_vlan_v2_caps = ice_vc_get_offload_vlan_v2_caps,
3944	.add_vlan_v2_msg = ice_vc_add_vlan_v2_msg,
3945	.remove_vlan_v2_msg = ice_vc_remove_vlan_v2_msg,
3946	.ena_vlan_stripping_v2_msg = ice_vc_ena_vlan_stripping_v2_msg,
3947	.dis_vlan_stripping_v2_msg = ice_vc_dis_vlan_stripping_v2_msg,
3948	.ena_vlan_insertion_v2_msg = ice_vc_ena_vlan_insertion_v2_msg,
3949	.dis_vlan_insertion_v2_msg = ice_vc_dis_vlan_insertion_v2_msg,
3950};
3951
3952/**
3953 * ice_virtchnl_set_repr_ops - Switch to representor virtchnl ops
3954 * @vf: the VF to switch ops
3955 */
3956void ice_virtchnl_set_repr_ops(struct ice_vf *vf)
3957{
3958	vf->virtchnl_ops = &ice_virtchnl_repr_ops;
3959}
3960
3961/**
3962 * ice_is_malicious_vf - check if this vf might be overflowing mailbox
3963 * @vf: the VF to check
3964 * @mbxdata: data about the state of the mailbox
3965 *
3966 * Detect if a given VF might be malicious and attempting to overflow the PF
3967 * mailbox. If so, log a warning message and ignore this event.
3968 */
3969static bool
3970ice_is_malicious_vf(struct ice_vf *vf, struct ice_mbx_data *mbxdata)
3971{
3972	bool report_malvf = false;
3973	struct device *dev;
3974	struct ice_pf *pf;
3975	int status;
3976
3977	pf = vf->pf;
3978	dev = ice_pf_to_dev(pf);
3979
3980	if (test_bit(ICE_VF_STATE_DIS, vf->vf_states))
3981		return vf->mbx_info.malicious;
3982
3983	/* check to see if we have a newly malicious VF */
3984	status = ice_mbx_vf_state_handler(&pf->hw, mbxdata, &vf->mbx_info,
3985					  &report_malvf);
3986	if (status)
3987		dev_warn_ratelimited(dev, "Unable to check status of mailbox overflow for VF %u MAC %pM, status %d\n",
3988				     vf->vf_id, vf->dev_lan_addr, status);
3989
3990	if (report_malvf) {
3991		struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
3992		u8 zero_addr[ETH_ALEN] = {};
3993
3994		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",
3995			 vf->dev_lan_addr,
3996			 pf_vsi ? pf_vsi->netdev->dev_addr : zero_addr);
3997	}
3998
3999	return vf->mbx_info.malicious;
4000}
4001
4002/**
4003 * ice_vc_process_vf_msg - Process request from VF
4004 * @pf: pointer to the PF structure
4005 * @event: pointer to the AQ event
4006 * @mbxdata: information used to detect VF attempting mailbox overflow
4007 *
4008 * called from the common asq/arq handler to
4009 * process request from VF
4010 */
4011void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event,
4012			   struct ice_mbx_data *mbxdata)
4013{
4014	u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
4015	s16 vf_id = le16_to_cpu(event->desc.retval);
4016	const struct ice_virtchnl_ops *ops;
4017	u16 msglen = event->msg_len;
4018	u8 *msg = event->msg_buf;
4019	struct ice_vf *vf = NULL;
4020	struct device *dev;
4021	int err = 0;
4022
4023	dev = ice_pf_to_dev(pf);
4024
4025	vf = ice_get_vf_by_id(pf, vf_id);
4026	if (!vf) {
4027		dev_err(dev, "Unable to locate VF for message from VF ID %d, opcode %d, len %d\n",
4028			vf_id, v_opcode, msglen);
4029		return;
4030	}
4031
4032	mutex_lock(&vf->cfg_lock);
4033
4034	/* Check if the VF is trying to overflow the mailbox */
4035	if (ice_is_malicious_vf(vf, mbxdata))
4036		goto finish;
4037
4038	/* Check if VF is disabled. */
4039	if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
4040		err = -EPERM;
4041		goto error_handler;
4042	}
4043
4044	ops = vf->virtchnl_ops;
4045
4046	/* Perform basic checks on the msg */
4047	err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
4048	if (err) {
4049		if (err == VIRTCHNL_STATUS_ERR_PARAM)
4050			err = -EPERM;
4051		else
4052			err = -EINVAL;
4053	}
4054
4055error_handler:
4056	if (err) {
4057		ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
4058				      NULL, 0);
4059		dev_err(dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
4060			vf_id, v_opcode, msglen, err);
4061		goto finish;
4062	}
4063
4064	if (!ice_vc_is_opcode_allowed(vf, v_opcode)) {
4065		ice_vc_send_msg_to_vf(vf, v_opcode,
4066				      VIRTCHNL_STATUS_ERR_NOT_SUPPORTED, NULL,
4067				      0);
4068		goto finish;
4069	}
4070
4071	switch (v_opcode) {
4072	case VIRTCHNL_OP_VERSION:
4073		err = ops->get_ver_msg(vf, msg);
4074		break;
4075	case VIRTCHNL_OP_GET_VF_RESOURCES:
4076		err = ops->get_vf_res_msg(vf, msg);
4077		if (ice_vf_init_vlan_stripping(vf))
4078			dev_dbg(dev, "Failed to initialize VLAN stripping for VF %d\n",
4079				vf->vf_id);
4080		ice_vc_notify_vf_link_state(vf);
4081		break;
4082	case VIRTCHNL_OP_RESET_VF:
4083		ops->reset_vf(vf);
4084		break;
4085	case VIRTCHNL_OP_ADD_ETH_ADDR:
4086		err = ops->add_mac_addr_msg(vf, msg);
4087		break;
4088	case VIRTCHNL_OP_DEL_ETH_ADDR:
4089		err = ops->del_mac_addr_msg(vf, msg);
4090		break;
4091	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
4092		err = ops->cfg_qs_msg(vf, msg);
4093		break;
4094	case VIRTCHNL_OP_ENABLE_QUEUES:
4095		err = ops->ena_qs_msg(vf, msg);
4096		ice_vc_notify_vf_link_state(vf);
4097		break;
4098	case VIRTCHNL_OP_DISABLE_QUEUES:
4099		err = ops->dis_qs_msg(vf, msg);
4100		break;
4101	case VIRTCHNL_OP_REQUEST_QUEUES:
4102		err = ops->request_qs_msg(vf, msg);
4103		break;
4104	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
4105		err = ops->cfg_irq_map_msg(vf, msg);
4106		break;
4107	case VIRTCHNL_OP_CONFIG_RSS_KEY:
4108		err = ops->config_rss_key(vf, msg);
4109		break;
4110	case VIRTCHNL_OP_CONFIG_RSS_LUT:
4111		err = ops->config_rss_lut(vf, msg);
4112		break;
4113	case VIRTCHNL_OP_CONFIG_RSS_HFUNC:
4114		err = ops->config_rss_hfunc(vf, msg);
4115		break;
4116	case VIRTCHNL_OP_GET_STATS:
4117		err = ops->get_stats_msg(vf, msg);
4118		break;
4119	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
4120		err = ops->cfg_promiscuous_mode_msg(vf, msg);
4121		break;
4122	case VIRTCHNL_OP_ADD_VLAN:
4123		err = ops->add_vlan_msg(vf, msg);
4124		break;
4125	case VIRTCHNL_OP_DEL_VLAN:
4126		err = ops->remove_vlan_msg(vf, msg);
4127		break;
4128	case VIRTCHNL_OP_GET_SUPPORTED_RXDIDS:
4129		err = ops->query_rxdid(vf);
4130		break;
4131	case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
4132		err = ops->get_rss_hena(vf);
4133		break;
4134	case VIRTCHNL_OP_SET_RSS_HENA:
4135		err = ops->set_rss_hena_msg(vf, msg);
4136		break;
4137	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
4138		err = ops->ena_vlan_stripping(vf);
4139		break;
4140	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
4141		err = ops->dis_vlan_stripping(vf);
4142		break;
4143	case VIRTCHNL_OP_ADD_FDIR_FILTER:
4144		err = ops->add_fdir_fltr_msg(vf, msg);
4145		break;
4146	case VIRTCHNL_OP_DEL_FDIR_FILTER:
4147		err = ops->del_fdir_fltr_msg(vf, msg);
4148		break;
4149	case VIRTCHNL_OP_ADD_RSS_CFG:
4150		err = ops->handle_rss_cfg_msg(vf, msg, true);
4151		break;
4152	case VIRTCHNL_OP_DEL_RSS_CFG:
4153		err = ops->handle_rss_cfg_msg(vf, msg, false);
4154		break;
4155	case VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS:
4156		err = ops->get_offload_vlan_v2_caps(vf);
4157		break;
4158	case VIRTCHNL_OP_ADD_VLAN_V2:
4159		err = ops->add_vlan_v2_msg(vf, msg);
4160		break;
4161	case VIRTCHNL_OP_DEL_VLAN_V2:
4162		err = ops->remove_vlan_v2_msg(vf, msg);
4163		break;
4164	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2:
4165		err = ops->ena_vlan_stripping_v2_msg(vf, msg);
4166		break;
4167	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2:
4168		err = ops->dis_vlan_stripping_v2_msg(vf, msg);
4169		break;
4170	case VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2:
4171		err = ops->ena_vlan_insertion_v2_msg(vf, msg);
4172		break;
4173	case VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2:
4174		err = ops->dis_vlan_insertion_v2_msg(vf, msg);
4175		break;
4176	case VIRTCHNL_OP_UNKNOWN:
4177	default:
4178		dev_err(dev, "Unsupported opcode %d from VF %d\n", v_opcode,
4179			vf_id);
4180		err = ice_vc_send_msg_to_vf(vf, v_opcode,
4181					    VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4182					    NULL, 0);
4183		break;
4184	}
4185	if (err) {
4186		/* Helper function cares less about error return values here
4187		 * as it is busy with pending work.
4188		 */
4189		dev_info(dev, "PF failed to honor VF %d, opcode %d, error %d\n",
4190			 vf_id, v_opcode, err);
4191	}
4192
4193finish:
4194	mutex_unlock(&vf->cfg_lock);
4195	ice_put_vf(vf);
4196}
v6.2
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (C) 2022, Intel Corporation. */
   3
   4#include "ice_virtchnl.h"
   5#include "ice_vf_lib_private.h"
   6#include "ice.h"
   7#include "ice_base.h"
   8#include "ice_lib.h"
   9#include "ice_fltr.h"
  10#include "ice_virtchnl_allowlist.h"
  11#include "ice_vf_vsi_vlan_ops.h"
  12#include "ice_vlan.h"
  13#include "ice_flex_pipe.h"
  14#include "ice_dcb_lib.h"
  15
  16#define FIELD_SELECTOR(proto_hdr_field) \
  17		BIT((proto_hdr_field) & PROTO_HDR_FIELD_MASK)
  18
  19struct ice_vc_hdr_match_type {
  20	u32 vc_hdr;	/* virtchnl headers (VIRTCHNL_PROTO_HDR_XXX) */
  21	u32 ice_hdr;	/* ice headers (ICE_FLOW_SEG_HDR_XXX) */
  22};
  23
  24static const struct ice_vc_hdr_match_type ice_vc_hdr_list[] = {
  25	{VIRTCHNL_PROTO_HDR_NONE,	ICE_FLOW_SEG_HDR_NONE},
  26	{VIRTCHNL_PROTO_HDR_ETH,	ICE_FLOW_SEG_HDR_ETH},
  27	{VIRTCHNL_PROTO_HDR_S_VLAN,	ICE_FLOW_SEG_HDR_VLAN},
  28	{VIRTCHNL_PROTO_HDR_C_VLAN,	ICE_FLOW_SEG_HDR_VLAN},
  29	{VIRTCHNL_PROTO_HDR_IPV4,	ICE_FLOW_SEG_HDR_IPV4 |
  30					ICE_FLOW_SEG_HDR_IPV_OTHER},
  31	{VIRTCHNL_PROTO_HDR_IPV6,	ICE_FLOW_SEG_HDR_IPV6 |
  32					ICE_FLOW_SEG_HDR_IPV_OTHER},
  33	{VIRTCHNL_PROTO_HDR_TCP,	ICE_FLOW_SEG_HDR_TCP},
  34	{VIRTCHNL_PROTO_HDR_UDP,	ICE_FLOW_SEG_HDR_UDP},
  35	{VIRTCHNL_PROTO_HDR_SCTP,	ICE_FLOW_SEG_HDR_SCTP},
  36	{VIRTCHNL_PROTO_HDR_PPPOE,	ICE_FLOW_SEG_HDR_PPPOE},
  37	{VIRTCHNL_PROTO_HDR_GTPU_IP,	ICE_FLOW_SEG_HDR_GTPU_IP},
  38	{VIRTCHNL_PROTO_HDR_GTPU_EH,	ICE_FLOW_SEG_HDR_GTPU_EH},
  39	{VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_DWN,
  40					ICE_FLOW_SEG_HDR_GTPU_DWN},
  41	{VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_UP,
  42					ICE_FLOW_SEG_HDR_GTPU_UP},
  43	{VIRTCHNL_PROTO_HDR_L2TPV3,	ICE_FLOW_SEG_HDR_L2TPV3},
  44	{VIRTCHNL_PROTO_HDR_ESP,	ICE_FLOW_SEG_HDR_ESP},
  45	{VIRTCHNL_PROTO_HDR_AH,		ICE_FLOW_SEG_HDR_AH},
  46	{VIRTCHNL_PROTO_HDR_PFCP,	ICE_FLOW_SEG_HDR_PFCP_SESSION},
  47};
  48
  49struct ice_vc_hash_field_match_type {
  50	u32 vc_hdr;		/* virtchnl headers
  51				 * (VIRTCHNL_PROTO_HDR_XXX)
  52				 */
  53	u32 vc_hash_field;	/* virtchnl hash fields selector
  54				 * FIELD_SELECTOR((VIRTCHNL_PROTO_HDR_ETH_XXX))
  55				 */
  56	u64 ice_hash_field;	/* ice hash fields
  57				 * (BIT_ULL(ICE_FLOW_FIELD_IDX_XXX))
  58				 */
  59};
  60
  61static const struct
  62ice_vc_hash_field_match_type ice_vc_hash_field_list[] = {
  63	{VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC),
  64		BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_SA)},
  65	{VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
  66		BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_DA)},
  67	{VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC) |
  68		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
  69		ICE_FLOW_HASH_ETH},
  70	{VIRTCHNL_PROTO_HDR_ETH,
  71		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_ETHERTYPE),
  72		BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_TYPE)},
  73	{VIRTCHNL_PROTO_HDR_S_VLAN,
  74		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_S_VLAN_ID),
  75		BIT_ULL(ICE_FLOW_FIELD_IDX_S_VLAN)},
  76	{VIRTCHNL_PROTO_HDR_C_VLAN,
  77		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_C_VLAN_ID),
  78		BIT_ULL(ICE_FLOW_FIELD_IDX_C_VLAN)},
  79	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC),
  80		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA)},
  81	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
  82		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA)},
  83	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
  84		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
  85		ICE_FLOW_HASH_IPV4},
  86	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
  87		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
  88		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA) |
  89		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
  90	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
  91		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
  92		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA) |
  93		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
  94	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
  95		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
  96		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
  97		ICE_FLOW_HASH_IPV4 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
  98	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
  99		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
 100	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC),
 101		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA)},
 102	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
 103		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA)},
 104	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
 105		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
 106		ICE_FLOW_HASH_IPV6},
 107	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
 108		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
 109		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA) |
 110		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
 111	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
 112		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
 113		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA) |
 114		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
 115	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
 116		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
 117		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
 118		ICE_FLOW_HASH_IPV6 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
 119	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
 120		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
 121	{VIRTCHNL_PROTO_HDR_TCP,
 122		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT),
 123		BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_SRC_PORT)},
 124	{VIRTCHNL_PROTO_HDR_TCP,
 125		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
 126		BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_DST_PORT)},
 127	{VIRTCHNL_PROTO_HDR_TCP,
 128		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT) |
 129		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
 130		ICE_FLOW_HASH_TCP_PORT},
 131	{VIRTCHNL_PROTO_HDR_UDP,
 132		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT),
 133		BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_SRC_PORT)},
 134	{VIRTCHNL_PROTO_HDR_UDP,
 135		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
 136		BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_DST_PORT)},
 137	{VIRTCHNL_PROTO_HDR_UDP,
 138		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT) |
 139		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
 140		ICE_FLOW_HASH_UDP_PORT},
 141	{VIRTCHNL_PROTO_HDR_SCTP,
 142		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT),
 143		BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT)},
 144	{VIRTCHNL_PROTO_HDR_SCTP,
 145		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
 146		BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_DST_PORT)},
 147	{VIRTCHNL_PROTO_HDR_SCTP,
 148		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT) |
 149		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
 150		ICE_FLOW_HASH_SCTP_PORT},
 151	{VIRTCHNL_PROTO_HDR_PPPOE,
 152		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PPPOE_SESS_ID),
 153		BIT_ULL(ICE_FLOW_FIELD_IDX_PPPOE_SESS_ID)},
 154	{VIRTCHNL_PROTO_HDR_GTPU_IP,
 155		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_GTPU_IP_TEID),
 156		BIT_ULL(ICE_FLOW_FIELD_IDX_GTPU_IP_TEID)},
 157	{VIRTCHNL_PROTO_HDR_L2TPV3,
 158		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_L2TPV3_SESS_ID),
 159		BIT_ULL(ICE_FLOW_FIELD_IDX_L2TPV3_SESS_ID)},
 160	{VIRTCHNL_PROTO_HDR_ESP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ESP_SPI),
 161		BIT_ULL(ICE_FLOW_FIELD_IDX_ESP_SPI)},
 162	{VIRTCHNL_PROTO_HDR_AH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_AH_SPI),
 163		BIT_ULL(ICE_FLOW_FIELD_IDX_AH_SPI)},
 164	{VIRTCHNL_PROTO_HDR_PFCP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PFCP_SEID),
 165		BIT_ULL(ICE_FLOW_FIELD_IDX_PFCP_SEID)},
 166};
 167
 168/**
 169 * ice_vc_vf_broadcast - Broadcast a message to all VFs on PF
 170 * @pf: pointer to the PF structure
 171 * @v_opcode: operation code
 172 * @v_retval: return value
 173 * @msg: pointer to the msg buffer
 174 * @msglen: msg length
 175 */
 176static void
 177ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
 178		    enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
 179{
 180	struct ice_hw *hw = &pf->hw;
 181	struct ice_vf *vf;
 182	unsigned int bkt;
 183
 184	mutex_lock(&pf->vfs.table_lock);
 185	ice_for_each_vf(pf, bkt, vf) {
 186		/* Not all vfs are enabled so skip the ones that are not */
 187		if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
 188		    !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
 189			continue;
 190
 191		/* Ignore return value on purpose - a given VF may fail, but
 192		 * we need to keep going and send to all of them
 193		 */
 194		ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
 195				      msglen, NULL);
 196	}
 197	mutex_unlock(&pf->vfs.table_lock);
 198}
 199
 200/**
 201 * ice_set_pfe_link - Set the link speed/status of the virtchnl_pf_event
 202 * @vf: pointer to the VF structure
 203 * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
 204 * @ice_link_speed: link speed specified by ICE_AQ_LINK_SPEED_*
 205 * @link_up: whether or not to set the link up/down
 206 */
 207static void
 208ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
 209		 int ice_link_speed, bool link_up)
 210{
 211	if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
 212		pfe->event_data.link_event_adv.link_status = link_up;
 213		/* Speed in Mbps */
 214		pfe->event_data.link_event_adv.link_speed =
 215			ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
 216	} else {
 217		pfe->event_data.link_event.link_status = link_up;
 218		/* Legacy method for virtchnl link speeds */
 219		pfe->event_data.link_event.link_speed =
 220			(enum virtchnl_link_speed)
 221			ice_conv_link_speed_to_virtchnl(false, ice_link_speed);
 222	}
 223}
 224
 225/**
 226 * ice_vc_notify_vf_link_state - Inform a VF of link status
 227 * @vf: pointer to the VF structure
 228 *
 229 * send a link status message to a single VF
 230 */
 231void ice_vc_notify_vf_link_state(struct ice_vf *vf)
 232{
 233	struct virtchnl_pf_event pfe = { 0 };
 234	struct ice_hw *hw = &vf->pf->hw;
 235
 236	pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
 237	pfe.severity = PF_EVENT_SEVERITY_INFO;
 238
 239	if (ice_is_vf_link_up(vf))
 240		ice_set_pfe_link(vf, &pfe,
 241				 hw->port_info->phy.link_info.link_speed, true);
 242	else
 243		ice_set_pfe_link(vf, &pfe, ICE_AQ_LINK_SPEED_UNKNOWN, false);
 244
 245	ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
 246			      VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,
 247			      sizeof(pfe), NULL);
 248}
 249
 250/**
 251 * ice_vc_notify_link_state - Inform all VFs on a PF of link status
 252 * @pf: pointer to the PF structure
 253 */
 254void ice_vc_notify_link_state(struct ice_pf *pf)
 255{
 256	struct ice_vf *vf;
 257	unsigned int bkt;
 258
 259	mutex_lock(&pf->vfs.table_lock);
 260	ice_for_each_vf(pf, bkt, vf)
 261		ice_vc_notify_vf_link_state(vf);
 262	mutex_unlock(&pf->vfs.table_lock);
 263}
 264
 265/**
 266 * ice_vc_notify_reset - Send pending reset message to all VFs
 267 * @pf: pointer to the PF structure
 268 *
 269 * indicate a pending reset to all VFs on a given PF
 270 */
 271void ice_vc_notify_reset(struct ice_pf *pf)
 272{
 273	struct virtchnl_pf_event pfe;
 274
 275	if (!ice_has_vfs(pf))
 276		return;
 277
 278	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
 279	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
 280	ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
 281			    (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
 282}
 283
 284/**
 285 * ice_vc_send_msg_to_vf - Send message to VF
 286 * @vf: pointer to the VF info
 287 * @v_opcode: virtual channel opcode
 288 * @v_retval: virtual channel return value
 289 * @msg: pointer to the msg buffer
 290 * @msglen: msg length
 291 *
 292 * send msg to VF
 293 */
 294int
 295ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
 296		      enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
 297{
 298	struct device *dev;
 299	struct ice_pf *pf;
 300	int aq_ret;
 301
 302	pf = vf->pf;
 303	dev = ice_pf_to_dev(pf);
 304
 305	aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
 306				       msg, msglen, NULL);
 307	if (aq_ret && pf->hw.mailboxq.sq_last_status != ICE_AQ_RC_ENOSYS) {
 308		dev_info(dev, "Unable to send the message to VF %d ret %d aq_err %s\n",
 309			 vf->vf_id, aq_ret,
 310			 ice_aq_str(pf->hw.mailboxq.sq_last_status));
 311		return -EIO;
 312	}
 313
 314	return 0;
 315}
 316
 317/**
 318 * ice_vc_get_ver_msg
 319 * @vf: pointer to the VF info
 320 * @msg: pointer to the msg buffer
 321 *
 322 * called from the VF to request the API version used by the PF
 323 */
 324static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
 325{
 326	struct virtchnl_version_info info = {
 327		VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
 328	};
 329
 330	vf->vf_ver = *(struct virtchnl_version_info *)msg;
 331	/* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
 332	if (VF_IS_V10(&vf->vf_ver))
 333		info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
 334
 335	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
 336				     VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
 337				     sizeof(struct virtchnl_version_info));
 338}
 339
 340/**
 341 * ice_vc_get_max_frame_size - get max frame size allowed for VF
 342 * @vf: VF used to determine max frame size
 343 *
 344 * Max frame size is determined based on the current port's max frame size and
 345 * whether a port VLAN is configured on this VF. The VF is not aware whether
 346 * it's in a port VLAN so the PF needs to account for this in max frame size
 347 * checks and sending the max frame size to the VF.
 348 */
 349static u16 ice_vc_get_max_frame_size(struct ice_vf *vf)
 350{
 351	struct ice_port_info *pi = ice_vf_get_port_info(vf);
 352	u16 max_frame_size;
 353
 354	max_frame_size = pi->phy.link_info.max_frame_size;
 355
 356	if (ice_vf_is_port_vlan_ena(vf))
 357		max_frame_size -= VLAN_HLEN;
 358
 359	return max_frame_size;
 360}
 361
 362/**
 363 * ice_vc_get_vlan_caps
 364 * @hw: pointer to the hw
 365 * @vf: pointer to the VF info
 366 * @vsi: pointer to the VSI
 367 * @driver_caps: current driver caps
 368 *
 369 * Return 0 if there is no VLAN caps supported, or VLAN caps value
 370 */
 371static u32
 372ice_vc_get_vlan_caps(struct ice_hw *hw, struct ice_vf *vf, struct ice_vsi *vsi,
 373		     u32 driver_caps)
 374{
 375	if (ice_is_eswitch_mode_switchdev(vf->pf))
 376		/* In switchdev setting VLAN from VF isn't supported */
 377		return 0;
 378
 379	if (driver_caps & VIRTCHNL_VF_OFFLOAD_VLAN_V2) {
 380		/* VLAN offloads based on current device configuration */
 381		return VIRTCHNL_VF_OFFLOAD_VLAN_V2;
 382	} else if (driver_caps & VIRTCHNL_VF_OFFLOAD_VLAN) {
 383		/* allow VF to negotiate VIRTCHNL_VF_OFFLOAD explicitly for
 384		 * these two conditions, which amounts to guest VLAN filtering
 385		 * and offloads being based on the inner VLAN or the
 386		 * inner/single VLAN respectively and don't allow VF to
 387		 * negotiate VIRTCHNL_VF_OFFLOAD in any other cases
 388		 */
 389		if (ice_is_dvm_ena(hw) && ice_vf_is_port_vlan_ena(vf)) {
 390			return VIRTCHNL_VF_OFFLOAD_VLAN;
 391		} else if (!ice_is_dvm_ena(hw) &&
 392			   !ice_vf_is_port_vlan_ena(vf)) {
 393			/* configure backward compatible support for VFs that
 394			 * only support VIRTCHNL_VF_OFFLOAD_VLAN, the PF is
 395			 * configured in SVM, and no port VLAN is configured
 396			 */
 397			ice_vf_vsi_cfg_svm_legacy_vlan_mode(vsi);
 398			return VIRTCHNL_VF_OFFLOAD_VLAN;
 399		} else if (ice_is_dvm_ena(hw)) {
 400			/* configure software offloaded VLAN support when DVM
 401			 * is enabled, but no port VLAN is enabled
 402			 */
 403			ice_vf_vsi_cfg_dvm_legacy_vlan_mode(vsi);
 404		}
 405	}
 406
 407	return 0;
 408}
 409
 410/**
 411 * ice_vc_get_vf_res_msg
 412 * @vf: pointer to the VF info
 413 * @msg: pointer to the msg buffer
 414 *
 415 * called from the VF to request its resources
 416 */
 417static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
 418{
 419	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
 420	struct virtchnl_vf_resource *vfres = NULL;
 421	struct ice_hw *hw = &vf->pf->hw;
 422	struct ice_vsi *vsi;
 423	int len = 0;
 424	int ret;
 425
 426	if (ice_check_vf_init(vf)) {
 427		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 428		goto err;
 429	}
 430
 431	len = sizeof(struct virtchnl_vf_resource);
 432
 433	vfres = kzalloc(len, GFP_KERNEL);
 434	if (!vfres) {
 435		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
 436		len = 0;
 437		goto err;
 438	}
 439	if (VF_IS_V11(&vf->vf_ver))
 440		vf->driver_caps = *(u32 *)msg;
 441	else
 442		vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
 443				  VIRTCHNL_VF_OFFLOAD_RSS_REG |
 444				  VIRTCHNL_VF_OFFLOAD_VLAN;
 445
 446	vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
 447	vsi = ice_get_vf_vsi(vf);
 448	if (!vsi) {
 449		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 450		goto err;
 451	}
 452
 453	vfres->vf_cap_flags |= ice_vc_get_vlan_caps(hw, vf, vsi,
 454						    vf->driver_caps);
 455
 456	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
 457		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
 458	} else {
 459		if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
 460			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
 461		else
 462			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
 463	}
 464
 465	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC)
 466		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC;
 467
 468	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_FDIR_PF)
 469		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_FDIR_PF;
 470
 471	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
 472		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
 473
 474	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
 475		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
 476
 477	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
 478		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
 479
 480	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
 481		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
 482
 483	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
 484		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
 485
 486	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
 487		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
 488
 
 
 
 489	if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
 490		vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
 491
 492	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF)
 493		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF;
 494
 495	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_USO)
 496		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_USO;
 497
 498	vfres->num_vsis = 1;
 499	/* Tx and Rx queue are equal for VF */
 500	vfres->num_queue_pairs = vsi->num_txq;
 501	vfres->max_vectors = vf->pf->vfs.num_msix_per;
 502	vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
 503	vfres->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
 504	vfres->max_mtu = ice_vc_get_max_frame_size(vf);
 505
 506	vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
 507	vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
 508	vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
 509	ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
 510			vf->hw_lan_addr.addr);
 511
 512	/* match guest capabilities */
 513	vf->driver_caps = vfres->vf_cap_flags;
 514
 515	ice_vc_set_caps_allowlist(vf);
 516	ice_vc_set_working_allowlist(vf);
 517
 518	set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
 519
 520err:
 521	/* send the response back to the VF */
 522	ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
 523				    (u8 *)vfres, len);
 524
 525	kfree(vfres);
 526	return ret;
 527}
 528
 529/**
 530 * ice_vc_reset_vf_msg
 531 * @vf: pointer to the VF info
 532 *
 533 * called from the VF to reset itself,
 534 * unlike other virtchnl messages, PF driver
 535 * doesn't send the response back to the VF
 536 */
 537static void ice_vc_reset_vf_msg(struct ice_vf *vf)
 538{
 539	if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
 540		ice_reset_vf(vf, 0);
 541}
 542
 543/**
 544 * ice_vc_isvalid_vsi_id
 545 * @vf: pointer to the VF info
 546 * @vsi_id: VF relative VSI ID
 547 *
 548 * check for the valid VSI ID
 549 */
 550bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
 551{
 552	struct ice_pf *pf = vf->pf;
 553	struct ice_vsi *vsi;
 554
 555	vsi = ice_find_vsi(pf, vsi_id);
 556
 557	return (vsi && (vsi->vf == vf));
 558}
 559
 560/**
 561 * ice_vc_isvalid_q_id
 562 * @vf: pointer to the VF info
 563 * @vsi_id: VSI ID
 564 * @qid: VSI relative queue ID
 565 *
 566 * check for the valid queue ID
 567 */
 568static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
 569{
 570	struct ice_vsi *vsi = ice_find_vsi(vf->pf, vsi_id);
 571	/* allocated Tx and Rx queues should be always equal for VF VSI */
 572	return (vsi && (qid < vsi->alloc_txq));
 573}
 574
 575/**
 576 * ice_vc_isvalid_ring_len
 577 * @ring_len: length of ring
 578 *
 579 * check for the valid ring count, should be multiple of ICE_REQ_DESC_MULTIPLE
 580 * or zero
 581 */
 582static bool ice_vc_isvalid_ring_len(u16 ring_len)
 583{
 584	return ring_len == 0 ||
 585	       (ring_len >= ICE_MIN_NUM_DESC &&
 586		ring_len <= ICE_MAX_NUM_DESC &&
 587		!(ring_len % ICE_REQ_DESC_MULTIPLE));
 588}
 589
 590/**
 591 * ice_vc_validate_pattern
 592 * @vf: pointer to the VF info
 593 * @proto: virtchnl protocol headers
 594 *
 595 * validate the pattern is supported or not.
 596 *
 597 * Return: true on success, false on error.
 598 */
 599bool
 600ice_vc_validate_pattern(struct ice_vf *vf, struct virtchnl_proto_hdrs *proto)
 601{
 602	bool is_ipv4 = false;
 603	bool is_ipv6 = false;
 604	bool is_udp = false;
 605	u16 ptype = -1;
 606	int i = 0;
 607
 608	while (i < proto->count &&
 609	       proto->proto_hdr[i].type != VIRTCHNL_PROTO_HDR_NONE) {
 610		switch (proto->proto_hdr[i].type) {
 611		case VIRTCHNL_PROTO_HDR_ETH:
 612			ptype = ICE_PTYPE_MAC_PAY;
 613			break;
 614		case VIRTCHNL_PROTO_HDR_IPV4:
 615			ptype = ICE_PTYPE_IPV4_PAY;
 616			is_ipv4 = true;
 617			break;
 618		case VIRTCHNL_PROTO_HDR_IPV6:
 619			ptype = ICE_PTYPE_IPV6_PAY;
 620			is_ipv6 = true;
 621			break;
 622		case VIRTCHNL_PROTO_HDR_UDP:
 623			if (is_ipv4)
 624				ptype = ICE_PTYPE_IPV4_UDP_PAY;
 625			else if (is_ipv6)
 626				ptype = ICE_PTYPE_IPV6_UDP_PAY;
 627			is_udp = true;
 628			break;
 629		case VIRTCHNL_PROTO_HDR_TCP:
 630			if (is_ipv4)
 631				ptype = ICE_PTYPE_IPV4_TCP_PAY;
 632			else if (is_ipv6)
 633				ptype = ICE_PTYPE_IPV6_TCP_PAY;
 634			break;
 635		case VIRTCHNL_PROTO_HDR_SCTP:
 636			if (is_ipv4)
 637				ptype = ICE_PTYPE_IPV4_SCTP_PAY;
 638			else if (is_ipv6)
 639				ptype = ICE_PTYPE_IPV6_SCTP_PAY;
 640			break;
 641		case VIRTCHNL_PROTO_HDR_GTPU_IP:
 642		case VIRTCHNL_PROTO_HDR_GTPU_EH:
 643			if (is_ipv4)
 644				ptype = ICE_MAC_IPV4_GTPU;
 645			else if (is_ipv6)
 646				ptype = ICE_MAC_IPV6_GTPU;
 647			goto out;
 648		case VIRTCHNL_PROTO_HDR_L2TPV3:
 649			if (is_ipv4)
 650				ptype = ICE_MAC_IPV4_L2TPV3;
 651			else if (is_ipv6)
 652				ptype = ICE_MAC_IPV6_L2TPV3;
 653			goto out;
 654		case VIRTCHNL_PROTO_HDR_ESP:
 655			if (is_ipv4)
 656				ptype = is_udp ? ICE_MAC_IPV4_NAT_T_ESP :
 657						ICE_MAC_IPV4_ESP;
 658			else if (is_ipv6)
 659				ptype = is_udp ? ICE_MAC_IPV6_NAT_T_ESP :
 660						ICE_MAC_IPV6_ESP;
 661			goto out;
 662		case VIRTCHNL_PROTO_HDR_AH:
 663			if (is_ipv4)
 664				ptype = ICE_MAC_IPV4_AH;
 665			else if (is_ipv6)
 666				ptype = ICE_MAC_IPV6_AH;
 667			goto out;
 668		case VIRTCHNL_PROTO_HDR_PFCP:
 669			if (is_ipv4)
 670				ptype = ICE_MAC_IPV4_PFCP_SESSION;
 671			else if (is_ipv6)
 672				ptype = ICE_MAC_IPV6_PFCP_SESSION;
 673			goto out;
 674		default:
 675			break;
 676		}
 677		i++;
 678	}
 679
 680out:
 681	return ice_hw_ptype_ena(&vf->pf->hw, ptype);
 682}
 683
 684/**
 685 * ice_vc_parse_rss_cfg - parses hash fields and headers from
 686 * a specific virtchnl RSS cfg
 687 * @hw: pointer to the hardware
 688 * @rss_cfg: pointer to the virtchnl RSS cfg
 689 * @addl_hdrs: pointer to the protocol header fields (ICE_FLOW_SEG_HDR_*)
 690 * to configure
 691 * @hash_flds: pointer to the hash bit fields (ICE_FLOW_HASH_*) to configure
 692 *
 693 * Return true if all the protocol header and hash fields in the RSS cfg could
 694 * be parsed, else return false
 695 *
 696 * This function parses the virtchnl RSS cfg to be the intended
 697 * hash fields and the intended header for RSS configuration
 698 */
 699static bool
 700ice_vc_parse_rss_cfg(struct ice_hw *hw, struct virtchnl_rss_cfg *rss_cfg,
 701		     u32 *addl_hdrs, u64 *hash_flds)
 702{
 703	const struct ice_vc_hash_field_match_type *hf_list;
 704	const struct ice_vc_hdr_match_type *hdr_list;
 705	int i, hf_list_len, hdr_list_len;
 
 
 
 
 
 
 
 
 
 
 706
 707	hf_list = ice_vc_hash_field_list;
 708	hf_list_len = ARRAY_SIZE(ice_vc_hash_field_list);
 709	hdr_list = ice_vc_hdr_list;
 710	hdr_list_len = ARRAY_SIZE(ice_vc_hdr_list);
 711
 712	for (i = 0; i < rss_cfg->proto_hdrs.count; i++) {
 713		struct virtchnl_proto_hdr *proto_hdr =
 714					&rss_cfg->proto_hdrs.proto_hdr[i];
 715		bool hdr_found = false;
 716		int j;
 717
 718		/* Find matched ice headers according to virtchnl headers. */
 719		for (j = 0; j < hdr_list_len; j++) {
 720			struct ice_vc_hdr_match_type hdr_map = hdr_list[j];
 721
 722			if (proto_hdr->type == hdr_map.vc_hdr) {
 723				*addl_hdrs |= hdr_map.ice_hdr;
 724				hdr_found = true;
 725			}
 726		}
 727
 728		if (!hdr_found)
 729			return false;
 730
 731		/* Find matched ice hash fields according to
 732		 * virtchnl hash fields.
 733		 */
 734		for (j = 0; j < hf_list_len; j++) {
 735			struct ice_vc_hash_field_match_type hf_map = hf_list[j];
 736
 737			if (proto_hdr->type == hf_map.vc_hdr &&
 738			    proto_hdr->field_selector == hf_map.vc_hash_field) {
 739				*hash_flds |= hf_map.ice_hash_field;
 740				break;
 741			}
 742		}
 743	}
 744
 745	return true;
 746}
 747
 748/**
 749 * ice_vf_adv_rss_offload_ena - determine if capabilities support advanced
 750 * RSS offloads
 751 * @caps: VF driver negotiated capabilities
 752 *
 753 * Return true if VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF capability is set,
 754 * else return false
 755 */
 756static bool ice_vf_adv_rss_offload_ena(u32 caps)
 757{
 758	return !!(caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF);
 759}
 760
 761/**
 762 * ice_vc_handle_rss_cfg
 763 * @vf: pointer to the VF info
 764 * @msg: pointer to the message buffer
 765 * @add: add a RSS config if true, otherwise delete a RSS config
 766 *
 767 * This function adds/deletes a RSS config
 768 */
 769static int ice_vc_handle_rss_cfg(struct ice_vf *vf, u8 *msg, bool add)
 770{
 771	u32 v_opcode = add ? VIRTCHNL_OP_ADD_RSS_CFG : VIRTCHNL_OP_DEL_RSS_CFG;
 772	struct virtchnl_rss_cfg *rss_cfg = (struct virtchnl_rss_cfg *)msg;
 773	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
 774	struct device *dev = ice_pf_to_dev(vf->pf);
 775	struct ice_hw *hw = &vf->pf->hw;
 776	struct ice_vsi *vsi;
 777
 778	if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
 779		dev_dbg(dev, "VF %d attempting to configure RSS, but RSS is not supported by the PF\n",
 780			vf->vf_id);
 781		v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
 782		goto error_param;
 783	}
 784
 785	if (!ice_vf_adv_rss_offload_ena(vf->driver_caps)) {
 786		dev_dbg(dev, "VF %d attempting to configure RSS, but Advanced RSS offload is not supported\n",
 787			vf->vf_id);
 788		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 789		goto error_param;
 790	}
 791
 792	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
 793		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 794		goto error_param;
 795	}
 796
 797	if (rss_cfg->proto_hdrs.count > VIRTCHNL_MAX_NUM_PROTO_HDRS ||
 798	    rss_cfg->rss_algorithm < VIRTCHNL_RSS_ALG_TOEPLITZ_ASYMMETRIC ||
 799	    rss_cfg->rss_algorithm > VIRTCHNL_RSS_ALG_XOR_SYMMETRIC) {
 800		dev_dbg(dev, "VF %d attempting to configure RSS, but RSS configuration is not valid\n",
 801			vf->vf_id);
 802		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 803		goto error_param;
 804	}
 805
 806	vsi = ice_get_vf_vsi(vf);
 807	if (!vsi) {
 808		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 809		goto error_param;
 810	}
 811
 812	if (!ice_vc_validate_pattern(vf, &rss_cfg->proto_hdrs)) {
 813		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 814		goto error_param;
 815	}
 816
 817	if (rss_cfg->rss_algorithm == VIRTCHNL_RSS_ALG_R_ASYMMETRIC) {
 818		struct ice_vsi_ctx *ctx;
 819		u8 lut_type, hash_type;
 820		int status;
 821
 822		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
 823		hash_type = add ? ICE_AQ_VSI_Q_OPT_RSS_XOR :
 824				ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
 825
 826		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
 827		if (!ctx) {
 828			v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
 829			goto error_param;
 830		}
 831
 832		ctx->info.q_opt_rss = ((lut_type <<
 833					ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
 834				       ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
 835				       (hash_type &
 836					ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
 837
 838		/* Preserve existing queueing option setting */
 839		ctx->info.q_opt_rss |= (vsi->info.q_opt_rss &
 840					  ICE_AQ_VSI_Q_OPT_RSS_GBL_LUT_M);
 841		ctx->info.q_opt_tc = vsi->info.q_opt_tc;
 842		ctx->info.q_opt_flags = vsi->info.q_opt_rss;
 843
 844		ctx->info.valid_sections =
 845				cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
 846
 847		status = ice_update_vsi(hw, vsi->idx, ctx, NULL);
 848		if (status) {
 849			dev_err(dev, "update VSI for RSS failed, err %d aq_err %s\n",
 850				status, ice_aq_str(hw->adminq.sq_last_status));
 851			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 852		} else {
 853			vsi->info.q_opt_rss = ctx->info.q_opt_rss;
 854		}
 855
 856		kfree(ctx);
 857	} else {
 858		u32 addl_hdrs = ICE_FLOW_SEG_HDR_NONE;
 859		u64 hash_flds = ICE_HASH_INVALID;
 860
 861		if (!ice_vc_parse_rss_cfg(hw, rss_cfg, &addl_hdrs,
 862					  &hash_flds)) {
 
 
 
 
 
 
 
 
 863			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 864			goto error_param;
 865		}
 866
 867		if (add) {
 868			if (ice_add_rss_cfg(hw, vsi->idx, hash_flds,
 869					    addl_hdrs)) {
 870				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 871				dev_err(dev, "ice_add_rss_cfg failed for vsi = %d, v_ret = %d\n",
 872					vsi->vsi_num, v_ret);
 873			}
 874		} else {
 875			int status;
 876
 877			status = ice_rem_rss_cfg(hw, vsi->idx, hash_flds,
 878						 addl_hdrs);
 879			/* We just ignore -ENOENT, because if two configurations
 880			 * share the same profile remove one of them actually
 881			 * removes both, since the profile is deleted.
 882			 */
 883			if (status && status != -ENOENT) {
 884				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 885				dev_err(dev, "ice_rem_rss_cfg failed for VF ID:%d, error:%d\n",
 886					vf->vf_id, status);
 887			}
 888		}
 889	}
 890
 891error_param:
 892	return ice_vc_send_msg_to_vf(vf, v_opcode, v_ret, NULL, 0);
 893}
 894
 895/**
 896 * ice_vc_config_rss_key
 897 * @vf: pointer to the VF info
 898 * @msg: pointer to the msg buffer
 899 *
 900 * Configure the VF's RSS key
 901 */
 902static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
 903{
 904	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
 905	struct virtchnl_rss_key *vrk =
 906		(struct virtchnl_rss_key *)msg;
 907	struct ice_vsi *vsi;
 908
 909	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
 910		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 911		goto error_param;
 912	}
 913
 914	if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
 915		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 916		goto error_param;
 917	}
 918
 919	if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
 920		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 921		goto error_param;
 922	}
 923
 924	if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
 925		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 926		goto error_param;
 927	}
 928
 929	vsi = ice_get_vf_vsi(vf);
 930	if (!vsi) {
 931		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 932		goto error_param;
 933	}
 934
 935	if (ice_set_rss_key(vsi, vrk->key))
 936		v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
 937error_param:
 938	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
 939				     NULL, 0);
 940}
 941
 942/**
 943 * ice_vc_config_rss_lut
 944 * @vf: pointer to the VF info
 945 * @msg: pointer to the msg buffer
 946 *
 947 * Configure the VF's RSS LUT
 948 */
 949static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
 950{
 951	struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
 952	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
 953	struct ice_vsi *vsi;
 954
 955	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
 956		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 957		goto error_param;
 958	}
 959
 960	if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
 961		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 962		goto error_param;
 963	}
 964
 965	if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE) {
 966		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 967		goto error_param;
 968	}
 969
 970	if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
 971		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 972		goto error_param;
 973	}
 974
 975	vsi = ice_get_vf_vsi(vf);
 976	if (!vsi) {
 977		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 978		goto error_param;
 979	}
 980
 981	if (ice_set_rss_lut(vsi, vrl->lut, ICE_VSIQF_HLUT_ARRAY_SIZE))
 982		v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
 983error_param:
 984	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
 985				     NULL, 0);
 986}
 987
 988/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 989 * ice_vc_cfg_promiscuous_mode_msg
 990 * @vf: pointer to the VF info
 991 * @msg: pointer to the msg buffer
 992 *
 993 * called from the VF to configure VF VSIs promiscuous mode
 994 */
 995static int ice_vc_cfg_promiscuous_mode_msg(struct ice_vf *vf, u8 *msg)
 996{
 997	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
 998	bool rm_promisc, alluni = false, allmulti = false;
 999	struct virtchnl_promisc_info *info =
1000	    (struct virtchnl_promisc_info *)msg;
1001	struct ice_vsi_vlan_ops *vlan_ops;
1002	int mcast_err = 0, ucast_err = 0;
1003	struct ice_pf *pf = vf->pf;
1004	struct ice_vsi *vsi;
1005	u8 mcast_m, ucast_m;
1006	struct device *dev;
1007	int ret = 0;
1008
1009	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1010		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1011		goto error_param;
1012	}
1013
1014	if (!ice_vc_isvalid_vsi_id(vf, info->vsi_id)) {
1015		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1016		goto error_param;
1017	}
1018
1019	vsi = ice_get_vf_vsi(vf);
1020	if (!vsi) {
1021		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1022		goto error_param;
1023	}
1024
1025	dev = ice_pf_to_dev(pf);
1026	if (!ice_is_vf_trusted(vf)) {
1027		dev_err(dev, "Unprivileged VF %d is attempting to configure promiscuous mode\n",
1028			vf->vf_id);
1029		/* Leave v_ret alone, lie to the VF on purpose. */
1030		goto error_param;
1031	}
1032
1033	if (info->flags & FLAG_VF_UNICAST_PROMISC)
1034		alluni = true;
1035
1036	if (info->flags & FLAG_VF_MULTICAST_PROMISC)
1037		allmulti = true;
1038
1039	rm_promisc = !allmulti && !alluni;
1040
1041	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
1042	if (rm_promisc)
1043		ret = vlan_ops->ena_rx_filtering(vsi);
1044	else
1045		ret = vlan_ops->dis_rx_filtering(vsi);
1046	if (ret) {
1047		dev_err(dev, "Failed to configure VLAN pruning in promiscuous mode\n");
1048		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1049		goto error_param;
1050	}
1051
1052	ice_vf_get_promisc_masks(vf, vsi, &ucast_m, &mcast_m);
1053
1054	if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
1055		if (alluni) {
1056			/* in this case we're turning on promiscuous mode */
1057			ret = ice_set_dflt_vsi(vsi);
1058		} else {
1059			/* in this case we're turning off promiscuous mode */
1060			if (ice_is_dflt_vsi_in_use(vsi->port_info))
1061				ret = ice_clear_dflt_vsi(vsi);
1062		}
1063
1064		/* in this case we're turning on/off only
1065		 * allmulticast
1066		 */
1067		if (allmulti)
1068			mcast_err = ice_vf_set_vsi_promisc(vf, vsi, mcast_m);
1069		else
1070			mcast_err = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
1071
1072		if (ret) {
1073			dev_err(dev, "Turning on/off promiscuous mode for VF %d failed, error: %d\n",
1074				vf->vf_id, ret);
1075			v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1076			goto error_param;
1077		}
1078	} else {
1079		if (alluni)
1080			ucast_err = ice_vf_set_vsi_promisc(vf, vsi, ucast_m);
1081		else
1082			ucast_err = ice_vf_clear_vsi_promisc(vf, vsi, ucast_m);
1083
1084		if (allmulti)
1085			mcast_err = ice_vf_set_vsi_promisc(vf, vsi, mcast_m);
1086		else
1087			mcast_err = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
1088
1089		if (ucast_err || mcast_err)
1090			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1091	}
1092
1093	if (!mcast_err) {
1094		if (allmulti &&
1095		    !test_and_set_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
1096			dev_info(dev, "VF %u successfully set multicast promiscuous mode\n",
1097				 vf->vf_id);
1098		else if (!allmulti &&
1099			 test_and_clear_bit(ICE_VF_STATE_MC_PROMISC,
1100					    vf->vf_states))
1101			dev_info(dev, "VF %u successfully unset multicast promiscuous mode\n",
1102				 vf->vf_id);
1103	} else {
1104		dev_err(dev, "Error while modifying multicast promiscuous mode for VF %u, error: %d\n",
1105			vf->vf_id, mcast_err);
1106	}
1107
1108	if (!ucast_err) {
1109		if (alluni &&
1110		    !test_and_set_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
1111			dev_info(dev, "VF %u successfully set unicast promiscuous mode\n",
1112				 vf->vf_id);
1113		else if (!alluni &&
1114			 test_and_clear_bit(ICE_VF_STATE_UC_PROMISC,
1115					    vf->vf_states))
1116			dev_info(dev, "VF %u successfully unset unicast promiscuous mode\n",
1117				 vf->vf_id);
1118	} else {
1119		dev_err(dev, "Error while modifying unicast promiscuous mode for VF %u, error: %d\n",
1120			vf->vf_id, ucast_err);
1121	}
1122
1123error_param:
1124	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1125				     v_ret, NULL, 0);
1126}
1127
1128/**
1129 * ice_vc_get_stats_msg
1130 * @vf: pointer to the VF info
1131 * @msg: pointer to the msg buffer
1132 *
1133 * called from the VF to get VSI stats
1134 */
1135static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
1136{
1137	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1138	struct virtchnl_queue_select *vqs =
1139		(struct virtchnl_queue_select *)msg;
1140	struct ice_eth_stats stats = { 0 };
1141	struct ice_vsi *vsi;
1142
1143	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1144		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1145		goto error_param;
1146	}
1147
1148	if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1149		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1150		goto error_param;
1151	}
1152
1153	vsi = ice_get_vf_vsi(vf);
1154	if (!vsi) {
1155		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1156		goto error_param;
1157	}
1158
1159	ice_update_eth_stats(vsi);
1160
1161	stats = vsi->eth_stats;
1162
1163error_param:
1164	/* send the response to the VF */
1165	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
1166				     (u8 *)&stats, sizeof(stats));
1167}
1168
1169/**
1170 * ice_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTCHNL
1171 * @vqs: virtchnl_queue_select structure containing bitmaps to validate
1172 *
1173 * Return true on successful validation, else false
1174 */
1175static bool ice_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
1176{
1177	if ((!vqs->rx_queues && !vqs->tx_queues) ||
1178	    vqs->rx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF) ||
1179	    vqs->tx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF))
1180		return false;
1181
1182	return true;
1183}
1184
1185/**
1186 * ice_vf_ena_txq_interrupt - enable Tx queue interrupt via QINT_TQCTL
1187 * @vsi: VSI of the VF to configure
1188 * @q_idx: VF queue index used to determine the queue in the PF's space
1189 */
1190static void ice_vf_ena_txq_interrupt(struct ice_vsi *vsi, u32 q_idx)
1191{
1192	struct ice_hw *hw = &vsi->back->hw;
1193	u32 pfq = vsi->txq_map[q_idx];
1194	u32 reg;
1195
1196	reg = rd32(hw, QINT_TQCTL(pfq));
1197
1198	/* MSI-X index 0 in the VF's space is always for the OICR, which means
1199	 * this is most likely a poll mode VF driver, so don't enable an
1200	 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
1201	 */
1202	if (!(reg & QINT_TQCTL_MSIX_INDX_M))
1203		return;
1204
1205	wr32(hw, QINT_TQCTL(pfq), reg | QINT_TQCTL_CAUSE_ENA_M);
1206}
1207
1208/**
1209 * ice_vf_ena_rxq_interrupt - enable Tx queue interrupt via QINT_RQCTL
1210 * @vsi: VSI of the VF to configure
1211 * @q_idx: VF queue index used to determine the queue in the PF's space
1212 */
1213static void ice_vf_ena_rxq_interrupt(struct ice_vsi *vsi, u32 q_idx)
1214{
1215	struct ice_hw *hw = &vsi->back->hw;
1216	u32 pfq = vsi->rxq_map[q_idx];
1217	u32 reg;
1218
1219	reg = rd32(hw, QINT_RQCTL(pfq));
1220
1221	/* MSI-X index 0 in the VF's space is always for the OICR, which means
1222	 * this is most likely a poll mode VF driver, so don't enable an
1223	 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
1224	 */
1225	if (!(reg & QINT_RQCTL_MSIX_INDX_M))
1226		return;
1227
1228	wr32(hw, QINT_RQCTL(pfq), reg | QINT_RQCTL_CAUSE_ENA_M);
1229}
1230
1231/**
1232 * ice_vc_ena_qs_msg
1233 * @vf: pointer to the VF info
1234 * @msg: pointer to the msg buffer
1235 *
1236 * called from the VF to enable all or specific queue(s)
1237 */
1238static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
1239{
1240	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1241	struct virtchnl_queue_select *vqs =
1242	    (struct virtchnl_queue_select *)msg;
1243	struct ice_vsi *vsi;
1244	unsigned long q_map;
1245	u16 vf_q_id;
1246
1247	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1248		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1249		goto error_param;
1250	}
1251
1252	if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1253		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1254		goto error_param;
1255	}
1256
1257	if (!ice_vc_validate_vqs_bitmaps(vqs)) {
1258		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1259		goto error_param;
1260	}
1261
1262	vsi = ice_get_vf_vsi(vf);
1263	if (!vsi) {
1264		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1265		goto error_param;
1266	}
1267
1268	/* Enable only Rx rings, Tx rings were enabled by the FW when the
1269	 * Tx queue group list was configured and the context bits were
1270	 * programmed using ice_vsi_cfg_txqs
1271	 */
1272	q_map = vqs->rx_queues;
1273	for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1274		if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
1275			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1276			goto error_param;
1277		}
1278
1279		/* Skip queue if enabled */
1280		if (test_bit(vf_q_id, vf->rxq_ena))
1281			continue;
1282
1283		if (ice_vsi_ctrl_one_rx_ring(vsi, true, vf_q_id, true)) {
1284			dev_err(ice_pf_to_dev(vsi->back), "Failed to enable Rx ring %d on VSI %d\n",
1285				vf_q_id, vsi->vsi_num);
1286			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1287			goto error_param;
1288		}
1289
1290		ice_vf_ena_rxq_interrupt(vsi, vf_q_id);
1291		set_bit(vf_q_id, vf->rxq_ena);
1292	}
1293
1294	q_map = vqs->tx_queues;
1295	for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1296		if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
1297			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1298			goto error_param;
1299		}
1300
1301		/* Skip queue if enabled */
1302		if (test_bit(vf_q_id, vf->txq_ena))
1303			continue;
1304
1305		ice_vf_ena_txq_interrupt(vsi, vf_q_id);
1306		set_bit(vf_q_id, vf->txq_ena);
1307	}
1308
1309	/* Set flag to indicate that queues are enabled */
1310	if (v_ret == VIRTCHNL_STATUS_SUCCESS)
1311		set_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
1312
1313error_param:
1314	/* send the response to the VF */
1315	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
1316				     NULL, 0);
1317}
1318
1319/**
1320 * ice_vf_vsi_dis_single_txq - disable a single Tx queue
1321 * @vf: VF to disable queue for
1322 * @vsi: VSI for the VF
1323 * @q_id: VF relative (0-based) queue ID
1324 *
1325 * Attempt to disable the Tx queue passed in. If the Tx queue was successfully
1326 * disabled then clear q_id bit in the enabled queues bitmap and return
1327 * success. Otherwise return error.
1328 */
1329static int
1330ice_vf_vsi_dis_single_txq(struct ice_vf *vf, struct ice_vsi *vsi, u16 q_id)
1331{
1332	struct ice_txq_meta txq_meta = { 0 };
1333	struct ice_tx_ring *ring;
1334	int err;
1335
1336	if (!test_bit(q_id, vf->txq_ena))
1337		dev_dbg(ice_pf_to_dev(vsi->back), "Queue %u on VSI %u is not enabled, but stopping it anyway\n",
1338			q_id, vsi->vsi_num);
1339
1340	ring = vsi->tx_rings[q_id];
1341	if (!ring)
1342		return -EINVAL;
1343
1344	ice_fill_txq_meta(vsi, ring, &txq_meta);
1345
1346	err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, vf->vf_id, ring, &txq_meta);
1347	if (err) {
1348		dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Tx ring %d on VSI %d\n",
1349			q_id, vsi->vsi_num);
1350		return err;
1351	}
1352
1353	/* Clear enabled queues flag */
1354	clear_bit(q_id, vf->txq_ena);
1355
1356	return 0;
1357}
1358
1359/**
1360 * ice_vc_dis_qs_msg
1361 * @vf: pointer to the VF info
1362 * @msg: pointer to the msg buffer
1363 *
1364 * called from the VF to disable all or specific queue(s)
1365 */
1366static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
1367{
1368	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1369	struct virtchnl_queue_select *vqs =
1370	    (struct virtchnl_queue_select *)msg;
1371	struct ice_vsi *vsi;
1372	unsigned long q_map;
1373	u16 vf_q_id;
1374
1375	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
1376	    !test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states)) {
1377		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1378		goto error_param;
1379	}
1380
1381	if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1382		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1383		goto error_param;
1384	}
1385
1386	if (!ice_vc_validate_vqs_bitmaps(vqs)) {
1387		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1388		goto error_param;
1389	}
1390
1391	vsi = ice_get_vf_vsi(vf);
1392	if (!vsi) {
1393		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1394		goto error_param;
1395	}
1396
1397	if (vqs->tx_queues) {
1398		q_map = vqs->tx_queues;
1399
1400		for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1401			if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
1402				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1403				goto error_param;
1404			}
1405
1406			if (ice_vf_vsi_dis_single_txq(vf, vsi, vf_q_id)) {
1407				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1408				goto error_param;
1409			}
1410		}
1411	}
1412
1413	q_map = vqs->rx_queues;
1414	/* speed up Rx queue disable by batching them if possible */
1415	if (q_map &&
1416	    bitmap_equal(&q_map, vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF)) {
1417		if (ice_vsi_stop_all_rx_rings(vsi)) {
1418			dev_err(ice_pf_to_dev(vsi->back), "Failed to stop all Rx rings on VSI %d\n",
1419				vsi->vsi_num);
1420			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1421			goto error_param;
1422		}
1423
1424		bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
1425	} else if (q_map) {
1426		for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1427			if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
1428				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1429				goto error_param;
1430			}
1431
1432			/* Skip queue if not enabled */
1433			if (!test_bit(vf_q_id, vf->rxq_ena))
1434				continue;
1435
1436			if (ice_vsi_ctrl_one_rx_ring(vsi, false, vf_q_id,
1437						     true)) {
1438				dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Rx ring %d on VSI %d\n",
1439					vf_q_id, vsi->vsi_num);
1440				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1441				goto error_param;
1442			}
1443
1444			/* Clear enabled queues flag */
1445			clear_bit(vf_q_id, vf->rxq_ena);
1446		}
1447	}
1448
1449	/* Clear enabled queues flag */
1450	if (v_ret == VIRTCHNL_STATUS_SUCCESS && ice_vf_has_no_qs_ena(vf))
1451		clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
1452
1453error_param:
1454	/* send the response to the VF */
1455	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
1456				     NULL, 0);
1457}
1458
1459/**
1460 * ice_cfg_interrupt
1461 * @vf: pointer to the VF info
1462 * @vsi: the VSI being configured
1463 * @vector_id: vector ID
1464 * @map: vector map for mapping vectors to queues
1465 * @q_vector: structure for interrupt vector
1466 * configure the IRQ to queue map
1467 */
1468static int
1469ice_cfg_interrupt(struct ice_vf *vf, struct ice_vsi *vsi, u16 vector_id,
1470		  struct virtchnl_vector_map *map,
1471		  struct ice_q_vector *q_vector)
1472{
1473	u16 vsi_q_id, vsi_q_id_idx;
1474	unsigned long qmap;
1475
1476	q_vector->num_ring_rx = 0;
1477	q_vector->num_ring_tx = 0;
1478
1479	qmap = map->rxq_map;
1480	for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
1481		vsi_q_id = vsi_q_id_idx;
1482
1483		if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
1484			return VIRTCHNL_STATUS_ERR_PARAM;
1485
1486		q_vector->num_ring_rx++;
1487		q_vector->rx.itr_idx = map->rxitr_idx;
1488		vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
1489		ice_cfg_rxq_interrupt(vsi, vsi_q_id, vector_id,
1490				      q_vector->rx.itr_idx);
1491	}
1492
1493	qmap = map->txq_map;
1494	for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
1495		vsi_q_id = vsi_q_id_idx;
1496
1497		if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
1498			return VIRTCHNL_STATUS_ERR_PARAM;
1499
1500		q_vector->num_ring_tx++;
1501		q_vector->tx.itr_idx = map->txitr_idx;
1502		vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
1503		ice_cfg_txq_interrupt(vsi, vsi_q_id, vector_id,
1504				      q_vector->tx.itr_idx);
1505	}
1506
1507	return VIRTCHNL_STATUS_SUCCESS;
1508}
1509
1510/**
1511 * ice_vc_cfg_irq_map_msg
1512 * @vf: pointer to the VF info
1513 * @msg: pointer to the msg buffer
1514 *
1515 * called from the VF to configure the IRQ to queue map
1516 */
1517static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
1518{
1519	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1520	u16 num_q_vectors_mapped, vsi_id, vector_id;
1521	struct virtchnl_irq_map_info *irqmap_info;
1522	struct virtchnl_vector_map *map;
1523	struct ice_pf *pf = vf->pf;
1524	struct ice_vsi *vsi;
1525	int i;
1526
1527	irqmap_info = (struct virtchnl_irq_map_info *)msg;
1528	num_q_vectors_mapped = irqmap_info->num_vectors;
1529
1530	/* Check to make sure number of VF vectors mapped is not greater than
1531	 * number of VF vectors originally allocated, and check that
1532	 * there is actually at least a single VF queue vector mapped
1533	 */
1534	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
1535	    pf->vfs.num_msix_per < num_q_vectors_mapped ||
1536	    !num_q_vectors_mapped) {
1537		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1538		goto error_param;
1539	}
1540
1541	vsi = ice_get_vf_vsi(vf);
1542	if (!vsi) {
1543		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1544		goto error_param;
1545	}
1546
1547	for (i = 0; i < num_q_vectors_mapped; i++) {
1548		struct ice_q_vector *q_vector;
1549
1550		map = &irqmap_info->vecmap[i];
1551
1552		vector_id = map->vector_id;
1553		vsi_id = map->vsi_id;
1554		/* vector_id is always 0-based for each VF, and can never be
1555		 * larger than or equal to the max allowed interrupts per VF
1556		 */
1557		if (!(vector_id < pf->vfs.num_msix_per) ||
1558		    !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
1559		    (!vector_id && (map->rxq_map || map->txq_map))) {
1560			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1561			goto error_param;
1562		}
1563
1564		/* No need to map VF miscellaneous or rogue vector */
1565		if (!vector_id)
1566			continue;
1567
1568		/* Subtract non queue vector from vector_id passed by VF
1569		 * to get actual number of VSI queue vector array index
1570		 */
1571		q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
1572		if (!q_vector) {
1573			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1574			goto error_param;
1575		}
1576
1577		/* lookout for the invalid queue index */
1578		v_ret = (enum virtchnl_status_code)
1579			ice_cfg_interrupt(vf, vsi, vector_id, map, q_vector);
1580		if (v_ret)
1581			goto error_param;
1582	}
1583
1584error_param:
1585	/* send the response to the VF */
1586	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
1587				     NULL, 0);
1588}
1589
1590/**
1591 * ice_vc_cfg_qs_msg
1592 * @vf: pointer to the VF info
1593 * @msg: pointer to the msg buffer
1594 *
1595 * called from the VF to configure the Rx/Tx queues
1596 */
1597static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
1598{
1599	struct virtchnl_vsi_queue_config_info *qci =
1600	    (struct virtchnl_vsi_queue_config_info *)msg;
1601	struct virtchnl_queue_pair_info *qpi;
1602	struct ice_pf *pf = vf->pf;
 
1603	struct ice_vsi *vsi;
 
1604	int i = -1, q_idx;
1605
 
 
 
 
 
 
 
 
 
 
 
 
 
1606	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
1607		goto error_param;
1608
1609	if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id))
1610		goto error_param;
1611
1612	vsi = ice_get_vf_vsi(vf);
1613	if (!vsi)
1614		goto error_param;
1615
1616	if (qci->num_queue_pairs > ICE_MAX_RSS_QS_PER_VF ||
1617	    qci->num_queue_pairs > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
1618		dev_err(ice_pf_to_dev(pf), "VF-%d requesting more than supported number of queues: %d\n",
1619			vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
1620		goto error_param;
1621	}
1622
1623	for (i = 0; i < qci->num_queue_pairs; i++) {
 
 
 
 
 
 
 
 
 
1624		qpi = &qci->qpair[i];
1625		if (qpi->txq.vsi_id != qci->vsi_id ||
1626		    qpi->rxq.vsi_id != qci->vsi_id ||
1627		    qpi->rxq.queue_id != qpi->txq.queue_id ||
1628		    qpi->txq.headwb_enabled ||
1629		    !ice_vc_isvalid_ring_len(qpi->txq.ring_len) ||
1630		    !ice_vc_isvalid_ring_len(qpi->rxq.ring_len) ||
1631		    !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
1632			goto error_param;
1633		}
1634
1635		q_idx = qpi->rxq.queue_id;
1636
1637		/* make sure selected "q_idx" is in valid range of queues
1638		 * for selected "vsi"
1639		 */
1640		if (q_idx >= vsi->alloc_txq || q_idx >= vsi->alloc_rxq) {
1641			goto error_param;
1642		}
1643
1644		/* copy Tx queue info from VF into VSI */
1645		if (qpi->txq.ring_len > 0) {
1646			vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
1647			vsi->tx_rings[i]->count = qpi->txq.ring_len;
1648
1649			/* Disable any existing queue first */
1650			if (ice_vf_vsi_dis_single_txq(vf, vsi, q_idx))
1651				goto error_param;
1652
1653			/* Configure a queue with the requested settings */
1654			if (ice_vsi_cfg_single_txq(vsi, vsi->tx_rings, q_idx)) {
1655				dev_warn(ice_pf_to_dev(pf), "VF-%d failed to configure TX queue %d\n",
1656					 vf->vf_id, i);
1657				goto error_param;
1658			}
1659		}
1660
1661		/* copy Rx queue info from VF into VSI */
1662		if (qpi->rxq.ring_len > 0) {
1663			u16 max_frame_size = ice_vc_get_max_frame_size(vf);
1664			u32 rxdid;
1665
1666			vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
1667			vsi->rx_rings[i]->count = qpi->rxq.ring_len;
1668
 
 
 
 
 
 
 
1669			if (qpi->rxq.databuffer_size != 0 &&
1670			    (qpi->rxq.databuffer_size > ((16 * 1024) - 128) ||
1671			     qpi->rxq.databuffer_size < 1024))
1672				goto error_param;
1673			vsi->rx_buf_len = qpi->rxq.databuffer_size;
1674			vsi->rx_rings[i]->rx_buf_len = vsi->rx_buf_len;
1675			if (qpi->rxq.max_pkt_size > max_frame_size ||
1676			    qpi->rxq.max_pkt_size < 64)
1677				goto error_param;
1678
1679			vsi->max_frame = qpi->rxq.max_pkt_size;
1680			/* add space for the port VLAN since the VF driver is
1681			 * not expected to account for it in the MTU
1682			 * calculation
1683			 */
1684			if (ice_vf_is_port_vlan_ena(vf))
1685				vsi->max_frame += VLAN_HLEN;
1686
1687			if (ice_vsi_cfg_single_rxq(vsi, q_idx)) {
1688				dev_warn(ice_pf_to_dev(pf), "VF-%d failed to configure RX queue %d\n",
1689					 vf->vf_id, i);
1690				goto error_param;
1691			}
1692
1693			/* If Rx flex desc is supported, select RXDID for Rx
1694			 * queues. Otherwise, use legacy 32byte descriptor
1695			 * format. Legacy 16byte descriptor is not supported.
1696			 * If this RXDID is selected, return error.
1697			 */
1698			if (vf->driver_caps &
1699			    VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC) {
1700				rxdid = qpi->rxq.rxdid;
1701				if (!(BIT(rxdid) & pf->supported_rxdids))
1702					goto error_param;
1703			} else {
1704				rxdid = ICE_RXDID_LEGACY_1;
1705			}
1706
1707			ice_write_qrxflxp_cntxt(&vsi->back->hw,
1708						vsi->rxq_map[q_idx],
1709						rxdid, 0x03, false);
1710		}
1711	}
1712
 
 
 
 
 
1713	/* send the response to the VF */
1714	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1715				     VIRTCHNL_STATUS_SUCCESS, NULL, 0);
1716error_param:
1717	/* disable whatever we can */
1718	for (; i >= 0; i--) {
1719		if (ice_vsi_ctrl_one_rx_ring(vsi, false, i, true))
1720			dev_err(ice_pf_to_dev(pf), "VF-%d could not disable RX queue %d\n",
1721				vf->vf_id, i);
1722		if (ice_vf_vsi_dis_single_txq(vf, vsi, i))
1723			dev_err(ice_pf_to_dev(pf), "VF-%d could not disable TX queue %d\n",
1724				vf->vf_id, i);
1725	}
1726
 
 
 
 
 
 
 
1727	/* send the response to the VF */
1728	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1729				     VIRTCHNL_STATUS_ERR_PARAM, NULL, 0);
1730}
1731
1732/**
1733 * ice_can_vf_change_mac
1734 * @vf: pointer to the VF info
1735 *
1736 * Return true if the VF is allowed to change its MAC filters, false otherwise
1737 */
1738static bool ice_can_vf_change_mac(struct ice_vf *vf)
1739{
1740	/* If the VF MAC address has been set administratively (via the
1741	 * ndo_set_vf_mac command), then deny permission to the VF to
1742	 * add/delete unicast MAC addresses, unless the VF is trusted
1743	 */
1744	if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
1745		return false;
1746
1747	return true;
1748}
1749
1750/**
1751 * ice_vc_ether_addr_type - get type of virtchnl_ether_addr
1752 * @vc_ether_addr: used to extract the type
1753 */
1754static u8
1755ice_vc_ether_addr_type(struct virtchnl_ether_addr *vc_ether_addr)
1756{
1757	return (vc_ether_addr->type & VIRTCHNL_ETHER_ADDR_TYPE_MASK);
1758}
1759
1760/**
1761 * ice_is_vc_addr_legacy - check if the MAC address is from an older VF
1762 * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
1763 */
1764static bool
1765ice_is_vc_addr_legacy(struct virtchnl_ether_addr *vc_ether_addr)
1766{
1767	u8 type = ice_vc_ether_addr_type(vc_ether_addr);
1768
1769	return (type == VIRTCHNL_ETHER_ADDR_LEGACY);
1770}
1771
1772/**
1773 * ice_is_vc_addr_primary - check if the MAC address is the VF's primary MAC
1774 * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
1775 *
1776 * This function should only be called when the MAC address in
1777 * virtchnl_ether_addr is a valid unicast MAC
1778 */
1779static bool
1780ice_is_vc_addr_primary(struct virtchnl_ether_addr __maybe_unused *vc_ether_addr)
1781{
1782	u8 type = ice_vc_ether_addr_type(vc_ether_addr);
1783
1784	return (type == VIRTCHNL_ETHER_ADDR_PRIMARY);
1785}
1786
1787/**
1788 * ice_vfhw_mac_add - update the VF's cached hardware MAC if allowed
1789 * @vf: VF to update
1790 * @vc_ether_addr: structure from VIRTCHNL with MAC to add
1791 */
1792static void
1793ice_vfhw_mac_add(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
1794{
1795	u8 *mac_addr = vc_ether_addr->addr;
1796
1797	if (!is_valid_ether_addr(mac_addr))
1798		return;
1799
1800	/* only allow legacy VF drivers to set the device and hardware MAC if it
1801	 * is zero and allow new VF drivers to set the hardware MAC if the type
1802	 * was correctly specified over VIRTCHNL
1803	 */
1804	if ((ice_is_vc_addr_legacy(vc_ether_addr) &&
1805	     is_zero_ether_addr(vf->hw_lan_addr.addr)) ||
1806	    ice_is_vc_addr_primary(vc_ether_addr)) {
1807		ether_addr_copy(vf->dev_lan_addr.addr, mac_addr);
1808		ether_addr_copy(vf->hw_lan_addr.addr, mac_addr);
1809	}
1810
1811	/* hardware and device MACs are already set, but its possible that the
1812	 * VF driver sent the VIRTCHNL_OP_ADD_ETH_ADDR message before the
1813	 * VIRTCHNL_OP_DEL_ETH_ADDR when trying to update its MAC, so save it
1814	 * away for the legacy VF driver case as it will be updated in the
1815	 * delete flow for this case
1816	 */
1817	if (ice_is_vc_addr_legacy(vc_ether_addr)) {
1818		ether_addr_copy(vf->legacy_last_added_umac.addr,
1819				mac_addr);
1820		vf->legacy_last_added_umac.time_modified = jiffies;
1821	}
1822}
1823
1824/**
1825 * ice_vc_add_mac_addr - attempt to add the MAC address passed in
1826 * @vf: pointer to the VF info
1827 * @vsi: pointer to the VF's VSI
1828 * @vc_ether_addr: VIRTCHNL MAC address structure used to add MAC
1829 */
1830static int
1831ice_vc_add_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
1832		    struct virtchnl_ether_addr *vc_ether_addr)
1833{
1834	struct device *dev = ice_pf_to_dev(vf->pf);
1835	u8 *mac_addr = vc_ether_addr->addr;
1836	int ret;
1837
1838	/* device MAC already added */
1839	if (ether_addr_equal(mac_addr, vf->dev_lan_addr.addr))
1840		return 0;
1841
1842	if (is_unicast_ether_addr(mac_addr) && !ice_can_vf_change_mac(vf)) {
1843		dev_err(dev, "VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
1844		return -EPERM;
1845	}
1846
1847	ret = ice_fltr_add_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
1848	if (ret == -EEXIST) {
1849		dev_dbg(dev, "MAC %pM already exists for VF %d\n", mac_addr,
1850			vf->vf_id);
1851		/* don't return since we might need to update
1852		 * the primary MAC in ice_vfhw_mac_add() below
1853		 */
1854	} else if (ret) {
1855		dev_err(dev, "Failed to add MAC %pM for VF %d\n, error %d\n",
1856			mac_addr, vf->vf_id, ret);
1857		return ret;
1858	} else {
1859		vf->num_mac++;
1860	}
1861
1862	ice_vfhw_mac_add(vf, vc_ether_addr);
1863
1864	return ret;
1865}
1866
1867/**
1868 * ice_is_legacy_umac_expired - check if last added legacy unicast MAC expired
1869 * @last_added_umac: structure used to check expiration
1870 */
1871static bool ice_is_legacy_umac_expired(struct ice_time_mac *last_added_umac)
1872{
1873#define ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME	msecs_to_jiffies(3000)
1874	return time_is_before_jiffies(last_added_umac->time_modified +
1875				      ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME);
1876}
1877
1878/**
1879 * ice_update_legacy_cached_mac - update cached hardware MAC for legacy VF
1880 * @vf: VF to update
1881 * @vc_ether_addr: structure from VIRTCHNL with MAC to check
1882 *
1883 * only update cached hardware MAC for legacy VF drivers on delete
1884 * because we cannot guarantee order/type of MAC from the VF driver
1885 */
1886static void
1887ice_update_legacy_cached_mac(struct ice_vf *vf,
1888			     struct virtchnl_ether_addr *vc_ether_addr)
1889{
1890	if (!ice_is_vc_addr_legacy(vc_ether_addr) ||
1891	    ice_is_legacy_umac_expired(&vf->legacy_last_added_umac))
1892		return;
1893
1894	ether_addr_copy(vf->dev_lan_addr.addr, vf->legacy_last_added_umac.addr);
1895	ether_addr_copy(vf->hw_lan_addr.addr, vf->legacy_last_added_umac.addr);
1896}
1897
1898/**
1899 * ice_vfhw_mac_del - update the VF's cached hardware MAC if allowed
1900 * @vf: VF to update
1901 * @vc_ether_addr: structure from VIRTCHNL with MAC to delete
1902 */
1903static void
1904ice_vfhw_mac_del(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
1905{
1906	u8 *mac_addr = vc_ether_addr->addr;
1907
1908	if (!is_valid_ether_addr(mac_addr) ||
1909	    !ether_addr_equal(vf->dev_lan_addr.addr, mac_addr))
1910		return;
1911
1912	/* allow the device MAC to be repopulated in the add flow and don't
1913	 * clear the hardware MAC (i.e. hw_lan_addr.addr) here as that is meant
1914	 * to be persistent on VM reboot and across driver unload/load, which
1915	 * won't work if we clear the hardware MAC here
1916	 */
1917	eth_zero_addr(vf->dev_lan_addr.addr);
1918
1919	ice_update_legacy_cached_mac(vf, vc_ether_addr);
1920}
1921
1922/**
1923 * ice_vc_del_mac_addr - attempt to delete the MAC address passed in
1924 * @vf: pointer to the VF info
1925 * @vsi: pointer to the VF's VSI
1926 * @vc_ether_addr: VIRTCHNL MAC address structure used to delete MAC
1927 */
1928static int
1929ice_vc_del_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
1930		    struct virtchnl_ether_addr *vc_ether_addr)
1931{
1932	struct device *dev = ice_pf_to_dev(vf->pf);
1933	u8 *mac_addr = vc_ether_addr->addr;
1934	int status;
1935
1936	if (!ice_can_vf_change_mac(vf) &&
1937	    ether_addr_equal(vf->dev_lan_addr.addr, mac_addr))
1938		return 0;
1939
1940	status = ice_fltr_remove_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
1941	if (status == -ENOENT) {
1942		dev_err(dev, "MAC %pM does not exist for VF %d\n", mac_addr,
1943			vf->vf_id);
1944		return -ENOENT;
1945	} else if (status) {
1946		dev_err(dev, "Failed to delete MAC %pM for VF %d, error %d\n",
1947			mac_addr, vf->vf_id, status);
1948		return -EIO;
1949	}
1950
1951	ice_vfhw_mac_del(vf, vc_ether_addr);
1952
1953	vf->num_mac--;
1954
1955	return 0;
1956}
1957
1958/**
1959 * ice_vc_handle_mac_addr_msg
1960 * @vf: pointer to the VF info
1961 * @msg: pointer to the msg buffer
1962 * @set: true if MAC filters are being set, false otherwise
1963 *
1964 * add guest MAC address filter
1965 */
1966static int
1967ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
1968{
1969	int (*ice_vc_cfg_mac)
1970		(struct ice_vf *vf, struct ice_vsi *vsi,
1971		 struct virtchnl_ether_addr *virtchnl_ether_addr);
1972	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1973	struct virtchnl_ether_addr_list *al =
1974	    (struct virtchnl_ether_addr_list *)msg;
1975	struct ice_pf *pf = vf->pf;
1976	enum virtchnl_ops vc_op;
1977	struct ice_vsi *vsi;
1978	int i;
1979
1980	if (set) {
1981		vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
1982		ice_vc_cfg_mac = ice_vc_add_mac_addr;
1983	} else {
1984		vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
1985		ice_vc_cfg_mac = ice_vc_del_mac_addr;
1986	}
1987
1988	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
1989	    !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
1990		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1991		goto handle_mac_exit;
1992	}
1993
1994	/* If this VF is not privileged, then we can't add more than a
1995	 * limited number of addresses. Check to make sure that the
1996	 * additions do not push us over the limit.
1997	 */
1998	if (set && !ice_is_vf_trusted(vf) &&
1999	    (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
2000		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",
2001			vf->vf_id);
2002		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2003		goto handle_mac_exit;
2004	}
2005
2006	vsi = ice_get_vf_vsi(vf);
2007	if (!vsi) {
2008		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2009		goto handle_mac_exit;
2010	}
2011
2012	for (i = 0; i < al->num_elements; i++) {
2013		u8 *mac_addr = al->list[i].addr;
2014		int result;
2015
2016		if (is_broadcast_ether_addr(mac_addr) ||
2017		    is_zero_ether_addr(mac_addr))
2018			continue;
2019
2020		result = ice_vc_cfg_mac(vf, vsi, &al->list[i]);
2021		if (result == -EEXIST || result == -ENOENT) {
2022			continue;
2023		} else if (result) {
2024			v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2025			goto handle_mac_exit;
2026		}
2027	}
2028
2029handle_mac_exit:
2030	/* send the response to the VF */
2031	return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
2032}
2033
2034/**
2035 * ice_vc_add_mac_addr_msg
2036 * @vf: pointer to the VF info
2037 * @msg: pointer to the msg buffer
2038 *
2039 * add guest MAC address filter
2040 */
2041static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2042{
2043	return ice_vc_handle_mac_addr_msg(vf, msg, true);
2044}
2045
2046/**
2047 * ice_vc_del_mac_addr_msg
2048 * @vf: pointer to the VF info
2049 * @msg: pointer to the msg buffer
2050 *
2051 * remove guest MAC address filter
2052 */
2053static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2054{
2055	return ice_vc_handle_mac_addr_msg(vf, msg, false);
2056}
2057
2058/**
2059 * ice_vc_request_qs_msg
2060 * @vf: pointer to the VF info
2061 * @msg: pointer to the msg buffer
2062 *
2063 * VFs get a default number of queues but can use this message to request a
2064 * different number. If the request is successful, PF will reset the VF and
2065 * return 0. If unsuccessful, PF will send message informing VF of number of
2066 * available queue pairs via virtchnl message response to VF.
2067 */
2068static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
2069{
2070	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2071	struct virtchnl_vf_res_request *vfres =
2072		(struct virtchnl_vf_res_request *)msg;
2073	u16 req_queues = vfres->num_queue_pairs;
2074	struct ice_pf *pf = vf->pf;
2075	u16 max_allowed_vf_queues;
2076	u16 tx_rx_queue_left;
2077	struct device *dev;
2078	u16 cur_queues;
2079
2080	dev = ice_pf_to_dev(pf);
2081	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2082		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2083		goto error_param;
2084	}
2085
2086	cur_queues = vf->num_vf_qs;
2087	tx_rx_queue_left = min_t(u16, ice_get_avail_txq_count(pf),
2088				 ice_get_avail_rxq_count(pf));
2089	max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
2090	if (!req_queues) {
2091		dev_err(dev, "VF %d tried to request 0 queues. Ignoring.\n",
2092			vf->vf_id);
2093	} else if (req_queues > ICE_MAX_RSS_QS_PER_VF) {
2094		dev_err(dev, "VF %d tried to request more than %d queues.\n",
2095			vf->vf_id, ICE_MAX_RSS_QS_PER_VF);
2096		vfres->num_queue_pairs = ICE_MAX_RSS_QS_PER_VF;
2097	} else if (req_queues > cur_queues &&
2098		   req_queues - cur_queues > tx_rx_queue_left) {
2099		dev_warn(dev, "VF %d requested %u more queues, but only %u left.\n",
2100			 vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
2101		vfres->num_queue_pairs = min_t(u16, max_allowed_vf_queues,
2102					       ICE_MAX_RSS_QS_PER_VF);
2103	} else {
2104		/* request is successful, then reset VF */
2105		vf->num_req_qs = req_queues;
2106		ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
2107		dev_info(dev, "VF %d granted request of %u queues.\n",
2108			 vf->vf_id, req_queues);
2109		return 0;
2110	}
2111
2112error_param:
2113	/* send the response to the VF */
2114	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
2115				     v_ret, (u8 *)vfres, sizeof(*vfres));
2116}
2117
2118/**
2119 * ice_vf_vlan_offload_ena - determine if capabilities support VLAN offloads
2120 * @caps: VF driver negotiated capabilities
2121 *
2122 * Return true if VIRTCHNL_VF_OFFLOAD_VLAN capability is set, else return false
2123 */
2124static bool ice_vf_vlan_offload_ena(u32 caps)
2125{
2126	return !!(caps & VIRTCHNL_VF_OFFLOAD_VLAN);
2127}
2128
2129/**
2130 * ice_is_vlan_promisc_allowed - check if VLAN promiscuous config is allowed
2131 * @vf: VF used to determine if VLAN promiscuous config is allowed
2132 */
2133static bool ice_is_vlan_promisc_allowed(struct ice_vf *vf)
2134{
2135	if ((test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2136	     test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) &&
2137	    test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, vf->pf->flags))
2138		return true;
2139
2140	return false;
2141}
2142
2143/**
2144 * ice_vf_ena_vlan_promisc - Enable Tx/Rx VLAN promiscuous for the VLAN
2145 * @vsi: VF's VSI used to enable VLAN promiscuous mode
2146 * @vlan: VLAN used to enable VLAN promiscuous
2147 *
2148 * This function should only be called if VLAN promiscuous mode is allowed,
2149 * which can be determined via ice_is_vlan_promisc_allowed().
2150 */
2151static int ice_vf_ena_vlan_promisc(struct ice_vsi *vsi, struct ice_vlan *vlan)
2152{
2153	u8 promisc_m = ICE_PROMISC_VLAN_TX | ICE_PROMISC_VLAN_RX;
2154	int status;
2155
2156	status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m,
2157					  vlan->vid);
2158	if (status && status != -EEXIST)
2159		return status;
2160
2161	return 0;
2162}
2163
2164/**
2165 * ice_vf_dis_vlan_promisc - Disable Tx/Rx VLAN promiscuous for the VLAN
2166 * @vsi: VF's VSI used to disable VLAN promiscuous mode for
2167 * @vlan: VLAN used to disable VLAN promiscuous
2168 *
2169 * This function should only be called if VLAN promiscuous mode is allowed,
2170 * which can be determined via ice_is_vlan_promisc_allowed().
2171 */
2172static int ice_vf_dis_vlan_promisc(struct ice_vsi *vsi, struct ice_vlan *vlan)
2173{
2174	u8 promisc_m = ICE_PROMISC_VLAN_TX | ICE_PROMISC_VLAN_RX;
2175	int status;
2176
2177	status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m,
2178					    vlan->vid);
2179	if (status && status != -ENOENT)
2180		return status;
2181
2182	return 0;
2183}
2184
2185/**
2186 * ice_vf_has_max_vlans - check if VF already has the max allowed VLAN filters
2187 * @vf: VF to check against
2188 * @vsi: VF's VSI
2189 *
2190 * If the VF is trusted then the VF is allowed to add as many VLANs as it
2191 * wants to, so return false.
2192 *
2193 * When the VF is untrusted compare the number of non-zero VLANs + 1 to the max
2194 * allowed VLANs for an untrusted VF. Return the result of this comparison.
2195 */
2196static bool ice_vf_has_max_vlans(struct ice_vf *vf, struct ice_vsi *vsi)
2197{
2198	if (ice_is_vf_trusted(vf))
2199		return false;
2200
2201#define ICE_VF_ADDED_VLAN_ZERO_FLTRS	1
2202	return ((ice_vsi_num_non_zero_vlans(vsi) +
2203		ICE_VF_ADDED_VLAN_ZERO_FLTRS) >= ICE_MAX_VLAN_PER_VF);
2204}
2205
2206/**
2207 * ice_vc_process_vlan_msg
2208 * @vf: pointer to the VF info
2209 * @msg: pointer to the msg buffer
2210 * @add_v: Add VLAN if true, otherwise delete VLAN
2211 *
2212 * Process virtchnl op to add or remove programmed guest VLAN ID
2213 */
2214static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
2215{
2216	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2217	struct virtchnl_vlan_filter_list *vfl =
2218	    (struct virtchnl_vlan_filter_list *)msg;
2219	struct ice_pf *pf = vf->pf;
2220	bool vlan_promisc = false;
2221	struct ice_vsi *vsi;
2222	struct device *dev;
2223	int status = 0;
2224	int i;
2225
2226	dev = ice_pf_to_dev(pf);
2227	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2228		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2229		goto error_param;
2230	}
2231
2232	if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2233		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2234		goto error_param;
2235	}
2236
2237	if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
2238		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2239		goto error_param;
2240	}
2241
2242	for (i = 0; i < vfl->num_elements; i++) {
2243		if (vfl->vlan_id[i] >= VLAN_N_VID) {
2244			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2245			dev_err(dev, "invalid VF VLAN id %d\n",
2246				vfl->vlan_id[i]);
2247			goto error_param;
2248		}
2249	}
2250
2251	vsi = ice_get_vf_vsi(vf);
2252	if (!vsi) {
2253		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2254		goto error_param;
2255	}
2256
2257	if (add_v && ice_vf_has_max_vlans(vf, vsi)) {
2258		dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2259			 vf->vf_id);
2260		/* There is no need to let VF know about being not trusted,
2261		 * so we can just return success message here
2262		 */
2263		goto error_param;
2264	}
2265
2266	/* in DVM a VF can add/delete inner VLAN filters when
2267	 * VIRTCHNL_VF_OFFLOAD_VLAN is negotiated, so only reject in SVM
2268	 */
2269	if (ice_vf_is_port_vlan_ena(vf) && !ice_is_dvm_ena(&pf->hw)) {
2270		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2271		goto error_param;
2272	}
2273
2274	/* in DVM VLAN promiscuous is based on the outer VLAN, which would be
2275	 * the port VLAN if VIRTCHNL_VF_OFFLOAD_VLAN was negotiated, so only
2276	 * allow vlan_promisc = true in SVM and if no port VLAN is configured
2277	 */
2278	vlan_promisc = ice_is_vlan_promisc_allowed(vf) &&
2279		!ice_is_dvm_ena(&pf->hw) &&
2280		!ice_vf_is_port_vlan_ena(vf);
2281
2282	if (add_v) {
2283		for (i = 0; i < vfl->num_elements; i++) {
2284			u16 vid = vfl->vlan_id[i];
2285			struct ice_vlan vlan;
2286
2287			if (ice_vf_has_max_vlans(vf, vsi)) {
2288				dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2289					 vf->vf_id);
2290				/* There is no need to let VF know about being
2291				 * not trusted, so we can just return success
2292				 * message here as well.
2293				 */
2294				goto error_param;
2295			}
2296
2297			/* we add VLAN 0 by default for each VF so we can enable
2298			 * Tx VLAN anti-spoof without triggering MDD events so
2299			 * we don't need to add it again here
2300			 */
2301			if (!vid)
2302				continue;
2303
2304			vlan = ICE_VLAN(ETH_P_8021Q, vid, 0);
2305			status = vsi->inner_vlan_ops.add_vlan(vsi, &vlan);
2306			if (status) {
2307				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2308				goto error_param;
2309			}
2310
2311			/* Enable VLAN filtering on first non-zero VLAN */
2312			if (!vlan_promisc && vid && !ice_is_dvm_ena(&pf->hw)) {
2313				if (vf->spoofchk) {
2314					status = vsi->inner_vlan_ops.ena_tx_filtering(vsi);
2315					if (status) {
2316						v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2317						dev_err(dev, "Enable VLAN anti-spoofing on VLAN ID: %d failed error-%d\n",
2318							vid, status);
2319						goto error_param;
2320					}
2321				}
2322				if (vsi->inner_vlan_ops.ena_rx_filtering(vsi)) {
2323					v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2324					dev_err(dev, "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
2325						vid, status);
2326					goto error_param;
2327				}
2328			} else if (vlan_promisc) {
2329				status = ice_vf_ena_vlan_promisc(vsi, &vlan);
2330				if (status) {
2331					v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2332					dev_err(dev, "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
2333						vid, status);
2334				}
2335			}
2336		}
2337	} else {
2338		/* In case of non_trusted VF, number of VLAN elements passed
2339		 * to PF for removal might be greater than number of VLANs
2340		 * filter programmed for that VF - So, use actual number of
2341		 * VLANS added earlier with add VLAN opcode. In order to avoid
2342		 * removing VLAN that doesn't exist, which result to sending
2343		 * erroneous failed message back to the VF
2344		 */
2345		int num_vf_vlan;
2346
2347		num_vf_vlan = vsi->num_vlan;
2348		for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
2349			u16 vid = vfl->vlan_id[i];
2350			struct ice_vlan vlan;
2351
2352			/* we add VLAN 0 by default for each VF so we can enable
2353			 * Tx VLAN anti-spoof without triggering MDD events so
2354			 * we don't want a VIRTCHNL request to remove it
2355			 */
2356			if (!vid)
2357				continue;
2358
2359			vlan = ICE_VLAN(ETH_P_8021Q, vid, 0);
2360			status = vsi->inner_vlan_ops.del_vlan(vsi, &vlan);
2361			if (status) {
2362				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2363				goto error_param;
2364			}
2365
2366			/* Disable VLAN filtering when only VLAN 0 is left */
2367			if (!ice_vsi_has_non_zero_vlans(vsi)) {
2368				vsi->inner_vlan_ops.dis_tx_filtering(vsi);
2369				vsi->inner_vlan_ops.dis_rx_filtering(vsi);
2370			}
2371
2372			if (vlan_promisc)
2373				ice_vf_dis_vlan_promisc(vsi, &vlan);
2374		}
2375	}
2376
2377error_param:
2378	/* send the response to the VF */
2379	if (add_v)
2380		return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
2381					     NULL, 0);
2382	else
2383		return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
2384					     NULL, 0);
2385}
2386
2387/**
2388 * ice_vc_add_vlan_msg
2389 * @vf: pointer to the VF info
2390 * @msg: pointer to the msg buffer
2391 *
2392 * Add and program guest VLAN ID
2393 */
2394static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
2395{
2396	return ice_vc_process_vlan_msg(vf, msg, true);
2397}
2398
2399/**
2400 * ice_vc_remove_vlan_msg
2401 * @vf: pointer to the VF info
2402 * @msg: pointer to the msg buffer
2403 *
2404 * remove programmed guest VLAN ID
2405 */
2406static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
2407{
2408	return ice_vc_process_vlan_msg(vf, msg, false);
2409}
2410
2411/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2412 * ice_vc_ena_vlan_stripping
2413 * @vf: pointer to the VF info
2414 *
2415 * Enable VLAN header stripping for a given VF
2416 */
2417static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
2418{
2419	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2420	struct ice_vsi *vsi;
2421
2422	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2423		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2424		goto error_param;
2425	}
2426
2427	if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2428		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2429		goto error_param;
2430	}
2431
2432	vsi = ice_get_vf_vsi(vf);
2433	if (!vsi) {
2434		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2435		goto error_param;
2436	}
2437
2438	if (vsi->inner_vlan_ops.ena_stripping(vsi, ETH_P_8021Q))
2439		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 
 
2440
2441error_param:
2442	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
2443				     v_ret, NULL, 0);
2444}
2445
2446/**
2447 * ice_vc_dis_vlan_stripping
2448 * @vf: pointer to the VF info
2449 *
2450 * Disable VLAN header stripping for a given VF
2451 */
2452static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
2453{
2454	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2455	struct ice_vsi *vsi;
2456
2457	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2458		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2459		goto error_param;
2460	}
2461
2462	if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2463		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2464		goto error_param;
2465	}
2466
2467	vsi = ice_get_vf_vsi(vf);
2468	if (!vsi) {
2469		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2470		goto error_param;
2471	}
2472
2473	if (vsi->inner_vlan_ops.dis_stripping(vsi))
2474		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
 
 
2475
2476error_param:
2477	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
2478				     v_ret, NULL, 0);
2479}
2480
2481/**
2482 * ice_vc_get_rss_hena - return the RSS HENA bits allowed by the hardware
2483 * @vf: pointer to the VF info
2484 */
2485static int ice_vc_get_rss_hena(struct ice_vf *vf)
2486{
2487	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2488	struct virtchnl_rss_hena *vrh = NULL;
2489	int len = 0, ret;
2490
2491	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2492		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2493		goto err;
2494	}
2495
2496	if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2497		dev_err(ice_pf_to_dev(vf->pf), "RSS not supported by PF\n");
2498		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2499		goto err;
2500	}
2501
2502	len = sizeof(struct virtchnl_rss_hena);
2503	vrh = kzalloc(len, GFP_KERNEL);
2504	if (!vrh) {
2505		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2506		len = 0;
2507		goto err;
2508	}
2509
2510	vrh->hena = ICE_DEFAULT_RSS_HENA;
2511err:
2512	/* send the response back to the VF */
2513	ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS, v_ret,
2514				    (u8 *)vrh, len);
2515	kfree(vrh);
2516	return ret;
2517}
2518
2519/**
2520 * ice_vc_set_rss_hena - set RSS HENA bits for the VF
2521 * @vf: pointer to the VF info
2522 * @msg: pointer to the msg buffer
2523 */
2524static int ice_vc_set_rss_hena(struct ice_vf *vf, u8 *msg)
2525{
2526	struct virtchnl_rss_hena *vrh = (struct virtchnl_rss_hena *)msg;
2527	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2528	struct ice_pf *pf = vf->pf;
2529	struct ice_vsi *vsi;
2530	struct device *dev;
2531	int status;
2532
2533	dev = ice_pf_to_dev(pf);
2534
2535	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2536		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2537		goto err;
2538	}
2539
2540	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2541		dev_err(dev, "RSS not supported by PF\n");
2542		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2543		goto err;
2544	}
2545
2546	vsi = ice_get_vf_vsi(vf);
2547	if (!vsi) {
2548		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2549		goto err;
2550	}
2551
2552	/* clear all previously programmed RSS configuration to allow VF drivers
2553	 * the ability to customize the RSS configuration and/or completely
2554	 * disable RSS
2555	 */
2556	status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
2557	if (status && !vrh->hena) {
2558		/* only report failure to clear the current RSS configuration if
2559		 * that was clearly the VF's intention (i.e. vrh->hena = 0)
2560		 */
2561		v_ret = ice_err_to_virt_err(status);
2562		goto err;
2563	} else if (status) {
2564		/* allow the VF to update the RSS configuration even on failure
2565		 * to clear the current RSS confguration in an attempt to keep
2566		 * RSS in a working state
2567		 */
2568		dev_warn(dev, "Failed to clear the RSS configuration for VF %u\n",
2569			 vf->vf_id);
2570	}
2571
2572	if (vrh->hena) {
2573		status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, vrh->hena);
2574		v_ret = ice_err_to_virt_err(status);
2575	}
2576
2577	/* send the response to the VF */
2578err:
2579	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, v_ret,
2580				     NULL, 0);
2581}
2582
2583/**
2584 * ice_vc_query_rxdid - query RXDID supported by DDP package
2585 * @vf: pointer to VF info
2586 *
2587 * Called from VF to query a bitmap of supported flexible
2588 * descriptor RXDIDs of a DDP package.
2589 */
2590static int ice_vc_query_rxdid(struct ice_vf *vf)
2591{
2592	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2593	struct virtchnl_supported_rxdids *rxdid = NULL;
2594	struct ice_hw *hw = &vf->pf->hw;
2595	struct ice_pf *pf = vf->pf;
2596	int len = 0;
2597	int ret, i;
2598	u32 regval;
2599
2600	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2601		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2602		goto err;
2603	}
2604
2605	if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC)) {
2606		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2607		goto err;
2608	}
2609
2610	len = sizeof(struct virtchnl_supported_rxdids);
2611	rxdid = kzalloc(len, GFP_KERNEL);
2612	if (!rxdid) {
2613		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2614		len = 0;
2615		goto err;
2616	}
2617
2618	/* Read flexiflag registers to determine whether the
2619	 * corresponding RXDID is configured and supported or not.
2620	 * Since Legacy 16byte descriptor format is not supported,
2621	 * start from Legacy 32byte descriptor.
2622	 */
2623	for (i = ICE_RXDID_LEGACY_1; i < ICE_FLEX_DESC_RXDID_MAX_NUM; i++) {
 
 
2624		regval = rd32(hw, GLFLXP_RXDID_FLAGS(i, 0));
2625		if ((regval >> GLFLXP_RXDID_FLAGS_FLEXIFLAG_4N_S)
2626			& GLFLXP_RXDID_FLAGS_FLEXIFLAG_4N_M)
2627			rxdid->supported_rxdids |= BIT(i);
2628	}
2629
2630	pf->supported_rxdids = rxdid->supported_rxdids;
2631
2632err:
2633	ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_SUPPORTED_RXDIDS,
2634				    v_ret, (u8 *)rxdid, len);
2635	kfree(rxdid);
2636	return ret;
2637}
2638
2639/**
2640 * ice_vf_init_vlan_stripping - enable/disable VLAN stripping on initialization
2641 * @vf: VF to enable/disable VLAN stripping for on initialization
2642 *
2643 * Set the default for VLAN stripping based on whether a port VLAN is configured
2644 * and the current VLAN mode of the device.
2645 */
2646static int ice_vf_init_vlan_stripping(struct ice_vf *vf)
2647{
2648	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
2649
 
 
2650	if (!vsi)
2651		return -EINVAL;
2652
2653	/* don't modify stripping if port VLAN is configured in SVM since the
2654	 * port VLAN is based on the inner/single VLAN in SVM
2655	 */
2656	if (ice_vf_is_port_vlan_ena(vf) && !ice_is_dvm_ena(&vsi->back->hw))
2657		return 0;
2658
2659	if (ice_vf_vlan_offload_ena(vf->driver_caps))
2660		return vsi->inner_vlan_ops.ena_stripping(vsi, ETH_P_8021Q);
2661	else
2662		return vsi->inner_vlan_ops.dis_stripping(vsi);
 
 
 
 
 
 
2663}
2664
2665static u16 ice_vc_get_max_vlan_fltrs(struct ice_vf *vf)
2666{
2667	if (vf->trusted)
2668		return VLAN_N_VID;
2669	else
2670		return ICE_MAX_VLAN_PER_VF;
2671}
2672
2673/**
2674 * ice_vf_outer_vlan_not_allowed - check if outer VLAN can be used
2675 * @vf: VF that being checked for
2676 *
2677 * When the device is in double VLAN mode, check whether or not the outer VLAN
2678 * is allowed.
2679 */
2680static bool ice_vf_outer_vlan_not_allowed(struct ice_vf *vf)
2681{
2682	if (ice_vf_is_port_vlan_ena(vf))
2683		return true;
2684
2685	return false;
2686}
2687
2688/**
2689 * ice_vc_set_dvm_caps - set VLAN capabilities when the device is in DVM
2690 * @vf: VF that capabilities are being set for
2691 * @caps: VLAN capabilities to populate
2692 *
2693 * Determine VLAN capabilities support based on whether a port VLAN is
2694 * configured. If a port VLAN is configured then the VF should use the inner
2695 * filtering/offload capabilities since the port VLAN is using the outer VLAN
2696 * capabilies.
2697 */
2698static void
2699ice_vc_set_dvm_caps(struct ice_vf *vf, struct virtchnl_vlan_caps *caps)
2700{
2701	struct virtchnl_vlan_supported_caps *supported_caps;
2702
2703	if (ice_vf_outer_vlan_not_allowed(vf)) {
2704		/* until support for inner VLAN filtering is added when a port
2705		 * VLAN is configured, only support software offloaded inner
2706		 * VLANs when a port VLAN is confgured in DVM
2707		 */
2708		supported_caps = &caps->filtering.filtering_support;
2709		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2710
2711		supported_caps = &caps->offloads.stripping_support;
2712		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2713					VIRTCHNL_VLAN_TOGGLE |
2714					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2715		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2716
2717		supported_caps = &caps->offloads.insertion_support;
2718		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2719					VIRTCHNL_VLAN_TOGGLE |
2720					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2721		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2722
2723		caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
2724		caps->offloads.ethertype_match =
2725			VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
2726	} else {
2727		supported_caps = &caps->filtering.filtering_support;
2728		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2729		supported_caps->outer = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2730					VIRTCHNL_VLAN_ETHERTYPE_88A8 |
2731					VIRTCHNL_VLAN_ETHERTYPE_9100 |
2732					VIRTCHNL_VLAN_ETHERTYPE_AND;
2733		caps->filtering.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2734						 VIRTCHNL_VLAN_ETHERTYPE_88A8 |
2735						 VIRTCHNL_VLAN_ETHERTYPE_9100;
2736
2737		supported_caps = &caps->offloads.stripping_support;
2738		supported_caps->inner = VIRTCHNL_VLAN_TOGGLE |
2739					VIRTCHNL_VLAN_ETHERTYPE_8100 |
2740					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2741		supported_caps->outer = VIRTCHNL_VLAN_TOGGLE |
2742					VIRTCHNL_VLAN_ETHERTYPE_8100 |
2743					VIRTCHNL_VLAN_ETHERTYPE_88A8 |
2744					VIRTCHNL_VLAN_ETHERTYPE_9100 |
2745					VIRTCHNL_VLAN_ETHERTYPE_XOR |
2746					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2;
2747
2748		supported_caps = &caps->offloads.insertion_support;
2749		supported_caps->inner = VIRTCHNL_VLAN_TOGGLE |
2750					VIRTCHNL_VLAN_ETHERTYPE_8100 |
2751					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2752		supported_caps->outer = VIRTCHNL_VLAN_TOGGLE |
2753					VIRTCHNL_VLAN_ETHERTYPE_8100 |
2754					VIRTCHNL_VLAN_ETHERTYPE_88A8 |
2755					VIRTCHNL_VLAN_ETHERTYPE_9100 |
2756					VIRTCHNL_VLAN_ETHERTYPE_XOR |
2757					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2;
2758
2759		caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
2760
2761		caps->offloads.ethertype_match =
2762			VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
2763	}
2764
2765	caps->filtering.max_filters = ice_vc_get_max_vlan_fltrs(vf);
2766}
2767
2768/**
2769 * ice_vc_set_svm_caps - set VLAN capabilities when the device is in SVM
2770 * @vf: VF that capabilities are being set for
2771 * @caps: VLAN capabilities to populate
2772 *
2773 * Determine VLAN capabilities support based on whether a port VLAN is
2774 * configured. If a port VLAN is configured then the VF does not have any VLAN
2775 * filtering or offload capabilities since the port VLAN is using the inner VLAN
2776 * capabilities in single VLAN mode (SVM). Otherwise allow the VF to use inner
2777 * VLAN fitlering and offload capabilities.
2778 */
2779static void
2780ice_vc_set_svm_caps(struct ice_vf *vf, struct virtchnl_vlan_caps *caps)
2781{
2782	struct virtchnl_vlan_supported_caps *supported_caps;
2783
2784	if (ice_vf_is_port_vlan_ena(vf)) {
2785		supported_caps = &caps->filtering.filtering_support;
2786		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2787		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2788
2789		supported_caps = &caps->offloads.stripping_support;
2790		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2791		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2792
2793		supported_caps = &caps->offloads.insertion_support;
2794		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2795		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2796
2797		caps->offloads.ethertype_init = VIRTCHNL_VLAN_UNSUPPORTED;
2798		caps->offloads.ethertype_match = VIRTCHNL_VLAN_UNSUPPORTED;
2799		caps->filtering.max_filters = 0;
2800	} else {
2801		supported_caps = &caps->filtering.filtering_support;
2802		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100;
2803		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2804		caps->filtering.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
2805
2806		supported_caps = &caps->offloads.stripping_support;
2807		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2808					VIRTCHNL_VLAN_TOGGLE |
2809					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2810		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2811
2812		supported_caps = &caps->offloads.insertion_support;
2813		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2814					VIRTCHNL_VLAN_TOGGLE |
2815					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2816		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2817
2818		caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
2819		caps->offloads.ethertype_match =
2820			VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
2821		caps->filtering.max_filters = ice_vc_get_max_vlan_fltrs(vf);
2822	}
2823}
2824
2825/**
2826 * ice_vc_get_offload_vlan_v2_caps - determine VF's VLAN capabilities
2827 * @vf: VF to determine VLAN capabilities for
2828 *
2829 * This will only be called if the VF and PF successfully negotiated
2830 * VIRTCHNL_VF_OFFLOAD_VLAN_V2.
2831 *
2832 * Set VLAN capabilities based on the current VLAN mode and whether a port VLAN
2833 * is configured or not.
2834 */
2835static int ice_vc_get_offload_vlan_v2_caps(struct ice_vf *vf)
2836{
2837	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2838	struct virtchnl_vlan_caps *caps = NULL;
2839	int err, len = 0;
2840
2841	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2842		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2843		goto out;
2844	}
2845
2846	caps = kzalloc(sizeof(*caps), GFP_KERNEL);
2847	if (!caps) {
2848		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2849		goto out;
2850	}
2851	len = sizeof(*caps);
2852
2853	if (ice_is_dvm_ena(&vf->pf->hw))
2854		ice_vc_set_dvm_caps(vf, caps);
2855	else
2856		ice_vc_set_svm_caps(vf, caps);
2857
2858	/* store negotiated caps to prevent invalid VF messages */
2859	memcpy(&vf->vlan_v2_caps, caps, sizeof(*caps));
2860
2861out:
2862	err = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS,
2863				    v_ret, (u8 *)caps, len);
2864	kfree(caps);
2865	return err;
2866}
2867
2868/**
2869 * ice_vc_validate_vlan_tpid - validate VLAN TPID
2870 * @filtering_caps: negotiated/supported VLAN filtering capabilities
2871 * @tpid: VLAN TPID used for validation
2872 *
2873 * Convert the VLAN TPID to a VIRTCHNL_VLAN_ETHERTYPE_* and then compare against
2874 * the negotiated/supported filtering caps to see if the VLAN TPID is valid.
2875 */
2876static bool ice_vc_validate_vlan_tpid(u16 filtering_caps, u16 tpid)
2877{
2878	enum virtchnl_vlan_support vlan_ethertype = VIRTCHNL_VLAN_UNSUPPORTED;
2879
2880	switch (tpid) {
2881	case ETH_P_8021Q:
2882		vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_8100;
2883		break;
2884	case ETH_P_8021AD:
2885		vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_88A8;
2886		break;
2887	case ETH_P_QINQ1:
2888		vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_9100;
2889		break;
2890	}
2891
2892	if (!(filtering_caps & vlan_ethertype))
2893		return false;
2894
2895	return true;
2896}
2897
2898/**
2899 * ice_vc_is_valid_vlan - validate the virtchnl_vlan
2900 * @vc_vlan: virtchnl_vlan to validate
2901 *
2902 * If the VLAN TCI and VLAN TPID are 0, then this filter is invalid, so return
2903 * false. Otherwise return true.
2904 */
2905static bool ice_vc_is_valid_vlan(struct virtchnl_vlan *vc_vlan)
2906{
2907	if (!vc_vlan->tci || !vc_vlan->tpid)
2908		return false;
2909
2910	return true;
2911}
2912
2913/**
2914 * ice_vc_validate_vlan_filter_list - validate the filter list from the VF
2915 * @vfc: negotiated/supported VLAN filtering capabilities
2916 * @vfl: VLAN filter list from VF to validate
2917 *
2918 * Validate all of the filters in the VLAN filter list from the VF. If any of
2919 * the checks fail then return false. Otherwise return true.
2920 */
2921static bool
2922ice_vc_validate_vlan_filter_list(struct virtchnl_vlan_filtering_caps *vfc,
2923				 struct virtchnl_vlan_filter_list_v2 *vfl)
2924{
2925	u16 i;
2926
2927	if (!vfl->num_elements)
2928		return false;
2929
2930	for (i = 0; i < vfl->num_elements; i++) {
2931		struct virtchnl_vlan_supported_caps *filtering_support =
2932			&vfc->filtering_support;
2933		struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
2934		struct virtchnl_vlan *outer = &vlan_fltr->outer;
2935		struct virtchnl_vlan *inner = &vlan_fltr->inner;
2936
2937		if ((ice_vc_is_valid_vlan(outer) &&
2938		     filtering_support->outer == VIRTCHNL_VLAN_UNSUPPORTED) ||
2939		    (ice_vc_is_valid_vlan(inner) &&
2940		     filtering_support->inner == VIRTCHNL_VLAN_UNSUPPORTED))
2941			return false;
2942
2943		if ((outer->tci_mask &&
2944		     !(filtering_support->outer & VIRTCHNL_VLAN_FILTER_MASK)) ||
2945		    (inner->tci_mask &&
2946		     !(filtering_support->inner & VIRTCHNL_VLAN_FILTER_MASK)))
2947			return false;
2948
2949		if (((outer->tci & VLAN_PRIO_MASK) &&
2950		     !(filtering_support->outer & VIRTCHNL_VLAN_PRIO)) ||
2951		    ((inner->tci & VLAN_PRIO_MASK) &&
2952		     !(filtering_support->inner & VIRTCHNL_VLAN_PRIO)))
2953			return false;
2954
2955		if ((ice_vc_is_valid_vlan(outer) &&
2956		     !ice_vc_validate_vlan_tpid(filtering_support->outer,
2957						outer->tpid)) ||
2958		    (ice_vc_is_valid_vlan(inner) &&
2959		     !ice_vc_validate_vlan_tpid(filtering_support->inner,
2960						inner->tpid)))
2961			return false;
2962	}
2963
2964	return true;
2965}
2966
2967/**
2968 * ice_vc_to_vlan - transform from struct virtchnl_vlan to struct ice_vlan
2969 * @vc_vlan: struct virtchnl_vlan to transform
2970 */
2971static struct ice_vlan ice_vc_to_vlan(struct virtchnl_vlan *vc_vlan)
2972{
2973	struct ice_vlan vlan = { 0 };
2974
2975	vlan.prio = (vc_vlan->tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
2976	vlan.vid = vc_vlan->tci & VLAN_VID_MASK;
2977	vlan.tpid = vc_vlan->tpid;
2978
2979	return vlan;
2980}
2981
2982/**
2983 * ice_vc_vlan_action - action to perform on the virthcnl_vlan
2984 * @vsi: VF's VSI used to perform the action
2985 * @vlan_action: function to perform the action with (i.e. add/del)
2986 * @vlan: VLAN filter to perform the action with
2987 */
2988static int
2989ice_vc_vlan_action(struct ice_vsi *vsi,
2990		   int (*vlan_action)(struct ice_vsi *, struct ice_vlan *),
2991		   struct ice_vlan *vlan)
2992{
2993	int err;
2994
2995	err = vlan_action(vsi, vlan);
2996	if (err)
2997		return err;
2998
2999	return 0;
3000}
3001
3002/**
3003 * ice_vc_del_vlans - delete VLAN(s) from the virtchnl filter list
3004 * @vf: VF used to delete the VLAN(s)
3005 * @vsi: VF's VSI used to delete the VLAN(s)
3006 * @vfl: virthchnl filter list used to delete the filters
3007 */
3008static int
3009ice_vc_del_vlans(struct ice_vf *vf, struct ice_vsi *vsi,
3010		 struct virtchnl_vlan_filter_list_v2 *vfl)
3011{
3012	bool vlan_promisc = ice_is_vlan_promisc_allowed(vf);
3013	int err;
3014	u16 i;
3015
3016	for (i = 0; i < vfl->num_elements; i++) {
3017		struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
3018		struct virtchnl_vlan *vc_vlan;
3019
3020		vc_vlan = &vlan_fltr->outer;
3021		if (ice_vc_is_valid_vlan(vc_vlan)) {
3022			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3023
3024			err = ice_vc_vlan_action(vsi,
3025						 vsi->outer_vlan_ops.del_vlan,
3026						 &vlan);
3027			if (err)
3028				return err;
3029
3030			if (vlan_promisc)
3031				ice_vf_dis_vlan_promisc(vsi, &vlan);
3032
3033			/* Disable VLAN filtering when only VLAN 0 is left */
3034			if (!ice_vsi_has_non_zero_vlans(vsi) && ice_is_dvm_ena(&vsi->back->hw)) {
3035				err = vsi->outer_vlan_ops.dis_tx_filtering(vsi);
3036				if (err)
3037					return err;
3038			}
3039		}
3040
3041		vc_vlan = &vlan_fltr->inner;
3042		if (ice_vc_is_valid_vlan(vc_vlan)) {
3043			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3044
3045			err = ice_vc_vlan_action(vsi,
3046						 vsi->inner_vlan_ops.del_vlan,
3047						 &vlan);
3048			if (err)
3049				return err;
3050
3051			/* no support for VLAN promiscuous on inner VLAN unless
3052			 * we are in Single VLAN Mode (SVM)
3053			 */
3054			if (!ice_is_dvm_ena(&vsi->back->hw)) {
3055				if (vlan_promisc)
3056					ice_vf_dis_vlan_promisc(vsi, &vlan);
3057
3058				/* Disable VLAN filtering when only VLAN 0 is left */
3059				if (!ice_vsi_has_non_zero_vlans(vsi)) {
3060					err = vsi->inner_vlan_ops.dis_tx_filtering(vsi);
3061					if (err)
3062						return err;
3063				}
3064			}
3065		}
3066	}
3067
3068	return 0;
3069}
3070
3071/**
3072 * ice_vc_remove_vlan_v2_msg - virtchnl handler for VIRTCHNL_OP_DEL_VLAN_V2
3073 * @vf: VF the message was received from
3074 * @msg: message received from the VF
3075 */
3076static int ice_vc_remove_vlan_v2_msg(struct ice_vf *vf, u8 *msg)
3077{
3078	struct virtchnl_vlan_filter_list_v2 *vfl =
3079		(struct virtchnl_vlan_filter_list_v2 *)msg;
3080	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3081	struct ice_vsi *vsi;
3082
3083	if (!ice_vc_validate_vlan_filter_list(&vf->vlan_v2_caps.filtering,
3084					      vfl)) {
3085		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3086		goto out;
3087	}
3088
3089	if (!ice_vc_isvalid_vsi_id(vf, vfl->vport_id)) {
3090		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3091		goto out;
3092	}
3093
3094	vsi = ice_get_vf_vsi(vf);
3095	if (!vsi) {
3096		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3097		goto out;
3098	}
3099
3100	if (ice_vc_del_vlans(vf, vsi, vfl))
3101		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3102
3103out:
3104	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN_V2, v_ret, NULL,
3105				     0);
3106}
3107
3108/**
3109 * ice_vc_add_vlans - add VLAN(s) from the virtchnl filter list
3110 * @vf: VF used to add the VLAN(s)
3111 * @vsi: VF's VSI used to add the VLAN(s)
3112 * @vfl: virthchnl filter list used to add the filters
3113 */
3114static int
3115ice_vc_add_vlans(struct ice_vf *vf, struct ice_vsi *vsi,
3116		 struct virtchnl_vlan_filter_list_v2 *vfl)
3117{
3118	bool vlan_promisc = ice_is_vlan_promisc_allowed(vf);
3119	int err;
3120	u16 i;
3121
3122	for (i = 0; i < vfl->num_elements; i++) {
3123		struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
3124		struct virtchnl_vlan *vc_vlan;
3125
3126		vc_vlan = &vlan_fltr->outer;
3127		if (ice_vc_is_valid_vlan(vc_vlan)) {
3128			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3129
3130			err = ice_vc_vlan_action(vsi,
3131						 vsi->outer_vlan_ops.add_vlan,
3132						 &vlan);
3133			if (err)
3134				return err;
3135
3136			if (vlan_promisc) {
3137				err = ice_vf_ena_vlan_promisc(vsi, &vlan);
3138				if (err)
3139					return err;
3140			}
3141
3142			/* Enable VLAN filtering on first non-zero VLAN */
3143			if (vf->spoofchk && vlan.vid && ice_is_dvm_ena(&vsi->back->hw)) {
3144				err = vsi->outer_vlan_ops.ena_tx_filtering(vsi);
3145				if (err)
3146					return err;
3147			}
3148		}
3149
3150		vc_vlan = &vlan_fltr->inner;
3151		if (ice_vc_is_valid_vlan(vc_vlan)) {
3152			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3153
3154			err = ice_vc_vlan_action(vsi,
3155						 vsi->inner_vlan_ops.add_vlan,
3156						 &vlan);
3157			if (err)
3158				return err;
3159
3160			/* no support for VLAN promiscuous on inner VLAN unless
3161			 * we are in Single VLAN Mode (SVM)
3162			 */
3163			if (!ice_is_dvm_ena(&vsi->back->hw)) {
3164				if (vlan_promisc) {
3165					err = ice_vf_ena_vlan_promisc(vsi, &vlan);
3166					if (err)
3167						return err;
3168				}
3169
3170				/* Enable VLAN filtering on first non-zero VLAN */
3171				if (vf->spoofchk && vlan.vid) {
3172					err = vsi->inner_vlan_ops.ena_tx_filtering(vsi);
3173					if (err)
3174						return err;
3175				}
3176			}
3177		}
3178	}
3179
3180	return 0;
3181}
3182
3183/**
3184 * ice_vc_validate_add_vlan_filter_list - validate add filter list from the VF
3185 * @vsi: VF VSI used to get number of existing VLAN filters
3186 * @vfc: negotiated/supported VLAN filtering capabilities
3187 * @vfl: VLAN filter list from VF to validate
3188 *
3189 * Validate all of the filters in the VLAN filter list from the VF during the
3190 * VIRTCHNL_OP_ADD_VLAN_V2 opcode. If any of the checks fail then return false.
3191 * Otherwise return true.
3192 */
3193static bool
3194ice_vc_validate_add_vlan_filter_list(struct ice_vsi *vsi,
3195				     struct virtchnl_vlan_filtering_caps *vfc,
3196				     struct virtchnl_vlan_filter_list_v2 *vfl)
3197{
3198	u16 num_requested_filters = ice_vsi_num_non_zero_vlans(vsi) +
3199		vfl->num_elements;
3200
3201	if (num_requested_filters > vfc->max_filters)
3202		return false;
3203
3204	return ice_vc_validate_vlan_filter_list(vfc, vfl);
3205}
3206
3207/**
3208 * ice_vc_add_vlan_v2_msg - virtchnl handler for VIRTCHNL_OP_ADD_VLAN_V2
3209 * @vf: VF the message was received from
3210 * @msg: message received from the VF
3211 */
3212static int ice_vc_add_vlan_v2_msg(struct ice_vf *vf, u8 *msg)
3213{
3214	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3215	struct virtchnl_vlan_filter_list_v2 *vfl =
3216		(struct virtchnl_vlan_filter_list_v2 *)msg;
3217	struct ice_vsi *vsi;
3218
3219	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3220		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3221		goto out;
3222	}
3223
3224	if (!ice_vc_isvalid_vsi_id(vf, vfl->vport_id)) {
3225		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3226		goto out;
3227	}
3228
3229	vsi = ice_get_vf_vsi(vf);
3230	if (!vsi) {
3231		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3232		goto out;
3233	}
3234
3235	if (!ice_vc_validate_add_vlan_filter_list(vsi,
3236						  &vf->vlan_v2_caps.filtering,
3237						  vfl)) {
3238		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3239		goto out;
3240	}
3241
3242	if (ice_vc_add_vlans(vf, vsi, vfl))
3243		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3244
3245out:
3246	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN_V2, v_ret, NULL,
3247				     0);
3248}
3249
3250/**
3251 * ice_vc_valid_vlan_setting - validate VLAN setting
3252 * @negotiated_settings: negotiated VLAN settings during VF init
3253 * @ethertype_setting: ethertype(s) requested for the VLAN setting
3254 */
3255static bool
3256ice_vc_valid_vlan_setting(u32 negotiated_settings, u32 ethertype_setting)
3257{
3258	if (ethertype_setting && !(negotiated_settings & ethertype_setting))
3259		return false;
3260
3261	/* only allow a single VIRTCHNL_VLAN_ETHERTYPE if
3262	 * VIRTHCNL_VLAN_ETHERTYPE_AND is not negotiated/supported
3263	 */
3264	if (!(negotiated_settings & VIRTCHNL_VLAN_ETHERTYPE_AND) &&
3265	    hweight32(ethertype_setting) > 1)
3266		return false;
3267
3268	/* ability to modify the VLAN setting was not negotiated */
3269	if (!(negotiated_settings & VIRTCHNL_VLAN_TOGGLE))
3270		return false;
3271
3272	return true;
3273}
3274
3275/**
3276 * ice_vc_valid_vlan_setting_msg - validate the VLAN setting message
3277 * @caps: negotiated VLAN settings during VF init
3278 * @msg: message to validate
3279 *
3280 * Used to validate any VLAN virtchnl message sent as a
3281 * virtchnl_vlan_setting structure. Validates the message against the
3282 * negotiated/supported caps during VF driver init.
3283 */
3284static bool
3285ice_vc_valid_vlan_setting_msg(struct virtchnl_vlan_supported_caps *caps,
3286			      struct virtchnl_vlan_setting *msg)
3287{
3288	if ((!msg->outer_ethertype_setting &&
3289	     !msg->inner_ethertype_setting) ||
3290	    (!caps->outer && !caps->inner))
3291		return false;
3292
3293	if (msg->outer_ethertype_setting &&
3294	    !ice_vc_valid_vlan_setting(caps->outer,
3295				       msg->outer_ethertype_setting))
3296		return false;
3297
3298	if (msg->inner_ethertype_setting &&
3299	    !ice_vc_valid_vlan_setting(caps->inner,
3300				       msg->inner_ethertype_setting))
3301		return false;
3302
3303	return true;
3304}
3305
3306/**
3307 * ice_vc_get_tpid - transform from VIRTCHNL_VLAN_ETHERTYPE_* to VLAN TPID
3308 * @ethertype_setting: VIRTCHNL_VLAN_ETHERTYPE_* used to get VLAN TPID
3309 * @tpid: VLAN TPID to populate
3310 */
3311static int ice_vc_get_tpid(u32 ethertype_setting, u16 *tpid)
3312{
3313	switch (ethertype_setting) {
3314	case VIRTCHNL_VLAN_ETHERTYPE_8100:
3315		*tpid = ETH_P_8021Q;
3316		break;
3317	case VIRTCHNL_VLAN_ETHERTYPE_88A8:
3318		*tpid = ETH_P_8021AD;
3319		break;
3320	case VIRTCHNL_VLAN_ETHERTYPE_9100:
3321		*tpid = ETH_P_QINQ1;
3322		break;
3323	default:
3324		*tpid = 0;
3325		return -EINVAL;
3326	}
3327
3328	return 0;
3329}
3330
3331/**
3332 * ice_vc_ena_vlan_offload - enable VLAN offload based on the ethertype_setting
3333 * @vsi: VF's VSI used to enable the VLAN offload
3334 * @ena_offload: function used to enable the VLAN offload
3335 * @ethertype_setting: VIRTCHNL_VLAN_ETHERTYPE_* to enable offloads for
3336 */
3337static int
3338ice_vc_ena_vlan_offload(struct ice_vsi *vsi,
3339			int (*ena_offload)(struct ice_vsi *vsi, u16 tpid),
3340			u32 ethertype_setting)
3341{
3342	u16 tpid;
3343	int err;
3344
3345	err = ice_vc_get_tpid(ethertype_setting, &tpid);
3346	if (err)
3347		return err;
3348
3349	err = ena_offload(vsi, tpid);
3350	if (err)
3351		return err;
3352
3353	return 0;
3354}
3355
3356#define ICE_L2TSEL_QRX_CONTEXT_REG_IDX	3
3357#define ICE_L2TSEL_BIT_OFFSET		23
3358enum ice_l2tsel {
3359	ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND,
3360	ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG1,
3361};
3362
3363/**
3364 * ice_vsi_update_l2tsel - update l2tsel field for all Rx rings on this VSI
3365 * @vsi: VSI used to update l2tsel on
3366 * @l2tsel: l2tsel setting requested
3367 *
3368 * Use the l2tsel setting to update all of the Rx queue context bits for l2tsel.
3369 * This will modify which descriptor field the first offloaded VLAN will be
3370 * stripped into.
3371 */
3372static void ice_vsi_update_l2tsel(struct ice_vsi *vsi, enum ice_l2tsel l2tsel)
3373{
3374	struct ice_hw *hw = &vsi->back->hw;
3375	u32 l2tsel_bit;
3376	int i;
3377
3378	if (l2tsel == ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND)
3379		l2tsel_bit = 0;
3380	else
3381		l2tsel_bit = BIT(ICE_L2TSEL_BIT_OFFSET);
3382
3383	for (i = 0; i < vsi->alloc_rxq; i++) {
3384		u16 pfq = vsi->rxq_map[i];
3385		u32 qrx_context_offset;
3386		u32 regval;
3387
3388		qrx_context_offset =
3389			QRX_CONTEXT(ICE_L2TSEL_QRX_CONTEXT_REG_IDX, pfq);
3390
3391		regval = rd32(hw, qrx_context_offset);
3392		regval &= ~BIT(ICE_L2TSEL_BIT_OFFSET);
3393		regval |= l2tsel_bit;
3394		wr32(hw, qrx_context_offset, regval);
3395	}
3396}
3397
3398/**
3399 * ice_vc_ena_vlan_stripping_v2_msg
3400 * @vf: VF the message was received from
3401 * @msg: message received from the VF
3402 *
3403 * virthcnl handler for VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2
3404 */
3405static int ice_vc_ena_vlan_stripping_v2_msg(struct ice_vf *vf, u8 *msg)
3406{
3407	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3408	struct virtchnl_vlan_supported_caps *stripping_support;
3409	struct virtchnl_vlan_setting *strip_msg =
3410		(struct virtchnl_vlan_setting *)msg;
3411	u32 ethertype_setting;
3412	struct ice_vsi *vsi;
3413
3414	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3415		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3416		goto out;
3417	}
3418
3419	if (!ice_vc_isvalid_vsi_id(vf, strip_msg->vport_id)) {
3420		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3421		goto out;
3422	}
3423
3424	vsi = ice_get_vf_vsi(vf);
3425	if (!vsi) {
3426		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3427		goto out;
3428	}
3429
3430	stripping_support = &vf->vlan_v2_caps.offloads.stripping_support;
3431	if (!ice_vc_valid_vlan_setting_msg(stripping_support, strip_msg)) {
3432		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3433		goto out;
3434	}
3435
 
 
 
 
 
3436	ethertype_setting = strip_msg->outer_ethertype_setting;
3437	if (ethertype_setting) {
3438		if (ice_vc_ena_vlan_offload(vsi,
3439					    vsi->outer_vlan_ops.ena_stripping,
3440					    ethertype_setting)) {
3441			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3442			goto out;
3443		} else {
3444			enum ice_l2tsel l2tsel =
3445				ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND;
3446
3447			/* PF tells the VF that the outer VLAN tag is always
3448			 * extracted to VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2 and
3449			 * inner is always extracted to
3450			 * VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1. This is needed to
3451			 * support outer stripping so the first tag always ends
3452			 * up in L2TAG2_2ND and the second/inner tag, if
3453			 * enabled, is extracted in L2TAG1.
3454			 */
3455			ice_vsi_update_l2tsel(vsi, l2tsel);
 
 
3456		}
3457	}
3458
3459	ethertype_setting = strip_msg->inner_ethertype_setting;
3460	if (ethertype_setting &&
3461	    ice_vc_ena_vlan_offload(vsi, vsi->inner_vlan_ops.ena_stripping,
3462				    ethertype_setting)) {
3463		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3464		goto out;
3465	}
3466
 
 
 
3467out:
3468	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2,
3469				     v_ret, NULL, 0);
3470}
3471
3472/**
3473 * ice_vc_dis_vlan_stripping_v2_msg
3474 * @vf: VF the message was received from
3475 * @msg: message received from the VF
3476 *
3477 * virthcnl handler for VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2
3478 */
3479static int ice_vc_dis_vlan_stripping_v2_msg(struct ice_vf *vf, u8 *msg)
3480{
3481	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3482	struct virtchnl_vlan_supported_caps *stripping_support;
3483	struct virtchnl_vlan_setting *strip_msg =
3484		(struct virtchnl_vlan_setting *)msg;
3485	u32 ethertype_setting;
3486	struct ice_vsi *vsi;
3487
3488	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3489		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3490		goto out;
3491	}
3492
3493	if (!ice_vc_isvalid_vsi_id(vf, strip_msg->vport_id)) {
3494		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3495		goto out;
3496	}
3497
3498	vsi = ice_get_vf_vsi(vf);
3499	if (!vsi) {
3500		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3501		goto out;
3502	}
3503
3504	stripping_support = &vf->vlan_v2_caps.offloads.stripping_support;
3505	if (!ice_vc_valid_vlan_setting_msg(stripping_support, strip_msg)) {
3506		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3507		goto out;
3508	}
3509
3510	ethertype_setting = strip_msg->outer_ethertype_setting;
3511	if (ethertype_setting) {
3512		if (vsi->outer_vlan_ops.dis_stripping(vsi)) {
3513			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3514			goto out;
3515		} else {
3516			enum ice_l2tsel l2tsel =
3517				ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG1;
3518
3519			/* PF tells the VF that the outer VLAN tag is always
3520			 * extracted to VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2 and
3521			 * inner is always extracted to
3522			 * VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1. This is needed to
3523			 * support inner stripping while outer stripping is
3524			 * disabled so that the first and only tag is extracted
3525			 * in L2TAG1.
3526			 */
3527			ice_vsi_update_l2tsel(vsi, l2tsel);
 
 
3528		}
3529	}
3530
3531	ethertype_setting = strip_msg->inner_ethertype_setting;
3532	if (ethertype_setting && vsi->inner_vlan_ops.dis_stripping(vsi)) {
3533		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3534		goto out;
3535	}
3536
 
 
 
3537out:
3538	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2,
3539				     v_ret, NULL, 0);
3540}
3541
3542/**
3543 * ice_vc_ena_vlan_insertion_v2_msg
3544 * @vf: VF the message was received from
3545 * @msg: message received from the VF
3546 *
3547 * virthcnl handler for VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2
3548 */
3549static int ice_vc_ena_vlan_insertion_v2_msg(struct ice_vf *vf, u8 *msg)
3550{
3551	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3552	struct virtchnl_vlan_supported_caps *insertion_support;
3553	struct virtchnl_vlan_setting *insertion_msg =
3554		(struct virtchnl_vlan_setting *)msg;
3555	u32 ethertype_setting;
3556	struct ice_vsi *vsi;
3557
3558	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3559		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3560		goto out;
3561	}
3562
3563	if (!ice_vc_isvalid_vsi_id(vf, insertion_msg->vport_id)) {
3564		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3565		goto out;
3566	}
3567
3568	vsi = ice_get_vf_vsi(vf);
3569	if (!vsi) {
3570		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3571		goto out;
3572	}
3573
3574	insertion_support = &vf->vlan_v2_caps.offloads.insertion_support;
3575	if (!ice_vc_valid_vlan_setting_msg(insertion_support, insertion_msg)) {
3576		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3577		goto out;
3578	}
3579
3580	ethertype_setting = insertion_msg->outer_ethertype_setting;
3581	if (ethertype_setting &&
3582	    ice_vc_ena_vlan_offload(vsi, vsi->outer_vlan_ops.ena_insertion,
3583				    ethertype_setting)) {
3584		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3585		goto out;
3586	}
3587
3588	ethertype_setting = insertion_msg->inner_ethertype_setting;
3589	if (ethertype_setting &&
3590	    ice_vc_ena_vlan_offload(vsi, vsi->inner_vlan_ops.ena_insertion,
3591				    ethertype_setting)) {
3592		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3593		goto out;
3594	}
3595
3596out:
3597	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2,
3598				     v_ret, NULL, 0);
3599}
3600
3601/**
3602 * ice_vc_dis_vlan_insertion_v2_msg
3603 * @vf: VF the message was received from
3604 * @msg: message received from the VF
3605 *
3606 * virthcnl handler for VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2
3607 */
3608static int ice_vc_dis_vlan_insertion_v2_msg(struct ice_vf *vf, u8 *msg)
3609{
3610	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3611	struct virtchnl_vlan_supported_caps *insertion_support;
3612	struct virtchnl_vlan_setting *insertion_msg =
3613		(struct virtchnl_vlan_setting *)msg;
3614	u32 ethertype_setting;
3615	struct ice_vsi *vsi;
3616
3617	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3618		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3619		goto out;
3620	}
3621
3622	if (!ice_vc_isvalid_vsi_id(vf, insertion_msg->vport_id)) {
3623		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3624		goto out;
3625	}
3626
3627	vsi = ice_get_vf_vsi(vf);
3628	if (!vsi) {
3629		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3630		goto out;
3631	}
3632
3633	insertion_support = &vf->vlan_v2_caps.offloads.insertion_support;
3634	if (!ice_vc_valid_vlan_setting_msg(insertion_support, insertion_msg)) {
3635		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3636		goto out;
3637	}
3638
3639	ethertype_setting = insertion_msg->outer_ethertype_setting;
3640	if (ethertype_setting && vsi->outer_vlan_ops.dis_insertion(vsi)) {
3641		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3642		goto out;
3643	}
3644
3645	ethertype_setting = insertion_msg->inner_ethertype_setting;
3646	if (ethertype_setting && vsi->inner_vlan_ops.dis_insertion(vsi)) {
3647		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3648		goto out;
3649	}
3650
3651out:
3652	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2,
3653				     v_ret, NULL, 0);
3654}
3655
3656static const struct ice_virtchnl_ops ice_virtchnl_dflt_ops = {
3657	.get_ver_msg = ice_vc_get_ver_msg,
3658	.get_vf_res_msg = ice_vc_get_vf_res_msg,
3659	.reset_vf = ice_vc_reset_vf_msg,
3660	.add_mac_addr_msg = ice_vc_add_mac_addr_msg,
3661	.del_mac_addr_msg = ice_vc_del_mac_addr_msg,
3662	.cfg_qs_msg = ice_vc_cfg_qs_msg,
3663	.ena_qs_msg = ice_vc_ena_qs_msg,
3664	.dis_qs_msg = ice_vc_dis_qs_msg,
3665	.request_qs_msg = ice_vc_request_qs_msg,
3666	.cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
3667	.config_rss_key = ice_vc_config_rss_key,
3668	.config_rss_lut = ice_vc_config_rss_lut,
 
3669	.get_stats_msg = ice_vc_get_stats_msg,
3670	.cfg_promiscuous_mode_msg = ice_vc_cfg_promiscuous_mode_msg,
3671	.add_vlan_msg = ice_vc_add_vlan_msg,
3672	.remove_vlan_msg = ice_vc_remove_vlan_msg,
3673	.query_rxdid = ice_vc_query_rxdid,
3674	.get_rss_hena = ice_vc_get_rss_hena,
3675	.set_rss_hena_msg = ice_vc_set_rss_hena,
3676	.ena_vlan_stripping = ice_vc_ena_vlan_stripping,
3677	.dis_vlan_stripping = ice_vc_dis_vlan_stripping,
3678	.handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
3679	.add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
3680	.del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
3681	.get_offload_vlan_v2_caps = ice_vc_get_offload_vlan_v2_caps,
3682	.add_vlan_v2_msg = ice_vc_add_vlan_v2_msg,
3683	.remove_vlan_v2_msg = ice_vc_remove_vlan_v2_msg,
3684	.ena_vlan_stripping_v2_msg = ice_vc_ena_vlan_stripping_v2_msg,
3685	.dis_vlan_stripping_v2_msg = ice_vc_dis_vlan_stripping_v2_msg,
3686	.ena_vlan_insertion_v2_msg = ice_vc_ena_vlan_insertion_v2_msg,
3687	.dis_vlan_insertion_v2_msg = ice_vc_dis_vlan_insertion_v2_msg,
3688};
3689
3690/**
3691 * ice_virtchnl_set_dflt_ops - Switch to default virtchnl ops
3692 * @vf: the VF to switch ops
3693 */
3694void ice_virtchnl_set_dflt_ops(struct ice_vf *vf)
3695{
3696	vf->virtchnl_ops = &ice_virtchnl_dflt_ops;
3697}
3698
3699/**
3700 * ice_vc_repr_add_mac
3701 * @vf: pointer to VF
3702 * @msg: virtchannel message
3703 *
3704 * When port representors are created, we do not add MAC rule
3705 * to firmware, we store it so that PF could report same
3706 * MAC as VF.
3707 */
3708static int ice_vc_repr_add_mac(struct ice_vf *vf, u8 *msg)
3709{
3710	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3711	struct virtchnl_ether_addr_list *al =
3712	    (struct virtchnl_ether_addr_list *)msg;
3713	struct ice_vsi *vsi;
3714	struct ice_pf *pf;
3715	int i;
3716
3717	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
3718	    !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
3719		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3720		goto handle_mac_exit;
3721	}
3722
3723	pf = vf->pf;
3724
3725	vsi = ice_get_vf_vsi(vf);
3726	if (!vsi) {
3727		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3728		goto handle_mac_exit;
3729	}
3730
3731	for (i = 0; i < al->num_elements; i++) {
3732		u8 *mac_addr = al->list[i].addr;
3733		int result;
3734
3735		if (!is_unicast_ether_addr(mac_addr) ||
3736		    ether_addr_equal(mac_addr, vf->hw_lan_addr.addr))
3737			continue;
3738
3739		if (vf->pf_set_mac) {
3740			dev_err(ice_pf_to_dev(pf), "VF attempting to override administratively set MAC address\n");
3741			v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
3742			goto handle_mac_exit;
3743		}
3744
3745		result = ice_eswitch_add_vf_mac_rule(pf, vf, mac_addr);
3746		if (result) {
3747			dev_err(ice_pf_to_dev(pf), "Failed to add MAC %pM for VF %d\n, error %d\n",
3748				mac_addr, vf->vf_id, result);
3749			goto handle_mac_exit;
3750		}
3751
3752		ice_vfhw_mac_add(vf, &al->list[i]);
3753		vf->num_mac++;
3754		break;
3755	}
3756
3757handle_mac_exit:
3758	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
3759				     v_ret, NULL, 0);
3760}
3761
3762/**
3763 * ice_vc_repr_del_mac - response with success for deleting MAC
3764 * @vf: pointer to VF
3765 * @msg: virtchannel message
3766 *
3767 * Respond with success to not break normal VF flow.
3768 * For legacy VF driver try to update cached MAC address.
3769 */
3770static int
3771ice_vc_repr_del_mac(struct ice_vf __always_unused *vf, u8 __always_unused *msg)
3772{
3773	struct virtchnl_ether_addr_list *al =
3774		(struct virtchnl_ether_addr_list *)msg;
3775
3776	ice_update_legacy_cached_mac(vf, &al->list[0]);
3777
3778	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
3779				     VIRTCHNL_STATUS_SUCCESS, NULL, 0);
3780}
3781
3782static int
3783ice_vc_repr_cfg_promiscuous_mode(struct ice_vf *vf, u8 __always_unused *msg)
3784{
3785	dev_dbg(ice_pf_to_dev(vf->pf),
3786		"Can't config promiscuous mode in switchdev mode for VF %d\n",
3787		vf->vf_id);
3788	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
3789				     VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
3790				     NULL, 0);
3791}
3792
3793static const struct ice_virtchnl_ops ice_virtchnl_repr_ops = {
3794	.get_ver_msg = ice_vc_get_ver_msg,
3795	.get_vf_res_msg = ice_vc_get_vf_res_msg,
3796	.reset_vf = ice_vc_reset_vf_msg,
3797	.add_mac_addr_msg = ice_vc_repr_add_mac,
3798	.del_mac_addr_msg = ice_vc_repr_del_mac,
3799	.cfg_qs_msg = ice_vc_cfg_qs_msg,
3800	.ena_qs_msg = ice_vc_ena_qs_msg,
3801	.dis_qs_msg = ice_vc_dis_qs_msg,
3802	.request_qs_msg = ice_vc_request_qs_msg,
3803	.cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
3804	.config_rss_key = ice_vc_config_rss_key,
3805	.config_rss_lut = ice_vc_config_rss_lut,
 
3806	.get_stats_msg = ice_vc_get_stats_msg,
3807	.cfg_promiscuous_mode_msg = ice_vc_repr_cfg_promiscuous_mode,
3808	.add_vlan_msg = ice_vc_add_vlan_msg,
3809	.remove_vlan_msg = ice_vc_remove_vlan_msg,
3810	.query_rxdid = ice_vc_query_rxdid,
3811	.get_rss_hena = ice_vc_get_rss_hena,
3812	.set_rss_hena_msg = ice_vc_set_rss_hena,
3813	.ena_vlan_stripping = ice_vc_ena_vlan_stripping,
3814	.dis_vlan_stripping = ice_vc_dis_vlan_stripping,
3815	.handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
3816	.add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
3817	.del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
3818	.get_offload_vlan_v2_caps = ice_vc_get_offload_vlan_v2_caps,
3819	.add_vlan_v2_msg = ice_vc_add_vlan_v2_msg,
3820	.remove_vlan_v2_msg = ice_vc_remove_vlan_v2_msg,
3821	.ena_vlan_stripping_v2_msg = ice_vc_ena_vlan_stripping_v2_msg,
3822	.dis_vlan_stripping_v2_msg = ice_vc_dis_vlan_stripping_v2_msg,
3823	.ena_vlan_insertion_v2_msg = ice_vc_ena_vlan_insertion_v2_msg,
3824	.dis_vlan_insertion_v2_msg = ice_vc_dis_vlan_insertion_v2_msg,
3825};
3826
3827/**
3828 * ice_virtchnl_set_repr_ops - Switch to representor virtchnl ops
3829 * @vf: the VF to switch ops
3830 */
3831void ice_virtchnl_set_repr_ops(struct ice_vf *vf)
3832{
3833	vf->virtchnl_ops = &ice_virtchnl_repr_ops;
3834}
3835
3836/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3837 * ice_vc_process_vf_msg - Process request from VF
3838 * @pf: pointer to the PF structure
3839 * @event: pointer to the AQ event
 
3840 *
3841 * called from the common asq/arq handler to
3842 * process request from VF
3843 */
3844void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
 
3845{
3846	u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
3847	s16 vf_id = le16_to_cpu(event->desc.retval);
3848	const struct ice_virtchnl_ops *ops;
3849	u16 msglen = event->msg_len;
3850	u8 *msg = event->msg_buf;
3851	struct ice_vf *vf = NULL;
3852	struct device *dev;
3853	int err = 0;
3854
3855	dev = ice_pf_to_dev(pf);
3856
3857	vf = ice_get_vf_by_id(pf, vf_id);
3858	if (!vf) {
3859		dev_err(dev, "Unable to locate VF for message from VF ID %d, opcode %d, len %d\n",
3860			vf_id, v_opcode, msglen);
3861		return;
3862	}
3863
3864	mutex_lock(&vf->cfg_lock);
3865
 
 
 
 
3866	/* Check if VF is disabled. */
3867	if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
3868		err = -EPERM;
3869		goto error_handler;
3870	}
3871
3872	ops = vf->virtchnl_ops;
3873
3874	/* Perform basic checks on the msg */
3875	err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
3876	if (err) {
3877		if (err == VIRTCHNL_STATUS_ERR_PARAM)
3878			err = -EPERM;
3879		else
3880			err = -EINVAL;
3881	}
3882
3883error_handler:
3884	if (err) {
3885		ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
3886				      NULL, 0);
3887		dev_err(dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
3888			vf_id, v_opcode, msglen, err);
3889		goto finish;
3890	}
3891
3892	if (!ice_vc_is_opcode_allowed(vf, v_opcode)) {
3893		ice_vc_send_msg_to_vf(vf, v_opcode,
3894				      VIRTCHNL_STATUS_ERR_NOT_SUPPORTED, NULL,
3895				      0);
3896		goto finish;
3897	}
3898
3899	switch (v_opcode) {
3900	case VIRTCHNL_OP_VERSION:
3901		err = ops->get_ver_msg(vf, msg);
3902		break;
3903	case VIRTCHNL_OP_GET_VF_RESOURCES:
3904		err = ops->get_vf_res_msg(vf, msg);
3905		if (ice_vf_init_vlan_stripping(vf))
3906			dev_dbg(dev, "Failed to initialize VLAN stripping for VF %d\n",
3907				vf->vf_id);
3908		ice_vc_notify_vf_link_state(vf);
3909		break;
3910	case VIRTCHNL_OP_RESET_VF:
3911		ops->reset_vf(vf);
3912		break;
3913	case VIRTCHNL_OP_ADD_ETH_ADDR:
3914		err = ops->add_mac_addr_msg(vf, msg);
3915		break;
3916	case VIRTCHNL_OP_DEL_ETH_ADDR:
3917		err = ops->del_mac_addr_msg(vf, msg);
3918		break;
3919	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
3920		err = ops->cfg_qs_msg(vf, msg);
3921		break;
3922	case VIRTCHNL_OP_ENABLE_QUEUES:
3923		err = ops->ena_qs_msg(vf, msg);
3924		ice_vc_notify_vf_link_state(vf);
3925		break;
3926	case VIRTCHNL_OP_DISABLE_QUEUES:
3927		err = ops->dis_qs_msg(vf, msg);
3928		break;
3929	case VIRTCHNL_OP_REQUEST_QUEUES:
3930		err = ops->request_qs_msg(vf, msg);
3931		break;
3932	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
3933		err = ops->cfg_irq_map_msg(vf, msg);
3934		break;
3935	case VIRTCHNL_OP_CONFIG_RSS_KEY:
3936		err = ops->config_rss_key(vf, msg);
3937		break;
3938	case VIRTCHNL_OP_CONFIG_RSS_LUT:
3939		err = ops->config_rss_lut(vf, msg);
 
 
 
3940		break;
3941	case VIRTCHNL_OP_GET_STATS:
3942		err = ops->get_stats_msg(vf, msg);
3943		break;
3944	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
3945		err = ops->cfg_promiscuous_mode_msg(vf, msg);
3946		break;
3947	case VIRTCHNL_OP_ADD_VLAN:
3948		err = ops->add_vlan_msg(vf, msg);
3949		break;
3950	case VIRTCHNL_OP_DEL_VLAN:
3951		err = ops->remove_vlan_msg(vf, msg);
3952		break;
3953	case VIRTCHNL_OP_GET_SUPPORTED_RXDIDS:
3954		err = ops->query_rxdid(vf);
3955		break;
3956	case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
3957		err = ops->get_rss_hena(vf);
3958		break;
3959	case VIRTCHNL_OP_SET_RSS_HENA:
3960		err = ops->set_rss_hena_msg(vf, msg);
3961		break;
3962	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
3963		err = ops->ena_vlan_stripping(vf);
3964		break;
3965	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
3966		err = ops->dis_vlan_stripping(vf);
3967		break;
3968	case VIRTCHNL_OP_ADD_FDIR_FILTER:
3969		err = ops->add_fdir_fltr_msg(vf, msg);
3970		break;
3971	case VIRTCHNL_OP_DEL_FDIR_FILTER:
3972		err = ops->del_fdir_fltr_msg(vf, msg);
3973		break;
3974	case VIRTCHNL_OP_ADD_RSS_CFG:
3975		err = ops->handle_rss_cfg_msg(vf, msg, true);
3976		break;
3977	case VIRTCHNL_OP_DEL_RSS_CFG:
3978		err = ops->handle_rss_cfg_msg(vf, msg, false);
3979		break;
3980	case VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS:
3981		err = ops->get_offload_vlan_v2_caps(vf);
3982		break;
3983	case VIRTCHNL_OP_ADD_VLAN_V2:
3984		err = ops->add_vlan_v2_msg(vf, msg);
3985		break;
3986	case VIRTCHNL_OP_DEL_VLAN_V2:
3987		err = ops->remove_vlan_v2_msg(vf, msg);
3988		break;
3989	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2:
3990		err = ops->ena_vlan_stripping_v2_msg(vf, msg);
3991		break;
3992	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2:
3993		err = ops->dis_vlan_stripping_v2_msg(vf, msg);
3994		break;
3995	case VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2:
3996		err = ops->ena_vlan_insertion_v2_msg(vf, msg);
3997		break;
3998	case VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2:
3999		err = ops->dis_vlan_insertion_v2_msg(vf, msg);
4000		break;
4001	case VIRTCHNL_OP_UNKNOWN:
4002	default:
4003		dev_err(dev, "Unsupported opcode %d from VF %d\n", v_opcode,
4004			vf_id);
4005		err = ice_vc_send_msg_to_vf(vf, v_opcode,
4006					    VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4007					    NULL, 0);
4008		break;
4009	}
4010	if (err) {
4011		/* Helper function cares less about error return values here
4012		 * as it is busy with pending work.
4013		 */
4014		dev_info(dev, "PF failed to honor VF %d, opcode %d, error %d\n",
4015			 vf_id, v_opcode, err);
4016	}
4017
4018finish:
4019	mutex_unlock(&vf->cfg_lock);
4020	ice_put_vf(vf);
4021}