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