net/mlx5: Add CONFIG_MLX5_ESWITCH Kconfig
[platform/kernel/linux-rpi.git] / drivers / net / ethernet / mellanox / mlx5 / core / sriov.c
1 /*
2  * Copyright (c) 2014, Mellanox Technologies inc.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <linux/pci.h>
34 #include <linux/mlx5/driver.h>
35 #include "mlx5_core.h"
36 #include "eswitch.h"
37
38 bool mlx5_sriov_is_enabled(struct mlx5_core_dev *dev)
39 {
40         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
41
42         return !!sriov->num_vfs;
43 }
44
45 static int mlx5_device_enable_sriov(struct mlx5_core_dev *dev, int num_vfs)
46 {
47         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
48         int err;
49         int vf;
50
51         if (sriov->enabled_vfs) {
52                 mlx5_core_warn(dev,
53                                "failed to enable SRIOV on device, already enabled with %d vfs\n",
54                                sriov->enabled_vfs);
55                 return -EBUSY;
56         }
57
58         err = mlx5_eswitch_enable_sriov(dev->priv.eswitch, num_vfs, SRIOV_LEGACY);
59         if (err) {
60                 mlx5_core_warn(dev,
61                                "failed to enable eswitch SRIOV (%d)\n", err);
62                 return err;
63         }
64
65         for (vf = 0; vf < num_vfs; vf++) {
66                 err = mlx5_core_enable_hca(dev, vf + 1);
67                 if (err) {
68                         mlx5_core_warn(dev, "failed to enable VF %d (%d)\n", vf, err);
69                         continue;
70                 }
71                 sriov->vfs_ctx[vf].enabled = 1;
72                 sriov->enabled_vfs++;
73                 mlx5_core_dbg(dev, "successfully enabled VF* %d\n", vf);
74
75         }
76
77         return 0;
78 }
79
80 static void mlx5_device_disable_sriov(struct mlx5_core_dev *dev)
81 {
82         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
83         int err;
84         int vf;
85
86         if (!sriov->enabled_vfs)
87                 goto out;
88
89         for (vf = 0; vf < sriov->num_vfs; vf++) {
90                 if (!sriov->vfs_ctx[vf].enabled)
91                         continue;
92                 err = mlx5_core_disable_hca(dev, vf + 1);
93                 if (err) {
94                         mlx5_core_warn(dev, "failed to disable VF %d\n", vf);
95                         continue;
96                 }
97                 sriov->vfs_ctx[vf].enabled = 0;
98                 sriov->enabled_vfs--;
99         }
100
101 out:
102         mlx5_eswitch_disable_sriov(dev->priv.eswitch);
103
104         if (mlx5_wait_for_vf_pages(dev))
105                 mlx5_core_warn(dev, "timeout reclaiming VFs pages\n");
106 }
107
108 static int mlx5_pci_enable_sriov(struct pci_dev *pdev, int num_vfs)
109 {
110         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
111         int err = 0;
112
113         if (pci_num_vf(pdev)) {
114                 mlx5_core_warn(dev, "Unable to enable pci sriov, already enabled\n");
115                 return -EBUSY;
116         }
117
118         err = pci_enable_sriov(pdev, num_vfs);
119         if (err)
120                 mlx5_core_warn(dev, "pci_enable_sriov failed : %d\n", err);
121
122         return err;
123 }
124
125 static void mlx5_pci_disable_sriov(struct pci_dev *pdev)
126 {
127         pci_disable_sriov(pdev);
128 }
129
130 static int mlx5_sriov_enable(struct pci_dev *pdev, int num_vfs)
131 {
132         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
133         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
134         int err = 0;
135
136         err = mlx5_device_enable_sriov(dev, num_vfs);
137         if (err) {
138                 mlx5_core_warn(dev, "mlx5_device_enable_sriov failed : %d\n", err);
139                 return err;
140         }
141
142         err = mlx5_pci_enable_sriov(pdev, num_vfs);
143         if (err) {
144                 mlx5_core_warn(dev, "mlx5_pci_enable_sriov failed : %d\n", err);
145                 mlx5_device_disable_sriov(dev);
146                 return err;
147         }
148
149         sriov->num_vfs = num_vfs;
150
151         return 0;
152 }
153
154 static void mlx5_sriov_disable(struct pci_dev *pdev)
155 {
156         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
157         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
158
159         mlx5_pci_disable_sriov(pdev);
160         mlx5_device_disable_sriov(dev);
161         sriov->num_vfs = 0;
162 }
163
164 int mlx5_core_sriov_configure(struct pci_dev *pdev, int num_vfs)
165 {
166         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
167         int err = 0;
168
169         mlx5_core_dbg(dev, "requested num_vfs %d\n", num_vfs);
170         if (!mlx5_core_is_pf(dev))
171                 return -EPERM;
172
173         if (num_vfs) {
174                 int ret;
175
176                 ret = mlx5_lag_forbid(dev);
177                 if (ret && (ret != -ENODEV))
178                         return ret;
179         }
180
181         if (num_vfs) {
182                 err = mlx5_sriov_enable(pdev, num_vfs);
183         } else {
184                 mlx5_sriov_disable(pdev);
185                 mlx5_lag_allow(dev);
186         }
187
188         return err ? err : num_vfs;
189 }
190
191 int mlx5_sriov_attach(struct mlx5_core_dev *dev)
192 {
193         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
194
195         if (!mlx5_core_is_pf(dev) || !sriov->num_vfs)
196                 return 0;
197
198         /* If sriov VFs exist in PCI level, enable them in device level */
199         return mlx5_device_enable_sriov(dev, sriov->num_vfs);
200 }
201
202 void mlx5_sriov_detach(struct mlx5_core_dev *dev)
203 {
204         if (!mlx5_core_is_pf(dev))
205                 return;
206
207         mlx5_device_disable_sriov(dev);
208 }
209
210 int mlx5_sriov_init(struct mlx5_core_dev *dev)
211 {
212         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
213         struct pci_dev *pdev = dev->pdev;
214         int total_vfs;
215
216         if (!mlx5_core_is_pf(dev))
217                 return 0;
218
219         total_vfs = pci_sriov_get_totalvfs(pdev);
220         sriov->num_vfs = pci_num_vf(pdev);
221         sriov->vfs_ctx = kcalloc(total_vfs, sizeof(*sriov->vfs_ctx), GFP_KERNEL);
222         if (!sriov->vfs_ctx)
223                 return -ENOMEM;
224
225         return 0;
226 }
227
228 void mlx5_sriov_cleanup(struct mlx5_core_dev *dev)
229 {
230         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
231
232         if (!mlx5_core_is_pf(dev))
233                 return;
234
235         kfree(sriov->vfs_ctx);
236 }