sfc: manually allocate and free vadaptors
[deliverable/linux.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
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
71 static 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
81 static 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
110 static 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
120 static 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
129 static 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
138 static 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
166 static 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
175 static 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
205 static 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;
225 fail:
226 efx_ef10_sriov_free_vf_vports(efx);
227 kfree(nic_data->vf);
228 nic_data->vf = NULL;
229 return rc;
230 }
231
232 static 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;
244 fail:
245 efx_ef10_sriov_free_vf_vswitching(efx);
246 return rc;
247 }
248
249 /* On top of the default firmware vswitch setup, create a VEB vswitch and
250 * expansion vport for use by this function.
251 */
252 int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
253 {
254 struct efx_ef10_nic_data *nic_data = efx->nic_data;
255 struct net_device *net_dev = efx->net_dev;
256 int rc;
257
258 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 }
263
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
275 rc = efx_ef10_vport_add_mac(efx, nic_data->vport_id, net_dev->dev_addr);
276 if (rc)
277 goto fail3;
278 ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
279
280 rc = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
281 if (rc)
282 goto fail4;
283
284 return 0;
285 fail4:
286 efx_ef10_vport_del_mac(efx, nic_data->vport_id, nic_data->vport_mac);
287 eth_zero_addr(nic_data->vport_mac);
288 fail3:
289 efx_ef10_vport_free(efx, nic_data->vport_id);
290 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
291 fail2:
292 efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
293 fail1:
294 return rc;
295 }
296
297 int 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
304 int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
305 {
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
312 rc = efx_ef10_vswitching_probe_pf(efx);
313 if (rc)
314 goto fail;
315
316 rc = efx_ef10_sriov_restore_vf_vswitching(efx);
317 if (rc)
318 goto fail;
319
320 nic_data->must_probe_vswitching = false;
321 fail:
322 return rc;
323 }
324
325 int 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
341 void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
342 {
343 struct efx_ef10_nic_data *nic_data = efx->nic_data;
344
345 efx_ef10_sriov_free_vf_vswitching(efx);
346
347 efx_ef10_vadaptor_free(efx, nic_data->vport_id);
348
349 if (nic_data->vport_id == EVB_PORT_ID_ASSIGNED)
350 return; /* No vswitch was ever created */
351
352 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 }
357 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 }
362
363 void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
364 {
365 efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
366 }
367
368 static 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;
384 fail2:
385 efx_ef10_sriov_free_vf_vswitching(efx);
386 fail1:
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
393 static 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
403 int 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
411 int efx_ef10_sriov_init(struct efx_nic *efx)
412 {
413 return 0;
414 }
415
416 void 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 }
This page took 0.042255 seconds and 5 git commands to generate.