ice_sriov.c 180.1 KB
Newer Older
1 2 3 4
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2018, Intel Corporation. */

#include "ice.h"
A
Anirudh Venkataramanan 已提交
5
#include "ice_base.h"
6
#include "ice_lib.h"
7
#include "ice_fltr.h"
8
#include "ice_dcb_lib.h"
Q
Qi Zhang 已提交
9
#include "ice_flow.h"
10
#include "ice_eswitch.h"
11
#include "ice_virtchnl_allowlist.h"
J
Jeff Guo 已提交
12
#include "ice_flex_pipe.h"
13
#include "ice_vf_vsi_vlan_ops.h"
14
#include "ice_vlan.h"
15

Q
Qi Zhang 已提交
16 17 18 19 20 21 22 23
#define FIELD_SELECTOR(proto_hdr_field) \
		BIT((proto_hdr_field) & PROTO_HDR_FIELD_MASK)

struct ice_vc_hdr_match_type {
	u32 vc_hdr;	/* virtchnl headers (VIRTCHNL_PROTO_HDR_XXX) */
	u32 ice_hdr;	/* ice headers (ICE_FLOW_SEG_HDR_XXX) */
};

J
Jeff Guo 已提交
24
static const struct ice_vc_hdr_match_type ice_vc_hdr_list[] = {
Q
Qi Zhang 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
	{VIRTCHNL_PROTO_HDR_NONE,	ICE_FLOW_SEG_HDR_NONE},
	{VIRTCHNL_PROTO_HDR_ETH,	ICE_FLOW_SEG_HDR_ETH},
	{VIRTCHNL_PROTO_HDR_S_VLAN,	ICE_FLOW_SEG_HDR_VLAN},
	{VIRTCHNL_PROTO_HDR_C_VLAN,	ICE_FLOW_SEG_HDR_VLAN},
	{VIRTCHNL_PROTO_HDR_IPV4,	ICE_FLOW_SEG_HDR_IPV4 |
					ICE_FLOW_SEG_HDR_IPV_OTHER},
	{VIRTCHNL_PROTO_HDR_IPV6,	ICE_FLOW_SEG_HDR_IPV6 |
					ICE_FLOW_SEG_HDR_IPV_OTHER},
	{VIRTCHNL_PROTO_HDR_TCP,	ICE_FLOW_SEG_HDR_TCP},
	{VIRTCHNL_PROTO_HDR_UDP,	ICE_FLOW_SEG_HDR_UDP},
	{VIRTCHNL_PROTO_HDR_SCTP,	ICE_FLOW_SEG_HDR_SCTP},
	{VIRTCHNL_PROTO_HDR_PPPOE,	ICE_FLOW_SEG_HDR_PPPOE},
	{VIRTCHNL_PROTO_HDR_GTPU_IP,	ICE_FLOW_SEG_HDR_GTPU_IP},
	{VIRTCHNL_PROTO_HDR_GTPU_EH,	ICE_FLOW_SEG_HDR_GTPU_EH},
	{VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_DWN,
					ICE_FLOW_SEG_HDR_GTPU_DWN},
	{VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_UP,
					ICE_FLOW_SEG_HDR_GTPU_UP},
	{VIRTCHNL_PROTO_HDR_L2TPV3,	ICE_FLOW_SEG_HDR_L2TPV3},
	{VIRTCHNL_PROTO_HDR_ESP,	ICE_FLOW_SEG_HDR_ESP},
	{VIRTCHNL_PROTO_HDR_AH,		ICE_FLOW_SEG_HDR_AH},
	{VIRTCHNL_PROTO_HDR_PFCP,	ICE_FLOW_SEG_HDR_PFCP_SESSION},
};

struct ice_vc_hash_field_match_type {
	u32 vc_hdr;		/* virtchnl headers
				 * (VIRTCHNL_PROTO_HDR_XXX)
				 */
	u32 vc_hash_field;	/* virtchnl hash fields selector
				 * FIELD_SELECTOR((VIRTCHNL_PROTO_HDR_ETH_XXX))
				 */
	u64 ice_hash_field;	/* ice hash fields
				 * (BIT_ULL(ICE_FLOW_FIELD_IDX_XXX))
				 */
};

static const struct
J
Jeff Guo 已提交
62
ice_vc_hash_field_match_type ice_vc_hash_field_list[] = {
Q
Qi Zhang 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	{VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC),
		BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_SA)},
	{VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
		BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_DA)},
	{VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC) |
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
		ICE_FLOW_HASH_ETH},
	{VIRTCHNL_PROTO_HDR_ETH,
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_ETHERTYPE),
		BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_TYPE)},
	{VIRTCHNL_PROTO_HDR_S_VLAN,
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_S_VLAN_ID),
		BIT_ULL(ICE_FLOW_FIELD_IDX_S_VLAN)},
	{VIRTCHNL_PROTO_HDR_C_VLAN,
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_C_VLAN_ID),
		BIT_ULL(ICE_FLOW_FIELD_IDX_C_VLAN)},
	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC),
		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA)},
	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA)},
	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
		ICE_FLOW_HASH_IPV4},
	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA) |
		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA) |
		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
		ICE_FLOW_HASH_IPV4 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC),
		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA)},
	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA)},
	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
		ICE_FLOW_HASH_IPV6},
	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA) |
		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA) |
		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
		ICE_FLOW_HASH_IPV6 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
	{VIRTCHNL_PROTO_HDR_TCP,
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT),
		BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_SRC_PORT)},
	{VIRTCHNL_PROTO_HDR_TCP,
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
		BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_DST_PORT)},
	{VIRTCHNL_PROTO_HDR_TCP,
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT) |
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
		ICE_FLOW_HASH_TCP_PORT},
	{VIRTCHNL_PROTO_HDR_UDP,
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT),
		BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_SRC_PORT)},
	{VIRTCHNL_PROTO_HDR_UDP,
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
		BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_DST_PORT)},
	{VIRTCHNL_PROTO_HDR_UDP,
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT) |
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
		ICE_FLOW_HASH_UDP_PORT},
	{VIRTCHNL_PROTO_HDR_SCTP,
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT),
		BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT)},
	{VIRTCHNL_PROTO_HDR_SCTP,
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
		BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_DST_PORT)},
	{VIRTCHNL_PROTO_HDR_SCTP,
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT) |
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
		ICE_FLOW_HASH_SCTP_PORT},
	{VIRTCHNL_PROTO_HDR_PPPOE,
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PPPOE_SESS_ID),
		BIT_ULL(ICE_FLOW_FIELD_IDX_PPPOE_SESS_ID)},
	{VIRTCHNL_PROTO_HDR_GTPU_IP,
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_GTPU_IP_TEID),
		BIT_ULL(ICE_FLOW_FIELD_IDX_GTPU_IP_TEID)},
	{VIRTCHNL_PROTO_HDR_L2TPV3,
		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_L2TPV3_SESS_ID),
		BIT_ULL(ICE_FLOW_FIELD_IDX_L2TPV3_SESS_ID)},
	{VIRTCHNL_PROTO_HDR_ESP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ESP_SPI),
		BIT_ULL(ICE_FLOW_FIELD_IDX_ESP_SPI)},
	{VIRTCHNL_PROTO_HDR_AH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_AH_SPI),
		BIT_ULL(ICE_FLOW_FIELD_IDX_AH_SPI)},
	{VIRTCHNL_PROTO_HDR_PFCP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PFCP_SEID),
		BIT_ULL(ICE_FLOW_FIELD_IDX_PFCP_SEID)},
};

168 169 170 171
/**
 * ice_get_vf_vsi - get VF's VSI based on the stored index
 * @vf: VF used to get VSI
 */
172
struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf)
173 174 175 176
{
	return vf->pf->vsi[vf->lan_vsi_idx];
}

J
Jesse Brandeburg 已提交
177
/**
178 179 180 181 182 183 184
 * ice_get_vf_by_id - Get pointer to VF by ID
 * @pf: the PF private structure
 * @vf_id: the VF ID to locate
 *
 * Locate and return a pointer to the VF structure associated with a given ID.
 * Returns NULL if the ID does not have a valid VF structure associated with
 * it.
185 186 187 188
 *
 * This function takes a reference to the VF, which must be released by
 * calling ice_put_vf() once the caller is finished accessing the VF structure
 * returned.
J
Jesse Brandeburg 已提交
189
 */
190
struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id)
J
Jesse Brandeburg 已提交
191
{
192
	struct ice_vf *vf;
193

194 195 196 197 198 199 200 201 202 203 204 205 206
	rcu_read_lock();
	hash_for_each_possible_rcu(pf->vfs.table, vf, entry, vf_id) {
		if (vf->vf_id == vf_id) {
			struct ice_vf *found;

			if (kref_get_unless_zero(&vf->refcnt))
				found = vf;
			else
				found = NULL;

			rcu_read_unlock();
			return found;
		}
J
Jesse Brandeburg 已提交
207
	}
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	rcu_read_unlock();

	return NULL;
}

/**
 * ice_release_vf - Release VF associated with a refcount
 * @ref: the kref decremented to zero
 *
 * Callback function for kref_put to release a VF once its reference count has
 * hit zero.
 */
static void ice_release_vf(struct kref *ref)
{
	struct ice_vf *vf = container_of(ref, struct ice_vf, refcnt);

	mutex_destroy(&vf->cfg_lock);

	kfree_rcu(vf, rcu);
}
228

229 230 231 232 233 234 235 236 237 238 239
/**
 * ice_put_vf - Release a reference to a VF
 * @vf: the VF structure to decrease reference count on
 *
 * This must be called after ice_get_vf_by_id() once the reference to the VF
 * structure is no longer used. Otherwise, the VF structure will never be
 * freed.
 */
void ice_put_vf(struct ice_vf *vf)
{
	kref_put(&vf->refcnt, ice_release_vf);
240 241 242 243 244 245 246 247 248 249 250 251 252
}

/**
 * ice_has_vfs - Return true if the PF has any associated VFs
 * @pf: the PF private structure
 *
 * Return whether or not the PF has any allocated VFs.
 *
 * Note that this function only guarantees that there are no VFs at the point
 * of calling it. It does not guarantee that no more VFs will be added.
 */
bool ice_has_vfs(struct ice_pf *pf)
{
253 254 255 256
	/* A simple check that the hash table is not empty does not require
	 * the mutex or rcu_read_lock.
	 */
	return !hash_empty(pf->vfs.table);
257 258 259 260 261 262 263 264 265 266 267 268
}

/**
 * ice_get_num_vfs - Get number of allocated VFs
 * @pf: the PF private structure
 *
 * Return the total number of allocated VFs. NOTE: VF IDs are not guaranteed
 * to be contiguous. Do not assume that a VF ID is guaranteed to be less than
 * the output of this function.
 */
u16 ice_get_num_vfs(struct ice_pf *pf)
{
269 270 271 272 273 274 275 276
	struct ice_vf *vf;
	unsigned int bkt;
	u16 num_vfs = 0;

	rcu_read_lock();
	ice_for_each_vf_rcu(pf, bkt, vf)
		num_vfs++;
	rcu_read_unlock();
277

278
	return num_vfs;
J
Jesse Brandeburg 已提交
279 280 281 282 283 284 285 286 287 288
}

/**
 * ice_check_vf_init - helper to check if VF init complete
 * @pf: pointer to the PF structure
 * @vf: the pointer to the VF to check
 */
static int ice_check_vf_init(struct ice_pf *pf, struct ice_vf *vf)
{
	if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
J
Jesse Brandeburg 已提交
289
		dev_err(ice_pf_to_dev(pf), "VF ID: %u in reset. Try again.\n",
J
Jesse Brandeburg 已提交
290 291 292 293 294 295
			vf->vf_id);
		return -EBUSY;
	}
	return 0;
}

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
/**
 * ice_free_vf_entries - Free all VF entries from the hash table
 * @pf: pointer to the PF structure
 *
 * Iterate over the VF hash table, removing and releasing all VF entries.
 * Called during VF teardown or as cleanup during failed VF initialization.
 */
static void ice_free_vf_entries(struct ice_pf *pf)
{
	struct ice_vfs *vfs = &pf->vfs;
	struct hlist_node *tmp;
	struct ice_vf *vf;
	unsigned int bkt;

	/* Remove all VFs from the hash table and release their main
	 * reference. Once all references to the VF are dropped, ice_put_vf()
	 * will call ice_release_vf which will remove the VF memory.
	 */
	lockdep_assert_held(&vfs->table_lock);

	hash_for_each_safe(vfs->table, bkt, tmp, vf, entry) {
		hash_del_rcu(&vf->entry);
		ice_put_vf(vf);
	}
}

322 323 324 325 326 327 328 329 330 331
/**
 * ice_vc_vf_broadcast - Broadcast a message to all VFs on PF
 * @pf: pointer to the PF structure
 * @v_opcode: operation code
 * @v_retval: return value
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 */
static void
ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
332
		    enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
333 334
{
	struct ice_hw *hw = &pf->hw;
335 336
	struct ice_vf *vf;
	unsigned int bkt;
B
Brett Creeley 已提交
337

338
	mutex_lock(&pf->vfs.table_lock);
339
	ice_for_each_vf(pf, bkt, vf) {
340 341 342 343 344 345 346 347 348 349 350
		/* Not all vfs are enabled so skip the ones that are not */
		if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
		    !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
			continue;

		/* Ignore return value on purpose - a given VF may fail, but
		 * we need to keep going and send to all of them
		 */
		ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
				      msglen, NULL);
	}
351
	mutex_unlock(&pf->vfs.table_lock);
352 353
}

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
/**
 * ice_set_pfe_link - Set the link speed/status of the virtchnl_pf_event
 * @vf: pointer to the VF structure
 * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
 * @ice_link_speed: link speed specified by ICE_AQ_LINK_SPEED_*
 * @link_up: whether or not to set the link up/down
 */
static void
ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
		 int ice_link_speed, bool link_up)
{
	if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
		pfe->event_data.link_event_adv.link_status = link_up;
		/* Speed in Mbps */
		pfe->event_data.link_event_adv.link_speed =
			ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
	} else {
		pfe->event_data.link_event.link_status = link_up;
		/* Legacy method for virtchnl link speeds */
		pfe->event_data.link_event.link_speed =
			(enum virtchnl_link_speed)
			ice_conv_link_speed_to_virtchnl(false, ice_link_speed);
	}
}

379 380 381 382 383 384 385 386 387
/**
 * ice_vf_has_no_qs_ena - check if the VF has any Rx or Tx queues enabled
 * @vf: the VF to check
 *
 * Returns true if the VF has no Rx and no Tx queues enabled and returns false
 * otherwise
 */
static bool ice_vf_has_no_qs_ena(struct ice_vf *vf)
{
M
Mitch Williams 已提交
388 389
	return (!bitmap_weight(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF) &&
		!bitmap_weight(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF));
390 391
}

392 393 394 395 396 397 398 399 400 401 402
/**
 * ice_is_vf_link_up - check if the VF's link is up
 * @vf: VF to check if link is up
 */
static bool ice_is_vf_link_up(struct ice_vf *vf)
{
	struct ice_pf *pf = vf->pf;

	if (ice_check_vf_init(pf, vf))
		return false;

403
	if (ice_vf_has_no_qs_ena(vf))
404 405 406 407 408 409 410 411
		return false;
	else if (vf->link_forced)
		return vf->link_up;
	else
		return pf->hw.port_info->phy.link_info.link_info &
			ICE_AQ_LINK_UP;
}

412 413 414 415 416 417
/**
 * ice_vc_notify_vf_link_state - Inform a VF of link status
 * @vf: pointer to the VF structure
 *
 * send a link status message to a single VF
 */
418
void ice_vc_notify_vf_link_state(struct ice_vf *vf)
419 420
{
	struct virtchnl_pf_event pfe = { 0 };
421
	struct ice_hw *hw = &vf->pf->hw;
422 423 424 425

	pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
	pfe.severity = PF_EVENT_SEVERITY_INFO;

426 427 428 429
	if (ice_is_vf_link_up(vf))
		ice_set_pfe_link(vf, &pfe,
				 hw->port_info->phy.link_info.link_speed, true);
	else
430
		ice_set_pfe_link(vf, &pfe, ICE_AQ_LINK_SPEED_UNKNOWN, false);
431

432 433
	ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
			      VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,
434 435 436
			      sizeof(pfe), NULL);
}

437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
/**
 * ice_vf_invalidate_vsi - invalidate vsi_idx/vsi_num to remove VSI access
 * @vf: VF to remove access to VSI for
 */
static void ice_vf_invalidate_vsi(struct ice_vf *vf)
{
	vf->lan_vsi_idx = ICE_NO_VSI;
	vf->lan_vsi_num = ICE_NO_VSI;
}

/**
 * ice_vf_vsi_release - invalidate the VF's VSI after freeing it
 * @vf: invalidate this VF's VSI after freeing it
 */
static void ice_vf_vsi_release(struct ice_vf *vf)
{
453
	ice_vsi_release(ice_get_vf_vsi(vf));
454 455 456
	ice_vf_invalidate_vsi(vf);
}

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
/**
 * ice_vf_ctrl_invalidate_vsi - invalidate ctrl_vsi_idx to remove VSI access
 * @vf: VF that control VSI is being invalidated on
 */
static void ice_vf_ctrl_invalidate_vsi(struct ice_vf *vf)
{
	vf->ctrl_vsi_idx = ICE_NO_VSI;
}

/**
 * ice_vf_ctrl_vsi_release - invalidate the VF's control VSI after freeing it
 * @vf: VF that control VSI is being released on
 */
static void ice_vf_ctrl_vsi_release(struct ice_vf *vf)
{
	ice_vsi_release(vf->pf->vsi[vf->ctrl_vsi_idx]);
	ice_vf_ctrl_invalidate_vsi(vf);
}

476 477 478 479 480 481 482
/**
 * ice_free_vf_res - Free a VF's resources
 * @vf: pointer to the VF info
 */
static void ice_free_vf_res(struct ice_vf *vf)
{
	struct ice_pf *pf = vf->pf;
483
	int i, last_vector_idx;
484 485 486 487 488

	/* First, disable VF's configuration API to prevent OS from
	 * accessing the VF's VSI after it's freed or invalidated.
	 */
	clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
Q
Qi Zhang 已提交
489
	ice_vf_fdir_exit(vf);
490 491 492
	/* free VF control VSI */
	if (vf->ctrl_vsi_idx != ICE_NO_VSI)
		ice_vf_ctrl_vsi_release(vf);
493

494
	/* free VSI and disconnect it from the parent uplink */
495 496
	if (vf->lan_vsi_idx != ICE_NO_VSI) {
		ice_vf_vsi_release(vf);
497 498 499
		vf->num_mac = 0;
	}

500
	last_vector_idx = vf->first_vector_idx + pf->vfs.num_msix_per - 1;
501 502 503 504 505

	/* clear VF MDD event information */
	memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
	memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));

506
	/* Disable interrupts so that VF starts in a known state */
507 508
	for (i = vf->first_vector_idx; i <= last_vector_idx; i++) {
		wr32(&pf->hw, GLINT_DYN_CTL(i), GLINT_DYN_CTL_CLEARPBA_M);
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
		ice_flush(&pf->hw);
	}
	/* reset some of the state variables keeping track of the resources */
	clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
	clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
}

/**
 * ice_dis_vf_mappings
 * @vf: pointer to the VF structure
 */
static void ice_dis_vf_mappings(struct ice_vf *vf)
{
	struct ice_pf *pf = vf->pf;
	struct ice_vsi *vsi;
B
Brett Creeley 已提交
524
	struct device *dev;
525 526 527 528
	int first, last, v;
	struct ice_hw *hw;

	hw = &pf->hw;
529
	vsi = ice_get_vf_vsi(vf);
530

B
Brett Creeley 已提交
531
	dev = ice_pf_to_dev(pf);
532
	wr32(hw, VPINT_ALLOC(vf->vf_id), 0);
533
	wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), 0);
534

B
Brett Creeley 已提交
535
	first = vf->first_vector_idx;
536
	last = first + pf->vfs.num_msix_per - 1;
537 538 539 540 541 542 543 544 545 546 547 548 549
	for (v = first; v <= last; v++) {
		u32 reg;

		reg = (((1 << GLINT_VECT2FUNC_IS_PF_S) &
			GLINT_VECT2FUNC_IS_PF_M) |
		       ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
			GLINT_VECT2FUNC_PF_NUM_M));
		wr32(hw, GLINT_VECT2FUNC(v), reg);
	}

	if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
		wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
	else
B
Brett Creeley 已提交
550
		dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
551 552 553 554

	if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
		wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
	else
555
		dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
556 557
}

B
Brett Creeley 已提交
558 559 560 561
/**
 * ice_sriov_free_msix_res - Reset/free any used MSIX resources
 * @pf: pointer to the PF structure
 *
M
Mitch Williams 已提交
562
 * Since no MSIX entries are taken from the pf->irq_tracker then just clear
B
Brett Creeley 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
 * the pf->sriov_base_vector.
 *
 * Returns 0 on success, and -EINVAL on error.
 */
static int ice_sriov_free_msix_res(struct ice_pf *pf)
{
	struct ice_res_tracker *res;

	if (!pf)
		return -EINVAL;

	res = pf->irq_tracker;
	if (!res)
		return -EINVAL;

	/* give back irq_tracker resources used */
M
Mitch Williams 已提交
579
	WARN_ON(pf->sriov_base_vector < res->num_entries);
B
Brett Creeley 已提交
580 581 582 583 584 585

	pf->sriov_base_vector = 0;

	return 0;
}

586 587 588 589 590 591 592
/**
 * ice_set_vf_state_qs_dis - Set VF queues state to disabled
 * @vf: pointer to the VF structure
 */
void ice_set_vf_state_qs_dis(struct ice_vf *vf)
{
	/* Clear Rx/Tx enabled queues flag */
M
Mitch Williams 已提交
593 594
	bitmap_zero(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF);
	bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
595 596 597 598 599 600 601 602 603
	clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
}

/**
 * ice_dis_vf_qs - Disable the VF queues
 * @vf: pointer to the VF structure
 */
static void ice_dis_vf_qs(struct ice_vf *vf)
{
604
	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
605 606

	ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id);
607
	ice_vsi_stop_all_rx_rings(vsi);
608 609 610
	ice_set_vf_state_qs_dis(vf);
}

611 612 613 614 615 616
/**
 * ice_free_vfs - Free all VFs
 * @pf: pointer to the PF structure
 */
void ice_free_vfs(struct ice_pf *pf)
{
B
Brett Creeley 已提交
617
	struct device *dev = ice_pf_to_dev(pf);
618
	struct ice_vfs *vfs = &pf->vfs;
619
	struct ice_hw *hw = &pf->hw;
620 621
	struct ice_vf *vf;
	unsigned int bkt;
622

623
	if (!ice_has_vfs(pf))
624 625
		return;

626
	while (test_and_set_bit(ICE_VF_DIS, pf->state))
627 628
		usleep_range(1000, 2000);

629 630 631 632 633 634 635
	/* Disable IOV before freeing resources. This lets any VF drivers
	 * running in the host get themselves cleaned up before we yank
	 * the carpet out from underneath their feet.
	 */
	if (!pci_vfs_assigned(pf->pdev))
		pci_disable_sriov(pf->pdev);
	else
B
Brett Creeley 已提交
636
		dev_warn(dev, "VFs are assigned - not disabling SR-IOV\n");
637

638 639 640 641
	mutex_lock(&vfs->table_lock);

	ice_eswitch_release(pf);

642
	ice_for_each_vf(pf, bkt, vf) {
643 644 645 646 647
		mutex_lock(&vf->cfg_lock);

		ice_dis_vf_qs(vf);

		if (test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
648
			/* disable VF qp mappings and set VF disable state */
649 650 651
			ice_dis_vf_mappings(vf);
			set_bit(ICE_VF_STATE_DIS, vf->vf_states);
			ice_free_vf_res(vf);
652
		}
653

654 655 656 657 658 659 660 661
		if (!pci_vfs_assigned(pf->pdev)) {
			u32 reg_idx, bit_idx;

			reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
			bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
			wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
		}

662
		/* clear malicious info since the VF is getting released */
663
		if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->vfs.malvfs,
664
					ICE_MAX_SRIOV_VFS, vf->vf_id))
665 666 667
			dev_dbg(dev, "failed to clear malicious VF state for VF %u\n",
				vf->vf_id);

668
		mutex_unlock(&vf->cfg_lock);
669 670
	}

B
Brett Creeley 已提交
671
	if (ice_sriov_free_msix_res(pf))
B
Brett Creeley 已提交
672
		dev_err(dev, "Failed to free MSIX resources used by SR-IOV\n");
B
Brett Creeley 已提交
673

674
	vfs->num_qps_per = 0;
675 676 677
	ice_free_vf_entries(pf);

	mutex_unlock(&vfs->table_lock);
678

679
	clear_bit(ICE_VF_DIS, pf->state);
680 681 682 683 684 685 686
	clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
}

/**
 * ice_trigger_vf_reset - Reset a VF on HW
 * @vf: pointer to the VF structure
 * @is_vflr: true if VFLR was issued, false if not
M
Mitch Williams 已提交
687
 * @is_pfr: true if the reset was triggered due to a previous PFR
688 689 690 691 692
 *
 * Trigger hardware to start a reset for a particular VF. Expects the caller
 * to wait the proper amount of time to allow hardware to reset the VF before
 * it cleans up and restores VF functionality.
 */
M
Mitch Williams 已提交
693
static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr, bool is_pfr)
694 695 696
{
	struct ice_pf *pf = vf->pf;
	u32 reg, reg_idx, bit_idx;
J
Jesse Brandeburg 已提交
697
	unsigned int vf_abs_id, i;
B
Brett Creeley 已提交
698
	struct device *dev;
699 700
	struct ice_hw *hw;

B
Brett Creeley 已提交
701
	dev = ice_pf_to_dev(pf);
702 703 704 705 706 707 708
	hw = &pf->hw;
	vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;

	/* Inform VF that it is no longer active, as a warning */
	clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);

	/* Disable VF's configuration API during reset. The flag is re-enabled
B
Brett Creeley 已提交
709
	 * when it's safe again to access VF's VSI.
710 711
	 */
	clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
712

713 714 715 716
	/* VF_MBX_ARQLEN and VF_MBX_ATQLEN are cleared by PFR, so the driver
	 * needs to clear them in the case of VFR/VFLR. If this is done for
	 * PFR, it can mess up VF resets because the VF driver may already
	 * have started cleanup by the time we get here.
717
	 */
718
	if (!is_pfr) {
719
		wr32(hw, VF_MBX_ARQLEN(vf->vf_id), 0);
720 721
		wr32(hw, VF_MBX_ATQLEN(vf->vf_id), 0);
	}
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739

	/* In the case of a VFLR, the HW has already reset the VF and we
	 * just need to clean up, so don't hit the VFRTRIG register.
	 */
	if (!is_vflr) {
		/* reset VF using VPGEN_VFRTRIG reg */
		reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
		reg |= VPGEN_VFRTRIG_VFSWR_M;
		wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
	}
	/* clear the VFLR bit in GLGEN_VFLRSTAT */
	reg_idx = (vf_abs_id) / 32;
	bit_idx = (vf_abs_id) % 32;
	wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
	ice_flush(hw);

	wr32(hw, PF_PCI_CIAA,
	     VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
740
	for (i = 0; i < ICE_PCI_CIAD_WAIT_COUNT; i++) {
741
		reg = rd32(hw, PF_PCI_CIAD);
742 743 744 745
		/* no transactions pending so stop polling */
		if ((reg & VF_TRANS_PENDING_M) == 0)
			break;

J
Jesse Brandeburg 已提交
746
		dev_err(dev, "VF %u PCI transactions stuck\n", vf->vf_id);
747
		udelay(ICE_PCI_CIAD_WAIT_DELAY_US);
748 749 750
	}
}

751 752 753 754 755 756 757 758 759
/**
 * ice_vf_get_port_info - Get the VF's port info structure
 * @vf: VF used to get the port info structure for
 */
static struct ice_port_info *ice_vf_get_port_info(struct ice_vf *vf)
{
	return vf->pf->hw.port_info;
}

760 761
/**
 * ice_vf_vsi_setup - Set up a VF VSI
762
 * @vf: VF to setup VSI for
763 764 765 766
 *
 * Returns pointer to the successfully allocated VSI struct on success,
 * otherwise returns NULL on failure.
 */
767
static struct ice_vsi *ice_vf_vsi_setup(struct ice_vf *vf)
768
{
769 770 771 772
	struct ice_port_info *pi = ice_vf_get_port_info(vf);
	struct ice_pf *pf = vf->pf;
	struct ice_vsi *vsi;

773
	vsi = ice_vsi_setup(pf, pi, ICE_VSI_VF, vf, NULL);
774 775 776 777 778 779 780 781 782 783 784

	if (!vsi) {
		dev_err(ice_pf_to_dev(pf), "Failed to create VF VSI\n");
		ice_vf_invalidate_vsi(vf);
		return NULL;
	}

	vf->lan_vsi_idx = vsi->idx;
	vf->lan_vsi_num = vsi->vsi_num;

	return vsi;
785 786
}

787 788 789 790 791 792 793 794 795 796 797 798 799
/**
 * ice_vf_ctrl_vsi_setup - Set up a VF control VSI
 * @vf: VF to setup control VSI for
 *
 * Returns pointer to the successfully allocated VSI struct on success,
 * otherwise returns NULL on failure.
 */
