Skip to contentSkip to conversation

net/mlx5: Introduce shared devlink instance for PFs on same chip

RFC PATCH In review Jiri Pirko 4 participants 18 messages Mar 18, 2025 – Mar 19, 2025

Subsystems (6)
lore
Share & tools
lore

Jiri Pirko

author
#0 lore raw

cover net/mlx5: Introduce shared devlink instance for PFs on same chip

From: Jiri Pirko <[email protected]>

This patchsets aims to introduce an entity, that allows to pin devlink configuration objects (params, resources, etc) on for things shared among multiple PFs.

As the shared entity that kind of float above the actual PFs does not have explicit a PCI function, use faux device to back the devlink instance. Expose the relationship between PF devlink instances and this new shared instance by nested devlink attributes.

Example:

$ devlink dev
pci/0000:08:00.0:                   <--- PF0
  nested_devlink:
    auxiliary/mlx5_core.eth.0
faux/mlx5_core_83013c12b77faa1a30000c82a1045c91:  <--- shared devlink for chip that PF0 and PF1 are part of
  nested_devlink:
    pci/0000:08:00.0
    pci/0000:08:00.1
auxiliary/mlx5_core.eth.0           <--- ethernet auxiliary device of PF0
pci/0000:08:00.1:                   <--- PF1
  nested_devlink:
    auxiliary/mlx5_core.eth.1
auxiliary/mlx5_core.eth.1           <--- ethernet auxiliary device of PF1

The first patch is a little adjustment to recently introduced faux driver. The second patch introduces the shared devlink instance.
The last patch introduces example devlink param for the shared instance.

Jiri Pirko (3):
faux: extend the creation function for module namespace net/mlx5: Introduce shared devlink instance for PFs on same chip net/mlx5: Introduce enable_sriov param for shared devlink

 drivers/base/faux.c                           |  20 +-
 .../net/ethernet/mellanox/mlx5/core/Makefile  |   5 +-
 .../mellanox/mlx5/core/lib/nv_param.c         | 236 ++++++++++++++++++
 .../mellanox/mlx5/core/lib/nv_param.h         |  14 ++
 .../net/ethernet/mellanox/mlx5/core/main.c    |  18 ++
 .../ethernet/mellanox/mlx5/core/sh_devlink.c  | 164 ++++++++++++
 .../ethernet/mellanox/mlx5/core/sh_devlink.h  |  11 +
 include/linux/device/faux.h                   |   6 +-
 include/linux/mlx5/driver.h                   |   6 +
 include/linux/module.h                        |   2 +-
 10 files changed, 469 insertions(+), 13 deletions(-)
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/lib/nv_param.c
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/lib/nv_param.h
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.h
Signature · 1 line
2.48.1
author
#1 lore raw

RFC PATCH 1/3 faux: extend the creation function for module namespace

From: Jiri Pirko <[email protected]>

It is hard for the faux user to avoid potential name conflicts, as it is only in control of faux devices it creates. Therefore extend the faux device creation function by module parameter, embed the module name into the device name in format "modulename_permodulename" and allow module to control it's namespace.

---
 drivers/base/faux.c         | 20 ++++++++++++--------
 include/linux/device/faux.h |  6 ++++--
 include/linux/module.h      |  2 +-
 3 files changed, 17 insertions(+), 11 deletions(-)
drivers/base/faux.c +12 -8
index 531e9d789ee0..b1fcac6b0946 100644
@@ -85,8 +85,9 @@ static void faux_device_release(struct device *dev)
  * faux_device_create_with_groups - Create and register with the driver
  *		core a faux device and populate the device with an initial
  *		set of sysfs attributes.
- * @name:	The name of the device we are adding, must be unique for
- *		all faux devices.
+ * @module:	Pointer to module this device is associated.
+ * @name:	The name of the device we are adding, must be unique for all
+ *		faux devices within a single module.
  * @parent:	Pointer to a potential parent struct device.  If set to
  *		NULL, the device will be created in the "root" of the faux
  *		device tree in sysfs.
@@ -108,7 +109,8 @@ static void faux_device_release(struct device *dev)
  * * NULL if an error happened with creating the device
  * * pointer to a valid struct faux_device that is registered with sysfs
  */
