staging: vt6655: dead code remove wcmd.c
[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
447                         if (pDevice->bLinkPass && (pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
448                                 if (KeybGetTransmitKey(&(pDevice->sKey), pDevice->abyBSSID, PAIRWISE_KEY, &pTransmitKey) ||
449                                     KeybGetTransmitKey(&(pDevice->sKey), pDevice->abyBSSID, GROUP_KEY, &pTransmitKey)) {
450                                         pDevice->gsPMKIDCandidate.StatusType = Ndis802_11StatusType_PMKID_CandidateList;
451                                         pDevice->gsPMKIDCandidate.Version = 1;
452
453                                 }
454
455                         }
456                 }
457         }
458
459         if (pDevice->bUpdateBBVGA) {
460                 /* monitor if RSSI is too strong */
461                 pBSSList->byRSSIStatCnt = 0;
462                 RFvRSSITodBm(pDevice, (unsigned char)(pRxPacket->uRSSI), &pBSSList->ldBmMAX);
463                 pBSSList->ldBmAverage[0] = pBSSList->ldBmMAX;
464                 for (ii = 1; ii < RSSI_STAT_COUNT; ii++)
465                         pBSSList->ldBmAverage[ii] = 0;
466         }
467
468         if ((pIE_Country != NULL) && pMgmt->b11hEnable) {
469                 set_country_info(pMgmt->pAdapter, pBSSList->eNetworkTypeInUse,
470                                  pIE_Country);
471         }
472
473         if (bParsingQuiet && (pIE_Quiet != NULL)) {
474                 if ((((PWLAN_IE_QUIET)pIE_Quiet)->len == 8) &&
475                     (((PWLAN_IE_QUIET)pIE_Quiet)->byQuietCount != 0)) {
476                         /* valid EID */
477                         if (pQuiet == NULL) {
478                                 pQuiet = (PWLAN_IE_QUIET)pIE_Quiet;
479                                 CARDbSetQuiet(pMgmt->pAdapter,
480                                               true,
481                                               pQuiet->byQuietCount,
482                                               pQuiet->byQuietPeriod,
483                                               *((unsigned short *)pQuiet->abyQuietDuration),
484                                               *((unsigned short *)pQuiet->abyQuietOffset)
485 );
486                         } else {
487                                 pQuiet = (PWLAN_IE_QUIET)pIE_Quiet;
488                                 CARDbSetQuiet(pMgmt->pAdapter,
489                                               false,
490                                               pQuiet->byQuietCount,
491                                               pQuiet->byQuietPeriod,
492                                               *((unsigned short *)pQuiet->abyQuietDuration),
493                                               *((unsigned short *)pQuiet->abyQuietOffset)
494                                         );
495                         }
496                 }
497         }
498
499         if (bParsingQuiet && (pQuiet != NULL))
500                 CARDbStartQuiet(pMgmt->pAdapter);
501
502         pBSSList->uIELength = uIELength;
503         if (pBSSList->uIELength > WLAN_BEACON_FR_MAXLEN)
504                 pBSSList->uIELength = WLAN_BEACON_FR_MAXLEN;
505         memcpy(pBSSList->abyIEs, pbyIEs, pBSSList->uIELength);
506
507         return true;
508 }
509
510 /*+
511  *
512  * Routine Description:
513  *    Update BSS set in known BSS list
514  *
515  * Return Value:
516  *    true if success.
517  *
518  -*/
519 /* TODO: input structure modify */
520
521 bool
522 BSSbUpdateToBSSList(
523         void *hDeviceContext,
524         __le64 qwTimestamp,
525         unsigned short wBeaconInterval,
526         unsigned short wCapInfo,
527         unsigned char byCurrChannel,
528         bool bChannelHit,
529         PWLAN_IE_SSID pSSID,
530         PWLAN_IE_SUPP_RATES pSuppRates,
531         PWLAN_IE_SUPP_RATES pExtSuppRates,
532         PERPObject psERP,
533         PWLAN_IE_RSN pRSN,
534         PWLAN_IE_RSN_EXT pRSNWPA,
535         PWLAN_IE_COUNTRY pIE_Country,
536         PWLAN_IE_QUIET pIE_Quiet,
537         PKnownBSS pBSSList,
538         unsigned int uIELength,
539         unsigned char *pbyIEs,
540         void *pRxPacketContext
541 )
542 {
543         int             ii;
544         struct vnt_private *pDevice = hDeviceContext;
545         PSMgmtObject    pMgmt = pDevice->pMgmt;
546         PSRxMgmtPacket  pRxPacket = (PSRxMgmtPacket)pRxPacketContext;
547         long            ldBm;
548         bool bParsingQuiet = false;
549         PWLAN_IE_QUIET  pQuiet = NULL;
550
551         if (pBSSList == NULL)
552                 return false;
553
554         pBSSList->qwBSSTimestamp = le64_to_cpu(qwTimestamp);
555         pBSSList->wBeaconInterval = cpu_to_le16(wBeaconInterval);
556         pBSSList->wCapInfo = cpu_to_le16(wCapInfo);
557         pBSSList->uClearCount = 0;
558         pBSSList->uChannel = byCurrChannel;
559
560         if (pSSID->len > WLAN_SSID_MAXLEN)
561                 pSSID->len = WLAN_SSID_MAXLEN;
562
563         if ((pSSID->len != 0) && (pSSID->abySSID[0] != 0))
564                 memcpy(pBSSList->abySSID, pSSID, pSSID->len + WLAN_IEHDR_LEN);
565         memcpy(pBSSList->abySuppRates, pSuppRates, pSuppRates->len + WLAN_IEHDR_LEN);
566
567         if (pExtSuppRates != NULL)
568                 memcpy(pBSSList->abyExtSuppRates, pExtSuppRates, pExtSuppRates->len + WLAN_IEHDR_LEN);
569         else
570                 memset(pBSSList->abyExtSuppRates, 0, WLAN_IEHDR_LEN + WLAN_RATES_MAXLEN + 1);
571         pBSSList->sERP.byERP = psERP->byERP;
572         pBSSList->sERP.bERPExist = psERP->bERPExist;
573
574         /* check if BSS is 802.11a/b/g */
575         if (pBSSList->uChannel > CB_MAX_CHANNEL_24G) {
576                 pBSSList->eNetworkTypeInUse = PHY_TYPE_11A;
577         } else {
578                 if (pBSSList->sERP.bERPExist)
579                         pBSSList->eNetworkTypeInUse = PHY_TYPE_11G;
580                 else
581                         pBSSList->eNetworkTypeInUse = PHY_TYPE_11B;
582         }
583
584         pBSSList->byRxRate = pRxPacket->byRxRate;
585         pBSSList->qwLocalTSF = pRxPacket->qwLocalTSF;
586         if (bChannelHit)
587                 pBSSList->uRSSI = pRxPacket->uRSSI;
588         pBSSList->bySQ = pRxPacket->bySQ;
589
590         if ((pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
591             (pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
592                 /* assoc with BSS */
593                 if (pBSSList == pMgmt->pCurrBSS)
594                         bParsingQuiet = true;
595         }
596
597         WPA_ClearRSN(pBSSList);         /* mike update */
598
599         if (pRSNWPA != NULL) {
600                 unsigned int uLen = pRSNWPA->len + 2;
601
602                 if (uLen <= (uIELength - (unsigned int)((unsigned char *)pRSNWPA - pbyIEs))) {
603                         pBSSList->wWPALen = uLen;
604                         memcpy(pBSSList->byWPAIE, pRSNWPA, uLen);
605                         WPA_ParseRSN(pBSSList, pRSNWPA);
606                 }
607         }
608
609         WPA2_ClearRSN(pBSSList);  /* mike update */
610
611         if (pRSN != NULL) {
612                 unsigned int uLen = pRSN->len + 2;
613
614                 if (uLen <= (uIELength - (unsigned int)((unsigned char *)pRSN - pbyIEs))) {
615                         pBSSList->wRSNLen = uLen;
616                         memcpy(pBSSList->byRSNIE, pRSN, uLen);
617                         WPA2vParseRSN(pBSSList, pRSN);
618                 }
619         }
620
621         if (pRxPacket->uRSSI != 0) {
622                 RFvRSSITodBm(pDevice, (unsigned char)(pRxPacket->uRSSI), &ldBm);
623                 /* monitor if RSSI is too strong */
624                 pBSSList->byRSSIStatCnt++;
625                 pBSSList->byRSSIStatCnt %= RSSI_STAT_COUNT;
626                 pBSSList->ldBmAverage[pBSSList->byRSSIStatCnt] = ldBm;
627                 for (ii = 0; ii < RSSI_STAT_COUNT; ii++) {
628                         if (pBSSList->ldBmAverage[ii] != 0)
629                                 pBSSList->ldBmMAX = max(pBSSList->ldBmAverage[ii], ldBm);
630                 }
631         }
632
633         if ((pIE_Country != NULL) && pMgmt->b11hEnable) {
634                 set_country_info(pMgmt->pAdapter, pBSSList->eNetworkTypeInUse,
635                                  pIE_Country);
636         }
637
638         if (bParsingQuiet && (pIE_Quiet != NULL)) {
639                 if ((((PWLAN_IE_QUIET)pIE_Quiet)->len == 8) &&
640                     (((PWLAN_IE_QUIET)pIE_Quiet)->byQuietCount != 0)) {
641                         /* valid EID */
642                         if (pQuiet == NULL) {
643                                 pQuiet = (PWLAN_IE_QUIET)pIE_Quiet;
644                                 CARDbSetQuiet(pMgmt->pAdapter,
645                                               true,
646                                               pQuiet->byQuietCount,
647                                               pQuiet->byQuietPeriod,
648                                               *((unsigned short *)pQuiet->abyQuietDuration),
649                                               *((unsigned short *)pQuiet->abyQuietOffset)
650 );
651                         } else {
652                                 pQuiet = (PWLAN_IE_QUIET)pIE_Quiet;
653                                 CARDbSetQuiet(pMgmt->pAdapter,
654                                               false,
655                                               pQuiet->byQuietCount,
656                                               pQuiet->byQuietPeriod,
657                                               *((unsigned short *)pQuiet->abyQuietDuration),
658                                               *((unsigned short *)pQuiet->abyQuietOffset)
659                                         );
660                         }
661                 }
662         }
663
664         if (bParsingQuiet && (pQuiet != NULL))
665                 CARDbStartQuiet(pMgmt->pAdapter);
666
667         pBSSList->uIELength = uIELength;
668         if (pBSSList->uIELength > WLAN_BEACON_FR_MAXLEN)
669                 pBSSList->uIELength = WLAN_BEACON_FR_MAXLEN;
670         memcpy(pBSSList->abyIEs, pbyIEs, pBSSList->uIELength);
671
672         return true;
673 }
674
675 /*+
676  *
677  * Routine Description:
678  *    Search Node DB table to find the index of matched DstAddr
679  *
680  * Return Value:
681  *    None
682  *
683  -*/
684
685 bool
686 BSSDBbIsSTAInNodeDB(void *pMgmtObject, unsigned char *abyDstAddr,
687                     unsigned int *puNodeIndex)
688 {
689         PSMgmtObject    pMgmt = (PSMgmtObject) pMgmtObject;
690         unsigned int ii;
691
692         /* Index = 0 reserved for AP Node */
693         for (ii = 1; ii < (MAX_NODE_NUM + 1); ii++) {
694                 if (pMgmt->sNodeDBTable[ii].bActive) {
695                         if (ether_addr_equal(abyDstAddr,
696                                              pMgmt->sNodeDBTable[ii].abyMACAddr)) {
697                                 *puNodeIndex = ii;
698                                 return true;
699                         }
700                 }
701         }
702
703         return false;
704 };
705
706 /*+
707  *
708  * Routine Description:
709  *    Find an empty node and allocat it; if there is no empty node,
710  *    then use the most inactive one.
711  *
712  * Return Value:
713  *    None
714  *
715  -*/
716 void
717 BSSvCreateOneNode(void *hDeviceContext, unsigned int *puNodeIndex)
718 {
719         struct vnt_private *pDevice = hDeviceContext;
720         PSMgmtObject    pMgmt = pDevice->pMgmt;
721         unsigned int ii;
722         unsigned int BigestCount = 0;
723         unsigned int SelectIndex;
724         struct sk_buff  *skb;
725         /*
726          * Index = 0 reserved for AP Node (In STA mode)
727          * Index = 0 reserved for Broadcast/MultiCast (In AP mode)
728          */
729         SelectIndex = 1;
730         for (ii = 1; ii < (MAX_NODE_NUM + 1); ii++) {
731                 if (pMgmt->sNodeDBTable[ii].bActive) {
732                         if (pMgmt->sNodeDBTable[ii].uInActiveCount > BigestCount) {
733                                 BigestCount = pMgmt->sNodeDBTable[ii].uInActiveCount;
734                                 SelectIndex = ii;
735                         }
736                 } else {
737                         break;
738                 }
739         }
740
741         /* if not found replace uInActiveCount is largest one */
742         if (ii == (MAX_NODE_NUM + 1)) {
743                 *puNodeIndex = SelectIndex;
744                 pr_info("Replace inactive node = %d\n", SelectIndex);
745                 /* clear ps buffer */
746                 if (pMgmt->sNodeDBTable[*puNodeIndex].sTxPSQueue.next != NULL) {
747                         while ((skb = skb_dequeue(&pMgmt->sNodeDBTable[*puNodeIndex].sTxPSQueue)) != NULL)
748                                 dev_kfree_skb(skb);
749                 }
750         } else {
751                 *puNodeIndex = ii;
752         }
753
754         memset(&pMgmt->sNodeDBTable[*puNodeIndex], 0, sizeof(KnownNodeDB));
755         pMgmt->sNodeDBTable[*puNodeIndex].bActive = true;
756         pMgmt->sNodeDBTable[*puNodeIndex].uRatePollTimeout = FALLBACK_POLL_SECOND;
757         /* for AP mode PS queue */
758         skb_queue_head_init(&pMgmt->sNodeDBTable[*puNodeIndex].sTxPSQueue);
759         pMgmt->sNodeDBTable[*puNodeIndex].byAuthSequence = 0;
760         pMgmt->sNodeDBTable[*puNodeIndex].wEnQueueCnt = 0;
761         pr_debug("Create node index = %d\n", ii);
762         return;
763 };
764
765 /*+
766  *
767  * Routine Description:
768  *    Remove Node by NodeIndex
769  *
770  *
771  * Return Value:
772  *    None
773  *
774  -*/
775 void
776 BSSvRemoveOneNode(
777         void *hDeviceContext,
778         unsigned int uNodeIndex
779 )
780 {
781         struct vnt_private *pDevice = hDeviceContext;
782         PSMgmtObject    pMgmt = pDevice->pMgmt;
783         unsigned char byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
784         struct sk_buff  *skb;
785
786         while ((skb = skb_dequeue(&pMgmt->sNodeDBTable[uNodeIndex].sTxPSQueue)) != NULL)
787                 dev_kfree_skb(skb);
788         /* clear context */
789         memset(&pMgmt->sNodeDBTable[uNodeIndex], 0, sizeof(KnownNodeDB));
790         /* clear tx bit map */
791         pMgmt->abyPSTxMap[pMgmt->sNodeDBTable[uNodeIndex].wAID >> 3] &=  ~byMask[pMgmt->sNodeDBTable[uNodeIndex].wAID & 7];
792
793         return;
794 };
795 /*+
796  *
797  * Routine Description:
798  *    Update AP Node content in Index 0 of KnownNodeDB
799  *
800  *
801  * Return Value:
802  *    None
803  *
804  -*/
805
806 void
807 BSSvUpdateAPNode(
808         void *hDeviceContext,
809         unsigned short *pwCapInfo,
810         PWLAN_IE_SUPP_RATES pSuppRates,
811         PWLAN_IE_SUPP_RATES pExtSuppRates
812 )
813 {
814         struct vnt_private *pDevice = hDeviceContext;
815         PSMgmtObject    pMgmt = pDevice->pMgmt;
816         unsigned int uRateLen = WLAN_RATES_MAXLEN;
817
818         memset(&pMgmt->sNodeDBTable[0], 0, sizeof(KnownNodeDB));
819
820         pMgmt->sNodeDBTable[0].bActive = true;
821         if (pDevice->eCurrentPHYType == PHY_TYPE_11B)
822                 uRateLen = WLAN_RATES_MAXLEN_11B;
823         pMgmt->abyCurrSuppRates[1] = RATEuSetIE((PWLAN_IE_SUPP_RATES)pSuppRates,
824                                                 (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
825                                                 uRateLen);
826         pMgmt->abyCurrExtSuppRates[1] = RATEuSetIE((PWLAN_IE_SUPP_RATES)pExtSuppRates,
827                                                    (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates,
828                                                    uRateLen);
829         RATEvParseMaxRate((void *)pDevice,
830                           (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
831                           (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates,
832                           true,
833                           &(pMgmt->sNodeDBTable[0].wMaxBasicRate),
834                           &(pMgmt->sNodeDBTable[0].wMaxSuppRate),
835                           &(pMgmt->sNodeDBTable[0].wSuppRate),
836                           &(pMgmt->sNodeDBTable[0].byTopCCKBasicRate),
837                           &(pMgmt->sNodeDBTable[0].byTopOFDMBasicRate)
838 );
839         memcpy(pMgmt->sNodeDBTable[0].abyMACAddr, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
840         pMgmt->sNodeDBTable[0].wTxDataRate = pMgmt->sNodeDBTable[0].wMaxSuppRate;
841         pMgmt->sNodeDBTable[0].bShortPreamble = WLAN_GET_CAP_INFO_SHORTPREAMBLE(*pwCapInfo);
842         pMgmt->sNodeDBTable[0].uRatePollTimeout = FALLBACK_POLL_SECOND;
843         netdev_dbg(pDevice->dev, "BSSvUpdateAPNode:MaxSuppRate is %d\n",
844                    pMgmt->sNodeDBTable[0].wMaxSuppRate);
845         /* auto rate fallback function initiation */
846         pr_debug("pMgmt->sNodeDBTable[0].wTxDataRate = %d\n",
847                  pMgmt->sNodeDBTable[0].wTxDataRate);
848 };
849
850 /*+
851  *
852  * Routine Description:
853  *    Add Multicast Node content in Index 0 of KnownNodeDB
854  *
855  *
856  * Return Value:
857  *    None
858  *
859  -*/
860
861 void
862 BSSvAddMulticastNode(
863         void *hDeviceContext
864 )
865 {
866         struct vnt_private *pDevice = hDeviceContext;
867         PSMgmtObject    pMgmt = pDevice->pMgmt;
868
869         if (!pDevice->bEnableHostWEP)
870                 memset(&pMgmt->sNodeDBTable[0], 0, sizeof(KnownNodeDB));
871         memset(pMgmt->sNodeDBTable[0].abyMACAddr, 0xff, WLAN_ADDR_LEN);
872         pMgmt->sNodeDBTable[0].bActive = true;
873         pMgmt->sNodeDBTable[0].bPSEnable = false;
874         skb_queue_head_init(&pMgmt->sNodeDBTable[0].sTxPSQueue);
875         RATEvParseMaxRate((void *)pDevice,
876                           (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
877                           (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates,
878                           true,
879                           &(pMgmt->sNodeDBTable[0].wMaxBasicRate),
880                           &(pMgmt->sNodeDBTable[0].wMaxSuppRate),
881                           &(pMgmt->sNodeDBTable[0].wSuppRate),
882                           &(pMgmt->sNodeDBTable[0].byTopCCKBasicRate),
883                           &(pMgmt->sNodeDBTable[0].byTopOFDMBasicRate)
884 );
885         pMgmt->sNodeDBTable[0].wTxDataRate = pMgmt->sNodeDBTable[0].wMaxBasicRate;
886         netdev_dbg(pDevice->dev,
887                    "BSSvAddMultiCastNode:pMgmt->sNodeDBTable[0].wTxDataRate is %d\n",
888                    pMgmt->sNodeDBTable[0].wTxDataRate);
889         pMgmt->sNodeDBTable[0].uRatePollTimeout = FALLBACK_POLL_SECOND;
890 };
891
892 /*+
893  *
894  * Routine Description:
895  *
896  *
897  *  Second call back function to update Node DB info & AP link status
898  *
899  *
900  * Return Value:
901  *    none.
902  *
903  -*/
904 void
905 BSSvSecondCallBack(
906         void *hDeviceContext
907 )
908 {
909         struct vnt_private *pDevice = hDeviceContext;
910         PSMgmtObject    pMgmt = pDevice->pMgmt;
911         unsigned int ii;
912         PWLAN_IE_SSID   pItemSSID, pCurrSSID;
913         unsigned int uSleepySTACnt = 0;
914         unsigned int uNonShortSlotSTACnt = 0;
915         unsigned int uLongPreambleSTACnt = 0;
916         viawget_wpa_header *wpahdr;  /* DavidWang */
917
918         spin_lock_irq(&pDevice->lock);
919
920         pDevice->uAssocCount = 0;
921
922         pDevice->byERPFlag &=
923                 ~(WLAN_SET_ERP_BARKER_MODE(1) | WLAN_SET_ERP_NONERP_PRESENT(1));
924
925         if (pDevice->wUseProtectCntDown > 0) {
926                 pDevice->wUseProtectCntDown--;
927         } else {
928                 /* disable protect mode */
929                 pDevice->byERPFlag &= ~(WLAN_SET_ERP_USE_PROTECTION(1));
930         }
931
932         if (pDevice->eCommandState == WLAN_ASSOCIATE_WAIT) {
933                 pDevice->byReAssocCount++;
934                 /* 10 sec timeout */
935                 if ((pDevice->byReAssocCount > 10) && (!pDevice->bLinkPass)) {
936                         netdev_info(pDevice->dev, "Re-association timeout!!!\n");
937                         pDevice->byReAssocCount = 0;
938 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
939                         {
940                                 union iwreq_data  wrqu;
941
942                                 memset(&wrqu, 0, sizeof(wrqu));
943                                 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
944                                 PRINT_K("wireless_send_event--->SIOCGIWAP(disassociated)\n");
945                                 wireless_send_event(pDevice->dev, SIOCGIWAP, &wrqu, NULL);
946                         }
947 #endif
948                 } else if (pDevice->bLinkPass)
949                         pDevice->byReAssocCount = 0;
950         }
951
952 #ifdef Calcu_LinkQual
953         s_uCalculateLinkQual((void *)pDevice);
954 #endif
955
956         for (ii = 0; ii < (MAX_NODE_NUM + 1); ii++) {
957                 if (pMgmt->sNodeDBTable[ii].bActive) {
958                         /* increase in-activity counter */
959                         pMgmt->sNodeDBTable[ii].uInActiveCount++;
960
961                         if (ii > 0) {
962                                 if (pMgmt->sNodeDBTable[ii].uInActiveCount > MAX_INACTIVE_COUNT) {
963                                         BSSvRemoveOneNode(pDevice, ii);
964                                         pr_debug("Inactive timeout [%d] sec, STA index = [%d] remove\n",
965                                                  MAX_INACTIVE_COUNT, ii);
966                                         continue;
967                                 }
968
969                                 if (pMgmt->sNodeDBTable[ii].eNodeState >= NODE_ASSOC) {
970                                         pDevice->uAssocCount++;
971
972                                         /* check if Non ERP exist */
973                                         if (pMgmt->sNodeDBTable[ii].uInActiveCount < ERP_RECOVER_COUNT) {
974                                                 if (!pMgmt->sNodeDBTable[ii].bShortPreamble) {
975                                                         pDevice->byERPFlag |= WLAN_SET_ERP_BARKER_MODE(1);
976                                                         uLongPreambleSTACnt++;
977                                                 }
978                                                 if (!pMgmt->sNodeDBTable[ii].bERPExist) {
979                                                         pDevice->byERPFlag |= WLAN_SET_ERP_NONERP_PRESENT(1);
980                                                         pDevice->byERPFlag |= WLAN_SET_ERP_USE_PROTECTION(1);
981                                                 }
982                                                 if (!pMgmt->sNodeDBTable[ii].bShortSlotTime)
983                                                         uNonShortSlotSTACnt++;
984                                         }
985                                 }
986
987                                 /* check if any STA in PS mode */
988                                 if (pMgmt->sNodeDBTable[ii].bPSEnable)
989                                         uSleepySTACnt++;
990
991                         }
992
993                         /* rate fallback check */
994                         if (!pDevice->bFixRate) {
995                                 if (ii > 0) {
996                                         /* ii = 0 for multicast node (AP & Adhoc) */
997                                         RATEvTxRateFallBack((void *)pDevice, &(pMgmt->sNodeDBTable[ii]));
998                                 } else {
999                                         /* ii = 0 reserved for unicast AP node (Infra STA) */
1000                                         if (pMgmt->eCurrMode == WMAC_MODE_ESS_STA)
1001                                                 netdev_dbg(pDevice->dev,
1002                                                            "SecondCallback:Before:TxDataRate is %d\n",
1003                                                            pMgmt->sNodeDBTable[0].wTxDataRate);
1004                                         RATEvTxRateFallBack((void *)pDevice, &(pMgmt->sNodeDBTable[ii]));
1005                                         netdev_dbg(pDevice->dev,
1006                                                    "SecondCallback:After:TxDataRate is %d\n",
1007                                                    pMgmt->sNodeDBTable[0].wTxDataRate);
1008
1009                                 }
1010
1011                         }
1012
1013                         /* check if pending PS queue */
1014                         if (pMgmt->sNodeDBTable[ii].wEnQueueCnt != 0) {
1015                                 pr_debug("Index= %d, Queue = %d pending\n",
1016                                          ii,
1017                                          pMgmt->sNodeDBTable[ii].wEnQueueCnt);
1018                                 if ((ii > 0) && (pMgmt->sNodeDBTable[ii].wEnQueueCnt > 15)) {
1019                                         BSSvRemoveOneNode(pDevice, ii);
1020                                         pr_info("Pending many queues PS STA Index = %d remove\n",
1021                                                 ii);
1022                                         continue;
1023                                 }
1024                         }
1025                 }
1026
1027         }
1028
1029         if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) && (pDevice->eCurrentPHYType == PHY_TYPE_11G)) {
1030                 /* on/off protect mode */
1031                 if (WLAN_GET_ERP_USE_PROTECTION(pDevice->byERPFlag)) {
1032                         if (!pDevice->bProtectMode) {
1033                                 MACvEnableProtectMD(pDevice->PortOffset);
1034                                 pDevice->bProtectMode = true;
1035                         }
1036                 } else {
1037                         if (pDevice->bProtectMode) {
1038                                 MACvDisableProtectMD(pDevice->PortOffset);
1039                                 pDevice->bProtectMode = false;
1040                         }
1041                 }
1042                 /* on/off short slot time */
1043
1044                 if (uNonShortSlotSTACnt > 0) {
1045                         if (pDevice->bShortSlotTime) {
1046                                 pDevice->bShortSlotTime = false;
1047                                 BBvSetShortSlotTime(pDevice);
1048                                 vUpdateIFS((void *)pDevice);
1049                         }
1050                 } else {
1051                         if (!pDevice->bShortSlotTime) {
1052                                 pDevice->bShortSlotTime = true;
1053                                 BBvSetShortSlotTime(pDevice);
1054                                 vUpdateIFS((void *)pDevice);
1055                         }
1056                 }
1057
1058                 /* on/off barker long preamble mode */
1059
1060                 if (uLongPreambleSTACnt > 0) {
1061                         if (!pDevice->bBarkerPreambleMd) {
1062                                 MACvEnableBarkerPreambleMd(pDevice->PortOffset);
1063                                 pDevice->bBarkerPreambleMd = true;
1064                         }
1065                 } else {
1066                         if (pDevice->bBarkerPreambleMd) {
1067                                 MACvDisableBarkerPreambleMd(pDevice->PortOffset);
1068                                 pDevice->bBarkerPreambleMd = false;
1069                         }
1070                 }
1071
1072         }
1073
1074         /* check if any STA in PS mode, enable DTIM multicast deliver */
1075         if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
1076                 if (uSleepySTACnt > 0)
1077                         pMgmt->sNodeDBTable[0].bPSEnable = true;
1078                 else
1079                         pMgmt->sNodeDBTable[0].bPSEnable = false;
1080         }
1081
1082         pItemSSID = (PWLAN_IE_SSID)pMgmt->abyDesireSSID;
1083         pCurrSSID = (PWLAN_IE_SSID)pMgmt->abyCurrSSID;
1084
1085         if ((pMgmt->eCurrMode == WMAC_MODE_STANDBY) ||
1086             (pMgmt->eCurrMode == WMAC_MODE_ESS_STA)) {
1087                 /* assoc with BSS */
1088                 if (pMgmt->sNodeDBTable[0].bActive) {
1089                         if (pDevice->bUpdateBBVGA)
1090                                 s_vCheckPreEDThreshold((void *)pDevice);
1091
1092                         if ((pMgmt->sNodeDBTable[0].uInActiveCount >= (LOST_BEACON_COUNT/2)) &&
1093                             (pDevice->byBBVGACurrent != pDevice->abyBBVGA[0])) {
1094                                 pDevice->byBBVGANew = pDevice->abyBBVGA[0];
1095                         }
1096
1097                         if (pMgmt->sNodeDBTable[0].uInActiveCount >= LOST_BEACON_COUNT) {
1098                                 pMgmt->sNodeDBTable[0].bActive = false;
1099                                 pMgmt->eCurrMode = WMAC_MODE_STANDBY;
1100                                 pMgmt->eCurrState = WMAC_STATE_IDLE;
1101                                 netif_stop_queue(pDevice->dev);
1102                                 pDevice->bLinkPass = false;
1103                                 pDevice->bRoaming = true;
1104                                 pr_info("Lost AP beacon [%d] sec, disconnected !\n",
1105                                         pMgmt->sNodeDBTable[0].uInActiveCount);
1106                                 if ((pDevice->bWPADEVUp) && (pDevice->skb != NULL)) {
1107                                         wpahdr = (viawget_wpa_header *)pDevice->skb->data;
1108                                         wpahdr->type = VIAWGET_DISASSOC_MSG;
1109                                         wpahdr->resp_ie_len = 0;
1110                                         wpahdr->req_ie_len = 0;
1111                                         skb_put(pDevice->skb, sizeof(viawget_wpa_header));
1112                                         pDevice->skb->dev = pDevice->wpadev;
1113                                         skb_reset_mac_header(pDevice->skb);
1114                                         pDevice->skb->pkt_type = PACKET_HOST;
1115                                         pDevice->skb->protocol = htons(ETH_P_802_2);
1116                                         memset(pDevice->skb->cb, 0, sizeof(pDevice->skb->cb));
1117                                         netif_rx(pDevice->skb);
1118                                         pDevice->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1119                                 }
1120 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
1121                                 {
1122                                         union iwreq_data  wrqu;
1123
1124                                         memset(&wrqu, 0, sizeof(wrqu));
1125                                         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1126                                         PRINT_K("wireless_send_event--->SIOCGIWAP(disassociated)\n");
1127                                         wireless_send_event(pDevice->dev, SIOCGIWAP, &wrqu, NULL);
1128                                 }
1129 #endif
1130                         }
1131                 } else if (pItemSSID->len != 0) {
1132                         if (pDevice->uAutoReConnectTime < 10) {
1133                                 pDevice->uAutoReConnectTime++;
1134 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
1135                                 /*
1136                                  * network manager support need not do
1137                                  * Roaming scan???
1138                                  */
1139                                 if (pDevice->bWPASuppWextEnabled)
1140                                         pDevice->uAutoReConnectTime = 0;
1141 #endif
1142                         } else {
1143                                 /*
1144                                  * mike use old encryption status
1145                                  * for wpa reauthentication
1146                                  */
1147                                 if (pDevice->bWPADEVUp)
1148                                         pDevice->eEncryptionStatus = pDevice->eOldEncryptionStatus;
1149
1150                                 pr_debug("Roaming ...\n");
1151                                 BSSvClearBSSList((void *)pDevice, pDevice->bLinkPass);
1152                                 pMgmt->eScanType = WMAC_SCAN_ACTIVE;
1153                                 pDevice->uAutoReConnectTime = 0;
1154                         }
1155                 }
1156         }
1157
1158         if (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
1159                 /* if adhoc started which essid is NULL string, rescanning */
1160                 if ((pMgmt->eCurrState == WMAC_STATE_STARTED) && (pCurrSSID->len == 0)) {
1161                         if (pDevice->uAutoReConnectTime < 10) {
1162                                 pDevice->uAutoReConnectTime++;
1163                         } else {
1164                                 pr_info("Adhoc re-scanning ...\n");
1165                                 pMgmt->eScanType = WMAC_SCAN_ACTIVE;
1166                                 pDevice->uAutoReConnectTime = 0;
1167                         }
1168                 }
1169                 if (pMgmt->eCurrState == WMAC_STATE_JOINTED) {
1170                         if (pDevice->bUpdateBBVGA)
1171                                 s_vCheckPreEDThreshold((void *)pDevice);
1172                         if (pMgmt->sNodeDBTable[0].uInActiveCount >= ADHOC_LOST_BEACON_COUNT) {
1173                                 pr_info("Lost other STA beacon [%d] sec, started !\n",
1174                                         pMgmt->sNodeDBTable[0].uInActiveCount);
1175                                 pMgmt->sNodeDBTable[0].uInActiveCount = 0;
1176                                 pMgmt->eCurrState = WMAC_STATE_STARTED;
1177                                 netif_stop_queue(pDevice->dev);
1178                                 pDevice->bLinkPass = false;
1179                         }
1180                 }
1181         }
1182
1183         spin_unlock_irq(&pDevice->lock);
1184
1185         pMgmt->sTimerSecondCallback.expires = RUN_AT(HZ);
1186         add_timer(&pMgmt->sTimerSecondCallback);
1187 }
1188
1189 /*+
1190  *
1191  * Routine Description:
1192  *
1193  *
1194  *  Update Tx attemps, Tx failure counter in Node DB
1195  *
1196  *
1197  * Return Value:
1198  *    none.
1199  *
1200  -*/
1201
1202 void
1203 BSSvUpdateNodeTxCounter(
1204         void *hDeviceContext,
1205         unsigned char byTsr0,
1206         unsigned char byTsr1,
1207         unsigned char *pbyBuffer,
1208         unsigned int uFIFOHeaderSize
1209 )
1210 {
1211         struct vnt_private *pDevice = hDeviceContext;
1212         PSMgmtObject    pMgmt = pDevice->pMgmt;
1213         unsigned int uNodeIndex = 0;
1214         unsigned char byTxRetry = (byTsr0 & TSR0_NCR);
1215         PSTxBufHead     pTxBufHead;
1216         PS802_11Header  pMACHeader;
1217         unsigned short wRate;
1218         unsigned short wFallBackRate = RATE_1M;
1219         unsigned char byFallBack;
1220         unsigned int ii;
1221
1222         pTxBufHead = (PSTxBufHead) pbyBuffer;
1223         if (pTxBufHead->wFIFOCtl & FIFOCTL_AUTO_FB_0)
1224                 byFallBack = AUTO_FB_0;
1225         else if (pTxBufHead->wFIFOCtl & FIFOCTL_AUTO_FB_1)
1226                 byFallBack = AUTO_FB_1;
1227         else
1228                 byFallBack = AUTO_FB_NONE;
1229         wRate = pTxBufHead->wReserved;
1230
1231         /* Only Unicast using support rates */
1232         if (pTxBufHead->wFIFOCtl & FIFOCTL_NEEDACK) {
1233                 pr_debug("wRate %04X, byTsr0 %02X, byTsr1 %02X\n",
1234                          wRate, byTsr0, byTsr1);
1235                 if (pMgmt->eCurrMode == WMAC_MODE_ESS_STA) {
1236                         pMgmt->sNodeDBTable[0].uTxAttempts += 1;
1237                         if ((byTsr1 & TSR1_TERR) == 0) {
1238                                 /* transmit success, TxAttempts at least plus one */
1239                                 pMgmt->sNodeDBTable[0].uTxOk[MAX_RATE]++;
1240                                 if ((byFallBack == AUTO_FB_NONE) ||
1241                                     (wRate < RATE_18M)) {
1242                                         wFallBackRate = wRate;
1243                                 } else if (byFallBack == AUTO_FB_0) {
1244                                         if (byTxRetry < 5)
1245                                                 wFallBackRate = awHWRetry0[wRate-RATE_18M][byTxRetry];
1246                                         else
1247                                                 wFallBackRate = awHWRetry0[wRate-RATE_18M][4];
1248                                 } else if (byFallBack == AUTO_FB_1) {
1249                                         if (byTxRetry < 5)
1250                                                 wFallBackRate = awHWRetry1[wRate-RATE_18M][byTxRetry];
1251                                         else
1252                                                 wFallBackRate = awHWRetry1[wRate-RATE_18M][4];
1253                                 }
1254                                 pMgmt->sNodeDBTable[0].uTxOk[wFallBackRate]++;
1255                         } else {
1256                                 pMgmt->sNodeDBTable[0].uTxFailures++;
1257                         }
1258                         pMgmt->sNodeDBTable[0].uTxRetry += byTxRetry;
1259                         if (byTxRetry != 0) {
1260                                 pMgmt->sNodeDBTable[0].uTxFail[MAX_RATE] += byTxRetry;
1261                                 if ((byFallBack == AUTO_FB_NONE) ||
1262                                     (wRate < RATE_18M)) {
1263                                         pMgmt->sNodeDBTable[0].uTxFail[wRate] += byTxRetry;
1264                                 } else if (byFallBack == AUTO_FB_0) {
1265                                         for (ii = 0; ii < byTxRetry; ii++) {
1266                                                 if (ii < 5)
1267                                                         wFallBackRate = awHWRetry0[wRate-RATE_18M][ii];
1268                                                 else
1269                                                         wFallBackRate = awHWRetry0[wRate-RATE_18M][4];
1270                                                 pMgmt->sNodeDBTable[0].uTxFail[wFallBackRate]++;
1271                                         }
1272                                 } else if (byFallBack == AUTO_FB_1) {
1273                                         for (ii = 0; ii < byTxRetry; ii++) {
1274                                                 if (ii < 5)
1275                                                         wFallBackRate = awHWRetry1[wRate-RATE_18M][ii];
1276                                                 else
1277                                                         wFallBackRate = awHWRetry1[wRate-RATE_18M][4];
1278                                                 pMgmt->sNodeDBTable[0].uTxFail[wFallBackRate]++;
1279                                         }
1280                                 }
1281                         }
1282                 }
1283
1284                 if ((pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) ||
1285                     (pMgmt->eCurrMode == WMAC_MODE_ESS_AP)) {
1286                         pMACHeader = (PS802_11Header)(pbyBuffer + uFIFOHeaderSize);
1287
1288                         if (BSSDBbIsSTAInNodeDB((void *)pMgmt,  &(pMACHeader->abyAddr1[0]), &uNodeIndex)) {
1289                                 pMgmt->sNodeDBTable[uNodeIndex].uTxAttempts += 1;
1290                                 if ((byTsr1 & TSR1_TERR) == 0) {
1291                                         /* transmit success, TxAttempts at least plus one */
1292                                         pMgmt->sNodeDBTable[uNodeIndex].uTxOk[MAX_RATE]++;
1293                                         if ((byFallBack == AUTO_FB_NONE) ||
1294                                             (wRate < RATE_18M)) {
1295                                                 wFallBackRate = wRate;
1296                                         } else if (byFallBack == AUTO_FB_0) {
1297                                                 if (byTxRetry < 5)
1298                                                         wFallBackRate = awHWRetry0[wRate-RATE_18M][byTxRetry];
1299                                                 else
1300                                                         wFallBackRate = awHWRetry0[wRate-RATE_18M][4];
1301                                         } else if (byFallBack == AUTO_FB_1) {
1302                                                 if (byTxRetry < 5)
1303                                                         wFallBackRate = awHWRetry1[wRate-RATE_18M][byTxRetry];
1304                                                 else
1305                                                         wFallBackRate = awHWRetry1[wRate-RATE_18M][4];
1306                                         }
1307                                         pMgmt->sNodeDBTable[uNodeIndex].uTxOk[wFallBackRate]++;
1308                                 } else {
1309                                         pMgmt->sNodeDBTable[uNodeIndex].uTxFailures++;
1310                                 }
1311                                 pMgmt->sNodeDBTable[uNodeIndex].uTxRetry += byTxRetry;
1312                                 if (byTxRetry != 0) {
1313                                         pMgmt->sNodeDBTable[uNodeIndex].uTxFail[MAX_RATE] += byTxRetry;
1314                                         if ((byFallBack == AUTO_FB_NONE) ||
1315                                             (wRate < RATE_18M)) {
1316                                                 pMgmt->sNodeDBTable[uNodeIndex].uTxFail[wRate] += byTxRetry;
1317                                         } else if (byFallBack == AUTO_FB_0) {
1318                                                 for (ii = 0; ii < byTxRetry; ii++) {
1319                                                         if (ii < 5)
1320                                                                 wFallBackRate = awHWRetry0[wRate - RATE_18M][ii];
1321                                                         else
1322                                                                 wFallBackRate = awHWRetry0[wRate - RATE_18M][4];
1323                                                         pMgmt->sNodeDBTable[uNodeIndex].uTxFail[wFallBackRate]++;
1324                                                 }
1325                                         } else if (byFallBack == AUTO_FB_1) {
1326                                                 for (ii = 0; ii < byTxRetry; ii++) {
1327                                                         if (ii < 5)
1328                                                                 wFallBackRate = awHWRetry1[wRate-RATE_18M][ii];
1329                                                         else
1330                                                                 wFallBackRate = awHWRetry1[wRate-RATE_18M][4];
1331                                                         pMgmt->sNodeDBTable[uNodeIndex].uTxFail[wFallBackRate]++;
1332                                                 }
1333                                         }
1334                                 }
1335                         }
1336                 }
1337         }
1338 }
1339
1340 /*+
1341  *
1342  * Routine Description:
1343  *    Clear Nodes & skb in DB Table
1344  *
1345  *
1346  * Parameters:
1347  *  In:
1348  *      hDeviceContext        - The adapter context.
1349  *      uStartIndex           - starting index
1350  *  Out:
1351  *      none
1352  *
1353  * Return Value:
1354  *    None.
1355  *
1356  -*/
1357
1358 void
1359 BSSvClearNodeDBTable(
1360         void *hDeviceContext,
1361         unsigned int uStartIndex
1362 )
1363
1364 {
1365         struct vnt_private *pDevice = hDeviceContext;
1366         PSMgmtObject    pMgmt = pDevice->pMgmt;
1367         struct sk_buff  *skb;
1368         unsigned int ii;
1369
1370         for (ii = uStartIndex; ii < (MAX_NODE_NUM + 1); ii++) {
1371                 if (pMgmt->sNodeDBTable[ii].bActive) {
1372                         /* check if sTxPSQueue has been initial */
1373                         if (pMgmt->sNodeDBTable[ii].sTxPSQueue.next != NULL) {
1374                                 while ((skb = skb_dequeue(&pMgmt->sNodeDBTable[ii].sTxPSQueue)) != NULL) {
1375                                         pr_debug("PS skb != NULL %d\n", ii);
1376                                         dev_kfree_skb(skb);
1377                                 }
1378                         }
1379                         memset(&pMgmt->sNodeDBTable[ii], 0, sizeof(KnownNodeDB));
1380                 }
1381         }
1382
1383         return;
1384 };
1385
1386 void s_vCheckSensitivity(
1387         void *hDeviceContext
1388 )
1389 {
1390         struct vnt_private *pDevice = hDeviceContext;
1391         PKnownBSS       pBSSList = NULL;
1392         PSMgmtObject    pMgmt = pDevice->pMgmt;
1393         int             ii;
1394
1395         if ((pDevice->byLocalID <= REV_ID_VT3253_A1) && (pDevice->byRFType == RF_RFMD2959) &&
1396             (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA)) {
1397                 return;
1398         }
1399
1400         if ((pMgmt->eCurrState == WMAC_STATE_ASSOC) ||
1401             ((pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) && (pMgmt->eCurrState == WMAC_STATE_JOINTED))) {
1402                 pBSSList = BSSpAddrIsInBSSList(pDevice, pMgmt->abyCurrBSSID, (PWLAN_IE_SSID)pMgmt->abyCurrSSID);
1403                 if (pBSSList != NULL) {
1404                         /* Update BB Reg if RSSI is too strong */
1405                         long    LocalldBmAverage = 0;
1406                         long    uNumofdBm = 0;
1407
1408                         for (ii = 0; ii < RSSI_STAT_COUNT; ii++) {
1409                                 if (pBSSList->ldBmAverage[ii] != 0) {
1410                                         uNumofdBm++;
1411                                         LocalldBmAverage += pBSSList->ldBmAverage[ii];
1412                                 }
1413                         }
1414                         if (uNumofdBm > 0) {
1415                                 LocalldBmAverage = LocalldBmAverage/uNumofdBm;
1416                                 for (ii = 0; ii < BB_VGA_LEVEL; ii++) {
1417                                         pr_debug("LocalldBmAverage:%ld, %ld %02x\n",
1418                                                  LocalldBmAverage,
1419                                                  pDevice->ldBmThreshold[ii],
1420                                                  pDevice->abyBBVGA[ii]);
1421                                         if (LocalldBmAverage < pDevice->ldBmThreshold[ii]) {
1422                                                 pDevice->byBBVGANew = pDevice->abyBBVGA[ii];
1423                                                 break;
1424                                         }
1425                                 }
1426                                 if (pDevice->byBBVGANew != pDevice->byBBVGACurrent) {
1427                                         pDevice->uBBVGADiffCount++;
1428                                 } else {
1429                                         pDevice->uBBVGADiffCount = 0;
1430                                 }
1431                         }
1432                 }
1433         }
1434 }
1435
1436 void
1437 BSSvClearAnyBSSJoinRecord(
1438         void *hDeviceContext
1439 )
1440 {
1441         struct vnt_private *pDevice = hDeviceContext;
1442         PSMgmtObject    pMgmt = pDevice->pMgmt;
1443         unsigned int ii;
1444
1445         for (ii = 0; ii < MAX_BSS_NUM; ii++)
1446                 pMgmt->sBSSList[ii].bSelected = false;
1447 }
1448
1449 #ifdef Calcu_LinkQual
1450 void s_uCalculateLinkQual(
1451         void *hDeviceContext
1452 )
1453 {
1454         struct vnt_private *pDevice = hDeviceContext;
1455         unsigned long TxOkRatio, TxCnt;
1456         unsigned long RxOkRatio, RxCnt;
1457         unsigned long RssiRatio;
1458         long ldBm;
1459
1460         TxCnt = pDevice->scStatistic.TxNoRetryOkCount +
1461                 pDevice->scStatistic.TxRetryOkCount +
1462                 pDevice->scStatistic.TxFailCount;
1463         RxCnt = pDevice->scStatistic.RxFcsErrCnt +
1464                 pDevice->scStatistic.RxOkCnt;
1465         TxOkRatio = (TxCnt < 6) ? 4000 : ((pDevice->scStatistic.TxNoRetryOkCount * 4000) / TxCnt);
1466         RxOkRatio = (RxCnt < 6) ? 2000 : ((pDevice->scStatistic.RxOkCnt * 2000) / RxCnt);
1467         /* decide link quality */
1468         if (!pDevice->bLinkPass) {
1469                 pDevice->scStatistic.LinkQuality = 0;
1470                 pDevice->scStatistic.SignalStren = 0;
1471         } else {
1472                 RFvRSSITodBm(pDevice, (unsigned char)(pDevice->uCurrRSSI), &ldBm);
1473                 if (-ldBm < 50)
1474                         RssiRatio = 4000;
1475                 else if (-ldBm > 90)
1476                         RssiRatio = 0;
1477                 else
1478                         RssiRatio = (40-(-ldBm-50))*4000/40;
1479                 pDevice->scStatistic.SignalStren = RssiRatio/40;
1480                 pDevice->scStatistic.LinkQuality = (RssiRatio+TxOkRatio+RxOkRatio)/100;
1481         }
1482         pDevice->scStatistic.RxFcsErrCnt = 0;
1483         pDevice->scStatistic.RxOkCnt = 0;
1484         pDevice->scStatistic.TxFailCount = 0;
1485         pDevice->scStatistic.TxNoRetryOkCount = 0;
1486         pDevice->scStatistic.TxRetryOkCount = 0;
1487 }
1488 #endif
1489
1490 void s_vCheckPreEDThreshold(
1491         void *hDeviceContext
1492 )
1493 {
1494         struct vnt_private *pDevice = hDeviceContext;
1495         PKnownBSS       pBSSList = NULL;
1496         PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
1497
1498         if ((pMgmt->eCurrState == WMAC_STATE_ASSOC) ||
1499             ((pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) && (pMgmt->eCurrState == WMAC_STATE_JOINTED))) {
1500                 pBSSList = BSSpAddrIsInBSSList(pDevice, pMgmt->abyCurrBSSID, (PWLAN_IE_SSID)pMgmt->abyCurrSSID);
1501                 if (pBSSList != NULL)
1502                         pDevice->byBBPreEDRSSI = (unsigned char) (~(pBSSList->ldBmAverRange) + 1);
1503         }
1504 }