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