blob: e08627785590267452f279ab5dd1880adf31fa4b [file] [log] [blame]
Eli Cohenfc50db92015-12-01 18:03:09 +02001/*
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"
Saeed Mahameed81848732015-12-01 18:03:20 +020036#ifdef CONFIG_MLX5_CORE_EN
37#include "eswitch.h"
38#endif
Eli Cohenfc50db92015-12-01 18:03:09 +020039
Aviv Helleredb31b12016-04-17 19:32:13 +030040bool 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
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +030047static int mlx5_device_enable_sriov(struct mlx5_core_dev *dev, int num_vfs)
Eli Cohenfc50db92015-12-01 18:03:09 +020048{
49 struct mlx5_core_sriov *sriov = &dev->priv.sriov;
50 int err;
51 int vf;
52
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +030053 if (sriov->enabled_vfs) {
54 mlx5_core_warn(dev,
55 "failed to enable SRIOV on device, already enabled with %d vfs\n",
56 sriov->enabled_vfs);
57 return -EBUSY;
Eli Cohenfc50db92015-12-01 18:03:09 +020058 }
Eli Cohenfc50db92015-12-01 18:03:09 +020059
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +030060#ifdef CONFIG_MLX5_CORE_EN
61 err = mlx5_eswitch_enable_sriov(dev->priv.eswitch, num_vfs, SRIOV_LEGACY);
Eli Cohenfc50db92015-12-01 18:03:09 +020062 if (err) {
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +030063 mlx5_core_warn(dev,
64 "failed to enable eswitch SRIOV (%d)\n", err);
65 return err;
66 }
67#endif
68
69 for (vf = 0; vf < num_vfs; vf++) {
70 err = mlx5_core_enable_hca(dev, vf + 1);
71 if (err) {
72 mlx5_core_warn(dev, "failed to enable VF %d (%d)\n", vf, err);
73 continue;
74 }
75 sriov->vfs_ctx[vf].enabled = 1;
76 sriov->enabled_vfs++;
77 mlx5_core_dbg(dev, "successfully enabled VF* %d\n", vf);
78
Eli Cohenfc50db92015-12-01 18:03:09 +020079 }
80
81 return 0;
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +030082}
Eli Cohenfc50db92015-12-01 18:03:09 +020083
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +030084static void mlx5_device_disable_sriov(struct mlx5_core_dev *dev)
85{
86 struct mlx5_core_sriov *sriov = &dev->priv.sriov;
87 int err;
88 int vf;
89
90 if (!sriov->enabled_vfs)
91 return;
92
93 for (vf = 0; vf < sriov->num_vfs; vf++) {
94 if (!sriov->vfs_ctx[vf].enabled)
95 continue;
96 err = mlx5_core_disable_hca(dev, vf + 1);
97 if (err) {
98 mlx5_core_warn(dev, "failed to disable VF %d\n", vf);
99 continue;
100 }
101 sriov->vfs_ctx[vf].enabled = 0;
102 sriov->enabled_vfs--;
103 }
104
105#ifdef CONFIG_MLX5_CORE_EN
106 mlx5_eswitch_disable_sriov(dev->priv.eswitch);
107#endif
108
109 if (mlx5_wait_for_vf_pages(dev))
110 mlx5_core_warn(dev, "timeout reclaiming VFs pages\n");
111}
112
113static int mlx5_pci_enable_sriov(struct pci_dev *pdev, int num_vfs)
114{
115 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
116 int err = 0;
117
118 if (pci_num_vf(pdev)) {
119 mlx5_core_warn(dev, "Unable to enable pci sriov, already enabled\n");
120 return -EBUSY;
121 }
122
123 err = pci_enable_sriov(pdev, num_vfs);
124 if (err)
125 mlx5_core_warn(dev, "pci_enable_sriov failed : %d\n", err);
126
Eli Cohenfc50db92015-12-01 18:03:09 +0200127 return err;
128}
129
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +0300130static void mlx5_pci_disable_sriov(struct pci_dev *pdev)
131{
132 pci_disable_sriov(pdev);
133}
134
135static int mlx5_sriov_enable(struct pci_dev *pdev, int num_vfs)
Eli Cohenfc50db92015-12-01 18:03:09 +0200136{
137 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
138 struct mlx5_core_sriov *sriov = &dev->priv.sriov;
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +0300139 int err = 0;
Eli Cohenfc50db92015-12-01 18:03:09 +0200140
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +0300141 err = mlx5_device_enable_sriov(dev, num_vfs);
Eli Cohenfc50db92015-12-01 18:03:09 +0200142 if (err) {
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +0300143 mlx5_core_warn(dev, "mlx5_device_enable_sriov failed : %d\n", err);
Eli Cohenfc50db92015-12-01 18:03:09 +0200144 return err;
145 }
146
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +0300147 err = mlx5_pci_enable_sriov(pdev, num_vfs);
148 if (err) {
149 mlx5_core_warn(dev, "mlx5_pci_enable_sriov failed : %d\n", err);
150 mlx5_device_disable_sriov(dev);
151 return err;
152 }
153
154 sriov->num_vfs = num_vfs;
155
Eli Cohenfc50db92015-12-01 18:03:09 +0200156 return 0;
157}
158
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +0300159static void mlx5_sriov_disable(struct pci_dev *pdev)
Eli Cohenfc50db92015-12-01 18:03:09 +0200160{
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +0300161 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
Eli Cohenfc50db92015-12-01 18:03:09 +0200162 struct mlx5_core_sriov *sriov = &dev->priv.sriov;
163
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +0300164 mlx5_pci_disable_sriov(pdev);
165 mlx5_device_disable_sriov(dev);
Eli Cohenfc50db92015-12-01 18:03:09 +0200166 sriov->num_vfs = 0;
167}
168
169int mlx5_core_sriov_configure(struct pci_dev *pdev, int num_vfs)
170{
171 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +0300172 int err = 0;
Eli Cohenfc50db92015-12-01 18:03:09 +0200173
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900174 mlx5_core_dbg(dev, "requested num_vfs %d\n", num_vfs);
Eli Cohenfc50db92015-12-01 18:03:09 +0200175 if (!mlx5_core_is_pf(dev))
176 return -EPERM;
177
Aviv Helleredb31b12016-04-17 19:32:13 +0300178 if (num_vfs && mlx5_lag_is_active(dev)) {
179 mlx5_core_warn(dev, "can't turn sriov on while LAG is active");
180 return -EINVAL;
181 }
182
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +0300183 if (num_vfs)
184 err = mlx5_sriov_enable(pdev, num_vfs);
185 else
186 mlx5_sriov_disable(pdev);
Eli Cohenfc50db92015-12-01 18:03:09 +0200187
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +0300188 return err ? err : num_vfs;
Eli Cohenfc50db92015-12-01 18:03:09 +0200189}
190
Mohamad Haj Yahiaacab7212016-09-09 17:35:21 +0300191int 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
202void 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
Eli Cohenfc50db92015-12-01 18:03:09 +0200210int 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;
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +0300214 int total_vfs;
Eli Cohenfc50db92015-12-01 18:03:09 +0200215
216 if (!mlx5_core_is_pf(dev))
217 return 0;
218
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +0300219 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);
Eli Cohenfc50db92015-12-01 18:03:09 +0200222 if (!sriov->vfs_ctx)
223 return -ENOMEM;
224
Mohamad Haj Yahiac2d6e312016-09-09 17:35:23 +0300225 return 0;
Eli Cohenfc50db92015-12-01 18:03:09 +0200226}
227
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +0300228void mlx5_sriov_cleanup(struct mlx5_core_dev *dev)
Eli Cohenfc50db92015-12-01 18:03:09 +0200229{
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +0300230 struct mlx5_core_sriov *sriov = &dev->priv.sriov;
Eli Cohenfc50db92015-12-01 18:03:09 +0200231
232 if (!mlx5_core_is_pf(dev))
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +0300233 return;
Mohamad Haj Yahiac2d6e312016-09-09 17:35:23 +0300234
Mohamad Haj Yahia6b6adee2016-09-09 17:35:18 +0300235 kfree(sriov->vfs_ctx);
Eli Cohenfc50db92015-12-01 18:03:09 +0200236}