tag_brcm.c 4.0 KB

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