net/mlx5: LAG and SRIOV cannot be used together
[cascardo/linux.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 #ifdef CONFIG_MLX5_CORE_EN
37 #include "eswitch.h"
38 #endif
39
40 bool mlx5_sriov_is_enabled(struct mlx5_core_dev *dev)
41 {
42         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
43
44         return !!sriov->num_vfs;
45 }
46
47 static void enable_vfs(struct mlx5_core_dev *dev, int num_vfs)
48 {
49         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
50         int err;
51         int vf;
52
53         for (vf = 1; vf <= num_vfs; vf++) {
54                 err = mlx5_core_enable_hca(dev, vf);
55                 if (err) {
56                         mlx5_core_warn(dev, "failed to enable VF %d\n", vf - 1);
57                 } else {
58                         sriov->vfs_ctx[vf - 1].enabled = 1;
59                         mlx5_core_dbg(dev, "successfully enabled VF %d\n", vf - 1);
60                 }
61         }
62 }
63
64 static void disable_vfs(struct mlx5_core_dev *dev, int num_vfs)
65 {
66         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
67         int vf;
68
69         for (vf = 1; vf <= num_vfs; vf++) {
70                 if (sriov->vfs_ctx[vf - 1].enabled) {
71                         if (mlx5_core_disable_hca(dev, vf))
72                                 mlx5_core_warn(dev, "failed to disable VF %d\n", vf - 1);
73                         else
74                                 sriov->vfs_ctx[vf - 1].enabled = 0;
75                 }
76         }
77 }
78
79 static int mlx5_core_create_vfs(struct pci_dev *pdev, int num_vfs)
80 {
81         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
82         int err;
83
84         if (pci_num_vf(pdev))
85                 pci_disable_sriov(pdev);
86
87         enable_vfs(dev, num_vfs);
88
89         err = pci_enable_sriov(pdev, num_vfs);
90         if (err) {
91                 dev_warn(&pdev->dev, "enable sriov failed %d\n", err);
92                 goto ex;
93         }
94
95         return 0;
96
97 ex:
98         disable_vfs(dev, num_vfs);
99         return err;
100 }
101
102 static int mlx5_core_sriov_enable(struct pci_dev *pdev, int num_vfs)
103 {
104         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
105         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
106         int err;
107
108         kfree(sriov->vfs_ctx);
109         sriov->vfs_ctx = kcalloc(num_vfs, sizeof(*sriov->vfs_ctx), GFP_ATOMIC);
110         if (!sriov->vfs_ctx)
111                 return -ENOMEM;
112
113         sriov->enabled_vfs = num_vfs;
114         err = mlx5_core_create_vfs(pdev, num_vfs);
115         if (err) {
116                 kfree(sriov->vfs_ctx);
117                 sriov->vfs_ctx = NULL;
118                 return err;
119         }
120
121         return 0;
122 }
123
124 static void mlx5_core_init_vfs(struct mlx5_core_dev *dev, int num_vfs)
125 {
126         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
127
128         sriov->num_vfs = num_vfs;
129 }
130
131 static void mlx5_core_cleanup_vfs(struct mlx5_core_dev *dev)
132 {
133         struct mlx5_core_sriov *sriov;
134
135         sriov = &dev->priv.sriov;
136         disable_vfs(dev, sriov->num_vfs);
137
138         if (mlx5_wait_for_vf_pages(dev))
139                 mlx5_core_warn(dev, "timeout claiming VFs pages\n");
140
141         sriov->num_vfs = 0;
142 }
143
144 int mlx5_core_sriov_configure(struct pci_dev *pdev, int num_vfs)
145 {
146         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
147         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
148         int err;
149
150         mlx5_core_dbg(dev, "requested num_vfs %d\n", num_vfs);
151         if (!mlx5_core_is_pf(dev))
152                 return -EPERM;
153
154         if (num_vfs && mlx5_lag_is_active(dev)) {
155                 mlx5_core_warn(dev, "can't turn sriov on while LAG is active");
156                 return -EINVAL;
157         }
158
159         mlx5_core_cleanup_vfs(dev);
160
161         if (!num_vfs) {
162 #ifdef CONFIG_MLX5_CORE_EN
163                 mlx5_eswitch_disable_sriov(dev->priv.eswitch);
164 #endif
165                 kfree(sriov->vfs_ctx);
166                 sriov->vfs_ctx = NULL;
167                 if (!pci_vfs_assigned(pdev))
168                         pci_disable_sriov(pdev);
169                 else
170                         mlx5_core_info(dev, "unloading PF driver while leaving orphan VFs\n");
171                 return 0;
172         }
173
174         err = mlx5_core_sriov_enable(pdev, num_vfs);
175         if (err) {
176                 mlx5_core_warn(dev, "mlx5_core_sriov_enable failed %d\n", err);
177                 return err;
178         }
179
180         mlx5_core_init_vfs(dev, num_vfs);
181 #ifdef CONFIG_MLX5_CORE_EN
182         mlx5_eswitch_enable_sriov(dev->priv.eswitch, num_vfs, SRIOV_LEGACY);
183 #endif
184
185         return num_vfs;
186 }
187
188 static int sync_required(struct pci_dev *pdev)
189 {
190         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
191         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
192         int cur_vfs = pci_num_vf(pdev);
193
194         if (cur_vfs != sriov->num_vfs) {
195                 mlx5_core_warn(dev, "current VFs %d, registered %d - sync needed\n",
196                                cur_vfs, sriov->num_vfs);
197                 return 1;
198         }
199
200         return 0;
201 }
202
203 int mlx5_sriov_init(struct mlx5_core_dev *dev)
204 {
205         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
206         struct pci_dev *pdev = dev->pdev;
207         int cur_vfs;
208
209         if (!mlx5_core_is_pf(dev))
210                 return 0;
211
212         if (!sync_required(dev->pdev))
213                 return 0;
214
215         cur_vfs = pci_num_vf(pdev);
216         sriov->vfs_ctx = kcalloc(cur_vfs, sizeof(*sriov->vfs_ctx), GFP_KERNEL);
217         if (!sriov->vfs_ctx)
218                 return -ENOMEM;
219
220         sriov->enabled_vfs = cur_vfs;
221
222         mlx5_core_init_vfs(dev, cur_vfs);
223 #ifdef CONFIG_MLX5_CORE_EN
224         if (cur_vfs)
225                 mlx5_eswitch_enable_sriov(dev->priv.eswitch, cur_vfs,
226                                           SRIOV_LEGACY);
227 #endif
228
229         enable_vfs(dev, cur_vfs);
230
231         return 0;
232 }
233
234 int mlx5_sriov_cleanup(struct mlx5_core_dev *dev)
235 {
236         struct pci_dev *pdev = dev->pdev;
237         int err;
238
239         if (!mlx5_core_is_pf(dev))
240                 return 0;
241
242         err = mlx5_core_sriov_configure(pdev, 0);
243         if (err)
244                 return err;
245
246         return 0;
247 }