summaryrefslogtreecommitdiff
path: root/drivers/net/phy/phy_link_topology.c
blob: 214ccce23cf69168ab505d288d2bad067331f10e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// SPDX-License-Identifier: GPL-2.0+
/*
 * Infrastructure to handle all PHY devices connected to a given netdev,
 * either directly or indirectly attached.
 *
 * Copyright (c) 2023 Maxime Chevallier<maxime.chevallier@bootlin.com>
 */

#include <linux/phy_link_topology.h>
#include <linux/phy_port.h>
#include <linux/phy.h>
#include <linux/rtnetlink.h>
#include <linux/xarray.h>
#include <net/netdev_lock.h>

static int netdev_alloc_phy_link_topology(struct net_device *dev)
{
	struct phy_link_topology *topo;

	topo = kzalloc_obj(*topo);
	if (!topo)
		return -ENOMEM;

	xa_init_flags(&topo->phys, XA_FLAGS_ALLOC1);
	topo->next_phy_index = 1;

	xa_init_flags(&topo->ports, XA_FLAGS_ALLOC1);
	topo->next_port_index = 1;

	dev->link_topo = topo;

	return 0;
}

static struct phy_link_topology *phy_link_topo_get_or_alloc(struct net_device *dev)
{
	int ret;

	if (dev->link_topo)
		return dev->link_topo;

	/* The topology is allocated the first time we add an object to it.
	 * It is freed alongside the netdev. It can be called on multiple
	 * contexts:
	 *  - It can be called from .probe() : No rtnl, no netdev_lock
	 *  - .ndo_open() : rtnl and possibly netdev_lock
	 *  - SFP state machine : rtnl held or not
	 *
	 *  However, we can't really have races :
	 *  - If we have a PHY, phy_link_topo_add_phy() will always run first
	 *    and trigger the alloc. Only then the ports can be added through
	 *    phylib or sfp.
	 *  - If we don't, the SFP port for the cage is registered first, and
	 *    only then other ports/PHYs can be registered.
	 */
	ret = netdev_alloc_phy_link_topology(dev);
	if (ret)
		return ERR_PTR(ret);

	return dev->link_topo;
}

int phy_link_topo_add_port(struct net_device *dev, struct phy_port *port)
{
	struct phy_link_topology *topo;
	int ret;

	/* Ports can now be queried without rtnl for ops-locked devices, which
	 * we don't support now as port retrieval is done under rtnl.
	 * We don't have phy_port enabled devices yet, let's make sure
	 * we are loudly warned about that when it happens.
	 */
	if (WARN_ON_ONCE(dev->netdev_ops && netdev_need_ops_lock(dev)))
		return -EOPNOTSUPP;

	topo = phy_link_topo_get_or_alloc(dev);
	if (IS_ERR(topo))
		return PTR_ERR(topo);

	/* Attempt to re-use a previously allocated port_id */
	if (port->id)
		ret = xa_insert(&topo->ports, port->id, port, GFP_KERNEL);
	else
		ret = xa_alloc_cyclic(&topo->ports, &port->id, port,
				      xa_limit_32b, &topo->next_port_index,
				      GFP_KERNEL);

	return ret < 0 ? ret : 0;
}
EXPORT_SYMBOL_GPL(phy_link_topo_add_port);

void phy_link_topo_del_port(struct net_device *dev, struct phy_port *port)
{
	struct phy_link_topology *topo = dev->link_topo;

	if (!topo)
		return;

	xa_erase(&topo->ports, port->id);
}
EXPORT_SYMBOL_GPL(phy_link_topo_del_port);

int phy_link_topo_add_phy(struct net_device *dev,
			  struct phy_device *phy,
			  enum phy_upstream upt, void *upstream)
{
	struct phy_link_topology *topo;
	struct phy_device_node *pdn;
	struct phy_port *port;
	int ret;

	/* ethtool ops may run without rtnl_lock, and rtnl_lock is what
	 * currently protects the PHY topology. No driver currently mixes
	 * the two, flag if someone tries. See also:
	 *  - ethnl_req_get_phydev()
	 *  - phy_detach()
	 */
	if (WARN_ON_ONCE(netdev_need_ops_lock(dev)))
		return -EOPNOTSUPP;

	topo = phy_link_topo_get_or_alloc(dev);
	if (IS_ERR(topo))
		return PTR_ERR(topo);

	pdn = kzalloc_obj(*pdn);
	if (!pdn)
		return -ENOMEM;

	pdn->phy = phy;
	switch (upt) {
	case PHY_UPSTREAM_MAC:
		pdn->upstream.netdev = (struct net_device *)upstream;
		if (phy_on_sfp(phy))
			pdn->parent_sfp_bus = pdn->upstream.netdev->sfp_bus;
		break;
	case PHY_UPSTREAM_PHY:
		pdn->upstream.phydev = (struct phy_device *)upstream;
		if (phy_on_sfp(phy))
			pdn->parent_sfp_bus = pdn->upstream.phydev->sfp_bus;
		break;
	default:
		ret = -EINVAL;
		goto err;
	}
	pdn->upstream_type = upt;

	/* Attempt to re-use a previously allocated phy_index */
	if (phy->phyindex)
		ret = xa_insert(&topo->phys, phy->phyindex, pdn, GFP_KERNEL);
	else
		ret = xa_alloc_cyclic(&topo->phys, &phy->phyindex, pdn,
				      xa_limit_32b, &topo->next_phy_index,
				      GFP_KERNEL);

	if (ret < 0)
		goto err;

	/* Add all the PHY's ports to the topology */
	list_for_each_entry(port, &phy->ports, head) {
		ret = phy_link_topo_add_port(dev, port);
		if (ret)
			goto del_ports;
	}

	return 0;

del_ports:
	list_for_each_entry_continue_reverse(port, &phy->ports, head)
		phy_link_topo_del_port(dev, port);

	xa_erase(&topo->phys, phy->phyindex);
err:
	kfree(pdn);
	return ret;
}
EXPORT_SYMBOL_GPL(phy_link_topo_add_phy);

void phy_link_topo_del_phy(struct net_device *dev,
			   struct phy_device *phy)
{
	struct phy_link_topology *topo = dev->link_topo;
	struct phy_device_node *pdn;
	struct phy_port *port;

	if (!topo)
		return;

	list_for_each_entry(port, &phy->ports, head)
		phy_link_topo_del_port(dev, port);

	pdn = xa_erase(&topo->phys, phy->phyindex);

	/* We delete the PHY from the topology, however we don't re-set the
	 * phy->phyindex field. If the PHY isn't gone, we can re-assign it the
	 * same index next time it's added back to the topology
	 */

	kfree(pdn);
}
EXPORT_SYMBOL_GPL(phy_link_topo_del_phy);