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 
20 /*   Atheros Ethernet framing. Every Ethernet frame is surrounded by an atheros
21  *   frame while transmitted over a serial channel.
22  */
23 
24 #ifndef _QCA_FRAMING_H
25 #define _QCA_FRAMING_H
26 
27 #include <linux/if_ether.h>
28 #include <linux/if_vlan.h>
29 #include <linux/types.h>
30 
31 /* Frame is currently being received */
32 #define QCAFRM_GATHER 0
33 
34 /*  No header byte while expecting it */
35 #define QCAFRM_NOHEAD (QCAFRM_ERR_BASE - 1)
36 
37 /* No tailer byte while expecting it */
38 #define QCAFRM_NOTAIL (QCAFRM_ERR_BASE - 2)
39 
40 /* Frame length is invalid */
41 #define QCAFRM_INVLEN (QCAFRM_ERR_BASE - 3)
42 
43 /* Frame length is invalid */
44 #define QCAFRM_INVFRAME (QCAFRM_ERR_BASE - 4)
45 
46 /* Min/Max Ethernet MTU: 46/1500 */
47 #define QCAFRM_MIN_MTU (ETH_ZLEN - ETH_HLEN)
48 #define QCAFRM_MAX_MTU ETH_DATA_LEN
49 
50 /* Min/Max frame lengths */
51 #define QCAFRM_MIN_LEN (QCAFRM_MIN_MTU + ETH_HLEN)
52 #define QCAFRM_MAX_LEN (QCAFRM_MAX_MTU + VLAN_ETH_HLEN)
53 
54 /* QCA7K header len */
55 #define QCAFRM_HEADER_LEN 8
56 
57 /* QCA7K footer len */
58 #define QCAFRM_FOOTER_LEN 2
59 
60 /* QCA7K Framing. */
61 #define QCAFRM_ERR_BASE -1000
62 
63 enum qcafrm_state {
64 	/* HW length is only available on SPI */
65 	QCAFRM_HW_LEN0 = 0x8000,
66 	QCAFRM_HW_LEN1 = QCAFRM_HW_LEN0 - 1,
67 	QCAFRM_HW_LEN2 = QCAFRM_HW_LEN1 - 1,
68 	QCAFRM_HW_LEN3 = QCAFRM_HW_LEN2 - 1,
69 
70 	/*  Waiting first 0xAA of header */
71 	QCAFRM_WAIT_AA1 = QCAFRM_HW_LEN3 - 1,
72 
73 	/*  Waiting second 0xAA of header */
74 	QCAFRM_WAIT_AA2 = QCAFRM_WAIT_AA1 - 1,
75 
76 	/*  Waiting third 0xAA of header */
77 	QCAFRM_WAIT_AA3 = QCAFRM_WAIT_AA2 - 1,
78 
79 	/*  Waiting fourth 0xAA of header */
80 	QCAFRM_WAIT_AA4 = QCAFRM_WAIT_AA3 - 1,
81 
82 	/*  Waiting Byte 0-1 of length (litte endian) */
83 	QCAFRM_WAIT_LEN_BYTE0 = QCAFRM_WAIT_AA4 - 1,
84 	QCAFRM_WAIT_LEN_BYTE1 = QCAFRM_WAIT_AA4 - 2,
85 
86 	/* Reserved bytes */
87 	QCAFRM_WAIT_RSVD_BYTE1 = QCAFRM_WAIT_AA4 - 3,
88 	QCAFRM_WAIT_RSVD_BYTE2 = QCAFRM_WAIT_AA4 - 4,
89 
90 	/*  The frame length is used as the state until
91 	 *  the end of the Ethernet frame
92 	 *  Waiting for first 0x55 of footer
93 	 */
94 	QCAFRM_WAIT_551 = 1,
95 
96 	/*  Waiting for second 0x55 of footer */
97 	QCAFRM_WAIT_552 = QCAFRM_WAIT_551 - 1
98 };
99 
100 /*   Structure to maintain the frame decoding during reception. */
101 
102 struct qcafrm_handle {
103 	/*  Current decoding state */
104 	enum qcafrm_state state;
105 	/* Initial state depends on connection type */
106 	enum qcafrm_state init;
107 
108 	/* Offset in buffer (borrowed for length too) */
109 	u16 offset;
110 
111 	/* Frame length as kept by this module */
112 	u16 len;
113 };
114 
115 u16 qcafrm_create_header(u8 *buf, u16 len);
116 
117 u16 qcafrm_create_footer(u8 *buf);
118 
qcafrm_fsm_init_spi(struct qcafrm_handle * handle)119 static inline void qcafrm_fsm_init_spi(struct qcafrm_handle *handle)
120 {
121 	handle->init = QCAFRM_HW_LEN0;
122 	handle->state = handle->init;
123 }
124 
qcafrm_fsm_init_uart(struct qcafrm_handle * handle)125 static inline void qcafrm_fsm_init_uart(struct qcafrm_handle *handle)
126 {
127 	handle->init = QCAFRM_WAIT_AA1;
128 	handle->state = handle->init;
129 }
130 
131 /*   Gather received bytes and try to extract a full Ethernet frame
132  *   by following a simple state machine.
133  *
134  * Return:   QCAFRM_GATHER       No Ethernet frame fully received yet.
135  *           QCAFRM_NOHEAD       Header expected but not found.
136  *           QCAFRM_INVLEN       QCA7K frame length is invalid
137  *           QCAFRM_NOTAIL       Footer expected but not found.
138  *           > 0                 Number of byte in the fully received
139  *                               Ethernet frame
140  */
141 
142 s32 qcafrm_fsm_decode(struct qcafrm_handle *handle, u8 *buf, u16 buf_len, u8 recv_byte);
143 
144 #endif /* _QCA_FRAMING_H */
145