]> Git Repo - J-linux.git/blob - drivers/net/ethernet/intel/ice/ice_virtchnl.c
Merge tag 'vfs-6.13-rc7.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
[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_VLAN;
444
445         vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
446         vsi = ice_get_vf_vsi(vf);
447         if (!vsi) {
448                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
449                 goto err;
450         }
451
452         vfres->vf_cap_flags |= ice_vc_get_vlan_caps(hw, vf, vsi,
453                                                     vf->driver_caps);
454
455         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF)
456                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
457
458         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC)
459                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC;
460
461         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_FDIR_PF)
462                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_FDIR_PF;
463
464         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_TC_U32 &&
465             vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_FDIR_PF)
466                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_TC_U32;
467
468         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
469                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
470
471         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
472                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
473
474         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
475                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
476
477         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
478                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
479
480         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
481                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
482
483         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
484                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
485
486         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_CRC)
487                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_CRC;
488
489         if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
490                 vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
491
492         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF)
493                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF;
494
495         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_USO)
496                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_USO;
497
498         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_QOS)
499                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_QOS;
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 = ICE_VF_VSI_ID;
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         return vsi_id == ICE_VF_VSI_ID;
556 }
557
558 /**
559  * ice_vc_isvalid_q_id
560  * @vsi: VSI to check queue ID against
561  * @qid: VSI relative queue ID
562  *
563  * check for the valid queue ID
564  */
565 static bool ice_vc_isvalid_q_id(struct ice_vsi *vsi, u8 qid)
566 {
567         /* allocated Tx and Rx queues should be always equal for VF VSI */
568         return qid < vsi->alloc_txq;
569 }
570
571 /**
572  * ice_vc_isvalid_ring_len
573  * @ring_len: length of ring
574  *
575  * check for the valid ring count, should be multiple of ICE_REQ_DESC_MULTIPLE
576  * or zero
577  */
578 static bool ice_vc_isvalid_ring_len(u16 ring_len)
579 {
580         return ring_len == 0 ||
581                (ring_len >= ICE_MIN_NUM_DESC &&
582                 ring_len <= ICE_MAX_NUM_DESC &&
583                 !(ring_len % ICE_REQ_DESC_MULTIPLE));
584 }
585
586 /**
587  * ice_vc_validate_pattern
588  * @vf: pointer to the VF info
589  * @proto: virtchnl protocol headers
590  *
591  * validate the pattern is supported or not.
592  *
593  * Return: true on success, false on error.
594  */
595 bool
596 ice_vc_validate_pattern(struct ice_vf *vf, struct virtchnl_proto_hdrs *proto)
597 {
598         bool is_ipv4 = false;
599         bool is_ipv6 = false;
600         bool is_udp = false;
601         u16 ptype = -1;
602         int i = 0;
603
604         while (i < proto->count &&
605                proto->proto_hdr[i].type != VIRTCHNL_PROTO_HDR_NONE) {
606                 switch (proto->proto_hdr[i].type) {
607                 case VIRTCHNL_PROTO_HDR_ETH:
608                         ptype = ICE_PTYPE_MAC_PAY;
609                         break;
610                 case VIRTCHNL_PROTO_HDR_IPV4:
611                         ptype = ICE_PTYPE_IPV4_PAY;
612                         is_ipv4 = true;
613                         break;
614                 case VIRTCHNL_PROTO_HDR_IPV6:
615                         ptype = ICE_PTYPE_IPV6_PAY;
616                         is_ipv6 = true;
617                         break;
618                 case VIRTCHNL_PROTO_HDR_UDP:
619                         if (is_ipv4)
620                                 ptype = ICE_PTYPE_IPV4_UDP_PAY;
621                         else if (is_ipv6)
622                                 ptype = ICE_PTYPE_IPV6_UDP_PAY;
623                         is_udp = true;
624                         break;
625                 case VIRTCHNL_PROTO_HDR_TCP:
626                         if (is_ipv4)
627                                 ptype = ICE_PTYPE_IPV4_TCP_PAY;
628                         else if (is_ipv6)
629                                 ptype = ICE_PTYPE_IPV6_TCP_PAY;
630                         break;
631                 case VIRTCHNL_PROTO_HDR_SCTP:
632                         if (is_ipv4)
633                                 ptype = ICE_PTYPE_IPV4_SCTP_PAY;
634                         else if (is_ipv6)
635                                 ptype = ICE_PTYPE_IPV6_SCTP_PAY;
636                         break;
637                 case VIRTCHNL_PROTO_HDR_GTPU_IP:
638                 case VIRTCHNL_PROTO_HDR_GTPU_EH:
639                         if (is_ipv4)
640                                 ptype = ICE_MAC_IPV4_GTPU;
641                         else if (is_ipv6)
642                                 ptype = ICE_MAC_IPV6_GTPU;
643                         goto out;
644                 case VIRTCHNL_PROTO_HDR_L2TPV3:
645                         if (is_ipv4)
646                                 ptype = ICE_MAC_IPV4_L2TPV3;
647                         else if (is_ipv6)
648                                 ptype = ICE_MAC_IPV6_L2TPV3;
649                         goto out;
650                 case VIRTCHNL_PROTO_HDR_ESP:
651                         if (is_ipv4)
652                                 ptype = is_udp ? ICE_MAC_IPV4_NAT_T_ESP :
653                                                 ICE_MAC_IPV4_ESP;
654                         else if (is_ipv6)
655                                 ptype = is_udp ? ICE_MAC_IPV6_NAT_T_ESP :
656                                                 ICE_MAC_IPV6_ESP;
657                         goto out;
658                 case VIRTCHNL_PROTO_HDR_AH:
659                         if (is_ipv4)
660                                 ptype = ICE_MAC_IPV4_AH;
661                         else if (is_ipv6)
662                                 ptype = ICE_MAC_IPV6_AH;
663                         goto out;
664                 case VIRTCHNL_PROTO_HDR_PFCP:
665                         if (is_ipv4)
666                                 ptype = ICE_MAC_IPV4_PFCP_SESSION;
667                         else if (is_ipv6)
668                                 ptype = ICE_MAC_IPV6_PFCP_SESSION;
669                         goto out;
670                 default:
671                         break;
672                 }
673                 i++;
674         }
675
676 out:
677         return ice_hw_ptype_ena(&vf->pf->hw, ptype);
678 }
679
680 /**
681  * ice_vc_parse_rss_cfg - parses hash fields and headers from
682  * a specific virtchnl RSS cfg
683  * @hw: pointer to the hardware
684  * @rss_cfg: pointer to the virtchnl RSS cfg
685  * @hash_cfg: pointer to the HW hash configuration
686  *
687  * Return true if all the protocol header and hash fields in the RSS cfg could
688  * be parsed, else return false
689  *
690  * This function parses the virtchnl RSS cfg to be the intended
691  * hash fields and the intended header for RSS configuration
692  */
693 static bool ice_vc_parse_rss_cfg(struct ice_hw *hw,
694                                  struct virtchnl_rss_cfg *rss_cfg,
695                                  struct ice_rss_hash_cfg *hash_cfg)
696 {
697         const struct ice_vc_hash_field_match_type *hf_list;
698         const struct ice_vc_hdr_match_type *hdr_list;
699         int i, hf_list_len, hdr_list_len;
700         u32 *addl_hdrs = &hash_cfg->addl_hdrs;
701         u64 *hash_flds = &hash_cfg->hash_flds;
702
703         /* set outer layer RSS as default */
704         hash_cfg->hdr_type = ICE_RSS_OUTER_HEADERS;
705
706         if (rss_cfg->rss_algorithm == VIRTCHNL_RSS_ALG_TOEPLITZ_SYMMETRIC)
707                 hash_cfg->symm = true;
708         else
709                 hash_cfg->symm = false;
710
711         hf_list = ice_vc_hash_field_list;
712         hf_list_len = ARRAY_SIZE(ice_vc_hash_field_list);
713         hdr_list = ice_vc_hdr_list;
714         hdr_list_len = ARRAY_SIZE(ice_vc_hdr_list);
715
716         for (i = 0; i < rss_cfg->proto_hdrs.count; i++) {
717                 struct virtchnl_proto_hdr *proto_hdr =
718                                         &rss_cfg->proto_hdrs.proto_hdr[i];
719                 bool hdr_found = false;
720                 int j;
721
722                 /* Find matched ice headers according to virtchnl headers. */
723                 for (j = 0; j < hdr_list_len; j++) {
724                         struct ice_vc_hdr_match_type hdr_map = hdr_list[j];
725
726                         if (proto_hdr->type == hdr_map.vc_hdr) {
727                                 *addl_hdrs |= hdr_map.ice_hdr;
728                                 hdr_found = true;
729                         }
730                 }
731
732                 if (!hdr_found)
733                         return false;
734
735                 /* Find matched ice hash fields according to
736                  * virtchnl hash fields.
737                  */
738                 for (j = 0; j < hf_list_len; j++) {
739                         struct ice_vc_hash_field_match_type hf_map = hf_list[j];
740
741                         if (proto_hdr->type == hf_map.vc_hdr &&
742                             proto_hdr->field_selector == hf_map.vc_hash_field) {
743                                 *hash_flds |= hf_map.ice_hash_field;
744                                 break;
745                         }
746                 }
747         }
748
749         return true;
750 }
751
752 /**
753  * ice_vf_adv_rss_offload_ena - determine if capabilities support advanced
754  * RSS offloads
755  * @caps: VF driver negotiated capabilities
756  *
757  * Return true if VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF capability is set,
758  * else return false
759  */
760 static bool ice_vf_adv_rss_offload_ena(u32 caps)
761 {
762         return !!(caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF);
763 }
764
765 /**
766  * ice_vc_handle_rss_cfg
767  * @vf: pointer to the VF info
768  * @msg: pointer to the message buffer
769  * @add: add a RSS config if true, otherwise delete a RSS config
770  *
771  * This function adds/deletes a RSS config
772  */
773 static int ice_vc_handle_rss_cfg(struct ice_vf *vf, u8 *msg, bool add)
774 {
775         u32 v_opcode = add ? VIRTCHNL_OP_ADD_RSS_CFG : VIRTCHNL_OP_DEL_RSS_CFG;
776         struct virtchnl_rss_cfg *rss_cfg = (struct virtchnl_rss_cfg *)msg;
777         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
778         struct device *dev = ice_pf_to_dev(vf->pf);
779         struct ice_hw *hw = &vf->pf->hw;
780         struct ice_vsi *vsi;
781
782         if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
783                 dev_dbg(dev, "VF %d attempting to configure RSS, but RSS is not supported by the PF\n",
784                         vf->vf_id);
785                 v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
786                 goto error_param;
787         }
788
789         if (!ice_vf_adv_rss_offload_ena(vf->driver_caps)) {
790                 dev_dbg(dev, "VF %d attempting to configure RSS, but Advanced RSS offload is not supported\n",
791                         vf->vf_id);
792                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
793                 goto error_param;
794         }
795
796         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
797                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
798                 goto error_param;
799         }
800
801         if (rss_cfg->proto_hdrs.count > VIRTCHNL_MAX_NUM_PROTO_HDRS ||
802             rss_cfg->rss_algorithm < VIRTCHNL_RSS_ALG_TOEPLITZ_ASYMMETRIC ||
803             rss_cfg->rss_algorithm > VIRTCHNL_RSS_ALG_XOR_SYMMETRIC) {
804                 dev_dbg(dev, "VF %d attempting to configure RSS, but RSS configuration is not valid\n",
805                         vf->vf_id);
806                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
807                 goto error_param;
808         }
809
810         vsi = ice_get_vf_vsi(vf);
811         if (!vsi) {
812                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
813                 goto error_param;
814         }
815
816         if (!ice_vc_validate_pattern(vf, &rss_cfg->proto_hdrs)) {
817                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
818                 goto error_param;
819         }
820
821         if (rss_cfg->rss_algorithm == VIRTCHNL_RSS_ALG_R_ASYMMETRIC) {
822                 struct ice_vsi_ctx *ctx;
823                 u8 lut_type, hash_type;
824                 int status;
825
826                 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
827                 hash_type = add ? ICE_AQ_VSI_Q_OPT_RSS_HASH_XOR :
828                                 ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ;
829
830                 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
831                 if (!ctx) {
832                         v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
833                         goto error_param;
834                 }
835
836                 ctx->info.q_opt_rss =
837                         FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_LUT_M, lut_type) |
838                         FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_HASH_M, hash_type);
839
840                 /* Preserve existing queueing option setting */
841                 ctx->info.q_opt_rss |= (vsi->info.q_opt_rss &
842                                           ICE_AQ_VSI_Q_OPT_RSS_GBL_LUT_M);
843                 ctx->info.q_opt_tc = vsi->info.q_opt_tc;
844                 ctx->info.q_opt_flags = vsi->info.q_opt_rss;
845
846                 ctx->info.valid_sections =
847                                 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
848
849                 status = ice_update_vsi(hw, vsi->idx, ctx, NULL);
850                 if (status) {
851                         dev_err(dev, "update VSI for RSS failed, err %d aq_err %s\n",
852                                 status, ice_aq_str(hw->adminq.sq_last_status));
853                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
854                 } else {
855                         vsi->info.q_opt_rss = ctx->info.q_opt_rss;
856                 }
857
858                 kfree(ctx);
859         } else {
860                 struct ice_rss_hash_cfg cfg;
861
862                 /* Only check for none raw pattern case */
863                 if (!ice_vc_validate_pattern(vf, &rss_cfg->proto_hdrs)) {
864                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
865                         goto error_param;
866                 }
867                 cfg.addl_hdrs = ICE_FLOW_SEG_HDR_NONE;
868                 cfg.hash_flds = ICE_HASH_INVALID;
869                 cfg.hdr_type = ICE_RSS_ANY_HEADERS;
870
871                 if (!ice_vc_parse_rss_cfg(hw, rss_cfg, &cfg)) {
872                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
873                         goto error_param;
874                 }
875
876                 if (add) {
877                         if (ice_add_rss_cfg(hw, vsi, &cfg)) {
878                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
879                                 dev_err(dev, "ice_add_rss_cfg failed for vsi = %d, v_ret = %d\n",
880                                         vsi->vsi_num, v_ret);
881                         }
882                 } else {
883                         int status;
884
885                         status = ice_rem_rss_cfg(hw, vsi->idx, &cfg);
886                         /* We just ignore -ENOENT, because if two configurations
887                          * share the same profile remove one of them actually
888                          * removes both, since the profile is deleted.
889                          */
890                         if (status && status != -ENOENT) {
891                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
892                                 dev_err(dev, "ice_rem_rss_cfg failed for VF ID:%d, error:%d\n",
893                                         vf->vf_id, status);
894                         }
895                 }
896         }
897
898 error_param:
899         return ice_vc_send_msg_to_vf(vf, v_opcode, v_ret, NULL, 0);
900 }
901
902 /**
903  * ice_vc_config_rss_key
904  * @vf: pointer to the VF info
905  * @msg: pointer to the msg buffer
906  *
907  * Configure the VF's RSS key
908  */
909 static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
910 {
911         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
912         struct virtchnl_rss_key *vrk =
913                 (struct virtchnl_rss_key *)msg;
914         struct ice_vsi *vsi;
915
916         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
917                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
918                 goto error_param;
919         }
920
921         if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
922                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
923                 goto error_param;
924         }
925
926         if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
927                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
928                 goto error_param;
929         }
930
931         if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
932                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
933                 goto error_param;
934         }
935
936         vsi = ice_get_vf_vsi(vf);
937         if (!vsi) {
938                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
939                 goto error_param;
940         }
941
942         if (ice_set_rss_key(vsi, vrk->key))
943                 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
944 error_param:
945         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
946                                      NULL, 0);
947 }
948
949 /**
950  * ice_vc_config_rss_lut
951  * @vf: pointer to the VF info
952  * @msg: pointer to the msg buffer
953  *
954  * Configure the VF's RSS LUT
955  */
956 static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
957 {
958         struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
959         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
960         struct ice_vsi *vsi;
961
962         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
963                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
964                 goto error_param;
965         }
966
967         if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
968                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
969                 goto error_param;
970         }
971
972         if (vrl->lut_entries != ICE_LUT_VSI_SIZE) {
973                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
974                 goto error_param;
975         }
976
977         if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
978                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
979                 goto error_param;
980         }
981
982         vsi = ice_get_vf_vsi(vf);
983         if (!vsi) {
984                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
985                 goto error_param;
986         }
987
988         if (ice_set_rss_lut(vsi, vrl->lut, ICE_LUT_VSI_SIZE))
989                 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
990 error_param:
991         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
992                                      NULL, 0);
993 }
994
995 /**
996  * ice_vc_config_rss_hfunc
997  * @vf: pointer to the VF info
998  * @msg: pointer to the msg buffer
999  *
1000  * Configure the VF's RSS Hash function
1001  */
1002 static int ice_vc_config_rss_hfunc(struct ice_vf *vf, u8 *msg)
1003 {
1004         struct virtchnl_rss_hfunc *vrh = (struct virtchnl_rss_hfunc *)msg;
1005         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1006         u8 hfunc = ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ;
1007         struct ice_vsi *vsi;
1008
1009         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1010                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1011                 goto error_param;
1012         }
1013
1014         if (!ice_vc_isvalid_vsi_id(vf, vrh->vsi_id)) {
1015                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1016                 goto error_param;
1017         }
1018
1019         if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
1020                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1021                 goto error_param;
1022         }
1023
1024         vsi = ice_get_vf_vsi(vf);
1025         if (!vsi) {
1026                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1027                 goto error_param;
1028         }
1029
1030         if (vrh->rss_algorithm == VIRTCHNL_RSS_ALG_TOEPLITZ_SYMMETRIC)
1031                 hfunc = ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ;
1032
1033         if (ice_set_rss_hfunc(vsi, hfunc))
1034                 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1035 error_param:
1036         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_HFUNC, v_ret,
1037                                      NULL, 0);
1038 }
1039
1040 /**
1041  * ice_vc_get_qos_caps - Get current QoS caps from PF
1042  * @vf: pointer to the VF info
1043  *
1044  * Get VF's QoS capabilities, such as TC number, arbiter and
1045  * bandwidth from PF.
1046  *
1047  * Return: 0 on success or negative error value.
1048  */
1049 static int ice_vc_get_qos_caps(struct ice_vf *vf)
1050 {
1051         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1052         struct virtchnl_qos_cap_list *cap_list = NULL;
1053         u8 tc_prio[ICE_MAX_TRAFFIC_CLASS] = { 0 };
1054         struct virtchnl_qos_cap_elem *cfg = NULL;
1055         struct ice_vsi_ctx *vsi_ctx;
1056         struct ice_pf *pf = vf->pf;
1057         struct ice_port_info *pi;
1058         struct ice_vsi *vsi;
1059         u8 numtc, tc;
1060         u16 len = 0;
1061         int ret, i;
1062
1063         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1064                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1065                 goto err;
1066         }
1067
1068         vsi = ice_get_vf_vsi(vf);
1069         if (!vsi) {
1070                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1071                 goto err;
1072         }
1073
1074         pi = pf->hw.port_info;
1075         numtc = vsi->tc_cfg.numtc;
1076
1077         vsi_ctx = ice_get_vsi_ctx(pi->hw, vf->lan_vsi_idx);
1078         if (!vsi_ctx) {
1079                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1080                 goto err;
1081         }
1082
1083         len = struct_size(cap_list, cap, numtc);
1084         cap_list = kzalloc(len, GFP_KERNEL);
1085         if (!cap_list) {
1086                 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
1087                 len = 0;
1088                 goto err;
1089         }
1090
1091         cap_list->vsi_id = vsi->vsi_num;
1092         cap_list->num_elem = numtc;
1093
1094         /* Store the UP2TC configuration from DCB to a user priority bitmap
1095          * of each TC. Each element of prio_of_tc represents one TC. Each
1096          * bitmap indicates the user priorities belong to this TC.
1097          */
1098         for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
1099                 tc = pi->qos_cfg.local_dcbx_cfg.etscfg.prio_table[i];
1100                 tc_prio[tc] |= BIT(i);
1101         }
1102
1103         for (i = 0; i < numtc; i++) {
1104                 cfg = &cap_list->cap[i];
1105                 cfg->tc_num = i;
1106                 cfg->tc_prio = tc_prio[i];
1107                 cfg->arbiter = pi->qos_cfg.local_dcbx_cfg.etscfg.tsatable[i];
1108                 cfg->weight = VIRTCHNL_STRICT_WEIGHT;
1109                 cfg->type = VIRTCHNL_BW_SHAPER;
1110                 cfg->shaper.committed = vsi_ctx->sched.bw_t_info[i].cir_bw.bw;
1111                 cfg->shaper.peak = vsi_ctx->sched.bw_t_info[i].eir_bw.bw;
1112         }
1113
1114 err:
1115         ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_QOS_CAPS, v_ret,
1116                                     (u8 *)cap_list, len);
1117         kfree(cap_list);
1118         return ret;
1119 }
1120
1121 /**
1122  * ice_vf_cfg_qs_bw - Configure per queue bandwidth
1123  * @vf: pointer to the VF info
1124  * @num_queues: number of queues to be configured
1125  *
1126  * Configure per queue bandwidth.
1127  *
1128  * Return: 0 on success or negative error value.
1129  */
1130 static int ice_vf_cfg_qs_bw(struct ice_vf *vf, u16 num_queues)
1131 {
1132         struct ice_hw *hw = &vf->pf->hw;
1133         struct ice_vsi *vsi;
1134         int ret;
1135         u16 i;
1136
1137         vsi = ice_get_vf_vsi(vf);
1138         if (!vsi)
1139                 return -EINVAL;
1140
1141         for (i = 0; i < num_queues; i++) {
1142                 u32 p_rate, min_rate;
1143                 u8 tc;
1144
1145                 p_rate = vf->qs_bw[i].peak;
1146                 min_rate = vf->qs_bw[i].committed;
1147                 tc = vf->qs_bw[i].tc;
1148                 if (p_rate)
1149                         ret = ice_cfg_q_bw_lmt(hw->port_info, vsi->idx, tc,
1150                                                vf->qs_bw[i].queue_id,
1151                                                ICE_MAX_BW, p_rate);
1152                 else
1153                         ret = ice_cfg_q_bw_dflt_lmt(hw->port_info, vsi->idx, tc,
1154                                                     vf->qs_bw[i].queue_id,
1155                                                     ICE_MAX_BW);
1156                 if (ret)
1157                         return ret;
1158
1159                 if (min_rate)
1160                         ret = ice_cfg_q_bw_lmt(hw->port_info, vsi->idx, tc,
1161                                                vf->qs_bw[i].queue_id,
1162                                                ICE_MIN_BW, min_rate);
1163                 else
1164                         ret = ice_cfg_q_bw_dflt_lmt(hw->port_info, vsi->idx, tc,
1165                                                     vf->qs_bw[i].queue_id,
1166                                                     ICE_MIN_BW);
1167
1168                 if (ret)
1169                         return ret;
1170         }
1171
1172         return 0;
1173 }
1174
1175 /**
1176  * ice_vf_cfg_q_quanta_profile - Configure quanta profile
1177  * @vf: pointer to the VF info
1178  * @quanta_prof_idx: pointer to the quanta profile index
1179  * @quanta_size: quanta size to be set
1180  *
1181  * This function chooses available quanta profile and configures the register.
1182  * The quanta profile is evenly divided by the number of device ports, and then
1183  * available to the specific PF and VFs. The first profile for each PF is a
1184  * reserved default profile. Only quanta size of the rest unused profile can be
1185  * modified.
1186  *
1187  * Return: 0 on success or negative error value.
1188  */
1189 static int ice_vf_cfg_q_quanta_profile(struct ice_vf *vf, u16 quanta_size,
1190                                        u16 *quanta_prof_idx)
1191 {
1192         const u16 n_desc = calc_quanta_desc(quanta_size);
1193         struct ice_hw *hw = &vf->pf->hw;
1194         const u16 n_cmd = 2 * n_desc;
1195         struct ice_pf *pf = vf->pf;
1196         u16 per_pf, begin_id;
1197         u8 n_used;
1198         u32 reg;
1199
1200         begin_id = (GLCOMM_QUANTA_PROF_MAX_INDEX + 1) / hw->dev_caps.num_funcs *
1201                    hw->logical_pf_id;
1202
1203         if (quanta_size == ICE_DFLT_QUANTA) {
1204                 *quanta_prof_idx = begin_id;
1205         } else {
1206                 per_pf = (GLCOMM_QUANTA_PROF_MAX_INDEX + 1) /
1207                          hw->dev_caps.num_funcs;
1208                 n_used = pf->num_quanta_prof_used;
1209                 if (n_used < per_pf) {
1210                         *quanta_prof_idx = begin_id + 1 + n_used;
1211                         pf->num_quanta_prof_used++;
1212                 } else {
1213                         return -EINVAL;
1214                 }
1215         }
1216
1217         reg = FIELD_PREP(GLCOMM_QUANTA_PROF_QUANTA_SIZE_M, quanta_size) |
1218               FIELD_PREP(GLCOMM_QUANTA_PROF_MAX_CMD_M, n_cmd) |
1219               FIELD_PREP(GLCOMM_QUANTA_PROF_MAX_DESC_M, n_desc);
1220         wr32(hw, GLCOMM_QUANTA_PROF(*quanta_prof_idx), reg);
1221
1222         return 0;
1223 }
1224
1225 /**
1226  * ice_vc_cfg_promiscuous_mode_msg
1227  * @vf: pointer to the VF info
1228  * @msg: pointer to the msg buffer
1229  *
1230  * called from the VF to configure VF VSIs promiscuous mode
1231  */
1232 static int ice_vc_cfg_promiscuous_mode_msg(struct ice_vf *vf, u8 *msg)
1233 {
1234         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1235         bool rm_promisc, alluni = false, allmulti = false;
1236         struct virtchnl_promisc_info *info =
1237             (struct virtchnl_promisc_info *)msg;
1238         struct ice_vsi_vlan_ops *vlan_ops;
1239         int mcast_err = 0, ucast_err = 0;
1240         struct ice_pf *pf = vf->pf;
1241         struct ice_vsi *vsi;
1242         u8 mcast_m, ucast_m;
1243         struct device *dev;
1244         int ret = 0;
1245
1246         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1247                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1248                 goto error_param;
1249         }
1250
1251         if (!ice_vc_isvalid_vsi_id(vf, info->vsi_id)) {
1252                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1253                 goto error_param;
1254         }
1255
1256         vsi = ice_get_vf_vsi(vf);
1257         if (!vsi) {
1258                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1259                 goto error_param;
1260         }
1261
1262         dev = ice_pf_to_dev(pf);
1263         if (!ice_is_vf_trusted(vf)) {
1264                 dev_err(dev, "Unprivileged VF %d is attempting to configure promiscuous mode\n",
1265                         vf->vf_id);
1266                 /* Leave v_ret alone, lie to the VF on purpose. */
1267                 goto error_param;
1268         }
1269
1270         if (info->flags & FLAG_VF_UNICAST_PROMISC)
1271                 alluni = true;
1272
1273         if (info->flags & FLAG_VF_MULTICAST_PROMISC)
1274                 allmulti = true;
1275
1276         rm_promisc = !allmulti && !alluni;
1277
1278         vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
1279         if (rm_promisc)
1280                 ret = vlan_ops->ena_rx_filtering(vsi);
1281         else
1282                 ret = vlan_ops->dis_rx_filtering(vsi);
1283         if (ret) {
1284                 dev_err(dev, "Failed to configure VLAN pruning in promiscuous mode\n");
1285                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1286                 goto error_param;
1287         }
1288
1289         ice_vf_get_promisc_masks(vf, vsi, &ucast_m, &mcast_m);
1290
1291         if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
1292                 if (alluni) {
1293                         /* in this case we're turning on promiscuous mode */
1294                         ret = ice_set_dflt_vsi(vsi);
1295                 } else {
1296                         /* in this case we're turning off promiscuous mode */
1297                         if (ice_is_dflt_vsi_in_use(vsi->port_info))
1298                                 ret = ice_clear_dflt_vsi(vsi);
1299                 }
1300
1301                 /* in this case we're turning on/off only
1302                  * allmulticast
1303                  */
1304                 if (allmulti)
1305                         mcast_err = ice_vf_set_vsi_promisc(vf, vsi, mcast_m);
1306                 else
1307                         mcast_err = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
1308
1309                 if (ret) {
1310                         dev_err(dev, "Turning on/off promiscuous mode for VF %d failed, error: %d\n",
1311                                 vf->vf_id, ret);
1312                         v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1313                         goto error_param;
1314                 }
1315         } else {
1316                 if (alluni)
1317                         ucast_err = ice_vf_set_vsi_promisc(vf, vsi, ucast_m);
1318                 else
1319                         ucast_err = ice_vf_clear_vsi_promisc(vf, vsi, ucast_m);
1320
1321                 if (allmulti)
1322                         mcast_err = ice_vf_set_vsi_promisc(vf, vsi, mcast_m);
1323                 else
1324                         mcast_err = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
1325
1326                 if (ucast_err || mcast_err)
1327                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1328         }
1329
1330         if (!mcast_err) {
1331                 if (allmulti &&
1332                     !test_and_set_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
1333                         dev_info(dev, "VF %u successfully set multicast promiscuous mode\n",
1334                                  vf->vf_id);
1335                 else if (!allmulti &&
1336                          test_and_clear_bit(ICE_VF_STATE_MC_PROMISC,
1337                                             vf->vf_states))
1338                         dev_info(dev, "VF %u successfully unset multicast promiscuous mode\n",
1339                                  vf->vf_id);
1340         } else {
1341                 dev_err(dev, "Error while modifying multicast promiscuous mode for VF %u, error: %d\n",
1342                         vf->vf_id, mcast_err);
1343         }
1344
1345         if (!ucast_err) {
1346                 if (alluni &&
1347                     !test_and_set_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
1348                         dev_info(dev, "VF %u successfully set unicast promiscuous mode\n",
1349                                  vf->vf_id);
1350                 else if (!alluni &&
1351                          test_and_clear_bit(ICE_VF_STATE_UC_PROMISC,
1352                                             vf->vf_states))
1353                         dev_info(dev, "VF %u successfully unset unicast promiscuous mode\n",
1354                                  vf->vf_id);
1355         } else {
1356                 dev_err(dev, "Error while modifying unicast promiscuous mode for VF %u, error: %d\n",
1357                         vf->vf_id, ucast_err);
1358         }
1359
1360 error_param:
1361         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1362                                      v_ret, NULL, 0);
1363 }
1364
1365 /**
1366  * ice_vc_get_stats_msg
1367  * @vf: pointer to the VF info
1368  * @msg: pointer to the msg buffer
1369  *
1370  * called from the VF to get VSI stats
1371  */
1372 static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
1373 {
1374         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1375         struct virtchnl_queue_select *vqs =
1376                 (struct virtchnl_queue_select *)msg;
1377         struct ice_eth_stats stats = { 0 };
1378         struct ice_vsi *vsi;
1379
1380         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1381                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1382                 goto error_param;
1383         }
1384
1385         if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1386                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1387                 goto error_param;
1388         }
1389
1390         vsi = ice_get_vf_vsi(vf);
1391         if (!vsi) {
1392                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1393                 goto error_param;
1394         }
1395
1396         ice_update_eth_stats(vsi);
1397
1398         stats = vsi->eth_stats;
1399
1400 error_param:
1401         /* send the response to the VF */
1402         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
1403                                      (u8 *)&stats, sizeof(stats));
1404 }
1405
1406 /**
1407  * ice_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTCHNL
1408  * @vqs: virtchnl_queue_select structure containing bitmaps to validate
1409  *
1410  * Return true on successful validation, else false
1411  */
1412 static bool ice_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
1413 {
1414         if ((!vqs->rx_queues && !vqs->tx_queues) ||
1415             vqs->rx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF) ||
1416             vqs->tx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF))
1417                 return false;
1418
1419         return true;
1420 }
1421
1422 /**
1423  * ice_vf_ena_txq_interrupt - enable Tx queue interrupt via QINT_TQCTL
1424  * @vsi: VSI of the VF to configure
1425  * @q_idx: VF queue index used to determine the queue in the PF's space
1426  */
1427 static void ice_vf_ena_txq_interrupt(struct ice_vsi *vsi, u32 q_idx)
1428 {
1429         struct ice_hw *hw = &vsi->back->hw;
1430         u32 pfq = vsi->txq_map[q_idx];
1431         u32 reg;
1432
1433         reg = rd32(hw, QINT_TQCTL(pfq));
1434
1435         /* MSI-X index 0 in the VF's space is always for the OICR, which means
1436          * this is most likely a poll mode VF driver, so don't enable an
1437          * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
1438          */
1439         if (!(reg & QINT_TQCTL_MSIX_INDX_M))
1440                 return;
1441
1442         wr32(hw, QINT_TQCTL(pfq), reg | QINT_TQCTL_CAUSE_ENA_M);
1443 }
1444
1445 /**
1446  * ice_vf_ena_rxq_interrupt - enable Tx queue interrupt via QINT_RQCTL
1447  * @vsi: VSI of the VF to configure
1448  * @q_idx: VF queue index used to determine the queue in the PF's space
1449  */
1450 static void ice_vf_ena_rxq_interrupt(struct ice_vsi *vsi, u32 q_idx)
1451 {
1452         struct ice_hw *hw = &vsi->back->hw;
1453         u32 pfq = vsi->rxq_map[q_idx];
1454         u32 reg;
1455
1456         reg = rd32(hw, QINT_RQCTL(pfq));
1457
1458         /* MSI-X index 0 in the VF's space is always for the OICR, which means
1459          * this is most likely a poll mode VF driver, so don't enable an
1460          * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
1461          */
1462         if (!(reg & QINT_RQCTL_MSIX_INDX_M))
1463                 return;
1464
1465         wr32(hw, QINT_RQCTL(pfq), reg | QINT_RQCTL_CAUSE_ENA_M);
1466 }
1467
1468 /**
1469  * ice_vc_ena_qs_msg
1470  * @vf: pointer to the VF info
1471  * @msg: pointer to the msg buffer
1472  *
1473  * called from the VF to enable all or specific queue(s)
1474  */
1475 static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
1476 {
1477         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1478         struct virtchnl_queue_select *vqs =
1479             (struct virtchnl_queue_select *)msg;
1480         struct ice_vsi *vsi;
1481         unsigned long q_map;
1482         u16 vf_q_id;
1483
1484         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1485                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1486                 goto error_param;
1487         }
1488
1489         if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1490                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1491                 goto error_param;
1492         }
1493
1494         if (!ice_vc_validate_vqs_bitmaps(vqs)) {
1495                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1496                 goto error_param;
1497         }
1498
1499         vsi = ice_get_vf_vsi(vf);
1500         if (!vsi) {
1501                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1502                 goto error_param;
1503         }
1504
1505         /* Enable only Rx rings, Tx rings were enabled by the FW when the
1506          * Tx queue group list was configured and the context bits were
1507          * programmed using ice_vsi_cfg_txqs
1508          */
1509         q_map = vqs->rx_queues;
1510         for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1511                 if (!ice_vc_isvalid_q_id(vsi, vf_q_id)) {
1512                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1513                         goto error_param;
1514                 }
1515
1516                 /* Skip queue if enabled */
1517                 if (test_bit(vf_q_id, vf->rxq_ena))
1518                         continue;
1519
1520                 if (ice_vsi_ctrl_one_rx_ring(vsi, true, vf_q_id, true)) {
1521                         dev_err(ice_pf_to_dev(vsi->back), "Failed to enable Rx ring %d on VSI %d\n",
1522                                 vf_q_id, vsi->vsi_num);
1523                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1524                         goto error_param;
1525                 }
1526
1527                 ice_vf_ena_rxq_interrupt(vsi, vf_q_id);
1528                 set_bit(vf_q_id, vf->rxq_ena);
1529         }
1530
1531         q_map = vqs->tx_queues;
1532         for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1533                 if (!ice_vc_isvalid_q_id(vsi, vf_q_id)) {
1534                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1535                         goto error_param;
1536                 }
1537
1538                 /* Skip queue if enabled */
1539                 if (test_bit(vf_q_id, vf->txq_ena))
1540                         continue;
1541
1542                 ice_vf_ena_txq_interrupt(vsi, vf_q_id);
1543                 set_bit(vf_q_id, vf->txq_ena);
1544         }
1545
1546         /* Set flag to indicate that queues are enabled */
1547         if (v_ret == VIRTCHNL_STATUS_SUCCESS)
1548                 set_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
1549
1550 error_param:
1551         /* send the response to the VF */
1552         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
1553                                      NULL, 0);
1554 }
1555
1556 /**
1557  * ice_vf_vsi_dis_single_txq - disable a single Tx queue
1558  * @vf: VF to disable queue for
1559  * @vsi: VSI for the VF
1560  * @q_id: VF relative (0-based) queue ID
1561  *
1562  * Attempt to disable the Tx queue passed in. If the Tx queue was successfully
1563  * disabled then clear q_id bit in the enabled queues bitmap and return
1564  * success. Otherwise return error.
1565  */
1566 static int
1567 ice_vf_vsi_dis_single_txq(struct ice_vf *vf, struct ice_vsi *vsi, u16 q_id)
1568 {
1569         struct ice_txq_meta txq_meta = { 0 };
1570         struct ice_tx_ring *ring;
1571         int err;
1572
1573         if (!test_bit(q_id, vf->txq_ena))
1574                 dev_dbg(ice_pf_to_dev(vsi->back), "Queue %u on VSI %u is not enabled, but stopping it anyway\n",
1575                         q_id, vsi->vsi_num);
1576
1577         ring = vsi->tx_rings[q_id];
1578         if (!ring)
1579                 return -EINVAL;
1580
1581         ice_fill_txq_meta(vsi, ring, &txq_meta);
1582
1583         err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, vf->vf_id, ring, &txq_meta);
1584         if (err) {
1585                 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Tx ring %d on VSI %d\n",
1586                         q_id, vsi->vsi_num);
1587                 return err;
1588         }
1589
1590         /* Clear enabled queues flag */
1591         clear_bit(q_id, vf->txq_ena);
1592
1593         return 0;
1594 }
1595
1596 /**
1597  * ice_vc_dis_qs_msg
1598  * @vf: pointer to the VF info
1599  * @msg: pointer to the msg buffer
1600  *
1601  * called from the VF to disable all or specific queue(s)
1602  */
1603 static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
1604 {
1605         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1606         struct virtchnl_queue_select *vqs =
1607             (struct virtchnl_queue_select *)msg;
1608         struct ice_vsi *vsi;
1609         unsigned long q_map;
1610         u16 vf_q_id;
1611
1612         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
1613             !test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states)) {
1614                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1615                 goto error_param;
1616         }
1617
1618         if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1619                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1620                 goto error_param;
1621         }
1622
1623         if (!ice_vc_validate_vqs_bitmaps(vqs)) {
1624                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1625                 goto error_param;
1626         }
1627
1628         vsi = ice_get_vf_vsi(vf);
1629         if (!vsi) {
1630                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1631                 goto error_param;
1632         }
1633
1634         if (vqs->tx_queues) {
1635                 q_map = vqs->tx_queues;
1636
1637                 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1638                         if (!ice_vc_isvalid_q_id(vsi, vf_q_id)) {
1639                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1640                                 goto error_param;
1641                         }
1642
1643                         if (ice_vf_vsi_dis_single_txq(vf, vsi, vf_q_id)) {
1644                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1645                                 goto error_param;
1646                         }
1647                 }
1648         }
1649
1650         q_map = vqs->rx_queues;
1651         /* speed up Rx queue disable by batching them if possible */
1652         if (q_map &&
1653             bitmap_equal(&q_map, vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF)) {
1654                 if (ice_vsi_stop_all_rx_rings(vsi)) {
1655                         dev_err(ice_pf_to_dev(vsi->back), "Failed to stop all Rx rings on VSI %d\n",
1656                                 vsi->vsi_num);
1657                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1658                         goto error_param;
1659                 }
1660
1661                 bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
1662         } else if (q_map) {
1663                 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1664                         if (!ice_vc_isvalid_q_id(vsi, vf_q_id)) {
1665                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1666                                 goto error_param;
1667                         }
1668
1669                         /* Skip queue if not enabled */
1670                         if (!test_bit(vf_q_id, vf->rxq_ena))
1671                                 continue;
1672
1673                         if (ice_vsi_ctrl_one_rx_ring(vsi, false, vf_q_id,
1674                                                      true)) {
1675                                 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Rx ring %d on VSI %d\n",
1676                                         vf_q_id, vsi->vsi_num);
1677                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1678                                 goto error_param;
1679                         }
1680
1681                         /* Clear enabled queues flag */
1682                         clear_bit(vf_q_id, vf->rxq_ena);
1683                 }
1684         }
1685
1686         /* Clear enabled queues flag */
1687         if (v_ret == VIRTCHNL_STATUS_SUCCESS && ice_vf_has_no_qs_ena(vf))
1688                 clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
1689
1690 error_param:
1691         /* send the response to the VF */
1692         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
1693                                      NULL, 0);
1694 }
1695
1696 /**
1697  * ice_cfg_interrupt
1698  * @vf: pointer to the VF info
1699  * @vsi: the VSI being configured
1700  * @map: vector map for mapping vectors to queues
1701  * @q_vector: structure for interrupt vector
1702  * configure the IRQ to queue map
1703  */
1704 static enum virtchnl_status_code
1705 ice_cfg_interrupt(struct ice_vf *vf, struct ice_vsi *vsi,
1706                   struct virtchnl_vector_map *map,
1707                   struct ice_q_vector *q_vector)
1708 {
1709         u16 vsi_q_id, vsi_q_id_idx;
1710         unsigned long qmap;
1711
1712         q_vector->num_ring_rx = 0;
1713         q_vector->num_ring_tx = 0;
1714
1715         qmap = map->rxq_map;
1716         for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
1717                 vsi_q_id = vsi_q_id_idx;
1718
1719                 if (!ice_vc_isvalid_q_id(vsi, vsi_q_id))
1720                         return VIRTCHNL_STATUS_ERR_PARAM;
1721
1722                 q_vector->num_ring_rx++;
1723                 q_vector->rx.itr_idx = map->rxitr_idx;
1724                 vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
1725                 ice_cfg_rxq_interrupt(vsi, vsi_q_id,
1726                                       q_vector->vf_reg_idx,
1727                                       q_vector->rx.itr_idx);
1728         }
1729
1730         qmap = map->txq_map;
1731         for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
1732                 vsi_q_id = vsi_q_id_idx;
1733
1734                 if (!ice_vc_isvalid_q_id(vsi, vsi_q_id))
1735                         return VIRTCHNL_STATUS_ERR_PARAM;
1736
1737                 q_vector->num_ring_tx++;
1738                 q_vector->tx.itr_idx = map->txitr_idx;
1739                 vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
1740                 ice_cfg_txq_interrupt(vsi, vsi_q_id,
1741                                       q_vector->vf_reg_idx,
1742                                       q_vector->tx.itr_idx);
1743         }
1744
1745         return VIRTCHNL_STATUS_SUCCESS;
1746 }
1747
1748 /**
1749  * ice_vc_cfg_irq_map_msg
1750  * @vf: pointer to the VF info
1751  * @msg: pointer to the msg buffer
1752  *
1753  * called from the VF to configure the IRQ to queue map
1754  */
1755 static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
1756 {
1757         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1758         u16 num_q_vectors_mapped, vsi_id, vector_id;
1759         struct virtchnl_irq_map_info *irqmap_info;
1760         struct virtchnl_vector_map *map;
1761         struct ice_vsi *vsi;
1762         int i;
1763
1764         irqmap_info = (struct virtchnl_irq_map_info *)msg;
1765         num_q_vectors_mapped = irqmap_info->num_vectors;
1766
1767         /* Check to make sure number of VF vectors mapped is not greater than
1768          * number of VF vectors originally allocated, and check that
1769          * there is actually at least a single VF queue vector mapped
1770          */
1771         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
1772             vf->num_msix < num_q_vectors_mapped ||
1773             !num_q_vectors_mapped) {
1774                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1775                 goto error_param;
1776         }
1777
1778         vsi = ice_get_vf_vsi(vf);
1779         if (!vsi) {
1780                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1781                 goto error_param;
1782         }
1783
1784         for (i = 0; i < num_q_vectors_mapped; i++) {
1785                 struct ice_q_vector *q_vector;
1786
1787                 map = &irqmap_info->vecmap[i];
1788
1789                 vector_id = map->vector_id;
1790                 vsi_id = map->vsi_id;
1791                 /* vector_id is always 0-based for each VF, and can never be
1792                  * larger than or equal to the max allowed interrupts per VF
1793                  */
1794                 if (!(vector_id < vf->num_msix) ||
1795                     !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
1796                     (!vector_id && (map->rxq_map || map->txq_map))) {
1797                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1798                         goto error_param;
1799                 }
1800
1801                 /* No need to map VF miscellaneous or rogue vector */
1802                 if (!vector_id)
1803                         continue;
1804
1805                 /* Subtract non queue vector from vector_id passed by VF
1806                  * to get actual number of VSI queue vector array index
1807                  */
1808                 q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
1809                 if (!q_vector) {
1810                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1811                         goto error_param;
1812                 }
1813
1814                 /* lookout for the invalid queue index */
1815                 v_ret = ice_cfg_interrupt(vf, vsi, map, q_vector);
1816                 if (v_ret)
1817                         goto error_param;
1818         }
1819
1820 error_param:
1821         /* send the response to the VF */
1822         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
1823                                      NULL, 0);
1824 }
1825
1826 /**
1827  * ice_vc_cfg_q_bw - Configure per queue bandwidth
1828  * @vf: pointer to the VF info
1829  * @msg: pointer to the msg buffer which holds the command descriptor
1830  *
1831  * Configure VF queues bandwidth.
1832  *
1833  * Return: 0 on success or negative error value.
1834  */
1835 static int ice_vc_cfg_q_bw(struct ice_vf *vf, u8 *msg)
1836 {
1837         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1838         struct virtchnl_queues_bw_cfg *qbw =
1839                 (struct virtchnl_queues_bw_cfg *)msg;
1840         struct ice_vsi *vsi;
1841         u16 i;
1842
1843         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
1844             !ice_vc_isvalid_vsi_id(vf, qbw->vsi_id)) {
1845                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1846                 goto err;
1847         }
1848
1849         vsi = ice_get_vf_vsi(vf);
1850         if (!vsi) {
1851                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1852                 goto err;
1853         }
1854
1855         if (qbw->num_queues > ICE_MAX_RSS_QS_PER_VF ||
1856             qbw->num_queues > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
1857                 dev_err(ice_pf_to_dev(vf->pf), "VF-%d trying to configure more than allocated number of queues: %d\n",
1858                         vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
1859                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1860                 goto err;
1861         }
1862
1863         for (i = 0; i < qbw->num_queues; i++) {
1864                 if (qbw->cfg[i].shaper.peak != 0 && vf->max_tx_rate != 0 &&
1865                     qbw->cfg[i].shaper.peak > vf->max_tx_rate)
1866                         dev_warn(ice_pf_to_dev(vf->pf), "The maximum queue %d rate limit configuration may not take effect because the maximum TX rate for VF-%d is %d\n",
1867                                  qbw->cfg[i].queue_id, vf->vf_id,
1868                                  vf->max_tx_rate);
1869                 if (qbw->cfg[i].shaper.committed != 0 && vf->min_tx_rate != 0 &&
1870                     qbw->cfg[i].shaper.committed < vf->min_tx_rate)
1871                         dev_warn(ice_pf_to_dev(vf->pf), "The minimum queue %d rate limit configuration may not take effect because the minimum TX rate for VF-%d is %d\n",
1872                                  qbw->cfg[i].queue_id, vf->vf_id,
1873                                  vf->max_tx_rate);
1874         }
1875
1876         for (i = 0; i < qbw->num_queues; i++) {
1877                 vf->qs_bw[i].queue_id = qbw->cfg[i].queue_id;
1878                 vf->qs_bw[i].peak = qbw->cfg[i].shaper.peak;
1879                 vf->qs_bw[i].committed = qbw->cfg[i].shaper.committed;
1880                 vf->qs_bw[i].tc = qbw->cfg[i].tc;
1881         }
1882
1883         if (ice_vf_cfg_qs_bw(vf, qbw->num_queues))
1884                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1885
1886 err:
1887         /* send the response to the VF */
1888         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_QUEUE_BW,
1889                                     v_ret, NULL, 0);
1890 }
1891
1892 /**
1893  * ice_vc_cfg_q_quanta - Configure per queue quanta
1894  * @vf: pointer to the VF info
1895  * @msg: pointer to the msg buffer which holds the command descriptor
1896  *
1897  * Configure VF queues quanta.
1898  *
1899  * Return: 0 on success or negative error value.
1900  */
1901 static int ice_vc_cfg_q_quanta(struct ice_vf *vf, u8 *msg)
1902 {
1903         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1904         u16 quanta_prof_id, quanta_size, start_qid, end_qid, i;
1905         struct virtchnl_quanta_cfg *qquanta =
1906                 (struct virtchnl_quanta_cfg *)msg;
1907         struct ice_vsi *vsi;
1908         int ret;
1909
1910         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1911                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1912                 goto err;
1913         }
1914
1915         vsi = ice_get_vf_vsi(vf);
1916         if (!vsi) {
1917                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1918                 goto err;
1919         }
1920
1921         end_qid = qquanta->queue_select.start_queue_id +
1922                   qquanta->queue_select.num_queues;
1923         if (end_qid > ICE_MAX_RSS_QS_PER_VF ||
1924             end_qid > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
1925                 dev_err(ice_pf_to_dev(vf->pf), "VF-%d trying to configure more than allocated number of queues: %d\n",
1926                         vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
1927                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1928                 goto err;
1929         }
1930
1931         quanta_size = qquanta->quanta_size;
1932         if (quanta_size > ICE_MAX_QUANTA_SIZE ||
1933             quanta_size < ICE_MIN_QUANTA_SIZE) {
1934                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1935                 goto err;
1936         }
1937
1938         if (quanta_size % 64) {
1939                 dev_err(ice_pf_to_dev(vf->pf), "quanta size should be the product of 64\n");
1940                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1941                 goto err;
1942         }
1943
1944         ret = ice_vf_cfg_q_quanta_profile(vf, quanta_size,
1945                                           &quanta_prof_id);
1946         if (ret) {
1947                 v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
1948                 goto err;
1949         }
1950
1951         start_qid = qquanta->queue_select.start_queue_id;
1952         for (i = start_qid; i < end_qid; i++)
1953                 vsi->tx_rings[i]->quanta_prof_id = quanta_prof_id;
1954
1955 err:
1956         /* send the response to the VF */
1957         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_QUANTA,
1958                                      v_ret, NULL, 0);
1959 }
1960
1961 /**
1962  * ice_vc_cfg_qs_msg
1963  * @vf: pointer to the VF info
1964  * @msg: pointer to the msg buffer
1965  *
1966  * called from the VF to configure the Rx/Tx queues
1967  */
1968 static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
1969 {
1970         struct virtchnl_vsi_queue_config_info *qci =
1971             (struct virtchnl_vsi_queue_config_info *)msg;
1972         struct virtchnl_queue_pair_info *qpi;
1973         struct ice_pf *pf = vf->pf;
1974         struct ice_lag *lag;
1975         struct ice_vsi *vsi;
1976         u8 act_prt, pri_prt;
1977         int i = -1, q_idx;
1978
1979         lag = pf->lag;
1980         mutex_lock(&pf->lag_mutex);
1981         act_prt = ICE_LAG_INVALID_PORT;
1982         pri_prt = pf->hw.port_info->lport;
1983         if (lag && lag->bonded && lag->primary) {
1984                 act_prt = lag->active_port;
1985                 if (act_prt != pri_prt && act_prt != ICE_LAG_INVALID_PORT &&
1986                     lag->upper_netdev)
1987                         ice_lag_move_vf_nodes_cfg(lag, act_prt, pri_prt);
1988                 else
1989                         act_prt = ICE_LAG_INVALID_PORT;
1990         }
1991
1992         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
1993                 goto error_param;
1994
1995         if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id))
1996                 goto error_param;
1997
1998         vsi = ice_get_vf_vsi(vf);
1999         if (!vsi)
2000                 goto error_param;
2001
2002         if (qci->num_queue_pairs > ICE_MAX_RSS_QS_PER_VF ||
2003             qci->num_queue_pairs > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
2004                 dev_err(ice_pf_to_dev(pf), "VF-%d requesting more than supported number of queues: %d\n",
2005                         vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
2006                 goto error_param;
2007         }
2008
2009         for (i = 0; i < qci->num_queue_pairs; i++) {
2010                 if (!qci->qpair[i].rxq.crc_disable)
2011                         continue;
2012
2013                 if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_CRC) ||
2014                     vf->vlan_strip_ena)
2015                         goto error_param;
2016         }
2017
2018         for (i = 0; i < qci->num_queue_pairs; i++) {
2019                 qpi = &qci->qpair[i];
2020                 if (qpi->txq.vsi_id != qci->vsi_id ||
2021                     qpi->rxq.vsi_id != qci->vsi_id ||
2022                     qpi->rxq.queue_id != qpi->txq.queue_id ||
2023                     qpi->txq.headwb_enabled ||
2024                     !ice_vc_isvalid_ring_len(qpi->txq.ring_len) ||
2025                     !ice_vc_isvalid_ring_len(qpi->rxq.ring_len) ||
2026                     !ice_vc_isvalid_q_id(vsi, qpi->txq.queue_id)) {
2027                         goto error_param;
2028                 }
2029
2030                 q_idx = qpi->rxq.queue_id;
2031
2032                 /* make sure selected "q_idx" is in valid range of queues
2033                  * for selected "vsi"
2034                  */
2035                 if (q_idx >= vsi->alloc_txq || q_idx >= vsi->alloc_rxq) {
2036                         goto error_param;
2037                 }
2038
2039                 /* copy Tx queue info from VF into VSI */
2040                 if (qpi->txq.ring_len > 0) {
2041                         vsi->tx_rings[q_idx]->dma = qpi->txq.dma_ring_addr;
2042                         vsi->tx_rings[q_idx]->count = qpi->txq.ring_len;
2043
2044                         /* Disable any existing queue first */
2045                         if (ice_vf_vsi_dis_single_txq(vf, vsi, q_idx))
2046                                 goto error_param;
2047
2048                         /* Configure a queue with the requested settings */
2049                         if (ice_vsi_cfg_single_txq(vsi, vsi->tx_rings, q_idx)) {
2050                                 dev_warn(ice_pf_to_dev(pf), "VF-%d failed to configure TX queue %d\n",
2051                                          vf->vf_id, q_idx);
2052                                 goto error_param;
2053                         }
2054                 }
2055
2056                 /* copy Rx queue info from VF into VSI */
2057                 if (qpi->rxq.ring_len > 0) {
2058                         u16 max_frame_size = ice_vc_get_max_frame_size(vf);
2059                         struct ice_rx_ring *ring = vsi->rx_rings[q_idx];
2060                         u32 rxdid;
2061
2062                         ring->dma = qpi->rxq.dma_ring_addr;
2063                         ring->count = qpi->rxq.ring_len;
2064
2065                         if (qpi->rxq.crc_disable)
2066                                 ring->flags |= ICE_RX_FLAGS_CRC_STRIP_DIS;
2067                         else
2068                                 ring->flags &= ~ICE_RX_FLAGS_CRC_STRIP_DIS;
2069
2070                         if (qpi->rxq.databuffer_size != 0 &&
2071                             (qpi->rxq.databuffer_size > ((16 * 1024) - 128) ||
2072                              qpi->rxq.databuffer_size < 1024))
2073                                 goto error_param;
2074                         ring->rx_buf_len = qpi->rxq.databuffer_size;
2075                         if (qpi->rxq.max_pkt_size > max_frame_size ||
2076                             qpi->rxq.max_pkt_size < 64)
2077                                 goto error_param;
2078
2079                         ring->max_frame = qpi->rxq.max_pkt_size;
2080                         /* add space for the port VLAN since the VF driver is
2081                          * not expected to account for it in the MTU
2082                          * calculation
2083                          */
2084                         if (ice_vf_is_port_vlan_ena(vf))
2085                                 ring->max_frame += VLAN_HLEN;
2086
2087                         if (ice_vsi_cfg_single_rxq(vsi, q_idx)) {
2088                                 dev_warn(ice_pf_to_dev(pf), "VF-%d failed to configure RX queue %d\n",
2089                                          vf->vf_id, q_idx);
2090                                 goto error_param;
2091                         }
2092
2093                         /* If Rx flex desc is supported, select RXDID for Rx
2094                          * queues. Otherwise, use legacy 32byte descriptor
2095                          * format. Legacy 16byte descriptor is not supported.
2096                          * If this RXDID is selected, return error.
2097                          */
2098                         if (vf->driver_caps &
2099                             VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC) {
2100                                 rxdid = qpi->rxq.rxdid;
2101                                 if (!(BIT(rxdid) & pf->supported_rxdids))
2102                                         goto error_param;
2103                         } else {
2104                                 rxdid = ICE_RXDID_LEGACY_1;
2105                         }
2106
2107                         ice_write_qrxflxp_cntxt(&vsi->back->hw,
2108                                                 vsi->rxq_map[q_idx],
2109                                                 rxdid, 0x03, false);
2110                 }
2111         }
2112
2113         if (lag && lag->bonded && lag->primary &&
2114             act_prt != ICE_LAG_INVALID_PORT)
2115                 ice_lag_move_vf_nodes_cfg(lag, pri_prt, act_prt);
2116         mutex_unlock(&pf->lag_mutex);
2117
2118         /* send the response to the VF */
2119         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
2120                                      VIRTCHNL_STATUS_SUCCESS, NULL, 0);
2121 error_param:
2122         /* disable whatever we can */
2123         for (; i >= 0; i--) {
2124                 if (ice_vsi_ctrl_one_rx_ring(vsi, false, i, true))
2125                         dev_err(ice_pf_to_dev(pf), "VF-%d could not disable RX queue %d\n",
2126                                 vf->vf_id, i);
2127                 if (ice_vf_vsi_dis_single_txq(vf, vsi, i))
2128                         dev_err(ice_pf_to_dev(pf), "VF-%d could not disable TX queue %d\n",
2129                                 vf->vf_id, i);
2130         }
2131
2132         if (lag && lag->bonded && lag->primary &&
2133             act_prt != ICE_LAG_INVALID_PORT)
2134                 ice_lag_move_vf_nodes_cfg(lag, pri_prt, act_prt);
2135         mutex_unlock(&pf->lag_mutex);
2136
2137         ice_lag_move_new_vf_nodes(vf);
2138
2139         /* send the response to the VF */
2140         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
2141                                      VIRTCHNL_STATUS_ERR_PARAM, NULL, 0);
2142 }
2143
2144 /**
2145  * ice_can_vf_change_mac
2146  * @vf: pointer to the VF info
2147  *
2148  * Return true if the VF is allowed to change its MAC filters, false otherwise
2149  */
2150 static bool ice_can_vf_change_mac(struct ice_vf *vf)
2151 {
2152         /* If the VF MAC address has been set administratively (via the
2153          * ndo_set_vf_mac command), then deny permission to the VF to
2154          * add/delete unicast MAC addresses, unless the VF is trusted
2155          */
2156         if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
2157                 return false;
2158
2159         return true;
2160 }
2161
2162 /**
2163  * ice_vc_ether_addr_type - get type of virtchnl_ether_addr
2164  * @vc_ether_addr: used to extract the type
2165  */
2166 static u8
2167 ice_vc_ether_addr_type(struct virtchnl_ether_addr *vc_ether_addr)
2168 {
2169         return (vc_ether_addr->type & VIRTCHNL_ETHER_ADDR_TYPE_MASK);
2170 }
2171
2172 /**
2173  * ice_is_vc_addr_legacy - check if the MAC address is from an older VF
2174  * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
2175  */
2176 static bool
2177 ice_is_vc_addr_legacy(struct virtchnl_ether_addr *vc_ether_addr)
2178 {
2179         u8 type = ice_vc_ether_addr_type(vc_ether_addr);
2180
2181         return (type == VIRTCHNL_ETHER_ADDR_LEGACY);
2182 }
2183
2184 /**
2185  * ice_is_vc_addr_primary - check if the MAC address is the VF's primary MAC
2186  * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
2187  *
2188  * This function should only be called when the MAC address in
2189  * virtchnl_ether_addr is a valid unicast MAC
2190  */
2191 static bool
2192 ice_is_vc_addr_primary(struct virtchnl_ether_addr __maybe_unused *vc_ether_addr)
2193 {
2194         u8 type = ice_vc_ether_addr_type(vc_ether_addr);
2195
2196         return (type == VIRTCHNL_ETHER_ADDR_PRIMARY);
2197 }
2198
2199 /**
2200  * ice_vfhw_mac_add - update the VF's cached hardware MAC if allowed
2201  * @vf: VF to update
2202  * @vc_ether_addr: structure from VIRTCHNL with MAC to add
2203  */
2204 static void
2205 ice_vfhw_mac_add(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
2206 {
2207         u8 *mac_addr = vc_ether_addr->addr;
2208
2209         if (!is_valid_ether_addr(mac_addr))
2210                 return;
2211
2212         /* only allow legacy VF drivers to set the device and hardware MAC if it
2213          * is zero and allow new VF drivers to set the hardware MAC if the type
2214          * was correctly specified over VIRTCHNL
2215          */
2216         if ((ice_is_vc_addr_legacy(vc_ether_addr) &&
2217              is_zero_ether_addr(vf->hw_lan_addr)) ||
2218             ice_is_vc_addr_primary(vc_ether_addr)) {
2219                 ether_addr_copy(vf->dev_lan_addr, mac_addr);
2220                 ether_addr_copy(vf->hw_lan_addr, mac_addr);
2221         }
2222
2223         /* hardware and device MACs are already set, but its possible that the
2224          * VF driver sent the VIRTCHNL_OP_ADD_ETH_ADDR message before the
2225          * VIRTCHNL_OP_DEL_ETH_ADDR when trying to update its MAC, so save it
2226          * away for the legacy VF driver case as it will be updated in the
2227          * delete flow for this case
2228          */
2229         if (ice_is_vc_addr_legacy(vc_ether_addr)) {
2230                 ether_addr_copy(vf->legacy_last_added_umac.addr,
2231                                 mac_addr);
2232                 vf->legacy_last_added_umac.time_modified = jiffies;
2233         }
2234 }
2235
2236 /**
2237  * ice_vc_add_mac_addr - attempt to add the MAC address passed in
2238  * @vf: pointer to the VF info
2239  * @vsi: pointer to the VF's VSI
2240  * @vc_ether_addr: VIRTCHNL MAC address structure used to add MAC
2241  */
2242 static int
2243 ice_vc_add_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
2244                     struct virtchnl_ether_addr *vc_ether_addr)
2245 {
2246         struct device *dev = ice_pf_to_dev(vf->pf);
2247         u8 *mac_addr = vc_ether_addr->addr;
2248         int ret;
2249
2250         /* device MAC already added */
2251         if (ether_addr_equal(mac_addr, vf->dev_lan_addr))
2252                 return 0;
2253
2254         if (is_unicast_ether_addr(mac_addr) && !ice_can_vf_change_mac(vf)) {
2255                 dev_err(dev, "VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
2256                 return -EPERM;
2257         }
2258
2259         ret = ice_fltr_add_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
2260         if (ret == -EEXIST) {
2261                 dev_dbg(dev, "MAC %pM already exists for VF %d\n", mac_addr,
2262                         vf->vf_id);
2263                 /* don't return since we might need to update
2264                  * the primary MAC in ice_vfhw_mac_add() below
2265                  */
2266         } else if (ret) {
2267                 dev_err(dev, "Failed to add MAC %pM for VF %d\n, error %d\n",
2268                         mac_addr, vf->vf_id, ret);
2269                 return ret;
2270         } else {
2271                 vf->num_mac++;
2272         }
2273
2274         ice_vfhw_mac_add(vf, vc_ether_addr);
2275
2276         return ret;
2277 }
2278
2279 /**
2280  * ice_is_legacy_umac_expired - check if last added legacy unicast MAC expired
2281  * @last_added_umac: structure used to check expiration
2282  */
2283 static bool ice_is_legacy_umac_expired(struct ice_time_mac *last_added_umac)
2284 {
2285 #define ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME    msecs_to_jiffies(3000)
2286         return time_is_before_jiffies(last_added_umac->time_modified +
2287                                       ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME);
2288 }
2289
2290 /**
2291  * ice_update_legacy_cached_mac - update cached hardware MAC for legacy VF
2292  * @vf: VF to update
2293  * @vc_ether_addr: structure from VIRTCHNL with MAC to check
2294  *
2295  * only update cached hardware MAC for legacy VF drivers on delete
2296  * because we cannot guarantee order/type of MAC from the VF driver
2297  */
2298 static void
2299 ice_update_legacy_cached_mac(struct ice_vf *vf,
2300                              struct virtchnl_ether_addr *vc_ether_addr)
2301 {
2302         if (!ice_is_vc_addr_legacy(vc_ether_addr) ||
2303             ice_is_legacy_umac_expired(&vf->legacy_last_added_umac))
2304                 return;
2305
2306         ether_addr_copy(vf->dev_lan_addr, vf->legacy_last_added_umac.addr);
2307         ether_addr_copy(vf->hw_lan_addr, vf->legacy_last_added_umac.addr);
2308 }
2309
2310 /**
2311  * ice_vfhw_mac_del - update the VF's cached hardware MAC if allowed
2312  * @vf: VF to update
2313  * @vc_ether_addr: structure from VIRTCHNL with MAC to delete
2314  */
2315 static void
2316 ice_vfhw_mac_del(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
2317 {
2318         u8 *mac_addr = vc_ether_addr->addr;
2319
2320         if (!is_valid_ether_addr(mac_addr) ||
2321             !ether_addr_equal(vf->dev_lan_addr, mac_addr))
2322                 return;
2323
2324         /* allow the device MAC to be repopulated in the add flow and don't
2325          * clear the hardware MAC (i.e. hw_lan_addr) here as that is meant
2326          * to be persistent on VM reboot and across driver unload/load, which
2327          * won't work if we clear the hardware MAC here
2328          */
2329         eth_zero_addr(vf->dev_lan_addr);
2330
2331         ice_update_legacy_cached_mac(vf, vc_ether_addr);
2332 }
2333
2334 /**
2335  * ice_vc_del_mac_addr - attempt to delete the MAC address passed in
2336  * @vf: pointer to the VF info
2337  * @vsi: pointer to the VF's VSI
2338  * @vc_ether_addr: VIRTCHNL MAC address structure used to delete MAC
2339  */
2340 static int
2341 ice_vc_del_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
2342                     struct virtchnl_ether_addr *vc_ether_addr)
2343 {
2344         struct device *dev = ice_pf_to_dev(vf->pf);
2345         u8 *mac_addr = vc_ether_addr->addr;
2346         int status;
2347
2348         if (!ice_can_vf_change_mac(vf) &&
2349             ether_addr_equal(vf->dev_lan_addr, mac_addr))
2350                 return 0;
2351
2352         status = ice_fltr_remove_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
2353         if (status == -ENOENT) {
2354                 dev_err(dev, "MAC %pM does not exist for VF %d\n", mac_addr,
2355                         vf->vf_id);
2356                 return -ENOENT;
2357         } else if (status) {
2358                 dev_err(dev, "Failed to delete MAC %pM for VF %d, error %d\n",
2359                         mac_addr, vf->vf_id, status);
2360                 return -EIO;
2361         }
2362
2363         ice_vfhw_mac_del(vf, vc_ether_addr);
2364
2365         vf->num_mac--;
2366
2367         return 0;
2368 }
2369
2370 /**
2371  * ice_vc_handle_mac_addr_msg
2372  * @vf: pointer to the VF info
2373  * @msg: pointer to the msg buffer
2374  * @set: true if MAC filters are being set, false otherwise
2375  *
2376  * add guest MAC address filter
2377  */
2378 static int
2379 ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
2380 {
2381         int (*ice_vc_cfg_mac)
2382                 (struct ice_vf *vf, struct ice_vsi *vsi,
2383                  struct virtchnl_ether_addr *virtchnl_ether_addr);
2384         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2385         struct virtchnl_ether_addr_list *al =
2386             (struct virtchnl_ether_addr_list *)msg;
2387         struct ice_pf *pf = vf->pf;
2388         enum virtchnl_ops vc_op;
2389         struct ice_vsi *vsi;
2390         int i;
2391
2392         if (set) {
2393                 vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
2394                 ice_vc_cfg_mac = ice_vc_add_mac_addr;
2395         } else {
2396                 vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
2397                 ice_vc_cfg_mac = ice_vc_del_mac_addr;
2398         }
2399
2400         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
2401             !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
2402                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2403                 goto handle_mac_exit;
2404         }
2405
2406         /* If this VF is not privileged, then we can't add more than a
2407          * limited number of addresses. Check to make sure that the
2408          * additions do not push us over the limit.
2409          */
2410         if (set && !ice_is_vf_trusted(vf) &&
2411             (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
2412                 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",
2413                         vf->vf_id);
2414                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2415                 goto handle_mac_exit;
2416         }
2417
2418         vsi = ice_get_vf_vsi(vf);
2419         if (!vsi) {
2420                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2421                 goto handle_mac_exit;
2422         }
2423
2424         for (i = 0; i < al->num_elements; i++) {
2425                 u8 *mac_addr = al->list[i].addr;
2426                 int result;
2427
2428                 if (is_broadcast_ether_addr(mac_addr) ||
2429                     is_zero_ether_addr(mac_addr))
2430                         continue;
2431
2432                 result = ice_vc_cfg_mac(vf, vsi, &al->list[i]);
2433                 if (result == -EEXIST || result == -ENOENT) {
2434                         continue;
2435                 } else if (result) {
2436                         v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2437                         goto handle_mac_exit;
2438                 }
2439         }
2440
2441 handle_mac_exit:
2442         /* send the response to the VF */
2443         return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
2444 }
2445
2446 /**
2447  * ice_vc_add_mac_addr_msg
2448  * @vf: pointer to the VF info
2449  * @msg: pointer to the msg buffer
2450  *
2451  * add guest MAC address filter
2452  */
2453 static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2454 {
2455         return ice_vc_handle_mac_addr_msg(vf, msg, true);
2456 }
2457
2458 /**
2459  * ice_vc_del_mac_addr_msg
2460  * @vf: pointer to the VF info
2461  * @msg: pointer to the msg buffer
2462  *
2463  * remove guest MAC address filter
2464  */
2465 static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2466 {
2467         return ice_vc_handle_mac_addr_msg(vf, msg, false);
2468 }
2469
2470 /**
2471  * ice_vc_request_qs_msg
2472  * @vf: pointer to the VF info
2473  * @msg: pointer to the msg buffer
2474  *
2475  * VFs get a default number of queues but can use this message to request a
2476  * different number. If the request is successful, PF will reset the VF and
2477  * return 0. If unsuccessful, PF will send message informing VF of number of
2478  * available queue pairs via virtchnl message response to VF.
2479  */
2480 static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
2481 {
2482         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2483         struct virtchnl_vf_res_request *vfres =
2484                 (struct virtchnl_vf_res_request *)msg;
2485         u16 req_queues = vfres->num_queue_pairs;
2486         struct ice_pf *pf = vf->pf;
2487         u16 max_allowed_vf_queues;
2488         u16 tx_rx_queue_left;
2489         struct device *dev;
2490         u16 cur_queues;
2491
2492         dev = ice_pf_to_dev(pf);
2493         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2494                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2495                 goto error_param;
2496         }
2497
2498         cur_queues = vf->num_vf_qs;
2499         tx_rx_queue_left = min_t(u16, ice_get_avail_txq_count(pf),
2500                                  ice_get_avail_rxq_count(pf));
2501         max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
2502         if (!req_queues) {
2503                 dev_err(dev, "VF %d tried to request 0 queues. Ignoring.\n",
2504                         vf->vf_id);
2505         } else if (req_queues > ICE_MAX_RSS_QS_PER_VF) {
2506                 dev_err(dev, "VF %d tried to request more than %d queues.\n",
2507                         vf->vf_id, ICE_MAX_RSS_QS_PER_VF);
2508                 vfres->num_queue_pairs = ICE_MAX_RSS_QS_PER_VF;
2509         } else if (req_queues > cur_queues &&
2510                    req_queues - cur_queues > tx_rx_queue_left) {
2511                 dev_warn(dev, "VF %d requested %u more queues, but only %u left.\n",
2512                          vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
2513                 vfres->num_queue_pairs = min_t(u16, max_allowed_vf_queues,
2514                                                ICE_MAX_RSS_QS_PER_VF);
2515         } else {
2516                 /* request is successful, then reset VF */
2517                 vf->num_req_qs = req_queues;
2518                 ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
2519                 dev_info(dev, "VF %d granted request of %u queues.\n",
2520                          vf->vf_id, req_queues);
2521                 return 0;
2522         }
2523
2524 error_param:
2525         /* send the response to the VF */
2526         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
2527                                      v_ret, (u8 *)vfres, sizeof(*vfres));
2528 }
2529
2530 /**
2531  * ice_vf_vlan_offload_ena - determine if capabilities support VLAN offloads
2532  * @caps: VF driver negotiated capabilities
2533  *
2534  * Return true if VIRTCHNL_VF_OFFLOAD_VLAN capability is set, else return false
2535  */
2536 static bool ice_vf_vlan_offload_ena(u32 caps)
2537 {
2538         return !!(caps & VIRTCHNL_VF_OFFLOAD_VLAN);
2539 }
2540
2541 /**
2542  * ice_is_vlan_promisc_allowed - check if VLAN promiscuous config is allowed
2543  * @vf: VF used to determine if VLAN promiscuous config is allowed
2544  */
2545 static bool ice_is_vlan_promisc_allowed(struct ice_vf *vf)
2546 {
2547         if ((test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2548              test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) &&
2549             test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, vf->pf->flags))
2550                 return true;
2551
2552         return false;
2553 }
2554
2555 /**
2556  * ice_vf_ena_vlan_promisc - Enable Tx/Rx VLAN promiscuous for the VLAN
2557  * @vf: VF to enable VLAN promisc on
2558  * @vsi: VF's VSI used to enable VLAN promiscuous mode
2559  * @vlan: VLAN used to enable VLAN promiscuous
2560  *
2561  * This function should only be called if VLAN promiscuous mode is allowed,
2562  * which can be determined via ice_is_vlan_promisc_allowed().
2563  */
2564 static int ice_vf_ena_vlan_promisc(struct ice_vf *vf, struct ice_vsi *vsi,
2565                                    struct ice_vlan *vlan)
2566 {
2567         u8 promisc_m = 0;
2568         int status;
2569
2570         if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
2571                 promisc_m |= ICE_UCAST_VLAN_PROMISC_BITS;
2572         if (test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
2573                 promisc_m |= ICE_MCAST_VLAN_PROMISC_BITS;
2574
2575         if (!promisc_m)
2576                 return 0;
2577
2578         status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m,
2579                                           vlan->vid);
2580         if (status && status != -EEXIST)
2581                 return status;
2582
2583         return 0;
2584 }
2585
2586 /**
2587  * ice_vf_dis_vlan_promisc - Disable Tx/Rx VLAN promiscuous for the VLAN
2588  * @vsi: VF's VSI used to disable VLAN promiscuous mode for
2589  * @vlan: VLAN used to disable VLAN promiscuous
2590  *
2591  * This function should only be called if VLAN promiscuous mode is allowed,
2592  * which can be determined via ice_is_vlan_promisc_allowed().
2593  */
2594 static int ice_vf_dis_vlan_promisc(struct ice_vsi *vsi, struct ice_vlan *vlan)
2595 {
2596         u8 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS | ICE_MCAST_VLAN_PROMISC_BITS;
2597         int status;
2598
2599         status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m,
2600                                             vlan->vid);
2601         if (status && status != -ENOENT)
2602                 return status;
2603
2604         return 0;
2605 }
2606
2607 /**
2608  * ice_vf_has_max_vlans - check if VF already has the max allowed VLAN filters
2609  * @vf: VF to check against
2610  * @vsi: VF's VSI
2611  *
2612  * If the VF is trusted then the VF is allowed to add as many VLANs as it
2613  * wants to, so return false.
2614  *
2615  * When the VF is untrusted compare the number of non-zero VLANs + 1 to the max
2616  * allowed VLANs for an untrusted VF. Return the result of this comparison.
2617  */
2618 static bool ice_vf_has_max_vlans(struct ice_vf *vf, struct ice_vsi *vsi)
2619 {
2620         if (ice_is_vf_trusted(vf))
2621                 return false;
2622
2623 #define ICE_VF_ADDED_VLAN_ZERO_FLTRS    1
2624         return ((ice_vsi_num_non_zero_vlans(vsi) +
2625                 ICE_VF_ADDED_VLAN_ZERO_FLTRS) >= ICE_MAX_VLAN_PER_VF);
2626 }
2627
2628 /**
2629  * ice_vc_process_vlan_msg
2630  * @vf: pointer to the VF info
2631  * @msg: pointer to the msg buffer
2632  * @add_v: Add VLAN if true, otherwise delete VLAN
2633  *
2634  * Process virtchnl op to add or remove programmed guest VLAN ID
2635  */
2636 static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
2637 {
2638         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2639         struct virtchnl_vlan_filter_list *vfl =
2640             (struct virtchnl_vlan_filter_list *)msg;
2641         struct ice_pf *pf = vf->pf;
2642         bool vlan_promisc = false;
2643         struct ice_vsi *vsi;
2644         struct device *dev;
2645         int status = 0;
2646         int i;
2647
2648         dev = ice_pf_to_dev(pf);
2649         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2650                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2651                 goto error_param;
2652         }
2653
2654         if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2655                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2656                 goto error_param;
2657         }
2658
2659         if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
2660                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2661                 goto error_param;
2662         }
2663
2664         for (i = 0; i < vfl->num_elements; i++) {
2665                 if (vfl->vlan_id[i] >= VLAN_N_VID) {
2666                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2667                         dev_err(dev, "invalid VF VLAN id %d\n",
2668                                 vfl->vlan_id[i]);
2669                         goto error_param;
2670                 }
2671         }
2672
2673         vsi = ice_get_vf_vsi(vf);
2674         if (!vsi) {
2675                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2676                 goto error_param;
2677         }
2678
2679         if (add_v && ice_vf_has_max_vlans(vf, vsi)) {
2680                 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2681                          vf->vf_id);
2682                 /* There is no need to let VF know about being not trusted,
2683                  * so we can just return success message here
2684                  */
2685                 goto error_param;
2686         }
2687
2688         /* in DVM a VF can add/delete inner VLAN filters when
2689          * VIRTCHNL_VF_OFFLOAD_VLAN is negotiated, so only reject in SVM
2690          */
2691         if (ice_vf_is_port_vlan_ena(vf) && !ice_is_dvm_ena(&pf->hw)) {
2692                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2693                 goto error_param;
2694         }
2695
2696         /* in DVM VLAN promiscuous is based on the outer VLAN, which would be
2697          * the port VLAN if VIRTCHNL_VF_OFFLOAD_VLAN was negotiated, so only
2698          * allow vlan_promisc = true in SVM and if no port VLAN is configured
2699          */
2700         vlan_promisc = ice_is_vlan_promisc_allowed(vf) &&
2701                 !ice_is_dvm_ena(&pf->hw) &&
2702                 !ice_vf_is_port_vlan_ena(vf);
2703
2704         if (add_v) {
2705                 for (i = 0; i < vfl->num_elements; i++) {
2706                         u16 vid = vfl->vlan_id[i];
2707                         struct ice_vlan vlan;
2708
2709                         if (ice_vf_has_max_vlans(vf, vsi)) {
2710                                 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2711                                          vf->vf_id);
2712                                 /* There is no need to let VF know about being
2713                                  * not trusted, so we can just return success
2714                                  * message here as well.
2715                                  */
2716                                 goto error_param;
2717                         }
2718
2719                         /* we add VLAN 0 by default for each VF so we can enable
2720                          * Tx VLAN anti-spoof without triggering MDD events so
2721                          * we don't need to add it again here
2722                          */
2723                         if (!vid)
2724                                 continue;
2725
2726                         vlan = ICE_VLAN(ETH_P_8021Q, vid, 0);
2727                         status = vsi->inner_vlan_ops.add_vlan(vsi, &vlan);
2728                         if (status) {
2729                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2730                                 goto error_param;
2731                         }
2732
2733                         /* Enable VLAN filtering on first non-zero VLAN */
2734                         if (!vlan_promisc && vid && !ice_is_dvm_ena(&pf->hw)) {
2735                                 if (vf->spoofchk) {
2736                                         status = vsi->inner_vlan_ops.ena_tx_filtering(vsi);
2737                                         if (status) {
2738                                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2739                                                 dev_err(dev, "Enable VLAN anti-spoofing on VLAN ID: %d failed error-%d\n",
2740                                                         vid, status);
2741                                                 goto error_param;
2742                                         }
2743                                 }
2744                                 if (vsi->inner_vlan_ops.ena_rx_filtering(vsi)) {
2745                                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2746                                         dev_err(dev, "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
2747                                                 vid, status);
2748                                         goto error_param;
2749                                 }
2750                         } else if (vlan_promisc) {
2751                                 status = ice_vf_ena_vlan_promisc(vf, vsi, &vlan);
2752                                 if (status) {
2753                                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2754                                         dev_err(dev, "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
2755                                                 vid, status);
2756                                 }
2757                         }
2758                 }
2759         } else {
2760                 /* In case of non_trusted VF, number of VLAN elements passed
2761                  * to PF for removal might be greater than number of VLANs
2762                  * filter programmed for that VF - So, use actual number of
2763                  * VLANS added earlier with add VLAN opcode. In order to avoid
2764                  * removing VLAN that doesn't exist, which result to sending
2765                  * erroneous failed message back to the VF
2766                  */
2767                 int num_vf_vlan;
2768
2769                 num_vf_vlan = vsi->num_vlan;
2770                 for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
2771                         u16 vid = vfl->vlan_id[i];
2772                         struct ice_vlan vlan;
2773
2774                         /* we add VLAN 0 by default for each VF so we can enable
2775                          * Tx VLAN anti-spoof without triggering MDD events so
2776                          * we don't want a VIRTCHNL request to remove it
2777                          */
2778                         if (!vid)
2779                                 continue;
2780
2781                         vlan = ICE_VLAN(ETH_P_8021Q, vid, 0);
2782                         status = vsi->inner_vlan_ops.del_vlan(vsi, &vlan);
2783                         if (status) {
2784                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2785                                 goto error_param;
2786                         }
2787
2788                         /* Disable VLAN filtering when only VLAN 0 is left */
2789                         if (!ice_vsi_has_non_zero_vlans(vsi)) {
2790                                 vsi->inner_vlan_ops.dis_tx_filtering(vsi);
2791                                 vsi->inner_vlan_ops.dis_rx_filtering(vsi);
2792                         }
2793
2794                         if (vlan_promisc)
2795                                 ice_vf_dis_vlan_promisc(vsi, &vlan);
2796                 }
2797         }
2798
2799 error_param:
2800         /* send the response to the VF */
2801         if (add_v)
2802                 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
2803                                              NULL, 0);
2804         else
2805                 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
2806                                              NULL, 0);
2807 }
2808
2809 /**
2810  * ice_vc_add_vlan_msg
2811  * @vf: pointer to the VF info
2812  * @msg: pointer to the msg buffer
2813  *
2814  * Add and program guest VLAN ID
2815  */
2816 static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
2817 {
2818         return ice_vc_process_vlan_msg(vf, msg, true);
2819 }
2820
2821 /**
2822  * ice_vc_remove_vlan_msg
2823  * @vf: pointer to the VF info
2824  * @msg: pointer to the msg buffer
2825  *
2826  * remove programmed guest VLAN ID
2827  */
2828 static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
2829 {
2830         return ice_vc_process_vlan_msg(vf, msg, false);
2831 }
2832
2833 /**
2834  * ice_vsi_is_rxq_crc_strip_dis - check if Rx queue CRC strip is disabled or not
2835  * @vsi: pointer to the VF VSI info
2836  */
2837 static bool ice_vsi_is_rxq_crc_strip_dis(struct ice_vsi *vsi)
2838 {
2839         unsigned int i;
2840
2841         ice_for_each_alloc_rxq(vsi, i)
2842                 if (vsi->rx_rings[i]->flags & ICE_RX_FLAGS_CRC_STRIP_DIS)
2843                         return true;
2844
2845         return false;
2846 }
2847
2848 /**
2849  * ice_vc_ena_vlan_stripping
2850  * @vf: pointer to the VF info
2851  *
2852  * Enable VLAN header stripping for a given VF
2853  */
2854 static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
2855 {
2856         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2857         struct ice_vsi *vsi;
2858
2859         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2860                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2861                 goto error_param;
2862         }
2863
2864         if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2865                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2866                 goto error_param;
2867         }
2868
2869         vsi = ice_get_vf_vsi(vf);
2870         if (!vsi) {
2871                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2872                 goto error_param;
2873         }
2874
2875         if (vsi->inner_vlan_ops.ena_stripping(vsi, ETH_P_8021Q))
2876                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2877         else
2878                 vf->vlan_strip_ena |= ICE_INNER_VLAN_STRIP_ENA;
2879
2880 error_param:
2881         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
2882                                      v_ret, NULL, 0);
2883 }
2884
2885 /**
2886  * ice_vc_dis_vlan_stripping
2887  * @vf: pointer to the VF info
2888  *
2889  * Disable VLAN header stripping for a given VF
2890  */
2891 static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
2892 {
2893         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2894         struct ice_vsi *vsi;
2895
2896         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2897                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2898                 goto error_param;
2899         }
2900
2901         if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2902                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2903                 goto error_param;
2904         }
2905
2906         vsi = ice_get_vf_vsi(vf);
2907         if (!vsi) {
2908                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2909                 goto error_param;
2910         }
2911
2912         if (vsi->inner_vlan_ops.dis_stripping(vsi))
2913                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2914         else
2915                 vf->vlan_strip_ena &= ~ICE_INNER_VLAN_STRIP_ENA;
2916
2917 error_param:
2918         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
2919                                      v_ret, NULL, 0);
2920 }
2921
2922 /**
2923  * ice_vc_get_rss_hena - return the RSS HENA bits allowed by the hardware
2924  * @vf: pointer to the VF info
2925  */
2926 static int ice_vc_get_rss_hena(struct ice_vf *vf)
2927 {
2928         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2929         struct virtchnl_rss_hena *vrh = NULL;
2930         int len = 0, ret;
2931
2932         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2933                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2934                 goto err;
2935         }
2936
2937         if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2938                 dev_err(ice_pf_to_dev(vf->pf), "RSS not supported by PF\n");
2939                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2940                 goto err;
2941         }
2942
2943         len = sizeof(struct virtchnl_rss_hena);
2944         vrh = kzalloc(len, GFP_KERNEL);
2945         if (!vrh) {
2946                 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2947                 len = 0;
2948                 goto err;
2949         }
2950
2951         vrh->hena = ICE_DEFAULT_RSS_HENA;
2952 err:
2953         /* send the response back to the VF */
2954         ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS, v_ret,
2955                                     (u8 *)vrh, len);
2956         kfree(vrh);
2957         return ret;
2958 }
2959
2960 /**
2961  * ice_vc_set_rss_hena - set RSS HENA bits for the VF
2962  * @vf: pointer to the VF info
2963  * @msg: pointer to the msg buffer
2964  */
2965 static int ice_vc_set_rss_hena(struct ice_vf *vf, u8 *msg)
2966 {
2967         struct virtchnl_rss_hena *vrh = (struct virtchnl_rss_hena *)msg;
2968         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2969         struct ice_pf *pf = vf->pf;
2970         struct ice_vsi *vsi;
2971         struct device *dev;
2972         int status;
2973
2974         dev = ice_pf_to_dev(pf);
2975
2976         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2977                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2978                 goto err;
2979         }
2980
2981         if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2982                 dev_err(dev, "RSS not supported by PF\n");
2983                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2984                 goto err;
2985         }
2986
2987         vsi = ice_get_vf_vsi(vf);
2988         if (!vsi) {
2989                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2990                 goto err;
2991         }
2992
2993         /* clear all previously programmed RSS configuration to allow VF drivers
2994          * the ability to customize the RSS configuration and/or completely
2995          * disable RSS
2996          */
2997         status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
2998         if (status && !vrh->hena) {
2999                 /* only report failure to clear the current RSS configuration if
3000                  * that was clearly the VF's intention (i.e. vrh->hena = 0)
3001                  */
3002                 v_ret = ice_err_to_virt_err(status);
3003                 goto err;
3004         } else if (status) {
3005                 /* allow the VF to update the RSS configuration even on failure
3006                  * to clear the current RSS confguration in an attempt to keep
3007                  * RSS in a working state
3008                  */
3009                 dev_warn(dev, "Failed to clear the RSS configuration for VF %u\n",
3010                          vf->vf_id);
3011         }
3012
3013         if (vrh->hena) {
3014                 status = ice_add_avf_rss_cfg(&pf->hw, vsi, vrh->hena);
3015                 v_ret = ice_err_to_virt_err(status);
3016         }
3017
3018         /* send the response to the VF */
3019 err:
3020         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, v_ret,
3021                                      NULL, 0);
3022 }
3023
3024 /**
3025  * ice_vc_query_rxdid - query RXDID supported by DDP package
3026  * @vf: pointer to VF info
3027  *
3028  * Called from VF to query a bitmap of supported flexible
3029  * descriptor RXDIDs of a DDP package.
3030  */
3031 static int ice_vc_query_rxdid(struct ice_vf *vf)
3032 {
3033         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3034         struct virtchnl_supported_rxdids rxdid = {};
3035         struct ice_pf *pf = vf->pf;
3036
3037         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3038                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3039                 goto err;
3040         }
3041
3042         if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC)) {
3043                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3044                 goto err;
3045         }
3046
3047         rxdid.supported_rxdids = pf->supported_rxdids;
3048
3049 err:
3050         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_SUPPORTED_RXDIDS,
3051                                      v_ret, (u8 *)&rxdid, sizeof(rxdid));
3052 }
3053
3054 /**
3055  * ice_vf_init_vlan_stripping - enable/disable VLAN stripping on initialization
3056  * @vf: VF to enable/disable VLAN stripping for on initialization
3057  *
3058  * Set the default for VLAN stripping based on whether a port VLAN is configured
3059  * and the current VLAN mode of the device.
3060  */
3061 static int ice_vf_init_vlan_stripping(struct ice_vf *vf)
3062 {
3063         struct ice_vsi *vsi = ice_get_vf_vsi(vf);
3064
3065         vf->vlan_strip_ena = 0;
3066
3067         if (!vsi)
3068                 return -EINVAL;
3069
3070         /* don't modify stripping if port VLAN is configured in SVM since the
3071          * port VLAN is based on the inner/single VLAN in SVM
3072          */
3073         if (ice_vf_is_port_vlan_ena(vf) && !ice_is_dvm_ena(&vsi->back->hw))
3074                 return 0;
3075
3076         if (ice_vf_vlan_offload_ena(vf->driver_caps)) {
3077                 int err;
3078
3079                 err = vsi->inner_vlan_ops.ena_stripping(vsi, ETH_P_8021Q);
3080                 if (!err)
3081                         vf->vlan_strip_ena |= ICE_INNER_VLAN_STRIP_ENA;
3082                 return err;
3083         }
3084
3085         return vsi->inner_vlan_ops.dis_stripping(vsi);
3086 }
3087
3088 static u16 ice_vc_get_max_vlan_fltrs(struct ice_vf *vf)
3089 {
3090         if (vf->trusted)
3091                 return VLAN_N_VID;
3092         else
3093                 return ICE_MAX_VLAN_PER_VF;
3094 }
3095
3096 /**
3097  * ice_vf_outer_vlan_not_allowed - check if outer VLAN can be used
3098  * @vf: VF that being checked for
3099  *
3100  * When the device is in double VLAN mode, check whether or not the outer VLAN
3101  * is allowed.
3102  */
3103 static bool ice_vf_outer_vlan_not_allowed(struct ice_vf *vf)
3104 {
3105         if (ice_vf_is_port_vlan_ena(vf))
3106                 return true;
3107
3108         return false;
3109 }
3110
3111 /**
3112  * ice_vc_set_dvm_caps - set VLAN capabilities when the device is in DVM
3113  * @vf: VF that capabilities are being set for
3114  * @caps: VLAN capabilities to populate
3115  *
3116  * Determine VLAN capabilities support based on whether a port VLAN is
3117  * configured. If a port VLAN is configured then the VF should use the inner
3118  * filtering/offload capabilities since the port VLAN is using the outer VLAN
3119  * capabilies.
3120  */
3121 static void
3122 ice_vc_set_dvm_caps(struct ice_vf *vf, struct virtchnl_vlan_caps *caps)
3123 {
3124         struct virtchnl_vlan_supported_caps *supported_caps;
3125
3126         if (ice_vf_outer_vlan_not_allowed(vf)) {
3127                 /* until support for inner VLAN filtering is added when a port
3128                  * VLAN is configured, only support software offloaded inner
3129                  * VLANs when a port VLAN is confgured in DVM
3130                  */
3131                 supported_caps = &caps->filtering.filtering_support;
3132                 supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
3133
3134                 supported_caps = &caps->offloads.stripping_support;
3135                 supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
3136                                         VIRTCHNL_VLAN_TOGGLE |
3137                                         VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
3138                 supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
3139
3140                 supported_caps = &caps->offloads.insertion_support;
3141                 supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
3142                                         VIRTCHNL_VLAN_TOGGLE |
3143                                         VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
3144                 supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
3145
3146                 caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
3147                 caps->offloads.ethertype_match =
3148                         VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
3149         } else {
3150                 supported_caps = &caps->filtering.filtering_support;
3151                 supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
3152                 supported_caps->outer = VIRTCHNL_VLAN_ETHERTYPE_8100 |
3153                                         VIRTCHNL_VLAN_ETHERTYPE_88A8 |
3154                                         VIRTCHNL_VLAN_ETHERTYPE_9100 |
3155                                         VIRTCHNL_VLAN_ETHERTYPE_AND;
3156                 caps->filtering.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100 |
3157                                                  VIRTCHNL_VLAN_ETHERTYPE_88A8 |
3158                                                  VIRTCHNL_VLAN_ETHERTYPE_9100;
3159
3160                 supported_caps = &caps->offloads.stripping_support;
3161                 supported_caps->inner = VIRTCHNL_VLAN_TOGGLE |
3162                                         VIRTCHNL_VLAN_ETHERTYPE_8100 |
3163                                         VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
3164                 supported_caps->outer = VIRTCHNL_VLAN_TOGGLE |
3165                                         VIRTCHNL_VLAN_ETHERTYPE_8100 |
3166                                         VIRTCHNL_VLAN_ETHERTYPE_88A8 |
3167                                         VIRTCHNL_VLAN_ETHERTYPE_9100 |
3168                                         VIRTCHNL_VLAN_ETHERTYPE_XOR |
3169                                         VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2;
3170
3171                 supported_caps = &caps->offloads.insertion_support;
3172                 supported_caps->inner = VIRTCHNL_VLAN_TOGGLE |
3173                                         VIRTCHNL_VLAN_ETHERTYPE_8100 |
3174                                         VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
3175                 supported_caps->outer = VIRTCHNL_VLAN_TOGGLE |
3176                                         VIRTCHNL_VLAN_ETHERTYPE_8100 |
3177                                         VIRTCHNL_VLAN_ETHERTYPE_88A8 |
3178                                         VIRTCHNL_VLAN_ETHERTYPE_9100 |
3179                                         VIRTCHNL_VLAN_ETHERTYPE_XOR |
3180                                         VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2;
3181
3182                 caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
3183
3184                 caps->offloads.ethertype_match =
3185                         VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
3186         }
3187
3188         caps->filtering.max_filters = ice_vc_get_max_vlan_fltrs(vf);
3189 }
3190
3191 /**
3192  * ice_vc_set_svm_caps - set VLAN capabilities when the device is in SVM
3193  * @vf: VF that capabilities are being set for
3194  * @caps: VLAN capabilities to populate
3195  *
3196  * Determine VLAN capabilities support based on whether a port VLAN is
3197  * configured. If a port VLAN is configured then the VF does not have any VLAN
3198  * filtering or offload capabilities since the port VLAN is using the inner VLAN
3199  * capabilities in single VLAN mode (SVM). Otherwise allow the VF to use inner
3200  * VLAN fitlering and offload capabilities.
3201  */
3202 static void
3203 ice_vc_set_svm_caps(struct ice_vf *vf, struct virtchnl_vlan_caps *caps)
3204 {
3205         struct virtchnl_vlan_supported_caps *supported_caps;
3206
3207         if (ice_vf_is_port_vlan_ena(vf)) {
3208                 supported_caps = &caps->filtering.filtering_support;
3209                 supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
3210                 supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
3211
3212                 supported_caps = &caps->offloads.stripping_support;
3213                 supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
3214                 supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
3215
3216                 supported_caps = &caps->offloads.insertion_support;
3217                 supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
3218                 supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
3219
3220                 caps->offloads.ethertype_init = VIRTCHNL_VLAN_UNSUPPORTED;
3221                 caps->offloads.ethertype_match = VIRTCHNL_VLAN_UNSUPPORTED;
3222                 caps->filtering.max_filters = 0;
3223         } else {
3224                 supported_caps = &caps->filtering.filtering_support;
3225                 supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100;
3226                 supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
3227                 caps->filtering.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
3228
3229                 supported_caps = &caps->offloads.stripping_support;
3230                 supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
3231                                         VIRTCHNL_VLAN_TOGGLE |
3232                                         VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
3233                 supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
3234
3235                 supported_caps = &caps->offloads.insertion_support;
3236                 supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
3237                                         VIRTCHNL_VLAN_TOGGLE |
3238                                         VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
3239                 supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
3240
3241                 caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
3242                 caps->offloads.ethertype_match =
3243                         VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
3244                 caps->filtering.max_filters = ice_vc_get_max_vlan_fltrs(vf);
3245         }
3246 }
3247
3248 /**
3249  * ice_vc_get_offload_vlan_v2_caps - determine VF's VLAN capabilities
3250  * @vf: VF to determine VLAN capabilities for
3251  *
3252  * This will only be called if the VF and PF successfully negotiated
3253  * VIRTCHNL_VF_OFFLOAD_VLAN_V2.
3254  *
3255  * Set VLAN capabilities based on the current VLAN mode and whether a port VLAN
3256  * is configured or not.
3257  */
3258 static int ice_vc_get_offload_vlan_v2_caps(struct ice_vf *vf)
3259 {
3260         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3261         struct virtchnl_vlan_caps *caps = NULL;
3262         int err, len = 0;
3263
3264         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3265                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3266                 goto out;
3267         }
3268
3269         caps = kzalloc(sizeof(*caps), GFP_KERNEL);
3270         if (!caps) {
3271                 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
3272                 goto out;
3273         }
3274         len = sizeof(*caps);
3275
3276         if (ice_is_dvm_ena(&vf->pf->hw))
3277                 ice_vc_set_dvm_caps(vf, caps);
3278         else
3279                 ice_vc_set_svm_caps(vf, caps);
3280
3281         /* store negotiated caps to prevent invalid VF messages */
3282         memcpy(&vf->vlan_v2_caps, caps, sizeof(*caps));
3283
3284 out:
3285         err = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS,
3286                                     v_ret, (u8 *)caps, len);
3287         kfree(caps);
3288         return err;
3289 }
3290
3291 /**
3292  * ice_vc_validate_vlan_tpid - validate VLAN TPID
3293  * @filtering_caps: negotiated/supported VLAN filtering capabilities
3294  * @tpid: VLAN TPID used for validation
3295  *
3296  * Convert the VLAN TPID to a VIRTCHNL_VLAN_ETHERTYPE_* and then compare against
3297  * the negotiated/supported filtering caps to see if the VLAN TPID is valid.
3298  */
3299 static bool ice_vc_validate_vlan_tpid(u16 filtering_caps, u16 tpid)
3300 {
3301         enum virtchnl_vlan_support vlan_ethertype = VIRTCHNL_VLAN_UNSUPPORTED;
3302
3303         switch (tpid) {
3304         case ETH_P_8021Q:
3305                 vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_8100;
3306                 break;
3307         case ETH_P_8021AD:
3308                 vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_88A8;
3309                 break;
3310         case ETH_P_QINQ1:
3311                 vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_9100;
3312                 break;
3313         }
3314
3315         if (!(filtering_caps & vlan_ethertype))
3316                 return false;
3317
3318         return true;
3319 }
3320
3321 /**
3322  * ice_vc_is_valid_vlan - validate the virtchnl_vlan
3323  * @vc_vlan: virtchnl_vlan to validate
3324  *
3325  * If the VLAN TCI and VLAN TPID are 0, then this filter is invalid, so return
3326  * false. Otherwise return true.
3327  */
3328 static bool ice_vc_is_valid_vlan(struct virtchnl_vlan *vc_vlan)
3329 {
3330         if (!vc_vlan->tci || !vc_vlan->tpid)
3331                 return false;
3332
3333         return true;
3334 }
3335
3336 /**
3337  * ice_vc_validate_vlan_filter_list - validate the filter list from the VF
3338  * @vfc: negotiated/supported VLAN filtering capabilities
3339  * @vfl: VLAN filter list from VF to validate
3340  *
3341  * Validate all of the filters in the VLAN filter list from the VF. If any of
3342  * the checks fail then return false. Otherwise return true.
3343  */
3344 static bool
3345 ice_vc_validate_vlan_filter_list(struct virtchnl_vlan_filtering_caps *vfc,
3346                                  struct virtchnl_vlan_filter_list_v2 *vfl)
3347 {
3348         u16 i;
3349
3350         if (!vfl->num_elements)
3351                 return false;
3352
3353         for (i = 0; i < vfl->num_elements; i++) {
3354                 struct virtchnl_vlan_supported_caps *filtering_support =
3355                         &vfc->filtering_support;
3356                 struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
3357                 struct virtchnl_vlan *outer = &vlan_fltr->outer;
3358                 struct virtchnl_vlan *inner = &vlan_fltr->inner;
3359
3360                 if ((ice_vc_is_valid_vlan(outer) &&
3361                      filtering_support->outer == VIRTCHNL_VLAN_UNSUPPORTED) ||
3362                     (ice_vc_is_valid_vlan(inner) &&
3363                      filtering_support->inner == VIRTCHNL_VLAN_UNSUPPORTED))
3364                         return false;
3365
3366                 if ((outer->tci_mask &&
3367                      !(filtering_support->outer & VIRTCHNL_VLAN_FILTER_MASK)) ||
3368                     (inner->tci_mask &&
3369                      !(filtering_support->inner & VIRTCHNL_VLAN_FILTER_MASK)))
3370                         return false;
3371
3372                 if (((outer->tci & VLAN_PRIO_MASK) &&
3373                      !(filtering_support->outer & VIRTCHNL_VLAN_PRIO)) ||
3374                     ((inner->tci & VLAN_PRIO_MASK) &&
3375                      !(filtering_support->inner & VIRTCHNL_VLAN_PRIO)))
3376                         return false;
3377
3378                 if ((ice_vc_is_valid_vlan(outer) &&
3379                      !ice_vc_validate_vlan_tpid(filtering_support->outer,
3380                                                 outer->tpid)) ||
3381                     (ice_vc_is_valid_vlan(inner) &&
3382                      !ice_vc_validate_vlan_tpid(filtering_support->inner,
3383                                                 inner->tpid)))
3384                         return false;
3385         }
3386
3387         return true;
3388 }
3389
3390 /**
3391  * ice_vc_to_vlan - transform from struct virtchnl_vlan to struct ice_vlan
3392  * @vc_vlan: struct virtchnl_vlan to transform
3393  */
3394 static struct ice_vlan ice_vc_to_vlan(struct virtchnl_vlan *vc_vlan)
3395 {
3396         struct ice_vlan vlan = { 0 };
3397
3398         vlan.prio = FIELD_GET(VLAN_PRIO_MASK, vc_vlan->tci);
3399         vlan.vid = vc_vlan->tci & VLAN_VID_MASK;
3400         vlan.tpid = vc_vlan->tpid;
3401
3402         return vlan;
3403 }
3404
3405 /**
3406  * ice_vc_vlan_action - action to perform on the virthcnl_vlan
3407  * @vsi: VF's VSI used to perform the action
3408  * @vlan_action: function to perform the action with (i.e. add/del)
3409  * @vlan: VLAN filter to perform the action with
3410  */
3411 static int
3412 ice_vc_vlan_action(struct ice_vsi *vsi,
3413                    int (*vlan_action)(struct ice_vsi *, struct ice_vlan *),
3414                    struct ice_vlan *vlan)
3415 {
3416         int err;
3417
3418         err = vlan_action(vsi, vlan);
3419         if (err)
3420                 return err;
3421
3422         return 0;
3423 }
3424
3425 /**
3426  * ice_vc_del_vlans - delete VLAN(s) from the virtchnl filter list
3427  * @vf: VF used to delete the VLAN(s)
3428  * @vsi: VF's VSI used to delete the VLAN(s)
3429  * @vfl: virthchnl filter list used to delete the filters
3430  */
3431 static int
3432 ice_vc_del_vlans(struct ice_vf *vf, struct ice_vsi *vsi,
3433                  struct virtchnl_vlan_filter_list_v2 *vfl)
3434 {
3435         bool vlan_promisc = ice_is_vlan_promisc_allowed(vf);
3436         int err;
3437         u16 i;
3438
3439         for (i = 0; i < vfl->num_elements; i++) {
3440                 struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
3441                 struct virtchnl_vlan *vc_vlan;
3442
3443                 vc_vlan = &vlan_fltr->outer;
3444                 if (ice_vc_is_valid_vlan(vc_vlan)) {
3445                         struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3446
3447                         err = ice_vc_vlan_action(vsi,
3448                                                  vsi->outer_vlan_ops.del_vlan,
3449                                                  &vlan);
3450                         if (err)
3451                                 return err;
3452
3453                         if (vlan_promisc)
3454                                 ice_vf_dis_vlan_promisc(vsi, &vlan);
3455
3456                         /* Disable VLAN filtering when only VLAN 0 is left */
3457                         if (!ice_vsi_has_non_zero_vlans(vsi) && ice_is_dvm_ena(&vsi->back->hw)) {
3458                                 err = vsi->outer_vlan_ops.dis_tx_filtering(vsi);
3459                                 if (err)
3460                                         return err;
3461                         }
3462                 }
3463
3464                 vc_vlan = &vlan_fltr->inner;
3465                 if (ice_vc_is_valid_vlan(vc_vlan)) {
3466                         struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3467
3468                         err = ice_vc_vlan_action(vsi,
3469                                                  vsi->inner_vlan_ops.del_vlan,
3470                                                  &vlan);
3471                         if (err)
3472                                 return err;
3473
3474                         /* no support for VLAN promiscuous on inner VLAN unless
3475                          * we are in Single VLAN Mode (SVM)
3476                          */
3477                         if (!ice_is_dvm_ena(&vsi->back->hw)) {
3478                                 if (vlan_promisc)
3479                                         ice_vf_dis_vlan_promisc(vsi, &vlan);
3480
3481                                 /* Disable VLAN filtering when only VLAN 0 is left */
3482                                 if (!ice_vsi_has_non_zero_vlans(vsi)) {
3483                                         err = vsi->inner_vlan_ops.dis_tx_filtering(vsi);
3484                                         if (err)
3485                                                 return err;
3486                                 }
3487                         }
3488                 }
3489         }
3490
3491         return 0;
3492 }
3493
3494 /**
3495  * ice_vc_remove_vlan_v2_msg - virtchnl handler for VIRTCHNL_OP_DEL_VLAN_V2
3496  * @vf: VF the message was received from
3497  * @msg: message received from the VF
3498  */
3499 static int ice_vc_remove_vlan_v2_msg(struct ice_vf *vf, u8 *msg)
3500 {
3501         struct virtchnl_vlan_filter_list_v2 *vfl =
3502                 (struct virtchnl_vlan_filter_list_v2 *)msg;
3503         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3504         struct ice_vsi *vsi;
3505
3506         if (!ice_vc_validate_vlan_filter_list(&vf->vlan_v2_caps.filtering,
3507                                               vfl)) {
3508                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3509                 goto out;
3510         }
3511
3512         if (!ice_vc_isvalid_vsi_id(vf, vfl->vport_id)) {
3513                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3514                 goto out;
3515         }
3516
3517         vsi = ice_get_vf_vsi(vf);
3518         if (!vsi) {
3519                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3520                 goto out;
3521         }
3522
3523         if (ice_vc_del_vlans(vf, vsi, vfl))
3524                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3525
3526 out:
3527         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN_V2, v_ret, NULL,
3528                                      0);
3529 }
3530
3531 /**
3532  * ice_vc_add_vlans - add VLAN(s) from the virtchnl filter list
3533  * @vf: VF used to add the VLAN(s)
3534  * @vsi: VF's VSI used to add the VLAN(s)
3535  * @vfl: virthchnl filter list used to add the filters
3536  */
3537 static int
3538 ice_vc_add_vlans(struct ice_vf *vf, struct ice_vsi *vsi,
3539                  struct virtchnl_vlan_filter_list_v2 *vfl)
3540 {
3541         bool vlan_promisc = ice_is_vlan_promisc_allowed(vf);
3542         int err;
3543         u16 i;
3544
3545         for (i = 0; i < vfl->num_elements; i++) {
3546                 struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
3547                 struct virtchnl_vlan *vc_vlan;
3548
3549                 vc_vlan = &vlan_fltr->outer;
3550                 if (ice_vc_is_valid_vlan(vc_vlan)) {
3551                         struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3552
3553                         err = ice_vc_vlan_action(vsi,
3554                                                  vsi->outer_vlan_ops.add_vlan,
3555                                                  &vlan);
3556                         if (err)
3557                                 return err;
3558
3559                         if (vlan_promisc) {
3560                                 err = ice_vf_ena_vlan_promisc(vf, vsi, &vlan);
3561                                 if (err)
3562                                         return err;
3563                         }
3564
3565                         /* Enable VLAN filtering on first non-zero VLAN */
3566                         if (vf->spoofchk && vlan.vid && ice_is_dvm_ena(&vsi->back->hw)) {
3567                                 err = vsi->outer_vlan_ops.ena_tx_filtering(vsi);
3568                                 if (err)
3569                                         return err;
3570                         }
3571                 }
3572
3573                 vc_vlan = &vlan_fltr->inner;
3574                 if (ice_vc_is_valid_vlan(vc_vlan)) {
3575                         struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3576
3577                         err = ice_vc_vlan_action(vsi,
3578                                                  vsi->inner_vlan_ops.add_vlan,
3579                                                  &vlan);
3580                         if (err)
3581                                 return err;
3582
3583                         /* no support for VLAN promiscuous on inner VLAN unless
3584                          * we are in Single VLAN Mode (SVM)
3585                          */
3586                         if (!ice_is_dvm_ena(&vsi->back->hw)) {
3587                                 if (vlan_promisc) {
3588                                         err = ice_vf_ena_vlan_promisc(vf, vsi,
3589                                                                       &vlan);
3590                                         if (err)
3591                                                 return err;
3592                                 }
3593
3594                                 /* Enable VLAN filtering on first non-zero VLAN */
3595                                 if (vf->spoofchk && vlan.vid) {
3596                                         err = vsi->inner_vlan_ops.ena_tx_filtering(vsi);
3597                                         if (err)
3598                                                 return err;
3599                                 }
3600                         }
3601                 }
3602         }
3603
3604         return 0;
3605 }
3606
3607 /**
3608  * ice_vc_validate_add_vlan_filter_list - validate add filter list from the VF
3609  * @vsi: VF VSI used to get number of existing VLAN filters
3610  * @vfc: negotiated/supported VLAN filtering capabilities
3611  * @vfl: VLAN filter list from VF to validate
3612  *
3613  * Validate all of the filters in the VLAN filter list from the VF during the
3614  * VIRTCHNL_OP_ADD_VLAN_V2 opcode. If any of the checks fail then return false.
3615  * Otherwise return true.
3616  */
3617 static bool
3618 ice_vc_validate_add_vlan_filter_list(struct ice_vsi *vsi,
3619                                      struct virtchnl_vlan_filtering_caps *vfc,
3620                                      struct virtchnl_vlan_filter_list_v2 *vfl)
3621 {
3622         u16 num_requested_filters = ice_vsi_num_non_zero_vlans(vsi) +
3623                 vfl->num_elements;
3624
3625         if (num_requested_filters > vfc->max_filters)
3626                 return false;
3627
3628         return ice_vc_validate_vlan_filter_list(vfc, vfl);
3629 }
3630
3631 /**
3632  * ice_vc_add_vlan_v2_msg - virtchnl handler for VIRTCHNL_OP_ADD_VLAN_V2
3633  * @vf: VF the message was received from
3634  * @msg: message received from the VF
3635  */
3636 static int ice_vc_add_vlan_v2_msg(struct ice_vf *vf, u8 *msg)
3637 {
3638         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3639         struct virtchnl_vlan_filter_list_v2 *vfl =
3640                 (struct virtchnl_vlan_filter_list_v2 *)msg;
3641         struct ice_vsi *vsi;
3642
3643         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3644                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3645                 goto out;
3646         }
3647
3648         if (!ice_vc_isvalid_vsi_id(vf, vfl->vport_id)) {
3649                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3650                 goto out;
3651         }
3652
3653         vsi = ice_get_vf_vsi(vf);
3654         if (!vsi) {
3655                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3656                 goto out;
3657         }
3658
3659         if (!ice_vc_validate_add_vlan_filter_list(vsi,
3660                                                   &vf->vlan_v2_caps.filtering,
3661                                                   vfl)) {
3662                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3663                 goto out;
3664         }
3665
3666         if (ice_vc_add_vlans(vf, vsi, vfl))
3667                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3668
3669 out:
3670         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN_V2, v_ret, NULL,
3671                                      0);
3672 }
3673
3674 /**
3675  * ice_vc_valid_vlan_setting - validate VLAN setting
3676  * @negotiated_settings: negotiated VLAN settings during VF init
3677  * @ethertype_setting: ethertype(s) requested for the VLAN setting
3678  */
3679 static bool
3680 ice_vc_valid_vlan_setting(u32 negotiated_settings, u32 ethertype_setting)
3681 {
3682         if (ethertype_setting && !(negotiated_settings & ethertype_setting))
3683                 return false;
3684
3685         /* only allow a single VIRTCHNL_VLAN_ETHERTYPE if
3686          * VIRTHCNL_VLAN_ETHERTYPE_AND is not negotiated/supported
3687          */
3688         if (!(negotiated_settings & VIRTCHNL_VLAN_ETHERTYPE_AND) &&
3689             hweight32(ethertype_setting) > 1)
3690                 return false;
3691
3692         /* ability to modify the VLAN setting was not negotiated */
3693         if (!(negotiated_settings & VIRTCHNL_VLAN_TOGGLE))
3694                 return false;
3695
3696         return true;
3697 }
3698
3699 /**
3700  * ice_vc_valid_vlan_setting_msg - validate the VLAN setting message
3701  * @caps: negotiated VLAN settings during VF init
3702  * @msg: message to validate
3703  *
3704  * Used to validate any VLAN virtchnl message sent as a
3705  * virtchnl_vlan_setting structure. Validates the message against the
3706  * negotiated/supported caps during VF driver init.
3707  */
3708 static bool
3709 ice_vc_valid_vlan_setting_msg(struct virtchnl_vlan_supported_caps *caps,
3710                               struct virtchnl_vlan_setting *msg)
3711 {
3712         if ((!msg->outer_ethertype_setting &&
3713              !msg->inner_ethertype_setting) ||
3714             (!caps->outer && !caps->inner))
3715                 return false;
3716
3717         if (msg->outer_ethertype_setting &&
3718             !ice_vc_valid_vlan_setting(caps->outer,
3719                                        msg->outer_ethertype_setting))
3720                 return false;
3721
3722         if (msg->inner_ethertype_setting &&
3723             !ice_vc_valid_vlan_setting(caps->inner,
3724                                        msg->inner_ethertype_setting))
3725                 return false;
3726
3727         return true;
3728 }
3729
3730 /**
3731  * ice_vc_get_tpid - transform from VIRTCHNL_VLAN_ETHERTYPE_* to VLAN TPID
3732  * @ethertype_setting: VIRTCHNL_VLAN_ETHERTYPE_* used to get VLAN TPID
3733  * @tpid: VLAN TPID to populate
3734  */
3735 static int ice_vc_get_tpid(u32 ethertype_setting, u16 *tpid)
3736 {
3737         switch (ethertype_setting) {
3738         case VIRTCHNL_VLAN_ETHERTYPE_8100:
3739                 *tpid = ETH_P_8021Q;
3740                 break;
3741         case VIRTCHNL_VLAN_ETHERTYPE_88A8:
3742                 *tpid = ETH_P_8021AD;
3743                 break;
3744         case VIRTCHNL_VLAN_ETHERTYPE_9100:
3745                 *tpid = ETH_P_QINQ1;
3746                 break;
3747         default:
3748                 *tpid = 0;
3749                 return -EINVAL;
3750         }
3751
3752         return 0;
3753 }
3754
3755 /**
3756  * ice_vc_ena_vlan_offload - enable VLAN offload based on the ethertype_setting
3757  * @vsi: VF's VSI used to enable the VLAN offload
3758  * @ena_offload: function used to enable the VLAN offload
3759  * @ethertype_setting: VIRTCHNL_VLAN_ETHERTYPE_* to enable offloads for
3760  */
3761 static int
3762 ice_vc_ena_vlan_offload(struct ice_vsi *vsi,
3763                         int (*ena_offload)(struct ice_vsi *vsi, u16 tpid),
3764                         u32 ethertype_setting)
3765 {
3766         u16 tpid;
3767         int err;
3768
3769         err = ice_vc_get_tpid(ethertype_setting, &tpid);
3770         if (err)
3771                 return err;
3772
3773         err = ena_offload(vsi, tpid);
3774         if (err)
3775                 return err;
3776
3777         return 0;
3778 }
3779
3780 #define ICE_L2TSEL_QRX_CONTEXT_REG_IDX  3
3781 #define ICE_L2TSEL_BIT_OFFSET           23
3782 enum ice_l2tsel {
3783         ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND,
3784         ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG1,
3785 };
3786
3787 /**
3788  * ice_vsi_update_l2tsel - update l2tsel field for all Rx rings on this VSI
3789  * @vsi: VSI used to update l2tsel on
3790  * @l2tsel: l2tsel setting requested
3791  *
3792  * Use the l2tsel setting to update all of the Rx queue context bits for l2tsel.
3793  * This will modify which descriptor field the first offloaded VLAN will be
3794  * stripped into.
3795  */
3796 static void ice_vsi_update_l2tsel(struct ice_vsi *vsi, enum ice_l2tsel l2tsel)
3797 {
3798         struct ice_hw *hw = &vsi->back->hw;
3799         u32 l2tsel_bit;
3800         int i;
3801
3802         if (l2tsel == ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND)
3803                 l2tsel_bit = 0;
3804         else
3805                 l2tsel_bit = BIT(ICE_L2TSEL_BIT_OFFSET);
3806
3807         for (i = 0; i < vsi->alloc_rxq; i++) {
3808                 u16 pfq = vsi->rxq_map[i];
3809                 u32 qrx_context_offset;
3810                 u32 regval;
3811
3812                 qrx_context_offset =
3813                         QRX_CONTEXT(ICE_L2TSEL_QRX_CONTEXT_REG_IDX, pfq);
3814
3815                 regval = rd32(hw, qrx_context_offset);
3816                 regval &= ~BIT(ICE_L2TSEL_BIT_OFFSET);
3817                 regval |= l2tsel_bit;
3818                 wr32(hw, qrx_context_offset, regval);
3819         }
3820 }
3821
3822 /**
3823  * ice_vc_ena_vlan_stripping_v2_msg
3824  * @vf: VF the message was received from
3825  * @msg: message received from the VF
3826  *
3827  * virthcnl handler for VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2
3828  */
3829 static int ice_vc_ena_vlan_stripping_v2_msg(struct ice_vf *vf, u8 *msg)
3830 {
3831         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3832         struct virtchnl_vlan_supported_caps *stripping_support;
3833         struct virtchnl_vlan_setting *strip_msg =
3834                 (struct virtchnl_vlan_setting *)msg;
3835         u32 ethertype_setting;
3836         struct ice_vsi *vsi;
3837
3838         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3839                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3840                 goto out;
3841         }
3842
3843         if (!ice_vc_isvalid_vsi_id(vf, strip_msg->vport_id)) {
3844                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3845                 goto out;
3846         }
3847
3848         vsi = ice_get_vf_vsi(vf);
3849         if (!vsi) {
3850                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3851                 goto out;
3852         }
3853
3854         stripping_support = &vf->vlan_v2_caps.offloads.stripping_support;
3855         if (!ice_vc_valid_vlan_setting_msg(stripping_support, strip_msg)) {
3856                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3857                 goto out;
3858         }
3859
3860         if (ice_vsi_is_rxq_crc_strip_dis(vsi)) {
3861                 v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
3862                 goto out;
3863         }
3864
3865         ethertype_setting = strip_msg->outer_ethertype_setting;
3866         if (ethertype_setting) {
3867                 if (ice_vc_ena_vlan_offload(vsi,
3868                                             vsi->outer_vlan_ops.ena_stripping,
3869                                             ethertype_setting)) {
3870                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3871                         goto out;
3872                 } else {
3873                         enum ice_l2tsel l2tsel =
3874                                 ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND;
3875
3876                         /* PF tells the VF that the outer VLAN tag is always
3877                          * extracted to VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2 and
3878                          * inner is always extracted to
3879                          * VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1. This is needed to
3880                          * support outer stripping so the first tag always ends
3881                          * up in L2TAG2_2ND and the second/inner tag, if
3882                          * enabled, is extracted in L2TAG1.
3883                          */
3884                         ice_vsi_update_l2tsel(vsi, l2tsel);
3885
3886                         vf->vlan_strip_ena |= ICE_OUTER_VLAN_STRIP_ENA;
3887                 }
3888         }
3889
3890         ethertype_setting = strip_msg->inner_ethertype_setting;
3891         if (ethertype_setting &&
3892             ice_vc_ena_vlan_offload(vsi, vsi->inner_vlan_ops.ena_stripping,
3893                                     ethertype_setting)) {
3894                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3895                 goto out;
3896         }
3897
3898         if (ethertype_setting)
3899                 vf->vlan_strip_ena |= ICE_INNER_VLAN_STRIP_ENA;
3900
3901 out:
3902         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2,
3903                                      v_ret, NULL, 0);
3904 }
3905
3906 /**
3907  * ice_vc_dis_vlan_stripping_v2_msg
3908  * @vf: VF the message was received from
3909  * @msg: message received from the VF
3910  *
3911  * virthcnl handler for VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2
3912  */
3913 static int ice_vc_dis_vlan_stripping_v2_msg(struct ice_vf *vf, u8 *msg)
3914 {
3915         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3916         struct virtchnl_vlan_supported_caps *stripping_support;
3917         struct virtchnl_vlan_setting *strip_msg =
3918                 (struct virtchnl_vlan_setting *)msg;
3919         u32 ethertype_setting;
3920         struct ice_vsi *vsi;
3921
3922         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3923                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3924                 goto out;
3925         }
3926
3927         if (!ice_vc_isvalid_vsi_id(vf, strip_msg->vport_id)) {
3928                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3929                 goto out;
3930         }
3931
3932         vsi = ice_get_vf_vsi(vf);
3933         if (!vsi) {
3934                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3935                 goto out;
3936         }
3937
3938         stripping_support = &vf->vlan_v2_caps.offloads.stripping_support;
3939         if (!ice_vc_valid_vlan_setting_msg(stripping_support, strip_msg)) {
3940                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3941                 goto out;
3942         }
3943
3944         ethertype_setting = strip_msg->outer_ethertype_setting;
3945         if (ethertype_setting) {
3946                 if (vsi->outer_vlan_ops.dis_stripping(vsi)) {
3947                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3948                         goto out;
3949                 } else {
3950                         enum ice_l2tsel l2tsel =
3951                                 ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG1;
3952
3953                         /* PF tells the VF that the outer VLAN tag is always
3954                          * extracted to VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2 and
3955                          * inner is always extracted to
3956                          * VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1. This is needed to
3957                          * support inner stripping while outer stripping is
3958                          * disabled so that the first and only tag is extracted
3959                          * in L2TAG1.
3960                          */
3961                         ice_vsi_update_l2tsel(vsi, l2tsel);
3962
3963                         vf->vlan_strip_ena &= ~ICE_OUTER_VLAN_STRIP_ENA;
3964                 }
3965         }
3966
3967         ethertype_setting = strip_msg->inner_ethertype_setting;
3968         if (ethertype_setting && vsi->inner_vlan_ops.dis_stripping(vsi)) {
3969                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3970                 goto out;
3971         }
3972
3973         if (ethertype_setting)
3974                 vf->vlan_strip_ena &= ~ICE_INNER_VLAN_STRIP_ENA;
3975
3976 out:
3977         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2,
3978                                      v_ret, NULL, 0);
3979 }
3980
3981 /**
3982  * ice_vc_ena_vlan_insertion_v2_msg
3983  * @vf: VF the message was received from
3984  * @msg: message received from the VF
3985  *
3986  * virthcnl handler for VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2
3987  */
3988 static int ice_vc_ena_vlan_insertion_v2_msg(struct ice_vf *vf, u8 *msg)
3989 {
3990         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3991         struct virtchnl_vlan_supported_caps *insertion_support;
3992         struct virtchnl_vlan_setting *insertion_msg =
3993                 (struct virtchnl_vlan_setting *)msg;
3994         u32 ethertype_setting;
3995         struct ice_vsi *vsi;
3996
3997         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3998                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3999                 goto out;
4000         }
4001
4002         if (!ice_vc_isvalid_vsi_id(vf, insertion_msg->vport_id)) {
4003                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4004                 goto out;
4005         }
4006
4007         vsi = ice_get_vf_vsi(vf);
4008         if (!vsi) {
4009                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4010                 goto out;
4011         }
4012
4013         insertion_support = &vf->vlan_v2_caps.offloads.insertion_support;
4014         if (!ice_vc_valid_vlan_setting_msg(insertion_support, insertion_msg)) {
4015                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4016                 goto out;
4017         }
4018
4019         ethertype_setting = insertion_msg->outer_ethertype_setting;
4020         if (ethertype_setting &&
4021             ice_vc_ena_vlan_offload(vsi, vsi->outer_vlan_ops.ena_insertion,
4022                                     ethertype_setting)) {
4023                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4024                 goto out;
4025         }
4026
4027         ethertype_setting = insertion_msg->inner_ethertype_setting;
4028         if (ethertype_setting &&
4029             ice_vc_ena_vlan_offload(vsi, vsi->inner_vlan_ops.ena_insertion,
4030                                     ethertype_setting)) {
4031                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4032                 goto out;
4033         }
4034
4035 out:
4036         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2,
4037                                      v_ret, NULL, 0);
4038 }
4039
4040 /**
4041  * ice_vc_dis_vlan_insertion_v2_msg
4042  * @vf: VF the message was received from
4043  * @msg: message received from the VF
4044  *
4045  * virthcnl handler for VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2
4046  */
4047 static int ice_vc_dis_vlan_insertion_v2_msg(struct ice_vf *vf, u8 *msg)
4048 {
4049         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4050         struct virtchnl_vlan_supported_caps *insertion_support;
4051         struct virtchnl_vlan_setting *insertion_msg =
4052                 (struct virtchnl_vlan_setting *)msg;
4053         u32 ethertype_setting;
4054         struct ice_vsi *vsi;
4055
4056         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4057                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4058                 goto out;
4059         }
4060
4061         if (!ice_vc_isvalid_vsi_id(vf, insertion_msg->vport_id)) {
4062                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4063                 goto out;
4064         }
4065
4066         vsi = ice_get_vf_vsi(vf);
4067         if (!vsi) {
4068                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4069                 goto out;
4070         }
4071
4072         insertion_support = &vf->vlan_v2_caps.offloads.insertion_support;
4073         if (!ice_vc_valid_vlan_setting_msg(insertion_support, insertion_msg)) {
4074                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4075                 goto out;
4076         }
4077
4078         ethertype_setting = insertion_msg->outer_ethertype_setting;
4079         if (ethertype_setting && vsi->outer_vlan_ops.dis_insertion(vsi)) {
4080                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4081                 goto out;
4082         }
4083
4084         ethertype_setting = insertion_msg->inner_ethertype_setting;
4085         if (ethertype_setting && vsi->inner_vlan_ops.dis_insertion(vsi)) {
4086                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4087                 goto out;
4088         }
4089
4090 out:
4091         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2,
4092                                      v_ret, NULL, 0);
4093 }
4094
4095 static const struct ice_virtchnl_ops ice_virtchnl_dflt_ops = {
4096         .get_ver_msg = ice_vc_get_ver_msg,
4097         .get_vf_res_msg = ice_vc_get_vf_res_msg,
4098         .reset_vf = ice_vc_reset_vf_msg,
4099         .add_mac_addr_msg = ice_vc_add_mac_addr_msg,
4100         .del_mac_addr_msg = ice_vc_del_mac_addr_msg,
4101         .cfg_qs_msg = ice_vc_cfg_qs_msg,
4102         .ena_qs_msg = ice_vc_ena_qs_msg,
4103         .dis_qs_msg = ice_vc_dis_qs_msg,
4104         .request_qs_msg = ice_vc_request_qs_msg,
4105         .cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
4106         .config_rss_key = ice_vc_config_rss_key,
4107         .config_rss_lut = ice_vc_config_rss_lut,
4108         .config_rss_hfunc = ice_vc_config_rss_hfunc,
4109         .get_stats_msg = ice_vc_get_stats_msg,
4110         .cfg_promiscuous_mode_msg = ice_vc_cfg_promiscuous_mode_msg,
4111         .add_vlan_msg = ice_vc_add_vlan_msg,
4112         .remove_vlan_msg = ice_vc_remove_vlan_msg,
4113         .query_rxdid = ice_vc_query_rxdid,
4114         .get_rss_hena = ice_vc_get_rss_hena,
4115         .set_rss_hena_msg = ice_vc_set_rss_hena,
4116         .ena_vlan_stripping = ice_vc_ena_vlan_stripping,
4117         .dis_vlan_stripping = ice_vc_dis_vlan_stripping,
4118         .handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
4119         .add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
4120         .del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
4121         .get_offload_vlan_v2_caps = ice_vc_get_offload_vlan_v2_caps,
4122         .add_vlan_v2_msg = ice_vc_add_vlan_v2_msg,
4123         .remove_vlan_v2_msg = ice_vc_remove_vlan_v2_msg,
4124         .ena_vlan_stripping_v2_msg = ice_vc_ena_vlan_stripping_v2_msg,
4125         .dis_vlan_stripping_v2_msg = ice_vc_dis_vlan_stripping_v2_msg,
4126         .ena_vlan_insertion_v2_msg = ice_vc_ena_vlan_insertion_v2_msg,
4127         .dis_vlan_insertion_v2_msg = ice_vc_dis_vlan_insertion_v2_msg,
4128         .get_qos_caps = ice_vc_get_qos_caps,
4129         .cfg_q_bw = ice_vc_cfg_q_bw,
4130         .cfg_q_quanta = ice_vc_cfg_q_quanta,
4131         /* If you add a new op here please make sure to add it to
4132          * ice_virtchnl_repr_ops as well.
4133          */
4134 };
4135
4136 /**
4137  * ice_virtchnl_set_dflt_ops - Switch to default virtchnl ops
4138  * @vf: the VF to switch ops
4139  */
4140 void ice_virtchnl_set_dflt_ops(struct ice_vf *vf)
4141 {
4142         vf->virtchnl_ops = &ice_virtchnl_dflt_ops;
4143 }
4144
4145 /**
4146  * ice_vc_repr_add_mac
4147  * @vf: pointer to VF
4148  * @msg: virtchannel message
4149  *
4150  * When port representors are created, we do not add MAC rule
4151  * to firmware, we store it so that PF could report same
4152  * MAC as VF.
4153  */
4154 static int ice_vc_repr_add_mac(struct ice_vf *vf, u8 *msg)
4155 {
4156         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4157         struct virtchnl_ether_addr_list *al =
4158             (struct virtchnl_ether_addr_list *)msg;
4159         struct ice_vsi *vsi;
4160         struct ice_pf *pf;
4161         int i;
4162
4163         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
4164             !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
4165                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4166                 goto handle_mac_exit;
4167         }
4168
4169         pf = vf->pf;
4170
4171         vsi = ice_get_vf_vsi(vf);
4172         if (!vsi) {
4173                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4174                 goto handle_mac_exit;
4175         }
4176
4177         for (i = 0; i < al->num_elements; i++) {
4178                 u8 *mac_addr = al->list[i].addr;
4179
4180                 if (!is_unicast_ether_addr(mac_addr) ||
4181                     ether_addr_equal(mac_addr, vf->hw_lan_addr))
4182                         continue;
4183
4184                 if (vf->pf_set_mac) {
4185                         dev_err(ice_pf_to_dev(pf), "VF attempting to override administratively set MAC address\n");
4186                         v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
4187                         goto handle_mac_exit;
4188                 }
4189
4190                 ice_vfhw_mac_add(vf, &al->list[i]);
4191                 vf->num_mac++;
4192                 break;
4193         }
4194
4195 handle_mac_exit:
4196         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
4197                                      v_ret, NULL, 0);
4198 }
4199
4200 /**
4201  * ice_vc_repr_del_mac - response with success for deleting MAC
4202  * @vf: pointer to VF
4203  * @msg: virtchannel message
4204  *
4205  * Respond with success to not break normal VF flow.
4206  * For legacy VF driver try to update cached MAC address.
4207  */
4208 static int
4209 ice_vc_repr_del_mac(struct ice_vf __always_unused *vf, u8 __always_unused *msg)
4210 {
4211         struct virtchnl_ether_addr_list *al =
4212                 (struct virtchnl_ether_addr_list *)msg;
4213
4214         ice_update_legacy_cached_mac(vf, &al->list[0]);
4215
4216         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
4217                                      VIRTCHNL_STATUS_SUCCESS, NULL, 0);
4218 }
4219
4220 static int
4221 ice_vc_repr_cfg_promiscuous_mode(struct ice_vf *vf, u8 __always_unused *msg)
4222 {
4223         dev_dbg(ice_pf_to_dev(vf->pf),
4224                 "Can't config promiscuous mode in switchdev mode for VF %d\n",
4225                 vf->vf_id);
4226         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
4227                                      VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4228                                      NULL, 0);
4229 }
4230
4231 static const struct ice_virtchnl_ops ice_virtchnl_repr_ops = {
4232         .get_ver_msg = ice_vc_get_ver_msg,
4233         .get_vf_res_msg = ice_vc_get_vf_res_msg,
4234         .reset_vf = ice_vc_reset_vf_msg,
4235         .add_mac_addr_msg = ice_vc_repr_add_mac,
4236         .del_mac_addr_msg = ice_vc_repr_del_mac,
4237         .cfg_qs_msg = ice_vc_cfg_qs_msg,
4238         .ena_qs_msg = ice_vc_ena_qs_msg,
4239         .dis_qs_msg = ice_vc_dis_qs_msg,
4240         .request_qs_msg = ice_vc_request_qs_msg,
4241         .cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
4242         .config_rss_key = ice_vc_config_rss_key,
4243         .config_rss_lut = ice_vc_config_rss_lut,
4244         .config_rss_hfunc = ice_vc_config_rss_hfunc,
4245         .get_stats_msg = ice_vc_get_stats_msg,
4246         .cfg_promiscuous_mode_msg = ice_vc_repr_cfg_promiscuous_mode,
4247         .add_vlan_msg = ice_vc_add_vlan_msg,
4248         .remove_vlan_msg = ice_vc_remove_vlan_msg,
4249         .query_rxdid = ice_vc_query_rxdid,
4250         .get_rss_hena = ice_vc_get_rss_hena,
4251         .set_rss_hena_msg = ice_vc_set_rss_hena,
4252         .ena_vlan_stripping = ice_vc_ena_vlan_stripping,
4253         .dis_vlan_stripping = ice_vc_dis_vlan_stripping,
4254         .handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
4255         .add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
4256         .del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
4257         .get_offload_vlan_v2_caps = ice_vc_get_offload_vlan_v2_caps,
4258         .add_vlan_v2_msg = ice_vc_add_vlan_v2_msg,
4259         .remove_vlan_v2_msg = ice_vc_remove_vlan_v2_msg,
4260         .ena_vlan_stripping_v2_msg = ice_vc_ena_vlan_stripping_v2_msg,
4261         .dis_vlan_stripping_v2_msg = ice_vc_dis_vlan_stripping_v2_msg,
4262         .ena_vlan_insertion_v2_msg = ice_vc_ena_vlan_insertion_v2_msg,
4263         .dis_vlan_insertion_v2_msg = ice_vc_dis_vlan_insertion_v2_msg,
4264         .get_qos_caps = ice_vc_get_qos_caps,
4265         .cfg_q_bw = ice_vc_cfg_q_bw,
4266         .cfg_q_quanta = ice_vc_cfg_q_quanta,
4267 };
4268
4269 /**
4270  * ice_virtchnl_set_repr_ops - Switch to representor virtchnl ops
4271  * @vf: the VF to switch ops
4272  */
4273 void ice_virtchnl_set_repr_ops(struct ice_vf *vf)
4274 {
4275         vf->virtchnl_ops = &ice_virtchnl_repr_ops;
4276 }
4277
4278 /**
4279  * ice_is_malicious_vf - check if this vf might be overflowing mailbox
4280  * @vf: the VF to check
4281  * @mbxdata: data about the state of the mailbox
4282  *
4283  * Detect if a given VF might be malicious and attempting to overflow the PF
4284  * mailbox. If so, log a warning message and ignore this event.
4285  */
4286 static bool
4287 ice_is_malicious_vf(struct ice_vf *vf, struct ice_mbx_data *mbxdata)
4288 {
4289         bool report_malvf = false;
4290         struct device *dev;
4291         struct ice_pf *pf;
4292         int status;
4293
4294         pf = vf->pf;
4295         dev = ice_pf_to_dev(pf);
4296
4297         if (test_bit(ICE_VF_STATE_DIS, vf->vf_states))
4298                 return vf->mbx_info.malicious;
4299
4300         /* check to see if we have a newly malicious VF */
4301         status = ice_mbx_vf_state_handler(&pf->hw, mbxdata, &vf->mbx_info,
4302                                           &report_malvf);
4303         if (status)
4304                 dev_warn_ratelimited(dev, "Unable to check status of mailbox overflow for VF %u MAC %pM, status %d\n",
4305                                      vf->vf_id, vf->dev_lan_addr, status);
4306
4307         if (report_malvf) {
4308                 struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
4309                 u8 zero_addr[ETH_ALEN] = {};
4310
4311                 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",
4312                          vf->dev_lan_addr,
4313                          pf_vsi ? pf_vsi->netdev->dev_addr : zero_addr);
4314         }
4315
4316         return vf->mbx_info.malicious;
4317 }
4318
4319 /**
4320  * ice_vc_process_vf_msg - Process request from VF
4321  * @pf: pointer to the PF structure
4322  * @event: pointer to the AQ event
4323  * @mbxdata: information used to detect VF attempting mailbox overflow
4324  *
4325  * Called from the common asq/arq handler to process request from VF. When this
4326  * flow is used for devices with hardware VF to PF message queue overflow
4327  * support (ICE_F_MBX_LIMIT) mbxdata is set to NULL and ice_is_malicious_vf
4328  * check is skipped.
4329  */
4330 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event,
4331                            struct ice_mbx_data *mbxdata)
4332 {
4333         u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
4334         s16 vf_id = le16_to_cpu(event->desc.retval);
4335         const struct ice_virtchnl_ops *ops;
4336         u16 msglen = event->msg_len;
4337         u8 *msg = event->msg_buf;
4338         struct ice_vf *vf = NULL;
4339         struct device *dev;
4340         int err = 0;
4341
4342         dev = ice_pf_to_dev(pf);
4343
4344         vf = ice_get_vf_by_id(pf, vf_id);
4345         if (!vf) {
4346                 dev_err(dev, "Unable to locate VF for message from VF ID %d, opcode %d, len %d\n",
4347                         vf_id, v_opcode, msglen);
4348                 return;
4349         }
4350
4351         mutex_lock(&vf->cfg_lock);
4352
4353         /* Check if the VF is trying to overflow the mailbox */
4354         if (mbxdata && ice_is_malicious_vf(vf, mbxdata))
4355                 goto finish;
4356
4357         /* Check if VF is disabled. */
4358         if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
4359                 err = -EPERM;
4360                 goto error_handler;
4361         }
4362
4363         ops = vf->virtchnl_ops;
4364
4365         /* Perform basic checks on the msg */
4366         err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
4367         if (err) {
4368                 if (err == VIRTCHNL_STATUS_ERR_PARAM)
4369                         err = -EPERM;
4370                 else
4371                         err = -EINVAL;
4372         }
4373
4374 error_handler:
4375         if (err) {
4376                 ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
4377                                       NULL, 0);
4378                 dev_err(dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
4379                         vf_id, v_opcode, msglen, err);
4380                 goto finish;
4381         }
4382
4383         if (!ice_vc_is_opcode_allowed(vf, v_opcode)) {
4384                 ice_vc_send_msg_to_vf(vf, v_opcode,
4385                                       VIRTCHNL_STATUS_ERR_NOT_SUPPORTED, NULL,
4386                                       0);
4387                 goto finish;
4388         }
4389
4390         switch (v_opcode) {
4391         case VIRTCHNL_OP_VERSION:
4392                 err = ops->get_ver_msg(vf, msg);
4393                 break;
4394         case VIRTCHNL_OP_GET_VF_RESOURCES:
4395                 err = ops->get_vf_res_msg(vf, msg);
4396                 if (ice_vf_init_vlan_stripping(vf))
4397                         dev_dbg(dev, "Failed to initialize VLAN stripping for VF %d\n",
4398                                 vf->vf_id);
4399                 ice_vc_notify_vf_link_state(vf);
4400                 break;
4401         case VIRTCHNL_OP_RESET_VF:
4402                 ops->reset_vf(vf);
4403                 break;
4404         case VIRTCHNL_OP_ADD_ETH_ADDR:
4405                 err = ops->add_mac_addr_msg(vf, msg);
4406                 break;
4407         case VIRTCHNL_OP_DEL_ETH_ADDR:
4408                 err = ops->del_mac_addr_msg(vf, msg);
4409                 break;
4410         case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
4411                 err = ops->cfg_qs_msg(vf, msg);
4412                 break;
4413         case VIRTCHNL_OP_ENABLE_QUEUES:
4414                 err = ops->ena_qs_msg(vf, msg);
4415                 ice_vc_notify_vf_link_state(vf);
4416                 break;
4417         case VIRTCHNL_OP_DISABLE_QUEUES:
4418                 err = ops->dis_qs_msg(vf, msg);
4419                 break;
4420         case VIRTCHNL_OP_REQUEST_QUEUES:
4421                 err = ops->request_qs_msg(vf, msg);
4422                 break;
4423         case VIRTCHNL_OP_CONFIG_IRQ_MAP:
4424                 err = ops->cfg_irq_map_msg(vf, msg);
4425                 break;
4426         case VIRTCHNL_OP_CONFIG_RSS_KEY:
4427                 err = ops->config_rss_key(vf, msg);
4428                 break;
4429         case VIRTCHNL_OP_CONFIG_RSS_LUT:
4430                 err = ops->config_rss_lut(vf, msg);
4431                 break;
4432         case VIRTCHNL_OP_CONFIG_RSS_HFUNC:
4433                 err = ops->config_rss_hfunc(vf, msg);
4434                 break;
4435         case VIRTCHNL_OP_GET_STATS:
4436                 err = ops->get_stats_msg(vf, msg);
4437                 break;
4438         case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
4439                 err = ops->cfg_promiscuous_mode_msg(vf, msg);
4440                 break;
4441         case VIRTCHNL_OP_ADD_VLAN:
4442                 err = ops->add_vlan_msg(vf, msg);
4443                 break;
4444         case VIRTCHNL_OP_DEL_VLAN:
4445                 err = ops->remove_vlan_msg(vf, msg);
4446                 break;
4447         case VIRTCHNL_OP_GET_SUPPORTED_RXDIDS:
4448                 err = ops->query_rxdid(vf);
4449                 break;
4450         case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
4451                 err = ops->get_rss_hena(vf);
4452                 break;
4453         case VIRTCHNL_OP_SET_RSS_HENA:
4454                 err = ops->set_rss_hena_msg(vf, msg);
4455                 break;
4456         case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
4457                 err = ops->ena_vlan_stripping(vf);
4458                 break;
4459         case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
4460                 err = ops->dis_vlan_stripping(vf);
4461                 break;
4462         case VIRTCHNL_OP_ADD_FDIR_FILTER:
4463                 err = ops->add_fdir_fltr_msg(vf, msg);
4464                 break;
4465         case VIRTCHNL_OP_DEL_FDIR_FILTER:
4466                 err = ops->del_fdir_fltr_msg(vf, msg);
4467                 break;
4468         case VIRTCHNL_OP_ADD_RSS_CFG:
4469                 err = ops->handle_rss_cfg_msg(vf, msg, true);
4470                 break;
4471         case VIRTCHNL_OP_DEL_RSS_CFG:
4472                 err = ops->handle_rss_cfg_msg(vf, msg, false);
4473                 break;
4474         case VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS:
4475                 err = ops->get_offload_vlan_v2_caps(vf);
4476                 break;
4477         case VIRTCHNL_OP_ADD_VLAN_V2:
4478                 err = ops->add_vlan_v2_msg(vf, msg);
4479                 break;
4480         case VIRTCHNL_OP_DEL_VLAN_V2:
4481                 err = ops->remove_vlan_v2_msg(vf, msg);
4482                 break;
4483         case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2:
4484                 err = ops->ena_vlan_stripping_v2_msg(vf, msg);
4485                 break;
4486         case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2:
4487                 err = ops->dis_vlan_stripping_v2_msg(vf, msg);
4488                 break;
4489         case VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2:
4490                 err = ops->ena_vlan_insertion_v2_msg(vf, msg);
4491                 break;
4492         case VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2:
4493                 err = ops->dis_vlan_insertion_v2_msg(vf, msg);
4494                 break;
4495         case VIRTCHNL_OP_GET_QOS_CAPS:
4496                 err = ops->get_qos_caps(vf);
4497                 break;
4498         case VIRTCHNL_OP_CONFIG_QUEUE_BW:
4499                 err = ops->cfg_q_bw(vf, msg);
4500                 break;
4501         case VIRTCHNL_OP_CONFIG_QUANTA:
4502                 err = ops->cfg_q_quanta(vf, msg);
4503                 break;
4504         case VIRTCHNL_OP_UNKNOWN:
4505         default:
4506                 dev_err(dev, "Unsupported opcode %d from VF %d\n", v_opcode,
4507                         vf_id);
4508                 err = ice_vc_send_msg_to_vf(vf, v_opcode,
4509                                             VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4510                                             NULL, 0);
4511                 break;
4512         }
4513         if (err) {
4514                 /* Helper function cares less about error return values here
4515                  * as it is busy with pending work.
4516                  */
4517                 dev_info(dev, "PF failed to honor VF %d, opcode %d, error %d\n",
4518                          vf_id, v_opcode, err);
4519         }
4520
4521 finish:
4522         mutex_unlock(&vf->cfg_lock);
4523         ice_put_vf(vf);
4524 }
This page took 0.291218 seconds and 4 git commands to generate.