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