blob: 2aba7b7234eae8a50eb1010cb18abc92d44f169b [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
17static int efx_ef10_pci_sriov_enable(struct efx_nic *efx, int num_vfs)
18{
19 int rc = 0;
20 struct pci_dev *dev = efx->pci_dev;
21
22 efx->vf_count = num_vfs;
23 rc = pci_enable_sriov(dev, num_vfs);
24 if (rc) {
25 efx->vf_count = 0;
26 netif_err(efx, probe, efx->net_dev,
27 "Failed to enable SRIOV VFs\n");
28 }
29 return rc;
30}
31
32static int efx_ef10_pci_sriov_disable(struct efx_nic *efx)
33{
34 struct pci_dev *dev = efx->pci_dev;
35
36 efx->vf_count = 0;
37 pci_disable_sriov(dev);
38 return 0;
39}
40
41int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
42{
43 if (num_vfs == 0)
44 return efx_ef10_pci_sriov_disable(efx);
45 else
46 return efx_ef10_pci_sriov_enable(efx, num_vfs);
47}
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +010048
49static int efx_ef10_vswitch_alloc(struct efx_nic *efx, unsigned int port_id,
50 unsigned int vswitch_type)
51{
52 MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_ALLOC_IN_LEN);
53
54 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
55 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_TYPE, vswitch_type);
56 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 0);
57 MCDI_POPULATE_DWORD_1(inbuf, VSWITCH_ALLOC_IN_FLAGS,
58 VSWITCH_ALLOC_IN_FLAG_AUTO_PORT, 0);
59
60 return efx_mcdi_rpc(efx, MC_CMD_VSWITCH_ALLOC, inbuf, sizeof(inbuf),
61 NULL, 0, NULL);
62}
63
64static int efx_ef10_vswitch_free(struct efx_nic *efx, unsigned int port_id)
65{
66 MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_FREE_IN_LEN);
67
68 MCDI_SET_DWORD(inbuf, VSWITCH_FREE_IN_UPSTREAM_PORT_ID, port_id);
69
70 return efx_mcdi_rpc(efx, MC_CMD_VSWITCH_FREE, inbuf, sizeof(inbuf),
71 NULL, 0, NULL);
72}
73
74static int efx_ef10_vport_alloc(struct efx_nic *efx,
75 unsigned int port_id_in,
76 unsigned int vport_type,
77 unsigned int *port_id_out)
78{
79 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_ALLOC_IN_LEN);
80 MCDI_DECLARE_BUF(outbuf, MC_CMD_VPORT_ALLOC_OUT_LEN);
81 size_t outlen;
82 int rc;
83
84 EFX_WARN_ON_PARANOID(!port_id_out);
85
86 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_UPSTREAM_PORT_ID, port_id_in);
87 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_TYPE, vport_type);
88 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_NUM_VLAN_TAGS, 0);
89 MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_FLAGS,
90 VPORT_ALLOC_IN_FLAG_AUTO_PORT, 0);
91
92 rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_ALLOC, inbuf, sizeof(inbuf),
93 outbuf, sizeof(outbuf), &outlen);
94 if (rc)
95 return rc;
96 if (outlen < MC_CMD_VPORT_ALLOC_OUT_LEN)
97 return -EIO;
98
99 *port_id_out = MCDI_DWORD(outbuf, VPORT_ALLOC_OUT_VPORT_ID);
100 return 0;
101}
102
103static int efx_ef10_vport_free(struct efx_nic *efx, unsigned int port_id)
104{
105 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_FREE_IN_LEN);
106
107 MCDI_SET_DWORD(inbuf, VPORT_FREE_IN_VPORT_ID, port_id);
108
109 return efx_mcdi_rpc(efx, MC_CMD_VPORT_FREE, inbuf, sizeof(inbuf),
110 NULL, 0, NULL);
111}
112
113/* On top of the default firmware vswitch setup, create a VEB vswitch and
114 * expansion vport for use by this function.
115 */
116int efx_ef10_vswitching_probe(struct efx_nic *efx)
117{
118 struct efx_ef10_nic_data *nic_data = efx->nic_data;
119 int rc;
120
121 if (pci_sriov_get_totalvfs(efx->pci_dev) <= 0)
122 return 0; /* vswitch not needed as we have no VFs */
123
124 rc = efx_ef10_vswitch_alloc(efx, EVB_PORT_ID_ASSIGNED,
125 MC_CMD_VSWITCH_ALLOC_IN_VSWITCH_TYPE_VEB);
126 if (rc)
127 goto fail1;
128
129 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
130 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
131 &nic_data->vport_id);
132 if (rc)
133 goto fail2;
134
135 return 0;
136fail2:
137 efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
138fail1:
139 return rc;
140}
141
142int efx_ef10_vswitching_restore(struct efx_nic *efx)
143{
144 struct efx_ef10_nic_data *nic_data = efx->nic_data;
145 int rc;
146
147 if (!nic_data->must_probe_vswitching)
148 return 0;
149
150 rc = efx_ef10_vswitching_probe(efx);
151
152 if (!rc)
153 nic_data->must_probe_vswitching = false;
154 return rc;
155}
156
157void efx_ef10_vswitching_remove(struct efx_nic *efx)
158{
159 struct efx_ef10_nic_data *nic_data = efx->nic_data;
160
161 if (nic_data->vport_id == EVB_PORT_ID_ASSIGNED)
162 return; /* No vswitch was ever created */
163
164 efx_ef10_vport_free(efx, nic_data->vport_id);
165 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
166
167 efx_ef10_vswitch_free(efx, nic_data->vport_id);
168}