tag_brcm.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Broadcom tag support
  3. *
  4. * Copyright (C) 2014 Broadcom Corporation
  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/etherdevice.h>
  12. #include <linux/list.h>
  13. #include <linux/slab.h>
  14. #include "dsa_priv.h"
  15. /* This tag length is 4 bytes, older ones were 6 bytes, we do not
  16. * handle them
  17. */
  18. #define BRCM_TAG_LEN 4
  19. /* Tag is constructed and desconstructed using byte by byte access
  20. * because the tag is placed after the MAC Source Address, which does
  21. * not make it 4-bytes aligned, so this might cause unaligned accesses
  22. * on most systems where this is used.
  23. */
  24. /* Ingress and egress opcodes */
  25. #define BRCM_OPCODE_SHIFT 5
  26. #define BRCM_OPCODE_MASK 0x7
  27. /* Ingress fields */
  28. /* 1st byte in the tag */
  29. #define BRCM_IG_TC_SHIFT 2
  30. #define BRCM_IG_TC_MASK 0x7
  31. /* 2nd byte in the tag */
  32. #define BRCM_IG_TE_MASK 0x3
  33. #define BRCM_IG_TS_SHIFT 7
  34. /* 3rd byte in the tag */
  35. #define BRCM_IG_DSTMAP2_MASK 1
  36. #define BRCM_IG_DSTMAP1_MASK 0xff
  37. /* Egress fields */
  38. /* 2nd byte in the tag */
  39. #define BRCM_EG_CID_MASK 0xff
  40. /* 3rd byte in the tag */
  41. #define BRCM_EG_RC_MASK 0xff
  42. #define BRCM_EG_RC_RSVD (3 << 6)
  43. #define BRCM_EG_RC_EXCEPTION (1 << 5)
  44. #define BRCM_EG_RC_PROT_SNOOP (1 << 4)
  45. #define BRCM_EG_RC_PROT_TERM (1 << 3)
  46. #define BRCM_EG_RC_SWITCH (1 << 2)
  47. #define BRCM_EG_RC_MAC_LEARN (1 << 1)
  48. #define BRCM_EG_RC_MIRROR (1 << 0)
  49. #define BRCM_EG_TC_SHIFT 5
  50. #define BRCM_EG_TC_MASK 0x7
  51. #define BRCM_EG_PID_MASK 0x1f
  52. static struct sk_buff *brcm_tag_xmit_ll(struct sk_buff *skb,
  53. struct net_device *dev,
  54. unsigned int offset)
  55. {
  56. struct dsa_port *dp = dsa_slave_to_port(dev);
  57. u16 queue = skb_get_queue_mapping(skb);
  58. u8 *brcm_tag;
  59. if (skb_cow_head(skb, BRCM_TAG_LEN) < 0)
  60. return NULL;
  61. /* The Ethernet switch we are interfaced with needs packets to be at
  62. * least 64 bytes (including FCS) otherwise they will be discarded when
  63. * they enter the switch port logic. When Broadcom tags are enabled, we
  64. * need to make sure that packets are at least 68 bytes
  65. * (including FCS and tag) because the length verification is done after
  66. * the Broadcom tag is stripped off the ingress packet.
  67. *
  68. * Let dsa_slave_xmit() free the SKB
  69. */
  70. if (__skb_put_padto(skb, ETH_ZLEN + BRCM_TAG_LEN, false))
  71. return NULL;
  72. skb_push(skb, BRCM_TAG_LEN);
  73. if (offset)
  74. memmove(skb->data, skb->data + BRCM_TAG_LEN, offset);
  75. brcm_tag = skb->data + offset;
  76. /* Set the ingress opcode, traffic class, tag enforcment is
  77. * deprecated
  78. */
  79. brcm_tag[0] = (1 << BRCM_OPCODE_SHIFT) |
  80. ((queue & BRCM_IG_TC_MASK) << BRCM_IG_TC_SHIFT);
  81. brcm_tag[1] = 0;
  82. brcm_tag[2] = 0;
  83. if (dp->index == 8)
  84. brcm_tag[2] = BRCM_IG_DSTMAP2_MASK;
  85. brcm_tag[3] = (1 << dp->index) & BRCM_IG_DSTMAP1_MASK;
  86. /* Now tell the master network device about the desired output queue
  87. * as well
  88. */
  89. skb_set_queue_mapping(skb, BRCM_TAG_SET_PORT_QUEUE(dp->index, queue));
  90. return skb;
  91. }
  92. static struct sk_buff *brcm_tag_rcv_ll(struct sk_buff *skb,
  93. struct net_device *dev,
  94. struct packet_type *pt,
  95. unsigned int offset)
  96. {
  97. int source_port;
  98. u8 *brcm_tag;
  99. if (unlikely(!pskb_may_pull(skb, BRCM_TAG_LEN)))
  100. return NULL;
  101. brcm_tag = skb->data - offset;
  102. /* The opcode should never be different than 0b000 */
  103. if (unlikely((brcm_tag[0] >> BRCM_OPCODE_SHIFT) & BRCM_OPCODE_MASK))
  104. return NULL;
  105. /* We should never see a reserved reason code without knowing how to
  106. * handle it
  107. */
  108. if (unlikely(brcm_tag[2] & BRCM_EG_RC_RSVD))
  109. return NULL;
  110. /* Locate which port this is coming from */
  111. source_port = brcm_tag[3] & BRCM_EG_PID_MASK;
  112. skb->dev = dsa_master_find_slave(dev, 0, source_port);
  113. if (!skb->dev)
  114. return NULL;
  115. /* Remove Broadcom tag and update checksum */
  116. skb_pull_rcsum(skb, BRCM_TAG_LEN);
  117. skb->offload_fwd_mark = 1;
  118. return skb;
  119. }
  120. #ifdef CONFIG_NET_DSA_TAG_BRCM
  121. static struct sk_buff *brcm_tag_xmit(struct sk_buff *skb,
  122. struct net_device *dev)
  123. {
  124. /* Build the tag after the MAC Source Address */
  125. return brcm_tag_xmit_ll(skb, dev, 2 * ETH_ALEN);
  126. }
  127. static struct sk_buff *brcm_tag_rcv(struct sk_buff *skb, struct net_device *dev,
  128. struct packet_type *pt)
  129. {
  130. struct sk_buff *nskb;
  131. /* skb->data points to the EtherType, the tag is right before it */
  132. nskb = brcm_tag_rcv_ll(skb, dev, pt, 2);
  133. if (!nskb)
  134. return nskb;
  135. /* Move the Ethernet DA and SA */
  136. memmove(nskb->data - ETH_HLEN,
  137. nskb->data - ETH_HLEN - BRCM_TAG_LEN,
  138. 2 * ETH_ALEN);
  139. return nskb;
  140. }
  141. const struct dsa_device_ops brcm_netdev_ops = {
  142. .xmit = brcm_tag_xmit,
  143. .rcv = brcm_tag_rcv,
  144. };
  145. #endif
  146. #ifdef CONFIG_NET_DSA_TAG_BRCM_PREPEND
  147. static struct sk_buff *brcm_tag_xmit_prepend(struct sk_buff *skb,
  148. struct net_device *dev)
  149. {
  150. /* tag is prepended to the packet */
  151. return brcm_tag_xmit_ll(skb, dev, 0);
  152. }
  153. static struct sk_buff *brcm_tag_rcv_prepend(struct sk_buff *skb,
  154. struct net_device *dev,
  155. struct packet_type *pt)
  156. {
  157. /* tag is prepended to the packet */
  158. return brcm_tag_rcv_ll(skb, dev, pt, ETH_HLEN);
  159. }
  160. const struct dsa_device_ops brcm_prepend_netdev_ops = {
  161. .xmit = brcm_tag_xmit_prepend,
  162. .rcv = brcm_tag_rcv_prepend,
  163. };
  164. #endif