Merge branch 'slab/next' into slab/for-linus
[firefly-linux-kernel-4.4.55.git] / drivers / staging / vt6656 / bssdb.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: bssdb.c
20  *
21  * Purpose: Handles the Basic Service Set & Node Database functions
22  *
23  * Functions:
24  *      BSSpSearchBSSList - Search known BSS list for Desire SSID or BSSID
25  *      BSSvClearBSSList - Clear BSS List
26  *      BSSbInsertToBSSList - Insert a BSS set into known BSS list
27  *      BSSbUpdateToBSSList - Update BSS set in known BSS list
28  *      BSSbIsSTAInNodeDB - Search Node DB table to find the index of matched DstAddr
29  *      BSSvCreateOneNode - Allocate an Node for Node DB
30  *      BSSvUpdateAPNode - Update AP Node content in Index 0 of KnownNodeDB
31  *      BSSvSecondCallBack - One second timer callback function to update Node DB info & AP link status
32  *      BSSvUpdateNodeTxCounter - Update Tx attemps, Tx failure counter in Node DB for auto-fall back rate control
33  *
34  * Revision History:
35  *
36  * Author: Lyndon Chen
37  *
38  * Date: July 17, 2002
39  *
40  */
41
42 #include "ttype.h"
43 #include "tmacro.h"
44 #include "tether.h"
45 #include "device.h"
46 #include "80211hdr.h"
47 #include "bssdb.h"
48 #include "wmgr.h"
49 #include "datarate.h"
50 #include "desc.h"
51 #include "wcmd.h"
52 #include "wpa.h"
53 #include "baseband.h"
54 #include "rf.h"
55 #include "card.h"
56 #include "mac.h"
57 #include "wpa2.h"
58 #include "control.h"
59 #include "rndis.h"
60 #include "iowpa.h"
61
62 /*---------------------  Static Definitions -------------------------*/
63
64
65
66
67 /*---------------------  Static Classes  ----------------------------*/
68
69 /*---------------------  Static Variables  --------------------------*/
70 static int          msglevel                =MSG_LEVEL_INFO;
71 //static int          msglevel                =MSG_LEVEL_DEBUG;
72
73
74
75 const WORD             awHWRetry0[5][5] = {
76                                             {RATE_18M, RATE_18M, RATE_12M, RATE_12M, RATE_12M},
77                                             {RATE_24M, RATE_24M, RATE_18M, RATE_12M, RATE_12M},
78                                             {RATE_36M, RATE_36M, RATE_24M, RATE_18M, RATE_18M},
79                                             {RATE_48M, RATE_48M, RATE_36M, RATE_24M, RATE_24M},
80                                             {RATE_54M, RATE_54M, RATE_48M, RATE_36M, RATE_36M}
81                                            };
82 const WORD             awHWRetry1[5][5] = {
83                                             {RATE_18M, RATE_18M, RATE_12M, RATE_6M, RATE_6M},
84                                             {RATE_24M, RATE_24M, RATE_18M, RATE_6M, RATE_6M},
85                                             {RATE_36M, RATE_36M, RATE_24M, RATE_12M, RATE_12M},
86                                             {RATE_48M, RATE_48M, RATE_24M, RATE_12M, RATE_12M},
87                                             {RATE_54M, RATE_54M, RATE_36M, RATE_18M, RATE_18M}
88                                            };
89
90
91
92 /*---------------------  Static Functions  --------------------------*/
93
94 static void s_vCheckSensitivity(struct vnt_private *pDevice);
95 static void s_vCheckPreEDThreshold(struct vnt_private *pDevice);
96 static void s_uCalculateLinkQual(struct vnt_private *pDevice);
97
98 /*---------------------  Export Variables  --------------------------*/
99
100
101 /*---------------------  Export Functions  --------------------------*/
102
103
104
105
106
107 /*+
108  *
109  * Routine Description:
110  *    Search known BSS list for Desire SSID or BSSID.
111  *
112  * Return Value:
113  *    PTR to KnownBSS or NULL
114  *
115 -*/
116
117 PKnownBSS BSSpSearchBSSList(struct vnt_private *pDevice,
118                 u8 *pbyDesireBSSID, u8 *pbyDesireSSID,
119                 CARD_PHY_TYPE ePhyType)
120 {
121         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
122         u8 *pbyBSSID = NULL;
123         PWLAN_IE_SSID pSSID = NULL;
124         PKnownBSS pCurrBSS = NULL;
125         PKnownBSS pSelect = NULL;
126         u8 ZeroBSSID[WLAN_BSSID_LEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
127         int ii = 0;
128         int jj = 0;
129
130     if (pbyDesireBSSID != NULL) {
131                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
132                         "BSSpSearchBSSList BSSID[%pM]\n", pbyDesireBSSID);
133         if ((!is_broadcast_ether_addr(pbyDesireBSSID)) &&
134              (memcmp(pbyDesireBSSID, ZeroBSSID, 6)!= 0)){
135             pbyBSSID = pbyDesireBSSID;
136         }
137     }
138     if (pbyDesireSSID != NULL) {
139         if (((PWLAN_IE_SSID)pbyDesireSSID)->len != 0) {
140             pSSID = (PWLAN_IE_SSID) pbyDesireSSID;
141         }
142     }
143
144     if ((pbyBSSID != NULL)&&(pDevice->bRoaming == false)) {
145         // match BSSID first
146         for (ii = 0; ii <MAX_BSS_NUM; ii++) {
147             pCurrBSS = &(pMgmt->sBSSList[ii]);
148
149            pCurrBSS->bSelected = false;
150
151             if ((pCurrBSS->bActive) &&
152                 (pCurrBSS->bSelected == false)) {
153                     if (!compare_ether_addr(pCurrBSS->abyBSSID, pbyBSSID)) {
154                     if (pSSID != NULL) {
155                         // compare ssid
156                         if ( !memcmp(pSSID->abySSID,
157                             ((PWLAN_IE_SSID)pCurrBSS->abySSID)->abySSID,
158                             pSSID->len)) {
159                             if ((pMgmt->eConfigMode == WMAC_CONFIG_AUTO) ||
160                                 ((pMgmt->eConfigMode == WMAC_CONFIG_IBSS_STA) && WLAN_GET_CAP_INFO_IBSS(pCurrBSS->wCapInfo)) ||
161                                 ((pMgmt->eConfigMode == WMAC_CONFIG_ESS_STA) && WLAN_GET_CAP_INFO_ESS(pCurrBSS->wCapInfo))
162                                 ) {
163                                 pCurrBSS->bSelected = true;
164                                 return(pCurrBSS);
165                             }
166                         }
167                     } else {
168                         if ((pMgmt->eConfigMode == WMAC_CONFIG_AUTO) ||
169                             ((pMgmt->eConfigMode == WMAC_CONFIG_IBSS_STA) && WLAN_GET_CAP_INFO_IBSS(pCurrBSS->wCapInfo)) ||
170                             ((pMgmt->eConfigMode == WMAC_CONFIG_ESS_STA) && WLAN_GET_CAP_INFO_ESS(pCurrBSS->wCapInfo))
171                             ) {
172                             pCurrBSS->bSelected = true;
173                             return(pCurrBSS);
174                         }
175                     }
176                 }
177             }
178         }
179     } else {
180         // ignore BSSID
181         for (ii = 0; ii <MAX_BSS_NUM; ii++) {
182             pCurrBSS = &(pMgmt->sBSSList[ii]);
183
184            //2007-0721-01<Mark>by MikeLiu
185          //   if ((pCurrBSS->bActive) &&
186          //       (pCurrBSS->bSelected == false)) {
187
188           pCurrBSS->bSelected = false;
189           if (pCurrBSS->bActive) {
190
191                 if (pSSID != NULL) {
192                     // matched SSID
193                     if (memcmp(pSSID->abySSID,
194                         ((PWLAN_IE_SSID)pCurrBSS->abySSID)->abySSID,
195                         pSSID->len) ||
196                         (pSSID->len != ((PWLAN_IE_SSID)pCurrBSS->abySSID)->len)) {
197                         // SSID not match skip this BSS
198                         continue;
199                       }
200                 }
201                 if (((pMgmt->eConfigMode == WMAC_CONFIG_IBSS_STA) && WLAN_GET_CAP_INFO_ESS(pCurrBSS->wCapInfo)) ||
202                     ((pMgmt->eConfigMode == WMAC_CONFIG_ESS_STA) && WLAN_GET_CAP_INFO_IBSS(pCurrBSS->wCapInfo))
203                     ){
204                     // Type not match skip this BSS
205                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BSS type mismatch.... Config[%d] BSS[0x%04x]\n", pMgmt->eConfigMode, pCurrBSS->wCapInfo);
206                     continue;
207                 }
208
209                 if (ePhyType != PHY_TYPE_AUTO) {
210                     if (((ePhyType == PHY_TYPE_11A) && (PHY_TYPE_11A != pCurrBSS->eNetworkTypeInUse)) ||
211                         ((ePhyType != PHY_TYPE_11A) && (PHY_TYPE_11A == pCurrBSS->eNetworkTypeInUse))) {
212                         // PhyType not match skip this BSS
213                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Physical type mismatch.... ePhyType[%d] BSS[%d]\n", ePhyType, pCurrBSS->eNetworkTypeInUse);
214                         continue;
215                     }
216                 }
217
218         pMgmt->pSameBSS[jj].uChannel = pCurrBSS->uChannel;
219                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
220                         "BSSpSearchBSSList pSelect1[%pM]\n",
221                         pCurrBSS->abyBSSID);
222         jj++;
223
224
225                 if (pSelect == NULL) {
226                     pSelect = pCurrBSS;
227                 } else {
228                     // compare RSSI, select the strongest signal 
229                     if (pCurrBSS->uRSSI < pSelect->uRSSI) {
230                         pSelect = pCurrBSS;
231                     }
232                 }
233             }
234         }
235
236 pDevice->bSameBSSMaxNum = jj;
237
238         if (pSelect != NULL) {
239             pSelect->bSelected = true;
240                         if (pDevice->bRoaming == false)  {
241         //       Einsn Add @20070907
242                         memcpy(pbyDesireSSID,pCurrBSS->abySSID,WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1) ;
243                                                 }
244
245             return(pSelect);
246         }
247     }
248     return(NULL);
249
250 }
251
252
253 /*+
254  *
255  * Routine Description:
256  *    Clear BSS List
257  *
258  * Return Value:
259  *    None.
260  *
261 -*/
262
263
264 void BSSvClearBSSList(struct vnt_private *pDevice, int bKeepCurrBSSID)
265 {
266         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
267         int ii;
268
269     for (ii = 0; ii < MAX_BSS_NUM; ii++) {
270         if (bKeepCurrBSSID) {
271             if (pMgmt->sBSSList[ii].bActive &&
272                 !compare_ether_addr(pMgmt->sBSSList[ii].abyBSSID,
273                                     pMgmt->abyCurrBSSID)) {
274  //mike mark: there are two BSSID's in list. If that AP is in hidden ssid mode, one SSID is null,
275  //                 but other's might not be obvious, so if it associate's with your STA,
276  //                 you must keep the two of them!!
277                // bKeepCurrBSSID = false;
278                 continue;
279             }
280         }
281
282         pMgmt->sBSSList[ii].bActive = false;
283         memset(&pMgmt->sBSSList[ii], 0, sizeof(KnownBSS));
284     }
285     BSSvClearAnyBSSJoinRecord(pDevice);
286 }
287
288
289
290 /*+
291  *
292  * Routine Description:
293  *    search BSS list by BSSID & SSID if matched
294  *
295  * Return Value:
296  *    true if found.
297  *
298 -*/
299 PKnownBSS BSSpAddrIsInBSSList(struct vnt_private *pDevice,
300         u8 *abyBSSID, PWLAN_IE_SSID pSSID)
301 {
302         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
303         PKnownBSS pBSSList = NULL;
304         int ii;
305
306     for (ii = 0; ii < MAX_BSS_NUM; ii++) {
307         pBSSList = &(pMgmt->sBSSList[ii]);
308         if (pBSSList->bActive) {
309                 if (!compare_ether_addr(pBSSList->abyBSSID, abyBSSID)) {
310                 if (pSSID->len == ((PWLAN_IE_SSID)pBSSList->abySSID)->len){
311                     if (memcmp(pSSID->abySSID,
312                             ((PWLAN_IE_SSID)pBSSList->abySSID)->abySSID,
313                             pSSID->len) == 0)
314                         return pBSSList;
315                 }
316             }
317         }
318     }
319
320     return NULL;
321 };
322
323
324
325 /*+
326  *
327  * Routine Description:
328  *    Insert a BSS set into known BSS list
329  *
330  * Return Value:
331  *    true if success.
332  *
333 -*/
334
335 int BSSbInsertToBSSList(struct vnt_private *pDevice,
336                         u8 *abyBSSIDAddr,
337                         u64 qwTimestamp,
338                         u16 wBeaconInterval,
339                         u16 wCapInfo,
340                         u8 byCurrChannel,
341                         PWLAN_IE_SSID pSSID,
342                         PWLAN_IE_SUPP_RATES pSuppRates,
343                         PWLAN_IE_SUPP_RATES pExtSuppRates,
344                         PERPObject psERP,
345                         PWLAN_IE_RSN pRSN,
346                         PWLAN_IE_RSN_EXT pRSNWPA,
347                         PWLAN_IE_COUNTRY pIE_Country,
348                         PWLAN_IE_QUIET pIE_Quiet,
349                         u32 uIELength,
350                         u8 *pbyIEs,
351                         void *pRxPacketContext)
352 {
353         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
354         struct vnt_rx_mgmt *pRxPacket =
355                 (struct vnt_rx_mgmt *)pRxPacketContext;
356         PKnownBSS pBSSList = NULL;
357         unsigned int ii;
358         bool bParsingQuiet = false;
359
360
361     pBSSList = (PKnownBSS)&(pMgmt->sBSSList[0]);
362
363     for (ii = 0; ii < MAX_BSS_NUM; ii++) {
364         pBSSList = (PKnownBSS)&(pMgmt->sBSSList[ii]);
365         if (!pBSSList->bActive)
366                 break;
367     }
368
369     if (ii == MAX_BSS_NUM){
370         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Get free KnowBSS node failed.\n");
371         return false;
372     }
373     // save the BSS info
374     pBSSList->bActive = true;
375     memcpy( pBSSList->abyBSSID, abyBSSIDAddr, WLAN_BSSID_LEN);
376         pBSSList->qwBSSTimestamp = cpu_to_le64(qwTimestamp);
377     pBSSList->wBeaconInterval = cpu_to_le16(wBeaconInterval);
378     pBSSList->wCapInfo = cpu_to_le16(wCapInfo);
379     pBSSList->uClearCount = 0;
380
381     if (pSSID->len > WLAN_SSID_MAXLEN)
382         pSSID->len = WLAN_SSID_MAXLEN;
383     memcpy( pBSSList->abySSID, pSSID, pSSID->len + WLAN_IEHDR_LEN);
384
385     pBSSList->uChannel = byCurrChannel;
386
387     if (pSuppRates->len > WLAN_RATES_MAXLEN)
388         pSuppRates->len = WLAN_RATES_MAXLEN;
389     memcpy( pBSSList->abySuppRates, pSuppRates, pSuppRates->len + WLAN_IEHDR_LEN);
390
391     if (pExtSuppRates != NULL) {
392         if (pExtSuppRates->len > WLAN_RATES_MAXLEN)
393             pExtSuppRates->len = WLAN_RATES_MAXLEN;
394         memcpy(pBSSList->abyExtSuppRates, pExtSuppRates, pExtSuppRates->len + WLAN_IEHDR_LEN);
395         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BSSbInsertToBSSList: pExtSuppRates->len = %d\n", pExtSuppRates->len);
396
397     } else {
398         memset(pBSSList->abyExtSuppRates, 0, WLAN_IEHDR_LEN + WLAN_RATES_MAXLEN + 1);
399     }
400     pBSSList->sERP.byERP = psERP->byERP;
401     pBSSList->sERP.bERPExist = psERP->bERPExist;
402
403     // Check if BSS is 802.11a/b/g
404     if (pBSSList->uChannel > CB_MAX_CHANNEL_24G) {
405         pBSSList->eNetworkTypeInUse = PHY_TYPE_11A;
406     } else {
407         if (pBSSList->sERP.bERPExist == true) {
408             pBSSList->eNetworkTypeInUse = PHY_TYPE_11G;
409         } else {
410             pBSSList->eNetworkTypeInUse = PHY_TYPE_11B;
411         }
412     }
413
414     pBSSList->byRxRate = pRxPacket->byRxRate;
415     pBSSList->qwLocalTSF = pRxPacket->qwLocalTSF;
416     pBSSList->uRSSI = pRxPacket->uRSSI;
417     pBSSList->bySQ = pRxPacket->bySQ;
418
419    if ((pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
420         (pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
421         // assoc with BSS
422         if (pBSSList == pMgmt->pCurrBSS) {
423             bParsingQuiet = true;
424         }
425     }
426
427     WPA_ClearRSN(pBSSList);
428
429     if (pRSNWPA != NULL) {
430         unsigned int uLen = pRSNWPA->len + 2;
431
432         if (uLen <= (uIELength -
433                      (unsigned int) (ULONG_PTR) ((PBYTE) pRSNWPA - pbyIEs))) {
434                 pBSSList->wWPALen = uLen;
435                 memcpy(pBSSList->byWPAIE, pRSNWPA, uLen);
436                 WPA_ParseRSN(pBSSList, pRSNWPA);
437         }
438     }
439
440     WPA2_ClearRSN(pBSSList);
441
442     if (pRSN != NULL) {
443         unsigned int uLen = pRSN->len + 2;
444
445         if (uLen <= (uIELength -
446                      (unsigned int) (ULONG_PTR) ((PBYTE) pRSN - pbyIEs))) {
447                 pBSSList->wRSNLen = uLen;
448                 memcpy(pBSSList->byRSNIE, pRSN, uLen);
449                 WPA2vParseRSN(pBSSList, pRSN);
450         }
451     }
452
453     if ((pMgmt->eAuthenMode == WMAC_AUTH_WPA2) || (pBSSList->bWPA2Valid == true)) {
454
455         PSKeyItem  pTransmitKey = NULL;
456         bool       bIs802_1x = false;
457
458         for (ii = 0; ii < pBSSList->wAKMSSAuthCount; ii ++) {
459             if (pBSSList->abyAKMSSAuthType[ii] == WLAN_11i_AKMSS_802_1X) {
460                 bIs802_1x = true;
461                 break;
462             }
463         }
464         if ((bIs802_1x == true) && (pSSID->len == ((PWLAN_IE_SSID)pMgmt->abyDesireSSID)->len) &&
465             ( !memcmp(pSSID->abySSID, ((PWLAN_IE_SSID)pMgmt->abyDesireSSID)->abySSID, pSSID->len))) {
466
467                 bAdd_PMKID_Candidate((void *) pDevice,
468                                      pBSSList->abyBSSID,
469                                      &pBSSList->sRSNCapObj);
470
471             if ((pDevice->bLinkPass == true) && (pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
472                 if ((KeybGetTransmitKey(&(pDevice->sKey), pDevice->abyBSSID, PAIRWISE_KEY, &pTransmitKey) == true) ||
473                     (KeybGetTransmitKey(&(pDevice->sKey), pDevice->abyBSSID, GROUP_KEY, &pTransmitKey) == true)) {
474                     pDevice->gsPMKIDCandidate.StatusType = Ndis802_11StatusType_PMKID_CandidateList;
475                     pDevice->gsPMKIDCandidate.Version = 1;
476
477                 }
478
479             }
480         }
481     }
482
483     if (pDevice->bUpdateBBVGA) {
484         // Monitor if RSSI is too strong.
485         pBSSList->byRSSIStatCnt = 0;
486         RFvRSSITodBm(pDevice, (BYTE)(pRxPacket->uRSSI), &pBSSList->ldBmMAX);
487         pBSSList->ldBmAverage[0] = pBSSList->ldBmMAX;
488         pBSSList->ldBmAverRange = pBSSList->ldBmMAX;
489         for (ii = 1; ii < RSSI_STAT_COUNT; ii++)
490             pBSSList->ldBmAverage[ii] = 0;
491     }
492
493     pBSSList->uIELength = uIELength;
494     if (pBSSList->uIELength > WLAN_BEACON_FR_MAXLEN)
495         pBSSList->uIELength = WLAN_BEACON_FR_MAXLEN;
496     memcpy(pBSSList->abyIEs, pbyIEs, pBSSList->uIELength);
497
498     return true;
499 }
500
501
502 /*+
503  *
504  * Routine Description:
505  *    Update BSS set in known BSS list
506  *
507  * Return Value:
508  *    true if success.
509  *
510 -*/
511 // TODO: input structure modify
512
513 int BSSbUpdateToBSSList(struct vnt_private *pDevice,
514                         u64 qwTimestamp,
515                         u16 wBeaconInterval,
516                         u16 wCapInfo,
517                         u8 byCurrChannel,
518                         int bChannelHit,
519                         PWLAN_IE_SSID pSSID,
520                         PWLAN_IE_SUPP_RATES pSuppRates,
521                         PWLAN_IE_SUPP_RATES pExtSuppRates,
522                         PERPObject psERP,
523                         PWLAN_IE_RSN pRSN,
524                         PWLAN_IE_RSN_EXT pRSNWPA,
525                         PWLAN_IE_COUNTRY pIE_Country,
526                         PWLAN_IE_QUIET pIE_Quiet,
527                         PKnownBSS pBSSList,
528                         u32 uIELength,
529                         u8 *pbyIEs,
530                         void *pRxPacketContext)
531 {
532         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
533         struct vnt_rx_mgmt *pRxPacket =
534                 (struct vnt_rx_mgmt *)pRxPacketContext;
535         int ii, jj;
536         signed long ldBm, ldBmSum;
537         bool bParsingQuiet = false;
538
539     if (pBSSList == NULL)
540         return false;
541
542
543         pBSSList->qwBSSTimestamp = cpu_to_le64(qwTimestamp);
544
545     pBSSList->wBeaconInterval = cpu_to_le16(wBeaconInterval);
546     pBSSList->wCapInfo = cpu_to_le16(wCapInfo);
547     pBSSList->uClearCount = 0;
548     pBSSList->uChannel = byCurrChannel;
549
550     if (pSSID->len > WLAN_SSID_MAXLEN)
551         pSSID->len = WLAN_SSID_MAXLEN;
552
553     if ((pSSID->len != 0) && (pSSID->abySSID[0] != 0))
554         memcpy(pBSSList->abySSID, pSSID, pSSID->len + WLAN_IEHDR_LEN);
555     memcpy(pBSSList->abySuppRates, pSuppRates,pSuppRates->len + WLAN_IEHDR_LEN);
556
557     if (pExtSuppRates != NULL) {
558         memcpy(pBSSList->abyExtSuppRates, pExtSuppRates,pExtSuppRates->len + WLAN_IEHDR_LEN);
559     } else {
560         memset(pBSSList->abyExtSuppRates, 0, WLAN_IEHDR_LEN + WLAN_RATES_MAXLEN + 1);
561     }
562     pBSSList->sERP.byERP = psERP->byERP;
563     pBSSList->sERP.bERPExist = psERP->bERPExist;
564
565     // Check if BSS is 802.11a/b/g
566     if (pBSSList->uChannel > CB_MAX_CHANNEL_24G) {
567         pBSSList->eNetworkTypeInUse = PHY_TYPE_11A;
568     } else {
569         if (pBSSList->sERP.bERPExist == true) {
570             pBSSList->eNetworkTypeInUse = PHY_TYPE_11G;
571         } else {
572             pBSSList->eNetworkTypeInUse = PHY_TYPE_11B;
573         }
574     }
575
576     pBSSList->byRxRate = pRxPacket->byRxRate;
577     pBSSList->qwLocalTSF = pRxPacket->qwLocalTSF;
578     if(bChannelHit)
579         pBSSList->uRSSI = pRxPacket->uRSSI;
580     pBSSList->bySQ = pRxPacket->bySQ;
581
582    if ((pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
583         (pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
584         // assoc with BSS
585         if (pBSSList == pMgmt->pCurrBSS) {
586             bParsingQuiet = true;
587         }
588     }
589
590    WPA_ClearRSN(pBSSList);         //mike update
591
592    if (pRSNWPA != NULL) {
593         unsigned int uLen = pRSNWPA->len + 2;
594         if (uLen <= (uIELength -
595                      (unsigned int) (ULONG_PTR) ((PBYTE) pRSNWPA - pbyIEs))) {
596                 pBSSList->wWPALen = uLen;
597                 memcpy(pBSSList->byWPAIE, pRSNWPA, uLen);
598                 WPA_ParseRSN(pBSSList, pRSNWPA);
599         }
600    }
601
602    WPA2_ClearRSN(pBSSList);  //mike update
603
604     if (pRSN != NULL) {
605         unsigned int uLen = pRSN->len + 2;
606         if (uLen <= (uIELength -
607                         (unsigned int) (ULONG_PTR) ((PBYTE) pRSN - pbyIEs))) {
608                 pBSSList->wRSNLen = uLen;
609                 memcpy(pBSSList->byRSNIE, pRSN, uLen);
610                 WPA2vParseRSN(pBSSList, pRSN);
611         }
612     }
613
614     if (pRxPacket->uRSSI != 0) {
615         RFvRSSITodBm(pDevice, (BYTE)(pRxPacket->uRSSI), &ldBm);
616         // Monitor if RSSI is too strong.
617         pBSSList->byRSSIStatCnt++;
618         pBSSList->byRSSIStatCnt %= RSSI_STAT_COUNT;
619         pBSSList->ldBmAverage[pBSSList->byRSSIStatCnt] = ldBm;
620         ldBmSum = 0;
621         for (ii = 0, jj = 0; ii < RSSI_STAT_COUNT; ii++) {
622                 if (pBSSList->ldBmAverage[ii] != 0) {
623                         pBSSList->ldBmMAX =
624                                 max(pBSSList->ldBmAverage[ii], ldBm);
625                         ldBmSum +=
626                                 pBSSList->ldBmAverage[ii];
627                         jj++;
628                 }
629         }
630         pBSSList->ldBmAverRange = ldBmSum /jj;
631     }
632
633     pBSSList->uIELength = uIELength;
634     if (pBSSList->uIELength > WLAN_BEACON_FR_MAXLEN)
635         pBSSList->uIELength = WLAN_BEACON_FR_MAXLEN;
636     memcpy(pBSSList->abyIEs, pbyIEs, pBSSList->uIELength);
637
638     return true;
639 }
640
641
642
643
644
645 /*+
646  *
647  * Routine Description:
648  *    Search Node DB table to find the index of matched DstAddr
649  *
650  * Return Value:
651  *    None
652  *
653 -*/
654
655 int BSSbIsSTAInNodeDB(struct vnt_private *pDevice,
656                 u8 *abyDstAddr, u32 *puNodeIndex)
657 {
658         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
659         unsigned int ii;
660
661     // Index = 0 reserved for AP Node
662     for (ii = 1; ii < (MAX_NODE_NUM + 1); ii++) {
663         if (pMgmt->sNodeDBTable[ii].bActive) {
664                 if (!compare_ether_addr(abyDstAddr,
665                                         pMgmt->sNodeDBTable[ii].abyMACAddr)) {
666                 *puNodeIndex = ii;
667                 return true;
668             }
669         }
670     }
671
672    return false;
673 };
674
675
676
677 /*+
678  *
679  * Routine Description:
680  *    Find an empty node and allocate it; if no empty node
681  *    is found, then use the most inactive one.
682  *
683  * Return Value:
684  *    None
685  *
686 -*/
687 void BSSvCreateOneNode(struct vnt_private *pDevice, u32 *puNodeIndex)
688 {
689         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
690         int            ii;
691         u32 BigestCount = 0;
692         u32 SelectIndex;
693         struct sk_buff  *skb;
694
695     // Index = 0 reserved for AP Node (In STA mode)
696     // Index = 0 reserved for Broadcast/MultiCast (In AP mode)
697     SelectIndex = 1;
698     for (ii = 1; ii < (MAX_NODE_NUM + 1); ii++) {
699         if (pMgmt->sNodeDBTable[ii].bActive) {
700             if (pMgmt->sNodeDBTable[ii].uInActiveCount > BigestCount) {
701                 BigestCount = pMgmt->sNodeDBTable[ii].uInActiveCount;
702                 SelectIndex = ii;
703             }
704         }
705         else {
706             break;
707         }
708     }
709
710     // if not found replace uInActiveCount with the largest one.
711     if ( ii == (MAX_NODE_NUM + 1)) {
712         *puNodeIndex = SelectIndex;
713         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Replace inactive node = %d\n", SelectIndex);
714         // clear ps buffer
715         if (pMgmt->sNodeDBTable[*puNodeIndex].sTxPSQueue.next != NULL) {
716             while ((skb = skb_dequeue(&pMgmt->sNodeDBTable[*puNodeIndex].sTxPSQueue)) != NULL)
717             dev_kfree_skb(skb);
718         }
719     }
720     else {
721         *puNodeIndex = ii;
722     }
723
724     memset(&pMgmt->sNodeDBTable[*puNodeIndex], 0, sizeof(KnownNodeDB));
725     pMgmt->sNodeDBTable[*puNodeIndex].bActive = true;
726     pMgmt->sNodeDBTable[*puNodeIndex].uRatePollTimeout = FALLBACK_POLL_SECOND;
727     // for AP mode PS queue
728     skb_queue_head_init(&pMgmt->sNodeDBTable[*puNodeIndex].sTxPSQueue);
729     pMgmt->sNodeDBTable[*puNodeIndex].byAuthSequence = 0;
730     pMgmt->sNodeDBTable[*puNodeIndex].wEnQueueCnt = 0;
731     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Create node index = %d\n", ii);
732 };
733
734
735
736 /*+
737  *
738  * Routine Description:
739  *    Remove Node by NodeIndex
740  *
741  *
742  * Return Value:
743  *    None
744  *
745 -*/
746
747 void BSSvRemoveOneNode(struct vnt_private *pDevice, u32 uNodeIndex)
748 {
749         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
750         u8 byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
751         struct sk_buff  *skb;
752
753
754     while ((skb = skb_dequeue(&pMgmt->sNodeDBTable[uNodeIndex].sTxPSQueue)) != NULL)
755             dev_kfree_skb(skb);
756     // clear context
757     memset(&pMgmt->sNodeDBTable[uNodeIndex], 0, sizeof(KnownNodeDB));
758     // clear tx bit map
759     pMgmt->abyPSTxMap[pMgmt->sNodeDBTable[uNodeIndex].wAID >> 3] &=  ~byMask[pMgmt->sNodeDBTable[uNodeIndex].wAID & 7];
760 };
761 /*+
762  *
763  * Routine Description:
764  *    Update AP Node content in Index 0 of KnownNodeDB
765  *
766  *
767  * Return Value:
768  *    None
769  *
770 -*/
771
772 void BSSvUpdateAPNode(struct vnt_private *pDevice, u16 *pwCapInfo,
773         PWLAN_IE_SUPP_RATES pSuppRates, PWLAN_IE_SUPP_RATES pExtSuppRates)
774 {
775         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
776         u32 uRateLen = WLAN_RATES_MAXLEN;
777
778     memset(&pMgmt->sNodeDBTable[0], 0, sizeof(KnownNodeDB));
779
780     pMgmt->sNodeDBTable[0].bActive = true;
781     if (pDevice->byBBType == BB_TYPE_11B) {
782         uRateLen = WLAN_RATES_MAXLEN_11B;
783     }
784     pMgmt->abyCurrSuppRates[1] = RATEuSetIE((PWLAN_IE_SUPP_RATES)pSuppRates,
785                                             (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
786                                             uRateLen);
787     pMgmt->abyCurrExtSuppRates[1] = RATEuSetIE((PWLAN_IE_SUPP_RATES)pExtSuppRates,
788                                             (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates,
789                                             uRateLen);
790     RATEvParseMaxRate((void *) pDevice,
791                        (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
792                        (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates,
793                        true,
794                        &(pMgmt->sNodeDBTable[0].wMaxBasicRate),
795                        &(pMgmt->sNodeDBTable[0].wMaxSuppRate),
796                        &(pMgmt->sNodeDBTable[0].wSuppRate),
797                        &(pMgmt->sNodeDBTable[0].byTopCCKBasicRate),
798                        &(pMgmt->sNodeDBTable[0].byTopOFDMBasicRate)
799                       );
800     memcpy(pMgmt->sNodeDBTable[0].abyMACAddr, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
801     pMgmt->sNodeDBTable[0].wTxDataRate = pMgmt->sNodeDBTable[0].wMaxSuppRate;
802     pMgmt->sNodeDBTable[0].bShortPreamble = WLAN_GET_CAP_INFO_SHORTPREAMBLE(*pwCapInfo);
803     pMgmt->sNodeDBTable[0].uRatePollTimeout = FALLBACK_POLL_SECOND;
804     // Auto rate fallback function initiation.
805     // RATEbInit(pDevice);
806     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pMgmt->sNodeDBTable[0].wTxDataRate = %d \n", pMgmt->sNodeDBTable[0].wTxDataRate);
807
808 };
809
810 /*+
811  *
812  * Routine Description:
813  *    Add Multicast Node content in Index 0 of KnownNodeDB
814  *
815  *
816  * Return Value:
817  *    None
818  *
819 -*/
820
821 void BSSvAddMulticastNode(struct vnt_private *pDevice)
822 {
823         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
824
825     if (!pDevice->bEnableHostWEP)
826         memset(&pMgmt->sNodeDBTable[0], 0, sizeof(KnownNodeDB));
827     memset(pMgmt->sNodeDBTable[0].abyMACAddr, 0xff, WLAN_ADDR_LEN);
828     pMgmt->sNodeDBTable[0].bActive = true;
829     pMgmt->sNodeDBTable[0].bPSEnable = false;
830     skb_queue_head_init(&pMgmt->sNodeDBTable[0].sTxPSQueue);
831     RATEvParseMaxRate((void *) pDevice,
832                       (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
833                       (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates,
834                       true,
835                       &(pMgmt->sNodeDBTable[0].wMaxBasicRate),
836                       &(pMgmt->sNodeDBTable[0].wMaxSuppRate),
837                        &(pMgmt->sNodeDBTable[0].wSuppRate),
838                       &(pMgmt->sNodeDBTable[0].byTopCCKBasicRate),
839                       &(pMgmt->sNodeDBTable[0].byTopOFDMBasicRate)
840                      );
841     pMgmt->sNodeDBTable[0].wTxDataRate = pMgmt->sNodeDBTable[0].wMaxBasicRate;
842     pMgmt->sNodeDBTable[0].uRatePollTimeout = FALLBACK_POLL_SECOND;
843
844 };
845
846 /*+
847  *
848  * Routine Description:
849  *
850  *
851  *  Second call back function to update Node DB info & AP link status
852  *
853  *
854  * Return Value:
855  *    none.
856  *
857 -*/
858
859 void BSSvSecondCallBack(struct vnt_private *pDevice)
860 {
861         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
862         int ii;
863         PWLAN_IE_SSID pItemSSID, pCurrSSID;
864         u32 uSleepySTACnt = 0;
865         u32 uNonShortSlotSTACnt = 0;
866         u32 uLongPreambleSTACnt = 0;
867
868     spin_lock_irq(&pDevice->lock);
869
870     pDevice->uAssocCount = 0;
871
872     //Power Saving Mode Tx Burst
873     if ( pDevice->bEnablePSMode == true ) {
874         pDevice->ulPSModeWaitTx++;
875         if ( pDevice->ulPSModeWaitTx >= 2 ) {
876             pDevice->ulPSModeWaitTx = 0;
877             pDevice->bPSModeTxBurst = false;
878         }
879     }
880
881     pDevice->byERPFlag &=
882         ~(WLAN_SET_ERP_BARKER_MODE(1) | WLAN_SET_ERP_NONERP_PRESENT(1));
883
884     if (pDevice->wUseProtectCntDown > 0) {
885         pDevice->wUseProtectCntDown --;
886     }
887     else {
888         // disable protect mode
889         pDevice->byERPFlag &= ~(WLAN_SET_ERP_USE_PROTECTION(1));
890     }
891
892 if(pDevice->byReAssocCount > 0) {
893        pDevice->byReAssocCount++;
894    if((pDevice->byReAssocCount > 10) && (pDevice->bLinkPass != true)) {  //10 sec timeout
895                      printk("Re-association timeout!!!\n");
896                    pDevice->byReAssocCount = 0;
897                     // if(pDevice->bWPASuppWextEnabled == true)
898                         {
899                         union iwreq_data  wrqu;
900                         memset(&wrqu, 0, sizeof (wrqu));
901                           wrqu.ap_addr.sa_family = ARPHRD_ETHER;
902                         PRINT_K("wireless_send_event--->SIOCGIWAP(disassociated)\n");
903                         wireless_send_event(pDevice->dev, SIOCGIWAP, &wrqu, NULL);
904                        }
905      }
906    else if(pDevice->bLinkPass == true)
907         pDevice->byReAssocCount = 0;
908 }
909
910  pMgmt->eLastState = pMgmt->eCurrState ;
911
912         s_uCalculateLinkQual(pDevice);
913
914     for (ii = 0; ii < (MAX_NODE_NUM + 1); ii++) {
915
916         if (pMgmt->sNodeDBTable[ii].bActive) {
917             // Increase in-activity counter
918             pMgmt->sNodeDBTable[ii].uInActiveCount++;
919
920             if (ii > 0) {
921                 if (pMgmt->sNodeDBTable[ii].uInActiveCount > MAX_INACTIVE_COUNT) {
922                     BSSvRemoveOneNode(pDevice, ii);
923                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
924                         "Inactive timeout [%d] sec, STA index = [%d] remove\n", MAX_INACTIVE_COUNT, ii);
925                     continue;
926                 }
927
928                 if (pMgmt->sNodeDBTable[ii].eNodeState >= NODE_ASSOC) {
929
930                     pDevice->uAssocCount++;
931
932                     // check if Non ERP exist
933                     if (pMgmt->sNodeDBTable[ii].uInActiveCount < ERP_RECOVER_COUNT) {
934                         if (!pMgmt->sNodeDBTable[ii].bShortPreamble) {
935                             pDevice->byERPFlag |= WLAN_SET_ERP_BARKER_MODE(1);
936                             uLongPreambleSTACnt ++;
937                         }
938                         if (!pMgmt->sNodeDBTable[ii].bERPExist) {
939                             pDevice->byERPFlag |= WLAN_SET_ERP_NONERP_PRESENT(1);
940                             pDevice->byERPFlag |= WLAN_SET_ERP_USE_PROTECTION(1);
941                         }
942                         if (!pMgmt->sNodeDBTable[ii].bShortSlotTime)
943                             uNonShortSlotSTACnt++;
944                     }
945                 }
946
947                 // check if any STA in PS mode
948                 if (pMgmt->sNodeDBTable[ii].bPSEnable)
949                     uSleepySTACnt++;
950
951
952             }
953
954             // Rate fallback check
955             if (!pDevice->bFixRate) {
956                 if (ii > 0) {
957                     // ii = 0 for multicast node (AP & Adhoc)
958                         RATEvTxRateFallBack((void *)pDevice,
959                                             &(pMgmt->sNodeDBTable[ii]));
960                 }
961                 else {
962                     // ii = 0 reserved for unicast AP node (Infra STA)
963                         if (pMgmt->eCurrMode == WMAC_MODE_ESS_STA)
964                                 RATEvTxRateFallBack((void *)pDevice,
965                                                     &(pMgmt->sNodeDBTable[ii]));
966                 }
967
968             }
969
970             // check if pending PS queue
971             if (pMgmt->sNodeDBTable[ii].wEnQueueCnt != 0) {
972                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Index= %d, Queue = %d pending \n",
973                            ii, pMgmt->sNodeDBTable[ii].wEnQueueCnt);
974                 if ((ii >0) && (pMgmt->sNodeDBTable[ii].wEnQueueCnt > 15)) {
975                     BSSvRemoveOneNode(pDevice, ii);
976                     DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Pending many queues PS STA Index = %d remove \n", ii);
977                     continue;
978                 }
979             }
980         }
981
982     }
983
984
985     if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) && (pDevice->byBBType == BB_TYPE_11G)) {
986
987         // on/off protect mode
988         if (WLAN_GET_ERP_USE_PROTECTION(pDevice->byERPFlag)) {
989             if (!pDevice->bProtectMode) {
990                 MACvEnableProtectMD(pDevice);
991                 pDevice->bProtectMode = true;
992             }
993         }
994         else {
995             if (pDevice->bProtectMode) {
996                 MACvDisableProtectMD(pDevice);
997                 pDevice->bProtectMode = false;
998             }
999         }
1000         // on/off short slot time
1001
1002         if (uNonShortSlotSTACnt > 0) {
1003             if (pDevice->bShortSlotTime) {
1004                 pDevice->bShortSlotTime = false;
1005                 BBvSetShortSlotTime(pDevice);
1006                 vUpdateIFS((void *)pDevice);
1007             }
1008         }
1009         else {
1010             if (!pDevice->bShortSlotTime) {
1011                 pDevice->bShortSlotTime = true;
1012                 BBvSetShortSlotTime(pDevice);
1013                 vUpdateIFS((void *)pDevice);
1014             }
1015         }
1016
1017         // on/off barker long preamble mode
1018
1019         if (uLongPreambleSTACnt > 0) {
1020             if (!pDevice->bBarkerPreambleMd) {
1021                 MACvEnableBarkerPreambleMd(pDevice);
1022                 pDevice->bBarkerPreambleMd = true;
1023             }
1024         }
1025         else {
1026             if (pDevice->bBarkerPreambleMd) {
1027                 MACvDisableBarkerPreambleMd(pDevice);
1028                 pDevice->bBarkerPreambleMd = false;
1029             }
1030         }
1031
1032     }
1033
1034
1035     // Check if any STA in PS mode, enable DTIM multicast deliver
1036     if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
1037         if (uSleepySTACnt > 0)
1038             pMgmt->sNodeDBTable[0].bPSEnable = true;
1039         else
1040             pMgmt->sNodeDBTable[0].bPSEnable = false;
1041     }
1042
1043     pItemSSID = (PWLAN_IE_SSID)pMgmt->abyDesireSSID;
1044     pCurrSSID = (PWLAN_IE_SSID)pMgmt->abyCurrSSID;
1045
1046     if ((pMgmt->eCurrMode == WMAC_MODE_STANDBY) ||
1047         (pMgmt->eCurrMode == WMAC_MODE_ESS_STA)) {
1048
1049         if (pMgmt->sNodeDBTable[0].bActive) { // Assoc with BSS
1050
1051             if (pDevice->bUpdateBBVGA) {
1052                 s_vCheckSensitivity(pDevice);
1053                 s_vCheckPreEDThreshold(pDevice);
1054             }
1055
1056             if ((pMgmt->sNodeDBTable[0].uInActiveCount >= (LOST_BEACON_COUNT/2)) &&
1057                 (pDevice->byBBVGACurrent != pDevice->abyBBVGA[0]) ) {
1058                 pDevice->byBBVGANew = pDevice->abyBBVGA[0];
1059                 bScheduleCommand((void *) pDevice,
1060                                  WLAN_CMD_CHANGE_BBSENSITIVITY,
1061                                  NULL);
1062             }
1063
1064                 if (pMgmt->sNodeDBTable[0].uInActiveCount >= LOST_BEACON_COUNT) {
1065                 pMgmt->sNodeDBTable[0].bActive = false;
1066                 pMgmt->eCurrMode = WMAC_MODE_STANDBY;
1067                 pMgmt->eCurrState = WMAC_STATE_IDLE;
1068                 netif_stop_queue(pDevice->dev);
1069                 pDevice->bLinkPass = false;
1070                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
1071                 pDevice->bRoaming = true;
1072                 pDevice->bIsRoaming = false;
1073
1074                 DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Lost AP beacon [%d] sec, disconnected !\n", pMgmt->sNodeDBTable[0].uInActiveCount);
1075                 /* let wpa supplicant know AP may disconnect */
1076       {
1077         union iwreq_data  wrqu;
1078         memset(&wrqu, 0, sizeof (wrqu));
1079         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1080         PRINT_K("wireless_send_event--->SIOCGIWAP(disassociated)\n");
1081         wireless_send_event(pDevice->dev, SIOCGIWAP, &wrqu, NULL);
1082      }
1083             }
1084         }
1085         else if (pItemSSID->len != 0) {
1086 //Davidwang
1087       if ((pDevice->bEnableRoaming == true)&&(!(pMgmt->Cisco_cckm))) {
1088 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "bRoaming %d, !\n", pDevice->bRoaming );
1089 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "bIsRoaming %d, !\n", pDevice->bIsRoaming );
1090           if ((pDevice->bRoaming == true)&&(pDevice->bIsRoaming == true)){
1091                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Fast   Roaming ...\n");
1092                 BSSvClearBSSList((void *) pDevice, pDevice->bLinkPass);
1093                 bScheduleCommand((void *) pDevice,
1094                                  WLAN_CMD_BSSID_SCAN,
1095                                  pMgmt->abyDesireSSID);
1096                 bScheduleCommand((void *) pDevice,
1097                                  WLAN_CMD_SSID,
1098                                  pMgmt->abyDesireSSID);
1099                 pDevice->uAutoReConnectTime = 0;
1100                 pDevice->uIsroamingTime = 0;
1101                 pDevice->bRoaming = false;
1102           }
1103       else if ((pDevice->bRoaming == false)&&(pDevice->bIsRoaming == true)) {
1104                             pDevice->uIsroamingTime++;
1105        if (pDevice->uIsroamingTime >= 20)
1106             pDevice->bIsRoaming = false;
1107          }
1108
1109    }
1110 else {
1111             if (pDevice->uAutoReConnectTime < 10) {
1112                 pDevice->uAutoReConnectTime++;
1113                 //network manager support need not do Roaming scan???
1114                 if(pDevice->bWPASuppWextEnabled ==true)
1115                  pDevice->uAutoReConnectTime = 0;
1116             }
1117             else {
1118             //mike use old encryption status for wpa reauthen
1119               if(pDevice->bWPADEVUp)
1120                   pDevice->eEncryptionStatus = pDevice->eOldEncryptionStatus;
1121
1122                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Roaming ...\n");
1123                 BSSvClearBSSList((void *) pDevice, pDevice->bLinkPass);
1124                 pMgmt->eScanType = WMAC_SCAN_ACTIVE;
1125                 bScheduleCommand((void *) pDevice,
1126                                  WLAN_CMD_BSSID_SCAN,
1127                                  pMgmt->abyDesireSSID);
1128                 bScheduleCommand((void *) pDevice,
1129                                  WLAN_CMD_SSID,
1130                                  pMgmt->abyDesireSSID);
1131                 pDevice->uAutoReConnectTime = 0;
1132             }
1133         }
1134     }
1135     }
1136
1137     if (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
1138         // if adhoc started which essid is NULL string, rescanning.
1139         if ((pMgmt->eCurrState == WMAC_STATE_STARTED) && (pCurrSSID->len == 0)) {
1140             if (pDevice->uAutoReConnectTime < 10) {
1141                 pDevice->uAutoReConnectTime++;
1142             }
1143             else {
1144                 DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Adhoc re-scanning ...\n");
1145                pMgmt->eScanType = WMAC_SCAN_ACTIVE;
1146                 bScheduleCommand((void *) pDevice, WLAN_CMD_BSSID_SCAN, NULL);
1147                 bScheduleCommand((void *) pDevice, WLAN_CMD_SSID, NULL);
1148                 pDevice->uAutoReConnectTime = 0;
1149             };
1150         }
1151         if (pMgmt->eCurrState == WMAC_STATE_JOINTED) {
1152
1153                 if (pDevice->bUpdateBBVGA) {
1154                         s_vCheckSensitivity(pDevice);
1155                         s_vCheckPreEDThreshold(pDevice);
1156                 }
1157                 if (pMgmt->sNodeDBTable[0].uInActiveCount >=ADHOC_LOST_BEACON_COUNT) {
1158                     DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Lost other STA beacon [%d] sec, started !\n", pMgmt->sNodeDBTable[0].uInActiveCount);
1159                 pMgmt->sNodeDBTable[0].uInActiveCount = 0;
1160                 pMgmt->eCurrState = WMAC_STATE_STARTED;
1161                 netif_stop_queue(pDevice->dev);
1162                 pDevice->bLinkPass = false;
1163                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
1164             }
1165         }
1166     }
1167
1168     if (pDevice->bLinkPass == true) {
1169         if (netif_queue_stopped(pDevice->dev))
1170             netif_wake_queue(pDevice->dev);
1171     }
1172
1173     spin_unlock_irq(&pDevice->lock);
1174
1175     pMgmt->sTimerSecondCallback.expires = RUN_AT(HZ);
1176     add_timer(&pMgmt->sTimerSecondCallback);
1177 }
1178
1179 /*+
1180  *
1181  * Routine Description:
1182  *
1183  *
1184  *  Update Tx attemps, Tx failure counter in Node DB
1185  *
1186  *
1187  * Return Value:
1188  *    none.
1189  *
1190 -*/
1191
1192 void BSSvUpdateNodeTxCounter(struct vnt_private *pDevice,
1193         PSStatCounter pStatistic, u8 byTSR, u8 byPktNO)
1194 {
1195         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
1196         u32 uNodeIndex = 0;
1197         u8 byTxRetry;
1198         u16 wRate;
1199         u16 wFallBackRate = RATE_1M;
1200         u8 byFallBack;
1201         int ii;
1202         u8 *pbyDestAddr;
1203         u8 byPktNum;
1204         u16 wFIFOCtl;
1205
1206     byPktNum = (byPktNO & 0x0F) >> 4;
1207     byTxRetry = (byTSR & 0xF0) >> 4;
1208     wRate = (WORD) (byPktNO & 0xF0) >> 4;
1209     wFIFOCtl = pStatistic->abyTxPktInfo[byPktNum].wFIFOCtl;
1210     pbyDestAddr = (PBYTE) &( pStatistic->abyTxPktInfo[byPktNum].abyDestAddr[0]);
1211
1212     if (wFIFOCtl & FIFOCTL_AUTO_FB_0) {
1213         byFallBack = AUTO_FB_0;
1214     } else if (wFIFOCtl & FIFOCTL_AUTO_FB_1) {
1215         byFallBack = AUTO_FB_1;
1216     } else {
1217         byFallBack = AUTO_FB_NONE;
1218     }
1219
1220     // Only Unicast using support rates
1221     if (wFIFOCtl & FIFOCTL_NEEDACK) {
1222         if (pMgmt->eCurrMode == WMAC_MODE_ESS_STA) {
1223             pMgmt->sNodeDBTable[0].uTxAttempts += 1;
1224             if ( !(byTSR & (TSR_TMO | TSR_RETRYTMO))) {
1225                 // transmit success, TxAttempts at least plus one
1226                 pMgmt->sNodeDBTable[0].uTxOk[MAX_RATE]++;
1227                 if ( (byFallBack == AUTO_FB_NONE) ||
1228                      (wRate < RATE_18M) ) {
1229                     wFallBackRate = wRate;
1230                 } else if (byFallBack == AUTO_FB_0) {
1231                     if (byTxRetry < 5)
1232                         wFallBackRate = awHWRetry0[wRate-RATE_18M][byTxRetry];
1233                     else
1234                         wFallBackRate = awHWRetry0[wRate-RATE_18M][4];
1235                 } else if (byFallBack == AUTO_FB_1) {
1236                     if (byTxRetry < 5)
1237                         wFallBackRate = awHWRetry1[wRate-RATE_18M][byTxRetry];
1238                     else
1239                         wFallBackRate = awHWRetry1[wRate-RATE_18M][4];
1240                 }
1241                 pMgmt->sNodeDBTable[0].uTxOk[wFallBackRate]++;
1242             } else {
1243                 pMgmt->sNodeDBTable[0].uTxFailures ++;
1244             }
1245             pMgmt->sNodeDBTable[0].uTxRetry += byTxRetry;
1246             if (byTxRetry != 0) {
1247                 pMgmt->sNodeDBTable[0].uTxFail[MAX_RATE]+=byTxRetry;
1248                 if ( (byFallBack == AUTO_FB_NONE) ||
1249                      (wRate < RATE_18M) ) {
1250                     pMgmt->sNodeDBTable[0].uTxFail[wRate]+=byTxRetry;
1251                 } else if (byFallBack == AUTO_FB_0) {
1252                         for (ii = 0; ii < byTxRetry; ii++) {
1253                                 if (ii < 5)
1254                                         wFallBackRate =
1255                                                 awHWRetry0[wRate-RATE_18M][ii];
1256                                 else
1257                                         wFallBackRate =
1258                                                 awHWRetry0[wRate-RATE_18M][4];
1259                                 pMgmt->sNodeDBTable[0].uTxFail[wFallBackRate]++;
1260                         }
1261                 } else if (byFallBack == AUTO_FB_1) {
1262                         for (ii = 0; ii < byTxRetry; ii++) {
1263                                 if (ii < 5)
1264                                         wFallBackRate =
1265                                                 awHWRetry1[wRate-RATE_18M][ii];
1266                                 else
1267                                         wFallBackRate =
1268                                                 awHWRetry1[wRate-RATE_18M][4];
1269                                 pMgmt->sNodeDBTable[0].uTxFail[wFallBackRate]++;
1270                         }
1271                 }
1272             }
1273         }
1274
1275         if ((pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) ||
1276             (pMgmt->eCurrMode == WMAC_MODE_ESS_AP)) {
1277
1278                 if (BSSbIsSTAInNodeDB((void *) pDevice,
1279                                       pbyDestAddr,
1280                                       &uNodeIndex)) {
1281                         pMgmt->sNodeDBTable[uNodeIndex].uTxAttempts += 1;
1282                 if ( !(byTSR & (TSR_TMO | TSR_RETRYTMO))) {
1283                     // transmit success, TxAttempts at least plus one
1284                     pMgmt->sNodeDBTable[uNodeIndex].uTxOk[MAX_RATE]++;
1285                     if ( (byFallBack == AUTO_FB_NONE) ||
1286                          (wRate < RATE_18M) ) {
1287                         wFallBackRate = wRate;
1288                     } else if (byFallBack == AUTO_FB_0) {
1289                         if (byTxRetry < 5)
1290                             wFallBackRate = awHWRetry0[wRate-RATE_18M][byTxRetry];
1291                         else
1292                             wFallBackRate = awHWRetry0[wRate-RATE_18M][4];
1293                     } else if (byFallBack == AUTO_FB_1) {
1294                         if (byTxRetry < 5)
1295                             wFallBackRate = awHWRetry1[wRate-RATE_18M][byTxRetry];
1296                         else
1297                             wFallBackRate = awHWRetry1[wRate-RATE_18M][4];
1298                     }
1299                     pMgmt->sNodeDBTable[uNodeIndex].uTxOk[wFallBackRate]++;
1300                 } else {
1301                     pMgmt->sNodeDBTable[uNodeIndex].uTxFailures ++;
1302                 }
1303                 pMgmt->sNodeDBTable[uNodeIndex].uTxRetry += byTxRetry;
1304                 if (byTxRetry != 0) {
1305                     pMgmt->sNodeDBTable[uNodeIndex].uTxFail[MAX_RATE]+=byTxRetry;
1306                     if ( (byFallBack == AUTO_FB_NONE) ||
1307                          (wRate < RATE_18M) ) {
1308                         pMgmt->sNodeDBTable[uNodeIndex].uTxFail[wRate]+=byTxRetry;
1309                     } else if (byFallBack == AUTO_FB_0) {
1310                         for (ii = 0; ii < byTxRetry; ii++) {
1311                                 if (ii < 5)
1312                                         wFallBackRate =
1313                                                 awHWRetry0[wRate-RATE_18M][ii];
1314                                 else
1315                                         wFallBackRate =
1316                                                 awHWRetry0[wRate-RATE_18M][4];
1317                                 pMgmt->sNodeDBTable[uNodeIndex].uTxFail[wFallBackRate]++;
1318                         }
1319                     } else if (byFallBack == AUTO_FB_1) {
1320                       for (ii = 0; ii < byTxRetry; ii++) {
1321                         if (ii < 5)
1322                                 wFallBackRate = awHWRetry1[wRate-RATE_18M][ii];
1323                         else
1324                                 wFallBackRate = awHWRetry1[wRate-RATE_18M][4];
1325                         pMgmt->sNodeDBTable[uNodeIndex].uTxFail[wFallBackRate]++;
1326                       }
1327                     }
1328                 }
1329             }
1330         }
1331     }
1332 }
1333
1334 /*+
1335  *
1336  * Routine Description:
1337  *    Clear Nodes & skb in DB Table
1338  *
1339  *
1340  * Parameters:
1341  *  In:
1342  *      hDeviceContext        - The adapter context.
1343  *      uStartIndex           - starting index
1344  *  Out:
1345  *      none
1346  *
1347  * Return Value:
1348  *    None.
1349  *
1350 -*/
1351
1352 void BSSvClearNodeDBTable(struct vnt_private *pDevice, u32 uStartIndex)
1353 {
1354         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
1355         struct sk_buff  *skb;
1356         int ii;
1357
1358     for (ii = uStartIndex; ii < (MAX_NODE_NUM + 1); ii++) {
1359         if (pMgmt->sNodeDBTable[ii].bActive) {
1360             // check if sTxPSQueue has been initial
1361             if (pMgmt->sNodeDBTable[ii].sTxPSQueue.next != NULL) {
1362                 while ((skb = skb_dequeue(&pMgmt->sNodeDBTable[ii].sTxPSQueue)) != NULL){
1363                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "PS skb != NULL %d\n", ii);
1364                         dev_kfree_skb(skb);
1365                 }
1366             }
1367             memset(&pMgmt->sNodeDBTable[ii], 0, sizeof(KnownNodeDB));
1368         }
1369     }
1370 };
1371
1372 static void s_vCheckSensitivity(struct vnt_private *pDevice)
1373 {
1374         PKnownBSS pBSSList = NULL;
1375         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
1376         int ii;
1377
1378     if ((pMgmt->eCurrState == WMAC_STATE_ASSOC) ||
1379         ((pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) && (pMgmt->eCurrState == WMAC_STATE_JOINTED))) {
1380         pBSSList = BSSpAddrIsInBSSList(pDevice, pMgmt->abyCurrBSSID, (PWLAN_IE_SSID)pMgmt->abyCurrSSID);
1381         if (pBSSList != NULL) {
1382                 /* Update BB register if RSSI is too strong */
1383                 signed long    LocalldBmAverage = 0;
1384                 signed long    uNumofdBm = 0;
1385             for (ii = 0; ii < RSSI_STAT_COUNT; ii++) {
1386                 if (pBSSList->ldBmAverage[ii] != 0) {
1387                     uNumofdBm ++;
1388                     LocalldBmAverage += pBSSList->ldBmAverage[ii];
1389                 }
1390             }
1391             if (uNumofdBm > 0) {
1392                 LocalldBmAverage = LocalldBmAverage/uNumofdBm;
1393                 for (ii=0;ii<BB_VGA_LEVEL;ii++) {
1394                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"LocalldBmAverage:%ld, %ld %02x\n", LocalldBmAverage, pDevice->ldBmThreshold[ii], pDevice->abyBBVGA[ii]);
1395                     if (LocalldBmAverage < pDevice->ldBmThreshold[ii]) {
1396                             pDevice->byBBVGANew = pDevice->abyBBVGA[ii];
1397                         break;
1398                     }
1399                 }
1400                 if (pDevice->byBBVGANew != pDevice->byBBVGACurrent) {
1401                     pDevice->uBBVGADiffCount++;
1402                     if (pDevice->uBBVGADiffCount >= BB_VGA_CHANGE_THRESHOLD)
1403                         bScheduleCommand(pDevice,
1404                                          WLAN_CMD_CHANGE_BBSENSITIVITY,
1405                                          NULL);
1406                 } else {
1407                     pDevice->uBBVGADiffCount = 0;
1408                 }
1409             }
1410         }
1411     }
1412 }
1413
1414 static void s_uCalculateLinkQual(struct vnt_private *pDevice)
1415 {
1416         unsigned long TxOkRatio, TxCnt;
1417         unsigned long RxOkRatio, RxCnt;
1418         unsigned long RssiRatio;
1419         long ldBm;
1420
1421 TxCnt = pDevice->scStatistic.TxNoRetryOkCount +
1422               pDevice->scStatistic.TxRetryOkCount +
1423               pDevice->scStatistic.TxFailCount;
1424 RxCnt = pDevice->scStatistic.RxFcsErrCnt +
1425               pDevice->scStatistic.RxOkCnt;
1426 TxOkRatio = (TxCnt < 6) ? 4000:((pDevice->scStatistic.TxNoRetryOkCount * 4000) / TxCnt);
1427 RxOkRatio = (RxCnt < 6) ? 2000:((pDevice->scStatistic.RxOkCnt * 2000) / RxCnt);
1428 //decide link quality
1429 if(pDevice->bLinkPass !=true)
1430 {
1431    pDevice->scStatistic.LinkQuality = 0;
1432    pDevice->scStatistic.SignalStren = 0;
1433 }
1434 else
1435 {
1436    RFvRSSITodBm(pDevice, (BYTE)(pDevice->uCurrRSSI), &ldBm);
1437    if(-ldBm < 50)  {
1438         RssiRatio = 4000;
1439      }
1440    else if(-ldBm > 90) {
1441         RssiRatio = 0;
1442      }
1443    else {
1444         RssiRatio = (40-(-ldBm-50))*4000/40;
1445      }
1446    pDevice->scStatistic.SignalStren = RssiRatio/40;
1447    pDevice->scStatistic.LinkQuality = (RssiRatio+TxOkRatio+RxOkRatio)/100;
1448 }
1449    pDevice->scStatistic.RxFcsErrCnt = 0;
1450    pDevice->scStatistic.RxOkCnt = 0;
1451    pDevice->scStatistic.TxFailCount = 0;
1452    pDevice->scStatistic.TxNoRetryOkCount = 0;
1453    pDevice->scStatistic.TxRetryOkCount = 0;
1454 }
1455
1456 void BSSvClearAnyBSSJoinRecord(struct vnt_private *pDevice)
1457 {
1458         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
1459         int ii;
1460
1461         for (ii = 0; ii < MAX_BSS_NUM; ii++)
1462                 pMgmt->sBSSList[ii].bSelected = false;
1463
1464         return;
1465 }
1466
1467 static void s_vCheckPreEDThreshold(struct vnt_private *pDevice)
1468 {
1469         PKnownBSS pBSSList = NULL;
1470         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
1471
1472     if ((pMgmt->eCurrState == WMAC_STATE_ASSOC) ||
1473         ((pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) && (pMgmt->eCurrState == WMAC_STATE_JOINTED))) {
1474         pBSSList = BSSpAddrIsInBSSList(pDevice, pMgmt->abyCurrBSSID, (PWLAN_IE_SSID)pMgmt->abyCurrSSID);
1475         if (pBSSList != NULL) {
1476             pDevice->byBBPreEDRSSI = (BYTE) (~(pBSSList->ldBmAverRange) + 1);
1477             BBvUpdatePreEDThreshold(pDevice, false);
1478         }
1479     }
1480 }
1481