struct ice_vsi *ice_vf_ctrl_vsi_setup(struct ice_vf *vf)
{
	struct ice_port_info *pi = ice_vf_get_port_info(vf);
	struct ice_pf *pf = vf->pf;
	struct ice_vsi *vsi;

800
	vsi = ice_vsi_setup(pf, pi, ICE_VSI_CTRL, vf, NULL);
801 802 803 804 805 806 807 808
	if (!vsi) {
		dev_err(ice_pf_to_dev(pf), "Failed to create VF control VSI\n");
		ice_vf_ctrl_invalidate_vsi(vf);
	}

	return vsi;
}

B
Brett Creeley 已提交
809
/**
810
 * ice_calc_vf_first_vector_idx - Calculate MSIX vector index in the PF space
B
Brett Creeley 已提交
811 812 813
 * @pf: pointer to PF structure
 * @vf: pointer to VF that the first MSIX vector index is being calculated for
 *
814 815 816 817
 * This returns the first MSIX vector index in PF space that is used by this VF.
 * This index is used when accessing PF relative registers such as
 * GLINT_VECT2FUNC and GLINT_DYN_CTL.
 * This will always be the OICR index in the AVF driver so any functionality
B
Brett Creeley 已提交
818 819 820 821 822
 * using vf->first_vector_idx for queue configuration will have to increment by
 * 1 to avoid meddling with the OICR index.
 */
static int ice_calc_vf_first_vector_idx(struct ice_pf *pf, struct ice_vf *vf)
{
823
	return pf->sriov_base_vector + vf->vf_id * pf->vfs.num_msix_per;
B
Brett Creeley 已提交
824 825
}

826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
/**
 * ice_vf_rebuild_host_tx_rate_cfg - re-apply the Tx rate limiting configuration
 * @vf: VF to re-apply the configuration for
 *
 * Called after a VF VSI has been re-added/rebuild during reset. The PF driver
 * needs to re-apply the host configured Tx rate limiting configuration.
 */
static int ice_vf_rebuild_host_tx_rate_cfg(struct ice_vf *vf)
{
	struct device *dev = ice_pf_to_dev(vf->pf);
	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
	int err;

	if (vf->min_tx_rate) {
		err = ice_set_min_bw_limit(vsi, (u64)vf->min_tx_rate * 1000);
		if (err) {
			dev_err(dev, "failed to set min Tx rate to %d Mbps for VF %u, error %d\n",
				vf->min_tx_rate, vf->vf_id, err);
			return err;
		}
	}

	if (vf->max_tx_rate) {
		err = ice_set_max_bw_limit(vsi, (u64)vf->max_tx_rate * 1000);
		if (err) {
			dev_err(dev, "failed to set max Tx rate to %d Mbps for VF %u, error %d\n",
				vf->max_tx_rate, vf->vf_id, err);
			return err;
		}
	}

	return 0;
}

860 861 862 863 864 865 866 867 868 869
static u16 ice_vf_get_port_vlan_id(struct ice_vf *vf)
{
	return vf->port_vlan_info.vid;
}

static u8 ice_vf_get_port_vlan_prio(struct ice_vf *vf)
{
	return vf->port_vlan_info.prio;
}

870
bool ice_vf_is_port_vlan_ena(struct ice_vf *vf)
871 872 873 874
{
	return (ice_vf_get_port_vlan_id(vf) || ice_vf_get_port_vlan_prio(vf));
}

875 876 877 878 879
static u16 ice_vf_get_port_vlan_tpid(struct ice_vf *vf)
{
	return vf->port_vlan_info.tpid;
}

880 881 882
/**
 * ice_vf_rebuild_host_vlan_cfg - add VLAN 0 filter or rebuild the Port VLAN
 * @vf: VF to add MAC filters for
883
 * @vsi: Pointer to VSI
884 885 886 887
 *
 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
 * always re-adds either a VLAN 0 or port VLAN based filter after reset.
 */
888
static int ice_vf_rebuild_host_vlan_cfg(struct ice_vf *vf, struct ice_vsi *vsi)
889
{
890
	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
891 892 893
	struct device *dev = ice_pf_to_dev(vf->pf);
	int err;

894
	if (ice_vf_is_port_vlan_ena(vf)) {
895
		err = vlan_ops->set_port_vlan(vsi, &vf->port_vlan_info);
896 897 898 899 900
		if (err) {
			dev_err(dev, "failed to configure port VLAN via VSI parameters for VF %u, error %d\n",
				vf->vf_id, err);
			return err;
		}
901 902 903 904

		err = vlan_ops->add_vlan(vsi, &vf->port_vlan_info);
	} else {
		err = ice_vsi_add_vlan_zero(vsi);
905 906 907
	}

	if (err) {
908 909 910
		dev_err(dev, "failed to add VLAN %u filter for VF %u during VF rebuild, error %d\n",
			ice_vf_is_port_vlan_ena(vf) ?
			ice_vf_get_port_vlan_id(vf) : 0, vf->vf_id, err);
911 912 913
		return err;
	}

914 915 916 917 918
	err = vlan_ops->ena_rx_filtering(vsi);
	if (err)
		dev_warn(dev, "failed to enable Rx VLAN filtering for VF %d VSI %d during VF rebuild, error %d\n",
			 vf->vf_id, vsi->idx, err);

919 920 921
	return 0;
}

922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
static int ice_cfg_mac_antispoof(struct ice_vsi *vsi, bool enable)
{
	struct ice_vsi_ctx *ctx;
	int err;

	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
	if (!ctx)
		return -ENOMEM;

	ctx->info.sec_flags = vsi->info.sec_flags;
	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);

	if (enable)
		ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
	else
		ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;

	err = ice_update_vsi(&vsi->back->hw, vsi->idx, ctx, NULL);
	if (err)
		dev_err(ice_pf_to_dev(vsi->back), "Failed to configure Tx MAC anti-spoof %s for VSI %d, error %d\n",
			enable ? "ON" : "OFF", vsi->vsi_num, err);
	else
		vsi->info.sec_flags = ctx->info.sec_flags;

	kfree(ctx);

	return err;
}

/**
 * ice_vsi_ena_spoofchk - enable Tx spoof checking for this VSI
 * @vsi: VSI to enable Tx spoof checking for
 */
static int ice_vsi_ena_spoofchk(struct ice_vsi *vsi)
{
957
	struct ice_vsi_vlan_ops *vlan_ops;
958 959
	int err;

960 961 962
	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);

	err = vlan_ops->ena_tx_filtering(vsi);
963 964 965 966 967 968 969 970 971 972 973 974
	if (err)
		return err;

	return ice_cfg_mac_antispoof(vsi, true);
}

/**
 * ice_vsi_dis_spoofchk - disable Tx spoof checking for this VSI
 * @vsi: VSI to disable Tx spoof checking for
 */
static int ice_vsi_dis_spoofchk(struct ice_vsi *vsi)
{
975
	struct ice_vsi_vlan_ops *vlan_ops;
976 977
	int err;

978 979 980
	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);

	err = vlan_ops->dis_tx_filtering(vsi);
981 982 983 984 985 986 987
	if (err)
		return err;

	return ice_cfg_mac_antispoof(vsi, false);
}

/**
988
 * ice_vsi_apply_spoofchk - Apply Tx spoof checking setting to a VSI
989
 * @vsi: VSI associated to the VF
990
 * @enable: whether to enable or disable the spoof checking
991
 */
992
static int ice_vsi_apply_spoofchk(struct ice_vsi *vsi, bool enable)
993 994 995
{
	int err;

996
	if (enable)
997 998 999 1000 1001 1002 1003
		err = ice_vsi_ena_spoofchk(vsi);
	else
		err = ice_vsi_dis_spoofchk(vsi);

	return err;
}

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
/**
 * ice_vf_rebuild_host_mac_cfg - add broadcast and the VF's perm_addr/LAA
 * @vf: VF to add MAC filters for
 *
 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
 * always re-adds a broadcast filter and the VF's perm_addr/LAA after reset.
 */
static int ice_vf_rebuild_host_mac_cfg(struct ice_vf *vf)
{
	struct device *dev = ice_pf_to_dev(vf->pf);
1014
	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1015
	u8 broadcast[ETH_ALEN];
1016
	int status;
1017

1018 1019 1020
	if (ice_is_eswitch_mode_switchdev(vf->pf))
		return 0;

1021 1022 1023
	eth_broadcast_addr(broadcast);
	status = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
	if (status) {
1024 1025
		dev_err(dev, "failed to add broadcast MAC filter for VF %u, error %d\n",
			vf->vf_id, status);
T
Tony Nguyen 已提交
1026
		return status;
1027 1028 1029 1030
	}

	vf->num_mac++;

1031 1032
	if (is_valid_ether_addr(vf->hw_lan_addr.addr)) {
		status = ice_fltr_add_mac(vsi, vf->hw_lan_addr.addr,
1033 1034
					  ICE_FWD_TO_VSI);
		if (status) {
1035
			dev_err(dev, "failed to add default unicast MAC filter %pM for VF %u, error %d\n",
1036
				&vf->hw_lan_addr.addr[0], vf->vf_id,
1037
				status);
T
Tony Nguyen 已提交
1038
			return status;
1039 1040
		}
		vf->num_mac++;
B
Brett Creeley 已提交
1041 1042

		ether_addr_copy(vf->dev_lan_addr.addr, vf->hw_lan_addr.addr);
1043 1044 1045 1046 1047
	}

	return 0;
}

1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
/**
 * ice_vf_set_host_trust_cfg - set trust setting based on pre-reset value
 * @vf: VF to configure trust setting for
 */
static void ice_vf_set_host_trust_cfg(struct ice_vf *vf)
{
	if (vf->trusted)
		set_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
	else
		clear_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
}

1060
/**
1061 1062
 * ice_ena_vf_msix_mappings - enable VF MSIX mappings in hardware
 * @vf: VF to enable MSIX mappings for
1063
 *
1064 1065 1066
 * Some of the registers need to be indexed/configured using hardware global
 * device values and other registers need 0-based values, which represent PF
 * based values.
1067
 */
1068
static void ice_ena_vf_msix_mappings(struct ice_vf *vf)
1069
{
1070 1071
	int device_based_first_msix, device_based_last_msix;
	int pf_based_first_msix, pf_based_last_msix, v;
1072
	struct ice_pf *pf = vf->pf;
1073
	int device_based_vf_id;
1074 1075 1076 1077
	struct ice_hw *hw;
	u32 reg;

	hw = &pf->hw;
1078
	pf_based_first_msix = vf->first_vector_idx;
1079
	pf_based_last_msix = (pf_based_first_msix + pf->vfs.num_msix_per) - 1;
1080 1081 1082 1083

	device_based_first_msix = pf_based_first_msix +
		pf->hw.func_caps.common_cap.msix_vector_first_id;
	device_based_last_msix =
1084
		(device_based_first_msix + pf->vfs.num_msix_per) - 1;
1085 1086 1087 1088 1089 1090
	device_based_vf_id = vf->vf_id + hw->func_caps.vf_base_id;

	reg = (((device_based_first_msix << VPINT_ALLOC_FIRST_S) &
		VPINT_ALLOC_FIRST_M) |
	       ((device_based_last_msix << VPINT_ALLOC_LAST_S) &
		VPINT_ALLOC_LAST_M) | VPINT_ALLOC_VALID_M);
1091 1092
	wr32(hw, VPINT_ALLOC(vf->vf_id), reg);

1093
	reg = (((device_based_first_msix << VPINT_ALLOC_PCI_FIRST_S)
1094
		 & VPINT_ALLOC_PCI_FIRST_M) |
1095 1096
	       ((device_based_last_msix << VPINT_ALLOC_PCI_LAST_S) &
		VPINT_ALLOC_PCI_LAST_M) | VPINT_ALLOC_PCI_VALID_M);
1097
	wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg);
1098

1099
	/* map the interrupts to its functions */
1100 1101
	for (v = pf_based_first_msix; v <= pf_based_last_msix; v++) {
		reg = (((device_based_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
1102 1103 1104 1105 1106 1107
			GLINT_VECT2FUNC_VF_NUM_M) |
		       ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
			GLINT_VECT2FUNC_PF_NUM_M));
		wr32(hw, GLINT_VECT2FUNC(v), reg);
	}

1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
	/* Map mailbox interrupt to VF MSI-X vector 0 */
	wr32(hw, VPINT_MBX_CTL(device_based_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M);
}

/**
 * ice_ena_vf_q_mappings - enable Rx/Tx queue mappings for a VF
 * @vf: VF to enable the mappings for
 * @max_txq: max Tx queues allowed on the VF's VSI
 * @max_rxq: max Rx queues allowed on the VF's VSI
 */
static void ice_ena_vf_q_mappings(struct ice_vf *vf, u16 max_txq, u16 max_rxq)
{
	struct device *dev = ice_pf_to_dev(vf->pf);
1121
	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1122 1123 1124
	struct ice_hw *hw = &vf->pf->hw;
	u32 reg;

1125 1126 1127
	/* set regardless of mapping mode */
	wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M);

1128 1129 1130 1131 1132 1133 1134 1135
	/* VF Tx queues allocation */
	if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
		/* set the VF PF Tx queue range
		 * VFNUMQ value should be set to (number of queues - 1). A value
		 * of 0 means 1 queue and a value of 255 means 256 queues
		 */
		reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
			VPLAN_TX_QBASE_VFFIRSTQ_M) |
1136
		       (((max_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
1137 1138 1139
			VPLAN_TX_QBASE_VFNUMQ_M));
		wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
	} else {
B
Brett Creeley 已提交
1140
		dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
1141 1142
	}

1143 1144 1145
	/* set regardless of mapping mode */
	wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M);

1146 1147 1148 1149 1150 1151 1152 1153
	/* VF Rx queues allocation */
	if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
		/* set the VF PF Rx queue range
		 * VFNUMQ value should be set to (number of queues - 1). A value
		 * of 0 means 1 queue and a value of 255 means 256 queues
		 */
		reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
			VPLAN_RX_QBASE_VFFIRSTQ_M) |
1154
		       (((max_rxq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
1155 1156 1157
			VPLAN_RX_QBASE_VFNUMQ_M));
		wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
	} else {
B
Brett Creeley 已提交
1158
		dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
1159 1160 1161
	}
}

1162 1163 1164 1165 1166 1167
/**
 * ice_ena_vf_mappings - enable VF MSIX and queue mapping
 * @vf: pointer to the VF structure
 */
static void ice_ena_vf_mappings(struct ice_vf *vf)
{
1168
	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1169 1170 1171 1172 1173

	ice_ena_vf_msix_mappings(vf);
	ice_ena_vf_q_mappings(vf, vsi->alloc_txq, vsi->alloc_rxq);
}

B
Brett Creeley 已提交
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
/**
 * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space
 * @vf: VF to calculate the register index for
 * @q_vector: a q_vector associated to the VF
 */
int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector)
{
	struct ice_pf *pf;

	if (!vf || !q_vector)
		return -EINVAL;

	pf = vf->pf;

	/* always add one to account for the OICR being the first MSIX */
1189
	return pf->sriov_base_vector + pf->vfs.num_msix_per * vf->vf_id +
B
Brett Creeley 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
		q_vector->v_idx + 1;
}

/**
 * ice_get_max_valid_res_idx - Get the max valid resource index
 * @res: pointer to the resource to find the max valid index for
 *
 * Start from the end of the ice_res_tracker and return right when we find the
 * first res->list entry with the ICE_RES_VALID_BIT set. This function is only
 * valid for SR-IOV because it is the only consumer that manipulates the
 * res->end and this is always called when res->end is set to res->num_entries.
 */
static int ice_get_max_valid_res_idx(struct ice_res_tracker *res)
{
	int i;

	if (!res)
		return -EINVAL;

	for (i = res->num_entries - 1; i >= 0; i--)
		if (res->list[i] & ICE_RES_VALID_BIT)
			return i;

	return 0;
}

/**
 * ice_sriov_set_msix_res - Set any used MSIX resources
 * @pf: pointer to PF structure
 * @num_msix_needed: number of MSIX vectors needed for all SR-IOV VFs
 *
 * This function allows SR-IOV resources to be taken from the end of the PF's
M
Mitch Williams 已提交
1222 1223
 * allowed HW MSIX vectors so that the irq_tracker will not be affected. We
 * just set the pf->sriov_base_vector and return success.
B
Brett Creeley 已提交
1224
 *
M
Mitch Williams 已提交
1225 1226
 * If there are not enough resources available, return an error. This should
 * always be caught by ice_set_per_vf_res().
B
Brett Creeley 已提交
1227
 *
B
Bruce Allan 已提交
1228
 * Return 0 on success, and -EINVAL when there are not enough MSIX vectors
B
Brett Creeley 已提交
1229 1230 1231 1232
 * in the PF's space available for SR-IOV.
 */
static int ice_sriov_set_msix_res(struct ice_pf *pf, u16 num_msix_needed)
{
M
Mitch Williams 已提交
1233 1234
	u16 total_vectors = pf->hw.func_caps.common_cap.num_msix_vectors;
	int vectors_used = pf->irq_tracker->num_entries;
B
Brett Creeley 已提交
1235 1236
	int sriov_base_vector;

M
Mitch Williams 已提交
1237
	sriov_base_vector = total_vectors - num_msix_needed;
B
Brett Creeley 已提交
1238 1239 1240 1241

	/* make sure we only grab irq_tracker entries from the list end and
	 * that we have enough available MSIX vectors
	 */
M
Mitch Williams 已提交
1242
	if (sriov_base_vector < vectors_used)
B
Brett Creeley 已提交
1243 1244 1245 1246 1247 1248 1249
		return -EINVAL;

	pf->sriov_base_vector = sriov_base_vector;

	return 0;
}

1250
/**
M
Mitch Williams 已提交
1251
 * ice_set_per_vf_res - check if vectors and queues are available
1252
 * @pf: pointer to the PF structure
1253
 * @num_vfs: the number of SR-IOV VFs being configured
1254
 *
M
Mitch Williams 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
 * First, determine HW interrupts from common pool. If we allocate fewer VFs, we
 * get more vectors and can enable more queues per VF. Note that this does not
 * grab any vectors from the SW pool already allocated. Also note, that all
 * vector counts include one for each VF's miscellaneous interrupt vector
 * (i.e. OICR).
 *
 * Minimum VFs - 2 vectors, 1 queue pair
 * Small VFs - 5 vectors, 4 queue pairs
 * Medium VFs - 17 vectors, 16 queue pairs
 *
 * Second, determine number of queue pairs per VF by starting with a pre-defined
 * maximum each VF supports. If this is not possible, then we adjust based on
 * queue pairs available on the device.
 *
 * Lastly, set queue and MSI-X VF variables tracked by the PF so it can be used
 * by each VF during VF initialization and reset.
1271
 */
1272
static int ice_set_per_vf_res(struct ice_pf *pf, u16 num_vfs)
1273
{
B
Brett Creeley 已提交
1274
	int max_valid_res_idx = ice_get_max_valid_res_idx(pf->irq_tracker);
1275
	u16 num_msix_per_vf, num_txq, num_rxq, avail_qs;
1276
	int msix_avail_per_vf, msix_avail_for_sriov;
B
Brett Creeley 已提交
1277
	struct device *dev = ice_pf_to_dev(pf);
1278

1279 1280
	lockdep_assert_held(&pf->vfs.table_lock);

1281
	if (!num_vfs || max_valid_res_idx < 0)
1282 1283
		return -EINVAL;

M
Mitch Williams 已提交
1284
	/* determine MSI-X resources per VF */
1285 1286
	msix_avail_for_sriov = pf->hw.func_caps.common_cap.num_msix_vectors -
		pf->irq_tracker->num_entries;
1287
	msix_avail_per_vf = msix_avail_for_sriov / num_vfs;
1288 1289 1290 1291
	if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MED) {
		num_msix_per_vf = ICE_NUM_VF_MSIX_MED;
	} else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_SMALL) {
		num_msix_per_vf = ICE_NUM_VF_MSIX_SMALL;
1292 1293
	} else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MULTIQ_MIN) {
		num_msix_per_vf = ICE_NUM_VF_MSIX_MULTIQ_MIN;
1294 1295
	} else if (msix_avail_per_vf >= ICE_MIN_INTR_PER_VF) {
		num_msix_per_vf = ICE_MIN_INTR_PER_VF;
1296
	} else {
1297 1298
		dev_err(dev, "Only %d MSI-X interrupts available for SR-IOV. Not enough to support minimum of %d MSI-X interrupts per VF for %d VFs\n",
			msix_avail_for_sriov, ICE_MIN_INTR_PER_VF,
1299
			num_vfs);
1300 1301 1302
		return -EIO;
	}

1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
	num_txq = min_t(u16, num_msix_per_vf - ICE_NONQ_VECS_VF,
			ICE_MAX_RSS_QS_PER_VF);
	avail_qs = ice_get_avail_txq_count(pf) / num_vfs;
	if (!avail_qs)
		num_txq = 0;
	else if (num_txq > avail_qs)
		num_txq = rounddown_pow_of_two(avail_qs);

	num_rxq = min_t(u16, num_msix_per_vf - ICE_NONQ_VECS_VF,
			ICE_MAX_RSS_QS_PER_VF);
	avail_qs = ice_get_avail_rxq_count(pf) / num_vfs;
	if (!avail_qs)
		num_rxq = 0;
	else if (num_rxq > avail_qs)
		num_rxq = rounddown_pow_of_two(avail_qs);

	if (num_txq < ICE_MIN_QS_PER_VF || num_rxq < ICE_MIN_QS_PER_VF) {
1320
		dev_err(dev, "Not enough queues to support minimum of %d queue pairs per VF for %d VFs\n",
1321
			ICE_MIN_QS_PER_VF, num_vfs);
1322
		return -EIO;
M
Mitch Williams 已提交
1323
	}
1324

1325
	if (ice_sriov_set_msix_res(pf, num_msix_per_vf * num_vfs)) {
M
Mitch Williams 已提交
1326
		dev_err(dev, "Unable to set MSI-X resources for %d VFs\n",
1327
			num_vfs);
B
Brett Creeley 已提交
1328
		return -EINVAL;
M
Mitch Williams 已提交
1329
	}
B
Brett Creeley 已提交
1330

M
Mitch Williams 已提交
1331
	/* only allow equal Tx/Rx queue count (i.e. queue pairs) */
1332 1333
	pf->vfs.num_qps_per = min_t(int, num_txq, num_rxq);
	pf->vfs.num_msix_per = num_msix_per_vf;
M
Mitch Williams 已提交
1334
	dev_info(dev, "Enabling %d VFs with %d vectors and %d queues per VF\n",
1335
		 num_vfs, pf->vfs.num_msix_per, pf->vfs.num_qps_per);
1336 1337 1338 1339

	return 0;
}

1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
/**
 * ice_clear_vf_reset_trigger - enable VF to access hardware
 * @vf: VF to enabled hardware access for
 */
static void ice_clear_vf_reset_trigger(struct ice_vf *vf)
{
	struct ice_hw *hw = &vf->pf->hw;
	u32 reg;

	reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
	reg &= ~VPGEN_VFRTRIG_VFSWR_M;
	wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
	ice_flush(hw);
}

1355 1356
static int
ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
1357
{
1358
	struct ice_hw *hw = &vsi->back->hw;
T
Tony Nguyen 已提交
1359
	int status;
1360

1361
	if (ice_vf_is_port_vlan_ena(vf))
1362
		status = ice_fltr_set_vsi_promisc(hw, vsi->idx, promisc_m,
1363
						  ice_vf_get_port_vlan_id(vf));
1364
	else if (ice_vsi_has_non_zero_vlans(vsi))
1365 1366 1367 1368
		status = ice_fltr_set_vlan_vsi_promisc(hw, vsi, promisc_m);
	else
		status = ice_fltr_set_vsi_promisc(hw, vsi->idx, promisc_m, 0);

T
Tony Nguyen 已提交
1369
	if (status && status != -EEXIST) {
1370 1371
		dev_err(ice_pf_to_dev(vsi->back), "enable Tx/Rx filter promiscuous mode on VF-%u failed, error: %d\n",
			vf->vf_id, status);
T
Tony Nguyen 已提交
1372
		return status;
1373 1374
	}

1375 1376 1377 1378 1379 1380 1381
	return 0;
}

static int
ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
{
	struct ice_hw *hw = &vsi->back->hw;
T
Tony Nguyen 已提交
1382
	int status;
1383

1384
	if (ice_vf_is_port_vlan_ena(vf))
1385
		status = ice_fltr_clear_vsi_promisc(hw, vsi->idx, promisc_m,
1386
						    ice_vf_get_port_vlan_id(vf));
1387
	else if (ice_vsi_has_non_zero_vlans(vsi))
1388 1389 1390 1391
		status = ice_fltr_clear_vlan_vsi_promisc(hw, vsi, promisc_m);
	else
		status = ice_fltr_clear_vsi_promisc(hw, vsi->idx, promisc_m, 0);

T
Tony Nguyen 已提交
1392
	if (status && status != -ENOENT) {
1393 1394
		dev_err(ice_pf_to_dev(vsi->back), "disable Tx/Rx filter promiscuous mode on VF-%u failed, error: %d\n",
			vf->vf_id, status);
T
Tony Nguyen 已提交
1395
		return status;
1396 1397 1398
	}

	return 0;
1399 1400
}

B
Brett Creeley 已提交
1401 1402
static void ice_vf_clear_counters(struct ice_vf *vf)
{
1403
	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
B
Brett Creeley 已提交
1404 1405 1406 1407 1408 1409 1410

	vf->num_mac = 0;
	vsi->num_vlan = 0;
	memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
	memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
}

1411
/**
B
Brett Creeley 已提交
1412 1413
 * ice_vf_pre_vsi_rebuild - tasks to be done prior to VSI rebuild
 * @vf: VF to perform pre VSI rebuild tasks
1414
 *
B
Brett Creeley 已提交
1415 1416
 * These tasks are items that don't need to be amortized since they are most
 * likely called in a for loop with all VF(s) in the reset_all_vfs() case.
1417
 */
B
Brett Creeley 已提交
1418
static void ice_vf_pre_vsi_rebuild(struct ice_vf *vf)
1419
{
B
Brett Creeley 已提交
1420 1421 1422
	ice_vf_clear_counters(vf);
	ice_clear_vf_reset_trigger(vf);
}
1423

1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
/**
 * ice_vf_rebuild_aggregator_node_cfg - rebuild aggregator node config
 * @vsi: Pointer to VSI
 *
 * This function moves VSI into corresponding scheduler aggregator node
 * based on cached value of "aggregator node info" per VSI
 */
static void ice_vf_rebuild_aggregator_node_cfg(struct ice_vsi *vsi)
{
	struct ice_pf *pf = vsi->back;
	struct device *dev;
1435
	int status;
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456

	if (!vsi->agg_node)
		return;

	dev = ice_pf_to_dev(pf);
	if (vsi->agg_node->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
		dev_dbg(dev,
			"agg_id %u already has reached max_num_vsis %u\n",
			vsi->agg_node->agg_id, vsi->agg_node->num_vsis);
		return;
	}

	status = ice_move_vsi_to_agg(pf->hw.port_info, vsi->agg_node->agg_id,
				     vsi->idx, vsi->tc_cfg.ena_tc);
	if (status)
		dev_dbg(dev, "unable to move VSI idx %u into aggregator %u node",
			vsi->idx, vsi->agg_node->agg_id);
	else
		vsi->agg_node->num_vsis++;
}

B
Brett Creeley 已提交
1457 1458 1459 1460 1461 1462 1463
/**
 * ice_vf_rebuild_host_cfg - host admin configuration is persistent across reset
 * @vf: VF to rebuild host configuration on
 */
static void ice_vf_rebuild_host_cfg(struct ice_vf *vf)
{
	struct device *dev = ice_pf_to_dev(vf->pf);
1464
	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
B
Brett Creeley 已提交
1465 1466 1467 1468 1469 1470 1471

	ice_vf_set_host_trust_cfg(vf);

	if (ice_vf_rebuild_host_mac_cfg(vf))
		dev_err(dev, "failed to rebuild default MAC configuration for VF %d\n",
			vf->vf_id);

1472
	if (ice_vf_rebuild_host_vlan_cfg(vf, vsi))
B
Brett Creeley 已提交
1473 1474
		dev_err(dev, "failed to rebuild VLAN configuration for VF %u\n",
			vf->vf_id);
1475 1476 1477 1478 1479

	if (ice_vf_rebuild_host_tx_rate_cfg(vf))
		dev_err(dev, "failed to rebuild Tx rate limiting configuration for VF %u\n",
			vf->vf_id);

1480
	if (ice_vsi_apply_spoofchk(vsi, vf->spoofchk))
1481 1482 1483
		dev_err(dev, "failed to rebuild spoofchk configuration for VF %d\n",
			vf->vf_id);

1484 1485
	/* rebuild aggregator node config for main VF VSI */
	ice_vf_rebuild_aggregator_node_cfg(vsi);
B
Brett Creeley 已提交
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
}

/**
 * ice_vf_rebuild_vsi_with_release - release and setup the VF's VSI
 * @vf: VF to release and setup the VSI for
 *
 * This is only called when a single VF is being reset (i.e. VFR, VFLR, host VF
 * configuration change, etc.).
 */
static int ice_vf_rebuild_vsi_with_release(struct ice_vf *vf)
{
1497 1498
	ice_vf_vsi_release(vf);
	if (!ice_vf_vsi_setup(vf))
B
Brett Creeley 已提交
1499
		return -ENOMEM;
1500

B
Brett Creeley 已提交
1501 1502
	return 0;
}
1503

