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 25 struct dsa_loop_vlan { 26 u16 members; 27 u16 untagged; 28 }; 29 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 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; 61 struct dsa_loop_port ports[DSA_MAX_PORTS]; 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 int port) 69 { 70 dev_dbg(ds->dev, "%s\n", __func__); 71 72 return DSA_TAG_PROTO_NONE; 73 } 74 75 static 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 89 static int dsa_loop_get_sset_count(struct dsa_switch *ds) 90 { 91 return __DSA_LOOP_CNT_MAX; 92 } 93 94 static 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 104 static 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 114 static 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 131 static 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 149 static 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 157 static 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 163 static 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 169 static 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 177 static int dsa_loop_port_vlan_prepare(struct dsa_switch *ds, int port, 178 const struct switchdev_obj_port_vlan *vlan, 179 struct switchdev_trans *trans) 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 195 static void dsa_loop_port_vlan_add(struct dsa_switch *ds, int port, 196 const struct switchdev_obj_port_vlan *vlan, 197 struct switchdev_trans *trans) 198 { 199 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 200 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID; 201 struct dsa_loop_priv *ps = ds->priv; 202 struct mii_bus *bus = ps->bus; 203 struct dsa_loop_vlan *vl; 204 u16 vid; 205 206 dev_dbg(ds->dev, "%s\n", __func__); 207 208 /* Just do a sleeping operation to make lockdep checks effective */ 209 mdiobus_read(bus, ps->port_base + port, MII_BMSR); 210 211 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) { 212 vl = &ps->vlans[vid]; 213 214 vl->members |= BIT(port); 215 if (untagged) 216 vl->untagged |= BIT(port); 217 else 218 vl->untagged &= ~BIT(port); 219 } 220 221 if (pvid) 222 ps->pvid = vid; 223 } 224 225 static int dsa_loop_port_vlan_del(struct dsa_switch *ds, int port, 226 const struct switchdev_obj_port_vlan *vlan) 227 { 228 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 229 struct dsa_loop_priv *ps = ds->priv; 230 struct mii_bus *bus = ps->bus; 231 struct dsa_loop_vlan *vl; 232 u16 vid, pvid = ps->pvid; 233 234 dev_dbg(ds->dev, "%s\n", __func__); 235 236 /* Just do a sleeping operation to make lockdep checks effective */ 237 mdiobus_read(bus, ps->port_base + port, MII_BMSR); 238 239 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) { 240 vl = &ps->vlans[vid]; 241 242 vl->members &= ~BIT(port); 243 if (untagged) 244 vl->untagged &= ~BIT(port); 245 246 if (pvid == vid) 247 pvid = 1; 248 } 249 ps->pvid = pvid; 250 251 return 0; 252 } 253 254 static const struct dsa_switch_ops dsa_loop_driver = { 255 .get_tag_protocol = dsa_loop_get_protocol, 256 .setup = dsa_loop_setup, 257 .get_strings = dsa_loop_get_strings, 258 .get_ethtool_stats = dsa_loop_get_ethtool_stats, 259 .get_sset_count = dsa_loop_get_sset_count, 260 .phy_read = dsa_loop_phy_read, 261 .phy_write = dsa_loop_phy_write, 262 .port_bridge_join = dsa_loop_port_bridge_join, 263 .port_bridge_leave = dsa_loop_port_bridge_leave, 264 .port_stp_state_set = dsa_loop_port_stp_state_set, 265 .port_vlan_filtering = dsa_loop_port_vlan_filtering, 266 .port_vlan_prepare = dsa_loop_port_vlan_prepare, 267 .port_vlan_add = dsa_loop_port_vlan_add, 268 .port_vlan_del = dsa_loop_port_vlan_del, 269 }; 270 271 static int dsa_loop_drv_probe(struct mdio_device *mdiodev) 272 { 273 struct dsa_loop_pdata *pdata = mdiodev->dev.platform_data; 274 struct dsa_loop_priv *ps; 275 struct dsa_switch *ds; 276 277 if (!pdata) 278 return -ENODEV; 279 280 dev_info(&mdiodev->dev, "%s: 0x%0x\n", 281 pdata->name, pdata->enabled_ports); 282 283 ds = dsa_switch_alloc(&mdiodev->dev, DSA_MAX_PORTS); 284 if (!ds) 285 return -ENOMEM; 286 287 ps = devm_kzalloc(&mdiodev->dev, sizeof(*ps), GFP_KERNEL); 288 if (!ps) 289 return -ENOMEM; 290 291 ps->netdev = dev_get_by_name(&init_net, pdata->netdev); 292 if (!ps->netdev) 293 return -EPROBE_DEFER; 294 295 pdata->cd.netdev[DSA_LOOP_CPU_PORT] = &ps->netdev->dev; 296 297 ds->dev = &mdiodev->dev; 298 ds->ops = &dsa_loop_driver; 299 ds->priv = ps; 300 ps->bus = mdiodev->bus; 301 302 dev_set_drvdata(&mdiodev->dev, ds); 303 304 return dsa_register_switch(ds); 305 } 306 307 static void dsa_loop_drv_remove(struct mdio_device *mdiodev) 308 { 309 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev); 310 struct dsa_loop_priv *ps = ds->priv; 311 312 dsa_unregister_switch(ds); 313 dev_put(ps->netdev); 314 } 315 316 static struct mdio_driver dsa_loop_drv = { 317 .mdiodrv.driver = { 318 .name = "dsa-loop", 319 }, 320 .probe = dsa_loop_drv_probe, 321 .remove = dsa_loop_drv_remove, 322 }; 323 324 #define NUM_FIXED_PHYS (DSA_LOOP_NUM_PORTS - 2) 325 326 static int __init dsa_loop_init(void) 327 { 328 struct fixed_phy_status status = { 329 .link = 1, 330 .speed = SPEED_100, 331 .duplex = DUPLEX_FULL, 332 }; 333 unsigned int i; 334 335 for (i = 0; i < NUM_FIXED_PHYS; i++) 336 phydevs[i] = fixed_phy_register(PHY_POLL, &status, -1, NULL); 337 338 return mdio_driver_register(&dsa_loop_drv); 339 } 340 module_init(dsa_loop_init); 341 342 static void __exit dsa_loop_exit(void) 343 { 344 unsigned int i; 345 346 mdio_driver_unregister(&dsa_loop_drv); 347 for (i = 0; i < NUM_FIXED_PHYS; i++) 348 if (!IS_ERR(phydevs[i])) 349 fixed_phy_unregister(phydevs[i]); 350 } 351 module_exit(dsa_loop_exit); 352 353 MODULE_LICENSE("GPL"); 354 MODULE_AUTHOR("Florian Fainelli"); 355 MODULE_DESCRIPTION("DSA loopback driver"); 356