dsa.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 <net/dsa.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_DSA
  40. [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops,
  41. #endif
  42. #ifdef CONFIG_NET_DSA_TAG_EDSA
  43. [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
  44. #endif
  45. #ifdef CONFIG_NET_DSA_TAG_TRAILER
  46. [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops,
  47. #endif
  48. #ifdef CONFIG_NET_DSA_TAG_BRCM
  49. [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops,
  50. #endif
  51. #ifdef CONFIG_NET_DSA_TAG_QCA
  52. [DSA_TAG_PROTO_QCA] = &qca_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_LAN9303
  58. [DSA_TAG_PROTO_LAN9303] = &lan9303_netdev_ops,
  59. #endif
  60. [DSA_TAG_PROTO_NONE] = &none_ops,
  61. };
  62. int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct device *dev,
  63. struct dsa_port *dport, int port)
  64. {
  65. struct device_node *port_dn = dport->dn;
  66. struct phy_device *phydev;
  67. int ret, mode;
  68. if (of_phy_is_fixed_link(port_dn)) {
  69. ret = of_phy_register_fixed_link(port_dn);
  70. if (ret) {
  71. dev_err(dev, "failed to register fixed PHY\n");
  72. return ret;
  73. }
  74. phydev = of_phy_find_device(port_dn);
  75. mode = of_get_phy_mode(port_dn);
  76. if (mode < 0)
  77. mode = PHY_INTERFACE_MODE_NA;
  78. phydev->interface = mode;
  79. genphy_config_init(phydev);
  80. genphy_read_status(phydev);
  81. if (ds->ops->adjust_link)
  82. ds->ops->adjust_link(ds, port, phydev);
  83. put_device(&phydev->mdio.dev);
  84. }
  85. return 0;
  86. }
  87. const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
  88. {
  89. const struct dsa_device_ops *ops;
  90. if (tag_protocol >= DSA_TAG_LAST)
  91. return ERR_PTR(-EINVAL);
  92. ops = dsa_device_ops[tag_protocol];
  93. if (!ops)
  94. return ERR_PTR(-ENOPROTOOPT);
  95. return ops;
  96. }
  97. int dsa_cpu_port_ethtool_setup(struct dsa_switch *ds)
  98. {
  99. struct net_device *master;
  100. struct ethtool_ops *cpu_ops;
  101. master = ds->dst->master_netdev;
  102. if (ds->master_netdev)
  103. master = ds->master_netdev;
  104. cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL);
  105. if (!cpu_ops)
  106. return -ENOMEM;
  107. memcpy(&ds->dst->master_ethtool_ops, master->ethtool_ops,
  108. sizeof(struct ethtool_ops));
  109. ds->dst->master_orig_ethtool_ops = master->ethtool_ops;
  110. memcpy(cpu_ops, &ds->dst->master_ethtool_ops,
  111. sizeof(struct ethtool_ops));
  112. dsa_cpu_port_ethtool_init(cpu_ops);
  113. master->ethtool_ops = cpu_ops;
  114. return 0;
  115. }
  116. void dsa_cpu_port_ethtool_restore(struct dsa_switch *ds)
  117. {
  118. struct net_device *master;
  119. master = ds->dst->master_netdev;
  120. if (ds->master_netdev)
  121. master = ds->master_netdev;
  122. master->ethtool_ops = ds->dst->master_orig_ethtool_ops;
  123. }
  124. void dsa_cpu_dsa_destroy(struct dsa_port *port)
  125. {
  126. struct device_node *port_dn = port->dn;
  127. if (of_phy_is_fixed_link(port_dn))
  128. of_phy_deregister_fixed_link(port_dn);
  129. }
  130. static int dev_is_class(struct device *dev, void *class)
  131. {
  132. if (dev->class != NULL && !strcmp(dev->class->name, class))
  133. return 1;
  134. return 0;
  135. }
  136. static struct device *dev_find_class(struct device *parent, char *class)
  137. {
  138. if (dev_is_class(parent, class)) {
  139. get_device(parent);
  140. return parent;
  141. }
  142. return device_find_child(parent, class, dev_is_class);
  143. }
  144. struct net_device *dsa_dev_to_net_device(struct device *dev)
  145. {
  146. struct device *d;
  147. d = dev_find_class(dev, "net");
  148. if (d != NULL) {
  149. struct net_device *nd;
  150. nd = to_net_dev(d);
  151. dev_hold(nd);
  152. put_device(d);
  153. return nd;
  154. }
  155. return NULL;
  156. }
  157. EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
  158. static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
  159. struct packet_type *pt, struct net_device *orig_dev)
  160. {
  161. struct dsa_switch_tree *dst = dev->dsa_ptr;
  162. struct sk_buff *nskb = NULL;
  163. if (unlikely(dst == NULL)) {
  164. kfree_skb(skb);
  165. return 0;
  166. }
  167. skb = skb_unshare(skb, GFP_ATOMIC);
  168. if (!skb)
  169. return 0;
  170. nskb = dst->rcv(skb, dev, pt, orig_dev);
  171. if (!nskb) {
  172. kfree_skb(skb);
  173. return 0;
  174. }
  175. skb = nskb;
  176. skb_push(skb, ETH_HLEN);
  177. skb->pkt_type = PACKET_HOST;
  178. skb->protocol = eth_type_trans(skb, skb->dev);
  179. skb->dev->stats.rx_packets++;
  180. skb->dev->stats.rx_bytes += skb->len;
  181. netif_receive_skb(skb);
  182. return 0;
  183. }
  184. static struct packet_type dsa_pack_type __read_mostly = {
  185. .type = cpu_to_be16(ETH_P_XDSA),
  186. .func = dsa_switch_rcv,
  187. };
  188. static int __init dsa_init_module(void)
  189. {
  190. int rc;
  191. rc = dsa_slave_register_notifier();
  192. if (rc)
  193. return rc;
  194. rc = dsa_legacy_register();
  195. if (rc)
  196. return rc;
  197. dev_add_pack(&dsa_pack_type);
  198. return 0;
  199. }
  200. module_init(dsa_init_module);
  201. static void __exit dsa_cleanup_module(void)
  202. {
  203. dsa_slave_unregister_notifier();
  204. dev_remove_pack(&dsa_pack_type);
  205. dsa_legacy_unregister();
  206. }
  207. module_exit(dsa_cleanup_module);
  208. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  209. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  210. MODULE_LICENSE("GPL");
  211. MODULE_ALIAS("platform:dsa");