tag_lan9303.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C) 2017 Pengutronix, Juergen Borleis <jbe@pengutronix.de>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/dsa/lan9303.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/list.h>
  17. #include <linux/slab.h>
  18. #include "dsa_priv.h"
  19. /* To define the outgoing port and to discover the incoming port a regular
  20. * VLAN tag is used by the LAN9303. But its VID meaning is 'special':
  21. *
  22. * Dest MAC Src MAC TAG Type
  23. * ...| 1 2 3 4 5 6 | 1 2 3 4 5 6 | 1 2 3 4 | 1 2 |...
  24. * |<------->|
  25. * TAG:
  26. * |<------------->|
  27. * | 1 2 | 3 4 |
  28. * TPID VID
  29. * 0x8100
  30. *
  31. * VID bit 3 indicates a request for an ALR lookup.
  32. *
  33. * If VID bit 3 is zero, then bits 0 and 1 specify the destination port
  34. * (0, 1, 2) or broadcast (3) or the source port (1, 2).
  35. *
  36. * VID bit 4 is used to specify if the STP port state should be overridden.
  37. * Required when no forwarding between the external ports should happen.
  38. */
  39. #define LAN9303_TAG_LEN 4
  40. # define LAN9303_TAG_TX_USE_ALR BIT(3)
  41. # define LAN9303_TAG_TX_STP_OVERRIDE BIT(4)
  42. # define LAN9303_TAG_RX_IGMP BIT(3)
  43. # define LAN9303_TAG_RX_STP BIT(4)
  44. # define LAN9303_TAG_RX_TRAPPED_TO_CPU (LAN9303_TAG_RX_IGMP | \
  45. LAN9303_TAG_RX_STP)
  46. /* Decide whether to transmit using ALR lookup, or transmit directly to
  47. * port using tag. ALR learning is performed only when using ALR lookup.
  48. * If the two external ports are bridged and the frame is unicast,
  49. * then use ALR lookup to allow ALR learning on CPU port.
  50. * Otherwise transmit directly to port with STP state override.
  51. * See also: lan9303_separate_ports() and lan9303.pdf 6.4.10.1
  52. */
  53. static int lan9303_xmit_use_arl(struct dsa_port *dp, u8 *dest_addr)
  54. {
  55. struct lan9303 *chip = dp->ds->priv;
  56. return chip->is_bridged && !is_multicast_ether_addr(dest_addr);
  57. }
  58. static struct sk_buff *lan9303_xmit(struct sk_buff *skb, struct net_device *dev)
  59. {
  60. struct dsa_port *dp = dsa_slave_to_port(dev);
  61. u16 *lan9303_tag;
  62. /* insert a special VLAN tag between the MAC addresses
  63. * and the current ethertype field.
  64. */
  65. if (skb_cow_head(skb, LAN9303_TAG_LEN) < 0) {
  66. dev_dbg(&dev->dev,
  67. "Cannot make room for the special tag. Dropping packet\n");
  68. return NULL;
  69. }
  70. /* provide 'LAN9303_TAG_LEN' bytes additional space */
  71. skb_push(skb, LAN9303_TAG_LEN);
  72. /* make room between MACs and Ether-Type */
  73. memmove(skb->data, skb->data + LAN9303_TAG_LEN, 2 * ETH_ALEN);
  74. lan9303_tag = (u16 *)(skb->data + 2 * ETH_ALEN);
  75. lan9303_tag[0] = htons(ETH_P_8021Q);
  76. lan9303_tag[1] = lan9303_xmit_use_arl(dp, skb->data) ?
  77. LAN9303_TAG_TX_USE_ALR :
  78. dp->index | LAN9303_TAG_TX_STP_OVERRIDE;
  79. lan9303_tag[1] = htons(lan9303_tag[1]);
  80. return skb;
  81. }
  82. static struct sk_buff *lan9303_rcv(struct sk_buff *skb, struct net_device *dev,
  83. struct packet_type *pt)
  84. {
  85. u16 *lan9303_tag;
  86. u16 lan9303_tag1;
  87. unsigned int source_port;
  88. if (unlikely(!pskb_may_pull(skb, LAN9303_TAG_LEN))) {
  89. dev_warn_ratelimited(&dev->dev,
  90. "Dropping packet, cannot pull\n");
  91. return NULL;
  92. }
  93. /* '->data' points into the middle of our special VLAN tag information:
  94. *
  95. * ~ MAC src | 0x81 | 0x00 | 0xyy | 0xzz | ether type
  96. * ^
  97. * ->data
  98. */
  99. lan9303_tag = (u16 *)(skb->data - 2);
  100. if (lan9303_tag[0] != htons(ETH_P_8021Q)) {
  101. dev_warn_ratelimited(&dev->dev, "Dropping packet due to invalid VLAN marker\n");
  102. return NULL;
  103. }
  104. lan9303_tag1 = ntohs(lan9303_tag[1]);
  105. source_port = lan9303_tag1 & 0x3;
  106. skb->dev = dsa_master_find_slave(dev, 0, source_port);
  107. if (!skb->dev) {
  108. dev_warn_ratelimited(&dev->dev, "Dropping packet due to invalid source port\n");
  109. return NULL;
  110. }
  111. /* remove the special VLAN tag between the MAC addresses
  112. * and the current ethertype field.
  113. */
  114. skb_pull_rcsum(skb, 2 + 2);
  115. memmove(skb->data - ETH_HLEN, skb->data - (ETH_HLEN + LAN9303_TAG_LEN),
  116. 2 * ETH_ALEN);
  117. skb->offload_fwd_mark = !(lan9303_tag1 & LAN9303_TAG_RX_TRAPPED_TO_CPU);
  118. return skb;
  119. }
  120. const struct dsa_device_ops lan9303_netdev_ops = {
  121. .xmit = lan9303_xmit,
  122. .rcv = lan9303_rcv,
  123. };