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