tag_qca.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/etherdevice.h>
  14. #include "dsa_priv.h"
  15. #define QCA_HDR_LEN 2
  16. #define QCA_HDR_VERSION 0x2
  17. #define QCA_HDR_RECV_VERSION_MASK GENMASK(15, 14)
  18. #define QCA_HDR_RECV_VERSION_S 14
  19. #define QCA_HDR_RECV_PRIORITY_MASK GENMASK(13, 11)
  20. #define QCA_HDR_RECV_PRIORITY_S 11
  21. #define QCA_HDR_RECV_TYPE_MASK GENMASK(10, 6)
  22. #define QCA_HDR_RECV_TYPE_S 6
  23. #define QCA_HDR_RECV_FRAME_IS_TAGGED BIT(3)
  24. #define QCA_HDR_RECV_SOURCE_PORT_MASK GENMASK(2, 0)
  25. #define QCA_HDR_XMIT_VERSION_MASK GENMASK(15, 14)
  26. #define QCA_HDR_XMIT_VERSION_S 14
  27. #define QCA_HDR_XMIT_PRIORITY_MASK GENMASK(13, 11)
  28. #define QCA_HDR_XMIT_PRIORITY_S 11
  29. #define QCA_HDR_XMIT_CONTROL_MASK GENMASK(10, 8)
  30. #define QCA_HDR_XMIT_CONTROL_S 8
  31. #define QCA_HDR_XMIT_FROM_CPU BIT(7)
  32. #define QCA_HDR_XMIT_DP_BIT_MASK GENMASK(6, 0)
  33. static struct sk_buff *qca_tag_xmit(struct sk_buff *skb, struct net_device *dev)
  34. {
  35. struct dsa_slave_priv *p = netdev_priv(dev);
  36. u16 *phdr, hdr;
  37. dev->stats.tx_packets++;
  38. dev->stats.tx_bytes += skb->len;
  39. if (skb_cow_head(skb, 0) < 0)
  40. goto out_free;
  41. skb_push(skb, QCA_HDR_LEN);
  42. memmove(skb->data, skb->data + QCA_HDR_LEN, 2 * ETH_ALEN);
  43. phdr = (u16 *)(skb->data + 2 * ETH_ALEN);
  44. /* Set the version field, and set destination port information */
  45. hdr = QCA_HDR_VERSION << QCA_HDR_XMIT_VERSION_S |
  46. QCA_HDR_XMIT_FROM_CPU |
  47. BIT(p->port);
  48. *phdr = htons(hdr);
  49. return skb;
  50. out_free:
  51. kfree_skb(skb);
  52. return NULL;
  53. }
  54. static int qca_tag_rcv(struct sk_buff *skb, struct net_device *dev,
  55. struct packet_type *pt, struct net_device *orig_dev)
  56. {
  57. struct dsa_switch_tree *dst = dev->dsa_ptr;
  58. struct dsa_switch *ds;
  59. u8 ver;
  60. int port;
  61. __be16 *phdr, hdr;
  62. if (unlikely(!dst))
  63. goto out_drop;
  64. skb = skb_unshare(skb, GFP_ATOMIC);
  65. if (!skb)
  66. goto out;
  67. if (unlikely(!pskb_may_pull(skb, QCA_HDR_LEN)))
  68. goto out_drop;
  69. /* The QCA header is added by the switch between src addr and Ethertype
  70. * At this point, skb->data points to ethertype so header should be
  71. * right before
  72. */
  73. phdr = (__be16 *)(skb->data - 2);
  74. hdr = ntohs(*phdr);
  75. /* Make sure the version is correct */
  76. ver = (hdr & QCA_HDR_RECV_VERSION_MASK) >> QCA_HDR_RECV_VERSION_S;
  77. if (unlikely(ver != QCA_HDR_VERSION))
  78. goto out_drop;
  79. /* Remove QCA tag and recalculate checksum */
  80. skb_pull_rcsum(skb, QCA_HDR_LEN);
  81. memmove(skb->data - ETH_HLEN, skb->data - ETH_HLEN - QCA_HDR_LEN,
  82. ETH_HLEN - QCA_HDR_LEN);
  83. /* This protocol doesn't support cascading multiple switches so it's
  84. * safe to assume the switch is first in the tree
  85. */
  86. ds = dst->ds[0];
  87. if (!ds)
  88. goto out_drop;
  89. /* Get source port information */
  90. port = (hdr & QCA_HDR_RECV_SOURCE_PORT_MASK);
  91. if (!ds->ports[port].netdev)
  92. goto out_drop;
  93. /* Update skb & forward the frame accordingly */
  94. skb_push(skb, ETH_HLEN);
  95. skb->pkt_type = PACKET_HOST;
  96. skb->dev = ds->ports[port].netdev;
  97. skb->protocol = eth_type_trans(skb, skb->dev);
  98. skb->dev->stats.rx_packets++;
  99. skb->dev->stats.rx_bytes += skb->len;
  100. netif_receive_skb(skb);
  101. return 0;
  102. out_drop:
  103. kfree_skb(skb);
  104. out:
  105. return 0;
  106. }
  107. const struct dsa_device_ops qca_netdev_ops = {
  108. .xmit = qca_tag_xmit,
  109. .rcv = qca_tag_rcv,
  110. };