-struct faux_device *faux_device_create_with_groups(const char *name,
+struct faux_device *faux_device_create_with_groups(const struct module *module,
+						   const char *name,
 						   struct device *parent,
 						   const struct faux_device_ops *faux_ops,
 						   const struct attribute_group **groups)
@@ -137,12 +139,12 @@ struct faux_device *faux_device_create_with_groups(const char *name,
 		dev->parent = &faux_bus_root;
 	dev->bus = &faux_bus_type;
 	dev->groups = groups;
-	dev_set_name(dev, "%s", name);
+	dev_set_name(dev, "%s_%s", module_name(module), name);
 
 	ret = device_add(dev);
 	if (ret) {
 		pr_err("%s: device_add for faux device '%s' failed with %d\n",
-		       __func__, name, ret);
+		       __func__, dev_name(dev), ret);
 		put_device(dev);
 		return NULL;
 	}
@@ -153,8 +155,9 @@ EXPORT_SYMBOL_GPL(faux_device_create_with_groups);
 
 /**
  * faux_device_create - create and register with the driver core a faux device
+ * @module:	Pointer to module this device is associated.
  * @name:	The name of the device we are adding, must be unique for all
- *		faux devices.
+ *		faux devices within a single module.
  * @parent:	Pointer to a potential parent struct device.  If set to
  *		NULL, the device will be created in the "root" of the faux
  *		device tree in sysfs.
@@ -174,11 +177,12 @@ EXPORT_SYMBOL_GPL(faux_device_create_with_groups);
  * * NULL if an error happened with creating the device
  * * pointer to a valid struct faux_device that is registered with sysfs
  */
-struct faux_device *faux_device_create(const char *name,
+struct faux_device *faux_device_create(const struct module *module,
+				       const char *name,
 				       struct device *parent,
 				       const struct faux_device_ops *faux_ops)
 {
-	return faux_device_create_with_groups(name, parent, faux_ops, NULL);
+	return faux_device_create_with_groups(module, name, parent, faux_ops, NULL);
 }
 EXPORT_SYMBOL_GPL(faux_device_create);
 
include/linux/device/faux.h +4 -2
index 9f43c0e46aa4..b1393a34b4f9 100644
@@ -47,10 +47,12 @@ struct faux_device_ops {
 	void (*remove)(struct faux_device *faux_dev);
 };
 
-struct faux_device *faux_device_create(const char *name,
+struct faux_device *faux_device_create(const struct module *module,
+				       const char *name,
 				       struct device *parent,
 				       const struct faux_device_ops *faux_ops);
-struct faux_device *faux_device_create_with_groups(const char *name,
+struct faux_device *faux_device_create_with_groups(const struct module *module,
+						   const char *name,
 						   struct device *parent,
 						   const struct faux_device_ops *faux_ops,
 						   const struct attribute_group **groups);
include/linux/module.h +1 -1
index 30e5b19bafa9..8d1a7e65d2e4 100644
@@ -744,7 +744,7 @@ static inline void __module_get(struct module *module)
 /* This is a #define so the string doesn't get put in every .o file */
 #define module_name(mod)			\
 ({						\
-	struct module *__mod = (mod);		\
+	const struct module *__mod = (mod);	\
 	__mod ? __mod->name : "kernel";		\
 })
 
Signature · 1 line
2.48.1
maintainer
#2 lore raw

Re: [PATCH net-next RFC 1/3] faux: extend the creation function for module namespace

include/linux/module.h static inline void __module_get(struct module *module) · line 749

Quoted 10 lines · Jiri Pirko
include/linux/module.h +1 -1
@@ -744,7 +744,7 @@ static inline void __module_get(struct module *module)
 /* This is a #define so the string doesn't get put in every .o file */
 #define module_name(mod)			\
 ({						\
-	struct module *__mod = (mod);		\
+	const struct module *__mod = (mod);	\
 	__mod ? __mod->name : "kernel";		\
 })

This feels like it should be a separate change, right? Doesn't have to do with this patch.

greg k-h

author
#3 lore raw

Re: [PATCH net-next RFC 1/3] faux: extend the creation function for module namespace

Quoted 14 lines · Tue, Mar 18, 2025 at 02:46:56PM +0100, [email protected] wrote:

include/linux/module.h static inline void __module_get(struct module *module) · line 749

Quoted 10 lines · Jiri Pirko
include/linux/module.h +1 -1
@@ -744,7 +744,7 @@ static inline void __module_get(struct module *module)
 /* This is a #define so the string doesn't get put in every .o file */
 #define module_name(mod)			\
 ({						\
-	struct module *__mod = (mod);		\
+	const struct module *__mod = (mod);	\
 	__mod ? __mod->name : "kernel";		\
 })

This feels like it should be a separate change, right? Doesn't have to do with this patch.

True. Will split.

Quoted 2 lines

greg k-h

maintainer
#4 lore raw

Re: [PATCH net-next RFC 1/3] faux: extend the creation function for module namespace

In reply to Jiri Pirko
Quoted 7 lines · Jiri Pirko

From: Jiri Pirko <[email protected]>

It is hard for the faux user to avoid potential name conflicts, as it is only in control of faux devices it creates. Therefore extend the faux device creation function by module parameter, embed the module name into the device name in format "modulename_permodulename" and allow module to control it's namespace.

Do you have an example of how this will change the current names we have in the system to this new way? What is going to break if those names change?

I say this as the perf devices seem to have "issues" with their names and locations in sysfs as userspace tools use them today, and in a straight port to faux it is ok, but if the device name changes, that is going to have problems.

Why can't you handle this "namespace" issue yourself in the caller to the api? Why must the faux code handle it for you? We don't do this for platform devices, why is this any different?

thanks,

greg k-h

author
#5 lore raw

Re: [PATCH net-next RFC 1/3] faux: extend the creation function for module namespace

Quoted 9 lines · Tue, Mar 18, 2025 at 03:36:34PM +0100, [email protected] wrote:
Quoted 7 lines · Jiri Pirko

From: Jiri Pirko <[email protected]>

It is hard for the faux user to avoid potential name conflicts, as it is only in control of faux devices it creates. Therefore extend the faux device creation function by module parameter, embed the module name into the device name in format "modulename_permodulename" and allow module to control it's namespace.

Quoted 3 lines

Do you have an example of how this will change the current names we have in the system to this new way? What is going to break if those names change?

I was under impression, that since there are no in-tree users of faux yet (at least I don't see them in net-next tree), there is no breakage.

Quoted 5 lines

I say this as the perf devices seem to have "issues" with their names and locations in sysfs as userspace tools use them today, and in a straight port to faux it is ok, but if the device name changes, that is going to have problems.

Got it. I didn't consider that.

Quoted 4 lines

Why can't you handle this "namespace" issue yourself in the caller to the api? Why must the faux code handle it for you? We don't do this for platform devices, why is this any different?

Well, I wanted to avoid alloc&printf names in driver, since dev_set_name() accepts vararg and faux_device_create()/faux_device_create_with_groups() don't.

Perhaps "const char *name" could be formatted as well for faux_device_create()/faux_device_create_with_groups(). My laziness wanted to avoid that :) Would that make sense to you?

Quoted 4 lines

thanks,

greg k-h

maintainer
#6 lore raw

Re: [PATCH net-next RFC 1/3] faux: extend the creation function for module namespace

Quoted 10 lines · Jiri Pirko
Quoted 9 lines · Tue, Mar 18, 2025 at 03:36:34PM +0100, [email protected] wrote:
Quoted 7 lines · Jiri Pirko

From: Jiri Pirko <[email protected]>

It is hard for the faux user to avoid potential name conflicts, as it is only in control of faux devices it creates. Therefore extend the faux device creation function by module parameter, embed the module name into the device name in format "modulename_permodulename" and allow module to control it's namespace.

Quoted 6 lines
Quoted 3 lines

Do you have an example of how this will change the current names we have in the system to this new way? What is going to break if those names change?

I was under impression, that since there are no in-tree users of faux yet (at least I don't see them in net-next tree), there is no breakage.

Look at linux-next please.

Quoted 7 lines
Quoted 3 lines

Why can't you handle this "namespace" issue yourself in the caller to the api? Why must the faux code handle it for you? We don't do this for platform devices, why is this any different?

Well, I wanted to avoid alloc&printf names in driver, since dev_set_name() accepts vararg and faux_device_create()/faux_device_create_with_groups() don't.

If you want to do something complex, do it in your driver :)

Quoted 3 lines

Perhaps "const char *name" could be formatted as well for faux_device_create()/faux_device_create_with_groups(). My laziness wanted to avoid that :) Would that make sense to you?

I wouldn't object to that, making it a vararg? How would the rust binding handle that?

thanks,

greg k-h

author
#7 lore raw

Re: [PATCH net-next RFC 1/3] faux: extend the creation function for module namespace

Quoted 11 lines · Tue, Mar 18, 2025 at 05:04:37PM +0100, [email protected] wrote:
Quoted 10 lines · Jiri Pirko
Quoted 9 lines · Tue, Mar 18, 2025 at 03:36:34PM +0100, [email protected] wrote:
Quoted 7 lines · Jiri Pirko

From: Jiri Pirko <[email protected]>

It is hard for the faux user to avoid potential name conflicts, as it is only in control of faux devices it creates. Therefore extend the faux device creation function by module parameter, embed the module name into the device name in format "modulename_permodulename" and allow module to control it's namespace.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Do you have an example of how this will change the current names we have in the system to this new way? What is going to break if those names change?

I was under impression, that since there are no in-tree users of faux yet (at least I don't see them in net-next tree), there is no breakage.

Look at linux-next please.

Sure, but it's still next. Next might break (uapi) as long it's next, right?

Quoted 10 lines
Quoted 8 lines
Quoted 4 lines

Why can't you handle this "namespace" issue yourself in the caller to the api? Why must the faux code handle it for you? We don't do this for platform devices, why is this any different?

Well, I wanted to avoid alloc&printf names in driver, since dev_set_name() accepts vararg and faux_device_create()/faux_device_create_with_groups() don't.

If you want to do something complex, do it in your driver :)

Yeah, I don't really want to do anything complex, that's why I wanted to take leverage of dev_set_name()

Quoted 7 lines
Quoted 4 lines

Perhaps "const char *name" could be formatted as well for faux_device_create()/faux_device_create_with_groups(). My laziness wanted to avoid that :) Would that make sense to you?

I wouldn't object to that, making it a vararg? How would the rust binding handle that?

Why should I care about rust? I got the impression the deal is that rust bindings are taken care of by rust people. Did that change and we need to keep rust in mind for all internal API? That sounds scarry to me :(

Quoted 4 lines

thanks,

greg k-h

maintainer
#8 lore raw

Re: [PATCH net-next RFC 1/3] faux: extend the creation function for module namespace

Quoted 16 lines · Jiri Pirko
Quoted 15 lines · Tue, Mar 18, 2025 at 05:04:37PM +0100, [email protected] wrote:
Quoted 14 lines · Jiri Pirko
Quoted 12 lines · Tue, Mar 18, 2025 at 03:36:34PM +0100, [email protected] wrote:
Quoted 7 lines · Jiri Pirko

From: Jiri Pirko <[email protected]>

It is hard for the faux user to avoid potential name conflicts, as it is only in control of faux devices it creates. Therefore extend the faux device creation function by module parameter, embed the module name into the device name in format "modulename_permodulename" and allow module to control it's namespace.

Do you have an example of how this will change the current names we have in the system to this new way? What is going to break if those names change?

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

I was under impression, that since there are no in-tree users of faux yet (at least I don't see them in net-next tree), there is no breakage.

Look at linux-next please.

Sure, but it's still next. Next might break (uapi) as long it's next, right?

The point is that these conversions are thinking that their name is stable. This change is going to mean that those patches that have been accepted into different trees are going to change.

Quoted 11 lines
Quoted 6 lines
Quoted 3 lines

Perhaps "const char *name" could be formatted as well for faux_device_create()/faux_device_create_with_groups(). My laziness wanted to avoid that :) Would that make sense to you?

I wouldn't object to that, making it a vararg? How would the rust binding handle that?

Why should I care about rust? I got the impression the deal is that rust bindings are taken care of by rust people. Did that change and we need to keep rust in mind for all internal API? That sounds scarry to me :(

I was just asking if you knew, that's all.

thanks,

greg k-h

author
#9 lore raw

Re: [PATCH net-next RFC 1/3] faux: extend the creation function for module namespace

Quoted 20 lines · Tue, Mar 18, 2025 at 06:27:17PM +0100, [email protected] wrote:
Quoted 19 lines · Jiri Pirko
Quoted 18 lines · Tue, Mar 18, 2025 at 05:04:37PM +0100, [email protected] wrote:
Quoted 16 lines · Jiri Pirko
Quoted 9 lines · Tue, Mar 18, 2025 at 03:36:34PM +0100, [email protected] wrote:
Quoted 7 lines · Jiri Pirko

From: Jiri Pirko <[email protected]>

It is hard for the faux user to avoid potential name conflicts, as it is only in control of faux devices it creates. Therefore extend the faux device creation function by module parameter, embed the module name into the device name in format "modulename_permodulename" and allow module to control it's namespace.

Quoted 3 lines

Do you have an example of how this will change the current names we have in the system to this new way? What is going to break if those names change?

I was under impression, that since there are no in-tree users of faux yet (at least I don't see them in net-next tree), there is no breakage.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Look at linux-next please.

Sure, but it's still next. Next might break (uapi) as long it's next, right?

The point is that these conversions are thinking that their name is stable. This change is going to mean that those patches that have been accepted into different trees are going to change.

Correct. I was under impression this could be handled in within "next". If not, my bad.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines

Perhaps "const char *name" could be formatted as well for faux_device_create()/faux_device_create_with_groups(). My laziness wanted to avoid that :) Would that make sense to you?

I wouldn't object to that, making it a vararg? How would the rust binding handle that?

Quoted 6 lines
Quoted 4 lines

Why should I care about rust? I got the impression the deal is that rust bindings are taken care of by rust people. Did that change and we need to keep rust in mind for all internal API? That sounds scarry to me :(

I was just asking if you knew, that's all.

Okay, I misunderstood. I have near 0 knowledge of Rust :)

Quoted 4 lines

thanks,

greg k-h

author
#10 lore raw

RFC PATCH 2/3 net/mlx5: Introduce shared devlink instance for PFs on same chip

In reply to Jiri Pirko

From: Jiri Pirko <[email protected]>

Multiple PFS may reside on the same physical chip, running a single firmware. Some of the resources and configurations may be shared among these PFs. Currently, there is not good object to pin the configuration knobs on.

Introduce a shared devlink, instantiated upon probe of the first PF, removed during remove of the last PF. Back this shared devlink instance by faux device, as there is no PCI device related to it.

Make the PF devlink instances nested in this shared devlink instance.

Example:

$ devlink dev
pci/0000:08:00.0:
  nested_devlink:
    auxiliary/mlx5_core.eth.0
faux/mlx5_core_83013c12b77faa1a30000c82a1045c91:
  nested_devlink:
    pci/0000:08:00.0
    pci/0000:08:00.1
auxiliary/mlx5_core.eth.0
pci/0000:08:00.1:
  nested_devlink:
    auxiliary/mlx5_core.eth.1
auxiliary/mlx5_core.eth.1
---
 .../net/ethernet/mellanox/mlx5/core/Makefile  |   4 +-
 .../net/ethernet/mellanox/mlx5/core/main.c    |  18 +++
 .../ethernet/mellanox/mlx5/core/sh_devlink.c  | 150 ++++++++++++++++++
 .../ethernet/mellanox/mlx5/core/sh_devlink.h  |  10 ++
 include/linux/mlx5/driver.h                   |   5 +
 5 files changed, 185 insertions(+), 2 deletions(-)
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.h
drivers/net/ethernet/mellanox/mlx5/core/Makefile +2 -2
index 568bbe5f83f5..510850b6e6e2 100644
@@ -16,8 +16,8 @@ mlx5_core-y :=	main.o cmd.o debugfs.o fw.o eq.o uar.o pagealloc.o \
 		transobj.o vport.o sriov.o fs_cmd.o fs_core.o pci_irq.o \
 		fs_counters.o fs_ft_pool.o rl.o lag/debugfs.o lag/lag.o dev.o events.o wq.o lib/gid.o \
 		lib/devcom.o lib/pci_vsc.o lib/dm.o lib/fs_ttc.o diag/fs_tracepoint.o \
-		diag/fw_tracer.o diag/crdump.o devlink.o diag/rsc_dump.o diag/reporter_vnic.o \
-		fw_reset.o qos.o lib/tout.o lib/aso.o wc.o fs_pool.o
+		diag/fw_tracer.o diag/crdump.o devlink.o sh_devlink.o diag/rsc_dump.o \
+		diag/reporter_vnic.o fw_reset.o qos.o lib/tout.o lib/aso.o wc.o fs_pool.o
 
 #
 # Netdev basic
drivers/net/ethernet/mellanox/mlx5/core/main.c +18 -0
index 710633d5fdbe..e1217a8bf4db 100644
@@ -74,6 +74,7 @@
 #include "mlx5_irq.h"
 #include "hwmon.h"
 #include "lag/lag.h"
+#include "sh_devlink.h"
 
 MODULE_AUTHOR("Eli Cohen <[email protected]>");
 MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX series) core driver");
@@ -1554,10 +1555,17 @@ int mlx5_init_one(struct mlx5_core_dev *dev)
 	int err;
 
 	devl_lock(devlink);
+	if (dev->shd) {
+		err = devl_nested_devlink_set(priv_to_devlink(dev->shd),
+					      devlink);
+		if (err)
+			goto unlock;
+	}
 	devl_register(devlink);
 	err = mlx5_init_one_devl_locked(dev);
 	if (err)
 		devl_unregister(devlink);
+unlock:
 	devl_unlock(devlink);
 	return err;
 }
@@ -1998,6 +2006,13 @@ static int probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
 		goto pci_init_err;
 	}
 
+	err = mlx5_shd_init(dev);
+	if (err) {
+		mlx5_core_err(dev, "mlx5_shd_init failed with error code %d\n",
+			      err);
+		goto shd_init_err;
+	}
+
 	err = mlx5_init_one(dev);
 	if (err) {
 		mlx5_core_err(dev, "mlx5_init_one failed with error code %d\n",
@@ -2009,6 +2024,8 @@ static int probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
 	return 0;
 
 err_init_one:
+	mlx5_shd_uninit(dev);
+shd_init_err:
 	mlx5_pci_close(dev);
 pci_init_err:
 	mlx5_mdev_uninit(dev);
@@ -2030,6 +2047,7 @@ static void remove_one(struct pci_dev *pdev)
 	mlx5_drain_health_wq(dev);
 	mlx5_sriov_disable(pdev, false);
 	mlx5_uninit_one(dev);
+	mlx5_shd_uninit(dev);
 	mlx5_pci_close(dev);
 	mlx5_mdev_uninit(dev);
 	mlx5_adev_idx_free(dev->priv.adev_idx);
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +150 -0

150 lines not on this page · raw message

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.h new file +10 -0
new file mode 100644
index 000000000000..67df03e3c72e
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
+/* Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
+
+#ifndef __MLX5_SH_DEVLINK_H__
+#define __MLX5_SH_DEVLINK_H__
+
+int mlx5_shd_init(struct mlx5_core_dev *dev);
+void mlx5_shd_uninit(struct mlx5_core_dev *dev);
+
+#endif /* __MLX5_SH_DEVLINK_H__ */
include/linux/mlx5/driver.h +5 -0
index 46bd7550adf8..78f1f034568f 100644
@@ -721,6 +721,8 @@ enum mlx5_wc_state {
 	MLX5_WC_STATE_SUPPORTED,
 };
 
+struct mlx5_shd;
+
 struct mlx5_core_dev {
 	struct device *device;
 	enum mlx5_coredev_type coredev_type;
@@ -783,6 +785,9 @@ struct mlx5_core_dev {
 	enum mlx5_wc_state wc_state;
 	/* sync write combining state */
 	struct mutex wc_state_lock;
+	/* node in shared devlink list */
+	struct list_head shd_list;
+	struct mlx5_shd *shd;
 };
 
 struct mlx5_db {
Signature · 1 line
2.48.1
#11 lore raw
Quoted 167 lines

-----Original Message-----
From: Jiri Pirko <[email protected]>
Sent: Tuesday, March 18, 2025 5:47 AM
To: [email protected]
Cc: [email protected]; Dumazet, Eric <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Kitszel, Przemyslaw <[email protected]>; Nguyen, Anthony L <[email protected]>; [email protected]; Keller, Jacob E <[email protected]>; Knitter, Konrad <[email protected]>; [email protected]
Subject: [PATCH net-next RFC 2/3] net/mlx5: Introduce shared devlink instance for PFs on same chip

From: Jiri Pirko <[email protected]>

Multiple PFS may reside on the same physical chip, running a single firmware. Some of the resources and configurations may be shared among these PFs. Currently, there is not good object to pin the configuration knobs on.

Introduce a shared devlink, instantiated upon probe of the first PF, removed during remove of the last PF. Back this shared devlink instance by faux device, as there is no PCI device related to it.

Make the PF devlink instances nested in this shared devlink instance.

Example:

$ devlink dev
pci/0000:08:00.0:
  nested_devlink:
    auxiliary/mlx5_core.eth.0
faux/mlx5_core_83013c12b77faa1a30000c82a1045c91:
  nested_devlink:
    pci/0000:08:00.0
    pci/0000:08:00.1
auxiliary/mlx5_core.eth.0
pci/0000:08:00.1:
  nested_devlink:
    auxiliary/mlx5_core.eth.1
auxiliary/mlx5_core.eth.1
---
 .../net/ethernet/mellanox/mlx5/core/Makefile  |   4 +-
 .../net/ethernet/mellanox/mlx5/core/main.c    |  18 +++
 .../ethernet/mellanox/mlx5/core/sh_devlink.c  | 150 ++++++++++++++++++
 .../ethernet/mellanox/mlx5/core/sh_devlink.h  |  10 ++
 include/linux/mlx5/driver.h                   |   5 +
 5 files changed, 185 insertions(+), 2 deletions(-)
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.h
drivers/net/ethernet/mellanox/mlx5/core/Makefile +1 -2
index 568bbe5f83f5..510850b6e6e2 100644
@@ -16,8 +16,8 @@ mlx5_core-y :=	main.o cmd.o debugfs.o fw.o eq.o uar.o
 pagealloc.o \
 		transobj.o vport.o sriov.o fs_cmd.o fs_core.o pci_irq.o \
 		fs_counters.o fs_ft_pool.o rl.o lag/debugfs.o lag/lag.o dev.o
 events.o wq.o lib/gid.o \
 		lib/devcom.o lib/pci_vsc.o lib/dm.o lib/fs_ttc.o
 diag/fs_tracepoint.o \
-		diag/fw_tracer.o diag/crdump.o devlink.o diag/rsc_dump.o
 diag/reporter_vnic.o \
-		fw_reset.o qos.o lib/tout.o lib/aso.o wc.o fs_pool.o
+		diag/fw_tracer.o diag/crdump.o devlink.o sh_devlink.o

diag/rsc_dump.o \

drivers/net/ethernet/mellanox/mlx5/core/Makefile +1 -0
+		diag/reporter_vnic.o fw_reset.o qos.o lib/tout.o lib/aso.o wc.o

fs_pool.o

#
# Netdev basic

drivers/net/ethernet/mellanox/mlx5/core/main.c +1 -0
index 710633d5fdbe..e1217a8bf4db 100644
@@ -74,6 +74,7 @@
 #include "mlx5_irq.h"
 #include "hwmon.h"
 #include "lag/lag.h"
+#include "sh_devlink.h"
 
 MODULE_AUTHOR("Eli Cohen <[email protected]>");
 MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX

series) core driver");

drivers/net/ethernet/mellanox/mlx5/core/main.c +14 -0
@@ -1554,10 +1555,17 @@ int mlx5_init_one(struct mlx5_core_dev *dev)
 	int err;
 
 	devl_lock(devlink);
+	if (dev->shd) {
+		err = devl_nested_devlink_set(priv_to_devlink(dev->shd),
+					      devlink);
+		if (err)
+			goto unlock;
+	}
 	devl_register(devlink);
 	err = mlx5_init_one_devl_locked(dev);
 	if (err)
 		devl_unregister(devlink);
+unlock:
 	devl_unlock(devlink);
 	return err;
 }
@@ -1998,6 +2006,13 @@ static int probe_one(struct pci_dev *pdev, const struct
 pci_device_id *id)
 		goto pci_init_err;
 	}
 
+	err = mlx5_shd_init(dev);
+	if (err) {
+		mlx5_core_err(dev, "mlx5_shd_init failed with error code %d\n",
+			      err);
+		goto shd_init_err;
+	}
+
 	err = mlx5_init_one(dev);
 	if (err) {
drivers/net/ethernet/mellanox/mlx5/core/main.c +2 -0
 		mlx5_core_err(dev, "mlx5_init_one failed with error code %d\n",
@@ -2009,6 +2024,8 @@ static int probe_one(struct pci_dev *pdev, const struct
 pci_device_id *id)
 	return 0;
 
 err_init_one:
+	mlx5_shd_uninit(dev);
+shd_init_err:
 	mlx5_pci_close(dev);
 pci_init_err:
drivers/net/ethernet/mellanox/mlx5/core/main.c +1 -0
 	mlx5_mdev_uninit(dev);
@@ -2030,6 +2047,7 @@ static void remove_one(struct pci_dev *pdev)
 	mlx5_drain_health_wq(dev);
 	mlx5_sriov_disable(pdev, false);
 	mlx5_uninit_one(dev);
+	mlx5_shd_uninit(dev);
 	mlx5_pci_close(dev);
 	mlx5_mdev_uninit(dev);
 	mlx5_adev_idx_free(dev->priv.adev_idx);
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +16 -0
new file mode 100644
index 000000000000..671a3442525b
@@ -0,0 +1,150 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/* Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
+
+#include <linux/device/faux.h>
+#include <linux/mlx5/driver.h>
+#include <linux/mlx5/vport.h>
+
+#include "sh_devlink.h"
+
+static LIST_HEAD(shd_list);
+static DEFINE_MUTEX(shd_mutex); /* Protects shd_list and shd->list */
+
+/* This structure represents a shared devlink instance,
+ * there is one created for PF group of the same chip.
+ */
+struct mlx5_shd {

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 26

Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +10 -0
+	/* Node in shd list */
+	struct list_head list;
+	/* Serial number of the chip */
+	const char *sn;
+	/* List of per-PF dev instances. */
+	struct list_head dev_list;
+	/* Related faux device */
+	struct faux_device *faux_dev;
+};
+

For ice, the equivalent of this would essentially replace ice_adapter I imagine.

Quoted 100 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +9 -0
+static const struct devlink_ops mlx5_shd_ops = {
+};
+
+static int mlx5_shd_faux_probe(struct faux_device *faux_dev)
+{
+	struct devlink *devlink;
+	struct mlx5_shd *shd;
+
+	devlink = devlink_alloc(&mlx5_shd_ops, sizeof(struct mlx5_shd),

&faux_dev->dev);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +33 -0
+	if (!devlink)
+		return -ENOMEM;
+	shd = devlink_priv(devlink);
+	faux_device_set_drvdata(faux_dev, shd);
+
+	devl_lock(devlink);
+	devl_register(devlink);
+	devl_unlock(devlink);
+	return 0;
+}
+
+static void mlx5_shd_faux_remove(struct faux_device *faux_dev)
+{
+	struct mlx5_shd *shd = faux_device_get_drvdata(faux_dev);
+	struct devlink *devlink = priv_to_devlink(shd);
+
+	devl_lock(devlink);
+	devl_unregister(devlink);
+	devl_unlock(devlink);
+	devlink_free(devlink);
+}
+
+static const struct faux_device_ops mlx5_shd_faux_ops = {
+	.probe = mlx5_shd_faux_probe,
+	.remove = mlx5_shd_faux_remove,
+};
+
+static struct mlx5_shd *mlx5_shd_create(const char *sn)
+{
+	struct faux_device *faux_dev;
+	struct mlx5_shd *shd;
+
+	faux_dev = faux_device_create(THIS_MODULE, sn, NULL,

&mlx5_shd_faux_ops);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +39 -0
+	if (!faux_dev)
+		return NULL;
+	shd = faux_device_get_drvdata(faux_dev);
+	if (!shd)
+		return NULL;
+	list_add_tail(&shd->list, &shd_list);
+	shd->sn = sn;
+	INIT_LIST_HEAD(&shd->dev_list);
+	shd->faux_dev = faux_dev;
+	return shd;
+}
+
+static void mlx5_shd_destroy(struct mlx5_shd *shd)
+{
+	list_del(&shd->list);
+	kfree(shd->sn);
+	faux_device_destroy(shd->faux_dev);
+}
+
+int mlx5_shd_init(struct mlx5_core_dev *dev)
+{
+	u8 *vpd_data __free(kfree) = NULL;
+	struct pci_dev *pdev = dev->pdev;
+	unsigned int vpd_size, kw_len;
+	struct mlx5_shd *shd;
+	const char *sn;
+	char *end;
+	int start;
+	int err;
+
+	if (!mlx5_core_is_pf(dev))
+		return 0;
+
+	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
+	if (IS_ERR(vpd_data)) {
+		err = PTR_ERR(vpd_data);
+		return err == -ENODEV ? 0 : err;
+	}
+	start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, "V3",

&kw_len);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +4 -0
+	if (start < 0) {
+		/* Fall-back to SN for older devices. */
+		start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
+

PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +11 -0
+		if (start < 0)
+			return -ENOENT;
+	}
+	sn = kstrndup(vpd_data + start, kw_len, GFP_KERNEL);
+	if (!sn)
+		return -ENOMEM;
+	end = strchrnul(sn, ' ');
+	*end = '\0';
+
+	guard(mutex)(&shd_mutex);
+	list_for_each_entry(shd, &shd_list, list) {

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 132

Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +10 -0
+		if (!strcmp(shd->sn, sn)) {
+			kfree(sn);
+			goto found;
+		}
+	}
+	shd = mlx5_shd_create(sn);
+	if (!shd) {
+		kfree(sn);
+		return -ENOMEM;
+	}

How is the faux device kept in memory? I guess its reference counted somewhere? But I don't see that reference being incremented in the list_for_each.

Quoted 60 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +18 -0
+found:
+	list_add_tail(&dev->shd_list, &shd->dev_list);
+	dev->shd = shd;
+	return 0;
+}
+
+void mlx5_shd_uninit(struct mlx5_core_dev *dev)
+{
+	struct mlx5_shd *shd = dev->shd;
+
+	if (!dev->shd)
+		return;
+
+	guard(mutex)(&shd_mutex);
+	list_del(&dev->shd_list);
+	if (list_empty(&shd->dev_list))
+		mlx5_shd_destroy(shd);
+}
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.h new file +10 -0
new file mode 100644
index 000000000000..67df03e3c72e
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
+/* Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
+
+#ifndef __MLX5_SH_DEVLINK_H__
+#define __MLX5_SH_DEVLINK_H__
+
+int mlx5_shd_init(struct mlx5_core_dev *dev);
+void mlx5_shd_uninit(struct mlx5_core_dev *dev);
+
+#endif /* __MLX5_SH_DEVLINK_H__ */
include/linux/mlx5/driver.h +5 -1
index 46bd7550adf8..78f1f034568f 100644
@@ -721,6 +721,8 @@ enum mlx5_wc_state {
 	MLX5_WC_STATE_SUPPORTED,
 };
 
+struct mlx5_shd;
+
 struct mlx5_core_dev {
 	struct device *device;
 	enum mlx5_coredev_type coredev_type;
@@ -783,6 +785,9 @@ struct mlx5_core_dev {
 	enum mlx5_wc_state wc_state;
 	/* sync write combining state */
 	struct mutex wc_state_lock;
+	/* node in shared devlink list */
+	struct list_head shd_list;
+	struct mlx5_shd *shd;
 };
 
 struct mlx5_db {
--

2.48.1

#12 lore raw
Quoted 5 lines · Jacob E
Quoted 4 lines

-----Original Message-----
From: Jiri Pirko <[email protected]>

Quoted 13 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 11

Quoted 13 lines

+++ b/drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +11 -0
@@ -0,0 +1,150 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/* Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
+
+#include <linux/device/faux.h>
+#include <linux/mlx5/driver.h>
+#include <linux/mlx5/vport.h>
+
+#include "sh_devlink.h"
+
+static LIST_HEAD(shd_list);
+static DEFINE_MUTEX(shd_mutex); /* Protects shd_list and shd->list */

I essentially agree that faux_device could be used as-is, without any devlink changes, works for me.
That does not remove the need to invent the name at some point ;)

we have resolved this in similar manner, that's fine, given my understanding that you cannot let faux to dispatch for you, like:
faux_get_instance(serial_number_equivalent)

Quoted 9 lines
Quoted 9 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +9 -0
+
+/* This structure represents a shared devlink instance,
+ * there is one created for PF group of the same chip.
+ */
+struct mlx5_shd {
+	/* Node in shd list */
+	struct list_head list;
+	/* Serial number of the chip */
+	const char *sn;
Quoted 8 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 26

Quoted 6 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +6 -0
+	/* List of per-PF dev instances. */
+	struct list_head dev_list;
+	/* Related faux device */
+	struct faux_device *faux_dev;
+};
+

For ice, the equivalent of this would essentially replace ice_adapter I imagine.

or "ice_adapter will be the ice equivalent"

Quoted 2 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 27

Quoted 2 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +1 -0
+static const struct devlink_ops mlx5_shd_ops = {

please double check if there is no crash for:

$ devlink dev info the/faux/thing
Quoted 8 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c

Quoted 8 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +8 -0
+};
+
+static int mlx5_shd_faux_probe(struct faux_device *faux_dev)
+{
+	struct devlink *devlink;
+	struct mlx5_shd *shd;
+
+	devlink = devlink_alloc(&mlx5_shd_ops, sizeof(struct mlx5_shd),

sizeof(*shd)

I like that you reuse devlink_alloc(), with allocation of priv data, that suits also our needs

Quoted 11 lines
Quoted 11 lines

&faux_dev->dev);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +10 -0
+	if (!devlink)
+		return -ENOMEM;
+	shd = devlink_priv(devlink);
+	faux_device_set_drvdata(faux_dev, shd);
+
+	devl_lock(devlink);
+	devl_register(devlink);
+	devl_unlock(devlink);
+	return 0;
+}

[...]

Quoted 3 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 90

Quoted 3 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +3 -0
+int mlx5_shd_init(struct mlx5_core_dev *dev)
+{
+	u8 *vpd_data __free(kfree) = NULL;

so bad that netdev mainainers discourage __free() :( perhaps I should propose higher abstraction wrapper for it on April 1st

Quoted 4 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 94

Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +4 -0
+	struct pci_dev *pdev = dev->pdev;
+	unsigned int vpd_size, kw_len;
+	struct mlx5_shd *shd;
+	const char *sn;

I would extract name retrieval, perhaps mlx5_shd_get_name()?

Quoted 11 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 105

Quoted 11 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +11 -0
+	char *end;
+	int start;
+	int err;
+
+	if (!mlx5_core_is_pf(dev))
+		return 0;
+
+	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
+	if (IS_ERR(vpd_data)) {
+		err = PTR_ERR(vpd_data);
+		return err == -ENODEV ? 0 : err;

what? that means the shared devlink instance is something you will work properly without?

Quoted 10 lines
Quoted 10 lines
(quoted diff) +2 -0
+	}
+	start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, "V3",

&kw_len);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +4 -0
+	if (start < 0) {
+		/* Fall-back to SN for older devices. */
+		start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
+

PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +2 -0
+		if (start < 0)
+			return -ENOENT;
Quoted 8 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 121

Quoted 8 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +8 -0
+	}
+	sn = kstrndup(vpd_data + start, kw_len, GFP_KERNEL);
+	if (!sn)
+		return -ENOMEM;
+	end = strchrnul(sn, ' ');
+	*end = '\0';
+
+	guard(mutex)(&shd_mutex);

guard()() is a no-no too, per "discouraged by netdev maintainers", and here I'm on board with discouraging ;)

Quoted 5 lines
Quoted 5 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +5 -0
+	list_for_each_entry(shd, &shd_list, list) {
+		if (!strcmp(shd->sn, sn)) {
+			kfree(sn);
+			goto found;
+		}
Quoted 8 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 132

Quoted 6 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +6 -0
+	}
+	shd = mlx5_shd_create(sn);
+	if (!shd) {
+		kfree(sn);
+		return -ENOMEM;
+	}

How is the faux device kept in memory? I guess its reference counted somewhere?

get_device()/put_device() with faxu_dev->dev as argument

But I don't see that reference being incremented in the list_for_each.

Jiri keeps "the counter" as the implicit observation of shd list size :) which is protected by mutex

author
#13 lore raw
Quoted 15 lines · Wed, Mar 19, 2025 at 09:21:52AM +0100, [email protected] wrote:
Quoted 14 lines · Jacob E
Quoted 14 lines

-----Original Message-----
From: Jiri Pirko <[email protected]>

+++ b/drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +7 -0
@@ -0,0 +1,150 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/* Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
+
+#include <linux/device/faux.h>
+#include <linux/mlx5/driver.h>
+#include <linux/mlx5/vport.h>
+
Quoted 8 lines
Quoted 4 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 11

Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +4 -0
+#include "sh_devlink.h"
+
+static LIST_HEAD(shd_list);
+static DEFINE_MUTEX(shd_mutex); /* Protects shd_list and shd->list */

I essentially agree that faux_device could be used as-is, without any devlink changes, works for me.
That does not remove the need to invent the name at some point ;)

What name? Name of faux device? Sure. In case of ice it's probably PCI DSN

Quoted 4 lines

we have resolved this in similar manner, that's fine, given my understanding that you cannot let faux to dispatch for you, like:
faux_get_instance(serial_number_equivalent)

Not sure what you are asking TBH.

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +9 -0
+
+/* This structure represents a shared devlink instance,
+ * there is one created for PF group of the same chip.
+ */
+struct mlx5_shd {
+	/* Node in shd list */
+	struct list_head list;
+	/* Serial number of the chip */
+	const char *sn;
Quoted 8 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 26

Quoted 6 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +6 -0
+	/* List of per-PF dev instances. */
+	struct list_head dev_list;
+	/* Related faux device */
+	struct faux_device *faux_dev;
+};
+

For ice, the equivalent of this would essentially replace ice_adapter I imagine.

Quoted 1 line

or "ice_adapter will be the ice equivalent"

Oh yes, that makes sense.

Quoted 6 lines
Quoted 3 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 27

Quoted 3 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +1 -0
+static const struct devlink_ops mlx5_shd_ops = {

please double check if there is no crash for:

$ devlink dev info the/faux/thing

Will do, but why do you think so?

Quoted 11 lines
Quoted 9 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c

Quoted 9 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +8 -0
+};
+
+static int mlx5_shd_faux_probe(struct faux_device *faux_dev)
+{
+	struct devlink *devlink;
+	struct mlx5_shd *shd;
+
+	devlink = devlink_alloc(&mlx5_shd_ops, sizeof(struct mlx5_shd),

sizeof(*shd)

Sure.

Quoted 26 lines

I like that you reuse devlink_alloc(), with allocation of priv data, that suits also our needs

Quoted 11 lines
Quoted 11 lines

&faux_dev->dev);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +10 -0
+	if (!devlink)
+		return -ENOMEM;
+	shd = devlink_priv(devlink);
+	faux_device_set_drvdata(faux_dev, shd);
+
+	devl_lock(devlink);
+	devl_register(devlink);
+	devl_unlock(devlink);
+	return 0;
+}

[...]

Quoted 3 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 90

Quoted 3 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +3 -0
+int mlx5_shd_init(struct mlx5_core_dev *dev)
+{
+	u8 *vpd_data __free(kfree) = NULL;

so bad that netdev mainainers discourage __free() :( perhaps I should propose higher abstraction wrapper for it on April 1st

Quoted 6 lines
Quoted 4 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 94

Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +4 -0
+	struct pci_dev *pdev = dev->pdev;
+	unsigned int vpd_size, kw_len;
+	struct mlx5_shd *shd;
+	const char *sn;

I would extract name retrieval, perhaps mlx5_shd_get_name()?

I had that, did not really make the code nicer :)

Quoted 15 lines
Quoted 12 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 105

Quoted 12 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +11 -0
+	char *end;
+	int start;
+	int err;
+
+	if (!mlx5_core_is_pf(dev))
+		return 0;
+
+	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
+	if (IS_ERR(vpd_data)) {
+		err = PTR_ERR(vpd_data);
+		return err == -ENODEV ? 0 : err;

what? that means the shared devlink instance is something you will work properly without?

Not sure. This is something that should not happen for any existing device.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
(quoted diff) +2 -0
+	}
+	start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, "V3",

&kw_len);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +4 -0
+	if (start < 0) {
+		/* Fall-back to SN for older devices. */
+		start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
+

PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +5 -0
+		if (start < 0)
+			return -ENOENT;
+	}
+	sn = kstrndup(vpd_data + start, kw_len, GFP_KERNEL);
+	if (!sn)
Quoted 8 lines
Quoted 5 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 121

Quoted 5 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +5 -0
+		return -ENOMEM;
+	end = strchrnul(sn, ' ');
+	*end = '\0';
+
+	guard(mutex)(&shd_mutex);

guard()() is a no-no too, per "discouraged by netdev maintainers", and here I'm on board with discouraging ;)

That's a fight with windmills. It will happen sooner than later anyway. It is just too conventient. I don't understand why netdev has to have special treat comparing to the rest of the kernel all the time...

Quoted 16 lines
Quoted 15 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 132

Quoted 12 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +11 -0
+	list_for_each_entry(shd, &shd_list, list) {
+		if (!strcmp(shd->sn, sn)) {
+			kfree(sn);
+			goto found;
+		}
+	}
+	shd = mlx5_shd_create(sn);
+	if (!shd) {
+		kfree(sn);
+		return -ENOMEM;
+	}

How is the faux device kept in memory? I guess its reference counted somewhere?

Quoted 6 lines

get_device()/put_device() with faxu_dev->dev as argument

But I don't see that reference being incremented in the list_for_each.

Jiri keeps "the counter" as the implicit observation of shd list size :) which is protected by mutex

Yep. Why isn't that enough? I need the list anyway. Plus, I'm using the list to reference count shd, not the fauxdev. Fauxdev is explicitly create/destroyed by calling appropriate function. I belive that is the correct way (maybe the only way?) to instantiate/deinstantiate faux.

#14 lore raw
Quoted 25 lines · Jiri Pirko
Quoted 15 lines · Wed, Mar 19, 2025 at 09:21:52AM +0100, [email protected] wrote:
Quoted 14 lines · Jacob E
Quoted 14 lines

-----Original Message-----
From: Jiri Pirko <[email protected]>

+++ b/drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +7 -0
@@ -0,0 +1,150 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/* Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
+
+#include <linux/device/faux.h>
+#include <linux/mlx5/driver.h>
+#include <linux/mlx5/vport.h>
+
Quoted 8 lines
Quoted 4 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 11

Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +4 -0
+#include "sh_devlink.h"
+
+static LIST_HEAD(shd_list);
+static DEFINE_MUTEX(shd_mutex); /* Protects shd_list and shd->list */

I essentially agree that faux_device could be used as-is, without any devlink changes, works for me.
That does not remove the need to invent the name at some point ;)

Quoted 1 line

What name? Name of faux device? Sure. In case of ice it's probably PCI DSN

yes x2

Quoted 7 lines
Quoted 5 lines

we have resolved this in similar manner, that's fine, given my understanding that you cannot let faux to dispatch for you, like:
faux_get_instance(serial_number_equivalent)

Not sure what you are asking TBH.

not asking, just noticing, with the hope that Greg will notice inconvenience for drivers and perhaps think of something even better

anyway faux dev let's us resolve problems in rather elegant way, compared to what we have to do before (like this series is not even touching devlink/)

Quoted 28 lines
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +9 -0
+
+/* This structure represents a shared devlink instance,
+ * there is one created for PF group of the same chip.
+ */
+struct mlx5_shd {
+	/* Node in shd list */
+	struct list_head list;
+	/* Serial number of the chip */
+	const char *sn;
Quoted 8 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 26

Quoted 6 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +6 -0
+	/* List of per-PF dev instances. */
+	struct list_head dev_list;
+	/* Related faux device */
+	struct faux_device *faux_dev;
+};
+

For ice, the equivalent of this would essentially replace ice_adapter I imagine.

Quoted 1 line

or "ice_adapter will be the ice equivalent"

Oh yes, that makes sense.

Quoted 2 lines
Quoted 2 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 27

Quoted 1 line
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +1 -0
+static const struct devlink_ops mlx5_shd_ops = {

please double check if there is no crash for:

$ devlink dev info the/faux/thing

Will do, but why do you think so?

My local version of the ice equivalent crashes here, due to null ops pointers, despite all the checks in devlink core trying to prevent that.

[...]

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +6 -0
+	char *end;
+	int start;
+	int err;
+
+	if (!mlx5_core_is_pf(dev))
+		return 0;
Quoted 8 lines
Quoted 5 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 105

Quoted 5 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +5 -0
+
+	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
+	if (IS_ERR(vpd_data)) {
+		err = PTR_ERR(vpd_data);
+		return err == -ENODEV ? 0 : err;

what? that means the shared devlink instance is something you will work properly without?

Quoted 2 lines

Not sure. This is something that should not happen for any existing device.

then failing is a best option to notice that :)

Quoted 50 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
(quoted diff) +2 -0
+	}
+	start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, "V3",

&kw_len);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +4 -0
+	if (start < 0) {
+		/* Fall-back to SN for older devices. */
+		start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
+

PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +5 -0
+		if (start < 0)
+			return -ENOENT;
+	}
+	sn = kstrndup(vpd_data + start, kw_len, GFP_KERNEL);
+	if (!sn)
Quoted 8 lines
Quoted 5 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 121

