Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[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 &&
126                     vf->pci_dev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
127                         continue;
128
129                 if (vf->vport_assigned) {
130                         efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, i);
131                         vf->vport_assigned = 0;
132                 }
133
134                 if (!is_zero_ether_addr(vf->mac)) {
135                         efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
136                         eth_zero_addr(vf->mac);
137                 }
138
139                 if (vf->vport_id) {
140                         efx_ef10_vport_free(efx, vf->vport_id);
141                         vf->vport_id = 0;
142                 }
143
144                 vf->efx = NULL;
145         }
146 }
147
148 static void efx_ef10_sriov_free_vf_vswitching(struct efx_nic *efx)
149 {
150         struct efx_ef10_nic_data *nic_data = efx->nic_data;
151
152         efx_ef10_sriov_free_vf_vports(efx);
153         kfree(nic_data->vf);
154         nic_data->vf = NULL;
155 }
156
157 static int efx_ef10_sriov_assign_vf_vport(struct efx_nic *efx,
158                                           unsigned int vf_i)
159 {
160         struct efx_ef10_nic_data *nic_data = efx->nic_data;
161         struct ef10_vf *vf = nic_data->vf + vf_i;
162         int rc;
163
164         if (WARN_ON_ONCE(!nic_data->vf))
165                 return -EOPNOTSUPP;
166
167         rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
168                                   MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
169                                   vf->vlan, &vf->vport_id);
170         if (rc)
171                 return rc;
172
173         rc = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
174         if (rc) {
175                 eth_zero_addr(vf->mac);
176                 return rc;
177         }
178
179         rc =  efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
180         if (rc)
181                 return rc;
182
183         vf->vport_assigned = 1;
184         return 0;
185 }
186
187 static int efx_ef10_sriov_alloc_vf_vswitching(struct efx_nic *efx)
188 {
189         struct efx_ef10_nic_data *nic_data = efx->nic_data;
190         unsigned int i;
191         int rc;
192
193         nic_data->vf = kcalloc(efx->vf_count, sizeof(struct ef10_vf),
194                                GFP_KERNEL);
195         if (!nic_data->vf)
196                 return -ENOMEM;
197
198         for (i = 0; i < efx->vf_count; i++) {
199                 eth_random_addr(nic_data->vf[i].mac);
200                 nic_data->vf[i].efx = NULL;
201                 nic_data->vf[i].vlan = EFX_EF10_NO_VLAN;
202
203                 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
204                 if (rc)
205                         goto fail;
206         }
207
208         return 0;
209 fail:
210         efx_ef10_sriov_free_vf_vports(efx);
211         kfree(nic_data->vf);
212         nic_data->vf = NULL;
213         return rc;
214 }
215
216 static int efx_ef10_sriov_restore_vf_vswitching(struct efx_nic *efx)
217 {
218         unsigned int i;
219         int rc;
220
221         for (i = 0; i < efx->vf_count; i++) {
222                 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
223                 if (rc)
224                         goto fail;
225         }
226
227         return 0;
228 fail:
229         efx_ef10_sriov_free_vf_vswitching(efx);
230         return rc;
231 }
232
233 static int efx_ef10_vadaptor_alloc_set_features(struct efx_nic *efx)
234 {
235         struct efx_ef10_nic_data *nic_data = efx->nic_data;
236         u32 port_flags;
237         int rc;
238
239         rc = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
240         if (rc)
241                 goto fail_vadaptor_alloc;
242
243         rc = efx_ef10_vadaptor_query(efx, nic_data->vport_id,
244                                      &port_flags, NULL, NULL);
245         if (rc)
246                 goto fail_vadaptor_query;
247
248         if (port_flags &
249             (1 << MC_CMD_VPORT_ALLOC_IN_FLAG_VLAN_RESTRICT_LBN))
250                 efx->fixed_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
251         else
252                 efx->fixed_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
253
254         return 0;
255
256 fail_vadaptor_query:
257         efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
258 fail_vadaptor_alloc:
259         return rc;
260 }
261
262 /* On top of the default firmware vswitch setup, create a VEB vswitch and
263  * expansion vport for use by this function.
264  */
265 int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
266 {
267         struct efx_ef10_nic_data *nic_data = efx->nic_data;
268         struct net_device *net_dev = efx->net_dev;
269         int rc;
270
271         if (pci_sriov_get_totalvfs(efx->pci_dev) <= 0) {
272                 /* vswitch not needed as we have no VFs */
273                 efx_ef10_vadaptor_alloc_set_features(efx);
274                 return 0;
275         }
276
277         rc = efx_ef10_vswitch_alloc(efx, EVB_PORT_ID_ASSIGNED,
278                                     MC_CMD_VSWITCH_ALLOC_IN_VSWITCH_TYPE_VEB);
279         if (rc)
280                 goto fail1;
281
282         rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
283                                   MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
284                                   EFX_EF10_NO_VLAN, &nic_data->vport_id);
285         if (rc)
286                 goto fail2;
287
288         rc = efx_ef10_vport_add_mac(efx, nic_data->vport_id, net_dev->dev_addr);
289         if (rc)
290                 goto fail3;
291         ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
292
293         rc = efx_ef10_vadaptor_alloc_set_features(efx);
294         if (rc)
295                 goto fail4;
296
297         return 0;
298 fail4:
299         efx_ef10_vport_del_mac(efx, nic_data->vport_id, nic_data->vport_mac);
300         eth_zero_addr(nic_data->vport_mac);
301 fail3:
302         efx_ef10_vport_free(efx, nic_data->vport_id);
303         nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
304 fail2:
305         efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
306 fail1:
307         return rc;
308 }
309
310 int efx_ef10_vswitching_probe_vf(struct efx_nic *efx)
311 {
312         return efx_ef10_vadaptor_alloc_set_features(efx);
313 }
314
315 int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
316 {
317         struct efx_ef10_nic_data *nic_data = efx->nic_data;
318         int rc;
319
320         if (!nic_data->must_probe_vswitching)
321                 return 0;
322
323         rc = efx_ef10_vswitching_probe_pf(efx);
324         if (rc)
325                 goto fail;
326
327         rc = efx_ef10_sriov_restore_vf_vswitching(efx);
328         if (rc)
329                 goto fail;
330
331         nic_data->must_probe_vswitching = false;
332 fail:
333         return rc;
334 }
335
336 int efx_ef10_vswitching_restore_vf(struct efx_nic *efx)
337 {
338         struct efx_ef10_nic_data *nic_data = efx->nic_data;
339         int rc;
340
341         if (!nic_data->must_probe_vswitching)
342                 return 0;
343
344         rc = efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
345         if (rc)
346                 return rc;
347
348         nic_data->must_probe_vswitching = false;
349         return 0;
350 }
351
352 void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
353 {
354         struct efx_ef10_nic_data *nic_data = efx->nic_data;
355
356         efx_ef10_sriov_free_vf_vswitching(efx);
357
358         efx_ef10_vadaptor_free(efx, nic_data->vport_id);
359
360         if (nic_data->vport_id == EVB_PORT_ID_ASSIGNED)
361                 return; /* No vswitch was ever created */
362
363         if (!is_zero_ether_addr(nic_data->vport_mac)) {
364                 efx_ef10_vport_del_mac(efx, nic_data->vport_id,
365                                        efx->net_dev->dev_addr);
366                 eth_zero_addr(nic_data->vport_mac);
367         }
368         efx_ef10_vport_free(efx, nic_data->vport_id);
369         nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
370
371         /* Only free the vswitch if no VFs are assigned */
372         if (!pci_vfs_assigned(efx->pci_dev))
373                 efx_ef10_vswitch_free(efx, nic_data->vport_id);
374 }
375
376 void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
377 {
378         efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
379 }
380
381 static int efx_ef10_pci_sriov_enable(struct efx_nic *efx, int num_vfs)
382 {
383         int rc = 0;
384         struct pci_dev *dev = efx->pci_dev;
385
386         efx->vf_count = num_vfs;
387
388         rc = efx_ef10_sriov_alloc_vf_vswitching(efx);
389         if (rc)
390                 goto fail1;
391
392         rc = pci_enable_sriov(dev, num_vfs);
393         if (rc)
394                 goto fail2;
395
396         return 0;
397 fail2:
398         efx_ef10_sriov_free_vf_vswitching(efx);
399 fail1:
400         efx->vf_count = 0;
401         netif_err(efx, probe, efx->net_dev,
402                   "Failed to enable SRIOV VFs\n");
403         return rc;
404 }
405
406 static int efx_ef10_pci_sriov_disable(struct efx_nic *efx, bool force)
407 {
408         struct pci_dev *dev = efx->pci_dev;
409         unsigned int vfs_assigned = 0;
410
411         vfs_assigned = pci_vfs_assigned(dev);
412
413         if (vfs_assigned && !force) {
414                 netif_info(efx, drv, efx->net_dev, "VFs are assigned to guests; "
415                            "please detach them before disabling SR-IOV\n");
416                 return -EBUSY;
417         }
418
419         if (!vfs_assigned)
420                 pci_disable_sriov(dev);
421
422         efx_ef10_sriov_free_vf_vswitching(efx);
423         efx->vf_count = 0;
424         return 0;
425 }
426
427 int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
428 {
429         if (num_vfs == 0)
430                 return efx_ef10_pci_sriov_disable(efx, false);
431         else
432                 return efx_ef10_pci_sriov_enable(efx, num_vfs);
433 }
434
435 int efx_ef10_sriov_init(struct efx_nic *efx)
436 {
437         return 0;
438 }
439
440 void efx_ef10_sriov_fini(struct efx_nic *efx)
441 {
442         struct efx_ef10_nic_data *nic_data = efx->nic_data;
443         unsigned int i;
444         int rc;
445
446         if (!nic_data->vf) {
447                 /* Remove any un-assigned orphaned VFs */
448                 if (pci_num_vf(efx->pci_dev) && !pci_vfs_assigned(efx->pci_dev))
449                         pci_disable_sriov(efx->pci_dev);
450                 return;
451         }
452
453         /* Remove any VFs in the host */
454         for (i = 0; i < efx->vf_count; ++i) {
455                 struct efx_nic *vf_efx = nic_data->vf[i].efx;
456
457                 if (vf_efx)
458                         vf_efx->pci_dev->driver->remove(vf_efx->pci_dev);
459         }
460
461         rc = efx_ef10_pci_sriov_disable(efx, true);
462         if (rc)
463                 netif_dbg(efx, drv, efx->net_dev,
464                           "Disabling SRIOV was not successful rc=%d\n", rc);
465         else
466                 netif_dbg(efx, drv, efx->net_dev, "SRIOV disabled\n");
467 }
468
469 static int efx_ef10_vport_del_vf_mac(struct efx_nic *efx, unsigned int port_id,
470                                      u8 *mac)
471 {
472         MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
473         MCDI_DECLARE_BUF_ERR(outbuf);
474         size_t outlen;
475         int rc;
476
477         MCDI_SET_DWORD(inbuf, VPORT_DEL_MAC_ADDRESS_IN_VPORT_ID, port_id);
478         ether_addr_copy(MCDI_PTR(inbuf, VPORT_DEL_MAC_ADDRESS_IN_MACADDR), mac);
479
480         rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
481                           sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
482
483         return rc;
484 }
485
486 int efx_ef10_sriov_set_vf_mac(struct efx_nic *efx, int vf_i, u8 *mac)
487 {
488         struct efx_ef10_nic_data *nic_data = efx->nic_data;
489         struct ef10_vf *vf;
490         int rc;
491
492         if (!nic_data->vf)
493                 return -EOPNOTSUPP;
494
495         if (vf_i >= efx->vf_count)
496                 return -EINVAL;
497         vf = nic_data->vf + vf_i;
498
499         if (vf->efx) {
500                 efx_device_detach_sync(vf->efx);
501                 efx_net_stop(vf->efx->net_dev);
502
503                 down_write(&vf->efx->filter_sem);
504                 vf->efx->type->filter_table_remove(vf->efx);
505
506                 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
507                 if (rc) {
508                         up_write(&vf->efx->filter_sem);
509                         return rc;
510                 }
511         }
512
513         rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
514         if (rc)
515                 return rc;
516
517         if (!is_zero_ether_addr(vf->mac)) {
518                 rc = efx_ef10_vport_del_vf_mac(efx, vf->vport_id, vf->mac);
519                 if (rc)
520                         return rc;
521         }
522
523         if (!is_zero_ether_addr(mac)) {
524                 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, mac);
525                 if (rc) {
526                         eth_zero_addr(vf->mac);
527                         goto fail;
528                 }
529                 if (vf->efx)
530                         ether_addr_copy(vf->efx->net_dev->dev_addr, mac);
531         }
532
533         ether_addr_copy(vf->mac, mac);
534
535         rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
536         if (rc)
537                 goto fail;
538
539         if (vf->efx) {
540                 /* VF cannot use the vport_id that the PF created */
541                 rc = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
542                 if (rc) {
543                         up_write(&vf->efx->filter_sem);
544                         return rc;
545                 }
546                 vf->efx->type->filter_table_probe(vf->efx);
547                 up_write(&vf->efx->filter_sem);
548                 efx_net_open(vf->efx->net_dev);
549                 efx_device_attach_if_not_resetting(vf->efx);
550         }
551
552         return 0;
553
554 fail:
555         eth_zero_addr(vf->mac);
556         return rc;
557 }
558
559 int efx_ef10_sriov_set_vf_vlan(struct efx_nic *efx, int vf_i, u16 vlan,
560                                u8 qos)
561 {
562         struct efx_ef10_nic_data *nic_data = efx->nic_data;
563         struct ef10_vf *vf;
564         u16 new_vlan;
565         int rc = 0, rc2 = 0;
566
567         if (vf_i >= efx->vf_count)
568                 return -EINVAL;
569         if (qos != 0)
570                 return -EINVAL;
571
572         vf = nic_data->vf + vf_i;
573
574         new_vlan = (vlan == 0) ? EFX_EF10_NO_VLAN : vlan;
575         if (new_vlan == vf->vlan)
576                 return 0;
577
578         if (vf->efx) {
579                 efx_device_detach_sync(vf->efx);
580                 efx_net_stop(vf->efx->net_dev);
581
582                 mutex_lock(&vf->efx->mac_lock);
583                 down_write(&vf->efx->filter_sem);
584                 vf->efx->type->filter_table_remove(vf->efx);
585
586                 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
587                 if (rc)
588                         goto restore_filters;
589         }
590
591         if (vf->vport_assigned) {
592                 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
593                 if (rc) {
594                         netif_warn(efx, drv, efx->net_dev,
595                                    "Failed to change vlan on VF %d.\n", vf_i);
596                         netif_warn(efx, drv, efx->net_dev,
597                                    "This is likely because the VF is bound to a driver in a VM.\n");
598                         netif_warn(efx, drv, efx->net_dev,
599                                    "Please unload the driver in the VM.\n");
600                         goto restore_vadaptor;
601                 }
602                 vf->vport_assigned = 0;
603         }
604
605         if (!is_zero_ether_addr(vf->mac)) {
606                 rc = efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
607                 if (rc)
608                         goto restore_evb_port;
609         }
610
611         if (vf->vport_id) {
612                 rc = efx_ef10_vport_free(efx, vf->vport_id);
613                 if (rc)
614                         goto restore_mac;
615                 vf->vport_id = 0;
616         }
617
618         /* Do the actual vlan change */
619         vf->vlan = new_vlan;
620
621         /* Restore everything in reverse order */
622         rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
623                                   MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
624                                   vf->vlan, &vf->vport_id);
625         if (rc)
626                 goto reset_nic_up_write;
627
628 restore_mac:
629         if (!is_zero_ether_addr(vf->mac)) {
630                 rc2 = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
631                 if (rc2) {
632                         eth_zero_addr(vf->mac);
633                         goto reset_nic_up_write;
634                 }
635         }
636
637 restore_evb_port:
638         rc2 = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
639         if (rc2)
640                 goto reset_nic_up_write;
641         else
642                 vf->vport_assigned = 1;
643
644 restore_vadaptor:
645         if (vf->efx) {
646                 rc2 = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
647                 if (rc2)
648                         goto reset_nic_up_write;
649         }
650
651 restore_filters:
652         if (vf->efx) {
653                 rc2 = vf->efx->type->filter_table_probe(vf->efx);
654                 if (rc2)
655                         goto reset_nic_up_write;
656
657                 up_write(&vf->efx->filter_sem);
658                 mutex_unlock(&vf->efx->mac_lock);
659
660                 rc2 = efx_net_open(vf->efx->net_dev);
661                 if (rc2)
662                         goto reset_nic;
663
664                 efx_device_attach_if_not_resetting(vf->efx);
665         }
666         return rc;
667
668 reset_nic_up_write:
669         if (vf->efx) {
670                 up_write(&vf->efx->filter_sem);
671                 mutex_unlock(&vf->efx->mac_lock);
672         }
673 reset_nic:
674         if (vf->efx) {
675                 netif_err(efx, drv, efx->net_dev,
676                           "Failed to restore VF - scheduling reset.\n");
677                 efx_schedule_reset(vf->efx, RESET_TYPE_DATAPATH);
678         } else {
679                 netif_err(efx, drv, efx->net_dev,
680                           "Failed to restore the VF and cannot reset the VF "
681                           "- VF is not functional.\n");
682                 netif_err(efx, drv, efx->net_dev,
683                           "Please reload the driver attached to the VF.\n");
684         }
685
686         return rc ? rc : rc2;
687 }
688
689 int efx_ef10_sriov_set_vf_spoofchk(struct efx_nic *efx, int vf_i,
690                                    bool spoofchk)
691 {
692         return spoofchk ? -EOPNOTSUPP : 0;
693 }
694
695 int efx_ef10_sriov_set_vf_link_state(struct efx_nic *efx, int vf_i,
696                                      int link_state)
697 {
698         MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
699         struct efx_ef10_nic_data *nic_data = efx->nic_data;
700
701         BUILD_BUG_ON(IFLA_VF_LINK_STATE_AUTO !=
702                      MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_AUTO);
703         BUILD_BUG_ON(IFLA_VF_LINK_STATE_ENABLE !=
704                      MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_UP);
705         BUILD_BUG_ON(IFLA_VF_LINK_STATE_DISABLE !=
706                      MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_DOWN);
707         MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
708                               LINK_STATE_MODE_IN_FUNCTION_PF,
709                               nic_data->pf_index,
710                               LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
711         MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE, link_state);
712         return efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
713                             NULL, 0, NULL); /* don't care what old mode was */
714 }
715
716 int efx_ef10_sriov_get_vf_config(struct efx_nic *efx, int vf_i,
717                                  struct ifla_vf_info *ivf)
718 {
719         MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
720         MCDI_DECLARE_BUF(outbuf, MC_CMD_LINK_STATE_MODE_OUT_LEN);
721
722         struct efx_ef10_nic_data *nic_data = efx->nic_data;
723         struct ef10_vf *vf;
724         size_t outlen;
725         int rc;
726
727         if (vf_i >= efx->vf_count)
728                 return -EINVAL;
729
730         if (!nic_data->vf)
731                 return -EOPNOTSUPP;
732
733         vf = nic_data->vf + vf_i;
734
735         ivf->vf = vf_i;
736         ivf->min_tx_rate = 0;
737         ivf->max_tx_rate = 0;
738         ether_addr_copy(ivf->mac, vf->mac);
739         ivf->vlan = (vf->vlan == EFX_EF10_NO_VLAN) ? 0 : vf->vlan;
740         ivf->qos = 0;
741
742         MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
743                               LINK_STATE_MODE_IN_FUNCTION_PF,
744                               nic_data->pf_index,
745                               LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
746         MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE,
747                        MC_CMD_LINK_STATE_MODE_IN_DO_NOT_CHANGE);
748         rc = efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
749                           outbuf, sizeof(outbuf), &outlen);
750         if (rc)
751                 return rc;
752         if (outlen < MC_CMD_LINK_STATE_MODE_OUT_LEN)
753                 return -EIO;
754         ivf->linkstate = MCDI_DWORD(outbuf, LINK_STATE_MODE_OUT_OLD_MODE);
755
756         return 0;
757 }