tag_qca.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/etherdevice.h>
  6. #include "dsa_priv.h"
  7. #define QCA_HDR_LEN 2
  8. #define QCA_HDR_VERSION 0x2
  9. #define QCA_HDR_RECV_VERSION_MASK GENMASK(15, 14)
  10. #define QCA_HDR_RECV_VERSION_S 14
  11. #define QCA_HDR_RECV_PRIORITY_MASK GENMASK(13, 11)
  12. #define QCA_HDR_RECV_PRIORITY_S 11
  13. #define QCA_HDR_RECV_TYPE_MASK GENMASK(10, 6)
  14. #define QCA_HDR_RECV_TYPE_S 6
  15. #define QCA_HDR_RECV_FRAME_IS_TAGGED BIT(3)
  16. #define QCA_HDR_RECV_SOURCE_PORT_MASK GENMASK(2, 0)
  17. #define QCA_HDR_XMIT_VERSION_MASK GENMASK(15, 14)
  18. #define QCA_HDR_XMIT_VERSION_S 14
  19. #define QCA_HDR_XMIT_PRIORITY_MASK GENMASK(13, 11)
  20. #define QCA_HDR_XMIT_PRIORITY_S 11
  21. #define QCA_HDR_XMIT_CONTROL_MASK GENMASK(10, 8)
  22. #define QCA_HDR_XMIT_CONTROL_S 8
  23. #define QCA_HDR_XMIT_FROM_CPU BIT(7)
  24. #define QCA_HDR_XMIT_DP_BIT_MASK GENMASK(6, 0)
  25. static struct sk_buff *qca_tag_xmit(struct sk_buff *skb, struct net_device *dev)
  26. {
  27. struct dsa_port *dp = dsa_slave_to_port(dev);
  28. __be16 *phdr;
  29. u16 hdr;
  30. skb_push(skb, QCA_HDR_LEN);
  31. memmove(skb->data, skb->data + QCA_HDR_LEN, 2 * ETH_ALEN);
  32. phdr = (__be16 *)(skb->data + 2 * ETH_ALEN);
  33. /* Set the version field, and set destination port information */
  34. hdr = QCA_HDR_VERSION << QCA_HDR_XMIT_VERSION_S |
  35. QCA_HDR_XMIT_FROM_CPU | BIT(dp->index);
  36. *phdr = htons(hdr);
  37. return skb;
  38. }
  39. static struct sk_buff *qca_tag_rcv(struct sk_buff *skb, struct net_device *dev,
  40. struct packet_type *pt)
  41. {
  42. u8 ver;
  43. u16 hdr;
  44. int port;
  45. __be16 *phdr;
  46. if (unlikely(!pskb_may_pull(skb, QCA_HDR_LEN)))
  47. return NULL;
  48. /* The QCA header is added by the switch between src addr and Ethertype
  49. * At this point, skb->data points to ethertype so header should be
  50. * right before
  51. */
  52. phdr = (__be16 *)(skb->data - 2);
  53. hdr = ntohs(*phdr);
  54. /* Make sure the version is correct */
  55. ver = (hdr & QCA_HDR_RECV_VERSION_MASK) >> QCA_HDR_RECV_VERSION_S;
  56. if (unlikely(ver != QCA_HDR_VERSION))
  57. return NULL;
  58. /* Remove QCA tag and recalculate checksum */
  59. skb_pull_rcsum(skb, QCA_HDR_LEN);
  60. memmove(skb->data - ETH_HLEN, skb->data - ETH_HLEN - QCA_HDR_LEN,
  61. ETH_HLEN - QCA_HDR_LEN);
  62. /* Get source port information */
  63. port = (hdr & QCA_HDR_RECV_SOURCE_PORT_MASK);
  64. skb->dev = dsa_master_find_slave(dev, 0, port);
  65. if (!skb->dev)
  66. return NULL;
  67. return skb;
  68. }
  69. static const struct dsa_device_ops qca_netdev_ops = {
  70. .name = "qca",
  71. .proto = DSA_TAG_PROTO_QCA,
  72. .xmit = qca_tag_xmit,
  73. .rcv = qca_tag_rcv,
  74. .overhead = QCA_HDR_LEN,
  75. };
  76. MODULE_LICENSE("GPL");
  77. MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_QCA);
  78. module_dsa_tag_driver(qca_netdev_ops);