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