File Index
Files and directories
Sign in
FILE

sriov.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/ethernet/mellanox/mlx5/core/sriov.c 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 "mlx5_irq.h"
 38#include "eswitch.h"
 39
 40static int sriov_restore_guids(struct mlx5_core_dev *dev, int vf, u16 func_id)
 41{
 42	struct mlx5_core_sriov *sriov = &dev->priv.sriov;
 43	struct mlx5_hca_vport_context *in;
 44	int err = 0;
 45
 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);
 51		if (!in)
 52			return -ENOMEM;
 53
 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;
 57		in->field_select =
 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;
 61
 62		err = mlx5_core_modify_hca_vport_context(dev, 1, 1, func_id, in);
 63		if (err)
 64			mlx5_core_warn(dev, "modify vport context failed, unable to restore VF %d settings\n", vf);
 65
 66		kfree(in);
 67	}
 68
 69	return err;
 70}
 71
 72static int mlx5_device_enable_sriov(struct mlx5_core_dev *dev, int num_vfs)
 73{
 74	struct mlx5_core_sriov *sriov = &dev->priv.sriov;
 75	int err, vf, num_msix_count;
 76	int vport_num;
 77
 78	err = mlx5_eswitch_enable(dev->priv.eswitch, num_vfs);
 79	if (err) {
 80		mlx5_core_warn(dev,
 81			       "failed to enable eswitch SRIOV (%d)\n", err);
 82		return err;
 83	}
 84
 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
 88		 * some stuff.
 89		 */
 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);
 93		if (err) {
 94			mlx5_core_warn(dev, "failed to enable VF %d (%d)\n", vf, err);
 95			continue;
 96		}
 97
 98		err = mlx5_set_msix_vec_count(dev, vf + 1, num_msix_count);
 99		if (err) {
100			mlx5_core_warn(dev,
101				       "failed to set MSI-X vector counts VF %d, err %d\n",
102				       vf, err);
103			continue;
104		}
105
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
110					: vf + 1;
111			err = sriov_restore_guids(dev, vf, vport_num);
112			if (err) {
113				mlx5_core_warn(dev,
114					       "failed to restore VF %d settings, err %d\n",
115					       vf, err);
116				continue;
117			}
118		}
119		mlx5_core_dbg(dev, "successfully enabled VF* %d\n", vf);
120	}
121
122	return 0;
123}
124
125static void
126mlx5_device_disable_sriov(struct mlx5_core_dev *dev, int num_vfs, bool clear_vf, bool num_vf_change)
127{
128	struct mlx5_core_sriov *sriov = &dev->priv.sriov;
129	bool wait_for_ec_vf_pages = true;
130	bool wait_for_vf_pages = true;
131	int err;
132	int vf;
133
134	for (vf = num_vfs - 1; vf >= 0; vf--) {
135		if (!sriov->vfs_ctx[vf].enabled)
136			continue;
137		/* Notify the VF before its disablement to let it clean
138		 * some resources.
139		 */
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);
143		if (err) {
144			mlx5_core_warn(dev, "failed to disable VF %d\n", vf);
145			continue;
146		}
147		sriov->vfs_ctx[vf].enabled = 0;
148	}
149
150	mlx5_eswitch_disable_sriov(dev->priv.eswitch, clear_vf);
151
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.
158	 *
159	 *     2. The driver is being unloaded. In this case wait for all pages.
160	 */
161	if (num_vf_change) {
162		if (mlx5_core_ec_sriov_enabled(dev))
163			wait_for_vf_pages = false;
164		else
165			wait_for_ec_vf_pages = false;
166	}
167
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");
170
171	/* For ECPFs, skip waiting for host VF pages until ECPF is destroyed */
172	if (mlx5_core_is_ecpf(dev))
173		return;
174
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");
177}
178
179static int mlx5_sriov_enable(struct pci_dev *pdev, int num_vfs)
180{
181	struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
182	struct devlink *devlink = priv_to_devlink(dev);
183	int err;
184
185	devl_lock(devlink);
186	err = mlx5_device_enable_sriov(dev, num_vfs);
187	devl_unlock(devlink);
188	if (err) {
189		mlx5_core_warn(dev, "mlx5_device_enable_sriov failed : %d\n", err);
190		return err;
191	}
192
193	err = pci_enable_sriov(pdev, num_vfs);
194	if (err) {
195		mlx5_core_warn(dev, "pci_enable_sriov failed : %d\n", err);
196		devl_lock(devlink);
197		mlx5_device_disable_sriov(dev, num_vfs, true, true);
198		devl_unlock(devlink);
199	}
200	return err;
201}
202
203void mlx5_sriov_disable(struct pci_dev *pdev, bool num_vf_change)
204{
205	struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
206	struct devlink *devlink = priv_to_devlink(dev);
207	int num_vfs = pci_num_vf(dev->pdev);
208
209	pci_disable_sriov(pdev);
210	devl_lock(devlink);
211	mlx5_device_disable_sriov(dev, num_vfs, true, num_vf_change);
212	devl_unlock(devlink);
213}
214
215int mlx5_core_sriov_configure(struct pci_dev *pdev, int num_vfs)
216{
217	struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
218	struct mlx5_core_sriov *sriov = &dev->priv.sriov;
219	int err = 0;
220
221	mlx5_core_dbg(dev, "requested num_vfs %d\n", num_vfs);
222
223	if (num_vfs)
224		err = mlx5_sriov_enable(pdev, num_vfs);
225	else
226		mlx5_sriov_disable(pdev, true);
227
228	if (!err)
229		sriov->num_vfs = num_vfs;
230	return err ? err : num_vfs;
231}
232
233int mlx5_core_sriov_set_msix_vec_count(struct pci_dev *vf, int msix_vec_count)
234{
235	struct pci_dev *pf = pci_physfn(vf);
236	struct mlx5_core_sriov *sriov;
237	struct mlx5_core_dev *dev;
238	int num_vf_msix, id;
239
240	dev = pci_get_drvdata(pf);
241	num_vf_msix = MLX5_CAP_GEN_MAX(dev, num_total_dynamic_vf_msix);
242	if (!num_vf_msix)
243		return -EOPNOTSUPP;
244
245	if (!msix_vec_count)
246		msix_vec_count =
247			mlx5_get_default_msix_vec_count(dev, pci_num_vf(pf));
248
249	sriov = &dev->priv.sriov;
250	id = pci_iov_vf_id(vf);
251	if (id < 0 || !sriov->vfs_ctx[id].enabled)
252		return -EINVAL;
253
254	return mlx5_set_msix_vec_count(dev, id + 1, msix_vec_count);
255}
256
257int mlx5_sriov_attach(struct mlx5_core_dev *dev)
258{
259	if (!mlx5_core_is_pf(dev) || !pci_num_vf(dev->pdev))
260		return 0;
261
262	/* If sriov VFs exist in PCI level, enable them in device level */
263	return mlx5_device_enable_sriov(dev, pci_num_vf(dev->pdev));
264}
265
266void mlx5_sriov_detach(struct mlx5_core_dev *dev)
267{
268	if (!mlx5_core_is_pf(dev))
269		return;
270
271	mlx5_device_disable_sriov(dev, pci_num_vf(dev->pdev), false, false);
272}
273
274static u16 mlx5_get_max_vfs(struct mlx5_core_dev *dev)
275{
276	u16 host_total_vfs;
277	const u32 *out;
278
279	if (mlx5_core_is_ecpf_esw_manager(dev)) {
280		out = mlx5_esw_query_functions(dev);
281
282		/* Old FW doesn't support getting total_vfs from esw func
283		 * but supports getting it from pci_sriov.
284		 */
285		if (IS_ERR(out))
286			goto done;
287		host_total_vfs = MLX5_GET(query_esw_functions_out, out,
288					  host_params_context.host_total_vfs);
289		kvfree(out);
290		return host_total_vfs;
291	}
292
293done:
294	return pci_sriov_get_totalvfs(dev->pdev);
295}
296
297int mlx5_sriov_init(struct mlx5_core_dev *dev)
298{
299	struct mlx5_core_sriov *sriov = &dev->priv.sriov;
300	struct pci_dev *pdev = dev->pdev;
301	int total_vfs, i;
302
303	if (!mlx5_core_is_pf(dev))
304		return 0;
305
306	total_vfs = pci_sriov_get_totalvfs(pdev);
307	sriov->max_vfs = mlx5_get_max_vfs(dev);
308	sriov->num_vfs = pci_num_vf(pdev);
309	sriov->max_ec_vfs = mlx5_core_ec_sriov_enabled(dev) ? pci_sriov_get_totalvfs(dev->pdev) : 0;
310	sriov->vfs_ctx = kcalloc(total_vfs, sizeof(*sriov->vfs_ctx), GFP_KERNEL);
311	if (!sriov->vfs_ctx)
312		return -ENOMEM;
313
314	for (i = 0; i < total_vfs; i++)
315		BLOCKING_INIT_NOTIFIER_HEAD(&sriov->vfs_ctx[i].notifier);
316
317	return 0;
318}
319
320void mlx5_sriov_cleanup(struct mlx5_core_dev *dev)
321{
322	struct mlx5_core_sriov *sriov = &dev->priv.sriov;
323
324	if (!mlx5_core_is_pf(dev))
325		return;
326
327	kfree(sriov->vfs_ctx);
328}
329
330/**
331 * mlx5_sriov_blocking_notifier_unregister - Unregister a VF from
332 * a notification block chain.
333 *
334 * @mdev: The mlx5 core device.
335 * @vf_id: The VF id.
336 * @nb: The notifier block to be unregistered.
337 */
338void mlx5_sriov_blocking_notifier_unregister(struct mlx5_core_dev *mdev,
339					     int vf_id,
340					     struct notifier_block *nb)
341{
342	struct mlx5_vf_context *vfs_ctx;
343	struct mlx5_core_sriov *sriov;
344
345	sriov = &mdev->priv.sriov;
346	if (WARN_ON(vf_id < 0 || vf_id >= sriov->num_vfs))
347		return;
348
349	vfs_ctx = &sriov->vfs_ctx[vf_id];
350	blocking_notifier_chain_unregister(&vfs_ctx->notifier, nb);
351}
352EXPORT_SYMBOL(mlx5_sriov_blocking_notifier_unregister);
353
354/**
355 * mlx5_sriov_blocking_notifier_register - Register a VF notification
356 * block chain.
357 *
358 * @mdev: The mlx5 core device.
359 * @vf_id: The VF id.
360 * @nb: The notifier block to be called upon the VF events.
361 *
362 * Returns 0 on success or an error code.
363 */
364int mlx5_sriov_blocking_notifier_register(struct mlx5_core_dev *mdev,
365					  int vf_id,
366					  struct notifier_block *nb)
367{
368	struct mlx5_vf_context *vfs_ctx;
369	struct mlx5_core_sriov *sriov;
370
371	sriov = &mdev->priv.sriov;
372	if (vf_id < 0 || vf_id >= sriov->num_vfs)
373		return -EINVAL;
374
375	vfs_ctx = &sriov->vfs_ctx[vf_id];
376	return blocking_notifier_chain_register(&vfs_ctx->notifier, nb);
377}
378EXPORT_SYMBOL(mlx5_sriov_blocking_notifier_register);
~/ Read-only file preview.