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