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