1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
8 #include "ice_dcb_lib.h"
10 #include "ice_eswitch.h"
11 #include "ice_virtchnl_allowlist.h"
12 #include "ice_flex_pipe.h"
14 #define FIELD_SELECTOR(proto_hdr_field) \
15 BIT((proto_hdr_field) & PROTO_HDR_FIELD_MASK)
17 struct ice_vc_hdr_match_type {
18 u32 vc_hdr; /* virtchnl headers (VIRTCHNL_PROTO_HDR_XXX) */
19 u32 ice_hdr; /* ice headers (ICE_FLOW_SEG_HDR_XXX) */
22 static const struct ice_vc_hdr_match_type ice_vc_hdr_list[] = {
23 {VIRTCHNL_PROTO_HDR_NONE, ICE_FLOW_SEG_HDR_NONE},
24 {VIRTCHNL_PROTO_HDR_ETH, ICE_FLOW_SEG_HDR_ETH},
25 {VIRTCHNL_PROTO_HDR_S_VLAN, ICE_FLOW_SEG_HDR_VLAN},
26 {VIRTCHNL_PROTO_HDR_C_VLAN, ICE_FLOW_SEG_HDR_VLAN},
27 {VIRTCHNL_PROTO_HDR_IPV4, ICE_FLOW_SEG_HDR_IPV4 |
28 ICE_FLOW_SEG_HDR_IPV_OTHER},
29 {VIRTCHNL_PROTO_HDR_IPV6, ICE_FLOW_SEG_HDR_IPV6 |
30 ICE_FLOW_SEG_HDR_IPV_OTHER},
31 {VIRTCHNL_PROTO_HDR_TCP, ICE_FLOW_SEG_HDR_TCP},
32 {VIRTCHNL_PROTO_HDR_UDP, ICE_FLOW_SEG_HDR_UDP},
33 {VIRTCHNL_PROTO_HDR_SCTP, ICE_FLOW_SEG_HDR_SCTP},
34 {VIRTCHNL_PROTO_HDR_PPPOE, ICE_FLOW_SEG_HDR_PPPOE},
35 {VIRTCHNL_PROTO_HDR_GTPU_IP, ICE_FLOW_SEG_HDR_GTPU_IP},
36 {VIRTCHNL_PROTO_HDR_GTPU_EH, ICE_FLOW_SEG_HDR_GTPU_EH},
37 {VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_DWN,
38 ICE_FLOW_SEG_HDR_GTPU_DWN},
39 {VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_UP,
40 ICE_FLOW_SEG_HDR_GTPU_UP},
41 {VIRTCHNL_PROTO_HDR_L2TPV3, ICE_FLOW_SEG_HDR_L2TPV3},
42 {VIRTCHNL_PROTO_HDR_ESP, ICE_FLOW_SEG_HDR_ESP},
43 {VIRTCHNL_PROTO_HDR_AH, ICE_FLOW_SEG_HDR_AH},
44 {VIRTCHNL_PROTO_HDR_PFCP, ICE_FLOW_SEG_HDR_PFCP_SESSION},
47 struct ice_vc_hash_field_match_type {
48 u32 vc_hdr; /* virtchnl headers
49 * (VIRTCHNL_PROTO_HDR_XXX)
51 u32 vc_hash_field; /* virtchnl hash fields selector
52 * FIELD_SELECTOR((VIRTCHNL_PROTO_HDR_ETH_XXX))
54 u64 ice_hash_field; /* ice hash fields
55 * (BIT_ULL(ICE_FLOW_FIELD_IDX_XXX))
60 ice_vc_hash_field_match_type ice_vc_hash_field_list[] = {
61 {VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC),
62 BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_SA)},
63 {VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
64 BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_DA)},
65 {VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC) |
66 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
68 {VIRTCHNL_PROTO_HDR_ETH,
69 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_ETHERTYPE),
70 BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_TYPE)},
71 {VIRTCHNL_PROTO_HDR_S_VLAN,
72 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_S_VLAN_ID),
73 BIT_ULL(ICE_FLOW_FIELD_IDX_S_VLAN)},
74 {VIRTCHNL_PROTO_HDR_C_VLAN,
75 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_C_VLAN_ID),
76 BIT_ULL(ICE_FLOW_FIELD_IDX_C_VLAN)},
77 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC),
78 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA)},
79 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
80 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA)},
81 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
82 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
84 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
85 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
86 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA) |
87 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
88 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
89 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
90 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA) |
91 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
92 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
93 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
94 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
95 ICE_FLOW_HASH_IPV4 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
96 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
97 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
98 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC),
99 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA)},
100 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
101 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA)},
102 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
103 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
105 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
106 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
107 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA) |
108 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
109 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
110 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
111 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA) |
112 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
113 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
114 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
115 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
116 ICE_FLOW_HASH_IPV6 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
117 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
118 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
119 {VIRTCHNL_PROTO_HDR_TCP,
120 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT),
121 BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_SRC_PORT)},
122 {VIRTCHNL_PROTO_HDR_TCP,
123 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
124 BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_DST_PORT)},
125 {VIRTCHNL_PROTO_HDR_TCP,
126 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT) |
127 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
128 ICE_FLOW_HASH_TCP_PORT},
129 {VIRTCHNL_PROTO_HDR_UDP,
130 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT),
131 BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_SRC_PORT)},
132 {VIRTCHNL_PROTO_HDR_UDP,
133 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
134 BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_DST_PORT)},
135 {VIRTCHNL_PROTO_HDR_UDP,
136 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT) |
137 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
138 ICE_FLOW_HASH_UDP_PORT},
139 {VIRTCHNL_PROTO_HDR_SCTP,
140 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT),
141 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT)},
142 {VIRTCHNL_PROTO_HDR_SCTP,
143 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
144 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_DST_PORT)},
145 {VIRTCHNL_PROTO_HDR_SCTP,
146 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT) |
147 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
148 ICE_FLOW_HASH_SCTP_PORT},
149 {VIRTCHNL_PROTO_HDR_PPPOE,
150 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PPPOE_SESS_ID),
151 BIT_ULL(ICE_FLOW_FIELD_IDX_PPPOE_SESS_ID)},
152 {VIRTCHNL_PROTO_HDR_GTPU_IP,
153 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_GTPU_IP_TEID),
154 BIT_ULL(ICE_FLOW_FIELD_IDX_GTPU_IP_TEID)},
155 {VIRTCHNL_PROTO_HDR_L2TPV3,
156 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_L2TPV3_SESS_ID),
157 BIT_ULL(ICE_FLOW_FIELD_IDX_L2TPV3_SESS_ID)},
158 {VIRTCHNL_PROTO_HDR_ESP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ESP_SPI),
159 BIT_ULL(ICE_FLOW_FIELD_IDX_ESP_SPI)},
160 {VIRTCHNL_PROTO_HDR_AH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_AH_SPI),
161 BIT_ULL(ICE_FLOW_FIELD_IDX_AH_SPI)},
162 {VIRTCHNL_PROTO_HDR_PFCP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PFCP_SEID),
163 BIT_ULL(ICE_FLOW_FIELD_IDX_PFCP_SEID)},
167 * ice_get_vf_vsi - get VF's VSI based on the stored index
168 * @vf: VF used to get VSI
170 struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf)
172 return vf->pf->vsi[vf->lan_vsi_idx];
176 * ice_validate_vf_id - helper to check if VF ID is valid
177 * @pf: pointer to the PF structure
178 * @vf_id: the ID of the VF to check
180 static int ice_validate_vf_id(struct ice_pf *pf, u16 vf_id)
182 /* vf_id range is only valid for 0-255, and should always be unsigned */
183 if (vf_id >= pf->num_alloc_vfs) {
184 dev_err(ice_pf_to_dev(pf), "Invalid VF ID: %u\n", vf_id);
191 * ice_check_vf_init - helper to check if VF init complete
192 * @pf: pointer to the PF structure
193 * @vf: the pointer to the VF to check
195 static int ice_check_vf_init(struct ice_pf *pf, struct ice_vf *vf)
197 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
198 dev_err(ice_pf_to_dev(pf), "VF ID: %u in reset. Try again.\n",
206 * ice_vc_vf_broadcast - Broadcast a message to all VFs on PF
207 * @pf: pointer to the PF structure
208 * @v_opcode: operation code
209 * @v_retval: return value
210 * @msg: pointer to the msg buffer
211 * @msglen: msg length
214 ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
215 enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
217 struct ice_hw *hw = &pf->hw;
220 ice_for_each_vf(pf, i) {
221 struct ice_vf *vf = &pf->vf[i];
223 /* Not all vfs are enabled so skip the ones that are not */
224 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
225 !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
228 /* Ignore return value on purpose - a given VF may fail, but
229 * we need to keep going and send to all of them
231 ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
237 * ice_set_pfe_link - Set the link speed/status of the virtchnl_pf_event
238 * @vf: pointer to the VF structure
239 * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
240 * @ice_link_speed: link speed specified by ICE_AQ_LINK_SPEED_*
241 * @link_up: whether or not to set the link up/down
244 ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
245 int ice_link_speed, bool link_up)
247 if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
248 pfe->event_data.link_event_adv.link_status = link_up;
250 pfe->event_data.link_event_adv.link_speed =
251 ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
253 pfe->event_data.link_event.link_status = link_up;
254 /* Legacy method for virtchnl link speeds */
255 pfe->event_data.link_event.link_speed =
256 (enum virtchnl_link_speed)
257 ice_conv_link_speed_to_virtchnl(false, ice_link_speed);
262 * ice_vf_has_no_qs_ena - check if the VF has any Rx or Tx queues enabled
263 * @vf: the VF to check
265 * Returns true if the VF has no Rx and no Tx queues enabled and returns false
268 static bool ice_vf_has_no_qs_ena(struct ice_vf *vf)
270 return (!bitmap_weight(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF) &&
271 !bitmap_weight(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF));
275 * ice_is_vf_link_up - check if the VF's link is up
276 * @vf: VF to check if link is up
278 static bool ice_is_vf_link_up(struct ice_vf *vf)
280 struct ice_pf *pf = vf->pf;
282 if (ice_check_vf_init(pf, vf))
285 if (ice_vf_has_no_qs_ena(vf))
287 else if (vf->link_forced)
290 return pf->hw.port_info->phy.link_info.link_info &
295 * ice_vc_notify_vf_link_state - Inform a VF of link status
296 * @vf: pointer to the VF structure
298 * send a link status message to a single VF
300 void ice_vc_notify_vf_link_state(struct ice_vf *vf)
302 struct virtchnl_pf_event pfe = { 0 };
303 struct ice_hw *hw = &vf->pf->hw;
305 pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
306 pfe.severity = PF_EVENT_SEVERITY_INFO;
308 if (ice_is_vf_link_up(vf))
309 ice_set_pfe_link(vf, &pfe,
310 hw->port_info->phy.link_info.link_speed, true);
312 ice_set_pfe_link(vf, &pfe, ICE_AQ_LINK_SPEED_UNKNOWN, false);
314 ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
315 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,
320 * ice_vf_invalidate_vsi - invalidate vsi_idx/vsi_num to remove VSI access
321 * @vf: VF to remove access to VSI for
323 static void ice_vf_invalidate_vsi(struct ice_vf *vf)
325 vf->lan_vsi_idx = ICE_NO_VSI;
326 vf->lan_vsi_num = ICE_NO_VSI;
330 * ice_vf_vsi_release - invalidate the VF's VSI after freeing it
331 * @vf: invalidate this VF's VSI after freeing it
333 static void ice_vf_vsi_release(struct ice_vf *vf)
335 ice_vsi_release(ice_get_vf_vsi(vf));
336 ice_vf_invalidate_vsi(vf);
340 * ice_vf_ctrl_invalidate_vsi - invalidate ctrl_vsi_idx to remove VSI access
341 * @vf: VF that control VSI is being invalidated on
343 static void ice_vf_ctrl_invalidate_vsi(struct ice_vf *vf)
345 vf->ctrl_vsi_idx = ICE_NO_VSI;
349 * ice_vf_ctrl_vsi_release - invalidate the VF's control VSI after freeing it
350 * @vf: VF that control VSI is being released on
352 static void ice_vf_ctrl_vsi_release(struct ice_vf *vf)
354 ice_vsi_release(vf->pf->vsi[vf->ctrl_vsi_idx]);
355 ice_vf_ctrl_invalidate_vsi(vf);
359 * ice_free_vf_res - Free a VF's resources
360 * @vf: pointer to the VF info
362 static void ice_free_vf_res(struct ice_vf *vf)
364 struct ice_pf *pf = vf->pf;
365 int i, last_vector_idx;
367 /* First, disable VF's configuration API to prevent OS from
368 * accessing the VF's VSI after it's freed or invalidated.
370 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
371 ice_vf_fdir_exit(vf);
372 /* free VF control VSI */
373 if (vf->ctrl_vsi_idx != ICE_NO_VSI)
374 ice_vf_ctrl_vsi_release(vf);
376 /* free VSI and disconnect it from the parent uplink */
377 if (vf->lan_vsi_idx != ICE_NO_VSI) {
378 ice_vf_vsi_release(vf);
382 last_vector_idx = vf->first_vector_idx + pf->num_msix_per_vf - 1;
384 /* clear VF MDD event information */
385 memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
386 memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
388 /* Disable interrupts so that VF starts in a known state */
389 for (i = vf->first_vector_idx; i <= last_vector_idx; i++) {
390 wr32(&pf->hw, GLINT_DYN_CTL(i), GLINT_DYN_CTL_CLEARPBA_M);
393 /* reset some of the state variables keeping track of the resources */
394 clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
395 clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
399 * ice_dis_vf_mappings
400 * @vf: pointer to the VF structure
402 static void ice_dis_vf_mappings(struct ice_vf *vf)
404 struct ice_pf *pf = vf->pf;
411 vsi = ice_get_vf_vsi(vf);
413 dev = ice_pf_to_dev(pf);
414 wr32(hw, VPINT_ALLOC(vf->vf_id), 0);
415 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), 0);
417 first = vf->first_vector_idx;
418 last = first + pf->num_msix_per_vf - 1;
419 for (v = first; v <= last; v++) {
422 reg = (((1 << GLINT_VECT2FUNC_IS_PF_S) &
423 GLINT_VECT2FUNC_IS_PF_M) |
424 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
425 GLINT_VECT2FUNC_PF_NUM_M));
426 wr32(hw, GLINT_VECT2FUNC(v), reg);
429 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
430 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
432 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
434 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
435 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
437 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
441 * ice_sriov_free_msix_res - Reset/free any used MSIX resources
442 * @pf: pointer to the PF structure
444 * Since no MSIX entries are taken from the pf->irq_tracker then just clear
445 * the pf->sriov_base_vector.
447 * Returns 0 on success, and -EINVAL on error.
449 static int ice_sriov_free_msix_res(struct ice_pf *pf)
451 struct ice_res_tracker *res;
456 res = pf->irq_tracker;
460 /* give back irq_tracker resources used */
461 WARN_ON(pf->sriov_base_vector < res->num_entries);
463 pf->sriov_base_vector = 0;
469 * ice_set_vf_state_qs_dis - Set VF queues state to disabled
470 * @vf: pointer to the VF structure
472 void ice_set_vf_state_qs_dis(struct ice_vf *vf)
474 /* Clear Rx/Tx enabled queues flag */
475 bitmap_zero(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF);
476 bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
477 clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
481 * ice_dis_vf_qs - Disable the VF queues
482 * @vf: pointer to the VF structure
484 static void ice_dis_vf_qs(struct ice_vf *vf)
486 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
488 ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id);
489 ice_vsi_stop_all_rx_rings(vsi);
490 ice_set_vf_state_qs_dis(vf);
494 * ice_free_vfs - Free all VFs
495 * @pf: pointer to the PF structure
497 void ice_free_vfs(struct ice_pf *pf)
499 struct device *dev = ice_pf_to_dev(pf);
500 struct ice_hw *hw = &pf->hw;
503 set_bit(ICE_VF_DEINIT_IN_PROGRESS, pf->state);
508 ice_eswitch_release(pf);
510 while (test_and_set_bit(ICE_VF_DIS, pf->state))
511 usleep_range(1000, 2000);
513 /* Disable IOV before freeing resources. This lets any VF drivers
514 * running in the host get themselves cleaned up before we yank
515 * the carpet out from underneath their feet.
517 if (!pci_vfs_assigned(pf->pdev))
518 pci_disable_sriov(pf->pdev);
520 dev_warn(dev, "VFs are assigned - not disabling SR-IOV\n");
522 /* Avoid wait time by stopping all VFs at the same time */
523 ice_for_each_vf(pf, i)
524 ice_dis_vf_qs(&pf->vf[i]);
526 tmp = pf->num_alloc_vfs;
527 pf->num_qps_per_vf = 0;
528 pf->num_alloc_vfs = 0;
529 for (i = 0; i < tmp; i++) {
530 if (test_bit(ICE_VF_STATE_INIT, pf->vf[i].vf_states)) {
531 /* disable VF qp mappings and set VF disable state */
532 ice_dis_vf_mappings(&pf->vf[i]);
533 set_bit(ICE_VF_STATE_DIS, pf->vf[i].vf_states);
534 ice_free_vf_res(&pf->vf[i]);
537 mutex_destroy(&pf->vf[i].cfg_lock);
540 if (ice_sriov_free_msix_res(pf))
541 dev_err(dev, "Failed to free MSIX resources used by SR-IOV\n");
543 devm_kfree(dev, pf->vf);
546 /* This check is for when the driver is unloaded while VFs are
547 * assigned. Setting the number of VFs to 0 through sysfs is caught
548 * before this function ever gets called.
550 if (!pci_vfs_assigned(pf->pdev)) {
553 /* Acknowledge VFLR for all VFs. Without this, VFs will fail to
554 * work correctly when SR-IOV gets re-enabled.
556 for (vf_id = 0; vf_id < tmp; vf_id++) {
557 u32 reg_idx, bit_idx;
559 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
560 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
561 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
565 /* clear malicious info if the VFs are getting released */
566 for (i = 0; i < tmp; i++)
567 if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->malvfs,
568 ICE_MAX_VF_COUNT, i))
569 dev_dbg(dev, "failed to clear malicious VF state for VF %u\n",
572 clear_bit(ICE_VF_DIS, pf->state);
573 clear_bit(ICE_VF_DEINIT_IN_PROGRESS, pf->state);
574 clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
578 * ice_trigger_vf_reset - Reset a VF on HW
579 * @vf: pointer to the VF structure
580 * @is_vflr: true if VFLR was issued, false if not
581 * @is_pfr: true if the reset was triggered due to a previous PFR
583 * Trigger hardware to start a reset for a particular VF. Expects the caller
584 * to wait the proper amount of time to allow hardware to reset the VF before
585 * it cleans up and restores VF functionality.
587 static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr, bool is_pfr)
589 struct ice_pf *pf = vf->pf;
590 u32 reg, reg_idx, bit_idx;
591 unsigned int vf_abs_id, i;
595 dev = ice_pf_to_dev(pf);
597 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
599 /* Inform VF that it is no longer active, as a warning */
600 clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
602 /* Disable VF's configuration API during reset. The flag is re-enabled
603 * when it's safe again to access VF's VSI.
605 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
607 /* VF_MBX_ARQLEN and VF_MBX_ATQLEN are cleared by PFR, so the driver
608 * needs to clear them in the case of VFR/VFLR. If this is done for
609 * PFR, it can mess up VF resets because the VF driver may already
610 * have started cleanup by the time we get here.
613 wr32(hw, VF_MBX_ARQLEN(vf->vf_id), 0);
614 wr32(hw, VF_MBX_ATQLEN(vf->vf_id), 0);
617 /* In the case of a VFLR, the HW has already reset the VF and we
618 * just need to clean up, so don't hit the VFRTRIG register.
621 /* reset VF using VPGEN_VFRTRIG reg */
622 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
623 reg |= VPGEN_VFRTRIG_VFSWR_M;
624 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
626 /* clear the VFLR bit in GLGEN_VFLRSTAT */
627 reg_idx = (vf_abs_id) / 32;
628 bit_idx = (vf_abs_id) % 32;
629 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
632 wr32(hw, PF_PCI_CIAA,
633 VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
634 for (i = 0; i < ICE_PCI_CIAD_WAIT_COUNT; i++) {
635 reg = rd32(hw, PF_PCI_CIAD);
636 /* no transactions pending so stop polling */
637 if ((reg & VF_TRANS_PENDING_M) == 0)
640 dev_err(dev, "VF %u PCI transactions stuck\n", vf->vf_id);
641 udelay(ICE_PCI_CIAD_WAIT_DELAY_US);
646 * ice_vsi_manage_pvid - Enable or disable port VLAN for VSI
647 * @vsi: the VSI to update
648 * @pvid_info: VLAN ID and QoS used to set the PVID VSI context field
649 * @enable: true for enable PVID false for disable
651 static int ice_vsi_manage_pvid(struct ice_vsi *vsi, u16 pvid_info, bool enable)
653 struct ice_hw *hw = &vsi->back->hw;
654 struct ice_aqc_vsi_props *info;
655 struct ice_vsi_ctx *ctxt;
658 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
662 ctxt->info = vsi->info;
665 info->vlan_flags = ICE_AQ_VSI_VLAN_MODE_UNTAGGED |
666 ICE_AQ_VSI_PVLAN_INSERT_PVID |
667 ICE_AQ_VSI_VLAN_EMOD_STR;
668 info->sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
670 info->vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING |
671 ICE_AQ_VSI_VLAN_MODE_ALL;
672 info->sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
675 info->pvid = cpu_to_le16(pvid_info);
676 info->valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
677 ICE_AQ_VSI_PROP_SW_VALID);
679 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
681 dev_info(ice_hw_to_dev(hw), "update VSI for port VLAN failed, err %d aq_err %s\n",
682 ret, ice_aq_str(hw->adminq.sq_last_status));
686 vsi->info.vlan_flags = info->vlan_flags;
687 vsi->info.sw_flags2 = info->sw_flags2;
688 vsi->info.pvid = info->pvid;
695 * ice_vf_get_port_info - Get the VF's port info structure
696 * @vf: VF used to get the port info structure for
698 static struct ice_port_info *ice_vf_get_port_info(struct ice_vf *vf)
700 return vf->pf->hw.port_info;
704 * ice_vf_vsi_setup - Set up a VF VSI
705 * @vf: VF to setup VSI for
707 * Returns pointer to the successfully allocated VSI struct on success,
708 * otherwise returns NULL on failure.
710 static struct ice_vsi *ice_vf_vsi_setup(struct ice_vf *vf)
712 struct ice_port_info *pi = ice_vf_get_port_info(vf);
713 struct ice_pf *pf = vf->pf;
716 vsi = ice_vsi_setup(pf, pi, ICE_VSI_VF, vf->vf_id, NULL);
719 dev_err(ice_pf_to_dev(pf), "Failed to create VF VSI\n");
720 ice_vf_invalidate_vsi(vf);
724 vf->lan_vsi_idx = vsi->idx;
725 vf->lan_vsi_num = vsi->vsi_num;
731 * ice_vf_ctrl_vsi_setup - Set up a VF control VSI
732 * @vf: VF to setup control VSI for
734 * Returns pointer to the successfully allocated VSI struct on success,
735 * otherwise returns NULL on failure.
737 struct ice_vsi *ice_vf_ctrl_vsi_setup(struct ice_vf *vf)
739 struct ice_port_info *pi = ice_vf_get_port_info(vf);
740 struct ice_pf *pf = vf->pf;
743 vsi = ice_vsi_setup(pf, pi, ICE_VSI_CTRL, vf->vf_id, NULL);
745 dev_err(ice_pf_to_dev(pf), "Failed to create VF control VSI\n");
746 ice_vf_ctrl_invalidate_vsi(vf);
753 * ice_calc_vf_first_vector_idx - Calculate MSIX vector index in the PF space
754 * @pf: pointer to PF structure
755 * @vf: pointer to VF that the first MSIX vector index is being calculated for
757 * This returns the first MSIX vector index in PF space that is used by this VF.
758 * This index is used when accessing PF relative registers such as
759 * GLINT_VECT2FUNC and GLINT_DYN_CTL.
760 * This will always be the OICR index in the AVF driver so any functionality
761 * using vf->first_vector_idx for queue configuration will have to increment by
762 * 1 to avoid meddling with the OICR index.
764 static int ice_calc_vf_first_vector_idx(struct ice_pf *pf, struct ice_vf *vf)
766 return pf->sriov_base_vector + vf->vf_id * pf->num_msix_per_vf;
770 * ice_vf_rebuild_host_tx_rate_cfg - re-apply the Tx rate limiting configuration
771 * @vf: VF to re-apply the configuration for
773 * Called after a VF VSI has been re-added/rebuild during reset. The PF driver
774 * needs to re-apply the host configured Tx rate limiting configuration.
776 static int ice_vf_rebuild_host_tx_rate_cfg(struct ice_vf *vf)
778 struct device *dev = ice_pf_to_dev(vf->pf);
779 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
782 if (vf->min_tx_rate) {
783 err = ice_set_min_bw_limit(vsi, (u64)vf->min_tx_rate * 1000);
785 dev_err(dev, "failed to set min Tx rate to %d Mbps for VF %u, error %d\n",
786 vf->min_tx_rate, vf->vf_id, err);
791 if (vf->max_tx_rate) {
792 err = ice_set_max_bw_limit(vsi, (u64)vf->max_tx_rate * 1000);
794 dev_err(dev, "failed to set max Tx rate to %d Mbps for VF %u, error %d\n",
795 vf->max_tx_rate, vf->vf_id, err);
804 * ice_vf_rebuild_host_vlan_cfg - add VLAN 0 filter or rebuild the Port VLAN
805 * @vf: VF to add MAC filters for
807 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
808 * always re-adds either a VLAN 0 or port VLAN based filter after reset.
810 static int ice_vf_rebuild_host_vlan_cfg(struct ice_vf *vf)
812 struct device *dev = ice_pf_to_dev(vf->pf);
813 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
817 if (vf->port_vlan_info) {
818 err = ice_vsi_manage_pvid(vsi, vf->port_vlan_info, true);
820 dev_err(dev, "failed to configure port VLAN via VSI parameters for VF %u, error %d\n",
825 vlan_id = vf->port_vlan_info & VLAN_VID_MASK;
828 /* vlan_id will either be 0 or the port VLAN number */
829 err = ice_vsi_add_vlan(vsi, vlan_id, ICE_FWD_TO_VSI);
831 dev_err(dev, "failed to add %s VLAN %u filter for VF %u, error %d\n",
832 vf->port_vlan_info ? "port" : "", vlan_id, vf->vf_id,
841 * ice_vf_rebuild_host_mac_cfg - add broadcast and the VF's perm_addr/LAA
842 * @vf: VF to add MAC filters for
844 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
845 * always re-adds a broadcast filter and the VF's perm_addr/LAA after reset.
847 static int ice_vf_rebuild_host_mac_cfg(struct ice_vf *vf)
849 struct device *dev = ice_pf_to_dev(vf->pf);
850 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
851 u8 broadcast[ETH_ALEN];
854 if (ice_is_eswitch_mode_switchdev(vf->pf))
857 eth_broadcast_addr(broadcast);
858 status = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
860 dev_err(dev, "failed to add broadcast MAC filter for VF %u, error %d\n",
867 if (is_valid_ether_addr(vf->hw_lan_addr.addr)) {
868 status = ice_fltr_add_mac(vsi, vf->hw_lan_addr.addr,
871 dev_err(dev, "failed to add default unicast MAC filter %pM for VF %u, error %d\n",
872 &vf->hw_lan_addr.addr[0], vf->vf_id,
878 ether_addr_copy(vf->dev_lan_addr.addr, vf->hw_lan_addr.addr);
885 * ice_vf_set_host_trust_cfg - set trust setting based on pre-reset value
886 * @vf: VF to configure trust setting for
888 static void ice_vf_set_host_trust_cfg(struct ice_vf *vf)
891 set_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
893 clear_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
897 * ice_ena_vf_msix_mappings - enable VF MSIX mappings in hardware
898 * @vf: VF to enable MSIX mappings for
900 * Some of the registers need to be indexed/configured using hardware global
901 * device values and other registers need 0-based values, which represent PF
904 static void ice_ena_vf_msix_mappings(struct ice_vf *vf)
906 int device_based_first_msix, device_based_last_msix;
907 int pf_based_first_msix, pf_based_last_msix, v;
908 struct ice_pf *pf = vf->pf;
909 int device_based_vf_id;
914 pf_based_first_msix = vf->first_vector_idx;
915 pf_based_last_msix = (pf_based_first_msix + pf->num_msix_per_vf) - 1;
917 device_based_first_msix = pf_based_first_msix +
918 pf->hw.func_caps.common_cap.msix_vector_first_id;
919 device_based_last_msix =
920 (device_based_first_msix + pf->num_msix_per_vf) - 1;
921 device_based_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
923 reg = (((device_based_first_msix << VPINT_ALLOC_FIRST_S) &
924 VPINT_ALLOC_FIRST_M) |
925 ((device_based_last_msix << VPINT_ALLOC_LAST_S) &
926 VPINT_ALLOC_LAST_M) | VPINT_ALLOC_VALID_M);
927 wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
929 reg = (((device_based_first_msix << VPINT_ALLOC_PCI_FIRST_S)
930 & VPINT_ALLOC_PCI_FIRST_M) |
931 ((device_based_last_msix << VPINT_ALLOC_PCI_LAST_S) &
932 VPINT_ALLOC_PCI_LAST_M) | VPINT_ALLOC_PCI_VALID_M);
933 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg);
935 /* map the interrupts to its functions */
936 for (v = pf_based_first_msix; v <= pf_based_last_msix; v++) {
937 reg = (((device_based_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
938 GLINT_VECT2FUNC_VF_NUM_M) |
939 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
940 GLINT_VECT2FUNC_PF_NUM_M));
941 wr32(hw, GLINT_VECT2FUNC(v), reg);
944 /* Map mailbox interrupt to VF MSI-X vector 0 */
945 wr32(hw, VPINT_MBX_CTL(device_based_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M);
949 * ice_ena_vf_q_mappings - enable Rx/Tx queue mappings for a VF
950 * @vf: VF to enable the mappings for
951 * @max_txq: max Tx queues allowed on the VF's VSI
952 * @max_rxq: max Rx queues allowed on the VF's VSI
954 static void ice_ena_vf_q_mappings(struct ice_vf *vf, u16 max_txq, u16 max_rxq)
956 struct device *dev = ice_pf_to_dev(vf->pf);
957 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
958 struct ice_hw *hw = &vf->pf->hw;
961 /* set regardless of mapping mode */
962 wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M);
964 /* VF Tx queues allocation */
965 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
966 /* set the VF PF Tx queue range
967 * VFNUMQ value should be set to (number of queues - 1). A value
968 * of 0 means 1 queue and a value of 255 means 256 queues
970 reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
971 VPLAN_TX_QBASE_VFFIRSTQ_M) |
972 (((max_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
973 VPLAN_TX_QBASE_VFNUMQ_M));
974 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
976 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
979 /* set regardless of mapping mode */
980 wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M);
982 /* VF Rx queues allocation */
983 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
984 /* set the VF PF Rx queue range
985 * VFNUMQ value should be set to (number of queues - 1). A value
986 * of 0 means 1 queue and a value of 255 means 256 queues
988 reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
989 VPLAN_RX_QBASE_VFFIRSTQ_M) |
990 (((max_rxq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
991 VPLAN_RX_QBASE_VFNUMQ_M));
992 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
994 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
999 * ice_ena_vf_mappings - enable VF MSIX and queue mapping
1000 * @vf: pointer to the VF structure
1002 static void ice_ena_vf_mappings(struct ice_vf *vf)
1004 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1006 ice_ena_vf_msix_mappings(vf);
1007 ice_ena_vf_q_mappings(vf, vsi->alloc_txq, vsi->alloc_rxq);
1012 * @pf: pointer to the PF structure
1013 * @avail_res: available resources in the PF structure
1014 * @max_res: maximum resources that can be given per VF
1015 * @min_res: minimum resources that can be given per VF
1017 * Returns non-zero value if resources (queues/vectors) are available or
1018 * returns zero if PF cannot accommodate for all num_alloc_vfs.
1021 ice_determine_res(struct ice_pf *pf, u16 avail_res, u16 max_res, u16 min_res)
1023 bool checked_min_res = false;
1026 /* start by checking if PF can assign max number of resources for
1027 * all num_alloc_vfs.
1028 * if yes, return number per VF
1029 * If no, divide by 2 and roundup, check again
1030 * repeat the loop till we reach a point where even minimum resources
1031 * are not available, in that case return 0
1034 while ((res >= min_res) && !checked_min_res) {
1037 num_all_res = pf->num_alloc_vfs * res;
1038 if (num_all_res <= avail_res)
1042 checked_min_res = true;
1044 res = DIV_ROUND_UP(res, 2);
1050 * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space
1051 * @vf: VF to calculate the register index for
1052 * @q_vector: a q_vector associated to the VF
1054 int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector)
1058 if (!vf || !q_vector)
1063 /* always add one to account for the OICR being the first MSIX */
1064 return pf->sriov_base_vector + pf->num_msix_per_vf * vf->vf_id +
1065 q_vector->v_idx + 1;
1069 * ice_get_max_valid_res_idx - Get the max valid resource index
1070 * @res: pointer to the resource to find the max valid index for
1072 * Start from the end of the ice_res_tracker and return right when we find the
1073 * first res->list entry with the ICE_RES_VALID_BIT set. This function is only
1074 * valid for SR-IOV because it is the only consumer that manipulates the
1075 * res->end and this is always called when res->end is set to res->num_entries.
1077 static int ice_get_max_valid_res_idx(struct ice_res_tracker *res)
1084 for (i = res->num_entries - 1; i >= 0; i--)
1085 if (res->list[i] & ICE_RES_VALID_BIT)
1092 * ice_sriov_set_msix_res - Set any used MSIX resources
1093 * @pf: pointer to PF structure
1094 * @num_msix_needed: number of MSIX vectors needed for all SR-IOV VFs
1096 * This function allows SR-IOV resources to be taken from the end of the PF's
1097 * allowed HW MSIX vectors so that the irq_tracker will not be affected. We
1098 * just set the pf->sriov_base_vector and return success.
1100 * If there are not enough resources available, return an error. This should
1101 * always be caught by ice_set_per_vf_res().
1103 * Return 0 on success, and -EINVAL when there are not enough MSIX vectors
1104 * in the PF's space available for SR-IOV.
1106 static int ice_sriov_set_msix_res(struct ice_pf *pf, u16 num_msix_needed)
1108 u16 total_vectors = pf->hw.func_caps.common_cap.num_msix_vectors;
1109 int vectors_used = pf->irq_tracker->num_entries;
1110 int sriov_base_vector;
1112 sriov_base_vector = total_vectors - num_msix_needed;
1114 /* make sure we only grab irq_tracker entries from the list end and
1115 * that we have enough available MSIX vectors
1117 if (sriov_base_vector < vectors_used)
1120 pf->sriov_base_vector = sriov_base_vector;
1126 * ice_set_per_vf_res - check if vectors and queues are available
1127 * @pf: pointer to the PF structure
1129 * First, determine HW interrupts from common pool. If we allocate fewer VFs, we
1130 * get more vectors and can enable more queues per VF. Note that this does not
1131 * grab any vectors from the SW pool already allocated. Also note, that all
1132 * vector counts include one for each VF's miscellaneous interrupt vector
1135 * Minimum VFs - 2 vectors, 1 queue pair
1136 * Small VFs - 5 vectors, 4 queue pairs
1137 * Medium VFs - 17 vectors, 16 queue pairs
1139 * Second, determine number of queue pairs per VF by starting with a pre-defined
1140 * maximum each VF supports. If this is not possible, then we adjust based on
1141 * queue pairs available on the device.
1143 * Lastly, set queue and MSI-X VF variables tracked by the PF so it can be used
1144 * by each VF during VF initialization and reset.
1146 static int ice_set_per_vf_res(struct ice_pf *pf)
1148 int max_valid_res_idx = ice_get_max_valid_res_idx(pf->irq_tracker);
1149 int msix_avail_per_vf, msix_avail_for_sriov;
1150 struct device *dev = ice_pf_to_dev(pf);
1151 u16 num_msix_per_vf, num_txq, num_rxq;
1153 if (!pf->num_alloc_vfs || max_valid_res_idx < 0)
1156 /* determine MSI-X resources per VF */
1157 msix_avail_for_sriov = pf->hw.func_caps.common_cap.num_msix_vectors -
1158 pf->irq_tracker->num_entries;
1159 msix_avail_per_vf = msix_avail_for_sriov / pf->num_alloc_vfs;
1160 if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MED) {
1161 num_msix_per_vf = ICE_NUM_VF_MSIX_MED;
1162 } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_SMALL) {
1163 num_msix_per_vf = ICE_NUM_VF_MSIX_SMALL;
1164 } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MULTIQ_MIN) {
1165 num_msix_per_vf = ICE_NUM_VF_MSIX_MULTIQ_MIN;
1166 } else if (msix_avail_per_vf >= ICE_MIN_INTR_PER_VF) {
1167 num_msix_per_vf = ICE_MIN_INTR_PER_VF;
1169 dev_err(dev, "Only %d MSI-X interrupts available for SR-IOV. Not enough to support minimum of %d MSI-X interrupts per VF for %d VFs\n",
1170 msix_avail_for_sriov, ICE_MIN_INTR_PER_VF,
1175 /* determine queue resources per VF */
1176 num_txq = ice_determine_res(pf, ice_get_avail_txq_count(pf),
1178 num_msix_per_vf - ICE_NONQ_VECS_VF,
1179 ICE_MAX_RSS_QS_PER_VF),
1182 num_rxq = ice_determine_res(pf, ice_get_avail_rxq_count(pf),
1184 num_msix_per_vf - ICE_NONQ_VECS_VF,
1185 ICE_MAX_RSS_QS_PER_VF),
1188 if (!num_txq || !num_rxq) {
1189 dev_err(dev, "Not enough queues to support minimum of %d queue pairs per VF for %d VFs\n",
1190 ICE_MIN_QS_PER_VF, pf->num_alloc_vfs);
1194 if (ice_sriov_set_msix_res(pf, num_msix_per_vf * pf->num_alloc_vfs)) {
1195 dev_err(dev, "Unable to set MSI-X resources for %d VFs\n",
1200 /* only allow equal Tx/Rx queue count (i.e. queue pairs) */
1201 pf->num_qps_per_vf = min_t(int, num_txq, num_rxq);
1202 pf->num_msix_per_vf = num_msix_per_vf;
1203 dev_info(dev, "Enabling %d VFs with %d vectors and %d queues per VF\n",
1204 pf->num_alloc_vfs, pf->num_msix_per_vf, pf->num_qps_per_vf);
1210 * ice_clear_vf_reset_trigger - enable VF to access hardware
1211 * @vf: VF to enabled hardware access for
1213 static void ice_clear_vf_reset_trigger(struct ice_vf *vf)
1215 struct ice_hw *hw = &vf->pf->hw;
1218 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
1219 reg &= ~VPGEN_VFRTRIG_VFSWR_M;
1220 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
1225 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
1227 struct ice_hw *hw = &vsi->back->hw;
1230 if (vf->port_vlan_info)
1231 status = ice_fltr_set_vsi_promisc(hw, vsi->idx, promisc_m,
1232 vf->port_vlan_info & VLAN_VID_MASK);
1233 else if (vsi->num_vlan > 1)
1234 status = ice_fltr_set_vlan_vsi_promisc(hw, vsi, promisc_m);
1236 status = ice_fltr_set_vsi_promisc(hw, vsi->idx, promisc_m, 0);
1238 if (status && status != -EEXIST) {
1239 dev_err(ice_pf_to_dev(vsi->back), "enable Tx/Rx filter promiscuous mode on VF-%u failed, error: %d\n",
1248 ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
1250 struct ice_hw *hw = &vsi->back->hw;
1253 if (vf->port_vlan_info)
1254 status = ice_fltr_clear_vsi_promisc(hw, vsi->idx, promisc_m,
1255 vf->port_vlan_info & VLAN_VID_MASK);
1256 else if (vsi->num_vlan > 1)
1257 status = ice_fltr_clear_vlan_vsi_promisc(hw, vsi, promisc_m);
1259 status = ice_fltr_clear_vsi_promisc(hw, vsi->idx, promisc_m, 0);
1261 if (status && status != -ENOENT) {
1262 dev_err(ice_pf_to_dev(vsi->back), "disable Tx/Rx filter promiscuous mode on VF-%u failed, error: %d\n",
1270 static void ice_vf_clear_counters(struct ice_vf *vf)
1272 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1276 memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
1277 memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
1281 * ice_vf_pre_vsi_rebuild - tasks to be done prior to VSI rebuild
1282 * @vf: VF to perform pre VSI rebuild tasks
1284 * These tasks are items that don't need to be amortized since they are most
1285 * likely called in a for loop with all VF(s) in the reset_all_vfs() case.
1287 static void ice_vf_pre_vsi_rebuild(struct ice_vf *vf)
1289 ice_vf_clear_counters(vf);
1290 ice_clear_vf_reset_trigger(vf);
1294 * ice_vf_rebuild_aggregator_node_cfg - rebuild aggregator node config
1295 * @vsi: Pointer to VSI
1297 * This function moves VSI into corresponding scheduler aggregator node
1298 * based on cached value of "aggregator node info" per VSI
1300 static void ice_vf_rebuild_aggregator_node_cfg(struct ice_vsi *vsi)
1302 struct ice_pf *pf = vsi->back;
1309 dev = ice_pf_to_dev(pf);
1310 if (vsi->agg_node->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
1312 "agg_id %u already has reached max_num_vsis %u\n",
1313 vsi->agg_node->agg_id, vsi->agg_node->num_vsis);
1317 status = ice_move_vsi_to_agg(pf->hw.port_info, vsi->agg_node->agg_id,
1318 vsi->idx, vsi->tc_cfg.ena_tc);
1320 dev_dbg(dev, "unable to move VSI idx %u into aggregator %u node",
1321 vsi->idx, vsi->agg_node->agg_id);
1323 vsi->agg_node->num_vsis++;
1327 * ice_vf_rebuild_host_cfg - host admin configuration is persistent across reset
1328 * @vf: VF to rebuild host configuration on
1330 static void ice_vf_rebuild_host_cfg(struct ice_vf *vf)
1332 struct device *dev = ice_pf_to_dev(vf->pf);
1333 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1335 ice_vf_set_host_trust_cfg(vf);
1337 if (ice_vf_rebuild_host_mac_cfg(vf))
1338 dev_err(dev, "failed to rebuild default MAC configuration for VF %d\n",
1341 if (ice_vf_rebuild_host_vlan_cfg(vf))
1342 dev_err(dev, "failed to rebuild VLAN configuration for VF %u\n",
1345 if (ice_vf_rebuild_host_tx_rate_cfg(vf))
1346 dev_err(dev, "failed to rebuild Tx rate limiting configuration for VF %u\n",
1349 /* rebuild aggregator node config for main VF VSI */
1350 ice_vf_rebuild_aggregator_node_cfg(vsi);
1354 * ice_vf_rebuild_vsi_with_release - release and setup the VF's VSI
1355 * @vf: VF to release and setup the VSI for
1357 * This is only called when a single VF is being reset (i.e. VFR, VFLR, host VF
1358 * configuration change, etc.).
1360 static int ice_vf_rebuild_vsi_with_release(struct ice_vf *vf)
1362 ice_vf_vsi_release(vf);
1363 if (!ice_vf_vsi_setup(vf))
1370 * ice_vf_rebuild_vsi - rebuild the VF's VSI
1371 * @vf: VF to rebuild the VSI for
1373 * This is only called when all VF(s) are being reset (i.e. PCIe Reset on the
1374 * host, PFR, CORER, etc.).
1376 static int ice_vf_rebuild_vsi(struct ice_vf *vf)
1378 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1379 struct ice_pf *pf = vf->pf;
1381 if (ice_vsi_rebuild(vsi, true)) {
1382 dev_err(ice_pf_to_dev(pf), "failed to rebuild VF %d VSI\n",
1386 /* vsi->idx will remain the same in this case so don't update
1389 vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
1390 vf->lan_vsi_num = vsi->vsi_num;
1396 * ice_vf_set_initialized - VF is ready for VIRTCHNL communication
1397 * @vf: VF to set in initialized state
1399 * After this function the VF will be ready to receive/handle the
1400 * VIRTCHNL_OP_GET_VF_RESOURCES message
1402 static void ice_vf_set_initialized(struct ice_vf *vf)
1404 ice_set_vf_state_qs_dis(vf);
1405 clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
1406 clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
1407 clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
1408 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
1412 * ice_vf_post_vsi_rebuild - tasks to do after the VF's VSI have been rebuilt
1413 * @vf: VF to perform tasks on
1415 static void ice_vf_post_vsi_rebuild(struct ice_vf *vf)
1417 struct ice_pf *pf = vf->pf;
1422 ice_vf_rebuild_host_cfg(vf);
1424 ice_vf_set_initialized(vf);
1425 ice_ena_vf_mappings(vf);
1426 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1430 * ice_reset_all_vfs - reset all allocated VFs in one go
1431 * @pf: pointer to the PF structure
1432 * @is_vflr: true if VFLR was issued, false if not
1434 * First, tell the hardware to reset each VF, then do all the waiting in one
1435 * chunk, and finally finish restoring each VF after the wait. This is useful
1436 * during PF routines which need to reset all VFs, as otherwise it must perform
1437 * these resets in a serialized fashion.
1439 * Returns true if any VFs were reset, and false otherwise.
1441 bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr)
1443 struct device *dev = ice_pf_to_dev(pf);
1444 struct ice_hw *hw = &pf->hw;
1448 /* If we don't have any VFs, then there is nothing to reset */
1449 if (!pf->num_alloc_vfs)
1452 /* clear all malicious info if the VFs are getting reset */
1453 ice_for_each_vf(pf, i)
1454 if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->malvfs, ICE_MAX_VF_COUNT, i))
1455 dev_dbg(dev, "failed to clear malicious VF state for VF %u\n", i);
1457 /* If VFs have been disabled, there is no need to reset */
1458 if (test_and_set_bit(ICE_VF_DIS, pf->state))
1461 /* Begin reset on all VFs at once */
1462 ice_for_each_vf(pf, v)
1463 ice_trigger_vf_reset(&pf->vf[v], is_vflr, true);
1465 /* HW requires some time to make sure it can flush the FIFO for a VF
1466 * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1467 * sequence to make sure that it has completed. We'll keep track of
1468 * the VFs using a simple iterator that increments once that VF has
1469 * finished resetting.
1471 for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1472 /* Check each VF in sequence */
1473 while (v < pf->num_alloc_vfs) {
1477 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1478 if (!(reg & VPGEN_VFRSTAT_VFRD_M)) {
1479 /* only delay if the check failed */
1480 usleep_range(10, 20);
1484 /* If the current VF has finished resetting, move on
1485 * to the next VF in sequence.
1491 /* Display a warning if at least one VF didn't manage to reset in
1492 * time, but continue on with the operation.
1494 if (v < pf->num_alloc_vfs)
1495 dev_warn(dev, "VF reset check timeout\n");
1497 /* free VF resources to begin resetting the VSI state */
1498 ice_for_each_vf(pf, v) {
1501 vf->driver_caps = 0;
1502 ice_vc_set_default_allowlist(vf);
1504 ice_vf_fdir_exit(vf);
1505 ice_vf_fdir_init(vf);
1506 /* clean VF control VSI when resetting VFs since it should be
1507 * setup only when VF creates its first FDIR rule.
1509 if (vf->ctrl_vsi_idx != ICE_NO_VSI)
1510 ice_vf_ctrl_invalidate_vsi(vf);
1512 ice_vf_pre_vsi_rebuild(vf);
1513 ice_vf_rebuild_vsi(vf);
1514 ice_vf_post_vsi_rebuild(vf);
1517 if (ice_is_eswitch_mode_switchdev(pf))
1518 if (ice_eswitch_rebuild(pf))
1519 dev_warn(dev, "eswitch rebuild failed\n");
1522 clear_bit(ICE_VF_DIS, pf->state);
1528 * ice_is_vf_disabled
1529 * @vf: pointer to the VF info
1531 * Returns true if the PF or VF is disabled, false otherwise.
1533 bool ice_is_vf_disabled(struct ice_vf *vf)
1535 struct ice_pf *pf = vf->pf;
1537 /* If the PF has been disabled, there is no need resetting VF until
1538 * PF is active again. Similarly, if the VF has been disabled, this
1539 * means something else is resetting the VF, so we shouldn't continue.
1540 * Otherwise, set disable VF state bit for actual reset, and continue.
1542 return (test_bit(ICE_VF_DIS, pf->state) ||
1543 test_bit(ICE_VF_STATE_DIS, vf->vf_states));
1547 * ice_reset_vf - Reset a particular VF
1548 * @vf: pointer to the VF structure
1549 * @is_vflr: true if VFLR was issued, false if not
1551 * Returns true if the VF is currently in reset, resets successfully, or resets
1552 * are disabled and false otherwise.
1554 bool ice_reset_vf(struct ice_vf *vf, bool is_vflr)
1556 struct ice_pf *pf = vf->pf;
1557 struct ice_vsi *vsi;
1565 dev = ice_pf_to_dev(pf);
1567 if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
1568 dev_dbg(dev, "Trying to reset VF %d, but all VF resets are disabled\n",
1573 if (ice_is_vf_disabled(vf)) {
1574 dev_dbg(dev, "VF is already disabled, there is no need for resetting it, telling VM, all is fine %d\n",
1579 /* Set VF disable bit state here, before triggering reset */
1580 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
1581 ice_trigger_vf_reset(vf, is_vflr, false);
1583 vsi = ice_get_vf_vsi(vf);
1587 /* Call Disable LAN Tx queue AQ whether or not queues are
1588 * enabled. This is needed for successful completion of VFR.
1590 ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
1591 NULL, ICE_VF_RESET, vf->vf_id, NULL);
1594 /* poll VPGEN_VFRSTAT reg to make sure
1595 * that reset is complete
1597 for (i = 0; i < 10; i++) {
1598 /* VF reset requires driver to first reset the VF and then
1599 * poll the status register to make sure that the reset
1600 * completed successfully.
1602 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1603 if (reg & VPGEN_VFRSTAT_VFRD_M) {
1608 /* only sleep if the reset is not done */
1609 usleep_range(10, 20);
1612 vf->driver_caps = 0;
1613 ice_vc_set_default_allowlist(vf);
1615 /* Display a warning if VF didn't manage to reset in time, but need to
1616 * continue on with the operation.
1619 dev_warn(dev, "VF reset check timeout on VF %d\n", vf->vf_id);
1621 /* disable promiscuous modes in case they were enabled
1622 * ignore any error if disabling process failed
1624 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
1625 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
1626 if (vf->port_vlan_info || vsi->num_vlan)
1627 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
1629 promisc_m = ICE_UCAST_PROMISC_BITS;
1631 if (ice_vf_clear_vsi_promisc(vf, vsi, promisc_m))
1632 dev_err(dev, "disabling promiscuous mode failed\n");
1635 ice_eswitch_del_vf_mac_rule(vf);
1637 ice_vf_fdir_exit(vf);
1638 ice_vf_fdir_init(vf);
1639 /* clean VF control VSI when resetting VF since it should be setup
1640 * only when VF creates its first FDIR rule.
1642 if (vf->ctrl_vsi_idx != ICE_NO_VSI)
1643 ice_vf_ctrl_vsi_release(vf);
1645 ice_vf_pre_vsi_rebuild(vf);
1647 if (ice_vf_rebuild_vsi_with_release(vf)) {
1648 dev_err(dev, "Failed to release and setup the VF%u's VSI\n", vf->vf_id);
1652 ice_vf_post_vsi_rebuild(vf);
1653 vsi = ice_get_vf_vsi(vf);
1654 ice_eswitch_update_repr(vsi);
1655 ice_eswitch_replay_vf_mac_rule(vf);
1657 /* if the VF has been reset allow it to come up again */
1658 if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->malvfs, ICE_MAX_VF_COUNT, vf->vf_id))
1659 dev_dbg(dev, "failed to clear malicious VF state for VF %u\n", i);
1665 * ice_vc_notify_link_state - Inform all VFs on a PF of link status
1666 * @pf: pointer to the PF structure
1668 void ice_vc_notify_link_state(struct ice_pf *pf)
1672 ice_for_each_vf(pf, i)
1673 ice_vc_notify_vf_link_state(&pf->vf[i]);
1677 * ice_vc_notify_reset - Send pending reset message to all VFs
1678 * @pf: pointer to the PF structure
1680 * indicate a pending reset to all VFs on a given PF
1682 void ice_vc_notify_reset(struct ice_pf *pf)
1684 struct virtchnl_pf_event pfe;
1686 if (!pf->num_alloc_vfs)
1689 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1690 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1691 ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
1692 (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
1696 * ice_vc_notify_vf_reset - Notify VF of a reset event
1697 * @vf: pointer to the VF structure
1699 static void ice_vc_notify_vf_reset(struct ice_vf *vf)
1701 struct virtchnl_pf_event pfe;
1708 if (ice_validate_vf_id(pf, vf->vf_id))
1711 /* Bail out if VF is in disabled state, neither initialized, nor active
1712 * state - otherwise proceed with notifications
1714 if ((!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
1715 !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) ||
1716 test_bit(ICE_VF_STATE_DIS, vf->vf_states))
1719 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1720 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1721 ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, VIRTCHNL_OP_EVENT,
1722 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe, sizeof(pfe),
1727 * ice_init_vf_vsi_res - initialize/setup VF VSI resources
1728 * @vf: VF to initialize/setup the VSI for
1730 * This function creates a VSI for the VF, adds a VLAN 0 filter, and sets up the
1731 * VF VSI's broadcast filter and is only used during initial VF creation.
1733 static int ice_init_vf_vsi_res(struct ice_vf *vf)
1735 struct ice_pf *pf = vf->pf;
1736 u8 broadcast[ETH_ALEN];
1737 struct ice_vsi *vsi;
1741 vf->first_vector_idx = ice_calc_vf_first_vector_idx(pf, vf);
1743 dev = ice_pf_to_dev(pf);
1744 vsi = ice_vf_vsi_setup(vf);
1748 err = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
1750 dev_warn(dev, "Failed to add VLAN 0 filter for VF %d\n",
1755 eth_broadcast_addr(broadcast);
1756 err = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
1758 dev_err(dev, "Failed to add broadcast MAC filter for VF %d, error %d\n",
1768 ice_vf_vsi_release(vf);
1773 * ice_start_vfs - start VFs so they are ready to be used by SR-IOV
1774 * @pf: PF the VFs are associated with
1776 static int ice_start_vfs(struct ice_pf *pf)
1778 struct ice_hw *hw = &pf->hw;
1781 ice_for_each_vf(pf, i) {
1782 struct ice_vf *vf = &pf->vf[i];
1784 ice_clear_vf_reset_trigger(vf);
1786 retval = ice_init_vf_vsi_res(vf);
1788 dev_err(ice_pf_to_dev(pf), "Failed to initialize VSI resources for VF %d, error %d\n",
1793 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
1794 ice_ena_vf_mappings(vf);
1795 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1802 for (i = i - 1; i >= 0; i--) {
1803 struct ice_vf *vf = &pf->vf[i];
1805 ice_dis_vf_mappings(vf);
1806 ice_vf_vsi_release(vf);
1813 * ice_set_dflt_settings_vfs - set VF defaults during initialization/creation
1814 * @pf: PF holding reference to all VFs for default configuration
1816 static void ice_set_dflt_settings_vfs(struct ice_pf *pf)
1820 ice_for_each_vf(pf, i) {
1821 struct ice_vf *vf = &pf->vf[i];
1825 vf->vf_sw_id = pf->first_sw;
1826 /* assign default capabilities */
1827 set_bit(ICE_VIRTCHNL_VF_CAP_L2, &vf->vf_caps);
1828 vf->spoofchk = true;
1829 vf->num_vf_qs = pf->num_qps_per_vf;
1830 ice_vc_set_default_allowlist(vf);
1832 /* ctrl_vsi_idx will be set to a valid value only when VF
1833 * creates its first fdir rule.
1835 ice_vf_ctrl_invalidate_vsi(vf);
1836 ice_vf_fdir_init(vf);
1838 ice_vc_set_dflt_vf_ops(&vf->vc_ops);
1840 mutex_init(&vf->cfg_lock);
1845 * ice_alloc_vfs - allocate num_vfs in the PF structure
1846 * @pf: PF to store the allocated VFs in
1847 * @num_vfs: number of VFs to allocate
1849 static int ice_alloc_vfs(struct ice_pf *pf, int num_vfs)
1853 vfs = devm_kcalloc(ice_pf_to_dev(pf), num_vfs, sizeof(*vfs),
1859 pf->num_alloc_vfs = num_vfs;
1865 * ice_ena_vfs - enable VFs so they are ready to be used
1866 * @pf: pointer to the PF structure
1867 * @num_vfs: number of VFs to enable
1869 static int ice_ena_vfs(struct ice_pf *pf, u16 num_vfs)
1871 struct device *dev = ice_pf_to_dev(pf);
1872 struct ice_hw *hw = &pf->hw;
1875 /* Disable global interrupt 0 so we don't try to handle the VFLR. */
1876 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1877 ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
1878 set_bit(ICE_OICR_INTR_DIS, pf->state);
1881 ret = pci_enable_sriov(pf->pdev, num_vfs);
1883 pf->num_alloc_vfs = 0;
1884 goto err_unroll_intr;
1887 ret = ice_alloc_vfs(pf, num_vfs);
1889 goto err_pci_disable_sriov;
1891 if (ice_set_per_vf_res(pf)) {
1892 dev_err(dev, "Not enough resources for %d VFs, try with fewer number of VFs\n",
1895 goto err_unroll_sriov;
1898 ice_set_dflt_settings_vfs(pf);
1900 if (ice_start_vfs(pf)) {
1901 dev_err(dev, "Failed to start VF(s)\n");
1903 goto err_unroll_sriov;
1906 clear_bit(ICE_VF_DIS, pf->state);
1908 ret = ice_eswitch_configure(pf);
1910 goto err_unroll_sriov;
1912 /* rearm global interrupts */
1913 if (test_and_clear_bit(ICE_OICR_INTR_DIS, pf->state))
1914 ice_irq_dynamic_ena(hw, NULL, NULL);
1919 devm_kfree(dev, pf->vf);
1921 pf->num_alloc_vfs = 0;
1922 err_pci_disable_sriov:
1923 pci_disable_sriov(pf->pdev);
1925 /* rearm interrupts here */
1926 ice_irq_dynamic_ena(hw, NULL, NULL);
1927 clear_bit(ICE_OICR_INTR_DIS, pf->state);
1932 * ice_pci_sriov_ena - Enable or change number of VFs
1933 * @pf: pointer to the PF structure
1934 * @num_vfs: number of VFs to allocate
1936 * Returns 0 on success and negative on failure
1938 static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
1940 int pre_existing_vfs = pci_num_vf(pf->pdev);
1941 struct device *dev = ice_pf_to_dev(pf);
1944 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1946 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1949 if (num_vfs > pf->num_vfs_supported) {
1950 dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
1951 num_vfs, pf->num_vfs_supported);
1955 dev_info(dev, "Enabling %d VFs\n", num_vfs);
1956 err = ice_ena_vfs(pf, num_vfs);
1958 dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
1962 set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
1967 * ice_check_sriov_allowed - check if SR-IOV is allowed based on various checks
1968 * @pf: PF to enabled SR-IOV on
1970 static int ice_check_sriov_allowed(struct ice_pf *pf)
1972 struct device *dev = ice_pf_to_dev(pf);
1974 if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
1975 dev_err(dev, "This device is not capable of SR-IOV\n");
1979 if (ice_is_safe_mode(pf)) {
1980 dev_err(dev, "SR-IOV cannot be configured - Device is in Safe Mode\n");
1984 if (!ice_pf_state_is_nominal(pf)) {
1985 dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
1993 * ice_sriov_configure - Enable or change number of VFs via sysfs
1994 * @pdev: pointer to a pci_dev structure
1995 * @num_vfs: number of VFs to allocate or 0 to free VFs
1997 * This function is called when the user updates the number of VFs in sysfs. On
1998 * success return whatever num_vfs was set to by the caller. Return negative on
2001 int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
2003 struct ice_pf *pf = pci_get_drvdata(pdev);
2004 struct device *dev = ice_pf_to_dev(pf);
2007 err = ice_check_sriov_allowed(pf);
2012 if (!pci_vfs_assigned(pdev)) {
2013 ice_mbx_deinit_snapshot(&pf->hw);
2016 ice_enable_lag(pf->lag);
2020 dev_err(dev, "can't free VFs because some are assigned to VMs.\n");
2024 err = ice_mbx_init_snapshot(&pf->hw, num_vfs);
2028 err = ice_pci_sriov_ena(pf, num_vfs);
2030 ice_mbx_deinit_snapshot(&pf->hw);
2035 ice_disable_lag(pf->lag);
2040 * ice_process_vflr_event - Free VF resources via IRQ calls
2041 * @pf: pointer to the PF structure
2043 * called from the VFLR IRQ handler to
2044 * free up VF resources and state variables
2046 void ice_process_vflr_event(struct ice_pf *pf)
2048 struct ice_hw *hw = &pf->hw;
2052 if (!test_and_clear_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
2056 ice_for_each_vf(pf, vf_id) {
2057 struct ice_vf *vf = &pf->vf[vf_id];
2058 u32 reg_idx, bit_idx;
2060 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
2061 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
2062 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
2063 reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
2064 if (reg & BIT(bit_idx))
2065 /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
2066 ice_reset_vf(vf, true);
2071 * ice_vc_reset_vf - Perform software reset on the VF after informing the AVF
2072 * @vf: pointer to the VF info
2074 static void ice_vc_reset_vf(struct ice_vf *vf)
2076 ice_vc_notify_vf_reset(vf);
2077 ice_reset_vf(vf, false);
2081 * ice_get_vf_from_pfq - get the VF who owns the PF space queue passed in
2082 * @pf: PF used to index all VFs
2083 * @pfq: queue index relative to the PF's function space
2085 * If no VF is found who owns the pfq then return NULL, otherwise return a
2086 * pointer to the VF who owns the pfq
2088 static struct ice_vf *ice_get_vf_from_pfq(struct ice_pf *pf, u16 pfq)
2092 ice_for_each_vf(pf, vf_id) {
2093 struct ice_vf *vf = &pf->vf[vf_id];
2094 struct ice_vsi *vsi;
2097 vsi = ice_get_vf_vsi(vf);
2099 ice_for_each_rxq(vsi, rxq_idx)
2100 if (vsi->rxq_map[rxq_idx] == pfq)
2108 * ice_globalq_to_pfq - convert from global queue index to PF space queue index
2109 * @pf: PF used for conversion
2110 * @globalq: global queue index used to convert to PF space queue index
2112 static u32 ice_globalq_to_pfq(struct ice_pf *pf, u32 globalq)
2114 return globalq - pf->hw.func_caps.common_cap.rxq_first_id;
2118 * ice_vf_lan_overflow_event - handle LAN overflow event for a VF
2119 * @pf: PF that the LAN overflow event happened on
2120 * @event: structure holding the event information for the LAN overflow event
2122 * Determine if the LAN overflow event was caused by a VF queue. If it was not
2123 * caused by a VF, do nothing. If a VF caused this LAN overflow event trigger a
2124 * reset on the offending VF.
2127 ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event)
2129 u32 gldcb_rtctq, queue;
2132 gldcb_rtctq = le32_to_cpu(event->desc.params.lan_overflow.prtdcb_ruptq);
2133 dev_dbg(ice_pf_to_dev(pf), "GLDCB_RTCTQ: 0x%08x\n", gldcb_rtctq);
2135 /* event returns device global Rx queue number */
2136 queue = (gldcb_rtctq & GLDCB_RTCTQ_RXQNUM_M) >>
2137 GLDCB_RTCTQ_RXQNUM_S;
2139 vf = ice_get_vf_from_pfq(pf, ice_globalq_to_pfq(pf, queue));
2143 ice_vc_reset_vf(vf);
2147 * ice_vc_send_msg_to_vf - Send message to VF
2148 * @vf: pointer to the VF info
2149 * @v_opcode: virtual channel opcode
2150 * @v_retval: virtual channel return value
2151 * @msg: pointer to the msg buffer
2152 * @msglen: msg length
2157 ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
2158 enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
2168 if (ice_validate_vf_id(pf, vf->vf_id))
2171 dev = ice_pf_to_dev(pf);
2173 /* single place to detect unsuccessful return values */
2175 vf->num_inval_msgs++;
2176 dev_info(dev, "VF %d failed opcode %d, retval: %d\n", vf->vf_id,
2177 v_opcode, v_retval);
2178 if (vf->num_inval_msgs > ICE_DFLT_NUM_INVAL_MSGS_ALLOWED) {
2179 dev_err(dev, "Number of invalid messages exceeded for VF %d\n",
2181 dev_err(dev, "Use PF Control I/F to enable the VF\n");
2182 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
2186 vf->num_valid_msgs++;
2187 /* reset the invalid counter, if a valid message is received. */
2188 vf->num_inval_msgs = 0;
2191 aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
2193 if (aq_ret && pf->hw.mailboxq.sq_last_status != ICE_AQ_RC_ENOSYS) {
2194 dev_info(dev, "Unable to send the message to VF %d ret %d aq_err %s\n",
2196 ice_aq_str(pf->hw.mailboxq.sq_last_status));
2204 * ice_vc_get_ver_msg
2205 * @vf: pointer to the VF info
2206 * @msg: pointer to the msg buffer
2208 * called from the VF to request the API version used by the PF
2210 static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
2212 struct virtchnl_version_info info = {
2213 VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
2216 vf->vf_ver = *(struct virtchnl_version_info *)msg;
2217 /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
2218 if (VF_IS_V10(&vf->vf_ver))
2219 info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
2221 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
2222 VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
2223 sizeof(struct virtchnl_version_info));
2227 * ice_vc_get_max_frame_size - get max frame size allowed for VF
2228 * @vf: VF used to determine max frame size
2230 * Max frame size is determined based on the current port's max frame size and
2231 * whether a port VLAN is configured on this VF. The VF is not aware whether
2232 * it's in a port VLAN so the PF needs to account for this in max frame size
2233 * checks and sending the max frame size to the VF.
2235 static u16 ice_vc_get_max_frame_size(struct ice_vf *vf)
2237 struct ice_port_info *pi = ice_vf_get_port_info(vf);
2240 max_frame_size = pi->phy.link_info.max_frame_size;
2242 if (vf->port_vlan_info)
2243 max_frame_size -= VLAN_HLEN;
2245 return max_frame_size;
2249 * ice_vc_get_vf_res_msg
2250 * @vf: pointer to the VF info
2251 * @msg: pointer to the msg buffer
2253 * called from the VF to request its resources
2255 static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
2257 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2258 struct virtchnl_vf_resource *vfres = NULL;
2259 struct ice_pf *pf = vf->pf;
2260 struct ice_vsi *vsi;
2264 if (ice_check_vf_init(pf, vf)) {
2265 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2269 len = sizeof(struct virtchnl_vf_resource);
2271 vfres = kzalloc(len, GFP_KERNEL);
2273 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2277 if (VF_IS_V11(&vf->vf_ver))
2278 vf->driver_caps = *(u32 *)msg;
2280 vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
2281 VIRTCHNL_VF_OFFLOAD_RSS_REG |
2282 VIRTCHNL_VF_OFFLOAD_VLAN;
2284 vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
2285 vsi = ice_get_vf_vsi(vf);
2287 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2291 if (!vsi->info.pvid)
2292 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
2294 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2295 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
2297 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
2298 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
2300 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
2303 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_FDIR_PF)
2304 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_FDIR_PF;
2306 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
2307 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
2309 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
2310 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
2312 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
2313 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
2315 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
2316 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
2318 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2319 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
2321 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
2322 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
2324 if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
2325 vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
2327 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF)
2328 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF;
2330 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_USO)
2331 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_USO;
2333 vfres->num_vsis = 1;
2334 /* Tx and Rx queue are equal for VF */
2335 vfres->num_queue_pairs = vsi->num_txq;
2336 vfres->max_vectors = pf->num_msix_per_vf;
2337 vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
2338 vfres->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
2339 vfres->max_mtu = ice_vc_get_max_frame_size(vf);
2341 vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
2342 vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
2343 vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
2344 ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
2345 vf->hw_lan_addr.addr);
2347 /* match guest capabilities */
2348 vf->driver_caps = vfres->vf_cap_flags;
2350 ice_vc_set_caps_allowlist(vf);
2351 ice_vc_set_working_allowlist(vf);
2353 set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
2356 /* send the response back to the VF */
2357 ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
2365 * ice_vc_reset_vf_msg
2366 * @vf: pointer to the VF info
2368 * called from the VF to reset itself,
2369 * unlike other virtchnl messages, PF driver
2370 * doesn't send the response back to the VF
2372 static void ice_vc_reset_vf_msg(struct ice_vf *vf)
2374 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
2375 ice_reset_vf(vf, false);
2379 * ice_find_vsi_from_id
2380 * @pf: the PF structure to search for the VSI
2381 * @id: ID of the VSI it is searching for
2383 * searches for the VSI with the given ID
2385 static struct ice_vsi *ice_find_vsi_from_id(struct ice_pf *pf, u16 id)
2389 ice_for_each_vsi(pf, i)
2390 if (pf->vsi[i] && pf->vsi[i]->vsi_num == id)
2397 * ice_vc_isvalid_vsi_id
2398 * @vf: pointer to the VF info
2399 * @vsi_id: VF relative VSI ID
2401 * check for the valid VSI ID
2403 bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
2405 struct ice_pf *pf = vf->pf;
2406 struct ice_vsi *vsi;
2408 vsi = ice_find_vsi_from_id(pf, vsi_id);
2410 return (vsi && (vsi->vf_id == vf->vf_id));
2414 * ice_vc_isvalid_q_id
2415 * @vf: pointer to the VF info
2417 * @qid: VSI relative queue ID
2419 * check for the valid queue ID
2421 static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
2423 struct ice_vsi *vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
2424 /* allocated Tx and Rx queues should be always equal for VF VSI */
2425 return (vsi && (qid < vsi->alloc_txq));
2429 * ice_vc_isvalid_ring_len
2430 * @ring_len: length of ring
2432 * check for the valid ring count, should be multiple of ICE_REQ_DESC_MULTIPLE
2435 static bool ice_vc_isvalid_ring_len(u16 ring_len)
2437 return ring_len == 0 ||
2438 (ring_len >= ICE_MIN_NUM_DESC &&
2439 ring_len <= ICE_MAX_NUM_DESC &&
2440 !(ring_len % ICE_REQ_DESC_MULTIPLE));
2444 * ice_vc_validate_pattern
2445 * @vf: pointer to the VF info
2446 * @proto: virtchnl protocol headers
2448 * validate the pattern is supported or not.
2450 * Return: true on success, false on error.
2453 ice_vc_validate_pattern(struct ice_vf *vf, struct virtchnl_proto_hdrs *proto)
2455 bool is_ipv4 = false;
2456 bool is_ipv6 = false;
2457 bool is_udp = false;
2461 while (i < proto->count &&
2462 proto->proto_hdr[i].type != VIRTCHNL_PROTO_HDR_NONE) {
2463 switch (proto->proto_hdr[i].type) {
2464 case VIRTCHNL_PROTO_HDR_ETH:
2465 ptype = ICE_PTYPE_MAC_PAY;
2467 case VIRTCHNL_PROTO_HDR_IPV4:
2468 ptype = ICE_PTYPE_IPV4_PAY;
2471 case VIRTCHNL_PROTO_HDR_IPV6:
2472 ptype = ICE_PTYPE_IPV6_PAY;
2475 case VIRTCHNL_PROTO_HDR_UDP:
2477 ptype = ICE_PTYPE_IPV4_UDP_PAY;
2479 ptype = ICE_PTYPE_IPV6_UDP_PAY;
2482 case VIRTCHNL_PROTO_HDR_TCP:
2484 ptype = ICE_PTYPE_IPV4_TCP_PAY;
2486 ptype = ICE_PTYPE_IPV6_TCP_PAY;
2488 case VIRTCHNL_PROTO_HDR_SCTP:
2490 ptype = ICE_PTYPE_IPV4_SCTP_PAY;
2492 ptype = ICE_PTYPE_IPV6_SCTP_PAY;
2494 case VIRTCHNL_PROTO_HDR_GTPU_IP:
2495 case VIRTCHNL_PROTO_HDR_GTPU_EH:
2497 ptype = ICE_MAC_IPV4_GTPU;
2499 ptype = ICE_MAC_IPV6_GTPU;
2501 case VIRTCHNL_PROTO_HDR_L2TPV3:
2503 ptype = ICE_MAC_IPV4_L2TPV3;
2505 ptype = ICE_MAC_IPV6_L2TPV3;
2507 case VIRTCHNL_PROTO_HDR_ESP:
2509 ptype = is_udp ? ICE_MAC_IPV4_NAT_T_ESP :
2512 ptype = is_udp ? ICE_MAC_IPV6_NAT_T_ESP :
2515 case VIRTCHNL_PROTO_HDR_AH:
2517 ptype = ICE_MAC_IPV4_AH;
2519 ptype = ICE_MAC_IPV6_AH;
2521 case VIRTCHNL_PROTO_HDR_PFCP:
2523 ptype = ICE_MAC_IPV4_PFCP_SESSION;
2525 ptype = ICE_MAC_IPV6_PFCP_SESSION;
2534 return ice_hw_ptype_ena(&vf->pf->hw, ptype);
2538 * ice_vc_parse_rss_cfg - parses hash fields and headers from
2539 * a specific virtchnl RSS cfg
2540 * @hw: pointer to the hardware
2541 * @rss_cfg: pointer to the virtchnl RSS cfg
2542 * @addl_hdrs: pointer to the protocol header fields (ICE_FLOW_SEG_HDR_*)
2544 * @hash_flds: pointer to the hash bit fields (ICE_FLOW_HASH_*) to configure
2546 * Return true if all the protocol header and hash fields in the RSS cfg could
2547 * be parsed, else return false
2549 * This function parses the virtchnl RSS cfg to be the intended
2550 * hash fields and the intended header for RSS configuration
2553 ice_vc_parse_rss_cfg(struct ice_hw *hw, struct virtchnl_rss_cfg *rss_cfg,
2554 u32 *addl_hdrs, u64 *hash_flds)
2556 const struct ice_vc_hash_field_match_type *hf_list;
2557 const struct ice_vc_hdr_match_type *hdr_list;
2558 int i, hf_list_len, hdr_list_len;
2560 hf_list = ice_vc_hash_field_list;
2561 hf_list_len = ARRAY_SIZE(ice_vc_hash_field_list);
2562 hdr_list = ice_vc_hdr_list;
2563 hdr_list_len = ARRAY_SIZE(ice_vc_hdr_list);
2565 for (i = 0; i < rss_cfg->proto_hdrs.count; i++) {
2566 struct virtchnl_proto_hdr *proto_hdr =
2567 &rss_cfg->proto_hdrs.proto_hdr[i];
2568 bool hdr_found = false;
2571 /* Find matched ice headers according to virtchnl headers. */
2572 for (j = 0; j < hdr_list_len; j++) {
2573 struct ice_vc_hdr_match_type hdr_map = hdr_list[j];
2575 if (proto_hdr->type == hdr_map.vc_hdr) {
2576 *addl_hdrs |= hdr_map.ice_hdr;
2584 /* Find matched ice hash fields according to
2585 * virtchnl hash fields.
2587 for (j = 0; j < hf_list_len; j++) {
2588 struct ice_vc_hash_field_match_type hf_map = hf_list[j];
2590 if (proto_hdr->type == hf_map.vc_hdr &&
2591 proto_hdr->field_selector == hf_map.vc_hash_field) {
2592 *hash_flds |= hf_map.ice_hash_field;
2602 * ice_vf_adv_rss_offload_ena - determine if capabilities support advanced
2604 * @caps: VF driver negotiated capabilities
2606 * Return true if VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF capability is set,
2609 static bool ice_vf_adv_rss_offload_ena(u32 caps)
2611 return !!(caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF);
2615 * ice_vc_handle_rss_cfg
2616 * @vf: pointer to the VF info
2617 * @msg: pointer to the message buffer
2618 * @add: add a RSS config if true, otherwise delete a RSS config
2620 * This function adds/deletes a RSS config
2622 static int ice_vc_handle_rss_cfg(struct ice_vf *vf, u8 *msg, bool add)
2624 u32 v_opcode = add ? VIRTCHNL_OP_ADD_RSS_CFG : VIRTCHNL_OP_DEL_RSS_CFG;
2625 struct virtchnl_rss_cfg *rss_cfg = (struct virtchnl_rss_cfg *)msg;
2626 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2627 struct device *dev = ice_pf_to_dev(vf->pf);
2628 struct ice_hw *hw = &vf->pf->hw;
2629 struct ice_vsi *vsi;
2631 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2632 dev_dbg(dev, "VF %d attempting to configure RSS, but RSS is not supported by the PF\n",
2634 v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
2638 if (!ice_vf_adv_rss_offload_ena(vf->driver_caps)) {
2639 dev_dbg(dev, "VF %d attempting to configure RSS, but Advanced RSS offload is not supported\n",
2641 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2645 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2646 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2650 if (rss_cfg->proto_hdrs.count > VIRTCHNL_MAX_NUM_PROTO_HDRS ||
2651 rss_cfg->rss_algorithm < VIRTCHNL_RSS_ALG_TOEPLITZ_ASYMMETRIC ||
2652 rss_cfg->rss_algorithm > VIRTCHNL_RSS_ALG_XOR_SYMMETRIC) {
2653 dev_dbg(dev, "VF %d attempting to configure RSS, but RSS configuration is not valid\n",
2655 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2659 vsi = ice_get_vf_vsi(vf);
2661 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2665 if (!ice_vc_validate_pattern(vf, &rss_cfg->proto_hdrs)) {
2666 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2670 if (rss_cfg->rss_algorithm == VIRTCHNL_RSS_ALG_R_ASYMMETRIC) {
2671 struct ice_vsi_ctx *ctx;
2672 u8 lut_type, hash_type;
2675 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
2676 hash_type = add ? ICE_AQ_VSI_Q_OPT_RSS_XOR :
2677 ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
2679 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2681 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2685 ctx->info.q_opt_rss = ((lut_type <<
2686 ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
2687 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
2689 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
2691 /* Preserve existing queueing option setting */
2692 ctx->info.q_opt_rss |= (vsi->info.q_opt_rss &
2693 ICE_AQ_VSI_Q_OPT_RSS_GBL_LUT_M);
2694 ctx->info.q_opt_tc = vsi->info.q_opt_tc;
2695 ctx->info.q_opt_flags = vsi->info.q_opt_rss;
2697 ctx->info.valid_sections =
2698 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
2700 status = ice_update_vsi(hw, vsi->idx, ctx, NULL);
2702 dev_err(dev, "update VSI for RSS failed, err %d aq_err %s\n",
2703 status, ice_aq_str(hw->adminq.sq_last_status));
2704 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2706 vsi->info.q_opt_rss = ctx->info.q_opt_rss;
2711 u32 addl_hdrs = ICE_FLOW_SEG_HDR_NONE;
2712 u64 hash_flds = ICE_HASH_INVALID;
2714 if (!ice_vc_parse_rss_cfg(hw, rss_cfg, &addl_hdrs,
2716 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2721 if (ice_add_rss_cfg(hw, vsi->idx, hash_flds,
2723 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2724 dev_err(dev, "ice_add_rss_cfg failed for vsi = %d, v_ret = %d\n",
2725 vsi->vsi_num, v_ret);
2730 status = ice_rem_rss_cfg(hw, vsi->idx, hash_flds,
2732 /* We just ignore -ENOENT, because if two configurations
2733 * share the same profile remove one of them actually
2734 * removes both, since the profile is deleted.
2736 if (status && status != -ENOENT) {
2737 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2738 dev_err(dev, "ice_rem_rss_cfg failed for VF ID:%d, error:%d\n",
2745 return ice_vc_send_msg_to_vf(vf, v_opcode, v_ret, NULL, 0);
2749 * ice_vc_config_rss_key
2750 * @vf: pointer to the VF info
2751 * @msg: pointer to the msg buffer
2753 * Configure the VF's RSS key
2755 static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
2757 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2758 struct virtchnl_rss_key *vrk =
2759 (struct virtchnl_rss_key *)msg;
2760 struct ice_vsi *vsi;
2762 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2763 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2767 if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
2768 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2772 if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
2773 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2777 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2778 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2782 vsi = ice_get_vf_vsi(vf);
2784 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2788 if (ice_set_rss_key(vsi, vrk->key))
2789 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2791 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
2796 * ice_vc_config_rss_lut
2797 * @vf: pointer to the VF info
2798 * @msg: pointer to the msg buffer
2800 * Configure the VF's RSS LUT
2802 static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
2804 struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
2805 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2806 struct ice_vsi *vsi;
2808 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2809 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2813 if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
2814 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2818 if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE) {
2819 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2823 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2824 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2828 vsi = ice_get_vf_vsi(vf);
2830 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2834 if (ice_set_rss_lut(vsi, vrl->lut, ICE_VSIQF_HLUT_ARRAY_SIZE))
2835 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2837 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
2842 * ice_wait_on_vf_reset - poll to make sure a given VF is ready after reset
2843 * @vf: The VF being resseting
2845 * The max poll time is about ~800ms, which is about the maximum time it takes
2846 * for a VF to be reset and/or a VF driver to be removed.
2848 static void ice_wait_on_vf_reset(struct ice_vf *vf)
2852 for (i = 0; i < ICE_MAX_VF_RESET_TRIES; i++) {
2853 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
2855 msleep(ICE_MAX_VF_RESET_SLEEP_MS);
2860 * ice_check_vf_ready_for_cfg - check if VF is ready to be configured/queried
2861 * @vf: VF to check if it's ready to be configured/queried
2863 * The purpose of this function is to make sure the VF is not in reset, not
2864 * disabled, and initialized so it can be configured and/or queried by a host
2867 int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
2871 ice_wait_on_vf_reset(vf);
2873 if (ice_is_vf_disabled(vf))
2877 if (ice_check_vf_init(pf, vf))
2884 * ice_set_vf_spoofchk
2885 * @netdev: network interface device structure
2886 * @vf_id: VF identifier
2887 * @ena: flag to enable or disable feature
2889 * Enable or disable VF spoof checking
2891 int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
2893 struct ice_netdev_priv *np = netdev_priv(netdev);
2894 struct ice_pf *pf = np->vsi->back;
2895 struct ice_vsi_ctx *ctx;
2896 struct ice_vsi *vf_vsi;
2901 dev = ice_pf_to_dev(pf);
2902 if (ice_validate_vf_id(pf, vf_id))
2905 vf = &pf->vf[vf_id];
2906 ret = ice_check_vf_ready_for_cfg(vf);
2910 vf_vsi = ice_get_vf_vsi(vf);
2912 netdev_err(netdev, "VSI %d for VF %d is null\n",
2913 vf->lan_vsi_idx, vf->vf_id);
2917 if (vf_vsi->type != ICE_VSI_VF) {
2918 netdev_err(netdev, "Type %d of VSI %d for VF %d is no ICE_VSI_VF\n",
2919 vf_vsi->type, vf_vsi->vsi_num, vf->vf_id);
2923 if (ena == vf->spoofchk) {
2924 dev_dbg(dev, "VF spoofchk already %s\n", ena ? "ON" : "OFF");
2928 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2932 ctx->info.sec_flags = vf_vsi->info.sec_flags;
2933 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
2935 ctx->info.sec_flags |=
2936 ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
2937 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2938 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
2940 ctx->info.sec_flags &=
2941 ~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
2942 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2943 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
2946 ret = ice_update_vsi(&pf->hw, vf_vsi->idx, ctx, NULL);
2948 dev_err(dev, "Failed to %sable spoofchk on VF %d VSI %d\n error %d\n",
2949 ena ? "en" : "dis", vf->vf_id, vf_vsi->vsi_num, ret);
2953 /* only update spoofchk state and VSI context on success */
2954 vf_vsi->info.sec_flags = ctx->info.sec_flags;
2963 * ice_is_any_vf_in_promisc - check if any VF(s) are in promiscuous mode
2964 * @pf: PF structure for accessing VF(s)
2966 * Return false if no VF(s) are in unicast and/or multicast promiscuous mode,
2969 bool ice_is_any_vf_in_promisc(struct ice_pf *pf)
2973 ice_for_each_vf(pf, vf_idx) {
2974 struct ice_vf *vf = &pf->vf[vf_idx];
2976 /* found a VF that has promiscuous mode configured */
2977 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2978 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
2986 * ice_vc_cfg_promiscuous_mode_msg
2987 * @vf: pointer to the VF info
2988 * @msg: pointer to the msg buffer
2990 * called from the VF to configure VF VSIs promiscuous mode
2992 static int ice_vc_cfg_promiscuous_mode_msg(struct ice_vf *vf, u8 *msg)
2994 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2995 bool rm_promisc, alluni = false, allmulti = false;
2996 struct virtchnl_promisc_info *info =
2997 (struct virtchnl_promisc_info *)msg;
2998 int mcast_err = 0, ucast_err = 0;
2999 struct ice_pf *pf = vf->pf;
3000 struct ice_vsi *vsi;
3004 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3005 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3009 if (!ice_vc_isvalid_vsi_id(vf, info->vsi_id)) {
3010 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3014 vsi = ice_get_vf_vsi(vf);
3016 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3020 dev = ice_pf_to_dev(pf);
3021 if (!test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
3022 dev_err(dev, "Unprivileged VF %d is attempting to configure promiscuous mode\n",
3024 /* Leave v_ret alone, lie to the VF on purpose. */
3028 if (info->flags & FLAG_VF_UNICAST_PROMISC)
3031 if (info->flags & FLAG_VF_MULTICAST_PROMISC)
3034 rm_promisc = !allmulti && !alluni;
3036 if (vsi->num_vlan || vf->port_vlan_info) {
3038 ret = ice_cfg_vlan_pruning(vsi, true);
3040 ret = ice_cfg_vlan_pruning(vsi, false);
3042 dev_err(dev, "Failed to configure VLAN pruning in promiscuous mode\n");
3043 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3048 if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
3049 bool set_dflt_vsi = alluni || allmulti;
3051 if (set_dflt_vsi && !ice_is_dflt_vsi_in_use(pf->first_sw))
3052 /* only attempt to set the default forwarding VSI if
3053 * it's not currently set
3055 ret = ice_set_dflt_vsi(pf->first_sw, vsi);
3056 else if (!set_dflt_vsi &&
3057 ice_is_vsi_dflt_vsi(pf->first_sw, vsi))
3058 /* only attempt to free the default forwarding VSI if we
3061 ret = ice_clear_dflt_vsi(pf->first_sw);
3064 dev_err(dev, "%sable VF %d as the default VSI failed, error %d\n",
3065 set_dflt_vsi ? "en" : "dis", vf->vf_id, ret);
3066 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
3070 u8 mcast_m, ucast_m;
3072 if (vf->port_vlan_info || vsi->num_vlan > 1) {
3073 mcast_m = ICE_MCAST_VLAN_PROMISC_BITS;
3074 ucast_m = ICE_UCAST_VLAN_PROMISC_BITS;
3076 mcast_m = ICE_MCAST_PROMISC_BITS;
3077 ucast_m = ICE_UCAST_PROMISC_BITS;
3081 ucast_err = ice_vf_set_vsi_promisc(vf, vsi, ucast_m);
3083 ucast_err = ice_vf_clear_vsi_promisc(vf, vsi, ucast_m);
3086 mcast_err = ice_vf_set_vsi_promisc(vf, vsi, mcast_m);
3088 mcast_err = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
3090 if (ucast_err || mcast_err)
3091 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3096 !test_and_set_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
3097 dev_info(dev, "VF %u successfully set multicast promiscuous mode\n",
3099 else if (!allmulti && test_and_clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
3100 dev_info(dev, "VF %u successfully unset multicast promiscuous mode\n",
3105 if (alluni && !test_and_set_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
3106 dev_info(dev, "VF %u successfully set unicast promiscuous mode\n",
3108 else if (!alluni && test_and_clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
3109 dev_info(dev, "VF %u successfully unset unicast promiscuous mode\n",
3114 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
3119 * ice_vc_get_stats_msg
3120 * @vf: pointer to the VF info
3121 * @msg: pointer to the msg buffer
3123 * called from the VF to get VSI stats
3125 static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
3127 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3128 struct virtchnl_queue_select *vqs =
3129 (struct virtchnl_queue_select *)msg;
3130 struct ice_eth_stats stats = { 0 };
3131 struct ice_vsi *vsi;
3133 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3134 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3138 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3139 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3143 vsi = ice_get_vf_vsi(vf);
3145 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3149 ice_update_eth_stats(vsi);
3151 stats = vsi->eth_stats;
3154 /* send the response to the VF */
3155 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
3156 (u8 *)&stats, sizeof(stats));
3160 * ice_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTCHNL
3161 * @vqs: virtchnl_queue_select structure containing bitmaps to validate
3163 * Return true on successful validation, else false
3165 static bool ice_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
3167 if ((!vqs->rx_queues && !vqs->tx_queues) ||
3168 vqs->rx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF) ||
3169 vqs->tx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF))
3176 * ice_vf_ena_txq_interrupt - enable Tx queue interrupt via QINT_TQCTL
3177 * @vsi: VSI of the VF to configure
3178 * @q_idx: VF queue index used to determine the queue in the PF's space
3180 static void ice_vf_ena_txq_interrupt(struct ice_vsi *vsi, u32 q_idx)
3182 struct ice_hw *hw = &vsi->back->hw;
3183 u32 pfq = vsi->txq_map[q_idx];
3186 reg = rd32(hw, QINT_TQCTL(pfq));
3188 /* MSI-X index 0 in the VF's space is always for the OICR, which means
3189 * this is most likely a poll mode VF driver, so don't enable an
3190 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
3192 if (!(reg & QINT_TQCTL_MSIX_INDX_M))
3195 wr32(hw, QINT_TQCTL(pfq), reg | QINT_TQCTL_CAUSE_ENA_M);
3199 * ice_vf_ena_rxq_interrupt - enable Tx queue interrupt via QINT_RQCTL
3200 * @vsi: VSI of the VF to configure
3201 * @q_idx: VF queue index used to determine the queue in the PF's space
3203 static void ice_vf_ena_rxq_interrupt(struct ice_vsi *vsi, u32 q_idx)
3205 struct ice_hw *hw = &vsi->back->hw;
3206 u32 pfq = vsi->rxq_map[q_idx];
3209 reg = rd32(hw, QINT_RQCTL(pfq));
3211 /* MSI-X index 0 in the VF's space is always for the OICR, which means
3212 * this is most likely a poll mode VF driver, so don't enable an
3213 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
3215 if (!(reg & QINT_RQCTL_MSIX_INDX_M))
3218 wr32(hw, QINT_RQCTL(pfq), reg | QINT_RQCTL_CAUSE_ENA_M);
3223 * @vf: pointer to the VF info
3224 * @msg: pointer to the msg buffer
3226 * called from the VF to enable all or specific queue(s)
3228 static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
3230 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3231 struct virtchnl_queue_select *vqs =
3232 (struct virtchnl_queue_select *)msg;
3233 struct ice_vsi *vsi;
3234 unsigned long q_map;
3237 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3238 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3242 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3243 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3247 if (!ice_vc_validate_vqs_bitmaps(vqs)) {
3248 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3252 vsi = ice_get_vf_vsi(vf);
3254 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3258 /* Enable only Rx rings, Tx rings were enabled by the FW when the
3259 * Tx queue group list was configured and the context bits were
3260 * programmed using ice_vsi_cfg_txqs
3262 q_map = vqs->rx_queues;
3263 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3264 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3265 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3269 /* Skip queue if enabled */
3270 if (test_bit(vf_q_id, vf->rxq_ena))
3273 if (ice_vsi_ctrl_one_rx_ring(vsi, true, vf_q_id, true)) {
3274 dev_err(ice_pf_to_dev(vsi->back), "Failed to enable Rx ring %d on VSI %d\n",
3275 vf_q_id, vsi->vsi_num);
3276 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3280 ice_vf_ena_rxq_interrupt(vsi, vf_q_id);
3281 set_bit(vf_q_id, vf->rxq_ena);
3284 q_map = vqs->tx_queues;
3285 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3286 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3287 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3291 /* Skip queue if enabled */
3292 if (test_bit(vf_q_id, vf->txq_ena))
3295 ice_vf_ena_txq_interrupt(vsi, vf_q_id);
3296 set_bit(vf_q_id, vf->txq_ena);
3299 /* Set flag to indicate that queues are enabled */
3300 if (v_ret == VIRTCHNL_STATUS_SUCCESS)
3301 set_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
3304 /* send the response to the VF */
3305 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
3311 * @vf: pointer to the VF info
3312 * @msg: pointer to the msg buffer
3314 * called from the VF to disable all or specific
3317 static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
3319 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3320 struct virtchnl_queue_select *vqs =
3321 (struct virtchnl_queue_select *)msg;
3322 struct ice_vsi *vsi;
3323 unsigned long q_map;
3326 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
3327 !test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states)) {
3328 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3332 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3333 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3337 if (!ice_vc_validate_vqs_bitmaps(vqs)) {
3338 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3342 vsi = ice_get_vf_vsi(vf);
3344 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3348 if (vqs->tx_queues) {
3349 q_map = vqs->tx_queues;
3351 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3352 struct ice_tx_ring *ring = vsi->tx_rings[vf_q_id];
3353 struct ice_txq_meta txq_meta = { 0 };
3355 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3356 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3360 /* Skip queue if not enabled */
3361 if (!test_bit(vf_q_id, vf->txq_ena))
3364 ice_fill_txq_meta(vsi, ring, &txq_meta);
3366 if (ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, vf->vf_id,
3368 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Tx ring %d on VSI %d\n",
3369 vf_q_id, vsi->vsi_num);
3370 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3374 /* Clear enabled queues flag */
3375 clear_bit(vf_q_id, vf->txq_ena);
3379 q_map = vqs->rx_queues;
3380 /* speed up Rx queue disable by batching them if possible */
3382 bitmap_equal(&q_map, vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF)) {
3383 if (ice_vsi_stop_all_rx_rings(vsi)) {
3384 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop all Rx rings on VSI %d\n",
3386 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3390 bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
3392 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3393 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3394 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3398 /* Skip queue if not enabled */
3399 if (!test_bit(vf_q_id, vf->rxq_ena))
3402 if (ice_vsi_ctrl_one_rx_ring(vsi, false, vf_q_id,
3404 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Rx ring %d on VSI %d\n",
3405 vf_q_id, vsi->vsi_num);
3406 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3410 /* Clear enabled queues flag */
3411 clear_bit(vf_q_id, vf->rxq_ena);
3415 /* Clear enabled queues flag */
3416 if (v_ret == VIRTCHNL_STATUS_SUCCESS && ice_vf_has_no_qs_ena(vf))
3417 clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
3420 /* send the response to the VF */
3421 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
3427 * @vf: pointer to the VF info
3428 * @vsi: the VSI being configured
3429 * @vector_id: vector ID
3430 * @map: vector map for mapping vectors to queues
3431 * @q_vector: structure for interrupt vector
3432 * configure the IRQ to queue map
3435 ice_cfg_interrupt(struct ice_vf *vf, struct ice_vsi *vsi, u16 vector_id,
3436 struct virtchnl_vector_map *map,
3437 struct ice_q_vector *q_vector)
3439 u16 vsi_q_id, vsi_q_id_idx;
3442 q_vector->num_ring_rx = 0;
3443 q_vector->num_ring_tx = 0;
3445 qmap = map->rxq_map;
3446 for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
3447 vsi_q_id = vsi_q_id_idx;
3449 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
3450 return VIRTCHNL_STATUS_ERR_PARAM;
3452 q_vector->num_ring_rx++;
3453 q_vector->rx.itr_idx = map->rxitr_idx;
3454 vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
3455 ice_cfg_rxq_interrupt(vsi, vsi_q_id, vector_id,
3456 q_vector->rx.itr_idx);
3459 qmap = map->txq_map;
3460 for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
3461 vsi_q_id = vsi_q_id_idx;
3463 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
3464 return VIRTCHNL_STATUS_ERR_PARAM;
3466 q_vector->num_ring_tx++;
3467 q_vector->tx.itr_idx = map->txitr_idx;
3468 vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
3469 ice_cfg_txq_interrupt(vsi, vsi_q_id, vector_id,
3470 q_vector->tx.itr_idx);
3473 return VIRTCHNL_STATUS_SUCCESS;
3477 * ice_vc_cfg_irq_map_msg
3478 * @vf: pointer to the VF info
3479 * @msg: pointer to the msg buffer
3481 * called from the VF to configure the IRQ to queue map
3483 static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
3485 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3486 u16 num_q_vectors_mapped, vsi_id, vector_id;
3487 struct virtchnl_irq_map_info *irqmap_info;
3488 struct virtchnl_vector_map *map;
3489 struct ice_pf *pf = vf->pf;
3490 struct ice_vsi *vsi;
3493 irqmap_info = (struct virtchnl_irq_map_info *)msg;
3494 num_q_vectors_mapped = irqmap_info->num_vectors;
3496 /* Check to make sure number of VF vectors mapped is not greater than
3497 * number of VF vectors originally allocated, and check that
3498 * there is actually at least a single VF queue vector mapped
3500 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
3501 pf->num_msix_per_vf < num_q_vectors_mapped ||
3502 !num_q_vectors_mapped) {
3503 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3507 vsi = ice_get_vf_vsi(vf);
3509 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3513 for (i = 0; i < num_q_vectors_mapped; i++) {
3514 struct ice_q_vector *q_vector;
3516 map = &irqmap_info->vecmap[i];
3518 vector_id = map->vector_id;
3519 vsi_id = map->vsi_id;
3520 /* vector_id is always 0-based for each VF, and can never be
3521 * larger than or equal to the max allowed interrupts per VF
3523 if (!(vector_id < pf->num_msix_per_vf) ||
3524 !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
3525 (!vector_id && (map->rxq_map || map->txq_map))) {
3526 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3530 /* No need to map VF miscellaneous or rogue vector */
3534 /* Subtract non queue vector from vector_id passed by VF
3535 * to get actual number of VSI queue vector array index
3537 q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
3539 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3543 /* lookout for the invalid queue index */
3544 v_ret = (enum virtchnl_status_code)
3545 ice_cfg_interrupt(vf, vsi, vector_id, map, q_vector);
3551 /* send the response to the VF */
3552 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
3558 * @vf: pointer to the VF info
3559 * @msg: pointer to the msg buffer
3561 * called from the VF to configure the Rx/Tx queues
3563 static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
3565 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3566 struct virtchnl_vsi_queue_config_info *qci =
3567 (struct virtchnl_vsi_queue_config_info *)msg;
3568 struct virtchnl_queue_pair_info *qpi;
3569 struct ice_pf *pf = vf->pf;
3570 struct ice_vsi *vsi;
3573 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3574 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3578 if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
3579 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3583 vsi = ice_get_vf_vsi(vf);
3585 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3589 if (qci->num_queue_pairs > ICE_MAX_RSS_QS_PER_VF ||
3590 qci->num_queue_pairs > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
3591 dev_err(ice_pf_to_dev(pf), "VF-%d requesting more than supported number of queues: %d\n",
3592 vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
3593 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3597 for (i = 0; i < qci->num_queue_pairs; i++) {
3598 qpi = &qci->qpair[i];
3599 if (qpi->txq.vsi_id != qci->vsi_id ||
3600 qpi->rxq.vsi_id != qci->vsi_id ||
3601 qpi->rxq.queue_id != qpi->txq.queue_id ||
3602 qpi->txq.headwb_enabled ||
3603 !ice_vc_isvalid_ring_len(qpi->txq.ring_len) ||
3604 !ice_vc_isvalid_ring_len(qpi->rxq.ring_len) ||
3605 !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
3606 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3610 q_idx = qpi->rxq.queue_id;
3612 /* make sure selected "q_idx" is in valid range of queues
3613 * for selected "vsi"
3615 if (q_idx >= vsi->alloc_txq || q_idx >= vsi->alloc_rxq) {
3616 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3620 /* copy Tx queue info from VF into VSI */
3621 if (qpi->txq.ring_len > 0) {
3622 vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
3623 vsi->tx_rings[i]->count = qpi->txq.ring_len;
3624 if (ice_vsi_cfg_single_txq(vsi, vsi->tx_rings, q_idx)) {
3625 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3630 /* copy Rx queue info from VF into VSI */
3631 if (qpi->rxq.ring_len > 0) {
3632 u16 max_frame_size = ice_vc_get_max_frame_size(vf);
3634 vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
3635 vsi->rx_rings[i]->count = qpi->rxq.ring_len;
3637 if (qpi->rxq.databuffer_size != 0 &&
3638 (qpi->rxq.databuffer_size > ((16 * 1024) - 128) ||
3639 qpi->rxq.databuffer_size < 1024)) {
3640 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3643 vsi->rx_buf_len = qpi->rxq.databuffer_size;
3644 vsi->rx_rings[i]->rx_buf_len = vsi->rx_buf_len;
3645 if (qpi->rxq.max_pkt_size > max_frame_size ||
3646 qpi->rxq.max_pkt_size < 64) {
3647 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3651 vsi->max_frame = qpi->rxq.max_pkt_size;
3652 /* add space for the port VLAN since the VF driver is not
3653 * expected to account for it in the MTU calculation
3655 if (vf->port_vlan_info)
3656 vsi->max_frame += VLAN_HLEN;
3658 if (ice_vsi_cfg_single_rxq(vsi, q_idx)) {
3659 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3666 /* send the response to the VF */
3667 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES, v_ret,
3673 * @vf: pointer to the VF info
3675 static bool ice_is_vf_trusted(struct ice_vf *vf)
3677 return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
3681 * ice_can_vf_change_mac
3682 * @vf: pointer to the VF info
3684 * Return true if the VF is allowed to change its MAC filters, false otherwise
3686 static bool ice_can_vf_change_mac(struct ice_vf *vf)
3688 /* If the VF MAC address has been set administratively (via the
3689 * ndo_set_vf_mac command), then deny permission to the VF to
3690 * add/delete unicast MAC addresses, unless the VF is trusted
3692 if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
3699 * ice_vc_ether_addr_type - get type of virtchnl_ether_addr
3700 * @vc_ether_addr: used to extract the type
3703 ice_vc_ether_addr_type(struct virtchnl_ether_addr *vc_ether_addr)
3705 return (vc_ether_addr->type & VIRTCHNL_ETHER_ADDR_TYPE_MASK);
3709 * ice_is_vc_addr_legacy - check if the MAC address is from an older VF
3710 * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
3713 ice_is_vc_addr_legacy(struct virtchnl_ether_addr *vc_ether_addr)
3715 u8 type = ice_vc_ether_addr_type(vc_ether_addr);
3717 return (type == VIRTCHNL_ETHER_ADDR_LEGACY);
3721 * ice_is_vc_addr_primary - check if the MAC address is the VF's primary MAC
3722 * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
3724 * This function should only be called when the MAC address in
3725 * virtchnl_ether_addr is a valid unicast MAC
3728 ice_is_vc_addr_primary(struct virtchnl_ether_addr __maybe_unused *vc_ether_addr)
3730 u8 type = ice_vc_ether_addr_type(vc_ether_addr);
3732 return (type == VIRTCHNL_ETHER_ADDR_PRIMARY);
3736 * ice_vfhw_mac_add - update the VF's cached hardware MAC if allowed
3738 * @vc_ether_addr: structure from VIRTCHNL with MAC to add
3741 ice_vfhw_mac_add(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
3743 u8 *mac_addr = vc_ether_addr->addr;
3745 if (!is_valid_ether_addr(mac_addr))
3748 /* only allow legacy VF drivers to set the device and hardware MAC if it
3749 * is zero and allow new VF drivers to set the hardware MAC if the type
3750 * was correctly specified over VIRTCHNL
3752 if ((ice_is_vc_addr_legacy(vc_ether_addr) &&
3753 is_zero_ether_addr(vf->hw_lan_addr.addr)) ||
3754 ice_is_vc_addr_primary(vc_ether_addr)) {
3755 ether_addr_copy(vf->dev_lan_addr.addr, mac_addr);
3756 ether_addr_copy(vf->hw_lan_addr.addr, mac_addr);
3759 /* hardware and device MACs are already set, but its possible that the
3760 * VF driver sent the VIRTCHNL_OP_ADD_ETH_ADDR message before the
3761 * VIRTCHNL_OP_DEL_ETH_ADDR when trying to update its MAC, so save it
3762 * away for the legacy VF driver case as it will be updated in the
3763 * delete flow for this case
3765 if (ice_is_vc_addr_legacy(vc_ether_addr)) {
3766 ether_addr_copy(vf->legacy_last_added_umac.addr,
3768 vf->legacy_last_added_umac.time_modified = jiffies;
3773 * ice_vc_add_mac_addr - attempt to add the MAC address passed in
3774 * @vf: pointer to the VF info
3775 * @vsi: pointer to the VF's VSI
3776 * @vc_ether_addr: VIRTCHNL MAC address structure used to add MAC
3779 ice_vc_add_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
3780 struct virtchnl_ether_addr *vc_ether_addr)
3782 struct device *dev = ice_pf_to_dev(vf->pf);
3783 u8 *mac_addr = vc_ether_addr->addr;
3786 /* device MAC already added */
3787 if (ether_addr_equal(mac_addr, vf->dev_lan_addr.addr))
3790 if (is_unicast_ether_addr(mac_addr) && !ice_can_vf_change_mac(vf)) {
3791 dev_err(dev, "VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
3795 ret = ice_fltr_add_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
3796 if (ret == -EEXIST) {
3797 dev_dbg(dev, "MAC %pM already exists for VF %d\n", mac_addr,
3799 /* don't return since we might need to update
3800 * the primary MAC in ice_vfhw_mac_add() below
3803 dev_err(dev, "Failed to add MAC %pM for VF %d\n, error %d\n",
3804 mac_addr, vf->vf_id, ret);
3810 ice_vfhw_mac_add(vf, vc_ether_addr);
3816 * ice_is_legacy_umac_expired - check if last added legacy unicast MAC expired
3817 * @last_added_umac: structure used to check expiration
3819 static bool ice_is_legacy_umac_expired(struct ice_time_mac *last_added_umac)
3821 #define ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME msecs_to_jiffies(3000)
3822 return time_is_before_jiffies(last_added_umac->time_modified +
3823 ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME);
3827 * ice_update_legacy_cached_mac - update cached hardware MAC for legacy VF
3829 * @vc_ether_addr: structure from VIRTCHNL with MAC to check
3831 * only update cached hardware MAC for legacy VF drivers on delete
3832 * because we cannot guarantee order/type of MAC from the VF driver
3835 ice_update_legacy_cached_mac(struct ice_vf *vf,
3836 struct virtchnl_ether_addr *vc_ether_addr)
3838 if (!ice_is_vc_addr_legacy(vc_ether_addr) ||
3839 ice_is_legacy_umac_expired(&vf->legacy_last_added_umac))
3842 ether_addr_copy(vf->dev_lan_addr.addr, vf->legacy_last_added_umac.addr);
3843 ether_addr_copy(vf->hw_lan_addr.addr, vf->legacy_last_added_umac.addr);
3847 * ice_vfhw_mac_del - update the VF's cached hardware MAC if allowed
3849 * @vc_ether_addr: structure from VIRTCHNL with MAC to delete
3852 ice_vfhw_mac_del(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
3854 u8 *mac_addr = vc_ether_addr->addr;
3856 if (!is_valid_ether_addr(mac_addr) ||
3857 !ether_addr_equal(vf->dev_lan_addr.addr, mac_addr))
3860 /* allow the device MAC to be repopulated in the add flow and don't
3861 * clear the hardware MAC (i.e. hw_lan_addr.addr) here as that is meant
3862 * to be persistent on VM reboot and across driver unload/load, which
3863 * won't work if we clear the hardware MAC here
3865 eth_zero_addr(vf->dev_lan_addr.addr);
3867 ice_update_legacy_cached_mac(vf, vc_ether_addr);
3871 * ice_vc_del_mac_addr - attempt to delete the MAC address passed in
3872 * @vf: pointer to the VF info
3873 * @vsi: pointer to the VF's VSI
3874 * @vc_ether_addr: VIRTCHNL MAC address structure used to delete MAC
3877 ice_vc_del_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
3878 struct virtchnl_ether_addr *vc_ether_addr)
3880 struct device *dev = ice_pf_to_dev(vf->pf);
3881 u8 *mac_addr = vc_ether_addr->addr;
3884 if (!ice_can_vf_change_mac(vf) &&
3885 ether_addr_equal(vf->dev_lan_addr.addr, mac_addr))
3888 status = ice_fltr_remove_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
3889 if (status == -ENOENT) {
3890 dev_err(dev, "MAC %pM does not exist for VF %d\n", mac_addr,
3893 } else if (status) {
3894 dev_err(dev, "Failed to delete MAC %pM for VF %d, error %d\n",
3895 mac_addr, vf->vf_id, status);
3899 ice_vfhw_mac_del(vf, vc_ether_addr);
3907 * ice_vc_handle_mac_addr_msg
3908 * @vf: pointer to the VF info
3909 * @msg: pointer to the msg buffer
3910 * @set: true if MAC filters are being set, false otherwise
3912 * add guest MAC address filter
3915 ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
3917 int (*ice_vc_cfg_mac)
3918 (struct ice_vf *vf, struct ice_vsi *vsi,
3919 struct virtchnl_ether_addr *virtchnl_ether_addr);
3920 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3921 struct virtchnl_ether_addr_list *al =
3922 (struct virtchnl_ether_addr_list *)msg;
3923 struct ice_pf *pf = vf->pf;
3924 enum virtchnl_ops vc_op;
3925 struct ice_vsi *vsi;
3929 vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
3930 ice_vc_cfg_mac = ice_vc_add_mac_addr;
3932 vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
3933 ice_vc_cfg_mac = ice_vc_del_mac_addr;
3936 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
3937 !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
3938 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3939 goto handle_mac_exit;
3942 /* If this VF is not privileged, then we can't add more than a
3943 * limited number of addresses. Check to make sure that the
3944 * additions do not push us over the limit.
3946 if (set && !ice_is_vf_trusted(vf) &&
3947 (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
3948 dev_err(ice_pf_to_dev(pf), "Can't add more MAC addresses, because VF-%d is not trusted, switch the VF to trusted mode in order to add more functionalities\n",
3950 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3951 goto handle_mac_exit;
3954 vsi = ice_get_vf_vsi(vf);
3956 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3957 goto handle_mac_exit;
3960 for (i = 0; i < al->num_elements; i++) {
3961 u8 *mac_addr = al->list[i].addr;
3964 if (is_broadcast_ether_addr(mac_addr) ||
3965 is_zero_ether_addr(mac_addr))
3968 result = ice_vc_cfg_mac(vf, vsi, &al->list[i]);
3969 if (result == -EEXIST || result == -ENOENT) {
3971 } else if (result) {
3972 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
3973 goto handle_mac_exit;
3978 /* send the response to the VF */
3979 return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
3983 * ice_vc_add_mac_addr_msg
3984 * @vf: pointer to the VF info
3985 * @msg: pointer to the msg buffer
3987 * add guest MAC address filter
3989 static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
3991 return ice_vc_handle_mac_addr_msg(vf, msg, true);
3995 * ice_vc_del_mac_addr_msg
3996 * @vf: pointer to the VF info
3997 * @msg: pointer to the msg buffer
3999 * remove guest MAC address filter
4001 static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
4003 return ice_vc_handle_mac_addr_msg(vf, msg, false);
4007 * ice_vc_request_qs_msg
4008 * @vf: pointer to the VF info
4009 * @msg: pointer to the msg buffer
4011 * VFs get a default number of queues but can use this message to request a
4012 * different number. If the request is successful, PF will reset the VF and
4013 * return 0. If unsuccessful, PF will send message informing VF of number of
4014 * available queue pairs via virtchnl message response to VF.
4016 static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
4018 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4019 struct virtchnl_vf_res_request *vfres =
4020 (struct virtchnl_vf_res_request *)msg;
4021 u16 req_queues = vfres->num_queue_pairs;
4022 struct ice_pf *pf = vf->pf;
4023 u16 max_allowed_vf_queues;
4024 u16 tx_rx_queue_left;
4028 dev = ice_pf_to_dev(pf);
4029 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4030 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4034 cur_queues = vf->num_vf_qs;
4035 tx_rx_queue_left = min_t(u16, ice_get_avail_txq_count(pf),
4036 ice_get_avail_rxq_count(pf));
4037 max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
4039 dev_err(dev, "VF %d tried to request 0 queues. Ignoring.\n",
4041 } else if (req_queues > ICE_MAX_RSS_QS_PER_VF) {
4042 dev_err(dev, "VF %d tried to request more than %d queues.\n",
4043 vf->vf_id, ICE_MAX_RSS_QS_PER_VF);
4044 vfres->num_queue_pairs = ICE_MAX_RSS_QS_PER_VF;
4045 } else if (req_queues > cur_queues &&
4046 req_queues - cur_queues > tx_rx_queue_left) {
4047 dev_warn(dev, "VF %d requested %u more queues, but only %u left.\n",
4048 vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
4049 vfres->num_queue_pairs = min_t(u16, max_allowed_vf_queues,
4050 ICE_MAX_RSS_QS_PER_VF);
4052 /* request is successful, then reset VF */
4053 vf->num_req_qs = req_queues;
4054 ice_vc_reset_vf(vf);
4055 dev_info(dev, "VF %d granted request of %u queues.\n",
4056 vf->vf_id, req_queues);
4061 /* send the response to the VF */
4062 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
4063 v_ret, (u8 *)vfres, sizeof(*vfres));
4067 * ice_set_vf_port_vlan
4068 * @netdev: network interface device structure
4069 * @vf_id: VF identifier
4070 * @vlan_id: VLAN ID being set
4071 * @qos: priority setting
4072 * @vlan_proto: VLAN protocol
4074 * program VF Port VLAN ID and/or QoS
4077 ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
4080 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4086 dev = ice_pf_to_dev(pf);
4087 if (ice_validate_vf_id(pf, vf_id))
4090 if (vlan_id >= VLAN_N_VID || qos > 7) {
4091 dev_err(dev, "Invalid Port VLAN parameters for VF %d, ID %d, QoS %d\n",
4092 vf_id, vlan_id, qos);
4096 if (vlan_proto != htons(ETH_P_8021Q)) {
4097 dev_err(dev, "VF VLAN protocol is not supported\n");
4098 return -EPROTONOSUPPORT;
4101 vf = &pf->vf[vf_id];
4102 ret = ice_check_vf_ready_for_cfg(vf);
4106 vlanprio = vlan_id | (qos << VLAN_PRIO_SHIFT);
4108 if (vf->port_vlan_info == vlanprio) {
4109 /* duplicate request, so just return success */
4110 dev_dbg(dev, "Duplicate pvid %d request\n", vlanprio);
4114 mutex_lock(&vf->cfg_lock);
4116 vf->port_vlan_info = vlanprio;
4118 if (vf->port_vlan_info)
4119 dev_info(dev, "Setting VLAN %d, QoS 0x%x on VF %d\n",
4120 vlan_id, qos, vf_id);
4122 dev_info(dev, "Clearing port VLAN on VF %d\n", vf_id);
4124 ice_vc_reset_vf(vf);
4125 mutex_unlock(&vf->cfg_lock);
4131 * ice_vf_vlan_offload_ena - determine if capabilities support VLAN offloads
4132 * @caps: VF driver negotiated capabilities
4134 * Return true if VIRTCHNL_VF_OFFLOAD_VLAN capability is set, else return false
4136 static bool ice_vf_vlan_offload_ena(u32 caps)
4138 return !!(caps & VIRTCHNL_VF_OFFLOAD_VLAN);
4142 * ice_vc_process_vlan_msg
4143 * @vf: pointer to the VF info
4144 * @msg: pointer to the msg buffer
4145 * @add_v: Add VLAN if true, otherwise delete VLAN
4147 * Process virtchnl op to add or remove programmed guest VLAN ID
4149 static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
4151 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4152 struct virtchnl_vlan_filter_list *vfl =
4153 (struct virtchnl_vlan_filter_list *)msg;
4154 struct ice_pf *pf = vf->pf;
4155 bool vlan_promisc = false;
4156 struct ice_vsi *vsi;
4163 dev = ice_pf_to_dev(pf);
4164 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4165 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4169 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
4170 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4174 if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
4175 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4179 for (i = 0; i < vfl->num_elements; i++) {
4180 if (vfl->vlan_id[i] >= VLAN_N_VID) {
4181 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4182 dev_err(dev, "invalid VF VLAN id %d\n",
4189 vsi = ice_get_vf_vsi(vf);
4191 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4195 if (add_v && !ice_is_vf_trusted(vf) &&
4196 vsi->num_vlan >= ICE_MAX_VLAN_PER_VF) {
4197 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
4199 /* There is no need to let VF know about being not trusted,
4200 * so we can just return success message here
4205 if (vsi->info.pvid) {
4206 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4210 if ((test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
4211 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) &&
4212 test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags))
4213 vlan_promisc = true;
4216 for (i = 0; i < vfl->num_elements; i++) {
4217 u16 vid = vfl->vlan_id[i];
4219 if (!ice_is_vf_trusted(vf) &&
4220 vsi->num_vlan >= ICE_MAX_VLAN_PER_VF) {
4221 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
4223 /* There is no need to let VF know about being
4224 * not trusted, so we can just return success
4225 * message here as well.
4230 /* we add VLAN 0 by default for each VF so we can enable
4231 * Tx VLAN anti-spoof without triggering MDD events so
4232 * we don't need to add it again here
4237 status = ice_vsi_add_vlan(vsi, vid, ICE_FWD_TO_VSI);
4239 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4243 /* Enable VLAN pruning when non-zero VLAN is added */
4244 if (!vlan_promisc && vid &&
4245 !ice_vsi_is_vlan_pruning_ena(vsi)) {
4246 status = ice_cfg_vlan_pruning(vsi, true);
4248 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4249 dev_err(dev, "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
4253 } else if (vlan_promisc) {
4254 /* Enable Ucast/Mcast VLAN promiscuous mode */
4255 promisc_m = ICE_PROMISC_VLAN_TX |
4256 ICE_PROMISC_VLAN_RX;
4258 status = ice_set_vsi_promisc(hw, vsi->idx,
4261 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4262 dev_err(dev, "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
4268 /* In case of non_trusted VF, number of VLAN elements passed
4269 * to PF for removal might be greater than number of VLANs
4270 * filter programmed for that VF - So, use actual number of
4271 * VLANS added earlier with add VLAN opcode. In order to avoid
4272 * removing VLAN that doesn't exist, which result to sending
4273 * erroneous failed message back to the VF
4277 num_vf_vlan = vsi->num_vlan;
4278 for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
4279 u16 vid = vfl->vlan_id[i];
4281 /* we add VLAN 0 by default for each VF so we can enable
4282 * Tx VLAN anti-spoof without triggering MDD events so
4283 * we don't want a VIRTCHNL request to remove it
4288 /* Make sure ice_vsi_kill_vlan is successful before
4289 * updating VLAN information
4291 status = ice_vsi_kill_vlan(vsi, vid);
4293 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4297 /* Disable VLAN pruning when only VLAN 0 is left */
4298 if (vsi->num_vlan == 1 &&
4299 ice_vsi_is_vlan_pruning_ena(vsi))
4300 ice_cfg_vlan_pruning(vsi, false);
4302 /* Disable Unicast/Multicast VLAN promiscuous mode */
4304 promisc_m = ICE_PROMISC_VLAN_TX |
4305 ICE_PROMISC_VLAN_RX;
4307 ice_clear_vsi_promisc(hw, vsi->idx,
4314 /* send the response to the VF */
4316 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
4319 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
4324 * ice_vc_add_vlan_msg
4325 * @vf: pointer to the VF info
4326 * @msg: pointer to the msg buffer
4328 * Add and program guest VLAN ID
4330 static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
4332 return ice_vc_process_vlan_msg(vf, msg, true);
4336 * ice_vc_remove_vlan_msg
4337 * @vf: pointer to the VF info
4338 * @msg: pointer to the msg buffer
4340 * remove programmed guest VLAN ID
4342 static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
4344 return ice_vc_process_vlan_msg(vf, msg, false);
4348 * ice_vc_ena_vlan_stripping
4349 * @vf: pointer to the VF info
4351 * Enable VLAN header stripping for a given VF
4353 static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
4355 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4356 struct ice_vsi *vsi;
4358 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4359 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4363 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
4364 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4368 vsi = ice_get_vf_vsi(vf);
4369 if (ice_vsi_manage_vlan_stripping(vsi, true))
4370 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4373 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
4378 * ice_vc_dis_vlan_stripping
4379 * @vf: pointer to the VF info
4381 * Disable VLAN header stripping for a given VF
4383 static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
4385 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4386 struct ice_vsi *vsi;
4388 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4389 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4393 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
4394 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4398 vsi = ice_get_vf_vsi(vf);
4400 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4404 if (ice_vsi_manage_vlan_stripping(vsi, false))
4405 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4408 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
4413 * ice_vf_init_vlan_stripping - enable/disable VLAN stripping on initialization
4414 * @vf: VF to enable/disable VLAN stripping for on initialization
4416 * If the VIRTCHNL_VF_OFFLOAD_VLAN flag is set enable VLAN stripping, else if
4417 * the flag is cleared then we want to disable stripping. For example, the flag
4418 * will be cleared when port VLANs are configured by the administrator before
4419 * passing the VF to the guest or if the AVF driver doesn't support VLAN
4422 static int ice_vf_init_vlan_stripping(struct ice_vf *vf)
4424 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
4429 /* don't modify stripping if port VLAN is configured */
4433 if (ice_vf_vlan_offload_ena(vf->driver_caps))
4434 return ice_vsi_manage_vlan_stripping(vsi, true);
4436 return ice_vsi_manage_vlan_stripping(vsi, false);
4439 static struct ice_vc_vf_ops ice_vc_vf_dflt_ops = {
4440 .get_ver_msg = ice_vc_get_ver_msg,
4441 .get_vf_res_msg = ice_vc_get_vf_res_msg,
4442 .reset_vf = ice_vc_reset_vf_msg,
4443 .add_mac_addr_msg = ice_vc_add_mac_addr_msg,
4444 .del_mac_addr_msg = ice_vc_del_mac_addr_msg,
4445 .cfg_qs_msg = ice_vc_cfg_qs_msg,
4446 .ena_qs_msg = ice_vc_ena_qs_msg,
4447 .dis_qs_msg = ice_vc_dis_qs_msg,
4448 .request_qs_msg = ice_vc_request_qs_msg,
4449 .cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
4450 .config_rss_key = ice_vc_config_rss_key,
4451 .config_rss_lut = ice_vc_config_rss_lut,
4452 .get_stats_msg = ice_vc_get_stats_msg,
4453 .cfg_promiscuous_mode_msg = ice_vc_cfg_promiscuous_mode_msg,
4454 .add_vlan_msg = ice_vc_add_vlan_msg,
4455 .remove_vlan_msg = ice_vc_remove_vlan_msg,
4456 .ena_vlan_stripping = ice_vc_ena_vlan_stripping,
4457 .dis_vlan_stripping = ice_vc_dis_vlan_stripping,
4458 .handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
4459 .add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
4460 .del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
4463 void ice_vc_set_dflt_vf_ops(struct ice_vc_vf_ops *ops)
4465 *ops = ice_vc_vf_dflt_ops;
4469 * ice_vc_repr_add_mac
4470 * @vf: pointer to VF
4471 * @msg: virtchannel message
4473 * When port representors are created, we do not add MAC rule
4474 * to firmware, we store it so that PF could report same
4477 static int ice_vc_repr_add_mac(struct ice_vf *vf, u8 *msg)
4479 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4480 struct virtchnl_ether_addr_list *al =
4481 (struct virtchnl_ether_addr_list *)msg;
4482 struct ice_vsi *vsi;
4486 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
4487 !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
4488 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4489 goto handle_mac_exit;
4494 vsi = ice_get_vf_vsi(vf);
4496 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4497 goto handle_mac_exit;
4500 for (i = 0; i < al->num_elements; i++) {
4501 u8 *mac_addr = al->list[i].addr;
4504 if (!is_unicast_ether_addr(mac_addr) ||
4505 ether_addr_equal(mac_addr, vf->hw_lan_addr.addr))
4508 if (vf->pf_set_mac) {
4509 dev_err(ice_pf_to_dev(pf), "VF attempting to override administratively set MAC address\n");
4510 v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
4511 goto handle_mac_exit;
4514 result = ice_eswitch_add_vf_mac_rule(pf, vf, mac_addr);
4516 dev_err(ice_pf_to_dev(pf), "Failed to add MAC %pM for VF %d\n, error %d\n",
4517 mac_addr, vf->vf_id, result);
4518 goto handle_mac_exit;
4521 ice_vfhw_mac_add(vf, &al->list[i]);
4527 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
4532 * ice_vc_repr_del_mac - response with success for deleting MAC
4533 * @vf: pointer to VF
4534 * @msg: virtchannel message
4536 * Respond with success to not break normal VF flow.
4537 * For legacy VF driver try to update cached MAC address.
4540 ice_vc_repr_del_mac(struct ice_vf __always_unused *vf, u8 __always_unused *msg)
4542 struct virtchnl_ether_addr_list *al =
4543 (struct virtchnl_ether_addr_list *)msg;
4545 ice_update_legacy_cached_mac(vf, &al->list[0]);
4547 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
4548 VIRTCHNL_STATUS_SUCCESS, NULL, 0);
4551 static int ice_vc_repr_add_vlan(struct ice_vf *vf, u8 __always_unused *msg)
4553 dev_dbg(ice_pf_to_dev(vf->pf),
4554 "Can't add VLAN in switchdev mode for VF %d\n", vf->vf_id);
4555 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN,
4556 VIRTCHNL_STATUS_SUCCESS, NULL, 0);
4559 static int ice_vc_repr_del_vlan(struct ice_vf *vf, u8 __always_unused *msg)
4561 dev_dbg(ice_pf_to_dev(vf->pf),
4562 "Can't delete VLAN in switchdev mode for VF %d\n", vf->vf_id);
4563 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN,
4564 VIRTCHNL_STATUS_SUCCESS, NULL, 0);
4567 static int ice_vc_repr_ena_vlan_stripping(struct ice_vf *vf)
4569 dev_dbg(ice_pf_to_dev(vf->pf),
4570 "Can't enable VLAN stripping in switchdev mode for VF %d\n",
4572 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
4573 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4577 static int ice_vc_repr_dis_vlan_stripping(struct ice_vf *vf)
4579 dev_dbg(ice_pf_to_dev(vf->pf),
4580 "Can't disable VLAN stripping in switchdev mode for VF %d\n",
4582 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
4583 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4588 ice_vc_repr_cfg_promiscuous_mode(struct ice_vf *vf, u8 __always_unused *msg)
4590 dev_dbg(ice_pf_to_dev(vf->pf),
4591 "Can't config promiscuous mode in switchdev mode for VF %d\n",
4593 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
4594 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4598 void ice_vc_change_ops_to_repr(struct ice_vc_vf_ops *ops)
4600 ops->add_mac_addr_msg = ice_vc_repr_add_mac;
4601 ops->del_mac_addr_msg = ice_vc_repr_del_mac;
4602 ops->add_vlan_msg = ice_vc_repr_add_vlan;
4603 ops->remove_vlan_msg = ice_vc_repr_del_vlan;
4604 ops->ena_vlan_stripping = ice_vc_repr_ena_vlan_stripping;
4605 ops->dis_vlan_stripping = ice_vc_repr_dis_vlan_stripping;
4606 ops->cfg_promiscuous_mode_msg = ice_vc_repr_cfg_promiscuous_mode;
4610 * ice_vc_process_vf_msg - Process request from VF
4611 * @pf: pointer to the PF structure
4612 * @event: pointer to the AQ event
4614 * called from the common asq/arq handler to
4615 * process request from VF
4617 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
4619 u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
4620 s16 vf_id = le16_to_cpu(event->desc.retval);
4621 u16 msglen = event->msg_len;
4622 struct ice_vc_vf_ops *ops;
4623 u8 *msg = event->msg_buf;
4624 struct ice_vf *vf = NULL;
4628 /* if de-init is underway, don't process messages from VF */
4629 if (test_bit(ICE_VF_DEINIT_IN_PROGRESS, pf->state))
4632 dev = ice_pf_to_dev(pf);
4633 if (ice_validate_vf_id(pf, vf_id)) {
4638 vf = &pf->vf[vf_id];
4640 /* Check if VF is disabled. */
4641 if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
4648 /* Perform basic checks on the msg */
4649 err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
4651 if (err == VIRTCHNL_STATUS_ERR_PARAM)
4657 if (!ice_vc_is_opcode_allowed(vf, v_opcode)) {
4658 ice_vc_send_msg_to_vf(vf, v_opcode,
4659 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED, NULL,
4666 ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
4668 dev_err(dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
4669 vf_id, v_opcode, msglen, err);
4673 /* VF is being configured in another context that triggers a VFR, so no
4674 * need to process this message
4676 if (!mutex_trylock(&vf->cfg_lock)) {
4677 dev_info(dev, "VF %u is being configured in another context that will trigger a VFR, so there is no need to handle this message\n",
4683 case VIRTCHNL_OP_VERSION:
4684 err = ops->get_ver_msg(vf, msg);
4686 case VIRTCHNL_OP_GET_VF_RESOURCES:
4687 err = ops->get_vf_res_msg(vf, msg);
4688 if (ice_vf_init_vlan_stripping(vf))
4689 dev_err(dev, "Failed to initialize VLAN stripping for VF %d\n",
4691 ice_vc_notify_vf_link_state(vf);
4693 case VIRTCHNL_OP_RESET_VF:
4696 case VIRTCHNL_OP_ADD_ETH_ADDR:
4697 err = ops->add_mac_addr_msg(vf, msg);
4699 case VIRTCHNL_OP_DEL_ETH_ADDR:
4700 err = ops->del_mac_addr_msg(vf, msg);
4702 case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
4703 err = ops->cfg_qs_msg(vf, msg);
4705 case VIRTCHNL_OP_ENABLE_QUEUES:
4706 err = ops->ena_qs_msg(vf, msg);
4707 ice_vc_notify_vf_link_state(vf);
4709 case VIRTCHNL_OP_DISABLE_QUEUES:
4710 err = ops->dis_qs_msg(vf, msg);
4712 case VIRTCHNL_OP_REQUEST_QUEUES:
4713 err = ops->request_qs_msg(vf, msg);
4715 case VIRTCHNL_OP_CONFIG_IRQ_MAP:
4716 err = ops->cfg_irq_map_msg(vf, msg);
4718 case VIRTCHNL_OP_CONFIG_RSS_KEY:
4719 err = ops->config_rss_key(vf, msg);
4721 case VIRTCHNL_OP_CONFIG_RSS_LUT:
4722 err = ops->config_rss_lut(vf, msg);
4724 case VIRTCHNL_OP_GET_STATS:
4725 err = ops->get_stats_msg(vf, msg);
4727 case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
4728 err = ops->cfg_promiscuous_mode_msg(vf, msg);
4730 case VIRTCHNL_OP_ADD_VLAN:
4731 err = ops->add_vlan_msg(vf, msg);
4733 case VIRTCHNL_OP_DEL_VLAN:
4734 err = ops->remove_vlan_msg(vf, msg);
4736 case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
4737 err = ops->ena_vlan_stripping(vf);
4739 case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
4740 err = ops->dis_vlan_stripping(vf);
4742 case VIRTCHNL_OP_ADD_FDIR_FILTER:
4743 err = ops->add_fdir_fltr_msg(vf, msg);
4745 case VIRTCHNL_OP_DEL_FDIR_FILTER:
4746 err = ops->del_fdir_fltr_msg(vf, msg);
4748 case VIRTCHNL_OP_ADD_RSS_CFG:
4749 err = ops->handle_rss_cfg_msg(vf, msg, true);
4751 case VIRTCHNL_OP_DEL_RSS_CFG:
4752 err = ops->handle_rss_cfg_msg(vf, msg, false);
4754 case VIRTCHNL_OP_UNKNOWN:
4756 dev_err(dev, "Unsupported opcode %d from VF %d\n", v_opcode,
4758 err = ice_vc_send_msg_to_vf(vf, v_opcode,
4759 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4764 /* Helper function cares less about error return values here
4765 * as it is busy with pending work.
4767 dev_info(dev, "PF failed to honor VF %d, opcode %d, error %d\n",
4768 vf_id, v_opcode, err);
4771 mutex_unlock(&vf->cfg_lock);
4776 * @netdev: network interface device structure
4777 * @vf_id: VF identifier
4778 * @ivi: VF configuration structure
4780 * return VF configuration
4783 ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
4785 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4788 if (ice_validate_vf_id(pf, vf_id))
4791 vf = &pf->vf[vf_id];
4793 if (ice_check_vf_init(pf, vf))
4797 ether_addr_copy(ivi->mac, vf->hw_lan_addr.addr);
4799 /* VF configuration for VLAN and applicable QoS */
4800 ivi->vlan = vf->port_vlan_info & VLAN_VID_MASK;
4801 ivi->qos = (vf->port_vlan_info & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
4803 ivi->trusted = vf->trusted;
4804 ivi->spoofchk = vf->spoofchk;
4805 if (!vf->link_forced)
4806 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
4807 else if (vf->link_up)
4808 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
4810 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
4811 ivi->max_tx_rate = vf->max_tx_rate;
4812 ivi->min_tx_rate = vf->min_tx_rate;
4817 * ice_unicast_mac_exists - check if the unicast MAC exists on the PF's switch
4818 * @pf: PF used to reference the switch's rules
4819 * @umac: unicast MAC to compare against existing switch rules
4821 * Return true on the first/any match, else return false
4823 static bool ice_unicast_mac_exists(struct ice_pf *pf, u8 *umac)
4825 struct ice_sw_recipe *mac_recipe_list =
4826 &pf->hw.switch_info->recp_list[ICE_SW_LKUP_MAC];
4827 struct ice_fltr_mgmt_list_entry *list_itr;
4828 struct list_head *rule_head;
4829 struct mutex *rule_lock; /* protect MAC filter list access */
4831 rule_head = &mac_recipe_list->filt_rules;
4832 rule_lock = &mac_recipe_list->filt_rule_lock;
4834 mutex_lock(rule_lock);
4835 list_for_each_entry(list_itr, rule_head, list_entry) {
4836 u8 *existing_mac = &list_itr->fltr_info.l_data.mac.mac_addr[0];
4838 if (ether_addr_equal(existing_mac, umac)) {
4839 mutex_unlock(rule_lock);
4844 mutex_unlock(rule_lock);
4851 * @netdev: network interface device structure
4852 * @vf_id: VF identifier
4855 * program VF MAC address
4857 int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
4859 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4863 if (ice_validate_vf_id(pf, vf_id))
4866 if (is_multicast_ether_addr(mac)) {
4867 netdev_err(netdev, "%pM not a valid unicast address\n", mac);
4871 vf = &pf->vf[vf_id];
4872 /* nothing left to do, unicast MAC already set */
4873 if (ether_addr_equal(vf->dev_lan_addr.addr, mac) &&
4874 ether_addr_equal(vf->hw_lan_addr.addr, mac))
4877 ret = ice_check_vf_ready_for_cfg(vf);
4881 if (ice_unicast_mac_exists(pf, mac)) {
4882 netdev_err(netdev, "Unicast MAC %pM already exists on this PF. Preventing setting VF %u unicast MAC address to %pM\n",
4887 mutex_lock(&vf->cfg_lock);
4889 /* VF is notified of its new MAC via the PF's response to the
4890 * VIRTCHNL_OP_GET_VF_RESOURCES message after the VF has been reset
4892 ether_addr_copy(vf->dev_lan_addr.addr, mac);
4893 ether_addr_copy(vf->hw_lan_addr.addr, mac);
4894 if (is_zero_ether_addr(mac)) {
4895 /* VF will send VIRTCHNL_OP_ADD_ETH_ADDR message with its MAC */
4896 vf->pf_set_mac = false;
4897 netdev_info(netdev, "Removing MAC on VF %d. VF driver will be reinitialized\n",
4900 /* PF will add MAC rule for the VF */
4901 vf->pf_set_mac = true;
4902 netdev_info(netdev, "Setting MAC %pM on VF %d. VF driver will be reinitialized\n",
4906 ice_vc_reset_vf(vf);
4907 mutex_unlock(&vf->cfg_lock);
4913 * @netdev: network interface device structure
4914 * @vf_id: VF identifier
4915 * @trusted: Boolean value to enable/disable trusted VF
4917 * Enable or disable a given VF as trusted
4919 int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
4921 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4925 if (ice_is_eswitch_mode_switchdev(pf)) {
4926 dev_info(ice_pf_to_dev(pf), "Trusted VF is forbidden in switchdev mode\n");
4930 if (ice_validate_vf_id(pf, vf_id))
4933 vf = &pf->vf[vf_id];
4934 ret = ice_check_vf_ready_for_cfg(vf);
4938 /* Check if already trusted */
4939 if (trusted == vf->trusted)
4942 mutex_lock(&vf->cfg_lock);
4944 vf->trusted = trusted;
4945 ice_vc_reset_vf(vf);
4946 dev_info(ice_pf_to_dev(pf), "VF %u is now %strusted\n",
4947 vf_id, trusted ? "" : "un");
4949 mutex_unlock(&vf->cfg_lock);
4955 * ice_set_vf_link_state
4956 * @netdev: network interface device structure
4957 * @vf_id: VF identifier
4958 * @link_state: required link state
4960 * Set VF's link state, irrespective of physical link state status
4962 int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
4964 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4968 if (ice_validate_vf_id(pf, vf_id))
4971 vf = &pf->vf[vf_id];
4972 ret = ice_check_vf_ready_for_cfg(vf);
4976 switch (link_state) {
4977 case IFLA_VF_LINK_STATE_AUTO:
4978 vf->link_forced = false;
4980 case IFLA_VF_LINK_STATE_ENABLE:
4981 vf->link_forced = true;
4984 case IFLA_VF_LINK_STATE_DISABLE:
4985 vf->link_forced = true;
4986 vf->link_up = false;
4992 ice_vc_notify_vf_link_state(vf);
4998 * ice_calc_all_vfs_min_tx_rate - calculate cumulative min Tx rate on all VFs
4999 * @pf: PF associated with VFs
5001 static int ice_calc_all_vfs_min_tx_rate(struct ice_pf *pf)
5005 ice_for_each_vf(pf, i)
5006 rate += pf->vf[i].min_tx_rate;
5012 * ice_min_tx_rate_oversubscribed - check if min Tx rate causes oversubscription
5013 * @vf: VF trying to configure min_tx_rate
5014 * @min_tx_rate: min Tx rate in Mbps
5016 * Check if the min_tx_rate being passed in will cause oversubscription of total
5017 * min_tx_rate based on the current link speed and all other VFs configured
5020 * Return true if the passed min_tx_rate would cause oversubscription, else
5024 ice_min_tx_rate_oversubscribed(struct ice_vf *vf, int min_tx_rate)
5026 int link_speed_mbps = ice_get_link_speed_mbps(ice_get_vf_vsi(vf));
5027 int all_vfs_min_tx_rate = ice_calc_all_vfs_min_tx_rate(vf->pf);
5029 /* this VF's previous rate is being overwritten */
5030 all_vfs_min_tx_rate -= vf->min_tx_rate;
5032 if (all_vfs_min_tx_rate + min_tx_rate > link_speed_mbps) {
5033 dev_err(ice_pf_to_dev(vf->pf), "min_tx_rate of %d Mbps on VF %u would cause oversubscription of %d Mbps based on the current link speed %d Mbps\n",
5034 min_tx_rate, vf->vf_id,
5035 all_vfs_min_tx_rate + min_tx_rate - link_speed_mbps,
5044 * ice_set_vf_bw - set min/max VF bandwidth
5045 * @netdev: network interface device structure
5046 * @vf_id: VF identifier
5047 * @min_tx_rate: Minimum Tx rate in Mbps
5048 * @max_tx_rate: Maximum Tx rate in Mbps
5051 ice_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
5054 struct ice_pf *pf = ice_netdev_to_pf(netdev);
5055 struct ice_vsi *vsi;
5060 dev = ice_pf_to_dev(pf);
5061 if (ice_validate_vf_id(pf, vf_id))
5064 vf = &pf->vf[vf_id];
5065 ret = ice_check_vf_ready_for_cfg(vf);
5069 vsi = ice_get_vf_vsi(vf);
5071 /* when max_tx_rate is zero that means no max Tx rate limiting, so only
5072 * check if max_tx_rate is non-zero
5074 if (max_tx_rate && min_tx_rate > max_tx_rate) {
5075 dev_err(dev, "Cannot set min Tx rate %d Mbps greater than max Tx rate %d Mbps\n",
5076 min_tx_rate, max_tx_rate);
5080 if (min_tx_rate && ice_is_dcb_active(pf)) {
5081 dev_err(dev, "DCB on PF is currently enabled. VF min Tx rate limiting not allowed on this PF.\n");
5085 if (ice_min_tx_rate_oversubscribed(vf, min_tx_rate))
5088 if (vf->min_tx_rate != (unsigned int)min_tx_rate) {
5089 ret = ice_set_min_bw_limit(vsi, (u64)min_tx_rate * 1000);
5091 dev_err(dev, "Unable to set min-tx-rate for VF %d\n",
5096 vf->min_tx_rate = min_tx_rate;
5099 if (vf->max_tx_rate != (unsigned int)max_tx_rate) {
5100 ret = ice_set_max_bw_limit(vsi, (u64)max_tx_rate * 1000);
5102 dev_err(dev, "Unable to set max-tx-rate for VF %d\n",
5107 vf->max_tx_rate = max_tx_rate;
5114 * ice_get_vf_stats - populate some stats for the VF
5115 * @netdev: the netdev of the PF
5116 * @vf_id: the host OS identifier (0-255)
5117 * @vf_stats: pointer to the OS memory to be initialized
5119 int ice_get_vf_stats(struct net_device *netdev, int vf_id,
5120 struct ifla_vf_stats *vf_stats)
5122 struct ice_pf *pf = ice_netdev_to_pf(netdev);
5123 struct ice_eth_stats *stats;
5124 struct ice_vsi *vsi;
5128 if (ice_validate_vf_id(pf, vf_id))
5131 vf = &pf->vf[vf_id];
5132 ret = ice_check_vf_ready_for_cfg(vf);
5136 vsi = ice_get_vf_vsi(vf);
5140 ice_update_eth_stats(vsi);
5141 stats = &vsi->eth_stats;
5143 memset(vf_stats, 0, sizeof(*vf_stats));
5145 vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
5146 stats->rx_multicast;
5147 vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
5148 stats->tx_multicast;
5149 vf_stats->rx_bytes = stats->rx_bytes;
5150 vf_stats->tx_bytes = stats->tx_bytes;
5151 vf_stats->broadcast = stats->rx_broadcast;
5152 vf_stats->multicast = stats->rx_multicast;
5153 vf_stats->rx_dropped = stats->rx_discards;
5154 vf_stats->tx_dropped = stats->tx_discards;
5160 * ice_print_vf_rx_mdd_event - print VF Rx malicious driver detect event
5161 * @vf: pointer to the VF structure
5163 void ice_print_vf_rx_mdd_event(struct ice_vf *vf)
5165 struct ice_pf *pf = vf->pf;
5168 dev = ice_pf_to_dev(pf);
5170 dev_info(dev, "%d Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pM. mdd-auto-reset-vfs=%s\n",
5171 vf->mdd_rx_events.count, pf->hw.pf_id, vf->vf_id,
5172 vf->dev_lan_addr.addr,
5173 test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)
5178 * ice_print_vfs_mdd_events - print VFs malicious driver detect event
5179 * @pf: pointer to the PF structure
5181 * Called from ice_handle_mdd_event to rate limit and print VFs MDD events.
5183 void ice_print_vfs_mdd_events(struct ice_pf *pf)
5185 struct device *dev = ice_pf_to_dev(pf);
5186 struct ice_hw *hw = &pf->hw;
5189 /* check that there are pending MDD events to print */
5190 if (!test_and_clear_bit(ICE_MDD_VF_PRINT_PENDING, pf->state))
5193 /* VF MDD event logs are rate limited to one second intervals */
5194 if (time_is_after_jiffies(pf->last_printed_mdd_jiffies + HZ * 1))
5197 pf->last_printed_mdd_jiffies = jiffies;
5199 ice_for_each_vf(pf, i) {
5200 struct ice_vf *vf = &pf->vf[i];
5202 /* only print Rx MDD event message if there are new events */
5203 if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
5204 vf->mdd_rx_events.last_printed =
5205 vf->mdd_rx_events.count;
5206 ice_print_vf_rx_mdd_event(vf);
5209 /* only print Tx MDD event message if there are new events */
5210 if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
5211 vf->mdd_tx_events.last_printed =
5212 vf->mdd_tx_events.count;
5214 dev_info(dev, "%d Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pM.\n",
5215 vf->mdd_tx_events.count, hw->pf_id, i,
5216 vf->dev_lan_addr.addr);
5222 * ice_restore_all_vfs_msi_state - restore VF MSI state after PF FLR
5223 * @pdev: pointer to a pci_dev structure
5225 * Called when recovering from a PF FLR to restore interrupt capability to
5228 void ice_restore_all_vfs_msi_state(struct pci_dev *pdev)
5233 if (!pci_num_vf(pdev))
5236 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
5238 struct pci_dev *vfdev;
5240 pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID,
5242 vfdev = pci_get_device(pdev->vendor, vf_id, NULL);
5244 if (vfdev->is_virtfn && vfdev->physfn == pdev)
5245 pci_restore_msi_state(vfdev);
5246 vfdev = pci_get_device(pdev->vendor, vf_id,
5253 * ice_is_malicious_vf - helper function to detect a malicious VF
5254 * @pf: ptr to struct ice_pf
5255 * @event: pointer to the AQ event
5256 * @num_msg_proc: the number of messages processed so far
5257 * @num_msg_pending: the number of messages peinding in admin queue
5260 ice_is_malicious_vf(struct ice_pf *pf, struct ice_rq_event_info *event,
5261 u16 num_msg_proc, u16 num_msg_pending)
5263 s16 vf_id = le16_to_cpu(event->desc.retval);
5264 struct device *dev = ice_pf_to_dev(pf);
5265 struct ice_mbx_data mbxdata;
5270 if (ice_validate_vf_id(pf, vf_id))
5273 vf = &pf->vf[vf_id];
5274 /* Check if VF is disabled. */
5275 if (test_bit(ICE_VF_STATE_DIS, vf->vf_states))
5278 mbxdata.num_msg_proc = num_msg_proc;
5279 mbxdata.num_pending_arq = num_msg_pending;
5280 mbxdata.max_num_msgs_mbx = pf->hw.mailboxq.num_rq_entries;
5281 #define ICE_MBX_OVERFLOW_WATERMARK 64
5282 mbxdata.async_watermark_val = ICE_MBX_OVERFLOW_WATERMARK;
5284 /* check to see if we have a malicious VF */
5285 status = ice_mbx_vf_state_handler(&pf->hw, &mbxdata, vf_id, &malvf);
5290 bool report_vf = false;
5292 /* if the VF is malicious and we haven't let the user
5293 * know about it, then let them know now
5295 status = ice_mbx_report_malvf(&pf->hw, pf->malvfs,
5296 ICE_MAX_VF_COUNT, vf_id,
5299 dev_dbg(dev, "Error reporting malicious VF\n");
5302 struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
5305 dev_warn(dev, "VF MAC %pM on PF MAC %pM is generating asynchronous messages and may be overflowing the PF message queue. Please see the Adapter User Guide for more information\n",
5306 &vf->dev_lan_addr.addr[0],
5307 pf_vsi->netdev->dev_addr);
5313 /* if there was an error in detection or the VF is not malicious then