xref: /OK3568_Linux_fs/external/rkwifibt/drivers/rtl8188fu/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 			precvframe->u.hdr.pkt->dev = padapter->pnetdev;
143 			precvframe->u.hdr.rx_head = precvframe->u.hdr.rx_data = precvframe->u.hdr.rx_tail = pdata;
144 			precvframe->u.hdr.rx_end =  pdata + alloc_sz;
145 		} else {
146 			RTW_INFO("%s: rtw_skb_clone fail\n", __FUNCTION__);
147 			/* rtw_free_recvframe(precvframe, pfree_recv_queue); */
148 			/*exit_rtw_os_recv_resource_alloc;*/
149 			res = _FAIL;
150 		}
151 #endif
152 	}
153 
154 exit_rtw_os_recv_resource_alloc:
155 
156 	return res;
157 
158 }
159 
rtw_os_free_recvframe(union recv_frame * precvframe)160 void rtw_os_free_recvframe(union recv_frame *precvframe)
161 {
162 	if (precvframe->u.hdr.pkt) {
163 		rtw_os_pkt_free(precvframe->u.hdr.pkt);
164 		precvframe->u.hdr.pkt = NULL;
165 	}
166 }
167 
168 /* init os related resource in struct recv_priv */
rtw_os_recv_resource_init(struct recv_priv * precvpriv,_adapter * padapter)169 int rtw_os_recv_resource_init(struct recv_priv *precvpriv, _adapter *padapter)
170 {
171 	int	res = _SUCCESS;
172 
173 
174 #ifdef CONFIG_RTW_NAPI
175 	skb_queue_head_init(&precvpriv->rx_napi_skb_queue);
176 #endif /* CONFIG_RTW_NAPI */
177 
178 	return res;
179 }
180 
181 /* alloc os related resource in union recv_frame */
rtw_os_recv_resource_alloc(_adapter * padapter,union recv_frame * precvframe)182 int rtw_os_recv_resource_alloc(_adapter *padapter, union recv_frame *precvframe)
183 {
184 	int	res = _SUCCESS;
185 
186 	precvframe->u.hdr.pkt = NULL;
187 
188 	return res;
189 }
190 
191 /* free os related resource in union recv_frame */
rtw_os_recv_resource_free(struct recv_priv * precvpriv)192 void rtw_os_recv_resource_free(struct recv_priv *precvpriv)
193 {
194 	sint i;
195 	union recv_frame *precvframe;
196 	precvframe = (union recv_frame *) precvpriv->precv_frame_buf;
197 
198 
199 #ifdef CONFIG_RTW_NAPI
200 	if (skb_queue_len(&precvpriv->rx_napi_skb_queue))
201 		RTW_WARN("rx_napi_skb_queue not empty\n");
202 	rtw_skb_queue_purge(&precvpriv->rx_napi_skb_queue);
203 #endif /* CONFIG_RTW_NAPI */
204 
205 	for (i = 0; i < NR_RECVFRAME; i++) {
206 		rtw_os_free_recvframe(precvframe);
207 		precvframe++;
208 	}
209 }
210 
211 #if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
212 #if !defined(CONFIG_RTL8822B) && !defined(CONFIG_RTL8822C)
213 #ifdef CONFIG_SDIO_RX_COPY
sdio_init_recvbuf_with_skb(struct recv_priv * recvpriv,struct recv_buf * rbuf,u32 size)214 static int sdio_init_recvbuf_with_skb(struct recv_priv *recvpriv, struct recv_buf *rbuf, u32 size)
215 {
216 #ifdef CONFIG_PREALLOC_RX_SKB_BUFFER
217 	if (RBUF_IS_PREALLOC(rbuf)) {
218 		rbuf->pskb = rtw_alloc_skb_premem(size);
219 		if (!rbuf->pskb) {
220 			RTW_WARN("%s: Fail to get pre-alloc skb! size=%d\n", __func__, size);
221 			return _FAIL;
222 		}
223 		skb_set_tail_pointer(rbuf->pskb, 0); /* TODO: do this in RTKM */
224 	} else
225 #else
226 	{
227 		SIZE_PTR tmpaddr = 0;
228 		SIZE_PTR alignment = 0;
229 
230 		rbuf->pskb = rtw_skb_alloc(size + RECVBUFF_ALIGN_SZ);
231 		if (!rbuf->pskb)
232 			return _FAIL;
233 
234 		tmpaddr = (SIZE_PTR)rbuf->pskb->data;
235 		alignment = tmpaddr & (RECVBUFF_ALIGN_SZ - 1);
236 		skb_reserve(rbuf->pskb, (RECVBUFF_ALIGN_SZ - alignment));
237 	}
238 #endif
239 
240 	rbuf->pskb->dev = recvpriv->adapter->pnetdev;
241 
242 	/* init recvbuf */
243 	rbuf->phead = rbuf->pskb->head;
244 	rbuf->pdata = rbuf->pskb->data;
245 	rbuf->ptail = skb_tail_pointer(rbuf->pskb);
246 	rbuf->pend = skb_end_pointer(rbuf->pskb);
247 	rbuf->len = 0;
248 
249 	return _SUCCESS;
250 }
251 #endif /* CONFIG_SDIO_RX_COPY */
252 #endif /* !defined(CONFIG_RTL8822B) && !defined(CONFIG_RTL8822C) */
253 #endif /* defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) */
254 
255 /* alloc os related resource in struct recv_buf */
rtw_os_recvbuf_resource_alloc(_adapter * padapter,struct recv_buf * precvbuf,u32 size)256 int rtw_os_recvbuf_resource_alloc(_adapter *padapter, struct recv_buf *precvbuf, u32 size)
257 {
258 	int res = _SUCCESS;
259 
260 #ifdef CONFIG_USB_HCI
261 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_RX
262 	struct dvobj_priv	*pdvobjpriv = adapter_to_dvobj(padapter);
263 	struct usb_device	*pusbd = pdvobjpriv->pusbdev;
264 #endif
265 
266 	precvbuf->irp_pending = _FALSE;
267 	precvbuf->purb = usb_alloc_urb(0, GFP_KERNEL);
268 	if (precvbuf->purb == NULL)
269 		res = _FAIL;
270 
271 	precvbuf->pskb = NULL;
272 
273 	precvbuf->pallocated_buf  = precvbuf->pbuf = NULL;
274 
275 	precvbuf->pdata = precvbuf->phead = precvbuf->ptail = precvbuf->pend = NULL;
276 
277 	precvbuf->transfer_len = 0;
278 
279 	precvbuf->len = 0;
280 
281 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_RX
282 	precvbuf->pallocated_buf = rtw_usb_buffer_alloc(pusbd, (size_t)size, &precvbuf->dma_transfer_addr);
283 	precvbuf->pbuf = precvbuf->pallocated_buf;
284 	if (precvbuf->pallocated_buf == NULL)
285 		return _FAIL;
286 #endif /* CONFIG_USE_USB_BUFFER_ALLOC_RX */
287 
288 #elif defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
289 	#if !defined(CONFIG_RTL8822B) && !defined(CONFIG_RTL8822C)
290 	#ifdef CONFIG_SDIO_RX_COPY
291 	res = sdio_init_recvbuf_with_skb(&padapter->recvpriv, precvbuf, size);
292 	#endif
293 	#endif
294 
295 #endif /* CONFIG_XXX_HCI */
296 
297 	return res;
298 }
299 
300 /* free os related resource in struct recv_buf */
rtw_os_recvbuf_resource_free(_adapter * padapter,struct recv_buf * precvbuf)301 int rtw_os_recvbuf_resource_free(_adapter *padapter, struct recv_buf *precvbuf)
302 {
303 	int ret = _SUCCESS;
304 
305 #ifdef CONFIG_USB_HCI
306 
307 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_RX
308 
309 	struct dvobj_priv	*pdvobjpriv = adapter_to_dvobj(padapter);
310 	struct usb_device	*pusbd = pdvobjpriv->pusbdev;
311 
312 	rtw_usb_buffer_free(pusbd, (size_t)precvbuf->alloc_sz, precvbuf->pallocated_buf, precvbuf->dma_transfer_addr);
313 	precvbuf->pallocated_buf =  NULL;
314 	precvbuf->dma_transfer_addr = 0;
315 
316 #endif /* CONFIG_USE_USB_BUFFER_ALLOC_RX */
317 
318 	if (precvbuf->purb) {
319 		/* usb_kill_urb(precvbuf->purb); */
320 		usb_free_urb(precvbuf->purb);
321 	}
322 
323 #endif /* CONFIG_USB_HCI */
324 
325 
326 	if (precvbuf->pskb) {
327 #ifdef CONFIG_PREALLOC_RX_SKB_BUFFER
328 		if (rtw_free_skb_premem(precvbuf->pskb) != 0)
329 #endif
330 			rtw_skb_free(precvbuf->pskb);
331 	}
332 	return ret;
333 
334 }
335 
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)336 _pkt *rtw_os_alloc_msdu_pkt(union recv_frame *prframe, const u8 *da, const u8 *sa
337 	, u8 *msdu ,u16 msdu_len, enum rtw_rx_llc_hdl llc_hdl)
338 {
339 	u8	*data_ptr;
340 	_pkt *sub_skb;
341 	struct rx_pkt_attrib *pattrib;
342 
343 	pattrib = &prframe->u.hdr.attrib;
344 
345 #ifdef CONFIG_SKB_COPY
346 	sub_skb = rtw_skb_alloc(msdu_len + 14);
347 	if (sub_skb) {
348 		skb_reserve(sub_skb, 14);
349 		data_ptr = (u8 *)skb_put(sub_skb, msdu_len);
350 		_rtw_memcpy(data_ptr, msdu, msdu_len);
351 	} else
352 #endif /* CONFIG_SKB_COPY */
353 	{
354 		sub_skb = rtw_skb_clone(prframe->u.hdr.pkt);
355 		if (sub_skb) {
356 			sub_skb->data = msdu;
357 			sub_skb->len = msdu_len;
358 			skb_set_tail_pointer(sub_skb, msdu_len);
359 		} else {
360 			RTW_INFO("%s(): rtw_skb_clone() Fail!!!\n", __FUNCTION__);
361 			return NULL;
362 		}
363 	}
364 
365 	if (llc_hdl) {
366 		/* remove RFC1042 or Bridge-Tunnel encapsulation and replace EtherType */
367 		skb_pull(sub_skb, SNAP_SIZE);
368 		_rtw_memcpy(skb_push(sub_skb, ETH_ALEN), sa, ETH_ALEN);
369 		_rtw_memcpy(skb_push(sub_skb, ETH_ALEN), da, ETH_ALEN);
370 	} else {
371 		/* Leave Ethernet header part of hdr and full payload */
372 		u16 len;
373 
374 		len = htons(sub_skb->len);
375 		_rtw_memcpy(skb_push(sub_skb, 2), &len, 2);
376 		_rtw_memcpy(skb_push(sub_skb, ETH_ALEN), sa, ETH_ALEN);
377 		_rtw_memcpy(skb_push(sub_skb, ETH_ALEN), da, ETH_ALEN);
378 	}
379 
380 	return sub_skb;
381 }
382 
383 #ifdef CONFIG_RTW_NAPI
napi_recv(_adapter * padapter,int budget)384 static int napi_recv(_adapter *padapter, int budget)
385 {
386 	_pkt *pskb;
387 	struct recv_priv *precvpriv = &padapter->recvpriv;
388 	int work_done = 0;
389 	struct registry_priv *pregistrypriv = &padapter->registrypriv;
390 	u8 rx_ok;
391 
392 
393 	while ((work_done < budget) &&
394 	       (!skb_queue_empty(&precvpriv->rx_napi_skb_queue))) {
395 		pskb = skb_dequeue(&precvpriv->rx_napi_skb_queue);
396 		if (!pskb)
397 			break;
398 
399 		rx_ok = _FALSE;
400 
401 #ifdef CONFIG_RTW_GRO
402 		/*
403 			cloned SKB use dataref to avoid kernel release it.
404 			But dataref changed in napi_gro_receive.
405 			So, we should prevent cloned SKB go into napi_gro_receive.
406 		*/
407 		if (pregistrypriv->en_gro && !skb_cloned(pskb)) {
408 			if (rtw_napi_gro_receive(&padapter->napi, pskb) != GRO_DROP)
409 				rx_ok = _TRUE;
410 			goto next;
411 		}
412 #endif /* CONFIG_RTW_GRO */
413 
414 		if (rtw_netif_receive_skb(padapter->pnetdev, pskb) == NET_RX_SUCCESS)
415 			rx_ok = _TRUE;
416 
417 next:
418 		if (rx_ok == _TRUE) {
419 			work_done++;
420 			DBG_COUNTER(padapter->rx_logs.os_netif_ok);
421 		} else {
422 			DBG_COUNTER(padapter->rx_logs.os_netif_err);
423 		}
424 	}
425 
426 	return work_done;
427 }
428 
rtw_recv_napi_poll(struct napi_struct * napi,int budget)429 int rtw_recv_napi_poll(struct napi_struct *napi, int budget)
430 {
431 	_adapter *padapter = container_of(napi, _adapter, napi);
432 	int work_done = 0;
433 	struct recv_priv *precvpriv = &padapter->recvpriv;
434 
435 
436 	work_done = napi_recv(padapter, budget);
437 	if (work_done < budget) {
438 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)) && defined(CONFIG_PCI_HCI)
439 		napi_complete_done(napi, work_done);
440 #else
441 		napi_complete(napi);
442 #endif
443 		if (!skb_queue_empty(&precvpriv->rx_napi_skb_queue))
444 			napi_schedule(napi);
445 	}
446 
447 	return work_done;
448 }
449 
450 #ifdef CONFIG_RTW_NAPI_DYNAMIC
dynamic_napi_th_chk(_adapter * adapter)451 void dynamic_napi_th_chk (_adapter *adapter)
452 {
453 
454 	if (adapter->registrypriv.en_napi) {
455 		struct dvobj_priv *dvobj;
456 		struct registry_priv *registry;
457 
458 		dvobj = adapter_to_dvobj(adapter);
459 		registry = &adapter->registrypriv;
460 		if (dvobj->traffic_stat.cur_rx_tp > registry->napi_threshold)
461 			dvobj->en_napi_dynamic = 1;
462 		else
463 			dvobj->en_napi_dynamic = 0;
464 	}
465 
466 }
467 #endif /* CONFIG_RTW_NAPI_DYNAMIC */
468 #endif /* CONFIG_RTW_NAPI */
469 
rtw_os_recv_indicate_pkt(_adapter * padapter,_pkt * pkt,union recv_frame * rframe)470 void rtw_os_recv_indicate_pkt(_adapter *padapter, _pkt *pkt, union recv_frame *rframe)
471 {
472 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
473 	struct recv_priv *precvpriv = &(padapter->recvpriv);
474 	struct registry_priv	*pregistrypriv = &padapter->registrypriv;
475 #ifdef CONFIG_BR_EXT
476 	void *br_port = NULL;
477 #endif
478 	int ret;
479 
480 	/* Indicat the packets to upper layer */
481 	if (pkt) {
482 		struct ethhdr *ehdr = (struct ethhdr *)pkt->data;
483 
484 		DBG_COUNTER(padapter->rx_logs.os_indicate);
485 
486 #ifdef CONFIG_BR_EXT
487 		if (!adapter_use_wds(padapter) && check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_ADHOC_STATE) == _TRUE) {
488 			/* Insert NAT2.5 RX here! */
489 			#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 35))
490 			br_port = padapter->pnetdev->br_port;
491 			#else
492 			rcu_read_lock();
493 			br_port = rcu_dereference(padapter->pnetdev->rx_handler_data);
494 			rcu_read_unlock();
495 			#endif
496 
497 			if (br_port) {
498 				int nat25_handle_frame(_adapter *priv, struct sk_buff *skb);
499 
500 				if (nat25_handle_frame(padapter, pkt) == -1) {
501 					/* priv->ext_stats.rx_data_drops++; */
502 					/* DEBUG_ERR("RX DROP: nat25_handle_frame fail!\n"); */
503 					/* return FAIL; */
504 
505 					#if 1
506 					/* bypass this frame to upper layer!! */
507 					#else
508 					rtw_skb_free(sub_skb);
509 					continue;
510 					#endif
511 				}
512 			}
513 		}
514 #endif /* CONFIG_BR_EXT */
515 
516 		/* After eth_type_trans process , pkt->data pointer will move from ethrnet header to ip header */
517 		pkt->protocol = eth_type_trans(pkt, padapter->pnetdev);
518 		pkt->dev = padapter->pnetdev;
519 		pkt->ip_summed = CHECKSUM_NONE; /* CONFIG_TCP_CSUM_OFFLOAD_RX */
520 #ifdef CONFIG_TCP_CSUM_OFFLOAD_RX
521 		if ((rframe->u.hdr.attrib.csum_valid == 1)
522 		    && (rframe->u.hdr.attrib.csum_err == 0))
523 			pkt->ip_summed = CHECKSUM_UNNECESSARY;
524 #endif /* CONFIG_TCP_CSUM_OFFLOAD_RX */
525 
526 #ifdef CONFIG_RTW_NAPI
527 #ifdef CONFIG_RTW_NAPI_DYNAMIC
528 		if (!skb_queue_empty(&precvpriv->rx_napi_skb_queue)
529 			&& !adapter_to_dvobj(padapter)->en_napi_dynamic
530 			)
531 			napi_recv(padapter, RTL_NAPI_WEIGHT);
532 #endif
533 
534 		if (pregistrypriv->en_napi
535 			#ifdef CONFIG_RTW_NAPI_DYNAMIC
536 			&& adapter_to_dvobj(padapter)->en_napi_dynamic
537 			#endif
538 		) {
539 			skb_queue_tail(&precvpriv->rx_napi_skb_queue, pkt);
540 			#ifndef CONFIG_RTW_NAPI_V2
541 			napi_schedule(&padapter->napi);
542 			#endif
543 			return;
544 		}
545 #endif /* CONFIG_RTW_NAPI */
546 
547 		ret = rtw_netif_rx(padapter->pnetdev, pkt);
548 		if (ret == NET_RX_SUCCESS)
549 			DBG_COUNTER(padapter->rx_logs.os_netif_ok);
550 		else
551 			DBG_COUNTER(padapter->rx_logs.os_netif_err);
552 	}
553 }
554 
rtw_handle_tkip_mic_err(_adapter * padapter,struct sta_info * sta,u8 bgroup)555 void rtw_handle_tkip_mic_err(_adapter *padapter, struct sta_info *sta, u8 bgroup)
556 {
557 #ifdef CONFIG_IOCTL_CFG80211
558 	enum nl80211_key_type key_type = 0;
559 #endif
560 	union iwreq_data wrqu;
561 	struct iw_michaelmicfailure    ev;
562 	struct security_priv	*psecuritypriv = &padapter->securitypriv;
563 	systime cur_time = 0;
564 
565 	if (psecuritypriv->last_mic_err_time == 0)
566 		psecuritypriv->last_mic_err_time = rtw_get_current_time();
567 	else {
568 		cur_time = rtw_get_current_time();
569 
570 		if (cur_time - psecuritypriv->last_mic_err_time < 60 * HZ) {
571 			psecuritypriv->btkip_countermeasure = _TRUE;
572 			psecuritypriv->last_mic_err_time = 0;
573 			psecuritypriv->btkip_countermeasure_time = cur_time;
574 		} else
575 			psecuritypriv->last_mic_err_time = rtw_get_current_time();
576 	}
577 
578 #ifdef CONFIG_IOCTL_CFG80211
579 	if (bgroup)
580 		key_type |= NL80211_KEYTYPE_GROUP;
581 	else
582 		key_type |= NL80211_KEYTYPE_PAIRWISE;
583 
584 	cfg80211_michael_mic_failure(padapter->pnetdev, sta->cmn.mac_addr, key_type, -1, NULL, GFP_ATOMIC);
585 #endif
586 
587 	_rtw_memset(&ev, 0x00, sizeof(ev));
588 	if (bgroup)
589 		ev.flags |= IW_MICFAILURE_GROUP;
590 	else
591 		ev.flags |= IW_MICFAILURE_PAIRWISE;
592 
593 	ev.src_addr.sa_family = ARPHRD_ETHER;
594 	_rtw_memcpy(ev.src_addr.sa_data, sta->cmn.mac_addr, ETH_ALEN);
595 
596 	_rtw_memset(&wrqu, 0x00, sizeof(wrqu));
597 	wrqu.data.length = sizeof(ev);
598 
599 #ifndef CONFIG_IOCTL_CFG80211
600 	wireless_send_event(padapter->pnetdev, IWEVMICHAELMICFAILURE, &wrqu, (char *) &ev);
601 #endif
602 }
603 
604 #ifdef CONFIG_HOSTAPD_MLME
rtw_hostapd_mlme_rx(_adapter * padapter,union recv_frame * precv_frame)605 void rtw_hostapd_mlme_rx(_adapter *padapter, union recv_frame *precv_frame)
606 {
607 	_pkt *skb;
608 	struct hostapd_priv *phostapdpriv  = padapter->phostapdpriv;
609 	struct net_device *pmgnt_netdev = phostapdpriv->pmgnt_netdev;
610 
611 
612 	skb = precv_frame->u.hdr.pkt;
613 
614 	if (skb == NULL)
615 		return;
616 
617 	skb->data = precv_frame->u.hdr.rx_data;
618 	skb->tail = precv_frame->u.hdr.rx_tail;
619 	skb->len = precv_frame->u.hdr.len;
620 
621 	/* pskb_copy = rtw_skb_copy(skb);
622 	*	if(skb == NULL) goto _exit; */
623 
624 	skb->dev = pmgnt_netdev;
625 	skb->ip_summed = CHECKSUM_NONE;
626 	skb->pkt_type = PACKET_OTHERHOST;
627 	/* skb->protocol = __constant_htons(0x0019); ETH_P_80211_RAW */
628 	skb->protocol = __constant_htons(0x0003); /*ETH_P_80211_RAW*/
629 
630 	/* 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); */
631 
632 	/* skb->mac.raw = skb->data; */
633 	skb_reset_mac_header(skb);
634 
635 	/* skb_pull(skb, 24); */
636 	_rtw_memset(skb->cb, 0, sizeof(skb->cb));
637 
638 	rtw_netif_rx(pmgnt_netdev, skb);
639 
640 	precv_frame->u.hdr.pkt = NULL; /* set pointer to NULL before rtw_free_recvframe() if call rtw_netif_rx() */
641 }
642 #endif /* CONFIG_HOSTAPD_MLME */
643 
644 #ifdef CONFIG_WIFI_MONITOR
645 /*
646    precv_frame: impossible to be NULL
647    precv_frame: free by caller
648  */
rtw_recv_monitor(_adapter * padapter,union recv_frame * precv_frame)649 int rtw_recv_monitor(_adapter *padapter, union recv_frame *precv_frame)
650 {
651 	int ret = _FAIL;
652 	_pkt *skb;
653 
654 	skb = precv_frame->u.hdr.pkt;
655 	if (skb == NULL) {
656 		RTW_INFO("%s :skb==NULL something wrong!!!!\n", __func__);
657 		goto _recv_drop;
658 	}
659 
660 	skb->data = precv_frame->u.hdr.rx_data;
661 	skb_set_tail_pointer(skb, precv_frame->u.hdr.len);
662 	skb->len = precv_frame->u.hdr.len;
663 	skb->ip_summed = CHECKSUM_NONE;
664 	skb->pkt_type = PACKET_OTHERHOST;
665 	skb->protocol = htons(0x0019); /* ETH_P_80211_RAW */
666 
667 	/* send to kernel */
668 	rtw_netif_rx(padapter->pnetdev, skb);
669 
670 	/* pointers to NULL before rtw_free_recvframe() */
671 	precv_frame->u.hdr.pkt = NULL;
672 
673 	ret = _SUCCESS;
674 
675 _recv_drop:
676 	return ret;
677 }
678 #endif /* CONFIG_WIFI_MONITOR */
679 
rtw_rframe_set_os_pkt(union recv_frame * rframe)680 inline void rtw_rframe_set_os_pkt(union recv_frame *rframe)
681 {
682 	_pkt *skb = rframe->u.hdr.pkt;
683 
684 	skb->data = rframe->u.hdr.rx_data;
685 	skb_set_tail_pointer(skb, rframe->u.hdr.len);
686 	skb->len = rframe->u.hdr.len;
687 }
688 
rtw_recv_indicatepkt(_adapter * padapter,union recv_frame * precv_frame)689 int rtw_recv_indicatepkt(_adapter *padapter, union recv_frame *precv_frame)
690 {
691 	struct recv_priv *precvpriv;
692 	_queue	*pfree_recv_queue;
693 
694 	precvpriv = &(padapter->recvpriv);
695 	pfree_recv_queue = &(precvpriv->free_recv_queue);
696 
697 	if (precv_frame->u.hdr.pkt == NULL)
698 		goto _recv_indicatepkt_drop;
699 
700 	rtw_os_recv_indicate_pkt(padapter, precv_frame->u.hdr.pkt, precv_frame);
701 
702 	precv_frame->u.hdr.pkt = NULL;
703 	rtw_free_recvframe(precv_frame, pfree_recv_queue);
704 	return _SUCCESS;
705 
706 _recv_indicatepkt_drop:
707 	rtw_free_recvframe(precv_frame, pfree_recv_queue);
708 	DBG_COUNTER(padapter->rx_logs.os_indicate_err);
709 	return _FAIL;
710 }
711 
rtw_os_read_port(_adapter * padapter,struct recv_buf * precvbuf)712 void rtw_os_read_port(_adapter *padapter, struct recv_buf *precvbuf)
713 {
714 #ifdef CONFIG_USB_HCI
715 	struct recv_priv *precvpriv = &padapter->recvpriv;
716 
717 	precvbuf->ref_cnt--;
718 
719 	/* free skb in recv_buf */
720 	rtw_skb_free(precvbuf->pskb);
721 
722 	precvbuf->pskb = NULL;
723 
724 	if (precvbuf->irp_pending == _FALSE)
725 		rtw_read_port(padapter, precvpriv->ff_hwaddr, 0, (unsigned char *)precvbuf);
726 
727 
728 #endif
729 #if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
730 	precvbuf->pskb = NULL;
731 #endif
732 
733 }
734 
735