tag_gswip.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel / Lantiq GSWIP V2.0 PMAC tag support
  4. *
  5. * Copyright (C) 2017 - 2018 Hauke Mehrtens <hauke@hauke-m.de>
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/etherdevice.h>
  9. #include <linux/skbuff.h>
  10. #include <net/dsa.h>
  11. #include "dsa_priv.h"
  12. #define GSWIP_TX_HEADER_LEN 4
  13. /* special tag in TX path header */
  14. /* Byte 0 */
  15. #define GSWIP_TX_SLPID_SHIFT 0 /* source port ID */
  16. #define GSWIP_TX_SLPID_CPU 2
  17. #define GSWIP_TX_SLPID_APP1 3
  18. #define GSWIP_TX_SLPID_APP2 4
  19. #define GSWIP_TX_SLPID_APP3 5
  20. #define GSWIP_TX_SLPID_APP4 6
  21. #define GSWIP_TX_SLPID_APP5 7
  22. /* Byte 1 */
  23. #define GSWIP_TX_CRCGEN_DIS BIT(7)
  24. #define GSWIP_TX_DPID_SHIFT 0 /* destination group ID */
  25. #define GSWIP_TX_DPID_ELAN 0
  26. #define GSWIP_TX_DPID_EWAN 1
  27. #define GSWIP_TX_DPID_CPU 2
  28. #define GSWIP_TX_DPID_APP1 3
  29. #define GSWIP_TX_DPID_APP2 4
  30. #define GSWIP_TX_DPID_APP3 5
  31. #define GSWIP_TX_DPID_APP4 6
  32. #define GSWIP_TX_DPID_APP5 7
  33. /* Byte 2 */
  34. #define GSWIP_TX_PORT_MAP_EN BIT(7)
  35. #define GSWIP_TX_PORT_MAP_SEL BIT(6)
  36. #define GSWIP_TX_LRN_DIS BIT(5)
  37. #define GSWIP_TX_CLASS_EN BIT(4)
  38. #define GSWIP_TX_CLASS_SHIFT 0
  39. #define GSWIP_TX_CLASS_MASK GENMASK(3, 0)
  40. /* Byte 3 */
  41. #define GSWIP_TX_DPID_EN BIT(0)
  42. #define GSWIP_TX_PORT_MAP_SHIFT 1
  43. #define GSWIP_TX_PORT_MAP_MASK GENMASK(6, 1)
  44. #define GSWIP_RX_HEADER_LEN 8
  45. /* special tag in RX path header */
  46. /* Byte 7 */
  47. #define GSWIP_RX_SPPID_SHIFT 4
  48. #define GSWIP_RX_SPPID_MASK GENMASK(6, 4)
  49. static struct sk_buff *gswip_tag_xmit(struct sk_buff *skb,
  50. struct net_device *dev)
  51. {
  52. struct dsa_port *dp = dsa_slave_to_port(dev);
  53. u8 *gswip_tag;
  54. skb_push(skb, GSWIP_TX_HEADER_LEN);
  55. gswip_tag = skb->data;
  56. gswip_tag[0] = GSWIP_TX_SLPID_CPU;
  57. gswip_tag[1] = GSWIP_TX_DPID_ELAN;
  58. gswip_tag[2] = GSWIP_TX_PORT_MAP_EN | GSWIP_TX_PORT_MAP_SEL;
  59. gswip_tag[3] = BIT(dp->index + GSWIP_TX_PORT_MAP_SHIFT) & GSWIP_TX_PORT_MAP_MASK;
  60. gswip_tag[3] |= GSWIP_TX_DPID_EN;
  61. return skb;
  62. }
  63. static struct sk_buff *gswip_tag_rcv(struct sk_buff *skb,
  64. struct net_device *dev,
  65. struct packet_type *pt)
  66. {
  67. int port;
  68. u8 *gswip_tag;
  69. if (unlikely(!pskb_may_pull(skb, GSWIP_RX_HEADER_LEN)))
  70. return NULL;
  71. gswip_tag = skb->data - ETH_HLEN;
  72. /* Get source port information */
  73. port = (gswip_tag[7] & GSWIP_RX_SPPID_MASK) >> GSWIP_RX_SPPID_SHIFT;
  74. skb->dev = dsa_master_find_slave(dev, 0, port);
  75. if (!skb->dev)
  76. return NULL;
  77. /* remove GSWIP tag */
  78. skb_pull_rcsum(skb, GSWIP_RX_HEADER_LEN);
  79. return skb;
  80. }
  81. static const struct dsa_device_ops gswip_netdev_ops = {
  82. .name = "gswip",
  83. .proto = DSA_TAG_PROTO_GSWIP,
  84. .xmit = gswip_tag_xmit,
  85. .rcv = gswip_tag_rcv,
  86. .overhead = GSWIP_RX_HEADER_LEN,
  87. };
  88. MODULE_LICENSE("GPL");
  89. MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_GSWIP);
  90. module_dsa_tag_driver(gswip_netdev_ops);