Quoted 5 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +5 -0
+		return -ENOMEM;
+	end = strchrnul(sn, ' ');
+	*end = '\0';
+
+	guard(mutex)(&shd_mutex);

guard()() is a no-no too, per "discouraged by netdev maintainers", and here I'm on board with discouraging ;)

That's a fight with windmills. It will happen sooner than later anyway. It is just too conventient. I don't understand why netdev has to have special treat comparing to the rest of the kernel all the time...

Quoted 20 lines
Quoted 15 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 132

Quoted 12 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +11 -0
+	list_for_each_entry(shd, &shd_list, list) {
+		if (!strcmp(shd->sn, sn)) {
+			kfree(sn);
+			goto found;
+		}
+	}
+	shd = mlx5_shd_create(sn);
+	if (!shd) {
+		kfree(sn);
+		return -ENOMEM;
+	}

How is the faux device kept in memory? I guess its reference counted somewhere?

get_device()/put_device() with faxu_dev->dev as argument

But I don't see that reference being incremented in the list_for_each.

Quoted 7 lines
Quoted 2 lines

Jiri keeps "the counter" as the implicit observation of shd list size :) which is protected by mutex

Yep. Why isn't that enough? I need the list anyway. Plus, I'm using the list to reference count shd, not the fauxdev. Fauxdev is explicitly create/destroyed by calling appropriate function. I belive that is the correct way (maybe the only way?) to instantiate/deinstantiate faux.

