Revert "unicode: Don't special case ignorable code points"
[linux.git] / drivers / net / dsa / dsa_loop.c
blobadbab544c60ff361ec6b2d02026d9d578e5b095c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Distributed Switch Architecture loopback driver
5 * Copyright (C) 2016, Florian Fainelli <f.fainelli@gmail.com>
6 */
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 <linux/dsa/loop.h>
18 #include <net/dsa.h>
20 #include "dsa_loop.h"
22 static struct dsa_loop_mib_entry dsa_loop_mibs[] = {
23 [DSA_LOOP_PHY_READ_OK] = { "phy_read_ok", },
24 [DSA_LOOP_PHY_READ_ERR] = { "phy_read_err", },
25 [DSA_LOOP_PHY_WRITE_OK] = { "phy_write_ok", },
26 [DSA_LOOP_PHY_WRITE_ERR] = { "phy_write_err", },
29 static struct phy_device *phydevs[PHY_MAX_ADDR];
31 enum dsa_loop_devlink_resource_id {
32 DSA_LOOP_DEVLINK_PARAM_ID_VTU,
35 static u64 dsa_loop_devlink_vtu_get(void *priv)
37 struct dsa_loop_priv *ps = priv;
38 unsigned int i, count = 0;
39 struct dsa_loop_vlan *vl;
41 for (i = 0; i < ARRAY_SIZE(ps->vlans); i++) {
42 vl = &ps->vlans[i];
43 if (vl->members)
44 count++;
47 return count;
50 static int dsa_loop_setup_devlink_resources(struct dsa_switch *ds)
52 struct devlink_resource_size_params size_params;
53 struct dsa_loop_priv *ps = ds->priv;
54 int err;
56 devlink_resource_size_params_init(&size_params, ARRAY_SIZE(ps->vlans),
57 ARRAY_SIZE(ps->vlans),
58 1, DEVLINK_RESOURCE_UNIT_ENTRY);
60 err = dsa_devlink_resource_register(ds, "VTU", ARRAY_SIZE(ps->vlans),
61 DSA_LOOP_DEVLINK_PARAM_ID_VTU,
62 DEVLINK_RESOURCE_ID_PARENT_TOP,
63 &size_params);
64 if (err)
65 goto out;
67 dsa_devlink_resource_occ_get_register(ds,
68 DSA_LOOP_DEVLINK_PARAM_ID_VTU,
69 dsa_loop_devlink_vtu_get, ps);
71 return 0;
73 out:
74 dsa_devlink_resources_unregister(ds);
75 return err;
78 static enum dsa_tag_protocol dsa_loop_get_protocol(struct dsa_switch *ds,
79 int port,
80 enum dsa_tag_protocol mp)
82 dev_dbg(ds->dev, "%s: port: %d\n", __func__, port);
84 return DSA_TAG_PROTO_NONE;
87 static int dsa_loop_setup(struct dsa_switch *ds)
89 struct dsa_loop_priv *ps = ds->priv;
90 unsigned int i;
92 for (i = 0; i < ds->num_ports; i++)
93 memcpy(ps->ports[i].mib, dsa_loop_mibs,
94 sizeof(dsa_loop_mibs));
96 dev_dbg(ds->dev, "%s\n", __func__);
98 return dsa_loop_setup_devlink_resources(ds);
101 static void dsa_loop_teardown(struct dsa_switch *ds)
103 dsa_devlink_resources_unregister(ds);
106 static int dsa_loop_get_sset_count(struct dsa_switch *ds, int port, int sset)
108 if (sset != ETH_SS_STATS && sset != ETH_SS_PHY_STATS)
109 return 0;
111 return __DSA_LOOP_CNT_MAX;
114 static void dsa_loop_get_strings(struct dsa_switch *ds, int port,
115 u32 stringset, uint8_t *data)
117 struct dsa_loop_priv *ps = ds->priv;
118 unsigned int i;
120 if (stringset != ETH_SS_STATS && stringset != ETH_SS_PHY_STATS)
121 return;
123 for (i = 0; i < __DSA_LOOP_CNT_MAX; i++)
124 ethtool_puts(&data, ps->ports[port].mib[i].name);
127 static void dsa_loop_get_ethtool_stats(struct dsa_switch *ds, int port,
128 uint64_t *data)
130 struct dsa_loop_priv *ps = ds->priv;
131 unsigned int i;
133 for (i = 0; i < __DSA_LOOP_CNT_MAX; i++)
134 data[i] = ps->ports[port].mib[i].val;
137 static int dsa_loop_phy_read(struct dsa_switch *ds, int port, int regnum)
139 struct dsa_loop_priv *ps = ds->priv;
140 struct mii_bus *bus = ps->bus;
141 int ret;
143 ret = mdiobus_read_nested(bus, ps->port_base + port, regnum);
144 if (ret < 0)
145 ps->ports[port].mib[DSA_LOOP_PHY_READ_ERR].val++;
146 else
147 ps->ports[port].mib[DSA_LOOP_PHY_READ_OK].val++;
149 return ret;
152 static int dsa_loop_phy_write(struct dsa_switch *ds, int port,
153 int regnum, u16 value)
155 struct dsa_loop_priv *ps = ds->priv;
156 struct mii_bus *bus = ps->bus;
157 int ret;
159 ret = mdiobus_write_nested(bus, ps->port_base + port, regnum, value);
160 if (ret < 0)
161 ps->ports[port].mib[DSA_LOOP_PHY_WRITE_ERR].val++;
162 else
163 ps->ports[port].mib[DSA_LOOP_PHY_WRITE_OK].val++;
165 return ret;
168 static int dsa_loop_port_bridge_join(struct dsa_switch *ds, int port,
169 struct dsa_bridge bridge,
170 bool *tx_fwd_offload,
171 struct netlink_ext_ack *extack)
173 dev_dbg(ds->dev, "%s: port: %d, bridge: %s\n",
174 __func__, port, bridge.dev->name);
176 return 0;
179 static void dsa_loop_port_bridge_leave(struct dsa_switch *ds, int port,
180 struct dsa_bridge bridge)
182 dev_dbg(ds->dev, "%s: port: %d, bridge: %s\n",
183 __func__, port, bridge.dev->name);
186 static void dsa_loop_port_stp_state_set(struct dsa_switch *ds, int port,
187 u8 state)
189 dev_dbg(ds->dev, "%s: port: %d, state: %d\n",
190 __func__, port, state);
193 static int dsa_loop_port_vlan_filtering(struct dsa_switch *ds, int port,
194 bool vlan_filtering,
195 struct netlink_ext_ack *extack)
197 dev_dbg(ds->dev, "%s: port: %d, vlan_filtering: %d\n",
198 __func__, port, vlan_filtering);
200 return 0;
203 static int dsa_loop_port_vlan_add(struct dsa_switch *ds, int port,
204 const struct switchdev_obj_port_vlan *vlan,
205 struct netlink_ext_ack *extack)
207 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
208 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
209 struct dsa_loop_priv *ps = ds->priv;
210 struct mii_bus *bus = ps->bus;
211 struct dsa_loop_vlan *vl;
213 if (vlan->vid >= ARRAY_SIZE(ps->vlans))
214 return -ERANGE;
216 /* Just do a sleeping operation to make lockdep checks effective */
217 mdiobus_read(bus, ps->port_base + port, MII_BMSR);
219 vl = &ps->vlans[vlan->vid];
221 vl->members |= BIT(port);
222 if (untagged)
223 vl->untagged |= BIT(port);
224 else
225 vl->untagged &= ~BIT(port);
227 dev_dbg(ds->dev, "%s: port: %d vlan: %d, %stagged, pvid: %d\n",
228 __func__, port, vlan->vid, untagged ? "un" : "", pvid);
230 if (pvid)
231 ps->ports[port].pvid = vlan->vid;
233 return 0;
236 static int dsa_loop_port_vlan_del(struct dsa_switch *ds, int port,
237 const struct switchdev_obj_port_vlan *vlan)
239 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
240 struct dsa_loop_priv *ps = ds->priv;
241 u16 pvid = ps->ports[port].pvid;
242 struct mii_bus *bus = ps->bus;
243 struct dsa_loop_vlan *vl;
245 /* Just do a sleeping operation to make lockdep checks effective */
246 mdiobus_read(bus, ps->port_base + port, MII_BMSR);
248 vl = &ps->vlans[vlan->vid];
250 vl->members &= ~BIT(port);
251 if (untagged)
252 vl->untagged &= ~BIT(port);
254 if (pvid == vlan->vid)
255 pvid = 1;
257 dev_dbg(ds->dev, "%s: port: %d vlan: %d, %stagged, pvid: %d\n",
258 __func__, port, vlan->vid, untagged ? "un" : "", pvid);
259 ps->ports[port].pvid = pvid;
261 return 0;
264 static int dsa_loop_port_change_mtu(struct dsa_switch *ds, int port,
265 int new_mtu)
267 struct dsa_loop_priv *priv = ds->priv;
269 priv->ports[port].mtu = new_mtu;
271 return 0;
274 static int dsa_loop_port_max_mtu(struct dsa_switch *ds, int port)
276 return ETH_MAX_MTU;
279 static void dsa_loop_phylink_get_caps(struct dsa_switch *dsa, int port,
280 struct phylink_config *config)
282 bitmap_fill(config->supported_interfaces, PHY_INTERFACE_MODE_MAX);
283 __clear_bit(PHY_INTERFACE_MODE_NA, config->supported_interfaces);
284 config->mac_capabilities = ~0;
287 static const struct dsa_switch_ops dsa_loop_driver = {
288 .get_tag_protocol = dsa_loop_get_protocol,
289 .setup = dsa_loop_setup,
290 .teardown = dsa_loop_teardown,
291 .get_strings = dsa_loop_get_strings,
292 .get_ethtool_stats = dsa_loop_get_ethtool_stats,
293 .get_sset_count = dsa_loop_get_sset_count,
294 .get_ethtool_phy_stats = dsa_loop_get_ethtool_stats,
295 .phy_read = dsa_loop_phy_read,
296 .phy_write = dsa_loop_phy_write,
297 .port_bridge_join = dsa_loop_port_bridge_join,
298 .port_bridge_leave = dsa_loop_port_bridge_leave,
299 .port_stp_state_set = dsa_loop_port_stp_state_set,
300 .port_vlan_filtering = dsa_loop_port_vlan_filtering,
301 .port_vlan_add = dsa_loop_port_vlan_add,
302 .port_vlan_del = dsa_loop_port_vlan_del,
303 .port_change_mtu = dsa_loop_port_change_mtu,
304 .port_max_mtu = dsa_loop_port_max_mtu,
305 .phylink_get_caps = dsa_loop_phylink_get_caps,
308 static int dsa_loop_drv_probe(struct mdio_device *mdiodev)
310 struct dsa_loop_pdata *pdata = mdiodev->dev.platform_data;
311 struct dsa_loop_priv *ps;
312 struct dsa_switch *ds;
313 int ret;
315 if (!pdata)
316 return -ENODEV;
318 ds = devm_kzalloc(&mdiodev->dev, sizeof(*ds), GFP_KERNEL);
319 if (!ds)
320 return -ENOMEM;
322 ds->dev = &mdiodev->dev;
323 ds->num_ports = DSA_LOOP_NUM_PORTS;
325 ps = devm_kzalloc(&mdiodev->dev, sizeof(*ps), GFP_KERNEL);
326 if (!ps)
327 return -ENOMEM;
329 ps->netdev = dev_get_by_name(&init_net, pdata->netdev);
330 if (!ps->netdev)
331 return -EPROBE_DEFER;
333 pdata->cd.netdev[DSA_LOOP_CPU_PORT] = &ps->netdev->dev;
335 ds->dev = &mdiodev->dev;
336 ds->ops = &dsa_loop_driver;
337 ds->priv = ps;
338 ps->bus = mdiodev->bus;
340 dev_set_drvdata(&mdiodev->dev, ds);
342 ret = dsa_register_switch(ds);
343 if (!ret)
344 dev_info(&mdiodev->dev, "%s: 0x%0x\n",
345 pdata->name, pdata->enabled_ports);
347 return ret;
350 static void dsa_loop_drv_remove(struct mdio_device *mdiodev)
352 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
353 struct dsa_loop_priv *ps;
355 if (!ds)
356 return;
358 ps = ds->priv;
360 dsa_unregister_switch(ds);
361 dev_put(ps->netdev);
364 static void dsa_loop_drv_shutdown(struct mdio_device *mdiodev)
366 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
368 if (!ds)
369 return;
371 dsa_switch_shutdown(ds);
373 dev_set_drvdata(&mdiodev->dev, NULL);
376 static struct mdio_driver dsa_loop_drv = {
377 .mdiodrv.driver = {
378 .name = "dsa-loop",
380 .probe = dsa_loop_drv_probe,
381 .remove = dsa_loop_drv_remove,
382 .shutdown = dsa_loop_drv_shutdown,
385 #define NUM_FIXED_PHYS (DSA_LOOP_NUM_PORTS - 2)
387 static void dsa_loop_phydevs_unregister(void)
389 unsigned int i;
391 for (i = 0; i < NUM_FIXED_PHYS; i++)
392 if (!IS_ERR(phydevs[i])) {
393 fixed_phy_unregister(phydevs[i]);
394 phy_device_free(phydevs[i]);
398 static int __init dsa_loop_init(void)
400 struct fixed_phy_status status = {
401 .link = 1,
402 .speed = SPEED_100,
403 .duplex = DUPLEX_FULL,
405 unsigned int i, ret;
407 for (i = 0; i < NUM_FIXED_PHYS; i++)
408 phydevs[i] = fixed_phy_register(PHY_POLL, &status, NULL);
410 ret = mdio_driver_register(&dsa_loop_drv);
411 if (ret)
412 dsa_loop_phydevs_unregister();
414 return ret;
416 module_init(dsa_loop_init);
418 static void __exit dsa_loop_exit(void)
420 mdio_driver_unregister(&dsa_loop_drv);
421 dsa_loop_phydevs_unregister();
423 module_exit(dsa_loop_exit);
425 MODULE_SOFTDEP("pre: dsa_loop_bdinfo");
426 MODULE_LICENSE("GPL");
427 MODULE_AUTHOR("Florian Fainelli");
428 MODULE_DESCRIPTION("DSA loopback driver");