ice: Recognize higher speeds
[linux-2.6-block.git] / drivers / net / ethernet / intel / ice / ice_virtchnl_pf.c
CommitLineData
ddf30f7f
AV
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2018, Intel Corporation. */
3
4#include "ice.h"
5#include "ice_lib.h"
6
cf6c6e01
MW
7/**
8 * ice_err_to_virt err - translate errors for VF return code
9 * @ice_err: error return code
10 */
11static enum virtchnl_status_code ice_err_to_virt_err(enum ice_status ice_err)
12{
13 switch (ice_err) {
14 case ICE_SUCCESS:
15 return VIRTCHNL_STATUS_SUCCESS;
16 case ICE_ERR_BAD_PTR:
17 case ICE_ERR_INVAL_SIZE:
18 case ICE_ERR_DEVICE_NOT_SUPPORTED:
19 case ICE_ERR_PARAM:
20 case ICE_ERR_CFG:
21 return VIRTCHNL_STATUS_ERR_PARAM;
22 case ICE_ERR_NO_MEMORY:
23 return VIRTCHNL_STATUS_ERR_NO_MEMORY;
24 case ICE_ERR_NOT_READY:
25 case ICE_ERR_RESET_FAILED:
26 case ICE_ERR_FW_API_VER:
27 case ICE_ERR_AQ_ERROR:
28 case ICE_ERR_AQ_TIMEOUT:
29 case ICE_ERR_AQ_FULL:
30 case ICE_ERR_AQ_NO_WORK:
31 case ICE_ERR_AQ_EMPTY:
32 return VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
33 default:
34 return VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
35 }
36}
37
007676b4
AV
38/**
39 * ice_vc_vf_broadcast - Broadcast a message to all VFs on PF
40 * @pf: pointer to the PF structure
41 * @v_opcode: operation code
42 * @v_retval: return value
43 * @msg: pointer to the msg buffer
44 * @msglen: msg length
45 */
46static void
47ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
cf6c6e01 48 enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
007676b4
AV
49{
50 struct ice_hw *hw = &pf->hw;
51 struct ice_vf *vf = pf->vf;
52 int i;
53
54 for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
55 /* Not all vfs are enabled so skip the ones that are not */
56 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
57 !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
58 continue;
59
60 /* Ignore return value on purpose - a given VF may fail, but
61 * we need to keep going and send to all of them
62 */
63 ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
64 msglen, NULL);
65 }
66}
67
7c710869
AV
68/**
69 * ice_set_pfe_link - Set the link speed/status of the virtchnl_pf_event
70 * @vf: pointer to the VF structure
71 * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
72 * @ice_link_speed: link speed specified by ICE_AQ_LINK_SPEED_*
73 * @link_up: whether or not to set the link up/down
74 */
75static void
76ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
77 int ice_link_speed, bool link_up)
78{
79 if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
80 pfe->event_data.link_event_adv.link_status = link_up;
81 /* Speed in Mbps */
82 pfe->event_data.link_event_adv.link_speed =
83 ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
84 } else {
85 pfe->event_data.link_event.link_status = link_up;
86 /* Legacy method for virtchnl link speeds */
87 pfe->event_data.link_event.link_speed =
88 (enum virtchnl_link_speed)
89 ice_conv_link_speed_to_virtchnl(false, ice_link_speed);
90 }
91}
92
93/**
94 * ice_set_pfe_link_forced - Force the virtchnl_pf_event link speed/status
95 * @vf: pointer to the VF structure
96 * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
97 * @link_up: whether or not to set the link up/down
98 */
99static void
100ice_set_pfe_link_forced(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
101 bool link_up)
102{
103 u16 link_speed;
104
105 if (link_up)
072efdf8 106 link_speed = ICE_AQ_LINK_SPEED_100GB;
7c710869
AV
107 else
108 link_speed = ICE_AQ_LINK_SPEED_UNKNOWN;
109
110 ice_set_pfe_link(vf, pfe, link_speed, link_up);
111}
112
1071a835
AV
113/**
114 * ice_vc_notify_vf_link_state - Inform a VF of link status
115 * @vf: pointer to the VF structure
116 *
117 * send a link status message to a single VF
118 */
119static void ice_vc_notify_vf_link_state(struct ice_vf *vf)
120{
121 struct virtchnl_pf_event pfe = { 0 };
122 struct ice_link_status *ls;
123 struct ice_pf *pf = vf->pf;
124 struct ice_hw *hw;
125
126 hw = &pf->hw;
127 ls = &hw->port_info->phy.link_info;
128
129 pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
130 pfe.severity = PF_EVENT_SEVERITY_INFO;
131
132 if (vf->link_forced)
133 ice_set_pfe_link_forced(vf, &pfe, vf->link_up);
134 else
135 ice_set_pfe_link(vf, &pfe, ls->link_speed, ls->link_info &
136 ICE_AQ_LINK_UP);
137
cf6c6e01
MW
138 ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
139 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,
1071a835
AV
140 sizeof(pfe), NULL);
141}
142
ddf30f7f
AV
143/**
144 * ice_free_vf_res - Free a VF's resources
145 * @vf: pointer to the VF info
146 */
147static void ice_free_vf_res(struct ice_vf *vf)
148{
149 struct ice_pf *pf = vf->pf;
72ecb896 150 int i, last_vector_idx;
ddf30f7f
AV
151
152 /* First, disable VF's configuration API to prevent OS from
153 * accessing the VF's VSI after it's freed or invalidated.
154 */
155 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
156
157 /* free vsi & disconnect it from the parent uplink */
158 if (vf->lan_vsi_idx) {
159 ice_vsi_release(pf->vsi[vf->lan_vsi_idx]);
160 vf->lan_vsi_idx = 0;
161 vf->lan_vsi_num = 0;
162 vf->num_mac = 0;
163 }
164
72ecb896 165 last_vector_idx = vf->first_vector_idx + pf->num_vf_msix - 1;
ddf30f7f 166 /* Disable interrupts so that VF starts in a known state */
72ecb896
BC
167 for (i = vf->first_vector_idx; i <= last_vector_idx; i++) {
168 wr32(&pf->hw, GLINT_DYN_CTL(i), GLINT_DYN_CTL_CLEARPBA_M);
ddf30f7f
AV
169 ice_flush(&pf->hw);
170 }
171 /* reset some of the state variables keeping track of the resources */
172 clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
173 clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
174}
175
ddf30f7f
AV
176/**
177 * ice_dis_vf_mappings
178 * @vf: pointer to the VF structure
179 */
180static void ice_dis_vf_mappings(struct ice_vf *vf)
181{
182 struct ice_pf *pf = vf->pf;
183 struct ice_vsi *vsi;
184 int first, last, v;
185 struct ice_hw *hw;
186
187 hw = &pf->hw;
188 vsi = pf->vsi[vf->lan_vsi_idx];
189
190 wr32(hw, VPINT_ALLOC(vf->vf_id), 0);
982b1219 191 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), 0);
ddf30f7f 192
cbe66bfe 193 first = vf->first_vector_idx;
ddf30f7f
AV
194 last = first + pf->num_vf_msix - 1;
195 for (v = first; v <= last; v++) {
196 u32 reg;
197
198 reg = (((1 << GLINT_VECT2FUNC_IS_PF_S) &
199 GLINT_VECT2FUNC_IS_PF_M) |
200 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
201 GLINT_VECT2FUNC_PF_NUM_M));
202 wr32(hw, GLINT_VECT2FUNC(v), reg);
203 }
204
205 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
206 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
207 else
208 dev_err(&pf->pdev->dev,
209 "Scattered mode for VF Tx queues is not yet implemented\n");
210
211 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
212 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
213 else
214 dev_err(&pf->pdev->dev,
215 "Scattered mode for VF Rx queues is not yet implemented\n");
216}
217
cbe66bfe
BC
218/**
219 * ice_sriov_free_msix_res - Reset/free any used MSIX resources
220 * @pf: pointer to the PF structure
221 *
222 * If MSIX entries from the pf->irq_tracker were needed then we need to
223 * reset the irq_tracker->end and give back the entries we needed to
224 * num_avail_sw_msix.
225 *
226 * If no MSIX entries were taken from the pf->irq_tracker then just clear
227 * the pf->sriov_base_vector.
228 *
229 * Returns 0 on success, and -EINVAL on error.
230 */
231static int ice_sriov_free_msix_res(struct ice_pf *pf)
232{
233 struct ice_res_tracker *res;
234
235 if (!pf)
236 return -EINVAL;
237
238 res = pf->irq_tracker;
239 if (!res)
240 return -EINVAL;
241
242 /* give back irq_tracker resources used */
243 if (pf->sriov_base_vector < res->num_entries) {
244 res->end = res->num_entries;
245 pf->num_avail_sw_msix +=
246 res->num_entries - pf->sriov_base_vector;
247 }
248
249 pf->sriov_base_vector = 0;
250
251 return 0;
252}
253
ddf30f7f
AV
254/**
255 * ice_free_vfs - Free all VFs
256 * @pf: pointer to the PF structure
257 */
258void ice_free_vfs(struct ice_pf *pf)
259{
260 struct ice_hw *hw = &pf->hw;
261 int tmp, i;
262
263 if (!pf->vf)
264 return;
265
266 while (test_and_set_bit(__ICE_VF_DIS, pf->state))
267 usleep_range(1000, 2000);
268
269 /* Avoid wait time by stopping all VFs at the same time */
270 for (i = 0; i < pf->num_alloc_vfs; i++) {
03f7a986
AV
271 struct ice_vsi *vsi;
272
ddf30f7f
AV
273 if (!test_bit(ICE_VF_STATE_ENA, pf->vf[i].vf_states))
274 continue;
275
03f7a986 276 vsi = pf->vsi[pf->vf[i].lan_vsi_idx];
ddf30f7f 277 /* stop rings without wait time */
03f7a986
AV
278 ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, i);
279 ice_vsi_stop_rx_rings(vsi);
ddf30f7f
AV
280
281 clear_bit(ICE_VF_STATE_ENA, pf->vf[i].vf_states);
282 }
283
72ecb896
BC
284 /* Disable IOV before freeing resources. This lets any VF drivers
285 * running in the host get themselves cleaned up before we yank
286 * the carpet out from underneath their feet.
287 */
288 if (!pci_vfs_assigned(pf->pdev))
289 pci_disable_sriov(pf->pdev);
290 else
291 dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
292
ddf30f7f
AV
293 tmp = pf->num_alloc_vfs;
294 pf->num_vf_qps = 0;
295 pf->num_alloc_vfs = 0;
296 for (i = 0; i < tmp; i++) {
297 if (test_bit(ICE_VF_STATE_INIT, pf->vf[i].vf_states)) {
298 /* disable VF qp mappings */
299 ice_dis_vf_mappings(&pf->vf[i]);
300
301 /* Set this state so that assigned VF vectors can be
302 * reclaimed by PF for reuse in ice_vsi_release(). No
303 * need to clear this bit since pf->vf array is being
304 * freed anyways after this for loop
305 */
306 set_bit(ICE_VF_STATE_CFG_INTR, pf->vf[i].vf_states);
307 ice_free_vf_res(&pf->vf[i]);
308 }
309 }
310
cbe66bfe
BC
311 if (ice_sriov_free_msix_res(pf))
312 dev_err(&pf->pdev->dev,
313 "Failed to free MSIX resources used by SR-IOV\n");
314
ddf30f7f
AV
315 devm_kfree(&pf->pdev->dev, pf->vf);
316 pf->vf = NULL;
317
318 /* This check is for when the driver is unloaded while VFs are
319 * assigned. Setting the number of VFs to 0 through sysfs is caught
320 * before this function ever gets called.
321 */
322 if (!pci_vfs_assigned(pf->pdev)) {
323 int vf_id;
324
325 /* Acknowledge VFLR for all VFs. Without this, VFs will fail to
326 * work correctly when SR-IOV gets re-enabled.
327 */
328 for (vf_id = 0; vf_id < tmp; vf_id++) {
329 u32 reg_idx, bit_idx;
330
331 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
332 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
333 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
334 }
335 }
336 clear_bit(__ICE_VF_DIS, pf->state);
337 clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
338}
339
340/**
341 * ice_trigger_vf_reset - Reset a VF on HW
342 * @vf: pointer to the VF structure
343 * @is_vflr: true if VFLR was issued, false if not
344 *
345 * Trigger hardware to start a reset for a particular VF. Expects the caller
346 * to wait the proper amount of time to allow hardware to reset the VF before
347 * it cleans up and restores VF functionality.
348 */
349static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr)
350{
351 struct ice_pf *pf = vf->pf;
352 u32 reg, reg_idx, bit_idx;
353 struct ice_hw *hw;
354 int vf_abs_id, i;
355
356 hw = &pf->hw;
357 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
358
359 /* Inform VF that it is no longer active, as a warning */
360 clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
361
362 /* Disable VF's configuration API during reset. The flag is re-enabled
363 * in ice_alloc_vf_res(), when it's safe again to access VF's VSI.
364 * It's normally disabled in ice_free_vf_res(), but it's safer
365 * to do it earlier to give some time to finish to any VF config
366 * functions that may still be running at this point.
367 */
368 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
82ba0128
MW
369
370 /* Clear the VF's ARQLEN register. This is how the VF detects reset,
371 * since the VFGEN_RSTAT register doesn't stick at 0 after reset.
372 */
373 wr32(hw, VF_MBX_ARQLEN(vf_abs_id), 0);
ddf30f7f
AV
374
375 /* In the case of a VFLR, the HW has already reset the VF and we
376 * just need to clean up, so don't hit the VFRTRIG register.
377 */
378 if (!is_vflr) {
379 /* reset VF using VPGEN_VFRTRIG reg */
380 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
381 reg |= VPGEN_VFRTRIG_VFSWR_M;
382 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
383 }
384 /* clear the VFLR bit in GLGEN_VFLRSTAT */
385 reg_idx = (vf_abs_id) / 32;
386 bit_idx = (vf_abs_id) % 32;
387 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
388 ice_flush(hw);
389
390 wr32(hw, PF_PCI_CIAA,
391 VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
392 for (i = 0; i < 100; i++) {
393 reg = rd32(hw, PF_PCI_CIAD);
394 if ((reg & VF_TRANS_PENDING_M) != 0)
395 dev_err(&pf->pdev->dev,
396 "VF %d PCI transactions stuck\n", vf->vf_id);
397 udelay(1);
398 }
399}
400
401/**
f9867df6
AV
402 * ice_vsi_set_pvid_fill_ctxt - Set VSI ctxt for add PVID
403 * @ctxt: the VSI ctxt to fill
404 * @vid: the VLAN ID to set as a PVID
ddf30f7f 405 */
77a7a84d
MS
406static void ice_vsi_set_pvid_fill_ctxt(struct ice_vsi_ctx *ctxt, u16 vid)
407{
408 ctxt->info.vlan_flags = (ICE_AQ_VSI_VLAN_MODE_UNTAGGED |
409 ICE_AQ_VSI_PVLAN_INSERT_PVID |
410 ICE_AQ_VSI_VLAN_EMOD_STR);
411 ctxt->info.pvid = cpu_to_le16(vid);
412 ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
413 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
414 ICE_AQ_VSI_PROP_SW_VALID);
415}
416
417/**
f9867df6 418 * ice_vsi_kill_pvid_fill_ctxt - Set VSI ctx for remove PVID
77a7a84d
MS
419 * @ctxt: the VSI ctxt to fill
420 */
421static void ice_vsi_kill_pvid_fill_ctxt(struct ice_vsi_ctx *ctxt)
422{
423 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
424 ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
425 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
426 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
427 ICE_AQ_VSI_PROP_SW_VALID);
428}
429
430/**
431 * ice_vsi_manage_pvid - Enable or disable port VLAN for VSI
432 * @vsi: the VSI to update
f9867df6
AV
433 * @vid: the VLAN ID to set as a PVID
434 * @enable: true for enable PVID false for disable
77a7a84d
MS
435 */
436static int ice_vsi_manage_pvid(struct ice_vsi *vsi, u16 vid, bool enable)
ddf30f7f
AV
437{
438 struct device *dev = &vsi->back->pdev->dev;
439 struct ice_hw *hw = &vsi->back->hw;
198a666a 440 struct ice_vsi_ctx *ctxt;
ddf30f7f 441 enum ice_status status;
198a666a
BA
442 int ret = 0;
443
444 ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
445 if (!ctxt)
446 return -ENOMEM;
ddf30f7f 447
77a7a84d
MS
448 ctxt->info = vsi->info;
449 if (enable)
450 ice_vsi_set_pvid_fill_ctxt(ctxt, vid);
451 else
452 ice_vsi_kill_pvid_fill_ctxt(ctxt);
ddf30f7f 453
198a666a 454 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
ddf30f7f 455 if (status) {
77a7a84d 456 dev_info(dev, "update VSI for port VLAN failed, err %d aq_err %d\n",
ddf30f7f 457 status, hw->adminq.sq_last_status);
198a666a
BA
458 ret = -EIO;
459 goto out;
ddf30f7f
AV
460 }
461
77a7a84d 462 vsi->info = ctxt->info;
198a666a
BA
463out:
464 devm_kfree(dev, ctxt);
465 return ret;
ddf30f7f
AV
466}
467
468/**
469 * ice_vf_vsi_setup - Set up a VF VSI
470 * @pf: board private structure
471 * @pi: pointer to the port_info instance
f9867df6 472 * @vf_id: defines VF ID to which this VSI connects.
ddf30f7f
AV
473 *
474 * Returns pointer to the successfully allocated VSI struct on success,
475 * otherwise returns NULL on failure.
476 */
477static struct ice_vsi *
478ice_vf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi, u16 vf_id)
479{
480 return ice_vsi_setup(pf, pi, ICE_VSI_VF, vf_id);
481}
482
cbe66bfe
BC
483/**
484 * ice_calc_vf_first_vector_idx - Calculate absolute MSIX vector index in HW
485 * @pf: pointer to PF structure
486 * @vf: pointer to VF that the first MSIX vector index is being calculated for
487 *
488 * This returns the first MSIX vector index in HW that is used by this VF and
489 * this will always be the OICR index in the AVF driver so any functionality
490 * using vf->first_vector_idx for queue configuration will have to increment by
491 * 1 to avoid meddling with the OICR index.
492 */
493static int ice_calc_vf_first_vector_idx(struct ice_pf *pf, struct ice_vf *vf)
494{
495 return pf->hw.func_caps.common_cap.msix_vector_first_id +
496 pf->sriov_base_vector + vf->vf_id * pf->num_vf_msix;
497}
498
ddf30f7f
AV
499/**
500 * ice_alloc_vsi_res - Setup VF VSI and its resources
501 * @vf: pointer to the VF structure
502 *
503 * Returns 0 on success, negative value on failure
504 */
505static int ice_alloc_vsi_res(struct ice_vf *vf)
506{
507 struct ice_pf *pf = vf->pf;
508 LIST_HEAD(tmp_add_list);
509 u8 broadcast[ETH_ALEN];
510 struct ice_vsi *vsi;
511 int status = 0;
512
cbe66bfe
BC
513 /* first vector index is the VFs OICR index */
514 vf->first_vector_idx = ice_calc_vf_first_vector_idx(pf, vf);
515
ddf30f7f
AV
516 vsi = ice_vf_vsi_setup(pf, pf->hw.port_info, vf->vf_id);
517
518 if (!vsi) {
519 dev_err(&pf->pdev->dev, "Failed to create VF VSI\n");
520 return -ENOMEM;
521 }
522
523 vf->lan_vsi_idx = vsi->idx;
524 vf->lan_vsi_num = vsi->vsi_num;
525
ddf30f7f 526 /* Check if port VLAN exist before, and restore it accordingly */
840bcd88 527 if (vf->port_vlan_id) {
77a7a84d 528 ice_vsi_manage_pvid(vsi, vf->port_vlan_id, true);
840bcd88
MS
529 ice_vsi_add_vlan(vsi, vf->port_vlan_id & ICE_VLAN_M);
530 }
ddf30f7f
AV
531
532 eth_broadcast_addr(broadcast);
533
534 status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
535 if (status)
536 goto ice_alloc_vsi_res_exit;
537
538 if (is_valid_ether_addr(vf->dflt_lan_addr.addr)) {
539 status = ice_add_mac_to_list(vsi, &tmp_add_list,
540 vf->dflt_lan_addr.addr);
541 if (status)
542 goto ice_alloc_vsi_res_exit;
543 }
544
545 status = ice_add_mac(&pf->hw, &tmp_add_list);
546 if (status)
547 dev_err(&pf->pdev->dev, "could not add mac filters\n");
548
549 /* Clear this bit after VF initialization since we shouldn't reclaim
550 * and reassign interrupts for synchronous or asynchronous VFR events.
94c4441b 551 * We don't want to reconfigure interrupts since AVF driver doesn't
ddf30f7f
AV
552 * expect vector assignment to be changed unless there is a request for
553 * more vectors.
554 */
555 clear_bit(ICE_VF_STATE_CFG_INTR, vf->vf_states);
556ice_alloc_vsi_res_exit:
557 ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
558 return status;
559}
560
561/**
562 * ice_alloc_vf_res - Allocate VF resources
563 * @vf: pointer to the VF structure
564 */
565static int ice_alloc_vf_res(struct ice_vf *vf)
566{
5743020d
AA
567 struct ice_pf *pf = vf->pf;
568 int tx_rx_queue_left;
ddf30f7f
AV
569 int status;
570
571 /* setup VF VSI and necessary resources */
572 status = ice_alloc_vsi_res(vf);
573 if (status)
574 goto ice_alloc_vf_res_exit;
575
5743020d
AA
576 /* Update number of VF queues, in case VF had requested for queue
577 * changes
578 */
579 tx_rx_queue_left = min_t(int, pf->q_left_tx, pf->q_left_rx);
580 tx_rx_queue_left += ICE_DFLT_QS_PER_VF;
581 if (vf->num_req_qs && vf->num_req_qs <= tx_rx_queue_left &&
582 vf->num_req_qs != vf->num_vf_qs)
583 vf->num_vf_qs = vf->num_req_qs;
584
ddf30f7f
AV
585 if (vf->trusted)
586 set_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
587 else
588 clear_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
589
590 /* VF is now completely initialized */
591 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
592
593 return status;
594
595ice_alloc_vf_res_exit:
596 ice_free_vf_res(vf);
597 return status;
598}
599
600/**
601 * ice_ena_vf_mappings
602 * @vf: pointer to the VF structure
603 *
604 * Enable VF vectors and queues allocation by writing the details into
605 * respective registers.
606 */
607static void ice_ena_vf_mappings(struct ice_vf *vf)
608{
609 struct ice_pf *pf = vf->pf;
610 struct ice_vsi *vsi;
611 int first, last, v;
612 struct ice_hw *hw;
613 int abs_vf_id;
614 u32 reg;
615
616 hw = &pf->hw;
617 vsi = pf->vsi[vf->lan_vsi_idx];
cbe66bfe 618 first = vf->first_vector_idx;
ddf30f7f
AV
619 last = (first + pf->num_vf_msix) - 1;
620 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
621
622 /* VF Vector allocation */
623 reg = (((first << VPINT_ALLOC_FIRST_S) & VPINT_ALLOC_FIRST_M) |
624 ((last << VPINT_ALLOC_LAST_S) & VPINT_ALLOC_LAST_M) |
625 VPINT_ALLOC_VALID_M);
626 wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
627
982b1219
AV
628 reg = (((first << VPINT_ALLOC_PCI_FIRST_S) & VPINT_ALLOC_PCI_FIRST_M) |
629 ((last << VPINT_ALLOC_PCI_LAST_S) & VPINT_ALLOC_PCI_LAST_M) |
630 VPINT_ALLOC_PCI_VALID_M);
631 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg);
ddf30f7f
AV
632 /* map the interrupts to its functions */
633 for (v = first; v <= last; v++) {
634 reg = (((abs_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
635 GLINT_VECT2FUNC_VF_NUM_M) |
636 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
637 GLINT_VECT2FUNC_PF_NUM_M));
638 wr32(hw, GLINT_VECT2FUNC(v), reg);
639 }
640
a7c9b47b
MW
641 /* Map mailbox interrupt. We put an explicit 0 here to remind us that
642 * VF admin queue interrupts will go to VF MSI-X vector 0.
643 */
644 wr32(hw, VPINT_MBX_CTL(abs_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M | 0);
982b1219
AV
645 /* set regardless of mapping mode */
646 wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M);
647
ddf30f7f
AV
648 /* VF Tx queues allocation */
649 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
ddf30f7f
AV
650 /* set the VF PF Tx queue range
651 * VFNUMQ value should be set to (number of queues - 1). A value
652 * of 0 means 1 queue and a value of 255 means 256 queues
653 */
654 reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
655 VPLAN_TX_QBASE_VFFIRSTQ_M) |
656 (((vsi->alloc_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
657 VPLAN_TX_QBASE_VFNUMQ_M));
658 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
659 } else {
660 dev_err(&pf->pdev->dev,
661 "Scattered mode for VF Tx queues is not yet implemented\n");
662 }
663
982b1219
AV
664 /* set regardless of mapping mode */
665 wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M);
666
ddf30f7f
AV
667 /* VF Rx queues allocation */
668 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
ddf30f7f
AV
669 /* set the VF PF Rx queue range
670 * VFNUMQ value should be set to (number of queues - 1). A value
671 * of 0 means 1 queue and a value of 255 means 256 queues
672 */
673 reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
674 VPLAN_RX_QBASE_VFFIRSTQ_M) |
675 (((vsi->alloc_txq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
676 VPLAN_RX_QBASE_VFNUMQ_M));
677 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
678 } else {
679 dev_err(&pf->pdev->dev,
680 "Scattered mode for VF Rx queues is not yet implemented\n");
681 }
682}
683
684/**
685 * ice_determine_res
686 * @pf: pointer to the PF structure
687 * @avail_res: available resources in the PF structure
688 * @max_res: maximum resources that can be given per VF
689 * @min_res: minimum resources that can be given per VF
690 *
691 * Returns non-zero value if resources (queues/vectors) are available or
692 * returns zero if PF cannot accommodate for all num_alloc_vfs.
693 */
694static int
695ice_determine_res(struct ice_pf *pf, u16 avail_res, u16 max_res, u16 min_res)
696{
697 bool checked_min_res = false;
698 int res;
699
700 /* start by checking if PF can assign max number of resources for
701 * all num_alloc_vfs.
702 * if yes, return number per VF
703 * If no, divide by 2 and roundup, check again
704 * repeat the loop till we reach a point where even minimum resources
705 * are not available, in that case return 0
706 */
707 res = max_res;
708 while ((res >= min_res) && !checked_min_res) {
709 int num_all_res;
710
711 num_all_res = pf->num_alloc_vfs * res;
712 if (num_all_res <= avail_res)
713 return res;
714
715 if (res == min_res)
716 checked_min_res = true;
717
718 res = DIV_ROUND_UP(res, 2);
719 }
720 return 0;
721}
722
cbe66bfe
BC
723/**
724 * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space
725 * @vf: VF to calculate the register index for
726 * @q_vector: a q_vector associated to the VF
727 */
728int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector)
729{
730 struct ice_pf *pf;
731
732 if (!vf || !q_vector)
733 return -EINVAL;
734
735 pf = vf->pf;
736
737 /* always add one to account for the OICR being the first MSIX */
738 return pf->sriov_base_vector + pf->num_vf_msix * vf->vf_id +
739 q_vector->v_idx + 1;
740}
741
742/**
743 * ice_get_max_valid_res_idx - Get the max valid resource index
744 * @res: pointer to the resource to find the max valid index for
745 *
746 * Start from the end of the ice_res_tracker and return right when we find the
747 * first res->list entry with the ICE_RES_VALID_BIT set. This function is only
748 * valid for SR-IOV because it is the only consumer that manipulates the
749 * res->end and this is always called when res->end is set to res->num_entries.
750 */
751static int ice_get_max_valid_res_idx(struct ice_res_tracker *res)
752{
753 int i;
754
755 if (!res)
756 return -EINVAL;
757
758 for (i = res->num_entries - 1; i >= 0; i--)
759 if (res->list[i] & ICE_RES_VALID_BIT)
760 return i;
761
762 return 0;
763}
764
765/**
766 * ice_sriov_set_msix_res - Set any used MSIX resources
767 * @pf: pointer to PF structure
768 * @num_msix_needed: number of MSIX vectors needed for all SR-IOV VFs
769 *
770 * This function allows SR-IOV resources to be taken from the end of the PF's
771 * allowed HW MSIX vectors so in many cases the irq_tracker will not
772 * be needed. In these cases we just set the pf->sriov_base_vector and return
773 * success.
774 *
775 * If SR-IOV needs to use any pf->irq_tracker entries it updates the
776 * irq_tracker->end based on the first entry needed for SR-IOV. This makes it
777 * so any calls to ice_get_res() using the irq_tracker will not try to use
778 * resources at or beyond the newly set value.
779 *
780 * Return 0 on success, and -EINVAL when there are not enough MSIX vectors in
781 * in the PF's space available for SR-IOV.
782 */
783static int ice_sriov_set_msix_res(struct ice_pf *pf, u16 num_msix_needed)
784{
785 int max_valid_res_idx = ice_get_max_valid_res_idx(pf->irq_tracker);
786 u16 pf_total_msix_vectors =
787 pf->hw.func_caps.common_cap.num_msix_vectors;
788 struct ice_res_tracker *res = pf->irq_tracker;
789 int sriov_base_vector;
790
791 if (max_valid_res_idx < 0)
792 return max_valid_res_idx;
793
794 sriov_base_vector = pf_total_msix_vectors - num_msix_needed;
795
796 /* make sure we only grab irq_tracker entries from the list end and
797 * that we have enough available MSIX vectors
798 */
799 if (sriov_base_vector <= max_valid_res_idx)
800 return -EINVAL;
801
802 pf->sriov_base_vector = sriov_base_vector;
803
804 /* dip into irq_tracker entries and update used resources */
805 if (num_msix_needed > (pf_total_msix_vectors - res->num_entries)) {
806 pf->num_avail_sw_msix -=
807 res->num_entries - pf->sriov_base_vector;
808 res->end = pf->sriov_base_vector;
809 }
810
811 return 0;
812}
813
ddf30f7f
AV
814/**
815 * ice_check_avail_res - check if vectors and queues are available
816 * @pf: pointer to the PF structure
817 *
818 * This function is where we calculate actual number of resources for VF VSIs,
819 * we don't reserve ahead of time during probe. Returns success if vectors and
820 * queues resources are available, otherwise returns error code
821 */
822static int ice_check_avail_res(struct ice_pf *pf)
823{
cbe66bfe
BC
824 int max_valid_res_idx = ice_get_max_valid_res_idx(pf->irq_tracker);
825 u16 num_msix, num_txq, num_rxq, num_avail_msix;
ddf30f7f 826
cbe66bfe 827 if (!pf->num_alloc_vfs || max_valid_res_idx < 0)
ddf30f7f
AV
828 return -EINVAL;
829
cbe66bfe
BC
830 /* add 1 to max_valid_res_idx to account for it being 0-based */
831 num_avail_msix = pf->hw.func_caps.common_cap.num_msix_vectors -
832 (max_valid_res_idx + 1);
833
ddf30f7f
AV
834 /* Grab from HW interrupts common pool
835 * Note: By the time the user decides it needs more vectors in a VF
836 * its already too late since one must decide this prior to creating the
837 * VF interface. So the best we can do is take a guess as to what the
838 * user might want.
839 *
840 * We have two policies for vector allocation:
841 * 1. if num_alloc_vfs is from 1 to 16, then we consider this as small
842 * number of NFV VFs used for NFV appliances, since this is a special
843 * case, we try to assign maximum vectors per VF (65) as much as
844 * possible, based on determine_resources algorithm.
845 * 2. if num_alloc_vfs is from 17 to 256, then its large number of
846 * regular VFs which are not used for any special purpose. Hence try to
847 * grab default interrupt vectors (5 as supported by AVF driver).
848 */
849 if (pf->num_alloc_vfs <= 16) {
cbe66bfe 850 num_msix = ice_determine_res(pf, num_avail_msix,
ddf30f7f
AV
851 ICE_MAX_INTR_PER_VF,
852 ICE_MIN_INTR_PER_VF);
853 } else if (pf->num_alloc_vfs <= ICE_MAX_VF_COUNT) {
cbe66bfe 854 num_msix = ice_determine_res(pf, num_avail_msix,
ddf30f7f
AV
855 ICE_DFLT_INTR_PER_VF,
856 ICE_MIN_INTR_PER_VF);
857 } else {
858 dev_err(&pf->pdev->dev,
859 "Number of VFs %d exceeds max VF count %d\n",
860 pf->num_alloc_vfs, ICE_MAX_VF_COUNT);
861 return -EIO;
862 }
863
864 if (!num_msix)
865 return -EIO;
866
867 /* Grab from the common pool
868 * start by requesting Default queues (4 as supported by AVF driver),
869 * Note that, the main difference between queues and vectors is, latter
870 * can only be reserved at init time but queues can be requested by VF
871 * at runtime through Virtchnl, that is the reason we start by reserving
872 * few queues.
873 */
874 num_txq = ice_determine_res(pf, pf->q_left_tx, ICE_DFLT_QS_PER_VF,
875 ICE_MIN_QS_PER_VF);
876
877 num_rxq = ice_determine_res(pf, pf->q_left_rx, ICE_DFLT_QS_PER_VF,
878 ICE_MIN_QS_PER_VF);
879
880 if (!num_txq || !num_rxq)
881 return -EIO;
882
cbe66bfe
BC
883 if (ice_sriov_set_msix_res(pf, num_msix * pf->num_alloc_vfs))
884 return -EINVAL;
885
ddf30f7f
AV
886 /* since AVF driver works with only queue pairs which means, it expects
887 * to have equal number of Rx and Tx queues, so take the minimum of
888 * available Tx or Rx queues
889 */
890 pf->num_vf_qps = min_t(int, num_txq, num_rxq);
891 pf->num_vf_msix = num_msix;
892
893 return 0;
894}
895
896/**
897 * ice_cleanup_and_realloc_vf - Clean up VF and reallocate resources after reset
898 * @vf: pointer to the VF structure
899 *
900 * Cleanup a VF after the hardware reset is finished. Expects the caller to
901 * have verified whether the reset is finished properly, and ensure the
902 * minimum amount of wait time has passed. Reallocate VF resources back to make
903 * VF state active
904 */
905static void ice_cleanup_and_realloc_vf(struct ice_vf *vf)
906{
907 struct ice_pf *pf = vf->pf;
908 struct ice_hw *hw;
909 u32 reg;
910
911 hw = &pf->hw;
912
913 /* PF software completes the flow by notifying VF that reset flow is
914 * completed. This is done by enabling hardware by clearing the reset
915 * bit in the VPGEN_VFRTRIG reg and setting VFR_STATE in the VFGEN_RSTAT
916 * register to VFR completed (done at the end of this function)
917 * By doing this we allow HW to access VF memory at any point. If we
918 * did it any sooner, HW could access memory while it was being freed
919 * in ice_free_vf_res(), causing an IOMMU fault.
920 *
921 * On the other hand, this needs to be done ASAP, because the VF driver
922 * is waiting for this to happen and may report a timeout. It's
923 * harmless, but it gets logged into Guest OS kernel log, so best avoid
924 * it.
925 */
926 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
927 reg &= ~VPGEN_VFRTRIG_VFSWR_M;
928 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
929
930 /* reallocate VF resources to finish resetting the VSI state */
931 if (!ice_alloc_vf_res(vf)) {
932 ice_ena_vf_mappings(vf);
933 set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
934 clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
935 vf->num_vlan = 0;
936 }
937
938 /* Tell the VF driver the reset is done. This needs to be done only
939 * after VF has been fully initialized, because the VF driver may
940 * request resources immediately after setting this flag.
941 */
942 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
943}
944
5eda8afd
AA
945/**
946 * ice_vf_set_vsi_promisc - set given VF VSI to given promiscuous mode(s)
947 * @vf: pointer to the VF info
948 * @vsi: the VSI being configured
949 * @promisc_m: mask of promiscuous config bits
950 * @rm_promisc: promisc flag request from the VF to remove or add filter
951 *
952 * This function configures VF VSI promiscuous mode, based on the VF requests,
953 * for Unicast, Multicast and VLAN
954 */
955static enum ice_status
956ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m,
957 bool rm_promisc)
958{
959 struct ice_pf *pf = vf->pf;
960 enum ice_status status = 0;
961 struct ice_hw *hw;
962
963 hw = &pf->hw;
964 if (vf->num_vlan) {
965 status = ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_m,
966 rm_promisc);
967 } else if (vf->port_vlan_id) {
968 if (rm_promisc)
969 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
970 vf->port_vlan_id);
971 else
972 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
973 vf->port_vlan_id);
974 } else {
975 if (rm_promisc)
976 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
977 0);
978 else
979 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
980 0);
981 }
982
983 return status;
984}
985
ddf30f7f
AV
986/**
987 * ice_reset_all_vfs - reset all allocated VFs in one go
988 * @pf: pointer to the PF structure
989 * @is_vflr: true if VFLR was issued, false if not
990 *
991 * First, tell the hardware to reset each VF, then do all the waiting in one
992 * chunk, and finally finish restoring each VF after the wait. This is useful
993 * during PF routines which need to reset all VFs, as otherwise it must perform
994 * these resets in a serialized fashion.
995 *
996 * Returns true if any VFs were reset, and false otherwise.
997 */
998bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr)
999{
1000 struct ice_hw *hw = &pf->hw;
42b2cc83 1001 struct ice_vf *vf;
ddf30f7f
AV
1002 int v, i;
1003
1004 /* If we don't have any VFs, then there is nothing to reset */
1005 if (!pf->num_alloc_vfs)
1006 return false;
1007
1008 /* If VFs have been disabled, there is no need to reset */
1009 if (test_and_set_bit(__ICE_VF_DIS, pf->state))
1010 return false;
1011
1012 /* Begin reset on all VFs at once */
1013 for (v = 0; v < pf->num_alloc_vfs; v++)
1014 ice_trigger_vf_reset(&pf->vf[v], is_vflr);
1015
42b2cc83
AA
1016 for (v = 0; v < pf->num_alloc_vfs; v++) {
1017 struct ice_vsi *vsi;
1018
1019 vf = &pf->vf[v];
1020 vsi = pf->vsi[vf->lan_vsi_idx];
1021 if (test_bit(ICE_VF_STATE_ENA, vf->vf_states)) {
1022 ice_vsi_stop_lan_tx_rings(vsi, ICE_VF_RESET, vf->vf_id);
1023 ice_vsi_stop_rx_rings(vsi);
1024 clear_bit(ICE_VF_STATE_ENA, vf->vf_states);
1025 }
1026 }
ddf30f7f
AV
1027
1028 /* HW requires some time to make sure it can flush the FIFO for a VF
1029 * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1030 * sequence to make sure that it has completed. We'll keep track of
1031 * the VFs using a simple iterator that increments once that VF has
1032 * finished resetting.
1033 */
1034 for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1035 usleep_range(10000, 20000);
1036
1037 /* Check each VF in sequence */
1038 while (v < pf->num_alloc_vfs) {
ddf30f7f
AV
1039 u32 reg;
1040
42b2cc83 1041 vf = &pf->vf[v];
ddf30f7f
AV
1042 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1043 if (!(reg & VPGEN_VFRSTAT_VFRD_M))
1044 break;
1045
1046 /* If the current VF has finished resetting, move on
1047 * to the next VF in sequence.
1048 */
1049 v++;
1050 }
1051 }
1052
1053 /* Display a warning if at least one VF didn't manage to reset in
1054 * time, but continue on with the operation.
1055 */
1056 if (v < pf->num_alloc_vfs)
1057 dev_warn(&pf->pdev->dev, "VF reset check timeout\n");
1058 usleep_range(10000, 20000);
1059
1060 /* free VF resources to begin resetting the VSI state */
5743020d
AA
1061 for (v = 0; v < pf->num_alloc_vfs; v++) {
1062 vf = &pf->vf[v];
1063
1064 ice_free_vf_res(vf);
1065
1066 /* Free VF queues as well, and reallocate later.
1067 * If a given VF has different number of queues
1068 * configured, the request for update will come
1069 * via mailbox communication.
1070 */
1071 vf->num_vf_qs = 0;
1072 }
ddf30f7f 1073
cbe66bfe
BC
1074 if (ice_sriov_free_msix_res(pf))
1075 dev_err(&pf->pdev->dev,
1076 "Failed to free MSIX resources used by SR-IOV\n");
1077
ddf30f7f
AV
1078 if (ice_check_avail_res(pf)) {
1079 dev_err(&pf->pdev->dev,
1080 "Cannot allocate VF resources, try with fewer number of VFs\n");
1081 return false;
1082 }
1083
1084 /* Finish the reset on each VF */
5743020d
AA
1085 for (v = 0; v < pf->num_alloc_vfs; v++) {
1086 vf = &pf->vf[v];
1087
1088 vf->num_vf_qs = pf->num_vf_qps;
1089 dev_dbg(&pf->pdev->dev,
1090 "VF-id %d has %d queues configured\n",
1091 vf->vf_id, vf->num_vf_qs);
1092 ice_cleanup_and_realloc_vf(vf);
1093 }
ddf30f7f
AV
1094
1095 ice_flush(hw);
1096 clear_bit(__ICE_VF_DIS, pf->state);
1097
1098 return true;
1099}
1100
007676b4
AV
1101/**
1102 * ice_reset_vf - Reset a particular VF
1103 * @vf: pointer to the VF structure
1104 * @is_vflr: true if VFLR was issued, false if not
1105 *
1106 * Returns true if the VF is reset, false otherwise.
1107 */
1108static bool ice_reset_vf(struct ice_vf *vf, bool is_vflr)
1109{
1110 struct ice_pf *pf = vf->pf;
03f7a986 1111 struct ice_vsi *vsi;
5eda8afd 1112 struct ice_hw *hw;
007676b4 1113 bool rsd = false;
5eda8afd 1114 u8 promisc_m;
007676b4
AV
1115 u32 reg;
1116 int i;
1117
1118 /* If the VFs have been disabled, this means something else is
1119 * resetting the VF, so we shouldn't continue.
1120 */
1121 if (test_and_set_bit(__ICE_VF_DIS, pf->state))
1122 return false;
1123
1124 ice_trigger_vf_reset(vf, is_vflr);
1125
03f7a986
AV
1126 vsi = pf->vsi[vf->lan_vsi_idx];
1127
007676b4 1128 if (test_bit(ICE_VF_STATE_ENA, vf->vf_states)) {
03f7a986
AV
1129 ice_vsi_stop_lan_tx_rings(vsi, ICE_VF_RESET, vf->vf_id);
1130 ice_vsi_stop_rx_rings(vsi);
007676b4
AV
1131 clear_bit(ICE_VF_STATE_ENA, vf->vf_states);
1132 } else {
1133 /* Call Disable LAN Tx queue AQ call even when queues are not
1134 * enabled. This is needed for successful completiom of VFR
1135 */
bb87ee0e
AV
1136 ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
1137 NULL, ICE_VF_RESET, vf->vf_id, NULL);
007676b4
AV
1138 }
1139
5eda8afd 1140 hw = &pf->hw;
007676b4
AV
1141 /* poll VPGEN_VFRSTAT reg to make sure
1142 * that reset is complete
1143 */
1144 for (i = 0; i < 10; i++) {
1145 /* VF reset requires driver to first reset the VF and then
1146 * poll the status register to make sure that the reset
1147 * completed successfully.
1148 */
1149 usleep_range(10000, 20000);
1150 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1151 if (reg & VPGEN_VFRSTAT_VFRD_M) {
1152 rsd = true;
1153 break;
1154 }
1155 }
1156
1157 /* Display a warning if VF didn't manage to reset in time, but need to
1158 * continue on with the operation.
1159 */
1160 if (!rsd)
1161 dev_warn(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
1162 vf->vf_id);
1163
1164 usleep_range(10000, 20000);
1165
5eda8afd
AA
1166 /* disable promiscuous modes in case they were enabled
1167 * ignore any error if disabling process failed
1168 */
1169 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
1170 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
1171 if (vf->port_vlan_id || vf->num_vlan)
1172 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
1173 else
1174 promisc_m = ICE_UCAST_PROMISC_BITS;
1175
1176 vsi = pf->vsi[vf->lan_vsi_idx];
1177 if (ice_vf_set_vsi_promisc(vf, vsi, promisc_m, true))
1178 dev_err(&pf->pdev->dev, "disabling promiscuous mode failed\n");
1179 }
1180
007676b4
AV
1181 /* free VF resources to begin resetting the VSI state */
1182 ice_free_vf_res(vf);
1183
1184 ice_cleanup_and_realloc_vf(vf);
1185
1186 ice_flush(hw);
1187 clear_bit(__ICE_VF_DIS, pf->state);
1188
1189 return true;
1190}
1191
53b8decb
AV
1192/**
1193 * ice_vc_notify_link_state - Inform all VFs on a PF of link status
1194 * @pf: pointer to the PF structure
1195 */
1196void ice_vc_notify_link_state(struct ice_pf *pf)
1197{
1198 int i;
1199
1200 for (i = 0; i < pf->num_alloc_vfs; i++)
1201 ice_vc_notify_vf_link_state(&pf->vf[i]);
1202}
1203
007676b4
AV
1204/**
1205 * ice_vc_notify_reset - Send pending reset message to all VFs
1206 * @pf: pointer to the PF structure
1207 *
1208 * indicate a pending reset to all VFs on a given PF
1209 */
1210void ice_vc_notify_reset(struct ice_pf *pf)
1211{
1212 struct virtchnl_pf_event pfe;
1213
1214 if (!pf->num_alloc_vfs)
1215 return;
1216
1217 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1218 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
cf6c6e01 1219 ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
007676b4
AV
1220 (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
1221}
1222
7c710869
AV
1223/**
1224 * ice_vc_notify_vf_reset - Notify VF of a reset event
1225 * @vf: pointer to the VF structure
1226 */
1227static void ice_vc_notify_vf_reset(struct ice_vf *vf)
1228{
1229 struct virtchnl_pf_event pfe;
1230
1231 /* validate the request */
1232 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1233 return;
1234
1235 /* verify if the VF is in either init or active before proceeding */
1236 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
1237 !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
1238 return;
1239
1240 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1241 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
cf6c6e01
MW
1242 ice_aq_send_msg_to_vf(&vf->pf->hw, vf->vf_id, VIRTCHNL_OP_EVENT,
1243 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe, sizeof(pfe),
1244 NULL);
7c710869
AV
1245}
1246
ddf30f7f
AV
1247/**
1248 * ice_alloc_vfs - Allocate and set up VFs resources
1249 * @pf: pointer to the PF structure
1250 * @num_alloc_vfs: number of VFs to allocate
1251 */
1252static int ice_alloc_vfs(struct ice_pf *pf, u16 num_alloc_vfs)
1253{
1254 struct ice_hw *hw = &pf->hw;
1255 struct ice_vf *vfs;
1256 int i, ret;
1257
1258 /* Disable global interrupt 0 so we don't try to handle the VFLR. */
cbe66bfe 1259 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
ddf30f7f
AV
1260 ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
1261
1262 ice_flush(hw);
1263
1264 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
1265 if (ret) {
1266 pf->num_alloc_vfs = 0;
1267 goto err_unroll_intr;
1268 }
1269 /* allocate memory */
1270 vfs = devm_kcalloc(&pf->pdev->dev, num_alloc_vfs, sizeof(*vfs),
1271 GFP_KERNEL);
1272 if (!vfs) {
1273 ret = -ENOMEM;
72f9c203 1274 goto err_pci_disable_sriov;
ddf30f7f
AV
1275 }
1276 pf->vf = vfs;
1277
1278 /* apply default profile */
1279 for (i = 0; i < num_alloc_vfs; i++) {
1280 vfs[i].pf = pf;
1281 vfs[i].vf_sw_id = pf->first_sw;
1282 vfs[i].vf_id = i;
1283
1284 /* assign default capabilities */
1285 set_bit(ICE_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
1286 vfs[i].spoofchk = true;
1287
1288 /* Set this state so that PF driver does VF vector assignment */
1289 set_bit(ICE_VF_STATE_CFG_INTR, vfs[i].vf_states);
1290 }
1291 pf->num_alloc_vfs = num_alloc_vfs;
1292
1293 /* VF resources get allocated during reset */
72f9c203
BC
1294 if (!ice_reset_all_vfs(pf, true)) {
1295 ret = -EIO;
ddf30f7f 1296 goto err_unroll_sriov;
72f9c203 1297 }
ddf30f7f
AV
1298
1299 goto err_unroll_intr;
1300
1301err_unroll_sriov:
72f9c203
BC
1302 pf->vf = NULL;
1303 devm_kfree(&pf->pdev->dev, vfs);
1304 vfs = NULL;
1305 pf->num_alloc_vfs = 0;
1306err_pci_disable_sriov:
ddf30f7f
AV
1307 pci_disable_sriov(pf->pdev);
1308err_unroll_intr:
1309 /* rearm interrupts here */
1310 ice_irq_dynamic_ena(hw, NULL, NULL);
1311 return ret;
1312}
1313
1314/**
1315 * ice_pf_state_is_nominal - checks the pf for nominal state
1316 * @pf: pointer to pf to check
1317 *
1318 * Check the PF's state for a collection of bits that would indicate
1319 * the PF is in a state that would inhibit normal operation for
1320 * driver functionality.
1321 *
1322 * Returns true if PF is in a nominal state.
1323 * Returns false otherwise
1324 */
1325static bool ice_pf_state_is_nominal(struct ice_pf *pf)
1326{
1327 DECLARE_BITMAP(check_bits, __ICE_STATE_NBITS) = { 0 };
1328
1329 if (!pf)
1330 return false;
1331
1332 bitmap_set(check_bits, 0, __ICE_STATE_NOMINAL_CHECK_BITS);
1333 if (bitmap_intersects(pf->state, check_bits, __ICE_STATE_NBITS))
1334 return false;
1335
1336 return true;
1337}
1338
1339/**
1340 * ice_pci_sriov_ena - Enable or change number of VFs
1341 * @pf: pointer to the PF structure
1342 * @num_vfs: number of VFs to allocate
1343 */
1344static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
1345{
1346 int pre_existing_vfs = pci_num_vf(pf->pdev);
1347 struct device *dev = &pf->pdev->dev;
1348 int err;
1349
1350 if (!ice_pf_state_is_nominal(pf)) {
1351 dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
1352 return -EBUSY;
1353 }
1354
1355 if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
1356 dev_err(dev, "This device is not capable of SR-IOV\n");
1357 return -ENODEV;
1358 }
1359
1360 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1361 ice_free_vfs(pf);
1362 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1363 return num_vfs;
1364
1365 if (num_vfs > pf->num_vfs_supported) {
1366 dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
1367 num_vfs, pf->num_vfs_supported);
1368 return -ENOTSUPP;
1369 }
1370
1371 dev_info(dev, "Allocating %d VFs\n", num_vfs);
1372 err = ice_alloc_vfs(pf, num_vfs);
1373 if (err) {
1374 dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
1375 return err;
1376 }
1377
1378 set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
1379 return num_vfs;
1380}
1381
1382/**
1383 * ice_sriov_configure - Enable or change number of VFs via sysfs
1384 * @pdev: pointer to a pci_dev structure
1385 * @num_vfs: number of VFs to allocate
1386 *
1387 * This function is called when the user updates the number of VFs in sysfs.
1388 */
1389int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
1390{
1391 struct ice_pf *pf = pci_get_drvdata(pdev);
1392
1393 if (num_vfs)
1394 return ice_pci_sriov_ena(pf, num_vfs);
1395
1396 if (!pci_vfs_assigned(pdev)) {
1397 ice_free_vfs(pf);
1398 } else {
1399 dev_err(&pf->pdev->dev,
1400 "can't free VFs because some are assigned to VMs.\n");
1401 return -EBUSY;
1402 }
1403
1404 return 0;
1405}
007676b4
AV
1406
1407/**
1408 * ice_process_vflr_event - Free VF resources via IRQ calls
1409 * @pf: pointer to the PF structure
1410 *
df17b7e0 1411 * called from the VFLR IRQ handler to
007676b4
AV
1412 * free up VF resources and state variables
1413 */
1414void ice_process_vflr_event(struct ice_pf *pf)
1415{
1416 struct ice_hw *hw = &pf->hw;
1417 int vf_id;
1418 u32 reg;
1419
8d7189d2 1420 if (!test_and_clear_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
007676b4
AV
1421 !pf->num_alloc_vfs)
1422 return;
1423
007676b4
AV
1424 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1425 struct ice_vf *vf = &pf->vf[vf_id];
1426 u32 reg_idx, bit_idx;
1427
1428 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1429 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1430 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
1431 reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
1432 if (reg & BIT(bit_idx))
1433 /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
1434 ice_reset_vf(vf, true);
1435 }
1436}
7c710869
AV
1437
1438/**
1439 * ice_vc_dis_vf - Disable a given VF via SW reset
1440 * @vf: pointer to the VF info
1441 *
1442 * Disable the VF through a SW reset
1443 */
1444static void ice_vc_dis_vf(struct ice_vf *vf)
1445{
1446 ice_vc_notify_vf_reset(vf);
1447 ice_reset_vf(vf, false);
1448}
1449
1071a835
AV
1450/**
1451 * ice_vc_send_msg_to_vf - Send message to VF
1452 * @vf: pointer to the VF info
1453 * @v_opcode: virtual channel opcode
1454 * @v_retval: virtual channel return value
1455 * @msg: pointer to the msg buffer
1456 * @msglen: msg length
1457 *
1458 * send msg to VF
1459 */
c8b7abdd 1460static int
cf6c6e01
MW
1461ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
1462 enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
1071a835
AV
1463{
1464 enum ice_status aq_ret;
1465 struct ice_pf *pf;
1466
1467 /* validate the request */
1468 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1469 return -EINVAL;
1470
1471 pf = vf->pf;
1472
1473 /* single place to detect unsuccessful return values */
1474 if (v_retval) {
1475 vf->num_inval_msgs++;
1476 dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
1477 vf->vf_id, v_opcode, v_retval);
1478 if (vf->num_inval_msgs > ICE_DFLT_NUM_INVAL_MSGS_ALLOWED) {
1479 dev_err(&pf->pdev->dev,
1480 "Number of invalid messages exceeded for VF %d\n",
1481 vf->vf_id);
1482 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1483 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
1484 return -EIO;
1485 }
1486 } else {
1487 vf->num_valid_msgs++;
1488 /* reset the invalid counter, if a valid message is received. */
1489 vf->num_inval_msgs = 0;
1490 }
1491
1492 aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
1493 msg, msglen, NULL);
1494 if (aq_ret) {
1495 dev_info(&pf->pdev->dev,
1496 "Unable to send the message to VF %d aq_err %d\n",
1497 vf->vf_id, pf->hw.mailboxq.sq_last_status);
1498 return -EIO;
1499 }
1500
1501 return 0;
1502}
1503
1504/**
1505 * ice_vc_get_ver_msg
1506 * @vf: pointer to the VF info
1507 * @msg: pointer to the msg buffer
1508 *
1509 * called from the VF to request the API version used by the PF
1510 */
1511static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
1512{
1513 struct virtchnl_version_info info = {
1514 VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
1515 };
1516
1517 vf->vf_ver = *(struct virtchnl_version_info *)msg;
1518 /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1519 if (VF_IS_V10(&vf->vf_ver))
1520 info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
1521
cf6c6e01
MW
1522 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
1523 VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
1071a835
AV
1524 sizeof(struct virtchnl_version_info));
1525}
1526
1527/**
1528 * ice_vc_get_vf_res_msg
1529 * @vf: pointer to the VF info
1530 * @msg: pointer to the msg buffer
1531 *
1532 * called from the VF to request its resources
1533 */
1534static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
1535{
cf6c6e01 1536 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1071a835 1537 struct virtchnl_vf_resource *vfres = NULL;
1071a835
AV
1538 struct ice_pf *pf = vf->pf;
1539 struct ice_vsi *vsi;
1540 int len = 0;
1541 int ret;
1542
1543 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
cf6c6e01 1544 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1545 goto err;
1546 }
1547
1548 len = sizeof(struct virtchnl_vf_resource);
1549
1550 vfres = devm_kzalloc(&pf->pdev->dev, len, GFP_KERNEL);
1551 if (!vfres) {
cf6c6e01 1552 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
1071a835
AV
1553 len = 0;
1554 goto err;
1555 }
1556 if (VF_IS_V11(&vf->vf_ver))
1557 vf->driver_caps = *(u32 *)msg;
1558 else
1559 vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
1560 VIRTCHNL_VF_OFFLOAD_RSS_REG |
1561 VIRTCHNL_VF_OFFLOAD_VLAN;
1562
1563 vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
1564 vsi = pf->vsi[vf->lan_vsi_idx];
f1ef73f5 1565 if (!vsi) {
cf6c6e01 1566 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
f1ef73f5
AA
1567 goto err;
1568 }
1569
1071a835
AV
1570 if (!vsi->info.pvid)
1571 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
1572
1573 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1574 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
1575 } else {
1576 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
1577 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1578 else
1579 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
1580 }
1581
1582 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1583 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
1584
1585 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
1586 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
1587
1588 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
1589 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
1590
1591 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
1592 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
1593
1594 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1595 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
1596
1597 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
1598 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
1599
1600 if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
1601 vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
1602
1603 vfres->num_vsis = 1;
1604 /* Tx and Rx queue are equal for VF */
1605 vfres->num_queue_pairs = vsi->num_txq;
1606 vfres->max_vectors = pf->num_vf_msix;
1607 vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
1608 vfres->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
1609
1610 vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
1611 vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
1612 vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
1613 ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
1614 vf->dflt_lan_addr.addr);
1615
1616 set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
1617
1618err:
1619 /* send the response back to the VF */
cf6c6e01 1620 ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
1071a835
AV
1621 (u8 *)vfres, len);
1622
1623 devm_kfree(&pf->pdev->dev, vfres);
1624 return ret;
1625}
1626
1627/**
1628 * ice_vc_reset_vf_msg
1629 * @vf: pointer to the VF info
1630 *
1631 * called from the VF to reset itself,
1632 * unlike other virtchnl messages, PF driver
1633 * doesn't send the response back to the VF
1634 */
1635static void ice_vc_reset_vf_msg(struct ice_vf *vf)
1636{
1637 if (test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
1638 ice_reset_vf(vf, false);
1639}
1640
1641/**
1642 * ice_find_vsi_from_id
1643 * @pf: the pf structure to search for the VSI
f9867df6 1644 * @id: ID of the VSI it is searching for
1071a835 1645 *
f9867df6 1646 * searches for the VSI with the given ID
1071a835
AV
1647 */
1648static struct ice_vsi *ice_find_vsi_from_id(struct ice_pf *pf, u16 id)
1649{
1650 int i;
1651
80ed404a 1652 ice_for_each_vsi(pf, i)
1071a835
AV
1653 if (pf->vsi[i] && pf->vsi[i]->vsi_num == id)
1654 return pf->vsi[i];
1655
1656 return NULL;
1657}
1658
1659/**
1660 * ice_vc_isvalid_vsi_id
1661 * @vf: pointer to the VF info
f9867df6 1662 * @vsi_id: VF relative VSI ID
1071a835 1663 *
f9867df6 1664 * check for the valid VSI ID
1071a835
AV
1665 */
1666static bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
1667{
1668 struct ice_pf *pf = vf->pf;
1669 struct ice_vsi *vsi;
1670
1671 vsi = ice_find_vsi_from_id(pf, vsi_id);
1672
1673 return (vsi && (vsi->vf_id == vf->vf_id));
1674}
1675
1676/**
1677 * ice_vc_isvalid_q_id
1678 * @vf: pointer to the VF info
f9867df6
AV
1679 * @vsi_id: VSI ID
1680 * @qid: VSI relative queue ID
1071a835 1681 *
f9867df6 1682 * check for the valid queue ID
1071a835
AV
1683 */
1684static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
1685{
1686 struct ice_vsi *vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
1687 /* allocated Tx and Rx queues should be always equal for VF VSI */
1688 return (vsi && (qid < vsi->alloc_txq));
1689}
1690
1691/**
1692 * ice_vc_config_rss_key
1693 * @vf: pointer to the VF info
1694 * @msg: pointer to the msg buffer
1695 *
1696 * Configure the VF's RSS key
1697 */
1698static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
1699{
cf6c6e01 1700 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1071a835
AV
1701 struct virtchnl_rss_key *vrk =
1702 (struct virtchnl_rss_key *)msg;
f1ef73f5 1703 struct ice_pf *pf = vf->pf;
cf6c6e01 1704 struct ice_vsi *vsi = NULL;
1071a835
AV
1705
1706 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
cf6c6e01 1707 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1708 goto error_param;
1709 }
1710
1711 if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
cf6c6e01 1712 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1713 goto error_param;
1714 }
1715
f1ef73f5 1716 vsi = pf->vsi[vf->lan_vsi_idx];
1071a835 1717 if (!vsi) {
cf6c6e01 1718 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1719 goto error_param;
1720 }
1721
1722 if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
cf6c6e01 1723 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1724 goto error_param;
1725 }
1726
1727 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
cf6c6e01 1728 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1729 goto error_param;
1730 }
1731
cf6c6e01
MW
1732 if (ice_set_rss(vsi, vrk->key, NULL, 0))
1733 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1071a835 1734error_param:
cf6c6e01 1735 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
1071a835
AV
1736 NULL, 0);
1737}
1738
1739/**
1740 * ice_vc_config_rss_lut
1741 * @vf: pointer to the VF info
1742 * @msg: pointer to the msg buffer
1743 *
1744 * Configure the VF's RSS LUT
1745 */
1746static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
1747{
1748 struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
cf6c6e01 1749 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
f1ef73f5 1750 struct ice_pf *pf = vf->pf;
cf6c6e01 1751 struct ice_vsi *vsi = NULL;
1071a835
AV
1752
1753 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
cf6c6e01 1754 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1755 goto error_param;
1756 }
1757
1758 if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
cf6c6e01 1759 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1760 goto error_param;
1761 }
1762
f1ef73f5 1763 vsi = pf->vsi[vf->lan_vsi_idx];
1071a835 1764 if (!vsi) {
cf6c6e01 1765 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1766 goto error_param;
1767 }
1768
1769 if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE) {
cf6c6e01 1770 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1771 goto error_param;
1772 }
1773
1774 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
cf6c6e01 1775 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1776 goto error_param;
1777 }
1778
cf6c6e01
MW
1779 if (ice_set_rss(vsi, NULL, vrl->lut, ICE_VSIQF_HLUT_ARRAY_SIZE))
1780 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1071a835 1781error_param:
cf6c6e01 1782 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
1071a835
AV
1783 NULL, 0);
1784}
1785
1786/**
1787 * ice_vc_get_stats_msg
1788 * @vf: pointer to the VF info
1789 * @msg: pointer to the msg buffer
1790 *
1791 * called from the VF to get VSI stats
1792 */
1793static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
1794{
cf6c6e01 1795 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1071a835
AV
1796 struct virtchnl_queue_select *vqs =
1797 (struct virtchnl_queue_select *)msg;
f1ef73f5 1798 struct ice_pf *pf = vf->pf;
1071a835
AV
1799 struct ice_eth_stats stats;
1800 struct ice_vsi *vsi;
1801
1802 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
cf6c6e01 1803 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1804 goto error_param;
1805 }
1806
1807 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
cf6c6e01 1808 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1809 goto error_param;
1810 }
1811
f1ef73f5 1812 vsi = pf->vsi[vf->lan_vsi_idx];
1071a835 1813 if (!vsi) {
cf6c6e01 1814 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1815 goto error_param;
1816 }
1817
1818 memset(&stats, 0, sizeof(struct ice_eth_stats));
1819 ice_update_eth_stats(vsi);
1820
1821 stats = vsi->eth_stats;
1822
1823error_param:
1824 /* send the response to the VF */
cf6c6e01 1825 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
1071a835
AV
1826 (u8 *)&stats, sizeof(stats));
1827}
1828
1829/**
1830 * ice_vc_ena_qs_msg
1831 * @vf: pointer to the VF info
1832 * @msg: pointer to the msg buffer
1833 *
1834 * called from the VF to enable all or specific queue(s)
1835 */
1836static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
1837{
cf6c6e01 1838 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1071a835
AV
1839 struct virtchnl_queue_select *vqs =
1840 (struct virtchnl_queue_select *)msg;
f1ef73f5 1841 struct ice_pf *pf = vf->pf;
1071a835
AV
1842 struct ice_vsi *vsi;
1843
1844 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
cf6c6e01 1845 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1846 goto error_param;
1847 }
1848
1849 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
cf6c6e01 1850 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1851 goto error_param;
1852 }
1853
1854 if (!vqs->rx_queues && !vqs->tx_queues) {
cf6c6e01 1855 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1856 goto error_param;
1857 }
1858
f1ef73f5 1859 vsi = pf->vsi[vf->lan_vsi_idx];
1071a835 1860 if (!vsi) {
cf6c6e01 1861 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1862 goto error_param;
1863 }
1864
1865 /* Enable only Rx rings, Tx rings were enabled by the FW when the
1866 * Tx queue group list was configured and the context bits were
1867 * programmed using ice_vsi_cfg_txqs
1868 */
1869 if (ice_vsi_start_rx_rings(vsi))
cf6c6e01 1870 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1871
1872 /* Set flag to indicate that queues are enabled */
cf6c6e01 1873 if (v_ret == VIRTCHNL_STATUS_SUCCESS)
1071a835
AV
1874 set_bit(ICE_VF_STATE_ENA, vf->vf_states);
1875
1876error_param:
1877 /* send the response to the VF */
cf6c6e01 1878 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
1071a835
AV
1879 NULL, 0);
1880}
1881
1882/**
1883 * ice_vc_dis_qs_msg
1884 * @vf: pointer to the VF info
1885 * @msg: pointer to the msg buffer
1886 *
1887 * called from the VF to disable all or specific
1888 * queue(s)
1889 */
1890static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
1891{
cf6c6e01 1892 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1071a835
AV
1893 struct virtchnl_queue_select *vqs =
1894 (struct virtchnl_queue_select *)msg;
f1ef73f5 1895 struct ice_pf *pf = vf->pf;
1071a835
AV
1896 struct ice_vsi *vsi;
1897
1898 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
1899 !test_bit(ICE_VF_STATE_ENA, vf->vf_states)) {
cf6c6e01 1900 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1901 goto error_param;
1902 }
1903
1904 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
cf6c6e01 1905 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1906 goto error_param;
1907 }
1908
1909 if (!vqs->rx_queues && !vqs->tx_queues) {
cf6c6e01 1910 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1911 goto error_param;
1912 }
1913
f1ef73f5 1914 vsi = pf->vsi[vf->lan_vsi_idx];
1071a835 1915 if (!vsi) {
cf6c6e01 1916 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1917 goto error_param;
1918 }
1919
03f7a986 1920 if (ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id)) {
1071a835
AV
1921 dev_err(&vsi->back->pdev->dev,
1922 "Failed to stop tx rings on VSI %d\n",
1923 vsi->vsi_num);
cf6c6e01 1924 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1925 }
1926
1927 if (ice_vsi_stop_rx_rings(vsi)) {
1928 dev_err(&vsi->back->pdev->dev,
1929 "Failed to stop rx rings on VSI %d\n",
1930 vsi->vsi_num);
cf6c6e01 1931 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1932 }
1933
1934 /* Clear enabled queues flag */
cf6c6e01 1935 if (v_ret == VIRTCHNL_STATUS_SUCCESS)
1071a835
AV
1936 clear_bit(ICE_VF_STATE_ENA, vf->vf_states);
1937
1938error_param:
1939 /* send the response to the VF */
cf6c6e01 1940 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
1071a835
AV
1941 NULL, 0);
1942}
1943
1944/**
1945 * ice_vc_cfg_irq_map_msg
1946 * @vf: pointer to the VF info
1947 * @msg: pointer to the msg buffer
1948 *
1949 * called from the VF to configure the IRQ to queue map
1950 */
1951static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
1952{
cf6c6e01 1953 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
173e23c0 1954 struct virtchnl_irq_map_info *irqmap_info;
1071a835
AV
1955 u16 vsi_id, vsi_q_id, vector_id;
1956 struct virtchnl_vector_map *map;
1071a835 1957 struct ice_pf *pf = vf->pf;
047e52c0 1958 u16 num_q_vectors_mapped;
173e23c0 1959 struct ice_vsi *vsi;
1071a835
AV
1960 unsigned long qmap;
1961 int i;
1962
173e23c0 1963 irqmap_info = (struct virtchnl_irq_map_info *)msg;
047e52c0
AV
1964 num_q_vectors_mapped = irqmap_info->num_vectors;
1965
ba0db585 1966 vsi = pf->vsi[vf->lan_vsi_idx];
047e52c0
AV
1967 if (!vsi) {
1968 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1969 goto error_param;
1970 }
ba0db585 1971
047e52c0
AV
1972 /* Check to make sure number of VF vectors mapped is not greater than
1973 * number of VF vectors originally allocated, and check that
1974 * there is actually at least a single VF queue vector mapped
1975 */
ba0db585 1976 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
047e52c0
AV
1977 pf->num_vf_msix < num_q_vectors_mapped ||
1978 !irqmap_info->num_vectors) {
cf6c6e01 1979 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
1980 goto error_param;
1981 }
1982
047e52c0
AV
1983 for (i = 0; i < num_q_vectors_mapped; i++) {
1984 struct ice_q_vector *q_vector;
ba0db585 1985
1071a835
AV
1986 map = &irqmap_info->vecmap[i];
1987
1988 vector_id = map->vector_id;
1989 vsi_id = map->vsi_id;
1990 /* validate msg params */
1991 if (!(vector_id < pf->hw.func_caps.common_cap
047e52c0
AV
1992 .num_msix_vectors) || !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
1993 (!vector_id && (map->rxq_map || map->txq_map))) {
1994 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1995 goto error_param;
1996 }
1997
1998 /* No need to map VF miscellaneous or rogue vector */
1999 if (!vector_id)
2000 continue;
2001
2002 /* Subtract non queue vector from vector_id passed by VF
2003 * to get actual number of VSI queue vector array index
2004 */
2005 q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
2006 if (!q_vector) {
cf6c6e01 2007 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2008 goto error_param;
2009 }
2010
1071a835
AV
2011 /* lookout for the invalid queue index */
2012 qmap = map->rxq_map;
ba0db585 2013 q_vector->num_ring_rx = 0;
1071a835
AV
2014 for_each_set_bit(vsi_q_id, &qmap, ICE_MAX_BASE_QS_PER_VF) {
2015 if (!ice_vc_isvalid_q_id(vf, vsi_id, vsi_q_id)) {
cf6c6e01 2016 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2017 goto error_param;
2018 }
d2b464a7
BC
2019 q_vector->num_ring_rx++;
2020 q_vector->rx.itr_idx = map->rxitr_idx;
2021 vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
047e52c0
AV
2022 ice_cfg_rxq_interrupt(vsi, vsi_q_id, vector_id,
2023 q_vector->rx.itr_idx);
1071a835
AV
2024 }
2025
2026 qmap = map->txq_map;
ba0db585 2027 q_vector->num_ring_tx = 0;
1071a835
AV
2028 for_each_set_bit(vsi_q_id, &qmap, ICE_MAX_BASE_QS_PER_VF) {
2029 if (!ice_vc_isvalid_q_id(vf, vsi_id, vsi_q_id)) {
cf6c6e01 2030 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2031 goto error_param;
2032 }
d2b464a7
BC
2033 q_vector->num_ring_tx++;
2034 q_vector->tx.itr_idx = map->txitr_idx;
2035 vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
047e52c0
AV
2036 ice_cfg_txq_interrupt(vsi, vsi_q_id, vector_id,
2037 q_vector->tx.itr_idx);
1071a835
AV
2038 }
2039 }
2040
1071a835
AV
2041error_param:
2042 /* send the response to the VF */
cf6c6e01 2043 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
1071a835
AV
2044 NULL, 0);
2045}
2046
2047/**
2048 * ice_vc_cfg_qs_msg
2049 * @vf: pointer to the VF info
2050 * @msg: pointer to the msg buffer
2051 *
2052 * called from the VF to configure the Rx/Tx queues
2053 */
2054static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
2055{
cf6c6e01 2056 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1071a835
AV
2057 struct virtchnl_vsi_queue_config_info *qci =
2058 (struct virtchnl_vsi_queue_config_info *)msg;
2059 struct virtchnl_queue_pair_info *qpi;
5743020d 2060 struct ice_pf *pf = vf->pf;
1071a835
AV
2061 struct ice_vsi *vsi;
2062 int i;
2063
2064 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
cf6c6e01 2065 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2066 goto error_param;
2067 }
2068
2069 if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
cf6c6e01 2070 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2071 goto error_param;
2072 }
2073
f1ef73f5 2074 vsi = pf->vsi[vf->lan_vsi_idx];
f24e35d8 2075 if (!vsi)
1071a835 2076 goto error_param;
1071a835 2077
5743020d
AA
2078 if (qci->num_queue_pairs > ICE_MAX_BASE_QS_PER_VF) {
2079 dev_err(&pf->pdev->dev,
2080 "VF-%d requesting more than supported number of queues: %d\n",
2081 vf->vf_id, qci->num_queue_pairs);
cf6c6e01 2082 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
5743020d
AA
2083 goto error_param;
2084 }
2085
1071a835
AV
2086 for (i = 0; i < qci->num_queue_pairs; i++) {
2087 qpi = &qci->qpair[i];
2088 if (qpi->txq.vsi_id != qci->vsi_id ||
2089 qpi->rxq.vsi_id != qci->vsi_id ||
2090 qpi->rxq.queue_id != qpi->txq.queue_id ||
2091 !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
cf6c6e01 2092 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2093 goto error_param;
2094 }
2095 /* copy Tx queue info from VF into VSI */
2096 vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
2097 vsi->tx_rings[i]->count = qpi->txq.ring_len;
df17b7e0 2098 /* copy Rx queue info from VF into VSI */
1071a835
AV
2099 vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
2100 vsi->rx_rings[i]->count = qpi->rxq.ring_len;
2101 if (qpi->rxq.databuffer_size > ((16 * 1024) - 128)) {
cf6c6e01 2102 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2103 goto error_param;
2104 }
2105 vsi->rx_buf_len = qpi->rxq.databuffer_size;
2106 if (qpi->rxq.max_pkt_size >= (16 * 1024) ||
2107 qpi->rxq.max_pkt_size < 64) {
cf6c6e01 2108 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2109 goto error_param;
2110 }
2111 vsi->max_frame = qpi->rxq.max_pkt_size;
2112 }
2113
2114 /* VF can request to configure less than allocated queues
2115 * or default allocated queues. So update the VSI with new number
2116 */
2117 vsi->num_txq = qci->num_queue_pairs;
2118 vsi->num_rxq = qci->num_queue_pairs;
105e5bc2
PB
2119 /* All queues of VF VSI are in TC 0 */
2120 vsi->tc_cfg.tc_info[0].qcount_tx = qci->num_queue_pairs;
2121 vsi->tc_cfg.tc_info[0].qcount_rx = qci->num_queue_pairs;
1071a835 2122
cf6c6e01
MW
2123 if (ice_vsi_cfg_lan_txqs(vsi) || ice_vsi_cfg_rxqs(vsi))
2124 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1071a835
AV
2125
2126error_param:
2127 /* send the response to the VF */
cf6c6e01 2128 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES, v_ret,
1071a835
AV
2129 NULL, 0);
2130}
2131
2132/**
2133 * ice_is_vf_trusted
2134 * @vf: pointer to the VF info
2135 */
2136static bool ice_is_vf_trusted(struct ice_vf *vf)
2137{
2138 return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
2139}
2140
2141/**
2142 * ice_can_vf_change_mac
2143 * @vf: pointer to the VF info
2144 *
2145 * Return true if the VF is allowed to change its MAC filters, false otherwise
2146 */
2147static bool ice_can_vf_change_mac(struct ice_vf *vf)
2148{
2149 /* If the VF MAC address has been set administratively (via the
2150 * ndo_set_vf_mac command), then deny permission to the VF to
2151 * add/delete unicast MAC addresses, unless the VF is trusted
2152 */
2153 if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
2154 return false;
2155
2156 return true;
2157}
2158
2159/**
2160 * ice_vc_handle_mac_addr_msg
2161 * @vf: pointer to the VF info
2162 * @msg: pointer to the msg buffer
f9867df6 2163 * @set: true if MAC filters are being set, false otherwise
1071a835 2164 *
df17b7e0 2165 * add guest MAC address filter
1071a835
AV
2166 */
2167static int
2168ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
2169{
cf6c6e01 2170 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1071a835
AV
2171 struct virtchnl_ether_addr_list *al =
2172 (struct virtchnl_ether_addr_list *)msg;
2173 struct ice_pf *pf = vf->pf;
2174 enum virtchnl_ops vc_op;
1071a835
AV
2175 LIST_HEAD(mac_list);
2176 struct ice_vsi *vsi;
2177 int mac_count = 0;
2178 int i;
2179
2180 if (set)
2181 vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
2182 else
2183 vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
2184
2185 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
2186 !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
cf6c6e01 2187 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2188 goto handle_mac_exit;
2189 }
2190
2191 if (set && !ice_is_vf_trusted(vf) &&
2192 (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
2193 dev_err(&pf->pdev->dev,
d84b899a
AA
2194 "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",
2195 vf->vf_id);
2196 /* There is no need to let VF know about not being trusted
2197 * to add more MAC addr, so we can just return success message.
2198 */
cf6c6e01 2199 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2200 goto handle_mac_exit;
2201 }
2202
2203 vsi = pf->vsi[vf->lan_vsi_idx];
f1ef73f5 2204 if (!vsi) {
cf6c6e01 2205 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
f1ef73f5
AA
2206 goto handle_mac_exit;
2207 }
1071a835
AV
2208
2209 for (i = 0; i < al->num_elements; i++) {
2210 u8 *maddr = al->list[i].addr;
2211
2212 if (ether_addr_equal(maddr, vf->dflt_lan_addr.addr) ||
2213 is_broadcast_ether_addr(maddr)) {
2214 if (set) {
2215 /* VF is trying to add filters that the PF
2216 * already added. Just continue.
2217 */
2218 dev_info(&pf->pdev->dev,
4e1af7bf 2219 "MAC %pM already set for VF %d\n",
1071a835
AV
2220 maddr, vf->vf_id);
2221 continue;
2222 } else {
f9867df6 2223 /* VF can't remove dflt_lan_addr/bcast MAC */
1071a835 2224 dev_err(&pf->pdev->dev,
4e1af7bf 2225 "VF can't remove default MAC address or MAC %pM programmed by PF for VF %d\n",
1071a835 2226 maddr, vf->vf_id);
4e1af7bf 2227 continue;
1071a835
AV
2228 }
2229 }
2230
2231 /* check for the invalid cases and bail if necessary */
2232 if (is_zero_ether_addr(maddr)) {
2233 dev_err(&pf->pdev->dev,
4e1af7bf 2234 "invalid MAC %pM provided for VF %d\n",
1071a835 2235 maddr, vf->vf_id);
cf6c6e01 2236 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2237 goto handle_mac_exit;
2238 }
2239
2240 if (is_unicast_ether_addr(maddr) &&
2241 !ice_can_vf_change_mac(vf)) {
2242 dev_err(&pf->pdev->dev,
4e1af7bf 2243 "can't change unicast MAC for untrusted VF %d\n",
1071a835 2244 vf->vf_id);
cf6c6e01 2245 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2246 goto handle_mac_exit;
2247 }
2248
f9867df6 2249 /* get here if maddr is multicast or if VF can change MAC */
1071a835 2250 if (ice_add_mac_to_list(vsi, &mac_list, al->list[i].addr)) {
cf6c6e01 2251 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
1071a835
AV
2252 goto handle_mac_exit;
2253 }
2254 mac_count++;
2255 }
2256
2257 /* program the updated filter list */
2258 if (set)
cf6c6e01 2259 v_ret = ice_err_to_virt_err(ice_add_mac(&pf->hw, &mac_list));
1071a835 2260 else
cf6c6e01 2261 v_ret = ice_err_to_virt_err(ice_remove_mac(&pf->hw, &mac_list));
1071a835 2262
cf6c6e01 2263 if (v_ret) {
1071a835 2264 dev_err(&pf->pdev->dev,
4e1af7bf 2265 "can't update MAC filters for VF %d, error %d\n",
cf6c6e01 2266 vf->vf_id, v_ret);
1071a835
AV
2267 } else {
2268 if (set)
2269 vf->num_mac += mac_count;
2270 else
2271 vf->num_mac -= mac_count;
2272 }
2273
2274handle_mac_exit:
2275 ice_free_fltr_list(&pf->pdev->dev, &mac_list);
2276 /* send the response to the VF */
cf6c6e01 2277 return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
1071a835
AV
2278}
2279
2280/**
2281 * ice_vc_add_mac_addr_msg
2282 * @vf: pointer to the VF info
2283 * @msg: pointer to the msg buffer
2284 *
2285 * add guest MAC address filter
2286 */
2287static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2288{
2289 return ice_vc_handle_mac_addr_msg(vf, msg, true);
2290}
2291
2292/**
2293 * ice_vc_del_mac_addr_msg
2294 * @vf: pointer to the VF info
2295 * @msg: pointer to the msg buffer
2296 *
2297 * remove guest MAC address filter
2298 */
2299static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2300{
2301 return ice_vc_handle_mac_addr_msg(vf, msg, false);
2302}
2303
2304/**
2305 * ice_vc_request_qs_msg
2306 * @vf: pointer to the VF info
2307 * @msg: pointer to the msg buffer
2308 *
2309 * VFs get a default number of queues but can use this message to request a
df17b7e0 2310 * different number. If the request is successful, PF will reset the VF and
1071a835 2311 * return 0. If unsuccessful, PF will send message informing VF of number of
f9867df6 2312 * available queue pairs via virtchnl message response to VF.
1071a835
AV
2313 */
2314static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
2315{
cf6c6e01 2316 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1071a835
AV
2317 struct virtchnl_vf_res_request *vfres =
2318 (struct virtchnl_vf_res_request *)msg;
2319 int req_queues = vfres->num_queue_pairs;
1071a835 2320 struct ice_pf *pf = vf->pf;
5743020d 2321 int max_allowed_vf_queues;
1071a835
AV
2322 int tx_rx_queue_left;
2323 int cur_queues;
2324
2325 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
cf6c6e01 2326 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2327 goto error_param;
2328 }
2329
5743020d 2330 cur_queues = vf->num_vf_qs;
1071a835 2331 tx_rx_queue_left = min_t(int, pf->q_left_tx, pf->q_left_rx);
5743020d 2332 max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
1071a835
AV
2333 if (req_queues <= 0) {
2334 dev_err(&pf->pdev->dev,
df17b7e0 2335 "VF %d tried to request %d queues. Ignoring.\n",
1071a835 2336 vf->vf_id, req_queues);
5743020d 2337 } else if (req_queues > ICE_MAX_BASE_QS_PER_VF) {
1071a835
AV
2338 dev_err(&pf->pdev->dev,
2339 "VF %d tried to request more than %d queues.\n",
5743020d
AA
2340 vf->vf_id, ICE_MAX_BASE_QS_PER_VF);
2341 vfres->num_queue_pairs = ICE_MAX_BASE_QS_PER_VF;
1071a835
AV
2342 } else if (req_queues - cur_queues > tx_rx_queue_left) {
2343 dev_warn(&pf->pdev->dev,
2344 "VF %d requested %d more queues, but only %d left.\n",
2345 vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
5743020d
AA
2346 vfres->num_queue_pairs = min_t(int, max_allowed_vf_queues,
2347 ICE_MAX_BASE_QS_PER_VF);
1071a835
AV
2348 } else {
2349 /* request is successful, then reset VF */
2350 vf->num_req_qs = req_queues;
2351 ice_vc_dis_vf(vf);
2352 dev_info(&pf->pdev->dev,
2353 "VF %d granted request of %d queues.\n",
2354 vf->vf_id, req_queues);
2355 return 0;
2356 }
2357
2358error_param:
2359 /* send the response to the VF */
2360 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
cf6c6e01 2361 v_ret, (u8 *)vfres, sizeof(*vfres));
1071a835
AV
2362}
2363
7c710869
AV
2364/**
2365 * ice_set_vf_port_vlan
2366 * @netdev: network interface device structure
2367 * @vf_id: VF identifier
f9867df6 2368 * @vlan_id: VLAN ID being set
7c710869
AV
2369 * @qos: priority setting
2370 * @vlan_proto: VLAN protocol
2371 *
f9867df6 2372 * program VF Port VLAN ID and/or QoS
7c710869
AV
2373 */
2374int
2375ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
2376 __be16 vlan_proto)
2377{
2378 u16 vlanprio = vlan_id | (qos << ICE_VLAN_PRIORITY_S);
2379 struct ice_netdev_priv *np = netdev_priv(netdev);
2380 struct ice_pf *pf = np->vsi->back;
2381 struct ice_vsi *vsi;
2382 struct ice_vf *vf;
2383 int ret = 0;
2384
2385 /* validate the request */
2386 if (vf_id >= pf->num_alloc_vfs) {
2387 dev_err(&pf->pdev->dev, "invalid VF id: %d\n", vf_id);
2388 return -EINVAL;
2389 }
2390
2391 if (vlan_id > ICE_MAX_VLANID || qos > 7) {
2392 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2393 return -EINVAL;
2394 }
2395
2396 if (vlan_proto != htons(ETH_P_8021Q)) {
2397 dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
2398 return -EPROTONOSUPPORT;
2399 }
2400
2401 vf = &pf->vf[vf_id];
2402 vsi = pf->vsi[vf->lan_vsi_idx];
2403 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
2404 dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
2405 return -EBUSY;
2406 }
2407
2408 if (le16_to_cpu(vsi->info.pvid) == vlanprio) {
2409 /* duplicate request, so just return success */
2410 dev_info(&pf->pdev->dev,
2411 "Duplicate pvid %d request\n", vlanprio);
2412 return ret;
2413 }
2414
f9867df6 2415 /* If PVID, then remove all filters on the old VLAN */
7c710869
AV
2416 if (vsi->info.pvid)
2417 ice_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2418 VLAN_VID_MASK));
2419
2420 if (vlan_id || qos) {
77a7a84d 2421 ret = ice_vsi_manage_pvid(vsi, vlanprio, true);
7c710869
AV
2422 if (ret)
2423 goto error_set_pvid;
2424 } else {
77a7a84d
MS
2425 ice_vsi_manage_pvid(vsi, 0, false);
2426 vsi->info.pvid = 0;
7c710869
AV
2427 }
2428
2429 if (vlan_id) {
2430 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2431 vlan_id, qos, vf_id);
2432
2433 /* add new VLAN filter for each MAC */
2434 ret = ice_vsi_add_vlan(vsi, vlan_id);
2435 if (ret)
2436 goto error_set_pvid;
2437 }
2438
2439 /* The Port VLAN needs to be saved across resets the same as the
2440 * default LAN MAC address.
2441 */
2442 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
2443
2444error_set_pvid:
2445 return ret;
2446}
2447
1071a835
AV
2448/**
2449 * ice_vc_process_vlan_msg
2450 * @vf: pointer to the VF info
2451 * @msg: pointer to the msg buffer
2452 * @add_v: Add VLAN if true, otherwise delete VLAN
2453 *
f9867df6 2454 * Process virtchnl op to add or remove programmed guest VLAN ID
1071a835
AV
2455 */
2456static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
2457{
cf6c6e01 2458 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1071a835
AV
2459 struct virtchnl_vlan_filter_list *vfl =
2460 (struct virtchnl_vlan_filter_list *)msg;
1071a835 2461 struct ice_pf *pf = vf->pf;
5eda8afd 2462 bool vlan_promisc = false;
1071a835 2463 struct ice_vsi *vsi;
5eda8afd
AA
2464 struct ice_hw *hw;
2465 int status = 0;
2466 u8 promisc_m;
1071a835
AV
2467 int i;
2468
2469 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
cf6c6e01 2470 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2471 goto error_param;
2472 }
2473
2474 if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
cf6c6e01 2475 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2476 goto error_param;
2477 }
2478
2479 if (add_v && !ice_is_vf_trusted(vf) &&
2480 vf->num_vlan >= ICE_MAX_VLAN_PER_VF) {
2481 dev_info(&pf->pdev->dev,
d84b899a
AA
2482 "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2483 vf->vf_id);
5eda8afd
AA
2484 /* There is no need to let VF know about being not trusted,
2485 * so we can just return success message here
2486 */
1071a835
AV
2487 goto error_param;
2488 }
2489
2490 for (i = 0; i < vfl->num_elements; i++) {
2491 if (vfl->vlan_id[i] > ICE_MAX_VLANID) {
cf6c6e01 2492 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2493 dev_err(&pf->pdev->dev,
2494 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
2495 goto error_param;
2496 }
2497 }
2498
5eda8afd 2499 hw = &pf->hw;
f1ef73f5 2500 vsi = pf->vsi[vf->lan_vsi_idx];
1071a835 2501 if (!vsi) {
cf6c6e01 2502 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2503 goto error_param;
2504 }
2505
2506 if (vsi->info.pvid) {
cf6c6e01 2507 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2508 goto error_param;
2509 }
2510
2511 if (ice_vsi_manage_vlan_stripping(vsi, add_v)) {
2512 dev_err(&pf->pdev->dev,
2513 "%sable VLAN stripping failed for VSI %i\n",
2514 add_v ? "en" : "dis", vsi->vsi_num);
cf6c6e01 2515 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2516 goto error_param;
2517 }
2518
5eda8afd
AA
2519 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2520 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
2521 vlan_promisc = true;
2522
1071a835
AV
2523 if (add_v) {
2524 for (i = 0; i < vfl->num_elements; i++) {
2525 u16 vid = vfl->vlan_id[i];
2526
5079b853
AA
2527 if (!ice_is_vf_trusted(vf) &&
2528 vf->num_vlan >= ICE_MAX_VLAN_PER_VF) {
2529 dev_info(&pf->pdev->dev,
2530 "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2531 vf->vf_id);
2532 /* There is no need to let VF know about being
2533 * not trusted, so we can just return success
2534 * message here as well.
2535 */
2536 goto error_param;
2537 }
2538
5eda8afd 2539 if (ice_vsi_add_vlan(vsi, vid)) {
cf6c6e01 2540 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
5eda8afd
AA
2541 goto error_param;
2542 }
1071a835 2543
5eda8afd
AA
2544 vf->num_vlan++;
2545 /* Enable VLAN pruning when VLAN is added */
2546 if (!vlan_promisc) {
2547 status = ice_cfg_vlan_pruning(vsi, true, false);
2548 if (status) {
cf6c6e01 2549 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
5eda8afd
AA
2550 dev_err(&pf->pdev->dev,
2551 "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
2552 vid, status);
2553 goto error_param;
2554 }
1071a835 2555 } else {
5eda8afd
AA
2556 /* Enable Ucast/Mcast VLAN promiscuous mode */
2557 promisc_m = ICE_PROMISC_VLAN_TX |
2558 ICE_PROMISC_VLAN_RX;
2559
2560 status = ice_set_vsi_promisc(hw, vsi->idx,
2561 promisc_m, vid);
cf6c6e01
MW
2562 if (status) {
2563 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
5eda8afd
AA
2564 dev_err(&pf->pdev->dev,
2565 "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
2566 vid, status);
cf6c6e01 2567 }
1071a835
AV
2568 }
2569 }
2570 } else {
bb877b22
AA
2571 /* In case of non_trusted VF, number of VLAN elements passed
2572 * to PF for removal might be greater than number of VLANs
2573 * filter programmed for that VF - So, use actual number of
2574 * VLANS added earlier with add VLAN opcode. In order to avoid
2575 * removing VLAN that doesn't exist, which result to sending
2576 * erroneous failed message back to the VF
2577 */
2578 int num_vf_vlan;
2579
2580 num_vf_vlan = vf->num_vlan;
2581 for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
1071a835
AV
2582 u16 vid = vfl->vlan_id[i];
2583
2584 /* Make sure ice_vsi_kill_vlan is successful before
2585 * updating VLAN information
2586 */
5eda8afd 2587 if (ice_vsi_kill_vlan(vsi, vid)) {
cf6c6e01 2588 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
5eda8afd
AA
2589 goto error_param;
2590 }
2591
2592 vf->num_vlan--;
2593 /* Disable VLAN pruning when removing VLAN */
2594 ice_cfg_vlan_pruning(vsi, false, false);
2595
2596 /* Disable Unicast/Multicast VLAN promiscuous mode */
2597 if (vlan_promisc) {
2598 promisc_m = ICE_PROMISC_VLAN_TX |
2599 ICE_PROMISC_VLAN_RX;
1071a835 2600
5eda8afd
AA
2601 ice_clear_vsi_promisc(hw, vsi->idx,
2602 promisc_m, vid);
1071a835
AV
2603 }
2604 }
2605 }
2606
2607error_param:
2608 /* send the response to the VF */
2609 if (add_v)
cf6c6e01 2610 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
1071a835
AV
2611 NULL, 0);
2612 else
cf6c6e01 2613 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
1071a835
AV
2614 NULL, 0);
2615}
2616
2617/**
2618 * ice_vc_add_vlan_msg
2619 * @vf: pointer to the VF info
2620 * @msg: pointer to the msg buffer
2621 *
f9867df6 2622 * Add and program guest VLAN ID
1071a835
AV
2623 */
2624static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
2625{
2626 return ice_vc_process_vlan_msg(vf, msg, true);
2627}
2628
2629/**
2630 * ice_vc_remove_vlan_msg
2631 * @vf: pointer to the VF info
2632 * @msg: pointer to the msg buffer
2633 *
f9867df6 2634 * remove programmed guest VLAN ID
1071a835
AV
2635 */
2636static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
2637{
2638 return ice_vc_process_vlan_msg(vf, msg, false);
2639}
2640
2641/**
2642 * ice_vc_ena_vlan_stripping
2643 * @vf: pointer to the VF info
2644 *
2645 * Enable VLAN header stripping for a given VF
2646 */
2647static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
2648{
cf6c6e01 2649 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1071a835
AV
2650 struct ice_pf *pf = vf->pf;
2651 struct ice_vsi *vsi;
2652
2653 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
cf6c6e01 2654 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2655 goto error_param;
2656 }
2657
2658 vsi = pf->vsi[vf->lan_vsi_idx];
2659 if (ice_vsi_manage_vlan_stripping(vsi, true))
cf6c6e01 2660 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2661
2662error_param:
2663 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
cf6c6e01 2664 v_ret, NULL, 0);
1071a835
AV
2665}
2666
2667/**
2668 * ice_vc_dis_vlan_stripping
2669 * @vf: pointer to the VF info
2670 *
2671 * Disable VLAN header stripping for a given VF
2672 */
2673static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
2674{
cf6c6e01 2675 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1071a835
AV
2676 struct ice_pf *pf = vf->pf;
2677 struct ice_vsi *vsi;
2678
2679 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
cf6c6e01 2680 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2681 goto error_param;
2682 }
2683
2684 vsi = pf->vsi[vf->lan_vsi_idx];
f1ef73f5 2685 if (!vsi) {
cf6c6e01 2686 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
f1ef73f5
AA
2687 goto error_param;
2688 }
2689
1071a835 2690 if (ice_vsi_manage_vlan_stripping(vsi, false))
cf6c6e01 2691 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071a835
AV
2692
2693error_param:
2694 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
cf6c6e01 2695 v_ret, NULL, 0);
1071a835
AV
2696}
2697
2698/**
2699 * ice_vc_process_vf_msg - Process request from VF
2700 * @pf: pointer to the PF structure
2701 * @event: pointer to the AQ event
2702 *
2703 * called from the common asq/arq handler to
2704 * process request from VF
2705 */
2706void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
2707{
2708 u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
2709 s16 vf_id = le16_to_cpu(event->desc.retval);
2710 u16 msglen = event->msg_len;
2711 u8 *msg = event->msg_buf;
2712 struct ice_vf *vf = NULL;
2713 int err = 0;
2714
2715 if (vf_id >= pf->num_alloc_vfs) {
2716 err = -EINVAL;
2717 goto error_handler;
2718 }
2719
2720 vf = &pf->vf[vf_id];
2721
2722 /* Check if VF is disabled. */
2723 if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
2724 err = -EPERM;
2725 goto error_handler;
2726 }
2727
2728 /* Perform basic checks on the msg */
2729 err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
2730 if (err) {
cf6c6e01 2731 if (err == VIRTCHNL_STATUS_ERR_PARAM)
1071a835
AV
2732 err = -EPERM;
2733 else
2734 err = -EINVAL;
2735 goto error_handler;
2736 }
2737
2738 /* Perform additional checks specific to RSS and Virtchnl */
2739 if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_KEY) {
2740 struct virtchnl_rss_key *vrk = (struct virtchnl_rss_key *)msg;
2741
2742 if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE)
2743 err = -EINVAL;
2744 } else if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_LUT) {
2745 struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
2746
2747 if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE)
2748 err = -EINVAL;
2749 }
2750
2751error_handler:
2752 if (err) {
cf6c6e01
MW
2753 ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
2754 NULL, 0);
1071a835
AV
2755 dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
2756 vf_id, v_opcode, msglen, err);
2757 return;
2758 }
2759
2760 switch (v_opcode) {
2761 case VIRTCHNL_OP_VERSION:
2762 err = ice_vc_get_ver_msg(vf, msg);
2763 break;
2764 case VIRTCHNL_OP_GET_VF_RESOURCES:
2765 err = ice_vc_get_vf_res_msg(vf, msg);
2766 break;
2767 case VIRTCHNL_OP_RESET_VF:
2768 ice_vc_reset_vf_msg(vf);
2769 break;
2770 case VIRTCHNL_OP_ADD_ETH_ADDR:
2771 err = ice_vc_add_mac_addr_msg(vf, msg);
2772 break;
2773 case VIRTCHNL_OP_DEL_ETH_ADDR:
2774 err = ice_vc_del_mac_addr_msg(vf, msg);
2775 break;
2776 case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
2777 err = ice_vc_cfg_qs_msg(vf, msg);
2778 break;
2779 case VIRTCHNL_OP_ENABLE_QUEUES:
2780 err = ice_vc_ena_qs_msg(vf, msg);
2781 ice_vc_notify_vf_link_state(vf);
2782 break;
2783 case VIRTCHNL_OP_DISABLE_QUEUES:
2784 err = ice_vc_dis_qs_msg(vf, msg);
2785 break;
2786 case VIRTCHNL_OP_REQUEST_QUEUES:
2787 err = ice_vc_request_qs_msg(vf, msg);
2788 break;
2789 case VIRTCHNL_OP_CONFIG_IRQ_MAP:
2790 err = ice_vc_cfg_irq_map_msg(vf, msg);
2791 break;
2792 case VIRTCHNL_OP_CONFIG_RSS_KEY:
2793 err = ice_vc_config_rss_key(vf, msg);
2794 break;
2795 case VIRTCHNL_OP_CONFIG_RSS_LUT:
2796 err = ice_vc_config_rss_lut(vf, msg);
2797 break;
2798 case VIRTCHNL_OP_GET_STATS:
2799 err = ice_vc_get_stats_msg(vf, msg);
2800 break;
2801 case VIRTCHNL_OP_ADD_VLAN:
2802 err = ice_vc_add_vlan_msg(vf, msg);
2803 break;
2804 case VIRTCHNL_OP_DEL_VLAN:
2805 err = ice_vc_remove_vlan_msg(vf, msg);
2806 break;
2807 case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
2808 err = ice_vc_ena_vlan_stripping(vf);
2809 break;
2810 case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
2811 err = ice_vc_dis_vlan_stripping(vf);
2812 break;
2813 case VIRTCHNL_OP_UNKNOWN:
2814 default:
2815 dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
2816 v_opcode, vf_id);
cf6c6e01
MW
2817 err = ice_vc_send_msg_to_vf(vf, v_opcode,
2818 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
1071a835
AV
2819 NULL, 0);
2820 break;
2821 }
2822 if (err) {
2823 /* Helper function cares less about error return values here
2824 * as it is busy with pending work.
2825 */
2826 dev_info(&pf->pdev->dev,
91dab5d5 2827 "PF failed to honor VF %d, opcode %d, error %d\n",
1071a835
AV
2828 vf_id, v_opcode, err);
2829 }
2830}
2831
7c710869
AV
2832/**
2833 * ice_get_vf_cfg
2834 * @netdev: network interface device structure
2835 * @vf_id: VF identifier
2836 * @ivi: VF configuration structure
2837 *
2838 * return VF configuration
2839 */
c8b7abdd
BA
2840int
2841ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
7c710869
AV
2842{
2843 struct ice_netdev_priv *np = netdev_priv(netdev);
2844 struct ice_vsi *vsi = np->vsi;
2845 struct ice_pf *pf = vsi->back;
2846 struct ice_vf *vf;
2847
2848 /* validate the request */
2849 if (vf_id >= pf->num_alloc_vfs) {
2850 netdev_err(netdev, "invalid VF id: %d\n", vf_id);
2851 return -EINVAL;
2852 }
2853
2854 vf = &pf->vf[vf_id];
2855 vsi = pf->vsi[vf->lan_vsi_idx];
2856
2857 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
2858 netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
2859 return -EBUSY;
2860 }
2861
2862 ivi->vf = vf_id;
2863 ether_addr_copy(ivi->mac, vf->dflt_lan_addr.addr);
2864
2865 /* VF configuration for VLAN and applicable QoS */
2866 ivi->vlan = le16_to_cpu(vsi->info.pvid) & ICE_VLAN_M;
2867 ivi->qos = (le16_to_cpu(vsi->info.pvid) & ICE_PRIORITY_M) >>
2868 ICE_VLAN_PRIORITY_S;
2869
2870 ivi->trusted = vf->trusted;
2871 ivi->spoofchk = vf->spoofchk;
2872 if (!vf->link_forced)
2873 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
2874 else if (vf->link_up)
2875 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
2876 else
2877 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
2878 ivi->max_tx_rate = vf->tx_rate;
2879 ivi->min_tx_rate = 0;
2880 return 0;
2881}
2882
2883/**
2884 * ice_set_vf_spoofchk
2885 * @netdev: network interface device structure
2886 * @vf_id: VF identifier
2887 * @ena: flag to enable or disable feature
2888 *
2889 * Enable or disable VF spoof checking
2890 */
2891int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
2892{
2893 struct ice_netdev_priv *np = netdev_priv(netdev);
7c710869
AV
2894 struct ice_vsi *vsi = np->vsi;
2895 struct ice_pf *pf = vsi->back;
198a666a
BA
2896 struct ice_vsi_ctx *ctx;
2897 enum ice_status status;
7c710869 2898 struct ice_vf *vf;
198a666a 2899 int ret = 0;
7c710869
AV
2900
2901 /* validate the request */
2902 if (vf_id >= pf->num_alloc_vfs) {
2903 netdev_err(netdev, "invalid VF id: %d\n", vf_id);
2904 return -EINVAL;
2905 }
2906
2907 vf = &pf->vf[vf_id];
2908 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
2909 netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
2910 return -EBUSY;
2911 }
2912
2913 if (ena == vf->spoofchk) {
2914 dev_dbg(&pf->pdev->dev, "VF spoofchk already %s\n",
2915 ena ? "ON" : "OFF");
2916 return 0;
2917 }
2918
198a666a
BA
2919 ctx = devm_kzalloc(&pf->pdev->dev, sizeof(*ctx), GFP_KERNEL);
2920 if (!ctx)
2921 return -ENOMEM;
2922
2923 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
7c710869
AV
2924
2925 if (ena) {
198a666a
BA
2926 ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
2927 ctx->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_PRUNE_EN_M;
7c710869
AV
2928 }
2929
198a666a 2930 status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
7c710869
AV
2931 if (status) {
2932 dev_dbg(&pf->pdev->dev,
2933 "Error %d, failed to update VSI* parameters\n", status);
198a666a
BA
2934 ret = -EIO;
2935 goto out;
7c710869
AV
2936 }
2937
2938 vf->spoofchk = ena;
198a666a
BA
2939 vsi->info.sec_flags = ctx->info.sec_flags;
2940 vsi->info.sw_flags2 = ctx->info.sw_flags2;
2941out:
2942 devm_kfree(&pf->pdev->dev, ctx);
2943 return ret;
7c710869
AV
2944}
2945
2946/**
2947 * ice_set_vf_mac
2948 * @netdev: network interface device structure
2949 * @vf_id: VF identifier
f9867df6 2950 * @mac: MAC address
7c710869 2951 *
f9867df6 2952 * program VF MAC address
7c710869
AV
2953 */
2954int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
2955{
2956 struct ice_netdev_priv *np = netdev_priv(netdev);
2957 struct ice_vsi *vsi = np->vsi;
2958 struct ice_pf *pf = vsi->back;
2959 struct ice_vf *vf;
2960 int ret = 0;
2961
2962 /* validate the request */
2963 if (vf_id >= pf->num_alloc_vfs) {
2964 netdev_err(netdev, "invalid VF id: %d\n", vf_id);
2965 return -EINVAL;
2966 }
2967
2968 vf = &pf->vf[vf_id];
2969 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
2970 netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
2971 return -EBUSY;
2972 }
2973
2974 if (is_zero_ether_addr(mac) || is_multicast_ether_addr(mac)) {
2975 netdev_err(netdev, "%pM not a valid unicast address\n", mac);
2976 return -EINVAL;
2977 }
2978
f9867df6 2979 /* copy MAC into dflt_lan_addr and trigger a VF reset. The reset
7c710869
AV
2980 * flow will use the updated dflt_lan_addr and add a MAC filter
2981 * using ice_add_mac. Also set pf_set_mac to indicate that the PF has
2982 * set the MAC address for this VF.
2983 */
2984 ether_addr_copy(vf->dflt_lan_addr.addr, mac);
2985 vf->pf_set_mac = true;
2986 netdev_info(netdev,
4e1af7bf 2987 "MAC on VF %d set to %pM. VF driver will be reinitialized\n",
7c710869
AV
2988 vf_id, mac);
2989
2990 ice_vc_dis_vf(vf);
2991 return ret;
2992}
2993
2994/**
2995 * ice_set_vf_trust
2996 * @netdev: network interface device structure
2997 * @vf_id: VF identifier
2998 * @trusted: Boolean value to enable/disable trusted VF
2999 *
3000 * Enable or disable a given VF as trusted
3001 */
3002int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
3003{
3004 struct ice_netdev_priv *np = netdev_priv(netdev);
3005 struct ice_vsi *vsi = np->vsi;
3006 struct ice_pf *pf = vsi->back;
3007 struct ice_vf *vf;
3008
3009 /* validate the request */
3010 if (vf_id >= pf->num_alloc_vfs) {
3011 dev_err(&pf->pdev->dev, "invalid VF id: %d\n", vf_id);
3012 return -EINVAL;
3013 }
3014
3015 vf = &pf->vf[vf_id];
3016 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
3017 dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
3018 return -EBUSY;
3019 }
3020
3021 /* Check if already trusted */
3022 if (trusted == vf->trusted)
3023 return 0;
3024
3025 vf->trusted = trusted;
3026 ice_vc_dis_vf(vf);
3027 dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
3028 vf_id, trusted ? "" : "un");
3029
3030 return 0;
3031}
3032
3033/**
3034 * ice_set_vf_link_state
3035 * @netdev: network interface device structure
3036 * @vf_id: VF identifier
3037 * @link_state: required link state
3038 *
3039 * Set VF's link state, irrespective of physical link state status
3040 */
3041int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
3042{
3043 struct ice_netdev_priv *np = netdev_priv(netdev);
3044 struct ice_pf *pf = np->vsi->back;
3045 struct virtchnl_pf_event pfe = { 0 };
3046 struct ice_link_status *ls;
3047 struct ice_vf *vf;
3048 struct ice_hw *hw;
3049
3050 if (vf_id >= pf->num_alloc_vfs) {
3051 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3052 return -EINVAL;
3053 }
3054
3055 vf = &pf->vf[vf_id];
3056 hw = &pf->hw;
3057 ls = &pf->hw.port_info->phy.link_info;
3058
3059 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
3060 dev_err(&pf->pdev->dev, "vf %d in reset. Try again.\n", vf_id);
3061 return -EBUSY;
3062 }
3063
3064 pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
3065 pfe.severity = PF_EVENT_SEVERITY_INFO;
3066
3067 switch (link_state) {
3068 case IFLA_VF_LINK_STATE_AUTO:
3069 vf->link_forced = false;
3070 vf->link_up = ls->link_info & ICE_AQ_LINK_UP;
3071 break;
3072 case IFLA_VF_LINK_STATE_ENABLE:
3073 vf->link_forced = true;
3074 vf->link_up = true;
3075 break;
3076 case IFLA_VF_LINK_STATE_DISABLE:
3077 vf->link_forced = true;
3078 vf->link_up = false;
3079 break;
3080 default:
3081 return -EINVAL;
3082 }
3083
3084 if (vf->link_forced)
3085 ice_set_pfe_link_forced(vf, &pfe, vf->link_up);
3086 else
3087 ice_set_pfe_link(vf, &pfe, ls->link_speed, vf->link_up);
3088
3089 /* Notify the VF of its new link state */
cf6c6e01
MW
3090 ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
3091 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,
7c710869
AV
3092 sizeof(pfe), NULL);
3093
3094 return 0;
3095}