I've just explained my reasoning why your code is correct :)

The other correct design would be to have it in the faux/ to dispatch/de-duplicate given a string or numeric ID

#15 lore raw
In reply to Jiri Pirko
Quoted 205 lines

-----Original Message-----
From: Jiri Pirko <[email protected]>
Sent: Wednesday, March 19, 2025 4:56 AM
To: Kitszel, Przemyslaw <[email protected]>
Cc: Keller, Jacob E <[email protected]>; [email protected]; [email protected]; [email protected]; Dumazet, Eric <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Nguyen, Anthony L <[email protected]>; [email protected]; Knitter, Konrad <[email protected]>; [email protected]
Subject: Re: [PATCH net-next RFC 2/3] net/mlx5: Introduce shared devlink instance for PFs on same chip

Quoted 10 lines · Wed, Mar 19, 2025 at 09:21:52AM +0100, [email protected] wrote:
Quoted 9 lines · Jacob E

Review line 2

Quoted 9 lines

-----Original Message-----
From: Jiri Pirko <[email protected]>

+++ b/drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c

(quoted diff) +2 -0
@@ -0,0 +1,150 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/* Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights

reserved. */

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +5 -0
+
+#include <linux/device/faux.h>
+#include <linux/mlx5/driver.h>
+#include <linux/mlx5/vport.h>
+
Quoted 8 lines
Quoted 4 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 11

Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +4 -0
+#include "sh_devlink.h"
+
+static LIST_HEAD(shd_list);
+static DEFINE_MUTEX(shd_mutex); /* Protects shd_list and shd->list */

