Staging: rt2870: prepare for rt{28,30}70/common/*.[ch] merge
[linux-2.6] / drivers / staging / rt2870 / common / ba_action.c
1 /*
2  *************************************************************************
3  * Ralink Tech Inc.
4  * 5F., No.36, Taiyuan St., Jhubei City,
5  * Hsinchu County 302,
6  * Taiwan, R.O.C.
7  *
8  * (c) Copyright 2002-2007, Ralink Technology, Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify  *
11  * it under the terms of the GNU General Public License as published by  *
12  * the Free Software Foundation; either version 2 of the License, or     *
13  * (at your option) any later version.                                   *
14  *                                                                       *
15  * This program is distributed in the hope that it will be useful,       *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  * GNU General Public License for more details.                          *
19  *                                                                       *
20  * You should have received a copy of the GNU General Public License     *
21  * along with this program; if not, write to the                         *
22  * Free Software Foundation, Inc.,                                       *
23  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
24  *                                                                       *
25  *************************************************************************
26  */
27
28
29 #include "../rt_config.h"
30
31
32
33 #define BA_ORI_INIT_SEQ         (pEntry->TxSeq[TID]) //1                        // inital sequence number of BA session
34
35 #define ORI_SESSION_MAX_RETRY   8
36 #define ORI_BA_SESSION_TIMEOUT  (2000)  // ms
37 #define REC_BA_SESSION_IDLE_TIMEOUT     (1000)  // ms
38
39 #define REORDERING_PACKET_TIMEOUT               ((100 * HZ)/1000)       // system ticks -- 100 ms
40 #define MAX_REORDERING_PACKET_TIMEOUT   ((3000 * HZ)/1000)      // system ticks -- 100 ms
41
42 #define RESET_RCV_SEQ           (0xFFFF)
43
44 static void ba_mpdu_blk_free(PRTMP_ADAPTER pAd, struct reordering_mpdu *mpdu_blk);
45
46
47 BA_ORI_ENTRY *BATableAllocOriEntry(
48                                                                   IN  PRTMP_ADAPTER   pAd,
49                                                                   OUT USHORT          *Idx);
50
51 BA_REC_ENTRY *BATableAllocRecEntry(
52                                                                   IN  PRTMP_ADAPTER   pAd,
53                                                                   OUT USHORT          *Idx);
54
55 VOID BAOriSessionSetupTimeout(
56     IN PVOID SystemSpecific1,
57     IN PVOID FunctionContext,
58     IN PVOID SystemSpecific2,
59     IN PVOID SystemSpecific3);
60
61 VOID BARecSessionIdleTimeout(
62     IN PVOID SystemSpecific1,
63     IN PVOID FunctionContext,
64     IN PVOID SystemSpecific2,
65     IN PVOID SystemSpecific3);
66
67
68 BUILD_TIMER_FUNCTION(BAOriSessionSetupTimeout);
69 BUILD_TIMER_FUNCTION(BARecSessionIdleTimeout);
70
71 #define ANNOUNCE_REORDERING_PACKET(_pAd, _mpdu_blk)     \
72                         Announce_Reordering_Packet(_pAd, _mpdu_blk);
73
74 VOID BA_MaxWinSizeReasign(
75         IN PRTMP_ADAPTER        pAd,
76         IN MAC_TABLE_ENTRY  *pEntryPeer,
77         OUT UCHAR                       *pWinSize)
78 {
79         UCHAR MaxSize;
80
81
82         if (pAd->MACVersion >= RALINK_2883_VERSION) // 3*3
83         {
84                 if (pAd->MACVersion >= RALINK_3070_VERSION)
85                 {
86                         if (pEntryPeer->WepStatus != Ndis802_11EncryptionDisabled)
87                                 MaxSize = 7; // for non-open mode
88                         else
89                                 MaxSize = 13;
90                 }
91                 else
92                         MaxSize = 31;
93         }
94         else if (pAd->MACVersion >= RALINK_2880E_VERSION) // 2880 e
95         {
96                 if (pEntryPeer->WepStatus != Ndis802_11EncryptionDisabled)
97                         MaxSize = 7; // for non-open mode
98                 else
99                         MaxSize = 13;
100         }
101         else
102                 MaxSize = 7;
103
104         DBGPRINT(RT_DEBUG_TRACE, ("ba> Win Size = %d, Max Size = %d\n",
105                         *pWinSize, MaxSize));
106
107         if ((*pWinSize) > MaxSize)
108         {
109                 DBGPRINT(RT_DEBUG_TRACE, ("ba> reassign max win size from %d to %d\n",
110                                 *pWinSize, MaxSize));
111
112                 *pWinSize = MaxSize;
113         }
114 }
115
116 void Announce_Reordering_Packet(IN PRTMP_ADAPTER                        pAd,
117                                                                 IN struct reordering_mpdu       *mpdu)
118 {
119         PNDIS_PACKET    pPacket;
120
121         pPacket = mpdu->pPacket;
122
123         if (mpdu->bAMSDU)
124         {
125                 ASSERT(0);
126                 BA_Reorder_AMSDU_Annnounce(pAd, pPacket);
127         }
128         else
129         {
130                 //
131                 // pass this 802.3 packet to upper layer or forward this packet to WM directly
132                 //
133
134                 ANNOUNCE_OR_FORWARD_802_3_PACKET(pAd, pPacket, RTMP_GET_PACKET_IF(pPacket));
135         }
136 }
137
138 /*
139  * Insert a reordering mpdu into sorted linked list by sequence no.
140  */
141 BOOLEAN ba_reordering_mpdu_insertsorted(struct reordering_list *list, struct reordering_mpdu *mpdu)
142 {
143
144         struct reordering_mpdu **ppScan = &list->next;
145
146         while (*ppScan != NULL)
147         {
148                 if (SEQ_SMALLER((*ppScan)->Sequence, mpdu->Sequence, MAXSEQ))
149                 {
150                         ppScan = &(*ppScan)->next;
151                 }
152                 else if ((*ppScan)->Sequence == mpdu->Sequence)
153                 {
154                         /* give up this duplicated frame */
155                         return(FALSE);
156                 }
157                 else
158                 {
159                         /* find position */
160                         break;
161                 }
162         }
163
164         mpdu->next = *ppScan;
165         *ppScan = mpdu;
166         list->qlen++;
167         return TRUE;
168 }
169
170
171 /*
172  * caller lock critical section if necessary
173  */
174 static inline void ba_enqueue(struct reordering_list *list, struct reordering_mpdu *mpdu_blk)
175 {
176         list->qlen++;
177         mpdu_blk->next = list->next;
178         list->next = mpdu_blk;
179 }
180
181 /*
182  * caller lock critical section if necessary
183  */
184 static inline struct reordering_mpdu * ba_dequeue(struct reordering_list *list)
185 {
186         struct reordering_mpdu *mpdu_blk = NULL;
187
188         ASSERT(list);
189
190                 if (list->qlen)
191                 {
192                         list->qlen--;
193                         mpdu_blk = list->next;
194                         if (mpdu_blk)
195                         {
196                                 list->next = mpdu_blk->next;
197                                 mpdu_blk->next = NULL;
198                         }
199                 }
200         return mpdu_blk;
201 }
202
203
204 static inline struct reordering_mpdu  *ba_reordering_mpdu_dequeue(struct reordering_list *list)
205 {
206         return(ba_dequeue(list));
207 }
208
209
210 static inline struct reordering_mpdu  *ba_reordering_mpdu_probe(struct reordering_list *list)
211         {
212         ASSERT(list);
213
214                 return(list->next);
215         }
216
217
218 /*
219  * free all resource for reordering mechanism
220  */
221 void ba_reordering_resource_release(PRTMP_ADAPTER pAd)
222 {
223         BA_TABLE        *Tab;
224         PBA_REC_ENTRY   pBAEntry;
225         struct reordering_mpdu *mpdu_blk;
226         int i;
227
228         Tab = &pAd->BATable;
229
230         /* I.  release all pending reordering packet */
231         NdisAcquireSpinLock(&pAd->BATabLock);
232         for (i = 0; i < MAX_LEN_OF_BA_REC_TABLE; i++)
233         {
234                 pBAEntry = &Tab->BARecEntry[i];
235                 if (pBAEntry->REC_BA_Status != Recipient_NONE)
236                 {
237                         while ((mpdu_blk = ba_reordering_mpdu_dequeue(&pBAEntry->list)))
238                         {
239                                 ASSERT(mpdu_blk->pPacket);
240                                 RELEASE_NDIS_PACKET(pAd, mpdu_blk->pPacket, NDIS_STATUS_FAILURE);
241                                 ba_mpdu_blk_free(pAd, mpdu_blk);
242                         }
243                 }
244         }
245         NdisReleaseSpinLock(&pAd->BATabLock);
246
247         ASSERT(pBAEntry->list.qlen == 0);
248         /* II. free memory of reordering mpdu table */
249         NdisAcquireSpinLock(&pAd->mpdu_blk_pool.lock);
250         os_free_mem(pAd, pAd->mpdu_blk_pool.mem);
251         NdisReleaseSpinLock(&pAd->mpdu_blk_pool.lock);
252 }
253
254
255
256 /*
257  * Allocate all resource for reordering mechanism
258  */
259 BOOLEAN ba_reordering_resource_init(PRTMP_ADAPTER pAd, int num)
260 {
261         int     i;
262         PUCHAR  mem;
263         struct reordering_mpdu *mpdu_blk;
264         struct reordering_list *freelist;
265
266         /* allocate spinlock */
267         NdisAllocateSpinLock(&pAd->mpdu_blk_pool.lock);
268
269         /* initialize freelist */
270         freelist = &pAd->mpdu_blk_pool.freelist;
271         freelist->next = NULL;
272         freelist->qlen = 0;
273
274         DBGPRINT(RT_DEBUG_TRACE, ("Allocate %d memory for BA reordering\n", (UINT32)(num*sizeof(struct reordering_mpdu))));
275
276         /* allocate number of mpdu_blk memory */
277         os_alloc_mem(pAd, (PUCHAR *)&mem, (num*sizeof(struct reordering_mpdu)));
278
279         pAd->mpdu_blk_pool.mem = mem;
280
281         if (mem == NULL)
282         {
283                 DBGPRINT(RT_DEBUG_ERROR, ("Can't Allocate Memory for BA Reordering\n"));
284                 return(FALSE);
285         }
286
287         /* build mpdu_blk free list */
288         for (i=0; i<num; i++)
289         {
290                 /* get mpdu_blk */
291                 mpdu_blk = (struct reordering_mpdu *) mem;
292                 /* initial mpdu_blk */
293                 NdisZeroMemory(mpdu_blk, sizeof(struct reordering_mpdu));
294                 /* next mpdu_blk */
295                 mem += sizeof(struct reordering_mpdu);
296                 /* insert mpdu_blk into freelist */
297                 ba_enqueue(freelist, mpdu_blk);
298         }
299
300         return(TRUE);
301 }
302
303 //static int blk_count=0; // sample take off, no use
304
305 static struct reordering_mpdu *ba_mpdu_blk_alloc(PRTMP_ADAPTER pAd)
306 {
307         struct reordering_mpdu *mpdu_blk;
308
309         NdisAcquireSpinLock(&pAd->mpdu_blk_pool.lock);
310         mpdu_blk = ba_dequeue(&pAd->mpdu_blk_pool.freelist);
311         if (mpdu_blk)
312         {
313 //              blk_count++;
314                 /* reset mpdu_blk */
315                 NdisZeroMemory(mpdu_blk, sizeof(struct reordering_mpdu));
316         }
317         NdisReleaseSpinLock(&pAd->mpdu_blk_pool.lock);
318         return mpdu_blk;
319 }
320
321 static void ba_mpdu_blk_free(PRTMP_ADAPTER pAd, struct reordering_mpdu *mpdu_blk)
322 {
323         ASSERT(mpdu_blk);
324
325         NdisAcquireSpinLock(&pAd->mpdu_blk_pool.lock);
326 //      blk_count--;
327         ba_enqueue(&pAd->mpdu_blk_pool.freelist, mpdu_blk);
328         NdisReleaseSpinLock(&pAd->mpdu_blk_pool.lock);
329 }
330
331
332 static USHORT ba_indicate_reordering_mpdus_in_order(
333                                                                                                    IN PRTMP_ADAPTER    pAd,
334                                                                                                    IN PBA_REC_ENTRY    pBAEntry,
335                                                                                                    IN USHORT           StartSeq)
336 {
337         struct reordering_mpdu *mpdu_blk;
338         USHORT  LastIndSeq = RESET_RCV_SEQ;
339
340         NdisAcquireSpinLock(&pBAEntry->RxReRingLock);
341
342         while ((mpdu_blk = ba_reordering_mpdu_probe(&pBAEntry->list)))
343                 {
344                         /* find in-order frame */
345                 if (!SEQ_STEPONE(mpdu_blk->Sequence, StartSeq, MAXSEQ))
346                         {
347                                 break;
348                         }
349                         /* dequeue in-order frame from reodering list */
350                         mpdu_blk = ba_reordering_mpdu_dequeue(&pBAEntry->list);
351                         /* pass this frame up */
352                 ANNOUNCE_REORDERING_PACKET(pAd, mpdu_blk);
353                 /* move to next sequence */
354                         StartSeq = mpdu_blk->Sequence;
355                 LastIndSeq = StartSeq;
356                 /* free mpdu_blk */
357                         ba_mpdu_blk_free(pAd, mpdu_blk);
358         }
359
360         NdisReleaseSpinLock(&pBAEntry->RxReRingLock);
361
362         /* update last indicated sequence */
363         return LastIndSeq;
364 }
365
366 static void ba_indicate_reordering_mpdus_le_seq(
367                                                                                            IN PRTMP_ADAPTER    pAd,
368                                                                                            IN PBA_REC_ENTRY    pBAEntry,
369                                                                                            IN USHORT           Sequence)
370 {
371         struct reordering_mpdu *mpdu_blk;
372
373         NdisAcquireSpinLock(&pBAEntry->RxReRingLock);
374         while ((mpdu_blk = ba_reordering_mpdu_probe(&pBAEntry->list)))
375                 {
376                         /* find in-order frame */
377                 if ((mpdu_blk->Sequence == Sequence) || SEQ_SMALLER(mpdu_blk->Sequence, Sequence, MAXSEQ))
378                 {
379                         /* dequeue in-order frame from reodering list */
380                         mpdu_blk = ba_reordering_mpdu_dequeue(&pBAEntry->list);
381                         /* pass this frame up */
382                         ANNOUNCE_REORDERING_PACKET(pAd, mpdu_blk);
383                         /* free mpdu_blk */
384                         ba_mpdu_blk_free(pAd, mpdu_blk);
385                 }
386                 else
387                         {
388                                 break;
389                         }
390         }
391         NdisReleaseSpinLock(&pBAEntry->RxReRingLock);
392 }
393
394
395 static void ba_refresh_reordering_mpdus(
396                                                                            IN PRTMP_ADAPTER    pAd,
397                                                                            PBA_REC_ENTRY       pBAEntry)
398 {
399         struct reordering_mpdu *mpdu_blk;
400
401         NdisAcquireSpinLock(&pBAEntry->RxReRingLock);
402
403                         /* dequeue in-order frame from reodering list */
404         while ((mpdu_blk = ba_reordering_mpdu_dequeue(&pBAEntry->list)))
405         {
406                         /* pass this frame up */
407                 ANNOUNCE_REORDERING_PACKET(pAd, mpdu_blk);
408
409                 pBAEntry->LastIndSeq = mpdu_blk->Sequence;
410                         ba_mpdu_blk_free(pAd, mpdu_blk);
411
412                 /* update last indicated sequence */
413         }
414         ASSERT(pBAEntry->list.qlen == 0);
415         pBAEntry->LastIndSeq = RESET_RCV_SEQ;
416         NdisReleaseSpinLock(&pBAEntry->RxReRingLock);
417 }
418
419
420 //static
421 void ba_flush_reordering_timeout_mpdus(
422                                                                         IN PRTMP_ADAPTER    pAd,
423                                                                         IN PBA_REC_ENTRY    pBAEntry,
424                                                                         IN ULONG            Now32)
425
426 {
427         USHORT Sequence;
428
429 //      if ((RTMP_TIME_AFTER((unsigned long)Now32, (unsigned long)(pBAEntry->LastIndSeqAtTimer+REORDERING_PACKET_TIMEOUT)) &&
430 //               (pBAEntry->list.qlen > ((pBAEntry->BAWinSize*7)/8))) //||
431 //              (RTMP_TIME_AFTER((unsigned long)Now32, (unsigned long)(pBAEntry->LastIndSeqAtTimer+(10*REORDERING_PACKET_TIMEOUT))) &&
432 //               (pBAEntry->list.qlen > (pBAEntry->BAWinSize/8)))
433         if (RTMP_TIME_AFTER((unsigned long)Now32, (unsigned long)(pBAEntry->LastIndSeqAtTimer+(MAX_REORDERING_PACKET_TIMEOUT/6)))
434                  &&(pBAEntry->list.qlen > 1)
435                 )
436         {
437                 DBGPRINT(RT_DEBUG_TRACE,("timeout[%d] (%08lx-%08lx = %d > %d): %x, flush all!\n ", pBAEntry->list.qlen, Now32, (pBAEntry->LastIndSeqAtTimer),
438                            (int)((long) Now32 - (long)(pBAEntry->LastIndSeqAtTimer)), MAX_REORDERING_PACKET_TIMEOUT,
439                            pBAEntry->LastIndSeq));
440                 ba_refresh_reordering_mpdus(pAd, pBAEntry);
441                 pBAEntry->LastIndSeqAtTimer = Now32;
442         }
443         else
444         if (RTMP_TIME_AFTER((unsigned long)Now32, (unsigned long)(pBAEntry->LastIndSeqAtTimer+(REORDERING_PACKET_TIMEOUT)))
445                 && (pBAEntry->list.qlen > 0)
446            )
447                 {
448                 //
449                 // force LastIndSeq to shift to LastIndSeq+1
450                 //
451                 Sequence = (pBAEntry->LastIndSeq+1) & MAXSEQ;
452                 ba_indicate_reordering_mpdus_le_seq(pAd, pBAEntry, Sequence);
453                 pBAEntry->LastIndSeqAtTimer = Now32;
454                         pBAEntry->LastIndSeq = Sequence;
455                 //
456                 // indicate in-order mpdus
457                 //
458                 Sequence = ba_indicate_reordering_mpdus_in_order(pAd, pBAEntry, Sequence);
459                 if (Sequence != RESET_RCV_SEQ)
460                 {
461                         pBAEntry->LastIndSeq = Sequence;
462                 }
463
464         }
465 }
466
467
468 /*
469  * generate ADDBA request to
470  * set up BA agreement
471  */
472 VOID BAOriSessionSetUp(
473                                           IN PRTMP_ADAPTER    pAd,
474                                           IN MAC_TABLE_ENTRY  *pEntry,
475                                           IN UCHAR            TID,
476                                           IN USHORT           TimeOut,
477                                           IN ULONG            DelayTime,
478                                           IN BOOLEAN          isForced)
479
480 {
481         //MLME_ADDBA_REQ_STRUCT AddbaReq;
482         BA_ORI_ENTRY            *pBAEntry = NULL;
483         USHORT                  Idx;
484         BOOLEAN                 Cancelled;
485
486         if ((pAd->CommonCfg.BACapability.field.AutoBA != TRUE)  &&  (isForced == FALSE))
487                 return;
488
489         // if this entry is limited to use legacy tx mode, it doesn't generate BA.
490         if (RTMPStaFixedTxMode(pAd, pEntry) != FIXED_TXMODE_HT)
491                 return;
492
493         if ((pEntry->BADeclineBitmap & (1<<TID)) && (isForced == FALSE))
494         {
495                 // try again after 3 secs
496                 DelayTime = 3000;
497 //              printk("DeCline BA from Peer\n");
498 //              return;
499         }
500
501
502         Idx = pEntry->BAOriWcidArray[TID];
503         if (Idx == 0)
504         {
505                 // allocate a BA session
506                 pBAEntry = BATableAllocOriEntry(pAd, &Idx);
507                 if (pBAEntry == NULL)
508                 {
509                         DBGPRINT(RT_DEBUG_TRACE,("ADDBA - MlmeADDBAAction() allocate BA session failed \n"));
510                         return;
511                 }
512         }
513         else
514         {
515                 pBAEntry =&pAd->BATable.BAOriEntry[Idx];
516         }
517
518         if (pBAEntry->ORI_BA_Status >= Originator_WaitRes)
519         {
520                 return;
521         }
522
523         pEntry->BAOriWcidArray[TID] = Idx;
524
525         // Initialize BA session
526         pBAEntry->ORI_BA_Status = Originator_WaitRes;
527         pBAEntry->Wcid = pEntry->Aid;
528         pBAEntry->BAWinSize = pAd->CommonCfg.BACapability.field.RxBAWinLimit;
529         pBAEntry->Sequence = BA_ORI_INIT_SEQ;
530         pBAEntry->Token = 1;    // (2008-01-21) Jan Lee recommends it - this token can't be 0
531         pBAEntry->TID = TID;
532         pBAEntry->TimeOutValue = TimeOut;
533         pBAEntry->pAdapter = pAd;
534
535 #ifdef RT30xx
536         DBGPRINT(RT_DEBUG_TRACE,("Send AddBA to %02x:%02x:%02x:%02x:%02x:%02x Tid:%d isForced:%d Wcid:%d\n"
537                 ,pEntry->Addr[0],pEntry->Addr[1],pEntry->Addr[2]
538                 ,pEntry->Addr[3],pEntry->Addr[4],pEntry->Addr[5]
539                 ,TID,isForced,pEntry->Aid));
540 #endif
541
542         if (!(pEntry->TXBAbitmap & (1<<TID)))
543         {
544                 RTMPInitTimer(pAd, &pBAEntry->ORIBATimer, GET_TIMER_FUNCTION(BAOriSessionSetupTimeout), pBAEntry, FALSE);
545         }
546         else
547                 RTMPCancelTimer(&pBAEntry->ORIBATimer, &Cancelled);
548
549         // set timer to send ADDBA request
550         RTMPSetTimer(&pBAEntry->ORIBATimer, DelayTime);
551 }
552
553 VOID BAOriSessionAdd(
554                         IN PRTMP_ADAPTER    pAd,
555                                         IN MAC_TABLE_ENTRY  *pEntry,
556                         IN PFRAME_ADDBA_RSP pFrame)
557 {
558         BA_ORI_ENTRY  *pBAEntry = NULL;
559         BOOLEAN       Cancelled;
560         UCHAR         TID;
561         USHORT        Idx;
562         PUCHAR          pOutBuffer2 = NULL;
563         NDIS_STATUS     NStatus;
564         ULONG           FrameLen;
565         FRAME_BAR       FrameBar;
566
567         TID = pFrame->BaParm.TID;
568         Idx = pEntry->BAOriWcidArray[TID];
569         pBAEntry =&pAd->BATable.BAOriEntry[Idx];
570
571         // Start fill in parameters.
572         if ((Idx !=0) && (pBAEntry->TID == TID) && (pBAEntry->ORI_BA_Status == Originator_WaitRes))
573         {
574                 pBAEntry->BAWinSize = min(pBAEntry->BAWinSize, ((UCHAR)pFrame->BaParm.BufSize));
575                 BA_MaxWinSizeReasign(pAd, pEntry, &pBAEntry->BAWinSize);
576
577                 pBAEntry->TimeOutValue = pFrame->TimeOutValue;
578                 pBAEntry->ORI_BA_Status = Originator_Done;
579                 // reset sequence number
580                 pBAEntry->Sequence = BA_ORI_INIT_SEQ;
581                 // Set Bitmap flag.
582                 pEntry->TXBAbitmap |= (1<<TID);
583                                 RTMPCancelTimer(&pBAEntry->ORIBATimer, &Cancelled);
584
585                 pBAEntry->ORIBATimer.TimerValue = 0;    //pFrame->TimeOutValue;
586
587                 DBGPRINT(RT_DEBUG_TRACE,("%s : TXBAbitmap = %x, BAWinSize = %d, TimeOut = %ld\n", __func__, pEntry->TXBAbitmap,
588                                                                  pBAEntry->BAWinSize, pBAEntry->ORIBATimer.TimerValue));
589
590                 // SEND BAR ;
591                 NStatus = MlmeAllocateMemory(pAd, &pOutBuffer2);  //Get an unused nonpaged memory
592                 if (NStatus != NDIS_STATUS_SUCCESS)
593                 {
594                         DBGPRINT(RT_DEBUG_TRACE,("BA - BAOriSessionAdd() allocate memory failed \n"));
595                         return;
596                 }
597
598                 BarHeaderInit(pAd, &FrameBar, pAd->MacTab.Content[pBAEntry->Wcid].Addr, pAd->CurrentAddress);
599
600                 FrameBar.StartingSeq.field.FragNum = 0; // make sure sequence not clear in DEL function.
601                 FrameBar.StartingSeq.field.StartSeq = pBAEntry->Sequence; // make sure sequence not clear in DEL funciton.
602                 FrameBar.BarControl.TID = pBAEntry->TID; // make sure sequence not clear in DEL funciton.
603                 MakeOutgoingFrame(pOutBuffer2,              &FrameLen,
604                                                   sizeof(FRAME_BAR),      &FrameBar,
605                                           END_OF_ARGS);
606                 MiniportMMRequest(pAd, QID_AC_BE, pOutBuffer2, FrameLen);
607                 MlmeFreeMemory(pAd, pOutBuffer2);
608
609
610                 if (pBAEntry->ORIBATimer.TimerValue)
611                         RTMPSetTimer(&pBAEntry->ORIBATimer, pBAEntry->ORIBATimer.TimerValue); // in mSec
612         }
613 }
614
615 BOOLEAN BARecSessionAdd(
616                                            IN PRTMP_ADAPTER    pAd,
617                                            IN MAC_TABLE_ENTRY  *pEntry,
618                                            IN PFRAME_ADDBA_REQ pFrame)
619 {
620         BA_REC_ENTRY            *pBAEntry = NULL;
621         BOOLEAN                 Status = TRUE;
622         BOOLEAN                 Cancelled;
623         USHORT                  Idx;
624         UCHAR                   TID;
625         UCHAR                   BAWinSize;
626         //UINT32                  Value;
627         //UINT                    offset;
628
629
630         ASSERT(pEntry);
631
632         // find TID
633         TID = pFrame->BaParm.TID;
634
635         BAWinSize = min(((UCHAR)pFrame->BaParm.BufSize), (UCHAR)pAd->CommonCfg.BACapability.field.RxBAWinLimit);
636
637         // Intel patch
638         if (BAWinSize == 0)
639         {
640                 BAWinSize = 64;
641         }
642
643         Idx = pEntry->BARecWcidArray[TID];
644
645
646         if (Idx == 0)
647         {
648                 pBAEntry = BATableAllocRecEntry(pAd, &Idx);
649         }
650         else
651         {
652                 pBAEntry = &pAd->BATable.BARecEntry[Idx];
653                 // flush all pending reordering mpdus
654                 ba_refresh_reordering_mpdus(pAd, pBAEntry);
655         }
656
657         DBGPRINT(RT_DEBUG_TRACE,("%s(%ld): Idx = %d, BAWinSize(req %d) = %d\n", __func__, pAd->BATable.numAsRecipient, Idx,
658                                                          pFrame->BaParm.BufSize, BAWinSize));
659
660         // Start fill in parameters.
661         if (pBAEntry != NULL)
662         {
663                 ASSERT(pBAEntry->list.qlen == 0);
664
665                 pBAEntry->REC_BA_Status = Recipient_HandleRes;
666                 pBAEntry->BAWinSize = BAWinSize;
667                 pBAEntry->Wcid = pEntry->Aid;
668                 pBAEntry->TID = TID;
669                 pBAEntry->TimeOutValue = pFrame->TimeOutValue;
670                 pBAEntry->REC_BA_Status = Recipient_Accept;
671                 // initial sequence number
672                 pBAEntry->LastIndSeq = RESET_RCV_SEQ; //pFrame->BaStartSeq.field.StartSeq;
673
674                 printk("Start Seq = %08x\n",  pFrame->BaStartSeq.field.StartSeq);
675
676                 if (pEntry->RXBAbitmap & (1<<TID))
677                 {
678                         RTMPCancelTimer(&pBAEntry->RECBATimer, &Cancelled);
679                 }
680                 else
681                 {
682                         RTMPInitTimer(pAd, &pBAEntry->RECBATimer, GET_TIMER_FUNCTION(BARecSessionIdleTimeout), pBAEntry, TRUE);
683                 }
684
685                 // Set Bitmap flag.
686                 pEntry->RXBAbitmap |= (1<<TID);
687                 pEntry->BARecWcidArray[TID] = Idx;
688
689                 pEntry->BADeclineBitmap &= ~(1<<TID);
690
691                 // Set BA session mask in WCID table.
692                 RT28XX_ADD_BA_SESSION_TO_ASIC(pAd, pEntry->Aid, TID);
693
694                 DBGPRINT(RT_DEBUG_TRACE,("MACEntry[%d]RXBAbitmap = 0x%x. BARecWcidArray=%d\n",
695                                 pEntry->Aid, pEntry->RXBAbitmap, pEntry->BARecWcidArray[TID]));
696         }
697         else
698         {
699                 Status = FALSE;
700                 DBGPRINT(RT_DEBUG_TRACE,("Can't Accept ADDBA for %02x:%02x:%02x:%02x:%02x:%02x TID = %d\n",
701                                 PRINT_MAC(pEntry->Addr), TID));
702         }
703         return(Status);
704 }
705
706
707 BA_REC_ENTRY *BATableAllocRecEntry(
708                                                                   IN  PRTMP_ADAPTER   pAd,
709                                                                   OUT USHORT          *Idx)
710 {
711         int             i;
712         BA_REC_ENTRY    *pBAEntry = NULL;
713
714
715         NdisAcquireSpinLock(&pAd->BATabLock);
716
717         if (pAd->BATable.numAsRecipient >= MAX_BARECI_SESSION)
718         {
719                 printk("BA Recipeint Session (%ld) > %d\n", pAd->BATable.numAsRecipient,
720                         MAX_BARECI_SESSION);
721                 goto done;
722         }
723
724         // reserve idx 0 to identify BAWcidArray[TID] as empty
725         for (i=1; i < MAX_LEN_OF_BA_REC_TABLE; i++)
726         {
727                 pBAEntry =&pAd->BATable.BARecEntry[i];
728                 if ((pBAEntry->REC_BA_Status == Recipient_NONE))
729                 {
730                         // get one
731                         pAd->BATable.numAsRecipient++;
732                         pBAEntry->REC_BA_Status = Recipient_USED;
733                         *Idx = i;
734                         break;
735                 }
736         }
737
738 done:
739         NdisReleaseSpinLock(&pAd->BATabLock);
740         return pBAEntry;
741 }
742
743 BA_ORI_ENTRY *BATableAllocOriEntry(
744                                                                   IN  PRTMP_ADAPTER   pAd,
745                                                                   OUT USHORT          *Idx)
746 {
747         int             i;
748         BA_ORI_ENTRY    *pBAEntry = NULL;
749
750         NdisAcquireSpinLock(&pAd->BATabLock);
751
752         if (pAd->BATable.numAsOriginator >= (MAX_LEN_OF_BA_ORI_TABLE))
753         {
754                 goto done;
755         }
756
757         // reserve idx 0 to identify BAWcidArray[TID] as empty
758         for (i=1; i<MAX_LEN_OF_BA_ORI_TABLE; i++)
759         {
760                 pBAEntry =&pAd->BATable.BAOriEntry[i];
761                 if ((pBAEntry->ORI_BA_Status == Originator_NONE))
762                 {
763                         // get one
764                         pAd->BATable.numAsOriginator++;
765                         pBAEntry->ORI_BA_Status = Originator_USED;
766                         pBAEntry->pAdapter = pAd;
767                         *Idx = i;
768                         break;
769                 }
770         }
771
772 done:
773         NdisReleaseSpinLock(&pAd->BATabLock);
774         return pBAEntry;
775 }
776
777
778 VOID BATableFreeOriEntry(
779                                                 IN  PRTMP_ADAPTER   pAd,
780                                                 IN  ULONG           Idx)
781 {
782         BA_ORI_ENTRY    *pBAEntry = NULL;
783         MAC_TABLE_ENTRY *pEntry;
784
785
786         if ((Idx == 0) || (Idx >= MAX_LEN_OF_BA_ORI_TABLE))
787                 return;
788
789         pBAEntry =&pAd->BATable.BAOriEntry[Idx];
790
791         if (pBAEntry->ORI_BA_Status != Originator_NONE)
792         {
793                 pEntry = &pAd->MacTab.Content[pBAEntry->Wcid];
794                 pEntry->BAOriWcidArray[pBAEntry->TID] = 0;
795
796
797                 NdisAcquireSpinLock(&pAd->BATabLock);
798                 if (pBAEntry->ORI_BA_Status == Originator_Done)
799                 {
800                         pEntry->TXBAbitmap &= (~(1<<(pBAEntry->TID) ));
801                         DBGPRINT(RT_DEBUG_TRACE, ("BATableFreeOriEntry numAsOriginator= %ld\n", pAd->BATable.numAsRecipient));
802                         // Erase Bitmap flag.
803                 }
804
805                 ASSERT(pAd->BATable.numAsOriginator != 0);
806
807                 pAd->BATable.numAsOriginator -= 1;
808
809                 pBAEntry->ORI_BA_Status = Originator_NONE;
810                 pBAEntry->Token = 0;
811                 NdisReleaseSpinLock(&pAd->BATabLock);
812         }
813 }
814
815
816 VOID BATableFreeRecEntry(
817                                                 IN  PRTMP_ADAPTER   pAd,
818                                                 IN  ULONG           Idx)
819 {
820         BA_REC_ENTRY    *pBAEntry = NULL;
821         MAC_TABLE_ENTRY *pEntry;
822
823
824         if ((Idx == 0) || (Idx >= MAX_LEN_OF_BA_REC_TABLE))
825                 return;
826
827         pBAEntry =&pAd->BATable.BARecEntry[Idx];
828
829         if (pBAEntry->REC_BA_Status != Recipient_NONE)
830         {
831                 pEntry = &pAd->MacTab.Content[pBAEntry->Wcid];
832                 pEntry->BARecWcidArray[pBAEntry->TID] = 0;
833
834                 NdisAcquireSpinLock(&pAd->BATabLock);
835
836                 ASSERT(pAd->BATable.numAsRecipient != 0);
837
838                 pAd->BATable.numAsRecipient -= 1;
839
840                 pBAEntry->REC_BA_Status = Recipient_NONE;
841                 NdisReleaseSpinLock(&pAd->BATabLock);
842         }
843 }
844
845
846 VOID BAOriSessionTearDown(
847                                                  IN OUT  PRTMP_ADAPTER   pAd,
848                                                  IN      UCHAR           Wcid,
849                                                  IN      UCHAR           TID,
850                                                  IN      BOOLEAN         bPassive,
851                                                  IN      BOOLEAN         bForceSend)
852 {
853         ULONG           Idx = 0;
854         BA_ORI_ENTRY    *pBAEntry;
855         BOOLEAN         Cancelled;
856
857         if (Wcid >= MAX_LEN_OF_MAC_TABLE)
858         {
859                 return;
860         }
861
862         //
863         // Locate corresponding BA Originator Entry in BA Table with the (pAddr,TID).
864         //
865         Idx = pAd->MacTab.Content[Wcid].BAOriWcidArray[TID];
866         if ((Idx == 0) || (Idx >= MAX_LEN_OF_BA_ORI_TABLE))
867         {
868                 if (bForceSend == TRUE)
869                 {
870                         // force send specified TID DelBA
871                         MLME_DELBA_REQ_STRUCT   DelbaReq;
872                         MLME_QUEUE_ELEM *Elem = (MLME_QUEUE_ELEM *) kmalloc(sizeof(MLME_QUEUE_ELEM), MEM_ALLOC_FLAG);
873
874                         NdisZeroMemory(&DelbaReq, sizeof(DelbaReq));
875                         NdisZeroMemory(Elem, sizeof(MLME_QUEUE_ELEM));
876
877                         COPY_MAC_ADDR(DelbaReq.Addr, pAd->MacTab.Content[Wcid].Addr);
878                         DelbaReq.Wcid = Wcid;
879                         DelbaReq.TID = TID;
880                         DelbaReq.Initiator = ORIGINATOR;
881 #if 1
882                         Elem->MsgLen  = sizeof(DelbaReq);
883                         NdisMoveMemory(Elem->Msg, &DelbaReq, sizeof(DelbaReq));
884                         MlmeDELBAAction(pAd, Elem);
885                         kfree(Elem);
886 #else
887                         MlmeEnqueue(pAd, ACTION_STATE_MACHINE, MT2_MLME_ORI_DELBA_CATE, sizeof(MLME_DELBA_REQ_STRUCT), (PVOID)&DelbaReq);
888                         RT28XX_MLME_HANDLER(pAd);
889 #endif
890                 }
891
892                 return;
893         }
894
895         DBGPRINT(RT_DEBUG_TRACE,("%s===>Wcid=%d.TID=%d \n", __func__, Wcid, TID));
896
897         pBAEntry = &pAd->BATable.BAOriEntry[Idx];
898         DBGPRINT(RT_DEBUG_TRACE,("\t===>Idx = %ld, Wcid=%d.TID=%d, ORI_BA_Status = %d \n", Idx, Wcid, TID, pBAEntry->ORI_BA_Status));
899         //
900         // Prepare DelBA action frame and send to the peer.
901         //
902         if ((bPassive == FALSE) && (TID == pBAEntry->TID) && (pBAEntry->ORI_BA_Status == Originator_Done))
903         {
904                 MLME_DELBA_REQ_STRUCT   DelbaReq;
905                 MLME_QUEUE_ELEM *Elem = (MLME_QUEUE_ELEM *) kmalloc(sizeof(MLME_QUEUE_ELEM), MEM_ALLOC_FLAG);
906
907                 NdisZeroMemory(&DelbaReq, sizeof(DelbaReq));
908                 NdisZeroMemory(Elem, sizeof(MLME_QUEUE_ELEM));
909
910                 COPY_MAC_ADDR(DelbaReq.Addr, pAd->MacTab.Content[Wcid].Addr);
911                 DelbaReq.Wcid = Wcid;
912                 DelbaReq.TID = pBAEntry->TID;
913                 DelbaReq.Initiator = ORIGINATOR;
914 #if 1
915                 Elem->MsgLen  = sizeof(DelbaReq);
916                 NdisMoveMemory(Elem->Msg, &DelbaReq, sizeof(DelbaReq));
917                 MlmeDELBAAction(pAd, Elem);
918                 kfree(Elem);
919 #else
920                 MlmeEnqueue(pAd, ACTION_STATE_MACHINE, MT2_MLME_ORI_DELBA_CATE, sizeof(MLME_DELBA_REQ_STRUCT), (PVOID)&DelbaReq);
921                 RT28XX_MLME_HANDLER(pAd);
922 #endif
923         }
924         RTMPCancelTimer(&pBAEntry->ORIBATimer, &Cancelled);
925         BATableFreeOriEntry(pAd, Idx);
926
927         if (bPassive)
928         {
929                 //BAOriSessionSetUp(pAd, &pAd->MacTab.Content[Wcid], TID, 0, 10000, TRUE);
930         }
931 }
932
933 VOID BARecSessionTearDown(
934                                                  IN OUT  PRTMP_ADAPTER   pAd,
935                                                  IN      UCHAR           Wcid,
936                                                  IN      UCHAR           TID,
937                                                  IN      BOOLEAN         bPassive)
938 {
939         ULONG           Idx = 0;
940         BA_REC_ENTRY    *pBAEntry;
941
942         if (Wcid >= MAX_LEN_OF_MAC_TABLE)
943         {
944                 return;
945         }
946
947         //
948         //  Locate corresponding BA Originator Entry in BA Table with the (pAddr,TID).
949         //
950         Idx = pAd->MacTab.Content[Wcid].BARecWcidArray[TID];
951         if (Idx == 0)
952                 return;
953
954         DBGPRINT(RT_DEBUG_TRACE,("%s===>Wcid=%d.TID=%d \n", __func__, Wcid, TID));
955
956
957         pBAEntry = &pAd->BATable.BARecEntry[Idx];
958         DBGPRINT(RT_DEBUG_TRACE,("\t===>Idx = %ld, Wcid=%d.TID=%d, REC_BA_Status = %d \n", Idx, Wcid, TID, pBAEntry->REC_BA_Status));
959         //
960         // Prepare DelBA action frame and send to the peer.
961         //
962         if ((TID == pBAEntry->TID) && (pBAEntry->REC_BA_Status == Recipient_Accept))
963         {
964                 MLME_DELBA_REQ_STRUCT   DelbaReq;
965                 BOOLEAN                                 Cancelled;
966                 MLME_QUEUE_ELEM *Elem = (MLME_QUEUE_ELEM *) kmalloc(sizeof(MLME_QUEUE_ELEM), MEM_ALLOC_FLAG);
967                 //ULONG   offset;
968                 //UINT32  VALUE;
969
970                 RTMPCancelTimer(&pBAEntry->RECBATimer, &Cancelled);
971
972                 //
973                 // 1. Send DELBA Action Frame
974                 //
975                 if (bPassive == FALSE)
976                 {
977                         NdisZeroMemory(&DelbaReq, sizeof(DelbaReq));
978                         NdisZeroMemory(Elem, sizeof(MLME_QUEUE_ELEM));
979
980                         COPY_MAC_ADDR(DelbaReq.Addr, pAd->MacTab.Content[Wcid].Addr);
981                         DelbaReq.Wcid = Wcid;
982                         DelbaReq.TID = TID;
983                         DelbaReq.Initiator = RECIPIENT;
984 #if 1
985                         Elem->MsgLen  = sizeof(DelbaReq);
986                         NdisMoveMemory(Elem->Msg, &DelbaReq, sizeof(DelbaReq));
987                         MlmeDELBAAction(pAd, Elem);
988                         kfree(Elem);
989 #else
990                         MlmeEnqueue(pAd, ACTION_STATE_MACHINE, MT2_MLME_ORI_DELBA_CATE, sizeof(MLME_DELBA_REQ_STRUCT), (PVOID)&DelbaReq);
991                         RT28XX_MLME_HANDLER(pAd);
992 #endif
993                 }
994
995
996                 //
997                 // 2. Free resource of BA session
998                 //
999                 // flush all pending reordering mpdus
1000                 ba_refresh_reordering_mpdus(pAd, pBAEntry);
1001
1002                 NdisAcquireSpinLock(&pAd->BATabLock);
1003
1004                 // Erase Bitmap flag.
1005                 pBAEntry->LastIndSeq = RESET_RCV_SEQ;
1006                 pBAEntry->BAWinSize = 0;
1007                 // Erase Bitmap flag at software mactable
1008                 pAd->MacTab.Content[Wcid].RXBAbitmap &= (~(1<<(pBAEntry->TID)));
1009                 pAd->MacTab.Content[Wcid].BARecWcidArray[TID] = 0;
1010
1011                 RT28XX_DEL_BA_SESSION_FROM_ASIC(pAd, Wcid, TID);
1012
1013                 NdisReleaseSpinLock(&pAd->BATabLock);
1014
1015         }
1016
1017         BATableFreeRecEntry(pAd, Idx);
1018 }
1019
1020 VOID BASessionTearDownALL(
1021                                                  IN OUT  PRTMP_ADAPTER pAd,
1022                                                  IN      UCHAR Wcid)
1023 {
1024         int i;
1025
1026         for (i=0; i<NUM_OF_TID; i++)
1027         {
1028                 BAOriSessionTearDown(pAd, Wcid, i, FALSE, FALSE);
1029                 BARecSessionTearDown(pAd, Wcid, i, FALSE);
1030         }
1031 }
1032
1033
1034 /*
1035         ==========================================================================
1036         Description:
1037                 Retry sending ADDBA Reqest.
1038
1039         IRQL = DISPATCH_LEVEL
1040
1041         Parametrs:
1042         p8023Header: if this is already 802.3 format, p8023Header is NULL
1043
1044         Return  : TRUE if put into rx reordering buffer, shouldn't indicaterxhere.
1045                                 FALSE , then continue indicaterx at this moment.
1046         ==========================================================================
1047  */
1048 VOID BAOriSessionSetupTimeout(
1049     IN PVOID SystemSpecific1,
1050     IN PVOID FunctionContext,
1051     IN PVOID SystemSpecific2,
1052     IN PVOID SystemSpecific3)
1053 {
1054         BA_ORI_ENTRY    *pBAEntry = (BA_ORI_ENTRY *)FunctionContext;
1055         MAC_TABLE_ENTRY *pEntry;
1056         PRTMP_ADAPTER   pAd;
1057
1058         if (pBAEntry == NULL)
1059                 return;
1060
1061         pAd = pBAEntry->pAdapter;
1062
1063         // Do nothing if monitor mode is on
1064         if (MONITOR_ON(pAd))
1065                 return;
1066
1067         pEntry = &pAd->MacTab.Content[pBAEntry->Wcid];
1068
1069         if ((pBAEntry->ORI_BA_Status == Originator_WaitRes) && (pBAEntry->Token < ORI_SESSION_MAX_RETRY))
1070         {
1071                 MLME_ADDBA_REQ_STRUCT    AddbaReq;
1072
1073                 NdisZeroMemory(&AddbaReq, sizeof(AddbaReq));
1074                 COPY_MAC_ADDR(AddbaReq.pAddr, pEntry->Addr);
1075                 AddbaReq.Wcid = (UCHAR)(pEntry->Aid);
1076                 AddbaReq.TID = pBAEntry->TID;
1077                 AddbaReq.BaBufSize = pAd->CommonCfg.BACapability.field.RxBAWinLimit;
1078                 AddbaReq.TimeOutValue = 0;
1079                 AddbaReq.Token = pBAEntry->Token;
1080                 MlmeEnqueue(pAd, ACTION_STATE_MACHINE, MT2_MLME_ADD_BA_CATE, sizeof(MLME_ADDBA_REQ_STRUCT), (PVOID)&AddbaReq);
1081                 RT28XX_MLME_HANDLER(pAd);
1082 #ifndef RT30xx
1083                 DBGPRINT(RT_DEBUG_TRACE,("BA Ori Session Timeout(%d) : Send ADD BA again\n", pBAEntry->Token));
1084 #endif
1085 #ifdef RT30xx
1086                 DBGPRINT(RT_DEBUG_TRACE,("BA Ori Session Timeout(%d) to %02x:%02x:%02x:%02x:%02x:%02x Tid:%d Wcid:%d\n"
1087                 ,pBAEntry->Token
1088                 ,pEntry->Addr[0],pEntry->Addr[1],pEntry->Addr[2]
1089                 ,pEntry->Addr[3],pEntry->Addr[4],pEntry->Addr[5]
1090                 ,pBAEntry->TID,pEntry->Aid));
1091 #endif
1092                 pBAEntry->Token++;
1093                 RTMPSetTimer(&pBAEntry->ORIBATimer, ORI_BA_SESSION_TIMEOUT);
1094         }
1095         else
1096         {
1097                 BATableFreeOriEntry(pAd, pEntry->BAOriWcidArray[pBAEntry->TID]);
1098         }
1099 }
1100
1101 /*
1102         ==========================================================================
1103         Description:
1104                 Retry sending ADDBA Reqest.
1105
1106         IRQL = DISPATCH_LEVEL
1107
1108         Parametrs:
1109         p8023Header: if this is already 802.3 format, p8023Header is NULL
1110
1111         Return  : TRUE if put into rx reordering buffer, shouldn't indicaterxhere.
1112                                 FALSE , then continue indicaterx at this moment.
1113         ==========================================================================
1114  */
1115 VOID BARecSessionIdleTimeout(
1116     IN PVOID SystemSpecific1,
1117     IN PVOID FunctionContext,
1118     IN PVOID SystemSpecific2,
1119     IN PVOID SystemSpecific3)
1120 {
1121
1122         BA_REC_ENTRY    *pBAEntry = (BA_REC_ENTRY *)FunctionContext;
1123         PRTMP_ADAPTER   pAd;
1124         ULONG           Now32;
1125
1126         if (pBAEntry == NULL)
1127                 return;
1128
1129         if ((pBAEntry->REC_BA_Status == Recipient_Accept))
1130         {
1131                 NdisGetSystemUpTime(&Now32);
1132
1133                 if (RTMP_TIME_AFTER((unsigned long)Now32, (unsigned long)(pBAEntry->LastIndSeqAtTimer + REC_BA_SESSION_IDLE_TIMEOUT)))
1134                 {
1135                         pAd = pBAEntry->pAdapter;
1136                         // flush all pending reordering mpdus
1137                         ba_refresh_reordering_mpdus(pAd, pBAEntry);
1138                         printk("%ld: REC BA session Timeout\n", Now32);
1139                 }
1140         }
1141 }
1142
1143
1144 VOID PeerAddBAReqAction(
1145         IN PRTMP_ADAPTER pAd,
1146         IN MLME_QUEUE_ELEM *Elem)
1147
1148 {
1149         //      7.4.4.1
1150         //ULONG Idx;
1151         UCHAR   Status = 1;
1152         UCHAR   pAddr[6];
1153         FRAME_ADDBA_RSP ADDframe;
1154         PUCHAR         pOutBuffer = NULL;
1155         NDIS_STATUS     NStatus;
1156         PFRAME_ADDBA_REQ  pAddreqFrame = NULL;
1157         //UCHAR         BufSize;
1158         ULONG       FrameLen;
1159         PULONG      ptemp;
1160         PMAC_TABLE_ENTRY        pMacEntry;
1161
1162         DBGPRINT(RT_DEBUG_TRACE, ("%s ==> (Wcid = %d)\n", __func__, Elem->Wcid));
1163
1164         //hex_dump("AddBAReq", Elem->Msg, Elem->MsgLen);
1165
1166         //ADDBA Request from unknown peer, ignore this.
1167         if (Elem->Wcid >= MAX_LEN_OF_MAC_TABLE)
1168                 return;
1169
1170         pMacEntry = &pAd->MacTab.Content[Elem->Wcid];
1171         DBGPRINT(RT_DEBUG_TRACE,("BA - PeerAddBAReqAction----> \n"));
1172         ptemp = (PULONG)Elem->Msg;
1173         //DBGPRINT_RAW(RT_DEBUG_EMU, ("%08x:: %08x:: %08x:: %08x:: %08x:: %08x:: %08x:: %08x:: %08x\n", *(ptemp), *(ptemp+1), *(ptemp+2), *(ptemp+3), *(ptemp+4), *(ptemp+5), *(ptemp+6), *(ptemp+7), *(ptemp+8)));
1174
1175         if (PeerAddBAReqActionSanity(pAd, Elem->Msg, Elem->MsgLen, pAddr))
1176         {
1177
1178                 if ((pAd->CommonCfg.bBADecline == FALSE) && IS_HT_STA(pMacEntry))
1179                 {
1180                         pAddreqFrame = (PFRAME_ADDBA_REQ)(&Elem->Msg[0]);
1181                         printk("Rcv Wcid(%d) AddBAReq\n", Elem->Wcid);
1182                         if (BARecSessionAdd(pAd, &pAd->MacTab.Content[Elem->Wcid], pAddreqFrame))
1183                                 Status = 0;
1184                         else
1185                                 Status = 38; // more parameters have invalid values
1186                 }
1187                 else
1188                 {
1189                         Status = 37; // the request has been declined.
1190                 }
1191         }
1192
1193         if (pAd->MacTab.Content[Elem->Wcid].ValidAsCLI)
1194                 ASSERT(pAd->MacTab.Content[Elem->Wcid].Sst == SST_ASSOC);
1195
1196         pAddreqFrame = (PFRAME_ADDBA_REQ)(&Elem->Msg[0]);
1197         // 2. Always send back ADDBA Response
1198         NStatus = MlmeAllocateMemory(pAd, &pOutBuffer);  //Get an unused nonpaged memory
1199         if (NStatus != NDIS_STATUS_SUCCESS)
1200         {
1201                 DBGPRINT(RT_DEBUG_TRACE,("ACTION - PeerBAAction() allocate memory failed \n"));
1202                 return;
1203         }
1204
1205         NdisZeroMemory(&ADDframe, sizeof(FRAME_ADDBA_RSP));
1206
1207         // 2-1. Prepare ADDBA Response frame.
1208         {
1209                 if (ADHOC_ON(pAd))
1210                         ActHeaderInit(pAd, &ADDframe.Hdr, pAddr, pAd->CurrentAddress, pAd->CommonCfg.Bssid);
1211                 else
1212                         ActHeaderInit(pAd, &ADDframe.Hdr, pAd->CommonCfg.Bssid, pAd->CurrentAddress, pAddr);
1213         }
1214
1215         ADDframe.Category = CATEGORY_BA;
1216         ADDframe.Action = ADDBA_RESP;
1217         ADDframe.Token = pAddreqFrame->Token;
1218         // What is the Status code??  need to check.
1219         ADDframe.StatusCode = Status;
1220         ADDframe.BaParm.BAPolicy = IMMED_BA;
1221         ADDframe.BaParm.AMSDUSupported = 0;
1222         ADDframe.BaParm.TID = pAddreqFrame->BaParm.TID;
1223         ADDframe.BaParm.BufSize = min(((UCHAR)pAddreqFrame->BaParm.BufSize), (UCHAR)pAd->CommonCfg.BACapability.field.RxBAWinLimit);
1224         if (ADDframe.BaParm.BufSize == 0)
1225         {
1226                 ADDframe.BaParm.BufSize = 64;
1227         }
1228         ADDframe.TimeOutValue = 0; //pAddreqFrame->TimeOutValue;
1229
1230         *(USHORT *)(&ADDframe.BaParm) = cpu2le16(*(USHORT *)(&ADDframe.BaParm));
1231         ADDframe.StatusCode = cpu2le16(ADDframe.StatusCode);
1232         ADDframe.TimeOutValue = cpu2le16(ADDframe.TimeOutValue);
1233
1234         MakeOutgoingFrame(pOutBuffer,               &FrameLen,
1235                                           sizeof(FRAME_ADDBA_RSP),  &ADDframe,
1236                           END_OF_ARGS);
1237         MiniportMMRequest(pAd, QID_AC_BE, pOutBuffer, FrameLen);
1238         MlmeFreeMemory(pAd, pOutBuffer);
1239
1240         DBGPRINT(RT_DEBUG_TRACE, ("%s(%d): TID(%d), BufSize(%d) <== \n", __func__, Elem->Wcid, ADDframe.BaParm.TID,
1241                                                           ADDframe.BaParm.BufSize));
1242 }
1243
1244
1245 VOID PeerAddBARspAction(
1246         IN PRTMP_ADAPTER pAd,
1247         IN MLME_QUEUE_ELEM *Elem)
1248
1249 {
1250         //UCHAR         Idx, i;
1251         //PUCHAR                   pOutBuffer = NULL;
1252         PFRAME_ADDBA_RSP    pFrame = NULL;
1253         //PBA_ORI_ENTRY         pBAEntry;
1254
1255         //ADDBA Response from unknown peer, ignore this.
1256         if (Elem->Wcid >= MAX_LEN_OF_MAC_TABLE)
1257                 return;
1258
1259         DBGPRINT(RT_DEBUG_TRACE, ("%s ==> Wcid(%d)\n", __func__, Elem->Wcid));
1260
1261         //hex_dump("PeerAddBARspAction()", Elem->Msg, Elem->MsgLen);
1262
1263         if (PeerAddBARspActionSanity(pAd, Elem->Msg, Elem->MsgLen))
1264         {
1265                 pFrame = (PFRAME_ADDBA_RSP)(&Elem->Msg[0]);
1266
1267                 DBGPRINT(RT_DEBUG_TRACE, ("\t\t StatusCode = %d\n", pFrame->StatusCode));
1268                 switch (pFrame->StatusCode)
1269                 {
1270                         case 0:
1271                                 // I want a BAsession with this peer as an originator.
1272                                 BAOriSessionAdd(pAd, &pAd->MacTab.Content[Elem->Wcid], pFrame);
1273                                 break;
1274                         default:
1275                                 // check status == USED ???
1276                                 BAOriSessionTearDown(pAd, Elem->Wcid, pFrame->BaParm.TID, TRUE, FALSE);
1277                                 break;
1278                 }
1279                 // Rcv Decline StatusCode
1280                 if ((pFrame->StatusCode == 37)
1281             || ((pAd->OpMode == OPMODE_STA) && STA_TGN_WIFI_ON(pAd) && (pFrame->StatusCode != 0))
1282             )
1283                 {
1284                         pAd->MacTab.Content[Elem->Wcid].BADeclineBitmap |= 1<<pFrame->BaParm.TID;
1285                 }
1286         }
1287 }
1288
1289 VOID PeerDelBAAction(
1290         IN PRTMP_ADAPTER pAd,
1291         IN MLME_QUEUE_ELEM *Elem)
1292
1293 {
1294         //UCHAR                         Idx;
1295         //PUCHAR                                pOutBuffer = NULL;
1296         PFRAME_DELBA_REQ    pDelFrame = NULL;
1297
1298         DBGPRINT(RT_DEBUG_TRACE,("%s ==>\n", __func__));
1299         //DELBA Request from unknown peer, ignore this.
1300         if (PeerDelBAActionSanity(pAd, Elem->Wcid, Elem->Msg, Elem->MsgLen))
1301         {
1302                 pDelFrame = (PFRAME_DELBA_REQ)(&Elem->Msg[0]);
1303                 if (pDelFrame->DelbaParm.Initiator == ORIGINATOR)
1304                 {
1305                         DBGPRINT(RT_DEBUG_TRACE,("BA - PeerDelBAAction----> ORIGINATOR\n"));
1306                         BARecSessionTearDown(pAd, Elem->Wcid, pDelFrame->DelbaParm.TID, TRUE);
1307                 }
1308                 else
1309                 {
1310                         DBGPRINT(RT_DEBUG_TRACE,("BA - PeerDelBAAction----> RECIPIENT, Reason = %d\n",  pDelFrame->ReasonCode));
1311                         //hex_dump("DelBA Frame", pDelFrame, Elem->MsgLen);
1312                         BAOriSessionTearDown(pAd, Elem->Wcid, pDelFrame->DelbaParm.TID, TRUE, FALSE);
1313                 }
1314         }
1315 }
1316
1317
1318 BOOLEAN CntlEnqueueForRecv(
1319                                                   IN PRTMP_ADAPTER              pAd,
1320                                                   IN ULONG                              Wcid,
1321                                                   IN ULONG                              MsgLen,
1322                                                   IN PFRAME_BA_REQ              pMsg)
1323 {
1324         PFRAME_BA_REQ   pFrame = pMsg;
1325         //PRTMP_REORDERBUF      pBuffer;
1326         //PRTMP_REORDERBUF      pDmaBuf;
1327         PBA_REC_ENTRY pBAEntry;
1328         //BOOLEAN       Result;
1329         ULONG   Idx;
1330         //UCHAR NumRxPkt;
1331         UCHAR   TID;//, i;
1332
1333         TID = (UCHAR)pFrame->BARControl.TID;
1334
1335         DBGPRINT(RT_DEBUG_TRACE, ("%s(): BAR-Wcid(%ld), Tid (%d)\n", __func__, Wcid, TID));
1336         //hex_dump("BAR", (PCHAR) pFrame, MsgLen);
1337         // Do nothing if the driver is starting halt state.
1338         // This might happen when timer already been fired before cancel timer with mlmehalt
1339         if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
1340                 return FALSE;
1341
1342         // First check the size, it MUST not exceed the mlme queue size
1343         if (MsgLen > MGMT_DMA_BUFFER_SIZE)
1344         {
1345                 DBGPRINT_ERR(("CntlEnqueueForRecv: frame too large, size = %ld \n", MsgLen));
1346                 return FALSE;
1347         }
1348         else if (MsgLen != sizeof(FRAME_BA_REQ))
1349         {
1350                 DBGPRINT_ERR(("CntlEnqueueForRecv: BlockAck Request frame length size = %ld incorrect\n", MsgLen));
1351                 return FALSE;
1352         }
1353         else if (MsgLen != sizeof(FRAME_BA_REQ))
1354         {
1355                 DBGPRINT_ERR(("CntlEnqueueForRecv: BlockAck Request frame length size = %ld incorrect\n", MsgLen));
1356                 return FALSE;
1357         }
1358
1359         if ((Wcid < MAX_LEN_OF_MAC_TABLE) && (TID < 8))
1360                 {
1361                 // if this receiving packet is from SA that is in our OriEntry. Since WCID <9 has direct mapping. no need search.
1362                 Idx = pAd->MacTab.Content[Wcid].BARecWcidArray[TID];
1363                 pBAEntry = &pAd->BATable.BARecEntry[Idx];
1364                 }
1365                 else
1366                 {
1367                 return FALSE;
1368         }
1369
1370         DBGPRINT(RT_DEBUG_TRACE, ("BAR(%ld) : Tid (%d) - %04x:%04x\n", Wcid, TID, pFrame->BAStartingSeq.field.StartSeq, pBAEntry->LastIndSeq ));
1371
1372         if (SEQ_SMALLER(pBAEntry->LastIndSeq, pFrame->BAStartingSeq.field.StartSeq, MAXSEQ))
1373         {
1374                 //printk("BAR Seq = %x, LastIndSeq = %x\n", pFrame->BAStartingSeq.field.StartSeq, pBAEntry->LastIndSeq);
1375                 ba_indicate_reordering_mpdus_le_seq(pAd, pBAEntry, pFrame->BAStartingSeq.field.StartSeq);
1376                 pBAEntry->LastIndSeq = (pFrame->BAStartingSeq.field.StartSeq == 0) ? MAXSEQ :(pFrame->BAStartingSeq.field.StartSeq -1);
1377         }
1378         //ba_refresh_reordering_mpdus(pAd, pBAEntry);
1379         return TRUE;
1380 }
1381
1382 /*
1383 Description : Send PSMP Action frame If PSMP mode switches.
1384 */
1385 VOID SendPSMPAction(
1386                                    IN PRTMP_ADAPTER             pAd,
1387                                    IN UCHAR                             Wcid,
1388                                    IN UCHAR                             Psmp)
1389 {
1390         PUCHAR          pOutBuffer = NULL;
1391         NDIS_STATUS     NStatus;
1392         //ULONG           Idx;
1393         FRAME_PSMP_ACTION   Frame;
1394         ULONG           FrameLen;
1395 #ifdef RT30xx
1396         UCHAR                   bbpdata=0;
1397         UINT32                  macdata;
1398 #endif // RT30xx //
1399
1400         NStatus = MlmeAllocateMemory(pAd, &pOutBuffer);  //Get an unused nonpaged memory
1401         if (NStatus != NDIS_STATUS_SUCCESS)
1402         {
1403                 DBGPRINT(RT_DEBUG_ERROR,("BA - MlmeADDBAAction() allocate memory failed \n"));
1404                 return;
1405         }
1406
1407         ActHeaderInit(pAd, &Frame.Hdr, pAd->CommonCfg.Bssid, pAd->CurrentAddress, pAd->MacTab.Content[Wcid].Addr);
1408
1409         Frame.Category = CATEGORY_HT;
1410         Frame.Action = SMPS_ACTION;
1411         switch (Psmp)
1412         {
1413                 case MMPS_ENABLE:
1414 #ifdef RT30xx
1415                         if (IS_RT3090(pAd))
1416                         {
1417                                 // disable MMPS BBP control register
1418                                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &bbpdata);
1419                                 bbpdata &= ~(0x04);     //bit 2
1420                                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, bbpdata);
1421
1422                                 // disable MMPS MAC control register
1423                                 RTMP_IO_READ32(pAd, 0x1210, &macdata);
1424                                 macdata &= ~(0x09);     //bit 0, 3
1425                                 RTMP_IO_WRITE32(pAd, 0x1210, macdata);
1426                         }
1427 #endif // RT30xx //
1428                         Frame.Psmp = 0;
1429                         break;
1430                 case MMPS_DYNAMIC:
1431 #ifdef RT30xx
1432                         if (IS_RT3090(pAd))
1433                         {
1434                                 // enable MMPS BBP control register
1435                                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &bbpdata);
1436                                 bbpdata |= 0x04;        //bit 2
1437                                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, bbpdata);
1438
1439                                 // enable MMPS MAC control register
1440                                 RTMP_IO_READ32(pAd, 0x1210, &macdata);
1441                                 macdata |= 0x09;        //bit 0, 3
1442                                 RTMP_IO_WRITE32(pAd, 0x1210, macdata);
1443                         }
1444 #endif // RT30xx //
1445                         Frame.Psmp = 3;
1446                         break;
1447                 case MMPS_STATIC:
1448 #ifdef RT30xx
1449                         if (IS_RT3090(pAd))
1450                         {
1451                                 // enable MMPS BBP control register
1452                                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &bbpdata);
1453                                 bbpdata |= 0x04;        //bit 2
1454                                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, bbpdata);
1455
1456                                 // enable MMPS MAC control register
1457                                 RTMP_IO_READ32(pAd, 0x1210, &macdata);
1458                                 macdata |= 0x09;        //bit 0, 3
1459                                 RTMP_IO_WRITE32(pAd, 0x1210, macdata);
1460                         }
1461 #endif // RT30xx //
1462                         Frame.Psmp = 1;
1463                         break;
1464         }
1465         MakeOutgoingFrame(pOutBuffer,               &FrameLen,
1466                                           sizeof(FRAME_PSMP_ACTION),      &Frame,
1467                                           END_OF_ARGS);
1468         MiniportMMRequest(pAd, QID_AC_BE, pOutBuffer, FrameLen);
1469         MlmeFreeMemory(pAd, pOutBuffer);
1470         DBGPRINT(RT_DEBUG_ERROR,("HT - SendPSMPAction( %d )  \n", Frame.Psmp));
1471 }
1472
1473
1474 #define RADIO_MEASUREMENT_REQUEST_ACTION        0
1475
1476 typedef struct PACKED
1477 {
1478         UCHAR   RegulatoryClass;
1479         UCHAR   ChannelNumber;
1480         USHORT  RandomInterval;
1481         USHORT  MeasurementDuration;
1482         UCHAR   MeasurementMode;
1483         UCHAR   BSSID[MAC_ADDR_LEN];
1484         UCHAR   ReportingCondition;
1485         UCHAR   Threshold;
1486         UCHAR   SSIDIE[2];                      // 2 byte
1487 } BEACON_REQUEST;
1488
1489 typedef struct PACKED
1490 {
1491         UCHAR   ID;
1492         UCHAR   Length;
1493         UCHAR   Token;
1494         UCHAR   RequestMode;
1495         UCHAR   Type;
1496 } MEASUREMENT_REQ;
1497
1498
1499
1500
1501 void convert_reordering_packet_to_preAMSDU_or_802_3_packet(
1502         IN      PRTMP_ADAPTER   pAd,
1503         IN      RX_BLK                  *pRxBlk,
1504         IN  UCHAR                       FromWhichBSSID)
1505 {
1506         PNDIS_PACKET    pRxPkt;
1507         UCHAR                   Header802_3[LENGTH_802_3];
1508
1509         // 1. get 802.3 Header
1510         // 2. remove LLC
1511         //              a. pointer pRxBlk->pData to payload
1512         //      b. modify pRxBlk->DataSize
1513
1514         RTMP_802_11_REMOVE_LLC_AND_CONVERT_TO_802_3(pRxBlk, Header802_3);
1515
1516         ASSERT(pRxBlk->pRxPacket);
1517         pRxPkt = RTPKT_TO_OSPKT(pRxBlk->pRxPacket);
1518
1519         RTPKT_TO_OSPKT(pRxPkt)->dev = get_netdev_from_bssid(pAd, FromWhichBSSID);
1520         RTPKT_TO_OSPKT(pRxPkt)->data = pRxBlk->pData;
1521         RTPKT_TO_OSPKT(pRxPkt)->len = pRxBlk->DataSize;
1522         RTPKT_TO_OSPKT(pRxPkt)->tail = RTPKT_TO_OSPKT(pRxPkt)->data + RTPKT_TO_OSPKT(pRxPkt)->len;
1523
1524         //
1525         // copy 802.3 header, if necessary
1526         //
1527         if (!RX_BLK_TEST_FLAG(pRxBlk, fRX_AMSDU))
1528         {
1529 #ifdef LINUX
1530                 NdisMoveMemory(skb_push(pRxPkt, LENGTH_802_3), Header802_3, LENGTH_802_3);
1531 #endif
1532         }
1533 }
1534
1535
1536 #define INDICATE_LEGACY_OR_AMSDU(_pAd, _pRxBlk, _fromWhichBSSID)                \
1537         do                                                                                                                                      \
1538         {                                                                                                                                       \
1539         if (RX_BLK_TEST_FLAG(_pRxBlk, fRX_AMSDU))                                               \
1540         {                                                                                                                               \
1541                 Indicate_AMSDU_Packet(_pAd, _pRxBlk, _fromWhichBSSID);          \
1542         }                                                                                                                               \
1543                 else if (RX_BLK_TEST_FLAG(_pRxBlk, fRX_EAP))                                    \
1544                 {                                                                                                                               \
1545                         Indicate_EAPOL_Packet(_pAd, _pRxBlk, _fromWhichBSSID);          \
1546                 }                                                                                                                               \
1547         else                                                                                                                    \
1548         {                                                                                                                               \
1549                 Indicate_Legacy_Packet(_pAd, _pRxBlk, _fromWhichBSSID);         \
1550         }                                                                                                                               \
1551         } while (0);
1552
1553
1554
1555 static VOID ba_enqueue_reordering_packet(
1556         IN      PRTMP_ADAPTER   pAd,
1557         IN      PBA_REC_ENTRY   pBAEntry,
1558         IN      RX_BLK                  *pRxBlk,
1559         IN      UCHAR                   FromWhichBSSID)
1560 {
1561         struct reordering_mpdu *mpdu_blk;
1562         UINT16  Sequence = (UINT16) pRxBlk->pHeader->Sequence;
1563
1564         mpdu_blk = ba_mpdu_blk_alloc(pAd);
1565         if (mpdu_blk != NULL)
1566         {
1567                 // Write RxD buffer address & allocated buffer length
1568                 NdisAcquireSpinLock(&pBAEntry->RxReRingLock);
1569
1570                 mpdu_blk->Sequence = Sequence;
1571
1572                 mpdu_blk->bAMSDU = RX_BLK_TEST_FLAG(pRxBlk, fRX_AMSDU);
1573
1574                 convert_reordering_packet_to_preAMSDU_or_802_3_packet(pAd, pRxBlk, FromWhichBSSID);
1575
1576                 STATS_INC_RX_PACKETS(pAd, FromWhichBSSID);
1577
1578         //
1579                 // it is necessary for reordering packet to record
1580                 // which BSS it come from
1581                 //
1582                 RTMP_SET_PACKET_IF(pRxBlk->pRxPacket, FromWhichBSSID);
1583
1584                 mpdu_blk->pPacket = pRxBlk->pRxPacket;
1585
1586                 if (ba_reordering_mpdu_insertsorted(&pBAEntry->list, mpdu_blk) == FALSE)
1587                 {
1588                         // had been already within reordering list
1589                         // don't indicate
1590                         RELEASE_NDIS_PACKET(pAd, pRxBlk->pRxPacket, NDIS_STATUS_SUCCESS);
1591                         ba_mpdu_blk_free(pAd, mpdu_blk);
1592                 }
1593
1594                 ASSERT((0<= pBAEntry->list.qlen)  && (pBAEntry->list.qlen <= pBAEntry->BAWinSize));
1595                 NdisReleaseSpinLock(&pBAEntry->RxReRingLock);
1596         }
1597         else
1598         {
1599                 DBGPRINT(RT_DEBUG_ERROR,  ("!!! (%d) Can't allocate reordering mpdu blk\n",
1600                                                                    pBAEntry->list.qlen));
1601
1602                 /*
1603                  * flush all pending reordering mpdus
1604                  * and receving mpdu to upper layer
1605                  * make tcp/ip to take care reordering mechanism
1606                  */
1607                 //ba_refresh_reordering_mpdus(pAd, pBAEntry);
1608                 ba_indicate_reordering_mpdus_le_seq(pAd, pBAEntry, Sequence);
1609
1610                 pBAEntry->LastIndSeq = Sequence;
1611                 INDICATE_LEGACY_OR_AMSDU(pAd, pRxBlk, FromWhichBSSID);
1612         }
1613 }
1614
1615
1616 /*
1617         ==========================================================================
1618         Description:
1619                 Indicate this packet to upper layer or put it into reordering buffer
1620
1621         Parametrs:
1622                 pRxBlk         : carry necessary packet info 802.11 format
1623                 FromWhichBSSID : the packet received from which BSS
1624
1625         Return  :
1626                           none
1627
1628         Note    :
1629                   the packet queued into reordering buffer need to cover to 802.3 format
1630                           or pre_AMSDU format
1631         ==========================================================================
1632  */
1633
1634 VOID Indicate_AMPDU_Packet(
1635         IN      PRTMP_ADAPTER   pAd,
1636         IN      RX_BLK                  *pRxBlk,
1637         IN      UCHAR                   FromWhichBSSID)
1638 {
1639         USHORT                          Idx;
1640         PBA_REC_ENTRY           pBAEntry = NULL;
1641         UINT16                          Sequence = pRxBlk->pHeader->Sequence;
1642         ULONG                           Now32;
1643         UCHAR                           Wcid = pRxBlk->pRxWI->WirelessCliID;
1644         UCHAR                           TID = pRxBlk->pRxWI->TID;
1645
1646
1647         if (!RX_BLK_TEST_FLAG(pRxBlk, fRX_AMSDU) &&  (pRxBlk->DataSize > MAX_RX_PKT_LEN))
1648         {
1649                 // release packet
1650                 RELEASE_NDIS_PACKET(pAd, pRxBlk->pRxPacket, NDIS_STATUS_FAILURE);
1651                 return;
1652         }
1653
1654         if (Wcid < MAX_LEN_OF_MAC_TABLE)
1655         {
1656                 Idx = pAd->MacTab.Content[Wcid].BARecWcidArray[TID];
1657                 if (Idx == 0)
1658                 {
1659                         /* Rec BA Session had been torn down */
1660                         INDICATE_LEGACY_OR_AMSDU(pAd, pRxBlk, FromWhichBSSID);
1661                         return;
1662                 }
1663                 pBAEntry = &pAd->BATable.BARecEntry[Idx];
1664         }
1665         else
1666         {
1667                 // impossible !!!
1668                 ASSERT(0);
1669                 // release packet
1670                 RELEASE_NDIS_PACKET(pAd, pRxBlk->pRxPacket, NDIS_STATUS_FAILURE);
1671                 return;
1672         }
1673
1674         ASSERT(pBAEntry);
1675
1676         // update last rx time
1677         NdisGetSystemUpTime(&Now32);
1678
1679         pBAEntry->rcvSeq = Sequence;
1680
1681
1682         ba_flush_reordering_timeout_mpdus(pAd, pBAEntry, Now32);
1683         pBAEntry->LastIndSeqAtTimer = Now32;
1684
1685         //
1686         // Reset Last Indicate Sequence
1687         //
1688         if (pBAEntry->LastIndSeq == RESET_RCV_SEQ)
1689         {
1690                 ASSERT((pBAEntry->list.qlen == 0) && (pBAEntry->list.next == NULL));
1691
1692                 // reset rcv sequence of BA session
1693                 pBAEntry->LastIndSeq = Sequence;
1694                 pBAEntry->LastIndSeqAtTimer = Now32;
1695                 INDICATE_LEGACY_OR_AMSDU(pAd, pRxBlk, FromWhichBSSID);
1696                 return;
1697         }
1698
1699
1700         //
1701         // I. Check if in order.
1702         //
1703         if (SEQ_STEPONE(Sequence, pBAEntry->LastIndSeq, MAXSEQ))
1704         {
1705                 USHORT  LastIndSeq;
1706
1707                 pBAEntry->LastIndSeq = Sequence;
1708                 INDICATE_LEGACY_OR_AMSDU(pAd, pRxBlk, FromWhichBSSID);
1709                 LastIndSeq = ba_indicate_reordering_mpdus_in_order(pAd, pBAEntry, pBAEntry->LastIndSeq);
1710                 if (LastIndSeq != RESET_RCV_SEQ)
1711                 {
1712                         pBAEntry->LastIndSeq = LastIndSeq;
1713                 }
1714                 pBAEntry->LastIndSeqAtTimer = Now32;
1715         }
1716         //
1717         // II. Drop Duplicated Packet
1718         //
1719         else if (Sequence == pBAEntry->LastIndSeq)
1720         {
1721
1722                 // drop and release packet
1723                 pBAEntry->nDropPacket++;
1724                 RELEASE_NDIS_PACKET(pAd, pRxBlk->pRxPacket, NDIS_STATUS_FAILURE);
1725         }
1726         //
1727         // III. Drop Old Received Packet
1728         //
1729         else if (SEQ_SMALLER(Sequence, pBAEntry->LastIndSeq, MAXSEQ))
1730         {
1731
1732                 // drop and release packet
1733                 pBAEntry->nDropPacket++;
1734                 RELEASE_NDIS_PACKET(pAd, pRxBlk->pRxPacket, NDIS_STATUS_FAILURE);
1735         }
1736         //
1737         // IV. Receive Sequence within Window Size
1738         //
1739         else if (SEQ_SMALLER(Sequence, (((pBAEntry->LastIndSeq+pBAEntry->BAWinSize+1)) & MAXSEQ), MAXSEQ))
1740         {
1741                 ba_enqueue_reordering_packet(pAd, pBAEntry, pRxBlk, FromWhichBSSID);
1742         }
1743         //
1744         // V. Receive seq surpasses Win(lastseq + nMSDU). So refresh all reorder buffer
1745         //
1746         else
1747         {
1748                 LONG WinStartSeq, TmpSeq;
1749
1750
1751                 TmpSeq = Sequence - (pBAEntry->BAWinSize) -1;
1752                 if (TmpSeq < 0)
1753                 {
1754                         TmpSeq = (MAXSEQ+1) + TmpSeq;
1755                 }
1756                 WinStartSeq = (TmpSeq+1) & MAXSEQ;
1757                 ba_indicate_reordering_mpdus_le_seq(pAd, pBAEntry, WinStartSeq);
1758                 pBAEntry->LastIndSeq = WinStartSeq; //TmpSeq;
1759
1760                 pBAEntry->LastIndSeqAtTimer = Now32;
1761
1762                 ba_enqueue_reordering_packet(pAd, pBAEntry, pRxBlk, FromWhichBSSID);
1763
1764                 TmpSeq = ba_indicate_reordering_mpdus_in_order(pAd, pBAEntry, pBAEntry->LastIndSeq);
1765                 if (TmpSeq != RESET_RCV_SEQ)
1766                 {
1767                         pBAEntry->LastIndSeq = TmpSeq;
1768                 }
1769         }
1770 }