]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/net/ethernet/mellanox/mlx5/core/sriov.c
2 * Copyright (c) 2014, Mellanox Technologies inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/pci.h>
34 #include <linux/mlx5/driver.h>
35 #include "mlx5_core.h"
36 #ifdef CONFIG_MLX5_CORE_EN
40 bool mlx5_sriov_is_enabled(struct mlx5_core_dev
*dev
)
42 struct mlx5_core_sriov
*sriov
= &dev
->priv
.sriov
;
44 return !!sriov
->num_vfs
;
47 static int mlx5_device_enable_sriov(struct mlx5_core_dev
*dev
, int num_vfs
)
49 struct mlx5_core_sriov
*sriov
= &dev
->priv
.sriov
;
53 if (sriov
->enabled_vfs
) {
55 "failed to enable SRIOV on device, already enabled with %d vfs\n",
60 #ifdef CONFIG_MLX5_CORE_EN
61 err
= mlx5_eswitch_enable_sriov(dev
->priv
.eswitch
, num_vfs
, SRIOV_LEGACY
);
64 "failed to enable eswitch SRIOV (%d)\n", err
);
69 for (vf
= 0; vf
< num_vfs
; vf
++) {
70 err
= mlx5_core_enable_hca(dev
, vf
+ 1);
72 mlx5_core_warn(dev
, "failed to enable VF %d (%d)\n", vf
, err
);
75 sriov
->vfs_ctx
[vf
].enabled
= 1;
77 mlx5_core_dbg(dev
, "successfully enabled VF* %d\n", vf
);
84 static void mlx5_device_disable_sriov(struct mlx5_core_dev
*dev
)
86 struct mlx5_core_sriov
*sriov
= &dev
->priv
.sriov
;
90 if (!sriov
->enabled_vfs
)
93 for (vf
= 0; vf
< sriov
->num_vfs
; vf
++) {
94 if (!sriov
->vfs_ctx
[vf
].enabled
)
96 err
= mlx5_core_disable_hca(dev
, vf
+ 1);
98 mlx5_core_warn(dev
, "failed to disable VF %d\n", vf
);
101 sriov
->vfs_ctx
[vf
].enabled
= 0;
102 sriov
->enabled_vfs
--;
105 #ifdef CONFIG_MLX5_CORE_EN
106 mlx5_eswitch_disable_sriov(dev
->priv
.eswitch
);
109 if (mlx5_wait_for_vf_pages(dev
))
110 mlx5_core_warn(dev
, "timeout reclaiming VFs pages\n");
113 static int mlx5_pci_enable_sriov(struct pci_dev
*pdev
, int num_vfs
)
115 struct mlx5_core_dev
*dev
= pci_get_drvdata(pdev
);
118 if (pci_num_vf(pdev
)) {
119 mlx5_core_warn(dev
, "Unable to enable pci sriov, already enabled\n");
123 err
= pci_enable_sriov(pdev
, num_vfs
);
125 mlx5_core_warn(dev
, "pci_enable_sriov failed : %d\n", err
);
130 static void mlx5_pci_disable_sriov(struct pci_dev
*pdev
)
132 pci_disable_sriov(pdev
);
135 static int mlx5_sriov_enable(struct pci_dev
*pdev
, int num_vfs
)
137 struct mlx5_core_dev
*dev
= pci_get_drvdata(pdev
);
138 struct mlx5_core_sriov
*sriov
= &dev
->priv
.sriov
;
141 err
= mlx5_device_enable_sriov(dev
, num_vfs
);
143 mlx5_core_warn(dev
, "mlx5_device_enable_sriov failed : %d\n", err
);
147 err
= mlx5_pci_enable_sriov(pdev
, num_vfs
);
149 mlx5_core_warn(dev
, "mlx5_pci_enable_sriov failed : %d\n", err
);
150 mlx5_device_disable_sriov(dev
);
154 sriov
->num_vfs
= num_vfs
;
159 static void mlx5_sriov_disable(struct pci_dev
*pdev
)
161 struct mlx5_core_dev
*dev
= pci_get_drvdata(pdev
);
162 struct mlx5_core_sriov
*sriov
= &dev
->priv
.sriov
;
164 mlx5_pci_disable_sriov(pdev
);
165 mlx5_device_disable_sriov(dev
);
169 int mlx5_core_sriov_configure(struct pci_dev
*pdev
, int num_vfs
)
171 struct mlx5_core_dev
*dev
= pci_get_drvdata(pdev
);
174 mlx5_core_dbg(dev
, "requested num_vfs %d\n", num_vfs
);
175 if (!mlx5_core_is_pf(dev
))
181 ret
= mlx5_lag_forbid(dev
);
182 if (ret
&& (ret
!= -ENODEV
))
187 err
= mlx5_sriov_enable(pdev
, num_vfs
);
189 mlx5_sriov_disable(pdev
);
193 return err
? err
: num_vfs
;
196 int mlx5_sriov_attach(struct mlx5_core_dev
*dev
)
198 struct mlx5_core_sriov
*sriov
= &dev
->priv
.sriov
;
200 if (!mlx5_core_is_pf(dev
) || !sriov
->num_vfs
)
203 /* If sriov VFs exist in PCI level, enable them in device level */
204 return mlx5_device_enable_sriov(dev
, sriov
->num_vfs
);
207 void mlx5_sriov_detach(struct mlx5_core_dev
*dev
)
209 if (!mlx5_core_is_pf(dev
))
212 mlx5_device_disable_sriov(dev
);
215 int mlx5_sriov_init(struct mlx5_core_dev
*dev
)
217 struct mlx5_core_sriov
*sriov
= &dev
->priv
.sriov
;
218 struct pci_dev
*pdev
= dev
->pdev
;
221 if (!mlx5_core_is_pf(dev
))
224 total_vfs
= pci_sriov_get_totalvfs(pdev
);
225 sriov
->num_vfs
= pci_num_vf(pdev
);
226 sriov
->vfs_ctx
= kcalloc(total_vfs
, sizeof(*sriov
->vfs_ctx
), GFP_KERNEL
);
233 void mlx5_sriov_cleanup(struct mlx5_core_dev
*dev
)
235 struct mlx5_core_sriov
*sriov
= &dev
->priv
.sriov
;
237 if (!mlx5_core_is_pf(dev
))
240 kfree(sriov
->vfs_ctx
);