xref: /OK3568_Linux_fs/external/rkwifibt/drivers/rtl8822cs/os_dep/linux/recv_linux.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2017 Realtek Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  *****************************************************************************/
15 #define _RECV_OSDEP_C_
16 
17 #include <drv_types.h>
18 
rtw_os_recvframe_duplicate_skb(_adapter * padapter,union recv_frame * pcloneframe,_pkt * pskb)19 int rtw_os_recvframe_duplicate_skb(_adapter *padapter, union recv_frame *pcloneframe, _pkt *pskb)
20 {
21 	int res = _SUCCESS;
22 	_pkt	*pkt_copy = NULL;
23 
24 	if (pskb == NULL) {
25 		RTW_INFO("%s [WARN] skb == NULL, drop frag frame\n", __func__);
26 		return _FAIL;
27 	}
28 #if 1
29 	pkt_copy = rtw_skb_copy(pskb);
30 
31 	if (pkt_copy == NULL) {
32 		RTW_INFO("%s [WARN] rtw_skb_copy fail , drop frag frame\n", __func__);
33 		return _FAIL;
34 	}
35 #else
36 	pkt_copy = rtw_skb_clone(pskb);
37 
38 	if (pkt_copy == NULL) {
39 		RTW_INFO("%s [WARN] rtw_skb_clone fail , drop frag frame\n", __func__);
40 		return _FAIL;
41 	}
42 #endif
43 	pkt_copy->dev = padapter->pnetdev;
44 
45 	pcloneframe->u.hdr.pkt = pkt_copy;
46 	pcloneframe->u.hdr.rx_head = pkt_copy->head;
47 	pcloneframe->u.hdr.rx_data = pkt_copy->data;
48 	pcloneframe->u.hdr.rx_end = skb_end_pointer(pkt_copy);
49 	pcloneframe->u.hdr.rx_tail = skb_tail_pointer(pkt_copy);
50 	pcloneframe->u.hdr.len = pkt_copy->len;
51 
52 	return res;
53 }
54 
rtw_os_alloc_recvframe(_adapter * padapter,union recv_frame * precvframe,u8 * pdata,_pkt * pskb)55 int rtw_os_alloc_recvframe(_adapter *padapter, union recv_frame *precvframe, u8 *pdata, _pkt *pskb)
56 {
57 	int res = _SUCCESS;
58 	u8	shift_sz = 0;
59 	u32	skb_len, alloc_sz;
60 	_pkt	*pkt_copy = NULL;
61 	struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
62 
63 
64 	if (pdata == NULL) {
65 		precvframe->u.hdr.pkt = NULL;
66 		res = _FAIL;
67 		return res;
68 	}
69 
70 
71 	/*	Modified by Albert 20101213 */
72 	/*	For 8 bytes IP header alignment. */
73 	shift_sz = pattrib->qos ? 6 : 0; /*	Qos data, wireless lan header length is 26 */
74 
75 	skb_len = pattrib->pkt_len;
76 
77 	/* for first fragment packet, driver need allocate 1536+drvinfo_sz+RXDESC_SIZE to defrag packet. */
78 	/* modify alloc_sz for recvive crc error packet by thomas 2011-06-02 */
79 	if ((pattrib->mfrag == 1) && (pattrib->frag_num == 0)) {
80 		/* alloc_sz = 1664;	 */ /* 1664 is 128 alignment. */
81 		alloc_sz = (skb_len <= 1650) ? 1664 : (skb_len + 14);
82 	} else {
83 		alloc_sz = skb_len;
84 		/*	6 is for IP header 8 bytes alignment in QoS packet case. */
85 		/*	8 is for skb->data 4 bytes alignment. */
86 		alloc_sz += 14;
87 	}
88 
89 	pkt_copy = rtw_skb_alloc(alloc_sz);
90 
91 	if (pkt_copy) {
92 		pkt_copy->dev = padapter->pnetdev;
93 		pkt_copy->len = skb_len;
94 		precvframe->u.hdr.pkt = pkt_copy;
95 		precvframe->u.hdr.rx_head = pkt_copy->head;
96 		precvframe->u.hdr.rx_end = pkt_copy->data + alloc_sz;
97 		skb_reserve(pkt_copy, 8 - ((SIZE_PTR)(pkt_copy->data) & 7));  /* force pkt_copy->data at 8-byte alignment address */
98 		skb_reserve(pkt_copy, shift_sz);/* force ip_hdr at 8-byte alignment address according to shift_sz. */
99 		_rtw_memcpy(pkt_copy->data, pdata, skb_len);
100 		precvframe->u.hdr.rx_data = precvframe->u.hdr.rx_tail = pkt_copy->data;
101 	} else {
102 #if 0
103 		{
104 			rtw_free_recvframe(precvframe_if2, &precvpriv->free_recv_queue);
105 			rtw_enqueue_recvbuf_to_head(precvbuf, &precvpriv->recv_buf_pending_queue);
106 
107 			/* The case of can't allocate skb is serious and may never be recovered,
108 			 once bDriverStopped is enable, this task should be stopped.*/
109 			if (!rtw_is_drv_stopped(secondary_padapter))
110 #ifdef PLATFORM_LINUX
111 				tasklet_schedule(&precvpriv->recv_tasklet);
112 #endif
113 			return ret;
114 		}
115 
116 #endif
117 
118 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_RX
119 		RTW_INFO("%s:can not allocate memory for skb copy\n", __func__);
120 
121 		precvframe->u.hdr.pkt = NULL;
122 
123 		/* rtw_free_recvframe(precvframe, pfree_recv_queue); */
124 		/*exit_rtw_os_recv_resource_alloc;*/
125 
126 		res = _FAIL;
127 #else
128 		if ((pattrib->mfrag == 1) && (pattrib->frag_num == 0)) {
129 			RTW_INFO("%s: alloc_skb fail , drop frag frame\n", __FUNCTION__);
130 			/* rtw_free_recvframe(precvframe, pfree_recv_queue); */
131 			res = _FAIL;
132 			goto exit_rtw_os_recv_resource_alloc;
133 		}
134 
135 		if (pskb == NULL) {
136 			res = _FAIL;
137 			goto exit_rtw_os_recv_resource_alloc;
138 		}
139 
140 		precvframe->u.hdr.pkt = rtw_skb_clone(pskb);
141 		if (precvframe->u.hdr.pkt) {
142 			RTW_INFO("%s: rtw_skb_clone success, RX throughput may be low!\n", __FUNCTION__);
143 			precvframe->u.hdr.pkt->dev = padapter->pnetdev;
144 			precvframe->u.hdr.rx_head = precvframe->u.hdr.rx_data = precvframe->u.hdr.rx_tail = pdata;
145 			precvframe->u.hdr.rx_end =  pdata + alloc_sz;
146 		} else {
147 			RTW_INFO("%s: rtw_skb_clone fail\n", __FUNCTION__);
148 			/* rtw_free_recvframe(precvframe, pfree_recv_queue); */
149 			/*exit_rtw_os_recv_resource_alloc;*/
150 			res = _FAIL;
151 		}
152 #endif
153 	}
154 
155 exit_rtw_os_recv_resource_alloc:
156 
157 	return res;
158 
159 }
160 
rtw_os_free_recvframe(union recv_frame * precvframe)161 void rtw_os_free_recvframe(union recv_frame *precvframe)
162 {
163 	if (precvframe->u.hdr.pkt) {
164 		rtw_os_pkt_free(precvframe->u.hdr.pkt);
165 		precvframe->u.hdr.pkt = NULL;
166 	}
167 }
168 
169 /* init os related resource in struct recv_priv */
rtw_os_recv_resource_init(struct recv_priv * precvpriv,_adapter * padapter)170 int rtw_os_recv_resource_init(struct recv_priv *precvpriv, _adapter *padapter)
171 {
172 	int	res = _SUCCESS;
173 
174 
175 #ifdef CONFIG_RTW_NAPI
176 	skb_queue_head_init(&precvpriv->rx_napi_skb_queue);
177 #endif /* CONFIG_RTW_NAPI */
178 
179 	return res;
180 }
181 
182 /* alloc os related resource in union recv_frame */
rtw_os_recv_resource_alloc(_adapter * padapter,union recv_frame * precvframe)183 int rtw_os_recv_resource_alloc(_adapter *padapter, union recv_frame *precvframe)
184 {
185 	int	res = _SUCCESS;
186 
187 	precvframe->u.hdr.pkt = NULL;
188 
189 	return res;
190 }
191 
192 /* free os related resource in union recv_frame */
rtw_os_recv_resource_free(struct recv_priv * precvpriv)193 void rtw_os_recv_resource_free(struct recv_priv *precvpriv)
194 {
195 	sint i;
196 	union recv_frame *precvframe;
197 	precvframe = (union recv_frame *) precvpriv->precv_frame_buf;
198 
199 
200 #ifdef CONFIG_RTW_NAPI
201 	if (skb_queue_len(&precvpriv->rx_napi_skb_queue))
202 		RTW_WARN("rx_napi_skb_queue not empty\n");
203 	rtw_skb_queue_purge(&precvpriv->rx_napi_skb_queue);
204 #endif /* CONFIG_RTW_NAPI */
205 
206 	for (i = 0; i < NR_RECVFRAME; i++) {
207 		rtw_os_free_recvframe(precvframe);
208 		precvframe++;
209 	}
210 }
211 
212 #if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
213 #if !defined(CONFIG_RTL8822B) && !defined(CONFIG_RTL8822C) && !defined(CONFIG_RTL8723F)
214 #ifdef CONFIG_SDIO_RX_COPY
sdio_init_recvbuf_with_skb(struct recv_priv * recvpriv,struct recv_buf * rbuf,u32 size)215 static int sdio_init_recvbuf_with_skb(struct recv_priv *recvpriv, struct recv_buf *rbuf, u32 size)
216 {
217 #ifdef CONFIG_PREALLOC_RX_SKB_BUFFER
218 	if (RBUF_IS_PREALLOC(rbuf)) {
219 		rbuf->pskb = rtw_alloc_skb_premem(size);
220 		if (!rbuf->pskb) {
221 			RTW_WARN("%s: Fail to get pre-alloc skb! size=%d\n", __func__, size);
222 			return _FAIL;
223 		}
224 		skb_set_tail_pointer(rbuf->pskb, 0); /* TODO: do this in RTKM */
225 	} else
226 #else
227 	{
228 		SIZE_PTR tmpaddr = 0;
229 		SIZE_PTR alignment = 0;
230 
231 		rbuf->pskb = rtw_skb_alloc(size + RECVBUFF_ALIGN_SZ);
232 		if (!rbuf->pskb)
233 			return _FAIL;
234 
235 		tmpaddr = (SIZE_PTR)rbuf->pskb->data;
236 		alignment = tmpaddr & (RECVBUFF_ALIGN_SZ - 1);
237 		skb_reserve(rbuf->pskb, (RECVBUFF_ALIGN_SZ - alignment));
238 	}
239 #endif
240 
241 	rbuf->pskb->dev = recvpriv->adapter->pnetdev;
242 
243 	/* init recvbuf */
244 	rbuf->phead = rbuf->pskb->head;
245 	rbuf->pdata = rbuf->pskb->data;
246 	rbuf->ptail = skb_tail_pointer(rbuf->pskb);
247 	rbuf->pend = skb_end_pointer(rbuf->pskb);
248 	rbuf->len = 0;
249 
250 	return _SUCCESS;
251 }
252 #endif /* CONFIG_SDIO_RX_COPY */
253 #endif /* !defined(CONFIG_RTL8822B) && !defined(CONFIG_RTL8822C) && !defined(CONFIG_RTL8723F) */
254 #endif /* defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) */
255 
256 /* alloc os related resource in struct recv_buf */
rtw_os_recvbuf_resource_alloc(_adapter * padapter,struct recv_buf * precvbuf,u32 size)257 int rtw_os_recvbuf_resource_alloc(_adapter *padapter, struct recv_buf *precvbuf, u32 size)
258 {
259 	int res = _SUCCESS;
260 
261 #ifdef CONFIG_USB_HCI
262 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_RX
263 	struct dvobj_priv	*pdvobjpriv = adapter_to_dvobj(padapter);
264 	struct usb_device	*pusbd = pdvobjpriv->pusbdev;
265 #endif
266 
267 	precvbuf->irp_pending = _FALSE;
268 	precvbuf->purb = usb_alloc_urb(0, GFP_KERNEL);
269 	if (precvbuf->purb == NULL)
270 		res = _FAIL;
271 
272 	precvbuf->pskb = NULL;
273 
274 	precvbuf->pallocated_buf  = precvbuf->pbuf = NULL;
275 
276 	precvbuf->pdata = precvbuf->phead = precvbuf->ptail = precvbuf->pend = NULL;
277 
278 	precvbuf->transfer_len = 0;
279 
280 	precvbuf->len = 0;
281 
282 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_RX
283 	precvbuf->pallocated_buf = rtw_usb_buffer_alloc(pusbd, (size_t)size, &precvbuf->dma_transfer_addr);
284 	precvbuf->pbuf = precvbuf->pallocated_buf;
285 	if (precvbuf->pallocated_buf == NULL)
286 		return _FAIL;
287 #endif /* CONFIG_USE_USB_BUFFER_ALLOC_RX */
288 
289 #elif defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
290 	#if !defined(CONFIG_RTL8822B) && !defined(CONFIG_RTL8822C) && !defined(CONFIG_RTL8723F)
291 	#ifdef CONFIG_SDIO_RX_COPY
292 	res = sdio_init_recvbuf_with_skb(&padapter->recvpriv, precvbuf, size);
293 	#endif
294 	#endif
295 
296 #endif /* CONFIG_XXX_HCI */
297 
298 	return res;
299 }
300 
301 /* free os related resource in struct recv_buf */
rtw_os_recvbuf_resource_free(_adapter * padapter,struct recv_buf * precvbuf)302 int rtw_os_recvbuf_resource_free(_adapter *padapter, struct recv_buf *precvbuf)
303 {
304 	int ret = _SUCCESS;
305 
306 #ifdef CONFIG_USB_HCI
307 
308 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_RX
309 
310 	struct dvobj_priv	*pdvobjpriv = adapter_to_dvobj(padapter);
311 	struct usb_device	*pusbd = pdvobjpriv->pusbdev;
312 
313 	rtw_usb_buffer_free(pusbd, (size_t)precvbuf->alloc_sz, precvbuf->pallocated_buf, precvbuf->dma_transfer_addr);
314 	precvbuf->pallocated_buf =  NULL;
315 	precvbuf->dma_transfer_addr = 0;
316 
317 #endif /* CONFIG_USE_USB_BUFFER_ALLOC_RX */
318 
319 	if (precvbuf->purb) {
320 		/* usb_kill_urb(precvbuf->purb); */
321 		usb_free_urb(precvbuf->purb);
322 	}
323 
324 #endif /* CONFIG_USB_HCI */
325 
326 
327 	if (precvbuf->pskb) {
328 #ifdef CONFIG_PREALLOC_RX_SKB_BUFFER
329 		if (rtw_free_skb_premem(precvbuf->pskb) != 0)
330 #endif
331 			rtw_skb_free(precvbuf->pskb);
332 	}
333 	return ret;
334 
335 }
336 
rtw_os_alloc_msdu_pkt(union recv_frame * prframe,const u8 * da,const u8 * sa,u8 * msdu,u16 msdu_len,enum rtw_rx_llc_hdl llc_hdl)337 _pkt *rtw_os_alloc_msdu_pkt(union recv_frame *prframe, const u8 *da, const u8 *sa
338 	, u8 *msdu ,u16 msdu_len, enum rtw_rx_llc_hdl llc_hdl)
339 {
340 	u8	*data_ptr;
341 	_pkt *sub_skb;
342 	struct rx_pkt_attrib *pattrib;
343 
344 	pattrib = &prframe->u.hdr.attrib;
345 
346 #ifdef CONFIG_SKB_COPY
347 	sub_skb = rtw_skb_alloc(msdu_len + 14);
348 	if (sub_skb) {
349 		skb_reserve(sub_skb, 14);
350 		data_ptr = (u8 *)skb_put(sub_skb, msdu_len);
351 		_rtw_memcpy(data_ptr, msdu, msdu_len);
352 	} else
353 #endif /* CONFIG_SKB_COPY */
354 	{
355 		sub_skb = rtw_skb_clone(prframe->u.hdr.pkt);
356 		if (sub_skb) {
357 			sub_skb->data = msdu;
358 			sub_skb->len = msdu_len;
359 			skb_set_tail_pointer(sub_skb, msdu_len);
360 		} else {
361 			RTW_INFO("%s(): rtw_skb_clone() Fail!!!\n", __FUNCTION__);
362 			return NULL;
363 		}
364 	}
365 
366 	if (llc_hdl) {
367 		/* remove RFC1042 or Bridge-Tunnel encapsulation and replace EtherType */
368 		skb_pull(sub_skb, SNAP_SIZE);
369 		_rtw_memcpy(skb_push(sub_skb, ETH_ALEN), sa, ETH_ALEN);
370 		_rtw_memcpy(skb_push(sub_skb, ETH_ALEN), da, ETH_ALEN);
371 	} else {
372 		/* Leave Ethernet header part of hdr and full payload */
373 		u16 len;
374 
375 		len = htons(sub_skb->len);
376 		_rtw_memcpy(skb_push(sub_skb, 2), &len, 2);
377 		_rtw_memcpy(skb_push(sub_skb, ETH_ALEN), sa, ETH_ALEN);
378 		_rtw_memcpy(skb_push(sub_skb, ETH_ALEN), da, ETH_ALEN);
379 	}
380 
381 	return sub_skb;
382 }
383 
384 #ifdef CONFIG_RTW_NAPI
napi_recv(_adapter * padapter,int budget)385 static int napi_recv(_adapter *padapter, int budget)
386 {
387 	_pkt *pskb;
388 	struct recv_priv *precvpriv = &padapter->recvpriv;
389 	int work_done = 0;
390 	struct registry_priv *pregistrypriv = &padapter->registrypriv;
391 	u8 rx_ok;
392 
393 
394 	while ((work_done < budget) &&
395 	       (!skb_queue_empty(&precvpriv->rx_napi_skb_queue))) {
396 		pskb = skb_dequeue(&precvpriv->rx_napi_skb_queue);
397 		if (!pskb)
398 			break;
399 
400 		rx_ok = _FALSE;
401 
402 #ifdef CONFIG_RTW_GRO
403 		/*
404 			cloned SKB use dataref to avoid kernel release it.
405 			But dataref changed in napi_gro_receive.
406 			So, we should prevent cloned SKB go into napi_gro_receive.
407 		*/
408 		if (pregistrypriv->en_gro && !skb_cloned(pskb)) {
409 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0))
410 			rtw_napi_gro_receive(&padapter->napi, pskb);
411 			rx_ok = _TRUE;
412 #else
413 			if (rtw_napi_gro_receive(&padapter->napi, pskb) != GRO_DROP)
414 				rx_ok = _TRUE;
415 #endif
416 			goto next;
417 		}
418 #endif /* CONFIG_RTW_GRO */
419 
420 		if (rtw_netif_receive_skb(padapter->pnetdev, pskb) == NET_RX_SUCCESS)
421 			rx_ok = _TRUE;
422 
423 next:
424 		if (rx_ok == _TRUE) {
425 			work_done++;
426 			DBG_COUNTER(padapter->rx_logs.os_netif_ok);
427 		} else {
428 			DBG_COUNTER(padapter->rx_logs.os_netif_err);
429 		}
430 	}
431 
432 	return work_done;
433 }
434 
rtw_recv_napi_poll(struct napi_struct * napi,int budget)435 int rtw_recv_napi_poll(struct napi_struct *napi, int budget)
436 {
437 	_adapter *padapter = container_of(napi, _adapter, napi);
438 	int work_done = 0;
439 	struct recv_priv *precvpriv = &padapter->recvpriv;
440 
441 
442 	work_done = napi_recv(padapter, budget);
443 	if (work_done < budget) {
444 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)) && defined(CONFIG_PCI_HCI)
445 		napi_complete_done(napi, work_done);
446 #else
447 		napi_complete(napi);
448 #endif
449 		if (!skb_queue_empty(&precvpriv->rx_napi_skb_queue))
450 			napi_schedule(napi);
451 	}
452 
453 	return work_done;
454 }
455 
456 #ifdef CONFIG_RTW_NAPI_DYNAMIC
dynamic_napi_th_chk(_adapter * adapter)457 void dynamic_napi_th_chk (_adapter *adapter)
458 {
459 
460 	if (adapter->registrypriv.en_napi) {
461 		struct dvobj_priv *dvobj;
462 		struct registry_priv *registry;
463 
464 		dvobj = adapter_to_dvobj(adapter);
465 		registry = &adapter->registrypriv;
466 		if (dvobj->traffic_stat.cur_rx_tp > registry->napi_threshold)
467 			dvobj->en_napi_dynamic = 1;
468 		else
469 			dvobj->en_napi_dynamic = 0;
470 	}
471 
472 }
473 #endif /* CONFIG_RTW_NAPI_DYNAMIC */
474 #endif /* CONFIG_RTW_NAPI */
475 
rtw_os_recv_indicate_pkt(_adapter * padapter,_pkt * pkt,union recv_frame * rframe)476 void rtw_os_recv_indicate_pkt(_adapter *padapter, _pkt *pkt, union recv_frame *rframe)
477 {
478 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
479 	struct recv_priv *precvpriv = &(padapter->recvpriv);
480 	struct registry_priv	*pregistrypriv = &padapter->registrypriv;
481 #ifdef CONFIG_BR_EXT
482 	void *br_port = NULL;
483 #endif
484 	int ret;
485 
486 	/* Indicat the packets to upper layer */
487 	if (pkt) {
488 		struct ethhdr *ehdr = (struct ethhdr *)pkt->data;
489 
490 		DBG_COUNTER(padapter->rx_logs.os_indicate);
491 
492 #ifdef CONFIG_BR_EXT
493 		if (!adapter_use_wds(padapter) && check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_ADHOC_STATE) == _TRUE) {
494 			/* Insert NAT2.5 RX here! */
495 			#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 35))
496 			br_port = padapter->pnetdev->br_port;
497 			#else
498 			rcu_read_lock();
499 			br_port = rcu_dereference(padapter->pnetdev->rx_handler_data);
500 			rcu_read_unlock();
501 			#endif
502 
503 			if (br_port) {
504 				int nat25_handle_frame(_adapter *priv, struct sk_buff *skb);
505 
506 				if (nat25_handle_frame(padapter, pkt) == -1) {
507 					/* priv->ext_stats.rx_data_drops++; */
508 					/* DEBUG_ERR("RX DROP: nat25_handle_frame fail!\n"); */
509 					/* return FAIL; */
510 
511 					#if 1
512 					/* bypass this frame to upper layer!! */
513 					#else
514 					rtw_skb_free(sub_skb);
515 					continue;
516 					#endif
517 				}
518 			}
519 		}
520 #endif /* CONFIG_BR_EXT */
521 
522 		/* After eth_type_trans process , pkt->data pointer will move from ethrnet header to ip header */
523 		pkt->protocol = eth_type_trans(pkt, padapter->pnetdev);
524 		pkt->dev = padapter->pnetdev;
525 		pkt->ip_summed = CHECKSUM_NONE; /* CONFIG_TCP_CSUM_OFFLOAD_RX */
526 
527 		if (padapter->recvpriv.ip_statistic.enabled)
528 			rtw_rx_dbg_monitor_ip_statistic(padapter, pkt);
529 
530 #ifdef CONFIG_TCP_CSUM_OFFLOAD_RX
531 		if ((rframe->u.hdr.attrib.csum_valid == 1)
532 		    && (rframe->u.hdr.attrib.csum_err == 0))
533 			pkt->ip_summed = CHECKSUM_UNNECESSARY;
534 #endif /* CONFIG_TCP_CSUM_OFFLOAD_RX */
535 
536 #ifdef CONFIG_RTW_NAPI
537 #ifdef CONFIG_RTW_NAPI_DYNAMIC
538 		if (!skb_queue_empty(&precvpriv->rx_napi_skb_queue)
539 			&& !adapter_to_dvobj(padapter)->en_napi_dynamic
540 			)
541 			napi_recv(padapter, RTL_NAPI_WEIGHT);
542 #endif
543 
544 		if (pregistrypriv->en_napi
545 			#ifdef CONFIG_RTW_NAPI_DYNAMIC
546 			&& adapter_to_dvobj(padapter)->en_napi_dynamic
547 			#endif
548 		) {
549 			skb_queue_tail(&precvpriv->rx_napi_skb_queue, pkt);
550 			#ifndef CONFIG_RTW_NAPI_V2
551 			napi_schedule(&padapter->napi);
552 			#endif
553 			return;
554 		}
555 #endif /* CONFIG_RTW_NAPI */
556 
557 		ret = rtw_netif_rx(padapter->pnetdev, pkt);
558 		if (ret == NET_RX_SUCCESS)
559 			DBG_COUNTER(padapter->rx_logs.os_netif_ok);
560 		else
561 			DBG_COUNTER(padapter->rx_logs.os_netif_err);
562 	}
563 }
564 
rtw_handle_tkip_mic_err(_adapter * padapter,struct sta_info * sta,u8 bgroup)565 void rtw_handle_tkip_mic_err(_adapter *padapter, struct sta_info *sta, u8 bgroup)
566 {
567 #ifdef CONFIG_IOCTL_CFG80211
568 	enum nl80211_key_type key_type = 0;
569 #endif
570 	union iwreq_data wrqu;
571 	struct iw_michaelmicfailure    ev;
572 	struct security_priv	*psecuritypriv = &padapter->securitypriv;
573 	systime cur_time = 0;
574 
575 	if (psecuritypriv->last_mic_err_time == 0)
576 		psecuritypriv->last_mic_err_time = rtw_get_current_time();
577 	else {
578 		cur_time = rtw_get_current_time();
579 
580 		if (cur_time - psecuritypriv->last_mic_err_time < 60 * HZ) {
581 			psecuritypriv->btkip_countermeasure = _TRUE;
582 			psecuritypriv->last_mic_err_time = 0;
583 			psecuritypriv->btkip_countermeasure_time = cur_time;
584 		} else
585 			psecuritypriv->last_mic_err_time = rtw_get_current_time();
586 	}
587 
588 #ifdef CONFIG_IOCTL_CFG80211
589 	if (bgroup)
590 		key_type |= NL80211_KEYTYPE_GROUP;
591 	else
592 		key_type |= NL80211_KEYTYPE_PAIRWISE;
593 
594 	cfg80211_michael_mic_failure(padapter->pnetdev, sta->cmn.mac_addr, key_type, -1, NULL, GFP_ATOMIC);
595 #endif
596 
597 	_rtw_memset(&ev, 0x00, sizeof(ev));
598 	if (bgroup)
599 		ev.flags |= IW_MICFAILURE_GROUP;
600 	else
601 		ev.flags |= IW_MICFAILURE_PAIRWISE;
602 
603 	ev.src_addr.sa_family = ARPHRD_ETHER;
604 	_rtw_memcpy(ev.src_addr.sa_data, sta->cmn.mac_addr, ETH_ALEN);
605 
606 	_rtw_memset(&wrqu, 0x00, sizeof(wrqu));
607 	wrqu.data.length = sizeof(ev);
608 
609 #ifndef CONFIG_IOCTL_CFG80211
610 	wireless_send_event(padapter->pnetdev, IWEVMICHAELMICFAILURE, &wrqu, (char *) &ev);
611 #endif
612 }
613 
614 #ifdef CONFIG_HOSTAPD_MLME
rtw_hostapd_mlme_rx(_adapter * padapter,union recv_frame * precv_frame)615 void rtw_hostapd_mlme_rx(_adapter *padapter, union recv_frame *precv_frame)
616 {
617 	_pkt *skb;
618 	struct hostapd_priv *phostapdpriv  = padapter->phostapdpriv;
619 	struct net_device *pmgnt_netdev = phostapdpriv->pmgnt_netdev;
620 
621 
622 	skb = precv_frame->u.hdr.pkt;
623 
624 	if (skb == NULL)
625 		return;
626 
627 	skb->data = precv_frame->u.hdr.rx_data;
628 	skb->tail = precv_frame->u.hdr.rx_tail;
629 	skb->len = precv_frame->u.hdr.len;
630 
631 	/* pskb_copy = rtw_skb_copy(skb);
632 	*	if(skb == NULL) goto _exit; */
633 
634 	skb->dev = pmgnt_netdev;
635 	skb->ip_summed = CHECKSUM_NONE;
636 	skb->pkt_type = PACKET_OTHERHOST;
637 	/* skb->protocol = __constant_htons(0x0019); ETH_P_80211_RAW */
638 	skb->protocol = __constant_htons(0x0003); /*ETH_P_80211_RAW*/
639 
640 	/* RTW_INFO("(1)data=0x%x, head=0x%x, tail=0x%x, mac_header=0x%x, len=%d\n", skb->data, skb->head, skb->tail, skb->mac_header, skb->len); */
641 
642 	/* skb->mac.raw = skb->data; */
643 	skb_reset_mac_header(skb);
644 
645 	/* skb_pull(skb, 24); */
646 	_rtw_memset(skb->cb, 0, sizeof(skb->cb));
647 
648 	rtw_netif_rx(pmgnt_netdev, skb);
649 
650 	precv_frame->u.hdr.pkt = NULL; /* set pointer to NULL before rtw_free_recvframe() if call rtw_netif_rx() */
651 }
652 #endif /* CONFIG_HOSTAPD_MLME */
653 
654 #ifdef CONFIG_WIFI_MONITOR
655 /*
656    precv_frame: impossible to be NULL
657    precv_frame: free by caller
658  */
rtw_recv_monitor(_adapter * padapter,union recv_frame * precv_frame)659 int rtw_recv_monitor(_adapter *padapter, union recv_frame *precv_frame)
660 {
661 	int ret = _FAIL;
662 	_pkt *skb;
663 
664 	skb = precv_frame->u.hdr.pkt;
665 	if (skb == NULL) {
666 		RTW_INFO("%s :skb==NULL something wrong!!!!\n", __func__);
667 		goto _recv_drop;
668 	}
669 
670 	skb->data = precv_frame->u.hdr.rx_data;
671 	skb_set_tail_pointer(skb, precv_frame->u.hdr.len);
672 	skb->len = precv_frame->u.hdr.len;
673 	skb->ip_summed = CHECKSUM_NONE;
674 	skb->pkt_type = PACKET_OTHERHOST;
675 	skb->protocol = htons(0x0019); /* ETH_P_80211_RAW */
676 
677 	/* send to kernel */
678 	rtw_netif_rx(padapter->pnetdev, skb);
679 
680 	/* pointers to NULL before rtw_free_recvframe() */
681 	precv_frame->u.hdr.pkt = NULL;
682 
683 	ret = _SUCCESS;
684 
685 _recv_drop:
686 	return ret;
687 }
688 #endif /* CONFIG_WIFI_MONITOR */
689 
rtw_rframe_set_os_pkt(union recv_frame * rframe)690 inline void rtw_rframe_set_os_pkt(union recv_frame *rframe)
691 {
692 	_pkt *skb = rframe->u.hdr.pkt;
693 
694 	skb->data = rframe->u.hdr.rx_data;
695 	skb_set_tail_pointer(skb, rframe->u.hdr.len);
696 	skb->len = rframe->u.hdr.len;
697 }
698 
rtw_recv_indicatepkt(_adapter * padapter,union recv_frame * precv_frame)699 int rtw_recv_indicatepkt(_adapter *padapter, union recv_frame *precv_frame)
700 {
701 	struct recv_priv *precvpriv;
702 	_queue	*pfree_recv_queue;
703 
704 	precvpriv = &(padapter->recvpriv);
705 	pfree_recv_queue = &(precvpriv->free_recv_queue);
706 
707 	if (precv_frame->u.hdr.pkt == NULL)
708 		goto _recv_indicatepkt_drop;
709 
710 	rtw_os_recv_indicate_pkt(padapter, precv_frame->u.hdr.pkt, precv_frame);
711 
712 	precv_frame->u.hdr.pkt = NULL;
713 	rtw_free_recvframe(precv_frame, pfree_recv_queue);
714 	return _SUCCESS;
715 
716 _recv_indicatepkt_drop:
717 	rtw_free_recvframe(precv_frame, pfree_recv_queue);
718 	DBG_COUNTER(padapter->rx_logs.os_indicate_err);
719 	return _FAIL;
720 }
721 
rtw_os_read_port(_adapter * padapter,struct recv_buf * precvbuf)722 void rtw_os_read_port(_adapter *padapter, struct recv_buf *precvbuf)
723 {
724 #ifdef CONFIG_USB_HCI
725 	struct recv_priv *precvpriv = &padapter->recvpriv;
726 
727 	precvbuf->ref_cnt--;
728 
729 	/* free skb in recv_buf */
730 	rtw_skb_free(precvbuf->pskb);
731 
732 	precvbuf->pskb = NULL;
733 
734 	if (precvbuf->irp_pending == _FALSE)
735 		rtw_read_port(padapter, precvpriv->ff_hwaddr, 0, (unsigned char *)precvbuf);
736 
737 
738 #endif
739 #if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
740 	precvbuf->pskb = NULL;
741 #endif
742 
743 }
744 
745