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