Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / drivers / staging / rtl8723bs / core / rtw_recv.c
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #define _RTW_RECV_C_
8
9 #include <drv_types.h>
10 #include <rtw_debug.h>
11 #include <linux/jiffies.h>
12 #include <rtw_recv.h>
13
14 static u8 SNAP_ETH_TYPE_IPX[2] = {0x81, 0x37};
15 static u8 SNAP_ETH_TYPE_APPLETALK_AARP[2] = {0x80, 0xf3};
16
17 u8 rtw_rfc1042_header[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
18 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
19 u8 rtw_bridge_tunnel_header[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
20
21 static void rtw_signal_stat_timer_hdl(struct timer_list *t);
22
23 void _rtw_init_sta_recv_priv(struct sta_recv_priv *psta_recvpriv)
24 {
25         memset((u8 *)psta_recvpriv, 0, sizeof(struct sta_recv_priv));
26
27         spin_lock_init(&psta_recvpriv->lock);
28
29         /* for (i = 0; i<MAX_RX_NUMBLKS; i++) */
30         /*      _rtw_init_queue(&psta_recvpriv->blk_strms[i]); */
31
32         _rtw_init_queue(&psta_recvpriv->defrag_q);
33 }
34
35 sint _rtw_init_recv_priv(struct recv_priv *precvpriv, struct adapter *padapter)
36 {
37         sint i;
38         union recv_frame *precvframe;
39         sint    res = _SUCCESS;
40
41         spin_lock_init(&precvpriv->lock);
42
43         _rtw_init_queue(&precvpriv->free_recv_queue);
44         _rtw_init_queue(&precvpriv->recv_pending_queue);
45         _rtw_init_queue(&precvpriv->uc_swdec_pending_queue);
46
47         precvpriv->adapter = padapter;
48
49         precvpriv->free_recvframe_cnt = NR_RECVFRAME;
50
51         precvpriv->pallocated_frame_buf = vzalloc(NR_RECVFRAME * sizeof(union recv_frame) + RXFRAME_ALIGN_SZ);
52
53         if (!precvpriv->pallocated_frame_buf) {
54                 res = _FAIL;
55                 goto exit;
56         }
57
58         precvpriv->precv_frame_buf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(precvpriv->pallocated_frame_buf), RXFRAME_ALIGN_SZ);
59         /* precvpriv->precv_frame_buf = precvpriv->pallocated_frame_buf + RXFRAME_ALIGN_SZ - */
60         /*                                              ((SIZE_PTR) (precvpriv->pallocated_frame_buf) &(RXFRAME_ALIGN_SZ-1)); */
61
62         precvframe = (union recv_frame *) precvpriv->precv_frame_buf;
63
64
65         for (i = 0; i < NR_RECVFRAME; i++) {
66                 INIT_LIST_HEAD(&(precvframe->u.list));
67
68                 list_add_tail(&(precvframe->u.list), &(precvpriv->free_recv_queue.queue));
69
70                 rtw_os_recv_resource_alloc(padapter, precvframe);
71
72                 precvframe->u.hdr.len = 0;
73
74                 precvframe->u.hdr.adapter = padapter;
75                 precvframe++;
76
77         }
78
79         res = rtw_hal_init_recv_priv(padapter);
80
81         timer_setup(&precvpriv->signal_stat_timer, rtw_signal_stat_timer_hdl,
82                     0);
83
84         precvpriv->signal_stat_sampling_interval = 2000; /* ms */
85
86         rtw_set_signal_stat_timer(precvpriv);
87
88 exit:
89         return res;
90 }
91
92 void _rtw_free_recv_priv(struct recv_priv *precvpriv)
93 {
94         struct adapter  *padapter = precvpriv->adapter;
95
96         rtw_free_uc_swdec_pending_queue(padapter);
97
98         rtw_os_recv_resource_free(precvpriv);
99
100         if (precvpriv->pallocated_frame_buf)
101                 vfree(precvpriv->pallocated_frame_buf);
102
103         rtw_hal_free_recv_priv(padapter);
104 }
105
106 union recv_frame *_rtw_alloc_recvframe(struct __queue *pfree_recv_queue)
107 {
108
109         union recv_frame  *precvframe;
110         struct list_head        *plist, *phead;
111         struct adapter *padapter;
112         struct recv_priv *precvpriv;
113
114         if (list_empty(&pfree_recv_queue->queue))
115                 precvframe = NULL;
116         else {
117                 phead = get_list_head(pfree_recv_queue);
118
119                 plist = get_next(phead);
120
121                 precvframe = (union recv_frame *)plist;
122
123                 list_del_init(&precvframe->u.hdr.list);
124                 padapter = precvframe->u.hdr.adapter;
125                 if (padapter) {
126                         precvpriv = &padapter->recvpriv;
127                         if (pfree_recv_queue == &precvpriv->free_recv_queue)
128                                 precvpriv->free_recvframe_cnt--;
129                 }
130         }
131         return precvframe;
132 }
133
134 union recv_frame *rtw_alloc_recvframe(struct __queue *pfree_recv_queue)
135 {
136         union recv_frame  *precvframe;
137
138         spin_lock_bh(&pfree_recv_queue->lock);
139
140         precvframe = _rtw_alloc_recvframe(pfree_recv_queue);
141
142         spin_unlock_bh(&pfree_recv_queue->lock);
143
144         return precvframe;
145 }
146
147 int rtw_free_recvframe(union recv_frame *precvframe, struct __queue *pfree_recv_queue)
148 {
149         struct adapter *padapter = precvframe->u.hdr.adapter;
150         struct recv_priv *precvpriv = &padapter->recvpriv;
151
152         rtw_os_free_recvframe(precvframe);
153
154
155         spin_lock_bh(&pfree_recv_queue->lock);
156
157         list_del_init(&(precvframe->u.hdr.list));
158
159         precvframe->u.hdr.len = 0;
160
161         list_add_tail(&(precvframe->u.hdr.list), get_list_head(pfree_recv_queue));
162
163         if (padapter) {
164                 if (pfree_recv_queue == &precvpriv->free_recv_queue)
165                                 precvpriv->free_recvframe_cnt++;
166         }
167         spin_unlock_bh(&pfree_recv_queue->lock);
168         return _SUCCESS;
169 }
170
171
172
173
174 sint _rtw_enqueue_recvframe(union recv_frame *precvframe, struct __queue *queue)
175 {
176
177         struct adapter *padapter = precvframe->u.hdr.adapter;
178         struct recv_priv *precvpriv = &padapter->recvpriv;
179
180         /* INIT_LIST_HEAD(&(precvframe->u.hdr.list)); */
181         list_del_init(&(precvframe->u.hdr.list));
182
183
184         list_add_tail(&(precvframe->u.hdr.list), get_list_head(queue));
185
186         if (padapter)
187                 if (queue == &precvpriv->free_recv_queue)
188                         precvpriv->free_recvframe_cnt++;
189
190         return _SUCCESS;
191 }
192
193 sint rtw_enqueue_recvframe(union recv_frame *precvframe, struct __queue *queue)
194 {
195         sint ret;
196
197         /* _spinlock(&pfree_recv_queue->lock); */
198         spin_lock_bh(&queue->lock);
199         ret = _rtw_enqueue_recvframe(precvframe, queue);
200         /* spin_unlock(&pfree_recv_queue->lock); */
201         spin_unlock_bh(&queue->lock);
202
203         return ret;
204 }
205
206 /*
207 sint    rtw_enqueue_recvframe(union recv_frame *precvframe, struct __queue *queue)
208 {
209         return rtw_free_recvframe(precvframe, queue);
210 }
211 */
212
213
214
215
216 /*
217 caller : defrag ; recvframe_chk_defrag in recv_thread  (passive)
218 pframequeue: defrag_queue : will be accessed in recv_thread  (passive)
219
220 using spinlock to protect
221
222 */
223
224 void rtw_free_recvframe_queue(struct __queue *pframequeue,  struct __queue *pfree_recv_queue)
225 {
226         union   recv_frame      *precvframe;
227         struct list_head        *plist, *phead;
228
229         spin_lock(&pframequeue->lock);
230
231         phead = get_list_head(pframequeue);
232         plist = get_next(phead);
233
234         while (phead != plist) {
235                 precvframe = (union recv_frame *)plist;
236
237                 plist = get_next(plist);
238
239                 rtw_free_recvframe(precvframe, pfree_recv_queue);
240         }
241
242         spin_unlock(&pframequeue->lock);
243 }
244
245 u32 rtw_free_uc_swdec_pending_queue(struct adapter *adapter)
246 {
247         u32 cnt = 0;
248         union recv_frame *pending_frame;
249         while ((pending_frame = rtw_alloc_recvframe(&adapter->recvpriv.uc_swdec_pending_queue))) {
250                 rtw_free_recvframe(pending_frame, &adapter->recvpriv.free_recv_queue);
251                 cnt++;
252         }
253
254         if (cnt)
255                 DBG_871X(FUNC_ADPT_FMT" dequeue %d\n", FUNC_ADPT_ARG(adapter), cnt);
256
257         return cnt;
258 }
259
260
261 sint rtw_enqueue_recvbuf_to_head(struct recv_buf *precvbuf, struct __queue *queue)
262 {
263         spin_lock_bh(&queue->lock);
264
265         list_del_init(&precvbuf->list);
266         list_add(&precvbuf->list, get_list_head(queue));
267
268         spin_unlock_bh(&queue->lock);
269
270         return _SUCCESS;
271 }
272
273 sint rtw_enqueue_recvbuf(struct recv_buf *precvbuf, struct __queue *queue)
274 {
275         spin_lock_bh(&queue->lock);
276
277         list_del_init(&precvbuf->list);
278
279         list_add_tail(&precvbuf->list, get_list_head(queue));
280         spin_unlock_bh(&queue->lock);
281         return _SUCCESS;
282
283 }
284
285 struct recv_buf *rtw_dequeue_recvbuf(struct __queue *queue)
286 {
287         struct recv_buf *precvbuf;
288         struct list_head        *plist, *phead;
289
290         spin_lock_bh(&queue->lock);
291
292         if (list_empty(&queue->queue))
293                 precvbuf = NULL;
294         else {
295                 phead = get_list_head(queue);
296
297                 plist = get_next(phead);
298
299                 precvbuf = LIST_CONTAINOR(plist, struct recv_buf, list);
300
301                 list_del_init(&precvbuf->list);
302
303         }
304
305         spin_unlock_bh(&queue->lock);
306
307         return precvbuf;
308
309 }
310
311 sint recvframe_chkmic(struct adapter *adapter,  union recv_frame *precvframe);
312 sint recvframe_chkmic(struct adapter *adapter,  union recv_frame *precvframe)
313 {
314
315         sint    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         /* u8 *iv, rxdata_key_idx = 0; */
322         struct  sta_info        *stainfo;
323         struct  rx_pkt_attrib   *prxattrib = &precvframe->u.hdr.attrib;
324         struct  security_priv *psecuritypriv = &adapter->securitypriv;
325
326         struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv;
327         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
328
329         stainfo = rtw_get_stainfo(&adapter->stapriv, &prxattrib->ta[0]);
330
331         if (prxattrib->encrypt == _TKIP_) {
332                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n recvframe_chkmic:prxattrib->encrypt == _TKIP_\n"));
333                 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",
334                         prxattrib->ra[0], prxattrib->ra[1], prxattrib->ra[2], prxattrib->ra[3], prxattrib->ra[4], prxattrib->ra[5]));
335
336                 /* calculate mic code */
337                 if (stainfo) {
338                         if (IS_MCAST(prxattrib->ra)) {
339                                 /* mickey =&psecuritypriv->dot118021XGrprxmickey.skey[0]; */
340                                 /* iv = precvframe->u.hdr.rx_data+prxattrib->hdrlen; */
341                                 /* rxdata_key_idx =(((iv[3])>>6)&0x3) ; */
342                                 mickey = &psecuritypriv->dot118021XGrprxmickey[prxattrib->key_index].skey[0];
343
344                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n recvframe_chkmic: bcmc key\n"));
345                                 /* DBG_871X("\n recvframe_chkmic: bcmc key psecuritypriv->dot118021XGrpKeyid(%d), pmlmeinfo->key_index(%d) , recv key_id(%d)\n", */
346                                 /*                                                              psecuritypriv->dot118021XGrpKeyid, pmlmeinfo->key_index, rxdata_key_idx); */
347
348                                 if (psecuritypriv->binstallGrpkey == false) {
349                                         res = _FAIL;
350                                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n recvframe_chkmic:didn't install group key!!!!!!!!!!\n"));
351                                         DBG_871X("\n recvframe_chkmic:didn't install group key!!!!!!!!!!\n");
352                                         goto exit;
353                                 }
354                         } else {
355                                 mickey = &stainfo->dot11tkiprxmickey.skey[0];
356                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n recvframe_chkmic: unicast key\n"));
357                         }
358
359                         datalen = precvframe->u.hdr.len-prxattrib->hdrlen-prxattrib->iv_len-prxattrib->icv_len-8;/* icv_len included the mic code */
360                         pframe = precvframe->u.hdr.rx_data;
361                         payload = pframe+prxattrib->hdrlen+prxattrib->iv_len;
362
363                         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n prxattrib->iv_len =%d prxattrib->icv_len =%d\n", prxattrib->iv_len, prxattrib->icv_len));
364
365
366                         rtw_seccalctkipmic(mickey, pframe, payload, datalen, &miccode[0], (unsigned char)prxattrib->priority); /* care the length of the data */
367
368                         pframemic = payload+datalen;
369
370                         bmic_err = false;
371
372                         for (i = 0; i < 8; i++) {
373                                 if (miccode[i] != *(pframemic+i)) {
374                                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chkmic:miccode[%d](%02x) != *(pframemic+%d)(%02x) ", i, miccode[i], i, *(pframemic+i)));
375                                         bmic_err = true;
376                                 }
377                         }
378
379
380                         if (bmic_err == true) {
381
382                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n *(pframemic-8)-*(pframemic-1) = 0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
383                                         *(pframemic-8), *(pframemic-7), *(pframemic-6), *(pframemic-5), *(pframemic-4), *(pframemic-3), *(pframemic-2), *(pframemic-1)));
384                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n *(pframemic-16)-*(pframemic-9) = 0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
385                                         *(pframemic-16), *(pframemic-15), *(pframemic-14), *(pframemic-13), *(pframemic-12), *(pframemic-11), *(pframemic-10), *(pframemic-9)));
386
387                                 {
388                                         uint i;
389                                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n ======demp packet (len =%d) ======\n", precvframe->u.hdr.len));
390                                         for (i = 0; i < precvframe->u.hdr.len; i = i+8) {
391                                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x",
392                                                         *(precvframe->u.hdr.rx_data+i), *(precvframe->u.hdr.rx_data+i+1),
393                                                         *(precvframe->u.hdr.rx_data+i+2), *(precvframe->u.hdr.rx_data+i+3),
394                                                         *(precvframe->u.hdr.rx_data+i+4), *(precvframe->u.hdr.rx_data+i+5),
395                                                         *(precvframe->u.hdr.rx_data+i+6), *(precvframe->u.hdr.rx_data+i+7)));
396                                         }
397                                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n ======demp packet end [len =%d]======\n", precvframe->u.hdr.len));
398                                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("\n hrdlen =%d,\n", prxattrib->hdrlen));
399                                 }
400
401                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("ra = 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x psecuritypriv->binstallGrpkey =%d ",
402                                         prxattrib->ra[0], prxattrib->ra[1], prxattrib->ra[2],
403                                         prxattrib->ra[3], prxattrib->ra[4], prxattrib->ra[5], psecuritypriv->binstallGrpkey));
404
405                                 /*  double check key_index for some timing issue , */
406                                 /*  cannot compare with psecuritypriv->dot118021XGrpKeyid also cause timing issue */
407                                 if ((IS_MCAST(prxattrib->ra) == true)  && (prxattrib->key_index != pmlmeinfo->key_index))
408                                         brpt_micerror = false;
409
410                                 if ((prxattrib->bdecrypted == true) && (brpt_micerror == true)) {
411                                         rtw_handle_tkip_mic_err(adapter, (u8)IS_MCAST(prxattrib->ra));
412                                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" mic error :prxattrib->bdecrypted =%d ", prxattrib->bdecrypted));
413                                         DBG_871X(" mic error :prxattrib->bdecrypted =%d\n", prxattrib->bdecrypted);
414                                 } else {
415                                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" mic error :prxattrib->bdecrypted =%d ", prxattrib->bdecrypted));
416                                         DBG_871X(" mic error :prxattrib->bdecrypted =%d\n", prxattrib->bdecrypted);
417                                 }
418
419                                 res = _FAIL;
420
421                         } else {
422                                 /* mic checked ok */
423                                 if ((psecuritypriv->bcheck_grpkey == false) && (IS_MCAST(prxattrib->ra) == true)) {
424                                         psecuritypriv->bcheck_grpkey = true;
425                                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("psecuritypriv->bcheck_grpkey =true"));
426                                 }
427                         }
428
429                 } else
430                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chkmic: rtw_get_stainfo == NULL!!!\n"));
431
432                 recvframe_pull_tail(precvframe, 8);
433
434         }
435
436 exit:
437         return res;
438
439 }
440
441 /* decrypt and set the ivlen, icvlen of the recv_frame */
442 union recv_frame *decryptor(struct adapter *padapter, union recv_frame *precv_frame);
443 union recv_frame *decryptor(struct adapter *padapter, union recv_frame *precv_frame)
444 {
445
446         struct rx_pkt_attrib *prxattrib = &precv_frame->u.hdr.attrib;
447         struct security_priv *psecuritypriv = &padapter->securitypriv;
448         union recv_frame *return_packet = precv_frame;
449         u32  res = _SUCCESS;
450
451         DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt);
452
453         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("prxstat->decrypted =%x prxattrib->encrypt = 0x%03x\n", prxattrib->bdecrypted, prxattrib->encrypt));
454
455         if (prxattrib->encrypt > 0) {
456                 u8 *iv = precv_frame->u.hdr.rx_data+prxattrib->hdrlen;
457                 prxattrib->key_index = (((iv[3])>>6)&0x3);
458
459                 if (prxattrib->key_index > WEP_KEYS) {
460                         DBG_871X("prxattrib->key_index(%d) > WEP_KEYS\n", prxattrib->key_index);
461
462                         switch (prxattrib->encrypt) {
463                         case _WEP40_:
464                         case _WEP104_:
465                                 prxattrib->key_index = psecuritypriv->dot11PrivacyKeyIndex;
466                                 break;
467                         case _TKIP_:
468                         case _AES_:
469                         default:
470                                 prxattrib->key_index = psecuritypriv->dot118021XGrpKeyid;
471                                 break;
472                         }
473                 }
474         }
475
476         if ((prxattrib->encrypt > 0) && ((prxattrib->bdecrypted == 0) || (psecuritypriv->sw_decrypt == true))) {
477                 psecuritypriv->hw_decrypted = false;
478
479                 #ifdef DBG_RX_DECRYPTOR
480                 DBG_871X("[%s] %d:prxstat->bdecrypted:%d,  prxattrib->encrypt:%d,  Setting psecuritypriv->hw_decrypted = %d\n",
481                         __func__,
482                         __LINE__,
483                         prxattrib->bdecrypted,
484                         prxattrib->encrypt,
485                         psecuritypriv->hw_decrypted);
486                 #endif
487
488                 switch (prxattrib->encrypt) {
489                 case _WEP40_:
490                 case _WEP104_:
491                         DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_wep);
492                         rtw_wep_decrypt(padapter, (u8 *)precv_frame);
493                         break;
494                 case _TKIP_:
495                         DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_tkip);
496                         res = rtw_tkip_decrypt(padapter, (u8 *)precv_frame);
497                         break;
498                 case _AES_:
499                         DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_aes);
500                         res = rtw_aes_decrypt(padapter, (u8 *)precv_frame);
501                         break;
502                 default:
503                                 break;
504                 }
505         } else if (prxattrib->bdecrypted == 1
506                 && prxattrib->encrypt > 0
507                 && (psecuritypriv->busetkipkey == 1 || prxattrib->encrypt != _TKIP_)
508                 ) {
509                 DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_hw);
510
511                 psecuritypriv->hw_decrypted = true;
512                 #ifdef DBG_RX_DECRYPTOR
513                 DBG_871X("[%s] %d:prxstat->bdecrypted:%d,  prxattrib->encrypt:%d,  Setting psecuritypriv->hw_decrypted = %d\n",
514                         __func__,
515                         __LINE__,
516                         prxattrib->bdecrypted,
517                         prxattrib->encrypt,
518                         psecuritypriv->hw_decrypted);
519
520                 #endif
521         } else {
522                 DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_unknown);
523                 #ifdef DBG_RX_DECRYPTOR
524                 DBG_871X("[%s] %d:prxstat->bdecrypted:%d,  prxattrib->encrypt:%d,  Setting psecuritypriv->hw_decrypted = %d\n",
525                         __func__,
526                         __LINE__,
527                         prxattrib->bdecrypted,
528                         prxattrib->encrypt,
529                         psecuritypriv->hw_decrypted);
530                 #endif
531         }
532
533         if (res == _FAIL) {
534                 rtw_free_recvframe(return_packet, &padapter->recvpriv.free_recv_queue);
535                 return_packet = NULL;
536         } else
537                 prxattrib->bdecrypted = true;
538
539         return return_packet;
540 }
541
542 /* set the security information in the recv_frame */
543 union recv_frame *portctrl(struct adapter *adapter, union recv_frame *precv_frame);
544 union recv_frame *portctrl(struct adapter *adapter, union recv_frame *precv_frame)
545 {
546         u8 *psta_addr = NULL;
547         u8 *ptr;
548         uint  auth_alg;
549         struct recv_frame_hdr *pfhdr;
550         struct sta_info *psta;
551         struct sta_priv *pstapriv;
552         union recv_frame *prtnframe;
553         u16 ether_type = 0;
554         u16  eapol_type = 0x888e;/* for Funia BD's WPA issue */
555         struct rx_pkt_attrib *pattrib;
556
557         pstapriv = &adapter->stapriv;
558
559         auth_alg = adapter->securitypriv.dot11AuthAlgrthm;
560
561         ptr = get_recvframe_data(precv_frame);
562         pfhdr = &precv_frame->u.hdr;
563         pattrib = &pfhdr->attrib;
564         psta_addr = pattrib->ta;
565
566         prtnframe = NULL;
567
568         psta = rtw_get_stainfo(pstapriv, psta_addr);
569
570         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:adapter->securitypriv.dot11AuthAlgrthm =%d\n", adapter->securitypriv.dot11AuthAlgrthm));
571
572         if (auth_alg == 2) {
573                 if ((psta) && (psta->ieee8021x_blocked)) {
574                         __be16 be_tmp;
575
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_, ("portctrl:precv_frame->hdr.attrib.privacy =%x\n", precv_frame->u.hdr.attrib.privacy));
599
600                         if (pattrib->bdecrypted == 0)
601                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("portctrl:prxstat->decrypted =%x\n", pattrib->bdecrypted));
602
603                         prtnframe = precv_frame;
604                         /* check is the EAPOL frame or not (Rekey) */
605                         /* if (ether_type == eapol_type) { */
606                         /*      RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("########portctrl:ether_type == 0x888e\n")); */
607                                 /* check Rekey */
608
609                         /*      prtnframe =precv_frame; */
610                         /*  */
611                         /* else { */
612                         /*      RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("########portctrl:ether_type = 0x%04x\n", ether_type)); */
613                         /*  */
614                 }
615         } else
616                 prtnframe = precv_frame;
617
618         return prtnframe;
619 }
620
621 sint recv_decache(union recv_frame *precv_frame, u8 bretry, struct stainfo_rxcache *prxcache);
622 sint recv_decache(union recv_frame *precv_frame, u8 bretry, struct stainfo_rxcache *prxcache)
623 {
624         sint tid = precv_frame->u.hdr.attrib.priority;
625
626         u16 seq_ctrl = ((precv_frame->u.hdr.attrib.seq_num&0xffff) << 4) |
627                 (precv_frame->u.hdr.attrib.frag_num & 0xf);
628
629         if (tid > 15) {
630                 RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("recv_decache, (tid>15)! seq_ctrl = 0x%x, tid = 0x%x\n", seq_ctrl, tid));
631
632                 return _FAIL;
633         }
634
635         if (1) { /* if (bretry) */
636                 if (seq_ctrl == prxcache->tid_rxseq[tid]) {
637                         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]));
638
639                         return _FAIL;
640                 }
641         }
642
643         prxcache->tid_rxseq[tid] = seq_ctrl;
644
645         return _SUCCESS;
646
647 }
648
649 void process_pwrbit_data(struct adapter *padapter, union recv_frame *precv_frame);
650 void process_pwrbit_data(struct adapter *padapter, union recv_frame *precv_frame)
651 {
652         unsigned char pwrbit;
653         u8 *ptr = precv_frame->u.hdr.rx_data;
654         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
655         struct sta_priv *pstapriv = &padapter->stapriv;
656         struct sta_info *psta = NULL;
657
658         psta = rtw_get_stainfo(pstapriv, pattrib->src);
659
660         pwrbit = GetPwrMgt(ptr);
661
662         if (psta) {
663                 if (pwrbit) {
664                         if (!(psta->state & WIFI_SLEEP_STATE)) {
665                                 /* psta->state |= WIFI_SLEEP_STATE; */
666                                 /* pstapriv->sta_dz_bitmap |= BIT(psta->aid); */
667
668                                 stop_sta_xmit(padapter, psta);
669
670                                 /* DBG_871X("to sleep, sta_dz_bitmap =%x\n", pstapriv->sta_dz_bitmap); */
671                         }
672                 } else {
673                         if (psta->state & WIFI_SLEEP_STATE) {
674                                 /* psta->state ^= WIFI_SLEEP_STATE; */
675                                 /* pstapriv->sta_dz_bitmap &= ~BIT(psta->aid); */
676
677                                 wakeup_sta_to_xmit(padapter, psta);
678
679                                 /* DBG_871X("to wakeup, sta_dz_bitmap =%x\n", pstapriv->sta_dz_bitmap); */
680                         }
681                 }
682
683         }
684 }
685
686 void process_wmmps_data(struct adapter *padapter, union recv_frame *precv_frame);
687 void process_wmmps_data(struct adapter *padapter, union recv_frame *precv_frame)
688 {
689         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
690         struct sta_priv *pstapriv = &padapter->stapriv;
691         struct sta_info *psta = NULL;
692
693         psta = rtw_get_stainfo(pstapriv, pattrib->src);
694
695         if (!psta)
696                 return;
697
698         if (!psta->qos_option)
699                 return;
700
701         if (!(psta->qos_info&0xf))
702                 return;
703
704         if (psta->state&WIFI_SLEEP_STATE) {
705                 u8 wmmps_ac = 0;
706
707                 switch (pattrib->priority) {
708                 case 1:
709                 case 2:
710                         wmmps_ac = psta->uapsd_bk&BIT(1);
711                         break;
712                 case 4:
713                 case 5:
714                         wmmps_ac = psta->uapsd_vi&BIT(1);
715                         break;
716                 case 6:
717                 case 7:
718                         wmmps_ac = psta->uapsd_vo&BIT(1);
719                         break;
720                 case 0:
721                 case 3:
722                 default:
723                         wmmps_ac = psta->uapsd_be&BIT(1);
724                         break;
725                 }
726
727                 if (wmmps_ac) {
728                         if (psta->sleepq_ac_len > 0)
729                                 /* process received triggered frame */
730                                 xmit_delivery_enabled_frames(padapter, psta);
731                         else
732                                 /* issue one qos null frame with More data bit = 0 and the EOSP bit set (= 1) */
733                                 issue_qos_nulldata(padapter, psta->hwaddr, (u16)pattrib->priority, 0, 0);
734                 }
735         }
736 }
737
738 void count_rx_stats(struct adapter *padapter, union recv_frame *prframe, struct sta_info *sta);
739 void count_rx_stats(struct adapter *padapter, union recv_frame *prframe, struct sta_info *sta)
740 {
741         int     sz;
742         struct sta_info         *psta = NULL;
743         struct stainfo_stats    *pstats = NULL;
744         struct rx_pkt_attrib    *pattrib = &prframe->u.hdr.attrib;
745         struct recv_priv        *precvpriv = &padapter->recvpriv;
746
747         sz = get_recvframe_len(prframe);
748         precvpriv->rx_bytes += sz;
749
750         padapter->mlmepriv.LinkDetectInfo.NumRxOkInPeriod++;
751
752         if ((!MacAddr_isBcst(pattrib->dst)) && (!IS_MCAST(pattrib->dst))) {
753                 padapter->mlmepriv.LinkDetectInfo.NumRxUnicastOkInPeriod++;
754         }
755
756         if (sta)
757                 psta = sta;
758         else
759                 psta = prframe->u.hdr.psta;
760
761         if (psta) {
762                 pstats = &psta->sta_stats;
763
764                 pstats->rx_data_pkts++;
765                 pstats->rx_bytes += sz;
766         }
767
768         traffic_check_for_leave_lps(padapter, false, 0);
769 }
770
771 sint sta2sta_data_frame(
772         struct adapter *adapter,
773         union recv_frame *precv_frame,
774         struct sta_info **psta
775 );
776 sint sta2sta_data_frame(
777         struct adapter *adapter,
778         union recv_frame *precv_frame,
779         struct sta_info **psta
780 )
781 {
782         u8 *ptr = precv_frame->u.hdr.rx_data;
783         sint ret = _SUCCESS;
784         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
785         struct  sta_priv        *pstapriv = &adapter->stapriv;
786         struct  mlme_priv *pmlmepriv = &adapter->mlmepriv;
787         u8 *mybssid  = get_bssid(pmlmepriv);
788         u8 *myhwaddr = myid(&adapter->eeprompriv);
789         u8 *sta_addr = NULL;
790         sint bmcast = IS_MCAST(pattrib->dst);
791
792         /* DBG_871X("[%s] %d, seqnum:%d\n", __func__, __LINE__, pattrib->seq_num); */
793
794         if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true) ||
795                 (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true)) {
796
797                 /*  filter packets that SA is myself or multicast or broadcast */
798                 if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN)) {
799                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" SA ==myself\n"));
800                         ret = _FAIL;
801                         goto exit;
802                 }
803
804                 if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN))  && (!bmcast)) {
805                         ret = _FAIL;
806                         goto exit;
807                 }
808
809                 if (!memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
810                    !memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
811                    (memcmp(pattrib->bssid, mybssid, ETH_ALEN))) {
812                         ret = _FAIL;
813                         goto exit;
814                 }
815
816                 sta_addr = pattrib->src;
817
818         } else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true) {
819                 /*  For Station mode, sa and bssid should always be BSSID, and DA is my mac-address */
820                 if (memcmp(pattrib->bssid, pattrib->src, ETH_ALEN)) {
821                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("bssid != TA under STATION_MODE; drop pkt\n"));
822                         ret = _FAIL;
823                         goto exit;
824                 }
825
826                 sta_addr = pattrib->bssid;
827         } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
828                 if (bmcast) {
829                         /*  For AP mode, if DA == MCAST, then BSSID should be also MCAST */
830                         if (!IS_MCAST(pattrib->bssid)) {
831                                         ret = _FAIL;
832                                         goto exit;
833                         }
834                 } else { /*  not mc-frame */
835                         /*  For AP mode, if DA is non-MCAST, then it must be BSSID, and bssid == BSSID */
836                         if (memcmp(pattrib->bssid, pattrib->dst, ETH_ALEN)) {
837                                 ret = _FAIL;
838                                 goto exit;
839                         }
840
841                         sta_addr = pattrib->src;
842                 }
843
844         } else if (check_fwstate(pmlmepriv, WIFI_MP_STATE) == true) {
845                 memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
846                 memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
847                 memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
848                 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
849                 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
850
851                 sta_addr = mybssid;
852         } else
853                 ret  = _FAIL;
854
855
856
857         if (bmcast)
858                 *psta = rtw_get_bcmc_stainfo(adapter);
859         else
860                 *psta = rtw_get_stainfo(pstapriv, sta_addr); /*  get ap_info */
861
862         if (!*psta) {
863                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("can't get psta under sta2sta_data_frame ; drop pkt\n"));
864                 ret = _FAIL;
865                 goto exit;
866         }
867
868 exit:
869         return ret;
870 }
871
872 sint ap2sta_data_frame(
873         struct adapter *adapter,
874         union recv_frame *precv_frame,
875         struct sta_info **psta);
876 sint ap2sta_data_frame(
877         struct adapter *adapter,
878         union recv_frame *precv_frame,
879         struct sta_info **psta)
880 {
881         u8 *ptr = precv_frame->u.hdr.rx_data;
882         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
883         sint ret = _SUCCESS;
884         struct  sta_priv        *pstapriv = &adapter->stapriv;
885         struct  mlme_priv *pmlmepriv = &adapter->mlmepriv;
886         u8 *mybssid  = get_bssid(pmlmepriv);
887         u8 *myhwaddr = myid(&adapter->eeprompriv);
888         sint bmcast = IS_MCAST(pattrib->dst);
889
890         if ((check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true)
891                 && (check_fwstate(pmlmepriv, _FW_LINKED) == true
892                         || check_fwstate(pmlmepriv, _FW_UNDER_LINKING) == true)
893                 ) {
894
895                 /*  filter packets that SA is myself or multicast or broadcast */
896                 if (!memcmp(myhwaddr, pattrib->src, ETH_ALEN)) {
897                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" SA ==myself\n"));
898                         #ifdef DBG_RX_DROP_FRAME
899                         DBG_871X("DBG_RX_DROP_FRAME %s SA ="MAC_FMT", myhwaddr ="MAC_FMT"\n",
900                                 __func__, MAC_ARG(pattrib->src), MAC_ARG(myhwaddr));
901                         #endif
902                         ret = _FAIL;
903                         goto exit;
904                 }
905
906                 /*  da should be for me */
907                 if ((memcmp(myhwaddr, pattrib->dst, ETH_ALEN)) && (!bmcast)) {
908                         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_,
909                                 (" ap2sta_data_frame:  compare DA fail; DA ="MAC_FMT"\n", MAC_ARG(pattrib->dst)));
910                         #ifdef DBG_RX_DROP_FRAME
911                         DBG_871X("DBG_RX_DROP_FRAME %s DA ="MAC_FMT"\n", __func__, MAC_ARG(pattrib->dst));
912                         #endif
913                         ret = _FAIL;
914                         goto exit;
915                 }
916
917
918                 /*  check BSSID */
919                 if (!memcmp(pattrib->bssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
920                      !memcmp(mybssid, "\x0\x0\x0\x0\x0\x0", ETH_ALEN) ||
921                      (memcmp(pattrib->bssid, mybssid, ETH_ALEN))) {
922                         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_,
923                                 (" ap2sta_data_frame:  compare BSSID fail ; BSSID ="MAC_FMT"\n", MAC_ARG(pattrib->bssid)));
924                         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("mybssid ="MAC_FMT"\n", MAC_ARG(mybssid)));
925                         #ifdef DBG_RX_DROP_FRAME
926                         DBG_871X("DBG_RX_DROP_FRAME %s BSSID ="MAC_FMT", mybssid ="MAC_FMT"\n",
927                                 __func__, MAC_ARG(pattrib->bssid), MAC_ARG(mybssid));
928                         DBG_871X("this adapter = %d, buddy adapter = %d\n", adapter->adapter_type, adapter->pbuddystruct adapter->adapter_type);
929                         #endif
930
931                         if (!bmcast) {
932                                 DBG_871X("issue_deauth to the nonassociated ap =" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->bssid));
933                                 issue_deauth(adapter, pattrib->bssid, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
934                         }
935
936                         ret = _FAIL;
937                         goto exit;
938                 }
939
940                 if (bmcast)
941                         *psta = rtw_get_bcmc_stainfo(adapter);
942                 else
943                         *psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /*  get ap_info */
944
945                 if (!*psta) {
946                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("ap2sta: can't get psta under STATION_MODE ; drop pkt\n"));
947                         #ifdef DBG_RX_DROP_FRAME
948                         DBG_871X("DBG_RX_DROP_FRAME %s can't get psta under STATION_MODE ; drop pkt\n", __func__);
949                         #endif
950                         ret = _FAIL;
951                         goto exit;
952                 }
953
954                 if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) == WIFI_QOS_DATA_TYPE) {
955                 }
956
957                 if (GetFrameSubType(ptr) & BIT(6)) {
958                         /* No data, will not indicate to upper layer, temporily count it here */
959                         count_rx_stats(adapter, precv_frame, *psta);
960                         ret = RTW_RX_HANDLED;
961                         goto exit;
962                 }
963
964         } else if ((check_fwstate(pmlmepriv, WIFI_MP_STATE) == true) &&
965                      (check_fwstate(pmlmepriv, _FW_LINKED) == true)) {
966                 memcpy(pattrib->dst, GetAddr1Ptr(ptr), ETH_ALEN);
967                 memcpy(pattrib->src, GetAddr2Ptr(ptr), ETH_ALEN);
968                 memcpy(pattrib->bssid, GetAddr3Ptr(ptr), ETH_ALEN);
969                 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
970                 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
971
972                 /*  */
973                 memcpy(pattrib->bssid,  mybssid, ETH_ALEN);
974
975
976                 *psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /*  get sta_info */
977                 if (!*psta) {
978                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("can't get psta under MP_MODE ; drop pkt\n"));
979                         #ifdef DBG_RX_DROP_FRAME
980                         DBG_871X("DBG_RX_DROP_FRAME %s can't get psta under WIFI_MP_STATE ; drop pkt\n", __func__);
981                         #endif
982                         ret = _FAIL;
983                         goto exit;
984                 }
985
986
987         } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
988                 /* Special case */
989                 ret = RTW_RX_HANDLED;
990                 goto exit;
991         } else {
992                 if (!memcmp(myhwaddr, pattrib->dst, ETH_ALEN) && (!bmcast)) {
993                         *psta = rtw_get_stainfo(pstapriv, pattrib->bssid); /*  get sta_info */
994                         if (!*psta) {
995
996                                 /* for AP multicast issue , modify by yiwei */
997                                 static unsigned long send_issue_deauth_time;
998
999                                 /* DBG_871X("After send deauth , %u ms has elapsed.\n", jiffies_to_msecs(jiffies - send_issue_deauth_time)); */
1000
1001                                 if (jiffies_to_msecs(jiffies - send_issue_deauth_time) > 10000 || send_issue_deauth_time == 0) {
1002                                         send_issue_deauth_time = jiffies;
1003
1004                                         DBG_871X("issue_deauth to the ap =" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->bssid));
1005
1006                                         issue_deauth(adapter, pattrib->bssid, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1007                                 }
1008                         }
1009                 }
1010
1011                 ret = _FAIL;
1012                 #ifdef DBG_RX_DROP_FRAME
1013                 DBG_871X("DBG_RX_DROP_FRAME %s fw_state:0x%x\n", __func__, get_fwstate(pmlmepriv));
1014                 #endif
1015         }
1016
1017 exit:
1018         return ret;
1019 }
1020
1021 sint sta2ap_data_frame(
1022         struct adapter *adapter,
1023         union recv_frame *precv_frame,
1024         struct sta_info **psta);
1025 sint sta2ap_data_frame(
1026         struct adapter *adapter,
1027         union recv_frame *precv_frame,
1028         struct sta_info **psta)
1029 {
1030         u8 *ptr = precv_frame->u.hdr.rx_data;
1031         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1032         struct  sta_priv        *pstapriv = &adapter->stapriv;
1033         struct  mlme_priv *pmlmepriv = &adapter->mlmepriv;
1034         unsigned char *mybssid  = get_bssid(pmlmepriv);
1035         sint ret = _SUCCESS;
1036
1037         if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
1038                 /* For AP mode, RA =BSSID, TX =STA(SRC_ADDR), A3 =DST_ADDR */
1039                 if (memcmp(pattrib->bssid, mybssid, ETH_ALEN)) {
1040                         ret = _FAIL;
1041                         goto exit;
1042                 }
1043
1044                 *psta = rtw_get_stainfo(pstapriv, pattrib->src);
1045                 if (!*psta) {
1046                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("can't get psta under AP_MODE; drop pkt\n"));
1047                         DBG_871X("issue_deauth to sta =" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->src));
1048
1049                         issue_deauth(adapter, pattrib->src, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1050
1051                         ret = RTW_RX_HANDLED;
1052                         goto exit;
1053                 }
1054
1055                 process_pwrbit_data(adapter, precv_frame);
1056
1057                 if ((GetFrameSubType(ptr) & WIFI_QOS_DATA_TYPE) == WIFI_QOS_DATA_TYPE) {
1058                         process_wmmps_data(adapter, precv_frame);
1059                 }
1060
1061                 if (GetFrameSubType(ptr) & BIT(6)) {
1062                         /* No data, will not indicate to upper layer, temporily count it here */
1063                         count_rx_stats(adapter, precv_frame, *psta);
1064                         ret = RTW_RX_HANDLED;
1065                         goto exit;
1066                 }
1067         } else {
1068                 u8 *myhwaddr = myid(&adapter->eeprompriv);
1069                 if (memcmp(pattrib->ra, myhwaddr, ETH_ALEN)) {
1070                         ret = RTW_RX_HANDLED;
1071                         goto exit;
1072                 }
1073                 DBG_871X("issue_deauth to sta =" MAC_FMT " for the reason(7)\n", MAC_ARG(pattrib->src));
1074                 issue_deauth(adapter, pattrib->src, WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1075                 ret = RTW_RX_HANDLED;
1076                 goto exit;
1077         }
1078
1079 exit:
1080         return ret;
1081 }
1082
1083 sint validate_recv_ctrl_frame(struct adapter *padapter, union recv_frame *precv_frame);
1084 sint validate_recv_ctrl_frame(struct adapter *padapter, union recv_frame *precv_frame)
1085 {
1086         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1087         struct sta_priv *pstapriv = &padapter->stapriv;
1088         u8 *pframe = precv_frame->u.hdr.rx_data;
1089         struct sta_info *psta = NULL;
1090         /* uint len = precv_frame->u.hdr.len; */
1091
1092         /* DBG_871X("+validate_recv_ctrl_frame\n"); */
1093
1094         if (GetFrameType(pframe) != WIFI_CTRL_TYPE)
1095                 return _FAIL;
1096
1097         /* receive the frames that ra(a1) is my address */
1098         if (memcmp(GetAddr1Ptr(pframe), myid(&padapter->eeprompriv), ETH_ALEN))
1099                 return _FAIL;
1100
1101         psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
1102         if (!psta)
1103                 return _FAIL;
1104
1105         /* for rx pkt statistics */
1106         psta->sta_stats.rx_ctrl_pkts++;
1107
1108         /* only handle ps-poll */
1109         if (GetFrameSubType(pframe) == WIFI_PSPOLL) {
1110                 u16 aid;
1111                 u8 wmmps_ac = 0;
1112
1113                 aid = GetAid(pframe);
1114                 if (psta->aid != aid)
1115                         return _FAIL;
1116
1117                 switch (pattrib->priority) {
1118                 case 1:
1119                 case 2:
1120                         wmmps_ac = psta->uapsd_bk&BIT(0);
1121                         break;
1122                 case 4:
1123                 case 5:
1124                         wmmps_ac = psta->uapsd_vi&BIT(0);
1125                         break;
1126                 case 6:
1127                 case 7:
1128                         wmmps_ac = psta->uapsd_vo&BIT(0);
1129                         break;
1130                 case 0:
1131                 case 3:
1132                 default:
1133                         wmmps_ac = psta->uapsd_be&BIT(0);
1134                         break;
1135                 }
1136
1137                 if (wmmps_ac)
1138                         return _FAIL;
1139
1140                 if (psta->state & WIFI_STA_ALIVE_CHK_STATE) {
1141                         DBG_871X("%s alive check-rx ps-poll\n", __func__);
1142                         psta->expire_to = pstapriv->expire_to;
1143                         psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
1144                 }
1145
1146                 if ((psta->state&WIFI_SLEEP_STATE) && (pstapriv->sta_dz_bitmap&BIT(psta->aid))) {
1147                         struct list_head        *xmitframe_plist, *xmitframe_phead;
1148                         struct xmit_frame *pxmitframe = NULL;
1149                         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1150
1151                         /* spin_lock_bh(&psta->sleep_q.lock); */
1152                         spin_lock_bh(&pxmitpriv->lock);
1153
1154                         xmitframe_phead = get_list_head(&psta->sleep_q);
1155                         xmitframe_plist = get_next(xmitframe_phead);
1156
1157                         if (xmitframe_phead != xmitframe_plist) {
1158                                 pxmitframe = LIST_CONTAINOR(xmitframe_plist, struct xmit_frame, list);
1159
1160                                 xmitframe_plist = get_next(xmitframe_plist);
1161
1162                                 list_del_init(&pxmitframe->list);
1163
1164                                 psta->sleepq_len--;
1165
1166                                 if (psta->sleepq_len > 0)
1167                                         pxmitframe->attrib.mdata = 1;
1168                                 else
1169                                         pxmitframe->attrib.mdata = 0;
1170
1171                                 pxmitframe->attrib.triggered = 1;
1172
1173                                 /* DBG_871X("handling ps-poll, q_len =%d, tim =%x\n", psta->sleepq_len, pstapriv->tim_bitmap); */
1174
1175                                 rtw_hal_xmitframe_enqueue(padapter, pxmitframe);
1176
1177                                 if (psta->sleepq_len == 0) {
1178                                         pstapriv->tim_bitmap &= ~BIT(psta->aid);
1179
1180                                         /* DBG_871X("after handling ps-poll, tim =%x\n", pstapriv->tim_bitmap); */
1181
1182                                         /* upate BCN for TIM IE */
1183                                         /* update_BCNTIM(padapter); */
1184                                         update_beacon(padapter, _TIM_IE_, NULL, true);
1185                                 }
1186
1187                                 /* spin_unlock_bh(&psta->sleep_q.lock); */
1188                                 spin_unlock_bh(&pxmitpriv->lock);
1189
1190                         } else {
1191                                 /* spin_unlock_bh(&psta->sleep_q.lock); */
1192                                 spin_unlock_bh(&pxmitpriv->lock);
1193
1194                                 /* DBG_871X("no buffered packets to xmit\n"); */
1195                                 if (pstapriv->tim_bitmap&BIT(psta->aid)) {
1196                                         if (psta->sleepq_len == 0) {
1197                                                 DBG_871X("no buffered packets to xmit\n");
1198
1199                                                 /* issue nulldata with More data bit = 0 to indicate we have no buffered packets */
1200                                                 issue_nulldata_in_interrupt(padapter, psta->hwaddr);
1201                                         } else {
1202                                                 DBG_871X("error!psta->sleepq_len =%d\n", psta->sleepq_len);
1203                                                 psta->sleepq_len = 0;
1204                                         }
1205
1206                                         pstapriv->tim_bitmap &= ~BIT(psta->aid);
1207
1208                                         /* upate BCN for TIM IE */
1209                                         /* update_BCNTIM(padapter); */
1210                                         update_beacon(padapter, _TIM_IE_, NULL, true);
1211                                 }
1212                         }
1213                 }
1214         }
1215
1216         return _FAIL;
1217
1218 }
1219
1220 union recv_frame *recvframe_chk_defrag(struct adapter *padapter, union recv_frame *precv_frame);
1221 sint validate_recv_mgnt_frame(struct adapter *padapter, union recv_frame *precv_frame);
1222 sint validate_recv_mgnt_frame(struct adapter *padapter, union recv_frame *precv_frame)
1223 {
1224         /* struct mlme_priv *pmlmepriv = &adapter->mlmepriv; */
1225
1226         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("+validate_recv_mgnt_frame\n"));
1227
1228         precv_frame = recvframe_chk_defrag(padapter, precv_frame);
1229         if (!precv_frame) {
1230                 RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("%s: fragment packet\n", __func__));
1231                 return _SUCCESS;
1232         }
1233
1234         {
1235                 /* for rx pkt statistics */
1236                 struct sta_info *psta = rtw_get_stainfo(&padapter->stapriv, GetAddr2Ptr(precv_frame->u.hdr.rx_data));
1237                 if (psta) {
1238                         psta->sta_stats.rx_mgnt_pkts++;
1239                         if (GetFrameSubType(precv_frame->u.hdr.rx_data) == WIFI_BEACON)
1240                                 psta->sta_stats.rx_beacon_pkts++;
1241                         else if (GetFrameSubType(precv_frame->u.hdr.rx_data) == WIFI_PROBEREQ)
1242                                 psta->sta_stats.rx_probereq_pkts++;
1243                         else if (GetFrameSubType(precv_frame->u.hdr.rx_data) == WIFI_PROBERSP) {
1244                                 if (!memcmp(padapter->eeprompriv.mac_addr, GetAddr1Ptr(precv_frame->u.hdr.rx_data), ETH_ALEN))
1245                                         psta->sta_stats.rx_probersp_pkts++;
1246                                 else if (is_broadcast_mac_addr(GetAddr1Ptr(precv_frame->u.hdr.rx_data))
1247                                         || is_multicast_mac_addr(GetAddr1Ptr(precv_frame->u.hdr.rx_data)))
1248                                         psta->sta_stats.rx_probersp_bm_pkts++;
1249                                 else
1250                                         psta->sta_stats.rx_probersp_uo_pkts++;
1251                         }
1252                 }
1253         }
1254
1255         mgt_dispatcher(padapter, precv_frame);
1256
1257         return _SUCCESS;
1258
1259 }
1260
1261 sint validate_recv_data_frame(struct adapter *adapter, union recv_frame *precv_frame);
1262 sint validate_recv_data_frame(struct adapter *adapter, union recv_frame *precv_frame)
1263 {
1264         u8 bretry;
1265         u8 *psa, *pda, *pbssid;
1266         struct sta_info *psta = NULL;
1267         u8 *ptr = precv_frame->u.hdr.rx_data;
1268         struct rx_pkt_attrib    *pattrib = &precv_frame->u.hdr.attrib;
1269         struct security_priv *psecuritypriv = &adapter->securitypriv;
1270         sint ret = _SUCCESS;
1271
1272         bretry = GetRetry(ptr);
1273         pda = get_da(ptr);
1274         psa = get_sa(ptr);
1275         pbssid = get_hdr_bssid(ptr);
1276
1277         if (!pbssid) {
1278                 #ifdef DBG_RX_DROP_FRAME
1279                 DBG_871X("DBG_RX_DROP_FRAME %s pbssid == NULL\n", __func__);
1280                 #endif
1281                 ret = _FAIL;
1282                 goto exit;
1283         }
1284
1285         memcpy(pattrib->dst, pda, ETH_ALEN);
1286         memcpy(pattrib->src, psa, ETH_ALEN);
1287
1288         memcpy(pattrib->bssid, pbssid, ETH_ALEN);
1289
1290         switch (pattrib->to_fr_ds) {
1291         case 0:
1292                 memcpy(pattrib->ra, pda, ETH_ALEN);
1293                 memcpy(pattrib->ta, psa, ETH_ALEN);
1294                 ret = sta2sta_data_frame(adapter, precv_frame, &psta);
1295                 break;
1296
1297         case 1:
1298                 memcpy(pattrib->ra, pda, ETH_ALEN);
1299                 memcpy(pattrib->ta, pbssid, ETH_ALEN);
1300                 ret = ap2sta_data_frame(adapter, precv_frame, &psta);
1301                 break;
1302
1303         case 2:
1304                 memcpy(pattrib->ra, pbssid, ETH_ALEN);
1305                 memcpy(pattrib->ta, psa, ETH_ALEN);
1306                 ret = sta2ap_data_frame(adapter, precv_frame, &psta);
1307                 break;
1308
1309         case 3:
1310                 memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
1311                 memcpy(pattrib->ta, GetAddr2Ptr(ptr), ETH_ALEN);
1312                 ret = _FAIL;
1313                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" case 3\n"));
1314                 break;
1315
1316         default:
1317                 ret = _FAIL;
1318                 break;
1319
1320         }
1321
1322         if (ret == _FAIL) {
1323                 #ifdef DBG_RX_DROP_FRAME
1324                 DBG_871X("DBG_RX_DROP_FRAME %s case:%d, res:%d\n", __func__, pattrib->to_fr_ds, ret);
1325                 #endif
1326                 goto exit;
1327         } else if (ret == RTW_RX_HANDLED) {
1328                 goto exit;
1329         }
1330
1331
1332         if (!psta) {
1333                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, (" after to_fr_ds_chk; psta == NULL\n"));
1334                 #ifdef DBG_RX_DROP_FRAME
1335                 DBG_871X("DBG_RX_DROP_FRAME %s psta == NULL\n", __func__);
1336                 #endif
1337                 ret = _FAIL;
1338                 goto exit;
1339         }
1340
1341         /* psta->rssi = prxcmd->rssi; */
1342         /* psta->signal_quality = prxcmd->sq; */
1343         precv_frame->u.hdr.psta = psta;
1344
1345
1346         pattrib->amsdu = 0;
1347         pattrib->ack_policy = 0;
1348         /* parsing QC field */
1349         if (pattrib->qos == 1) {
1350                 pattrib->priority = GetPriority((ptr + 24));
1351                 pattrib->ack_policy = GetAckpolicy((ptr + 24));
1352                 pattrib->amsdu = GetAMsdu((ptr + 24));
1353                 pattrib->hdrlen = pattrib->to_fr_ds == 3 ? 32 : 26;
1354
1355                 if (pattrib->priority != 0 && pattrib->priority != 3)
1356                         adapter->recvpriv.bIsAnyNonBEPkts = true;
1357
1358         } else {
1359                 pattrib->priority = 0;
1360                 pattrib->hdrlen = pattrib->to_fr_ds == 3 ? 30 : 24;
1361         }
1362
1363
1364         if (pattrib->order)/* HT-CTRL 11n */
1365                 pattrib->hdrlen += 4;
1366
1367         precv_frame->u.hdr.preorder_ctrl = &psta->recvreorder_ctrl[pattrib->priority];
1368
1369         /*  decache, drop duplicate recv packets */
1370         if (recv_decache(precv_frame, bretry, &psta->sta_recvpriv.rxcache) == _FAIL) {
1371                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("decache : drop pkt\n"));
1372                 #ifdef DBG_RX_DROP_FRAME
1373                 DBG_871X("DBG_RX_DROP_FRAME %s recv_decache return _FAIL\n", __func__);
1374                 #endif
1375                 ret = _FAIL;
1376                 goto exit;
1377         }
1378
1379         if (pattrib->privacy) {
1380
1381                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("validate_recv_data_frame:pattrib->privacy =%x\n", pattrib->privacy));
1382                 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)));
1383
1384                 GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt, IS_MCAST(pattrib->ra));
1385
1386                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n pattrib->encrypt =%d\n", pattrib->encrypt));
1387
1388                 SET_ICE_IV_LEN(pattrib->iv_len, pattrib->icv_len, pattrib->encrypt);
1389         } else {
1390                 pattrib->encrypt = 0;
1391                 pattrib->iv_len = pattrib->icv_len = 0;
1392         }
1393
1394 exit:
1395         return ret;
1396 }
1397
1398 static sint validate_80211w_mgmt(struct adapter *adapter, union recv_frame *precv_frame)
1399 {
1400         struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1401         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1402         u8 *ptr = precv_frame->u.hdr.rx_data;
1403         u8 type;
1404         u8 subtype;
1405
1406         type =  GetFrameType(ptr);
1407         subtype = GetFrameSubType(ptr); /* bit(7)~bit(2) */
1408
1409         /* only support station mode */
1410         if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) && check_fwstate(pmlmepriv, _FW_LINKED)
1411                 && adapter->securitypriv.binstallBIPkey == true) {
1412                 /* unicast management frame decrypt */
1413                 if (pattrib->privacy && !(IS_MCAST(GetAddr1Ptr(ptr))) &&
1414                         (subtype == WIFI_DEAUTH || subtype == WIFI_DISASSOC || subtype == WIFI_ACTION)) {
1415                         u8 *ppp, *mgmt_DATA;
1416                         u32 data_len = 0;
1417                         ppp = GetAddr2Ptr(ptr);
1418
1419                         pattrib->bdecrypted = 0;
1420                         pattrib->encrypt = _AES_;
1421                         pattrib->hdrlen = sizeof(struct ieee80211_hdr_3addr);
1422                         /* set iv and icv length */
1423                         SET_ICE_IV_LEN(pattrib->iv_len, pattrib->icv_len, pattrib->encrypt);
1424                         memcpy(pattrib->ra, GetAddr1Ptr(ptr), ETH_ALEN);
1425                         memcpy(pattrib->ta, GetAddr2Ptr(ptr), ETH_ALEN);
1426                         /* actual management data frame body */
1427                         data_len = pattrib->pkt_len - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len;
1428                         mgmt_DATA = rtw_zmalloc(data_len);
1429                         if (!mgmt_DATA) {
1430                                 DBG_871X("%s mgmt allocate fail  !!!!!!!!!\n", __func__);
1431                                 goto validate_80211w_fail;
1432                         }
1433                         precv_frame = decryptor(adapter, precv_frame);
1434                         /* save actual management data frame body */
1435                         memcpy(mgmt_DATA, ptr+pattrib->hdrlen+pattrib->iv_len, data_len);
1436                         /* overwrite the iv field */
1437                         memcpy(ptr+pattrib->hdrlen, mgmt_DATA, data_len);
1438                         /* remove the iv and icv length */
1439                         pattrib->pkt_len = pattrib->pkt_len - pattrib->iv_len - pattrib->icv_len;
1440                         kfree(mgmt_DATA);
1441                         if (!precv_frame) {
1442                                 DBG_871X("%s mgmt descrypt fail  !!!!!!!!!\n", __func__);
1443                                 goto validate_80211w_fail;
1444                         }
1445                 } else if (IS_MCAST(GetAddr1Ptr(ptr)) &&
1446                         (subtype == WIFI_DEAUTH || subtype == WIFI_DISASSOC)) {
1447                         sint BIP_ret = _SUCCESS;
1448                         /* verify BIP MME IE of broadcast/multicast de-auth/disassoc packet */
1449                         BIP_ret = rtw_BIP_verify(adapter, (u8 *)precv_frame);
1450                         if (BIP_ret == _FAIL) {
1451                                 /* DBG_871X("802.11w BIP verify fail\n"); */
1452                                 goto validate_80211w_fail;
1453                         } else if (BIP_ret == RTW_RX_HANDLED) {
1454                                 /* DBG_871X("802.11w recv none protected packet\n"); */
1455                                 /* issue sa query request */
1456                                 issue_action_SA_Query(adapter, NULL, 0, 0);
1457                                 goto validate_80211w_fail;
1458                         }
1459                 } else { /* 802.11w protect */
1460                         if (subtype == WIFI_ACTION) {
1461                                 /* according 802.11-2012 standard, these five types are not robust types */
1462                                 if (ptr[WLAN_HDR_A3_LEN] != RTW_WLAN_CATEGORY_PUBLIC          &&
1463                                         ptr[WLAN_HDR_A3_LEN] != RTW_WLAN_CATEGORY_HT              &&
1464                                         ptr[WLAN_HDR_A3_LEN] != RTW_WLAN_CATEGORY_UNPROTECTED_WNM &&
1465                                         ptr[WLAN_HDR_A3_LEN] != RTW_WLAN_CATEGORY_SELF_PROTECTED  &&
1466                                         ptr[WLAN_HDR_A3_LEN] != RTW_WLAN_CATEGORY_P2P) {
1467                                         DBG_871X("action frame category =%d should robust\n", ptr[WLAN_HDR_A3_LEN]);
1468                                         goto validate_80211w_fail;
1469                                 }
1470                         } else if (subtype == WIFI_DEAUTH || subtype == WIFI_DISASSOC) {
1471                                 DBG_871X("802.11w recv none protected packet\n");
1472                                 /* issue sa query request */
1473                                 issue_action_SA_Query(adapter, NULL, 0, 0);
1474                                 goto validate_80211w_fail;
1475                         }
1476                 }
1477         }
1478         return _SUCCESS;
1479
1480 validate_80211w_fail:
1481         return _FAIL;
1482
1483 }
1484
1485 static inline void dump_rx_packet(u8 *ptr)
1486 {
1487         int i;
1488
1489         DBG_871X("#############################\n");
1490         for (i = 0; i < 64; i = i+8)
1491                 DBG_871X("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:\n", *(ptr+i),
1492                 *(ptr+i+1), *(ptr+i+2), *(ptr+i+3), *(ptr+i+4), *(ptr+i+5), *(ptr+i+6), *(ptr+i+7));
1493         DBG_871X("#############################\n");
1494 }
1495
1496 sint validate_recv_frame(struct adapter *adapter, union recv_frame *precv_frame);
1497 sint validate_recv_frame(struct adapter *adapter, union recv_frame *precv_frame)
1498 {
1499         /* shall check frame subtype, to / from ds, da, bssid */
1500
1501         /* then call check if rx seq/frag. duplicated. */
1502
1503         u8 type;
1504         u8 subtype;
1505         sint retval = _SUCCESS;
1506         u8 bDumpRxPkt;
1507
1508         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
1509
1510         u8 *ptr = precv_frame->u.hdr.rx_data;
1511         u8  ver = (unsigned char) (*ptr)&0x3;
1512
1513         /* add version chk */
1514         if (ver != 0) {
1515                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_data_frame fail! (ver!= 0)\n"));
1516                 retval = _FAIL;
1517                 DBG_COUNTER(adapter->rx_logs.core_rx_pre_ver_err);
1518                 goto exit;
1519         }
1520
1521         type =  GetFrameType(ptr);
1522         subtype = GetFrameSubType(ptr); /* bit(7)~bit(2) */
1523
1524         pattrib->to_fr_ds = get_tofr_ds(ptr);
1525
1526         pattrib->frag_num = GetFragNum(ptr);
1527         pattrib->seq_num = GetSequence(ptr);
1528
1529         pattrib->pw_save = GetPwrMgt(ptr);
1530         pattrib->mfrag = GetMFrag(ptr);
1531         pattrib->mdata = GetMData(ptr);
1532         pattrib->privacy = GetPrivacy(ptr);
1533         pattrib->order = GetOrder(ptr);
1534         rtw_hal_get_def_var(adapter, HAL_DEF_DBG_DUMP_RXPKT, &(bDumpRxPkt));
1535         if (bDumpRxPkt == 1) /* dump all rx packets */
1536                 dump_rx_packet(ptr);
1537         else if ((bDumpRxPkt == 2) && (type == WIFI_MGT_TYPE))
1538                 dump_rx_packet(ptr);
1539         else if ((bDumpRxPkt == 3) && (type == WIFI_DATA_TYPE))
1540                 dump_rx_packet(ptr);
1541
1542         switch (type) {
1543         case WIFI_MGT_TYPE: /* mgnt */
1544                 DBG_COUNTER(adapter->rx_logs.core_rx_pre_mgmt);
1545                 if (validate_80211w_mgmt(adapter, precv_frame) == _FAIL) {
1546                         retval = _FAIL;
1547                         DBG_COUNTER(padapter->rx_logs.core_rx_pre_mgmt_err_80211w);
1548                         break;
1549                 }
1550
1551                 retval = validate_recv_mgnt_frame(adapter, precv_frame);
1552                 if (retval == _FAIL) {
1553                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_mgnt_frame fail\n"));
1554                         DBG_COUNTER(adapter->rx_logs.core_rx_pre_mgmt_err);
1555                 }
1556                 retval = _FAIL; /*  only data frame return _SUCCESS */
1557                 break;
1558         case WIFI_CTRL_TYPE: /* ctrl */
1559                 DBG_COUNTER(adapter->rx_logs.core_rx_pre_ctrl);
1560                 retval = validate_recv_ctrl_frame(adapter, precv_frame);
1561                 if (retval == _FAIL) {
1562                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_ctrl_frame fail\n"));
1563                         DBG_COUNTER(adapter->rx_logs.core_rx_pre_ctrl_err);
1564                 }
1565                 retval = _FAIL; /*  only data frame return _SUCCESS */
1566                 break;
1567         case WIFI_DATA_TYPE: /* data */
1568                 DBG_COUNTER(adapter->rx_logs.core_rx_pre_data);
1569
1570                 pattrib->qos = (subtype & BIT(7)) ? 1:0;
1571                 retval = validate_recv_data_frame(adapter, precv_frame);
1572                 if (retval == _FAIL) {
1573                         struct recv_priv *precvpriv = &adapter->recvpriv;
1574                         /* RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_data_frame fail\n")); */
1575                         precvpriv->rx_drop++;
1576                         DBG_COUNTER(adapter->rx_logs.core_rx_pre_data_err);
1577                 } else if (retval == _SUCCESS) {
1578 #ifdef DBG_RX_DUMP_EAP
1579                         u8 bDumpRxPkt;
1580                         u16 eth_type;
1581
1582                         /*  dump eapol */
1583                         rtw_hal_get_def_var(adapter, HAL_DEF_DBG_DUMP_RXPKT, &(bDumpRxPkt));
1584                         /*  get ether_type */
1585                         memcpy(&eth_type, ptr + pattrib->hdrlen + pattrib->iv_len + LLC_HEADER_SIZE, 2);
1586                         eth_type = ntohs((unsigned short) eth_type);
1587                         if ((bDumpRxPkt == 4) && (eth_type == 0x888e))
1588                                 dump_rx_packet(ptr);
1589 #endif
1590                 } else
1591                         DBG_COUNTER(adapter->rx_logs.core_rx_pre_data_handled);
1592                 break;
1593         default:
1594                 DBG_COUNTER(adapter->rx_logs.core_rx_pre_unknown);
1595                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("validate_recv_data_frame fail! type = 0x%x\n", type));
1596                 #ifdef DBG_RX_DROP_FRAME
1597                 DBG_871X("DBG_RX_DROP_FRAME validate_recv_data_frame fail! type = 0x%x\n", type);
1598                 #endif
1599                 retval = _FAIL;
1600                 break;
1601         }
1602
1603 exit:
1604         return retval;
1605 }
1606
1607
1608 /* remove the wlanhdr and add the eth_hdr */
1609 sint wlanhdr_to_ethhdr(union recv_frame *precvframe);
1610 sint wlanhdr_to_ethhdr(union recv_frame *precvframe)
1611 {
1612         sint    rmv_len;
1613         u16 eth_type, len;
1614         u8 bsnaphdr;
1615         u8 *psnap_type;
1616         struct ieee80211_snap_hdr       *psnap;
1617         __be16 be_tmp;
1618         struct adapter                  *adapter = precvframe->u.hdr.adapter;
1619         struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1620         u8 *ptr = get_recvframe_data(precvframe) ; /*  point to frame_ctrl field */
1621         struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
1622
1623         if (pattrib->encrypt) {
1624                 recvframe_pull_tail(precvframe, pattrib->icv_len);
1625         }
1626
1627         psnap = (struct ieee80211_snap_hdr      *)(ptr+pattrib->hdrlen + pattrib->iv_len);
1628         psnap_type = ptr+pattrib->hdrlen + pattrib->iv_len+SNAP_SIZE;
1629         /* convert hdr + possible LLC headers into Ethernet header */
1630         /* eth_type = (psnap_type[0] << 8) | psnap_type[1]; */
1631         if ((!memcmp(psnap, rtw_rfc1042_header, SNAP_SIZE) &&
1632                 (memcmp(psnap_type, SNAP_ETH_TYPE_IPX, 2)) &&
1633                 (memcmp(psnap_type, SNAP_ETH_TYPE_APPLETALK_AARP, 2))) ||
1634                 /* eth_type != ETH_P_AARP && eth_type != ETH_P_IPX) || */
1635                  !memcmp(psnap, rtw_bridge_tunnel_header, SNAP_SIZE)) {
1636                 /* remove RFC1042 or Bridge-Tunnel encapsulation and replace EtherType */
1637                 bsnaphdr = true;
1638         } else
1639                 /* Leave Ethernet header part of hdr and full payload */
1640                 bsnaphdr = false;
1641
1642         rmv_len = pattrib->hdrlen + pattrib->iv_len + (bsnaphdr?SNAP_SIZE:0);
1643         len = precvframe->u.hdr.len - rmv_len;
1644
1645         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("\n ===pattrib->hdrlen: %x,  pattrib->iv_len:%x ===\n\n", pattrib->hdrlen,  pattrib->iv_len));
1646
1647         memcpy(&be_tmp, ptr+rmv_len, 2);
1648         eth_type = ntohs(be_tmp); /* pattrib->ether_type */
1649         pattrib->eth_type = eth_type;
1650
1651 #ifdef CONFIG_AUTO_AP_MODE
1652         if (0x8899 == pattrib->eth_type) {
1653                 struct sta_info *psta = precvframe->u.hdr.psta;
1654
1655                 DBG_871X("wlan rx: got eth_type = 0x%x\n", pattrib->eth_type);
1656
1657                 if (psta && psta->isrc && psta->pid > 0) {
1658                         u16 rx_pid;
1659
1660                         rx_pid = *(u16 *)(ptr+rmv_len+2);
1661
1662                         DBG_871X("wlan rx(pid = 0x%x): sta("MAC_FMT") pid = 0x%x\n",
1663                                 rx_pid, MAC_ARG(psta->hwaddr), psta->pid);
1664
1665                         if (rx_pid == psta->pid) {
1666                                 int i;
1667                                 u16 len = *(u16 *)(ptr+rmv_len+4);
1668                                 /* u16 ctrl_type = *(u16*)(ptr+rmv_len+6); */
1669
1670                                 /* DBG_871X("RC: len = 0x%x, ctrl_type = 0x%x\n", len, ctrl_type); */
1671                                 DBG_871X("RC: len = 0x%x\n", len);
1672
1673                                 for (i = 0; i < len ; i++)
1674                                         DBG_871X("0x%x\n", *(ptr+rmv_len+6+i));
1675                                         /* DBG_871X("0x%x\n", *(ptr+rmv_len+8+i)); */
1676
1677                                 DBG_871X("RC-end\n");
1678                         }
1679                 }
1680         }
1681 #endif /* CONFIG_AUTO_AP_MODE */
1682
1683         if ((check_fwstate(pmlmepriv, WIFI_MP_STATE) == true)) {
1684                 ptr += rmv_len;
1685                 *ptr = 0x87;
1686                 *(ptr+1) = 0x12;
1687
1688                 eth_type = 0x8712;
1689                 /*  append rx status for mp test packets */
1690                 ptr = recvframe_pull(precvframe, (rmv_len-sizeof(struct ethhdr)+2)-24);
1691                 memcpy(ptr, get_rxmem(precvframe), 24);
1692                 ptr += 24;
1693         } else
1694                 ptr = recvframe_pull(precvframe, (rmv_len-sizeof(struct ethhdr) + (bsnaphdr?2:0)));
1695
1696         memcpy(ptr, pattrib->dst, ETH_ALEN);
1697         memcpy(ptr+ETH_ALEN, pattrib->src, ETH_ALEN);
1698
1699         if (!bsnaphdr) {
1700                 be_tmp = htons(len);
1701                 memcpy(ptr+12, &be_tmp, 2);
1702         }
1703
1704         return _SUCCESS;
1705 }
1706
1707 /* perform defrag */
1708 static union recv_frame *recvframe_defrag(struct adapter *adapter,
1709                                           struct __queue *defrag_q)
1710 {
1711         struct list_head         *plist, *phead;
1712         u8 *data, wlanhdr_offset;
1713         u8 curfragnum;
1714         struct recv_frame_hdr *pfhdr, *pnfhdr;
1715         union recv_frame *prframe, *pnextrframe;
1716         struct __queue  *pfree_recv_queue;
1717
1718         curfragnum = 0;
1719         pfree_recv_queue = &adapter->recvpriv.free_recv_queue;
1720
1721         phead = get_list_head(defrag_q);
1722         plist = get_next(phead);
1723         prframe = (union recv_frame *)plist;
1724         pfhdr = &prframe->u.hdr;
1725         list_del_init(&(prframe->u.list));
1726
1727         if (curfragnum != pfhdr->attrib.frag_num) {
1728                 /* the first fragment number must be 0 */
1729                 /* free the whole queue */
1730                 rtw_free_recvframe(prframe, pfree_recv_queue);
1731                 rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1732
1733                 return NULL;
1734         }
1735
1736         curfragnum++;
1737
1738         plist = get_list_head(defrag_q);
1739
1740         plist = get_next(plist);
1741
1742         data = get_recvframe_data(prframe);
1743
1744         while (phead != plist) {
1745                 pnextrframe = (union recv_frame *)plist;
1746                 pnfhdr = &pnextrframe->u.hdr;
1747
1748
1749                 /* check the fragment sequence  (2nd ~n fragment frame) */
1750
1751                 if (curfragnum != pnfhdr->attrib.frag_num) {
1752                         /* the fragment number must be increasing  (after decache) */
1753                         /* release the defrag_q & prframe */
1754                         rtw_free_recvframe(prframe, pfree_recv_queue);
1755                         rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1756                         return NULL;
1757                 }
1758
1759                 curfragnum++;
1760
1761                 /* copy the 2nd~n fragment frame's payload to the first fragment */
1762                 /* get the 2nd~last fragment frame's payload */
1763
1764                 wlanhdr_offset = pnfhdr->attrib.hdrlen + pnfhdr->attrib.iv_len;
1765
1766                 recvframe_pull(pnextrframe, wlanhdr_offset);
1767
1768                 /* append  to first fragment frame's tail (if privacy frame, pull the ICV) */
1769                 recvframe_pull_tail(prframe, pfhdr->attrib.icv_len);
1770
1771                 /* memcpy */
1772                 memcpy(pfhdr->rx_tail, pnfhdr->rx_data, pnfhdr->len);
1773
1774                 recvframe_put(prframe, pnfhdr->len);
1775
1776                 pfhdr->attrib.icv_len = pnfhdr->attrib.icv_len;
1777                 plist = get_next(plist);
1778
1779         }
1780
1781         /* free the defrag_q queue and return the prframe */
1782         rtw_free_recvframe_queue(defrag_q, pfree_recv_queue);
1783
1784         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("Performance defrag!!!!!\n"));
1785
1786         return prframe;
1787 }
1788
1789 /* check if need to defrag, if needed queue the frame to defrag_q */
1790 union recv_frame *recvframe_chk_defrag(struct adapter *padapter, union recv_frame *precv_frame)
1791 {
1792         u8 ismfrag;
1793         u8 fragnum;
1794         u8 *psta_addr;
1795         struct recv_frame_hdr *pfhdr;
1796         struct sta_info *psta;
1797         struct sta_priv *pstapriv;
1798         struct list_head *phead;
1799         union recv_frame *prtnframe = NULL;
1800         struct __queue *pfree_recv_queue, *pdefrag_q;
1801
1802         pstapriv = &padapter->stapriv;
1803
1804         pfhdr = &precv_frame->u.hdr;
1805
1806         pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
1807
1808         /* need to define struct of wlan header frame ctrl */
1809         ismfrag = pfhdr->attrib.mfrag;
1810         fragnum = pfhdr->attrib.frag_num;
1811
1812         psta_addr = pfhdr->attrib.ta;
1813         psta = rtw_get_stainfo(pstapriv, psta_addr);
1814         if (!psta) {
1815                 u8 type = GetFrameType(pfhdr->rx_data);
1816                 if (type != WIFI_DATA_TYPE) {
1817                         psta = rtw_get_bcmc_stainfo(padapter);
1818                         pdefrag_q = &psta->sta_recvpriv.defrag_q;
1819                 } else
1820                         pdefrag_q = NULL;
1821         } else
1822                 pdefrag_q = &psta->sta_recvpriv.defrag_q;
1823
1824         if ((ismfrag == 0) && (fragnum == 0))
1825                 prtnframe = precv_frame;/* isn't a fragment frame */
1826
1827         if (ismfrag == 1) {
1828                 /* 0~(n-1) fragment frame */
1829                 /* enqueue to defraf_g */
1830                 if (pdefrag_q) {
1831                         if (fragnum == 0)
1832                                 /* the first fragment */
1833                                 if (!list_empty(&pdefrag_q->queue))
1834                                         /* free current defrag_q */
1835                                         rtw_free_recvframe_queue(pdefrag_q, pfree_recv_queue);
1836
1837
1838                         /* Then enqueue the 0~(n-1) fragment into the defrag_q */
1839
1840                         /* spin_lock(&pdefrag_q->lock); */
1841                         phead = get_list_head(pdefrag_q);
1842                         list_add_tail(&pfhdr->list, phead);
1843                         /* spin_unlock(&pdefrag_q->lock); */
1844
1845                         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("Enqueuq: ismfrag = %d, fragnum = %d\n", ismfrag, fragnum));
1846
1847                         prtnframe = NULL;
1848
1849                 } else {
1850                         /* can't find this ta's defrag_queue, so free this recv_frame */
1851                         rtw_free_recvframe(precv_frame, pfree_recv_queue);
1852                         prtnframe = NULL;
1853                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("Free because pdefrag_q == NULL: ismfrag = %d, fragnum = %d\n", ismfrag, fragnum));
1854                 }
1855
1856         }
1857
1858         if ((ismfrag == 0) && (fragnum != 0)) {
1859                 /* the last fragment frame */
1860                 /* enqueue the last fragment */
1861                 if (pdefrag_q) {
1862                         /* spin_lock(&pdefrag_q->lock); */
1863                         phead = get_list_head(pdefrag_q);
1864                         list_add_tail(&pfhdr->list, phead);
1865                         /* spin_unlock(&pdefrag_q->lock); */
1866
1867                         /* call recvframe_defrag to defrag */
1868                         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("defrag: ismfrag = %d, fragnum = %d\n", ismfrag, fragnum));
1869                         precv_frame = recvframe_defrag(padapter, pdefrag_q);
1870                         prtnframe = precv_frame;
1871
1872                 } else {
1873                         /* can't find this ta's defrag_queue, so free this recv_frame */
1874                         rtw_free_recvframe(precv_frame, pfree_recv_queue);
1875                         prtnframe = NULL;
1876                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("Free because pdefrag_q == NULL: ismfrag = %d, fragnum = %d\n", ismfrag, fragnum));
1877                 }
1878
1879         }
1880
1881
1882         if ((prtnframe) && (prtnframe->u.hdr.attrib.privacy)) {
1883                 /* after defrag we must check tkip mic code */
1884                 if (recvframe_chkmic(padapter,  prtnframe) == _FAIL) {
1885                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chkmic(padapter,  prtnframe) == _FAIL\n"));
1886                         rtw_free_recvframe(prtnframe, pfree_recv_queue);
1887                         prtnframe = NULL;
1888                 }
1889         }
1890         return prtnframe;
1891 }
1892
1893 static int amsdu_to_msdu(struct adapter *padapter, union recv_frame *prframe)
1894 {
1895         int     a_len, padding_len;
1896         u16 nSubframe_Length;
1897         u8 nr_subframes, i;
1898         u8 *pdata;
1899         _pkt *sub_pkt, *subframes[MAX_SUBFRAME_COUNT];
1900         struct recv_priv *precvpriv = &padapter->recvpriv;
1901         struct __queue *pfree_recv_queue = &(precvpriv->free_recv_queue);
1902
1903         nr_subframes = 0;
1904
1905         recvframe_pull(prframe, prframe->u.hdr.attrib.hdrlen);
1906
1907         if (prframe->u.hdr.attrib.iv_len > 0)
1908                 recvframe_pull(prframe, prframe->u.hdr.attrib.iv_len);
1909
1910         a_len = prframe->u.hdr.len;
1911
1912         pdata = prframe->u.hdr.rx_data;
1913
1914         while (a_len > ETH_HLEN) {
1915
1916                 /* Offset 12 denote 2 mac address */
1917                 nSubframe_Length = RTW_GET_BE16(pdata + 12);
1918
1919                 if (a_len < (ETHERNET_HEADER_SIZE + nSubframe_Length)) {
1920                         DBG_871X("nRemain_Length is %d and nSubframe_Length is : %d\n", a_len, nSubframe_Length);
1921                         break;
1922                 }
1923
1924                 sub_pkt = rtw_os_alloc_msdu_pkt(prframe, nSubframe_Length, pdata);
1925                 if (!sub_pkt) {
1926                         DBG_871X("%s(): allocate sub packet fail !!!\n", __func__);
1927                         break;
1928                 }
1929
1930                 /* move the data point to data content */
1931                 pdata += ETH_HLEN;
1932                 a_len -= ETH_HLEN;
1933
1934                 subframes[nr_subframes++] = sub_pkt;
1935
1936                 if (nr_subframes >= MAX_SUBFRAME_COUNT) {
1937                         DBG_871X("ParseSubframe(): Too many Subframes! Packets dropped!\n");
1938                         break;
1939                 }
1940
1941                 pdata += nSubframe_Length;
1942                 a_len -= nSubframe_Length;
1943                 if (a_len != 0) {
1944                         padding_len = 4 - ((nSubframe_Length + ETH_HLEN) & (4-1));
1945                         if (padding_len == 4) {
1946                                 padding_len = 0;
1947                         }
1948
1949                         if (a_len < padding_len) {
1950                                 DBG_871X("ParseSubframe(): a_len < padding_len !\n");
1951                                 break;
1952                         }
1953                         pdata += padding_len;
1954                         a_len -= padding_len;
1955                 }
1956         }
1957
1958         for (i = 0; i < nr_subframes; i++) {
1959                 sub_pkt = subframes[i];
1960
1961                 /* Indicat the packets to upper layer */
1962                 if (sub_pkt) {
1963                         rtw_os_recv_indicate_pkt(padapter, sub_pkt, &prframe->u.hdr.attrib);
1964                 }
1965         }
1966
1967         prframe->u.hdr.len = 0;
1968         rtw_free_recvframe(prframe, pfree_recv_queue);/* free this recv_frame */
1969
1970         return  _SUCCESS;
1971 }
1972
1973 int check_indicate_seq(struct recv_reorder_ctrl *preorder_ctrl, u16 seq_num);
1974 int check_indicate_seq(struct recv_reorder_ctrl *preorder_ctrl, u16 seq_num)
1975 {
1976         struct adapter *padapter = preorder_ctrl->padapter;
1977         struct dvobj_priv *psdpriv = padapter->dvobj;
1978         struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
1979         u8 wsize = preorder_ctrl->wsize_b;
1980         u16 wend = (preorder_ctrl->indicate_seq + wsize - 1) & 0xFFF;/*  4096; */
1981
1982         /*  Rx Reorder initialize condition. */
1983         if (preorder_ctrl->indicate_seq == 0xFFFF) {
1984                 preorder_ctrl->indicate_seq = seq_num;
1985                 #ifdef DBG_RX_SEQ
1986                 DBG_871X("DBG_RX_SEQ %s:%d init IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
1987                         preorder_ctrl->indicate_seq, seq_num);
1988                 #endif
1989
1990                 /* DbgPrint("check_indicate_seq, 1st->indicate_seq =%d\n", precvpriv->indicate_seq); */
1991         }
1992
1993         /* DbgPrint("enter->check_indicate_seq(): IndicateSeq: %d, NewSeq: %d\n", precvpriv->indicate_seq, seq_num); */
1994
1995         /*  Drop out the packet which SeqNum is smaller than WinStart */
1996         if (SN_LESS(seq_num, preorder_ctrl->indicate_seq)) {
1997                 /* RT_TRACE(COMP_RX_REORDER, DBG_LOUD, ("CheckRxTsIndicateSeq(): Packet Drop! IndicateSeq: %d, NewSeq: %d\n", pTS->RxIndicateSeq, NewSeqNum)); */
1998                 /* DbgPrint("CheckRxTsIndicateSeq(): Packet Drop! IndicateSeq: %d, NewSeq: %d\n", precvpriv->indicate_seq, seq_num); */
1999
2000                 #ifdef DBG_RX_DROP_FRAME
2001                 DBG_871X("%s IndicateSeq: %d > NewSeq: %d\n", __func__,
2002                         preorder_ctrl->indicate_seq, seq_num);
2003                 #endif
2004
2005
2006                 return false;
2007         }
2008
2009         /*  */
2010         /*  Sliding window manipulation. Conditions includes: */
2011         /*  1. Incoming SeqNum is equal to WinStart =>Window shift 1 */
2012         /*  2. Incoming SeqNum is larger than the WinEnd => Window shift N */
2013         /*  */
2014         if (SN_EQUAL(seq_num, preorder_ctrl->indicate_seq)) {
2015                 preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
2016
2017                 #ifdef DBG_RX_SEQ
2018                 DBG_871X("DBG_RX_SEQ %s:%d SN_EQUAL IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2019                         preorder_ctrl->indicate_seq, seq_num);
2020                 #endif
2021         } else if (SN_LESS(wend, seq_num)) {
2022                 /* RT_TRACE(COMP_RX_REORDER, DBG_LOUD, ("CheckRxTsIndicateSeq(): Window Shift! IndicateSeq: %d, NewSeq: %d\n", pTS->RxIndicateSeq, NewSeqNum)); */
2023                 /* DbgPrint("CheckRxTsIndicateSeq(): Window Shift! IndicateSeq: %d, NewSeq: %d\n", precvpriv->indicate_seq, seq_num); */
2024
2025                 /*  boundary situation, when seq_num cross 0xFFF */
2026                 if (seq_num >= (wsize - 1))
2027                         preorder_ctrl->indicate_seq = seq_num + 1 - wsize;
2028                 else
2029                         preorder_ctrl->indicate_seq = 0xFFF - (wsize - (seq_num + 1)) + 1;
2030                 pdbgpriv->dbg_rx_ampdu_window_shift_cnt++;
2031                 #ifdef DBG_RX_SEQ
2032                 DBG_871X("DBG_RX_SEQ %s:%d SN_LESS(wend, seq_num) IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2033                         preorder_ctrl->indicate_seq, seq_num);
2034                 #endif
2035         }
2036
2037         /* DbgPrint("exit->check_indicate_seq(): IndicateSeq: %d, NewSeq: %d\n", precvpriv->indicate_seq, seq_num); */
2038
2039         return true;
2040 }
2041
2042 int enqueue_reorder_recvframe(struct recv_reorder_ctrl *preorder_ctrl, union recv_frame *prframe);
2043 int enqueue_reorder_recvframe(struct recv_reorder_ctrl *preorder_ctrl, union recv_frame *prframe)
2044 {
2045         struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
2046         struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
2047         struct list_head        *phead, *plist;
2048         union recv_frame *pnextrframe;
2049         struct rx_pkt_attrib *pnextattrib;
2050
2051         /* DbgPrint("+enqueue_reorder_recvframe()\n"); */
2052
2053         /* spin_lock_irqsave(&ppending_recvframe_queue->lock, irql); */
2054         /* spin_lock(&ppending_recvframe_queue->lock); */
2055
2056
2057         phead = get_list_head(ppending_recvframe_queue);
2058         plist = get_next(phead);
2059
2060         while (phead != plist) {
2061                 pnextrframe = (union recv_frame *)plist;
2062                 pnextattrib = &pnextrframe->u.hdr.attrib;
2063
2064                 if (SN_LESS(pnextattrib->seq_num, pattrib->seq_num))
2065                         plist = get_next(plist);
2066                 else if (SN_EQUAL(pnextattrib->seq_num, pattrib->seq_num))
2067                         /* Duplicate entry is found!! Do not insert current entry. */
2068                         /* RT_TRACE(COMP_RX_REORDER, DBG_TRACE, ("InsertRxReorderList(): Duplicate packet is dropped!! IndicateSeq: %d, NewSeq: %d\n", pTS->RxIndicateSeq, SeqNum)); */
2069                         /* spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql); */
2070                         return false;
2071                 else
2072                         break;
2073
2074                 /* DbgPrint("enqueue_reorder_recvframe():while\n"); */
2075
2076         }
2077
2078
2079         /* spin_lock_irqsave(&ppending_recvframe_queue->lock, irql); */
2080         /* spin_lock(&ppending_recvframe_queue->lock); */
2081
2082         list_del_init(&(prframe->u.hdr.list));
2083
2084         list_add_tail(&(prframe->u.hdr.list), plist);
2085
2086         /* spin_unlock(&ppending_recvframe_queue->lock); */
2087         /* spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql); */
2088
2089
2090         /* RT_TRACE(COMP_RX_REORDER, DBG_TRACE, ("InsertRxReorderList(): Pkt insert into buffer!! IndicateSeq: %d, NewSeq: %d\n", pTS->RxIndicateSeq, SeqNum)); */
2091         return true;
2092
2093 }
2094
2095 void recv_indicatepkts_pkt_loss_cnt(struct debug_priv *pdbgpriv, u64 prev_seq, u64 current_seq);
2096 void recv_indicatepkts_pkt_loss_cnt(struct debug_priv *pdbgpriv, u64 prev_seq, u64 current_seq)
2097 {
2098         if (current_seq < prev_seq)
2099                 pdbgpriv->dbg_rx_ampdu_loss_count += (4096 + current_seq - prev_seq);
2100         else
2101                 pdbgpriv->dbg_rx_ampdu_loss_count += (current_seq - prev_seq);
2102
2103 }
2104 int recv_indicatepkts_in_order(struct adapter *padapter, struct recv_reorder_ctrl *preorder_ctrl, int bforced);
2105 int recv_indicatepkts_in_order(struct adapter *padapter, struct recv_reorder_ctrl *preorder_ctrl, int bforced)
2106 {
2107         struct list_head        *phead, *plist;
2108         union recv_frame *prframe;
2109         struct rx_pkt_attrib *pattrib;
2110         /* u8 index = 0; */
2111         int bPktInBuf = false;
2112         struct recv_priv *precvpriv = &padapter->recvpriv;
2113         struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
2114         struct dvobj_priv *psdpriv = padapter->dvobj;
2115         struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
2116
2117         DBG_COUNTER(padapter->rx_logs.core_rx_post_indicate_in_oder);
2118
2119         /* DbgPrint("+recv_indicatepkts_in_order\n"); */
2120
2121         /* spin_lock_irqsave(&ppending_recvframe_queue->lock, irql); */
2122         /* spin_lock(&ppending_recvframe_queue->lock); */
2123
2124         phead =         get_list_head(ppending_recvframe_queue);
2125         plist = get_next(phead);
2126
2127         /*  Handling some condition for forced indicate case. */
2128         if (bforced == true) {
2129                 pdbgpriv->dbg_rx_ampdu_forced_indicate_count++;
2130                 if (list_empty(phead)) {
2131                         /*  spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql); */
2132                         /* spin_unlock(&ppending_recvframe_queue->lock); */
2133                         return true;
2134                 }
2135
2136                 prframe = (union recv_frame *)plist;
2137                 pattrib = &prframe->u.hdr.attrib;
2138
2139                 #ifdef DBG_RX_SEQ
2140                 DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2141                         preorder_ctrl->indicate_seq, pattrib->seq_num);
2142                 #endif
2143                 recv_indicatepkts_pkt_loss_cnt(pdbgpriv, preorder_ctrl->indicate_seq, pattrib->seq_num);
2144                 preorder_ctrl->indicate_seq = pattrib->seq_num;
2145
2146         }
2147
2148         /*  Prepare indication list and indication. */
2149         /*  Check if there is any packet need indicate. */
2150         while (!list_empty(phead)) {
2151
2152                 prframe = (union recv_frame *)plist;
2153                 pattrib = &prframe->u.hdr.attrib;
2154
2155                 if (!SN_LESS(preorder_ctrl->indicate_seq, pattrib->seq_num)) {
2156                         RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_,
2157                                  ("recv_indicatepkts_in_order: indicate =%d seq =%d amsdu =%d\n",
2158                                   preorder_ctrl->indicate_seq, pattrib->seq_num, pattrib->amsdu));
2159
2160                         plist = get_next(plist);
2161                         list_del_init(&(prframe->u.hdr.list));
2162
2163                         if (SN_EQUAL(preorder_ctrl->indicate_seq, pattrib->seq_num)) {
2164                                 preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
2165                                 #ifdef DBG_RX_SEQ
2166                                 DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2167                                         preorder_ctrl->indicate_seq, pattrib->seq_num);
2168                                 #endif
2169                         }
2170
2171                         /* Set this as a lock to make sure that only one thread is indicating packet. */
2172                         /* pTS->RxIndicateState = RXTS_INDICATE_PROCESSING; */
2173
2174                         /*  Indicate packets */
2175                         /* RT_ASSERT((index<=REORDER_WIN_SIZE), ("RxReorderIndicatePacket(): Rx Reorder buffer full!!\n")); */
2176
2177
2178                         /* indicate this recv_frame */
2179                         /* DbgPrint("recv_indicatepkts_in_order, indicate_seq =%d, seq_num =%d\n", precvpriv->indicate_seq, pattrib->seq_num); */
2180                         if (!pattrib->amsdu) {
2181                                 /* DBG_871X("recv_indicatepkts_in_order, amsdu!= 1, indicate_seq =%d, seq_num =%d\n", preorder_ctrl->indicate_seq, pattrib->seq_num); */
2182
2183                                 if ((padapter->bDriverStopped == false) &&
2184                                     (padapter->bSurpriseRemoved == false))
2185                                         rtw_recv_indicatepkt(padapter, prframe);/* indicate this recv_frame */
2186
2187                         } else if (pattrib->amsdu == 1) {
2188                                 if (amsdu_to_msdu(padapter, prframe) != _SUCCESS)
2189                                         rtw_free_recvframe(prframe, &precvpriv->free_recv_queue);
2190
2191                         } else {
2192                                 /* error condition; */
2193                         }
2194
2195
2196                         /* Update local variables. */
2197                         bPktInBuf = false;
2198
2199                 } else {
2200                         bPktInBuf = true;
2201                         break;
2202                 }
2203
2204                 /* DbgPrint("recv_indicatepkts_in_order():while\n"); */
2205
2206         }
2207
2208         /* spin_unlock(&ppending_recvframe_queue->lock); */
2209         /* spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql); */
2210
2211         return bPktInBuf;
2212 }
2213
2214 int recv_indicatepkt_reorder(struct adapter *padapter, union recv_frame *prframe);
2215 int recv_indicatepkt_reorder(struct adapter *padapter, union recv_frame *prframe)
2216 {
2217         int retval = _SUCCESS;
2218         struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
2219         struct recv_reorder_ctrl *preorder_ctrl = prframe->u.hdr.preorder_ctrl;
2220         struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
2221         struct dvobj_priv *psdpriv = padapter->dvobj;
2222         struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
2223
2224         DBG_COUNTER(padapter->rx_logs.core_rx_post_indicate_reoder);
2225
2226         if (!pattrib->amsdu) {
2227                 /* s1. */
2228                 wlanhdr_to_ethhdr(prframe);
2229
2230                 if (pattrib->qos != 1) {
2231                         if ((padapter->bDriverStopped == false) &&
2232                             (padapter->bSurpriseRemoved == false)) {
2233                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@  recv_indicatepkt_reorder -recv_func recv_indicatepkt\n"));
2234
2235                                 rtw_recv_indicatepkt(padapter, prframe);
2236                                 return _SUCCESS;
2237
2238                         }
2239
2240                         #ifdef DBG_RX_DROP_FRAME
2241                         DBG_871X("DBG_RX_DROP_FRAME %s pattrib->qos != 1\n", __func__);
2242                         #endif
2243
2244                         return _FAIL;
2245
2246                 }
2247
2248                 if (preorder_ctrl->enable == false) {
2249                         /* indicate this recv_frame */
2250                         preorder_ctrl->indicate_seq = pattrib->seq_num;
2251                         #ifdef DBG_RX_SEQ
2252                         DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2253                                 preorder_ctrl->indicate_seq, pattrib->seq_num);
2254                         #endif
2255
2256                         rtw_recv_indicatepkt(padapter, prframe);
2257
2258                         preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1)%4096;
2259                         #ifdef DBG_RX_SEQ
2260                         DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2261                                 preorder_ctrl->indicate_seq, pattrib->seq_num);
2262                         #endif
2263
2264                         return _SUCCESS;
2265                 }
2266         } else if (pattrib->amsdu == 1) { /* temp filter -> means didn't support A-MSDUs in a A-MPDU */
2267                 if (preorder_ctrl->enable == false) {
2268                         preorder_ctrl->indicate_seq = pattrib->seq_num;
2269                         #ifdef DBG_RX_SEQ
2270                         DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2271                                 preorder_ctrl->indicate_seq, pattrib->seq_num);
2272                         #endif
2273
2274                         retval = amsdu_to_msdu(padapter, prframe);
2275
2276                         preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1)%4096;
2277                         #ifdef DBG_RX_SEQ
2278                         DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, NewSeq: %d\n", __func__, __LINE__,
2279                                 preorder_ctrl->indicate_seq, pattrib->seq_num);
2280                         #endif
2281
2282                         if (retval != _SUCCESS) {
2283                                 #ifdef DBG_RX_DROP_FRAME
2284                                 DBG_871X("DBG_RX_DROP_FRAME %s amsdu_to_msdu fail\n", __func__);
2285                                 #endif
2286                         }
2287
2288                         return retval;
2289                 }
2290         }
2291
2292         spin_lock_bh(&ppending_recvframe_queue->lock);
2293
2294         RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_,
2295                  ("recv_indicatepkt_reorder: indicate =%d seq =%d\n",
2296                   preorder_ctrl->indicate_seq, pattrib->seq_num));
2297
2298         /* s2. check if winstart_b(indicate_seq) needs to been updated */
2299         if (!check_indicate_seq(preorder_ctrl, pattrib->seq_num)) {
2300                 pdbgpriv->dbg_rx_ampdu_drop_count++;
2301                 #ifdef DBG_RX_DROP_FRAME
2302                 DBG_871X("DBG_RX_DROP_FRAME %s check_indicate_seq fail\n", __func__);
2303                 #endif
2304                 goto _err_exit;
2305         }
2306
2307
2308         /* s3. Insert all packet into Reorder Queue to maintain its ordering. */
2309         if (!enqueue_reorder_recvframe(preorder_ctrl, prframe)) {
2310                 /* DbgPrint("recv_indicatepkt_reorder, enqueue_reorder_recvframe fail!\n"); */
2311                 /* spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql); */
2312                 /* return _FAIL; */
2313                 #ifdef DBG_RX_DROP_FRAME
2314                 DBG_871X("DBG_RX_DROP_FRAME %s enqueue_reorder_recvframe fail\n", __func__);
2315                 #endif
2316                 goto _err_exit;
2317         }
2318
2319
2320         /* s4. */
2321         /*  Indication process. */
2322         /*  After Packet dropping and Sliding Window shifting as above, we can now just indicate the packets */
2323         /*  with the SeqNum smaller than latest WinStart and buffer other packets. */
2324         /*  */
2325         /*  For Rx Reorder condition: */
2326         /*  1. All packets with SeqNum smaller than WinStart => Indicate */
2327         /*  2. All packets with SeqNum larger than or equal to WinStart => Buffer it. */
2328         /*  */
2329
2330         /* recv_indicatepkts_in_order(padapter, preorder_ctrl, true); */
2331         if (recv_indicatepkts_in_order(padapter, preorder_ctrl, false) == true) {
2332                 _set_timer(&preorder_ctrl->reordering_ctrl_timer, REORDER_WAIT_TIME);
2333                 spin_unlock_bh(&ppending_recvframe_queue->lock);
2334         } else {
2335                 spin_unlock_bh(&ppending_recvframe_queue->lock);
2336                 del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
2337         }
2338
2339         return _SUCCESS;
2340
2341 _err_exit:
2342         spin_unlock_bh(&ppending_recvframe_queue->lock);
2343
2344         return _FAIL;
2345 }
2346
2347
2348 void rtw_reordering_ctrl_timeout_handler(struct timer_list *t)
2349 {
2350         struct recv_reorder_ctrl *preorder_ctrl =
2351                 from_timer(preorder_ctrl, t, reordering_ctrl_timer);
2352         struct adapter *padapter = preorder_ctrl->padapter;
2353         struct __queue *ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
2354
2355
2356         if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
2357                 return;
2358
2359         /* DBG_871X("+rtw_reordering_ctrl_timeout_handler() =>\n"); */
2360
2361         spin_lock_bh(&ppending_recvframe_queue->lock);
2362
2363         if (recv_indicatepkts_in_order(padapter, preorder_ctrl, true) == true)
2364                 _set_timer(&preorder_ctrl->reordering_ctrl_timer, REORDER_WAIT_TIME);
2365
2366         spin_unlock_bh(&ppending_recvframe_queue->lock);
2367
2368 }
2369
2370 int process_recv_indicatepkts(struct adapter *padapter, union recv_frame *prframe);
2371 int process_recv_indicatepkts(struct adapter *padapter, union recv_frame *prframe)
2372 {
2373         int retval = _SUCCESS;
2374         /* struct recv_priv *precvpriv = &padapter->recvpriv; */
2375         /* struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib; */
2376         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2377         struct ht_priv *phtpriv = &pmlmepriv->htpriv;
2378
2379         DBG_COUNTER(padapter->rx_logs.core_rx_post_indicate);
2380
2381         if (phtpriv->ht_option == true) { /* B/G/N Mode */
2382                 /* prframe->u.hdr.preorder_ctrl = &precvpriv->recvreorder_ctrl[pattrib->priority]; */
2383
2384                 if (recv_indicatepkt_reorder(padapter, prframe) != _SUCCESS) { /*  including perform A-MPDU Rx Ordering Buffer Control */
2385                         #ifdef DBG_RX_DROP_FRAME
2386                         DBG_871X("DBG_RX_DROP_FRAME %s recv_indicatepkt_reorder error!\n", __func__);
2387                         #endif
2388
2389                         if ((padapter->bDriverStopped == false) &&
2390                             (padapter->bSurpriseRemoved == false)) {
2391                                 retval = _FAIL;
2392                                 return retval;
2393                         }
2394                 }
2395         } else { /* B/G mode */
2396                 retval = wlanhdr_to_ethhdr(prframe);
2397                 if (retval != _SUCCESS) {
2398                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("wlanhdr_to_ethhdr: drop pkt\n"));
2399                         #ifdef DBG_RX_DROP_FRAME
2400                         DBG_871X("DBG_RX_DROP_FRAME %s wlanhdr_to_ethhdr error!\n", __func__);
2401                         #endif
2402                         return retval;
2403                 }
2404
2405                 if ((padapter->bDriverStopped == false) && (padapter->bSurpriseRemoved == false)) {
2406                         /* indicate this recv_frame */
2407                         RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@ process_recv_indicatepkts- recv_func recv_indicatepkt\n"));
2408                         rtw_recv_indicatepkt(padapter, prframe);
2409
2410
2411                 } else {
2412                         RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("@@@@ process_recv_indicatepkts- recv_func free_indicatepkt\n"));
2413
2414                         RT_TRACE(_module_rtl871x_recv_c_, _drv_notice_, ("recv_func:bDriverStopped(%d) OR bSurpriseRemoved(%d)", padapter->bDriverStopped, padapter->bSurpriseRemoved));
2415                         retval = _FAIL;
2416                         return retval;
2417                 }
2418
2419         }
2420
2421         return retval;
2422
2423 }
2424
2425 static int recv_func_prehandle(struct adapter *padapter, union recv_frame *rframe)
2426 {
2427         int ret = _SUCCESS;
2428         struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
2429
2430         DBG_COUNTER(padapter->rx_logs.core_rx_pre);
2431
2432         /* check the frame crtl field and decache */
2433         ret = validate_recv_frame(padapter, rframe);
2434         if (ret != _SUCCESS) {
2435                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("recv_func: validate_recv_frame fail! drop pkt\n"));
2436                 rtw_free_recvframe(rframe, pfree_recv_queue);/* free this recv_frame */
2437                 goto exit;
2438         }
2439
2440 exit:
2441         return ret;
2442 }
2443
2444 static int recv_func_posthandle(struct adapter *padapter, union recv_frame *prframe)
2445 {
2446         int ret = _SUCCESS;
2447         union recv_frame *orig_prframe = prframe;
2448         struct recv_priv *precvpriv = &padapter->recvpriv;
2449         struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
2450
2451         DBG_COUNTER(padapter->rx_logs.core_rx_post);
2452
2453         prframe = decryptor(padapter, prframe);
2454         if (!prframe) {
2455                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("decryptor: drop pkt\n"));
2456                 #ifdef DBG_RX_DROP_FRAME
2457                 DBG_871X("DBG_RX_DROP_FRAME %s decryptor: drop pkt\n", __func__);
2458                 #endif
2459                 ret = _FAIL;
2460                 DBG_COUNTER(padapter->rx_logs.core_rx_post_decrypt_err);
2461                 goto _recv_data_drop;
2462         }
2463
2464         prframe = recvframe_chk_defrag(padapter, prframe);
2465         if (!prframe)   {
2466                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvframe_chk_defrag: drop pkt\n"));
2467                 #ifdef DBG_RX_DROP_FRAME
2468                 DBG_871X("DBG_RX_DROP_FRAME %s recvframe_chk_defrag: drop pkt\n", __func__);
2469                 #endif
2470                 DBG_COUNTER(padapter->rx_logs.core_rx_post_defrag_err);
2471                 goto _recv_data_drop;
2472         }
2473
2474         prframe = portctrl(padapter, prframe);
2475         if (!prframe) {
2476                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("portctrl: drop pkt\n"));
2477                 #ifdef DBG_RX_DROP_FRAME
2478                 DBG_871X("DBG_RX_DROP_FRAME %s portctrl: drop pkt\n", __func__);
2479                 #endif
2480                 ret = _FAIL;
2481                 DBG_COUNTER(padapter->rx_logs.core_rx_post_portctrl_err);
2482                 goto _recv_data_drop;
2483         }
2484
2485         count_rx_stats(padapter, prframe, NULL);
2486
2487         ret = process_recv_indicatepkts(padapter, prframe);
2488         if (ret != _SUCCESS) {
2489                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recv_func: process_recv_indicatepkts fail!\n"));
2490                 #ifdef DBG_RX_DROP_FRAME
2491                 DBG_871X("DBG_RX_DROP_FRAME %s process_recv_indicatepkts fail!\n", __func__);
2492                 #endif
2493                 rtw_free_recvframe(orig_prframe, pfree_recv_queue);/* free this recv_frame */
2494                 DBG_COUNTER(padapter->rx_logs.core_rx_post_indicate_err);
2495                 goto _recv_data_drop;
2496         }
2497
2498 _recv_data_drop:
2499         precvpriv->rx_drop++;
2500         return ret;
2501 }
2502
2503
2504 int recv_func(struct adapter *padapter, union recv_frame *rframe);
2505 int recv_func(struct adapter *padapter, union recv_frame *rframe)
2506 {
2507         int ret;
2508         struct rx_pkt_attrib *prxattrib = &rframe->u.hdr.attrib;
2509         struct recv_priv *recvpriv = &padapter->recvpriv;
2510         struct security_priv *psecuritypriv = &padapter->securitypriv;
2511         struct mlme_priv *mlmepriv = &padapter->mlmepriv;
2512
2513         /* check if need to handle uc_swdec_pending_queue*/
2514         if (check_fwstate(mlmepriv, WIFI_STATION_STATE) && psecuritypriv->busetkipkey) {
2515                 union recv_frame *pending_frame;
2516                 int cnt = 0;
2517
2518                 while ((pending_frame = rtw_alloc_recvframe(&padapter->recvpriv.uc_swdec_pending_queue))) {
2519                         cnt++;
2520                         DBG_COUNTER(padapter->rx_logs.core_rx_dequeue);
2521                         recv_func_posthandle(padapter, pending_frame);
2522                 }
2523
2524                 if (cnt)
2525                         DBG_871X(FUNC_ADPT_FMT" dequeue %d from uc_swdec_pending_queue\n",
2526                                 FUNC_ADPT_ARG(padapter), cnt);
2527         }
2528
2529         DBG_COUNTER(padapter->rx_logs.core_rx);
2530         ret = recv_func_prehandle(padapter, rframe);
2531
2532         if (ret == _SUCCESS) {
2533
2534                 /* check if need to enqueue into uc_swdec_pending_queue*/
2535                 if (check_fwstate(mlmepriv, WIFI_STATION_STATE) &&
2536                         !IS_MCAST(prxattrib->ra) && prxattrib->encrypt > 0 &&
2537                         (prxattrib->bdecrypted == 0 || psecuritypriv->sw_decrypt == true) &&
2538                         psecuritypriv->ndisauthtype == Ndis802_11AuthModeWPAPSK &&
2539                         !psecuritypriv->busetkipkey) {
2540                         DBG_COUNTER(padapter->rx_logs.core_rx_enqueue);
2541                         rtw_enqueue_recvframe(rframe, &padapter->recvpriv.uc_swdec_pending_queue);
2542                         /* DBG_871X("%s: no key, enqueue uc_swdec_pending_queue\n", __func__); */
2543
2544                         if (recvpriv->free_recvframe_cnt < NR_RECVFRAME/4) {
2545                                 /* to prevent from recvframe starvation, get recvframe from uc_swdec_pending_queue to free_recvframe_cnt  */
2546                                 rframe = rtw_alloc_recvframe(&padapter->recvpriv.uc_swdec_pending_queue);
2547                                 if (rframe)
2548                                         goto do_posthandle;
2549                         }
2550                         goto exit;
2551                 }
2552
2553 do_posthandle:
2554                 ret = recv_func_posthandle(padapter, rframe);
2555         }
2556
2557 exit:
2558         return ret;
2559 }
2560
2561
2562 s32 rtw_recv_entry(union recv_frame *precvframe)
2563 {
2564         struct adapter *padapter;
2565         struct recv_priv *precvpriv;
2566         s32 ret = _SUCCESS;
2567
2568 /*      RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("+rtw_recv_entry\n")); */
2569
2570         padapter = precvframe->u.hdr.adapter;
2571
2572         precvpriv = &padapter->recvpriv;
2573
2574         ret = recv_func(padapter, precvframe);
2575         if (ret == _FAIL) {
2576                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("rtw_recv_entry: recv_func return fail!!!\n"));
2577                 goto _recv_entry_drop;
2578         }
2579
2580
2581         precvpriv->rx_pkts++;
2582
2583         return ret;
2584
2585 _recv_entry_drop:
2586
2587         /* RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("_recv_entry_drop\n")); */
2588
2589         return ret;
2590 }
2591
2592 static void rtw_signal_stat_timer_hdl(struct timer_list *t)
2593 {
2594         struct adapter *adapter =
2595                 from_timer(adapter, t, recvpriv.signal_stat_timer);
2596         struct recv_priv *recvpriv = &adapter->recvpriv;
2597
2598         u32 tmp_s, tmp_q;
2599         u8 avg_signal_strength = 0;
2600         u8 avg_signal_qual = 0;
2601         u32 num_signal_strength = 0;
2602         u32 num_signal_qual = 0;
2603         u8 _alpha = 5; /*  this value is based on converging_constant = 5000 and sampling_interval = 1000 */
2604
2605         if (adapter->recvpriv.is_signal_dbg) {
2606                 /* update the user specific value, signal_strength_dbg, to signal_strength, rssi */
2607                 adapter->recvpriv.signal_strength = adapter->recvpriv.signal_strength_dbg;
2608                 adapter->recvpriv.rssi = (s8)translate_percentage_to_dbm((u8)adapter->recvpriv.signal_strength_dbg);
2609         } else {
2610
2611                 if (recvpriv->signal_strength_data.update_req == 0) {/*  update_req is clear, means we got rx */
2612                         avg_signal_strength = recvpriv->signal_strength_data.avg_val;
2613                         num_signal_strength = recvpriv->signal_strength_data.total_num;
2614                         /*  after avg_vals are accquired, we can re-stat the signal values */
2615                         recvpriv->signal_strength_data.update_req = 1;
2616                 }
2617
2618                 if (recvpriv->signal_qual_data.update_req == 0) {/*  update_req is clear, means we got rx */
2619                         avg_signal_qual = recvpriv->signal_qual_data.avg_val;
2620                         num_signal_qual = recvpriv->signal_qual_data.total_num;
2621                         /*  after avg_vals are accquired, we can re-stat the signal values */
2622                         recvpriv->signal_qual_data.update_req = 1;
2623                 }
2624
2625                 if (num_signal_strength == 0) {
2626                         if (rtw_get_on_cur_ch_time(adapter) == 0
2627                                 || jiffies_to_msecs(jiffies - rtw_get_on_cur_ch_time(adapter)) < 2 * adapter->mlmeextpriv.mlmext_info.bcn_interval
2628                         ) {
2629                                 goto set_timer;
2630                         }
2631                 }
2632
2633                 if (check_fwstate(&adapter->mlmepriv, _FW_UNDER_SURVEY) == true
2634                         || check_fwstate(&adapter->mlmepriv, _FW_LINKED) == false
2635                 ) {
2636                         goto set_timer;
2637                 }
2638
2639                 /* update value of signal_strength, rssi, signal_qual */
2640                 tmp_s = (avg_signal_strength+(_alpha-1)*recvpriv->signal_strength);
2641                 if (tmp_s % _alpha)
2642                         tmp_s = tmp_s/_alpha + 1;
2643                 else
2644                         tmp_s = tmp_s/_alpha;
2645                 if (tmp_s > 100)
2646                         tmp_s = 100;
2647
2648                 tmp_q = (avg_signal_qual+(_alpha-1)*recvpriv->signal_qual);
2649                 if (tmp_q % _alpha)
2650                         tmp_q = tmp_q/_alpha + 1;
2651                 else
2652                         tmp_q = tmp_q/_alpha;
2653                 if (tmp_q > 100)
2654                         tmp_q = 100;
2655
2656                 recvpriv->signal_strength = tmp_s;
2657                 recvpriv->rssi = (s8)translate_percentage_to_dbm(tmp_s);
2658                 recvpriv->signal_qual = tmp_q;
2659
2660                 #if defined(DBG_RX_SIGNAL_DISPLAY_PROCESSING) && 1
2661                 DBG_871X(FUNC_ADPT_FMT" signal_strength:%3u, rssi:%3d, signal_qual:%3u"
2662                         ", num_signal_strength:%u, num_signal_qual:%u"
2663                         ", on_cur_ch_ms:%d"
2664                         "\n"
2665                         , FUNC_ADPT_ARG(adapter)
2666                         , recvpriv->signal_strength
2667                         , recvpriv->rssi
2668                         , recvpriv->signal_qual
2669                         , num_signal_strength, num_signal_qual
2670                         , rtw_get_on_cur_ch_time(adapter) ? jiffies_to_msecs(jiffies - rtw_get_on_cur_ch_time(adapter)) : 0
2671                 );
2672                 #endif
2673         }
2674
2675 set_timer:
2676         rtw_set_signal_stat_timer(recvpriv);
2677
2678 }