1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2012 Realtek Corporation. */
6 #include "../include/osdep_service.h"
7 #include "../include/drv_types.h"
8 #include "../include/wifi.h"
9 #include "../include/osdep_intf.h"
10 #include "../include/usb_ops.h"
11 #include "../include/usb_osintf.h"
12 #include "../include/rtl8188e_xmit.h"
14 static u8 P802_1H_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0xf8 };
15 static u8 RFC1042_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0x00 };
17 static void _init_txservq(struct tx_servq *ptxservq)
20 INIT_LIST_HEAD(&ptxservq->tx_pending);
21 rtw_init_queue(&ptxservq->sta_pending);
26 void _rtw_init_sta_xmit_priv(struct sta_xmit_priv *psta_xmitpriv)
29 memset((unsigned char *)psta_xmitpriv, 0, sizeof(struct sta_xmit_priv));
30 spin_lock_init(&psta_xmitpriv->lock);
31 _init_txservq(&psta_xmitpriv->be_q);
32 _init_txservq(&psta_xmitpriv->bk_q);
33 _init_txservq(&psta_xmitpriv->vi_q);
34 _init_txservq(&psta_xmitpriv->vo_q);
35 INIT_LIST_HEAD(&psta_xmitpriv->legacy_dz);
36 INIT_LIST_HEAD(&psta_xmitpriv->apsd);
40 s32 _rtw_init_xmit_priv(struct xmit_priv *pxmitpriv, struct adapter *padapter)
43 struct xmit_buf *pxmitbuf;
44 struct xmit_frame *pxframe;
46 u32 max_xmit_extbuf_size = MAX_XMIT_EXTBUF_SZ;
47 u32 num_xmit_extbuf = NR_XMIT_EXTBUFF;
49 /* We don't need to memset padapter->XXX to zero, because adapter is allocated by vzalloc(). */
51 spin_lock_init(&pxmitpriv->lock);
52 sema_init(&pxmitpriv->terminate_xmitthread_sema, 0);
55 * Please insert all the queue initializaiton using rtw_init_queue below
58 pxmitpriv->adapter = padapter;
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);
66 rtw_init_queue(&pxmitpriv->free_xmit_queue);
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...
74 pxmitpriv->pallocated_frame_buf = vzalloc(NR_XMITFRAME * sizeof(struct xmit_frame) + 4);
76 if (!pxmitpriv->pallocated_frame_buf) {
77 pxmitpriv->pxmit_frame_buf = NULL;
81 pxmitpriv->pxmit_frame_buf = (u8 *)N_BYTE_ALIGMENT((size_t)(pxmitpriv->pallocated_frame_buf), 4);
82 /* pxmitpriv->pxmit_frame_buf = pxmitpriv->pallocated_frame_buf + 4 - */
83 /* ((size_t) (pxmitpriv->pallocated_frame_buf) &3); */
85 pxframe = (struct xmit_frame *)pxmitpriv->pxmit_frame_buf;
87 for (i = 0; i < NR_XMITFRAME; i++) {
88 INIT_LIST_HEAD(&pxframe->list);
90 pxframe->padapter = padapter;
91 pxframe->frame_tag = NULL_FRAMETAG;
95 pxframe->buf_addr = NULL;
96 pxframe->pxmitbuf = NULL;
98 list_add_tail(&pxframe->list, &pxmitpriv->free_xmit_queue.queue);
103 pxmitpriv->free_xmitframe_cnt = NR_XMITFRAME;
105 pxmitpriv->frag_len = MAX_FRAG_THRESHOLD;
108 rtw_init_queue(&pxmitpriv->free_xmitbuf_queue);
109 rtw_init_queue(&pxmitpriv->pending_xmitbuf_queue);
111 pxmitpriv->pallocated_xmitbuf = vzalloc(NR_XMITBUFF * sizeof(struct xmit_buf) + 4);
113 if (!pxmitpriv->pallocated_xmitbuf) {
118 pxmitpriv->pxmitbuf = (u8 *)N_BYTE_ALIGMENT((size_t)(pxmitpriv->pallocated_xmitbuf), 4);
119 /* pxmitpriv->pxmitbuf = pxmitpriv->pallocated_xmitbuf + 4 - */
120 /* ((size_t) (pxmitpriv->pallocated_xmitbuf) &3); */
122 pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
124 for (i = 0; i < NR_XMITBUFF; i++) {
125 INIT_LIST_HEAD(&pxmitbuf->list);
127 pxmitbuf->priv_data = NULL;
128 pxmitbuf->padapter = padapter;
129 pxmitbuf->ext_tag = false;
131 /* Tx buf allocation may fail sometimes, so sleep and retry. */
132 res = rtw_os_xmit_resource_alloc(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ));
135 res = rtw_os_xmit_resource_alloc(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ));
140 pxmitbuf->flags = XMIT_VO_QUEUE;
142 list_add_tail(&pxmitbuf->list, &pxmitpriv->free_xmitbuf_queue.queue);
146 pxmitpriv->free_xmitbuf_cnt = NR_XMITBUFF;
148 /* Init xmit extension buff */
149 rtw_init_queue(&pxmitpriv->free_xmit_extbuf_queue);
151 pxmitpriv->pallocated_xmit_extbuf = vzalloc(num_xmit_extbuf * sizeof(struct xmit_buf) + 4);
153 if (!pxmitpriv->pallocated_xmit_extbuf) {
158 pxmitpriv->pxmit_extbuf = (u8 *)N_BYTE_ALIGMENT((size_t)(pxmitpriv->pallocated_xmit_extbuf), 4);
160 pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmit_extbuf;
162 for (i = 0; i < num_xmit_extbuf; i++) {
163 INIT_LIST_HEAD(&pxmitbuf->list);
165 pxmitbuf->priv_data = NULL;
166 pxmitbuf->padapter = padapter;
167 pxmitbuf->ext_tag = true;
169 res = rtw_os_xmit_resource_alloc(padapter, pxmitbuf, max_xmit_extbuf_size + XMITBUF_ALIGN_SZ);
175 list_add_tail(&pxmitbuf->list, &pxmitpriv->free_xmit_extbuf_queue.queue);
179 pxmitpriv->free_xmit_extbuf_cnt = num_xmit_extbuf;
181 if (rtw_alloc_hwxmits(padapter)) {
186 rtw_init_hwxmits(pxmitpriv->hwxmits, pxmitpriv->hwxmit_entry);
188 for (i = 0; i < 4; i++)
189 pxmitpriv->wmm_para_seq[i] = i;
191 pxmitpriv->txirp_cnt = 1;
193 sema_init(&pxmitpriv->tx_retevt, 0);
195 /* per AC pending irp */
196 pxmitpriv->beq_cnt = 0;
197 pxmitpriv->bkq_cnt = 0;
198 pxmitpriv->viq_cnt = 0;
199 pxmitpriv->voq_cnt = 0;
201 pxmitpriv->ack_tx = false;
202 mutex_init(&pxmitpriv->ack_tx_mutex);
203 rtw_sctx_init(&pxmitpriv->ack_tx_ops, 0);
205 rtl8188eu_init_xmit_priv(padapter);
212 void _rtw_free_xmit_priv(struct xmit_priv *pxmitpriv)
215 struct adapter *padapter = pxmitpriv->adapter;
216 struct xmit_frame *pxmitframe = (struct xmit_frame *)pxmitpriv->pxmit_frame_buf;
217 struct xmit_buf *pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
218 u32 max_xmit_extbuf_size = MAX_XMIT_EXTBUF_SZ;
219 u32 num_xmit_extbuf = NR_XMIT_EXTBUFF;
221 if (!pxmitpriv->pxmit_frame_buf)
224 for (i = 0; i < NR_XMITFRAME; i++) {
225 rtw_os_xmit_complete(padapter, pxmitframe);
230 for (i = 0; i < NR_XMITBUFF; i++) {
231 rtw_os_xmit_resource_free(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ));
235 vfree(pxmitpriv->pallocated_frame_buf);
237 vfree(pxmitpriv->pallocated_xmitbuf);
239 pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmit_extbuf;
240 for (i = 0; i < num_xmit_extbuf; i++) {
241 rtw_os_xmit_resource_free(padapter, pxmitbuf, (max_xmit_extbuf_size + XMITBUF_ALIGN_SZ));
245 vfree(pxmitpriv->pallocated_xmit_extbuf);
247 rtw_free_hwxmits(padapter);
249 mutex_destroy(&pxmitpriv->ack_tx_mutex);
252 static void update_attrib_vcs_info(struct adapter *padapter, struct xmit_frame *pxmitframe)
255 struct pkt_attrib *pattrib = &pxmitframe->attrib;
256 struct sta_info *psta = pattrib->psta;
257 struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
258 struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info;
260 if (pattrib->nr_frags != 1)
261 sz = padapter->xmitpriv.frag_len;
263 sz = pattrib->last_txcmdsz;
265 /* (1) RTS_Threshold is compared to the MPDU, not MSDU. */
266 /* (2) If there are more than one frag in this MSDU, only the first frag uses protection frame. */
267 /* Other fragments are protected by previous fragment. */
268 /* So we only need to check the length of first fragment. */
269 if (pmlmeext->cur_wireless_mode < WIRELESS_11_24N || padapter->registrypriv.wifi_spec) {
270 if (sz > padapter->registrypriv.rts_thresh) {
271 pattrib->vcs_mode = RTS_CTS;
274 pattrib->vcs_mode = RTS_CTS;
275 else if (psta->cts2self)
276 pattrib->vcs_mode = CTS_TO_SELF;
278 pattrib->vcs_mode = NONE_VCS;
283 if ((pmlmeinfo->assoc_AP_vendor == HT_IOT_PEER_ATHEROS) && pattrib->ampdu_en &&
284 (padapter->securitypriv.dot11PrivacyAlgrthm == _AES_)) {
285 pattrib->vcs_mode = CTS_TO_SELF;
289 /* check ERP protection */
290 if (psta->rtsen || psta->cts2self) {
292 pattrib->vcs_mode = RTS_CTS;
293 else if (psta->cts2self)
294 pattrib->vcs_mode = CTS_TO_SELF;
299 /* check HT op mode */
300 if (pattrib->ht_en) {
301 u8 htopmode = pmlmeinfo->HT_protection;
302 if ((pmlmeext->cur_bwmode && (htopmode == 2 || htopmode == 3)) ||
303 (!pmlmeext->cur_bwmode && htopmode == 3)) {
304 pattrib->vcs_mode = RTS_CTS;
310 if (sz > padapter->registrypriv.rts_thresh) {
311 pattrib->vcs_mode = RTS_CTS;
315 /* to do list: check MIMO power save condition. */
317 /* check AMPDU aggregation for TXOP */
318 if (pattrib->ampdu_en) {
319 pattrib->vcs_mode = RTS_CTS;
323 pattrib->vcs_mode = NONE_VCS;
329 static void update_attrib_phy_info(struct pkt_attrib *pattrib, struct sta_info *psta)
332 pattrib->vcs_mode = RTS_CTS;
333 else if (psta->cts2self)
334 pattrib->vcs_mode = CTS_TO_SELF;
336 pattrib->vcs_mode = NONE_VCS;*/
340 pattrib->triggered = 0;
342 /* qos_en, ht_en, init rate, , bw, ch_offset, sgi */
343 pattrib->qos_en = psta->qos_option;
345 pattrib->raid = psta->raid;
346 pattrib->ht_en = psta->htpriv.ht_option;
347 pattrib->bwmode = psta->htpriv.bwmode;
348 pattrib->ch_offset = psta->htpriv.ch_offset;
349 pattrib->sgi = psta->htpriv.sgi;
350 pattrib->ampdu_en = false;
351 pattrib->retry_ctrl = false;
354 u8 qos_acm(u8 acm_mask, u8 priority)
356 u8 change_priority = priority;
361 if (acm_mask & BIT(1))
369 if (acm_mask & BIT(2))
374 if (acm_mask & BIT(3))
381 return change_priority;
384 static void set_qos(struct pkt_file *ppktfile, struct pkt_attrib *pattrib)
386 struct ethhdr etherhdr;
390 _rtw_open_pktfile(ppktfile->pkt, ppktfile);
391 _rtw_pktfile_read(ppktfile, (unsigned char *)ðerhdr, ETH_HLEN);
393 /* get user_prio from IP hdr */
394 if (pattrib->ether_type == 0x0800) {
395 _rtw_pktfile_read(ppktfile, (u8 *)&ip_hdr, sizeof(ip_hdr));
396 /* user_prio = (ntohs(ip_hdr.tos) >> 5) & 0x3; */
397 user_prio = ip_hdr.tos >> 5;
398 } else if (pattrib->ether_type == 0x888e) {
399 /* "When priority processing of data frames is supported, */
400 /* a STA's SME should send EAPOL-Key frames at the highest priority." */
404 pattrib->priority = user_prio;
405 pattrib->hdrlen = WLAN_HDR_A3_QOS_LEN;
406 pattrib->subtype = IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA;
409 static s32 update_attrib(struct adapter *padapter, struct sk_buff *pkt, struct pkt_attrib *pattrib)
411 struct pkt_file pktfile;
412 struct sta_info *psta = NULL;
413 struct ethhdr etherhdr;
416 struct sta_priv *pstapriv = &padapter->stapriv;
417 struct security_priv *psecuritypriv = &padapter->securitypriv;
418 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
419 struct qos_priv *pqospriv = &pmlmepriv->qospriv;
424 _rtw_open_pktfile(pkt, &pktfile);
425 _rtw_pktfile_read(&pktfile, (u8 *)ðerhdr, ETH_HLEN);
427 pattrib->ether_type = ntohs(etherhdr.h_proto);
429 memcpy(pattrib->dst, ðerhdr.h_dest, ETH_ALEN);
430 memcpy(pattrib->src, ðerhdr.h_source, ETH_ALEN);
434 if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) ||
435 check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
436 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
437 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
438 } else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
439 memcpy(pattrib->ra, get_bssid(pmlmepriv), ETH_ALEN);
440 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
441 } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
442 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
443 memcpy(pattrib->ta, get_bssid(pmlmepriv), ETH_ALEN);
446 pattrib->pktlen = pktfile.pkt_len;
448 if (ETH_P_IP == pattrib->ether_type) {
449 /* The following is for DHCP and ARP packet, we use cck1M to tx these packets and let LPS awake some time */
450 /* to prevent DHCP protocol fail */
452 _rtw_pktfile_read(&pktfile, &tmp[0], 24);
453 pattrib->dhcp_pkt = 0;
454 if (pktfile.pkt_len > 282) {/* MINIMUM_DHCP_PACKET_SIZE) { */
455 if (((tmp[21] == 68) && (tmp[23] == 67)) ||
456 ((tmp[21] == 67) && (tmp[23] == 68))) {
457 /* 68 : UDP BOOTP client */
458 /* 67 : UDP BOOTP server */
459 /* Use low rate to send DHCP packet. */
460 pattrib->dhcp_pkt = 1;
465 if ((pattrib->ether_type == 0x888e) || (pattrib->dhcp_pkt == 1))
466 rtw_set_scan_deny(padapter, 3000);
468 /* If EAPOL , ARP , OR DHCP packet, driver must be in active mode. */
469 if ((pattrib->ether_type == 0x0806) || (pattrib->ether_type == 0x888e) || (pattrib->dhcp_pkt == 1))
470 rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_SPECIAL_PACKET, 1);
472 bmcast = is_multicast_ether_addr(pattrib->ra);
476 psta = rtw_get_bcmc_stainfo(padapter);
478 psta = rtw_get_stainfo(pstapriv, pattrib->ra);
479 if (!psta) { /* if we cannot get psta => drrp the pkt */
482 } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE) && !(psta->state & _FW_LINKED)) {
489 pattrib->mac_id = psta->mac_id;
490 pattrib->psta = psta;
492 /* if we cannot get psta => drop the pkt */
497 pattrib->ack_policy = 0;
498 /* get ether_hdr_len */
499 pattrib->pkt_hdrlen = ETH_HLEN;/* pattrib->ether_type == 0x8100) ? (14 + 4): 14; vlan tag */
501 pattrib->hdrlen = WLAN_HDR_A3_LEN;
502 pattrib->subtype = IEEE80211_FTYPE_DATA;
503 pattrib->priority = 0;
505 if (check_fwstate(pmlmepriv, WIFI_AP_STATE | WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE)) {
506 if (psta->qos_option)
507 set_qos(&pktfile, pattrib);
509 if (pqospriv->qos_option) {
510 set_qos(&pktfile, pattrib);
512 if (pmlmepriv->acm_mask != 0)
513 pattrib->priority = qos_acm(pmlmepriv->acm_mask, pattrib->priority);
517 if (psta->ieee8021x_blocked) {
518 pattrib->encrypt = 0;
520 if ((pattrib->ether_type != 0x888e) && !check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
525 GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt, bmcast);
527 switch (psecuritypriv->dot11AuthAlgrthm) {
528 case dot11AuthAlgrthm_Open:
529 case dot11AuthAlgrthm_Shared:
530 case dot11AuthAlgrthm_Auto:
531 pattrib->key_idx = (u8)psecuritypriv->dot11PrivacyKeyIndex;
533 case dot11AuthAlgrthm_8021X:
535 pattrib->key_idx = (u8)psecuritypriv->dot118021XGrpKeyid;
537 pattrib->key_idx = 0;
540 pattrib->key_idx = 0;
545 switch (pattrib->encrypt) {
549 pattrib->icv_len = 4;
553 pattrib->icv_len = 4;
555 if (padapter->securitypriv.busetkipkey == _FAIL) {
562 pattrib->icv_len = 8;
566 pattrib->icv_len = 0;
570 if (pattrib->encrypt &&
571 (padapter->securitypriv.sw_encrypt || !psecuritypriv->hw_decrypted))
572 pattrib->bswenc = true;
574 pattrib->bswenc = false;
576 update_attrib_phy_info(pattrib, psta);
583 static s32 xmitframe_addmic(struct adapter *padapter, struct xmit_frame *pxmitframe)
585 int curfragnum, length;
586 u8 *pframe, *payload, mic[8];
587 struct mic_data micdata;
588 struct sta_info *stainfo;
589 struct pkt_attrib *pattrib = &pxmitframe->attrib;
590 struct security_priv *psecuritypriv = &padapter->securitypriv;
591 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
592 u8 priority[4] = {0x0, 0x0, 0x0, 0x0};
593 u8 hw_hdr_offset = 0;
596 stainfo = pattrib->psta;
598 stainfo = rtw_get_stainfo(&padapter->stapriv, &pattrib->ra[0]);
600 hw_hdr_offset = TXDESC_SIZE + (pxmitframe->pkt_offset * PACKET_OFFSET_SZ);
602 if (pattrib->encrypt == _TKIP_) {/* if (psecuritypriv->dot11PrivacyAlgrthm == _TKIP_PRIVACY_) */
603 /* encode mic code */
605 u8 null_key[16] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
606 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
609 pframe = pxmitframe->buf_addr + hw_hdr_offset;
611 if (is_multicast_ether_addr(pattrib->ra)) {
612 if (!memcmp(psecuritypriv->dot118021XGrptxmickey[psecuritypriv->dot118021XGrpKeyid].skey, null_key, 16))
614 /* start to calculate the mic code */
615 rtw_secmicsetkey(&micdata, psecuritypriv->dot118021XGrptxmickey[psecuritypriv->dot118021XGrpKeyid].skey);
617 if (!memcmp(&stainfo->dot11tkiptxmickey.skey[0], null_key, 16)) {
621 /* start to calculate the mic code */
622 rtw_secmicsetkey(&micdata, &stainfo->dot11tkiptxmickey.skey[0]);
625 if (pframe[1] & 1) { /* ToDS == 1 */
626 rtw_secmicappend(&micdata, &pframe[16], 6); /* DA */
627 if (pframe[1] & 2) /* From Ds == 1 */
628 rtw_secmicappend(&micdata, &pframe[24], 6);
630 rtw_secmicappend(&micdata, &pframe[10], 6);
631 } else { /* ToDS == 0 */
632 rtw_secmicappend(&micdata, &pframe[4], 6); /* DA */
633 if (pframe[1] & 2) /* From Ds == 1 */
634 rtw_secmicappend(&micdata, &pframe[16], 6);
636 rtw_secmicappend(&micdata, &pframe[10], 6);
640 priority[0] = (u8)pxmitframe->attrib.priority;
642 rtw_secmicappend(&micdata, &priority[0], 4);
646 for (curfragnum = 0; curfragnum < pattrib->nr_frags; curfragnum++) {
647 payload = PTR_ALIGN(payload, 4);
649 payload = payload + pattrib->hdrlen + pattrib->iv_len;
650 if ((curfragnum + 1) == pattrib->nr_frags) {
651 length = pattrib->last_txcmdsz - pattrib->hdrlen - pattrib->iv_len - ((pattrib->bswenc) ? pattrib->icv_len : 0);
652 rtw_secmicappend(&micdata, payload, length);
653 payload = payload + length;
655 length = pxmitpriv->frag_len - pattrib->hdrlen - pattrib->iv_len - ((pattrib->bswenc) ? pattrib->icv_len : 0);
656 rtw_secmicappend(&micdata, payload, length);
657 payload = payload + length + pattrib->icv_len;
660 rtw_secgetmic(&micdata, &mic[0]);
661 /* add mic code and add the mic code length in last_txcmdsz */
663 memcpy(payload, &mic[0], 8);
664 pattrib->last_txcmdsz += 8;
666 payload = payload - pattrib->last_txcmdsz + 8;
673 static s32 xmitframe_swencrypt(struct adapter *padapter, struct xmit_frame *pxmitframe)
675 struct pkt_attrib *pattrib = &pxmitframe->attrib;
677 if (pattrib->bswenc) {
678 switch (pattrib->encrypt) {
681 rtw_wep_encrypt(padapter, pxmitframe);
684 rtw_tkip_encrypt(padapter, pxmitframe);
687 rtw_aes_encrypt(padapter, pxmitframe);
697 s32 rtw_make_wlanhdr(struct adapter *padapter, u8 *hdr, struct pkt_attrib *pattrib)
701 struct ieee80211_hdr *pwlanhdr = (struct ieee80211_hdr *)hdr;
702 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
703 struct qos_priv *pqospriv = &pmlmepriv->qospriv;
704 u8 qos_option = false;
707 __le16 *fctrl = &pwlanhdr->frame_control;
709 struct sta_info *psta;
712 psta = pattrib->psta;
713 else if (is_multicast_ether_addr(pattrib->ra))
714 psta = rtw_get_bcmc_stainfo(padapter);
716 psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
718 memset(hdr, 0, WLANHDR_OFFSET);
720 SetFrameSubType(fctrl, pattrib->subtype);
722 if (pattrib->subtype & IEEE80211_FTYPE_DATA) {
723 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
724 /* to_ds = 1, fr_ds = 0; */
725 /* Data transfer to AP */
727 memcpy(pwlanhdr->addr1, get_bssid(pmlmepriv), ETH_ALEN);
728 memcpy(pwlanhdr->addr2, pattrib->src, ETH_ALEN);
729 memcpy(pwlanhdr->addr3, pattrib->dst, ETH_ALEN);
731 if (pqospriv->qos_option)
733 } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
734 /* to_ds = 0, fr_ds = 1; */
736 memcpy(pwlanhdr->addr1, pattrib->dst, ETH_ALEN);
737 memcpy(pwlanhdr->addr2, get_bssid(pmlmepriv), ETH_ALEN);
738 memcpy(pwlanhdr->addr3, pattrib->src, ETH_ALEN);
740 if (psta->qos_option)
742 } else if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) ||
743 check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
744 memcpy(pwlanhdr->addr1, pattrib->dst, ETH_ALEN);
745 memcpy(pwlanhdr->addr2, pattrib->src, ETH_ALEN);
746 memcpy(pwlanhdr->addr3, get_bssid(pmlmepriv), ETH_ALEN);
748 if (psta->qos_option)
758 if (pattrib->encrypt)
762 qc = (unsigned short *)(hdr + pattrib->hdrlen - 2);
764 if (pattrib->priority)
765 SetPriority(qc, pattrib->priority);
767 SetEOSP(qc, pattrib->eosp);
769 SetAckpolicy(qc, pattrib->ack_policy);
772 /* TODO: fill HT Control Field */
774 /* Update Seq Num will be handled by f/w */
776 psta->sta_xmitpriv.txseq_tid[pattrib->priority]++;
777 psta->sta_xmitpriv.txseq_tid[pattrib->priority] &= 0xFFF;
779 pattrib->seqnum = psta->sta_xmitpriv.txseq_tid[pattrib->priority];
781 SetSeqNum(hdr, pattrib->seqnum);
783 /* check if enable ampdu */
784 if (pattrib->ht_en && psta->htpriv.ampdu_enable) {
785 if (psta->htpriv.agg_enable_bitmap & BIT(pattrib->priority))
786 pattrib->ampdu_en = true;
789 /* re-check if enable ampdu by BA_starting_seqctrl */
790 if (pattrib->ampdu_en) {
793 tx_seq = psta->BA_starting_seqctrl[pattrib->priority & 0x0f];
795 /* check BA_starting_seqctrl */
796 if (SN_LESS(pattrib->seqnum, tx_seq)) {
797 pattrib->ampdu_en = false;/* AGG BK */
798 } else if (SN_EQUAL(pattrib->seqnum, tx_seq)) {
799 psta->BA_starting_seqctrl[pattrib->priority & 0x0f] = (tx_seq + 1) & 0xfff;
801 pattrib->ampdu_en = true;/* AGG EN */
803 psta->BA_starting_seqctrl[pattrib->priority & 0x0f] = (pattrib->seqnum + 1) & 0xfff;
804 pattrib->ampdu_en = true;/* AGG EN */
814 s32 rtw_txframes_pending(struct adapter *padapter)
816 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
818 return (!list_empty(&pxmitpriv->be_pending.queue) ||
819 !list_empty(&pxmitpriv->bk_pending.queue) ||
820 !list_empty(&pxmitpriv->vi_pending.queue) ||
821 !list_empty(&pxmitpriv->vo_pending.queue));
824 s32 rtw_txframes_sta_ac_pending(struct adapter *padapter, struct pkt_attrib *pattrib)
826 struct sta_info *psta;
827 struct tx_servq *ptxservq;
828 int priority = pattrib->priority;
830 psta = pattrib->psta;
835 ptxservq = &psta->sta_xmitpriv.bk_q;
839 ptxservq = &psta->sta_xmitpriv.vi_q;
843 ptxservq = &psta->sta_xmitpriv.vo_q;
848 ptxservq = &psta->sta_xmitpriv.be_q;
853 return ptxservq->qcnt;
858 * This sub-routine will perform all the following:
860 * 1. remove 802.3 header.
861 * 2. create wlan_header, based on the info in pxmitframe
862 * 3. append sta's iv/ext-iv
864 * 5. move frag chunk from pframe to pxmitframe->mem
865 * 6. apply sw-encrypt, if necessary.
867 s32 rtw_xmitframe_coalesce(struct adapter *padapter, struct sk_buff *pkt, struct xmit_frame *pxmitframe)
869 struct pkt_file pktfile;
870 s32 frg_inx, frg_len, mpdu_len, llc_sz, mem_sz;
871 u8 *pframe, *mem_start;
873 struct sta_info *psta;
874 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
875 struct pkt_attrib *pattrib = &pxmitframe->attrib;
877 bool bmcst = is_multicast_ether_addr(pattrib->ra);
883 psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
888 if (!pxmitframe->buf_addr)
891 pbuf_start = pxmitframe->buf_addr;
893 hw_hdr_offset = TXDESC_SIZE + (pxmitframe->pkt_offset * PACKET_OFFSET_SZ);
895 mem_start = pbuf_start + hw_hdr_offset;
897 if (rtw_make_wlanhdr(padapter, mem_start, pattrib) == _FAIL) {
902 _rtw_open_pktfile(pkt, &pktfile);
903 _rtw_pktfile_read(&pktfile, NULL, pattrib->pkt_hdrlen);
906 frg_len = pxmitpriv->frag_len - 4;/* 2346-4 = 2342 */
917 pframe += pattrib->hdrlen;
918 mpdu_len -= pattrib->hdrlen;
920 /* adding icv, if necessary... */
921 if (pattrib->iv_len) {
923 switch (pattrib->encrypt) {
926 WEP_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
930 TKIP_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
932 TKIP_IV(pattrib->iv, psta->dot11txpn, 0);
936 AES_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
938 AES_IV(pattrib->iv, psta->dot11txpn, 0);
943 memcpy(pframe, pattrib->iv, pattrib->iv_len);
945 pframe += pattrib->iv_len;
947 mpdu_len -= pattrib->iv_len;
951 llc_sz = rtw_put_snap(pframe, pattrib->ether_type);
956 if ((pattrib->icv_len > 0) && (pattrib->bswenc)) {
957 mpdu_len -= pattrib->icv_len;
961 /* don't do fragment to broadcat/multicast packets */
962 mem_sz = _rtw_pktfile_read(&pktfile, pframe, pattrib->pktlen);
964 mem_sz = _rtw_pktfile_read(&pktfile, pframe, mpdu_len);
969 if ((pattrib->icv_len > 0) && (pattrib->bswenc)) {
970 memcpy(pframe, pattrib->icv, pattrib->icv_len);
971 pframe += pattrib->icv_len;
976 if (bmcst || rtw_endofpktfile(&pktfile)) {
977 pattrib->nr_frags = frg_inx;
979 pattrib->last_txcmdsz = pattrib->hdrlen + pattrib->iv_len + ((pattrib->nr_frags == 1) ? llc_sz : 0) +
980 ((pattrib->bswenc) ? pattrib->icv_len : 0) + mem_sz;
982 ClearMFrag(mem_start);
987 mem_start = PTR_ALIGN(pframe, 4) + hw_hdr_offset;
988 memcpy(mem_start, pbuf_start + hw_hdr_offset, pattrib->hdrlen);
991 if (xmitframe_addmic(padapter, pxmitframe) == _FAIL) {
996 xmitframe_swencrypt(padapter, pxmitframe);
999 update_attrib_vcs_info(padapter, pxmitframe);
1001 pattrib->vcs_mode = NONE_VCS;
1008 /* Logical Link Control(LLC) SubNetwork Attachment Point(SNAP) header
1009 * IEEE LLC/SNAP header contains 8 octets
1010 * First 3 octets comprise the LLC portion
1011 * SNAP portion, 5 octets, is divided into two fields:
1012 * Organizationally Unique Identifier(OUI), 3 octets,
1013 * type, defined by that organization, 2 octets.
1015 s32 rtw_put_snap(u8 *data, u16 h_proto)
1017 struct ieee80211_snap_hdr *snap;
1020 snap = (struct ieee80211_snap_hdr *)data;
1025 if (h_proto == 0x8137 || h_proto == 0x80f3)
1030 snap->oui[0] = oui[0];
1031 snap->oui[1] = oui[1];
1032 snap->oui[2] = oui[2];
1034 *(__be16 *)(data + SNAP_SIZE) = htons(h_proto);
1036 return SNAP_SIZE + sizeof(u16);
1039 void rtw_update_protection(struct adapter *padapter, u8 *ie, uint ie_len)
1044 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1045 struct registry_priv *pregistrypriv = &padapter->registrypriv;
1047 switch (pxmitpriv->vcs_setting) {
1049 pxmitpriv->vcs = NONE_VCS;
1055 perp = rtw_get_ie(ie, _ERPINFO_IE_, &erp_len, ie_len);
1057 pxmitpriv->vcs = NONE_VCS;
1059 protection = (*(perp + 2)) & BIT(1);
1061 if (pregistrypriv->vcs_type == RTS_CTS)
1062 pxmitpriv->vcs = RTS_CTS;
1064 pxmitpriv->vcs = CTS_TO_SELF;
1066 pxmitpriv->vcs = NONE_VCS;
1074 void rtw_count_tx_stats(struct adapter *padapter, struct xmit_frame *pxmitframe, int sz)
1076 struct sta_info *psta = NULL;
1077 struct stainfo_stats *pstats = NULL;
1078 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1079 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1081 if ((pxmitframe->frame_tag & 0x0f) == DATA_FRAMETAG) {
1082 pxmitpriv->tx_bytes += sz;
1083 pmlmepriv->LinkDetectInfo.NumTxOkInPeriod += pxmitframe->agg_num;
1085 psta = pxmitframe->attrib.psta;
1087 pstats = &psta->sta_stats;
1088 pstats->tx_pkts += pxmitframe->agg_num;
1089 pstats->tx_bytes += sz;
1094 struct xmit_buf *rtw_alloc_xmitbuf_ext(struct xmit_priv *pxmitpriv)
1096 struct xmit_buf *pxmitbuf = NULL;
1097 struct list_head *plist, *phead;
1098 struct __queue *pfree_queue = &pxmitpriv->free_xmit_extbuf_queue;
1099 unsigned long flags;
1101 spin_lock_irqsave(&pfree_queue->lock, flags);
1103 if (list_empty(&pfree_queue->queue)) {
1106 phead = get_list_head(pfree_queue);
1108 plist = phead->next;
1110 pxmitbuf = container_of(plist, struct xmit_buf, list);
1112 list_del_init(&pxmitbuf->list);
1116 pxmitpriv->free_xmit_extbuf_cnt--;
1118 pxmitbuf->priv_data = NULL;
1119 /* pxmitbuf->ext_tag = true; */
1122 rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_ALLOC);
1125 spin_unlock_irqrestore(&pfree_queue->lock, flags);
1130 s32 rtw_free_xmitbuf_ext(struct xmit_priv *pxmitpriv, struct xmit_buf *pxmitbuf)
1132 struct __queue *pfree_queue = &pxmitpriv->free_xmit_extbuf_queue;
1133 unsigned long flags;
1138 spin_lock_irqsave(&pfree_queue->lock, flags);
1140 list_del_init(&pxmitbuf->list);
1142 list_add_tail(&pxmitbuf->list, get_list_head(pfree_queue));
1143 pxmitpriv->free_xmit_extbuf_cnt++;
1145 spin_unlock_irqrestore(&pfree_queue->lock, flags);
1150 struct xmit_buf *rtw_alloc_xmitbuf(struct xmit_priv *pxmitpriv)
1152 struct xmit_buf *pxmitbuf = NULL;
1153 struct list_head *plist, *phead;
1154 struct __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
1155 unsigned long flags;
1157 spin_lock_irqsave(&pfree_xmitbuf_queue->lock, flags);
1159 if (list_empty(&pfree_xmitbuf_queue->queue)) {
1162 phead = get_list_head(pfree_xmitbuf_queue);
1164 plist = phead->next;
1166 pxmitbuf = container_of(plist, struct xmit_buf, list);
1168 list_del_init(&pxmitbuf->list);
1172 pxmitpriv->free_xmitbuf_cnt--;
1173 pxmitbuf->priv_data = NULL;
1175 rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_ALLOC);
1177 spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, flags);
1182 s32 rtw_free_xmitbuf(struct xmit_priv *pxmitpriv, struct xmit_buf *pxmitbuf)
1184 struct __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
1185 unsigned long flags;
1191 rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_FREE);
1193 if (pxmitbuf->ext_tag) {
1194 rtw_free_xmitbuf_ext(pxmitpriv, pxmitbuf);
1196 spin_lock_irqsave(&pfree_xmitbuf_queue->lock, flags);
1198 list_del_init(&pxmitbuf->list);
1200 list_add_tail(&pxmitbuf->list, get_list_head(pfree_xmitbuf_queue));
1202 pxmitpriv->free_xmitbuf_cnt++;
1203 spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, flags);
1212 * 2. RXENTRY (rx_thread or RX_ISR/RX_CallBack)
1214 * If we turn on USE_RXTHREAD, then, no need for critical section.
1215 * Otherwise, we must use _enter/_exit critical to protect free_xmit_queue...
1217 * Must be very very cautious...
1219 struct xmit_frame *rtw_alloc_xmitframe(struct xmit_priv *pxmitpriv)/* _queue *pfree_xmit_queue) */
1222 * Please remember to use all the osdep_service api,
1223 * and lock/unlock or _enter/_exit critical to protect
1227 struct xmit_frame *pxframe = NULL;
1228 struct list_head *plist, *phead;
1229 struct __queue *pfree_xmit_queue = &pxmitpriv->free_xmit_queue;
1231 spin_lock_bh(&pfree_xmit_queue->lock);
1233 if (list_empty(&pfree_xmit_queue->queue)) {
1236 phead = get_list_head(pfree_xmit_queue);
1238 plist = phead->next;
1240 pxframe = container_of(plist, struct xmit_frame, list);
1242 list_del_init(&pxframe->list);
1245 if (pxframe) { /* default value setting */
1246 pxmitpriv->free_xmitframe_cnt--;
1248 pxframe->buf_addr = NULL;
1249 pxframe->pxmitbuf = NULL;
1251 memset(&pxframe->attrib, 0, sizeof(struct pkt_attrib));
1252 /* pxframe->attrib.psta = NULL; */
1254 pxframe->frame_tag = DATA_FRAMETAG;
1256 pxframe->pkt = NULL;
1257 pxframe->pkt_offset = 1;/* default use pkt_offset to fill tx desc */
1259 pxframe->agg_num = 1;
1260 pxframe->ack_report = 0;
1263 spin_unlock_bh(&pfree_xmit_queue->lock);
1268 s32 rtw_free_xmitframe(struct xmit_priv *pxmitpriv, struct xmit_frame *pxmitframe)
1270 struct __queue *pfree_xmit_queue = &pxmitpriv->free_xmit_queue;
1271 struct adapter *padapter = pxmitpriv->adapter;
1272 struct sk_buff *pndis_pkt = NULL;
1277 spin_lock_bh(&pfree_xmit_queue->lock);
1279 list_del_init(&pxmitframe->list);
1281 if (pxmitframe->pkt) {
1282 pndis_pkt = pxmitframe->pkt;
1283 pxmitframe->pkt = NULL;
1286 list_add_tail(&pxmitframe->list, get_list_head(pfree_xmit_queue));
1288 pxmitpriv->free_xmitframe_cnt++;
1290 spin_unlock_bh(&pfree_xmit_queue->lock);
1293 rtw_os_pkt_complete(padapter, pndis_pkt);
1300 void rtw_free_xmitframe_queue(struct xmit_priv *pxmitpriv, struct __queue *pframequeue)
1302 struct list_head *plist, *phead;
1303 struct xmit_frame *pxmitframe;
1305 spin_lock_bh(&pframequeue->lock);
1307 phead = get_list_head(pframequeue);
1308 plist = phead->next;
1310 while (phead != plist) {
1311 pxmitframe = container_of(plist, struct xmit_frame, list);
1313 plist = plist->next;
1315 rtw_free_xmitframe(pxmitpriv, pxmitframe);
1317 spin_unlock_bh(&pframequeue->lock);
1321 s32 rtw_xmitframe_enqueue(struct adapter *padapter, struct xmit_frame *pxmitframe)
1323 if (rtw_xmit_classifier(padapter, pxmitframe) == _FAIL) {
1324 /* pxmitframe->pkt = NULL; */
1331 static struct xmit_frame *dequeue_one_xmitframe(struct xmit_priv *pxmitpriv, struct hw_xmit *phwxmit, struct tx_servq *ptxservq, struct __queue *pframe_queue)
1333 struct list_head *xmitframe_plist, *xmitframe_phead;
1334 struct xmit_frame *pxmitframe = NULL;
1336 xmitframe_phead = get_list_head(pframe_queue);
1337 xmitframe_plist = xmitframe_phead->next;
1339 if (xmitframe_phead != xmitframe_plist) {
1340 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
1342 xmitframe_plist = xmitframe_plist->next;
1344 list_del_init(&pxmitframe->list);
1351 struct xmit_frame *rtw_dequeue_xframe(struct xmit_priv *pxmitpriv, struct hw_xmit *phwxmit_i, int entry)
1353 struct list_head *sta_plist, *sta_phead;
1354 struct hw_xmit *phwxmit;
1355 struct tx_servq *ptxservq = NULL;
1356 struct __queue *pframe_queue = NULL;
1357 struct xmit_frame *pxmitframe = NULL;
1358 struct adapter *padapter = pxmitpriv->adapter;
1359 struct registry_priv *pregpriv = &padapter->registrypriv;
1362 inx[0] = 0; inx[1] = 1; inx[2] = 2; inx[3] = 3;
1364 if (pregpriv->wifi_spec == 1) {
1367 for (j = 0; j < 4; j++)
1368 inx[j] = pxmitpriv->wmm_para_seq[j];
1371 spin_lock_bh(&pxmitpriv->lock);
1373 for (i = 0; i < entry; i++) {
1374 phwxmit = phwxmit_i + inx[i];
1376 sta_phead = get_list_head(phwxmit->sta_queue);
1377 sta_plist = sta_phead->next;
1379 while (sta_phead != sta_plist) {
1380 ptxservq = container_of(sta_plist, struct tx_servq, tx_pending);
1382 pframe_queue = &ptxservq->sta_pending;
1384 pxmitframe = dequeue_one_xmitframe(pxmitpriv, phwxmit, ptxservq, pframe_queue);
1389 /* Remove sta node when there are no pending packets. */
1390 if (list_empty(&pframe_queue->queue)) /* must be done after get_next and before break */
1391 list_del_init(&ptxservq->tx_pending);
1395 sta_plist = sta_plist->next;
1399 spin_unlock_bh(&pxmitpriv->lock);
1404 struct tx_servq *rtw_get_sta_pending(struct adapter *padapter, struct sta_info *psta, int up, u8 *ac)
1406 struct tx_servq *ptxservq;
1411 ptxservq = &psta->sta_xmitpriv.bk_q;
1416 ptxservq = &psta->sta_xmitpriv.vi_q;
1421 ptxservq = &psta->sta_xmitpriv.vo_q;
1427 ptxservq = &psta->sta_xmitpriv.be_q;
1436 * Will enqueue pxmitframe to the proper queue,
1437 * and indicate it to xx_pending list.....
1439 s32 rtw_xmit_classifier(struct adapter *padapter, struct xmit_frame *pxmitframe)
1442 struct sta_info *psta;
1443 struct tx_servq *ptxservq;
1444 struct pkt_attrib *pattrib = &pxmitframe->attrib;
1445 struct sta_priv *pstapriv = &padapter->stapriv;
1446 struct hw_xmit *phwxmits = padapter->xmitpriv.hwxmits;
1450 psta = pattrib->psta;
1452 psta = rtw_get_stainfo(pstapriv, pattrib->ra);
1459 ptxservq = rtw_get_sta_pending(padapter, psta, pattrib->priority, (u8 *)(&ac_index));
1461 if (list_empty(&ptxservq->tx_pending))
1462 list_add_tail(&ptxservq->tx_pending, get_list_head(phwxmits[ac_index].sta_queue));
1464 list_add_tail(&pxmitframe->list, get_list_head(&ptxservq->sta_pending));
1466 phwxmits[ac_index].accnt++;
1472 int rtw_alloc_hwxmits(struct adapter *padapter)
1474 struct hw_xmit *hwxmits;
1475 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1477 pxmitpriv->hwxmit_entry = HWXMIT_ENTRY;
1479 pxmitpriv->hwxmits = kzalloc(sizeof(struct hw_xmit) * pxmitpriv->hwxmit_entry, GFP_KERNEL);
1480 if (!pxmitpriv->hwxmits)
1483 hwxmits = pxmitpriv->hwxmits;
1485 hwxmits[0].sta_queue = &pxmitpriv->vo_pending;
1486 hwxmits[1].sta_queue = &pxmitpriv->vi_pending;
1487 hwxmits[2].sta_queue = &pxmitpriv->be_pending;
1488 hwxmits[3].sta_queue = &pxmitpriv->bk_pending;
1493 void rtw_free_hwxmits(struct adapter *padapter)
1495 struct hw_xmit *hwxmits;
1496 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1498 hwxmits = pxmitpriv->hwxmits;
1502 void rtw_init_hwxmits(struct hw_xmit *phwxmit, int entry)
1506 for (i = 0; i < entry; i++, phwxmit++)
1511 static int rtw_br_client_tx(struct adapter *padapter, struct sk_buff **pskb)
1513 struct sk_buff *skb = *pskb;
1514 int res, is_vlan_tag = 0, i, do_nat25 = 1;
1515 unsigned short vlan_hdr = 0;
1516 void *br_port = NULL;
1519 br_port = rcu_dereference(padapter->pnetdev->rx_handler_data);
1521 spin_lock_bh(&padapter->br_ext_lock);
1522 if (!(skb->data[0] & 1) && br_port &&
1523 memcmp(skb->data + ETH_ALEN, padapter->br_mac, ETH_ALEN) &&
1524 *((__be16 *)(skb->data + ETH_ALEN * 2)) != __constant_htons(ETH_P_8021Q) &&
1525 *((__be16 *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_IP) &&
1526 !memcmp(padapter->scdb_mac, skb->data + ETH_ALEN, ETH_ALEN) && padapter->scdb_entry) {
1527 memcpy(skb->data + ETH_ALEN, GET_MY_HWADDR(padapter), ETH_ALEN);
1528 padapter->scdb_entry->ageing_timer = jiffies;
1529 spin_unlock_bh(&padapter->br_ext_lock);
1531 if (*((__be16 *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_8021Q)) {
1533 vlan_hdr = *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2));
1534 for (i = 0; i < 6; i++)
1535 *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2 - i * 2)) = *((unsigned short *)(skb->data + ETH_ALEN * 2 - 2 - i * 2));
1538 if (!memcmp(skb->data + ETH_ALEN, padapter->br_mac, ETH_ALEN) &&
1539 (*((__be16 *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_IP)))
1540 memcpy(padapter->br_ip, skb->data + WLAN_ETHHDR_LEN + 12, 4);
1542 if (*((__be16 *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_IP)) {
1543 if (memcmp(padapter->scdb_mac, skb->data + ETH_ALEN, ETH_ALEN)) {
1544 padapter->scdb_entry = (struct nat25_network_db_entry *)scdb_findEntry(padapter,
1545 skb->data + WLAN_ETHHDR_LEN + 12);
1546 if (padapter->scdb_entry) {
1547 memcpy(padapter->scdb_mac, skb->data + ETH_ALEN, ETH_ALEN);
1548 memcpy(padapter->scdb_ip, skb->data + WLAN_ETHHDR_LEN + 12, 4);
1549 padapter->scdb_entry->ageing_timer = jiffies;
1553 if (padapter->scdb_entry) {
1554 padapter->scdb_entry->ageing_timer = jiffies;
1557 memset(padapter->scdb_mac, 0, ETH_ALEN);
1558 memset(padapter->scdb_ip, 0, 4);
1562 spin_unlock_bh(&padapter->br_ext_lock);
1564 if (nat25_db_handle(padapter, skb, NAT25_CHECK) == 0) {
1565 struct sk_buff *newskb;
1569 for (i = 0; i < 6; i++)
1570 *((unsigned short *)(skb->data + i * 2)) = *((unsigned short *)(skb->data + 4 + i * 2));
1571 *((__be16 *)(skb->data + ETH_ALEN * 2)) = __constant_htons(ETH_P_8021Q);
1572 *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2)) = vlan_hdr;
1575 newskb = skb_copy(skb, GFP_ATOMIC);
1578 dev_kfree_skb_any(skb);
1580 *pskb = skb = newskb;
1582 vlan_hdr = *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2));
1583 for (i = 0; i < 6; i++)
1584 *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2 - i * 2)) = *((unsigned short *)(skb->data + ETH_ALEN * 2 - 2 - i * 2));
1589 res = skb_linearize(skb);
1593 res = nat25_db_handle(padapter, skb, NAT25_INSERT);
1602 memcpy(skb->data + ETH_ALEN, GET_MY_HWADDR(padapter), ETH_ALEN);
1604 dhcp_flag_bcast(padapter, skb);
1608 for (i = 0; i < 6; i++)
1609 *((unsigned short *)(skb->data + i * 2)) = *((unsigned short *)(skb->data + 4 + i * 2));
1610 *((__be16 *)(skb->data + ETH_ALEN * 2)) = __constant_htons(ETH_P_8021Q);
1611 *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2)) = vlan_hdr;
1615 /* check if SA is equal to our MAC */
1616 if (memcmp(skb->data + ETH_ALEN, GET_MY_HWADDR(padapter), ETH_ALEN))
1622 u32 rtw_get_ff_hwaddr(struct xmit_frame *pxmitframe)
1625 struct pkt_attrib *pattrib = &pxmitframe->attrib;
1627 switch (pattrib->qsel) {
1630 addr = BE_QUEUE_INX;
1634 addr = BK_QUEUE_INX;
1638 addr = VI_QUEUE_INX;
1642 addr = VO_QUEUE_INX;
1645 addr = BCN_QUEUE_INX;
1647 case 0x11:/* BC/MC in PS (HIQ) */
1648 addr = HIGH_QUEUE_INX;
1652 addr = MGT_QUEUE_INX;
1659 static void do_queue_select(struct adapter *padapter, struct pkt_attrib *pattrib)
1663 qsel = pattrib->priority;
1665 pattrib->qsel = qsel;
1669 * The main transmit(tx) entry
1673 * 0 success, hardware will handle this xmit frame(packet)
1676 s32 rtw_xmit(struct adapter *padapter, struct sk_buff **ppkt)
1678 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1679 struct xmit_frame *pxmitframe = NULL;
1680 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1681 void *br_port = NULL;
1684 pxmitframe = rtw_alloc_xmitframe(pxmitpriv);
1689 br_port = rcu_dereference(padapter->pnetdev->rx_handler_data);
1692 if (br_port && check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_ADHOC_STATE)) {
1693 res = rtw_br_client_tx(padapter, ppkt);
1695 rtw_free_xmitframe(pxmitpriv, pxmitframe);
1700 res = update_attrib(padapter, *ppkt, &pxmitframe->attrib);
1703 rtw_free_xmitframe(pxmitpriv, pxmitframe);
1706 pxmitframe->pkt = *ppkt;
1708 rtw_led_control(padapter, LED_CTL_TX);
1710 do_queue_select(padapter, &pxmitframe->attrib);
1712 spin_lock_bh(&pxmitpriv->lock);
1713 if (xmitframe_enqueue_for_sleeping_sta(padapter, pxmitframe)) {
1714 spin_unlock_bh(&pxmitpriv->lock);
1717 spin_unlock_bh(&pxmitpriv->lock);
1719 if (!rtl8188eu_hal_xmit(padapter, pxmitframe))
1725 int xmitframe_enqueue_for_sleeping_sta(struct adapter *padapter, struct xmit_frame *pxmitframe)
1728 struct sta_info *psta = NULL;
1729 struct sta_priv *pstapriv = &padapter->stapriv;
1730 struct pkt_attrib *pattrib = &pxmitframe->attrib;
1731 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1732 bool bmcst = is_multicast_ether_addr(pattrib->ra);
1734 if (!check_fwstate(pmlmepriv, WIFI_AP_STATE))
1738 psta = pattrib->psta;
1740 psta = rtw_get_stainfo(pstapriv, pattrib->ra);
1745 if (pattrib->triggered == 1) {
1747 pattrib->qsel = 0x11;/* HIQ */
1752 spin_lock_bh(&psta->sleep_q.lock);
1754 if (pstapriv->sta_dz_bitmap) {/* if any one sta is in ps mode */
1755 list_del_init(&pxmitframe->list);
1757 list_add_tail(&pxmitframe->list, get_list_head(&psta->sleep_q));
1761 pstapriv->tim_bitmap |= BIT(0);/* */
1762 pstapriv->sta_dz_bitmap |= BIT(0);
1764 update_beacon(padapter, _TIM_IE_, NULL, false);/* tx bc/mc packets after upate bcn */
1769 spin_unlock_bh(&psta->sleep_q.lock);
1774 spin_lock_bh(&psta->sleep_q.lock);
1776 if (psta->state & WIFI_SLEEP_STATE) {
1779 if (pstapriv->sta_dz_bitmap & BIT(psta->aid)) {
1780 list_del_init(&pxmitframe->list);
1782 list_add_tail(&pxmitframe->list, get_list_head(&psta->sleep_q));
1786 switch (pattrib->priority) {
1789 wmmps_ac = psta->uapsd_bk & BIT(0);
1793 wmmps_ac = psta->uapsd_vi & BIT(0);
1797 wmmps_ac = psta->uapsd_vo & BIT(0);
1802 wmmps_ac = psta->uapsd_be & BIT(0);
1807 psta->sleepq_ac_len++;
1809 if (((psta->has_legacy_ac) && (!wmmps_ac)) ||
1810 ((!psta->has_legacy_ac) && (wmmps_ac))) {
1811 pstapriv->tim_bitmap |= BIT(psta->aid);
1813 if (psta->sleepq_len == 1) {
1814 /* upate BCN for TIM IE */
1815 update_beacon(padapter, _TIM_IE_, NULL, false);
1822 spin_unlock_bh(&psta->sleep_q.lock);
1827 static void dequeue_xmitframes_to_sleeping_queue(struct adapter *padapter, struct sta_info *psta, struct __queue *pframequeue)
1829 struct list_head *plist, *phead;
1831 struct tx_servq *ptxservq;
1832 struct pkt_attrib *pattrib;
1833 struct xmit_frame *pxmitframe;
1834 struct hw_xmit *phwxmits = padapter->xmitpriv.hwxmits;
1836 phead = get_list_head(pframequeue);
1837 plist = phead->next;
1839 while (phead != plist) {
1840 pxmitframe = container_of(plist, struct xmit_frame, list);
1842 plist = plist->next;
1844 xmitframe_enqueue_for_sleeping_sta(padapter, pxmitframe);
1846 pattrib = &pxmitframe->attrib;
1848 ptxservq = rtw_get_sta_pending(padapter, psta, pattrib->priority, (u8 *)(&ac_index));
1851 phwxmits[ac_index].accnt--;
1855 void stop_sta_xmit(struct adapter *padapter, struct sta_info *psta)
1857 struct sta_info *psta_bmc;
1858 struct sta_xmit_priv *pstaxmitpriv;
1859 struct sta_priv *pstapriv = &padapter->stapriv;
1860 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1862 pstaxmitpriv = &psta->sta_xmitpriv;
1864 /* for BC/MC Frames */
1865 psta_bmc = rtw_get_bcmc_stainfo(padapter);
1867 spin_lock_bh(&pxmitpriv->lock);
1869 psta->state |= WIFI_SLEEP_STATE;
1871 pstapriv->sta_dz_bitmap |= BIT(psta->aid);
1873 dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->vo_q.sta_pending);
1874 list_del_init(&pstaxmitpriv->vo_q.tx_pending);
1876 dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->vi_q.sta_pending);
1877 list_del_init(&pstaxmitpriv->vi_q.tx_pending);
1879 dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->be_q.sta_pending);
1880 list_del_init(&pstaxmitpriv->be_q.tx_pending);
1882 dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->bk_q.sta_pending);
1883 list_del_init(&pstaxmitpriv->bk_q.tx_pending);
1885 /* for BC/MC Frames */
1886 pstaxmitpriv = &psta_bmc->sta_xmitpriv;
1887 dequeue_xmitframes_to_sleeping_queue(padapter, psta_bmc, &pstaxmitpriv->be_q.sta_pending);
1888 list_del_init(&pstaxmitpriv->be_q.tx_pending);
1890 spin_unlock_bh(&pxmitpriv->lock);
1893 void wakeup_sta_to_xmit(struct adapter *padapter, struct sta_info *psta)
1895 u8 update_mask = 0, wmmps_ac = 0;
1896 struct sta_info *psta_bmc;
1897 struct list_head *xmitframe_plist, *xmitframe_phead;
1898 struct xmit_frame *pxmitframe = NULL;
1899 struct sta_priv *pstapriv = &padapter->stapriv;
1901 spin_lock_bh(&psta->sleep_q.lock);
1903 xmitframe_phead = get_list_head(&psta->sleep_q);
1904 xmitframe_plist = xmitframe_phead->next;
1906 while (xmitframe_phead != xmitframe_plist) {
1907 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
1909 xmitframe_plist = xmitframe_plist->next;
1911 list_del_init(&pxmitframe->list);
1913 switch (pxmitframe->attrib.priority) {
1916 wmmps_ac = psta->uapsd_bk & BIT(1);
1920 wmmps_ac = psta->uapsd_vi & BIT(1);
1924 wmmps_ac = psta->uapsd_vo & BIT(1);
1929 wmmps_ac = psta->uapsd_be & BIT(1);
1934 if (psta->sleepq_len > 0)
1935 pxmitframe->attrib.mdata = 1;
1937 pxmitframe->attrib.mdata = 0;
1940 psta->sleepq_ac_len--;
1941 if (psta->sleepq_ac_len > 0) {
1942 pxmitframe->attrib.mdata = 1;
1943 pxmitframe->attrib.eosp = 0;
1945 pxmitframe->attrib.mdata = 0;
1946 pxmitframe->attrib.eosp = 1;
1950 pxmitframe->attrib.triggered = 1;
1952 spin_unlock_bh(&psta->sleep_q.lock);
1953 if (rtl8188eu_hal_xmit(padapter, pxmitframe))
1954 rtw_os_xmit_complete(padapter, pxmitframe);
1955 spin_lock_bh(&psta->sleep_q.lock);
1958 if (psta->sleepq_len == 0) {
1959 pstapriv->tim_bitmap &= ~BIT(psta->aid);
1961 update_mask = BIT(0);
1963 if (psta->state & WIFI_SLEEP_STATE)
1964 psta->state ^= WIFI_SLEEP_STATE;
1966 if (psta->state & WIFI_STA_ALIVE_CHK_STATE) {
1967 psta->expire_to = pstapriv->expire_to;
1968 psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
1971 pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
1974 spin_unlock_bh(&psta->sleep_q.lock);
1976 /* for BC/MC Frames */
1977 psta_bmc = rtw_get_bcmc_stainfo(padapter);
1981 if ((pstapriv->sta_dz_bitmap & 0xfffe) == 0x0) { /* no any sta in ps mode */
1982 spin_lock_bh(&psta_bmc->sleep_q.lock);
1984 xmitframe_phead = get_list_head(&psta_bmc->sleep_q);
1985 xmitframe_plist = xmitframe_phead->next;
1987 while (xmitframe_phead != xmitframe_plist) {
1988 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
1990 xmitframe_plist = xmitframe_plist->next;
1992 list_del_init(&pxmitframe->list);
1994 psta_bmc->sleepq_len--;
1995 if (psta_bmc->sleepq_len > 0)
1996 pxmitframe->attrib.mdata = 1;
1998 pxmitframe->attrib.mdata = 0;
2000 pxmitframe->attrib.triggered = 1;
2002 spin_unlock_bh(&psta_bmc->sleep_q.lock);
2003 if (rtl8188eu_hal_xmit(padapter, pxmitframe))
2004 rtw_os_xmit_complete(padapter, pxmitframe);
2005 spin_lock_bh(&psta_bmc->sleep_q.lock);
2008 if (psta_bmc->sleepq_len == 0) {
2009 pstapriv->tim_bitmap &= ~BIT(0);
2010 pstapriv->sta_dz_bitmap &= ~BIT(0);
2012 update_mask |= BIT(1);
2015 spin_unlock_bh(&psta_bmc->sleep_q.lock);
2019 update_beacon(padapter, _TIM_IE_, NULL, false);
2022 void xmit_delivery_enabled_frames(struct adapter *padapter, struct sta_info *psta)
2025 struct list_head *xmitframe_plist, *xmitframe_phead;
2026 struct xmit_frame *pxmitframe = NULL;
2027 struct sta_priv *pstapriv = &padapter->stapriv;
2029 spin_lock_bh(&psta->sleep_q.lock);
2031 xmitframe_phead = get_list_head(&psta->sleep_q);
2032 xmitframe_plist = xmitframe_phead->next;
2034 while (xmitframe_phead != xmitframe_plist) {
2035 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
2037 xmitframe_plist = xmitframe_plist->next;
2039 switch (pxmitframe->attrib.priority) {
2042 wmmps_ac = psta->uapsd_bk & BIT(1);
2046 wmmps_ac = psta->uapsd_vi & BIT(1);
2050 wmmps_ac = psta->uapsd_vo & BIT(1);
2055 wmmps_ac = psta->uapsd_be & BIT(1);
2062 list_del_init(&pxmitframe->list);
2065 psta->sleepq_ac_len--;
2067 if (psta->sleepq_ac_len > 0) {
2068 pxmitframe->attrib.mdata = 1;
2069 pxmitframe->attrib.eosp = 0;
2071 pxmitframe->attrib.mdata = 0;
2072 pxmitframe->attrib.eosp = 1;
2075 pxmitframe->attrib.triggered = 1;
2077 if (rtl8188eu_hal_xmit(padapter, pxmitframe))
2078 rtw_os_xmit_complete(padapter, pxmitframe);
2080 if ((psta->sleepq_ac_len == 0) && (!psta->has_legacy_ac) && (wmmps_ac)) {
2081 pstapriv->tim_bitmap &= ~BIT(psta->aid);
2083 /* upate BCN for TIM IE */
2084 update_beacon(padapter, _TIM_IE_, NULL, false);
2088 spin_unlock_bh(&psta->sleep_q.lock);
2091 void rtw_sctx_init(struct submit_ctx *sctx, int timeout_ms)
2093 sctx->timeout_ms = timeout_ms;
2094 sctx->submit_time = jiffies;
2095 init_completion(&sctx->done);
2096 sctx->status = RTW_SCTX_SUBMITTED;
2099 int rtw_sctx_wait(struct submit_ctx *sctx)
2102 unsigned long expire;
2105 expire = sctx->timeout_ms ? msecs_to_jiffies(sctx->timeout_ms) : MAX_SCHEDULE_TIMEOUT;
2106 if (!wait_for_completion_timeout(&sctx->done, expire))
2107 /* timeout, do something?? */
2108 status = RTW_SCTX_DONE_TIMEOUT;
2110 status = sctx->status;
2112 if (status == RTW_SCTX_DONE_SUCCESS)
2118 void rtw_sctx_done_err(struct submit_ctx **sctx, int status)
2121 (*sctx)->status = status;
2122 complete(&((*sctx)->done));
2127 int rtw_ack_tx_wait(struct xmit_priv *pxmitpriv, u32 timeout_ms)
2129 struct submit_ctx *pack_tx_ops = &pxmitpriv->ack_tx_ops;
2131 pack_tx_ops->submit_time = jiffies;
2132 pack_tx_ops->timeout_ms = timeout_ms;
2133 pack_tx_ops->status = RTW_SCTX_SUBMITTED;
2135 return rtw_sctx_wait(pack_tx_ops);
2138 void rtw_ack_tx_done(struct xmit_priv *pxmitpriv, int status)
2140 struct submit_ctx *pack_tx_ops = &pxmitpriv->ack_tx_ops;
2142 if (pxmitpriv->ack_tx)
2143 rtw_sctx_done_err(&pack_tx_ops, status);