tag_ksz.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * net/dsa/tag_ksz.c - Microchip KSZ Switch tag format handling
  3. * Copyright (c) 2017 Microchip Technology
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #include <linux/etherdevice.h>
  11. #include <linux/list.h>
  12. #include <linux/slab.h>
  13. #include <net/dsa.h>
  14. #include "dsa_priv.h"
  15. /* For Ingress (Host -> KSZ), 2 bytes are added before FCS.
  16. * ---------------------------------------------------------------------------
  17. * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|tag1(1byte)|FCS(4bytes)
  18. * ---------------------------------------------------------------------------
  19. * tag0 : Prioritization (not used now)
  20. * tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
  21. *
  22. * For Egress (KSZ -> Host), 1 byte is added before FCS.
  23. * ---------------------------------------------------------------------------
  24. * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
  25. * ---------------------------------------------------------------------------
  26. * tag0 : zero-based value represents port
  27. * (eg, 0x00=port1, 0x02=port3, 0x06=port7)
  28. */
  29. #define KSZ_INGRESS_TAG_LEN 2
  30. #define KSZ_EGRESS_TAG_LEN 1
  31. static struct sk_buff *ksz_xmit(struct sk_buff *skb, struct net_device *dev)
  32. {
  33. struct dsa_port *dp = dsa_slave_to_port(dev);
  34. struct sk_buff *nskb;
  35. int padlen;
  36. u8 *tag;
  37. padlen = (skb->len >= ETH_ZLEN) ? 0 : ETH_ZLEN - skb->len;
  38. if (skb_tailroom(skb) >= padlen + KSZ_INGRESS_TAG_LEN) {
  39. /* Let dsa_slave_xmit() free skb */
  40. if (__skb_put_padto(skb, skb->len + padlen, false))
  41. return NULL;
  42. nskb = skb;
  43. } else {
  44. nskb = alloc_skb(NET_IP_ALIGN + skb->len +
  45. padlen + KSZ_INGRESS_TAG_LEN, GFP_ATOMIC);
  46. if (!nskb)
  47. return NULL;
  48. skb_reserve(nskb, NET_IP_ALIGN);
  49. skb_reset_mac_header(nskb);
  50. skb_set_network_header(nskb,
  51. skb_network_header(skb) - skb->head);
  52. skb_set_transport_header(nskb,
  53. skb_transport_header(skb) - skb->head);
  54. skb_copy_and_csum_dev(skb, skb_put(nskb, skb->len));
  55. /* Let skb_put_padto() free nskb, and let dsa_slave_xmit() free
  56. * skb
  57. */
  58. if (skb_put_padto(nskb, nskb->len + padlen))
  59. return NULL;
  60. consume_skb(skb);
  61. }
  62. tag = skb_put(nskb, KSZ_INGRESS_TAG_LEN);
  63. tag[0] = 0;
  64. tag[1] = 1 << dp->index; /* destination port */
  65. return nskb;
  66. }
  67. static struct sk_buff *ksz_rcv(struct sk_buff *skb, struct net_device *dev,
  68. struct packet_type *pt)
  69. {
  70. u8 *tag;
  71. int source_port;
  72. tag = skb_tail_pointer(skb) - KSZ_EGRESS_TAG_LEN;
  73. source_port = tag[0] & 7;
  74. skb->dev = dsa_master_find_slave(dev, 0, source_port);
  75. if (!skb->dev)
  76. return NULL;
  77. pskb_trim_rcsum(skb, skb->len - KSZ_EGRESS_TAG_LEN);
  78. return skb;
  79. }
  80. const struct dsa_device_ops ksz_netdev_ops = {
  81. .xmit = ksz_xmit,
  82. .rcv = ksz_rcv,
  83. };