1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
9 #include "ice_virtchnl_allowlist.h"
11 #define FIELD_SELECTOR(proto_hdr_field) \
12 BIT((proto_hdr_field) & PROTO_HDR_FIELD_MASK)
14 struct ice_vc_hdr_match_type {
15 u32 vc_hdr; /* virtchnl headers (VIRTCHNL_PROTO_HDR_XXX) */
16 u32 ice_hdr; /* ice headers (ICE_FLOW_SEG_HDR_XXX) */
19 static const struct ice_vc_hdr_match_type ice_vc_hdr_list_os[] = {
20 {VIRTCHNL_PROTO_HDR_NONE, ICE_FLOW_SEG_HDR_NONE},
21 {VIRTCHNL_PROTO_HDR_IPV4, ICE_FLOW_SEG_HDR_IPV4 |
22 ICE_FLOW_SEG_HDR_IPV_OTHER},
23 {VIRTCHNL_PROTO_HDR_IPV6, ICE_FLOW_SEG_HDR_IPV6 |
24 ICE_FLOW_SEG_HDR_IPV_OTHER},
25 {VIRTCHNL_PROTO_HDR_TCP, ICE_FLOW_SEG_HDR_TCP},
26 {VIRTCHNL_PROTO_HDR_UDP, ICE_FLOW_SEG_HDR_UDP},
27 {VIRTCHNL_PROTO_HDR_SCTP, ICE_FLOW_SEG_HDR_SCTP},
30 static const struct ice_vc_hdr_match_type ice_vc_hdr_list_comms[] = {
31 {VIRTCHNL_PROTO_HDR_NONE, ICE_FLOW_SEG_HDR_NONE},
32 {VIRTCHNL_PROTO_HDR_ETH, ICE_FLOW_SEG_HDR_ETH},
33 {VIRTCHNL_PROTO_HDR_S_VLAN, ICE_FLOW_SEG_HDR_VLAN},
34 {VIRTCHNL_PROTO_HDR_C_VLAN, ICE_FLOW_SEG_HDR_VLAN},
35 {VIRTCHNL_PROTO_HDR_IPV4, ICE_FLOW_SEG_HDR_IPV4 |
36 ICE_FLOW_SEG_HDR_IPV_OTHER},
37 {VIRTCHNL_PROTO_HDR_IPV6, ICE_FLOW_SEG_HDR_IPV6 |
38 ICE_FLOW_SEG_HDR_IPV_OTHER},
39 {VIRTCHNL_PROTO_HDR_TCP, ICE_FLOW_SEG_HDR_TCP},
40 {VIRTCHNL_PROTO_HDR_UDP, ICE_FLOW_SEG_HDR_UDP},
41 {VIRTCHNL_PROTO_HDR_SCTP, ICE_FLOW_SEG_HDR_SCTP},
42 {VIRTCHNL_PROTO_HDR_PPPOE, ICE_FLOW_SEG_HDR_PPPOE},
43 {VIRTCHNL_PROTO_HDR_GTPU_IP, ICE_FLOW_SEG_HDR_GTPU_IP},
44 {VIRTCHNL_PROTO_HDR_GTPU_EH, ICE_FLOW_SEG_HDR_GTPU_EH},
45 {VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_DWN,
46 ICE_FLOW_SEG_HDR_GTPU_DWN},
47 {VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_UP,
48 ICE_FLOW_SEG_HDR_GTPU_UP},
49 {VIRTCHNL_PROTO_HDR_L2TPV3, ICE_FLOW_SEG_HDR_L2TPV3},
50 {VIRTCHNL_PROTO_HDR_ESP, ICE_FLOW_SEG_HDR_ESP},
51 {VIRTCHNL_PROTO_HDR_AH, ICE_FLOW_SEG_HDR_AH},
52 {VIRTCHNL_PROTO_HDR_PFCP, ICE_FLOW_SEG_HDR_PFCP_SESSION},
55 struct ice_vc_hash_field_match_type {
56 u32 vc_hdr; /* virtchnl headers
57 * (VIRTCHNL_PROTO_HDR_XXX)
59 u32 vc_hash_field; /* virtchnl hash fields selector
60 * FIELD_SELECTOR((VIRTCHNL_PROTO_HDR_ETH_XXX))
62 u64 ice_hash_field; /* ice hash fields
63 * (BIT_ULL(ICE_FLOW_FIELD_IDX_XXX))
68 ice_vc_hash_field_match_type ice_vc_hash_field_list_os[] = {
69 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC),
70 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA)},
71 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
72 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA)},
73 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
74 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
76 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
77 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
78 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA) |
79 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
80 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
81 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
82 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA) |
83 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
84 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
85 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
86 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
87 ICE_FLOW_HASH_IPV4 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
88 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
89 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
90 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC),
91 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA)},
92 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
93 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA)},
94 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
95 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
97 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
98 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
99 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA) |
100 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
101 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
102 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
103 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA) |
104 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
105 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
106 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
107 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
108 ICE_FLOW_HASH_IPV6 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
109 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
110 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
111 {VIRTCHNL_PROTO_HDR_TCP,
112 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT),
113 BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_SRC_PORT)},
114 {VIRTCHNL_PROTO_HDR_TCP,
115 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
116 BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_DST_PORT)},
117 {VIRTCHNL_PROTO_HDR_TCP,
118 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT) |
119 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
120 ICE_FLOW_HASH_TCP_PORT},
121 {VIRTCHNL_PROTO_HDR_UDP,
122 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT),
123 BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_SRC_PORT)},
124 {VIRTCHNL_PROTO_HDR_UDP,
125 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
126 BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_DST_PORT)},
127 {VIRTCHNL_PROTO_HDR_UDP,
128 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT) |
129 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
130 ICE_FLOW_HASH_UDP_PORT},
131 {VIRTCHNL_PROTO_HDR_SCTP,
132 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT),
133 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT)},
134 {VIRTCHNL_PROTO_HDR_SCTP,
135 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
136 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_DST_PORT)},
137 {VIRTCHNL_PROTO_HDR_SCTP,
138 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT) |
139 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
140 ICE_FLOW_HASH_SCTP_PORT},
144 ice_vc_hash_field_match_type ice_vc_hash_field_list_comms[] = {
145 {VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC),
146 BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_SA)},
147 {VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
148 BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_DA)},
149 {VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC) |
150 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
152 {VIRTCHNL_PROTO_HDR_ETH,
153 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_ETHERTYPE),
154 BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_TYPE)},
155 {VIRTCHNL_PROTO_HDR_S_VLAN,
156 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_S_VLAN_ID),
157 BIT_ULL(ICE_FLOW_FIELD_IDX_S_VLAN)},
158 {VIRTCHNL_PROTO_HDR_C_VLAN,
159 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_C_VLAN_ID),
160 BIT_ULL(ICE_FLOW_FIELD_IDX_C_VLAN)},
161 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC),
162 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA)},
163 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
164 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA)},
165 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
166 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
168 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
169 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
170 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA) |
171 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
172 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
173 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
174 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA) |
175 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
176 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
177 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
178 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
179 ICE_FLOW_HASH_IPV4 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
180 {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
181 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
182 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC),
183 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA)},
184 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
185 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA)},
186 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
187 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
189 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
190 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
191 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA) |
192 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
193 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
194 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
195 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA) |
196 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
197 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
198 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
199 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
200 ICE_FLOW_HASH_IPV6 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
201 {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
202 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
203 {VIRTCHNL_PROTO_HDR_TCP,
204 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT),
205 BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_SRC_PORT)},
206 {VIRTCHNL_PROTO_HDR_TCP,
207 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
208 BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_DST_PORT)},
209 {VIRTCHNL_PROTO_HDR_TCP,
210 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT) |
211 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
212 ICE_FLOW_HASH_TCP_PORT},
213 {VIRTCHNL_PROTO_HDR_UDP,
214 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT),
215 BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_SRC_PORT)},
216 {VIRTCHNL_PROTO_HDR_UDP,
217 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
218 BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_DST_PORT)},
219 {VIRTCHNL_PROTO_HDR_UDP,
220 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT) |
221 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
222 ICE_FLOW_HASH_UDP_PORT},
223 {VIRTCHNL_PROTO_HDR_SCTP,
224 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT),
225 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT)},
226 {VIRTCHNL_PROTO_HDR_SCTP,
227 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
228 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_DST_PORT)},
229 {VIRTCHNL_PROTO_HDR_SCTP,
230 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT) |
231 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
232 ICE_FLOW_HASH_SCTP_PORT},
233 {VIRTCHNL_PROTO_HDR_PPPOE,
234 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PPPOE_SESS_ID),
235 BIT_ULL(ICE_FLOW_FIELD_IDX_PPPOE_SESS_ID)},
236 {VIRTCHNL_PROTO_HDR_GTPU_IP,
237 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_GTPU_IP_TEID),
238 BIT_ULL(ICE_FLOW_FIELD_IDX_GTPU_IP_TEID)},
239 {VIRTCHNL_PROTO_HDR_L2TPV3,
240 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_L2TPV3_SESS_ID),
241 BIT_ULL(ICE_FLOW_FIELD_IDX_L2TPV3_SESS_ID)},
242 {VIRTCHNL_PROTO_HDR_ESP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ESP_SPI),
243 BIT_ULL(ICE_FLOW_FIELD_IDX_ESP_SPI)},
244 {VIRTCHNL_PROTO_HDR_AH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_AH_SPI),
245 BIT_ULL(ICE_FLOW_FIELD_IDX_AH_SPI)},
246 {VIRTCHNL_PROTO_HDR_PFCP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PFCP_SEID),
247 BIT_ULL(ICE_FLOW_FIELD_IDX_PFCP_SEID)},
251 * ice_get_vf_vsi - get VF's VSI based on the stored index
252 * @vf: VF used to get VSI
254 static struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf)
256 return vf->pf->vsi[vf->lan_vsi_idx];
260 * ice_validate_vf_id - helper to check if VF ID is valid
261 * @pf: pointer to the PF structure
262 * @vf_id: the ID of the VF to check
264 static int ice_validate_vf_id(struct ice_pf *pf, u16 vf_id)
266 /* vf_id range is only valid for 0-255, and should always be unsigned */
267 if (vf_id >= pf->num_alloc_vfs) {
268 dev_err(ice_pf_to_dev(pf), "Invalid VF ID: %u\n", vf_id);
275 * ice_check_vf_init - helper to check if VF init complete
276 * @pf: pointer to the PF structure
277 * @vf: the pointer to the VF to check
279 static int ice_check_vf_init(struct ice_pf *pf, struct ice_vf *vf)
281 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
282 dev_err(ice_pf_to_dev(pf), "VF ID: %u in reset. Try again.\n",
290 * ice_err_to_virt_err - translate errors for VF return code
291 * @ice_err: error return code
293 static enum virtchnl_status_code ice_err_to_virt_err(enum ice_status ice_err)
297 return VIRTCHNL_STATUS_SUCCESS;
298 case ICE_ERR_BAD_PTR:
299 case ICE_ERR_INVAL_SIZE:
300 case ICE_ERR_DEVICE_NOT_SUPPORTED:
303 return VIRTCHNL_STATUS_ERR_PARAM;
304 case ICE_ERR_NO_MEMORY:
305 return VIRTCHNL_STATUS_ERR_NO_MEMORY;
306 case ICE_ERR_NOT_READY:
307 case ICE_ERR_RESET_FAILED:
308 case ICE_ERR_FW_API_VER:
309 case ICE_ERR_AQ_ERROR:
310 case ICE_ERR_AQ_TIMEOUT:
311 case ICE_ERR_AQ_FULL:
312 case ICE_ERR_AQ_NO_WORK:
313 case ICE_ERR_AQ_EMPTY:
314 return VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
316 return VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
321 * ice_vc_vf_broadcast - Broadcast a message to all VFs on PF
322 * @pf: pointer to the PF structure
323 * @v_opcode: operation code
324 * @v_retval: return value
325 * @msg: pointer to the msg buffer
326 * @msglen: msg length
329 ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
330 enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
332 struct ice_hw *hw = &pf->hw;
335 ice_for_each_vf(pf, i) {
336 struct ice_vf *vf = &pf->vf[i];
338 /* Not all vfs are enabled so skip the ones that are not */
339 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
340 !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
343 /* Ignore return value on purpose - a given VF may fail, but
344 * we need to keep going and send to all of them
346 ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
352 * ice_set_pfe_link - Set the link speed/status of the virtchnl_pf_event
353 * @vf: pointer to the VF structure
354 * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
355 * @ice_link_speed: link speed specified by ICE_AQ_LINK_SPEED_*
356 * @link_up: whether or not to set the link up/down
359 ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
360 int ice_link_speed, bool link_up)
362 if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
363 pfe->event_data.link_event_adv.link_status = link_up;
365 pfe->event_data.link_event_adv.link_speed =
366 ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
368 pfe->event_data.link_event.link_status = link_up;
369 /* Legacy method for virtchnl link speeds */
370 pfe->event_data.link_event.link_speed =
371 (enum virtchnl_link_speed)
372 ice_conv_link_speed_to_virtchnl(false, ice_link_speed);
377 * ice_vf_has_no_qs_ena - check if the VF has any Rx or Tx queues enabled
378 * @vf: the VF to check
380 * Returns true if the VF has no Rx and no Tx queues enabled and returns false
383 static bool ice_vf_has_no_qs_ena(struct ice_vf *vf)
385 return (!bitmap_weight(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF) &&
386 !bitmap_weight(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF));
390 * ice_is_vf_link_up - check if the VF's link is up
391 * @vf: VF to check if link is up
393 static bool ice_is_vf_link_up(struct ice_vf *vf)
395 struct ice_pf *pf = vf->pf;
397 if (ice_check_vf_init(pf, vf))
400 if (ice_vf_has_no_qs_ena(vf))
402 else if (vf->link_forced)
405 return pf->hw.port_info->phy.link_info.link_info &
410 * ice_vc_notify_vf_link_state - Inform a VF of link status
411 * @vf: pointer to the VF structure
413 * send a link status message to a single VF
415 static void ice_vc_notify_vf_link_state(struct ice_vf *vf)
417 struct virtchnl_pf_event pfe = { 0 };
418 struct ice_hw *hw = &vf->pf->hw;
420 pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
421 pfe.severity = PF_EVENT_SEVERITY_INFO;
423 if (ice_is_vf_link_up(vf))
424 ice_set_pfe_link(vf, &pfe,
425 hw->port_info->phy.link_info.link_speed, true);
427 ice_set_pfe_link(vf, &pfe, ICE_AQ_LINK_SPEED_UNKNOWN, false);
429 ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
430 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,
435 * ice_vf_invalidate_vsi - invalidate vsi_idx/vsi_num to remove VSI access
436 * @vf: VF to remove access to VSI for
438 static void ice_vf_invalidate_vsi(struct ice_vf *vf)
440 vf->lan_vsi_idx = ICE_NO_VSI;
441 vf->lan_vsi_num = ICE_NO_VSI;
445 * ice_vf_vsi_release - invalidate the VF's VSI after freeing it
446 * @vf: invalidate this VF's VSI after freeing it
448 static void ice_vf_vsi_release(struct ice_vf *vf)
450 ice_vsi_release(ice_get_vf_vsi(vf));
451 ice_vf_invalidate_vsi(vf);
455 * ice_vf_ctrl_invalidate_vsi - invalidate ctrl_vsi_idx to remove VSI access
456 * @vf: VF that control VSI is being invalidated on
458 static void ice_vf_ctrl_invalidate_vsi(struct ice_vf *vf)
460 vf->ctrl_vsi_idx = ICE_NO_VSI;
464 * ice_vf_ctrl_vsi_release - invalidate the VF's control VSI after freeing it
465 * @vf: VF that control VSI is being released on
467 static void ice_vf_ctrl_vsi_release(struct ice_vf *vf)
469 ice_vsi_release(vf->pf->vsi[vf->ctrl_vsi_idx]);
470 ice_vf_ctrl_invalidate_vsi(vf);
474 * ice_free_vf_res - Free a VF's resources
475 * @vf: pointer to the VF info
477 static void ice_free_vf_res(struct ice_vf *vf)
479 struct ice_pf *pf = vf->pf;
480 int i, last_vector_idx;
482 /* First, disable VF's configuration API to prevent OS from
483 * accessing the VF's VSI after it's freed or invalidated.
485 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
486 ice_vf_fdir_exit(vf);
487 /* free VF control VSI */
488 if (vf->ctrl_vsi_idx != ICE_NO_VSI)
489 ice_vf_ctrl_vsi_release(vf);
491 /* free VSI and disconnect it from the parent uplink */
492 if (vf->lan_vsi_idx != ICE_NO_VSI) {
493 ice_vf_vsi_release(vf);
497 last_vector_idx = vf->first_vector_idx + pf->num_msix_per_vf - 1;
499 /* clear VF MDD event information */
500 memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
501 memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
503 /* Disable interrupts so that VF starts in a known state */
504 for (i = vf->first_vector_idx; i <= last_vector_idx; i++) {
505 wr32(&pf->hw, GLINT_DYN_CTL(i), GLINT_DYN_CTL_CLEARPBA_M);
508 /* reset some of the state variables keeping track of the resources */
509 clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
510 clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
514 * ice_dis_vf_mappings
515 * @vf: pointer to the VF structure
517 static void ice_dis_vf_mappings(struct ice_vf *vf)
519 struct ice_pf *pf = vf->pf;
526 vsi = ice_get_vf_vsi(vf);
528 dev = ice_pf_to_dev(pf);
529 wr32(hw, VPINT_ALLOC(vf->vf_id), 0);
530 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), 0);
532 first = vf->first_vector_idx;
533 last = first + pf->num_msix_per_vf - 1;
534 for (v = first; v <= last; v++) {
537 reg = (((1 << GLINT_VECT2FUNC_IS_PF_S) &
538 GLINT_VECT2FUNC_IS_PF_M) |
539 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
540 GLINT_VECT2FUNC_PF_NUM_M));
541 wr32(hw, GLINT_VECT2FUNC(v), reg);
544 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
545 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
547 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
549 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
550 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
552 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
556 * ice_sriov_free_msix_res - Reset/free any used MSIX resources
557 * @pf: pointer to the PF structure
559 * Since no MSIX entries are taken from the pf->irq_tracker then just clear
560 * the pf->sriov_base_vector.
562 * Returns 0 on success, and -EINVAL on error.
564 static int ice_sriov_free_msix_res(struct ice_pf *pf)
566 struct ice_res_tracker *res;
571 res = pf->irq_tracker;
575 /* give back irq_tracker resources used */
576 WARN_ON(pf->sriov_base_vector < res->num_entries);
578 pf->sriov_base_vector = 0;
584 * ice_set_vf_state_qs_dis - Set VF queues state to disabled
585 * @vf: pointer to the VF structure
587 void ice_set_vf_state_qs_dis(struct ice_vf *vf)
589 /* Clear Rx/Tx enabled queues flag */
590 bitmap_zero(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF);
591 bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
592 clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
596 * ice_dis_vf_qs - Disable the VF queues
597 * @vf: pointer to the VF structure
599 static void ice_dis_vf_qs(struct ice_vf *vf)
601 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
603 ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id);
604 ice_vsi_stop_all_rx_rings(vsi);
605 ice_set_vf_state_qs_dis(vf);
609 * ice_free_vfs - Free all VFs
610 * @pf: pointer to the PF structure
612 void ice_free_vfs(struct ice_pf *pf)
614 struct device *dev = ice_pf_to_dev(pf);
615 struct ice_hw *hw = &pf->hw;
621 while (test_and_set_bit(ICE_VF_DIS, pf->state))
622 usleep_range(1000, 2000);
624 /* Disable IOV before freeing resources. This lets any VF drivers
625 * running in the host get themselves cleaned up before we yank
626 * the carpet out from underneath their feet.
628 if (!pci_vfs_assigned(pf->pdev))
629 pci_disable_sriov(pf->pdev);
631 dev_warn(dev, "VFs are assigned - not disabling SR-IOV\n");
633 /* Avoid wait time by stopping all VFs at the same time */
634 ice_for_each_vf(pf, i)
635 if (test_bit(ICE_VF_STATE_QS_ENA, pf->vf[i].vf_states))
636 ice_dis_vf_qs(&pf->vf[i]);
638 tmp = pf->num_alloc_vfs;
639 pf->num_qps_per_vf = 0;
640 pf->num_alloc_vfs = 0;
641 for (i = 0; i < tmp; i++) {
642 if (test_bit(ICE_VF_STATE_INIT, pf->vf[i].vf_states)) {
643 /* disable VF qp mappings and set VF disable state */
644 ice_dis_vf_mappings(&pf->vf[i]);
645 set_bit(ICE_VF_STATE_DIS, pf->vf[i].vf_states);
646 ice_free_vf_res(&pf->vf[i]);
650 if (ice_sriov_free_msix_res(pf))
651 dev_err(dev, "Failed to free MSIX resources used by SR-IOV\n");
653 devm_kfree(dev, pf->vf);
656 /* This check is for when the driver is unloaded while VFs are
657 * assigned. Setting the number of VFs to 0 through sysfs is caught
658 * before this function ever gets called.
660 if (!pci_vfs_assigned(pf->pdev)) {
663 /* Acknowledge VFLR for all VFs. Without this, VFs will fail to
664 * work correctly when SR-IOV gets re-enabled.
666 for (vf_id = 0; vf_id < tmp; vf_id++) {
667 u32 reg_idx, bit_idx;
669 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
670 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
671 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
675 /* clear malicious info if the VFs are getting released */
676 for (i = 0; i < tmp; i++)
677 if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->malvfs,
678 ICE_MAX_VF_COUNT, i))
679 dev_dbg(dev, "failed to clear malicious VF state for VF %u\n",
682 clear_bit(ICE_VF_DIS, pf->state);
683 clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
687 * ice_trigger_vf_reset - Reset a VF on HW
688 * @vf: pointer to the VF structure
689 * @is_vflr: true if VFLR was issued, false if not
690 * @is_pfr: true if the reset was triggered due to a previous PFR
692 * Trigger hardware to start a reset for a particular VF. Expects the caller
693 * to wait the proper amount of time to allow hardware to reset the VF before
694 * it cleans up and restores VF functionality.
696 static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr, bool is_pfr)
698 struct ice_pf *pf = vf->pf;
699 u32 reg, reg_idx, bit_idx;
700 unsigned int vf_abs_id, i;
704 dev = ice_pf_to_dev(pf);
706 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
708 /* Inform VF that it is no longer active, as a warning */
709 clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
711 /* Disable VF's configuration API during reset. The flag is re-enabled
712 * when it's safe again to access VF's VSI.
714 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
716 /* VF_MBX_ARQLEN and VF_MBX_ATQLEN are cleared by PFR, so the driver
717 * needs to clear them in the case of VFR/VFLR. If this is done for
718 * PFR, it can mess up VF resets because the VF driver may already
719 * have started cleanup by the time we get here.
722 wr32(hw, VF_MBX_ARQLEN(vf->vf_id), 0);
723 wr32(hw, VF_MBX_ATQLEN(vf->vf_id), 0);
726 /* In the case of a VFLR, the HW has already reset the VF and we
727 * just need to clean up, so don't hit the VFRTRIG register.
730 /* reset VF using VPGEN_VFRTRIG reg */
731 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
732 reg |= VPGEN_VFRTRIG_VFSWR_M;
733 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
735 /* clear the VFLR bit in GLGEN_VFLRSTAT */
736 reg_idx = (vf_abs_id) / 32;
737 bit_idx = (vf_abs_id) % 32;
738 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
741 wr32(hw, PF_PCI_CIAA,
742 VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
743 for (i = 0; i < ICE_PCI_CIAD_WAIT_COUNT; i++) {
744 reg = rd32(hw, PF_PCI_CIAD);
745 /* no transactions pending so stop polling */
746 if ((reg & VF_TRANS_PENDING_M) == 0)
749 dev_err(dev, "VF %u PCI transactions stuck\n", vf->vf_id);
750 udelay(ICE_PCI_CIAD_WAIT_DELAY_US);
755 * ice_vsi_manage_pvid - Enable or disable port VLAN for VSI
756 * @vsi: the VSI to update
757 * @pvid_info: VLAN ID and QoS used to set the PVID VSI context field
758 * @enable: true for enable PVID false for disable
760 static int ice_vsi_manage_pvid(struct ice_vsi *vsi, u16 pvid_info, bool enable)
762 struct ice_hw *hw = &vsi->back->hw;
763 struct ice_aqc_vsi_props *info;
764 struct ice_vsi_ctx *ctxt;
765 enum ice_status status;
768 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
772 ctxt->info = vsi->info;
775 info->vlan_flags = ICE_AQ_VSI_VLAN_MODE_UNTAGGED |
776 ICE_AQ_VSI_PVLAN_INSERT_PVID |
777 ICE_AQ_VSI_VLAN_EMOD_STR;
778 info->sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
780 info->vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING |
781 ICE_AQ_VSI_VLAN_MODE_ALL;
782 info->sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
785 info->pvid = cpu_to_le16(pvid_info);
786 info->valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
787 ICE_AQ_VSI_PROP_SW_VALID);
789 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
791 dev_info(ice_hw_to_dev(hw), "update VSI for port VLAN failed, err %s aq_err %s\n",
792 ice_stat_str(status),
793 ice_aq_str(hw->adminq.sq_last_status));
798 vsi->info.vlan_flags = info->vlan_flags;
799 vsi->info.sw_flags2 = info->sw_flags2;
800 vsi->info.pvid = info->pvid;
807 * ice_vf_get_port_info - Get the VF's port info structure
808 * @vf: VF used to get the port info structure for
810 static struct ice_port_info *ice_vf_get_port_info(struct ice_vf *vf)
812 return vf->pf->hw.port_info;
816 * ice_vf_vsi_setup - Set up a VF VSI
817 * @vf: VF to setup VSI for
819 * Returns pointer to the successfully allocated VSI struct on success,
820 * otherwise returns NULL on failure.
822 static struct ice_vsi *ice_vf_vsi_setup(struct ice_vf *vf)
824 struct ice_port_info *pi = ice_vf_get_port_info(vf);
825 struct ice_pf *pf = vf->pf;
828 vsi = ice_vsi_setup(pf, pi, ICE_VSI_VF, vf->vf_id);
831 dev_err(ice_pf_to_dev(pf), "Failed to create VF VSI\n");
832 ice_vf_invalidate_vsi(vf);
836 vf->lan_vsi_idx = vsi->idx;
837 vf->lan_vsi_num = vsi->vsi_num;
843 * ice_vf_ctrl_vsi_setup - Set up a VF control VSI
844 * @vf: VF to setup control VSI for
846 * Returns pointer to the successfully allocated VSI struct on success,
847 * otherwise returns NULL on failure.
849 struct ice_vsi *ice_vf_ctrl_vsi_setup(struct ice_vf *vf)
851 struct ice_port_info *pi = ice_vf_get_port_info(vf);
852 struct ice_pf *pf = vf->pf;
855 vsi = ice_vsi_setup(pf, pi, ICE_VSI_CTRL, vf->vf_id);
857 dev_err(ice_pf_to_dev(pf), "Failed to create VF control VSI\n");
858 ice_vf_ctrl_invalidate_vsi(vf);
865 * ice_calc_vf_first_vector_idx - Calculate MSIX vector index in the PF space
866 * @pf: pointer to PF structure
867 * @vf: pointer to VF that the first MSIX vector index is being calculated for
869 * This returns the first MSIX vector index in PF space that is used by this VF.
870 * This index is used when accessing PF relative registers such as
871 * GLINT_VECT2FUNC and GLINT_DYN_CTL.
872 * This will always be the OICR index in the AVF driver so any functionality
873 * using vf->first_vector_idx for queue configuration will have to increment by
874 * 1 to avoid meddling with the OICR index.
876 static int ice_calc_vf_first_vector_idx(struct ice_pf *pf, struct ice_vf *vf)
878 return pf->sriov_base_vector + vf->vf_id * pf->num_msix_per_vf;
882 * ice_vf_rebuild_host_vlan_cfg - add VLAN 0 filter or rebuild the Port VLAN
883 * @vf: VF to add MAC filters for
885 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
886 * always re-adds either a VLAN 0 or port VLAN based filter after reset.
888 static int ice_vf_rebuild_host_vlan_cfg(struct ice_vf *vf)
890 struct device *dev = ice_pf_to_dev(vf->pf);
891 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
895 if (vf->port_vlan_info) {
896 err = ice_vsi_manage_pvid(vsi, vf->port_vlan_info, true);
898 dev_err(dev, "failed to configure port VLAN via VSI parameters for VF %u, error %d\n",
903 vlan_id = vf->port_vlan_info & VLAN_VID_MASK;
906 /* vlan_id will either be 0 or the port VLAN number */
907 err = ice_vsi_add_vlan(vsi, vlan_id, ICE_FWD_TO_VSI);
909 dev_err(dev, "failed to add %s VLAN %u filter for VF %u, error %d\n",
910 vf->port_vlan_info ? "port" : "", vlan_id, vf->vf_id,
919 * ice_vf_rebuild_host_mac_cfg - add broadcast and the VF's perm_addr/LAA
920 * @vf: VF to add MAC filters for
922 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
923 * always re-adds a broadcast filter and the VF's perm_addr/LAA after reset.
925 static int ice_vf_rebuild_host_mac_cfg(struct ice_vf *vf)
927 struct device *dev = ice_pf_to_dev(vf->pf);
928 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
929 enum ice_status status;
930 u8 broadcast[ETH_ALEN];
932 eth_broadcast_addr(broadcast);
933 status = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
935 dev_err(dev, "failed to add broadcast MAC filter for VF %u, error %s\n",
936 vf->vf_id, ice_stat_str(status));
937 return ice_status_to_errno(status);
942 if (is_valid_ether_addr(vf->dflt_lan_addr.addr)) {
943 status = ice_fltr_add_mac(vsi, vf->dflt_lan_addr.addr,
946 dev_err(dev, "failed to add default unicast MAC filter %pM for VF %u, error %s\n",
947 &vf->dflt_lan_addr.addr[0], vf->vf_id,
948 ice_stat_str(status));
949 return ice_status_to_errno(status);
958 * ice_vf_set_host_trust_cfg - set trust setting based on pre-reset value
959 * @vf: VF to configure trust setting for
961 static void ice_vf_set_host_trust_cfg(struct ice_vf *vf)
964 set_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
966 clear_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
970 * ice_ena_vf_msix_mappings - enable VF MSIX mappings in hardware
971 * @vf: VF to enable MSIX mappings for
973 * Some of the registers need to be indexed/configured using hardware global
974 * device values and other registers need 0-based values, which represent PF
977 static void ice_ena_vf_msix_mappings(struct ice_vf *vf)
979 int device_based_first_msix, device_based_last_msix;
980 int pf_based_first_msix, pf_based_last_msix, v;
981 struct ice_pf *pf = vf->pf;
982 int device_based_vf_id;
987 pf_based_first_msix = vf->first_vector_idx;
988 pf_based_last_msix = (pf_based_first_msix + pf->num_msix_per_vf) - 1;
990 device_based_first_msix = pf_based_first_msix +
991 pf->hw.func_caps.common_cap.msix_vector_first_id;
992 device_based_last_msix =
993 (device_based_first_msix + pf->num_msix_per_vf) - 1;
994 device_based_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
996 reg = (((device_based_first_msix << VPINT_ALLOC_FIRST_S) &
997 VPINT_ALLOC_FIRST_M) |
998 ((device_based_last_msix << VPINT_ALLOC_LAST_S) &
999 VPINT_ALLOC_LAST_M) | VPINT_ALLOC_VALID_M);
1000 wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
1002 reg = (((device_based_first_msix << VPINT_ALLOC_PCI_FIRST_S)
1003 & VPINT_ALLOC_PCI_FIRST_M) |
1004 ((device_based_last_msix << VPINT_ALLOC_PCI_LAST_S) &
1005 VPINT_ALLOC_PCI_LAST_M) | VPINT_ALLOC_PCI_VALID_M);
1006 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg);
1008 /* map the interrupts to its functions */
1009 for (v = pf_based_first_msix; v <= pf_based_last_msix; v++) {
1010 reg = (((device_based_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
1011 GLINT_VECT2FUNC_VF_NUM_M) |
1012 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
1013 GLINT_VECT2FUNC_PF_NUM_M));
1014 wr32(hw, GLINT_VECT2FUNC(v), reg);
1017 /* Map mailbox interrupt to VF MSI-X vector 0 */
1018 wr32(hw, VPINT_MBX_CTL(device_based_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M);
1022 * ice_ena_vf_q_mappings - enable Rx/Tx queue mappings for a VF
1023 * @vf: VF to enable the mappings for
1024 * @max_txq: max Tx queues allowed on the VF's VSI
1025 * @max_rxq: max Rx queues allowed on the VF's VSI
1027 static void ice_ena_vf_q_mappings(struct ice_vf *vf, u16 max_txq, u16 max_rxq)
1029 struct device *dev = ice_pf_to_dev(vf->pf);
1030 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1031 struct ice_hw *hw = &vf->pf->hw;
1034 /* set regardless of mapping mode */
1035 wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M);
1037 /* VF Tx queues allocation */
1038 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
1039 /* set the VF PF Tx queue range
1040 * VFNUMQ value should be set to (number of queues - 1). A value
1041 * of 0 means 1 queue and a value of 255 means 256 queues
1043 reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
1044 VPLAN_TX_QBASE_VFFIRSTQ_M) |
1045 (((max_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
1046 VPLAN_TX_QBASE_VFNUMQ_M));
1047 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
1049 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
1052 /* set regardless of mapping mode */
1053 wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M);
1055 /* VF Rx queues allocation */
1056 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
1057 /* set the VF PF Rx queue range
1058 * VFNUMQ value should be set to (number of queues - 1). A value
1059 * of 0 means 1 queue and a value of 255 means 256 queues
1061 reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
1062 VPLAN_RX_QBASE_VFFIRSTQ_M) |
1063 (((max_rxq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
1064 VPLAN_RX_QBASE_VFNUMQ_M));
1065 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
1067 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
1072 * ice_ena_vf_mappings - enable VF MSIX and queue mapping
1073 * @vf: pointer to the VF structure
1075 static void ice_ena_vf_mappings(struct ice_vf *vf)
1077 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1079 ice_ena_vf_msix_mappings(vf);
1080 ice_ena_vf_q_mappings(vf, vsi->alloc_txq, vsi->alloc_rxq);
1085 * @pf: pointer to the PF structure
1086 * @avail_res: available resources in the PF structure
1087 * @max_res: maximum resources that can be given per VF
1088 * @min_res: minimum resources that can be given per VF
1090 * Returns non-zero value if resources (queues/vectors) are available or
1091 * returns zero if PF cannot accommodate for all num_alloc_vfs.
1094 ice_determine_res(struct ice_pf *pf, u16 avail_res, u16 max_res, u16 min_res)
1096 bool checked_min_res = false;
1099 /* start by checking if PF can assign max number of resources for
1100 * all num_alloc_vfs.
1101 * if yes, return number per VF
1102 * If no, divide by 2 and roundup, check again
1103 * repeat the loop till we reach a point where even minimum resources
1104 * are not available, in that case return 0
1107 while ((res >= min_res) && !checked_min_res) {
1110 num_all_res = pf->num_alloc_vfs * res;
1111 if (num_all_res <= avail_res)
1115 checked_min_res = true;
1117 res = DIV_ROUND_UP(res, 2);
1123 * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space
1124 * @vf: VF to calculate the register index for
1125 * @q_vector: a q_vector associated to the VF
1127 int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector)
1131 if (!vf || !q_vector)
1136 /* always add one to account for the OICR being the first MSIX */
1137 return pf->sriov_base_vector + pf->num_msix_per_vf * vf->vf_id +
1138 q_vector->v_idx + 1;
1142 * ice_get_max_valid_res_idx - Get the max valid resource index
1143 * @res: pointer to the resource to find the max valid index for
1145 * Start from the end of the ice_res_tracker and return right when we find the
1146 * first res->list entry with the ICE_RES_VALID_BIT set. This function is only
1147 * valid for SR-IOV because it is the only consumer that manipulates the
1148 * res->end and this is always called when res->end is set to res->num_entries.
1150 static int ice_get_max_valid_res_idx(struct ice_res_tracker *res)
1157 for (i = res->num_entries - 1; i >= 0; i--)
1158 if (res->list[i] & ICE_RES_VALID_BIT)
1165 * ice_sriov_set_msix_res - Set any used MSIX resources
1166 * @pf: pointer to PF structure
1167 * @num_msix_needed: number of MSIX vectors needed for all SR-IOV VFs
1169 * This function allows SR-IOV resources to be taken from the end of the PF's
1170 * allowed HW MSIX vectors so that the irq_tracker will not be affected. We
1171 * just set the pf->sriov_base_vector and return success.
1173 * If there are not enough resources available, return an error. This should
1174 * always be caught by ice_set_per_vf_res().
1176 * Return 0 on success, and -EINVAL when there are not enough MSIX vectors
1177 * in the PF's space available for SR-IOV.
1179 static int ice_sriov_set_msix_res(struct ice_pf *pf, u16 num_msix_needed)
1181 u16 total_vectors = pf->hw.func_caps.common_cap.num_msix_vectors;
1182 int vectors_used = pf->irq_tracker->num_entries;
1183 int sriov_base_vector;
1185 sriov_base_vector = total_vectors - num_msix_needed;
1187 /* make sure we only grab irq_tracker entries from the list end and
1188 * that we have enough available MSIX vectors
1190 if (sriov_base_vector < vectors_used)
1193 pf->sriov_base_vector = sriov_base_vector;
1199 * ice_set_per_vf_res - check if vectors and queues are available
1200 * @pf: pointer to the PF structure
1202 * First, determine HW interrupts from common pool. If we allocate fewer VFs, we
1203 * get more vectors and can enable more queues per VF. Note that this does not
1204 * grab any vectors from the SW pool already allocated. Also note, that all
1205 * vector counts include one for each VF's miscellaneous interrupt vector
1208 * Minimum VFs - 2 vectors, 1 queue pair
1209 * Small VFs - 5 vectors, 4 queue pairs
1210 * Medium VFs - 17 vectors, 16 queue pairs
1212 * Second, determine number of queue pairs per VF by starting with a pre-defined
1213 * maximum each VF supports. If this is not possible, then we adjust based on
1214 * queue pairs available on the device.
1216 * Lastly, set queue and MSI-X VF variables tracked by the PF so it can be used
1217 * by each VF during VF initialization and reset.
1219 static int ice_set_per_vf_res(struct ice_pf *pf)
1221 int max_valid_res_idx = ice_get_max_valid_res_idx(pf->irq_tracker);
1222 int msix_avail_per_vf, msix_avail_for_sriov;
1223 struct device *dev = ice_pf_to_dev(pf);
1224 u16 num_msix_per_vf, num_txq, num_rxq;
1226 if (!pf->num_alloc_vfs || max_valid_res_idx < 0)
1229 /* determine MSI-X resources per VF */
1230 msix_avail_for_sriov = pf->hw.func_caps.common_cap.num_msix_vectors -
1231 pf->irq_tracker->num_entries;
1232 msix_avail_per_vf = msix_avail_for_sriov / pf->num_alloc_vfs;
1233 if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MED) {
1234 num_msix_per_vf = ICE_NUM_VF_MSIX_MED;
1235 } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_SMALL) {
1236 num_msix_per_vf = ICE_NUM_VF_MSIX_SMALL;
1237 } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MULTIQ_MIN) {
1238 num_msix_per_vf = ICE_NUM_VF_MSIX_MULTIQ_MIN;
1239 } else if (msix_avail_per_vf >= ICE_MIN_INTR_PER_VF) {
1240 num_msix_per_vf = ICE_MIN_INTR_PER_VF;
1242 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",
1243 msix_avail_for_sriov, ICE_MIN_INTR_PER_VF,
1248 /* determine queue resources per VF */
1249 num_txq = ice_determine_res(pf, ice_get_avail_txq_count(pf),
1251 num_msix_per_vf - ICE_NONQ_VECS_VF,
1252 ICE_MAX_RSS_QS_PER_VF),
1255 num_rxq = ice_determine_res(pf, ice_get_avail_rxq_count(pf),
1257 num_msix_per_vf - ICE_NONQ_VECS_VF,
1258 ICE_MAX_RSS_QS_PER_VF),
1261 if (!num_txq || !num_rxq) {
1262 dev_err(dev, "Not enough queues to support minimum of %d queue pairs per VF for %d VFs\n",
1263 ICE_MIN_QS_PER_VF, pf->num_alloc_vfs);
1267 if (ice_sriov_set_msix_res(pf, num_msix_per_vf * pf->num_alloc_vfs)) {
1268 dev_err(dev, "Unable to set MSI-X resources for %d VFs\n",
1273 /* only allow equal Tx/Rx queue count (i.e. queue pairs) */
1274 pf->num_qps_per_vf = min_t(int, num_txq, num_rxq);
1275 pf->num_msix_per_vf = num_msix_per_vf;
1276 dev_info(dev, "Enabling %d VFs with %d vectors and %d queues per VF\n",
1277 pf->num_alloc_vfs, pf->num_msix_per_vf, pf->num_qps_per_vf);
1283 * ice_clear_vf_reset_trigger - enable VF to access hardware
1284 * @vf: VF to enabled hardware access for
1286 static void ice_clear_vf_reset_trigger(struct ice_vf *vf)
1288 struct ice_hw *hw = &vf->pf->hw;
1291 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
1292 reg &= ~VPGEN_VFRTRIG_VFSWR_M;
1293 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
1298 * ice_vf_set_vsi_promisc - set given VF VSI to given promiscuous mode(s)
1299 * @vf: pointer to the VF info
1300 * @vsi: the VSI being configured
1301 * @promisc_m: mask of promiscuous config bits
1302 * @rm_promisc: promisc flag request from the VF to remove or add filter
1304 * This function configures VF VSI promiscuous mode, based on the VF requests,
1305 * for Unicast, Multicast and VLAN
1307 static enum ice_status
1308 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m,
1311 struct ice_pf *pf = vf->pf;
1312 enum ice_status status = 0;
1316 if (vsi->num_vlan) {
1317 status = ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_m,
1319 } else if (vf->port_vlan_info) {
1321 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
1322 vf->port_vlan_info);
1324 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
1325 vf->port_vlan_info);
1328 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
1331 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
1338 static void ice_vf_clear_counters(struct ice_vf *vf)
1340 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1344 memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
1345 memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
1349 * ice_vf_pre_vsi_rebuild - tasks to be done prior to VSI rebuild
1350 * @vf: VF to perform pre VSI rebuild tasks
1352 * These tasks are items that don't need to be amortized since they are most
1353 * likely called in a for loop with all VF(s) in the reset_all_vfs() case.
1355 static void ice_vf_pre_vsi_rebuild(struct ice_vf *vf)
1357 ice_vf_clear_counters(vf);
1358 ice_clear_vf_reset_trigger(vf);
1362 * ice_vf_rebuild_aggregator_node_cfg - rebuild aggregator node config
1363 * @vsi: Pointer to VSI
1365 * This function moves VSI into corresponding scheduler aggregator node
1366 * based on cached value of "aggregator node info" per VSI
1368 static void ice_vf_rebuild_aggregator_node_cfg(struct ice_vsi *vsi)
1370 struct ice_pf *pf = vsi->back;
1371 enum ice_status status;
1377 dev = ice_pf_to_dev(pf);
1378 if (vsi->agg_node->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
1380 "agg_id %u already has reached max_num_vsis %u\n",
1381 vsi->agg_node->agg_id, vsi->agg_node->num_vsis);
1385 status = ice_move_vsi_to_agg(pf->hw.port_info, vsi->agg_node->agg_id,
1386 vsi->idx, vsi->tc_cfg.ena_tc);
1388 dev_dbg(dev, "unable to move VSI idx %u into aggregator %u node",
1389 vsi->idx, vsi->agg_node->agg_id);
1391 vsi->agg_node->num_vsis++;
1395 * ice_vf_rebuild_host_cfg - host admin configuration is persistent across reset
1396 * @vf: VF to rebuild host configuration on
1398 static void ice_vf_rebuild_host_cfg(struct ice_vf *vf)
1400 struct device *dev = ice_pf_to_dev(vf->pf);
1401 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1403 ice_vf_set_host_trust_cfg(vf);
1405 if (ice_vf_rebuild_host_mac_cfg(vf))
1406 dev_err(dev, "failed to rebuild default MAC configuration for VF %d\n",
1409 if (ice_vf_rebuild_host_vlan_cfg(vf))
1410 dev_err(dev, "failed to rebuild VLAN configuration for VF %u\n",
1412 /* rebuild aggregator node config for main VF VSI */
1413 ice_vf_rebuild_aggregator_node_cfg(vsi);
1417 * ice_vf_rebuild_vsi_with_release - release and setup the VF's VSI
1418 * @vf: VF to release and setup the VSI for
1420 * This is only called when a single VF is being reset (i.e. VFR, VFLR, host VF
1421 * configuration change, etc.).
1423 static int ice_vf_rebuild_vsi_with_release(struct ice_vf *vf)
1425 ice_vf_vsi_release(vf);
1426 if (!ice_vf_vsi_setup(vf))
1433 * ice_vf_rebuild_vsi - rebuild the VF's VSI
1434 * @vf: VF to rebuild the VSI for
1436 * This is only called when all VF(s) are being reset (i.e. PCIe Reset on the
1437 * host, PFR, CORER, etc.).
1439 static int ice_vf_rebuild_vsi(struct ice_vf *vf)
1441 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1442 struct ice_pf *pf = vf->pf;
1444 if (ice_vsi_rebuild(vsi, true)) {
1445 dev_err(ice_pf_to_dev(pf), "failed to rebuild VF %d VSI\n",
1449 /* vsi->idx will remain the same in this case so don't update
1452 vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
1453 vf->lan_vsi_num = vsi->vsi_num;
1459 * ice_vf_set_initialized - VF is ready for VIRTCHNL communication
1460 * @vf: VF to set in initialized state
1462 * After this function the VF will be ready to receive/handle the
1463 * VIRTCHNL_OP_GET_VF_RESOURCES message
1465 static void ice_vf_set_initialized(struct ice_vf *vf)
1467 ice_set_vf_state_qs_dis(vf);
1468 clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
1469 clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
1470 clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
1471 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
1475 * ice_vf_post_vsi_rebuild - tasks to do after the VF's VSI have been rebuilt
1476 * @vf: VF to perform tasks on
1478 static void ice_vf_post_vsi_rebuild(struct ice_vf *vf)
1480 struct ice_pf *pf = vf->pf;
1485 ice_vf_rebuild_host_cfg(vf);
1487 ice_vf_set_initialized(vf);
1488 ice_ena_vf_mappings(vf);
1489 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1493 * ice_reset_all_vfs - reset all allocated VFs in one go
1494 * @pf: pointer to the PF structure
1495 * @is_vflr: true if VFLR was issued, false if not
1497 * First, tell the hardware to reset each VF, then do all the waiting in one
1498 * chunk, and finally finish restoring each VF after the wait. This is useful
1499 * during PF routines which need to reset all VFs, as otherwise it must perform
1500 * these resets in a serialized fashion.
1502 * Returns true if any VFs were reset, and false otherwise.
1504 bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr)
1506 struct device *dev = ice_pf_to_dev(pf);
1507 struct ice_hw *hw = &pf->hw;
1511 /* If we don't have any VFs, then there is nothing to reset */
1512 if (!pf->num_alloc_vfs)
1515 /* clear all malicious info if the VFs are getting reset */
1516 ice_for_each_vf(pf, i)
1517 if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->malvfs, ICE_MAX_VF_COUNT, i))
1518 dev_dbg(dev, "failed to clear malicious VF state for VF %u\n", i);
1520 /* If VFs have been disabled, there is no need to reset */
1521 if (test_and_set_bit(ICE_VF_DIS, pf->state))
1524 /* Begin reset on all VFs at once */
1525 ice_for_each_vf(pf, v)
1526 ice_trigger_vf_reset(&pf->vf[v], is_vflr, true);
1528 /* HW requires some time to make sure it can flush the FIFO for a VF
1529 * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1530 * sequence to make sure that it has completed. We'll keep track of
1531 * the VFs using a simple iterator that increments once that VF has
1532 * finished resetting.
1534 for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1535 /* Check each VF in sequence */
1536 while (v < pf->num_alloc_vfs) {
1540 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1541 if (!(reg & VPGEN_VFRSTAT_VFRD_M)) {
1542 /* only delay if the check failed */
1543 usleep_range(10, 20);
1547 /* If the current VF has finished resetting, move on
1548 * to the next VF in sequence.
1554 /* Display a warning if at least one VF didn't manage to reset in
1555 * time, but continue on with the operation.
1557 if (v < pf->num_alloc_vfs)
1558 dev_warn(dev, "VF reset check timeout\n");
1560 /* free VF resources to begin resetting the VSI state */
1561 ice_for_each_vf(pf, v) {
1564 vf->driver_caps = 0;
1565 ice_vc_set_default_allowlist(vf);
1567 ice_vf_fdir_exit(vf);
1568 /* clean VF control VSI when resetting VFs since it should be
1569 * setup only when VF creates its first FDIR rule.
1571 if (vf->ctrl_vsi_idx != ICE_NO_VSI)
1572 ice_vf_ctrl_invalidate_vsi(vf);
1574 ice_vf_pre_vsi_rebuild(vf);
1575 ice_vf_rebuild_vsi(vf);
1576 ice_vf_post_vsi_rebuild(vf);
1580 clear_bit(ICE_VF_DIS, pf->state);
1586 * ice_is_vf_disabled
1587 * @vf: pointer to the VF info
1589 * Returns true if the PF or VF is disabled, false otherwise.
1591 static bool ice_is_vf_disabled(struct ice_vf *vf)
1593 struct ice_pf *pf = vf->pf;
1595 /* If the PF has been disabled, there is no need resetting VF until
1596 * PF is active again. Similarly, if the VF has been disabled, this
1597 * means something else is resetting the VF, so we shouldn't continue.
1598 * Otherwise, set disable VF state bit for actual reset, and continue.
1600 return (test_bit(ICE_VF_DIS, pf->state) ||
1601 test_bit(ICE_VF_STATE_DIS, vf->vf_states));
1605 * ice_reset_vf - Reset a particular VF
1606 * @vf: pointer to the VF structure
1607 * @is_vflr: true if VFLR was issued, false if not
1609 * Returns true if the VF is currently in reset, resets successfully, or resets
1610 * are disabled and false otherwise.
1612 bool ice_reset_vf(struct ice_vf *vf, bool is_vflr)
1614 struct ice_pf *pf = vf->pf;
1615 struct ice_vsi *vsi;
1623 dev = ice_pf_to_dev(pf);
1625 if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
1626 dev_dbg(dev, "Trying to reset VF %d, but all VF resets are disabled\n",
1631 if (ice_is_vf_disabled(vf)) {
1632 dev_dbg(dev, "VF is already disabled, there is no need for resetting it, telling VM, all is fine %d\n",
1637 /* Set VF disable bit state here, before triggering reset */
1638 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
1639 ice_trigger_vf_reset(vf, is_vflr, false);
1641 vsi = ice_get_vf_vsi(vf);
1643 if (test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states))
1646 /* Call Disable LAN Tx queue AQ whether or not queues are
1647 * enabled. This is needed for successful completion of VFR.
1649 ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
1650 NULL, ICE_VF_RESET, vf->vf_id, NULL);
1653 /* poll VPGEN_VFRSTAT reg to make sure
1654 * that reset is complete
1656 for (i = 0; i < 10; i++) {
1657 /* VF reset requires driver to first reset the VF and then
1658 * poll the status register to make sure that the reset
1659 * completed successfully.
1661 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1662 if (reg & VPGEN_VFRSTAT_VFRD_M) {
1667 /* only sleep if the reset is not done */
1668 usleep_range(10, 20);
1671 vf->driver_caps = 0;
1672 ice_vc_set_default_allowlist(vf);
1674 /* Display a warning if VF didn't manage to reset in time, but need to
1675 * continue on with the operation.
1678 dev_warn(dev, "VF reset check timeout on VF %d\n", vf->vf_id);
1680 /* disable promiscuous modes in case they were enabled
1681 * ignore any error if disabling process failed
1683 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
1684 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
1685 if (vf->port_vlan_info || vsi->num_vlan)
1686 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
1688 promisc_m = ICE_UCAST_PROMISC_BITS;
1690 vsi = ice_get_vf_vsi(vf);
1691 if (ice_vf_set_vsi_promisc(vf, vsi, promisc_m, true))
1692 dev_err(dev, "disabling promiscuous mode failed\n");
1695 ice_vf_fdir_exit(vf);
1696 /* clean VF control VSI when resetting VF since it should be setup
1697 * only when VF creates its first FDIR rule.
1699 if (vf->ctrl_vsi_idx != ICE_NO_VSI)
1700 ice_vf_ctrl_vsi_release(vf);
1702 ice_vf_pre_vsi_rebuild(vf);
1703 ice_vf_rebuild_vsi_with_release(vf);
1704 ice_vf_post_vsi_rebuild(vf);
1706 /* if the VF has been reset allow it to come up again */
1707 if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->malvfs, ICE_MAX_VF_COUNT, vf->vf_id))
1708 dev_dbg(dev, "failed to clear malicious VF state for VF %u\n", i);
1714 * ice_vc_notify_link_state - Inform all VFs on a PF of link status
1715 * @pf: pointer to the PF structure
1717 void ice_vc_notify_link_state(struct ice_pf *pf)
1721 ice_for_each_vf(pf, i)
1722 ice_vc_notify_vf_link_state(&pf->vf[i]);
1726 * ice_vc_notify_reset - Send pending reset message to all VFs
1727 * @pf: pointer to the PF structure
1729 * indicate a pending reset to all VFs on a given PF
1731 void ice_vc_notify_reset(struct ice_pf *pf)
1733 struct virtchnl_pf_event pfe;
1735 if (!pf->num_alloc_vfs)
1738 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1739 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1740 ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
1741 (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
1745 * ice_vc_notify_vf_reset - Notify VF of a reset event
1746 * @vf: pointer to the VF structure
1748 static void ice_vc_notify_vf_reset(struct ice_vf *vf)
1750 struct virtchnl_pf_event pfe;
1757 if (ice_validate_vf_id(pf, vf->vf_id))
1760 /* Bail out if VF is in disabled state, neither initialized, nor active
1761 * state - otherwise proceed with notifications
1763 if ((!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
1764 !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) ||
1765 test_bit(ICE_VF_STATE_DIS, vf->vf_states))
1768 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1769 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1770 ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, VIRTCHNL_OP_EVENT,
1771 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe, sizeof(pfe),
1776 * ice_init_vf_vsi_res - initialize/setup VF VSI resources
1777 * @vf: VF to initialize/setup the VSI for
1779 * This function creates a VSI for the VF, adds a VLAN 0 filter, and sets up the
1780 * VF VSI's broadcast filter and is only used during initial VF creation.
1782 static int ice_init_vf_vsi_res(struct ice_vf *vf)
1784 struct ice_pf *pf = vf->pf;
1785 u8 broadcast[ETH_ALEN];
1786 enum ice_status status;
1787 struct ice_vsi *vsi;
1791 vf->first_vector_idx = ice_calc_vf_first_vector_idx(pf, vf);
1793 dev = ice_pf_to_dev(pf);
1794 vsi = ice_vf_vsi_setup(vf);
1798 err = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
1800 dev_warn(dev, "Failed to add VLAN 0 filter for VF %d\n",
1805 eth_broadcast_addr(broadcast);
1806 status = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
1808 dev_err(dev, "Failed to add broadcast MAC filter for VF %d, status %s\n",
1809 vf->vf_id, ice_stat_str(status));
1810 err = ice_status_to_errno(status);
1819 ice_vf_vsi_release(vf);
1824 * ice_start_vfs - start VFs so they are ready to be used by SR-IOV
1825 * @pf: PF the VFs are associated with
1827 static int ice_start_vfs(struct ice_pf *pf)
1829 struct ice_hw *hw = &pf->hw;
1832 ice_for_each_vf(pf, i) {
1833 struct ice_vf *vf = &pf->vf[i];
1835 ice_clear_vf_reset_trigger(vf);
1837 retval = ice_init_vf_vsi_res(vf);
1839 dev_err(ice_pf_to_dev(pf), "Failed to initialize VSI resources for VF %d, error %d\n",
1844 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
1845 ice_ena_vf_mappings(vf);
1846 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1853 for (i = i - 1; i >= 0; i--) {
1854 struct ice_vf *vf = &pf->vf[i];
1856 ice_dis_vf_mappings(vf);
1857 ice_vf_vsi_release(vf);
1864 * ice_set_dflt_settings_vfs - set VF defaults during initialization/creation
1865 * @pf: PF holding reference to all VFs for default configuration
1867 static void ice_set_dflt_settings_vfs(struct ice_pf *pf)
1871 ice_for_each_vf(pf, i) {
1872 struct ice_vf *vf = &pf->vf[i];
1876 vf->vf_sw_id = pf->first_sw;
1877 /* assign default capabilities */
1878 set_bit(ICE_VIRTCHNL_VF_CAP_L2, &vf->vf_caps);
1879 vf->spoofchk = true;
1880 vf->num_vf_qs = pf->num_qps_per_vf;
1881 ice_vc_set_default_allowlist(vf);
1883 /* ctrl_vsi_idx will be set to a valid value only when VF
1884 * creates its first fdir rule.
1886 ice_vf_ctrl_invalidate_vsi(vf);
1887 ice_vf_fdir_init(vf);
1892 * ice_alloc_vfs - allocate num_vfs in the PF structure
1893 * @pf: PF to store the allocated VFs in
1894 * @num_vfs: number of VFs to allocate
1896 static int ice_alloc_vfs(struct ice_pf *pf, int num_vfs)
1900 vfs = devm_kcalloc(ice_pf_to_dev(pf), num_vfs, sizeof(*vfs),
1906 pf->num_alloc_vfs = num_vfs;
1912 * ice_ena_vfs - enable VFs so they are ready to be used
1913 * @pf: pointer to the PF structure
1914 * @num_vfs: number of VFs to enable
1916 static int ice_ena_vfs(struct ice_pf *pf, u16 num_vfs)
1918 struct device *dev = ice_pf_to_dev(pf);
1919 struct ice_hw *hw = &pf->hw;
1922 /* Disable global interrupt 0 so we don't try to handle the VFLR. */
1923 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1924 ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
1925 set_bit(ICE_OICR_INTR_DIS, pf->state);
1928 ret = pci_enable_sriov(pf->pdev, num_vfs);
1930 pf->num_alloc_vfs = 0;
1931 goto err_unroll_intr;
1934 ret = ice_alloc_vfs(pf, num_vfs);
1936 goto err_pci_disable_sriov;
1938 if (ice_set_per_vf_res(pf)) {
1939 dev_err(dev, "Not enough resources for %d VFs, try with fewer number of VFs\n",
1942 goto err_unroll_sriov;
1945 ice_set_dflt_settings_vfs(pf);
1947 if (ice_start_vfs(pf)) {
1948 dev_err(dev, "Failed to start VF(s)\n");
1950 goto err_unroll_sriov;
1953 clear_bit(ICE_VF_DIS, pf->state);
1957 devm_kfree(dev, pf->vf);
1959 pf->num_alloc_vfs = 0;
1960 err_pci_disable_sriov:
1961 pci_disable_sriov(pf->pdev);
1963 /* rearm interrupts here */
1964 ice_irq_dynamic_ena(hw, NULL, NULL);
1965 clear_bit(ICE_OICR_INTR_DIS, pf->state);
1970 * ice_pci_sriov_ena - Enable or change number of VFs
1971 * @pf: pointer to the PF structure
1972 * @num_vfs: number of VFs to allocate
1974 * Returns 0 on success and negative on failure
1976 static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
1978 int pre_existing_vfs = pci_num_vf(pf->pdev);
1979 struct device *dev = ice_pf_to_dev(pf);
1982 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1984 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1987 if (num_vfs > pf->num_vfs_supported) {
1988 dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
1989 num_vfs, pf->num_vfs_supported);
1993 dev_info(dev, "Enabling %d VFs\n", num_vfs);
1994 err = ice_ena_vfs(pf, num_vfs);
1996 dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
2000 set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
2005 * ice_check_sriov_allowed - check if SR-IOV is allowed based on various checks
2006 * @pf: PF to enabled SR-IOV on
2008 static int ice_check_sriov_allowed(struct ice_pf *pf)
2010 struct device *dev = ice_pf_to_dev(pf);
2012 if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
2013 dev_err(dev, "This device is not capable of SR-IOV\n");
2017 if (ice_is_safe_mode(pf)) {
2018 dev_err(dev, "SR-IOV cannot be configured - Device is in Safe Mode\n");
2022 if (!ice_pf_state_is_nominal(pf)) {
2023 dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
2031 * ice_sriov_configure - Enable or change number of VFs via sysfs
2032 * @pdev: pointer to a pci_dev structure
2033 * @num_vfs: number of VFs to allocate or 0 to free VFs
2035 * This function is called when the user updates the number of VFs in sysfs. On
2036 * success return whatever num_vfs was set to by the caller. Return negative on
2039 int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
2041 struct ice_pf *pf = pci_get_drvdata(pdev);
2042 struct device *dev = ice_pf_to_dev(pf);
2043 enum ice_status status;
2046 err = ice_check_sriov_allowed(pf);
2051 if (!pci_vfs_assigned(pdev)) {
2052 ice_mbx_deinit_snapshot(&pf->hw);
2055 ice_enable_lag(pf->lag);
2059 dev_err(dev, "can't free VFs because some are assigned to VMs.\n");
2063 status = ice_mbx_init_snapshot(&pf->hw, num_vfs);
2065 return ice_status_to_errno(status);
2067 err = ice_pci_sriov_ena(pf, num_vfs);
2069 ice_mbx_deinit_snapshot(&pf->hw);
2074 ice_disable_lag(pf->lag);
2079 * ice_process_vflr_event - Free VF resources via IRQ calls
2080 * @pf: pointer to the PF structure
2082 * called from the VFLR IRQ handler to
2083 * free up VF resources and state variables
2085 void ice_process_vflr_event(struct ice_pf *pf)
2087 struct ice_hw *hw = &pf->hw;
2091 if (!test_and_clear_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
2095 ice_for_each_vf(pf, vf_id) {
2096 struct ice_vf *vf = &pf->vf[vf_id];
2097 u32 reg_idx, bit_idx;
2099 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
2100 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
2101 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
2102 reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
2103 if (reg & BIT(bit_idx))
2104 /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
2105 ice_reset_vf(vf, true);
2110 * ice_vc_reset_vf - Perform software reset on the VF after informing the AVF
2111 * @vf: pointer to the VF info
2113 static void ice_vc_reset_vf(struct ice_vf *vf)
2115 ice_vc_notify_vf_reset(vf);
2116 ice_reset_vf(vf, false);
2120 * ice_get_vf_from_pfq - get the VF who owns the PF space queue passed in
2121 * @pf: PF used to index all VFs
2122 * @pfq: queue index relative to the PF's function space
2124 * If no VF is found who owns the pfq then return NULL, otherwise return a
2125 * pointer to the VF who owns the pfq
2127 static struct ice_vf *ice_get_vf_from_pfq(struct ice_pf *pf, u16 pfq)
2131 ice_for_each_vf(pf, vf_id) {
2132 struct ice_vf *vf = &pf->vf[vf_id];
2133 struct ice_vsi *vsi;
2136 vsi = ice_get_vf_vsi(vf);
2138 ice_for_each_rxq(vsi, rxq_idx)
2139 if (vsi->rxq_map[rxq_idx] == pfq)
2147 * ice_globalq_to_pfq - convert from global queue index to PF space queue index
2148 * @pf: PF used for conversion
2149 * @globalq: global queue index used to convert to PF space queue index
2151 static u32 ice_globalq_to_pfq(struct ice_pf *pf, u32 globalq)
2153 return globalq - pf->hw.func_caps.common_cap.rxq_first_id;
2157 * ice_vf_lan_overflow_event - handle LAN overflow event for a VF
2158 * @pf: PF that the LAN overflow event happened on
2159 * @event: structure holding the event information for the LAN overflow event
2161 * Determine if the LAN overflow event was caused by a VF queue. If it was not
2162 * caused by a VF, do nothing. If a VF caused this LAN overflow event trigger a
2163 * reset on the offending VF.
2166 ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event)
2168 u32 gldcb_rtctq, queue;
2171 gldcb_rtctq = le32_to_cpu(event->desc.params.lan_overflow.prtdcb_ruptq);
2172 dev_dbg(ice_pf_to_dev(pf), "GLDCB_RTCTQ: 0x%08x\n", gldcb_rtctq);
2174 /* event returns device global Rx queue number */
2175 queue = (gldcb_rtctq & GLDCB_RTCTQ_RXQNUM_M) >>
2176 GLDCB_RTCTQ_RXQNUM_S;
2178 vf = ice_get_vf_from_pfq(pf, ice_globalq_to_pfq(pf, queue));
2182 ice_vc_reset_vf(vf);
2186 * ice_vc_send_msg_to_vf - Send message to VF
2187 * @vf: pointer to the VF info
2188 * @v_opcode: virtual channel opcode
2189 * @v_retval: virtual channel return value
2190 * @msg: pointer to the msg buffer
2191 * @msglen: msg length
2196 ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
2197 enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
2199 enum ice_status aq_ret;
2207 if (ice_validate_vf_id(pf, vf->vf_id))
2210 dev = ice_pf_to_dev(pf);
2212 /* single place to detect unsuccessful return values */
2214 vf->num_inval_msgs++;
2215 dev_info(dev, "VF %d failed opcode %d, retval: %d\n", vf->vf_id,
2216 v_opcode, v_retval);
2217 if (vf->num_inval_msgs > ICE_DFLT_NUM_INVAL_MSGS_ALLOWED) {
2218 dev_err(dev, "Number of invalid messages exceeded for VF %d\n",
2220 dev_err(dev, "Use PF Control I/F to enable the VF\n");
2221 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
2225 vf->num_valid_msgs++;
2226 /* reset the invalid counter, if a valid message is received. */
2227 vf->num_inval_msgs = 0;
2230 aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
2232 if (aq_ret && pf->hw.mailboxq.sq_last_status != ICE_AQ_RC_ENOSYS) {
2233 dev_info(dev, "Unable to send the message to VF %d ret %s aq_err %s\n",
2234 vf->vf_id, ice_stat_str(aq_ret),
2235 ice_aq_str(pf->hw.mailboxq.sq_last_status));
2243 * ice_vc_get_ver_msg
2244 * @vf: pointer to the VF info
2245 * @msg: pointer to the msg buffer
2247 * called from the VF to request the API version used by the PF
2249 static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
2251 struct virtchnl_version_info info = {
2252 VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
2255 vf->vf_ver = *(struct virtchnl_version_info *)msg;
2256 /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
2257 if (VF_IS_V10(&vf->vf_ver))
2258 info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
2260 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
2261 VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
2262 sizeof(struct virtchnl_version_info));
2266 * ice_vc_get_max_frame_size - get max frame size allowed for VF
2267 * @vf: VF used to determine max frame size
2269 * Max frame size is determined based on the current port's max frame size and
2270 * whether a port VLAN is configured on this VF. The VF is not aware whether
2271 * it's in a port VLAN so the PF needs to account for this in max frame size
2272 * checks and sending the max frame size to the VF.
2274 static u16 ice_vc_get_max_frame_size(struct ice_vf *vf)
2276 struct ice_port_info *pi = ice_vf_get_port_info(vf);
2279 max_frame_size = pi->phy.link_info.max_frame_size;
2281 if (vf->port_vlan_info)
2282 max_frame_size -= VLAN_HLEN;
2284 return max_frame_size;
2288 * ice_vc_get_vf_res_msg
2289 * @vf: pointer to the VF info
2290 * @msg: pointer to the msg buffer
2292 * called from the VF to request its resources
2294 static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
2296 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2297 struct virtchnl_vf_resource *vfres = NULL;
2298 struct ice_pf *pf = vf->pf;
2299 struct ice_vsi *vsi;
2303 if (ice_check_vf_init(pf, vf)) {
2304 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2308 len = sizeof(struct virtchnl_vf_resource);
2310 vfres = kzalloc(len, GFP_KERNEL);
2312 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2316 if (VF_IS_V11(&vf->vf_ver))
2317 vf->driver_caps = *(u32 *)msg;
2319 vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
2320 VIRTCHNL_VF_OFFLOAD_RSS_REG |
2321 VIRTCHNL_VF_OFFLOAD_VLAN;
2323 vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
2324 vsi = ice_get_vf_vsi(vf);
2326 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2330 if (!vsi->info.pvid)
2331 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
2333 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2334 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
2336 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
2337 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
2339 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
2342 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_FDIR_PF)
2343 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_FDIR_PF;
2345 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
2346 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
2348 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
2349 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
2351 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
2352 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
2354 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
2355 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
2357 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2358 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
2360 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
2361 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
2363 if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
2364 vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
2366 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF)
2367 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF;
2369 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_USO)
2370 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_USO;
2372 vfres->num_vsis = 1;
2373 /* Tx and Rx queue are equal for VF */
2374 vfres->num_queue_pairs = vsi->num_txq;
2375 vfres->max_vectors = pf->num_msix_per_vf;
2376 vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
2377 vfres->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
2378 vfres->max_mtu = ice_vc_get_max_frame_size(vf);
2380 vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
2381 vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
2382 vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
2383 ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
2384 vf->dflt_lan_addr.addr);
2386 /* match guest capabilities */
2387 vf->driver_caps = vfres->vf_cap_flags;
2389 ice_vc_set_caps_allowlist(vf);
2390 ice_vc_set_working_allowlist(vf);
2392 set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
2395 /* send the response back to the VF */
2396 ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
2404 * ice_vc_reset_vf_msg
2405 * @vf: pointer to the VF info
2407 * called from the VF to reset itself,
2408 * unlike other virtchnl messages, PF driver
2409 * doesn't send the response back to the VF
2411 static void ice_vc_reset_vf_msg(struct ice_vf *vf)
2413 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
2414 ice_reset_vf(vf, false);
2418 * ice_find_vsi_from_id
2419 * @pf: the PF structure to search for the VSI
2420 * @id: ID of the VSI it is searching for
2422 * searches for the VSI with the given ID
2424 static struct ice_vsi *ice_find_vsi_from_id(struct ice_pf *pf, u16 id)
2428 ice_for_each_vsi(pf, i)
2429 if (pf->vsi[i] && pf->vsi[i]->vsi_num == id)
2436 * ice_vc_isvalid_vsi_id
2437 * @vf: pointer to the VF info
2438 * @vsi_id: VF relative VSI ID
2440 * check for the valid VSI ID
2442 bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
2444 struct ice_pf *pf = vf->pf;
2445 struct ice_vsi *vsi;
2447 vsi = ice_find_vsi_from_id(pf, vsi_id);
2449 return (vsi && (vsi->vf_id == vf->vf_id));
2453 * ice_vc_isvalid_q_id
2454 * @vf: pointer to the VF info
2456 * @qid: VSI relative queue ID
2458 * check for the valid queue ID
2460 static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
2462 struct ice_vsi *vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
2463 /* allocated Tx and Rx queues should be always equal for VF VSI */
2464 return (vsi && (qid < vsi->alloc_txq));
2468 * ice_vc_isvalid_ring_len
2469 * @ring_len: length of ring
2471 * check for the valid ring count, should be multiple of ICE_REQ_DESC_MULTIPLE
2474 static bool ice_vc_isvalid_ring_len(u16 ring_len)
2476 return ring_len == 0 ||
2477 (ring_len >= ICE_MIN_NUM_DESC &&
2478 ring_len <= ICE_MAX_NUM_DESC &&
2479 !(ring_len % ICE_REQ_DESC_MULTIPLE));
2483 * ice_vc_parse_rss_cfg - parses hash fields and headers from
2484 * a specific virtchnl RSS cfg
2485 * @hw: pointer to the hardware
2486 * @rss_cfg: pointer to the virtchnl RSS cfg
2487 * @addl_hdrs: pointer to the protocol header fields (ICE_FLOW_SEG_HDR_*)
2489 * @hash_flds: pointer to the hash bit fields (ICE_FLOW_HASH_*) to configure
2491 * Return true if all the protocol header and hash fields in the RSS cfg could
2492 * be parsed, else return false
2494 * This function parses the virtchnl RSS cfg to be the intended
2495 * hash fields and the intended header for RSS configuration
2498 ice_vc_parse_rss_cfg(struct ice_hw *hw, struct virtchnl_rss_cfg *rss_cfg,
2499 u32 *addl_hdrs, u64 *hash_flds)
2501 const struct ice_vc_hash_field_match_type *hf_list;
2502 const struct ice_vc_hdr_match_type *hdr_list;
2503 int i, hf_list_len, hdr_list_len;
2505 if (!strncmp(hw->active_pkg_name, "ICE COMMS Package",
2506 sizeof(hw->active_pkg_name))) {
2507 hf_list = ice_vc_hash_field_list_comms;
2508 hf_list_len = ARRAY_SIZE(ice_vc_hash_field_list_comms);
2509 hdr_list = ice_vc_hdr_list_comms;
2510 hdr_list_len = ARRAY_SIZE(ice_vc_hdr_list_comms);
2512 hf_list = ice_vc_hash_field_list_os;
2513 hf_list_len = ARRAY_SIZE(ice_vc_hash_field_list_os);
2514 hdr_list = ice_vc_hdr_list_os;
2515 hdr_list_len = ARRAY_SIZE(ice_vc_hdr_list_os);
2518 for (i = 0; i < rss_cfg->proto_hdrs.count; i++) {
2519 struct virtchnl_proto_hdr *proto_hdr =
2520 &rss_cfg->proto_hdrs.proto_hdr[i];
2521 bool hdr_found = false;
2524 /* Find matched ice headers according to virtchnl headers. */
2525 for (j = 0; j < hdr_list_len; j++) {
2526 struct ice_vc_hdr_match_type hdr_map = hdr_list[j];
2528 if (proto_hdr->type == hdr_map.vc_hdr) {
2529 *addl_hdrs |= hdr_map.ice_hdr;
2537 /* Find matched ice hash fields according to
2538 * virtchnl hash fields.
2540 for (j = 0; j < hf_list_len; j++) {
2541 struct ice_vc_hash_field_match_type hf_map = hf_list[j];
2543 if (proto_hdr->type == hf_map.vc_hdr &&
2544 proto_hdr->field_selector == hf_map.vc_hash_field) {
2545 *hash_flds |= hf_map.ice_hash_field;
2555 * ice_vf_adv_rss_offload_ena - determine if capabilities support advanced
2557 * @caps: VF driver negotiated capabilities
2559 * Return true if VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF capability is set,
2562 static bool ice_vf_adv_rss_offload_ena(u32 caps)
2564 return !!(caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF);
2568 * ice_vc_handle_rss_cfg
2569 * @vf: pointer to the VF info
2570 * @msg: pointer to the message buffer
2571 * @add: add a RSS config if true, otherwise delete a RSS config
2573 * This function adds/deletes a RSS config
2575 static int ice_vc_handle_rss_cfg(struct ice_vf *vf, u8 *msg, bool add)
2577 u32 v_opcode = add ? VIRTCHNL_OP_ADD_RSS_CFG : VIRTCHNL_OP_DEL_RSS_CFG;
2578 struct virtchnl_rss_cfg *rss_cfg = (struct virtchnl_rss_cfg *)msg;
2579 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2580 struct device *dev = ice_pf_to_dev(vf->pf);
2581 struct ice_hw *hw = &vf->pf->hw;
2582 struct ice_vsi *vsi;
2584 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2585 dev_dbg(dev, "VF %d attempting to configure RSS, but RSS is not supported by the PF\n",
2587 v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
2591 if (!ice_vf_adv_rss_offload_ena(vf->driver_caps)) {
2592 dev_dbg(dev, "VF %d attempting to configure RSS, but Advanced RSS offload is not supported\n",
2594 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2598 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2599 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2603 if (rss_cfg->proto_hdrs.count > VIRTCHNL_MAX_NUM_PROTO_HDRS ||
2604 rss_cfg->rss_algorithm < VIRTCHNL_RSS_ALG_TOEPLITZ_ASYMMETRIC ||
2605 rss_cfg->rss_algorithm > VIRTCHNL_RSS_ALG_XOR_SYMMETRIC) {
2606 dev_dbg(dev, "VF %d attempting to configure RSS, but RSS configuration is not valid\n",
2608 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2612 vsi = ice_get_vf_vsi(vf);
2614 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2618 if (rss_cfg->rss_algorithm == VIRTCHNL_RSS_ALG_R_ASYMMETRIC) {
2619 struct ice_vsi_ctx *ctx;
2620 enum ice_status status;
2621 u8 lut_type, hash_type;
2623 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
2624 hash_type = add ? ICE_AQ_VSI_Q_OPT_RSS_XOR :
2625 ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
2627 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2629 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2633 ctx->info.q_opt_rss = ((lut_type <<
2634 ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
2635 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
2637 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
2639 /* Preserve existing queueing option setting */
2640 ctx->info.q_opt_rss |= (vsi->info.q_opt_rss &
2641 ICE_AQ_VSI_Q_OPT_RSS_GBL_LUT_M);
2642 ctx->info.q_opt_tc = vsi->info.q_opt_tc;
2643 ctx->info.q_opt_flags = vsi->info.q_opt_rss;
2645 ctx->info.valid_sections =
2646 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
2648 status = ice_update_vsi(hw, vsi->idx, ctx, NULL);
2650 dev_err(dev, "update VSI for RSS failed, err %s aq_err %s\n",
2651 ice_stat_str(status),
2652 ice_aq_str(hw->adminq.sq_last_status));
2653 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2655 vsi->info.q_opt_rss = ctx->info.q_opt_rss;
2660 u32 addl_hdrs = ICE_FLOW_SEG_HDR_NONE;
2661 u64 hash_flds = ICE_HASH_INVALID;
2663 if (!ice_vc_parse_rss_cfg(hw, rss_cfg, &addl_hdrs,
2665 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2670 if (ice_add_rss_cfg(hw, vsi->idx, hash_flds,
2672 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2673 dev_err(dev, "ice_add_rss_cfg failed for vsi = %d, v_ret = %d\n",
2674 vsi->vsi_num, v_ret);
2677 enum ice_status status;
2679 status = ice_rem_rss_cfg(hw, vsi->idx, hash_flds,
2681 /* We just ignore ICE_ERR_DOES_NOT_EXIST, because
2682 * if two configurations share the same profile remove
2683 * one of them actually removes both, since the
2684 * profile is deleted.
2686 if (status && status != ICE_ERR_DOES_NOT_EXIST) {
2687 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2688 dev_err(dev, "ice_rem_rss_cfg failed for VF ID:%d, error:%s\n",
2689 vf->vf_id, ice_stat_str(status));
2695 return ice_vc_send_msg_to_vf(vf, v_opcode, v_ret, NULL, 0);
2699 * ice_vc_config_rss_key
2700 * @vf: pointer to the VF info
2701 * @msg: pointer to the msg buffer
2703 * Configure the VF's RSS key
2705 static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
2707 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2708 struct virtchnl_rss_key *vrk =
2709 (struct virtchnl_rss_key *)msg;
2710 struct ice_vsi *vsi;
2712 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2713 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2717 if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
2718 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2722 if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
2723 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2727 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2728 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2732 vsi = ice_get_vf_vsi(vf);
2734 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2738 if (ice_set_rss_key(vsi, vrk->key))
2739 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2741 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
2746 * ice_vc_config_rss_lut
2747 * @vf: pointer to the VF info
2748 * @msg: pointer to the msg buffer
2750 * Configure the VF's RSS LUT
2752 static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
2754 struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
2755 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2756 struct ice_vsi *vsi;
2758 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2759 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2763 if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
2764 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2768 if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE) {
2769 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2773 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2774 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2778 vsi = ice_get_vf_vsi(vf);
2780 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2784 if (ice_set_rss_lut(vsi, vrl->lut, ICE_VSIQF_HLUT_ARRAY_SIZE))
2785 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2787 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
2792 * ice_wait_on_vf_reset - poll to make sure a given VF is ready after reset
2793 * @vf: The VF being resseting
2795 * The max poll time is about ~800ms, which is about the maximum time it takes
2796 * for a VF to be reset and/or a VF driver to be removed.
2798 static void ice_wait_on_vf_reset(struct ice_vf *vf)
2802 for (i = 0; i < ICE_MAX_VF_RESET_TRIES; i++) {
2803 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
2805 msleep(ICE_MAX_VF_RESET_SLEEP_MS);
2810 * ice_check_vf_ready_for_cfg - check if VF is ready to be configured/queried
2811 * @vf: VF to check if it's ready to be configured/queried
2813 * The purpose of this function is to make sure the VF is not in reset, not
2814 * disabled, and initialized so it can be configured and/or queried by a host
2817 static int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
2821 ice_wait_on_vf_reset(vf);
2823 if (ice_is_vf_disabled(vf))
2827 if (ice_check_vf_init(pf, vf))
2834 * ice_set_vf_spoofchk
2835 * @netdev: network interface device structure
2836 * @vf_id: VF identifier
2837 * @ena: flag to enable or disable feature
2839 * Enable or disable VF spoof checking
2841 int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
2843 struct ice_netdev_priv *np = netdev_priv(netdev);
2844 struct ice_pf *pf = np->vsi->back;
2845 struct ice_vsi_ctx *ctx;
2846 struct ice_vsi *vf_vsi;
2847 enum ice_status status;
2852 dev = ice_pf_to_dev(pf);
2853 if (ice_validate_vf_id(pf, vf_id))
2856 vf = &pf->vf[vf_id];
2857 ret = ice_check_vf_ready_for_cfg(vf);
2861 vf_vsi = ice_get_vf_vsi(vf);
2863 netdev_err(netdev, "VSI %d for VF %d is null\n",
2864 vf->lan_vsi_idx, vf->vf_id);
2868 if (vf_vsi->type != ICE_VSI_VF) {
2869 netdev_err(netdev, "Type %d of VSI %d for VF %d is no ICE_VSI_VF\n",
2870 vf_vsi->type, vf_vsi->vsi_num, vf->vf_id);
2874 if (ena == vf->spoofchk) {
2875 dev_dbg(dev, "VF spoofchk already %s\n", ena ? "ON" : "OFF");
2879 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2883 ctx->info.sec_flags = vf_vsi->info.sec_flags;
2884 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
2886 ctx->info.sec_flags |=
2887 ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
2888 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2889 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
2891 ctx->info.sec_flags &=
2892 ~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
2893 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2894 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
2897 status = ice_update_vsi(&pf->hw, vf_vsi->idx, ctx, NULL);
2899 dev_err(dev, "Failed to %sable spoofchk on VF %d VSI %d\n error %s\n",
2900 ena ? "en" : "dis", vf->vf_id, vf_vsi->vsi_num,
2901 ice_stat_str(status));
2906 /* only update spoofchk state and VSI context on success */
2907 vf_vsi->info.sec_flags = ctx->info.sec_flags;
2916 * ice_is_any_vf_in_promisc - check if any VF(s) are in promiscuous mode
2917 * @pf: PF structure for accessing VF(s)
2919 * Return false if no VF(s) are in unicast and/or multicast promiscuous mode,
2922 bool ice_is_any_vf_in_promisc(struct ice_pf *pf)
2926 ice_for_each_vf(pf, vf_idx) {
2927 struct ice_vf *vf = &pf->vf[vf_idx];
2929 /* found a VF that has promiscuous mode configured */
2930 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2931 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
2939 * ice_vc_cfg_promiscuous_mode_msg
2940 * @vf: pointer to the VF info
2941 * @msg: pointer to the msg buffer
2943 * called from the VF to configure VF VSIs promiscuous mode
2945 static int ice_vc_cfg_promiscuous_mode_msg(struct ice_vf *vf, u8 *msg)
2947 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2948 bool rm_promisc, alluni = false, allmulti = false;
2949 struct virtchnl_promisc_info *info =
2950 (struct virtchnl_promisc_info *)msg;
2951 struct ice_pf *pf = vf->pf;
2952 struct ice_vsi *vsi;
2956 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2957 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2961 if (!ice_vc_isvalid_vsi_id(vf, info->vsi_id)) {
2962 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2966 vsi = ice_get_vf_vsi(vf);
2968 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2972 dev = ice_pf_to_dev(pf);
2973 if (!test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
2974 dev_err(dev, "Unprivileged VF %d is attempting to configure promiscuous mode\n",
2976 /* Leave v_ret alone, lie to the VF on purpose. */
2980 if (info->flags & FLAG_VF_UNICAST_PROMISC)
2983 if (info->flags & FLAG_VF_MULTICAST_PROMISC)
2986 rm_promisc = !allmulti && !alluni;
2988 if (vsi->num_vlan || vf->port_vlan_info) {
2989 struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
2990 struct net_device *pf_netdev;
2993 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2997 pf_netdev = pf_vsi->netdev;
2999 ret = ice_set_vf_spoofchk(pf_netdev, vf->vf_id, rm_promisc);
3001 dev_err(dev, "Failed to update spoofchk to %s for VF %d VSI %d when setting promiscuous mode\n",
3002 rm_promisc ? "ON" : "OFF", vf->vf_id,
3004 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3007 ret = ice_cfg_vlan_pruning(vsi, true, !rm_promisc);
3009 dev_err(dev, "Failed to configure VLAN pruning in promiscuous mode\n");
3010 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3015 if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
3016 bool set_dflt_vsi = alluni || allmulti;
3018 if (set_dflt_vsi && !ice_is_dflt_vsi_in_use(pf->first_sw))
3019 /* only attempt to set the default forwarding VSI if
3020 * it's not currently set
3022 ret = ice_set_dflt_vsi(pf->first_sw, vsi);
3023 else if (!set_dflt_vsi &&
3024 ice_is_vsi_dflt_vsi(pf->first_sw, vsi))
3025 /* only attempt to free the default forwarding VSI if we
3028 ret = ice_clear_dflt_vsi(pf->first_sw);
3031 dev_err(dev, "%sable VF %d as the default VSI failed, error %d\n",
3032 set_dflt_vsi ? "en" : "dis", vf->vf_id, ret);
3033 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
3037 enum ice_status status;
3041 if (vf->port_vlan_info || vsi->num_vlan)
3042 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
3044 promisc_m = ICE_UCAST_PROMISC_BITS;
3045 } else if (allmulti) {
3046 if (vf->port_vlan_info || vsi->num_vlan)
3047 promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
3049 promisc_m = ICE_MCAST_PROMISC_BITS;
3051 if (vf->port_vlan_info || vsi->num_vlan)
3052 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
3054 promisc_m = ICE_UCAST_PROMISC_BITS;
3057 /* Configure multicast/unicast with or without VLAN promiscuous
3060 status = ice_vf_set_vsi_promisc(vf, vsi, promisc_m, rm_promisc);
3062 dev_err(dev, "%sable Tx/Rx filter promiscuous mode on VF-%d failed, error: %s\n",
3063 rm_promisc ? "dis" : "en", vf->vf_id,
3064 ice_stat_str(status));
3065 v_ret = ice_err_to_virt_err(status);
3068 dev_dbg(dev, "%sable Tx/Rx filter promiscuous mode on VF-%d succeeded\n",
3069 rm_promisc ? "dis" : "en", vf->vf_id);
3074 !test_and_set_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
3075 dev_info(dev, "VF %u successfully set multicast promiscuous mode\n", vf->vf_id);
3076 else if (!allmulti && test_and_clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
3077 dev_info(dev, "VF %u successfully unset multicast promiscuous mode\n", vf->vf_id);
3079 if (alluni && !test_and_set_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
3080 dev_info(dev, "VF %u successfully set unicast promiscuous mode\n", vf->vf_id);
3081 else if (!alluni && test_and_clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
3082 dev_info(dev, "VF %u successfully unset unicast promiscuous mode\n", vf->vf_id);
3085 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
3090 * ice_vc_get_stats_msg
3091 * @vf: pointer to the VF info
3092 * @msg: pointer to the msg buffer
3094 * called from the VF to get VSI stats
3096 static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
3098 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3099 struct virtchnl_queue_select *vqs =
3100 (struct virtchnl_queue_select *)msg;
3101 struct ice_eth_stats stats = { 0 };
3102 struct ice_vsi *vsi;
3104 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3105 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3109 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3110 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3114 vsi = ice_get_vf_vsi(vf);
3116 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3120 ice_update_eth_stats(vsi);
3122 stats = vsi->eth_stats;
3125 /* send the response to the VF */
3126 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
3127 (u8 *)&stats, sizeof(stats));
3131 * ice_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTCHNL
3132 * @vqs: virtchnl_queue_select structure containing bitmaps to validate
3134 * Return true on successful validation, else false
3136 static bool ice_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
3138 if ((!vqs->rx_queues && !vqs->tx_queues) ||
3139 vqs->rx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF) ||
3140 vqs->tx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF))
3147 * ice_vf_ena_txq_interrupt - enable Tx queue interrupt via QINT_TQCTL
3148 * @vsi: VSI of the VF to configure
3149 * @q_idx: VF queue index used to determine the queue in the PF's space
3151 static void ice_vf_ena_txq_interrupt(struct ice_vsi *vsi, u32 q_idx)
3153 struct ice_hw *hw = &vsi->back->hw;
3154 u32 pfq = vsi->txq_map[q_idx];
3157 reg = rd32(hw, QINT_TQCTL(pfq));
3159 /* MSI-X index 0 in the VF's space is always for the OICR, which means
3160 * this is most likely a poll mode VF driver, so don't enable an
3161 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
3163 if (!(reg & QINT_TQCTL_MSIX_INDX_M))
3166 wr32(hw, QINT_TQCTL(pfq), reg | QINT_TQCTL_CAUSE_ENA_M);
3170 * ice_vf_ena_rxq_interrupt - enable Tx queue interrupt via QINT_RQCTL
3171 * @vsi: VSI of the VF to configure
3172 * @q_idx: VF queue index used to determine the queue in the PF's space
3174 static void ice_vf_ena_rxq_interrupt(struct ice_vsi *vsi, u32 q_idx)
3176 struct ice_hw *hw = &vsi->back->hw;
3177 u32 pfq = vsi->rxq_map[q_idx];
3180 reg = rd32(hw, QINT_RQCTL(pfq));
3182 /* MSI-X index 0 in the VF's space is always for the OICR, which means
3183 * this is most likely a poll mode VF driver, so don't enable an
3184 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
3186 if (!(reg & QINT_RQCTL_MSIX_INDX_M))
3189 wr32(hw, QINT_RQCTL(pfq), reg | QINT_RQCTL_CAUSE_ENA_M);
3194 * @vf: pointer to the VF info
3195 * @msg: pointer to the msg buffer
3197 * called from the VF to enable all or specific queue(s)
3199 static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
3201 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3202 struct virtchnl_queue_select *vqs =
3203 (struct virtchnl_queue_select *)msg;
3204 struct ice_vsi *vsi;
3205 unsigned long q_map;
3208 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3209 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3213 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3214 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3218 if (!ice_vc_validate_vqs_bitmaps(vqs)) {
3219 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3223 vsi = ice_get_vf_vsi(vf);
3225 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3229 /* Enable only Rx rings, Tx rings were enabled by the FW when the
3230 * Tx queue group list was configured and the context bits were
3231 * programmed using ice_vsi_cfg_txqs
3233 q_map = vqs->rx_queues;
3234 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3235 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3236 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3240 /* Skip queue if enabled */
3241 if (test_bit(vf_q_id, vf->rxq_ena))
3244 if (ice_vsi_ctrl_one_rx_ring(vsi, true, vf_q_id, true)) {
3245 dev_err(ice_pf_to_dev(vsi->back), "Failed to enable Rx ring %d on VSI %d\n",
3246 vf_q_id, vsi->vsi_num);
3247 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3251 ice_vf_ena_rxq_interrupt(vsi, vf_q_id);
3252 set_bit(vf_q_id, vf->rxq_ena);
3255 q_map = vqs->tx_queues;
3256 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3257 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3258 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3262 /* Skip queue if enabled */
3263 if (test_bit(vf_q_id, vf->txq_ena))
3266 ice_vf_ena_txq_interrupt(vsi, vf_q_id);
3267 set_bit(vf_q_id, vf->txq_ena);
3270 /* Set flag to indicate that queues are enabled */
3271 if (v_ret == VIRTCHNL_STATUS_SUCCESS)
3272 set_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
3275 /* send the response to the VF */
3276 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
3282 * @vf: pointer to the VF info
3283 * @msg: pointer to the msg buffer
3285 * called from the VF to disable all or specific
3288 static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
3290 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3291 struct virtchnl_queue_select *vqs =
3292 (struct virtchnl_queue_select *)msg;
3293 struct ice_vsi *vsi;
3294 unsigned long q_map;
3297 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
3298 !test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states)) {
3299 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3303 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3304 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3308 if (!ice_vc_validate_vqs_bitmaps(vqs)) {
3309 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3313 vsi = ice_get_vf_vsi(vf);
3315 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3319 if (vqs->tx_queues) {
3320 q_map = vqs->tx_queues;
3322 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3323 struct ice_ring *ring = vsi->tx_rings[vf_q_id];
3324 struct ice_txq_meta txq_meta = { 0 };
3326 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3327 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3331 /* Skip queue if not enabled */
3332 if (!test_bit(vf_q_id, vf->txq_ena))
3335 ice_fill_txq_meta(vsi, ring, &txq_meta);
3337 if (ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, vf->vf_id,
3339 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Tx ring %d on VSI %d\n",
3340 vf_q_id, vsi->vsi_num);
3341 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3345 /* Clear enabled queues flag */
3346 clear_bit(vf_q_id, vf->txq_ena);
3350 q_map = vqs->rx_queues;
3351 /* speed up Rx queue disable by batching them if possible */
3353 bitmap_equal(&q_map, vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF)) {
3354 if (ice_vsi_stop_all_rx_rings(vsi)) {
3355 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop all Rx rings on VSI %d\n",
3357 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3361 bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
3363 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3364 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3365 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3369 /* Skip queue if not enabled */
3370 if (!test_bit(vf_q_id, vf->rxq_ena))
3373 if (ice_vsi_ctrl_one_rx_ring(vsi, false, vf_q_id,
3375 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Rx ring %d on VSI %d\n",
3376 vf_q_id, vsi->vsi_num);
3377 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3381 /* Clear enabled queues flag */
3382 clear_bit(vf_q_id, vf->rxq_ena);
3386 /* Clear enabled queues flag */
3387 if (v_ret == VIRTCHNL_STATUS_SUCCESS && ice_vf_has_no_qs_ena(vf))
3388 clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
3391 /* send the response to the VF */
3392 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
3398 * @vf: pointer to the VF info
3399 * @vsi: the VSI being configured
3400 * @vector_id: vector ID
3401 * @map: vector map for mapping vectors to queues
3402 * @q_vector: structure for interrupt vector
3403 * configure the IRQ to queue map
3406 ice_cfg_interrupt(struct ice_vf *vf, struct ice_vsi *vsi, u16 vector_id,
3407 struct virtchnl_vector_map *map,
3408 struct ice_q_vector *q_vector)
3410 u16 vsi_q_id, vsi_q_id_idx;
3413 q_vector->num_ring_rx = 0;
3414 q_vector->num_ring_tx = 0;
3416 qmap = map->rxq_map;
3417 for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
3418 vsi_q_id = vsi_q_id_idx;
3420 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
3421 return VIRTCHNL_STATUS_ERR_PARAM;
3423 q_vector->num_ring_rx++;
3424 q_vector->rx.itr_idx = map->rxitr_idx;
3425 vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
3426 ice_cfg_rxq_interrupt(vsi, vsi_q_id, vector_id,
3427 q_vector->rx.itr_idx);
3430 qmap = map->txq_map;
3431 for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
3432 vsi_q_id = vsi_q_id_idx;
3434 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
3435 return VIRTCHNL_STATUS_ERR_PARAM;
3437 q_vector->num_ring_tx++;
3438 q_vector->tx.itr_idx = map->txitr_idx;
3439 vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
3440 ice_cfg_txq_interrupt(vsi, vsi_q_id, vector_id,
3441 q_vector->tx.itr_idx);
3444 return VIRTCHNL_STATUS_SUCCESS;
3448 * ice_vc_cfg_irq_map_msg
3449 * @vf: pointer to the VF info
3450 * @msg: pointer to the msg buffer
3452 * called from the VF to configure the IRQ to queue map
3454 static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
3456 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3457 u16 num_q_vectors_mapped, vsi_id, vector_id;
3458 struct virtchnl_irq_map_info *irqmap_info;
3459 struct virtchnl_vector_map *map;
3460 struct ice_pf *pf = vf->pf;
3461 struct ice_vsi *vsi;
3464 irqmap_info = (struct virtchnl_irq_map_info *)msg;
3465 num_q_vectors_mapped = irqmap_info->num_vectors;
3467 /* Check to make sure number of VF vectors mapped is not greater than
3468 * number of VF vectors originally allocated, and check that
3469 * there is actually at least a single VF queue vector mapped
3471 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
3472 pf->num_msix_per_vf < num_q_vectors_mapped ||
3473 !num_q_vectors_mapped) {
3474 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3478 vsi = ice_get_vf_vsi(vf);
3480 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3484 for (i = 0; i < num_q_vectors_mapped; i++) {
3485 struct ice_q_vector *q_vector;
3487 map = &irqmap_info->vecmap[i];
3489 vector_id = map->vector_id;
3490 vsi_id = map->vsi_id;
3491 /* vector_id is always 0-based for each VF, and can never be
3492 * larger than or equal to the max allowed interrupts per VF
3494 if (!(vector_id < pf->num_msix_per_vf) ||
3495 !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
3496 (!vector_id && (map->rxq_map || map->txq_map))) {
3497 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3501 /* No need to map VF miscellaneous or rogue vector */
3505 /* Subtract non queue vector from vector_id passed by VF
3506 * to get actual number of VSI queue vector array index
3508 q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
3510 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3514 /* lookout for the invalid queue index */
3515 v_ret = (enum virtchnl_status_code)
3516 ice_cfg_interrupt(vf, vsi, vector_id, map, q_vector);
3522 /* send the response to the VF */
3523 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
3529 * @vf: pointer to the VF info
3530 * @msg: pointer to the msg buffer
3532 * called from the VF to configure the Rx/Tx queues
3534 static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
3536 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3537 struct virtchnl_vsi_queue_config_info *qci =
3538 (struct virtchnl_vsi_queue_config_info *)msg;
3539 struct virtchnl_queue_pair_info *qpi;
3540 u16 num_rxq = 0, num_txq = 0;
3541 struct ice_pf *pf = vf->pf;
3542 struct ice_vsi *vsi;
3545 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3546 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3550 if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
3551 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3555 vsi = ice_get_vf_vsi(vf);
3557 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3561 if (qci->num_queue_pairs > ICE_MAX_RSS_QS_PER_VF ||
3562 qci->num_queue_pairs > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
3563 dev_err(ice_pf_to_dev(pf), "VF-%d requesting more than supported number of queues: %d\n",
3564 vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
3565 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3569 for (i = 0; i < qci->num_queue_pairs; i++) {
3570 qpi = &qci->qpair[i];
3571 if (qpi->txq.vsi_id != qci->vsi_id ||
3572 qpi->rxq.vsi_id != qci->vsi_id ||
3573 qpi->rxq.queue_id != qpi->txq.queue_id ||
3574 qpi->txq.headwb_enabled ||
3575 !ice_vc_isvalid_ring_len(qpi->txq.ring_len) ||
3576 !ice_vc_isvalid_ring_len(qpi->rxq.ring_len) ||
3577 !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
3578 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3581 /* copy Tx queue info from VF into VSI */
3582 if (qpi->txq.ring_len > 0) {
3584 vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
3585 vsi->tx_rings[i]->count = qpi->txq.ring_len;
3588 /* copy Rx queue info from VF into VSI */
3589 if (qpi->rxq.ring_len > 0) {
3590 u16 max_frame_size = ice_vc_get_max_frame_size(vf);
3593 vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
3594 vsi->rx_rings[i]->count = qpi->rxq.ring_len;
3596 if (qpi->rxq.databuffer_size != 0 &&
3597 (qpi->rxq.databuffer_size > ((16 * 1024) - 128) ||
3598 qpi->rxq.databuffer_size < 1024)) {
3599 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3602 vsi->rx_buf_len = qpi->rxq.databuffer_size;
3603 vsi->rx_rings[i]->rx_buf_len = vsi->rx_buf_len;
3604 if (qpi->rxq.max_pkt_size > max_frame_size ||
3605 qpi->rxq.max_pkt_size < 64) {
3606 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3611 vsi->max_frame = qpi->rxq.max_pkt_size;
3612 /* add space for the port VLAN since the VF driver is not
3613 * expected to account for it in the MTU calculation
3615 if (vf->port_vlan_info)
3616 vsi->max_frame += VLAN_HLEN;
3619 /* VF can request to configure less than allocated queues or default
3620 * allocated queues. So update the VSI with new number
3622 vsi->num_txq = num_txq;
3623 vsi->num_rxq = num_rxq;
3624 /* All queues of VF VSI are in TC 0 */
3625 vsi->tc_cfg.tc_info[0].qcount_tx = num_txq;
3626 vsi->tc_cfg.tc_info[0].qcount_rx = num_rxq;
3628 if (ice_vsi_cfg_lan_txqs(vsi) || ice_vsi_cfg_rxqs(vsi))
3629 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
3632 /* send the response to the VF */
3633 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES, v_ret,
3639 * @vf: pointer to the VF info
3641 static bool ice_is_vf_trusted(struct ice_vf *vf)
3643 return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
3647 * ice_can_vf_change_mac
3648 * @vf: pointer to the VF info
3650 * Return true if the VF is allowed to change its MAC filters, false otherwise
3652 static bool ice_can_vf_change_mac(struct ice_vf *vf)
3654 /* If the VF MAC address has been set administratively (via the
3655 * ndo_set_vf_mac command), then deny permission to the VF to
3656 * add/delete unicast MAC addresses, unless the VF is trusted
3658 if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
3665 * ice_vc_add_mac_addr - attempt to add the MAC address passed in
3666 * @vf: pointer to the VF info
3667 * @vsi: pointer to the VF's VSI
3668 * @mac_addr: MAC address to add
3671 ice_vc_add_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi, u8 *mac_addr)
3673 struct device *dev = ice_pf_to_dev(vf->pf);
3674 enum ice_status status;
3676 /* default unicast MAC already added */
3677 if (ether_addr_equal(mac_addr, vf->dflt_lan_addr.addr))
3680 if (is_unicast_ether_addr(mac_addr) && !ice_can_vf_change_mac(vf)) {
3681 dev_err(dev, "VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
3685 status = ice_fltr_add_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
3686 if (status == ICE_ERR_ALREADY_EXISTS) {
3687 dev_err(dev, "MAC %pM already exists for VF %d\n", mac_addr,
3690 } else if (status) {
3691 dev_err(dev, "Failed to add MAC %pM for VF %d\n, error %s\n",
3692 mac_addr, vf->vf_id, ice_stat_str(status));
3696 /* Set the default LAN address to the latest unicast MAC address added
3697 * by the VF. The default LAN address is reported by the PF via
3698 * ndo_get_vf_config.
3700 if (is_unicast_ether_addr(mac_addr))
3701 ether_addr_copy(vf->dflt_lan_addr.addr, mac_addr);
3709 * ice_vc_del_mac_addr - attempt to delete the MAC address passed in
3710 * @vf: pointer to the VF info
3711 * @vsi: pointer to the VF's VSI
3712 * @mac_addr: MAC address to delete
3715 ice_vc_del_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi, u8 *mac_addr)
3717 struct device *dev = ice_pf_to_dev(vf->pf);
3718 enum ice_status status;
3720 if (!ice_can_vf_change_mac(vf) &&
3721 ether_addr_equal(mac_addr, vf->dflt_lan_addr.addr))
3724 status = ice_fltr_remove_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
3725 if (status == ICE_ERR_DOES_NOT_EXIST) {
3726 dev_err(dev, "MAC %pM does not exist for VF %d\n", mac_addr,
3729 } else if (status) {
3730 dev_err(dev, "Failed to delete MAC %pM for VF %d, error %s\n",
3731 mac_addr, vf->vf_id, ice_stat_str(status));
3735 if (ether_addr_equal(mac_addr, vf->dflt_lan_addr.addr))
3736 eth_zero_addr(vf->dflt_lan_addr.addr);
3744 * ice_vc_handle_mac_addr_msg
3745 * @vf: pointer to the VF info
3746 * @msg: pointer to the msg buffer
3747 * @set: true if MAC filters are being set, false otherwise
3749 * add guest MAC address filter
3752 ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
3754 int (*ice_vc_cfg_mac)
3755 (struct ice_vf *vf, struct ice_vsi *vsi, u8 *mac_addr);
3756 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3757 struct virtchnl_ether_addr_list *al =
3758 (struct virtchnl_ether_addr_list *)msg;
3759 struct ice_pf *pf = vf->pf;
3760 enum virtchnl_ops vc_op;
3761 struct ice_vsi *vsi;
3765 vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
3766 ice_vc_cfg_mac = ice_vc_add_mac_addr;
3768 vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
3769 ice_vc_cfg_mac = ice_vc_del_mac_addr;
3772 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
3773 !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
3774 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3775 goto handle_mac_exit;
3778 /* If this VF is not privileged, then we can't add more than a
3779 * limited number of addresses. Check to make sure that the
3780 * additions do not push us over the limit.
3782 if (set && !ice_is_vf_trusted(vf) &&
3783 (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
3784 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",
3786 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3787 goto handle_mac_exit;
3790 vsi = ice_get_vf_vsi(vf);
3792 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3793 goto handle_mac_exit;
3796 for (i = 0; i < al->num_elements; i++) {
3797 u8 *mac_addr = al->list[i].addr;
3800 if (is_broadcast_ether_addr(mac_addr) ||
3801 is_zero_ether_addr(mac_addr))
3804 result = ice_vc_cfg_mac(vf, vsi, mac_addr);
3805 if (result == -EEXIST || result == -ENOENT) {
3807 } else if (result) {
3808 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
3809 goto handle_mac_exit;
3814 /* send the response to the VF */
3815 return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
3819 * ice_vc_add_mac_addr_msg
3820 * @vf: pointer to the VF info
3821 * @msg: pointer to the msg buffer
3823 * add guest MAC address filter
3825 static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
3827 return ice_vc_handle_mac_addr_msg(vf, msg, true);
3831 * ice_vc_del_mac_addr_msg
3832 * @vf: pointer to the VF info
3833 * @msg: pointer to the msg buffer
3835 * remove guest MAC address filter
3837 static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
3839 return ice_vc_handle_mac_addr_msg(vf, msg, false);
3843 * ice_vc_request_qs_msg
3844 * @vf: pointer to the VF info
3845 * @msg: pointer to the msg buffer
3847 * VFs get a default number of queues but can use this message to request a
3848 * different number. If the request is successful, PF will reset the VF and
3849 * return 0. If unsuccessful, PF will send message informing VF of number of
3850 * available queue pairs via virtchnl message response to VF.
3852 static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
3854 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3855 struct virtchnl_vf_res_request *vfres =
3856 (struct virtchnl_vf_res_request *)msg;
3857 u16 req_queues = vfres->num_queue_pairs;
3858 struct ice_pf *pf = vf->pf;
3859 u16 max_allowed_vf_queues;
3860 u16 tx_rx_queue_left;
3864 dev = ice_pf_to_dev(pf);
3865 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3866 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3870 cur_queues = vf->num_vf_qs;
3871 tx_rx_queue_left = min_t(u16, ice_get_avail_txq_count(pf),
3872 ice_get_avail_rxq_count(pf));
3873 max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
3875 dev_err(dev, "VF %d tried to request 0 queues. Ignoring.\n",
3877 } else if (req_queues > ICE_MAX_RSS_QS_PER_VF) {
3878 dev_err(dev, "VF %d tried to request more than %d queues.\n",
3879 vf->vf_id, ICE_MAX_RSS_QS_PER_VF);
3880 vfres->num_queue_pairs = ICE_MAX_RSS_QS_PER_VF;
3881 } else if (req_queues > cur_queues &&
3882 req_queues - cur_queues > tx_rx_queue_left) {
3883 dev_warn(dev, "VF %d requested %u more queues, but only %u left.\n",
3884 vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
3885 vfres->num_queue_pairs = min_t(u16, max_allowed_vf_queues,
3886 ICE_MAX_RSS_QS_PER_VF);
3888 /* request is successful, then reset VF */
3889 vf->num_req_qs = req_queues;
3890 ice_vc_reset_vf(vf);
3891 dev_info(dev, "VF %d granted request of %u queues.\n",
3892 vf->vf_id, req_queues);
3897 /* send the response to the VF */
3898 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
3899 v_ret, (u8 *)vfres, sizeof(*vfres));
3903 * ice_set_vf_port_vlan
3904 * @netdev: network interface device structure
3905 * @vf_id: VF identifier
3906 * @vlan_id: VLAN ID being set
3907 * @qos: priority setting
3908 * @vlan_proto: VLAN protocol
3910 * program VF Port VLAN ID and/or QoS
3913 ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
3916 struct ice_pf *pf = ice_netdev_to_pf(netdev);
3922 dev = ice_pf_to_dev(pf);
3923 if (ice_validate_vf_id(pf, vf_id))
3926 if (vlan_id >= VLAN_N_VID || qos > 7) {
3927 dev_err(dev, "Invalid Port VLAN parameters for VF %d, ID %d, QoS %d\n",
3928 vf_id, vlan_id, qos);
3932 if (vlan_proto != htons(ETH_P_8021Q)) {
3933 dev_err(dev, "VF VLAN protocol is not supported\n");
3934 return -EPROTONOSUPPORT;
3937 vf = &pf->vf[vf_id];
3938 ret = ice_check_vf_ready_for_cfg(vf);
3942 vlanprio = vlan_id | (qos << VLAN_PRIO_SHIFT);
3944 if (vf->port_vlan_info == vlanprio) {
3945 /* duplicate request, so just return success */
3946 dev_dbg(dev, "Duplicate pvid %d request\n", vlanprio);
3950 vf->port_vlan_info = vlanprio;
3952 if (vf->port_vlan_info)
3953 dev_info(dev, "Setting VLAN %d, QoS 0x%x on VF %d\n",
3954 vlan_id, qos, vf_id);
3956 dev_info(dev, "Clearing port VLAN on VF %d\n", vf_id);
3958 ice_vc_reset_vf(vf);
3964 * ice_vf_vlan_offload_ena - determine if capabilities support VLAN offloads
3965 * @caps: VF driver negotiated capabilities
3967 * Return true if VIRTCHNL_VF_OFFLOAD_VLAN capability is set, else return false
3969 static bool ice_vf_vlan_offload_ena(u32 caps)
3971 return !!(caps & VIRTCHNL_VF_OFFLOAD_VLAN);
3975 * ice_vc_process_vlan_msg
3976 * @vf: pointer to the VF info
3977 * @msg: pointer to the msg buffer
3978 * @add_v: Add VLAN if true, otherwise delete VLAN
3980 * Process virtchnl op to add or remove programmed guest VLAN ID
3982 static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
3984 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3985 struct virtchnl_vlan_filter_list *vfl =
3986 (struct virtchnl_vlan_filter_list *)msg;
3987 struct ice_pf *pf = vf->pf;
3988 bool vlan_promisc = false;
3989 struct ice_vsi *vsi;
3996 dev = ice_pf_to_dev(pf);
3997 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3998 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4002 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
4003 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4007 if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
4008 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4012 for (i = 0; i < vfl->num_elements; i++) {
4013 if (vfl->vlan_id[i] >= VLAN_N_VID) {
4014 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4015 dev_err(dev, "invalid VF VLAN id %d\n",
4022 vsi = ice_get_vf_vsi(vf);
4024 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4028 if (add_v && !ice_is_vf_trusted(vf) &&
4029 vsi->num_vlan >= ICE_MAX_VLAN_PER_VF) {
4030 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
4032 /* There is no need to let VF know about being not trusted,
4033 * so we can just return success message here
4038 if (vsi->info.pvid) {
4039 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4043 if ((test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
4044 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) &&
4045 test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags))
4046 vlan_promisc = true;
4049 for (i = 0; i < vfl->num_elements; i++) {
4050 u16 vid = vfl->vlan_id[i];
4052 if (!ice_is_vf_trusted(vf) &&
4053 vsi->num_vlan >= ICE_MAX_VLAN_PER_VF) {
4054 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
4056 /* There is no need to let VF know about being
4057 * not trusted, so we can just return success
4058 * message here as well.
4063 /* we add VLAN 0 by default for each VF so we can enable
4064 * Tx VLAN anti-spoof without triggering MDD events so
4065 * we don't need to add it again here
4070 status = ice_vsi_add_vlan(vsi, vid, ICE_FWD_TO_VSI);
4072 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4076 /* Enable VLAN pruning when non-zero VLAN is added */
4077 if (!vlan_promisc && vid &&
4078 !ice_vsi_is_vlan_pruning_ena(vsi)) {
4079 status = ice_cfg_vlan_pruning(vsi, true, false);
4081 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4082 dev_err(dev, "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
4086 } else if (vlan_promisc) {
4087 /* Enable Ucast/Mcast VLAN promiscuous mode */
4088 promisc_m = ICE_PROMISC_VLAN_TX |
4089 ICE_PROMISC_VLAN_RX;
4091 status = ice_set_vsi_promisc(hw, vsi->idx,
4094 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4095 dev_err(dev, "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
4101 /* In case of non_trusted VF, number of VLAN elements passed
4102 * to PF for removal might be greater than number of VLANs
4103 * filter programmed for that VF - So, use actual number of
4104 * VLANS added earlier with add VLAN opcode. In order to avoid
4105 * removing VLAN that doesn't exist, which result to sending
4106 * erroneous failed message back to the VF
4110 num_vf_vlan = vsi->num_vlan;
4111 for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
4112 u16 vid = vfl->vlan_id[i];
4114 /* we add VLAN 0 by default for each VF so we can enable
4115 * Tx VLAN anti-spoof without triggering MDD events so
4116 * we don't want a VIRTCHNL request to remove it
4121 /* Make sure ice_vsi_kill_vlan is successful before
4122 * updating VLAN information
4124 status = ice_vsi_kill_vlan(vsi, vid);
4126 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4130 /* Disable VLAN pruning when only VLAN 0 is left */
4131 if (vsi->num_vlan == 1 &&
4132 ice_vsi_is_vlan_pruning_ena(vsi))
4133 ice_cfg_vlan_pruning(vsi, false, false);
4135 /* Disable Unicast/Multicast VLAN promiscuous mode */
4137 promisc_m = ICE_PROMISC_VLAN_TX |
4138 ICE_PROMISC_VLAN_RX;
4140 ice_clear_vsi_promisc(hw, vsi->idx,
4147 /* send the response to the VF */
4149 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
4152 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
4157 * ice_vc_add_vlan_msg
4158 * @vf: pointer to the VF info
4159 * @msg: pointer to the msg buffer
4161 * Add and program guest VLAN ID
4163 static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
4165 return ice_vc_process_vlan_msg(vf, msg, true);
4169 * ice_vc_remove_vlan_msg
4170 * @vf: pointer to the VF info
4171 * @msg: pointer to the msg buffer
4173 * remove programmed guest VLAN ID
4175 static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
4177 return ice_vc_process_vlan_msg(vf, msg, false);
4181 * ice_vc_ena_vlan_stripping
4182 * @vf: pointer to the VF info
4184 * Enable VLAN header stripping for a given VF
4186 static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
4188 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4189 struct ice_vsi *vsi;
4191 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4192 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4196 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
4197 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4201 vsi = ice_get_vf_vsi(vf);
4202 if (ice_vsi_manage_vlan_stripping(vsi, true))
4203 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4206 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
4211 * ice_vc_dis_vlan_stripping
4212 * @vf: pointer to the VF info
4214 * Disable VLAN header stripping for a given VF
4216 static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
4218 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4219 struct ice_vsi *vsi;
4221 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4222 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4226 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
4227 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4231 vsi = ice_get_vf_vsi(vf);
4233 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4237 if (ice_vsi_manage_vlan_stripping(vsi, false))
4238 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4241 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
4246 * ice_vf_init_vlan_stripping - enable/disable VLAN stripping on initialization
4247 * @vf: VF to enable/disable VLAN stripping for on initialization
4249 * If the VIRTCHNL_VF_OFFLOAD_VLAN flag is set enable VLAN stripping, else if
4250 * the flag is cleared then we want to disable stripping. For example, the flag
4251 * will be cleared when port VLANs are configured by the administrator before
4252 * passing the VF to the guest or if the AVF driver doesn't support VLAN
4255 static int ice_vf_init_vlan_stripping(struct ice_vf *vf)
4257 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
4262 /* don't modify stripping if port VLAN is configured */
4266 if (ice_vf_vlan_offload_ena(vf->driver_caps))
4267 return ice_vsi_manage_vlan_stripping(vsi, true);
4269 return ice_vsi_manage_vlan_stripping(vsi, false);
4273 * ice_vc_process_vf_msg - Process request from VF
4274 * @pf: pointer to the PF structure
4275 * @event: pointer to the AQ event
4277 * called from the common asq/arq handler to
4278 * process request from VF
4280 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
4282 u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
4283 s16 vf_id = le16_to_cpu(event->desc.retval);
4284 u16 msglen = event->msg_len;
4285 u8 *msg = event->msg_buf;
4286 struct ice_vf *vf = NULL;
4290 dev = ice_pf_to_dev(pf);
4291 if (ice_validate_vf_id(pf, vf_id)) {
4296 vf = &pf->vf[vf_id];
4298 /* Check if VF is disabled. */
4299 if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
4304 /* Perform basic checks on the msg */
4305 err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
4307 if (err == VIRTCHNL_STATUS_ERR_PARAM)
4313 if (!ice_vc_is_opcode_allowed(vf, v_opcode)) {
4314 ice_vc_send_msg_to_vf(vf, v_opcode,
4315 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED, NULL,
4322 ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
4324 dev_err(dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
4325 vf_id, v_opcode, msglen, err);
4330 case VIRTCHNL_OP_VERSION:
4331 err = ice_vc_get_ver_msg(vf, msg);
4333 case VIRTCHNL_OP_GET_VF_RESOURCES:
4334 err = ice_vc_get_vf_res_msg(vf, msg);
4335 if (ice_vf_init_vlan_stripping(vf))
4336 dev_err(dev, "Failed to initialize VLAN stripping for VF %d\n",
4338 ice_vc_notify_vf_link_state(vf);
4340 case VIRTCHNL_OP_RESET_VF:
4341 ice_vc_reset_vf_msg(vf);
4343 case VIRTCHNL_OP_ADD_ETH_ADDR:
4344 err = ice_vc_add_mac_addr_msg(vf, msg);
4346 case VIRTCHNL_OP_DEL_ETH_ADDR:
4347 err = ice_vc_del_mac_addr_msg(vf, msg);
4349 case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
4350 err = ice_vc_cfg_qs_msg(vf, msg);
4352 case VIRTCHNL_OP_ENABLE_QUEUES:
4353 err = ice_vc_ena_qs_msg(vf, msg);
4354 ice_vc_notify_vf_link_state(vf);
4356 case VIRTCHNL_OP_DISABLE_QUEUES:
4357 err = ice_vc_dis_qs_msg(vf, msg);
4359 case VIRTCHNL_OP_REQUEST_QUEUES:
4360 err = ice_vc_request_qs_msg(vf, msg);
4362 case VIRTCHNL_OP_CONFIG_IRQ_MAP:
4363 err = ice_vc_cfg_irq_map_msg(vf, msg);
4365 case VIRTCHNL_OP_CONFIG_RSS_KEY:
4366 err = ice_vc_config_rss_key(vf, msg);
4368 case VIRTCHNL_OP_CONFIG_RSS_LUT:
4369 err = ice_vc_config_rss_lut(vf, msg);
4371 case VIRTCHNL_OP_GET_STATS:
4372 err = ice_vc_get_stats_msg(vf, msg);
4374 case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
4375 err = ice_vc_cfg_promiscuous_mode_msg(vf, msg);
4377 case VIRTCHNL_OP_ADD_VLAN:
4378 err = ice_vc_add_vlan_msg(vf, msg);
4380 case VIRTCHNL_OP_DEL_VLAN:
4381 err = ice_vc_remove_vlan_msg(vf, msg);
4383 case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
4384 err = ice_vc_ena_vlan_stripping(vf);
4386 case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
4387 err = ice_vc_dis_vlan_stripping(vf);
4389 case VIRTCHNL_OP_ADD_FDIR_FILTER:
4390 err = ice_vc_add_fdir_fltr(vf, msg);
4392 case VIRTCHNL_OP_DEL_FDIR_FILTER:
4393 err = ice_vc_del_fdir_fltr(vf, msg);
4395 case VIRTCHNL_OP_ADD_RSS_CFG:
4396 err = ice_vc_handle_rss_cfg(vf, msg, true);
4398 case VIRTCHNL_OP_DEL_RSS_CFG:
4399 err = ice_vc_handle_rss_cfg(vf, msg, false);
4401 case VIRTCHNL_OP_UNKNOWN:
4403 dev_err(dev, "Unsupported opcode %d from VF %d\n", v_opcode,
4405 err = ice_vc_send_msg_to_vf(vf, v_opcode,
4406 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4411 /* Helper function cares less about error return values here
4412 * as it is busy with pending work.
4414 dev_info(dev, "PF failed to honor VF %d, opcode %d, error %d\n",
4415 vf_id, v_opcode, err);
4421 * @netdev: network interface device structure
4422 * @vf_id: VF identifier
4423 * @ivi: VF configuration structure
4425 * return VF configuration
4428 ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
4430 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4433 if (ice_validate_vf_id(pf, vf_id))
4436 vf = &pf->vf[vf_id];
4438 if (ice_check_vf_init(pf, vf))
4442 ether_addr_copy(ivi->mac, vf->dflt_lan_addr.addr);
4444 /* VF configuration for VLAN and applicable QoS */
4445 ivi->vlan = vf->port_vlan_info & VLAN_VID_MASK;
4446 ivi->qos = (vf->port_vlan_info & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
4448 ivi->trusted = vf->trusted;
4449 ivi->spoofchk = vf->spoofchk;
4450 if (!vf->link_forced)
4451 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
4452 else if (vf->link_up)
4453 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
4455 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
4456 ivi->max_tx_rate = vf->tx_rate;
4457 ivi->min_tx_rate = 0;
4462 * ice_unicast_mac_exists - check if the unicast MAC exists on the PF's switch
4463 * @pf: PF used to reference the switch's rules
4464 * @umac: unicast MAC to compare against existing switch rules
4466 * Return true on the first/any match, else return false
4468 static bool ice_unicast_mac_exists(struct ice_pf *pf, u8 *umac)
4470 struct ice_sw_recipe *mac_recipe_list =
4471 &pf->hw.switch_info->recp_list[ICE_SW_LKUP_MAC];
4472 struct ice_fltr_mgmt_list_entry *list_itr;
4473 struct list_head *rule_head;
4474 struct mutex *rule_lock; /* protect MAC filter list access */
4476 rule_head = &mac_recipe_list->filt_rules;
4477 rule_lock = &mac_recipe_list->filt_rule_lock;
4479 mutex_lock(rule_lock);
4480 list_for_each_entry(list_itr, rule_head, list_entry) {
4481 u8 *existing_mac = &list_itr->fltr_info.l_data.mac.mac_addr[0];
4483 if (ether_addr_equal(existing_mac, umac)) {
4484 mutex_unlock(rule_lock);
4489 mutex_unlock(rule_lock);
4496 * @netdev: network interface device structure
4497 * @vf_id: VF identifier
4500 * program VF MAC address
4502 int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
4504 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4508 if (ice_validate_vf_id(pf, vf_id))
4511 if (is_multicast_ether_addr(mac)) {
4512 netdev_err(netdev, "%pM not a valid unicast address\n", mac);
4516 vf = &pf->vf[vf_id];
4517 /* nothing left to do, unicast MAC already set */
4518 if (ether_addr_equal(vf->dflt_lan_addr.addr, mac))
4521 ret = ice_check_vf_ready_for_cfg(vf);
4525 if (ice_unicast_mac_exists(pf, mac)) {
4526 netdev_err(netdev, "Unicast MAC %pM already exists on this PF. Preventing setting VF %u unicast MAC address to %pM\n",
4531 /* VF is notified of its new MAC via the PF's response to the
4532 * VIRTCHNL_OP_GET_VF_RESOURCES message after the VF has been reset
4534 ether_addr_copy(vf->dflt_lan_addr.addr, mac);
4535 if (is_zero_ether_addr(mac)) {
4536 /* VF will send VIRTCHNL_OP_ADD_ETH_ADDR message with its MAC */
4537 vf->pf_set_mac = false;
4538 netdev_info(netdev, "Removing MAC on VF %d. VF driver will be reinitialized\n",
4541 /* PF will add MAC rule for the VF */
4542 vf->pf_set_mac = true;
4543 netdev_info(netdev, "Setting MAC %pM on VF %d. VF driver will be reinitialized\n",
4547 ice_vc_reset_vf(vf);
4553 * @netdev: network interface device structure
4554 * @vf_id: VF identifier
4555 * @trusted: Boolean value to enable/disable trusted VF
4557 * Enable or disable a given VF as trusted
4559 int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
4561 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4565 if (ice_validate_vf_id(pf, vf_id))
4568 vf = &pf->vf[vf_id];
4569 ret = ice_check_vf_ready_for_cfg(vf);
4573 /* Check if already trusted */
4574 if (trusted == vf->trusted)
4577 vf->trusted = trusted;
4578 ice_vc_reset_vf(vf);
4579 dev_info(ice_pf_to_dev(pf), "VF %u is now %strusted\n",
4580 vf_id, trusted ? "" : "un");
4586 * ice_set_vf_link_state
4587 * @netdev: network interface device structure
4588 * @vf_id: VF identifier
4589 * @link_state: required link state
4591 * Set VF's link state, irrespective of physical link state status
4593 int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
4595 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4599 if (ice_validate_vf_id(pf, vf_id))
4602 vf = &pf->vf[vf_id];
4603 ret = ice_check_vf_ready_for_cfg(vf);
4607 switch (link_state) {
4608 case IFLA_VF_LINK_STATE_AUTO:
4609 vf->link_forced = false;
4611 case IFLA_VF_LINK_STATE_ENABLE:
4612 vf->link_forced = true;
4615 case IFLA_VF_LINK_STATE_DISABLE:
4616 vf->link_forced = true;
4617 vf->link_up = false;
4623 ice_vc_notify_vf_link_state(vf);
4629 * ice_get_vf_stats - populate some stats for the VF
4630 * @netdev: the netdev of the PF
4631 * @vf_id: the host OS identifier (0-255)
4632 * @vf_stats: pointer to the OS memory to be initialized
4634 int ice_get_vf_stats(struct net_device *netdev, int vf_id,
4635 struct ifla_vf_stats *vf_stats)
4637 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4638 struct ice_eth_stats *stats;
4639 struct ice_vsi *vsi;
4643 if (ice_validate_vf_id(pf, vf_id))
4646 vf = &pf->vf[vf_id];
4647 ret = ice_check_vf_ready_for_cfg(vf);
4651 vsi = ice_get_vf_vsi(vf);
4655 ice_update_eth_stats(vsi);
4656 stats = &vsi->eth_stats;
4658 memset(vf_stats, 0, sizeof(*vf_stats));
4660 vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
4661 stats->rx_multicast;
4662 vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
4663 stats->tx_multicast;
4664 vf_stats->rx_bytes = stats->rx_bytes;
4665 vf_stats->tx_bytes = stats->tx_bytes;
4666 vf_stats->broadcast = stats->rx_broadcast;
4667 vf_stats->multicast = stats->rx_multicast;
4668 vf_stats->rx_dropped = stats->rx_discards;
4669 vf_stats->tx_dropped = stats->tx_discards;
4675 * ice_print_vf_rx_mdd_event - print VF Rx malicious driver detect event
4676 * @vf: pointer to the VF structure
4678 void ice_print_vf_rx_mdd_event(struct ice_vf *vf)
4680 struct ice_pf *pf = vf->pf;
4683 dev = ice_pf_to_dev(pf);
4685 dev_info(dev, "%d Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pM. mdd-auto-reset-vfs=%s\n",
4686 vf->mdd_rx_events.count, pf->hw.pf_id, vf->vf_id,
4687 vf->dflt_lan_addr.addr,
4688 test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)
4693 * ice_print_vfs_mdd_events - print VFs malicious driver detect event
4694 * @pf: pointer to the PF structure
4696 * Called from ice_handle_mdd_event to rate limit and print VFs MDD events.
4698 void ice_print_vfs_mdd_events(struct ice_pf *pf)
4700 struct device *dev = ice_pf_to_dev(pf);
4701 struct ice_hw *hw = &pf->hw;
4704 /* check that there are pending MDD events to print */
4705 if (!test_and_clear_bit(ICE_MDD_VF_PRINT_PENDING, pf->state))
4708 /* VF MDD event logs are rate limited to one second intervals */
4709 if (time_is_after_jiffies(pf->last_printed_mdd_jiffies + HZ * 1))
4712 pf->last_printed_mdd_jiffies = jiffies;
4714 ice_for_each_vf(pf, i) {
4715 struct ice_vf *vf = &pf->vf[i];
4717 /* only print Rx MDD event message if there are new events */
4718 if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
4719 vf->mdd_rx_events.last_printed =
4720 vf->mdd_rx_events.count;
4721 ice_print_vf_rx_mdd_event(vf);
4724 /* only print Tx MDD event message if there are new events */
4725 if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
4726 vf->mdd_tx_events.last_printed =
4727 vf->mdd_tx_events.count;
4729 dev_info(dev, "%d Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pM.\n",
4730 vf->mdd_tx_events.count, hw->pf_id, i,
4731 vf->dflt_lan_addr.addr);
4737 * ice_restore_all_vfs_msi_state - restore VF MSI state after PF FLR
4738 * @pdev: pointer to a pci_dev structure
4740 * Called when recovering from a PF FLR to restore interrupt capability to
4743 void ice_restore_all_vfs_msi_state(struct pci_dev *pdev)
4748 if (!pci_num_vf(pdev))
4751 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4753 struct pci_dev *vfdev;
4755 pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID,
4757 vfdev = pci_get_device(pdev->vendor, vf_id, NULL);
4759 if (vfdev->is_virtfn && vfdev->physfn == pdev)
4760 pci_restore_msi_state(vfdev);
4761 vfdev = pci_get_device(pdev->vendor, vf_id,
4768 * ice_is_malicious_vf - helper function to detect a malicious VF
4769 * @pf: ptr to struct ice_pf
4770 * @event: pointer to the AQ event
4771 * @num_msg_proc: the number of messages processed so far
4772 * @num_msg_pending: the number of messages peinding in admin queue
4775 ice_is_malicious_vf(struct ice_pf *pf, struct ice_rq_event_info *event,
4776 u16 num_msg_proc, u16 num_msg_pending)
4778 s16 vf_id = le16_to_cpu(event->desc.retval);
4779 struct device *dev = ice_pf_to_dev(pf);
4780 struct ice_mbx_data mbxdata;
4781 enum ice_status status;
4785 if (ice_validate_vf_id(pf, vf_id))
4788 vf = &pf->vf[vf_id];
4789 /* Check if VF is disabled. */
4790 if (test_bit(ICE_VF_STATE_DIS, vf->vf_states))
4793 mbxdata.num_msg_proc = num_msg_proc;
4794 mbxdata.num_pending_arq = num_msg_pending;
4795 mbxdata.max_num_msgs_mbx = pf->hw.mailboxq.num_rq_entries;
4796 #define ICE_MBX_OVERFLOW_WATERMARK 64
4797 mbxdata.async_watermark_val = ICE_MBX_OVERFLOW_WATERMARK;
4799 /* check to see if we have a malicious VF */
4800 status = ice_mbx_vf_state_handler(&pf->hw, &mbxdata, vf_id, &malvf);
4805 bool report_vf = false;
4807 /* if the VF is malicious and we haven't let the user
4808 * know about it, then let them know now
4810 status = ice_mbx_report_malvf(&pf->hw, pf->malvfs,
4811 ICE_MAX_VF_COUNT, vf_id,
4814 dev_dbg(dev, "Error reporting malicious VF\n");
4817 struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
4820 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",
4821 &vf->dflt_lan_addr.addr[0],
4822 pf_vsi->netdev->dev_addr);
4828 /* if there was an error in detection or the VF is not malicious then