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