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 <linux/mlx5/vport.h>
36 #include "mlx5_core.h"
39 static int sriov_restore_guids(struct mlx5_core_dev *dev, int vf)
41 struct mlx5_core_sriov *sriov = &dev->priv.sriov;
42 struct mlx5_hca_vport_context *in;
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);
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;
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;
61 err = mlx5_core_modify_hca_vport_context(dev, 1, 1, vf + 1, in);
63 mlx5_core_warn(dev, "modify vport context failed, unable to restore VF %d settings\n", vf);
71 static int mlx5_device_enable_sriov(struct mlx5_core_dev *dev, int num_vfs)
73 struct mlx5_core_sriov *sriov = &dev->priv.sriov;
74 int err, vf, num_msix_count;
76 if (!MLX5_ESWITCH_MANAGER(dev))
79 err = mlx5_eswitch_enable(dev->priv.eswitch, num_vfs);
82 "failed to enable eswitch SRIOV (%d)\n", err);
87 num_msix_count = mlx5_get_default_msix_vec_count(dev, num_vfs);
88 for (vf = 0; vf < num_vfs; vf++) {
89 err = mlx5_core_enable_hca(dev, vf + 1);
91 mlx5_core_warn(dev, "failed to enable VF %d (%d)\n", vf, err);
95 err = mlx5_set_msix_vec_count(dev, vf + 1, num_msix_count);
98 "failed to set MSI-X vector counts VF %d, err %d\n",
103 sriov->vfs_ctx[vf].enabled = 1;
104 if (MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) {
105 err = sriov_restore_guids(dev, vf);
108 "failed to restore VF %d settings, err %d\n",
113 mlx5_core_dbg(dev, "successfully enabled VF* %d\n", vf);
120 mlx5_device_disable_sriov(struct mlx5_core_dev *dev, int num_vfs, bool clear_vf)
122 struct mlx5_core_sriov *sriov = &dev->priv.sriov;
126 for (vf = num_vfs - 1; vf >= 0; vf--) {
127 if (!sriov->vfs_ctx[vf].enabled)
129 err = mlx5_core_disable_hca(dev, vf + 1);
131 mlx5_core_warn(dev, "failed to disable VF %d\n", vf);
134 sriov->vfs_ctx[vf].enabled = 0;
137 if (MLX5_ESWITCH_MANAGER(dev))
138 mlx5_eswitch_disable(dev->priv.eswitch, clear_vf);
140 if (mlx5_wait_for_pages(dev, &dev->priv.vfs_pages))
141 mlx5_core_warn(dev, "timeout reclaiming VFs pages\n");
144 static int mlx5_sriov_enable(struct pci_dev *pdev, int num_vfs)
146 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
149 err = mlx5_device_enable_sriov(dev, num_vfs);
151 mlx5_core_warn(dev, "mlx5_device_enable_sriov failed : %d\n", err);
155 err = pci_enable_sriov(pdev, num_vfs);
157 mlx5_core_warn(dev, "pci_enable_sriov failed : %d\n", err);
158 mlx5_device_disable_sriov(dev, num_vfs, true);
163 static void mlx5_sriov_disable(struct pci_dev *pdev)
165 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
166 int num_vfs = pci_num_vf(dev->pdev);
168 pci_disable_sriov(pdev);
169 mlx5_device_disable_sriov(dev, num_vfs, true);
172 int mlx5_core_sriov_configure(struct pci_dev *pdev, int num_vfs)
174 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
175 struct mlx5_core_sriov *sriov = &dev->priv.sriov;
178 mlx5_core_dbg(dev, "requested num_vfs %d\n", num_vfs);
181 err = mlx5_sriov_enable(pdev, num_vfs);
183 mlx5_sriov_disable(pdev);
186 sriov->num_vfs = num_vfs;
187 return err ? err : num_vfs;
190 int mlx5_core_sriov_set_msix_vec_count(struct pci_dev *vf, int msix_vec_count)
192 struct pci_dev *pf = pci_physfn(vf);
193 struct mlx5_core_sriov *sriov;
194 struct mlx5_core_dev *dev;
197 dev = pci_get_drvdata(pf);
198 num_vf_msix = MLX5_CAP_GEN_MAX(dev, num_total_dynamic_vf_msix);
204 mlx5_get_default_msix_vec_count(dev, pci_num_vf(pf));
206 sriov = &dev->priv.sriov;
208 /* Reversed translation of PCI VF function number to the internal
209 * function_id, which exists in the name of virtfn symlink.
211 for (id = 0; id < pci_num_vf(pf); id++) {
212 if (!sriov->vfs_ctx[id].enabled)
215 if (vf->devfn == pci_iov_virtfn_devfn(pf, id))
219 if (id == pci_num_vf(pf) || !sriov->vfs_ctx[id].enabled)
222 return mlx5_set_msix_vec_count(dev, id + 1, msix_vec_count);
225 int mlx5_sriov_attach(struct mlx5_core_dev *dev)
227 if (!mlx5_core_is_pf(dev) || !pci_num_vf(dev->pdev))
230 /* If sriov VFs exist in PCI level, enable them in device level */
231 return mlx5_device_enable_sriov(dev, pci_num_vf(dev->pdev));
234 void mlx5_sriov_detach(struct mlx5_core_dev *dev)
236 if (!mlx5_core_is_pf(dev))
239 mlx5_device_disable_sriov(dev, pci_num_vf(dev->pdev), false);
242 static u16 mlx5_get_max_vfs(struct mlx5_core_dev *dev)
247 if (mlx5_core_is_ecpf_esw_manager(dev)) {
248 out = mlx5_esw_query_functions(dev);
250 /* Old FW doesn't support getting total_vfs from esw func
251 * but supports getting it from pci_sriov.
255 host_total_vfs = MLX5_GET(query_esw_functions_out, out,
256 host_params_context.host_total_vfs);
259 return host_total_vfs;
263 return pci_sriov_get_totalvfs(dev->pdev);
266 int mlx5_sriov_init(struct mlx5_core_dev *dev)
268 struct mlx5_core_sriov *sriov = &dev->priv.sriov;
269 struct pci_dev *pdev = dev->pdev;
272 if (!mlx5_core_is_pf(dev))
275 total_vfs = pci_sriov_get_totalvfs(pdev);
276 sriov->max_vfs = mlx5_get_max_vfs(dev);
277 sriov->num_vfs = pci_num_vf(pdev);
278 sriov->vfs_ctx = kcalloc(total_vfs, sizeof(*sriov->vfs_ctx), GFP_KERNEL);
285 void mlx5_sriov_cleanup(struct mlx5_core_dev *dev)
287 struct mlx5_core_sriov *sriov = &dev->priv.sriov;
289 if (!mlx5_core_is_pf(dev))
292 kfree(sriov->vfs_ctx);