Merge tag 'amlogic-fixes-v5.17-rc-v2' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / drivers / net / ethernet / intel / ice / ice_virtchnl_pf.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3
4 #include "ice.h"
5 #include "ice_base.h"
6 #include "ice_lib.h"
7 #include "ice_fltr.h"
8 #include "ice_dcb_lib.h"
9 #include "ice_flow.h"
10 #include "ice_eswitch.h"
11 #include "ice_virtchnl_allowlist.h"
12 #include "ice_flex_pipe.h"
13
14 #define FIELD_SELECTOR(proto_hdr_field) \
15                 BIT((proto_hdr_field) & PROTO_HDR_FIELD_MASK)
16
17 struct ice_vc_hdr_match_type {
18         u32 vc_hdr;     /* virtchnl headers (VIRTCHNL_PROTO_HDR_XXX) */
19         u32 ice_hdr;    /* ice headers (ICE_FLOW_SEG_HDR_XXX) */
20 };
21
22 static const struct ice_vc_hdr_match_type ice_vc_hdr_list[] = {
23         {VIRTCHNL_PROTO_HDR_NONE,       ICE_FLOW_SEG_HDR_NONE},
24         {VIRTCHNL_PROTO_HDR_ETH,        ICE_FLOW_SEG_HDR_ETH},
25         {VIRTCHNL_PROTO_HDR_S_VLAN,     ICE_FLOW_SEG_HDR_VLAN},
26         {VIRTCHNL_PROTO_HDR_C_VLAN,     ICE_FLOW_SEG_HDR_VLAN},
27         {VIRTCHNL_PROTO_HDR_IPV4,       ICE_FLOW_SEG_HDR_IPV4 |
28                                         ICE_FLOW_SEG_HDR_IPV_OTHER},
29         {VIRTCHNL_PROTO_HDR_IPV6,       ICE_FLOW_SEG_HDR_IPV6 |
30                                         ICE_FLOW_SEG_HDR_IPV_OTHER},
31         {VIRTCHNL_PROTO_HDR_TCP,        ICE_FLOW_SEG_HDR_TCP},
32         {VIRTCHNL_PROTO_HDR_UDP,        ICE_FLOW_SEG_HDR_UDP},
33         {VIRTCHNL_PROTO_HDR_SCTP,       ICE_FLOW_SEG_HDR_SCTP},
34         {VIRTCHNL_PROTO_HDR_PPPOE,      ICE_FLOW_SEG_HDR_PPPOE},
35         {VIRTCHNL_PROTO_HDR_GTPU_IP,    ICE_FLOW_SEG_HDR_GTPU_IP},
36         {VIRTCHNL_PROTO_HDR_GTPU_EH,    ICE_FLOW_SEG_HDR_GTPU_EH},
37         {VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_DWN,
38                                         ICE_FLOW_SEG_HDR_GTPU_DWN},
39         {VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_UP,
40                                         ICE_FLOW_SEG_HDR_GTPU_UP},
41         {VIRTCHNL_PROTO_HDR_L2TPV3,     ICE_FLOW_SEG_HDR_L2TPV3},
42         {VIRTCHNL_PROTO_HDR_ESP,        ICE_FLOW_SEG_HDR_ESP},
43         {VIRTCHNL_PROTO_HDR_AH,         ICE_FLOW_SEG_HDR_AH},
44         {VIRTCHNL_PROTO_HDR_PFCP,       ICE_FLOW_SEG_HDR_PFCP_SESSION},
45 };
46
47 struct ice_vc_hash_field_match_type {
48         u32 vc_hdr;             /* virtchnl headers
49                                  * (VIRTCHNL_PROTO_HDR_XXX)
50                                  */
51         u32 vc_hash_field;      /* virtchnl hash fields selector
52                                  * FIELD_SELECTOR((VIRTCHNL_PROTO_HDR_ETH_XXX))
53                                  */
54         u64 ice_hash_field;     /* ice hash fields
55                                  * (BIT_ULL(ICE_FLOW_FIELD_IDX_XXX))
56                                  */
57 };
58
59 static const struct
60 ice_vc_hash_field_match_type ice_vc_hash_field_list[] = {
61         {VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC),
62                 BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_SA)},
63         {VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
64                 BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_DA)},
65         {VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC) |
66                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
67                 ICE_FLOW_HASH_ETH},
68         {VIRTCHNL_PROTO_HDR_ETH,
69                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_ETHERTYPE),
70                 BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_TYPE)},
71         {VIRTCHNL_PROTO_HDR_S_VLAN,
72                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_S_VLAN_ID),
73                 BIT_ULL(ICE_FLOW_FIELD_IDX_S_VLAN)},
74         {VIRTCHNL_PROTO_HDR_C_VLAN,
75                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_C_VLAN_ID),
76                 BIT_ULL(ICE_FLOW_FIELD_IDX_C_VLAN)},
77         {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC),
78                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA)},
79         {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
80                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA)},
81         {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
82                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
83                 ICE_FLOW_HASH_IPV4},
84         {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
85                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
86                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA) |
87                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
88         {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
89                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
90                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA) |
91                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
92         {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
93                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
94                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
95                 ICE_FLOW_HASH_IPV4 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
96         {VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
97                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
98         {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC),
99                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA)},
100         {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
101                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA)},
102         {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
103                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
104                 ICE_FLOW_HASH_IPV6},
105         {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
106                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
107                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA) |
108                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
109         {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
110                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
111                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA) |
112                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
113         {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
114                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
115                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
116                 ICE_FLOW_HASH_IPV6 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
117         {VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
118                 BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
119         {VIRTCHNL_PROTO_HDR_TCP,
120                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT),
121                 BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_SRC_PORT)},
122         {VIRTCHNL_PROTO_HDR_TCP,
123                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
124                 BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_DST_PORT)},
125         {VIRTCHNL_PROTO_HDR_TCP,
126                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT) |
127                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
128                 ICE_FLOW_HASH_TCP_PORT},
129         {VIRTCHNL_PROTO_HDR_UDP,
130                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT),
131                 BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_SRC_PORT)},
132         {VIRTCHNL_PROTO_HDR_UDP,
133                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
134                 BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_DST_PORT)},
135         {VIRTCHNL_PROTO_HDR_UDP,
136                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT) |
137                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
138                 ICE_FLOW_HASH_UDP_PORT},
139         {VIRTCHNL_PROTO_HDR_SCTP,
140                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT),
141                 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT)},
142         {VIRTCHNL_PROTO_HDR_SCTP,
143                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
144                 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_DST_PORT)},
145         {VIRTCHNL_PROTO_HDR_SCTP,
146                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT) |
147                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
148                 ICE_FLOW_HASH_SCTP_PORT},
149         {VIRTCHNL_PROTO_HDR_PPPOE,
150                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PPPOE_SESS_ID),
151                 BIT_ULL(ICE_FLOW_FIELD_IDX_PPPOE_SESS_ID)},
152         {VIRTCHNL_PROTO_HDR_GTPU_IP,
153                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_GTPU_IP_TEID),
154                 BIT_ULL(ICE_FLOW_FIELD_IDX_GTPU_IP_TEID)},
155         {VIRTCHNL_PROTO_HDR_L2TPV3,
156                 FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_L2TPV3_SESS_ID),
157                 BIT_ULL(ICE_FLOW_FIELD_IDX_L2TPV3_SESS_ID)},
158         {VIRTCHNL_PROTO_HDR_ESP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ESP_SPI),
159                 BIT_ULL(ICE_FLOW_FIELD_IDX_ESP_SPI)},
160         {VIRTCHNL_PROTO_HDR_AH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_AH_SPI),
161                 BIT_ULL(ICE_FLOW_FIELD_IDX_AH_SPI)},
162         {VIRTCHNL_PROTO_HDR_PFCP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PFCP_SEID),
163                 BIT_ULL(ICE_FLOW_FIELD_IDX_PFCP_SEID)},
164 };
165
166 /**
167  * ice_get_vf_vsi - get VF's VSI based on the stored index
168  * @vf: VF used to get VSI
169  */
170 struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf)
171 {
172         return vf->pf->vsi[vf->lan_vsi_idx];
173 }
174
175 /**
176  * ice_validate_vf_id - helper to check if VF ID is valid
177  * @pf: pointer to the PF structure
178  * @vf_id: the ID of the VF to check
179  */
180 static int ice_validate_vf_id(struct ice_pf *pf, u16 vf_id)
181 {
182         /* vf_id range is only valid for 0-255, and should always be unsigned */
183         if (vf_id >= pf->num_alloc_vfs) {
184                 dev_err(ice_pf_to_dev(pf), "Invalid VF ID: %u\n", vf_id);
185                 return -EINVAL;
186         }
187         return 0;
188 }
189
190 /**
191  * ice_check_vf_init - helper to check if VF init complete
192  * @pf: pointer to the PF structure
193  * @vf: the pointer to the VF to check
194  */
195 static int ice_check_vf_init(struct ice_pf *pf, struct ice_vf *vf)
196 {
197         if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
198                 dev_err(ice_pf_to_dev(pf), "VF ID: %u in reset. Try again.\n",
199                         vf->vf_id);
200                 return -EBUSY;
201         }
202         return 0;
203 }
204
205 /**
206  * ice_vc_vf_broadcast - Broadcast a message to all VFs on PF
207  * @pf: pointer to the PF structure
208  * @v_opcode: operation code
209  * @v_retval: return value
210  * @msg: pointer to the msg buffer
211  * @msglen: msg length
212  */
213 static void
214 ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
215                     enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
216 {
217         struct ice_hw *hw = &pf->hw;
218         unsigned int i;
219
220         ice_for_each_vf(pf, i) {
221                 struct ice_vf *vf = &pf->vf[i];
222
223                 /* Not all vfs are enabled so skip the ones that are not */
224                 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
225                     !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
226                         continue;
227
228                 /* Ignore return value on purpose - a given VF may fail, but
229                  * we need to keep going and send to all of them
230                  */
231                 ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
232                                       msglen, NULL);
233         }
234 }
235
236 /**
237  * ice_set_pfe_link - Set the link speed/status of the virtchnl_pf_event
238  * @vf: pointer to the VF structure
239  * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
240  * @ice_link_speed: link speed specified by ICE_AQ_LINK_SPEED_*
241  * @link_up: whether or not to set the link up/down
242  */
243 static void
244 ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
245                  int ice_link_speed, bool link_up)
246 {
247         if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
248                 pfe->event_data.link_event_adv.link_status = link_up;
249                 /* Speed in Mbps */
250                 pfe->event_data.link_event_adv.link_speed =
251                         ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
252         } else {
253                 pfe->event_data.link_event.link_status = link_up;
254                 /* Legacy method for virtchnl link speeds */
255                 pfe->event_data.link_event.link_speed =
256                         (enum virtchnl_link_speed)
257                         ice_conv_link_speed_to_virtchnl(false, ice_link_speed);
258         }
259 }
260
261 /**
262  * ice_vf_has_no_qs_ena - check if the VF has any Rx or Tx queues enabled
263  * @vf: the VF to check
264  *
265  * Returns true if the VF has no Rx and no Tx queues enabled and returns false
266  * otherwise
267  */
268 static bool ice_vf_has_no_qs_ena(struct ice_vf *vf)
269 {
270         return (!bitmap_weight(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF) &&
271                 !bitmap_weight(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF));
272 }
273
274 /**
275  * ice_is_vf_link_up - check if the VF's link is up
276  * @vf: VF to check if link is up
277  */
278 static bool ice_is_vf_link_up(struct ice_vf *vf)
279 {
280         struct ice_pf *pf = vf->pf;
281
282         if (ice_check_vf_init(pf, vf))
283                 return false;
284
285         if (ice_vf_has_no_qs_ena(vf))
286                 return false;
287         else if (vf->link_forced)
288                 return vf->link_up;
289         else
290                 return pf->hw.port_info->phy.link_info.link_info &
291                         ICE_AQ_LINK_UP;
292 }
293
294 /**
295  * ice_vc_notify_vf_link_state - Inform a VF of link status
296  * @vf: pointer to the VF structure
297  *
298  * send a link status message to a single VF
299  */
300 void ice_vc_notify_vf_link_state(struct ice_vf *vf)
301 {
302         struct virtchnl_pf_event pfe = { 0 };
303         struct ice_hw *hw = &vf->pf->hw;
304
305         pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
306         pfe.severity = PF_EVENT_SEVERITY_INFO;
307
308         if (ice_is_vf_link_up(vf))
309                 ice_set_pfe_link(vf, &pfe,
310                                  hw->port_info->phy.link_info.link_speed, true);
311         else
312                 ice_set_pfe_link(vf, &pfe, ICE_AQ_LINK_SPEED_UNKNOWN, false);
313
314         ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
315                               VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,
316                               sizeof(pfe), NULL);
317 }
318
319 /**
320  * ice_vf_invalidate_vsi - invalidate vsi_idx/vsi_num to remove VSI access
321  * @vf: VF to remove access to VSI for
322  */
323 static void ice_vf_invalidate_vsi(struct ice_vf *vf)
324 {
325         vf->lan_vsi_idx = ICE_NO_VSI;
326         vf->lan_vsi_num = ICE_NO_VSI;
327 }
328
329 /**
330  * ice_vf_vsi_release - invalidate the VF's VSI after freeing it
331  * @vf: invalidate this VF's VSI after freeing it
332  */
333 static void ice_vf_vsi_release(struct ice_vf *vf)
334 {
335         ice_vsi_release(ice_get_vf_vsi(vf));
336         ice_vf_invalidate_vsi(vf);
337 }
338
339 /**
340  * ice_vf_ctrl_invalidate_vsi - invalidate ctrl_vsi_idx to remove VSI access
341  * @vf: VF that control VSI is being invalidated on
342  */
343 static void ice_vf_ctrl_invalidate_vsi(struct ice_vf *vf)
344 {
345         vf->ctrl_vsi_idx = ICE_NO_VSI;
346 }
347
348 /**
349  * ice_vf_ctrl_vsi_release - invalidate the VF's control VSI after freeing it
350  * @vf: VF that control VSI is being released on
351  */
352 static void ice_vf_ctrl_vsi_release(struct ice_vf *vf)
353 {
354         ice_vsi_release(vf->pf->vsi[vf->ctrl_vsi_idx]);
355         ice_vf_ctrl_invalidate_vsi(vf);
356 }
357
358 /**
359  * ice_free_vf_res - Free a VF's resources
360  * @vf: pointer to the VF info
361  */
362 static void ice_free_vf_res(struct ice_vf *vf)
363 {
364         struct ice_pf *pf = vf->pf;
365         int i, last_vector_idx;
366
367         /* First, disable VF's configuration API to prevent OS from
368          * accessing the VF's VSI after it's freed or invalidated.
369          */
370         clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
371         ice_vf_fdir_exit(vf);
372         /* free VF control VSI */
373         if (vf->ctrl_vsi_idx != ICE_NO_VSI)
374                 ice_vf_ctrl_vsi_release(vf);
375
376         /* free VSI and disconnect it from the parent uplink */
377         if (vf->lan_vsi_idx != ICE_NO_VSI) {
378                 ice_vf_vsi_release(vf);
379                 vf->num_mac = 0;
380         }
381
382         last_vector_idx = vf->first_vector_idx + pf->num_msix_per_vf - 1;
383
384         /* clear VF MDD event information */
385         memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
386         memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
387
388         /* Disable interrupts so that VF starts in a known state */
389         for (i = vf->first_vector_idx; i <= last_vector_idx; i++) {
390                 wr32(&pf->hw, GLINT_DYN_CTL(i), GLINT_DYN_CTL_CLEARPBA_M);
391                 ice_flush(&pf->hw);
392         }
393         /* reset some of the state variables keeping track of the resources */
394         clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
395         clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
396 }
397
398 /**
399  * ice_dis_vf_mappings
400  * @vf: pointer to the VF structure
401  */
402 static void ice_dis_vf_mappings(struct ice_vf *vf)
403 {
404         struct ice_pf *pf = vf->pf;
405         struct ice_vsi *vsi;
406         struct device *dev;
407         int first, last, v;
408         struct ice_hw *hw;
409
410         hw = &pf->hw;
411         vsi = ice_get_vf_vsi(vf);
412
413         dev = ice_pf_to_dev(pf);
414         wr32(hw, VPINT_ALLOC(vf->vf_id), 0);
415         wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), 0);
416
417         first = vf->first_vector_idx;
418         last = first + pf->num_msix_per_vf - 1;
419         for (v = first; v <= last; v++) {
420                 u32 reg;
421
422                 reg = (((1 << GLINT_VECT2FUNC_IS_PF_S) &
423                         GLINT_VECT2FUNC_IS_PF_M) |
424                        ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
425                         GLINT_VECT2FUNC_PF_NUM_M));
426                 wr32(hw, GLINT_VECT2FUNC(v), reg);
427         }
428
429         if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
430                 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
431         else
432                 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
433
434         if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
435                 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
436         else
437                 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
438 }
439
440 /**
441  * ice_sriov_free_msix_res - Reset/free any used MSIX resources
442  * @pf: pointer to the PF structure
443  *
444  * Since no MSIX entries are taken from the pf->irq_tracker then just clear
445  * the pf->sriov_base_vector.
446  *
447  * Returns 0 on success, and -EINVAL on error.
448  */
449 static int ice_sriov_free_msix_res(struct ice_pf *pf)
450 {
451         struct ice_res_tracker *res;
452
453         if (!pf)
454                 return -EINVAL;
455
456         res = pf->irq_tracker;
457         if (!res)
458                 return -EINVAL;
459
460         /* give back irq_tracker resources used */
461         WARN_ON(pf->sriov_base_vector < res->num_entries);
462
463         pf->sriov_base_vector = 0;
464
465         return 0;
466 }
467
468 /**
469  * ice_set_vf_state_qs_dis - Set VF queues state to disabled
470  * @vf: pointer to the VF structure
471  */
472 void ice_set_vf_state_qs_dis(struct ice_vf *vf)
473 {
474         /* Clear Rx/Tx enabled queues flag */
475         bitmap_zero(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF);
476         bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
477         clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
478 }
479
480 /**
481  * ice_dis_vf_qs - Disable the VF queues
482  * @vf: pointer to the VF structure
483  */
484 static void ice_dis_vf_qs(struct ice_vf *vf)
485 {
486         struct ice_vsi *vsi = ice_get_vf_vsi(vf);
487
488         ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id);
489         ice_vsi_stop_all_rx_rings(vsi);
490         ice_set_vf_state_qs_dis(vf);
491 }
492
493 /**
494  * ice_free_vfs - Free all VFs
495  * @pf: pointer to the PF structure
496  */
497 void ice_free_vfs(struct ice_pf *pf)
498 {
499         struct device *dev = ice_pf_to_dev(pf);
500         struct ice_hw *hw = &pf->hw;
501         unsigned int tmp, i;
502
503         set_bit(ICE_VF_DEINIT_IN_PROGRESS, pf->state);
504
505         if (!pf->vf)
506                 return;
507
508         ice_eswitch_release(pf);
509
510         while (test_and_set_bit(ICE_VF_DIS, pf->state))
511                 usleep_range(1000, 2000);
512
513         /* Disable IOV before freeing resources. This lets any VF drivers
514          * running in the host get themselves cleaned up before we yank
515          * the carpet out from underneath their feet.
516          */
517         if (!pci_vfs_assigned(pf->pdev))
518                 pci_disable_sriov(pf->pdev);
519         else
520                 dev_warn(dev, "VFs are assigned - not disabling SR-IOV\n");
521
522         /* Avoid wait time by stopping all VFs at the same time */
523         ice_for_each_vf(pf, i)
524                 ice_dis_vf_qs(&pf->vf[i]);
525
526         tmp = pf->num_alloc_vfs;
527         pf->num_qps_per_vf = 0;
528         pf->num_alloc_vfs = 0;
529         for (i = 0; i < tmp; i++) {
530                 if (test_bit(ICE_VF_STATE_INIT, pf->vf[i].vf_states)) {
531                         /* disable VF qp mappings and set VF disable state */
532                         ice_dis_vf_mappings(&pf->vf[i]);
533                         set_bit(ICE_VF_STATE_DIS, pf->vf[i].vf_states);
534                         ice_free_vf_res(&pf->vf[i]);
535                 }
536
537                 mutex_destroy(&pf->vf[i].cfg_lock);
538         }
539
540         if (ice_sriov_free_msix_res(pf))
541                 dev_err(dev, "Failed to free MSIX resources used by SR-IOV\n");
542
543         devm_kfree(dev, pf->vf);
544         pf->vf = NULL;
545
546         /* This check is for when the driver is unloaded while VFs are
547          * assigned. Setting the number of VFs to 0 through sysfs is caught
548          * before this function ever gets called.
549          */
550         if (!pci_vfs_assigned(pf->pdev)) {
551                 unsigned int vf_id;
552
553                 /* Acknowledge VFLR for all VFs. Without this, VFs will fail to
554                  * work correctly when SR-IOV gets re-enabled.
555                  */
556                 for (vf_id = 0; vf_id < tmp; vf_id++) {
557                         u32 reg_idx, bit_idx;
558
559                         reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
560                         bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
561                         wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
562                 }
563         }
564
565         /* clear malicious info if the VFs are getting released */
566         for (i = 0; i < tmp; i++)
567                 if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->malvfs,
568                                         ICE_MAX_VF_COUNT, i))
569                         dev_dbg(dev, "failed to clear malicious VF state for VF %u\n",
570                                 i);
571
572         clear_bit(ICE_VF_DIS, pf->state);
573         clear_bit(ICE_VF_DEINIT_IN_PROGRESS, pf->state);
574         clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
575 }
576
577 /**
578  * ice_trigger_vf_reset - Reset a VF on HW
579  * @vf: pointer to the VF structure
580  * @is_vflr: true if VFLR was issued, false if not
581  * @is_pfr: true if the reset was triggered due to a previous PFR
582  *
583  * Trigger hardware to start a reset for a particular VF. Expects the caller
584  * to wait the proper amount of time to allow hardware to reset the VF before
585  * it cleans up and restores VF functionality.
586  */
587 static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr, bool is_pfr)
588 {
589         struct ice_pf *pf = vf->pf;
590         u32 reg, reg_idx, bit_idx;
591         unsigned int vf_abs_id, i;
592         struct device *dev;
593         struct ice_hw *hw;
594
595         dev = ice_pf_to_dev(pf);
596         hw = &pf->hw;
597         vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
598
599         /* Inform VF that it is no longer active, as a warning */
600         clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
601
602         /* Disable VF's configuration API during reset. The flag is re-enabled
603          * when it's safe again to access VF's VSI.
604          */
605         clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
606
607         /* VF_MBX_ARQLEN and VF_MBX_ATQLEN are cleared by PFR, so the driver
608          * needs to clear them in the case of VFR/VFLR. If this is done for
609          * PFR, it can mess up VF resets because the VF driver may already
610          * have started cleanup by the time we get here.
611          */
612         if (!is_pfr) {
613                 wr32(hw, VF_MBX_ARQLEN(vf->vf_id), 0);
614                 wr32(hw, VF_MBX_ATQLEN(vf->vf_id), 0);
615         }
616
617         /* In the case of a VFLR, the HW has already reset the VF and we
618          * just need to clean up, so don't hit the VFRTRIG register.
619          */
620         if (!is_vflr) {
621                 /* reset VF using VPGEN_VFRTRIG reg */
622                 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
623                 reg |= VPGEN_VFRTRIG_VFSWR_M;
624                 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
625         }
626         /* clear the VFLR bit in GLGEN_VFLRSTAT */
627         reg_idx = (vf_abs_id) / 32;
628         bit_idx = (vf_abs_id) % 32;
629         wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
630         ice_flush(hw);
631
632         wr32(hw, PF_PCI_CIAA,
633              VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
634         for (i = 0; i < ICE_PCI_CIAD_WAIT_COUNT; i++) {
635                 reg = rd32(hw, PF_PCI_CIAD);
636                 /* no transactions pending so stop polling */
637                 if ((reg & VF_TRANS_PENDING_M) == 0)
638                         break;
639
640                 dev_err(dev, "VF %u PCI transactions stuck\n", vf->vf_id);
641                 udelay(ICE_PCI_CIAD_WAIT_DELAY_US);
642         }
643 }
644
645 /**
646  * ice_vsi_manage_pvid - Enable or disable port VLAN for VSI
647  * @vsi: the VSI to update
648  * @pvid_info: VLAN ID and QoS used to set the PVID VSI context field
649  * @enable: true for enable PVID false for disable
650  */
651 static int ice_vsi_manage_pvid(struct ice_vsi *vsi, u16 pvid_info, bool enable)
652 {
653         struct ice_hw *hw = &vsi->back->hw;
654         struct ice_aqc_vsi_props *info;
655         struct ice_vsi_ctx *ctxt;
656         int ret;
657
658         ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
659         if (!ctxt)
660                 return -ENOMEM;
661
662         ctxt->info = vsi->info;
663         info = &ctxt->info;
664         if (enable) {
665                 info->vlan_flags = ICE_AQ_VSI_VLAN_MODE_UNTAGGED |
666                         ICE_AQ_VSI_PVLAN_INSERT_PVID |
667                         ICE_AQ_VSI_VLAN_EMOD_STR;
668                 info->sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
669         } else {
670                 info->vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING |
671                         ICE_AQ_VSI_VLAN_MODE_ALL;
672                 info->sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
673         }
674
675         info->pvid = cpu_to_le16(pvid_info);
676         info->valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
677                                            ICE_AQ_VSI_PROP_SW_VALID);
678
679         ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
680         if (ret) {
681                 dev_info(ice_hw_to_dev(hw), "update VSI for port VLAN failed, err %d aq_err %s\n",
682                          ret, ice_aq_str(hw->adminq.sq_last_status));
683                 goto out;
684         }
685
686         vsi->info.vlan_flags = info->vlan_flags;
687         vsi->info.sw_flags2 = info->sw_flags2;
688         vsi->info.pvid = info->pvid;
689 out:
690         kfree(ctxt);
691         return ret;
692 }
693
694 /**
695  * ice_vf_get_port_info - Get the VF's port info structure
696  * @vf: VF used to get the port info structure for
697  */
698 static struct ice_port_info *ice_vf_get_port_info(struct ice_vf *vf)
699 {
700         return vf->pf->hw.port_info;
701 }
702
703 /**
704  * ice_vf_vsi_setup - Set up a VF VSI
705  * @vf: VF to setup VSI for
706  *
707  * Returns pointer to the successfully allocated VSI struct on success,
708  * otherwise returns NULL on failure.
709  */
710 static struct ice_vsi *ice_vf_vsi_setup(struct ice_vf *vf)
711 {
712         struct ice_port_info *pi = ice_vf_get_port_info(vf);
713         struct ice_pf *pf = vf->pf;
714         struct ice_vsi *vsi;
715
716         vsi = ice_vsi_setup(pf, pi, ICE_VSI_VF, vf->vf_id, NULL);
717
718         if (!vsi) {
719                 dev_err(ice_pf_to_dev(pf), "Failed to create VF VSI\n");
720                 ice_vf_invalidate_vsi(vf);
721                 return NULL;
722         }
723
724         vf->lan_vsi_idx = vsi->idx;
725         vf->lan_vsi_num = vsi->vsi_num;
726
727         return vsi;
728 }
729
730 /**
731  * ice_vf_ctrl_vsi_setup - Set up a VF control VSI
732  * @vf: VF to setup control VSI for
733  *
734  * Returns pointer to the successfully allocated VSI struct on success,
735  * otherwise returns NULL on failure.
736  */
737 struct ice_vsi *ice_vf_ctrl_vsi_setup(struct ice_vf *vf)
738 {
739         struct ice_port_info *pi = ice_vf_get_port_info(vf);
740         struct ice_pf *pf = vf->pf;
741         struct ice_vsi *vsi;
742
743         vsi = ice_vsi_setup(pf, pi, ICE_VSI_CTRL, vf->vf_id, NULL);
744         if (!vsi) {
745                 dev_err(ice_pf_to_dev(pf), "Failed to create VF control VSI\n");
746                 ice_vf_ctrl_invalidate_vsi(vf);
747         }
748
749         return vsi;
750 }
751
752 /**
753  * ice_calc_vf_first_vector_idx - Calculate MSIX vector index in the PF space
754  * @pf: pointer to PF structure
755  * @vf: pointer to VF that the first MSIX vector index is being calculated for
756  *
757  * This returns the first MSIX vector index in PF space that is used by this VF.
758  * This index is used when accessing PF relative registers such as
759  * GLINT_VECT2FUNC and GLINT_DYN_CTL.
760  * This will always be the OICR index in the AVF driver so any functionality
761  * using vf->first_vector_idx for queue configuration will have to increment by
762  * 1 to avoid meddling with the OICR index.
763  */
764 static int ice_calc_vf_first_vector_idx(struct ice_pf *pf, struct ice_vf *vf)
765 {
766         return pf->sriov_base_vector + vf->vf_id * pf->num_msix_per_vf;
767 }
768
769 /**
770  * ice_vf_rebuild_host_tx_rate_cfg - re-apply the Tx rate limiting configuration
771  * @vf: VF to re-apply the configuration for
772  *
773  * Called after a VF VSI has been re-added/rebuild during reset. The PF driver
774  * needs to re-apply the host configured Tx rate limiting configuration.
775  */
776 static int ice_vf_rebuild_host_tx_rate_cfg(struct ice_vf *vf)
777 {
778         struct device *dev = ice_pf_to_dev(vf->pf);
779         struct ice_vsi *vsi = ice_get_vf_vsi(vf);
780         int err;
781
782         if (vf->min_tx_rate) {
783                 err = ice_set_min_bw_limit(vsi, (u64)vf->min_tx_rate * 1000);
784                 if (err) {
785                         dev_err(dev, "failed to set min Tx rate to %d Mbps for VF %u, error %d\n",
786                                 vf->min_tx_rate, vf->vf_id, err);
787                         return err;
788                 }
789         }
790
791         if (vf->max_tx_rate) {
792                 err = ice_set_max_bw_limit(vsi, (u64)vf->max_tx_rate * 1000);
793                 if (err) {
794                         dev_err(dev, "failed to set max Tx rate to %d Mbps for VF %u, error %d\n",
795                                 vf->max_tx_rate, vf->vf_id, err);
796                         return err;
797                 }
798         }
799
800         return 0;
801 }
802
803 /**
804  * ice_vf_rebuild_host_vlan_cfg - add VLAN 0 filter or rebuild the Port VLAN
805  * @vf: VF to add MAC filters for
806  *
807  * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
808  * always re-adds either a VLAN 0 or port VLAN based filter after reset.
809  */
810 static int ice_vf_rebuild_host_vlan_cfg(struct ice_vf *vf)
811 {
812         struct device *dev = ice_pf_to_dev(vf->pf);
813         struct ice_vsi *vsi = ice_get_vf_vsi(vf);
814         u16 vlan_id = 0;
815         int err;
816
817         if (vf->port_vlan_info) {
818                 err = ice_vsi_manage_pvid(vsi, vf->port_vlan_info, true);
819                 if (err) {
820                         dev_err(dev, "failed to configure port VLAN via VSI parameters for VF %u, error %d\n",
821                                 vf->vf_id, err);
822                         return err;
823                 }
824
825                 vlan_id = vf->port_vlan_info & VLAN_VID_MASK;
826         }
827
828         /* vlan_id will either be 0 or the port VLAN number */
829         err = ice_vsi_add_vlan(vsi, vlan_id, ICE_FWD_TO_VSI);
830         if (err) {
831                 dev_err(dev, "failed to add %s VLAN %u filter for VF %u, error %d\n",
832                         vf->port_vlan_info ? "port" : "", vlan_id, vf->vf_id,
833                         err);
834                 return err;
835         }
836
837         return 0;
838 }
839
840 /**
841  * ice_vf_rebuild_host_mac_cfg - add broadcast and the VF's perm_addr/LAA
842  * @vf: VF to add MAC filters for
843  *
844  * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
845  * always re-adds a broadcast filter and the VF's perm_addr/LAA after reset.
846  */
847 static int ice_vf_rebuild_host_mac_cfg(struct ice_vf *vf)
848 {
849         struct device *dev = ice_pf_to_dev(vf->pf);
850         struct ice_vsi *vsi = ice_get_vf_vsi(vf);
851         u8 broadcast[ETH_ALEN];
852         int status;
853
854         if (ice_is_eswitch_mode_switchdev(vf->pf))
855                 return 0;
856
857         eth_broadcast_addr(broadcast);
858         status = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
859         if (status) {
860                 dev_err(dev, "failed to add broadcast MAC filter for VF %u, error %d\n",
861                         vf->vf_id, status);
862                 return status;
863         }
864
865         vf->num_mac++;
866
867         if (is_valid_ether_addr(vf->hw_lan_addr.addr)) {
868                 status = ice_fltr_add_mac(vsi, vf->hw_lan_addr.addr,
869                                           ICE_FWD_TO_VSI);
870                 if (status) {
871                         dev_err(dev, "failed to add default unicast MAC filter %pM for VF %u, error %d\n",
872                                 &vf->hw_lan_addr.addr[0], vf->vf_id,
873                                 status);
874                         return status;
875                 }
876                 vf->num_mac++;
877
878                 ether_addr_copy(vf->dev_lan_addr.addr, vf->hw_lan_addr.addr);
879         }
880
881         return 0;
882 }
883
884 /**
885  * ice_vf_set_host_trust_cfg - set trust setting based on pre-reset value
886  * @vf: VF to configure trust setting for
887  */
888 static void ice_vf_set_host_trust_cfg(struct ice_vf *vf)
889 {
890         if (vf->trusted)
891                 set_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
892         else
893                 clear_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
894 }
895
896 /**
897  * ice_ena_vf_msix_mappings - enable VF MSIX mappings in hardware
898  * @vf: VF to enable MSIX mappings for
899  *
900  * Some of the registers need to be indexed/configured using hardware global
901  * device values and other registers need 0-based values, which represent PF
902  * based values.
903  */
904 static void ice_ena_vf_msix_mappings(struct ice_vf *vf)
905 {
906         int device_based_first_msix, device_based_last_msix;
907         int pf_based_first_msix, pf_based_last_msix, v;
908         struct ice_pf *pf = vf->pf;
909         int device_based_vf_id;
910         struct ice_hw *hw;
911         u32 reg;
912
913         hw = &pf->hw;
914         pf_based_first_msix = vf->first_vector_idx;
915         pf_based_last_msix = (pf_based_first_msix + pf->num_msix_per_vf) - 1;
916
917         device_based_first_msix = pf_based_first_msix +
918                 pf->hw.func_caps.common_cap.msix_vector_first_id;
919         device_based_last_msix =
920                 (device_based_first_msix + pf->num_msix_per_vf) - 1;
921         device_based_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
922
923         reg = (((device_based_first_msix << VPINT_ALLOC_FIRST_S) &
924                 VPINT_ALLOC_FIRST_M) |
925                ((device_based_last_msix << VPINT_ALLOC_LAST_S) &
926                 VPINT_ALLOC_LAST_M) | VPINT_ALLOC_VALID_M);
927         wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
928
929         reg = (((device_based_first_msix << VPINT_ALLOC_PCI_FIRST_S)
930                  & VPINT_ALLOC_PCI_FIRST_M) |
931                ((device_based_last_msix << VPINT_ALLOC_PCI_LAST_S) &
932                 VPINT_ALLOC_PCI_LAST_M) | VPINT_ALLOC_PCI_VALID_M);
933         wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg);
934
935         /* map the interrupts to its functions */
936         for (v = pf_based_first_msix; v <= pf_based_last_msix; v++) {
937                 reg = (((device_based_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
938                         GLINT_VECT2FUNC_VF_NUM_M) |
939                        ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
940                         GLINT_VECT2FUNC_PF_NUM_M));
941                 wr32(hw, GLINT_VECT2FUNC(v), reg);
942         }
943
944         /* Map mailbox interrupt to VF MSI-X vector 0 */
945         wr32(hw, VPINT_MBX_CTL(device_based_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M);
946 }
947
948 /**
949  * ice_ena_vf_q_mappings - enable Rx/Tx queue mappings for a VF
950  * @vf: VF to enable the mappings for
951  * @max_txq: max Tx queues allowed on the VF's VSI
952  * @max_rxq: max Rx queues allowed on the VF's VSI
953  */
954 static void ice_ena_vf_q_mappings(struct ice_vf *vf, u16 max_txq, u16 max_rxq)
955 {
956         struct device *dev = ice_pf_to_dev(vf->pf);
957         struct ice_vsi *vsi = ice_get_vf_vsi(vf);
958         struct ice_hw *hw = &vf->pf->hw;
959         u32 reg;
960
961         /* set regardless of mapping mode */
962         wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M);
963
964         /* VF Tx queues allocation */
965         if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
966                 /* set the VF PF Tx queue range
967                  * VFNUMQ value should be set to (number of queues - 1). A value
968                  * of 0 means 1 queue and a value of 255 means 256 queues
969                  */
970                 reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
971                         VPLAN_TX_QBASE_VFFIRSTQ_M) |
972                        (((max_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
973                         VPLAN_TX_QBASE_VFNUMQ_M));
974                 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
975         } else {
976                 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
977         }
978
979         /* set regardless of mapping mode */
980         wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M);
981
982         /* VF Rx queues allocation */
983         if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
984                 /* set the VF PF Rx queue range
985                  * VFNUMQ value should be set to (number of queues - 1). A value
986                  * of 0 means 1 queue and a value of 255 means 256 queues
987                  */
988                 reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
989                         VPLAN_RX_QBASE_VFFIRSTQ_M) |
990                        (((max_rxq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
991                         VPLAN_RX_QBASE_VFNUMQ_M));
992                 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
993         } else {
994                 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
995         }
996 }
997
998 /**
999  * ice_ena_vf_mappings - enable VF MSIX and queue mapping
1000  * @vf: pointer to the VF structure
1001  */
1002 static void ice_ena_vf_mappings(struct ice_vf *vf)
1003 {
1004         struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1005
1006         ice_ena_vf_msix_mappings(vf);
1007         ice_ena_vf_q_mappings(vf, vsi->alloc_txq, vsi->alloc_rxq);
1008 }
1009
1010 /**
1011  * ice_determine_res
1012  * @pf: pointer to the PF structure
1013  * @avail_res: available resources in the PF structure
1014  * @max_res: maximum resources that can be given per VF
1015  * @min_res: minimum resources that can be given per VF
1016  *
1017  * Returns non-zero value if resources (queues/vectors) are available or
1018  * returns zero if PF cannot accommodate for all num_alloc_vfs.
1019  */
1020 static int
1021 ice_determine_res(struct ice_pf *pf, u16 avail_res, u16 max_res, u16 min_res)
1022 {
1023         bool checked_min_res = false;
1024         int res;
1025
1026         /* start by checking if PF can assign max number of resources for
1027          * all num_alloc_vfs.
1028          * if yes, return number per VF
1029          * If no, divide by 2 and roundup, check again
1030          * repeat the loop till we reach a point where even minimum resources
1031          * are not available, in that case return 0
1032          */
1033         res = max_res;
1034         while ((res >= min_res) && !checked_min_res) {
1035                 int num_all_res;
1036
1037                 num_all_res = pf->num_alloc_vfs * res;
1038                 if (num_all_res <= avail_res)
1039                         return res;
1040
1041                 if (res == min_res)
1042                         checked_min_res = true;
1043
1044                 res = DIV_ROUND_UP(res, 2);
1045         }
1046         return 0;
1047 }
1048
1049 /**
1050  * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space
1051  * @vf: VF to calculate the register index for
1052  * @q_vector: a q_vector associated to the VF
1053  */
1054 int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector)
1055 {
1056         struct ice_pf *pf;
1057
1058         if (!vf || !q_vector)
1059                 return -EINVAL;
1060
1061         pf = vf->pf;
1062
1063         /* always add one to account for the OICR being the first MSIX */
1064         return pf->sriov_base_vector + pf->num_msix_per_vf * vf->vf_id +
1065                 q_vector->v_idx + 1;
1066 }
1067
1068 /**
1069  * ice_get_max_valid_res_idx - Get the max valid resource index
1070  * @res: pointer to the resource to find the max valid index for
1071  *
1072  * Start from the end of the ice_res_tracker and return right when we find the
1073  * first res->list entry with the ICE_RES_VALID_BIT set. This function is only
1074  * valid for SR-IOV because it is the only consumer that manipulates the
1075  * res->end and this is always called when res->end is set to res->num_entries.
1076  */
1077 static int ice_get_max_valid_res_idx(struct ice_res_tracker *res)
1078 {
1079         int i;
1080
1081         if (!res)
1082                 return -EINVAL;
1083
1084         for (i = res->num_entries - 1; i >= 0; i--)
1085                 if (res->list[i] & ICE_RES_VALID_BIT)
1086                         return i;
1087
1088         return 0;
1089 }
1090
1091 /**
1092  * ice_sriov_set_msix_res - Set any used MSIX resources
1093  * @pf: pointer to PF structure
1094  * @num_msix_needed: number of MSIX vectors needed for all SR-IOV VFs
1095  *
1096  * This function allows SR-IOV resources to be taken from the end of the PF's
1097  * allowed HW MSIX vectors so that the irq_tracker will not be affected. We
1098  * just set the pf->sriov_base_vector and return success.
1099  *
1100  * If there are not enough resources available, return an error. This should
1101  * always be caught by ice_set_per_vf_res().
1102  *
1103  * Return 0 on success, and -EINVAL when there are not enough MSIX vectors
1104  * in the PF's space available for SR-IOV.
1105  */
1106 static int ice_sriov_set_msix_res(struct ice_pf *pf, u16 num_msix_needed)
1107 {
1108         u16 total_vectors = pf->hw.func_caps.common_cap.num_msix_vectors;
1109         int vectors_used = pf->irq_tracker->num_entries;
1110         int sriov_base_vector;
1111
1112         sriov_base_vector = total_vectors - num_msix_needed;
1113
1114         /* make sure we only grab irq_tracker entries from the list end and
1115          * that we have enough available MSIX vectors
1116          */
1117         if (sriov_base_vector < vectors_used)
1118                 return -EINVAL;
1119
1120         pf->sriov_base_vector = sriov_base_vector;
1121
1122         return 0;
1123 }
1124
1125 /**
1126  * ice_set_per_vf_res - check if vectors and queues are available
1127  * @pf: pointer to the PF structure
1128  *
1129  * First, determine HW interrupts from common pool. If we allocate fewer VFs, we
1130  * get more vectors and can enable more queues per VF. Note that this does not
1131  * grab any vectors from the SW pool already allocated. Also note, that all
1132  * vector counts include one for each VF's miscellaneous interrupt vector
1133  * (i.e. OICR).
1134  *
1135  * Minimum VFs - 2 vectors, 1 queue pair
1136  * Small VFs - 5 vectors, 4 queue pairs
1137  * Medium VFs - 17 vectors, 16 queue pairs
1138  *
1139  * Second, determine number of queue pairs per VF by starting with a pre-defined
1140  * maximum each VF supports. If this is not possible, then we adjust based on
1141  * queue pairs available on the device.
1142  *
1143  * Lastly, set queue and MSI-X VF variables tracked by the PF so it can be used
1144  * by each VF during VF initialization and reset.
1145  */
1146 static int ice_set_per_vf_res(struct ice_pf *pf)
1147 {
1148         int max_valid_res_idx = ice_get_max_valid_res_idx(pf->irq_tracker);
1149         int msix_avail_per_vf, msix_avail_for_sriov;
1150         struct device *dev = ice_pf_to_dev(pf);
1151         u16 num_msix_per_vf, num_txq, num_rxq;
1152
1153         if (!pf->num_alloc_vfs || max_valid_res_idx < 0)
1154                 return -EINVAL;
1155
1156         /* determine MSI-X resources per VF */
1157         msix_avail_for_sriov = pf->hw.func_caps.common_cap.num_msix_vectors -
1158                 pf->irq_tracker->num_entries;
1159         msix_avail_per_vf = msix_avail_for_sriov / pf->num_alloc_vfs;
1160         if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MED) {
1161                 num_msix_per_vf = ICE_NUM_VF_MSIX_MED;
1162         } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_SMALL) {
1163                 num_msix_per_vf = ICE_NUM_VF_MSIX_SMALL;
1164         } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MULTIQ_MIN) {
1165                 num_msix_per_vf = ICE_NUM_VF_MSIX_MULTIQ_MIN;
1166         } else if (msix_avail_per_vf >= ICE_MIN_INTR_PER_VF) {
1167                 num_msix_per_vf = ICE_MIN_INTR_PER_VF;
1168         } else {
1169                 dev_err(dev, "Only %d MSI-X interrupts available for SR-IOV. Not enough to support minimum of %d MSI-X interrupts per VF for %d VFs\n",
1170                         msix_avail_for_sriov, ICE_MIN_INTR_PER_VF,
1171                         pf->num_alloc_vfs);
1172                 return -EIO;
1173         }
1174
1175         /* determine queue resources per VF */
1176         num_txq = ice_determine_res(pf, ice_get_avail_txq_count(pf),
1177                                     min_t(u16,
1178                                           num_msix_per_vf - ICE_NONQ_VECS_VF,
1179                                           ICE_MAX_RSS_QS_PER_VF),
1180                                     ICE_MIN_QS_PER_VF);
1181
1182         num_rxq = ice_determine_res(pf, ice_get_avail_rxq_count(pf),
1183                                     min_t(u16,
1184                                           num_msix_per_vf - ICE_NONQ_VECS_VF,
1185                                           ICE_MAX_RSS_QS_PER_VF),
1186                                     ICE_MIN_QS_PER_VF);
1187
1188         if (!num_txq || !num_rxq) {
1189                 dev_err(dev, "Not enough queues to support minimum of %d queue pairs per VF for %d VFs\n",
1190                         ICE_MIN_QS_PER_VF, pf->num_alloc_vfs);
1191                 return -EIO;
1192         }
1193
1194         if (ice_sriov_set_msix_res(pf, num_msix_per_vf * pf->num_alloc_vfs)) {
1195                 dev_err(dev, "Unable to set MSI-X resources for %d VFs\n",
1196                         pf->num_alloc_vfs);
1197                 return -EINVAL;
1198         }
1199
1200         /* only allow equal Tx/Rx queue count (i.e. queue pairs) */
1201         pf->num_qps_per_vf = min_t(int, num_txq, num_rxq);
1202         pf->num_msix_per_vf = num_msix_per_vf;
1203         dev_info(dev, "Enabling %d VFs with %d vectors and %d queues per VF\n",
1204                  pf->num_alloc_vfs, pf->num_msix_per_vf, pf->num_qps_per_vf);
1205
1206         return 0;
1207 }
1208
1209 /**
1210  * ice_clear_vf_reset_trigger - enable VF to access hardware
1211  * @vf: VF to enabled hardware access for
1212  */
1213 static void ice_clear_vf_reset_trigger(struct ice_vf *vf)
1214 {
1215         struct ice_hw *hw = &vf->pf->hw;
1216         u32 reg;
1217
1218         reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
1219         reg &= ~VPGEN_VFRTRIG_VFSWR_M;
1220         wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
1221         ice_flush(hw);
1222 }
1223
1224 static int
1225 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
1226 {
1227         struct ice_hw *hw = &vsi->back->hw;
1228         int status;
1229
1230         if (vf->port_vlan_info)
1231                 status = ice_fltr_set_vsi_promisc(hw, vsi->idx, promisc_m,
1232                                                   vf->port_vlan_info & VLAN_VID_MASK);
1233         else if (vsi->num_vlan > 1)
1234                 status = ice_fltr_set_vlan_vsi_promisc(hw, vsi, promisc_m);
1235         else
1236                 status = ice_fltr_set_vsi_promisc(hw, vsi->idx, promisc_m, 0);
1237
1238         if (status && status != -EEXIST) {
1239                 dev_err(ice_pf_to_dev(vsi->back), "enable Tx/Rx filter promiscuous mode on VF-%u failed, error: %d\n",
1240                         vf->vf_id, status);
1241                 return status;
1242         }
1243
1244         return 0;
1245 }
1246
1247 static int
1248 ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
1249 {
1250         struct ice_hw *hw = &vsi->back->hw;
1251         int status;
1252
1253         if (vf->port_vlan_info)
1254                 status = ice_fltr_clear_vsi_promisc(hw, vsi->idx, promisc_m,
1255                                                     vf->port_vlan_info & VLAN_VID_MASK);
1256         else if (vsi->num_vlan > 1)
1257                 status = ice_fltr_clear_vlan_vsi_promisc(hw, vsi, promisc_m);
1258         else
1259                 status = ice_fltr_clear_vsi_promisc(hw, vsi->idx, promisc_m, 0);
1260
1261         if (status && status != -ENOENT) {
1262                 dev_err(ice_pf_to_dev(vsi->back), "disable Tx/Rx filter promiscuous mode on VF-%u failed, error: %d\n",
1263                         vf->vf_id, status);
1264                 return status;
1265         }
1266
1267         return 0;
1268 }
1269
1270 static void ice_vf_clear_counters(struct ice_vf *vf)
1271 {
1272         struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1273
1274         vf->num_mac = 0;
1275         vsi->num_vlan = 0;
1276         memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
1277         memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
1278 }
1279
1280 /**
1281  * ice_vf_pre_vsi_rebuild - tasks to be done prior to VSI rebuild
1282  * @vf: VF to perform pre VSI rebuild tasks
1283  *
1284  * These tasks are items that don't need to be amortized since they are most
1285  * likely called in a for loop with all VF(s) in the reset_all_vfs() case.
1286  */
1287 static void ice_vf_pre_vsi_rebuild(struct ice_vf *vf)
1288 {
1289         ice_vf_clear_counters(vf);
1290         ice_clear_vf_reset_trigger(vf);
1291 }
1292
1293 /**
1294  * ice_vf_rebuild_aggregator_node_cfg - rebuild aggregator node config
1295  * @vsi: Pointer to VSI
1296  *
1297  * This function moves VSI into corresponding scheduler aggregator node
1298  * based on cached value of "aggregator node info" per VSI
1299  */
1300 static void ice_vf_rebuild_aggregator_node_cfg(struct ice_vsi *vsi)
1301 {
1302         struct ice_pf *pf = vsi->back;
1303         struct device *dev;
1304         int status;
1305
1306         if (!vsi->agg_node)
1307                 return;
1308
1309         dev = ice_pf_to_dev(pf);
1310         if (vsi->agg_node->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
1311                 dev_dbg(dev,
1312                         "agg_id %u already has reached max_num_vsis %u\n",
1313                         vsi->agg_node->agg_id, vsi->agg_node->num_vsis);
1314                 return;
1315         }
1316
1317         status = ice_move_vsi_to_agg(pf->hw.port_info, vsi->agg_node->agg_id,
1318                                      vsi->idx, vsi->tc_cfg.ena_tc);
1319         if (status)
1320                 dev_dbg(dev, "unable to move VSI idx %u into aggregator %u node",
1321                         vsi->idx, vsi->agg_node->agg_id);
1322         else
1323                 vsi->agg_node->num_vsis++;
1324 }
1325
1326 /**
1327  * ice_vf_rebuild_host_cfg - host admin configuration is persistent across reset
1328  * @vf: VF to rebuild host configuration on
1329  */
1330 static void ice_vf_rebuild_host_cfg(struct ice_vf *vf)
1331 {
1332         struct device *dev = ice_pf_to_dev(vf->pf);
1333         struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1334
1335         ice_vf_set_host_trust_cfg(vf);
1336
1337         if (ice_vf_rebuild_host_mac_cfg(vf))
1338                 dev_err(dev, "failed to rebuild default MAC configuration for VF %d\n",
1339                         vf->vf_id);
1340
1341         if (ice_vf_rebuild_host_vlan_cfg(vf))
1342                 dev_err(dev, "failed to rebuild VLAN configuration for VF %u\n",
1343                         vf->vf_id);
1344
1345         if (ice_vf_rebuild_host_tx_rate_cfg(vf))
1346                 dev_err(dev, "failed to rebuild Tx rate limiting configuration for VF %u\n",
1347                         vf->vf_id);
1348
1349         /* rebuild aggregator node config for main VF VSI */
1350         ice_vf_rebuild_aggregator_node_cfg(vsi);
1351 }
1352
1353 /**
1354  * ice_vf_rebuild_vsi_with_release - release and setup the VF's VSI
1355  * @vf: VF to release and setup the VSI for
1356  *
1357  * This is only called when a single VF is being reset (i.e. VFR, VFLR, host VF
1358  * configuration change, etc.).
1359  */
1360 static int ice_vf_rebuild_vsi_with_release(struct ice_vf *vf)
1361 {
1362         ice_vf_vsi_release(vf);
1363         if (!ice_vf_vsi_setup(vf))
1364                 return -ENOMEM;
1365
1366         return 0;
1367 }
1368
1369 /**
1370  * ice_vf_rebuild_vsi - rebuild the VF's VSI
1371  * @vf: VF to rebuild the VSI for
1372  *
1373  * This is only called when all VF(s) are being reset (i.e. PCIe Reset on the
1374  * host, PFR, CORER, etc.).
1375  */
1376 static int ice_vf_rebuild_vsi(struct ice_vf *vf)
1377 {
1378         struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1379         struct ice_pf *pf = vf->pf;
1380
1381         if (ice_vsi_rebuild(vsi, true)) {
1382                 dev_err(ice_pf_to_dev(pf), "failed to rebuild VF %d VSI\n",
1383                         vf->vf_id);
1384                 return -EIO;
1385         }
1386         /* vsi->idx will remain the same in this case so don't update
1387          * vf->lan_vsi_idx
1388          */
1389         vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
1390         vf->lan_vsi_num = vsi->vsi_num;
1391
1392         return 0;
1393 }
1394
1395 /**
1396  * ice_vf_set_initialized - VF is ready for VIRTCHNL communication
1397  * @vf: VF to set in initialized state
1398  *
1399  * After this function the VF will be ready to receive/handle the
1400  * VIRTCHNL_OP_GET_VF_RESOURCES message
1401  */
1402 static void ice_vf_set_initialized(struct ice_vf *vf)
1403 {
1404         ice_set_vf_state_qs_dis(vf);
1405         clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
1406         clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
1407         clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
1408         set_bit(ICE_VF_STATE_INIT, vf->vf_states);
1409 }
1410
1411 /**
1412  * ice_vf_post_vsi_rebuild - tasks to do after the VF's VSI have been rebuilt
1413  * @vf: VF to perform tasks on
1414  */
1415 static void ice_vf_post_vsi_rebuild(struct ice_vf *vf)
1416 {
1417         struct ice_pf *pf = vf->pf;
1418         struct ice_hw *hw;
1419
1420         hw = &pf->hw;
1421
1422         ice_vf_rebuild_host_cfg(vf);
1423
1424         ice_vf_set_initialized(vf);
1425         ice_ena_vf_mappings(vf);
1426         wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1427 }
1428
1429 /**
1430  * ice_reset_all_vfs - reset all allocated VFs in one go
1431  * @pf: pointer to the PF structure
1432  * @is_vflr: true if VFLR was issued, false if not
1433  *
1434  * First, tell the hardware to reset each VF, then do all the waiting in one
1435  * chunk, and finally finish restoring each VF after the wait. This is useful
1436  * during PF routines which need to reset all VFs, as otherwise it must perform
1437  * these resets in a serialized fashion.
1438  *
1439  * Returns true if any VFs were reset, and false otherwise.
1440  */
1441 bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr)
1442 {
1443         struct device *dev = ice_pf_to_dev(pf);
1444         struct ice_hw *hw = &pf->hw;
1445         struct ice_vf *vf;
1446         int v, i;
1447
1448         /* If we don't have any VFs, then there is nothing to reset */
1449         if (!pf->num_alloc_vfs)
1450                 return false;
1451
1452         /* clear all malicious info if the VFs are getting reset */
1453         ice_for_each_vf(pf, i)
1454                 if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->malvfs, ICE_MAX_VF_COUNT, i))
1455                         dev_dbg(dev, "failed to clear malicious VF state for VF %u\n", i);
1456
1457         /* If VFs have been disabled, there is no need to reset */
1458         if (test_and_set_bit(ICE_VF_DIS, pf->state))
1459                 return false;
1460
1461         /* Begin reset on all VFs at once */
1462         ice_for_each_vf(pf, v)
1463                 ice_trigger_vf_reset(&pf->vf[v], is_vflr, true);
1464
1465         /* HW requires some time to make sure it can flush the FIFO for a VF
1466          * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1467          * sequence to make sure that it has completed. We'll keep track of
1468          * the VFs using a simple iterator that increments once that VF has
1469          * finished resetting.
1470          */
1471         for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1472                 /* Check each VF in sequence */
1473                 while (v < pf->num_alloc_vfs) {
1474                         u32 reg;
1475
1476                         vf = &pf->vf[v];
1477                         reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1478                         if (!(reg & VPGEN_VFRSTAT_VFRD_M)) {
1479                                 /* only delay if the check failed */
1480                                 usleep_range(10, 20);
1481                                 break;
1482                         }
1483
1484                         /* If the current VF has finished resetting, move on
1485                          * to the next VF in sequence.
1486                          */
1487                         v++;
1488                 }
1489         }
1490
1491         /* Display a warning if at least one VF didn't manage to reset in
1492          * time, but continue on with the operation.
1493          */
1494         if (v < pf->num_alloc_vfs)
1495                 dev_warn(dev, "VF reset check timeout\n");
1496
1497         /* free VF resources to begin resetting the VSI state */
1498         ice_for_each_vf(pf, v) {
1499                 vf = &pf->vf[v];
1500
1501                 vf->driver_caps = 0;
1502                 ice_vc_set_default_allowlist(vf);
1503
1504                 ice_vf_fdir_exit(vf);
1505                 ice_vf_fdir_init(vf);
1506                 /* clean VF control VSI when resetting VFs since it should be
1507                  * setup only when VF creates its first FDIR rule.
1508                  */
1509                 if (vf->ctrl_vsi_idx != ICE_NO_VSI)
1510                         ice_vf_ctrl_invalidate_vsi(vf);
1511
1512                 ice_vf_pre_vsi_rebuild(vf);
1513                 ice_vf_rebuild_vsi(vf);
1514                 ice_vf_post_vsi_rebuild(vf);
1515         }
1516
1517         if (ice_is_eswitch_mode_switchdev(pf))
1518                 if (ice_eswitch_rebuild(pf))
1519                         dev_warn(dev, "eswitch rebuild failed\n");
1520
1521         ice_flush(hw);
1522         clear_bit(ICE_VF_DIS, pf->state);
1523
1524         return true;
1525 }
1526
1527 /**
1528  * ice_is_vf_disabled
1529  * @vf: pointer to the VF info
1530  *
1531  * Returns true if the PF or VF is disabled, false otherwise.
1532  */
1533 bool ice_is_vf_disabled(struct ice_vf *vf)
1534 {
1535         struct ice_pf *pf = vf->pf;
1536
1537         /* If the PF has been disabled, there is no need resetting VF until
1538          * PF is active again. Similarly, if the VF has been disabled, this
1539          * means something else is resetting the VF, so we shouldn't continue.
1540          * Otherwise, set disable VF state bit for actual reset, and continue.
1541          */
1542         return (test_bit(ICE_VF_DIS, pf->state) ||
1543                 test_bit(ICE_VF_STATE_DIS, vf->vf_states));
1544 }
1545
1546 /**
1547  * ice_reset_vf - Reset a particular VF
1548  * @vf: pointer to the VF structure
1549  * @is_vflr: true if VFLR was issued, false if not
1550  *
1551  * Returns true if the VF is currently in reset, resets successfully, or resets
1552  * are disabled and false otherwise.
1553  */
1554 bool ice_reset_vf(struct ice_vf *vf, bool is_vflr)
1555 {
1556         struct ice_pf *pf = vf->pf;
1557         struct ice_vsi *vsi;
1558         struct device *dev;
1559         struct ice_hw *hw;
1560         bool rsd = false;
1561         u8 promisc_m;
1562         u32 reg;
1563         int i;
1564
1565         dev = ice_pf_to_dev(pf);
1566
1567         if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
1568                 dev_dbg(dev, "Trying to reset VF %d, but all VF resets are disabled\n",
1569                         vf->vf_id);
1570                 return true;
1571         }
1572
1573         if (ice_is_vf_disabled(vf)) {
1574                 dev_dbg(dev, "VF is already disabled, there is no need for resetting it, telling VM, all is fine %d\n",
1575                         vf->vf_id);
1576                 return true;
1577         }
1578
1579         /* Set VF disable bit state here, before triggering reset */
1580         set_bit(ICE_VF_STATE_DIS, vf->vf_states);
1581         ice_trigger_vf_reset(vf, is_vflr, false);
1582
1583         vsi = ice_get_vf_vsi(vf);
1584
1585         ice_dis_vf_qs(vf);
1586
1587         /* Call Disable LAN Tx queue AQ whether or not queues are
1588          * enabled. This is needed for successful completion of VFR.
1589          */
1590         ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
1591                         NULL, ICE_VF_RESET, vf->vf_id, NULL);
1592
1593         hw = &pf->hw;
1594         /* poll VPGEN_VFRSTAT reg to make sure
1595          * that reset is complete
1596          */
1597         for (i = 0; i < 10; i++) {
1598                 /* VF reset requires driver to first reset the VF and then
1599                  * poll the status register to make sure that the reset
1600                  * completed successfully.
1601                  */
1602                 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1603                 if (reg & VPGEN_VFRSTAT_VFRD_M) {
1604                         rsd = true;
1605                         break;
1606                 }
1607
1608                 /* only sleep if the reset is not done */
1609                 usleep_range(10, 20);
1610         }
1611
1612         vf->driver_caps = 0;
1613         ice_vc_set_default_allowlist(vf);
1614
1615         /* Display a warning if VF didn't manage to reset in time, but need to
1616          * continue on with the operation.
1617          */
1618         if (!rsd)
1619                 dev_warn(dev, "VF reset check timeout on VF %d\n", vf->vf_id);
1620
1621         /* disable promiscuous modes in case they were enabled
1622          * ignore any error if disabling process failed
1623          */
1624         if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
1625             test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
1626                 if (vf->port_vlan_info || vsi->num_vlan)
1627                         promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
1628                 else
1629                         promisc_m = ICE_UCAST_PROMISC_BITS;
1630
1631                 if (ice_vf_clear_vsi_promisc(vf, vsi, promisc_m))
1632                         dev_err(dev, "disabling promiscuous mode failed\n");
1633         }
1634
1635         ice_eswitch_del_vf_mac_rule(vf);
1636
1637         ice_vf_fdir_exit(vf);
1638         ice_vf_fdir_init(vf);
1639         /* clean VF control VSI when resetting VF since it should be setup
1640          * only when VF creates its first FDIR rule.
1641          */
1642         if (vf->ctrl_vsi_idx != ICE_NO_VSI)
1643                 ice_vf_ctrl_vsi_release(vf);
1644
1645         ice_vf_pre_vsi_rebuild(vf);
1646
1647         if (ice_vf_rebuild_vsi_with_release(vf)) {
1648                 dev_err(dev, "Failed to release and setup the VF%u's VSI\n", vf->vf_id);
1649                 return false;
1650         }
1651
1652         ice_vf_post_vsi_rebuild(vf);
1653         vsi = ice_get_vf_vsi(vf);
1654         ice_eswitch_update_repr(vsi);
1655         ice_eswitch_replay_vf_mac_rule(vf);
1656
1657         /* if the VF has been reset allow it to come up again */
1658         if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->malvfs, ICE_MAX_VF_COUNT, vf->vf_id))
1659                 dev_dbg(dev, "failed to clear malicious VF state for VF %u\n", i);
1660
1661         return true;
1662 }
1663
1664 /**
1665  * ice_vc_notify_link_state - Inform all VFs on a PF of link status
1666  * @pf: pointer to the PF structure
1667  */
1668 void ice_vc_notify_link_state(struct ice_pf *pf)
1669 {
1670         int i;
1671
1672         ice_for_each_vf(pf, i)
1673                 ice_vc_notify_vf_link_state(&pf->vf[i]);
1674 }
1675
1676 /**
1677  * ice_vc_notify_reset - Send pending reset message to all VFs
1678  * @pf: pointer to the PF structure
1679  *
1680  * indicate a pending reset to all VFs on a given PF
1681  */
1682 void ice_vc_notify_reset(struct ice_pf *pf)
1683 {
1684         struct virtchnl_pf_event pfe;
1685
1686         if (!pf->num_alloc_vfs)
1687                 return;
1688
1689         pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1690         pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1691         ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
1692                             (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
1693 }
1694
1695 /**
1696  * ice_vc_notify_vf_reset - Notify VF of a reset event
1697  * @vf: pointer to the VF structure
1698  */
1699 static void ice_vc_notify_vf_reset(struct ice_vf *vf)
1700 {
1701         struct virtchnl_pf_event pfe;
1702         struct ice_pf *pf;
1703
1704         if (!vf)
1705                 return;
1706
1707         pf = vf->pf;
1708         if (ice_validate_vf_id(pf, vf->vf_id))
1709                 return;
1710
1711         /* Bail out if VF is in disabled state, neither initialized, nor active
1712          * state - otherwise proceed with notifications
1713          */
1714         if ((!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
1715              !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) ||
1716             test_bit(ICE_VF_STATE_DIS, vf->vf_states))
1717                 return;
1718
1719         pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1720         pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1721         ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, VIRTCHNL_OP_EVENT,
1722                               VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe, sizeof(pfe),
1723                               NULL);
1724 }
1725
1726 /**
1727  * ice_init_vf_vsi_res - initialize/setup VF VSI resources
1728  * @vf: VF to initialize/setup the VSI for
1729  *
1730  * This function creates a VSI for the VF, adds a VLAN 0 filter, and sets up the
1731  * VF VSI's broadcast filter and is only used during initial VF creation.
1732  */
1733 static int ice_init_vf_vsi_res(struct ice_vf *vf)
1734 {
1735         struct ice_pf *pf = vf->pf;
1736         u8 broadcast[ETH_ALEN];
1737         struct ice_vsi *vsi;
1738         struct device *dev;
1739         int err;
1740
1741         vf->first_vector_idx = ice_calc_vf_first_vector_idx(pf, vf);
1742
1743         dev = ice_pf_to_dev(pf);
1744         vsi = ice_vf_vsi_setup(vf);
1745         if (!vsi)
1746                 return -ENOMEM;
1747
1748         err = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
1749         if (err) {
1750                 dev_warn(dev, "Failed to add VLAN 0 filter for VF %d\n",
1751                          vf->vf_id);
1752                 goto release_vsi;
1753         }
1754
1755         eth_broadcast_addr(broadcast);
1756         err = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
1757         if (err) {
1758                 dev_err(dev, "Failed to add broadcast MAC filter for VF %d, error %d\n",
1759                         vf->vf_id, err);
1760                 goto release_vsi;
1761         }
1762
1763         vf->num_mac = 1;
1764
1765         return 0;
1766
1767 release_vsi:
1768         ice_vf_vsi_release(vf);
1769         return err;
1770 }
1771
1772 /**
1773  * ice_start_vfs - start VFs so they are ready to be used by SR-IOV
1774  * @pf: PF the VFs are associated with
1775  */
1776 static int ice_start_vfs(struct ice_pf *pf)
1777 {
1778         struct ice_hw *hw = &pf->hw;
1779         int retval, i;
1780
1781         ice_for_each_vf(pf, i) {
1782                 struct ice_vf *vf = &pf->vf[i];
1783
1784                 ice_clear_vf_reset_trigger(vf);
1785
1786                 retval = ice_init_vf_vsi_res(vf);
1787                 if (retval) {
1788                         dev_err(ice_pf_to_dev(pf), "Failed to initialize VSI resources for VF %d, error %d\n",
1789                                 vf->vf_id, retval);
1790                         goto teardown;
1791                 }
1792
1793                 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
1794                 ice_ena_vf_mappings(vf);
1795                 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1796         }
1797
1798         ice_flush(hw);
1799         return 0;
1800
1801 teardown:
1802         for (i = i - 1; i >= 0; i--) {
1803                 struct ice_vf *vf = &pf->vf[i];
1804
1805                 ice_dis_vf_mappings(vf);
1806                 ice_vf_vsi_release(vf);
1807         }
1808
1809         return retval;
1810 }
1811
1812 /**
1813  * ice_set_dflt_settings_vfs - set VF defaults during initialization/creation
1814  * @pf: PF holding reference to all VFs for default configuration
1815  */
1816 static void ice_set_dflt_settings_vfs(struct ice_pf *pf)
1817 {
1818         int i;
1819
1820         ice_for_each_vf(pf, i) {
1821                 struct ice_vf *vf = &pf->vf[i];
1822
1823                 vf->pf = pf;
1824                 vf->vf_id = i;
1825                 vf->vf_sw_id = pf->first_sw;
1826                 /* assign default capabilities */
1827                 set_bit(ICE_VIRTCHNL_VF_CAP_L2, &vf->vf_caps);
1828                 vf->spoofchk = true;
1829                 vf->num_vf_qs = pf->num_qps_per_vf;
1830                 ice_vc_set_default_allowlist(vf);
1831
1832                 /* ctrl_vsi_idx will be set to a valid value only when VF
1833                  * creates its first fdir rule.
1834                  */
1835                 ice_vf_ctrl_invalidate_vsi(vf);
1836                 ice_vf_fdir_init(vf);
1837
1838                 ice_vc_set_dflt_vf_ops(&vf->vc_ops);
1839
1840                 mutex_init(&vf->cfg_lock);
1841         }
1842 }
1843
1844 /**
1845  * ice_alloc_vfs - allocate num_vfs in the PF structure
1846  * @pf: PF to store the allocated VFs in
1847  * @num_vfs: number of VFs to allocate
1848  */
1849 static int ice_alloc_vfs(struct ice_pf *pf, int num_vfs)
1850 {
1851         struct ice_vf *vfs;
1852
1853         vfs = devm_kcalloc(ice_pf_to_dev(pf), num_vfs, sizeof(*vfs),
1854                            GFP_KERNEL);
1855         if (!vfs)
1856                 return -ENOMEM;
1857
1858         pf->vf = vfs;
1859         pf->num_alloc_vfs = num_vfs;
1860
1861         return 0;
1862 }
1863
1864 /**
1865  * ice_ena_vfs - enable VFs so they are ready to be used
1866  * @pf: pointer to the PF structure
1867  * @num_vfs: number of VFs to enable
1868  */
1869 static int ice_ena_vfs(struct ice_pf *pf, u16 num_vfs)
1870 {
1871         struct device *dev = ice_pf_to_dev(pf);
1872         struct ice_hw *hw = &pf->hw;
1873         int ret;
1874
1875         /* Disable global interrupt 0 so we don't try to handle the VFLR. */
1876         wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1877              ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
1878         set_bit(ICE_OICR_INTR_DIS, pf->state);
1879         ice_flush(hw);
1880
1881         ret = pci_enable_sriov(pf->pdev, num_vfs);
1882         if (ret) {
1883                 pf->num_alloc_vfs = 0;
1884                 goto err_unroll_intr;
1885         }
1886
1887         ret = ice_alloc_vfs(pf, num_vfs);
1888         if (ret)
1889                 goto err_pci_disable_sriov;
1890
1891         if (ice_set_per_vf_res(pf)) {
1892                 dev_err(dev, "Not enough resources for %d VFs, try with fewer number of VFs\n",
1893                         num_vfs);
1894                 ret = -ENOSPC;
1895                 goto err_unroll_sriov;
1896         }
1897
1898         ice_set_dflt_settings_vfs(pf);
1899
1900         if (ice_start_vfs(pf)) {
1901                 dev_err(dev, "Failed to start VF(s)\n");
1902                 ret = -EAGAIN;
1903                 goto err_unroll_sriov;
1904         }
1905
1906         clear_bit(ICE_VF_DIS, pf->state);
1907
1908         ret = ice_eswitch_configure(pf);
1909         if (ret)
1910                 goto err_unroll_sriov;
1911
1912         /* rearm global interrupts */
1913         if (test_and_clear_bit(ICE_OICR_INTR_DIS, pf->state))
1914                 ice_irq_dynamic_ena(hw, NULL, NULL);
1915
1916         return 0;
1917
1918 err_unroll_sriov:
1919         devm_kfree(dev, pf->vf);
1920         pf->vf = NULL;
1921         pf->num_alloc_vfs = 0;
1922 err_pci_disable_sriov:
1923         pci_disable_sriov(pf->pdev);
1924 err_unroll_intr:
1925         /* rearm interrupts here */
1926         ice_irq_dynamic_ena(hw, NULL, NULL);
1927         clear_bit(ICE_OICR_INTR_DIS, pf->state);
1928         return ret;
1929 }
1930
1931 /**
1932  * ice_pci_sriov_ena - Enable or change number of VFs
1933  * @pf: pointer to the PF structure
1934  * @num_vfs: number of VFs to allocate
1935  *
1936  * Returns 0 on success and negative on failure
1937  */
1938 static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
1939 {
1940         int pre_existing_vfs = pci_num_vf(pf->pdev);
1941         struct device *dev = ice_pf_to_dev(pf);
1942         int err;
1943
1944         if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1945                 ice_free_vfs(pf);
1946         else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1947                 return 0;
1948
1949         if (num_vfs > pf->num_vfs_supported) {
1950                 dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
1951                         num_vfs, pf->num_vfs_supported);
1952                 return -EOPNOTSUPP;
1953         }
1954
1955         dev_info(dev, "Enabling %d VFs\n", num_vfs);
1956         err = ice_ena_vfs(pf, num_vfs);
1957         if (err) {
1958                 dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
1959                 return err;
1960         }
1961
1962         set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
1963         return 0;
1964 }
1965
1966 /**
1967  * ice_check_sriov_allowed - check if SR-IOV is allowed based on various checks
1968  * @pf: PF to enabled SR-IOV on
1969  */
1970 static int ice_check_sriov_allowed(struct ice_pf *pf)
1971 {
1972         struct device *dev = ice_pf_to_dev(pf);
1973
1974         if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
1975                 dev_err(dev, "This device is not capable of SR-IOV\n");
1976                 return -EOPNOTSUPP;
1977         }
1978
1979         if (ice_is_safe_mode(pf)) {
1980                 dev_err(dev, "SR-IOV cannot be configured - Device is in Safe Mode\n");
1981                 return -EOPNOTSUPP;
1982         }
1983
1984         if (!ice_pf_state_is_nominal(pf)) {
1985                 dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
1986                 return -EBUSY;
1987         }
1988
1989         return 0;
1990 }
1991
1992 /**
1993  * ice_sriov_configure - Enable or change number of VFs via sysfs
1994  * @pdev: pointer to a pci_dev structure
1995  * @num_vfs: number of VFs to allocate or 0 to free VFs
1996  *
1997  * This function is called when the user updates the number of VFs in sysfs. On
1998  * success return whatever num_vfs was set to by the caller. Return negative on
1999  * failure.
2000  */
2001 int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
2002 {
2003         struct ice_pf *pf = pci_get_drvdata(pdev);
2004         struct device *dev = ice_pf_to_dev(pf);
2005         int err;
2006
2007         err = ice_check_sriov_allowed(pf);
2008         if (err)
2009                 return err;
2010
2011         if (!num_vfs) {
2012                 if (!pci_vfs_assigned(pdev)) {
2013                         ice_mbx_deinit_snapshot(&pf->hw);
2014                         ice_free_vfs(pf);
2015                         if (pf->lag)
2016                                 ice_enable_lag(pf->lag);
2017                         return 0;
2018                 }
2019
2020                 dev_err(dev, "can't free VFs because some are assigned to VMs.\n");
2021                 return -EBUSY;
2022         }
2023
2024         err = ice_mbx_init_snapshot(&pf->hw, num_vfs);
2025         if (err)
2026                 return err;
2027
2028         err = ice_pci_sriov_ena(pf, num_vfs);
2029         if (err) {
2030                 ice_mbx_deinit_snapshot(&pf->hw);
2031                 return err;
2032         }
2033
2034         if (pf->lag)
2035                 ice_disable_lag(pf->lag);
2036         return num_vfs;
2037 }
2038
2039 /**
2040  * ice_process_vflr_event - Free VF resources via IRQ calls
2041  * @pf: pointer to the PF structure
2042  *
2043  * called from the VFLR IRQ handler to
2044  * free up VF resources and state variables
2045  */
2046 void ice_process_vflr_event(struct ice_pf *pf)
2047 {
2048         struct ice_hw *hw = &pf->hw;
2049         unsigned int vf_id;
2050         u32 reg;
2051
2052         if (!test_and_clear_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
2053             !pf->num_alloc_vfs)
2054                 return;
2055
2056         ice_for_each_vf(pf, vf_id) {
2057                 struct ice_vf *vf = &pf->vf[vf_id];
2058                 u32 reg_idx, bit_idx;
2059
2060                 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
2061                 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
2062                 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
2063                 reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
2064                 if (reg & BIT(bit_idx))
2065                         /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
2066                         ice_reset_vf(vf, true);
2067         }
2068 }
2069
2070 /**
2071  * ice_vc_reset_vf - Perform software reset on the VF after informing the AVF
2072  * @vf: pointer to the VF info
2073  */
2074 static void ice_vc_reset_vf(struct ice_vf *vf)
2075 {
2076         ice_vc_notify_vf_reset(vf);
2077         ice_reset_vf(vf, false);
2078 }
2079
2080 /**
2081  * ice_get_vf_from_pfq - get the VF who owns the PF space queue passed in
2082  * @pf: PF used to index all VFs
2083  * @pfq: queue index relative to the PF's function space
2084  *
2085  * If no VF is found who owns the pfq then return NULL, otherwise return a
2086  * pointer to the VF who owns the pfq
2087  */
2088 static struct ice_vf *ice_get_vf_from_pfq(struct ice_pf *pf, u16 pfq)
2089 {
2090         unsigned int vf_id;
2091
2092         ice_for_each_vf(pf, vf_id) {
2093                 struct ice_vf *vf = &pf->vf[vf_id];
2094                 struct ice_vsi *vsi;
2095                 u16 rxq_idx;
2096
2097                 vsi = ice_get_vf_vsi(vf);
2098
2099                 ice_for_each_rxq(vsi, rxq_idx)
2100                         if (vsi->rxq_map[rxq_idx] == pfq)
2101                                 return vf;
2102         }
2103
2104         return NULL;
2105 }
2106
2107 /**
2108  * ice_globalq_to_pfq - convert from global queue index to PF space queue index
2109  * @pf: PF used for conversion
2110  * @globalq: global queue index used to convert to PF space queue index
2111  */
2112 static u32 ice_globalq_to_pfq(struct ice_pf *pf, u32 globalq)
2113 {
2114         return globalq - pf->hw.func_caps.common_cap.rxq_first_id;
2115 }
2116
2117 /**
2118  * ice_vf_lan_overflow_event - handle LAN overflow event for a VF
2119  * @pf: PF that the LAN overflow event happened on
2120  * @event: structure holding the event information for the LAN overflow event
2121  *
2122  * Determine if the LAN overflow event was caused by a VF queue. If it was not
2123  * caused by a VF, do nothing. If a VF caused this LAN overflow event trigger a
2124  * reset on the offending VF.
2125  */
2126 void
2127 ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event)
2128 {
2129         u32 gldcb_rtctq, queue;
2130         struct ice_vf *vf;
2131
2132         gldcb_rtctq = le32_to_cpu(event->desc.params.lan_overflow.prtdcb_ruptq);
2133         dev_dbg(ice_pf_to_dev(pf), "GLDCB_RTCTQ: 0x%08x\n", gldcb_rtctq);
2134
2135         /* event returns device global Rx queue number */
2136         queue = (gldcb_rtctq & GLDCB_RTCTQ_RXQNUM_M) >>
2137                 GLDCB_RTCTQ_RXQNUM_S;
2138
2139         vf = ice_get_vf_from_pfq(pf, ice_globalq_to_pfq(pf, queue));
2140         if (!vf)
2141                 return;
2142
2143         ice_vc_reset_vf(vf);
2144 }
2145
2146 /**
2147  * ice_vc_send_msg_to_vf - Send message to VF
2148  * @vf: pointer to the VF info
2149  * @v_opcode: virtual channel opcode
2150  * @v_retval: virtual channel return value
2151  * @msg: pointer to the msg buffer
2152  * @msglen: msg length
2153  *
2154  * send msg to VF
2155  */
2156 int
2157 ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
2158                       enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
2159 {
2160         struct device *dev;
2161         struct ice_pf *pf;
2162         int aq_ret;
2163
2164         if (!vf)
2165                 return -EINVAL;
2166
2167         pf = vf->pf;
2168         if (ice_validate_vf_id(pf, vf->vf_id))
2169                 return -EINVAL;
2170
2171         dev = ice_pf_to_dev(pf);
2172
2173         /* single place to detect unsuccessful return values */
2174         if (v_retval) {
2175                 vf->num_inval_msgs++;
2176                 dev_info(dev, "VF %d failed opcode %d, retval: %d\n", vf->vf_id,
2177                          v_opcode, v_retval);
2178                 if (vf->num_inval_msgs > ICE_DFLT_NUM_INVAL_MSGS_ALLOWED) {
2179                         dev_err(dev, "Number of invalid messages exceeded for VF %d\n",
2180                                 vf->vf_id);
2181                         dev_err(dev, "Use PF Control I/F to enable the VF\n");
2182                         set_bit(ICE_VF_STATE_DIS, vf->vf_states);
2183                         return -EIO;
2184                 }
2185         } else {
2186                 vf->num_valid_msgs++;
2187                 /* reset the invalid counter, if a valid message is received. */
2188                 vf->num_inval_msgs = 0;
2189         }
2190
2191         aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
2192                                        msg, msglen, NULL);
2193         if (aq_ret && pf->hw.mailboxq.sq_last_status != ICE_AQ_RC_ENOSYS) {
2194                 dev_info(dev, "Unable to send the message to VF %d ret %d aq_err %s\n",
2195                          vf->vf_id, aq_ret,
2196                          ice_aq_str(pf->hw.mailboxq.sq_last_status));
2197                 return -EIO;
2198         }
2199
2200         return 0;
2201 }
2202
2203 /**
2204  * ice_vc_get_ver_msg
2205  * @vf: pointer to the VF info
2206  * @msg: pointer to the msg buffer
2207  *
2208  * called from the VF to request the API version used by the PF
2209  */
2210 static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
2211 {
2212         struct virtchnl_version_info info = {
2213                 VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
2214         };
2215
2216         vf->vf_ver = *(struct virtchnl_version_info *)msg;
2217         /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
2218         if (VF_IS_V10(&vf->vf_ver))
2219                 info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
2220
2221         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
2222                                      VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
2223                                      sizeof(struct virtchnl_version_info));
2224 }
2225
2226 /**
2227  * ice_vc_get_max_frame_size - get max frame size allowed for VF
2228  * @vf: VF used to determine max frame size
2229  *
2230  * Max frame size is determined based on the current port's max frame size and
2231  * whether a port VLAN is configured on this VF. The VF is not aware whether
2232  * it's in a port VLAN so the PF needs to account for this in max frame size
2233  * checks and sending the max frame size to the VF.
2234  */
2235 static u16 ice_vc_get_max_frame_size(struct ice_vf *vf)
2236 {
2237         struct ice_port_info *pi = ice_vf_get_port_info(vf);
2238         u16 max_frame_size;
2239
2240         max_frame_size = pi->phy.link_info.max_frame_size;
2241
2242         if (vf->port_vlan_info)
2243                 max_frame_size -= VLAN_HLEN;
2244
2245         return max_frame_size;
2246 }
2247
2248 /**
2249  * ice_vc_get_vf_res_msg
2250  * @vf: pointer to the VF info
2251  * @msg: pointer to the msg buffer
2252  *
2253  * called from the VF to request its resources
2254  */
2255 static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
2256 {
2257         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2258         struct virtchnl_vf_resource *vfres = NULL;
2259         struct ice_pf *pf = vf->pf;
2260         struct ice_vsi *vsi;
2261         int len = 0;
2262         int ret;
2263
2264         if (ice_check_vf_init(pf, vf)) {
2265                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2266                 goto err;
2267         }
2268
2269         len = sizeof(struct virtchnl_vf_resource);
2270
2271         vfres = kzalloc(len, GFP_KERNEL);
2272         if (!vfres) {
2273                 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2274                 len = 0;
2275                 goto err;
2276         }
2277         if (VF_IS_V11(&vf->vf_ver))
2278                 vf->driver_caps = *(u32 *)msg;
2279         else
2280                 vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
2281                                   VIRTCHNL_VF_OFFLOAD_RSS_REG |
2282                                   VIRTCHNL_VF_OFFLOAD_VLAN;
2283
2284         vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
2285         vsi = ice_get_vf_vsi(vf);
2286         if (!vsi) {
2287                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2288                 goto err;
2289         }
2290
2291         if (!vsi->info.pvid)
2292                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
2293
2294         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2295                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
2296         } else {
2297                 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
2298                         vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
2299                 else
2300                         vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
2301         }
2302
2303         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_FDIR_PF)
2304                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_FDIR_PF;
2305
2306         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
2307                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
2308
2309         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
2310                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
2311
2312         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
2313                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
2314
2315         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
2316                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
2317
2318         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2319                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
2320
2321         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
2322                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
2323
2324         if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
2325                 vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
2326
2327         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF)
2328                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF;
2329
2330         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_USO)
2331                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_USO;
2332
2333         vfres->num_vsis = 1;
2334         /* Tx and Rx queue are equal for VF */
2335         vfres->num_queue_pairs = vsi->num_txq;
2336         vfres->max_vectors = pf->num_msix_per_vf;
2337         vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
2338         vfres->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
2339         vfres->max_mtu = ice_vc_get_max_frame_size(vf);
2340
2341         vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
2342         vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
2343         vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
2344         ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
2345                         vf->hw_lan_addr.addr);
2346
2347         /* match guest capabilities */
2348         vf->driver_caps = vfres->vf_cap_flags;
2349
2350         ice_vc_set_caps_allowlist(vf);
2351         ice_vc_set_working_allowlist(vf);
2352
2353         set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
2354
2355 err:
2356         /* send the response back to the VF */
2357         ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
2358                                     (u8 *)vfres, len);
2359
2360         kfree(vfres);
2361         return ret;
2362 }
2363
2364 /**
2365  * ice_vc_reset_vf_msg
2366  * @vf: pointer to the VF info
2367  *
2368  * called from the VF to reset itself,
2369  * unlike other virtchnl messages, PF driver
2370  * doesn't send the response back to the VF
2371  */
2372 static void ice_vc_reset_vf_msg(struct ice_vf *vf)
2373 {
2374         if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
2375                 ice_reset_vf(vf, false);
2376 }
2377
2378 /**
2379  * ice_find_vsi_from_id
2380  * @pf: the PF structure to search for the VSI
2381  * @id: ID of the VSI it is searching for
2382  *
2383  * searches for the VSI with the given ID
2384  */
2385 static struct ice_vsi *ice_find_vsi_from_id(struct ice_pf *pf, u16 id)
2386 {
2387         int i;
2388
2389         ice_for_each_vsi(pf, i)
2390                 if (pf->vsi[i] && pf->vsi[i]->vsi_num == id)
2391                         return pf->vsi[i];
2392
2393         return NULL;
2394 }
2395
2396 /**
2397  * ice_vc_isvalid_vsi_id
2398  * @vf: pointer to the VF info
2399  * @vsi_id: VF relative VSI ID
2400  *
2401  * check for the valid VSI ID
2402  */
2403 bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
2404 {
2405         struct ice_pf *pf = vf->pf;
2406         struct ice_vsi *vsi;
2407
2408         vsi = ice_find_vsi_from_id(pf, vsi_id);
2409
2410         return (vsi && (vsi->vf_id == vf->vf_id));
2411 }
2412
2413 /**
2414  * ice_vc_isvalid_q_id
2415  * @vf: pointer to the VF info
2416  * @vsi_id: VSI ID
2417  * @qid: VSI relative queue ID
2418  *
2419  * check for the valid queue ID
2420  */
2421 static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
2422 {
2423         struct ice_vsi *vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
2424         /* allocated Tx and Rx queues should be always equal for VF VSI */
2425         return (vsi && (qid < vsi->alloc_txq));
2426 }
2427
2428 /**
2429  * ice_vc_isvalid_ring_len
2430  * @ring_len: length of ring
2431  *
2432  * check for the valid ring count, should be multiple of ICE_REQ_DESC_MULTIPLE
2433  * or zero
2434  */
2435 static bool ice_vc_isvalid_ring_len(u16 ring_len)
2436 {
2437         return ring_len == 0 ||
2438                (ring_len >= ICE_MIN_NUM_DESC &&
2439                 ring_len <= ICE_MAX_NUM_DESC &&
2440                 !(ring_len % ICE_REQ_DESC_MULTIPLE));
2441 }
2442
2443 /**
2444  * ice_vc_validate_pattern
2445  * @vf: pointer to the VF info
2446  * @proto: virtchnl protocol headers
2447  *
2448  * validate the pattern is supported or not.
2449  *
2450  * Return: true on success, false on error.
2451  */
2452 bool
2453 ice_vc_validate_pattern(struct ice_vf *vf, struct virtchnl_proto_hdrs *proto)
2454 {
2455         bool is_ipv4 = false;
2456         bool is_ipv6 = false;
2457         bool is_udp = false;
2458         u16 ptype = -1;
2459         int i = 0;
2460
2461         while (i < proto->count &&
2462                proto->proto_hdr[i].type != VIRTCHNL_PROTO_HDR_NONE) {
2463                 switch (proto->proto_hdr[i].type) {
2464                 case VIRTCHNL_PROTO_HDR_ETH:
2465                         ptype = ICE_PTYPE_MAC_PAY;
2466                         break;
2467                 case VIRTCHNL_PROTO_HDR_IPV4:
2468                         ptype = ICE_PTYPE_IPV4_PAY;
2469                         is_ipv4 = true;
2470                         break;
2471                 case VIRTCHNL_PROTO_HDR_IPV6:
2472                         ptype = ICE_PTYPE_IPV6_PAY;
2473                         is_ipv6 = true;
2474                         break;
2475                 case VIRTCHNL_PROTO_HDR_UDP:
2476                         if (is_ipv4)
2477                                 ptype = ICE_PTYPE_IPV4_UDP_PAY;
2478                         else if (is_ipv6)
2479                                 ptype = ICE_PTYPE_IPV6_UDP_PAY;
2480                         is_udp = true;
2481                         break;
2482                 case VIRTCHNL_PROTO_HDR_TCP:
2483                         if (is_ipv4)
2484                                 ptype = ICE_PTYPE_IPV4_TCP_PAY;
2485                         else if (is_ipv6)
2486                                 ptype = ICE_PTYPE_IPV6_TCP_PAY;
2487                         break;
2488                 case VIRTCHNL_PROTO_HDR_SCTP:
2489                         if (is_ipv4)
2490                                 ptype = ICE_PTYPE_IPV4_SCTP_PAY;
2491                         else if (is_ipv6)
2492                                 ptype = ICE_PTYPE_IPV6_SCTP_PAY;
2493                         break;
2494                 case VIRTCHNL_PROTO_HDR_GTPU_IP:
2495                 case VIRTCHNL_PROTO_HDR_GTPU_EH:
2496                         if (is_ipv4)
2497                                 ptype = ICE_MAC_IPV4_GTPU;
2498                         else if (is_ipv6)
2499                                 ptype = ICE_MAC_IPV6_GTPU;
2500                         goto out;
2501                 case VIRTCHNL_PROTO_HDR_L2TPV3:
2502                         if (is_ipv4)
2503                                 ptype = ICE_MAC_IPV4_L2TPV3;
2504                         else if (is_ipv6)
2505                                 ptype = ICE_MAC_IPV6_L2TPV3;
2506                         goto out;
2507                 case VIRTCHNL_PROTO_HDR_ESP:
2508                         if (is_ipv4)
2509                                 ptype = is_udp ? ICE_MAC_IPV4_NAT_T_ESP :
2510                                                 ICE_MAC_IPV4_ESP;
2511                         else if (is_ipv6)
2512                                 ptype = is_udp ? ICE_MAC_IPV6_NAT_T_ESP :
2513                                                 ICE_MAC_IPV6_ESP;
2514                         goto out;
2515                 case VIRTCHNL_PROTO_HDR_AH:
2516                         if (is_ipv4)
2517                                 ptype = ICE_MAC_IPV4_AH;
2518                         else if (is_ipv6)
2519                                 ptype = ICE_MAC_IPV6_AH;
2520                         goto out;
2521                 case VIRTCHNL_PROTO_HDR_PFCP:
2522                         if (is_ipv4)
2523                                 ptype = ICE_MAC_IPV4_PFCP_SESSION;
2524                         else if (is_ipv6)
2525                                 ptype = ICE_MAC_IPV6_PFCP_SESSION;
2526                         goto out;
2527                 default:
2528                         break;
2529                 }
2530                 i++;
2531         }
2532
2533 out:
2534         return ice_hw_ptype_ena(&vf->pf->hw, ptype);
2535 }
2536
2537 /**
2538  * ice_vc_parse_rss_cfg - parses hash fields and headers from
2539  * a specific virtchnl RSS cfg
2540  * @hw: pointer to the hardware
2541  * @rss_cfg: pointer to the virtchnl RSS cfg
2542  * @addl_hdrs: pointer to the protocol header fields (ICE_FLOW_SEG_HDR_*)
2543  * to configure
2544  * @hash_flds: pointer to the hash bit fields (ICE_FLOW_HASH_*) to configure
2545  *
2546  * Return true if all the protocol header and hash fields in the RSS cfg could
2547  * be parsed, else return false
2548  *
2549  * This function parses the virtchnl RSS cfg to be the intended
2550  * hash fields and the intended header for RSS configuration
2551  */
2552 static bool
2553 ice_vc_parse_rss_cfg(struct ice_hw *hw, struct virtchnl_rss_cfg *rss_cfg,
2554                      u32 *addl_hdrs, u64 *hash_flds)
2555 {
2556         const struct ice_vc_hash_field_match_type *hf_list;
2557         const struct ice_vc_hdr_match_type *hdr_list;
2558         int i, hf_list_len, hdr_list_len;
2559
2560         hf_list = ice_vc_hash_field_list;
2561         hf_list_len = ARRAY_SIZE(ice_vc_hash_field_list);
2562         hdr_list = ice_vc_hdr_list;
2563         hdr_list_len = ARRAY_SIZE(ice_vc_hdr_list);
2564
2565         for (i = 0; i < rss_cfg->proto_hdrs.count; i++) {
2566                 struct virtchnl_proto_hdr *proto_hdr =
2567                                         &rss_cfg->proto_hdrs.proto_hdr[i];
2568                 bool hdr_found = false;
2569                 int j;
2570
2571                 /* Find matched ice headers according to virtchnl headers. */
2572                 for (j = 0; j < hdr_list_len; j++) {
2573                         struct ice_vc_hdr_match_type hdr_map = hdr_list[j];
2574
2575                         if (proto_hdr->type == hdr_map.vc_hdr) {
2576                                 *addl_hdrs |= hdr_map.ice_hdr;
2577                                 hdr_found = true;
2578                         }
2579                 }
2580
2581                 if (!hdr_found)
2582                         return false;
2583
2584                 /* Find matched ice hash fields according to
2585                  * virtchnl hash fields.
2586                  */
2587                 for (j = 0; j < hf_list_len; j++) {
2588                         struct ice_vc_hash_field_match_type hf_map = hf_list[j];
2589
2590                         if (proto_hdr->type == hf_map.vc_hdr &&
2591                             proto_hdr->field_selector == hf_map.vc_hash_field) {
2592                                 *hash_flds |= hf_map.ice_hash_field;
2593                                 break;
2594                         }
2595                 }
2596         }
2597
2598         return true;
2599 }
2600
2601 /**
2602  * ice_vf_adv_rss_offload_ena - determine if capabilities support advanced
2603  * RSS offloads
2604  * @caps: VF driver negotiated capabilities
2605  *
2606  * Return true if VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF capability is set,
2607  * else return false
2608  */
2609 static bool ice_vf_adv_rss_offload_ena(u32 caps)
2610 {
2611         return !!(caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF);
2612 }
2613
2614 /**
2615  * ice_vc_handle_rss_cfg
2616  * @vf: pointer to the VF info
2617  * @msg: pointer to the message buffer
2618  * @add: add a RSS config if true, otherwise delete a RSS config
2619  *
2620  * This function adds/deletes a RSS config
2621  */
2622 static int ice_vc_handle_rss_cfg(struct ice_vf *vf, u8 *msg, bool add)
2623 {
2624         u32 v_opcode = add ? VIRTCHNL_OP_ADD_RSS_CFG : VIRTCHNL_OP_DEL_RSS_CFG;
2625         struct virtchnl_rss_cfg *rss_cfg = (struct virtchnl_rss_cfg *)msg;
2626         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2627         struct device *dev = ice_pf_to_dev(vf->pf);
2628         struct ice_hw *hw = &vf->pf->hw;
2629         struct ice_vsi *vsi;
2630
2631         if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2632                 dev_dbg(dev, "VF %d attempting to configure RSS, but RSS is not supported by the PF\n",
2633                         vf->vf_id);
2634                 v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
2635                 goto error_param;
2636         }
2637
2638         if (!ice_vf_adv_rss_offload_ena(vf->driver_caps)) {
2639                 dev_dbg(dev, "VF %d attempting to configure RSS, but Advanced RSS offload is not supported\n",
2640                         vf->vf_id);
2641                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2642                 goto error_param;
2643         }
2644
2645         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2646                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2647                 goto error_param;
2648         }
2649
2650         if (rss_cfg->proto_hdrs.count > VIRTCHNL_MAX_NUM_PROTO_HDRS ||
2651             rss_cfg->rss_algorithm < VIRTCHNL_RSS_ALG_TOEPLITZ_ASYMMETRIC ||
2652             rss_cfg->rss_algorithm > VIRTCHNL_RSS_ALG_XOR_SYMMETRIC) {
2653                 dev_dbg(dev, "VF %d attempting to configure RSS, but RSS configuration is not valid\n",
2654                         vf->vf_id);
2655                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2656                 goto error_param;
2657         }
2658
2659         vsi = ice_get_vf_vsi(vf);
2660         if (!vsi) {
2661                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2662                 goto error_param;
2663         }
2664
2665         if (!ice_vc_validate_pattern(vf, &rss_cfg->proto_hdrs)) {
2666                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2667                 goto error_param;
2668         }
2669
2670         if (rss_cfg->rss_algorithm == VIRTCHNL_RSS_ALG_R_ASYMMETRIC) {
2671                 struct ice_vsi_ctx *ctx;
2672                 u8 lut_type, hash_type;
2673                 int status;
2674
2675                 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
2676                 hash_type = add ? ICE_AQ_VSI_Q_OPT_RSS_XOR :
2677                                 ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
2678
2679                 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2680                 if (!ctx) {
2681                         v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2682                         goto error_param;
2683                 }
2684
2685                 ctx->info.q_opt_rss = ((lut_type <<
2686                                         ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
2687                                        ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
2688                                        (hash_type &
2689                                         ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
2690
2691                 /* Preserve existing queueing option setting */
2692                 ctx->info.q_opt_rss |= (vsi->info.q_opt_rss &
2693                                           ICE_AQ_VSI_Q_OPT_RSS_GBL_LUT_M);
2694                 ctx->info.q_opt_tc = vsi->info.q_opt_tc;
2695                 ctx->info.q_opt_flags = vsi->info.q_opt_rss;
2696
2697                 ctx->info.valid_sections =
2698                                 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
2699
2700                 status = ice_update_vsi(hw, vsi->idx, ctx, NULL);
2701                 if (status) {
2702                         dev_err(dev, "update VSI for RSS failed, err %d aq_err %s\n",
2703                                 status, ice_aq_str(hw->adminq.sq_last_status));
2704                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2705                 } else {
2706                         vsi->info.q_opt_rss = ctx->info.q_opt_rss;
2707                 }
2708
2709                 kfree(ctx);
2710         } else {
2711                 u32 addl_hdrs = ICE_FLOW_SEG_HDR_NONE;
2712                 u64 hash_flds = ICE_HASH_INVALID;
2713
2714                 if (!ice_vc_parse_rss_cfg(hw, rss_cfg, &addl_hdrs,
2715                                           &hash_flds)) {
2716                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2717                         goto error_param;
2718                 }
2719
2720                 if (add) {
2721                         if (ice_add_rss_cfg(hw, vsi->idx, hash_flds,
2722                                             addl_hdrs)) {
2723                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2724                                 dev_err(dev, "ice_add_rss_cfg failed for vsi = %d, v_ret = %d\n",
2725                                         vsi->vsi_num, v_ret);
2726                         }
2727                 } else {
2728                         int status;
2729
2730                         status = ice_rem_rss_cfg(hw, vsi->idx, hash_flds,
2731                                                  addl_hdrs);
2732                         /* We just ignore -ENOENT, because if two configurations
2733                          * share the same profile remove one of them actually
2734                          * removes both, since the profile is deleted.
2735                          */
2736                         if (status && status != -ENOENT) {
2737                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2738                                 dev_err(dev, "ice_rem_rss_cfg failed for VF ID:%d, error:%d\n",
2739                                         vf->vf_id, status);
2740                         }
2741                 }
2742         }
2743
2744 error_param:
2745         return ice_vc_send_msg_to_vf(vf, v_opcode, v_ret, NULL, 0);
2746 }
2747
2748 /**
2749  * ice_vc_config_rss_key
2750  * @vf: pointer to the VF info
2751  * @msg: pointer to the msg buffer
2752  *
2753  * Configure the VF's RSS key
2754  */
2755 static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
2756 {
2757         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2758         struct virtchnl_rss_key *vrk =
2759                 (struct virtchnl_rss_key *)msg;
2760         struct ice_vsi *vsi;
2761
2762         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2763                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2764                 goto error_param;
2765         }
2766
2767         if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
2768                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2769                 goto error_param;
2770         }
2771
2772         if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
2773                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2774                 goto error_param;
2775         }
2776
2777         if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2778                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2779                 goto error_param;
2780         }
2781
2782         vsi = ice_get_vf_vsi(vf);
2783         if (!vsi) {
2784                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2785                 goto error_param;
2786         }
2787
2788         if (ice_set_rss_key(vsi, vrk->key))
2789                 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2790 error_param:
2791         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
2792                                      NULL, 0);
2793 }
2794
2795 /**
2796  * ice_vc_config_rss_lut
2797  * @vf: pointer to the VF info
2798  * @msg: pointer to the msg buffer
2799  *
2800  * Configure the VF's RSS LUT
2801  */
2802 static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
2803 {
2804         struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
2805         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2806         struct ice_vsi *vsi;
2807
2808         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2809                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2810                 goto error_param;
2811         }
2812
2813         if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
2814                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2815                 goto error_param;
2816         }
2817
2818         if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE) {
2819                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2820                 goto error_param;
2821         }
2822
2823         if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2824                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2825                 goto error_param;
2826         }
2827
2828         vsi = ice_get_vf_vsi(vf);
2829         if (!vsi) {
2830                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2831                 goto error_param;
2832         }
2833
2834         if (ice_set_rss_lut(vsi, vrl->lut, ICE_VSIQF_HLUT_ARRAY_SIZE))
2835                 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2836 error_param:
2837         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
2838                                      NULL, 0);
2839 }
2840
2841 /**
2842  * ice_wait_on_vf_reset - poll to make sure a given VF is ready after reset
2843  * @vf: The VF being resseting
2844  *
2845  * The max poll time is about ~800ms, which is about the maximum time it takes
2846  * for a VF to be reset and/or a VF driver to be removed.
2847  */
2848 static void ice_wait_on_vf_reset(struct ice_vf *vf)
2849 {
2850         int i;
2851
2852         for (i = 0; i < ICE_MAX_VF_RESET_TRIES; i++) {
2853                 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
2854                         break;
2855                 msleep(ICE_MAX_VF_RESET_SLEEP_MS);
2856         }
2857 }
2858
2859 /**
2860  * ice_check_vf_ready_for_cfg - check if VF is ready to be configured/queried
2861  * @vf: VF to check if it's ready to be configured/queried
2862  *
2863  * The purpose of this function is to make sure the VF is not in reset, not
2864  * disabled, and initialized so it can be configured and/or queried by a host
2865  * administrator.
2866  */
2867 int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
2868 {
2869         struct ice_pf *pf;
2870
2871         ice_wait_on_vf_reset(vf);
2872
2873         if (ice_is_vf_disabled(vf))
2874                 return -EINVAL;
2875
2876         pf = vf->pf;
2877         if (ice_check_vf_init(pf, vf))
2878                 return -EBUSY;
2879
2880         return 0;
2881 }
2882
2883 /**
2884  * ice_set_vf_spoofchk
2885  * @netdev: network interface device structure
2886  * @vf_id: VF identifier
2887  * @ena: flag to enable or disable feature
2888  *
2889  * Enable or disable VF spoof checking
2890  */
2891 int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
2892 {
2893         struct ice_netdev_priv *np = netdev_priv(netdev);
2894         struct ice_pf *pf = np->vsi->back;
2895         struct ice_vsi_ctx *ctx;
2896         struct ice_vsi *vf_vsi;
2897         struct device *dev;
2898         struct ice_vf *vf;
2899         int ret;
2900
2901         dev = ice_pf_to_dev(pf);
2902         if (ice_validate_vf_id(pf, vf_id))
2903                 return -EINVAL;
2904
2905         vf = &pf->vf[vf_id];
2906         ret = ice_check_vf_ready_for_cfg(vf);
2907         if (ret)
2908                 return ret;
2909
2910         vf_vsi = ice_get_vf_vsi(vf);
2911         if (!vf_vsi) {
2912                 netdev_err(netdev, "VSI %d for VF %d is null\n",
2913                            vf->lan_vsi_idx, vf->vf_id);
2914                 return -EINVAL;
2915         }
2916
2917         if (vf_vsi->type != ICE_VSI_VF) {
2918                 netdev_err(netdev, "Type %d of VSI %d for VF %d is no ICE_VSI_VF\n",
2919                            vf_vsi->type, vf_vsi->vsi_num, vf->vf_id);
2920                 return -ENODEV;
2921         }
2922
2923         if (ena == vf->spoofchk) {
2924                 dev_dbg(dev, "VF spoofchk already %s\n", ena ? "ON" : "OFF");
2925                 return 0;
2926         }
2927
2928         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2929         if (!ctx)
2930                 return -ENOMEM;
2931
2932         ctx->info.sec_flags = vf_vsi->info.sec_flags;
2933         ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
2934         if (ena) {
2935                 ctx->info.sec_flags |=
2936                         ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
2937                         (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2938                          ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
2939         } else {
2940                 ctx->info.sec_flags &=
2941                         ~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
2942                           (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2943                            ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
2944         }
2945
2946         ret = ice_update_vsi(&pf->hw, vf_vsi->idx, ctx, NULL);
2947         if (ret) {
2948                 dev_err(dev, "Failed to %sable spoofchk on VF %d VSI %d\n error %d\n",
2949                         ena ? "en" : "dis", vf->vf_id, vf_vsi->vsi_num, ret);
2950                 goto out;
2951         }
2952
2953         /* only update spoofchk state and VSI context on success */
2954         vf_vsi->info.sec_flags = ctx->info.sec_flags;
2955         vf->spoofchk = ena;
2956
2957 out:
2958         kfree(ctx);
2959         return ret;
2960 }
2961
2962 /**
2963  * ice_is_any_vf_in_promisc - check if any VF(s) are in promiscuous mode
2964  * @pf: PF structure for accessing VF(s)
2965  *
2966  * Return false if no VF(s) are in unicast and/or multicast promiscuous mode,
2967  * else return true
2968  */
2969 bool ice_is_any_vf_in_promisc(struct ice_pf *pf)
2970 {
2971         int vf_idx;
2972
2973         ice_for_each_vf(pf, vf_idx) {
2974                 struct ice_vf *vf = &pf->vf[vf_idx];
2975
2976                 /* found a VF that has promiscuous mode configured */
2977                 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2978                     test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
2979                         return true;
2980         }
2981
2982         return false;
2983 }
2984
2985 /**
2986  * ice_vc_cfg_promiscuous_mode_msg
2987  * @vf: pointer to the VF info
2988  * @msg: pointer to the msg buffer
2989  *
2990  * called from the VF to configure VF VSIs promiscuous mode
2991  */
2992 static int ice_vc_cfg_promiscuous_mode_msg(struct ice_vf *vf, u8 *msg)
2993 {
2994         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2995         bool rm_promisc, alluni = false, allmulti = false;
2996         struct virtchnl_promisc_info *info =
2997             (struct virtchnl_promisc_info *)msg;
2998         int mcast_err = 0, ucast_err = 0;
2999         struct ice_pf *pf = vf->pf;
3000         struct ice_vsi *vsi;
3001         struct device *dev;
3002         int ret = 0;
3003
3004         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3005                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3006                 goto error_param;
3007         }
3008
3009         if (!ice_vc_isvalid_vsi_id(vf, info->vsi_id)) {
3010                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3011                 goto error_param;
3012         }
3013
3014         vsi = ice_get_vf_vsi(vf);
3015         if (!vsi) {
3016                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3017                 goto error_param;
3018         }
3019
3020         dev = ice_pf_to_dev(pf);
3021         if (!test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
3022                 dev_err(dev, "Unprivileged VF %d is attempting to configure promiscuous mode\n",
3023                         vf->vf_id);
3024                 /* Leave v_ret alone, lie to the VF on purpose. */
3025                 goto error_param;
3026         }
3027
3028         if (info->flags & FLAG_VF_UNICAST_PROMISC)
3029                 alluni = true;
3030
3031         if (info->flags & FLAG_VF_MULTICAST_PROMISC)
3032                 allmulti = true;
3033
3034         rm_promisc = !allmulti && !alluni;
3035
3036         if (vsi->num_vlan || vf->port_vlan_info) {
3037                 if (rm_promisc)
3038                         ret = ice_cfg_vlan_pruning(vsi, true);
3039                 else
3040                         ret = ice_cfg_vlan_pruning(vsi, false);
3041                 if (ret) {
3042                         dev_err(dev, "Failed to configure VLAN pruning in promiscuous mode\n");
3043                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3044                         goto error_param;
3045                 }
3046         }
3047
3048         if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
3049                 bool set_dflt_vsi = alluni || allmulti;
3050
3051                 if (set_dflt_vsi && !ice_is_dflt_vsi_in_use(pf->first_sw))
3052                         /* only attempt to set the default forwarding VSI if
3053                          * it's not currently set
3054                          */
3055                         ret = ice_set_dflt_vsi(pf->first_sw, vsi);
3056                 else if (!set_dflt_vsi &&
3057                          ice_is_vsi_dflt_vsi(pf->first_sw, vsi))
3058                         /* only attempt to free the default forwarding VSI if we
3059                          * are the owner
3060                          */
3061                         ret = ice_clear_dflt_vsi(pf->first_sw);
3062
3063                 if (ret) {
3064                         dev_err(dev, "%sable VF %d as the default VSI failed, error %d\n",
3065                                 set_dflt_vsi ? "en" : "dis", vf->vf_id, ret);
3066                         v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
3067                         goto error_param;
3068                 }
3069         } else {
3070                 u8 mcast_m, ucast_m;
3071
3072                 if (vf->port_vlan_info || vsi->num_vlan > 1) {
3073                         mcast_m = ICE_MCAST_VLAN_PROMISC_BITS;
3074                         ucast_m = ICE_UCAST_VLAN_PROMISC_BITS;
3075                 } else {
3076                         mcast_m = ICE_MCAST_PROMISC_BITS;
3077                         ucast_m = ICE_UCAST_PROMISC_BITS;
3078                 }
3079
3080                 if (alluni)
3081                         ucast_err = ice_vf_set_vsi_promisc(vf, vsi, ucast_m);
3082                 else
3083                         ucast_err = ice_vf_clear_vsi_promisc(vf, vsi, ucast_m);
3084
3085                 if (allmulti)
3086                         mcast_err = ice_vf_set_vsi_promisc(vf, vsi, mcast_m);
3087                 else
3088                         mcast_err = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
3089
3090                 if (ucast_err || mcast_err)
3091                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3092         }
3093
3094         if (!mcast_err) {
3095                 if (allmulti &&
3096                     !test_and_set_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
3097                         dev_info(dev, "VF %u successfully set multicast promiscuous mode\n",
3098                                  vf->vf_id);
3099                 else if (!allmulti && test_and_clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
3100                         dev_info(dev, "VF %u successfully unset multicast promiscuous mode\n",
3101                                  vf->vf_id);
3102         }
3103
3104         if (!ucast_err) {
3105                 if (alluni && !test_and_set_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
3106                         dev_info(dev, "VF %u successfully set unicast promiscuous mode\n",
3107                                  vf->vf_id);
3108                 else if (!alluni && test_and_clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
3109                         dev_info(dev, "VF %u successfully unset unicast promiscuous mode\n",
3110                                  vf->vf_id);
3111         }
3112
3113 error_param:
3114         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
3115                                      v_ret, NULL, 0);
3116 }
3117
3118 /**
3119  * ice_vc_get_stats_msg
3120  * @vf: pointer to the VF info
3121  * @msg: pointer to the msg buffer
3122  *
3123  * called from the VF to get VSI stats
3124  */
3125 static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
3126 {
3127         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3128         struct virtchnl_queue_select *vqs =
3129                 (struct virtchnl_queue_select *)msg;
3130         struct ice_eth_stats stats = { 0 };
3131         struct ice_vsi *vsi;
3132
3133         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3134                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3135                 goto error_param;
3136         }
3137
3138         if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3139                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3140                 goto error_param;
3141         }
3142
3143         vsi = ice_get_vf_vsi(vf);
3144         if (!vsi) {
3145                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3146                 goto error_param;
3147         }
3148
3149         ice_update_eth_stats(vsi);
3150
3151         stats = vsi->eth_stats;
3152
3153 error_param:
3154         /* send the response to the VF */
3155         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
3156                                      (u8 *)&stats, sizeof(stats));
3157 }
3158
3159 /**
3160  * ice_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTCHNL
3161  * @vqs: virtchnl_queue_select structure containing bitmaps to validate
3162  *
3163  * Return true on successful validation, else false
3164  */
3165 static bool ice_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
3166 {
3167         if ((!vqs->rx_queues && !vqs->tx_queues) ||
3168             vqs->rx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF) ||
3169             vqs->tx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF))
3170                 return false;
3171
3172         return true;
3173 }
3174
3175 /**
3176  * ice_vf_ena_txq_interrupt - enable Tx queue interrupt via QINT_TQCTL
3177  * @vsi: VSI of the VF to configure
3178  * @q_idx: VF queue index used to determine the queue in the PF's space
3179  */
3180 static void ice_vf_ena_txq_interrupt(struct ice_vsi *vsi, u32 q_idx)
3181 {
3182         struct ice_hw *hw = &vsi->back->hw;
3183         u32 pfq = vsi->txq_map[q_idx];
3184         u32 reg;
3185
3186         reg = rd32(hw, QINT_TQCTL(pfq));
3187
3188         /* MSI-X index 0 in the VF's space is always for the OICR, which means
3189          * this is most likely a poll mode VF driver, so don't enable an
3190          * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
3191          */
3192         if (!(reg & QINT_TQCTL_MSIX_INDX_M))
3193                 return;
3194
3195         wr32(hw, QINT_TQCTL(pfq), reg | QINT_TQCTL_CAUSE_ENA_M);
3196 }
3197
3198 /**
3199  * ice_vf_ena_rxq_interrupt - enable Tx queue interrupt via QINT_RQCTL
3200  * @vsi: VSI of the VF to configure
3201  * @q_idx: VF queue index used to determine the queue in the PF's space
3202  */
3203 static void ice_vf_ena_rxq_interrupt(struct ice_vsi *vsi, u32 q_idx)
3204 {
3205         struct ice_hw *hw = &vsi->back->hw;
3206         u32 pfq = vsi->rxq_map[q_idx];
3207         u32 reg;
3208
3209         reg = rd32(hw, QINT_RQCTL(pfq));
3210
3211         /* MSI-X index 0 in the VF's space is always for the OICR, which means
3212          * this is most likely a poll mode VF driver, so don't enable an
3213          * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
3214          */
3215         if (!(reg & QINT_RQCTL_MSIX_INDX_M))
3216                 return;
3217
3218         wr32(hw, QINT_RQCTL(pfq), reg | QINT_RQCTL_CAUSE_ENA_M);
3219 }
3220
3221 /**
3222  * ice_vc_ena_qs_msg
3223  * @vf: pointer to the VF info
3224  * @msg: pointer to the msg buffer
3225  *
3226  * called from the VF to enable all or specific queue(s)
3227  */
3228 static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
3229 {
3230         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3231         struct virtchnl_queue_select *vqs =
3232             (struct virtchnl_queue_select *)msg;
3233         struct ice_vsi *vsi;
3234         unsigned long q_map;
3235         u16 vf_q_id;
3236
3237         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3238                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3239                 goto error_param;
3240         }
3241
3242         if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3243                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3244                 goto error_param;
3245         }
3246
3247         if (!ice_vc_validate_vqs_bitmaps(vqs)) {
3248                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3249                 goto error_param;
3250         }
3251
3252         vsi = ice_get_vf_vsi(vf);
3253         if (!vsi) {
3254                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3255                 goto error_param;
3256         }
3257
3258         /* Enable only Rx rings, Tx rings were enabled by the FW when the
3259          * Tx queue group list was configured and the context bits were
3260          * programmed using ice_vsi_cfg_txqs
3261          */
3262         q_map = vqs->rx_queues;
3263         for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3264                 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3265                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3266                         goto error_param;
3267                 }
3268
3269                 /* Skip queue if enabled */
3270                 if (test_bit(vf_q_id, vf->rxq_ena))
3271                         continue;
3272
3273                 if (ice_vsi_ctrl_one_rx_ring(vsi, true, vf_q_id, true)) {
3274                         dev_err(ice_pf_to_dev(vsi->back), "Failed to enable Rx ring %d on VSI %d\n",
3275                                 vf_q_id, vsi->vsi_num);
3276                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3277                         goto error_param;
3278                 }
3279
3280                 ice_vf_ena_rxq_interrupt(vsi, vf_q_id);
3281                 set_bit(vf_q_id, vf->rxq_ena);
3282         }
3283
3284         q_map = vqs->tx_queues;
3285         for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3286                 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3287                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3288                         goto error_param;
3289                 }
3290
3291                 /* Skip queue if enabled */
3292                 if (test_bit(vf_q_id, vf->txq_ena))
3293                         continue;
3294
3295                 ice_vf_ena_txq_interrupt(vsi, vf_q_id);
3296                 set_bit(vf_q_id, vf->txq_ena);
3297         }
3298
3299         /* Set flag to indicate that queues are enabled */
3300         if (v_ret == VIRTCHNL_STATUS_SUCCESS)
3301                 set_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
3302
3303 error_param:
3304         /* send the response to the VF */
3305         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
3306                                      NULL, 0);
3307 }
3308
3309 /**
3310  * ice_vc_dis_qs_msg
3311  * @vf: pointer to the VF info
3312  * @msg: pointer to the msg buffer
3313  *
3314  * called from the VF to disable all or specific
3315  * queue(s)
3316  */
3317 static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
3318 {
3319         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3320         struct virtchnl_queue_select *vqs =
3321             (struct virtchnl_queue_select *)msg;
3322         struct ice_vsi *vsi;
3323         unsigned long q_map;
3324         u16 vf_q_id;
3325
3326         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
3327             !test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states)) {
3328                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3329                 goto error_param;
3330         }
3331
3332         if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
3333                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3334                 goto error_param;
3335         }
3336
3337         if (!ice_vc_validate_vqs_bitmaps(vqs)) {
3338                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3339                 goto error_param;
3340         }
3341
3342         vsi = ice_get_vf_vsi(vf);
3343         if (!vsi) {
3344                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3345                 goto error_param;
3346         }
3347
3348         if (vqs->tx_queues) {
3349                 q_map = vqs->tx_queues;
3350
3351                 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3352                         struct ice_tx_ring *ring = vsi->tx_rings[vf_q_id];
3353                         struct ice_txq_meta txq_meta = { 0 };
3354
3355                         if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3356                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3357                                 goto error_param;
3358                         }
3359
3360                         /* Skip queue if not enabled */
3361                         if (!test_bit(vf_q_id, vf->txq_ena))
3362                                 continue;
3363
3364                         ice_fill_txq_meta(vsi, ring, &txq_meta);
3365
3366                         if (ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, vf->vf_id,
3367                                                  ring, &txq_meta)) {
3368                                 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Tx ring %d on VSI %d\n",
3369                                         vf_q_id, vsi->vsi_num);
3370                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3371                                 goto error_param;
3372                         }
3373
3374                         /* Clear enabled queues flag */
3375                         clear_bit(vf_q_id, vf->txq_ena);
3376                 }
3377         }
3378
3379         q_map = vqs->rx_queues;
3380         /* speed up Rx queue disable by batching them if possible */
3381         if (q_map &&
3382             bitmap_equal(&q_map, vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF)) {
3383                 if (ice_vsi_stop_all_rx_rings(vsi)) {
3384                         dev_err(ice_pf_to_dev(vsi->back), "Failed to stop all Rx rings on VSI %d\n",
3385                                 vsi->vsi_num);
3386                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3387                         goto error_param;
3388                 }
3389
3390                 bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
3391         } else if (q_map) {
3392                 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
3393                         if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
3394                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3395                                 goto error_param;
3396                         }
3397
3398                         /* Skip queue if not enabled */
3399                         if (!test_bit(vf_q_id, vf->rxq_ena))
3400                                 continue;
3401
3402                         if (ice_vsi_ctrl_one_rx_ring(vsi, false, vf_q_id,
3403                                                      true)) {
3404                                 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Rx ring %d on VSI %d\n",
3405                                         vf_q_id, vsi->vsi_num);
3406                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3407                                 goto error_param;
3408                         }
3409
3410                         /* Clear enabled queues flag */
3411                         clear_bit(vf_q_id, vf->rxq_ena);
3412                 }
3413         }
3414
3415         /* Clear enabled queues flag */
3416         if (v_ret == VIRTCHNL_STATUS_SUCCESS && ice_vf_has_no_qs_ena(vf))
3417                 clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
3418
3419 error_param:
3420         /* send the response to the VF */
3421         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
3422                                      NULL, 0);
3423 }
3424
3425 /**
3426  * ice_cfg_interrupt
3427  * @vf: pointer to the VF info
3428  * @vsi: the VSI being configured
3429  * @vector_id: vector ID
3430  * @map: vector map for mapping vectors to queues
3431  * @q_vector: structure for interrupt vector
3432  * configure the IRQ to queue map
3433  */
3434 static int
3435 ice_cfg_interrupt(struct ice_vf *vf, struct ice_vsi *vsi, u16 vector_id,
3436                   struct virtchnl_vector_map *map,
3437                   struct ice_q_vector *q_vector)
3438 {
3439         u16 vsi_q_id, vsi_q_id_idx;
3440         unsigned long qmap;
3441
3442         q_vector->num_ring_rx = 0;
3443         q_vector->num_ring_tx = 0;
3444
3445         qmap = map->rxq_map;
3446         for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
3447                 vsi_q_id = vsi_q_id_idx;
3448
3449                 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
3450                         return VIRTCHNL_STATUS_ERR_PARAM;
3451
3452                 q_vector->num_ring_rx++;
3453                 q_vector->rx.itr_idx = map->rxitr_idx;
3454                 vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
3455                 ice_cfg_rxq_interrupt(vsi, vsi_q_id, vector_id,
3456                                       q_vector->rx.itr_idx);
3457         }
3458
3459         qmap = map->txq_map;
3460         for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
3461                 vsi_q_id = vsi_q_id_idx;
3462
3463                 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
3464                         return VIRTCHNL_STATUS_ERR_PARAM;
3465
3466                 q_vector->num_ring_tx++;
3467                 q_vector->tx.itr_idx = map->txitr_idx;
3468                 vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
3469                 ice_cfg_txq_interrupt(vsi, vsi_q_id, vector_id,
3470                                       q_vector->tx.itr_idx);
3471         }
3472
3473         return VIRTCHNL_STATUS_SUCCESS;
3474 }
3475
3476 /**
3477  * ice_vc_cfg_irq_map_msg
3478  * @vf: pointer to the VF info
3479  * @msg: pointer to the msg buffer
3480  *
3481  * called from the VF to configure the IRQ to queue map
3482  */
3483 static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
3484 {
3485         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3486         u16 num_q_vectors_mapped, vsi_id, vector_id;
3487         struct virtchnl_irq_map_info *irqmap_info;
3488         struct virtchnl_vector_map *map;
3489         struct ice_pf *pf = vf->pf;
3490         struct ice_vsi *vsi;
3491         int i;
3492
3493         irqmap_info = (struct virtchnl_irq_map_info *)msg;
3494         num_q_vectors_mapped = irqmap_info->num_vectors;
3495
3496         /* Check to make sure number of VF vectors mapped is not greater than
3497          * number of VF vectors originally allocated, and check that
3498          * there is actually at least a single VF queue vector mapped
3499          */
3500         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
3501             pf->num_msix_per_vf < num_q_vectors_mapped ||
3502             !num_q_vectors_mapped) {
3503                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3504                 goto error_param;
3505         }
3506
3507         vsi = ice_get_vf_vsi(vf);
3508         if (!vsi) {
3509                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3510                 goto error_param;
3511         }
3512
3513         for (i = 0; i < num_q_vectors_mapped; i++) {
3514                 struct ice_q_vector *q_vector;
3515
3516                 map = &irqmap_info->vecmap[i];
3517
3518                 vector_id = map->vector_id;
3519                 vsi_id = map->vsi_id;
3520                 /* vector_id is always 0-based for each VF, and can never be
3521                  * larger than or equal to the max allowed interrupts per VF
3522                  */
3523                 if (!(vector_id < pf->num_msix_per_vf) ||
3524                     !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
3525                     (!vector_id && (map->rxq_map || map->txq_map))) {
3526                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3527                         goto error_param;
3528                 }
3529
3530                 /* No need to map VF miscellaneous or rogue vector */
3531                 if (!vector_id)
3532                         continue;
3533
3534                 /* Subtract non queue vector from vector_id passed by VF
3535                  * to get actual number of VSI queue vector array index
3536                  */
3537                 q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
3538                 if (!q_vector) {
3539                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3540                         goto error_param;
3541                 }
3542
3543                 /* lookout for the invalid queue index */
3544                 v_ret = (enum virtchnl_status_code)
3545                         ice_cfg_interrupt(vf, vsi, vector_id, map, q_vector);
3546                 if (v_ret)
3547                         goto error_param;
3548         }
3549
3550 error_param:
3551         /* send the response to the VF */
3552         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
3553                                      NULL, 0);
3554 }
3555
3556 /**
3557  * ice_vc_cfg_qs_msg
3558  * @vf: pointer to the VF info
3559  * @msg: pointer to the msg buffer
3560  *
3561  * called from the VF to configure the Rx/Tx queues
3562  */
3563 static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
3564 {
3565         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3566         struct virtchnl_vsi_queue_config_info *qci =
3567             (struct virtchnl_vsi_queue_config_info *)msg;
3568         struct virtchnl_queue_pair_info *qpi;
3569         struct ice_pf *pf = vf->pf;
3570         struct ice_vsi *vsi;
3571         int i, q_idx;
3572
3573         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3574                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3575                 goto error_param;
3576         }
3577
3578         if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
3579                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3580                 goto error_param;
3581         }
3582
3583         vsi = ice_get_vf_vsi(vf);
3584         if (!vsi) {
3585                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3586                 goto error_param;
3587         }
3588
3589         if (qci->num_queue_pairs > ICE_MAX_RSS_QS_PER_VF ||
3590             qci->num_queue_pairs > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
3591                 dev_err(ice_pf_to_dev(pf), "VF-%d requesting more than supported number of queues: %d\n",
3592                         vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
3593                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3594                 goto error_param;
3595         }
3596
3597         for (i = 0; i < qci->num_queue_pairs; i++) {
3598                 qpi = &qci->qpair[i];
3599                 if (qpi->txq.vsi_id != qci->vsi_id ||
3600                     qpi->rxq.vsi_id != qci->vsi_id ||
3601                     qpi->rxq.queue_id != qpi->txq.queue_id ||
3602                     qpi->txq.headwb_enabled ||
3603                     !ice_vc_isvalid_ring_len(qpi->txq.ring_len) ||
3604                     !ice_vc_isvalid_ring_len(qpi->rxq.ring_len) ||
3605                     !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
3606                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3607                         goto error_param;
3608                 }
3609
3610                 q_idx = qpi->rxq.queue_id;
3611
3612                 /* make sure selected "q_idx" is in valid range of queues
3613                  * for selected "vsi"
3614                  */
3615                 if (q_idx >= vsi->alloc_txq || q_idx >= vsi->alloc_rxq) {
3616                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3617                         goto error_param;
3618                 }
3619
3620                 /* copy Tx queue info from VF into VSI */
3621                 if (qpi->txq.ring_len > 0) {
3622                         vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
3623                         vsi->tx_rings[i]->count = qpi->txq.ring_len;
3624                         if (ice_vsi_cfg_single_txq(vsi, vsi->tx_rings, q_idx)) {
3625                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3626                                 goto error_param;
3627                         }
3628                 }
3629
3630                 /* copy Rx queue info from VF into VSI */
3631                 if (qpi->rxq.ring_len > 0) {
3632                         u16 max_frame_size = ice_vc_get_max_frame_size(vf);
3633
3634                         vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
3635                         vsi->rx_rings[i]->count = qpi->rxq.ring_len;
3636
3637                         if (qpi->rxq.databuffer_size != 0 &&
3638                             (qpi->rxq.databuffer_size > ((16 * 1024) - 128) ||
3639                              qpi->rxq.databuffer_size < 1024)) {
3640                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3641                                 goto error_param;
3642                         }
3643                         vsi->rx_buf_len = qpi->rxq.databuffer_size;
3644                         vsi->rx_rings[i]->rx_buf_len = vsi->rx_buf_len;
3645                         if (qpi->rxq.max_pkt_size > max_frame_size ||
3646                             qpi->rxq.max_pkt_size < 64) {
3647                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3648                                 goto error_param;
3649                         }
3650
3651                         vsi->max_frame = qpi->rxq.max_pkt_size;
3652                         /* add space for the port VLAN since the VF driver is not
3653                          * expected to account for it in the MTU calculation
3654                          */
3655                         if (vf->port_vlan_info)
3656                                 vsi->max_frame += VLAN_HLEN;
3657
3658                         if (ice_vsi_cfg_single_rxq(vsi, q_idx)) {
3659                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3660                                 goto error_param;
3661                         }
3662                 }
3663         }
3664
3665 error_param:
3666         /* send the response to the VF */
3667         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES, v_ret,
3668                                      NULL, 0);
3669 }
3670
3671 /**
3672  * ice_is_vf_trusted
3673  * @vf: pointer to the VF info
3674  */
3675 static bool ice_is_vf_trusted(struct ice_vf *vf)
3676 {
3677         return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
3678 }
3679
3680 /**
3681  * ice_can_vf_change_mac
3682  * @vf: pointer to the VF info
3683  *
3684  * Return true if the VF is allowed to change its MAC filters, false otherwise
3685  */
3686 static bool ice_can_vf_change_mac(struct ice_vf *vf)
3687 {
3688         /* If the VF MAC address has been set administratively (via the
3689          * ndo_set_vf_mac command), then deny permission to the VF to
3690          * add/delete unicast MAC addresses, unless the VF is trusted
3691          */
3692         if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
3693                 return false;
3694
3695         return true;
3696 }
3697
3698 /**
3699  * ice_vc_ether_addr_type - get type of virtchnl_ether_addr
3700  * @vc_ether_addr: used to extract the type
3701  */
3702 static u8
3703 ice_vc_ether_addr_type(struct virtchnl_ether_addr *vc_ether_addr)
3704 {
3705         return (vc_ether_addr->type & VIRTCHNL_ETHER_ADDR_TYPE_MASK);
3706 }
3707
3708 /**
3709  * ice_is_vc_addr_legacy - check if the MAC address is from an older VF
3710  * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
3711  */
3712 static bool
3713 ice_is_vc_addr_legacy(struct virtchnl_ether_addr *vc_ether_addr)
3714 {
3715         u8 type = ice_vc_ether_addr_type(vc_ether_addr);
3716
3717         return (type == VIRTCHNL_ETHER_ADDR_LEGACY);
3718 }
3719
3720 /**
3721  * ice_is_vc_addr_primary - check if the MAC address is the VF's primary MAC
3722  * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
3723  *
3724  * This function should only be called when the MAC address in
3725  * virtchnl_ether_addr is a valid unicast MAC
3726  */
3727 static bool
3728 ice_is_vc_addr_primary(struct virtchnl_ether_addr __maybe_unused *vc_ether_addr)
3729 {
3730         u8 type = ice_vc_ether_addr_type(vc_ether_addr);
3731
3732         return (type == VIRTCHNL_ETHER_ADDR_PRIMARY);
3733 }
3734
3735 /**
3736  * ice_vfhw_mac_add - update the VF's cached hardware MAC if allowed
3737  * @vf: VF to update
3738  * @vc_ether_addr: structure from VIRTCHNL with MAC to add
3739  */
3740 static void
3741 ice_vfhw_mac_add(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
3742 {
3743         u8 *mac_addr = vc_ether_addr->addr;
3744
3745         if (!is_valid_ether_addr(mac_addr))
3746                 return;
3747
3748         /* only allow legacy VF drivers to set the device and hardware MAC if it
3749          * is zero and allow new VF drivers to set the hardware MAC if the type
3750          * was correctly specified over VIRTCHNL
3751          */
3752         if ((ice_is_vc_addr_legacy(vc_ether_addr) &&
3753              is_zero_ether_addr(vf->hw_lan_addr.addr)) ||
3754             ice_is_vc_addr_primary(vc_ether_addr)) {
3755                 ether_addr_copy(vf->dev_lan_addr.addr, mac_addr);
3756                 ether_addr_copy(vf->hw_lan_addr.addr, mac_addr);
3757         }
3758
3759         /* hardware and device MACs are already set, but its possible that the
3760          * VF driver sent the VIRTCHNL_OP_ADD_ETH_ADDR message before the
3761          * VIRTCHNL_OP_DEL_ETH_ADDR when trying to update its MAC, so save it
3762          * away for the legacy VF driver case as it will be updated in the
3763          * delete flow for this case
3764          */
3765         if (ice_is_vc_addr_legacy(vc_ether_addr)) {
3766                 ether_addr_copy(vf->legacy_last_added_umac.addr,
3767                                 mac_addr);
3768                 vf->legacy_last_added_umac.time_modified = jiffies;
3769         }
3770 }
3771
3772 /**
3773  * ice_vc_add_mac_addr - attempt to add the MAC address passed in
3774  * @vf: pointer to the VF info
3775  * @vsi: pointer to the VF's VSI
3776  * @vc_ether_addr: VIRTCHNL MAC address structure used to add MAC
3777  */
3778 static int
3779 ice_vc_add_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
3780                     struct virtchnl_ether_addr *vc_ether_addr)
3781 {
3782         struct device *dev = ice_pf_to_dev(vf->pf);
3783         u8 *mac_addr = vc_ether_addr->addr;
3784         int ret;
3785
3786         /* device MAC already added */
3787         if (ether_addr_equal(mac_addr, vf->dev_lan_addr.addr))
3788                 return 0;
3789
3790         if (is_unicast_ether_addr(mac_addr) && !ice_can_vf_change_mac(vf)) {
3791                 dev_err(dev, "VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
3792                 return -EPERM;
3793         }
3794
3795         ret = ice_fltr_add_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
3796         if (ret == -EEXIST) {
3797                 dev_dbg(dev, "MAC %pM already exists for VF %d\n", mac_addr,
3798                         vf->vf_id);
3799                 /* don't return since we might need to update
3800                  * the primary MAC in ice_vfhw_mac_add() below
3801                  */
3802         } else if (ret) {
3803                 dev_err(dev, "Failed to add MAC %pM for VF %d\n, error %d\n",
3804                         mac_addr, vf->vf_id, ret);
3805                 return ret;
3806         } else {
3807                 vf->num_mac++;
3808         }
3809
3810         ice_vfhw_mac_add(vf, vc_ether_addr);
3811
3812         return ret;
3813 }
3814
3815 /**
3816  * ice_is_legacy_umac_expired - check if last added legacy unicast MAC expired
3817  * @last_added_umac: structure used to check expiration
3818  */
3819 static bool ice_is_legacy_umac_expired(struct ice_time_mac *last_added_umac)
3820 {
3821 #define ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME    msecs_to_jiffies(3000)
3822         return time_is_before_jiffies(last_added_umac->time_modified +
3823                                       ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME);
3824 }
3825
3826 /**
3827  * ice_update_legacy_cached_mac - update cached hardware MAC for legacy VF
3828  * @vf: VF to update
3829  * @vc_ether_addr: structure from VIRTCHNL with MAC to check
3830  *
3831  * only update cached hardware MAC for legacy VF drivers on delete
3832  * because we cannot guarantee order/type of MAC from the VF driver
3833  */
3834 static void
3835 ice_update_legacy_cached_mac(struct ice_vf *vf,
3836                              struct virtchnl_ether_addr *vc_ether_addr)
3837 {
3838         if (!ice_is_vc_addr_legacy(vc_ether_addr) ||
3839             ice_is_legacy_umac_expired(&vf->legacy_last_added_umac))
3840                 return;
3841
3842         ether_addr_copy(vf->dev_lan_addr.addr, vf->legacy_last_added_umac.addr);
3843         ether_addr_copy(vf->hw_lan_addr.addr, vf->legacy_last_added_umac.addr);
3844 }
3845
3846 /**
3847  * ice_vfhw_mac_del - update the VF's cached hardware MAC if allowed
3848  * @vf: VF to update
3849  * @vc_ether_addr: structure from VIRTCHNL with MAC to delete
3850  */
3851 static void
3852 ice_vfhw_mac_del(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
3853 {
3854         u8 *mac_addr = vc_ether_addr->addr;
3855
3856         if (!is_valid_ether_addr(mac_addr) ||
3857             !ether_addr_equal(vf->dev_lan_addr.addr, mac_addr))
3858                 return;
3859
3860         /* allow the device MAC to be repopulated in the add flow and don't
3861          * clear the hardware MAC (i.e. hw_lan_addr.addr) here as that is meant
3862          * to be persistent on VM reboot and across driver unload/load, which
3863          * won't work if we clear the hardware MAC here
3864          */
3865         eth_zero_addr(vf->dev_lan_addr.addr);
3866
3867         ice_update_legacy_cached_mac(vf, vc_ether_addr);
3868 }
3869
3870 /**
3871  * ice_vc_del_mac_addr - attempt to delete the MAC address passed in
3872  * @vf: pointer to the VF info
3873  * @vsi: pointer to the VF's VSI
3874  * @vc_ether_addr: VIRTCHNL MAC address structure used to delete MAC
3875  */
3876 static int
3877 ice_vc_del_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
3878                     struct virtchnl_ether_addr *vc_ether_addr)
3879 {
3880         struct device *dev = ice_pf_to_dev(vf->pf);
3881         u8 *mac_addr = vc_ether_addr->addr;
3882         int status;
3883
3884         if (!ice_can_vf_change_mac(vf) &&
3885             ether_addr_equal(vf->dev_lan_addr.addr, mac_addr))
3886                 return 0;
3887
3888         status = ice_fltr_remove_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
3889         if (status == -ENOENT) {
3890                 dev_err(dev, "MAC %pM does not exist for VF %d\n", mac_addr,
3891                         vf->vf_id);
3892                 return -ENOENT;
3893         } else if (status) {
3894                 dev_err(dev, "Failed to delete MAC %pM for VF %d, error %d\n",
3895                         mac_addr, vf->vf_id, status);
3896                 return -EIO;
3897         }
3898
3899         ice_vfhw_mac_del(vf, vc_ether_addr);
3900
3901         vf->num_mac--;
3902
3903         return 0;
3904 }
3905
3906 /**
3907  * ice_vc_handle_mac_addr_msg
3908  * @vf: pointer to the VF info
3909  * @msg: pointer to the msg buffer
3910  * @set: true if MAC filters are being set, false otherwise
3911  *
3912  * add guest MAC address filter
3913  */
3914 static int
3915 ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
3916 {
3917         int (*ice_vc_cfg_mac)
3918                 (struct ice_vf *vf, struct ice_vsi *vsi,
3919                  struct virtchnl_ether_addr *virtchnl_ether_addr);
3920         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3921         struct virtchnl_ether_addr_list *al =
3922             (struct virtchnl_ether_addr_list *)msg;
3923         struct ice_pf *pf = vf->pf;
3924         enum virtchnl_ops vc_op;
3925         struct ice_vsi *vsi;
3926         int i;
3927
3928         if (set) {
3929                 vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
3930                 ice_vc_cfg_mac = ice_vc_add_mac_addr;
3931         } else {
3932                 vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
3933                 ice_vc_cfg_mac = ice_vc_del_mac_addr;
3934         }
3935
3936         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
3937             !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
3938                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3939                 goto handle_mac_exit;
3940         }
3941
3942         /* If this VF is not privileged, then we can't add more than a
3943          * limited number of addresses. Check to make sure that the
3944          * additions do not push us over the limit.
3945          */
3946         if (set && !ice_is_vf_trusted(vf) &&
3947             (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
3948                 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",
3949                         vf->vf_id);
3950                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3951                 goto handle_mac_exit;
3952         }
3953
3954         vsi = ice_get_vf_vsi(vf);
3955         if (!vsi) {
3956                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3957                 goto handle_mac_exit;
3958         }
3959
3960         for (i = 0; i < al->num_elements; i++) {
3961                 u8 *mac_addr = al->list[i].addr;
3962                 int result;
3963
3964                 if (is_broadcast_ether_addr(mac_addr) ||
3965                     is_zero_ether_addr(mac_addr))
3966                         continue;
3967
3968                 result = ice_vc_cfg_mac(vf, vsi, &al->list[i]);
3969                 if (result == -EEXIST || result == -ENOENT) {
3970                         continue;
3971                 } else if (result) {
3972                         v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
3973                         goto handle_mac_exit;
3974                 }
3975         }
3976
3977 handle_mac_exit:
3978         /* send the response to the VF */
3979         return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
3980 }
3981
3982 /**
3983  * ice_vc_add_mac_addr_msg
3984  * @vf: pointer to the VF info
3985  * @msg: pointer to the msg buffer
3986  *
3987  * add guest MAC address filter
3988  */
3989 static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
3990 {
3991         return ice_vc_handle_mac_addr_msg(vf, msg, true);
3992 }
3993
3994 /**
3995  * ice_vc_del_mac_addr_msg
3996  * @vf: pointer to the VF info
3997  * @msg: pointer to the msg buffer
3998  *
3999  * remove guest MAC address filter
4000  */
4001 static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
4002 {
4003         return ice_vc_handle_mac_addr_msg(vf, msg, false);
4004 }
4005
4006 /**
4007  * ice_vc_request_qs_msg
4008  * @vf: pointer to the VF info
4009  * @msg: pointer to the msg buffer
4010  *
4011  * VFs get a default number of queues but can use this message to request a
4012  * different number. If the request is successful, PF will reset the VF and
4013  * return 0. If unsuccessful, PF will send message informing VF of number of
4014  * available queue pairs via virtchnl message response to VF.
4015  */
4016 static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
4017 {
4018         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4019         struct virtchnl_vf_res_request *vfres =
4020                 (struct virtchnl_vf_res_request *)msg;
4021         u16 req_queues = vfres->num_queue_pairs;
4022         struct ice_pf *pf = vf->pf;
4023         u16 max_allowed_vf_queues;
4024         u16 tx_rx_queue_left;
4025         struct device *dev;
4026         u16 cur_queues;
4027
4028         dev = ice_pf_to_dev(pf);
4029         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4030                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4031                 goto error_param;
4032         }
4033
4034         cur_queues = vf->num_vf_qs;
4035         tx_rx_queue_left = min_t(u16, ice_get_avail_txq_count(pf),
4036                                  ice_get_avail_rxq_count(pf));
4037         max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
4038         if (!req_queues) {
4039                 dev_err(dev, "VF %d tried to request 0 queues. Ignoring.\n",
4040                         vf->vf_id);
4041         } else if (req_queues > ICE_MAX_RSS_QS_PER_VF) {
4042                 dev_err(dev, "VF %d tried to request more than %d queues.\n",
4043                         vf->vf_id, ICE_MAX_RSS_QS_PER_VF);
4044                 vfres->num_queue_pairs = ICE_MAX_RSS_QS_PER_VF;
4045         } else if (req_queues > cur_queues &&
4046                    req_queues - cur_queues > tx_rx_queue_left) {
4047                 dev_warn(dev, "VF %d requested %u more queues, but only %u left.\n",
4048                          vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
4049                 vfres->num_queue_pairs = min_t(u16, max_allowed_vf_queues,
4050                                                ICE_MAX_RSS_QS_PER_VF);
4051         } else {
4052                 /* request is successful, then reset VF */
4053                 vf->num_req_qs = req_queues;
4054                 ice_vc_reset_vf(vf);
4055                 dev_info(dev, "VF %d granted request of %u queues.\n",
4056                          vf->vf_id, req_queues);
4057                 return 0;
4058         }
4059
4060 error_param:
4061         /* send the response to the VF */
4062         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
4063                                      v_ret, (u8 *)vfres, sizeof(*vfres));
4064 }
4065
4066 /**
4067  * ice_set_vf_port_vlan
4068  * @netdev: network interface device structure
4069  * @vf_id: VF identifier
4070  * @vlan_id: VLAN ID being set
4071  * @qos: priority setting
4072  * @vlan_proto: VLAN protocol
4073  *
4074  * program VF Port VLAN ID and/or QoS
4075  */
4076 int
4077 ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
4078                      __be16 vlan_proto)
4079 {
4080         struct ice_pf *pf = ice_netdev_to_pf(netdev);
4081         struct device *dev;
4082         struct ice_vf *vf;
4083         u16 vlanprio;
4084         int ret;
4085
4086         dev = ice_pf_to_dev(pf);
4087         if (ice_validate_vf_id(pf, vf_id))
4088                 return -EINVAL;
4089
4090         if (vlan_id >= VLAN_N_VID || qos > 7) {
4091                 dev_err(dev, "Invalid Port VLAN parameters for VF %d, ID %d, QoS %d\n",
4092                         vf_id, vlan_id, qos);
4093                 return -EINVAL;
4094         }
4095
4096         if (vlan_proto != htons(ETH_P_8021Q)) {
4097                 dev_err(dev, "VF VLAN protocol is not supported\n");
4098                 return -EPROTONOSUPPORT;
4099         }
4100
4101         vf = &pf->vf[vf_id];
4102         ret = ice_check_vf_ready_for_cfg(vf);
4103         if (ret)
4104                 return ret;
4105
4106         vlanprio = vlan_id | (qos << VLAN_PRIO_SHIFT);
4107
4108         if (vf->port_vlan_info == vlanprio) {
4109                 /* duplicate request, so just return success */
4110                 dev_dbg(dev, "Duplicate pvid %d request\n", vlanprio);
4111                 return 0;
4112         }
4113
4114         mutex_lock(&vf->cfg_lock);
4115
4116         vf->port_vlan_info = vlanprio;
4117
4118         if (vf->port_vlan_info)
4119                 dev_info(dev, "Setting VLAN %d, QoS 0x%x on VF %d\n",
4120                          vlan_id, qos, vf_id);
4121         else
4122                 dev_info(dev, "Clearing port VLAN on VF %d\n", vf_id);
4123
4124         ice_vc_reset_vf(vf);
4125         mutex_unlock(&vf->cfg_lock);
4126
4127         return 0;
4128 }
4129
4130 /**
4131  * ice_vf_vlan_offload_ena - determine if capabilities support VLAN offloads
4132  * @caps: VF driver negotiated capabilities
4133  *
4134  * Return true if VIRTCHNL_VF_OFFLOAD_VLAN capability is set, else return false
4135  */
4136 static bool ice_vf_vlan_offload_ena(u32 caps)
4137 {
4138         return !!(caps & VIRTCHNL_VF_OFFLOAD_VLAN);
4139 }
4140
4141 /**
4142  * ice_vc_process_vlan_msg
4143  * @vf: pointer to the VF info
4144  * @msg: pointer to the msg buffer
4145  * @add_v: Add VLAN if true, otherwise delete VLAN
4146  *
4147  * Process virtchnl op to add or remove programmed guest VLAN ID
4148  */
4149 static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
4150 {
4151         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4152         struct virtchnl_vlan_filter_list *vfl =
4153             (struct virtchnl_vlan_filter_list *)msg;
4154         struct ice_pf *pf = vf->pf;
4155         bool vlan_promisc = false;
4156         struct ice_vsi *vsi;
4157         struct device *dev;
4158         struct ice_hw *hw;
4159         int status = 0;
4160         u8 promisc_m;
4161         int i;
4162
4163         dev = ice_pf_to_dev(pf);
4164         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4165                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4166                 goto error_param;
4167         }
4168
4169         if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
4170                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4171                 goto error_param;
4172         }
4173
4174         if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
4175                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4176                 goto error_param;
4177         }
4178
4179         for (i = 0; i < vfl->num_elements; i++) {
4180                 if (vfl->vlan_id[i] >= VLAN_N_VID) {
4181                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4182                         dev_err(dev, "invalid VF VLAN id %d\n",
4183                                 vfl->vlan_id[i]);
4184                         goto error_param;
4185                 }
4186         }
4187
4188         hw = &pf->hw;
4189         vsi = ice_get_vf_vsi(vf);
4190         if (!vsi) {
4191                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4192                 goto error_param;
4193         }
4194
4195         if (add_v && !ice_is_vf_trusted(vf) &&
4196             vsi->num_vlan >= ICE_MAX_VLAN_PER_VF) {
4197                 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
4198                          vf->vf_id);
4199                 /* There is no need to let VF know about being not trusted,
4200                  * so we can just return success message here
4201                  */
4202                 goto error_param;
4203         }
4204
4205         if (vsi->info.pvid) {
4206                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4207                 goto error_param;
4208         }
4209
4210         if ((test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
4211              test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) &&
4212             test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags))
4213                 vlan_promisc = true;
4214
4215         if (add_v) {
4216                 for (i = 0; i < vfl->num_elements; i++) {
4217                         u16 vid = vfl->vlan_id[i];
4218
4219                         if (!ice_is_vf_trusted(vf) &&
4220                             vsi->num_vlan >= ICE_MAX_VLAN_PER_VF) {
4221                                 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
4222                                          vf->vf_id);
4223                                 /* There is no need to let VF know about being
4224                                  * not trusted, so we can just return success
4225                                  * message here as well.
4226                                  */
4227                                 goto error_param;
4228                         }
4229
4230                         /* we add VLAN 0 by default for each VF so we can enable
4231                          * Tx VLAN anti-spoof without triggering MDD events so
4232                          * we don't need to add it again here
4233                          */
4234                         if (!vid)
4235                                 continue;
4236
4237                         status = ice_vsi_add_vlan(vsi, vid, ICE_FWD_TO_VSI);
4238                         if (status) {
4239                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4240                                 goto error_param;
4241                         }
4242
4243                         /* Enable VLAN pruning when non-zero VLAN is added */
4244                         if (!vlan_promisc && vid &&
4245                             !ice_vsi_is_vlan_pruning_ena(vsi)) {
4246                                 status = ice_cfg_vlan_pruning(vsi, true);
4247                                 if (status) {
4248                                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4249                                         dev_err(dev, "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
4250                                                 vid, status);
4251                                         goto error_param;
4252                                 }
4253                         } else if (vlan_promisc) {
4254                                 /* Enable Ucast/Mcast VLAN promiscuous mode */
4255                                 promisc_m = ICE_PROMISC_VLAN_TX |
4256                                             ICE_PROMISC_VLAN_RX;
4257
4258                                 status = ice_set_vsi_promisc(hw, vsi->idx,
4259                                                              promisc_m, vid);
4260                                 if (status) {
4261                                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4262                                         dev_err(dev, "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
4263                                                 vid, status);
4264                                 }
4265                         }
4266                 }
4267         } else {
4268                 /* In case of non_trusted VF, number of VLAN elements passed
4269                  * to PF for removal might be greater than number of VLANs
4270                  * filter programmed for that VF - So, use actual number of
4271                  * VLANS added earlier with add VLAN opcode. In order to avoid
4272                  * removing VLAN that doesn't exist, which result to sending
4273                  * erroneous failed message back to the VF
4274                  */
4275                 int num_vf_vlan;
4276
4277                 num_vf_vlan = vsi->num_vlan;
4278                 for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
4279                         u16 vid = vfl->vlan_id[i];
4280
4281                         /* we add VLAN 0 by default for each VF so we can enable
4282                          * Tx VLAN anti-spoof without triggering MDD events so
4283                          * we don't want a VIRTCHNL request to remove it
4284                          */
4285                         if (!vid)
4286                                 continue;
4287
4288                         /* Make sure ice_vsi_kill_vlan is successful before
4289                          * updating VLAN information
4290                          */
4291                         status = ice_vsi_kill_vlan(vsi, vid);
4292                         if (status) {
4293                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4294                                 goto error_param;
4295                         }
4296
4297                         /* Disable VLAN pruning when only VLAN 0 is left */
4298                         if (vsi->num_vlan == 1 &&
4299                             ice_vsi_is_vlan_pruning_ena(vsi))
4300                                 ice_cfg_vlan_pruning(vsi, false);
4301
4302                         /* Disable Unicast/Multicast VLAN promiscuous mode */
4303                         if (vlan_promisc) {
4304                                 promisc_m = ICE_PROMISC_VLAN_TX |
4305                                             ICE_PROMISC_VLAN_RX;
4306
4307                                 ice_clear_vsi_promisc(hw, vsi->idx,
4308                                                       promisc_m, vid);
4309                         }
4310                 }
4311         }
4312
4313 error_param:
4314         /* send the response to the VF */
4315         if (add_v)
4316                 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
4317                                              NULL, 0);
4318         else
4319                 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
4320                                              NULL, 0);
4321 }
4322
4323 /**
4324  * ice_vc_add_vlan_msg
4325  * @vf: pointer to the VF info
4326  * @msg: pointer to the msg buffer
4327  *
4328  * Add and program guest VLAN ID
4329  */
4330 static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
4331 {
4332         return ice_vc_process_vlan_msg(vf, msg, true);
4333 }
4334
4335 /**
4336  * ice_vc_remove_vlan_msg
4337  * @vf: pointer to the VF info
4338  * @msg: pointer to the msg buffer
4339  *
4340  * remove programmed guest VLAN ID
4341  */
4342 static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
4343 {
4344         return ice_vc_process_vlan_msg(vf, msg, false);
4345 }
4346
4347 /**
4348  * ice_vc_ena_vlan_stripping
4349  * @vf: pointer to the VF info
4350  *
4351  * Enable VLAN header stripping for a given VF
4352  */
4353 static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
4354 {
4355         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4356         struct ice_vsi *vsi;
4357
4358         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4359                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4360                 goto error_param;
4361         }
4362
4363         if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
4364                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4365                 goto error_param;
4366         }
4367
4368         vsi = ice_get_vf_vsi(vf);
4369         if (ice_vsi_manage_vlan_stripping(vsi, true))
4370                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4371
4372 error_param:
4373         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
4374                                      v_ret, NULL, 0);
4375 }
4376
4377 /**
4378  * ice_vc_dis_vlan_stripping
4379  * @vf: pointer to the VF info
4380  *
4381  * Disable VLAN header stripping for a given VF
4382  */
4383 static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
4384 {
4385         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4386         struct ice_vsi *vsi;
4387
4388         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4389                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4390                 goto error_param;
4391         }
4392
4393         if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
4394                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4395                 goto error_param;
4396         }
4397
4398         vsi = ice_get_vf_vsi(vf);
4399         if (!vsi) {
4400                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4401                 goto error_param;
4402         }
4403
4404         if (ice_vsi_manage_vlan_stripping(vsi, false))
4405                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4406
4407 error_param:
4408         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
4409                                      v_ret, NULL, 0);
4410 }
4411
4412 /**
4413  * ice_vf_init_vlan_stripping - enable/disable VLAN stripping on initialization
4414  * @vf: VF to enable/disable VLAN stripping for on initialization
4415  *
4416  * If the VIRTCHNL_VF_OFFLOAD_VLAN flag is set enable VLAN stripping, else if
4417  * the flag is cleared then we want to disable stripping. For example, the flag
4418  * will be cleared when port VLANs are configured by the administrator before
4419  * passing the VF to the guest or if the AVF driver doesn't support VLAN
4420  * offloads.
4421  */
4422 static int ice_vf_init_vlan_stripping(struct ice_vf *vf)
4423 {
4424         struct ice_vsi *vsi = ice_get_vf_vsi(vf);
4425
4426         if (!vsi)
4427                 return -EINVAL;
4428
4429         /* don't modify stripping if port VLAN is configured */
4430         if (vsi->info.pvid)
4431                 return 0;
4432
4433         if (ice_vf_vlan_offload_ena(vf->driver_caps))
4434                 return ice_vsi_manage_vlan_stripping(vsi, true);
4435         else
4436                 return ice_vsi_manage_vlan_stripping(vsi, false);
4437 }
4438
4439 static struct ice_vc_vf_ops ice_vc_vf_dflt_ops = {
4440         .get_ver_msg = ice_vc_get_ver_msg,
4441         .get_vf_res_msg = ice_vc_get_vf_res_msg,
4442         .reset_vf = ice_vc_reset_vf_msg,
4443         .add_mac_addr_msg = ice_vc_add_mac_addr_msg,
4444         .del_mac_addr_msg = ice_vc_del_mac_addr_msg,
4445         .cfg_qs_msg = ice_vc_cfg_qs_msg,
4446         .ena_qs_msg = ice_vc_ena_qs_msg,
4447         .dis_qs_msg = ice_vc_dis_qs_msg,
4448         .request_qs_msg = ice_vc_request_qs_msg,
4449         .cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
4450         .config_rss_key = ice_vc_config_rss_key,
4451         .config_rss_lut = ice_vc_config_rss_lut,
4452         .get_stats_msg = ice_vc_get_stats_msg,
4453         .cfg_promiscuous_mode_msg = ice_vc_cfg_promiscuous_mode_msg,
4454         .add_vlan_msg = ice_vc_add_vlan_msg,
4455         .remove_vlan_msg = ice_vc_remove_vlan_msg,
4456         .ena_vlan_stripping = ice_vc_ena_vlan_stripping,
4457         .dis_vlan_stripping = ice_vc_dis_vlan_stripping,
4458         .handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
4459         .add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
4460         .del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
4461 };
4462
4463 void ice_vc_set_dflt_vf_ops(struct ice_vc_vf_ops *ops)
4464 {
4465         *ops = ice_vc_vf_dflt_ops;
4466 }
4467
4468 /**
4469  * ice_vc_repr_add_mac
4470  * @vf: pointer to VF
4471  * @msg: virtchannel message
4472  *
4473  * When port representors are created, we do not add MAC rule
4474  * to firmware, we store it so that PF could report same
4475  * MAC as VF.
4476  */
4477 static int ice_vc_repr_add_mac(struct ice_vf *vf, u8 *msg)
4478 {
4479         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4480         struct virtchnl_ether_addr_list *al =
4481             (struct virtchnl_ether_addr_list *)msg;
4482         struct ice_vsi *vsi;
4483         struct ice_pf *pf;
4484         int i;
4485
4486         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
4487             !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
4488                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4489                 goto handle_mac_exit;
4490         }
4491
4492         pf = vf->pf;
4493
4494         vsi = ice_get_vf_vsi(vf);
4495         if (!vsi) {
4496                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4497                 goto handle_mac_exit;
4498         }
4499
4500         for (i = 0; i < al->num_elements; i++) {
4501                 u8 *mac_addr = al->list[i].addr;
4502                 int result;
4503
4504                 if (!is_unicast_ether_addr(mac_addr) ||
4505                     ether_addr_equal(mac_addr, vf->hw_lan_addr.addr))
4506                         continue;
4507
4508                 if (vf->pf_set_mac) {
4509                         dev_err(ice_pf_to_dev(pf), "VF attempting to override administratively set MAC address\n");
4510                         v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
4511                         goto handle_mac_exit;
4512                 }
4513
4514                 result = ice_eswitch_add_vf_mac_rule(pf, vf, mac_addr);
4515                 if (result) {
4516                         dev_err(ice_pf_to_dev(pf), "Failed to add MAC %pM for VF %d\n, error %d\n",
4517                                 mac_addr, vf->vf_id, result);
4518                         goto handle_mac_exit;
4519                 }
4520
4521                 ice_vfhw_mac_add(vf, &al->list[i]);
4522                 vf->num_mac++;
4523                 break;
4524         }
4525
4526 handle_mac_exit:
4527         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
4528                                      v_ret, NULL, 0);
4529 }
4530
4531 /**
4532  * ice_vc_repr_del_mac - response with success for deleting MAC
4533  * @vf: pointer to VF
4534  * @msg: virtchannel message
4535  *
4536  * Respond with success to not break normal VF flow.
4537  * For legacy VF driver try to update cached MAC address.
4538  */
4539 static int
4540 ice_vc_repr_del_mac(struct ice_vf __always_unused *vf, u8 __always_unused *msg)
4541 {
4542         struct virtchnl_ether_addr_list *al =
4543                 (struct virtchnl_ether_addr_list *)msg;
4544
4545         ice_update_legacy_cached_mac(vf, &al->list[0]);
4546
4547         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
4548                                      VIRTCHNL_STATUS_SUCCESS, NULL, 0);
4549 }
4550
4551 static int ice_vc_repr_add_vlan(struct ice_vf *vf, u8 __always_unused *msg)
4552 {
4553         dev_dbg(ice_pf_to_dev(vf->pf),
4554                 "Can't add VLAN in switchdev mode for VF %d\n", vf->vf_id);
4555         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN,
4556                                      VIRTCHNL_STATUS_SUCCESS, NULL, 0);
4557 }
4558
4559 static int ice_vc_repr_del_vlan(struct ice_vf *vf, u8 __always_unused *msg)
4560 {
4561         dev_dbg(ice_pf_to_dev(vf->pf),
4562                 "Can't delete VLAN in switchdev mode for VF %d\n", vf->vf_id);
4563         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN,
4564                                      VIRTCHNL_STATUS_SUCCESS, NULL, 0);
4565 }
4566
4567 static int ice_vc_repr_ena_vlan_stripping(struct ice_vf *vf)
4568 {
4569         dev_dbg(ice_pf_to_dev(vf->pf),
4570                 "Can't enable VLAN stripping in switchdev mode for VF %d\n",
4571                 vf->vf_id);
4572         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
4573                                      VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4574                                      NULL, 0);
4575 }
4576
4577 static int ice_vc_repr_dis_vlan_stripping(struct ice_vf *vf)
4578 {
4579         dev_dbg(ice_pf_to_dev(vf->pf),
4580                 "Can't disable VLAN stripping in switchdev mode for VF %d\n",
4581                 vf->vf_id);
4582         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
4583                                      VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4584                                      NULL, 0);
4585 }
4586
4587 static int
4588 ice_vc_repr_cfg_promiscuous_mode(struct ice_vf *vf, u8 __always_unused *msg)
4589 {
4590         dev_dbg(ice_pf_to_dev(vf->pf),
4591                 "Can't config promiscuous mode in switchdev mode for VF %d\n",
4592                 vf->vf_id);
4593         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
4594                                      VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4595                                      NULL, 0);
4596 }
4597
4598 void ice_vc_change_ops_to_repr(struct ice_vc_vf_ops *ops)
4599 {
4600         ops->add_mac_addr_msg = ice_vc_repr_add_mac;
4601         ops->del_mac_addr_msg = ice_vc_repr_del_mac;
4602         ops->add_vlan_msg = ice_vc_repr_add_vlan;
4603         ops->remove_vlan_msg = ice_vc_repr_del_vlan;
4604         ops->ena_vlan_stripping = ice_vc_repr_ena_vlan_stripping;
4605         ops->dis_vlan_stripping = ice_vc_repr_dis_vlan_stripping;
4606         ops->cfg_promiscuous_mode_msg = ice_vc_repr_cfg_promiscuous_mode;
4607 }
4608
4609 /**
4610  * ice_vc_process_vf_msg - Process request from VF
4611  * @pf: pointer to the PF structure
4612  * @event: pointer to the AQ event
4613  *
4614  * called from the common asq/arq handler to
4615  * process request from VF
4616  */
4617 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
4618 {
4619         u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
4620         s16 vf_id = le16_to_cpu(event->desc.retval);
4621         u16 msglen = event->msg_len;
4622         struct ice_vc_vf_ops *ops;
4623         u8 *msg = event->msg_buf;
4624         struct ice_vf *vf = NULL;
4625         struct device *dev;
4626         int err = 0;
4627
4628         /* if de-init is underway, don't process messages from VF */
4629         if (test_bit(ICE_VF_DEINIT_IN_PROGRESS, pf->state))
4630                 return;
4631
4632         dev = ice_pf_to_dev(pf);
4633         if (ice_validate_vf_id(pf, vf_id)) {
4634                 err = -EINVAL;
4635                 goto error_handler;
4636         }
4637
4638         vf = &pf->vf[vf_id];
4639
4640         /* Check if VF is disabled. */
4641         if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
4642                 err = -EPERM;
4643                 goto error_handler;
4644         }
4645
4646         ops = &vf->vc_ops;
4647
4648         /* Perform basic checks on the msg */
4649         err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
4650         if (err) {
4651                 if (err == VIRTCHNL_STATUS_ERR_PARAM)
4652                         err = -EPERM;
4653                 else
4654                         err = -EINVAL;
4655         }
4656
4657         if (!ice_vc_is_opcode_allowed(vf, v_opcode)) {
4658                 ice_vc_send_msg_to_vf(vf, v_opcode,
4659                                       VIRTCHNL_STATUS_ERR_NOT_SUPPORTED, NULL,
4660                                       0);
4661                 return;
4662         }
4663
4664 error_handler:
4665         if (err) {
4666                 ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
4667                                       NULL, 0);
4668                 dev_err(dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
4669                         vf_id, v_opcode, msglen, err);
4670                 return;
4671         }
4672
4673         /* VF is being configured in another context that triggers a VFR, so no
4674          * need to process this message
4675          */
4676         if (!mutex_trylock(&vf->cfg_lock)) {
4677                 dev_info(dev, "VF %u is being configured in another context that will trigger a VFR, so there is no need to handle this message\n",
4678                          vf->vf_id);
4679                 return;
4680         }
4681
4682         switch (v_opcode) {
4683         case VIRTCHNL_OP_VERSION:
4684                 err = ops->get_ver_msg(vf, msg);
4685                 break;
4686         case VIRTCHNL_OP_GET_VF_RESOURCES:
4687                 err = ops->get_vf_res_msg(vf, msg);
4688                 if (ice_vf_init_vlan_stripping(vf))
4689                         dev_err(dev, "Failed to initialize VLAN stripping for VF %d\n",
4690                                 vf->vf_id);
4691                 ice_vc_notify_vf_link_state(vf);
4692                 break;
4693         case VIRTCHNL_OP_RESET_VF:
4694                 ops->reset_vf(vf);
4695                 break;
4696         case VIRTCHNL_OP_ADD_ETH_ADDR:
4697                 err = ops->add_mac_addr_msg(vf, msg);
4698                 break;
4699         case VIRTCHNL_OP_DEL_ETH_ADDR:
4700                 err = ops->del_mac_addr_msg(vf, msg);
4701                 break;
4702         case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
4703                 err = ops->cfg_qs_msg(vf, msg);
4704                 break;
4705         case VIRTCHNL_OP_ENABLE_QUEUES:
4706                 err = ops->ena_qs_msg(vf, msg);
4707                 ice_vc_notify_vf_link_state(vf);
4708                 break;
4709         case VIRTCHNL_OP_DISABLE_QUEUES:
4710                 err = ops->dis_qs_msg(vf, msg);
4711                 break;
4712         case VIRTCHNL_OP_REQUEST_QUEUES:
4713                 err = ops->request_qs_msg(vf, msg);
4714                 break;
4715         case VIRTCHNL_OP_CONFIG_IRQ_MAP:
4716                 err = ops->cfg_irq_map_msg(vf, msg);
4717                 break;
4718         case VIRTCHNL_OP_CONFIG_RSS_KEY:
4719                 err = ops->config_rss_key(vf, msg);
4720                 break;
4721         case VIRTCHNL_OP_CONFIG_RSS_LUT:
4722                 err = ops->config_rss_lut(vf, msg);
4723                 break;
4724         case VIRTCHNL_OP_GET_STATS:
4725                 err = ops->get_stats_msg(vf, msg);
4726                 break;
4727         case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
4728                 err = ops->cfg_promiscuous_mode_msg(vf, msg);
4729                 break;
4730         case VIRTCHNL_OP_ADD_VLAN:
4731                 err = ops->add_vlan_msg(vf, msg);
4732                 break;
4733         case VIRTCHNL_OP_DEL_VLAN:
4734                 err = ops->remove_vlan_msg(vf, msg);
4735                 break;
4736         case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
4737                 err = ops->ena_vlan_stripping(vf);
4738                 break;
4739         case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
4740                 err = ops->dis_vlan_stripping(vf);
4741                 break;
4742         case VIRTCHNL_OP_ADD_FDIR_FILTER:
4743                 err = ops->add_fdir_fltr_msg(vf, msg);
4744                 break;
4745         case VIRTCHNL_OP_DEL_FDIR_FILTER:
4746                 err = ops->del_fdir_fltr_msg(vf, msg);
4747                 break;
4748         case VIRTCHNL_OP_ADD_RSS_CFG:
4749                 err = ops->handle_rss_cfg_msg(vf, msg, true);
4750                 break;
4751         case VIRTCHNL_OP_DEL_RSS_CFG:
4752                 err = ops->handle_rss_cfg_msg(vf, msg, false);
4753                 break;
4754         case VIRTCHNL_OP_UNKNOWN:
4755         default:
4756                 dev_err(dev, "Unsupported opcode %d from VF %d\n", v_opcode,
4757                         vf_id);
4758                 err = ice_vc_send_msg_to_vf(vf, v_opcode,
4759                                             VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4760                                             NULL, 0);
4761                 break;
4762         }
4763         if (err) {
4764                 /* Helper function cares less about error return values here
4765                  * as it is busy with pending work.
4766                  */
4767                 dev_info(dev, "PF failed to honor VF %d, opcode %d, error %d\n",
4768                          vf_id, v_opcode, err);
4769         }
4770
4771         mutex_unlock(&vf->cfg_lock);
4772 }
4773
4774 /**
4775  * ice_get_vf_cfg
4776  * @netdev: network interface device structure
4777  * @vf_id: VF identifier
4778  * @ivi: VF configuration structure
4779  *
4780  * return VF configuration
4781  */
4782 int
4783 ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
4784 {
4785         struct ice_pf *pf = ice_netdev_to_pf(netdev);
4786         struct ice_vf *vf;
4787
4788         if (ice_validate_vf_id(pf, vf_id))
4789                 return -EINVAL;
4790
4791         vf = &pf->vf[vf_id];
4792
4793         if (ice_check_vf_init(pf, vf))
4794                 return -EBUSY;
4795
4796         ivi->vf = vf_id;
4797         ether_addr_copy(ivi->mac, vf->hw_lan_addr.addr);
4798
4799         /* VF configuration for VLAN and applicable QoS */
4800         ivi->vlan = vf->port_vlan_info & VLAN_VID_MASK;
4801         ivi->qos = (vf->port_vlan_info & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
4802
4803         ivi->trusted = vf->trusted;
4804         ivi->spoofchk = vf->spoofchk;
4805         if (!vf->link_forced)
4806                 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
4807         else if (vf->link_up)
4808                 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
4809         else
4810                 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
4811         ivi->max_tx_rate = vf->max_tx_rate;
4812         ivi->min_tx_rate = vf->min_tx_rate;
4813         return 0;
4814 }
4815
4816 /**
4817  * ice_unicast_mac_exists - check if the unicast MAC exists on the PF's switch
4818  * @pf: PF used to reference the switch's rules
4819  * @umac: unicast MAC to compare against existing switch rules
4820  *
4821  * Return true on the first/any match, else return false
4822  */
4823 static bool ice_unicast_mac_exists(struct ice_pf *pf, u8 *umac)
4824 {
4825         struct ice_sw_recipe *mac_recipe_list =
4826                 &pf->hw.switch_info->recp_list[ICE_SW_LKUP_MAC];
4827         struct ice_fltr_mgmt_list_entry *list_itr;
4828         struct list_head *rule_head;
4829         struct mutex *rule_lock; /* protect MAC filter list access */
4830
4831         rule_head = &mac_recipe_list->filt_rules;
4832         rule_lock = &mac_recipe_list->filt_rule_lock;
4833
4834         mutex_lock(rule_lock);
4835         list_for_each_entry(list_itr, rule_head, list_entry) {
4836                 u8 *existing_mac = &list_itr->fltr_info.l_data.mac.mac_addr[0];
4837
4838                 if (ether_addr_equal(existing_mac, umac)) {
4839                         mutex_unlock(rule_lock);
4840                         return true;
4841                 }
4842         }
4843
4844         mutex_unlock(rule_lock);
4845
4846         return false;
4847 }
4848
4849 /**
4850  * ice_set_vf_mac
4851  * @netdev: network interface device structure
4852  * @vf_id: VF identifier
4853  * @mac: MAC address
4854  *
4855  * program VF MAC address
4856  */
4857 int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
4858 {
4859         struct ice_pf *pf = ice_netdev_to_pf(netdev);
4860         struct ice_vf *vf;
4861         int ret;
4862
4863         if (ice_validate_vf_id(pf, vf_id))
4864                 return -EINVAL;
4865
4866         if (is_multicast_ether_addr(mac)) {
4867                 netdev_err(netdev, "%pM not a valid unicast address\n", mac);
4868                 return -EINVAL;
4869         }
4870
4871         vf = &pf->vf[vf_id];
4872         /* nothing left to do, unicast MAC already set */
4873         if (ether_addr_equal(vf->dev_lan_addr.addr, mac) &&
4874             ether_addr_equal(vf->hw_lan_addr.addr, mac))
4875                 return 0;
4876
4877         ret = ice_check_vf_ready_for_cfg(vf);
4878         if (ret)
4879                 return ret;
4880
4881         if (ice_unicast_mac_exists(pf, mac)) {
4882                 netdev_err(netdev, "Unicast MAC %pM already exists on this PF. Preventing setting VF %u unicast MAC address to %pM\n",
4883                            mac, vf_id, mac);
4884                 return -EINVAL;
4885         }
4886
4887         mutex_lock(&vf->cfg_lock);
4888
4889         /* VF is notified of its new MAC via the PF's response to the
4890          * VIRTCHNL_OP_GET_VF_RESOURCES message after the VF has been reset
4891          */
4892         ether_addr_copy(vf->dev_lan_addr.addr, mac);
4893         ether_addr_copy(vf->hw_lan_addr.addr, mac);
4894         if (is_zero_ether_addr(mac)) {
4895                 /* VF will send VIRTCHNL_OP_ADD_ETH_ADDR message with its MAC */
4896                 vf->pf_set_mac = false;
4897                 netdev_info(netdev, "Removing MAC on VF %d. VF driver will be reinitialized\n",
4898                             vf->vf_id);
4899         } else {
4900                 /* PF will add MAC rule for the VF */
4901                 vf->pf_set_mac = true;
4902                 netdev_info(netdev, "Setting MAC %pM on VF %d. VF driver will be reinitialized\n",
4903                             mac, vf_id);
4904         }
4905
4906         ice_vc_reset_vf(vf);
4907         mutex_unlock(&vf->cfg_lock);
4908         return 0;
4909 }
4910
4911 /**
4912  * ice_set_vf_trust
4913  * @netdev: network interface device structure
4914  * @vf_id: VF identifier
4915  * @trusted: Boolean value to enable/disable trusted VF
4916  *
4917  * Enable or disable a given VF as trusted
4918  */
4919 int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
4920 {
4921         struct ice_pf *pf = ice_netdev_to_pf(netdev);
4922         struct ice_vf *vf;
4923         int ret;
4924
4925         if (ice_is_eswitch_mode_switchdev(pf)) {
4926                 dev_info(ice_pf_to_dev(pf), "Trusted VF is forbidden in switchdev mode\n");
4927                 return -EOPNOTSUPP;
4928         }
4929
4930         if (ice_validate_vf_id(pf, vf_id))
4931                 return -EINVAL;
4932
4933         vf = &pf->vf[vf_id];
4934         ret = ice_check_vf_ready_for_cfg(vf);
4935         if (ret)
4936                 return ret;
4937
4938         /* Check if already trusted */
4939         if (trusted == vf->trusted)
4940                 return 0;
4941
4942         mutex_lock(&vf->cfg_lock);
4943
4944         vf->trusted = trusted;
4945         ice_vc_reset_vf(vf);
4946         dev_info(ice_pf_to_dev(pf), "VF %u is now %strusted\n",
4947                  vf_id, trusted ? "" : "un");
4948
4949         mutex_unlock(&vf->cfg_lock);
4950
4951         return 0;
4952 }
4953
4954 /**
4955  * ice_set_vf_link_state
4956  * @netdev: network interface device structure
4957  * @vf_id: VF identifier
4958  * @link_state: required link state
4959  *
4960  * Set VF's link state, irrespective of physical link state status
4961  */
4962 int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
4963 {
4964         struct ice_pf *pf = ice_netdev_to_pf(netdev);
4965         struct ice_vf *vf;
4966         int ret;
4967
4968         if (ice_validate_vf_id(pf, vf_id))
4969                 return -EINVAL;
4970
4971         vf = &pf->vf[vf_id];
4972         ret = ice_check_vf_ready_for_cfg(vf);
4973         if (ret)
4974                 return ret;
4975
4976         switch (link_state) {
4977         case IFLA_VF_LINK_STATE_AUTO:
4978                 vf->link_forced = false;
4979                 break;
4980         case IFLA_VF_LINK_STATE_ENABLE:
4981                 vf->link_forced = true;
4982                 vf->link_up = true;
4983                 break;
4984         case IFLA_VF_LINK_STATE_DISABLE:
4985                 vf->link_forced = true;
4986                 vf->link_up = false;
4987                 break;
4988         default:
4989                 return -EINVAL;
4990         }
4991
4992         ice_vc_notify_vf_link_state(vf);
4993
4994         return 0;
4995 }
4996
4997 /**
4998  * ice_calc_all_vfs_min_tx_rate - calculate cumulative min Tx rate on all VFs
4999  * @pf: PF associated with VFs
5000  */
5001 static int ice_calc_all_vfs_min_tx_rate(struct ice_pf *pf)
5002 {
5003         int rate = 0, i;
5004
5005         ice_for_each_vf(pf, i)
5006                 rate += pf->vf[i].min_tx_rate;
5007
5008         return rate;
5009 }
5010
5011 /**
5012  * ice_min_tx_rate_oversubscribed - check if min Tx rate causes oversubscription
5013  * @vf: VF trying to configure min_tx_rate
5014  * @min_tx_rate: min Tx rate in Mbps
5015  *
5016  * Check if the min_tx_rate being passed in will cause oversubscription of total
5017  * min_tx_rate based on the current link speed and all other VFs configured
5018  * min_tx_rate
5019  *
5020  * Return true if the passed min_tx_rate would cause oversubscription, else
5021  * return false
5022  */
5023 static bool
5024 ice_min_tx_rate_oversubscribed(struct ice_vf *vf, int min_tx_rate)
5025 {
5026         int link_speed_mbps = ice_get_link_speed_mbps(ice_get_vf_vsi(vf));
5027         int all_vfs_min_tx_rate = ice_calc_all_vfs_min_tx_rate(vf->pf);
5028
5029         /* this VF's previous rate is being overwritten */
5030         all_vfs_min_tx_rate -= vf->min_tx_rate;
5031
5032         if (all_vfs_min_tx_rate + min_tx_rate > link_speed_mbps) {
5033                 dev_err(ice_pf_to_dev(vf->pf), "min_tx_rate of %d Mbps on VF %u would cause oversubscription of %d Mbps based on the current link speed %d Mbps\n",
5034                         min_tx_rate, vf->vf_id,
5035                         all_vfs_min_tx_rate + min_tx_rate - link_speed_mbps,
5036                         link_speed_mbps);
5037                 return true;
5038         }
5039
5040         return false;
5041 }
5042
5043 /**
5044  * ice_set_vf_bw - set min/max VF bandwidth
5045  * @netdev: network interface device structure
5046  * @vf_id: VF identifier
5047  * @min_tx_rate: Minimum Tx rate in Mbps
5048  * @max_tx_rate: Maximum Tx rate in Mbps
5049  */
5050 int
5051 ice_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
5052               int max_tx_rate)
5053 {
5054         struct ice_pf *pf = ice_netdev_to_pf(netdev);
5055         struct ice_vsi *vsi;
5056         struct device *dev;
5057         struct ice_vf *vf;
5058         int ret;
5059
5060         dev = ice_pf_to_dev(pf);
5061         if (ice_validate_vf_id(pf, vf_id))
5062                 return -EINVAL;
5063
5064         vf = &pf->vf[vf_id];
5065         ret = ice_check_vf_ready_for_cfg(vf);
5066         if (ret)
5067                 return ret;
5068
5069         vsi = ice_get_vf_vsi(vf);
5070
5071         /* when max_tx_rate is zero that means no max Tx rate limiting, so only
5072          * check if max_tx_rate is non-zero
5073          */
5074         if (max_tx_rate && min_tx_rate > max_tx_rate) {
5075                 dev_err(dev, "Cannot set min Tx rate %d Mbps greater than max Tx rate %d Mbps\n",
5076                         min_tx_rate, max_tx_rate);
5077                 return -EINVAL;
5078         }
5079
5080         if (min_tx_rate && ice_is_dcb_active(pf)) {
5081                 dev_err(dev, "DCB on PF is currently enabled. VF min Tx rate limiting not allowed on this PF.\n");
5082                 return -EOPNOTSUPP;
5083         }
5084
5085         if (ice_min_tx_rate_oversubscribed(vf, min_tx_rate))
5086                 return -EINVAL;
5087
5088         if (vf->min_tx_rate != (unsigned int)min_tx_rate) {
5089                 ret = ice_set_min_bw_limit(vsi, (u64)min_tx_rate * 1000);
5090                 if (ret) {
5091                         dev_err(dev, "Unable to set min-tx-rate for VF %d\n",
5092                                 vf->vf_id);
5093                         return ret;
5094                 }
5095
5096                 vf->min_tx_rate = min_tx_rate;
5097         }
5098
5099         if (vf->max_tx_rate != (unsigned int)max_tx_rate) {
5100                 ret = ice_set_max_bw_limit(vsi, (u64)max_tx_rate * 1000);
5101                 if (ret) {
5102                         dev_err(dev, "Unable to set max-tx-rate for VF %d\n",
5103                                 vf->vf_id);
5104                         return ret;
5105                 }
5106
5107                 vf->max_tx_rate = max_tx_rate;
5108         }
5109
5110         return 0;
5111 }
5112
5113 /**
5114  * ice_get_vf_stats - populate some stats for the VF
5115  * @netdev: the netdev of the PF
5116  * @vf_id: the host OS identifier (0-255)
5117  * @vf_stats: pointer to the OS memory to be initialized
5118  */
5119 int ice_get_vf_stats(struct net_device *netdev, int vf_id,
5120                      struct ifla_vf_stats *vf_stats)
5121 {
5122         struct ice_pf *pf = ice_netdev_to_pf(netdev);
5123         struct ice_eth_stats *stats;
5124         struct ice_vsi *vsi;
5125         struct ice_vf *vf;
5126         int ret;
5127
5128         if (ice_validate_vf_id(pf, vf_id))
5129                 return -EINVAL;
5130
5131         vf = &pf->vf[vf_id];
5132         ret = ice_check_vf_ready_for_cfg(vf);
5133         if (ret)
5134                 return ret;
5135
5136         vsi = ice_get_vf_vsi(vf);
5137         if (!vsi)
5138                 return -EINVAL;
5139
5140         ice_update_eth_stats(vsi);
5141         stats = &vsi->eth_stats;
5142
5143         memset(vf_stats, 0, sizeof(*vf_stats));
5144
5145         vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
5146                 stats->rx_multicast;
5147         vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
5148                 stats->tx_multicast;
5149         vf_stats->rx_bytes   = stats->rx_bytes;
5150         vf_stats->tx_bytes   = stats->tx_bytes;
5151         vf_stats->broadcast  = stats->rx_broadcast;
5152         vf_stats->multicast  = stats->rx_multicast;
5153         vf_stats->rx_dropped = stats->rx_discards;
5154         vf_stats->tx_dropped = stats->tx_discards;
5155
5156         return 0;
5157 }
5158
5159 /**
5160  * ice_print_vf_rx_mdd_event - print VF Rx malicious driver detect event
5161  * @vf: pointer to the VF structure
5162  */
5163 void ice_print_vf_rx_mdd_event(struct ice_vf *vf)
5164 {
5165         struct ice_pf *pf = vf->pf;
5166         struct device *dev;
5167
5168         dev = ice_pf_to_dev(pf);
5169
5170         dev_info(dev, "%d Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pM. mdd-auto-reset-vfs=%s\n",
5171                  vf->mdd_rx_events.count, pf->hw.pf_id, vf->vf_id,
5172                  vf->dev_lan_addr.addr,
5173                  test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)
5174                           ? "on" : "off");
5175 }
5176
5177 /**
5178  * ice_print_vfs_mdd_events - print VFs malicious driver detect event
5179  * @pf: pointer to the PF structure
5180  *
5181  * Called from ice_handle_mdd_event to rate limit and print VFs MDD events.
5182  */
5183 void ice_print_vfs_mdd_events(struct ice_pf *pf)
5184 {
5185         struct device *dev = ice_pf_to_dev(pf);
5186         struct ice_hw *hw = &pf->hw;
5187         int i;
5188
5189         /* check that there are pending MDD events to print */
5190         if (!test_and_clear_bit(ICE_MDD_VF_PRINT_PENDING, pf->state))
5191                 return;
5192
5193         /* VF MDD event logs are rate limited to one second intervals */
5194         if (time_is_after_jiffies(pf->last_printed_mdd_jiffies + HZ * 1))
5195                 return;
5196
5197         pf->last_printed_mdd_jiffies = jiffies;
5198
5199         ice_for_each_vf(pf, i) {
5200                 struct ice_vf *vf = &pf->vf[i];
5201
5202                 /* only print Rx MDD event message if there are new events */
5203                 if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
5204                         vf->mdd_rx_events.last_printed =
5205                                                         vf->mdd_rx_events.count;
5206                         ice_print_vf_rx_mdd_event(vf);
5207                 }
5208
5209                 /* only print Tx MDD event message if there are new events */
5210                 if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
5211                         vf->mdd_tx_events.last_printed =
5212                                                         vf->mdd_tx_events.count;
5213
5214                         dev_info(dev, "%d Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pM.\n",
5215                                  vf->mdd_tx_events.count, hw->pf_id, i,
5216                                  vf->dev_lan_addr.addr);
5217                 }
5218         }
5219 }
5220
5221 /**
5222  * ice_restore_all_vfs_msi_state - restore VF MSI state after PF FLR
5223  * @pdev: pointer to a pci_dev structure
5224  *
5225  * Called when recovering from a PF FLR to restore interrupt capability to
5226  * the VFs.
5227  */
5228 void ice_restore_all_vfs_msi_state(struct pci_dev *pdev)
5229 {
5230         u16 vf_id;
5231         int pos;
5232
5233         if (!pci_num_vf(pdev))
5234                 return;
5235
5236         pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
5237         if (pos) {
5238                 struct pci_dev *vfdev;
5239
5240                 pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID,
5241                                      &vf_id);
5242                 vfdev = pci_get_device(pdev->vendor, vf_id, NULL);
5243                 while (vfdev) {
5244                         if (vfdev->is_virtfn && vfdev->physfn == pdev)
5245                                 pci_restore_msi_state(vfdev);
5246                         vfdev = pci_get_device(pdev->vendor, vf_id,
5247                                                vfdev);
5248                 }
5249         }
5250 }
5251
5252 /**
5253  * ice_is_malicious_vf - helper function to detect a malicious VF
5254  * @pf: ptr to struct ice_pf
5255  * @event: pointer to the AQ event
5256  * @num_msg_proc: the number of messages processed so far
5257  * @num_msg_pending: the number of messages peinding in admin queue
5258  */
5259 bool
5260 ice_is_malicious_vf(struct ice_pf *pf, struct ice_rq_event_info *event,
5261                     u16 num_msg_proc, u16 num_msg_pending)
5262 {
5263         s16 vf_id = le16_to_cpu(event->desc.retval);
5264         struct device *dev = ice_pf_to_dev(pf);
5265         struct ice_mbx_data mbxdata;
5266         bool malvf = false;
5267         struct ice_vf *vf;
5268         int status;
5269
5270         if (ice_validate_vf_id(pf, vf_id))
5271                 return false;
5272
5273         vf = &pf->vf[vf_id];
5274         /* Check if VF is disabled. */
5275         if (test_bit(ICE_VF_STATE_DIS, vf->vf_states))
5276                 return false;
5277
5278         mbxdata.num_msg_proc = num_msg_proc;
5279         mbxdata.num_pending_arq = num_msg_pending;
5280         mbxdata.max_num_msgs_mbx = pf->hw.mailboxq.num_rq_entries;
5281 #define ICE_MBX_OVERFLOW_WATERMARK 64
5282         mbxdata.async_watermark_val = ICE_MBX_OVERFLOW_WATERMARK;
5283
5284         /* check to see if we have a malicious VF */
5285         status = ice_mbx_vf_state_handler(&pf->hw, &mbxdata, vf_id, &malvf);
5286         if (status)
5287                 return false;
5288
5289         if (malvf) {
5290                 bool report_vf = false;
5291
5292                 /* if the VF is malicious and we haven't let the user
5293                  * know about it, then let them know now
5294                  */
5295                 status = ice_mbx_report_malvf(&pf->hw, pf->malvfs,
5296                                               ICE_MAX_VF_COUNT, vf_id,
5297                                               &report_vf);
5298                 if (status)
5299                         dev_dbg(dev, "Error reporting malicious VF\n");
5300
5301                 if (report_vf) {
5302                         struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
5303
5304                         if (pf_vsi)
5305                                 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",
5306                                          &vf->dev_lan_addr.addr[0],
5307                                          pf_vsi->netdev->dev_addr);
5308                 }
5309
5310                 return true;
5311         }
5312
5313         /* if there was an error in detection or the VF is not malicious then
5314          * return false
5315          */
5316         return false;
5317 }