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