B
Brett Creeley 已提交
1504 1505 1506 1507 1508 1509 1510 1511 1512
/**
 * ice_vf_rebuild_vsi - rebuild the VF's VSI
 * @vf: VF to rebuild the VSI for
 *
 * This is only called when all VF(s) are being reset (i.e. PCIe Reset on the
 * host, PFR, CORER, etc.).
 */
static int ice_vf_rebuild_vsi(struct ice_vf *vf)
{
1513
	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
B
Brett Creeley 已提交
1514 1515 1516 1517 1518 1519
	struct ice_pf *pf = vf->pf;

	if (ice_vsi_rebuild(vsi, true)) {
		dev_err(ice_pf_to_dev(pf), "failed to rebuild VF %d VSI\n",
			vf->vf_id);
		return -EIO;
1520
	}
B
Brett Creeley 已提交
1521 1522 1523 1524 1525
	/* vsi->idx will remain the same in this case so don't update
	 * vf->lan_vsi_idx
	 */
	vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
	vf->lan_vsi_num = vsi->vsi_num;
1526

B
Brett Creeley 已提交
1527 1528
	return 0;
}
1529

B
Brett Creeley 已提交
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
/**
 * ice_vf_set_initialized - VF is ready for VIRTCHNL communication
 * @vf: VF to set in initialized state
 *
 * After this function the VF will be ready to receive/handle the
 * VIRTCHNL_OP_GET_VF_RESOURCES message
 */
static void ice_vf_set_initialized(struct ice_vf *vf)
{
	ice_set_vf_state_qs_dis(vf);
	clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
	clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
	clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
	set_bit(ICE_VF_STATE_INIT, vf->vf_states);
1544
	memset(&vf->vlan_v2_caps, 0, sizeof(vf->vlan_v2_caps));
B
Brett Creeley 已提交
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
}

/**
 * ice_vf_post_vsi_rebuild - tasks to do after the VF's VSI have been rebuilt
 * @vf: VF to perform tasks on
 */
static void ice_vf_post_vsi_rebuild(struct ice_vf *vf)
{
	struct ice_pf *pf = vf->pf;
	struct ice_hw *hw;

	hw = &pf->hw;

	ice_vf_rebuild_host_cfg(vf);

	ice_vf_set_initialized(vf);
	ice_ena_vf_mappings(vf);
	wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1563 1564
}

1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
/**
 * ice_reset_all_vfs - reset all allocated VFs in one go
 * @pf: pointer to the PF structure
 * @is_vflr: true if VFLR was issued, false if not
 *
 * First, tell the hardware to reset each VF, then do all the waiting in one
 * chunk, and finally finish restoring each VF after the wait. This is useful
 * during PF routines which need to reset all VFs, as otherwise it must perform
 * these resets in a serialized fashion.
 *
 * Returns true if any VFs were reset, and false otherwise.
 */
bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr)
{
B
Brett Creeley 已提交
1579
	struct device *dev = ice_pf_to_dev(pf);
1580
	struct ice_hw *hw = &pf->hw;
1581
	struct ice_vf *vf;
1582
	unsigned int bkt;
1583 1584

	/* If we don't have any VFs, then there is nothing to reset */
1585
	if (!ice_has_vfs(pf))
1586 1587
		return false;

1588 1589
	mutex_lock(&pf->vfs.table_lock);

1590
	/* clear all malicious info if the VFs are getting reset */
1591
	ice_for_each_vf(pf, bkt, vf)
1592
		if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->vfs.malvfs,
1593
					ICE_MAX_SRIOV_VFS, vf->vf_id))
1594 1595
			dev_dbg(dev, "failed to clear malicious VF state for VF %u\n",
				vf->vf_id);
1596

1597
	/* If VFs have been disabled, there is no need to reset */
1598 1599
	if (test_and_set_bit(ICE_VF_DIS, pf->state)) {
		mutex_unlock(&pf->vfs.table_lock);
1600
		return false;
1601
	}
1602 1603

	/* Begin reset on all VFs at once */
1604 1605
	ice_for_each_vf(pf, bkt, vf)
		ice_trigger_vf_reset(vf, is_vflr, true);
1606 1607 1608 1609 1610 1611 1612

	/* HW requires some time to make sure it can flush the FIFO for a VF
	 * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
	 * sequence to make sure that it has completed. We'll keep track of
	 * the VFs using a simple iterator that increments once that VF has
	 * finished resetting.
	 */
1613 1614 1615 1616 1617 1618 1619 1620 1621
	ice_for_each_vf(pf, bkt, vf) {
		bool done = false;
		unsigned int i;
		u32 reg;

		for (i = 0; i < 10; i++) {
			reg = rd32(&pf->hw, VPGEN_VFRSTAT(vf->vf_id));
			if (reg & VPGEN_VFRSTAT_VFRD_M) {
				done = true;
1622
				break;
1623
			}
1624

1625 1626 1627 1628 1629 1630 1631 1632
			/* only delay if check failed */
			usleep_range(10, 20);
		}

		if (!done) {
			/* Display a warning if at least one VF didn't manage
			 * to reset in time, but continue on with the
			 * operation.
1633
			 */
1634 1635
			dev_warn(dev, "VF %u reset check timeout\n", vf->vf_id);
			break;
1636 1637 1638 1639
		}
	}

	/* free VF resources to begin resetting the VSI state */
1640
	ice_for_each_vf(pf, bkt, vf) {
1641 1642
		mutex_lock(&vf->cfg_lock);

1643 1644 1645
		vf->driver_caps = 0;
		ice_vc_set_default_allowlist(vf);

Q
Qi Zhang 已提交
1646
		ice_vf_fdir_exit(vf);
1647
		ice_vf_fdir_init(vf);
1648 1649 1650 1651 1652 1653
		/* clean VF control VSI when resetting VFs since it should be
		 * setup only when VF creates its first FDIR rule.
		 */
		if (vf->ctrl_vsi_idx != ICE_NO_VSI)
			ice_vf_ctrl_invalidate_vsi(vf);

B
Brett Creeley 已提交
1654 1655 1656
		ice_vf_pre_vsi_rebuild(vf);
		ice_vf_rebuild_vsi(vf);
		ice_vf_post_vsi_rebuild(vf);
1657 1658

		mutex_unlock(&vf->cfg_lock);
1659
	}
1660

1661 1662 1663 1664
	if (ice_is_eswitch_mode_switchdev(pf))
		if (ice_eswitch_rebuild(pf))
			dev_warn(dev, "eswitch rebuild failed\n");

B
Brett Creeley 已提交
1665
	ice_flush(hw);
1666
	clear_bit(ICE_VF_DIS, pf->state);
1667

1668 1669
	mutex_unlock(&pf->vfs.table_lock);

1670 1671 1672
	return true;
}

1673 1674 1675 1676 1677 1678
/**
 * ice_is_vf_disabled
 * @vf: pointer to the VF info
 *
 * Returns true if the PF or VF is disabled, false otherwise.
 */
1679
bool ice_is_vf_disabled(struct ice_vf *vf)
1680 1681 1682 1683 1684 1685 1686 1687
{
	struct ice_pf *pf = vf->pf;

	/* If the PF has been disabled, there is no need resetting VF until
	 * PF is active again. Similarly, if the VF has been disabled, this
	 * means something else is resetting the VF, so we shouldn't continue.
	 * Otherwise, set disable VF state bit for actual reset, and continue.
	 */
1688
	return (test_bit(ICE_VF_DIS, pf->state) ||
1689 1690 1691
		test_bit(ICE_VF_STATE_DIS, vf->vf_states));
}

1692 1693 1694 1695 1696
/**
 * ice_reset_vf - Reset a particular VF
 * @vf: pointer to the VF structure
 * @is_vflr: true if VFLR was issued, false if not
 *
1697 1698
 * Returns true if the VF is currently in reset, resets successfully, or resets
 * are disabled and false otherwise.
1699
 */
1700
bool ice_reset_vf(struct ice_vf *vf, bool is_vflr)
1701 1702
{
	struct ice_pf *pf = vf->pf;
1703
	struct ice_vsi *vsi;
B
Brett Creeley 已提交
1704
	struct device *dev;
1705
	struct ice_hw *hw;
1706
	bool rsd = false;
1707
	u8 promisc_m;
1708 1709 1710
	u32 reg;
	int i;

1711 1712
	lockdep_assert_held(&vf->cfg_lock);

B
Brett Creeley 已提交
1713 1714
	dev = ice_pf_to_dev(pf);

1715
	if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
1716 1717 1718 1719 1720
		dev_dbg(dev, "Trying to reset VF %d, but all VF resets are disabled\n",
			vf->vf_id);
		return true;
	}

1721
	if (ice_is_vf_disabled(vf)) {
B
Brett Creeley 已提交
1722 1723
		dev_dbg(dev, "VF is already disabled, there is no need for resetting it, telling VM, all is fine %d\n",
			vf->vf_id);
1724 1725
		return true;
	}
1726

1727 1728
	/* Set VF disable bit state here, before triggering reset */
	set_bit(ICE_VF_STATE_DIS, vf->vf_states);
M
Mitch Williams 已提交
1729
	ice_trigger_vf_reset(vf, is_vflr, false);
1730

1731
	vsi = ice_get_vf_vsi(vf);
1732

1733
	ice_dis_vf_qs(vf);
1734 1735 1736 1737 1738 1739

	/* Call Disable LAN Tx queue AQ whether or not queues are
	 * enabled. This is needed for successful completion of VFR.
	 */
	ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
			NULL, ICE_VF_RESET, vf->vf_id, NULL);
1740

1741
	hw = &pf->hw;
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
	/* poll VPGEN_VFRSTAT reg to make sure
	 * that reset is complete
	 */
	for (i = 0; i < 10; i++) {
		/* VF reset requires driver to first reset the VF and then
		 * poll the status register to make sure that the reset
		 * completed successfully.
		 */
		reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
		if (reg & VPGEN_VFRSTAT_VFRD_M) {
			rsd = true;
			break;
		}
1755 1756 1757

		/* only sleep if the reset is not done */
		usleep_range(10, 20);
1758 1759
	}

1760 1761 1762
	vf->driver_caps = 0;
	ice_vc_set_default_allowlist(vf);

1763 1764 1765 1766
	/* Display a warning if VF didn't manage to reset in time, but need to
	 * continue on with the operation.
	 */
	if (!rsd)
B
Brett Creeley 已提交
1767
		dev_warn(dev, "VF reset check timeout on VF %d\n", vf->vf_id);
1768

1769 1770 1771 1772 1773
	/* disable promiscuous modes in case they were enabled
	 * ignore any error if disabling process failed
	 */
	if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
	    test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
1774
		if (ice_vf_is_port_vlan_ena(vf) || vsi->num_vlan)
1775 1776 1777 1778
			promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
		else
			promisc_m = ICE_UCAST_PROMISC_BITS;

1779
		if (ice_vf_clear_vsi_promisc(vf, vsi, promisc_m))
B
Brett Creeley 已提交
1780
			dev_err(dev, "disabling promiscuous mode failed\n");
1781 1782
	}

1783 1784
	ice_eswitch_del_vf_mac_rule(vf);

Q
Qi Zhang 已提交
1785
	ice_vf_fdir_exit(vf);
1786
	ice_vf_fdir_init(vf);
1787 1788 1789 1790 1791 1792
	/* clean VF control VSI when resetting VF since it should be setup
	 * only when VF creates its first FDIR rule.
	 */
	if (vf->ctrl_vsi_idx != ICE_NO_VSI)
		ice_vf_ctrl_vsi_release(vf);

B
Brett Creeley 已提交
1793
	ice_vf_pre_vsi_rebuild(vf);
1794 1795 1796 1797 1798 1799

	if (ice_vf_rebuild_vsi_with_release(vf)) {
		dev_err(dev, "Failed to release and setup the VF%u's VSI\n", vf->vf_id);
		return false;
	}

B
Brett Creeley 已提交
1800
	ice_vf_post_vsi_rebuild(vf);
1801 1802
	vsi = ice_get_vf_vsi(vf);
	ice_eswitch_update_repr(vsi);
1803
	ice_eswitch_replay_vf_mac_rule(vf);
1804

1805
	/* if the VF has been reset allow it to come up again */
1806
	if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->vfs.malvfs,
1807
				ICE_MAX_SRIOV_VFS, vf->vf_id))
1808 1809
		dev_dbg(dev, "failed to clear malicious VF state for VF %u\n", i);

1810 1811 1812
	return true;
}

1813 1814 1815 1816 1817 1818
/**
 * ice_vc_notify_link_state - Inform all VFs on a PF of link status
 * @pf: pointer to the PF structure
 */
void ice_vc_notify_link_state(struct ice_pf *pf)
{
1819 1820
	struct ice_vf *vf;
	unsigned int bkt;
1821

1822
	mutex_lock(&pf->vfs.table_lock);
1823 1824
	ice_for_each_vf(pf, bkt, vf)
		ice_vc_notify_vf_link_state(vf);
1825
	mutex_unlock(&pf->vfs.table_lock);
1826 1827
}

1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
/**
 * ice_vc_notify_reset - Send pending reset message to all VFs
 * @pf: pointer to the PF structure
 *
 * indicate a pending reset to all VFs on a given PF
 */
void ice_vc_notify_reset(struct ice_pf *pf)
{
	struct virtchnl_pf_event pfe;

1838
	if (!ice_has_vfs(pf))
1839 1840 1841 1842
		return;

	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1843
	ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
1844 1845 1846
			    (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
}

1847 1848 1849 1850 1851 1852 1853
/**
 * ice_vc_notify_vf_reset - Notify VF of a reset event
 * @vf: pointer to the VF structure
 */
static void ice_vc_notify_vf_reset(struct ice_vf *vf)
{
	struct virtchnl_pf_event pfe;
1854
	struct ice_pf *pf = vf->pf;
1855

1856 1857 1858 1859 1860 1861
	/* Bail out if VF is in disabled state, neither initialized, nor active
	 * state - otherwise proceed with notifications
	 */
	if ((!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
	     !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) ||
	    test_bit(ICE_VF_STATE_DIS, vf->vf_states))
1862 1863 1864 1865
		return;

	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
J
Jesse Brandeburg 已提交
1866
	ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, VIRTCHNL_OP_EVENT,
1867 1868
			      VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe, sizeof(pfe),
			      NULL);
1869 1870
}

1871 1872 1873 1874 1875 1876 1877 1878 1879
/**
 * ice_init_vf_vsi_res - initialize/setup VF VSI resources
 * @vf: VF to initialize/setup the VSI for
 *
 * This function creates a VSI for the VF, adds a VLAN 0 filter, and sets up the
 * VF VSI's broadcast filter and is only used during initial VF creation.
 */
static int ice_init_vf_vsi_res(struct ice_vf *vf)
{
1880
	struct ice_vsi_vlan_ops *vlan_ops;
1881 1882 1883 1884 1885 1886 1887 1888 1889
	struct ice_pf *pf = vf->pf;
	u8 broadcast[ETH_ALEN];
	struct ice_vsi *vsi;
	struct device *dev;
	int err;

	vf->first_vector_idx = ice_calc_vf_first_vector_idx(pf, vf);

	dev = ice_pf_to_dev(pf);
1890 1891
	vsi = ice_vf_vsi_setup(vf);
	if (!vsi)
1892 1893
		return -ENOMEM;

1894
	err = ice_vsi_add_vlan_zero(vsi);
1895 1896 1897 1898 1899 1900
	if (err) {
		dev_warn(dev, "Failed to add VLAN 0 filter for VF %d\n",
			 vf->vf_id);
		goto release_vsi;
	}

1901 1902 1903 1904 1905 1906 1907 1908
	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
	err = vlan_ops->ena_rx_filtering(vsi);
	if (err) {
		dev_warn(dev, "Failed to enable Rx VLAN filtering for VF %d\n",
			 vf->vf_id);
		goto release_vsi;
	}

1909
	eth_broadcast_addr(broadcast);
T
Tony Nguyen 已提交
1910 1911
	err = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
	if (err) {
1912
		dev_err(dev, "Failed to add broadcast MAC filter for VF %d, error %d\n",
T
Tony Nguyen 已提交
1913
			vf->vf_id, err);
1914 1915 1916
		goto release_vsi;
	}

1917
	err = ice_vsi_apply_spoofchk(vsi, vf->spoofchk);
1918 1919 1920 1921 1922 1923
	if (err) {
		dev_warn(dev, "Failed to initialize spoofchk setting for VF %d\n",
			 vf->vf_id);
		goto release_vsi;
	}

1924 1925 1926 1927 1928
	vf->num_mac = 1;

	return 0;

release_vsi:
1929
	ice_vf_vsi_release(vf);
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
	return err;
}

/**
 * ice_start_vfs - start VFs so they are ready to be used by SR-IOV
 * @pf: PF the VFs are associated with
 */
static int ice_start_vfs(struct ice_pf *pf)
{
	struct ice_hw *hw = &pf->hw;
1940 1941 1942
	unsigned int bkt, it_cnt;
	struct ice_vf *vf;
	int retval;
1943

1944 1945
	lockdep_assert_held(&pf->vfs.table_lock);

1946 1947
	it_cnt = 0;
	ice_for_each_vf(pf, bkt, vf) {
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
		ice_clear_vf_reset_trigger(vf);

		retval = ice_init_vf_vsi_res(vf);
		if (retval) {
			dev_err(ice_pf_to_dev(pf), "Failed to initialize VSI resources for VF %d, error %d\n",
				vf->vf_id, retval);
			goto teardown;
		}

		set_bit(ICE_VF_STATE_INIT, vf->vf_states);
		ice_ena_vf_mappings(vf);
		wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1960
		it_cnt++;
1961 1962 1963 1964 1965 1966
	}

	ice_flush(hw);
	return 0;

teardown:
1967 1968 1969
	ice_for_each_vf(pf, bkt, vf) {
		if (it_cnt == 0)
			break;
1970 1971

		ice_dis_vf_mappings(vf);
1972
		ice_vf_vsi_release(vf);
1973
		it_cnt--;
1974 1975 1976 1977 1978
	}

	return retval;
}

1979
/**
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
 * ice_create_vf_entries - Allocate and insert VF entries
 * @pf: pointer to the PF structure
 * @num_vfs: the number of VFs to allocate
 *
 * Allocate new VF entries and insert them into the hash table. Set some
 * basic default fields for initializing the new VFs.
 *
 * After this function exits, the hash table will have num_vfs entries
 * inserted.
 *
 * Returns 0 on success or an integer error code on failure.
1991
 */
1992
static int ice_create_vf_entries(struct ice_pf *pf, u16 num_vfs)
1993
{
1994
	struct ice_vfs *vfs = &pf->vfs;
1995
	struct ice_vf *vf;
1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
	u16 vf_id;
	int err;

	lockdep_assert_held(&vfs->table_lock);

	for (vf_id = 0; vf_id < num_vfs; vf_id++) {
		vf = kzalloc(sizeof(*vf), GFP_KERNEL);
		if (!vf) {
			err = -ENOMEM;
			goto err_free_entries;
		}
		kref_init(&vf->refcnt);
2008 2009

		vf->pf = pf;
2010 2011
		vf->vf_id = vf_id;

2012 2013 2014
		vf->vf_sw_id = pf->first_sw;
		/* assign default capabilities */
		vf->spoofchk = true;
2015
		vf->num_vf_qs = pf->vfs.num_qps_per;
2016
		ice_vc_set_default_allowlist(vf);
2017 2018 2019 2020 2021

		/* ctrl_vsi_idx will be set to a valid value only when VF
		 * creates its first fdir rule.
		 */
		ice_vf_ctrl_invalidate_vsi(vf);
Q
Qi Zhang 已提交
2022
		ice_vf_fdir_init(vf);
2023

2024
		ice_virtchnl_set_dflt_ops(vf);
2025 2026

		mutex_init(&vf->cfg_lock);
2027

2028 2029
		hash_add_rcu(vfs->table, &vf->entry, vf_id);
	}
2030 2031

	return 0;
2032 2033 2034 2035

err_free_entries:
	ice_free_vf_entries(pf);
	return err;
2036 2037 2038 2039
}

/**
 * ice_ena_vfs - enable VFs so they are ready to be used
2040
 * @pf: pointer to the PF structure
2041
 * @num_vfs: number of VFs to enable
2042
 */
2043
static int ice_ena_vfs(struct ice_pf *pf, u16 num_vfs)
2044
{
B
Brett Creeley 已提交
2045
	struct device *dev = ice_pf_to_dev(pf);
2046
	struct ice_hw *hw = &pf->hw;
2047
	int ret;
2048 2049

	/* Disable global interrupt 0 so we don't try to handle the VFLR. */
B
Brett Creeley 已提交
2050
	wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
2051
	     ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
2052
	set_bit(ICE_OICR_INTR_DIS, pf->state);
2053 2054
	ice_flush(hw);

2055
	ret = pci_enable_sriov(pf->pdev, num_vfs);
2056
	if (ret)
2057
		goto err_unroll_intr;
2058

2059
	mutex_lock(&pf->vfs.table_lock);
2060

2061
	if (ice_set_per_vf_res(pf, num_vfs)) {
2062
		dev_err(dev, "Not enough resources for %d VFs, try with fewer number of VFs\n",
2063
			num_vfs);
2064 2065 2066 2067
		ret = -ENOSPC;
		goto err_unroll_sriov;
	}

2068 2069 2070 2071 2072 2073
	ret = ice_create_vf_entries(pf, num_vfs);
	if (ret) {
		dev_err(dev, "Failed to allocate VF entries for %d VFs\n",
			num_vfs);
		goto err_unroll_sriov;
	}
2074

2075 2076 2077
	if (ice_start_vfs(pf)) {
		dev_err(dev, "Failed to start VF(s)\n");
		ret = -EAGAIN;
2078
		goto err_unroll_vf_entries;
2079
	}
2080

2081
	clear_bit(ICE_VF_DIS, pf->state);
2082

2083 2084
	ret = ice_eswitch_configure(pf);
	if (ret)
2085 2086
		goto err_unroll_sriov;

2087 2088 2089 2090
	/* rearm global interrupts */
	if (test_and_clear_bit(ICE_OICR_INTR_DIS, pf->state))
		ice_irq_dynamic_ena(hw, NULL, NULL);

2091 2092
	mutex_unlock(&pf->vfs.table_lock);

2093
	return 0;
2094

2095 2096
err_unroll_vf_entries:
	ice_free_vf_entries(pf);
2097
err_unroll_sriov:
2098
	mutex_unlock(&pf->vfs.table_lock);
2099 2100 2101 2102
	pci_disable_sriov(pf->pdev);
err_unroll_intr:
	/* rearm interrupts here */
	ice_irq_dynamic_ena(hw, NULL, NULL);
2103
	clear_bit(ICE_OICR_INTR_DIS, pf->state);
2104 2105 2106 2107 2108 2109 2110
	return ret;
}

/**
 * ice_pci_sriov_ena - Enable or change number of VFs
 * @pf: pointer to the PF structure
 * @num_vfs: number of VFs to allocate
2111 2112
 *
 * Returns 0 on success and negative on failure
2113 2114 2115 2116
 */
static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
{
	int pre_existing_vfs = pci_num_vf(pf->pdev);
B
Brett Creeley 已提交
2117
	struct device *dev = ice_pf_to_dev(pf);
2118 2119 2120 2121 2122
	int err;

	if (pre_existing_vfs && pre_existing_vfs != num_vfs)
		ice_free_vfs(pf);
	else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
2123
		return 0;
2124

2125
	if (num_vfs > pf->vfs.num_supported) {
2126
		dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
2127
			num_vfs, pf->vfs.num_supported);
2128
		return -EOPNOTSUPP;
2129 2130
	}

2131 2132
	dev_info(dev, "Enabling %d VFs\n", num_vfs);
	err = ice_ena_vfs(pf, num_vfs);
2133 2134 2135 2136 2137 2138
	if (err) {
		dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
		return err;
	}

	set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165
	return 0;
}

/**
 * ice_check_sriov_allowed - check if SR-IOV is allowed based on various checks
 * @pf: PF to enabled SR-IOV on
 */
static int ice_check_sriov_allowed(struct ice_pf *pf)
{
	struct device *dev = ice_pf_to_dev(pf);

	if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
		dev_err(dev, "This device is not capable of SR-IOV\n");
		return -EOPNOTSUPP;
	}

	if (ice_is_safe_mode(pf)) {
		dev_err(dev, "SR-IOV cannot be configured - Device is in Safe Mode\n");
		return -EOPNOTSUPP;
	}

	if (!ice_pf_state_is_nominal(pf)) {
		dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
		return -EBUSY;
	}

	return 0;
2166 2167 2168 2169 2170
}

/**
 * ice_sriov_configure - Enable or change number of VFs via sysfs
 * @pdev: pointer to a pci_dev structure
2171
 * @num_vfs: number of VFs to allocate or 0 to free VFs
2172
 *
2173 2174 2175
 * This function is called when the user updates the number of VFs in sysfs. On
 * success return whatever num_vfs was set to by the caller. Return negative on
 * failure.
2176 2177 2178 2179
 */
int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
{
	struct ice_pf *pf = pci_get_drvdata(pdev);
B
Brett Creeley 已提交
2180
	struct device *dev = ice_pf_to_dev(pf);
2181
	int err;
2182

2183 2184 2185
	err = ice_check_sriov_allowed(pf);
	if (err)
		return err;
T
Tony Nguyen 已提交
2186

2187 2188
	if (!num_vfs) {
		if (!pci_vfs_assigned(pdev)) {
2189
			ice_mbx_deinit_snapshot(&pf->hw);
2190
			ice_free_vfs(pf);
2191 2192
			if (pf->lag)
				ice_enable_lag(pf->lag);
2193 2194
			return 0;
		}
2195

B
Brett Creeley 已提交
2196
		dev_err(dev, "can't free VFs because some are assigned to VMs.\n");
2197 2198 2199
		return -EBUSY;
	}

T
Tony Nguyen 已提交
2200 2201 2202
	err = ice_mbx_init_snapshot(&pf->hw, num_vfs);
	if (err)
		return err;
2203

2204
	err = ice_pci_sriov_ena(pf, num_vfs);
2205 2206
	if (err) {
		ice_mbx_deinit_snapshot(&pf->hw);
2207
		return err;
2208
	}
2209

2210 2211
	if (pf->lag)
		ice_disable_lag(pf->lag);
2212
	return num_vfs;
2213
}
2214 2215 2216 2217 2218

/**
 * ice_process_vflr_event - Free VF resources via IRQ calls
 * @pf: pointer to the PF structure
 *
2219
 * called from the VFLR IRQ handler to
2220 2221 2222 2223 2224
 * free up VF resources and state variables
 */
