Linux Audio

Check our new training course

Loading...
v4.17
  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/etherdevice.h>
 10#include <linux/pci.h>
 11#include <linux/module.h>
 12#include "net_driver.h"
 13#include "ef10_sriov.h"
 14#include "efx.h"
 15#include "nic.h"
 16#include "mcdi_pcol.h"
 17
 18static int efx_ef10_evb_port_assign(struct efx_nic *efx, unsigned int port_id,
 19				    unsigned int vf_fn)
 20{
 21	MCDI_DECLARE_BUF(inbuf, MC_CMD_EVB_PORT_ASSIGN_IN_LEN);
 22	struct efx_ef10_nic_data *nic_data = efx->nic_data;
 23
 24	MCDI_SET_DWORD(inbuf, EVB_PORT_ASSIGN_IN_PORT_ID, port_id);
 25	MCDI_POPULATE_DWORD_2(inbuf, EVB_PORT_ASSIGN_IN_FUNCTION,
 26			      EVB_PORT_ASSIGN_IN_PF, nic_data->pf_index,
 27			      EVB_PORT_ASSIGN_IN_VF, vf_fn);
 28
 29	return efx_mcdi_rpc(efx, MC_CMD_EVB_PORT_ASSIGN, inbuf, sizeof(inbuf),
 30			    NULL, 0, NULL);
 31}
 32
 33static int efx_ef10_vswitch_alloc(struct efx_nic *efx, unsigned int port_id,
 34				  unsigned int vswitch_type)
 35{
 36	MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_ALLOC_IN_LEN);
 37	int rc;
 38
 39	MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
 40	MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_TYPE, vswitch_type);
 41	MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 2);
 42	MCDI_POPULATE_DWORD_1(inbuf, VSWITCH_ALLOC_IN_FLAGS,
 43			      VSWITCH_ALLOC_IN_FLAG_AUTO_PORT, 0);
 44
 45	/* Quietly try to allocate 2 VLAN tags */
 46	rc = efx_mcdi_rpc_quiet(efx, MC_CMD_VSWITCH_ALLOC, inbuf, sizeof(inbuf),
 47				NULL, 0, NULL);
 48
 49	/* If 2 VLAN tags is too many, revert to trying with 1 VLAN tags */
 50	if (rc == -EPROTO) {
 51		MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 1);
 52		rc = efx_mcdi_rpc(efx, MC_CMD_VSWITCH_ALLOC, inbuf,
 53				  sizeof(inbuf), NULL, 0, NULL);
 54	} else if (rc) {
 55		efx_mcdi_display_error(efx, MC_CMD_VSWITCH_ALLOC,
 56				       MC_CMD_VSWITCH_ALLOC_IN_LEN,
 57				       NULL, 0, rc);
 58	}
 59	return rc;
 60}
 61
 62static int efx_ef10_vswitch_free(struct efx_nic *efx, unsigned int port_id)
 63{
 64	MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_FREE_IN_LEN);
 65
 66	MCDI_SET_DWORD(inbuf, VSWITCH_FREE_IN_UPSTREAM_PORT_ID, port_id);
 67
 68	return efx_mcdi_rpc(efx, MC_CMD_VSWITCH_FREE, inbuf, sizeof(inbuf),
 69			    NULL, 0, NULL);
 70}
 71
 72static int efx_ef10_vport_alloc(struct efx_nic *efx,
 73				unsigned int port_id_in,
 74				unsigned int vport_type,
 75				u16 vlan,
 76				unsigned int *port_id_out)
 77{
 78	MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_ALLOC_IN_LEN);
 79	MCDI_DECLARE_BUF(outbuf, MC_CMD_VPORT_ALLOC_OUT_LEN);
 80	size_t outlen;
 81	int rc;
 82
 83	EFX_WARN_ON_PARANOID(!port_id_out);
 84
 85	MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_UPSTREAM_PORT_ID, port_id_in);
 86	MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_TYPE, vport_type);
 87	MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_NUM_VLAN_TAGS,
 88		       (vlan != EFX_EF10_NO_VLAN));
 89	MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_FLAGS,
 90			      VPORT_ALLOC_IN_FLAG_AUTO_PORT, 0);
 91	if (vlan != EFX_EF10_NO_VLAN)
 92		MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_VLAN_TAGS,
 93				      VPORT_ALLOC_IN_VLAN_TAG_0, vlan);
 94
 95	rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_ALLOC, inbuf, sizeof(inbuf),
 96			  outbuf, sizeof(outbuf), &outlen);
 97	if (rc)
 98		return rc;
 99	if (outlen < MC_CMD_VPORT_ALLOC_OUT_LEN)
