OSDN Git Service

rtl8723au: Remove typedef for _pkt and replace with correct struct
[android-x86/external-modules-rtl8723au.git] / core / rtw_recv.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 #define _RTW_RECV_C_
21 #include <drv_conf.h>
22 #include <osdep_service.h>
23 #include <drv_types.h>
24 #include <recv_osdep.h>
25 #include <mlme_osdep.h>
26 #include <linux/ip.h>
27 #include <linux/if_ether.h>
28 #include <ethernet.h>
29 #include <usb_ops.h>
30 #include <linux/ieee80211.h>
31 #include <wifi.h>
32 #include <circ_buf.h>
33
34 #ifdef CONFIG_NEW_SIGNAL_STAT_PROCESS
35 void rtw_signal_stat_timer_hdl(RTW_TIMER_HDL_ARGS);
36 #endif /* CONFIG_NEW_SIGNAL_STAT_PROCESS */
37
38 void _rtw_init_sta_recv_priv(struct sta_recv_priv *psta_recvpriv)
39 {
40
41 _func_enter_;
42
43         memset((u8 *)psta_recvpriv, 0, sizeof (struct sta_recv_priv));
44
45         spin_lock_init(&psta_recvpriv->lock);
46
47         /* for(i=0; i<MAX_RX_NUMBLKS; i++) */
48         /*      _rtw_init_queue(&psta_recvpriv->blk_strms[i]); */
49
50         _rtw_init_queue(&psta_recvpriv->defrag_q);
51
52 _func_exit_;
53 }
54
55 int _rtw_init_recv_priv(struct recv_priv *precvpriv, _adapter *padapter)
56 {
57         int i;
58
59         union recv_frame *precvframe;
60
61         int     res=_SUCCESS;
62
63 _func_enter_;
64
65         /*  We don't need to memset padapter->XXX to zero, because adapter is allocated by rtw_zvmalloc(). */
66         /* memset((unsigned char *)precvpriv, 0, sizeof (struct  recv_priv)); */
67
68         spin_lock_init(&precvpriv->lock);
69
70         _rtw_init_queue(&precvpriv->free_recv_queue);
71         _rtw_init_queue(&precvpriv->recv_pending_queue);
72         _rtw_init_queue(&precvpriv->uc_swdec_pending_queue);
73
74         precvpriv->adapter = padapter;
75
76         precvpriv->free_recvframe_cnt = NR_RECVFRAME;
77
78         rtw_os_recv_resource_init(precvpriv, padapter);
79
80         precvpriv->pallocated_frame_buf = rtw_zvmalloc(NR_RECVFRAME * sizeof(union recv_frame) + RXFRAME_ALIGN_SZ);
81
82         if(precvpriv->pallocated_frame_buf==NULL){
83                 res= _FAIL;
84                 goto exit;
85         }
86         /* memset(precvpriv->pallocated_frame_buf, 0, NR_RECVFRAME * sizeof(union recv_frame) + RXFRAME_ALIGN_SZ); */
87
88         precvpriv->precv_frame_buf = PTR_ALIGN(precvpriv->pallocated_frame_buf, RXFRAME_ALIGN_SZ);
89
90         precvframe = (union recv_frame*) precvpriv->precv_frame_buf;
91
92         for(i=0; i < NR_RECVFRAME ; i++)
93         {
94                 INIT_LIST_HEAD(&(precvframe->u.list));
95
96                 list_add_tail(&(precvframe->u.list), &(precvpriv->free_recv_queue.queue));
97
98                 res = rtw_os_recv_resource_alloc(padapter, precvframe);
99
100                 precvframe->u.hdr.len = 0;
101
102                 precvframe->u.hdr.adapter =padapter;
103                 precvframe++;
104
105         }
106
107         precvpriv->rx_pending_cnt=1;
108
109         sema_init(&precvpriv->allrxreturnevt, 0);
110
111         res = rtw_hal_init_recv_priv(padapter);
112
113 #ifdef CONFIG_NEW_SIGNAL_STAT_PROCESS
114         _init_timer(&precvpriv->signal_stat_timer, padapter->pnetdev, RTW_TIMER_HDL_NAME(signal_stat), padapter);
115
116         precvpriv->signal_stat_sampling_interval = 1000; /* ms */
117
118         rtw_set_signal_stat_timer(precvpriv);
119 #endif /* CONFIG_NEW_SIGNAL_STAT_PROCESS */
120
121 exit:
122
123 _func_exit_;
124
125         return res;
126 }
127
128 void _rtw_free_recv_priv (struct recv_priv *precvpriv)
129 {
130         _adapter        *padapter = precvpriv->adapter;
131
132 _func_enter_;
133
134         rtw_free_uc_swdec_pending_queue(padapter);
135
136         rtw_os_recv_resource_free(precvpriv);
137
138         if(precvpriv->pallocated_frame_buf) {
139                 rtw_vmfree(precvpriv->pallocated_frame_buf, NR_RECVFRAME * sizeof(union recv_frame) + RXFRAME_ALIGN_SZ);
140         }
141
142         rtw_hal_free_recv_priv(padapter);
143
144 _func_exit_;
145 }
146
147 union recv_frame *_rtw_alloc_recvframe (_queue *pfree_recv_queue)
148 {
149         struct recv_frame_hdr *hdr;
150         struct list_head        *plist, *phead;
151         _adapter *padapter;
152         struct recv_priv *precvpriv;
153 _func_enter_;
154
155         if(_rtw_queue_empty(pfree_recv_queue) == _TRUE)
156         {
157                 hdr = NULL;
158         }
159         else
160         {
161                 phead = get_list_head(pfree_recv_queue);
162
163                 plist = phead->next;
164
165                 hdr = container_of(plist, struct recv_frame_hdr, list);
166
167                 list_del_init(&hdr->list);
168                 padapter=hdr->adapter;
169                 if(padapter !=NULL){
170                         precvpriv=&padapter->recvpriv;
171                         if(pfree_recv_queue == &precvpriv->free_recv_queue)
172                                 precvpriv->free_recvframe_cnt--;
173                 }
174         }
175
176 _func_exit_;
177         return (union recv_frame *)hdr;
178 }
179
180 union recv_frame *rtw_alloc_recvframe (_queue *pfree_recv_queue)
181 {
182         union recv_frame  *precvframe;
183
184         spin_lock_bh(&pfree_recv_queue->lock);
185
186         precvframe = _rtw_alloc_recvframe(pfree_recv_queue);
187
188         spin_unlock_bh(&pfree_recv_queue->lock);
189
190         return precvframe;
191 }
192
193 void rtw_init_recvframe(union recv_frame *precvframe, struct recv_priv *precvpriv)
194 {
195         /* Perry: This can be removed */
196         INIT_LIST_HEAD(&precvframe->u.hdr.list);
197
198         precvframe->u.hdr.len=0;
199 }
200
201 int rtw_free_recvframe(union recv_frame *precvframe, _queue *pfree_recv_queue)
202 {
203         _adapter *padapter=precvframe->u.hdr.adapter;
204         struct recv_priv *precvpriv = &padapter->recvpriv;
205
206 _func_enter_;
207
208 #ifdef CONFIG_CONCURRENT_MODE
209         if(padapter->adapter_type > PRIMARY_ADAPTER)
210         {
211                 padapter = padapter->pbuddy_adapter;/* get primary_padapter */
212                 precvpriv = &padapter->recvpriv;
213                 pfree_recv_queue = &precvpriv->free_recv_queue;
214                 precvframe->u.hdr.adapter = padapter;
215         }
216 #endif
217
218         if(precvframe->u.hdr.pkt)
219         {
220 #ifdef CONFIG_BSD_RX_USE_MBUF
221                 m_freem(precvframe->u.hdr.pkt);
222 #else   /*  CONFIG_BSD_RX_USE_MBUF */
223                 dev_kfree_skb_any(precvframe->u.hdr.pkt);/* free skb by driver */
224 #endif  /*  CONFIG_BSD_RX_USE_MBUF */
225                 precvframe->u.hdr.pkt = NULL;
226         }
227
228         spin_lock_bh(&pfree_recv_queue->lock);
229
230         list_del_init(&(precvframe->u.hdr.list));
231
232         precvframe->u.hdr.len = 0;
233
234         list_add_tail(&(precvframe->u.hdr.list), get_list_head(pfree_recv_queue));
235
236         if(padapter !=NULL){
237                 if(pfree_recv_queue == &precvpriv->free_recv_queue)
238                                 precvpriv->free_recvframe_cnt++;
239         }
240
241       spin_unlock_bh(&pfree_recv_queue->lock);
242
243 _func_exit_;
244
245         return _SUCCESS;
246 }
247
248 int _rtw_enqueue_recvframe(union recv_frame *precvframe, _queue *queue)
249 {
250
251         _adapter *padapter=precvframe->u.hdr.adapter;
252         struct recv_priv *precvpriv = &padapter->recvpriv;
253
254 _func_enter_;
255
256         /* INIT_LIST_HEAD(&(precvframe->u.hdr.list)); */
257         list_del_init(&(precvframe->u.hdr.list));
258
259         list_add_tail(&(precvframe->u.hdr.list), get_list_head(queue));
260
261         if (padapter != NULL) {
262                 if (queue == &precvpriv->free_recv_queue)
263                         precvpriv->free_recvframe_cnt++;
264         }
265
266 _func_exit_;
267
268         return _SUCCESS;
269 }
270
271 int rtw_enqueue_recvframe(union recv_frame *precvframe, _queue *queue)
272 {
273         int ret;
274
275         /* _spinlock(&pfree_recv_queue->lock); */
276         spin_lock_bh(&queue->lock);
277         ret = _rtw_enqueue_recvframe(precvframe, queue);
278         /* spin_unlock(&pfree_recv_queue->lock); */
279         spin_unlock_bh(&queue->lock);
280
281         return ret;
282 }
283
284 /*
285 int     rtw_enqueue_recvframe(union recv_frame *precvframe, _queue *queue)
286 {
287         return rtw_free_recvframe(precvframe, queue);
288 }
289 */
290
291 /*
292 caller : defrag ; recvframe_chk_defrag in recv_thread  (passive)
293 pframequeue: defrag_queue : will be accessed in recv_thread  (passive)
294
295 using spinlock to protect
296
297 */
298
299 void rtw_free_recvframe_queue(_queue *pframequeue,  _queue *pfree_recv_queue)
300 {
301         struct recv_frame_hdr *hdr;
302         struct list_head        *plist, *phead;
303
304 _func_enter_;
305         spin_lock(&pframequeue->lock);
306
307         phead = get_list_head(pframequeue);
308         plist = phead->next;
309
310         while(rtw_end_of_queue_search(phead, plist) == _FALSE) {
311                 hdr = container_of(plist, struct recv_frame_hdr, list);
312                 plist = plist->next;
313                 rtw_free_recvframe((union recv_frame *)hdr, pfree_recv_queue);
314         }
315
316         spin_unlock(&pframequeue->lock);
317
318 _func_exit_;
319 }
320
321 u32 rtw_free_uc_swdec_pending_queue(_adapter *adapter)
322 {
323         u32 cnt = 0;
324         union recv_frame *pending_frame;
325         while((pending_frame=rtw_alloc_recvframe(&adapter->recvpriv.uc_swdec_pending_queue))) {
326                 rtw_free_recvframe(pending_frame, &adapter->recvpriv.free_recv_queue);
327                 DBG_8723A("%s: dequeue uc_swdec_pending_queue\n", __func__);
328                 cnt++;
329         }
330
331         return cnt;
332 }
333
334 int rtw_enqueue_recvbuf_to_head(struct recv_buf *precvbuf, _queue *queue)
335 {
336         spin_lock_bh(&queue->lock);
337
338         list_del_init(&precvbuf->list);
339         list_add(&precvbuf->list, get_list_head(queue));
340
341         spin_unlock_bh(&queue->lock);
342
343         return _SUCCESS;
344 }
345
346 int rtw_enqueue_recvbuf(struct recv_buf *precvbuf, _queue *queue)
347 {
348         unsigned long irqL;
349         spin_lock_irqsave(&queue->lock, irqL);
350
351         list_del_init(&precvbuf->list);
352
353         list_add_tail(&precvbuf->list, get_list_head(queue));
354         spin_unlock_irqrestore(&queue->lock, irqL);
355         return _SUCCESS;
356 }
357
358 struct recv_buf *rtw_dequeue_recvbuf (_queue *queue)
359 {
360         unsigned long irqL;
361         struct recv_buf *precvbuf;
362         struct list_head        *plist, *phead;
363
364         spin_lock_irqsave(&queue->lock, irqL);
365
366         if(_rtw_queue_empty(queue) == _TRUE)
367         {
368                 precvbuf = NULL;
369         }
370         else
371         {
372                 phead = get_list_head(queue);
373
374                 plist = phead->next;
375
376                 precvbuf = container_of(plist, struct recv_buf, list);
377
378                 list_del_init(&precvbuf->list);
379
380         }
381
382         spin_unlock_irqrestore(&queue->lock, irqL);
383
384         return precvbuf;
385 }
386
387 int recvframe_chkmic(_adapter *adapter,  union recv_frame *precvframe);
388 int recvframe_chkmic(_adapter *adapter,  union recv_frame *precvframe){
389
390         int     i,res=_SUCCESS;
391         u32     datalen;
392         u8      miccode[8];
393         u8      bmic_err=_FALSE,brpt_micerror = _TRUE;
394         u8      *pframe, *payload,*pframemic;
395         u8      *mickey;
396         /* u8   *iv,rxdata_key_idx=0; */
397         struct  sta_info                *stainfo;
398         struct  rx_pkt_attrib   *prxattrib=&precvframe->u.hdr.attrib;
399         struct  security_priv   *psecuritypriv=&adapter->securitypriv;
400
401         struct mlme_ext_priv    *pmlmeext = &adapter->mlmeextpriv;
402         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
403 _func_enter_;
404
405         stainfo=rtw_get_stainfo(&adapter->stapriv ,&prxattrib->ta[0]);
406
407         if(prxattrib->encrypt ==_TKIP_)
408         {
409                 RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("\n recvframe_chkmic:prxattrib->encrypt ==_TKIP_\n"));
410                 RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("\n recvframe_chkmic:da=0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
411                         prxattrib->ra[0],prxattrib->ra[1],prxattrib->ra[2],prxattrib->ra[3],prxattrib->ra[4],prxattrib->ra[5]));
412
413                 /* calculate mic code */
414                 if (stainfo!= NULL) {
415                         if(is_multicast_ether_addr(prxattrib->ra)) {
416                                 mickey=&psecuritypriv->dot118021XGrprxmickey[prxattrib->key_index].skey[0];
417
418                                 RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("\n recvframe_chkmic: bcmc key \n"));
419
420                                 if(psecuritypriv->binstallGrpkey==_FALSE)
421                                 {
422                                         res=_FAIL;
423                                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("\n recvframe_chkmic:didn't install group key!!!!!!!!!!\n"));
424                                         DBG_8723A("\n recvframe_chkmic:didn't install group key!!!!!!!!!!\n");
425                                         goto exit;
426                                 }
427                         }
428                         else{
429                                 mickey=&stainfo->dot11tkiprxmickey.skey[0];
430                                 RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("\n recvframe_chkmic: unicast key \n"));
431                         }
432
433                         datalen=precvframe->u.hdr.len-prxattrib->hdrlen-prxattrib->iv_len-prxattrib->icv_len-8;/* icv_len included the mic code */
434                         pframe=precvframe->u.hdr.rx_data;
435                         payload=pframe+prxattrib->hdrlen+prxattrib->iv_len;
436
437                         RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("\n prxattrib->iv_len=%d prxattrib->icv_len=%d\n",prxattrib->iv_len,prxattrib->icv_len));
438
439                         rtw_seccalctkipmic(mickey,pframe,payload, datalen ,&miccode[0],(unsigned char)prxattrib->priority); /* care the length of the data */
440
441                         pframemic=payload+datalen;
442
443                         bmic_err=_FALSE;
444
445                         for(i=0;i<8;i++){
446                                 if(miccode[i] != *(pframemic+i)){
447                                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("recvframe_chkmic:miccode[%d](%02x) != *(pframemic+%d)(%02x) ",i,miccode[i],i,*(pframemic+i)));
448                                         bmic_err=_TRUE;
449                                 }
450                         }
451
452                         if(bmic_err==_TRUE){
453
454                                 RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("\n *(pframemic-8)-*(pframemic-1)=0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
455                                         *(pframemic-8),*(pframemic-7),*(pframemic-6),*(pframemic-5),*(pframemic-4),*(pframemic-3),*(pframemic-2),*(pframemic-1)));
456                                 RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("\n *(pframemic-16)-*(pframemic-9)=0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
457                                         *(pframemic-16),*(pframemic-15),*(pframemic-14),*(pframemic-13),*(pframemic-12),*(pframemic-11),*(pframemic-10),*(pframemic-9)));
458
459                                 {
460                                         uint i;
461                                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("\n ======demp packet (len=%d)======\n",precvframe->u.hdr.len));
462                                         for(i=0;i<precvframe->u.hdr.len;i=i+8){
463                                                 RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x",
464                                                         *(precvframe->u.hdr.rx_data+i),*(precvframe->u.hdr.rx_data+i+1),
465                                                         *(precvframe->u.hdr.rx_data+i+2),*(precvframe->u.hdr.rx_data+i+3),
466                                                         *(precvframe->u.hdr.rx_data+i+4),*(precvframe->u.hdr.rx_data+i+5),
467                                                         *(precvframe->u.hdr.rx_data+i+6),*(precvframe->u.hdr.rx_data+i+7)));
468                                         }
469                                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("\n ======demp packet end [len=%d]======\n",precvframe->u.hdr.len));
470                                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("\n hrdlen=%d, \n",prxattrib->hdrlen));
471                                 }
472
473                                 RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("ra=0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x psecuritypriv->binstallGrpkey=%d ",
474                                         prxattrib->ra[0],prxattrib->ra[1],prxattrib->ra[2],
475                                         prxattrib->ra[3],prxattrib->ra[4],prxattrib->ra[5],psecuritypriv->binstallGrpkey));
476
477                                 /*  double check key_index for some timing issue , */
478                                 /*  cannot compare with psecuritypriv->dot118021XGrpKeyid also cause timing issue */
479                                 if ((is_multicast_ether_addr(prxattrib->ra)) &&
480                                     (prxattrib->key_index != pmlmeinfo->key_index ))
481                                         brpt_micerror = _FALSE;
482
483                                 if((prxattrib->bdecrypted ==_TRUE)&& (brpt_micerror == _TRUE))
484                                 {
485                                         rtw_handle_tkip_mic_err(adapter,(u8)is_multicast_ether_addr(prxattrib->ra));
486                                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,(" mic error :prxattrib->bdecrypted=%d ",prxattrib->bdecrypted));
487                                         DBG_8723A(" mic error :prxattrib->bdecrypted=%d\n",prxattrib->bdecrypted);
488                                 }
489                                 else
490                                 {
491                                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,(" mic error :prxattrib->bdecrypted=%d ",prxattrib->bdecrypted));
492                                         DBG_8723A(" mic error :prxattrib->bdecrypted=%d\n",prxattrib->bdecrypted);
493                                 }
494
495                                 res=_FAIL;
496
497                         }
498                         else{
499                                 /* mic checked ok */
500                                 if((psecuritypriv->bcheck_grpkey ==_FALSE)&&(is_multicast_ether_addr(prxattrib->ra))){
501                                         psecuritypriv->bcheck_grpkey =_TRUE;
502                                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("psecuritypriv->bcheck_grpkey =_TRUE"));
503                                 }
504                         }
505
506                 }
507                 else
508                 {
509                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("recvframe_chkmic: rtw_get_stainfo==NULL!!!\n"));
510                 }
511
512                 recvframe_pull_tail(precvframe, 8);
513
514         }
515
516 exit:
517
518 _func_exit_;
519
520         return res;
521 }
522
523 /* decrypt and set the ivlen,icvlen of the recv_frame */
524 union recv_frame * decryptor(_adapter *padapter,union recv_frame *precv_frame);
525 union recv_frame * decryptor(_adapter *padapter,union recv_frame *precv_frame)
526 {
527
528         struct rx_pkt_attrib *prxattrib = &precv_frame->u.hdr.attrib;
529         struct security_priv *psecuritypriv=&padapter->securitypriv;
530         union recv_frame *return_packet=precv_frame;
531         u32      res=_SUCCESS;
532 _func_enter_;
533
534         RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("prxstat->decrypted=%x prxattrib->encrypt = 0x%03x\n",prxattrib->bdecrypted,prxattrib->encrypt));
535
536         if(prxattrib->encrypt>0)
537         {
538                 u8 *iv = precv_frame->u.hdr.rx_data+prxattrib->hdrlen;
539                 prxattrib->key_index = ( ((iv[3])>>6)&0x3) ;
540
541                 if(prxattrib->key_index > WEP_KEYS)
542                 {
543                         DBG_8723A("prxattrib->key_index(%d) > WEP_KEYS \n", prxattrib->key_index);
544
545                         switch(prxattrib->encrypt){
546                                 case _WEP40_:
547                                 case _WEP104_:
548                                         prxattrib->key_index = psecuritypriv->dot11PrivacyKeyIndex;
549                                         break;
550                                 case _TKIP_:
551                                 case _AES_:
552                                 default:
553                                         prxattrib->key_index = psecuritypriv->dot118021XGrpKeyid;
554                                         break;
555                         }
556                 }
557         }
558
559         if((prxattrib->encrypt>0) && ((prxattrib->bdecrypted==0) ||(psecuritypriv->sw_decrypt==_TRUE)))
560         {
561
562 #ifdef CONFIG_CONCURRENT_MODE
563                 if(!is_multicast_ether_addr(prxattrib->ra))/* bc/mc packets use sw decryption for concurrent mode */
564 #endif
565                 psecuritypriv->hw_decrypted=_FALSE;
566
567                 #ifdef DBG_RX_DECRYPTOR
568                 DBG_8723A("prxstat->bdecrypted:%d,  prxattrib->encrypt:%d,  Setting psecuritypriv->hw_decrypted = %d\n"
569                         , prxattrib->bdecrypted ,prxattrib->encrypt, psecuritypriv->hw_decrypted);
570                 #endif
571
572                 switch(prxattrib->encrypt){
573                 case _WEP40_:
574                 case _WEP104_:
575                         rtw_wep_decrypt(padapter, (u8 *)precv_frame);
576                         break;
577                 case _TKIP_:
578                         res = rtw_tkip_decrypt(padapter, (u8 *)precv_frame);
579                         break;
580                 case _AES_:
581                         res = rtw_aes_decrypt(padapter, (u8 * )precv_frame);
582                         break;
583                 default:
584                                 break;
585                 }
586         }
587         else if(prxattrib->bdecrypted==1
588                 && prxattrib->encrypt >0
589                 && (psecuritypriv->busetkipkey==1 || prxattrib->encrypt !=_TKIP_ )
590                 )
591         {
592                 {
593                         psecuritypriv->hw_decrypted=_TRUE;
594                         #ifdef DBG_RX_DECRYPTOR
595                         DBG_8723A("prxstat->bdecrypted:%d,  prxattrib->encrypt:%d,  Setting psecuritypriv->hw_decrypted = %d\n"
596                         , prxattrib->bdecrypted ,prxattrib->encrypt, psecuritypriv->hw_decrypted);
597                         #endif
598
599                 }
600         }
601         else {
602                 #ifdef DBG_RX_DECRYPTOR
603                 DBG_8723A("prxstat->bdecrypted:%d,  prxattrib->encrypt:%d,  psecuritypriv->hw_decrypted:%d\n"
604                 , prxattrib->bdecrypted ,prxattrib->encrypt, psecuritypriv->hw_decrypted);
605                 #endif
606         }
607
608         if(res == _FAIL)
609         {
610                 rtw_free_recvframe(return_packet,&padapter->recvpriv.free_recv_queue);
611                 return_packet = NULL;
612
613         }
614
615 _func_exit_;
616
617         return return_packet;
618 }
619 /* set the security information in the recv_frame */
620 union recv_frame * portctrl(_adapter *adapter,union recv_frame * precv_frame)
621 {
622         u8   *psta_addr, *ptr;
623         uint  auth_alg;
624         struct recv_frame_hdr *pfhdr;
625         struct sta_info *psta;
626         struct sta_priv *pstapriv ;
627         union recv_frame *prtnframe;
628         u16     ether_type=0;
629         u16  eapol_type = 0x888e;/* for Funia BD's WPA issue */
630         struct rx_pkt_attrib *pattrib;
631
632 _func_enter_;
633
634         pstapriv = &adapter->stapriv;
635         psta = rtw_get_stainfo(pstapriv, psta_addr);
636
637         auth_alg = adapter->securitypriv.dot11AuthAlgrthm;
638
639         ptr = get_recvframe_data(precv_frame);
640         pfhdr = &precv_frame->u.hdr;
641         pattrib = &pfhdr->attrib;
642         psta_addr = pattrib->ta;
643
644         prtnframe = NULL;
645
646         RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("########portctrl:adapter->securitypriv.dot11AuthAlgrthm=%d\n",adapter->securitypriv.dot11AuthAlgrthm));
647
648         if(auth_alg==2)
649         {
650                 if ((psta!=NULL) && (psta->ieee8021x_blocked))
651                 {
652                         /* blocked */
653                         /* only accept EAPOL frame */
654                         RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("########portctrl:psta->ieee8021x_blocked==1\n"));
655
656                         prtnframe=precv_frame;
657
658                         /* get ether_type */
659                         ptr=ptr+pfhdr->attrib.hdrlen+pfhdr->attrib.iv_len+LLC_HEADER_SIZE;
660                         memcpy(&ether_type,ptr, 2);
661                         ether_type= ntohs((unsigned short )ether_type);
662
663                         if (ether_type == eapol_type) {
664                                 prtnframe=precv_frame;
665                         }
666                         else {
667                                 /* free this frame */
668                                 rtw_free_recvframe(precv_frame, &adapter->recvpriv.free_recv_queue);
669                                 prtnframe=NULL;
670                         }
671                 }
672                 else
673                 {
674                         /* allowed */
675                         /* check decryption status, and decrypt the frame if needed */
676                         RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("########portctrl:psta->ieee8021x_blocked==0\n"));
677                         RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("portctrl:precv_frame->hdr.attrib.privacy=%x\n",precv_frame->u.hdr.attrib.privacy));
678
679                         if (pattrib->bdecrypted == 0)
680                         {
681                                 RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("portctrl:prxstat->decrypted=%x\n", pattrib->bdecrypted));
682                         }
683
684                         prtnframe=precv_frame;
685                         /* check is the EAPOL frame or not (Rekey) */
686                         if(ether_type == eapol_type){
687
688                                 RT_TRACE(_module_rtl871x_recv_c_,_drv_notice_,("########portctrl:ether_type == 0x888e\n"));
689                                 /* check Rekey */
690
691                                 prtnframe=precv_frame;
692                         }
693                         else{
694                                 RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("########portctrl:ether_type=0x%04x\n", ether_type));
695                         }
696                 }
697         }
698         else
699         {
700                 prtnframe=precv_frame;
701         }
702
703 _func_exit_;
704
705                 return prtnframe;
706 }
707
708 int recv_decache(union recv_frame *precv_frame, u8 bretry, struct stainfo_rxcache *prxcache);
709 int recv_decache(union recv_frame *precv_frame, u8 bretry, struct stainfo_rxcache *prxcache)
710 {
711         int tid = precv_frame->u.hdr.attrib.priority;
712
713         u16 seq_ctrl = ( (precv_frame->u.hdr.attrib.seq_num&0xffff) << 4) |
714                 (precv_frame->u.hdr.attrib.frag_num & 0xf);
715
716 _func_enter_;
717
718         if(tid>15)
719         {
720                 RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("recv_decache, (tid>15)! seq_ctrl=0x%x, tid=0x%x\n", seq_ctrl, tid));
721
722                 return _FAIL;
723         }
724
725         if(1)/* if(bretry) */
726         {
727                 if(seq_ctrl == prxcache->tid_rxseq[tid])
728                 {
729                         RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("recv_decache, seq_ctrl=0x%x, tid=0x%x, tid_rxseq=0x%x\n", seq_ctrl, tid, prxcache->tid_rxseq[tid]));
730
731                         return _FAIL;
732                 }
733         }
734
735         prxcache->tid_rxseq[tid] = seq_ctrl;
736
737 _func_exit_;
738
739         return _SUCCESS;
740 }
741
742 void process_pwrbit_data(_adapter *padapter, union recv_frame *precv_frame);
743 void process_pwrbit_data(_adapter *padapter, union recv_frame *precv_frame)
744 {
745 #ifdef CONFIG_AP_MODE
746         unsigned char pwrbit;
747         u8 *ptr = precv_frame->u.hdr.rx_data;
748         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
749         struct sta_priv *pstapriv = &padapter->stapriv;
750         struct sta_info *psta=NULL;
751
752         psta = rtw_get_stainfo(pstapriv, pattrib->src);
753
754         pwrbit = GetPwrMgt(ptr);
755
756         if(psta)
757         {
758                 if(pwrbit)
759                 {
760                         if(!(psta->state & WIFI_SLEEP_STATE))
761                         {
762                                 /* psta->state |= WIFI_SLEEP_STATE; */
763                                 /* pstapriv->sta_dz_bitmap |= BIT(psta->aid); */
764
765                                 stop_sta_xmit(padapter, psta);
766
767                                 /* DBG_8723A("to sleep, sta_dz_bitmap=%x\n", pstapriv->sta_dz_bitmap); */
768                         }
769                 }
770                 else
771                 {
772                         if(psta->state & WIFI_SLEEP_STATE)
773                         {
774                                 /* psta->state ^= WIFI_SLEEP_STATE; */
775                                 /* pstapriv->sta_dz_bitmap &= ~BIT(psta->aid); */
776
777                                 wakeup_sta_to_xmit(padapter, psta);
778
779                                 /* DBG_8723A("to wakeup, sta_dz_bitmap=%x\n", pstapriv->sta_dz_bitmap); */
780                         }
781                 }
782
783         }
784
785 #endif
786 }
787
788 void process_wmmps_data(_adapter *padapter, union recv_frame *precv_frame);
789 void process_wmmps_data(_adapter *padapter, union recv_frame *precv_frame)
790 {
791 #ifdef CONFIG_AP_MODE
792         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
793         struct sta_priv *pstapriv = &padapter->stapriv;
794         struct sta_info *psta=NULL;
795
796         psta = rtw_get_stainfo(pstapriv, pattrib->src);
797
798         if(!psta) return;
799
800 #ifdef CONFIG_TDLS
801         if( !(psta->tdls_sta_state & TDLS_LINKED_STATE ) )
802         {
803 #endif /* CONFIG_TDLS */
804
805         if(!psta->qos_option)
806                 return;
807
808         if(!(psta->qos_info&0xf))
809                 return;
810
811 #ifdef CONFIG_TDLS
812         }
813 #endif /* CONFIG_TDLS */
814
815         if(psta->state&WIFI_SLEEP_STATE)
816         {
817                 u8 wmmps_ac=0;
818
819                 switch(pattrib->priority)
820                 {
821                         case 1:
822                         case 2:
823                                 wmmps_ac = psta->uapsd_bk&BIT(1);
824                                 break;
825                         case 4:
826                         case 5:
827                                 wmmps_ac = psta->uapsd_vi&BIT(1);
828                                 break;
829                         case 6:
830                         case 7:
831                                 wmmps_ac = psta->uapsd_vo&BIT(1);
832                                 break;
833                         case 0:
834                         case 3:
835                         default:
836                                 wmmps_ac = psta->uapsd_be&BIT(1);
837                                 break;
838                 }
839
840                 if(wmmps_ac)
841                 {
842                         if(psta->sleepq_ac_len>0)
843                         {
844                                 /* process received triggered frame */
845                                 xmit_delivery_enabled_frames(padapter, psta);
846                         }
847                         else
848                         {
849                                 /* issue one qos null frame with More data bit = 0 and the EOSP bit set (=1) */
850                                 issue_qos_nulldata(padapter, psta->hwaddr, (u16)pattrib->priority, 0, 0);
851                         }
852                 }
853
854         }
855
856 #endif
857 }
858
859 #ifdef CONFIG_TDLS
860 int OnTDLS(_adapter *adapter, union recv_frame *precv_frame)
861 {
862         struct rx_pkt_attrib    *pattrib = & precv_frame->u.hdr.attrib;
863         int ret = _SUCCESS;
864         u8 *paction = get_recvframe_data(precv_frame);
865         u8 category_field = 1;
866 #ifdef CONFIG_WFD
867         u8 WFA_OUI[3] = { 0x50, 0x6f, 0x9a };
868 #endif /* CONFIG_WFD */
869         struct tdls_info *ptdlsinfo = &(adapter->tdlsinfo);
870
871         /* point to action field */
872         paction+=pattrib->hdrlen
873                         + pattrib->iv_len
874                         + SNAP_SIZE
875                         + ETH_TYPE_LEN
876                         + PAYLOAD_TYPE_LEN
877                         + category_field;
878
879         if(ptdlsinfo->enable == 0)
880         {
881                 DBG_8723A("recv tdls frame, "
882                                 "but tdls haven't enabled\n");
883                 ret = _FAIL;
884                 return ret;
885         }
886
887         switch(*paction){
888                 case TDLS_SETUP_REQUEST:
889                         DBG_8723A("recv tdls setup request frame\n");
890                         ret=On_TDLS_Setup_Req(adapter, precv_frame);
891                         break;
892                 case TDLS_SETUP_RESPONSE:
893                         DBG_8723A("recv tdls setup response frame\n");
894                         ret=On_TDLS_Setup_Rsp(adapter, precv_frame);
895                         break;
896                 case TDLS_SETUP_CONFIRM:
897                         DBG_8723A("recv tdls setup confirm frame\n");
898                         ret=On_TDLS_Setup_Cfm(adapter, precv_frame);
899                         break;
900                 case TDLS_TEARDOWN:
901                         DBG_8723A("recv tdls teardown, free sta_info\n");
902                         ret=On_TDLS_Teardown(adapter, precv_frame);
903                         break;
904                 case TDLS_DISCOVERY_REQUEST:
905                         DBG_8723A("recv tdls discovery request frame\n");
906                         ret=On_TDLS_Dis_Req(adapter, precv_frame);
907                         break;
908                 case TDLS_PEER_TRAFFIC_RESPONSE:
909                         DBG_8723A("recv tdls peer traffic response frame\n");
910                         ret=On_TDLS_Peer_Traffic_Rsp(adapter, precv_frame);
911                         break;
912                 case TDLS_CHANNEL_SWITCH_REQUEST:
913                         DBG_8723A("recv tdls channel switch request frame\n");
914                         ret=On_TDLS_Ch_Switch_Req(adapter, precv_frame);
915                         break;
916                 case TDLS_CHANNEL_SWITCH_RESPONSE:
917                         DBG_8723A("recv tdls channel switch response frame\n");
918                         ret=On_TDLS_Ch_Switch_Rsp(adapter, precv_frame);
919                         break;
920 #ifdef CONFIG_WFD
921                 case 0x50:      /* First byte of WFA OUI */
922                         if (!memcmp(WFA_OUI, (paction), 3))
923                         {
924                                 if( *(paction + 3) == 0x04)     /* Probe request frame */
925                                 {
926                                         /* WFDTDLS: for sigma test, do not setup direct link automatically */
927                                         ptdlsinfo->dev_discovered = 1;
928                                         DBG_8723A("recv tunneled probe request frame\n");
929                                         issue_tunneled_probe_rsp(adapter, precv_frame);
930                                 }
931                                 if( *(paction + 3) == 0x05)     /* Probe response frame */
932                                 {
933                                         /* WFDTDLS: for sigma test, do not setup direct link automatically */
934                                         ptdlsinfo->dev_discovered = 1;
935                                         DBG_8723A("recv tunneled probe response frame\n");
936                                 }
937                         }
938                         break;
939 #endif /* CONFIG_WFD */
940                 default:
941                         DBG_8723A("receive TDLS frame but not supported\n");
942                         ret=_FAIL;
943                         break;
944         }
945
946 exit:
947         return ret;
948 }
949 #endif
950
951 void count_rx_stats(_adapter *padapter, union recv_frame *prframe, struct sta_info*sta);
952 void count_rx_stats(_adapter *padapter, union recv_frame *prframe, struct sta_info*sta)
953 {
954         int     sz;
955         struct sta_info         *psta = NULL;
956         struct stainfo_stats    *pstats = NULL;
957         struct rx_pkt_attrib    *pattrib = & prframe->u.hdr.attrib;
958         struct recv_priv                *precvpriv = &padapter->recvpriv;
959
960         sz = get_recvframe_len(prframe);
961         precvpriv->rx_bytes += sz;
962
963         padapter->mlmepriv.LinkDetectInfo.NumRxOkInPeriod++;
964
965         if ((!is_broadcast_ether_addr(pattrib->dst)) &&
966             (!is_multicast_ether_addr(pattrib->dst)))
967                 padapter->mlmepriv.LinkDetectInfo.NumRxUnicastOkInPeriod++;
968
969         if(sta)
970                 psta = sta;
971         else
972                 psta = prframe->u.hdr.psta;
973
974         if(psta)
975         {
976                 pstats = &psta->sta_stats;
977
978                 pstats->rx_data_pkts++;
979                 pstats->rx_bytes += sz;
980         }
981 }
982
983 int sta2sta_data_frame(
984         _adapter *adapter,
985         union recv_frame *precv_frame,
986         struct sta_info**psta
987 );
988 int sta2sta_data_frame(
989         _adapter *adapter,
990         union recv_frame *precv_frame,
991         struct sta_info**psta
992 )
993 {
994         u8 *ptr = precv_frame->u.hdr.rx_data;
995         int ret = _SUCCESS;
996         struct rx_pkt_attrib *pattrib = & precv_frame->u.hdr.attrib;
997         struct  sta_priv                *pstapriv = &adapter->stapriv;
998         struct  mlme_priv       *pmlmepriv = &adapter->mlmepriv;
999         u8 *mybssid  = get_bssid(pmlmepriv);
1000         u8 *myhwaddr = myid(&adapter->eeprompriv);
1001         u8 * sta_addr = NULL;
1002         int bmcast = is_multicast_ether_addr(pattrib->dst);
1003
1004 #ifdef CONFIG_TDLS
1005         struct tdls_info *ptdlsinfo = &adapter->tdlsinfo;
1006         struct sta_info *ptdls_sta=NULL;
1007         u8 *psnap_type=ptr+pattrib->hdrlen + pattrib->iv_len+SNAP_SIZE;
1008         /* frame body located after [+2]: ether-type, [+1]: payload type */
1009         u8 *pframe_body = psnap_type+2+1;
1010 #endif
1011
1012 _func_enter_;
1013
1014         if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == _TRUE) ||
1015                 (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == _TRUE))
1016         {
1017
1018                 /*  filter packets that SA is myself or multicast or broadcast */
1019                 if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN)) {
1020                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,(" SA==myself \n"));
1021                         ret= _FAIL;
1022                         goto exit;
1023                 }
1024
1025                 if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast)) {
1026                         ret= _FAIL;
1027                         goto exit;
1028                 }
1029
1030                 if (!memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
1031                     !memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
1032                     memcmp(pattrib->bssid, mybssid, ETH_ALEN)) {
1033                         ret= _FAIL;
1034                         goto exit;
1035                 }
1036
1037                 sta_addr = pattrib->src;
1038
1039         }
1040         else if(check_fwstate(pmlmepriv, WIFI_STATION_STATE) == _TRUE)
1041         {
1042 #ifdef CONFIG_TDLS
1043
1044                 /* direct link data transfer */
1045                 if(ptdlsinfo->setup_state == TDLS_LINKED_STATE){
1046                         ptdls_sta = rtw_get_stainfo(pstapriv, pattrib->src);
1047                         if(ptdls_sta==NULL)
1048                         {
1049                                 ret=_FAIL;
1050                                 goto exit;
1051                         }
1052                         else if(ptdls_sta->tdls_sta_state&TDLS_LINKED_STATE)
1053                         {
1054
1055                                 /* drop QoS-SubType Data, including QoS NULL, excluding QoS-Data */
1056                                 if( (GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE )== WIFI_QOS_DATA_TYPE)
1057                                 {
1058                                         if(GetFrameSubType(ptr)&(BIT(4)|BIT(5)|BIT(6)))
1059                                         {
1060                                                 DBG_8723A("drop QoS-Sybtype Data\n");
1061                                         ret= _FAIL;
1062                                         goto exit;
1063                                         }
1064                                 }
1065                                 /*  filter packets that SA is myself or multicast or broadcast */
1066                                 if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN)){
1067                                         ret= _FAIL;
1068                                         goto exit;
1069                                 }
1070                                 /*  da should be for me */
1071                                 if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN))&& (!bmcast))
1072                                 {
1073                                         ret= _FAIL;
1074                                         goto exit;
1075                                 }
1076                                 /*  check BSSID */
1077                                 if (!memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
1078                                     !memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
1079                                     memcmp(pattrib->bssid, mybssid, ETH_ALEN)) {
1080                                         ret= _FAIL;
1081                                         goto exit;
1082                                 }
1083
1084                                 /* process UAPSD tdls sta */
1085                                 process_pwrbit_data(adapter, precv_frame);
1086
1087                                 /*  if NULL-frame, check pwrbit */
1088                                 if ((GetFrameSubType(ptr)) == WIFI_DATA_NULL)
1089                                 {
1090                                         /* NULL-frame with pwrbit=1, buffer_STA should buffer frames for sleep_STA */
1091                                         if(GetPwrMgt(ptr))
1092                                         {
1093                                                 DBG_8723A("TDLS: recv peer null frame with pwr bit 1\n");
1094                                                 ptdls_sta->tdls_sta_state|=TDLS_PEER_SLEEP_STATE;
1095                                         /*  it would be triggered when we are off channel and receiving NULL DATA */
1096                                         /*  we can confirm that peer STA is at off channel */
1097                                         }
1098                                         else if(ptdls_sta->tdls_sta_state&TDLS_CH_SWITCH_ON_STATE)
1099                                         {
1100                                                 if((ptdls_sta->tdls_sta_state & TDLS_PEER_AT_OFF_STATE) != TDLS_PEER_AT_OFF_STATE)
1101                                                 {
1102                                                         issue_nulldata_to_TDLS_peer_STA(adapter, ptdls_sta, 0);
1103                                                         ptdls_sta->tdls_sta_state |= TDLS_PEER_AT_OFF_STATE;
1104                                                         On_TDLS_Peer_Traffic_Rsp(adapter, precv_frame);
1105                                                 }
1106                                         }
1107
1108                                         ret= _FAIL;
1109                                         goto exit;
1110                                 }
1111                                 /* receive some of all TDLS management frames, process it at ON_TDLS */
1112                                 if (!memcmp(psnap_type, SNAP_ETH_TYPE_TDLS, 2)){
1113                                         ret = OnTDLS(adapter, precv_frame);
1114                                         goto exit;
1115                                 }
1116
1117                         }
1118
1119                         sta_addr = pattrib->src;
1120
1121                 }
1122                 else
1123 #endif /* CONFIG_TDLS */
1124                 {
1125                         /*  For Station mode, sa and bssid should always be BSSID, and DA is my mac-address */
1126                         if (memcmp(pattrib->bssid, pattrib->src, ETH_ALEN))
1127                         {
1128                                 RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("bssid != TA under STATION_MODE; drop pkt\n"));
1129                                 ret= _FAIL;
1130                                 goto exit;
1131                 }
1132
1133                 sta_addr = pattrib->bssid;
1134                 }
1135
1136         }
1137         else if(check_fwstate(pmlmepriv, WIFI_AP_STATE) == _TRUE)
1138         {
1139                 if (bmcast)
1140                 {
1141                         /*  For AP mode, if DA == MCAST, then BSSID should be also MCAST */
1142                         if (!is_multicast_ether_addr(pattrib->bssid)){
1143                                 ret= _FAIL;
1144                                 goto exit;
1145                         }
1146                 }
1147                 else /*  not mc-frame */
1148                 {
1149                         /*  For AP mode, if DA is non-MCAST, then it must be BSSID, and bssid == BSSID */
1150                         if (memcmp(pattrib->bssid, pattrib->dst, ETH_ALEN)) {
1151                                 ret= _FAIL;
1152                                 goto exit;
1153                         }
1154
1155                         sta_addr = pattrib->src;
1156                 }
1157
1158         }
1159         else if(check_fwstate(pmlmepriv, WIFI_MP_STATE) == _TRUE)
1160         {
1161                 memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
1162                 memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
1163                 memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
1164                 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
1165                 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
1166
1167                 sta_addr = mybssid;
1168         }
1169         else
1170         {
1171                 ret  = _FAIL;
1172         }
1173
1174         if(bmcast)
1175                 *psta = rtw_get_bcmc_stainfo(adapter);
1176         else
1177                 *psta = rtw_get_stainfo(pstapriv, sta_addr); /*  get ap_info */
1178
1179 #ifdef CONFIG_TDLS
1180         if(ptdls_sta != NULL)
1181                 *psta = ptdls_sta;
1182 #endif /* CONFIG_TDLS */
1183
1184         if (*psta == NULL) {
1185                 RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("can't get psta under sta2sta_data_frame ; drop pkt\n"));
1186                 ret= _FAIL;
1187                 goto exit;
1188         }
1189
1190 exit:
1191 _func_exit_;
1192         return ret;
1193 }
1194
1195 int ap2sta_data_frame(
1196         _adapter *adapter,
1197         union recv_frame *precv_frame,
1198         struct sta_info**psta );
1199 int ap2sta_data_frame(
1200         _adapter *adapter,
1201         union recv_frame *precv_frame,
1202         struct sta_info**psta )
1203 {
1204         u8 *ptr = precv_frame->u.hdr.rx_data;
1205         struct rx_pkt_attrib *pattrib = & precv_frame->u.hdr.attrib;
1206         int ret = _SUCCESS;
1207         struct  sta_priv                *pstapriv = &adapter->stapriv;
1208         struct  mlme_priv       *pmlmepriv = &adapter->mlmepriv;
1209         u8 *mybssid  = get_bssid(pmlmepriv);
1210         u8 *myhwaddr = myid(&adapter->eeprompriv);
1211         int bmcast = is_multicast_ether_addr(pattrib->dst);
1212
1213 _func_enter_;
1214
1215         if ((check_fwstate(pmlmepriv, WIFI_STATION_STATE) == _TRUE)
1216                 && (check_fwstate(pmlmepriv, _FW_LINKED) == _TRUE
1217                         || check_fwstate(pmlmepriv, _FW_UNDER_LINKING) == _TRUE )
1218                 )
1219         {
1220
1221                 /*  filter packets that SA is myself or multicast or broadcast */
1222                 if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN)) {
1223                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,(" SA==myself \n"));
1224                         #ifdef DBG_RX_DROP_FRAME
1225                         DBG_8723A("DBG_RX_DROP_FRAME %s SA="MAC_FMT", myhwaddr="MAC_FMT"\n",
1226                                 __FUNCTION__, MAC_ARG(pattrib->src), MAC_ARG(myhwaddr));
1227                         #endif
1228                         ret= _FAIL;
1229                         goto exit;
1230                 }
1231
1232                 /*  da should be for me */
1233                 if (memcmp(myhwaddr, pattrib->dst, ETH_ALEN) && (!bmcast))
1234                 {
1235                         RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,
1236                                 (" ap2sta_data_frame:  compare DA fail; DA="MAC_FMT"\n", MAC_ARG(pattrib->dst)));
1237                         #ifdef DBG_RX_DROP_FRAME
1238                         DBG_8723A("DBG_RX_DROP_FRAME %s DA="MAC_FMT"\n", __func__, MAC_ARG(pattrib->dst));
1239                         #endif
1240                         ret= _FAIL;
1241                         goto exit;
1242                 }
1243
1244                 /*  check BSSID */
1245                 if (!memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
1246                     !memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
1247                     memcmp(pattrib->bssid, mybssid, ETH_ALEN)) {
1248                         RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,
1249                                 (" ap2sta_data_frame:  compare BSSID fail ; BSSID="MAC_FMT"\n", MAC_ARG(pattrib->bssid)));
1250                         RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("mybssid="MAC_FMT"\n", MAC_ARG(mybssid)));
1251                         #ifdef DBG_RX_DROP_FRAME
1252                         DBG_8723A("DBG_RX_DROP_FRAME %s BSSID="MAC_FMT", mybssid="MAC_FMT"\n",
1253                                 __FUNCTION__, MAC_ARG(pattrib->bssid), MAC_ARG(mybssid));
1254                         DBG_8723A( "this adapter = %d, buddy adapter = %d\n", adapter->adapter_type, adapter->pbuddy_adapter->adapter_type );
1255                         #endif
1256
1257                         if(!bmcast)
1258                         {
1259                                 DBG_8723A("issue_deauth to the nonassociated ap=" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->bssid));
1260                                 issue_deauth(adapter, pattrib->bssid, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1261                         }
1262
1263                         ret= _FAIL;
1264                         goto exit;
1265                 }
1266
1267                 if(bmcast)
1268                         *psta = rtw_get_bcmc_stainfo(adapter);
1269                 else
1270                         *psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /*  get ap_info */
1271
1272                 if (*psta == NULL) {
1273                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("ap2sta: can't get psta under STATION_MODE ; drop pkt\n"));
1274                         #ifdef DBG_RX_DROP_FRAME
1275                         DBG_8723A("DBG_RX_DROP_FRAME %s can't get psta under STATION_MODE ; drop pkt\n", __FUNCTION__);
1276                         #endif
1277                         ret= _FAIL;
1278                         goto exit;
1279                 }
1280
1281                 /* if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) == WIFI_QOS_DATA_TYPE) { */
1282                 /*  */
1283
1284                 if (GetFrameSubType(ptr) & BIT(6)) {
1285                         /* No data, will not indicate to upper layer, temporily count it here */
1286                         count_rx_stats(adapter, precv_frame, *psta);
1287                         ret = RTW_RX_HANDLED;
1288                         goto exit;
1289                 }
1290
1291         }
1292         else if ((check_fwstate(pmlmepriv, WIFI_MP_STATE) == _TRUE) &&
1293                      (check_fwstate(pmlmepriv, _FW_LINKED) == _TRUE) )
1294         {
1295                 memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
1296                 memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
1297                 memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
1298                 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
1299                 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
1300
1301                 /*  */
1302                 memcpy(pattrib->bssid,  mybssid, ETH_ALEN);
1303
1304                 *psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /*  get sta_info */
1305                 if (*psta == NULL) {
1306                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("can't get psta under MP_MODE ; drop pkt\n"));
1307                         #ifdef DBG_RX_DROP_FRAME
1308                         DBG_8723A("DBG_RX_DROP_FRAME %s can't get psta under WIFI_MP_STATE ; drop pkt\n", __FUNCTION__);
1309                         #endif
1310                         ret= _FAIL;
1311                         goto exit;
1312                 }
1313
1314         }
1315         else if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == _TRUE)
1316         {
1317                 /* Special case */
1318                 ret = RTW_RX_HANDLED;
1319                 goto exit;
1320         }
1321         else
1322         {
1323                 if (!memcmp(myhwaddr, pattrib->dst, ETH_ALEN) && (!bmcast))
1324                 {
1325                         *psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /*  get sta_info */
1326                         if (*psta == NULL)
1327                         {
1328                                 DBG_8723A("issue_deauth to the ap=" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->bssid));
1329
1330                                 issue_deauth(adapter, pattrib->bssid, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1331                         }
1332                 }
1333
1334                 ret = _FAIL;
1335                 #ifdef DBG_RX_DROP_FRAME
1336                 DBG_8723A("DBG_RX_DROP_FRAME %s fw_state:0x%x\n", __FUNCTION__, get_fwstate(pmlmepriv));
1337                 #endif
1338         }
1339
1340 exit:
1341
1342 _func_exit_;
1343
1344         return ret;
1345 }
1346
1347 int sta2ap_data_frame(
1348         _adapter *adapter,
1349         union recv_frame *precv_frame,
1350         struct sta_info**psta );
1351 int sta2ap_data_frame(
1352         _adapter *adapter,
1353         union recv_frame *precv_frame,
1354         struct sta_info**psta )
1355 {
1356         u8 *ptr = precv_frame->u.hdr.rx_data;
1357         struct rx_pkt_attrib *pattrib = & precv_frame->u.hdr.attrib;
1358         struct  sta_priv                *pstapriv = &adapter->stapriv;
1359         struct  mlme_priv       *pmlmepriv = &adapter->mlmepriv;
1360         unsigned char *mybssid  = get_bssid(pmlmepriv);
1361         int ret=_SUCCESS;
1362
1363 _func_enter_;
1364
1365         if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == _TRUE)
1366         {
1367                 /* For AP mode, RA=BSSID, TX=STA(SRC_ADDR), A3=DST_ADDR */
1368                 if(memcmp(pattrib->bssid, mybssid, ETH_ALEN))
1369                 {
1370                         ret= _FAIL;
1371                         goto exit;
1372                 }
1373
1374                 *psta = rtw_get_stainfo(pstapriv, pattrib->src);
1375                 if (*psta == NULL)
1376                 {
1377                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("can't get psta under AP_MODE; drop pkt\n"));
1378                         DBG_8723A("issue_deauth to sta=" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->src));
1379
1380                         issue_deauth(adapter, pattrib->src, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1381
1382                         ret = RTW_RX_HANDLED;
1383                         goto exit;
1384                 }
1385
1386                 process_pwrbit_data(adapter, precv_frame);
1387
1388                 if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) == WIFI_QOS_DATA_TYPE) {
1389                         process_wmmps_data(adapter, precv_frame);
1390                 }
1391
1392                 if (GetFrameSubType(ptr) & BIT(6)) {
1393                         /* No data, will not indicate to upper layer, temporily count it here */
1394                         count_rx_stats(adapter, precv_frame, *psta);
1395                         ret = RTW_RX_HANDLED;
1396                         goto exit;
1397                 }
1398         }
1399         else {
1400                 u8 *myhwaddr = myid(&adapter->eeprompriv);
1401                 if (memcmp(pattrib->ra, myhwaddr, ETH_ALEN)) {
1402                         ret = RTW_RX_HANDLED;
1403                         goto exit;
1404                 }
1405                 DBG_8723A("issue_deauth to sta=" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->src));
1406                 issue_deauth(adapter, pattrib->src, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1407                 ret = RTW_RX_HANDLED;
1408                 goto exit;
1409         }
1410
1411 exit:
1412
1413 _func_exit_;
1414
1415         return ret;
1416 }
1417
1418 int validate_recv_ctrl_frame(_adapter *padapter, union recv_frame *precv_frame);
1419 int validate_recv_ctrl_frame(_adapter *padapter, union recv_frame *precv_frame)
1420 {
1421 #ifdef CONFIG_AP_MODE
1422         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1423         struct sta_priv *pstapriv = &padapter->stapriv;
1424         u8 *pframe = precv_frame->u.hdr.rx_data;
1425         /* uint len = precv_frame->u.hdr.len; */
1426
1427         /* DBG_8723A("+validate_recv_ctrl_frame\n"); */
1428
1429         if (GetFrameType(pframe) != WIFI_CTRL_TYPE)
1430         {
1431                 return _FAIL;
1432         }
1433
1434         /* receive the frames that ra(a1) is my address */
1435         if (memcmp(GetAddr1Ptr(pframe), myid(&padapter->eeprompriv), ETH_ALEN))
1436         {
1437                 return _FAIL;
1438         }
1439
1440         /* only handle ps-poll */
1441         if(GetFrameSubType(pframe) == WIFI_PSPOLL)
1442         {
1443                 u16 aid;
1444                 u8 wmmps_ac=0;
1445                 struct sta_info *psta=NULL;
1446
1447                 aid = GetAid(pframe);
1448                 psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
1449
1450                 if((psta==NULL) || (psta->aid!=aid))
1451                 {
1452                         return _FAIL;
1453                 }
1454
1455                 /* for rx pkt statistics */
1456                 psta->sta_stats.rx_ctrl_pkts++;
1457
1458                 switch(pattrib->priority)
1459                 {
1460                         case 1:
1461                         case 2:
1462                                 wmmps_ac = psta->uapsd_bk&BIT(0);
1463                                 break;
1464                         case 4:
1465                         case 5:
1466                                 wmmps_ac = psta->uapsd_vi&BIT(0);
1467                                 break;
1468                         case 6:
1469                         case 7:
1470                                 wmmps_ac = psta->uapsd_vo&BIT(0);
1471                                 break;
1472                         case 0:
1473                         case 3:
1474                         default:
1475                                 wmmps_ac = psta->uapsd_be&BIT(0);
1476                                 break;
1477                 }
1478
1479                 if(wmmps_ac)
1480                         return _FAIL;
1481
1482                 if(psta->state & WIFI_STA_ALIVE_CHK_STATE)
1483                 {
1484                         DBG_8723A("%s alive check-rx ps-poll\n", __func__);
1485                         psta->expire_to = pstapriv->expire_to;
1486                         psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
1487                 }
1488
1489                 if((psta->state&WIFI_SLEEP_STATE) && (pstapriv->sta_dz_bitmap&BIT(psta->aid)))
1490                 {
1491                         struct list_head        *xmitframe_plist, *xmitframe_phead;
1492                         struct xmit_frame *pxmitframe=NULL;
1493                         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1494
1495                         /* spin_lock_bh(&psta->sleep_q.lock); */
1496                         spin_lock_bh(&pxmitpriv->lock);
1497
1498                         xmitframe_phead = get_list_head(&psta->sleep_q);
1499                         xmitframe_plist = xmitframe_phead->next;
1500
1501                         if ((rtw_end_of_queue_search(xmitframe_phead, xmitframe_plist)) == _FALSE)
1502                         {
1503                                 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
1504
1505                                 xmitframe_plist = xmitframe_plist->next;
1506
1507                                 list_del_init(&pxmitframe->list);
1508
1509                                 psta->sleepq_len--;
1510
1511                                 if(psta->sleepq_len>0)
1512                                         pxmitframe->attrib.mdata = 1;
1513                                 else
1514                                         pxmitframe->attrib.mdata = 0;
1515
1516                                 pxmitframe->attrib.triggered = 1;
1517
1518                                 /* DBG_8723A("handling ps-poll, q_len=%d, tim=%x\n", psta->sleepq_len, pstapriv->tim_bitmap); */
1519
1520                                 rtw_hal_xmitframe_enqueue(padapter, pxmitframe);
1521
1522                                 if(psta->sleepq_len==0)
1523                                 {
1524                                         pstapriv->tim_bitmap &= ~BIT(psta->aid);
1525
1526                                         /* DBG_8723A("after handling ps-poll, tim=%x\n", pstapriv->tim_bitmap); */
1527
1528                                         /* upate BCN for TIM IE */
1529                                         /* update_BCNTIM(padapter); */
1530                                         update_beacon(padapter, _TIM_IE_, NULL, _FALSE);
1531                                 }
1532
1533                                 /* spin_unlock_bh(&psta->sleep_q.lock); */
1534                                 spin_unlock_bh(&pxmitpriv->lock);
1535
1536                         }
1537                         else
1538                         {
1539                                 /* spin_unlock_bh(&psta->sleep_q.lock); */
1540                                 spin_unlock_bh(&pxmitpriv->lock);
1541
1542                                 /* DBG_8723A("no buffered packets to xmit\n"); */
1543                                 if(pstapriv->tim_bitmap&BIT(psta->aid))
1544                                 {
1545                                         if(psta->sleepq_len==0)
1546                                         {
1547                                                 DBG_8723A("no buffered packets to xmit\n");
1548
1549                                                 /* issue nulldata with More data bit = 0 to indicate we have no buffered packets */
1550                                                 issue_nulldata(padapter, psta->hwaddr, 0, 0, 0);
1551                                         }
1552                                         else
1553                                         {
1554                                                 DBG_8723A("error!psta->sleepq_len=%d\n", psta->sleepq_len);
1555                                                 psta->sleepq_len=0;
1556                                         }
1557
1558                                         pstapriv->tim_bitmap &= ~BIT(psta->aid);
1559
1560                                         /* upate BCN for TIM IE */
1561                                         /* update_BCNTIM(padapter); */
1562                                         update_beacon(padapter, _TIM_IE_, NULL, _FALSE);
1563                                 }
1564
1565                         }
1566
1567                 }
1568
1569         }
1570
1571 #endif
1572
1573         return _FAIL;
1574 }
1575
1576 union recv_frame* recvframe_chk_defrag(PADAPTER padapter, union recv_frame *precv_frame);
1577 int validate_recv_mgnt_frame(PADAPTER padapter, union recv_frame *precv_frame);
1578 int validate_recv_mgnt_frame(PADAPTER padapter, union recv_frame *precv_frame)
1579 {
1580         /* struct mlme_priv *pmlmepriv = &adapter->mlmepriv; */
1581
1582         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("+validate_recv_mgnt_frame\n"));
1583
1584         precv_frame = recvframe_chk_defrag(padapter, precv_frame);
1585         if (precv_frame == NULL) {
1586                 RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_,("%s: fragment packet\n",__FUNCTION__));
1587                 return _SUCCESS;
1588         }
1589
1590         {
1591                 /* for rx pkt statistics */
1592                 struct sta_info *psta = rtw_get_stainfo(&padapter->stapriv, GetAddr2Ptr(precv_frame->u.hdr.rx_data));
1593                 if (psta) {
1594                         psta->sta_stats.rx_mgnt_pkts++;
1595                         if (GetFrameSubType(precv_frame->u.hdr.rx_data) == WIFI_BEACON)
1596                                 psta->sta_stats.rx_beacon_pkts++;
1597                         else if (GetFrameSubType(precv_frame->u.hdr.rx_data) == WIFI_PROBEREQ)
1598                                 psta->sta_stats.rx_probereq_pkts++;
1599                         else if (GetFrameSubType(precv_frame->u.hdr.rx_data) == WIFI_PROBERSP) {
1600                                 if (!memcmp(padapter->eeprompriv.mac_addr, GetAddr1Ptr(precv_frame->u.hdr.rx_data), ETH_ALEN))
1601                                         psta->sta_stats.rx_probersp_pkts++;
1602                                 else if (is_broadcast_ether_addr(GetAddr1Ptr(precv_frame->u.hdr.rx_data))
1603                                         || is_multicast_mac_addr(GetAddr1Ptr(precv_frame->u.hdr.rx_data)))
1604                                         psta->sta_stats.rx_probersp_bm_pkts++;
1605                                 else
1606                                         psta->sta_stats.rx_probersp_uo_pkts++;
1607                         }
1608                 }
1609         }
1610
1611 #ifdef CONFIG_INTEL_PROXIM
1612         if(padapter->proximity.proxim_on==_TRUE)
1613         {
1614                 struct rx_pkt_attrib * pattrib=&precv_frame->u.hdr.attrib;
1615                  struct recv_stat* prxstat=( struct recv_stat * )  precv_frame->u.hdr.rx_head ;
1616                  u8 * pda,*psa,*pbssid,*ptr;
1617                  ptr=precv_frame->u.hdr.rx_data;
1618                 pda = get_da(ptr);
1619                 psa = get_sa(ptr);
1620                 pbssid = get_hdr_bssid(ptr);
1621
1622                 memcpy(pattrib->dst, pda, ETH_ALEN);
1623                 memcpy(pattrib->src, psa, ETH_ALEN);
1624
1625                 memcpy(pattrib->bssid, pbssid, ETH_ALEN);
1626
1627         switch(pattrib->to_fr_ds)
1628         {
1629                 case 0:
1630                         memcpy(pattrib->ra, pda, ETH_ALEN);
1631                         memcpy(pattrib->ta, psa, ETH_ALEN);
1632                         break;
1633
1634                 case 1:
1635                         memcpy(pattrib->ra, pda, ETH_ALEN);
1636                         memcpy(pattrib->ta, pbssid, ETH_ALEN);
1637                         break;
1638
1639                 case 2:
1640                         memcpy(pattrib->ra, pbssid, ETH_ALEN);
1641                         memcpy(pattrib->ta, psa, ETH_ALEN);
1642                         break;
1643
1644                 case 3:
1645                         memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
1646                         memcpy(pattrib->ta, GetAddr2Ptr(ptr), ETH_ALEN);
1647                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,(" case 3\n"));
1648                         break;
1649
1650                 default:
1651                         break;
1652
1653                 }
1654                         pattrib->priority=0;
1655                         pattrib->hdrlen = pattrib->to_fr_ds==3 ? 30 : 24;
1656
1657                  padapter->proximity.proxim_rx(padapter,precv_frame);
1658         }
1659 #endif
1660         mgt_dispatcher(padapter, precv_frame);
1661
1662         return _SUCCESS;
1663 }
1664
1665 int validate_recv_data_frame(_adapter *adapter, union recv_frame *precv_frame);
1666 int validate_recv_data_frame(_adapter *adapter, union recv_frame *precv_frame)
1667 {
1668         u8 bretry;
1669         u8 *psa, *pda, *pbssid;
1670         struct sta_info *psta = NULL;
1671         u8 *ptr = precv_frame->u.hdr.rx_data;
1672         struct rx_pkt_attrib    *pattrib = & precv_frame->u.hdr.attrib;
1673         struct sta_priv         *pstapriv = &adapter->stapriv;
1674         struct security_priv    *psecuritypriv = &adapter->securitypriv;
1675         int ret = _SUCCESS;
1676 #ifdef CONFIG_TDLS
1677         struct tdls_info *ptdlsinfo = &adapter->tdlsinfo;
1678 #endif /* CONFIG_TDLS */
1679
1680 _func_enter_;
1681
1682         bretry = GetRetry(ptr);
1683         pda = get_da(ptr);
1684         psa = get_sa(ptr);
1685         pbssid = get_hdr_bssid(ptr);
1686
1687         if(pbssid == NULL){
1688                 #ifdef DBG_RX_DROP_FRAME
1689                 DBG_8723A("DBG_RX_DROP_FRAME %s pbssid == NULL\n", __func__);
1690                 #endif
1691                 ret= _FAIL;
1692                 goto exit;
1693         }
1694
1695         memcpy(pattrib->dst, pda, ETH_ALEN);
1696         memcpy(pattrib->src, psa, ETH_ALEN);
1697
1698         memcpy(pattrib->bssid, pbssid, ETH_ALEN);
1699
1700         switch(pattrib->to_fr_ds)
1701         {
1702                 case 0:
1703                         memcpy(pattrib->ra, pda, ETH_ALEN);
1704                         memcpy(pattrib->ta, psa, ETH_ALEN);
1705                         ret = sta2sta_data_frame(adapter, precv_frame, &psta);
1706                         break;
1707
1708                 case 1:
1709                         memcpy(pattrib->ra, pda, ETH_ALEN);
1710                         memcpy(pattrib->ta, pbssid, ETH_ALEN);
1711                         ret = ap2sta_data_frame(adapter, precv_frame, &psta);
1712                         break;
1713
1714                 case 2:
1715                         memcpy(pattrib->ra, pbssid, ETH_ALEN);
1716                         memcpy(pattrib->ta, psa, ETH_ALEN);
1717                         ret = sta2ap_data_frame(adapter, precv_frame, &psta);
1718                         break;
1719
1720                 case 3:
1721                         memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
1722                         memcpy(pattrib->ta, GetAddr2Ptr(ptr), ETH_ALEN);
1723                         ret =_FAIL;
1724                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,(" case 3\n"));
1725                         break;
1726
1727                 default:
1728                         ret =_FAIL;
1729                         break;
1730
1731         }
1732
1733         if(ret ==_FAIL){
1734                 #ifdef DBG_RX_DROP_FRAME
1735                 DBG_8723A("DBG_RX_DROP_FRAME %s case:%d, res:%d\n", __FUNCTION__, pattrib->to_fr_ds, ret);
1736                 #endif
1737                 goto exit;
1738         } else if (ret == RTW_RX_HANDLED) {
1739                 goto exit;
1740         }
1741
1742         if(psta==NULL){
1743                 RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,(" after to_fr_ds_chk; psta==NULL \n"));
1744                 #ifdef DBG_RX_DROP_FRAME
1745                 DBG_8723A("DBG_RX_DROP_FRAME %s psta == NULL\n", __func__);
1746                 #endif
1747                 ret= _FAIL;
1748                 goto exit;
1749         }
1750
1751         /* psta->rssi = prxcmd->rssi; */
1752         /* psta->signal_quality= prxcmd->sq; */
1753         precv_frame->u.hdr.psta = psta;
1754
1755         pattrib->amsdu=0;
1756         pattrib->ack_policy = 0;
1757         /* parsing QC field */
1758         if(pattrib->qos == 1)
1759         {
1760                 pattrib->priority = GetPriority((ptr + 24));
1761                 pattrib->ack_policy = GetAckpolicy((ptr + 24));
1762                 pattrib->amsdu = GetAMsdu((ptr + 24));
1763                 pattrib->hdrlen = pattrib->to_fr_ds==3 ? 32 : 26;
1764
1765                 if(pattrib->priority!=0 && pattrib->priority!=3)
1766                 {
1767                         adapter->recvpriv.bIsAnyNonBEPkts = _TRUE;
1768                 }
1769         }
1770         else
1771         {
1772                 pattrib->priority=0;
1773                 pattrib->hdrlen = pattrib->to_fr_ds==3 ? 30 : 24;
1774         }
1775
1776         if(pattrib->order)/* HT-CTRL 11n */
1777         {
1778                 pattrib->hdrlen += 4;
1779         }
1780
1781         precv_frame->u.hdr.preorder_ctrl = &psta->recvreorder_ctrl[pattrib->priority];
1782
1783         /*  decache, drop duplicate recv packets */
1784         if(recv_decache(precv_frame, bretry, &psta->sta_recvpriv.rxcache) == _FAIL)
1785         {
1786                 RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("decache : drop pkt\n"));
1787                 #ifdef DBG_RX_DROP_FRAME
1788                 DBG_8723A("DBG_RX_DROP_FRAME %s recv_decache return _FAIL\n", __func__);
1789                 #endif
1790                 ret= _FAIL;
1791                 goto exit;
1792         }
1793
1794         if(pattrib->privacy){
1795
1796                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_,
1797                          ("validate_recv_data_frame:pattrib->privacy=%x\n",
1798                          pattrib->privacy));
1799                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_,
1800                          ("\n ^^^^^^^^^^^is_multicast_ether_addr(pattrib->ra(0x%02x))=%d^^^^^^^^^^^^^^^6\n",
1801                          pattrib->ra[0], is_multicast_ether_addr(pattrib->ra)));
1802
1803 #ifdef CONFIG_TDLS
1804                 if((psta->tdls_sta_state & TDLS_LINKED_STATE) && (psta->dot118021XPrivacy==_AES_))
1805                 {
1806                         pattrib->encrypt=psta->dot118021XPrivacy;
1807                 }
1808                 else
1809 #endif /* CONFIG_TDLS */
1810                 GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt,
1811                                is_multicast_ether_addr(pattrib->ra));
1812
1813                 RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("\n pattrib->encrypt=%d\n",pattrib->encrypt));
1814
1815                 SET_ICE_IV_LEN(pattrib->iv_len, pattrib->icv_len, pattrib->encrypt);
1816         }
1817         else
1818         {
1819                 pattrib->encrypt = 0;
1820                 pattrib->iv_len = pattrib->icv_len = 0;
1821         }
1822
1823 exit:
1824
1825 _func_exit_;
1826
1827         return ret;
1828 }
1829
1830 int validate_recv_frame(_adapter *adapter, union recv_frame *precv_frame);
1831 int validate_recv_frame(_adapter *adapter, union recv_frame *precv_frame)
1832 {
1833         /* shall check frame subtype, to / from ds, da, bssid */
1834
1835         /* then call check if rx seq/frag. duplicated. */
1836
1837         u8 type;
1838         u8 subtype;
1839         int retval = _SUCCESS;
1840
1841         struct rx_pkt_attrib *pattrib = & precv_frame->u.hdr.attrib;
1842
1843         u8 *ptr = precv_frame->u.hdr.rx_data;
1844         u8  ver =(unsigned char) (*ptr)&0x3 ;
1845 #ifdef CONFIG_FIND_BEST_CHANNEL
1846         struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv;
1847 #endif
1848
1849 #ifdef CONFIG_TDLS
1850         struct tdls_info *ptdlsinfo = &adapter->tdlsinfo;
1851 #endif /* CONFIG_TDLS */
1852
1853 _func_enter_;
1854
1855 #ifdef CONFIG_FIND_BEST_CHANNEL
1856         if (pmlmeext->sitesurvey_res.state == SCAN_PROCESS) {
1857                 int ch_set_idx = rtw_ch_set_search_ch(pmlmeext->channel_set, rtw_get_oper_ch(adapter));
1858                 if (ch_set_idx >= 0)
1859                         pmlmeext->channel_set[ch_set_idx].rx_count++;
1860         }
1861 #endif
1862
1863 #ifdef CONFIG_TDLS
1864         if(ptdlsinfo->ch_sensing==1 && ptdlsinfo->cur_channel !=0){
1865                 ptdlsinfo->collect_pkt_num[ptdlsinfo->cur_channel-1]++;
1866         }
1867 #endif /* CONFIG_TDLS */
1868
1869         /* add version chk */
1870         if(ver!=0){
1871                 RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("validate_recv_data_frame fail! (ver!=0)\n"));
1872                 retval= _FAIL;
1873                 goto exit;
1874         }
1875
1876         type =  GetFrameType(ptr);
1877         subtype = GetFrameSubType(ptr); /* bit(7)~bit(2) */
1878
1879         pattrib->to_fr_ds = get_tofr_ds(ptr);
1880
1881         pattrib->frag_num = GetFragNum(ptr);
1882         pattrib->seq_num = GetSequence(ptr);
1883
1884         pattrib->pw_save = GetPwrMgt(ptr);
1885         pattrib->mfrag = GetMFrag(ptr);
1886         pattrib->mdata = GetMData(ptr);
1887         pattrib->privacy = GetPrivacy(ptr);
1888         pattrib->order = GetOrder(ptr);
1889
1890 #if 1 /* Dump rx packets */
1891 {
1892         u8 bDumpRxPkt;
1893         rtw_hal_get_def_var(adapter, HAL_DEF_DBG_DUMP_RXPKT, &(bDumpRxPkt));
1894         if(bDumpRxPkt ==1){/* dump all rx packets */
1895                 int i;
1896                 DBG_8723A("############################# \n");
1897
1898                 for(i=0; i<64;i=i+8)
1899                         DBG_8723A("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:\n", *(ptr+i),
1900                         *(ptr+i+1), *(ptr+i+2) ,*(ptr+i+3) ,*(ptr+i+4),*(ptr+i+5), *(ptr+i+6), *(ptr+i+7));
1901                 DBG_8723A("############################# \n");
1902         }
1903         else if(bDumpRxPkt ==2){
1904                 if(type== WIFI_MGT_TYPE){
1905                         int i;
1906                         DBG_8723A("############################# \n");
1907
1908                         for(i=0; i<64;i=i+8)
1909                                 DBG_8723A("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:\n", *(ptr+i),
1910                                 *(ptr+i+1), *(ptr+i+2) ,*(ptr+i+3) ,*(ptr+i+4),*(ptr+i+5), *(ptr+i+6), *(ptr+i+7));
1911                         DBG_8723A("############################# \n");
1912                 }
1913         }
1914         else if(bDumpRxPkt ==3){
1915                 if(type== WIFI_DATA_TYPE){
1916                         int i;
1917                         DBG_8723A("############################# \n");
1918
1919                         for(i=0; i<64;i=i+8)
1920                                 DBG_8723A("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:\n", *(ptr+i),
1921                                 *(ptr+i+1), *(ptr+i+2) ,*(ptr+i+3) ,*(ptr+i+4),*(ptr+i+5), *(ptr+i+6), *(ptr+i+7));
1922                         DBG_8723A("############################# \n");
1923                 }
1924         }
1925 }
1926 #endif
1927         switch (type)
1928         {
1929                 case WIFI_MGT_TYPE: /* mgnt */
1930                         retval = validate_recv_mgnt_frame(adapter, precv_frame);
1931                         if (retval == _FAIL)
1932                         {
1933                                 RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("validate_recv_mgnt_frame fail\n"));
1934                         }
1935                         retval = _FAIL; /*  only data frame return _SUCCESS */
1936                         break;
1937                 case WIFI_CTRL_TYPE: /* ctrl */
1938                         retval = validate_recv_ctrl_frame(adapter, precv_frame);
1939                         if (retval == _FAIL)
1940                         {
1941                                 RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("validate_recv_ctrl_frame fail\n"));
1942                         }
1943                         retval = _FAIL; /*  only data frame return _SUCCESS */
1944                         break;
1945                 case WIFI_DATA_TYPE: /* data */
1946                         rtw_led_control(adapter, LED_CTL_RX);
1947                         pattrib->qos = (subtype & BIT(7))? 1:0;
1948                         retval = validate_recv_data_frame(adapter, precv_frame);
1949                         if (retval == _FAIL)
1950                         {
1951                                 struct recv_priv *precvpriv = &adapter->recvpriv;
1952                                 /* RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("validate_recv_data_frame fail\n")); */
1953                                 precvpriv->rx_drop++;
1954                         }
1955                         break;
1956                 default:
1957                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("validate_recv_data_frame fail! type=0x%x\n", type));
1958                         #ifdef DBG_RX_DROP_FRAME
1959                         DBG_8723A("DBG_RX_DROP_FRAME validate_recv_data_frame fail! type=0x%x\n", type);
1960                         #endif
1961                         retval = _FAIL;
1962                         break;
1963         }
1964
1965 exit:
1966
1967 _func_exit_;
1968
1969         return retval;
1970 }
1971
1972 /* remove the wlanhdr and add the eth_hdr */
1973
1974 int wlanhdr_to_ethhdr ( union recv_frame *precvframe)
1975 {
1976         int     rmv_len;
1977         u16     eth_type, len;
1978         u8      bsnaphdr;
1979         u8      *psnap_type;
1980         struct ieee80211_snap_hdr       *psnap;
1981
1982         int ret=_SUCCESS;
1983         _adapter                        *adapter =precvframe->u.hdr.adapter;
1984         struct mlme_priv        *pmlmepriv = &adapter->mlmepriv;
1985
1986         u8      *ptr = get_recvframe_data(precvframe) ; /*  point to frame_ctrl field */
1987         struct rx_pkt_attrib *pattrib = & precvframe->u.hdr.attrib;
1988
1989 _func_enter_;
1990
1991         if(pattrib->encrypt){
1992                 recvframe_pull_tail(precvframe, pattrib->icv_len);
1993         }
1994
1995         psnap=(struct ieee80211_snap_hdr        *)(ptr+pattrib->hdrlen + pattrib->iv_len);
1996         psnap_type=ptr+pattrib->hdrlen + pattrib->iv_len+SNAP_SIZE;
1997         /* convert hdr + possible LLC headers into Ethernet header */
1998         /* eth_type = (psnap_type[0] << 8) | psnap_type[1]; */
1999         if ((!memcmp(psnap, rtw_rfc1042_header, SNAP_SIZE) &&
2000              memcmp(psnap_type, SNAP_ETH_TYPE_IPX, 2) &&
2001              memcmp(psnap_type, SNAP_ETH_TYPE_APPLETALK_AARP, 2)) ||
2002              /* eth_type != ETH_P_AARP && eth_type != ETH_P_IPX) || */
2003             !memcmp(psnap, rtw_bridge_tunnel_header, SNAP_SIZE)) {
2004                 /* remove RFC1042 or Bridge-Tunnel encapsulation and replace EtherType */
2005                 bsnaphdr = _TRUE;
2006         }
2007         else {
2008                 /* Leave Ethernet header part of hdr and full payload */
2009                 bsnaphdr = _FALSE;
2010         }
2011
2012         rmv_len = pattrib->hdrlen + pattrib->iv_len +(bsnaphdr?SNAP_SIZE:0);
2013         len = precvframe->u.hdr.len - rmv_len;
2014
2015         RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("\n===pattrib->hdrlen: %x,  pattrib->iv_len:%x ===\n\n", pattrib->hdrlen,  pattrib->iv_len));
2016
2017         memcpy(&eth_type, ptr+rmv_len, 2);
2018         eth_type= ntohs((unsigned short )eth_type); /* pattrib->ether_type */
2019         pattrib->eth_type = eth_type;
2020
2021         if ((check_fwstate(pmlmepriv, WIFI_MP_STATE) == _TRUE))
2022         {
2023                 ptr += rmv_len ;
2024                 *ptr = 0x87;
2025                 *(ptr+1) = 0x12;
2026
2027                 eth_type = 0x8712;
2028                 /*  append rx status for mp test packets */
2029                 ptr = recvframe_pull(precvframe, (rmv_len-sizeof(struct ethhdr)+2)-24);
2030                 memcpy(ptr, get_rxmem(precvframe), 24);
2031                 ptr+=24;
2032         }
2033         else {
2034                 ptr = recvframe_pull(precvframe, (rmv_len-sizeof(struct ethhdr)+ (bsnaphdr?2:0)));
2035         }
2036
2037         memcpy(ptr, pattrib->dst, ETH_ALEN);
2038         memcpy(ptr+ETH_ALEN, pattrib->src, ETH_ALEN);
2039
2040         if(!bsnaphdr) {
2041                 len = htons(len);
2042                 memcpy(ptr+12, &len, 2);
2043         }
2044
2045 _func_exit_;
2046         return ret;
2047 }
2048
2049 /* perform defrag */
2050 union recv_frame * recvframe_defrag(_adapter *adapter,_queue *defrag_q);
2051 union recv_frame * recvframe_defrag(_adapter *adapter,_queue *defrag_q)
2052 {
2053         struct list_head         *plist, *phead;
2054         u8      *data,wlanhdr_offset;
2055         u8      curfragnum;
2056         struct recv_frame_hdr *pfhdr,*pnfhdr;
2057         union recv_frame* prframe, *pnextrframe;
2058         _queue  *pfree_recv_queue;
2059
2060 _func_enter_;
2061
2062         curfragnum=0;
2063         pfree_recv_queue=&adapter->recvpriv.free_recv_queue;
2064
2065         phead = get_list_head(defrag_q);
2066         plist = phead->next;
2067         pfhdr = container_of(plist, struct recv_frame_hdr, list);
2068         prframe = (union recv_frame *)pfhdr;
2069         list_del_init(&(pfhdr->list));
2070
2071         if(curfragnum!=pfhdr->attrib.frag_num)
2072         {
2073                 /* the first fragment number must be 0 */
2074                 /* free the whole queue */
2075                 rtw_free_recvframe(prframe, pfree_recv_queue);
2076                 rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
2077
2078                 return NULL;
2079         }
2080
2081         curfragnum++;
2082
2083         plist= get_list_head(defrag_q);
2084
2085         plist = plist->next;
2086
2087         data=get_recvframe_data(prframe);
2088
2089         while(rtw_end_of_queue_search(phead, plist) == _FALSE)
2090         {
2091                 pnfhdr = container_of(plist, struct recv_frame_hdr , list);
2092                 pnextrframe = (union recv_frame *)pnfhdr;
2093                 /* check the fragment sequence  (2nd ~n fragment frame) */
2094
2095                 if(curfragnum!=pnfhdr->attrib.frag_num)
2096                 {
2097                         /* the fragment number must be increasing  (after decache) */
2098                         /* release the defrag_q & prframe */
2099                         rtw_free_recvframe(prframe, pfree_recv_queue);
2100                         rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
2101                         return NULL;
2102                 }
2103
2104                 curfragnum++;
2105
2106                 /* copy the 2nd~n fragment frame's payload to the first fragment */
2107                 /* get the 2nd~last fragment frame's payload */
2108
2109                 wlanhdr_offset = pnfhdr->attrib.hdrlen + pnfhdr->attrib.iv_len;
2110
2111                 recvframe_pull(pnextrframe, wlanhdr_offset);
2112
2113                 /* append  to first fragment frame's tail (if privacy frame, pull the ICV) */
2114                 recvframe_pull_tail(prframe, pfhdr->attrib.icv_len);
2115
2116                 /* memcpy */
2117                 memcpy(pfhdr->rx_tail, pnfhdr->rx_data, pnfhdr->len);
2118
2119                 recvframe_put(prframe, pnfhdr->len);
2120
2121                 pfhdr->attrib.icv_len=pnfhdr->attrib.icv_len;
2122                 plist = plist->next;
2123
2124         };
2125
2126         /* free the defrag_q queue and return the prframe */
2127         rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
2128
2129         RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("Performance defrag!!!!!\n"));
2130
2131 _func_exit_;
2132
2133         return prframe;
2134 }
2135
2136 /* check if need to defrag, if needed queue the frame to defrag_q */
2137 union recv_frame* recvframe_chk_defrag(PADAPTER padapter, union recv_frame *precv_frame)
2138 {
2139         u8      ismfrag;
2140         u8      fragnum;
2141         u8      *psta_addr;
2142         struct recv_frame_hdr *pfhdr;
2143         struct sta_info *psta;
2144         struct sta_priv *pstapriv;
2145         struct list_head *phead;
2146         union recv_frame *prtnframe = NULL;
2147         _queue *pfree_recv_queue, *pdefrag_q;
2148
2149 _func_enter_;
2150
2151         pstapriv = &padapter->stapriv;
2152
2153         pfhdr = &precv_frame->u.hdr;
2154
2155         pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
2156
2157         /* need to define struct of wlan header frame ctrl */
2158         ismfrag = pfhdr->attrib.mfrag;
2159         fragnum = pfhdr->attrib.frag_num;
2160
2161         psta_addr = pfhdr->attrib.ta;
2162         psta = rtw_get_stainfo(pstapriv, psta_addr);
2163         if (psta == NULL)
2164         {
2165                 u8 type = GetFrameType(pfhdr->rx_data);
2166                 if (type != WIFI_DATA_TYPE) {
2167                         psta = rtw_get_bcmc_stainfo(padapter);
2168                         pdefrag_q = &psta->sta_recvpriv.defrag_q;
2169                 } else
2170                         pdefrag_q = NULL;
2171         }
2172         else
2173                 pdefrag_q = &psta->sta_recvpriv.defrag_q;
2174
2175         if ((ismfrag==0) && (fragnum==0))
2176         {
2177                 prtnframe = precv_frame;/* isn't a fragment frame */
2178         }
2179
2180         if (ismfrag==1)
2181         {
2182                 /* 0~(n-1) fragment frame */
2183                 /* enqueue to defraf_g */
2184                 if(pdefrag_q != NULL)
2185                 {
2186                         if(fragnum==0)
2187                         {
2188                                 /* the first fragment */
2189                                 if(_rtw_queue_empty(pdefrag_q) == _FALSE)
2190                                 {
2191                                         /* free current defrag_q */
2192                                         rtw_free_recvframe_queue(pdefrag_q, pfree_recv_queue);
2193                                 }
2194                         }
2195
2196                         /* Then enqueue the 0~(n-1) fragment into the defrag_q */
2197
2198                         /* spin_lock(&pdefrag_q->lock); */
2199                         phead = get_list_head(pdefrag_q);
2200                         list_add_tail(&pfhdr->list, phead);
2201                         /* spin_unlock(&pdefrag_q->lock); */
2202
2203                         RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("Enqueuq: ismfrag = %d, fragnum= %d\n", ismfrag,fragnum));
2204
2205                         prtnframe=NULL;
2206
2207                 }
2208                 else
2209                 {
2210                         /* can't find this ta's defrag_queue, so free this recv_frame */
2211                         rtw_free_recvframe(precv_frame, pfree_recv_queue);
2212                         prtnframe=NULL;
2213                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("Free because pdefrag_q ==NULL: ismfrag = %d, fragnum= %d\n", ismfrag, fragnum));
2214                 }
2215
2216         }
2217
2218         if((ismfrag==0)&&(fragnum!=0))
2219         {
2220                 /* the last fragment frame */
2221                 /* enqueue the last fragment */
2222                 if(pdefrag_q != NULL)
2223                 {
2224                         /* spin_lock(&pdefrag_q->lock); */
2225                         phead = get_list_head(pdefrag_q);
2226                         list_add_tail(&pfhdr->list,phead);
2227                         /* spin_unlock(&pdefrag_q->lock); */
2228
2229                         /* call recvframe_defrag to defrag */
2230                         RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("defrag: ismfrag = %d, fragnum= %d\n", ismfrag, fragnum));
2231                         precv_frame = recvframe_defrag(padapter, pdefrag_q);
2232                         prtnframe=precv_frame;
2233
2234                 }
2235                 else
2236                 {
2237                         /* can't find this ta's defrag_queue, so free this recv_frame */
2238                         rtw_free_recvframe(precv_frame, pfree_recv_queue);
2239                         prtnframe=NULL;
2240                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("Free because pdefrag_q ==NULL: ismfrag = %d, fragnum= %d\n", ismfrag,fragnum));
2241                 }
2242
2243         }
2244
2245         if((prtnframe!=NULL)&&(prtnframe->u.hdr.attrib.privacy))
2246         {
2247                 /* after defrag we must check tkip mic code */
2248                 if(recvframe_chkmic(padapter,  prtnframe)==_FAIL)
2249                 {
2250                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("recvframe_chkmic(padapter,  prtnframe)==_FAIL\n"));
2251                         rtw_free_recvframe(prtnframe,pfree_recv_queue);
2252                         prtnframe=NULL;
2253                 }
2254         }
2255
2256 _func_exit_;
2257
2258         return prtnframe;
2259 }
2260
2261 #define ENDIAN_FREE 1
2262
2263 int amsdu_to_msdu(_adapter *padapter, union recv_frame *prframe);
2264 int amsdu_to_msdu(_adapter *padapter, union recv_frame *prframe)
2265 {
2266         int     a_len, padding_len;
2267         u16     eth_type, nSubframe_Length;
2268         u8      nr_subframes, i;
2269         unsigned char *pdata;
2270         struct rx_pkt_attrib *pattrib;
2271         unsigned char *data_ptr;
2272         struct sk_buff *sub_skb,*subframes[MAX_SUBFRAME_COUNT];
2273         struct recv_priv *precvpriv = &padapter->recvpriv;
2274         _queue *pfree_recv_queue = &(precvpriv->free_recv_queue);
2275         int     ret = _SUCCESS;
2276         nr_subframes = 0;
2277
2278         pattrib = &prframe->u.hdr.attrib;
2279
2280         recvframe_pull(prframe, prframe->u.hdr.attrib.hdrlen);
2281
2282         if(prframe->u.hdr.attrib.iv_len >0)
2283         {
2284                 recvframe_pull(prframe, prframe->u.hdr.attrib.iv_len);
2285         }
2286
2287         a_len = prframe->u.hdr.len;
2288
2289         pdata = prframe->u.hdr.rx_data;
2290
2291         while(a_len > ETH_HLEN) {
2292
2293                 /* Offset 12 denote 2 mac address */
2294 #ifdef ENDIAN_FREE
2295                 /* nSubframe_Length = ntohs(*((u16*)(pdata + 12))); */
2296                 nSubframe_Length = RTW_GET_BE16(pdata + 12);
2297 #else /*  ENDIAN_FREE */
2298                 nSubframe_Length = *((u16*)(pdata + 12));
2299                 /* m==>change the length order */
2300                 nSubframe_Length = (nSubframe_Length>>8) + (nSubframe_Length<<8);
2301                 /* ntohs(nSubframe_Length); */
2302 #endif /*  ENDIAN_FREE */
2303
2304                 if (a_len < (ETH_HLEN + nSubframe_Length)) {
2305                         DBG_8723A("nRemain_Length is %d and nSubframe_Length is : %d\n",a_len,nSubframe_Length);
2306                         goto exit;
2307                 }
2308
2309                 /* move the data point to data content */
2310                 pdata += ETH_HLEN;
2311                 a_len -= ETH_HLEN;
2312
2313                 /* Allocate new skb for releasing to upper layer */
2314 #ifdef CONFIG_SKB_COPY
2315                 sub_skb = dev_alloc_skb(nSubframe_Length + 12);
2316                 if(sub_skb)
2317                 {
2318                         skb_reserve(sub_skb, 12);
2319                         data_ptr = (u8 *)skb_put(sub_skb, nSubframe_Length);
2320                         memcpy(data_ptr, pdata, nSubframe_Length);
2321                 }
2322                 else
2323 #endif /*  CONFIG_SKB_COPY */
2324                 {
2325                         sub_skb = skb_clone(prframe->u.hdr.pkt, GFP_ATOMIC);
2326                         if(sub_skb)
2327                         {
2328                                 sub_skb->data = pdata;
2329                                 sub_skb->len = nSubframe_Length;
2330                                 skb_set_tail_pointer(sub_skb, nSubframe_Length);
2331                         }
2332                         else
2333                         {
2334                                 DBG_8723A("skb_clone() Fail!!! , nr_subframes = %d\n",nr_subframes);
2335                                 break;
2336                         }
2337                 }
2338
2339
2340                 /* sub_skb->dev = padapter->pnetdev; */
2341                 subframes[nr_subframes++] = sub_skb;
2342
2343                 if(nr_subframes >= MAX_SUBFRAME_COUNT) {
2344                         DBG_8723A("ParseSubframe(): Too many Subframes! Packets dropped!\n");
2345                         break;
2346                 }
2347
2348                 pdata += nSubframe_Length;
2349                 a_len -= nSubframe_Length;
2350                 if(a_len != 0) {
2351                         padding_len = 4 - ((nSubframe_Length + ETH_HLEN) & (4-1));
2352                         if(padding_len == 4) {
2353                                 padding_len = 0;
2354                         }
2355
2356                         if(a_len < padding_len) {
2357                                 goto exit;
2358                         }
2359                         pdata += padding_len;
2360                         a_len -= padding_len;
2361                 }
2362         }
2363
2364         for(i=0; i<nr_subframes; i++){
2365                 sub_skb = subframes[i];
2366                 /* convert hdr + possible LLC headers into Ethernet header */
2367 #ifdef ENDIAN_FREE
2368                 /* eth_type = ntohs(*(u16*)&sub_skb->data[6]); */
2369                 eth_type = RTW_GET_BE16(&sub_skb->data[6]);
2370 #else /*  ENDIAN_FREE */
2371                 eth_type = (sub_skb->data[6] << 8) | sub_skb->data[7];
2372 #endif /*  ENDIAN_FREE */
2373                 if (sub_skb->len >= 8 &&
2374                     ((!memcmp(sub_skb->data, rtw_rfc1042_header, SNAP_SIZE) &&
2375                       eth_type != ETH_P_AARP && eth_type != ETH_P_IPX) ||
2376                      !memcmp(sub_skb->data, rtw_bridge_tunnel_header, SNAP_SIZE) )) {
2377                         /* remove RFC1042 or Bridge-Tunnel encapsulation and replace EtherType */
2378                         skb_pull(sub_skb, SNAP_SIZE);
2379                         memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src, ETH_ALEN);
2380                         memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst, ETH_ALEN);
2381                 } else {
2382                         u16 len;
2383                         /* Leave Ethernet header part of hdr and full payload */
2384                         len = htons(sub_skb->len);
2385                         memcpy(skb_push(sub_skb, 2), &len, 2);
2386                         memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src, ETH_ALEN);
2387                         memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst, ETH_ALEN);
2388                 }
2389
2390                 /* Indicat the packets to upper layer */
2391                 {
2392 #ifdef CONFIG_BR_EXT
2393                         /*  Insert NAT2.5 RX here! */
2394                         struct mlme_priv        *pmlmepriv = &padapter->mlmepriv;
2395                         void *br_port = NULL;
2396 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 35))
2397                         br_port = padapter->pnetdev->br_port;
2398 #else   /*  (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 35)) */
2399                         rcu_read_lock();
2400                         br_port = rcu_dereference(padapter->pnetdev->rx_handler_data);
2401                         rcu_read_unlock();
2402 #endif  /*  (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 35)) */
2403
2404                         if (br_port &&
2405                             (check_fwstate(pmlmepriv, WIFI_STATION_STATE|WIFI_ADHOC_STATE) == _TRUE)) {
2406                                 int nat25_handle_frame(_adapter *priv, struct sk_buff *skb);
2407                                 if (nat25_handle_frame(padapter, sub_skb) == -1) {
2408                                         /*  bypass this frame to upper layer!! */
2409                                 }
2410                         }
2411 #endif  /*  CONFIG_BR_EXT */
2412
2413                         sub_skb->protocol = eth_type_trans(sub_skb, padapter->pnetdev);
2414                         sub_skb->dev = padapter->pnetdev;
2415
2416 #ifdef CONFIG_TCP_CSUM_OFFLOAD_RX
2417                         if ( (pattrib->tcpchk_valid == 1) && (pattrib->tcp_chkrpt == 1) ) {
2418                                 sub_skb->ip_summed = CHECKSUM_UNNECESSARY;
2419                         } else {
2420                                 sub_skb->ip_summed = CHECKSUM_NONE;
2421                         }
2422 #else /* !CONFIG_TCP_CSUM_OFFLOAD_RX */
2423                         sub_skb->ip_summed = CHECKSUM_NONE;
2424 #endif /* CONFIG_TCP_CSUM_OFFLOAD_RX */
2425
2426                         netif_rx(sub_skb);
2427                 }
2428         }
2429
2430 exit:
2431
2432         prframe->u.hdr.len=0;
2433         rtw_free_recvframe(prframe, pfree_recv_queue);/* free this recv_frame */
2434
2435         return ret;
2436 }
2437
2438 int check_indicate_seq(struct recv_reorder_ctrl *preorder_ctrl, u16 seq_num);
2439 int check_indicate_seq(struct recv_reorder_ctrl *preorder_ctrl, u16 seq_num)
2440 {
2441         u8      wsize = preorder_ctrl->wsize_b;
2442         u16     wend = (preorder_ctrl->indicate_seq + wsize -1) & 0xFFF;/*  4096; */
2443
2444         /*  Rx Reorder initialize condition. */
2445         if (preorder_ctrl->indicate_seq == 0xFFFF)
2446         {
2447                 preorder_ctrl->indicate_seq = seq_num;
2448                 #ifdef DBG_RX_SEQ
2449                 DBG_8723A("DBG_RX_SEQ %s:%d init IndicateSeq: %d, NewSeq: %d\n", __FUNCTION__, __LINE__,
2450                         preorder_ctrl->indicate_seq, seq_num);
2451                 #endif
2452
2453                 /* DbgPrint("check_indicate_seq, 1st->indicate_seq=%d\n", precvpriv->indicate_seq); */
2454         }
2455
2456         /* DbgPrint("enter->check_indicate_seq(): IndicateSeq: %d, NewSeq: %d\n", precvpriv->indicate_seq, seq_num); */
2457
2458         /*  Drop out the packet which SeqNum is smaller than WinStart */
2459         if( SN_LESS(seq_num, preorder_ctrl->indicate_seq) )
2460         {
2461                 /* RT_TRACE(COMP_RX_REORDER, DBG_LOUD, ("CheckRxTsIndicateSeq(): Packet Drop! IndicateSeq: %d, NewSeq: %d\n", pTS->RxIndicateSeq, NewSeqNum)); */
2462                 /* DbgPrint("CheckRxTsIndicateSeq(): Packet Drop! IndicateSeq: %d, NewSeq: %d\n", precvpriv->indicate_seq, seq_num); */
2463
2464                 #ifdef DBG_RX_DROP_FRAME
2465                 DBG_8723A("%s IndicateSeq: %d > NewSeq: %d\n", __FUNCTION__,
2466                         preorder_ctrl->indicate_seq, seq_num);
2467                 #endif
2468
2469                 return _FALSE;
2470         }
2471
2472         /*  */
2473         /*  Sliding window manipulation. Conditions includes: */
2474         /*  1. Incoming SeqNum is equal to WinStart =>Window shift 1 */
2475         /*  2. Incoming SeqNum is larger than the WinEnd => Window shift N */
2476         /*  */
2477         if( SN_EQUAL(seq_num, preorder_ctrl->indicate_seq) )
2478         {
2479                 preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
2480                 #ifdef DBG_RX_SEQ
2481                 DBG_8723A("DBG_RX_SEQ %s:%d SN_EQUAL IndicateSeq: %d, NewSeq: %d\n", __FUNCTION__, __LINE__,
2482                         preorder_ctrl->indicate_seq, seq_num);
2483                 #endif
2484         }
2485         else if(SN_LESS(wend, seq_num))
2486         {
2487                 /* RT_TRACE(COMP_RX_REORDER, DBG_LOUD, ("CheckRxTsIndicateSeq(): Window Shift! IndicateSeq: %d, NewSeq: %d\n", pTS->RxIndicateSeq, NewSeqNum)); */
2488                 /* DbgPrint("CheckRxTsIndicateSeq(): Window Shift! IndicateSeq: %d, NewSeq: %d\n", precvpriv->indicate_seq, seq_num); */
2489
2490                 /*  boundary situation, when seq_num cross 0xFFF */
2491                 if(seq_num >= (wsize - 1))
2492                         preorder_ctrl->indicate_seq = seq_num + 1 -wsize;
2493                 else
2494                         preorder_ctrl->indicate_seq = 0xFFF - (wsize - (seq_num + 1)) + 1;
2495
2496                 #ifdef DBG_RX_SEQ
2497                 DBG_8723A("DBG_RX_SEQ %s:%d SN_LESS(wend, seq_num) IndicateSeq: %d, NewSeq: %d\n", __FUNCTION__, __LINE__,
2498                         preorder_ctrl->indicate_seq, seq_num);
2499                 #endif
2500         }
2501
2502         /* DbgPrint("exit->check_indicate_seq(): IndicateSeq: %d, NewSeq: %d\n", precvpriv->indicate_seq, seq_num); */
2503
2504         return _TRUE;
2505 }
2506
2507 int enqueue_reorder_recvframe(struct recv_reorder_ctrl *preorder_ctrl, union recv_frame *prframe);
2508 int enqueue_reorder_recvframe(struct recv_reorder_ctrl *preorder_ctrl, union recv_frame *prframe)
2509 {
2510         struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
2511         _queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
2512         struct list_head        *phead, *plist;
2513         struct recv_frame_hdr *hdr;
2514         struct rx_pkt_attrib *pnextattrib;
2515
2516         /* DbgPrint("+enqueue_reorder_recvframe()\n"); */
2517
2518         /* spin_lock_irqsave(&ppending_recvframe_queue->lock); */
2519         /* spin_lock_ex(&ppending_recvframe_queue->lock); */
2520
2521         phead = get_list_head(ppending_recvframe_queue);
2522         plist = phead->next;
2523
2524         while(rtw_end_of_queue_search(phead, plist) == _FALSE)
2525         {
2526                 hdr = container_of(plist, struct recv_frame_hdr, list);
2527                 pnextattrib = &hdr->attrib;
2528
2529                 if(SN_LESS(pnextattrib->seq_num, pattrib->seq_num))
2530                 {
2531                         plist = plist->next;
2532                 }
2533                 else if( SN_EQUAL(pnextattrib->seq_num, pattrib->seq_num))
2534                 {
2535                         /* Duplicate entry is found!! Do not insert current entry. */
2536                         /* RT_TRACE(COMP_RX_REORDER, DBG_TRACE, ("InsertRxReorderList(): Duplicate packet is dropped!! IndicateSeq: %d, NewSeq: %d\n", pTS->RxIndicateSeq, SeqNum)); */
2537
2538                         /* spin_unlock_irqrestore(&ppending_recvframe_queue->lock); */
2539
2540                         return _FALSE;
2541                 }
2542                 else
2543                 {
2544                         break;
2545                 }
2546
2547                 /* DbgPrint("enqueue_reorder_recvframe():while\n"); */
2548
2549         }
2550
2551         /* spin_lock_irqsave(&ppending_recvframe_queue->lock); */
2552         /* spin_lock_ex(&ppending_recvframe_queue->lock); */
2553
2554         list_del_init(&(prframe->u.hdr.list));
2555
2556         list_add_tail(&(prframe->u.hdr.list), plist);
2557
2558         /* spin_unlock_ex(&ppending_recvframe_queue->lock); */
2559         /* spin_unlock_irqrestore(&ppending_recvframe_queue->lock); */
2560
2561         /* RT_TRACE(COMP_RX_REORDER, DBG_TRACE, ("InsertRxReorderList(): Pkt insert into buffer!! IndicateSeq: %d, NewSeq: %d\n", pTS->RxIndicateSeq, SeqNum)); */
2562         return _TRUE;
2563 }
2564
2565 int recv_indicatepkts_in_order(_adapter *padapter, struct recv_reorder_ctrl *preorder_ctrl, int bforced);
2566 int recv_indicatepkts_in_order(_adapter *padapter, struct recv_reorder_ctrl *preorder_ctrl, int bforced)
2567 {
2568         /* u8 bcancelled; */
2569         struct list_head        *phead, *plist;
2570         union recv_frame *prframe;
2571         struct recv_frame_hdr *prhdr;
2572         struct rx_pkt_attrib *pattrib;
2573         /* u8 index = 0; */
2574         int bPktInBuf = _FALSE;
2575         struct recv_priv *precvpriv = &padapter->recvpriv;
2576         _queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
2577
2578         /* DbgPrint("+recv_indicatepkts_in_order\n"); */
2579
2580         /* spin_lock_irqsave(&ppending_recvframe_queue->lock); */
2581         /* spin_lock_ex(&ppending_recvframe_queue->lock); */
2582
2583         phead =         get_list_head(ppending_recvframe_queue);
2584         plist = phead->next;
2585
2586         /*  Handling some condition for forced indicate case. */
2587         if(bforced==_TRUE)
2588         {
2589                 if (list_empty(phead)) {
2590                         /*  spin_unlock_irqrestore(&ppending_recvframe_queue->lock); */
2591                         /* spin_unlock_ex(&ppending_recvframe_queue->lock); */
2592                         return _TRUE;
2593                 }
2594
2595                 prhdr = container_of(plist, struct recv_frame_hdr, list);
2596                 pattrib = &prhdr->attrib;
2597                 preorder_ctrl->indicate_seq = pattrib->seq_num;
2598                 #ifdef DBG_RX_SEQ
2599                 DBG_8723A("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __FUNCTION__, __LINE__,
2600                         preorder_ctrl->indicate_seq, pattrib->seq_num);
2601                 #endif
2602         }
2603
2604         /*  Prepare indication list and indication. */
2605         /*  Check if there is any packet need indicate. */
2606         while (!list_empty(phead)) {
2607
2608                 prhdr = container_of(plist, struct recv_frame_hdr, list);
2609                 prframe = (union recv_frame *)prhdr;
2610                 pattrib = &prhdr->attrib;
2611
2612                 if(!SN_LESS(preorder_ctrl->indicate_seq, pattrib->seq_num))
2613                 {
2614                         RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_,
2615                                  ("recv_indicatepkts_in_order: indicate=%d seq=%d amsdu=%d\n",
2616                                   preorder_ctrl->indicate_seq, pattrib->seq_num, pattrib->amsdu));
2617
2618                         plist = plist->next;
2619                         list_del_init(&(prframe->u.hdr.list));
2620
2621                         if(SN_EQUAL(preorder_ctrl->indicate_seq, pattrib->seq_num))
2622                         {
2623                                 preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
2624                                 #ifdef DBG_RX_SEQ
2625                                 DBG_8723A("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __FUNCTION__, __LINE__,
2626                                         preorder_ctrl->indicate_seq, pattrib->seq_num);
2627                                 #endif
2628                         }
2629
2630                         if(!pattrib->amsdu)
2631                         {
2632                                 if ((padapter->bDriverStopped == _FALSE) &&
2633                                     (padapter->bSurpriseRemoved == _FALSE))
2634                                 {
2635
2636                                         rtw_recv_indicatepkt(padapter, prframe);/* indicate this recv_frame */
2637
2638                                 }
2639                         }
2640                         else if(pattrib->amsdu==1)
2641                         {
2642                                 if(amsdu_to_msdu(padapter, prframe)!=_SUCCESS)
2643                                 {
2644                                         rtw_free_recvframe(prframe, &precvpriv->free_recv_queue);
2645                                 }
2646                         }
2647                         else
2648                         {
2649                                 /* error condition; */
2650                         }
2651
2652                         /* Update local variables. */
2653                         bPktInBuf = _FALSE;
2654
2655                 }
2656                 else
2657                 {
2658                         bPktInBuf = _TRUE;
2659                         break;
2660                 }
2661
2662                 /* DbgPrint("recv_indicatepkts_in_order():while\n"); */
2663
2664         }
2665
2666         /* spin_unlock_ex(&ppending_recvframe_queue->lock); */
2667         /* spin_unlock_irqrestore(&ppending_recvframe_queue->lock); */
2668
2669         return bPktInBuf;
2670 }
2671
2672 int recv_indicatepkt_reorder(_adapter *padapter, union recv_frame *prframe);
2673 int recv_indicatepkt_reorder(_adapter *padapter, union recv_frame *prframe)
2674 {
2675         int retval = _SUCCESS;
2676         struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
2677         struct recv_reorder_ctrl *preorder_ctrl = prframe->u.hdr.preorder_ctrl;
2678         _queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
2679
2680         if(!pattrib->amsdu)
2681         {
2682                 /* s1. */
2683                 wlanhdr_to_ethhdr(prframe);
2684
2685                 if ((pattrib->qos!=1) || (pattrib->eth_type==0x0806) ||
2686                     (pattrib->ack_policy!=0)) {
2687                         if ((padapter->bDriverStopped == _FALSE) &&
2688                             (padapter->bSurpriseRemoved == _FALSE)) {
2689                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@  recv_indicatepkt_reorder -recv_func recv_indicatepkt\n" ));
2690
2691                                 rtw_recv_indicatepkt(padapter, prframe);
2692                                 return _SUCCESS;
2693
2694                         }
2695
2696                         #ifdef DBG_RX_DROP_FRAME
2697                         DBG_8723A("DBG_RX_DROP_FRAME %s pattrib->qos !=1\n", __FUNCTION__);
2698                         #endif
2699
2700                         return _FAIL;
2701
2702                 }
2703
2704                 if (preorder_ctrl->enable == _FALSE)
2705                 {
2706                         /* indicate this recv_frame */
2707                         preorder_ctrl->indicate_seq = pattrib->seq_num;
2708                         #ifdef DBG_RX_SEQ
2709                         DBG_8723A("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __FUNCTION__, __LINE__,
2710                                 preorder_ctrl->indicate_seq, pattrib->seq_num);
2711                         #endif
2712
2713                         rtw_recv_indicatepkt(padapter, prframe);
2714
2715                         preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1)%4096;
2716                         #ifdef DBG_RX_SEQ
2717                         DBG_8723A("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __FUNCTION__, __LINE__,
2718                                 preorder_ctrl->indicate_seq, pattrib->seq_num);
2719                         #endif
2720
2721                         return _SUCCESS;
2722                 }
2723
2724 #ifndef CONFIG_RECV_REORDERING_CTRL
2725                 /* indicate this recv_frame */
2726                 rtw_recv_indicatepkt(padapter, prframe);
2727                 return _SUCCESS;
2728 #endif
2729
2730         }
2731         else if(pattrib->amsdu==1) /* temp filter -> means didn't support A-MSDUs in a A-MPDU */
2732         {
2733                 if (preorder_ctrl->enable == _FALSE)
2734                 {
2735                         preorder_ctrl->indicate_seq = pattrib->seq_num;
2736                         #ifdef DBG_RX_SEQ
2737                         DBG_8723A("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __FUNCTION__, __LINE__,
2738                                 preorder_ctrl->indicate_seq, pattrib->seq_num);
2739                         #endif
2740
2741                         retval = amsdu_to_msdu(padapter, prframe);
2742
2743                         preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1)%4096;
2744                         #ifdef DBG_RX_SEQ
2745                         DBG_8723A("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __FUNCTION__, __LINE__,
2746                                 preorder_ctrl->indicate_seq, pattrib->seq_num);
2747                         #endif
2748
2749                         if(retval != _SUCCESS){
2750                                 #ifdef DBG_RX_DROP_FRAME
2751                                 DBG_8723A("DBG_RX_DROP_FRAME %s amsdu_to_msdu fail\n", __FUNCTION__);
2752                                 #endif
2753                         }
2754
2755                         return retval;
2756                 }
2757         }
2758         else
2759         {
2760
2761         }
2762
2763         spin_lock_bh(&ppending_recvframe_queue->lock);
2764
2765         RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_,
2766                  ("recv_indicatepkt_reorder: indicate=%d seq=%d\n",
2767                   preorder_ctrl->indicate_seq, pattrib->seq_num));
2768
2769         /* s2. check if winstart_b(indicate_seq) needs to been updated */
2770         if(!check_indicate_seq(preorder_ctrl, pattrib->seq_num))
2771         {
2772                 /* pHTInfo->RxReorderDropCounter++; */
2773                 /* ReturnRFDList(Adapter, pRfd); */
2774                 /* RT_TRACE(COMP_RX_REORDER, DBG_TRACE, ("RxReorderIndicatePacket() ==> Packet Drop!!\n")); */
2775                 /* spin_unlock_irqrestore(&ppending_recvframe_queue->lock); */
2776                 /* return _FAIL; */
2777
2778                 #ifdef DBG_RX_DROP_FRAME
2779                 DBG_8723A("DBG_RX_DROP_FRAME %s check_indicate_seq fail\n", __FUNCTION__);
2780                 #endif
2781                 goto _err_exit;
2782         }
2783
2784         /* s3. Insert all packet into Reorder Queue to maintain its ordering. */
2785         if(!enqueue_reorder_recvframe(preorder_ctrl, prframe))
2786         {
2787                 /* DbgPrint("recv_indicatepkt_reorder, enqueue_reorder_recvframe fail!\n"); */
2788                 /* spin_unlock_irqrestore(&ppending_recvframe_queue->lock); */
2789                 /* return _FAIL; */
2790                 #ifdef DBG_RX_DROP_FRAME
2791                 DBG_8723A("DBG_RX_DROP_FRAME %s enqueue_reorder_recvframe fail\n", __FUNCTION__);
2792                 #endif
2793                 goto _err_exit;
2794         }
2795
2796         /* s4. */
2797         /*  Indication process. */
2798         /*  After Packet dropping and Sliding Window shifting as above, we can now just indicate the packets */
2799         /*  with the SeqNum smaller than latest WinStart and buffer other packets. */
2800         /*  */
2801         /*  For Rx Reorder condition: */
2802         /*  1. All packets with SeqNum smaller than WinStart => Indicate */
2803         /*  2. All packets with SeqNum larger than or equal to WinStart => Buffer it. */
2804         /*  */
2805
2806         /* recv_indicatepkts_in_order(padapter, preorder_ctrl, _TRUE); */
2807         if(recv_indicatepkts_in_order(padapter, preorder_ctrl, _FALSE)==_TRUE)
2808         {
2809                 _set_timer(&preorder_ctrl->reordering_ctrl_timer, REORDER_WAIT_TIME);
2810                 spin_unlock_bh(&ppending_recvframe_queue->lock);
2811         }
2812         else
2813         {
2814                 spin_unlock_bh(&ppending_recvframe_queue->lock);
2815                 _cancel_timer_ex(&preorder_ctrl->reordering_ctrl_timer);
2816         }
2817
2818 _success_exit:
2819
2820         return _SUCCESS;
2821
2822 _err_exit:
2823
2824         spin_unlock_bh(&ppending_recvframe_queue->lock);
2825
2826         return _FAIL;
2827 }
2828
2829 void rtw_reordering_ctrl_timeout_handler(void *pcontext)
2830 {
2831         struct recv_reorder_ctrl *preorder_ctrl = (struct recv_reorder_ctrl *)pcontext;
2832         _adapter *padapter = preorder_ctrl->padapter;
2833         _queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
2834
2835         if(padapter->bDriverStopped ||padapter->bSurpriseRemoved)
2836         {
2837                 return;
2838         }
2839
2840         /* DBG_8723A("+rtw_reordering_ctrl_timeout_handler()=>\n"); */
2841
2842         spin_lock_bh(&ppending_recvframe_queue->lock);
2843
2844         if(recv_indicatepkts_in_order(padapter, preorder_ctrl, _TRUE)==_TRUE)
2845         {
2846                 _set_timer(&preorder_ctrl->reordering_ctrl_timer, REORDER_WAIT_TIME);
2847         }
2848
2849         spin_unlock_bh(&ppending_recvframe_queue->lock);
2850 }
2851
2852 int process_recv_indicatepkts(_adapter *padapter, union recv_frame *prframe);
2853 int process_recv_indicatepkts(_adapter *padapter, union recv_frame *prframe)
2854 {
2855         int retval = _SUCCESS;
2856         /* struct recv_priv *precvpriv = &padapter->recvpriv; */
2857         /* struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib; */
2858         struct mlme_priv        *pmlmepriv = &padapter->mlmepriv;
2859 #ifdef CONFIG_TDLS
2860         struct sta_info *psta = prframe->u.hdr.psta;
2861 #endif /* CONFIG_TDLS */
2862
2863 #ifdef CONFIG_80211N_HT
2864
2865         struct ht_priv  *phtpriv = &pmlmepriv->htpriv;
2866
2867 #ifdef CONFIG_TDLS
2868         if( (phtpriv->ht_option==_TRUE) ||
2869                 ((psta->tdls_sta_state & TDLS_LINKED_STATE) &&
2870                  (psta->htpriv.ht_option==_TRUE) &&
2871                  (psta->htpriv.ampdu_enable==_TRUE))) /* B/G/N Mode */
2872 #else
2873         if(phtpriv->ht_option==_TRUE)  /* B/G/N Mode */
2874 #endif /* CONFIG_TDLS */
2875         {
2876                 /* prframe->u.hdr.preorder_ctrl = &precvpriv->recvreorder_ctrl[pattrib->priority]; */
2877
2878                 if(recv_indicatepkt_reorder(padapter, prframe)!=_SUCCESS)/*  including perform A-MPDU Rx Ordering Buffer Control */
2879                 {
2880                         #ifdef DBG_RX_DROP_FRAME
2881                         DBG_8723A("DBG_RX_DROP_FRAME %s recv_indicatepkt_reorder error!\n", __FUNCTION__);
2882                         #endif
2883
2884                         if ((padapter->bDriverStopped == _FALSE) &&
2885                             (padapter->bSurpriseRemoved == _FALSE))
2886                         {
2887                                 retval = _FAIL;
2888                                 return retval;
2889                         }
2890                 }
2891         }
2892         else /* B/G mode */
2893 #endif
2894         {
2895                 retval=wlanhdr_to_ethhdr (prframe);
2896                 if(retval != _SUCCESS)
2897                 {
2898                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("wlanhdr_to_ethhdr: drop pkt \n"));
2899                         #ifdef DBG_RX_DROP_FRAME
2900                         DBG_8723A("DBG_RX_DROP_FRAME %s wlanhdr_to_ethhdr error!\n", __FUNCTION__);
2901                         #endif
2902                         return retval;
2903                 }
2904
2905                 if ((padapter->bDriverStopped ==_FALSE)&&( padapter->bSurpriseRemoved==_FALSE))
2906                 {
2907                         /* indicate this recv_frame */
2908                         RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@ process_recv_indicatepkts- recv_func recv_indicatepkt\n" ));
2909                         rtw_recv_indicatepkt(padapter, prframe);
2910
2911                 }
2912                 else
2913                 {
2914                         RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@ process_recv_indicatepkts- recv_func free_indicatepkt\n" ));
2915
2916                         RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("recv_func:bDriverStopped(%d) OR bSurpriseRemoved(%d)", padapter->bDriverStopped, padapter->bSurpriseRemoved));
2917                         retval = _FAIL;
2918                         return retval;
2919                 }
2920
2921         }
2922
2923         return retval;
2924 }
2925
2926 int recv_func_prehandle(_adapter *padapter, union recv_frame *rframe)
2927 {
2928         int ret = _SUCCESS;
2929         struct rx_pkt_attrib *pattrib = &rframe->u.hdr.attrib;
2930         struct recv_priv *precvpriv = &padapter->recvpriv;
2931         _queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
2932
2933         /* check the frame crtl field and decache */
2934         ret = validate_recv_frame(padapter, rframe);
2935         if (ret != _SUCCESS)
2936         {
2937                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("recv_func: validate_recv_frame fail! drop pkt\n"));
2938                 rtw_free_recvframe(rframe, pfree_recv_queue);/* free this recv_frame */
2939                 goto exit;
2940         }
2941
2942 exit:
2943         return ret;
2944 }
2945
2946 int recv_func_posthandle(_adapter *padapter, union recv_frame *prframe)
2947 {
2948         int ret = _SUCCESS;
2949         union recv_frame *orig_prframe = prframe;
2950         struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
2951         struct recv_priv *precvpriv = &padapter->recvpriv;
2952         _queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
2953
2954 #ifdef CONFIG_TDLS
2955         u8 *psnap_type, *pcategory;
2956         struct sta_info *ptdls_sta = NULL;
2957 #endif /* CONFIG_TDLS */
2958
2959         /*  DATA FRAME */
2960         rtw_led_control(padapter, LED_CTL_RX);
2961
2962         prframe = decryptor(padapter, prframe);
2963         if (prframe == NULL) {
2964                 RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("decryptor: drop pkt\n"));
2965                 #ifdef DBG_RX_DROP_FRAME
2966                 DBG_8723A("DBG_RX_DROP_FRAME %s decryptor: drop pkt\n", __FUNCTION__);
2967                 #endif
2968                 ret = _FAIL;
2969                 goto _recv_data_drop;
2970         }
2971
2972 #ifdef CONFIG_TDLS
2973         /* check TDLS frame */
2974         psnap_type = get_recvframe_data(orig_prframe);
2975         psnap_type+=pattrib->hdrlen + pattrib->iv_len+SNAP_SIZE;
2976         pcategory = psnap_type + ETH_TYPE_LEN + PAYLOAD_TYPE_LEN;
2977
2978         if (!memcmp(psnap_type, SNAP_ETH_TYPE_TDLS, ETH_TYPE_LEN) &&
2979             ((*pcategory==RTW_WLAN_CATEGORY_TDLS) ||
2980              (*pcategory==RTW_WLAN_CATEGORY_P2P))) {
2981                 ret = OnTDLS(padapter, prframe);        /* all of functions will return _FAIL */
2982                 goto _exit_recv_func;
2983         }
2984 #endif /* CONFIG_TDLS */
2985
2986         prframe = recvframe_chk_defrag(padapter, prframe);
2987         if(prframe==NULL)       {
2988                 RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("recvframe_chk_defrag: drop pkt\n"));
2989                 #ifdef DBG_RX_DROP_FRAME
2990                 DBG_8723A("DBG_RX_DROP_FRAME %s recvframe_chk_defrag: drop pkt\n", __FUNCTION__);
2991                 #endif
2992                 goto _recv_data_drop;
2993         }
2994
2995         prframe=portctrl(padapter, prframe);
2996         if (prframe == NULL) {
2997                 RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("portctrl: drop pkt \n"));
2998                 #ifdef DBG_RX_DROP_FRAME
2999                 DBG_8723A("DBG_RX_DROP_FRAME %s portctrl: drop pkt\n", __FUNCTION__);
3000                 #endif
3001                 ret = _FAIL;
3002                 goto _recv_data_drop;
3003         }
3004
3005 #ifdef CONFIG_TDLS
3006         if(padapter->tdlsinfo.setup_state == TDLS_LINKED_STATE)
3007                 ptdls_sta = rtw_get_stainfo(&padapter->stapriv, pattrib->src);
3008         count_rx_stats(padapter, prframe, ptdls_sta);
3009 #else
3010         count_rx_stats(padapter, prframe, NULL);
3011 #endif /* CONFIG_TDLS */
3012
3013 #ifdef CONFIG_80211N_HT
3014         ret = process_recv_indicatepkts(padapter, prframe);
3015         if (ret != _SUCCESS)
3016         {
3017                 RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("recv_func: process_recv_indicatepkts fail! \n"));
3018                 #ifdef DBG_RX_DROP_FRAME
3019                 DBG_8723A("DBG_RX_DROP_FRAME %s process_recv_indicatepkts fail!\n", __FUNCTION__);
3020                 #endif
3021                 rtw_free_recvframe(orig_prframe, pfree_recv_queue);/* free this recv_frame */
3022                 goto _recv_data_drop;
3023         }
3024 #else /*  CONFIG_80211N_HT */
3025         if (!pattrib->amsdu)
3026         {
3027                 ret = wlanhdr_to_ethhdr (prframe);
3028                 if (ret != _SUCCESS)
3029                 {
3030                         RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("wlanhdr_to_ethhdr: drop pkt \n"));
3031                         #ifdef DBG_RX_DROP_FRAME
3032                         DBG_8723A("DBG_RX_DROP_FRAME %s wlanhdr_to_ethhdr: drop pkt\n", __FUNCTION__);
3033                         #endif
3034                         rtw_free_recvframe(orig_prframe, pfree_recv_queue);/* free this recv_frame */
3035                         goto _recv_data_drop;
3036                 }
3037
3038                 if ((padapter->bDriverStopped == _FALSE) && (padapter->bSurpriseRemoved == _FALSE))
3039                 {
3040                         RT_TRACE(_module_rtl871x_recv_c_, _drv_alert_, ("@@@@ recv_func: recv_func rtw_recv_indicatepkt\n" ));
3041                         /* indicate this recv_frame */
3042                         ret = rtw_recv_indicatepkt(padapter, prframe);
3043                         if (ret != _SUCCESS)
3044                         {
3045                                 #ifdef DBG_RX_DROP_FRAME
3046                                 DBG_8723A("DBG_RX_DROP_FRAME %s rtw_recv_indicatepkt fail!\n", __FUNCTION__);
3047                                 #endif
3048                                 goto _recv_data_drop;
3049                         }
3050                 }
3051                 else
3052                 {
3053                         RT_TRACE(_module_rtl871x_recv_c_, _drv_alert_, ("@@@@  recv_func: rtw_free_recvframe\n" ));
3054                         RT_TRACE(_module_rtl871x_recv_c_, _drv_debug_, ("recv_func:bDriverStopped(%d) OR bSurpriseRemoved(%d)", padapter->bDriverStopped, padapter->bSurpriseRemoved));
3055                         #ifdef DBG_RX_DROP_FRAME
3056                         DBG_8723A("DBG_RX_DROP_FRAME %s ecv_func:bDriverStopped(%d) OR bSurpriseRemoved(%d)\n", __FUNCTION__,
3057                                 padapter->bDriverStopped, padapter->bSurpriseRemoved);
3058                         #endif
3059                         ret = _FAIL;
3060                         rtw_free_recvframe(orig_prframe, pfree_recv_queue); /* free this recv_frame */
3061                 }
3062
3063         }
3064         else if(pattrib->amsdu==1)
3065         {
3066
3067                 ret = amsdu_to_msdu(padapter, prframe);
3068                 if(ret != _SUCCESS)
3069                 {
3070                         #ifdef DBG_RX_DROP_FRAME
3071                         DBG_8723A("DBG_RX_DROP_FRAME %s amsdu_to_msdu fail\n", __FUNCTION__);
3072                         #endif
3073                         rtw_free_recvframe(orig_prframe, pfree_recv_queue);
3074                         goto _recv_data_drop;
3075                 }
3076         }
3077         else
3078         {
3079                 #ifdef DBG_RX_DROP_FRAME
3080                 DBG_8723A("DBG_RX_DROP_FRAME %s what is this condition??\n", __FUNCTION__);
3081                 #endif
3082                 goto _recv_data_drop;
3083         }
3084 #endif /*  CONFIG_80211N_HT */
3085
3086 _exit_recv_func:
3087         return ret;
3088
3089 _recv_data_drop:
3090         precvpriv->rx_drop++;
3091         return ret;
3092 }
3093
3094 int recv_func(_adapter *padapter, union recv_frame *rframe);
3095 int recv_func(_adapter *padapter, union recv_frame *rframe)
3096 {
3097         int ret;
3098         struct rx_pkt_attrib *prxattrib = &rframe->u.hdr.attrib;
3099         struct recv_priv *recvpriv = &padapter->recvpriv;
3100         struct security_priv *psecuritypriv=&padapter->securitypriv;
3101         struct mlme_priv *mlmepriv = &padapter->mlmepriv;
3102
3103         /* check if need to handle uc_swdec_pending_queue*/
3104         if (check_fwstate(mlmepriv, WIFI_STATION_STATE) && psecuritypriv->busetkipkey)
3105         {
3106                 union recv_frame *pending_frame;
3107
3108                 while((pending_frame=rtw_alloc_recvframe(&padapter->recvpriv.uc_swdec_pending_queue))) {
3109                         if (recv_func_posthandle(padapter, pending_frame) == _SUCCESS)
3110                                 DBG_8723A("%s: dequeue uc_swdec_pending_queue\n", __func__);
3111                 }
3112         }
3113
3114         ret = recv_func_prehandle(padapter, rframe);
3115
3116         if(ret == _SUCCESS) {
3117
3118                 /* check if need to enqueue into uc_swdec_pending_queue*/
3119                 if (check_fwstate(mlmepriv, WIFI_STATION_STATE) &&
3120                         !is_multicast_ether_addr(prxattrib->ra) && prxattrib->encrypt>0 &&
3121                         (prxattrib->bdecrypted == 0 ||psecuritypriv->sw_decrypt == _TRUE) &&
3122                         !is_wep_enc(psecuritypriv->dot11PrivacyAlgrthm) &&
3123                         !psecuritypriv->busetkipkey) {
3124                         rtw_enqueue_recvframe(rframe, &padapter->recvpriv.uc_swdec_pending_queue);
3125                         DBG_8723A("%s: no key, enqueue uc_swdec_pending_queue\n", __func__);
3126                         goto exit;
3127                 }
3128
3129                 ret = recv_func_posthandle(padapter, rframe);
3130         }
3131
3132 exit:
3133         return ret;
3134 }
3135
3136 s32 rtw_recv_entry(union recv_frame *precvframe)
3137 {
3138         _adapter *padapter;
3139         struct recv_priv *precvpriv;
3140         s32 ret=_SUCCESS;
3141
3142 _func_enter_;
3143
3144 /*      RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("+rtw_recv_entry\n")); */
3145
3146         padapter = precvframe->u.hdr.adapter;
3147
3148         precvpriv = &padapter->recvpriv;
3149
3150         if ((ret = recv_func(padapter, precvframe)) == _FAIL)
3151         {
3152                 RT_TRACE(_module_rtl871x_recv_c_,_drv_info_,("rtw_recv_entry: recv_func return fail!!!\n"));
3153                 goto _recv_entry_drop;
3154         }
3155
3156         precvpriv->rx_pkts++;
3157
3158 _func_exit_;
3159
3160         return ret;
3161
3162 _recv_entry_drop:
3163
3164         /* RT_TRACE(_module_rtl871x_recv_c_,_drv_err_,("_recv_entry_drop\n")); */
3165
3166 _func_exit_;
3167
3168         return ret;
3169 }
3170
3171 #ifdef CONFIG_NEW_SIGNAL_STAT_PROCESS
3172 void rtw_signal_stat_timer_hdl(RTW_TIMER_HDL_ARGS){
3173         _adapter *adapter = (_adapter *)FunctionContext;
3174         struct recv_priv *recvpriv = &adapter->recvpriv;
3175
3176         u32 tmp_s, tmp_q;
3177         u8 avg_signal_strength = 0;
3178         u8 avg_signal_qual = 0;
3179         u32 num_signal_strength = 0;
3180         u32 num_signal_qual = 0;
3181         u8 _alpha = 3; /*  this value is based on converging_constant = 5000 and sampling_interval = 1000 */
3182
3183         if(adapter->recvpriv.is_signal_dbg) {
3184                 /* update the user specific value, signal_strength_dbg, to signal_strength, rssi */
3185                 adapter->recvpriv.signal_strength= adapter->recvpriv.signal_strength_dbg;
3186                 adapter->recvpriv.rssi=(s8)translate_percentage_to_dbm((u8)adapter->recvpriv.signal_strength_dbg);
3187         } else {
3188
3189                 if(recvpriv->signal_strength_data.update_req == 0) {/*  update_req is clear, means we got rx */
3190                         avg_signal_strength = recvpriv->signal_strength_data.avg_val;
3191                         num_signal_strength = recvpriv->signal_strength_data.total_num;
3192                         /*  after avg_vals are accquired, we can re-stat the signal values */
3193                         recvpriv->signal_strength_data.update_req = 1;
3194                 }
3195
3196                 if(recvpriv->signal_qual_data.update_req == 0) {/*  update_req is clear, means we got rx */
3197                         avg_signal_qual = recvpriv->signal_qual_data.avg_val;
3198                         num_signal_qual = recvpriv->signal_qual_data.total_num;
3199                         /*  after avg_vals are accquired, we can re-stat the signal values */
3200                         recvpriv->signal_qual_data.update_req = 1;
3201                 }
3202
3203                 /* update value of signal_strength, rssi, signal_qual */
3204                 if(check_fwstate(&adapter->mlmepriv, _FW_UNDER_SURVEY) == _FALSE) {
3205                         tmp_s = (avg_signal_strength+(_alpha-1)*recvpriv->signal_strength);
3206                         if(tmp_s %_alpha)
3207                                 tmp_s = tmp_s/_alpha + 1;
3208                         else
3209                                 tmp_s = tmp_s/_alpha;
3210                         if(tmp_s>100)
3211                                 tmp_s = 100;
3212
3213                         tmp_q = (avg_signal_qual+(_alpha-1)*recvpriv->signal_qual);
3214                         if(tmp_q %_alpha)
3215                                 tmp_q = tmp_q/_alpha + 1;
3216                         else
3217                                 tmp_q = tmp_q/_alpha;
3218                         if(tmp_q>100)
3219                                 tmp_q = 100;
3220
3221                         recvpriv->signal_strength = tmp_s;
3222                         recvpriv->rssi = (s8)translate_percentage_to_dbm(tmp_s);
3223                         recvpriv->signal_qual = tmp_q;
3224
3225                         #if defined(DBG_RX_SIGNAL_DISPLAY_PROCESSING) && 1
3226                         DBG_8723A("%s signal_strength:%3u, rssi:%3d, signal_qual:%3u"
3227                                 ", num_signal_strength:%u, num_signal_qual:%u"
3228                                 "\n"
3229                                 , __FUNCTION__
3230                                 , recvpriv->signal_strength
3231                                 , recvpriv->rssi
3232                                 , recvpriv->signal_qual
3233                                 , num_signal_strength, num_signal_qual
3234                         );
3235                         #endif
3236                 }
3237         }
3238         rtw_set_signal_stat_timer(recvpriv);
3239 }
3240 #endif /* CONFIG_NEW_SIGNAL_STAT_PROCESS */