tag_mtk.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Mediatek DSA Tag support
  3. * Copyright (C) 2017 Landen Chao <landen.chao@mediatek.com>
  4. * Sean Wang <sean.wang@mediatek.com>
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/etherdevice.h>
  15. #include <linux/if_vlan.h>
  16. #include "dsa_priv.h"
  17. #define MTK_HDR_LEN 4
  18. #define MTK_HDR_XMIT_UNTAGGED 0
  19. #define MTK_HDR_XMIT_TAGGED_TPID_8100 1
  20. #define MTK_HDR_RECV_SOURCE_PORT_MASK GENMASK(2, 0)
  21. #define MTK_HDR_XMIT_DP_BIT_MASK GENMASK(5, 0)
  22. static struct sk_buff *mtk_tag_xmit(struct sk_buff *skb,
  23. struct net_device *dev)
  24. {
  25. struct dsa_port *dp = dsa_slave_to_port(dev);
  26. u8 *mtk_tag;
  27. bool is_vlan_skb = true;
  28. /* Build the special tag after the MAC Source Address. If VLAN header
  29. * is present, it's required that VLAN header and special tag is
  30. * being combined. Only in this way we can allow the switch can parse
  31. * the both special and VLAN tag at the same time and then look up VLAN
  32. * table with VID.
  33. */
  34. if (!skb_vlan_tagged(skb)) {
  35. if (skb_cow_head(skb, MTK_HDR_LEN) < 0)
  36. return NULL;
  37. skb_push(skb, MTK_HDR_LEN);
  38. memmove(skb->data, skb->data + MTK_HDR_LEN, 2 * ETH_ALEN);
  39. is_vlan_skb = false;
  40. }
  41. mtk_tag = skb->data + 2 * ETH_ALEN;
  42. /* Mark tag attribute on special tag insertion to notify hardware
  43. * whether that's a combined special tag with 802.1Q header.
  44. */
  45. mtk_tag[0] = is_vlan_skb ? MTK_HDR_XMIT_TAGGED_TPID_8100 :
  46. MTK_HDR_XMIT_UNTAGGED;
  47. mtk_tag[1] = (1 << dp->index) & MTK_HDR_XMIT_DP_BIT_MASK;
  48. /* Tag control information is kept for 802.1Q */
  49. if (!is_vlan_skb) {
  50. mtk_tag[2] = 0;
  51. mtk_tag[3] = 0;
  52. }
  53. return skb;
  54. }
  55. static struct sk_buff *mtk_tag_rcv(struct sk_buff *skb, struct net_device *dev,
  56. struct packet_type *pt)
  57. {
  58. int port;
  59. __be16 *phdr, hdr;
  60. if (unlikely(!pskb_may_pull(skb, MTK_HDR_LEN)))
  61. return NULL;
  62. /* The MTK header is added by the switch between src addr
  63. * and ethertype at this point, skb->data points to 2 bytes
  64. * after src addr so header should be 2 bytes right before.
  65. */
  66. phdr = (__be16 *)(skb->data - 2);
  67. hdr = ntohs(*phdr);
  68. /* Remove MTK tag and recalculate checksum. */
  69. skb_pull_rcsum(skb, MTK_HDR_LEN);
  70. memmove(skb->data - ETH_HLEN,
  71. skb->data - ETH_HLEN - MTK_HDR_LEN,
  72. 2 * ETH_ALEN);
  73. /* Get source port information */
  74. port = (hdr & MTK_HDR_RECV_SOURCE_PORT_MASK);
  75. skb->dev = dsa_master_find_slave(dev, 0, port);
  76. if (!skb->dev)
  77. return NULL;
  78. return skb;
  79. }
  80. static int mtk_tag_flow_dissect(const struct sk_buff *skb, __be16 *proto,
  81. int *offset)
  82. {
  83. *offset = 4;
  84. *proto = ((__be16 *)skb->data)[1];
  85. return 0;
  86. }
  87. const struct dsa_device_ops mtk_netdev_ops = {
  88. .xmit = mtk_tag_xmit,
  89. .rcv = mtk_tag_rcv,
  90. .flow_dissect = mtk_tag_flow_dissect,
  91. };