I essentially agree that faux_device could be used as-is, without any devlink changes, works for me.
That does not remove the need to invent the name at some point ;)

What name? Name of faux device? Sure. In case of ice it's probably PCI DSN

Quoted 4 lines

we have resolved this in similar manner, that's fine, given my understanding that you cannot let faux to dispatch for you, like:
faux_get_instance(serial_number_equivalent)

Not sure what you are asking TBH.

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +9 -0
+
+/* This structure represents a shared devlink instance,
+ * there is one created for PF group of the same chip.
+ */
+struct mlx5_shd {
+	/* Node in shd list */
+	struct list_head list;
+	/* Serial number of the chip */
+	const char *sn;
Quoted 8 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 26

Quoted 6 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +6 -0
+	/* List of per-PF dev instances. */
+	struct list_head dev_list;
+	/* Related faux device */
+	struct faux_device *faux_dev;
+};
+

For ice, the equivalent of this would essentially replace ice_adapter I imagine.

Quoted 1 line

or "ice_adapter will be the ice equivalent"

Oh yes, that makes sense.

Quoted 6 lines
Quoted 3 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 27

Quoted 3 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +1 -0
+static const struct devlink_ops mlx5_shd_ops = {

please double check if there is no crash for:

$ devlink dev info the/faux/thing

Will do, but why do you think so?

Quoted 11 lines
Quoted 9 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c

Quoted 9 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +8 -0
+};
+
+static int mlx5_shd_faux_probe(struct faux_device *faux_dev)
+{
+	struct devlink *devlink;
+	struct mlx5_shd *shd;
+
+	devlink = devlink_alloc(&mlx5_shd_ops, sizeof(struct mlx5_shd),

sizeof(*shd)

Sure.

Quoted 26 lines

I like that you reuse devlink_alloc(), with allocation of priv data, that suits also our needs

Quoted 11 lines
Quoted 11 lines

&faux_dev->dev);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +10 -0
+	if (!devlink)
+		return -ENOMEM;
+	shd = devlink_priv(devlink);
+	faux_device_set_drvdata(faux_dev, shd);
+
+	devl_lock(devlink);
+	devl_register(devlink);
+	devl_unlock(devlink);
+	return 0;
+}

