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