dsa.c 7.1 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/ptp_classify.h>
  26. #include <linux/gpio/consumer.h>
  27. #include <linux/etherdevice.h>
  28. #include "dsa_priv.h"
  29. static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
  30. struct net_device *dev)
  31. {
  32. /* Just return the original SKB */
  33. return skb;
  34. }
  35. static const struct dsa_device_ops none_ops = {
  36. .xmit = dsa_slave_notag_xmit,
  37. .rcv = NULL,
  38. };
  39. const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = {
  40. #ifdef CONFIG_NET_DSA_TAG_BRCM
  41. [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops,
  42. #endif
  43. #ifdef CONFIG_NET_DSA_TAG_BRCM_PREPEND
  44. [DSA_TAG_PROTO_BRCM_PREPEND] = &brcm_prepend_netdev_ops,
  45. #endif
  46. #ifdef CONFIG_NET_DSA_TAG_DSA
  47. [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops,
  48. #endif
  49. #ifdef CONFIG_NET_DSA_TAG_EDSA
  50. [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
  51. #endif
  52. #ifdef CONFIG_NET_DSA_TAG_KSZ
  53. [DSA_TAG_PROTO_KSZ] = &ksz_netdev_ops,
  54. #endif
  55. #ifdef CONFIG_NET_DSA_TAG_LAN9303
  56. [DSA_TAG_PROTO_LAN9303] = &lan9303_netdev_ops,
  57. #endif
  58. #ifdef CONFIG_NET_DSA_TAG_MTK
  59. [DSA_TAG_PROTO_MTK] = &mtk_netdev_ops,
  60. #endif
  61. #ifdef CONFIG_NET_DSA_TAG_QCA
  62. [DSA_TAG_PROTO_QCA] = &qca_netdev_ops,
  63. #endif
  64. #ifdef CONFIG_NET_DSA_TAG_TRAILER
  65. [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops,
  66. #endif
  67. [DSA_TAG_PROTO_NONE] = &none_ops,
  68. };
  69. const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
  70. {
  71. const struct dsa_device_ops *ops;
  72. if (tag_protocol >= DSA_TAG_LAST)
  73. return ERR_PTR(-EINVAL);
  74. ops = dsa_device_ops[tag_protocol];
  75. if (!ops)
  76. return ERR_PTR(-ENOPROTOOPT);
  77. return ops;
  78. }
  79. static int dev_is_class(struct device *dev, void *class)
  80. {
  81. if (dev->class != NULL && !strcmp(dev->class->name, class))
  82. return 1;
  83. return 0;
  84. }
  85. static struct device *dev_find_class(struct device *parent, char *class)
  86. {
  87. if (dev_is_class(parent, class)) {
  88. get_device(parent);
  89. return parent;
  90. }
  91. return device_find_child(parent, class, dev_is_class);
  92. }
  93. struct net_device *dsa_dev_to_net_device(struct device *dev)
  94. {
  95. struct device *d;
  96. d = dev_find_class(dev, "net");
  97. if (d != NULL) {
  98. struct net_device *nd;
  99. nd = to_net_dev(d);
  100. dev_hold(nd);
  101. put_device(d);
  102. return nd;
  103. }
  104. return NULL;
  105. }
  106. EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
  107. /* Determine if we should defer delivery of skb until we have a rx timestamp.
  108. *
  109. * Called from dsa_switch_rcv. For now, this will only work if tagging is
  110. * enabled on the switch. Normally the MAC driver would retrieve the hardware
  111. * timestamp when it reads the packet out of the hardware. However in a DSA
  112. * switch, the DSA driver owning the interface to which the packet is
  113. * delivered is never notified unless we do so here.
  114. */
  115. static bool dsa_skb_defer_rx_timestamp(struct dsa_slave_priv *p,
  116. struct sk_buff *skb)
  117. {
  118. struct dsa_switch *ds = p->dp->ds;
  119. unsigned int type;
  120. if (skb_headroom(skb) < ETH_HLEN)
  121. return false;
  122. __skb_push(skb, ETH_HLEN);
  123. type = ptp_classify_raw(skb);
  124. __skb_pull(skb, ETH_HLEN);
  125. if (type == PTP_CLASS_NONE)
  126. return false;
  127. if (likely(ds->ops->port_rxtstamp))
  128. return ds->ops->port_rxtstamp(ds, p->dp->index, skb, type);
  129. return false;
  130. }
  131. static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
  132. struct packet_type *pt, struct net_device *unused)
  133. {
  134. struct dsa_port *cpu_dp = dev->dsa_ptr;
  135. struct sk_buff *nskb = NULL;
  136. struct pcpu_sw_netstats *s;
  137. struct dsa_slave_priv *p;
  138. if (unlikely(!cpu_dp)) {
  139. kfree_skb(skb);
  140. return 0;
  141. }
  142. skb = skb_unshare(skb, GFP_ATOMIC);
  143. if (!skb)
  144. return 0;
  145. nskb = cpu_dp->rcv(skb, dev, pt);
  146. if (!nskb) {
  147. kfree_skb(skb);
  148. return 0;
  149. }
  150. skb = nskb;
  151. p = netdev_priv(skb->dev);
  152. skb_push(skb, ETH_HLEN);
  153. skb->pkt_type = PACKET_HOST;
  154. skb->protocol = eth_type_trans(skb, skb->dev);
  155. s = this_cpu_ptr(p->stats64);
  156. u64_stats_update_begin(&s->syncp);
  157. s->rx_packets++;
  158. s->rx_bytes += skb->len;
  159. u64_stats_update_end(&s->syncp);
  160. if (dsa_skb_defer_rx_timestamp(p, skb))
  161. return 0;
  162. netif_receive_skb(skb);
  163. return 0;
  164. }
  165. #ifdef CONFIG_PM_SLEEP
  166. static bool dsa_is_port_initialized(struct dsa_switch *ds, int p)
  167. {
  168. return dsa_is_user_port(ds, p) && ds->ports[p].slave;
  169. }
  170. int dsa_switch_suspend(struct dsa_switch *ds)
  171. {
  172. int i, ret = 0;
  173. /* Suspend slave network devices */
  174. for (i = 0; i < ds->num_ports; i++) {
  175. if (!dsa_is_port_initialized(ds, i))
  176. continue;
  177. ret = dsa_slave_suspend(ds->ports[i].slave);
  178. if (ret)
  179. return ret;
  180. }
  181. if (ds->ops->suspend)
  182. ret = ds->ops->suspend(ds);
  183. return ret;
  184. }
  185. EXPORT_SYMBOL_GPL(dsa_switch_suspend);
  186. int dsa_switch_resume(struct dsa_switch *ds)
  187. {
  188. int i, ret = 0;
  189. if (ds->ops->resume)
  190. ret = ds->ops->resume(ds);
  191. if (ret)
  192. return ret;
  193. /* Resume slave network devices */
  194. for (i = 0; i < ds->num_ports; i++) {
  195. if (!dsa_is_port_initialized(ds, i))
  196. continue;
  197. ret = dsa_slave_resume(ds->ports[i].slave);
  198. if (ret)
  199. return ret;
  200. }
  201. return 0;
  202. }
  203. EXPORT_SYMBOL_GPL(dsa_switch_resume);
  204. #endif
  205. static struct packet_type dsa_pack_type __read_mostly = {
  206. .type = cpu_to_be16(ETH_P_XDSA),
  207. .func = dsa_switch_rcv,
  208. };
  209. static struct workqueue_struct *dsa_owq;
  210. bool dsa_schedule_work(struct work_struct *work)
  211. {
  212. return queue_work(dsa_owq, work);
  213. }
  214. static ATOMIC_NOTIFIER_HEAD(dsa_notif_chain);
  215. int register_dsa_notifier(struct notifier_block *nb)
  216. {
  217. return atomic_notifier_chain_register(&dsa_notif_chain, nb);
  218. }
  219. EXPORT_SYMBOL_GPL(register_dsa_notifier);
  220. int unregister_dsa_notifier(struct notifier_block *nb)
  221. {
  222. return atomic_notifier_chain_unregister(&dsa_notif_chain, nb);
  223. }
  224. EXPORT_SYMBOL_GPL(unregister_dsa_notifier);
  225. int call_dsa_notifiers(unsigned long val, struct net_device *dev,
  226. struct dsa_notifier_info *info)
  227. {
  228. info->dev = dev;
  229. return atomic_notifier_call_chain(&dsa_notif_chain, val, info);
  230. }
  231. EXPORT_SYMBOL_GPL(call_dsa_notifiers);
  232. static int __init dsa_init_module(void)
  233. {
  234. int rc;
  235. dsa_owq = alloc_ordered_workqueue("dsa_ordered",
  236. WQ_MEM_RECLAIM);
  237. if (!dsa_owq)
  238. return -ENOMEM;
  239. rc = dsa_slave_register_notifier();
  240. if (rc)
  241. return rc;
  242. rc = dsa_legacy_register();
  243. if (rc)
  244. return rc;
  245. dev_add_pack(&dsa_pack_type);
  246. return 0;
  247. }
  248. module_init(dsa_init_module);
  249. static void __exit dsa_cleanup_module(void)
  250. {
  251. dsa_slave_unregister_notifier();
  252. dev_remove_pack(&dsa_pack_type);
  253. dsa_legacy_unregister();
  254. destroy_workqueue(dsa_owq);
  255. }
  256. module_exit(dsa_cleanup_module);
  257. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  258. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  259. MODULE_LICENSE("GPL");
  260. MODULE_ALIAS("platform:dsa");