qca_7k_common.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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: 46/1500 */
  38. #define QCAFRM_MIN_MTU (ETH_ZLEN - ETH_HLEN)
  39. #define QCAFRM_MAX_MTU ETH_DATA_LEN
  40. /* Min/Max frame lengths */
  41. #define QCAFRM_MIN_LEN (QCAFRM_MIN_MTU + ETH_HLEN)
  42. #define QCAFRM_MAX_LEN (QCAFRM_MAX_MTU + 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. /* HW length is only available on SPI */
  51. QCAFRM_HW_LEN0 = 0x8000,
  52. QCAFRM_HW_LEN1 = QCAFRM_HW_LEN0 - 1,
  53. QCAFRM_HW_LEN2 = QCAFRM_HW_LEN1 - 1,
  54. QCAFRM_HW_LEN3 = QCAFRM_HW_LEN2 - 1,
  55. /* Waiting first 0xAA of header */
  56. QCAFRM_WAIT_AA1 = QCAFRM_HW_LEN3 - 1,
  57. /* Waiting second 0xAA of header */
  58. QCAFRM_WAIT_AA2 = QCAFRM_WAIT_AA1 - 1,
  59. /* Waiting third 0xAA of header */
  60. QCAFRM_WAIT_AA3 = QCAFRM_WAIT_AA2 - 1,
  61. /* Waiting fourth 0xAA of header */
  62. QCAFRM_WAIT_AA4 = QCAFRM_WAIT_AA3 - 1,
  63. /* Waiting Byte 0-1 of length (litte endian) */
  64. QCAFRM_WAIT_LEN_BYTE0 = QCAFRM_WAIT_AA4 - 1,
  65. QCAFRM_WAIT_LEN_BYTE1 = QCAFRM_WAIT_AA4 - 2,
  66. /* Reserved bytes */
  67. QCAFRM_WAIT_RSVD_BYTE1 = QCAFRM_WAIT_AA4 - 3,
  68. QCAFRM_WAIT_RSVD_BYTE2 = QCAFRM_WAIT_AA4 - 4,
  69. /* The frame length is used as the state until
  70. * the end of the Ethernet frame
  71. * Waiting for first 0x55 of footer
  72. */
  73. QCAFRM_WAIT_551 = 1,
  74. /* Waiting for second 0x55 of footer */
  75. QCAFRM_WAIT_552 = QCAFRM_WAIT_551 - 1
  76. };
  77. /* Structure to maintain the frame decoding during reception. */
  78. struct qcafrm_handle {
  79. /* Current decoding state */
  80. enum qcafrm_state state;
  81. /* Initial state depends on connection type */
  82. enum qcafrm_state init;
  83. /* Offset in buffer (borrowed for length too) */
  84. u16 offset;
  85. /* Frame length as kept by this module */
  86. u16 len;
  87. };
  88. u16 qcafrm_create_header(u8 *buf, u16 len);
  89. u16 qcafrm_create_footer(u8 *buf);
  90. static inline void qcafrm_fsm_init_spi(struct qcafrm_handle *handle)
  91. {
  92. handle->init = QCAFRM_HW_LEN0;
  93. handle->state = handle->init;
  94. }
  95. static inline void qcafrm_fsm_init_uart(struct qcafrm_handle *handle)
  96. {
  97. handle->init = QCAFRM_WAIT_AA1;
  98. handle->state = handle->init;
  99. }
  100. /* Gather received bytes and try to extract a full Ethernet frame
  101. * by following a simple state machine.
  102. *
  103. * Return: QCAFRM_GATHER No Ethernet frame fully received yet.
  104. * QCAFRM_NOHEAD Header expected but not found.
  105. * QCAFRM_INVLEN QCA7K frame length is invalid
  106. * QCAFRM_NOTAIL Footer expected but not found.
  107. * > 0 Number of byte in the fully received
  108. * Ethernet frame
  109. */
  110. s32 qcafrm_fsm_decode(struct qcafrm_handle *handle, u8 *buf, u16 buf_len, u8 recv_byte);
  111. #endif /* _QCA_FRAMING_H */