tag_brcm.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 netdev_tx_t brcm_tag_xmit(struct sk_buff *skb, struct net_device *dev)
  53. {
  54. struct dsa_slave_priv *p = netdev_priv(dev);
  55. u8 *brcm_tag;
  56. dev->stats.tx_packets++;
  57. dev->stats.tx_bytes += skb->len;
  58. if (skb_cow_head(skb, BRCM_TAG_LEN) < 0)
  59. goto out_free;
  60. skb_push(skb, BRCM_TAG_LEN);
  61. memmove(skb->data, skb->data + BRCM_TAG_LEN, 2 * ETH_ALEN);
  62. /* Build the tag after the MAC Source Address */
  63. brcm_tag = skb->data + 2 * ETH_ALEN;
  64. /* Set the ingress opcode, traffic class, tag enforcment is
  65. * deprecated
  66. */
  67. brcm_tag[0] = (1 << BRCM_OPCODE_SHIFT) |
  68. ((skb->priority << BRCM_IG_TC_SHIFT) & BRCM_IG_TC_MASK);
  69. brcm_tag[1] = 0;
  70. brcm_tag[2] = 0;
  71. if (p->port == 8)
  72. brcm_tag[2] = BRCM_IG_DSTMAP2_MASK;
  73. brcm_tag[3] = (1 << p->port) & BRCM_IG_DSTMAP1_MASK;
  74. /* Queue the SKB for transmission on the parent interface, but
  75. * do not modify its EtherType
  76. */
  77. skb->dev = p->parent->dst->master_netdev;
  78. dev_queue_xmit(skb);
  79. return NETDEV_TX_OK;
  80. out_free:
  81. kfree_skb(skb);
  82. return NETDEV_TX_OK;
  83. }
  84. static int brcm_tag_rcv(struct sk_buff *skb, struct net_device *dev,
  85. struct packet_type *pt, struct net_device *orig_dev)
  86. {
  87. struct dsa_switch_tree *dst = dev->dsa_ptr;
  88. struct dsa_switch *ds;
  89. int source_port;
  90. u8 *brcm_tag;
  91. if (unlikely(dst == NULL))
  92. goto out_drop;
  93. ds = dst->ds[0];
  94. skb = skb_unshare(skb, GFP_ATOMIC);
  95. if (skb == NULL)
  96. goto out;
  97. if (unlikely(!pskb_may_pull(skb, BRCM_TAG_LEN)))
  98. goto out_drop;
  99. /* skb->data points to the EtherType, the tag is right before it */
  100. brcm_tag = skb->data - 2;
  101. /* The opcode should never be different than 0b000 */
  102. if (unlikely((brcm_tag[0] >> BRCM_OPCODE_SHIFT) & BRCM_OPCODE_MASK))
  103. goto out_drop;
  104. /* We should never see a reserved reason code without knowing how to
  105. * handle it
  106. */
  107. WARN_ON(brcm_tag[2] & BRCM_EG_RC_RSVD);
  108. /* Locate which port this is coming from */
  109. source_port = brcm_tag[3] & BRCM_EG_PID_MASK;
  110. /* Validate port against switch setup, either the port is totally */
  111. if (source_port >= DSA_MAX_PORTS || ds->ports[source_port] == NULL)
  112. goto out_drop;
  113. /* Remove Broadcom tag and update checksum */
  114. skb_pull_rcsum(skb, BRCM_TAG_LEN);
  115. /* Move the Ethernet DA and SA */
  116. memmove(skb->data - ETH_HLEN,
  117. skb->data - ETH_HLEN - BRCM_TAG_LEN,
  118. 2 * ETH_ALEN);
  119. skb_push(skb, ETH_HLEN);
  120. skb->pkt_type = PACKET_HOST;
  121. skb->dev = ds->ports[source_port];
  122. skb->protocol = eth_type_trans(skb, skb->dev);
  123. skb->dev->stats.rx_packets++;
  124. skb->dev->stats.rx_bytes += skb->len;
  125. netif_receive_skb(skb);
  126. return 0;
  127. out_drop:
  128. kfree_skb(skb);
  129. out:
  130. return 0;
  131. }
  132. const struct dsa_device_ops brcm_netdev_ops = {
  133. .xmit = brcm_tag_xmit,
  134. .rcv = brcm_tag_rcv,
  135. };