[...]

Quoted 3 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 90

Quoted 3 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +3 -0
+int mlx5_shd_init(struct mlx5_core_dev *dev)
+{
+	u8 *vpd_data __free(kfree) = NULL;

so bad that netdev mainainers discourage __free() :( perhaps I should propose higher abstraction wrapper for it on April 1st

Quoted 6 lines
Quoted 4 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 94

Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +4 -0
+	struct pci_dev *pdev = dev->pdev;
+	unsigned int vpd_size, kw_len;
+	struct mlx5_shd *shd;
+	const char *sn;

I would extract name retrieval, perhaps mlx5_shd_get_name()?

I had that, did not really make the code nicer :)

Quoted 15 lines
Quoted 12 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 105

Quoted 12 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +11 -0
+	char *end;
+	int start;
+	int err;
+
+	if (!mlx5_core_is_pf(dev))
+		return 0;
+
+	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
+	if (IS_ERR(vpd_data)) {
+		err = PTR_ERR(vpd_data);
+		return err == -ENODEV ? 0 : err;

what? that means the shared devlink instance is something you will work properly without?

Not sure. This is something that should not happen for any existing device.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
(quoted diff) +2 -0
+	}
+	start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, "V3",

&kw_len);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +4 -0
+	if (start < 0) {
+		/* Fall-back to SN for older devices. */
+		start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
+

PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +5 -0
+		if (start < 0)
+			return -ENOENT;
+	}
+	sn = kstrndup(vpd_data + start, kw_len, GFP_KERNEL);
+	if (!sn)
Quoted 8 lines
Quoted 5 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 121

Quoted 5 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +5 -0
+		return -ENOMEM;
+	end = strchrnul(sn, ' ');
+	*end = '\0';
+
+	guard(mutex)(&shd_mutex);

guard()() is a no-no too, per "discouraged by netdev maintainers", and here I'm on board with discouraging ;)

That's a fight with windmills. It will happen sooner than later anyway. It is just too conventient. I don't understand why netdev has to have special treat comparing to the rest of the kernel all the time...

Quoted 20 lines
Quoted 15 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 132

Quoted 12 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +11 -0
+	list_for_each_entry(shd, &shd_list, list) {
+		if (!strcmp(shd->sn, sn)) {
+			kfree(sn);
+			goto found;
+		}
+	}
+	shd = mlx5_shd_create(sn);
+	if (!shd) {
+		kfree(sn);
+		return -ENOMEM;
+	}

