blob: 6208dd76bc2b3f88a97c2104783ef02db7076040 [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 }
Shradha Shahf1122a32015-05-20 11:09:46 +0100163
164 vf->efx = NULL;
Shradha Shah3c5eb872015-05-06 00:58:31 +0100165 }
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);
Shradha Shahf1122a32015-05-20 11:09:46 +0100220 nic_data->vf[i].efx = NULL;
Shradha Shah3c5eb872015-05-06 00:58:31 +0100221
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
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100252/* On top of the default firmware vswitch setup, create a VEB vswitch and
253 * expansion vport for use by this function.
254 */
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100255int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100256{
257 struct efx_ef10_nic_data *nic_data = efx->nic_data;
Shradha Shah3c5eb872015-05-06 00:58:31 +0100258 struct net_device *net_dev = efx->net_dev;
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100259 int rc;
260
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100261 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 }
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100266
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
Shradha Shah3c5eb872015-05-06 00:58:31 +0100278 rc = efx_ef10_vport_add_mac(efx, nic_data->vport_id, net_dev->dev_addr);
279 if (rc)
280 goto fail3;
Shradha Shah3c5eb872015-05-06 00:58:31 +0100281 ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
282
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100283 rc = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
284 if (rc)
285 goto fail4;
286
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100287 return 0;
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100288fail4:
289 efx_ef10_vport_del_mac(efx, nic_data->vport_id, nic_data->vport_mac);
290 eth_zero_addr(nic_data->vport_mac);
Shradha Shah3c5eb872015-05-06 00:58:31 +0100291fail3:
292 efx_ef10_vport_free(efx, nic_data->vport_id);
293 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100294fail2:
295 efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
296fail1:
297 return rc;
298}
299
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100300int 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)
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100308{
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
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100315 rc = efx_ef10_vswitching_probe_pf(efx);
Shradha Shah3c5eb872015-05-06 00:58:31 +0100316 if (rc)
317 goto fail;
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100318
Shradha Shah3c5eb872015-05-06 00:58:31 +0100319 rc = efx_ef10_sriov_restore_vf_vswitching(efx);
320 if (rc)
321 goto fail;
322
323 nic_data->must_probe_vswitching = false;
324fail:
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100325 return rc;
326}
327
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100328int 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)
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100345{
346 struct efx_ef10_nic_data *nic_data = efx->nic_data;
347
Shradha Shah3c5eb872015-05-06 00:58:31 +0100348 efx_ef10_sriov_free_vf_vswitching(efx);
349
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100350 efx_ef10_vadaptor_free(efx, nic_data->vport_id);
351
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100352 if (nic_data->vport_id == EVB_PORT_ID_ASSIGNED)
353 return; /* No vswitch was ever created */
354
Shradha Shah3c5eb872015-05-06 00:58:31 +0100355 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 }
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +0100360 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}
Shradha Shah3c5eb872015-05-06 00:58:31 +0100365
Shradha Shah7b8c7b52015-05-06 00:58:54 +0100366void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
367{
368 efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
369}
370
Shradha Shah3c5eb872015-05-06 00:58:31 +0100371static 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}