dsa_loop.c 8.6 KB

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