How is the faux device kept in memory? I guess its reference counted somewhere?

get_device()/put_device() with faxu_dev->dev as argument

But I don't see that reference being incremented in the list_for_each.

Quoted 7 lines
Quoted 2 lines

Jiri keeps "the counter" as the implicit observation of shd list size :) which is protected by mutex

Yep. Why isn't that enough? I need the list anyway. Plus, I'm using the list to reference count shd, not the fauxdev. Fauxdev is explicitly create/destroyed by calling appropriate function. I belive that is the correct way (maybe the only way?) to instantiate/deinstantiate faux.

Re-using the list as the count for how many users seems reasonable. I just didn't catch that's how you did it.

Thanks,
Jake

author
#16 lore raw
In reply to Keller, Jacob E
Quoted 171 lines · Tue, Mar 18, 2025 at 11:05:23PM +0100, [email protected] wrote:
Quoted 171 lines

-----Original Message-----
From: Jiri Pirko <[email protected]>
Sent: Tuesday, March 18, 2025 5:47 AM
To: [email protected]
Cc: [email protected]; Dumazet, Eric <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Kitszel, Przemyslaw <[email protected]>; Nguyen, Anthony L <[email protected]>; [email protected]; Keller, Jacob E <[email protected]>; Knitter, Konrad <[email protected]>; [email protected]
Subject: [PATCH net-next RFC 2/3] net/mlx5: Introduce shared devlink instance for PFs on same chip

From: Jiri Pirko <[email protected]>

Multiple PFS may reside on the same physical chip, running a single firmware. Some of the resources and configurations may be shared among these PFs. Currently, there is not good object to pin the configuration knobs on.

Introduce a shared devlink, instantiated upon probe of the first PF, removed during remove of the last PF. Back this shared devlink instance by faux device, as there is no PCI device related to it.

Make the PF devlink instances nested in this shared devlink instance.

Example:

$ devlink dev
pci/0000:08:00.0:
  nested_devlink:
    auxiliary/mlx5_core.eth.0
faux/mlx5_core_83013c12b77faa1a30000c82a1045c91:
  nested_devlink:
    pci/0000:08:00.0
    pci/0000:08:00.1
auxiliary/mlx5_core.eth.0
pci/0000:08:00.1:
  nested_devlink:
    auxiliary/mlx5_core.eth.1
auxiliary/mlx5_core.eth.1
---
 .../net/ethernet/mellanox/mlx5/core/Makefile  |   4 +-
 .../net/ethernet/mellanox/mlx5/core/main.c    |  18 +++
 .../ethernet/mellanox/mlx5/core/sh_devlink.c  | 150 ++++++++++++++++++
 .../ethernet/mellanox/mlx5/core/sh_devlink.h  |  10 ++
 include/linux/mlx5/driver.h                   |   5 +
 5 files changed, 185 insertions(+), 2 deletions(-)
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.h
drivers/net/ethernet/mellanox/mlx5/core/Makefile +1 -2
index 568bbe5f83f5..510850b6e6e2 100644
@@ -16,8 +16,8 @@ mlx5_core-y :=	main.o cmd.o debugfs.o fw.o eq.o uar.o
 pagealloc.o \
 		transobj.o vport.o sriov.o fs_cmd.o fs_core.o pci_irq.o \
 		fs_counters.o fs_ft_pool.o rl.o lag/debugfs.o lag/lag.o dev.o
 events.o wq.o lib/gid.o \
 		lib/devcom.o lib/pci_vsc.o lib/dm.o lib/fs_ttc.o
 diag/fs_tracepoint.o \
-		diag/fw_tracer.o diag/crdump.o devlink.o diag/rsc_dump.o
 diag/reporter_vnic.o \
-		fw_reset.o qos.o lib/tout.o lib/aso.o wc.o fs_pool.o
+		diag/fw_tracer.o diag/crdump.o devlink.o sh_devlink.o

diag/rsc_dump.o \

drivers/net/ethernet/mellanox/mlx5/core/Makefile +1 -0
+		diag/reporter_vnic.o fw_reset.o qos.o lib/tout.o lib/aso.o wc.o

fs_pool.o

#
# Netdev basic

drivers/net/ethernet/mellanox/mlx5/core/main.c +1 -0
index 710633d5fdbe..e1217a8bf4db 100644
@@ -74,6 +74,7 @@
 #include "mlx5_irq.h"
 #include "hwmon.h"
 #include "lag/lag.h"
+#include "sh_devlink.h"
 
 MODULE_AUTHOR("Eli Cohen <[email protected]>");
 MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX

series) core driver");

drivers/net/ethernet/mellanox/mlx5/core/main.c +14 -0
@@ -1554,10 +1555,17 @@ int mlx5_init_one(struct mlx5_core_dev *dev)
 	int err;
 
 	devl_lock(devlink);
+	if (dev->shd) {
+		err = devl_nested_devlink_set(priv_to_devlink(dev->shd),
+					      devlink);
+		if (err)
+			goto unlock;
+	}
 	devl_register(devlink);
 	err = mlx5_init_one_devl_locked(dev);
 	if (err)
 		devl_unregister(devlink);
+unlock:
 	devl_unlock(devlink);
 	return err;
 }
@@ -1998,6 +2006,13 @@ static int probe_one(struct pci_dev *pdev, const struct
 pci_device_id *id)
 		goto pci_init_err;
 	}
 
+	err = mlx5_shd_init(dev);
+	if (err) {
+		mlx5_core_err(dev, "mlx5_shd_init failed with error code %d\n",
+			      err);
+		goto shd_init_err;
+	}
+
 	err = mlx5_init_one(dev);
 	if (err) {
drivers/net/ethernet/mellanox/mlx5/core/main.c +2 -0
 		mlx5_core_err(dev, "mlx5_init_one failed with error code %d\n",
@@ -2009,6 +2024,8 @@ static int probe_one(struct pci_dev *pdev, const struct
 pci_device_id *id)
 	return 0;
 
 err_init_one:
+	mlx5_shd_uninit(dev);
+shd_init_err:
 	mlx5_pci_close(dev);
 pci_init_err:
drivers/net/ethernet/mellanox/mlx5/core/main.c +1 -0
 	mlx5_mdev_uninit(dev);
@@ -2030,6 +2047,7 @@ static void remove_one(struct pci_dev *pdev)
 	mlx5_drain_health_wq(dev);
 	mlx5_sriov_disable(pdev, false);
 	mlx5_uninit_one(dev);
+	mlx5_shd_uninit(dev);
 	mlx5_pci_close(dev);
 	mlx5_mdev_uninit(dev);
 	mlx5_adev_idx_free(dev->priv.adev_idx);
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +20 -0
new file mode 100644
index 000000000000..671a3442525b
@@ -0,0 +1,150 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/* Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
+
+#include <linux/device/faux.h>
+#include <linux/mlx5/driver.h>
+#include <linux/mlx5/vport.h>
+
+#include "sh_devlink.h"
+
+static LIST_HEAD(shd_list);
+static DEFINE_MUTEX(shd_mutex); /* Protects shd_list and shd->list */
+
+/* This structure represents a shared devlink instance,
+ * there is one created for PF group of the same chip.
+ */
+struct mlx5_shd {
+	/* Node in shd list */
+	struct list_head list;
+	/* Serial number of the chip */
+	const char *sn;
Quoted 8 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 26

Quoted 6 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +6 -0
+	/* List of per-PF dev instances. */
+	struct list_head dev_list;
+	/* Related faux device */
+	struct faux_device *faux_dev;
+};
+

For ice, the equivalent of this would essentially replace ice_adapter I imagine.

Yep.

Quoted 105 lines
Quoted 105 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +9 -0
+static const struct devlink_ops mlx5_shd_ops = {
+};
+
+static int mlx5_shd_faux_probe(struct faux_device *faux_dev)
+{
+	struct devlink *devlink;
+	struct mlx5_shd *shd;
+
+	devlink = devlink_alloc(&mlx5_shd_ops, sizeof(struct mlx5_shd),

&faux_dev->dev);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +33 -0
+	if (!devlink)
+		return -ENOMEM;
+	shd = devlink_priv(devlink);
+	faux_device_set_drvdata(faux_dev, shd);
+
+	devl_lock(devlink);
+	devl_register(devlink);
+	devl_unlock(devlink);
+	return 0;
+}
+
+static void mlx5_shd_faux_remove(struct faux_device *faux_dev)
+{
+	struct mlx5_shd *shd = faux_device_get_drvdata(faux_dev);
+	struct devlink *devlink = priv_to_devlink(shd);
+
+	devl_lock(devlink);
+	devl_unregister(devlink);
+	devl_unlock(devlink);
+	devlink_free(devlink);
+}
+
+static const struct faux_device_ops mlx5_shd_faux_ops = {
+	.probe = mlx5_shd_faux_probe,
+	.remove = mlx5_shd_faux_remove,
+};
+
+static struct mlx5_shd *mlx5_shd_create(const char *sn)
+{
+	struct faux_device *faux_dev;
+	struct mlx5_shd *shd;
+
+	faux_dev = faux_device_create(THIS_MODULE, sn, NULL,

&mlx5_shd_faux_ops);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +39 -0
+	if (!faux_dev)
+		return NULL;
+	shd = faux_device_get_drvdata(faux_dev);
+	if (!shd)
+		return NULL;
+	list_add_tail(&shd->list, &shd_list);
+	shd->sn = sn;
+	INIT_LIST_HEAD(&shd->dev_list);
+	shd->faux_dev = faux_dev;
+	return shd;
+}
+
+static void mlx5_shd_destroy(struct mlx5_shd *shd)
+{
+	list_del(&shd->list);
+	kfree(shd->sn);
+	faux_device_destroy(shd->faux_dev);
+}
+
+int mlx5_shd_init(struct mlx5_core_dev *dev)
+{
+	u8 *vpd_data __free(kfree) = NULL;
+	struct pci_dev *pdev = dev->pdev;
+	unsigned int vpd_size, kw_len;
+	struct mlx5_shd *shd;
+	const char *sn;
+	char *end;
+	int start;
+	int err;
+
+	if (!mlx5_core_is_pf(dev))
+		return 0;
+
+	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
+	if (IS_ERR(vpd_data)) {
+		err = PTR_ERR(vpd_data);
+		return err == -ENODEV ? 0 : err;
+	}
+	start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, "V3",

&kw_len);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +4 -0
+	if (start < 0) {
+		/* Fall-back to SN for older devices. */
+		start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
+

PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len);

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +15 -0
+		if (start < 0)
+			return -ENOENT;
+	}
+	sn = kstrndup(vpd_data + start, kw_len, GFP_KERNEL);
+	if (!sn)
+		return -ENOMEM;
+	end = strchrnul(sn, ' ');
+	*end = '\0';
+
+	guard(mutex)(&shd_mutex);
+	list_for_each_entry(shd, &shd_list, list) {
+		if (!strcmp(shd->sn, sn)) {
+			kfree(sn);
+			goto found;
+		}
Quoted 8 lines

drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c line 132

Quoted 6 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +6 -0
+	}
+	shd = mlx5_shd_create(sn);
+	if (!shd) {
+		kfree(sn);
+		return -ENOMEM;
+	}

How is the faux device kept in memory? I guess its reference counted somewhere? But I don't see that reference being incremented in the list_for_each.

There is one faux created in mlx5_shd_create() for one shd instance. Reference counting is done by &shd->dev_list. See mlx5_shd_uninit() where mlx5_shd_destroy() is called.

Quoted 62 lines
Quoted 61 lines
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c new file +18 -0
+found:
+	list_add_tail(&dev->shd_list, &shd->dev_list);
+	dev->shd = shd;
+	return 0;
+}
+
+void mlx5_shd_uninit(struct mlx5_core_dev *dev)
+{
+	struct mlx5_shd *shd = dev->shd;
+
+	if (!dev->shd)
+		return;
+
+	guard(mutex)(&shd_mutex);
+	list_del(&dev->shd_list);
+	if (list_empty(&shd->dev_list))
+		mlx5_shd_destroy(shd);
+}
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.h new file +10 -0
new file mode 100644
index 000000000000..67df03e3c72e
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
+/* Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
+
+#ifndef __MLX5_SH_DEVLINK_H__
+#define __MLX5_SH_DEVLINK_H__
+
+int mlx5_shd_init(struct mlx5_core_dev *dev);
+void mlx5_shd_uninit(struct mlx5_core_dev *dev);
+
+#endif /* __MLX5_SH_DEVLINK_H__ */
include/linux/mlx5/driver.h +5 -1
index 46bd7550adf8..78f1f034568f 100644
@@ -721,6 +721,8 @@ enum mlx5_wc_state {
 	MLX5_WC_STATE_SUPPORTED,
 };
 
