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, int port, int sset) 90 { 91 if (sset != ETH_SS_STATS && sset != ETH_SS_PHY_STATS) 92 return 0; 93 94 return __DSA_LOOP_CNT_MAX; 95 } 96 97 static void dsa_loop_get_strings(struct dsa_switch *ds, int port, 98 u32 stringset, uint8_t *data) 99 { 100 struct dsa_loop_priv *ps = ds->priv; 101 unsigned int i; 102 103 if (stringset != ETH_SS_STATS && stringset != ETH_SS_PHY_STATS) 104 return; 105 106 for (i = 0; i < __DSA_LOOP_CNT_MAX; i++) 107 memcpy(data + i * ETH_GSTRING_LEN, 108 ps->ports[port].mib[i].name, ETH_GSTRING_LEN); 109 } 110 111 static void dsa_loop_get_ethtool_stats(struct dsa_switch *ds, int port, 112 uint64_t *data) 113 { 114 struct dsa_loop_priv *ps = ds->priv; 115 unsigned int i; 116 117 for (i = 0; i < __DSA_LOOP_CNT_MAX; i++) 118 data[i] = ps->ports[port].mib[i].val; 119 } 120 121 static int dsa_loop_phy_read(struct dsa_switch *ds, int port, int regnum) 122 { 123 struct dsa_loop_priv *ps = ds->priv; 124 struct mii_bus *bus = ps->bus; 125 int ret; 126 127 dev_dbg(ds->dev, "%s\n", __func__); 128 129 ret = mdiobus_read_nested(bus, ps->port_base + port, regnum); 130 if (ret < 0) 131 ps->ports[port].mib[DSA_LOOP_PHY_READ_ERR].val++; 132 else 133 ps->ports[port].mib[DSA_LOOP_PHY_READ_OK].val++; 134 135 return ret; 136 } 137 138 static int dsa_loop_phy_write(struct dsa_switch *ds, int port, 139 int regnum, u16 value) 140 { 141 struct dsa_loop_priv *ps = ds->priv; 142 struct mii_bus *bus = ps->bus; 143 int ret; 144 145 dev_dbg(ds->dev, "%s\n", __func__); 146 147 ret = mdiobus_write_nested(bus, ps->port_base + port, regnum, value); 148 if (ret < 0) 149 ps->ports[port].mib[DSA_LOOP_PHY_WRITE_ERR].val++; 150 else 151 ps->ports[port].mib[DSA_LOOP_PHY_WRITE_OK].val++; 152 153 return ret; 154 } 155 156 static int dsa_loop_port_bridge_join(struct dsa_switch *ds, int port, 157 struct net_device *bridge) 158 { 159 dev_dbg(ds->dev, "%s\n", __func__); 160 161 return 0; 162 } 163 164 static void dsa_loop_port_bridge_leave(struct dsa_switch *ds, int port, 165 struct net_device *bridge) 166 { 167 dev_dbg(ds->dev, "%s\n", __func__); 168 } 169 170 static void dsa_loop_port_stp_state_set(struct dsa_switch *ds, int port, 171 u8 state) 172 { 173 dev_dbg(ds->dev, "%s\n", __func__); 174 } 175 176 static int dsa_loop_port_vlan_filtering(struct dsa_switch *ds, int port, 177 bool vlan_filtering) 178 { 179 dev_dbg(ds->dev, "%s\n", __func__); 180 181 return 0; 182 } 183 184 static int 185 dsa_loop_port_vlan_prepare(struct dsa_switch *ds, int port, 186 const struct switchdev_obj_port_vlan *vlan) 187 { 188 struct dsa_loop_priv *ps = ds->priv; 189 struct mii_bus *bus = ps->bus; 190 191 dev_dbg(ds->dev, "%s\n", __func__); 192 193 /* Just do a sleeping operation to make lockdep checks effective */ 194 mdiobus_read(bus, ps->port_base + port, MII_BMSR); 195 196 if (vlan->vid_end > DSA_LOOP_VLANS) 197 return -ERANGE; 198 199 return 0; 200 } 201 202 static void dsa_loop_port_vlan_add(struct dsa_switch *ds, int port, 203 const struct switchdev_obj_port_vlan *vlan) 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; 238 u16 vid, pvid = ps->pvid; 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 const struct dsa_switch_ops dsa_loop_driver = { 261 .get_tag_protocol = dsa_loop_get_protocol, 262 .setup = dsa_loop_setup, 263 .get_strings = dsa_loop_get_strings, 264 .get_ethtool_stats = dsa_loop_get_ethtool_stats, 265 .get_sset_count = dsa_loop_get_sset_count, 266 .get_ethtool_phy_stats = dsa_loop_get_ethtool_stats, 267 .phy_read = dsa_loop_phy_read, 268 .phy_write = dsa_loop_phy_write, 269 .port_bridge_join = dsa_loop_port_bridge_join, 270 .port_bridge_leave = dsa_loop_port_bridge_leave, 271 .port_stp_state_set = dsa_loop_port_stp_state_set, 272 .port_vlan_filtering = dsa_loop_port_vlan_filtering, 273 .port_vlan_prepare = dsa_loop_port_vlan_prepare, 274 .port_vlan_add = dsa_loop_port_vlan_add, 275 .port_vlan_del = dsa_loop_port_vlan_del, 276 }; 277 278 static int dsa_loop_drv_probe(struct mdio_device *mdiodev) 279 { 280 struct dsa_loop_pdata *pdata = mdiodev->dev.platform_data; 281 struct dsa_loop_priv *ps; 282 struct dsa_switch *ds; 283 284 if (!pdata) 285 return -ENODEV; 286 287 dev_info(&mdiodev->dev, "%s: 0x%0x\n", 288 pdata->name, pdata->enabled_ports); 289 290 ds = dsa_switch_alloc(&mdiodev->dev, DSA_MAX_PORTS); 291 if (!ds) 292 return -ENOMEM; 293 294 ps = devm_kzalloc(&mdiodev->dev, sizeof(*ps), GFP_KERNEL); 295 if (!ps) 296 return -ENOMEM; 297 298 ps->netdev = dev_get_by_name(&init_net, pdata->netdev); 299 if (!ps->netdev) 300 return -EPROBE_DEFER; 301 302 pdata->cd.netdev[DSA_LOOP_CPU_PORT] = &ps->netdev->dev; 303 304 ds->dev = &mdiodev->dev; 305 ds->ops = &dsa_loop_driver; 306 ds->priv = ps; 307 ps->bus = mdiodev->bus; 308 309 dev_set_drvdata(&mdiodev->dev, ds); 310 311 return dsa_register_switch(ds); 312 } 313 314 static void dsa_loop_drv_remove(struct mdio_device *mdiodev) 315 { 316 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev); 317 struct dsa_loop_priv *ps = ds->priv; 318 319 dsa_unregister_switch(ds); 320 dev_put(ps->netdev); 321 } 322 323 static struct mdio_driver dsa_loop_drv = { 324 .mdiodrv.driver = { 325 .name = "dsa-loop", 326 }, 327 .probe = dsa_loop_drv_probe, 328 .remove = dsa_loop_drv_remove, 329 }; 330 331 #define NUM_FIXED_PHYS (DSA_LOOP_NUM_PORTS - 2) 332 333 static int __init dsa_loop_init(void) 334 { 335 struct fixed_phy_status status = { 336 .link = 1, 337 .speed = SPEED_100, 338 .duplex = DUPLEX_FULL, 339 }; 340 unsigned int i; 341 342 for (i = 0; i < NUM_FIXED_PHYS; i++) 343 phydevs[i] = fixed_phy_register(PHY_POLL, &status, -1, NULL); 344 345 return mdio_driver_register(&dsa_loop_drv); 346 } 347 module_init(dsa_loop_init); 348 349 static void __exit dsa_loop_exit(void) 350 { 351 unsigned int i; 352 353 mdio_driver_unregister(&dsa_loop_drv); 354 for (i = 0; i < NUM_FIXED_PHYS; i++) 355 if (!IS_ERR(phydevs[i])) 356 fixed_phy_unregister(phydevs[i]); 357 } 358 module_exit(dsa_loop_exit); 359 360 MODULE_LICENSE("GPL"); 361 MODULE_AUTHOR("Florian Fainelli"); 362 MODULE_DESCRIPTION("DSA loopback driver"); 363