blob: 1b93acf2d28dc5a454f76429c7027fdcf9c59bd3 [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
Shradha Shah3c5eb872015-05-06 00:58:31 +010032static int efx_ef10_vport_add_mac(struct efx_nic *efx,
33 unsigned int port_id, u8 *mac)
Shradha Shah834e23d2015-05-06 00:55:58 +010034{
Shradha Shah3c5eb872015-05-06 00:58:31 +010035 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_ADD_MAC_ADDRESS_IN_LEN);
Shradha Shah834e23d2015-05-06 00:55:58 +010036
Shradha Shah3c5eb872015-05-06 00:58:31 +010037 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);
Shradha Shah834e23d2015-05-06 00:55:58 +010042}
43
Shradha Shah3c5eb872015-05-06 00:58:31 +010044static int efx_ef10_vport_del_mac(struct efx_nic *efx,
45 unsigned int port_id, u8 *mac)
Shradha Shah834e23d2015-05-06 00:55:58 +010046{
Shradha Shah3c5eb872015-05-06 00:58:31 +010047 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +010048
Shradha Shah3c5eb872015-05-06 00:58:31 +010049 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);
Shradha Shah02246a72015-05-06 00:58:14 +010051
Shradha Shah3c5eb872015-05-06 00:58:31 +010052 return efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
53 sizeof(inbuf), NULL, 0, NULL);
Shradha Shah02246a72015-05-06 00:58:14 +010054}
55
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +010056static 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
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100120static 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
Shradha Shah3c5eb872015-05-06 00:58:31 +0100138static 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 }
163 }
164}
165
166static void efx_ef10_sriov_free_vf_vswitching(struct efx_nic *efx)
167{
168 struct efx_ef10_nic_data *nic_data = efx->nic_data;
169
170 efx_ef10_sriov_free_vf_vports(efx);
171 kfree(nic_data->vf);
172 nic_data->vf = NULL;
173}
174
175static int efx_ef10_sriov_assign_vf_vport(struct efx_nic *efx,
176 unsigned int vf_i)
177{
178 struct efx_ef10_nic_data *nic_data = efx->nic_data;
179 struct ef10_vf *vf = nic_data->vf + vf_i;
180 int rc;
181
182 if (WARN_ON_ONCE(!nic_data->vf))
183 return -EOPNOTSUPP;
184
185 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
186 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
187 &vf->vport_id);
188 if (rc)
189 return rc;
190
191 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
192 if (rc) {
193 eth_zero_addr(vf->mac);
194 return rc;
195 }
196
197 rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
198 if (rc)
199 return rc;
200
201 vf->vport_assigned = 1;
202 return 0;
203}
204
205static int efx_ef10_sriov_alloc_vf_vswitching(struct efx_nic *efx)
206{
207 struct efx_ef10_nic_data *nic_data = efx->nic_data;
208 unsigned int i;
209 int rc;
210
211 nic_data->vf = kcalloc(efx->vf_count, sizeof(struct ef10_vf),
212 GFP_KERNEL);
213 if (!nic_data->vf)
214 return -ENOMEM;
215
216 for (i = 0; i < efx->vf_count; i++) {
217 random_ether_addr(nic_data->vf[i].mac);
218
219 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
220 if (rc)
221 goto fail;
222 }
223
224 return 0;
225fail:
226 efx_ef10_sriov_free_vf_vports(efx);
227 kfree(nic_data->vf);
228 nic_data->vf = NULL;
229 return rc;
230}
231
232static int efx_ef10_sriov_restore_vf_vswitching(struct efx_nic *efx)
233{
234 unsigned int i;
235 int rc;
236
237 for (i = 0; i < efx->vf_count; i++) {
238 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
239 if (rc)
240 goto fail;
241 }
242
243 return 0;
244fail:
245 efx_ef10_sriov_free_vf_vswitching(efx);
246 return rc;
247}
248
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100249/* On top of the default firmware vswitch setup, create a VEB vswitch and
250 * expansion vport for use by this function.
251 */
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100252int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100253{
254 struct efx_ef10_nic_data *nic_data = efx->nic_data;
Shradha Shah3c5eb872015-05-06 00:58:31 +0100255 struct net_device *net_dev = efx->net_dev;
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100256 int rc;
257
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100258 if (pci_sriov_get_totalvfs(efx->pci_dev) <= 0) {
259 /* vswitch not needed as we have no VFs */
260 efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
261 return 0;
262 }
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100263
264 rc = efx_ef10_vswitch_alloc(efx, EVB_PORT_ID_ASSIGNED,
265 MC_CMD_VSWITCH_ALLOC_IN_VSWITCH_TYPE_VEB);
266 if (rc)
267 goto fail1;
268
269 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
270 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
271 &nic_data->vport_id);
272 if (rc)
273 goto fail2;
274
Shradha Shah3c5eb872015-05-06 00:58:31 +0100275 rc = efx_ef10_vport_add_mac(efx, nic_data->vport_id, net_dev->dev_addr);
276 if (rc)
277 goto fail3;
Shradha Shah3c5eb872015-05-06 00:58:31 +0100278 ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
279
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100280 rc = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
281 if (rc)
282 goto fail4;
283
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100284 return 0;
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100285fail4:
286 efx_ef10_vport_del_mac(efx, nic_data->vport_id, nic_data->vport_mac);
287 eth_zero_addr(nic_data->vport_mac);
Shradha Shah3c5eb872015-05-06 00:58:31 +0100288fail3:
289 efx_ef10_vport_free(efx, nic_data->vport_id);
290 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100291fail2:
292 efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
293fail1:
294 return rc;
295}
296
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100297int efx_ef10_vswitching_probe_vf(struct efx_nic *efx)
298{
299 struct efx_ef10_nic_data *nic_data = efx->nic_data;
300
301 return efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
302}
303
304int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100305{
306 struct efx_ef10_nic_data *nic_data = efx->nic_data;
307 int rc;
308
309 if (!nic_data->must_probe_vswitching)
310 return 0;
311
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100312 rc = efx_ef10_vswitching_probe_pf(efx);
Shradha Shah3c5eb872015-05-06 00:58:31 +0100313 if (rc)
314 goto fail;
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100315
Shradha Shah3c5eb872015-05-06 00:58:31 +0100316 rc = efx_ef10_sriov_restore_vf_vswitching(efx);
317 if (rc)
318 goto fail;
319
320 nic_data->must_probe_vswitching = false;
321fail:
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100322 return rc;
323}
324
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100325int efx_ef10_vswitching_restore_vf(struct efx_nic *efx)
326{
327 struct efx_ef10_nic_data *nic_data = efx->nic_data;
328 int rc;
329
330 if (!nic_data->must_probe_vswitching)
331 return 0;
332
333 rc = efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
334 if (rc)
335 return rc;
336
337 nic_data->must_probe_vswitching = false;
338 return 0;
339}
340
341void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100342{
343 struct efx_ef10_nic_data *nic_data = efx->nic_data;
344
Shradha Shah3c5eb872015-05-06 00:58:31 +0100345 efx_ef10_sriov_free_vf_vswitching(efx);
346
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100347 efx_ef10_vadaptor_free(efx, nic_data->vport_id);
348
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100349 if (nic_data->vport_id == EVB_PORT_ID_ASSIGNED)
350 return; /* No vswitch was ever created */
351
Shradha Shah3c5eb872015-05-06 00:58:31 +0100352 if (!is_zero_ether_addr(nic_data->vport_mac)) {
353 efx_ef10_vport_del_mac(efx, nic_data->vport_id,
354 efx->net_dev->dev_addr);
355 eth_zero_addr(nic_data->vport_mac);
356 }
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100357 efx_ef10_vport_free(efx, nic_data->vport_id);
358 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
359
360 efx_ef10_vswitch_free(efx, nic_data->vport_id);
361}
Shradha Shah3c5eb872015-05-06 00:58:31 +0100362
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100363void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
364{
365 efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
366}
367
Shradha Shah3c5eb872015-05-06 00:58:31 +0100368static int efx_ef10_pci_sriov_enable(struct efx_nic *efx, int num_vfs)
369{
370 int rc = 0;
371 struct pci_dev *dev = efx->pci_dev;
372
373 efx->vf_count = num_vfs;
374
375 rc = efx_ef10_sriov_alloc_vf_vswitching(efx);
376 if (rc)
377 goto fail1;
378
379 rc = pci_enable_sriov(dev, num_vfs);
380 if (rc)
381 goto fail2;
382
383 return 0;
384fail2:
385 efx_ef10_sriov_free_vf_vswitching(efx);
386fail1:
387 efx->vf_count = 0;
388 netif_err(efx, probe, efx->net_dev,
389 "Failed to enable SRIOV VFs\n");
390 return rc;
391}
392
393static int efx_ef10_pci_sriov_disable(struct efx_nic *efx)
394{
395 struct pci_dev *dev = efx->pci_dev;
396
397 pci_disable_sriov(dev);
398 efx_ef10_sriov_free_vf_vswitching(efx);
399 efx->vf_count = 0;
400 return 0;
401}
402
403int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
404{
405 if (num_vfs == 0)
406 return efx_ef10_pci_sriov_disable(efx);
407 else
408 return efx_ef10_pci_sriov_enable(efx, num_vfs);
409}
410
411int efx_ef10_sriov_init(struct efx_nic *efx)
412{
413 return 0;
414}
415
416void efx_ef10_sriov_fini(struct efx_nic *efx)
417{
418 struct efx_ef10_nic_data *nic_data = efx->nic_data;
419 int rc;
420
421 if (!nic_data->vf)
422 return;
423
424 rc = efx_ef10_pci_sriov_disable(efx);
425 if (rc)
426 netif_dbg(efx, drv, efx->net_dev,
427 "Disabling SRIOV was not successful rc=%d\n", rc);
428 else
429 netif_dbg(efx, drv, efx->net_dev, "SRIOV disabled\n");
430}