]> Git Repo - J-linux.git/blob - drivers/net/ethernet/intel/ice/ice_virtchnl.c
Merge tag 'net-6.7-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[J-linux.git] / drivers / net / ethernet / intel / ice / ice_virtchnl.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2022, Intel Corporation. */
3
4 #include "ice_virtchnl.h"
5 #include "ice_vf_lib_private.h"
6 #include "ice.h"
7 #include "ice_base.h"
8 #include "ice_lib.h"
9 #include "ice_fltr.h"
10 #include "ice_virtchnl_allowlist.h"
11 #include "ice_vf_vsi_vlan_ops.h"
12 #include "ice_vlan.h"
13 #include "ice_flex_pipe.h"
14 #include "ice_dcb_lib.h"
15
16 #define FIELD_SELECTOR(proto_hdr_field) \
17                 BIT((proto_hdr_field) & PROTO_HDR_FIELD_MASK)
18
19 struct ice_vc_hdr_match_type {
20         u32 vc_hdr;     /* virtchnl headers (VIRTCHNL_PROTO_HDR_XXX) */
21         u32 ice_hdr;    /* ice headers (ICE_FLOW_SEG_HDR_XXX) */
22 };
23
24 static const struct ice_vc_hdr_match_type ice_vc_hdr_list[] = {
25         {VIRTCHNL_PROTO_HDR_NONE,       ICE_FLOW_SEG_HDR_NONE},
26         {VIRTCHNL_PROTO_HDR_ETH,        ICE_FLOW_SEG_HDR_ETH},
27         {VIRTCHNL_PROTO_HDR_S_VLAN,     ICE_FLOW_SEG_HDR_VLAN},
28         {VIRTCHNL_PROTO_HDR_C_VLAN,     ICE_FLOW_SEG_HDR_VLAN},
29         {VIRTCHNL_PROTO_HDR_IPV4,       ICE_FLOW_SEG_HDR_IPV4 |
30                                         ICE_FLOW_SEG_HDR_IPV_OTHER},
31         {VIRTCHNL_PROTO_HDR_IPV6,       ICE_FLOW_SEG_HDR_IPV6 |
32                                         ICE_FLOW_SEG_HDR_IPV_OTHER},
33         {VIRTCHNL_PROTO_HDR_TCP,        ICE_FLOW_SEG_HDR_TCP},
34         {VIRTCHNL_PROTO_HDR_UDP,        ICE_FLOW_SEG_HDR_UDP},
35         {VIRTCHNL_PROTO_HDR_SCTP,       ICE_FLOW_SEG_HDR_SCTP},
36         {VIRTCHNL_PROTO_HDR_PPPOE,      ICE_FLOW_SEG_HDR_PPPOE},
37         {VIRTCHNL_PROTO_HDR_GTPU_IP,    ICE_FLOW_SEG_HDR_GTPU_IP},
38         {VIRTCHNL_PROTO_HDR_GTPU_EH,    ICE_FLOW_SEG_HDR_GTPU_EH},
39         {VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_DWN,
40                                         ICE_FLOW_SEG_HDR_GTPU_DWN},
41         {VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_UP,
42                                         ICE_FLOW_SEG_HDR_GTPU_UP},
43         {VIRTCHNL_PROTO_HDR_L2TPV3,     ICE_FLOW_SEG_HDR_L2TPV3},
44         {VIRTCHNL_PROTO_HDR_ESP,        ICE_FLOW_SEG_HDR_ESP},
45         {VIRTCHNL_PROTO_HDR_AH,         ICE_FLOW_SEG_HDR_AH},
46         {VIRTCHNL_PROTO_HDR_PFCP,       ICE_FLOW_SEG_HDR_PFCP_SESSION},
47 };
48
49 struct ice_vc_hash_field_match_type {
50         u32 vc_hdr;             /* virtchnl headers
51                                  * (VIRTCHNL_PROTO_HDR_XXX)
52                                  */
53         u32 vc_hash_field;      /* virtchnl hash fields selector
54                                  * FIELD_SELECTOR((VIRTCHNL_PROTO_HDR_ETH_XXX))
55                                  */
56         u64 ice_hash_field;     /* ice hash fields
57                                  * (BIT_ULL(ICE_FLOW_FIELD_IDX_XXX))
58                                  */
59 };
60
61 static const struct
62 ice_vc_hash_field_match_type ice_vc_hash_field_list[] = {
63         {VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC),
64                 BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_SA)},
65         {VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
66                 BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_DA)},
67         {VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC) |
68                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
69                 ICE_FLOW_HASH_ETH},
70         {VIRTCHNL_PROTO_HDR_ETH,
71                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_ETHERTYPE),
72                 BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_TYPE)},
73         {VIRTCHNL_PROTO_HDR_S_VLAN,
74                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_S_VLAN_ID),
75                 BIT_ULL(ICE_FLOW_FIELD_IDX_S_VLAN)},
76         {VIRTCHNL_PROTO_HDR_C_VLAN,
77                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_C_VLAN_ID),
78                 BIT_ULL(ICE_FLOW_FIELD_IDX_C_VLAN)},
79         {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC),
80                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA)},
81         {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
82                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA)},
83         {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
84                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
85                 ICE_FLOW_HASH_IPV4},
86         {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
87                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
88                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA) |
89                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
90         {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
91                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
92                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA) |
93                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
94         {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
95                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
96                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
97                 ICE_FLOW_HASH_IPV4 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
98         {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
99                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
100         {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC),
101                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA)},
102         {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
103                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA)},
104         {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
105                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
106                 ICE_FLOW_HASH_IPV6},
107         {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
108                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
109                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA) |
110                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
111         {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
112                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
113                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA) |
114                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
115         {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
116                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
117                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
118                 ICE_FLOW_HASH_IPV6 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
119         {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
120                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
121         {VIRTCHNL_PROTO_HDR_TCP,
122                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT),
123                 BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_SRC_PORT)},
124         {VIRTCHNL_PROTO_HDR_TCP,
125                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
126                 BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_DST_PORT)},
127         {VIRTCHNL_PROTO_HDR_TCP,
128                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT) |
129                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
130                 ICE_FLOW_HASH_TCP_PORT},
131         {VIRTCHNL_PROTO_HDR_UDP,
132                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT),
133                 BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_SRC_PORT)},
134         {VIRTCHNL_PROTO_HDR_UDP,
135                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
136                 BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_DST_PORT)},
137         {VIRTCHNL_PROTO_HDR_UDP,
138                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT) |
139                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
140                 ICE_FLOW_HASH_UDP_PORT},
141         {VIRTCHNL_PROTO_HDR_SCTP,
142                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT),
143                 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT)},
144         {VIRTCHNL_PROTO_HDR_SCTP,
145                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
146                 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_DST_PORT)},
147         {VIRTCHNL_PROTO_HDR_SCTP,
148                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT) |
149                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
150                 ICE_FLOW_HASH_SCTP_PORT},
151         {VIRTCHNL_PROTO_HDR_PPPOE,
152                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PPPOE_SESS_ID),
153                 BIT_ULL(ICE_FLOW_FIELD_IDX_PPPOE_SESS_ID)},
154         {VIRTCHNL_PROTO_HDR_GTPU_IP,
155                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_GTPU_IP_TEID),
156                 BIT_ULL(ICE_FLOW_FIELD_IDX_GTPU_IP_TEID)},
157         {VIRTCHNL_PROTO_HDR_L2TPV3,
158                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_L2TPV3_SESS_ID),
159                 BIT_ULL(ICE_FLOW_FIELD_IDX_L2TPV3_SESS_ID)},
160         {VIRTCHNL_PROTO_HDR_ESP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ESP_SPI),
161                 BIT_ULL(ICE_FLOW_FIELD_IDX_ESP_SPI)},
162         {VIRTCHNL_PROTO_HDR_AH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_AH_SPI),
163                 BIT_ULL(ICE_FLOW_FIELD_IDX_AH_SPI)},
164         {VIRTCHNL_PROTO_HDR_PFCP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PFCP_SEID),
165                 BIT_ULL(ICE_FLOW_FIELD_IDX_PFCP_SEID)},
166 };
167
168 /**
169  * ice_vc_vf_broadcast - Broadcast a message to all VFs on PF
170  * @pf: pointer to the PF structure
171  * @v_opcode: operation code
172  * @v_retval: return value
173  * @msg: pointer to the msg buffer
174  * @msglen: msg length
175  */
176 static void
177 ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
178                     enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
179 {
180         struct ice_hw *hw = &pf->hw;
181         struct ice_vf *vf;
182         unsigned int bkt;
183
184         mutex_lock(&pf->vfs.table_lock);
185         ice_for_each_vf(pf, bkt, vf) {
186                 /* Not all vfs are enabled so skip the ones that are not */
187                 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
188                     !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
189                         continue;
190
191                 /* Ignore return value on purpose - a given VF may fail, but
192                  * we need to keep going and send to all of them
193                  */
194                 ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
195                                       msglen, NULL);
196         }
197         mutex_unlock(&pf->vfs.table_lock);
198 }
199
200 /**
201  * ice_set_pfe_link - Set the link speed/status of the virtchnl_pf_event
202  * @vf: pointer to the VF structure
203  * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
204  * @ice_link_speed: link speed specified by ICE_AQ_LINK_SPEED_*
205  * @link_up: whether or not to set the link up/down
206  */
207 static void
208 ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
209                  int ice_link_speed, bool link_up)
210 {
211         if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
212                 pfe->event_data.link_event_adv.link_status = link_up;
213                 /* Speed in Mbps */
214                 pfe->event_data.link_event_adv.link_speed =
215                         ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
216         } else {
217                 pfe->event_data.link_event.link_status = link_up;
218                 /* Legacy method for virtchnl link speeds */
219                 pfe->event_data.link_event.link_speed =
220                         (enum virtchnl_link_speed)
221                         ice_conv_link_speed_to_virtchnl(false, ice_link_speed);
222         }
223 }
224
225 /**
226  * ice_vc_notify_vf_link_state - Inform a VF of link status
227  * @vf: pointer to the VF structure
228  *
229  * send a link status message to a single VF
230  */
231 void ice_vc_notify_vf_link_state(struct ice_vf *vf)
232 {
233         struct virtchnl_pf_event pfe = { 0 };
234         struct ice_hw *hw = &vf->pf->hw;
235
236         pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
237         pfe.severity = PF_EVENT_SEVERITY_INFO;
238
239         if (ice_is_vf_link_up(vf))
240                 ice_set_pfe_link(vf, &pfe,
241                                  hw->port_info->phy.link_info.link_speed, true);
242         else
243                 ice_set_pfe_link(vf, &pfe, ICE_AQ_LINK_SPEED_UNKNOWN, false);
244
245         ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
246                               VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,
247                               sizeof(pfe), NULL);
248 }
249
250 /**
251  * ice_vc_notify_link_state - Inform all VFs on a PF of link status
252  * @pf: pointer to the PF structure
253  */
254 void ice_vc_notify_link_state(struct ice_pf *pf)
255 {
256         struct ice_vf *vf;
257         unsigned int bkt;
258
259         mutex_lock(&pf->vfs.table_lock);
260         ice_for_each_vf(pf, bkt, vf)
261                 ice_vc_notify_vf_link_state(vf);
262         mutex_unlock(&pf->vfs.table_lock);
263 }
264
265 /**
266  * ice_vc_notify_reset - Send pending reset message to all VFs
267  * @pf: pointer to the PF structure
268  *
269  * indicate a pending reset to all VFs on a given PF
270  */
271 void ice_vc_notify_reset(struct ice_pf *pf)
272 {
273         struct virtchnl_pf_event pfe;
274
275         if (!ice_has_vfs(pf))
276                 return;
277
278         pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
279         pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
280         ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
281                             (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
282 }
283
284 /**
285  * ice_vc_send_msg_to_vf - Send message to VF
286  * @vf: pointer to the VF info
287  * @v_opcode: virtual channel opcode
288  * @v_retval: virtual channel return value
289  * @msg: pointer to the msg buffer
290  * @msglen: msg length
291  *
292  * send msg to VF
293  */
294 int
295 ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
296                       enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
297 {
298         struct device *dev;
299         struct ice_pf *pf;
300         int aq_ret;
301
302         pf = vf->pf;
303         dev = ice_pf_to_dev(pf);
304
305         aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
306                                        msg, msglen, NULL);
307         if (aq_ret && pf->hw.mailboxq.sq_last_status != ICE_AQ_RC_ENOSYS) {
308                 dev_info(dev, "Unable to send the message to VF %d ret %d aq_err %s\n",
309                          vf->vf_id, aq_ret,
310                          ice_aq_str(pf->hw.mailboxq.sq_last_status));
311                 return -EIO;
312         }
313
314         return 0;
315 }
316
317 /**
318  * ice_vc_get_ver_msg
319  * @vf: pointer to the VF info
320  * @msg: pointer to the msg buffer
321  *
322  * called from the VF to request the API version used by the PF
323  */
324 static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
325 {
326         struct virtchnl_version_info info = {
327                 VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
328         };
329
330         vf->vf_ver = *(struct virtchnl_version_info *)msg;
331         /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
332         if (VF_IS_V10(&vf->vf_ver))
333                 info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
334
335         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
336                                      VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
337                                      sizeof(struct virtchnl_version_info));
338 }
339
340 /**
341  * ice_vc_get_max_frame_size - get max frame size allowed for VF
342  * @vf: VF used to determine max frame size
343  *
344  * Max frame size is determined based on the current port's max frame size and
345  * whether a port VLAN is configured on this VF. The VF is not aware whether
346  * it's in a port VLAN so the PF needs to account for this in max frame size
347  * checks and sending the max frame size to the VF.
348  */
349 static u16 ice_vc_get_max_frame_size(struct ice_vf *vf)
350 {
351         struct ice_port_info *pi = ice_vf_get_port_info(vf);
352         u16 max_frame_size;
353
354         max_frame_size = pi->phy.link_info.max_frame_size;
355
356         if (ice_vf_is_port_vlan_ena(vf))
357                 max_frame_size -= VLAN_HLEN;
358
359         return max_frame_size;
360 }
361
362 /**
363  * ice_vc_get_vlan_caps
364  * @hw: pointer to the hw
365  * @vf: pointer to the VF info
366  * @vsi: pointer to the VSI
367  * @driver_caps: current driver caps
368  *
369  * Return 0 if there is no VLAN caps supported, or VLAN caps value
370  */
371 static u32
372 ice_vc_get_vlan_caps(struct ice_hw *hw, struct ice_vf *vf, struct ice_vsi *vsi,
373                      u32 driver_caps)
374 {
375         if (ice_is_eswitch_mode_switchdev(vf->pf))
376                 /* In switchdev setting VLAN from VF isn't supported */
377                 return 0;
378
379         if (driver_caps & VIRTCHNL_VF_OFFLOAD_VLAN_V2) {
380                 /* VLAN offloads based on current device configuration */
381                 return VIRTCHNL_VF_OFFLOAD_VLAN_V2;
382         } else if (driver_caps & VIRTCHNL_VF_OFFLOAD_VLAN) {
383                 /* allow VF to negotiate VIRTCHNL_VF_OFFLOAD explicitly for
384                  * these two conditions, which amounts to guest VLAN filtering
385                  * and offloads being based on the inner VLAN or the
386                  * inner/single VLAN respectively and don't allow VF to
387                  * negotiate VIRTCHNL_VF_OFFLOAD in any other cases
388                  */
389                 if (ice_is_dvm_ena(hw) && ice_vf_is_port_vlan_ena(vf)) {
390                         return VIRTCHNL_VF_OFFLOAD_VLAN;
391                 } else if (!ice_is_dvm_ena(hw) &&
392                            !ice_vf_is_port_vlan_ena(vf)) {
393                         /* configure backward compatible support for VFs that
394                          * only support VIRTCHNL_VF_OFFLOAD_VLAN, the PF is
395                          * configured in SVM, and no port VLAN is configured
396                          */
397                         ice_vf_vsi_cfg_svm_legacy_vlan_mode(vsi);
398                         return VIRTCHNL_VF_OFFLOAD_VLAN;
399                 } else if (ice_is_dvm_ena(hw)) {
400                         /* configure software offloaded VLAN support when DVM
401                          * is enabled, but no port VLAN is enabled
402                          */
403                         ice_vf_vsi_cfg_dvm_legacy_vlan_mode(vsi);
404                 }
405         }
406
407         return 0;
408 }
409
410 /**
411  * ice_vc_get_vf_res_msg
412  * @vf: pointer to the VF info
413  * @msg: pointer to the msg buffer
414  *
415  * called from the VF to request its resources
416  */
417 static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
418 {
419         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
420         struct virtchnl_vf_resource *vfres = NULL;
421         struct ice_hw *hw = &vf->pf->hw;
422         struct ice_vsi *vsi;
423         int len = 0;
424         int ret;
425
426         if (ice_check_vf_init(vf)) {
427                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
428                 goto err;
429         }
430
431         len = virtchnl_struct_size(vfres, vsi_res, 0);
432
433         vfres = kzalloc(len, GFP_KERNEL);
434         if (!vfres) {
435                 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
436                 len = 0;
437                 goto err;
438         }
439         if (VF_IS_V11(&vf->vf_ver))
440                 vf->driver_caps = *(u32 *)msg;
441         else
442                 vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
443                                   VIRTCHNL_VF_OFFLOAD_RSS_REG |
444                                   VIRTCHNL_VF_OFFLOAD_VLAN;
445
446         vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
447         vsi = ice_get_vf_vsi(vf);
448         if (!vsi) {
449                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
450                 goto err;
451         }
452
453         vfres->vf_cap_flags |= ice_vc_get_vlan_caps(hw, vf, vsi,
454                                                     vf->driver_caps);
455
456         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
457                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
458         } else {
459                 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
460                         vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
461                 else
462                         vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
463         }
464
465         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC)
466                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC;
467
468         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_FDIR_PF)
469                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_FDIR_PF;
470
471         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
472                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
473
474         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
475                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
476
477         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
478                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
479
480         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
481                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
482
483         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
484                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
485
486         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
487                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
488
489         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_CRC)
490                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_CRC;
491
492         if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
493                 vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
494
495         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF)
496                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF;
497
498         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_USO)
499                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_USO;
500
501         vfres->num_vsis = 1;
502         /* Tx and Rx queue are equal for VF */
503         vfres->num_queue_pairs = vsi->num_txq;
504         vfres->max_vectors = vf->num_msix;
505         vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
506         vfres->rss_lut_size = ICE_LUT_VSI_SIZE;
507         vfres->max_mtu = ice_vc_get_max_frame_size(vf);
508
509         vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
510         vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
511         vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
512         ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
513                         vf->hw_lan_addr);
514
515         /* match guest capabilities */
516         vf->driver_caps = vfres->vf_cap_flags;
517
518         ice_vc_set_caps_allowlist(vf);
519         ice_vc_set_working_allowlist(vf);
520
521         set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
522
523 err:
524         /* send the response back to the VF */
525         ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
526                                     (u8 *)vfres, len);
527
528         kfree(vfres);
529         return ret;
530 }
531
532 /**
533  * ice_vc_reset_vf_msg
534  * @vf: pointer to the VF info
535  *
536  * called from the VF to reset itself,
537  * unlike other virtchnl messages, PF driver
538  * doesn't send the response back to the VF
539  */
540 static void ice_vc_reset_vf_msg(struct ice_vf *vf)
541 {
542         if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
543                 ice_reset_vf(vf, 0);
544 }
545
546 /**
547  * ice_vc_isvalid_vsi_id
548  * @vf: pointer to the VF info
549  * @vsi_id: VF relative VSI ID
550  *
551  * check for the valid VSI ID
552  */
553 bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
554 {
555         struct ice_pf *pf = vf->pf;
556         struct ice_vsi *vsi;
557
558         vsi = ice_find_vsi(pf, vsi_id);
559
560         return (vsi && (vsi->vf == vf));
561 }
562
563 /**
564  * ice_vc_isvalid_q_id
565  * @vf: pointer to the VF info
566  * @vsi_id: VSI ID
567  * @qid: VSI relative queue ID
568  *
569  * check for the valid queue ID
570  */
571 static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
572 {
573         struct ice_vsi *vsi = ice_find_vsi(vf->pf, vsi_id);
574         /* allocated Tx and Rx queues should be always equal for VF VSI */
575         return (vsi && (qid < vsi->alloc_txq));
576 }
577
578 /**
579  * ice_vc_isvalid_ring_len
580  * @ring_len: length of ring
581  *
582  * check for the valid ring count, should be multiple of ICE_REQ_DESC_MULTIPLE
583  * or zero
584  */
585 static bool ice_vc_isvalid_ring_len(u16 ring_len)
586 {
587         return ring_len == 0 ||
588                (ring_len >= ICE_MIN_NUM_DESC &&
589                 ring_len <= ICE_MAX_NUM_DESC &&
590                 !(ring_len % ICE_REQ_DESC_MULTIPLE));
591 }
592
593 /**
594  * ice_vc_validate_pattern
595  * @vf: pointer to the VF info
596  * @proto: virtchnl protocol headers
597  *
598  * validate the pattern is supported or not.
599  *
600  * Return: true on success, false on error.
601  */
602 bool
603 ice_vc_validate_pattern(struct ice_vf *vf, struct virtchnl_proto_hdrs *proto)
604 {
605         bool is_ipv4 = false;
606         bool is_ipv6 = false;
607         bool is_udp = false;
608         u16 ptype = -1;
609         int i = 0;
610
611         while (i < proto->count &&
612                proto->proto_hdr[i].type != VIRTCHNL_PROTO_HDR_NONE) {
613                 switch (proto->proto_hdr[i].type) {
614                 case VIRTCHNL_PROTO_HDR_ETH:
615                         ptype = ICE_PTYPE_MAC_PAY;
616                         break;
617                 case VIRTCHNL_PROTO_HDR_IPV4:
618                         ptype = ICE_PTYPE_IPV4_PAY;
619                         is_ipv4 = true;
620                         break;
621                 case VIRTCHNL_PROTO_HDR_IPV6:
622                         ptype = ICE_PTYPE_IPV6_PAY;
623                         is_ipv6 = true;
624                         break;
625                 case VIRTCHNL_PROTO_HDR_UDP:
626                         if (is_ipv4)
627                                 ptype = ICE_PTYPE_IPV4_UDP_PAY;
628                         else if (is_ipv6)
629                                 ptype = ICE_PTYPE_IPV6_UDP_PAY;
630                         is_udp = true;
631                         break;
632                 case VIRTCHNL_PROTO_HDR_TCP:
633                         if (is_ipv4)
634                                 ptype = ICE_PTYPE_IPV4_TCP_PAY;
635                         else if (is_ipv6)
636                                 ptype = ICE_PTYPE_IPV6_TCP_PAY;
637                         break;
638                 case VIRTCHNL_PROTO_HDR_SCTP:
639                         if (is_ipv4)
640                                 ptype = ICE_PTYPE_IPV4_SCTP_PAY;
641                         else if (is_ipv6)
642                                 ptype = ICE_PTYPE_IPV6_SCTP_PAY;
643                         break;
644                 case VIRTCHNL_PROTO_HDR_GTPU_IP:
645                 case VIRTCHNL_PROTO_HDR_GTPU_EH:
646                         if (is_ipv4)
647                                 ptype = ICE_MAC_IPV4_GTPU;
648                         else if (is_ipv6)
649                                 ptype = ICE_MAC_IPV6_GTPU;
650                         goto out;
651                 case VIRTCHNL_PROTO_HDR_L2TPV3:
652                         if (is_ipv4)
653                                 ptype = ICE_MAC_IPV4_L2TPV3;
654                         else if (is_ipv6)
655                                 ptype = ICE_MAC_IPV6_L2TPV3;
656                         goto out;
657                 case VIRTCHNL_PROTO_HDR_ESP:
658                         if (is_ipv4)
659                                 ptype = is_udp ? ICE_MAC_IPV4_NAT_T_ESP :
660                                                 ICE_MAC_IPV4_ESP;
661                         else if (is_ipv6)
662                                 ptype = is_udp ? ICE_MAC_IPV6_NAT_T_ESP :
663                                                 ICE_MAC_IPV6_ESP;
664                         goto out;
665                 case VIRTCHNL_PROTO_HDR_AH:
666                         if (is_ipv4)
667                                 ptype = ICE_MAC_IPV4_AH;
668                         else if (is_ipv6)
669                                 ptype = ICE_MAC_IPV6_AH;
670                         goto out;
671                 case VIRTCHNL_PROTO_HDR_PFCP:
672                         if (is_ipv4)
673                                 ptype = ICE_MAC_IPV4_PFCP_SESSION;
674                         else if (is_ipv6)
675                                 ptype = ICE_MAC_IPV6_PFCP_SESSION;
676                         goto out;
677                 default:
678                         break;
679                 }
680                 i++;
681         }
682
683 out:
684         return ice_hw_ptype_ena(&vf->pf->hw, ptype);
685 }
686
687 /**
688  * ice_vc_parse_rss_cfg - parses hash fields and headers from
689  * a specific virtchnl RSS cfg
690  * @hw: pointer to the hardware
691  * @rss_cfg: pointer to the virtchnl RSS cfg
692  * @addl_hdrs: pointer to the protocol header fields (ICE_FLOW_SEG_HDR_*)
693  * to configure
694  * @hash_flds: pointer to the hash bit fields (ICE_FLOW_HASH_*) to configure
695  *
696  * Return true if all the protocol header and hash fields in the RSS cfg could
697  * be parsed, else return false
698  *
699  * This function parses the virtchnl RSS cfg to be the intended
700  * hash fields and the intended header for RSS configuration
701  */
702 static bool
703 ice_vc_parse_rss_cfg(struct ice_hw *hw, struct virtchnl_rss_cfg *rss_cfg,
704                      u32 *addl_hdrs, u64 *hash_flds)
705 {
706         const struct ice_vc_hash_field_match_type *hf_list;
707         const struct ice_vc_hdr_match_type *hdr_list;
708         int i, hf_list_len, hdr_list_len;
709
710         hf_list = ice_vc_hash_field_list;
711         hf_list_len = ARRAY_SIZE(ice_vc_hash_field_list);
712         hdr_list = ice_vc_hdr_list;
713         hdr_list_len = ARRAY_SIZE(ice_vc_hdr_list);
714
715         for (i = 0; i < rss_cfg->proto_hdrs.count; i++) {
716                 struct virtchnl_proto_hdr *proto_hdr =
717                                         &rss_cfg->proto_hdrs.proto_hdr[i];
718                 bool hdr_found = false;
719                 int j;
720
721                 /* Find matched ice headers according to virtchnl headers. */
722                 for (j = 0; j < hdr_list_len; j++) {
723                         struct ice_vc_hdr_match_type hdr_map = hdr_list[j];
724
725                         if (proto_hdr->type == hdr_map.vc_hdr) {
726                                 *addl_hdrs |= hdr_map.ice_hdr;
727                                 hdr_found = true;
728                         }
729                 }
730
731                 if (!hdr_found)
732                         return false;
733
734                 /* Find matched ice hash fields according to
735                  * virtchnl hash fields.
736                  */
737                 for (j = 0; j < hf_list_len; j++) {
738                         struct ice_vc_hash_field_match_type hf_map = hf_list[j];
739
740                         if (proto_hdr->type == hf_map.vc_hdr &&
741                             proto_hdr->field_selector == hf_map.vc_hash_field) {
742                                 *hash_flds |= hf_map.ice_hash_field;
743                                 break;
744                         }
745                 }
746         }
747
748         return true;
749 }
750
751 /**
752  * ice_vf_adv_rss_offload_ena - determine if capabilities support advanced
753  * RSS offloads
754  * @caps: VF driver negotiated capabilities
755  *
756  * Return true if VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF capability is set,
757  * else return false
758  */
759 static bool ice_vf_adv_rss_offload_ena(u32 caps)
760 {
761         return !!(caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF);
762 }
763
764 /**
765  * ice_vc_handle_rss_cfg
766  * @vf: pointer to the VF info
767  * @msg: pointer to the message buffer
768  * @add: add a RSS config if true, otherwise delete a RSS config
769  *
770  * This function adds/deletes a RSS config
771  */
772 static int ice_vc_handle_rss_cfg(struct ice_vf *vf, u8 *msg, bool add)
773 {
774         u32 v_opcode = add ? VIRTCHNL_OP_ADD_RSS_CFG : VIRTCHNL_OP_DEL_RSS_CFG;
775         struct virtchnl_rss_cfg *rss_cfg = (struct virtchnl_rss_cfg *)msg;
776         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
777         struct device *dev = ice_pf_to_dev(vf->pf);
778         struct ice_hw *hw = &vf->pf->hw;
779         struct ice_vsi *vsi;
780
781         if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
782                 dev_dbg(dev, "VF %d attempting to configure RSS, but RSS is not supported by the PF\n",
783                         vf->vf_id);
784                 v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
785                 goto error_param;
786         }
787
788         if (!ice_vf_adv_rss_offload_ena(vf->driver_caps)) {
789                 dev_dbg(dev, "VF %d attempting to configure RSS, but Advanced RSS offload is not supported\n",
790                         vf->vf_id);
791                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
792                 goto error_param;
793         }
794
795         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
796                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
797                 goto error_param;
798         }
799
800         if (rss_cfg->proto_hdrs.count > VIRTCHNL_MAX_NUM_PROTO_HDRS ||
801             rss_cfg->rss_algorithm < VIRTCHNL_RSS_ALG_TOEPLITZ_ASYMMETRIC ||
802             rss_cfg->rss_algorithm > VIRTCHNL_RSS_ALG_XOR_SYMMETRIC) {
803                 dev_dbg(dev, "VF %d attempting to configure RSS, but RSS configuration is not valid\n",
804                         vf->vf_id);
805                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
806                 goto error_param;
807         }
808
809         vsi = ice_get_vf_vsi(vf);
810         if (!vsi) {
811                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
812                 goto error_param;
813         }
814
815         if (!ice_vc_validate_pattern(vf, &rss_cfg->proto_hdrs)) {
816                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
817                 goto error_param;
818         }
819
820         if (rss_cfg->rss_algorithm == VIRTCHNL_RSS_ALG_R_ASYMMETRIC) {
821                 struct ice_vsi_ctx *ctx;
822                 u8 lut_type, hash_type;
823                 int status;
824
825                 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
826                 hash_type = add ? ICE_AQ_VSI_Q_OPT_RSS_XOR :
827                                 ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
828
829                 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
830                 if (!ctx) {
831                         v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
832                         goto error_param;
833                 }
834
835                 ctx->info.q_opt_rss = ((lut_type <<
836                                         ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
837                                        ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
838                                        (hash_type &
839                                         ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
840
841                 /* Preserve existing queueing option setting */
842                 ctx->info.q_opt_rss |= (vsi->info.q_opt_rss &
843                                           ICE_AQ_VSI_Q_OPT_RSS_GBL_LUT_M);
844                 ctx->info.q_opt_tc = vsi->info.q_opt_tc;
845                 ctx->info.q_opt_flags = vsi->info.q_opt_rss;
846
847                 ctx->info.valid_sections =
848                                 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
849
850                 status = ice_update_vsi(hw, vsi->idx, ctx, NULL);
851                 if (status) {
852                         dev_err(dev, "update VSI for RSS failed, err %d aq_err %s\n",
853                                 status, ice_aq_str(hw->adminq.sq_last_status));
854                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
855                 } else {
856                         vsi->info.q_opt_rss = ctx->info.q_opt_rss;
857                 }
858
859                 kfree(ctx);
860         } else {
861                 u32 addl_hdrs = ICE_FLOW_SEG_HDR_NONE;
862                 u64 hash_flds = ICE_HASH_INVALID;
863
864                 if (!ice_vc_parse_rss_cfg(hw, rss_cfg, &addl_hdrs,
865                                           &hash_flds)) {
866                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
867                         goto error_param;
868                 }
869
870                 if (add) {
871                         if (ice_add_rss_cfg(hw, vsi->idx, hash_flds,
872                                             addl_hdrs)) {
873                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
874                                 dev_err(dev, "ice_add_rss_cfg failed for vsi = %d, v_ret = %d\n",
875                                         vsi->vsi_num, v_ret);
876                         }
877                 } else {
878                         int status;
879
880                         status = ice_rem_rss_cfg(hw, vsi->idx, hash_flds,
881                                                  addl_hdrs);
882                         /* We just ignore -ENOENT, because if two configurations
883                          * share the same profile remove one of them actually
884                          * removes both, since the profile is deleted.
885                          */
886                         if (status && status != -ENOENT) {
887                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
888                                 dev_err(dev, "ice_rem_rss_cfg failed for VF ID:%d, error:%d\n",
889                                         vf->vf_id, status);
890                         }
891                 }
892         }
893
894 error_param:
895         return ice_vc_send_msg_to_vf(vf, v_opcode, v_ret, NULL, 0);
896 }
897
898 /**
899  * ice_vc_config_rss_key
900  * @vf: pointer to the VF info
901  * @msg: pointer to the msg buffer
902  *
903  * Configure the VF's RSS key
904  */
905 static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
906 {
907         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
908         struct virtchnl_rss_key *vrk =
909                 (struct virtchnl_rss_key *)msg;
910         struct ice_vsi *vsi;
911
912         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
913                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
914                 goto error_param;
915         }
916
917         if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
918                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
919                 goto error_param;
920         }
921
922         if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
923                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
924                 goto error_param;
925         }
926
927         if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
928                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
929                 goto error_param;
930         }
931
932         vsi = ice_get_vf_vsi(vf);
933         if (!vsi) {
934                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
935                 goto error_param;
936         }
937
938         if (ice_set_rss_key(vsi, vrk->key))
939                 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
940 error_param:
941         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
942                                      NULL, 0);
943 }
944
945 /**
946  * ice_vc_config_rss_lut
947  * @vf: pointer to the VF info
948  * @msg: pointer to the msg buffer
949  *
950  * Configure the VF's RSS LUT
951  */
952 static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
953 {
954         struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
955         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
956         struct ice_vsi *vsi;
957
958         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
959                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
960                 goto error_param;
961         }
962
963         if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
964                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
965                 goto error_param;
966         }
967
968         if (vrl->lut_entries != ICE_LUT_VSI_SIZE) {
969                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
970                 goto error_param;
971         }
972
973         if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
974                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
975                 goto error_param;
976         }
977
978         vsi = ice_get_vf_vsi(vf);
979         if (!vsi) {
980                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
981                 goto error_param;
982         }
983
984         if (ice_set_rss_lut(vsi, vrl->lut, ICE_LUT_VSI_SIZE))
985                 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
986 error_param:
987         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
988                                      NULL, 0);
989 }
990
991 /**
992  * ice_vc_cfg_promiscuous_mode_msg
993  * @vf: pointer to the VF info
994  * @msg: pointer to the msg buffer
995  *
996  * called from the VF to configure VF VSIs promiscuous mode
997  */
998 static int ice_vc_cfg_promiscuous_mode_msg(struct ice_vf *vf, u8 *msg)
999 {
1000         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1001         bool rm_promisc, alluni = false, allmulti = false;
1002         struct virtchnl_promisc_info *info =
1003             (struct virtchnl_promisc_info *)msg;
1004         struct ice_vsi_vlan_ops *vlan_ops;
1005         int mcast_err = 0, ucast_err = 0;
1006         struct ice_pf *pf = vf->pf;
1007         struct ice_vsi *vsi;
1008         u8 mcast_m, ucast_m;
1009         struct device *dev;
1010         int ret = 0;
1011
1012         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1013                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1014                 goto error_param;
1015         }
1016
1017         if (!ice_vc_isvalid_vsi_id(vf, info->vsi_id)) {
1018                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1019                 goto error_param;
1020         }
1021
1022         vsi = ice_get_vf_vsi(vf);
1023         if (!vsi) {
1024                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1025                 goto error_param;
1026         }
1027
1028         dev = ice_pf_to_dev(pf);
1029         if (!ice_is_vf_trusted(vf)) {
1030                 dev_err(dev, "Unprivileged VF %d is attempting to configure promiscuous mode\n",
1031                         vf->vf_id);
1032                 /* Leave v_ret alone, lie to the VF on purpose. */
1033                 goto error_param;
1034         }
1035
1036         if (info->flags & FLAG_VF_UNICAST_PROMISC)
1037                 alluni = true;
1038
1039         if (info->flags & FLAG_VF_MULTICAST_PROMISC)
1040                 allmulti = true;
1041
1042         rm_promisc = !allmulti && !alluni;
1043
1044         vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
1045         if (rm_promisc)
1046                 ret = vlan_ops->ena_rx_filtering(vsi);
1047         else
1048                 ret = vlan_ops->dis_rx_filtering(vsi);
1049         if (ret) {
1050                 dev_err(dev, "Failed to configure VLAN pruning in promiscuous mode\n");
1051                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1052                 goto error_param;
1053         }
1054
1055         ice_vf_get_promisc_masks(vf, vsi, &ucast_m, &mcast_m);
1056
1057         if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
1058                 if (alluni) {
1059                         /* in this case we're turning on promiscuous mode */
1060                         ret = ice_set_dflt_vsi(vsi);
1061                 } else {
1062                         /* in this case we're turning off promiscuous mode */
1063                         if (ice_is_dflt_vsi_in_use(vsi->port_info))
1064                                 ret = ice_clear_dflt_vsi(vsi);
1065                 }
1066
1067                 /* in this case we're turning on/off only
1068                  * allmulticast
1069                  */
1070                 if (allmulti)
1071                         mcast_err = ice_vf_set_vsi_promisc(vf, vsi, mcast_m);
1072                 else
1073                         mcast_err = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
1074
1075                 if (ret) {
1076                         dev_err(dev, "Turning on/off promiscuous mode for VF %d failed, error: %d\n",
1077                                 vf->vf_id, ret);
1078                         v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1079                         goto error_param;
1080                 }
1081         } else {
1082                 if (alluni)
1083                         ucast_err = ice_vf_set_vsi_promisc(vf, vsi, ucast_m);
1084                 else
1085                         ucast_err = ice_vf_clear_vsi_promisc(vf, vsi, ucast_m);
1086
1087                 if (allmulti)
1088                         mcast_err = ice_vf_set_vsi_promisc(vf, vsi, mcast_m);
1089                 else
1090                         mcast_err = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
1091
1092                 if (ucast_err || mcast_err)
1093                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1094         }
1095
1096         if (!mcast_err) {
1097                 if (allmulti &&
1098                     !test_and_set_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
1099                         dev_info(dev, "VF %u successfully set multicast promiscuous mode\n",
1100                                  vf->vf_id);
1101                 else if (!allmulti &&
1102                          test_and_clear_bit(ICE_VF_STATE_MC_PROMISC,
1103                                             vf->vf_states))
1104                         dev_info(dev, "VF %u successfully unset multicast promiscuous mode\n",
1105                                  vf->vf_id);
1106         } else {
1107                 dev_err(dev, "Error while modifying multicast promiscuous mode for VF %u, error: %d\n",
1108                         vf->vf_id, mcast_err);
1109         }
1110
1111         if (!ucast_err) {
1112                 if (alluni &&
1113                     !test_and_set_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
1114                         dev_info(dev, "VF %u successfully set unicast promiscuous mode\n",
1115                                  vf->vf_id);
1116                 else if (!alluni &&
1117                          test_and_clear_bit(ICE_VF_STATE_UC_PROMISC,
1118                                             vf->vf_states))
1119                         dev_info(dev, "VF %u successfully unset unicast promiscuous mode\n",
1120                                  vf->vf_id);
1121         } else {
1122                 dev_err(dev, "Error while modifying unicast promiscuous mode for VF %u, error: %d\n",
1123                         vf->vf_id, ucast_err);
1124         }
1125
1126 error_param:
1127         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1128                                      v_ret, NULL, 0);
1129 }
1130
1131 /**
1132  * ice_vc_get_stats_msg
1133  * @vf: pointer to the VF info
1134  * @msg: pointer to the msg buffer
1135  *
1136  * called from the VF to get VSI stats
1137  */
1138 static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
1139 {
1140         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1141         struct virtchnl_queue_select *vqs =
1142                 (struct virtchnl_queue_select *)msg;
1143         struct ice_eth_stats stats = { 0 };
1144         struct ice_vsi *vsi;
1145
1146         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1147                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1148                 goto error_param;
1149         }
1150
1151         if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1152                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1153                 goto error_param;
1154         }
1155
1156         vsi = ice_get_vf_vsi(vf);
1157         if (!vsi) {
1158                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1159                 goto error_param;
1160         }
1161
1162         ice_update_eth_stats(vsi);
1163
1164         stats = vsi->eth_stats;
1165
1166 error_param:
1167         /* send the response to the VF */
1168         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
1169                                      (u8 *)&stats, sizeof(stats));
1170 }
1171
1172 /**
1173  * ice_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTCHNL
1174  * @vqs: virtchnl_queue_select structure containing bitmaps to validate
1175  *
1176  * Return true on successful validation, else false
1177  */
1178 static bool ice_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
1179 {
1180         if ((!vqs->rx_queues && !vqs->tx_queues) ||
1181             vqs->rx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF) ||
1182             vqs->tx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF))
1183                 return false;
1184
1185         return true;
1186 }
1187
1188 /**
1189  * ice_vf_ena_txq_interrupt - enable Tx queue interrupt via QINT_TQCTL
1190  * @vsi: VSI of the VF to configure
1191  * @q_idx: VF queue index used to determine the queue in the PF's space
1192  */
1193 static void ice_vf_ena_txq_interrupt(struct ice_vsi *vsi, u32 q_idx)
1194 {
1195         struct ice_hw *hw = &vsi->back->hw;
1196         u32 pfq = vsi->txq_map[q_idx];
1197         u32 reg;
1198
1199         reg = rd32(hw, QINT_TQCTL(pfq));
1200
1201         /* MSI-X index 0 in the VF's space is always for the OICR, which means
1202          * this is most likely a poll mode VF driver, so don't enable an
1203          * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
1204          */
1205         if (!(reg & QINT_TQCTL_MSIX_INDX_M))
1206                 return;
1207
1208         wr32(hw, QINT_TQCTL(pfq), reg | QINT_TQCTL_CAUSE_ENA_M);
1209 }
1210
1211 /**
1212  * ice_vf_ena_rxq_interrupt - enable Tx queue interrupt via QINT_RQCTL
1213  * @vsi: VSI of the VF to configure
1214  * @q_idx: VF queue index used to determine the queue in the PF's space
1215  */
1216 static void ice_vf_ena_rxq_interrupt(struct ice_vsi *vsi, u32 q_idx)
1217 {
1218         struct ice_hw *hw = &vsi->back->hw;
1219         u32 pfq = vsi->rxq_map[q_idx];
1220         u32 reg;
1221
1222         reg = rd32(hw, QINT_RQCTL(pfq));
1223
1224         /* MSI-X index 0 in the VF's space is always for the OICR, which means
1225          * this is most likely a poll mode VF driver, so don't enable an
1226          * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
1227          */
1228         if (!(reg & QINT_RQCTL_MSIX_INDX_M))
1229                 return;
1230
1231         wr32(hw, QINT_RQCTL(pfq), reg | QINT_RQCTL_CAUSE_ENA_M);
1232 }
1233
1234 /**
1235  * ice_vc_ena_qs_msg
1236  * @vf: pointer to the VF info
1237  * @msg: pointer to the msg buffer
1238  *
1239  * called from the VF to enable all or specific queue(s)
1240  */
1241 static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
1242 {
1243         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1244         struct virtchnl_queue_select *vqs =
1245             (struct virtchnl_queue_select *)msg;
1246         struct ice_vsi *vsi;
1247         unsigned long q_map;
1248         u16 vf_q_id;
1249
1250         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1251                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1252                 goto error_param;
1253         }
1254
1255         if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1256                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1257                 goto error_param;
1258         }
1259
1260         if (!ice_vc_validate_vqs_bitmaps(vqs)) {
1261                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1262                 goto error_param;
1263         }
1264
1265         vsi = ice_get_vf_vsi(vf);
1266         if (!vsi) {
1267                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1268                 goto error_param;
1269         }
1270
1271         /* Enable only Rx rings, Tx rings were enabled by the FW when the
1272          * Tx queue group list was configured and the context bits were
1273          * programmed using ice_vsi_cfg_txqs
1274          */
1275         q_map = vqs->rx_queues;
1276         for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1277                 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
1278                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1279                         goto error_param;
1280                 }
1281
1282                 /* Skip queue if enabled */
1283                 if (test_bit(vf_q_id, vf->rxq_ena))
1284                         continue;
1285
1286                 if (ice_vsi_ctrl_one_rx_ring(vsi, true, vf_q_id, true)) {
1287                         dev_err(ice_pf_to_dev(vsi->back), "Failed to enable Rx ring %d on VSI %d\n",
1288                                 vf_q_id, vsi->vsi_num);
1289                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1290                         goto error_param;
1291                 }
1292
1293                 ice_vf_ena_rxq_interrupt(vsi, vf_q_id);
1294                 set_bit(vf_q_id, vf->rxq_ena);
1295         }
1296
1297         q_map = vqs->tx_queues;
1298         for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1299                 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
1300                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1301                         goto error_param;
1302                 }
1303
1304                 /* Skip queue if enabled */
1305                 if (test_bit(vf_q_id, vf->txq_ena))
1306                         continue;
1307
1308                 ice_vf_ena_txq_interrupt(vsi, vf_q_id);
1309                 set_bit(vf_q_id, vf->txq_ena);
1310         }
1311
1312         /* Set flag to indicate that queues are enabled */
1313         if (v_ret == VIRTCHNL_STATUS_SUCCESS)
1314                 set_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
1315
1316 error_param:
1317         /* send the response to the VF */
1318         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
1319                                      NULL, 0);
1320 }
1321
1322 /**
1323  * ice_vf_vsi_dis_single_txq - disable a single Tx queue
1324  * @vf: VF to disable queue for
1325  * @vsi: VSI for the VF
1326  * @q_id: VF relative (0-based) queue ID
1327  *
1328  * Attempt to disable the Tx queue passed in. If the Tx queue was successfully
1329  * disabled then clear q_id bit in the enabled queues bitmap and return
1330  * success. Otherwise return error.
1331  */
1332 static int
1333 ice_vf_vsi_dis_single_txq(struct ice_vf *vf, struct ice_vsi *vsi, u16 q_id)
1334 {
1335         struct ice_txq_meta txq_meta = { 0 };
1336         struct ice_tx_ring *ring;
1337         int err;
1338
1339         if (!test_bit(q_id, vf->txq_ena))
1340                 dev_dbg(ice_pf_to_dev(vsi->back), "Queue %u on VSI %u is not enabled, but stopping it anyway\n",
1341                         q_id, vsi->vsi_num);
1342
1343         ring = vsi->tx_rings[q_id];
1344         if (!ring)
1345                 return -EINVAL;
1346
1347         ice_fill_txq_meta(vsi, ring, &txq_meta);
1348
1349         err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, vf->vf_id, ring, &txq_meta);
1350         if (err) {
1351                 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Tx ring %d on VSI %d\n",
1352                         q_id, vsi->vsi_num);
1353                 return err;
1354         }
1355
1356         /* Clear enabled queues flag */
1357         clear_bit(q_id, vf->txq_ena);
1358
1359         return 0;
1360 }
1361
1362 /**
1363  * ice_vc_dis_qs_msg
1364  * @vf: pointer to the VF info
1365  * @msg: pointer to the msg buffer
1366  *
1367  * called from the VF to disable all or specific queue(s)
1368  */
1369 static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
1370 {
1371         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1372         struct virtchnl_queue_select *vqs =
1373             (struct virtchnl_queue_select *)msg;
1374         struct ice_vsi *vsi;
1375         unsigned long q_map;
1376         u16 vf_q_id;
1377
1378         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
1379             !test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states)) {
1380                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1381                 goto error_param;
1382         }
1383
1384         if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1385                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1386                 goto error_param;
1387         }
1388
1389         if (!ice_vc_validate_vqs_bitmaps(vqs)) {
1390                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1391                 goto error_param;
1392         }
1393
1394         vsi = ice_get_vf_vsi(vf);
1395         if (!vsi) {
1396                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1397                 goto error_param;
1398         }
1399
1400         if (vqs->tx_queues) {
1401                 q_map = vqs->tx_queues;
1402
1403                 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1404                         if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
1405                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1406                                 goto error_param;
1407                         }
1408
1409                         if (ice_vf_vsi_dis_single_txq(vf, vsi, vf_q_id)) {
1410                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1411                                 goto error_param;
1412                         }
1413                 }
1414         }
1415
1416         q_map = vqs->rx_queues;
1417         /* speed up Rx queue disable by batching them if possible */
1418         if (q_map &&
1419             bitmap_equal(&q_map, vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF)) {
1420                 if (ice_vsi_stop_all_rx_rings(vsi)) {
1421                         dev_err(ice_pf_to_dev(vsi->back), "Failed to stop all Rx rings on VSI %d\n",
1422                                 vsi->vsi_num);
1423                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1424                         goto error_param;
1425                 }
1426
1427                 bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
1428         } else if (q_map) {
1429                 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1430                         if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
1431                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1432                                 goto error_param;
1433                         }
1434
1435                         /* Skip queue if not enabled */
1436                         if (!test_bit(vf_q_id, vf->rxq_ena))
1437                                 continue;
1438
1439                         if (ice_vsi_ctrl_one_rx_ring(vsi, false, vf_q_id,
1440                                                      true)) {
1441                                 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Rx ring %d on VSI %d\n",
1442                                         vf_q_id, vsi->vsi_num);
1443                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1444                                 goto error_param;
1445                         }
1446
1447                         /* Clear enabled queues flag */
1448                         clear_bit(vf_q_id, vf->rxq_ena);
1449                 }
1450         }
1451
1452         /* Clear enabled queues flag */
1453         if (v_ret == VIRTCHNL_STATUS_SUCCESS && ice_vf_has_no_qs_ena(vf))
1454                 clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
1455
1456 error_param:
1457         /* send the response to the VF */
1458         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
1459                                      NULL, 0);
1460 }
1461
1462 /**
1463  * ice_cfg_interrupt
1464  * @vf: pointer to the VF info
1465  * @vsi: the VSI being configured
1466  * @vector_id: vector ID
1467  * @map: vector map for mapping vectors to queues
1468  * @q_vector: structure for interrupt vector
1469  * configure the IRQ to queue map
1470  */
1471 static int
1472 ice_cfg_interrupt(struct ice_vf *vf, struct ice_vsi *vsi, u16 vector_id,
1473                   struct virtchnl_vector_map *map,
1474                   struct ice_q_vector *q_vector)
1475 {
1476         u16 vsi_q_id, vsi_q_id_idx;
1477         unsigned long qmap;
1478
1479         q_vector->num_ring_rx = 0;
1480         q_vector->num_ring_tx = 0;
1481
1482         qmap = map->rxq_map;
1483         for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
1484                 vsi_q_id = vsi_q_id_idx;
1485
1486                 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
1487                         return VIRTCHNL_STATUS_ERR_PARAM;
1488
1489                 q_vector->num_ring_rx++;
1490                 q_vector->rx.itr_idx = map->rxitr_idx;
1491                 vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
1492                 ice_cfg_rxq_interrupt(vsi, vsi_q_id, vector_id,
1493                                       q_vector->rx.itr_idx);
1494         }
1495
1496         qmap = map->txq_map;
1497         for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
1498                 vsi_q_id = vsi_q_id_idx;
1499
1500                 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
1501                         return VIRTCHNL_STATUS_ERR_PARAM;
1502
1503                 q_vector->num_ring_tx++;
1504                 q_vector->tx.itr_idx = map->txitr_idx;
1505                 vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
1506                 ice_cfg_txq_interrupt(vsi, vsi_q_id, vector_id,
1507                                       q_vector->tx.itr_idx);
1508         }
1509
1510         return VIRTCHNL_STATUS_SUCCESS;
1511 }
1512
1513 /**
1514  * ice_vc_cfg_irq_map_msg
1515  * @vf: pointer to the VF info
1516  * @msg: pointer to the msg buffer
1517  *
1518  * called from the VF to configure the IRQ to queue map
1519  */
1520 static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
1521 {
1522         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1523         u16 num_q_vectors_mapped, vsi_id, vector_id;
1524         struct virtchnl_irq_map_info *irqmap_info;
1525         struct virtchnl_vector_map *map;
1526         struct ice_vsi *vsi;
1527         int i;
1528
1529         irqmap_info = (struct virtchnl_irq_map_info *)msg;
1530         num_q_vectors_mapped = irqmap_info->num_vectors;
1531
1532         /* Check to make sure number of VF vectors mapped is not greater than
1533          * number of VF vectors originally allocated, and check that
1534          * there is actually at least a single VF queue vector mapped
1535          */
1536         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
1537             vf->num_msix < num_q_vectors_mapped ||
1538             !num_q_vectors_mapped) {
1539                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1540                 goto error_param;
1541         }
1542
1543         vsi = ice_get_vf_vsi(vf);
1544         if (!vsi) {
1545                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1546                 goto error_param;
1547         }
1548
1549         for (i = 0; i < num_q_vectors_mapped; i++) {
1550                 struct ice_q_vector *q_vector;
1551
1552                 map = &irqmap_info->vecmap[i];
1553
1554                 vector_id = map->vector_id;
1555                 vsi_id = map->vsi_id;
1556                 /* vector_id is always 0-based for each VF, and can never be
1557                  * larger than or equal to the max allowed interrupts per VF
1558                  */
1559                 if (!(vector_id < vf->num_msix) ||
1560                     !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
1561                     (!vector_id && (map->rxq_map || map->txq_map))) {
1562                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1563                         goto error_param;
1564                 }
1565
1566                 /* No need to map VF miscellaneous or rogue vector */
1567                 if (!vector_id)
1568                         continue;
1569
1570                 /* Subtract non queue vector from vector_id passed by VF
1571                  * to get actual number of VSI queue vector array index
1572                  */
1573                 q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
1574                 if (!q_vector) {
1575                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1576                         goto error_param;
1577                 }
1578
1579                 /* lookout for the invalid queue index */
1580                 v_ret = (enum virtchnl_status_code)
1581                         ice_cfg_interrupt(vf, vsi, vector_id, map, q_vector);
1582                 if (v_ret)
1583                         goto error_param;
1584         }
1585
1586 error_param:
1587         /* send the response to the VF */
1588         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
1589                                      NULL, 0);
1590 }
1591
1592 /**
1593  * ice_vc_cfg_qs_msg
1594  * @vf: pointer to the VF info
1595  * @msg: pointer to the msg buffer
1596  *
1597  * called from the VF to configure the Rx/Tx queues
1598  */
1599 static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
1600 {
1601         struct virtchnl_vsi_queue_config_info *qci =
1602             (struct virtchnl_vsi_queue_config_info *)msg;
1603         struct virtchnl_queue_pair_info *qpi;
1604         struct ice_pf *pf = vf->pf;
1605         struct ice_lag *lag;
1606         struct ice_vsi *vsi;
1607         u8 act_prt, pri_prt;
1608         int i = -1, q_idx;
1609
1610         lag = pf->lag;
1611         mutex_lock(&pf->lag_mutex);
1612         act_prt = ICE_LAG_INVALID_PORT;
1613         pri_prt = pf->hw.port_info->lport;
1614         if (lag && lag->bonded && lag->primary) {
1615                 act_prt = lag->active_port;
1616                 if (act_prt != pri_prt && act_prt != ICE_LAG_INVALID_PORT &&
1617                     lag->upper_netdev)
1618                         ice_lag_move_vf_nodes_cfg(lag, act_prt, pri_prt);
1619                 else
1620                         act_prt = ICE_LAG_INVALID_PORT;
1621         }
1622
1623         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
1624                 goto error_param;
1625
1626         if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id))
1627                 goto error_param;
1628
1629         vsi = ice_get_vf_vsi(vf);
1630         if (!vsi)
1631                 goto error_param;
1632
1633         if (qci->num_queue_pairs > ICE_MAX_RSS_QS_PER_VF ||
1634             qci->num_queue_pairs > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
1635                 dev_err(ice_pf_to_dev(pf), "VF-%d requesting more than supported number of queues: %d\n",
1636                         vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
1637                 goto error_param;
1638         }
1639
1640         for (i = 0; i < qci->num_queue_pairs; i++) {
1641                 if (!qci->qpair[i].rxq.crc_disable)
1642                         continue;
1643
1644                 if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_CRC) ||
1645                     vf->vlan_strip_ena)
1646                         goto error_param;
1647         }
1648
1649         for (i = 0; i < qci->num_queue_pairs; i++) {
1650                 qpi = &qci->qpair[i];
1651                 if (qpi->txq.vsi_id != qci->vsi_id ||
1652                     qpi->rxq.vsi_id != qci->vsi_id ||
1653                     qpi->rxq.queue_id != qpi->txq.queue_id ||
1654                     qpi->txq.headwb_enabled ||
1655                     !ice_vc_isvalid_ring_len(qpi->txq.ring_len) ||
1656                     !ice_vc_isvalid_ring_len(qpi->rxq.ring_len) ||
1657                     !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
1658                         goto error_param;
1659                 }
1660
1661                 q_idx = qpi->rxq.queue_id;
1662
1663                 /* make sure selected "q_idx" is in valid range of queues
1664                  * for selected "vsi"
1665                  */
1666                 if (q_idx >= vsi->alloc_txq || q_idx >= vsi->alloc_rxq) {
1667                         goto error_param;
1668                 }
1669
1670                 /* copy Tx queue info from VF into VSI */
1671                 if (qpi->txq.ring_len > 0) {
1672                         vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
1673                         vsi->tx_rings[i]->count = qpi->txq.ring_len;
1674
1675                         /* Disable any existing queue first */
1676                         if (ice_vf_vsi_dis_single_txq(vf, vsi, q_idx))
1677                                 goto error_param;
1678
1679                         /* Configure a queue with the requested settings */
1680                         if (ice_vsi_cfg_single_txq(vsi, vsi->tx_rings, q_idx)) {
1681                                 dev_warn(ice_pf_to_dev(pf), "VF-%d failed to configure TX queue %d\n",
1682                                          vf->vf_id, i);
1683                                 goto error_param;
1684                         }
1685                 }
1686
1687                 /* copy Rx queue info from VF into VSI */
1688                 if (qpi->rxq.ring_len > 0) {
1689                         u16 max_frame_size = ice_vc_get_max_frame_size(vf);
1690                         u32 rxdid;
1691
1692                         vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
1693                         vsi->rx_rings[i]->count = qpi->rxq.ring_len;
1694
1695                         if (qpi->rxq.crc_disable)
1696                                 vsi->rx_rings[q_idx]->flags |=
1697                                         ICE_RX_FLAGS_CRC_STRIP_DIS;
1698                         else
1699                                 vsi->rx_rings[q_idx]->flags &=
1700                                         ~ICE_RX_FLAGS_CRC_STRIP_DIS;
1701
1702                         if (qpi->rxq.databuffer_size != 0 &&
1703                             (qpi->rxq.databuffer_size > ((16 * 1024) - 128) ||
1704                              qpi->rxq.databuffer_size < 1024))
1705                                 goto error_param;
1706                         vsi->rx_buf_len = qpi->rxq.databuffer_size;
1707                         vsi->rx_rings[i]->rx_buf_len = vsi->rx_buf_len;
1708                         if (qpi->rxq.max_pkt_size > max_frame_size ||
1709                             qpi->rxq.max_pkt_size < 64)
1710                                 goto error_param;
1711
1712                         vsi->max_frame = qpi->rxq.max_pkt_size;
1713                         /* add space for the port VLAN since the VF driver is
1714                          * not expected to account for it in the MTU
1715                          * calculation
1716                          */
1717                         if (ice_vf_is_port_vlan_ena(vf))
1718                                 vsi->max_frame += VLAN_HLEN;
1719
1720                         if (ice_vsi_cfg_single_rxq(vsi, q_idx)) {
1721                                 dev_warn(ice_pf_to_dev(pf), "VF-%d failed to configure RX queue %d\n",
1722                                          vf->vf_id, i);
1723                                 goto error_param;
1724                         }
1725
1726                         /* If Rx flex desc is supported, select RXDID for Rx
1727                          * queues. Otherwise, use legacy 32byte descriptor
1728                          * format. Legacy 16byte descriptor is not supported.
1729                          * If this RXDID is selected, return error.
1730                          */
1731                         if (vf->driver_caps &
1732                             VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC) {
1733                                 rxdid = qpi->rxq.rxdid;
1734                                 if (!(BIT(rxdid) & pf->supported_rxdids))
1735                                         goto error_param;
1736                         } else {
1737                                 rxdid = ICE_RXDID_LEGACY_1;
1738                         }
1739
1740                         ice_write_qrxflxp_cntxt(&vsi->back->hw,
1741                                                 vsi->rxq_map[q_idx],
1742                                                 rxdid, 0x03, false);
1743                 }
1744         }
1745
1746         if (lag && lag->bonded && lag->primary &&
1747             act_prt != ICE_LAG_INVALID_PORT)
1748                 ice_lag_move_vf_nodes_cfg(lag, pri_prt, act_prt);
1749         mutex_unlock(&pf->lag_mutex);
1750
1751         /* send the response to the VF */
1752         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1753                                      VIRTCHNL_STATUS_SUCCESS, NULL, 0);
1754 error_param:
1755         /* disable whatever we can */
1756         for (; i >= 0; i--) {
1757                 if (ice_vsi_ctrl_one_rx_ring(vsi, false, i, true))
1758                         dev_err(ice_pf_to_dev(pf), "VF-%d could not disable RX queue %d\n",
1759                                 vf->vf_id, i);
1760                 if (ice_vf_vsi_dis_single_txq(vf, vsi, i))
1761                         dev_err(ice_pf_to_dev(pf), "VF-%d could not disable TX queue %d\n",
1762                                 vf->vf_id, i);
1763         }
1764
1765         if (lag && lag->bonded && lag->primary &&
1766             act_prt != ICE_LAG_INVALID_PORT)
1767                 ice_lag_move_vf_nodes_cfg(lag, pri_prt, act_prt);
1768         mutex_unlock(&pf->lag_mutex);
1769
1770         ice_lag_move_new_vf_nodes(vf);
1771
1772         /* send the response to the VF */
1773         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1774                                      VIRTCHNL_STATUS_ERR_PARAM, NULL, 0);
1775 }
1776
1777 /**
1778  * ice_can_vf_change_mac
1779  * @vf: pointer to the VF info
1780  *
1781  * Return true if the VF is allowed to change its MAC filters, false otherwise
1782  */
1783 static bool ice_can_vf_change_mac(struct ice_vf *vf)
1784 {
1785         /* If the VF MAC address has been set administratively (via the
1786          * ndo_set_vf_mac command), then deny permission to the VF to
1787          * add/delete unicast MAC addresses, unless the VF is trusted
1788          */
1789         if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
1790                 return false;
1791
1792         return true;
1793 }
1794
1795 /**
1796  * ice_vc_ether_addr_type - get type of virtchnl_ether_addr
1797  * @vc_ether_addr: used to extract the type
1798  */
1799 static u8
1800 ice_vc_ether_addr_type(struct virtchnl_ether_addr *vc_ether_addr)
1801 {
1802         return (vc_ether_addr->type & VIRTCHNL_ETHER_ADDR_TYPE_MASK);
1803 }
1804
1805 /**
1806  * ice_is_vc_addr_legacy - check if the MAC address is from an older VF
1807  * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
1808  */
1809 static bool
1810 ice_is_vc_addr_legacy(struct virtchnl_ether_addr *vc_ether_addr)
1811 {
1812         u8 type = ice_vc_ether_addr_type(vc_ether_addr);
1813
1814         return (type == VIRTCHNL_ETHER_ADDR_LEGACY);
1815 }
1816
1817 /**
1818  * ice_is_vc_addr_primary - check if the MAC address is the VF's primary MAC
1819  * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
1820  *
1821  * This function should only be called when the MAC address in
1822  * virtchnl_ether_addr is a valid unicast MAC
1823  */
1824 static bool
1825 ice_is_vc_addr_primary(struct virtchnl_ether_addr __maybe_unused *vc_ether_addr)
1826 {
1827         u8 type = ice_vc_ether_addr_type(vc_ether_addr);
1828
1829         return (type == VIRTCHNL_ETHER_ADDR_PRIMARY);
1830 }
1831
1832 /**
1833  * ice_vfhw_mac_add - update the VF's cached hardware MAC if allowed
1834  * @vf: VF to update
1835  * @vc_ether_addr: structure from VIRTCHNL with MAC to add
1836  */
1837 static void
1838 ice_vfhw_mac_add(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
1839 {
1840         u8 *mac_addr = vc_ether_addr->addr;
1841
1842         if (!is_valid_ether_addr(mac_addr))
1843                 return;
1844
1845         /* only allow legacy VF drivers to set the device and hardware MAC if it
1846          * is zero and allow new VF drivers to set the hardware MAC if the type
1847          * was correctly specified over VIRTCHNL
1848          */
1849         if ((ice_is_vc_addr_legacy(vc_ether_addr) &&
1850              is_zero_ether_addr(vf->hw_lan_addr)) ||
1851             ice_is_vc_addr_primary(vc_ether_addr)) {
1852                 ether_addr_copy(vf->dev_lan_addr, mac_addr);
1853                 ether_addr_copy(vf->hw_lan_addr, mac_addr);
1854         }
1855
1856         /* hardware and device MACs are already set, but its possible that the
1857          * VF driver sent the VIRTCHNL_OP_ADD_ETH_ADDR message before the
1858          * VIRTCHNL_OP_DEL_ETH_ADDR when trying to update its MAC, so save it
1859          * away for the legacy VF driver case as it will be updated in the
1860          * delete flow for this case
1861          */
1862         if (ice_is_vc_addr_legacy(vc_ether_addr)) {
1863                 ether_addr_copy(vf->legacy_last_added_umac.addr,
1864                                 mac_addr);
1865                 vf->legacy_last_added_umac.time_modified = jiffies;
1866         }
1867 }
1868
1869 /**
1870  * ice_vc_add_mac_addr - attempt to add the MAC address passed in
1871  * @vf: pointer to the VF info
1872  * @vsi: pointer to the VF's VSI
1873  * @vc_ether_addr: VIRTCHNL MAC address structure used to add MAC
1874  */
1875 static int
1876 ice_vc_add_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
1877                     struct virtchnl_ether_addr *vc_ether_addr)
1878 {
1879         struct device *dev = ice_pf_to_dev(vf->pf);
1880         u8 *mac_addr = vc_ether_addr->addr;
1881         int ret;
1882
1883         /* device MAC already added */
1884         if (ether_addr_equal(mac_addr, vf->dev_lan_addr))
1885                 return 0;
1886
1887         if (is_unicast_ether_addr(mac_addr) && !ice_can_vf_change_mac(vf)) {
1888                 dev_err(dev, "VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
1889                 return -EPERM;
1890         }
1891
1892         ret = ice_fltr_add_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
1893         if (ret == -EEXIST) {
1894                 dev_dbg(dev, "MAC %pM already exists for VF %d\n", mac_addr,
1895                         vf->vf_id);
1896                 /* don't return since we might need to update
1897                  * the primary MAC in ice_vfhw_mac_add() below
1898                  */
1899         } else if (ret) {
1900                 dev_err(dev, "Failed to add MAC %pM for VF %d\n, error %d\n",
1901                         mac_addr, vf->vf_id, ret);
1902                 return ret;
1903         } else {
1904                 vf->num_mac++;
1905         }
1906
1907         ice_vfhw_mac_add(vf, vc_ether_addr);
1908
1909         return ret;
1910 }
1911
1912 /**
1913  * ice_is_legacy_umac_expired - check if last added legacy unicast MAC expired
1914  * @last_added_umac: structure used to check expiration
1915  */
1916 static bool ice_is_legacy_umac_expired(struct ice_time_mac *last_added_umac)
1917 {
1918 #define ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME    msecs_to_jiffies(3000)
1919         return time_is_before_jiffies(last_added_umac->time_modified +
1920                                       ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME);
1921 }
1922
1923 /**
1924  * ice_update_legacy_cached_mac - update cached hardware MAC for legacy VF
1925  * @vf: VF to update
1926  * @vc_ether_addr: structure from VIRTCHNL with MAC to check
1927  *
1928  * only update cached hardware MAC for legacy VF drivers on delete
1929  * because we cannot guarantee order/type of MAC from the VF driver
1930  */
1931 static void
1932 ice_update_legacy_cached_mac(struct ice_vf *vf,
1933                              struct virtchnl_ether_addr *vc_ether_addr)
1934 {
1935         if (!ice_is_vc_addr_legacy(vc_ether_addr) ||
1936             ice_is_legacy_umac_expired(&vf->legacy_last_added_umac))
1937                 return;
1938
1939         ether_addr_copy(vf->dev_lan_addr, vf->legacy_last_added_umac.addr);
1940         ether_addr_copy(vf->hw_lan_addr, vf->legacy_last_added_umac.addr);
1941 }
1942
1943 /**
1944  * ice_vfhw_mac_del - update the VF's cached hardware MAC if allowed
1945  * @vf: VF to update
1946  * @vc_ether_addr: structure from VIRTCHNL with MAC to delete
1947  */
1948 static void
1949 ice_vfhw_mac_del(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
1950 {
1951         u8 *mac_addr = vc_ether_addr->addr;
1952
1953         if (!is_valid_ether_addr(mac_addr) ||
1954             !ether_addr_equal(vf->dev_lan_addr, mac_addr))
1955                 return;
1956
1957         /* allow the device MAC to be repopulated in the add flow and don't
1958          * clear the hardware MAC (i.e. hw_lan_addr) here as that is meant
1959          * to be persistent on VM reboot and across driver unload/load, which
1960          * won't work if we clear the hardware MAC here
1961          */
1962         eth_zero_addr(vf->dev_lan_addr);
1963
1964         ice_update_legacy_cached_mac(vf, vc_ether_addr);
1965 }
1966
1967 /**
1968  * ice_vc_del_mac_addr - attempt to delete the MAC address passed in
1969  * @vf: pointer to the VF info
1970  * @vsi: pointer to the VF's VSI
1971  * @vc_ether_addr: VIRTCHNL MAC address structure used to delete MAC
1972  */
1973 static int
1974 ice_vc_del_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
1975                     struct virtchnl_ether_addr *vc_ether_addr)
1976 {
1977         struct device *dev = ice_pf_to_dev(vf->pf);
1978         u8 *mac_addr = vc_ether_addr->addr;
1979         int status;
1980
1981         if (!ice_can_vf_change_mac(vf) &&
1982             ether_addr_equal(vf->dev_lan_addr, mac_addr))
1983                 return 0;
1984
1985         status = ice_fltr_remove_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
1986         if (status == -ENOENT) {
1987                 dev_err(dev, "MAC %pM does not exist for VF %d\n", mac_addr,
1988                         vf->vf_id);
1989                 return -ENOENT;
1990         } else if (status) {
1991                 dev_err(dev, "Failed to delete MAC %pM for VF %d, error %d\n",
1992                         mac_addr, vf->vf_id, status);
1993                 return -EIO;
1994         }
1995
1996         ice_vfhw_mac_del(vf, vc_ether_addr);
1997
1998         vf->num_mac--;
1999
2000         return 0;
2001 }
2002
2003 /**
2004  * ice_vc_handle_mac_addr_msg
2005  * @vf: pointer to the VF info
2006  * @msg: pointer to the msg buffer
2007  * @set: true if MAC filters are being set, false otherwise
2008  *
2009  * add guest MAC address filter
2010  */
2011 static int
2012 ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
2013 {
2014         int (*ice_vc_cfg_mac)
2015                 (struct ice_vf *vf, struct ice_vsi *vsi,
2016                  struct virtchnl_ether_addr *virtchnl_ether_addr);
2017         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2018         struct virtchnl_ether_addr_list *al =
2019             (struct virtchnl_ether_addr_list *)msg;
2020         struct ice_pf *pf = vf->pf;
2021         enum virtchnl_ops vc_op;
2022         struct ice_vsi *vsi;
2023         int i;
2024
2025         if (set) {
2026                 vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
2027                 ice_vc_cfg_mac = ice_vc_add_mac_addr;
2028         } else {
2029                 vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
2030                 ice_vc_cfg_mac = ice_vc_del_mac_addr;
2031         }
2032
2033         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
2034             !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
2035                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2036                 goto handle_mac_exit;
2037         }
2038
2039         /* If this VF is not privileged, then we can't add more than a
2040          * limited number of addresses. Check to make sure that the
2041          * additions do not push us over the limit.
2042          */
2043         if (set && !ice_is_vf_trusted(vf) &&
2044             (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
2045                 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",
2046                         vf->vf_id);
2047                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2048                 goto handle_mac_exit;
2049         }
2050
2051         vsi = ice_get_vf_vsi(vf);
2052         if (!vsi) {
2053                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2054                 goto handle_mac_exit;
2055         }
2056
2057         for (i = 0; i < al->num_elements; i++) {
2058                 u8 *mac_addr = al->list[i].addr;
2059                 int result;
2060
2061                 if (is_broadcast_ether_addr(mac_addr) ||
2062                     is_zero_ether_addr(mac_addr))
2063                         continue;
2064
2065                 result = ice_vc_cfg_mac(vf, vsi, &al->list[i]);
2066                 if (result == -EEXIST || result == -ENOENT) {
2067                         continue;
2068                 } else if (result) {
2069                         v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2070                         goto handle_mac_exit;
2071                 }
2072         }
2073
2074 handle_mac_exit:
2075         /* send the response to the VF */
2076         return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
2077 }
2078
2079 /**
2080  * ice_vc_add_mac_addr_msg
2081  * @vf: pointer to the VF info
2082  * @msg: pointer to the msg buffer
2083  *
2084  * add guest MAC address filter
2085  */
2086 static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2087 {
2088         return ice_vc_handle_mac_addr_msg(vf, msg, true);
2089 }
2090
2091 /**
2092  * ice_vc_del_mac_addr_msg
2093  * @vf: pointer to the VF info
2094  * @msg: pointer to the msg buffer
2095  *
2096  * remove guest MAC address filter
2097  */
2098 static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2099 {
2100         return ice_vc_handle_mac_addr_msg(vf, msg, false);
2101 }
2102
2103 /**
2104  * ice_vc_request_qs_msg
2105  * @vf: pointer to the VF info
2106  * @msg: pointer to the msg buffer
2107  *
2108  * VFs get a default number of queues but can use this message to request a
2109  * different number. If the request is successful, PF will reset the VF and
2110  * return 0. If unsuccessful, PF will send message informing VF of number of
2111  * available queue pairs via virtchnl message response to VF.
2112  */
2113 static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
2114 {
2115         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2116         struct virtchnl_vf_res_request *vfres =
2117                 (struct virtchnl_vf_res_request *)msg;
2118         u16 req_queues = vfres->num_queue_pairs;
2119         struct ice_pf *pf = vf->pf;
2120         u16 max_allowed_vf_queues;
2121         u16 tx_rx_queue_left;
2122         struct device *dev;
2123         u16 cur_queues;
2124
2125         dev = ice_pf_to_dev(pf);
2126         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2127                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2128                 goto error_param;
2129         }
2130
2131         cur_queues = vf->num_vf_qs;
2132         tx_rx_queue_left = min_t(u16, ice_get_avail_txq_count(pf),
2133                                  ice_get_avail_rxq_count(pf));
2134         max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
2135         if (!req_queues) {
2136                 dev_err(dev, "VF %d tried to request 0 queues. Ignoring.\n",
2137                         vf->vf_id);
2138         } else if (req_queues > ICE_MAX_RSS_QS_PER_VF) {
2139                 dev_err(dev, "VF %d tried to request more than %d queues.\n",
2140                         vf->vf_id, ICE_MAX_RSS_QS_PER_VF);
2141                 vfres->num_queue_pairs = ICE_MAX_RSS_QS_PER_VF;
2142         } else if (req_queues > cur_queues &&
2143                    req_queues - cur_queues > tx_rx_queue_left) {
2144                 dev_warn(dev, "VF %d requested %u more queues, but only %u left.\n",
2145                          vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
2146                 vfres->num_queue_pairs = min_t(u16, max_allowed_vf_queues,
2147                                                ICE_MAX_RSS_QS_PER_VF);
2148         } else {
2149                 /* request is successful, then reset VF */
2150                 vf->num_req_qs = req_queues;
2151                 ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
2152                 dev_info(dev, "VF %d granted request of %u queues.\n",
2153                          vf->vf_id, req_queues);
2154                 return 0;
2155         }
2156
2157 error_param:
2158         /* send the response to the VF */
2159         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
2160                                      v_ret, (u8 *)vfres, sizeof(*vfres));
2161 }
2162
2163 /**
2164  * ice_vf_vlan_offload_ena - determine if capabilities support VLAN offloads
2165  * @caps: VF driver negotiated capabilities
2166  *
2167  * Return true if VIRTCHNL_VF_OFFLOAD_VLAN capability is set, else return false
2168  */
2169 static bool ice_vf_vlan_offload_ena(u32 caps)
2170 {
2171         return !!(caps & VIRTCHNL_VF_OFFLOAD_VLAN);
2172 }
2173
2174 /**
2175  * ice_is_vlan_promisc_allowed - check if VLAN promiscuous config is allowed
2176  * @vf: VF used to determine if VLAN promiscuous config is allowed
2177  */
2178 static bool ice_is_vlan_promisc_allowed(struct ice_vf *vf)
2179 {
2180         if ((test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2181              test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) &&
2182             test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, vf->pf->flags))
2183                 return true;
2184
2185         return false;
2186 }
2187
2188 /**
2189  * ice_vf_ena_vlan_promisc - Enable Tx/Rx VLAN promiscuous for the VLAN
2190  * @vsi: VF's VSI used to enable VLAN promiscuous mode
2191  * @vlan: VLAN used to enable VLAN promiscuous
2192  *
2193  * This function should only be called if VLAN promiscuous mode is allowed,
2194  * which can be determined via ice_is_vlan_promisc_allowed().
2195  */
2196 static int ice_vf_ena_vlan_promisc(struct ice_vsi *vsi, struct ice_vlan *vlan)
2197 {
2198         u8 promisc_m = ICE_PROMISC_VLAN_TX | ICE_PROMISC_VLAN_RX;
2199         int status;
2200
2201         status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m,
2202                                           vlan->vid);
2203         if (status && status != -EEXIST)
2204                 return status;
2205
2206         return 0;
2207 }
2208
2209 /**
2210  * ice_vf_dis_vlan_promisc - Disable Tx/Rx VLAN promiscuous for the VLAN
2211  * @vsi: VF's VSI used to disable VLAN promiscuous mode for
2212  * @vlan: VLAN used to disable VLAN promiscuous
2213  *
2214  * This function should only be called if VLAN promiscuous mode is allowed,
2215  * which can be determined via ice_is_vlan_promisc_allowed().
2216  */
2217 static int ice_vf_dis_vlan_promisc(struct ice_vsi *vsi, struct ice_vlan *vlan)
2218 {
2219         u8 promisc_m = ICE_PROMISC_VLAN_TX | ICE_PROMISC_VLAN_RX;
2220         int status;
2221
2222         status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m,
2223                                             vlan->vid);
2224         if (status && status != -ENOENT)
2225                 return status;
2226
2227         return 0;
2228 }
2229
2230 /**
2231  * ice_vf_has_max_vlans - check if VF already has the max allowed VLAN filters
2232  * @vf: VF to check against
2233  * @vsi: VF's VSI
2234  *
2235  * If the VF is trusted then the VF is allowed to add as many VLANs as it
2236  * wants to, so return false.
2237  *
2238  * When the VF is untrusted compare the number of non-zero VLANs + 1 to the max
2239  * allowed VLANs for an untrusted VF. Return the result of this comparison.
2240  */
2241 static bool ice_vf_has_max_vlans(struct ice_vf *vf, struct ice_vsi *vsi)
2242 {
2243         if (ice_is_vf_trusted(vf))
2244                 return false;
2245
2246 #define ICE_VF_ADDED_VLAN_ZERO_FLTRS    1
2247         return ((ice_vsi_num_non_zero_vlans(vsi) +
2248                 ICE_VF_ADDED_VLAN_ZERO_FLTRS) >= ICE_MAX_VLAN_PER_VF);
2249 }
2250
2251 /**
2252  * ice_vc_process_vlan_msg
2253  * @vf: pointer to the VF info
2254  * @msg: pointer to the msg buffer
2255  * @add_v: Add VLAN if true, otherwise delete VLAN
2256  *
2257  * Process virtchnl op to add or remove programmed guest VLAN ID
2258  */
2259 static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
2260 {
2261         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2262         struct virtchnl_vlan_filter_list *vfl =
2263             (struct virtchnl_vlan_filter_list *)msg;
2264         struct ice_pf *pf = vf->pf;
2265         bool vlan_promisc = false;
2266         struct ice_vsi *vsi;
2267         struct device *dev;
2268         int status = 0;
2269         int i;
2270
2271         dev = ice_pf_to_dev(pf);
2272         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2273                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2274                 goto error_param;
2275         }
2276
2277         if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2278                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2279                 goto error_param;
2280         }
2281
2282         if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
2283                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2284                 goto error_param;
2285         }
2286
2287         for (i = 0; i < vfl->num_elements; i++) {
2288                 if (vfl->vlan_id[i] >= VLAN_N_VID) {
2289                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2290                         dev_err(dev, "invalid VF VLAN id %d\n",
2291                                 vfl->vlan_id[i]);
2292                         goto error_param;
2293                 }
2294         }
2295
2296         vsi = ice_get_vf_vsi(vf);
2297         if (!vsi) {
2298                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2299                 goto error_param;
2300         }
2301
2302         if (add_v && ice_vf_has_max_vlans(vf, vsi)) {
2303                 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2304                          vf->vf_id);
2305                 /* There is no need to let VF know about being not trusted,
2306                  * so we can just return success message here
2307                  */
2308                 goto error_param;
2309         }
2310
2311         /* in DVM a VF can add/delete inner VLAN filters when
2312          * VIRTCHNL_VF_OFFLOAD_VLAN is negotiated, so only reject in SVM
2313          */
2314         if (ice_vf_is_port_vlan_ena(vf) && !ice_is_dvm_ena(&pf->hw)) {
2315                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2316                 goto error_param;
2317         }
2318
2319         /* in DVM VLAN promiscuous is based on the outer VLAN, which would be
2320          * the port VLAN if VIRTCHNL_VF_OFFLOAD_VLAN was negotiated, so only
2321          * allow vlan_promisc = true in SVM and if no port VLAN is configured
2322          */
2323         vlan_promisc = ice_is_vlan_promisc_allowed(vf) &&
2324                 !ice_is_dvm_ena(&pf->hw) &&
2325                 !ice_vf_is_port_vlan_ena(vf);
2326
2327         if (add_v) {
2328                 for (i = 0; i < vfl->num_elements; i++) {
2329                         u16 vid = vfl->vlan_id[i];
2330                         struct ice_vlan vlan;
2331
2332                         if (ice_vf_has_max_vlans(vf, vsi)) {
2333                                 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2334                                          vf->vf_id);
2335                                 /* There is no need to let VF know about being
2336                                  * not trusted, so we can just return success
2337                                  * message here as well.
2338                                  */
2339                                 goto error_param;
2340                         }
2341
2342                         /* we add VLAN 0 by default for each VF so we can enable
2343                          * Tx VLAN anti-spoof without triggering MDD events so
2344                          * we don't need to add it again here
2345                          */
2346                         if (!vid)
2347                                 continue;
2348
2349                         vlan = ICE_VLAN(ETH_P_8021Q, vid, 0);
2350                         status = vsi->inner_vlan_ops.add_vlan(vsi, &vlan);
2351                         if (status) {
2352                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2353                                 goto error_param;
2354                         }
2355
2356                         /* Enable VLAN filtering on first non-zero VLAN */
2357                         if (!vlan_promisc && vid && !ice_is_dvm_ena(&pf->hw)) {
2358                                 if (vf->spoofchk) {
2359                                         status = vsi->inner_vlan_ops.ena_tx_filtering(vsi);
2360                                         if (status) {
2361                                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2362                                                 dev_err(dev, "Enable VLAN anti-spoofing on VLAN ID: %d failed error-%d\n",
2363                                                         vid, status);
2364                                                 goto error_param;
2365                                         }
2366                                 }
2367                                 if (vsi->inner_vlan_ops.ena_rx_filtering(vsi)) {
2368                                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2369                                         dev_err(dev, "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
2370                                                 vid, status);
2371                                         goto error_param;
2372                                 }
2373                         } else if (vlan_promisc) {
2374                                 status = ice_vf_ena_vlan_promisc(vsi, &vlan);
2375                                 if (status) {
2376                                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2377                                         dev_err(dev, "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
2378                                                 vid, status);
2379                                 }
2380                         }
2381                 }
2382         } else {
2383                 /* In case of non_trusted VF, number of VLAN elements passed
2384                  * to PF for removal might be greater than number of VLANs
2385                  * filter programmed for that VF - So, use actual number of
2386                  * VLANS added earlier with add VLAN opcode. In order to avoid
2387                  * removing VLAN that doesn't exist, which result to sending
2388                  * erroneous failed message back to the VF
2389                  */
2390                 int num_vf_vlan;
2391
2392                 num_vf_vlan = vsi->num_vlan;
2393                 for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
2394                         u16 vid = vfl->vlan_id[i];
2395                         struct ice_vlan vlan;
2396
2397                         /* we add VLAN 0 by default for each VF so we can enable
2398                          * Tx VLAN anti-spoof without triggering MDD events so
2399                          * we don't want a VIRTCHNL request to remove it
2400                          */
2401                         if (!vid)
2402                                 continue;
2403
2404                         vlan = ICE_VLAN(ETH_P_8021Q, vid, 0);
2405                         status = vsi->inner_vlan_ops.del_vlan(vsi, &vlan);
2406                         if (status) {
2407                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2408                                 goto error_param;
2409                         }
2410
2411                         /* Disable VLAN filtering when only VLAN 0 is left */
2412                         if (!ice_vsi_has_non_zero_vlans(vsi)) {
2413                                 vsi->inner_vlan_ops.dis_tx_filtering(vsi);
2414                                 vsi->inner_vlan_ops.dis_rx_filtering(vsi);
2415                         }
2416
2417                         if (vlan_promisc)
2418                                 ice_vf_dis_vlan_promisc(vsi, &vlan);
2419                 }
2420         }
2421
2422 error_param:
2423         /* send the response to the VF */
2424         if (add_v)
2425                 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
2426                                              NULL, 0);
2427         else
2428                 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
2429                                              NULL, 0);
2430 }
2431
2432 /**
2433  * ice_vc_add_vlan_msg
2434  * @vf: pointer to the VF info
2435  * @msg: pointer to the msg buffer
2436  *
2437  * Add and program guest VLAN ID
2438  */
2439 static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
2440 {
2441         return ice_vc_process_vlan_msg(vf, msg, true);
2442 }
2443
2444 /**
2445  * ice_vc_remove_vlan_msg
2446  * @vf: pointer to the VF info
2447  * @msg: pointer to the msg buffer
2448  *
2449  * remove programmed guest VLAN ID
2450  */
2451 static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
2452 {
2453         return ice_vc_process_vlan_msg(vf, msg, false);
2454 }
2455
2456 /**
2457  * ice_vsi_is_rxq_crc_strip_dis - check if Rx queue CRC strip is disabled or not
2458  * @vsi: pointer to the VF VSI info
2459  */
2460 static bool ice_vsi_is_rxq_crc_strip_dis(struct ice_vsi *vsi)
2461 {
2462         unsigned int i;
2463
2464         ice_for_each_alloc_rxq(vsi, i)
2465                 if (vsi->rx_rings[i]->flags & ICE_RX_FLAGS_CRC_STRIP_DIS)
2466                         return true;
2467
2468         return false;
2469 }
2470
2471 /**
2472  * ice_vc_ena_vlan_stripping
2473  * @vf: pointer to the VF info
2474  *
2475  * Enable VLAN header stripping for a given VF
2476  */
2477 static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
2478 {
2479         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2480         struct ice_vsi *vsi;
2481
2482         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2483                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2484                 goto error_param;
2485         }
2486
2487         if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2488                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2489                 goto error_param;
2490         }
2491
2492         vsi = ice_get_vf_vsi(vf);
2493         if (!vsi) {
2494                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2495                 goto error_param;
2496         }
2497
2498         if (vsi->inner_vlan_ops.ena_stripping(vsi, ETH_P_8021Q))
2499                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2500         else
2501                 vf->vlan_strip_ena |= ICE_INNER_VLAN_STRIP_ENA;
2502
2503 error_param:
2504         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
2505                                      v_ret, NULL, 0);
2506 }
2507
2508 /**
2509  * ice_vc_dis_vlan_stripping
2510  * @vf: pointer to the VF info
2511  *
2512  * Disable VLAN header stripping for a given VF
2513  */
2514 static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
2515 {
2516         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2517         struct ice_vsi *vsi;
2518
2519         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2520                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2521                 goto error_param;
2522         }
2523
2524         if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2525                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2526                 goto error_param;
2527         }
2528
2529         vsi = ice_get_vf_vsi(vf);
2530         if (!vsi) {
2531                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2532                 goto error_param;
2533         }
2534
2535         if (vsi->inner_vlan_ops.dis_stripping(vsi))
2536                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2537         else
2538                 vf->vlan_strip_ena &= ~ICE_INNER_VLAN_STRIP_ENA;
2539
2540 error_param:
2541         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
2542                                      v_ret, NULL, 0);
2543 }
2544
2545 /**
2546  * ice_vc_get_rss_hena - return the RSS HENA bits allowed by the hardware
2547  * @vf: pointer to the VF info
2548  */
2549 static int ice_vc_get_rss_hena(struct ice_vf *vf)
2550 {
2551         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2552         struct virtchnl_rss_hena *vrh = NULL;
2553         int len = 0, ret;
2554
2555         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2556                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2557                 goto err;
2558         }
2559
2560         if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2561                 dev_err(ice_pf_to_dev(vf->pf), "RSS not supported by PF\n");
2562                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2563                 goto err;
2564         }
2565
2566         len = sizeof(struct virtchnl_rss_hena);
2567         vrh = kzalloc(len, GFP_KERNEL);
2568         if (!vrh) {
2569                 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2570                 len = 0;
2571                 goto err;
2572         }
2573
2574         vrh->hena = ICE_DEFAULT_RSS_HENA;
2575 err:
2576         /* send the response back to the VF */
2577         ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS, v_ret,
2578                                     (u8 *)vrh, len);
2579         kfree(vrh);
2580         return ret;
2581 }
2582
2583 /**
2584  * ice_vc_set_rss_hena - set RSS HENA bits for the VF
2585  * @vf: pointer to the VF info
2586  * @msg: pointer to the msg buffer
2587  */
2588 static int ice_vc_set_rss_hena(struct ice_vf *vf, u8 *msg)
2589 {
2590         struct virtchnl_rss_hena *vrh = (struct virtchnl_rss_hena *)msg;
2591         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2592         struct ice_pf *pf = vf->pf;
2593         struct ice_vsi *vsi;
2594         struct device *dev;
2595         int status;
2596
2597         dev = ice_pf_to_dev(pf);
2598
2599         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2600                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2601                 goto err;
2602         }
2603
2604         if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2605                 dev_err(dev, "RSS not supported by PF\n");
2606                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2607                 goto err;
2608         }
2609
2610         vsi = ice_get_vf_vsi(vf);
2611         if (!vsi) {
2612                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2613                 goto err;
2614         }
2615
2616         /* clear all previously programmed RSS configuration to allow VF drivers
2617          * the ability to customize the RSS configuration and/or completely
2618          * disable RSS
2619          */
2620         status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
2621         if (status && !vrh->hena) {
2622                 /* only report failure to clear the current RSS configuration if
2623                  * that was clearly the VF's intention (i.e. vrh->hena = 0)
2624                  */
2625                 v_ret = ice_err_to_virt_err(status);
2626                 goto err;
2627         } else if (status) {
2628                 /* allow the VF to update the RSS configuration even on failure
2629                  * to clear the current RSS confguration in an attempt to keep
2630                  * RSS in a working state
2631                  */
2632                 dev_warn(dev, "Failed to clear the RSS configuration for VF %u\n",
2633                          vf->vf_id);
2634         }
2635
2636         if (vrh->hena) {
2637                 status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, vrh->hena);
2638                 v_ret = ice_err_to_virt_err(status);
2639         }
2640
2641         /* send the response to the VF */
2642 err:
2643         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, v_ret,
2644                                      NULL, 0);
2645 }
2646
2647 /**
2648  * ice_vc_query_rxdid - query RXDID supported by DDP package
2649  * @vf: pointer to VF info
2650  *
2651  * Called from VF to query a bitmap of supported flexible
2652  * descriptor RXDIDs of a DDP package.
2653  */
2654 static int ice_vc_query_rxdid(struct ice_vf *vf)
2655 {
2656         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2657         struct virtchnl_supported_rxdids *rxdid = NULL;
2658         struct ice_hw *hw = &vf->pf->hw;
2659         struct ice_pf *pf = vf->pf;
2660         int len = 0;
2661         int ret, i;
2662         u32 regval;
2663
2664         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2665                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2666                 goto err;
2667         }
2668
2669         if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC)) {
2670                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2671                 goto err;
2672         }
2673
2674         len = sizeof(struct virtchnl_supported_rxdids);
2675         rxdid = kzalloc(len, GFP_KERNEL);
2676         if (!rxdid) {
2677                 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2678                 len = 0;
2679                 goto err;
2680         }
2681
2682         /* RXDIDs supported by DDP package can be read from the register
2683          * to get the supported RXDID bitmap. But the legacy 32byte RXDID
2684          * is not listed in DDP package, add it in the bitmap manually.
2685          * Legacy 16byte descriptor is not supported.
2686          */
2687         rxdid->supported_rxdids |= BIT(ICE_RXDID_LEGACY_1);
2688
2689         for (i = ICE_RXDID_FLEX_NIC; i < ICE_FLEX_DESC_RXDID_MAX_NUM; i++) {
2690                 regval = rd32(hw, GLFLXP_RXDID_FLAGS(i, 0));
2691                 if ((regval >> GLFLXP_RXDID_FLAGS_FLEXIFLAG_4N_S)
2692                         & GLFLXP_RXDID_FLAGS_FLEXIFLAG_4N_M)
2693                         rxdid->supported_rxdids |= BIT(i);
2694         }
2695
2696         pf->supported_rxdids = rxdid->supported_rxdids;
2697
2698 err:
2699         ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_SUPPORTED_RXDIDS,
2700                                     v_ret, (u8 *)rxdid, len);
2701         kfree(rxdid);
2702         return ret;
2703 }
2704
2705 /**
2706  * ice_vf_init_vlan_stripping - enable/disable VLAN stripping on initialization
2707  * @vf: VF to enable/disable VLAN stripping for on initialization
2708  *
2709  * Set the default for VLAN stripping based on whether a port VLAN is configured
2710  * and the current VLAN mode of the device.
2711  */
2712 static int ice_vf_init_vlan_stripping(struct ice_vf *vf)
2713 {
2714         struct ice_vsi *vsi = ice_get_vf_vsi(vf);
2715
2716         vf->vlan_strip_ena = 0;
2717
2718         if (!vsi)
2719                 return -EINVAL;
2720
2721         /* don't modify stripping if port VLAN is configured in SVM since the
2722          * port VLAN is based on the inner/single VLAN in SVM
2723          */
2724         if (ice_vf_is_port_vlan_ena(vf) && !ice_is_dvm_ena(&vsi->back->hw))
2725                 return 0;
2726
2727         if (ice_vf_vlan_offload_ena(vf->driver_caps)) {
2728                 int err;
2729
2730                 err = vsi->inner_vlan_ops.ena_stripping(vsi, ETH_P_8021Q);
2731                 if (!err)
2732                         vf->vlan_strip_ena |= ICE_INNER_VLAN_STRIP_ENA;
2733                 return err;
2734         }
2735
2736         return vsi->inner_vlan_ops.dis_stripping(vsi);
2737 }
2738
2739 static u16 ice_vc_get_max_vlan_fltrs(struct ice_vf *vf)
2740 {
2741         if (vf->trusted)
2742                 return VLAN_N_VID;
2743         else
2744                 return ICE_MAX_VLAN_PER_VF;
2745 }
2746
2747 /**
2748  * ice_vf_outer_vlan_not_allowed - check if outer VLAN can be used
2749  * @vf: VF that being checked for
2750  *
2751  * When the device is in double VLAN mode, check whether or not the outer VLAN
2752  * is allowed.
2753  */
2754 static bool ice_vf_outer_vlan_not_allowed(struct ice_vf *vf)
2755 {
2756         if (ice_vf_is_port_vlan_ena(vf))
2757                 return true;
2758
2759         return false;
2760 }
2761
2762 /**
2763  * ice_vc_set_dvm_caps - set VLAN capabilities when the device is in DVM
2764  * @vf: VF that capabilities are being set for
2765  * @caps: VLAN capabilities to populate
2766  *
2767  * Determine VLAN capabilities support based on whether a port VLAN is
2768  * configured. If a port VLAN is configured then the VF should use the inner
2769  * filtering/offload capabilities since the port VLAN is using the outer VLAN
2770  * capabilies.
2771  */
2772 static void
2773 ice_vc_set_dvm_caps(struct ice_vf *vf, struct virtchnl_vlan_caps *caps)
2774 {
2775         struct virtchnl_vlan_supported_caps *supported_caps;
2776
2777         if (ice_vf_outer_vlan_not_allowed(vf)) {
2778                 /* until support for inner VLAN filtering is added when a port
2779                  * VLAN is configured, only support software offloaded inner
2780                  * VLANs when a port VLAN is confgured in DVM
2781                  */
2782                 supported_caps = &caps->filtering.filtering_support;
2783                 supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2784
2785                 supported_caps = &caps->offloads.stripping_support;
2786                 supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2787                                         VIRTCHNL_VLAN_TOGGLE |
2788                                         VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2789                 supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2790
2791                 supported_caps = &caps->offloads.insertion_support;
2792                 supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2793                                         VIRTCHNL_VLAN_TOGGLE |
2794                                         VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2795                 supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2796
2797                 caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
2798                 caps->offloads.ethertype_match =
2799                         VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
2800         } else {
2801                 supported_caps = &caps->filtering.filtering_support;
2802                 supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2803                 supported_caps->outer = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2804                                         VIRTCHNL_VLAN_ETHERTYPE_88A8 |
2805                                         VIRTCHNL_VLAN_ETHERTYPE_9100 |
2806                                         VIRTCHNL_VLAN_ETHERTYPE_AND;
2807                 caps->filtering.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2808                                                  VIRTCHNL_VLAN_ETHERTYPE_88A8 |
2809                                                  VIRTCHNL_VLAN_ETHERTYPE_9100;
2810
2811                 supported_caps = &caps->offloads.stripping_support;
2812                 supported_caps->inner = VIRTCHNL_VLAN_TOGGLE |
2813                                         VIRTCHNL_VLAN_ETHERTYPE_8100 |
2814                                         VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2815                 supported_caps->outer = VIRTCHNL_VLAN_TOGGLE |
2816                                         VIRTCHNL_VLAN_ETHERTYPE_8100 |
2817                                         VIRTCHNL_VLAN_ETHERTYPE_88A8 |
2818                                         VIRTCHNL_VLAN_ETHERTYPE_9100 |
2819                                         VIRTCHNL_VLAN_ETHERTYPE_XOR |
2820                                         VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2;
2821
2822                 supported_caps = &caps->offloads.insertion_support;
2823                 supported_caps->inner = VIRTCHNL_VLAN_TOGGLE |
2824                                         VIRTCHNL_VLAN_ETHERTYPE_8100 |
2825                                         VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2826                 supported_caps->outer = VIRTCHNL_VLAN_TOGGLE |
2827                                         VIRTCHNL_VLAN_ETHERTYPE_8100 |
2828                                         VIRTCHNL_VLAN_ETHERTYPE_88A8 |
2829                                         VIRTCHNL_VLAN_ETHERTYPE_9100 |
2830                                         VIRTCHNL_VLAN_ETHERTYPE_XOR |
2831                                         VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2;
2832
2833                 caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
2834
2835                 caps->offloads.ethertype_match =
2836                         VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
2837         }
2838
2839         caps->filtering.max_filters = ice_vc_get_max_vlan_fltrs(vf);
2840 }
2841
2842 /**
2843  * ice_vc_set_svm_caps - set VLAN capabilities when the device is in SVM
2844  * @vf: VF that capabilities are being set for
2845  * @caps: VLAN capabilities to populate
2846  *
2847  * Determine VLAN capabilities support based on whether a port VLAN is
2848  * configured. If a port VLAN is configured then the VF does not have any VLAN
2849  * filtering or offload capabilities since the port VLAN is using the inner VLAN
2850  * capabilities in single VLAN mode (SVM). Otherwise allow the VF to use inner
2851  * VLAN fitlering and offload capabilities.
2852  */
2853 static void
2854 ice_vc_set_svm_caps(struct ice_vf *vf, struct virtchnl_vlan_caps *caps)
2855 {
2856         struct virtchnl_vlan_supported_caps *supported_caps;
2857
2858         if (ice_vf_is_port_vlan_ena(vf)) {
2859                 supported_caps = &caps->filtering.filtering_support;
2860                 supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2861                 supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2862
2863                 supported_caps = &caps->offloads.stripping_support;
2864                 supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2865                 supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2866
2867                 supported_caps = &caps->offloads.insertion_support;
2868                 supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2869                 supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2870
2871                 caps->offloads.ethertype_init = VIRTCHNL_VLAN_UNSUPPORTED;
2872                 caps->offloads.ethertype_match = VIRTCHNL_VLAN_UNSUPPORTED;
2873                 caps->filtering.max_filters = 0;
2874         } else {
2875                 supported_caps = &caps->filtering.filtering_support;
2876                 supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100;
2877                 supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2878                 caps->filtering.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
2879
2880                 supported_caps = &caps->offloads.stripping_support;
2881                 supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2882                                         VIRTCHNL_VLAN_TOGGLE |
2883                                         VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2884                 supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2885
2886                 supported_caps = &caps->offloads.insertion_support;
2887                 supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2888                                         VIRTCHNL_VLAN_TOGGLE |
2889                                         VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2890                 supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2891
2892                 caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
2893                 caps->offloads.ethertype_match =
2894                         VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
2895                 caps->filtering.max_filters = ice_vc_get_max_vlan_fltrs(vf);
2896         }
2897 }
2898
2899 /**
2900  * ice_vc_get_offload_vlan_v2_caps - determine VF's VLAN capabilities
2901  * @vf: VF to determine VLAN capabilities for
2902  *
2903  * This will only be called if the VF and PF successfully negotiated
2904  * VIRTCHNL_VF_OFFLOAD_VLAN_V2.
2905  *
2906  * Set VLAN capabilities based on the current VLAN mode and whether a port VLAN
2907  * is configured or not.
2908  */
2909 static int ice_vc_get_offload_vlan_v2_caps(struct ice_vf *vf)
2910 {
2911         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2912         struct virtchnl_vlan_caps *caps = NULL;
2913         int err, len = 0;
2914
2915         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2916                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2917                 goto out;
2918         }
2919
2920         caps = kzalloc(sizeof(*caps), GFP_KERNEL);
2921         if (!caps) {
2922                 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2923                 goto out;
2924         }
2925         len = sizeof(*caps);
2926
2927         if (ice_is_dvm_ena(&vf->pf->hw))
2928                 ice_vc_set_dvm_caps(vf, caps);
2929         else
2930                 ice_vc_set_svm_caps(vf, caps);
2931
2932         /* store negotiated caps to prevent invalid VF messages */
2933         memcpy(&vf->vlan_v2_caps, caps, sizeof(*caps));
2934
2935 out:
2936         err = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS,
2937                                     v_ret, (u8 *)caps, len);
2938         kfree(caps);
2939         return err;
2940 }
2941
2942 /**
2943  * ice_vc_validate_vlan_tpid - validate VLAN TPID
2944  * @filtering_caps: negotiated/supported VLAN filtering capabilities
2945  * @tpid: VLAN TPID used for validation
2946  *
2947  * Convert the VLAN TPID to a VIRTCHNL_VLAN_ETHERTYPE_* and then compare against
2948  * the negotiated/supported filtering caps to see if the VLAN TPID is valid.
2949  */
2950 static bool ice_vc_validate_vlan_tpid(u16 filtering_caps, u16 tpid)
2951 {
2952         enum virtchnl_vlan_support vlan_ethertype = VIRTCHNL_VLAN_UNSUPPORTED;
2953
2954         switch (tpid) {
2955         case ETH_P_8021Q:
2956                 vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_8100;
2957                 break;
2958         case ETH_P_8021AD:
2959                 vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_88A8;
2960                 break;
2961         case ETH_P_QINQ1:
2962                 vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_9100;
2963                 break;
2964         }
2965
2966         if (!(filtering_caps & vlan_ethertype))
2967                 return false;
2968
2969         return true;
2970 }
2971
2972 /**
2973  * ice_vc_is_valid_vlan - validate the virtchnl_vlan
2974  * @vc_vlan: virtchnl_vlan to validate
2975  *
2976  * If the VLAN TCI and VLAN TPID are 0, then this filter is invalid, so return
2977  * false. Otherwise return true.
2978  */
2979 static bool ice_vc_is_valid_vlan(struct virtchnl_vlan *vc_vlan)
2980 {
2981         if (!vc_vlan->tci || !vc_vlan->tpid)
2982                 return false;
2983
2984         return true;
2985 }
2986
2987 /**
2988  * ice_vc_validate_vlan_filter_list - validate the filter list from the VF
2989  * @vfc: negotiated/supported VLAN filtering capabilities
2990  * @vfl: VLAN filter list from VF to validate
2991  *
2992  * Validate all of the filters in the VLAN filter list from the VF. If any of
2993  * the checks fail then return false. Otherwise return true.
2994  */
2995 static bool
2996 ice_vc_validate_vlan_filter_list(struct virtchnl_vlan_filtering_caps *vfc,
2997                                  struct virtchnl_vlan_filter_list_v2 *vfl)
2998 {
2999         u16 i;
3000
3001         if (!vfl->num_elements)
3002                 return false;
3003
3004         for (i = 0; i < vfl->num_elements; i++) {
3005                 struct virtchnl_vlan_supported_caps *filtering_support =
3006                         &vfc->filtering_support;
3007                 struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
3008                 struct virtchnl_vlan *outer = &vlan_fltr->outer;
3009                 struct virtchnl_vlan *inner = &vlan_fltr->inner;
3010
3011                 if ((ice_vc_is_valid_vlan(outer) &&
3012                      filtering_support->outer == VIRTCHNL_VLAN_UNSUPPORTED) ||
3013                     (ice_vc_is_valid_vlan(inner) &&
3014                      filtering_support->inner == VIRTCHNL_VLAN_UNSUPPORTED))
3015                         return false;
3016
3017                 if ((outer->tci_mask &&
3018                      !(filtering_support->outer & VIRTCHNL_VLAN_FILTER_MASK)) ||
3019                     (inner->tci_mask &&
3020                      !(filtering_support->inner & VIRTCHNL_VLAN_FILTER_MASK)))
3021                         return false;
3022
3023                 if (((outer->tci & VLAN_PRIO_MASK) &&
3024                      !(filtering_support->outer & VIRTCHNL_VLAN_PRIO)) ||
3025                     ((inner->tci & VLAN_PRIO_MASK) &&
3026                      !(filtering_support->inner & VIRTCHNL_VLAN_PRIO)))
3027                         return false;
3028
3029                 if ((ice_vc_is_valid_vlan(outer) &&
3030                      !ice_vc_validate_vlan_tpid(filtering_support->outer,
3031                                                 outer->tpid)) ||
3032                     (ice_vc_is_valid_vlan(inner) &&
3033                      !ice_vc_validate_vlan_tpid(filtering_support->inner,
3034                                                 inner->tpid)))
3035                         return false;
3036         }
3037
3038         return true;
3039 }
3040
3041 /**
3042  * ice_vc_to_vlan - transform from struct virtchnl_vlan to struct ice_vlan
3043  * @vc_vlan: struct virtchnl_vlan to transform
3044  */
3045 static struct ice_vlan ice_vc_to_vlan(struct virtchnl_vlan *vc_vlan)
3046 {
3047         struct ice_vlan vlan = { 0 };
3048
3049         vlan.prio = (vc_vlan->tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
3050         vlan.vid = vc_vlan->tci & VLAN_VID_MASK;
3051         vlan.tpid = vc_vlan->tpid;
3052
3053         return vlan;
3054 }
3055
3056 /**
3057  * ice_vc_vlan_action - action to perform on the virthcnl_vlan
3058  * @vsi: VF's VSI used to perform the action
3059  * @vlan_action: function to perform the action with (i.e. add/del)
3060  * @vlan: VLAN filter to perform the action with
3061  */
3062 static int
3063 ice_vc_vlan_action(struct ice_vsi *vsi,
3064                    int (*vlan_action)(struct ice_vsi *, struct ice_vlan *),
3065                    struct ice_vlan *vlan)
3066 {
3067         int err;
3068
3069         err = vlan_action(vsi, vlan);
3070         if (err)
3071                 return err;
3072
3073         return 0;
3074 }
3075
3076 /**
3077  * ice_vc_del_vlans - delete VLAN(s) from the virtchnl filter list
3078  * @vf: VF used to delete the VLAN(s)
3079  * @vsi: VF's VSI used to delete the VLAN(s)
3080  * @vfl: virthchnl filter list used to delete the filters
3081  */
3082 static int
3083 ice_vc_del_vlans(struct ice_vf *vf, struct ice_vsi *vsi,
3084                  struct virtchnl_vlan_filter_list_v2 *vfl)
3085 {
3086         bool vlan_promisc = ice_is_vlan_promisc_allowed(vf);
3087         int err;
3088         u16 i;
3089
3090         for (i = 0; i < vfl->num_elements; i++) {
3091                 struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
3092                 struct virtchnl_vlan *vc_vlan;
3093
3094                 vc_vlan = &vlan_fltr->outer;
3095                 if (ice_vc_is_valid_vlan(vc_vlan)) {
3096                         struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3097
3098                         err = ice_vc_vlan_action(vsi,
3099                                                  vsi->outer_vlan_ops.del_vlan,
3100                                                  &vlan);
3101                         if (err)
3102                                 return err;
3103
3104                         if (vlan_promisc)
3105                                 ice_vf_dis_vlan_promisc(vsi, &vlan);
3106
3107                         /* Disable VLAN filtering when only VLAN 0 is left */
3108                         if (!ice_vsi_has_non_zero_vlans(vsi) && ice_is_dvm_ena(&vsi->back->hw)) {
3109                                 err = vsi->outer_vlan_ops.dis_tx_filtering(vsi);
3110                                 if (err)
3111                                         return err;
3112                         }
3113                 }
3114
3115                 vc_vlan = &vlan_fltr->inner;
3116                 if (ice_vc_is_valid_vlan(vc_vlan)) {
3117                         struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3118
3119                         err = ice_vc_vlan_action(vsi,
3120                                                  vsi->inner_vlan_ops.del_vlan,
3121                                                  &vlan);
3122                         if (err)
3123                                 return err;
3124
3125                         /* no support for VLAN promiscuous on inner VLAN unless
3126                          * we are in Single VLAN Mode (SVM)
3127                          */
3128                         if (!ice_is_dvm_ena(&vsi->back->hw)) {
3129                                 if (vlan_promisc)
3130                                         ice_vf_dis_vlan_promisc(vsi, &vlan);
3131
3132                                 /* Disable VLAN filtering when only VLAN 0 is left */
3133                                 if (!ice_vsi_has_non_zero_vlans(vsi)) {
3134                                         err = vsi->inner_vlan_ops.dis_tx_filtering(vsi);
3135                                         if (err)
3136                                                 return err;
3137                                 }
3138                         }
3139                 }
3140         }
3141
3142         return 0;
3143 }
3144
3145 /**
3146  * ice_vc_remove_vlan_v2_msg - virtchnl handler for VIRTCHNL_OP_DEL_VLAN_V2
3147  * @vf: VF the message was received from
3148  * @msg: message received from the VF
3149  */
3150 static int ice_vc_remove_vlan_v2_msg(struct ice_vf *vf, u8 *msg)
3151 {
3152         struct virtchnl_vlan_filter_list_v2 *vfl =
3153                 (struct virtchnl_vlan_filter_list_v2 *)msg;
3154         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3155         struct ice_vsi *vsi;
3156
3157         if (!ice_vc_validate_vlan_filter_list(&vf->vlan_v2_caps.filtering,
3158                                               vfl)) {
3159                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3160                 goto out;
3161         }
3162
3163         if (!ice_vc_isvalid_vsi_id(vf, vfl->vport_id)) {
3164                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3165                 goto out;
3166         }
3167
3168         vsi = ice_get_vf_vsi(vf);
3169         if (!vsi) {
3170                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3171                 goto out;
3172         }
3173
3174         if (ice_vc_del_vlans(vf, vsi, vfl))
3175                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3176
3177 out:
3178         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN_V2, v_ret, NULL,
3179                                      0);
3180 }
3181
3182 /**
3183  * ice_vc_add_vlans - add VLAN(s) from the virtchnl filter list
3184  * @vf: VF used to add the VLAN(s)
3185  * @vsi: VF's VSI used to add the VLAN(s)
3186  * @vfl: virthchnl filter list used to add the filters
3187  */
3188 static int
3189 ice_vc_add_vlans(struct ice_vf *vf, struct ice_vsi *vsi,
3190                  struct virtchnl_vlan_filter_list_v2 *vfl)
3191 {
3192         bool vlan_promisc = ice_is_vlan_promisc_allowed(vf);
3193         int err;
3194         u16 i;
3195
3196         for (i = 0; i < vfl->num_elements; i++) {
3197                 struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
3198                 struct virtchnl_vlan *vc_vlan;
3199
3200                 vc_vlan = &vlan_fltr->outer;
3201                 if (ice_vc_is_valid_vlan(vc_vlan)) {
3202                         struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3203
3204                         err = ice_vc_vlan_action(vsi,
3205                                                  vsi->outer_vlan_ops.add_vlan,
3206                                                  &vlan);
3207                         if (err)
3208                                 return err;
3209
3210                         if (vlan_promisc) {
3211                                 err = ice_vf_ena_vlan_promisc(vsi, &vlan);
3212                                 if (err)
3213                                         return err;
3214                         }
3215
3216                         /* Enable VLAN filtering on first non-zero VLAN */
3217                         if (vf->spoofchk && vlan.vid && ice_is_dvm_ena(&vsi->back->hw)) {
3218                                 err = vsi->outer_vlan_ops.ena_tx_filtering(vsi);
3219                                 if (err)
3220                                         return err;
3221                         }
3222                 }
3223
3224                 vc_vlan = &vlan_fltr->inner;
3225                 if (ice_vc_is_valid_vlan(vc_vlan)) {
3226                         struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3227
3228                         err = ice_vc_vlan_action(vsi,
3229                                                  vsi->inner_vlan_ops.add_vlan,
3230                                                  &vlan);
3231                         if (err)
3232                                 return err;
3233
3234                         /* no support for VLAN promiscuous on inner VLAN unless
3235                          * we are in Single VLAN Mode (SVM)
3236                          */
3237                         if (!ice_is_dvm_ena(&vsi->back->hw)) {
3238                                 if (vlan_promisc) {
3239                                         err = ice_vf_ena_vlan_promisc(vsi, &vlan);
3240                                         if (err)
3241                                                 return err;
3242                                 }
3243
3244                                 /* Enable VLAN filtering on first non-zero VLAN */
3245                                 if (vf->spoofchk && vlan.vid) {
3246                                         err = vsi->inner_vlan_ops.ena_tx_filtering(vsi);
3247                                         if (err)
3248                                                 return err;
3249                                 }
3250                         }
3251                 }
3252         }
3253
3254         return 0;
3255 }
3256
3257 /**
3258  * ice_vc_validate_add_vlan_filter_list - validate add filter list from the VF
3259  * @vsi: VF VSI used to get number of existing VLAN filters
3260  * @vfc: negotiated/supported VLAN filtering capabilities
3261  * @vfl: VLAN filter list from VF to validate
3262  *
3263  * Validate all of the filters in the VLAN filter list from the VF during the
3264  * VIRTCHNL_OP_ADD_VLAN_V2 opcode. If any of the checks fail then return false.
3265  * Otherwise return true.
3266  */
3267 static bool
3268 ice_vc_validate_add_vlan_filter_list(struct ice_vsi *vsi,
3269                                      struct virtchnl_vlan_filtering_caps *vfc,
3270                                      struct virtchnl_vlan_filter_list_v2 *vfl)
3271 {
3272         u16 num_requested_filters = ice_vsi_num_non_zero_vlans(vsi) +
3273                 vfl->num_elements;
3274
3275         if (num_requested_filters > vfc->max_filters)
3276                 return false;
3277
3278         return ice_vc_validate_vlan_filter_list(vfc, vfl);
3279 }
3280
3281 /**
3282  * ice_vc_add_vlan_v2_msg - virtchnl handler for VIRTCHNL_OP_ADD_VLAN_V2
3283  * @vf: VF the message was received from
3284  * @msg: message received from the VF
3285  */
3286 static int ice_vc_add_vlan_v2_msg(struct ice_vf *vf, u8 *msg)
3287 {
3288         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3289         struct virtchnl_vlan_filter_list_v2 *vfl =
3290                 (struct virtchnl_vlan_filter_list_v2 *)msg;
3291         struct ice_vsi *vsi;
3292
3293         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3294                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3295                 goto out;
3296         }
3297
3298         if (!ice_vc_isvalid_vsi_id(vf, vfl->vport_id)) {
3299                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3300                 goto out;
3301         }
3302
3303         vsi = ice_get_vf_vsi(vf);
3304         if (!vsi) {
3305                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3306                 goto out;
3307         }
3308
3309         if (!ice_vc_validate_add_vlan_filter_list(vsi,
3310                                                   &vf->vlan_v2_caps.filtering,
3311                                                   vfl)) {
3312                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3313                 goto out;
3314         }
3315
3316         if (ice_vc_add_vlans(vf, vsi, vfl))
3317                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3318
3319 out:
3320         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN_V2, v_ret, NULL,
3321                                      0);
3322 }
3323
3324 /**
3325  * ice_vc_valid_vlan_setting - validate VLAN setting
3326  * @negotiated_settings: negotiated VLAN settings during VF init
3327  * @ethertype_setting: ethertype(s) requested for the VLAN setting
3328  */
3329 static bool
3330 ice_vc_valid_vlan_setting(u32 negotiated_settings, u32 ethertype_setting)
3331 {
3332         if (ethertype_setting && !(negotiated_settings & ethertype_setting))
3333                 return false;
3334
3335         /* only allow a single VIRTCHNL_VLAN_ETHERTYPE if
3336          * VIRTHCNL_VLAN_ETHERTYPE_AND is not negotiated/supported
3337          */
3338         if (!(negotiated_settings & VIRTCHNL_VLAN_ETHERTYPE_AND) &&
3339             hweight32(ethertype_setting) > 1)
3340                 return false;
3341
3342         /* ability to modify the VLAN setting was not negotiated */
3343         if (!(negotiated_settings & VIRTCHNL_VLAN_TOGGLE))
3344                 return false;
3345
3346         return true;
3347 }
3348
3349 /**
3350  * ice_vc_valid_vlan_setting_msg - validate the VLAN setting message
3351  * @caps: negotiated VLAN settings during VF init
3352  * @msg: message to validate
3353  *
3354  * Used to validate any VLAN virtchnl message sent as a
3355  * virtchnl_vlan_setting structure. Validates the message against the
3356  * negotiated/supported caps during VF driver init.
3357  */
3358 static bool
3359 ice_vc_valid_vlan_setting_msg(struct virtchnl_vlan_supported_caps *caps,
3360                               struct virtchnl_vlan_setting *msg)
3361 {
3362         if ((!msg->outer_ethertype_setting &&
3363              !msg->inner_ethertype_setting) ||
3364             (!caps->outer && !caps->inner))
3365                 return false;
3366
3367         if (msg->outer_ethertype_setting &&
3368             !ice_vc_valid_vlan_setting(caps->outer,
3369                                        msg->outer_ethertype_setting))
3370                 return false;
3371
3372         if (msg->inner_ethertype_setting &&
3373             !ice_vc_valid_vlan_setting(caps->inner,
3374                                        msg->inner_ethertype_setting))
3375                 return false;
3376
3377         return true;
3378 }
3379
3380 /**
3381  * ice_vc_get_tpid - transform from VIRTCHNL_VLAN_ETHERTYPE_* to VLAN TPID
3382  * @ethertype_setting: VIRTCHNL_VLAN_ETHERTYPE_* used to get VLAN TPID
3383  * @tpid: VLAN TPID to populate
3384  */
3385 static int ice_vc_get_tpid(u32 ethertype_setting, u16 *tpid)
3386 {
3387         switch (ethertype_setting) {
3388         case VIRTCHNL_VLAN_ETHERTYPE_8100:
3389                 *tpid = ETH_P_8021Q;
3390                 break;
3391         case VIRTCHNL_VLAN_ETHERTYPE_88A8:
3392                 *tpid = ETH_P_8021AD;
3393                 break;
3394         case VIRTCHNL_VLAN_ETHERTYPE_9100:
3395                 *tpid = ETH_P_QINQ1;
3396                 break;
3397         default:
3398                 *tpid = 0;
3399                 return -EINVAL;
3400         }
3401
3402         return 0;
3403 }
3404
3405 /**
3406  * ice_vc_ena_vlan_offload - enable VLAN offload based on the ethertype_setting
3407  * @vsi: VF's VSI used to enable the VLAN offload
3408  * @ena_offload: function used to enable the VLAN offload
3409  * @ethertype_setting: VIRTCHNL_VLAN_ETHERTYPE_* to enable offloads for
3410  */
3411 static int
3412 ice_vc_ena_vlan_offload(struct ice_vsi *vsi,
3413                         int (*ena_offload)(struct ice_vsi *vsi, u16 tpid),
3414                         u32 ethertype_setting)
3415 {
3416         u16 tpid;
3417         int err;
3418
3419         err = ice_vc_get_tpid(ethertype_setting, &tpid);
3420         if (err)
3421                 return err;
3422
3423         err = ena_offload(vsi, tpid);
3424         if (err)
3425                 return err;
3426
3427         return 0;
3428 }
3429
3430 #define ICE_L2TSEL_QRX_CONTEXT_REG_IDX  3
3431 #define ICE_L2TSEL_BIT_OFFSET           23
3432 enum ice_l2tsel {
3433         ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND,
3434         ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG1,
3435 };
3436
3437 /**
3438  * ice_vsi_update_l2tsel - update l2tsel field for all Rx rings on this VSI
3439  * @vsi: VSI used to update l2tsel on
3440  * @l2tsel: l2tsel setting requested
3441  *
3442  * Use the l2tsel setting to update all of the Rx queue context bits for l2tsel.
3443  * This will modify which descriptor field the first offloaded VLAN will be
3444  * stripped into.
3445  */
3446 static void ice_vsi_update_l2tsel(struct ice_vsi *vsi, enum ice_l2tsel l2tsel)
3447 {
3448         struct ice_hw *hw = &vsi->back->hw;
3449         u32 l2tsel_bit;
3450         int i;
3451
3452         if (l2tsel == ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND)
3453                 l2tsel_bit = 0;
3454         else
3455                 l2tsel_bit = BIT(ICE_L2TSEL_BIT_OFFSET);
3456
3457         for (i = 0; i < vsi->alloc_rxq; i++) {
3458                 u16 pfq = vsi->rxq_map[i];
3459                 u32 qrx_context_offset;
3460                 u32 regval;
3461
3462                 qrx_context_offset =
3463                         QRX_CONTEXT(ICE_L2TSEL_QRX_CONTEXT_REG_IDX, pfq);
3464
3465                 regval = rd32(hw, qrx_context_offset);
3466                 regval &= ~BIT(ICE_L2TSEL_BIT_OFFSET);
3467                 regval |= l2tsel_bit;
3468                 wr32(hw, qrx_context_offset, regval);
3469         }
3470 }
3471
3472 /**
3473  * ice_vc_ena_vlan_stripping_v2_msg
3474  * @vf: VF the message was received from
3475  * @msg: message received from the VF
3476  *
3477  * virthcnl handler for VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2
3478  */
3479 static int ice_vc_ena_vlan_stripping_v2_msg(struct ice_vf *vf, u8 *msg)
3480 {
3481         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3482         struct virtchnl_vlan_supported_caps *stripping_support;
3483         struct virtchnl_vlan_setting *strip_msg =
3484                 (struct virtchnl_vlan_setting *)msg;
3485         u32 ethertype_setting;
3486         struct ice_vsi *vsi;
3487
3488         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3489                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3490                 goto out;
3491         }
3492
3493         if (!ice_vc_isvalid_vsi_id(vf, strip_msg->vport_id)) {
3494                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3495                 goto out;
3496         }
3497
3498         vsi = ice_get_vf_vsi(vf);
3499         if (!vsi) {
3500                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3501                 goto out;
3502         }
3503
3504         stripping_support = &vf->vlan_v2_caps.offloads.stripping_support;
3505         if (!ice_vc_valid_vlan_setting_msg(stripping_support, strip_msg)) {
3506                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3507                 goto out;
3508         }
3509
3510         if (ice_vsi_is_rxq_crc_strip_dis(vsi)) {
3511                 v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
3512                 goto out;
3513         }
3514
3515         ethertype_setting = strip_msg->outer_ethertype_setting;
3516         if (ethertype_setting) {
3517                 if (ice_vc_ena_vlan_offload(vsi,
3518                                             vsi->outer_vlan_ops.ena_stripping,
3519                                             ethertype_setting)) {
3520                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3521                         goto out;
3522                 } else {
3523                         enum ice_l2tsel l2tsel =
3524                                 ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND;
3525
3526                         /* PF tells the VF that the outer VLAN tag is always
3527                          * extracted to VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2 and
3528                          * inner is always extracted to
3529                          * VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1. This is needed to
3530                          * support outer stripping so the first tag always ends
3531                          * up in L2TAG2_2ND and the second/inner tag, if
3532                          * enabled, is extracted in L2TAG1.
3533                          */
3534                         ice_vsi_update_l2tsel(vsi, l2tsel);
3535
3536                         vf->vlan_strip_ena |= ICE_OUTER_VLAN_STRIP_ENA;
3537                 }
3538         }
3539
3540         ethertype_setting = strip_msg->inner_ethertype_setting;
3541         if (ethertype_setting &&
3542             ice_vc_ena_vlan_offload(vsi, vsi->inner_vlan_ops.ena_stripping,
3543                                     ethertype_setting)) {
3544                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3545                 goto out;
3546         }
3547
3548         if (ethertype_setting)
3549                 vf->vlan_strip_ena |= ICE_INNER_VLAN_STRIP_ENA;
3550
3551 out:
3552         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2,
3553                                      v_ret, NULL, 0);
3554 }
3555
3556 /**
3557  * ice_vc_dis_vlan_stripping_v2_msg
3558  * @vf: VF the message was received from
3559  * @msg: message received from the VF
3560  *
3561  * virthcnl handler for VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2
3562  */
3563 static int ice_vc_dis_vlan_stripping_v2_msg(struct ice_vf *vf, u8 *msg)
3564 {
3565         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3566         struct virtchnl_vlan_supported_caps *stripping_support;
3567         struct virtchnl_vlan_setting *strip_msg =
3568                 (struct virtchnl_vlan_setting *)msg;
3569         u32 ethertype_setting;
3570         struct ice_vsi *vsi;
3571
3572         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3573                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3574                 goto out;
3575         }
3576
3577         if (!ice_vc_isvalid_vsi_id(vf, strip_msg->vport_id)) {
3578                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3579                 goto out;
3580         }
3581
3582         vsi = ice_get_vf_vsi(vf);
3583         if (!vsi) {
3584                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3585                 goto out;
3586         }
3587
3588         stripping_support = &vf->vlan_v2_caps.offloads.stripping_support;
3589         if (!ice_vc_valid_vlan_setting_msg(stripping_support, strip_msg)) {
3590                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3591                 goto out;
3592         }
3593
3594         ethertype_setting = strip_msg->outer_ethertype_setting;
3595         if (ethertype_setting) {
3596                 if (vsi->outer_vlan_ops.dis_stripping(vsi)) {
3597                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3598                         goto out;
3599                 } else {
3600                         enum ice_l2tsel l2tsel =
3601                                 ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG1;
3602
3603                         /* PF tells the VF that the outer VLAN tag is always
3604                          * extracted to VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2 and
3605                          * inner is always extracted to
3606                          * VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1. This is needed to
3607                          * support inner stripping while outer stripping is
3608                          * disabled so that the first and only tag is extracted
3609                          * in L2TAG1.
3610                          */
3611                         ice_vsi_update_l2tsel(vsi, l2tsel);
3612
3613                         vf->vlan_strip_ena &= ~ICE_OUTER_VLAN_STRIP_ENA;
3614                 }
3615         }
3616
3617         ethertype_setting = strip_msg->inner_ethertype_setting;
3618         if (ethertype_setting && vsi->inner_vlan_ops.dis_stripping(vsi)) {
3619                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3620                 goto out;
3621         }
3622
3623         if (ethertype_setting)
3624                 vf->vlan_strip_ena &= ~ICE_INNER_VLAN_STRIP_ENA;
3625
3626 out:
3627         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2,
3628                                      v_ret, NULL, 0);
3629 }
3630
3631 /**
3632  * ice_vc_ena_vlan_insertion_v2_msg
3633  * @vf: VF the message was received from
3634  * @msg: message received from the VF
3635  *
3636  * virthcnl handler for VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2
3637  */
3638 static int ice_vc_ena_vlan_insertion_v2_msg(struct ice_vf *vf, u8 *msg)
3639 {
3640         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3641         struct virtchnl_vlan_supported_caps *insertion_support;
3642         struct virtchnl_vlan_setting *insertion_msg =
3643                 (struct virtchnl_vlan_setting *)msg;
3644         u32 ethertype_setting;
3645         struct ice_vsi *vsi;
3646
3647         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3648                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3649                 goto out;
3650         }
3651
3652         if (!ice_vc_isvalid_vsi_id(vf, insertion_msg->vport_id)) {
3653                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3654                 goto out;
3655         }
3656
3657         vsi = ice_get_vf_vsi(vf);
3658         if (!vsi) {
3659                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3660                 goto out;
3661         }
3662
3663         insertion_support = &vf->vlan_v2_caps.offloads.insertion_support;
3664         if (!ice_vc_valid_vlan_setting_msg(insertion_support, insertion_msg)) {
3665                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3666                 goto out;
3667         }
3668
3669         ethertype_setting = insertion_msg->outer_ethertype_setting;
3670         if (ethertype_setting &&
3671             ice_vc_ena_vlan_offload(vsi, vsi->outer_vlan_ops.ena_insertion,
3672                                     ethertype_setting)) {
3673                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3674                 goto out;
3675         }
3676
3677         ethertype_setting = insertion_msg->inner_ethertype_setting;
3678         if (ethertype_setting &&
3679             ice_vc_ena_vlan_offload(vsi, vsi->inner_vlan_ops.ena_insertion,
3680                                     ethertype_setting)) {
3681                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3682                 goto out;
3683         }
3684
3685 out:
3686         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2,
3687                                      v_ret, NULL, 0);
3688 }
3689
3690 /**
3691  * ice_vc_dis_vlan_insertion_v2_msg
3692  * @vf: VF the message was received from
3693  * @msg: message received from the VF
3694  *
3695  * virthcnl handler for VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2
3696  */
3697 static int ice_vc_dis_vlan_insertion_v2_msg(struct ice_vf *vf, u8 *msg)
3698 {
3699         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3700         struct virtchnl_vlan_supported_caps *insertion_support;
3701         struct virtchnl_vlan_setting *insertion_msg =
3702                 (struct virtchnl_vlan_setting *)msg;
3703         u32 ethertype_setting;
3704         struct ice_vsi *vsi;
3705
3706         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3707                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3708                 goto out;
3709         }
3710
3711         if (!ice_vc_isvalid_vsi_id(vf, insertion_msg->vport_id)) {
3712                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3713                 goto out;
3714         }
3715
3716         vsi = ice_get_vf_vsi(vf);
3717         if (!vsi) {
3718                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3719                 goto out;
3720         }
3721
3722         insertion_support = &vf->vlan_v2_caps.offloads.insertion_support;
3723         if (!ice_vc_valid_vlan_setting_msg(insertion_support, insertion_msg)) {
3724                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3725                 goto out;
3726         }
3727
3728         ethertype_setting = insertion_msg->outer_ethertype_setting;
3729         if (ethertype_setting && vsi->outer_vlan_ops.dis_insertion(vsi)) {
3730                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3731                 goto out;
3732         }
3733
3734         ethertype_setting = insertion_msg->inner_ethertype_setting;
3735         if (ethertype_setting && vsi->inner_vlan_ops.dis_insertion(vsi)) {
3736                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3737                 goto out;
3738         }
3739
3740 out:
3741         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2,
3742                                      v_ret, NULL, 0);
3743 }
3744
3745 static const struct ice_virtchnl_ops ice_virtchnl_dflt_ops = {
3746         .get_ver_msg = ice_vc_get_ver_msg,
3747         .get_vf_res_msg = ice_vc_get_vf_res_msg,
3748         .reset_vf = ice_vc_reset_vf_msg,
3749         .add_mac_addr_msg = ice_vc_add_mac_addr_msg,
3750         .del_mac_addr_msg = ice_vc_del_mac_addr_msg,
3751         .cfg_qs_msg = ice_vc_cfg_qs_msg,
3752         .ena_qs_msg = ice_vc_ena_qs_msg,
3753         .dis_qs_msg = ice_vc_dis_qs_msg,
3754         .request_qs_msg = ice_vc_request_qs_msg,
3755         .cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
3756         .config_rss_key = ice_vc_config_rss_key,
3757         .config_rss_lut = ice_vc_config_rss_lut,
3758         .get_stats_msg = ice_vc_get_stats_msg,
3759         .cfg_promiscuous_mode_msg = ice_vc_cfg_promiscuous_mode_msg,
3760         .add_vlan_msg = ice_vc_add_vlan_msg,
3761         .remove_vlan_msg = ice_vc_remove_vlan_msg,
3762         .query_rxdid = ice_vc_query_rxdid,
3763         .get_rss_hena = ice_vc_get_rss_hena,
3764         .set_rss_hena_msg = ice_vc_set_rss_hena,
3765         .ena_vlan_stripping = ice_vc_ena_vlan_stripping,
3766         .dis_vlan_stripping = ice_vc_dis_vlan_stripping,
3767         .handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
3768         .add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
3769         .del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
3770         .get_offload_vlan_v2_caps = ice_vc_get_offload_vlan_v2_caps,
3771         .add_vlan_v2_msg = ice_vc_add_vlan_v2_msg,
3772         .remove_vlan_v2_msg = ice_vc_remove_vlan_v2_msg,
3773         .ena_vlan_stripping_v2_msg = ice_vc_ena_vlan_stripping_v2_msg,
3774         .dis_vlan_stripping_v2_msg = ice_vc_dis_vlan_stripping_v2_msg,
3775         .ena_vlan_insertion_v2_msg = ice_vc_ena_vlan_insertion_v2_msg,
3776         .dis_vlan_insertion_v2_msg = ice_vc_dis_vlan_insertion_v2_msg,
3777 };
3778
3779 /**
3780  * ice_virtchnl_set_dflt_ops - Switch to default virtchnl ops
3781  * @vf: the VF to switch ops
3782  */
3783 void ice_virtchnl_set_dflt_ops(struct ice_vf *vf)
3784 {
3785         vf->virtchnl_ops = &ice_virtchnl_dflt_ops;
3786 }
3787
3788 /**
3789  * ice_vc_repr_add_mac
3790  * @vf: pointer to VF
3791  * @msg: virtchannel message
3792  *
3793  * When port representors are created, we do not add MAC rule
3794  * to firmware, we store it so that PF could report same
3795  * MAC as VF.
3796  */
3797 static int ice_vc_repr_add_mac(struct ice_vf *vf, u8 *msg)
3798 {
3799         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3800         struct virtchnl_ether_addr_list *al =
3801             (struct virtchnl_ether_addr_list *)msg;
3802         struct ice_vsi *vsi;
3803         struct ice_pf *pf;
3804         int i;
3805
3806         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
3807             !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
3808                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3809                 goto handle_mac_exit;
3810         }
3811
3812         pf = vf->pf;
3813
3814         vsi = ice_get_vf_vsi(vf);
3815         if (!vsi) {
3816                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3817                 goto handle_mac_exit;
3818         }
3819
3820         for (i = 0; i < al->num_elements; i++) {
3821                 u8 *mac_addr = al->list[i].addr;
3822
3823                 if (!is_unicast_ether_addr(mac_addr) ||
3824                     ether_addr_equal(mac_addr, vf->hw_lan_addr))
3825                         continue;
3826
3827                 if (vf->pf_set_mac) {
3828                         dev_err(ice_pf_to_dev(pf), "VF attempting to override administratively set MAC address\n");
3829                         v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
3830                         goto handle_mac_exit;
3831                 }
3832
3833                 ice_vfhw_mac_add(vf, &al->list[i]);
3834                 vf->num_mac++;
3835                 break;
3836         }
3837
3838 handle_mac_exit:
3839         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
3840                                      v_ret, NULL, 0);
3841 }
3842
3843 /**
3844  * ice_vc_repr_del_mac - response with success for deleting MAC
3845  * @vf: pointer to VF
3846  * @msg: virtchannel message
3847  *
3848  * Respond with success to not break normal VF flow.
3849  * For legacy VF driver try to update cached MAC address.
3850  */
3851 static int
3852 ice_vc_repr_del_mac(struct ice_vf __always_unused *vf, u8 __always_unused *msg)
3853 {
3854         struct virtchnl_ether_addr_list *al =
3855                 (struct virtchnl_ether_addr_list *)msg;
3856
3857         ice_update_legacy_cached_mac(vf, &al->list[0]);
3858
3859         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
3860                                      VIRTCHNL_STATUS_SUCCESS, NULL, 0);
3861 }
3862
3863 static int
3864 ice_vc_repr_cfg_promiscuous_mode(struct ice_vf *vf, u8 __always_unused *msg)
3865 {
3866         dev_dbg(ice_pf_to_dev(vf->pf),
3867                 "Can't config promiscuous mode in switchdev mode for VF %d\n",
3868                 vf->vf_id);
3869         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
3870                                      VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
3871                                      NULL, 0);
3872 }
3873
3874 static const struct ice_virtchnl_ops ice_virtchnl_repr_ops = {
3875         .get_ver_msg = ice_vc_get_ver_msg,
3876         .get_vf_res_msg = ice_vc_get_vf_res_msg,
3877         .reset_vf = ice_vc_reset_vf_msg,
3878         .add_mac_addr_msg = ice_vc_repr_add_mac,
3879         .del_mac_addr_msg = ice_vc_repr_del_mac,
3880         .cfg_qs_msg = ice_vc_cfg_qs_msg,
3881         .ena_qs_msg = ice_vc_ena_qs_msg,
3882         .dis_qs_msg = ice_vc_dis_qs_msg,
3883         .request_qs_msg = ice_vc_request_qs_msg,
3884         .cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
3885         .config_rss_key = ice_vc_config_rss_key,
3886         .config_rss_lut = ice_vc_config_rss_lut,
3887         .get_stats_msg = ice_vc_get_stats_msg,
3888         .cfg_promiscuous_mode_msg = ice_vc_repr_cfg_promiscuous_mode,
3889         .add_vlan_msg = ice_vc_add_vlan_msg,
3890         .remove_vlan_msg = ice_vc_remove_vlan_msg,
3891         .query_rxdid = ice_vc_query_rxdid,
3892         .get_rss_hena = ice_vc_get_rss_hena,
3893         .set_rss_hena_msg = ice_vc_set_rss_hena,
3894         .ena_vlan_stripping = ice_vc_ena_vlan_stripping,
3895         .dis_vlan_stripping = ice_vc_dis_vlan_stripping,
3896         .handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
3897         .add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
3898         .del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
3899         .get_offload_vlan_v2_caps = ice_vc_get_offload_vlan_v2_caps,
3900         .add_vlan_v2_msg = ice_vc_add_vlan_v2_msg,
3901         .remove_vlan_v2_msg = ice_vc_remove_vlan_v2_msg,
3902         .ena_vlan_stripping_v2_msg = ice_vc_ena_vlan_stripping_v2_msg,
3903         .dis_vlan_stripping_v2_msg = ice_vc_dis_vlan_stripping_v2_msg,
3904         .ena_vlan_insertion_v2_msg = ice_vc_ena_vlan_insertion_v2_msg,
3905         .dis_vlan_insertion_v2_msg = ice_vc_dis_vlan_insertion_v2_msg,
3906 };
3907
3908 /**
3909  * ice_virtchnl_set_repr_ops - Switch to representor virtchnl ops
3910  * @vf: the VF to switch ops
3911  */
3912 void ice_virtchnl_set_repr_ops(struct ice_vf *vf)
3913 {
3914         vf->virtchnl_ops = &ice_virtchnl_repr_ops;
3915 }
3916
3917 /**
3918  * ice_is_malicious_vf - check if this vf might be overflowing mailbox
3919  * @vf: the VF to check
3920  * @mbxdata: data about the state of the mailbox
3921  *
3922  * Detect if a given VF might be malicious and attempting to overflow the PF
3923  * mailbox. If so, log a warning message and ignore this event.
3924  */
3925 static bool
3926 ice_is_malicious_vf(struct ice_vf *vf, struct ice_mbx_data *mbxdata)
3927 {
3928         bool report_malvf = false;
3929         struct device *dev;
3930         struct ice_pf *pf;
3931         int status;
3932
3933         pf = vf->pf;
3934         dev = ice_pf_to_dev(pf);
3935
3936         if (test_bit(ICE_VF_STATE_DIS, vf->vf_states))
3937                 return vf->mbx_info.malicious;
3938
3939         /* check to see if we have a newly malicious VF */
3940         status = ice_mbx_vf_state_handler(&pf->hw, mbxdata, &vf->mbx_info,
3941                                           &report_malvf);
3942         if (status)
3943                 dev_warn_ratelimited(dev, "Unable to check status of mailbox overflow for VF %u MAC %pM, status %d\n",
3944                                      vf->vf_id, vf->dev_lan_addr, status);
3945
3946         if (report_malvf) {
3947                 struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
3948                 u8 zero_addr[ETH_ALEN] = {};
3949
3950                 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",
3951                          vf->dev_lan_addr,
3952                          pf_vsi ? pf_vsi->netdev->dev_addr : zero_addr);
3953         }
3954
3955         return vf->mbx_info.malicious;
3956 }
3957
3958 /**
3959  * ice_vc_process_vf_msg - Process request from VF
3960  * @pf: pointer to the PF structure
3961  * @event: pointer to the AQ event
3962  * @mbxdata: information used to detect VF attempting mailbox overflow
3963  *
3964  * called from the common asq/arq handler to
3965  * process request from VF
3966  */
3967 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event,
3968                            struct ice_mbx_data *mbxdata)
3969 {
3970         u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
3971         s16 vf_id = le16_to_cpu(event->desc.retval);
3972         const struct ice_virtchnl_ops *ops;
3973         u16 msglen = event->msg_len;
3974         u8 *msg = event->msg_buf;
3975         struct ice_vf *vf = NULL;
3976         struct device *dev;
3977         int err = 0;
3978
3979         dev = ice_pf_to_dev(pf);
3980
3981         vf = ice_get_vf_by_id(pf, vf_id);
3982         if (!vf) {
3983                 dev_err(dev, "Unable to locate VF for message from VF ID %d, opcode %d, len %d\n",
3984                         vf_id, v_opcode, msglen);
3985                 return;
3986         }
3987
3988         mutex_lock(&vf->cfg_lock);
3989
3990         /* Check if the VF is trying to overflow the mailbox */
3991         if (ice_is_malicious_vf(vf, mbxdata))
3992                 goto finish;
3993
3994         /* Check if VF is disabled. */
3995         if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
3996                 err = -EPERM;
3997                 goto error_handler;
3998         }
3999
4000         ops = vf->virtchnl_ops;
4001
4002         /* Perform basic checks on the msg */
4003         err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
4004         if (err) {
4005                 if (err == VIRTCHNL_STATUS_ERR_PARAM)
4006                         err = -EPERM;
4007                 else
4008                         err = -EINVAL;
4009         }
4010
4011 error_handler:
4012         if (err) {
4013                 ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
4014                                       NULL, 0);
4015                 dev_err(dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
4016                         vf_id, v_opcode, msglen, err);
4017                 goto finish;
4018         }
4019
4020         if (!ice_vc_is_opcode_allowed(vf, v_opcode)) {
4021                 ice_vc_send_msg_to_vf(vf, v_opcode,
4022                                       VIRTCHNL_STATUS_ERR_NOT_SUPPORTED, NULL,
4023                                       0);
4024                 goto finish;
4025         }
4026
4027         switch (v_opcode) {
4028         case VIRTCHNL_OP_VERSION:
4029                 err = ops->get_ver_msg(vf, msg);
4030                 break;
4031         case VIRTCHNL_OP_GET_VF_RESOURCES:
4032                 err = ops->get_vf_res_msg(vf, msg);
4033                 if (ice_vf_init_vlan_stripping(vf))
4034                         dev_dbg(dev, "Failed to initialize VLAN stripping for VF %d\n",
4035                                 vf->vf_id);
4036                 ice_vc_notify_vf_link_state(vf);
4037                 break;
4038         case VIRTCHNL_OP_RESET_VF:
4039                 ops->reset_vf(vf);
4040                 break;
4041         case VIRTCHNL_OP_ADD_ETH_ADDR:
4042                 err = ops->add_mac_addr_msg(vf, msg);
4043                 break;
4044         case VIRTCHNL_OP_DEL_ETH_ADDR:
4045                 err = ops->del_mac_addr_msg(vf, msg);
4046                 break;
4047         case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
4048                 err = ops->cfg_qs_msg(vf, msg);
4049                 break;
4050         case VIRTCHNL_OP_ENABLE_QUEUES:
4051                 err = ops->ena_qs_msg(vf, msg);
4052                 ice_vc_notify_vf_link_state(vf);
4053                 break;
4054         case VIRTCHNL_OP_DISABLE_QUEUES:
4055                 err = ops->dis_qs_msg(vf, msg);
4056                 break;
4057         case VIRTCHNL_OP_REQUEST_QUEUES:
4058                 err = ops->request_qs_msg(vf, msg);
4059                 break;
4060         case VIRTCHNL_OP_CONFIG_IRQ_MAP:
4061                 err = ops->cfg_irq_map_msg(vf, msg);
4062                 break;
4063         case VIRTCHNL_OP_CONFIG_RSS_KEY:
4064                 err = ops->config_rss_key(vf, msg);
4065                 break;
4066         case VIRTCHNL_OP_CONFIG_RSS_LUT:
4067                 err = ops->config_rss_lut(vf, msg);
4068                 break;
4069         case VIRTCHNL_OP_GET_STATS:
4070                 err = ops->get_stats_msg(vf, msg);
4071                 break;
4072         case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
4073                 err = ops->cfg_promiscuous_mode_msg(vf, msg);
4074                 break;
4075         case VIRTCHNL_OP_ADD_VLAN:
4076                 err = ops->add_vlan_msg(vf, msg);
4077                 break;
4078         case VIRTCHNL_OP_DEL_VLAN:
4079                 err = ops->remove_vlan_msg(vf, msg);
4080                 break;
4081         case VIRTCHNL_OP_GET_SUPPORTED_RXDIDS:
4082                 err = ops->query_rxdid(vf);
4083                 break;
4084         case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
4085                 err = ops->get_rss_hena(vf);
4086                 break;
4087         case VIRTCHNL_OP_SET_RSS_HENA:
4088                 err = ops->set_rss_hena_msg(vf, msg);
4089                 break;
4090         case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
4091                 err = ops->ena_vlan_stripping(vf);
4092                 break;
4093         case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
4094                 err = ops->dis_vlan_stripping(vf);
4095                 break;
4096         case VIRTCHNL_OP_ADD_FDIR_FILTER:
4097                 err = ops->add_fdir_fltr_msg(vf, msg);
4098                 break;
4099         case VIRTCHNL_OP_DEL_FDIR_FILTER:
4100                 err = ops->del_fdir_fltr_msg(vf, msg);
4101                 break;
4102         case VIRTCHNL_OP_ADD_RSS_CFG:
4103                 err = ops->handle_rss_cfg_msg(vf, msg, true);
4104                 break;
4105         case VIRTCHNL_OP_DEL_RSS_CFG:
4106                 err = ops->handle_rss_cfg_msg(vf, msg, false);
4107                 break;
4108         case VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS:
4109                 err = ops->get_offload_vlan_v2_caps(vf);
4110                 break;
4111         case VIRTCHNL_OP_ADD_VLAN_V2:
4112                 err = ops->add_vlan_v2_msg(vf, msg);
4113                 break;
4114         case VIRTCHNL_OP_DEL_VLAN_V2:
4115                 err = ops->remove_vlan_v2_msg(vf, msg);
4116                 break;
4117         case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2:
4118                 err = ops->ena_vlan_stripping_v2_msg(vf, msg);
4119                 break;
4120         case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2:
4121                 err = ops->dis_vlan_stripping_v2_msg(vf, msg);
4122                 break;
4123         case VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2:
4124                 err = ops->ena_vlan_insertion_v2_msg(vf, msg);
4125                 break;
4126         case VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2:
4127                 err = ops->dis_vlan_insertion_v2_msg(vf, msg);
4128                 break;
4129         case VIRTCHNL_OP_UNKNOWN:
4130         default:
4131                 dev_err(dev, "Unsupported opcode %d from VF %d\n", v_opcode,
4132                         vf_id);
4133                 err = ice_vc_send_msg_to_vf(vf, v_opcode,
4134                                             VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4135                                             NULL, 0);
4136                 break;
4137         }
4138         if (err) {
4139                 /* Helper function cares less about error return values here
4140                  * as it is busy with pending work.
4141                  */
4142                 dev_info(dev, "PF failed to honor VF %d, opcode %d, error %d\n",
4143                          vf_id, v_opcode, err);
4144         }
4145
4146 finish:
4147         mutex_unlock(&vf->cfg_lock);
4148         ice_put_vf(vf);
4149 }
This page took 0.28581 seconds and 4 git commands to generate.