Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  *   Copyright (c) 2011, 2012, Atheros Communications Inc.
0003  *   Copyright (c) 2014, I2SE GmbH
0004  *
0005  *   Permission to use, copy, modify, and/or distribute this software
0006  *   for any purpose with or without fee is hereby granted, provided
0007  *   that the above copyright notice and this permission notice appear
0008  *   in all copies.
0009  *
0010  *   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
0011  *   WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
0012  *   WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
0013  *   THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
0014  *   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
0015  *   LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
0016  *   NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
0017  *   CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0018  */
0019 
0020 /*   Atheros Ethernet framing. Every Ethernet frame is surrounded by an atheros
0021  *   frame while transmitted over a serial channel.
0022  */
0023 
0024 #ifndef _QCA_FRAMING_H
0025 #define _QCA_FRAMING_H
0026 
0027 #include <linux/if_ether.h>
0028 #include <linux/if_vlan.h>
0029 #include <linux/types.h>
0030 
0031 /* Frame is currently being received */
0032 #define QCAFRM_GATHER 0
0033 
0034 /*  No header byte while expecting it */
0035 #define QCAFRM_NOHEAD (QCAFRM_ERR_BASE - 1)
0036 
0037 /* No tailer byte while expecting it */
0038 #define QCAFRM_NOTAIL (QCAFRM_ERR_BASE - 2)
0039 
0040 /* Frame length is invalid */
0041 #define QCAFRM_INVLEN (QCAFRM_ERR_BASE - 3)
0042 
0043 /* Frame length is invalid */
0044 #define QCAFRM_INVFRAME (QCAFRM_ERR_BASE - 4)
0045 
0046 /* Min/Max Ethernet MTU: 46/1500 */
0047 #define QCAFRM_MIN_MTU (ETH_ZLEN - ETH_HLEN)
0048 #define QCAFRM_MAX_MTU ETH_DATA_LEN
0049 
0050 /* Min/Max frame lengths */
0051 #define QCAFRM_MIN_LEN (QCAFRM_MIN_MTU + ETH_HLEN)
0052 #define QCAFRM_MAX_LEN (QCAFRM_MAX_MTU + VLAN_ETH_HLEN)
0053 
0054 /* QCA7K header len */
0055 #define QCAFRM_HEADER_LEN 8
0056 
0057 /* QCA7K footer len */
0058 #define QCAFRM_FOOTER_LEN 2
0059 
0060 /* QCA7K Framing. */
0061 #define QCAFRM_ERR_BASE -1000
0062 
0063 enum qcafrm_state {
0064     /* HW length is only available on SPI */
0065     QCAFRM_HW_LEN0 = 0x8000,
0066     QCAFRM_HW_LEN1 = QCAFRM_HW_LEN0 - 1,
0067     QCAFRM_HW_LEN2 = QCAFRM_HW_LEN1 - 1,
0068     QCAFRM_HW_LEN3 = QCAFRM_HW_LEN2 - 1,
0069 
0070     /*  Waiting first 0xAA of header */
0071     QCAFRM_WAIT_AA1 = QCAFRM_HW_LEN3 - 1,
0072 
0073     /*  Waiting second 0xAA of header */
0074     QCAFRM_WAIT_AA2 = QCAFRM_WAIT_AA1 - 1,
0075 
0076     /*  Waiting third 0xAA of header */
0077     QCAFRM_WAIT_AA3 = QCAFRM_WAIT_AA2 - 1,
0078 
0079     /*  Waiting fourth 0xAA of header */
0080     QCAFRM_WAIT_AA4 = QCAFRM_WAIT_AA3 - 1,
0081 
0082     /*  Waiting Byte 0-1 of length (litte endian) */
0083     QCAFRM_WAIT_LEN_BYTE0 = QCAFRM_WAIT_AA4 - 1,
0084     QCAFRM_WAIT_LEN_BYTE1 = QCAFRM_WAIT_AA4 - 2,
0085 
0086     /* Reserved bytes */
0087     QCAFRM_WAIT_RSVD_BYTE1 = QCAFRM_WAIT_AA4 - 3,
0088     QCAFRM_WAIT_RSVD_BYTE2 = QCAFRM_WAIT_AA4 - 4,
0089 
0090     /*  The frame length is used as the state until
0091      *  the end of the Ethernet frame
0092      *  Waiting for first 0x55 of footer
0093      */
0094     QCAFRM_WAIT_551 = 1,
0095 
0096     /*  Waiting for second 0x55 of footer */
0097     QCAFRM_WAIT_552 = QCAFRM_WAIT_551 - 1
0098 };
0099 
0100 /*   Structure to maintain the frame decoding during reception. */
0101 
0102 struct qcafrm_handle {
0103     /*  Current decoding state */
0104     enum qcafrm_state state;
0105     /* Initial state depends on connection type */
0106     enum qcafrm_state init;
0107 
0108     /* Offset in buffer (borrowed for length too) */
0109     u16 offset;
0110 
0111     /* Frame length as kept by this module */
0112     u16 len;
0113 };
0114 
0115 u16 qcafrm_create_header(u8 *buf, u16 len);
0116 
0117 u16 qcafrm_create_footer(u8 *buf);
0118 
0119 static inline void qcafrm_fsm_init_spi(struct qcafrm_handle *handle)
0120 {
0121     handle->init = QCAFRM_HW_LEN0;
0122     handle->state = handle->init;
0123 }
0124 
0125 static inline void qcafrm_fsm_init_uart(struct qcafrm_handle *handle)
0126 {
0127     handle->init = QCAFRM_WAIT_AA1;
0128     handle->state = handle->init;
0129 }
0130 
0131 /*   Gather received bytes and try to extract a full Ethernet frame
0132  *   by following a simple state machine.
0133  *
0134  * Return:   QCAFRM_GATHER       No Ethernet frame fully received yet.
0135  *           QCAFRM_NOHEAD       Header expected but not found.
0136  *           QCAFRM_INVLEN       QCA7K frame length is invalid
0137  *           QCAFRM_NOTAIL       Footer expected but not found.
0138  *           > 0                 Number of byte in the fully received
0139  *                               Ethernet frame
0140  */
0141 
0142 s32 qcafrm_fsm_decode(struct qcafrm_handle *handle, u8 *buf, u16 buf_len, u8 recv_byte);
0143 
0144 #endif /* _QCA_FRAMING_H */