tag_rtl4_a.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Handler for Realtek 4 byte DSA switch tags
  4. * Currently only supports protocol "A" found in RTL8366RB
  5. * Copyright (c) 2020 Linus Walleij <linus.walleij@linaro.org>
  6. *
  7. * This "proprietary tag" header looks like so:
  8. *
  9. * -------------------------------------------------
  10. * | MAC DA | MAC SA | 0x8899 | 2 bytes tag | Type |
  11. * -------------------------------------------------
  12. *
  13. * The 2 bytes tag form a 16 bit big endian word. The exact
  14. * meaning has been guessed from packet dumps from ingress
  15. * frames.
  16. */
  17. #include <linux/etherdevice.h>
  18. #include <linux/bits.h>
  19. #include "dsa_priv.h"
  20. #define RTL4_A_HDR_LEN 4
  21. #define RTL4_A_ETHERTYPE 0x8899
  22. #define RTL4_A_PROTOCOL_SHIFT 12
  23. /*
  24. * 0x1 = Realtek Remote Control protocol (RRCP)
  25. * 0x2/0x3 seems to be used for loopback testing
  26. * 0x9 = RTL8306 DSA protocol
  27. * 0xa = RTL8366RB DSA protocol
  28. */
  29. #define RTL4_A_PROTOCOL_RTL8366RB 0xa
  30. static struct sk_buff *rtl4a_tag_xmit(struct sk_buff *skb,
  31. struct net_device *dev)
  32. {
  33. struct dsa_port *dp = dsa_slave_to_port(dev);
  34. __be16 *p;
  35. u8 *tag;
  36. u16 out;
  37. /* Pad out to at least 60 bytes */
  38. if (unlikely(__skb_put_padto(skb, ETH_ZLEN, false)))
  39. return NULL;
  40. netdev_dbg(dev, "add realtek tag to package to port %d\n",
  41. dp->index);
  42. skb_push(skb, RTL4_A_HDR_LEN);
  43. memmove(skb->data, skb->data + RTL4_A_HDR_LEN, 2 * ETH_ALEN);
  44. tag = skb->data + 2 * ETH_ALEN;
  45. /* Set Ethertype */
  46. p = (__be16 *)tag;
  47. *p = htons(RTL4_A_ETHERTYPE);
  48. out = (RTL4_A_PROTOCOL_RTL8366RB << RTL4_A_PROTOCOL_SHIFT) | (2 << 8);
  49. /* The lower bits indicate the port number */
  50. out |= BIT(dp->index);
  51. p = (__be16 *)(tag + 2);
  52. *p = htons(out);
  53. return skb;
  54. }
  55. static struct sk_buff *rtl4a_tag_rcv(struct sk_buff *skb,
  56. struct net_device *dev,
  57. struct packet_type *pt)
  58. {
  59. u16 protport;
  60. __be16 *p;
  61. u16 etype;
  62. u8 *tag;
  63. u8 prot;
  64. u8 port;
  65. if (unlikely(!pskb_may_pull(skb, RTL4_A_HDR_LEN)))
  66. return NULL;
  67. /* The RTL4 header has its own custom Ethertype 0x8899 and that
  68. * starts right at the beginning of the packet, after the src
  69. * ethernet addr. Apparantly skb->data always points 2 bytes in,
  70. * behind the Ethertype.
  71. */
  72. tag = skb->data - 2;
  73. p = (__be16 *)tag;
  74. etype = ntohs(*p);
  75. if (etype != RTL4_A_ETHERTYPE) {
  76. /* Not custom, just pass through */
  77. netdev_dbg(dev, "non-realtek ethertype 0x%04x\n", etype);
  78. return skb;
  79. }
  80. p = (__be16 *)(tag + 2);
  81. protport = ntohs(*p);
  82. /* The 4 upper bits are the protocol */
  83. prot = (protport >> RTL4_A_PROTOCOL_SHIFT) & 0x0f;
  84. if (prot != RTL4_A_PROTOCOL_RTL8366RB) {
  85. netdev_err(dev, "unknown realtek protocol 0x%01x\n", prot);
  86. return NULL;
  87. }
  88. port = protport & 0xff;
  89. skb->dev = dsa_master_find_slave(dev, 0, port);
  90. if (!skb->dev) {
  91. netdev_dbg(dev, "could not find slave for port %d\n", port);
  92. return NULL;
  93. }
  94. /* Remove RTL4 tag and recalculate checksum */
  95. skb_pull_rcsum(skb, RTL4_A_HDR_LEN);
  96. /* Move ethernet DA and SA in front of the data */
  97. memmove(skb->data - ETH_HLEN,
  98. skb->data - ETH_HLEN - RTL4_A_HDR_LEN,
  99. 2 * ETH_ALEN);
  100. skb->offload_fwd_mark = 1;
  101. return skb;
  102. }
  103. static const struct dsa_device_ops rtl4a_netdev_ops = {
  104. .name = "rtl4a",
  105. .proto = DSA_TAG_PROTO_RTL4_A,
  106. .xmit = rtl4a_tag_xmit,
  107. .rcv = rtl4a_tag_rcv,
  108. .overhead = RTL4_A_HDR_LEN,
  109. };
  110. module_dsa_tag_driver(rtl4a_netdev_ops);
  111. MODULE_LICENSE("GPL");
  112. MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_RTL4_A);