sfc: add ndo_set_vf_mac() function for EF10
[deliverable/linux.git] / drivers / net / ethernet / sfc / ef10_sriov.c
CommitLineData
834e23dd
SS
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
3c5eb876
SS
17static int efx_ef10_evb_port_assign(struct efx_nic *efx, unsigned int port_id,
18 unsigned int vf_fn)
834e23dd 19{
3c5eb876
SS
20 MCDI_DECLARE_BUF(inbuf, MC_CMD_EVB_PORT_ASSIGN_IN_LEN);
21 struct efx_ef10_nic_data *nic_data = efx->nic_data;
834e23dd 22
3c5eb876
SS
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);
834e23dd
SS
30}
31
3c5eb876
SS
32static int efx_ef10_vport_add_mac(struct efx_nic *efx,
33 unsigned int port_id, u8 *mac)
834e23dd 34{
3c5eb876 35 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_ADD_MAC_ADDRESS_IN_LEN);
834e23dd 36
3c5eb876
SS
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);
834e23dd 39
3c5eb876
SS
40 return efx_mcdi_rpc(efx, MC_CMD_VPORT_ADD_MAC_ADDRESS, inbuf,
41 sizeof(inbuf), NULL, 0, NULL);
834e23dd 42}
6d8aaaf6 43
3c5eb876
SS
44static int efx_ef10_vport_del_mac(struct efx_nic *efx,
45 unsigned int port_id, u8 *mac)
02246a7f 46{
3c5eb876 47 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
02246a7f 48
3c5eb876
SS
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);
02246a7f 51
3c5eb876
SS
52 return efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
53 sizeof(inbuf), NULL, 0, NULL);
02246a7f
SS
54}
55
6d8aaaf6
DP
56static 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
61 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
62 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_TYPE, vswitch_type);
63 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 0);
64 MCDI_POPULATE_DWORD_1(inbuf, VSWITCH_ALLOC_IN_FLAGS,
65 VSWITCH_ALLOC_IN_FLAG_AUTO_PORT, 0);
66
67 return efx_mcdi_rpc(efx, MC_CMD_VSWITCH_ALLOC, inbuf, sizeof(inbuf),
68 NULL, 0, NULL);
69}
70
71static int efx_ef10_vswitch_free(struct efx_nic *efx, unsigned int port_id)
72{
73 MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_FREE_IN_LEN);
74
75 MCDI_SET_DWORD(inbuf, VSWITCH_FREE_IN_UPSTREAM_PORT_ID, port_id);
76
77 return efx_mcdi_rpc(efx, MC_CMD_VSWITCH_FREE, inbuf, sizeof(inbuf),
78 NULL, 0, NULL);
79}
80
81static int efx_ef10_vport_alloc(struct efx_nic *efx,
82 unsigned int port_id_in,
83 unsigned int vport_type,
84 unsigned int *port_id_out)
85{
86 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_ALLOC_IN_LEN);
87 MCDI_DECLARE_BUF(outbuf, MC_CMD_VPORT_ALLOC_OUT_LEN);
88 size_t outlen;
89 int rc;
90
91 EFX_WARN_ON_PARANOID(!port_id_out);
92
93 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_UPSTREAM_PORT_ID, port_id_in);
94 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_TYPE, vport_type);
95 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_NUM_VLAN_TAGS, 0);
96 MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_FLAGS,
97 VPORT_ALLOC_IN_FLAG_AUTO_PORT, 0);
98
99 rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_ALLOC, inbuf, sizeof(inbuf),
100 outbuf, sizeof(outbuf), &outlen);
101 if (rc)
102 return rc;
103 if (outlen < MC_CMD_VPORT_ALLOC_OUT_LEN)
104 return -EIO;
105
106 *port_id_out = MCDI_DWORD(outbuf, VPORT_ALLOC_OUT_VPORT_ID);
107 return 0;
108}
109
110static int efx_ef10_vport_free(struct efx_nic *efx, unsigned int port_id)
111{
112 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_FREE_IN_LEN);
113
114 MCDI_SET_DWORD(inbuf, VPORT_FREE_IN_VPORT_ID, port_id);
115
116 return efx_mcdi_rpc(efx, MC_CMD_VPORT_FREE, inbuf, sizeof(inbuf),
117 NULL, 0, NULL);
118}
119
7b8c7b54
SS
120static int efx_ef10_vadaptor_alloc(struct efx_nic *efx, unsigned int port_id)
121{
122 MCDI_DECLARE_BUF(inbuf, MC_CMD_VADAPTOR_ALLOC_IN_LEN);
123
124 MCDI_SET_DWORD(inbuf, VADAPTOR_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
125 return efx_mcdi_rpc(efx, MC_CMD_VADAPTOR_ALLOC, inbuf, sizeof(inbuf),
126 NULL, 0, NULL);
127}
128
129static int efx_ef10_vadaptor_free(struct efx_nic *efx, unsigned int port_id)
130{
131 MCDI_DECLARE_BUF(inbuf, MC_CMD_VADAPTOR_FREE_IN_LEN);
132
133 MCDI_SET_DWORD(inbuf, VADAPTOR_FREE_IN_UPSTREAM_PORT_ID, port_id);
134 return efx_mcdi_rpc(efx, MC_CMD_VADAPTOR_FREE, inbuf, sizeof(inbuf),
135 NULL, 0, NULL);
136}
137
3c5eb876
SS
138static void efx_ef10_sriov_free_vf_vports(struct efx_nic *efx)
139{
140 struct efx_ef10_nic_data *nic_data = efx->nic_data;
141 int i;
142
143 if (!nic_data->vf)
144 return;
145
146 for (i = 0; i < efx->vf_count; i++) {
147 struct ef10_vf *vf = nic_data->vf + i;
148
149 if (vf->vport_assigned) {
150 efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, i);
151 vf->vport_assigned = 0;
152 }
153
154 if (!is_zero_ether_addr(vf->mac)) {
155 efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
156 eth_zero_addr(vf->mac);
157 }
158
159 if (vf->vport_id) {
160 efx_ef10_vport_free(efx, vf->vport_id);
161 vf->vport_id = 0;
162 }
f1122a34
SS
163
164 vf->efx = NULL;
3c5eb876
SS
165 }
166}
167
168static void efx_ef10_sriov_free_vf_vswitching(struct efx_nic *efx)
169{
170 struct efx_ef10_nic_data *nic_data = efx->nic_data;
171
172 efx_ef10_sriov_free_vf_vports(efx);
173 kfree(nic_data->vf);
174 nic_data->vf = NULL;
175}
176
177static int efx_ef10_sriov_assign_vf_vport(struct efx_nic *efx,
178 unsigned int vf_i)
179{
180 struct efx_ef10_nic_data *nic_data = efx->nic_data;
181 struct ef10_vf *vf = nic_data->vf + vf_i;
182 int rc;
183
184 if (WARN_ON_ONCE(!nic_data->vf))
185 return -EOPNOTSUPP;
186
187 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
188 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
189 &vf->vport_id);
190 if (rc)
191 return rc;
192
193 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
194 if (rc) {
195 eth_zero_addr(vf->mac);
196 return rc;
197 }
198
199 rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
200 if (rc)
201 return rc;
202
203 vf->vport_assigned = 1;
204 return 0;
205}
206
207static int efx_ef10_sriov_alloc_vf_vswitching(struct efx_nic *efx)
208{
209 struct efx_ef10_nic_data *nic_data = efx->nic_data;
210 unsigned int i;
211 int rc;
212
213 nic_data->vf = kcalloc(efx->vf_count, sizeof(struct ef10_vf),
214 GFP_KERNEL);
215 if (!nic_data->vf)
216 return -ENOMEM;
217
218 for (i = 0; i < efx->vf_count; i++) {
219 random_ether_addr(nic_data->vf[i].mac);
f1122a34 220 nic_data->vf[i].efx = NULL;
3c5eb876
SS
221
222 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
223 if (rc)
224 goto fail;
225 }
226
227 return 0;
228fail:
229 efx_ef10_sriov_free_vf_vports(efx);
230 kfree(nic_data->vf);
231 nic_data->vf = NULL;
232 return rc;
233}
234
235static int efx_ef10_sriov_restore_vf_vswitching(struct efx_nic *efx)
236{
237 unsigned int i;
238 int rc;
239
240 for (i = 0; i < efx->vf_count; i++) {
241 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
242 if (rc)
243 goto fail;
244 }
245
246 return 0;
247fail:
248 efx_ef10_sriov_free_vf_vswitching(efx);
249 return rc;
250}
251
6d8aaaf6
DP
252/* On top of the default firmware vswitch setup, create a VEB vswitch and
253 * expansion vport for use by this function.
254 */
7b8c7b54 255int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
6d8aaaf6
DP
256{
257 struct efx_ef10_nic_data *nic_data = efx->nic_data;
3c5eb876 258 struct net_device *net_dev = efx->net_dev;
6d8aaaf6
DP
259 int rc;
260
7b8c7b54
SS
261 if (pci_sriov_get_totalvfs(efx->pci_dev) <= 0) {
262 /* vswitch not needed as we have no VFs */
263 efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
264 return 0;
265 }
6d8aaaf6
DP
266
267 rc = efx_ef10_vswitch_alloc(efx, EVB_PORT_ID_ASSIGNED,
268 MC_CMD_VSWITCH_ALLOC_IN_VSWITCH_TYPE_VEB);
269 if (rc)
270 goto fail1;
271
272 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
273 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
274 &nic_data->vport_id);
275 if (rc)
276 goto fail2;
277
3c5eb876
SS
278 rc = efx_ef10_vport_add_mac(efx, nic_data->vport_id, net_dev->dev_addr);
279 if (rc)
280 goto fail3;
3c5eb876
SS
281 ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
282
7b8c7b54
SS
283 rc = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
284 if (rc)
285 goto fail4;
286
6d8aaaf6 287 return 0;
7b8c7b54
SS
288fail4:
289 efx_ef10_vport_del_mac(efx, nic_data->vport_id, nic_data->vport_mac);
290 eth_zero_addr(nic_data->vport_mac);
3c5eb876
SS
291fail3:
292 efx_ef10_vport_free(efx, nic_data->vport_id);
293 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
6d8aaaf6
DP
294fail2:
295 efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
296fail1:
297 return rc;
298}
299
7b8c7b54
SS
300int efx_ef10_vswitching_probe_vf(struct efx_nic *efx)
301{
302 struct efx_ef10_nic_data *nic_data = efx->nic_data;
303
304 return efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
305}
306
307int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
6d8aaaf6
DP
308{
309 struct efx_ef10_nic_data *nic_data = efx->nic_data;
310 int rc;
311
312 if (!nic_data->must_probe_vswitching)
313 return 0;
314
7b8c7b54 315 rc = efx_ef10_vswitching_probe_pf(efx);
3c5eb876
SS
316 if (rc)
317 goto fail;
318
319 rc = efx_ef10_sriov_restore_vf_vswitching(efx);
320 if (rc)
321 goto fail;
6d8aaaf6 322
3c5eb876
SS
323 nic_data->must_probe_vswitching = false;
324fail:
6d8aaaf6
DP
325 return rc;
326}
327
7b8c7b54
SS
328int efx_ef10_vswitching_restore_vf(struct efx_nic *efx)
329{
330 struct efx_ef10_nic_data *nic_data = efx->nic_data;
331 int rc;
332
333 if (!nic_data->must_probe_vswitching)
334 return 0;
335
336 rc = efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
337 if (rc)
338 return rc;
339
340 nic_data->must_probe_vswitching = false;
341 return 0;
342}
343
344void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
6d8aaaf6
DP
345{
346 struct efx_ef10_nic_data *nic_data = efx->nic_data;
347
3c5eb876
SS
348 efx_ef10_sriov_free_vf_vswitching(efx);
349
7b8c7b54
SS
350 efx_ef10_vadaptor_free(efx, nic_data->vport_id);
351
6d8aaaf6
DP
352 if (nic_data->vport_id == EVB_PORT_ID_ASSIGNED)
353 return; /* No vswitch was ever created */
354
3c5eb876
SS
355 if (!is_zero_ether_addr(nic_data->vport_mac)) {
356 efx_ef10_vport_del_mac(efx, nic_data->vport_id,
357 efx->net_dev->dev_addr);
358 eth_zero_addr(nic_data->vport_mac);
359 }
6d8aaaf6
DP
360 efx_ef10_vport_free(efx, nic_data->vport_id);
361 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
362
363 efx_ef10_vswitch_free(efx, nic_data->vport_id);
364}
3c5eb876 365
7b8c7b54
SS
366void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
367{
368 efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
369}
370
3c5eb876
SS
371static int efx_ef10_pci_sriov_enable(struct efx_nic *efx, int num_vfs)
372{
373 int rc = 0;
374 struct pci_dev *dev = efx->pci_dev;
375
376 efx->vf_count = num_vfs;
377
378 rc = efx_ef10_sriov_alloc_vf_vswitching(efx);
379 if (rc)
380 goto fail1;
381
382 rc = pci_enable_sriov(dev, num_vfs);
383 if (rc)
384 goto fail2;
385
386 return 0;
387fail2:
388 efx_ef10_sriov_free_vf_vswitching(efx);
389fail1:
390 efx->vf_count = 0;
391 netif_err(efx, probe, efx->net_dev,
392 "Failed to enable SRIOV VFs\n");
393 return rc;
394}
395
396static int efx_ef10_pci_sriov_disable(struct efx_nic *efx)
397{
398 struct pci_dev *dev = efx->pci_dev;
399
400 pci_disable_sriov(dev);
401 efx_ef10_sriov_free_vf_vswitching(efx);
402 efx->vf_count = 0;
403 return 0;
404}
405
406int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
407{
408 if (num_vfs == 0)
409 return efx_ef10_pci_sriov_disable(efx);
410 else
411 return efx_ef10_pci_sriov_enable(efx, num_vfs);
412}
413
414int efx_ef10_sriov_init(struct efx_nic *efx)
415{
416 return 0;
417}
418
419void efx_ef10_sriov_fini(struct efx_nic *efx)
420{
421 struct efx_ef10_nic_data *nic_data = efx->nic_data;
422 int rc;
423
424 if (!nic_data->vf)
425 return;
426
427 rc = efx_ef10_pci_sriov_disable(efx);
428 if (rc)
429 netif_dbg(efx, drv, efx->net_dev,
430 "Disabling SRIOV was not successful rc=%d\n", rc);
431 else
432 netif_dbg(efx, drv, efx->net_dev, "SRIOV disabled\n");
433}
e340be92
SS
434
435static int efx_ef10_vport_del_vf_mac(struct efx_nic *efx, unsigned int port_id,
436 u8 *mac)
437{
438 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
439 MCDI_DECLARE_BUF_ERR(outbuf);
440 size_t outlen;
441 int rc;
442
443 MCDI_SET_DWORD(inbuf, VPORT_DEL_MAC_ADDRESS_IN_VPORT_ID, port_id);
444 ether_addr_copy(MCDI_PTR(inbuf, VPORT_DEL_MAC_ADDRESS_IN_MACADDR), mac);
445
446 rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
447 sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
448
449 return rc;
450}
451
452int efx_ef10_sriov_set_vf_mac(struct efx_nic *efx, int vf_i, u8 *mac)
453{
454 struct efx_ef10_nic_data *nic_data = efx->nic_data;
455 struct ef10_vf *vf;
456 int rc;
457
458 if (!nic_data->vf)
459 return -EOPNOTSUPP;
460
461 if (vf_i >= efx->vf_count)
462 return -EINVAL;
463 vf = nic_data->vf + vf_i;
464
465 if (vf->efx) {
466 efx_device_detach_sync(vf->efx);
467 efx_net_stop(vf->efx->net_dev);
468
469 down_write(&vf->efx->filter_sem);
470 vf->efx->type->filter_table_remove(vf->efx);
471
472 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
473 if (rc) {
474 up_write(&vf->efx->filter_sem);
475 return rc;
476 }
477 }
478
479 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
480 if (rc)
481 return rc;
482
483 if (!is_zero_ether_addr(vf->mac)) {
484 rc = efx_ef10_vport_del_vf_mac(efx, vf->vport_id, vf->mac);
485 if (rc)
486 return rc;
487 }
488
489 if (!is_zero_ether_addr(mac)) {
490 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, mac);
491 if (rc) {
492 eth_zero_addr(vf->mac);
493 goto fail;
494 }
495 if (vf->efx)
496 ether_addr_copy(vf->efx->net_dev->dev_addr, mac);
497 }
498
499 ether_addr_copy(vf->mac, mac);
500
501 rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
502 if (rc)
503 goto fail;
504
505 if (vf->efx) {
506 /* VF cannot use the vport_id that the PF created */
507 rc = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
508 if (rc) {
509 up_write(&vf->efx->filter_sem);
510 return rc;
511 }
512 vf->efx->type->filter_table_probe(vf->efx);
513 up_write(&vf->efx->filter_sem);
514 efx_net_open(vf->efx->net_dev);
515 netif_device_attach(vf->efx->net_dev);
516 }
517
518 return 0;
519
520fail:
521 memset(vf->mac, 0, ETH_ALEN);
522 return rc;
523}
This page took 0.051221 seconds and 5 git commands to generate.