dsa.c 6.0 KB

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