Implement the foundation of firmware node support for PCS drivers. To support this, implement a simple provider API where a PCS driver can expose multiple PCS with a fwnode_xlate callback. The PCS driver calls fwnode_pcs_add_provider() and passes the firmware node pointer, an xlate function and context data. The xlate function returns the appropriate PCS for the firmware reference and its #pcs-cells arguments. It must return the same preallocated PCS for a given reference and arguments, and the PCS must remain valid until the provider is removed. This registers the provider in a global list so that consumers can access its PCS. The first successful lookup associates the PCS with its provider. The consumer uses fwnode_pcs_get() to get the actual PCS by passing the consumer's firmware node pointer and the index in the "pcs-handle" property. The lookup returns -ENODEV if no provider supplies a PCS for that reference. For a simple implementation where #pcs-cells is 0 and the PCS driver exposes a single PCS, the xlate function fwnode_pcs_simple_xlate() is provided. For an advanced implementation, a custom xlate function is required. On removal, the PCS driver calls fwnode_pcs_del_provider() to remove an unmanaged provider from the provider list and clear its association with its PCS. A devm variant, devm_fwnode_pcs_add_provider(), is provided to automate the release procedure. Generic functions fwnode_phylink_pcs_count() and fwnode_phylink_pcs_parse() are provided for MAC drivers that declare PCS in DT or ACPI. Function fwnode_phylink_pcs_count() parses the "pcs-handle" property and returns the number of PCS references in the passed firmware node, regardless of whether the corresponding PCS are available. Function fwnode_phylink_pcs_parse() parses up to num_pcs entries in the "pcs-handle" property, fills the passed available_pcs array with the PCS it finds and returns the number stored. It skips lookups that return -ENODEV, stops at -ENOENT and propagates other lookup errors. It returns -ENODEV if the "pcs-handle" property is absent. Co-developed-by: Daniel Golle <dani…@makrotopia.org> Signed-off-by: Daniel Golle <dani…@makrotopia.org> Signed-off-by: Christian Marangi <ansu…@gmail.com> --- drivers/net/pcs/Kconfig | 6 + drivers/net/pcs/Makefile | 1 + drivers/net/pcs/pcs.c | 279 +++++++++++++++++++++++++++++++ include/linux/pcs/pcs-provider.h | 72 ++++++++ include/linux/pcs/pcs.h | 74 ++++++++ include/linux/phylink.h | 3 + 6 files changed, 435 insertions(+) create mode 100644 drivers/net/pcs/pcs.c create mode 100644 include/linux/pcs/pcs-provider.h create mode 100644 include/linux/pcs/pcs.h diff --git a/drivers/net/pcs/Kconfig b/drivers/net/pcs/Kconfig index 108d47257bee..3d4d81c8e098 100644 --- a/drivers/net/pcs/Kconfig +++ b/drivers/net/pcs/Kconfig @@ -5,6 +5,12 @@ menu "PCS device drivers" +config FWNODE_PCS + bool "PCS Firmware Node" + depends on ACPI || OF || COMPILE_TEST + help + Firmware node PCS accessors + config PCS_XPCS tristate "Synopsys DesignWare Ethernet XPCS" select PHYLINK diff --git a/drivers/net/pcs/Makefile b/drivers/net/pcs/Makefile index f9f6cf2578d7..53ba1d560f69 100644 --- a/drivers/net/pcs/Makefile +++ b/drivers/net/pcs/Makefile @@ -3,6 +3,7 @@ pcs_xpcs-$(CONFIG_PCS_XPCS) := pcs-xpcs.o pcs-xpcs-nxp.o pcs-xpcs-wx.o +obj-$(CONFIG_FWNODE_PCS) += pcs.o obj-$(CONFIG_PCS_XPCS) += pcs_xpcs.o obj-$(CONFIG_PCS_XPCS_PLATFORM) += pcs-xpcs-plat.o obj-$(CONFIG_PCS_XPCS_ROCKCHIP) += pcs-xpcs-rk.o diff --git a/drivers/net/pcs/pcs.c b/drivers/net/pcs/pcs.c new file mode 100644 index 000000000000..a0b3681db54b --- /dev/null +++ b/drivers/net/pcs/pcs.c @@ -0,0 +1,279 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include <linux/mutex.h> +#include <linux/property.h> +#include <linux/phylink.h> +#include <linux/pcs/pcs.h> +#include <linux/pcs/pcs-provider.h> + +MODULE_DESCRIPTION("PCS library"); +MODULE_AUTHOR("Christian Marangi <ansu…@gmail.com>"); +MODULE_LICENSE("GPL"); + +struct fwnode_pcs_provider { + struct list_head link; + struct list_head pcss; + + struct fwnode_handle *fwnode; + struct phylink_pcs *(*fwnode_xlate)(struct fwnode_reference_args *pcsspec, + void *data); + + void *data; +}; + +static LIST_HEAD(fwnode_pcs_providers); +static DEFINE_MUTEX(fwnode_pcs_mutex); + +struct phylink_pcs *fwnode_pcs_simple_xlate(struct fwnode_reference_args *pcsspec, + void *data) +{ + return data; +} +EXPORT_SYMBOL_GPL(fwnode_pcs_simple_xlate); + +struct fwnode_pcs_provider * +fwnode_pcs_add_provider(struct fwnode_handle *fwnode, + struct phylink_pcs *(*fwnode_xlate)(struct fwnode_reference_args *pcsspec, + void *data), + void *data) +{ + struct fwnode_pcs_provider *pp; + + if (!fwnode) + return ERR_PTR(-EINVAL); + + pp = kzalloc_obj(*pp); + if (!pp) + return ERR_PTR(-ENOMEM); + + pp->fwnode = fwnode_handle_get(fwnode); + pp->data = data; + pp->fwnode_xlate = fwnode_xlate; + INIT_LIST_HEAD(&pp->pcss); + + mutex_lock(&fwnode_pcs_mutex); + + list_add(&pp->link, &fwnode_pcs_providers); + fwnode_dev_initialized(fwnode, true); + + mutex_unlock(&fwnode_pcs_mutex); + + pr_debug("Added pcs provider from %pfwf\n", fwnode); + + return pp; +} +EXPORT_SYMBOL_GPL(fwnode_pcs_add_provider); + +static void fwnode_pcs_free_provider(struct fwnode_pcs_provider *pp) +{ + struct phylink_pcs *pcs, *tmp; + + mutex_lock(&fwnode_pcs_mutex); + list_for_each_entry_safe(pcs, tmp, &pp->pcss, provider_list) { + list_del(&pcs->provider_list); + pcs->provider = NULL; + } + mutex_unlock(&fwnode_pcs_mutex); + + fwnode_handle_put(pp->fwnode); + kfree(pp); +} + +void fwnode_pcs_del_provider(struct fwnode_pcs_provider *pp) +{ + if (IS_ERR_OR_NULL(pp)) + return; + + mutex_lock(&fwnode_pcs_mutex); + + list_del(&pp->link); + fwnode_dev_initialized(pp->fwnode, false); + + mutex_unlock(&fwnode_pcs_mutex); + + fwnode_pcs_free_provider(pp); +} +EXPORT_SYMBOL_GPL(fwnode_pcs_del_provider); + +static void devm_fwnode_pcs_del(struct device *dev, void *res) +{ + struct fwnode_pcs_provider *pp = *(struct fwnode_pcs_provider **)res; + + fwnode_pcs_del_provider(pp); +} + +struct fwnode_pcs_provider * +devm_fwnode_pcs_add_provider(struct device *dev, struct fwnode_handle *fwnode, + struct phylink_pcs *(*fwnode_xlate)(struct fwnode_reference_args *pcsspec, + void *data), + void *data) +{ + struct fwnode_pcs_provider **ptr, *pp; + + ptr = devres_alloc(devm_fwnode_pcs_del, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return ERR_PTR(-ENOMEM); + + pp = fwnode_pcs_add_provider(fwnode, fwnode_xlate, data); + + if (!IS_ERR(pp)) { + *ptr = pp; + devres_add(dev, ptr); + } else { + devres_free(ptr); + } + + return pp; +} +EXPORT_SYMBOL_GPL(devm_fwnode_pcs_add_provider); + +static int fwnode_parse_pcsspec(const struct fwnode_handle *fwnode, + unsigned int index, const char *name, + struct fwnode_reference_args *out_args) +{ + if (!fwnode) + return -EINVAL; + + if (name) { + int ret; + + ret = fwnode_property_match_string(fwnode, "pcs-handle-names", + name); + if (ret < 0) + return ret; + + index = ret; + } + + return fwnode_property_get_reference_args(fwnode, "pcs-handle", + "#pcs-cells", 0, index, + out_args); +} + +static struct phylink_pcs * +__fwnode_pcs_get_from_pcsspec_provider(struct fwnode_reference_args *pcsspec, + struct fwnode_pcs_provider *provider) +{ + struct phylink_pcs *pcs; + + lockdep_assert_held(&fwnode_pcs_mutex); + + if (provider->fwnode != pcsspec->fwnode) + return ERR_PTR(-EINVAL); + + pcs = provider->fwnode_xlate(pcsspec, provider->data); + if (IS_ERR_OR_NULL(pcs)) + return pcs; + + /* Track provider ownership on first lookup */ + if (pcs->provider != provider) { + if (pcs->provider) + return ERR_PTR(-EBUSY); + + pcs->provider = provider; + list_add_tail(&pcs->provider_list, &provider->pcss); + } + + return pcs; +} + +static struct phylink_pcs * +fwnode_pcs_get_from_pcsspec(struct fwnode_reference_args *pcsspec) +{ + struct fwnode_pcs_provider *provider; + struct phylink_pcs *pcs = NULL; + + if (!pcsspec) + return ERR_PTR(-EINVAL); + + mutex_lock(&fwnode_pcs_mutex); + list_for_each_entry(provider, &fwnode_pcs_providers, link) { + pcs = __fwnode_pcs_get_from_pcsspec_provider(pcsspec, provider); + if (!IS_ERR(pcs)) + break; + } + mutex_unlock(&fwnode_pcs_mutex); + + return !IS_ERR_OR_NULL(pcs) ? pcs : ERR_PTR(-ENODEV); +} + +static struct phylink_pcs *__fwnode_pcs_get(const struct fwnode_handle *fwnode, + unsigned int index, const char *con_id) +{ + struct fwnode_reference_args pcsspec; + struct phylink_pcs *pcs; + int ret; + + ret = fwnode_parse_pcsspec(fwnode, index, con_id, &pcsspec); + if (ret) + return ERR_PTR(ret); + + pcs = fwnode_pcs_get_from_pcsspec(&pcsspec); + fwnode_handle_put(pcsspec.fwnode); + + return pcs; +} + +struct phylink_pcs *fwnode_pcs_get(const struct fwnode_handle *fwnode, unsigned int index) +{ + return __fwnode_pcs_get(fwnode, index, NULL); +} +EXPORT_SYMBOL_GPL(fwnode_pcs_get); + +unsigned int fwnode_phylink_pcs_count(struct fwnode_handle *fwnode) +{ + struct fwnode_reference_args out_args; + unsigned int index = 0; + int ret; + + while (true) { + ret = fwnode_property_get_reference_args(fwnode, "pcs-handle", + "#pcs-cells", 0, index, + &out_args); + /* We expect to reach an -ENOENT error while counting */ + if (ret) + break; + + fwnode_handle_put(out_args.fwnode); + index++; + } + + return index; +} +EXPORT_SYMBOL_GPL(fwnode_phylink_pcs_count); + +int fwnode_phylink_pcs_parse(struct fwnode_handle *fwnode, + struct phylink_pcs **available_pcs, + unsigned int num_pcs) +{ + unsigned int i, found = 0; + + if (!available_pcs) + return -EINVAL; + + if (!fwnode_property_present(fwnode, "pcs-handle")) + return -ENODEV; + + for (i = 0; i < num_pcs; i++) { + struct phylink_pcs *pcs; + + pcs = fwnode_pcs_get(fwnode, i); + if (IS_ERR(pcs)) { + /* Exit early if no PCS remain */ + if (PTR_ERR(pcs) == -ENOENT) + break; + + /* Skip references that did not resolve to a PCS */ + if (PTR_ERR(pcs) == -ENODEV) + continue; + + return PTR_ERR(pcs); + } + + available_pcs[found] = pcs; + found++; + } + + return found; +} +EXPORT_SYMBOL_GPL(fwnode_phylink_pcs_parse); diff --git a/include/linux/pcs/pcs-provider.h b/include/linux/pcs/pcs-provider.h new file mode 100644 index 000000000000..6b03a635f73c --- /dev/null +++ b/include/linux/pcs/pcs-provider.h @@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#ifndef __LINUX_PCS_PROVIDER_H +#define __LINUX_PCS_PROVIDER_H + +struct fwnode_pcs_provider; + +/** + * fwnode_pcs_simple_xlate - Simple xlate function to retrieve PCS + * @pcsspec: reference arguments + * @data: Context data (assumed assigned to the single PCS) + * + * Returns: the PCS pointed by data. + */ +struct phylink_pcs *fwnode_pcs_simple_xlate(struct fwnode_reference_args *pcsspec, + void *data); + +/** + * fwnode_pcs_add_provider - Registers a new PCS provider + * @fwnode: Firmware node + * @fwnode_xlate: xlate function to retrieve the PCS + * @data: Context data + * + * Register and add a new PCS provider to the global providers list + * for the firmware node. The relevant PCS from the PCS provider + * is retrieved from the passed fwnode_xlate function. + * + * The xlate function MUST return a pointer to an existing, already-allocated + * struct phylink_pcs and MUST NOT dynamically allocate a new PCS. + * The same PCS object must be returned for a given firmware reference and args, + * as PCS pointer identity is used to associate PCS objects with their provider. + * A PCS object belongs to one provider and must remain valid until that + * provider is removed. + * + * Returns: A pointer to the registered PCS provider on success, or + * an ERR_PTR() encoded error code on failure. + */ +struct fwnode_pcs_provider * +fwnode_pcs_add_provider(struct fwnode_handle *fwnode, + struct phylink_pcs *(*fwnode_xlate)(struct fwnode_reference_args *pcsspec, + void *data), + void *data); + +/** + * fwnode_pcs_del_provider - Removes a PCS provider + * @pp: PCS provider returned by fwnode_pcs_add_provider() + */ +void fwnode_pcs_del_provider(struct fwnode_pcs_provider *pp); + +/** + * devm_fwnode_pcs_add_provider - Registers a new PCS provider + * @dev: Device of the PCS provider + * @fwnode: Firmware node + * @fwnode_xlate: xlate function to retrieve the PCS + * @data: Context data + * + * Register and add a new PCS provider to the global providers list + * for the firmware node. The relevant PCS from the PCS provider + * is retrieved from the passed fwnode_xlate function. While at that, it + * also associates the device with the PCS provider using devres. + * On driver detach, release function is invoked on the devres data, + * then, devres data is freed. + * + * Returns: A pointer to the registered PCS provider on success, or + * an ERR_PTR() encoded error code on failure. + */ +struct fwnode_pcs_provider * +devm_fwnode_pcs_add_provider(struct device *dev, struct fwnode_handle *fwnode, + struct phylink_pcs *(*fwnode_xlate)(struct fwnode_reference_args *pcsspec, + void *data), + void *data); + +#endif /* __LINUX_PCS_PROVIDER_H */ diff --git a/include/linux/pcs/pcs.h b/include/linux/pcs/pcs.h new file mode 100644 index 000000000000..b3bb158002d5 --- /dev/null +++ b/include/linux/pcs/pcs.h @@ -0,0 +1,74 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#ifndef __LINUX_PCS_H +#define __LINUX_PCS_H + +#include <linux/phylink.h> + +#if IS_ENABLED(CONFIG_FWNODE_PCS) +/** + * fwnode_pcs_get - Retrieves a PCS from a firmware node + * @fwnode: firmware node + * @index: index fwnode PCS handle in firmware node + * + * Get a PCS from the firmware node at index. + * + * Returns: A pointer to the PCS on success, or ERR_PTR(-ENODEV) if + * no registered provider supplies a PCS for the reference. Provider + * translation errors are reported to the caller as -ENODEV. Errors + * parsing the firmware reference are returned unchanged. + */ +struct phylink_pcs *fwnode_pcs_get(const struct fwnode_handle *fwnode, + unsigned int index); + +/** + * fwnode_phylink_pcs_count - Count PCS entries described in firmware node + * @fwnode: firmware node + * + * Helper function to count the number of PCS entries referenced by the + * "pcs-handle" property in a firmware node. + * + * Note that this function counts all PCS references in the firmware node, + * regardless of whether the corresponding PCS devices are already probed. + * + * Returns: number of PCS entries described in the firmware node. + */ +unsigned int fwnode_phylink_pcs_count(struct fwnode_handle *fwnode); + +/** + * fwnode_phylink_pcs_parse - Parse available PCS from firmware node + * @fwnode: firmware node + * @available_pcs: pointer to preallocated array of PCS + * @num_pcs: maximum number of PCS entries to scan + * + * Helper function that parses PCS references from the "pcs-handle" + * property of a firmware node and fills @available_pcs with PCS that are + * currently available up to @num_pcs. + * + * References for which fwnode_pcs_get() returns -ENODEV are skipped. + * + * Returns: number of PCS stored in @available_pcs, or negative error code. + */ +int fwnode_phylink_pcs_parse(struct fwnode_handle *fwnode, + struct phylink_pcs **available_pcs, + unsigned int num_pcs); +#else +static inline struct phylink_pcs *fwnode_pcs_get(const struct fwnode_handle *fwnode, + unsigned int index) +{ + return ERR_PTR(-ENOENT); +} + +static inline unsigned int fwnode_phylink_pcs_count(struct fwnode_handle *fwnode) +{ + return 0; +} + +static inline int fwnode_phylink_pcs_parse(struct fwnode_handle *fwnode, + struct phylink_pcs **available_pcs, + unsigned int num_pcs) +{ + return -EOPNOTSUPP; +} +#endif + +#endif /* __LINUX_PCS_H */ diff --git a/include/linux/phylink.h b/include/linux/phylink.h index a56289ecee58..9ce66b034437 100644 --- a/include/linux/phylink.h +++ b/include/linux/phylink.h @@ -10,6 +10,7 @@ struct device_node; struct ethtool_cmd; struct fwnode_handle; +struct fwnode_pcs_provider; struct net_device; struct phylink; struct phylink_pcs; @@ -513,6 +514,8 @@ struct phylink_pcs { /* private: */ struct list_head list; + struct fwnode_pcs_provider *provider; + struct list_head provider_list; }; /**
-- 2.55.0