1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2012 Realtek Corporation. */
3 
4 #define _RTW_XMIT_C_
5 
6 #include "../include/osdep_service.h"
7 #include "../include/drv_types.h"
8 #include "../include/wifi.h"
9 #include "../include/osdep_intf.h"
10 #include "../include/usb_ops.h"
11 #include "../include/usb_osintf.h"
12 #include "../include/rtl8188e_xmit.h"
13 
14 static u8 P802_1H_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0xf8 };
15 static u8 RFC1042_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0x00 };
16 
_init_txservq(struct tx_servq * ptxservq)17 static void _init_txservq(struct tx_servq *ptxservq)
18 {
19 	INIT_LIST_HEAD(&ptxservq->tx_pending);
20 	rtw_init_queue(&ptxservq->sta_pending);
21 	ptxservq->qcnt = 0;
22 }
23 
_rtw_init_sta_xmit_priv(struct sta_xmit_priv * psta_xmitpriv)24 void	_rtw_init_sta_xmit_priv(struct sta_xmit_priv *psta_xmitpriv)
25 {
26 	memset((unsigned char *)psta_xmitpriv, 0, sizeof(struct sta_xmit_priv));
27 	spin_lock_init(&psta_xmitpriv->lock);
28 	_init_txservq(&psta_xmitpriv->be_q);
29 	_init_txservq(&psta_xmitpriv->bk_q);
30 	_init_txservq(&psta_xmitpriv->vi_q);
31 	_init_txservq(&psta_xmitpriv->vo_q);
32 	INIT_LIST_HEAD(&psta_xmitpriv->legacy_dz);
33 	INIT_LIST_HEAD(&psta_xmitpriv->apsd);
34 }
35 
rtw_xmit_resource_alloc(struct adapter * padapter,struct xmit_buf * pxmitbuf,u32 alloc_sz)36 static int rtw_xmit_resource_alloc(struct adapter *padapter, struct xmit_buf *pxmitbuf,
37 				   u32 alloc_sz)
38 {
39 	pxmitbuf->pallocated_buf = kzalloc(alloc_sz, GFP_KERNEL);
40 	if (!pxmitbuf->pallocated_buf)
41 		return _FAIL;
42 
43 	pxmitbuf->pbuf = (u8 *)ALIGN((size_t)(pxmitbuf->pallocated_buf), XMITBUF_ALIGN_SZ);
44 	pxmitbuf->dma_transfer_addr = 0;
45 
46 	pxmitbuf->pxmit_urb = usb_alloc_urb(0, GFP_KERNEL);
47 	if (!pxmitbuf->pxmit_urb) {
48 		kfree(pxmitbuf->pallocated_buf);
49 		return _FAIL;
50 	}
51 
52 	return _SUCCESS;
53 }
54 
rtw_xmit_resource_free(struct adapter * padapter,struct xmit_buf * pxmitbuf,u32 free_sz)55 static void rtw_xmit_resource_free(struct adapter *padapter, struct xmit_buf *pxmitbuf,
56 				   u32 free_sz)
57 {
58 	usb_free_urb(pxmitbuf->pxmit_urb);
59 	kfree(pxmitbuf->pallocated_buf);
60 }
61 
_rtw_init_xmit_priv(struct xmit_priv * pxmitpriv,struct adapter * padapter)62 s32	_rtw_init_xmit_priv(struct xmit_priv *pxmitpriv, struct adapter *padapter)
63 {
64 	int i;
65 	struct xmit_buf *pxmitbuf;
66 	struct xmit_frame *pxframe;
67 	int	res = _SUCCESS;
68 	u32 max_xmit_extbuf_size = MAX_XMIT_EXTBUF_SZ;
69 	u32 num_xmit_extbuf = NR_XMIT_EXTBUFF;
70 
71 	/*  We don't need to memset padapter->XXX to zero, because adapter is allocated by vzalloc(). */
72 
73 	spin_lock_init(&pxmitpriv->lock);
74 	sema_init(&pxmitpriv->terminate_xmitthread_sema, 0);
75 
76 	/*
77 	 * Please insert all the queue initializaiton using rtw_init_queue below
78 	 */
79 
80 	pxmitpriv->adapter = padapter;
81 
82 	rtw_init_queue(&pxmitpriv->be_pending);
83 	rtw_init_queue(&pxmitpriv->bk_pending);
84 	rtw_init_queue(&pxmitpriv->vi_pending);
85 	rtw_init_queue(&pxmitpriv->vo_pending);
86 	rtw_init_queue(&pxmitpriv->bm_pending);
87 
88 	rtw_init_queue(&pxmitpriv->free_xmit_queue);
89 
90 	/*
91 	 * Please allocate memory with the sz = (struct xmit_frame) * NR_XMITFRAME,
92 	 * and initialize free_xmit_frame below.
93 	 * Please also apply  free_txobj to link_up all the xmit_frames...
94 	 */
95 
96 	pxmitpriv->pallocated_frame_buf = vzalloc(NR_XMITFRAME * sizeof(struct xmit_frame) + 4);
97 
98 	if (!pxmitpriv->pallocated_frame_buf) {
99 		pxmitpriv->pxmit_frame_buf = NULL;
100 		res = _FAIL;
101 		goto exit;
102 	}
103 	pxmitpriv->pxmit_frame_buf = (u8 *)ALIGN((size_t)(pxmitpriv->pallocated_frame_buf), 4);
104 	/* pxmitpriv->pxmit_frame_buf = pxmitpriv->pallocated_frame_buf + 4 - */
105 	/* 						((size_t) (pxmitpriv->pallocated_frame_buf) &3); */
106 
107 	pxframe = (struct xmit_frame *)pxmitpriv->pxmit_frame_buf;
108 
109 	for (i = 0; i < NR_XMITFRAME; i++) {
110 		INIT_LIST_HEAD(&pxframe->list);
111 
112 		pxframe->padapter = padapter;
113 		pxframe->frame_tag = NULL_FRAMETAG;
114 
115 		pxframe->pkt = NULL;
116 
117 		pxframe->buf_addr = NULL;
118 		pxframe->pxmitbuf = NULL;
119 
120 		list_add_tail(&pxframe->list, &pxmitpriv->free_xmit_queue.queue);
121 
122 		pxframe++;
123 	}
124 
125 	pxmitpriv->free_xmitframe_cnt = NR_XMITFRAME;
126 
127 	pxmitpriv->frag_len = MAX_FRAG_THRESHOLD;
128 
129 	/* init xmit_buf */
130 	rtw_init_queue(&pxmitpriv->free_xmitbuf_queue);
131 	rtw_init_queue(&pxmitpriv->pending_xmitbuf_queue);
132 
133 	pxmitpriv->pallocated_xmitbuf = vzalloc(NR_XMITBUFF * sizeof(struct xmit_buf) + 4);
134 
135 	if (!pxmitpriv->pallocated_xmitbuf) {
136 		res = _FAIL;
137 		goto free_frame_buf;
138 	}
139 
140 	pxmitpriv->pxmitbuf = (u8 *)ALIGN((size_t)(pxmitpriv->pallocated_xmitbuf), 4);
141 	/* pxmitpriv->pxmitbuf = pxmitpriv->pallocated_xmitbuf + 4 - */
142 	/* 						((size_t) (pxmitpriv->pallocated_xmitbuf) &3); */
143 
144 	pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
145 
146 	for (i = 0; i < NR_XMITBUFF; i++) {
147 		INIT_LIST_HEAD(&pxmitbuf->list);
148 
149 		pxmitbuf->priv_data = NULL;
150 		pxmitbuf->padapter = padapter;
151 		pxmitbuf->ext_tag = false;
152 
153 		/* Tx buf allocation may fail sometimes, so sleep and retry. */
154 		res = rtw_xmit_resource_alloc(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ));
155 		if (res == _FAIL) {
156 			msleep(10);
157 			res = rtw_xmit_resource_alloc(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ));
158 			if (res == _FAIL)
159 				goto free_xmitbuf;
160 		}
161 
162 		pxmitbuf->flags = XMIT_VO_QUEUE;
163 
164 		list_add_tail(&pxmitbuf->list, &pxmitpriv->free_xmitbuf_queue.queue);
165 		pxmitbuf++;
166 	}
167 
168 	pxmitpriv->free_xmitbuf_cnt = NR_XMITBUFF;
169 
170 	/*  Init xmit extension buff */
171 	rtw_init_queue(&pxmitpriv->free_xmit_extbuf_queue);
172 
173 	pxmitpriv->pallocated_xmit_extbuf = vzalloc(num_xmit_extbuf * sizeof(struct xmit_buf) + 4);
174 
175 	if (!pxmitpriv->pallocated_xmit_extbuf) {
176 		res = _FAIL;
177 		goto free_xmitbuf;
178 	}
179 
180 	pxmitpriv->pxmit_extbuf = (u8 *)ALIGN((size_t)(pxmitpriv->pallocated_xmit_extbuf), 4);
181 
182 	pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmit_extbuf;
183 
184 	for (i = 0; i < num_xmit_extbuf; i++) {
185 		INIT_LIST_HEAD(&pxmitbuf->list);
186 
187 		pxmitbuf->priv_data = NULL;
188 		pxmitbuf->padapter = padapter;
189 		pxmitbuf->ext_tag = true;
190 
191 		res = rtw_xmit_resource_alloc(padapter, pxmitbuf, max_xmit_extbuf_size + XMITBUF_ALIGN_SZ);
192 		if (res == _FAIL) {
193 			res = _FAIL;
194 			goto free_xmit_extbuf;
195 		}
196 
197 		list_add_tail(&pxmitbuf->list, &pxmitpriv->free_xmit_extbuf_queue.queue);
198 		pxmitbuf++;
199 	}
200 
201 	pxmitpriv->free_xmit_extbuf_cnt = num_xmit_extbuf;
202 
203 	if (rtw_alloc_hwxmits(padapter)) {
204 		res = _FAIL;
205 		goto free_xmit_extbuf;
206 	}
207 
208 	rtw_init_hwxmits(pxmitpriv->hwxmits, pxmitpriv->hwxmit_entry);
209 
210 	for (i = 0; i < 4; i++)
211 		pxmitpriv->wmm_para_seq[i] = i;
212 
213 	pxmitpriv->txirp_cnt = 1;
214 
215 	sema_init(&pxmitpriv->tx_retevt, 0);
216 
217 	/* per AC pending irp */
218 	pxmitpriv->beq_cnt = 0;
219 	pxmitpriv->bkq_cnt = 0;
220 	pxmitpriv->viq_cnt = 0;
221 	pxmitpriv->voq_cnt = 0;
222 
223 	pxmitpriv->ack_tx = false;
224 	mutex_init(&pxmitpriv->ack_tx_mutex);
225 	rtw_sctx_init(&pxmitpriv->ack_tx_ops, 0);
226 
227 	rtl8188eu_init_xmit_priv(padapter);
228 
229 	return _SUCCESS;
230 
231 free_xmit_extbuf:
232 	pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmit_extbuf;
233 	while (i--) {
234 		rtw_xmit_resource_free(padapter, pxmitbuf, (max_xmit_extbuf_size + XMITBUF_ALIGN_SZ));
235 		pxmitbuf++;
236 	}
237 	vfree(pxmitpriv->pallocated_xmit_extbuf);
238 	i = NR_XMITBUFF;
239 free_xmitbuf:
240 	pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
241 	while (i--) {
242 		rtw_xmit_resource_free(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ));
243 		pxmitbuf++;
244 	}
245 	vfree(pxmitpriv->pallocated_xmitbuf);
246 free_frame_buf:
247 	vfree(pxmitpriv->pallocated_frame_buf);
248 exit:
249 	return res;
250 }
251 
rtw_pkt_complete(struct adapter * padapter,struct sk_buff * pkt)252 static void rtw_pkt_complete(struct adapter *padapter, struct sk_buff *pkt)
253 {
254 	u16 queue;
255 	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
256 
257 	queue = skb_get_queue_mapping(pkt);
258 	if (padapter->registrypriv.wifi_spec) {
259 		if (__netif_subqueue_stopped(padapter->pnetdev, queue) &&
260 		    (pxmitpriv->hwxmits[queue].accnt < WMM_XMIT_THRESHOLD))
261 			netif_wake_subqueue(padapter->pnetdev, queue);
262 	} else {
263 		if (__netif_subqueue_stopped(padapter->pnetdev, queue))
264 			netif_wake_subqueue(padapter->pnetdev, queue);
265 	}
266 
267 	dev_kfree_skb_any(pkt);
268 }
269 
rtw_xmit_complete(struct adapter * padapter,struct xmit_frame * pxframe)270 void rtw_xmit_complete(struct adapter *padapter, struct xmit_frame *pxframe)
271 {
272 	if (pxframe->pkt)
273 		rtw_pkt_complete(padapter, pxframe->pkt);
274 	pxframe->pkt = NULL;
275 }
276 
_rtw_free_xmit_priv(struct xmit_priv * pxmitpriv)277 void _rtw_free_xmit_priv(struct xmit_priv *pxmitpriv)
278 {
279 	int i;
280 	struct adapter *padapter = pxmitpriv->adapter;
281 	struct xmit_frame *pxmitframe = (struct xmit_frame *)pxmitpriv->pxmit_frame_buf;
282 	struct xmit_buf *pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
283 	u32 max_xmit_extbuf_size = MAX_XMIT_EXTBUF_SZ;
284 	u32 num_xmit_extbuf = NR_XMIT_EXTBUFF;
285 
286 	if (!pxmitpriv->pxmit_frame_buf)
287 		return;
288 
289 	for (i = 0; i < NR_XMITFRAME; i++) {
290 		rtw_xmit_complete(padapter, pxmitframe);
291 
292 		pxmitframe++;
293 	}
294 
295 	for (i = 0; i < NR_XMITBUFF; i++) {
296 		rtw_xmit_resource_free(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ));
297 		pxmitbuf++;
298 	}
299 
300 	vfree(pxmitpriv->pallocated_frame_buf);
301 
302 	vfree(pxmitpriv->pallocated_xmitbuf);
303 
304 	pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmit_extbuf;
305 	for (i = 0; i < num_xmit_extbuf; i++) {
306 		rtw_xmit_resource_free(padapter, pxmitbuf, (max_xmit_extbuf_size + XMITBUF_ALIGN_SZ));
307 		pxmitbuf++;
308 	}
309 
310 	vfree(pxmitpriv->pallocated_xmit_extbuf);
311 
312 	rtw_free_hwxmits(padapter);
313 
314 	mutex_destroy(&pxmitpriv->ack_tx_mutex);
315 }
316 
update_attrib_vcs_info(struct adapter * padapter,struct xmit_frame * pxmitframe)317 static void update_attrib_vcs_info(struct adapter *padapter, struct xmit_frame *pxmitframe)
318 {
319 	u32	sz;
320 	struct pkt_attrib	*pattrib = &pxmitframe->attrib;
321 	struct sta_info	*psta = pattrib->psta;
322 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
323 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
324 
325 	if (pattrib->nr_frags != 1)
326 		sz = padapter->xmitpriv.frag_len;
327 	else /* no frag */
328 		sz = pattrib->last_txcmdsz;
329 
330 	/*  (1) RTS_Threshold is compared to the MPDU, not MSDU. */
331 	/*  (2) If there are more than one frag in  this MSDU, only the first frag uses protection frame. */
332 	/* 		Other fragments are protected by previous fragment. */
333 	/* 		So we only need to check the length of first fragment. */
334 	if (pmlmeext->cur_wireless_mode < WIRELESS_11_24N  || padapter->registrypriv.wifi_spec) {
335 		if (sz > padapter->registrypriv.rts_thresh) {
336 			pattrib->vcs_mode = RTS_CTS;
337 		} else {
338 			if (psta->rtsen)
339 				pattrib->vcs_mode = RTS_CTS;
340 			else if (psta->cts2self)
341 				pattrib->vcs_mode = CTS_TO_SELF;
342 			else
343 				pattrib->vcs_mode = NONE_VCS;
344 		}
345 	} else {
346 		while (true) {
347 			/* IOT action */
348 			if ((pmlmeinfo->assoc_AP_vendor == HT_IOT_PEER_ATHEROS) && pattrib->ampdu_en &&
349 			    (padapter->securitypriv.dot11PrivacyAlgrthm == _AES_)) {
350 				pattrib->vcs_mode = CTS_TO_SELF;
351 				break;
352 			}
353 
354 			/* check ERP protection */
355 			if (psta->rtsen || psta->cts2self) {
356 				if (psta->rtsen)
357 					pattrib->vcs_mode = RTS_CTS;
358 				else if (psta->cts2self)
359 					pattrib->vcs_mode = CTS_TO_SELF;
360 
361 				break;
362 			}
363 
364 			/* check HT op mode */
365 			if (pattrib->ht_en) {
366 				u8 htopmode = pmlmeinfo->HT_protection;
367 
368 				if ((pmlmeext->cur_bwmode && (htopmode == 2 || htopmode == 3)) ||
369 				    (!pmlmeext->cur_bwmode && htopmode == 3)) {
370 					pattrib->vcs_mode = RTS_CTS;
371 					break;
372 				}
373 			}
374 
375 			/* check rts */
376 			if (sz > padapter->registrypriv.rts_thresh) {
377 				pattrib->vcs_mode = RTS_CTS;
378 				break;
379 			}
380 
381 			/* to do list: check MIMO power save condition. */
382 
383 			/* check AMPDU aggregation for TXOP */
384 			if (pattrib->ampdu_en) {
385 				pattrib->vcs_mode = RTS_CTS;
386 				break;
387 			}
388 
389 			pattrib->vcs_mode = NONE_VCS;
390 			break;
391 		}
392 	}
393 }
394 
update_attrib_phy_info(struct pkt_attrib * pattrib,struct sta_info * psta)395 static void update_attrib_phy_info(struct pkt_attrib *pattrib, struct sta_info *psta)
396 {
397 	/*if (psta->rtsen)
398 		pattrib->vcs_mode = RTS_CTS;
399 	else if (psta->cts2self)
400 		pattrib->vcs_mode = CTS_TO_SELF;
401 	else
402 		pattrib->vcs_mode = NONE_VCS;*/
403 
404 	pattrib->mdata = 0;
405 	pattrib->eosp = 0;
406 	pattrib->triggered = 0;
407 
408 	/* qos_en, ht_en, init rate, , bw, ch_offset, sgi */
409 	pattrib->qos_en = psta->qos_option;
410 
411 	pattrib->raid = psta->raid;
412 	pattrib->ht_en = psta->htpriv.ht_option;
413 	pattrib->bwmode = psta->htpriv.bwmode;
414 	pattrib->ch_offset = psta->htpriv.ch_offset;
415 	pattrib->sgi = psta->htpriv.sgi;
416 	pattrib->ampdu_en = false;
417 	pattrib->retry_ctrl = false;
418 }
419 
qos_acm(u8 acm_mask,u8 priority)420 u8	qos_acm(u8 acm_mask, u8 priority)
421 {
422 	u8	change_priority = priority;
423 
424 	switch (priority) {
425 	case 0:
426 	case 3:
427 		if (acm_mask & BIT(1))
428 			change_priority = 1;
429 		break;
430 	case 1:
431 	case 2:
432 		break;
433 	case 4:
434 	case 5:
435 		if (acm_mask & BIT(2))
436 			change_priority = 0;
437 		break;
438 	case 6:
439 	case 7:
440 		if (acm_mask & BIT(3))
441 			change_priority = 5;
442 		break;
443 	default:
444 		break;
445 	}
446 
447 	return change_priority;
448 }
449 
rtw_open_pktfile(struct sk_buff * pktptr,struct pkt_file * pfile)450 static void rtw_open_pktfile(struct sk_buff *pktptr, struct pkt_file *pfile)
451 {
452 	if (!pktptr) {
453 		pr_err("8188eu: pktptr is NULL\n");
454 		return;
455 	}
456 	if (!pfile) {
457 		pr_err("8188eu: pfile is NULL\n");
458 		return;
459 	}
460 	pfile->pkt = pktptr;
461 	pfile->cur_addr = pktptr->data;
462 	pfile->buf_start = pktptr->data;
463 	pfile->pkt_len = pktptr->len;
464 	pfile->buf_len = pktptr->len;
465 
466 	pfile->cur_buffer = pfile->buf_start;
467 }
468 
rtw_remainder_len(struct pkt_file * pfile)469 static uint rtw_remainder_len(struct pkt_file *pfile)
470 {
471 	return pfile->buf_len - ((size_t)(pfile->cur_addr) -
472 	       (size_t)(pfile->buf_start));
473 }
474 
rtw_pktfile_read(struct pkt_file * pfile,u8 * rmem,uint rlen)475 static uint rtw_pktfile_read(struct pkt_file *pfile, u8 *rmem, uint rlen)
476 {
477 	uint len;
478 
479 	len = rtw_remainder_len(pfile);
480 	len = (rlen > len) ? len : rlen;
481 
482 	if (rmem)
483 		skb_copy_bits(pfile->pkt, pfile->buf_len - pfile->pkt_len, rmem, len);
484 
485 	pfile->cur_addr += len;
486 	pfile->pkt_len -= len;
487 
488 	return len;
489 }
490 
set_qos(struct pkt_file * ppktfile,struct pkt_attrib * pattrib)491 static void set_qos(struct pkt_file *ppktfile, struct pkt_attrib *pattrib)
492 {
493 	struct ethhdr etherhdr;
494 	struct iphdr ip_hdr;
495 	s32 user_prio = 0;
496 
497 	rtw_open_pktfile(ppktfile->pkt, ppktfile);
498 	rtw_pktfile_read(ppktfile, (unsigned char *)&etherhdr, ETH_HLEN);
499 
500 	/*  get user_prio from IP hdr */
501 	if (pattrib->ether_type == 0x0800) {
502 		rtw_pktfile_read(ppktfile, (u8 *)&ip_hdr, sizeof(ip_hdr));
503 /* 		user_prio = (ntohs(ip_hdr.tos) >> 5) & 0x3; */
504 		user_prio = ip_hdr.tos >> 5;
505 	} else if (pattrib->ether_type == 0x888e) {
506 		/*  "When priority processing of data frames is supported, */
507 		/*  a STA's SME should send EAPOL-Key frames at the highest priority." */
508 		user_prio = 7;
509 	}
510 
511 	pattrib->priority = user_prio;
512 	pattrib->hdrlen = WLAN_HDR_A3_QOS_LEN;
513 	pattrib->subtype = IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA;
514 }
515 
update_attrib(struct adapter * padapter,struct sk_buff * pkt,struct pkt_attrib * pattrib)516 static s32 update_attrib(struct adapter *padapter, struct sk_buff *pkt, struct pkt_attrib *pattrib)
517 {
518 	struct pkt_file pktfile;
519 	struct sta_info *psta = NULL;
520 	struct ethhdr etherhdr;
521 
522 	bool bmcast;
523 	struct sta_priv		*pstapriv = &padapter->stapriv;
524 	struct security_priv	*psecuritypriv = &padapter->securitypriv;
525 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
526 	struct qos_priv		*pqospriv = &pmlmepriv->qospriv;
527 	int res = _SUCCESS;
528 
529 
530 
531 	rtw_open_pktfile(pkt, &pktfile);
532 	rtw_pktfile_read(&pktfile, (u8 *)&etherhdr, ETH_HLEN);
533 
534 	pattrib->ether_type = ntohs(etherhdr.h_proto);
535 
536 	memcpy(pattrib->dst, &etherhdr.h_dest, ETH_ALEN);
537 	memcpy(pattrib->src, &etherhdr.h_source, ETH_ALEN);
538 
539 	pattrib->pctrl = 0;
540 
541 	if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) ||
542 	    check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
543 		memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
544 		memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
545 	} else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
546 		memcpy(pattrib->ra, get_bssid(pmlmepriv), ETH_ALEN);
547 		memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
548 	} else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
549 		memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
550 		memcpy(pattrib->ta, get_bssid(pmlmepriv), ETH_ALEN);
551 	}
552 
553 	pattrib->pktlen = pktfile.pkt_len;
554 
555 	if (pattrib->ether_type == ETH_P_IP) {
556 		/*  The following is for DHCP and ARP packet, we use cck1M to tx these packets and let LPS awake some time */
557 		/*  to prevent DHCP protocol fail */
558 		u8 tmp[24];
559 
560 		rtw_pktfile_read(&pktfile, &tmp[0], 24);
561 		pattrib->dhcp_pkt = 0;
562 		if (pktfile.pkt_len > 282) {/* MINIMUM_DHCP_PACKET_SIZE) { */
563 			if (((tmp[21] == 68) && (tmp[23] == 67)) ||
564 			    ((tmp[21] == 67) && (tmp[23] == 68))) {
565 				/*  68 : UDP BOOTP client */
566 				/*  67 : UDP BOOTP server */
567 				/*  Use low rate to send DHCP packet. */
568 				pattrib->dhcp_pkt = 1;
569 			}
570 		}
571 	}
572 
573 	/*  If EAPOL , ARP , OR DHCP packet, driver must be in active mode. */
574 	if ((pattrib->ether_type == 0x0806) || (pattrib->ether_type == 0x888e) || (pattrib->dhcp_pkt == 1))
575 		rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_SPECIAL_PACKET, 1);
576 
577 	bmcast = is_multicast_ether_addr(pattrib->ra);
578 
579 	/*  get sta_info */
580 	if (bmcast) {
581 		psta = rtw_get_bcmc_stainfo(padapter);
582 	} else {
583 		psta = rtw_get_stainfo(pstapriv, pattrib->ra);
584 		if (!psta) { /*  if we cannot get psta => drrp the pkt */
585 			res = _FAIL;
586 			goto exit;
587 		} else if (check_fwstate(pmlmepriv, WIFI_AP_STATE) && !(psta->state & _FW_LINKED)) {
588 			res = _FAIL;
589 			goto exit;
590 		}
591 	}
592 
593 	if (psta) {
594 		pattrib->mac_id = psta->mac_id;
595 		pattrib->psta = psta;
596 	} else {
597 		/*  if we cannot get psta => drop the pkt */
598 		res = _FAIL;
599 		goto exit;
600 	}
601 
602 	pattrib->ack_policy = 0;
603 	/*  get ether_hdr_len */
604 	pattrib->pkt_hdrlen = ETH_HLEN;/* pattrib->ether_type == 0x8100) ? (14 + 4): 14; vlan tag */
605 
606 	pattrib->hdrlen = WLAN_HDR_A3_LEN;
607 	pattrib->subtype = IEEE80211_FTYPE_DATA;
608 	pattrib->priority = 0;
609 
610 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE | WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE)) {
611 		if (psta->qos_option)
612 			set_qos(&pktfile, pattrib);
613 	} else {
614 		if (pqospriv->qos_option) {
615 			set_qos(&pktfile, pattrib);
616 
617 			if (pmlmepriv->acm_mask != 0)
618 				pattrib->priority = qos_acm(pmlmepriv->acm_mask, pattrib->priority);
619 		}
620 	}
621 
622 	if (psta->ieee8021x_blocked) {
623 		pattrib->encrypt = 0;
624 
625 		if ((pattrib->ether_type != 0x888e) && !check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
626 			res = _FAIL;
627 			goto exit;
628 		}
629 	} else {
630 		GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt, bmcast);
631 
632 		switch (psecuritypriv->dot11AuthAlgrthm) {
633 		case dot11AuthAlgrthm_Open:
634 		case dot11AuthAlgrthm_Shared:
635 		case dot11AuthAlgrthm_Auto:
636 			pattrib->key_idx = (u8)psecuritypriv->dot11PrivacyKeyIndex;
637 			break;
638 		case dot11AuthAlgrthm_8021X:
639 			if (bmcast)
640 				pattrib->key_idx = (u8)psecuritypriv->dot118021XGrpKeyid;
641 			else
642 				pattrib->key_idx = 0;
643 			break;
644 		default:
645 			pattrib->key_idx = 0;
646 			break;
647 		}
648 	}
649 
650 	switch (pattrib->encrypt) {
651 	case _WEP40_:
652 	case _WEP104_:
653 		pattrib->iv_len = 4;
654 		pattrib->icv_len = 4;
655 		break;
656 	case _TKIP_:
657 		pattrib->iv_len = 8;
658 		pattrib->icv_len = 4;
659 
660 		if (padapter->securitypriv.busetkipkey == _FAIL) {
661 			res = _FAIL;
662 			goto exit;
663 		}
664 		break;
665 	case _AES_:
666 		pattrib->iv_len = 8;
667 		pattrib->icv_len = 8;
668 		break;
669 	default:
670 		pattrib->iv_len = 0;
671 		pattrib->icv_len = 0;
672 		break;
673 	}
674 
675 	if (pattrib->encrypt &&
676 	    (padapter->securitypriv.sw_encrypt || !psecuritypriv->hw_decrypted))
677 		pattrib->bswenc = true;
678 	else
679 		pattrib->bswenc = false;
680 
681 	update_attrib_phy_info(pattrib, psta);
682 
683 exit:
684 
685 	return res;
686 }
687 
xmitframe_addmic(struct adapter * padapter,struct xmit_frame * pxmitframe)688 static s32 xmitframe_addmic(struct adapter *padapter, struct xmit_frame *pxmitframe)
689 {
690 	int curfragnum, length;
691 	u8	*pframe, *payload, mic[8];
692 	struct	mic_data micdata;
693 	struct	sta_info *stainfo;
694 	struct	pkt_attrib *pattrib = &pxmitframe->attrib;
695 	struct	security_priv	*psecuritypriv = &padapter->securitypriv;
696 	struct	xmit_priv *pxmitpriv = &padapter->xmitpriv;
697 	u8 priority[4] = {0x0, 0x0, 0x0, 0x0};
698 	u8 hw_hdr_offset = 0;
699 
700 	if (pattrib->psta)
701 		stainfo = pattrib->psta;
702 	else
703 		stainfo = rtw_get_stainfo(&padapter->stapriv, &pattrib->ra[0]);
704 
705 	hw_hdr_offset = TXDESC_SIZE + (pxmitframe->pkt_offset * PACKET_OFFSET_SZ);
706 
707 	if (pattrib->encrypt == _TKIP_) {/* if (psecuritypriv->dot11PrivacyAlgrthm == _TKIP_PRIVACY_) */
708 		/* encode mic code */
709 		if (stainfo) {
710 			u8 null_key[16] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
711 					   0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
712 					   0x0, 0x0};
713 
714 			pframe = pxmitframe->buf_addr + hw_hdr_offset;
715 
716 			if (is_multicast_ether_addr(pattrib->ra)) {
717 				if (!memcmp(psecuritypriv->dot118021XGrptxmickey[psecuritypriv->dot118021XGrpKeyid].skey, null_key, 16))
718 					return _FAIL;
719 				/* start to calculate the mic code */
720 				rtw_secmicsetkey(&micdata, psecuritypriv->dot118021XGrptxmickey[psecuritypriv->dot118021XGrpKeyid].skey);
721 			} else {
722 				if (!memcmp(&stainfo->dot11tkiptxmickey.skey[0], null_key, 16)) {
723 					/* msleep(10); */
724 					return _FAIL;
725 				}
726 				/* start to calculate the mic code */
727 				rtw_secmicsetkey(&micdata, &stainfo->dot11tkiptxmickey.skey[0]);
728 			}
729 
730 			if (pframe[1] & 1) {   /* ToDS == 1 */
731 				rtw_secmicappend(&micdata, &pframe[16], 6);  /* DA */
732 				if (pframe[1] & 2)  /* From Ds == 1 */
733 					rtw_secmicappend(&micdata, &pframe[24], 6);
734 				else
735 					rtw_secmicappend(&micdata, &pframe[10], 6);
736 			} else {	/* ToDS == 0 */
737 				rtw_secmicappend(&micdata, &pframe[4], 6);   /* DA */
738 				if (pframe[1] & 2)  /* From Ds == 1 */
739 					rtw_secmicappend(&micdata, &pframe[16], 6);
740 				else
741 					rtw_secmicappend(&micdata, &pframe[10], 6);
742 			}
743 
744 			if (pattrib->qos_en)
745 				priority[0] = (u8)pxmitframe->attrib.priority;
746 
747 			rtw_secmicappend(&micdata, &priority[0], 4);
748 
749 			payload = pframe;
750 
751 			for (curfragnum = 0; curfragnum < pattrib->nr_frags; curfragnum++) {
752 				payload = PTR_ALIGN(payload, 4);
753 
754 				payload = payload + pattrib->hdrlen + pattrib->iv_len;
755 				if ((curfragnum + 1) == pattrib->nr_frags) {
756 					length = pattrib->last_txcmdsz - pattrib->hdrlen - pattrib->iv_len - ((pattrib->bswenc) ? pattrib->icv_len : 0);
757 					rtw_secmicappend(&micdata, payload, length);
758 					payload = payload + length;
759 				} else {
760 					length = pxmitpriv->frag_len - pattrib->hdrlen - pattrib->iv_len - ((pattrib->bswenc) ? pattrib->icv_len : 0);
761 					rtw_secmicappend(&micdata, payload, length);
762 					payload = payload + length + pattrib->icv_len;
763 				}
764 			}
765 			rtw_secgetmic(&micdata, &mic[0]);
766 			/* add mic code  and add the mic code length in last_txcmdsz */
767 
768 			memcpy(payload, &mic[0], 8);
769 			pattrib->last_txcmdsz += 8;
770 
771 			payload = payload - pattrib->last_txcmdsz + 8;
772 		}
773 	}
774 
775 	return _SUCCESS;
776 }
777 
xmitframe_swencrypt(struct adapter * padapter,struct xmit_frame * pxmitframe)778 static s32 xmitframe_swencrypt(struct adapter *padapter, struct xmit_frame *pxmitframe)
779 {
780 	struct	pkt_attrib	 *pattrib = &pxmitframe->attrib;
781 
782 	if (pattrib->bswenc) {
783 		switch (pattrib->encrypt) {
784 		case _WEP40_:
785 		case _WEP104_:
786 			rtw_wep_encrypt(padapter, pxmitframe);
787 			break;
788 		case _TKIP_:
789 			rtw_tkip_encrypt(padapter, pxmitframe);
790 			break;
791 		case _AES_:
792 			rtw_aes_encrypt(padapter, pxmitframe);
793 			break;
794 		default:
795 			break;
796 		}
797 	}
798 
799 	return _SUCCESS;
800 }
801 
rtw_make_wlanhdr(struct adapter * padapter,u8 * hdr,struct pkt_attrib * pattrib)802 s32 rtw_make_wlanhdr(struct adapter *padapter, u8 *hdr, struct pkt_attrib *pattrib)
803 {
804 	u16 *qc;
805 
806 	struct ieee80211_hdr *pwlanhdr = (struct ieee80211_hdr *)hdr;
807 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
808 	struct qos_priv *pqospriv = &pmlmepriv->qospriv;
809 	u8 qos_option = false;
810 
811 	int res = _SUCCESS;
812 	__le16 *fctrl = &pwlanhdr->frame_control;
813 
814 	struct sta_info *psta;
815 
816 	if (pattrib->psta)
817 		psta = pattrib->psta;
818 	else if (is_multicast_ether_addr(pattrib->ra))
819 		psta = rtw_get_bcmc_stainfo(padapter);
820 	else
821 		psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
822 
823 	memset(hdr, 0, WLANHDR_OFFSET);
824 
825 	SetFrameSubType(fctrl, pattrib->subtype);
826 
827 	if (pattrib->subtype & IEEE80211_FTYPE_DATA) {
828 		if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
829 			/* to_ds = 1, fr_ds = 0; */
830 			/* Data transfer to AP */
831 			SetToDs(fctrl);
832 			memcpy(pwlanhdr->addr1, get_bssid(pmlmepriv), ETH_ALEN);
833 			memcpy(pwlanhdr->addr2, pattrib->src, ETH_ALEN);
834 			memcpy(pwlanhdr->addr3, pattrib->dst, ETH_ALEN);
835 
836 			if (pqospriv->qos_option)
837 				qos_option = true;
838 		} else if (check_fwstate(pmlmepriv,  WIFI_AP_STATE)) {
839 			/* to_ds = 0, fr_ds = 1; */
840 			SetFrDs(fctrl);
841 			memcpy(pwlanhdr->addr1, pattrib->dst, ETH_ALEN);
842 			memcpy(pwlanhdr->addr2, get_bssid(pmlmepriv), ETH_ALEN);
843 			memcpy(pwlanhdr->addr3, pattrib->src, ETH_ALEN);
844 
845 			if (psta->qos_option)
846 				qos_option = true;
847 		} else if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) ||
848 			   check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
849 			memcpy(pwlanhdr->addr1, pattrib->dst, ETH_ALEN);
850 			memcpy(pwlanhdr->addr2, pattrib->src, ETH_ALEN);
851 			memcpy(pwlanhdr->addr3, get_bssid(pmlmepriv), ETH_ALEN);
852 
853 			if (psta->qos_option)
854 				qos_option = true;
855 		} else {
856 			res = _FAIL;
857 			goto exit;
858 		}
859 
860 		if (pattrib->mdata)
861 			SetMData(fctrl);
862 
863 		if (pattrib->encrypt)
864 			SetPrivacy(fctrl);
865 
866 		if (qos_option) {
867 			qc = (unsigned short *)(hdr + pattrib->hdrlen - 2);
868 
869 			if (pattrib->priority)
870 				SetPriority(qc, pattrib->priority);
871 
872 			SetEOSP(qc, pattrib->eosp);
873 
874 			SetAckpolicy(qc, pattrib->ack_policy);
875 		}
876 
877 		/* TODO: fill HT Control Field */
878 
879 		/* Update Seq Num will be handled by f/w */
880 		if (psta) {
881 			psta->sta_xmitpriv.txseq_tid[pattrib->priority]++;
882 			psta->sta_xmitpriv.txseq_tid[pattrib->priority] &= 0xFFF;
883 
884 			pattrib->seqnum = psta->sta_xmitpriv.txseq_tid[pattrib->priority];
885 
886 			SetSeqNum(hdr, pattrib->seqnum);
887 
888 			/* check if enable ampdu */
889 			if (pattrib->ht_en && psta->htpriv.ampdu_enable) {
890 				if (psta->htpriv.agg_enable_bitmap & BIT(pattrib->priority))
891 					pattrib->ampdu_en = true;
892 			}
893 
894 			/* re-check if enable ampdu by BA_starting_seqctrl */
895 			if (pattrib->ampdu_en) {
896 				u16 tx_seq;
897 
898 				tx_seq = psta->BA_starting_seqctrl[pattrib->priority & 0x0f];
899 
900 				/* check BA_starting_seqctrl */
901 				if (SN_LESS(pattrib->seqnum, tx_seq)) {
902 					pattrib->ampdu_en = false;/* AGG BK */
903 				} else if (SN_EQUAL(pattrib->seqnum, tx_seq)) {
904 					psta->BA_starting_seqctrl[pattrib->priority & 0x0f] = (tx_seq + 1) & 0xfff;
905 
906 					pattrib->ampdu_en = true;/* AGG EN */
907 				} else {
908 					psta->BA_starting_seqctrl[pattrib->priority & 0x0f] = (pattrib->seqnum + 1) & 0xfff;
909 					pattrib->ampdu_en = true;/* AGG EN */
910 				}
911 			}
912 		}
913 	}
914 exit:
915 
916 	return res;
917 }
918 
rtw_txframes_pending(struct adapter * padapter)919 s32 rtw_txframes_pending(struct adapter *padapter)
920 {
921 	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
922 
923 	return (!list_empty(&pxmitpriv->be_pending.queue) ||
924 			!list_empty(&pxmitpriv->bk_pending.queue) ||
925 			!list_empty(&pxmitpriv->vi_pending.queue) ||
926 			!list_empty(&pxmitpriv->vo_pending.queue));
927 }
928 
rtw_txframes_sta_ac_pending(struct adapter * padapter,struct pkt_attrib * pattrib)929 s32 rtw_txframes_sta_ac_pending(struct adapter *padapter, struct pkt_attrib *pattrib)
930 {
931 	struct sta_info *psta;
932 	struct tx_servq *ptxservq;
933 	int priority = pattrib->priority;
934 
935 	psta = pattrib->psta;
936 
937 	switch (priority) {
938 	case 1:
939 	case 2:
940 		ptxservq = &psta->sta_xmitpriv.bk_q;
941 		break;
942 	case 4:
943 	case 5:
944 		ptxservq = &psta->sta_xmitpriv.vi_q;
945 		break;
946 	case 6:
947 	case 7:
948 		ptxservq = &psta->sta_xmitpriv.vo_q;
949 		break;
950 	case 0:
951 	case 3:
952 	default:
953 		ptxservq = &psta->sta_xmitpriv.be_q;
954 		break;
955 	}
956 
957 	if (ptxservq)
958 		return ptxservq->qcnt;
959 	return 0;
960 }
961 
962 /*
963  * This sub-routine will perform all the following:
964  *
965  * 1. remove 802.3 header.
966  * 2. create wlan_header, based on the info in pxmitframe
967  * 3. append sta's iv/ext-iv
968  * 4. append LLC
969  * 5. move frag chunk from pframe to pxmitframe->mem
970  * 6. apply sw-encrypt, if necessary.
971  */
rtw_xmitframe_coalesce(struct adapter * padapter,struct sk_buff * pkt,struct xmit_frame * pxmitframe)972 s32 rtw_xmitframe_coalesce(struct adapter *padapter, struct sk_buff *pkt, struct xmit_frame *pxmitframe)
973 {
974 	struct pkt_file pktfile;
975 	s32 frg_inx, frg_len, mpdu_len, llc_sz, mem_sz;
976 	u8 *pframe, *mem_start;
977 	u8 hw_hdr_offset;
978 	struct sta_info		*psta;
979 	struct xmit_priv	*pxmitpriv = &padapter->xmitpriv;
980 	struct pkt_attrib	*pattrib = &pxmitframe->attrib;
981 	u8 *pbuf_start;
982 	bool bmcst = is_multicast_ether_addr(pattrib->ra);
983 	s32 res = _SUCCESS;
984 
985 	if (!pkt)
986 		return _FAIL;
987 
988 	psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
989 
990 	if (!psta)
991 		return _FAIL;
992 
993 	if (!pxmitframe->buf_addr)
994 		return _FAIL;
995 
996 	pbuf_start = pxmitframe->buf_addr;
997 
998 	hw_hdr_offset =  TXDESC_SIZE + (pxmitframe->pkt_offset * PACKET_OFFSET_SZ);
999 
1000 	mem_start = pbuf_start +	hw_hdr_offset;
1001 
1002 	if (rtw_make_wlanhdr(padapter, mem_start, pattrib) == _FAIL) {
1003 		res = _FAIL;
1004 		goto exit;
1005 	}
1006 
1007 	rtw_open_pktfile(pkt, &pktfile);
1008 	rtw_pktfile_read(&pktfile, NULL, pattrib->pkt_hdrlen);
1009 
1010 	frg_inx = 0;
1011 	frg_len = pxmitpriv->frag_len - 4;/* 2346-4 = 2342 */
1012 
1013 	while (1) {
1014 		llc_sz = 0;
1015 
1016 		mpdu_len = frg_len;
1017 
1018 		pframe = mem_start;
1019 
1020 		SetMFrag(mem_start);
1021 
1022 		pframe += pattrib->hdrlen;
1023 		mpdu_len -= pattrib->hdrlen;
1024 
1025 		/* adding icv, if necessary... */
1026 		if (pattrib->iv_len) {
1027 			if (psta) {
1028 				switch (pattrib->encrypt) {
1029 				case _WEP40_:
1030 				case _WEP104_:
1031 					WEP_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
1032 					break;
1033 				case _TKIP_:
1034 					if (bmcst)
1035 						TKIP_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
1036 					else
1037 						TKIP_IV(pattrib->iv, psta->dot11txpn, 0);
1038 					break;
1039 				case _AES_:
1040 					if (bmcst)
1041 						AES_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
1042 					else
1043 						AES_IV(pattrib->iv, psta->dot11txpn, 0);
1044 					break;
1045 				}
1046 			}
1047 
1048 			memcpy(pframe, pattrib->iv, pattrib->iv_len);
1049 
1050 			pframe += pattrib->iv_len;
1051 
1052 			mpdu_len -= pattrib->iv_len;
1053 		}
1054 
1055 		if (frg_inx == 0) {
1056 			llc_sz = rtw_put_snap(pframe, pattrib->ether_type);
1057 			pframe += llc_sz;
1058 			mpdu_len -= llc_sz;
1059 		}
1060 
1061 		if ((pattrib->icv_len > 0) && (pattrib->bswenc))
1062 			mpdu_len -= pattrib->icv_len;
1063 
1064 		if (bmcst) {
1065 			/*  don't do fragment to broadcast/multicast packets */
1066 			mem_sz = rtw_pktfile_read(&pktfile, pframe, pattrib->pktlen);
1067 		} else {
1068 			mem_sz = rtw_pktfile_read(&pktfile, pframe, mpdu_len);
1069 		}
1070 
1071 		pframe += mem_sz;
1072 
1073 		if ((pattrib->icv_len > 0) && (pattrib->bswenc)) {
1074 			memcpy(pframe, pattrib->icv, pattrib->icv_len);
1075 			pframe += pattrib->icv_len;
1076 		}
1077 
1078 		frg_inx++;
1079 
1080 		if (bmcst || pktfile.pkt_len == 0) {
1081 			pattrib->nr_frags = frg_inx;
1082 
1083 			pattrib->last_txcmdsz = pattrib->hdrlen + pattrib->iv_len + ((pattrib->nr_frags == 1) ? llc_sz : 0) +
1084 						((pattrib->bswenc) ? pattrib->icv_len : 0) + mem_sz;
1085 
1086 			ClearMFrag(mem_start);
1087 
1088 			break;
1089 		}
1090 
1091 		mem_start = PTR_ALIGN(pframe, 4) + hw_hdr_offset;
1092 		memcpy(mem_start, pbuf_start + hw_hdr_offset, pattrib->hdrlen);
1093 	}
1094 
1095 	if (xmitframe_addmic(padapter, pxmitframe) == _FAIL) {
1096 		res = _FAIL;
1097 		goto exit;
1098 	}
1099 
1100 	xmitframe_swencrypt(padapter, pxmitframe);
1101 
1102 	if (!bmcst)
1103 		update_attrib_vcs_info(padapter, pxmitframe);
1104 	else
1105 		pattrib->vcs_mode = NONE_VCS;
1106 
1107 exit:
1108 
1109 	return res;
1110 }
1111 
1112 /* Logical Link Control(LLC) SubNetwork Attachment Point(SNAP) header
1113  * IEEE LLC/SNAP header contains 8 octets
1114  * First 3 octets comprise the LLC portion
1115  * SNAP portion, 5 octets, is divided into two fields:
1116  *	Organizationally Unique Identifier(OUI), 3 octets,
1117  *	type, defined by that organization, 2 octets.
1118  */
rtw_put_snap(u8 * data,u16 h_proto)1119 s32 rtw_put_snap(u8 *data, u16 h_proto)
1120 {
1121 	struct ieee80211_snap_hdr *snap;
1122 	u8 *oui;
1123 
1124 	snap = (struct ieee80211_snap_hdr *)data;
1125 	snap->dsap = 0xaa;
1126 	snap->ssap = 0xaa;
1127 	snap->ctrl = 0x03;
1128 
1129 	if (h_proto == 0x8137 || h_proto == 0x80f3)
1130 		oui = P802_1H_OUI;
1131 	else
1132 		oui = RFC1042_OUI;
1133 
1134 	snap->oui[0] = oui[0];
1135 	snap->oui[1] = oui[1];
1136 	snap->oui[2] = oui[2];
1137 
1138 	*(__be16 *)(data + SNAP_SIZE) = htons(h_proto);
1139 
1140 	return SNAP_SIZE + sizeof(u16);
1141 }
1142 
rtw_update_protection(struct adapter * padapter,u8 * ie,uint ie_len)1143 void rtw_update_protection(struct adapter *padapter, u8 *ie, uint ie_len)
1144 {
1145 	uint	protection;
1146 	u8	*perp;
1147 	int	 erp_len;
1148 	struct	xmit_priv *pxmitpriv = &padapter->xmitpriv;
1149 	struct	registry_priv *pregistrypriv = &padapter->registrypriv;
1150 
1151 	switch (pxmitpriv->vcs_setting) {
1152 	case DISABLE_VCS:
1153 		pxmitpriv->vcs = NONE_VCS;
1154 		break;
1155 	case ENABLE_VCS:
1156 		break;
1157 	case AUTO_VCS:
1158 	default:
1159 		perp = rtw_get_ie(ie, _ERPINFO_IE_, &erp_len, ie_len);
1160 		if (!perp) {
1161 			pxmitpriv->vcs = NONE_VCS;
1162 		} else {
1163 			protection = (*(perp + 2)) & BIT(1);
1164 			if (protection) {
1165 				if (pregistrypriv->vcs_type == RTS_CTS)
1166 					pxmitpriv->vcs = RTS_CTS;
1167 				else
1168 					pxmitpriv->vcs = CTS_TO_SELF;
1169 			} else {
1170 				pxmitpriv->vcs = NONE_VCS;
1171 			}
1172 		}
1173 		break;
1174 	}
1175 }
1176 
rtw_count_tx_stats(struct adapter * padapter,struct xmit_frame * pxmitframe,int sz)1177 void rtw_count_tx_stats(struct adapter *padapter, struct xmit_frame *pxmitframe, int sz)
1178 {
1179 	struct sta_info *psta = NULL;
1180 	struct stainfo_stats *pstats = NULL;
1181 	struct xmit_priv	*pxmitpriv = &padapter->xmitpriv;
1182 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
1183 
1184 	if ((pxmitframe->frame_tag & 0x0f) == DATA_FRAMETAG) {
1185 		pxmitpriv->tx_bytes += sz;
1186 		pmlmepriv->LinkDetectInfo.NumTxOkInPeriod += pxmitframe->agg_num;
1187 
1188 		psta = pxmitframe->attrib.psta;
1189 		if (psta) {
1190 			pstats = &psta->sta_stats;
1191 			pstats->tx_pkts += pxmitframe->agg_num;
1192 			pstats->tx_bytes += sz;
1193 		}
1194 	}
1195 }
1196 
rtw_alloc_xmitbuf_ext(struct xmit_priv * pxmitpriv)1197 struct xmit_buf *rtw_alloc_xmitbuf_ext(struct xmit_priv *pxmitpriv)
1198 {
1199 	struct xmit_buf *pxmitbuf =  NULL;
1200 	struct list_head *plist, *phead;
1201 	struct __queue *pfree_queue = &pxmitpriv->free_xmit_extbuf_queue;
1202 	unsigned long flags;
1203 
1204 	spin_lock_irqsave(&pfree_queue->lock, flags);
1205 
1206 	if (list_empty(&pfree_queue->queue)) {
1207 		pxmitbuf = NULL;
1208 	} else {
1209 		phead = get_list_head(pfree_queue);
1210 
1211 		plist = phead->next;
1212 
1213 		pxmitbuf = container_of(plist, struct xmit_buf, list);
1214 
1215 		list_del_init(&pxmitbuf->list);
1216 	}
1217 
1218 	if (pxmitbuf) {
1219 		pxmitpriv->free_xmit_extbuf_cnt--;
1220 
1221 		pxmitbuf->priv_data = NULL;
1222 		/* pxmitbuf->ext_tag = true; */
1223 
1224 		if (pxmitbuf->sctx)
1225 			rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_ALLOC);
1226 	}
1227 
1228 	spin_unlock_irqrestore(&pfree_queue->lock, flags);
1229 
1230 	return pxmitbuf;
1231 }
1232 
rtw_free_xmitbuf_ext(struct xmit_priv * pxmitpriv,struct xmit_buf * pxmitbuf)1233 s32 rtw_free_xmitbuf_ext(struct xmit_priv *pxmitpriv, struct xmit_buf *pxmitbuf)
1234 {
1235 	struct __queue *pfree_queue = &pxmitpriv->free_xmit_extbuf_queue;
1236 	unsigned long flags;
1237 
1238 	if (!pxmitbuf)
1239 		return _FAIL;
1240 
1241 	spin_lock_irqsave(&pfree_queue->lock, flags);
1242 
1243 	list_del_init(&pxmitbuf->list);
1244 
1245 	list_add_tail(&pxmitbuf->list, get_list_head(pfree_queue));
1246 	pxmitpriv->free_xmit_extbuf_cnt++;
1247 
1248 	spin_unlock_irqrestore(&pfree_queue->lock, flags);
1249 
1250 	return _SUCCESS;
1251 }
1252 
rtw_alloc_xmitbuf(struct xmit_priv * pxmitpriv)1253 struct xmit_buf *rtw_alloc_xmitbuf(struct xmit_priv *pxmitpriv)
1254 {
1255 	struct xmit_buf *pxmitbuf =  NULL;
1256 	struct list_head *plist, *phead;
1257 	struct __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
1258 	unsigned long flags;
1259 
1260 	spin_lock_irqsave(&pfree_xmitbuf_queue->lock, flags);
1261 
1262 	if (list_empty(&pfree_xmitbuf_queue->queue)) {
1263 		pxmitbuf = NULL;
1264 	} else {
1265 		phead = get_list_head(pfree_xmitbuf_queue);
1266 
1267 		plist = phead->next;
1268 
1269 		pxmitbuf = container_of(plist, struct xmit_buf, list);
1270 
1271 		list_del_init(&pxmitbuf->list);
1272 	}
1273 
1274 	if (pxmitbuf) {
1275 		pxmitpriv->free_xmitbuf_cnt--;
1276 		pxmitbuf->priv_data = NULL;
1277 		if (pxmitbuf->sctx)
1278 			rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_ALLOC);
1279 	}
1280 	spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, flags);
1281 
1282 	return pxmitbuf;
1283 }
1284 
rtw_free_xmitbuf(struct xmit_priv * pxmitpriv,struct xmit_buf * pxmitbuf)1285 s32 rtw_free_xmitbuf(struct xmit_priv *pxmitpriv, struct xmit_buf *pxmitbuf)
1286 {
1287 	struct __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
1288 	unsigned long flags;
1289 
1290 	if (!pxmitbuf)
1291 		return _FAIL;
1292 
1293 	if (pxmitbuf->sctx)
1294 		rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_FREE);
1295 
1296 	if (pxmitbuf->ext_tag) {
1297 		rtw_free_xmitbuf_ext(pxmitpriv, pxmitbuf);
1298 	} else {
1299 		spin_lock_irqsave(&pfree_xmitbuf_queue->lock, flags);
1300 
1301 		list_del_init(&pxmitbuf->list);
1302 
1303 		list_add_tail(&pxmitbuf->list, get_list_head(pfree_xmitbuf_queue));
1304 
1305 		pxmitpriv->free_xmitbuf_cnt++;
1306 		spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, flags);
1307 	}
1308 
1309 	return _SUCCESS;
1310 }
1311 
1312 /*
1313  * Calling context:
1314  * 1. OS_TXENTRY
1315  * 2. RXENTRY (rx_thread or RX_ISR/RX_CallBack)
1316  *
1317  * If we turn on USE_RXTHREAD, then, no need for critical section.
1318  * Otherwise, we must use _enter/_exit critical to protect free_xmit_queue...
1319  *
1320  * Must be very very cautious...
1321  */
rtw_alloc_xmitframe(struct xmit_priv * pxmitpriv)1322 struct xmit_frame *rtw_alloc_xmitframe(struct xmit_priv *pxmitpriv)/* _queue *pfree_xmit_queue) */
1323 {
1324 	/*
1325 	 * Please remember to use all the osdep_service api,
1326 	 * and lock/unlock or _enter/_exit critical to protect
1327 	 * pfree_xmit_queue
1328 	 */
1329 
1330 	struct xmit_frame *pxframe = NULL;
1331 	struct list_head *plist, *phead;
1332 	struct __queue *pfree_xmit_queue = &pxmitpriv->free_xmit_queue;
1333 
1334 	spin_lock_bh(&pfree_xmit_queue->lock);
1335 
1336 	if (list_empty(&pfree_xmit_queue->queue)) {
1337 		pxframe =  NULL;
1338 	} else {
1339 		phead = get_list_head(pfree_xmit_queue);
1340 
1341 		plist = phead->next;
1342 
1343 		pxframe = container_of(plist, struct xmit_frame, list);
1344 
1345 		list_del_init(&pxframe->list);
1346 	}
1347 
1348 	if (pxframe) { /* default value setting */
1349 		pxmitpriv->free_xmitframe_cnt--;
1350 
1351 		pxframe->buf_addr = NULL;
1352 		pxframe->pxmitbuf = NULL;
1353 
1354 		memset(&pxframe->attrib, 0, sizeof(struct pkt_attrib));
1355 		/* pxframe->attrib.psta = NULL; */
1356 
1357 		pxframe->frame_tag = DATA_FRAMETAG;
1358 
1359 		pxframe->pkt = NULL;
1360 		pxframe->pkt_offset = 1;/* default use pkt_offset to fill tx desc */
1361 
1362 		pxframe->agg_num = 1;
1363 		pxframe->ack_report = 0;
1364 	}
1365 
1366 	spin_unlock_bh(&pfree_xmit_queue->lock);
1367 
1368 	return pxframe;
1369 }
1370 
rtw_free_xmitframe(struct xmit_priv * pxmitpriv,struct xmit_frame * pxmitframe)1371 s32 rtw_free_xmitframe(struct xmit_priv *pxmitpriv, struct xmit_frame *pxmitframe)
1372 {
1373 	struct __queue *pfree_xmit_queue = &pxmitpriv->free_xmit_queue;
1374 	struct adapter *padapter = pxmitpriv->adapter;
1375 	struct sk_buff *pndis_pkt = NULL;
1376 
1377 	if (!pxmitframe)
1378 		goto exit;
1379 
1380 	spin_lock_bh(&pfree_xmit_queue->lock);
1381 
1382 	list_del_init(&pxmitframe->list);
1383 
1384 	if (pxmitframe->pkt) {
1385 		pndis_pkt = pxmitframe->pkt;
1386 		pxmitframe->pkt = NULL;
1387 	}
1388 
1389 	list_add_tail(&pxmitframe->list, get_list_head(pfree_xmit_queue));
1390 
1391 	pxmitpriv->free_xmitframe_cnt++;
1392 
1393 	spin_unlock_bh(&pfree_xmit_queue->lock);
1394 
1395 	if (pndis_pkt)
1396 		rtw_pkt_complete(padapter, pndis_pkt);
1397 
1398 exit:
1399 
1400 	return _SUCCESS;
1401 }
1402 
rtw_free_xmitframe_queue(struct xmit_priv * pxmitpriv,struct __queue * pframequeue)1403 void rtw_free_xmitframe_queue(struct xmit_priv *pxmitpriv, struct __queue *pframequeue)
1404 {
1405 	struct list_head *plist, *phead;
1406 	struct	xmit_frame	*pxmitframe;
1407 
1408 	spin_lock_bh(&pframequeue->lock);
1409 
1410 	phead = get_list_head(pframequeue);
1411 	plist = phead->next;
1412 
1413 	while (phead != plist) {
1414 		pxmitframe = container_of(plist, struct xmit_frame, list);
1415 
1416 		plist = plist->next;
1417 
1418 		rtw_free_xmitframe(pxmitpriv, pxmitframe);
1419 	}
1420 	spin_unlock_bh(&pframequeue->lock);
1421 }
1422 
rtw_xmitframe_enqueue(struct adapter * padapter,struct xmit_frame * pxmitframe)1423 s32 rtw_xmitframe_enqueue(struct adapter *padapter, struct xmit_frame *pxmitframe)
1424 {
1425 	if (rtw_xmit_classifier(padapter, pxmitframe) == _FAIL) {
1426 /* 		pxmitframe->pkt = NULL; */
1427 		return _FAIL;
1428 	}
1429 
1430 	return _SUCCESS;
1431 }
1432 
dequeue_one_xmitframe(struct xmit_priv * pxmitpriv,struct hw_xmit * phwxmit,struct tx_servq * ptxservq,struct __queue * pframe_queue)1433 static struct xmit_frame *dequeue_one_xmitframe(struct xmit_priv *pxmitpriv, struct hw_xmit *phwxmit, struct tx_servq *ptxservq, struct __queue *pframe_queue)
1434 {
1435 	struct list_head *xmitframe_plist, *xmitframe_phead;
1436 	struct	xmit_frame	*pxmitframe = NULL;
1437 
1438 	xmitframe_phead = get_list_head(pframe_queue);
1439 	xmitframe_plist = xmitframe_phead->next;
1440 
1441 	if (xmitframe_phead != xmitframe_plist) {
1442 		pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
1443 
1444 		xmitframe_plist = xmitframe_plist->next;
1445 
1446 		list_del_init(&pxmitframe->list);
1447 
1448 		ptxservq->qcnt--;
1449 	}
1450 	return pxmitframe;
1451 }
1452 
rtw_dequeue_xframe(struct xmit_priv * pxmitpriv,struct hw_xmit * phwxmit_i,int entry)1453 struct xmit_frame *rtw_dequeue_xframe(struct xmit_priv *pxmitpriv, struct hw_xmit *phwxmit_i, int entry)
1454 {
1455 	struct list_head *sta_plist, *sta_phead;
1456 	struct hw_xmit *phwxmit;
1457 	struct tx_servq *ptxservq = NULL;
1458 	struct __queue *pframe_queue = NULL;
1459 	struct xmit_frame *pxmitframe = NULL;
1460 	struct adapter *padapter = pxmitpriv->adapter;
1461 	struct registry_priv	*pregpriv = &padapter->registrypriv;
1462 	int i, inx[4];
1463 
1464 	inx[0] = 0; inx[1] = 1; inx[2] = 2; inx[3] = 3;
1465 
1466 	if (pregpriv->wifi_spec == 1) {
1467 		int j;
1468 
1469 		for (j = 0; j < 4; j++)
1470 			inx[j] = pxmitpriv->wmm_para_seq[j];
1471 	}
1472 
1473 	spin_lock_bh(&pxmitpriv->lock);
1474 
1475 	for (i = 0; i < entry; i++) {
1476 		phwxmit = phwxmit_i + inx[i];
1477 
1478 		sta_phead = get_list_head(phwxmit->sta_queue);
1479 		sta_plist = sta_phead->next;
1480 
1481 		while (sta_phead != sta_plist) {
1482 			ptxservq = container_of(sta_plist, struct tx_servq, tx_pending);
1483 
1484 			pframe_queue = &ptxservq->sta_pending;
1485 
1486 			pxmitframe = dequeue_one_xmitframe(pxmitpriv, phwxmit, ptxservq, pframe_queue);
1487 
1488 			if (pxmitframe) {
1489 				phwxmit->accnt--;
1490 
1491 				/* Remove sta node when there are no pending packets. */
1492 				if (list_empty(&pframe_queue->queue)) /* must be done after get_next and before break */
1493 					list_del_init(&ptxservq->tx_pending);
1494 				goto exit;
1495 			}
1496 
1497 			sta_plist = sta_plist->next;
1498 		}
1499 	}
1500 exit:
1501 	spin_unlock_bh(&pxmitpriv->lock);
1502 
1503 	return pxmitframe;
1504 }
1505 
rtw_get_sta_pending(struct adapter * padapter,struct sta_info * psta,int up,u8 * ac)1506 struct tx_servq *rtw_get_sta_pending(struct adapter *padapter, struct sta_info *psta, int up, u8 *ac)
1507 {
1508 	struct tx_servq *ptxservq;
1509 
1510 	switch (up) {
1511 	case 1:
1512 	case 2:
1513 		ptxservq = &psta->sta_xmitpriv.bk_q;
1514 		*(ac) = 3;
1515 		break;
1516 	case 4:
1517 	case 5:
1518 		ptxservq = &psta->sta_xmitpriv.vi_q;
1519 		*(ac) = 1;
1520 		break;
1521 	case 6:
1522 	case 7:
1523 		ptxservq = &psta->sta_xmitpriv.vo_q;
1524 		*(ac) = 0;
1525 		break;
1526 	case 0:
1527 	case 3:
1528 	default:
1529 		ptxservq = &psta->sta_xmitpriv.be_q;
1530 		*(ac) = 2;
1531 	break;
1532 	}
1533 
1534 	return ptxservq;
1535 }
1536 
1537 /*
1538  * Will enqueue pxmitframe to the proper queue,
1539  * and indicate it to xx_pending list.....
1540  */
rtw_xmit_classifier(struct adapter * padapter,struct xmit_frame * pxmitframe)1541 s32 rtw_xmit_classifier(struct adapter *padapter, struct xmit_frame *pxmitframe)
1542 {
1543 	u8	ac_index;
1544 	struct sta_info	*psta;
1545 	struct tx_servq	*ptxservq;
1546 	struct pkt_attrib	*pattrib = &pxmitframe->attrib;
1547 	struct sta_priv	*pstapriv = &padapter->stapriv;
1548 	struct hw_xmit	*phwxmits =  padapter->xmitpriv.hwxmits;
1549 	int res = _SUCCESS;
1550 
1551 	if (pattrib->psta)
1552 		psta = pattrib->psta;
1553 	else
1554 		psta = rtw_get_stainfo(pstapriv, pattrib->ra);
1555 
1556 	if (!psta) {
1557 		res = _FAIL;
1558 		goto exit;
1559 	}
1560 
1561 	ptxservq = rtw_get_sta_pending(padapter, psta, pattrib->priority, (u8 *)(&ac_index));
1562 
1563 	if (list_empty(&ptxservq->tx_pending))
1564 		list_add_tail(&ptxservq->tx_pending, get_list_head(phwxmits[ac_index].sta_queue));
1565 
1566 	list_add_tail(&pxmitframe->list, get_list_head(&ptxservq->sta_pending));
1567 	ptxservq->qcnt++;
1568 	phwxmits[ac_index].accnt++;
1569 exit:
1570 
1571 	return res;
1572 }
1573 
rtw_alloc_hwxmits(struct adapter * padapter)1574 int rtw_alloc_hwxmits(struct adapter *padapter)
1575 {
1576 	struct hw_xmit *hwxmits;
1577 	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1578 
1579 	pxmitpriv->hwxmit_entry = HWXMIT_ENTRY;
1580 
1581 	pxmitpriv->hwxmits = kzalloc(sizeof(struct hw_xmit) * pxmitpriv->hwxmit_entry, GFP_KERNEL);
1582 	if (!pxmitpriv->hwxmits)
1583 		return -ENOMEM;
1584 
1585 	hwxmits = pxmitpriv->hwxmits;
1586 
1587 	hwxmits[0].sta_queue = &pxmitpriv->vo_pending;
1588 	hwxmits[1].sta_queue = &pxmitpriv->vi_pending;
1589 	hwxmits[2].sta_queue = &pxmitpriv->be_pending;
1590 	hwxmits[3].sta_queue = &pxmitpriv->bk_pending;
1591 
1592 	return 0;
1593 }
1594 
rtw_free_hwxmits(struct adapter * padapter)1595 void rtw_free_hwxmits(struct adapter *padapter)
1596 {
1597 	struct hw_xmit *hwxmits;
1598 	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1599 
1600 	hwxmits = pxmitpriv->hwxmits;
1601 	kfree(hwxmits);
1602 }
1603 
rtw_init_hwxmits(struct hw_xmit * phwxmit,int entry)1604 void rtw_init_hwxmits(struct hw_xmit *phwxmit, int entry)
1605 {
1606 	int i;
1607 
1608 	for (i = 0; i < entry; i++, phwxmit++)
1609 		phwxmit->accnt = 0;
1610 }
1611 
rtw_br_client_tx(struct adapter * padapter,struct sk_buff ** pskb)1612 static int rtw_br_client_tx(struct adapter *padapter, struct sk_buff **pskb)
1613 {
1614 	struct sk_buff *skb = *pskb;
1615 	int res, is_vlan_tag = 0, i, do_nat25 = 1;
1616 	unsigned short vlan_hdr = 0;
1617 	void *br_port = NULL;
1618 
1619 	rcu_read_lock();
1620 	br_port = rcu_dereference(padapter->pnetdev->rx_handler_data);
1621 	rcu_read_unlock();
1622 	spin_lock_bh(&padapter->br_ext_lock);
1623 	if (!(skb->data[0] & 1) && br_port &&
1624 	    memcmp(skb->data + ETH_ALEN, padapter->br_mac, ETH_ALEN) &&
1625 	    *((__be16 *)(skb->data + ETH_ALEN * 2)) != __constant_htons(ETH_P_8021Q) &&
1626 	    *((__be16 *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_IP) &&
1627 	    !memcmp(padapter->scdb_mac, skb->data + ETH_ALEN, ETH_ALEN) && padapter->scdb_entry) {
1628 		memcpy(skb->data + ETH_ALEN, GET_MY_HWADDR(padapter), ETH_ALEN);
1629 		padapter->scdb_entry->ageing_timer = jiffies;
1630 		spin_unlock_bh(&padapter->br_ext_lock);
1631 	} else {
1632 		if (*((__be16 *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_8021Q)) {
1633 			is_vlan_tag = 1;
1634 			vlan_hdr = *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2));
1635 			for (i = 0; i < 6; i++)
1636 				*((unsigned short *)(skb->data + ETH_ALEN * 2 + 2 - i * 2)) = *((unsigned short *)(skb->data + ETH_ALEN * 2 - 2 - i * 2));
1637 			skb_pull(skb, 4);
1638 		}
1639 		if (!memcmp(skb->data + ETH_ALEN, padapter->br_mac, ETH_ALEN) &&
1640 		    (*((__be16 *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_IP)))
1641 			memcpy(padapter->br_ip, skb->data + WLAN_ETHHDR_LEN + 12, 4);
1642 
1643 		if (*((__be16 *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_IP)) {
1644 			if (memcmp(padapter->scdb_mac, skb->data + ETH_ALEN, ETH_ALEN)) {
1645 				padapter->scdb_entry = (struct nat25_network_db_entry *)scdb_findEntry(padapter,
1646 							skb->data + WLAN_ETHHDR_LEN + 12);
1647 				if (padapter->scdb_entry) {
1648 					memcpy(padapter->scdb_mac, skb->data + ETH_ALEN, ETH_ALEN);
1649 					memcpy(padapter->scdb_ip, skb->data + WLAN_ETHHDR_LEN + 12, 4);
1650 					padapter->scdb_entry->ageing_timer = jiffies;
1651 					do_nat25 = 0;
1652 				}
1653 			} else {
1654 				if (padapter->scdb_entry) {
1655 					padapter->scdb_entry->ageing_timer = jiffies;
1656 					do_nat25 = 0;
1657 				} else {
1658 					memset(padapter->scdb_mac, 0, ETH_ALEN);
1659 					memset(padapter->scdb_ip, 0, 4);
1660 				}
1661 			}
1662 		}
1663 		spin_unlock_bh(&padapter->br_ext_lock);
1664 		if (do_nat25) {
1665 			if (nat25_db_handle(padapter, skb, NAT25_CHECK) == 0) {
1666 				struct sk_buff *newskb;
1667 
1668 				if (is_vlan_tag) {
1669 					skb_push(skb, 4);
1670 					for (i = 0; i < 6; i++)
1671 						*((unsigned short *)(skb->data + i * 2)) = *((unsigned short *)(skb->data + 4 + i * 2));
1672 					*((__be16 *)(skb->data + ETH_ALEN * 2)) = __constant_htons(ETH_P_8021Q);
1673 					*((unsigned short *)(skb->data + ETH_ALEN * 2 + 2)) = vlan_hdr;
1674 				}
1675 
1676 				newskb = skb_copy(skb, GFP_ATOMIC);
1677 				if (!newskb)
1678 					return -1;
1679 				dev_kfree_skb_any(skb);
1680 
1681 				*pskb = skb = newskb;
1682 				if (is_vlan_tag) {
1683 					vlan_hdr = *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2));
1684 					for (i = 0; i < 6; i++)
1685 						*((unsigned short *)(skb->data + ETH_ALEN * 2 + 2 - i * 2)) = *((unsigned short *)(skb->data + ETH_ALEN * 2 - 2 - i * 2));
1686 					skb_pull(skb, 4);
1687 				}
1688 			}
1689 
1690 			res = skb_linearize(skb);
1691 			if (res < 0)
1692 				return -1;
1693 
1694 			res = nat25_db_handle(padapter, skb, NAT25_INSERT);
1695 			if (res < 0) {
1696 				if (res == -2)
1697 					return -1;
1698 
1699 				return 0;
1700 			}
1701 		}
1702 
1703 		memcpy(skb->data + ETH_ALEN, GET_MY_HWADDR(padapter), ETH_ALEN);
1704 
1705 		dhcp_flag_bcast(padapter, skb);
1706 
1707 		if (is_vlan_tag) {
1708 			skb_push(skb, 4);
1709 			for (i = 0; i < 6; i++)
1710 				*((unsigned short *)(skb->data + i * 2)) = *((unsigned short *)(skb->data + 4 + i * 2));
1711 			*((__be16 *)(skb->data + ETH_ALEN * 2)) = __constant_htons(ETH_P_8021Q);
1712 			*((unsigned short *)(skb->data + ETH_ALEN * 2 + 2)) = vlan_hdr;
1713 		}
1714 	}
1715 
1716 	/*  check if SA is equal to our MAC */
1717 	if (memcmp(skb->data + ETH_ALEN, GET_MY_HWADDR(padapter), ETH_ALEN))
1718 		return -1;
1719 
1720 	return 0;
1721 }
1722 
rtw_get_ff_hwaddr(struct xmit_frame * pxmitframe)1723 u32 rtw_get_ff_hwaddr(struct xmit_frame *pxmitframe)
1724 {
1725 	u32 addr;
1726 	struct pkt_attrib *pattrib = &pxmitframe->attrib;
1727 
1728 	switch (pattrib->qsel) {
1729 	case 0:
1730 	case 3:
1731 		addr = BE_QUEUE_INX;
1732 		break;
1733 	case 1:
1734 	case 2:
1735 		addr = BK_QUEUE_INX;
1736 		break;
1737 	case 4:
1738 	case 5:
1739 		addr = VI_QUEUE_INX;
1740 		break;
1741 	case 6:
1742 	case 7:
1743 		addr = VO_QUEUE_INX;
1744 		break;
1745 	case 0x10:
1746 		addr = BCN_QUEUE_INX;
1747 		break;
1748 	case 0x11:/* BC/MC in PS (HIQ) */
1749 		addr = HIGH_QUEUE_INX;
1750 		break;
1751 	case 0x12:
1752 	default:
1753 		addr = MGT_QUEUE_INX;
1754 		break;
1755 	}
1756 
1757 	return addr;
1758 }
1759 
do_queue_select(struct adapter * padapter,struct pkt_attrib * pattrib)1760 static void do_queue_select(struct adapter	*padapter, struct pkt_attrib *pattrib)
1761 {
1762 	u8 qsel;
1763 
1764 	qsel = pattrib->priority;
1765 
1766 	pattrib->qsel = qsel;
1767 }
1768 
1769 /*
1770  * The main transmit(tx) entry
1771  *
1772  * Return
1773  *	1	enqueue
1774  *	0	success, hardware will handle this xmit frame(packet)
1775  *	<0	fail
1776  */
rtw_xmit(struct adapter * padapter,struct sk_buff ** ppkt)1777 s32 rtw_xmit(struct adapter *padapter, struct sk_buff **ppkt)
1778 {
1779 	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1780 	struct xmit_frame *pxmitframe = NULL;
1781 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
1782 	void *br_port = NULL;
1783 	s32 res;
1784 
1785 	pxmitframe = rtw_alloc_xmitframe(pxmitpriv);
1786 	if (!pxmitframe)
1787 		return -1;
1788 
1789 	rcu_read_lock();
1790 	br_port = rcu_dereference(padapter->pnetdev->rx_handler_data);
1791 	rcu_read_unlock();
1792 
1793 	if (br_port && check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_ADHOC_STATE)) {
1794 		res = rtw_br_client_tx(padapter, ppkt);
1795 		if (res == -1) {
1796 			rtw_free_xmitframe(pxmitpriv, pxmitframe);
1797 			return -1;
1798 		}
1799 	}
1800 
1801 	res = update_attrib(padapter, *ppkt, &pxmitframe->attrib);
1802 
1803 	if (res == _FAIL) {
1804 		rtw_free_xmitframe(pxmitpriv, pxmitframe);
1805 		return -1;
1806 	}
1807 	pxmitframe->pkt = *ppkt;
1808 
1809 	rtw_led_control(padapter, LED_CTL_TX);
1810 
1811 	do_queue_select(padapter, &pxmitframe->attrib);
1812 
1813 	spin_lock_bh(&pxmitpriv->lock);
1814 	if (xmitframe_enqueue_for_sleeping_sta(padapter, pxmitframe)) {
1815 		spin_unlock_bh(&pxmitpriv->lock);
1816 		return 1;
1817 	}
1818 	spin_unlock_bh(&pxmitpriv->lock);
1819 
1820 	if (!rtl8188eu_hal_xmit(padapter, pxmitframe))
1821 		return 1;
1822 
1823 	return 0;
1824 }
1825 
xmitframe_enqueue_for_sleeping_sta(struct adapter * padapter,struct xmit_frame * pxmitframe)1826 int xmitframe_enqueue_for_sleeping_sta(struct adapter *padapter, struct xmit_frame *pxmitframe)
1827 {
1828 	int ret = false;
1829 	struct sta_info *psta = NULL;
1830 	struct sta_priv *pstapriv = &padapter->stapriv;
1831 	struct pkt_attrib *pattrib = &pxmitframe->attrib;
1832 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1833 	bool bmcst = is_multicast_ether_addr(pattrib->ra);
1834 
1835 	if (!check_fwstate(pmlmepriv, WIFI_AP_STATE))
1836 		return ret;
1837 
1838 	if (pattrib->psta)
1839 		psta = pattrib->psta;
1840 	else
1841 		psta = rtw_get_stainfo(pstapriv, pattrib->ra);
1842 
1843 	if (!psta)
1844 		return ret;
1845 
1846 	if (pattrib->triggered == 1) {
1847 		if (bmcst)
1848 			pattrib->qsel = 0x11;/* HIQ */
1849 		return ret;
1850 	}
1851 
1852 	if (bmcst) {
1853 		spin_lock_bh(&psta->sleep_q.lock);
1854 
1855 		if (pstapriv->sta_dz_bitmap) {/* if any one sta is in ps mode */
1856 			list_del_init(&pxmitframe->list);
1857 
1858 			list_add_tail(&pxmitframe->list, get_list_head(&psta->sleep_q));
1859 
1860 			psta->sleepq_len++;
1861 
1862 			pstapriv->tim_bitmap |= BIT(0);/*  */
1863 			pstapriv->sta_dz_bitmap |= BIT(0);
1864 			/* tx bc/mc packets after update bcn */
1865 			update_beacon(padapter, _TIM_IE_, NULL, false);
1866 
1867 			ret = true;
1868 		}
1869 
1870 		spin_unlock_bh(&psta->sleep_q.lock);
1871 
1872 		return ret;
1873 	}
1874 
1875 	spin_lock_bh(&psta->sleep_q.lock);
1876 
1877 	if (psta->state & WIFI_SLEEP_STATE) {
1878 		u8 wmmps_ac = 0;
1879 
1880 		if (pstapriv->sta_dz_bitmap & BIT(psta->aid)) {
1881 			list_del_init(&pxmitframe->list);
1882 
1883 			list_add_tail(&pxmitframe->list, get_list_head(&psta->sleep_q));
1884 
1885 			psta->sleepq_len++;
1886 
1887 			switch (pattrib->priority) {
1888 			case 1:
1889 			case 2:
1890 				wmmps_ac = psta->uapsd_bk & BIT(0);
1891 				break;
1892 			case 4:
1893 			case 5:
1894 				wmmps_ac = psta->uapsd_vi & BIT(0);
1895 				break;
1896 			case 6:
1897 			case 7:
1898 				wmmps_ac = psta->uapsd_vo & BIT(0);
1899 				break;
1900 			case 0:
1901 			case 3:
1902 			default:
1903 				wmmps_ac = psta->uapsd_be & BIT(0);
1904 				break;
1905 			}
1906 
1907 			if (wmmps_ac)
1908 				psta->sleepq_ac_len++;
1909 
1910 			if (((psta->has_legacy_ac) && (!wmmps_ac)) ||
1911 			    ((!psta->has_legacy_ac) && (wmmps_ac))) {
1912 				pstapriv->tim_bitmap |= BIT(psta->aid);
1913 
1914 				if (psta->sleepq_len == 1) {
1915 					/* update BCN for TIM IE */
1916 					update_beacon(padapter, _TIM_IE_, NULL, false);
1917 				}
1918 			}
1919 			ret = true;
1920 		}
1921 	}
1922 
1923 	spin_unlock_bh(&psta->sleep_q.lock);
1924 
1925 	return ret;
1926 }
1927 
dequeue_xmitframes_to_sleeping_queue(struct adapter * padapter,struct sta_info * psta,struct __queue * pframequeue)1928 static void dequeue_xmitframes_to_sleeping_queue(struct adapter *padapter, struct sta_info *psta, struct __queue *pframequeue)
1929 {
1930 	struct list_head *plist, *phead;
1931 	u8	ac_index;
1932 	struct tx_servq	*ptxservq;
1933 	struct pkt_attrib	*pattrib;
1934 	struct xmit_frame	*pxmitframe;
1935 	struct hw_xmit *phwxmits =  padapter->xmitpriv.hwxmits;
1936 
1937 	phead = get_list_head(pframequeue);
1938 	plist = phead->next;
1939 
1940 	while (phead != plist) {
1941 		pxmitframe = container_of(plist, struct xmit_frame, list);
1942 
1943 		plist = plist->next;
1944 
1945 		xmitframe_enqueue_for_sleeping_sta(padapter, pxmitframe);
1946 
1947 		pattrib = &pxmitframe->attrib;
1948 
1949 		ptxservq = rtw_get_sta_pending(padapter, psta, pattrib->priority, (u8 *)(&ac_index));
1950 
1951 		ptxservq->qcnt--;
1952 		phwxmits[ac_index].accnt--;
1953 	}
1954 }
1955 
stop_sta_xmit(struct adapter * padapter,struct sta_info * psta)1956 void stop_sta_xmit(struct adapter *padapter, struct sta_info *psta)
1957 {
1958 	struct sta_info *psta_bmc;
1959 	struct sta_xmit_priv *pstaxmitpriv;
1960 	struct sta_priv *pstapriv = &padapter->stapriv;
1961 	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1962 
1963 	pstaxmitpriv = &psta->sta_xmitpriv;
1964 
1965 	/* for BC/MC Frames */
1966 	psta_bmc = rtw_get_bcmc_stainfo(padapter);
1967 
1968 	spin_lock_bh(&pxmitpriv->lock);
1969 
1970 	psta->state |= WIFI_SLEEP_STATE;
1971 
1972 	pstapriv->sta_dz_bitmap |= BIT(psta->aid);
1973 
1974 	dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->vo_q.sta_pending);
1975 	list_del_init(&pstaxmitpriv->vo_q.tx_pending);
1976 
1977 	dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->vi_q.sta_pending);
1978 	list_del_init(&pstaxmitpriv->vi_q.tx_pending);
1979 
1980 	dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->be_q.sta_pending);
1981 	list_del_init(&pstaxmitpriv->be_q.tx_pending);
1982 
1983 	dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->bk_q.sta_pending);
1984 	list_del_init(&pstaxmitpriv->bk_q.tx_pending);
1985 
1986 	/* for BC/MC Frames */
1987 	pstaxmitpriv = &psta_bmc->sta_xmitpriv;
1988 	dequeue_xmitframes_to_sleeping_queue(padapter, psta_bmc, &pstaxmitpriv->be_q.sta_pending);
1989 	list_del_init(&pstaxmitpriv->be_q.tx_pending);
1990 
1991 	spin_unlock_bh(&pxmitpriv->lock);
1992 }
1993 
wakeup_sta_to_xmit(struct adapter * padapter,struct sta_info * psta)1994 void wakeup_sta_to_xmit(struct adapter *padapter, struct sta_info *psta)
1995 {
1996 	u8 update_mask = 0, wmmps_ac = 0;
1997 	struct sta_info *psta_bmc;
1998 	struct list_head *xmitframe_plist, *xmitframe_phead;
1999 	struct xmit_frame *pxmitframe = NULL;
2000 	struct sta_priv *pstapriv = &padapter->stapriv;
2001 
2002 	spin_lock_bh(&psta->sleep_q.lock);
2003 
2004 	xmitframe_phead = get_list_head(&psta->sleep_q);
2005 	xmitframe_plist = xmitframe_phead->next;
2006 
2007 	while (xmitframe_phead != xmitframe_plist) {
2008 		pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
2009 
2010 		xmitframe_plist = xmitframe_plist->next;
2011 
2012 		list_del_init(&pxmitframe->list);
2013 
2014 		switch (pxmitframe->attrib.priority) {
2015 		case 1:
2016 		case 2:
2017 			wmmps_ac = psta->uapsd_bk & BIT(1);
2018 			break;
2019 		case 4:
2020 		case 5:
2021 			wmmps_ac = psta->uapsd_vi & BIT(1);
2022 			break;
2023 		case 6:
2024 		case 7:
2025 			wmmps_ac = psta->uapsd_vo & BIT(1);
2026 			break;
2027 		case 0:
2028 		case 3:
2029 		default:
2030 			wmmps_ac = psta->uapsd_be & BIT(1);
2031 			break;
2032 		}
2033 
2034 		psta->sleepq_len--;
2035 		if (psta->sleepq_len > 0)
2036 			pxmitframe->attrib.mdata = 1;
2037 		else
2038 			pxmitframe->attrib.mdata = 0;
2039 
2040 		if (wmmps_ac) {
2041 			psta->sleepq_ac_len--;
2042 			if (psta->sleepq_ac_len > 0) {
2043 				pxmitframe->attrib.mdata = 1;
2044 				pxmitframe->attrib.eosp = 0;
2045 			} else {
2046 				pxmitframe->attrib.mdata = 0;
2047 				pxmitframe->attrib.eosp = 1;
2048 			}
2049 		}
2050 
2051 		pxmitframe->attrib.triggered = 1;
2052 
2053 		spin_unlock_bh(&psta->sleep_q.lock);
2054 		if (rtl8188eu_hal_xmit(padapter, pxmitframe))
2055 			rtw_xmit_complete(padapter, pxmitframe);
2056 		spin_lock_bh(&psta->sleep_q.lock);
2057 	}
2058 
2059 	if (psta->sleepq_len == 0) {
2060 		pstapriv->tim_bitmap &= ~BIT(psta->aid);
2061 
2062 		update_mask = BIT(0);
2063 
2064 		if (psta->state & WIFI_SLEEP_STATE)
2065 			psta->state ^= WIFI_SLEEP_STATE;
2066 
2067 		if (psta->state & WIFI_STA_ALIVE_CHK_STATE) {
2068 			psta->expire_to = pstapriv->expire_to;
2069 			psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
2070 		}
2071 
2072 		pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
2073 	}
2074 
2075 	spin_unlock_bh(&psta->sleep_q.lock);
2076 
2077 	/* for BC/MC Frames */
2078 	psta_bmc = rtw_get_bcmc_stainfo(padapter);
2079 	if (!psta_bmc)
2080 		return;
2081 
2082 	if ((pstapriv->sta_dz_bitmap & 0xfffe) == 0x0) { /* no any sta in ps mode */
2083 		spin_lock_bh(&psta_bmc->sleep_q.lock);
2084 
2085 		xmitframe_phead = get_list_head(&psta_bmc->sleep_q);
2086 		xmitframe_plist = xmitframe_phead->next;
2087 
2088 		while (xmitframe_phead != xmitframe_plist) {
2089 			pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
2090 
2091 			xmitframe_plist = xmitframe_plist->next;
2092 
2093 			list_del_init(&pxmitframe->list);
2094 
2095 			psta_bmc->sleepq_len--;
2096 			if (psta_bmc->sleepq_len > 0)
2097 				pxmitframe->attrib.mdata = 1;
2098 			else
2099 				pxmitframe->attrib.mdata = 0;
2100 
2101 			pxmitframe->attrib.triggered = 1;
2102 
2103 			spin_unlock_bh(&psta_bmc->sleep_q.lock);
2104 			if (rtl8188eu_hal_xmit(padapter, pxmitframe))
2105 				rtw_xmit_complete(padapter, pxmitframe);
2106 			spin_lock_bh(&psta_bmc->sleep_q.lock);
2107 		}
2108 
2109 		if (psta_bmc->sleepq_len == 0) {
2110 			pstapriv->tim_bitmap &= ~BIT(0);
2111 			pstapriv->sta_dz_bitmap &= ~BIT(0);
2112 
2113 			update_mask |= BIT(1);
2114 		}
2115 
2116 		spin_unlock_bh(&psta_bmc->sleep_q.lock);
2117 	}
2118 
2119 	if (update_mask)
2120 		update_beacon(padapter, _TIM_IE_, NULL, false);
2121 }
2122 
xmit_delivery_enabled_frames(struct adapter * padapter,struct sta_info * psta)2123 void xmit_delivery_enabled_frames(struct adapter *padapter, struct sta_info *psta)
2124 {
2125 	u8 wmmps_ac = 0;
2126 	struct list_head *xmitframe_plist, *xmitframe_phead;
2127 	struct xmit_frame *pxmitframe = NULL;
2128 	struct sta_priv *pstapriv = &padapter->stapriv;
2129 
2130 	spin_lock_bh(&psta->sleep_q.lock);
2131 
2132 	xmitframe_phead = get_list_head(&psta->sleep_q);
2133 	xmitframe_plist = xmitframe_phead->next;
2134 
2135 	while (xmitframe_phead != xmitframe_plist) {
2136 		pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
2137 
2138 		xmitframe_plist = xmitframe_plist->next;
2139 
2140 		switch (pxmitframe->attrib.priority) {
2141 		case 1:
2142 		case 2:
2143 			wmmps_ac = psta->uapsd_bk & BIT(1);
2144 			break;
2145 		case 4:
2146 		case 5:
2147 			wmmps_ac = psta->uapsd_vi & BIT(1);
2148 			break;
2149 		case 6:
2150 		case 7:
2151 			wmmps_ac = psta->uapsd_vo & BIT(1);
2152 			break;
2153 		case 0:
2154 		case 3:
2155 		default:
2156 			wmmps_ac = psta->uapsd_be & BIT(1);
2157 			break;
2158 		}
2159 
2160 		if (!wmmps_ac)
2161 			continue;
2162 
2163 		list_del_init(&pxmitframe->list);
2164 
2165 		psta->sleepq_len--;
2166 		psta->sleepq_ac_len--;
2167 
2168 		if (psta->sleepq_ac_len > 0) {
2169 			pxmitframe->attrib.mdata = 1;
2170 			pxmitframe->attrib.eosp = 0;
2171 		} else {
2172 			pxmitframe->attrib.mdata = 0;
2173 			pxmitframe->attrib.eosp = 1;
2174 		}
2175 
2176 		pxmitframe->attrib.triggered = 1;
2177 
2178 		if (rtl8188eu_hal_xmit(padapter, pxmitframe))
2179 			rtw_xmit_complete(padapter, pxmitframe);
2180 
2181 		if ((psta->sleepq_ac_len == 0) && (!psta->has_legacy_ac) && (wmmps_ac)) {
2182 			pstapriv->tim_bitmap &= ~BIT(psta->aid);
2183 
2184 			/* update BCN for TIM IE */
2185 			update_beacon(padapter, _TIM_IE_, NULL, false);
2186 		}
2187 	}
2188 
2189 	spin_unlock_bh(&psta->sleep_q.lock);
2190 }
2191 
rtw_sctx_init(struct submit_ctx * sctx,int timeout_ms)2192 void rtw_sctx_init(struct submit_ctx *sctx, int timeout_ms)
2193 {
2194 	sctx->timeout_ms = timeout_ms;
2195 	sctx->submit_time = jiffies;
2196 	init_completion(&sctx->done);
2197 	sctx->status = RTW_SCTX_SUBMITTED;
2198 }
2199 
rtw_sctx_wait(struct submit_ctx * sctx)2200 int rtw_sctx_wait(struct submit_ctx *sctx)
2201 {
2202 	int ret = _FAIL;
2203 	unsigned long expire;
2204 	int status = 0;
2205 
2206 	expire = sctx->timeout_ms ? msecs_to_jiffies(sctx->timeout_ms) : MAX_SCHEDULE_TIMEOUT;
2207 	if (!wait_for_completion_timeout(&sctx->done, expire))
2208 		/* timeout, do something?? */
2209 		status = RTW_SCTX_DONE_TIMEOUT;
2210 	else
2211 		status = sctx->status;
2212 
2213 	if (status == RTW_SCTX_DONE_SUCCESS)
2214 		ret = _SUCCESS;
2215 
2216 	return ret;
2217 }
2218 
rtw_sctx_done_err(struct submit_ctx ** sctx,int status)2219 void rtw_sctx_done_err(struct submit_ctx **sctx, int status)
2220 {
2221 	if (*sctx) {
2222 		(*sctx)->status = status;
2223 		complete(&((*sctx)->done));
2224 		*sctx = NULL;
2225 	}
2226 }
2227 
rtw_ack_tx_wait(struct xmit_priv * pxmitpriv,u32 timeout_ms)2228 int rtw_ack_tx_wait(struct xmit_priv *pxmitpriv, u32 timeout_ms)
2229 {
2230 	struct submit_ctx *pack_tx_ops = &pxmitpriv->ack_tx_ops;
2231 
2232 	pack_tx_ops->submit_time = jiffies;
2233 	pack_tx_ops->timeout_ms = timeout_ms;
2234 	pack_tx_ops->status = RTW_SCTX_SUBMITTED;
2235 
2236 	return rtw_sctx_wait(pack_tx_ops);
2237 }
2238 
rtw_ack_tx_done(struct xmit_priv * pxmitpriv,int status)2239 void rtw_ack_tx_done(struct xmit_priv *pxmitpriv, int status)
2240 {
2241 	struct submit_ctx *pack_tx_ops = &pxmitpriv->ack_tx_ops;
2242 
2243 	if (pxmitpriv->ack_tx)
2244 		rtw_sctx_done_err(&pack_tx_ops, status);
2245 }
2246 
rtw_check_xmit_resource(struct adapter * padapter,struct sk_buff * pkt)2247 static void rtw_check_xmit_resource(struct adapter *padapter, struct sk_buff *pkt)
2248 {
2249 	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2250 	u16 queue;
2251 
2252 	queue = skb_get_queue_mapping(pkt);
2253 	if (padapter->registrypriv.wifi_spec) {
2254 		/* No free space for Tx, tx_worker is too slow */
2255 		if (pxmitpriv->hwxmits[queue].accnt > WMM_XMIT_THRESHOLD)
2256 			netif_stop_subqueue(padapter->pnetdev, queue);
2257 	} else {
2258 		if (pxmitpriv->free_xmitframe_cnt <= 4) {
2259 			if (!netif_tx_queue_stopped(netdev_get_tx_queue(padapter->pnetdev, queue)))
2260 				netif_stop_subqueue(padapter->pnetdev, queue);
2261 		}
2262 	}
2263 }
2264 
rtw_mlcst2unicst(struct adapter * padapter,struct sk_buff * skb)2265 static int rtw_mlcst2unicst(struct adapter *padapter, struct sk_buff *skb)
2266 {
2267 	struct	sta_priv *pstapriv = &padapter->stapriv;
2268 	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2269 	struct list_head *phead, *plist;
2270 	struct sk_buff *newskb;
2271 	struct sta_info *psta = NULL;
2272 	s32 res;
2273 
2274 	spin_lock_bh(&pstapriv->asoc_list_lock);
2275 	phead = &pstapriv->asoc_list;
2276 	plist = phead->next;
2277 
2278 	/* free sta asoc_queue */
2279 	while (phead != plist) {
2280 		psta = container_of(plist, struct sta_info, asoc_list);
2281 
2282 		plist = plist->next;
2283 
2284 		/* avoid   come from STA1 and send back STA1 */
2285 		if (!memcmp(psta->hwaddr, &skb->data[6], 6))
2286 			continue;
2287 
2288 		newskb = skb_copy(skb, GFP_ATOMIC);
2289 
2290 		if (newskb) {
2291 			memcpy(newskb->data, psta->hwaddr, 6);
2292 			res = rtw_xmit(padapter, &newskb);
2293 			if (res < 0) {
2294 				pxmitpriv->tx_drop++;
2295 				dev_kfree_skb_any(newskb);
2296 			} else {
2297 				pxmitpriv->tx_pkts++;
2298 			}
2299 		} else {
2300 			pxmitpriv->tx_drop++;
2301 
2302 			spin_unlock_bh(&pstapriv->asoc_list_lock);
2303 			return false;	/*  Caller shall tx this multicast frame via normal way. */
2304 		}
2305 	}
2306 
2307 	spin_unlock_bh(&pstapriv->asoc_list_lock);
2308 	dev_kfree_skb_any(skb);
2309 	return true;
2310 }
2311 
rtw_xmit_entry(struct sk_buff * pkt,struct net_device * pnetdev)2312 netdev_tx_t rtw_xmit_entry(struct sk_buff *pkt, struct net_device *pnetdev)
2313 {
2314 	struct adapter *padapter = (struct adapter *)rtw_netdev_priv(pnetdev);
2315 	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2316 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2317 	s32 res = 0;
2318 
2319 	if (!rtw_if_up(padapter))
2320 		goto drop_packet;
2321 
2322 	rtw_check_xmit_resource(padapter, pkt);
2323 
2324 	if (!rtw_mc2u_disable && check_fwstate(pmlmepriv, WIFI_AP_STATE) &&
2325 	    (IP_MCAST_MAC(pkt->data) || ICMPV6_MCAST_MAC(pkt->data)) &&
2326 	    (padapter->registrypriv.wifi_spec == 0)) {
2327 		if (pxmitpriv->free_xmitframe_cnt > (NR_XMITFRAME / 4)) {
2328 			res = rtw_mlcst2unicst(padapter, pkt);
2329 			if (res)
2330 				goto exit;
2331 		}
2332 	}
2333 
2334 	res = rtw_xmit(padapter, &pkt);
2335 	if (res < 0)
2336 		goto drop_packet;
2337 
2338 	pxmitpriv->tx_pkts++;
2339 	goto exit;
2340 
2341 drop_packet:
2342 	pxmitpriv->tx_drop++;
2343 	dev_kfree_skb_any(pkt);
2344 
2345 exit:
2346 	return NETDEV_TX_OK;
2347 }
2348