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