void ice_process_vflr_event(struct ice_pf *pf)
{
	struct ice_hw *hw = &pf->hw;
2225 2226
	struct ice_vf *vf;
	unsigned int bkt;
2227 2228
	u32 reg;

2229
	if (!test_and_clear_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
2230
	    !ice_has_vfs(pf))
2231 2232
		return;

2233
	mutex_lock(&pf->vfs.table_lock);
2234
	ice_for_each_vf(pf, bkt, vf) {
2235 2236
		u32 reg_idx, bit_idx;

2237 2238
		reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
		bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
2239 2240
		/* read GLGEN_VFLRSTAT register to find out the flr VFs */
		reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
2241
		if (reg & BIT(bit_idx)) {
2242
			/* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
2243
			mutex_lock(&vf->cfg_lock);
2244
			ice_reset_vf(vf, true);
2245 2246
			mutex_unlock(&vf->cfg_lock);
		}
2247
	}
2248
	mutex_unlock(&pf->vfs.table_lock);
2249
}
2250 2251

/**
2252
 * ice_vc_reset_vf - Perform software reset on the VF after informing the AVF
2253 2254
 * @vf: pointer to the VF info
 */
2255
static void ice_vc_reset_vf(struct ice_vf *vf)
2256 2257 2258 2259 2260
{
	ice_vc_notify_vf_reset(vf);
	ice_reset_vf(vf, false);
}

2261 2262 2263 2264 2265 2266 2267
/**
 * ice_get_vf_from_pfq - get the VF who owns the PF space queue passed in
 * @pf: PF used to index all VFs
 * @pfq: queue index relative to the PF's function space
 *
 * If no VF is found who owns the pfq then return NULL, otherwise return a
 * pointer to the VF who owns the pfq
2268 2269 2270 2271
 *
 * If this function returns non-NULL, it acquires a reference count of the VF
 * structure. The caller is responsible for calling ice_put_vf() to drop this
 * reference.
2272 2273 2274
 */
static struct ice_vf *ice_get_vf_from_pfq(struct ice_pf *pf, u16 pfq)
{
2275 2276
	struct ice_vf *vf;
	unsigned int bkt;
2277

2278 2279
	rcu_read_lock();
	ice_for_each_vf_rcu(pf, bkt, vf) {
2280 2281 2282
		struct ice_vsi *vsi;
		u16 rxq_idx;

2283
		vsi = ice_get_vf_vsi(vf);
2284 2285

		ice_for_each_rxq(vsi, rxq_idx)
2286 2287 2288 2289 2290 2291 2292 2293 2294 2295
			if (vsi->rxq_map[rxq_idx] == pfq) {
				struct ice_vf *found;

				if (kref_get_unless_zero(&vf->refcnt))
					found = vf;
				else
					found = NULL;
				rcu_read_unlock();
				return found;
			}
2296
	}
2297
	rcu_read_unlock();
2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337

	return NULL;
}

/**
 * ice_globalq_to_pfq - convert from global queue index to PF space queue index
 * @pf: PF used for conversion
 * @globalq: global queue index used to convert to PF space queue index
 */
static u32 ice_globalq_to_pfq(struct ice_pf *pf, u32 globalq)
{
	return globalq - pf->hw.func_caps.common_cap.rxq_first_id;
}

/**
 * ice_vf_lan_overflow_event - handle LAN overflow event for a VF
 * @pf: PF that the LAN overflow event happened on
 * @event: structure holding the event information for the LAN overflow event
 *
 * Determine if the LAN overflow event was caused by a VF queue. If it was not
 * caused by a VF, do nothing. If a VF caused this LAN overflow event trigger a
 * reset on the offending VF.
 */
void
ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event)
{
	u32 gldcb_rtctq, queue;
	struct ice_vf *vf;

	gldcb_rtctq = le32_to_cpu(event->desc.params.lan_overflow.prtdcb_ruptq);
	dev_dbg(ice_pf_to_dev(pf), "GLDCB_RTCTQ: 0x%08x\n", gldcb_rtctq);

	/* event returns device global Rx queue number */
	queue = (gldcb_rtctq & GLDCB_RTCTQ_RXQNUM_M) >>
		GLDCB_RTCTQ_RXQNUM_S;

	vf = ice_get_vf_from_pfq(pf, ice_globalq_to_pfq(pf, queue));
	if (!vf)
		return;

2338
	mutex_lock(&vf->cfg_lock);
2339
	ice_vc_reset_vf(vf);
2340
	mutex_unlock(&vf->cfg_lock);
2341 2342

	ice_put_vf(vf);
2343 2344
}

2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
/**
 * ice_vc_send_msg_to_vf - Send message to VF
 * @vf: pointer to the VF info
 * @v_opcode: virtual channel opcode
 * @v_retval: virtual channel return value
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
 * send msg to VF
 */
Q
Qi Zhang 已提交
2355
int
2356 2357
ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
		      enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
2358
{
B
Brett Creeley 已提交
2359
	struct device *dev;
2360
	struct ice_pf *pf;
2361
	int aq_ret;
2362 2363

	pf = vf->pf;
B
Brett Creeley 已提交
2364 2365
	dev = ice_pf_to_dev(pf);

2366 2367
	aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
				       msg, msglen, NULL);
2368
	if (aq_ret && pf->hw.mailboxq.sq_last_status != ICE_AQ_RC_ENOSYS) {
2369 2370
		dev_info(dev, "Unable to send the message to VF %d ret %d aq_err %s\n",
			 vf->vf_id, aq_ret,
2371
			 ice_aq_str(pf->hw.mailboxq.sq_last_status));
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395
		return -EIO;
	}

	return 0;
}

/**
 * ice_vc_get_ver_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * called from the VF to request the API version used by the PF
 */
static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
{
	struct virtchnl_version_info info = {
		VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
	};

	vf->vf_ver = *(struct virtchnl_version_info *)msg;
	/* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
	if (VF_IS_V10(&vf->vf_ver))
		info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;

2396 2397
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
				     VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
2398 2399 2400
				     sizeof(struct virtchnl_version_info));
}

2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
/**
 * ice_vc_get_max_frame_size - get max frame size allowed for VF
 * @vf: VF used to determine max frame size
 *
 * Max frame size is determined based on the current port's max frame size and
 * whether a port VLAN is configured on this VF. The VF is not aware whether
 * it's in a port VLAN so the PF needs to account for this in max frame size
 * checks and sending the max frame size to the VF.
 */
static u16 ice_vc_get_max_frame_size(struct ice_vf *vf)
{
2412
	struct ice_port_info *pi = ice_vf_get_port_info(vf);
2413 2414 2415 2416
	u16 max_frame_size;

	max_frame_size = pi->phy.link_info.max_frame_size;

2417
	if (ice_vf_is_port_vlan_ena(vf))
2418 2419 2420 2421 2422
		max_frame_size -= VLAN_HLEN;

	return max_frame_size;
}

2423 2424 2425 2426 2427 2428 2429 2430 2431
/**
 * ice_vc_get_vf_res_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * called from the VF to request its resources
 */
static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
{
2432
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2433 2434 2435 2436 2437 2438
	struct virtchnl_vf_resource *vfres = NULL;
	struct ice_pf *pf = vf->pf;
	struct ice_vsi *vsi;
	int len = 0;
	int ret;

J
Jesse Brandeburg 已提交
2439
	if (ice_check_vf_init(pf, vf)) {
2440
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2441 2442 2443 2444 2445
		goto err;
	}

	len = sizeof(struct virtchnl_vf_resource);

2446
	vfres = kzalloc(len, GFP_KERNEL);
2447
	if (!vfres) {
2448
		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
		len = 0;
		goto err;
	}
	if (VF_IS_V11(&vf->vf_ver))
		vf->driver_caps = *(u32 *)msg;
	else
		vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
				  VIRTCHNL_VF_OFFLOAD_RSS_REG |
				  VIRTCHNL_VF_OFFLOAD_VLAN;

	vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
2460
	vsi = ice_get_vf_vsi(vf);
2461
	if (!vsi) {
2462
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2463 2464 2465
		goto err;
	}

2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492
	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_VLAN_V2) {
		/* VLAN offloads based on current device configuration */
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN_V2;
	} else if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_VLAN) {
		/* allow VF to negotiate VIRTCHNL_VF_OFFLOAD explicitly for
		 * these two conditions, which amounts to guest VLAN filtering
		 * and offloads being based on the inner VLAN or the
		 * inner/single VLAN respectively and don't allow VF to
		 * negotiate VIRTCHNL_VF_OFFLOAD in any other cases
		 */
		if (ice_is_dvm_ena(&pf->hw) && ice_vf_is_port_vlan_ena(vf)) {
			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
		} else if (!ice_is_dvm_ena(&pf->hw) &&
			   !ice_vf_is_port_vlan_ena(vf)) {
			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
			/* configure backward compatible support for VFs that
			 * only support VIRTCHNL_VF_OFFLOAD_VLAN, the PF is
			 * configured in SVM, and no port VLAN is configured
			 */
			ice_vf_vsi_cfg_svm_legacy_vlan_mode(vsi);
		} else if (ice_is_dvm_ena(&pf->hw)) {
			/* configure software offloaded VLAN support when DVM
			 * is enabled, but no port VLAN is enabled
			 */
			ice_vf_vsi_cfg_dvm_legacy_vlan_mode(vsi);
		}
	}
2493 2494 2495 2496 2497 2498 2499 2500 2501 2502

	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
	} else {
		if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
		else
			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
	}

Q
Qi Zhang 已提交
2503 2504 2505
	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_FDIR_PF)
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_FDIR_PF;

2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526
	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;

	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;

	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;

	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;

	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;

	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;

	if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
		vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;

Q
Qi Zhang 已提交
2527 2528 2529
	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF)
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF;

2530 2531 2532
	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_USO)
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_USO;

2533 2534 2535
	vfres->num_vsis = 1;
	/* Tx and Rx queue are equal for VF */
	vfres->num_queue_pairs = vsi->num_txq;
2536
	vfres->max_vectors = pf->vfs.num_msix_per;
2537 2538
	vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
	vfres->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
2539
	vfres->max_mtu = ice_vc_get_max_frame_size(vf);
2540 2541 2542 2543 2544

	vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
	vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
	vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
	ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
2545
			vf->hw_lan_addr.addr);
2546

2547 2548 2549
	/* match guest capabilities */
	vf->driver_caps = vfres->vf_cap_flags;

2550 2551 2552
	ice_vc_set_caps_allowlist(vf);
	ice_vc_set_working_allowlist(vf);

2553 2554 2555 2556
	set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);

err:
	/* send the response back to the VF */
2557
	ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
2558 2559
				    (u8 *)vfres, len);

2560
	kfree(vfres);
2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573
	return ret;
}

/**
 * ice_vc_reset_vf_msg
 * @vf: pointer to the VF info
 *
 * called from the VF to reset itself,
 * unlike other virtchnl messages, PF driver
 * doesn't send the response back to the VF
 */
static void ice_vc_reset_vf_msg(struct ice_vf *vf)
{
2574
	if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
2575 2576 2577 2578 2579
		ice_reset_vf(vf, false);
}

/**
 * ice_find_vsi_from_id
2580
 * @pf: the PF structure to search for the VSI
2581
 * @id: ID of the VSI it is searching for
2582
 *
2583
 * searches for the VSI with the given ID
2584 2585 2586 2587 2588
 */
static struct ice_vsi *ice_find_vsi_from_id(struct ice_pf *pf, u16 id)
{
	int i;

2589
	ice_for_each_vsi(pf, i)
2590 2591 2592 2593 2594 2595 2596 2597 2598
		if (pf->vsi[i] && pf->vsi[i]->vsi_num == id)
			return pf->vsi[i];

	return NULL;
}

/**
 * ice_vc_isvalid_vsi_id
 * @vf: pointer to the VF info
2599
 * @vsi_id: VF relative VSI ID
2600
 *
2601
 * check for the valid VSI ID
2602
 */
Q
Qi Zhang 已提交
2603
bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
2604 2605 2606 2607 2608 2609
{
	struct ice_pf *pf = vf->pf;
	struct ice_vsi *vsi;

	vsi = ice_find_vsi_from_id(pf, vsi_id);

2610
	return (vsi && (vsi->vf == vf));
2611 2612 2613 2614 2615
}

/**
 * ice_vc_isvalid_q_id
 * @vf: pointer to the VF info
2616 2617
 * @vsi_id: VSI ID
 * @qid: VSI relative queue ID
2618
 *
2619
 * check for the valid queue ID
2620 2621 2622 2623 2624 2625 2626 2627
 */
static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
{
	struct ice_vsi *vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
	/* allocated Tx and Rx queues should be always equal for VF VSI */
	return (vsi && (qid < vsi->alloc_txq));
}

2628 2629 2630 2631 2632
/**
 * ice_vc_isvalid_ring_len
 * @ring_len: length of ring
 *
 * check for the valid ring count, should be multiple of ICE_REQ_DESC_MULTIPLE
2633
 * or zero
2634 2635 2636
 */
static bool ice_vc_isvalid_ring_len(u16 ring_len)
{
2637 2638
	return ring_len == 0 ||
	       (ring_len >= ICE_MIN_NUM_DESC &&
2639 2640 2641 2642
		ring_len <= ICE_MAX_NUM_DESC &&
		!(ring_len % ICE_REQ_DESC_MULTIPLE));
}

J
Jeff Guo 已提交
2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736
/**
 * ice_vc_validate_pattern
 * @vf: pointer to the VF info
 * @proto: virtchnl protocol headers
 *
 * validate the pattern is supported or not.
 *
 * Return: true on success, false on error.
 */
bool
ice_vc_validate_pattern(struct ice_vf *vf, struct virtchnl_proto_hdrs *proto)
{
	bool is_ipv4 = false;
	bool is_ipv6 = false;
	bool is_udp = false;
	u16 ptype = -1;
	int i = 0;

	while (i < proto->count &&
	       proto->proto_hdr[i].type != VIRTCHNL_PROTO_HDR_NONE) {
		switch (proto->proto_hdr[i].type) {
		case VIRTCHNL_PROTO_HDR_ETH:
			ptype = ICE_PTYPE_MAC_PAY;
			break;
		case VIRTCHNL_PROTO_HDR_IPV4:
			ptype = ICE_PTYPE_IPV4_PAY;
			is_ipv4 = true;
			break;
		case VIRTCHNL_PROTO_HDR_IPV6:
			ptype = ICE_PTYPE_IPV6_PAY;
			is_ipv6 = true;
			break;
		case VIRTCHNL_PROTO_HDR_UDP:
			if (is_ipv4)
				ptype = ICE_PTYPE_IPV4_UDP_PAY;
			else if (is_ipv6)
				ptype = ICE_PTYPE_IPV6_UDP_PAY;
			is_udp = true;
			break;
		case VIRTCHNL_PROTO_HDR_TCP:
			if (is_ipv4)
				ptype = ICE_PTYPE_IPV4_TCP_PAY;
			else if (is_ipv6)
				ptype = ICE_PTYPE_IPV6_TCP_PAY;
			break;
		case VIRTCHNL_PROTO_HDR_SCTP:
			if (is_ipv4)
				ptype = ICE_PTYPE_IPV4_SCTP_PAY;
			else if (is_ipv6)
				ptype = ICE_PTYPE_IPV6_SCTP_PAY;
			break;
		case VIRTCHNL_PROTO_HDR_GTPU_IP:
		case VIRTCHNL_PROTO_HDR_GTPU_EH:
			if (is_ipv4)
				ptype = ICE_MAC_IPV4_GTPU;
			else if (is_ipv6)
				ptype = ICE_MAC_IPV6_GTPU;
			goto out;
		case VIRTCHNL_PROTO_HDR_L2TPV3:
			if (is_ipv4)
				ptype = ICE_MAC_IPV4_L2TPV3;
			else if (is_ipv6)
				ptype = ICE_MAC_IPV6_L2TPV3;
			goto out;
		case VIRTCHNL_PROTO_HDR_ESP:
			if (is_ipv4)
				ptype = is_udp ? ICE_MAC_IPV4_NAT_T_ESP :
						ICE_MAC_IPV4_ESP;
			else if (is_ipv6)
				ptype = is_udp ? ICE_MAC_IPV6_NAT_T_ESP :
						ICE_MAC_IPV6_ESP;
			goto out;
		case VIRTCHNL_PROTO_HDR_AH:
			if (is_ipv4)
				ptype = ICE_MAC_IPV4_AH;
			else if (is_ipv6)
				ptype = ICE_MAC_IPV6_AH;
			goto out;
		case VIRTCHNL_PROTO_HDR_PFCP:
			if (is_ipv4)
				ptype = ICE_MAC_IPV4_PFCP_SESSION;
			else if (is_ipv6)
				ptype = ICE_MAC_IPV6_PFCP_SESSION;
			goto out;
		default:
			break;
		}
		i++;
	}

out:
	return ice_hw_ptype_ena(&vf->pf->hw, ptype);
}

Q
Qi Zhang 已提交
2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759
/**
 * ice_vc_parse_rss_cfg - parses hash fields and headers from
 * a specific virtchnl RSS cfg
 * @hw: pointer to the hardware
 * @rss_cfg: pointer to the virtchnl RSS cfg
 * @addl_hdrs: pointer to the protocol header fields (ICE_FLOW_SEG_HDR_*)
 * to configure
 * @hash_flds: pointer to the hash bit fields (ICE_FLOW_HASH_*) to configure
 *
 * Return true if all the protocol header and hash fields in the RSS cfg could
 * be parsed, else return false
 *
 * This function parses the virtchnl RSS cfg to be the intended
 * hash fields and the intended header for RSS configuration
 */
static bool
ice_vc_parse_rss_cfg(struct ice_hw *hw, struct virtchnl_rss_cfg *rss_cfg,
		     u32 *addl_hdrs, u64 *hash_flds)
{
	const struct ice_vc_hash_field_match_type *hf_list;
	const struct ice_vc_hdr_match_type *hdr_list;
	int i, hf_list_len, hdr_list_len;

J
Jeff Guo 已提交
2760 2761 2762 2763
	hf_list = ice_vc_hash_field_list;
	hf_list_len = ARRAY_SIZE(ice_vc_hash_field_list);
	hdr_list = ice_vc_hdr_list;
	hdr_list_len = ARRAY_SIZE(ice_vc_hdr_list);
Q
Qi Zhang 已提交
2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864

	for (i = 0; i < rss_cfg->proto_hdrs.count; i++) {
		struct virtchnl_proto_hdr *proto_hdr =
					&rss_cfg->proto_hdrs.proto_hdr[i];
		bool hdr_found = false;
		int j;

		/* Find matched ice headers according to virtchnl headers. */
		for (j = 0; j < hdr_list_len; j++) {
			struct ice_vc_hdr_match_type hdr_map = hdr_list[j];

			if (proto_hdr->type == hdr_map.vc_hdr) {
				*addl_hdrs |= hdr_map.ice_hdr;
				hdr_found = true;
			}
		}

		if (!hdr_found)
			return false;

		/* Find matched ice hash fields according to
		 * virtchnl hash fields.
		 */
		for (j = 0; j < hf_list_len; j++) {
			struct ice_vc_hash_field_match_type hf_map = hf_list[j];

			if (proto_hdr->type == hf_map.vc_hdr &&
			    proto_hdr->field_selector == hf_map.vc_hash_field) {
				*hash_flds |= hf_map.ice_hash_field;
				break;
			}
		}
	}

	return true;
}

/**
 * ice_vf_adv_rss_offload_ena - determine if capabilities support advanced
 * RSS offloads
 * @caps: VF driver negotiated capabilities
 *
 * Return true if VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF capability is set,
 * else return false
 */
static bool ice_vf_adv_rss_offload_ena(u32 caps)
{
	return !!(caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF);
}

/**
 * ice_vc_handle_rss_cfg
 * @vf: pointer to the VF info
 * @msg: pointer to the message buffer
 * @add: add a RSS config if true, otherwise delete a RSS config
 *
 * This function adds/deletes a RSS config
 */
static int ice_vc_handle_rss_cfg(struct ice_vf *vf, u8 *msg, bool add)
{
	u32 v_opcode = add ? VIRTCHNL_OP_ADD_RSS_CFG : VIRTCHNL_OP_DEL_RSS_CFG;
	struct virtchnl_rss_cfg *rss_cfg = (struct virtchnl_rss_cfg *)msg;
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
	struct device *dev = ice_pf_to_dev(vf->pf);
	struct ice_hw *hw = &vf->pf->hw;
	struct ice_vsi *vsi;

	if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
		dev_dbg(dev, "VF %d attempting to configure RSS, but RSS is not supported by the PF\n",
			vf->vf_id);
		v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
		goto error_param;
	}

	if (!ice_vf_adv_rss_offload_ena(vf->driver_caps)) {
		dev_dbg(dev, "VF %d attempting to configure RSS, but Advanced RSS offload is not supported\n",
			vf->vf_id);
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto error_param;
	}

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto error_param;
	}

	if (rss_cfg->proto_hdrs.count > VIRTCHNL_MAX_NUM_PROTO_HDRS ||
	    rss_cfg->rss_algorithm < VIRTCHNL_RSS_ALG_TOEPLITZ_ASYMMETRIC ||
	    rss_cfg->rss_algorithm > VIRTCHNL_RSS_ALG_XOR_SYMMETRIC) {
		dev_dbg(dev, "VF %d attempting to configure RSS, but RSS configuration is not valid\n",
			vf->vf_id);
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto error_param;
	}

	vsi = ice_get_vf_vsi(vf);
	if (!vsi) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto error_param;
	}

J
Jeff Guo 已提交
2865 2866 2867 2868 2869
	if (!ice_vc_validate_pattern(vf, &rss_cfg->proto_hdrs)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto error_param;
	}

Q
Qi Zhang 已提交
2870 2871 2872
	if (rss_cfg->rss_algorithm == VIRTCHNL_RSS_ALG_R_ASYMMETRIC) {
		struct ice_vsi_ctx *ctx;
		u8 lut_type, hash_type;
2873
		int status;
Q
Qi Zhang 已提交
2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901

		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
		hash_type = add ? ICE_AQ_VSI_Q_OPT_RSS_XOR :
				ICE_AQ_VSI_Q_OPT_RSS_TPLZ;

		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
		if (!ctx) {
			v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
			goto error_param;
		}

		ctx->info.q_opt_rss = ((lut_type <<
					ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
				       ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
				       (hash_type &
					ICE_AQ_VSI_Q_OPT_RSS_HASH_M);

		/* Preserve existing queueing option setting */
		ctx->info.q_opt_rss |= (vsi->info.q_opt_rss &
					  ICE_AQ_VSI_Q_OPT_RSS_GBL_LUT_M);
		ctx->info.q_opt_tc = vsi->info.q_opt_tc;
		ctx->info.q_opt_flags = vsi->info.q_opt_rss;

		ctx->info.valid_sections =
				cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);

		status = ice_update_vsi(hw, vsi->idx, ctx, NULL);
		if (status) {
2902
			dev_err(dev, "update VSI for RSS failed, err %d aq_err %s\n",
2903
				status, ice_aq_str(hw->adminq.sq_last_status));
Q
Qi Zhang 已提交
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927
			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		} else {
			vsi->info.q_opt_rss = ctx->info.q_opt_rss;
		}

		kfree(ctx);
	} else {
		u32 addl_hdrs = ICE_FLOW_SEG_HDR_NONE;
		u64 hash_flds = ICE_HASH_INVALID;

		if (!ice_vc_parse_rss_cfg(hw, rss_cfg, &addl_hdrs,
					  &hash_flds)) {
			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
			goto error_param;
		}

		if (add) {
			if (ice_add_rss_cfg(hw, vsi->idx, hash_flds,
					    addl_hdrs)) {
				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
				dev_err(dev, "ice_add_rss_cfg failed for vsi = %d, v_ret = %d\n",
					vsi->vsi_num, v_ret);
			}
		} else {
T
Tony Nguyen 已提交
2928
			int status;
2929 2930 2931

			status = ice_rem_rss_cfg(hw, vsi->idx, hash_flds,
						 addl_hdrs);
2932 2933 2934
			/* We just ignore -ENOENT, because if two configurations
			 * share the same profile remove one of them actually
			 * removes both, since the profile is deleted.
2935
			 */
T
Tony Nguyen 已提交
2936
			if (status && status != -ENOENT) {
2937
				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2938 2939
				dev_err(dev, "ice_rem_rss_cfg failed for VF ID:%d, error:%d\n",
					vf->vf_id, status);
2940
			}
Q
Qi Zhang 已提交
2941 2942 2943 2944 2945 2946 2947
		}
	}

error_param:
	return ice_vc_send_msg_to_vf(vf, v_opcode, v_ret, NULL, 0);
}

2948 2949 2950 2951 2952 2953 2954 2955 2956
/**
 * ice_vc_config_rss_key
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * Configure the VF's RSS key
 */
static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
{
2957
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2958 2959
	struct virtchnl_rss_key *vrk =
		(struct virtchnl_rss_key *)msg;
J
Jesse Brandeburg 已提交
2960
	struct ice_vsi *vsi;
2961 2962

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2963
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2964 2965 2966 2967
		goto error_param;
	}

	if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
2968
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2969 2970 2971
		goto error_param;
	}

2972
	if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
2973
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2974 2975 2976
		goto error_param;
	}

2977
	if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2978
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2979 2980 2981
		goto error_param;
	}

2982
	vsi = ice_get_vf_vsi(vf);
2983
	if (!vsi) {
2984
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2985 2986 2987
		goto error_param;
	}

2988
	if (ice_set_rss_key(vsi, vrk->key))
2989
		v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2990
error_param:
2991
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004
				     NULL, 0);
}

/**
 * ice_vc_config_rss_lut
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * Configure the VF's RSS LUT
 */
static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
{
	struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
3005
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
J
Jesse Brandeburg 已提交
3006
	struct ice_vsi *vsi;
3007 3008

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3009
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3010 3011 3012 3013
		goto error_param;
	}

	if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
3014
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3015 3016 3017
		goto error_param;
	}

3018
	if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE) {
3019
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3020 3021 3022
		goto error_param;
	}

3023
	if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
3024
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3025 3026 3027
		goto error_param;
	}

3028
	vsi = ice_get_vf_vsi(vf);
3029
	if (!vsi) {
3030
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3031 3032 3033
		goto error_param;
	}

3034
	if (ice_set_rss_lut(vsi, vrl->lut, ICE_VSIQF_HLUT_ARRAY_SIZE))
3035
		v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
3036
error_param:
3037
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
3038 3039 3040
				     NULL, 0);
}

3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066
/**
 * ice_wait_on_vf_reset - poll to make sure a given VF is ready after reset
 * @vf: The VF being resseting
 *
 * The max poll time is about ~800ms, which is about the maximum time it takes
 * for a VF to be reset and/or a VF driver to be removed.
 */
static void ice_wait_on_vf_reset(struct ice_vf *vf)
{
	int i;

	for (i = 0; i < ICE_MAX_VF_RESET_TRIES; i++) {
		if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
			break;
		msleep(ICE_MAX_VF_RESET_SLEEP_MS);
	}
}

/**
 * ice_check_vf_ready_for_cfg - check if VF is ready to be configured/queried
 * @vf: VF to check if it's ready to be configured/queried
 *
 * The purpose of this function is to make sure the VF is not in reset, not
 * disabled, and initialized so it can be configured and/or queried by a host
 * administrator.
 */
3067
int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082
{
	struct ice_pf *pf;

	ice_wait_on_vf_reset(vf);

	if (ice_is_vf_disabled(vf))
		return -EINVAL;

	pf = vf->pf;
	if (ice_check_vf_init(pf, vf))
		return -EBUSY;

	return 0;
}

B
Brett Creeley 已提交
3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097
/**
 * ice_set_vf_spoofchk
 * @netdev: network interface device structure
 * @vf_id: VF identifier
 * @ena: flag to enable or disable feature
 *
 * Enable or disable VF spoof checking
 */
int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
{
	struct ice_netdev_priv *np = netdev_priv(netdev);
	struct ice_pf *pf = np->vsi->back;
	struct ice_vsi *vf_vsi;
	struct device *dev;
	struct ice_vf *vf;
3098
	int ret;
B
Brett Creeley 已提交
3099 3100

	dev = ice_pf_to_dev(pf);
3101 3102 3103

	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
B
Brett Creeley 已提交
3104 3105
		return -EINVAL;

3106 3107
	ret = ice_check_vf_ready_for_cfg(vf);
	if (ret)
3108
		goto out_put_vf;
B
Brett Creeley 已提交
3109

3110
	vf_vsi = ice_get_vf_vsi(vf);
B
Brett Creeley 已提交
3111 3112 3113
	if (!vf_vsi) {
		netdev_err(netdev, "VSI %d for VF %d is null\n",
			   vf->lan_vsi_idx, vf->vf_id);
3114 3115
		ret = -EINVAL;
		goto out_put_vf;
B
Brett Creeley 已提交
3116 3117 3118
	}

	if (vf_vsi->type != ICE_VSI_VF) {
3119
		netdev_err(netdev, "Type %d of VSI %d for VF %d is no ICE_VSI_VF\n",
B
Brett Creeley 已提交
3120
			   vf_vsi->type, vf_vsi->vsi_num, vf->vf_id);
3121 3122
		ret = -ENODEV;
		goto out_put_vf;
B
Brett Creeley 已提交
3123 3124 3125 3126
	}

	if (ena == vf->spoofchk) {
		dev_dbg(dev, "VF spoofchk already %s\n", ena ? "ON" : "OFF");
3127 3128
		ret = 0;
		goto out_put_vf;
B
Brett Creeley 已提交
3129 3130
	}

3131
	ret = ice_vsi_apply_spoofchk(vf_vsi, ena);
3132 3133 3134 3135 3136
	if (ret)
		dev_err(dev, "Failed to set spoofchk %s for VF %d VSI %d\n error %d\n",
			ena ? "ON" : "OFF", vf->vf_id, vf_vsi->vsi_num, ret);
	else
		vf->spoofchk = ena;
B
Brett Creeley 已提交
3137

3138 3139
out_put_vf:
	ice_put_vf(vf);
B
Brett Creeley 已提交
3140 3141 3142
	return ret;
}

B
Brett Creeley 已提交
3143 3144 3145 3146 3147 3148 3149 3150 3151
/**
 * ice_is_any_vf_in_promisc - check if any VF(s) are in promiscuous mode
 * @pf: PF structure for accessing VF(s)
 *
 * Return false if no VF(s) are in unicast and/or multicast promiscuous mode,
 * else return true
 */
bool ice_is_any_vf_in_promisc(struct ice_pf *pf)
{
3152
	bool is_vf_promisc = false;
3153 3154
	struct ice_vf *vf;
	unsigned int bkt;
B
Brett Creeley 已提交
3155

3156 3157
	rcu_read_lock();
	ice_for_each_vf_rcu(pf, bkt, vf) {
B
Brett Creeley 已提交
3158 3159
		/* found a VF that has promiscuous mode configured */
		if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
3160 3161 3162 3163
		    test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
			is_vf_promisc = true;
			break;
		}
B
Brett Creeley 已提交
3164
	}
3165
	rcu_read_unlock();
B
Brett Creeley 已提交
3166

3167
	return is_vf_promisc;
B
Brett Creeley 已提交
3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179
}

/**
 * ice_vc_cfg_promiscuous_mode_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * called from the VF to configure VF VSIs promiscuous mode
 */
static int ice_vc_cfg_promiscuous_mode_msg(struct ice_vf *vf, u8 *msg)
{
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3180
	bool rm_promisc, alluni = false, allmulti = false;
B
Brett Creeley 已提交
3181 3182
	struct virtchnl_promisc_info *info =
	    (struct virtchnl_promisc_info *)msg;
3183
	struct ice_vsi_vlan_ops *vlan_ops;
3184
	int mcast_err = 0, ucast_err = 0;
B
Brett Creeley 已提交
3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199
	struct ice_pf *pf = vf->pf;
	struct ice_vsi *vsi;
	struct device *dev;
	int ret = 0;

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto error_param;
	}

	if (!ice_vc_isvalid_vsi_id(vf, info->vsi_id)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto error_param;
	}

