/* SPDX-License-Identifier: GPL-2.0 */
/*
 * PHY device list allow maintaining a list of PHY devices that are
 * part of a netdevice's link topology. PHYs can for example be chained,
 * as is the case when using a PHY that exposes an SFP module, on which an
 * SFP transceiver that embeds a PHY is connected.
 *
 * This list can then be used by userspace to leverage individual PHY
 * capabilities.
 */
#ifndef __PHY_LINK_TOPOLOGY_H
#define __PHY_LINK_TOPOLOGY_H

#include <linux/ethtool.h>
#include <linux/netdevice.h>
#include <linux/rtnetlink.h>

struct xarray;
struct phy_device;
struct phy_port;
struct sfp_bus;

struct phy_link_topology {
	struct xarray phys;
	u32 next_phy_index;

	struct xarray ports;
	u32 next_port_index;
};

struct phy_device_node {
	enum phy_upstream upstream_type;

	union {
		struct net_device	*netdev;
		struct phy_device	*phydev;
	} upstream;

	struct sfp_bus *parent_sfp_bus;

	struct phy_device *phy;
};

static inline bool phy_link_topo_empty(struct net_device *dev)
{
	return !dev->link_topo;
}

#if IS_ENABLED(CONFIG_PHYLIB)
int phy_link_topo_add_phy(struct net_device *dev,
			  struct phy_device *phy,
			  enum phy_upstream upt, void *upstream);

void phy_link_topo_del_phy(struct net_device *dev, struct phy_device *phy);

int phy_link_topo_add_port(struct net_device *dev, struct phy_port *port);
void phy_link_topo_del_port(struct net_device *dev, struct phy_port *port);

static inline struct phy_device *
phy_link_topo_get_phy(struct net_device *dev, u32 phyindex)
{
	struct phy_link_topology *topo = dev->link_topo;
	struct phy_device_node *pdn;

	if (!topo)
		return NULL;

	pdn = xa_load(&topo->phys, phyindex);
	if (pdn)
		return pdn->phy;

	return NULL;
}

static inline struct phy_port *
phy_link_topo_get_port(struct net_device *dev, u32 port_id)
{
	struct phy_link_topology *topo = dev->link_topo;

	if (!topo)
		return NULL;

	/* ports in the topo are RTNL protected, but may be accessed under
	 * netdev_lock for ops-locked devices. For now keep them under rtnl
	 * protection, as no ops-locked devices have phy_port capabilities
	 * yet.
	 */
	ASSERT_RTNL();

	return xa_load(&topo->ports, port_id);
}

#else
static inline int phy_link_topo_add_phy(struct net_device *dev,
					struct phy_device *phy,
					enum phy_upstream upt, void *upstream)
{
	return 0;
}

static inline void phy_link_topo_del_phy(struct net_device *dev,
					 struct phy_device *phy)
{
}

static inline int phy_link_topo_add_port(struct net_device *dev,
					 struct phy_port *port)
{
	return 0;
}

static inline void phy_link_topo_del_port(struct net_device *dev,
					  struct phy_port *port)
{
}

static inline struct phy_device *
phy_link_topo_get_phy(struct net_device *dev, u32 phyindex)
{
	return NULL;
}

static inline struct phy_port *
phy_link_topo_get_port(struct net_device *dev, u32 port_id)
{
	return NULL;
}
#endif

#endif /* __PHY_LINK_TOPOLOGY_H */