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