18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *   Copyright (c) 2011, 2012, Atheros Communications Inc.
38c2ecf20Sopenharmony_ci *   Copyright (c) 2014, I2SE GmbH
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *   Permission to use, copy, modify, and/or distribute this software
68c2ecf20Sopenharmony_ci *   for any purpose with or without fee is hereby granted, provided
78c2ecf20Sopenharmony_ci *   that the above copyright notice and this permission notice appear
88c2ecf20Sopenharmony_ci *   in all copies.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
118c2ecf20Sopenharmony_ci *   WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
128c2ecf20Sopenharmony_ci *   WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
138c2ecf20Sopenharmony_ci *   THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
148c2ecf20Sopenharmony_ci *   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
158c2ecf20Sopenharmony_ci *   LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
168c2ecf20Sopenharmony_ci *   NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
178c2ecf20Sopenharmony_ci *   CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*   Atheros Ethernet framing. Every Ethernet frame is surrounded by an atheros
218c2ecf20Sopenharmony_ci *   frame while transmitted over a serial channel.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#ifndef _QCA_FRAMING_H
258c2ecf20Sopenharmony_ci#define _QCA_FRAMING_H
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include <linux/if_ether.h>
288c2ecf20Sopenharmony_ci#include <linux/if_vlan.h>
298c2ecf20Sopenharmony_ci#include <linux/types.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* Frame is currently being received */
328c2ecf20Sopenharmony_ci#define QCAFRM_GATHER 0
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/*  No header byte while expecting it */
358c2ecf20Sopenharmony_ci#define QCAFRM_NOHEAD (QCAFRM_ERR_BASE - 1)
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* No tailer byte while expecting it */
388c2ecf20Sopenharmony_ci#define QCAFRM_NOTAIL (QCAFRM_ERR_BASE - 2)
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/* Frame length is invalid */
418c2ecf20Sopenharmony_ci#define QCAFRM_INVLEN (QCAFRM_ERR_BASE - 3)
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/* Frame length is invalid */
448c2ecf20Sopenharmony_ci#define QCAFRM_INVFRAME (QCAFRM_ERR_BASE - 4)
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/* Min/Max Ethernet MTU: 46/1500 */
478c2ecf20Sopenharmony_ci#define QCAFRM_MIN_MTU (ETH_ZLEN - ETH_HLEN)
488c2ecf20Sopenharmony_ci#define QCAFRM_MAX_MTU ETH_DATA_LEN
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/* Min/Max frame lengths */
518c2ecf20Sopenharmony_ci#define QCAFRM_MIN_LEN (QCAFRM_MIN_MTU + ETH_HLEN)
528c2ecf20Sopenharmony_ci#define QCAFRM_MAX_LEN (QCAFRM_MAX_MTU + VLAN_ETH_HLEN)
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/* QCA7K header len */
558c2ecf20Sopenharmony_ci#define QCAFRM_HEADER_LEN 8
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/* QCA7K footer len */
588c2ecf20Sopenharmony_ci#define QCAFRM_FOOTER_LEN 2
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/* QCA7K Framing. */
618c2ecf20Sopenharmony_ci#define QCAFRM_ERR_BASE -1000
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cienum qcafrm_state {
648c2ecf20Sopenharmony_ci	/* HW length is only available on SPI */
658c2ecf20Sopenharmony_ci	QCAFRM_HW_LEN0 = 0x8000,
668c2ecf20Sopenharmony_ci	QCAFRM_HW_LEN1 = QCAFRM_HW_LEN0 - 1,
678c2ecf20Sopenharmony_ci	QCAFRM_HW_LEN2 = QCAFRM_HW_LEN1 - 1,
688c2ecf20Sopenharmony_ci	QCAFRM_HW_LEN3 = QCAFRM_HW_LEN2 - 1,
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	/*  Waiting first 0xAA of header */
718c2ecf20Sopenharmony_ci	QCAFRM_WAIT_AA1 = QCAFRM_HW_LEN3 - 1,
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	/*  Waiting second 0xAA of header */
748c2ecf20Sopenharmony_ci	QCAFRM_WAIT_AA2 = QCAFRM_WAIT_AA1 - 1,
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	/*  Waiting third 0xAA of header */
778c2ecf20Sopenharmony_ci	QCAFRM_WAIT_AA3 = QCAFRM_WAIT_AA2 - 1,
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	/*  Waiting fourth 0xAA of header */
808c2ecf20Sopenharmony_ci	QCAFRM_WAIT_AA4 = QCAFRM_WAIT_AA3 - 1,
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	/*  Waiting Byte 0-1 of length (litte endian) */
838c2ecf20Sopenharmony_ci	QCAFRM_WAIT_LEN_BYTE0 = QCAFRM_WAIT_AA4 - 1,
848c2ecf20Sopenharmony_ci	QCAFRM_WAIT_LEN_BYTE1 = QCAFRM_WAIT_AA4 - 2,
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	/* Reserved bytes */
878c2ecf20Sopenharmony_ci	QCAFRM_WAIT_RSVD_BYTE1 = QCAFRM_WAIT_AA4 - 3,
888c2ecf20Sopenharmony_ci	QCAFRM_WAIT_RSVD_BYTE2 = QCAFRM_WAIT_AA4 - 4,
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	/*  The frame length is used as the state until
918c2ecf20Sopenharmony_ci	 *  the end of the Ethernet frame
928c2ecf20Sopenharmony_ci	 *  Waiting for first 0x55 of footer
938c2ecf20Sopenharmony_ci	 */
948c2ecf20Sopenharmony_ci	QCAFRM_WAIT_551 = 1,
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/*  Waiting for second 0x55 of footer */
978c2ecf20Sopenharmony_ci	QCAFRM_WAIT_552 = QCAFRM_WAIT_551 - 1
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/*   Structure to maintain the frame decoding during reception. */
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistruct qcafrm_handle {
1038c2ecf20Sopenharmony_ci	/*  Current decoding state */
1048c2ecf20Sopenharmony_ci	enum qcafrm_state state;
1058c2ecf20Sopenharmony_ci	/* Initial state depends on connection type */
1068c2ecf20Sopenharmony_ci	enum qcafrm_state init;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	/* Offset in buffer (borrowed for length too) */
1098c2ecf20Sopenharmony_ci	u16 offset;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	/* Frame length as kept by this module */
1128c2ecf20Sopenharmony_ci	u16 len;
1138c2ecf20Sopenharmony_ci};
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ciu16 qcafrm_create_header(u8 *buf, u16 len);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ciu16 qcafrm_create_footer(u8 *buf);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic inline void qcafrm_fsm_init_spi(struct qcafrm_handle *handle)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	handle->init = QCAFRM_HW_LEN0;
1228c2ecf20Sopenharmony_ci	handle->state = handle->init;
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic inline void qcafrm_fsm_init_uart(struct qcafrm_handle *handle)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	handle->init = QCAFRM_WAIT_AA1;
1288c2ecf20Sopenharmony_ci	handle->state = handle->init;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci/*   Gather received bytes and try to extract a full Ethernet frame
1328c2ecf20Sopenharmony_ci *   by following a simple state machine.
1338c2ecf20Sopenharmony_ci *
1348c2ecf20Sopenharmony_ci * Return:   QCAFRM_GATHER       No Ethernet frame fully received yet.
1358c2ecf20Sopenharmony_ci *           QCAFRM_NOHEAD       Header expected but not found.
1368c2ecf20Sopenharmony_ci *           QCAFRM_INVLEN       QCA7K frame length is invalid
1378c2ecf20Sopenharmony_ci *           QCAFRM_NOTAIL       Footer expected but not found.
1388c2ecf20Sopenharmony_ci *           > 0                 Number of byte in the fully received
1398c2ecf20Sopenharmony_ci *                               Ethernet frame
1408c2ecf20Sopenharmony_ci */
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cis32 qcafrm_fsm_decode(struct qcafrm_handle *handle, u8 *buf, u16 buf_len, u8 recv_byte);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci#endif /* _QCA_FRAMING_H */
145