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"
40 static int sriov_restore_guids(struct mlx5_core_dev *dev, int vf, u16 func_id)
42 struct mlx5_core_sriov *sriov = &dev->priv.sriov;
43 struct mlx5_hca_vport_context *in;
46 /* Restore sriov guid and policy settings */
47 if (sriov->vfs_ctx[vf].node_guid ||
48 sriov->vfs_ctx[vf].port_guid ||
49 sriov->vfs_ctx[vf].policy != MLX5_POLICY_INVALID) {
50 in = kzalloc(sizeof(*in), GFP_KERNEL);
54 in->node_guid = sriov->vfs_ctx[vf].node_guid;
55 in->port_guid = sriov->vfs_ctx[vf].port_guid;
56 in->policy = sriov->vfs_ctx[vf].policy;
58 !!(in->port_guid) * MLX5_HCA_VPORT_SEL_PORT_GUID |
59 !!(in->node_guid) * MLX5_HCA_VPORT_SEL_NODE_GUID |
60 !!(in->policy) * MLX5_HCA_VPORT_SEL_STATE_POLICY;
62 err = mlx5_core_modify_hca_vport_context(dev, 1, 1, func_id, in);
64 mlx5_core_warn(dev, "modify vport context failed, unable to restore VF %d settings\n", vf);
72 static int mlx5_device_enable_sriov(struct mlx5_core_dev *dev, int num_vfs)
74 struct mlx5_core_sriov *sriov = &dev->priv.sriov;
75 int err, vf, num_msix_count;
78 err = mlx5_eswitch_enable(dev->priv.eswitch, num_vfs);
81 "failed to enable eswitch SRIOV (%d)\n", err);
85 num_msix_count = mlx5_get_default_msix_vec_count(dev, num_vfs);
86 for (vf = 0; vf < num_vfs; vf++) {
87 /* Notify the VF before its enablement to let it set
90 blocking_notifier_call_chain(&sriov->vfs_ctx[vf].notifier,
91 MLX5_PF_NOTIFY_ENABLE_VF, dev);
92 err = mlx5_core_enable_hca(dev, vf + 1);
94 mlx5_core_warn(dev, "failed to enable VF %d (%d)\n", vf, err);
98 err = mlx5_set_msix_vec_count(dev, vf + 1, num_msix_count);
101 "failed to set MSI-X vector counts VF %d, err %d\n",
106 sriov->vfs_ctx[vf].enabled = 1;
107 if (MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) {
108 vport_num = mlx5_core_ec_sriov_enabled(dev) ?
109 mlx5_core_ec_vf_vport_base(dev) + vf
111 err = sriov_restore_guids(dev, vf, vport_num);
114 "failed to restore VF %d settings, err %d\n",
119 mlx5_core_dbg(dev, "successfully enabled VF* %d\n", vf);
126 mlx5_device_disable_sriov(struct mlx5_core_dev *dev, int num_vfs, bool clear_vf, bool num_vf_change)
128 struct mlx5_core_sriov *sriov = &dev->priv.sriov;
129 bool wait_for_ec_vf_pages = true;
130 bool wait_for_vf_pages = true;
134 for (vf = num_vfs - 1; vf >= 0; vf--) {
135 if (!sriov->vfs_ctx[vf].enabled)
137 /* Notify the VF before its disablement to let it clean
140 blocking_notifier_call_chain(&sriov->vfs_ctx[vf].notifier,
141 MLX5_PF_NOTIFY_DISABLE_VF, dev);
142 err = mlx5_core_disable_hca(dev, vf + 1);
144 mlx5_core_warn(dev, "failed to disable VF %d\n", vf);
147 sriov->vfs_ctx[vf].enabled = 0;
150 mlx5_eswitch_disable_sriov(dev->priv.eswitch, clear_vf);
152 /* There are a number of scenarios when SRIOV is being disabled:
153 * 1. VFs or ECVFs had been created, and now set back to 0 (num_vf_change == true).
154 * - If EC SRIOV is enabled then this flow is happening on the
155 * embedded platform, wait for only EC VF pages.
156 * - If EC SRIOV is not enabled this flow is happening on non-embedded
157 * platform, wait for the VF pages.
159 * 2. The driver is being unloaded. In this case wait for all pages.
162 if (mlx5_core_ec_sriov_enabled(dev))
163 wait_for_vf_pages = false;
165 wait_for_ec_vf_pages = false;
168 if (wait_for_ec_vf_pages && mlx5_wait_for_pages(dev, &dev->priv.page_counters[MLX5_EC_VF]))
169 mlx5_core_warn(dev, "timeout reclaiming EC VFs pages\n");
171 /* For ECPFs, skip waiting for host VF pages until ECPF is destroyed */
172 if (mlx5_core_is_ecpf(dev))
175 if (wait_for_vf_pages && mlx5_wait_for_pages(dev, &dev->priv.page_counters[MLX5_VF]))
176 mlx5_core_warn(dev, "timeout reclaiming VFs pages\n");
179 static int mlx5_sriov_enable(struct pci_dev *pdev, int num_vfs)
181 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
182 struct devlink *devlink = priv_to_devlink(dev);
186 err = mlx5_device_enable_sriov(dev, num_vfs);
187 devl_unlock(devlink);
189 mlx5_core_warn(dev, "mlx5_device_enable_sriov failed : %d\n", err);
193 err = pci_enable_sriov(pdev, num_vfs);
195 mlx5_core_warn(dev, "pci_enable_sriov failed : %d\n", err);
196 mlx5_device_disable_sriov(dev, num_vfs, true, true);
201 void mlx5_sriov_disable(struct pci_dev *pdev, bool num_vf_change)
203 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
204 struct devlink *devlink = priv_to_devlink(dev);
205 int num_vfs = pci_num_vf(dev->pdev);
207 pci_disable_sriov(pdev);
209 mlx5_device_disable_sriov(dev, num_vfs, true, num_vf_change);
210 devl_unlock(devlink);
213 int mlx5_core_sriov_configure(struct pci_dev *pdev, int num_vfs)
215 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
216 struct mlx5_core_sriov *sriov = &dev->priv.sriov;
219 mlx5_core_dbg(dev, "requested num_vfs %d\n", num_vfs);
222 err = mlx5_sriov_enable(pdev, num_vfs);
224 mlx5_sriov_disable(pdev, true);
227 sriov->num_vfs = num_vfs;
228 return err ? err : num_vfs;
231 int mlx5_core_sriov_set_msix_vec_count(struct pci_dev *vf, int msix_vec_count)
233 struct pci_dev *pf = pci_physfn(vf);
234 struct mlx5_core_sriov *sriov;
235 struct mlx5_core_dev *dev;
238 dev = pci_get_drvdata(pf);
239 num_vf_msix = MLX5_CAP_GEN_MAX(dev, num_total_dynamic_vf_msix);
245 mlx5_get_default_msix_vec_count(dev, pci_num_vf(pf));
247 sriov = &dev->priv.sriov;
248 id = pci_iov_vf_id(vf);
249 if (id < 0 || !sriov->vfs_ctx[id].enabled)
252 return mlx5_set_msix_vec_count(dev, id + 1, msix_vec_count);
255 int mlx5_sriov_attach(struct mlx5_core_dev *dev)
257 if (!mlx5_core_is_pf(dev) || !pci_num_vf(dev->pdev))
260 /* If sriov VFs exist in PCI level, enable them in device level */
261 return mlx5_device_enable_sriov(dev, pci_num_vf(dev->pdev));
264 void mlx5_sriov_detach(struct mlx5_core_dev *dev)
266 if (!mlx5_core_is_pf(dev))
269 mlx5_device_disable_sriov(dev, pci_num_vf(dev->pdev), false, false);
272 static u16 mlx5_get_max_vfs(struct mlx5_core_dev *dev)
277 if (mlx5_core_is_ecpf_esw_manager(dev)) {
278 out = mlx5_esw_query_functions(dev);
280 /* Old FW doesn't support getting total_vfs from esw func
281 * but supports getting it from pci_sriov.
285 host_total_vfs = MLX5_GET(query_esw_functions_out, out,
286 host_params_context.host_total_vfs);
288 return host_total_vfs;
292 return pci_sriov_get_totalvfs(dev->pdev);
295 int mlx5_sriov_init(struct mlx5_core_dev *dev)
297 struct mlx5_core_sriov *sriov = &dev->priv.sriov;
298 struct pci_dev *pdev = dev->pdev;
301 if (!mlx5_core_is_pf(dev))
304 total_vfs = pci_sriov_get_totalvfs(pdev);
305 sriov->max_vfs = mlx5_get_max_vfs(dev);
306 sriov->num_vfs = pci_num_vf(pdev);
307 sriov->max_ec_vfs = mlx5_core_ec_sriov_enabled(dev) ? pci_sriov_get_totalvfs(dev->pdev) : 0;
308 sriov->vfs_ctx = kcalloc(total_vfs, sizeof(*sriov->vfs_ctx), GFP_KERNEL);
312 for (i = 0; i < total_vfs; i++)
313 BLOCKING_INIT_NOTIFIER_HEAD(&sriov->vfs_ctx[i].notifier);
318 void mlx5_sriov_cleanup(struct mlx5_core_dev *dev)
320 struct mlx5_core_sriov *sriov = &dev->priv.sriov;
322 if (!mlx5_core_is_pf(dev))
325 kfree(sriov->vfs_ctx);
329 * mlx5_sriov_blocking_notifier_unregister - Unregister a VF from
330 * a notification block chain.
332 * @mdev: The mlx5 core device.
334 * @nb: The notifier block to be unregistered.
336 void mlx5_sriov_blocking_notifier_unregister(struct mlx5_core_dev *mdev,
338 struct notifier_block *nb)
340 struct mlx5_vf_context *vfs_ctx;
341 struct mlx5_core_sriov *sriov;
343 sriov = &mdev->priv.sriov;
344 if (WARN_ON(vf_id < 0 || vf_id >= sriov->num_vfs))
347 vfs_ctx = &sriov->vfs_ctx[vf_id];
348 blocking_notifier_chain_unregister(&vfs_ctx->notifier, nb);
350 EXPORT_SYMBOL(mlx5_sriov_blocking_notifier_unregister);
353 * mlx5_sriov_blocking_notifier_register - Register a VF notification
356 * @mdev: The mlx5 core device.
358 * @nb: The notifier block to be called upon the VF events.
360 * Returns 0 on success or an error code.
362 int mlx5_sriov_blocking_notifier_register(struct mlx5_core_dev *mdev,
364 struct notifier_block *nb)
366 struct mlx5_vf_context *vfs_ctx;
367 struct mlx5_core_sriov *sriov;
369 sriov = &mdev->priv.sriov;
370 if (vf_id < 0 || vf_id >= sriov->num_vfs)
373 vfs_ctx = &sriov->vfs_ctx[vf_id];
374 return blocking_notifier_chain_register(&vfs_ctx->notifier, nb);
376 EXPORT_SYMBOL(mlx5_sriov_blocking_notifier_register);