]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/rtl8188eu/core/rtw_recv.c
regulator: Fix regulator_get_{optional,exclusive}() documentation
[karo-tx-linux.git] / drivers / staging / rtl8188eu / 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
22 #include <osdep_service.h>
23 #include <drv_types.h>
24 #include <recv_osdep.h>
25 #include <mlme_osdep.h>
26 #include <usb_ops.h>
27 #include <wifi.h>
28 #include <linux/vmalloc.h>
29
30 #define ETHERNET_HEADER_SIZE    14      /*  Ethernet Header Length */
31 #define LLC_HEADER_SIZE                 6       /*  LLC Header Length */
32
33 static u8 SNAP_ETH_TYPE_IPX[2] = {0x81, 0x37};
34 static u8 SNAP_ETH_TYPE_APPLETALK_AARP[2] = {0x80, 0xf3};
35
36 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
37 static u8 rtw_bridge_tunnel_header[] = {
38        0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8
39 };
40
41 static u8 rtw_rfc1042_header[] = {
42        0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00
43 };
44
45 void rtw_signal_stat_timer_hdl(RTW_TIMER_HDL_ARGS);
46
47 void _rtw_init_sta_recv_priv(struct sta_recv_priv *psta_recvpriv)
48 {
49
50         _rtw_memset((u8 *)psta_recvpriv, 0, sizeof (struct sta_recv_priv));
51
52         spin_lock_init(&psta_recvpriv->lock);
53
54         _rtw_init_queue(&psta_recvpriv->defrag_q);
55
56 }
57
58 int _rtw_init_recv_priv(struct recv_priv *precvpriv, struct adapter *padapter)
59 {
60         int i;
61
62         struct recv_frame *precvframe;
63
64         int     res = _SUCCESS;
65
66         spin_lock_init(&precvpriv->lock);
67
68         _rtw_init_queue(&precvpriv->free_recv_queue);
69         _rtw_init_queue(&precvpriv->recv_pending_queue);
70         _rtw_init_queue(&precvpriv->uc_swdec_pending_queue);
71
72         precvpriv->adapter = padapter;
73
74         precvpriv->free_recvframe_cnt = NR_RECVFRAME;
75
76         rtw_os_recv_resource_init(precvpriv, padapter);
77
78         precvpriv->pallocated_frame_buf = vzalloc(NR_RECVFRAME * sizeof(struct recv_frame) + RXFRAME_ALIGN_SZ);
79
80         if (precvpriv->pallocated_frame_buf == NULL) {
81                 res = _FAIL;
82                 goto exit;
83         }
84
85         precvpriv->precv_frame_buf = (u8 *)N_BYTE_ALIGMENT((size_t)(precvpriv->pallocated_frame_buf), RXFRAME_ALIGN_SZ);
86
87         precvframe = (struct recv_frame *)precvpriv->precv_frame_buf;
88
89         for (i = 0; i < NR_RECVFRAME; i++) {
90                 _rtw_init_listhead(&(precvframe->list));
91
92                 rtw_list_insert_tail(&(precvframe->list),
93                                      &(precvpriv->free_recv_queue.queue));
94
95                 res = rtw_os_recv_resource_alloc(padapter, precvframe);
96
97                 precvframe->len = 0;
98
99                 precvframe->adapter = padapter;
100                 precvframe++;
101         }
102         precvpriv->rx_pending_cnt = 1;
103
104         sema_init(&precvpriv->allrxreturnevt, 0);
105
106         res = rtw_hal_init_recv_priv(padapter);
107
108         _init_timer(&precvpriv->signal_stat_timer, padapter->pnetdev, RTW_TIMER_HDL_NAME(signal_stat), padapter);
109
110         precvpriv->signal_stat_sampling_interval = 1000; /* ms */
111
112         rtw_set_signal_stat_timer(precvpriv);
113 exit:
114
115
116         return res;
117 }
118
119 void _rtw_free_recv_priv (struct recv_priv *precvpriv)
120 {
121         struct adapter  *padapter = precvpriv->adapter;
122
123
124         rtw_free_uc_swdec_pending_queue(padapter);
125
126         rtw_os_recv_resource_free(precvpriv);
127
128         if (precvpriv->pallocated_frame_buf) {
129                 vfree(precvpriv->pallocated_frame_buf);
130         }
131
132         rtw_hal_free_recv_priv(padapter);
133
134 }
135
136 struct recv_frame *_rtw_alloc_recvframe (struct __queue *pfree_recv_queue)
137 {
138         struct recv_frame *hdr;
139         struct list_head *plist, *phead;
140         struct adapter *padapter;
141         struct recv_priv *precvpriv;
142
143         if (_rtw_queue_empty(pfree_recv_queue)) {
144                 hdr = NULL;
145         } else {
146                 phead = get_list_head(pfree_recv_queue);
147
148                 plist = phead->next;
149
150                 hdr = container_of(plist, struct recv_frame, list);
151
152                 rtw_list_delete(&hdr->list);
153                 padapter = hdr->adapter;
154                 if (padapter != NULL) {
155                         precvpriv = &padapter->recvpriv;
156                         if (pfree_recv_queue == &precvpriv->free_recv_queue)
157                                 precvpriv->free_recvframe_cnt--;
158                 }
159         }
160
161
162         return (struct recv_frame *)hdr;
163 }
164
165 struct recv_frame *rtw_alloc_recvframe(struct __queue *pfree_recv_queue)
166 {
167         struct recv_frame  *precvframe;
168
169         spin_lock_bh(&pfree_recv_queue->lock);
170
171         precvframe = _rtw_alloc_recvframe(pfree_recv_queue);
172
173         spin_unlock_bh(&pfree_recv_queue->lock);
174
175         return precvframe;
176 }
177
178 void rtw_init_recvframe(struct recv_frame *precvframe, struct recv_priv *precvpriv)
179 {
180         /* Perry: This can be removed */
181         _rtw_init_listhead(&precvframe->list);
182
183         precvframe->len = 0;
184 }
185
186 int rtw_free_recvframe(struct recv_frame *precvframe,
187                        struct __queue *pfree_recv_queue)
188 {
189         struct adapter *padapter;
190         struct recv_priv *precvpriv;
191
192         if (!precvframe)
193                 return _FAIL;
194         padapter = precvframe->adapter;
195         precvpriv = &padapter->recvpriv;
196         if (precvframe->pkt) {
197                 dev_kfree_skb_any(precvframe->pkt);/* free skb by driver */
198                 precvframe->pkt = NULL;
199         }
200
201         spin_lock_bh(&pfree_recv_queue->lock);
202
203         rtw_list_delete(&(precvframe->list));
204
205         precvframe->len = 0;
206
207         rtw_list_insert_tail(&(precvframe->list), get_list_head(pfree_recv_queue));
208
209         if (padapter != NULL) {
210                 if (pfree_recv_queue == &precvpriv->free_recv_queue)
211                                 precvpriv->free_recvframe_cnt++;
212         }
213
214       spin_unlock_bh(&pfree_recv_queue->lock);
215
216
217         return _SUCCESS;
218 }
219
220 int _rtw_enqueue_recvframe(struct recv_frame *precvframe, struct __queue *queue)
221 {
222         struct adapter *padapter = precvframe->adapter;
223         struct recv_priv *precvpriv = &padapter->recvpriv;
224
225
226         rtw_list_delete(&(precvframe->list));
227         rtw_list_insert_tail(&(precvframe->list), get_list_head(queue));
228
229         if (padapter != NULL) {
230                 if (queue == &precvpriv->free_recv_queue)
231                         precvpriv->free_recvframe_cnt++;
232         }
233
234
235         return _SUCCESS;
236 }
237
238 int rtw_enqueue_recvframe(struct recv_frame *precvframe, struct __queue *queue)
239 {
240         int ret;
241
242         spin_lock_bh(&queue->lock);
243         ret = _rtw_enqueue_recvframe(precvframe, queue);
244         spin_unlock_bh(&queue->lock);
245
246         return ret;
247 }
248
249 /*
250 caller : defrag ; recvframe_chk_defrag in recv_thread  (passive)
251 pframequeue: defrag_queue : will be accessed in recv_thread  (passive)
252
253 using spinlock to protect
254
255 */
256
257 void rtw_free_recvframe_queue(struct __queue *pframequeue,  struct __queue *pfree_recv_queue)
258 {
259         struct recv_frame *hdr;
260         struct list_head *plist, *phead;
261
262         spin_lock(&pframequeue->lock);
263
264         phead = get_list_head(pframequeue);
265         plist = phead->next;
266
267         while (rtw_end_of_queue_search(phead, plist) == false) {
268                 hdr = container_of(plist, struct recv_frame, list);
269
270                 plist = plist->next;
271
272                 rtw_free_recvframe((struct recv_frame *)hdr, pfree_recv_queue);
273         }
274
275         spin_unlock(&pframequeue->lock);
276
277 }
278
279 u32 rtw_free_uc_swdec_pending_queue(struct adapter *adapter)
280 {
281         u32 cnt = 0;
282         struct recv_frame *pending_frame;
283         while ((pending_frame = rtw_alloc_recvframe(&adapter->recvpriv.uc_swdec_pending_queue))) {
284                 rtw_free_recvframe(pending_frame, &adapter->recvpriv.free_recv_queue);
285                 DBG_88E("%s: dequeue uc_swdec_pending_queue\n", __func__);
286                 cnt++;
287         }
288
289         return cnt;
290 }
291
292 int rtw_enqueue_recvbuf_to_head(struct recv_buf *precvbuf, struct __queue *queue)
293 {
294         spin_lock_bh(&queue->lock);
295
296         rtw_list_delete(&precvbuf->list);
297         rtw_list_insert_head(&precvbuf->list, get_list_head(queue));
298
299         spin_unlock_bh(&queue->lock);
300
301         return _SUCCESS;
302 }
303
304 int rtw_enqueue_recvbuf(struct recv_buf *precvbuf, struct __queue *queue)
305 {
306         unsigned long irqL;
307         spin_lock_irqsave(&queue->lock, irqL);
308
309         rtw_list_delete(&precvbuf->list);
310
311         rtw_list_insert_tail(&precvbuf->list, get_list_head(queue));
312         spin_unlock_irqrestore(&queue->lock, irqL);
313         return _SUCCESS;
314 }
315
316 struct recv_buf *rtw_dequeue_recvbuf (struct __queue *queue)
317 {
318         unsigned long irqL;
319         struct recv_buf *precvbuf;
320         struct list_head *plist, *phead;
321
322         spin_lock_irqsave(&queue->lock, irqL);
323
324         if (_rtw_queue_empty(queue)) {
325                 precvbuf = NULL;
326         } else {
327                 phead = get_list_head(queue);
328
329                 plist = phead->next;
330
331                 precvbuf = container_of(plist, struct recv_buf, list);
332
333                 rtw_list_delete(&precvbuf->list);
334         }
335
336         spin_unlock_irqrestore(&queue->lock, irqL);
337
338         return precvbuf;
339 }
340
341 static int recvframe_chkmic(struct adapter *adapter,
342                             struct recv_frame *precvframe)
343 {
344         int     i, res = _SUCCESS;
345         u32     datalen;
346         u8      miccode[8];
347         u8      bmic_err = false, brpt_micerror = true;
348         u8      *pframe, *payload, *pframemic;
349         u8      *mickey;
350         struct  sta_info                *stainfo;
351         struct  rx_pkt_attrib   *prxattrib = &precvframe->attrib;
352         struct  security_priv   *psecuritypriv = &adapter->securitypriv;
353
354         struct mlme_ext_priv    *pmlmeext = &adapter->mlmeextpriv;
355         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
356
357         stainfo = rtw_get_stainfo(&adapter->stapriv, &prxattrib->ta[0]);
358
359         if (prxattrib->encrypt == _TKIP_) {
360                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n recvframe_chkmic:prxattrib->encrypt==_TKIP_\n"));
361                 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",
362                          prxattrib->ra[0], prxattrib->ra[1], prxattrib->ra[2], prxattrib->ra[3], prxattrib->ra[4], prxattrib->ra[5]));
363
364                 /* calculate mic code */
365                 if (stainfo != NULL) {
366                         if (IS_MCAST(prxattrib->ra)) {
367                                 if (!psecuritypriv) {
368                                         res = _FAIL;
369                                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n recvframe_chkmic:didn't install group key!!!!!!!!!!\n"));
370                                         DBG_88E("\n recvframe_chkmic:didn't install group key!!!!!!!!!!\n");
371                                         goto exit;
372                                 }
373                                 mickey = &psecuritypriv->dot118021XGrprxmickey[prxattrib->key_index].skey[0];
374
375                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n recvframe_chkmic: bcmc key\n"));
376                         } else {
377                                 mickey = &stainfo->dot11tkiprxmickey.skey[0];
378                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n recvframe_chkmic: unicast key\n"));
379                         }
380
381                         /* icv_len included the mic code */
382                         datalen = precvframe->len-prxattrib->hdrlen -
383                                   prxattrib->iv_len-prxattrib->icv_len-8;
384                         pframe = precvframe->rx_data;
385                         payload = pframe+prxattrib->hdrlen+prxattrib->iv_len;
386
387                         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n prxattrib->iv_len=%d prxattrib->icv_len=%d\n", prxattrib->iv_len, prxattrib->icv_len));
388                         rtw_seccalctkipmic(mickey, pframe, payload, datalen, &miccode[0],
389                                            (unsigned char)prxattrib->priority); /* care the length of the data */
390
391                         pframemic = payload+datalen;
392
393                         bmic_err = false;
394
395                         for (i = 0; i < 8; i++) {
396                                 if (miccode[i] != *(pframemic+i)) {
397                                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_,
398                                                  ("recvframe_chkmic:miccode[%d](%02x)!=*(pframemic+%d)(%02x) ",
399                                                  i, miccode[i], i, *(pframemic+i)));
400                                         bmic_err = true;
401                                 }
402                         }
403
404                         if (bmic_err) {
405                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_,
406                                          ("\n *(pframemic-8)-*(pframemic-1)=0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
407                                          *(pframemic-8), *(pframemic-7), *(pframemic-6),
408                                          *(pframemic-5), *(pframemic-4), *(pframemic-3),
409                                          *(pframemic-2), *(pframemic-1)));
410                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_,
411                                          ("\n *(pframemic-16)-*(pframemic-9)=0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
412                                          *(pframemic-16), *(pframemic-15), *(pframemic-14),
413                                          *(pframemic-13), *(pframemic-12), *(pframemic-11),
414                                          *(pframemic-10), *(pframemic-9)));
415                                 {
416                                         uint i;
417                                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_,
418                                                  ("\n ======demp packet (len=%d)======\n",
419                                                  precvframe->len));
420                                         for (i = 0; i < precvframe->len; i += 8) {
421                                                 RT_TRACE(_module_rtl871x_recv_c_,
422                                                          _drv_err_,
423                                                          ("0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x",
424                                                          *(precvframe->rx_data+i),
425                                                          *(precvframe->rx_data+i+1),
426                                                          *(precvframe->rx_data+i+2),
427                                                          *(precvframe->rx_data+i+3),
428                                                          *(precvframe->rx_data+i+4),
429                                                          *(precvframe->rx_data+i+5),
430                                                          *(precvframe->rx_data+i+6),
431                                                          *(precvframe->rx_data+i+7)));
432                                         }
433                                         RT_TRACE(_module_rtl871x_recv_c_,
434                                                  _drv_err_,
435                                                  ("\n ====== demp packet end [len=%d]======\n",
436                                                  precvframe->len));
437                                         RT_TRACE(_module_rtl871x_recv_c_,
438                                                  _drv_err_,
439                                                  ("\n hrdlen=%d,\n",
440                                                  prxattrib->hdrlen));
441                                 }
442
443                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_,
444                                          ("ra=0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x psecuritypriv->binstallGrpkey=%d ",
445                                          prxattrib->ra[0], prxattrib->ra[1], prxattrib->ra[2],
446                                          prxattrib->ra[3], prxattrib->ra[4], prxattrib->ra[5], psecuritypriv->binstallGrpkey));
447
448                                 /*  double check key_index for some timing issue , */
449                                 /*  cannot compare with psecuritypriv->dot118021XGrpKeyid also cause timing issue */
450                                 if ((IS_MCAST(prxattrib->ra) == true)  && (prxattrib->key_index != pmlmeinfo->key_index))
451                                         brpt_micerror = false;
452
453                                 if ((prxattrib->bdecrypted) && (brpt_micerror)) {
454                                         rtw_handle_tkip_mic_err(adapter, (u8)IS_MCAST(prxattrib->ra));
455                                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" mic error :prxattrib->bdecrypted=%d ", prxattrib->bdecrypted));
456                                         DBG_88E(" mic error :prxattrib->bdecrypted=%d\n", prxattrib->bdecrypted);
457                                 } else {
458                                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" mic error :prxattrib->bdecrypted=%d ", prxattrib->bdecrypted));
459                                         DBG_88E(" mic error :prxattrib->bdecrypted=%d\n", prxattrib->bdecrypted);
460                                 }
461                                 res = _FAIL;
462                         } else {
463                                 /* mic checked ok */
464                                 if ((!psecuritypriv->bcheck_grpkey) && (IS_MCAST(prxattrib->ra))) {
465                                         psecuritypriv->bcheck_grpkey = true;
466                                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("psecuritypriv->bcheck_grpkey = true"));
467                                 }
468                         }
469                 } else {
470                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chkmic: rtw_get_stainfo==NULL!!!\n"));
471                 }
472
473                 recvframe_pull_tail(precvframe, 8);
474         }
475
476 exit:
477
478
479         return res;
480 }
481
482 /* decrypt and set the ivlen, icvlen of the recv_frame */
483 static struct recv_frame *decryptor(struct adapter *padapter,
484                                     struct recv_frame *precv_frame)
485 {
486         struct rx_pkt_attrib *prxattrib = &precv_frame->attrib;
487         struct security_priv *psecuritypriv = &padapter->securitypriv;
488         struct recv_frame *return_packet = precv_frame;
489         u32      res = _SUCCESS;
490
491         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("prxstat->decrypted=%x prxattrib->encrypt=0x%03x\n", prxattrib->bdecrypted, prxattrib->encrypt));
492
493         if (prxattrib->encrypt > 0) {
494                 u8 *iv = precv_frame->rx_data+prxattrib->hdrlen;
495                 prxattrib->key_index = (((iv[3])>>6)&0x3);
496
497                 if (prxattrib->key_index > WEP_KEYS) {
498                         DBG_88E("prxattrib->key_index(%d)>WEP_KEYS\n", prxattrib->key_index);
499
500                         switch (prxattrib->encrypt) {
501                         case _WEP40_:
502                         case _WEP104_:
503                                 prxattrib->key_index = psecuritypriv->dot11PrivacyKeyIndex;
504                                 break;
505                         case _TKIP_:
506                         case _AES_:
507                         default:
508                                 prxattrib->key_index = psecuritypriv->dot118021XGrpKeyid;
509                                 break;
510                         }
511                 }
512         }
513
514         if ((prxattrib->encrypt > 0) && ((prxattrib->bdecrypted == 0) || (psecuritypriv->sw_decrypt))) {
515                 psecuritypriv->hw_decrypted = false;
516
517                 switch (prxattrib->encrypt) {
518                 case _WEP40_:
519                 case _WEP104_:
520                         rtw_wep_decrypt(padapter, (u8 *)precv_frame);
521                         break;
522                 case _TKIP_:
523                         res = rtw_tkip_decrypt(padapter, (u8 *)precv_frame);
524                         break;
525                 case _AES_:
526                         res = rtw_aes_decrypt(padapter, (u8 *)precv_frame);
527                         break;
528                 default:
529                         break;
530                 }
531         } else if (prxattrib->bdecrypted == 1 && prxattrib->encrypt > 0 &&
532                    (psecuritypriv->busetkipkey == 1 || prxattrib->encrypt != _TKIP_))
533                         psecuritypriv->hw_decrypted = true;
534
535         if (res == _FAIL) {
536                 rtw_free_recvframe(return_packet, &padapter->recvpriv.free_recv_queue);
537                 return_packet = NULL;
538         }
539
540
541         return return_packet;
542 }
543
544 /* set the security information in the recv_frame */
545 static struct recv_frame *portctrl(struct adapter *adapter,
546                                    struct recv_frame *precv_frame)
547 {
548         u8   *psta_addr = NULL, *ptr;
549         uint  auth_alg;
550         struct recv_frame *pfhdr;
551         struct sta_info *psta;
552         struct sta_priv *pstapriv;
553         struct recv_frame *prtnframe;
554         u16     ether_type = 0;
555         u16  eapol_type = 0x888e;/* for Funia BD's WPA issue */
556         struct rx_pkt_attrib *pattrib;
557         __be16 be_tmp;
558
559
560         pstapriv = &adapter->stapriv;
561         psta = rtw_get_stainfo(pstapriv, psta_addr);
562
563         auth_alg = adapter->securitypriv.dot11AuthAlgrthm;
564
565         ptr = precv_frame->rx_data;
566         pfhdr = precv_frame;
567         pattrib = &pfhdr->attrib;
568         psta_addr = pattrib->ta;
569
570         prtnframe = NULL;
571
572         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:adapter->securitypriv.dot11AuthAlgrthm=%d\n", adapter->securitypriv.dot11AuthAlgrthm));
573
574         if (auth_alg == 2) {
575                 if ((psta != NULL) && (psta->ieee8021x_blocked)) {
576                         /* blocked */
577                         /* only accept EAPOL frame */
578                         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:psta->ieee8021x_blocked==1\n"));
579
580                         prtnframe = precv_frame;
581
582                         /* get ether_type */
583                         ptr = ptr+pfhdr->attrib.hdrlen+pfhdr->attrib.iv_len+LLC_HEADER_SIZE;
584                         memcpy(&be_tmp, ptr, 2);
585                         ether_type = ntohs(be_tmp);
586
587                         if (ether_type == eapol_type) {
588                                 prtnframe = precv_frame;
589                         } else {
590                                 /* free this frame */
591                                 rtw_free_recvframe(precv_frame, &adapter->recvpriv.free_recv_queue);
592                                 prtnframe = NULL;
593                         }
594                 } else {
595                         /* allowed */
596                         /* check decryption status, and decrypt the frame if needed */
597                         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:psta->ieee8021x_blocked==0\n"));
598                         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_,
599                                  ("portctrl:precv_frame->hdr.attrib.privacy=%x\n",
600                                  precv_frame->attrib.privacy));
601
602                         if (pattrib->bdecrypted == 0)
603                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("portctrl:prxstat->decrypted=%x\n", pattrib->bdecrypted));
604
605                         prtnframe = precv_frame;
606                         /* check is the EAPOL frame or not (Rekey) */
607                         if (ether_type == eapol_type) {
608                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("########portctrl:ether_type==0x888e\n"));
609                                 /* check Rekey */
610
611                                 prtnframe = precv_frame;
612                         } else {
613                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:ether_type=0x%04x\n", ether_type));
614                         }
615                 }
616         } else {
617                 prtnframe = precv_frame;
618         }
619
620
621                 return prtnframe;
622 }
623
624 static int recv_decache(struct recv_frame *precv_frame, u8 bretry,
625                         struct stainfo_rxcache *prxcache)
626 {
627         int tid = precv_frame->attrib.priority;
628
629         u16 seq_ctrl = ((precv_frame->attrib.seq_num&0xffff) << 4) |
630                 (precv_frame->attrib.frag_num & 0xf);
631
632
633         if (tid > 15) {
634                 RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("recv_decache, (tid>15)! seq_ctrl=0x%x, tid=0x%x\n", seq_ctrl, tid));
635
636                 return _FAIL;
637         }
638
639         if (1) {/* if (bretry) */
640                 if (seq_ctrl == prxcache->tid_rxseq[tid]) {
641                         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]));
642
643                         return _FAIL;
644                 }
645         }
646
647         prxcache->tid_rxseq[tid] = seq_ctrl;
648
649
650         return _SUCCESS;
651 }
652
653 void process_pwrbit_data(struct adapter *padapter,
654                          struct recv_frame *precv_frame)
655 {
656 #ifdef CONFIG_88EU_AP_MODE
657         unsigned char pwrbit;
658         u8 *ptr = precv_frame->rx_data;
659         struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
660         struct sta_priv *pstapriv = &padapter->stapriv;
661         struct sta_info *psta = NULL;
662
663         psta = rtw_get_stainfo(pstapriv, pattrib->src);
664
665         pwrbit = GetPwrMgt(ptr);
666
667         if (psta) {
668                 if (pwrbit) {
669                         if (!(psta->state & WIFI_SLEEP_STATE))
670                                 stop_sta_xmit(padapter, psta);
671                 } else {
672                         if (psta->state & WIFI_SLEEP_STATE)
673                                 wakeup_sta_to_xmit(padapter, psta);
674                 }
675         }
676
677 #endif
678 }
679
680 static void process_wmmps_data(struct adapter *padapter,
681                                struct recv_frame *precv_frame)
682 {
683 #ifdef CONFIG_88EU_AP_MODE
684         struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
685         struct sta_priv *pstapriv = &padapter->stapriv;
686         struct sta_info *psta = NULL;
687
688         psta = rtw_get_stainfo(pstapriv, pattrib->src);
689
690         if (!psta)
691                 return;
692
693         if (!psta->qos_option)
694                 return;
695
696         if (!(psta->qos_info&0xf))
697                 return;
698
699         if (psta->state&WIFI_SLEEP_STATE) {
700                 u8 wmmps_ac = 0;
701
702                 switch (pattrib->priority) {
703                 case 1:
704                 case 2:
705                         wmmps_ac = psta->uapsd_bk&BIT(1);
706                         break;
707                 case 4:
708                 case 5:
709                         wmmps_ac = psta->uapsd_vi&BIT(1);
710                         break;
711                 case 6:
712                 case 7:
713                         wmmps_ac = psta->uapsd_vo&BIT(1);
714                         break;
715                 case 0:
716                 case 3:
717                 default:
718                         wmmps_ac = psta->uapsd_be&BIT(1);
719                         break;
720                 }
721
722                 if (wmmps_ac) {
723                         if (psta->sleepq_ac_len > 0) {
724                                 /* process received triggered frame */
725                                 xmit_delivery_enabled_frames(padapter, psta);
726                         } else {
727                                 /* issue one qos null frame with More data bit = 0 and the EOSP bit set (= 1) */
728                                 issue_qos_nulldata(padapter, psta->hwaddr, (u16)pattrib->priority, 0, 0);
729                         }
730                 }
731         }
732
733 #endif
734 }
735
736 static void count_rx_stats(struct adapter *padapter,
737                            struct recv_frame *prframe,
738                            struct sta_info *sta)
739 {
740         int     sz;
741         struct sta_info         *psta = NULL;
742         struct stainfo_stats    *pstats = NULL;
743         struct rx_pkt_attrib    *pattrib = &prframe->attrib;
744         struct recv_priv        *precvpriv = &padapter->recvpriv;
745
746         sz = prframe->len;
747         precvpriv->rx_bytes += sz;
748
749         padapter->mlmepriv.LinkDetectInfo.NumRxOkInPeriod++;
750
751         if ((!MacAddr_isBcst(pattrib->dst)) && (!IS_MCAST(pattrib->dst)))
752                 padapter->mlmepriv.LinkDetectInfo.NumRxUnicastOkInPeriod++;
753
754         if (sta)
755                 psta = sta;
756         else
757                 psta = prframe->psta;
758
759         if (psta) {
760                 pstats = &psta->sta_stats;
761
762                 pstats->rx_data_pkts++;
763                 pstats->rx_bytes += sz;
764         }
765 }
766
767 int sta2sta_data_frame(
768         struct adapter *adapter,
769         struct recv_frame *precv_frame,
770         struct sta_info **psta
771 );
772
773 int sta2sta_data_frame(struct adapter *adapter, struct recv_frame *precv_frame,
774                        struct sta_info **psta)
775 {
776         u8 *ptr = precv_frame->rx_data;
777         int ret = _SUCCESS;
778         struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
779         struct  sta_priv *pstapriv = &adapter->stapriv;
780         struct  mlme_priv *pmlmepriv = &adapter->mlmepriv;
781         u8 *mybssid  = get_bssid(pmlmepriv);
782         u8 *myhwaddr = myid(&adapter->eeprompriv);
783         u8 *sta_addr = NULL;
784         int bmcast = IS_MCAST(pattrib->dst);
785
786
787         if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true) ||
788             (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true)) {
789                 /*  filter packets that SA is myself or multicast or broadcast */
790                 if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN)) {
791                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" SA==myself\n"));
792                         ret = _FAIL;
793                         goto exit;
794                 }
795
796                 if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast)) {
797                         ret = _FAIL;
798                         goto exit;
799                 }
800
801                 if (!memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
802                     !memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
803                     memcmp(pattrib->bssid, mybssid, ETH_ALEN)) {
804                         ret = _FAIL;
805                         goto exit;
806                 }
807
808                 sta_addr = pattrib->src;
809         } else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
810                 /*  For Station mode, sa and bssid should always be BSSID, and DA is my mac-address */
811                 if (memcmp(pattrib->bssid, pattrib->src, ETH_ALEN)) {
812                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("bssid!=TA under STATION_MODE; drop pkt\n"));
813                         ret = _FAIL;
814                         goto exit;
815                 }
816                 sta_addr = pattrib->bssid;
817         } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
818                 if (bmcast) {
819                         /*  For AP mode, if DA == MCAST, then BSSID should be also MCAST */
820                         if (!IS_MCAST(pattrib->bssid)) {
821                                         ret = _FAIL;
822                                         goto exit;
823                         }
824                 } else { /*  not mc-frame */
825                         /*  For AP mode, if DA is non-MCAST, then it must be BSSID, and bssid == BSSID */
826                         if (memcmp(pattrib->bssid, pattrib->dst, ETH_ALEN)) {
827                                 ret = _FAIL;
828                                 goto exit;
829                         }
830
831                         sta_addr = pattrib->src;
832                 }
833         } else if (check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
834                 memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
835                 memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
836                 memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
837                 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
838                 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
839
840                 sta_addr = mybssid;
841         } else {
842                 ret  = _FAIL;
843         }
844
845         if (bmcast)
846                 *psta = rtw_get_bcmc_stainfo(adapter);
847         else
848                 *psta = rtw_get_stainfo(pstapriv, sta_addr); /*  get ap_info */
849
850         if (*psta == NULL) {
851                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("can't get psta under sta2sta_data_frame ; drop pkt\n"));
852                 if (adapter->registrypriv.mp_mode == 1) {
853                         if (check_fwstate(pmlmepriv, WIFI_MP_STATE) == true)
854                         adapter->mppriv.rx_pktloss++;
855                 }
856                 ret = _FAIL;
857                 goto exit;
858         }
859
860 exit:
861         return ret;
862 }
863
864 static int ap2sta_data_frame (
865         struct adapter *adapter,
866         struct recv_frame *precv_frame,
867         struct sta_info **psta)
868 {
869         u8 *ptr = precv_frame->rx_data;
870         struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
871         int ret = _SUCCESS;
872         struct  sta_priv *pstapriv = &adapter->stapriv;
873         struct  mlme_priv *pmlmepriv = &adapter->mlmepriv;
874         u8 *mybssid  = get_bssid(pmlmepriv);
875         u8 *myhwaddr = myid(&adapter->eeprompriv);
876         int bmcast = IS_MCAST(pattrib->dst);
877
878
879         if ((check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true) &&
880             (check_fwstate(pmlmepriv, _FW_LINKED) == true ||
881             check_fwstate(pmlmepriv, _FW_UNDER_LINKING))) {
882                 /*  filter packets that SA is myself or multicast or broadcast */
883                 if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN)) {
884                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" SA==myself\n"));
885                         ret = _FAIL;
886                         goto exit;
887                 }
888
889                 /*  da should be for me */
890                 if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast)) {
891                         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_,
892                                  (" ap2sta_data_frame:  compare DA fail; DA=%pM\n", (pattrib->dst)));
893                         ret = _FAIL;
894                         goto exit;
895                 }
896
897                 /*  check BSSID */
898                 if (!memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
899                     !memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
900                      (memcmp(pattrib->bssid, mybssid, ETH_ALEN))) {
901                         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_,
902                                  (" ap2sta_data_frame:  compare BSSID fail ; BSSID=%pM\n", (pattrib->bssid)));
903                         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("mybssid=%pM\n", (mybssid)));
904
905                         if (!bmcast) {
906                                 DBG_88E("issue_deauth to the nonassociated ap=%pM for the reason(7)\n", (pattrib->bssid));
907                                 issue_deauth(adapter, pattrib->bssid, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
908                         }
909
910                         ret = _FAIL;
911                         goto exit;
912                 }
913
914                 if (bmcast)
915                         *psta = rtw_get_bcmc_stainfo(adapter);
916                 else
917                         *psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /*  get ap_info */
918
919                 if (*psta == NULL) {
920                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("ap2sta: can't get psta under STATION_MODE ; drop pkt\n"));
921                         ret = _FAIL;
922                         goto exit;
923                 }
924
925                 /* if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) == WIFI_QOS_DATA_TYPE) { */
926                 /*  */
927
928                 if (GetFrameSubType(ptr) & BIT(6)) {
929                         /* No data, will not indicate to upper layer, temporily count it here */
930                         count_rx_stats(adapter, precv_frame, *psta);
931                         ret = RTW_RX_HANDLED;
932                         goto exit;
933                 }
934         } else if ((check_fwstate(pmlmepriv, WIFI_MP_STATE) == true) &&
935                    (check_fwstate(pmlmepriv, _FW_LINKED) == true)) {
936                 memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
937                 memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
938                 memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
939                 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
940                 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
941
942                 /*  */
943                 memcpy(pattrib->bssid,  mybssid, ETH_ALEN);
944
945                 *psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /*  get sta_info */
946                 if (*psta == NULL) {
947                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("can't get psta under MP_MODE ; drop pkt\n"));
948                         ret = _FAIL;
949                         goto exit;
950                 }
951         } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
952                 /* Special case */
953                 ret = RTW_RX_HANDLED;
954                 goto exit;
955         } else {
956                 if (!memcmp(myhwaddr, pattrib->dst, ETH_ALEN) && (!bmcast)) {
957                         *psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /*  get sta_info */
958                         if (*psta == NULL) {
959                                 DBG_88E("issue_deauth to the ap =%pM for the reason(7)\n", (pattrib->bssid));
960
961                                 issue_deauth(adapter, pattrib->bssid, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
962                         }
963                 }
964
965                 ret = _FAIL;
966         }
967
968 exit:
969
970
971         return ret;
972 }
973
974 static int sta2ap_data_frame(struct adapter *adapter,
975                              struct recv_frame *precv_frame,
976                              struct sta_info **psta)
977 {
978         struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
979         struct  sta_priv *pstapriv = &adapter->stapriv;
980         struct  mlme_priv *pmlmepriv = &adapter->mlmepriv;
981         u8 *ptr = precv_frame->rx_data;
982         unsigned char *mybssid  = get_bssid(pmlmepriv);
983         int ret = _SUCCESS;
984
985
986         if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
987                 /* For AP mode, RA = BSSID, TX = STA(SRC_ADDR), A3 = DST_ADDR */
988                 if (memcmp(pattrib->bssid, mybssid, ETH_ALEN)) {
989                         ret = _FAIL;
990                         goto exit;
991                 }
992
993                 *psta = rtw_get_stainfo(pstapriv, pattrib->src);
994                 if (*psta == NULL) {
995                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("can't get psta under AP_MODE; drop pkt\n"));
996                         DBG_88E("issue_deauth to sta=%pM for the reason(7)\n", (pattrib->src));
997
998                         issue_deauth(adapter, pattrib->src, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
999
1000                         ret = RTW_RX_HANDLED;
1001                         goto exit;
1002                 }
1003
1004                 process_pwrbit_data(adapter, precv_frame);
1005
1006                 if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) == WIFI_QOS_DATA_TYPE) {
1007                         process_wmmps_data(adapter, precv_frame);
1008                 }
1009
1010                 if (GetFrameSubType(ptr) & BIT(6)) {
1011                         /* No data, will not indicate to upper layer, temporily count it here */
1012                         count_rx_stats(adapter, precv_frame, *psta);
1013                         ret = RTW_RX_HANDLED;
1014                         goto exit;
1015                 }
1016         } else {
1017                 u8 *myhwaddr = myid(&adapter->eeprompriv);
1018                 if (memcmp(pattrib->ra, myhwaddr, ETH_ALEN)) {
1019                         ret = RTW_RX_HANDLED;
1020                         goto exit;
1021                 }
1022                 DBG_88E("issue_deauth to sta=%pM for the reason(7)\n", (pattrib->src));
1023                 issue_deauth(adapter, pattrib->src, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1024                 ret = RTW_RX_HANDLED;
1025                 goto exit;
1026         }
1027
1028 exit:
1029
1030
1031         return ret;
1032 }
1033
1034 static int validate_recv_ctrl_frame(struct adapter *padapter,
1035                                     struct recv_frame *precv_frame)
1036 {
1037 #ifdef CONFIG_88EU_AP_MODE
1038         struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
1039         struct sta_priv *pstapriv = &padapter->stapriv;
1040         u8 *pframe = precv_frame->rx_data;
1041
1042         if (GetFrameType(pframe) != WIFI_CTRL_TYPE)
1043                 return _FAIL;
1044
1045         /* receive the frames that ra(a1) is my address */
1046         if (memcmp(GetAddr1Ptr(pframe), myid(&padapter->eeprompriv), ETH_ALEN))
1047                 return _FAIL;
1048
1049         /* only handle ps-poll */
1050         if (GetFrameSubType(pframe) == WIFI_PSPOLL) {
1051                 u16 aid;
1052                 u8 wmmps_ac = 0;
1053                 struct sta_info *psta = NULL;
1054
1055                 aid = GetAid(pframe);
1056                 psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
1057
1058                 if ((psta == NULL) || (psta->aid != aid))
1059                         return _FAIL;
1060
1061                 /* for rx pkt statistics */
1062                 psta->sta_stats.rx_ctrl_pkts++;
1063
1064                 switch (pattrib->priority) {
1065                 case 1:
1066                 case 2:
1067                         wmmps_ac = psta->uapsd_bk&BIT(0);
1068                         break;
1069                 case 4:
1070                 case 5:
1071                         wmmps_ac = psta->uapsd_vi&BIT(0);
1072                         break;
1073                 case 6:
1074                 case 7:
1075                         wmmps_ac = psta->uapsd_vo&BIT(0);
1076                         break;
1077                 case 0:
1078                 case 3:
1079                 default:
1080                         wmmps_ac = psta->uapsd_be&BIT(0);
1081                         break;
1082                 }
1083
1084                 if (wmmps_ac)
1085                         return _FAIL;
1086
1087                 if (psta->state & WIFI_STA_ALIVE_CHK_STATE) {
1088                         DBG_88E("%s alive check-rx ps-poll\n", __func__);
1089                         psta->expire_to = pstapriv->expire_to;
1090                         psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
1091                 }
1092
1093                 if ((psta->state&WIFI_SLEEP_STATE) && (pstapriv->sta_dz_bitmap&BIT(psta->aid))) {
1094                         struct list_head *xmitframe_plist, *xmitframe_phead;
1095                         struct xmit_frame *pxmitframe = NULL;
1096
1097                         spin_lock_bh(&psta->sleep_q.lock);
1098
1099                         xmitframe_phead = get_list_head(&psta->sleep_q);
1100                         xmitframe_plist = xmitframe_phead->next;
1101
1102                         if ((rtw_end_of_queue_search(xmitframe_phead, xmitframe_plist)) == false) {
1103                                 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
1104
1105                                 xmitframe_plist = xmitframe_plist->next;
1106
1107                                 rtw_list_delete(&pxmitframe->list);
1108
1109                                 psta->sleepq_len--;
1110
1111                                 if (psta->sleepq_len > 0)
1112                                         pxmitframe->attrib.mdata = 1;
1113                                 else
1114                                         pxmitframe->attrib.mdata = 0;
1115
1116                                 pxmitframe->attrib.triggered = 1;
1117
1118                                 spin_unlock_bh(&psta->sleep_q.lock);
1119                                 if (rtw_hal_xmit(padapter, pxmitframe) == true)
1120                                         rtw_os_xmit_complete(padapter, pxmitframe);
1121                                 spin_lock_bh(&psta->sleep_q.lock);
1122
1123                                 if (psta->sleepq_len == 0) {
1124                                         pstapriv->tim_bitmap &= ~BIT(psta->aid);
1125
1126                                         /* update BCN for TIM IE */
1127                                         /* update_BCNTIM(padapter); */
1128                                         update_beacon(padapter, _TIM_IE_, NULL, false);
1129                                 }
1130                         } else {
1131                                 if (pstapriv->tim_bitmap&BIT(psta->aid)) {
1132                                         if (psta->sleepq_len == 0) {
1133                                                 DBG_88E("no buffered packets to xmit\n");
1134
1135                                                 /* issue nulldata with More data bit = 0 to indicate we have no buffered packets */
1136                                                 issue_nulldata(padapter, psta->hwaddr, 0, 0, 0);
1137                                         } else {
1138                                                 DBG_88E("error!psta->sleepq_len=%d\n", psta->sleepq_len);
1139                                                 psta->sleepq_len = 0;
1140                                         }
1141
1142                                         pstapriv->tim_bitmap &= ~BIT(psta->aid);
1143
1144                                         /* update BCN for TIM IE */
1145                                         /* update_BCNTIM(padapter); */
1146                                         update_beacon(padapter, _TIM_IE_, NULL, false);
1147                                 }
1148                         }
1149
1150                         spin_unlock_bh(&psta->sleep_q.lock);
1151                 }
1152         }
1153
1154 #endif
1155
1156         return _FAIL;
1157 }
1158
1159 struct recv_frame *recvframe_chk_defrag(struct adapter *padapter,
1160                                         struct recv_frame *precv_frame);
1161
1162 static int validate_recv_mgnt_frame(struct adapter *padapter,
1163                                     struct recv_frame *precv_frame)
1164 {
1165         struct sta_info *psta;
1166
1167         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("+validate_recv_mgnt_frame\n"));
1168
1169         precv_frame = recvframe_chk_defrag(padapter, precv_frame);
1170         if (precv_frame == NULL) {
1171                 RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("%s: fragment packet\n", __func__));
1172                 return _SUCCESS;
1173         }
1174
1175         /* for rx pkt statistics */
1176         psta = rtw_get_stainfo(&padapter->stapriv,
1177                                GetAddr2Ptr(precv_frame->rx_data));
1178         if (psta) {
1179                 psta->sta_stats.rx_mgnt_pkts++;
1180                 if (GetFrameSubType(precv_frame->rx_data) == WIFI_BEACON) {
1181                         psta->sta_stats.rx_beacon_pkts++;
1182                 } else if (GetFrameSubType(precv_frame->rx_data) == WIFI_PROBEREQ) {
1183                         psta->sta_stats.rx_probereq_pkts++;
1184                 } else if (GetFrameSubType(precv_frame->rx_data) == WIFI_PROBERSP) {
1185                         if (!memcmp(padapter->eeprompriv.mac_addr,
1186                                     GetAddr1Ptr(precv_frame->rx_data), ETH_ALEN))
1187                                 psta->sta_stats.rx_probersp_pkts++;
1188                         else if (is_broadcast_mac_addr(GetAddr1Ptr(precv_frame->rx_data)) ||
1189                                  is_multicast_mac_addr(GetAddr1Ptr(precv_frame->rx_data)))
1190                                 psta->sta_stats.rx_probersp_bm_pkts++;
1191                         else
1192                                 psta->sta_stats.rx_probersp_uo_pkts++;
1193                 }
1194         }
1195
1196         mgt_dispatcher(padapter, precv_frame);
1197
1198         return _SUCCESS;
1199 }
1200
1201 static int validate_recv_data_frame(struct adapter *adapter,
1202                                     struct recv_frame *precv_frame)
1203 {
1204         u8 bretry;
1205         u8 *psa, *pda, *pbssid;
1206         struct sta_info *psta = NULL;
1207         u8 *ptr = precv_frame->rx_data;
1208         struct rx_pkt_attrib    *pattrib = &precv_frame->attrib;
1209         struct security_priv    *psecuritypriv = &adapter->securitypriv;
1210         int ret = _SUCCESS;
1211
1212
1213         bretry = GetRetry(ptr);
1214         pda = get_da(ptr);
1215         psa = get_sa(ptr);
1216         pbssid = get_hdr_bssid(ptr);
1217
1218         if (pbssid == NULL) {
1219                 ret = _FAIL;
1220                 goto exit;
1221         }
1222
1223         memcpy(pattrib->dst, pda, ETH_ALEN);
1224         memcpy(pattrib->src, psa, ETH_ALEN);
1225
1226         memcpy(pattrib->bssid, pbssid, ETH_ALEN);
1227
1228         switch (pattrib->to_fr_ds) {
1229         case 0:
1230                 memcpy(pattrib->ra, pda, ETH_ALEN);
1231                 memcpy(pattrib->ta, psa, ETH_ALEN);
1232                 ret = sta2sta_data_frame(adapter, precv_frame, &psta);
1233                 break;
1234         case 1:
1235                 memcpy(pattrib->ra, pda, ETH_ALEN);
1236                 memcpy(pattrib->ta, pbssid, ETH_ALEN);
1237                 ret = ap2sta_data_frame(adapter, precv_frame, &psta);
1238                 break;
1239         case 2:
1240                 memcpy(pattrib->ra, pbssid, ETH_ALEN);
1241                 memcpy(pattrib->ta, psa, ETH_ALEN);
1242                 ret = sta2ap_data_frame(adapter, precv_frame, &psta);
1243                 break;
1244         case 3:
1245                 memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
1246                 memcpy(pattrib->ta, GetAddr2Ptr(ptr), ETH_ALEN);
1247                 ret = _FAIL;
1248                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" case 3\n"));
1249                 break;
1250         default:
1251                 ret = _FAIL;
1252                 break;
1253         }
1254
1255         if (ret == _FAIL) {
1256                 goto exit;
1257         } else if (ret == RTW_RX_HANDLED) {
1258                 goto exit;
1259         }
1260
1261         if (psta == NULL) {
1262                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" after to_fr_ds_chk; psta==NULL\n"));
1263                 ret = _FAIL;
1264                 goto exit;
1265         }
1266
1267         /* psta->rssi = prxcmd->rssi; */
1268         /* psta->signal_quality = prxcmd->sq; */
1269         precv_frame->psta = psta;
1270
1271         pattrib->amsdu = 0;
1272         pattrib->ack_policy = 0;
1273         /* parsing QC field */
1274         if (pattrib->qos == 1) {
1275                 pattrib->priority = GetPriority((ptr + 24));
1276                 pattrib->ack_policy = GetAckpolicy((ptr + 24));
1277                 pattrib->amsdu = GetAMsdu((ptr + 24));
1278                 pattrib->hdrlen = pattrib->to_fr_ds == 3 ? 32 : 26;
1279
1280                 if (pattrib->priority != 0 && pattrib->priority != 3)
1281                         adapter->recvpriv.bIsAnyNonBEPkts = true;
1282         } else {
1283                 pattrib->priority = 0;
1284                 pattrib->hdrlen = pattrib->to_fr_ds == 3 ? 30 : 24;
1285         }
1286
1287         if (pattrib->order)/* HT-CTRL 11n */
1288                 pattrib->hdrlen += 4;
1289
1290         precv_frame->preorder_ctrl = &psta->recvreorder_ctrl[pattrib->priority];
1291
1292         /*  decache, drop duplicate recv packets */
1293         if (recv_decache(precv_frame, bretry, &psta->sta_recvpriv.rxcache) == _FAIL) {
1294                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("decache : drop pkt\n"));
1295                 ret = _FAIL;
1296                 goto exit;
1297         }
1298
1299         if (pattrib->privacy) {
1300                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("validate_recv_data_frame:pattrib->privacy=%x\n", pattrib->privacy));
1301                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n ^^^^^^^^^^^IS_MCAST(pattrib->ra(0x%02x))=%d^^^^^^^^^^^^^^^6\n", pattrib->ra[0], IS_MCAST(pattrib->ra)));
1302
1303                 GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt, IS_MCAST(pattrib->ra));
1304
1305                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n pattrib->encrypt=%d\n", pattrib->encrypt));
1306
1307                 SET_ICE_IV_LEN(pattrib->iv_len, pattrib->icv_len, pattrib->encrypt);
1308         } else {
1309                 pattrib->encrypt = 0;
1310                 pattrib->iv_len = 0;
1311                 pattrib->icv_len = 0;
1312         }
1313
1314 exit:
1315
1316
1317         return ret;
1318 }
1319
1320 static int validate_recv_frame(struct adapter *adapter,
1321                                struct recv_frame *precv_frame)
1322 {
1323         /* shall check frame subtype, to / from ds, da, bssid */
1324
1325         /* then call check if rx seq/frag. duplicated. */
1326
1327         u8 type;
1328         u8 subtype;
1329         int retval = _SUCCESS;
1330         u8 bDumpRxPkt;
1331         struct rx_pkt_attrib *pattrib = &precv_frame->attrib;
1332         u8 *ptr = precv_frame->rx_data;
1333         u8  ver = (unsigned char) (*ptr)&0x3;
1334         struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv;
1335
1336
1337         if (pmlmeext->sitesurvey_res.state == SCAN_PROCESS) {
1338                 int ch_set_idx = rtw_ch_set_search_ch(pmlmeext->channel_set, rtw_get_oper_ch(adapter));
1339                 if (ch_set_idx >= 0)
1340                         pmlmeext->channel_set[ch_set_idx].rx_count++;
1341         }
1342
1343         /* add version chk */
1344         if (ver != 0) {
1345                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_data_frame fail! (ver!=0)\n"));
1346                 retval = _FAIL;
1347                 goto exit;
1348         }
1349
1350         type =  GetFrameType(ptr);
1351         subtype = GetFrameSubType(ptr); /* bit(7)~bit(2) */
1352
1353         pattrib->to_fr_ds = get_tofr_ds(ptr);
1354
1355         pattrib->frag_num = GetFragNum(ptr);
1356         pattrib->seq_num = GetSequence(ptr);
1357
1358         pattrib->pw_save = GetPwrMgt(ptr);
1359         pattrib->mfrag = GetMFrag(ptr);
1360         pattrib->mdata = GetMData(ptr);
1361         pattrib->privacy = GetPrivacy(ptr);
1362         pattrib->order = GetOrder(ptr);
1363
1364         /* Dump rx packets */
1365         rtw_hal_get_def_var(adapter, HAL_DEF_DBG_DUMP_RXPKT, &(bDumpRxPkt));
1366         if (bDumpRxPkt == 1) {/* dump all rx packets */
1367                 int i;
1368                 DBG_88E("#############################\n");
1369
1370                 for (i = 0; i < 64; i = i+8)
1371                         DBG_88E("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:\n", *(ptr+i),
1372                                 *(ptr+i+1), *(ptr+i+2), *(ptr+i+3), *(ptr+i+4), *(ptr+i+5), *(ptr+i+6), *(ptr+i+7));
1373                 DBG_88E("#############################\n");
1374         } else if (bDumpRxPkt == 2) {
1375                 if (type == WIFI_MGT_TYPE) {
1376                         int i;
1377                         DBG_88E("#############################\n");
1378
1379                         for (i = 0; i < 64; i = i+8)
1380                                 DBG_88E("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:\n", *(ptr+i),
1381                                         *(ptr+i+1), *(ptr+i+2), *(ptr+i+3), *(ptr+i+4), *(ptr+i+5), *(ptr+i+6), *(ptr+i+7));
1382                         DBG_88E("#############################\n");
1383                 }
1384         } else if (bDumpRxPkt == 3) {
1385                 if (type == WIFI_DATA_TYPE) {
1386                         int i;
1387                         DBG_88E("#############################\n");
1388
1389                         for (i = 0; i < 64; i = i+8)
1390                                 DBG_88E("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:\n", *(ptr+i),
1391                                         *(ptr+i+1), *(ptr+i+2), *(ptr+i+3), *(ptr+i+4), *(ptr+i+5), *(ptr+i+6), *(ptr+i+7));
1392                         DBG_88E("#############################\n");
1393                 }
1394         }
1395         switch (type) {
1396         case WIFI_MGT_TYPE: /* mgnt */
1397                 retval = validate_recv_mgnt_frame(adapter, precv_frame);
1398                 if (retval == _FAIL)
1399                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_mgnt_frame fail\n"));
1400                 retval = _FAIL; /*  only data frame return _SUCCESS */
1401                 break;
1402         case WIFI_CTRL_TYPE: /* ctrl */
1403                 retval = validate_recv_ctrl_frame(adapter, precv_frame);
1404                 if (retval == _FAIL)
1405                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_ctrl_frame fail\n"));
1406                 retval = _FAIL; /*  only data frame return _SUCCESS */
1407                 break;
1408         case WIFI_DATA_TYPE: /* data */
1409                 rtw_led_control(adapter, LED_CTL_RX);
1410                 pattrib->qos = (subtype & BIT(7)) ? 1 : 0;
1411                 retval = validate_recv_data_frame(adapter, precv_frame);
1412                 if (retval == _FAIL) {
1413                         struct recv_priv *precvpriv = &adapter->recvpriv;
1414                         precvpriv->rx_drop++;
1415                 }
1416                 break;
1417         default:
1418                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_data_frame fail! type= 0x%x\n", type));
1419                 retval = _FAIL;
1420                 break;
1421         }
1422
1423 exit:
1424
1425
1426         return retval;
1427 }
1428
1429 /* remove the wlanhdr and add the eth_hdr */
1430
1431 static int wlanhdr_to_ethhdr(struct recv_frame *precvframe)
1432 {
1433         int     rmv_len;
1434         u16     eth_type, len;
1435         __be16 be_tmp;
1436         u8      bsnaphdr;
1437         u8      *psnap_type;
1438         struct ieee80211_snap_hdr       *psnap;
1439
1440         int ret = _SUCCESS;
1441         struct adapter          *adapter = precvframe->adapter;
1442         struct mlme_priv        *pmlmepriv = &adapter->mlmepriv;
1443         u8 *ptr = precvframe->rx_data;
1444         struct rx_pkt_attrib *pattrib = &precvframe->attrib;
1445
1446         if (pattrib->encrypt)
1447                 recvframe_pull_tail(precvframe, pattrib->icv_len);
1448
1449         psnap = (struct ieee80211_snap_hdr *)(ptr+pattrib->hdrlen + pattrib->iv_len);
1450         psnap_type = ptr+pattrib->hdrlen + pattrib->iv_len+SNAP_SIZE;
1451         /* convert hdr + possible LLC headers into Ethernet header */
1452         if ((!memcmp(psnap, rtw_rfc1042_header, SNAP_SIZE) &&
1453              (!memcmp(psnap_type, SNAP_ETH_TYPE_IPX, 2) == false) &&
1454              (!memcmp(psnap_type, SNAP_ETH_TYPE_APPLETALK_AARP, 2) == false)) ||
1455              !memcmp(psnap, rtw_bridge_tunnel_header, SNAP_SIZE)) {
1456                 /* remove RFC1042 or Bridge-Tunnel encapsulation and replace EtherType */
1457                 bsnaphdr = true;
1458         } else {
1459                 /* Leave Ethernet header part of hdr and full payload */
1460                 bsnaphdr = false;
1461         }
1462
1463         rmv_len = pattrib->hdrlen + pattrib->iv_len + (bsnaphdr ? SNAP_SIZE : 0);
1464         len = precvframe->len - rmv_len;
1465
1466         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_,
1467                  ("\n===pattrib->hdrlen: %x,  pattrib->iv_len:%x===\n\n", pattrib->hdrlen,  pattrib->iv_len));
1468
1469         memcpy(&be_tmp, ptr+rmv_len, 2);
1470         eth_type = ntohs(be_tmp); /* pattrib->ether_type */
1471         pattrib->eth_type = eth_type;
1472
1473         if ((check_fwstate(pmlmepriv, WIFI_MP_STATE))) {
1474                 ptr += rmv_len;
1475                 *ptr = 0x87;
1476                 *(ptr+1) = 0x12;
1477
1478                 eth_type = 0x8712;
1479                 /*  append rx status for mp test packets */
1480                 ptr = recvframe_pull(precvframe, (rmv_len-sizeof(struct ethhdr)+2)-24);
1481                 memcpy(ptr, get_rxmem(precvframe), 24);
1482                 ptr += 24;
1483         } else {
1484                 ptr = recvframe_pull(precvframe, (rmv_len-sizeof(struct ethhdr) + (bsnaphdr ? 2 : 0)));
1485         }
1486
1487         memcpy(ptr, pattrib->dst, ETH_ALEN);
1488         memcpy(ptr+ETH_ALEN, pattrib->src, ETH_ALEN);
1489
1490         if (!bsnaphdr) {
1491                 be_tmp = htons(len);
1492                 memcpy(ptr+12, &be_tmp, 2);
1493         }
1494
1495         return ret;
1496 }
1497
1498 /* perform defrag */
1499 static struct recv_frame *recvframe_defrag(struct adapter *adapter,
1500                                            struct __queue *defrag_q)
1501 {
1502         struct list_head *plist, *phead;
1503         u8 wlanhdr_offset;
1504         u8      curfragnum;
1505         struct recv_frame *pfhdr, *pnfhdr;
1506         struct recv_frame *prframe, *pnextrframe;
1507         struct __queue *pfree_recv_queue;
1508
1509
1510         curfragnum = 0;
1511         pfree_recv_queue = &adapter->recvpriv.free_recv_queue;
1512
1513         phead = get_list_head(defrag_q);
1514         plist = phead->next;
1515         pfhdr = container_of(plist, struct recv_frame, list);
1516         prframe = (struct recv_frame *)pfhdr;
1517         rtw_list_delete(&(prframe->list));
1518
1519         if (curfragnum != pfhdr->attrib.frag_num) {
1520                 /* the first fragment number must be 0 */
1521                 /* free the whole queue */
1522                 rtw_free_recvframe(prframe, pfree_recv_queue);
1523                 rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1524
1525                 return NULL;
1526         }
1527
1528         curfragnum++;
1529
1530         plist = get_list_head(defrag_q);
1531
1532         plist = plist->next;
1533
1534         while (rtw_end_of_queue_search(phead, plist) == false) {
1535                 pnfhdr = container_of(plist, struct recv_frame, list);
1536                 pnextrframe = (struct recv_frame *)pnfhdr;
1537
1538                 /* check the fragment sequence  (2nd ~n fragment frame) */
1539
1540                 if (curfragnum != pnfhdr->attrib.frag_num) {
1541                         /* the fragment number must be increasing  (after decache) */
1542                         /* release the defrag_q & prframe */
1543                         rtw_free_recvframe(prframe, pfree_recv_queue);
1544                         rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1545                         return NULL;
1546                 }
1547
1548                 curfragnum++;
1549
1550                 /* copy the 2nd~n fragment frame's payload to the first fragment */
1551                 /* get the 2nd~last fragment frame's payload */
1552
1553                 wlanhdr_offset = pnfhdr->attrib.hdrlen + pnfhdr->attrib.iv_len;
1554
1555                 recvframe_pull(pnextrframe, wlanhdr_offset);
1556
1557                 /* append  to first fragment frame's tail (if privacy frame, pull the ICV) */
1558                 recvframe_pull_tail(prframe, pfhdr->attrib.icv_len);
1559
1560                 /* memcpy */
1561                 memcpy(pfhdr->rx_tail, pnfhdr->rx_data, pnfhdr->len);
1562
1563                 recvframe_put(prframe, pnfhdr->len);
1564
1565                 pfhdr->attrib.icv_len = pnfhdr->attrib.icv_len;
1566                 plist = plist->next;
1567         }
1568
1569         /* free the defrag_q queue and return the prframe */
1570         rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1571
1572         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("Performance defrag!!!!!\n"));
1573
1574
1575         return prframe;
1576 }
1577
1578 /* check if need to defrag, if needed queue the frame to defrag_q */
1579 struct recv_frame *recvframe_chk_defrag(struct adapter *padapter,
1580                                         struct recv_frame *precv_frame)
1581 {
1582         u8      ismfrag;
1583         u8      fragnum;
1584         u8      *psta_addr;
1585         struct recv_frame *pfhdr;
1586         struct sta_info *psta;
1587         struct sta_priv *pstapriv;
1588         struct list_head *phead;
1589         struct recv_frame *prtnframe = NULL;
1590         struct __queue *pfree_recv_queue, *pdefrag_q;
1591
1592
1593         pstapriv = &padapter->stapriv;
1594
1595         pfhdr = precv_frame;
1596
1597         pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
1598
1599         /* need to define struct of wlan header frame ctrl */
1600         ismfrag = pfhdr->attrib.mfrag;
1601         fragnum = pfhdr->attrib.frag_num;
1602
1603         psta_addr = pfhdr->attrib.ta;
1604         psta = rtw_get_stainfo(pstapriv, psta_addr);
1605         if (psta == NULL) {
1606                 u8 type = GetFrameType(pfhdr->rx_data);
1607                 if (type != WIFI_DATA_TYPE) {
1608                         psta = rtw_get_bcmc_stainfo(padapter);
1609                         pdefrag_q = &psta->sta_recvpriv.defrag_q;
1610                 } else {
1611                         pdefrag_q = NULL;
1612                 }
1613         } else {
1614                 pdefrag_q = &psta->sta_recvpriv.defrag_q;
1615         }
1616
1617         if ((ismfrag == 0) && (fragnum == 0))
1618                 prtnframe = precv_frame;/* isn't a fragment frame */
1619
1620         if (ismfrag == 1) {
1621                 /* 0~(n-1) fragment frame */
1622                 /* enqueue to defraf_g */
1623                 if (pdefrag_q != NULL) {
1624                         if (fragnum == 0) {
1625                                 /* the first fragment */
1626                                 if (_rtw_queue_empty(pdefrag_q) == false) {
1627                                         /* free current defrag_q */
1628                                         rtw_free_recvframe_queue(pdefrag_q, pfree_recv_queue);
1629                                 }
1630                         }
1631
1632                         /* Then enqueue the 0~(n-1) fragment into the defrag_q */
1633
1634                         phead = get_list_head(pdefrag_q);
1635                         rtw_list_insert_tail(&pfhdr->list, phead);
1636
1637                         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("Enqueuq: ismfrag=%d, fragnum=%d\n", ismfrag, fragnum));
1638
1639                         prtnframe = NULL;
1640                 } else {
1641                         /* can't find this ta's defrag_queue, so free this recv_frame */
1642                         rtw_free_recvframe(precv_frame, pfree_recv_queue);
1643                         prtnframe = NULL;
1644                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("Free because pdefrag_q==NULL: ismfrag=%d, fragnum=%d\n", ismfrag, fragnum));
1645                 }
1646         }
1647
1648         if ((ismfrag == 0) && (fragnum != 0)) {
1649                 /* the last fragment frame */
1650                 /* enqueue the last fragment */
1651                 if (pdefrag_q != NULL) {
1652                         phead = get_list_head(pdefrag_q);
1653                         rtw_list_insert_tail(&pfhdr->list, phead);
1654
1655                         /* call recvframe_defrag to defrag */
1656                         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("defrag: ismfrag=%d, fragnum=%d\n", ismfrag, fragnum));
1657                         precv_frame = recvframe_defrag(padapter, pdefrag_q);
1658                         prtnframe = precv_frame;
1659                 } else {
1660                         /* can't find this ta's defrag_queue, so free this recv_frame */
1661                         rtw_free_recvframe(precv_frame, pfree_recv_queue);
1662                         prtnframe = NULL;
1663                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("Free because pdefrag_q==NULL: ismfrag=%d, fragnum=%d\n", ismfrag, fragnum));
1664                 }
1665         }
1666
1667         if ((prtnframe != NULL) && (prtnframe->attrib.privacy)) {
1668                 /* after defrag we must check tkip mic code */
1669                 if (recvframe_chkmic(padapter,  prtnframe) == _FAIL) {
1670                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chkmic(padapter,  prtnframe)==_FAIL\n"));
1671                         rtw_free_recvframe(prtnframe, pfree_recv_queue);
1672                         prtnframe = NULL;
1673                 }
1674         }
1675
1676
1677         return prtnframe;
1678 }
1679
1680 static int amsdu_to_msdu(struct adapter *padapter, struct recv_frame *prframe)
1681 {
1682         int     a_len, padding_len;
1683         u16     eth_type, nSubframe_Length;
1684         u8      nr_subframes, i;
1685         unsigned char *pdata;
1686         struct rx_pkt_attrib *pattrib;
1687         unsigned char *data_ptr;
1688         struct sk_buff *sub_skb, *subframes[MAX_SUBFRAME_COUNT];
1689         struct recv_priv *precvpriv = &padapter->recvpriv;
1690         struct __queue *pfree_recv_queue = &(precvpriv->free_recv_queue);
1691         int     ret = _SUCCESS;
1692         nr_subframes = 0;
1693
1694         pattrib = &prframe->attrib;
1695
1696         recvframe_pull(prframe, prframe->attrib.hdrlen);
1697
1698         if (prframe->attrib.iv_len > 0)
1699                 recvframe_pull(prframe, prframe->attrib.iv_len);
1700
1701         a_len = prframe->len;
1702
1703         pdata = prframe->rx_data;
1704
1705         while (a_len > ETH_HLEN) {
1706                 /* Offset 12 denote 2 mac address */
1707                 nSubframe_Length = RTW_GET_BE16(pdata + 12);
1708
1709                 if (a_len < (ETHERNET_HEADER_SIZE + nSubframe_Length)) {
1710                         DBG_88E("nRemain_Length is %d and nSubframe_Length is : %d\n", a_len, nSubframe_Length);
1711                         goto exit;
1712                 }
1713
1714                 /* move the data point to data content */
1715                 pdata += ETH_HLEN;
1716                 a_len -= ETH_HLEN;
1717
1718                 /* Allocate new skb for releasing to upper layer */
1719                 sub_skb = dev_alloc_skb(nSubframe_Length + 12);
1720                 if (sub_skb) {
1721                         skb_reserve(sub_skb, 12);
1722                         data_ptr = (u8 *)skb_put(sub_skb, nSubframe_Length);
1723                         memcpy(data_ptr, pdata, nSubframe_Length);
1724                 } else {
1725                         sub_skb = skb_clone(prframe->pkt, GFP_ATOMIC);
1726                         if (sub_skb) {
1727                                 sub_skb->data = pdata;
1728                                 sub_skb->len = nSubframe_Length;
1729                                 skb_set_tail_pointer(sub_skb, nSubframe_Length);
1730                         } else {
1731                                 DBG_88E("skb_clone() Fail!!! , nr_subframes=%d\n", nr_subframes);
1732                                 break;
1733                         }
1734                 }
1735
1736                 subframes[nr_subframes++] = sub_skb;
1737
1738                 if (nr_subframes >= MAX_SUBFRAME_COUNT) {
1739                         DBG_88E("ParseSubframe(): Too many Subframes! Packets dropped!\n");
1740                         break;
1741                 }
1742
1743                 pdata += nSubframe_Length;
1744                 a_len -= nSubframe_Length;
1745                 if (a_len != 0) {
1746                         padding_len = 4 - ((nSubframe_Length + ETH_HLEN) & (4-1));
1747                         if (padding_len == 4) {
1748                                 padding_len = 0;
1749                         }
1750
1751                         if (a_len < padding_len) {
1752                                 goto exit;
1753                         }
1754                         pdata += padding_len;
1755                         a_len -= padding_len;
1756                 }
1757         }
1758
1759         for (i = 0; i < nr_subframes; i++) {
1760                 sub_skb = subframes[i];
1761                 /* convert hdr + possible LLC headers into Ethernet header */
1762                 eth_type = RTW_GET_BE16(&sub_skb->data[6]);
1763                 if (sub_skb->len >= 8 &&
1764                     ((!memcmp(sub_skb->data, rtw_rfc1042_header, SNAP_SIZE) &&
1765                           eth_type != ETH_P_AARP && eth_type != ETH_P_IPX) ||
1766                          !memcmp(sub_skb->data, rtw_bridge_tunnel_header, SNAP_SIZE))) {
1767                         /* remove RFC1042 or Bridge-Tunnel encapsulation and replace EtherType */
1768                         skb_pull(sub_skb, SNAP_SIZE);
1769                         memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src, ETH_ALEN);
1770                         memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst, ETH_ALEN);
1771                 } else {
1772                         __be16 len;
1773                         /* Leave Ethernet header part of hdr and full payload */
1774                         len = htons(sub_skb->len);
1775                         memcpy(skb_push(sub_skb, 2), &len, 2);
1776                         memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src, ETH_ALEN);
1777                         memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst, ETH_ALEN);
1778                 }
1779
1780                 /* Indicate the packets to upper layer */
1781                 /*  Insert NAT2.5 RX here! */
1782                 sub_skb->protocol = eth_type_trans(sub_skb, padapter->pnetdev);
1783                 sub_skb->dev = padapter->pnetdev;
1784
1785                 sub_skb->ip_summed = CHECKSUM_NONE;
1786
1787                 netif_rx(sub_skb);
1788         }
1789
1790 exit:
1791
1792         prframe->len = 0;
1793         rtw_free_recvframe(prframe, pfree_recv_queue);/* free this recv_frame */
1794
1795         return ret;
1796 }
1797
1798 static int check_indicate_seq(struct recv_reorder_ctrl *preorder_ctrl, u16 seq_num)
1799 {
1800         u8      wsize = preorder_ctrl->wsize_b;
1801         u16     wend = (preorder_ctrl->indicate_seq + wsize - 1) & 0xFFF;/*  4096; */
1802
1803         /*  Rx Reorder initialize condition. */
1804         if (preorder_ctrl->indicate_seq == 0xFFFF)
1805                 preorder_ctrl->indicate_seq = seq_num;
1806
1807         /*  Drop out the packet which SeqNum is smaller than WinStart */
1808         if (SN_LESS(seq_num, preorder_ctrl->indicate_seq))
1809                 return false;
1810
1811         /*  */
1812         /*  Sliding window manipulation. Conditions includes: */
1813         /*  1. Incoming SeqNum is equal to WinStart =>Window shift 1 */
1814         /*  2. Incoming SeqNum is larger than the WinEnd => Window shift N */
1815         /*  */
1816         if (SN_EQUAL(seq_num, preorder_ctrl->indicate_seq)) {
1817                 preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
1818         } else if (SN_LESS(wend, seq_num)) {
1819                 if (seq_num >= (wsize - 1))
1820                         preorder_ctrl->indicate_seq = seq_num + 1 - wsize;
1821                 else
1822                         preorder_ctrl->indicate_seq = 0xFFF - (wsize - (seq_num + 1)) + 1;
1823         }
1824
1825         return true;
1826 }
1827
1828 int enqueue_reorder_recvframe(struct recv_reorder_ctrl *preorder_ctrl,
1829                               struct recv_frame *prframe)
1830 {
1831         struct rx_pkt_attrib *pattrib = &prframe->attrib;
1832         struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
1833         struct list_head *phead, *plist;
1834         struct recv_frame *hdr;
1835         struct rx_pkt_attrib *pnextattrib;
1836
1837         phead = get_list_head(ppending_recvframe_queue);
1838         plist = phead->next;
1839
1840         while (rtw_end_of_queue_search(phead, plist) == false) {
1841                 hdr = container_of(plist, struct recv_frame, list);
1842                 pnextattrib = &hdr->attrib;
1843
1844                 if (SN_LESS(pnextattrib->seq_num, pattrib->seq_num))
1845                         plist = plist->next;
1846                 else if (SN_EQUAL(pnextattrib->seq_num, pattrib->seq_num))
1847                         return false;
1848                 else
1849                         break;
1850         }
1851
1852         rtw_list_delete(&(prframe->list));
1853
1854         rtw_list_insert_tail(&(prframe->list), plist);
1855         return true;
1856 }
1857
1858 static int recv_indicatepkts_in_order(struct adapter *padapter, struct recv_reorder_ctrl *preorder_ctrl, int bforced)
1859 {
1860         struct list_head *phead, *plist;
1861         struct recv_frame *prframe;
1862         struct recv_frame *prhdr;
1863         struct rx_pkt_attrib *pattrib;
1864         int bPktInBuf = false;
1865         struct recv_priv *precvpriv = &padapter->recvpriv;
1866         struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
1867
1868         phead =         get_list_head(ppending_recvframe_queue);
1869         plist = phead->next;
1870
1871         /*  Handling some condition for forced indicate case. */
1872         if (bforced) {
1873                 if (rtw_is_list_empty(phead))
1874                         return true;
1875
1876                 prhdr = container_of(plist, struct recv_frame, list);
1877                 pattrib = &prhdr->attrib;
1878                 preorder_ctrl->indicate_seq = pattrib->seq_num;
1879         }
1880
1881         /*  Prepare indication list and indication. */
1882         /*  Check if there is any packet need indicate. */
1883         while (!rtw_is_list_empty(phead)) {
1884                 prhdr = container_of(plist, struct recv_frame, list);
1885                 prframe = (struct recv_frame *)prhdr;
1886                 pattrib = &prframe->attrib;
1887
1888                 if (!SN_LESS(preorder_ctrl->indicate_seq, pattrib->seq_num)) {
1889                         RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_,
1890                                  ("recv_indicatepkts_in_order: indicate=%d seq=%d amsdu=%d\n",
1891                                   preorder_ctrl->indicate_seq, pattrib->seq_num, pattrib->amsdu));
1892                         plist = plist->next;
1893                         rtw_list_delete(&(prframe->list));
1894
1895                         if (SN_EQUAL(preorder_ctrl->indicate_seq, pattrib->seq_num))
1896                                 preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
1897
1898                         /* Set this as a lock to make sure that only one thread is indicating packet. */
1899
1900                         /* indicate this recv_frame */
1901                         if (!pattrib->amsdu) {
1902                                 if ((!padapter->bDriverStopped) &&
1903                                     (!padapter->bSurpriseRemoved))
1904                                         rtw_recv_indicatepkt(padapter, prframe);/* indicate this recv_frame */
1905                         } else if (pattrib->amsdu == 1) {
1906                                 if (amsdu_to_msdu(padapter, prframe) != _SUCCESS)
1907                                         rtw_free_recvframe(prframe, &precvpriv->free_recv_queue);
1908                         } else {
1909                                 /* error condition; */
1910                         }
1911
1912                         /* Update local variables. */
1913                         bPktInBuf = false;
1914                 } else {
1915                         bPktInBuf = true;
1916                         break;
1917                 }
1918         }
1919         return bPktInBuf;
1920 }
1921
1922 static int recv_indicatepkt_reorder(struct adapter *padapter,
1923                                     struct recv_frame *prframe)
1924 {
1925         int retval = _SUCCESS;
1926         struct rx_pkt_attrib *pattrib = &prframe->attrib;
1927         struct recv_reorder_ctrl *preorder_ctrl = prframe->preorder_ctrl;
1928         struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
1929
1930         if (!pattrib->amsdu) {
1931                 /* s1. */
1932                 wlanhdr_to_ethhdr(prframe);
1933
1934                 if ((pattrib->qos != 1) || (pattrib->eth_type == 0x0806) ||
1935                     (pattrib->ack_policy != 0)) {
1936                         if ((!padapter->bDriverStopped) &&
1937                             (!padapter->bSurpriseRemoved)) {
1938                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@  recv_indicatepkt_reorder -recv_func recv_indicatepkt\n"));
1939
1940                                 rtw_recv_indicatepkt(padapter, prframe);
1941                                 return _SUCCESS;
1942                         }
1943
1944                         return _FAIL;
1945                 }
1946
1947                 if (!preorder_ctrl->enable) {
1948                         /* indicate this recv_frame */
1949                         preorder_ctrl->indicate_seq = pattrib->seq_num;
1950                         rtw_recv_indicatepkt(padapter, prframe);
1951
1952                         preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1)%4096;
1953                         return _SUCCESS;
1954                 }
1955         } else if (pattrib->amsdu == 1) { /* temp filter -> means didn't support A-MSDUs in a A-MPDU */
1956                 if (!preorder_ctrl->enable) {
1957                         preorder_ctrl->indicate_seq = pattrib->seq_num;
1958                         retval = amsdu_to_msdu(padapter, prframe);
1959
1960                         preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1)%4096;
1961                         return retval;
1962                 }
1963         }
1964
1965         spin_lock_bh(&ppending_recvframe_queue->lock);
1966
1967         RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_,
1968                  ("recv_indicatepkt_reorder: indicate=%d seq=%d\n",
1969                   preorder_ctrl->indicate_seq, pattrib->seq_num));
1970
1971         /* s2. check if winstart_b(indicate_seq) needs to been updated */
1972         if (!check_indicate_seq(preorder_ctrl, pattrib->seq_num)) {
1973                 rtw_recv_indicatepkt(padapter, prframe);
1974
1975                 spin_unlock_bh(&ppending_recvframe_queue->lock);
1976
1977                 goto _success_exit;
1978         }
1979
1980         /* s3. Insert all packet into Reorder Queue to maintain its ordering. */
1981         if (!enqueue_reorder_recvframe(preorder_ctrl, prframe))
1982                 goto _err_exit;
1983
1984         /* s4. */
1985         /*  Indication process. */
1986         /*  After Packet dropping and Sliding Window shifting as above, we can now just indicate the packets */
1987         /*  with the SeqNum smaller than latest WinStart and buffer other packets. */
1988         /*  */
1989         /*  For Rx Reorder condition: */
1990         /*  1. All packets with SeqNum smaller than WinStart => Indicate */
1991         /*  2. All packets with SeqNum larger than or equal to WinStart => Buffer it. */
1992         /*  */
1993
1994         /* recv_indicatepkts_in_order(padapter, preorder_ctrl, true); */
1995         if (recv_indicatepkts_in_order(padapter, preorder_ctrl, false)) {
1996                 _set_timer(&preorder_ctrl->reordering_ctrl_timer, REORDER_WAIT_TIME);
1997                 spin_unlock_bh(&ppending_recvframe_queue->lock);
1998         } else {
1999                 spin_unlock_bh(&ppending_recvframe_queue->lock);
2000                 del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
2001         }
2002
2003 _success_exit:
2004
2005         return _SUCCESS;
2006
2007 _err_exit:
2008
2009         spin_unlock_bh(&ppending_recvframe_queue->lock);
2010
2011         return _FAIL;
2012 }
2013
2014 void rtw_reordering_ctrl_timeout_handler(void *pcontext)
2015 {
2016         struct recv_reorder_ctrl *preorder_ctrl = (struct recv_reorder_ctrl *)pcontext;
2017         struct adapter *padapter = preorder_ctrl->padapter;
2018         struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
2019
2020         if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
2021                 return;
2022
2023         spin_lock_bh(&ppending_recvframe_queue->lock);
2024
2025         if (recv_indicatepkts_in_order(padapter, preorder_ctrl, true) == true)
2026                 _set_timer(&preorder_ctrl->reordering_ctrl_timer, REORDER_WAIT_TIME);
2027
2028         spin_unlock_bh(&ppending_recvframe_queue->lock);
2029 }
2030
2031 static int process_recv_indicatepkts(struct adapter *padapter,
2032                                      struct recv_frame *prframe)
2033 {
2034         int retval = _SUCCESS;
2035         struct mlme_priv        *pmlmepriv = &padapter->mlmepriv;
2036         struct ht_priv  *phtpriv = &pmlmepriv->htpriv;
2037
2038         if (phtpriv->ht_option) {  /* B/G/N Mode */
2039                 if (recv_indicatepkt_reorder(padapter, prframe) != _SUCCESS) {
2040                         /*  including perform A-MPDU Rx Ordering Buffer Control */
2041                         if ((!padapter->bDriverStopped) &&
2042                             (!padapter->bSurpriseRemoved)) {
2043                                 retval = _FAIL;
2044                                 return retval;
2045                         }
2046                 }
2047         } else { /* B/G mode */
2048                 retval = wlanhdr_to_ethhdr (prframe);
2049                 if (retval != _SUCCESS) {
2050                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("wlanhdr_to_ethhdr: drop pkt\n"));
2051                         return retval;
2052                 }
2053
2054                 if ((!padapter->bDriverStopped) &&
2055                     (!padapter->bSurpriseRemoved)) {
2056                         /* indicate this recv_frame */
2057                         RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@ process_recv_indicatepkts- recv_func recv_indicatepkt\n"));
2058                         rtw_recv_indicatepkt(padapter, prframe);
2059                 } else {
2060                         RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@ process_recv_indicatepkts- recv_func free_indicatepkt\n"));
2061
2062                         RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("recv_func:bDriverStopped(%d) OR bSurpriseRemoved(%d)", padapter->bDriverStopped, padapter->bSurpriseRemoved));
2063                         retval = _FAIL;
2064                         return retval;
2065                 }
2066         }
2067
2068         return retval;
2069 }
2070
2071 static int recv_func_prehandle(struct adapter *padapter,
2072                                struct recv_frame *rframe)
2073 {
2074         int ret = _SUCCESS;
2075         struct rx_pkt_attrib *pattrib = &rframe->attrib;
2076         struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
2077         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2078
2079         if (padapter->registrypriv.mp_mode == 1) {
2080                 if ((check_fwstate(pmlmepriv, WIFI_MP_STATE) == true)) { /* padapter->mppriv.check_mp_pkt == 0)) */
2081                         if (pattrib->crc_err == 1)
2082                                 padapter->mppriv.rx_crcerrpktcount++;
2083                         else
2084                                 padapter->mppriv.rx_pktcount++;
2085
2086                         if (check_fwstate(pmlmepriv, WIFI_MP_LPBK_STATE) == false) {
2087                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_alert_, ("MP - Not in loopback mode , drop pkt\n"));
2088                                 ret = _FAIL;
2089                                 rtw_free_recvframe(rframe, pfree_recv_queue);/* free this recv_frame */
2090                                 goto exit;
2091                         }
2092                 }
2093         }
2094
2095         /* check the frame crtl field and decache */
2096         ret = validate_recv_frame(padapter, rframe);
2097         if (ret != _SUCCESS) {
2098                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("recv_func: validate_recv_frame fail! drop pkt\n"));
2099                 rtw_free_recvframe(rframe, pfree_recv_queue);/* free this recv_frame */
2100                 goto exit;
2101         }
2102
2103 exit:
2104         return ret;
2105 }
2106
2107 static int recv_func_posthandle(struct adapter *padapter,
2108                                 struct recv_frame *prframe)
2109 {
2110         int ret = _SUCCESS;
2111         struct recv_frame *orig_prframe = prframe;
2112         struct recv_priv *precvpriv = &padapter->recvpriv;
2113         struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
2114
2115         /*  DATA FRAME */
2116         rtw_led_control(padapter, LED_CTL_RX);
2117
2118         prframe = decryptor(padapter, prframe);
2119         if (prframe == NULL) {
2120                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("decryptor: drop pkt\n"));
2121                 ret = _FAIL;
2122                 goto _recv_data_drop;
2123         }
2124
2125         prframe = recvframe_chk_defrag(padapter, prframe);
2126         if (prframe == NULL) {
2127                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chk_defrag: drop pkt\n"));
2128                 goto _recv_data_drop;
2129         }
2130
2131         prframe = portctrl(padapter, prframe);
2132         if (prframe == NULL) {
2133                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("portctrl: drop pkt\n"));
2134                 ret = _FAIL;
2135                 goto _recv_data_drop;
2136         }
2137
2138         count_rx_stats(padapter, prframe, NULL);
2139
2140         ret = process_recv_indicatepkts(padapter, prframe);
2141         if (ret != _SUCCESS) {
2142                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recv_func: process_recv_indicatepkts fail!\n"));
2143                 rtw_free_recvframe(orig_prframe, pfree_recv_queue);/* free this recv_frame */
2144                 goto _recv_data_drop;
2145         }
2146         return ret;
2147
2148 _recv_data_drop:
2149         precvpriv->rx_drop++;
2150         return ret;
2151 }
2152
2153 static int recv_func(struct adapter *padapter, struct recv_frame *rframe)
2154 {
2155         int ret;
2156         struct rx_pkt_attrib *prxattrib = &rframe->attrib;
2157         struct security_priv *psecuritypriv = &padapter->securitypriv;
2158         struct mlme_priv *mlmepriv = &padapter->mlmepriv;
2159
2160         /* check if need to handle uc_swdec_pending_queue*/
2161         if (check_fwstate(mlmepriv, WIFI_STATION_STATE) && psecuritypriv->busetkipkey) {
2162                 struct recv_frame *pending_frame;
2163
2164                 while ((pending_frame = rtw_alloc_recvframe(&padapter->recvpriv.uc_swdec_pending_queue))) {
2165                         if (recv_func_posthandle(padapter, pending_frame) == _SUCCESS)
2166                                 DBG_88E("%s: dequeue uc_swdec_pending_queue\n", __func__);
2167                 }
2168         }
2169
2170         ret = recv_func_prehandle(padapter, rframe);
2171
2172         if (ret == _SUCCESS) {
2173                 /* check if need to enqueue into uc_swdec_pending_queue*/
2174                 if (check_fwstate(mlmepriv, WIFI_STATION_STATE) &&
2175                     !IS_MCAST(prxattrib->ra) && prxattrib->encrypt > 0 &&
2176                     (prxattrib->bdecrypted == 0 || psecuritypriv->sw_decrypt) &&
2177                     !is_wep_enc(psecuritypriv->dot11PrivacyAlgrthm) &&
2178                     !psecuritypriv->busetkipkey) {
2179                         rtw_enqueue_recvframe(rframe, &padapter->recvpriv.uc_swdec_pending_queue);
2180                         DBG_88E("%s: no key, enqueue uc_swdec_pending_queue\n", __func__);
2181                         goto exit;
2182                 }
2183
2184                 ret = recv_func_posthandle(padapter, rframe);
2185         }
2186
2187 exit:
2188         return ret;
2189 }
2190
2191 s32 rtw_recv_entry(struct recv_frame *precvframe)
2192 {
2193         struct adapter *padapter;
2194         struct recv_priv *precvpriv;
2195         s32 ret = _SUCCESS;
2196
2197
2198         padapter = precvframe->adapter;
2199
2200         precvpriv = &padapter->recvpriv;
2201
2202         ret = recv_func(padapter, precvframe);
2203         if (ret == _FAIL) {
2204                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("rtw_recv_entry: recv_func return fail!!!\n"));
2205                 goto _recv_entry_drop;
2206         }
2207
2208         precvpriv->rx_pkts++;
2209
2210
2211         return ret;
2212
2213 _recv_entry_drop:
2214
2215         if (padapter->registrypriv.mp_mode == 1)
2216                 padapter->mppriv.rx_pktloss = precvpriv->rx_drop;
2217
2218
2219         return ret;
2220 }
2221
2222 void rtw_signal_stat_timer_hdl(RTW_TIMER_HDL_ARGS)
2223 {
2224         struct adapter *adapter = (struct adapter *)FunctionContext;
2225         struct recv_priv *recvpriv = &adapter->recvpriv;
2226
2227         u32 tmp_s, tmp_q;
2228         u8 avg_signal_strength = 0;
2229         u8 avg_signal_qual = 0;
2230         u8 _alpha = 3; /*  this value is based on converging_constant = 5000 and sampling_interval = 1000 */
2231
2232         if (adapter->recvpriv.is_signal_dbg) {
2233                 /* update the user specific value, signal_strength_dbg, to signal_strength, rssi */
2234                 adapter->recvpriv.signal_strength = adapter->recvpriv.signal_strength_dbg;
2235                 adapter->recvpriv.rssi = (s8)translate_percentage_to_dbm((u8)adapter->recvpriv.signal_strength_dbg);
2236         } else {
2237                 if (recvpriv->signal_strength_data.update_req == 0) {/*  update_req is clear, means we got rx */
2238                         avg_signal_strength = recvpriv->signal_strength_data.avg_val;
2239                         /*  after avg_vals are acquired, we can re-stat the signal values */
2240                         recvpriv->signal_strength_data.update_req = 1;
2241                 }
2242
2243                 if (recvpriv->signal_qual_data.update_req == 0) {/*  update_req is clear, means we got rx */
2244                         avg_signal_qual = recvpriv->signal_qual_data.avg_val;
2245                         /*  after avg_vals are acquired, we can re-stat the signal values */
2246                         recvpriv->signal_qual_data.update_req = 1;
2247                 }
2248
2249                 /* update value of signal_strength, rssi, signal_qual */
2250                 if (check_fwstate(&adapter->mlmepriv, _FW_UNDER_SURVEY) == false) {
2251                         tmp_s = (avg_signal_strength+(_alpha-1)*recvpriv->signal_strength);
2252                         if (tmp_s % _alpha)
2253                                 tmp_s = tmp_s/_alpha + 1;
2254                         else
2255                                 tmp_s = tmp_s/_alpha;
2256                         if (tmp_s > 100)
2257                                 tmp_s = 100;
2258
2259                         tmp_q = (avg_signal_qual+(_alpha-1)*recvpriv->signal_qual);
2260                         if (tmp_q % _alpha)
2261                                 tmp_q = tmp_q/_alpha + 1;
2262                         else
2263                                 tmp_q = tmp_q/_alpha;
2264                         if (tmp_q > 100)
2265                                 tmp_q = 100;
2266
2267                         recvpriv->signal_strength = tmp_s;
2268                         recvpriv->rssi = (s8)translate_percentage_to_dbm(tmp_s);
2269                         recvpriv->signal_qual = tmp_q;
2270                 }
2271         }
2272         rtw_set_signal_stat_timer(recvpriv);
2273 }