1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3 * rtl8712_recv.c
4 *
5 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
6 * Linux device driver for RTL8192SU
7 *
8 * Modifications for inclusion into the Linux staging tree are
9 * Copyright(c) 2010 Larry Finger. All rights reserved.
10 *
11 * Contact information:
12 * WLAN FAE <wlanfae@realtek.com>
13 * Larry Finger <Larry.Finger@lwfinger.net>
14 *
15 ******************************************************************************/
16
17 #define _RTL8712_RECV_C_
18
19 #include <linux/if_ether.h>
20 #include <linux/ip.h>
21 #include <net/cfg80211.h>
22
23 #include "osdep_service.h"
24 #include "drv_types.h"
25 #include "recv_osdep.h"
26 #include "mlme_osdep.h"
27 #include "ethernet.h"
28 #include "usb_ops.h"
29 #include "wifi.h"
30
31 static void recv_tasklet(struct tasklet_struct *t);
32
r8712_init_recv_priv(struct recv_priv * precvpriv,struct _adapter * padapter)33 int r8712_init_recv_priv(struct recv_priv *precvpriv,
34 struct _adapter *padapter)
35 {
36 int i;
37 struct recv_buf *precvbuf;
38 addr_t tmpaddr = 0;
39 int alignment = 0;
40 struct sk_buff *pskb = NULL;
41
42 /*init recv_buf*/
43 _init_queue(&precvpriv->free_recv_buf_queue);
44 precvpriv->pallocated_recv_buf =
45 kzalloc(NR_RECVBUFF * sizeof(struct recv_buf) + 4, GFP_ATOMIC);
46 if (!precvpriv->pallocated_recv_buf)
47 return -ENOMEM;
48 precvpriv->precv_buf = precvpriv->pallocated_recv_buf + 4 -
49 ((addr_t)(precvpriv->pallocated_recv_buf) & 3);
50 precvbuf = (struct recv_buf *)precvpriv->precv_buf;
51 for (i = 0; i < NR_RECVBUFF; i++) {
52 INIT_LIST_HEAD(&precvbuf->list);
53 spin_lock_init(&precvbuf->recvbuf_lock);
54 if (r8712_os_recvbuf_resource_alloc(padapter, precvbuf))
55 break;
56 precvbuf->ref_cnt = 0;
57 precvbuf->adapter = padapter;
58 list_add_tail(&precvbuf->list,
59 &precvpriv->free_recv_buf_queue.queue);
60 precvbuf++;
61 }
62 precvpriv->free_recv_buf_queue_cnt = NR_RECVBUFF;
63 tasklet_setup(&precvpriv->recv_tasklet, recv_tasklet);
64 skb_queue_head_init(&precvpriv->rx_skb_queue);
65
66 skb_queue_head_init(&precvpriv->free_recv_skb_queue);
67 for (i = 0; i < NR_PREALLOC_RECV_SKB; i++) {
68 pskb = netdev_alloc_skb(padapter->pnetdev, MAX_RECVBUF_SZ +
69 RECVBUFF_ALIGN_SZ);
70 if (pskb) {
71 tmpaddr = (addr_t)pskb->data;
72 alignment = tmpaddr & (RECVBUFF_ALIGN_SZ - 1);
73 skb_reserve(pskb, (RECVBUFF_ALIGN_SZ - alignment));
74 skb_queue_tail(&precvpriv->free_recv_skb_queue, pskb);
75 }
76 pskb = NULL;
77 }
78 return 0;
79 }
80
r8712_free_recv_priv(struct recv_priv * precvpriv)81 void r8712_free_recv_priv(struct recv_priv *precvpriv)
82 {
83 int i;
84 struct recv_buf *precvbuf;
85 struct _adapter *padapter = precvpriv->adapter;
86
87 precvbuf = (struct recv_buf *)precvpriv->precv_buf;
88 for (i = 0; i < NR_RECVBUFF; i++) {
89 r8712_os_recvbuf_resource_free(padapter, precvbuf);
90 precvbuf++;
91 }
92 kfree(precvpriv->pallocated_recv_buf);
93 skb_queue_purge(&precvpriv->rx_skb_queue);
94 if (skb_queue_len(&precvpriv->rx_skb_queue))
95 netdev_warn(padapter->pnetdev, "r8712u: rx_skb_queue not empty\n");
96 skb_queue_purge(&precvpriv->free_recv_skb_queue);
97 if (skb_queue_len(&precvpriv->free_recv_skb_queue))
98 netdev_warn(padapter->pnetdev, "r8712u: free_recv_skb_queue not empty %d\n",
99 skb_queue_len(&precvpriv->free_recv_skb_queue));
100 }
101
r8712_init_recvbuf(struct _adapter * padapter,struct recv_buf * precvbuf)102 void r8712_init_recvbuf(struct _adapter *padapter, struct recv_buf *precvbuf)
103 {
104 precvbuf->transfer_len = 0;
105 precvbuf->len = 0;
106 precvbuf->ref_cnt = 0;
107 if (precvbuf->pbuf) {
108 precvbuf->pdata = precvbuf->pbuf;
109 precvbuf->phead = precvbuf->pbuf;
110 precvbuf->ptail = precvbuf->pbuf;
111 precvbuf->pend = precvbuf->pdata + MAX_RECVBUF_SZ;
112 }
113 }
114
r8712_free_recvframe(union recv_frame * precvframe,struct __queue * pfree_recv_queue)115 void r8712_free_recvframe(union recv_frame *precvframe,
116 struct __queue *pfree_recv_queue)
117 {
118 unsigned long irqL;
119 struct _adapter *padapter = precvframe->u.hdr.adapter;
120 struct recv_priv *precvpriv = &padapter->recvpriv;
121
122 if (precvframe->u.hdr.pkt) {
123 dev_kfree_skb_any(precvframe->u.hdr.pkt);/*free skb by driver*/
124 precvframe->u.hdr.pkt = NULL;
125 }
126 spin_lock_irqsave(&pfree_recv_queue->lock, irqL);
127 list_del_init(&precvframe->u.hdr.list);
128 list_add_tail(&precvframe->u.hdr.list, &pfree_recv_queue->queue);
129 if (padapter) {
130 if (pfree_recv_queue == &precvpriv->free_recv_queue)
131 precvpriv->free_recvframe_cnt++;
132 }
133 spin_unlock_irqrestore(&pfree_recv_queue->lock, irqL);
134 }
135
update_recvframe_attrib_from_recvstat(struct rx_pkt_attrib * pattrib,struct recv_stat * prxstat)136 static void update_recvframe_attrib_from_recvstat(struct rx_pkt_attrib *pattrib,
137 struct recv_stat *prxstat)
138 {
139 u16 drvinfo_sz;
140
141 drvinfo_sz = (le32_to_cpu(prxstat->rxdw0) & 0x000f0000) >> 16;
142 drvinfo_sz <<= 3;
143 /*TODO:
144 * Offset 0
145 */
146 pattrib->bdecrypted = (le32_to_cpu(prxstat->rxdw0) & BIT(27)) == 0;
147 pattrib->crc_err = (le32_to_cpu(prxstat->rxdw0) & BIT(14)) != 0;
148 /*Offset 4*/
149 /*Offset 8*/
150 /*Offset 12*/
151 if (le32_to_cpu(prxstat->rxdw3) & BIT(13)) {
152 pattrib->tcpchk_valid = 1; /* valid */
153 if (le32_to_cpu(prxstat->rxdw3) & BIT(11))
154 pattrib->tcp_chkrpt = 1; /* correct */
155 else
156 pattrib->tcp_chkrpt = 0; /* incorrect */
157 if (le32_to_cpu(prxstat->rxdw3) & BIT(12))
158 pattrib->ip_chkrpt = 1; /* correct */
159 else
160 pattrib->ip_chkrpt = 0; /* incorrect */
161 } else {
162 pattrib->tcpchk_valid = 0; /* invalid */
163 }
164 pattrib->mcs_rate = (u8)((le32_to_cpu(prxstat->rxdw3)) & 0x3f);
165 pattrib->htc = (u8)((le32_to_cpu(prxstat->rxdw3) >> 14) & 0x1);
166 /*Offset 16*/
167 /*Offset 20*/
168 /*phy_info*/
169 }
170
171 /*perform defrag*/
recvframe_defrag(struct _adapter * adapter,struct __queue * defrag_q)172 static union recv_frame *recvframe_defrag(struct _adapter *adapter,
173 struct __queue *defrag_q)
174 {
175 struct list_head *plist, *phead;
176 u8 wlanhdr_offset;
177 u8 curfragnum;
178 struct recv_frame_hdr *pfhdr, *pnfhdr;
179 union recv_frame *prframe, *pnextrframe;
180 struct __queue *pfree_recv_queue;
181
182 pfree_recv_queue = &adapter->recvpriv.free_recv_queue;
183 phead = &defrag_q->queue;
184 plist = phead->next;
185 prframe = container_of(plist, union recv_frame, u.list);
186 list_del_init(&prframe->u.list);
187 pfhdr = &prframe->u.hdr;
188 curfragnum = 0;
189 if (curfragnum != pfhdr->attrib.frag_num) {
190 /*the first fragment number must be 0
191 *free the whole queue
192 */
193 r8712_free_recvframe(prframe, pfree_recv_queue);
194 r8712_free_recvframe_queue(defrag_q, pfree_recv_queue);
195 return NULL;
196 }
197 curfragnum++;
198 plist = &defrag_q->queue;
199 plist = plist->next;
200 while (!end_of_queue_search(phead, plist)) {
201 pnextrframe = container_of(plist, union recv_frame, u.list);
202 pnfhdr = &pnextrframe->u.hdr;
203 /*check the fragment sequence (2nd ~n fragment frame) */
204 if (curfragnum != pnfhdr->attrib.frag_num) {
205 /* the fragment number must increase (after decache)
206 * release the defrag_q & prframe
207 */
208 r8712_free_recvframe(prframe, pfree_recv_queue);
209 r8712_free_recvframe_queue(defrag_q, pfree_recv_queue);
210 return NULL;
211 }
212 curfragnum++;
213 /* copy the 2nd~n fragment frame's payload to the first fragment
214 * get the 2nd~last fragment frame's payload
215 */
216 wlanhdr_offset = pnfhdr->attrib.hdrlen + pnfhdr->attrib.iv_len;
217 recvframe_pull(pnextrframe, wlanhdr_offset);
218 /* append to first fragment frame's tail (if privacy frame,
219 * pull the ICV)
220 */
221 recvframe_pull_tail(prframe, pfhdr->attrib.icv_len);
222 memcpy(pfhdr->rx_tail, pnfhdr->rx_data, pnfhdr->len);
223 recvframe_put(prframe, pnfhdr->len);
224 pfhdr->attrib.icv_len = pnfhdr->attrib.icv_len;
225 plist = plist->next;
226 }
227 /* free the defrag_q queue and return the prframe */
228 r8712_free_recvframe_queue(defrag_q, pfree_recv_queue);
229 return prframe;
230 }
231
232 /* check if need to defrag, if needed queue the frame to defrag_q */
r8712_recvframe_chk_defrag(struct _adapter * padapter,union recv_frame * precv_frame)233 union recv_frame *r8712_recvframe_chk_defrag(struct _adapter *padapter,
234 union recv_frame *precv_frame)
235 {
236 u8 ismfrag;
237 u8 fragnum;
238 u8 *psta_addr;
239 struct recv_frame_hdr *pfhdr;
240 struct sta_info *psta;
241 struct sta_priv *pstapriv;
242 struct list_head *phead;
243 union recv_frame *prtnframe = NULL;
244 struct __queue *pfree_recv_queue, *pdefrag_q;
245
246 pstapriv = &padapter->stapriv;
247 pfhdr = &precv_frame->u.hdr;
248 pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
249 /* need to define struct of wlan header frame ctrl */
250 ismfrag = pfhdr->attrib.mfrag;
251 fragnum = pfhdr->attrib.frag_num;
252 psta_addr = pfhdr->attrib.ta;
253 psta = r8712_get_stainfo(pstapriv, psta_addr);
254 if (!psta)
255 pdefrag_q = NULL;
256 else
257 pdefrag_q = &psta->sta_recvpriv.defrag_q;
258
259 if ((ismfrag == 0) && (fragnum == 0))
260 prtnframe = precv_frame;/*isn't a fragment frame*/
261 if (ismfrag == 1) {
262 /* 0~(n-1) fragment frame
263 * enqueue to defraf_g
264 */
265 if (pdefrag_q) {
266 if (fragnum == 0) {
267 /*the first fragment*/
268 if (!list_empty(&pdefrag_q->queue)) {
269 /*free current defrag_q */
270 r8712_free_recvframe_queue(pdefrag_q,
271 pfree_recv_queue);
272 }
273 }
274 /* Then enqueue the 0~(n-1) fragment to the defrag_q */
275 phead = &pdefrag_q->queue;
276 list_add_tail(&pfhdr->list, phead);
277 prtnframe = NULL;
278 } else {
279 /* can't find this ta's defrag_queue, so free this
280 * recv_frame
281 */
282 r8712_free_recvframe(precv_frame, pfree_recv_queue);
283 prtnframe = NULL;
284 }
285 }
286 if ((ismfrag == 0) && (fragnum != 0)) {
287 /* the last fragment frame
288 * enqueue the last fragment
289 */
290 if (pdefrag_q) {
291 phead = &pdefrag_q->queue;
292 list_add_tail(&pfhdr->list, phead);
293 /*call recvframe_defrag to defrag*/
294 precv_frame = recvframe_defrag(padapter, pdefrag_q);
295 prtnframe = precv_frame;
296 } else {
297 /* can't find this ta's defrag_queue, so free this
298 * recv_frame
299 */
300 r8712_free_recvframe(precv_frame, pfree_recv_queue);
301 prtnframe = NULL;
302 }
303 }
304 if (prtnframe && (prtnframe->u.hdr.attrib.privacy)) {
305 /* after defrag we must check tkip mic code */
306 if (r8712_recvframe_chkmic(padapter, prtnframe) == _FAIL) {
307 r8712_free_recvframe(prtnframe, pfree_recv_queue);
308 prtnframe = NULL;
309 }
310 }
311 return prtnframe;
312 }
313
amsdu_to_msdu(struct _adapter * padapter,union recv_frame * prframe)314 static void amsdu_to_msdu(struct _adapter *padapter, union recv_frame *prframe)
315 {
316 int a_len, padding_len;
317 u16 eth_type, nSubframe_Length;
318 u8 nr_subframes, i;
319 unsigned char *pdata;
320 struct rx_pkt_attrib *pattrib;
321 _pkt *sub_skb, *subframes[MAX_SUBFRAME_COUNT];
322 struct recv_priv *precvpriv = &padapter->recvpriv;
323 struct __queue *pfree_recv_queue = &precvpriv->free_recv_queue;
324
325 nr_subframes = 0;
326 pattrib = &prframe->u.hdr.attrib;
327 recvframe_pull(prframe, prframe->u.hdr.attrib.hdrlen);
328 if (prframe->u.hdr.attrib.iv_len > 0)
329 recvframe_pull(prframe, prframe->u.hdr.attrib.iv_len);
330 a_len = prframe->u.hdr.len;
331 pdata = prframe->u.hdr.rx_data;
332 while (a_len > ETH_HLEN) {
333 /* Offset 12 denote 2 mac address */
334 nSubframe_Length = *((u16 *)(pdata + 12));
335 /*==m==>change the length order*/
336 nSubframe_Length = (nSubframe_Length >> 8) +
337 (nSubframe_Length << 8);
338 if (a_len < (ETHERNET_HEADER_SIZE + nSubframe_Length)) {
339 netdev_warn(padapter->pnetdev, "r8712u: nRemain_Length is %d and nSubframe_Length is: %d\n",
340 a_len, nSubframe_Length);
341 goto exit;
342 }
343 /* move the data point to data content */
344 pdata += ETH_HLEN;
345 a_len -= ETH_HLEN;
346 /* Allocate new skb for releasing to upper layer */
347 sub_skb = dev_alloc_skb(nSubframe_Length + 12);
348 if (!sub_skb)
349 break;
350 skb_reserve(sub_skb, 12);
351 skb_put_data(sub_skb, pdata, nSubframe_Length);
352 subframes[nr_subframes++] = sub_skb;
353 if (nr_subframes >= MAX_SUBFRAME_COUNT) {
354 netdev_warn(padapter->pnetdev, "r8712u: ParseSubframe(): Too many Subframes! Packets dropped!\n");
355 break;
356 }
357 pdata += nSubframe_Length;
358 a_len -= nSubframe_Length;
359 if (a_len != 0) {
360 padding_len = 4 - ((nSubframe_Length + ETH_HLEN) & 3);
361 if (padding_len == 4)
362 padding_len = 0;
363 if (a_len < padding_len)
364 goto exit;
365 pdata += padding_len;
366 a_len -= padding_len;
367 }
368 }
369 for (i = 0; i < nr_subframes; i++) {
370 sub_skb = subframes[i];
371 /* convert hdr + possible LLC headers into Ethernet header */
372 eth_type = (sub_skb->data[6] << 8) | sub_skb->data[7];
373 if (sub_skb->len >= 8 &&
374 ((!memcmp(sub_skb->data, rfc1042_header, SNAP_SIZE) &&
375 eth_type != ETH_P_AARP && eth_type != ETH_P_IPX) ||
376 !memcmp(sub_skb->data, bridge_tunnel_header, SNAP_SIZE))) {
377 /* remove RFC1042 or Bridge-Tunnel encapsulation and
378 * replace EtherType
379 */
380 skb_pull(sub_skb, SNAP_SIZE);
381 memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src,
382 ETH_ALEN);
383 memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst,
384 ETH_ALEN);
385 } else {
386 __be16 len;
387 /* Leave Ethernet header part of hdr and full payload */
388 len = htons(sub_skb->len);
389 memcpy(skb_push(sub_skb, 2), &len, 2);
390 memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src,
391 ETH_ALEN);
392 memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst,
393 ETH_ALEN);
394 }
395 /* Indicate the packets to upper layer */
396 if (sub_skb) {
397 sub_skb->protocol =
398 eth_type_trans(sub_skb, padapter->pnetdev);
399 sub_skb->dev = padapter->pnetdev;
400 if ((pattrib->tcpchk_valid == 1) &&
401 (pattrib->tcp_chkrpt == 1)) {
402 sub_skb->ip_summed = CHECKSUM_UNNECESSARY;
403 } else {
404 sub_skb->ip_summed = CHECKSUM_NONE;
405 }
406 netif_rx(sub_skb);
407 }
408 }
409 exit:
410 prframe->u.hdr.len = 0;
411 r8712_free_recvframe(prframe, pfree_recv_queue);
412 }
413
r8712_rxcmd_event_hdl(struct _adapter * padapter,void * prxcmdbuf)414 void r8712_rxcmd_event_hdl(struct _adapter *padapter, void *prxcmdbuf)
415 {
416 __le32 voffset;
417 u8 *poffset;
418 u16 cmd_len, drvinfo_sz;
419 struct recv_stat *prxstat;
420
421 poffset = prxcmdbuf;
422 voffset = *(__le32 *)poffset;
423 prxstat = prxcmdbuf;
424 drvinfo_sz = (le32_to_cpu(prxstat->rxdw0) & 0x000f0000) >> 16;
425 drvinfo_sz <<= 3;
426 poffset += RXDESC_SIZE + drvinfo_sz;
427 do {
428 voffset = *(__le32 *)poffset;
429 cmd_len = (u16)(le32_to_cpu(voffset) & 0xffff);
430 r8712_event_handle(padapter, (__le32 *)poffset);
431 poffset += (cmd_len + 8);/*8 bytes alignment*/
432 } while (le32_to_cpu(voffset) & BIT(31));
433 }
434
check_indicate_seq(struct recv_reorder_ctrl * preorder_ctrl,u16 seq_num)435 static int check_indicate_seq(struct recv_reorder_ctrl *preorder_ctrl,
436 u16 seq_num)
437 {
438 u8 wsize = preorder_ctrl->wsize_b;
439 u16 wend = (preorder_ctrl->indicate_seq + wsize - 1) % 4096;
440
441 /* Rx Reorder initialize condition.*/
442 if (preorder_ctrl->indicate_seq == 0xffff)
443 preorder_ctrl->indicate_seq = seq_num;
444 /* Drop out the packet which SeqNum is smaller than WinStart */
445 if (SN_LESS(seq_num, preorder_ctrl->indicate_seq))
446 return false;
447 /*
448 * Sliding window manipulation. Conditions includes:
449 * 1. Incoming SeqNum is equal to WinStart =>Window shift 1
450 * 2. Incoming SeqNum is larger than the WinEnd => Window shift N
451 */
452 if (SN_EQUAL(seq_num, preorder_ctrl->indicate_seq))
453 preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq +
454 1) % 4096;
455 else if (SN_LESS(wend, seq_num)) {
456 if (seq_num >= (wsize - 1))
457 preorder_ctrl->indicate_seq = seq_num + 1 - wsize;
458 else
459 preorder_ctrl->indicate_seq = 4095 - (wsize -
460 (seq_num + 1)) + 1;
461 }
462 return true;
463 }
464
enqueue_reorder_recvframe(struct recv_reorder_ctrl * preorder_ctrl,union recv_frame * prframe)465 static int enqueue_reorder_recvframe(struct recv_reorder_ctrl *preorder_ctrl,
466 union recv_frame *prframe)
467 {
468 struct list_head *phead, *plist;
469 union recv_frame *pnextrframe;
470 struct rx_pkt_attrib *pnextattrib;
471 struct __queue *ppending_recvframe_queue =
472 &preorder_ctrl->pending_recvframe_queue;
473 struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
474
475 phead = &ppending_recvframe_queue->queue;
476 plist = phead->next;
477 while (!end_of_queue_search(phead, plist)) {
478 pnextrframe = container_of(plist, union recv_frame, u.list);
479 pnextattrib = &pnextrframe->u.hdr.attrib;
480
481 if (SN_EQUAL(pnextattrib->seq_num, pattrib->seq_num))
482 return false;
483
484 if (SN_LESS(pnextattrib->seq_num, pattrib->seq_num))
485 plist = plist->next;
486 else
487 break;
488 }
489 list_del_init(&prframe->u.hdr.list);
490 list_add_tail(&prframe->u.hdr.list, plist);
491 return true;
492 }
493
r8712_recv_indicatepkts_in_order(struct _adapter * padapter,struct recv_reorder_ctrl * preorder_ctrl,int bforced)494 int r8712_recv_indicatepkts_in_order(struct _adapter *padapter,
495 struct recv_reorder_ctrl *preorder_ctrl,
496 int bforced)
497 {
498 struct list_head *phead, *plist;
499 union recv_frame *prframe;
500 struct rx_pkt_attrib *pattrib;
501 int bPktInBuf = false;
502 struct __queue *ppending_recvframe_queue =
503 &preorder_ctrl->pending_recvframe_queue;
504
505 phead = &ppending_recvframe_queue->queue;
506 plist = phead->next;
507 /* Handling some condition for forced indicate case.*/
508 if (bforced) {
509 if (list_empty(phead))
510 return true;
511
512 prframe = container_of(plist, union recv_frame, u.list);
513 pattrib = &prframe->u.hdr.attrib;
514 preorder_ctrl->indicate_seq = pattrib->seq_num;
515 }
516 /* Prepare indication list and indication.
517 * Check if there is any packet need indicate.
518 */
519 while (!list_empty(phead)) {
520 prframe = container_of(plist, union recv_frame, u.list);
521 pattrib = &prframe->u.hdr.attrib;
522 if (!SN_LESS(preorder_ctrl->indicate_seq, pattrib->seq_num)) {
523 plist = plist->next;
524 list_del_init(&prframe->u.hdr.list);
525 if (SN_EQUAL(preorder_ctrl->indicate_seq,
526 pattrib->seq_num))
527 preorder_ctrl->indicate_seq =
528 (preorder_ctrl->indicate_seq + 1) % 4096;
529 /*indicate this recv_frame*/
530 if (!pattrib->amsdu) {
531 if (!padapter->driver_stopped &&
532 !padapter->surprise_removed) {
533 /* indicate this recv_frame */
534 r8712_recv_indicatepkt(padapter,
535 prframe);
536 }
537 } else if (pattrib->amsdu == 1) {
538 amsdu_to_msdu(padapter, prframe);
539 }
540 /* Update local variables. */
541 bPktInBuf = false;
542 } else {
543 bPktInBuf = true;
544 break;
545 }
546 }
547 return bPktInBuf;
548 }
549
recv_indicatepkt_reorder(struct _adapter * padapter,union recv_frame * prframe)550 static int recv_indicatepkt_reorder(struct _adapter *padapter,
551 union recv_frame *prframe)
552 {
553 unsigned long irql;
554 struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
555 struct recv_reorder_ctrl *preorder_ctrl = prframe->u.hdr.preorder_ctrl;
556 struct __queue *ppending_recvframe_queue =
557 &preorder_ctrl->pending_recvframe_queue;
558
559 if (!pattrib->amsdu) {
560 /* s1. */
561 r8712_wlanhdr_to_ethhdr(prframe);
562 if (pattrib->qos != 1) {
563 if (!padapter->driver_stopped &&
564 !padapter->surprise_removed) {
565 r8712_recv_indicatepkt(padapter, prframe);
566 return 0;
567 } else {
568 return -EINVAL;
569 }
570 }
571 }
572 spin_lock_irqsave(&ppending_recvframe_queue->lock, irql);
573 /*s2. check if winstart_b(indicate_seq) needs to be updated*/
574 if (!check_indicate_seq(preorder_ctrl, pattrib->seq_num))
575 goto _err_exit;
576 /*s3. Insert all packet into Reorder Queue to maintain its ordering.*/
577 if (!enqueue_reorder_recvframe(preorder_ctrl, prframe))
578 goto _err_exit;
579 /*s4.
580 * Indication process.
581 * After Packet dropping and Sliding Window shifting as above, we can
582 * now just indicate the packets with the SeqNum smaller than latest
583 * WinStart and buffer other packets.
584 *
585 * For Rx Reorder condition:
586 * 1. All packets with SeqNum smaller than WinStart => Indicate
587 * 2. All packets with SeqNum larger than or equal to
588 * WinStart => Buffer it.
589 */
590 if (r8712_recv_indicatepkts_in_order(padapter, preorder_ctrl, false)) {
591 mod_timer(&preorder_ctrl->reordering_ctrl_timer,
592 jiffies + msecs_to_jiffies(REORDER_WAIT_TIME));
593 spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql);
594 } else {
595 spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql);
596 del_timer(&preorder_ctrl->reordering_ctrl_timer);
597 }
598 return 0;
599 _err_exit:
600 spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql);
601 return -ENOMEM;
602 }
603
r8712_reordering_ctrl_timeout_handler(void * pcontext)604 void r8712_reordering_ctrl_timeout_handler(void *pcontext)
605 {
606 unsigned long irql;
607 struct recv_reorder_ctrl *preorder_ctrl = pcontext;
608 struct _adapter *padapter = preorder_ctrl->padapter;
609 struct __queue *ppending_recvframe_queue =
610 &preorder_ctrl->pending_recvframe_queue;
611
612 if (padapter->driver_stopped || padapter->surprise_removed)
613 return;
614 spin_lock_irqsave(&ppending_recvframe_queue->lock, irql);
615 r8712_recv_indicatepkts_in_order(padapter, preorder_ctrl, true);
616 spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql);
617 }
618
r8712_process_recv_indicatepkts(struct _adapter * padapter,union recv_frame * prframe)619 static int r8712_process_recv_indicatepkts(struct _adapter *padapter,
620 union recv_frame *prframe)
621 {
622 int retval = _SUCCESS;
623 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
624 struct ht_priv *phtpriv = &pmlmepriv->htpriv;
625
626 if (phtpriv->ht_option == 1) { /*B/G/N Mode*/
627 if (recv_indicatepkt_reorder(padapter, prframe)) {
628 /* including perform A-MPDU Rx Ordering Buffer Control*/
629 if (!padapter->driver_stopped &&
630 !padapter->surprise_removed)
631 return _FAIL;
632 }
633 } else { /*B/G mode*/
634 retval = r8712_wlanhdr_to_ethhdr(prframe);
635 if (retval)
636 return _FAIL;
637 if (!padapter->driver_stopped && !padapter->surprise_removed) {
638 /* indicate this recv_frame */
639 r8712_recv_indicatepkt(padapter, prframe);
640 } else {
641 return _FAIL;
642 }
643 }
644 return retval;
645 }
646
query_rx_pwr_percentage(s8 antpower)647 static u8 query_rx_pwr_percentage(s8 antpower)
648 {
649 if ((antpower <= -100) || (antpower >= 20))
650 return 0;
651 else if (antpower >= 0)
652 return 100;
653 else
654 return 100 + antpower;
655 }
656
evm_db2percentage(s8 value)657 static u8 evm_db2percentage(s8 value)
658 {
659 /*
660 * -33dB~0dB to 0%~99%
661 */
662 s8 ret_val = clamp(-value, 0, 33) * 3;
663
664 if (ret_val == 99)
665 ret_val = 100;
666
667 return ret_val;
668 }
669
r8712_signal_scale_mapping(s32 cur_sig)670 s32 r8712_signal_scale_mapping(s32 cur_sig)
671 {
672 s32 ret_sig;
673
674 if (cur_sig >= 51 && cur_sig <= 100)
675 ret_sig = 100;
676 else if (cur_sig >= 41 && cur_sig <= 50)
677 ret_sig = 80 + ((cur_sig - 40) * 2);
678 else if (cur_sig >= 31 && cur_sig <= 40)
679 ret_sig = 66 + (cur_sig - 30);
680 else if (cur_sig >= 21 && cur_sig <= 30)
681 ret_sig = 54 + (cur_sig - 20);
682 else if (cur_sig >= 10 && cur_sig <= 20)
683 ret_sig = 42 + (((cur_sig - 10) * 2) / 3);
684 else if (cur_sig >= 5 && cur_sig <= 9)
685 ret_sig = 22 + (((cur_sig - 5) * 3) / 2);
686 else if (cur_sig >= 1 && cur_sig <= 4)
687 ret_sig = 6 + (((cur_sig - 1) * 3) / 2);
688 else
689 ret_sig = cur_sig;
690 return ret_sig;
691 }
692
translate2dbm(struct _adapter * padapter,u8 signal_strength_idx)693 static s32 translate2dbm(struct _adapter *padapter, u8 signal_strength_idx)
694 {
695 s32 signal_power; /* in dBm.*/
696 /* Translate to dBm (x=0.5y-95).*/
697 signal_power = (s32)((signal_strength_idx + 1) >> 1);
698 signal_power -= 95;
699 return signal_power;
700 }
701
query_rx_phy_status(struct _adapter * padapter,union recv_frame * prframe)702 static void query_rx_phy_status(struct _adapter *padapter,
703 union recv_frame *prframe)
704 {
705 u8 i, max_spatial_stream, evm;
706 struct recv_stat *prxstat = (struct recv_stat *)prframe->u.hdr.rx_head;
707 struct phy_stat *pphy_stat = (struct phy_stat *)(prxstat + 1);
708 u8 *pphy_head = (u8 *)(prxstat + 1);
709 s8 rx_pwr[4], rx_pwr_all;
710 u8 pwdb_all;
711 u32 rssi, total_rssi = 0;
712 u8 bcck_rate = 0, rf_rx_num = 0, cck_highpwr = 0;
713 struct phy_cck_rx_status *pcck_buf;
714 u8 sq;
715
716 /* Record it for next packet processing*/
717 bcck_rate = (prframe->u.hdr.attrib.mcs_rate <= 3 ? 1 : 0);
718 if (bcck_rate) {
719 u8 report;
720
721 /* CCK Driver info Structure is not the same as OFDM packet.*/
722 pcck_buf = (struct phy_cck_rx_status *)pphy_stat;
723 /* (1)Hardware does not provide RSSI for CCK
724 * (2)PWDB, Average PWDB calculated by hardware
725 * (for rate adaptive)
726 */
727 if (!cck_highpwr) {
728 report = pcck_buf->cck_agc_rpt & 0xc0;
729 report >>= 6;
730 switch (report) {
731 /* Modify the RF RNA gain value to -40, -20,
732 * -2, 14 by Jenyu's suggestion
733 * Note: different RF with the different
734 * RNA gain.
735 */
736 case 0x3:
737 rx_pwr_all = -40 - (pcck_buf->cck_agc_rpt &
738 0x3e);
739 break;
740 case 0x2:
741 rx_pwr_all = -20 - (pcck_buf->cck_agc_rpt &
742 0x3e);
743 break;
744 case 0x1:
745 rx_pwr_all = -2 - (pcck_buf->cck_agc_rpt &
746 0x3e);
747 break;
748 case 0x0:
749 rx_pwr_all = 14 - (pcck_buf->cck_agc_rpt &
750 0x3e);
751 break;
752 }
753 } else {
754 report = ((u8)(le32_to_cpu(pphy_stat->phydw1) >> 8)) &
755 0x60;
756 report >>= 5;
757 switch (report) {
758 case 0x3:
759 rx_pwr_all = -40 - ((pcck_buf->cck_agc_rpt &
760 0x1f) << 1);
761 break;
762 case 0x2:
763 rx_pwr_all = -20 - ((pcck_buf->cck_agc_rpt &
764 0x1f) << 1);
765 break;
766 case 0x1:
767 rx_pwr_all = -2 - ((pcck_buf->cck_agc_rpt &
768 0x1f) << 1);
769 break;
770 case 0x0:
771 rx_pwr_all = 14 - ((pcck_buf->cck_agc_rpt &
772 0x1f) << 1);
773 break;
774 }
775 }
776 pwdb_all = query_rx_pwr_percentage(rx_pwr_all);
777 /* CCK gain is smaller than OFDM/MCS gain,*/
778 /* so we add gain diff by experiences, the val is 6 */
779 pwdb_all += 6;
780 if (pwdb_all > 100)
781 pwdb_all = 100;
782 /* modify the offset to make the same gain index with OFDM.*/
783 if (pwdb_all > 34 && pwdb_all <= 42)
784 pwdb_all -= 2;
785 else if (pwdb_all > 26 && pwdb_all <= 34)
786 pwdb_all -= 6;
787 else if (pwdb_all > 14 && pwdb_all <= 26)
788 pwdb_all -= 8;
789 else if (pwdb_all > 4 && pwdb_all <= 14)
790 pwdb_all -= 4;
791 /*
792 * (3) Get Signal Quality (EVM)
793 */
794 if (pwdb_all > 40) {
795 sq = 100;
796 } else {
797 sq = pcck_buf->sq_rpt;
798 if (pcck_buf->sq_rpt > 64)
799 sq = 0;
800 else if (pcck_buf->sq_rpt < 20)
801 sq = 100;
802 else
803 sq = ((64 - sq) * 100) / 44;
804 }
805 prframe->u.hdr.attrib.signal_qual = sq;
806 prframe->u.hdr.attrib.rx_mimo_signal_qual[0] = sq;
807 prframe->u.hdr.attrib.rx_mimo_signal_qual[1] = -1;
808 } else {
809 /* (1)Get RSSI for HT rate */
810 for (i = 0; i < ((padapter->registrypriv.rf_config) &
811 0x0f); i++) {
812 rf_rx_num++;
813 rx_pwr[i] = ((pphy_head[PHY_STAT_GAIN_TRSW_SHT + i]
814 & 0x3F) * 2) - 110;
815 /* Translate DBM to percentage. */
816 rssi = query_rx_pwr_percentage(rx_pwr[i]);
817 total_rssi += rssi;
818 }
819 /* (2)PWDB, Average PWDB calculated by hardware (for
820 * rate adaptive)
821 */
822 rx_pwr_all = (((pphy_head[PHY_STAT_PWDB_ALL_SHT]) >> 1) & 0x7f)
823 - 106;
824 pwdb_all = query_rx_pwr_percentage(rx_pwr_all);
825
826 {
827 /* (3)EVM of HT rate */
828 if (prframe->u.hdr.attrib.htc &&
829 prframe->u.hdr.attrib.mcs_rate >= 20 &&
830 prframe->u.hdr.attrib.mcs_rate <= 27) {
831 /* both spatial stream make sense */
832 max_spatial_stream = 2;
833 } else {
834 /* only spatial stream 1 makes sense */
835 max_spatial_stream = 1;
836 }
837 for (i = 0; i < max_spatial_stream; i++) {
838 evm = evm_db2percentage((pphy_head
839 [PHY_STAT_RXEVM_SHT + i]));/*dbm*/
840 prframe->u.hdr.attrib.signal_qual =
841 (u8)(evm & 0xff);
842 prframe->u.hdr.attrib.rx_mimo_signal_qual[i] =
843 (u8)(evm & 0xff);
844 }
845 }
846 }
847 /* UI BSS List signal strength(in percentage), make it good looking,
848 * from 0~100. It is assigned to the BSS List in
849 * GetValueFromBeaconOrProbeRsp().
850 */
851 if (bcck_rate) {
852 prframe->u.hdr.attrib.signal_strength =
853 (u8)r8712_signal_scale_mapping(pwdb_all);
854 } else {
855 if (rf_rx_num != 0)
856 prframe->u.hdr.attrib.signal_strength =
857 (u8)(r8712_signal_scale_mapping(total_rssi /=
858 rf_rx_num));
859 }
860 }
861
process_link_qual(struct _adapter * padapter,union recv_frame * prframe)862 static void process_link_qual(struct _adapter *padapter,
863 union recv_frame *prframe)
864 {
865 u32 last_evm = 0, tmpVal;
866 struct rx_pkt_attrib *pattrib;
867 struct smooth_rssi_data *sqd = &padapter->recvpriv.signal_qual_data;
868
869 if (!prframe || !padapter)
870 return;
871 pattrib = &prframe->u.hdr.attrib;
872 if (pattrib->signal_qual != 0) {
873 /*
874 * 1. Record the general EVM to the sliding window.
875 */
876 if (sqd->total_num++ >= PHY_LINKQUALITY_SLID_WIN_MAX) {
877 sqd->total_num = PHY_LINKQUALITY_SLID_WIN_MAX;
878 last_evm = sqd->elements[sqd->index];
879 sqd->total_val -= last_evm;
880 }
881 sqd->total_val += pattrib->signal_qual;
882 sqd->elements[sqd->index++] = pattrib->signal_qual;
883 if (sqd->index >= PHY_LINKQUALITY_SLID_WIN_MAX)
884 sqd->index = 0;
885
886 /* <1> Showed on UI for user, in percentage. */
887 tmpVal = sqd->total_val / sqd->total_num;
888 padapter->recvpriv.signal = (u8)tmpVal;
889 }
890 }
891
process_rssi(struct _adapter * padapter,union recv_frame * prframe)892 static void process_rssi(struct _adapter *padapter, union recv_frame *prframe)
893 {
894 u32 last_rssi, tmp_val;
895 struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
896 struct smooth_rssi_data *ssd = &padapter->recvpriv.signal_strength_data;
897
898 if (ssd->total_num++ >= PHY_RSSI_SLID_WIN_MAX) {
899 ssd->total_num = PHY_RSSI_SLID_WIN_MAX;
900 last_rssi = ssd->elements[ssd->index];
901 ssd->total_val -= last_rssi;
902 }
903 ssd->total_val += pattrib->signal_strength;
904 ssd->elements[ssd->index++] = pattrib->signal_strength;
905 if (ssd->index >= PHY_RSSI_SLID_WIN_MAX)
906 ssd->index = 0;
907 tmp_val = ssd->total_val / ssd->total_num;
908 padapter->recvpriv.rssi = (s8)translate2dbm(padapter, (u8)tmp_val);
909 }
910
process_phy_info(struct _adapter * padapter,union recv_frame * prframe)911 static void process_phy_info(struct _adapter *padapter,
912 union recv_frame *prframe)
913 {
914 query_rx_phy_status(padapter, prframe);
915 process_rssi(padapter, prframe);
916 process_link_qual(padapter, prframe);
917 }
918
recv_func(struct _adapter * padapter,void * pcontext)919 int recv_func(struct _adapter *padapter, void *pcontext)
920 {
921 struct rx_pkt_attrib *pattrib;
922 union recv_frame *prframe, *orig_prframe;
923 int retval = _SUCCESS;
924 struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
925 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
926
927 prframe = pcontext;
928 orig_prframe = prframe;
929 pattrib = &prframe->u.hdr.attrib;
930 if (check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
931 if (pattrib->crc_err == 1)
932 padapter->mppriv.rx_crcerrpktcount++;
933 else
934 padapter->mppriv.rx_pktcount++;
935 if (!check_fwstate(pmlmepriv, WIFI_MP_LPBK_STATE)) {
936 /* free this recv_frame */
937 r8712_free_recvframe(orig_prframe, pfree_recv_queue);
938 goto _exit_recv_func;
939 }
940 }
941 /* check the frame crtl field and decache */
942 retval = r8712_validate_recv_frame(padapter, prframe);
943 if (retval != _SUCCESS) {
944 /* free this recv_frame */
945 r8712_free_recvframe(orig_prframe, pfree_recv_queue);
946 goto _exit_recv_func;
947 }
948 process_phy_info(padapter, prframe);
949 prframe = r8712_decryptor(padapter, prframe);
950 if (!prframe) {
951 retval = _FAIL;
952 goto _exit_recv_func;
953 }
954 prframe = r8712_recvframe_chk_defrag(padapter, prframe);
955 if (!prframe)
956 goto _exit_recv_func;
957 prframe = r8712_portctrl(padapter, prframe);
958 if (!prframe) {
959 retval = _FAIL;
960 goto _exit_recv_func;
961 }
962 retval = r8712_process_recv_indicatepkts(padapter, prframe);
963 if (retval != _SUCCESS) {
964 r8712_free_recvframe(orig_prframe, pfree_recv_queue);
965 goto _exit_recv_func;
966 }
967 _exit_recv_func:
968 return retval;
969 }
970
recvbuf2recvframe(struct _adapter * padapter,struct sk_buff * pskb)971 static void recvbuf2recvframe(struct _adapter *padapter, struct sk_buff *pskb)
972 {
973 u8 *pbuf, shift_sz = 0;
974 u8 frag, mf;
975 uint pkt_len;
976 u32 transfer_len;
977 struct recv_stat *prxstat;
978 u16 pkt_cnt, drvinfo_sz, pkt_offset, tmp_len, alloc_sz;
979 struct __queue *pfree_recv_queue;
980 _pkt *pkt_copy = NULL;
981 union recv_frame *precvframe = NULL;
982 struct recv_priv *precvpriv = &padapter->recvpriv;
983
984 pfree_recv_queue = &precvpriv->free_recv_queue;
985 pbuf = pskb->data;
986 prxstat = (struct recv_stat *)pbuf;
987 pkt_cnt = (le32_to_cpu(prxstat->rxdw2) >> 16) & 0xff;
988 pkt_len = le32_to_cpu(prxstat->rxdw0) & 0x00003fff;
989 transfer_len = pskb->len;
990 /* Test throughput with Netgear 3700 (No security) with Chariot 3T3R
991 * pairs. The packet count will be a big number so that the containing
992 * packet will effect the Rx reordering.
993 */
994 if (transfer_len < pkt_len) {
995 /* In this case, it means the MAX_RECVBUF_SZ is too small to
996 * get the data from 8712u.
997 */
998 return;
999 }
1000 do {
1001 prxstat = (struct recv_stat *)pbuf;
1002 pkt_len = le32_to_cpu(prxstat->rxdw0) & 0x00003fff;
1003 /* more fragment bit */
1004 mf = (le32_to_cpu(prxstat->rxdw1) >> 27) & 0x1;
1005 /* ragmentation number */
1006 frag = (le32_to_cpu(prxstat->rxdw2) >> 12) & 0xf;
1007 /* uint 2^3 = 8 bytes */
1008 drvinfo_sz = (le32_to_cpu(prxstat->rxdw0) & 0x000f0000) >> 16;
1009 drvinfo_sz <<= 3;
1010 if (pkt_len <= 0)
1011 return;
1012 /* Qos data, wireless lan header length is 26 */
1013 if ((le32_to_cpu(prxstat->rxdw0) >> 23) & 0x01)
1014 shift_sz = 2;
1015 precvframe = r8712_alloc_recvframe(pfree_recv_queue);
1016 if (!precvframe)
1017 return;
1018 INIT_LIST_HEAD(&precvframe->u.hdr.list);
1019 precvframe->u.hdr.precvbuf = NULL; /*can't access the precvbuf*/
1020 precvframe->u.hdr.len = 0;
1021 tmp_len = pkt_len + drvinfo_sz + RXDESC_SIZE;
1022 pkt_offset = (u16)round_up(tmp_len, 128);
1023 /* for first fragment packet, driver need allocate 1536 +
1024 * drvinfo_sz + RXDESC_SIZE to defrag packet.
1025 */
1026 if ((mf == 1) && (frag == 0))
1027 /*1658+6=1664, 1664 is 128 alignment.*/
1028 alloc_sz = max_t(u16, tmp_len, 1658);
1029 else
1030 alloc_sz = tmp_len;
1031 /* 2 is for IP header 4 bytes alignment in QoS packet case.
1032 * 4 is for skb->data 4 bytes alignment.
1033 */
1034 alloc_sz += 6;
1035 pkt_copy = netdev_alloc_skb(padapter->pnetdev, alloc_sz);
1036 if (!pkt_copy)
1037 return;
1038
1039 precvframe->u.hdr.pkt = pkt_copy;
1040 skb_reserve(pkt_copy, 4 - ((addr_t)(pkt_copy->data) % 4));
1041 skb_reserve(pkt_copy, shift_sz);
1042 memcpy(pkt_copy->data, pbuf, tmp_len);
1043 precvframe->u.hdr.rx_head = pkt_copy->data;
1044 precvframe->u.hdr.rx_data = pkt_copy->data;
1045 precvframe->u.hdr.rx_tail = pkt_copy->data;
1046 precvframe->u.hdr.rx_end = pkt_copy->data + alloc_sz;
1047
1048 recvframe_put(precvframe, tmp_len);
1049 recvframe_pull(precvframe, drvinfo_sz + RXDESC_SIZE);
1050 /* because the endian issue, driver avoid reference to the
1051 * rxstat after calling update_recvframe_attrib_from_recvstat();
1052 */
1053 update_recvframe_attrib_from_recvstat(&precvframe->u.hdr.attrib,
1054 prxstat);
1055 r8712_recv_entry(precvframe);
1056 transfer_len -= pkt_offset;
1057 pbuf += pkt_offset;
1058 pkt_cnt--;
1059 precvframe = NULL;
1060 pkt_copy = NULL;
1061 } while ((transfer_len > 0) && pkt_cnt > 0);
1062 }
1063
recv_tasklet(struct tasklet_struct * t)1064 static void recv_tasklet(struct tasklet_struct *t)
1065 {
1066 struct sk_buff *pskb;
1067 struct _adapter *padapter = from_tasklet(padapter, t,
1068 recvpriv.recv_tasklet);
1069 struct recv_priv *precvpriv = &padapter->recvpriv;
1070
1071 while (NULL != (pskb = skb_dequeue(&precvpriv->rx_skb_queue))) {
1072 recvbuf2recvframe(padapter, pskb);
1073 skb_reset_tail_pointer(pskb);
1074 pskb->len = 0;
1075 if (!skb_cloned(pskb))
1076 skb_queue_tail(&precvpriv->free_recv_skb_queue, pskb);
1077 else
1078 consume_skb(pskb);
1079 }
1080 }
1081