blob: be74a7065bad27cb321b3a97ec972d11d266b14f [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
Shradha Shah02246a72015-05-06 00:58:14 +010049int efx_ef10_sriov_init(struct efx_nic *efx)
50{
51 return 0;
52}
53
54void efx_ef10_sriov_fini(struct efx_nic *efx)
55{
56 int rc;
57
58 rc = efx_ef10_pci_sriov_disable(efx);
59 if (rc)
60 netif_dbg(efx, drv, efx->net_dev,
61 "Disabling SRIOV was not successful rc=%d\n", rc);
62 else
63 netif_dbg(efx, drv, efx->net_dev, "SRIOV disabled\n");
64}
65
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +010066static int efx_ef10_vswitch_alloc(struct efx_nic *efx, unsigned int port_id,
67 unsigned int vswitch_type)
68{
69 MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_ALLOC_IN_LEN);
70
71 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
72 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_TYPE, vswitch_type);
73 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 0);
74 MCDI_POPULATE_DWORD_1(inbuf, VSWITCH_ALLOC_IN_FLAGS,
75 VSWITCH_ALLOC_IN_FLAG_AUTO_PORT, 0);
76
77 return efx_mcdi_rpc(efx, MC_CMD_VSWITCH_ALLOC, inbuf, sizeof(inbuf),
78 NULL, 0, NULL);
79}
80
81static int efx_ef10_vswitch_free(struct efx_nic *efx, unsigned int port_id)
82{
83 MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_FREE_IN_LEN);
84
85 MCDI_SET_DWORD(inbuf, VSWITCH_FREE_IN_UPSTREAM_PORT_ID, port_id);
86
87 return efx_mcdi_rpc(efx, MC_CMD_VSWITCH_FREE, inbuf, sizeof(inbuf),
88 NULL, 0, NULL);
89}
90
91static int efx_ef10_vport_alloc(struct efx_nic *efx,
92 unsigned int port_id_in,
93 unsigned int vport_type,
94 unsigned int *port_id_out)
95{
96 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_ALLOC_IN_LEN);
97 MCDI_DECLARE_BUF(outbuf, MC_CMD_VPORT_ALLOC_OUT_LEN);
98 size_t outlen;
99 int rc;
100
101 EFX_WARN_ON_PARANOID(!port_id_out);
102
103 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_UPSTREAM_PORT_ID, port_id_in);
104 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_TYPE, vport_type);
105 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_NUM_VLAN_TAGS, 0);
106 MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_FLAGS,
107 VPORT_ALLOC_IN_FLAG_AUTO_PORT, 0);
108
109 rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_ALLOC, inbuf, sizeof(inbuf),
110 outbuf, sizeof(outbuf), &outlen);
111 if (rc)
112 return rc;
113 if (outlen < MC_CMD_VPORT_ALLOC_OUT_LEN)
114 return -EIO;
115
116 *port_id_out = MCDI_DWORD(outbuf, VPORT_ALLOC_OUT_VPORT_ID);
117 return 0;
118}
119
120static int efx_ef10_vport_free(struct efx_nic *efx, unsigned int port_id)
121{
122 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_FREE_IN_LEN);
123
124 MCDI_SET_DWORD(inbuf, VPORT_FREE_IN_VPORT_ID, port_id);
125
126 return efx_mcdi_rpc(efx, MC_CMD_VPORT_FREE, inbuf, sizeof(inbuf),
127 NULL, 0, NULL);
128}
129
130/* On top of the default firmware vswitch setup, create a VEB vswitch and
131 * expansion vport for use by this function.
132 */
133int efx_ef10_vswitching_probe(struct efx_nic *efx)
134{
135 struct efx_ef10_nic_data *nic_data = efx->nic_data;
136 int rc;
137
138 if (pci_sriov_get_totalvfs(efx->pci_dev) <= 0)
139 return 0; /* vswitch not needed as we have no VFs */
140
141 rc = efx_ef10_vswitch_alloc(efx, EVB_PORT_ID_ASSIGNED,
142 MC_CMD_VSWITCH_ALLOC_IN_VSWITCH_TYPE_VEB);
143 if (rc)
144 goto fail1;
145
146 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
147 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
148 &nic_data->vport_id);
149 if (rc)
150 goto fail2;
151
152 return 0;
153fail2:
154 efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
155fail1:
156 return rc;
157}
158
159int efx_ef10_vswitching_restore(struct efx_nic *efx)
160{
161 struct efx_ef10_nic_data *nic_data = efx->nic_data;
162 int rc;
163
164 if (!nic_data->must_probe_vswitching)
165 return 0;
166
167 rc = efx_ef10_vswitching_probe(efx);
168
169 if (!rc)
170 nic_data->must_probe_vswitching = false;
171 return rc;
172}
173
174void efx_ef10_vswitching_remove(struct efx_nic *efx)
175{
176 struct efx_ef10_nic_data *nic_data = efx->nic_data;
177
178 if (nic_data->vport_id == EVB_PORT_ID_ASSIGNED)
179 return; /* No vswitch was ever created */
180
181 efx_ef10_vport_free(efx, nic_data->vport_id);
182 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
183
184 efx_ef10_vswitch_free(efx, nic_data->vport_id);
185}