dsa.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * net/dsa/dsa.c - Hardware switch handling
  3. * Copyright (c) 2008-2009 Marvell Semiconductor
  4. * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
  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/device.h>
  12. #include <linux/list.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #include <linux/notifier.h>
  17. #include <linux/of.h>
  18. #include <linux/of_mdio.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/of_net.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/sysfs.h>
  24. #include <linux/phy_fixed.h>
  25. #include <linux/gpio/consumer.h>
  26. #include <linux/etherdevice.h>
  27. #include "dsa_priv.h"
  28. static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
  29. struct net_device *dev)
  30. {
  31. /* Just return the original SKB */
  32. return skb;
  33. }
  34. static const struct dsa_device_ops none_ops = {
  35. .xmit = dsa_slave_notag_xmit,
  36. .rcv = NULL,
  37. };
  38. const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = {
  39. #ifdef CONFIG_NET_DSA_TAG_BRCM
  40. [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops,
  41. #endif
  42. #ifdef CONFIG_NET_DSA_TAG_DSA
  43. [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops,
  44. #endif
  45. #ifdef CONFIG_NET_DSA_TAG_EDSA
  46. [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
  47. #endif
  48. #ifdef CONFIG_NET_DSA_TAG_KSZ
  49. [DSA_TAG_PROTO_KSZ] = &ksz_netdev_ops,
  50. #endif
  51. #ifdef CONFIG_NET_DSA_TAG_LAN9303
  52. [DSA_TAG_PROTO_LAN9303] = &lan9303_netdev_ops,
  53. #endif
  54. #ifdef CONFIG_NET_DSA_TAG_MTK
  55. [DSA_TAG_PROTO_MTK] = &mtk_netdev_ops,
  56. #endif
  57. #ifdef CONFIG_NET_DSA_TAG_QCA
  58. [DSA_TAG_PROTO_QCA] = &qca_netdev_ops,
  59. #endif
  60. #ifdef CONFIG_NET_DSA_TAG_TRAILER
  61. [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops,
  62. #endif
  63. [DSA_TAG_PROTO_NONE] = &none_ops,
  64. };
  65. int dsa_cpu_dsa_setup(struct dsa_port *port)
  66. {
  67. struct device_node *port_dn = port->dn;
  68. struct dsa_switch *ds = port->ds;
  69. struct phy_device *phydev;
  70. int ret, mode;
  71. if (of_phy_is_fixed_link(port_dn)) {
  72. ret = of_phy_register_fixed_link(port_dn);
  73. if (ret) {
  74. dev_err(ds->dev, "failed to register fixed PHY\n");
  75. return ret;
  76. }
  77. phydev = of_phy_find_device(port_dn);
  78. mode = of_get_phy_mode(port_dn);
  79. if (mode < 0)
  80. mode = PHY_INTERFACE_MODE_NA;
  81. phydev->interface = mode;
  82. genphy_config_init(phydev);
  83. genphy_read_status(phydev);
  84. if (ds->ops->adjust_link)
  85. ds->ops->adjust_link(ds, port->index, phydev);
  86. put_device(&phydev->mdio.dev);
  87. }
  88. return 0;
  89. }
  90. const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
  91. {
  92. const struct dsa_device_ops *ops;
  93. if (tag_protocol >= DSA_TAG_LAST)
  94. return ERR_PTR(-EINVAL);
  95. ops = dsa_device_ops[tag_protocol];
  96. if (!ops)
  97. return ERR_PTR(-ENOPROTOOPT);
  98. return ops;
  99. }
  100. void dsa_cpu_dsa_destroy(struct dsa_port *port)
  101. {
  102. struct device_node *port_dn = port->dn;
  103. if (of_phy_is_fixed_link(port_dn))
  104. of_phy_deregister_fixed_link(port_dn);
  105. }
  106. static int dev_is_class(struct device *dev, void *class)
  107. {
  108. if (dev->class != NULL && !strcmp(dev->class->name, class))
  109. return 1;
  110. return 0;
  111. }
  112. static struct device *dev_find_class(struct device *parent, char *class)
  113. {
  114. if (dev_is_class(parent, class)) {
  115. get_device(parent);
  116. return parent;
  117. }
  118. return device_find_child(parent, class, dev_is_class);
  119. }
  120. struct net_device *dsa_dev_to_net_device(struct device *dev)
  121. {
  122. struct device *d;
  123. d = dev_find_class(dev, "net");
  124. if (d != NULL) {
  125. struct net_device *nd;
  126. nd = to_net_dev(d);
  127. dev_hold(nd);
  128. put_device(d);
  129. return nd;
  130. }
  131. return NULL;
  132. }
  133. EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
  134. static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
  135. struct packet_type *pt, struct net_device *unused)
  136. {
  137. struct dsa_port *cpu_dp = dev->dsa_ptr;
  138. struct sk_buff *nskb = NULL;
  139. struct pcpu_sw_netstats *s;
  140. struct dsa_slave_priv *p;
  141. if (unlikely(!cpu_dp)) {
  142. kfree_skb(skb);
  143. return 0;
  144. }
  145. skb = skb_unshare(skb, GFP_ATOMIC);
  146. if (!skb)
  147. return 0;
  148. nskb = cpu_dp->rcv(skb, dev, pt);
  149. if (!nskb) {
  150. kfree_skb(skb);
  151. return 0;
  152. }
  153. skb = nskb;
  154. p = netdev_priv(skb->dev);
  155. skb_push(skb, ETH_HLEN);
  156. skb->pkt_type = PACKET_HOST;
  157. skb->protocol = eth_type_trans(skb, skb->dev);
  158. s = this_cpu_ptr(p->stats64);
  159. u64_stats_update_begin(&s->syncp);
  160. s->rx_packets++;
  161. s->rx_bytes += skb->len;
  162. u64_stats_update_end(&s->syncp);
  163. netif_receive_skb(skb);
  164. return 0;
  165. }
  166. #ifdef CONFIG_PM_SLEEP
  167. static bool dsa_is_port_initialized(struct dsa_switch *ds, int p)
  168. {
  169. return ds->enabled_port_mask & (1 << p) && ds->ports[p].slave;
  170. }
  171. int dsa_switch_suspend(struct dsa_switch *ds)
  172. {
  173. int i, ret = 0;
  174. /* Suspend slave network devices */
  175. for (i = 0; i < ds->num_ports; i++) {
  176. if (!dsa_is_port_initialized(ds, i))
  177. continue;
  178. ret = dsa_slave_suspend(ds->ports[i].slave);
  179. if (ret)
  180. return ret;
  181. }
  182. if (ds->ops->suspend)
  183. ret = ds->ops->suspend(ds);
  184. return ret;
  185. }
  186. EXPORT_SYMBOL_GPL(dsa_switch_suspend);
  187. int dsa_switch_resume(struct dsa_switch *ds)
  188. {
  189. int i, ret = 0;
  190. if (ds->ops->resume)
  191. ret = ds->ops->resume(ds);
  192. if (ret)
  193. return ret;
  194. /* Resume slave network devices */
  195. for (i = 0; i < ds->num_ports; i++) {
  196. if (!dsa_is_port_initialized(ds, i))
  197. continue;
  198. ret = dsa_slave_resume(ds->ports[i].slave);
  199. if (ret)
  200. return ret;
  201. }
  202. return 0;
  203. }
  204. EXPORT_SYMBOL_GPL(dsa_switch_resume);
  205. #endif
  206. static struct packet_type dsa_pack_type __read_mostly = {
  207. .type = cpu_to_be16(ETH_P_XDSA),
  208. .func = dsa_switch_rcv,
  209. };
  210. static struct workqueue_struct *dsa_owq;
  211. bool dsa_schedule_work(struct work_struct *work)
  212. {
  213. return queue_work(dsa_owq, work);
  214. }
  215. static ATOMIC_NOTIFIER_HEAD(dsa_notif_chain);
  216. int register_dsa_notifier(struct notifier_block *nb)
  217. {
  218. return atomic_notifier_chain_register(&dsa_notif_chain, nb);
  219. }
  220. EXPORT_SYMBOL_GPL(register_dsa_notifier);
  221. int unregister_dsa_notifier(struct notifier_block *nb)
  222. {
  223. return atomic_notifier_chain_unregister(&dsa_notif_chain, nb);
  224. }
  225. EXPORT_SYMBOL_GPL(unregister_dsa_notifier);
  226. int call_dsa_notifiers(unsigned long val, struct net_device *dev,
  227. struct dsa_notifier_info *info)
  228. {
  229. info->dev = dev;
  230. return atomic_notifier_call_chain(&dsa_notif_chain, val, info);
  231. }
  232. EXPORT_SYMBOL_GPL(call_dsa_notifiers);
  233. static int __init dsa_init_module(void)
  234. {
  235. int rc;
  236. dsa_owq = alloc_ordered_workqueue("dsa_ordered",
  237. WQ_MEM_RECLAIM);
  238. if (!dsa_owq)
  239. return -ENOMEM;
  240. rc = dsa_slave_register_notifier();
  241. if (rc)
  242. return rc;
  243. rc = dsa_legacy_register();
  244. if (rc)
  245. return rc;
  246. dev_add_pack(&dsa_pack_type);
  247. return 0;
  248. }
  249. module_init(dsa_init_module);
  250. static void __exit dsa_cleanup_module(void)
  251. {
  252. dsa_slave_unregister_notifier();
  253. dev_remove_pack(&dsa_pack_type);
  254. dsa_legacy_unregister();
  255. destroy_workqueue(dsa_owq);
  256. }
  257. module_exit(dsa_cleanup_module);
  258. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  259. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  260. MODULE_LICENSE("GPL");
  261. MODULE_ALIAS("platform:dsa");