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"
13 #define FIELD_SELECTOR(proto_hdr_field) \
14 BIT((proto_hdr_field) & PROTO_HDR_FIELD_MASK)
16 struct ice_vc_hdr_match_type {
17 u32 vc_hdr; /* virtchnl headers (VIRTCHNL_PROTO_HDR_XXX) */
18 u32 ice_hdr; /* ice headers (ICE_FLOW_SEG_HDR_XXX) */
21 static const struct ice_vc_hdr_match_type ice_vc_hdr_list_os[] = {
22 {VIRTCHNL_PROTO_HDR_NONE, ICE_FLOW_SEG_HDR_NONE},
23 {VIRTCHNL_PROTO_HDR_IPV4, ICE_FLOW_SEG_HDR_IPV4 |
24 ICE_FLOW_SEG_HDR_IPV_OTHER},
25 {VIRTCHNL_PROTO_HDR_IPV6, ICE_FLOW_SEG_HDR_IPV6 |
26 ICE_FLOW_SEG_HDR_IPV_OTHER},
27 {VIRTCHNL_PROTO_HDR_TCP, ICE_FLOW_SEG_HDR_TCP},
28 {VIRTCHNL_PROTO_HDR_UDP, ICE_FLOW_SEG_HDR_UDP},
29 {VIRTCHNL_PROTO_HDR_SCTP, ICE_FLOW_SEG_HDR_SCTP},
32 static const struct ice_vc_hdr_match_type ice_vc_hdr_list_comms[] = {
33 {VIRTCHNL_PROTO_HDR_NONE, ICE_FLOW_SEG_HDR_NONE},
34 {VIRTCHNL_PROTO_HDR_ETH, ICE_FLOW_SEG_HDR_ETH},
35 {VIRTCHNL_PROTO_HDR_S_VLAN, ICE_FLOW_SEG_HDR_VLAN},
36 {VIRTCHNL_PROTO_HDR_C_VLAN, ICE_FLOW_SEG_HDR_VLAN},
37 {VIRTCHNL_PROTO_HDR_IPV4, ICE_FLOW_SEG_HDR_IPV4 |
38 ICE_FLOW_SEG_HDR_IPV_OTHER},
39 {VIRTCHNL_PROTO_HDR_IPV6, ICE_FLOW_SEG_HDR_IPV6 |
40 ICE_FLOW_SEG_HDR_IPV_OTHER},
41 {VIRTCHNL_PROTO_HDR_TCP, ICE_FLOW_SEG_HDR_TCP},
42 {VIRTCHNL_PROTO_HDR_UDP, ICE_FLOW_SEG_HDR_UDP},
43 {VIRTCHNL_PROTO_HDR_SCTP, ICE_FLOW_SEG_HDR_SCTP},
44 {VIRTCHNL_PROTO_HDR_PPPOE, ICE_FLOW_SEG_HDR_PPPOE},
45 {VIRTCHNL_PROTO_HDR_GTPU_IP, ICE_FLOW_SEG_HDR_GTPU_IP},
46 {VIRTCHNL_PROTO_HDR_GTPU_EH, ICE_FLOW_SEG_HDR_GTPU_EH},
47 {VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_DWN,
48 ICE_FLOW_SEG_HDR_GTPU_DWN},
49 {VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_UP,
50 ICE_FLOW_SEG_HDR_GTPU_UP},
51 {VIRTCHNL_PROTO_HDR_L2TPV3, ICE_FLOW_SEG_HDR_L2TPV3},
52 {VIRTCHNL_PROTO_HDR_ESP, ICE_FLOW_SEG_HDR_ESP},
53 {VIRTCHNL_PROTO_HDR_AH, ICE_FLOW_SEG_HDR_AH},
54 {VIRTCHNL_PROTO_HDR_PFCP, ICE_FLOW_SEG_HDR_PFCP_SESSION},
57 struct ice_vc_hash_field_match_type {
58 u32 vc_hdr; /* virtchnl headers
59 * (VIRTCHNL_PROTO_HDR_XXX)
61 u32 vc_hash_field; /* virtchnl hash fields selector
62 * FIELD_SELECTOR((VIRTCHNL_PROTO_HDR_ETH_XXX))
64 u64 ice_hash_field; /* ice hash fields
65 * (BIT_ULL(ICE_FLOW_FIELD_IDX_XXX))
70 ice_vc_hash_field_match_type ice_vc_hash_field_list_os[] = {
71 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC),
72 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA)},
73 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
74 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA)},
75 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
76 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
78 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
79 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
80 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA) |
81 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
82 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
83 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
84 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA) |
85 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
86 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
87 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
88 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
89 ICE_FLOW_HASH_IPV4 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
90 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
91 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
92 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC),
93 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA)},
94 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
95 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA)},
96 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
97 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
99 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
100 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
101 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA) |
102 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
103 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
104 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
105 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA) |
106 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
107 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
108 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
109 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
110 ICE_FLOW_HASH_IPV6 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
111 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
112 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
113 {VIRTCHNL_PROTO_HDR_TCP,
114 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT),
115 BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_SRC_PORT)},
116 {VIRTCHNL_PROTO_HDR_TCP,
117 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
118 BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_DST_PORT)},
119 {VIRTCHNL_PROTO_HDR_TCP,
120 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT) |
121 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
122 ICE_FLOW_HASH_TCP_PORT},
123 {VIRTCHNL_PROTO_HDR_UDP,
124 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT),
125 BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_SRC_PORT)},
126 {VIRTCHNL_PROTO_HDR_UDP,
127 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
128 BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_DST_PORT)},
129 {VIRTCHNL_PROTO_HDR_UDP,
130 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT) |
131 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
132 ICE_FLOW_HASH_UDP_PORT},
133 {VIRTCHNL_PROTO_HDR_SCTP,
134 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT),
135 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT)},
136 {VIRTCHNL_PROTO_HDR_SCTP,
137 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
138 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_DST_PORT)},
139 {VIRTCHNL_PROTO_HDR_SCTP,
140 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT) |
141 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
142 ICE_FLOW_HASH_SCTP_PORT},
146 ice_vc_hash_field_match_type ice_vc_hash_field_list_comms[] = {
147 {VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC),
148 BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_SA)},
149 {VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
150 BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_DA)},
151 {VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC) |
152 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
154 {VIRTCHNL_PROTO_HDR_ETH,
155 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_ETHERTYPE),
156 BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_TYPE)},
157 {VIRTCHNL_PROTO_HDR_S_VLAN,
158 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_S_VLAN_ID),
159 BIT_ULL(ICE_FLOW_FIELD_IDX_S_VLAN)},
160 {VIRTCHNL_PROTO_HDR_C_VLAN,
161 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_C_VLAN_ID),
162 BIT_ULL(ICE_FLOW_FIELD_IDX_C_VLAN)},
163 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC),
164 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA)},
165 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
166 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA)},
167 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
168 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
170 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
171 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
172 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA) |
173 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
174 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
175 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
176 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA) |
177 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
178 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
179 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
180 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
181 ICE_FLOW_HASH_IPV4 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
182 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
183 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
184 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC),
185 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA)},
186 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
187 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA)},
188 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
189 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
191 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
192 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
193 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA) |
194 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
195 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
196 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
197 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA) |
198 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
199 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
200 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
201 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
202 ICE_FLOW_HASH_IPV6 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
203 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
204 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
205 {VIRTCHNL_PROTO_HDR_TCP,
206 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT),
207 BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_SRC_PORT)},
208 {VIRTCHNL_PROTO_HDR_TCP,
209 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
210 BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_DST_PORT)},
211 {VIRTCHNL_PROTO_HDR_TCP,
212 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT) |
213 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
214 ICE_FLOW_HASH_TCP_PORT},
215 {VIRTCHNL_PROTO_HDR_UDP,
216 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT),
217 BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_SRC_PORT)},
218 {VIRTCHNL_PROTO_HDR_UDP,
219 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
220 BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_DST_PORT)},
221 {VIRTCHNL_PROTO_HDR_UDP,
222 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT) |
223 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
224 ICE_FLOW_HASH_UDP_PORT},
225 {VIRTCHNL_PROTO_HDR_SCTP,
226 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT),
227 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT)},
228 {VIRTCHNL_PROTO_HDR_SCTP,
229 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
230 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_DST_PORT)},
231 {VIRTCHNL_PROTO_HDR_SCTP,
232 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT) |
233 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
234 ICE_FLOW_HASH_SCTP_PORT},
235 {VIRTCHNL_PROTO_HDR_PPPOE,
236 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PPPOE_SESS_ID),
237 BIT_ULL(ICE_FLOW_FIELD_IDX_PPPOE_SESS_ID)},
238 {VIRTCHNL_PROTO_HDR_GTPU_IP,
239 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_GTPU_IP_TEID),
240 BIT_ULL(ICE_FLOW_FIELD_IDX_GTPU_IP_TEID)},
241 {VIRTCHNL_PROTO_HDR_L2TPV3,
242 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_L2TPV3_SESS_ID),
243 BIT_ULL(ICE_FLOW_FIELD_IDX_L2TPV3_SESS_ID)},
244 {VIRTCHNL_PROTO_HDR_ESP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ESP_SPI),
245 BIT_ULL(ICE_FLOW_FIELD_IDX_ESP_SPI)},
246 {VIRTCHNL_PROTO_HDR_AH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_AH_SPI),
247 BIT_ULL(ICE_FLOW_FIELD_IDX_AH_SPI)},
248 {VIRTCHNL_PROTO_HDR_PFCP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PFCP_SEID),
249 BIT_ULL(ICE_FLOW_FIELD_IDX_PFCP_SEID)},
253 * ice_get_vf_vsi - get VF's VSI based on the stored index
254 * @vf: VF used to get VSI
256 struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf)
258 return vf->pf->vsi[vf->lan_vsi_idx];
262 * ice_validate_vf_id - helper to check if VF ID is valid
263 * @pf: pointer to the PF structure
264 * @vf_id: the ID of the VF to check
266 static int ice_validate_vf_id(struct ice_pf *pf, u16 vf_id)
268 /* vf_id range is only valid for 0-255, and should always be unsigned */
269 if (vf_id >= pf->num_alloc_vfs) {
270 dev_err(ice_pf_to_dev(pf), "Invalid VF ID: %u\n", vf_id);
277 * ice_check_vf_init - helper to check if VF init complete
278 * @pf: pointer to the PF structure
279 * @vf: the pointer to the VF to check
281 static int ice_check_vf_init(struct ice_pf *pf, struct ice_vf *vf)
283 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
284 dev_err(ice_pf_to_dev(pf), "VF ID: %u in reset. Try again.\n",
292 * ice_err_to_virt_err - translate errors for VF return code
293 * @ice_err: error return code
295 static enum virtchnl_status_code ice_err_to_virt_err(enum ice_status ice_err)
299 return VIRTCHNL_STATUS_SUCCESS;
300 case ICE_ERR_BAD_PTR:
301 case ICE_ERR_INVAL_SIZE:
302 case ICE_ERR_DEVICE_NOT_SUPPORTED:
305 return VIRTCHNL_STATUS_ERR_PARAM;
306 case ICE_ERR_NO_MEMORY:
307 return VIRTCHNL_STATUS_ERR_NO_MEMORY;
308 case ICE_ERR_NOT_READY:
309 case ICE_ERR_RESET_FAILED:
310 case ICE_ERR_FW_API_VER:
311 case ICE_ERR_AQ_ERROR:
312 case ICE_ERR_AQ_TIMEOUT:
313 case ICE_ERR_AQ_FULL:
314 case ICE_ERR_AQ_NO_WORK:
315 case ICE_ERR_AQ_EMPTY:
316 return VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
318 return VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
323 * ice_vc_vf_broadcast - Broadcast a message to all VFs on PF
324 * @pf: pointer to the PF structure
325 * @v_opcode: operation code
326 * @v_retval: return value
327 * @msg: pointer to the msg buffer
328 * @msglen: msg length
331 ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
332 enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
334 struct ice_hw *hw = &pf->hw;
337 ice_for_each_vf(pf, i) {
338 struct ice_vf *vf = &pf->vf[i];
340 /* Not all vfs are enabled so skip the ones that are not */
341 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
342 !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
345 /* Ignore return value on purpose - a given VF may fail, but
346 * we need to keep going and send to all of them
348 ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
354 * ice_set_pfe_link - Set the link speed/status of the virtchnl_pf_event
355 * @vf: pointer to the VF structure
356 * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
357 * @ice_link_speed: link speed specified by ICE_AQ_LINK_SPEED_*
358 * @link_up: whether or not to set the link up/down
361 ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
362 int ice_link_speed, bool link_up)
364 if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
365 pfe->event_data.link_event_adv.link_status = link_up;
367 pfe->event_data.link_event_adv.link_speed =
368 ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
370 pfe->event_data.link_event.link_status = link_up;
371 /* Legacy method for virtchnl link speeds */
372 pfe->event_data.link_event.link_speed =
373 (enum virtchnl_link_speed)
374 ice_conv_link_speed_to_virtchnl(false, ice_link_speed);
379 * ice_vf_has_no_qs_ena - check if the VF has any Rx or Tx queues enabled
380 * @vf: the VF to check
382 * Returns true if the VF has no Rx and no Tx queues enabled and returns false
385 static bool ice_vf_has_no_qs_ena(struct ice_vf *vf)
387 return (!bitmap_weight(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF) &&
388 !bitmap_weight(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF));
392 * ice_is_vf_link_up - check if the VF's link is up
393 * @vf: VF to check if link is up
395 static bool ice_is_vf_link_up(struct ice_vf *vf)
397 struct ice_pf *pf = vf->pf;
399 if (ice_check_vf_init(pf, vf))
402 if (ice_vf_has_no_qs_ena(vf))
404 else if (vf->link_forced)
407 return pf->hw.port_info->phy.link_info.link_info &
412 * ice_vc_notify_vf_link_state - Inform a VF of link status
413 * @vf: pointer to the VF structure
415 * send a link status message to a single VF
417 void ice_vc_notify_vf_link_state(struct ice_vf *vf)
419 struct virtchnl_pf_event pfe = { 0 };
420 struct ice_hw *hw = &vf->pf->hw;
422 pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
423 pfe.severity = PF_EVENT_SEVERITY_INFO;
425 if (ice_is_vf_link_up(vf))
426 ice_set_pfe_link(vf, &pfe,
427 hw->port_info->phy.link_info.link_speed, true);
429 ice_set_pfe_link(vf, &pfe, ICE_AQ_LINK_SPEED_UNKNOWN, false);
431 ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
432 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,
437 * ice_vf_invalidate_vsi - invalidate vsi_idx/vsi_num to remove VSI access
438 * @vf: VF to remove access to VSI for
440 static void ice_vf_invalidate_vsi(struct ice_vf *vf)
442 vf->lan_vsi_idx = ICE_NO_VSI;
443 vf->lan_vsi_num = ICE_NO_VSI;
447 * ice_vf_vsi_release - invalidate the VF's VSI after freeing it
448 * @vf: invalidate this VF's VSI after freeing it
450 static void ice_vf_vsi_release(struct ice_vf *vf)
452 ice_vsi_release(ice_get_vf_vsi(vf));
453 ice_vf_invalidate_vsi(vf);
457 * ice_vf_ctrl_invalidate_vsi - invalidate ctrl_vsi_idx to remove VSI access
458 * @vf: VF that control VSI is being invalidated on
460 static void ice_vf_ctrl_invalidate_vsi(struct ice_vf *vf)
462 vf->ctrl_vsi_idx = ICE_NO_VSI;
466 * ice_vf_ctrl_vsi_release - invalidate the VF's control VSI after freeing it
467 * @vf: VF that control VSI is being released on
469 static void ice_vf_ctrl_vsi_release(struct ice_vf *vf)
471 ice_vsi_release(vf->pf->vsi[vf->ctrl_vsi_idx]);
472 ice_vf_ctrl_invalidate_vsi(vf);
476 * ice_free_vf_res - Free a VF's resources
477 * @vf: pointer to the VF info
479 static void ice_free_vf_res(struct ice_vf *vf)
481 struct ice_pf *pf = vf->pf;
482 int i, last_vector_idx;
484 /* First, disable VF's configuration API to prevent OS from
485 * accessing the VF's VSI after it's freed or invalidated.
487 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
488 ice_vf_fdir_exit(vf);
489 /* free VF control VSI */
490 if (vf->ctrl_vsi_idx != ICE_NO_VSI)
491 ice_vf_ctrl_vsi_release(vf);
493 /* free VSI and disconnect it from the parent uplink */
494 if (vf->lan_vsi_idx != ICE_NO_VSI) {
495 ice_vf_vsi_release(vf);
499 last_vector_idx = vf->first_vector_idx + pf->num_msix_per_vf - 1;
501 /* clear VF MDD event information */
502 memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
503 memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
505 /* Disable interrupts so that VF starts in a known state */
506 for (i = vf->first_vector_idx; i <= last_vector_idx; i++) {
507 wr32(&pf->hw, GLINT_DYN_CTL(i), GLINT_DYN_CTL_CLEARPBA_M);
510 /* reset some of the state variables keeping track of the resources */
511 clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
512 clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
516 * ice_dis_vf_mappings
517 * @vf: pointer to the VF structure
519 static void ice_dis_vf_mappings(struct ice_vf *vf)
521 struct ice_pf *pf = vf->pf;
528 vsi = ice_get_vf_vsi(vf);
530 dev = ice_pf_to_dev(pf);
531 wr32(hw, VPINT_ALLOC(vf->vf_id), 0);
532 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), 0);
534 first = vf->first_vector_idx;
535 last = first + pf->num_msix_per_vf - 1;
536 for (v = first; v <= last; v++) {
539 reg = (((1 << GLINT_VECT2FUNC_IS_PF_S) &
540 GLINT_VECT2FUNC_IS_PF_M) |
541 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
542 GLINT_VECT2FUNC_PF_NUM_M));
543 wr32(hw, GLINT_VECT2FUNC(v), reg);
546 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
547 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
549 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
551 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
552 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
554 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
558 * ice_sriov_free_msix_res - Reset/free any used MSIX resources
559 * @pf: pointer to the PF structure
561 * Since no MSIX entries are taken from the pf->irq_tracker then just clear
562 * the pf->sriov_base_vector.
564 * Returns 0 on success, and -EINVAL on error.
566 static int ice_sriov_free_msix_res(struct ice_pf *pf)
568 struct ice_res_tracker *res;
573 res = pf->irq_tracker;
577 /* give back irq_tracker resources used */
578 WARN_ON(pf->sriov_base_vector < res->num_entries);
580 pf->sriov_base_vector = 0;
586 * ice_set_vf_state_qs_dis - Set VF queues state to disabled
587 * @vf: pointer to the VF structure
589 void ice_set_vf_state_qs_dis(struct ice_vf *vf)
591 /* Clear Rx/Tx enabled queues flag */
592 bitmap_zero(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF);
593 bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
594 clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
598 * ice_dis_vf_qs - Disable the VF queues
599 * @vf: pointer to the VF structure
601 static void ice_dis_vf_qs(struct ice_vf *vf)
603 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
605 ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id);
606 ice_vsi_stop_all_rx_rings(vsi);
607 ice_set_vf_state_qs_dis(vf);
611 * ice_free_vfs - Free all VFs
612 * @pf: pointer to the PF structure
614 void ice_free_vfs(struct ice_pf *pf)
616 struct device *dev = ice_pf_to_dev(pf);
617 struct ice_hw *hw = &pf->hw;
620 set_bit(ICE_VF_DEINIT_IN_PROGRESS, pf->state);
625 ice_eswitch_release(pf);
627 while (test_and_set_bit(ICE_VF_DIS, pf->state))
628 usleep_range(1000, 2000);
630 /* Disable IOV before freeing resources. This lets any VF drivers
631 * running in the host get themselves cleaned up before we yank
632 * the carpet out from underneath their feet.
634 if (!pci_vfs_assigned(pf->pdev))
635 pci_disable_sriov(pf->pdev);
637 dev_warn(dev, "VFs are assigned - not disabling SR-IOV\n");
639 /* Avoid wait time by stopping all VFs at the same time */
640 ice_for_each_vf(pf, i)
641 if (test_bit(ICE_VF_STATE_QS_ENA, pf->vf[i].vf_states))
642 ice_dis_vf_qs(&pf->vf[i]);
644 tmp = pf->num_alloc_vfs;
645 pf->num_qps_per_vf = 0;
646 pf->num_alloc_vfs = 0;
647 for (i = 0; i < tmp; i++) {
648 if (test_bit(ICE_VF_STATE_INIT, pf->vf[i].vf_states)) {
649 /* disable VF qp mappings and set VF disable state */
650 ice_dis_vf_mappings(&pf->vf[i]);
651 set_bit(ICE_VF_STATE_DIS, pf->vf[i].vf_states);
652 ice_free_vf_res(&pf->vf[i]);
656 if (ice_sriov_free_msix_res(pf))
657 dev_err(dev, "Failed to free MSIX resources used by SR-IOV\n");
659 devm_kfree(dev, pf->vf);
662 /* This check is for when the driver is unloaded while VFs are
663 * assigned. Setting the number of VFs to 0 through sysfs is caught
664 * before this function ever gets called.
666 if (!pci_vfs_assigned(pf->pdev)) {
669 /* Acknowledge VFLR for all VFs. Without this, VFs will fail to
670 * work correctly when SR-IOV gets re-enabled.
672 for (vf_id = 0; vf_id < tmp; vf_id++) {
673 u32 reg_idx, bit_idx;
675 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
676 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
677 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
681 /* clear malicious info if the VFs are getting released */
682 for (i = 0; i < tmp; i++)
683 if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->malvfs,
684 ICE_MAX_VF_COUNT, i))
685 dev_dbg(dev, "failed to clear malicious VF state for VF %u\n",
688 clear_bit(ICE_VF_DIS, pf->state);
689 clear_bit(ICE_VF_DEINIT_IN_PROGRESS, pf->state);
690 clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
694 * ice_trigger_vf_reset - Reset a VF on HW
695 * @vf: pointer to the VF structure
696 * @is_vflr: true if VFLR was issued, false if not
697 * @is_pfr: true if the reset was triggered due to a previous PFR
699 * Trigger hardware to start a reset for a particular VF. Expects the caller
700 * to wait the proper amount of time to allow hardware to reset the VF before
701 * it cleans up and restores VF functionality.
703 static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr, bool is_pfr)
705 struct ice_pf *pf = vf->pf;
706 u32 reg, reg_idx, bit_idx;
707 unsigned int vf_abs_id, i;
711 dev = ice_pf_to_dev(pf);
713 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
715 /* Inform VF that it is no longer active, as a warning */
716 clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
718 /* Disable VF's configuration API during reset. The flag is re-enabled
719 * when it's safe again to access VF's VSI.
721 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
723 /* VF_MBX_ARQLEN and VF_MBX_ATQLEN are cleared by PFR, so the driver
724 * needs to clear them in the case of VFR/VFLR. If this is done for
725 * PFR, it can mess up VF resets because the VF driver may already
726 * have started cleanup by the time we get here.
729 wr32(hw, VF_MBX_ARQLEN(vf->vf_id), 0);
730 wr32(hw, VF_MBX_ATQLEN(vf->vf_id), 0);
733 /* In the case of a VFLR, the HW has already reset the VF and we
734 * just need to clean up, so don't hit the VFRTRIG register.
737 /* reset VF using VPGEN_VFRTRIG reg */
738 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
739 reg |= VPGEN_VFRTRIG_VFSWR_M;
740 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
742 /* clear the VFLR bit in GLGEN_VFLRSTAT */
743 reg_idx = (vf_abs_id) / 32;
744 bit_idx = (vf_abs_id) % 32;
745 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
748 wr32(hw, PF_PCI_CIAA,
749 VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
750 for (i = 0; i < ICE_PCI_CIAD_WAIT_COUNT; i++) {
751 reg = rd32(hw, PF_PCI_CIAD);
752 /* no transactions pending so stop polling */
753 if ((reg & VF_TRANS_PENDING_M) == 0)
756 dev_err(dev, "VF %u PCI transactions stuck\n", vf->vf_id);
757 udelay(ICE_PCI_CIAD_WAIT_DELAY_US);
762 * ice_vsi_manage_pvid - Enable or disable port VLAN for VSI
763 * @vsi: the VSI to update
764 * @pvid_info: VLAN ID and QoS used to set the PVID VSI context field
765 * @enable: true for enable PVID false for disable
767 static int ice_vsi_manage_pvid(struct ice_vsi *vsi, u16 pvid_info, bool enable)
769 struct ice_hw *hw = &vsi->back->hw;
770 struct ice_aqc_vsi_props *info;
771 struct ice_vsi_ctx *ctxt;
772 enum ice_status status;
775 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
779 ctxt->info = vsi->info;
782 info->vlan_flags = ICE_AQ_VSI_VLAN_MODE_UNTAGGED |
783 ICE_AQ_VSI_PVLAN_INSERT_PVID |
784 ICE_AQ_VSI_VLAN_EMOD_STR;
785 info->sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
787 info->vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING |
788 ICE_AQ_VSI_VLAN_MODE_ALL;
789 info->sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
792 info->pvid = cpu_to_le16(pvid_info);
793 info->valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
794 ICE_AQ_VSI_PROP_SW_VALID);
796 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
798 dev_info(ice_hw_to_dev(hw), "update VSI for port VLAN failed, err %s aq_err %s\n",
799 ice_stat_str(status),
800 ice_aq_str(hw->adminq.sq_last_status));
805 vsi->info.vlan_flags = info->vlan_flags;
806 vsi->info.sw_flags2 = info->sw_flags2;
807 vsi->info.pvid = info->pvid;
814 * ice_vf_get_port_info - Get the VF's port info structure
815 * @vf: VF used to get the port info structure for
817 static struct ice_port_info *ice_vf_get_port_info(struct ice_vf *vf)
819 return vf->pf->hw.port_info;
823 * ice_vf_vsi_setup - Set up a VF VSI
824 * @vf: VF to setup VSI for
826 * Returns pointer to the successfully allocated VSI struct on success,
827 * otherwise returns NULL on failure.
829 static struct ice_vsi *ice_vf_vsi_setup(struct ice_vf *vf)
831 struct ice_port_info *pi = ice_vf_get_port_info(vf);
832 struct ice_pf *pf = vf->pf;
835 vsi = ice_vsi_setup(pf, pi, ICE_VSI_VF, vf->vf_id);
838 dev_err(ice_pf_to_dev(pf), "Failed to create VF VSI\n");
839 ice_vf_invalidate_vsi(vf);
843 vf->lan_vsi_idx = vsi->idx;
844 vf->lan_vsi_num = vsi->vsi_num;
850 * ice_vf_ctrl_vsi_setup - Set up a VF control VSI
851 * @vf: VF to setup control VSI for
853 * Returns pointer to the successfully allocated VSI struct on success,
854 * otherwise returns NULL on failure.
856 struct ice_vsi *ice_vf_ctrl_vsi_setup(struct ice_vf *vf)
858 struct ice_port_info *pi = ice_vf_get_port_info(vf);
859 struct ice_pf *pf = vf->pf;
862 vsi = ice_vsi_setup(pf, pi, ICE_VSI_CTRL, vf->vf_id);
864 dev_err(ice_pf_to_dev(pf), "Failed to create VF control VSI\n");
865 ice_vf_ctrl_invalidate_vsi(vf);
872 * ice_calc_vf_first_vector_idx - Calculate MSIX vector index in the PF space
873 * @pf: pointer to PF structure
874 * @vf: pointer to VF that the first MSIX vector index is being calculated for
876 * This returns the first MSIX vector index in PF space that is used by this VF.
877 * This index is used when accessing PF relative registers such as
878 * GLINT_VECT2FUNC and GLINT_DYN_CTL.
879 * This will always be the OICR index in the AVF driver so any functionality
880 * using vf->first_vector_idx for queue configuration will have to increment by
881 * 1 to avoid meddling with the OICR index.
883 static int ice_calc_vf_first_vector_idx(struct ice_pf *pf, struct ice_vf *vf)
885 return pf->sriov_base_vector + vf->vf_id * pf->num_msix_per_vf;
889 * ice_vf_rebuild_host_tx_rate_cfg - re-apply the Tx rate limiting configuration
890 * @vf: VF to re-apply the configuration for
892 * Called after a VF VSI has been re-added/rebuild during reset. The PF driver
893 * needs to re-apply the host configured Tx rate limiting configuration.
895 static int ice_vf_rebuild_host_tx_rate_cfg(struct ice_vf *vf)
897 struct device *dev = ice_pf_to_dev(vf->pf);
898 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
901 if (vf->min_tx_rate) {
902 err = ice_set_min_bw_limit(vsi, (u64)vf->min_tx_rate * 1000);
904 dev_err(dev, "failed to set min Tx rate to %d Mbps for VF %u, error %d\n",
905 vf->min_tx_rate, vf->vf_id, err);
910 if (vf->max_tx_rate) {
911 err = ice_set_max_bw_limit(vsi, (u64)vf->max_tx_rate * 1000);
913 dev_err(dev, "failed to set max Tx rate to %d Mbps for VF %u, error %d\n",
914 vf->max_tx_rate, vf->vf_id, err);
923 * ice_vf_rebuild_host_vlan_cfg - add VLAN 0 filter or rebuild the Port VLAN
924 * @vf: VF to add MAC filters for
926 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
927 * always re-adds either a VLAN 0 or port VLAN based filter after reset.
929 static int ice_vf_rebuild_host_vlan_cfg(struct ice_vf *vf)
931 struct device *dev = ice_pf_to_dev(vf->pf);
932 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
936 if (vf->port_vlan_info) {
937 err = ice_vsi_manage_pvid(vsi, vf->port_vlan_info, true);
939 dev_err(dev, "failed to configure port VLAN via VSI parameters for VF %u, error %d\n",
944 vlan_id = vf->port_vlan_info & VLAN_VID_MASK;
947 /* vlan_id will either be 0 or the port VLAN number */
948 err = ice_vsi_add_vlan(vsi, vlan_id, ICE_FWD_TO_VSI);
950 dev_err(dev, "failed to add %s VLAN %u filter for VF %u, error %d\n",
951 vf->port_vlan_info ? "port" : "", vlan_id, vf->vf_id,
960 * ice_vf_rebuild_host_mac_cfg - add broadcast and the VF's perm_addr/LAA
961 * @vf: VF to add MAC filters for
963 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
964 * always re-adds a broadcast filter and the VF's perm_addr/LAA after reset.
966 static int ice_vf_rebuild_host_mac_cfg(struct ice_vf *vf)
968 struct device *dev = ice_pf_to_dev(vf->pf);
969 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
970 enum ice_status status;
971 u8 broadcast[ETH_ALEN];
973 if (ice_is_eswitch_mode_switchdev(vf->pf))
976 eth_broadcast_addr(broadcast);
977 status = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
979 dev_err(dev, "failed to add broadcast MAC filter for VF %u, error %s\n",
980 vf->vf_id, ice_stat_str(status));
981 return ice_status_to_errno(status);
986 if (is_valid_ether_addr(vf->hw_lan_addr.addr)) {
987 status = ice_fltr_add_mac(vsi, vf->hw_lan_addr.addr,
990 dev_err(dev, "failed to add default unicast MAC filter %pM for VF %u, error %s\n",
991 &vf->hw_lan_addr.addr[0], vf->vf_id,
992 ice_stat_str(status));
993 return ice_status_to_errno(status);
997 ether_addr_copy(vf->dev_lan_addr.addr, vf->hw_lan_addr.addr);
1004 * ice_vf_set_host_trust_cfg - set trust setting based on pre-reset value
1005 * @vf: VF to configure trust setting for
1007 static void ice_vf_set_host_trust_cfg(struct ice_vf *vf)
1010 set_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
1012 clear_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
1016 * ice_ena_vf_msix_mappings - enable VF MSIX mappings in hardware
1017 * @vf: VF to enable MSIX mappings for
1019 * Some of the registers need to be indexed/configured using hardware global
1020 * device values and other registers need 0-based values, which represent PF
1023 static void ice_ena_vf_msix_mappings(struct ice_vf *vf)
1025 int device_based_first_msix, device_based_last_msix;
1026 int pf_based_first_msix, pf_based_last_msix, v;
1027 struct ice_pf *pf = vf->pf;
1028 int device_based_vf_id;
1033 pf_based_first_msix = vf->first_vector_idx;
1034 pf_based_last_msix = (pf_based_first_msix + pf->num_msix_per_vf) - 1;
1036 device_based_first_msix = pf_based_first_msix +
1037 pf->hw.func_caps.common_cap.msix_vector_first_id;
1038 device_based_last_msix =
1039 (device_based_first_msix + pf->num_msix_per_vf) - 1;
1040 device_based_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1042 reg = (((device_based_first_msix << VPINT_ALLOC_FIRST_S) &
1043 VPINT_ALLOC_FIRST_M) |
1044 ((device_based_last_msix << VPINT_ALLOC_LAST_S) &
1045 VPINT_ALLOC_LAST_M) | VPINT_ALLOC_VALID_M);
1046 wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
1048 reg = (((device_based_first_msix << VPINT_ALLOC_PCI_FIRST_S)
1049 & VPINT_ALLOC_PCI_FIRST_M) |
1050 ((device_based_last_msix << VPINT_ALLOC_PCI_LAST_S) &
1051 VPINT_ALLOC_PCI_LAST_M) | VPINT_ALLOC_PCI_VALID_M);
1052 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg);
1054 /* map the interrupts to its functions */
1055 for (v = pf_based_first_msix; v <= pf_based_last_msix; v++) {
1056 reg = (((device_based_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
1057 GLINT_VECT2FUNC_VF_NUM_M) |
1058 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
1059 GLINT_VECT2FUNC_PF_NUM_M));
1060 wr32(hw, GLINT_VECT2FUNC(v), reg);
1063 /* Map mailbox interrupt to VF MSI-X vector 0 */
1064 wr32(hw, VPINT_MBX_CTL(device_based_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M);
1068 * ice_ena_vf_q_mappings - enable Rx/Tx queue mappings for a VF
1069 * @vf: VF to enable the mappings for
1070 * @max_txq: max Tx queues allowed on the VF's VSI
1071 * @max_rxq: max Rx queues allowed on the VF's VSI
1073 static void ice_ena_vf_q_mappings(struct ice_vf *vf, u16 max_txq, u16 max_rxq)
1075 struct device *dev = ice_pf_to_dev(vf->pf);
1076 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1077 struct ice_hw *hw = &vf->pf->hw;
1080 /* set regardless of mapping mode */
1081 wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M);
1083 /* VF Tx queues allocation */
1084 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
1085 /* set the VF PF Tx queue range
1086 * VFNUMQ value should be set to (number of queues - 1). A value
1087 * of 0 means 1 queue and a value of 255 means 256 queues
1089 reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
1090 VPLAN_TX_QBASE_VFFIRSTQ_M) |
1091 (((max_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
1092 VPLAN_TX_QBASE_VFNUMQ_M));
1093 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
1095 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
1098 /* set regardless of mapping mode */
1099 wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M);
1101 /* VF Rx queues allocation */
1102 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
1103 /* set the VF PF Rx queue range
1104 * VFNUMQ value should be set to (number of queues - 1). A value
1105 * of 0 means 1 queue and a value of 255 means 256 queues
1107 reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
1108 VPLAN_RX_QBASE_VFFIRSTQ_M) |
1109 (((max_rxq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
1110 VPLAN_RX_QBASE_VFNUMQ_M));
1111 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
1113 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
1118 * ice_ena_vf_mappings - enable VF MSIX and queue mapping
1119 * @vf: pointer to the VF structure
1121 static void ice_ena_vf_mappings(struct ice_vf *vf)
1123 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1125 ice_ena_vf_msix_mappings(vf);
1126 ice_ena_vf_q_mappings(vf, vsi->alloc_txq, vsi->alloc_rxq);
1131 * @pf: pointer to the PF structure
1132 * @avail_res: available resources in the PF structure
1133 * @max_res: maximum resources that can be given per VF
1134 * @min_res: minimum resources that can be given per VF
1136 * Returns non-zero value if resources (queues/vectors) are available or
1137 * returns zero if PF cannot accommodate for all num_alloc_vfs.
1140 ice_determine_res(struct ice_pf *pf, u16 avail_res, u16 max_res, u16 min_res)
1142 bool checked_min_res = false;
1145 /* start by checking if PF can assign max number of resources for
1146 * all num_alloc_vfs.
1147 * if yes, return number per VF
1148 * If no, divide by 2 and roundup, check again
1149 * repeat the loop till we reach a point where even minimum resources
1150 * are not available, in that case return 0
1153 while ((res >= min_res) && !checked_min_res) {
1156 num_all_res = pf->num_alloc_vfs * res;
1157 if (num_all_res <= avail_res)
1161 checked_min_res = true;
1163 res = DIV_ROUND_UP(res, 2);
1169 * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space
1170 * @vf: VF to calculate the register index for
1171 * @q_vector: a q_vector associated to the VF
1173 int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector)
1177 if (!vf || !q_vector)
1182 /* always add one to account for the OICR being the first MSIX */
1183 return pf->sriov_base_vector + pf->num_msix_per_vf * vf->vf_id +
1184 q_vector->v_idx + 1;
1188 * ice_get_max_valid_res_idx - Get the max valid resource index
1189 * @res: pointer to the resource to find the max valid index for
1191 * Start from the end of the ice_res_tracker and return right when we find the
1192 * first res->list entry with the ICE_RES_VALID_BIT set. This function is only
1193 * valid for SR-IOV because it is the only consumer that manipulates the
1194 * res->end and this is always called when res->end is set to res->num_entries.
1196 static int ice_get_max_valid_res_idx(struct ice_res_tracker *res)
1203 for (i = res->num_entries - 1; i >= 0; i--)
1204 if (res->list[i] & ICE_RES_VALID_BIT)
1211 * ice_sriov_set_msix_res - Set any used MSIX resources
1212 * @pf: pointer to PF structure
1213 * @num_msix_needed: number of MSIX vectors needed for all SR-IOV VFs
1215 * This function allows SR-IOV resources to be taken from the end of the PF's
1216 * allowed HW MSIX vectors so that the irq_tracker will not be affected. We
1217 * just set the pf->sriov_base_vector and return success.
1219 * If there are not enough resources available, return an error. This should
1220 * always be caught by ice_set_per_vf_res().
1222 * Return 0 on success, and -EINVAL when there are not enough MSIX vectors
1223 * in the PF's space available for SR-IOV.
1225 static int ice_sriov_set_msix_res(struct ice_pf *pf, u16 num_msix_needed)
1227 u16 total_vectors = pf->hw.func_caps.common_cap.num_msix_vectors;
1228 int vectors_used = pf->irq_tracker->num_entries;
1229 int sriov_base_vector;
1231 sriov_base_vector = total_vectors - num_msix_needed;
1233 /* make sure we only grab irq_tracker entries from the list end and
1234 * that we have enough available MSIX vectors
1236 if (sriov_base_vector < vectors_used)
1239 pf->sriov_base_vector = sriov_base_vector;
1245 * ice_set_per_vf_res - check if vectors and queues are available
1246 * @pf: pointer to the PF structure
1248 * First, determine HW interrupts from common pool. If we allocate fewer VFs, we
1249 * get more vectors and can enable more queues per VF. Note that this does not
1250 * grab any vectors from the SW pool already allocated. Also note, that all
1251 * vector counts include one for each VF's miscellaneous interrupt vector
1254 * Minimum VFs - 2 vectors, 1 queue pair
1255 * Small VFs - 5 vectors, 4 queue pairs
1256 * Medium VFs - 17 vectors, 16 queue pairs
1258 * Second, determine number of queue pairs per VF by starting with a pre-defined
1259 * maximum each VF supports. If this is not possible, then we adjust based on
1260 * queue pairs available on the device.
1262 * Lastly, set queue and MSI-X VF variables tracked by the PF so it can be used
1263 * by each VF during VF initialization and reset.
1265 static int ice_set_per_vf_res(struct ice_pf *pf)
1267 int max_valid_res_idx = ice_get_max_valid_res_idx(pf->irq_tracker);
1268 int msix_avail_per_vf, msix_avail_for_sriov;
1269 struct device *dev = ice_pf_to_dev(pf);
1270 u16 num_msix_per_vf, num_txq, num_rxq;
1272 if (!pf->num_alloc_vfs || max_valid_res_idx < 0)
1275 /* determine MSI-X resources per VF */
1276 msix_avail_for_sriov = pf->hw.func_caps.common_cap.num_msix_vectors -
1277 pf->irq_tracker->num_entries;
1278 msix_avail_per_vf = msix_avail_for_sriov / pf->num_alloc_vfs;
1279 if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MED) {
1280 num_msix_per_vf = ICE_NUM_VF_MSIX_MED;
1281 } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_SMALL) {
1282 num_msix_per_vf = ICE_NUM_VF_MSIX_SMALL;
1283 } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MULTIQ_MIN) {
1284 num_msix_per_vf = ICE_NUM_VF_MSIX_MULTIQ_MIN;
1285 } else if (msix_avail_per_vf >= ICE_MIN_INTR_PER_VF) {
1286 num_msix_per_vf = ICE_MIN_INTR_PER_VF;
1288 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",
1289 msix_avail_for_sriov, ICE_MIN_INTR_PER_VF,
1294 /* determine queue resources per VF */
1295 num_txq = ice_determine_res(pf, ice_get_avail_txq_count(pf),
1297 num_msix_per_vf - ICE_NONQ_VECS_VF,
1298 ICE_MAX_RSS_QS_PER_VF),
1301 num_rxq = ice_determine_res(pf, ice_get_avail_rxq_count(pf),
1303 num_msix_per_vf - ICE_NONQ_VECS_VF,
1304 ICE_MAX_RSS_QS_PER_VF),
1307 if (!num_txq || !num_rxq) {
1308 dev_err(dev, "Not enough queues to support minimum of %d queue pairs per VF for %d VFs\n",
1309 ICE_MIN_QS_PER_VF, pf->num_alloc_vfs);
1313 if (ice_sriov_set_msix_res(pf, num_msix_per_vf * pf->num_alloc_vfs)) {
1314 dev_err(dev, "Unable to set MSI-X resources for %d VFs\n",
1319 /* only allow equal Tx/Rx queue count (i.e. queue pairs) */
1320 pf->num_qps_per_vf = min_t(int, num_txq, num_rxq);
1321 pf->num_msix_per_vf = num_msix_per_vf;
1322 dev_info(dev, "Enabling %d VFs with %d vectors and %d queues per VF\n",
1323 pf->num_alloc_vfs, pf->num_msix_per_vf, pf->num_qps_per_vf);
1329 * ice_clear_vf_reset_trigger - enable VF to access hardware
1330 * @vf: VF to enabled hardware access for
1332 static void ice_clear_vf_reset_trigger(struct ice_vf *vf)
1334 struct ice_hw *hw = &vf->pf->hw;
1337 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
1338 reg &= ~VPGEN_VFRTRIG_VFSWR_M;
1339 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
1344 * ice_vf_set_vsi_promisc - set given VF VSI to given promiscuous mode(s)
1345 * @vf: pointer to the VF info
1346 * @vsi: the VSI being configured
1347 * @promisc_m: mask of promiscuous config bits
1348 * @rm_promisc: promisc flag request from the VF to remove or add filter
1350 * This function configures VF VSI promiscuous mode, based on the VF requests,
1351 * for Unicast, Multicast and VLAN
1353 static enum ice_status
1354 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m,
1357 struct ice_pf *pf = vf->pf;
1358 enum ice_status status = 0;
1362 if (vsi->num_vlan) {
1363 status = ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_m,
1365 } else if (vf->port_vlan_info) {
1367 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
1368 vf->port_vlan_info);
1370 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
1371 vf->port_vlan_info);
1374 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
1377 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
1384 static void ice_vf_clear_counters(struct ice_vf *vf)
1386 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1390 memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
1391 memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
1395 * ice_vf_pre_vsi_rebuild - tasks to be done prior to VSI rebuild
1396 * @vf: VF to perform pre VSI rebuild tasks
1398 * These tasks are items that don't need to be amortized since they are most
1399 * likely called in a for loop with all VF(s) in the reset_all_vfs() case.
1401 static void ice_vf_pre_vsi_rebuild(struct ice_vf *vf)
1403 ice_vf_clear_counters(vf);
1404 ice_clear_vf_reset_trigger(vf);
1408 * ice_vf_rebuild_aggregator_node_cfg - rebuild aggregator node config
1409 * @vsi: Pointer to VSI
1411 * This function moves VSI into corresponding scheduler aggregator node
1412 * based on cached value of "aggregator node info" per VSI
1414 static void ice_vf_rebuild_aggregator_node_cfg(struct ice_vsi *vsi)
1416 struct ice_pf *pf = vsi->back;
1417 enum ice_status status;
1423 dev = ice_pf_to_dev(pf);
1424 if (vsi->agg_node->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
1426 "agg_id %u already has reached max_num_vsis %u\n",
1427 vsi->agg_node->agg_id, vsi->agg_node->num_vsis);
1431 status = ice_move_vsi_to_agg(pf->hw.port_info, vsi->agg_node->agg_id,
1432 vsi->idx, vsi->tc_cfg.ena_tc);
1434 dev_dbg(dev, "unable to move VSI idx %u into aggregator %u node",
1435 vsi->idx, vsi->agg_node->agg_id);
1437 vsi->agg_node->num_vsis++;
1441 * ice_vf_rebuild_host_cfg - host admin configuration is persistent across reset
1442 * @vf: VF to rebuild host configuration on
1444 static void ice_vf_rebuild_host_cfg(struct ice_vf *vf)
1446 struct device *dev = ice_pf_to_dev(vf->pf);
1447 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1449 ice_vf_set_host_trust_cfg(vf);
1451 if (ice_vf_rebuild_host_mac_cfg(vf))
1452 dev_err(dev, "failed to rebuild default MAC configuration for VF %d\n",
1455 if (ice_vf_rebuild_host_vlan_cfg(vf))
1456 dev_err(dev, "failed to rebuild VLAN configuration for VF %u\n",
1459 if (ice_vf_rebuild_host_tx_rate_cfg(vf))
1460 dev_err(dev, "failed to rebuild Tx rate limiting configuration for VF %u\n",
1463 /* rebuild aggregator node config for main VF VSI */
1464 ice_vf_rebuild_aggregator_node_cfg(vsi);
1468 * ice_vf_rebuild_vsi_with_release - release and setup the VF's VSI
1469 * @vf: VF to release and setup the VSI for
1471 * This is only called when a single VF is being reset (i.e. VFR, VFLR, host VF
1472 * configuration change, etc.).
1474 static int ice_vf_rebuild_vsi_with_release(struct ice_vf *vf)
1476 ice_vf_vsi_release(vf);
1477 if (!ice_vf_vsi_setup(vf))
1484 * ice_vf_rebuild_vsi - rebuild the VF's VSI
1485 * @vf: VF to rebuild the VSI for
1487 * This is only called when all VF(s) are being reset (i.e. PCIe Reset on the
1488 * host, PFR, CORER, etc.).
1490 static int ice_vf_rebuild_vsi(struct ice_vf *vf)
1492 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1493 struct ice_pf *pf = vf->pf;
1495 if (ice_vsi_rebuild(vsi, true)) {
1496 dev_err(ice_pf_to_dev(pf), "failed to rebuild VF %d VSI\n",
1500 /* vsi->idx will remain the same in this case so don't update
1503 vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
1504 vf->lan_vsi_num = vsi->vsi_num;
1510 * ice_vf_set_initialized - VF is ready for VIRTCHNL communication
1511 * @vf: VF to set in initialized state
1513 * After this function the VF will be ready to receive/handle the
1514 * VIRTCHNL_OP_GET_VF_RESOURCES message
1516 static void ice_vf_set_initialized(struct ice_vf *vf)
1518 ice_set_vf_state_qs_dis(vf);
1519 clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
1520 clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
1521 clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
1522 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
1526 * ice_vf_post_vsi_rebuild - tasks to do after the VF's VSI have been rebuilt
1527 * @vf: VF to perform tasks on
1529 static void ice_vf_post_vsi_rebuild(struct ice_vf *vf)
1531 struct ice_pf *pf = vf->pf;
1536 ice_vf_rebuild_host_cfg(vf);
1538 ice_vf_set_initialized(vf);
1539 ice_ena_vf_mappings(vf);
1540 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1544 * ice_reset_all_vfs - reset all allocated VFs in one go
1545 * @pf: pointer to the PF structure
1546 * @is_vflr: true if VFLR was issued, false if not
1548 * First, tell the hardware to reset each VF, then do all the waiting in one
1549 * chunk, and finally finish restoring each VF after the wait. This is useful
1550 * during PF routines which need to reset all VFs, as otherwise it must perform
1551 * these resets in a serialized fashion.
1553 * Returns true if any VFs were reset, and false otherwise.
1555 bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr)
1557 struct device *dev = ice_pf_to_dev(pf);
1558 struct ice_hw *hw = &pf->hw;
1562 /* If we don't have any VFs, then there is nothing to reset */
1563 if (!pf->num_alloc_vfs)
1566 /* clear all malicious info if the VFs are getting reset */
1567 ice_for_each_vf(pf, i)
1568 if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->malvfs, ICE_MAX_VF_COUNT, i))
1569 dev_dbg(dev, "failed to clear malicious VF state for VF %u\n", i);
1571 /* If VFs have been disabled, there is no need to reset */
1572 if (test_and_set_bit(ICE_VF_DIS, pf->state))
1575 /* Begin reset on all VFs at once */
1576 ice_for_each_vf(pf, v)
1577 ice_trigger_vf_reset(&pf->vf[v], is_vflr, true);
1579 /* HW requires some time to make sure it can flush the FIFO for a VF
1580 * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1581 * sequence to make sure that it has completed. We'll keep track of
1582 * the VFs using a simple iterator that increments once that VF has
1583 * finished resetting.
1585 for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1586 /* Check each VF in sequence */
1587 while (v < pf->num_alloc_vfs) {
1591 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1592 if (!(reg & VPGEN_VFRSTAT_VFRD_M)) {
1593 /* only delay if the check failed */
1594 usleep_range(10, 20);
1598 /* If the current VF has finished resetting, move on
1599 * to the next VF in sequence.
1605 /* Display a warning if at least one VF didn't manage to reset in
1606 * time, but continue on with the operation.
1608 if (v < pf->num_alloc_vfs)
1609 dev_warn(dev, "VF reset check timeout\n");
1611 /* free VF resources to begin resetting the VSI state */
1612 ice_for_each_vf(pf, v) {
1615 vf->driver_caps = 0;
1616 ice_vc_set_default_allowlist(vf);
1618 ice_vf_fdir_exit(vf);
1619 /* clean VF control VSI when resetting VFs since it should be
1620 * setup only when VF creates its first FDIR rule.
1622 if (vf->ctrl_vsi_idx != ICE_NO_VSI)
1623 ice_vf_ctrl_invalidate_vsi(vf);
1625 ice_vf_pre_vsi_rebuild(vf);
1626 ice_vf_rebuild_vsi(vf);
1627 ice_vf_post_vsi_rebuild(vf);
1630 if (ice_is_eswitch_mode_switchdev(pf))
1631 if (ice_eswitch_rebuild(pf))
1632 dev_warn(dev, "eswitch rebuild failed\n");
1635 clear_bit(ICE_VF_DIS, pf->state);
1641 * ice_is_vf_disabled
1642 * @vf: pointer to the VF info
1644 * Returns true if the PF or VF is disabled, false otherwise.
1646 bool ice_is_vf_disabled(struct ice_vf *vf)
1648 struct ice_pf *pf = vf->pf;
1650 /* If the PF has been disabled, there is no need resetting VF until
1651 * PF is active again. Similarly, if the VF has been disabled, this
1652 * means something else is resetting the VF, so we shouldn't continue.
1653 * Otherwise, set disable VF state bit for actual reset, and continue.
1655 return (test_bit(ICE_VF_DIS, pf->state) ||
1656 test_bit(ICE_VF_STATE_DIS, vf->vf_states));
1660 * ice_reset_vf - Reset a particular VF
1661 * @vf: pointer to the VF structure
1662 * @is_vflr: true if VFLR was issued, false if not
1664 * Returns true if the VF is currently in reset, resets successfully, or resets
1665 * are disabled and false otherwise.
1667 bool ice_reset_vf(struct ice_vf *vf, bool is_vflr)
1669 struct ice_pf *pf = vf->pf;
1670 struct ice_vsi *vsi;
1678 dev = ice_pf_to_dev(pf);
1680 if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
1681 dev_dbg(dev, "Trying to reset VF %d, but all VF resets are disabled\n",
1686 if (ice_is_vf_disabled(vf)) {
1687 dev_dbg(dev, "VF is already disabled, there is no need for resetting it, telling VM, all is fine %d\n",
1692 /* Set VF disable bit state here, before triggering reset */
1693 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
1694 ice_trigger_vf_reset(vf, is_vflr, false);
1696 vsi = ice_get_vf_vsi(vf);
1698 if (test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states))
1701 /* Call Disable LAN Tx queue AQ whether or not queues are
1702 * enabled. This is needed for successful completion of VFR.
1704 ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
1705 NULL, ICE_VF_RESET, vf->vf_id, NULL);
1708 /* poll VPGEN_VFRSTAT reg to make sure
1709 * that reset is complete
1711 for (i = 0; i < 10; i++) {
1712 /* VF reset requires driver to first reset the VF and then
1713 * poll the status register to make sure that the reset
1714 * completed successfully.
1716 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1717 if (reg & VPGEN_VFRSTAT_VFRD_M) {
1722 /* only sleep if the reset is not done */
1723 usleep_range(10, 20);
1726 vf->driver_caps = 0;
1727 ice_vc_set_default_allowlist(vf);
1729 /* Display a warning if VF didn't manage to reset in time, but need to
1730 * continue on with the operation.
1733 dev_warn(dev, "VF reset check timeout on VF %d\n", vf->vf_id);
1735 /* disable promiscuous modes in case they were enabled
1736 * ignore any error if disabling process failed
1738 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
1739 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
1740 if (vf->port_vlan_info || vsi->num_vlan)
1741 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
1743 promisc_m = ICE_UCAST_PROMISC_BITS;
1745 if (ice_vf_set_vsi_promisc(vf, vsi, promisc_m, true))
1746 dev_err(dev, "disabling promiscuous mode failed\n");
1749 ice_vf_fdir_exit(vf);
1750 /* clean VF control VSI when resetting VF since it should be setup
1751 * only when VF creates its first FDIR rule.
1753 if (vf->ctrl_vsi_idx != ICE_NO_VSI)
1754 ice_vf_ctrl_vsi_release(vf);
1756 ice_vf_pre_vsi_rebuild(vf);
1758 if (ice_vf_rebuild_vsi_with_release(vf)) {
1759 dev_err(dev, "Failed to release and setup the VF%u's VSI\n", vf->vf_id);
1763 ice_vf_post_vsi_rebuild(vf);
1764 vsi = ice_get_vf_vsi(vf);
1765 ice_eswitch_update_repr(vsi);
1767 /* if the VF has been reset allow it to come up again */
1768 if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->malvfs, ICE_MAX_VF_COUNT, vf->vf_id))
1769 dev_dbg(dev, "failed to clear malicious VF state for VF %u\n", i);
1775 * ice_vc_notify_link_state - Inform all VFs on a PF of link status
1776 * @pf: pointer to the PF structure
1778 void ice_vc_notify_link_state(struct ice_pf *pf)
1782 ice_for_each_vf(pf, i)
1783 ice_vc_notify_vf_link_state(&pf->vf[i]);
1787 * ice_vc_notify_reset - Send pending reset message to all VFs
1788 * @pf: pointer to the PF structure
1790 * indicate a pending reset to all VFs on a given PF
1792 void ice_vc_notify_reset(struct ice_pf *pf)
1794 struct virtchnl_pf_event pfe;
1796 if (!pf->num_alloc_vfs)
1799 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1800 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1801 ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
1802 (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
1806 * ice_vc_notify_vf_reset - Notify VF of a reset event
1807 * @vf: pointer to the VF structure
1809 static void ice_vc_notify_vf_reset(struct ice_vf *vf)
1811 struct virtchnl_pf_event pfe;
1818 if (ice_validate_vf_id(pf, vf->vf_id))
1821 /* Bail out if VF is in disabled state, neither initialized, nor active
1822 * state - otherwise proceed with notifications
1824 if ((!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
1825 !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) ||
1826 test_bit(ICE_VF_STATE_DIS, vf->vf_states))
1829 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1830 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1831 ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, VIRTCHNL_OP_EVENT,
1832 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe, sizeof(pfe),
1837 * ice_init_vf_vsi_res - initialize/setup VF VSI resources
1838 * @vf: VF to initialize/setup the VSI for
1840 * This function creates a VSI for the VF, adds a VLAN 0 filter, and sets up the
1841 * VF VSI's broadcast filter and is only used during initial VF creation.
1843 static int ice_init_vf_vsi_res(struct ice_vf *vf)
1845 struct ice_pf *pf = vf->pf;
1846 u8 broadcast[ETH_ALEN];
1847 enum ice_status status;
1848 struct ice_vsi *vsi;
1852 vf->first_vector_idx = ice_calc_vf_first_vector_idx(pf, vf);
1854 dev = ice_pf_to_dev(pf);
1855 vsi = ice_vf_vsi_setup(vf);
1859 err = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
1861 dev_warn(dev, "Failed to add VLAN 0 filter for VF %d\n",
1866 eth_broadcast_addr(broadcast);
1867 status = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
1869 dev_err(dev, "Failed to add broadcast MAC filter for VF %d, status %s\n",
1870 vf->vf_id, ice_stat_str(status));
1871 err = ice_status_to_errno(status);
1880 ice_vf_vsi_release(vf);
1885 * ice_start_vfs - start VFs so they are ready to be used by SR-IOV
1886 * @pf: PF the VFs are associated with
1888 static int ice_start_vfs(struct ice_pf *pf)
1890 struct ice_hw *hw = &pf->hw;
1893 ice_for_each_vf(pf, i) {
1894 struct ice_vf *vf = &pf->vf[i];
1896 ice_clear_vf_reset_trigger(vf);
1898 retval = ice_init_vf_vsi_res(vf);
1900 dev_err(ice_pf_to_dev(pf), "Failed to initialize VSI resources for VF %d, error %d\n",
1905 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
1906 ice_ena_vf_mappings(vf);
1907 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1914 for (i = i - 1; i >= 0; i--) {
1915 struct ice_vf *vf = &pf->vf[i];
1917 ice_dis_vf_mappings(vf);
1918 ice_vf_vsi_release(vf);
1925 * ice_set_dflt_settings_vfs - set VF defaults during initialization/creation
1926 * @pf: PF holding reference to all VFs for default configuration
1928 static void ice_set_dflt_settings_vfs(struct ice_pf *pf)
1932 ice_for_each_vf(pf, i) {
1933 struct ice_vf *vf = &pf->vf[i];
1937 vf->vf_sw_id = pf->first_sw;
1938 /* assign default capabilities */
1939 set_bit(ICE_VIRTCHNL_VF_CAP_L2, &vf->vf_caps);
1940 vf->spoofchk = true;
1941 vf->num_vf_qs = pf->num_qps_per_vf;
1942 ice_vc_set_default_allowlist(vf);
1944 /* ctrl_vsi_idx will be set to a valid value only when VF
1945 * creates its first fdir rule.
1947 ice_vf_ctrl_invalidate_vsi(vf);
1948 ice_vf_fdir_init(vf);
1950 ice_vc_set_dflt_vf_ops(&vf->vc_ops);
1955 * ice_alloc_vfs - allocate num_vfs in the PF structure
1956 * @pf: PF to store the allocated VFs in
1957 * @num_vfs: number of VFs to allocate
1959 static int ice_alloc_vfs(struct ice_pf *pf, int num_vfs)
1963 vfs = devm_kcalloc(ice_pf_to_dev(pf), num_vfs, sizeof(*vfs),
1969 pf->num_alloc_vfs = num_vfs;
1975 * ice_ena_vfs - enable VFs so they are ready to be used
1976 * @pf: pointer to the PF structure
1977 * @num_vfs: number of VFs to enable
1979 static int ice_ena_vfs(struct ice_pf *pf, u16 num_vfs)
1981 struct device *dev = ice_pf_to_dev(pf);
1982 struct ice_hw *hw = &pf->hw;
1985 /* Disable global interrupt 0 so we don't try to handle the VFLR. */
1986 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1987 ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
1988 set_bit(ICE_OICR_INTR_DIS, pf->state);
1991 ret = pci_enable_sriov(pf->pdev, num_vfs);
1993 pf->num_alloc_vfs = 0;
1994 goto err_unroll_intr;
1997 ret = ice_alloc_vfs(pf, num_vfs);
1999 goto err_pci_disable_sriov;
2001 if (ice_set_per_vf_res(pf)) {
2002 dev_err(dev, "Not enough resources for %d VFs, try with fewer number of VFs\n",
2005 goto err_unroll_sriov;
2008 ice_set_dflt_settings_vfs(pf);
2010 if (ice_start_vfs(pf)) {
2011 dev_err(dev, "Failed to start VF(s)\n");
2013 goto err_unroll_sriov;
2016 clear_bit(ICE_VF_DIS, pf->state);
2018 if (ice_eswitch_configure(pf))
2019 goto err_unroll_sriov;
2024 devm_kfree(dev, pf->vf);
2026 pf->num_alloc_vfs = 0;
2027 err_pci_disable_sriov:
2028 pci_disable_sriov(pf->pdev);
2030 /* rearm interrupts here */
2031 ice_irq_dynamic_ena(hw, NULL, NULL);
2032 clear_bit(ICE_OICR_INTR_DIS, pf->state);
2037 * ice_pci_sriov_ena - Enable or change number of VFs
2038 * @pf: pointer to the PF structure
2039 * @num_vfs: number of VFs to allocate
2041 * Returns 0 on success and negative on failure
2043 static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
2045 int pre_existing_vfs = pci_num_vf(pf->pdev);
2046 struct device *dev = ice_pf_to_dev(pf);
2049 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
2051 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
2054 if (num_vfs > pf->num_vfs_supported) {
2055 dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
2056 num_vfs, pf->num_vfs_supported);
2060 dev_info(dev, "Enabling %d VFs\n", num_vfs);
2061 err = ice_ena_vfs(pf, num_vfs);
2063 dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
2067 set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
2072 * ice_check_sriov_allowed - check if SR-IOV is allowed based on various checks
2073 * @pf: PF to enabled SR-IOV on
2075 static int ice_check_sriov_allowed(struct ice_pf *pf)
2077 struct device *dev = ice_pf_to_dev(pf);
2079 if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
2080 dev_err(dev, "This device is not capable of SR-IOV\n");
2084 if (ice_is_safe_mode(pf)) {
2085 dev_err(dev, "SR-IOV cannot be configured - Device is in Safe Mode\n");
2089 if (!ice_pf_state_is_nominal(pf)) {
2090 dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
2098 * ice_sriov_configure - Enable or change number of VFs via sysfs
2099 * @pdev: pointer to a pci_dev structure
2100 * @num_vfs: number of VFs to allocate or 0 to free VFs
2102 * This function is called when the user updates the number of VFs in sysfs. On
2103 * success return whatever num_vfs was set to by the caller. Return negative on
2106 int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
2108 struct ice_pf *pf = pci_get_drvdata(pdev);
2109 struct device *dev = ice_pf_to_dev(pf);
2110 enum ice_status status;
2113 err = ice_check_sriov_allowed(pf);
2118 if (!pci_vfs_assigned(pdev)) {
2119 ice_mbx_deinit_snapshot(&pf->hw);
2122 ice_enable_lag(pf->lag);
2126 dev_err(dev, "can't free VFs because some are assigned to VMs.\n");
2130 status = ice_mbx_init_snapshot(&pf->hw, num_vfs);
2132 return ice_status_to_errno(status);
2134 err = ice_pci_sriov_ena(pf, num_vfs);
2136 ice_mbx_deinit_snapshot(&pf->hw);
2141 ice_disable_lag(pf->lag);
2146 * ice_process_vflr_event - Free VF resources via IRQ calls
2147 * @pf: pointer to the PF structure
2149 * called from the VFLR IRQ handler to
2150 * free up VF resources and state variables
2152 void ice_process_vflr_event(struct ice_pf *pf)
2154 struct ice_hw *hw = &pf->hw;
2158 if (!test_and_clear_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
2162 ice_for_each_vf(pf, vf_id) {
2163 struct ice_vf *vf = &pf->vf[vf_id];
2164 u32 reg_idx, bit_idx;
2166 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
2167 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
2168 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
2169 reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
2170 if (reg & BIT(bit_idx))
2171 /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
2172 ice_reset_vf(vf, true);
2177 * ice_vc_reset_vf - Perform software reset on the VF after informing the AVF
2178 * @vf: pointer to the VF info
2180 static void ice_vc_reset_vf(struct ice_vf *vf)
2182 ice_vc_notify_vf_reset(vf);
2183 ice_reset_vf(vf, false);
2187 * ice_get_vf_from_pfq - get the VF who owns the PF space queue passed in
2188 * @pf: PF used to index all VFs
2189 * @pfq: queue index relative to the PF's function space
2191 * If no VF is found who owns the pfq then return NULL, otherwise return a
2192 * pointer to the VF who owns the pfq
2194 static struct ice_vf *ice_get_vf_from_pfq(struct ice_pf *pf, u16 pfq)
2198 ice_for_each_vf(pf, vf_id) {
2199 struct ice_vf *vf = &pf->vf[vf_id];
2200 struct ice_vsi *vsi;
2203 vsi = ice_get_vf_vsi(vf);
2205 ice_for_each_rxq(vsi, rxq_idx)
2206 if (vsi->rxq_map[rxq_idx] == pfq)
2214 * ice_globalq_to_pfq - convert from global queue index to PF space queue index
2215 * @pf: PF used for conversion
2216 * @globalq: global queue index used to convert to PF space queue index
2218 static u32 ice_globalq_to_pfq(struct ice_pf *pf, u32 globalq)
2220 return globalq - pf->hw.func_caps.common_cap.rxq_first_id;
2224 * ice_vf_lan_overflow_event - handle LAN overflow event for a VF
2225 * @pf: PF that the LAN overflow event happened on
2226 * @event: structure holding the event information for the LAN overflow event
2228 * Determine if the LAN overflow event was caused by a VF queue. If it was not
2229 * caused by a VF, do nothing. If a VF caused this LAN overflow event trigger a
2230 * reset on the offending VF.
2233 ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event)
2235 u32 gldcb_rtctq, queue;
2238 gldcb_rtctq = le32_to_cpu(event->desc.params.lan_overflow.prtdcb_ruptq);
2239 dev_dbg(ice_pf_to_dev(pf), "GLDCB_RTCTQ: 0x%08x\n", gldcb_rtctq);
2241 /* event returns device global Rx queue number */
2242 queue = (gldcb_rtctq & GLDCB_RTCTQ_RXQNUM_M) >>
2243 GLDCB_RTCTQ_RXQNUM_S;
2245 vf = ice_get_vf_from_pfq(pf, ice_globalq_to_pfq(pf, queue));
2249 ice_vc_reset_vf(vf);
2253 * ice_vc_send_msg_to_vf - Send message to VF
2254 * @vf: pointer to the VF info
2255 * @v_opcode: virtual channel opcode
2256 * @v_retval: virtual channel return value
2257 * @msg: pointer to the msg buffer
2258 * @msglen: msg length
2263 ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
2264 enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
2266 enum ice_status aq_ret;
2274 if (ice_validate_vf_id(pf, vf->vf_id))
2277 dev = ice_pf_to_dev(pf);
2279 /* single place to detect unsuccessful return values */
2281 vf->num_inval_msgs++;
2282 dev_info(dev, "VF %d failed opcode %d, retval: %d\n", vf->vf_id,
2283 v_opcode, v_retval);
2284 if (vf->num_inval_msgs > ICE_DFLT_NUM_INVAL_MSGS_ALLOWED) {
2285 dev_err(dev, "Number of invalid messages exceeded for VF %d\n",
2287 dev_err(dev, "Use PF Control I/F to enable the VF\n");
2288 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
2292 vf->num_valid_msgs++;
2293 /* reset the invalid counter, if a valid message is received. */
2294 vf->num_inval_msgs = 0;
2297 aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
2299 if (aq_ret && pf->hw.mailboxq.sq_last_status != ICE_AQ_RC_ENOSYS) {
2300 dev_info(dev, "Unable to send the message to VF %d ret %s aq_err %s\n",
2301 vf->vf_id, ice_stat_str(aq_ret),
2302 ice_aq_str(pf->hw.mailboxq.sq_last_status));
2310 * ice_vc_get_ver_msg
2311 * @vf: pointer to the VF info
2312 * @msg: pointer to the msg buffer
2314 * called from the VF to request the API version used by the PF
2316 static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
2318 struct virtchnl_version_info info = {
2319 VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
2322 vf->vf_ver = *(struct virtchnl_version_info *)msg;
2323 /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
2324 if (VF_IS_V10(&vf->vf_ver))
2325 info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
2327 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
2328 VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
2329 sizeof(struct virtchnl_version_info));
2333 * ice_vc_get_max_frame_size - get max frame size allowed for VF
2334 * @vf: VF used to determine max frame size
2336 * Max frame size is determined based on the current port's max frame size and
2337 * whether a port VLAN is configured on this VF. The VF is not aware whether
2338 * it's in a port VLAN so the PF needs to account for this in max frame size
2339 * checks and sending the max frame size to the VF.
2341 static u16 ice_vc_get_max_frame_size(struct ice_vf *vf)
2343 struct ice_port_info *pi = ice_vf_get_port_info(vf);
2346 max_frame_size = pi->phy.link_info.max_frame_size;
2348 if (vf->port_vlan_info)
2349 max_frame_size -= VLAN_HLEN;
2351 return max_frame_size;
2355 * ice_vc_get_vf_res_msg
2356 * @vf: pointer to the VF info
2357 * @msg: pointer to the msg buffer
2359 * called from the VF to request its resources
2361 static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
2363 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2364 struct virtchnl_vf_resource *vfres = NULL;
2365 struct ice_pf *pf = vf->pf;
2366 struct ice_vsi *vsi;
2370 if (ice_check_vf_init(pf, vf)) {
2371 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2375 len = sizeof(struct virtchnl_vf_resource);
2377 vfres = kzalloc(len, GFP_KERNEL);
2379 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2383 if (VF_IS_V11(&vf->vf_ver))
2384 vf->driver_caps = *(u32 *)msg;
2386 vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
2387 VIRTCHNL_VF_OFFLOAD_RSS_REG |
2388 VIRTCHNL_VF_OFFLOAD_VLAN;
2390 vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
2391 vsi = ice_get_vf_vsi(vf);
2393 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2397 if (!vsi->info.pvid)
2398 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
2400 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2401 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
2403 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
2404 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
2406 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
2409 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_FDIR_PF)
2410 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_FDIR_PF;
2412 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
2413 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
2415 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
2416 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
2418 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
2419 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
2421 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
2422 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
2424 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2425 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
2427 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
2428 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
2430 if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
2431 vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
2433 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF)
2434 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF;
2436 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_USO)
2437 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_USO;
2439 vfres->num_vsis = 1;
2440 /* Tx and Rx queue are equal for VF */
2441 vfres->num_queue_pairs = vsi->num_txq;
2442 vfres->max_vectors = pf->num_msix_per_vf;
2443 vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
2444 vfres->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
2445 vfres->max_mtu = ice_vc_get_max_frame_size(vf);
2447 vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
2448 vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
2449 vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
2450 ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
2451 vf->hw_lan_addr.addr);
2453 /* match guest capabilities */
2454 vf->driver_caps = vfres->vf_cap_flags;
2456 ice_vc_set_caps_allowlist(vf);
2457 ice_vc_set_working_allowlist(vf);
2459 set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
2462 /* send the response back to the VF */
2463 ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
2471 * ice_vc_reset_vf_msg
2472 * @vf: pointer to the VF info
2474 * called from the VF to reset itself,
2475 * unlike other virtchnl messages, PF driver
2476 * doesn't send the response back to the VF
2478 static void ice_vc_reset_vf_msg(struct ice_vf *vf)
2480 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
2481 ice_reset_vf(vf, false);
2485 * ice_find_vsi_from_id
2486 * @pf: the PF structure to search for the VSI
2487 * @id: ID of the VSI it is searching for
2489 * searches for the VSI with the given ID
2491 static struct ice_vsi *ice_find_vsi_from_id(struct ice_pf *pf, u16 id)
2495 ice_for_each_vsi(pf, i)
2496 if (pf->vsi[i] && pf->vsi[i]->vsi_num == id)
2503 * ice_vc_isvalid_vsi_id
2504 * @vf: pointer to the VF info
2505 * @vsi_id: VF relative VSI ID
2507 * check for the valid VSI ID
2509 bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
2511 struct ice_pf *pf = vf->pf;
2512 struct ice_vsi *vsi;
2514 vsi = ice_find_vsi_from_id(pf, vsi_id);
2516 return (vsi && (vsi->vf_id == vf->vf_id));
2520 * ice_vc_isvalid_q_id
2521 * @vf: pointer to the VF info
2523 * @qid: VSI relative queue ID
2525 * check for the valid queue ID
2527 static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
2529 struct ice_vsi *vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
2530 /* allocated Tx and Rx queues should be always equal for VF VSI */
2531 return (vsi && (qid < vsi->alloc_txq));
2535 * ice_vc_isvalid_ring_len
2536 * @ring_len: length of ring
2538 * check for the valid ring count, should be multiple of ICE_REQ_DESC_MULTIPLE
2541 static bool ice_vc_isvalid_ring_len(u16 ring_len)
2543 return ring_len == 0 ||
2544 (ring_len >= ICE_MIN_NUM_DESC &&
2545 ring_len <= ICE_MAX_NUM_DESC &&
2546 !(ring_len % ICE_REQ_DESC_MULTIPLE));
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 if (!strncmp(hw->active_pkg_name, "ICE COMMS Package",
2573 sizeof(hw->active_pkg_name))) {
2574 hf_list = ice_vc_hash_field_list_comms;
2575 hf_list_len = ARRAY_SIZE(ice_vc_hash_field_list_comms);
2576 hdr_list = ice_vc_hdr_list_comms;
2577 hdr_list_len = ARRAY_SIZE(ice_vc_hdr_list_comms);
2579 hf_list = ice_vc_hash_field_list_os;
2580 hf_list_len = ARRAY_SIZE(ice_vc_hash_field_list_os);
2581 hdr_list = ice_vc_hdr_list_os;
2582 hdr_list_len = ARRAY_SIZE(ice_vc_hdr_list_os);
2585 for (i = 0; i < rss_cfg->proto_hdrs.count; i++) {
2586 struct virtchnl_proto_hdr *proto_hdr =
2587 &rss_cfg->proto_hdrs.proto_hdr[i];
2588 bool hdr_found = false;
2591 /* Find matched ice headers according to virtchnl headers. */
2592 for (j = 0; j < hdr_list_len; j++) {
2593 struct ice_vc_hdr_match_type hdr_map = hdr_list[j];
2595 if (proto_hdr->type == hdr_map.vc_hdr) {
2596 *addl_hdrs |= hdr_map.ice_hdr;
2604 /* Find matched ice hash fields according to
2605 * virtchnl hash fields.
2607 for (j = 0; j < hf_list_len; j++) {
2608 struct ice_vc_hash_field_match_type hf_map = hf_list[j];
2610 if (proto_hdr->type == hf_map.vc_hdr &&
2611 proto_hdr->field_selector == hf_map.vc_hash_field) {
2612 *hash_flds |= hf_map.ice_hash_field;
2622 * ice_vf_adv_rss_offload_ena - determine if capabilities support advanced
2624 * @caps: VF driver negotiated capabilities
2626 * Return true if VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF capability is set,
2629 static bool ice_vf_adv_rss_offload_ena(u32 caps)
2631 return !!(caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF);
2635 * ice_vc_handle_rss_cfg
2636 * @vf: pointer to the VF info
2637 * @msg: pointer to the message buffer
2638 * @add: add a RSS config if true, otherwise delete a RSS config
2640 * This function adds/deletes a RSS config
2642 static int ice_vc_handle_rss_cfg(struct ice_vf *vf, u8 *msg, bool add)
2644 u32 v_opcode = add ? VIRTCHNL_OP_ADD_RSS_CFG : VIRTCHNL_OP_DEL_RSS_CFG;
2645 struct virtchnl_rss_cfg *rss_cfg = (struct virtchnl_rss_cfg *)msg;
2646 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2647 struct device *dev = ice_pf_to_dev(vf->pf);
2648 struct ice_hw *hw = &vf->pf->hw;
2649 struct ice_vsi *vsi;
2651 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2652 dev_dbg(dev, "VF %d attempting to configure RSS, but RSS is not supported by the PF\n",
2654 v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
2658 if (!ice_vf_adv_rss_offload_ena(vf->driver_caps)) {
2659 dev_dbg(dev, "VF %d attempting to configure RSS, but Advanced RSS offload is not supported\n",
2661 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2665 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2666 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2670 if (rss_cfg->proto_hdrs.count > VIRTCHNL_MAX_NUM_PROTO_HDRS ||
2671 rss_cfg->rss_algorithm < VIRTCHNL_RSS_ALG_TOEPLITZ_ASYMMETRIC ||
2672 rss_cfg->rss_algorithm > VIRTCHNL_RSS_ALG_XOR_SYMMETRIC) {
2673 dev_dbg(dev, "VF %d attempting to configure RSS, but RSS configuration is not valid\n",
2675 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2679 vsi = ice_get_vf_vsi(vf);
2681 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2685 if (rss_cfg->rss_algorithm == VIRTCHNL_RSS_ALG_R_ASYMMETRIC) {
2686 struct ice_vsi_ctx *ctx;
2687 enum ice_status status;
2688 u8 lut_type, hash_type;
2690 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
2691 hash_type = add ? ICE_AQ_VSI_Q_OPT_RSS_XOR :
2692 ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
2694 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2696 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2700 ctx->info.q_opt_rss = ((lut_type <<
2701 ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
2702 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
2704 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
2706 /* Preserve existing queueing option setting */
2707 ctx->info.q_opt_rss |= (vsi->info.q_opt_rss &
2708 ICE_AQ_VSI_Q_OPT_RSS_GBL_LUT_M);
2709 ctx->info.q_opt_tc = vsi->info.q_opt_tc;
2710 ctx->info.q_opt_flags = vsi->info.q_opt_rss;
2712 ctx->info.valid_sections =
2713 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
2715 status = ice_update_vsi(hw, vsi->idx, ctx, NULL);
2717 dev_err(dev, "update VSI for RSS failed, err %s aq_err %s\n",
2718 ice_stat_str(status),
2719 ice_aq_str(hw->adminq.sq_last_status));
2720 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2722 vsi->info.q_opt_rss = ctx->info.q_opt_rss;
2727 u32 addl_hdrs = ICE_FLOW_SEG_HDR_NONE;
2728 u64 hash_flds = ICE_HASH_INVALID;
2730 if (!ice_vc_parse_rss_cfg(hw, rss_cfg, &addl_hdrs,
2732 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2737 if (ice_add_rss_cfg(hw, vsi->idx, hash_flds,
2739 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2740 dev_err(dev, "ice_add_rss_cfg failed for vsi = %d, v_ret = %d\n",
2741 vsi->vsi_num, v_ret);
2744 enum ice_status status;
2746 status = ice_rem_rss_cfg(hw, vsi->idx, hash_flds,
2748 /* We just ignore ICE_ERR_DOES_NOT_EXIST, because
2749 * if two configurations share the same profile remove
2750 * one of them actually removes both, since the
2751 * profile is deleted.
2753 if (status && status != ICE_ERR_DOES_NOT_EXIST) {
2754 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2755 dev_err(dev, "ice_rem_rss_cfg failed for VF ID:%d, error:%s\n",
2756 vf->vf_id, ice_stat_str(status));
2762 return ice_vc_send_msg_to_vf(vf, v_opcode, v_ret, NULL, 0);
2766 * ice_vc_config_rss_key
2767 * @vf: pointer to the VF info
2768 * @msg: pointer to the msg buffer
2770 * Configure the VF's RSS key
2772 static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
2774 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2775 struct virtchnl_rss_key *vrk =
2776 (struct virtchnl_rss_key *)msg;
2777 struct ice_vsi *vsi;
2779 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2780 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2784 if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
2785 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2789 if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
2790 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2794 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2795 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2799 vsi = ice_get_vf_vsi(vf);
2801 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2805 if (ice_set_rss_key(vsi, vrk->key))
2806 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2808 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
2813 * ice_vc_config_rss_lut
2814 * @vf: pointer to the VF info
2815 * @msg: pointer to the msg buffer
2817 * Configure the VF's RSS LUT
2819 static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
2821 struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
2822 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2823 struct ice_vsi *vsi;
2825 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2826 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2830 if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
2831 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2835 if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE) {
2836 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2840 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2841 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2845 vsi = ice_get_vf_vsi(vf);
2847 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2851 if (ice_set_rss_lut(vsi, vrl->lut, ICE_VSIQF_HLUT_ARRAY_SIZE))
2852 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2854 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
2859 * ice_wait_on_vf_reset - poll to make sure a given VF is ready after reset
2860 * @vf: The VF being resseting
2862 * The max poll time is about ~800ms, which is about the maximum time it takes
2863 * for a VF to be reset and/or a VF driver to be removed.
2865 static void ice_wait_on_vf_reset(struct ice_vf *vf)
2869 for (i = 0; i < ICE_MAX_VF_RESET_TRIES; i++) {
2870 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
2872 msleep(ICE_MAX_VF_RESET_SLEEP_MS);
2877 * ice_check_vf_ready_for_cfg - check if VF is ready to be configured/queried
2878 * @vf: VF to check if it's ready to be configured/queried
2880 * The purpose of this function is to make sure the VF is not in reset, not
2881 * disabled, and initialized so it can be configured and/or queried by a host
2884 int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
2888 ice_wait_on_vf_reset(vf);
2890 if (ice_is_vf_disabled(vf))
2894 if (ice_check_vf_init(pf, vf))
2901 * ice_set_vf_spoofchk
2902 * @netdev: network interface device structure
2903 * @vf_id: VF identifier
2904 * @ena: flag to enable or disable feature
2906 * Enable or disable VF spoof checking
2908 int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
2910 struct ice_netdev_priv *np = netdev_priv(netdev);
2911 struct ice_pf *pf = np->vsi->back;
2912 struct ice_vsi_ctx *ctx;
2913 struct ice_vsi *vf_vsi;
2914 enum ice_status status;
2919 dev = ice_pf_to_dev(pf);
2920 if (ice_validate_vf_id(pf, vf_id))
2923 vf = &pf->vf[vf_id];
2924 ret = ice_check_vf_ready_for_cfg(vf);
2928 vf_vsi = ice_get_vf_vsi(vf);
2930 netdev_err(netdev, "VSI %d for VF %d is null\n",
2931 vf->lan_vsi_idx, vf->vf_id);
2935 if (vf_vsi->type != ICE_VSI_VF) {
2936 netdev_err(netdev, "Type %d of VSI %d for VF %d is no ICE_VSI_VF\n",
2937 vf_vsi->type, vf_vsi->vsi_num, vf->vf_id);
2941 if (ena == vf->spoofchk) {
2942 dev_dbg(dev, "VF spoofchk already %s\n", ena ? "ON" : "OFF");
2946 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2950 ctx->info.sec_flags = vf_vsi->info.sec_flags;
2951 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
2953 ctx->info.sec_flags |=
2954 ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
2955 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2956 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
2958 ctx->info.sec_flags &=
2959 ~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
2960 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2961 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
2964 status = ice_update_vsi(&pf->hw, vf_vsi->idx, ctx, NULL);
2966 dev_err(dev, "Failed to %sable spoofchk on VF %d VSI %d\n error %s\n",
2967 ena ? "en" : "dis", vf->vf_id, vf_vsi->vsi_num,
2968 ice_stat_str(status));
2973 /* only update spoofchk state and VSI context on success */
2974 vf_vsi->info.sec_flags = ctx->info.sec_flags;
2983 * ice_is_any_vf_in_promisc - check if any VF(s) are in promiscuous mode
2984 * @pf: PF structure for accessing VF(s)
2986 * Return false if no VF(s) are in unicast and/or multicast promiscuous mode,
2989 bool ice_is_any_vf_in_promisc(struct ice_pf *pf)
2993 ice_for_each_vf(pf, vf_idx) {
2994 struct ice_vf *vf = &pf->vf[vf_idx];
2996 /* found a VF that has promiscuous mode configured */
2997 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2998 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
3006 * ice_vc_cfg_promiscuous_mode_msg
3007 * @vf: pointer to the VF info
3008 * @msg: pointer to the msg buffer
3010 * called from the VF to configure VF VSIs promiscuous mode
3012 static int ice_vc_cfg_promiscuous_mode_msg(struct ice_vf *vf, u8 *msg)
3014 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3015 bool rm_promisc, alluni = false, allmulti = false;
3016 struct virtchnl_promisc_info *info =
3017 (struct virtchnl_promisc_info *)msg;
3018 struct ice_pf *pf = vf->pf;
3019 struct ice_vsi *vsi;
3023 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3024 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3028 if (!ice_vc_isvalid_vsi_id(vf, info->vsi_id)) {
3029 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3033 vsi = ice_get_vf_vsi(vf);
3035 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3039 dev = ice_pf_to_dev(pf);
3040 if (!test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
3041 dev_err(dev, "Unprivileged VF %d is attempting to configure promiscuous mode\n",
3043 /* Leave v_ret alone, lie to the VF on purpose. */
3047 if (info->flags & FLAG_VF_UNICAST_PROMISC)
3050 if (info->flags & FLAG_VF_MULTICAST_PROMISC)
3053 rm_promisc = !allmulti && !alluni;
3055 if (vsi->num_vlan || vf->port_vlan_info) {
3056 struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
3057 struct net_device *pf_netdev;
3060 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3064 pf_netdev = pf_vsi->netdev;
3066 ret = ice_set_vf_spoofchk(pf_netdev, vf->vf_id, rm_promisc);
3068 dev_err(dev, "Failed to update spoofchk to %s for VF %d VSI %d when setting promiscuous mode\n",
3069 rm_promisc ? "ON" : "OFF", vf->vf_id,
3071 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3074 ret = ice_cfg_vlan_pruning(vsi, true, !rm_promisc);
3076 dev_err(dev, "Failed to configure VLAN pruning in promiscuous mode\n");
3077 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3082 if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
3083 bool set_dflt_vsi = alluni || allmulti;
3085 if (set_dflt_vsi && !ice_is_dflt_vsi_in_use(pf->first_sw))
3086 /* only attempt to set the default forwarding VSI if
3087 * it's not currently set
3089 ret = ice_set_dflt_vsi(pf->first_sw, vsi);
3090 else if (!set_dflt_vsi &&
3091 ice_is_vsi_dflt_vsi(pf->first_sw, vsi))
3092 /* only attempt to free the default forwarding VSI if we
3095 ret = ice_clear_dflt_vsi(pf->first_sw);
3098 dev_err(dev, "%sable VF %d as the default VSI failed, error %d\n",
3099 set_dflt_vsi ? "en" : "dis", vf->vf_id, ret);
3100 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
3104 enum ice_status status;
3108 if (vf->port_vlan_info || vsi->num_vlan)
3109 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
3111 promisc_m = ICE_UCAST_PROMISC_BITS;
3112 } else if (allmulti) {
3113 if (vf->port_vlan_info || vsi->num_vlan)
3114 promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
3116 promisc_m = ICE_MCAST_PROMISC_BITS;
3118 if (vf->port_vlan_info || vsi->num_vlan)
3119 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
3121 promisc_m = ICE_UCAST_PROMISC_BITS;
3124 /* Configure multicast/unicast with or without VLAN promiscuous
3127 status = ice_vf_set_vsi_promisc(vf, vsi, promisc_m, rm_promisc);
3129 dev_err(dev, "%sable Tx/Rx filter promiscuous mode on VF-%d failed, error: %s\n",
3130 rm_promisc ? "dis" : "en", vf->vf_id,
3131 ice_stat_str(status));
3132 v_ret = ice_err_to_virt_err(status);
3135 dev_dbg(dev, "%sable Tx/Rx filter promiscuous mode on VF-%d succeeded\n",
3136 rm_promisc ? "dis" : "en", vf->vf_id);
3141 !test_and_set_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
3142 dev_info(dev, "VF %u successfully set multicast promiscuous mode\n", vf->vf_id);
3143 else if (!allmulti && test_and_clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
3144 dev_info(dev, "VF %u successfully unset multicast promiscuous mode\n", vf->vf_id);
3146 if (alluni && !test_and_set_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
3147 dev_info(dev, "VF %u successfully set unicast promiscuous mode\n", vf->vf_id);
3148 else if (!alluni && test_and_clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
3149 dev_info(dev, "VF %u successfully unset unicast promiscuous mode\n", vf->vf_id);
3152 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
3157 * ice_vc_get_stats_msg
3158 * @vf: pointer to the VF info
3159 * @msg: pointer to the msg buffer
3161 * called from the VF to get VSI stats
3163 static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
3165 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3166 struct virtchnl_queue_select *vqs =
3167 (struct virtchnl_queue_select *)msg;
3168 struct ice_eth_stats stats = { 0 };
3169 struct ice_vsi *vsi;
3171 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3172 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3176 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3177 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3181 vsi = ice_get_vf_vsi(vf);
3183 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3187 ice_update_eth_stats(vsi);
3189 stats = vsi->eth_stats;
3192 /* send the response to the VF */
3193 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
3194 (u8 *)&stats, sizeof(stats));
3198 * ice_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTCHNL
3199 * @vqs: virtchnl_queue_select structure containing bitmaps to validate
3201 * Return true on successful validation, else false
3203 static bool ice_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
3205 if ((!vqs->rx_queues && !vqs->tx_queues) ||
3206 vqs->rx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF) ||
3207 vqs->tx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF))
3214 * ice_vf_ena_txq_interrupt - enable Tx queue interrupt via QINT_TQCTL
3215 * @vsi: VSI of the VF to configure
3216 * @q_idx: VF queue index used to determine the queue in the PF's space
3218 static void ice_vf_ena_txq_interrupt(struct ice_vsi *vsi, u32 q_idx)
3220 struct ice_hw *hw = &vsi->back->hw;
3221 u32 pfq = vsi->txq_map[q_idx];
3224 reg = rd32(hw, QINT_TQCTL(pfq));
3226 /* MSI-X index 0 in the VF's space is always for the OICR, which means
3227 * this is most likely a poll mode VF driver, so don't enable an
3228 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
3230 if (!(reg & QINT_TQCTL_MSIX_INDX_M))
3233 wr32(hw, QINT_TQCTL(pfq), reg | QINT_TQCTL_CAUSE_ENA_M);
3237 * ice_vf_ena_rxq_interrupt - enable Tx queue interrupt via QINT_RQCTL
3238 * @vsi: VSI of the VF to configure
3239 * @q_idx: VF queue index used to determine the queue in the PF's space
3241 static void ice_vf_ena_rxq_interrupt(struct ice_vsi *vsi, u32 q_idx)
3243 struct ice_hw *hw = &vsi->back->hw;
3244 u32 pfq = vsi->rxq_map[q_idx];
3247 reg = rd32(hw, QINT_RQCTL(pfq));
3249 /* MSI-X index 0 in the VF's space is always for the OICR, which means
3250 * this is most likely a poll mode VF driver, so don't enable an
3251 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
3253 if (!(reg & QINT_RQCTL_MSIX_INDX_M))
3256 wr32(hw, QINT_RQCTL(pfq), reg | QINT_RQCTL_CAUSE_ENA_M);
3261 * @vf: pointer to the VF info
3262 * @msg: pointer to the msg buffer
3264 * called from the VF to enable all or specific queue(s)
3266 static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
3268 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3269 struct virtchnl_queue_select *vqs =
3270 (struct virtchnl_queue_select *)msg;
3271 struct ice_vsi *vsi;
3272 unsigned long q_map;
3275 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3276 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3280 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3281 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3285 if (!ice_vc_validate_vqs_bitmaps(vqs)) {
3286 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3290 vsi = ice_get_vf_vsi(vf);
3292 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3296 /* Enable only Rx rings, Tx rings were enabled by the FW when the
3297 * Tx queue group list was configured and the context bits were
3298 * programmed using ice_vsi_cfg_txqs
3300 q_map = vqs->rx_queues;
3301 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3302 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3303 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3307 /* Skip queue if enabled */
3308 if (test_bit(vf_q_id, vf->rxq_ena))
3311 if (ice_vsi_ctrl_one_rx_ring(vsi, true, vf_q_id, true)) {
3312 dev_err(ice_pf_to_dev(vsi->back), "Failed to enable Rx ring %d on VSI %d\n",
3313 vf_q_id, vsi->vsi_num);
3314 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3318 ice_vf_ena_rxq_interrupt(vsi, vf_q_id);
3319 set_bit(vf_q_id, vf->rxq_ena);
3322 q_map = vqs->tx_queues;
3323 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3324 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3325 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3329 /* Skip queue if enabled */
3330 if (test_bit(vf_q_id, vf->txq_ena))
3333 ice_vf_ena_txq_interrupt(vsi, vf_q_id);
3334 set_bit(vf_q_id, vf->txq_ena);
3337 /* Set flag to indicate that queues are enabled */
3338 if (v_ret == VIRTCHNL_STATUS_SUCCESS)
3339 set_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
3342 /* send the response to the VF */
3343 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
3349 * @vf: pointer to the VF info
3350 * @msg: pointer to the msg buffer
3352 * called from the VF to disable all or specific
3355 static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
3357 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3358 struct virtchnl_queue_select *vqs =
3359 (struct virtchnl_queue_select *)msg;
3360 struct ice_vsi *vsi;
3361 unsigned long q_map;
3364 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
3365 !test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states)) {
3366 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3370 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3371 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3375 if (!ice_vc_validate_vqs_bitmaps(vqs)) {
3376 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3380 vsi = ice_get_vf_vsi(vf);
3382 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3386 if (vqs->tx_queues) {
3387 q_map = vqs->tx_queues;
3389 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3390 struct ice_tx_ring *ring = vsi->tx_rings[vf_q_id];
3391 struct ice_txq_meta txq_meta = { 0 };
3393 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3394 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3398 /* Skip queue if not enabled */
3399 if (!test_bit(vf_q_id, vf->txq_ena))
3402 ice_fill_txq_meta(vsi, ring, &txq_meta);
3404 if (ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, vf->vf_id,
3406 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Tx ring %d on VSI %d\n",
3407 vf_q_id, vsi->vsi_num);
3408 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3412 /* Clear enabled queues flag */
3413 clear_bit(vf_q_id, vf->txq_ena);
3417 q_map = vqs->rx_queues;
3418 /* speed up Rx queue disable by batching them if possible */
3420 bitmap_equal(&q_map, vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF)) {
3421 if (ice_vsi_stop_all_rx_rings(vsi)) {
3422 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop all Rx rings on VSI %d\n",
3424 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3428 bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
3430 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3431 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3432 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3436 /* Skip queue if not enabled */
3437 if (!test_bit(vf_q_id, vf->rxq_ena))
3440 if (ice_vsi_ctrl_one_rx_ring(vsi, false, vf_q_id,
3442 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Rx ring %d on VSI %d\n",
3443 vf_q_id, vsi->vsi_num);
3444 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3448 /* Clear enabled queues flag */
3449 clear_bit(vf_q_id, vf->rxq_ena);
3453 /* Clear enabled queues flag */
3454 if (v_ret == VIRTCHNL_STATUS_SUCCESS && ice_vf_has_no_qs_ena(vf))
3455 clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
3458 /* send the response to the VF */
3459 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
3465 * @vf: pointer to the VF info
3466 * @vsi: the VSI being configured
3467 * @vector_id: vector ID
3468 * @map: vector map for mapping vectors to queues
3469 * @q_vector: structure for interrupt vector
3470 * configure the IRQ to queue map
3473 ice_cfg_interrupt(struct ice_vf *vf, struct ice_vsi *vsi, u16 vector_id,
3474 struct virtchnl_vector_map *map,
3475 struct ice_q_vector *q_vector)
3477 u16 vsi_q_id, vsi_q_id_idx;
3480 q_vector->num_ring_rx = 0;
3481 q_vector->num_ring_tx = 0;
3483 qmap = map->rxq_map;
3484 for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
3485 vsi_q_id = vsi_q_id_idx;
3487 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
3488 return VIRTCHNL_STATUS_ERR_PARAM;
3490 q_vector->num_ring_rx++;
3491 q_vector->rx.itr_idx = map->rxitr_idx;
3492 vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
3493 ice_cfg_rxq_interrupt(vsi, vsi_q_id, vector_id,
3494 q_vector->rx.itr_idx);
3497 qmap = map->txq_map;
3498 for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
3499 vsi_q_id = vsi_q_id_idx;
3501 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
3502 return VIRTCHNL_STATUS_ERR_PARAM;
3504 q_vector->num_ring_tx++;
3505 q_vector->tx.itr_idx = map->txitr_idx;
3506 vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
3507 ice_cfg_txq_interrupt(vsi, vsi_q_id, vector_id,
3508 q_vector->tx.itr_idx);
3511 return VIRTCHNL_STATUS_SUCCESS;
3515 * ice_vc_cfg_irq_map_msg
3516 * @vf: pointer to the VF info
3517 * @msg: pointer to the msg buffer
3519 * called from the VF to configure the IRQ to queue map
3521 static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
3523 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3524 u16 num_q_vectors_mapped, vsi_id, vector_id;
3525 struct virtchnl_irq_map_info *irqmap_info;
3526 struct virtchnl_vector_map *map;
3527 struct ice_pf *pf = vf->pf;
3528 struct ice_vsi *vsi;
3531 irqmap_info = (struct virtchnl_irq_map_info *)msg;
3532 num_q_vectors_mapped = irqmap_info->num_vectors;
3534 /* Check to make sure number of VF vectors mapped is not greater than
3535 * number of VF vectors originally allocated, and check that
3536 * there is actually at least a single VF queue vector mapped
3538 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
3539 pf->num_msix_per_vf < num_q_vectors_mapped ||
3540 !num_q_vectors_mapped) {
3541 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3545 vsi = ice_get_vf_vsi(vf);
3547 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3551 for (i = 0; i < num_q_vectors_mapped; i++) {
3552 struct ice_q_vector *q_vector;
3554 map = &irqmap_info->vecmap[i];
3556 vector_id = map->vector_id;
3557 vsi_id = map->vsi_id;
3558 /* vector_id is always 0-based for each VF, and can never be
3559 * larger than or equal to the max allowed interrupts per VF
3561 if (!(vector_id < pf->num_msix_per_vf) ||
3562 !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
3563 (!vector_id && (map->rxq_map || map->txq_map))) {
3564 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3568 /* No need to map VF miscellaneous or rogue vector */
3572 /* Subtract non queue vector from vector_id passed by VF
3573 * to get actual number of VSI queue vector array index
3575 q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
3577 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3581 /* lookout for the invalid queue index */
3582 v_ret = (enum virtchnl_status_code)
3583 ice_cfg_interrupt(vf, vsi, vector_id, map, q_vector);
3589 /* send the response to the VF */
3590 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
3596 * @vf: pointer to the VF info
3597 * @msg: pointer to the msg buffer
3599 * called from the VF to configure the Rx/Tx queues
3601 static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
3603 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3604 struct virtchnl_vsi_queue_config_info *qci =
3605 (struct virtchnl_vsi_queue_config_info *)msg;
3606 struct virtchnl_queue_pair_info *qpi;
3607 struct ice_pf *pf = vf->pf;
3608 struct ice_vsi *vsi;
3611 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3612 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3616 if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
3617 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3621 vsi = ice_get_vf_vsi(vf);
3623 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3627 if (qci->num_queue_pairs > ICE_MAX_RSS_QS_PER_VF ||
3628 qci->num_queue_pairs > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
3629 dev_err(ice_pf_to_dev(pf), "VF-%d requesting more than supported number of queues: %d\n",
3630 vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
3631 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3635 for (i = 0; i < qci->num_queue_pairs; i++) {
3636 qpi = &qci->qpair[i];
3637 if (qpi->txq.vsi_id != qci->vsi_id ||
3638 qpi->rxq.vsi_id != qci->vsi_id ||
3639 qpi->rxq.queue_id != qpi->txq.queue_id ||
3640 qpi->txq.headwb_enabled ||
3641 !ice_vc_isvalid_ring_len(qpi->txq.ring_len) ||
3642 !ice_vc_isvalid_ring_len(qpi->rxq.ring_len) ||
3643 !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
3644 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3648 q_idx = qpi->rxq.queue_id;
3650 /* make sure selected "q_idx" is in valid range of queues
3651 * for selected "vsi"
3653 if (q_idx >= vsi->alloc_txq || q_idx >= vsi->alloc_rxq) {
3654 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3658 /* copy Tx queue info from VF into VSI */
3659 if (qpi->txq.ring_len > 0) {
3660 vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
3661 vsi->tx_rings[i]->count = qpi->txq.ring_len;
3662 if (ice_vsi_cfg_single_txq(vsi, vsi->tx_rings, q_idx)) {
3663 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3668 /* copy Rx queue info from VF into VSI */
3669 if (qpi->rxq.ring_len > 0) {
3670 u16 max_frame_size = ice_vc_get_max_frame_size(vf);
3672 vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
3673 vsi->rx_rings[i]->count = qpi->rxq.ring_len;
3675 if (qpi->rxq.databuffer_size != 0 &&
3676 (qpi->rxq.databuffer_size > ((16 * 1024) - 128) ||
3677 qpi->rxq.databuffer_size < 1024)) {
3678 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3681 vsi->rx_buf_len = qpi->rxq.databuffer_size;
3682 vsi->rx_rings[i]->rx_buf_len = vsi->rx_buf_len;
3683 if (qpi->rxq.max_pkt_size > max_frame_size ||
3684 qpi->rxq.max_pkt_size < 64) {
3685 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3689 vsi->max_frame = qpi->rxq.max_pkt_size;
3690 /* add space for the port VLAN since the VF driver is not
3691 * expected to account for it in the MTU calculation
3693 if (vf->port_vlan_info)
3694 vsi->max_frame += VLAN_HLEN;
3696 if (ice_vsi_cfg_single_rxq(vsi, q_idx)) {
3697 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3704 /* send the response to the VF */
3705 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES, v_ret,
3711 * @vf: pointer to the VF info
3713 static bool ice_is_vf_trusted(struct ice_vf *vf)
3715 return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
3719 * ice_can_vf_change_mac
3720 * @vf: pointer to the VF info
3722 * Return true if the VF is allowed to change its MAC filters, false otherwise
3724 static bool ice_can_vf_change_mac(struct ice_vf *vf)
3726 /* If the VF MAC address has been set administratively (via the
3727 * ndo_set_vf_mac command), then deny permission to the VF to
3728 * add/delete unicast MAC addresses, unless the VF is trusted
3730 if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
3737 * ice_vc_ether_addr_type - get type of virtchnl_ether_addr
3738 * @vc_ether_addr: used to extract the type
3741 ice_vc_ether_addr_type(struct virtchnl_ether_addr *vc_ether_addr)
3743 return (vc_ether_addr->type & VIRTCHNL_ETHER_ADDR_TYPE_MASK);
3747 * ice_is_vc_addr_legacy - check if the MAC address is from an older VF
3748 * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
3751 ice_is_vc_addr_legacy(struct virtchnl_ether_addr *vc_ether_addr)
3753 u8 type = ice_vc_ether_addr_type(vc_ether_addr);
3755 return (type == VIRTCHNL_ETHER_ADDR_LEGACY);
3759 * ice_is_vc_addr_primary - check if the MAC address is the VF's primary MAC
3760 * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
3762 * This function should only be called when the MAC address in
3763 * virtchnl_ether_addr is a valid unicast MAC
3766 ice_is_vc_addr_primary(struct virtchnl_ether_addr __maybe_unused *vc_ether_addr)
3768 u8 type = ice_vc_ether_addr_type(vc_ether_addr);
3770 return (type == VIRTCHNL_ETHER_ADDR_PRIMARY);
3774 * ice_vfhw_mac_add - update the VF's cached hardware MAC if allowed
3776 * @vc_ether_addr: structure from VIRTCHNL with MAC to add
3779 ice_vfhw_mac_add(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
3781 u8 *mac_addr = vc_ether_addr->addr;
3783 if (!is_valid_ether_addr(mac_addr))
3786 /* only allow legacy VF drivers to set the device and hardware MAC if it
3787 * is zero and allow new VF drivers to set the hardware MAC if the type
3788 * was correctly specified over VIRTCHNL
3790 if ((ice_is_vc_addr_legacy(vc_ether_addr) &&
3791 is_zero_ether_addr(vf->hw_lan_addr.addr)) ||
3792 ice_is_vc_addr_primary(vc_ether_addr)) {
3793 ether_addr_copy(vf->dev_lan_addr.addr, mac_addr);
3794 ether_addr_copy(vf->hw_lan_addr.addr, mac_addr);
3797 /* hardware and device MACs are already set, but its possible that the
3798 * VF driver sent the VIRTCHNL_OP_ADD_ETH_ADDR message before the
3799 * VIRTCHNL_OP_DEL_ETH_ADDR when trying to update its MAC, so save it
3800 * away for the legacy VF driver case as it will be updated in the
3801 * delete flow for this case
3803 if (ice_is_vc_addr_legacy(vc_ether_addr)) {
3804 ether_addr_copy(vf->legacy_last_added_umac.addr,
3806 vf->legacy_last_added_umac.time_modified = jiffies;
3811 * ice_vc_add_mac_addr - attempt to add the MAC address passed in
3812 * @vf: pointer to the VF info
3813 * @vsi: pointer to the VF's VSI
3814 * @vc_ether_addr: VIRTCHNL MAC address structure used to add MAC
3817 ice_vc_add_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
3818 struct virtchnl_ether_addr *vc_ether_addr)
3820 struct device *dev = ice_pf_to_dev(vf->pf);
3821 u8 *mac_addr = vc_ether_addr->addr;
3822 enum ice_status status;
3824 /* device MAC already added */
3825 if (ether_addr_equal(mac_addr, vf->dev_lan_addr.addr))
3828 if (is_unicast_ether_addr(mac_addr) && !ice_can_vf_change_mac(vf)) {
3829 dev_err(dev, "VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
3833 status = ice_fltr_add_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
3834 if (status == ICE_ERR_ALREADY_EXISTS) {
3835 dev_err(dev, "MAC %pM already exists for VF %d\n", mac_addr,
3838 } else if (status) {
3839 dev_err(dev, "Failed to add MAC %pM for VF %d\n, error %s\n",
3840 mac_addr, vf->vf_id, ice_stat_str(status));
3844 ice_vfhw_mac_add(vf, vc_ether_addr);
3852 * ice_is_legacy_umac_expired - check if last added legacy unicast MAC expired
3853 * @last_added_umac: structure used to check expiration
3855 static bool ice_is_legacy_umac_expired(struct ice_time_mac *last_added_umac)
3857 #define ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME msecs_to_jiffies(3000)
3858 return time_is_before_jiffies(last_added_umac->time_modified +
3859 ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME);
3863 * ice_update_legacy_cached_mac - update cached hardware MAC for legacy VF
3865 * @vc_ether_addr: structure from VIRTCHNL with MAC to check
3867 * only update cached hardware MAC for legacy VF drivers on delete
3868 * because we cannot guarantee order/type of MAC from the VF driver
3871 ice_update_legacy_cached_mac(struct ice_vf *vf,
3872 struct virtchnl_ether_addr *vc_ether_addr)
3874 if (!ice_is_vc_addr_legacy(vc_ether_addr) ||
3875 ice_is_legacy_umac_expired(&vf->legacy_last_added_umac))
3878 ether_addr_copy(vf->dev_lan_addr.addr, vf->legacy_last_added_umac.addr);
3879 ether_addr_copy(vf->hw_lan_addr.addr, vf->legacy_last_added_umac.addr);
3883 * ice_vfhw_mac_del - update the VF's cached hardware MAC if allowed
3885 * @vc_ether_addr: structure from VIRTCHNL with MAC to delete
3888 ice_vfhw_mac_del(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
3890 u8 *mac_addr = vc_ether_addr->addr;
3892 if (!is_valid_ether_addr(mac_addr) ||
3893 !ether_addr_equal(vf->dev_lan_addr.addr, mac_addr))
3896 /* allow the device MAC to be repopulated in the add flow and don't
3897 * clear the hardware MAC (i.e. hw_lan_addr.addr) here as that is meant
3898 * to be persistent on VM reboot and across driver unload/load, which
3899 * won't work if we clear the hardware MAC here
3901 eth_zero_addr(vf->dev_lan_addr.addr);
3903 ice_update_legacy_cached_mac(vf, vc_ether_addr);
3907 * ice_vc_del_mac_addr - attempt to delete the MAC address passed in
3908 * @vf: pointer to the VF info
3909 * @vsi: pointer to the VF's VSI
3910 * @vc_ether_addr: VIRTCHNL MAC address structure used to delete MAC
3913 ice_vc_del_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
3914 struct virtchnl_ether_addr *vc_ether_addr)
3916 struct device *dev = ice_pf_to_dev(vf->pf);
3917 u8 *mac_addr = vc_ether_addr->addr;
3918 enum ice_status status;
3920 if (!ice_can_vf_change_mac(vf) &&
3921 ether_addr_equal(vf->dev_lan_addr.addr, mac_addr))
3924 status = ice_fltr_remove_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
3925 if (status == ICE_ERR_DOES_NOT_EXIST) {
3926 dev_err(dev, "MAC %pM does not exist for VF %d\n", mac_addr,
3929 } else if (status) {
3930 dev_err(dev, "Failed to delete MAC %pM for VF %d, error %s\n",
3931 mac_addr, vf->vf_id, ice_stat_str(status));
3935 ice_vfhw_mac_del(vf, vc_ether_addr);
3943 * ice_vc_handle_mac_addr_msg
3944 * @vf: pointer to the VF info
3945 * @msg: pointer to the msg buffer
3946 * @set: true if MAC filters are being set, false otherwise
3948 * add guest MAC address filter
3951 ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
3953 int (*ice_vc_cfg_mac)
3954 (struct ice_vf *vf, struct ice_vsi *vsi,
3955 struct virtchnl_ether_addr *virtchnl_ether_addr);
3956 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3957 struct virtchnl_ether_addr_list *al =
3958 (struct virtchnl_ether_addr_list *)msg;
3959 struct ice_pf *pf = vf->pf;
3960 enum virtchnl_ops vc_op;
3961 struct ice_vsi *vsi;
3965 vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
3966 ice_vc_cfg_mac = ice_vc_add_mac_addr;
3968 vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
3969 ice_vc_cfg_mac = ice_vc_del_mac_addr;
3972 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
3973 !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
3974 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3975 goto handle_mac_exit;
3978 /* If this VF is not privileged, then we can't add more than a
3979 * limited number of addresses. Check to make sure that the
3980 * additions do not push us over the limit.
3982 if (set && !ice_is_vf_trusted(vf) &&
3983 (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
3984 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",
3986 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3987 goto handle_mac_exit;
3990 vsi = ice_get_vf_vsi(vf);
3992 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3993 goto handle_mac_exit;
3996 for (i = 0; i < al->num_elements; i++) {
3997 u8 *mac_addr = al->list[i].addr;
4000 if (is_broadcast_ether_addr(mac_addr) ||
4001 is_zero_ether_addr(mac_addr))
4004 result = ice_vc_cfg_mac(vf, vsi, &al->list[i]);
4005 if (result == -EEXIST || result == -ENOENT) {
4007 } else if (result) {
4008 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
4009 goto handle_mac_exit;
4014 /* send the response to the VF */
4015 return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
4019 * ice_vc_add_mac_addr_msg
4020 * @vf: pointer to the VF info
4021 * @msg: pointer to the msg buffer
4023 * add guest MAC address filter
4025 static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
4027 return ice_vc_handle_mac_addr_msg(vf, msg, true);
4031 * ice_vc_del_mac_addr_msg
4032 * @vf: pointer to the VF info
4033 * @msg: pointer to the msg buffer
4035 * remove guest MAC address filter
4037 static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
4039 return ice_vc_handle_mac_addr_msg(vf, msg, false);
4043 * ice_vc_request_qs_msg
4044 * @vf: pointer to the VF info
4045 * @msg: pointer to the msg buffer
4047 * VFs get a default number of queues but can use this message to request a
4048 * different number. If the request is successful, PF will reset the VF and
4049 * return 0. If unsuccessful, PF will send message informing VF of number of
4050 * available queue pairs via virtchnl message response to VF.
4052 static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
4054 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4055 struct virtchnl_vf_res_request *vfres =
4056 (struct virtchnl_vf_res_request *)msg;
4057 u16 req_queues = vfres->num_queue_pairs;
4058 struct ice_pf *pf = vf->pf;
4059 u16 max_allowed_vf_queues;
4060 u16 tx_rx_queue_left;
4064 dev = ice_pf_to_dev(pf);
4065 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4066 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4070 cur_queues = vf->num_vf_qs;
4071 tx_rx_queue_left = min_t(u16, ice_get_avail_txq_count(pf),
4072 ice_get_avail_rxq_count(pf));
4073 max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
4075 dev_err(dev, "VF %d tried to request 0 queues. Ignoring.\n",
4077 } else if (req_queues > ICE_MAX_RSS_QS_PER_VF) {
4078 dev_err(dev, "VF %d tried to request more than %d queues.\n",
4079 vf->vf_id, ICE_MAX_RSS_QS_PER_VF);
4080 vfres->num_queue_pairs = ICE_MAX_RSS_QS_PER_VF;
4081 } else if (req_queues > cur_queues &&
4082 req_queues - cur_queues > tx_rx_queue_left) {
4083 dev_warn(dev, "VF %d requested %u more queues, but only %u left.\n",
4084 vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
4085 vfres->num_queue_pairs = min_t(u16, max_allowed_vf_queues,
4086 ICE_MAX_RSS_QS_PER_VF);
4088 /* request is successful, then reset VF */
4089 vf->num_req_qs = req_queues;
4090 ice_vc_reset_vf(vf);
4091 dev_info(dev, "VF %d granted request of %u queues.\n",
4092 vf->vf_id, req_queues);
4097 /* send the response to the VF */
4098 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
4099 v_ret, (u8 *)vfres, sizeof(*vfres));
4103 * ice_set_vf_port_vlan
4104 * @netdev: network interface device structure
4105 * @vf_id: VF identifier
4106 * @vlan_id: VLAN ID being set
4107 * @qos: priority setting
4108 * @vlan_proto: VLAN protocol
4110 * program VF Port VLAN ID and/or QoS
4113 ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
4116 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4122 dev = ice_pf_to_dev(pf);
4123 if (ice_validate_vf_id(pf, vf_id))
4126 if (vlan_id >= VLAN_N_VID || qos > 7) {
4127 dev_err(dev, "Invalid Port VLAN parameters for VF %d, ID %d, QoS %d\n",
4128 vf_id, vlan_id, qos);
4132 if (vlan_proto != htons(ETH_P_8021Q)) {
4133 dev_err(dev, "VF VLAN protocol is not supported\n");
4134 return -EPROTONOSUPPORT;
4137 vf = &pf->vf[vf_id];
4138 ret = ice_check_vf_ready_for_cfg(vf);
4142 vlanprio = vlan_id | (qos << VLAN_PRIO_SHIFT);
4144 if (vf->port_vlan_info == vlanprio) {
4145 /* duplicate request, so just return success */
4146 dev_dbg(dev, "Duplicate pvid %d request\n", vlanprio);
4150 vf->port_vlan_info = vlanprio;
4152 if (vf->port_vlan_info)
4153 dev_info(dev, "Setting VLAN %d, QoS 0x%x on VF %d\n",
4154 vlan_id, qos, vf_id);
4156 dev_info(dev, "Clearing port VLAN on VF %d\n", vf_id);
4158 ice_vc_reset_vf(vf);
4164 * ice_vf_vlan_offload_ena - determine if capabilities support VLAN offloads
4165 * @caps: VF driver negotiated capabilities
4167 * Return true if VIRTCHNL_VF_OFFLOAD_VLAN capability is set, else return false
4169 static bool ice_vf_vlan_offload_ena(u32 caps)
4171 return !!(caps & VIRTCHNL_VF_OFFLOAD_VLAN);
4175 * ice_vc_process_vlan_msg
4176 * @vf: pointer to the VF info
4177 * @msg: pointer to the msg buffer
4178 * @add_v: Add VLAN if true, otherwise delete VLAN
4180 * Process virtchnl op to add or remove programmed guest VLAN ID
4182 static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
4184 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4185 struct virtchnl_vlan_filter_list *vfl =
4186 (struct virtchnl_vlan_filter_list *)msg;
4187 struct ice_pf *pf = vf->pf;
4188 bool vlan_promisc = false;
4189 struct ice_vsi *vsi;
4196 dev = ice_pf_to_dev(pf);
4197 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4198 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4202 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
4203 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4207 if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
4208 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4212 for (i = 0; i < vfl->num_elements; i++) {
4213 if (vfl->vlan_id[i] >= VLAN_N_VID) {
4214 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4215 dev_err(dev, "invalid VF VLAN id %d\n",
4222 vsi = ice_get_vf_vsi(vf);
4224 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4228 if (add_v && !ice_is_vf_trusted(vf) &&
4229 vsi->num_vlan >= ICE_MAX_VLAN_PER_VF) {
4230 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
4232 /* There is no need to let VF know about being not trusted,
4233 * so we can just return success message here
4238 if (vsi->info.pvid) {
4239 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4243 if ((test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
4244 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) &&
4245 test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags))
4246 vlan_promisc = true;
4249 for (i = 0; i < vfl->num_elements; i++) {
4250 u16 vid = vfl->vlan_id[i];
4252 if (!ice_is_vf_trusted(vf) &&
4253 vsi->num_vlan >= ICE_MAX_VLAN_PER_VF) {
4254 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
4256 /* There is no need to let VF know about being
4257 * not trusted, so we can just return success
4258 * message here as well.
4263 /* we add VLAN 0 by default for each VF so we can enable
4264 * Tx VLAN anti-spoof without triggering MDD events so
4265 * we don't need to add it again here
4270 status = ice_vsi_add_vlan(vsi, vid, ICE_FWD_TO_VSI);
4272 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4276 /* Enable VLAN pruning when non-zero VLAN is added */
4277 if (!vlan_promisc && vid &&
4278 !ice_vsi_is_vlan_pruning_ena(vsi)) {
4279 status = ice_cfg_vlan_pruning(vsi, true, false);
4281 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4282 dev_err(dev, "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
4286 } else if (vlan_promisc) {
4287 /* Enable Ucast/Mcast VLAN promiscuous mode */
4288 promisc_m = ICE_PROMISC_VLAN_TX |
4289 ICE_PROMISC_VLAN_RX;
4291 status = ice_set_vsi_promisc(hw, vsi->idx,
4294 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4295 dev_err(dev, "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
4301 /* In case of non_trusted VF, number of VLAN elements passed
4302 * to PF for removal might be greater than number of VLANs
4303 * filter programmed for that VF - So, use actual number of
4304 * VLANS added earlier with add VLAN opcode. In order to avoid
4305 * removing VLAN that doesn't exist, which result to sending
4306 * erroneous failed message back to the VF
4310 num_vf_vlan = vsi->num_vlan;
4311 for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
4312 u16 vid = vfl->vlan_id[i];
4314 /* we add VLAN 0 by default for each VF so we can enable
4315 * Tx VLAN anti-spoof without triggering MDD events so
4316 * we don't want a VIRTCHNL request to remove it
4321 /* Make sure ice_vsi_kill_vlan is successful before
4322 * updating VLAN information
4324 status = ice_vsi_kill_vlan(vsi, vid);
4326 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4330 /* Disable VLAN pruning when only VLAN 0 is left */
4331 if (vsi->num_vlan == 1 &&
4332 ice_vsi_is_vlan_pruning_ena(vsi))
4333 ice_cfg_vlan_pruning(vsi, false, false);
4335 /* Disable Unicast/Multicast VLAN promiscuous mode */
4337 promisc_m = ICE_PROMISC_VLAN_TX |
4338 ICE_PROMISC_VLAN_RX;
4340 ice_clear_vsi_promisc(hw, vsi->idx,
4347 /* send the response to the VF */
4349 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
4352 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
4357 * ice_vc_add_vlan_msg
4358 * @vf: pointer to the VF info
4359 * @msg: pointer to the msg buffer
4361 * Add and program guest VLAN ID
4363 static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
4365 return ice_vc_process_vlan_msg(vf, msg, true);
4369 * ice_vc_remove_vlan_msg
4370 * @vf: pointer to the VF info
4371 * @msg: pointer to the msg buffer
4373 * remove programmed guest VLAN ID
4375 static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
4377 return ice_vc_process_vlan_msg(vf, msg, false);
4381 * ice_vc_ena_vlan_stripping
4382 * @vf: pointer to the VF info
4384 * Enable VLAN header stripping for a given VF
4386 static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
4388 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4389 struct ice_vsi *vsi;
4391 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4392 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4396 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
4397 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4401 vsi = ice_get_vf_vsi(vf);
4402 if (ice_vsi_manage_vlan_stripping(vsi, true))
4403 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4406 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
4411 * ice_vc_dis_vlan_stripping
4412 * @vf: pointer to the VF info
4414 * Disable VLAN header stripping for a given VF
4416 static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
4418 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4419 struct ice_vsi *vsi;
4421 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4422 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4426 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
4427 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4431 vsi = ice_get_vf_vsi(vf);
4433 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4437 if (ice_vsi_manage_vlan_stripping(vsi, false))
4438 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4441 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
4446 * ice_vf_init_vlan_stripping - enable/disable VLAN stripping on initialization
4447 * @vf: VF to enable/disable VLAN stripping for on initialization
4449 * If the VIRTCHNL_VF_OFFLOAD_VLAN flag is set enable VLAN stripping, else if
4450 * the flag is cleared then we want to disable stripping. For example, the flag
4451 * will be cleared when port VLANs are configured by the administrator before
4452 * passing the VF to the guest or if the AVF driver doesn't support VLAN
4455 static int ice_vf_init_vlan_stripping(struct ice_vf *vf)
4457 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
4462 /* don't modify stripping if port VLAN is configured */
4466 if (ice_vf_vlan_offload_ena(vf->driver_caps))
4467 return ice_vsi_manage_vlan_stripping(vsi, true);
4469 return ice_vsi_manage_vlan_stripping(vsi, false);
4472 static struct ice_vc_vf_ops ice_vc_vf_dflt_ops = {
4473 .get_ver_msg = ice_vc_get_ver_msg,
4474 .get_vf_res_msg = ice_vc_get_vf_res_msg,
4475 .reset_vf = ice_vc_reset_vf_msg,
4476 .add_mac_addr_msg = ice_vc_add_mac_addr_msg,
4477 .del_mac_addr_msg = ice_vc_del_mac_addr_msg,
4478 .cfg_qs_msg = ice_vc_cfg_qs_msg,
4479 .ena_qs_msg = ice_vc_ena_qs_msg,
4480 .dis_qs_msg = ice_vc_dis_qs_msg,
4481 .request_qs_msg = ice_vc_request_qs_msg,
4482 .cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
4483 .config_rss_key = ice_vc_config_rss_key,
4484 .config_rss_lut = ice_vc_config_rss_lut,
4485 .get_stats_msg = ice_vc_get_stats_msg,
4486 .cfg_promiscuous_mode_msg = ice_vc_cfg_promiscuous_mode_msg,
4487 .add_vlan_msg = ice_vc_add_vlan_msg,
4488 .remove_vlan_msg = ice_vc_remove_vlan_msg,
4489 .ena_vlan_stripping = ice_vc_ena_vlan_stripping,
4490 .dis_vlan_stripping = ice_vc_dis_vlan_stripping,
4491 .handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
4492 .add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
4493 .del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
4496 void ice_vc_set_dflt_vf_ops(struct ice_vc_vf_ops *ops)
4498 *ops = ice_vc_vf_dflt_ops;
4502 ice_vc_repr_no_action_msg(struct ice_vf __always_unused *vf,
4503 u8 __always_unused *msg)
4509 * ice_vc_repr_add_mac
4510 * @vf: pointer to VF
4511 * @msg: virtchannel message
4513 * When port representors are created, we do not add MAC rule
4514 * to firmware, we store it so that PF could report same
4517 static int ice_vc_repr_add_mac(struct ice_vf *vf, u8 *msg)
4519 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4520 struct virtchnl_ether_addr_list *al =
4521 (struct virtchnl_ether_addr_list *)msg;
4522 struct ice_vsi *vsi;
4526 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
4527 !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
4528 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4529 goto handle_mac_exit;
4534 vsi = ice_get_vf_vsi(vf);
4536 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4537 goto handle_mac_exit;
4540 for (i = 0; i < al->num_elements; i++) {
4541 u8 *mac_addr = al->list[i].addr;
4543 if (!is_unicast_ether_addr(mac_addr) ||
4544 ether_addr_equal(mac_addr, vf->hw_lan_addr.addr))
4547 if (vf->pf_set_mac) {
4548 dev_err(ice_pf_to_dev(pf), "VF attempting to override administratively set MAC address\n");
4549 v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
4550 goto handle_mac_exit;
4553 ice_vfhw_mac_add(vf, &al->list[i]);
4559 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
4564 * ice_vc_repr_del_mac - response with success for deleting MAC
4565 * @vf: pointer to VF
4566 * @msg: virtchannel message
4568 * Respond with success to not break normal VF flow.
4569 * For legacy VF driver try to update cached MAC address.
4572 ice_vc_repr_del_mac(struct ice_vf __always_unused *vf, u8 __always_unused *msg)
4574 struct virtchnl_ether_addr_list *al =
4575 (struct virtchnl_ether_addr_list *)msg;
4577 ice_update_legacy_cached_mac(vf, &al->list[0]);
4579 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
4580 VIRTCHNL_STATUS_SUCCESS, NULL, 0);
4583 static int ice_vc_repr_no_action(struct ice_vf __always_unused *vf)
4588 void ice_vc_change_ops_to_repr(struct ice_vc_vf_ops *ops)
4590 ops->add_mac_addr_msg = ice_vc_repr_add_mac;
4591 ops->del_mac_addr_msg = ice_vc_repr_del_mac;
4592 ops->add_vlan_msg = ice_vc_repr_no_action_msg;
4593 ops->remove_vlan_msg = ice_vc_repr_no_action_msg;
4594 ops->ena_vlan_stripping = ice_vc_repr_no_action;
4595 ops->dis_vlan_stripping = ice_vc_repr_no_action;
4596 ops->cfg_promiscuous_mode_msg = ice_vc_repr_no_action_msg;
4600 * ice_vc_process_vf_msg - Process request from VF
4601 * @pf: pointer to the PF structure
4602 * @event: pointer to the AQ event
4604 * called from the common asq/arq handler to
4605 * process request from VF
4607 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
4609 u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
4610 s16 vf_id = le16_to_cpu(event->desc.retval);
4611 u16 msglen = event->msg_len;
4612 struct ice_vc_vf_ops *ops;
4613 u8 *msg = event->msg_buf;
4614 struct ice_vf *vf = NULL;
4618 /* if de-init is underway, don't process messages from VF */
4619 if (test_bit(ICE_VF_DEINIT_IN_PROGRESS, pf->state))
4622 dev = ice_pf_to_dev(pf);
4623 if (ice_validate_vf_id(pf, vf_id)) {
4628 vf = &pf->vf[vf_id];
4630 /* Check if VF is disabled. */
4631 if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
4638 /* Perform basic checks on the msg */
4639 err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
4641 if (err == VIRTCHNL_STATUS_ERR_PARAM)
4647 if (!ice_vc_is_opcode_allowed(vf, v_opcode)) {
4648 ice_vc_send_msg_to_vf(vf, v_opcode,
4649 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED, NULL,
4656 ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
4658 dev_err(dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
4659 vf_id, v_opcode, msglen, err);
4664 case VIRTCHNL_OP_VERSION:
4665 err = ops->get_ver_msg(vf, msg);
4667 case VIRTCHNL_OP_GET_VF_RESOURCES:
4668 err = ops->get_vf_res_msg(vf, msg);
4669 if (ice_vf_init_vlan_stripping(vf))
4670 dev_err(dev, "Failed to initialize VLAN stripping for VF %d\n",
4672 ice_vc_notify_vf_link_state(vf);
4674 case VIRTCHNL_OP_RESET_VF:
4677 case VIRTCHNL_OP_ADD_ETH_ADDR:
4678 err = ops->add_mac_addr_msg(vf, msg);
4680 case VIRTCHNL_OP_DEL_ETH_ADDR:
4681 err = ops->del_mac_addr_msg(vf, msg);
4683 case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
4684 err = ops->cfg_qs_msg(vf, msg);
4686 case VIRTCHNL_OP_ENABLE_QUEUES:
4687 err = ops->ena_qs_msg(vf, msg);
4688 ice_vc_notify_vf_link_state(vf);
4690 case VIRTCHNL_OP_DISABLE_QUEUES:
4691 err = ops->dis_qs_msg(vf, msg);
4693 case VIRTCHNL_OP_REQUEST_QUEUES:
4694 err = ops->request_qs_msg(vf, msg);
4696 case VIRTCHNL_OP_CONFIG_IRQ_MAP:
4697 err = ops->cfg_irq_map_msg(vf, msg);
4699 case VIRTCHNL_OP_CONFIG_RSS_KEY:
4700 err = ops->config_rss_key(vf, msg);
4702 case VIRTCHNL_OP_CONFIG_RSS_LUT:
4703 err = ops->config_rss_lut(vf, msg);
4705 case VIRTCHNL_OP_GET_STATS:
4706 err = ops->get_stats_msg(vf, msg);
4708 case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
4709 err = ops->cfg_promiscuous_mode_msg(vf, msg);
4711 case VIRTCHNL_OP_ADD_VLAN:
4712 err = ops->add_vlan_msg(vf, msg);
4714 case VIRTCHNL_OP_DEL_VLAN:
4715 err = ops->remove_vlan_msg(vf, msg);
4717 case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
4718 err = ops->ena_vlan_stripping(vf);
4720 case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
4721 err = ops->dis_vlan_stripping(vf);
4723 case VIRTCHNL_OP_ADD_FDIR_FILTER:
4724 err = ops->add_fdir_fltr_msg(vf, msg);
4726 case VIRTCHNL_OP_DEL_FDIR_FILTER:
4727 err = ops->del_fdir_fltr_msg(vf, msg);
4729 case VIRTCHNL_OP_ADD_RSS_CFG:
4730 err = ops->handle_rss_cfg_msg(vf, msg, true);
4732 case VIRTCHNL_OP_DEL_RSS_CFG:
4733 err = ops->handle_rss_cfg_msg(vf, msg, false);
4735 case VIRTCHNL_OP_UNKNOWN:
4737 dev_err(dev, "Unsupported opcode %d from VF %d\n", v_opcode,
4739 err = ice_vc_send_msg_to_vf(vf, v_opcode,
4740 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4745 /* Helper function cares less about error return values here
4746 * as it is busy with pending work.
4748 dev_info(dev, "PF failed to honor VF %d, opcode %d, error %d\n",
4749 vf_id, v_opcode, err);
4755 * @netdev: network interface device structure
4756 * @vf_id: VF identifier
4757 * @ivi: VF configuration structure
4759 * return VF configuration
4762 ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
4764 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4767 if (ice_validate_vf_id(pf, vf_id))
4770 vf = &pf->vf[vf_id];
4772 if (ice_check_vf_init(pf, vf))
4776 ether_addr_copy(ivi->mac, vf->hw_lan_addr.addr);
4778 /* VF configuration for VLAN and applicable QoS */
4779 ivi->vlan = vf->port_vlan_info & VLAN_VID_MASK;
4780 ivi->qos = (vf->port_vlan_info & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
4782 ivi->trusted = vf->trusted;
4783 ivi->spoofchk = vf->spoofchk;
4784 if (!vf->link_forced)
4785 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
4786 else if (vf->link_up)
4787 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
4789 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
4790 ivi->max_tx_rate = vf->max_tx_rate;
4791 ivi->min_tx_rate = vf->min_tx_rate;
4796 * ice_unicast_mac_exists - check if the unicast MAC exists on the PF's switch
4797 * @pf: PF used to reference the switch's rules
4798 * @umac: unicast MAC to compare against existing switch rules
4800 * Return true on the first/any match, else return false
4802 static bool ice_unicast_mac_exists(struct ice_pf *pf, u8 *umac)
4804 struct ice_sw_recipe *mac_recipe_list =
4805 &pf->hw.switch_info->recp_list[ICE_SW_LKUP_MAC];
4806 struct ice_fltr_mgmt_list_entry *list_itr;
4807 struct list_head *rule_head;
4808 struct mutex *rule_lock; /* protect MAC filter list access */
4810 rule_head = &mac_recipe_list->filt_rules;
4811 rule_lock = &mac_recipe_list->filt_rule_lock;
4813 mutex_lock(rule_lock);
4814 list_for_each_entry(list_itr, rule_head, list_entry) {
4815 u8 *existing_mac = &list_itr->fltr_info.l_data.mac.mac_addr[0];
4817 if (ether_addr_equal(existing_mac, umac)) {
4818 mutex_unlock(rule_lock);
4823 mutex_unlock(rule_lock);
4830 * @netdev: network interface device structure
4831 * @vf_id: VF identifier
4834 * program VF MAC address
4836 int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
4838 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4842 if (ice_is_eswitch_mode_switchdev(pf)) {
4843 dev_info(ice_pf_to_dev(pf), "Trusted VF is forbidden in switchdev mode\n");
4847 if (ice_validate_vf_id(pf, vf_id))
4850 if (is_multicast_ether_addr(mac)) {
4851 netdev_err(netdev, "%pM not a valid unicast address\n", mac);
4855 vf = &pf->vf[vf_id];
4856 /* nothing left to do, unicast MAC already set */
4857 if (ether_addr_equal(vf->dev_lan_addr.addr, mac) &&
4858 ether_addr_equal(vf->hw_lan_addr.addr, mac))
4861 ret = ice_check_vf_ready_for_cfg(vf);
4865 if (ice_unicast_mac_exists(pf, mac)) {
4866 netdev_err(netdev, "Unicast MAC %pM already exists on this PF. Preventing setting VF %u unicast MAC address to %pM\n",
4871 /* VF is notified of its new MAC via the PF's response to the
4872 * VIRTCHNL_OP_GET_VF_RESOURCES message after the VF has been reset
4874 ether_addr_copy(vf->dev_lan_addr.addr, mac);
4875 ether_addr_copy(vf->hw_lan_addr.addr, mac);
4876 if (is_zero_ether_addr(mac)) {
4877 /* VF will send VIRTCHNL_OP_ADD_ETH_ADDR message with its MAC */
4878 vf->pf_set_mac = false;
4879 netdev_info(netdev, "Removing MAC on VF %d. VF driver will be reinitialized\n",
4882 /* PF will add MAC rule for the VF */
4883 vf->pf_set_mac = true;
4884 netdev_info(netdev, "Setting MAC %pM on VF %d. VF driver will be reinitialized\n",
4888 ice_vc_reset_vf(vf);
4894 * @netdev: network interface device structure
4895 * @vf_id: VF identifier
4896 * @trusted: Boolean value to enable/disable trusted VF
4898 * Enable or disable a given VF as trusted
4900 int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
4902 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4906 if (ice_validate_vf_id(pf, vf_id))
4909 vf = &pf->vf[vf_id];
4910 ret = ice_check_vf_ready_for_cfg(vf);
4914 /* Check if already trusted */
4915 if (trusted == vf->trusted)
4918 vf->trusted = trusted;
4919 ice_vc_reset_vf(vf);
4920 dev_info(ice_pf_to_dev(pf), "VF %u is now %strusted\n",
4921 vf_id, trusted ? "" : "un");
4927 * ice_set_vf_link_state
4928 * @netdev: network interface device structure
4929 * @vf_id: VF identifier
4930 * @link_state: required link state
4932 * Set VF's link state, irrespective of physical link state status
4934 int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
4936 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4940 if (ice_validate_vf_id(pf, vf_id))
4943 vf = &pf->vf[vf_id];
4944 ret = ice_check_vf_ready_for_cfg(vf);
4948 switch (link_state) {
4949 case IFLA_VF_LINK_STATE_AUTO:
4950 vf->link_forced = false;
4952 case IFLA_VF_LINK_STATE_ENABLE:
4953 vf->link_forced = true;
4956 case IFLA_VF_LINK_STATE_DISABLE:
4957 vf->link_forced = true;
4958 vf->link_up = false;
4964 ice_vc_notify_vf_link_state(vf);
4970 * ice_calc_all_vfs_min_tx_rate - calculate cumulative min Tx rate on all VFs
4971 * @pf: PF associated with VFs
4973 static int ice_calc_all_vfs_min_tx_rate(struct ice_pf *pf)
4977 ice_for_each_vf(pf, i)
4978 rate += pf->vf[i].min_tx_rate;
4984 * ice_min_tx_rate_oversubscribed - check if min Tx rate causes oversubscription
4985 * @vf: VF trying to configure min_tx_rate
4986 * @min_tx_rate: min Tx rate in Mbps
4988 * Check if the min_tx_rate being passed in will cause oversubscription of total
4989 * min_tx_rate based on the current link speed and all other VFs configured
4992 * Return true if the passed min_tx_rate would cause oversubscription, else
4996 ice_min_tx_rate_oversubscribed(struct ice_vf *vf, int min_tx_rate)
4998 int link_speed_mbps = ice_get_link_speed_mbps(ice_get_vf_vsi(vf));
4999 int all_vfs_min_tx_rate = ice_calc_all_vfs_min_tx_rate(vf->pf);
5001 /* this VF's previous rate is being overwritten */
5002 all_vfs_min_tx_rate -= vf->min_tx_rate;
5004 if (all_vfs_min_tx_rate + min_tx_rate > link_speed_mbps) {
5005 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",
5006 min_tx_rate, vf->vf_id,
5007 all_vfs_min_tx_rate + min_tx_rate - link_speed_mbps,
5016 * ice_set_vf_bw - set min/max VF bandwidth
5017 * @netdev: network interface device structure
5018 * @vf_id: VF identifier
5019 * @min_tx_rate: Minimum Tx rate in Mbps
5020 * @max_tx_rate: Maximum Tx rate in Mbps
5023 ice_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
5026 struct ice_pf *pf = ice_netdev_to_pf(netdev);
5027 struct ice_vsi *vsi;
5032 dev = ice_pf_to_dev(pf);
5033 if (ice_validate_vf_id(pf, vf_id))
5036 vf = &pf->vf[vf_id];
5037 ret = ice_check_vf_ready_for_cfg(vf);
5041 vsi = ice_get_vf_vsi(vf);
5043 /* when max_tx_rate is zero that means no max Tx rate limiting, so only
5044 * check if max_tx_rate is non-zero
5046 if (max_tx_rate && min_tx_rate > max_tx_rate) {
5047 dev_err(dev, "Cannot set min Tx rate %d Mbps greater than max Tx rate %d Mbps\n",
5048 min_tx_rate, max_tx_rate);
5052 if (min_tx_rate && ice_is_dcb_active(pf)) {
5053 dev_err(dev, "DCB on PF is currently enabled. VF min Tx rate limiting not allowed on this PF.\n");
5057 if (ice_min_tx_rate_oversubscribed(vf, min_tx_rate))
5060 if (vf->min_tx_rate != (unsigned int)min_tx_rate) {
5061 ret = ice_set_min_bw_limit(vsi, (u64)min_tx_rate * 1000);
5063 dev_err(dev, "Unable to set min-tx-rate for VF %d\n",
5068 vf->min_tx_rate = min_tx_rate;
5071 if (vf->max_tx_rate != (unsigned int)max_tx_rate) {
5072 ret = ice_set_max_bw_limit(vsi, (u64)max_tx_rate * 1000);
5074 dev_err(dev, "Unable to set max-tx-rate for VF %d\n",
5079 vf->max_tx_rate = max_tx_rate;
5086 * ice_get_vf_stats - populate some stats for the VF
5087 * @netdev: the netdev of the PF
5088 * @vf_id: the host OS identifier (0-255)
5089 * @vf_stats: pointer to the OS memory to be initialized
5091 int ice_get_vf_stats(struct net_device *netdev, int vf_id,
5092 struct ifla_vf_stats *vf_stats)
5094 struct ice_pf *pf = ice_netdev_to_pf(netdev);
5095 struct ice_eth_stats *stats;
5096 struct ice_vsi *vsi;
5100 if (ice_validate_vf_id(pf, vf_id))
5103 vf = &pf->vf[vf_id];
5104 ret = ice_check_vf_ready_for_cfg(vf);
5108 vsi = ice_get_vf_vsi(vf);
5112 ice_update_eth_stats(vsi);
5113 stats = &vsi->eth_stats;
5115 memset(vf_stats, 0, sizeof(*vf_stats));
5117 vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
5118 stats->rx_multicast;
5119 vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
5120 stats->tx_multicast;
5121 vf_stats->rx_bytes = stats->rx_bytes;
5122 vf_stats->tx_bytes = stats->tx_bytes;
5123 vf_stats->broadcast = stats->rx_broadcast;
5124 vf_stats->multicast = stats->rx_multicast;
5125 vf_stats->rx_dropped = stats->rx_discards;
5126 vf_stats->tx_dropped = stats->tx_discards;
5132 * ice_print_vf_rx_mdd_event - print VF Rx malicious driver detect event
5133 * @vf: pointer to the VF structure
5135 void ice_print_vf_rx_mdd_event(struct ice_vf *vf)
5137 struct ice_pf *pf = vf->pf;
5140 dev = ice_pf_to_dev(pf);
5142 dev_info(dev, "%d Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pM. mdd-auto-reset-vfs=%s\n",
5143 vf->mdd_rx_events.count, pf->hw.pf_id, vf->vf_id,
5144 vf->dev_lan_addr.addr,
5145 test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)
5150 * ice_print_vfs_mdd_events - print VFs malicious driver detect event
5151 * @pf: pointer to the PF structure
5153 * Called from ice_handle_mdd_event to rate limit and print VFs MDD events.
5155 void ice_print_vfs_mdd_events(struct ice_pf *pf)
5157 struct device *dev = ice_pf_to_dev(pf);
5158 struct ice_hw *hw = &pf->hw;
5161 /* check that there are pending MDD events to print */
5162 if (!test_and_clear_bit(ICE_MDD_VF_PRINT_PENDING, pf->state))
5165 /* VF MDD event logs are rate limited to one second intervals */
5166 if (time_is_after_jiffies(pf->last_printed_mdd_jiffies + HZ * 1))
5169 pf->last_printed_mdd_jiffies = jiffies;
5171 ice_for_each_vf(pf, i) {
5172 struct ice_vf *vf = &pf->vf[i];
5174 /* only print Rx MDD event message if there are new events */
5175 if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
5176 vf->mdd_rx_events.last_printed =
5177 vf->mdd_rx_events.count;
5178 ice_print_vf_rx_mdd_event(vf);
5181 /* only print Tx MDD event message if there are new events */
5182 if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
5183 vf->mdd_tx_events.last_printed =
5184 vf->mdd_tx_events.count;
5186 dev_info(dev, "%d Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pM.\n",
5187 vf->mdd_tx_events.count, hw->pf_id, i,
5188 vf->dev_lan_addr.addr);
5194 * ice_restore_all_vfs_msi_state - restore VF MSI state after PF FLR
5195 * @pdev: pointer to a pci_dev structure
5197 * Called when recovering from a PF FLR to restore interrupt capability to
5200 void ice_restore_all_vfs_msi_state(struct pci_dev *pdev)
5205 if (!pci_num_vf(pdev))
5208 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
5210 struct pci_dev *vfdev;
5212 pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID,
5214 vfdev = pci_get_device(pdev->vendor, vf_id, NULL);
5216 if (vfdev->is_virtfn && vfdev->physfn == pdev)
5217 pci_restore_msi_state(vfdev);
5218 vfdev = pci_get_device(pdev->vendor, vf_id,
5225 * ice_is_malicious_vf - helper function to detect a malicious VF
5226 * @pf: ptr to struct ice_pf
5227 * @event: pointer to the AQ event
5228 * @num_msg_proc: the number of messages processed so far
5229 * @num_msg_pending: the number of messages peinding in admin queue
5232 ice_is_malicious_vf(struct ice_pf *pf, struct ice_rq_event_info *event,
5233 u16 num_msg_proc, u16 num_msg_pending)
5235 s16 vf_id = le16_to_cpu(event->desc.retval);
5236 struct device *dev = ice_pf_to_dev(pf);
5237 struct ice_mbx_data mbxdata;
5238 enum ice_status status;
5242 if (ice_validate_vf_id(pf, vf_id))
5245 vf = &pf->vf[vf_id];
5246 /* Check if VF is disabled. */
5247 if (test_bit(ICE_VF_STATE_DIS, vf->vf_states))
5250 mbxdata.num_msg_proc = num_msg_proc;
5251 mbxdata.num_pending_arq = num_msg_pending;
5252 mbxdata.max_num_msgs_mbx = pf->hw.mailboxq.num_rq_entries;
5253 #define ICE_MBX_OVERFLOW_WATERMARK 64
5254 mbxdata.async_watermark_val = ICE_MBX_OVERFLOW_WATERMARK;
5256 /* check to see if we have a malicious VF */
5257 status = ice_mbx_vf_state_handler(&pf->hw, &mbxdata, vf_id, &malvf);
5262 bool report_vf = false;
5264 /* if the VF is malicious and we haven't let the user
5265 * know about it, then let them know now
5267 status = ice_mbx_report_malvf(&pf->hw, pf->malvfs,
5268 ICE_MAX_VF_COUNT, vf_id,
5271 dev_dbg(dev, "Error reporting malicious VF\n");
5274 struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
5277 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",
5278 &vf->dev_lan_addr.addr[0],
5279 pf_vsi->netdev->dev_addr);
5285 /* if there was an error in detection or the VF is not malicious then