3200
	vsi = ice_get_vf_vsi(vf);
B
Brett Creeley 已提交
3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213
	if (!vsi) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto error_param;
	}

	dev = ice_pf_to_dev(pf);
	if (!test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
		dev_err(dev, "Unprivileged VF %d is attempting to configure promiscuous mode\n",
			vf->vf_id);
		/* Leave v_ret alone, lie to the VF on purpose. */
		goto error_param;
	}

3214 3215 3216 3217 3218 3219 3220
	if (info->flags & FLAG_VF_UNICAST_PROMISC)
		alluni = true;

	if (info->flags & FLAG_VF_MULTICAST_PROMISC)
		allmulti = true;

	rm_promisc = !allmulti && !alluni;
B
Brett Creeley 已提交
3221

3222 3223 3224 3225 3226 3227 3228 3229 3230
	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
	if (rm_promisc)
		ret = vlan_ops->ena_rx_filtering(vsi);
	else
		ret = vlan_ops->dis_rx_filtering(vsi);
	if (ret) {
		dev_err(dev, "Failed to configure VLAN pruning in promiscuous mode\n");
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto error_param;
B
Brett Creeley 已提交
3231 3232 3233
	}

	if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
3234
		bool set_dflt_vsi = alluni || allmulti;
B
Brett Creeley 已提交
3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254

		if (set_dflt_vsi && !ice_is_dflt_vsi_in_use(pf->first_sw))
			/* only attempt to set the default forwarding VSI if
			 * it's not currently set
			 */
			ret = ice_set_dflt_vsi(pf->first_sw, vsi);
		else if (!set_dflt_vsi &&
			 ice_is_vsi_dflt_vsi(pf->first_sw, vsi))
			/* only attempt to free the default forwarding VSI if we
			 * are the owner
			 */
			ret = ice_clear_dflt_vsi(pf->first_sw);

		if (ret) {
			dev_err(dev, "%sable VF %d as the default VSI failed, error %d\n",
				set_dflt_vsi ? "en" : "dis", vf->vf_id, ret);
			v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
			goto error_param;
		}
	} else {
B
Brett Creeley 已提交
3255 3256
		u8 mcast_m, ucast_m;

3257 3258
		if (ice_vf_is_port_vlan_ena(vf) ||
		    ice_vsi_has_non_zero_vlans(vsi)) {
B
Brett Creeley 已提交
3259 3260
			mcast_m = ICE_MCAST_VLAN_PROMISC_BITS;
			ucast_m = ICE_UCAST_VLAN_PROMISC_BITS;
B
Brett Creeley 已提交
3261
		} else {
B
Brett Creeley 已提交
3262 3263
			mcast_m = ICE_MCAST_PROMISC_BITS;
			ucast_m = ICE_UCAST_PROMISC_BITS;
B
Brett Creeley 已提交
3264 3265
		}

3266 3267 3268 3269
		if (alluni)
			ucast_err = ice_vf_set_vsi_promisc(vf, vsi, ucast_m);
		else
			ucast_err = ice_vf_clear_vsi_promisc(vf, vsi, ucast_m);
B
Brett Creeley 已提交
3270

3271 3272 3273 3274 3275 3276 3277
		if (allmulti)
			mcast_err = ice_vf_set_vsi_promisc(vf, vsi, mcast_m);
		else
			mcast_err = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);

		if (ucast_err || mcast_err)
			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
B
Brett Creeley 已提交
3278 3279
	}

3280
	if (!mcast_err) {
B
Brett Creeley 已提交
3281 3282 3283 3284 3285 3286 3287 3288
		if (allmulti &&
		    !test_and_set_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
			dev_info(dev, "VF %u successfully set multicast promiscuous mode\n",
				 vf->vf_id);
		else if (!allmulti && test_and_clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
			dev_info(dev, "VF %u successfully unset multicast promiscuous mode\n",
				 vf->vf_id);
	}
B
Brett Creeley 已提交
3289

3290
	if (!ucast_err) {
B
Brett Creeley 已提交
3291 3292 3293 3294 3295 3296 3297
		if (alluni && !test_and_set_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
			dev_info(dev, "VF %u successfully set unicast promiscuous mode\n",
				 vf->vf_id);
		else if (!alluni && test_and_clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
			dev_info(dev, "VF %u successfully unset unicast promiscuous mode\n",
				 vf->vf_id);
	}
B
Brett Creeley 已提交
3298 3299 3300 3301 3302 3303

error_param:
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
				     v_ret, NULL, 0);
}

3304 3305 3306 3307 3308 3309 3310 3311 3312
/**
 * ice_vc_get_stats_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * called from the VF to get VSI stats
 */
static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
{
3313
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3314 3315
	struct virtchnl_queue_select *vqs =
		(struct virtchnl_queue_select *)msg;
J
Jesse Brandeburg 已提交
3316
	struct ice_eth_stats stats = { 0 };
3317 3318 3319
	struct ice_vsi *vsi;

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3320
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3321 3322 3323 3324
		goto error_param;
	}

	if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3325
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3326 3327 3328
		goto error_param;
	}

3329
	vsi = ice_get_vf_vsi(vf);
3330
	if (!vsi) {
3331
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3332 3333 3334 3335 3336 3337 3338 3339 3340
		goto error_param;
	}

	ice_update_eth_stats(vsi);

	stats = vsi->eth_stats;

error_param:
	/* send the response to the VF */
3341
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
3342 3343 3344
				     (u8 *)&stats, sizeof(stats));
}

3345 3346 3347 3348 3349 3350 3351 3352 3353
/**
 * ice_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTCHNL
 * @vqs: virtchnl_queue_select structure containing bitmaps to validate
 *
 * Return true on successful validation, else false
 */
static bool ice_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
{
	if ((!vqs->rx_queues && !vqs->tx_queues) ||
M
Mitch Williams 已提交
3354 3355
	    vqs->rx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF) ||
	    vqs->tx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF))
3356 3357 3358 3359 3360
		return false;

	return true;
}

3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406
/**
 * ice_vf_ena_txq_interrupt - enable Tx queue interrupt via QINT_TQCTL
 * @vsi: VSI of the VF to configure
 * @q_idx: VF queue index used to determine the queue in the PF's space
 */
static void ice_vf_ena_txq_interrupt(struct ice_vsi *vsi, u32 q_idx)
{
	struct ice_hw *hw = &vsi->back->hw;
	u32 pfq = vsi->txq_map[q_idx];
	u32 reg;

	reg = rd32(hw, QINT_TQCTL(pfq));

	/* MSI-X index 0 in the VF's space is always for the OICR, which means
	 * this is most likely a poll mode VF driver, so don't enable an
	 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
	 */
	if (!(reg & QINT_TQCTL_MSIX_INDX_M))
		return;

	wr32(hw, QINT_TQCTL(pfq), reg | QINT_TQCTL_CAUSE_ENA_M);
}

/**
 * ice_vf_ena_rxq_interrupt - enable Tx queue interrupt via QINT_RQCTL
 * @vsi: VSI of the VF to configure
 * @q_idx: VF queue index used to determine the queue in the PF's space
 */
static void ice_vf_ena_rxq_interrupt(struct ice_vsi *vsi, u32 q_idx)
{
	struct ice_hw *hw = &vsi->back->hw;
	u32 pfq = vsi->rxq_map[q_idx];
	u32 reg;

	reg = rd32(hw, QINT_RQCTL(pfq));

	/* MSI-X index 0 in the VF's space is always for the OICR, which means
	 * this is most likely a poll mode VF driver, so don't enable an
	 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
	 */
	if (!(reg & QINT_RQCTL_MSIX_INDX_M))
		return;

	wr32(hw, QINT_RQCTL(pfq), reg | QINT_RQCTL_CAUSE_ENA_M);
}

3407 3408 3409 3410 3411 3412 3413 3414 3415
/**
 * ice_vc_ena_qs_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * called from the VF to enable all or specific queue(s)
 */
static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
{
3416
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3417 3418 3419
	struct virtchnl_queue_select *vqs =
	    (struct virtchnl_queue_select *)msg;
	struct ice_vsi *vsi;
3420 3421
	unsigned long q_map;
	u16 vf_q_id;
3422 3423

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3424
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3425 3426 3427 3428
		goto error_param;
	}

	if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3429
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3430 3431 3432
		goto error_param;
	}

3433
	if (!ice_vc_validate_vqs_bitmaps(vqs)) {
3434 3435 3436 3437
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto error_param;
	}

3438
	vsi = ice_get_vf_vsi(vf);
3439
	if (!vsi) {
3440
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3441 3442 3443 3444 3445 3446 3447
		goto error_param;
	}

	/* Enable only Rx rings, Tx rings were enabled by the FW when the
	 * Tx queue group list was configured and the context bits were
	 * programmed using ice_vsi_cfg_txqs
	 */
3448
	q_map = vqs->rx_queues;
M
Mitch Williams 已提交
3449
	for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3450 3451 3452 3453 3454 3455 3456 3457 3458
		if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
			goto error_param;
		}

		/* Skip queue if enabled */
		if (test_bit(vf_q_id, vf->rxq_ena))
			continue;

3459
		if (ice_vsi_ctrl_one_rx_ring(vsi, true, vf_q_id, true)) {
3460
			dev_err(ice_pf_to_dev(vsi->back), "Failed to enable Rx ring %d on VSI %d\n",
3461 3462 3463 3464 3465
				vf_q_id, vsi->vsi_num);
			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
			goto error_param;
		}

3466
		ice_vf_ena_rxq_interrupt(vsi, vf_q_id);
3467 3468 3469 3470
		set_bit(vf_q_id, vf->rxq_ena);
	}

	q_map = vqs->tx_queues;
M
Mitch Williams 已提交
3471
	for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3472 3473 3474 3475 3476 3477 3478 3479 3480
		if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
			goto error_param;
		}

		/* Skip queue if enabled */
		if (test_bit(vf_q_id, vf->txq_ena))
			continue;

3481
		ice_vf_ena_txq_interrupt(vsi, vf_q_id);
3482 3483
		set_bit(vf_q_id, vf->txq_ena);
	}
3484 3485

	/* Set flag to indicate that queues are enabled */
3486
	if (v_ret == VIRTCHNL_STATUS_SUCCESS)
3487
		set_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
3488 3489 3490

error_param:
	/* send the response to the VF */
3491
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504
				     NULL, 0);
}

/**
 * ice_vc_dis_qs_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * called from the VF to disable all or specific
 * queue(s)
 */
static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
{
3505
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3506 3507 3508
	struct virtchnl_queue_select *vqs =
	    (struct virtchnl_queue_select *)msg;
	struct ice_vsi *vsi;
3509 3510
	unsigned long q_map;
	u16 vf_q_id;
3511 3512

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
3513
	    !test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states)) {
3514
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3515 3516 3517 3518
		goto error_param;
	}

	if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3519
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3520 3521 3522
		goto error_param;
	}

3523
	if (!ice_vc_validate_vqs_bitmaps(vqs)) {
3524
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3525 3526 3527
		goto error_param;
	}

3528
	vsi = ice_get_vf_vsi(vf);
3529
	if (!vsi) {
3530
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3531 3532 3533
		goto error_param;
	}

3534 3535 3536
	if (vqs->tx_queues) {
		q_map = vqs->tx_queues;

M
Mitch Williams 已提交
3537
		for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3538
			struct ice_tx_ring *ring = vsi->tx_rings[vf_q_id];
3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553
			struct ice_txq_meta txq_meta = { 0 };

			if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
				goto error_param;
			}

			/* Skip queue if not enabled */
			if (!test_bit(vf_q_id, vf->txq_ena))
				continue;

			ice_fill_txq_meta(vsi, ring, &txq_meta);

			if (ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, vf->vf_id,
						 ring, &txq_meta)) {
3554
				dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Tx ring %d on VSI %d\n",
3555 3556 3557 3558 3559 3560 3561 3562
					vf_q_id, vsi->vsi_num);
				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
				goto error_param;
			}

			/* Clear enabled queues flag */
			clear_bit(vf_q_id, vf->txq_ena);
		}
3563 3564
	}

3565 3566 3567
	q_map = vqs->rx_queues;
	/* speed up Rx queue disable by batching them if possible */
	if (q_map &&
M
Mitch Williams 已提交
3568
	    bitmap_equal(&q_map, vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF)) {
3569 3570 3571 3572 3573 3574
		if (ice_vsi_stop_all_rx_rings(vsi)) {
			dev_err(ice_pf_to_dev(vsi->back), "Failed to stop all Rx rings on VSI %d\n",
				vsi->vsi_num);
			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
			goto error_param;
		}
3575

M
Mitch Williams 已提交
3576
		bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
3577
	} else if (q_map) {
M
Mitch Williams 已提交
3578
		for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3579 3580 3581 3582 3583 3584 3585 3586 3587
			if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
				goto error_param;
			}

			/* Skip queue if not enabled */
			if (!test_bit(vf_q_id, vf->rxq_ena))
				continue;

3588 3589
			if (ice_vsi_ctrl_one_rx_ring(vsi, false, vf_q_id,
						     true)) {
3590
				dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Rx ring %d on VSI %d\n",
3591 3592 3593 3594 3595 3596 3597 3598
					vf_q_id, vsi->vsi_num);
				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
				goto error_param;
			}

			/* Clear enabled queues flag */
			clear_bit(vf_q_id, vf->rxq_ena);
		}
3599 3600 3601
	}

	/* Clear enabled queues flag */
3602
	if (v_ret == VIRTCHNL_STATUS_SUCCESS && ice_vf_has_no_qs_ena(vf))
3603
		clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
3604 3605 3606

error_param:
	/* send the response to the VF */
3607
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
3608 3609 3610
				     NULL, 0);
}

M
Mitch Williams 已提交
3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661
/**
 * ice_cfg_interrupt
 * @vf: pointer to the VF info
 * @vsi: the VSI being configured
 * @vector_id: vector ID
 * @map: vector map for mapping vectors to queues
 * @q_vector: structure for interrupt vector
 * configure the IRQ to queue map
 */
static int
ice_cfg_interrupt(struct ice_vf *vf, struct ice_vsi *vsi, u16 vector_id,
		  struct virtchnl_vector_map *map,
		  struct ice_q_vector *q_vector)
{
	u16 vsi_q_id, vsi_q_id_idx;
	unsigned long qmap;

	q_vector->num_ring_rx = 0;
	q_vector->num_ring_tx = 0;

	qmap = map->rxq_map;
	for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
		vsi_q_id = vsi_q_id_idx;

		if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
			return VIRTCHNL_STATUS_ERR_PARAM;

		q_vector->num_ring_rx++;
		q_vector->rx.itr_idx = map->rxitr_idx;
		vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
		ice_cfg_rxq_interrupt(vsi, vsi_q_id, vector_id,
				      q_vector->rx.itr_idx);
	}

	qmap = map->txq_map;
	for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
		vsi_q_id = vsi_q_id_idx;

		if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
			return VIRTCHNL_STATUS_ERR_PARAM;

		q_vector->num_ring_tx++;
		q_vector->tx.itr_idx = map->txitr_idx;
		vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
		ice_cfg_txq_interrupt(vsi, vsi_q_id, vector_id,
				      q_vector->tx.itr_idx);
	}

	return VIRTCHNL_STATUS_SUCCESS;
}

3662 3663 3664 3665 3666 3667 3668 3669 3670
/**
 * ice_vc_cfg_irq_map_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * called from the VF to configure the IRQ to queue map
 */
static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
{
3671
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
M
Mitch Williams 已提交
3672
	u16 num_q_vectors_mapped, vsi_id, vector_id;
3673
	struct virtchnl_irq_map_info *irqmap_info;
3674 3675
	struct virtchnl_vector_map *map;
	struct ice_pf *pf = vf->pf;
3676
	struct ice_vsi *vsi;
3677 3678
	int i;

3679
	irqmap_info = (struct virtchnl_irq_map_info *)msg;
3680 3681 3682 3683 3684 3685
	num_q_vectors_mapped = irqmap_info->num_vectors;

	/* Check to make sure number of VF vectors mapped is not greater than
	 * number of VF vectors originally allocated, and check that
	 * there is actually at least a single VF queue vector mapped
	 */
3686
	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
3687
	    pf->vfs.num_msix_per < num_q_vectors_mapped ||
M
Mitch Williams 已提交
3688
	    !num_q_vectors_mapped) {
3689
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3690 3691 3692
		goto error_param;
	}

3693
	vsi = ice_get_vf_vsi(vf);
3694 3695 3696 3697 3698
	if (!vsi) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto error_param;
	}

3699 3700
	for (i = 0; i < num_q_vectors_mapped; i++) {
		struct ice_q_vector *q_vector;
3701

3702 3703 3704 3705
		map = &irqmap_info->vecmap[i];

		vector_id = map->vector_id;
		vsi_id = map->vsi_id;
3706 3707 3708
		/* vector_id is always 0-based for each VF, and can never be
		 * larger than or equal to the max allowed interrupts per VF
		 */
3709
		if (!(vector_id < pf->vfs.num_msix_per) ||
3710
		    !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724
		    (!vector_id && (map->rxq_map || map->txq_map))) {
			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
			goto error_param;
		}

		/* No need to map VF miscellaneous or rogue vector */
		if (!vector_id)
			continue;

		/* Subtract non queue vector from vector_id passed by VF
		 * to get actual number of VSI queue vector array index
		 */
		q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
		if (!q_vector) {
3725
			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3726 3727 3728 3729
			goto error_param;
		}

		/* lookout for the invalid queue index */
M
Mitch Williams 已提交
3730 3731 3732 3733
		v_ret = (enum virtchnl_status_code)
			ice_cfg_interrupt(vf, vsi, vector_id, map, q_vector);
		if (v_ret)
			goto error_param;
3734 3735 3736 3737
	}

error_param:
	/* send the response to the VF */
3738
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750
				     NULL, 0);
}

/**
 * ice_vc_cfg_qs_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * called from the VF to configure the Rx/Tx queues
 */
static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
{
3751
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3752 3753 3754
	struct virtchnl_vsi_queue_config_info *qci =
	    (struct virtchnl_vsi_queue_config_info *)msg;
	struct virtchnl_queue_pair_info *qpi;
3755
	struct ice_pf *pf = vf->pf;
3756
	struct ice_vsi *vsi;
3757
	int i, q_idx;
3758 3759

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3760
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3761 3762 3763 3764
		goto error_param;
	}

	if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
3765
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3766 3767 3768
		goto error_param;
	}

3769
	vsi = ice_get_vf_vsi(vf);
3770
	if (!vsi) {
3771
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3772 3773 3774
		goto error_param;
	}

M
Mitch Williams 已提交
3775
	if (qci->num_queue_pairs > ICE_MAX_RSS_QS_PER_VF ||
3776
	    qci->num_queue_pairs > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
3777
		dev_err(ice_pf_to_dev(pf), "VF-%d requesting more than supported number of queues: %d\n",
3778
			vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
3779 3780 3781 3782
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto error_param;
	}

3783 3784 3785 3786 3787
	for (i = 0; i < qci->num_queue_pairs; i++) {
		qpi = &qci->qpair[i];
		if (qpi->txq.vsi_id != qci->vsi_id ||
		    qpi->rxq.vsi_id != qci->vsi_id ||
		    qpi->rxq.queue_id != qpi->txq.queue_id ||
3788
		    qpi->txq.headwb_enabled ||
3789 3790
		    !ice_vc_isvalid_ring_len(qpi->txq.ring_len) ||
		    !ice_vc_isvalid_ring_len(qpi->rxq.ring_len) ||
3791
		    !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
3792
			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3793 3794
			goto error_param;
		}
3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805

		q_idx = qpi->rxq.queue_id;

		/* make sure selected "q_idx" is in valid range of queues
		 * for selected "vsi"
		 */
		if (q_idx >= vsi->alloc_txq || q_idx >= vsi->alloc_rxq) {
			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
			goto error_param;
		}

3806
		/* copy Tx queue info from VF into VSI */
3807 3808 3809
		if (qpi->txq.ring_len > 0) {
			vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
			vsi->tx_rings[i]->count = qpi->txq.ring_len;
3810 3811 3812 3813
			if (ice_vsi_cfg_single_txq(vsi, vsi->tx_rings, q_idx)) {
				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
				goto error_param;
			}
3814
		}
3815 3816 3817

		/* copy Rx queue info from VF into VSI */
		if (qpi->rxq.ring_len > 0) {
3818 3819
			u16 max_frame_size = ice_vc_get_max_frame_size(vf);

3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830
			vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
			vsi->rx_rings[i]->count = qpi->rxq.ring_len;

			if (qpi->rxq.databuffer_size != 0 &&
			    (qpi->rxq.databuffer_size > ((16 * 1024) - 128) ||
			     qpi->rxq.databuffer_size < 1024)) {
				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
				goto error_param;
			}
			vsi->rx_buf_len = qpi->rxq.databuffer_size;
			vsi->rx_rings[i]->rx_buf_len = vsi->rx_buf_len;
3831
			if (qpi->rxq.max_pkt_size > max_frame_size ||
3832 3833 3834 3835 3836
			    qpi->rxq.max_pkt_size < 64) {
				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
				goto error_param;
			}

3837 3838 3839 3840
			vsi->max_frame = qpi->rxq.max_pkt_size;
			/* add space for the port VLAN since the VF driver is not
			 * expected to account for it in the MTU calculation
			 */
3841
			if (ice_vf_is_port_vlan_ena(vf))
3842
				vsi->max_frame += VLAN_HLEN;
3843

3844 3845 3846 3847 3848 3849
			if (ice_vsi_cfg_single_rxq(vsi, q_idx)) {
				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
				goto error_param;
			}
		}
	}
3850 3851 3852

error_param:
	/* send the response to the VF */
3853
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES, v_ret,
3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883
				     NULL, 0);
}

/**
 * ice_is_vf_trusted
 * @vf: pointer to the VF info
 */
static bool ice_is_vf_trusted(struct ice_vf *vf)
{
	return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
}

/**
 * ice_can_vf_change_mac
 * @vf: pointer to the VF info
 *
 * Return true if the VF is allowed to change its MAC filters, false otherwise
 */
static bool ice_can_vf_change_mac(struct ice_vf *vf)
{
	/* If the VF MAC address has been set administratively (via the
	 * ndo_set_vf_mac command), then deny permission to the VF to
	 * add/delete unicast MAC addresses, unless the VF is trusted
	 */
	if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
		return false;

	return true;
}

3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933
/**
 * ice_vc_ether_addr_type - get type of virtchnl_ether_addr
 * @vc_ether_addr: used to extract the type
 */
static u8
ice_vc_ether_addr_type(struct virtchnl_ether_addr *vc_ether_addr)
{
	return (vc_ether_addr->type & VIRTCHNL_ETHER_ADDR_TYPE_MASK);
}

/**
 * ice_is_vc_addr_legacy - check if the MAC address is from an older VF
 * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
 */
static bool
ice_is_vc_addr_legacy(struct virtchnl_ether_addr *vc_ether_addr)
{
	u8 type = ice_vc_ether_addr_type(vc_ether_addr);

	return (type == VIRTCHNL_ETHER_ADDR_LEGACY);
}

/**
 * ice_is_vc_addr_primary - check if the MAC address is the VF's primary MAC
 * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
 *
 * This function should only be called when the MAC address in
 * virtchnl_ether_addr is a valid unicast MAC
 */
static bool
ice_is_vc_addr_primary(struct virtchnl_ether_addr __maybe_unused *vc_ether_addr)
{
	u8 type = ice_vc_ether_addr_type(vc_ether_addr);

	return (type == VIRTCHNL_ETHER_ADDR_PRIMARY);
}

/**
 * ice_vfhw_mac_add - update the VF's cached hardware MAC if allowed
 * @vf: VF to update
 * @vc_ether_addr: structure from VIRTCHNL with MAC to add
 */
static void
ice_vfhw_mac_add(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
{
	u8 *mac_addr = vc_ether_addr->addr;

	if (!is_valid_ether_addr(mac_addr))
		return;

B
Brett Creeley 已提交
3934 3935 3936
	/* only allow legacy VF drivers to set the device and hardware MAC if it
	 * is zero and allow new VF drivers to set the hardware MAC if the type
	 * was correctly specified over VIRTCHNL
3937 3938 3939
	 */
	if ((ice_is_vc_addr_legacy(vc_ether_addr) &&
	     is_zero_ether_addr(vf->hw_lan_addr.addr)) ||
B
Brett Creeley 已提交
3940 3941
	    ice_is_vc_addr_primary(vc_ether_addr)) {
		ether_addr_copy(vf->dev_lan_addr.addr, mac_addr);
3942
		ether_addr_copy(vf->hw_lan_addr.addr, mac_addr);
B
Brett Creeley 已提交
3943
	}
3944

B
Brett Creeley 已提交
3945 3946
	/* hardware and device MACs are already set, but its possible that the
	 * VF driver sent the VIRTCHNL_OP_ADD_ETH_ADDR message before the
3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957
	 * VIRTCHNL_OP_DEL_ETH_ADDR when trying to update its MAC, so save it
	 * away for the legacy VF driver case as it will be updated in the
	 * delete flow for this case
	 */
	if (ice_is_vc_addr_legacy(vc_ether_addr)) {
		ether_addr_copy(vf->legacy_last_added_umac.addr,
				mac_addr);
		vf->legacy_last_added_umac.time_modified = jiffies;
	}
}

3958 3959 3960 3961
/**
 * ice_vc_add_mac_addr - attempt to add the MAC address passed in
 * @vf: pointer to the VF info
 * @vsi: pointer to the VF's VSI
3962
 * @vc_ether_addr: VIRTCHNL MAC address structure used to add MAC
3963 3964
 */
static int
3965 3966
ice_vc_add_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
		    struct virtchnl_ether_addr *vc_ether_addr)
