staging: vt6656: removed custom 802.11 header usage
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / vt6656 / wctl.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * File: wctl.c
20  *
21  * Purpose: handle WMAC duplicate filter & defragment
22  *
23  * Author: Jerry Chen
24  *
25  * Date: Jun. 27, 2002
26  *
27  * Functions:
28  *      WCTLbIsDuplicate - Test if duplicate packet
29  *      WCTLuSearchDFCB - Search DeFragment Control Database
30  *      WCTLuInsertDFCB - Insert DeFragment Control Database
31  *      WCTLbHandleFragment - Handle received fragment packet
32  *
33  * Revision History:
34  *
35  */
36
37 #include "wctl.h"
38 #include "device.h"
39 #include "card.h"
40 #include "tmacro.h"
41
42 /*---------------------  Static Definitions -------------------------*/
43
44 /*---------------------  Static Classes  ----------------------------*/
45
46 /*---------------------  Static Variables  --------------------------*/
47 // static int          msglevel                =MSG_LEVEL_INFO;
48 /*---------------------  Static Functions  --------------------------*/
49
50 /*---------------------  Export Variables  --------------------------*/
51
52
53
54 /*
55  * Description:
56  *      Scan Rx cache.  Return true if packet is duplicate, else
57  *      inserts in receive cache and returns false.
58  *
59  * Parameters:
60  *  In:
61  *      pCache      - Receive packets history
62  *      pMACHeader  - 802.11 MAC Header of received packet
63  *  Out:
64  *      none
65  *
66  * Return Value: true if packet duplicate; otherwise false
67  *
68  */
69
70 bool WCTLbIsDuplicate (PSCache pCache, struct ieee80211_hdr *pMACHeader)
71 {
72     unsigned int            uIndex;
73     unsigned int            ii;
74     PSCacheEntry    pCacheEntry;
75
76     if (IS_FC_RETRY(pMACHeader)) {
77
78         uIndex = pCache->uInPtr;
79         for (ii = 0; ii < DUPLICATE_RX_CACHE_LENGTH; ii++) {
80             pCacheEntry = &(pCache->asCacheEntry[uIndex]);
81             if ((pCacheEntry->wFmSequence == pMACHeader->seq_ctrl) &&
82                 (!compare_ether_addr(&(pCacheEntry->abyAddr2[0]),
83                                      &(pMACHeader->addr2[0]))) &&
84                 (LOBYTE(pCacheEntry->wFrameCtl) == LOBYTE(pMACHeader->frame_control))
85                 ) {
86                 /* Duplicate match */
87                 return true;
88             }
89             ADD_ONE_WITH_WRAP_AROUND(uIndex, DUPLICATE_RX_CACHE_LENGTH);
90         }
91     }
92     /* Not found in cache - insert */
93     pCacheEntry = &pCache->asCacheEntry[pCache->uInPtr];
94     pCacheEntry->wFmSequence = pMACHeader->seq_ctrl;
95     memcpy(&(pCacheEntry->abyAddr2[0]), &(pMACHeader->addr2[0]), ETH_ALEN);
96     pCacheEntry->wFrameCtl = pMACHeader->frame_control;
97     ADD_ONE_WITH_WRAP_AROUND(pCache->uInPtr, DUPLICATE_RX_CACHE_LENGTH);
98     return false;
99 }
100
101 /*
102  * Description:
103  *      Found if sequence number of received fragment packet in Defragment Database
104  *
105  * Parameters:
106  *  In:
107  *      pDevice     - Pointer to adapter
108  *      pMACHeader  - 802.11 MAC Header of received packet
109  *  Out:
110  *      none
111  *
112  * Return Value: index number in Defragment Database
113  *
114  */
115
116 unsigned int WCTLuSearchDFCB(struct vnt_private *pDevice,
117                              struct ieee80211_hdr *pMACHeader)
118 {
119         unsigned int ii;
120
121         for (ii = 0; ii < pDevice->cbDFCB; ii++) {
122                 if ((pDevice->sRxDFCB[ii].bInUse == true) &&
123                     (!compare_ether_addr(&(pDevice->sRxDFCB[ii].abyAddr2[0]),
124                                           &(pMACHeader->addr2[0])))) {
125                         return ii;
126                 }
127         }
128         return pDevice->cbDFCB;
129 }
130
131 /*
132  * Description:
133  *      Insert received fragment packet in Defragment Database
134  *
135  * Parameters:
136  *  In:
137  *      pDevice     - Pointer to adapter
138  *      pMACHeader  - 802.11 MAC Header of received packet
139  *  Out:
140  *      none
141  *
142  * Return Value: index number in Defragment Database
143  *
144  */
145 unsigned int WCTLuInsertDFCB(struct vnt_private *pDevice,
146                              struct ieee80211_hdr *pMACHeader)
147 {
148         unsigned int ii;
149
150     if (pDevice->cbFreeDFCB == 0)
151         return(pDevice->cbDFCB);
152     for (ii = 0; ii < pDevice->cbDFCB; ii++) {
153         if (pDevice->sRxDFCB[ii].bInUse == false) {
154             pDevice->cbFreeDFCB--;
155             pDevice->sRxDFCB[ii].uLifetime = pDevice->dwMaxReceiveLifetime;
156             pDevice->sRxDFCB[ii].bInUse = true;
157             pDevice->sRxDFCB[ii].wSequence = (pMACHeader->seq_ctrl >> 4);
158             pDevice->sRxDFCB[ii].wFragNum = (pMACHeader->seq_ctrl & 0x000F);
159             memcpy(&(pDevice->sRxDFCB[ii].abyAddr2[0]),
160                    &(pMACHeader->addr2[0]),
161                    ETH_ALEN);
162             return(ii);
163         }
164     }
165     return(pDevice->cbDFCB);
166 }
167
168
169 /*
170  * Description:
171  *      Handle received fragment packet
172  *
173  * Parameters:
174  *  In:
175  *      pDevice         - Pointer to adapter
176  *      pMACHeader      - 802.11 MAC Header of received packet
177  *      cbFrameLength   - Frame length
178  *      bWEP            - is WEP packet
179  *  Out:
180  *      none
181  *
182  * Return Value: true if it is valid fragment packet and we have resource to defragment; otherwise false
183  *
184  */
185 bool WCTLbHandleFragment(struct vnt_private *pDevice, struct ieee80211_hdr *pMACHeader, unsigned int cbFrameLength, bool bWEP, bool bExtIV)
186 {
187         unsigned int uHeaderSize;
188
189
190     if (bWEP == true) {
191         uHeaderSize = 28;
192         if (bExtIV)
193         // ExtIV
194             uHeaderSize +=4;
195     }
196     else {
197         uHeaderSize = 24;
198     }
199
200     if (IS_FIRST_FRAGMENT_PKT(pMACHeader)) {
201         pDevice->uCurrentDFCBIdx = WCTLuSearchDFCB(pDevice, pMACHeader);
202         if (pDevice->uCurrentDFCBIdx < pDevice->cbDFCB) {
203             // duplicate, we must flush previous DCB
204             pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].uLifetime = pDevice->dwMaxReceiveLifetime;
205             pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wSequence = (pMACHeader->seq_ctrl >> 4);
206             pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wFragNum = (pMACHeader->seq_ctrl & 0x000F);
207         }
208         else {
209             pDevice->uCurrentDFCBIdx = WCTLuInsertDFCB(pDevice, pMACHeader);
210             if (pDevice->uCurrentDFCBIdx == pDevice->cbDFCB) {
211                 return(false);
212             }
213         }
214         // reserve 8 byte to match MAC RX Buffer
215         pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer = (u8 *) (pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].skb->data + 8);
216 //        pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer = (u8 *) (pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].skb->data + 4);
217         memcpy(pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer, pMACHeader, cbFrameLength);
218         pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].cbFrameLength = cbFrameLength;
219         pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer += cbFrameLength;
220         pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wFragNum++;
221         //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "First pDevice->uCurrentDFCBIdx= %d\n", pDevice->uCurrentDFCBIdx);
222         return(false);
223     }
224     else {
225         pDevice->uCurrentDFCBIdx = WCTLuSearchDFCB(pDevice, pMACHeader);
226         if (pDevice->uCurrentDFCBIdx != pDevice->cbDFCB) {
227             if ((pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wSequence == (pMACHeader->seq_ctrl >> 4)) &&
228                 (pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wFragNum == (pMACHeader->seq_ctrl & 0x000F)) &&
229                 ((pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].cbFrameLength + cbFrameLength - uHeaderSize) < 2346)) {
230
231                 memcpy(pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer, ((u8 *) (pMACHeader) + uHeaderSize), (cbFrameLength - uHeaderSize));
232                 pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].cbFrameLength += (cbFrameLength - uHeaderSize);
233                 pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer += (cbFrameLength - uHeaderSize);
234                 pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wFragNum++;
235                 //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Second pDevice->uCurrentDFCBIdx= %d\n", pDevice->uCurrentDFCBIdx);
236             }
237             else {
238                 // seq error or frag # error flush DFCB
239                 pDevice->cbFreeDFCB++;
240                 pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].bInUse = false;
241                 return(false);
242             }
243         }
244         else {
245             return(false);
246         }
247         if (IS_LAST_FRAGMENT_PKT(pMACHeader)) {
248             //enq defragcontrolblock
249             pDevice->cbFreeDFCB++;
250             pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].bInUse = false;
251             //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Last pDevice->uCurrentDFCBIdx= %d\n", pDevice->uCurrentDFCBIdx);
252             return(true);
253         }
254         return(false);
255     }
256 }
257
258