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