3967 3968
{
	struct device *dev = ice_pf_to_dev(vf->pf);
3969
	u8 *mac_addr = vc_ether_addr->addr;
T
Tony Nguyen 已提交
3970
	int ret;
3971

B
Brett Creeley 已提交
3972 3973
	/* device MAC already added */
	if (ether_addr_equal(mac_addr, vf->dev_lan_addr.addr))
3974 3975 3976 3977 3978 3979 3980
		return 0;

	if (is_unicast_ether_addr(mac_addr) && !ice_can_vf_change_mac(vf)) {
		dev_err(dev, "VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
		return -EPERM;
	}

T
Tony Nguyen 已提交
3981 3982
	ret = ice_fltr_add_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
	if (ret == -EEXIST) {
3983
		dev_dbg(dev, "MAC %pM already exists for VF %d\n", mac_addr,
3984
			vf->vf_id);
3985 3986 3987
		/* don't return since we might need to update
		 * the primary MAC in ice_vfhw_mac_add() below
		 */
T
Tony Nguyen 已提交
3988
	} else if (ret) {
3989
		dev_err(dev, "Failed to add MAC %pM for VF %d\n, error %d\n",
T
Tony Nguyen 已提交
3990
			mac_addr, vf->vf_id, ret);
T
Tony Nguyen 已提交
3991
		return ret;
3992 3993
	} else {
		vf->num_mac++;
3994 3995
	}

3996
	ice_vfhw_mac_add(vf, vc_ether_addr);
3997

3998
	return ret;
3999 4000
}

4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011
/**
 * ice_is_legacy_umac_expired - check if last added legacy unicast MAC expired
 * @last_added_umac: structure used to check expiration
 */
static bool ice_is_legacy_umac_expired(struct ice_time_mac *last_added_umac)
{
#define ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME	msecs_to_jiffies(3000)
	return time_is_before_jiffies(last_added_umac->time_modified +
				      ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME);
}

4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031
/**
 * ice_update_legacy_cached_mac - update cached hardware MAC for legacy VF
 * @vf: VF to update
 * @vc_ether_addr: structure from VIRTCHNL with MAC to check
 *
 * only update cached hardware MAC for legacy VF drivers on delete
 * because we cannot guarantee order/type of MAC from the VF driver
 */
static void
ice_update_legacy_cached_mac(struct ice_vf *vf,
			     struct virtchnl_ether_addr *vc_ether_addr)
{
	if (!ice_is_vc_addr_legacy(vc_ether_addr) ||
	    ice_is_legacy_umac_expired(&vf->legacy_last_added_umac))
		return;

	ether_addr_copy(vf->dev_lan_addr.addr, vf->legacy_last_added_umac.addr);
	ether_addr_copy(vf->hw_lan_addr.addr, vf->legacy_last_added_umac.addr);
}

4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042
/**
 * ice_vfhw_mac_del - update the VF's cached hardware MAC if allowed
 * @vf: VF to update
 * @vc_ether_addr: structure from VIRTCHNL with MAC to delete
 */
static void
ice_vfhw_mac_del(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
{
	u8 *mac_addr = vc_ether_addr->addr;

	if (!is_valid_ether_addr(mac_addr) ||
B
Brett Creeley 已提交
4043
	    !ether_addr_equal(vf->dev_lan_addr.addr, mac_addr))
4044 4045
		return;

B
Brett Creeley 已提交
4046 4047 4048 4049 4050 4051
	/* allow the device MAC to be repopulated in the add flow and don't
	 * clear the hardware MAC (i.e. hw_lan_addr.addr) here as that is meant
	 * to be persistent on VM reboot and across driver unload/load, which
	 * won't work if we clear the hardware MAC here
	 */
	eth_zero_addr(vf->dev_lan_addr.addr);
4052

4053
	ice_update_legacy_cached_mac(vf, vc_ether_addr);
4054 4055
}

4056 4057 4058 4059
/**
 * ice_vc_del_mac_addr - attempt to delete the MAC address passed in
 * @vf: pointer to the VF info
 * @vsi: pointer to the VF's VSI
4060
 * @vc_ether_addr: VIRTCHNL MAC address structure used to delete MAC
4061 4062
 */
static int
4063 4064
ice_vc_del_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
		    struct virtchnl_ether_addr *vc_ether_addr)
4065 4066
{
	struct device *dev = ice_pf_to_dev(vf->pf);
4067
	u8 *mac_addr = vc_ether_addr->addr;
T
Tony Nguyen 已提交
4068
	int status;
4069 4070

	if (!ice_can_vf_change_mac(vf) &&
B
Brett Creeley 已提交
4071
	    ether_addr_equal(vf->dev_lan_addr.addr, mac_addr))
4072 4073
		return 0;

4074
	status = ice_fltr_remove_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
T
Tony Nguyen 已提交
4075
	if (status == -ENOENT) {
4076 4077 4078 4079
		dev_err(dev, "MAC %pM does not exist for VF %d\n", mac_addr,
			vf->vf_id);
		return -ENOENT;
	} else if (status) {
4080 4081
		dev_err(dev, "Failed to delete MAC %pM for VF %d, error %d\n",
			mac_addr, vf->vf_id, status);
4082 4083 4084
		return -EIO;
	}

4085
	ice_vfhw_mac_del(vf, vc_ether_addr);
4086 4087 4088 4089 4090 4091

	vf->num_mac--;

	return 0;
}

4092 4093 4094 4095
/**
 * ice_vc_handle_mac_addr_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
4096
 * @set: true if MAC filters are being set, false otherwise
4097
 *
4098
 * add guest MAC address filter
4099 4100 4101 4102
 */
static int
ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
{
4103
	int (*ice_vc_cfg_mac)
4104 4105
		(struct ice_vf *vf, struct ice_vsi *vsi,
		 struct virtchnl_ether_addr *virtchnl_ether_addr);
4106
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4107 4108 4109 4110 4111 4112 4113
	struct virtchnl_ether_addr_list *al =
	    (struct virtchnl_ether_addr_list *)msg;
	struct ice_pf *pf = vf->pf;
	enum virtchnl_ops vc_op;
	struct ice_vsi *vsi;
	int i;

4114
	if (set) {
4115
		vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
4116 4117
		ice_vc_cfg_mac = ice_vc_add_mac_addr;
	} else {
4118
		vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
4119 4120
		ice_vc_cfg_mac = ice_vc_del_mac_addr;
	}
4121 4122 4123

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
	    !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
4124
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4125 4126 4127
		goto handle_mac_exit;
	}

4128 4129 4130 4131
	/* If this VF is not privileged, then we can't add more than a
	 * limited number of addresses. Check to make sure that the
	 * additions do not push us over the limit.
	 */
4132 4133
	if (set && !ice_is_vf_trusted(vf) &&
	    (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
4134
		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",
4135
			vf->vf_id);
4136
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4137 4138 4139
		goto handle_mac_exit;
	}

4140
	vsi = ice_get_vf_vsi(vf);
4141
	if (!vsi) {
4142
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4143 4144
		goto handle_mac_exit;
	}
4145 4146

	for (i = 0; i < al->num_elements; i++) {
4147 4148
		u8 *mac_addr = al->list[i].addr;
		int result;
4149

4150 4151 4152
		if (is_broadcast_ether_addr(mac_addr) ||
		    is_zero_ether_addr(mac_addr))
			continue;
4153

4154
		result = ice_vc_cfg_mac(vf, vsi, &al->list[i]);
4155 4156 4157 4158
		if (result == -EEXIST || result == -ENOENT) {
			continue;
		} else if (result) {
			v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
4159 4160 4161 4162 4163 4164
			goto handle_mac_exit;
		}
	}

handle_mac_exit:
	/* send the response to the VF */
4165
	return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197
}

/**
 * ice_vc_add_mac_addr_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * add guest MAC address filter
 */
static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
{
	return ice_vc_handle_mac_addr_msg(vf, msg, true);
}

/**
 * ice_vc_del_mac_addr_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * remove guest MAC address filter
 */
static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
{
	return ice_vc_handle_mac_addr_msg(vf, msg, false);
}

/**
 * ice_vc_request_qs_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * VFs get a default number of queues but can use this message to request a
4198
 * different number. If the request is successful, PF will reset the VF and
4199
 * return 0. If unsuccessful, PF will send message informing VF of number of
4200
 * available queue pairs via virtchnl message response to VF.
4201 4202 4203
 */
static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
{
4204
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4205 4206
	struct virtchnl_vf_res_request *vfres =
		(struct virtchnl_vf_res_request *)msg;
4207
	u16 req_queues = vfres->num_queue_pairs;
4208
	struct ice_pf *pf = vf->pf;
4209 4210
	u16 max_allowed_vf_queues;
	u16 tx_rx_queue_left;
B
Brett Creeley 已提交
4211
	struct device *dev;
T
Tony Nguyen 已提交
4212
	u16 cur_queues;
4213

B
Brett Creeley 已提交
4214
	dev = ice_pf_to_dev(pf);
4215
	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4216
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4217 4218 4219
		goto error_param;
	}

4220
	cur_queues = vf->num_vf_qs;
4221 4222
	tx_rx_queue_left = min_t(u16, ice_get_avail_txq_count(pf),
				 ice_get_avail_rxq_count(pf));
4223
	max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
4224
	if (!req_queues) {
B
Brett Creeley 已提交
4225
		dev_err(dev, "VF %d tried to request 0 queues. Ignoring.\n",
4226
			vf->vf_id);
M
Mitch Williams 已提交
4227
	} else if (req_queues > ICE_MAX_RSS_QS_PER_VF) {
B
Brett Creeley 已提交
4228
		dev_err(dev, "VF %d tried to request more than %d queues.\n",
M
Mitch Williams 已提交
4229 4230
			vf->vf_id, ICE_MAX_RSS_QS_PER_VF);
		vfres->num_queue_pairs = ICE_MAX_RSS_QS_PER_VF;
4231 4232
	} else if (req_queues > cur_queues &&
		   req_queues - cur_queues > tx_rx_queue_left) {
4233
		dev_warn(dev, "VF %d requested %u more queues, but only %u left.\n",
4234
			 vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
4235
		vfres->num_queue_pairs = min_t(u16, max_allowed_vf_queues,
M
Mitch Williams 已提交
4236
					       ICE_MAX_RSS_QS_PER_VF);
4237 4238 4239
	} else {
		/* request is successful, then reset VF */
		vf->num_req_qs = req_queues;
4240
		ice_vc_reset_vf(vf);
B
Brett Creeley 已提交
4241
		dev_info(dev, "VF %d granted request of %u queues.\n",
4242 4243 4244 4245 4246 4247 4248
			 vf->vf_id, req_queues);
		return 0;
	}

error_param:
	/* send the response to the VF */
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
4249
				     v_ret, (u8 *)vfres, sizeof(*vfres));
4250 4251
}

4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262
/**
 * ice_vf_vlan_offload_ena - determine if capabilities support VLAN offloads
 * @caps: VF driver negotiated capabilities
 *
 * Return true if VIRTCHNL_VF_OFFLOAD_VLAN capability is set, else return false
 */
static bool ice_vf_vlan_offload_ena(u32 caps)
{
	return !!(caps & VIRTCHNL_VF_OFFLOAD_VLAN);
}

4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318
/**
 * ice_is_vlan_promisc_allowed - check if VLAN promiscuous config is allowed
 * @vf: VF used to determine if VLAN promiscuous config is allowed
 */
static bool ice_is_vlan_promisc_allowed(struct ice_vf *vf)
{
	if ((test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
	     test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) &&
	    test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, vf->pf->flags))
		return true;

	return false;
}

/**
 * ice_vf_ena_vlan_promisc - Enable Tx/Rx VLAN promiscuous for the VLAN
 * @vsi: VF's VSI used to enable VLAN promiscuous mode
 * @vlan: VLAN used to enable VLAN promiscuous
 *
 * This function should only be called if VLAN promiscuous mode is allowed,
 * which can be determined via ice_is_vlan_promisc_allowed().
 */
static int ice_vf_ena_vlan_promisc(struct ice_vsi *vsi, struct ice_vlan *vlan)
{
	u8 promisc_m = ICE_PROMISC_VLAN_TX | ICE_PROMISC_VLAN_RX;
	int status;

	status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m,
					  vlan->vid);
	if (status && status != -EEXIST)
		return status;

	return 0;
}

/**
 * ice_vf_dis_vlan_promisc - Disable Tx/Rx VLAN promiscuous for the VLAN
 * @vsi: VF's VSI used to disable VLAN promiscuous mode for
 * @vlan: VLAN used to disable VLAN promiscuous
 *
 * This function should only be called if VLAN promiscuous mode is allowed,
 * which can be determined via ice_is_vlan_promisc_allowed().
 */
static int ice_vf_dis_vlan_promisc(struct ice_vsi *vsi, struct ice_vlan *vlan)
{
	u8 promisc_m = ICE_PROMISC_VLAN_TX | ICE_PROMISC_VLAN_RX;
	int status;

	status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m,
					    vlan->vid);
	if (status && status != -ENOENT)
		return status;

	return 0;
}

4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339
/**
 * ice_vf_has_max_vlans - check if VF already has the max allowed VLAN filters
 * @vf: VF to check against
 * @vsi: VF's VSI
 *
 * If the VF is trusted then the VF is allowed to add as many VLANs as it
 * wants to, so return false.
 *
 * When the VF is untrusted compare the number of non-zero VLANs + 1 to the max
 * allowed VLANs for an untrusted VF. Return the result of this comparison.
 */
static bool ice_vf_has_max_vlans(struct ice_vf *vf, struct ice_vsi *vsi)
{
	if (ice_is_vf_trusted(vf))
		return false;

#define ICE_VF_ADDED_VLAN_ZERO_FLTRS	1
	return ((ice_vsi_num_non_zero_vlans(vsi) +
		ICE_VF_ADDED_VLAN_ZERO_FLTRS) >= ICE_MAX_VLAN_PER_VF);
}

4340 4341 4342 4343 4344 4345
/**
 * ice_vc_process_vlan_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 * @add_v: Add VLAN if true, otherwise delete VLAN
 *
4346
 * Process virtchnl op to add or remove programmed guest VLAN ID
4347 4348 4349
 */
static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
{
4350
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4351 4352 4353
	struct virtchnl_vlan_filter_list *vfl =
	    (struct virtchnl_vlan_filter_list *)msg;
	struct ice_pf *pf = vf->pf;
4354
	bool vlan_promisc = false;
4355
	struct ice_vsi *vsi;
B
Brett Creeley 已提交
4356
	struct device *dev;
4357
	int status = 0;
4358 4359
	int i;

B
Brett Creeley 已提交
4360
	dev = ice_pf_to_dev(pf);
4361
	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4362
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4363 4364 4365
		goto error_param;
	}

4366 4367 4368 4369 4370
	if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto error_param;
	}

4371
	if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
4372
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4373 4374 4375 4376
		goto error_param;
	}

	for (i = 0; i < vfl->num_elements; i++) {
B
Brett Creeley 已提交
4377
		if (vfl->vlan_id[i] >= VLAN_N_VID) {
4378
			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4379 4380
			dev_err(dev, "invalid VF VLAN id %d\n",
				vfl->vlan_id[i]);
4381 4382 4383 4384
			goto error_param;
		}
	}

4385
	vsi = ice_get_vf_vsi(vf);
4386
	if (!vsi) {
4387
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4388 4389 4390
		goto error_param;
	}

4391
	if (add_v && ice_vf_has_max_vlans(vf, vsi)) {
4392
		dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
B
Brett Creeley 已提交
4393 4394 4395 4396 4397 4398 4399
			 vf->vf_id);
		/* There is no need to let VF know about being not trusted,
		 * so we can just return success message here
		 */
		goto error_param;
	}

4400 4401 4402 4403
	/* in DVM a VF can add/delete inner VLAN filters when
	 * VIRTCHNL_VF_OFFLOAD_VLAN is negotiated, so only reject in SVM
	 */
	if (ice_vf_is_port_vlan_ena(vf) && !ice_is_dvm_ena(&pf->hw)) {
4404
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4405 4406 4407
		goto error_param;
	}

4408 4409 4410 4411 4412 4413 4414
	/* in DVM VLAN promiscuous is based on the outer VLAN, which would be
	 * the port VLAN if VIRTCHNL_VF_OFFLOAD_VLAN was negotiated, so only
	 * allow vlan_promisc = true in SVM and if no port VLAN is configured
	 */
	vlan_promisc = ice_is_vlan_promisc_allowed(vf) &&
		!ice_is_dvm_ena(&pf->hw) &&
		!ice_vf_is_port_vlan_ena(vf);
4415

4416 4417 4418
	if (add_v) {
		for (i = 0; i < vfl->num_elements; i++) {
			u16 vid = vfl->vlan_id[i];
B
Brett Creeley 已提交
4419
			struct ice_vlan vlan;
4420

4421
			if (ice_vf_has_max_vlans(vf, vsi)) {
4422
				dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
4423 4424 4425 4426 4427 4428 4429 4430
					 vf->vf_id);
				/* There is no need to let VF know about being
				 * not trusted, so we can just return success
				 * message here as well.
				 */
				goto error_param;
			}

B
Brett Creeley 已提交
4431 4432 4433 4434 4435 4436 4437
			/* we add VLAN 0 by default for each VF so we can enable
			 * Tx VLAN anti-spoof without triggering MDD events so
			 * we don't need to add it again here
			 */
			if (!vid)
				continue;

4438
			vlan = ICE_VLAN(ETH_P_8021Q, vid, 0);
4439
			status = vsi->inner_vlan_ops.add_vlan(vsi, &vlan);
B
Brett Creeley 已提交
4440
			if (status) {
4441
				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4442 4443
				goto error_param;
			}
4444

4445 4446 4447
			/* Enable VLAN filtering on first non-zero VLAN */
			if (!vlan_promisc && vid && !ice_is_dvm_ena(&pf->hw)) {
				if (vsi->inner_vlan_ops.ena_rx_filtering(vsi)) {
4448
					v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4449
					dev_err(dev, "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
4450 4451 4452
						vid, status);
					goto error_param;
				}
4453
			} else if (vlan_promisc) {
4454
				status = ice_vf_ena_vlan_promisc(vsi, &vlan);
4455 4456
				if (status) {
					v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4457
					dev_err(dev, "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
4458
						vid, status);
4459
				}
4460 4461 4462
			}
		}
	} else {
4463 4464 4465 4466 4467 4468 4469 4470 4471
		/* In case of non_trusted VF, number of VLAN elements passed
		 * to PF for removal might be greater than number of VLANs
		 * filter programmed for that VF - So, use actual number of
		 * VLANS added earlier with add VLAN opcode. In order to avoid
		 * removing VLAN that doesn't exist, which result to sending
		 * erroneous failed message back to the VF
		 */
		int num_vf_vlan;

B
Brett Creeley 已提交
4472
		num_vf_vlan = vsi->num_vlan;
4473
		for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
4474
			u16 vid = vfl->vlan_id[i];
B
Brett Creeley 已提交
4475
			struct ice_vlan vlan;
4476

B
Brett Creeley 已提交
4477 4478 4479 4480 4481 4482 4483
			/* we add VLAN 0 by default for each VF so we can enable
			 * Tx VLAN anti-spoof without triggering MDD events so
			 * we don't want a VIRTCHNL request to remove it
			 */
			if (!vid)
				continue;

4484
			vlan = ICE_VLAN(ETH_P_8021Q, vid, 0);
4485
			status = vsi->inner_vlan_ops.del_vlan(vsi, &vlan);
B
Brett Creeley 已提交
4486
			if (status) {
4487
				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4488 4489 4490
				goto error_param;
			}

4491 4492 4493
			/* Disable VLAN filtering when only VLAN 0 is left */
			if (!ice_vsi_has_non_zero_vlans(vsi))
				vsi->inner_vlan_ops.dis_rx_filtering(vsi);
4494

4495 4496
			if (vlan_promisc)
				ice_vf_dis_vlan_promisc(vsi, &vlan);
4497 4498 4499 4500 4501 4502
		}
	}

error_param:
	/* send the response to the VF */
	if (add_v)
4503
		return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
4504 4505
					     NULL, 0);
	else
4506
		return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
4507 4508 4509 4510 4511 4512 4513 4514
					     NULL, 0);
}

/**
 * ice_vc_add_vlan_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
4515
 * Add and program guest VLAN ID
4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526
 */
static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
{
	return ice_vc_process_vlan_msg(vf, msg, true);
}

/**
 * ice_vc_remove_vlan_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
4527
 * remove programmed guest VLAN ID
4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541
 */
static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
{
	return ice_vc_process_vlan_msg(vf, msg, false);
}

/**
 * ice_vc_ena_vlan_stripping
 * @vf: pointer to the VF info
 *
 * Enable VLAN header stripping for a given VF
 */
static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
{
4542
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4543 4544 4545
	struct ice_vsi *vsi;

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4546
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4547 4548 4549
		goto error_param;
	}

4550 4551 4552 4553 4554
	if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto error_param;
	}

4555
	vsi = ice_get_vf_vsi(vf);
4556
	if (vsi->inner_vlan_ops.ena_stripping(vsi, ETH_P_8021Q))
4557
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4558 4559 4560

error_param:
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
4561
				     v_ret, NULL, 0);
4562 4563 4564 4565 4566 4567 4568 4569 4570 4571
}

/**
 * ice_vc_dis_vlan_stripping
 * @vf: pointer to the VF info
 *
 * Disable VLAN header stripping for a given VF
 */
static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
{
4572
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4573 4574 4575
	struct ice_vsi *vsi;

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4576
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4577 4578 4579
		goto error_param;
	}

4580 4581 4582 4583 4584
	if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto error_param;
	}

4585
	vsi = ice_get_vf_vsi(vf);
4586
	if (!vsi) {
4587
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4588 4589 4590
		goto error_param;
	}

4591
	if (vsi->inner_vlan_ops.dis_stripping(vsi))
4592
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4593 4594 4595

error_param:
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
4596
				     v_ret, NULL, 0);
4597 4598
}

4599 4600 4601 4602
/**
 * ice_vf_init_vlan_stripping - enable/disable VLAN stripping on initialization
 * @vf: VF to enable/disable VLAN stripping for on initialization
 *
4603 4604
 * Set the default for VLAN stripping based on whether a port VLAN is configured
 * and the current VLAN mode of the device.
4605 4606 4607
 */
static int ice_vf_init_vlan_stripping(struct ice_vf *vf)
{
4608
	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
4609 4610 4611 4612

	if (!vsi)
		return -EINVAL;

4613 4614 4615 4616
	/* don't modify stripping if port VLAN is configured in SVM since the
	 * port VLAN is based on the inner/single VLAN in SVM
	 */
	if (ice_vf_is_port_vlan_ena(vf) && !ice_is_dvm_ena(&vsi->back->hw))
4617 4618 4619
		return 0;

	if (ice_vf_vlan_offload_ena(vf->driver_caps))
4620
		return vsi->inner_vlan_ops.ena_stripping(vsi, ETH_P_8021Q);
4621
	else
4622
		return vsi->inner_vlan_ops.dis_stripping(vsi);
4623 4624
}

4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573
static u16 ice_vc_get_max_vlan_fltrs(struct ice_vf *vf)
{
	if (vf->trusted)
		return VLAN_N_VID;
	else
		return ICE_MAX_VLAN_PER_VF;
}

/**
 * ice_vf_outer_vlan_not_allowed - check outer VLAN can be used when the device is in DVM
 * @vf: VF that being checked for
 */
static bool ice_vf_outer_vlan_not_allowed(struct ice_vf *vf)
{
	if (ice_vf_is_port_vlan_ena(vf))
		return true;

	return false;
}

/**
 * ice_vc_set_dvm_caps - set VLAN capabilities when the device is in DVM
 * @vf: VF that capabilities are being set for
 * @caps: VLAN capabilities to populate
 *
 * Determine VLAN capabilities support based on whether a port VLAN is
 * configured. If a port VLAN is configured then the VF should use the inner
 * filtering/offload capabilities since the port VLAN is using the outer VLAN
 * capabilies.
 */
static void
ice_vc_set_dvm_caps(struct ice_vf *vf, struct virtchnl_vlan_caps *caps)
{
	struct virtchnl_vlan_supported_caps *supported_caps;

	if (ice_vf_outer_vlan_not_allowed(vf)) {
		/* until support for inner VLAN filtering is added when a port
		 * VLAN is configured, only support software offloaded inner
		 * VLANs when a port VLAN is confgured in DVM
		 */
		supported_caps = &caps->filtering.filtering_support;
		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;

		supported_caps = &caps->offloads.stripping_support;
		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
					VIRTCHNL_VLAN_TOGGLE |
					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;

		supported_caps = &caps->offloads.insertion_support;
		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
					VIRTCHNL_VLAN_TOGGLE |
					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;

		caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
		caps->offloads.ethertype_match =
			VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
	} else {
		supported_caps = &caps->filtering.filtering_support;
		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
		supported_caps->outer = VIRTCHNL_VLAN_ETHERTYPE_8100 |
					VIRTCHNL_VLAN_ETHERTYPE_88A8 |
					VIRTCHNL_VLAN_ETHERTYPE_9100 |
					VIRTCHNL_VLAN_ETHERTYPE_AND;
		caps->filtering.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100 |
						 VIRTCHNL_VLAN_ETHERTYPE_88A8 |
						 VIRTCHNL_VLAN_ETHERTYPE_9100;

		supported_caps = &caps->offloads.stripping_support;
		supported_caps->inner = VIRTCHNL_VLAN_TOGGLE |
					VIRTCHNL_VLAN_ETHERTYPE_8100 |
					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
		supported_caps->outer = VIRTCHNL_VLAN_TOGGLE |
					VIRTCHNL_VLAN_ETHERTYPE_8100 |
					VIRTCHNL_VLAN_ETHERTYPE_88A8 |
					VIRTCHNL_VLAN_ETHERTYPE_9100 |
					VIRTCHNL_VLAN_ETHERTYPE_XOR |
					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2;

		supported_caps = &caps->offloads.insertion_support;
		supported_caps->inner = VIRTCHNL_VLAN_TOGGLE |
					VIRTCHNL_VLAN_ETHERTYPE_8100 |
					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
		supported_caps->outer = VIRTCHNL_VLAN_TOGGLE |
					VIRTCHNL_VLAN_ETHERTYPE_8100 |
					VIRTCHNL_VLAN_ETHERTYPE_88A8 |
					VIRTCHNL_VLAN_ETHERTYPE_9100 |
					VIRTCHNL_VLAN_ETHERTYPE_XOR |
					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2;

		caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;

		caps->offloads.ethertype_match =
			VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
	}

	caps->filtering.max_filters = ice_vc_get_max_vlan_fltrs(vf);
}

/**
 * ice_vc_set_svm_caps - set VLAN capabilities when the device is in SVM
 * @vf: VF that capabilities are being set for
 * @caps: VLAN capabilities to populate
 *
 * Determine VLAN capabilities support based on whether a port VLAN is
 * configured. If a port VLAN is configured then the VF does not have any VLAN
 * filtering or offload capabilities since the port VLAN is using the inner VLAN
 * capabilities in single VLAN mode (SVM). Otherwise allow the VF to use inner
 * VLAN fitlering and offload capabilities.
 */
static void
ice_vc_set_svm_caps(struct ice_vf *vf, struct virtchnl_vlan_caps *caps)
{
	struct virtchnl_vlan_supported_caps *supported_caps;

	if (ice_vf_is_port_vlan_ena(vf)) {
		supported_caps = &caps->filtering.filtering_support;
		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;

		supported_caps = &caps->offloads.stripping_support;
		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;

		supported_caps = &caps->offloads.insertion_support;
		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;

		caps->offloads.ethertype_init = VIRTCHNL_VLAN_UNSUPPORTED;
		caps->offloads.ethertype_match = VIRTCHNL_VLAN_UNSUPPORTED;
		caps->filtering.max_filters = 0;
	} else {
		supported_caps = &caps->filtering.filtering_support;
		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100;
		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
		caps->filtering.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;

		supported_caps = &caps->offloads.stripping_support;
		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
					VIRTCHNL_VLAN_TOGGLE |
					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;

		supported_caps = &caps->offloads.insertion_support;
		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
					VIRTCHNL_VLAN_TOGGLE |
					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;

		caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
		caps->offloads.ethertype_match =
			VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
		caps->filtering.max_filters = ice_vc_get_max_vlan_fltrs(vf);
	}
}

/**
 * ice_vc_get_offload_vlan_v2_caps - determine VF's VLAN capabilities
 * @vf: VF to determine VLAN capabilities for
 *
 * This will only be called if the VF and PF successfully negotiated
 * VIRTCHNL_VF_OFFLOAD_VLAN_V2.
 *
 * Set VLAN capabilities based on the current VLAN mode and whether a port VLAN
 * is configured or not.
 */
static int ice_vc_get_offload_vlan_v2_caps(struct ice_vf *vf)
{
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
	struct virtchnl_vlan_caps *caps = NULL;
	int err, len = 0;

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	caps = kzalloc(sizeof(*caps), GFP_KERNEL);
	if (!caps) {
		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
		goto out;
	}
	len = sizeof(*caps);

	if (ice_is_dvm_ena(&vf->pf->hw))
		ice_vc_set_dvm_caps(vf, caps);
	else
		ice_vc_set_svm_caps(vf, caps);

	/* store negotiated caps to prevent invalid VF messages */
	memcpy(&vf->vlan_v2_caps, caps, sizeof(*caps));

out:
	err = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS,
				    v_ret, (u8 *)caps, len);
	kfree(caps);
	return err;
}

/**
 * ice_vc_validate_vlan_tpid - validate VLAN TPID
 * @filtering_caps: negotiated/supported VLAN filtering capabilities
 * @tpid: VLAN TPID used for validation
 *
 * Convert the VLAN TPID to a VIRTCHNL_VLAN_ETHERTYPE_* and then compare against
 * the negotiated/supported filtering caps to see if the VLAN TPID is valid.
 */
static bool ice_vc_validate_vlan_tpid(u16 filtering_caps, u16 tpid)
{
	enum virtchnl_vlan_support vlan_ethertype = VIRTCHNL_VLAN_UNSUPPORTED;

	switch (tpid) {
	case ETH_P_8021Q:
		vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_8100;
		break;
	case ETH_P_8021AD:
		vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_88A8;
		break;
	case ETH_P_QINQ1:
		vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_9100;
		break;
	}

	if (!(filtering_caps & vlan_ethertype))
		return false;

	return true;
}

/**
 * ice_vc_is_valid_vlan - validate the virtchnl_vlan
 * @vc_vlan: virtchnl_vlan to validate
 *
 * If the VLAN TCI and VLAN TPID are 0, then this filter is invalid, so return
 * false. Otherwise return true.
 */
static bool ice_vc_is_valid_vlan(struct virtchnl_vlan *vc_vlan)
{
	if (!vc_vlan->tci || !vc_vlan->tpid)
		return false;

	return true;
}

/**
 * ice_vc_validate_vlan_filter_list - validate the filter list from the VF
 * @vfc: negotiated/supported VLAN filtering capabilities
 * @vfl: VLAN filter list from VF to validate
 *
 * Validate all of the filters in the VLAN filter list from the VF. If any of
 * the checks fail then return false. Otherwise return true.
 */
static bool
ice_vc_validate_vlan_filter_list(struct virtchnl_vlan_filtering_caps *vfc,
				 struct virtchnl_vlan_filter_list_v2 *vfl)
{
	u16 i;

	if (!vfl->num_elements)
		return false;

	for (i = 0; i < vfl->num_elements; i++) {
		struct virtchnl_vlan_supported_caps *filtering_support =
			&vfc->filtering_support;
		struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
		struct virtchnl_vlan *outer = &vlan_fltr->outer;
		struct virtchnl_vlan *inner = &vlan_fltr->inner;

		if ((ice_vc_is_valid_vlan(outer) &&
		     filtering_support->outer == VIRTCHNL_VLAN_UNSUPPORTED) ||
		    (ice_vc_is_valid_vlan(inner) &&
		     filtering_support->inner == VIRTCHNL_VLAN_UNSUPPORTED))
			return false;

		if ((outer->tci_mask &&
		     !(filtering_support->outer & VIRTCHNL_VLAN_FILTER_MASK)) ||
		    (inner->tci_mask &&
		     !(filtering_support->inner & VIRTCHNL_VLAN_FILTER_MASK)))
			return false;

		if (((outer->tci & VLAN_PRIO_MASK) &&
		     !(filtering_support->outer & VIRTCHNL_VLAN_PRIO)) ||
		    ((inner->tci & VLAN_PRIO_MASK) &&
		     !(filtering_support->inner & VIRTCHNL_VLAN_PRIO)))
			return false;

		if ((ice_vc_is_valid_vlan(outer) &&
		     !ice_vc_validate_vlan_tpid(filtering_support->outer, outer->tpid)) ||
		    (ice_vc_is_valid_vlan(inner) &&
		     !ice_vc_validate_vlan_tpid(filtering_support->inner, inner->tpid)))
			return false;
	}

	return true;
}

