qca_framing.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2011, 2012, Atheros Communications Inc.
  3. * Copyright (c) 2014, I2SE GmbH
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software
  6. * for any purpose with or without fee is hereby granted, provided
  7. * that the above copyright notice and this permission notice appear
  8. * in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  11. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  12. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  13. * THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  14. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  15. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  16. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  17. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. /* Atheros Ethernet framing. Every Ethernet frame is surrounded by an atheros
  20. * frame while transmitted over a serial channel.
  21. */
  22. #ifndef _QCA_FRAMING_H
  23. #define _QCA_FRAMING_H
  24. #include <linux/if_ether.h>
  25. #include <linux/if_vlan.h>
  26. #include <linux/types.h>
  27. /* Frame is currently being received */
  28. #define QCAFRM_GATHER 0
  29. /* No header byte while expecting it */
  30. #define QCAFRM_NOHEAD (QCAFRM_ERR_BASE - 1)
  31. /* No tailer byte while expecting it */
  32. #define QCAFRM_NOTAIL (QCAFRM_ERR_BASE - 2)
  33. /* Frame length is invalid */
  34. #define QCAFRM_INVLEN (QCAFRM_ERR_BASE - 3)
  35. /* Frame length is invalid */
  36. #define QCAFRM_INVFRAME (QCAFRM_ERR_BASE - 4)
  37. /* Min/Max Ethernet MTU */
  38. #define QCAFRM_ETHMINMTU 46
  39. #define QCAFRM_ETHMAXMTU 1500
  40. /* Min/Max frame lengths */
  41. #define QCAFRM_ETHMINLEN (QCAFRM_ETHMINMTU + ETH_HLEN)
  42. #define QCAFRM_ETHMAXLEN (QCAFRM_ETHMAXMTU + VLAN_ETH_HLEN)
  43. /* QCA7K header len */
  44. #define QCAFRM_HEADER_LEN 8
  45. /* QCA7K footer len */
  46. #define QCAFRM_FOOTER_LEN 2
  47. /* QCA7K Framing. */
  48. #define QCAFRM_ERR_BASE -1000
  49. enum qcafrm_state {
  50. QCAFRM_HW_LEN0 = 0x8000,
  51. QCAFRM_HW_LEN1 = QCAFRM_HW_LEN0 - 1,
  52. QCAFRM_HW_LEN2 = QCAFRM_HW_LEN1 - 1,
  53. QCAFRM_HW_LEN3 = QCAFRM_HW_LEN2 - 1,
  54. /* Waiting first 0xAA of header */
  55. QCAFRM_WAIT_AA1 = QCAFRM_HW_LEN3 - 1,
  56. /* Waiting second 0xAA of header */
  57. QCAFRM_WAIT_AA2 = QCAFRM_WAIT_AA1 - 1,
  58. /* Waiting third 0xAA of header */
  59. QCAFRM_WAIT_AA3 = QCAFRM_WAIT_AA2 - 1,
  60. /* Waiting fourth 0xAA of header */
  61. QCAFRM_WAIT_AA4 = QCAFRM_WAIT_AA3 - 1,
  62. /* Waiting Byte 0-1 of length (litte endian) */
  63. QCAFRM_WAIT_LEN_BYTE0 = QCAFRM_WAIT_AA4 - 1,
  64. QCAFRM_WAIT_LEN_BYTE1 = QCAFRM_WAIT_AA4 - 2,
  65. /* Reserved bytes */
  66. QCAFRM_WAIT_RSVD_BYTE1 = QCAFRM_WAIT_AA4 - 3,
  67. QCAFRM_WAIT_RSVD_BYTE2 = QCAFRM_WAIT_AA4 - 4,
  68. /* The frame length is used as the state until
  69. * the end of the Ethernet frame
  70. * Waiting for first 0x55 of footer
  71. */
  72. QCAFRM_WAIT_551 = 1,
  73. /* Waiting for second 0x55 of footer */
  74. QCAFRM_WAIT_552 = QCAFRM_WAIT_551 - 1
  75. };
  76. /* Structure to maintain the frame decoding during reception. */
  77. struct qcafrm_handle {
  78. /* Current decoding state */
  79. enum qcafrm_state state;
  80. /* Offset in buffer (borrowed for length too) */
  81. s16 offset;
  82. /* Frame length as kept by this module */
  83. u16 len;
  84. };
  85. u16 qcafrm_create_header(u8 *buf, u16 len);
  86. u16 qcafrm_create_footer(u8 *buf);
  87. static inline void qcafrm_fsm_init(struct qcafrm_handle *handle)
  88. {
  89. handle->state = QCAFRM_HW_LEN0;
  90. }
  91. /* Gather received bytes and try to extract a full Ethernet frame
  92. * by following a simple state machine.
  93. *
  94. * Return: QCAFRM_GATHER No Ethernet frame fully received yet.
  95. * QCAFRM_NOHEAD Header expected but not found.
  96. * QCAFRM_INVLEN QCA7K frame length is invalid
  97. * QCAFRM_NOTAIL Footer expected but not found.
  98. * > 0 Number of byte in the fully received
  99. * Ethernet frame
  100. */
  101. s32 qcafrm_fsm_decode(struct qcafrm_handle *handle, u8 *buf, u16 buf_len, u8 recv_byte);
  102. #endif /* _QCA_FRAMING_H */