1 /******************************************************************************
4 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
5 * Linux device driver for RTL8192SU
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 * Modifications for inclusion into the Linux staging tree are
21 * Copyright(c) 2010 Larry Finger. All rights reserved.
23 * Contact information:
24 * WLAN FAE <wlanfae@realtek.com>
25 * Larry Finger <Larry.Finger@lwfinger.net>
27 ******************************************************************************/
29 #define _RTL8712_RECV_C_
31 #include <linux/if_ether.h>
34 #include "osdep_service.h"
35 #include "drv_types.h"
36 #include "recv_osdep.h"
37 #include "mlme_osdep.h"
42 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
43 static u8 bridge_tunnel_header
[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8};
45 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
46 static u8 rfc1042_header
[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
48 static void recv_tasklet(void *priv
);
50 int r8712_init_recv_priv(struct recv_priv
*precvpriv
, struct _adapter
*padapter
)
53 struct recv_buf
*precvbuf
;
57 struct sk_buff
*pskb
= NULL
;
60 _init_queue(&precvpriv
->free_recv_buf_queue
);
61 precvpriv
->pallocated_recv_buf
= _malloc(NR_RECVBUFF
*
62 sizeof(struct recv_buf
) + 4);
63 if (precvpriv
->pallocated_recv_buf
== NULL
)
65 memset(precvpriv
->pallocated_recv_buf
, 0, NR_RECVBUFF
*
66 sizeof(struct recv_buf
) + 4);
67 precvpriv
->precv_buf
= precvpriv
->pallocated_recv_buf
+ 4 -
68 ((addr_t
) (precvpriv
->pallocated_recv_buf
) & 3);
69 precvbuf
= (struct recv_buf
*)precvpriv
->precv_buf
;
70 for (i
= 0; i
< NR_RECVBUFF
; i
++) {
71 _init_listhead(&precvbuf
->list
);
72 spin_lock_init(&precvbuf
->recvbuf_lock
);
73 res
= r8712_os_recvbuf_resource_alloc(padapter
, precvbuf
);
76 precvbuf
->ref_cnt
= 0;
77 precvbuf
->adapter
= padapter
;
78 list_insert_tail(&precvbuf
->list
,
79 &(precvpriv
->free_recv_buf_queue
.queue
));
82 precvpriv
->free_recv_buf_queue_cnt
= NR_RECVBUFF
;
83 tasklet_init(&precvpriv
->recv_tasklet
,
84 (void(*)(unsigned long))recv_tasklet
,
85 (unsigned long)padapter
);
86 skb_queue_head_init(&precvpriv
->rx_skb_queue
);
88 skb_queue_head_init(&precvpriv
->free_recv_skb_queue
);
89 for (i
= 0; i
< NR_PREALLOC_RECV_SKB
; i
++) {
90 pskb
= netdev_alloc_skb(padapter
->pnetdev
, MAX_RECVBUF_SZ
+
93 pskb
->dev
= padapter
->pnetdev
;
94 tmpaddr
= (addr_t
)pskb
->data
;
95 alignment
= tmpaddr
& (RECVBUFF_ALIGN_SZ
-1);
96 skb_reserve(pskb
, (RECVBUFF_ALIGN_SZ
- alignment
));
97 skb_queue_tail(&precvpriv
->free_recv_skb_queue
, pskb
);
104 void r8712_free_recv_priv(struct recv_priv
*precvpriv
)
107 struct recv_buf
*precvbuf
;
108 struct _adapter
*padapter
= precvpriv
->adapter
;
110 precvbuf
= (struct recv_buf
*)precvpriv
->precv_buf
;
111 for (i
= 0; i
< NR_RECVBUFF
; i
++) {
112 r8712_os_recvbuf_resource_free(padapter
, precvbuf
);
115 kfree(precvpriv
->pallocated_recv_buf
);
116 skb_queue_purge(&precvpriv
->rx_skb_queue
);
117 if (skb_queue_len(&precvpriv
->rx_skb_queue
))
118 printk(KERN_WARNING
"r8712u: rx_skb_queue not empty\n");
119 skb_queue_purge(&precvpriv
->free_recv_skb_queue
);
120 if (skb_queue_len(&precvpriv
->free_recv_skb_queue
))
121 printk(KERN_WARNING
"r8712u: free_recv_skb_queue not empty "
122 "%d\n", skb_queue_len(&precvpriv
->free_recv_skb_queue
));
125 int r8712_init_recvbuf(struct _adapter
*padapter
, struct recv_buf
*precvbuf
)
129 precvbuf
->transfer_len
= 0;
131 precvbuf
->ref_cnt
= 0;
132 if (precvbuf
->pbuf
) {
133 precvbuf
->pdata
= precvbuf
->pbuf
;
134 precvbuf
->phead
= precvbuf
->pbuf
;
135 precvbuf
->ptail
= precvbuf
->pbuf
;
136 precvbuf
->pend
= precvbuf
->pdata
+ MAX_RECVBUF_SZ
;
141 int r8712_free_recvframe(union recv_frame
*precvframe
,
142 struct __queue
*pfree_recv_queue
)
145 struct _adapter
*padapter
= precvframe
->u
.hdr
.adapter
;
146 struct recv_priv
*precvpriv
= &padapter
->recvpriv
;
148 if (precvframe
->u
.hdr
.pkt
) {
149 dev_kfree_skb_any(precvframe
->u
.hdr
.pkt
);/*free skb by driver*/
150 precvframe
->u
.hdr
.pkt
= NULL
;
152 spin_lock_irqsave(&pfree_recv_queue
->lock
, irqL
);
153 list_delete(&(precvframe
->u
.hdr
.list
));
154 list_insert_tail(&(precvframe
->u
.hdr
.list
),
155 get_list_head(pfree_recv_queue
));
156 if (padapter
!= NULL
) {
157 if (pfree_recv_queue
== &precvpriv
->free_recv_queue
)
158 precvpriv
->free_recvframe_cnt
++;
160 spin_unlock_irqrestore(&pfree_recv_queue
->lock
, irqL
);
164 static void update_recvframe_attrib_from_recvstat(struct rx_pkt_attrib
*pattrib
,
165 struct recv_stat
*prxstat
)
168 struct phy_stat
*pphy_stat
;
171 drvinfo_sz
= (le32_to_cpu(prxstat
->rxdw0
)&0x000f0000)>>16;
172 drvinfo_sz
= drvinfo_sz
<<3;
175 pattrib
->bdecrypted
= ((le32_to_cpu(prxstat
->rxdw0
) & BIT(27)) >> 27)
177 pattrib
->crc_err
= ((le32_to_cpu(prxstat
->rxdw0
) & BIT(14)) >> 14);
181 if (le32_to_cpu(prxstat
->rxdw3
) & BIT(13)) {
182 pattrib
->tcpchk_valid
= 1; /* valid */
183 if (le32_to_cpu(prxstat
->rxdw3
) & BIT(11))
184 pattrib
->tcp_chkrpt
= 1; /* correct */
186 pattrib
->tcp_chkrpt
= 0; /* incorrect */
187 if (le32_to_cpu(prxstat
->rxdw3
) & BIT(12))
188 pattrib
->ip_chkrpt
= 1; /* correct */
190 pattrib
->ip_chkrpt
= 0; /* incorrect */
192 pattrib
->tcpchk_valid
= 0; /* invalid */
193 pattrib
->mcs_rate
= (u8
)((le32_to_cpu(prxstat
->rxdw3
)) & 0x3f);
194 pattrib
->htc
= (u8
)((le32_to_cpu(prxstat
->rxdw3
) >> 14) & 0x1);
199 pphy_stat
= (struct phy_stat
*)(prxstat
+1);
200 pphy_info
= (u32
*)prxstat
+1;
205 static union recv_frame
*recvframe_defrag(struct _adapter
*adapter
,
206 struct __queue
*defrag_q
)
208 struct list_head
*plist
, *phead
;
209 u8
*data
, wlanhdr_offset
;
211 struct recv_frame_hdr
*pfhdr
, *pnfhdr
;
212 union recv_frame
*prframe
, *pnextrframe
;
213 struct __queue
*pfree_recv_queue
;
215 pfree_recv_queue
= &adapter
->recvpriv
.free_recv_queue
;
216 phead
= get_list_head(defrag_q
);
217 plist
= get_next(phead
);
218 prframe
= LIST_CONTAINOR(plist
, union recv_frame
, u
);
219 list_delete(&prframe
->u
.list
);
220 pfhdr
= &prframe
->u
.hdr
;
222 if (curfragnum
!= pfhdr
->attrib
.frag_num
) {
223 /*the first fragment number must be 0
224 *free the whole queue*/
225 r8712_free_recvframe(prframe
, pfree_recv_queue
);
226 r8712_free_recvframe_queue(defrag_q
, pfree_recv_queue
);
230 plist
= get_list_head(defrag_q
);
231 plist
= get_next(plist
);
232 data
= get_recvframe_data(prframe
);
233 while (end_of_queue_search(phead
, plist
) == false) {
234 pnextrframe
= LIST_CONTAINOR(plist
, union recv_frame
, u
);
235 pnfhdr
= &pnextrframe
->u
.hdr
;
236 /*check the fragment sequence (2nd ~n fragment frame) */
237 if (curfragnum
!= pnfhdr
->attrib
.frag_num
) {
238 /* the fragment number must increase (after decache)
239 * release the defrag_q & prframe */
240 r8712_free_recvframe(prframe
, pfree_recv_queue
);
241 r8712_free_recvframe_queue(defrag_q
, pfree_recv_queue
);
245 /* copy the 2nd~n fragment frame's payload to the first fragment
246 * get the 2nd~last fragment frame's payload */
247 wlanhdr_offset
= pnfhdr
->attrib
.hdrlen
+ pnfhdr
->attrib
.iv_len
;
248 recvframe_pull(pnextrframe
, wlanhdr_offset
);
249 /* append to first fragment frame's tail (if privacy frame,
251 recvframe_pull_tail(prframe
, pfhdr
->attrib
.icv_len
);
252 memcpy(pfhdr
->rx_tail
, pnfhdr
->rx_data
, pnfhdr
->len
);
253 recvframe_put(prframe
, pnfhdr
->len
);
254 pfhdr
->attrib
.icv_len
= pnfhdr
->attrib
.icv_len
;
255 plist
= get_next(plist
);
257 /* free the defrag_q queue and return the prframe */
258 r8712_free_recvframe_queue(defrag_q
, pfree_recv_queue
);
262 /* check if need to defrag, if needed queue the frame to defrag_q */
263 union recv_frame
*r8712_recvframe_chk_defrag(struct _adapter
*padapter
,
264 union recv_frame
*precv_frame
)
269 struct recv_frame_hdr
*pfhdr
;
270 struct sta_info
*psta
;
271 struct sta_priv
*pstapriv
;
272 struct list_head
*phead
;
273 union recv_frame
*prtnframe
= NULL
;
274 struct __queue
*pfree_recv_queue
, *pdefrag_q
;
276 pstapriv
= &padapter
->stapriv
;
277 pfhdr
= &precv_frame
->u
.hdr
;
278 pfree_recv_queue
= &padapter
->recvpriv
.free_recv_queue
;
279 /* need to define struct of wlan header frame ctrl */
280 ismfrag
= pfhdr
->attrib
.mfrag
;
281 fragnum
= pfhdr
->attrib
.frag_num
;
282 psta_addr
= pfhdr
->attrib
.ta
;
283 psta
= r8712_get_stainfo(pstapriv
, psta_addr
);
287 pdefrag_q
= &psta
->sta_recvpriv
.defrag_q
;
289 if ((ismfrag
== 0) && (fragnum
== 0))
290 prtnframe
= precv_frame
;/*isn't a fragment frame*/
292 /* 0~(n-1) fragment frame
293 * enqueue to defraf_g */
294 if (pdefrag_q
!= NULL
) {
296 /*the first fragment*/
297 if (_queue_empty(pdefrag_q
) == false) {
298 /*free current defrag_q */
299 r8712_free_recvframe_queue(pdefrag_q
,
303 /* Then enqueue the 0~(n-1) fragment to the defrag_q */
304 phead
= get_list_head(pdefrag_q
);
305 list_insert_tail(&pfhdr
->list
, phead
);
308 /* can't find this ta's defrag_queue, so free this
310 r8712_free_recvframe(precv_frame
, pfree_recv_queue
);
315 if ((ismfrag
== 0) && (fragnum
!= 0)) {
316 /* the last fragment frame
317 * enqueue the last fragment */
318 if (pdefrag_q
!= NULL
) {
319 phead
= get_list_head(pdefrag_q
);
320 list_insert_tail(&pfhdr
->list
, phead
);
321 /*call recvframe_defrag to defrag*/
322 precv_frame
= recvframe_defrag(padapter
, pdefrag_q
);
323 prtnframe
= precv_frame
;
325 /* can't find this ta's defrag_queue, so free this
327 r8712_free_recvframe(precv_frame
, pfree_recv_queue
);
331 if ((prtnframe
!= NULL
) && (prtnframe
->u
.hdr
.attrib
.privacy
)) {
332 /* after defrag we must check tkip mic code */
333 if (r8712_recvframe_chkmic(padapter
, prtnframe
) == _FAIL
) {
334 r8712_free_recvframe(prtnframe
, pfree_recv_queue
);
341 static int amsdu_to_msdu(struct _adapter
*padapter
, union recv_frame
*prframe
)
343 int a_len
, padding_len
;
344 u16 eth_type
, nSubframe_Length
;
346 unsigned char *data_ptr
, *pdata
;
347 struct rx_pkt_attrib
*pattrib
;
348 _pkt
*sub_skb
, *subframes
[MAX_SUBFRAME_COUNT
];
349 struct recv_priv
*precvpriv
= &padapter
->recvpriv
;
350 struct __queue
*pfree_recv_queue
= &(precvpriv
->free_recv_queue
);
354 pattrib
= &prframe
->u
.hdr
.attrib
;
355 recvframe_pull(prframe
, prframe
->u
.hdr
.attrib
.hdrlen
);
356 if (prframe
->u
.hdr
.attrib
.iv_len
> 0)
357 recvframe_pull(prframe
, prframe
->u
.hdr
.attrib
.iv_len
);
358 a_len
= prframe
->u
.hdr
.len
;
359 pdata
= prframe
->u
.hdr
.rx_data
;
360 while (a_len
> ETH_HLEN
) {
361 /* Offset 12 denote 2 mac address */
362 nSubframe_Length
= *((u16
*)(pdata
+ 12));
363 /*==m==>change the length order*/
364 nSubframe_Length
= (nSubframe_Length
>> 8) +
365 (nSubframe_Length
<< 8);
366 if (a_len
< (ETHERNET_HEADER_SIZE
+ nSubframe_Length
)) {
367 printk(KERN_WARNING
"r8712u: nRemain_Length is %d and"
368 " nSubframe_Length is: %d\n",
369 a_len
, nSubframe_Length
);
372 /* move the data point to data content */
375 /* Allocate new skb for releasing to upper layer */
376 sub_skb
= dev_alloc_skb(nSubframe_Length
+ 12);
377 skb_reserve(sub_skb
, 12);
378 data_ptr
= (u8
*)skb_put(sub_skb
, nSubframe_Length
);
379 memcpy(data_ptr
, pdata
, nSubframe_Length
);
380 subframes
[nr_subframes
++] = sub_skb
;
381 if (nr_subframes
>= MAX_SUBFRAME_COUNT
) {
382 printk(KERN_WARNING
"r8712u: ParseSubframe(): Too"
383 " many Subframes! Packets dropped!\n");
386 pdata
+= nSubframe_Length
;
387 a_len
-= nSubframe_Length
;
389 padding_len
= 4 - ((nSubframe_Length
+ ETH_HLEN
) & 3);
390 if (padding_len
== 4)
392 if (a_len
< padding_len
)
394 pdata
+= padding_len
;
395 a_len
-= padding_len
;
398 for (i
= 0; i
< nr_subframes
; i
++) {
399 sub_skb
= subframes
[i
];
400 /* convert hdr + possible LLC headers into Ethernet header */
401 eth_type
= (sub_skb
->data
[6] << 8) | sub_skb
->data
[7];
402 if (sub_skb
->len
>= 8 &&
403 ((!memcmp(sub_skb
->data
, rfc1042_header
, SNAP_SIZE
) &&
404 eth_type
!= ETH_P_AARP
&& eth_type
!= ETH_P_IPX
) ||
405 !memcmp(sub_skb
->data
, bridge_tunnel_header
, SNAP_SIZE
))) {
406 /* remove RFC1042 or Bridge-Tunnel encapsulation and
407 * replace EtherType */
408 skb_pull(sub_skb
, SNAP_SIZE
);
409 memcpy(skb_push(sub_skb
, ETH_ALEN
), pattrib
->src
,
411 memcpy(skb_push(sub_skb
, ETH_ALEN
), pattrib
->dst
,
415 /* Leave Ethernet header part of hdr and full payload */
416 len
= htons(sub_skb
->len
);
417 memcpy(skb_push(sub_skb
, 2), &len
, 2);
418 memcpy(skb_push(sub_skb
, ETH_ALEN
), pattrib
->src
,
420 memcpy(skb_push(sub_skb
, ETH_ALEN
), pattrib
->dst
,
423 /* Indicate the packets to upper layer */
426 eth_type_trans(sub_skb
, padapter
->pnetdev
);
427 sub_skb
->dev
= padapter
->pnetdev
;
428 if ((pattrib
->tcpchk_valid
== 1) &&
429 (pattrib
->tcp_chkrpt
== 1)) {
430 sub_skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
432 sub_skb
->ip_summed
= CHECKSUM_NONE
;
437 prframe
->u
.hdr
.len
= 0;
438 r8712_free_recvframe(prframe
, pfree_recv_queue
);
442 void r8712_rxcmd_event_hdl(struct _adapter
*padapter
, void *prxcmdbuf
)
446 u16 pkt_len
, cmd_len
, drvinfo_sz
;
448 struct recv_stat
*prxstat
;
450 poffset
= (u8
*)prxcmdbuf
;
451 voffset
= *(uint
*)poffset
;
452 pkt_len
= le32_to_cpu(voffset
) & 0x00003fff;
453 prxstat
= (struct recv_stat
*)prxcmdbuf
;
454 drvinfo_sz
= ((le32_to_cpu(prxstat
->rxdw0
) & 0x000f0000) >> 16);
455 drvinfo_sz
= drvinfo_sz
<< 3;
456 poffset
+= RXDESC_SIZE
+ drvinfo_sz
;
458 voffset
= *(uint
*)poffset
;
459 cmd_len
= (u16
)(le32_to_cpu(voffset
) & 0xffff);
460 cmd_seq
= (u8
)((le32_to_cpu(voffset
) >> 24) & 0x7f);
461 eid
= (u8
)((le32_to_cpu(voffset
) >> 16) & 0xff);
462 r8712_event_handle(padapter
, (uint
*)poffset
);
463 poffset
+= (cmd_len
+ 8);/*8 bytes alignment*/
464 } while (le32_to_cpu(voffset
) & BIT(31));
468 static int check_indicate_seq(struct recv_reorder_ctrl
*preorder_ctrl
,
471 u8 wsize
= preorder_ctrl
->wsize_b
;
472 u16 wend
= (preorder_ctrl
->indicate_seq
+ wsize
- 1) % 4096;
474 /* Rx Reorder initialize condition.*/
475 if (preorder_ctrl
->indicate_seq
== 0xffff)
476 preorder_ctrl
->indicate_seq
= seq_num
;
477 /* Drop out the packet which SeqNum is smaller than WinStart */
478 if (SN_LESS(seq_num
, preorder_ctrl
->indicate_seq
))
481 * Sliding window manipulation. Conditions includes:
482 * 1. Incoming SeqNum is equal to WinStart =>Window shift 1
483 * 2. Incoming SeqNum is larger than the WinEnd => Window shift N
485 if (SN_EQUAL(seq_num
, preorder_ctrl
->indicate_seq
))
486 preorder_ctrl
->indicate_seq
= (preorder_ctrl
->indicate_seq
+
488 else if (SN_LESS(wend
, seq_num
)) {
489 if (seq_num
>= (wsize
- 1))
490 preorder_ctrl
->indicate_seq
= seq_num
+ 1 - wsize
;
492 preorder_ctrl
->indicate_seq
= 4095 - (wsize
-
498 static int enqueue_reorder_recvframe(struct recv_reorder_ctrl
*preorder_ctrl
,
499 union recv_frame
*prframe
)
501 struct list_head
*phead
, *plist
;
502 union recv_frame
*pnextrframe
;
503 struct rx_pkt_attrib
*pnextattrib
;
504 struct __queue
*ppending_recvframe_queue
=
505 &preorder_ctrl
->pending_recvframe_queue
;
506 struct rx_pkt_attrib
*pattrib
= &prframe
->u
.hdr
.attrib
;
508 phead
= get_list_head(ppending_recvframe_queue
);
509 plist
= get_next(phead
);
510 while (end_of_queue_search(phead
, plist
) == false) {
511 pnextrframe
= LIST_CONTAINOR(plist
, union recv_frame
, u
);
512 pnextattrib
= &pnextrframe
->u
.hdr
.attrib
;
513 if (SN_LESS(pnextattrib
->seq_num
, pattrib
->seq_num
))
514 plist
= get_next(plist
);
515 else if (SN_EQUAL(pnextattrib
->seq_num
, pattrib
->seq_num
))
520 list_delete(&(prframe
->u
.hdr
.list
));
521 list_insert_tail(&(prframe
->u
.hdr
.list
), plist
);
525 int r8712_recv_indicatepkts_in_order(struct _adapter
*padapter
,
526 struct recv_reorder_ctrl
*preorder_ctrl
,
529 struct list_head
*phead
, *plist
;
530 union recv_frame
*prframe
;
531 struct rx_pkt_attrib
*pattrib
;
532 int bPktInBuf
= false;
533 struct recv_priv
*precvpriv
= &padapter
->recvpriv
;
534 struct __queue
*ppending_recvframe_queue
=
535 &preorder_ctrl
->pending_recvframe_queue
;
537 phead
= get_list_head(ppending_recvframe_queue
);
538 plist
= get_next(phead
);
539 /* Handling some condition for forced indicate case.*/
540 if (bforced
== true) {
541 if (is_list_empty(phead
))
544 prframe
= LIST_CONTAINOR(plist
, union recv_frame
, u
);
545 pattrib
= &prframe
->u
.hdr
.attrib
;
546 preorder_ctrl
->indicate_seq
= pattrib
->seq_num
;
549 /* Prepare indication list and indication.
550 * Check if there is any packet need indicate. */
551 while (!is_list_empty(phead
)) {
552 prframe
= LIST_CONTAINOR(plist
, union recv_frame
, u
);
553 pattrib
= &prframe
->u
.hdr
.attrib
;
554 if (!SN_LESS(preorder_ctrl
->indicate_seq
, pattrib
->seq_num
)) {
555 plist
= get_next(plist
);
556 list_delete(&(prframe
->u
.hdr
.list
));
557 if (SN_EQUAL(preorder_ctrl
->indicate_seq
,
559 preorder_ctrl
->indicate_seq
=
560 (preorder_ctrl
->indicate_seq
+ 1) % 4096;
561 /*indicate this recv_frame*/
562 if (!pattrib
->amsdu
) {
563 if ((padapter
->bDriverStopped
== false) &&
564 (padapter
->bSurpriseRemoved
== false)) {
565 /* indicate this recv_frame */
566 r8712_recv_indicatepkt(padapter
,
569 } else if (pattrib
->amsdu
== 1) {
570 if (amsdu_to_msdu(padapter
, prframe
) !=
572 r8712_free_recvframe(prframe
,
573 &precvpriv
->free_recv_queue
);
575 /* Update local variables. */
585 static int recv_indicatepkt_reorder(struct _adapter
*padapter
,
586 union recv_frame
*prframe
)
589 struct rx_pkt_attrib
*pattrib
= &prframe
->u
.hdr
.attrib
;
590 struct recv_reorder_ctrl
*preorder_ctrl
= prframe
->u
.hdr
.preorder_ctrl
;
591 struct __queue
*ppending_recvframe_queue
=
592 &preorder_ctrl
->pending_recvframe_queue
;
594 if (!pattrib
->amsdu
) {
596 r8712_wlanhdr_to_ethhdr(prframe
);
597 if (pattrib
->qos
!= 1) {
598 if ((padapter
->bDriverStopped
== false) &&
599 (padapter
->bSurpriseRemoved
== false)) {
600 r8712_recv_indicatepkt(padapter
, prframe
);
606 spin_lock_irqsave(&ppending_recvframe_queue
->lock
, irql
);
607 /*s2. check if winstart_b(indicate_seq) needs to be updated*/
608 if (!check_indicate_seq(preorder_ctrl
, pattrib
->seq_num
))
610 /*s3. Insert all packet into Reorder Queue to maintain its ordering.*/
611 if (!enqueue_reorder_recvframe(preorder_ctrl
, prframe
))
614 * Indication process.
615 * After Packet dropping and Sliding Window shifting as above, we can
616 * now just indicate the packets with the SeqNum smaller than latest
617 * WinStart and buffer other packets.
619 * For Rx Reorder condition:
620 * 1. All packets with SeqNum smaller than WinStart => Indicate
621 * 2. All packets with SeqNum larger than or equal to
622 * WinStart => Buffer it.
624 if (r8712_recv_indicatepkts_in_order(padapter
, preorder_ctrl
, false) ==
626 _set_timer(&preorder_ctrl
->reordering_ctrl_timer
,
628 spin_unlock_irqrestore(&ppending_recvframe_queue
->lock
, irql
);
630 spin_unlock_irqrestore(&ppending_recvframe_queue
->lock
, irql
);
631 _cancel_timer_ex(&preorder_ctrl
->reordering_ctrl_timer
);
635 spin_unlock_irqrestore(&ppending_recvframe_queue
->lock
, irql
);
639 void r8712_reordering_ctrl_timeout_handler(void *pcontext
)
642 struct recv_reorder_ctrl
*preorder_ctrl
=
643 (struct recv_reorder_ctrl
*)pcontext
;
644 struct _adapter
*padapter
= preorder_ctrl
->padapter
;
645 struct __queue
*ppending_recvframe_queue
=
646 &preorder_ctrl
->pending_recvframe_queue
;
648 if (padapter
->bDriverStopped
|| padapter
->bSurpriseRemoved
)
650 spin_lock_irqsave(&ppending_recvframe_queue
->lock
, irql
);
651 r8712_recv_indicatepkts_in_order(padapter
, preorder_ctrl
, true);
652 spin_unlock_irqrestore(&ppending_recvframe_queue
->lock
, irql
);
655 static int r8712_process_recv_indicatepkts(struct _adapter
*padapter
,
656 union recv_frame
*prframe
)
658 int retval
= _SUCCESS
;
659 struct mlme_priv
*pmlmepriv
= &padapter
->mlmepriv
;
660 struct ht_priv
*phtpriv
= &pmlmepriv
->htpriv
;
662 if (phtpriv
->ht_option
== 1) { /*B/G/N Mode*/
663 if (recv_indicatepkt_reorder(padapter
, prframe
) != _SUCCESS
) {
664 /* including perform A-MPDU Rx Ordering Buffer Control*/
665 if ((padapter
->bDriverStopped
== false) &&
666 (padapter
->bSurpriseRemoved
== false))
669 } else { /*B/G mode*/
670 retval
= r8712_wlanhdr_to_ethhdr(prframe
);
671 if (retval
!= _SUCCESS
)
673 if ((padapter
->bDriverStopped
== false) &&
674 (padapter
->bSurpriseRemoved
== false)) {
675 /* indicate this recv_frame */
676 r8712_recv_indicatepkt(padapter
, prframe
);
683 static u8
query_rx_pwr_percentage(s8 antpower
)
685 if ((antpower
<= -100) || (antpower
>= 20))
687 else if (antpower
>= 0)
690 return 100 + antpower
;
693 static u8
evm_db2percentage(s8 value
)
696 * -33dB~0dB to 0%~99%
712 s32
r8712_signal_scale_mapping(s32 cur_sig
)
716 if (cur_sig
>= 51 && cur_sig
<= 100)
718 else if (cur_sig
>= 41 && cur_sig
<= 50)
719 ret_sig
= 80 + ((cur_sig
- 40) * 2);
720 else if (cur_sig
>= 31 && cur_sig
<= 40)
721 ret_sig
= 66 + (cur_sig
- 30);
722 else if (cur_sig
>= 21 && cur_sig
<= 30)
723 ret_sig
= 54 + (cur_sig
- 20);
724 else if (cur_sig
>= 10 && cur_sig
<= 20)
725 ret_sig
= 42 + (((cur_sig
- 10) * 2) / 3);
726 else if (cur_sig
>= 5 && cur_sig
<= 9)
727 ret_sig
= 22 + (((cur_sig
- 5) * 3) / 2);
728 else if (cur_sig
>= 1 && cur_sig
<= 4)
729 ret_sig
= 6 + (((cur_sig
- 1) * 3) / 2);
735 static s32
translate2dbm(struct _adapter
*padapter
, u8 signal_strength_idx
)
737 s32 signal_power
; /* in dBm.*/
738 /* Translate to dBm (x=0.5y-95).*/
739 signal_power
= (s32
)((signal_strength_idx
+ 1) >> 1);
744 static void query_rx_phy_status(struct _adapter
*padapter
,
745 union recv_frame
*prframe
)
747 u8 i
, max_spatial_stream
, evm
;
748 struct recv_stat
*prxstat
= (struct recv_stat
*)prframe
->u
.hdr
.rx_head
;
749 struct phy_stat
*pphy_stat
= (struct phy_stat
*)(prxstat
+ 1);
750 u8
*pphy_head
= (u8
*)(prxstat
+ 1);
751 s8 rx_pwr
[4], rx_pwr_all
;
753 u32 rssi
, total_rssi
= 0;
754 u8 bcck_rate
= 0, rf_rx_num
= 0, cck_highpwr
= 0;
755 struct phy_cck_rx_status
*pcck_buf
;
758 /* Record it for next packet processing*/
759 bcck_rate
= (prframe
->u
.hdr
.attrib
.mcs_rate
<= 3 ? 1 : 0);
763 /* CCK Driver info Structure is not the same as OFDM packet.*/
764 pcck_buf
= (struct phy_cck_rx_status
*)pphy_stat
;
765 /* (1)Hardware does not provide RSSI for CCK
766 * (2)PWDB, Average PWDB cacluated by hardware
767 * (for rate adaptive)
770 report
= pcck_buf
->cck_agc_rpt
& 0xc0;
771 report
= report
>> 6;
773 /* Modify the RF RNA gain value to -40, -20,
774 * -2, 14 by Jenyu's suggestion
775 * Note: different RF with the different
778 rx_pwr_all
= -40 - (pcck_buf
->cck_agc_rpt
&
782 rx_pwr_all
= -20 - (pcck_buf
->cck_agc_rpt
&
786 rx_pwr_all
= -2 - (pcck_buf
->cck_agc_rpt
&
790 rx_pwr_all
= 14 - (pcck_buf
->cck_agc_rpt
&
795 report
= ((u8
)(le32_to_cpu(pphy_stat
->phydw1
) >> 8)) &
797 report
= report
>> 5;
800 rx_pwr_all
= -40 - ((pcck_buf
->cck_agc_rpt
&
804 rx_pwr_all
= -20 - ((pcck_buf
->cck_agc_rpt
&
808 rx_pwr_all
= -2 - ((pcck_buf
->cck_agc_rpt
&
812 rx_pwr_all
= 14 - ((pcck_buf
->cck_agc_rpt
&
817 pwdb_all
= query_rx_pwr_percentage(rx_pwr_all
);
818 /* CCK gain is smaller than OFDM/MCS gain,*/
819 /* so we add gain diff by experiences, the val is 6 */
823 /* modify the offset to make the same gain index with OFDM.*/
824 if (pwdb_all
> 34 && pwdb_all
<= 42)
826 else if (pwdb_all
> 26 && pwdb_all
<= 34)
828 else if (pwdb_all
> 14 && pwdb_all
<= 26)
830 else if (pwdb_all
> 4 && pwdb_all
<= 14)
833 * (3) Get Signal Quality (EVM)
838 sq
= pcck_buf
->sq_rpt
;
839 if (pcck_buf
->sq_rpt
> 64)
841 else if (pcck_buf
->sq_rpt
< 20)
844 sq
= ((64-sq
) * 100) / 44;
846 prframe
->u
.hdr
.attrib
.signal_qual
= sq
;
847 prframe
->u
.hdr
.attrib
.rx_mimo_signal_qual
[0] = sq
;
848 prframe
->u
.hdr
.attrib
.rx_mimo_signal_qual
[1] = -1;
850 /* (1)Get RSSI for HT rate */
851 for (i
= 0; i
< ((padapter
->registrypriv
.rf_config
) &
854 rx_pwr
[i
] = ((pphy_head
[PHY_STAT_GAIN_TRSW_SHT
+ i
]
856 /* Translate DBM to percentage. */
857 rssi
= query_rx_pwr_percentage(rx_pwr
[i
]);
860 /* (2)PWDB, Average PWDB cacluated by hardware (for
862 rx_pwr_all
= (((pphy_head
[PHY_STAT_PWDB_ALL_SHT
]) >> 1) & 0x7f)
864 pwdb_all
= query_rx_pwr_percentage(rx_pwr_all
);
867 /* (3)EVM of HT rate */
868 if (prframe
->u
.hdr
.attrib
.htc
&&
869 prframe
->u
.hdr
.attrib
.mcs_rate
>= 20 &&
870 prframe
->u
.hdr
.attrib
.mcs_rate
<= 27) {
871 /* both spatial stream make sense */
872 max_spatial_stream
= 2;
874 /* only spatial stream 1 makes sense */
875 max_spatial_stream
= 1;
877 for (i
= 0; i
< max_spatial_stream
; i
++) {
878 evm
= evm_db2percentage((pphy_head
879 [PHY_STAT_RXEVM_SHT
+ i
]));/*dbm*/
880 prframe
->u
.hdr
.attrib
.signal_qual
=
882 prframe
->u
.hdr
.attrib
.rx_mimo_signal_qual
[i
] =
887 /* UI BSS List signal strength(in percentage), make it good looking,
888 * from 0~100. It is assigned to the BSS List in
889 * GetValueFromBeaconOrProbeRsp(). */
891 prframe
->u
.hdr
.attrib
.signal_strength
=
892 (u8
)r8712_signal_scale_mapping(pwdb_all
);
895 prframe
->u
.hdr
.attrib
.signal_strength
=
896 (u8
)(r8712_signal_scale_mapping(total_rssi
/=
901 static void process_link_qual(struct _adapter
*padapter
,
902 union recv_frame
*prframe
)
904 u32 last_evm
= 0, tmpVal
;
905 struct rx_pkt_attrib
*pattrib
;
907 if (prframe
== NULL
|| padapter
== NULL
)
909 pattrib
= &prframe
->u
.hdr
.attrib
;
910 if (pattrib
->signal_qual
!= 0) {
912 * 1. Record the general EVM to the sliding window.
914 if (padapter
->recvpriv
.signal_qual_data
.total_num
++ >=
915 PHY_LINKQUALITY_SLID_WIN_MAX
) {
916 padapter
->recvpriv
.signal_qual_data
.total_num
=
917 PHY_LINKQUALITY_SLID_WIN_MAX
;
918 last_evm
= padapter
->recvpriv
.signal_qual_data
.elements
919 [padapter
->recvpriv
.signal_qual_data
.index
];
920 padapter
->recvpriv
.signal_qual_data
.total_val
-=
923 padapter
->recvpriv
.signal_qual_data
.total_val
+=
924 pattrib
->signal_qual
;
925 padapter
->recvpriv
.signal_qual_data
.elements
[padapter
->
926 recvpriv
.signal_qual_data
.index
++] =
927 pattrib
->signal_qual
;
928 if (padapter
->recvpriv
.signal_qual_data
.index
>=
929 PHY_LINKQUALITY_SLID_WIN_MAX
)
930 padapter
->recvpriv
.signal_qual_data
.index
= 0;
932 /* <1> Showed on UI for user, in percentage. */
933 tmpVal
= padapter
->recvpriv
.signal_qual_data
.total_val
/
934 padapter
->recvpriv
.signal_qual_data
.total_num
;
935 padapter
->recvpriv
.signal
= (u8
)tmpVal
;
939 static void process_rssi(struct _adapter
*padapter
, union recv_frame
*prframe
)
941 u32 last_rssi
, tmp_val
;
942 struct rx_pkt_attrib
*pattrib
= &prframe
->u
.hdr
.attrib
;
944 if (padapter
->recvpriv
.signal_strength_data
.total_num
++ >=
945 PHY_RSSI_SLID_WIN_MAX
) {
946 padapter
->recvpriv
.signal_strength_data
.total_num
=
947 PHY_RSSI_SLID_WIN_MAX
;
948 last_rssi
= padapter
->recvpriv
.signal_strength_data
.elements
949 [padapter
->recvpriv
.signal_strength_data
.index
];
950 padapter
->recvpriv
.signal_strength_data
.total_val
-= last_rssi
;
952 padapter
->recvpriv
.signal_strength_data
.total_val
+=
953 pattrib
->signal_strength
;
954 padapter
->recvpriv
.signal_strength_data
.elements
[padapter
->recvpriv
.
955 signal_strength_data
.index
++] =
956 pattrib
->signal_strength
;
957 if (padapter
->recvpriv
.signal_strength_data
.index
>=
958 PHY_RSSI_SLID_WIN_MAX
)
959 padapter
->recvpriv
.signal_strength_data
.index
= 0;
960 tmp_val
= padapter
->recvpriv
.signal_strength_data
.total_val
/
961 padapter
->recvpriv
.signal_strength_data
.total_num
;
962 padapter
->recvpriv
.rssi
= (s8
)translate2dbm(padapter
, (u8
)tmp_val
);
965 static void process_phy_info(struct _adapter
*padapter
,
966 union recv_frame
*prframe
)
968 query_rx_phy_status(padapter
, prframe
);
969 process_rssi(padapter
, prframe
);
970 process_link_qual(padapter
, prframe
);
973 int recv_func(struct _adapter
*padapter
, void *pcontext
)
975 struct rx_pkt_attrib
*pattrib
;
976 union recv_frame
*prframe
, *orig_prframe
;
977 int retval
= _SUCCESS
;
978 struct __queue
*pfree_recv_queue
= &padapter
->recvpriv
.free_recv_queue
;
979 struct mlme_priv
*pmlmepriv
= &padapter
->mlmepriv
;
981 prframe
= (union recv_frame
*)pcontext
;
982 orig_prframe
= prframe
;
983 pattrib
= &prframe
->u
.hdr
.attrib
;
984 if ((check_fwstate(pmlmepriv
, WIFI_MP_STATE
) == true)) {
985 if (pattrib
->crc_err
== 1)
986 padapter
->mppriv
.rx_crcerrpktcount
++;
988 padapter
->mppriv
.rx_pktcount
++;
989 if (check_fwstate(pmlmepriv
, WIFI_MP_LPBK_STATE
) == false) {
990 /* free this recv_frame */
991 r8712_free_recvframe(orig_prframe
, pfree_recv_queue
);
992 goto _exit_recv_func
;
995 /* check the frame crtl field and decache */
996 retval
= r8712_validate_recv_frame(padapter
, prframe
);
997 if (retval
!= _SUCCESS
) {
998 /* free this recv_frame */
999 r8712_free_recvframe(orig_prframe
, pfree_recv_queue
);
1000 goto _exit_recv_func
;
1002 process_phy_info(padapter
, prframe
);
1003 prframe
= r8712_decryptor(padapter
, prframe
);
1004 if (prframe
== NULL
) {
1006 goto _exit_recv_func
;
1008 prframe
= r8712_recvframe_chk_defrag(padapter
, prframe
);
1009 if (prframe
== NULL
)
1010 goto _exit_recv_func
;
1011 prframe
= r8712_portctrl(padapter
, prframe
);
1012 if (prframe
== NULL
) {
1014 goto _exit_recv_func
;
1016 retval
= r8712_process_recv_indicatepkts(padapter
, prframe
);
1017 if (retval
!= _SUCCESS
) {
1018 r8712_free_recvframe(orig_prframe
, pfree_recv_queue
);
1019 goto _exit_recv_func
;
1025 static int recvbuf2recvframe(struct _adapter
*padapter
, struct sk_buff
*pskb
)
1027 u8
*pbuf
, shift_sz
= 0;
1031 struct recv_stat
*prxstat
;
1032 u16 pkt_cnt
, drvinfo_sz
, pkt_offset
, tmp_len
, alloc_sz
;
1033 struct __queue
*pfree_recv_queue
;
1034 _pkt
*pkt_copy
= NULL
;
1035 union recv_frame
*precvframe
= NULL
;
1036 struct recv_priv
*precvpriv
= &padapter
->recvpriv
;
1038 pfree_recv_queue
= &(precvpriv
->free_recv_queue
);
1040 prxstat
= (struct recv_stat
*)pbuf
;
1041 pkt_cnt
= (le32_to_cpu(prxstat
->rxdw2
)>>16)&0xff;
1042 pkt_len
= le32_to_cpu(prxstat
->rxdw0
)&0x00003fff;
1043 transfer_len
= pskb
->len
;
1044 /* Test throughput with Netgear 3700 (No security) with Chariot 3T3R
1045 * pairs. The packet count will be a big number so that the containing
1046 * packet will effect the Rx reordering. */
1047 if (transfer_len
< pkt_len
) {
1048 /* In this case, it means the MAX_RECVBUF_SZ is too small to
1049 * get the data from 8712u. */
1053 prxstat
= (struct recv_stat
*)pbuf
;
1054 pkt_len
= le32_to_cpu(prxstat
->rxdw0
)&0x00003fff;
1055 /* more fragment bit */
1056 mf
= (le32_to_cpu(prxstat
->rxdw1
) >> 27) & 0x1;
1057 /* ragmentation number */
1058 frag
= (le32_to_cpu(prxstat
->rxdw2
) >> 12) & 0xf;
1059 /* uint 2^3 = 8 bytes */
1060 drvinfo_sz
= (le32_to_cpu(prxstat
->rxdw0
) & 0x000f0000) >> 16;
1061 drvinfo_sz
= drvinfo_sz
<<3;
1063 goto _exit_recvbuf2recvframe
;
1064 /* Qos data, wireless lan header length is 26 */
1065 if ((le32_to_cpu(prxstat
->rxdw0
) >> 23) & 0x01)
1067 precvframe
= r8712_alloc_recvframe(pfree_recv_queue
);
1068 if (precvframe
== NULL
)
1069 goto _exit_recvbuf2recvframe
;
1070 _init_listhead(&precvframe
->u
.hdr
.list
);
1071 precvframe
->u
.hdr
.precvbuf
= NULL
; /*can't access the precvbuf*/
1072 precvframe
->u
.hdr
.len
= 0;
1073 tmp_len
= pkt_len
+ drvinfo_sz
+ RXDESC_SIZE
;
1074 pkt_offset
= (u16
)_RND128(tmp_len
);
1075 /* for first fragment packet, driver need allocate 1536 +
1076 * drvinfo_sz + RXDESC_SIZE to defrag packet. */
1077 if ((mf
== 1) && (frag
== 0))
1078 alloc_sz
= 1658;/*1658+6=1664, 1664 is 128 alignment.*/
1081 /* 2 is for IP header 4 bytes alignment in QoS packet case.
1082 * 4 is for skb->data 4 bytes alignment. */
1084 pkt_copy
= netdev_alloc_skb(padapter
->pnetdev
, alloc_sz
);
1086 pkt_copy
->dev
= padapter
->pnetdev
;
1087 precvframe
->u
.hdr
.pkt
= pkt_copy
;
1088 skb_reserve(pkt_copy
, 4 - ((addr_t
)(pkt_copy
->data
)
1090 skb_reserve(pkt_copy
, shift_sz
);
1091 memcpy(pkt_copy
->data
, pbuf
, tmp_len
);
1092 precvframe
->u
.hdr
.rx_head
= precvframe
->u
.hdr
.rx_data
=
1093 precvframe
->u
.hdr
.rx_tail
= pkt_copy
->data
;
1094 precvframe
->u
.hdr
.rx_end
= pkt_copy
->data
+ alloc_sz
;
1096 precvframe
->u
.hdr
.pkt
= skb_clone(pskb
, GFP_ATOMIC
);
1097 precvframe
->u
.hdr
.rx_head
= pbuf
;
1098 precvframe
->u
.hdr
.rx_data
= pbuf
;
1099 precvframe
->u
.hdr
.rx_tail
= pbuf
;
1100 precvframe
->u
.hdr
.rx_end
= pbuf
+ alloc_sz
;
1102 recvframe_put(precvframe
, tmp_len
);
1103 recvframe_pull(precvframe
, drvinfo_sz
+ RXDESC_SIZE
);
1104 /* because the endian issue, driver avoid reference to the
1105 * rxstat after calling update_recvframe_attrib_from_recvstat();
1107 update_recvframe_attrib_from_recvstat(&precvframe
->u
.hdr
.attrib
,
1109 r8712_recv_entry(precvframe
);
1110 transfer_len
-= pkt_offset
;
1115 } while ((transfer_len
> 0) && pkt_cnt
> 0);
1116 _exit_recvbuf2recvframe
:
1120 static void recv_tasklet(void *priv
)
1122 struct sk_buff
*pskb
;
1123 struct _adapter
*padapter
= (struct _adapter
*)priv
;
1124 struct recv_priv
*precvpriv
= &padapter
->recvpriv
;
1126 while (NULL
!= (pskb
= skb_dequeue(&precvpriv
->rx_skb_queue
))) {
1127 recvbuf2recvframe(padapter
, pskb
);
1128 skb_reset_tail_pointer(pskb
);
1130 if (!skb_cloned(pskb
))
1131 skb_queue_tail(&precvpriv
->free_recv_skb_queue
, pskb
);