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;
506 ice_eswitch_release(pf);
508 while (test_and_set_bit(ICE_VF_DIS, pf->state))
509 usleep_range(1000, 2000);
511 /* Disable IOV before freeing resources. This lets any VF drivers
512 * running in the host get themselves cleaned up before we yank
513 * the carpet out from underneath their feet.
515 if (!pci_vfs_assigned(pf->pdev))
516 pci_disable_sriov(pf->pdev);
518 dev_warn(dev, "VFs are assigned - not disabling SR-IOV\n");
520 tmp = pf->num_alloc_vfs;
521 pf->num_qps_per_vf = 0;
522 pf->num_alloc_vfs = 0;
523 for (i = 0; i < tmp; i++) {
524 struct ice_vf *vf = &pf->vf[i];
526 mutex_lock(&vf->cfg_lock);
530 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
531 /* disable VF qp mappings and set VF disable state */
532 ice_dis_vf_mappings(vf);
533 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
537 mutex_unlock(&vf->cfg_lock);
539 mutex_destroy(&vf->cfg_lock);
542 if (ice_sriov_free_msix_res(pf))
543 dev_err(dev, "Failed to free MSIX resources used by SR-IOV\n");
545 devm_kfree(dev, pf->vf);
548 /* This check is for when the driver is unloaded while VFs are
549 * assigned. Setting the number of VFs to 0 through sysfs is caught
550 * before this function ever gets called.
552 if (!pci_vfs_assigned(pf->pdev)) {
555 /* Acknowledge VFLR for all VFs. Without this, VFs will fail to
556 * work correctly when SR-IOV gets re-enabled.
558 for (vf_id = 0; vf_id < tmp; vf_id++) {
559 u32 reg_idx, bit_idx;
561 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
562 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
563 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
567 /* clear malicious info if the VFs are getting released */
568 for (i = 0; i < tmp; i++)
569 if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->malvfs,
570 ICE_MAX_VF_COUNT, i))
571 dev_dbg(dev, "failed to clear malicious VF state for VF %u\n",
574 clear_bit(ICE_VF_DIS, pf->state);
575 clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
579 * ice_trigger_vf_reset - Reset a VF on HW
580 * @vf: pointer to the VF structure
581 * @is_vflr: true if VFLR was issued, false if not
582 * @is_pfr: true if the reset was triggered due to a previous PFR
584 * Trigger hardware to start a reset for a particular VF. Expects the caller
585 * to wait the proper amount of time to allow hardware to reset the VF before
586 * it cleans up and restores VF functionality.
588 static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr, bool is_pfr)
590 struct ice_pf *pf = vf->pf;
591 u32 reg, reg_idx, bit_idx;
592 unsigned int vf_abs_id, i;
596 dev = ice_pf_to_dev(pf);
598 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
600 /* Inform VF that it is no longer active, as a warning */
601 clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
603 /* Disable VF's configuration API during reset. The flag is re-enabled
604 * when it's safe again to access VF's VSI.
606 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
608 /* VF_MBX_ARQLEN and VF_MBX_ATQLEN are cleared by PFR, so the driver
609 * needs to clear them in the case of VFR/VFLR. If this is done for
610 * PFR, it can mess up VF resets because the VF driver may already
611 * have started cleanup by the time we get here.
614 wr32(hw, VF_MBX_ARQLEN(vf->vf_id), 0);
615 wr32(hw, VF_MBX_ATQLEN(vf->vf_id), 0);
618 /* In the case of a VFLR, the HW has already reset the VF and we
619 * just need to clean up, so don't hit the VFRTRIG register.
622 /* reset VF using VPGEN_VFRTRIG reg */
623 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
624 reg |= VPGEN_VFRTRIG_VFSWR_M;
625 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
627 /* clear the VFLR bit in GLGEN_VFLRSTAT */
628 reg_idx = (vf_abs_id) / 32;
629 bit_idx = (vf_abs_id) % 32;
630 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
633 wr32(hw, PF_PCI_CIAA,
634 VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
635 for (i = 0; i < ICE_PCI_CIAD_WAIT_COUNT; i++) {
636 reg = rd32(hw, PF_PCI_CIAD);
637 /* no transactions pending so stop polling */
638 if ((reg & VF_TRANS_PENDING_M) == 0)
641 dev_err(dev, "VF %u PCI transactions stuck\n", vf->vf_id);
642 udelay(ICE_PCI_CIAD_WAIT_DELAY_US);
647 * ice_vsi_manage_pvid - Enable or disable port VLAN for VSI
648 * @vsi: the VSI to update
649 * @pvid_info: VLAN ID and QoS used to set the PVID VSI context field
650 * @enable: true for enable PVID false for disable
652 static int ice_vsi_manage_pvid(struct ice_vsi *vsi, u16 pvid_info, bool enable)
654 struct ice_hw *hw = &vsi->back->hw;
655 struct ice_aqc_vsi_props *info;
656 struct ice_vsi_ctx *ctxt;
659 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
663 ctxt->info = vsi->info;
666 info->vlan_flags = ICE_AQ_VSI_VLAN_MODE_UNTAGGED |
667 ICE_AQ_VSI_PVLAN_INSERT_PVID |
668 ICE_AQ_VSI_VLAN_EMOD_STR;
669 info->sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
671 info->vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING |
672 ICE_AQ_VSI_VLAN_MODE_ALL;
673 info->sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
676 info->pvid = cpu_to_le16(pvid_info);
677 info->valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
678 ICE_AQ_VSI_PROP_SW_VALID);
680 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
682 dev_info(ice_hw_to_dev(hw), "update VSI for port VLAN failed, err %d aq_err %s\n",
683 ret, ice_aq_str(hw->adminq.sq_last_status));
687 vsi->info.vlan_flags = info->vlan_flags;
688 vsi->info.sw_flags2 = info->sw_flags2;
689 vsi->info.pvid = info->pvid;
696 * ice_vf_get_port_info - Get the VF's port info structure
697 * @vf: VF used to get the port info structure for
699 static struct ice_port_info *ice_vf_get_port_info(struct ice_vf *vf)
701 return vf->pf->hw.port_info;
705 * ice_vf_vsi_setup - Set up a VF VSI
706 * @vf: VF to setup VSI for
708 * Returns pointer to the successfully allocated VSI struct on success,
709 * otherwise returns NULL on failure.
711 static struct ice_vsi *ice_vf_vsi_setup(struct ice_vf *vf)
713 struct ice_port_info *pi = ice_vf_get_port_info(vf);
714 struct ice_pf *pf = vf->pf;
717 vsi = ice_vsi_setup(pf, pi, ICE_VSI_VF, vf->vf_id, NULL);
720 dev_err(ice_pf_to_dev(pf), "Failed to create VF VSI\n");
721 ice_vf_invalidate_vsi(vf);
725 vf->lan_vsi_idx = vsi->idx;
726 vf->lan_vsi_num = vsi->vsi_num;
732 * ice_vf_ctrl_vsi_setup - Set up a VF control VSI
733 * @vf: VF to setup control VSI for
735 * Returns pointer to the successfully allocated VSI struct on success,
736 * otherwise returns NULL on failure.
738 struct ice_vsi *ice_vf_ctrl_vsi_setup(struct ice_vf *vf)
740 struct ice_port_info *pi = ice_vf_get_port_info(vf);
741 struct ice_pf *pf = vf->pf;
744 vsi = ice_vsi_setup(pf, pi, ICE_VSI_CTRL, vf->vf_id, NULL);
746 dev_err(ice_pf_to_dev(pf), "Failed to create VF control VSI\n");
747 ice_vf_ctrl_invalidate_vsi(vf);
754 * ice_calc_vf_first_vector_idx - Calculate MSIX vector index in the PF space
755 * @pf: pointer to PF structure
756 * @vf: pointer to VF that the first MSIX vector index is being calculated for
758 * This returns the first MSIX vector index in PF space that is used by this VF.
759 * This index is used when accessing PF relative registers such as
760 * GLINT_VECT2FUNC and GLINT_DYN_CTL.
761 * This will always be the OICR index in the AVF driver so any functionality
762 * using vf->first_vector_idx for queue configuration will have to increment by
763 * 1 to avoid meddling with the OICR index.
765 static int ice_calc_vf_first_vector_idx(struct ice_pf *pf, struct ice_vf *vf)
767 return pf->sriov_base_vector + vf->vf_id * pf->num_msix_per_vf;
771 * ice_vf_rebuild_host_tx_rate_cfg - re-apply the Tx rate limiting configuration
772 * @vf: VF to re-apply the configuration for
774 * Called after a VF VSI has been re-added/rebuild during reset. The PF driver
775 * needs to re-apply the host configured Tx rate limiting configuration.
777 static int ice_vf_rebuild_host_tx_rate_cfg(struct ice_vf *vf)
779 struct device *dev = ice_pf_to_dev(vf->pf);
780 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
783 if (vf->min_tx_rate) {
784 err = ice_set_min_bw_limit(vsi, (u64)vf->min_tx_rate * 1000);
786 dev_err(dev, "failed to set min Tx rate to %d Mbps for VF %u, error %d\n",
787 vf->min_tx_rate, vf->vf_id, err);
792 if (vf->max_tx_rate) {
793 err = ice_set_max_bw_limit(vsi, (u64)vf->max_tx_rate * 1000);
795 dev_err(dev, "failed to set max Tx rate to %d Mbps for VF %u, error %d\n",
796 vf->max_tx_rate, vf->vf_id, err);
805 * ice_vf_rebuild_host_vlan_cfg - add VLAN 0 filter or rebuild the Port VLAN
806 * @vf: VF to add MAC filters for
808 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
809 * always re-adds either a VLAN 0 or port VLAN based filter after reset.
811 static int ice_vf_rebuild_host_vlan_cfg(struct ice_vf *vf)
813 struct device *dev = ice_pf_to_dev(vf->pf);
814 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
818 if (vf->port_vlan_info) {
819 err = ice_vsi_manage_pvid(vsi, vf->port_vlan_info, true);
821 dev_err(dev, "failed to configure port VLAN via VSI parameters for VF %u, error %d\n",
826 vlan_id = vf->port_vlan_info & VLAN_VID_MASK;
829 /* vlan_id will either be 0 or the port VLAN number */
830 err = ice_vsi_add_vlan(vsi, vlan_id, ICE_FWD_TO_VSI);
832 dev_err(dev, "failed to add %s VLAN %u filter for VF %u, error %d\n",
833 vf->port_vlan_info ? "port" : "", vlan_id, vf->vf_id,
842 * ice_vf_rebuild_host_mac_cfg - add broadcast and the VF's perm_addr/LAA
843 * @vf: VF to add MAC filters for
845 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
846 * always re-adds a broadcast filter and the VF's perm_addr/LAA after reset.
848 static int ice_vf_rebuild_host_mac_cfg(struct ice_vf *vf)
850 struct device *dev = ice_pf_to_dev(vf->pf);
851 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
852 u8 broadcast[ETH_ALEN];
855 if (ice_is_eswitch_mode_switchdev(vf->pf))
858 eth_broadcast_addr(broadcast);
859 status = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
861 dev_err(dev, "failed to add broadcast MAC filter for VF %u, error %d\n",
868 if (is_valid_ether_addr(vf->hw_lan_addr.addr)) {
869 status = ice_fltr_add_mac(vsi, vf->hw_lan_addr.addr,
872 dev_err(dev, "failed to add default unicast MAC filter %pM for VF %u, error %d\n",
873 &vf->hw_lan_addr.addr[0], vf->vf_id,
879 ether_addr_copy(vf->dev_lan_addr.addr, vf->hw_lan_addr.addr);
886 * ice_vf_set_host_trust_cfg - set trust setting based on pre-reset value
887 * @vf: VF to configure trust setting for
889 static void ice_vf_set_host_trust_cfg(struct ice_vf *vf)
892 set_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
894 clear_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
898 * ice_ena_vf_msix_mappings - enable VF MSIX mappings in hardware
899 * @vf: VF to enable MSIX mappings for
901 * Some of the registers need to be indexed/configured using hardware global
902 * device values and other registers need 0-based values, which represent PF
905 static void ice_ena_vf_msix_mappings(struct ice_vf *vf)
907 int device_based_first_msix, device_based_last_msix;
908 int pf_based_first_msix, pf_based_last_msix, v;
909 struct ice_pf *pf = vf->pf;
910 int device_based_vf_id;
915 pf_based_first_msix = vf->first_vector_idx;
916 pf_based_last_msix = (pf_based_first_msix + pf->num_msix_per_vf) - 1;
918 device_based_first_msix = pf_based_first_msix +
919 pf->hw.func_caps.common_cap.msix_vector_first_id;
920 device_based_last_msix =
921 (device_based_first_msix + pf->num_msix_per_vf) - 1;
922 device_based_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
924 reg = (((device_based_first_msix << VPINT_ALLOC_FIRST_S) &
925 VPINT_ALLOC_FIRST_M) |
926 ((device_based_last_msix << VPINT_ALLOC_LAST_S) &
927 VPINT_ALLOC_LAST_M) | VPINT_ALLOC_VALID_M);
928 wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
930 reg = (((device_based_first_msix << VPINT_ALLOC_PCI_FIRST_S)
931 & VPINT_ALLOC_PCI_FIRST_M) |
932 ((device_based_last_msix << VPINT_ALLOC_PCI_LAST_S) &
933 VPINT_ALLOC_PCI_LAST_M) | VPINT_ALLOC_PCI_VALID_M);
934 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg);
936 /* map the interrupts to its functions */
937 for (v = pf_based_first_msix; v <= pf_based_last_msix; v++) {
938 reg = (((device_based_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
939 GLINT_VECT2FUNC_VF_NUM_M) |
940 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
941 GLINT_VECT2FUNC_PF_NUM_M));
942 wr32(hw, GLINT_VECT2FUNC(v), reg);
945 /* Map mailbox interrupt to VF MSI-X vector 0 */
946 wr32(hw, VPINT_MBX_CTL(device_based_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M);
950 * ice_ena_vf_q_mappings - enable Rx/Tx queue mappings for a VF
951 * @vf: VF to enable the mappings for
952 * @max_txq: max Tx queues allowed on the VF's VSI
953 * @max_rxq: max Rx queues allowed on the VF's VSI
955 static void ice_ena_vf_q_mappings(struct ice_vf *vf, u16 max_txq, u16 max_rxq)
957 struct device *dev = ice_pf_to_dev(vf->pf);
958 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
959 struct ice_hw *hw = &vf->pf->hw;
962 /* set regardless of mapping mode */
963 wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M);
965 /* VF Tx queues allocation */
966 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
967 /* set the VF PF Tx queue range
968 * VFNUMQ value should be set to (number of queues - 1). A value
969 * of 0 means 1 queue and a value of 255 means 256 queues
971 reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
972 VPLAN_TX_QBASE_VFFIRSTQ_M) |
973 (((max_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
974 VPLAN_TX_QBASE_VFNUMQ_M));
975 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
977 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
980 /* set regardless of mapping mode */
981 wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M);
983 /* VF Rx queues allocation */
984 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
985 /* set the VF PF Rx queue range
986 * VFNUMQ value should be set to (number of queues - 1). A value
987 * of 0 means 1 queue and a value of 255 means 256 queues
989 reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
990 VPLAN_RX_QBASE_VFFIRSTQ_M) |
991 (((max_rxq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
992 VPLAN_RX_QBASE_VFNUMQ_M));
993 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
995 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
1000 * ice_ena_vf_mappings - enable VF MSIX and queue mapping
1001 * @vf: pointer to the VF structure
1003 static void ice_ena_vf_mappings(struct ice_vf *vf)
1005 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1007 ice_ena_vf_msix_mappings(vf);
1008 ice_ena_vf_q_mappings(vf, vsi->alloc_txq, vsi->alloc_rxq);
1013 * @pf: pointer to the PF structure
1014 * @avail_res: available resources in the PF structure
1015 * @max_res: maximum resources that can be given per VF
1016 * @min_res: minimum resources that can be given per VF
1018 * Returns non-zero value if resources (queues/vectors) are available or
1019 * returns zero if PF cannot accommodate for all num_alloc_vfs.
1022 ice_determine_res(struct ice_pf *pf, u16 avail_res, u16 max_res, u16 min_res)
1024 bool checked_min_res = false;
1027 /* start by checking if PF can assign max number of resources for
1028 * all num_alloc_vfs.
1029 * if yes, return number per VF
1030 * If no, divide by 2 and roundup, check again
1031 * repeat the loop till we reach a point where even minimum resources
1032 * are not available, in that case return 0
1035 while ((res >= min_res) && !checked_min_res) {
1038 num_all_res = pf->num_alloc_vfs * res;
1039 if (num_all_res <= avail_res)
1043 checked_min_res = true;
1045 res = DIV_ROUND_UP(res, 2);
1051 * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space
1052 * @vf: VF to calculate the register index for
1053 * @q_vector: a q_vector associated to the VF
1055 int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector)
1059 if (!vf || !q_vector)
1064 /* always add one to account for the OICR being the first MSIX */
1065 return pf->sriov_base_vector + pf->num_msix_per_vf * vf->vf_id +
1066 q_vector->v_idx + 1;
1070 * ice_get_max_valid_res_idx - Get the max valid resource index
1071 * @res: pointer to the resource to find the max valid index for
1073 * Start from the end of the ice_res_tracker and return right when we find the
1074 * first res->list entry with the ICE_RES_VALID_BIT set. This function is only
1075 * valid for SR-IOV because it is the only consumer that manipulates the
1076 * res->end and this is always called when res->end is set to res->num_entries.
1078 static int ice_get_max_valid_res_idx(struct ice_res_tracker *res)
1085 for (i = res->num_entries - 1; i >= 0; i--)
1086 if (res->list[i] & ICE_RES_VALID_BIT)
1093 * ice_sriov_set_msix_res - Set any used MSIX resources
1094 * @pf: pointer to PF structure
1095 * @num_msix_needed: number of MSIX vectors needed for all SR-IOV VFs
1097 * This function allows SR-IOV resources to be taken from the end of the PF's
1098 * allowed HW MSIX vectors so that the irq_tracker will not be affected. We
1099 * just set the pf->sriov_base_vector and return success.
1101 * If there are not enough resources available, return an error. This should
1102 * always be caught by ice_set_per_vf_res().
1104 * Return 0 on success, and -EINVAL when there are not enough MSIX vectors
1105 * in the PF's space available for SR-IOV.
1107 static int ice_sriov_set_msix_res(struct ice_pf *pf, u16 num_msix_needed)
1109 u16 total_vectors = pf->hw.func_caps.common_cap.num_msix_vectors;
1110 int vectors_used = pf->irq_tracker->num_entries;
1111 int sriov_base_vector;
1113 sriov_base_vector = total_vectors - num_msix_needed;
1115 /* make sure we only grab irq_tracker entries from the list end and
1116 * that we have enough available MSIX vectors
1118 if (sriov_base_vector < vectors_used)
1121 pf->sriov_base_vector = sriov_base_vector;
1127 * ice_set_per_vf_res - check if vectors and queues are available
1128 * @pf: pointer to the PF structure
1130 * First, determine HW interrupts from common pool. If we allocate fewer VFs, we
1131 * get more vectors and can enable more queues per VF. Note that this does not
1132 * grab any vectors from the SW pool already allocated. Also note, that all
1133 * vector counts include one for each VF's miscellaneous interrupt vector
1136 * Minimum VFs - 2 vectors, 1 queue pair
1137 * Small VFs - 5 vectors, 4 queue pairs
1138 * Medium VFs - 17 vectors, 16 queue pairs
1140 * Second, determine number of queue pairs per VF by starting with a pre-defined
1141 * maximum each VF supports. If this is not possible, then we adjust based on
1142 * queue pairs available on the device.
1144 * Lastly, set queue and MSI-X VF variables tracked by the PF so it can be used
1145 * by each VF during VF initialization and reset.
1147 static int ice_set_per_vf_res(struct ice_pf *pf)
1149 int max_valid_res_idx = ice_get_max_valid_res_idx(pf->irq_tracker);
1150 int msix_avail_per_vf, msix_avail_for_sriov;
1151 struct device *dev = ice_pf_to_dev(pf);
1152 u16 num_msix_per_vf, num_txq, num_rxq;
1154 if (!pf->num_alloc_vfs || max_valid_res_idx < 0)
1157 /* determine MSI-X resources per VF */
1158 msix_avail_for_sriov = pf->hw.func_caps.common_cap.num_msix_vectors -
1159 pf->irq_tracker->num_entries;
1160 msix_avail_per_vf = msix_avail_for_sriov / pf->num_alloc_vfs;
1161 if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MED) {
1162 num_msix_per_vf = ICE_NUM_VF_MSIX_MED;
1163 } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_SMALL) {
1164 num_msix_per_vf = ICE_NUM_VF_MSIX_SMALL;
1165 } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MULTIQ_MIN) {
1166 num_msix_per_vf = ICE_NUM_VF_MSIX_MULTIQ_MIN;
1167 } else if (msix_avail_per_vf >= ICE_MIN_INTR_PER_VF) {
1168 num_msix_per_vf = ICE_MIN_INTR_PER_VF;
1170 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",
1171 msix_avail_for_sriov, ICE_MIN_INTR_PER_VF,
1176 /* determine queue resources per VF */
1177 num_txq = ice_determine_res(pf, ice_get_avail_txq_count(pf),
1179 num_msix_per_vf - ICE_NONQ_VECS_VF,
1180 ICE_MAX_RSS_QS_PER_VF),
1183 num_rxq = ice_determine_res(pf, ice_get_avail_rxq_count(pf),
1185 num_msix_per_vf - ICE_NONQ_VECS_VF,
1186 ICE_MAX_RSS_QS_PER_VF),
1189 if (!num_txq || !num_rxq) {
1190 dev_err(dev, "Not enough queues to support minimum of %d queue pairs per VF for %d VFs\n",
1191 ICE_MIN_QS_PER_VF, pf->num_alloc_vfs);
1195 if (ice_sriov_set_msix_res(pf, num_msix_per_vf * pf->num_alloc_vfs)) {
1196 dev_err(dev, "Unable to set MSI-X resources for %d VFs\n",
1201 /* only allow equal Tx/Rx queue count (i.e. queue pairs) */
1202 pf->num_qps_per_vf = min_t(int, num_txq, num_rxq);
1203 pf->num_msix_per_vf = num_msix_per_vf;
1204 dev_info(dev, "Enabling %d VFs with %d vectors and %d queues per VF\n",
1205 pf->num_alloc_vfs, pf->num_msix_per_vf, pf->num_qps_per_vf);
1211 * ice_clear_vf_reset_trigger - enable VF to access hardware
1212 * @vf: VF to enabled hardware access for
1214 static void ice_clear_vf_reset_trigger(struct ice_vf *vf)
1216 struct ice_hw *hw = &vf->pf->hw;
1219 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
1220 reg &= ~VPGEN_VFRTRIG_VFSWR_M;
1221 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
1226 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
1228 struct ice_hw *hw = &vsi->back->hw;
1231 if (vf->port_vlan_info)
1232 status = ice_fltr_set_vsi_promisc(hw, vsi->idx, promisc_m,
1233 vf->port_vlan_info & VLAN_VID_MASK);
1234 else if (vsi->num_vlan > 1)
1235 status = ice_fltr_set_vlan_vsi_promisc(hw, vsi, promisc_m);
1237 status = ice_fltr_set_vsi_promisc(hw, vsi->idx, promisc_m, 0);
1239 if (status && status != -EEXIST) {
1240 dev_err(ice_pf_to_dev(vsi->back), "enable Tx/Rx filter promiscuous mode on VF-%u failed, error: %d\n",
1249 ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
1251 struct ice_hw *hw = &vsi->back->hw;
1254 if (vf->port_vlan_info)
1255 status = ice_fltr_clear_vsi_promisc(hw, vsi->idx, promisc_m,
1256 vf->port_vlan_info & VLAN_VID_MASK);
1257 else if (vsi->num_vlan > 1)
1258 status = ice_fltr_clear_vlan_vsi_promisc(hw, vsi, promisc_m);
1260 status = ice_fltr_clear_vsi_promisc(hw, vsi->idx, promisc_m, 0);
1262 if (status && status != -ENOENT) {
1263 dev_err(ice_pf_to_dev(vsi->back), "disable Tx/Rx filter promiscuous mode on VF-%u failed, error: %d\n",
1271 static void ice_vf_clear_counters(struct ice_vf *vf)
1273 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1277 memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
1278 memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
1282 * ice_vf_pre_vsi_rebuild - tasks to be done prior to VSI rebuild
1283 * @vf: VF to perform pre VSI rebuild tasks
1285 * These tasks are items that don't need to be amortized since they are most
1286 * likely called in a for loop with all VF(s) in the reset_all_vfs() case.
1288 static void ice_vf_pre_vsi_rebuild(struct ice_vf *vf)
1290 ice_vf_clear_counters(vf);
1291 ice_clear_vf_reset_trigger(vf);
1295 * ice_vf_rebuild_aggregator_node_cfg - rebuild aggregator node config
1296 * @vsi: Pointer to VSI
1298 * This function moves VSI into corresponding scheduler aggregator node
1299 * based on cached value of "aggregator node info" per VSI
1301 static void ice_vf_rebuild_aggregator_node_cfg(struct ice_vsi *vsi)
1303 struct ice_pf *pf = vsi->back;
1310 dev = ice_pf_to_dev(pf);
1311 if (vsi->agg_node->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
1313 "agg_id %u already has reached max_num_vsis %u\n",
1314 vsi->agg_node->agg_id, vsi->agg_node->num_vsis);
1318 status = ice_move_vsi_to_agg(pf->hw.port_info, vsi->agg_node->agg_id,
1319 vsi->idx, vsi->tc_cfg.ena_tc);
1321 dev_dbg(dev, "unable to move VSI idx %u into aggregator %u node",
1322 vsi->idx, vsi->agg_node->agg_id);
1324 vsi->agg_node->num_vsis++;
1328 * ice_vf_rebuild_host_cfg - host admin configuration is persistent across reset
1329 * @vf: VF to rebuild host configuration on
1331 static void ice_vf_rebuild_host_cfg(struct ice_vf *vf)
1333 struct device *dev = ice_pf_to_dev(vf->pf);
1334 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1336 ice_vf_set_host_trust_cfg(vf);
1338 if (ice_vf_rebuild_host_mac_cfg(vf))
1339 dev_err(dev, "failed to rebuild default MAC configuration for VF %d\n",
1342 if (ice_vf_rebuild_host_vlan_cfg(vf))
1343 dev_err(dev, "failed to rebuild VLAN configuration for VF %u\n",
1346 if (ice_vf_rebuild_host_tx_rate_cfg(vf))
1347 dev_err(dev, "failed to rebuild Tx rate limiting configuration for VF %u\n",
1350 /* rebuild aggregator node config for main VF VSI */
1351 ice_vf_rebuild_aggregator_node_cfg(vsi);
1355 * ice_vf_rebuild_vsi_with_release - release and setup the VF's VSI
1356 * @vf: VF to release and setup the VSI for
1358 * This is only called when a single VF is being reset (i.e. VFR, VFLR, host VF
1359 * configuration change, etc.).
1361 static int ice_vf_rebuild_vsi_with_release(struct ice_vf *vf)
1363 ice_vf_vsi_release(vf);
1364 if (!ice_vf_vsi_setup(vf))
1371 * ice_vf_rebuild_vsi - rebuild the VF's VSI
1372 * @vf: VF to rebuild the VSI for
1374 * This is only called when all VF(s) are being reset (i.e. PCIe Reset on the
1375 * host, PFR, CORER, etc.).
1377 static int ice_vf_rebuild_vsi(struct ice_vf *vf)
1379 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1380 struct ice_pf *pf = vf->pf;
1382 if (ice_vsi_rebuild(vsi, true)) {
1383 dev_err(ice_pf_to_dev(pf), "failed to rebuild VF %d VSI\n",
1387 /* vsi->idx will remain the same in this case so don't update
1390 vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
1391 vf->lan_vsi_num = vsi->vsi_num;
1397 * ice_vf_set_initialized - VF is ready for VIRTCHNL communication
1398 * @vf: VF to set in initialized state
1400 * After this function the VF will be ready to receive/handle the
1401 * VIRTCHNL_OP_GET_VF_RESOURCES message
1403 static void ice_vf_set_initialized(struct ice_vf *vf)
1405 ice_set_vf_state_qs_dis(vf);
1406 clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
1407 clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
1408 clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
1409 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
1413 * ice_vf_post_vsi_rebuild - tasks to do after the VF's VSI have been rebuilt
1414 * @vf: VF to perform tasks on
1416 static void ice_vf_post_vsi_rebuild(struct ice_vf *vf)
1418 struct ice_pf *pf = vf->pf;
1423 ice_vf_rebuild_host_cfg(vf);
1425 ice_vf_set_initialized(vf);
1426 ice_ena_vf_mappings(vf);
1427 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1431 * ice_reset_all_vfs - reset all allocated VFs in one go
1432 * @pf: pointer to the PF structure
1433 * @is_vflr: true if VFLR was issued, false if not
1435 * First, tell the hardware to reset each VF, then do all the waiting in one
1436 * chunk, and finally finish restoring each VF after the wait. This is useful
1437 * during PF routines which need to reset all VFs, as otherwise it must perform
1438 * these resets in a serialized fashion.
1440 * Returns true if any VFs were reset, and false otherwise.
1442 bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr)
1444 struct device *dev = ice_pf_to_dev(pf);
1445 struct ice_hw *hw = &pf->hw;
1449 /* If we don't have any VFs, then there is nothing to reset */
1450 if (!pf->num_alloc_vfs)
1453 /* clear all malicious info if the VFs are getting reset */
1454 ice_for_each_vf(pf, i)
1455 if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->malvfs, ICE_MAX_VF_COUNT, i))
1456 dev_dbg(dev, "failed to clear malicious VF state for VF %u\n", i);
1458 /* If VFs have been disabled, there is no need to reset */
1459 if (test_and_set_bit(ICE_VF_DIS, pf->state))
1462 /* Begin reset on all VFs at once */
1463 ice_for_each_vf(pf, v)
1464 ice_trigger_vf_reset(&pf->vf[v], is_vflr, true);
1466 /* HW requires some time to make sure it can flush the FIFO for a VF
1467 * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1468 * sequence to make sure that it has completed. We'll keep track of
1469 * the VFs using a simple iterator that increments once that VF has
1470 * finished resetting.
1472 for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1473 /* Check each VF in sequence */
1474 while (v < pf->num_alloc_vfs) {
1478 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1479 if (!(reg & VPGEN_VFRSTAT_VFRD_M)) {
1480 /* only delay if the check failed */
1481 usleep_range(10, 20);
1485 /* If the current VF has finished resetting, move on
1486 * to the next VF in sequence.
1492 /* Display a warning if at least one VF didn't manage to reset in
1493 * time, but continue on with the operation.
1495 if (v < pf->num_alloc_vfs)
1496 dev_warn(dev, "VF reset check timeout\n");
1498 /* free VF resources to begin resetting the VSI state */
1499 ice_for_each_vf(pf, v) {
1502 mutex_lock(&vf->cfg_lock);
1504 vf->driver_caps = 0;
1505 ice_vc_set_default_allowlist(vf);
1507 ice_vf_fdir_exit(vf);
1508 ice_vf_fdir_init(vf);
1509 /* clean VF control VSI when resetting VFs since it should be
1510 * setup only when VF creates its first FDIR rule.
1512 if (vf->ctrl_vsi_idx != ICE_NO_VSI)
1513 ice_vf_ctrl_invalidate_vsi(vf);
1515 ice_vf_pre_vsi_rebuild(vf);
1516 ice_vf_rebuild_vsi(vf);
1517 ice_vf_post_vsi_rebuild(vf);
1519 mutex_unlock(&vf->cfg_lock);
1522 if (ice_is_eswitch_mode_switchdev(pf))
1523 if (ice_eswitch_rebuild(pf))
1524 dev_warn(dev, "eswitch rebuild failed\n");
1527 clear_bit(ICE_VF_DIS, pf->state);
1533 * ice_is_vf_disabled
1534 * @vf: pointer to the VF info
1536 * Returns true if the PF or VF is disabled, false otherwise.
1538 bool ice_is_vf_disabled(struct ice_vf *vf)
1540 struct ice_pf *pf = vf->pf;
1542 /* If the PF has been disabled, there is no need resetting VF until
1543 * PF is active again. Similarly, if the VF has been disabled, this
1544 * means something else is resetting the VF, so we shouldn't continue.
1545 * Otherwise, set disable VF state bit for actual reset, and continue.
1547 return (test_bit(ICE_VF_DIS, pf->state) ||
1548 test_bit(ICE_VF_STATE_DIS, vf->vf_states));
1552 * ice_reset_vf - Reset a particular VF
1553 * @vf: pointer to the VF structure
1554 * @is_vflr: true if VFLR was issued, false if not
1556 * Returns true if the VF is currently in reset, resets successfully, or resets
1557 * are disabled and false otherwise.
1559 bool ice_reset_vf(struct ice_vf *vf, bool is_vflr)
1561 struct ice_pf *pf = vf->pf;
1562 struct ice_vsi *vsi;
1570 lockdep_assert_held(&vf->cfg_lock);
1572 dev = ice_pf_to_dev(pf);
1574 if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
1575 dev_dbg(dev, "Trying to reset VF %d, but all VF resets are disabled\n",
1580 if (ice_is_vf_disabled(vf)) {
1581 dev_dbg(dev, "VF is already disabled, there is no need for resetting it, telling VM, all is fine %d\n",
1586 /* Set VF disable bit state here, before triggering reset */
1587 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
1588 ice_trigger_vf_reset(vf, is_vflr, false);
1590 vsi = ice_get_vf_vsi(vf);
1594 /* Call Disable LAN Tx queue AQ whether or not queues are
1595 * enabled. This is needed for successful completion of VFR.
1597 ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
1598 NULL, ICE_VF_RESET, vf->vf_id, NULL);
1601 /* poll VPGEN_VFRSTAT reg to make sure
1602 * that reset is complete
1604 for (i = 0; i < 10; i++) {
1605 /* VF reset requires driver to first reset the VF and then
1606 * poll the status register to make sure that the reset
1607 * completed successfully.
1609 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1610 if (reg & VPGEN_VFRSTAT_VFRD_M) {
1615 /* only sleep if the reset is not done */
1616 usleep_range(10, 20);
1619 vf->driver_caps = 0;
1620 ice_vc_set_default_allowlist(vf);
1622 /* Display a warning if VF didn't manage to reset in time, but need to
1623 * continue on with the operation.
1626 dev_warn(dev, "VF reset check timeout on VF %d\n", vf->vf_id);
1628 /* disable promiscuous modes in case they were enabled
1629 * ignore any error if disabling process failed
1631 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
1632 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
1633 if (vf->port_vlan_info || vsi->num_vlan)
1634 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
1636 promisc_m = ICE_UCAST_PROMISC_BITS;
1638 if (ice_vf_clear_vsi_promisc(vf, vsi, promisc_m))
1639 dev_err(dev, "disabling promiscuous mode failed\n");
1642 ice_eswitch_del_vf_mac_rule(vf);
1644 ice_vf_fdir_exit(vf);
1645 ice_vf_fdir_init(vf);
1646 /* clean VF control VSI when resetting VF since it should be setup
1647 * only when VF creates its first FDIR rule.
1649 if (vf->ctrl_vsi_idx != ICE_NO_VSI)
1650 ice_vf_ctrl_vsi_release(vf);
1652 ice_vf_pre_vsi_rebuild(vf);
1654 if (ice_vf_rebuild_vsi_with_release(vf)) {
1655 dev_err(dev, "Failed to release and setup the VF%u's VSI\n", vf->vf_id);
1659 ice_vf_post_vsi_rebuild(vf);
1660 vsi = ice_get_vf_vsi(vf);
1661 ice_eswitch_update_repr(vsi);
1662 ice_eswitch_replay_vf_mac_rule(vf);
1664 /* if the VF has been reset allow it to come up again */
1665 if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->malvfs, ICE_MAX_VF_COUNT, vf->vf_id))
1666 dev_dbg(dev, "failed to clear malicious VF state for VF %u\n", i);
1672 * ice_vc_notify_link_state - Inform all VFs on a PF of link status
1673 * @pf: pointer to the PF structure
1675 void ice_vc_notify_link_state(struct ice_pf *pf)
1679 ice_for_each_vf(pf, i)
1680 ice_vc_notify_vf_link_state(&pf->vf[i]);
1684 * ice_vc_notify_reset - Send pending reset message to all VFs
1685 * @pf: pointer to the PF structure
1687 * indicate a pending reset to all VFs on a given PF
1689 void ice_vc_notify_reset(struct ice_pf *pf)
1691 struct virtchnl_pf_event pfe;
1693 if (!pf->num_alloc_vfs)
1696 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1697 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1698 ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
1699 (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
1703 * ice_vc_notify_vf_reset - Notify VF of a reset event
1704 * @vf: pointer to the VF structure
1706 static void ice_vc_notify_vf_reset(struct ice_vf *vf)
1708 struct virtchnl_pf_event pfe;
1715 if (ice_validate_vf_id(pf, vf->vf_id))
1718 /* Bail out if VF is in disabled state, neither initialized, nor active
1719 * state - otherwise proceed with notifications
1721 if ((!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
1722 !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) ||
1723 test_bit(ICE_VF_STATE_DIS, vf->vf_states))
1726 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1727 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1728 ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, VIRTCHNL_OP_EVENT,
1729 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe, sizeof(pfe),
1734 * ice_init_vf_vsi_res - initialize/setup VF VSI resources
1735 * @vf: VF to initialize/setup the VSI for
1737 * This function creates a VSI for the VF, adds a VLAN 0 filter, and sets up the
1738 * VF VSI's broadcast filter and is only used during initial VF creation.
1740 static int ice_init_vf_vsi_res(struct ice_vf *vf)
1742 struct ice_pf *pf = vf->pf;
1743 u8 broadcast[ETH_ALEN];
1744 struct ice_vsi *vsi;
1748 vf->first_vector_idx = ice_calc_vf_first_vector_idx(pf, vf);
1750 dev = ice_pf_to_dev(pf);
1751 vsi = ice_vf_vsi_setup(vf);
1755 err = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
1757 dev_warn(dev, "Failed to add VLAN 0 filter for VF %d\n",
1762 eth_broadcast_addr(broadcast);
1763 err = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
1765 dev_err(dev, "Failed to add broadcast MAC filter for VF %d, error %d\n",
1775 ice_vf_vsi_release(vf);
1780 * ice_start_vfs - start VFs so they are ready to be used by SR-IOV
1781 * @pf: PF the VFs are associated with
1783 static int ice_start_vfs(struct ice_pf *pf)
1785 struct ice_hw *hw = &pf->hw;
1788 ice_for_each_vf(pf, i) {
1789 struct ice_vf *vf = &pf->vf[i];
1791 ice_clear_vf_reset_trigger(vf);
1793 retval = ice_init_vf_vsi_res(vf);
1795 dev_err(ice_pf_to_dev(pf), "Failed to initialize VSI resources for VF %d, error %d\n",
1800 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
1801 ice_ena_vf_mappings(vf);
1802 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1809 for (i = i - 1; i >= 0; i--) {
1810 struct ice_vf *vf = &pf->vf[i];
1812 ice_dis_vf_mappings(vf);
1813 ice_vf_vsi_release(vf);
1820 * ice_set_dflt_settings_vfs - set VF defaults during initialization/creation
1821 * @pf: PF holding reference to all VFs for default configuration
1823 static void ice_set_dflt_settings_vfs(struct ice_pf *pf)
1827 ice_for_each_vf(pf, i) {
1828 struct ice_vf *vf = &pf->vf[i];
1832 vf->vf_sw_id = pf->first_sw;
1833 /* assign default capabilities */
1834 set_bit(ICE_VIRTCHNL_VF_CAP_L2, &vf->vf_caps);
1835 vf->spoofchk = true;
1836 vf->num_vf_qs = pf->num_qps_per_vf;
1837 ice_vc_set_default_allowlist(vf);
1839 /* ctrl_vsi_idx will be set to a valid value only when VF
1840 * creates its first fdir rule.
1842 ice_vf_ctrl_invalidate_vsi(vf);
1843 ice_vf_fdir_init(vf);
1845 ice_vc_set_dflt_vf_ops(&vf->vc_ops);
1847 mutex_init(&vf->cfg_lock);
1852 * ice_alloc_vfs - allocate num_vfs in the PF structure
1853 * @pf: PF to store the allocated VFs in
1854 * @num_vfs: number of VFs to allocate
1856 static int ice_alloc_vfs(struct ice_pf *pf, int num_vfs)
1860 vfs = devm_kcalloc(ice_pf_to_dev(pf), num_vfs, sizeof(*vfs),
1866 pf->num_alloc_vfs = num_vfs;
1872 * ice_ena_vfs - enable VFs so they are ready to be used
1873 * @pf: pointer to the PF structure
1874 * @num_vfs: number of VFs to enable
1876 static int ice_ena_vfs(struct ice_pf *pf, u16 num_vfs)
1878 struct device *dev = ice_pf_to_dev(pf);
1879 struct ice_hw *hw = &pf->hw;
1882 /* Disable global interrupt 0 so we don't try to handle the VFLR. */
1883 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1884 ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
1885 set_bit(ICE_OICR_INTR_DIS, pf->state);
1888 ret = pci_enable_sriov(pf->pdev, num_vfs);
1890 pf->num_alloc_vfs = 0;
1891 goto err_unroll_intr;
1894 ret = ice_alloc_vfs(pf, num_vfs);
1896 goto err_pci_disable_sriov;
1898 if (ice_set_per_vf_res(pf)) {
1899 dev_err(dev, "Not enough resources for %d VFs, try with fewer number of VFs\n",
1902 goto err_unroll_sriov;
1905 ice_set_dflt_settings_vfs(pf);
1907 if (ice_start_vfs(pf)) {
1908 dev_err(dev, "Failed to start VF(s)\n");
1910 goto err_unroll_sriov;
1913 clear_bit(ICE_VF_DIS, pf->state);
1915 ret = ice_eswitch_configure(pf);
1917 goto err_unroll_sriov;
1919 /* rearm global interrupts */
1920 if (test_and_clear_bit(ICE_OICR_INTR_DIS, pf->state))
1921 ice_irq_dynamic_ena(hw, NULL, NULL);
1926 devm_kfree(dev, pf->vf);
1928 pf->num_alloc_vfs = 0;
1929 err_pci_disable_sriov:
1930 pci_disable_sriov(pf->pdev);
1932 /* rearm interrupts here */
1933 ice_irq_dynamic_ena(hw, NULL, NULL);
1934 clear_bit(ICE_OICR_INTR_DIS, pf->state);
1939 * ice_pci_sriov_ena - Enable or change number of VFs
1940 * @pf: pointer to the PF structure
1941 * @num_vfs: number of VFs to allocate
1943 * Returns 0 on success and negative on failure
1945 static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
1947 int pre_existing_vfs = pci_num_vf(pf->pdev);
1948 struct device *dev = ice_pf_to_dev(pf);
1951 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1953 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1956 if (num_vfs > pf->num_vfs_supported) {
1957 dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
1958 num_vfs, pf->num_vfs_supported);
1962 dev_info(dev, "Enabling %d VFs\n", num_vfs);
1963 err = ice_ena_vfs(pf, num_vfs);
1965 dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
1969 set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
1974 * ice_check_sriov_allowed - check if SR-IOV is allowed based on various checks
1975 * @pf: PF to enabled SR-IOV on
1977 static int ice_check_sriov_allowed(struct ice_pf *pf)
1979 struct device *dev = ice_pf_to_dev(pf);
1981 if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
1982 dev_err(dev, "This device is not capable of SR-IOV\n");
1986 if (ice_is_safe_mode(pf)) {
1987 dev_err(dev, "SR-IOV cannot be configured - Device is in Safe Mode\n");
1991 if (!ice_pf_state_is_nominal(pf)) {
1992 dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
2000 * ice_sriov_configure - Enable or change number of VFs via sysfs
2001 * @pdev: pointer to a pci_dev structure
2002 * @num_vfs: number of VFs to allocate or 0 to free VFs
2004 * This function is called when the user updates the number of VFs in sysfs. On
2005 * success return whatever num_vfs was set to by the caller. Return negative on
2008 int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
2010 struct ice_pf *pf = pci_get_drvdata(pdev);
2011 struct device *dev = ice_pf_to_dev(pf);
2014 err = ice_check_sriov_allowed(pf);
2019 if (!pci_vfs_assigned(pdev)) {
2020 ice_mbx_deinit_snapshot(&pf->hw);
2023 ice_enable_lag(pf->lag);
2027 dev_err(dev, "can't free VFs because some are assigned to VMs.\n");
2031 err = ice_mbx_init_snapshot(&pf->hw, num_vfs);
2035 err = ice_pci_sriov_ena(pf, num_vfs);
2037 ice_mbx_deinit_snapshot(&pf->hw);
2042 ice_disable_lag(pf->lag);
2047 * ice_process_vflr_event - Free VF resources via IRQ calls
2048 * @pf: pointer to the PF structure
2050 * called from the VFLR IRQ handler to
2051 * free up VF resources and state variables
2053 void ice_process_vflr_event(struct ice_pf *pf)
2055 struct ice_hw *hw = &pf->hw;
2059 if (!test_and_clear_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
2063 ice_for_each_vf(pf, vf_id) {
2064 struct ice_vf *vf = &pf->vf[vf_id];
2065 u32 reg_idx, bit_idx;
2067 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
2068 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
2069 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
2070 reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
2071 if (reg & BIT(bit_idx)) {
2072 /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
2073 mutex_lock(&vf->cfg_lock);
2074 ice_reset_vf(vf, true);
2075 mutex_unlock(&vf->cfg_lock);
2081 * ice_vc_reset_vf - Perform software reset on the VF after informing the AVF
2082 * @vf: pointer to the VF info
2084 static void ice_vc_reset_vf(struct ice_vf *vf)
2086 ice_vc_notify_vf_reset(vf);
2087 ice_reset_vf(vf, false);
2091 * ice_get_vf_from_pfq - get the VF who owns the PF space queue passed in
2092 * @pf: PF used to index all VFs
2093 * @pfq: queue index relative to the PF's function space
2095 * If no VF is found who owns the pfq then return NULL, otherwise return a
2096 * pointer to the VF who owns the pfq
2098 static struct ice_vf *ice_get_vf_from_pfq(struct ice_pf *pf, u16 pfq)
2102 ice_for_each_vf(pf, vf_id) {
2103 struct ice_vf *vf = &pf->vf[vf_id];
2104 struct ice_vsi *vsi;
2107 vsi = ice_get_vf_vsi(vf);
2109 ice_for_each_rxq(vsi, rxq_idx)
2110 if (vsi->rxq_map[rxq_idx] == pfq)
2118 * ice_globalq_to_pfq - convert from global queue index to PF space queue index
2119 * @pf: PF used for conversion
2120 * @globalq: global queue index used to convert to PF space queue index
2122 static u32 ice_globalq_to_pfq(struct ice_pf *pf, u32 globalq)
2124 return globalq - pf->hw.func_caps.common_cap.rxq_first_id;
2128 * ice_vf_lan_overflow_event - handle LAN overflow event for a VF
2129 * @pf: PF that the LAN overflow event happened on
2130 * @event: structure holding the event information for the LAN overflow event
2132 * Determine if the LAN overflow event was caused by a VF queue. If it was not
2133 * caused by a VF, do nothing. If a VF caused this LAN overflow event trigger a
2134 * reset on the offending VF.
2137 ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event)
2139 u32 gldcb_rtctq, queue;
2142 gldcb_rtctq = le32_to_cpu(event->desc.params.lan_overflow.prtdcb_ruptq);
2143 dev_dbg(ice_pf_to_dev(pf), "GLDCB_RTCTQ: 0x%08x\n", gldcb_rtctq);
2145 /* event returns device global Rx queue number */
2146 queue = (gldcb_rtctq & GLDCB_RTCTQ_RXQNUM_M) >>
2147 GLDCB_RTCTQ_RXQNUM_S;
2149 vf = ice_get_vf_from_pfq(pf, ice_globalq_to_pfq(pf, queue));
2153 mutex_lock(&vf->cfg_lock);
2154 ice_vc_reset_vf(vf);
2155 mutex_unlock(&vf->cfg_lock);
2159 * ice_vc_send_msg_to_vf - Send message to VF
2160 * @vf: pointer to the VF info
2161 * @v_opcode: virtual channel opcode
2162 * @v_retval: virtual channel return value
2163 * @msg: pointer to the msg buffer
2164 * @msglen: msg length
2169 ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
2170 enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
2180 if (ice_validate_vf_id(pf, vf->vf_id))
2183 dev = ice_pf_to_dev(pf);
2185 /* single place to detect unsuccessful return values */
2187 vf->num_inval_msgs++;
2188 dev_info(dev, "VF %d failed opcode %d, retval: %d\n", vf->vf_id,
2189 v_opcode, v_retval);
2190 if (vf->num_inval_msgs > ICE_DFLT_NUM_INVAL_MSGS_ALLOWED) {
2191 dev_err(dev, "Number of invalid messages exceeded for VF %d\n",
2193 dev_err(dev, "Use PF Control I/F to enable the VF\n");
2194 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
2198 vf->num_valid_msgs++;
2199 /* reset the invalid counter, if a valid message is received. */
2200 vf->num_inval_msgs = 0;
2203 aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
2205 if (aq_ret && pf->hw.mailboxq.sq_last_status != ICE_AQ_RC_ENOSYS) {
2206 dev_info(dev, "Unable to send the message to VF %d ret %d aq_err %s\n",
2208 ice_aq_str(pf->hw.mailboxq.sq_last_status));
2216 * ice_vc_get_ver_msg
2217 * @vf: pointer to the VF info
2218 * @msg: pointer to the msg buffer
2220 * called from the VF to request the API version used by the PF
2222 static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
2224 struct virtchnl_version_info info = {
2225 VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
2228 vf->vf_ver = *(struct virtchnl_version_info *)msg;
2229 /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
2230 if (VF_IS_V10(&vf->vf_ver))
2231 info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
2233 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
2234 VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
2235 sizeof(struct virtchnl_version_info));
2239 * ice_vc_get_max_frame_size - get max frame size allowed for VF
2240 * @vf: VF used to determine max frame size
2242 * Max frame size is determined based on the current port's max frame size and
2243 * whether a port VLAN is configured on this VF. The VF is not aware whether
2244 * it's in a port VLAN so the PF needs to account for this in max frame size
2245 * checks and sending the max frame size to the VF.
2247 static u16 ice_vc_get_max_frame_size(struct ice_vf *vf)
2249 struct ice_port_info *pi = ice_vf_get_port_info(vf);
2252 max_frame_size = pi->phy.link_info.max_frame_size;
2254 if (vf->port_vlan_info)
2255 max_frame_size -= VLAN_HLEN;
2257 return max_frame_size;
2261 * ice_vc_get_vf_res_msg
2262 * @vf: pointer to the VF info
2263 * @msg: pointer to the msg buffer
2265 * called from the VF to request its resources
2267 static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
2269 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2270 struct virtchnl_vf_resource *vfres = NULL;
2271 struct ice_pf *pf = vf->pf;
2272 struct ice_vsi *vsi;
2276 if (ice_check_vf_init(pf, vf)) {
2277 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2281 len = sizeof(struct virtchnl_vf_resource);
2283 vfres = kzalloc(len, GFP_KERNEL);
2285 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2289 if (VF_IS_V11(&vf->vf_ver))
2290 vf->driver_caps = *(u32 *)msg;
2292 vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
2293 VIRTCHNL_VF_OFFLOAD_RSS_REG |
2294 VIRTCHNL_VF_OFFLOAD_VLAN;
2296 vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
2297 vsi = ice_get_vf_vsi(vf);
2299 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2303 if (!vsi->info.pvid)
2304 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
2306 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2307 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
2309 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
2310 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
2312 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
2315 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_FDIR_PF)
2316 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_FDIR_PF;
2318 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
2319 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
2321 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
2322 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
2324 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
2325 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
2327 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
2328 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
2330 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2331 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
2333 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
2334 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
2336 if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
2337 vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
2339 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF)
2340 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF;
2342 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_USO)
2343 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_USO;
2345 vfres->num_vsis = 1;
2346 /* Tx and Rx queue are equal for VF */
2347 vfres->num_queue_pairs = vsi->num_txq;
2348 vfres->max_vectors = pf->num_msix_per_vf;
2349 vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
2350 vfres->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
2351 vfres->max_mtu = ice_vc_get_max_frame_size(vf);
2353 vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
2354 vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
2355 vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
2356 ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
2357 vf->hw_lan_addr.addr);
2359 /* match guest capabilities */
2360 vf->driver_caps = vfres->vf_cap_flags;
2362 ice_vc_set_caps_allowlist(vf);
2363 ice_vc_set_working_allowlist(vf);
2365 set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
2368 /* send the response back to the VF */
2369 ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
2377 * ice_vc_reset_vf_msg
2378 * @vf: pointer to the VF info
2380 * called from the VF to reset itself,
2381 * unlike other virtchnl messages, PF driver
2382 * doesn't send the response back to the VF
2384 static void ice_vc_reset_vf_msg(struct ice_vf *vf)
2386 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
2387 ice_reset_vf(vf, false);
2391 * ice_find_vsi_from_id
2392 * @pf: the PF structure to search for the VSI
2393 * @id: ID of the VSI it is searching for
2395 * searches for the VSI with the given ID
2397 static struct ice_vsi *ice_find_vsi_from_id(struct ice_pf *pf, u16 id)
2401 ice_for_each_vsi(pf, i)
2402 if (pf->vsi[i] && pf->vsi[i]->vsi_num == id)
2409 * ice_vc_isvalid_vsi_id
2410 * @vf: pointer to the VF info
2411 * @vsi_id: VF relative VSI ID
2413 * check for the valid VSI ID
2415 bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
2417 struct ice_pf *pf = vf->pf;
2418 struct ice_vsi *vsi;
2420 vsi = ice_find_vsi_from_id(pf, vsi_id);
2422 return (vsi && (vsi->vf_id == vf->vf_id));
2426 * ice_vc_isvalid_q_id
2427 * @vf: pointer to the VF info
2429 * @qid: VSI relative queue ID
2431 * check for the valid queue ID
2433 static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
2435 struct ice_vsi *vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
2436 /* allocated Tx and Rx queues should be always equal for VF VSI */
2437 return (vsi && (qid < vsi->alloc_txq));
2441 * ice_vc_isvalid_ring_len
2442 * @ring_len: length of ring
2444 * check for the valid ring count, should be multiple of ICE_REQ_DESC_MULTIPLE
2447 static bool ice_vc_isvalid_ring_len(u16 ring_len)
2449 return ring_len == 0 ||
2450 (ring_len >= ICE_MIN_NUM_DESC &&
2451 ring_len <= ICE_MAX_NUM_DESC &&
2452 !(ring_len % ICE_REQ_DESC_MULTIPLE));
2456 * ice_vc_validate_pattern
2457 * @vf: pointer to the VF info
2458 * @proto: virtchnl protocol headers
2460 * validate the pattern is supported or not.
2462 * Return: true on success, false on error.
2465 ice_vc_validate_pattern(struct ice_vf *vf, struct virtchnl_proto_hdrs *proto)
2467 bool is_ipv4 = false;
2468 bool is_ipv6 = false;
2469 bool is_udp = false;
2473 while (i < proto->count &&
2474 proto->proto_hdr[i].type != VIRTCHNL_PROTO_HDR_NONE) {
2475 switch (proto->proto_hdr[i].type) {
2476 case VIRTCHNL_PROTO_HDR_ETH:
2477 ptype = ICE_PTYPE_MAC_PAY;
2479 case VIRTCHNL_PROTO_HDR_IPV4:
2480 ptype = ICE_PTYPE_IPV4_PAY;
2483 case VIRTCHNL_PROTO_HDR_IPV6:
2484 ptype = ICE_PTYPE_IPV6_PAY;
2487 case VIRTCHNL_PROTO_HDR_UDP:
2489 ptype = ICE_PTYPE_IPV4_UDP_PAY;
2491 ptype = ICE_PTYPE_IPV6_UDP_PAY;
2494 case VIRTCHNL_PROTO_HDR_TCP:
2496 ptype = ICE_PTYPE_IPV4_TCP_PAY;
2498 ptype = ICE_PTYPE_IPV6_TCP_PAY;
2500 case VIRTCHNL_PROTO_HDR_SCTP:
2502 ptype = ICE_PTYPE_IPV4_SCTP_PAY;
2504 ptype = ICE_PTYPE_IPV6_SCTP_PAY;
2506 case VIRTCHNL_PROTO_HDR_GTPU_IP:
2507 case VIRTCHNL_PROTO_HDR_GTPU_EH:
2509 ptype = ICE_MAC_IPV4_GTPU;
2511 ptype = ICE_MAC_IPV6_GTPU;
2513 case VIRTCHNL_PROTO_HDR_L2TPV3:
2515 ptype = ICE_MAC_IPV4_L2TPV3;
2517 ptype = ICE_MAC_IPV6_L2TPV3;
2519 case VIRTCHNL_PROTO_HDR_ESP:
2521 ptype = is_udp ? ICE_MAC_IPV4_NAT_T_ESP :
2524 ptype = is_udp ? ICE_MAC_IPV6_NAT_T_ESP :
2527 case VIRTCHNL_PROTO_HDR_AH:
2529 ptype = ICE_MAC_IPV4_AH;
2531 ptype = ICE_MAC_IPV6_AH;
2533 case VIRTCHNL_PROTO_HDR_PFCP:
2535 ptype = ICE_MAC_IPV4_PFCP_SESSION;
2537 ptype = ICE_MAC_IPV6_PFCP_SESSION;
2546 return ice_hw_ptype_ena(&vf->pf->hw, ptype);
2550 * ice_vc_parse_rss_cfg - parses hash fields and headers from
2551 * a specific virtchnl RSS cfg
2552 * @hw: pointer to the hardware
2553 * @rss_cfg: pointer to the virtchnl RSS cfg
2554 * @addl_hdrs: pointer to the protocol header fields (ICE_FLOW_SEG_HDR_*)
2556 * @hash_flds: pointer to the hash bit fields (ICE_FLOW_HASH_*) to configure
2558 * Return true if all the protocol header and hash fields in the RSS cfg could
2559 * be parsed, else return false
2561 * This function parses the virtchnl RSS cfg to be the intended
2562 * hash fields and the intended header for RSS configuration
2565 ice_vc_parse_rss_cfg(struct ice_hw *hw, struct virtchnl_rss_cfg *rss_cfg,
2566 u32 *addl_hdrs, u64 *hash_flds)
2568 const struct ice_vc_hash_field_match_type *hf_list;
2569 const struct ice_vc_hdr_match_type *hdr_list;
2570 int i, hf_list_len, hdr_list_len;
2572 hf_list = ice_vc_hash_field_list;
2573 hf_list_len = ARRAY_SIZE(ice_vc_hash_field_list);
2574 hdr_list = ice_vc_hdr_list;
2575 hdr_list_len = ARRAY_SIZE(ice_vc_hdr_list);
2577 for (i = 0; i < rss_cfg->proto_hdrs.count; i++) {
2578 struct virtchnl_proto_hdr *proto_hdr =
2579 &rss_cfg->proto_hdrs.proto_hdr[i];
2580 bool hdr_found = false;
2583 /* Find matched ice headers according to virtchnl headers. */
2584 for (j = 0; j < hdr_list_len; j++) {
2585 struct ice_vc_hdr_match_type hdr_map = hdr_list[j];
2587 if (proto_hdr->type == hdr_map.vc_hdr) {
2588 *addl_hdrs |= hdr_map.ice_hdr;
2596 /* Find matched ice hash fields according to
2597 * virtchnl hash fields.
2599 for (j = 0; j < hf_list_len; j++) {
2600 struct ice_vc_hash_field_match_type hf_map = hf_list[j];
2602 if (proto_hdr->type == hf_map.vc_hdr &&
2603 proto_hdr->field_selector == hf_map.vc_hash_field) {
2604 *hash_flds |= hf_map.ice_hash_field;
2614 * ice_vf_adv_rss_offload_ena - determine if capabilities support advanced
2616 * @caps: VF driver negotiated capabilities
2618 * Return true if VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF capability is set,
2621 static bool ice_vf_adv_rss_offload_ena(u32 caps)
2623 return !!(caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF);
2627 * ice_vc_handle_rss_cfg
2628 * @vf: pointer to the VF info
2629 * @msg: pointer to the message buffer
2630 * @add: add a RSS config if true, otherwise delete a RSS config
2632 * This function adds/deletes a RSS config
2634 static int ice_vc_handle_rss_cfg(struct ice_vf *vf, u8 *msg, bool add)
2636 u32 v_opcode = add ? VIRTCHNL_OP_ADD_RSS_CFG : VIRTCHNL_OP_DEL_RSS_CFG;
2637 struct virtchnl_rss_cfg *rss_cfg = (struct virtchnl_rss_cfg *)msg;
2638 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2639 struct device *dev = ice_pf_to_dev(vf->pf);
2640 struct ice_hw *hw = &vf->pf->hw;
2641 struct ice_vsi *vsi;
2643 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2644 dev_dbg(dev, "VF %d attempting to configure RSS, but RSS is not supported by the PF\n",
2646 v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
2650 if (!ice_vf_adv_rss_offload_ena(vf->driver_caps)) {
2651 dev_dbg(dev, "VF %d attempting to configure RSS, but Advanced RSS offload is not supported\n",
2653 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2657 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2658 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2662 if (rss_cfg->proto_hdrs.count > VIRTCHNL_MAX_NUM_PROTO_HDRS ||
2663 rss_cfg->rss_algorithm < VIRTCHNL_RSS_ALG_TOEPLITZ_ASYMMETRIC ||
2664 rss_cfg->rss_algorithm > VIRTCHNL_RSS_ALG_XOR_SYMMETRIC) {
2665 dev_dbg(dev, "VF %d attempting to configure RSS, but RSS configuration is not valid\n",
2667 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2671 vsi = ice_get_vf_vsi(vf);
2673 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2677 if (!ice_vc_validate_pattern(vf, &rss_cfg->proto_hdrs)) {
2678 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2682 if (rss_cfg->rss_algorithm == VIRTCHNL_RSS_ALG_R_ASYMMETRIC) {
2683 struct ice_vsi_ctx *ctx;
2684 u8 lut_type, hash_type;
2687 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
2688 hash_type = add ? ICE_AQ_VSI_Q_OPT_RSS_XOR :
2689 ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
2691 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2693 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2697 ctx->info.q_opt_rss = ((lut_type <<
2698 ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
2699 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
2701 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
2703 /* Preserve existing queueing option setting */
2704 ctx->info.q_opt_rss |= (vsi->info.q_opt_rss &
2705 ICE_AQ_VSI_Q_OPT_RSS_GBL_LUT_M);
2706 ctx->info.q_opt_tc = vsi->info.q_opt_tc;
2707 ctx->info.q_opt_flags = vsi->info.q_opt_rss;
2709 ctx->info.valid_sections =
2710 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
2712 status = ice_update_vsi(hw, vsi->idx, ctx, NULL);
2714 dev_err(dev, "update VSI for RSS failed, err %d aq_err %s\n",
2715 status, ice_aq_str(hw->adminq.sq_last_status));
2716 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2718 vsi->info.q_opt_rss = ctx->info.q_opt_rss;
2723 u32 addl_hdrs = ICE_FLOW_SEG_HDR_NONE;
2724 u64 hash_flds = ICE_HASH_INVALID;
2726 if (!ice_vc_parse_rss_cfg(hw, rss_cfg, &addl_hdrs,
2728 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2733 if (ice_add_rss_cfg(hw, vsi->idx, hash_flds,
2735 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2736 dev_err(dev, "ice_add_rss_cfg failed for vsi = %d, v_ret = %d\n",
2737 vsi->vsi_num, v_ret);
2742 status = ice_rem_rss_cfg(hw, vsi->idx, hash_flds,
2744 /* We just ignore -ENOENT, because if two configurations
2745 * share the same profile remove one of them actually
2746 * removes both, since the profile is deleted.
2748 if (status && status != -ENOENT) {
2749 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2750 dev_err(dev, "ice_rem_rss_cfg failed for VF ID:%d, error:%d\n",
2757 return ice_vc_send_msg_to_vf(vf, v_opcode, v_ret, NULL, 0);
2761 * ice_vc_config_rss_key
2762 * @vf: pointer to the VF info
2763 * @msg: pointer to the msg buffer
2765 * Configure the VF's RSS key
2767 static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
2769 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2770 struct virtchnl_rss_key *vrk =
2771 (struct virtchnl_rss_key *)msg;
2772 struct ice_vsi *vsi;
2774 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2775 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2779 if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
2780 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2784 if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
2785 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2789 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2790 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2794 vsi = ice_get_vf_vsi(vf);
2796 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2800 if (ice_set_rss_key(vsi, vrk->key))
2801 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2803 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
2808 * ice_vc_config_rss_lut
2809 * @vf: pointer to the VF info
2810 * @msg: pointer to the msg buffer
2812 * Configure the VF's RSS LUT
2814 static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
2816 struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
2817 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2818 struct ice_vsi *vsi;
2820 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2821 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2825 if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
2826 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2830 if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE) {
2831 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2835 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2836 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2840 vsi = ice_get_vf_vsi(vf);
2842 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2846 if (ice_set_rss_lut(vsi, vrl->lut, ICE_VSIQF_HLUT_ARRAY_SIZE))
2847 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2849 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
2854 * ice_wait_on_vf_reset - poll to make sure a given VF is ready after reset
2855 * @vf: The VF being resseting
2857 * The max poll time is about ~800ms, which is about the maximum time it takes
2858 * for a VF to be reset and/or a VF driver to be removed.
2860 static void ice_wait_on_vf_reset(struct ice_vf *vf)
2864 for (i = 0; i < ICE_MAX_VF_RESET_TRIES; i++) {
2865 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
2867 msleep(ICE_MAX_VF_RESET_SLEEP_MS);
2872 * ice_check_vf_ready_for_cfg - check if VF is ready to be configured/queried
2873 * @vf: VF to check if it's ready to be configured/queried
2875 * The purpose of this function is to make sure the VF is not in reset, not
2876 * disabled, and initialized so it can be configured and/or queried by a host
2879 int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
2883 ice_wait_on_vf_reset(vf);
2885 if (ice_is_vf_disabled(vf))
2889 if (ice_check_vf_init(pf, vf))
2896 * ice_set_vf_spoofchk
2897 * @netdev: network interface device structure
2898 * @vf_id: VF identifier
2899 * @ena: flag to enable or disable feature
2901 * Enable or disable VF spoof checking
2903 int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
2905 struct ice_netdev_priv *np = netdev_priv(netdev);
2906 struct ice_pf *pf = np->vsi->back;
2907 struct ice_vsi_ctx *ctx;
2908 struct ice_vsi *vf_vsi;
2913 dev = ice_pf_to_dev(pf);
2914 if (ice_validate_vf_id(pf, vf_id))
2917 vf = &pf->vf[vf_id];
2918 ret = ice_check_vf_ready_for_cfg(vf);
2922 vf_vsi = ice_get_vf_vsi(vf);
2924 netdev_err(netdev, "VSI %d for VF %d is null\n",
2925 vf->lan_vsi_idx, vf->vf_id);
2929 if (vf_vsi->type != ICE_VSI_VF) {
2930 netdev_err(netdev, "Type %d of VSI %d for VF %d is no ICE_VSI_VF\n",
2931 vf_vsi->type, vf_vsi->vsi_num, vf->vf_id);
2935 if (ena == vf->spoofchk) {
2936 dev_dbg(dev, "VF spoofchk already %s\n", ena ? "ON" : "OFF");
2940 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2944 ctx->info.sec_flags = vf_vsi->info.sec_flags;
2945 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
2947 ctx->info.sec_flags |=
2948 ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
2949 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2950 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
2952 ctx->info.sec_flags &=
2953 ~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
2954 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2955 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
2958 ret = ice_update_vsi(&pf->hw, vf_vsi->idx, ctx, NULL);
2960 dev_err(dev, "Failed to %sable spoofchk on VF %d VSI %d\n error %d\n",
2961 ena ? "en" : "dis", vf->vf_id, vf_vsi->vsi_num, ret);
2965 /* only update spoofchk state and VSI context on success */
2966 vf_vsi->info.sec_flags = ctx->info.sec_flags;
2975 * ice_is_any_vf_in_promisc - check if any VF(s) are in promiscuous mode
2976 * @pf: PF structure for accessing VF(s)
2978 * Return false if no VF(s) are in unicast and/or multicast promiscuous mode,
2981 bool ice_is_any_vf_in_promisc(struct ice_pf *pf)
2985 ice_for_each_vf(pf, vf_idx) {
2986 struct ice_vf *vf = &pf->vf[vf_idx];
2988 /* found a VF that has promiscuous mode configured */
2989 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2990 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
2998 * ice_vc_cfg_promiscuous_mode_msg
2999 * @vf: pointer to the VF info
3000 * @msg: pointer to the msg buffer
3002 * called from the VF to configure VF VSIs promiscuous mode
3004 static int ice_vc_cfg_promiscuous_mode_msg(struct ice_vf *vf, u8 *msg)
3006 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3007 bool rm_promisc, alluni = false, allmulti = false;
3008 struct virtchnl_promisc_info *info =
3009 (struct virtchnl_promisc_info *)msg;
3010 int mcast_err = 0, ucast_err = 0;
3011 struct ice_pf *pf = vf->pf;
3012 struct ice_vsi *vsi;
3016 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3017 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3021 if (!ice_vc_isvalid_vsi_id(vf, info->vsi_id)) {
3022 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3026 vsi = ice_get_vf_vsi(vf);
3028 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3032 dev = ice_pf_to_dev(pf);
3033 if (!test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
3034 dev_err(dev, "Unprivileged VF %d is attempting to configure promiscuous mode\n",
3036 /* Leave v_ret alone, lie to the VF on purpose. */
3040 if (info->flags & FLAG_VF_UNICAST_PROMISC)
3043 if (info->flags & FLAG_VF_MULTICAST_PROMISC)
3046 rm_promisc = !allmulti && !alluni;
3048 if (vsi->num_vlan || vf->port_vlan_info) {
3050 ret = ice_cfg_vlan_pruning(vsi, true);
3052 ret = ice_cfg_vlan_pruning(vsi, false);
3054 dev_err(dev, "Failed to configure VLAN pruning in promiscuous mode\n");
3055 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3060 if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
3061 bool set_dflt_vsi = alluni || allmulti;
3063 if (set_dflt_vsi && !ice_is_dflt_vsi_in_use(pf->first_sw))
3064 /* only attempt to set the default forwarding VSI if
3065 * it's not currently set
3067 ret = ice_set_dflt_vsi(pf->first_sw, vsi);
3068 else if (!set_dflt_vsi &&
3069 ice_is_vsi_dflt_vsi(pf->first_sw, vsi))
3070 /* only attempt to free the default forwarding VSI if we
3073 ret = ice_clear_dflt_vsi(pf->first_sw);
3076 dev_err(dev, "%sable VF %d as the default VSI failed, error %d\n",
3077 set_dflt_vsi ? "en" : "dis", vf->vf_id, ret);
3078 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
3082 u8 mcast_m, ucast_m;
3084 if (vf->port_vlan_info || vsi->num_vlan > 1) {
3085 mcast_m = ICE_MCAST_VLAN_PROMISC_BITS;
3086 ucast_m = ICE_UCAST_VLAN_PROMISC_BITS;
3088 mcast_m = ICE_MCAST_PROMISC_BITS;
3089 ucast_m = ICE_UCAST_PROMISC_BITS;
3093 ucast_err = ice_vf_set_vsi_promisc(vf, vsi, ucast_m);
3095 ucast_err = ice_vf_clear_vsi_promisc(vf, vsi, ucast_m);
3098 mcast_err = ice_vf_set_vsi_promisc(vf, vsi, mcast_m);
3100 mcast_err = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
3102 if (ucast_err || mcast_err)
3103 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3108 !test_and_set_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
3109 dev_info(dev, "VF %u successfully set multicast promiscuous mode\n",
3111 else if (!allmulti && test_and_clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
3112 dev_info(dev, "VF %u successfully unset multicast promiscuous mode\n",
3117 if (alluni && !test_and_set_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
3118 dev_info(dev, "VF %u successfully set unicast promiscuous mode\n",
3120 else if (!alluni && test_and_clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
3121 dev_info(dev, "VF %u successfully unset unicast promiscuous mode\n",
3126 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
3131 * ice_vc_get_stats_msg
3132 * @vf: pointer to the VF info
3133 * @msg: pointer to the msg buffer
3135 * called from the VF to get VSI stats
3137 static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
3139 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3140 struct virtchnl_queue_select *vqs =
3141 (struct virtchnl_queue_select *)msg;
3142 struct ice_eth_stats stats = { 0 };
3143 struct ice_vsi *vsi;
3145 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3146 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3150 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3151 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3155 vsi = ice_get_vf_vsi(vf);
3157 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3161 ice_update_eth_stats(vsi);
3163 stats = vsi->eth_stats;
3166 /* send the response to the VF */
3167 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
3168 (u8 *)&stats, sizeof(stats));
3172 * ice_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTCHNL
3173 * @vqs: virtchnl_queue_select structure containing bitmaps to validate
3175 * Return true on successful validation, else false
3177 static bool ice_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
3179 if ((!vqs->rx_queues && !vqs->tx_queues) ||
3180 vqs->rx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF) ||
3181 vqs->tx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF))
3188 * ice_vf_ena_txq_interrupt - enable Tx queue interrupt via QINT_TQCTL
3189 * @vsi: VSI of the VF to configure
3190 * @q_idx: VF queue index used to determine the queue in the PF's space
3192 static void ice_vf_ena_txq_interrupt(struct ice_vsi *vsi, u32 q_idx)
3194 struct ice_hw *hw = &vsi->back->hw;
3195 u32 pfq = vsi->txq_map[q_idx];
3198 reg = rd32(hw, QINT_TQCTL(pfq));
3200 /* MSI-X index 0 in the VF's space is always for the OICR, which means
3201 * this is most likely a poll mode VF driver, so don't enable an
3202 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
3204 if (!(reg & QINT_TQCTL_MSIX_INDX_M))
3207 wr32(hw, QINT_TQCTL(pfq), reg | QINT_TQCTL_CAUSE_ENA_M);
3211 * ice_vf_ena_rxq_interrupt - enable Tx queue interrupt via QINT_RQCTL
3212 * @vsi: VSI of the VF to configure
3213 * @q_idx: VF queue index used to determine the queue in the PF's space
3215 static void ice_vf_ena_rxq_interrupt(struct ice_vsi *vsi, u32 q_idx)
3217 struct ice_hw *hw = &vsi->back->hw;
3218 u32 pfq = vsi->rxq_map[q_idx];
3221 reg = rd32(hw, QINT_RQCTL(pfq));
3223 /* MSI-X index 0 in the VF's space is always for the OICR, which means
3224 * this is most likely a poll mode VF driver, so don't enable an
3225 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
3227 if (!(reg & QINT_RQCTL_MSIX_INDX_M))
3230 wr32(hw, QINT_RQCTL(pfq), reg | QINT_RQCTL_CAUSE_ENA_M);
3235 * @vf: pointer to the VF info
3236 * @msg: pointer to the msg buffer
3238 * called from the VF to enable all or specific queue(s)
3240 static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
3242 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3243 struct virtchnl_queue_select *vqs =
3244 (struct virtchnl_queue_select *)msg;
3245 struct ice_vsi *vsi;
3246 unsigned long q_map;
3249 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3250 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3254 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3255 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3259 if (!ice_vc_validate_vqs_bitmaps(vqs)) {
3260 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3264 vsi = ice_get_vf_vsi(vf);
3266 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3270 /* Enable only Rx rings, Tx rings were enabled by the FW when the
3271 * Tx queue group list was configured and the context bits were
3272 * programmed using ice_vsi_cfg_txqs
3274 q_map = vqs->rx_queues;
3275 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3276 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3277 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3281 /* Skip queue if enabled */
3282 if (test_bit(vf_q_id, vf->rxq_ena))
3285 if (ice_vsi_ctrl_one_rx_ring(vsi, true, vf_q_id, true)) {
3286 dev_err(ice_pf_to_dev(vsi->back), "Failed to enable Rx ring %d on VSI %d\n",
3287 vf_q_id, vsi->vsi_num);
3288 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3292 ice_vf_ena_rxq_interrupt(vsi, vf_q_id);
3293 set_bit(vf_q_id, vf->rxq_ena);
3296 q_map = vqs->tx_queues;
3297 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3298 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3299 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3303 /* Skip queue if enabled */
3304 if (test_bit(vf_q_id, vf->txq_ena))
3307 ice_vf_ena_txq_interrupt(vsi, vf_q_id);
3308 set_bit(vf_q_id, vf->txq_ena);
3311 /* Set flag to indicate that queues are enabled */
3312 if (v_ret == VIRTCHNL_STATUS_SUCCESS)
3313 set_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
3316 /* send the response to the VF */
3317 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
3323 * @vf: pointer to the VF info
3324 * @msg: pointer to the msg buffer
3326 * called from the VF to disable all or specific
3329 static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
3331 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3332 struct virtchnl_queue_select *vqs =
3333 (struct virtchnl_queue_select *)msg;
3334 struct ice_vsi *vsi;
3335 unsigned long q_map;
3338 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
3339 !test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states)) {
3340 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3344 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3345 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3349 if (!ice_vc_validate_vqs_bitmaps(vqs)) {
3350 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3354 vsi = ice_get_vf_vsi(vf);
3356 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3360 if (vqs->tx_queues) {
3361 q_map = vqs->tx_queues;
3363 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3364 struct ice_tx_ring *ring = vsi->tx_rings[vf_q_id];
3365 struct ice_txq_meta txq_meta = { 0 };
3367 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3368 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3372 /* Skip queue if not enabled */
3373 if (!test_bit(vf_q_id, vf->txq_ena))
3376 ice_fill_txq_meta(vsi, ring, &txq_meta);
3378 if (ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, vf->vf_id,
3380 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Tx ring %d on VSI %d\n",
3381 vf_q_id, vsi->vsi_num);
3382 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3386 /* Clear enabled queues flag */
3387 clear_bit(vf_q_id, vf->txq_ena);
3391 q_map = vqs->rx_queues;
3392 /* speed up Rx queue disable by batching them if possible */
3394 bitmap_equal(&q_map, vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF)) {
3395 if (ice_vsi_stop_all_rx_rings(vsi)) {
3396 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop all Rx rings on VSI %d\n",
3398 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3402 bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
3404 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3405 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3406 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3410 /* Skip queue if not enabled */
3411 if (!test_bit(vf_q_id, vf->rxq_ena))
3414 if (ice_vsi_ctrl_one_rx_ring(vsi, false, vf_q_id,
3416 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Rx ring %d on VSI %d\n",
3417 vf_q_id, vsi->vsi_num);
3418 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3422 /* Clear enabled queues flag */
3423 clear_bit(vf_q_id, vf->rxq_ena);
3427 /* Clear enabled queues flag */
3428 if (v_ret == VIRTCHNL_STATUS_SUCCESS && ice_vf_has_no_qs_ena(vf))
3429 clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
3432 /* send the response to the VF */
3433 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
3439 * @vf: pointer to the VF info
3440 * @vsi: the VSI being configured
3441 * @vector_id: vector ID
3442 * @map: vector map for mapping vectors to queues
3443 * @q_vector: structure for interrupt vector
3444 * configure the IRQ to queue map
3447 ice_cfg_interrupt(struct ice_vf *vf, struct ice_vsi *vsi, u16 vector_id,
3448 struct virtchnl_vector_map *map,
3449 struct ice_q_vector *q_vector)
3451 u16 vsi_q_id, vsi_q_id_idx;
3454 q_vector->num_ring_rx = 0;
3455 q_vector->num_ring_tx = 0;
3457 qmap = map->rxq_map;
3458 for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
3459 vsi_q_id = vsi_q_id_idx;
3461 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
3462 return VIRTCHNL_STATUS_ERR_PARAM;
3464 q_vector->num_ring_rx++;
3465 q_vector->rx.itr_idx = map->rxitr_idx;
3466 vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
3467 ice_cfg_rxq_interrupt(vsi, vsi_q_id, vector_id,
3468 q_vector->rx.itr_idx);
3471 qmap = map->txq_map;
3472 for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
3473 vsi_q_id = vsi_q_id_idx;
3475 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
3476 return VIRTCHNL_STATUS_ERR_PARAM;
3478 q_vector->num_ring_tx++;
3479 q_vector->tx.itr_idx = map->txitr_idx;
3480 vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
3481 ice_cfg_txq_interrupt(vsi, vsi_q_id, vector_id,
3482 q_vector->tx.itr_idx);
3485 return VIRTCHNL_STATUS_SUCCESS;
3489 * ice_vc_cfg_irq_map_msg
3490 * @vf: pointer to the VF info
3491 * @msg: pointer to the msg buffer
3493 * called from the VF to configure the IRQ to queue map
3495 static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
3497 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3498 u16 num_q_vectors_mapped, vsi_id, vector_id;
3499 struct virtchnl_irq_map_info *irqmap_info;
3500 struct virtchnl_vector_map *map;
3501 struct ice_pf *pf = vf->pf;
3502 struct ice_vsi *vsi;
3505 irqmap_info = (struct virtchnl_irq_map_info *)msg;
3506 num_q_vectors_mapped = irqmap_info->num_vectors;
3508 /* Check to make sure number of VF vectors mapped is not greater than
3509 * number of VF vectors originally allocated, and check that
3510 * there is actually at least a single VF queue vector mapped
3512 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
3513 pf->num_msix_per_vf < num_q_vectors_mapped ||
3514 !num_q_vectors_mapped) {
3515 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3519 vsi = ice_get_vf_vsi(vf);
3521 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3525 for (i = 0; i < num_q_vectors_mapped; i++) {
3526 struct ice_q_vector *q_vector;
3528 map = &irqmap_info->vecmap[i];
3530 vector_id = map->vector_id;
3531 vsi_id = map->vsi_id;
3532 /* vector_id is always 0-based for each VF, and can never be
3533 * larger than or equal to the max allowed interrupts per VF
3535 if (!(vector_id < pf->num_msix_per_vf) ||
3536 !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
3537 (!vector_id && (map->rxq_map || map->txq_map))) {
3538 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3542 /* No need to map VF miscellaneous or rogue vector */
3546 /* Subtract non queue vector from vector_id passed by VF
3547 * to get actual number of VSI queue vector array index
3549 q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
3551 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3555 /* lookout for the invalid queue index */
3556 v_ret = (enum virtchnl_status_code)
3557 ice_cfg_interrupt(vf, vsi, vector_id, map, q_vector);
3563 /* send the response to the VF */
3564 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
3570 * @vf: pointer to the VF info
3571 * @msg: pointer to the msg buffer
3573 * called from the VF to configure the Rx/Tx queues
3575 static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
3577 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3578 struct virtchnl_vsi_queue_config_info *qci =
3579 (struct virtchnl_vsi_queue_config_info *)msg;
3580 struct virtchnl_queue_pair_info *qpi;
3581 struct ice_pf *pf = vf->pf;
3582 struct ice_vsi *vsi;
3585 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3586 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3590 if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
3591 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3595 vsi = ice_get_vf_vsi(vf);
3597 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3601 if (qci->num_queue_pairs > ICE_MAX_RSS_QS_PER_VF ||
3602 qci->num_queue_pairs > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
3603 dev_err(ice_pf_to_dev(pf), "VF-%d requesting more than supported number of queues: %d\n",
3604 vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
3605 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3609 for (i = 0; i < qci->num_queue_pairs; i++) {
3610 qpi = &qci->qpair[i];
3611 if (qpi->txq.vsi_id != qci->vsi_id ||
3612 qpi->rxq.vsi_id != qci->vsi_id ||
3613 qpi->rxq.queue_id != qpi->txq.queue_id ||
3614 qpi->txq.headwb_enabled ||
3615 !ice_vc_isvalid_ring_len(qpi->txq.ring_len) ||
3616 !ice_vc_isvalid_ring_len(qpi->rxq.ring_len) ||
3617 !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
3618 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3622 q_idx = qpi->rxq.queue_id;
3624 /* make sure selected "q_idx" is in valid range of queues
3625 * for selected "vsi"
3627 if (q_idx >= vsi->alloc_txq || q_idx >= vsi->alloc_rxq) {
3628 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3632 /* copy Tx queue info from VF into VSI */
3633 if (qpi->txq.ring_len > 0) {
3634 vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
3635 vsi->tx_rings[i]->count = qpi->txq.ring_len;
3636 if (ice_vsi_cfg_single_txq(vsi, vsi->tx_rings, q_idx)) {
3637 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3642 /* copy Rx queue info from VF into VSI */
3643 if (qpi->rxq.ring_len > 0) {
3644 u16 max_frame_size = ice_vc_get_max_frame_size(vf);
3646 vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
3647 vsi->rx_rings[i]->count = qpi->rxq.ring_len;
3649 if (qpi->rxq.databuffer_size != 0 &&
3650 (qpi->rxq.databuffer_size > ((16 * 1024) - 128) ||
3651 qpi->rxq.databuffer_size < 1024)) {
3652 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3655 vsi->rx_buf_len = qpi->rxq.databuffer_size;
3656 vsi->rx_rings[i]->rx_buf_len = vsi->rx_buf_len;
3657 if (qpi->rxq.max_pkt_size > max_frame_size ||
3658 qpi->rxq.max_pkt_size < 64) {
3659 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3663 vsi->max_frame = qpi->rxq.max_pkt_size;
3664 /* add space for the port VLAN since the VF driver is not
3665 * expected to account for it in the MTU calculation
3667 if (vf->port_vlan_info)
3668 vsi->max_frame += VLAN_HLEN;
3670 if (ice_vsi_cfg_single_rxq(vsi, q_idx)) {
3671 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3678 /* send the response to the VF */
3679 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES, v_ret,
3685 * @vf: pointer to the VF info
3687 static bool ice_is_vf_trusted(struct ice_vf *vf)
3689 return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
3693 * ice_can_vf_change_mac
3694 * @vf: pointer to the VF info
3696 * Return true if the VF is allowed to change its MAC filters, false otherwise
3698 static bool ice_can_vf_change_mac(struct ice_vf *vf)
3700 /* If the VF MAC address has been set administratively (via the
3701 * ndo_set_vf_mac command), then deny permission to the VF to
3702 * add/delete unicast MAC addresses, unless the VF is trusted
3704 if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
3711 * ice_vc_ether_addr_type - get type of virtchnl_ether_addr
3712 * @vc_ether_addr: used to extract the type
3715 ice_vc_ether_addr_type(struct virtchnl_ether_addr *vc_ether_addr)
3717 return (vc_ether_addr->type & VIRTCHNL_ETHER_ADDR_TYPE_MASK);
3721 * ice_is_vc_addr_legacy - check if the MAC address is from an older VF
3722 * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
3725 ice_is_vc_addr_legacy(struct virtchnl_ether_addr *vc_ether_addr)
3727 u8 type = ice_vc_ether_addr_type(vc_ether_addr);
3729 return (type == VIRTCHNL_ETHER_ADDR_LEGACY);
3733 * ice_is_vc_addr_primary - check if the MAC address is the VF's primary MAC
3734 * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
3736 * This function should only be called when the MAC address in
3737 * virtchnl_ether_addr is a valid unicast MAC
3740 ice_is_vc_addr_primary(struct virtchnl_ether_addr __maybe_unused *vc_ether_addr)
3742 u8 type = ice_vc_ether_addr_type(vc_ether_addr);
3744 return (type == VIRTCHNL_ETHER_ADDR_PRIMARY);
3748 * ice_vfhw_mac_add - update the VF's cached hardware MAC if allowed
3750 * @vc_ether_addr: structure from VIRTCHNL with MAC to add
3753 ice_vfhw_mac_add(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
3755 u8 *mac_addr = vc_ether_addr->addr;
3757 if (!is_valid_ether_addr(mac_addr))
3760 /* only allow legacy VF drivers to set the device and hardware MAC if it
3761 * is zero and allow new VF drivers to set the hardware MAC if the type
3762 * was correctly specified over VIRTCHNL
3764 if ((ice_is_vc_addr_legacy(vc_ether_addr) &&
3765 is_zero_ether_addr(vf->hw_lan_addr.addr)) ||
3766 ice_is_vc_addr_primary(vc_ether_addr)) {
3767 ether_addr_copy(vf->dev_lan_addr.addr, mac_addr);
3768 ether_addr_copy(vf->hw_lan_addr.addr, mac_addr);
3771 /* hardware and device MACs are already set, but its possible that the
3772 * VF driver sent the VIRTCHNL_OP_ADD_ETH_ADDR message before the
3773 * VIRTCHNL_OP_DEL_ETH_ADDR when trying to update its MAC, so save it
3774 * away for the legacy VF driver case as it will be updated in the
3775 * delete flow for this case
3777 if (ice_is_vc_addr_legacy(vc_ether_addr)) {
3778 ether_addr_copy(vf->legacy_last_added_umac.addr,
3780 vf->legacy_last_added_umac.time_modified = jiffies;
3785 * ice_vc_add_mac_addr - attempt to add the MAC address passed in
3786 * @vf: pointer to the VF info
3787 * @vsi: pointer to the VF's VSI
3788 * @vc_ether_addr: VIRTCHNL MAC address structure used to add MAC
3791 ice_vc_add_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
3792 struct virtchnl_ether_addr *vc_ether_addr)
3794 struct device *dev = ice_pf_to_dev(vf->pf);
3795 u8 *mac_addr = vc_ether_addr->addr;
3798 /* device MAC already added */
3799 if (ether_addr_equal(mac_addr, vf->dev_lan_addr.addr))
3802 if (is_unicast_ether_addr(mac_addr) && !ice_can_vf_change_mac(vf)) {
3803 dev_err(dev, "VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
3807 ret = ice_fltr_add_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
3808 if (ret == -EEXIST) {
3809 dev_dbg(dev, "MAC %pM already exists for VF %d\n", mac_addr,
3811 /* don't return since we might need to update
3812 * the primary MAC in ice_vfhw_mac_add() below
3815 dev_err(dev, "Failed to add MAC %pM for VF %d\n, error %d\n",
3816 mac_addr, vf->vf_id, ret);
3822 ice_vfhw_mac_add(vf, vc_ether_addr);
3828 * ice_is_legacy_umac_expired - check if last added legacy unicast MAC expired
3829 * @last_added_umac: structure used to check expiration
3831 static bool ice_is_legacy_umac_expired(struct ice_time_mac *last_added_umac)
3833 #define ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME msecs_to_jiffies(3000)
3834 return time_is_before_jiffies(last_added_umac->time_modified +
3835 ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME);
3839 * ice_update_legacy_cached_mac - update cached hardware MAC for legacy VF
3841 * @vc_ether_addr: structure from VIRTCHNL with MAC to check
3843 * only update cached hardware MAC for legacy VF drivers on delete
3844 * because we cannot guarantee order/type of MAC from the VF driver
3847 ice_update_legacy_cached_mac(struct ice_vf *vf,
3848 struct virtchnl_ether_addr *vc_ether_addr)
3850 if (!ice_is_vc_addr_legacy(vc_ether_addr) ||
3851 ice_is_legacy_umac_expired(&vf->legacy_last_added_umac))
3854 ether_addr_copy(vf->dev_lan_addr.addr, vf->legacy_last_added_umac.addr);
3855 ether_addr_copy(vf->hw_lan_addr.addr, vf->legacy_last_added_umac.addr);
3859 * ice_vfhw_mac_del - update the VF's cached hardware MAC if allowed
3861 * @vc_ether_addr: structure from VIRTCHNL with MAC to delete
3864 ice_vfhw_mac_del(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
3866 u8 *mac_addr = vc_ether_addr->addr;
3868 if (!is_valid_ether_addr(mac_addr) ||
3869 !ether_addr_equal(vf->dev_lan_addr.addr, mac_addr))
3872 /* allow the device MAC to be repopulated in the add flow and don't
3873 * clear the hardware MAC (i.e. hw_lan_addr.addr) here as that is meant
3874 * to be persistent on VM reboot and across driver unload/load, which
3875 * won't work if we clear the hardware MAC here
3877 eth_zero_addr(vf->dev_lan_addr.addr);
3879 ice_update_legacy_cached_mac(vf, vc_ether_addr);
3883 * ice_vc_del_mac_addr - attempt to delete the MAC address passed in
3884 * @vf: pointer to the VF info
3885 * @vsi: pointer to the VF's VSI
3886 * @vc_ether_addr: VIRTCHNL MAC address structure used to delete MAC
3889 ice_vc_del_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
3890 struct virtchnl_ether_addr *vc_ether_addr)
3892 struct device *dev = ice_pf_to_dev(vf->pf);
3893 u8 *mac_addr = vc_ether_addr->addr;
3896 if (!ice_can_vf_change_mac(vf) &&
3897 ether_addr_equal(vf->dev_lan_addr.addr, mac_addr))
3900 status = ice_fltr_remove_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
3901 if (status == -ENOENT) {
3902 dev_err(dev, "MAC %pM does not exist for VF %d\n", mac_addr,
3905 } else if (status) {
3906 dev_err(dev, "Failed to delete MAC %pM for VF %d, error %d\n",
3907 mac_addr, vf->vf_id, status);
3911 ice_vfhw_mac_del(vf, vc_ether_addr);
3919 * ice_vc_handle_mac_addr_msg
3920 * @vf: pointer to the VF info
3921 * @msg: pointer to the msg buffer
3922 * @set: true if MAC filters are being set, false otherwise
3924 * add guest MAC address filter
3927 ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
3929 int (*ice_vc_cfg_mac)
3930 (struct ice_vf *vf, struct ice_vsi *vsi,
3931 struct virtchnl_ether_addr *virtchnl_ether_addr);
3932 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3933 struct virtchnl_ether_addr_list *al =
3934 (struct virtchnl_ether_addr_list *)msg;
3935 struct ice_pf *pf = vf->pf;
3936 enum virtchnl_ops vc_op;
3937 struct ice_vsi *vsi;
3941 vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
3942 ice_vc_cfg_mac = ice_vc_add_mac_addr;
3944 vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
3945 ice_vc_cfg_mac = ice_vc_del_mac_addr;
3948 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
3949 !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
3950 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3951 goto handle_mac_exit;
3954 /* If this VF is not privileged, then we can't add more than a
3955 * limited number of addresses. Check to make sure that the
3956 * additions do not push us over the limit.
3958 if (set && !ice_is_vf_trusted(vf) &&
3959 (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
3960 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",
3962 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3963 goto handle_mac_exit;
3966 vsi = ice_get_vf_vsi(vf);
3968 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3969 goto handle_mac_exit;
3972 for (i = 0; i < al->num_elements; i++) {
3973 u8 *mac_addr = al->list[i].addr;
3976 if (is_broadcast_ether_addr(mac_addr) ||
3977 is_zero_ether_addr(mac_addr))
3980 result = ice_vc_cfg_mac(vf, vsi, &al->list[i]);
3981 if (result == -EEXIST || result == -ENOENT) {
3983 } else if (result) {
3984 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
3985 goto handle_mac_exit;
3990 /* send the response to the VF */
3991 return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
3995 * ice_vc_add_mac_addr_msg
3996 * @vf: pointer to the VF info
3997 * @msg: pointer to the msg buffer
3999 * add guest MAC address filter
4001 static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
4003 return ice_vc_handle_mac_addr_msg(vf, msg, true);
4007 * ice_vc_del_mac_addr_msg
4008 * @vf: pointer to the VF info
4009 * @msg: pointer to the msg buffer
4011 * remove guest MAC address filter
4013 static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
4015 return ice_vc_handle_mac_addr_msg(vf, msg, false);
4019 * ice_vc_request_qs_msg
4020 * @vf: pointer to the VF info
4021 * @msg: pointer to the msg buffer
4023 * VFs get a default number of queues but can use this message to request a
4024 * different number. If the request is successful, PF will reset the VF and
4025 * return 0. If unsuccessful, PF will send message informing VF of number of
4026 * available queue pairs via virtchnl message response to VF.
4028 static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
4030 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4031 struct virtchnl_vf_res_request *vfres =
4032 (struct virtchnl_vf_res_request *)msg;
4033 u16 req_queues = vfres->num_queue_pairs;
4034 struct ice_pf *pf = vf->pf;
4035 u16 max_allowed_vf_queues;
4036 u16 tx_rx_queue_left;
4040 dev = ice_pf_to_dev(pf);
4041 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4042 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4046 cur_queues = vf->num_vf_qs;
4047 tx_rx_queue_left = min_t(u16, ice_get_avail_txq_count(pf),
4048 ice_get_avail_rxq_count(pf));
4049 max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
4051 dev_err(dev, "VF %d tried to request 0 queues. Ignoring.\n",
4053 } else if (req_queues > ICE_MAX_RSS_QS_PER_VF) {
4054 dev_err(dev, "VF %d tried to request more than %d queues.\n",
4055 vf->vf_id, ICE_MAX_RSS_QS_PER_VF);
4056 vfres->num_queue_pairs = ICE_MAX_RSS_QS_PER_VF;
4057 } else if (req_queues > cur_queues &&
4058 req_queues - cur_queues > tx_rx_queue_left) {
4059 dev_warn(dev, "VF %d requested %u more queues, but only %u left.\n",
4060 vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
4061 vfres->num_queue_pairs = min_t(u16, max_allowed_vf_queues,
4062 ICE_MAX_RSS_QS_PER_VF);
4064 /* request is successful, then reset VF */
4065 vf->num_req_qs = req_queues;
4066 ice_vc_reset_vf(vf);
4067 dev_info(dev, "VF %d granted request of %u queues.\n",
4068 vf->vf_id, req_queues);
4073 /* send the response to the VF */
4074 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
4075 v_ret, (u8 *)vfres, sizeof(*vfres));
4079 * ice_set_vf_port_vlan
4080 * @netdev: network interface device structure
4081 * @vf_id: VF identifier
4082 * @vlan_id: VLAN ID being set
4083 * @qos: priority setting
4084 * @vlan_proto: VLAN protocol
4086 * program VF Port VLAN ID and/or QoS
4089 ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
4092 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4098 dev = ice_pf_to_dev(pf);
4099 if (ice_validate_vf_id(pf, vf_id))
4102 if (vlan_id >= VLAN_N_VID || qos > 7) {
4103 dev_err(dev, "Invalid Port VLAN parameters for VF %d, ID %d, QoS %d\n",
4104 vf_id, vlan_id, qos);
4108 if (vlan_proto != htons(ETH_P_8021Q)) {
4109 dev_err(dev, "VF VLAN protocol is not supported\n");
4110 return -EPROTONOSUPPORT;
4113 vf = &pf->vf[vf_id];
4114 ret = ice_check_vf_ready_for_cfg(vf);
4118 vlanprio = vlan_id | (qos << VLAN_PRIO_SHIFT);
4120 if (vf->port_vlan_info == vlanprio) {
4121 /* duplicate request, so just return success */
4122 dev_dbg(dev, "Duplicate pvid %d request\n", vlanprio);
4126 mutex_lock(&vf->cfg_lock);
4128 vf->port_vlan_info = vlanprio;
4130 if (vf->port_vlan_info)
4131 dev_info(dev, "Setting VLAN %d, QoS 0x%x on VF %d\n",
4132 vlan_id, qos, vf_id);
4134 dev_info(dev, "Clearing port VLAN on VF %d\n", vf_id);
4136 ice_vc_reset_vf(vf);
4137 mutex_unlock(&vf->cfg_lock);
4143 * ice_vf_vlan_offload_ena - determine if capabilities support VLAN offloads
4144 * @caps: VF driver negotiated capabilities
4146 * Return true if VIRTCHNL_VF_OFFLOAD_VLAN capability is set, else return false
4148 static bool ice_vf_vlan_offload_ena(u32 caps)
4150 return !!(caps & VIRTCHNL_VF_OFFLOAD_VLAN);
4154 * ice_vc_process_vlan_msg
4155 * @vf: pointer to the VF info
4156 * @msg: pointer to the msg buffer
4157 * @add_v: Add VLAN if true, otherwise delete VLAN
4159 * Process virtchnl op to add or remove programmed guest VLAN ID
4161 static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
4163 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4164 struct virtchnl_vlan_filter_list *vfl =
4165 (struct virtchnl_vlan_filter_list *)msg;
4166 struct ice_pf *pf = vf->pf;
4167 bool vlan_promisc = false;
4168 struct ice_vsi *vsi;
4175 dev = ice_pf_to_dev(pf);
4176 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4177 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4181 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
4182 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4186 if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
4187 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4191 for (i = 0; i < vfl->num_elements; i++) {
4192 if (vfl->vlan_id[i] >= VLAN_N_VID) {
4193 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4194 dev_err(dev, "invalid VF VLAN id %d\n",
4201 vsi = ice_get_vf_vsi(vf);
4203 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4207 if (add_v && !ice_is_vf_trusted(vf) &&
4208 vsi->num_vlan >= ICE_MAX_VLAN_PER_VF) {
4209 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
4211 /* There is no need to let VF know about being not trusted,
4212 * so we can just return success message here
4217 if (vsi->info.pvid) {
4218 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4222 if ((test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
4223 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) &&
4224 test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags))
4225 vlan_promisc = true;
4228 for (i = 0; i < vfl->num_elements; i++) {
4229 u16 vid = vfl->vlan_id[i];
4231 if (!ice_is_vf_trusted(vf) &&
4232 vsi->num_vlan >= ICE_MAX_VLAN_PER_VF) {
4233 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
4235 /* There is no need to let VF know about being
4236 * not trusted, so we can just return success
4237 * message here as well.
4242 /* we add VLAN 0 by default for each VF so we can enable
4243 * Tx VLAN anti-spoof without triggering MDD events so
4244 * we don't need to add it again here
4249 status = ice_vsi_add_vlan(vsi, vid, ICE_FWD_TO_VSI);
4251 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4255 /* Enable VLAN pruning when non-zero VLAN is added */
4256 if (!vlan_promisc && vid &&
4257 !ice_vsi_is_vlan_pruning_ena(vsi)) {
4258 status = ice_cfg_vlan_pruning(vsi, true);
4260 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4261 dev_err(dev, "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
4265 } else if (vlan_promisc) {
4266 /* Enable Ucast/Mcast VLAN promiscuous mode */
4267 promisc_m = ICE_PROMISC_VLAN_TX |
4268 ICE_PROMISC_VLAN_RX;
4270 status = ice_set_vsi_promisc(hw, vsi->idx,
4273 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4274 dev_err(dev, "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
4280 /* In case of non_trusted VF, number of VLAN elements passed
4281 * to PF for removal might be greater than number of VLANs
4282 * filter programmed for that VF - So, use actual number of
4283 * VLANS added earlier with add VLAN opcode. In order to avoid
4284 * removing VLAN that doesn't exist, which result to sending
4285 * erroneous failed message back to the VF
4289 num_vf_vlan = vsi->num_vlan;
4290 for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
4291 u16 vid = vfl->vlan_id[i];
4293 /* we add VLAN 0 by default for each VF so we can enable
4294 * Tx VLAN anti-spoof without triggering MDD events so
4295 * we don't want a VIRTCHNL request to remove it
4300 /* Make sure ice_vsi_kill_vlan is successful before
4301 * updating VLAN information
4303 status = ice_vsi_kill_vlan(vsi, vid);
4305 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4309 /* Disable VLAN pruning when only VLAN 0 is left */
4310 if (vsi->num_vlan == 1 &&
4311 ice_vsi_is_vlan_pruning_ena(vsi))
4312 ice_cfg_vlan_pruning(vsi, false);
4314 /* Disable Unicast/Multicast VLAN promiscuous mode */
4316 promisc_m = ICE_PROMISC_VLAN_TX |
4317 ICE_PROMISC_VLAN_RX;
4319 ice_clear_vsi_promisc(hw, vsi->idx,
4326 /* send the response to the VF */
4328 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
4331 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
4336 * ice_vc_add_vlan_msg
4337 * @vf: pointer to the VF info
4338 * @msg: pointer to the msg buffer
4340 * Add and program guest VLAN ID
4342 static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
4344 return ice_vc_process_vlan_msg(vf, msg, true);
4348 * ice_vc_remove_vlan_msg
4349 * @vf: pointer to the VF info
4350 * @msg: pointer to the msg buffer
4352 * remove programmed guest VLAN ID
4354 static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
4356 return ice_vc_process_vlan_msg(vf, msg, false);
4360 * ice_vc_ena_vlan_stripping
4361 * @vf: pointer to the VF info
4363 * Enable VLAN header stripping for a given VF
4365 static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
4367 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4368 struct ice_vsi *vsi;
4370 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4371 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4375 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
4376 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4380 vsi = ice_get_vf_vsi(vf);
4381 if (ice_vsi_manage_vlan_stripping(vsi, true))
4382 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4385 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
4390 * ice_vc_dis_vlan_stripping
4391 * @vf: pointer to the VF info
4393 * Disable VLAN header stripping for a given VF
4395 static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
4397 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4398 struct ice_vsi *vsi;
4400 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4401 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4405 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
4406 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4410 vsi = ice_get_vf_vsi(vf);
4412 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4416 if (ice_vsi_manage_vlan_stripping(vsi, false))
4417 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4420 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
4425 * ice_vf_init_vlan_stripping - enable/disable VLAN stripping on initialization
4426 * @vf: VF to enable/disable VLAN stripping for on initialization
4428 * If the VIRTCHNL_VF_OFFLOAD_VLAN flag is set enable VLAN stripping, else if
4429 * the flag is cleared then we want to disable stripping. For example, the flag
4430 * will be cleared when port VLANs are configured by the administrator before
4431 * passing the VF to the guest or if the AVF driver doesn't support VLAN
4434 static int ice_vf_init_vlan_stripping(struct ice_vf *vf)
4436 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
4441 /* don't modify stripping if port VLAN is configured */
4445 if (ice_vf_vlan_offload_ena(vf->driver_caps))
4446 return ice_vsi_manage_vlan_stripping(vsi, true);
4448 return ice_vsi_manage_vlan_stripping(vsi, false);
4451 static struct ice_vc_vf_ops ice_vc_vf_dflt_ops = {
4452 .get_ver_msg = ice_vc_get_ver_msg,
4453 .get_vf_res_msg = ice_vc_get_vf_res_msg,
4454 .reset_vf = ice_vc_reset_vf_msg,
4455 .add_mac_addr_msg = ice_vc_add_mac_addr_msg,
4456 .del_mac_addr_msg = ice_vc_del_mac_addr_msg,
4457 .cfg_qs_msg = ice_vc_cfg_qs_msg,
4458 .ena_qs_msg = ice_vc_ena_qs_msg,
4459 .dis_qs_msg = ice_vc_dis_qs_msg,
4460 .request_qs_msg = ice_vc_request_qs_msg,
4461 .cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
4462 .config_rss_key = ice_vc_config_rss_key,
4463 .config_rss_lut = ice_vc_config_rss_lut,
4464 .get_stats_msg = ice_vc_get_stats_msg,
4465 .cfg_promiscuous_mode_msg = ice_vc_cfg_promiscuous_mode_msg,
4466 .add_vlan_msg = ice_vc_add_vlan_msg,
4467 .remove_vlan_msg = ice_vc_remove_vlan_msg,
4468 .ena_vlan_stripping = ice_vc_ena_vlan_stripping,
4469 .dis_vlan_stripping = ice_vc_dis_vlan_stripping,
4470 .handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
4471 .add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
4472 .del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
4475 void ice_vc_set_dflt_vf_ops(struct ice_vc_vf_ops *ops)
4477 *ops = ice_vc_vf_dflt_ops;
4481 * ice_vc_repr_add_mac
4482 * @vf: pointer to VF
4483 * @msg: virtchannel message
4485 * When port representors are created, we do not add MAC rule
4486 * to firmware, we store it so that PF could report same
4489 static int ice_vc_repr_add_mac(struct ice_vf *vf, u8 *msg)
4491 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4492 struct virtchnl_ether_addr_list *al =
4493 (struct virtchnl_ether_addr_list *)msg;
4494 struct ice_vsi *vsi;
4498 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
4499 !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
4500 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4501 goto handle_mac_exit;
4506 vsi = ice_get_vf_vsi(vf);
4508 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4509 goto handle_mac_exit;
4512 for (i = 0; i < al->num_elements; i++) {
4513 u8 *mac_addr = al->list[i].addr;
4516 if (!is_unicast_ether_addr(mac_addr) ||
4517 ether_addr_equal(mac_addr, vf->hw_lan_addr.addr))
4520 if (vf->pf_set_mac) {
4521 dev_err(ice_pf_to_dev(pf), "VF attempting to override administratively set MAC address\n");
4522 v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
4523 goto handle_mac_exit;
4526 result = ice_eswitch_add_vf_mac_rule(pf, vf, mac_addr);
4528 dev_err(ice_pf_to_dev(pf), "Failed to add MAC %pM for VF %d\n, error %d\n",
4529 mac_addr, vf->vf_id, result);
4530 goto handle_mac_exit;
4533 ice_vfhw_mac_add(vf, &al->list[i]);
4539 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
4544 * ice_vc_repr_del_mac - response with success for deleting MAC
4545 * @vf: pointer to VF
4546 * @msg: virtchannel message
4548 * Respond with success to not break normal VF flow.
4549 * For legacy VF driver try to update cached MAC address.
4552 ice_vc_repr_del_mac(struct ice_vf __always_unused *vf, u8 __always_unused *msg)
4554 struct virtchnl_ether_addr_list *al =
4555 (struct virtchnl_ether_addr_list *)msg;
4557 ice_update_legacy_cached_mac(vf, &al->list[0]);
4559 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
4560 VIRTCHNL_STATUS_SUCCESS, NULL, 0);
4563 static int ice_vc_repr_add_vlan(struct ice_vf *vf, u8 __always_unused *msg)
4565 dev_dbg(ice_pf_to_dev(vf->pf),
4566 "Can't add VLAN in switchdev mode for VF %d\n", vf->vf_id);
4567 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN,
4568 VIRTCHNL_STATUS_SUCCESS, NULL, 0);
4571 static int ice_vc_repr_del_vlan(struct ice_vf *vf, u8 __always_unused *msg)
4573 dev_dbg(ice_pf_to_dev(vf->pf),
4574 "Can't delete VLAN in switchdev mode for VF %d\n", vf->vf_id);
4575 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN,
4576 VIRTCHNL_STATUS_SUCCESS, NULL, 0);
4579 static int ice_vc_repr_ena_vlan_stripping(struct ice_vf *vf)
4581 dev_dbg(ice_pf_to_dev(vf->pf),
4582 "Can't enable VLAN stripping in switchdev mode for VF %d\n",
4584 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
4585 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4589 static int ice_vc_repr_dis_vlan_stripping(struct ice_vf *vf)
4591 dev_dbg(ice_pf_to_dev(vf->pf),
4592 "Can't disable VLAN stripping in switchdev mode for VF %d\n",
4594 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
4595 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4600 ice_vc_repr_cfg_promiscuous_mode(struct ice_vf *vf, u8 __always_unused *msg)
4602 dev_dbg(ice_pf_to_dev(vf->pf),
4603 "Can't config promiscuous mode in switchdev mode for VF %d\n",
4605 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
4606 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4610 void ice_vc_change_ops_to_repr(struct ice_vc_vf_ops *ops)
4612 ops->add_mac_addr_msg = ice_vc_repr_add_mac;
4613 ops->del_mac_addr_msg = ice_vc_repr_del_mac;
4614 ops->add_vlan_msg = ice_vc_repr_add_vlan;
4615 ops->remove_vlan_msg = ice_vc_repr_del_vlan;
4616 ops->ena_vlan_stripping = ice_vc_repr_ena_vlan_stripping;
4617 ops->dis_vlan_stripping = ice_vc_repr_dis_vlan_stripping;
4618 ops->cfg_promiscuous_mode_msg = ice_vc_repr_cfg_promiscuous_mode;
4622 * ice_vc_process_vf_msg - Process request from VF
4623 * @pf: pointer to the PF structure
4624 * @event: pointer to the AQ event
4626 * called from the common asq/arq handler to
4627 * process request from VF
4629 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
4631 u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
4632 s16 vf_id = le16_to_cpu(event->desc.retval);
4633 u16 msglen = event->msg_len;
4634 struct ice_vc_vf_ops *ops;
4635 u8 *msg = event->msg_buf;
4636 struct ice_vf *vf = NULL;
4640 dev = ice_pf_to_dev(pf);
4641 if (ice_validate_vf_id(pf, vf_id)) {
4646 vf = &pf->vf[vf_id];
4648 /* Check if VF is disabled. */
4649 if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
4656 /* Perform basic checks on the msg */
4657 err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
4659 if (err == VIRTCHNL_STATUS_ERR_PARAM)
4665 if (!ice_vc_is_opcode_allowed(vf, v_opcode)) {
4666 ice_vc_send_msg_to_vf(vf, v_opcode,
4667 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED, NULL,
4674 ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
4676 dev_err(dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
4677 vf_id, v_opcode, msglen, err);
4681 /* VF is being configured in another context that triggers a VFR, so no
4682 * need to process this message
4684 if (!mutex_trylock(&vf->cfg_lock)) {
4685 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",
4691 case VIRTCHNL_OP_VERSION:
4692 err = ops->get_ver_msg(vf, msg);
4694 case VIRTCHNL_OP_GET_VF_RESOURCES:
4695 err = ops->get_vf_res_msg(vf, msg);
4696 if (ice_vf_init_vlan_stripping(vf))
4697 dev_err(dev, "Failed to initialize VLAN stripping for VF %d\n",
4699 ice_vc_notify_vf_link_state(vf);
4701 case VIRTCHNL_OP_RESET_VF:
4704 case VIRTCHNL_OP_ADD_ETH_ADDR:
4705 err = ops->add_mac_addr_msg(vf, msg);
4707 case VIRTCHNL_OP_DEL_ETH_ADDR:
4708 err = ops->del_mac_addr_msg(vf, msg);
4710 case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
4711 err = ops->cfg_qs_msg(vf, msg);
4713 case VIRTCHNL_OP_ENABLE_QUEUES:
4714 err = ops->ena_qs_msg(vf, msg);
4715 ice_vc_notify_vf_link_state(vf);
4717 case VIRTCHNL_OP_DISABLE_QUEUES:
4718 err = ops->dis_qs_msg(vf, msg);
4720 case VIRTCHNL_OP_REQUEST_QUEUES:
4721 err = ops->request_qs_msg(vf, msg);
4723 case VIRTCHNL_OP_CONFIG_IRQ_MAP:
4724 err = ops->cfg_irq_map_msg(vf, msg);
4726 case VIRTCHNL_OP_CONFIG_RSS_KEY:
4727 err = ops->config_rss_key(vf, msg);
4729 case VIRTCHNL_OP_CONFIG_RSS_LUT:
4730 err = ops->config_rss_lut(vf, msg);
4732 case VIRTCHNL_OP_GET_STATS:
4733 err = ops->get_stats_msg(vf, msg);
4735 case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
4736 err = ops->cfg_promiscuous_mode_msg(vf, msg);
4738 case VIRTCHNL_OP_ADD_VLAN:
4739 err = ops->add_vlan_msg(vf, msg);
4741 case VIRTCHNL_OP_DEL_VLAN:
4742 err = ops->remove_vlan_msg(vf, msg);
4744 case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
4745 err = ops->ena_vlan_stripping(vf);
4747 case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
4748 err = ops->dis_vlan_stripping(vf);
4750 case VIRTCHNL_OP_ADD_FDIR_FILTER:
4751 err = ops->add_fdir_fltr_msg(vf, msg);
4753 case VIRTCHNL_OP_DEL_FDIR_FILTER:
4754 err = ops->del_fdir_fltr_msg(vf, msg);
4756 case VIRTCHNL_OP_ADD_RSS_CFG:
4757 err = ops->handle_rss_cfg_msg(vf, msg, true);
4759 case VIRTCHNL_OP_DEL_RSS_CFG:
4760 err = ops->handle_rss_cfg_msg(vf, msg, false);
4762 case VIRTCHNL_OP_UNKNOWN:
4764 dev_err(dev, "Unsupported opcode %d from VF %d\n", v_opcode,
4766 err = ice_vc_send_msg_to_vf(vf, v_opcode,
4767 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4772 /* Helper function cares less about error return values here
4773 * as it is busy with pending work.
4775 dev_info(dev, "PF failed to honor VF %d, opcode %d, error %d\n",
4776 vf_id, v_opcode, err);
4779 mutex_unlock(&vf->cfg_lock);
4784 * @netdev: network interface device structure
4785 * @vf_id: VF identifier
4786 * @ivi: VF configuration structure
4788 * return VF configuration
4791 ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
4793 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4796 if (ice_validate_vf_id(pf, vf_id))
4799 vf = &pf->vf[vf_id];
4801 if (ice_check_vf_init(pf, vf))
4805 ether_addr_copy(ivi->mac, vf->hw_lan_addr.addr);
4807 /* VF configuration for VLAN and applicable QoS */
4808 ivi->vlan = vf->port_vlan_info & VLAN_VID_MASK;
4809 ivi->qos = (vf->port_vlan_info & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
4811 ivi->trusted = vf->trusted;
4812 ivi->spoofchk = vf->spoofchk;
4813 if (!vf->link_forced)
4814 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
4815 else if (vf->link_up)
4816 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
4818 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
4819 ivi->max_tx_rate = vf->max_tx_rate;
4820 ivi->min_tx_rate = vf->min_tx_rate;
4825 * ice_unicast_mac_exists - check if the unicast MAC exists on the PF's switch
4826 * @pf: PF used to reference the switch's rules
4827 * @umac: unicast MAC to compare against existing switch rules
4829 * Return true on the first/any match, else return false
4831 static bool ice_unicast_mac_exists(struct ice_pf *pf, u8 *umac)
4833 struct ice_sw_recipe *mac_recipe_list =
4834 &pf->hw.switch_info->recp_list[ICE_SW_LKUP_MAC];
4835 struct ice_fltr_mgmt_list_entry *list_itr;
4836 struct list_head *rule_head;
4837 struct mutex *rule_lock; /* protect MAC filter list access */
4839 rule_head = &mac_recipe_list->filt_rules;
4840 rule_lock = &mac_recipe_list->filt_rule_lock;
4842 mutex_lock(rule_lock);
4843 list_for_each_entry(list_itr, rule_head, list_entry) {
4844 u8 *existing_mac = &list_itr->fltr_info.l_data.mac.mac_addr[0];
4846 if (ether_addr_equal(existing_mac, umac)) {
4847 mutex_unlock(rule_lock);
4852 mutex_unlock(rule_lock);
4859 * @netdev: network interface device structure
4860 * @vf_id: VF identifier
4863 * program VF MAC address
4865 int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
4867 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4871 if (ice_validate_vf_id(pf, vf_id))
4874 if (is_multicast_ether_addr(mac)) {
4875 netdev_err(netdev, "%pM not a valid unicast address\n", mac);
4879 vf = &pf->vf[vf_id];
4880 /* nothing left to do, unicast MAC already set */
4881 if (ether_addr_equal(vf->dev_lan_addr.addr, mac) &&
4882 ether_addr_equal(vf->hw_lan_addr.addr, mac))
4885 ret = ice_check_vf_ready_for_cfg(vf);
4889 if (ice_unicast_mac_exists(pf, mac)) {
4890 netdev_err(netdev, "Unicast MAC %pM already exists on this PF. Preventing setting VF %u unicast MAC address to %pM\n",
4895 mutex_lock(&vf->cfg_lock);
4897 /* VF is notified of its new MAC via the PF's response to the
4898 * VIRTCHNL_OP_GET_VF_RESOURCES message after the VF has been reset
4900 ether_addr_copy(vf->dev_lan_addr.addr, mac);
4901 ether_addr_copy(vf->hw_lan_addr.addr, mac);
4902 if (is_zero_ether_addr(mac)) {
4903 /* VF will send VIRTCHNL_OP_ADD_ETH_ADDR message with its MAC */
4904 vf->pf_set_mac = false;
4905 netdev_info(netdev, "Removing MAC on VF %d. VF driver will be reinitialized\n",
4908 /* PF will add MAC rule for the VF */
4909 vf->pf_set_mac = true;
4910 netdev_info(netdev, "Setting MAC %pM on VF %d. VF driver will be reinitialized\n",
4914 ice_vc_reset_vf(vf);
4915 mutex_unlock(&vf->cfg_lock);
4921 * @netdev: network interface device structure
4922 * @vf_id: VF identifier
4923 * @trusted: Boolean value to enable/disable trusted VF
4925 * Enable or disable a given VF as trusted
4927 int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
4929 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4933 if (ice_is_eswitch_mode_switchdev(pf)) {
4934 dev_info(ice_pf_to_dev(pf), "Trusted VF is forbidden in switchdev mode\n");
4938 if (ice_validate_vf_id(pf, vf_id))
4941 vf = &pf->vf[vf_id];
4942 ret = ice_check_vf_ready_for_cfg(vf);
4946 /* Check if already trusted */
4947 if (trusted == vf->trusted)
4950 mutex_lock(&vf->cfg_lock);
4952 vf->trusted = trusted;
4953 ice_vc_reset_vf(vf);
4954 dev_info(ice_pf_to_dev(pf), "VF %u is now %strusted\n",
4955 vf_id, trusted ? "" : "un");
4957 mutex_unlock(&vf->cfg_lock);
4963 * ice_set_vf_link_state
4964 * @netdev: network interface device structure
4965 * @vf_id: VF identifier
4966 * @link_state: required link state
4968 * Set VF's link state, irrespective of physical link state status
4970 int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
4972 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4976 if (ice_validate_vf_id(pf, vf_id))
4979 vf = &pf->vf[vf_id];
4980 ret = ice_check_vf_ready_for_cfg(vf);
4984 switch (link_state) {
4985 case IFLA_VF_LINK_STATE_AUTO:
4986 vf->link_forced = false;
4988 case IFLA_VF_LINK_STATE_ENABLE:
4989 vf->link_forced = true;
4992 case IFLA_VF_LINK_STATE_DISABLE:
4993 vf->link_forced = true;
4994 vf->link_up = false;
5000 ice_vc_notify_vf_link_state(vf);
5006 * ice_calc_all_vfs_min_tx_rate - calculate cumulative min Tx rate on all VFs
5007 * @pf: PF associated with VFs
5009 static int ice_calc_all_vfs_min_tx_rate(struct ice_pf *pf)
5013 ice_for_each_vf(pf, i)
5014 rate += pf->vf[i].min_tx_rate;
5020 * ice_min_tx_rate_oversubscribed - check if min Tx rate causes oversubscription
5021 * @vf: VF trying to configure min_tx_rate
5022 * @min_tx_rate: min Tx rate in Mbps
5024 * Check if the min_tx_rate being passed in will cause oversubscription of total
5025 * min_tx_rate based on the current link speed and all other VFs configured
5028 * Return true if the passed min_tx_rate would cause oversubscription, else
5032 ice_min_tx_rate_oversubscribed(struct ice_vf *vf, int min_tx_rate)
5034 int link_speed_mbps = ice_get_link_speed_mbps(ice_get_vf_vsi(vf));
5035 int all_vfs_min_tx_rate = ice_calc_all_vfs_min_tx_rate(vf->pf);
5037 /* this VF's previous rate is being overwritten */
5038 all_vfs_min_tx_rate -= vf->min_tx_rate;
5040 if (all_vfs_min_tx_rate + min_tx_rate > link_speed_mbps) {
5041 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",
5042 min_tx_rate, vf->vf_id,
5043 all_vfs_min_tx_rate + min_tx_rate - link_speed_mbps,
5052 * ice_set_vf_bw - set min/max VF bandwidth
5053 * @netdev: network interface device structure
5054 * @vf_id: VF identifier
5055 * @min_tx_rate: Minimum Tx rate in Mbps
5056 * @max_tx_rate: Maximum Tx rate in Mbps
5059 ice_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
5062 struct ice_pf *pf = ice_netdev_to_pf(netdev);
5063 struct ice_vsi *vsi;
5068 dev = ice_pf_to_dev(pf);
5069 if (ice_validate_vf_id(pf, vf_id))
5072 vf = &pf->vf[vf_id];
5073 ret = ice_check_vf_ready_for_cfg(vf);
5077 vsi = ice_get_vf_vsi(vf);
5079 /* when max_tx_rate is zero that means no max Tx rate limiting, so only
5080 * check if max_tx_rate is non-zero
5082 if (max_tx_rate && min_tx_rate > max_tx_rate) {
5083 dev_err(dev, "Cannot set min Tx rate %d Mbps greater than max Tx rate %d Mbps\n",
5084 min_tx_rate, max_tx_rate);
5088 if (min_tx_rate && ice_is_dcb_active(pf)) {
5089 dev_err(dev, "DCB on PF is currently enabled. VF min Tx rate limiting not allowed on this PF.\n");
5093 if (ice_min_tx_rate_oversubscribed(vf, min_tx_rate))
5096 if (vf->min_tx_rate != (unsigned int)min_tx_rate) {
5097 ret = ice_set_min_bw_limit(vsi, (u64)min_tx_rate * 1000);
5099 dev_err(dev, "Unable to set min-tx-rate for VF %d\n",
5104 vf->min_tx_rate = min_tx_rate;
5107 if (vf->max_tx_rate != (unsigned int)max_tx_rate) {
5108 ret = ice_set_max_bw_limit(vsi, (u64)max_tx_rate * 1000);
5110 dev_err(dev, "Unable to set max-tx-rate for VF %d\n",
5115 vf->max_tx_rate = max_tx_rate;
5122 * ice_get_vf_stats - populate some stats for the VF
5123 * @netdev: the netdev of the PF
5124 * @vf_id: the host OS identifier (0-255)
5125 * @vf_stats: pointer to the OS memory to be initialized
5127 int ice_get_vf_stats(struct net_device *netdev, int vf_id,
5128 struct ifla_vf_stats *vf_stats)
5130 struct ice_pf *pf = ice_netdev_to_pf(netdev);
5131 struct ice_eth_stats *stats;
5132 struct ice_vsi *vsi;
5136 if (ice_validate_vf_id(pf, vf_id))
5139 vf = &pf->vf[vf_id];
5140 ret = ice_check_vf_ready_for_cfg(vf);
5144 vsi = ice_get_vf_vsi(vf);
5148 ice_update_eth_stats(vsi);
5149 stats = &vsi->eth_stats;
5151 memset(vf_stats, 0, sizeof(*vf_stats));
5153 vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
5154 stats->rx_multicast;
5155 vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
5156 stats->tx_multicast;
5157 vf_stats->rx_bytes = stats->rx_bytes;
5158 vf_stats->tx_bytes = stats->tx_bytes;
5159 vf_stats->broadcast = stats->rx_broadcast;
5160 vf_stats->multicast = stats->rx_multicast;
5161 vf_stats->rx_dropped = stats->rx_discards;
5162 vf_stats->tx_dropped = stats->tx_discards;
5168 * ice_print_vf_rx_mdd_event - print VF Rx malicious driver detect event
5169 * @vf: pointer to the VF structure
5171 void ice_print_vf_rx_mdd_event(struct ice_vf *vf)
5173 struct ice_pf *pf = vf->pf;
5176 dev = ice_pf_to_dev(pf);
5178 dev_info(dev, "%d Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pM. mdd-auto-reset-vfs=%s\n",
5179 vf->mdd_rx_events.count, pf->hw.pf_id, vf->vf_id,
5180 vf->dev_lan_addr.addr,
5181 test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)
5186 * ice_print_vfs_mdd_events - print VFs malicious driver detect event
5187 * @pf: pointer to the PF structure
5189 * Called from ice_handle_mdd_event to rate limit and print VFs MDD events.
5191 void ice_print_vfs_mdd_events(struct ice_pf *pf)
5193 struct device *dev = ice_pf_to_dev(pf);
5194 struct ice_hw *hw = &pf->hw;
5197 /* check that there are pending MDD events to print */
5198 if (!test_and_clear_bit(ICE_MDD_VF_PRINT_PENDING, pf->state))
5201 /* VF MDD event logs are rate limited to one second intervals */
5202 if (time_is_after_jiffies(pf->last_printed_mdd_jiffies + HZ * 1))
5205 pf->last_printed_mdd_jiffies = jiffies;
5207 ice_for_each_vf(pf, i) {
5208 struct ice_vf *vf = &pf->vf[i];
5210 /* only print Rx MDD event message if there are new events */
5211 if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
5212 vf->mdd_rx_events.last_printed =
5213 vf->mdd_rx_events.count;
5214 ice_print_vf_rx_mdd_event(vf);
5217 /* only print Tx MDD event message if there are new events */
5218 if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
5219 vf->mdd_tx_events.last_printed =
5220 vf->mdd_tx_events.count;
5222 dev_info(dev, "%d Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pM.\n",
5223 vf->mdd_tx_events.count, hw->pf_id, i,
5224 vf->dev_lan_addr.addr);
5230 * ice_restore_all_vfs_msi_state - restore VF MSI state after PF FLR
5231 * @pdev: pointer to a pci_dev structure
5233 * Called when recovering from a PF FLR to restore interrupt capability to
5236 void ice_restore_all_vfs_msi_state(struct pci_dev *pdev)
5241 if (!pci_num_vf(pdev))
5244 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
5246 struct pci_dev *vfdev;
5248 pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID,
5250 vfdev = pci_get_device(pdev->vendor, vf_id, NULL);
5252 if (vfdev->is_virtfn && vfdev->physfn == pdev)
5253 pci_restore_msi_state(vfdev);
5254 vfdev = pci_get_device(pdev->vendor, vf_id,
5261 * ice_is_malicious_vf - helper function to detect a malicious VF
5262 * @pf: ptr to struct ice_pf
5263 * @event: pointer to the AQ event
5264 * @num_msg_proc: the number of messages processed so far
5265 * @num_msg_pending: the number of messages peinding in admin queue
5268 ice_is_malicious_vf(struct ice_pf *pf, struct ice_rq_event_info *event,
5269 u16 num_msg_proc, u16 num_msg_pending)
5271 s16 vf_id = le16_to_cpu(event->desc.retval);
5272 struct device *dev = ice_pf_to_dev(pf);
5273 struct ice_mbx_data mbxdata;
5278 if (ice_validate_vf_id(pf, vf_id))
5281 vf = &pf->vf[vf_id];
5282 /* Check if VF is disabled. */
5283 if (test_bit(ICE_VF_STATE_DIS, vf->vf_states))
5286 mbxdata.num_msg_proc = num_msg_proc;
5287 mbxdata.num_pending_arq = num_msg_pending;
5288 mbxdata.max_num_msgs_mbx = pf->hw.mailboxq.num_rq_entries;
5289 #define ICE_MBX_OVERFLOW_WATERMARK 64
5290 mbxdata.async_watermark_val = ICE_MBX_OVERFLOW_WATERMARK;
5292 /* check to see if we have a malicious VF */
5293 status = ice_mbx_vf_state_handler(&pf->hw, &mbxdata, vf_id, &malvf);
5298 bool report_vf = false;
5300 /* if the VF is malicious and we haven't let the user
5301 * know about it, then let them know now
5303 status = ice_mbx_report_malvf(&pf->hw, pf->malvfs,
5304 ICE_MAX_VF_COUNT, vf_id,
5307 dev_dbg(dev, "Error reporting malicious VF\n");
5310 struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
5313 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",
5314 &vf->dev_lan_addr.addr[0],
5315 pf_vsi->netdev->dev_addr);
5321 /* if there was an error in detection or the VF is not malicious then