GNU Linux-libre 4.4.289-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 /* On top of the default firmware vswitch setup, create a VEB vswitch and
236  * expansion vport for use by this function.
237  */
238 int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
239 {
240         struct efx_ef10_nic_data *nic_data = efx->nic_data;
241         struct net_device *net_dev = efx->net_dev;
242         int rc;
243
244         if (pci_sriov_get_totalvfs(efx->pci_dev) <= 0) {
245                 /* vswitch not needed as we have no VFs */
246                 efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
247                 return 0;
248         }
249
250         rc = efx_ef10_vswitch_alloc(efx, EVB_PORT_ID_ASSIGNED,
251                                     MC_CMD_VSWITCH_ALLOC_IN_VSWITCH_TYPE_VEB);
252         if (rc)
253                 goto fail1;
254
255         rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
256                                   MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
257                                   EFX_EF10_NO_VLAN, &nic_data->vport_id);
258         if (rc)
259                 goto fail2;
260
261         rc = efx_ef10_vport_add_mac(efx, nic_data->vport_id, net_dev->dev_addr);
262         if (rc)
263                 goto fail3;
264         ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
265
266         rc = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
267         if (rc)
268                 goto fail4;
269
270         return 0;
271 fail4:
272         efx_ef10_vport_del_mac(efx, nic_data->vport_id, nic_data->vport_mac);
273         eth_zero_addr(nic_data->vport_mac);
274 fail3:
275         efx_ef10_vport_free(efx, nic_data->vport_id);
276         nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
277 fail2:
278         efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
279 fail1:
280         return rc;
281 }
282
283 int efx_ef10_vswitching_probe_vf(struct efx_nic *efx)
284 {
285         struct efx_ef10_nic_data *nic_data = efx->nic_data;
286
287         return efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
288 }
289
290 int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
291 {
292         struct efx_ef10_nic_data *nic_data = efx->nic_data;
293         int rc;
294
295         if (!nic_data->must_probe_vswitching)
296                 return 0;
297
298         rc = efx_ef10_vswitching_probe_pf(efx);
299         if (rc)
300                 goto fail;
301
302         rc = efx_ef10_sriov_restore_vf_vswitching(efx);
303         if (rc)
304                 goto fail;
305
306         nic_data->must_probe_vswitching = false;
307 fail:
308         return rc;
309 }
310
311 int efx_ef10_vswitching_restore_vf(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_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
320         if (rc)
321                 return rc;
322
323         nic_data->must_probe_vswitching = false;
324         return 0;
325 }
326
327 void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
328 {
329         struct efx_ef10_nic_data *nic_data = efx->nic_data;
330
331         efx_ef10_sriov_free_vf_vswitching(efx);
332
333         efx_ef10_vadaptor_free(efx, nic_data->vport_id);
334
335         if (nic_data->vport_id == EVB_PORT_ID_ASSIGNED)
336                 return; /* No vswitch was ever created */
337
338         if (!is_zero_ether_addr(nic_data->vport_mac)) {
339                 efx_ef10_vport_del_mac(efx, nic_data->vport_id,
340                                        efx->net_dev->dev_addr);
341                 eth_zero_addr(nic_data->vport_mac);
342         }
343         efx_ef10_vport_free(efx, nic_data->vport_id);
344         nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
345
346         /* Only free the vswitch if no VFs are assigned */
347         if (!pci_vfs_assigned(efx->pci_dev))
348                 efx_ef10_vswitch_free(efx, nic_data->vport_id);
349 }
350
351 void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
352 {
353         efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
354 }
355
356 static int efx_ef10_pci_sriov_enable(struct efx_nic *efx, int num_vfs)
357 {
358         int rc = 0;
359         struct pci_dev *dev = efx->pci_dev;
360
361         efx->vf_count = num_vfs;
362
363         rc = efx_ef10_sriov_alloc_vf_vswitching(efx);
364         if (rc)
365                 goto fail1;
366
367         rc = pci_enable_sriov(dev, num_vfs);
368         if (rc)
369                 goto fail2;
370
371         return 0;
372 fail2:
373         efx_ef10_sriov_free_vf_vswitching(efx);
374 fail1:
375         efx->vf_count = 0;
376         netif_err(efx, probe, efx->net_dev,
377                   "Failed to enable SRIOV VFs\n");
378         return rc;
379 }
380
381 /* Disable SRIOV and remove VFs
382  * If some VFs are attached to a guest (using Xen, only) nothing is
383  * done if force=false, and vports are freed if force=true (for the non
384  * attachedc ones, only) but SRIOV is not disabled and VFs are not
385  * removed in either case.
386  */
387 static int efx_ef10_pci_sriov_disable(struct efx_nic *efx, bool force)
388 {
389         struct pci_dev *dev = efx->pci_dev;
390         unsigned int vfs_assigned = pci_vfs_assigned(dev);
391         int rc = 0;
392
393         if (vfs_assigned && !force) {
394                 netif_info(efx, drv, efx->net_dev, "VFs are assigned to guests; "
395                            "please detach them before disabling SR-IOV\n");
396                 return -EBUSY;
397         }
398
399         if (!vfs_assigned)
400                 pci_disable_sriov(dev);
401         else
402                 rc = -EBUSY;
403
404         efx_ef10_sriov_free_vf_vswitching(efx);
405         efx->vf_count = 0;
406         return rc;
407 }
408
409 int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
410 {
411         if (num_vfs == 0)
412                 return efx_ef10_pci_sriov_disable(efx, false);
413         else
414                 return efx_ef10_pci_sriov_enable(efx, num_vfs);
415 }
416
417 int efx_ef10_sriov_init(struct efx_nic *efx)
418 {
419         return 0;
420 }
421
422 void efx_ef10_sriov_fini(struct efx_nic *efx)
423 {
424         struct efx_ef10_nic_data *nic_data = efx->nic_data;
425         int rc;
426
427         if (!nic_data->vf) {
428                 /* Remove any un-assigned orphaned VFs */
429                 if (pci_num_vf(efx->pci_dev) && !pci_vfs_assigned(efx->pci_dev))
430                         pci_disable_sriov(efx->pci_dev);
431                 return;
432         }
433
434         /* Disable SRIOV and remove any VFs in the host */
435         rc = efx_ef10_pci_sriov_disable(efx, true);
436         if (rc)
437                 netif_dbg(efx, drv, efx->net_dev,
438                           "Disabling SRIOV was not successful rc=%d\n", rc);
439         else
440                 netif_dbg(efx, drv, efx->net_dev, "SRIOV disabled\n");
441 }
442
443 static int efx_ef10_vport_del_vf_mac(struct efx_nic *efx, unsigned int port_id,
444                                      u8 *mac)
445 {
446         MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
447         MCDI_DECLARE_BUF_ERR(outbuf);
448         size_t outlen;
449         int rc;
450
451         MCDI_SET_DWORD(inbuf, VPORT_DEL_MAC_ADDRESS_IN_VPORT_ID, port_id);
452         ether_addr_copy(MCDI_PTR(inbuf, VPORT_DEL_MAC_ADDRESS_IN_MACADDR), mac);
453
454         rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
455                           sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
456
457         return rc;
458 }
459
460 int efx_ef10_sriov_set_vf_mac(struct efx_nic *efx, int vf_i, u8 *mac)
461 {
462         struct efx_ef10_nic_data *nic_data = efx->nic_data;
463         struct ef10_vf *vf;
464         int rc;
465
466         if (!nic_data->vf)
467                 return -EOPNOTSUPP;
468
469         if (vf_i >= efx->vf_count)
470                 return -EINVAL;
471         vf = nic_data->vf + vf_i;
472
473         if (vf->efx) {
474                 efx_device_detach_sync(vf->efx);
475                 efx_net_stop(vf->efx->net_dev);
476
477                 down_write(&vf->efx->filter_sem);
478                 vf->efx->type->filter_table_remove(vf->efx);
479
480                 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
481                 if (rc) {
482                         up_write(&vf->efx->filter_sem);
483                         return rc;
484                 }
485         }
486
487         rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
488         if (rc)
489                 return rc;
490
491         if (!is_zero_ether_addr(vf->mac)) {
492                 rc = efx_ef10_vport_del_vf_mac(efx, vf->vport_id, vf->mac);
493                 if (rc)
494                         return rc;
495         }
496
497         if (!is_zero_ether_addr(mac)) {
498                 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, mac);
499                 if (rc) {
500                         eth_zero_addr(vf->mac);
501                         goto fail;
502                 }
503                 if (vf->efx)
504                         ether_addr_copy(vf->efx->net_dev->dev_addr, mac);
505         }
506
507         ether_addr_copy(vf->mac, mac);
508
509         rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
510         if (rc)
511                 goto fail;
512
513         if (vf->efx) {
514                 /* VF cannot use the vport_id that the PF created */
515                 rc = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
516                 if (rc) {
517                         up_write(&vf->efx->filter_sem);
518                         return rc;
519                 }
520                 vf->efx->type->filter_table_probe(vf->efx);
521                 up_write(&vf->efx->filter_sem);
522                 efx_net_open(vf->efx->net_dev);
523                 netif_device_attach(vf->efx->net_dev);
524         }
525
526         return 0;
527
528 fail:
529         memset(vf->mac, 0, ETH_ALEN);
530         return rc;
531 }
532
533 int efx_ef10_sriov_set_vf_vlan(struct efx_nic *efx, int vf_i, u16 vlan,
534                                u8 qos)
535 {
536         struct efx_ef10_nic_data *nic_data = efx->nic_data;
537         struct ef10_vf *vf;
538         u16 old_vlan, new_vlan;
539         int rc = 0, rc2 = 0;
540
541         if (vf_i >= efx->vf_count)
542                 return -EINVAL;
543         if (qos != 0)
544                 return -EINVAL;
545
546         vf = nic_data->vf + vf_i;
547
548         new_vlan = (vlan == 0) ? EFX_EF10_NO_VLAN : vlan;
549         if (new_vlan == vf->vlan)
550                 return 0;
551
552         if (vf->efx) {
553                 efx_device_detach_sync(vf->efx);
554                 efx_net_stop(vf->efx->net_dev);
555
556                 down_write(&vf->efx->filter_sem);
557                 vf->efx->type->filter_table_remove(vf->efx);
558
559                 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
560                 if (rc)
561                         goto restore_filters;
562         }
563
564         if (vf->vport_assigned) {
565                 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
566                 if (rc) {
567                         netif_warn(efx, drv, efx->net_dev,
568                                    "Failed to change vlan on VF %d.\n", vf_i);
569                         netif_warn(efx, drv, efx->net_dev,
570                                    "This is likely because the VF is bound to a driver in a VM.\n");
571                         netif_warn(efx, drv, efx->net_dev,
572                                    "Please unload the driver in the VM.\n");
573                         goto restore_vadaptor;
574                 }
575                 vf->vport_assigned = 0;
576         }
577
578         if (!is_zero_ether_addr(vf->mac)) {
579                 rc = efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
580                 if (rc)
581                         goto restore_evb_port;
582         }
583
584         if (vf->vport_id) {
585                 rc = efx_ef10_vport_free(efx, vf->vport_id);
586                 if (rc)
587                         goto restore_mac;
588                 vf->vport_id = 0;
589         }
590
591         /* Do the actual vlan change */
592         old_vlan = vf->vlan;
593         vf->vlan = new_vlan;
594
595         /* Restore everything in reverse order */
596         rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
597                                   MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
598                                   vf->vlan, &vf->vport_id);
599         if (rc)
600                 goto reset_nic_up_write;
601
602 restore_mac:
603         if (!is_zero_ether_addr(vf->mac)) {
604                 rc2 = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
605                 if (rc2) {
606                         eth_zero_addr(vf->mac);
607                         goto reset_nic_up_write;
608                 }
609         }
610
611 restore_evb_port:
612         rc2 = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
613         if (rc2)
614                 goto reset_nic_up_write;
615         else
616                 vf->vport_assigned = 1;
617
618 restore_vadaptor:
619         if (vf->efx) {
620                 rc2 = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
621                 if (rc2)
622                         goto reset_nic_up_write;
623         }
624
625 restore_filters:
626         if (vf->efx) {
627                 rc2 = vf->efx->type->filter_table_probe(vf->efx);
628                 if (rc2)
629                         goto reset_nic_up_write;
630
631                 up_write(&vf->efx->filter_sem);
632
633                 up_write(&vf->efx->filter_sem);
634
635                 rc2 = efx_net_open(vf->efx->net_dev);
636                 if (rc2)
637                         goto reset_nic;
638
639                 netif_device_attach(vf->efx->net_dev);
640         }
641         return rc;
642
643 reset_nic_up_write:
644         if (vf->efx)
645                 up_write(&vf->efx->filter_sem);
646
647 reset_nic:
648         if (vf->efx) {
649                 netif_err(efx, drv, efx->net_dev,
650                           "Failed to restore VF - scheduling reset.\n");
651                 efx_schedule_reset(vf->efx, RESET_TYPE_DATAPATH);
652         } else {
653                 netif_err(efx, drv, efx->net_dev,
654                           "Failed to restore the VF and cannot reset the VF "
655                           "- VF is not functional.\n");
656                 netif_err(efx, drv, efx->net_dev,
657                           "Please reload the driver attached to the VF.\n");
658         }
659
660         return rc ? rc : rc2;
661 }
662
663 int efx_ef10_sriov_set_vf_spoofchk(struct efx_nic *efx, int vf_i,
664                                    bool spoofchk)
665 {
666         return spoofchk ? -EOPNOTSUPP : 0;
667 }
668
669 int efx_ef10_sriov_set_vf_link_state(struct efx_nic *efx, int vf_i,
670                                      int link_state)
671 {
672         MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
673         struct efx_ef10_nic_data *nic_data = efx->nic_data;
674
675         BUILD_BUG_ON(IFLA_VF_LINK_STATE_AUTO !=
676                      MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_AUTO);
677         BUILD_BUG_ON(IFLA_VF_LINK_STATE_ENABLE !=
678                      MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_UP);
679         BUILD_BUG_ON(IFLA_VF_LINK_STATE_DISABLE !=
680                      MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_DOWN);
681         MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
682                               LINK_STATE_MODE_IN_FUNCTION_PF,
683                               nic_data->pf_index,
684                               LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
685         MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE, link_state);
686         return efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
687                             NULL, 0, NULL); /* don't care what old mode was */
688 }
689
690 int efx_ef10_sriov_get_vf_config(struct efx_nic *efx, int vf_i,
691                                  struct ifla_vf_info *ivf)
692 {
693         MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
694         MCDI_DECLARE_BUF(outbuf, MC_CMD_LINK_STATE_MODE_OUT_LEN);
695
696         struct efx_ef10_nic_data *nic_data = efx->nic_data;
697         struct ef10_vf *vf;
698         size_t outlen;
699         int rc;
700
701         if (vf_i >= efx->vf_count)
702                 return -EINVAL;
703
704         if (!nic_data->vf)
705                 return -EOPNOTSUPP;
706
707         vf = nic_data->vf + vf_i;
708
709         ivf->vf = vf_i;
710         ivf->min_tx_rate = 0;
711         ivf->max_tx_rate = 0;
712         ether_addr_copy(ivf->mac, vf->mac);
713         ivf->vlan = (vf->vlan == EFX_EF10_NO_VLAN) ? 0 : vf->vlan;
714         ivf->qos = 0;
715
716         MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
717                               LINK_STATE_MODE_IN_FUNCTION_PF,
718                               nic_data->pf_index,
719                               LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
720         MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE,
721                        MC_CMD_LINK_STATE_MODE_IN_DO_NOT_CHANGE);
722         rc = efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
723                           outbuf, sizeof(outbuf), &outlen);
724         if (rc)
725                 return rc;
726         if (outlen < MC_CMD_LINK_STATE_MODE_OUT_LEN)
727                 return -EIO;
728         ivf->linkstate = MCDI_DWORD(outbuf, LINK_STATE_MODE_OUT_OLD_MODE);
729
730         return 0;
731 }
732
733 int efx_ef10_sriov_get_phys_port_id(struct efx_nic *efx,
734                                     struct netdev_phys_item_id *ppid)
735 {
736         struct efx_ef10_nic_data *nic_data = efx->nic_data;
737
738         if (!is_valid_ether_addr(nic_data->port_id))
739                 return -EOPNOTSUPP;
740
741         ppid->id_len = ETH_ALEN;
742         memcpy(ppid->id, nic_data->port_id, ppid->id_len);
743
744         return 0;
745 }