Merge branch 'perf/urgent' into perf/core, to pick up fixes
[linux-2.6-microblaze.git] / drivers / net / ethernet / sfc / ef10_sriov.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3  * Driver for Solarflare network controllers and boards
4  * Copyright 2015 Solarflare Communications Inc.
5  */
6 #include <linux/etherdevice.h>
7 #include <linux/pci.h>
8 #include <linux/module.h>
9 #include "net_driver.h"
10 #include "ef10_sriov.h"
11 #include "efx.h"
12 #include "nic.h"
13 #include "mcdi_pcol.h"
14
15 static int efx_ef10_evb_port_assign(struct efx_nic *efx, unsigned int port_id,
16                                     unsigned int vf_fn)
17 {
18         MCDI_DECLARE_BUF(inbuf, MC_CMD_EVB_PORT_ASSIGN_IN_LEN);
19         struct efx_ef10_nic_data *nic_data = efx->nic_data;
20
21         MCDI_SET_DWORD(inbuf, EVB_PORT_ASSIGN_IN_PORT_ID, port_id);
22         MCDI_POPULATE_DWORD_2(inbuf, EVB_PORT_ASSIGN_IN_FUNCTION,
23                               EVB_PORT_ASSIGN_IN_PF, nic_data->pf_index,
24                               EVB_PORT_ASSIGN_IN_VF, vf_fn);
25
26         return efx_mcdi_rpc(efx, MC_CMD_EVB_PORT_ASSIGN, inbuf, sizeof(inbuf),
27                             NULL, 0, NULL);
28 }
29
30 static int efx_ef10_vswitch_alloc(struct efx_nic *efx, unsigned int port_id,
31                                   unsigned int vswitch_type)
32 {
33         MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_ALLOC_IN_LEN);
34         int rc;
35
36         MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
37         MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_TYPE, vswitch_type);
38         MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 2);
39         MCDI_POPULATE_DWORD_1(inbuf, VSWITCH_ALLOC_IN_FLAGS,
40                               VSWITCH_ALLOC_IN_FLAG_AUTO_PORT, 0);
41
42         /* Quietly try to allocate 2 VLAN tags */
43         rc = efx_mcdi_rpc_quiet(efx, MC_CMD_VSWITCH_ALLOC, inbuf, sizeof(inbuf),
44                                 NULL, 0, NULL);
45
46         /* If 2 VLAN tags is too many, revert to trying with 1 VLAN tags */
47         if (rc == -EPROTO) {
48                 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 1);
49                 rc = efx_mcdi_rpc(efx, MC_CMD_VSWITCH_ALLOC, inbuf,
50                                   sizeof(inbuf), NULL, 0, NULL);
51         } else if (rc) {
52                 efx_mcdi_display_error(efx, MC_CMD_VSWITCH_ALLOC,
53                                        MC_CMD_VSWITCH_ALLOC_IN_LEN,
54                                        NULL, 0, rc);
55         }
56         return rc;
57 }
58
59 static int efx_ef10_vswitch_free(struct efx_nic *efx, unsigned int port_id)
60 {
61         MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_FREE_IN_LEN);
62
63         MCDI_SET_DWORD(inbuf, VSWITCH_FREE_IN_UPSTREAM_PORT_ID, port_id);
64
65         return efx_mcdi_rpc(efx, MC_CMD_VSWITCH_FREE, inbuf, sizeof(inbuf),
66                             NULL, 0, NULL);
67 }
68
69 static int efx_ef10_vport_alloc(struct efx_nic *efx,
70                                 unsigned int port_id_in,
71                                 unsigned int vport_type,
72                                 u16 vlan,
73                                 unsigned int *port_id_out)
74 {
75         MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_ALLOC_IN_LEN);
76         MCDI_DECLARE_BUF(outbuf, MC_CMD_VPORT_ALLOC_OUT_LEN);
77         size_t outlen;
78         int rc;
79
80         EFX_WARN_ON_PARANOID(!port_id_out);
81
82         MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_UPSTREAM_PORT_ID, port_id_in);
83         MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_TYPE, vport_type);
84         MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_NUM_VLAN_TAGS,
85                        (vlan != EFX_EF10_NO_VLAN));
86         MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_FLAGS,
87                               VPORT_ALLOC_IN_FLAG_AUTO_PORT, 0);
88         if (vlan != EFX_EF10_NO_VLAN)
89                 MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_VLAN_TAGS,
90                                       VPORT_ALLOC_IN_VLAN_TAG_0, vlan);
91
92         rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_ALLOC, inbuf, sizeof(inbuf),
93                           outbuf, sizeof(outbuf), &outlen);
94         if (rc)
95                 return rc;
96         if (outlen < MC_CMD_VPORT_ALLOC_OUT_LEN)
97                 return -EIO;
98
99         *port_id_out = MCDI_DWORD(outbuf, VPORT_ALLOC_OUT_VPORT_ID);
100         return 0;
101 }
102
103 static int efx_ef10_vport_free(struct efx_nic *efx, unsigned int port_id)
104 {
105         MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_FREE_IN_LEN);
106
107         MCDI_SET_DWORD(inbuf, VPORT_FREE_IN_VPORT_ID, port_id);
108
109         return efx_mcdi_rpc(efx, MC_CMD_VPORT_FREE, inbuf, sizeof(inbuf),
110                             NULL, 0, NULL);
111 }
112
113 static void efx_ef10_sriov_free_vf_vports(struct efx_nic *efx)
114 {
115         struct efx_ef10_nic_data *nic_data = efx->nic_data;
116         int i;
117
118         if (!nic_data->vf)
119                 return;
120
121         for (i = 0; i < efx->vf_count; i++) {
122                 struct ef10_vf *vf = nic_data->vf + i;
123
124                 /* If VF is assigned, do not free the vport  */
125                 if (vf->pci_dev && pci_is_dev_assigned(vf->pci_dev))
126                         continue;
127
128                 if (vf->vport_assigned) {
129                         efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, i);
130                         vf->vport_assigned = 0;
131                 }
132
133                 if (!is_zero_ether_addr(vf->mac)) {
134                         efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
135                         eth_zero_addr(vf->mac);
136                 }
137
138                 if (vf->vport_id) {
139                         efx_ef10_vport_free(efx, vf->vport_id);
140                         vf->vport_id = 0;
141                 }
142
143                 vf->efx = NULL;
144         }
145 }
146
147 static void efx_ef10_sriov_free_vf_vswitching(struct efx_nic *efx)
148 {
149         struct efx_ef10_nic_data *nic_data = efx->nic_data;
150
151         efx_ef10_sriov_free_vf_vports(efx);
152         kfree(nic_data->vf);
153         nic_data->vf = NULL;
154 }
155
156 static int efx_ef10_sriov_assign_vf_vport(struct efx_nic *efx,
157                                           unsigned int vf_i)
158 {
159         struct efx_ef10_nic_data *nic_data = efx->nic_data;
160         struct ef10_vf *vf = nic_data->vf + vf_i;
161         int rc;
162
163         if (WARN_ON_ONCE(!nic_data->vf))
164                 return -EOPNOTSUPP;
165
166         rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
167                                   MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
168                                   vf->vlan, &vf->vport_id);
169         if (rc)
170                 return rc;
171
172         rc = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
173         if (rc) {
174                 eth_zero_addr(vf->mac);
175                 return rc;
176         }
177
178         rc =  efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
179         if (rc)
180                 return rc;
181
182         vf->vport_assigned = 1;
183         return 0;
184 }
185
186 static int efx_ef10_sriov_alloc_vf_vswitching(struct efx_nic *efx)
187 {
188         struct efx_ef10_nic_data *nic_data = efx->nic_data;
189         unsigned int i;
190         int rc;
191
192         nic_data->vf = kcalloc(efx->vf_count, sizeof(struct ef10_vf),
193                                GFP_KERNEL);
194         if (!nic_data->vf)
195                 return -ENOMEM;
196
197         for (i = 0; i < efx->vf_count; i++) {
198                 eth_random_addr(nic_data->vf[i].mac);
199                 nic_data->vf[i].efx = NULL;
200                 nic_data->vf[i].vlan = EFX_EF10_NO_VLAN;
201
202                 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
203                 if (rc)
204                         goto fail;
205         }
206
207         return 0;
208 fail:
209         efx_ef10_sriov_free_vf_vswitching(efx);
210         return rc;
211 }
212
213 static int efx_ef10_sriov_restore_vf_vswitching(struct efx_nic *efx)
214 {
215         unsigned int i;
216         int rc;
217
218         for (i = 0; i < efx->vf_count; i++) {
219                 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
220                 if (rc)
221                         goto fail;
222         }
223
224         return 0;
225 fail:
226         efx_ef10_sriov_free_vf_vswitching(efx);
227         return rc;
228 }
229
230 static int efx_ef10_vadaptor_alloc_set_features(struct efx_nic *efx)
231 {
232         u32 port_flags;
233         int rc;
234
235         rc = efx_ef10_vadaptor_alloc(efx, efx->vport_id);
236         if (rc)
237                 goto fail_vadaptor_alloc;
238
239         rc = efx_ef10_vadaptor_query(efx, efx->vport_id,
240                                      &port_flags, NULL, NULL);
241         if (rc)
242                 goto fail_vadaptor_query;
243
244         if (port_flags &
245             (1 << MC_CMD_VPORT_ALLOC_IN_FLAG_VLAN_RESTRICT_LBN))
246                 efx->fixed_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
247         else
248                 efx->fixed_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
249
250         return 0;
251
252 fail_vadaptor_query:
253         efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
254 fail_vadaptor_alloc:
255         return rc;
256 }
257
258 /* On top of the default firmware vswitch setup, create a VEB vswitch and
259  * expansion vport for use by this function.
260  */
261 int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
262 {
263         struct efx_ef10_nic_data *nic_data = efx->nic_data;
264         struct net_device *net_dev = efx->net_dev;
265         int rc;
266
267         if (pci_sriov_get_totalvfs(efx->pci_dev) <= 0) {
268                 /* vswitch not needed as we have no VFs */
269                 efx_ef10_vadaptor_alloc_set_features(efx);
270                 return 0;
271         }
272
273         rc = efx_ef10_vswitch_alloc(efx, EVB_PORT_ID_ASSIGNED,
274                                     MC_CMD_VSWITCH_ALLOC_IN_VSWITCH_TYPE_VEB);
275         if (rc)
276                 goto fail1;
277
278         rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
279                                   MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
280                                   EFX_EF10_NO_VLAN, &efx->vport_id);
281         if (rc)
282                 goto fail2;
283
284         rc = efx_ef10_vport_add_mac(efx, efx->vport_id, net_dev->dev_addr);
285         if (rc)
286                 goto fail3;
287         ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
288
289         rc = efx_ef10_vadaptor_alloc_set_features(efx);
290         if (rc)
291                 goto fail4;
292
293         return 0;
294 fail4:
295         efx_ef10_vport_del_mac(efx, efx->vport_id, nic_data->vport_mac);
296         eth_zero_addr(nic_data->vport_mac);
297 fail3:
298         efx_ef10_vport_free(efx, efx->vport_id);
299         efx->vport_id = EVB_PORT_ID_ASSIGNED;
300 fail2:
301         efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
302 fail1:
303         return rc;
304 }
305
306 int efx_ef10_vswitching_probe_vf(struct efx_nic *efx)
307 {
308         return efx_ef10_vadaptor_alloc_set_features(efx);
309 }
310
311 int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
312 {
313         struct efx_ef10_nic_data *nic_data = efx->nic_data;
314         int rc;
315
316         if (!nic_data->must_probe_vswitching)
317                 return 0;
318
319         rc = efx_ef10_vswitching_probe_pf(efx);
320         if (rc)
321                 goto fail;
322
323         rc = efx_ef10_sriov_restore_vf_vswitching(efx);
324         if (rc)
325                 goto fail;
326
327         nic_data->must_probe_vswitching = false;
328 fail:
329         return rc;
330 }
331
332 int efx_ef10_vswitching_restore_vf(struct efx_nic *efx)
333 {
334         struct efx_ef10_nic_data *nic_data = efx->nic_data;
335         int rc;
336
337         if (!nic_data->must_probe_vswitching)
338                 return 0;
339
340         rc = efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
341         if (rc)
342                 return rc;
343
344         nic_data->must_probe_vswitching = false;
345         return 0;
346 }
347
348 void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
349 {
350         struct efx_ef10_nic_data *nic_data = efx->nic_data;
351
352         efx_ef10_sriov_free_vf_vswitching(efx);
353
354         efx_ef10_vadaptor_free(efx, efx->vport_id);
355
356         if (efx->vport_id == EVB_PORT_ID_ASSIGNED)
357                 return; /* No vswitch was ever created */
358
359         if (!is_zero_ether_addr(nic_data->vport_mac)) {
360                 efx_ef10_vport_del_mac(efx, efx->vport_id,
361                                        efx->net_dev->dev_addr);
362                 eth_zero_addr(nic_data->vport_mac);
363         }
364         efx_ef10_vport_free(efx, efx->vport_id);
365         efx->vport_id = EVB_PORT_ID_ASSIGNED;
366
367         /* Only free the vswitch if no VFs are assigned */
368         if (!pci_vfs_assigned(efx->pci_dev))
369                 efx_ef10_vswitch_free(efx, efx->vport_id);
370 }
371
372 void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
373 {
374         efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
375 }
376
377 static int efx_ef10_pci_sriov_enable(struct efx_nic *efx, int num_vfs)
378 {
379         int rc = 0;
380         struct pci_dev *dev = efx->pci_dev;
381
382         efx->vf_count = num_vfs;
383
384         rc = efx_ef10_sriov_alloc_vf_vswitching(efx);
385         if (rc)
386                 goto fail1;
387
388         rc = pci_enable_sriov(dev, num_vfs);
389         if (rc)
390                 goto fail2;
391
392         return 0;
393 fail2:
394         efx_ef10_sriov_free_vf_vswitching(efx);
395 fail1:
396         efx->vf_count = 0;
397         netif_err(efx, probe, efx->net_dev,
398                   "Failed to enable SRIOV VFs\n");
399         return rc;
400 }
401
402 /* Disable SRIOV and remove VFs
403  * If some VFs are attached to a guest (using Xen, only) nothing is
404  * done if force=false, and vports are freed if force=true (for the non
405  * attachedc ones, only) but SRIOV is not disabled and VFs are not
406  * removed in either case.
407  */
408 static int efx_ef10_pci_sriov_disable(struct efx_nic *efx, bool force)
409 {
410         struct pci_dev *dev = efx->pci_dev;
411         unsigned int vfs_assigned = pci_vfs_assigned(dev);
412         int rc = 0;
413
414         if (vfs_assigned && !force) {
415                 netif_info(efx, drv, efx->net_dev, "VFs are assigned to guests; "
416                            "please detach them before disabling SR-IOV\n");
417                 return -EBUSY;
418         }
419
420         if (!vfs_assigned)
421                 pci_disable_sriov(dev);
422         else
423                 rc = -EBUSY;
424
425         efx_ef10_sriov_free_vf_vswitching(efx);
426         efx->vf_count = 0;
427         return rc;
428 }
429
430 int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
431 {
432         if (num_vfs == 0)
433                 return efx_ef10_pci_sriov_disable(efx, false);
434         else
435                 return efx_ef10_pci_sriov_enable(efx, num_vfs);
436 }
437
438 int efx_ef10_sriov_init(struct efx_nic *efx)
439 {
440         return 0;
441 }
442
443 void efx_ef10_sriov_fini(struct efx_nic *efx)
444 {
445         struct efx_ef10_nic_data *nic_data = efx->nic_data;
446         int rc;
447
448         if (!nic_data->vf) {
449                 /* Remove any un-assigned orphaned VFs. This can happen if the PF driver
450                  * was unloaded while any VF was assigned to a guest (using Xen, only).
451                  */
452                 if (pci_num_vf(efx->pci_dev) && !pci_vfs_assigned(efx->pci_dev))
453                         pci_disable_sriov(efx->pci_dev);
454                 return;
455         }
456
457         /* Disable SRIOV and remove any VFs in the host */
458         rc = efx_ef10_pci_sriov_disable(efx, true);
459         if (rc)
460                 netif_dbg(efx, drv, efx->net_dev,
461                           "Disabling SRIOV was not successful rc=%d\n", rc);
462         else
463                 netif_dbg(efx, drv, efx->net_dev, "SRIOV disabled\n");
464 }
465
466 static int efx_ef10_vport_del_vf_mac(struct efx_nic *efx, unsigned int port_id,
467                                      u8 *mac)
468 {
469         MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
470         MCDI_DECLARE_BUF_ERR(outbuf);
471         size_t outlen;
472         int rc;
473
474         MCDI_SET_DWORD(inbuf, VPORT_DEL_MAC_ADDRESS_IN_VPORT_ID, port_id);
475         ether_addr_copy(MCDI_PTR(inbuf, VPORT_DEL_MAC_ADDRESS_IN_MACADDR), mac);
476
477         rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
478                           sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
479
480         return rc;
481 }
482
483 int efx_ef10_sriov_set_vf_mac(struct efx_nic *efx, int vf_i, u8 *mac)
484 {
485         struct efx_ef10_nic_data *nic_data = efx->nic_data;
486         struct ef10_vf *vf;
487         int rc;
488
489         if (!nic_data->vf)
490                 return -EOPNOTSUPP;
491
492         if (vf_i >= efx->vf_count)
493                 return -EINVAL;
494         vf = nic_data->vf + vf_i;
495
496         if (vf->efx) {
497                 efx_device_detach_sync(vf->efx);
498                 efx_net_stop(vf->efx->net_dev);
499
500                 down_write(&vf->efx->filter_sem);
501                 vf->efx->type->filter_table_remove(vf->efx);
502
503                 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
504                 if (rc) {
505                         up_write(&vf->efx->filter_sem);
506                         return rc;
507                 }
508         }
509
510         rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
511         if (rc)
512                 return rc;
513
514         if (!is_zero_ether_addr(vf->mac)) {
515                 rc = efx_ef10_vport_del_vf_mac(efx, vf->vport_id, vf->mac);
516                 if (rc)
517                         return rc;
518         }
519
520         if (!is_zero_ether_addr(mac)) {
521                 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, mac);
522                 if (rc)
523                         goto fail;
524
525                 if (vf->efx)
526                         ether_addr_copy(vf->efx->net_dev->dev_addr, mac);
527         }
528
529         ether_addr_copy(vf->mac, mac);
530
531         rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
532         if (rc)
533                 goto fail;
534
535         if (vf->efx) {
536                 /* VF cannot use the vport_id that the PF created */
537                 rc = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
538                 if (rc) {
539                         up_write(&vf->efx->filter_sem);
540                         return rc;
541                 }
542                 vf->efx->type->filter_table_probe(vf->efx);
543                 up_write(&vf->efx->filter_sem);
544                 efx_net_open(vf->efx->net_dev);
545                 efx_device_attach_if_not_resetting(vf->efx);
546         }
547
548         return 0;
549
550 fail:
551         eth_zero_addr(vf->mac);
552         return rc;
553 }
554
555 int efx_ef10_sriov_set_vf_vlan(struct efx_nic *efx, int vf_i, u16 vlan,
556                                u8 qos)
557 {
558         struct efx_ef10_nic_data *nic_data = efx->nic_data;
559         struct ef10_vf *vf;
560         u16 new_vlan;
561         int rc = 0, rc2 = 0;
562
563         if (vf_i >= efx->vf_count)
564                 return -EINVAL;
565         if (qos != 0)
566                 return -EINVAL;
567
568         vf = nic_data->vf + vf_i;
569
570         new_vlan = (vlan == 0) ? EFX_EF10_NO_VLAN : vlan;
571         if (new_vlan == vf->vlan)
572                 return 0;
573
574         if (vf->efx) {
575                 efx_device_detach_sync(vf->efx);
576                 efx_net_stop(vf->efx->net_dev);
577
578                 mutex_lock(&vf->efx->mac_lock);
579                 down_write(&vf->efx->filter_sem);
580                 vf->efx->type->filter_table_remove(vf->efx);
581
582                 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
583                 if (rc)
584                         goto restore_filters;
585         }
586
587         if (vf->vport_assigned) {
588                 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
589                 if (rc) {
590                         netif_warn(efx, drv, efx->net_dev,
591                                    "Failed to change vlan on VF %d.\n", vf_i);
592                         netif_warn(efx, drv, efx->net_dev,
593                                    "This is likely because the VF is bound to a driver in a VM.\n");
594                         netif_warn(efx, drv, efx->net_dev,
595                                    "Please unload the driver in the VM.\n");
596                         goto restore_vadaptor;
597                 }
598                 vf->vport_assigned = 0;
599         }
600
601         if (!is_zero_ether_addr(vf->mac)) {
602                 rc = efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
603                 if (rc)
604                         goto restore_evb_port;
605         }
606
607         if (vf->vport_id) {
608                 rc = efx_ef10_vport_free(efx, vf->vport_id);
609                 if (rc)
610                         goto restore_mac;
611                 vf->vport_id = 0;
612         }
613
614         /* Do the actual vlan change */
615         vf->vlan = new_vlan;
616
617         /* Restore everything in reverse order */
618         rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
619                                   MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
620                                   vf->vlan, &vf->vport_id);
621         if (rc)
622                 goto reset_nic_up_write;
623
624 restore_mac:
625         if (!is_zero_ether_addr(vf->mac)) {
626                 rc2 = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
627                 if (rc2) {
628                         eth_zero_addr(vf->mac);
629                         goto reset_nic_up_write;
630                 }
631         }
632
633 restore_evb_port:
634         rc2 = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
635         if (rc2)
636                 goto reset_nic_up_write;
637         else
638                 vf->vport_assigned = 1;
639
640 restore_vadaptor:
641         if (vf->efx) {
642                 rc2 = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
643                 if (rc2)
644                         goto reset_nic_up_write;
645         }
646
647 restore_filters:
648         if (vf->efx) {
649                 rc2 = vf->efx->type->filter_table_probe(vf->efx);
650                 if (rc2)
651                         goto reset_nic_up_write;
652
653                 up_write(&vf->efx->filter_sem);
654                 mutex_unlock(&vf->efx->mac_lock);
655
656                 rc2 = efx_net_open(vf->efx->net_dev);
657                 if (rc2)
658                         goto reset_nic;
659
660                 efx_device_attach_if_not_resetting(vf->efx);
661         }
662         return rc;
663
664 reset_nic_up_write:
665         if (vf->efx) {
666                 up_write(&vf->efx->filter_sem);
667                 mutex_unlock(&vf->efx->mac_lock);
668         }
669 reset_nic:
670         if (vf->efx) {
671                 netif_err(efx, drv, efx->net_dev,
672                           "Failed to restore VF - scheduling reset.\n");
673                 efx_schedule_reset(vf->efx, RESET_TYPE_DATAPATH);
674         } else {
675                 netif_err(efx, drv, efx->net_dev,
676                           "Failed to restore the VF and cannot reset the VF "
677                           "- VF is not functional.\n");
678                 netif_err(efx, drv, efx->net_dev,
679                           "Please reload the driver attached to the VF.\n");
680         }
681
682         return rc ? rc : rc2;
683 }
684
685 static int efx_ef10_sriov_set_privilege_mask(struct efx_nic *efx, int vf_i,
686                                              u32 mask, u32 value)
687 {
688         MCDI_DECLARE_BUF(pm_outbuf, MC_CMD_PRIVILEGE_MASK_OUT_LEN);
689         MCDI_DECLARE_BUF(pm_inbuf, MC_CMD_PRIVILEGE_MASK_IN_LEN);
690         struct efx_ef10_nic_data *nic_data = efx->nic_data;
691         u32 old_mask, new_mask;
692         size_t outlen;
693         int rc;
694
695         EFX_WARN_ON_PARANOID((value & ~mask) != 0);
696
697         /* Get privilege mask */
698         MCDI_POPULATE_DWORD_2(pm_inbuf, PRIVILEGE_MASK_IN_FUNCTION,
699                               PRIVILEGE_MASK_IN_FUNCTION_PF, nic_data->pf_index,
700                               PRIVILEGE_MASK_IN_FUNCTION_VF, vf_i);
701
702         rc = efx_mcdi_rpc(efx, MC_CMD_PRIVILEGE_MASK,
703                           pm_inbuf, sizeof(pm_inbuf),
704                           pm_outbuf, sizeof(pm_outbuf), &outlen);
705
706         if (rc != 0)
707                 return rc;
708         if (outlen != MC_CMD_PRIVILEGE_MASK_OUT_LEN)
709                 return -EIO;
710
711         old_mask = MCDI_DWORD(pm_outbuf, PRIVILEGE_MASK_OUT_OLD_MASK);
712
713         new_mask = old_mask & ~mask;
714         new_mask |= value;
715
716         if (new_mask == old_mask)
717                 return 0;
718
719         new_mask |= MC_CMD_PRIVILEGE_MASK_IN_DO_CHANGE;
720
721         /* Set privilege mask */
722         MCDI_SET_DWORD(pm_inbuf, PRIVILEGE_MASK_IN_NEW_MASK, new_mask);
723
724         rc = efx_mcdi_rpc(efx, MC_CMD_PRIVILEGE_MASK,
725                           pm_inbuf, sizeof(pm_inbuf),
726                           pm_outbuf, sizeof(pm_outbuf), &outlen);
727
728         if (rc != 0)
729                 return rc;
730         if (outlen != MC_CMD_PRIVILEGE_MASK_OUT_LEN)
731                 return -EIO;
732
733         return 0;
734 }
735
736 int efx_ef10_sriov_set_vf_spoofchk(struct efx_nic *efx, int vf_i, bool spoofchk)
737 {
738         struct efx_ef10_nic_data *nic_data = efx->nic_data;
739
740         /* Can't enable spoofchk if firmware doesn't support it. */
741         if (!(nic_data->datapath_caps &
742               BIT(MC_CMD_GET_CAPABILITIES_OUT_TX_MAC_SECURITY_FILTERING_LBN)) &&
743             spoofchk)
744                 return -EOPNOTSUPP;
745
746         return efx_ef10_sriov_set_privilege_mask(efx, vf_i,
747                 MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING_TX,
748                 spoofchk ? 0 : MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING_TX);
749 }
750
751 int efx_ef10_sriov_set_vf_link_state(struct efx_nic *efx, int vf_i,
752                                      int link_state)
753 {
754         MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
755         struct efx_ef10_nic_data *nic_data = efx->nic_data;
756
757         BUILD_BUG_ON(IFLA_VF_LINK_STATE_AUTO !=
758                      MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_AUTO);
759         BUILD_BUG_ON(IFLA_VF_LINK_STATE_ENABLE !=
760                      MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_UP);
761         BUILD_BUG_ON(IFLA_VF_LINK_STATE_DISABLE !=
762                      MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_DOWN);
763         MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
764                               LINK_STATE_MODE_IN_FUNCTION_PF,
765                               nic_data->pf_index,
766                               LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
767         MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE, link_state);
768         return efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
769                             NULL, 0, NULL); /* don't care what old mode was */
770 }
771
772 int efx_ef10_sriov_get_vf_config(struct efx_nic *efx, int vf_i,
773                                  struct ifla_vf_info *ivf)
774 {
775         MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
776         MCDI_DECLARE_BUF(outbuf, MC_CMD_LINK_STATE_MODE_OUT_LEN);
777
778         struct efx_ef10_nic_data *nic_data = efx->nic_data;
779         struct ef10_vf *vf;
780         size_t outlen;
781         int rc;
782
783         if (vf_i >= efx->vf_count)
784                 return -EINVAL;
785
786         if (!nic_data->vf)
787                 return -EOPNOTSUPP;
788
789         vf = nic_data->vf + vf_i;
790
791         ivf->vf = vf_i;
792         ivf->min_tx_rate = 0;
793         ivf->max_tx_rate = 0;
794         ether_addr_copy(ivf->mac, vf->mac);
795         ivf->vlan = (vf->vlan == EFX_EF10_NO_VLAN) ? 0 : vf->vlan;
796         ivf->qos = 0;
797
798         MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
799                               LINK_STATE_MODE_IN_FUNCTION_PF,
800                               nic_data->pf_index,
801                               LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
802         MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE,
803                        MC_CMD_LINK_STATE_MODE_IN_DO_NOT_CHANGE);
804         rc = efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
805                           outbuf, sizeof(outbuf), &outlen);
806         if (rc)
807                 return rc;
808         if (outlen < MC_CMD_LINK_STATE_MODE_OUT_LEN)
809                 return -EIO;
810         ivf->linkstate = MCDI_DWORD(outbuf, LINK_STATE_MODE_OUT_OLD_MODE);
811
812         return 0;
813 }