1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
2 /*
3  *   Copyright (c) 2011, 2012, Atheros Communications Inc.
4  *   Copyright (c) 2014, I2SE GmbH
5  */
6 
7 /*   Atheros Ethernet framing. Every Ethernet frame is surrounded by an atheros
8  *   frame while transmitted over a serial channel.
9  */
10 
11 #ifndef _QCA_FRAMING_H
12 #define _QCA_FRAMING_H
13 
14 #include <linux/if_ether.h>
15 #include <linux/if_vlan.h>
16 #include <linux/types.h>
17 
18 /* Frame is currently being received */
19 #define QCAFRM_GATHER 0
20 
21 /*  No header byte while expecting it */
22 #define QCAFRM_NOHEAD (QCAFRM_ERR_BASE - 1)
23 
24 /* No tailer byte while expecting it */
25 #define QCAFRM_NOTAIL (QCAFRM_ERR_BASE - 2)
26 
27 /* Frame length is invalid */
28 #define QCAFRM_INVLEN (QCAFRM_ERR_BASE - 3)
29 
30 /* Frame length is invalid */
31 #define QCAFRM_INVFRAME (QCAFRM_ERR_BASE - 4)
32 
33 /* Min/Max Ethernet MTU: 46/1500 */
34 #define QCAFRM_MIN_MTU (ETH_ZLEN - ETH_HLEN)
35 #define QCAFRM_MAX_MTU ETH_DATA_LEN
36 
37 /* Min/Max frame lengths */
38 #define QCAFRM_MIN_LEN (QCAFRM_MIN_MTU + ETH_HLEN)
39 #define QCAFRM_MAX_LEN (QCAFRM_MAX_MTU + VLAN_ETH_HLEN)
40 
41 /* QCA7K header len */
42 #define QCAFRM_HEADER_LEN 8
43 
44 /* QCA7K footer len */
45 #define QCAFRM_FOOTER_LEN 2
46 
47 /* QCA7K Framing. */
48 #define QCAFRM_ERR_BASE -1000
49 
50 enum qcafrm_state {
51 	/* HW length is only available on SPI */
52 	QCAFRM_HW_LEN0 = 0x8000,
53 	QCAFRM_HW_LEN1 = QCAFRM_HW_LEN0 - 1,
54 	QCAFRM_HW_LEN2 = QCAFRM_HW_LEN1 - 1,
55 	QCAFRM_HW_LEN3 = QCAFRM_HW_LEN2 - 1,
56 
57 	/*  Waiting first 0xAA of header */
58 	QCAFRM_WAIT_AA1 = QCAFRM_HW_LEN3 - 1,
59 
60 	/*  Waiting second 0xAA of header */
61 	QCAFRM_WAIT_AA2 = QCAFRM_WAIT_AA1 - 1,
62 
63 	/*  Waiting third 0xAA of header */
64 	QCAFRM_WAIT_AA3 = QCAFRM_WAIT_AA2 - 1,
65 
66 	/*  Waiting fourth 0xAA of header */
67 	QCAFRM_WAIT_AA4 = QCAFRM_WAIT_AA3 - 1,
68 
69 	/*  Waiting Byte 0-1 of length (litte endian) */
70 	QCAFRM_WAIT_LEN_BYTE0 = QCAFRM_WAIT_AA4 - 1,
71 	QCAFRM_WAIT_LEN_BYTE1 = QCAFRM_WAIT_AA4 - 2,
72 
73 	/* Reserved bytes */
74 	QCAFRM_WAIT_RSVD_BYTE1 = QCAFRM_WAIT_AA4 - 3,
75 	QCAFRM_WAIT_RSVD_BYTE2 = QCAFRM_WAIT_AA4 - 4,
76 
77 	/*  The frame length is used as the state until
78 	 *  the end of the Ethernet frame
79 	 *  Waiting for first 0x55 of footer
80 	 */
81 	QCAFRM_WAIT_551 = 1,
82 
83 	/*  Waiting for second 0x55 of footer */
84 	QCAFRM_WAIT_552 = QCAFRM_WAIT_551 - 1
85 };
86 
87 /*   Structure to maintain the frame decoding during reception. */
88 
89 struct qcafrm_handle {
90 	/*  Current decoding state */
91 	enum qcafrm_state state;
92 	/* Initial state depends on connection type */
93 	enum qcafrm_state init;
94 
95 	/* Offset in buffer (borrowed for length too) */
96 	u16 offset;
97 };
98 
99 u16 qcafrm_create_header(u8 *buf, u16 len);
100 
101 u16 qcafrm_create_footer(u8 *buf);
102 
qcafrm_fsm_init_spi(struct qcafrm_handle * handle)103 static inline void qcafrm_fsm_init_spi(struct qcafrm_handle *handle)
104 {
105 	handle->init = QCAFRM_HW_LEN0;
106 	handle->state = handle->init;
107 }
108 
qcafrm_fsm_init_uart(struct qcafrm_handle * handle)109 static inline void qcafrm_fsm_init_uart(struct qcafrm_handle *handle)
110 {
111 	handle->init = QCAFRM_WAIT_AA1;
112 	handle->state = handle->init;
113 }
114 
115 s32 qcafrm_fsm_decode(struct qcafrm_handle *handle, u8 *buf, u16 buf_len, u8 recv_byte);
116 
117 #endif /* _QCA_FRAMING_H */
118