dsa_loop.c 9.3 KB

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