dsa.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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_BRCM_PREPEND
  43. [DSA_TAG_PROTO_BRCM_PREPEND] = &brcm_prepend_netdev_ops,
  44. #endif
  45. #ifdef CONFIG_NET_DSA_TAG_DSA
  46. [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops,
  47. #endif
  48. #ifdef CONFIG_NET_DSA_TAG_EDSA
  49. [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
  50. #endif
  51. #ifdef CONFIG_NET_DSA_TAG_KSZ
  52. [DSA_TAG_PROTO_KSZ] = &ksz_netdev_ops,
  53. #endif
  54. #ifdef CONFIG_NET_DSA_TAG_LAN9303
  55. [DSA_TAG_PROTO_LAN9303] = &lan9303_netdev_ops,
  56. #endif
  57. #ifdef CONFIG_NET_DSA_TAG_MTK
  58. [DSA_TAG_PROTO_MTK] = &mtk_netdev_ops,
  59. #endif
  60. #ifdef CONFIG_NET_DSA_TAG_QCA
  61. [DSA_TAG_PROTO_QCA] = &qca_netdev_ops,
  62. #endif
  63. #ifdef CONFIG_NET_DSA_TAG_TRAILER
  64. [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops,
  65. #endif
  66. [DSA_TAG_PROTO_NONE] = &none_ops,
  67. };
  68. const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
  69. {
  70. const struct dsa_device_ops *ops;
  71. if (tag_protocol >= DSA_TAG_LAST)
  72. return ERR_PTR(-EINVAL);
  73. ops = dsa_device_ops[tag_protocol];
  74. if (!ops)
  75. return ERR_PTR(-ENOPROTOOPT);
  76. return ops;
  77. }
  78. static int dev_is_class(struct device *dev, void *class)
  79. {
  80. if (dev->class != NULL && !strcmp(dev->class->name, class))
  81. return 1;
  82. return 0;
  83. }
  84. static struct device *dev_find_class(struct device *parent, char *class)
  85. {
  86. if (dev_is_class(parent, class)) {
  87. get_device(parent);
  88. return parent;
  89. }
  90. return device_find_child(parent, class, dev_is_class);
  91. }
  92. struct net_device *dsa_dev_to_net_device(struct device *dev)
  93. {
  94. struct device *d;
  95. d = dev_find_class(dev, "net");
  96. if (d != NULL) {
  97. struct net_device *nd;
  98. nd = to_net_dev(d);
  99. dev_hold(nd);
  100. put_device(d);
  101. return nd;
  102. }
  103. return NULL;
  104. }
  105. EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
  106. static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
  107. struct packet_type *pt, struct net_device *unused)
  108. {
  109. struct dsa_port *cpu_dp = dev->dsa_ptr;
  110. struct sk_buff *nskb = NULL;
  111. struct pcpu_sw_netstats *s;
  112. struct dsa_slave_priv *p;
  113. if (unlikely(!cpu_dp)) {
  114. kfree_skb(skb);
  115. return 0;
  116. }
  117. skb = skb_unshare(skb, GFP_ATOMIC);
  118. if (!skb)
  119. return 0;
  120. nskb = cpu_dp->rcv(skb, dev, pt);
  121. if (!nskb) {
  122. kfree_skb(skb);
  123. return 0;
  124. }
  125. skb = nskb;
  126. p = netdev_priv(skb->dev);
  127. skb_push(skb, ETH_HLEN);
  128. skb->pkt_type = PACKET_HOST;
  129. skb->protocol = eth_type_trans(skb, skb->dev);
  130. s = this_cpu_ptr(p->stats64);
  131. u64_stats_update_begin(&s->syncp);
  132. s->rx_packets++;
  133. s->rx_bytes += skb->len;
  134. u64_stats_update_end(&s->syncp);
  135. netif_receive_skb(skb);
  136. return 0;
  137. }
  138. #ifdef CONFIG_PM_SLEEP
  139. static bool dsa_is_port_initialized(struct dsa_switch *ds, int p)
  140. {
  141. return dsa_is_user_port(ds, p) && ds->ports[p].slave;
  142. }
  143. int dsa_switch_suspend(struct dsa_switch *ds)
  144. {
  145. int i, ret = 0;
  146. /* Suspend slave network devices */
  147. for (i = 0; i < ds->num_ports; i++) {
  148. if (!dsa_is_port_initialized(ds, i))
  149. continue;
  150. ret = dsa_slave_suspend(ds->ports[i].slave);
  151. if (ret)
  152. return ret;
  153. }
  154. if (ds->ops->suspend)
  155. ret = ds->ops->suspend(ds);
  156. return ret;
  157. }
  158. EXPORT_SYMBOL_GPL(dsa_switch_suspend);
  159. int dsa_switch_resume(struct dsa_switch *ds)
  160. {
  161. int i, ret = 0;
  162. if (ds->ops->resume)
  163. ret = ds->ops->resume(ds);
  164. if (ret)
  165. return ret;
  166. /* Resume slave network devices */
  167. for (i = 0; i < ds->num_ports; i++) {
  168. if (!dsa_is_port_initialized(ds, i))
  169. continue;
  170. ret = dsa_slave_resume(ds->ports[i].slave);
  171. if (ret)
  172. return ret;
  173. }
  174. return 0;
  175. }
  176. EXPORT_SYMBOL_GPL(dsa_switch_resume);
  177. #endif
  178. static struct packet_type dsa_pack_type __read_mostly = {
  179. .type = cpu_to_be16(ETH_P_XDSA),
  180. .func = dsa_switch_rcv,
  181. };
  182. static struct workqueue_struct *dsa_owq;
  183. bool dsa_schedule_work(struct work_struct *work)
  184. {
  185. return queue_work(dsa_owq, work);
  186. }
  187. static ATOMIC_NOTIFIER_HEAD(dsa_notif_chain);
  188. int register_dsa_notifier(struct notifier_block *nb)
  189. {
  190. return atomic_notifier_chain_register(&dsa_notif_chain, nb);
  191. }
  192. EXPORT_SYMBOL_GPL(register_dsa_notifier);
  193. int unregister_dsa_notifier(struct notifier_block *nb)
  194. {
  195. return atomic_notifier_chain_unregister(&dsa_notif_chain, nb);
  196. }
  197. EXPORT_SYMBOL_GPL(unregister_dsa_notifier);
  198. int call_dsa_notifiers(unsigned long val, struct net_device *dev,
  199. struct dsa_notifier_info *info)
  200. {
  201. info->dev = dev;
  202. return atomic_notifier_call_chain(&dsa_notif_chain, val, info);
  203. }
  204. EXPORT_SYMBOL_GPL(call_dsa_notifiers);
  205. static int __init dsa_init_module(void)
  206. {
  207. int rc;
  208. dsa_owq = alloc_ordered_workqueue("dsa_ordered",
  209. WQ_MEM_RECLAIM);
  210. if (!dsa_owq)
  211. return -ENOMEM;
  212. rc = dsa_slave_register_notifier();
  213. if (rc)
  214. return rc;
  215. rc = dsa_legacy_register();
  216. if (rc)
  217. return rc;
  218. dev_add_pack(&dsa_pack_type);
  219. return 0;
  220. }
  221. module_init(dsa_init_module);
  222. static void __exit dsa_cleanup_module(void)
  223. {
  224. dsa_slave_unregister_notifier();
  225. dev_remove_pack(&dsa_pack_type);
  226. dsa_legacy_unregister();
  227. destroy_workqueue(dsa_owq);
  228. }
  229. module_exit(dsa_cleanup_module);
  230. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  231. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  232. MODULE_LICENSE("GPL");
  233. MODULE_ALIAS("platform:dsa");