/**
 * ice_vc_to_vlan - transform from struct virtchnl_vlan to struct ice_vlan
 * @vc_vlan: struct virtchnl_vlan to transform
 */
static struct ice_vlan ice_vc_to_vlan(struct virtchnl_vlan *vc_vlan)
{
	struct ice_vlan vlan = { 0 };

	vlan.prio = (vc_vlan->tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
	vlan.vid = vc_vlan->tci & VLAN_VID_MASK;
	vlan.tpid = vc_vlan->tpid;

	return vlan;
}

/**
 * ice_vc_vlan_action - action to perform on the virthcnl_vlan
 * @vsi: VF's VSI used to perform the action
 * @vlan_action: function to perform the action with (i.e. add/del)
 * @vlan: VLAN filter to perform the action with
 */
static int
ice_vc_vlan_action(struct ice_vsi *vsi,
		   int (*vlan_action)(struct ice_vsi *, struct ice_vlan *),
		   struct ice_vlan *vlan)
{
	int err;

	err = vlan_action(vsi, vlan);
	if (err)
		return err;

	return 0;
}

/**
 * ice_vc_del_vlans - delete VLAN(s) from the virtchnl filter list
 * @vf: VF used to delete the VLAN(s)
 * @vsi: VF's VSI used to delete the VLAN(s)
 * @vfl: virthchnl filter list used to delete the filters
 */
static int
ice_vc_del_vlans(struct ice_vf *vf, struct ice_vsi *vsi,
		 struct virtchnl_vlan_filter_list_v2 *vfl)
{
	bool vlan_promisc = ice_is_vlan_promisc_allowed(vf);
	int err;
	u16 i;

	for (i = 0; i < vfl->num_elements; i++) {
		struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
		struct virtchnl_vlan *vc_vlan;

		vc_vlan = &vlan_fltr->outer;
		if (ice_vc_is_valid_vlan(vc_vlan)) {
			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);

			err = ice_vc_vlan_action(vsi,
						 vsi->outer_vlan_ops.del_vlan,
						 &vlan);
			if (err)
				return err;

			if (vlan_promisc)
				ice_vf_dis_vlan_promisc(vsi, &vlan);
		}

		vc_vlan = &vlan_fltr->inner;
		if (ice_vc_is_valid_vlan(vc_vlan)) {
			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);

			err = ice_vc_vlan_action(vsi,
						 vsi->inner_vlan_ops.del_vlan,
						 &vlan);
			if (err)
				return err;

			/* no support for VLAN promiscuous on inner VLAN unless
			 * we are in Single VLAN Mode (SVM)
			 */
			if (!ice_is_dvm_ena(&vsi->back->hw) && vlan_promisc)
				ice_vf_dis_vlan_promisc(vsi, &vlan);
		}
	}

	return 0;
}

/**
 * ice_vc_remove_vlan_v2_msg - virtchnl handler for VIRTCHNL_OP_DEL_VLAN_V2
 * @vf: VF the message was received from
 * @msg: message received from the VF
 */
static int ice_vc_remove_vlan_v2_msg(struct ice_vf *vf, u8 *msg)
{
	struct virtchnl_vlan_filter_list_v2 *vfl =
		(struct virtchnl_vlan_filter_list_v2 *)msg;
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
	struct ice_vsi *vsi;

	if (!ice_vc_validate_vlan_filter_list(&vf->vlan_v2_caps.filtering,
					      vfl)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	if (!ice_vc_isvalid_vsi_id(vf, vfl->vport_id)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	vsi = ice_get_vf_vsi(vf);
	if (!vsi) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	if (ice_vc_del_vlans(vf, vsi, vfl))
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;

out:
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN_V2, v_ret, NULL,
				     0);
}

/**
 * ice_vc_add_vlans - add VLAN(s) from the virtchnl filter list
 * @vf: VF used to add the VLAN(s)
 * @vsi: VF's VSI used to add the VLAN(s)
 * @vfl: virthchnl filter list used to add the filters
 */
static int
ice_vc_add_vlans(struct ice_vf *vf, struct ice_vsi *vsi,
		 struct virtchnl_vlan_filter_list_v2 *vfl)
{
	bool vlan_promisc = ice_is_vlan_promisc_allowed(vf);
	int err;
	u16 i;

	for (i = 0; i < vfl->num_elements; i++) {
		struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
		struct virtchnl_vlan *vc_vlan;

		vc_vlan = &vlan_fltr->outer;
		if (ice_vc_is_valid_vlan(vc_vlan)) {
			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);

			err = ice_vc_vlan_action(vsi,
						 vsi->outer_vlan_ops.add_vlan,
						 &vlan);
			if (err)
				return err;

			if (vlan_promisc) {
				err = ice_vf_ena_vlan_promisc(vsi, &vlan);
				if (err)
					return err;
			}
		}

		vc_vlan = &vlan_fltr->inner;
		if (ice_vc_is_valid_vlan(vc_vlan)) {
			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);

			err = ice_vc_vlan_action(vsi,
						 vsi->inner_vlan_ops.add_vlan,
						 &vlan);
			if (err)
				return err;

			/* no support for VLAN promiscuous on inner VLAN unless
			 * we are in Single VLAN Mode (SVM)
			 */
			if (!ice_is_dvm_ena(&vsi->back->hw) && vlan_promisc) {
				err = ice_vf_ena_vlan_promisc(vsi, &vlan);
				if (err)
					return err;
			}
		}
	}

	return 0;
}

/**
 * ice_vc_validate_add_vlan_filter_list - validate add filter list from the VF
 * @vsi: VF VSI used to get number of existing VLAN filters
 * @vfc: negotiated/supported VLAN filtering capabilities
 * @vfl: VLAN filter list from VF to validate
 *
 * Validate all of the filters in the VLAN filter list from the VF during the
 * VIRTCHNL_OP_ADD_VLAN_V2 opcode. If any of the checks fail then return false.
 * Otherwise return true.
 */
static bool
ice_vc_validate_add_vlan_filter_list(struct ice_vsi *vsi,
				     struct virtchnl_vlan_filtering_caps *vfc,
				     struct virtchnl_vlan_filter_list_v2 *vfl)
{
	u16 num_requested_filters = vsi->num_vlan + vfl->num_elements;

	if (num_requested_filters > vfc->max_filters)
		return false;

	return ice_vc_validate_vlan_filter_list(vfc, vfl);
}

/**
 * ice_vc_add_vlan_v2_msg - virtchnl handler for VIRTCHNL_OP_ADD_VLAN_V2
 * @vf: VF the message was received from
 * @msg: message received from the VF
 */
static int ice_vc_add_vlan_v2_msg(struct ice_vf *vf, u8 *msg)
{
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
	struct virtchnl_vlan_filter_list_v2 *vfl =
		(struct virtchnl_vlan_filter_list_v2 *)msg;
	struct ice_vsi *vsi;

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	if (!ice_vc_isvalid_vsi_id(vf, vfl->vport_id)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	vsi = ice_get_vf_vsi(vf);
	if (!vsi) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	if (!ice_vc_validate_add_vlan_filter_list(vsi,
						  &vf->vlan_v2_caps.filtering,
						  vfl)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	if (ice_vc_add_vlans(vf, vsi, vfl))
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;

out:
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN_V2, v_ret, NULL,
				     0);
}

/**
 * ice_vc_valid_vlan_setting - validate VLAN setting
 * @negotiated_settings: negotiated VLAN settings during VF init
 * @ethertype_setting: ethertype(s) requested for the VLAN setting
 */
static bool
ice_vc_valid_vlan_setting(u32 negotiated_settings, u32 ethertype_setting)
{
	if (ethertype_setting && !(negotiated_settings & ethertype_setting))
		return false;

	/* only allow a single VIRTCHNL_VLAN_ETHERTYPE if
	 * VIRTHCNL_VLAN_ETHERTYPE_AND is not negotiated/supported
	 */
	if (!(negotiated_settings & VIRTCHNL_VLAN_ETHERTYPE_AND) &&
	    hweight32(ethertype_setting) > 1)
		return false;

	/* ability to modify the VLAN setting was not negotiated */
	if (!(negotiated_settings & VIRTCHNL_VLAN_TOGGLE))
		return false;

	return true;
}

/**
 * ice_vc_valid_vlan_setting_msg - validate the VLAN setting message
 * @caps: negotiated VLAN settings during VF init
 * @msg: message to validate
 *
 * Used to validate any VLAN virtchnl message sent as a
 * virtchnl_vlan_setting structure. Validates the message against the
 * negotiated/supported caps during VF driver init.
 */
static bool
ice_vc_valid_vlan_setting_msg(struct virtchnl_vlan_supported_caps *caps,
			      struct virtchnl_vlan_setting *msg)
{
	if ((!msg->outer_ethertype_setting &&
	     !msg->inner_ethertype_setting) ||
	    (!caps->outer && !caps->inner))
		return false;

	if (msg->outer_ethertype_setting &&
	    !ice_vc_valid_vlan_setting(caps->outer,
				       msg->outer_ethertype_setting))
		return false;

	if (msg->inner_ethertype_setting &&
	    !ice_vc_valid_vlan_setting(caps->inner,
				       msg->inner_ethertype_setting))
		return false;

	return true;
}

/**
 * ice_vc_get_tpid - transform from VIRTCHNL_VLAN_ETHERTYPE_* to VLAN TPID
 * @ethertype_setting: VIRTCHNL_VLAN_ETHERTYPE_* used to get VLAN TPID
 * @tpid: VLAN TPID to populate
 */
static int ice_vc_get_tpid(u32 ethertype_setting, u16 *tpid)
{
	switch (ethertype_setting) {
	case VIRTCHNL_VLAN_ETHERTYPE_8100:
		*tpid = ETH_P_8021Q;
		break;
	case VIRTCHNL_VLAN_ETHERTYPE_88A8:
		*tpid = ETH_P_8021AD;
		break;
	case VIRTCHNL_VLAN_ETHERTYPE_9100:
		*tpid = ETH_P_QINQ1;
		break;
	default:
		*tpid = 0;
		return -EINVAL;
	}

	return 0;
}

/**
 * ice_vc_ena_vlan_offload - enable VLAN offload based on the ethertype_setting
 * @vsi: VF's VSI used to enable the VLAN offload
 * @ena_offload: function used to enable the VLAN offload
 * @ethertype_setting: VIRTCHNL_VLAN_ETHERTYPE_* to enable offloads for
 */
static int
ice_vc_ena_vlan_offload(struct ice_vsi *vsi,
			int (*ena_offload)(struct ice_vsi *vsi, u16 tpid),
			u32 ethertype_setting)
{
	u16 tpid;
	int err;

	err = ice_vc_get_tpid(ethertype_setting, &tpid);
	if (err)
		return err;

	err = ena_offload(vsi, tpid);
	if (err)
		return err;

	return 0;
}

#define ICE_L2TSEL_QRX_CONTEXT_REG_IDX	3
#define ICE_L2TSEL_BIT_OFFSET		23
enum ice_l2tsel {
	ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND,
	ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG1,
};

/**
 * ice_vsi_update_l2tsel - update l2tsel field for all Rx rings on this VSI
 * @vsi: VSI used to update l2tsel on
 * @l2tsel: l2tsel setting requested
 *
 * Use the l2tsel setting to update all of the Rx queue context bits for l2tsel.
 * This will modify which descriptor field the first offloaded VLAN will be
 * stripped into.
 */
static void ice_vsi_update_l2tsel(struct ice_vsi *vsi, enum ice_l2tsel l2tsel)
{
	struct ice_hw *hw = &vsi->back->hw;
	u32 l2tsel_bit;
	int i;

	if (l2tsel == ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND)
		l2tsel_bit = 0;
	else
		l2tsel_bit = BIT(ICE_L2TSEL_BIT_OFFSET);

	for (i = 0; i < vsi->alloc_rxq; i++) {
		u16 pfq = vsi->rxq_map[i];
		u32 qrx_context_offset;
		u32 regval;

		qrx_context_offset =
			QRX_CONTEXT(ICE_L2TSEL_QRX_CONTEXT_REG_IDX, pfq);

		regval = rd32(hw, qrx_context_offset);
		regval &= ~BIT(ICE_L2TSEL_BIT_OFFSET);
		regval |= l2tsel_bit;
		wr32(hw, qrx_context_offset, regval);
	}
}

/**
 * ice_vc_ena_vlan_stripping_v2_msg
 * @vf: VF the message was received from
 * @msg: message received from the VF
 *
 * virthcnl handler for VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2
 */
static int ice_vc_ena_vlan_stripping_v2_msg(struct ice_vf *vf, u8 *msg)
{
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
	struct virtchnl_vlan_supported_caps *stripping_support;
	struct virtchnl_vlan_setting *strip_msg =
		(struct virtchnl_vlan_setting *)msg;
	u32 ethertype_setting;
	struct ice_vsi *vsi;

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	if (!ice_vc_isvalid_vsi_id(vf, strip_msg->vport_id)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	vsi = ice_get_vf_vsi(vf);
	if (!vsi) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	stripping_support = &vf->vlan_v2_caps.offloads.stripping_support;
	if (!ice_vc_valid_vlan_setting_msg(stripping_support, strip_msg)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	ethertype_setting = strip_msg->outer_ethertype_setting;
	if (ethertype_setting) {
		if (ice_vc_ena_vlan_offload(vsi,
					    vsi->outer_vlan_ops.ena_stripping,
					    ethertype_setting)) {
			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
			goto out;
		} else {
			enum ice_l2tsel l2tsel =
				ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND;

			/* PF tells the VF that the outer VLAN tag is always
			 * extracted to VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2 and
			 * inner is always extracted to
			 * VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1. This is needed to
			 * support outer stripping so the first tag always ends
			 * up in L2TAG2_2ND and the second/inner tag, if
			 * enabled, is extracted in L2TAG1.
			 */
			ice_vsi_update_l2tsel(vsi, l2tsel);
		}
	}

	ethertype_setting = strip_msg->inner_ethertype_setting;
	if (ethertype_setting &&
	    ice_vc_ena_vlan_offload(vsi, vsi->inner_vlan_ops.ena_stripping,
				    ethertype_setting)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

out:
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2, v_ret, NULL, 0);
}

/**
 * ice_vc_dis_vlan_stripping_v2_msg
 * @vf: VF the message was received from
 * @msg: message received from the VF
 *
 * virthcnl handler for VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2
 */
static int ice_vc_dis_vlan_stripping_v2_msg(struct ice_vf *vf, u8 *msg)
{
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
	struct virtchnl_vlan_supported_caps *stripping_support;
	struct virtchnl_vlan_setting *strip_msg =
		(struct virtchnl_vlan_setting *)msg;
	u32 ethertype_setting;
	struct ice_vsi *vsi;

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	if (!ice_vc_isvalid_vsi_id(vf, strip_msg->vport_id)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	vsi = ice_get_vf_vsi(vf);
	if (!vsi) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	stripping_support = &vf->vlan_v2_caps.offloads.stripping_support;
	if (!ice_vc_valid_vlan_setting_msg(stripping_support, strip_msg)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	ethertype_setting = strip_msg->outer_ethertype_setting;
	if (ethertype_setting) {
		if (vsi->outer_vlan_ops.dis_stripping(vsi)) {
			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
			goto out;
		} else {
			enum ice_l2tsel l2tsel =
				ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG1;

			/* PF tells the VF that the outer VLAN tag is always
			 * extracted to VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2 and
			 * inner is always extracted to
			 * VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1. This is needed to
			 * support inner stripping while outer stripping is
			 * disabled so that the first and only tag is extracted
			 * in L2TAG1.
			 */
			ice_vsi_update_l2tsel(vsi, l2tsel);
		}
	}

	ethertype_setting = strip_msg->inner_ethertype_setting;
	if (ethertype_setting && vsi->inner_vlan_ops.dis_stripping(vsi)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

out:
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2, v_ret, NULL, 0);
}

/**
 * ice_vc_ena_vlan_insertion_v2_msg
 * @vf: VF the message was received from
 * @msg: message received from the VF
 *
 * virthcnl handler for VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2
 */
static int ice_vc_ena_vlan_insertion_v2_msg(struct ice_vf *vf, u8 *msg)
{
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
	struct virtchnl_vlan_supported_caps *insertion_support;
	struct virtchnl_vlan_setting *insertion_msg =
		(struct virtchnl_vlan_setting *)msg;
	u32 ethertype_setting;
	struct ice_vsi *vsi;

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	if (!ice_vc_isvalid_vsi_id(vf, insertion_msg->vport_id)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	vsi = ice_get_vf_vsi(vf);
	if (!vsi) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	insertion_support = &vf->vlan_v2_caps.offloads.insertion_support;
	if (!ice_vc_valid_vlan_setting_msg(insertion_support, insertion_msg)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	ethertype_setting = insertion_msg->outer_ethertype_setting;
	if (ethertype_setting &&
	    ice_vc_ena_vlan_offload(vsi, vsi->outer_vlan_ops.ena_insertion,
				    ethertype_setting)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	ethertype_setting = insertion_msg->inner_ethertype_setting;
	if (ethertype_setting &&
	    ice_vc_ena_vlan_offload(vsi, vsi->inner_vlan_ops.ena_insertion,
				    ethertype_setting)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

out:
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2, v_ret, NULL, 0);
}

/**
 * ice_vc_dis_vlan_insertion_v2_msg
 * @vf: VF the message was received from
 * @msg: message received from the VF
 *
 * virthcnl handler for VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2
 */
static int ice_vc_dis_vlan_insertion_v2_msg(struct ice_vf *vf, u8 *msg)
{
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
	struct virtchnl_vlan_supported_caps *insertion_support;
	struct virtchnl_vlan_setting *insertion_msg =
		(struct virtchnl_vlan_setting *)msg;
	u32 ethertype_setting;
	struct ice_vsi *vsi;

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	if (!ice_vc_isvalid_vsi_id(vf, insertion_msg->vport_id)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	vsi = ice_get_vf_vsi(vf);
	if (!vsi) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	insertion_support = &vf->vlan_v2_caps.offloads.insertion_support;
	if (!ice_vc_valid_vlan_setting_msg(insertion_support, insertion_msg)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	ethertype_setting = insertion_msg->outer_ethertype_setting;
	if (ethertype_setting && vsi->outer_vlan_ops.dis_insertion(vsi)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

	ethertype_setting = insertion_msg->inner_ethertype_setting;
	if (ethertype_setting && vsi->inner_vlan_ops.dis_insertion(vsi)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto out;
	}

out:
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2, v_ret, NULL, 0);
}

5574
static const struct ice_virtchnl_ops ice_virtchnl_dflt_ops = {
5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595
	.get_ver_msg = ice_vc_get_ver_msg,
	.get_vf_res_msg = ice_vc_get_vf_res_msg,
	.reset_vf = ice_vc_reset_vf_msg,
	.add_mac_addr_msg = ice_vc_add_mac_addr_msg,
	.del_mac_addr_msg = ice_vc_del_mac_addr_msg,
	.cfg_qs_msg = ice_vc_cfg_qs_msg,
	.ena_qs_msg = ice_vc_ena_qs_msg,
	.dis_qs_msg = ice_vc_dis_qs_msg,
	.request_qs_msg = ice_vc_request_qs_msg,
	.cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
	.config_rss_key = ice_vc_config_rss_key,
	.config_rss_lut = ice_vc_config_rss_lut,
	.get_stats_msg = ice_vc_get_stats_msg,
	.cfg_promiscuous_mode_msg = ice_vc_cfg_promiscuous_mode_msg,
	.add_vlan_msg = ice_vc_add_vlan_msg,
	.remove_vlan_msg = ice_vc_remove_vlan_msg,
	.ena_vlan_stripping = ice_vc_ena_vlan_stripping,
	.dis_vlan_stripping = ice_vc_dis_vlan_stripping,
	.handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
	.add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
	.del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
5596 5597 5598 5599 5600 5601 5602
	.get_offload_vlan_v2_caps = ice_vc_get_offload_vlan_v2_caps,
	.add_vlan_v2_msg = ice_vc_add_vlan_v2_msg,
	.remove_vlan_v2_msg = ice_vc_remove_vlan_v2_msg,
	.ena_vlan_stripping_v2_msg = ice_vc_ena_vlan_stripping_v2_msg,
	.dis_vlan_stripping_v2_msg = ice_vc_dis_vlan_stripping_v2_msg,
	.ena_vlan_insertion_v2_msg = ice_vc_ena_vlan_insertion_v2_msg,
	.dis_vlan_insertion_v2_msg = ice_vc_dis_vlan_insertion_v2_msg,
5603 5604
};

5605 5606 5607 5608 5609
/**
 * ice_virtchnl_set_dflt_ops - Switch to default virtchnl ops
 * @vf: the VF to switch ops
 */
void ice_virtchnl_set_dflt_ops(struct ice_vf *vf)
5610
{
5611
	vf->virtchnl_ops = &ice_virtchnl_dflt_ops;
5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647
}

/**
 * ice_vc_repr_add_mac
 * @vf: pointer to VF
 * @msg: virtchannel message
 *
 * When port representors are created, we do not add MAC rule
 * to firmware, we store it so that PF could report same
 * MAC as VF.
 */
static int ice_vc_repr_add_mac(struct ice_vf *vf, u8 *msg)
{
	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
	struct virtchnl_ether_addr_list *al =
	    (struct virtchnl_ether_addr_list *)msg;
	struct ice_vsi *vsi;
	struct ice_pf *pf;
	int i;

	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
	    !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto handle_mac_exit;
	}

	pf = vf->pf;

	vsi = ice_get_vf_vsi(vf);
	if (!vsi) {
		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
		goto handle_mac_exit;
	}

	for (i = 0; i < al->num_elements; i++) {
		u8 *mac_addr = al->list[i].addr;
5648
		int result;
5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659

		if (!is_unicast_ether_addr(mac_addr) ||
		    ether_addr_equal(mac_addr, vf->hw_lan_addr.addr))
			continue;

		if (vf->pf_set_mac) {
			dev_err(ice_pf_to_dev(pf), "VF attempting to override administratively set MAC address\n");
			v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
			goto handle_mac_exit;
		}

5660 5661 5662 5663 5664 5665 5666
		result = ice_eswitch_add_vf_mac_rule(pf, vf, mac_addr);
		if (result) {
			dev_err(ice_pf_to_dev(pf), "Failed to add MAC %pM for VF %d\n, error %d\n",
				mac_addr, vf->vf_id, result);
			goto handle_mac_exit;
		}

5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696
		ice_vfhw_mac_add(vf, &al->list[i]);
		vf->num_mac++;
		break;
	}

handle_mac_exit:
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
				     v_ret, NULL, 0);
}

/**
 * ice_vc_repr_del_mac - response with success for deleting MAC
 * @vf: pointer to VF
 * @msg: virtchannel message
 *
 * Respond with success to not break normal VF flow.
 * For legacy VF driver try to update cached MAC address.
 */
static int
ice_vc_repr_del_mac(struct ice_vf __always_unused *vf, u8 __always_unused *msg)
{
	struct virtchnl_ether_addr_list *al =
		(struct virtchnl_ether_addr_list *)msg;

	ice_update_legacy_cached_mac(vf, &al->list[0]);

	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
				     VIRTCHNL_STATUS_SUCCESS, NULL, 0);
}

5697
static int ice_vc_repr_add_vlan(struct ice_vf *vf, u8 __always_unused *msg)
5698
{
5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741
	dev_dbg(ice_pf_to_dev(vf->pf),
		"Can't add VLAN in switchdev mode for VF %d\n", vf->vf_id);
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN,
				     VIRTCHNL_STATUS_SUCCESS, NULL, 0);
}

static int ice_vc_repr_del_vlan(struct ice_vf *vf, u8 __always_unused *msg)
{
	dev_dbg(ice_pf_to_dev(vf->pf),
		"Can't delete VLAN in switchdev mode for VF %d\n", vf->vf_id);
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN,
				     VIRTCHNL_STATUS_SUCCESS, NULL, 0);
}

static int ice_vc_repr_ena_vlan_stripping(struct ice_vf *vf)
{
	dev_dbg(ice_pf_to_dev(vf->pf),
		"Can't enable VLAN stripping in switchdev mode for VF %d\n",
		vf->vf_id);
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
				     VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
				     NULL, 0);
}

static int ice_vc_repr_dis_vlan_stripping(struct ice_vf *vf)
{
	dev_dbg(ice_pf_to_dev(vf->pf),
		"Can't disable VLAN stripping in switchdev mode for VF %d\n",
		vf->vf_id);
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
				     VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
				     NULL, 0);
}

static int
ice_vc_repr_cfg_promiscuous_mode(struct ice_vf *vf, u8 __always_unused *msg)
{
	dev_dbg(ice_pf_to_dev(vf->pf),
		"Can't config promiscuous mode in switchdev mode for VF %d\n",
		vf->vf_id);
	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
				     VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
				     NULL, 0);
5742 5743
}

5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779
static const struct ice_virtchnl_ops ice_virtchnl_repr_ops = {
	.get_ver_msg = ice_vc_get_ver_msg,
	.get_vf_res_msg = ice_vc_get_vf_res_msg,
	.reset_vf = ice_vc_reset_vf_msg,
	.add_mac_addr_msg = ice_vc_repr_add_mac,
	.del_mac_addr_msg = ice_vc_repr_del_mac,
	.cfg_qs_msg = ice_vc_cfg_qs_msg,
	.ena_qs_msg = ice_vc_ena_qs_msg,
	.dis_qs_msg = ice_vc_dis_qs_msg,
	.request_qs_msg = ice_vc_request_qs_msg,
	.cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
	.config_rss_key = ice_vc_config_rss_key,
	.config_rss_lut = ice_vc_config_rss_lut,
	.get_stats_msg = ice_vc_get_stats_msg,
	.cfg_promiscuous_mode_msg = ice_vc_repr_cfg_promiscuous_mode,
	.add_vlan_msg = ice_vc_repr_add_vlan,
	.remove_vlan_msg = ice_vc_repr_del_vlan,
	.ena_vlan_stripping = ice_vc_repr_ena_vlan_stripping,
	.dis_vlan_stripping = ice_vc_repr_dis_vlan_stripping,
	.handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
	.add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
	.del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
	.get_offload_vlan_v2_caps = ice_vc_get_offload_vlan_v2_caps,
	.add_vlan_v2_msg = ice_vc_add_vlan_v2_msg,
	.remove_vlan_v2_msg = ice_vc_remove_vlan_v2_msg,
	.ena_vlan_stripping_v2_msg = ice_vc_ena_vlan_stripping_v2_msg,
	.dis_vlan_stripping_v2_msg = ice_vc_dis_vlan_stripping_v2_msg,
	.ena_vlan_insertion_v2_msg = ice_vc_ena_vlan_insertion_v2_msg,
	.dis_vlan_insertion_v2_msg = ice_vc_dis_vlan_insertion_v2_msg,
};

/**
 * ice_virtchnl_set_repr_ops - Switch to representor virtchnl ops
 * @vf: the VF to switch ops
 */
void ice_virtchnl_set_repr_ops(struct ice_vf *vf)
5780
{
5781
	vf->virtchnl_ops = &ice_virtchnl_repr_ops;
5782 5783
}

5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795
/**
 * ice_vc_process_vf_msg - Process request from VF
 * @pf: pointer to the PF structure
 * @event: pointer to the AQ event
 *
 * called from the common asq/arq handler to
 * process request from VF
 */
void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
{
	u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
	s16 vf_id = le16_to_cpu(event->desc.retval);
5796
	const struct ice_virtchnl_ops *ops;
5797 5798 5799
	u16 msglen = event->msg_len;
	u8 *msg = event->msg_buf;
	struct ice_vf *vf = NULL;
B
Brett Creeley 已提交
5800
	struct device *dev;
5801 5802
	int err = 0;

B
Brett Creeley 已提交
5803
	dev = ice_pf_to_dev(pf);
5804 5805 5806

	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf) {
5807 5808 5809
		dev_err(dev, "Unable to locate VF for message from VF ID %d, opcode %d, len %d\n",
			vf_id, v_opcode, msglen);
		return;
5810 5811 5812 5813 5814 5815 5816 5817
	}

	/* Check if VF is disabled. */
	if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
		err = -EPERM;
		goto error_handler;
	}

5818
	ops = vf->virtchnl_ops;
5819

5820 5821 5822
	/* Perform basic checks on the msg */
	err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
	if (err) {
5823
		if (err == VIRTCHNL_STATUS_ERR_PARAM)
5824 5825 5826 5827 5828
			err = -EPERM;
		else
			err = -EINVAL;
	}

5829 5830 5831 5832
	if (!ice_vc_is_opcode_allowed(vf, v_opcode)) {
		ice_vc_send_msg_to_vf(vf, v_opcode,
				      VIRTCHNL_STATUS_ERR_NOT_SUPPORTED, NULL,
				      0);
5833
		ice_put_vf(vf);
5834 5835 5836
		return;
	}

5837 5838
error_handler:
	if (err) {
5839 5840
		ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
				      NULL, 0);
B
Brett Creeley 已提交
5841
		dev_err(dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
5842
			vf_id, v_opcode, msglen, err);
5843
		ice_put_vf(vf);
5844 5845 5846
		return;
	}

5847 5848 5849 5850 5851 5852
	/* VF is being configured in another context that triggers a VFR, so no
	 * need to process this message
	 */
	if (!mutex_trylock(&vf->cfg_lock)) {
		dev_info(dev, "VF %u is being configured in another context that will trigger a VFR, so there is no need to handle this message\n",
			 vf->vf_id);
5853
		ice_put_vf(vf);
5854 5855 5856
		return;
	}

