dsa.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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/of.h>
  17. #include <linux/of_mdio.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/of_net.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/sysfs.h>
  23. #include <linux/phy_fixed.h>
  24. #include <linux/gpio/consumer.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_DSA
  42. [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops,
  43. #endif
  44. #ifdef CONFIG_NET_DSA_TAG_EDSA
  45. [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
  46. #endif
  47. #ifdef CONFIG_NET_DSA_TAG_KSZ
  48. [DSA_TAG_PROTO_KSZ] = &ksz_netdev_ops,
  49. #endif
  50. #ifdef CONFIG_NET_DSA_TAG_LAN9303
  51. [DSA_TAG_PROTO_LAN9303] = &lan9303_netdev_ops,
  52. #endif
  53. #ifdef CONFIG_NET_DSA_TAG_MTK
  54. [DSA_TAG_PROTO_MTK] = &mtk_netdev_ops,
  55. #endif
  56. #ifdef CONFIG_NET_DSA_TAG_QCA
  57. [DSA_TAG_PROTO_QCA] = &qca_netdev_ops,
  58. #endif
  59. #ifdef CONFIG_NET_DSA_TAG_TRAILER
  60. [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops,
  61. #endif
  62. [DSA_TAG_PROTO_NONE] = &none_ops,
  63. };
  64. int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct device *dev,
  65. struct dsa_port *dport, int port)
  66. {
  67. struct device_node *port_dn = dport->dn;
  68. struct phy_device *phydev;
  69. int ret, mode;
  70. if (of_phy_is_fixed_link(port_dn)) {
  71. ret = of_phy_register_fixed_link(port_dn);
  72. if (ret) {
  73. dev_err(dev, "failed to register fixed PHY\n");
  74. return ret;
  75. }
  76. phydev = of_phy_find_device(port_dn);
  77. mode = of_get_phy_mode(port_dn);
  78. if (mode < 0)
  79. mode = PHY_INTERFACE_MODE_NA;
  80. phydev->interface = mode;
  81. genphy_config_init(phydev);
  82. genphy_read_status(phydev);
  83. if (ds->ops->adjust_link)
  84. ds->ops->adjust_link(ds, port, phydev);
  85. put_device(&phydev->mdio.dev);
  86. }
  87. return 0;
  88. }
  89. const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
  90. {
  91. const struct dsa_device_ops *ops;
  92. if (tag_protocol >= DSA_TAG_LAST)
  93. return ERR_PTR(-EINVAL);
  94. ops = dsa_device_ops[tag_protocol];
  95. if (!ops)
  96. return ERR_PTR(-ENOPROTOOPT);
  97. return ops;
  98. }
  99. int dsa_cpu_port_ethtool_setup(struct dsa_port *cpu_dp)
  100. {
  101. struct dsa_switch *ds = cpu_dp->ds;
  102. struct net_device *master;
  103. struct ethtool_ops *cpu_ops;
  104. master = cpu_dp->netdev;
  105. cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL);
  106. if (!cpu_ops)
  107. return -ENOMEM;
  108. memcpy(&cpu_dp->ethtool_ops, master->ethtool_ops,
  109. sizeof(struct ethtool_ops));
  110. cpu_dp->orig_ethtool_ops = master->ethtool_ops;
  111. memcpy(cpu_ops, &cpu_dp->ethtool_ops,
  112. sizeof(struct ethtool_ops));
  113. dsa_cpu_port_ethtool_init(cpu_ops);
  114. master->ethtool_ops = cpu_ops;
  115. return 0;
  116. }
  117. void dsa_cpu_port_ethtool_restore(struct dsa_port *cpu_dp)
  118. {
  119. cpu_dp->netdev->ethtool_ops = cpu_dp->orig_ethtool_ops;
  120. }
  121. void dsa_cpu_dsa_destroy(struct dsa_port *port)
  122. {
  123. struct device_node *port_dn = port->dn;
  124. if (of_phy_is_fixed_link(port_dn))
  125. of_phy_deregister_fixed_link(port_dn);
  126. }
  127. static int dev_is_class(struct device *dev, void *class)
  128. {
  129. if (dev->class != NULL && !strcmp(dev->class->name, class))
  130. return 1;
  131. return 0;
  132. }
  133. static struct device *dev_find_class(struct device *parent, char *class)
  134. {
  135. if (dev_is_class(parent, class)) {
  136. get_device(parent);
  137. return parent;
  138. }
  139. return device_find_child(parent, class, dev_is_class);
  140. }
  141. struct net_device *dsa_dev_to_net_device(struct device *dev)
  142. {
  143. struct device *d;
  144. d = dev_find_class(dev, "net");
  145. if (d != NULL) {
  146. struct net_device *nd;
  147. nd = to_net_dev(d);
  148. dev_hold(nd);
  149. put_device(d);
  150. return nd;
  151. }
  152. return NULL;
  153. }
  154. EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
  155. static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
  156. struct packet_type *pt, struct net_device *orig_dev)
  157. {
  158. struct dsa_switch_tree *dst = dev->dsa_ptr;
  159. struct sk_buff *nskb = NULL;
  160. if (unlikely(dst == NULL)) {
  161. kfree_skb(skb);
  162. return 0;
  163. }
  164. skb = skb_unshare(skb, GFP_ATOMIC);
  165. if (!skb)
  166. return 0;
  167. nskb = dst->rcv(skb, dev, pt, orig_dev);
  168. if (!nskb) {
  169. kfree_skb(skb);
  170. return 0;
  171. }
  172. skb = nskb;
  173. skb_push(skb, ETH_HLEN);
  174. skb->pkt_type = PACKET_HOST;
  175. skb->protocol = eth_type_trans(skb, skb->dev);
  176. skb->dev->stats.rx_packets++;
  177. skb->dev->stats.rx_bytes += skb->len;
  178. netif_receive_skb(skb);
  179. return 0;
  180. }
  181. #ifdef CONFIG_PM_SLEEP
  182. int dsa_switch_suspend(struct dsa_switch *ds)
  183. {
  184. int i, ret = 0;
  185. /* Suspend slave network devices */
  186. for (i = 0; i < ds->num_ports; i++) {
  187. if (!dsa_is_port_initialized(ds, i))
  188. continue;
  189. ret = dsa_slave_suspend(ds->ports[i].netdev);
  190. if (ret)
  191. return ret;
  192. }
  193. if (ds->ops->suspend)
  194. ret = ds->ops->suspend(ds);
  195. return ret;
  196. }
  197. EXPORT_SYMBOL_GPL(dsa_switch_suspend);
  198. int dsa_switch_resume(struct dsa_switch *ds)
  199. {
  200. int i, ret = 0;
  201. if (ds->ops->resume)
  202. ret = ds->ops->resume(ds);
  203. if (ret)
  204. return ret;
  205. /* Resume slave network devices */
  206. for (i = 0; i < ds->num_ports; i++) {
  207. if (!dsa_is_port_initialized(ds, i))
  208. continue;
  209. ret = dsa_slave_resume(ds->ports[i].netdev);
  210. if (ret)
  211. return ret;
  212. }
  213. return 0;
  214. }
  215. EXPORT_SYMBOL_GPL(dsa_switch_resume);
  216. #endif
  217. static struct packet_type dsa_pack_type __read_mostly = {
  218. .type = cpu_to_be16(ETH_P_XDSA),
  219. .func = dsa_switch_rcv,
  220. };
  221. static int __init dsa_init_module(void)
  222. {
  223. int rc;
  224. rc = dsa_slave_register_notifier();
  225. if (rc)
  226. return rc;
  227. rc = dsa_legacy_register();
  228. if (rc)
  229. return rc;
  230. dev_add_pack(&dsa_pack_type);
  231. return 0;
  232. }
  233. module_init(dsa_init_module);
  234. static void __exit dsa_cleanup_module(void)
  235. {
  236. dsa_slave_unregister_notifier();
  237. dev_remove_pack(&dsa_pack_type);
  238. dsa_legacy_unregister();
  239. }
  240. module_exit(dsa_cleanup_module);
  241. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  242. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  243. MODULE_LICENSE("GPL");
  244. MODULE_ALIAS("platform:dsa");