Linux Audio

Check our new training course

Loading...
v4.17
 
  1/*
  2 * Distributed Switch Architecture loopback driver
  3 *
  4 * Copyright (C) 2016, Florian Fainelli <f.fainelli@gmail.com>
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License as published by
  8 * the Free Software Foundation; either version 2 of the License, or
  9 * (at your option) any later version.
 10 */
 11
 12#include <linux/platform_device.h>
 13#include <linux/netdevice.h>
 14#include <linux/phy.h>
 15#include <linux/phy_fixed.h>
 16#include <linux/export.h>
 17#include <linux/ethtool.h>
 18#include <linux/workqueue.h>
 19#include <linux/module.h>
 20#include <linux/if_bridge.h>
 21#include <net/dsa.h>
 22
 23#include "dsa_loop.h"
 24
 25struct dsa_loop_vlan {
 26	u16 members;
 27	u16 untagged;
 28};
 29
 30struct dsa_loop_mib_entry {
 31	char name[ETH_GSTRING_LEN];
 32	unsigned long val;
 33};
 34
 35enum dsa_loop_mib_counters {
 36	DSA_LOOP_PHY_READ_OK,
 37	DSA_LOOP_PHY_READ_ERR,
 38	DSA_LOOP_PHY_WRITE_OK,
 39	DSA_LOOP_PHY_WRITE_ERR,
 40	__DSA_LOOP_CNT_MAX,
 41};
 42
 43static struct dsa_loop_mib_entry dsa_loop_mibs[] = {
 44	[DSA_LOOP_PHY_READ_OK]	= { "phy_read_ok", },
 45	[DSA_LOOP_PHY_READ_ERR]	= { "phy_read_err", },
 46	[DSA_LOOP_PHY_WRITE_OK] = { "phy_write_ok", },
 47	[DSA_LOOP_PHY_WRITE_ERR] = { "phy_write_err", },
 48};
 49
 50struct dsa_loop_port {
 51	struct dsa_loop_mib_entry mib[__DSA_LOOP_CNT_MAX];
 52};
 53
 54#define DSA_LOOP_VLANS	5
 55
 56struct dsa_loop_priv {
 57	struct mii_bus	*bus;
 58	unsigned int	port_base;
 59	struct dsa_loop_vlan vlans[DSA_LOOP_VLANS];
 60	struct net_device *netdev;
 61	struct dsa_loop_port ports[DSA_MAX_PORTS];
 62	u16 pvid;
 63};
 64
 65static struct phy_device *phydevs[PHY_MAX_ADDR];
 66
 67static enum dsa_tag_protocol dsa_loop_get_protocol(struct dsa_switch *ds,
 68						   int port)
 69{
 70	dev_dbg(ds->dev, "%s\n", __func__);
 71
 72	return DSA_TAG_PROTO_NONE;
 73}
 74
 75static int dsa_loop_setup(struct dsa_switch *ds)
 76{
 77	struct dsa_loop_priv *ps = ds->priv;
 78	unsigned int i;
 79
 80	for (i = 0; i < ds->num_ports; i++)
 81		memcpy(ps->ports[i].mib, dsa_loop_mibs,
 82		       sizeof(dsa_loop_mibs));
 83
 84	dev_dbg(ds->dev, "%s\n", __func__);
 85
 86	return 0;
 87}
 88
 89static int dsa_loop_get_sset_count(struct dsa_switch *ds, int port)
 90{
 
 
 
 91	return __DSA_LOOP_CNT_MAX;
 92}
 93
 94static void dsa_loop_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
 
 95{
 96	struct dsa_loop_priv *ps = ds->priv;
 97	unsigned int i;
 98
 
 
 
 99	for (i = 0; i < __DSA_LOOP_CNT_MAX; i++)
100		memcpy(data + i * ETH_GSTRING_LEN,
101		       ps->ports[port].mib[i].name, ETH_GSTRING_LEN);
102}
103
104static void dsa_loop_get_ethtool_stats(struct dsa_switch *ds, int port,
105				       uint64_t *data)
106{
107	struct dsa_loop_priv *ps = ds->priv;
108	unsigned int i;
109
110	for (i = 0; i < __DSA_LOOP_CNT_MAX; i++)
111		data[i] = ps->ports[port].mib[i].val;
112}
113
114static int dsa_loop_phy_read(struct dsa_switch *ds, int port, int regnum)
115{
116	struct dsa_loop_priv *ps = ds->priv;
117	struct mii_bus *bus = ps->bus;
118	int ret;
119
120	dev_dbg(ds->dev, "%s\n", __func__);
121
122	ret = mdiobus_read_nested(bus, ps->port_base + port, regnum);
123	if (ret < 0)
124		ps->ports[port].mib[DSA_LOOP_PHY_READ_ERR].val++;
125	else
126		ps->ports[port].mib[DSA_LOOP_PHY_READ_OK].val++;
127
128	return ret;
129}
130
131static int dsa_loop_phy_write(struct dsa_switch *ds, int port,
132			      int regnum, u16 value)
133{
134	struct dsa_loop_priv *ps = ds->priv;
135	struct mii_bus *bus = ps->bus;
136	int ret;
137
138	dev_dbg(ds->dev, "%s\n", __func__);
139
140	ret = mdiobus_write_nested(bus, ps->port_base + port, regnum, value);
141	if (ret < 0)
142		ps->ports[port].mib[DSA_LOOP_PHY_WRITE_ERR].val++;
143	else
144		ps->ports[port].mib[DSA_LOOP_PHY_WRITE_OK].val++;
145
146	return ret;
147}
148
149static int dsa_loop_port_bridge_join(struct dsa_switch *ds, int port,
150				     struct net_device *bridge)
151{
152	dev_dbg(ds->dev, "%s\n", __func__);
 
153
154	return 0;
155}
156
157static void dsa_loop_port_bridge_leave(struct dsa_switch *ds, int port,
158				       struct net_device *bridge)
159{
160	dev_dbg(ds->dev, "%s\n", __func__);
 
161}
162
163static void dsa_loop_port_stp_state_set(struct dsa_switch *ds, int port,
164					u8 state)
165{
166	dev_dbg(ds->dev, "%s\n", __func__);
 
167}
168
169static int dsa_loop_port_vlan_filtering(struct dsa_switch *ds, int port,
170					bool vlan_filtering)
171{
172	dev_dbg(ds->dev, "%s\n", __func__);
 
173
174	return 0;
175}
176
177static int
178dsa_loop_port_vlan_prepare(struct dsa_switch *ds, int port,
179			   const struct switchdev_obj_port_vlan *vlan)
180{
181	struct dsa_loop_priv *ps = ds->priv;
182	struct mii_bus *bus = ps->bus;
183
184	dev_dbg(ds->dev, "%s\n", __func__);
 
185
186	/* Just do a sleeping operation to make lockdep checks effective */
187	mdiobus_read(bus, ps->port_base + port, MII_BMSR);
188
189	if (vlan->vid_end > DSA_LOOP_VLANS)
190		return -ERANGE;
191
192	return 0;
193}
194
195static void dsa_loop_port_vlan_add(struct dsa_switch *ds, int port,
196				   const struct switchdev_obj_port_vlan *vlan)
197{
198	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
199	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
200	struct dsa_loop_priv *ps = ds->priv;
201	struct mii_bus *bus = ps->bus;
202	struct dsa_loop_vlan *vl;
203	u16 vid;
204
205	dev_dbg(ds->dev, "%s\n", __func__);
206
207	/* Just do a sleeping operation to make lockdep checks effective */
208	mdiobus_read(bus, ps->port_base + port, MII_BMSR);
209
210	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
211		vl = &ps->vlans[vid];
212
213		vl->members |= BIT(port);
214		if (untagged)
215			vl->untagged |= BIT(port);
216		else
217			vl->untagged &= ~BIT(port);
 
 
 
218	}
219
220	if (pvid)
221		ps->pvid = vid;
222}
223
224static int dsa_loop_port_vlan_del(struct dsa_switch *ds, int port,
225				  const struct switchdev_obj_port_vlan *vlan)
226{
227	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
228	struct dsa_loop_priv *ps = ds->priv;
229	struct mii_bus *bus = ps->bus;
230	struct dsa_loop_vlan *vl;
231	u16 vid, pvid = ps->pvid;
232
233	dev_dbg(ds->dev, "%s\n", __func__);
234
235	/* Just do a sleeping operation to make lockdep checks effective */
236	mdiobus_read(bus, ps->port_base + port, MII_BMSR);
237
238	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
239		vl = &ps->vlans[vid];
240
241		vl->members &= ~BIT(port);
242		if (untagged)
243			vl->untagged &= ~BIT(port);
244
245		if (pvid == vid)
246			pvid = 1;
 
 
 
247	}
248	ps->pvid = pvid;
249
250	return 0;
251}
252
253static const struct dsa_switch_ops dsa_loop_driver = {
254	.get_tag_protocol	= dsa_loop_get_protocol,
255	.setup			= dsa_loop_setup,
256	.get_strings		= dsa_loop_get_strings,
257	.get_ethtool_stats	= dsa_loop_get_ethtool_stats,
258	.get_sset_count		= dsa_loop_get_sset_count,
 
259	.phy_read		= dsa_loop_phy_read,
260	.phy_write		= dsa_loop_phy_write,
261	.port_bridge_join	= dsa_loop_port_bridge_join,
262	.port_bridge_leave	= dsa_loop_port_bridge_leave,
263	.port_stp_state_set	= dsa_loop_port_stp_state_set,
264	.port_vlan_filtering	= dsa_loop_port_vlan_filtering,
265	.port_vlan_prepare	= dsa_loop_port_vlan_prepare,
266	.port_vlan_add		= dsa_loop_port_vlan_add,
267	.port_vlan_del		= dsa_loop_port_vlan_del,
268};
269
270static int dsa_loop_drv_probe(struct mdio_device *mdiodev)
271{
272	struct dsa_loop_pdata *pdata = mdiodev->dev.platform_data;
273	struct dsa_loop_priv *ps;
274	struct dsa_switch *ds;
275
276	if (!pdata)
277		return -ENODEV;
278
279	dev_info(&mdiodev->dev, "%s: 0x%0x\n",
280		 pdata->name, pdata->enabled_ports);
281
282	ds = dsa_switch_alloc(&mdiodev->dev, DSA_MAX_PORTS);
283	if (!ds)
284		return -ENOMEM;
285
286	ps = devm_kzalloc(&mdiodev->dev, sizeof(*ps), GFP_KERNEL);
287	if (!ps)
288		return -ENOMEM;
289
290	ps->netdev = dev_get_by_name(&init_net, pdata->netdev);
291	if (!ps->netdev)
292		return -EPROBE_DEFER;
293
294	pdata->cd.netdev[DSA_LOOP_CPU_PORT] = &ps->netdev->dev;
295
296	ds->dev = &mdiodev->dev;
297	ds->ops = &dsa_loop_driver;
298	ds->priv = ps;
299	ps->bus = mdiodev->bus;
300
301	dev_set_drvdata(&mdiodev->dev, ds);
302
303	return dsa_register_switch(ds);
304}
305
306static void dsa_loop_drv_remove(struct mdio_device *mdiodev)
307{
308	struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
309	struct dsa_loop_priv *ps = ds->priv;
310
311	dsa_unregister_switch(ds);
312	dev_put(ps->netdev);
313}
314
315static struct mdio_driver dsa_loop_drv = {
316	.mdiodrv.driver	= {
317		.name	= "dsa-loop",
318	},
319	.probe	= dsa_loop_drv_probe,
320	.remove	= dsa_loop_drv_remove,
321};
322
323#define NUM_FIXED_PHYS	(DSA_LOOP_NUM_PORTS - 2)
324
325static int __init dsa_loop_init(void)
326{
327	struct fixed_phy_status status = {
328		.link = 1,
329		.speed = SPEED_100,
330		.duplex = DUPLEX_FULL,
331	};
332	unsigned int i;
333
334	for (i = 0; i < NUM_FIXED_PHYS; i++)
335		phydevs[i] = fixed_phy_register(PHY_POLL, &status, -1, NULL);
336
337	return mdio_driver_register(&dsa_loop_drv);
338}
339module_init(dsa_loop_init);
340
341static void __exit dsa_loop_exit(void)
342{
343	unsigned int i;
344
345	mdio_driver_unregister(&dsa_loop_drv);
346	for (i = 0; i < NUM_FIXED_PHYS; i++)
347		if (!IS_ERR(phydevs[i]))
348			fixed_phy_unregister(phydevs[i]);
349}
350module_exit(dsa_loop_exit);
351
352MODULE_LICENSE("GPL");
353MODULE_AUTHOR("Florian Fainelli");
354MODULE_DESCRIPTION("DSA loopback driver");
v5.4
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Distributed Switch Architecture loopback driver
  4 *
  5 * Copyright (C) 2016, Florian Fainelli <f.fainelli@gmail.com>
 
 
 
 
 
  6 */
  7
  8#include <linux/platform_device.h>
  9#include <linux/netdevice.h>
 10#include <linux/phy.h>
 11#include <linux/phy_fixed.h>
 12#include <linux/export.h>
 13#include <linux/ethtool.h>
 14#include <linux/workqueue.h>
 15#include <linux/module.h>
 16#include <linux/if_bridge.h>
 17#include <net/dsa.h>
 18
 19#include "dsa_loop.h"
 20
 21struct dsa_loop_vlan {
 22	u16 members;
 23	u16 untagged;
 24};
 25
 26struct dsa_loop_mib_entry {
 27	char name[ETH_GSTRING_LEN];
 28	unsigned long val;
 29};
 30
 31enum dsa_loop_mib_counters {
 32	DSA_LOOP_PHY_READ_OK,
 33	DSA_LOOP_PHY_READ_ERR,
 34	DSA_LOOP_PHY_WRITE_OK,
 35	DSA_LOOP_PHY_WRITE_ERR,
 36	__DSA_LOOP_CNT_MAX,
 37};
 38
 39static struct dsa_loop_mib_entry dsa_loop_mibs[] = {
 40	[DSA_LOOP_PHY_READ_OK]	= { "phy_read_ok", },
 41	[DSA_LOOP_PHY_READ_ERR]	= { "phy_read_err", },
 42	[DSA_LOOP_PHY_WRITE_OK] = { "phy_write_ok", },
 43	[DSA_LOOP_PHY_WRITE_ERR] = { "phy_write_err", },
 44};
 45
 46struct dsa_loop_port {
 47	struct dsa_loop_mib_entry mib[__DSA_LOOP_CNT_MAX];
 48};
 49
 50#define DSA_LOOP_VLANS	5
 51
 52struct dsa_loop_priv {
 53	struct mii_bus	*bus;
 54	unsigned int	port_base;
 55	struct dsa_loop_vlan vlans[DSA_LOOP_VLANS];
 56	struct net_device *netdev;
 57	struct dsa_loop_port ports[DSA_MAX_PORTS];
 58	u16 pvid;
 59};
 60
 61static struct phy_device *phydevs[PHY_MAX_ADDR];
 62
 63static enum dsa_tag_protocol dsa_loop_get_protocol(struct dsa_switch *ds,
 64						   int port)
 65{
 66	dev_dbg(ds->dev, "%s: port: %d\n", __func__, port);
 67
 68	return DSA_TAG_PROTO_NONE;
 69}
 70
 71static int dsa_loop_setup(struct dsa_switch *ds)
 72{
 73	struct dsa_loop_priv *ps = ds->priv;
 74	unsigned int i;
 75
 76	for (i = 0; i < ds->num_ports; i++)
 77		memcpy(ps->ports[i].mib, dsa_loop_mibs,
 78		       sizeof(dsa_loop_mibs));
 79
 80	dev_dbg(ds->dev, "%s\n", __func__);
 81
 82	return 0;
 83}
 84
 85static int dsa_loop_get_sset_count(struct dsa_switch *ds, int port, int sset)
 86{
 87	if (sset != ETH_SS_STATS && sset != ETH_SS_PHY_STATS)
 88		return 0;
 89
 90	return __DSA_LOOP_CNT_MAX;
 91}
 92
 93static void dsa_loop_get_strings(struct dsa_switch *ds, int port,
 94				 u32 stringset, uint8_t *data)
 95{
 96	struct dsa_loop_priv *ps = ds->priv;
 97	unsigned int i;
 98
 99	if (stringset != ETH_SS_STATS && stringset != ETH_SS_PHY_STATS)
100		return;
101
102	for (i = 0; i < __DSA_LOOP_CNT_MAX; i++)
103		memcpy(data + i * ETH_GSTRING_LEN,
104		       ps->ports[port].mib[i].name, ETH_GSTRING_LEN);
105}
106
107static void dsa_loop_get_ethtool_stats(struct dsa_switch *ds, int port,
108				       uint64_t *data)
109{
110	struct dsa_loop_priv *ps = ds->priv;
111	unsigned int i;
112
113	for (i = 0; i < __DSA_LOOP_CNT_MAX; i++)
114		data[i] = ps->ports[port].mib[i].val;
115}
116
117static int dsa_loop_phy_read(struct dsa_switch *ds, int port, int regnum)
118{
119	struct dsa_loop_priv *ps = ds->priv;
120	struct mii_bus *bus = ps->bus;
121	int ret;
122
 
 
123	ret = mdiobus_read_nested(bus, ps->port_base + port, regnum);
124	if (ret < 0)
125		ps->ports[port].mib[DSA_LOOP_PHY_READ_ERR].val++;
126	else
127		ps->ports[port].mib[DSA_LOOP_PHY_READ_OK].val++;
128
129	return ret;
130}
131
132static int dsa_loop_phy_write(struct dsa_switch *ds, int port,
133			      int regnum, u16 value)
134{
135	struct dsa_loop_priv *ps = ds->priv;
136	struct mii_bus *bus = ps->bus;
137	int ret;
138
 
 
139	ret = mdiobus_write_nested(bus, ps->port_base + port, regnum, value);
140	if (ret < 0)
141		ps->ports[port].mib[DSA_LOOP_PHY_WRITE_ERR].val++;
142	else
143		ps->ports[port].mib[DSA_LOOP_PHY_WRITE_OK].val++;
144
145	return ret;
146}
147
148static int dsa_loop_port_bridge_join(struct dsa_switch *ds, int port,
149				     struct net_device *bridge)
150{
151	dev_dbg(ds->dev, "%s: port: %d, bridge: %s\n",
152		__func__, port, bridge->name);
153
154	return 0;
155}
156
157static void dsa_loop_port_bridge_leave(struct dsa_switch *ds, int port,
158				       struct net_device *bridge)
159{
160	dev_dbg(ds->dev, "%s: port: %d, bridge: %s\n",
161		__func__, port, bridge->name);
162}
163
164static void dsa_loop_port_stp_state_set(struct dsa_switch *ds, int port,
165					u8 state)
166{
167	dev_dbg(ds->dev, "%s: port: %d, state: %d\n",
168		__func__, port, state);
169}
170
171static int dsa_loop_port_vlan_filtering(struct dsa_switch *ds, int port,
172					bool vlan_filtering)
173{
174	dev_dbg(ds->dev, "%s: port: %d, vlan_filtering: %d\n",
175		__func__, port, vlan_filtering);
176
177	return 0;
178}
179
180static int
181dsa_loop_port_vlan_prepare(struct dsa_switch *ds, int port,
182			   const struct switchdev_obj_port_vlan *vlan)
183{
184	struct dsa_loop_priv *ps = ds->priv;
185	struct mii_bus *bus = ps->bus;
186
187	dev_dbg(ds->dev, "%s: port: %d, vlan: %d-%d",
188		__func__, port, vlan->vid_begin, vlan->vid_end);
189
190	/* Just do a sleeping operation to make lockdep checks effective */
191	mdiobus_read(bus, ps->port_base + port, MII_BMSR);
192
193	if (vlan->vid_end > DSA_LOOP_VLANS)
194		return -ERANGE;
195
196	return 0;
197}
198
199static void dsa_loop_port_vlan_add(struct dsa_switch *ds, int port,
200				   const struct switchdev_obj_port_vlan *vlan)
201{
202	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
203	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
204	struct dsa_loop_priv *ps = ds->priv;
205	struct mii_bus *bus = ps->bus;
206	struct dsa_loop_vlan *vl;
207	u16 vid;
208
 
 
209	/* Just do a sleeping operation to make lockdep checks effective */
210	mdiobus_read(bus, ps->port_base + port, MII_BMSR);
211
212	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
213		vl = &ps->vlans[vid];
214
215		vl->members |= BIT(port);
216		if (untagged)
217			vl->untagged |= BIT(port);
218		else
219			vl->untagged &= ~BIT(port);
220
221		dev_dbg(ds->dev, "%s: port: %d vlan: %d, %stagged, pvid: %d\n",
222			__func__, port, vid, untagged ? "un" : "", pvid);
223	}
224
225	if (pvid)
226		ps->pvid = vid;
227}
228
229static int dsa_loop_port_vlan_del(struct dsa_switch *ds, int port,
230				  const struct switchdev_obj_port_vlan *vlan)
231{
232	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
233	struct dsa_loop_priv *ps = ds->priv;
234	struct mii_bus *bus = ps->bus;
235	struct dsa_loop_vlan *vl;
236	u16 vid, pvid = ps->pvid;
237
 
 
238	/* Just do a sleeping operation to make lockdep checks effective */
239	mdiobus_read(bus, ps->port_base + port, MII_BMSR);
240
241	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
242		vl = &ps->vlans[vid];
243
244		vl->members &= ~BIT(port);
245		if (untagged)
246			vl->untagged &= ~BIT(port);
247
248		if (pvid == vid)
249			pvid = 1;
250
251		dev_dbg(ds->dev, "%s: port: %d vlan: %d, %stagged, pvid: %d\n",
252			__func__, port, vid, untagged ? "un" : "", pvid);
253	}
254	ps->pvid = pvid;
255
256	return 0;
257}
258
259static const struct dsa_switch_ops dsa_loop_driver = {
260	.get_tag_protocol	= dsa_loop_get_protocol,
261	.setup			= dsa_loop_setup,
262	.get_strings		= dsa_loop_get_strings,
263	.get_ethtool_stats	= dsa_loop_get_ethtool_stats,
264	.get_sset_count		= dsa_loop_get_sset_count,
265	.get_ethtool_phy_stats	= dsa_loop_get_ethtool_stats,
266	.phy_read		= dsa_loop_phy_read,
267	.phy_write		= dsa_loop_phy_write,
268	.port_bridge_join	= dsa_loop_port_bridge_join,
269	.port_bridge_leave	= dsa_loop_port_bridge_leave,
270	.port_stp_state_set	= dsa_loop_port_stp_state_set,
271	.port_vlan_filtering	= dsa_loop_port_vlan_filtering,
272	.port_vlan_prepare	= dsa_loop_port_vlan_prepare,
273	.port_vlan_add		= dsa_loop_port_vlan_add,
274	.port_vlan_del		= dsa_loop_port_vlan_del,
275};
276
277static int dsa_loop_drv_probe(struct mdio_device *mdiodev)
278{
279	struct dsa_loop_pdata *pdata = mdiodev->dev.platform_data;
280	struct dsa_loop_priv *ps;
281	struct dsa_switch *ds;
282
283	if (!pdata)
284		return -ENODEV;
285
286	dev_info(&mdiodev->dev, "%s: 0x%0x\n",
287		 pdata->name, pdata->enabled_ports);
288
289	ds = dsa_switch_alloc(&mdiodev->dev, DSA_MAX_PORTS);
290	if (!ds)
291		return -ENOMEM;
292
293	ps = devm_kzalloc(&mdiodev->dev, sizeof(*ps), GFP_KERNEL);
294	if (!ps)
295		return -ENOMEM;
296
297	ps->netdev = dev_get_by_name(&init_net, pdata->netdev);
298	if (!ps->netdev)
299		return -EPROBE_DEFER;
300
301	pdata->cd.netdev[DSA_LOOP_CPU_PORT] = &ps->netdev->dev;
302
303	ds->dev = &mdiodev->dev;
304	ds->ops = &dsa_loop_driver;
305	ds->priv = ps;
306	ps->bus = mdiodev->bus;
307
308	dev_set_drvdata(&mdiodev->dev, ds);
309
310	return dsa_register_switch(ds);
311}
312
313static void dsa_loop_drv_remove(struct mdio_device *mdiodev)
314{
315	struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
316	struct dsa_loop_priv *ps = ds->priv;
317
318	dsa_unregister_switch(ds);
319	dev_put(ps->netdev);
320}
321
322static struct mdio_driver dsa_loop_drv = {
323	.mdiodrv.driver	= {
324		.name	= "dsa-loop",
325	},
326	.probe	= dsa_loop_drv_probe,
327	.remove	= dsa_loop_drv_remove,
328};
329
330#define NUM_FIXED_PHYS	(DSA_LOOP_NUM_PORTS - 2)
331
332static int __init dsa_loop_init(void)
333{
334	struct fixed_phy_status status = {
335		.link = 1,
336		.speed = SPEED_100,
337		.duplex = DUPLEX_FULL,
338	};
339	unsigned int i;
340
341	for (i = 0; i < NUM_FIXED_PHYS; i++)
342		phydevs[i] = fixed_phy_register(PHY_POLL, &status, NULL);
343
344	return mdio_driver_register(&dsa_loop_drv);
345}
346module_init(dsa_loop_init);
347
348static void __exit dsa_loop_exit(void)
349{
350	unsigned int i;
351
352	mdio_driver_unregister(&dsa_loop_drv);
353	for (i = 0; i < NUM_FIXED_PHYS; i++)
354		if (!IS_ERR(phydevs[i]))
355			fixed_phy_unregister(phydevs[i]);
356}
357module_exit(dsa_loop_exit);
358
359MODULE_LICENSE("GPL");
360MODULE_AUTHOR("Florian Fainelli");
361MODULE_DESCRIPTION("DSA loopback driver");