dsa.c 8.2 KB

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