5857 5858
	switch (v_opcode) {
	case VIRTCHNL_OP_VERSION:
5859
		err = ops->get_ver_msg(vf, msg);
5860 5861
		break;
	case VIRTCHNL_OP_GET_VF_RESOURCES:
5862
		err = ops->get_vf_res_msg(vf, msg);
5863
		if (ice_vf_init_vlan_stripping(vf))
5864
			dev_dbg(dev, "Failed to initialize VLAN stripping for VF %d\n",
5865
				vf->vf_id);
5866
		ice_vc_notify_vf_link_state(vf);
5867 5868
		break;
	case VIRTCHNL_OP_RESET_VF:
5869
		ops->reset_vf(vf);
5870 5871
		break;
	case VIRTCHNL_OP_ADD_ETH_ADDR:
5872
		err = ops->add_mac_addr_msg(vf, msg);
5873 5874
		break;
	case VIRTCHNL_OP_DEL_ETH_ADDR:
5875
		err = ops->del_mac_addr_msg(vf, msg);
5876 5877
		break;
	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
5878
		err = ops->cfg_qs_msg(vf, msg);
5879 5880
		break;
	case VIRTCHNL_OP_ENABLE_QUEUES:
5881
		err = ops->ena_qs_msg(vf, msg);
5882 5883 5884
		ice_vc_notify_vf_link_state(vf);
		break;
	case VIRTCHNL_OP_DISABLE_QUEUES:
5885
		err = ops->dis_qs_msg(vf, msg);
5886 5887
		break;
	case VIRTCHNL_OP_REQUEST_QUEUES:
5888
		err = ops->request_qs_msg(vf, msg);
5889 5890
		break;
	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
5891
		err = ops->cfg_irq_map_msg(vf, msg);
5892 5893
		break;
	case VIRTCHNL_OP_CONFIG_RSS_KEY:
5894
		err = ops->config_rss_key(vf, msg);
5895 5896
		break;
	case VIRTCHNL_OP_CONFIG_RSS_LUT:
5897
		err = ops->config_rss_lut(vf, msg);
5898 5899
		break;
	case VIRTCHNL_OP_GET_STATS:
5900
		err = ops->get_stats_msg(vf, msg);
5901
		break;
B
Brett Creeley 已提交
5902
	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
5903
		err = ops->cfg_promiscuous_mode_msg(vf, msg);
B
Brett Creeley 已提交
5904
		break;
5905
	case VIRTCHNL_OP_ADD_VLAN:
5906
		err = ops->add_vlan_msg(vf, msg);
5907 5908
		break;
	case VIRTCHNL_OP_DEL_VLAN:
5909
		err = ops->remove_vlan_msg(vf, msg);
5910 5911
		break;
	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
5912
		err = ops->ena_vlan_stripping(vf);
5913 5914
		break;
	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
5915
		err = ops->dis_vlan_stripping(vf);
5916
		break;
Q
Qi Zhang 已提交
5917
	case VIRTCHNL_OP_ADD_FDIR_FILTER:
5918
		err = ops->add_fdir_fltr_msg(vf, msg);
Q
Qi Zhang 已提交
5919 5920
		break;
	case VIRTCHNL_OP_DEL_FDIR_FILTER:
5921
		err = ops->del_fdir_fltr_msg(vf, msg);
Q
Qi Zhang 已提交
5922
		break;
Q
Qi Zhang 已提交
5923
	case VIRTCHNL_OP_ADD_RSS_CFG:
5924
		err = ops->handle_rss_cfg_msg(vf, msg, true);
Q
Qi Zhang 已提交
5925 5926
		break;
	case VIRTCHNL_OP_DEL_RSS_CFG:
5927
		err = ops->handle_rss_cfg_msg(vf, msg, false);
Q
Qi Zhang 已提交
5928
		break;
5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949
	case VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS:
		err = ops->get_offload_vlan_v2_caps(vf);
		break;
	case VIRTCHNL_OP_ADD_VLAN_V2:
		err = ops->add_vlan_v2_msg(vf, msg);
		break;
	case VIRTCHNL_OP_DEL_VLAN_V2:
		err = ops->remove_vlan_v2_msg(vf, msg);
		break;
	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2:
		err = ops->ena_vlan_stripping_v2_msg(vf, msg);
		break;
	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2:
		err = ops->dis_vlan_stripping_v2_msg(vf, msg);
		break;
	case VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2:
		err = ops->ena_vlan_insertion_v2_msg(vf, msg);
		break;
	case VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2:
		err = ops->dis_vlan_insertion_v2_msg(vf, msg);
		break;
5950 5951
	case VIRTCHNL_OP_UNKNOWN:
	default:
B
Brett Creeley 已提交
5952 5953
		dev_err(dev, "Unsupported opcode %d from VF %d\n", v_opcode,
			vf_id);
5954 5955
		err = ice_vc_send_msg_to_vf(vf, v_opcode,
					    VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
5956 5957 5958 5959 5960 5961 5962
					    NULL, 0);
		break;
	}
	if (err) {
		/* Helper function cares less about error return values here
		 * as it is busy with pending work.
		 */
B
Brett Creeley 已提交
5963
		dev_info(dev, "PF failed to honor VF %d, opcode %d, error %d\n",
5964 5965
			 vf_id, v_opcode, err);
	}
5966 5967

	mutex_unlock(&vf->cfg_lock);
5968
	ice_put_vf(vf);
5969 5970
}

5971 5972 5973 5974 5975 5976 5977 5978
/**
 * ice_get_vf_cfg
 * @netdev: network interface device structure
 * @vf_id: VF identifier
 * @ivi: VF configuration structure
 *
 * return VF configuration
 */
5979 5980
int
ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
5981
{
J
Jesse Brandeburg 已提交
5982
	struct ice_pf *pf = ice_netdev_to_pf(netdev);
5983
	struct ice_vf *vf;
5984
	int ret;
5985

5986 5987
	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
5988 5989
		return -EINVAL;

5990 5991
	ret = ice_check_vf_ready_for_cfg(vf);
	if (ret)
5992
		goto out_put_vf;
5993 5994

	ivi->vf = vf_id;
5995
	ether_addr_copy(ivi->mac, vf->hw_lan_addr.addr);
5996 5997

	/* VF configuration for VLAN and applicable QoS */
5998 5999
	ivi->vlan = ice_vf_get_port_vlan_id(vf);
	ivi->qos = ice_vf_get_port_vlan_prio(vf);
6000 6001
	if (ice_vf_is_port_vlan_ena(vf))
		ivi->vlan_proto = cpu_to_be16(ice_vf_get_port_vlan_tpid(vf));
6002 6003 6004 6005 6006 6007 6008 6009 6010

	ivi->trusted = vf->trusted;
	ivi->spoofchk = vf->spoofchk;
	if (!vf->link_forced)
		ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
	else if (vf->link_up)
		ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
	else
		ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
6011 6012
	ivi->max_tx_rate = vf->max_tx_rate;
	ivi->min_tx_rate = vf->min_tx_rate;
6013 6014 6015 6016

out_put_vf:
	ice_put_vf(vf);
	return ret;
6017 6018
}

6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051
/**
 * ice_unicast_mac_exists - check if the unicast MAC exists on the PF's switch
 * @pf: PF used to reference the switch's rules
 * @umac: unicast MAC to compare against existing switch rules
 *
 * Return true on the first/any match, else return false
 */
static bool ice_unicast_mac_exists(struct ice_pf *pf, u8 *umac)
{
	struct ice_sw_recipe *mac_recipe_list =
		&pf->hw.switch_info->recp_list[ICE_SW_LKUP_MAC];
	struct ice_fltr_mgmt_list_entry *list_itr;
	struct list_head *rule_head;
	struct mutex *rule_lock; /* protect MAC filter list access */

	rule_head = &mac_recipe_list->filt_rules;
	rule_lock = &mac_recipe_list->filt_rule_lock;

	mutex_lock(rule_lock);
	list_for_each_entry(list_itr, rule_head, list_entry) {
		u8 *existing_mac = &list_itr->fltr_info.l_data.mac.mac_addr[0];

		if (ether_addr_equal(existing_mac, umac)) {
			mutex_unlock(rule_lock);
			return true;
		}
	}

	mutex_unlock(rule_lock);

	return false;
}

6052 6053 6054 6055
/**
 * ice_set_vf_mac
 * @netdev: network interface device structure
 * @vf_id: VF identifier
6056
 * @mac: MAC address
6057
 *
6058
 * program VF MAC address
6059 6060 6061
 */
int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
{
J
Jesse Brandeburg 已提交
6062
	struct ice_pf *pf = ice_netdev_to_pf(netdev);
6063
	struct ice_vf *vf;
6064
	int ret;
6065

6066
	if (is_multicast_ether_addr(mac)) {
6067 6068 6069 6070
		netdev_err(netdev, "%pM not a valid unicast address\n", mac);
		return -EINVAL;
	}

6071 6072 6073 6074
	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
		return -EINVAL;

6075
	/* nothing left to do, unicast MAC already set */
B
Brett Creeley 已提交
6076
	if (ether_addr_equal(vf->dev_lan_addr.addr, mac) &&
6077 6078 6079 6080
	    ether_addr_equal(vf->hw_lan_addr.addr, mac)) {
		ret = 0;
		goto out_put_vf;
	}
6081

6082 6083
	ret = ice_check_vf_ready_for_cfg(vf);
	if (ret)
6084
		goto out_put_vf;
6085

6086 6087 6088
	if (ice_unicast_mac_exists(pf, mac)) {
		netdev_err(netdev, "Unicast MAC %pM already exists on this PF. Preventing setting VF %u unicast MAC address to %pM\n",
			   mac, vf_id, mac);
6089 6090
		ret = -EINVAL;
		goto out_put_vf;
6091 6092
	}

6093 6094
	mutex_lock(&vf->cfg_lock);

6095 6096
	/* VF is notified of its new MAC via the PF's response to the
	 * VIRTCHNL_OP_GET_VF_RESOURCES message after the VF has been reset
6097
	 */
B
Brett Creeley 已提交
6098
	ether_addr_copy(vf->dev_lan_addr.addr, mac);
6099
	ether_addr_copy(vf->hw_lan_addr.addr, mac);
6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110
	if (is_zero_ether_addr(mac)) {
		/* VF will send VIRTCHNL_OP_ADD_ETH_ADDR message with its MAC */
		vf->pf_set_mac = false;
		netdev_info(netdev, "Removing MAC on VF %d. VF driver will be reinitialized\n",
			    vf->vf_id);
	} else {
		/* PF will add MAC rule for the VF */
		vf->pf_set_mac = true;
		netdev_info(netdev, "Setting MAC %pM on VF %d. VF driver will be reinitialized\n",
			    mac, vf_id);
	}
6111

6112
	ice_vc_reset_vf(vf);
6113
	mutex_unlock(&vf->cfg_lock);
6114 6115 6116 6117

out_put_vf:
	ice_put_vf(vf);
	return ret;
6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129
}

/**
 * ice_set_vf_trust
 * @netdev: network interface device structure
 * @vf_id: VF identifier
 * @trusted: Boolean value to enable/disable trusted VF
 *
 * Enable or disable a given VF as trusted
 */
int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
{
J
Jesse Brandeburg 已提交
6130
	struct ice_pf *pf = ice_netdev_to_pf(netdev);
6131
	struct ice_vf *vf;
6132
	int ret;
6133

6134 6135 6136 6137 6138
	if (ice_is_eswitch_mode_switchdev(pf)) {
		dev_info(ice_pf_to_dev(pf), "Trusted VF is forbidden in switchdev mode\n");
		return -EOPNOTSUPP;
	}

6139 6140
	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
6141 6142
		return -EINVAL;

6143 6144
	ret = ice_check_vf_ready_for_cfg(vf);
	if (ret)
6145
		goto out_put_vf;
6146 6147

	/* Check if already trusted */
6148 6149 6150 6151
	if (trusted == vf->trusted) {
		ret = 0;
		goto out_put_vf;
	}
6152

6153 6154
	mutex_lock(&vf->cfg_lock);

6155
	vf->trusted = trusted;
6156
	ice_vc_reset_vf(vf);
6157
	dev_info(ice_pf_to_dev(pf), "VF %u is now %strusted\n",
6158 6159
		 vf_id, trusted ? "" : "un");

6160 6161
	mutex_unlock(&vf->cfg_lock);

6162 6163 6164
out_put_vf:
	ice_put_vf(vf);
	return ret;
6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176
}

/**
 * ice_set_vf_link_state
 * @netdev: network interface device structure
 * @vf_id: VF identifier
 * @link_state: required link state
 *
 * Set VF's link state, irrespective of physical link state status
 */
int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
{
J
Jesse Brandeburg 已提交
6177
	struct ice_pf *pf = ice_netdev_to_pf(netdev);
6178
	struct ice_vf *vf;
6179
	int ret;
6180

6181 6182
	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
6183 6184
		return -EINVAL;

6185 6186
	ret = ice_check_vf_ready_for_cfg(vf);
	if (ret)
6187
		goto out_put_vf;
6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201

	switch (link_state) {
	case IFLA_VF_LINK_STATE_AUTO:
		vf->link_forced = false;
		break;
	case IFLA_VF_LINK_STATE_ENABLE:
		vf->link_forced = true;
		vf->link_up = true;
		break;
	case IFLA_VF_LINK_STATE_DISABLE:
		vf->link_forced = true;
		vf->link_up = false;
		break;
	default:
6202 6203
		ret = -EINVAL;
		goto out_put_vf;
6204 6205
	}

6206
	ice_vc_notify_vf_link_state(vf);
6207

6208 6209 6210
out_put_vf:
	ice_put_vf(vf);
	return ret;
6211
}
J
Jesse Brandeburg 已提交
6212

6213 6214 6215 6216 6217 6218
/**
 * ice_calc_all_vfs_min_tx_rate - calculate cumulative min Tx rate on all VFs
 * @pf: PF associated with VFs
 */
static int ice_calc_all_vfs_min_tx_rate(struct ice_pf *pf)
{
6219 6220 6221
	struct ice_vf *vf;
	unsigned int bkt;
	int rate = 0;
6222

6223 6224
	rcu_read_lock();
	ice_for_each_vf_rcu(pf, bkt, vf)
6225
		rate += vf->min_tx_rate;
6226
	rcu_read_unlock();
6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280

	return rate;
}

/**
 * ice_min_tx_rate_oversubscribed - check if min Tx rate causes oversubscription
 * @vf: VF trying to configure min_tx_rate
 * @min_tx_rate: min Tx rate in Mbps
 *
 * Check if the min_tx_rate being passed in will cause oversubscription of total
 * min_tx_rate based on the current link speed and all other VFs configured
 * min_tx_rate
 *
 * Return true if the passed min_tx_rate would cause oversubscription, else
 * return false
 */
static bool
ice_min_tx_rate_oversubscribed(struct ice_vf *vf, int min_tx_rate)
{
	int link_speed_mbps = ice_get_link_speed_mbps(ice_get_vf_vsi(vf));
	int all_vfs_min_tx_rate = ice_calc_all_vfs_min_tx_rate(vf->pf);

	/* this VF's previous rate is being overwritten */
	all_vfs_min_tx_rate -= vf->min_tx_rate;

	if (all_vfs_min_tx_rate + min_tx_rate > link_speed_mbps) {
		dev_err(ice_pf_to_dev(vf->pf), "min_tx_rate of %d Mbps on VF %u would cause oversubscription of %d Mbps based on the current link speed %d Mbps\n",
			min_tx_rate, vf->vf_id,
			all_vfs_min_tx_rate + min_tx_rate - link_speed_mbps,
			link_speed_mbps);
		return true;
	}

	return false;
}

/**
 * ice_set_vf_bw - set min/max VF bandwidth
 * @netdev: network interface device structure
 * @vf_id: VF identifier
 * @min_tx_rate: Minimum Tx rate in Mbps
 * @max_tx_rate: Maximum Tx rate in Mbps
 */
int
ice_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
	      int max_tx_rate)
{
	struct ice_pf *pf = ice_netdev_to_pf(netdev);
	struct ice_vsi *vsi;
	struct device *dev;
	struct ice_vf *vf;
	int ret;

	dev = ice_pf_to_dev(pf);
6281 6282 6283

	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
6284 6285 6286 6287
		return -EINVAL;

	ret = ice_check_vf_ready_for_cfg(vf);
	if (ret)
6288
		goto out_put_vf;
6289 6290 6291 6292 6293 6294 6295 6296 6297

	vsi = ice_get_vf_vsi(vf);

	/* when max_tx_rate is zero that means no max Tx rate limiting, so only
	 * check if max_tx_rate is non-zero
	 */
	if (max_tx_rate && min_tx_rate > max_tx_rate) {
		dev_err(dev, "Cannot set min Tx rate %d Mbps greater than max Tx rate %d Mbps\n",
			min_tx_rate, max_tx_rate);
6298 6299
		ret = -EINVAL;
		goto out_put_vf;
6300 6301 6302 6303
	}

	if (min_tx_rate && ice_is_dcb_active(pf)) {
		dev_err(dev, "DCB on PF is currently enabled. VF min Tx rate limiting not allowed on this PF.\n");
6304 6305
		ret = -EOPNOTSUPP;
		goto out_put_vf;
6306 6307
	}

6308 6309 6310 6311
	if (ice_min_tx_rate_oversubscribed(vf, min_tx_rate)) {
		ret = -EINVAL;
		goto out_put_vf;
	}
6312 6313 6314 6315 6316 6317

	if (vf->min_tx_rate != (unsigned int)min_tx_rate) {
		ret = ice_set_min_bw_limit(vsi, (u64)min_tx_rate * 1000);
		if (ret) {
			dev_err(dev, "Unable to set min-tx-rate for VF %d\n",
				vf->vf_id);
6318
			goto out_put_vf;
6319 6320 6321 6322 6323 6324 6325 6326 6327 6328
		}

		vf->min_tx_rate = min_tx_rate;
	}

	if (vf->max_tx_rate != (unsigned int)max_tx_rate) {
		ret = ice_set_max_bw_limit(vsi, (u64)max_tx_rate * 1000);
		if (ret) {
			dev_err(dev, "Unable to set max-tx-rate for VF %d\n",
				vf->vf_id);
6329
			goto out_put_vf;
6330 6331 6332 6333 6334
		}

		vf->max_tx_rate = max_tx_rate;
	}

6335 6336 6337
out_put_vf:
	ice_put_vf(vf);
	return ret;
6338 6339
}

J
Jesse Brandeburg 已提交
6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352
/**
 * ice_get_vf_stats - populate some stats for the VF
 * @netdev: the netdev of the PF
 * @vf_id: the host OS identifier (0-255)
 * @vf_stats: pointer to the OS memory to be initialized
 */
int ice_get_vf_stats(struct net_device *netdev, int vf_id,
		     struct ifla_vf_stats *vf_stats)
{
	struct ice_pf *pf = ice_netdev_to_pf(netdev);
	struct ice_eth_stats *stats;
	struct ice_vsi *vsi;
	struct ice_vf *vf;
6353
	int ret;
J
Jesse Brandeburg 已提交
6354

6355 6356
	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
J
Jesse Brandeburg 已提交
6357 6358
		return -EINVAL;

6359 6360
	ret = ice_check_vf_ready_for_cfg(vf);
	if (ret)
6361
		goto out_put_vf;
J
Jesse Brandeburg 已提交
6362

6363
	vsi = ice_get_vf_vsi(vf);
6364 6365 6366 6367
	if (!vsi) {
		ret = -EINVAL;
		goto out_put_vf;
	}
J
Jesse Brandeburg 已提交
6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384

	ice_update_eth_stats(vsi);
	stats = &vsi->eth_stats;

	memset(vf_stats, 0, sizeof(*vf_stats));

	vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
		stats->rx_multicast;
	vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
		stats->tx_multicast;
	vf_stats->rx_bytes   = stats->rx_bytes;
	vf_stats->tx_bytes   = stats->tx_bytes;
	vf_stats->broadcast  = stats->rx_broadcast;
	vf_stats->multicast  = stats->rx_multicast;
	vf_stats->rx_dropped = stats->rx_discards;
	vf_stats->tx_dropped = stats->tx_discards;

6385 6386 6387
out_put_vf:
	ice_put_vf(vf);
	return ret;
J
Jesse Brandeburg 已提交
6388
}
6389

6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485
/**
 * ice_is_supported_port_vlan_proto - make sure the vlan_proto is supported
 * @hw: hardware structure used to check the VLAN mode
 * @vlan_proto: VLAN TPID being checked
 *
 * If the device is configured in Double VLAN Mode (DVM), then both ETH_P_8021Q
 * and ETH_P_8021AD are supported. If the device is configured in Single VLAN
 * Mode (SVM), then only ETH_P_8021Q is supported.
 */
static bool
ice_is_supported_port_vlan_proto(struct ice_hw *hw, u16 vlan_proto)
{
	bool is_supported = false;

	switch (vlan_proto) {
	case ETH_P_8021Q:
		is_supported = true;
		break;
	case ETH_P_8021AD:
		if (ice_is_dvm_ena(hw))
			is_supported = true;
		break;
	}

	return is_supported;
}

/**
 * ice_set_vf_port_vlan
 * @netdev: network interface device structure
 * @vf_id: VF identifier
 * @vlan_id: VLAN ID being set
 * @qos: priority setting
 * @vlan_proto: VLAN protocol
 *
 * program VF Port VLAN ID and/or QoS
 */
int
ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
		     __be16 vlan_proto)
{
	struct ice_pf *pf = ice_netdev_to_pf(netdev);
	u16 local_vlan_proto = ntohs(vlan_proto);
	struct device *dev;
	struct ice_vf *vf;
	int ret;

	dev = ice_pf_to_dev(pf);

	if (vlan_id >= VLAN_N_VID || qos > 7) {
		dev_err(dev, "Invalid Port VLAN parameters for VF %d, ID %d, QoS %d\n",
			vf_id, vlan_id, qos);
		return -EINVAL;
	}

	if (!ice_is_supported_port_vlan_proto(&pf->hw, local_vlan_proto)) {
		dev_err(dev, "VF VLAN protocol 0x%04x is not supported\n",
			local_vlan_proto);
		return -EPROTONOSUPPORT;
	}

	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
		return -EINVAL;

	ret = ice_check_vf_ready_for_cfg(vf);
	if (ret)
		goto out_put_vf;

	if (ice_vf_get_port_vlan_prio(vf) == qos &&
	    ice_vf_get_port_vlan_tpid(vf) == local_vlan_proto &&
	    ice_vf_get_port_vlan_id(vf) == vlan_id) {
		/* duplicate request, so just return success */
		dev_dbg(dev, "Duplicate port VLAN %u, QoS %u, TPID 0x%04x request\n",
			vlan_id, qos, local_vlan_proto);
		ret = 0;
		goto out_put_vf;
	}

	mutex_lock(&vf->cfg_lock);

	vf->port_vlan_info = ICE_VLAN(local_vlan_proto, vlan_id, qos);
	if (ice_vf_is_port_vlan_ena(vf))
		dev_info(dev, "Setting VLAN %u, QoS %u, TPID 0x%04x on VF %d\n",
			 vlan_id, qos, local_vlan_proto, vf_id);
	else
		dev_info(dev, "Clearing port VLAN on VF %d\n", vf_id);

	ice_vc_reset_vf(vf);
	mutex_unlock(&vf->cfg_lock);

out_put_vf:
	ice_put_vf(vf);
	return ret;
}

6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498
/**
 * ice_print_vf_rx_mdd_event - print VF Rx malicious driver detect event
 * @vf: pointer to the VF structure
 */
void ice_print_vf_rx_mdd_event(struct ice_vf *vf)
{
	struct ice_pf *pf = vf->pf;
	struct device *dev;

	dev = ice_pf_to_dev(pf);

	dev_info(dev, "%d Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pM. mdd-auto-reset-vfs=%s\n",
		 vf->mdd_rx_events.count, pf->hw.pf_id, vf->vf_id,
B
Brett Creeley 已提交
6499
		 vf->dev_lan_addr.addr,
6500 6501 6502 6503
		 test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)
			  ? "on" : "off");
}

6504
/**
T
Tony Nguyen 已提交
6505
 * ice_print_vfs_mdd_events - print VFs malicious driver detect event
6506 6507 6508 6509 6510 6511 6512 6513
 * @pf: pointer to the PF structure
 *
 * Called from ice_handle_mdd_event to rate limit and print VFs MDD events.
 */
void ice_print_vfs_mdd_events(struct ice_pf *pf)
{
	struct device *dev = ice_pf_to_dev(pf);
	struct ice_hw *hw = &pf->hw;
6514 6515
	struct ice_vf *vf;
	unsigned int bkt;
6516 6517

	/* check that there are pending MDD events to print */
6518
	if (!test_and_clear_bit(ICE_MDD_VF_PRINT_PENDING, pf->state))
6519 6520 6521
		return;

	/* VF MDD event logs are rate limited to one second intervals */
6522
	if (time_is_after_jiffies(pf->vfs.last_printed_mdd_jiffies + HZ * 1))
6523 6524
		return;

6525
	pf->vfs.last_printed_mdd_jiffies = jiffies;
6526

6527
	mutex_lock(&pf->vfs.table_lock);
6528
	ice_for_each_vf(pf, bkt, vf) {
6529 6530 6531 6532
		/* only print Rx MDD event message if there are new events */
		if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
			vf->mdd_rx_events.last_printed =
							vf->mdd_rx_events.count;
6533
			ice_print_vf_rx_mdd_event(vf);
6534 6535 6536 6537 6538 6539 6540 6541
		}

		/* only print Tx MDD event message if there are new events */
		if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
			vf->mdd_tx_events.last_printed =
							vf->mdd_tx_events.count;

			dev_info(dev, "%d Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pM.\n",
6542
				 vf->mdd_tx_events.count, hw->pf_id, vf->vf_id,
B
Brett Creeley 已提交
6543
				 vf->dev_lan_addr.addr);
6544 6545
		}
	}
6546
	mutex_unlock(&pf->vfs.table_lock);
6547
}
6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565

/**
 * ice_restore_all_vfs_msi_state - restore VF MSI state after PF FLR
 * @pdev: pointer to a pci_dev structure
 *
 * Called when recovering from a PF FLR to restore interrupt capability to
 * the VFs.
 */
void ice_restore_all_vfs_msi_state(struct pci_dev *pdev)
{
	u16 vf_id;
	int pos;

	if (!pci_num_vf(pdev))
		return;

	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
	if (pos) {
6566 6567
		struct pci_dev *vfdev;

6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578
		pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID,
				     &vf_id);
		vfdev = pci_get_device(pdev->vendor, vf_id, NULL);
		while (vfdev) {
			if (vfdev->is_virtfn && vfdev->physfn == pdev)
				pci_restore_msi_state(vfdev);
			vfdev = pci_get_device(pdev->vendor, vf_id,
					       vfdev);
		}
	}
}
6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595

/**
 * ice_is_malicious_vf - helper function to detect a malicious VF
 * @pf: ptr to struct ice_pf
 * @event: pointer to the AQ event
 * @num_msg_proc: the number of messages processed so far
 * @num_msg_pending: the number of messages peinding in admin queue
 */
bool
ice_is_malicious_vf(struct ice_pf *pf, struct ice_rq_event_info *event,
		    u16 num_msg_proc, u16 num_msg_pending)
{
	s16 vf_id = le16_to_cpu(event->desc.retval);
	struct device *dev = ice_pf_to_dev(pf);
	struct ice_mbx_data mbxdata;
	bool malvf = false;
	struct ice_vf *vf;
6596
	int status;
6597

6598 6599
	vf = ice_get_vf_by_id(pf, vf_id);
	if (!vf)
6600 6601 6602
		return false;

	if (test_bit(ICE_VF_STATE_DIS, vf->vf_states))
6603
		goto out_put_vf;
6604 6605 6606 6607 6608 6609 6610 6611 6612 6613

	mbxdata.num_msg_proc = num_msg_proc;
	mbxdata.num_pending_arq = num_msg_pending;
	mbxdata.max_num_msgs_mbx = pf->hw.mailboxq.num_rq_entries;
#define ICE_MBX_OVERFLOW_WATERMARK 64
	mbxdata.async_watermark_val = ICE_MBX_OVERFLOW_WATERMARK;

	/* check to see if we have a malicious VF */
	status = ice_mbx_vf_state_handler(&pf->hw, &mbxdata, vf_id, &malvf);
	if (status)
6614
		goto out_put_vf;
6615 6616 6617 6618 6619 6620 6621

	if (malvf) {
		bool report_vf = false;

		/* if the VF is malicious and we haven't let the user
		 * know about it, then let them know now
		 */
6622
		status = ice_mbx_report_malvf(&pf->hw, pf->vfs.malvfs,
6623
					      ICE_MAX_SRIOV_VFS, vf_id,
6624 6625 6626 6627 6628 6629 6630 6631 6632
					      &report_vf);
		if (status)
			dev_dbg(dev, "Error reporting malicious VF\n");

		if (report_vf) {
			struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);

			if (pf_vsi)
				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",
B
Brett Creeley 已提交
6633
					 &vf->dev_lan_addr.addr[0],
6634 6635 6636 6637
					 pf_vsi->netdev->dev_addr);
		}
	}

6638 6639 6640
out_put_vf:
	ice_put_vf(vf);
	return malvf;
6641
}