+struct mlx5_shd;
+
 struct mlx5_core_dev {
 	struct device *device;
 	enum mlx5_coredev_type coredev_type;
@@ -783,6 +785,9 @@ struct mlx5_core_dev {
 	enum mlx5_wc_state wc_state;
 	/* sync write combining state */
 	struct mutex wc_state_lock;
+	/* node in shared devlink list */
+	struct list_head shd_list;
+	struct mlx5_shd *shd;
 };
 
 struct mlx5_db {
--

2.48.1

author
#17 lore raw

RFC PATCH 3/3 net/mlx5: Introduce enable_sriov param for shared devlink

In reply to Jiri Pirko

From: Jiri Pirko <[email protected]>

As some of the mlx5 devices only support enabling/disabling SR-IOV per-chip and not per-PF, introduce this param attached to shared devlink instance.

Example:

$ devlink dev param show faux/mlx5_core_83013c12b77faa1a30000c82a1045c91
faux/mlx5_core_83013c12b77faa1a30000c82a1045c91:
  name enable_sriov type generic
    values:
      cmode permanent value false
$ devlink dev param set faux/mlx5_core_83013c12b77faa1a30000c82a1045c91 name enable_sriov cmode permanent value true
---
 .../net/ethernet/mellanox/mlx5/core/Makefile  |   3 +-
 .../mellanox/mlx5/core/lib/nv_param.c         | 236 ++++++++++++++++++
 .../mellanox/mlx5/core/lib/nv_param.h         |  14 ++
 .../ethernet/mellanox/mlx5/core/sh_devlink.c  |  16 +-
 .../ethernet/mellanox/mlx5/core/sh_devlink.h  |   1 +
 include/linux/mlx5/driver.h                   |   1 +
 6 files changed, 269 insertions(+), 2 deletions(-)
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/lib/nv_param.c
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/lib/nv_param.h
drivers/net/ethernet/mellanox/mlx5/core/Makefile +2 -1
index 510850b6e6e2..238c212ad0fd 100644
@@ -17,7 +17,8 @@ mlx5_core-y :=	main.o cmd.o debugfs.o fw.o eq.o uar.o pagealloc.o \
 		fs_counters.o fs_ft_pool.o rl.o lag/debugfs.o lag/lag.o dev.o events.o wq.o lib/gid.o \
 		lib/devcom.o lib/pci_vsc.o lib/dm.o lib/fs_ttc.o diag/fs_tracepoint.o \
 		diag/fw_tracer.o diag/crdump.o devlink.o sh_devlink.o diag/rsc_dump.o \
-		diag/reporter_vnic.o fw_reset.o qos.o lib/tout.o lib/aso.o wc.o fs_pool.o
+		diag/reporter_vnic.o fw_reset.o qos.o lib/tout.o lib/aso.o wc.o fs_pool.o \
+		lib/nv_param.o
 
 #
 # Netdev basic
drivers/net/ethernet/mellanox/mlx5/core/lib/nv_param.c new file +236 -0

236 lines not on this page · raw message

drivers/net/ethernet/mellanox/mlx5/core/lib/nv_param.h new file +14 -0
new file mode 100644
index 000000000000..785edfe9bf15
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
+/* Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
+
+#ifndef __MLX5_NV_PARAM_H
+#define __MLX5_NV_PARAM_H
+
+#include <linux/mlx5/driver.h>
+#include "devlink.h"
+
+int mlx5_shd_nv_param_register_dl_params(struct devlink *devlink);
+void mlx5_shd_nv_param_unregister_dl_params(struct devlink *devlink);
+
+#endif
+
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c +15 -1
index 671a3442525b..1c730071f00f 100644
@@ -2,9 +2,10 @@
 /* Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
 
 #include <linux/device/faux.h>
+#include <linux/mlx5/device.h>
 #include <linux/mlx5/driver.h>
 #include <linux/mlx5/vport.h>
-
+#include "lib/nv_param.h"
 #include "sh_devlink.h"
 
 static LIST_HEAD(shd_list);
@@ -24,6 +25,12 @@ struct mlx5_shd {
 	struct faux_device *faux_dev;
 };
 
+struct mlx5_core_dev *mlx5_shd_dev(struct mlx5_shd *shd)
+{
+	return list_first_entry(&shd->dev_list,
+				struct mlx5_core_dev, shd_list);
+}
+
 static const struct devlink_ops mlx5_shd_ops = {
 };
 
@@ -31,6 +38,7 @@ static int mlx5_shd_faux_probe(struct faux_device *faux_dev)
 {
 	struct devlink *devlink;
 	struct mlx5_shd *shd;
+	int err;
 
 	devlink = devlink_alloc(&mlx5_shd_ops, sizeof(struct mlx5_shd), &faux_dev->dev);
 	if (!devlink)
@@ -39,6 +47,11 @@ static int mlx5_shd_faux_probe(struct faux_device *faux_dev)
 	faux_device_set_drvdata(faux_dev, shd);
 
 	devl_lock(devlink);
+	err = mlx5_shd_nv_param_register_dl_params(devlink);
+	if (err) {
+		devl_unlock(devlink);
+		return err;
+	}
 	devl_register(devlink);
 	devl_unlock(devlink);
 	return 0;
@@ -51,6 +64,7 @@ static void mlx5_shd_faux_remove(struct faux_device *faux_dev)
 
 	devl_lock(devlink);
 	devl_unregister(devlink);
+	mlx5_shd_nv_param_unregister_dl_params(devlink);
 	devl_unlock(devlink);
 	devlink_free(devlink);
 }
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.h +1 -0
index 67df03e3c72e..837b8a3ca54e 100644
@@ -4,6 +4,7 @@
 #ifndef __MLX5_SH_DEVLINK_H__
 #define __MLX5_SH_DEVLINK_H__
 
+struct mlx5_core_dev *mlx5_shd_dev(struct mlx5_shd *shd);
 int mlx5_shd_init(struct mlx5_core_dev *dev);
 void mlx5_shd_uninit(struct mlx5_core_dev *dev);
 
include/linux/mlx5/driver.h +1 -0
index 78f1f034568f..044a45abe614 100644
@@ -135,6 +135,7 @@ enum {
 	MLX5_REG_MTCAP		 = 0x9009,
 	MLX5_REG_MTMP		 = 0x900A,
 	MLX5_REG_MCIA		 = 0x9014,
+	MLX5_REG_MNVDA		 = 0x9024,
 	MLX5_REG_MFRL		 = 0x9028,
 	MLX5_REG_MLCR		 = 0x902b,
 	MLX5_REG_MRTC		 = 0x902d,
Signature · 1 line
2.48.1