Staging: rt3070: remove dead code
[linux-2.6] / drivers / staging / rt3070 / 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         DBGPRINT(RT_DEBUG_TRACE,("Send AddBA to %02x:%02x:%02x:%02x:%02x:%02x Tid:%d isForced:%d Wcid:%d\n"
536                 ,pEntry->Addr[0],pEntry->Addr[1],pEntry->Addr[2]
537                 ,pEntry->Addr[3],pEntry->Addr[4],pEntry->Addr[5]
538                 ,TID,isForced,pEntry->Aid));
539
540         if (!(pEntry->TXBAbitmap & (1<<TID)))
541         {
542                 RTMPInitTimer(pAd, &pBAEntry->ORIBATimer, GET_TIMER_FUNCTION(BAOriSessionSetupTimeout), pBAEntry, FALSE);
543         }
544         else
545                 RTMPCancelTimer(&pBAEntry->ORIBATimer, &Cancelled);
546
547         // set timer to send ADDBA request
548         RTMPSetTimer(&pBAEntry->ORIBATimer, DelayTime);
549 }
550
551 VOID BAOriSessionAdd(
552                         IN PRTMP_ADAPTER    pAd,
553                                         IN MAC_TABLE_ENTRY  *pEntry,
554                         IN PFRAME_ADDBA_RSP pFrame)
555 {
556         BA_ORI_ENTRY  *pBAEntry = NULL;
557         BOOLEAN       Cancelled;
558         UCHAR         TID;
559         USHORT        Idx;
560         PUCHAR          pOutBuffer2 = NULL;
561         NDIS_STATUS     NStatus;
562         ULONG           FrameLen;
563         FRAME_BAR       FrameBar;
564
565         TID = pFrame->BaParm.TID;
566         Idx = pEntry->BAOriWcidArray[TID];
567         pBAEntry =&pAd->BATable.BAOriEntry[Idx];
568
569         // Start fill in parameters.
570         if ((Idx !=0) && (pBAEntry->TID == TID) && (pBAEntry->ORI_BA_Status == Originator_WaitRes))
571         {
572                 pBAEntry->BAWinSize = min(pBAEntry->BAWinSize, ((UCHAR)pFrame->BaParm.BufSize));
573                 BA_MaxWinSizeReasign(pAd, pEntry, &pBAEntry->BAWinSize);
574
575                 pBAEntry->TimeOutValue = pFrame->TimeOutValue;
576                 pBAEntry->ORI_BA_Status = Originator_Done;
577                 // reset sequence number
578                 pBAEntry->Sequence = BA_ORI_INIT_SEQ;
579                 // Set Bitmap flag.
580                 pEntry->TXBAbitmap |= (1<<TID);
581                                 RTMPCancelTimer(&pBAEntry->ORIBATimer, &Cancelled);
582
583                 pBAEntry->ORIBATimer.TimerValue = 0;    //pFrame->TimeOutValue;
584
585                 DBGPRINT(RT_DEBUG_TRACE,("%s : TXBAbitmap = %x, BAWinSize = %d, TimeOut = %ld\n", __func__, pEntry->TXBAbitmap,
586                                                                  pBAEntry->BAWinSize, pBAEntry->ORIBATimer.TimerValue));
587
588                 // SEND BAR ;
589                 NStatus = MlmeAllocateMemory(pAd, &pOutBuffer2);  //Get an unused nonpaged memory
590                 if (NStatus != NDIS_STATUS_SUCCESS)
591                 {
592                         DBGPRINT(RT_DEBUG_TRACE,("BA - BAOriSessionAdd() allocate memory failed \n"));
593                         return;
594                 }
595
596                 BarHeaderInit(pAd, &FrameBar, pAd->MacTab.Content[pBAEntry->Wcid].Addr, pAd->CurrentAddress);
597
598                 FrameBar.StartingSeq.field.FragNum = 0; // make sure sequence not clear in DEL function.
599                 FrameBar.StartingSeq.field.StartSeq = pBAEntry->Sequence; // make sure sequence not clear in DEL funciton.
600                 FrameBar.BarControl.TID = pBAEntry->TID; // make sure sequence not clear in DEL funciton.
601                 MakeOutgoingFrame(pOutBuffer2,              &FrameLen,
602                                                   sizeof(FRAME_BAR),      &FrameBar,
603                                           END_OF_ARGS);
604                 MiniportMMRequest(pAd, QID_AC_BE, pOutBuffer2, FrameLen);
605                 MlmeFreeMemory(pAd, pOutBuffer2);
606
607
608                 if (pBAEntry->ORIBATimer.TimerValue)
609                         RTMPSetTimer(&pBAEntry->ORIBATimer, pBAEntry->ORIBATimer.TimerValue); // in mSec
610         }
611 }
612
613 BOOLEAN BARecSessionAdd(
614                                            IN PRTMP_ADAPTER    pAd,
615                                            IN MAC_TABLE_ENTRY  *pEntry,
616                                            IN PFRAME_ADDBA_REQ pFrame)
617 {
618         BA_REC_ENTRY            *pBAEntry = NULL;
619         BOOLEAN                 Status = TRUE;
620         BOOLEAN                 Cancelled;
621         USHORT                  Idx;
622         UCHAR                   TID;
623         UCHAR                   BAWinSize;
624         //UINT32                  Value;
625         //UINT                    offset;
626
627
628         ASSERT(pEntry);
629
630         // find TID
631         TID = pFrame->BaParm.TID;
632
633         BAWinSize = min(((UCHAR)pFrame->BaParm.BufSize), (UCHAR)pAd->CommonCfg.BACapability.field.RxBAWinLimit);
634
635         // Intel patch
636         if (BAWinSize == 0)
637         {
638                 BAWinSize = 64;
639         }
640
641         Idx = pEntry->BARecWcidArray[TID];
642
643
644         if (Idx == 0)
645         {
646                 pBAEntry = BATableAllocRecEntry(pAd, &Idx);
647         }
648         else
649         {
650                 pBAEntry = &pAd->BATable.BARecEntry[Idx];
651                 // flush all pending reordering mpdus
652                 ba_refresh_reordering_mpdus(pAd, pBAEntry);
653         }
654
655         DBGPRINT(RT_DEBUG_TRACE,("%s(%ld): Idx = %d, BAWinSize(req %d) = %d\n", __func__, pAd->BATable.numAsRecipient, Idx,
656                                                          pFrame->BaParm.BufSize, BAWinSize));
657
658         // Start fill in parameters.
659         if (pBAEntry != NULL)
660         {
661                 ASSERT(pBAEntry->list.qlen == 0);
662
663                 pBAEntry->REC_BA_Status = Recipient_HandleRes;
664                 pBAEntry->BAWinSize = BAWinSize;
665                 pBAEntry->Wcid = pEntry->Aid;
666                 pBAEntry->TID = TID;
667                 pBAEntry->TimeOutValue = pFrame->TimeOutValue;
668                 pBAEntry->REC_BA_Status = Recipient_Accept;
669                 // initial sequence number
670                 pBAEntry->LastIndSeq = RESET_RCV_SEQ; //pFrame->BaStartSeq.field.StartSeq;
671
672                 printk("Start Seq = %08x\n",  pFrame->BaStartSeq.field.StartSeq);
673
674                 if (pEntry->RXBAbitmap & (1<<TID))
675                 {
676                         RTMPCancelTimer(&pBAEntry->RECBATimer, &Cancelled);
677                 }
678                 else
679                 {
680                         RTMPInitTimer(pAd, &pBAEntry->RECBATimer, GET_TIMER_FUNCTION(BARecSessionIdleTimeout), pBAEntry, TRUE);
681                 }
682
683                 // Set Bitmap flag.
684                 pEntry->RXBAbitmap |= (1<<TID);
685                 pEntry->BARecWcidArray[TID] = Idx;
686
687                 pEntry->BADeclineBitmap &= ~(1<<TID);
688
689                 // Set BA session mask in WCID table.
690                 RT28XX_ADD_BA_SESSION_TO_ASIC(pAd, pEntry->Aid, TID);
691
692                 DBGPRINT(RT_DEBUG_TRACE,("MACEntry[%d]RXBAbitmap = 0x%x. BARecWcidArray=%d\n",
693                                 pEntry->Aid, pEntry->RXBAbitmap, pEntry->BARecWcidArray[TID]));
694         }
695         else
696         {
697                 Status = FALSE;
698                 DBGPRINT(RT_DEBUG_TRACE,("Can't Accept ADDBA for %02x:%02x:%02x:%02x:%02x:%02x TID = %d\n",
699                                 PRINT_MAC(pEntry->Addr), TID));
700         }
701         return(Status);
702 }
703
704
705 BA_REC_ENTRY *BATableAllocRecEntry(
706                                                                   IN  PRTMP_ADAPTER   pAd,
707                                                                   OUT USHORT          *Idx)
708 {
709         int             i;
710         BA_REC_ENTRY    *pBAEntry = NULL;
711
712
713         NdisAcquireSpinLock(&pAd->BATabLock);
714
715         if (pAd->BATable.numAsRecipient >= MAX_BARECI_SESSION)
716         {
717                 printk("BA Recipeint Session (%ld) > %d\n", pAd->BATable.numAsRecipient,
718                         MAX_BARECI_SESSION);
719                 goto done;
720         }
721
722         // reserve idx 0 to identify BAWcidArray[TID] as empty
723         for (i=1; i < MAX_LEN_OF_BA_REC_TABLE; i++)
724         {
725                 pBAEntry =&pAd->BATable.BARecEntry[i];
726                 if ((pBAEntry->REC_BA_Status == Recipient_NONE))
727                 {
728                         // get one
729                         pAd->BATable.numAsRecipient++;
730                         pBAEntry->REC_BA_Status = Recipient_USED;
731                         *Idx = i;
732                         break;
733                 }
734         }
735
736 done:
737         NdisReleaseSpinLock(&pAd->BATabLock);
738         return pBAEntry;
739 }
740
741 BA_ORI_ENTRY *BATableAllocOriEntry(
742                                                                   IN  PRTMP_ADAPTER   pAd,
743                                                                   OUT USHORT          *Idx)
744 {
745         int             i;
746         BA_ORI_ENTRY    *pBAEntry = NULL;
747
748         NdisAcquireSpinLock(&pAd->BATabLock);
749
750         if (pAd->BATable.numAsOriginator >= (MAX_LEN_OF_BA_ORI_TABLE))
751         {
752                 goto done;
753         }
754
755         // reserve idx 0 to identify BAWcidArray[TID] as empty
756         for (i=1; i<MAX_LEN_OF_BA_ORI_TABLE; i++)
757         {
758                 pBAEntry =&pAd->BATable.BAOriEntry[i];
759                 if ((pBAEntry->ORI_BA_Status == Originator_NONE))
760                 {
761                         // get one
762                         pAd->BATable.numAsOriginator++;
763                         pBAEntry->ORI_BA_Status = Originator_USED;
764                         pBAEntry->pAdapter = pAd;
765                         *Idx = i;
766                         break;
767                 }
768         }
769
770 done:
771         NdisReleaseSpinLock(&pAd->BATabLock);
772         return pBAEntry;
773 }
774
775
776 VOID BATableFreeOriEntry(
777                                                 IN  PRTMP_ADAPTER   pAd,
778                                                 IN  ULONG           Idx)
779 {
780         BA_ORI_ENTRY    *pBAEntry = NULL;
781         MAC_TABLE_ENTRY *pEntry;
782
783
784         if ((Idx == 0) || (Idx >= MAX_LEN_OF_BA_ORI_TABLE))
785                 return;
786
787         pBAEntry =&pAd->BATable.BAOriEntry[Idx];
788
789         if (pBAEntry->ORI_BA_Status != Originator_NONE)
790         {
791                 pEntry = &pAd->MacTab.Content[pBAEntry->Wcid];
792                 pEntry->BAOriWcidArray[pBAEntry->TID] = 0;
793
794
795                 NdisAcquireSpinLock(&pAd->BATabLock);
796                 if (pBAEntry->ORI_BA_Status == Originator_Done)
797                 {
798                         pEntry->TXBAbitmap &= (~(1<<(pBAEntry->TID) ));
799                         DBGPRINT(RT_DEBUG_TRACE, ("BATableFreeOriEntry numAsOriginator= %ld\n", pAd->BATable.numAsRecipient));
800                         // Erase Bitmap flag.
801                 }
802
803                 ASSERT(pAd->BATable.numAsOriginator != 0);
804
805                 pAd->BATable.numAsOriginator -= 1;
806
807                 pBAEntry->ORI_BA_Status = Originator_NONE;
808                 pBAEntry->Token = 0;
809                 NdisReleaseSpinLock(&pAd->BATabLock);
810         }
811 }
812
813
814 VOID BATableFreeRecEntry(
815                                                 IN  PRTMP_ADAPTER   pAd,
816                                                 IN  ULONG           Idx)
817 {
818         BA_REC_ENTRY    *pBAEntry = NULL;
819         MAC_TABLE_ENTRY *pEntry;
820
821
822         if ((Idx == 0) || (Idx >= MAX_LEN_OF_BA_REC_TABLE))
823                 return;
824
825         pBAEntry =&pAd->BATable.BARecEntry[Idx];
826
827         if (pBAEntry->REC_BA_Status != Recipient_NONE)
828         {
829                 pEntry = &pAd->MacTab.Content[pBAEntry->Wcid];
830                 pEntry->BARecWcidArray[pBAEntry->TID] = 0;
831
832                 NdisAcquireSpinLock(&pAd->BATabLock);
833
834                 ASSERT(pAd->BATable.numAsRecipient != 0);
835
836                 pAd->BATable.numAsRecipient -= 1;
837
838                 pBAEntry->REC_BA_Status = Recipient_NONE;
839                 NdisReleaseSpinLock(&pAd->BATabLock);
840         }
841 }
842
843
844 VOID BAOriSessionTearDown(
845                                                  IN OUT  PRTMP_ADAPTER   pAd,
846                                                  IN      UCHAR           Wcid,
847                                                  IN      UCHAR           TID,
848                                                  IN      BOOLEAN         bPassive,
849                                                  IN      BOOLEAN         bForceSend)
850 {
851         ULONG           Idx = 0;
852         BA_ORI_ENTRY    *pBAEntry;
853         BOOLEAN         Cancelled;
854
855         if (Wcid >= MAX_LEN_OF_MAC_TABLE)
856         {
857                 return;
858         }
859
860         //
861         // Locate corresponding BA Originator Entry in BA Table with the (pAddr,TID).
862         //
863         Idx = pAd->MacTab.Content[Wcid].BAOriWcidArray[TID];
864         if ((Idx == 0) || (Idx >= MAX_LEN_OF_BA_ORI_TABLE))
865         {
866                 if (bForceSend == TRUE)
867                 {
868                         // force send specified TID DelBA
869                         MLME_DELBA_REQ_STRUCT   DelbaReq;
870                         MLME_QUEUE_ELEM *Elem = (MLME_QUEUE_ELEM *) kmalloc(sizeof(MLME_QUEUE_ELEM), MEM_ALLOC_FLAG);
871
872                         NdisZeroMemory(&DelbaReq, sizeof(DelbaReq));
873                         NdisZeroMemory(Elem, sizeof(MLME_QUEUE_ELEM));
874
875                         COPY_MAC_ADDR(DelbaReq.Addr, pAd->MacTab.Content[Wcid].Addr);
876                         DelbaReq.Wcid = Wcid;
877                         DelbaReq.TID = TID;
878                         DelbaReq.Initiator = ORIGINATOR;
879 #if 1
880                         Elem->MsgLen  = sizeof(DelbaReq);
881                         NdisMoveMemory(Elem->Msg, &DelbaReq, sizeof(DelbaReq));
882                         MlmeDELBAAction(pAd, Elem);
883                         kfree(Elem);
884 #else
885                         MlmeEnqueue(pAd, ACTION_STATE_MACHINE, MT2_MLME_ORI_DELBA_CATE, sizeof(MLME_DELBA_REQ_STRUCT), (PVOID)&DelbaReq);
886                         RT28XX_MLME_HANDLER(pAd);
887 #endif
888                 }
889
890                 return;
891         }
892
893         DBGPRINT(RT_DEBUG_TRACE,("%s===>Wcid=%d.TID=%d \n", __func__, Wcid, TID));
894
895         pBAEntry = &pAd->BATable.BAOriEntry[Idx];
896         DBGPRINT(RT_DEBUG_TRACE,("\t===>Idx = %ld, Wcid=%d.TID=%d, ORI_BA_Status = %d \n", Idx, Wcid, TID, pBAEntry->ORI_BA_Status));
897         //
898         // Prepare DelBA action frame and send to the peer.
899         //
900         if ((bPassive == FALSE) && (TID == pBAEntry->TID) && (pBAEntry->ORI_BA_Status == Originator_Done))
901         {
902                 MLME_DELBA_REQ_STRUCT   DelbaReq;
903                 MLME_QUEUE_ELEM *Elem = (MLME_QUEUE_ELEM *) kmalloc(sizeof(MLME_QUEUE_ELEM), MEM_ALLOC_FLAG);
904
905                 NdisZeroMemory(&DelbaReq, sizeof(DelbaReq));
906                 NdisZeroMemory(Elem, sizeof(MLME_QUEUE_ELEM));
907
908                 COPY_MAC_ADDR(DelbaReq.Addr, pAd->MacTab.Content[Wcid].Addr);
909                 DelbaReq.Wcid = Wcid;
910                 DelbaReq.TID = pBAEntry->TID;
911                 DelbaReq.Initiator = ORIGINATOR;
912 #if 1
913                 Elem->MsgLen  = sizeof(DelbaReq);
914                 NdisMoveMemory(Elem->Msg, &DelbaReq, sizeof(DelbaReq));
915                 MlmeDELBAAction(pAd, Elem);
916                 kfree(Elem);
917 #else
918                 MlmeEnqueue(pAd, ACTION_STATE_MACHINE, MT2_MLME_ORI_DELBA_CATE, sizeof(MLME_DELBA_REQ_STRUCT), (PVOID)&DelbaReq);
919                 RT28XX_MLME_HANDLER(pAd);
920 #endif
921         }
922         RTMPCancelTimer(&pBAEntry->ORIBATimer, &Cancelled);
923         BATableFreeOriEntry(pAd, Idx);
924
925         if (bPassive)
926         {
927                 //BAOriSessionSetUp(pAd, &pAd->MacTab.Content[Wcid], TID, 0, 10000, TRUE);
928         }
929 }
930
931 VOID BARecSessionTearDown(
932                                                  IN OUT  PRTMP_ADAPTER   pAd,
933                                                  IN      UCHAR           Wcid,
934                                                  IN      UCHAR           TID,
935                                                  IN      BOOLEAN         bPassive)
936 {
937         ULONG           Idx = 0;
938         BA_REC_ENTRY    *pBAEntry;
939
940         if (Wcid >= MAX_LEN_OF_MAC_TABLE)
941         {
942                 return;
943         }
944
945         //
946         //  Locate corresponding BA Originator Entry in BA Table with the (pAddr,TID).
947         //
948         Idx = pAd->MacTab.Content[Wcid].BARecWcidArray[TID];
949         if (Idx == 0)
950                 return;
951
952         DBGPRINT(RT_DEBUG_TRACE,("%s===>Wcid=%d.TID=%d \n", __func__, Wcid, TID));
953
954
955         pBAEntry = &pAd->BATable.BARecEntry[Idx];
956         DBGPRINT(RT_DEBUG_TRACE,("\t===>Idx = %ld, Wcid=%d.TID=%d, REC_BA_Status = %d \n", Idx, Wcid, TID, pBAEntry->REC_BA_Status));
957         //
958         // Prepare DelBA action frame and send to the peer.
959         //
960         if ((TID == pBAEntry->TID) && (pBAEntry->REC_BA_Status == Recipient_Accept))
961         {
962                 MLME_DELBA_REQ_STRUCT   DelbaReq;
963                 BOOLEAN                                 Cancelled;
964                 MLME_QUEUE_ELEM *Elem = (MLME_QUEUE_ELEM *) kmalloc(sizeof(MLME_QUEUE_ELEM), MEM_ALLOC_FLAG);
965                 //ULONG   offset;
966                 //UINT32  VALUE;
967
968                 RTMPCancelTimer(&pBAEntry->RECBATimer, &Cancelled);
969
970                 //
971                 // 1. Send DELBA Action Frame
972                 //
973                 if (bPassive == FALSE)
974                 {
975                         NdisZeroMemory(&DelbaReq, sizeof(DelbaReq));
976                         NdisZeroMemory(Elem, sizeof(MLME_QUEUE_ELEM));
977
978                         COPY_MAC_ADDR(DelbaReq.Addr, pAd->MacTab.Content[Wcid].Addr);
979                         DelbaReq.Wcid = Wcid;
980                         DelbaReq.TID = TID;
981                         DelbaReq.Initiator = RECIPIENT;
982 #if 1
983                         Elem->MsgLen  = sizeof(DelbaReq);
984                         NdisMoveMemory(Elem->Msg, &DelbaReq, sizeof(DelbaReq));
985                         MlmeDELBAAction(pAd, Elem);
986                         kfree(Elem);
987 #else
988                         MlmeEnqueue(pAd, ACTION_STATE_MACHINE, MT2_MLME_ORI_DELBA_CATE, sizeof(MLME_DELBA_REQ_STRUCT), (PVOID)&DelbaReq);
989                         RT28XX_MLME_HANDLER(pAd);
990 #endif
991                 }
992
993
994                 //
995                 // 2. Free resource of BA session
996                 //
997                 // flush all pending reordering mpdus
998                 ba_refresh_reordering_mpdus(pAd, pBAEntry);
999
1000                 NdisAcquireSpinLock(&pAd->BATabLock);
1001
1002                 // Erase Bitmap flag.
1003                 pBAEntry->LastIndSeq = RESET_RCV_SEQ;
1004                 pBAEntry->BAWinSize = 0;
1005                 // Erase Bitmap flag at software mactable
1006                 pAd->MacTab.Content[Wcid].RXBAbitmap &= (~(1<<(pBAEntry->TID)));
1007                 pAd->MacTab.Content[Wcid].BARecWcidArray[TID] = 0;
1008
1009                 RT28XX_DEL_BA_SESSION_FROM_ASIC(pAd, Wcid, TID);
1010
1011                 NdisReleaseSpinLock(&pAd->BATabLock);
1012
1013         }
1014
1015         BATableFreeRecEntry(pAd, Idx);
1016 }
1017
1018 VOID BASessionTearDownALL(
1019                                                  IN OUT  PRTMP_ADAPTER pAd,
1020                                                  IN      UCHAR Wcid)
1021 {
1022         int i;
1023
1024         for (i=0; i<NUM_OF_TID; i++)
1025         {
1026                 BAOriSessionTearDown(pAd, Wcid, i, FALSE, FALSE);
1027                 BARecSessionTearDown(pAd, Wcid, i, FALSE);
1028         }
1029 }
1030
1031
1032 /*
1033         ==========================================================================
1034         Description:
1035                 Retry sending ADDBA Reqest.
1036
1037         IRQL = DISPATCH_LEVEL
1038
1039         Parametrs:
1040         p8023Header: if this is already 802.3 format, p8023Header is NULL
1041
1042         Return  : TRUE if put into rx reordering buffer, shouldn't indicaterxhere.
1043                                 FALSE , then continue indicaterx at this moment.
1044         ==========================================================================
1045  */
1046 VOID BAOriSessionSetupTimeout(
1047     IN PVOID SystemSpecific1,
1048     IN PVOID FunctionContext,
1049     IN PVOID SystemSpecific2,
1050     IN PVOID SystemSpecific3)
1051 {
1052         BA_ORI_ENTRY    *pBAEntry = (BA_ORI_ENTRY *)FunctionContext;
1053         MAC_TABLE_ENTRY *pEntry;
1054         PRTMP_ADAPTER   pAd;
1055
1056         if (pBAEntry == NULL)
1057                 return;
1058
1059         pAd = pBAEntry->pAdapter;
1060
1061         // Do nothing if monitor mode is on
1062         if (MONITOR_ON(pAd))
1063                 return;
1064
1065         pEntry = &pAd->MacTab.Content[pBAEntry->Wcid];
1066
1067         if ((pBAEntry->ORI_BA_Status == Originator_WaitRes) && (pBAEntry->Token < ORI_SESSION_MAX_RETRY))
1068         {
1069                 MLME_ADDBA_REQ_STRUCT    AddbaReq;
1070
1071                 NdisZeroMemory(&AddbaReq, sizeof(AddbaReq));
1072                 COPY_MAC_ADDR(AddbaReq.pAddr, pEntry->Addr);
1073                 AddbaReq.Wcid = (UCHAR)(pEntry->Aid);
1074                 AddbaReq.TID = pBAEntry->TID;
1075                 AddbaReq.BaBufSize = pAd->CommonCfg.BACapability.field.RxBAWinLimit;
1076                 AddbaReq.TimeOutValue = 0;
1077                 AddbaReq.Token = pBAEntry->Token;
1078                 MlmeEnqueue(pAd, ACTION_STATE_MACHINE, MT2_MLME_ADD_BA_CATE, sizeof(MLME_ADDBA_REQ_STRUCT), (PVOID)&AddbaReq);
1079                 RT28XX_MLME_HANDLER(pAd);
1080
1081                 DBGPRINT(RT_DEBUG_TRACE,("BA Ori Session Timeout(%d) to %02x:%02x:%02x:%02x:%02x:%02x Tid:%d Wcid:%d\n"
1082                 ,pBAEntry->Token
1083                 ,pEntry->Addr[0],pEntry->Addr[1],pEntry->Addr[2]
1084                 ,pEntry->Addr[3],pEntry->Addr[4],pEntry->Addr[5]
1085                 ,pBAEntry->TID,pEntry->Aid));
1086
1087                 pBAEntry->Token++;
1088                 RTMPSetTimer(&pBAEntry->ORIBATimer, ORI_BA_SESSION_TIMEOUT);
1089         }
1090         else
1091         {
1092                 BATableFreeOriEntry(pAd, pEntry->BAOriWcidArray[pBAEntry->TID]);
1093         }
1094 }
1095
1096 /*
1097         ==========================================================================
1098         Description:
1099                 Retry sending ADDBA Reqest.
1100
1101         IRQL = DISPATCH_LEVEL
1102
1103         Parametrs:
1104         p8023Header: if this is already 802.3 format, p8023Header is NULL
1105
1106         Return  : TRUE if put into rx reordering buffer, shouldn't indicaterxhere.
1107                                 FALSE , then continue indicaterx at this moment.
1108         ==========================================================================
1109  */
1110 VOID BARecSessionIdleTimeout(
1111     IN PVOID SystemSpecific1,
1112     IN PVOID FunctionContext,
1113     IN PVOID SystemSpecific2,
1114     IN PVOID SystemSpecific3)
1115 {
1116
1117         BA_REC_ENTRY    *pBAEntry = (BA_REC_ENTRY *)FunctionContext;
1118         PRTMP_ADAPTER   pAd;
1119         ULONG           Now32;
1120
1121         if (pBAEntry == NULL)
1122                 return;
1123
1124         if ((pBAEntry->REC_BA_Status == Recipient_Accept))
1125         {
1126                 NdisGetSystemUpTime(&Now32);
1127
1128                 if (RTMP_TIME_AFTER((unsigned long)Now32, (unsigned long)(pBAEntry->LastIndSeqAtTimer + REC_BA_SESSION_IDLE_TIMEOUT)))
1129                 {
1130                         pAd = pBAEntry->pAdapter;
1131                         // flush all pending reordering mpdus
1132                         ba_refresh_reordering_mpdus(pAd, pBAEntry);
1133                         printk("%ld: REC BA session Timeout\n", Now32);
1134                 }
1135         }
1136 }
1137
1138
1139 VOID PeerAddBAReqAction(
1140         IN PRTMP_ADAPTER pAd,
1141         IN MLME_QUEUE_ELEM *Elem)
1142
1143 {
1144         //      7.4.4.1
1145         //ULONG Idx;
1146         UCHAR   Status = 1;
1147         UCHAR   pAddr[6];
1148         FRAME_ADDBA_RSP ADDframe;
1149         PUCHAR         pOutBuffer = NULL;
1150         NDIS_STATUS     NStatus;
1151         PFRAME_ADDBA_REQ  pAddreqFrame = NULL;
1152         //UCHAR         BufSize;
1153         ULONG       FrameLen;
1154         PULONG      ptemp;
1155         PMAC_TABLE_ENTRY        pMacEntry;
1156
1157         DBGPRINT(RT_DEBUG_TRACE, ("%s ==> (Wcid = %d)\n", __func__, Elem->Wcid));
1158
1159         //hex_dump("AddBAReq", Elem->Msg, Elem->MsgLen);
1160
1161         //ADDBA Request from unknown peer, ignore this.
1162         if (Elem->Wcid >= MAX_LEN_OF_MAC_TABLE)
1163                 return;
1164
1165         pMacEntry = &pAd->MacTab.Content[Elem->Wcid];
1166         DBGPRINT(RT_DEBUG_TRACE,("BA - PeerAddBAReqAction----> \n"));
1167         ptemp = (PULONG)Elem->Msg;
1168         //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)));
1169
1170         if (PeerAddBAReqActionSanity(pAd, Elem->Msg, Elem->MsgLen, pAddr))
1171         {
1172
1173                 if ((pAd->CommonCfg.bBADecline == FALSE) && IS_HT_STA(pMacEntry))
1174                 {
1175                         pAddreqFrame = (PFRAME_ADDBA_REQ)(&Elem->Msg[0]);
1176                         printk("Rcv Wcid(%d) AddBAReq\n", Elem->Wcid);
1177                         if (BARecSessionAdd(pAd, &pAd->MacTab.Content[Elem->Wcid], pAddreqFrame))
1178                                 Status = 0;
1179                         else
1180                                 Status = 38; // more parameters have invalid values
1181                 }
1182                 else
1183                 {
1184                         Status = 37; // the request has been declined.
1185                 }
1186         }
1187
1188         if (pAd->MacTab.Content[Elem->Wcid].ValidAsCLI)
1189                 ASSERT(pAd->MacTab.Content[Elem->Wcid].Sst == SST_ASSOC);
1190
1191         pAddreqFrame = (PFRAME_ADDBA_REQ)(&Elem->Msg[0]);
1192         // 2. Always send back ADDBA Response
1193         NStatus = MlmeAllocateMemory(pAd, &pOutBuffer);  //Get an unused nonpaged memory
1194         if (NStatus != NDIS_STATUS_SUCCESS)
1195         {
1196                 DBGPRINT(RT_DEBUG_TRACE,("ACTION - PeerBAAction() allocate memory failed \n"));
1197                 return;
1198         }
1199
1200         NdisZeroMemory(&ADDframe, sizeof(FRAME_ADDBA_RSP));
1201
1202         // 2-1. Prepare ADDBA Response frame.
1203         {
1204                 if (ADHOC_ON(pAd))
1205                         ActHeaderInit(pAd, &ADDframe.Hdr, pAddr, pAd->CurrentAddress, pAd->CommonCfg.Bssid);
1206                 else
1207                         ActHeaderInit(pAd, &ADDframe.Hdr, pAd->CommonCfg.Bssid, pAd->CurrentAddress, pAddr);
1208         }
1209
1210         ADDframe.Category = CATEGORY_BA;
1211         ADDframe.Action = ADDBA_RESP;
1212         ADDframe.Token = pAddreqFrame->Token;
1213         // What is the Status code??  need to check.
1214         ADDframe.StatusCode = Status;
1215         ADDframe.BaParm.BAPolicy = IMMED_BA;
1216         ADDframe.BaParm.AMSDUSupported = 0;
1217         ADDframe.BaParm.TID = pAddreqFrame->BaParm.TID;
1218         ADDframe.BaParm.BufSize = min(((UCHAR)pAddreqFrame->BaParm.BufSize), (UCHAR)pAd->CommonCfg.BACapability.field.RxBAWinLimit);
1219         if (ADDframe.BaParm.BufSize == 0)
1220         {
1221                 ADDframe.BaParm.BufSize = 64;
1222         }
1223         ADDframe.TimeOutValue = 0; //pAddreqFrame->TimeOutValue;
1224
1225         *(USHORT *)(&ADDframe.BaParm) = cpu2le16(*(USHORT *)(&ADDframe.BaParm));
1226         ADDframe.StatusCode = cpu2le16(ADDframe.StatusCode);
1227         ADDframe.TimeOutValue = cpu2le16(ADDframe.TimeOutValue);
1228
1229         MakeOutgoingFrame(pOutBuffer,               &FrameLen,
1230                                           sizeof(FRAME_ADDBA_RSP),  &ADDframe,
1231                           END_OF_ARGS);
1232         MiniportMMRequest(pAd, QID_AC_BE, pOutBuffer, FrameLen);
1233         MlmeFreeMemory(pAd, pOutBuffer);
1234
1235         DBGPRINT(RT_DEBUG_TRACE, ("%s(%d): TID(%d), BufSize(%d) <== \n", __func__, Elem->Wcid, ADDframe.BaParm.TID,
1236                                                           ADDframe.BaParm.BufSize));
1237 }
1238
1239
1240 VOID PeerAddBARspAction(
1241         IN PRTMP_ADAPTER pAd,
1242         IN MLME_QUEUE_ELEM *Elem)
1243
1244 {
1245         //UCHAR         Idx, i;
1246         //PUCHAR                   pOutBuffer = NULL;
1247         PFRAME_ADDBA_RSP    pFrame = NULL;
1248         //PBA_ORI_ENTRY         pBAEntry;
1249
1250         //ADDBA Response from unknown peer, ignore this.
1251         if (Elem->Wcid >= MAX_LEN_OF_MAC_TABLE)
1252                 return;
1253
1254         DBGPRINT(RT_DEBUG_TRACE, ("%s ==> Wcid(%d)\n", __func__, Elem->Wcid));
1255
1256         //hex_dump("PeerAddBARspAction()", Elem->Msg, Elem->MsgLen);
1257
1258         if (PeerAddBARspActionSanity(pAd, Elem->Msg, Elem->MsgLen))
1259         {
1260                 pFrame = (PFRAME_ADDBA_RSP)(&Elem->Msg[0]);
1261
1262                 DBGPRINT(RT_DEBUG_TRACE, ("\t\t StatusCode = %d\n", pFrame->StatusCode));
1263                 switch (pFrame->StatusCode)
1264                 {
1265                         case 0:
1266                                 // I want a BAsession with this peer as an originator.
1267                                 BAOriSessionAdd(pAd, &pAd->MacTab.Content[Elem->Wcid], pFrame);
1268                                 break;
1269                         default:
1270                                 // check status == USED ???
1271                                 BAOriSessionTearDown(pAd, Elem->Wcid, pFrame->BaParm.TID, TRUE, FALSE);
1272                                 break;
1273                 }
1274                 // Rcv Decline StatusCode
1275                 if ((pFrame->StatusCode == 37)
1276             || ((pAd->OpMode == OPMODE_STA) && STA_TGN_WIFI_ON(pAd) && (pFrame->StatusCode != 0))
1277             )
1278                 {
1279                         pAd->MacTab.Content[Elem->Wcid].BADeclineBitmap |= 1<<pFrame->BaParm.TID;
1280                 }
1281         }
1282 }
1283
1284 VOID PeerDelBAAction(
1285         IN PRTMP_ADAPTER pAd,
1286         IN MLME_QUEUE_ELEM *Elem)
1287
1288 {
1289         //UCHAR                         Idx;
1290         //PUCHAR                                pOutBuffer = NULL;
1291         PFRAME_DELBA_REQ    pDelFrame = NULL;
1292
1293         DBGPRINT(RT_DEBUG_TRACE,("%s ==>\n", __func__));
1294         //DELBA Request from unknown peer, ignore this.
1295         if (PeerDelBAActionSanity(pAd, Elem->Wcid, Elem->Msg, Elem->MsgLen))
1296         {
1297                 pDelFrame = (PFRAME_DELBA_REQ)(&Elem->Msg[0]);
1298                 if (pDelFrame->DelbaParm.Initiator == ORIGINATOR)
1299                 {
1300                         DBGPRINT(RT_DEBUG_TRACE,("BA - PeerDelBAAction----> ORIGINATOR\n"));
1301                         BARecSessionTearDown(pAd, Elem->Wcid, pDelFrame->DelbaParm.TID, TRUE);
1302                 }
1303                 else
1304                 {
1305                         DBGPRINT(RT_DEBUG_TRACE,("BA - PeerDelBAAction----> RECIPIENT, Reason = %d\n",  pDelFrame->ReasonCode));
1306                         //hex_dump("DelBA Frame", pDelFrame, Elem->MsgLen);
1307                         BAOriSessionTearDown(pAd, Elem->Wcid, pDelFrame->DelbaParm.TID, TRUE, FALSE);
1308                 }
1309         }
1310 }
1311
1312
1313 BOOLEAN CntlEnqueueForRecv(
1314                                                   IN PRTMP_ADAPTER              pAd,
1315                                                   IN ULONG                              Wcid,
1316                                                   IN ULONG                              MsgLen,
1317                                                   IN PFRAME_BA_REQ              pMsg)
1318 {
1319         PFRAME_BA_REQ   pFrame = pMsg;
1320         //PRTMP_REORDERBUF      pBuffer;
1321         //PRTMP_REORDERBUF      pDmaBuf;
1322         PBA_REC_ENTRY pBAEntry;
1323         //BOOLEAN       Result;
1324         ULONG   Idx;
1325         //UCHAR NumRxPkt;
1326         UCHAR   TID;//, i;
1327
1328         TID = (UCHAR)pFrame->BARControl.TID;
1329
1330         DBGPRINT(RT_DEBUG_TRACE, ("%s(): BAR-Wcid(%ld), Tid (%d)\n", __func__, Wcid, TID));
1331         //hex_dump("BAR", (PCHAR) pFrame, MsgLen);
1332         // Do nothing if the driver is starting halt state.
1333         // This might happen when timer already been fired before cancel timer with mlmehalt
1334         if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
1335                 return FALSE;
1336
1337         // First check the size, it MUST not exceed the mlme queue size
1338         if (MsgLen > MGMT_DMA_BUFFER_SIZE)
1339         {
1340                 DBGPRINT_ERR(("CntlEnqueueForRecv: frame too large, size = %ld \n", MsgLen));
1341                 return FALSE;
1342         }
1343         else if (MsgLen != sizeof(FRAME_BA_REQ))
1344         {
1345                 DBGPRINT_ERR(("CntlEnqueueForRecv: BlockAck Request frame length size = %ld incorrect\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
1354         if ((Wcid < MAX_LEN_OF_MAC_TABLE) && (TID < 8))
1355                 {
1356                 // if this receiving packet is from SA that is in our OriEntry. Since WCID <9 has direct mapping. no need search.
1357                 Idx = pAd->MacTab.Content[Wcid].BARecWcidArray[TID];
1358                 pBAEntry = &pAd->BATable.BARecEntry[Idx];
1359                 }
1360                 else
1361                 {
1362                 return FALSE;
1363         }
1364
1365         DBGPRINT(RT_DEBUG_TRACE, ("BAR(%ld) : Tid (%d) - %04x:%04x\n", Wcid, TID, pFrame->BAStartingSeq.field.StartSeq, pBAEntry->LastIndSeq ));
1366
1367         if (SEQ_SMALLER(pBAEntry->LastIndSeq, pFrame->BAStartingSeq.field.StartSeq, MAXSEQ))
1368         {
1369                 //printk("BAR Seq = %x, LastIndSeq = %x\n", pFrame->BAStartingSeq.field.StartSeq, pBAEntry->LastIndSeq);
1370                 ba_indicate_reordering_mpdus_le_seq(pAd, pBAEntry, pFrame->BAStartingSeq.field.StartSeq);
1371                 pBAEntry->LastIndSeq = (pFrame->BAStartingSeq.field.StartSeq == 0) ? MAXSEQ :(pFrame->BAStartingSeq.field.StartSeq -1);
1372         }
1373         //ba_refresh_reordering_mpdus(pAd, pBAEntry);
1374         return TRUE;
1375 }
1376
1377 /*
1378 Description : Send PSMP Action frame If PSMP mode switches.
1379 */
1380 VOID SendPSMPAction(
1381                                    IN PRTMP_ADAPTER             pAd,
1382                                    IN UCHAR                             Wcid,
1383                                    IN UCHAR                             Psmp)
1384 {
1385         PUCHAR          pOutBuffer = NULL;
1386         NDIS_STATUS     NStatus;
1387         //ULONG           Idx;
1388         FRAME_PSMP_ACTION   Frame;
1389         ULONG           FrameLen;
1390 #ifdef RT30xx
1391         UCHAR                   bbpdata=0;
1392         UINT32                  macdata;
1393 #endif // RT30xx //
1394
1395         NStatus = MlmeAllocateMemory(pAd, &pOutBuffer);  //Get an unused nonpaged memory
1396         if (NStatus != NDIS_STATUS_SUCCESS)
1397         {
1398                 DBGPRINT(RT_DEBUG_ERROR,("BA - MlmeADDBAAction() allocate memory failed \n"));
1399                 return;
1400         }
1401
1402         ActHeaderInit(pAd, &Frame.Hdr, pAd->CommonCfg.Bssid, pAd->CurrentAddress, pAd->MacTab.Content[Wcid].Addr);
1403
1404         Frame.Category = CATEGORY_HT;
1405         Frame.Action = SMPS_ACTION;
1406         switch (Psmp)
1407         {
1408                 case MMPS_ENABLE:
1409 #ifdef RT30xx
1410                         if (IS_RT3090(pAd))
1411                         {
1412                                 // disable MMPS BBP control register
1413                                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &bbpdata);
1414                                 bbpdata &= ~(0x04);     //bit 2
1415                                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, bbpdata);
1416
1417                                 // disable MMPS MAC control register
1418                                 RTMP_IO_READ32(pAd, 0x1210, &macdata);
1419                                 macdata &= ~(0x09);     //bit 0, 3
1420                                 RTMP_IO_WRITE32(pAd, 0x1210, macdata);
1421                         }
1422 #endif // RT30xx //
1423                         Frame.Psmp = 0;
1424                         break;
1425                 case MMPS_DYNAMIC:
1426 #ifdef RT30xx
1427                         if (IS_RT3090(pAd))
1428                         {
1429                                 // enable MMPS BBP control register
1430                                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &bbpdata);
1431                                 bbpdata |= 0x04;        //bit 2
1432                                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, bbpdata);
1433
1434                                 // enable MMPS MAC control register
1435                                 RTMP_IO_READ32(pAd, 0x1210, &macdata);
1436                                 macdata |= 0x09;        //bit 0, 3
1437                                 RTMP_IO_WRITE32(pAd, 0x1210, macdata);
1438                         }
1439 #endif // RT30xx //
1440                         Frame.Psmp = 3;
1441                         break;
1442                 case MMPS_STATIC:
1443 #ifdef RT30xx
1444                         if (IS_RT3090(pAd))
1445                         {
1446                                 // enable MMPS BBP control register
1447                                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &bbpdata);
1448                                 bbpdata |= 0x04;        //bit 2
1449                                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, bbpdata);
1450
1451                                 // enable MMPS MAC control register
1452                                 RTMP_IO_READ32(pAd, 0x1210, &macdata);
1453                                 macdata |= 0x09;        //bit 0, 3
1454                                 RTMP_IO_WRITE32(pAd, 0x1210, macdata);
1455                         }
1456 #endif // RT30xx //
1457                         Frame.Psmp = 1;
1458                         break;
1459         }
1460         MakeOutgoingFrame(pOutBuffer,               &FrameLen,
1461                                           sizeof(FRAME_PSMP_ACTION),      &Frame,
1462                                           END_OF_ARGS);
1463         MiniportMMRequest(pAd, QID_AC_BE, pOutBuffer, FrameLen);
1464         MlmeFreeMemory(pAd, pOutBuffer);
1465         DBGPRINT(RT_DEBUG_ERROR,("HT - SendPSMPAction( %d )  \n", Frame.Psmp));
1466 }
1467
1468
1469 #define RADIO_MEASUREMENT_REQUEST_ACTION        0
1470
1471 typedef struct PACKED
1472 {
1473         UCHAR   RegulatoryClass;
1474         UCHAR   ChannelNumber;
1475         USHORT  RandomInterval;
1476         USHORT  MeasurementDuration;
1477         UCHAR   MeasurementMode;
1478         UCHAR   BSSID[MAC_ADDR_LEN];
1479         UCHAR   ReportingCondition;
1480         UCHAR   Threshold;
1481         UCHAR   SSIDIE[2];                      // 2 byte
1482 } BEACON_REQUEST;
1483
1484 typedef struct PACKED
1485 {
1486         UCHAR   ID;
1487         UCHAR   Length;
1488         UCHAR   Token;
1489         UCHAR   RequestMode;
1490         UCHAR   Type;
1491 } MEASUREMENT_REQ;
1492
1493
1494
1495
1496 void convert_reordering_packet_to_preAMSDU_or_802_3_packet(
1497         IN      PRTMP_ADAPTER   pAd,
1498         IN      RX_BLK                  *pRxBlk,
1499         IN  UCHAR                       FromWhichBSSID)
1500 {
1501         PNDIS_PACKET    pRxPkt;
1502         UCHAR                   Header802_3[LENGTH_802_3];
1503
1504         // 1. get 802.3 Header
1505         // 2. remove LLC
1506         //              a. pointer pRxBlk->pData to payload
1507         //      b. modify pRxBlk->DataSize
1508
1509         RTMP_802_11_REMOVE_LLC_AND_CONVERT_TO_802_3(pRxBlk, Header802_3);
1510
1511         ASSERT(pRxBlk->pRxPacket);
1512         pRxPkt = RTPKT_TO_OSPKT(pRxBlk->pRxPacket);
1513
1514         RTPKT_TO_OSPKT(pRxPkt)->dev = get_netdev_from_bssid(pAd, FromWhichBSSID);
1515         RTPKT_TO_OSPKT(pRxPkt)->data = pRxBlk->pData;
1516         RTPKT_TO_OSPKT(pRxPkt)->len = pRxBlk->DataSize;
1517         RTPKT_TO_OSPKT(pRxPkt)->tail = RTPKT_TO_OSPKT(pRxPkt)->data + RTPKT_TO_OSPKT(pRxPkt)->len;
1518
1519         //
1520         // copy 802.3 header, if necessary
1521         //
1522         if (!RX_BLK_TEST_FLAG(pRxBlk, fRX_AMSDU))
1523         {
1524 #ifdef LINUX
1525                 NdisMoveMemory(skb_push(pRxPkt, LENGTH_802_3), Header802_3, LENGTH_802_3);
1526 #endif
1527         }
1528 }
1529
1530
1531 #define INDICATE_LEGACY_OR_AMSDU(_pAd, _pRxBlk, _fromWhichBSSID)                \
1532         do                                                                                                                                      \
1533         {                                                                                                                                       \
1534         if (RX_BLK_TEST_FLAG(_pRxBlk, fRX_AMSDU))                                               \
1535         {                                                                                                                               \
1536                 Indicate_AMSDU_Packet(_pAd, _pRxBlk, _fromWhichBSSID);          \
1537         }                                                                                                                               \
1538                 else if (RX_BLK_TEST_FLAG(_pRxBlk, fRX_EAP))                                    \
1539                 {                                                                                                                               \
1540                         Indicate_EAPOL_Packet(_pAd, _pRxBlk, _fromWhichBSSID);          \
1541                 }                                                                                                                               \
1542         else                                                                                                                    \
1543         {                                                                                                                               \
1544                 Indicate_Legacy_Packet(_pAd, _pRxBlk, _fromWhichBSSID);         \
1545         }                                                                                                                               \
1546         } while (0);
1547
1548
1549
1550 static VOID ba_enqueue_reordering_packet(
1551         IN      PRTMP_ADAPTER   pAd,
1552         IN      PBA_REC_ENTRY   pBAEntry,
1553         IN      RX_BLK                  *pRxBlk,
1554         IN      UCHAR                   FromWhichBSSID)
1555 {
1556         struct reordering_mpdu *mpdu_blk;
1557         UINT16  Sequence = (UINT16) pRxBlk->pHeader->Sequence;
1558
1559         mpdu_blk = ba_mpdu_blk_alloc(pAd);
1560         if (mpdu_blk != NULL)
1561         {
1562                 // Write RxD buffer address & allocated buffer length
1563                 NdisAcquireSpinLock(&pBAEntry->RxReRingLock);
1564
1565                 mpdu_blk->Sequence = Sequence;
1566
1567                 mpdu_blk->bAMSDU = RX_BLK_TEST_FLAG(pRxBlk, fRX_AMSDU);
1568
1569                 convert_reordering_packet_to_preAMSDU_or_802_3_packet(pAd, pRxBlk, FromWhichBSSID);
1570
1571                 STATS_INC_RX_PACKETS(pAd, FromWhichBSSID);
1572
1573         //
1574                 // it is necessary for reordering packet to record
1575                 // which BSS it come from
1576                 //
1577                 RTMP_SET_PACKET_IF(pRxBlk->pRxPacket, FromWhichBSSID);
1578
1579                 mpdu_blk->pPacket = pRxBlk->pRxPacket;
1580
1581                 if (ba_reordering_mpdu_insertsorted(&pBAEntry->list, mpdu_blk) == FALSE)
1582                 {
1583                         // had been already within reordering list
1584                         // don't indicate
1585                         RELEASE_NDIS_PACKET(pAd, pRxBlk->pRxPacket, NDIS_STATUS_SUCCESS);
1586                         ba_mpdu_blk_free(pAd, mpdu_blk);
1587                 }
1588
1589                 ASSERT((0<= pBAEntry->list.qlen)  && (pBAEntry->list.qlen <= pBAEntry->BAWinSize));
1590                 NdisReleaseSpinLock(&pBAEntry->RxReRingLock);
1591         }
1592         else
1593         {
1594                 DBGPRINT(RT_DEBUG_ERROR,  ("!!! (%d) Can't allocate reordering mpdu blk\n",
1595                                                                    pBAEntry->list.qlen));
1596                 /*
1597                  * flush all pending reordering mpdus
1598                  * and receving mpdu to upper layer
1599                  * make tcp/ip to take care reordering mechanism
1600                  */
1601                 //ba_refresh_reordering_mpdus(pAd, pBAEntry);
1602                 ba_indicate_reordering_mpdus_le_seq(pAd, pBAEntry, Sequence);
1603
1604                 pBAEntry->LastIndSeq = Sequence;
1605                 INDICATE_LEGACY_OR_AMSDU(pAd, pRxBlk, FromWhichBSSID);
1606         }
1607 }
1608
1609
1610 /*
1611         ==========================================================================
1612         Description:
1613                 Indicate this packet to upper layer or put it into reordering buffer
1614
1615         Parametrs:
1616                 pRxBlk         : carry necessary packet info 802.11 format
1617                 FromWhichBSSID : the packet received from which BSS
1618
1619         Return  :
1620                           none
1621
1622         Note    :
1623                   the packet queued into reordering buffer need to cover to 802.3 format
1624                           or pre_AMSDU format
1625         ==========================================================================
1626  */
1627
1628 VOID Indicate_AMPDU_Packet(
1629         IN      PRTMP_ADAPTER   pAd,
1630         IN      RX_BLK                  *pRxBlk,
1631         IN      UCHAR                   FromWhichBSSID)
1632 {
1633         USHORT                          Idx;
1634         PBA_REC_ENTRY           pBAEntry = NULL;
1635         UINT16                          Sequence = pRxBlk->pHeader->Sequence;
1636         ULONG                           Now32;
1637         UCHAR                           Wcid = pRxBlk->pRxWI->WirelessCliID;
1638         UCHAR                           TID = pRxBlk->pRxWI->TID;
1639
1640
1641         if (!RX_BLK_TEST_FLAG(pRxBlk, fRX_AMSDU) &&  (pRxBlk->DataSize > MAX_RX_PKT_LEN))
1642         {
1643                 // release packet
1644                 RELEASE_NDIS_PACKET(pAd, pRxBlk->pRxPacket, NDIS_STATUS_FAILURE);
1645                 return;
1646         }
1647
1648         if (Wcid < MAX_LEN_OF_MAC_TABLE)
1649         {
1650                 Idx = pAd->MacTab.Content[Wcid].BARecWcidArray[TID];
1651                 if (Idx == 0)
1652                 {
1653                         /* Rec BA Session had been torn down */
1654                         INDICATE_LEGACY_OR_AMSDU(pAd, pRxBlk, FromWhichBSSID);
1655                         return;
1656                 }
1657                 pBAEntry = &pAd->BATable.BARecEntry[Idx];
1658         }
1659         else
1660         {
1661                 // impossible !!!
1662                 ASSERT(0);
1663                 // release packet
1664                 RELEASE_NDIS_PACKET(pAd, pRxBlk->pRxPacket, NDIS_STATUS_FAILURE);
1665                 return;
1666         }
1667
1668         ASSERT(pBAEntry);
1669
1670         // update last rx time
1671         NdisGetSystemUpTime(&Now32);
1672
1673         pBAEntry->rcvSeq = Sequence;
1674
1675
1676         ba_flush_reordering_timeout_mpdus(pAd, pBAEntry, Now32);
1677         pBAEntry->LastIndSeqAtTimer = Now32;
1678
1679         //
1680         // Reset Last Indicate Sequence
1681         //
1682         if (pBAEntry->LastIndSeq == RESET_RCV_SEQ)
1683         {
1684                 ASSERT((pBAEntry->list.qlen == 0) && (pBAEntry->list.next == NULL));
1685
1686                 // reset rcv sequence of BA session
1687                 pBAEntry->LastIndSeq = Sequence;
1688                 pBAEntry->LastIndSeqAtTimer = Now32;
1689                 INDICATE_LEGACY_OR_AMSDU(pAd, pRxBlk, FromWhichBSSID);
1690                 return;
1691         }
1692
1693
1694         //
1695         // I. Check if in order.
1696         //
1697         if (SEQ_STEPONE(Sequence, pBAEntry->LastIndSeq, MAXSEQ))
1698         {
1699                 USHORT  LastIndSeq;
1700
1701                 pBAEntry->LastIndSeq = Sequence;
1702                 INDICATE_LEGACY_OR_AMSDU(pAd, pRxBlk, FromWhichBSSID);
1703                 LastIndSeq = ba_indicate_reordering_mpdus_in_order(pAd, pBAEntry, pBAEntry->LastIndSeq);
1704                 if (LastIndSeq != RESET_RCV_SEQ)
1705                 {
1706                         pBAEntry->LastIndSeq = LastIndSeq;
1707                 }
1708                 pBAEntry->LastIndSeqAtTimer = Now32;
1709         }
1710         //
1711         // II. Drop Duplicated Packet
1712         //
1713         else if (Sequence == pBAEntry->LastIndSeq)
1714         {
1715
1716                 // drop and release packet
1717                 pBAEntry->nDropPacket++;
1718                 RELEASE_NDIS_PACKET(pAd, pRxBlk->pRxPacket, NDIS_STATUS_FAILURE);
1719         }
1720         //
1721         // III. Drop Old Received Packet
1722         //
1723         else if (SEQ_SMALLER(Sequence, pBAEntry->LastIndSeq, MAXSEQ))
1724         {
1725
1726                 // drop and release packet
1727                 pBAEntry->nDropPacket++;
1728                 RELEASE_NDIS_PACKET(pAd, pRxBlk->pRxPacket, NDIS_STATUS_FAILURE);
1729         }
1730         //
1731         // IV. Receive Sequence within Window Size
1732         //
1733         else if (SEQ_SMALLER(Sequence, (((pBAEntry->LastIndSeq+pBAEntry->BAWinSize+1)) & MAXSEQ), MAXSEQ))
1734         {
1735                 ba_enqueue_reordering_packet(pAd, pBAEntry, pRxBlk, FromWhichBSSID);
1736         }
1737         //
1738         // V. Receive seq surpasses Win(lastseq + nMSDU). So refresh all reorder buffer
1739         //
1740         else
1741         {
1742                 LONG WinStartSeq, TmpSeq;
1743
1744
1745                 TmpSeq = Sequence - (pBAEntry->BAWinSize) -1;
1746                 if (TmpSeq < 0)
1747                 {
1748                         TmpSeq = (MAXSEQ+1) + TmpSeq;
1749                 }
1750                 WinStartSeq = (TmpSeq+1) & MAXSEQ;
1751                 ba_indicate_reordering_mpdus_le_seq(pAd, pBAEntry, WinStartSeq);
1752                 pBAEntry->LastIndSeq = WinStartSeq; //TmpSeq;
1753
1754                 pBAEntry->LastIndSeqAtTimer = Now32;
1755
1756                 ba_enqueue_reordering_packet(pAd, pBAEntry, pRxBlk, FromWhichBSSID);
1757
1758                 TmpSeq = ba_indicate_reordering_mpdus_in_order(pAd, pBAEntry, pBAEntry->LastIndSeq);
1759                 if (TmpSeq != RESET_RCV_SEQ)
1760                 {
1761                         pBAEntry->LastIndSeq = TmpSeq;
1762                 }
1763         }
1764 }