]>
Commit | Line | Data |
---|---|---|
98cd1552 FF |
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> | |
484c0172 | 17 | #include <linux/ethtool.h> |
98cd1552 FF |
18 | #include <linux/workqueue.h> |
19 | #include <linux/module.h> | |
20 | #include <linux/if_bridge.h> | |
98cd1552 FF |
21 | #include <net/dsa.h> |
22 | ||
23 | #include "dsa_loop.h" | |
24 | ||
25 | struct dsa_loop_vlan { | |
26 | u16 members; | |
27 | u16 untagged; | |
28 | }; | |
29 | ||
484c0172 FF |
30 | struct dsa_loop_mib_entry { |
31 | char name[ETH_GSTRING_LEN]; | |
32 | unsigned long val; | |
33 | }; | |
34 | ||
35 | enum 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 | ||
43 | static 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 | ||
50 | struct dsa_loop_port { | |
51 | struct dsa_loop_mib_entry mib[__DSA_LOOP_CNT_MAX]; | |
52 | }; | |
53 | ||
98cd1552 FF |
54 | #define DSA_LOOP_VLANS 5 |
55 | ||
56 | struct 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; | |
484c0172 | 61 | struct dsa_loop_port ports[DSA_MAX_PORTS]; |
98cd1552 FF |
62 | u16 pvid; |
63 | }; | |
64 | ||
65 | static struct phy_device *phydevs[PHY_MAX_ADDR]; | |
66 | ||
67 | static enum dsa_tag_protocol dsa_loop_get_protocol(struct dsa_switch *ds) | |
68 | { | |
69 | dev_dbg(ds->dev, "%s\n", __func__); | |
70 | ||
71 | return DSA_TAG_PROTO_NONE; | |
72 | } | |
73 | ||
74 | static int dsa_loop_setup(struct dsa_switch *ds) | |
75 | { | |
484c0172 FF |
76 | struct dsa_loop_priv *ps = ds->priv; |
77 | unsigned int i; | |
78 | ||
79 | for (i = 0; i < ds->num_ports; i++) | |
80 | memcpy(ps->ports[i].mib, dsa_loop_mibs, | |
81 | sizeof(dsa_loop_mibs)); | |
82 | ||
98cd1552 FF |
83 | dev_dbg(ds->dev, "%s\n", __func__); |
84 | ||
85 | return 0; | |
86 | } | |
87 | ||
484c0172 FF |
88 | static int dsa_loop_get_sset_count(struct dsa_switch *ds) |
89 | { | |
90 | return __DSA_LOOP_CNT_MAX; | |
91 | } | |
92 | ||
93 | static void dsa_loop_get_strings(struct dsa_switch *ds, int port, uint8_t *data) | |
94 | { | |
95 | struct dsa_loop_priv *ps = ds->priv; | |
96 | unsigned int i; | |
97 | ||
98 | for (i = 0; i < __DSA_LOOP_CNT_MAX; i++) | |
99 | memcpy(data + i * ETH_GSTRING_LEN, | |
100 | ps->ports[port].mib[i].name, ETH_GSTRING_LEN); | |
101 | } | |
102 | ||
103 | static void dsa_loop_get_ethtool_stats(struct dsa_switch *ds, int port, | |
104 | uint64_t *data) | |
105 | { | |
106 | struct dsa_loop_priv *ps = ds->priv; | |
107 | unsigned int i; | |
108 | ||
109 | for (i = 0; i < __DSA_LOOP_CNT_MAX; i++) | |
110 | data[i] = ps->ports[port].mib[i].val; | |
111 | } | |
112 | ||
98cd1552 FF |
113 | static int dsa_loop_set_addr(struct dsa_switch *ds, u8 *addr) |
114 | { | |
115 | dev_dbg(ds->dev, "%s\n", __func__); | |
116 | ||
117 | return 0; | |
118 | } | |
119 | ||
120 | static int dsa_loop_phy_read(struct dsa_switch *ds, int port, int regnum) | |
121 | { | |
122 | struct dsa_loop_priv *ps = ds->priv; | |
123 | struct mii_bus *bus = ps->bus; | |
484c0172 | 124 | int ret; |
98cd1552 FF |
125 | |
126 | dev_dbg(ds->dev, "%s\n", __func__); | |
127 | ||
484c0172 FF |
128 | ret = mdiobus_read_nested(bus, ps->port_base + port, regnum); |
129 | if (ret < 0) | |
130 | ps->ports[port].mib[DSA_LOOP_PHY_READ_ERR].val++; | |
131 | else | |
132 | ps->ports[port].mib[DSA_LOOP_PHY_READ_OK].val++; | |
133 | ||
134 | return ret; | |
98cd1552 FF |
135 | } |
136 | ||
137 | static int dsa_loop_phy_write(struct dsa_switch *ds, int port, | |
138 | int regnum, u16 value) | |
139 | { | |
140 | struct dsa_loop_priv *ps = ds->priv; | |
141 | struct mii_bus *bus = ps->bus; | |
484c0172 | 142 | int ret; |
98cd1552 FF |
143 | |
144 | dev_dbg(ds->dev, "%s\n", __func__); | |
145 | ||
484c0172 FF |
146 | ret = mdiobus_write_nested(bus, ps->port_base + port, regnum, value); |
147 | if (ret < 0) | |
148 | ps->ports[port].mib[DSA_LOOP_PHY_WRITE_ERR].val++; | |
149 | else | |
150 | ps->ports[port].mib[DSA_LOOP_PHY_WRITE_OK].val++; | |
151 | ||
152 | return ret; | |
98cd1552 FF |
153 | } |
154 | ||
155 | static int dsa_loop_port_bridge_join(struct dsa_switch *ds, int port, | |
156 | struct net_device *bridge) | |
157 | { | |
158 | dev_dbg(ds->dev, "%s\n", __func__); | |
159 | ||
160 | return 0; | |
161 | } | |
162 | ||
163 | static void dsa_loop_port_bridge_leave(struct dsa_switch *ds, int port, | |
164 | struct net_device *bridge) | |
165 | { | |
166 | dev_dbg(ds->dev, "%s\n", __func__); | |
167 | } | |
168 | ||
169 | static void dsa_loop_port_stp_state_set(struct dsa_switch *ds, int port, | |
170 | u8 state) | |
171 | { | |
172 | dev_dbg(ds->dev, "%s\n", __func__); | |
173 | } | |
174 | ||
175 | static int dsa_loop_port_vlan_filtering(struct dsa_switch *ds, int port, | |
176 | bool vlan_filtering) | |
177 | { | |
178 | dev_dbg(ds->dev, "%s\n", __func__); | |
179 | ||
180 | return 0; | |
181 | } | |
182 | ||
183 | static int dsa_loop_port_vlan_prepare(struct dsa_switch *ds, int port, | |
184 | const struct switchdev_obj_port_vlan *vlan, | |
185 | struct switchdev_trans *trans) | |
186 | { | |
187 | struct dsa_loop_priv *ps = ds->priv; | |
188 | struct mii_bus *bus = ps->bus; | |
189 | ||
190 | dev_dbg(ds->dev, "%s\n", __func__); | |
191 | ||
192 | /* Just do a sleeping operation to make lockdep checks effective */ | |
193 | mdiobus_read(bus, ps->port_base + port, MII_BMSR); | |
194 | ||
195 | if (vlan->vid_end > DSA_LOOP_VLANS) | |
196 | return -ERANGE; | |
197 | ||
198 | return 0; | |
199 | } | |
200 | ||
201 | static void dsa_loop_port_vlan_add(struct dsa_switch *ds, int port, | |
202 | const struct switchdev_obj_port_vlan *vlan, | |
203 | struct switchdev_trans *trans) | |
204 | { | |
205 | bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; | |
206 | bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID; | |
207 | struct dsa_loop_priv *ps = ds->priv; | |
208 | struct mii_bus *bus = ps->bus; | |
209 | struct dsa_loop_vlan *vl; | |
210 | u16 vid; | |
211 | ||
212 | dev_dbg(ds->dev, "%s\n", __func__); | |
213 | ||
214 | /* Just do a sleeping operation to make lockdep checks effective */ | |
215 | mdiobus_read(bus, ps->port_base + port, MII_BMSR); | |
216 | ||
217 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) { | |
218 | vl = &ps->vlans[vid]; | |
219 | ||
220 | vl->members |= BIT(port); | |
221 | if (untagged) | |
222 | vl->untagged |= BIT(port); | |
223 | else | |
224 | vl->untagged &= ~BIT(port); | |
225 | } | |
226 | ||
227 | if (pvid) | |
228 | ps->pvid = vid; | |
229 | } | |
230 | ||
231 | static int dsa_loop_port_vlan_del(struct dsa_switch *ds, int port, | |
232 | const struct switchdev_obj_port_vlan *vlan) | |
233 | { | |
234 | bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; | |
235 | struct dsa_loop_priv *ps = ds->priv; | |
236 | struct mii_bus *bus = ps->bus; | |
237 | struct dsa_loop_vlan *vl; | |
5865ccce | 238 | u16 vid, pvid = ps->pvid; |
98cd1552 FF |
239 | |
240 | dev_dbg(ds->dev, "%s\n", __func__); | |
241 | ||
242 | /* Just do a sleeping operation to make lockdep checks effective */ | |
243 | mdiobus_read(bus, ps->port_base + port, MII_BMSR); | |
244 | ||
245 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) { | |
246 | vl = &ps->vlans[vid]; | |
247 | ||
248 | vl->members &= ~BIT(port); | |
249 | if (untagged) | |
250 | vl->untagged &= ~BIT(port); | |
251 | ||
252 | if (pvid == vid) | |
253 | pvid = 1; | |
254 | } | |
255 | ps->pvid = pvid; | |
256 | ||
257 | return 0; | |
258 | } | |
259 | ||
260 | static int dsa_loop_port_vlan_dump(struct dsa_switch *ds, int port, | |
261 | struct switchdev_obj_port_vlan *vlan, | |
438ff537 | 262 | switchdev_obj_dump_cb_t *cb) |
98cd1552 FF |
263 | { |
264 | struct dsa_loop_priv *ps = ds->priv; | |
265 | struct mii_bus *bus = ps->bus; | |
266 | struct dsa_loop_vlan *vl; | |
267 | u16 vid, vid_start = 0; | |
d1db799e | 268 | int err = 0; |
98cd1552 FF |
269 | |
270 | dev_dbg(ds->dev, "%s\n", __func__); | |
271 | ||
272 | /* Just do a sleeping operation to make lockdep checks effective */ | |
273 | mdiobus_read(bus, ps->port_base + port, MII_BMSR); | |
274 | ||
275 | for (vid = vid_start; vid < DSA_LOOP_VLANS; vid++) { | |
276 | vl = &ps->vlans[vid]; | |
277 | ||
278 | if (!(vl->members & BIT(port))) | |
279 | continue; | |
280 | ||
281 | vlan->vid_begin = vlan->vid_end = vid; | |
282 | vlan->flags = 0; | |
283 | ||
284 | if (vl->untagged & BIT(port)) | |
285 | vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED; | |
286 | if (ps->pvid == vid) | |
287 | vlan->flags |= BRIDGE_VLAN_INFO_PVID; | |
288 | ||
289 | err = cb(&vlan->obj); | |
290 | if (err) | |
291 | break; | |
292 | } | |
293 | ||
294 | return err; | |
295 | } | |
296 | ||
297 | static struct dsa_switch_ops dsa_loop_driver = { | |
298 | .get_tag_protocol = dsa_loop_get_protocol, | |
299 | .setup = dsa_loop_setup, | |
484c0172 FF |
300 | .get_strings = dsa_loop_get_strings, |
301 | .get_ethtool_stats = dsa_loop_get_ethtool_stats, | |
302 | .get_sset_count = dsa_loop_get_sset_count, | |
98cd1552 FF |
303 | .set_addr = dsa_loop_set_addr, |
304 | .phy_read = dsa_loop_phy_read, | |
305 | .phy_write = dsa_loop_phy_write, | |
306 | .port_bridge_join = dsa_loop_port_bridge_join, | |
307 | .port_bridge_leave = dsa_loop_port_bridge_leave, | |
308 | .port_stp_state_set = dsa_loop_port_stp_state_set, | |
309 | .port_vlan_filtering = dsa_loop_port_vlan_filtering, | |
310 | .port_vlan_prepare = dsa_loop_port_vlan_prepare, | |
311 | .port_vlan_add = dsa_loop_port_vlan_add, | |
312 | .port_vlan_del = dsa_loop_port_vlan_del, | |
313 | .port_vlan_dump = dsa_loop_port_vlan_dump, | |
314 | }; | |
315 | ||
316 | static int dsa_loop_drv_probe(struct mdio_device *mdiodev) | |
317 | { | |
318 | struct dsa_loop_pdata *pdata = mdiodev->dev.platform_data; | |
319 | struct dsa_loop_priv *ps; | |
320 | struct dsa_switch *ds; | |
321 | ||
322 | if (!pdata) | |
323 | return -ENODEV; | |
324 | ||
325 | dev_info(&mdiodev->dev, "%s: 0x%0x\n", | |
326 | pdata->name, pdata->enabled_ports); | |
327 | ||
328 | ds = dsa_switch_alloc(&mdiodev->dev, DSA_MAX_PORTS); | |
329 | if (!ds) | |
330 | return -ENOMEM; | |
331 | ||
332 | ps = devm_kzalloc(&mdiodev->dev, sizeof(*ps), GFP_KERNEL); | |
8ce7aaaa CJ |
333 | if (!ps) |
334 | return -ENOMEM; | |
335 | ||
98cd1552 FF |
336 | ps->netdev = dev_get_by_name(&init_net, pdata->netdev); |
337 | if (!ps->netdev) | |
338 | return -EPROBE_DEFER; | |
339 | ||
340 | pdata->cd.netdev[DSA_LOOP_CPU_PORT] = &ps->netdev->dev; | |
341 | ||
342 | ds->dev = &mdiodev->dev; | |
343 | ds->ops = &dsa_loop_driver; | |
344 | ds->priv = ps; | |
345 | ps->bus = mdiodev->bus; | |
346 | ||
347 | dev_set_drvdata(&mdiodev->dev, ds); | |
348 | ||
23c9ee49 | 349 | return dsa_register_switch(ds); |
98cd1552 FF |
350 | } |
351 | ||
352 | static void dsa_loop_drv_remove(struct mdio_device *mdiodev) | |
353 | { | |
354 | struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev); | |
355 | struct dsa_loop_priv *ps = ds->priv; | |
356 | ||
357 | dsa_unregister_switch(ds); | |
358 | dev_put(ps->netdev); | |
359 | } | |
360 | ||
361 | static struct mdio_driver dsa_loop_drv = { | |
362 | .mdiodrv.driver = { | |
363 | .name = "dsa-loop", | |
364 | }, | |
365 | .probe = dsa_loop_drv_probe, | |
366 | .remove = dsa_loop_drv_remove, | |
367 | }; | |
368 | ||
369 | #define NUM_FIXED_PHYS (DSA_LOOP_NUM_PORTS - 2) | |
370 | ||
98cd1552 FF |
371 | static int __init dsa_loop_init(void) |
372 | { | |
373 | struct fixed_phy_status status = { | |
374 | .link = 1, | |
375 | .speed = SPEED_100, | |
376 | .duplex = DUPLEX_FULL, | |
377 | }; | |
378 | unsigned int i; | |
379 | ||
380 | for (i = 0; i < NUM_FIXED_PHYS; i++) | |
381 | phydevs[i] = fixed_phy_register(PHY_POLL, &status, -1, NULL); | |
382 | ||
383 | return mdio_driver_register(&dsa_loop_drv); | |
384 | } | |
385 | module_init(dsa_loop_init); | |
386 | ||
387 | static void __exit dsa_loop_exit(void) | |
388 | { | |
3407dc8e FF |
389 | unsigned int i; |
390 | ||
98cd1552 | 391 | mdio_driver_unregister(&dsa_loop_drv); |
3407dc8e FF |
392 | for (i = 0; i < NUM_FIXED_PHYS; i++) |
393 | if (phydevs[i]) | |
394 | fixed_phy_unregister(phydevs[i]); | |
98cd1552 FF |
395 | } |
396 | module_exit(dsa_loop_exit); | |
397 | ||
398 | MODULE_LICENSE("GPL"); | |
399 | MODULE_AUTHOR("Florian Fainelli"); | |
400 | MODULE_DESCRIPTION("DSA loopback driver"); |