1 /******************************************************************************
2  * rtl8712_recv.c
3  *
4  * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
5  * Linux device driver for RTL8192SU
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19  *
20  * Modifications for inclusion into the Linux staging tree are
21  * Copyright(c) 2010 Larry Finger. All rights reserved.
22  *
23  * Contact information:
24  * WLAN FAE <wlanfae@realtek.com>
25  * Larry Finger <Larry.Finger@lwfinger.net>
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 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
43 static u8 bridge_tunnel_header[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8};
44 
45 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
46 static u8 rfc1042_header[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
47 
48 static void recv_tasklet(void *priv);
49 
r8712_init_recv_priv(struct recv_priv * precvpriv,struct _adapter * padapter)50 int 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 	/*init recv_buf*/
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 
r8712_free_recv_priv(struct recv_priv * precvpriv)101 void 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 
r8712_init_recvbuf(struct _adapter * padapter,struct recv_buf * precvbuf)122 int 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 
r8712_free_recvframe(union recv_frame * precvframe,struct __queue * pfree_recv_queue)136 int 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);/*free skb by driver*/
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 
update_recvframe_attrib_from_recvstat(struct rx_pkt_attrib * pattrib,struct recv_stat * prxstat)158 static 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 	/*TODO:
166 	 * Offset 0
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 	/*Offset 4*/
172 	/*Offset 8*/
173 	/*Offset 12*/
174 	if (le32_to_cpu(prxstat->rxdw3) & BIT(13)) {
175 		pattrib->tcpchk_valid = 1; /* valid */
176 		if (le32_to_cpu(prxstat->rxdw3) & BIT(11))
177 			pattrib->tcp_chkrpt = 1; /* correct */
178 		else
179 			pattrib->tcp_chkrpt = 0; /* incorrect */
180 		if (le32_to_cpu(prxstat->rxdw3) & BIT(12))
181 			pattrib->ip_chkrpt = 1; /* correct */
182 		else
183 			pattrib->ip_chkrpt = 0; /* incorrect */
184 	} else {
185 		pattrib->tcpchk_valid = 0; /* invalid */
186 	}
187 	pattrib->mcs_rate = (u8)((le32_to_cpu(prxstat->rxdw3)) & 0x3f);
188 	pattrib->htc = (u8)((le32_to_cpu(prxstat->rxdw3) >> 14) & 0x1);
189 	/*Offset 16*/
190 	/*Offset 20*/
191 	/*phy_info*/
192 }
193 
194 /*perform defrag*/
recvframe_defrag(struct _adapter * adapter,struct __queue * defrag_q)195 static 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 		/*the first fragment number must be 0
214 		 *free the whole queue
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 		/*check the fragment sequence  (2nd ~n fragment frame) */
227 		if (curfragnum != pnfhdr->attrib.frag_num) {
228 			/* the fragment number must increase  (after decache)
229 			 * release the defrag_q & prframe
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 		/* copy the 2nd~n fragment frame's payload to the first fragment
237 		 * get the 2nd~last fragment frame's payload
238 		 */
239 		wlanhdr_offset = pnfhdr->attrib.hdrlen + pnfhdr->attrib.iv_len;
240 		recvframe_pull(pnextrframe, wlanhdr_offset);
241 		/* append  to first fragment frame's tail (if privacy frame,
242 		 * pull the ICV)
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 	/* free the defrag_q queue and return the prframe */
251 	r8712_free_recvframe_queue(defrag_q, pfree_recv_queue);
252 	return prframe;
253 }
254 
255 /* 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)256 union 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 	/* need to define struct of wlan header frame ctrl */
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;/*isn't a fragment frame*/
284 	if (ismfrag == 1) {
285 		/* 0~(n-1) fragment frame
286 		 * enqueue to defraf_g
287 		 */
288 		if (pdefrag_q != NULL) {
289 			if (fragnum == 0) {
290 				/*the first fragment*/
291 				if (!list_empty(&pdefrag_q->queue)) {
292 					/*free current defrag_q */
293 					r8712_free_recvframe_queue(pdefrag_q,
294 							     pfree_recv_queue);
295 				}
296 			}
297 			/* Then enqueue the 0~(n-1) fragment to the defrag_q */
298 			phead = &pdefrag_q->queue;
299 			list_add_tail(&pfhdr->list, phead);
300 			prtnframe = NULL;
301 		} else {
302 			/* can't find this ta's defrag_queue, so free this
303 			 * recv_frame
304 			 */
305 			r8712_free_recvframe(precv_frame, pfree_recv_queue);
306 			prtnframe = NULL;
307 		}
308 
309 	}
310 	if ((ismfrag == 0) && (fragnum != 0)) {
311 		/* the last fragment frame
312 		 * enqueue the last fragment
313 		 */
314 		if (pdefrag_q != NULL) {
315 			phead = &pdefrag_q->queue;
316 			list_add_tail(&pfhdr->list, phead);
317 			/*call recvframe_defrag to defrag*/
318 			precv_frame = recvframe_defrag(padapter, pdefrag_q);
319 			prtnframe = precv_frame;
320 		} else {
321 			/* can't find this ta's defrag_queue, so free this
322 			 *  recv_frame
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 		/* after defrag we must check tkip mic code */
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 
amsdu_to_msdu(struct _adapter * padapter,union recv_frame * prframe)338 static 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 *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 		/* Offset 12 denote 2 mac address */
358 		nSubframe_Length = *((u16 *)(pdata + 12));
359 		/*==m==>change the length order*/
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 		/* move the data point to data content */
368 		pdata += ETH_HLEN;
369 		a_len -= ETH_HLEN;
370 		/* Allocate new skb for releasing to upper layer */
371 		sub_skb = dev_alloc_skb(nSubframe_Length + 12);
372 		if (!sub_skb)
373 			break;
374 		skb_reserve(sub_skb, 12);
375 		skb_put_data(sub_skb, pdata, nSubframe_Length);
376 		subframes[nr_subframes++] = sub_skb;
377 		if (nr_subframes >= MAX_SUBFRAME_COUNT) {
378 			netdev_warn(padapter->pnetdev, "r8712u: ParseSubframe(): Too many Subframes! Packets dropped!\n");
379 			break;
380 		}
381 		pdata += nSubframe_Length;
382 		a_len -= nSubframe_Length;
383 		if (a_len != 0) {
384 			padding_len = 4 - ((nSubframe_Length + ETH_HLEN) & 3);
385 			if (padding_len == 4)
386 				padding_len = 0;
387 			if (a_len < padding_len)
388 				goto exit;
389 			pdata += padding_len;
390 			a_len -= padding_len;
391 		}
392 	}
393 	for (i = 0; i < nr_subframes; i++) {
394 		sub_skb = subframes[i];
395 		/* convert hdr + possible LLC headers into Ethernet header */
396 		eth_type = (sub_skb->data[6] << 8) | sub_skb->data[7];
397 		if (sub_skb->len >= 8 &&
398 		   ((!memcmp(sub_skb->data, rfc1042_header, SNAP_SIZE) &&
399 		   eth_type != ETH_P_AARP && eth_type != ETH_P_IPX) ||
400 		   !memcmp(sub_skb->data, bridge_tunnel_header, SNAP_SIZE))) {
401 			/* remove RFC1042 or Bridge-Tunnel encapsulation and
402 			 * replace EtherType
403 			 */
404 			skb_pull(sub_skb, SNAP_SIZE);
405 			memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src,
406 				ETH_ALEN);
407 			memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst,
408 				ETH_ALEN);
409 		} else {
410 			__be16 len;
411 			/* Leave Ethernet header part of hdr and full payload */
412 			len = htons(sub_skb->len);
413 			memcpy(skb_push(sub_skb, 2), &len, 2);
414 			memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src,
415 				ETH_ALEN);
416 			memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst,
417 				ETH_ALEN);
418 		}
419 		/* Indicate the packets to upper layer */
420 		if (sub_skb) {
421 			sub_skb->protocol =
422 				 eth_type_trans(sub_skb, padapter->pnetdev);
423 			sub_skb->dev = padapter->pnetdev;
424 			if ((pattrib->tcpchk_valid == 1) &&
425 			    (pattrib->tcp_chkrpt == 1)) {
426 				sub_skb->ip_summed = CHECKSUM_UNNECESSARY;
427 			} else {
428 				sub_skb->ip_summed = CHECKSUM_NONE;
429 			}
430 			netif_rx(sub_skb);
431 		}
432 	}
433 exit:
434 	prframe->u.hdr.len = 0;
435 	r8712_free_recvframe(prframe, pfree_recv_queue);
436 	return _SUCCESS;
437 }
438 
r8712_rxcmd_event_hdl(struct _adapter * padapter,void * prxcmdbuf)439 void r8712_rxcmd_event_hdl(struct _adapter *padapter, void *prxcmdbuf)
440 {
441 	__le32 voffset;
442 	u8 *poffset;
443 	u16 cmd_len, drvinfo_sz;
444 	struct recv_stat *prxstat;
445 
446 	poffset = prxcmdbuf;
447 	voffset = *(__le32 *)poffset;
448 	prxstat = prxcmdbuf;
449 	drvinfo_sz = (le32_to_cpu(prxstat->rxdw0) & 0x000f0000) >> 16;
450 	drvinfo_sz <<= 3;
451 	poffset += RXDESC_SIZE + drvinfo_sz;
452 	do {
453 		voffset  = *(__le32 *)poffset;
454 		cmd_len = (u16)(le32_to_cpu(voffset) & 0xffff);
455 		r8712_event_handle(padapter, (__le32 *)poffset);
456 		poffset += (cmd_len + 8);/*8 bytes alignment*/
457 	} while (le32_to_cpu(voffset) & BIT(31));
458 
459 }
460 
check_indicate_seq(struct recv_reorder_ctrl * preorder_ctrl,u16 seq_num)461 static int check_indicate_seq(struct recv_reorder_ctrl *preorder_ctrl,
462 			      u16 seq_num)
463 {
464 	u8 wsize = preorder_ctrl->wsize_b;
465 	u16 wend = (preorder_ctrl->indicate_seq + wsize - 1) % 4096;
466 
467 	/* Rx Reorder initialize condition.*/
468 	if (preorder_ctrl->indicate_seq == 0xffff)
469 		preorder_ctrl->indicate_seq = seq_num;
470 	/* Drop out the packet which SeqNum is smaller than WinStart */
471 	if (SN_LESS(seq_num, preorder_ctrl->indicate_seq))
472 		return false;
473 	/*
474 	 * Sliding window manipulation. Conditions includes:
475 	 * 1. Incoming SeqNum is equal to WinStart =>Window shift 1
476 	 * 2. Incoming SeqNum is larger than the WinEnd => Window shift N
477 	 */
478 	if (SN_EQUAL(seq_num, preorder_ctrl->indicate_seq))
479 		preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq +
480 					      1) % 4096;
481 	else if (SN_LESS(wend, seq_num)) {
482 		if (seq_num >= (wsize - 1))
483 			preorder_ctrl->indicate_seq = seq_num + 1 - wsize;
484 		else
485 			preorder_ctrl->indicate_seq = 4095 - (wsize -
486 						      (seq_num + 1)) + 1;
487 	}
488 	return true;
489 }
490 
enqueue_reorder_recvframe(struct recv_reorder_ctrl * preorder_ctrl,union recv_frame * prframe)491 static int enqueue_reorder_recvframe(struct recv_reorder_ctrl *preorder_ctrl,
492 			      union recv_frame *prframe)
493 {
494 	struct list_head *phead, *plist;
495 	union recv_frame *pnextrframe;
496 	struct rx_pkt_attrib *pnextattrib;
497 	struct  __queue *ppending_recvframe_queue =
498 					&preorder_ctrl->pending_recvframe_queue;
499 	struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
500 
501 	phead = &ppending_recvframe_queue->queue;
502 	plist = phead->next;
503 	while (!end_of_queue_search(phead, plist)) {
504 		pnextrframe = container_of(plist, union recv_frame, u.list);
505 		pnextattrib = &pnextrframe->u.hdr.attrib;
506 		if (SN_LESS(pnextattrib->seq_num, pattrib->seq_num))
507 			plist = plist->next;
508 		else if (SN_EQUAL(pnextattrib->seq_num, pattrib->seq_num))
509 			return false;
510 		else
511 			break;
512 	}
513 	list_del_init(&(prframe->u.hdr.list));
514 	list_add_tail(&(prframe->u.hdr.list), plist);
515 	return true;
516 }
517 
r8712_recv_indicatepkts_in_order(struct _adapter * padapter,struct recv_reorder_ctrl * preorder_ctrl,int bforced)518 int r8712_recv_indicatepkts_in_order(struct _adapter *padapter,
519 			       struct recv_reorder_ctrl *preorder_ctrl,
520 			       int bforced)
521 {
522 	struct list_head *phead, *plist;
523 	union recv_frame *prframe;
524 	struct rx_pkt_attrib *pattrib;
525 	int bPktInBuf = false;
526 	struct recv_priv *precvpriv = &padapter->recvpriv;
527 	struct  __queue *ppending_recvframe_queue =
528 			 &preorder_ctrl->pending_recvframe_queue;
529 
530 	phead = &ppending_recvframe_queue->queue;
531 	plist = phead->next;
532 	/* Handling some condition for forced indicate case.*/
533 	if (bforced) {
534 		if (list_empty(phead))
535 			return true;
536 
537 		prframe = container_of(plist, union recv_frame, u.list);
538 		pattrib = &prframe->u.hdr.attrib;
539 		preorder_ctrl->indicate_seq = pattrib->seq_num;
540 	}
541 	/* Prepare indication list and indication.
542 	 * Check if there is any packet need indicate.
543 	 */
544 	while (!list_empty(phead)) {
545 		prframe = container_of(plist, union recv_frame, u.list);
546 		pattrib = &prframe->u.hdr.attrib;
547 		if (!SN_LESS(preorder_ctrl->indicate_seq, pattrib->seq_num)) {
548 			plist = plist->next;
549 			list_del_init(&(prframe->u.hdr.list));
550 			if (SN_EQUAL(preorder_ctrl->indicate_seq,
551 			    pattrib->seq_num))
552 				preorder_ctrl->indicate_seq =
553 				  (preorder_ctrl->indicate_seq + 1) % 4096;
554 			/*indicate this recv_frame*/
555 			if (!pattrib->amsdu) {
556 				if (!padapter->bDriverStopped &&
557 				    !padapter->bSurpriseRemoved) {
558 					/* indicate this recv_frame */
559 					r8712_recv_indicatepkt(padapter,
560 							       prframe);
561 				}
562 			} else if (pattrib->amsdu == 1) {
563 				if (amsdu_to_msdu(padapter, prframe) !=
564 				    _SUCCESS)
565 					r8712_free_recvframe(prframe,
566 						   &precvpriv->free_recv_queue);
567 			}
568 			/* Update local variables. */
569 			bPktInBuf = false;
570 		} else {
571 			bPktInBuf = true;
572 			break;
573 		}
574 	}
575 	return bPktInBuf;
576 }
577 
recv_indicatepkt_reorder(struct _adapter * padapter,union recv_frame * prframe)578 static int recv_indicatepkt_reorder(struct _adapter *padapter,
579 			     union recv_frame *prframe)
580 {
581 	unsigned long irql;
582 	struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
583 	struct recv_reorder_ctrl *preorder_ctrl = prframe->u.hdr.preorder_ctrl;
584 	struct  __queue *ppending_recvframe_queue =
585 			 &preorder_ctrl->pending_recvframe_queue;
586 
587 	if (!pattrib->amsdu) {
588 		/* s1. */
589 		r8712_wlanhdr_to_ethhdr(prframe);
590 		if (pattrib->qos != 1) {
591 			if (!padapter->bDriverStopped &&
592 			    !padapter->bSurpriseRemoved) {
593 				r8712_recv_indicatepkt(padapter, prframe);
594 				return _SUCCESS;
595 			} else {
596 				return _FAIL;
597 			}
598 		}
599 	}
600 	spin_lock_irqsave(&ppending_recvframe_queue->lock, irql);
601 	/*s2. check if winstart_b(indicate_seq) needs to be updated*/
602 	if (!check_indicate_seq(preorder_ctrl, pattrib->seq_num))
603 		goto _err_exit;
604 	/*s3. Insert all packet into Reorder Queue to maintain its ordering.*/
605 	if (!enqueue_reorder_recvframe(preorder_ctrl, prframe))
606 		goto _err_exit;
607 	/*s4.
608 	 * Indication process.
609 	 * After Packet dropping and Sliding Window shifting as above, we can
610 	 * now just indicate the packets with the SeqNum smaller than latest
611 	 * WinStart and buffer other packets.
612 	 *
613 	 * For Rx Reorder condition:
614 	 * 1. All packets with SeqNum smaller than WinStart => Indicate
615 	 * 2. All packets with SeqNum larger than or equal to
616 	 * WinStart => Buffer it.
617 	 */
618 	if (r8712_recv_indicatepkts_in_order(padapter, preorder_ctrl, false) ==
619 	    true) {
620 		mod_timer(&preorder_ctrl->reordering_ctrl_timer,
621 			  jiffies + msecs_to_jiffies(REORDER_WAIT_TIME));
622 		spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql);
623 	} else {
624 		spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql);
625 		del_timer(&preorder_ctrl->reordering_ctrl_timer);
626 	}
627 	return _SUCCESS;
628 _err_exit:
629 	spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql);
630 	return _FAIL;
631 }
632 
r8712_reordering_ctrl_timeout_handler(void * pcontext)633 void r8712_reordering_ctrl_timeout_handler(void *pcontext)
634 {
635 	unsigned long irql;
636 	struct recv_reorder_ctrl *preorder_ctrl = pcontext;
637 	struct _adapter *padapter = preorder_ctrl->padapter;
638 	struct  __queue *ppending_recvframe_queue =
639 				 &preorder_ctrl->pending_recvframe_queue;
640 
641 	if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
642 		return;
643 	spin_lock_irqsave(&ppending_recvframe_queue->lock, irql);
644 	r8712_recv_indicatepkts_in_order(padapter, preorder_ctrl, true);
645 	spin_unlock_irqrestore(&ppending_recvframe_queue->lock, irql);
646 }
647 
r8712_process_recv_indicatepkts(struct _adapter * padapter,union recv_frame * prframe)648 static int r8712_process_recv_indicatepkts(struct _adapter *padapter,
649 			      union recv_frame *prframe)
650 {
651 	int retval = _SUCCESS;
652 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
653 	struct ht_priv	*phtpriv = &pmlmepriv->htpriv;
654 
655 	if (phtpriv->ht_option == 1) { /*B/G/N Mode*/
656 		if (recv_indicatepkt_reorder(padapter, prframe) != _SUCCESS) {
657 			/* including perform A-MPDU Rx Ordering Buffer Control*/
658 			if (!padapter->bDriverStopped &&
659 			    !padapter->bSurpriseRemoved)
660 				return _FAIL;
661 		}
662 	} else { /*B/G mode*/
663 		retval = r8712_wlanhdr_to_ethhdr(prframe);
664 		if (retval != _SUCCESS)
665 			return retval;
666 		if (!padapter->bDriverStopped && !padapter->bSurpriseRemoved) {
667 			/* indicate this recv_frame */
668 			r8712_recv_indicatepkt(padapter, prframe);
669 		} else {
670 			return _FAIL;
671 		}
672 	}
673 	return retval;
674 }
675 
query_rx_pwr_percentage(s8 antpower)676 static u8 query_rx_pwr_percentage(s8 antpower)
677 {
678 	if ((antpower <= -100) || (antpower >= 20))
679 		return	0;
680 	else if (antpower >= 0)
681 		return	100;
682 	else
683 		return 100 + antpower;
684 }
685 
evm_db2percentage(s8 value)686 static u8 evm_db2percentage(s8 value)
687 {
688 	/*
689 	 * -33dB~0dB to 0%~99%
690 	 */
691 	s8 ret_val;
692 
693 	ret_val = value;
694 	if (ret_val >= 0)
695 		ret_val = 0;
696 	if (ret_val <= -33)
697 		ret_val = -33;
698 	ret_val = -ret_val;
699 	ret_val *= 3;
700 	if (ret_val == 99)
701 		ret_val = 100;
702 	return ret_val;
703 }
704 
r8712_signal_scale_mapping(s32 cur_sig)705 s32 r8712_signal_scale_mapping(s32 cur_sig)
706 {
707 	s32 ret_sig;
708 
709 	if (cur_sig >= 51 && cur_sig <= 100)
710 		ret_sig = 100;
711 	else if (cur_sig >= 41 && cur_sig <= 50)
712 		ret_sig = 80 + ((cur_sig - 40) * 2);
713 	else if (cur_sig >= 31 && cur_sig <= 40)
714 		ret_sig = 66 + (cur_sig - 30);
715 	else if (cur_sig >= 21 && cur_sig <= 30)
716 		ret_sig = 54 + (cur_sig - 20);
717 	else if (cur_sig >= 10 && cur_sig <= 20)
718 		ret_sig = 42 + (((cur_sig - 10) * 2) / 3);
719 	else if (cur_sig >= 5 && cur_sig <= 9)
720 		ret_sig = 22 + (((cur_sig - 5) * 3) / 2);
721 	else if (cur_sig >= 1 && cur_sig <= 4)
722 		ret_sig = 6 + (((cur_sig - 1) * 3) / 2);
723 	else
724 		ret_sig = cur_sig;
725 	return ret_sig;
726 }
727 
translate2dbm(struct _adapter * padapter,u8 signal_strength_idx)728 static s32  translate2dbm(struct _adapter *padapter, u8 signal_strength_idx)
729 {
730 	s32 signal_power; /* in dBm.*/
731 	/* Translate to dBm (x=0.5y-95).*/
732 	signal_power = (s32)((signal_strength_idx + 1) >> 1);
733 	signal_power -= 95;
734 	return signal_power;
735 }
736 
query_rx_phy_status(struct _adapter * padapter,union recv_frame * prframe)737 static void query_rx_phy_status(struct _adapter *padapter,
738 				union recv_frame *prframe)
739 {
740 	u8 i, max_spatial_stream, evm;
741 	struct recv_stat *prxstat = (struct recv_stat *)prframe->u.hdr.rx_head;
742 	struct phy_stat *pphy_stat = (struct phy_stat *)(prxstat + 1);
743 	u8 *pphy_head = (u8 *)(prxstat + 1);
744 	s8 rx_pwr[4], rx_pwr_all;
745 	u8 pwdb_all;
746 	u32 rssi, total_rssi = 0;
747 	u8 bcck_rate = 0, rf_rx_num = 0, cck_highpwr = 0;
748 	struct phy_cck_rx_status *pcck_buf;
749 	u8 sq;
750 
751 	/* Record it for next packet processing*/
752 	bcck_rate = (prframe->u.hdr.attrib.mcs_rate <= 3 ? 1 : 0);
753 	if (bcck_rate) {
754 		u8 report;
755 
756 		/* CCK Driver info Structure is not the same as OFDM packet.*/
757 		pcck_buf = (struct phy_cck_rx_status *)pphy_stat;
758 		/* (1)Hardware does not provide RSSI for CCK
759 		 * (2)PWDB, Average PWDB calculated by hardware
760 		 * (for rate adaptive)
761 		 */
762 		if (!cck_highpwr) {
763 			report = pcck_buf->cck_agc_rpt & 0xc0;
764 			report >>= 6;
765 			switch (report) {
766 			/* Modify the RF RNA gain value to -40, -20,
767 			 * -2, 14 by Jenyu's suggestion
768 			 * Note: different RF with the different
769 			 * RNA gain.
770 			 */
771 			case 0x3:
772 				rx_pwr_all = -40 - (pcck_buf->cck_agc_rpt &
773 					     0x3e);
774 				break;
775 			case 0x2:
776 				rx_pwr_all = -20 - (pcck_buf->cck_agc_rpt &
777 					     0x3e);
778 				break;
779 			case 0x1:
780 				rx_pwr_all = -2 - (pcck_buf->cck_agc_rpt &
781 					     0x3e);
782 				break;
783 			case 0x0:
784 				rx_pwr_all = 14 - (pcck_buf->cck_agc_rpt &
785 					     0x3e);
786 				break;
787 			}
788 		} else {
789 			report = ((u8)(le32_to_cpu(pphy_stat->phydw1) >> 8)) &
790 				 0x60;
791 			report >>= 5;
792 			switch (report) {
793 			case 0x3:
794 				rx_pwr_all = -40 - ((pcck_buf->cck_agc_rpt &
795 					     0x1f) << 1);
796 				break;
797 			case 0x2:
798 				rx_pwr_all = -20 - ((pcck_buf->cck_agc_rpt &
799 					     0x1f) << 1);
800 				break;
801 			case 0x1:
802 				rx_pwr_all = -2 - ((pcck_buf->cck_agc_rpt &
803 					     0x1f) << 1);
804 				break;
805 			case 0x0:
806 				rx_pwr_all = 14 - ((pcck_buf->cck_agc_rpt &
807 					     0x1f) << 1);
808 				break;
809 			}
810 		}
811 		pwdb_all = query_rx_pwr_percentage(rx_pwr_all);
812 		/* CCK gain is smaller than OFDM/MCS gain,*/
813 		/* so we add gain diff by experiences, the val is 6 */
814 		pwdb_all += 6;
815 		if (pwdb_all > 100)
816 			pwdb_all = 100;
817 		/* modify the offset to make the same gain index with OFDM.*/
818 		if (pwdb_all > 34 && pwdb_all <= 42)
819 			pwdb_all -= 2;
820 		else if (pwdb_all > 26 && pwdb_all <= 34)
821 			pwdb_all -= 6;
822 		else if (pwdb_all > 14 && pwdb_all <= 26)
823 			pwdb_all -= 8;
824 		else if (pwdb_all > 4 && pwdb_all <= 14)
825 			pwdb_all -= 4;
826 		/*
827 		 * (3) Get Signal Quality (EVM)
828 		 */
829 		if (pwdb_all > 40) {
830 			sq = 100;
831 		} else {
832 			sq = pcck_buf->sq_rpt;
833 			if (pcck_buf->sq_rpt > 64)
834 				sq = 0;
835 			else if (pcck_buf->sq_rpt < 20)
836 				sq = 100;
837 			else
838 				sq = ((64 - sq) * 100) / 44;
839 		}
840 		prframe->u.hdr.attrib.signal_qual = sq;
841 		prframe->u.hdr.attrib.rx_mimo_signal_qual[0] = sq;
842 		prframe->u.hdr.attrib.rx_mimo_signal_qual[1] = -1;
843 	} else {
844 		/* (1)Get RSSI for HT rate */
845 		for (i = 0; i < ((padapter->registrypriv.rf_config) &
846 			    0x0f); i++) {
847 			rf_rx_num++;
848 			rx_pwr[i] = ((pphy_head[PHY_STAT_GAIN_TRSW_SHT + i]
849 				    & 0x3F) * 2) - 110;
850 			/* Translate DBM to percentage. */
851 			rssi = query_rx_pwr_percentage(rx_pwr[i]);
852 			total_rssi += rssi;
853 		}
854 		/* (2)PWDB, Average PWDB calculated by hardware (for
855 		 * rate adaptive)
856 		 */
857 		rx_pwr_all = (((pphy_head[PHY_STAT_PWDB_ALL_SHT]) >> 1) & 0x7f)
858 			     - 106;
859 		pwdb_all = query_rx_pwr_percentage(rx_pwr_all);
860 
861 		{
862 			/* (3)EVM of HT rate */
863 			if (prframe->u.hdr.attrib.htc &&
864 			    prframe->u.hdr.attrib.mcs_rate >= 20 &&
865 			    prframe->u.hdr.attrib.mcs_rate <= 27) {
866 				/* both spatial stream make sense */
867 				max_spatial_stream = 2;
868 			} else {
869 				/* only spatial stream 1 makes sense */
870 				max_spatial_stream = 1;
871 			}
872 			for (i = 0; i < max_spatial_stream; i++) {
873 				evm = evm_db2percentage((pphy_head
874 				      [PHY_STAT_RXEVM_SHT + i]));/*dbm*/
875 				prframe->u.hdr.attrib.signal_qual =
876 					 (u8)(evm & 0xff);
877 				prframe->u.hdr.attrib.rx_mimo_signal_qual[i] =
878 					 (u8)(evm & 0xff);
879 			}
880 		}
881 	}
882 	/* UI BSS List signal strength(in percentage), make it good looking,
883 	 * from 0~100. It is assigned to the BSS List in
884 	 * GetValueFromBeaconOrProbeRsp().
885 	 */
886 	if (bcck_rate) {
887 		prframe->u.hdr.attrib.signal_strength =
888 			 (u8)r8712_signal_scale_mapping(pwdb_all);
889 	} else {
890 		if (rf_rx_num != 0)
891 			prframe->u.hdr.attrib.signal_strength =
892 				 (u8)(r8712_signal_scale_mapping(total_rssi /=
893 				 rf_rx_num));
894 	}
895 }
896 
process_link_qual(struct _adapter * padapter,union recv_frame * prframe)897 static void process_link_qual(struct _adapter *padapter,
898 			      union recv_frame *prframe)
899 {
900 	u32	last_evm = 0, tmpVal;
901 	struct rx_pkt_attrib *pattrib;
902 	struct smooth_rssi_data *sqd = &padapter->recvpriv.signal_qual_data;
903 
904 	if (prframe == NULL || padapter == NULL)
905 		return;
906 	pattrib = &prframe->u.hdr.attrib;
907 	if (pattrib->signal_qual != 0) {
908 		/*
909 		 * 1. Record the general EVM to the sliding window.
910 		 */
911 		if (sqd->total_num++ >= PHY_LINKQUALITY_SLID_WIN_MAX) {
912 			sqd->total_num = PHY_LINKQUALITY_SLID_WIN_MAX;
913 			last_evm = sqd->elements[sqd->index];
914 			sqd->total_val -= last_evm;
915 		}
916 		sqd->total_val += pattrib->signal_qual;
917 		sqd->elements[sqd->index++] = pattrib->signal_qual;
918 		if (sqd->index >= PHY_LINKQUALITY_SLID_WIN_MAX)
919 			sqd->index = 0;
920 
921 		/* <1> Showed on UI for user, in percentage. */
922 		tmpVal = sqd->total_val / sqd->total_num;
923 		padapter->recvpriv.signal = (u8)tmpVal;
924 	}
925 }
926 
process_rssi(struct _adapter * padapter,union recv_frame * prframe)927 static void process_rssi(struct _adapter *padapter, union recv_frame *prframe)
928 {
929 	u32 last_rssi, tmp_val;
930 	struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib;
931 	struct smooth_rssi_data *ssd = &padapter->recvpriv.signal_strength_data;
932 
933 	if (ssd->total_num++ >= PHY_RSSI_SLID_WIN_MAX) {
934 		ssd->total_num = PHY_RSSI_SLID_WIN_MAX;
935 		last_rssi = ssd->elements[ssd->index];
936 		ssd->total_val -= last_rssi;
937 	}
938 	ssd->total_val += pattrib->signal_strength;
939 	ssd->elements[ssd->index++] = pattrib->signal_strength;
940 	if (ssd->index >= PHY_RSSI_SLID_WIN_MAX)
941 		ssd->index = 0;
942 	tmp_val = ssd->total_val / ssd->total_num;
943 	padapter->recvpriv.rssi = (s8)translate2dbm(padapter, (u8)tmp_val);
944 }
945 
process_phy_info(struct _adapter * padapter,union recv_frame * prframe)946 static void process_phy_info(struct _adapter *padapter,
947 			     union recv_frame *prframe)
948 {
949 	query_rx_phy_status(padapter, prframe);
950 	process_rssi(padapter, prframe);
951 	process_link_qual(padapter,  prframe);
952 }
953 
recv_func(struct _adapter * padapter,void * pcontext)954 int recv_func(struct _adapter *padapter, void *pcontext)
955 {
956 	struct rx_pkt_attrib *pattrib;
957 	union recv_frame *prframe, *orig_prframe;
958 	int retval = _SUCCESS;
959 	struct  __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
960 	struct	mlme_priv	*pmlmepriv = &padapter->mlmepriv;
961 
962 	prframe = pcontext;
963 	orig_prframe = prframe;
964 	pattrib = &prframe->u.hdr.attrib;
965 	if (check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
966 		if (pattrib->crc_err == 1)
967 			padapter->mppriv.rx_crcerrpktcount++;
968 		else
969 			padapter->mppriv.rx_pktcount++;
970 		if (!check_fwstate(pmlmepriv, WIFI_MP_LPBK_STATE)) {
971 			/* free this recv_frame */
972 			r8712_free_recvframe(orig_prframe, pfree_recv_queue);
973 			goto _exit_recv_func;
974 		}
975 	}
976 	/* check the frame crtl field and decache */
977 	retval = r8712_validate_recv_frame(padapter, prframe);
978 	if (retval != _SUCCESS) {
979 		/* free this recv_frame */
980 		r8712_free_recvframe(orig_prframe, pfree_recv_queue);
981 		goto _exit_recv_func;
982 	}
983 	process_phy_info(padapter, prframe);
984 	prframe = r8712_decryptor(padapter, prframe);
985 	if (!prframe) {
986 		retval = _FAIL;
987 		goto _exit_recv_func;
988 	}
989 	prframe = r8712_recvframe_chk_defrag(padapter, prframe);
990 	if (!prframe)
991 		goto _exit_recv_func;
992 	prframe = r8712_portctrl(padapter, prframe);
993 	if (!prframe) {
994 		retval = _FAIL;
995 		goto _exit_recv_func;
996 	}
997 	retval = r8712_process_recv_indicatepkts(padapter, prframe);
998 	if (retval != _SUCCESS) {
999 		r8712_free_recvframe(orig_prframe, pfree_recv_queue);
1000 		goto _exit_recv_func;
1001 	}
1002 _exit_recv_func:
1003 	return retval;
1004 }
1005 
recvbuf2recvframe(struct _adapter * padapter,struct sk_buff * pskb)1006 static int recvbuf2recvframe(struct _adapter *padapter, struct sk_buff *pskb)
1007 {
1008 	u8 *pbuf, shift_sz = 0;
1009 	u8	frag, mf;
1010 	uint	pkt_len;
1011 	u32 transfer_len;
1012 	struct recv_stat *prxstat;
1013 	u16	pkt_cnt, drvinfo_sz, pkt_offset, tmp_len, alloc_sz;
1014 	struct  __queue *pfree_recv_queue;
1015 	_pkt  *pkt_copy = NULL;
1016 	union recv_frame *precvframe = NULL;
1017 	struct recv_priv *precvpriv = &padapter->recvpriv;
1018 
1019 	pfree_recv_queue = &(precvpriv->free_recv_queue);
1020 	pbuf = pskb->data;
1021 	prxstat = (struct recv_stat *)pbuf;
1022 	pkt_cnt = (le32_to_cpu(prxstat->rxdw2) >> 16) & 0xff;
1023 	pkt_len =  le32_to_cpu(prxstat->rxdw0) & 0x00003fff;
1024 	transfer_len = pskb->len;
1025 	/* Test throughput with Netgear 3700 (No security) with Chariot 3T3R
1026 	 * pairs. The packet count will be a big number so that the containing
1027 	 * packet will effect the Rx reordering.
1028 	 */
1029 	if (transfer_len < pkt_len) {
1030 		/* In this case, it means the MAX_RECVBUF_SZ is too small to
1031 		 * get the data from 8712u.
1032 		 */
1033 		return _FAIL;
1034 	}
1035 	do {
1036 		prxstat = (struct recv_stat *)pbuf;
1037 		pkt_len =  le32_to_cpu(prxstat->rxdw0) & 0x00003fff;
1038 		/* more fragment bit */
1039 		mf = (le32_to_cpu(prxstat->rxdw1) >> 27) & 0x1;
1040 		/* ragmentation number */
1041 		frag = (le32_to_cpu(prxstat->rxdw2) >> 12) & 0xf;
1042 		/* uint 2^3 = 8 bytes */
1043 		drvinfo_sz = (le32_to_cpu(prxstat->rxdw0) & 0x000f0000) >> 16;
1044 		drvinfo_sz <<= 3;
1045 		if (pkt_len <= 0)
1046 			goto  _exit_recvbuf2recvframe;
1047 		/* Qos data, wireless lan header length is 26 */
1048 		if ((le32_to_cpu(prxstat->rxdw0) >> 23) & 0x01)
1049 			shift_sz = 2;
1050 		precvframe = r8712_alloc_recvframe(pfree_recv_queue);
1051 		if (!precvframe)
1052 			goto  _exit_recvbuf2recvframe;
1053 		INIT_LIST_HEAD(&precvframe->u.hdr.list);
1054 		precvframe->u.hdr.precvbuf = NULL; /*can't access the precvbuf*/
1055 		precvframe->u.hdr.len = 0;
1056 		tmp_len = pkt_len + drvinfo_sz + RXDESC_SIZE;
1057 		pkt_offset = (u16)round_up(tmp_len, 128);
1058 		/* for first fragment packet, driver need allocate 1536 +
1059 		 * drvinfo_sz + RXDESC_SIZE to defrag packet.
1060 		 */
1061 		if ((mf == 1) && (frag == 0))
1062 			/*1658+6=1664, 1664 is 128 alignment.*/
1063 			alloc_sz = max_t(u16, tmp_len, 1658);
1064 		else
1065 			alloc_sz = tmp_len;
1066 		/* 2 is for IP header 4 bytes alignment in QoS packet case.
1067 		 * 4 is for skb->data 4 bytes alignment.
1068 		 */
1069 		alloc_sz += 6;
1070 		pkt_copy = netdev_alloc_skb(padapter->pnetdev, alloc_sz);
1071 		if (pkt_copy) {
1072 			precvframe->u.hdr.pkt = pkt_copy;
1073 			skb_reserve(pkt_copy, 4 - ((addr_t)(pkt_copy->data)
1074 				    % 4));
1075 			skb_reserve(pkt_copy, shift_sz);
1076 			memcpy(pkt_copy->data, pbuf, tmp_len);
1077 			precvframe->u.hdr.rx_head = precvframe->u.hdr.rx_data =
1078 				 precvframe->u.hdr.rx_tail = pkt_copy->data;
1079 			precvframe->u.hdr.rx_end = pkt_copy->data + alloc_sz;
1080 		} else {
1081 			precvframe->u.hdr.pkt = skb_clone(pskb, GFP_ATOMIC);
1082 			if (!precvframe->u.hdr.pkt)
1083 				return _FAIL;
1084 			precvframe->u.hdr.rx_head = pbuf;
1085 			precvframe->u.hdr.rx_data = pbuf;
1086 			precvframe->u.hdr.rx_tail = pbuf;
1087 			precvframe->u.hdr.rx_end = pbuf + alloc_sz;
1088 		}
1089 		recvframe_put(precvframe, tmp_len);
1090 		recvframe_pull(precvframe, drvinfo_sz + RXDESC_SIZE);
1091 		/* because the endian issue, driver avoid reference to the
1092 		 * rxstat after calling update_recvframe_attrib_from_recvstat();
1093 		 */
1094 		update_recvframe_attrib_from_recvstat(&precvframe->u.hdr.attrib,
1095 						      prxstat);
1096 		r8712_recv_entry(precvframe);
1097 		transfer_len -= pkt_offset;
1098 		pbuf += pkt_offset;
1099 		pkt_cnt--;
1100 		precvframe = NULL;
1101 		pkt_copy = NULL;
1102 	} while ((transfer_len > 0) && pkt_cnt > 0);
1103 _exit_recvbuf2recvframe:
1104 	return _SUCCESS;
1105 }
1106 
recv_tasklet(void * priv)1107 static void recv_tasklet(void *priv)
1108 {
1109 	struct sk_buff *pskb;
1110 	struct _adapter *padapter = priv;
1111 	struct recv_priv *precvpriv = &padapter->recvpriv;
1112 
1113 	while (NULL != (pskb = skb_dequeue(&precvpriv->rx_skb_queue))) {
1114 		recvbuf2recvframe(padapter, pskb);
1115 		skb_reset_tail_pointer(pskb);
1116 		pskb->len = 0;
1117 		if (!skb_cloned(pskb))
1118 			skb_queue_tail(&precvpriv->free_recv_skb_queue, pskb);
1119 		else
1120 			consume_skb(pskb);
1121 	}
1122 }
1123