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