1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#define _RTL8712_RECV_C_
30
31#include <linux/if_ether.h>
32#include <linux/ip.h>
33
34#include "osdep_service.h"
35#include "drv_types.h"
36#include "recv_osdep.h"
37#include "mlme_osdep.h"
38#include "ethernet.h"
39#include "usb_ops.h"
40#include "wifi.h"
41
42
43static u8 bridge_tunnel_header[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8};
44
45
46static u8 rfc1042_header[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
47
48static void recv_tasklet(void *priv);
49
50int r8712_init_recv_priv(struct recv_priv *precvpriv, struct _adapter *padapter)
51{
52 int i;
53 struct recv_buf *precvbuf;
54 int res = _SUCCESS;
55 addr_t tmpaddr = 0;
56 int alignment = 0;
57 struct sk_buff *pskb = NULL;
58
59
60 _init_queue(&precvpriv->free_recv_buf_queue);
61 precvpriv->pallocated_recv_buf =
62 kzalloc(NR_RECVBUFF * sizeof(struct recv_buf) + 4, GFP_ATOMIC);
63 if (!precvpriv->pallocated_recv_buf)
64 return _FAIL;
65 precvpriv->precv_buf = precvpriv->pallocated_recv_buf + 4 -
66 ((addr_t) (precvpriv->pallocated_recv_buf) & 3);
67 precvbuf = (struct recv_buf *)precvpriv->precv_buf;
68 for (i = 0; i < NR_RECVBUFF; i++) {
69 INIT_LIST_HEAD(&precvbuf->list);
70 spin_lock_init(&precvbuf->recvbuf_lock);
71 res = r8712_os_recvbuf_resource_alloc(padapter, precvbuf);
72 if (res == _FAIL)
73 break;
74 precvbuf->ref_cnt = 0;
75 precvbuf->adapter = padapter;
76 list_add_tail(&precvbuf->list,
77 &(precvpriv->free_recv_buf_queue.queue));
78 precvbuf++;
79 }
80 precvpriv->free_recv_buf_queue_cnt = NR_RECVBUFF;
81 tasklet_init(&precvpriv->recv_tasklet,
82 (void(*)(unsigned long))recv_tasklet,
83 (unsigned long)padapter);
84 skb_queue_head_init(&precvpriv->rx_skb_queue);
85
86 skb_queue_head_init(&precvpriv->free_recv_skb_queue);
87 for (i = 0; i < NR_PREALLOC_RECV_SKB; i++) {
88 pskb = netdev_alloc_skb(padapter->pnetdev, MAX_RECVBUF_SZ +
89 RECVBUFF_ALIGN_SZ);
90 if (pskb) {
91 tmpaddr = (addr_t)pskb->data;
92 alignment = tmpaddr & (RECVBUFF_ALIGN_SZ - 1);
93 skb_reserve(pskb, (RECVBUFF_ALIGN_SZ - alignment));
94 skb_queue_tail(&precvpriv->free_recv_skb_queue, pskb);
95 }
96 pskb = NULL;
97 }
98 return res;
99}
100
101void r8712_free_recv_priv(struct recv_priv *precvpriv)
102{
103 int i;
104 struct recv_buf *precvbuf;
105 struct _adapter *padapter = precvpriv->adapter;
106
107 precvbuf = (struct recv_buf *)precvpriv->precv_buf;
108 for (i = 0; i < NR_RECVBUFF; i++) {
109 r8712_os_recvbuf_resource_free(padapter, precvbuf);
110 precvbuf++;
111 }
112 kfree(precvpriv->pallocated_recv_buf);
113 skb_queue_purge(&precvpriv->rx_skb_queue);
114 if (skb_queue_len(&precvpriv->rx_skb_queue))
115 netdev_warn(padapter->pnetdev, "r8712u: rx_skb_queue not empty\n");
116 skb_queue_purge(&precvpriv->free_recv_skb_queue);
117 if (skb_queue_len(&precvpriv->free_recv_skb_queue))
118 netdev_warn(padapter->pnetdev, "r8712u: free_recv_skb_queue not empty %d\n",
119 skb_queue_len(&precvpriv->free_recv_skb_queue));
120}
121
122int r8712_init_recvbuf(struct _adapter *padapter, struct recv_buf *precvbuf)
123{
124 precvbuf->transfer_len = 0;
125 precvbuf->len = 0;
126 precvbuf->ref_cnt = 0;
127 if (precvbuf->pbuf) {
128 precvbuf->pdata = precvbuf->pbuf;
129 precvbuf->phead = precvbuf->pbuf;
130 precvbuf->ptail = precvbuf->pbuf;
131 precvbuf->pend = precvbuf->pdata + MAX_RECVBUF_SZ;
132 }
133 return _SUCCESS;
134}
135
136int r8712_free_recvframe(union recv_frame *precvframe,
137 struct __queue *pfree_recv_queue)
138{
139 unsigned long irqL;
140 struct _adapter *padapter = precvframe->u.hdr.adapter;
141 struct recv_priv *precvpriv = &padapter->recvpriv;
142
143 if (precvframe->u.hdr.pkt) {
144 dev_kfree_skb_any(precvframe->u.hdr.pkt);
145 precvframe->u.hdr.pkt = NULL;
146 }
147 spin_lock_irqsave(&pfree_recv_queue->lock, irqL);
148 list_del_init(&(precvframe->u.hdr.list));
149 list_add_tail(&(precvframe->u.hdr.list), &pfree_recv_queue->queue);
150 if (padapter != NULL) {
151 if (pfree_recv_queue == &precvpriv->free_recv_queue)
152 precvpriv->free_recvframe_cnt++;
153 }
154 spin_unlock_irqrestore(&pfree_recv_queue->lock, irqL);
155 return _SUCCESS;
156}
157
158static void update_recvframe_attrib_from_recvstat(struct rx_pkt_attrib *pattrib,
159 struct recv_stat *prxstat)
160{
161 u16 drvinfo_sz;
162
163 drvinfo_sz = (le32_to_cpu(prxstat->rxdw0) & 0x000f0000) >> 16;
164 drvinfo_sz <<= 3;
165
166
167
168 pattrib->bdecrypted = ((le32_to_cpu(prxstat->rxdw0) & BIT(27)) >> 27)
169 ? 0 : 1;
170 pattrib->crc_err = (le32_to_cpu(prxstat->rxdw0) & BIT(14)) >> 14;
171
172
173
174 if (le32_to_cpu(prxstat->rxdw3) & BIT(13)) {
175 pattrib->tcpchk_valid = 1;
176 if (le32_to_cpu(prxstat->rxdw3) & BIT(11))
177 pattrib->tcp_chkrpt = 1;
178 else
179 pattrib->tcp_chkrpt = 0;
180 if (le32_to_cpu(prxstat->rxdw3) & BIT(12))
181 pattrib->ip_chkrpt = 1;
182 else
183 pattrib->ip_chkrpt = 0;
184 } else {
185 pattrib->tcpchk_valid = 0;
186 }
187 pattrib->mcs_rate = (u8)((le32_to_cpu(prxstat->rxdw3)) & 0x3f);
188 pattrib->htc = (u8)((le32_to_cpu(prxstat->rxdw3) >> 14) & 0x1);
189
190
191
192}
193
194
195static union recv_frame *recvframe_defrag(struct _adapter *adapter,
196 struct __queue *defrag_q)
197{
198 struct list_head *plist, *phead;
199 u8 wlanhdr_offset;
200 u8 curfragnum;
201 struct recv_frame_hdr *pfhdr, *pnfhdr;
202 union recv_frame *prframe, *pnextrframe;
203 struct __queue *pfree_recv_queue;
204
205 pfree_recv_queue = &adapter->recvpriv.free_recv_queue;
206 phead = &defrag_q->queue;
207 plist = phead->next;
208 prframe = container_of(plist, union recv_frame, u.list);
209 list_del_init(&prframe->u.list);
210 pfhdr = &prframe->u.hdr;
211 curfragnum = 0;
212 if (curfragnum != pfhdr->attrib.frag_num) {
213
214
215
216 r8712_free_recvframe(prframe, pfree_recv_queue);
217 r8712_free_recvframe_queue(defrag_q, pfree_recv_queue);
218 return NULL;
219 }
220 curfragnum++;
221 plist = &defrag_q->queue;
222 plist = plist->next;
223 while (!end_of_queue_search(phead, plist)) {
224 pnextrframe = container_of(plist, union recv_frame, u.list);
225 pnfhdr = &pnextrframe->u.hdr;
226
227 if (curfragnum != pnfhdr->attrib.frag_num) {
228
229
230
231 r8712_free_recvframe(prframe, pfree_recv_queue);
232 r8712_free_recvframe_queue(defrag_q, pfree_recv_queue);
233 return NULL;
234 }
235 curfragnum++;
236
237
238
239 wlanhdr_offset = pnfhdr->attrib.hdrlen + pnfhdr->attrib.iv_len;
240 recvframe_pull(pnextrframe, wlanhdr_offset);
241
242
243
244 recvframe_pull_tail(prframe, pfhdr->attrib.icv_len);
245 memcpy(pfhdr->rx_tail, pnfhdr->rx_data, pnfhdr->len);
246 recvframe_put(prframe, pnfhdr->len);
247 pfhdr->attrib.icv_len = pnfhdr->attrib.icv_len;
248 plist = plist->next;
249 }
250
251 r8712_free_recvframe_queue(defrag_q, pfree_recv_queue);
252 return prframe;
253}
254
255
256union recv_frame *r8712_recvframe_chk_defrag(struct _adapter *padapter,
257 union recv_frame *precv_frame)
258{
259 u8 ismfrag;
260 u8 fragnum;
261 u8 *psta_addr;
262 struct recv_frame_hdr *pfhdr;
263 struct sta_info *psta;
264 struct sta_priv *pstapriv;
265 struct list_head *phead;
266 union recv_frame *prtnframe = NULL;
267 struct __queue *pfree_recv_queue, *pdefrag_q;
268
269 pstapriv = &padapter->stapriv;
270 pfhdr = &precv_frame->u.hdr;
271 pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
272
273 ismfrag = pfhdr->attrib.mfrag;
274 fragnum = pfhdr->attrib.frag_num;
275 psta_addr = pfhdr->attrib.ta;
276 psta = r8712_get_stainfo(pstapriv, psta_addr);
277 if (!psta)
278 pdefrag_q = NULL;
279 else
280 pdefrag_q = &psta->sta_recvpriv.defrag_q;
281
282 if ((ismfrag == 0) && (fragnum == 0))
283 prtnframe = precv_frame;
284 if (ismfrag == 1) {
285
286
287
288 if (pdefrag_q != NULL) {
289 if (fragnum == 0) {
290
291 if (!list_empty(&pdefrag_q->queue)) {
292
293 r8712_free_recvframe_queue(pdefrag_q,
294 pfree_recv_queue);
295 }
296 }
297
298 phead = &pdefrag_q->queue;
299 list_add_tail(&pfhdr->list, phead);
300 prtnframe = NULL;
301 } else {
302
303
304
305 r8712_free_recvframe(precv_frame, pfree_recv_queue);
306 prtnframe = NULL;
307 }
308
309 }
310 if ((ismfrag == 0) && (fragnum != 0)) {
311
312
313
314 if (pdefrag_q != NULL) {
315 phead = &pdefrag_q->queue;
316 list_add_tail(&pfhdr->list, phead);
317
318 precv_frame = recvframe_defrag(padapter, pdefrag_q);
319 prtnframe = precv_frame;
320 } else {
321
322
323
324 r8712_free_recvframe(precv_frame, pfree_recv_queue);
325 prtnframe = NULL;
326 }
327 }
328 if ((prtnframe != NULL) && (prtnframe->u.hdr.attrib.privacy)) {
329
330 if (r8712_recvframe_chkmic(padapter, prtnframe) == _FAIL) {
331 r8712_free_recvframe(prtnframe, pfree_recv_queue);
332 prtnframe = NULL;
333 }
334 }
335 return prtnframe;
336}
337
338static int amsdu_to_msdu(struct _adapter *padapter, union recv_frame *prframe)
339{
340 int a_len, padding_len;
341 u16 eth_type, nSubframe_Length;
342 u8 nr_subframes, i;
343 unsigned char *data_ptr, *pdata;
344 struct rx_pkt_attrib *pattrib;
345 _pkt *sub_skb, *subframes[MAX_SUBFRAME_COUNT];
346 struct recv_priv *precvpriv = &padapter->recvpriv;
347 struct __queue *pfree_recv_queue = &(precvpriv->free_recv_queue);
348
349 nr_subframes = 0;
350 pattrib = &prframe->u.hdr.attrib;
351 recvframe_pull(prframe, prframe->u.hdr.attrib.hdrlen);
352 if (prframe->u.hdr.attrib.iv_len > 0)
353 recvframe_pull(prframe, prframe->u.hdr.attrib.iv_len);
354 a_len = prframe->u.hdr.len;
355 pdata = prframe->u.hdr.rx_data;
356 while (a_len > ETH_HLEN) {
357
358 nSubframe_Length = *((u16 *)(pdata + 12));
359
360 nSubframe_Length = (nSubframe_Length >> 8) +
361 (nSubframe_Length << 8);
362 if (a_len < (ETHERNET_HEADER_SIZE + nSubframe_Length)) {
363 netdev_warn(padapter->pnetdev, "r8712u: nRemain_Length is %d and nSubframe_Length is: %d\n",
364 a_len, nSubframe_Length);
365 goto exit;
366 }
367
368 pdata += ETH_HLEN;
369 a_len -= ETH_HLEN;
370
371 sub_skb = dev_alloc_skb(nSubframe_Length + 12);
372 if (!sub_skb)
373 break;
374 skb_reserve(sub_skb, 12);
375 data_ptr = (u8 *)skb_put(sub_skb, nSubframe_Length);
376 memcpy(data_ptr, pdata, nSubframe_Length);
377 subframes[nr_subframes++] = sub_skb;
378 if (nr_subframes >= MAX_SUBFRAME_COUNT) {
379 netdev_warn(padapter->pnetdev, "r8712u: ParseSubframe(): Too many Subframes! Packets dropped!\n");
380 break;
381 }
382 pdata += nSubframe_Length;
383 a_len -= nSubframe_Length;
384 if (a_len != 0) {
385 padding_len = 4 - ((nSubframe_Length + ETH_HLEN) & 3);
386 if (padding_len == 4)
387 padding_len = 0;
388 if (a_len < padding_len)
389 goto exit;
390 pdata += padding_len;
391 a_len -= padding_len;
392 }
393 }
394 for (i = 0; i < nr_subframes; i++) {
395 sub_skb = subframes[i];
396
397 eth_type = (sub_skb->data[6] << 8) | sub_skb->data[7];
398 if (sub_skb->len >= 8 &&
399 ((!memcmp(sub_skb->data, rfc1042_header, SNAP_SIZE) &&
400 eth_type != ETH_P_AARP && eth_type != ETH_P_IPX) ||
401 !memcmp(sub_skb->data, bridge_tunnel_header, SNAP_SIZE))) {
402
403
404
405 skb_pull(sub_skb, SNAP_SIZE);
406 memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src,
407 ETH_ALEN);
408 memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst,
409 ETH_ALEN);
410 } else {
411 u16 len;
412
413 len = htons(sub_skb->len);
414 memcpy(skb_push(sub_skb, 2), &len, 2);
415 memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src,
416 ETH_ALEN);
417 memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst,
418 ETH_ALEN);
419 }
420
421 if (sub_skb) {
422 sub_skb->protocol =
423 eth_type_trans(sub_skb, padapter->pnetdev);
424 sub_skb->dev = padapter->pnetdev;
425 if ((pattrib->tcpchk_valid == 1) &&
426 (pattrib->tcp_chkrpt == 1)) {
427 sub_skb->ip_summed = CHECKSUM_UNNECESSARY;
428 } else {
429 sub_skb->ip_summed = CHECKSUM_NONE;
430 }
431 netif_rx(sub_skb);
432 }
433 }
434exit:
435 prframe->u.hdr.len = 0;
436 r8712_free_recvframe(prframe, pfree_recv_queue);
437 return _SUCCESS;
438}
439
440void r8712_rxcmd_event_hdl(struct _adapter *padapter, void *prxcmdbuf)
441{
442 uint voffset;
443 u8 *poffset;
444 u16 cmd_len, drvinfo_sz;
445 struct recv_stat *prxstat;
446
447 poffset = (u8 *)prxcmdbuf;
448 voffset = *(uint *)poffset;
449 prxstat = (struct recv_stat *)prxcmdbuf;
450 drvinfo_sz = (le32_to_cpu(prxstat->rxdw0) & 0x000f0000) >> 16;
451 drvinfo_sz <<= 3;
452 poffset += RXDESC_SIZE + drvinfo_sz;
453 do {
454 voffset = *(uint *)poffset;
455 cmd_len = (u16)(le32_to_cpu(voffset) & 0xffff);
456 r8712_event_handle(padapter, (uint *)poffset);
457 poffset += (cmd_len + 8);
458 } while (le32_to_cpu(voffset) & BIT(31));
459
460}
461
462static int check_indicate_seq(struct recv_reorder_ctrl *preorder_ctrl,
463 u16 seq_num)
464{
465 u8 wsize = preorder_ctrl->wsize_b;
466 u16 wend = (preorder_ctrl->indicate_seq + wsize - 1) % 4096;
467
468
469 if (preorder_ctrl->indicate_seq == 0xffff)
470 preorder_ctrl->indicate_seq = seq_num;
471
472 if (SN_LESS(seq_num, preorder_ctrl->indicate_seq))
473 return false;
474
475
476
477
478
479 if (SN_EQUAL(seq_num, preorder_ctrl->indicate_seq))
480 preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq +
481 1) % 4096;
482 else if (SN_LESS(wend, seq_num)) {
483 if (seq_num >= (wsize - 1))
484 preorder_ctrl->indicate_seq = seq_num + 1 - wsize;
485 else
486 preorder_ctrl->indicate_seq = 4095 - (wsize -
487 (seq_num + 1)) + 1;
488 }
489 return true;
490}
491
492static int enqueue_reorder_recvframe(struct recv_reorder_ctrl *preorder_ctrl,
493 union recv_frame *prframe)
494{
495 struct list_head *phead, *plist;
496 union recv_frame *pnextrframe;
497 struct rx_pkt_attrib *pnextattrib;
498 struct __queue *ppending_recvframe_queue =
499 &preorder_ctrl->pending_recvframe_queue;
500 struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
501
502 phead = &ppending_recvframe_queue->queue;
503 plist = phead->next;
504 while (!end_of_queue_search(phead, plist)) {
505 pnextrframe = container_of(plist, union recv_frame, u.list);
506 pnextattrib = &pnextrframe->u.hdr.attrib;
507 if (SN_LESS(pnextattrib->seq_num, pattrib->seq_num))
508 plist = plist->next;
509 else if (SN_EQUAL(pnextattrib->seq_num, pattrib->seq_num))
510 return false;
511 else
512 break;
513 }
514 list_del_init(&(prframe->u.hdr.list));
515 list_add_tail(&(prframe->u.hdr.list), plist);
516 return true;
517}
518
519int r8712_recv_indicatepkts_in_order(struct _adapter *padapter,
520 struct recv_reorder_ctrl *preorder_ctrl,
521 int bforced)
522{
523 struct list_head *phead, *plist;
524 union recv_frame *prframe;
525 struct rx_pkt_attrib *pattrib;
526 int bPktInBuf = false;
527 struct recv_priv *precvpriv = &padapter->recvpriv;
528 struct __queue *ppending_recvframe_queue =
529 &preorder_ctrl->pending_recvframe_queue;
530
531 phead = &ppending_recvframe_queue->queue;
532 plist = phead->next;
533
534 if (bforced) {
535 if (list_empty(phead))
536 return true;
537
538 prframe = container_of(plist, union recv_frame, u.list);
539 pattrib = &prframe->u.hdr.attrib;
540 preorder_ctrl->indicate_seq = pattrib->seq_num;
541 }
542
543
544
545 while (!list_empty(phead)) {
546 prframe = container_of(plist, union recv_frame, u.list);
547 pattrib = &prframe->u.hdr.attrib;
548 if (!SN_LESS(preorder_ctrl->indicate_seq, pattrib->seq_num)) {
549 plist = plist->next;
550 list_del_init(&(prframe->u.hdr.list));
551 if (SN_EQUAL(preorder_ctrl->indicate_seq,
552 pattrib->seq_num))
553 preorder_ctrl->indicate_seq =
554 (preorder_ctrl->indicate_seq + 1) % 4096;
555
556 if (!pattrib->amsdu) {
557 if (!padapter->bDriverStopped &&
558 !padapter->bSurpriseRemoved) {
559
560 r8712_recv_indicatepkt(padapter,
561 prframe);
562 }
563 } else if (pattrib->amsdu == 1) {
564 if (amsdu_to_msdu(padapter, prframe) !=
565 _SUCCESS)
566 r8712_free_recvframe(prframe,
567 &precvpriv->free_recv_queue);
568 }
569
570 bPktInBuf = false;
571 } else {
572 bPktInBuf = true;
573 break;
574 }
575 }
576 return bPktInBuf;
577}
578
579static int recv_indicatepkt_reorder(struct _adapter *padapter,
580 union recv_frame *prframe)
581{
582 unsigned long irql;
583 struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
584 struct recv_reorder_ctrl *preorder_ctrl = prframe->u.hdr.preorder_ctrl;
585 struct __queue *ppending_recvframe_queue =
586 &preorder_ctrl->pending_recvframe_queue;
587
588 if (!pattrib->amsdu) {
589
590 r8712_wlanhdr_to_ethhdr(prframe);
591 if (pattrib->qos != 1) {
592 if (!padapter->bDriverStopped &&
593 !padapter->bSurpriseRemoved) {
594 r8712_recv_indicatepkt(padapter, prframe);
595 return _SUCCESS;
596 } else {
597 return _FAIL;
598 }
599 }
600 }
601 spin_lock_irqsave(&ppending_recvframe_queue->lock, irql);
602
603 if (!check_indicate_seq(preorder_ctrl, pattrib->seq_num))
604 goto _err_exit;
605
606 if (!enqueue_reorder_recvframe(preorder_ctrl, prframe))
607 goto _err_exit;
608
609
610
611
612
613
614
615
616
617
618
619 if (r8712_recv_indicatepkts_in_order(padapter, preorder_ctrl, false) ==
620 true) {
621 mod_timer(&preorder_ctrl->reordering_ctrl_timer,
622 jiffies + msecs_to_jiffies(REORDER_WAIT_TIME));
623 spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql);
624 } else {
625 spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql);
626 del_timer(&preorder_ctrl->reordering_ctrl_timer);
627 }
628 return _SUCCESS;
629_err_exit:
630 spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql);
631 return _FAIL;
632}
633
634void r8712_reordering_ctrl_timeout_handler(void *pcontext)
635{
636 unsigned long irql;
637 struct recv_reorder_ctrl *preorder_ctrl =
638 (struct recv_reorder_ctrl *)pcontext;
639 struct _adapter *padapter = preorder_ctrl->padapter;
640 struct __queue *ppending_recvframe_queue =
641 &preorder_ctrl->pending_recvframe_queue;
642
643 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
644 return;
645 spin_lock_irqsave(&ppending_recvframe_queue->lock, irql);
646 r8712_recv_indicatepkts_in_order(padapter, preorder_ctrl, true);
647 spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql);
648}
649
650static int r8712_process_recv_indicatepkts(struct _adapter *padapter,
651 union recv_frame *prframe)
652{
653 int retval = _SUCCESS;
654 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
655 struct ht_priv *phtpriv = &pmlmepriv->htpriv;
656
657 if (phtpriv->ht_option == 1) {
658 if (recv_indicatepkt_reorder(padapter, prframe) != _SUCCESS) {
659
660 if (!padapter->bDriverStopped &&
661 !padapter->bSurpriseRemoved)
662 return _FAIL;
663 }
664 } else {
665 retval = r8712_wlanhdr_to_ethhdr(prframe);
666 if (retval != _SUCCESS)
667 return retval;
668 if (!padapter->bDriverStopped && !padapter->bSurpriseRemoved) {
669
670 r8712_recv_indicatepkt(padapter, prframe);
671 } else {
672 return _FAIL;
673 }
674 }
675 return retval;
676}
677
678static u8 query_rx_pwr_percentage(s8 antpower)
679{
680 if ((antpower <= -100) || (antpower >= 20))
681 return 0;
682 else if (antpower >= 0)
683 return 100;
684 else
685 return 100 + antpower;
686}
687
688static u8 evm_db2percentage(s8 value)
689{
690
691
692
693 s8 ret_val;
694
695 ret_val = value;
696 if (ret_val >= 0)
697 ret_val = 0;
698 if (ret_val <= -33)
699 ret_val = -33;
700 ret_val = -ret_val;
701 ret_val *= 3;
702 if (ret_val == 99)
703 ret_val = 100;
704 return ret_val;
705}
706
707s32 r8712_signal_scale_mapping(s32 cur_sig)
708{
709 s32 ret_sig;
710
711 if (cur_sig >= 51 && cur_sig <= 100)
712 ret_sig = 100;
713 else if (cur_sig >= 41 && cur_sig <= 50)
714 ret_sig = 80 + ((cur_sig - 40) * 2);
715 else if (cur_sig >= 31 && cur_sig <= 40)
716 ret_sig = 66 + (cur_sig - 30);
717 else if (cur_sig >= 21 && cur_sig <= 30)
718 ret_sig = 54 + (cur_sig - 20);
719 else if (cur_sig >= 10 && cur_sig <= 20)
720 ret_sig = 42 + (((cur_sig - 10) * 2) / 3);
721 else if (cur_sig >= 5 && cur_sig <= 9)
722 ret_sig = 22 + (((cur_sig - 5) * 3) / 2);
723 else if (cur_sig >= 1 && cur_sig <= 4)
724 ret_sig = 6 + (((cur_sig - 1) * 3) / 2);
725 else
726 ret_sig = cur_sig;
727 return ret_sig;
728}
729
730static s32 translate2dbm(struct _adapter *padapter, u8 signal_strength_idx)
731{
732 s32 signal_power;
733
734 signal_power = (s32)((signal_strength_idx + 1) >> 1);
735 signal_power -= 95;
736 return signal_power;
737}
738
739static void query_rx_phy_status(struct _adapter *padapter,
740 union recv_frame *prframe)
741{
742 u8 i, max_spatial_stream, evm;
743 struct recv_stat *prxstat = (struct recv_stat *)prframe->u.hdr.rx_head;
744 struct phy_stat *pphy_stat = (struct phy_stat *)(prxstat + 1);
745 u8 *pphy_head = (u8 *)(prxstat + 1);
746 s8 rx_pwr[4], rx_pwr_all;
747 u8 pwdb_all;
748 u32 rssi, total_rssi = 0;
749 u8 bcck_rate = 0, rf_rx_num = 0, cck_highpwr = 0;
750 struct phy_cck_rx_status *pcck_buf;
751 u8 sq;
752
753
754 bcck_rate = (prframe->u.hdr.attrib.mcs_rate <= 3 ? 1 : 0);
755 if (bcck_rate) {
756 u8 report;
757
758
759 pcck_buf = (struct phy_cck_rx_status *)pphy_stat;
760
761
762
763
764 if (!cck_highpwr) {
765 report = pcck_buf->cck_agc_rpt & 0xc0;
766 report >>= 6;
767 switch (report) {
768
769
770
771
772
773 case 0x3:
774 rx_pwr_all = -40 - (pcck_buf->cck_agc_rpt &
775 0x3e);
776 break;
777 case 0x2:
778 rx_pwr_all = -20 - (pcck_buf->cck_agc_rpt &
779 0x3e);
780 break;
781 case 0x1:
782 rx_pwr_all = -2 - (pcck_buf->cck_agc_rpt &
783 0x3e);
784 break;
785 case 0x0:
786 rx_pwr_all = 14 - (pcck_buf->cck_agc_rpt &
787 0x3e);
788 break;
789 }
790 } else {
791 report = ((u8)(le32_to_cpu(pphy_stat->phydw1) >> 8)) &
792 0x60;
793 report >>= 5;
794 switch (report) {
795 case 0x3:
796 rx_pwr_all = -40 - ((pcck_buf->cck_agc_rpt &
797 0x1f) << 1);
798 break;
799 case 0x2:
800 rx_pwr_all = -20 - ((pcck_buf->cck_agc_rpt &
801 0x1f) << 1);
802 break;
803 case 0x1:
804 rx_pwr_all = -2 - ((pcck_buf->cck_agc_rpt &
805 0x1f) << 1);
806 break;
807 case 0x0:
808 rx_pwr_all = 14 - ((pcck_buf->cck_agc_rpt &
809 0x1f) << 1);
810 break;
811 }
812 }
813 pwdb_all = query_rx_pwr_percentage(rx_pwr_all);
814
815
816 pwdb_all += 6;
817 if (pwdb_all > 100)
818 pwdb_all = 100;
819
820 if (pwdb_all > 34 && pwdb_all <= 42)
821 pwdb_all -= 2;
822 else if (pwdb_all > 26 && pwdb_all <= 34)
823 pwdb_all -= 6;
824 else if (pwdb_all > 14 && pwdb_all <= 26)
825 pwdb_all -= 8;
826 else if (pwdb_all > 4 && pwdb_all <= 14)
827 pwdb_all -= 4;
828
829
830
831 if (pwdb_all > 40) {
832 sq = 100;
833 } else {
834 sq = pcck_buf->sq_rpt;
835 if (pcck_buf->sq_rpt > 64)
836 sq = 0;
837 else if (pcck_buf->sq_rpt < 20)
838 sq = 100;
839 else
840 sq = ((64 - sq) * 100) / 44;
841 }
842 prframe->u.hdr.attrib.signal_qual = sq;
843 prframe->u.hdr.attrib.rx_mimo_signal_qual[0] = sq;
844 prframe->u.hdr.attrib.rx_mimo_signal_qual[1] = -1;
845 } else {
846
847 for (i = 0; i < ((padapter->registrypriv.rf_config) &
848 0x0f); i++) {
849 rf_rx_num++;
850 rx_pwr[i] = ((pphy_head[PHY_STAT_GAIN_TRSW_SHT + i]
851 & 0x3F) * 2) - 110;
852
853 rssi = query_rx_pwr_percentage(rx_pwr[i]);
854 total_rssi += rssi;
855 }
856
857
858
859 rx_pwr_all = (((pphy_head[PHY_STAT_PWDB_ALL_SHT]) >> 1) & 0x7f)
860 - 106;
861 pwdb_all = query_rx_pwr_percentage(rx_pwr_all);
862
863 {
864
865 if (prframe->u.hdr.attrib.htc &&
866 prframe->u.hdr.attrib.mcs_rate >= 20 &&
867 prframe->u.hdr.attrib.mcs_rate <= 27) {
868
869 max_spatial_stream = 2;
870 } else {
871
872 max_spatial_stream = 1;
873 }
874 for (i = 0; i < max_spatial_stream; i++) {
875 evm = evm_db2percentage((pphy_head
876 [PHY_STAT_RXEVM_SHT + i]));
877 prframe->u.hdr.attrib.signal_qual =
878 (u8)(evm & 0xff);
879 prframe->u.hdr.attrib.rx_mimo_signal_qual[i] =
880 (u8)(evm & 0xff);
881 }
882 }
883 }
884
885
886
887
888 if (bcck_rate)
889 prframe->u.hdr.attrib.signal_strength =
890 (u8)r8712_signal_scale_mapping(pwdb_all);
891 else {
892 if (rf_rx_num != 0)
893 prframe->u.hdr.attrib.signal_strength =
894 (u8)(r8712_signal_scale_mapping(total_rssi /=
895 rf_rx_num));
896 }
897}
898
899static void process_link_qual(struct _adapter *padapter,
900 union recv_frame *prframe)
901{
902 u32 last_evm = 0, tmpVal;
903 struct rx_pkt_attrib *pattrib;
904
905 if (prframe == NULL || padapter == NULL)
906 return;
907 pattrib = &prframe->u.hdr.attrib;
908 if (pattrib->signal_qual != 0) {
909
910
911
912 if (padapter->recvpriv.signal_qual_data.total_num++ >=
913 PHY_LINKQUALITY_SLID_WIN_MAX) {
914 padapter->recvpriv.signal_qual_data.total_num =
915 PHY_LINKQUALITY_SLID_WIN_MAX;
916 last_evm = padapter->recvpriv.signal_qual_data.elements
917 [padapter->recvpriv.signal_qual_data.index];
918 padapter->recvpriv.signal_qual_data.total_val -=
919 last_evm;
920 }
921 padapter->recvpriv.signal_qual_data.total_val +=
922 pattrib->signal_qual;
923 padapter->recvpriv.signal_qual_data.elements[padapter->
924 recvpriv.signal_qual_data.index++] =
925 pattrib->signal_qual;
926 if (padapter->recvpriv.signal_qual_data.index >=
927 PHY_LINKQUALITY_SLID_WIN_MAX)
928 padapter->recvpriv.signal_qual_data.index = 0;
929
930
931 tmpVal = padapter->recvpriv.signal_qual_data.total_val /
932 padapter->recvpriv.signal_qual_data.total_num;
933 padapter->recvpriv.signal = (u8)tmpVal;
934 }
935}
936
937static void process_rssi(struct _adapter *padapter, union recv_frame *prframe)
938{
939 u32 last_rssi, tmp_val;
940 struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
941
942 if (padapter->recvpriv.signal_strength_data.total_num++ >=
943 PHY_RSSI_SLID_WIN_MAX) {
944 padapter->recvpriv.signal_strength_data.total_num =
945 PHY_RSSI_SLID_WIN_MAX;
946 last_rssi = padapter->recvpriv.signal_strength_data.elements
947 [padapter->recvpriv.signal_strength_data.index];
948 padapter->recvpriv.signal_strength_data.total_val -= last_rssi;
949 }
950 padapter->recvpriv.signal_strength_data.total_val +=
951 pattrib->signal_strength;
952 padapter->recvpriv.signal_strength_data.elements[padapter->recvpriv.
953 signal_strength_data.index++] =
954 pattrib->signal_strength;
955 if (padapter->recvpriv.signal_strength_data.index >=
956 PHY_RSSI_SLID_WIN_MAX)
957 padapter->recvpriv.signal_strength_data.index = 0;
958 tmp_val = padapter->recvpriv.signal_strength_data.total_val /
959 padapter->recvpriv.signal_strength_data.total_num;
960 padapter->recvpriv.rssi = (s8)translate2dbm(padapter, (u8)tmp_val);
961}
962
963static void process_phy_info(struct _adapter *padapter,
964 union recv_frame *prframe)
965{
966 query_rx_phy_status(padapter, prframe);
967 process_rssi(padapter, prframe);
968 process_link_qual(padapter, prframe);
969}
970
971int recv_func(struct _adapter *padapter, void *pcontext)
972{
973 struct rx_pkt_attrib *pattrib;
974 union recv_frame *prframe, *orig_prframe;
975 int retval = _SUCCESS;
976 struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
977 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
978
979 prframe = (union recv_frame *)pcontext;
980 orig_prframe = prframe;
981 pattrib = &prframe->u.hdr.attrib;
982 if (check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
983 if (pattrib->crc_err == 1)
984 padapter->mppriv.rx_crcerrpktcount++;
985 else
986 padapter->mppriv.rx_pktcount++;
987 if (!check_fwstate(pmlmepriv, WIFI_MP_LPBK_STATE)) {
988
989 r8712_free_recvframe(orig_prframe, pfree_recv_queue);
990 goto _exit_recv_func;
991 }
992 }
993
994 retval = r8712_validate_recv_frame(padapter, prframe);
995 if (retval != _SUCCESS) {
996
997 r8712_free_recvframe(orig_prframe, pfree_recv_queue);
998 goto _exit_recv_func;
999 }
1000 process_phy_info(padapter, prframe);
1001 prframe = r8712_decryptor(padapter, prframe);
1002 if (!prframe) {
1003 retval = _FAIL;
1004 goto _exit_recv_func;
1005 }
1006 prframe = r8712_recvframe_chk_defrag(padapter, prframe);
1007 if (!prframe)
1008 goto _exit_recv_func;
1009 prframe = r8712_portctrl(padapter, prframe);
1010 if (!prframe) {
1011 retval = _FAIL;
1012 goto _exit_recv_func;
1013 }
1014 retval = r8712_process_recv_indicatepkts(padapter, prframe);
1015 if (retval != _SUCCESS) {
1016 r8712_free_recvframe(orig_prframe, pfree_recv_queue);
1017 goto _exit_recv_func;
1018 }
1019_exit_recv_func:
1020 return retval;
1021}
1022
1023static int recvbuf2recvframe(struct _adapter *padapter, struct sk_buff *pskb)
1024{
1025 u8 *pbuf, shift_sz = 0;
1026 u8 frag, mf;
1027 uint pkt_len;
1028 u32 transfer_len;
1029 struct recv_stat *prxstat;
1030 u16 pkt_cnt, drvinfo_sz, pkt_offset, tmp_len, alloc_sz;
1031 struct __queue *pfree_recv_queue;
1032 _pkt *pkt_copy = NULL;
1033 union recv_frame *precvframe = NULL;
1034 struct recv_priv *precvpriv = &padapter->recvpriv;
1035
1036 pfree_recv_queue = &(precvpriv->free_recv_queue);
1037 pbuf = pskb->data;
1038 prxstat = (struct recv_stat *)pbuf;
1039 pkt_cnt = (le32_to_cpu(prxstat->rxdw2) >> 16) & 0xff;
1040 pkt_len = le32_to_cpu(prxstat->rxdw0) & 0x00003fff;
1041 transfer_len = pskb->len;
1042
1043
1044
1045
1046 if (transfer_len < pkt_len) {
1047
1048
1049
1050 return _FAIL;
1051 }
1052 do {
1053 prxstat = (struct recv_stat *)pbuf;
1054 pkt_len = le32_to_cpu(prxstat->rxdw0) & 0x00003fff;
1055
1056 mf = (le32_to_cpu(prxstat->rxdw1) >> 27) & 0x1;
1057
1058 frag = (le32_to_cpu(prxstat->rxdw2) >> 12) & 0xf;
1059
1060 drvinfo_sz = (le32_to_cpu(prxstat->rxdw0) & 0x000f0000) >> 16;
1061 drvinfo_sz <<= 3;
1062 if (pkt_len <= 0)
1063 goto _exit_recvbuf2recvframe;
1064
1065 if ((le32_to_cpu(prxstat->rxdw0) >> 23) & 0x01)
1066 shift_sz = 2;
1067 precvframe = r8712_alloc_recvframe(pfree_recv_queue);
1068 if (!precvframe)
1069 goto _exit_recvbuf2recvframe;
1070 INIT_LIST_HEAD(&precvframe->u.hdr.list);
1071 precvframe->u.hdr.precvbuf = NULL;
1072 precvframe->u.hdr.len = 0;
1073 tmp_len = pkt_len + drvinfo_sz + RXDESC_SIZE;
1074 pkt_offset = (u16)round_up(tmp_len, 128);
1075
1076
1077
1078 if ((mf == 1) && (frag == 0))
1079
1080 alloc_sz = max_t(u16, tmp_len, 1658);
1081 else
1082 alloc_sz = tmp_len;
1083
1084
1085
1086 alloc_sz += 6;
1087 pkt_copy = netdev_alloc_skb(padapter->pnetdev, alloc_sz);
1088 if (pkt_copy) {
1089 precvframe->u.hdr.pkt = pkt_copy;
1090 skb_reserve(pkt_copy, 4 - ((addr_t)(pkt_copy->data)
1091 % 4));
1092 skb_reserve(pkt_copy, shift_sz);
1093 memcpy(pkt_copy->data, pbuf, tmp_len);
1094 precvframe->u.hdr.rx_head = precvframe->u.hdr.rx_data =
1095 precvframe->u.hdr.rx_tail = pkt_copy->data;
1096 precvframe->u.hdr.rx_end = pkt_copy->data + alloc_sz;
1097 } else {
1098 precvframe->u.hdr.pkt = skb_clone(pskb, GFP_ATOMIC);
1099 if (!precvframe->u.hdr.pkt)
1100 return _FAIL;
1101 precvframe->u.hdr.rx_head = pbuf;
1102 precvframe->u.hdr.rx_data = pbuf;
1103 precvframe->u.hdr.rx_tail = pbuf;
1104 precvframe->u.hdr.rx_end = pbuf + alloc_sz;
1105 }
1106 recvframe_put(precvframe, tmp_len);
1107 recvframe_pull(precvframe, drvinfo_sz + RXDESC_SIZE);
1108
1109
1110
1111 update_recvframe_attrib_from_recvstat(&precvframe->u.hdr.attrib,
1112 prxstat);
1113 r8712_recv_entry(precvframe);
1114 transfer_len -= pkt_offset;
1115 pbuf += pkt_offset;
1116 pkt_cnt--;
1117 precvframe = NULL;
1118 pkt_copy = NULL;
1119 } while ((transfer_len > 0) && pkt_cnt > 0);
1120_exit_recvbuf2recvframe:
1121 return _SUCCESS;
1122}
1123
1124static void recv_tasklet(void *priv)
1125{
1126 struct sk_buff *pskb;
1127 struct _adapter *padapter = (struct _adapter *)priv;
1128 struct recv_priv *precvpriv = &padapter->recvpriv;
1129
1130 while (NULL != (pskb = skb_dequeue(&precvpriv->rx_skb_queue))) {
1131 recvbuf2recvframe(padapter, pskb);
1132 skb_reset_tail_pointer(pskb);
1133 pskb->len = 0;
1134 if (!skb_cloned(pskb))
1135 skb_queue_tail(&precvpriv->free_recv_skb_queue, pskb);
1136 else
1137 consume_skb(pskb);
1138 }
1139}
1140