]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/ethernet/mellanox/mlx5/core/sriov.c
2eecb831c4997adbebe5e8c091e0cba278d5ebab
[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 <linux/mlx5/vport.h>
36 #include "mlx5_core.h"
37 #include "eswitch.h"
38
39 static int sriov_restore_guids(struct mlx5_core_dev *dev, int vf)
40 {
41         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
42         struct mlx5_hca_vport_context *in;
43         int err = 0;
44
45         /* Restore sriov guid and policy settings */
46         if (sriov->vfs_ctx[vf].node_guid ||
47             sriov->vfs_ctx[vf].port_guid ||
48             sriov->vfs_ctx[vf].policy != MLX5_POLICY_INVALID) {
49                 in = kzalloc(sizeof(*in), GFP_KERNEL);
50                 if (!in)
51                         return -ENOMEM;
52
53                 in->node_guid = sriov->vfs_ctx[vf].node_guid;
54                 in->port_guid = sriov->vfs_ctx[vf].port_guid;
55                 in->policy = sriov->vfs_ctx[vf].policy;
56                 in->field_select =
57                         !!(in->port_guid) * MLX5_HCA_VPORT_SEL_PORT_GUID |
58                         !!(in->node_guid) * MLX5_HCA_VPORT_SEL_NODE_GUID |
59                         !!(in->policy) * MLX5_HCA_VPORT_SEL_STATE_POLICY;
60
61                 err = mlx5_core_modify_hca_vport_context(dev, 1, 1, vf + 1, in);
62                 if (err)
63                         mlx5_core_warn(dev, "modify vport context failed, unable to restore VF %d settings\n", vf);
64
65                 kfree(in);
66         }
67
68         return err;
69 }
70
71 static int mlx5_device_enable_sriov(struct mlx5_core_dev *dev, int num_vfs)
72 {
73         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
74         int err;
75         int vf;
76
77         if (sriov->enabled_vfs) {
78                 mlx5_core_warn(dev,
79                                "failed to enable SRIOV on device, already enabled with %d vfs\n",
80                                sriov->enabled_vfs);
81                 return -EBUSY;
82         }
83
84         if (!MLX5_ESWITCH_MANAGER(dev))
85                 goto enable_vfs_hca;
86
87         err = mlx5_eswitch_enable_sriov(dev->priv.eswitch, num_vfs, SRIOV_LEGACY);
88         if (err) {
89                 mlx5_core_warn(dev,
90                                "failed to enable eswitch SRIOV (%d)\n", err);
91                 return err;
92         }
93
94 enable_vfs_hca:
95         for (vf = 0; vf < num_vfs; vf++) {
96                 err = mlx5_core_enable_hca(dev, vf + 1);
97                 if (err) {
98                         mlx5_core_warn(dev, "failed to enable VF %d (%d)\n", vf, err);
99                         continue;
100                 }
101                 sriov->vfs_ctx[vf].enabled = 1;
102                 sriov->enabled_vfs++;
103                 if (MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) {
104                         err = sriov_restore_guids(dev, vf);
105                         if (err) {
106                                 mlx5_core_warn(dev,
107                                                "failed to restore VF %d settings, err %d\n",
108                                                vf, err);
109                                 continue;
110                         }
111                 }
112                 mlx5_core_dbg(dev, "successfully enabled VF* %d\n", vf);
113         }
114
115         return 0;
116 }
117
118 static void mlx5_device_disable_sriov(struct mlx5_core_dev *dev)
119 {
120         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
121         int err;
122         int vf;
123
124         if (!sriov->enabled_vfs)
125                 goto out;
126
127         for (vf = 0; vf < sriov->num_vfs; vf++) {
128                 if (!sriov->vfs_ctx[vf].enabled)
129                         continue;
130                 err = mlx5_core_disable_hca(dev, vf + 1);
131                 if (err) {
132                         mlx5_core_warn(dev, "failed to disable VF %d\n", vf);
133                         continue;
134                 }
135                 sriov->vfs_ctx[vf].enabled = 0;
136                 sriov->enabled_vfs--;
137         }
138
139 out:
140         if (MLX5_ESWITCH_MANAGER(dev))
141                 mlx5_eswitch_disable_sriov(dev->priv.eswitch);
142
143         if (mlx5_wait_for_pages(dev, &dev->priv.vfs_pages))
144                 mlx5_core_warn(dev, "timeout reclaiming VFs pages\n");
145 }
146
147 static int mlx5_sriov_enable(struct pci_dev *pdev, int num_vfs)
148 {
149         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
150         int err;
151
152         err = mlx5_device_enable_sriov(dev, num_vfs);
153         if (err) {
154                 mlx5_core_warn(dev, "mlx5_device_enable_sriov failed : %d\n", err);
155                 return err;
156         }
157
158         err = pci_enable_sriov(pdev, num_vfs);
159         if (err) {
160                 mlx5_core_warn(dev, "pci_enable_sriov failed : %d\n", err);
161                 mlx5_device_disable_sriov(dev);
162         }
163         return err;
164 }
165
166 static void mlx5_sriov_disable(struct pci_dev *pdev)
167 {
168         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
169
170         pci_disable_sriov(pdev);
171         mlx5_device_disable_sriov(dev);
172 }
173
174 int mlx5_core_sriov_configure(struct pci_dev *pdev, int num_vfs)
175 {
176         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
177         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
178         int err = 0;
179
180         mlx5_core_dbg(dev, "requested num_vfs %d\n", num_vfs);
181
182         if (num_vfs)
183                 err = mlx5_sriov_enable(pdev, num_vfs);
184         else
185                 mlx5_sriov_disable(pdev);
186
187         if (!err)
188                 sriov->num_vfs = num_vfs;
189         return err ? err : num_vfs;
190 }
191
192 int mlx5_sriov_attach(struct mlx5_core_dev *dev)
193 {
194         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
195
196         if (!mlx5_core_is_pf(dev) || !sriov->num_vfs)
197                 return 0;
198
199         /* If sriov VFs exist in PCI level, enable them in device level */
200         return mlx5_device_enable_sriov(dev, sriov->num_vfs);
201 }
202
203 void mlx5_sriov_detach(struct mlx5_core_dev *dev)
204 {
205         if (!mlx5_core_is_pf(dev))
206                 return;
207
208         mlx5_device_disable_sriov(dev);
209 }
210
211 static u16 mlx5_get_max_vfs(struct mlx5_core_dev *dev)
212 {
213         u32 out[MLX5_ST_SZ_DW(query_esw_functions_out)] = {};
214         u16 host_total_vfs;
215         int err;
216
217         if (mlx5_core_is_ecpf_esw_manager(dev)) {
218                 err = mlx5_esw_query_functions(dev, out, sizeof(out));
219                 host_total_vfs = MLX5_GET(query_esw_functions_out, out,
220                                           host_params_context.host_total_vfs);
221
222                 /* Old FW doesn't support getting total_vfs from esw func
223                  * but supports getting it from pci_sriov.
224                  */
225                 if (!err && host_total_vfs)
226                         return host_total_vfs;
227         }
228
229         return pci_sriov_get_totalvfs(dev->pdev);
230 }
231
232 int mlx5_sriov_init(struct mlx5_core_dev *dev)
233 {
234         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
235         struct pci_dev *pdev = dev->pdev;
236         int total_vfs;
237
238         if (!mlx5_core_is_pf(dev))
239                 return 0;
240
241         total_vfs = pci_sriov_get_totalvfs(pdev);
242         sriov->max_vfs = mlx5_get_max_vfs(dev);
243         sriov->num_vfs = pci_num_vf(pdev);
244         sriov->vfs_ctx = kcalloc(total_vfs, sizeof(*sriov->vfs_ctx), GFP_KERNEL);
245         if (!sriov->vfs_ctx)
246                 return -ENOMEM;
247
248         return 0;
249 }
250
251 void mlx5_sriov_cleanup(struct mlx5_core_dev *dev)
252 {
253         struct mlx5_core_sriov *sriov = &dev->priv.sriov;
254
255         if (!mlx5_core_is_pf(dev))
256                 return;
257
258         kfree(sriov->vfs_ctx);
259 }