blob: a76610ae9fb497f396c0e9b95b3a991561b69636 [file] [log] [blame]
Shradha Shah834e23d2015-05-06 00:55:58 +01001/****************************************************************************
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
Shradha Shah3c5eb872015-05-06 00:58:31 +010017static int efx_ef10_evb_port_assign(struct efx_nic *efx, unsigned int port_id,
18 unsigned int vf_fn)
Shradha Shah834e23d2015-05-06 00:55:58 +010019{
Shradha Shah3c5eb872015-05-06 00:58:31 +010020 MCDI_DECLARE_BUF(inbuf, MC_CMD_EVB_PORT_ASSIGN_IN_LEN);
21 struct efx_ef10_nic_data *nic_data = efx->nic_data;
Shradha Shah834e23d2015-05-06 00:55:58 +010022
Shradha Shah3c5eb872015-05-06 00:58:31 +010023 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);
Shradha Shah834e23d2015-05-06 00:55:58 +010030}
31
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +010032static 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);
Shradha Shah2d432f22015-05-20 11:11:54 +010036 int rc;
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +010037
38 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
39 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_TYPE, vswitch_type);
Shradha Shah2d432f22015-05-20 11:11:54 +010040 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 2);
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +010041 MCDI_POPULATE_DWORD_1(inbuf, VSWITCH_ALLOC_IN_FLAGS,
42 VSWITCH_ALLOC_IN_FLAG_AUTO_PORT, 0);
43
Shradha Shah2d432f22015-05-20 11:11:54 +010044 /* 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;
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +010059}
60
61static 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
71static int efx_ef10_vport_alloc(struct efx_nic *efx,
72 unsigned int port_id_in,
73 unsigned int vport_type,
Shradha Shah2d432f22015-05-20 11:11:54 +010074 u16 vlan,
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +010075 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);
Shradha Shah2d432f22015-05-20 11:11:54 +010086 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_NUM_VLAN_TAGS,
87 (vlan != EFX_EF10_NO_VLAN));
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +010088 MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_FLAGS,
89 VPORT_ALLOC_IN_FLAG_AUTO_PORT, 0);
Shradha Shah2d432f22015-05-20 11:11:54 +010090 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);
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +010093
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
105static 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
Shradha Shah3c5eb872015-05-06 00:58:31 +0100115static 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
Daniel Pieczko6598dad2015-06-02 11:41:00 +0100126 /* 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
Shradha Shah3c5eb872015-05-06 00:58:31 +0100131 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 }
Shradha Shahf1122a32015-05-20 11:09:46 +0100145
146 vf->efx = NULL;
Shradha Shah3c5eb872015-05-06 00:58:31 +0100147 }
148}
149
150static 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
159static 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,
Shradha Shah2d432f22015-05-20 11:11:54 +0100171 vf->vlan, &vf->vport_id);
Shradha Shah3c5eb872015-05-06 00:58:31 +0100172 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
189static 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);
Shradha Shahf1122a32015-05-20 11:09:46 +0100202 nic_data->vf[i].efx = NULL;
Shradha Shah2d432f22015-05-20 11:11:54 +0100203 nic_data->vf[i].vlan = EFX_EF10_NO_VLAN;
Shradha Shah3c5eb872015-05-06 00:58:31 +0100204
205 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
206 if (rc)
207 goto fail;
208 }
209
210 return 0;
211fail:
212 efx_ef10_sriov_free_vf_vports(efx);
213 kfree(nic_data->vf);
214 nic_data->vf = NULL;
215 return rc;
216}
217
218static 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;
230fail:
231 efx_ef10_sriov_free_vf_vswitching(efx);
232 return rc;
233}
234
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100235/* On top of the default firmware vswitch setup, create a VEB vswitch and
236 * expansion vport for use by this function.
237 */
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100238int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100239{
240 struct efx_ef10_nic_data *nic_data = efx->nic_data;
Shradha Shah3c5eb872015-05-06 00:58:31 +0100241 struct net_device *net_dev = efx->net_dev;
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100242 int rc;
243
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100244 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 }
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100249
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,
Shradha Shah2d432f22015-05-20 11:11:54 +0100257 EFX_EF10_NO_VLAN, &nic_data->vport_id);
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100258 if (rc)
259 goto fail2;
260
Shradha Shah3c5eb872015-05-06 00:58:31 +0100261 rc = efx_ef10_vport_add_mac(efx, nic_data->vport_id, net_dev->dev_addr);
262 if (rc)
263 goto fail3;
Shradha Shah3c5eb872015-05-06 00:58:31 +0100264 ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
265
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100266 rc = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
267 if (rc)
268 goto fail4;
269
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100270 return 0;
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100271fail4:
272 efx_ef10_vport_del_mac(efx, nic_data->vport_id, nic_data->vport_mac);
273 eth_zero_addr(nic_data->vport_mac);
Shradha Shah3c5eb872015-05-06 00:58:31 +0100274fail3:
275 efx_ef10_vport_free(efx, nic_data->vport_id);
276 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100277fail2:
278 efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
279fail1:
280 return rc;
281}
282
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100283int 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
290int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100291{
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
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100298 rc = efx_ef10_vswitching_probe_pf(efx);
Shradha Shah3c5eb872015-05-06 00:58:31 +0100299 if (rc)
300 goto fail;
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100301
Shradha Shah3c5eb872015-05-06 00:58:31 +0100302 rc = efx_ef10_sriov_restore_vf_vswitching(efx);
303 if (rc)
304 goto fail;
305
306 nic_data->must_probe_vswitching = false;
307fail:
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100308 return rc;
309}
310
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100311int 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
327void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100328{
329 struct efx_ef10_nic_data *nic_data = efx->nic_data;
330
Shradha Shah3c5eb872015-05-06 00:58:31 +0100331 efx_ef10_sriov_free_vf_vswitching(efx);
332
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100333 efx_ef10_vadaptor_free(efx, nic_data->vport_id);
334
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100335 if (nic_data->vport_id == EVB_PORT_ID_ASSIGNED)
336 return; /* No vswitch was ever created */
337
Shradha Shah3c5eb872015-05-06 00:58:31 +0100338 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 }
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100343 efx_ef10_vport_free(efx, nic_data->vport_id);
344 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
345
Daniel Pieczko6598dad2015-06-02 11:41:00 +0100346 /* 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);
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100349}
Shradha Shah3c5eb872015-05-06 00:58:31 +0100350
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100351void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
352{
353 efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
354}
355
Shradha Shah3c5eb872015-05-06 00:58:31 +0100356static 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;
372fail2:
373 efx_ef10_sriov_free_vf_vswitching(efx);
374fail1:
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
Daniel Pieczko6598dad2015-06-02 11:41:00 +0100381static int efx_ef10_pci_sriov_disable(struct efx_nic *efx, bool force)
Shradha Shah3c5eb872015-05-06 00:58:31 +0100382{
383 struct pci_dev *dev = efx->pci_dev;
Daniel Pieczko6598dad2015-06-02 11:41:00 +0100384 unsigned int vfs_assigned = 0;
Shradha Shah3c5eb872015-05-06 00:58:31 +0100385
Daniel Pieczko6598dad2015-06-02 11:41:00 +0100386 vfs_assigned = pci_vfs_assigned(dev);
Daniel Pieczko71158bf2015-06-02 11:40:31 +0100387
Daniel Pieczko6598dad2015-06-02 11:41:00 +0100388 if (vfs_assigned && !force) {
389 netif_info(efx, drv, efx->net_dev, "VFs are assigned to guests; "
390 "please detach them before disabling SR-IOV\n");
Daniel Pieczko71158bf2015-06-02 11:40:31 +0100391 return -EBUSY;
392 }
393
Daniel Pieczko6598dad2015-06-02 11:41:00 +0100394 if (!vfs_assigned)
395 pci_disable_sriov(dev);
396
Shradha Shah3c5eb872015-05-06 00:58:31 +0100397 efx_ef10_sriov_free_vf_vswitching(efx);
398 efx->vf_count = 0;
399 return 0;
400}
401
402int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
403{
404 if (num_vfs == 0)
Daniel Pieczko6598dad2015-06-02 11:41:00 +0100405 return efx_ef10_pci_sriov_disable(efx, false);
Shradha Shah3c5eb872015-05-06 00:58:31 +0100406 else
407 return efx_ef10_pci_sriov_enable(efx, num_vfs);
408}
409
410int efx_ef10_sriov_init(struct efx_nic *efx)
411{
412 return 0;
413}
414
415void efx_ef10_sriov_fini(struct efx_nic *efx)
416{
417 struct efx_ef10_nic_data *nic_data = efx->nic_data;
Daniel Pieczko2a3fc312015-06-02 11:40:46 +0100418 unsigned int i;
Shradha Shah3c5eb872015-05-06 00:58:31 +0100419 int rc;
420
Daniel Pieczko6598dad2015-06-02 11:41:00 +0100421 if (!nic_data->vf) {
422 /* Remove any un-assigned orphaned VFs */
423 if (pci_num_vf(efx->pci_dev) && !pci_vfs_assigned(efx->pci_dev))
424 pci_disable_sriov(efx->pci_dev);
Shradha Shah3c5eb872015-05-06 00:58:31 +0100425 return;
Daniel Pieczko6598dad2015-06-02 11:41:00 +0100426 }
Shradha Shah3c5eb872015-05-06 00:58:31 +0100427
Daniel Pieczko2a3fc312015-06-02 11:40:46 +0100428 /* Remove any VFs in the host */
429 for (i = 0; i < efx->vf_count; ++i) {
430 struct efx_nic *vf_efx = nic_data->vf[i].efx;
431
432 if (vf_efx)
433 vf_efx->pci_dev->driver->remove(vf_efx->pci_dev);
434 }
435
Daniel Pieczko6598dad2015-06-02 11:41:00 +0100436 rc = efx_ef10_pci_sriov_disable(efx, true);
Shradha Shah3c5eb872015-05-06 00:58:31 +0100437 if (rc)
438 netif_dbg(efx, drv, efx->net_dev,
439 "Disabling SRIOV was not successful rc=%d\n", rc);
440 else
441 netif_dbg(efx, drv, efx->net_dev, "SRIOV disabled\n");
442}
Shradha Shahe340be92015-05-20 11:11:03 +0100443
444static int efx_ef10_vport_del_vf_mac(struct efx_nic *efx, unsigned int port_id,
445 u8 *mac)
446{
447 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
448 MCDI_DECLARE_BUF_ERR(outbuf);
449 size_t outlen;
450 int rc;
451
452 MCDI_SET_DWORD(inbuf, VPORT_DEL_MAC_ADDRESS_IN_VPORT_ID, port_id);
453 ether_addr_copy(MCDI_PTR(inbuf, VPORT_DEL_MAC_ADDRESS_IN_MACADDR), mac);
454
455 rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
456 sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
457
458 return rc;
459}
460
461int efx_ef10_sriov_set_vf_mac(struct efx_nic *efx, int vf_i, u8 *mac)
462{
463 struct efx_ef10_nic_data *nic_data = efx->nic_data;
464 struct ef10_vf *vf;
465 int rc;
466
467 if (!nic_data->vf)
468 return -EOPNOTSUPP;
469
470 if (vf_i >= efx->vf_count)
471 return -EINVAL;
472 vf = nic_data->vf + vf_i;
473
474 if (vf->efx) {
475 efx_device_detach_sync(vf->efx);
476 efx_net_stop(vf->efx->net_dev);
477
478 down_write(&vf->efx->filter_sem);
479 vf->efx->type->filter_table_remove(vf->efx);
480
481 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
482 if (rc) {
483 up_write(&vf->efx->filter_sem);
484 return rc;
485 }
486 }
487
488 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
489 if (rc)
490 return rc;
491
492 if (!is_zero_ether_addr(vf->mac)) {
493 rc = efx_ef10_vport_del_vf_mac(efx, vf->vport_id, vf->mac);
494 if (rc)
495 return rc;
496 }
497
498 if (!is_zero_ether_addr(mac)) {
499 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, mac);
500 if (rc) {
501 eth_zero_addr(vf->mac);
502 goto fail;
503 }
504 if (vf->efx)
505 ether_addr_copy(vf->efx->net_dev->dev_addr, mac);
506 }
507
508 ether_addr_copy(vf->mac, mac);
509
510 rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
511 if (rc)
512 goto fail;
513
514 if (vf->efx) {
515 /* VF cannot use the vport_id that the PF created */
516 rc = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
517 if (rc) {
518 up_write(&vf->efx->filter_sem);
519 return rc;
520 }
521 vf->efx->type->filter_table_probe(vf->efx);
522 up_write(&vf->efx->filter_sem);
523 efx_net_open(vf->efx->net_dev);
524 netif_device_attach(vf->efx->net_dev);
525 }
526
527 return 0;
528
529fail:
530 memset(vf->mac, 0, ETH_ALEN);
531 return rc;
532}
Shradha Shahb9af9042015-05-20 11:11:18 +0100533
Shradha Shah2d432f22015-05-20 11:11:54 +0100534int efx_ef10_sriov_set_vf_vlan(struct efx_nic *efx, int vf_i, u16 vlan,
535 u8 qos)
536{
537 struct efx_ef10_nic_data *nic_data = efx->nic_data;
538 struct ef10_vf *vf;
539 u16 old_vlan, new_vlan;
540 int rc = 0, rc2 = 0;
541
542 if (vf_i >= efx->vf_count)
543 return -EINVAL;
544 if (qos != 0)
545 return -EINVAL;
546
547 vf = nic_data->vf + vf_i;
548
549 new_vlan = (vlan == 0) ? EFX_EF10_NO_VLAN : vlan;
550 if (new_vlan == vf->vlan)
551 return 0;
552
553 if (vf->efx) {
554 efx_device_detach_sync(vf->efx);
555 efx_net_stop(vf->efx->net_dev);
556
Martin Habetsd2489532016-06-15 17:48:49 +0100557 mutex_lock(&vf->efx->mac_lock);
Shradha Shah2d432f22015-05-20 11:11:54 +0100558 down_write(&vf->efx->filter_sem);
559 vf->efx->type->filter_table_remove(vf->efx);
560
561 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
562 if (rc)
563 goto restore_filters;
564 }
565
566 if (vf->vport_assigned) {
567 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
568 if (rc) {
569 netif_warn(efx, drv, efx->net_dev,
570 "Failed to change vlan on VF %d.\n", vf_i);
571 netif_warn(efx, drv, efx->net_dev,
572 "This is likely because the VF is bound to a driver in a VM.\n");
573 netif_warn(efx, drv, efx->net_dev,
574 "Please unload the driver in the VM.\n");
575 goto restore_vadaptor;
576 }
577 vf->vport_assigned = 0;
578 }
579
580 if (!is_zero_ether_addr(vf->mac)) {
581 rc = efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
582 if (rc)
583 goto restore_evb_port;
584 }
585
586 if (vf->vport_id) {
587 rc = efx_ef10_vport_free(efx, vf->vport_id);
588 if (rc)
589 goto restore_mac;
590 vf->vport_id = 0;
591 }
592
593 /* Do the actual vlan change */
594 old_vlan = vf->vlan;
595 vf->vlan = new_vlan;
596
597 /* Restore everything in reverse order */
598 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
599 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
600 vf->vlan, &vf->vport_id);
601 if (rc)
Shradha Shah671b53e2015-07-08 10:12:45 +0100602 goto reset_nic_up_write;
Shradha Shah2d432f22015-05-20 11:11:54 +0100603
604restore_mac:
605 if (!is_zero_ether_addr(vf->mac)) {
606 rc2 = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
607 if (rc2) {
608 eth_zero_addr(vf->mac);
Shradha Shah671b53e2015-07-08 10:12:45 +0100609 goto reset_nic_up_write;
Shradha Shah2d432f22015-05-20 11:11:54 +0100610 }
611 }
612
613restore_evb_port:
614 rc2 = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
615 if (rc2)
Shradha Shah671b53e2015-07-08 10:12:45 +0100616 goto reset_nic_up_write;
Shradha Shah2d432f22015-05-20 11:11:54 +0100617 else
618 vf->vport_assigned = 1;
619
620restore_vadaptor:
621 if (vf->efx) {
622 rc2 = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
623 if (rc2)
Shradha Shah671b53e2015-07-08 10:12:45 +0100624 goto reset_nic_up_write;
Shradha Shah2d432f22015-05-20 11:11:54 +0100625 }
626
627restore_filters:
628 if (vf->efx) {
629 rc2 = vf->efx->type->filter_table_probe(vf->efx);
630 if (rc2)
Shradha Shah671b53e2015-07-08 10:12:45 +0100631 goto reset_nic_up_write;
632
633 up_write(&vf->efx->filter_sem);
Martin Habetsd2489532016-06-15 17:48:49 +0100634 mutex_unlock(&vf->efx->mac_lock);
Shradha Shah2d432f22015-05-20 11:11:54 +0100635
636 up_write(&vf->efx->filter_sem);
637
638 rc2 = efx_net_open(vf->efx->net_dev);
639 if (rc2)
640 goto reset_nic;
641
642 netif_device_attach(vf->efx->net_dev);
643 }
644 return rc;
645
Shradha Shah671b53e2015-07-08 10:12:45 +0100646reset_nic_up_write:
Martin Habetsd2489532016-06-15 17:48:49 +0100647 if (vf->efx) {
Shradha Shah671b53e2015-07-08 10:12:45 +0100648 up_write(&vf->efx->filter_sem);
Martin Habetsd2489532016-06-15 17:48:49 +0100649 mutex_unlock(&vf->efx->mac_lock);
650 }
Shradha Shah2d432f22015-05-20 11:11:54 +0100651reset_nic:
652 if (vf->efx) {
Shradha Shah2d432f22015-05-20 11:11:54 +0100653 netif_err(efx, drv, efx->net_dev,
654 "Failed to restore VF - scheduling reset.\n");
655 efx_schedule_reset(vf->efx, RESET_TYPE_DATAPATH);
656 } else {
657 netif_err(efx, drv, efx->net_dev,
658 "Failed to restore the VF and cannot reset the VF "
659 "- VF is not functional.\n");
660 netif_err(efx, drv, efx->net_dev,
661 "Please reload the driver attached to the VF.\n");
662 }
663
664 return rc ? rc : rc2;
665}
666
Shradha Shah860d2ff2015-05-20 11:12:30 +0100667int efx_ef10_sriov_set_vf_spoofchk(struct efx_nic *efx, int vf_i,
668 bool spoofchk)
669{
670 return spoofchk ? -EOPNOTSUPP : 0;
671}
672
Edward Cree4392dc62015-05-20 11:12:13 +0100673int efx_ef10_sriov_set_vf_link_state(struct efx_nic *efx, int vf_i,
674 int link_state)
675{
676 MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
677 struct efx_ef10_nic_data *nic_data = efx->nic_data;
678
679 BUILD_BUG_ON(IFLA_VF_LINK_STATE_AUTO !=
680 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_AUTO);
681 BUILD_BUG_ON(IFLA_VF_LINK_STATE_ENABLE !=
682 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_UP);
683 BUILD_BUG_ON(IFLA_VF_LINK_STATE_DISABLE !=
684 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_DOWN);
685 MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
686 LINK_STATE_MODE_IN_FUNCTION_PF,
687 nic_data->pf_index,
688 LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
689 MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE, link_state);
690 return efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
691 NULL, 0, NULL); /* don't care what old mode was */
692}
693
Shradha Shahb9af9042015-05-20 11:11:18 +0100694int efx_ef10_sriov_get_vf_config(struct efx_nic *efx, int vf_i,
695 struct ifla_vf_info *ivf)
696{
Edward Cree4392dc62015-05-20 11:12:13 +0100697 MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
698 MCDI_DECLARE_BUF(outbuf, MC_CMD_LINK_STATE_MODE_OUT_LEN);
699
Shradha Shahb9af9042015-05-20 11:11:18 +0100700 struct efx_ef10_nic_data *nic_data = efx->nic_data;
701 struct ef10_vf *vf;
Edward Cree4392dc62015-05-20 11:12:13 +0100702 size_t outlen;
703 int rc;
Shradha Shahb9af9042015-05-20 11:11:18 +0100704
705 if (vf_i >= efx->vf_count)
706 return -EINVAL;
707
708 if (!nic_data->vf)
709 return -EOPNOTSUPP;
710
711 vf = nic_data->vf + vf_i;
712
713 ivf->vf = vf_i;
714 ivf->min_tx_rate = 0;
715 ivf->max_tx_rate = 0;
716 ether_addr_copy(ivf->mac, vf->mac);
Shradha Shah2d432f22015-05-20 11:11:54 +0100717 ivf->vlan = (vf->vlan == EFX_EF10_NO_VLAN) ? 0 : vf->vlan;
Shradha Shahb9af9042015-05-20 11:11:18 +0100718 ivf->qos = 0;
719
Edward Cree4392dc62015-05-20 11:12:13 +0100720 MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
721 LINK_STATE_MODE_IN_FUNCTION_PF,
722 nic_data->pf_index,
723 LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
724 MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE,
725 MC_CMD_LINK_STATE_MODE_IN_DO_NOT_CHANGE);
726 rc = efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
727 outbuf, sizeof(outbuf), &outlen);
728 if (rc)
729 return rc;
730 if (outlen < MC_CMD_LINK_STATE_MODE_OUT_LEN)
731 return -EIO;
732 ivf->linkstate = MCDI_DWORD(outbuf, LINK_STATE_MODE_OUT_OLD_MODE);
733
Shradha Shahb9af9042015-05-20 11:11:18 +0100734 return 0;
735}
Shradha Shah1d051e02015-06-02 11:38:16 +0100736
737int efx_ef10_sriov_get_phys_port_id(struct efx_nic *efx,
738 struct netdev_phys_item_id *ppid)
739{
740 struct efx_ef10_nic_data *nic_data = efx->nic_data;
741
742 if (!is_valid_ether_addr(nic_data->port_id))
743 return -EOPNOTSUPP;
744
745 ppid->id_len = ETH_ALEN;
746 memcpy(ppid->id, nic_data->port_id, ppid->id_len);
747
748 return 0;
749}