100		return -EIO;
101
102	*port_id_out = MCDI_DWORD(outbuf, VPORT_ALLOC_OUT_VPORT_ID);
103	return 0;
104}
105
106static int efx_ef10_vport_free(struct efx_nic *efx, unsigned int port_id)
107{
108	MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_FREE_IN_LEN);
109
110	MCDI_SET_DWORD(inbuf, VPORT_FREE_IN_VPORT_ID, port_id);
111
112	return efx_mcdi_rpc(efx, MC_CMD_VPORT_FREE, inbuf, sizeof(inbuf),
113			    NULL, 0, NULL);
114}
115
116static void efx_ef10_sriov_free_vf_vports(struct efx_nic *efx)
117{
118	struct efx_ef10_nic_data *nic_data = efx->nic_data;
119	int i;
120
121	if (!nic_data->vf)
122		return;
123
124	for (i = 0; i < efx->vf_count; i++) {
125		struct ef10_vf *vf = nic_data->vf + i;
126
127		/* If VF is assigned, do not free the vport  */
128		if (vf->pci_dev &&
129		    vf->pci_dev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
130			continue;
131
132		if (vf->vport_assigned) {
133			efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, i);
134			vf->vport_assigned = 0;
135		}
136
137		if (!is_zero_ether_addr(vf->mac)) {
138			efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
139			eth_zero_addr(vf->mac);
140		}
141
142		if (vf->vport_id) {
143			efx_ef10_vport_free(efx, vf->vport_id);
144			vf->vport_id = 0;
145		}
146
147		vf->efx = NULL;
148	}
149}
150
151static void efx_ef10_sriov_free_vf_vswitching(struct efx_nic *efx)
152{
153	struct efx_ef10_nic_data *nic_data = efx->nic_data;
154
155	efx_ef10_sriov_free_vf_vports(efx);
156	kfree(nic_data->vf);
157	nic_data->vf = NULL;
158}
159
160static int efx_ef10_sriov_assign_vf_vport(struct efx_nic *efx,
161					  unsigned int vf_i)
162{
163	struct efx_ef10_nic_data *nic_data = efx->nic_data;
164	struct ef10_vf *vf = nic_data->vf + vf_i;
165	int rc;
166
167	if (WARN_ON_ONCE(!nic_data->vf))
168		return -EOPNOTSUPP;
169
170	rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
171				  MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
172				  vf->vlan, &vf->vport_id);
173	if (rc)
174		return rc;
175
176	rc = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
177	if (rc) {
178		eth_zero_addr(vf->mac);
179		return rc;
180	}
181
182	rc =  efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
183	if (rc)
184		return rc;
185
186	vf->vport_assigned = 1;
187	return 0;
188}
189
190static int efx_ef10_sriov_alloc_vf_vswitching(struct efx_nic *efx)
191{
192	struct efx_ef10_nic_data *nic_data = efx->nic_data;
193	unsigned int i;
194	int rc;
195
196	nic_data->vf = kcalloc(efx->vf_count, sizeof(struct ef10_vf),
197			       GFP_KERNEL);
198	if (!nic_data->vf)
199		return -ENOMEM;
200
201	for (i = 0; i < efx->vf_count; i++) {
202		random_ether_addr(nic_data->vf[i].mac);
203		nic_data->vf[i].efx = NULL;
204		nic_data->vf[i].vlan = EFX_EF10_NO_VLAN;
205
206		rc = efx_ef10_sriov_assign_vf_vport(efx, i);
207		if (rc)
208			goto fail;
209	}
210
211	return 0;
212fail:
213	efx_ef10_sriov_free_vf_vports(efx);
214	kfree(nic_data->vf);
215	nic_data->vf = NULL;
216	return rc;
217}
218
219static int efx_ef10_sriov_restore_vf_vswitching(struct efx_nic *efx)
220{
221	unsigned int i;
222	int rc;
223
224	for (i = 0; i < efx->vf_count; i++) {
225		rc = efx_ef10_sriov_assign_vf_vport(efx, i);
226		if (rc)
227			goto fail;
228	}
229
230	return 0;
231fail:
232	efx_ef10_sriov_free_vf_vswitching(efx);
233	return rc;
234}
235
236static int efx_ef10_vadaptor_alloc_set_features(struct efx_nic *efx)
237{
238	struct efx_ef10_nic_data *nic_data = efx->nic_data;
239	u32 port_flags;
240	int rc;
241
242	rc = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
243	if (rc)
244		goto fail_vadaptor_alloc;
245
246	rc = efx_ef10_vadaptor_query(efx, nic_data->vport_id,
247				     &port_flags, NULL, NULL);
248	if (rc)
249		goto fail_vadaptor_query;
250
251	if (port_flags &
252	    (1 << MC_CMD_VPORT_ALLOC_IN_FLAG_VLAN_RESTRICT_LBN))
253		efx->fixed_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
254	else
255		efx->fixed_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
256
257	return 0;
258
259fail_vadaptor_query:
260	efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
261fail_vadaptor_alloc:
262	return rc;
263}
264
265/* On top of the default firmware vswitch setup, create a VEB vswitch and
266 * expansion vport for use by this function.
267 */
268int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
269{
270	struct efx_ef10_nic_data *nic_data = efx->nic_data;
271	struct net_device *net_dev = efx->net_dev;
272	int rc;
273
274	if (pci_sriov_get_totalvfs(efx->pci_dev) <= 0) {
275		/* vswitch not needed as we have no VFs */
276		efx_ef10_vadaptor_alloc_set_features(efx);
277		return 0;
278	}
279
280	rc = efx_ef10_vswitch_alloc(efx, EVB_PORT_ID_ASSIGNED,
281				    MC_CMD_VSWITCH_ALLOC_IN_VSWITCH_TYPE_VEB);
282	if (rc)
283		goto fail1;
284
285	rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
286				  MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
287				  EFX_EF10_NO_VLAN, &nic_data->vport_id);
288	if (rc)
289		goto fail2;
290
291	rc = efx_ef10_vport_add_mac(efx, nic_data->vport_id, net_dev->dev_addr);
292	if (rc)
293		goto fail3;
294	ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
295
296	rc = efx_ef10_vadaptor_alloc_set_features(efx);
297	if (rc)
298		goto fail4;
299
300	return 0;
301fail4:
302	efx_ef10_vport_del_mac(efx, nic_data->vport_id, nic_data->vport_mac);
303	eth_zero_addr(nic_data->vport_mac);
304fail3:
305	efx_ef10_vport_free(efx, nic_data->vport_id);
306	nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
307fail2:
308	efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
309fail1:
310	return rc;
311}
312
313int efx_ef10_vswitching_probe_vf(struct efx_nic *efx)
314{
315	return efx_ef10_vadaptor_alloc_set_features(efx);
316}
317
318int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
319{
320	struct efx_ef10_nic_data *nic_data = efx->nic_data;
321	int rc;
322
323	if (!nic_data->must_probe_vswitching)
324		return 0;
325
326	rc = efx_ef10_vswitching_probe_pf(efx);
327	if (rc)
328		goto fail;
329
330	rc = efx_ef10_sriov_restore_vf_vswitching(efx);
331	if (rc)
332		goto fail;
333
334	nic_data->must_probe_vswitching = false;
335fail:
336	return rc;
337}
338
339int efx_ef10_vswitching_restore_vf(struct efx_nic *efx)
340{
341	struct efx_ef10_nic_data *nic_data = efx->nic_data;
342	int rc;
343
344	if (!nic_data->must_probe_vswitching)
345		return 0;
346
347	rc = efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
348	if (rc)
349		return rc;
350
351	nic_data->must_probe_vswitching = false;
352	return 0;
353}
354
355void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
356{
357	struct efx_ef10_nic_data *nic_data = efx->nic_data;
358
359	efx_ef10_sriov_free_vf_vswitching(efx);
360
361	efx_ef10_vadaptor_free(efx, nic_data->vport_id);
362
363	if (nic_data->vport_id == EVB_PORT_ID_ASSIGNED)
364		return; /* No vswitch was ever created */
365
366	if (!is_zero_ether_addr(nic_data->vport_mac)) {
367		efx_ef10_vport_del_mac(efx, nic_data->vport_id,
368				       efx->net_dev->dev_addr);
369		eth_zero_addr(nic_data->vport_mac);
370	}
371	efx_ef10_vport_free(efx, nic_data->vport_id);
372	nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
373
374	/* Only free the vswitch if no VFs are assigned */
375	if (!pci_vfs_assigned(efx->pci_dev))
376		efx_ef10_vswitch_free(efx, nic_data->vport_id);
377}
378
379void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
380{
381	efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
382}
383
384static int efx_ef10_pci_sriov_enable(struct efx_nic *efx, int num_vfs)
385{
386	int rc = 0;
387	struct pci_dev *dev = efx->pci_dev;
388
389	efx->vf_count = num_vfs;
390
391	rc = efx_ef10_sriov_alloc_vf_vswitching(efx);
392	if (rc)
393		goto fail1;
394
395	rc = pci_enable_sriov(dev, num_vfs);
396	if (rc)
397		goto fail2;
398
399	return 0;
400fail2:
401	efx_ef10_sriov_free_vf_vswitching(efx);
402fail1:
403	efx->vf_count = 0;
404	netif_err(efx, probe, efx->net_dev,
405		  "Failed to enable SRIOV VFs\n");
406	return rc;
407}
408
409static int efx_ef10_pci_sriov_disable(struct efx_nic *efx, bool force)
410{
411	struct pci_dev *dev = efx->pci_dev;
412	unsigned int vfs_assigned = 0;
413
414	vfs_assigned = pci_vfs_assigned(dev);
415
416	if (vfs_assigned && !force) {
417		netif_info(efx, drv, efx->net_dev, "VFs are assigned to guests; "
418			   "please detach them before disabling SR-IOV\n");
419		return -EBUSY;
420	}
421
422	if (!vfs_assigned)
423		pci_disable_sriov(dev);
424
425	efx_ef10_sriov_free_vf_vswitching(efx);
426	efx->vf_count = 0;
427	return 0;
428}
429
430int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
431{
432	if (num_vfs == 0)
433		return efx_ef10_pci_sriov_disable(efx, false);
434	else
435		return efx_ef10_pci_sriov_enable(efx, num_vfs);
436}
437
438int efx_ef10_sriov_init(struct efx_nic *efx)
439{
440	return 0;
441}
442
443void efx_ef10_sriov_fini(struct efx_nic *efx)
444{
445	struct efx_ef10_nic_data *nic_data = efx->nic_data;
446	unsigned int i;
447	int rc;
448
449	if (!nic_data->vf) {
450		/* Remove any un-assigned orphaned VFs */
451		if (pci_num_vf(efx->pci_dev) && !pci_vfs_assigned(efx->pci_dev))
452			pci_disable_sriov(efx->pci_dev);
453		return;
454	}
455
456	/* Remove any VFs in the host */
457	for (i = 0; i < efx->vf_count; ++i) {
458		struct efx_nic *vf_efx = nic_data->vf[i].efx;
459
460		if (vf_efx)
461			vf_efx->pci_dev->driver->remove(vf_efx->pci_dev);
462	}
463
464	rc = efx_ef10_pci_sriov_disable(efx, true);
465	if (rc)
466		netif_dbg(efx, drv, efx->net_dev,
467			  "Disabling SRIOV was not successful rc=%d\n", rc);
468	else
469		netif_dbg(efx, drv, efx->net_dev, "SRIOV disabled\n");
470}
471
472static int efx_ef10_vport_del_vf_mac(struct efx_nic *efx, unsigned int port_id,
473				     u8 *mac)
474{
475	MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
476	MCDI_DECLARE_BUF_ERR(outbuf);
477	size_t outlen;
478	int rc;
479
480	MCDI_SET_DWORD(inbuf, VPORT_DEL_MAC_ADDRESS_IN_VPORT_ID, port_id);
481	ether_addr_copy(MCDI_PTR(inbuf, VPORT_DEL_MAC_ADDRESS_IN_MACADDR), mac);
482
483	rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
484			  sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
485
486	return rc;
487}
488
489int efx_ef10_sriov_set_vf_mac(struct efx_nic *efx, int vf_i, u8 *mac)
490{
491	struct efx_ef10_nic_data *nic_data = efx->nic_data;
492	struct ef10_vf *vf;
493	int rc;
494
495	if (!nic_data->vf)
496		return -EOPNOTSUPP;
497
498	if (vf_i >= efx->vf_count)
499		return -EINVAL;
500	vf = nic_data->vf + vf_i;
501
502	if (vf->efx) {
503		efx_device_detach_sync(vf->efx);
504		efx_net_stop(vf->efx->net_dev);
505
506		down_write(&vf->efx->filter_sem);
507		vf->efx->type->filter_table_remove(vf->efx);
508
509		rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
510		if (rc) {
511			up_write(&vf->efx->filter_sem);
512			return rc;
513		}
514	}
515
516	rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
517	if (rc)
518		return rc;
519
520	if (!is_zero_ether_addr(vf->mac)) {
521		rc = efx_ef10_vport_del_vf_mac(efx, vf->vport_id, vf->mac);
522		if (rc)
523			return rc;
524	}
525
526	if (!is_zero_ether_addr(mac)) {
527		rc = efx_ef10_vport_add_mac(efx, vf->vport_id, mac);
528		if (rc) {
529			eth_zero_addr(vf->mac);
530			goto fail;
531		}
532		if (vf->efx)
533			ether_addr_copy(vf->efx->net_dev->dev_addr, mac);
534	}
535
536	ether_addr_copy(vf->mac, mac);
537
538	rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
539	if (rc)
540		goto fail;
541
542	if (vf->efx) {
543		/* VF cannot use the vport_id that the PF created */
544		rc = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
545		if (rc) {
546			up_write(&vf->efx->filter_sem);
547			return rc;
548		}
549		vf->efx->type->filter_table_probe(vf->efx);
550		up_write(&vf->efx->filter_sem);
551		efx_net_open(vf->efx->net_dev);
552		efx_device_attach_if_not_resetting(vf->efx);
553	}
554
555	return 0;
556
557fail:
558	eth_zero_addr(vf->mac);
559	return rc;
560}
561
562int efx_ef10_sriov_set_vf_vlan(struct efx_nic *efx, int vf_i, u16 vlan,
563			       u8 qos)
564{
565	struct efx_ef10_nic_data *nic_data = efx->nic_data;
566	struct ef10_vf *vf;
567	u16 old_vlan, new_vlan;
568	int rc = 0, rc2 = 0;
569
570	if (vf_i >= efx->vf_count)
571		return -EINVAL;
572	if (qos != 0)
573		return -EINVAL;
574
575	vf = nic_data->vf + vf_i;
576
577	new_vlan = (vlan == 0) ? EFX_EF10_NO_VLAN : vlan;
578	if (new_vlan == vf->vlan)
579		return 0;
580
581	if (vf->efx) {
582		efx_device_detach_sync(vf->efx);
583		efx_net_stop(vf->efx->net_dev);
584
585		mutex_lock(&vf->efx->mac_lock);
586		down_write(&vf->efx->filter_sem);
587		vf->efx->type->filter_table_remove(vf->efx);
588
589		rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
590		if (rc)
591			goto restore_filters;
592	}
593
594	if (vf->vport_assigned) {
595		rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
596		if (rc) {
597			netif_warn(efx, drv, efx->net_dev,
598				   "Failed to change vlan on VF %d.\n", vf_i);
599			netif_warn(efx, drv, efx->net_dev,
600				   "This is likely because the VF is bound to a driver in a VM.\n");
601			netif_warn(efx, drv, efx->net_dev,
602				   "Please unload the driver in the VM.\n");
603			goto restore_vadaptor;
604		}
605		vf->vport_assigned = 0;
606	}
607
608	if (!is_zero_ether_addr(vf->mac)) {
609		rc = efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
610		if (rc)
611			goto restore_evb_port;
612	}
613
614	if (vf->vport_id) {
615		rc = efx_ef10_vport_free(efx, vf->vport_id);
616		if (rc)
617			goto restore_mac;
618		vf->vport_id = 0;
619	}
620
621	/* Do the actual vlan change */
622	old_vlan = vf->vlan;
623	vf->vlan = new_vlan;
624
625	/* Restore everything in reverse order */
626	rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
627				  MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
628				  vf->vlan, &vf->vport_id);
629	if (rc)
630		goto reset_nic_up_write;
631
632restore_mac:
633	if (!is_zero_ether_addr(vf->mac)) {
634		rc2 = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
635		if (rc2) {
636			eth_zero_addr(vf->mac);
637			goto reset_nic_up_write;
638		}
639	}
640
641restore_evb_port:
642	rc2 = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
643	if (rc2)
644		goto reset_nic_up_write;
645	else
646		vf->vport_assigned = 1;
647
648restore_vadaptor:
649	if (vf->efx) {
650		rc2 = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
651		if (rc2)
652			goto reset_nic_up_write;
653	}
654
655restore_filters:
656	if (vf->efx) {
657		rc2 = vf->efx->type->filter_table_probe(vf->efx);
658		if (rc2)
659			goto reset_nic_up_write;
660
661		up_write(&vf->efx->filter_sem);
662		mutex_unlock(&vf->efx->mac_lock);
663
 
 
664		rc2 = efx_net_open(vf->efx->net_dev);
665		if (rc2)
666			goto reset_nic;
667
668		efx_device_attach_if_not_resetting(vf->efx);
669	}
670	return rc;
671
672reset_nic_up_write:
673	if (vf->efx) {
674		up_write(&vf->efx->filter_sem);
675		mutex_unlock(&vf->efx->mac_lock);
676	}
677reset_nic:
678	if (vf->efx) {
679		netif_err(efx, drv, efx->net_dev,
680			  "Failed to restore VF - scheduling reset.\n");
681		efx_schedule_reset(vf->efx, RESET_TYPE_DATAPATH);
682	} else {
683		netif_err(efx, drv, efx->net_dev,
684			  "Failed to restore the VF and cannot reset the VF "
685			  "- VF is not functional.\n");
686		netif_err(efx, drv, efx->net_dev,
687			  "Please reload the driver attached to the VF.\n");
688	}
689
690	return rc ? rc : rc2;
691}
692
693int efx_ef10_sriov_set_vf_spoofchk(struct efx_nic *efx, int vf_i,
694				   bool spoofchk)
695{
696	return spoofchk ? -EOPNOTSUPP : 0;
697}
698
699int efx_ef10_sriov_set_vf_link_state(struct efx_nic *efx, int vf_i,
700				     int link_state)
701{
702	MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
703	struct efx_ef10_nic_data *nic_data = efx->nic_data;
704
705	BUILD_BUG_ON(IFLA_VF_LINK_STATE_AUTO !=
706		     MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_AUTO);
707	BUILD_BUG_ON(IFLA_VF_LINK_STATE_ENABLE !=
708		     MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_UP);
709	BUILD_BUG_ON(IFLA_VF_LINK_STATE_DISABLE !=
710		     MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_DOWN);
711	MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
712			      LINK_STATE_MODE_IN_FUNCTION_PF,
713			      nic_data->pf_index,
714			      LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
715	MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE, link_state);
716	return efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
717			    NULL, 0, NULL); /* don't care what old mode was */
718}
719
720int efx_ef10_sriov_get_vf_config(struct efx_nic *efx, int vf_i,
721				 struct ifla_vf_info *ivf)
722{
723	MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
724	MCDI_DECLARE_BUF(outbuf, MC_CMD_LINK_STATE_MODE_OUT_LEN);
725
726	struct efx_ef10_nic_data *nic_data = efx->nic_data;
727	struct ef10_vf *vf;
728	size_t outlen;
729	int rc;
730
731	if (vf_i >= efx->vf_count)
732		return -EINVAL;
733
734	if (!nic_data->vf)
735		return -EOPNOTSUPP;
736
737	vf = nic_data->vf + vf_i;
738
739	ivf->vf = vf_i;
740	ivf->min_tx_rate = 0;
741	ivf->max_tx_rate = 0;
742	ether_addr_copy(ivf->mac, vf->mac);
743	ivf->vlan = (vf->vlan == EFX_EF10_NO_VLAN) ? 0 : vf->vlan;
744	ivf->qos = 0;
745
746	MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
747			      LINK_STATE_MODE_IN_FUNCTION_PF,
748			      nic_data->pf_index,
749			      LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
750	MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE,
751		       MC_CMD_LINK_STATE_MODE_IN_DO_NOT_CHANGE);
752	rc = efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
753			  outbuf, sizeof(outbuf), &outlen);
754	if (rc)
755		return rc;
756	if (outlen < MC_CMD_LINK_STATE_MODE_OUT_LEN)
757		return -EIO;
758	ivf->linkstate = MCDI_DWORD(outbuf, LINK_STATE_MODE_OUT_OLD_MODE);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
759
760	return 0;
761}
v4.10.11
  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
 17static 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
 32static 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);
 36	int rc;
 37
 38	MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
 39	MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_TYPE, vswitch_type);
 40	MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 2);
 41	MCDI_POPULATE_DWORD_1(inbuf, VSWITCH_ALLOC_IN_FLAGS,
 42			      VSWITCH_ALLOC_IN_FLAG_AUTO_PORT, 0);
 43
 44	/* 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;
 59}
 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,
 74				u16 vlan,
 75				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);
 86	MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_NUM_VLAN_TAGS,
 87		       (vlan != EFX_EF10_NO_VLAN));
 88	MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_FLAGS,
 89			      VPORT_ALLOC_IN_FLAG_AUTO_PORT, 0);
 90	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);
 93
 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
115static 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
126		/* 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
131		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		}
145
146		vf->efx = NULL;
147	}
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,
171				  vf->vlan, &vf->vport_id);
172	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);
202		nic_data->vf[i].efx = NULL;
203		nic_data->vf[i].vlan = EFX_EF10_NO_VLAN;
204
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
235static int efx_ef10_vadaptor_alloc_set_features(struct efx_nic *efx)
236{
237	struct efx_ef10_nic_data *nic_data = efx->nic_data;
238	u32 port_flags;
239	int rc;
240
241	rc = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
242	if (rc)
243		goto fail_vadaptor_alloc;
244
245	rc = efx_ef10_vadaptor_query(efx, nic_data->vport_id,
246				     &port_flags, NULL, NULL);
247	if (rc)
248		goto fail_vadaptor_query;
249
250	if (port_flags &
251	    (1 << MC_CMD_VPORT_ALLOC_IN_FLAG_VLAN_RESTRICT_LBN))
252		efx->fixed_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
253	else
254		efx->fixed_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
255
256	return 0;
257
258fail_vadaptor_query:
259	efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
260fail_vadaptor_alloc:
261	return rc;
262}
263
264/* On top of the default firmware vswitch setup, create a VEB vswitch and
265 * expansion vport for use by this function.
266 */
267int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
268{
269	struct efx_ef10_nic_data *nic_data = efx->nic_data;
270	struct net_device *net_dev = efx->net_dev;
271	int rc;
272
273	if (pci_sriov_get_totalvfs(efx->pci_dev) <= 0) {
274		/* vswitch not needed as we have no VFs */
275		efx_ef10_vadaptor_alloc_set_features(efx);
276		return 0;
277	}
278
279	rc = efx_ef10_vswitch_alloc(efx, EVB_PORT_ID_ASSIGNED,
280				    MC_CMD_VSWITCH_ALLOC_IN_VSWITCH_TYPE_VEB);
281	if (rc)
282		goto fail1;
283
284	rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
285				  MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
286				  EFX_EF10_NO_VLAN, &nic_data->vport_id);
287	if (rc)
288		goto fail2;
289
290	rc = efx_ef10_vport_add_mac(efx, nic_data->vport_id, net_dev->dev_addr);
291	if (rc)
292		goto fail3;
293	ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
294
295	rc = efx_ef10_vadaptor_alloc_set_features(efx);
296	if (rc)
297		goto fail4;
298
299	return 0;
300fail4:
301	efx_ef10_vport_del_mac(efx, nic_data->vport_id, nic_data->vport_mac);
302	eth_zero_addr(nic_data->vport_mac);
303fail3:
304	efx_ef10_vport_free(efx, nic_data->vport_id);
305	nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
306fail2:
307	efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
308fail1:
309	return rc;
310}
311
312int efx_ef10_vswitching_probe_vf(struct efx_nic *efx)
313{
314	return efx_ef10_vadaptor_alloc_set_features(efx);
315}
316
317int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
318{
319	struct efx_ef10_nic_data *nic_data = efx->nic_data;
320	int rc;
321
322	if (!nic_data->must_probe_vswitching)
323		return 0;
324
325	rc = efx_ef10_vswitching_probe_pf(efx);
326	if (rc)
327		goto fail;
328
329	rc = efx_ef10_sriov_restore_vf_vswitching(efx);
330	if (rc)
331		goto fail;
332
333	nic_data->must_probe_vswitching = false;
334fail:
335	return rc;
336}
337
338int efx_ef10_vswitching_restore_vf(struct efx_nic *efx)
339{
340	struct efx_ef10_nic_data *nic_data = efx->nic_data;
341	int rc;
342
343	if (!nic_data->must_probe_vswitching)
344		return 0;
345
346	rc = efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
347	if (rc)
348		return rc;
349
350	nic_data->must_probe_vswitching = false;
351	return 0;
352}
353
354void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
355{
356	struct efx_ef10_nic_data *nic_data = efx->nic_data;
357
358	efx_ef10_sriov_free_vf_vswitching(efx);
359
360	efx_ef10_vadaptor_free(efx, nic_data->vport_id);
361
362	if (nic_data->vport_id == EVB_PORT_ID_ASSIGNED)
363		return; /* No vswitch was ever created */
364
365	if (!is_zero_ether_addr(nic_data->vport_mac)) {
366		efx_ef10_vport_del_mac(efx, nic_data->vport_id,
367				       efx->net_dev->dev_addr);
368		eth_zero_addr(nic_data->vport_mac);
369	}
370	efx_ef10_vport_free(efx, nic_data->vport_id);
371	nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
372
373	/* Only free the vswitch if no VFs are assigned */
374	if (!pci_vfs_assigned(efx->pci_dev))
375		efx_ef10_vswitch_free(efx, nic_data->vport_id);
376}
377
378void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
379{
380	efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
381}
382
383static int efx_ef10_pci_sriov_enable(struct efx_nic *efx, int num_vfs)
384{
385	int rc = 0;
386	struct pci_dev *dev = efx->pci_dev;
387
388	efx->vf_count = num_vfs;
389
390	rc = efx_ef10_sriov_alloc_vf_vswitching(efx);
391	if (rc)
392		goto fail1;
393
394	rc = pci_enable_sriov(dev, num_vfs);
395	if (rc)
396		goto fail2;
397
398	return 0;
399fail2:
400	efx_ef10_sriov_free_vf_vswitching(efx);
401fail1:
402	efx->vf_count = 0;
403	netif_err(efx, probe, efx->net_dev,
404		  "Failed to enable SRIOV VFs\n");
405	return rc;
406}
407
408static int efx_ef10_pci_sriov_disable(struct efx_nic *efx, bool force)
409{
410	struct pci_dev *dev = efx->pci_dev;
411	unsigned int vfs_assigned = 0;
412
413	vfs_assigned = pci_vfs_assigned(dev);
414
415	if (vfs_assigned && !force) {
416		netif_info(efx, drv, efx->net_dev, "VFs are assigned to guests; "
417			   "please detach them before disabling SR-IOV\n");
418		return -EBUSY;
419	}
420
421	if (!vfs_assigned)
422		pci_disable_sriov(dev);
423
424	efx_ef10_sriov_free_vf_vswitching(efx);
425	efx->vf_count = 0;
426	return 0;
427}
428
429int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
430{
431	if (num_vfs == 0)
432		return efx_ef10_pci_sriov_disable(efx, false);
433	else
434		return efx_ef10_pci_sriov_enable(efx, num_vfs);
435}
436
437int efx_ef10_sriov_init(struct efx_nic *efx)
438{
439	return 0;
440}
441
442void efx_ef10_sriov_fini(struct efx_nic *efx)
443{
444	struct efx_ef10_nic_data *nic_data = efx->nic_data;
445	unsigned int i;
446	int rc;
447
448	if (!nic_data->vf) {
449		/* Remove any un-assigned orphaned VFs */
450		if (pci_num_vf(efx->pci_dev) && !pci_vfs_assigned(efx->pci_dev))
451			pci_disable_sriov(efx->pci_dev);
452		return;
453	}
454
455	/* Remove any VFs in the host */
456	for (i = 0; i < efx->vf_count; ++i) {
457		struct efx_nic *vf_efx = nic_data->vf[i].efx;
458
459		if (vf_efx)
460			vf_efx->pci_dev->driver->remove(vf_efx->pci_dev);
461	}
462
463	rc = efx_ef10_pci_sriov_disable(efx, true);
464	if (rc)
465		netif_dbg(efx, drv, efx->net_dev,
466			  "Disabling SRIOV was not successful rc=%d\n", rc);
467	else
468		netif_dbg(efx, drv, efx->net_dev, "SRIOV disabled\n");
469}
470
471static int efx_ef10_vport_del_vf_mac(struct efx_nic *efx, unsigned int port_id,
472				     u8 *mac)
473{
474	MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
475	MCDI_DECLARE_BUF_ERR(outbuf);
476	size_t outlen;
477	int rc;
478
479	MCDI_SET_DWORD(inbuf, VPORT_DEL_MAC_ADDRESS_IN_VPORT_ID, port_id);
480	ether_addr_copy(MCDI_PTR(inbuf, VPORT_DEL_MAC_ADDRESS_IN_MACADDR), mac);
481
482	rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
483			  sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
484
485	return rc;
486}
487
488int efx_ef10_sriov_set_vf_mac(struct efx_nic *efx, int vf_i, u8 *mac)
489{
490	struct efx_ef10_nic_data *nic_data = efx->nic_data;
491	struct ef10_vf *vf;
492	int rc;
493
494	if (!nic_data->vf)
495		return -EOPNOTSUPP;
496
497	if (vf_i >= efx->vf_count)
498		return -EINVAL;
499	vf = nic_data->vf + vf_i;
500
501	if (vf->efx) {
502		efx_device_detach_sync(vf->efx);
503		efx_net_stop(vf->efx->net_dev);
504
505		down_write(&vf->efx->filter_sem);
506		vf->efx->type->filter_table_remove(vf->efx);
507
508		rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
509		if (rc) {
510			up_write(&vf->efx->filter_sem);
511			return rc;
512		}
513	}
514
515	rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
516	if (rc)
517		return rc;
518
519	if (!is_zero_ether_addr(vf->mac)) {
520		rc = efx_ef10_vport_del_vf_mac(efx, vf->vport_id, vf->mac);
521		if (rc)
522			return rc;
523	}
524
525	if (!is_zero_ether_addr(mac)) {
526		rc = efx_ef10_vport_add_mac(efx, vf->vport_id, mac);
527		if (rc) {
528			eth_zero_addr(vf->mac);
529			goto fail;
530		}
531		if (vf->efx)
532			ether_addr_copy(vf->efx->net_dev->dev_addr, mac);
533	}
534
535	ether_addr_copy(vf->mac, mac);
536
537	rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
538	if (rc)
539		goto fail;
540
541	if (vf->efx) {
542		/* VF cannot use the vport_id that the PF created */
543		rc = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
544		if (rc) {
545			up_write(&vf->efx->filter_sem);
546			return rc;
547		}
548		vf->efx->type->filter_table_probe(vf->efx);
549		up_write(&vf->efx->filter_sem);
550		efx_net_open(vf->efx->net_dev);
551		netif_device_attach(vf->efx->net_dev);
552	}
553
554	return 0;
555
556fail:
557	memset(vf->mac, 0, ETH_ALEN);
558	return rc;
559}
560
561int efx_ef10_sriov_set_vf_vlan(struct efx_nic *efx, int vf_i, u16 vlan,
562			       u8 qos)
563{
564	struct efx_ef10_nic_data *nic_data = efx->nic_data;
565	struct ef10_vf *vf;
566	u16 old_vlan, new_vlan;
567	int rc = 0, rc2 = 0;
568
569	if (vf_i >= efx->vf_count)
570		return -EINVAL;
571	if (qos != 0)
572		return -EINVAL;
573
574	vf = nic_data->vf + vf_i;
575
576	new_vlan = (vlan == 0) ? EFX_EF10_NO_VLAN : vlan;
577	if (new_vlan == vf->vlan)
578		return 0;
579
580	if (vf->efx) {
581		efx_device_detach_sync(vf->efx);
582		efx_net_stop(vf->efx->net_dev);
583
584		mutex_lock(&vf->efx->mac_lock);
585		down_write(&vf->efx->filter_sem);
586		vf->efx->type->filter_table_remove(vf->efx);
587
588		rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
589		if (rc)
590			goto restore_filters;
591	}
592
593	if (vf->vport_assigned) {
594		rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
595		if (rc) {
596			netif_warn(efx, drv, efx->net_dev,
597				   "Failed to change vlan on VF %d.\n", vf_i);
598			netif_warn(efx, drv, efx->net_dev,
599				   "This is likely because the VF is bound to a driver in a VM.\n");
600			netif_warn(efx, drv, efx->net_dev,
601				   "Please unload the driver in the VM.\n");
602			goto restore_vadaptor;
603		}
604		vf->vport_assigned = 0;
605	}
606
607	if (!is_zero_ether_addr(vf->mac)) {
608		rc = efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
609		if (rc)
610			goto restore_evb_port;
611	}
612
613	if (vf->vport_id) {
614		rc = efx_ef10_vport_free(efx, vf->vport_id);
615		if (rc)
616			goto restore_mac;
617		vf->vport_id = 0;
618	}
619
620	/* Do the actual vlan change */
621	old_vlan = vf->vlan;
622	vf->vlan = new_vlan;
623
624	/* Restore everything in reverse order */
625	rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
626				  MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
627				  vf->vlan, &vf->vport_id);
628	if (rc)
629		goto reset_nic_up_write;
630
631restore_mac:
632	if (!is_zero_ether_addr(vf->mac)) {
633		rc2 = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
634		if (rc2) {
635			eth_zero_addr(vf->mac);
636			goto reset_nic_up_write;
637		}
638	}
639
640restore_evb_port:
641	rc2 = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
642	if (rc2)
643		goto reset_nic_up_write;
644	else
645		vf->vport_assigned = 1;
646
647restore_vadaptor:
648	if (vf->efx) {
649		rc2 = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
650		if (rc2)
651			goto reset_nic_up_write;
652	}
653
654restore_filters:
655	if (vf->efx) {
656		rc2 = vf->efx->type->filter_table_probe(vf->efx);
657		if (rc2)
658			goto reset_nic_up_write;
659
660		up_write(&vf->efx->filter_sem);
661		mutex_unlock(&vf->efx->mac_lock);
662
663		up_write(&vf->efx->filter_sem);
664
665		rc2 = efx_net_open(vf->efx->net_dev);
666		if (rc2)
667			goto reset_nic;
668
669		netif_device_attach(vf->efx->net_dev);
670	}
671	return rc;
672
673reset_nic_up_write:
674	if (vf->efx) {
675		up_write(&vf->efx->filter_sem);
676		mutex_unlock(&vf->efx->mac_lock);
677	}
678reset_nic:
679	if (vf->efx) {
680		netif_err(efx, drv, efx->net_dev,
681			  "Failed to restore VF - scheduling reset.\n");
682		efx_schedule_reset(vf->efx, RESET_TYPE_DATAPATH);
683	} else {
684		netif_err(efx, drv, efx->net_dev,
685			  "Failed to restore the VF and cannot reset the VF "
686			  "- VF is not functional.\n");
687		netif_err(efx, drv, efx->net_dev,
688			  "Please reload the driver attached to the VF.\n");
689	}
690
691	return rc ? rc : rc2;
692}
693
694int efx_ef10_sriov_set_vf_spoofchk(struct efx_nic *efx, int vf_i,
695				   bool spoofchk)
696{
697	return spoofchk ? -EOPNOTSUPP : 0;
698}
699
700int efx_ef10_sriov_set_vf_link_state(struct efx_nic *efx, int vf_i,
701				     int link_state)
702{
703	MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
704	struct efx_ef10_nic_data *nic_data = efx->nic_data;
705
706	BUILD_BUG_ON(IFLA_VF_LINK_STATE_AUTO !=
707		     MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_AUTO);
708	BUILD_BUG_ON(IFLA_VF_LINK_STATE_ENABLE !=
709		     MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_UP);
710	BUILD_BUG_ON(IFLA_VF_LINK_STATE_DISABLE !=
711		     MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_DOWN);
712	MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
713			      LINK_STATE_MODE_IN_FUNCTION_PF,
714			      nic_data->pf_index,
715			      LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
716	MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE, link_state);
717	return efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
718			    NULL, 0, NULL); /* don't care what old mode was */
719}
720
721int efx_ef10_sriov_get_vf_config(struct efx_nic *efx, int vf_i,
722				 struct ifla_vf_info *ivf)
723{
724	MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
725	MCDI_DECLARE_BUF(outbuf, MC_CMD_LINK_STATE_MODE_OUT_LEN);
726
727	struct efx_ef10_nic_data *nic_data = efx->nic_data;
728	struct ef10_vf *vf;
729	size_t outlen;
730	int rc;
731
732	if (vf_i >= efx->vf_count)
733		return -EINVAL;
734
735	if (!nic_data->vf)
736		return -EOPNOTSUPP;
737
738	vf = nic_data->vf + vf_i;
739
740	ivf->vf = vf_i;
741	ivf->min_tx_rate = 0;
742	ivf->max_tx_rate = 0;
743	ether_addr_copy(ivf->mac, vf->mac);
744	ivf->vlan = (vf->vlan == EFX_EF10_NO_VLAN) ? 0 : vf->vlan;
745	ivf->qos = 0;
746
747	MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
748			      LINK_STATE_MODE_IN_FUNCTION_PF,
749			      nic_data->pf_index,
750			      LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
751	MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE,
752		       MC_CMD_LINK_STATE_MODE_IN_DO_NOT_CHANGE);
753	rc = efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
754			  outbuf, sizeof(outbuf), &outlen);
755	if (rc)
756		return rc;
757	if (outlen < MC_CMD_LINK_STATE_MODE_OUT_LEN)
758		return -EIO;
759	ivf->linkstate = MCDI_DWORD(outbuf, LINK_STATE_MODE_OUT_OLD_MODE);
760
761	return 0;
762}
763
764int efx_ef10_sriov_get_phys_port_id(struct efx_nic *efx,
765				    struct netdev_phys_item_id *ppid)
766{
767	struct efx_ef10_nic_data *nic_data = efx->nic_data;
768
769	if (!is_valid_ether_addr(nic_data->port_id))
770		return -EOPNOTSUPP;
771
772	ppid->id_len = ETH_ALEN;
773	memcpy(ppid->id, nic_data->port_id, ppid->id_len);
774
775	return 0;
776}