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