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 res = rtw_alloc_hwxmits(padapter);
187 rtw_init_hwxmits(pxmitpriv->hwxmits, pxmitpriv->hwxmit_entry);
189 for (i = 0; i < 4; i++)
190 pxmitpriv->wmm_para_seq[i] = i;
192 pxmitpriv->txirp_cnt = 1;
194 sema_init(&pxmitpriv->tx_retevt, 0);
196 /* per AC pending irp */
197 pxmitpriv->beq_cnt = 0;
198 pxmitpriv->bkq_cnt = 0;
199 pxmitpriv->viq_cnt = 0;
200 pxmitpriv->voq_cnt = 0;
202 pxmitpriv->ack_tx = false;
203 mutex_init(&pxmitpriv->ack_tx_mutex);
204 rtw_sctx_init(&pxmitpriv->ack_tx_ops, 0);
206 rtl8188eu_init_xmit_priv(padapter);
213 void _rtw_free_xmit_priv(struct xmit_priv *pxmitpriv)
216 struct adapter *padapter = pxmitpriv->adapter;
217 struct xmit_frame *pxmitframe = (struct xmit_frame *)pxmitpriv->pxmit_frame_buf;
218 struct xmit_buf *pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
219 u32 max_xmit_extbuf_size = MAX_XMIT_EXTBUF_SZ;
220 u32 num_xmit_extbuf = NR_XMIT_EXTBUFF;
222 if (!pxmitpriv->pxmit_frame_buf)
225 for (i = 0; i < NR_XMITFRAME; i++) {
226 rtw_os_xmit_complete(padapter, pxmitframe);
231 for (i = 0; i < NR_XMITBUFF; i++) {
232 rtw_os_xmit_resource_free(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ));
236 vfree(pxmitpriv->pallocated_frame_buf);
238 vfree(pxmitpriv->pallocated_xmitbuf);
240 pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmit_extbuf;
241 for (i = 0; i < num_xmit_extbuf; i++) {
242 rtw_os_xmit_resource_free(padapter, pxmitbuf, (max_xmit_extbuf_size + XMITBUF_ALIGN_SZ));
246 vfree(pxmitpriv->pallocated_xmit_extbuf);
248 rtw_free_hwxmits(padapter);
250 mutex_destroy(&pxmitpriv->ack_tx_mutex);
253 static void update_attrib_vcs_info(struct adapter *padapter, struct xmit_frame *pxmitframe)
256 struct pkt_attrib *pattrib = &pxmitframe->attrib;
257 struct sta_info *psta = pattrib->psta;
258 struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
259 struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info;
261 if (pattrib->nr_frags != 1)
262 sz = padapter->xmitpriv.frag_len;
264 sz = pattrib->last_txcmdsz;
266 /* (1) RTS_Threshold is compared to the MPDU, not MSDU. */
267 /* (2) If there are more than one frag in this MSDU, only the first frag uses protection frame. */
268 /* Other fragments are protected by previous fragment. */
269 /* So we only need to check the length of first fragment. */
270 if (pmlmeext->cur_wireless_mode < WIRELESS_11_24N || padapter->registrypriv.wifi_spec) {
271 if (sz > padapter->registrypriv.rts_thresh) {
272 pattrib->vcs_mode = RTS_CTS;
275 pattrib->vcs_mode = RTS_CTS;
276 else if (psta->cts2self)
277 pattrib->vcs_mode = CTS_TO_SELF;
279 pattrib->vcs_mode = NONE_VCS;
284 if ((pmlmeinfo->assoc_AP_vendor == HT_IOT_PEER_ATHEROS) && pattrib->ampdu_en &&
285 (padapter->securitypriv.dot11PrivacyAlgrthm == _AES_)) {
286 pattrib->vcs_mode = CTS_TO_SELF;
290 /* check ERP protection */
291 if (psta->rtsen || psta->cts2self) {
293 pattrib->vcs_mode = RTS_CTS;
294 else if (psta->cts2self)
295 pattrib->vcs_mode = CTS_TO_SELF;
300 /* check HT op mode */
301 if (pattrib->ht_en) {
302 u8 htopmode = pmlmeinfo->HT_protection;
303 if ((pmlmeext->cur_bwmode && (htopmode == 2 || htopmode == 3)) ||
304 (!pmlmeext->cur_bwmode && htopmode == 3)) {
305 pattrib->vcs_mode = RTS_CTS;
311 if (sz > padapter->registrypriv.rts_thresh) {
312 pattrib->vcs_mode = RTS_CTS;
316 /* to do list: check MIMO power save condition. */
318 /* check AMPDU aggregation for TXOP */
319 if (pattrib->ampdu_en) {
320 pattrib->vcs_mode = RTS_CTS;
324 pattrib->vcs_mode = NONE_VCS;
330 static void update_attrib_phy_info(struct pkt_attrib *pattrib, struct sta_info *psta)
333 pattrib->vcs_mode = RTS_CTS;
334 else if (psta->cts2self)
335 pattrib->vcs_mode = CTS_TO_SELF;
337 pattrib->vcs_mode = NONE_VCS;*/
341 pattrib->triggered = 0;
343 /* qos_en, ht_en, init rate, , bw, ch_offset, sgi */
344 pattrib->qos_en = psta->qos_option;
346 pattrib->raid = psta->raid;
347 pattrib->ht_en = psta->htpriv.ht_option;
348 pattrib->bwmode = psta->htpriv.bwmode;
349 pattrib->ch_offset = psta->htpriv.ch_offset;
350 pattrib->sgi = psta->htpriv.sgi;
351 pattrib->ampdu_en = false;
352 pattrib->retry_ctrl = false;
355 u8 qos_acm(u8 acm_mask, u8 priority)
357 u8 change_priority = priority;
362 if (acm_mask & BIT(1))
370 if (acm_mask & BIT(2))
375 if (acm_mask & BIT(3))
382 return change_priority;
385 static void set_qos(struct pkt_file *ppktfile, struct pkt_attrib *pattrib)
387 struct ethhdr etherhdr;
391 _rtw_open_pktfile(ppktfile->pkt, ppktfile);
392 _rtw_pktfile_read(ppktfile, (unsigned char *)ðerhdr, ETH_HLEN);
394 /* get user_prio from IP hdr */
395 if (pattrib->ether_type == 0x0800) {
396 _rtw_pktfile_read(ppktfile, (u8 *)&ip_hdr, sizeof(ip_hdr));
397 /* user_prio = (ntohs(ip_hdr.tos) >> 5) & 0x3; */
398 user_prio = ip_hdr.tos >> 5;
399 } else if (pattrib->ether_type == 0x888e) {
400 /* "When priority processing of data frames is supported, */
401 /* a STA's SME should send EAPOL-Key frames at the highest priority." */
405 pattrib->priority = user_prio;
406 pattrib->hdrlen = WLAN_HDR_A3_QOS_LEN;
407 pattrib->subtype = IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA;
410 static s32 update_attrib(struct adapter *padapter, struct sk_buff *pkt, struct pkt_attrib *pattrib)
412 struct pkt_file pktfile;
413 struct sta_info *psta = NULL;
414 struct ethhdr etherhdr;
417 struct sta_priv *pstapriv = &padapter->stapriv;
418 struct security_priv *psecuritypriv = &padapter->securitypriv;
419 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
420 struct qos_priv *pqospriv = &pmlmepriv->qospriv;
425 _rtw_open_pktfile(pkt, &pktfile);
426 _rtw_pktfile_read(&pktfile, (u8 *)ðerhdr, ETH_HLEN);
428 pattrib->ether_type = ntohs(etherhdr.h_proto);
430 memcpy(pattrib->dst, ðerhdr.h_dest, ETH_ALEN);
431 memcpy(pattrib->src, ðerhdr.h_source, ETH_ALEN);
435 if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) ||
436 check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
437 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
438 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
439 } else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
440 memcpy(pattrib->ra, get_bssid(pmlmepriv), ETH_ALEN);
441 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
442 } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
443 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
444 memcpy(pattrib->ta, get_bssid(pmlmepriv), ETH_ALEN);
447 pattrib->pktlen = pktfile.pkt_len;
449 if (ETH_P_IP == pattrib->ether_type) {
450 /* The following is for DHCP and ARP packet, we use cck1M to tx these packets and let LPS awake some time */
451 /* to prevent DHCP protocol fail */
453 _rtw_pktfile_read(&pktfile, &tmp[0], 24);
454 pattrib->dhcp_pkt = 0;
455 if (pktfile.pkt_len > 282) {/* MINIMUM_DHCP_PACKET_SIZE) { */
456 if (((tmp[21] == 68) && (tmp[23] == 67)) ||
457 ((tmp[21] == 67) && (tmp[23] == 68))) {
458 /* 68 : UDP BOOTP client */
459 /* 67 : UDP BOOTP server */
460 /* Use low rate to send DHCP packet. */
461 pattrib->dhcp_pkt = 1;
466 if ((pattrib->ether_type == 0x888e) || (pattrib->dhcp_pkt == 1))
467 rtw_set_scan_deny(padapter, 3000);
469 /* If EAPOL , ARP , OR DHCP packet, driver must be in active mode. */
470 if ((pattrib->ether_type == 0x0806) || (pattrib->ether_type == 0x888e) || (pattrib->dhcp_pkt == 1))
471 rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_SPECIAL_PACKET, 1);
473 bmcast = is_multicast_ether_addr(pattrib->ra);
477 psta = rtw_get_bcmc_stainfo(padapter);
479 psta = rtw_get_stainfo(pstapriv, pattrib->ra);
480 if (!psta) { /* if we cannot get psta => drrp the pkt */
483 } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE) && !(psta->state & _FW_LINKED)) {
490 pattrib->mac_id = psta->mac_id;
491 pattrib->psta = psta;
493 /* if we cannot get psta => drop the pkt */
498 pattrib->ack_policy = 0;
499 /* get ether_hdr_len */
500 pattrib->pkt_hdrlen = ETH_HLEN;/* pattrib->ether_type == 0x8100) ? (14 + 4): 14; vlan tag */
502 pattrib->hdrlen = WLAN_HDR_A3_LEN;
503 pattrib->subtype = IEEE80211_FTYPE_DATA;
504 pattrib->priority = 0;
506 if (check_fwstate(pmlmepriv, WIFI_AP_STATE | WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE)) {
507 if (psta->qos_option)
508 set_qos(&pktfile, pattrib);
510 if (pqospriv->qos_option) {
511 set_qos(&pktfile, pattrib);
513 if (pmlmepriv->acm_mask != 0)
514 pattrib->priority = qos_acm(pmlmepriv->acm_mask, pattrib->priority);
518 if (psta->ieee8021x_blocked) {
519 pattrib->encrypt = 0;
521 if ((pattrib->ether_type != 0x888e) && !check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
526 GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt, bmcast);
528 switch (psecuritypriv->dot11AuthAlgrthm) {
529 case dot11AuthAlgrthm_Open:
530 case dot11AuthAlgrthm_Shared:
531 case dot11AuthAlgrthm_Auto:
532 pattrib->key_idx = (u8)psecuritypriv->dot11PrivacyKeyIndex;
534 case dot11AuthAlgrthm_8021X:
536 pattrib->key_idx = (u8)psecuritypriv->dot118021XGrpKeyid;
538 pattrib->key_idx = 0;
541 pattrib->key_idx = 0;
546 switch (pattrib->encrypt) {
550 pattrib->icv_len = 4;
554 pattrib->icv_len = 4;
556 if (padapter->securitypriv.busetkipkey == _FAIL) {
563 pattrib->icv_len = 8;
567 pattrib->icv_len = 0;
571 if (pattrib->encrypt &&
572 (padapter->securitypriv.sw_encrypt || !psecuritypriv->hw_decrypted))
573 pattrib->bswenc = true;
575 pattrib->bswenc = false;
577 update_attrib_phy_info(pattrib, psta);
584 static s32 xmitframe_addmic(struct adapter *padapter, struct xmit_frame *pxmitframe)
586 int curfragnum, length;
587 u8 *pframe, *payload, mic[8];
588 struct mic_data micdata;
589 struct sta_info *stainfo;
590 struct pkt_attrib *pattrib = &pxmitframe->attrib;
591 struct security_priv *psecuritypriv = &padapter->securitypriv;
592 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
593 u8 priority[4] = {0x0, 0x0, 0x0, 0x0};
594 u8 hw_hdr_offset = 0;
597 stainfo = pattrib->psta;
599 stainfo = rtw_get_stainfo(&padapter->stapriv, &pattrib->ra[0]);
601 hw_hdr_offset = TXDESC_SIZE + (pxmitframe->pkt_offset * PACKET_OFFSET_SZ);
603 if (pattrib->encrypt == _TKIP_) {/* if (psecuritypriv->dot11PrivacyAlgrthm == _TKIP_PRIVACY_) */
604 /* encode mic code */
606 u8 null_key[16] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
607 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
610 pframe = pxmitframe->buf_addr + hw_hdr_offset;
612 if (is_multicast_ether_addr(pattrib->ra)) {
613 if (!memcmp(psecuritypriv->dot118021XGrptxmickey[psecuritypriv->dot118021XGrpKeyid].skey, null_key, 16))
615 /* start to calculate the mic code */
616 rtw_secmicsetkey(&micdata, psecuritypriv->dot118021XGrptxmickey[psecuritypriv->dot118021XGrpKeyid].skey);
618 if (!memcmp(&stainfo->dot11tkiptxmickey.skey[0], null_key, 16)) {
622 /* start to calculate the mic code */
623 rtw_secmicsetkey(&micdata, &stainfo->dot11tkiptxmickey.skey[0]);
626 if (pframe[1] & 1) { /* ToDS == 1 */
627 rtw_secmicappend(&micdata, &pframe[16], 6); /* DA */
628 if (pframe[1] & 2) /* From Ds == 1 */
629 rtw_secmicappend(&micdata, &pframe[24], 6);
631 rtw_secmicappend(&micdata, &pframe[10], 6);
632 } else { /* ToDS == 0 */
633 rtw_secmicappend(&micdata, &pframe[4], 6); /* DA */
634 if (pframe[1] & 2) /* From Ds == 1 */
635 rtw_secmicappend(&micdata, &pframe[16], 6);
637 rtw_secmicappend(&micdata, &pframe[10], 6);
641 priority[0] = (u8)pxmitframe->attrib.priority;
643 rtw_secmicappend(&micdata, &priority[0], 4);
647 for (curfragnum = 0; curfragnum < pattrib->nr_frags; curfragnum++) {
648 payload = PTR_ALIGN(payload, 4);
650 payload = payload + pattrib->hdrlen + pattrib->iv_len;
651 if ((curfragnum + 1) == pattrib->nr_frags) {
652 length = pattrib->last_txcmdsz - pattrib->hdrlen - pattrib->iv_len - ((pattrib->bswenc) ? pattrib->icv_len : 0);
653 rtw_secmicappend(&micdata, payload, length);
654 payload = payload + length;
656 length = pxmitpriv->frag_len - pattrib->hdrlen - pattrib->iv_len - ((pattrib->bswenc) ? pattrib->icv_len : 0);
657 rtw_secmicappend(&micdata, payload, length);
658 payload = payload + length + pattrib->icv_len;
661 rtw_secgetmic(&micdata, &mic[0]);
662 /* add mic code and add the mic code length in last_txcmdsz */
664 memcpy(payload, &mic[0], 8);
665 pattrib->last_txcmdsz += 8;
667 payload = payload - pattrib->last_txcmdsz + 8;
674 static s32 xmitframe_swencrypt(struct adapter *padapter, struct xmit_frame *pxmitframe)
676 struct pkt_attrib *pattrib = &pxmitframe->attrib;
678 if (pattrib->bswenc) {
679 switch (pattrib->encrypt) {
682 rtw_wep_encrypt(padapter, pxmitframe);
685 rtw_tkip_encrypt(padapter, pxmitframe);
688 rtw_aes_encrypt(padapter, pxmitframe);
698 s32 rtw_make_wlanhdr(struct adapter *padapter, u8 *hdr, struct pkt_attrib *pattrib)
702 struct ieee80211_hdr *pwlanhdr = (struct ieee80211_hdr *)hdr;
703 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
704 struct qos_priv *pqospriv = &pmlmepriv->qospriv;
705 u8 qos_option = false;
708 __le16 *fctrl = &pwlanhdr->frame_control;
710 struct sta_info *psta;
713 psta = pattrib->psta;
714 else if (is_multicast_ether_addr(pattrib->ra))
715 psta = rtw_get_bcmc_stainfo(padapter);
717 psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
719 memset(hdr, 0, WLANHDR_OFFSET);
721 SetFrameSubType(fctrl, pattrib->subtype);
723 if (pattrib->subtype & IEEE80211_FTYPE_DATA) {
724 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
725 /* to_ds = 1, fr_ds = 0; */
726 /* Data transfer to AP */
728 memcpy(pwlanhdr->addr1, get_bssid(pmlmepriv), ETH_ALEN);
729 memcpy(pwlanhdr->addr2, pattrib->src, ETH_ALEN);
730 memcpy(pwlanhdr->addr3, pattrib->dst, ETH_ALEN);
732 if (pqospriv->qos_option)
734 } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
735 /* to_ds = 0, fr_ds = 1; */
737 memcpy(pwlanhdr->addr1, pattrib->dst, ETH_ALEN);
738 memcpy(pwlanhdr->addr2, get_bssid(pmlmepriv), ETH_ALEN);
739 memcpy(pwlanhdr->addr3, pattrib->src, ETH_ALEN);
741 if (psta->qos_option)
743 } else if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) ||
744 check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
745 memcpy(pwlanhdr->addr1, pattrib->dst, ETH_ALEN);
746 memcpy(pwlanhdr->addr2, pattrib->src, ETH_ALEN);
747 memcpy(pwlanhdr->addr3, get_bssid(pmlmepriv), ETH_ALEN);
749 if (psta->qos_option)
759 if (pattrib->encrypt)
763 qc = (unsigned short *)(hdr + pattrib->hdrlen - 2);
765 if (pattrib->priority)
766 SetPriority(qc, pattrib->priority);
768 SetEOSP(qc, pattrib->eosp);
770 SetAckpolicy(qc, pattrib->ack_policy);
773 /* TODO: fill HT Control Field */
775 /* Update Seq Num will be handled by f/w */
777 psta->sta_xmitpriv.txseq_tid[pattrib->priority]++;
778 psta->sta_xmitpriv.txseq_tid[pattrib->priority] &= 0xFFF;
780 pattrib->seqnum = psta->sta_xmitpriv.txseq_tid[pattrib->priority];
782 SetSeqNum(hdr, pattrib->seqnum);
784 /* check if enable ampdu */
785 if (pattrib->ht_en && psta->htpriv.ampdu_enable) {
786 if (psta->htpriv.agg_enable_bitmap & BIT(pattrib->priority))
787 pattrib->ampdu_en = true;
790 /* re-check if enable ampdu by BA_starting_seqctrl */
791 if (pattrib->ampdu_en) {
794 tx_seq = psta->BA_starting_seqctrl[pattrib->priority & 0x0f];
796 /* check BA_starting_seqctrl */
797 if (SN_LESS(pattrib->seqnum, tx_seq)) {
798 pattrib->ampdu_en = false;/* AGG BK */
799 } else if (SN_EQUAL(pattrib->seqnum, tx_seq)) {
800 psta->BA_starting_seqctrl[pattrib->priority & 0x0f] = (tx_seq + 1) & 0xfff;
802 pattrib->ampdu_en = true;/* AGG EN */
804 psta->BA_starting_seqctrl[pattrib->priority & 0x0f] = (pattrib->seqnum + 1) & 0xfff;
805 pattrib->ampdu_en = true;/* AGG EN */
815 s32 rtw_txframes_pending(struct adapter *padapter)
817 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
819 return (!list_empty(&pxmitpriv->be_pending.queue) ||
820 !list_empty(&pxmitpriv->bk_pending.queue) ||
821 !list_empty(&pxmitpriv->vi_pending.queue) ||
822 !list_empty(&pxmitpriv->vo_pending.queue));
825 s32 rtw_txframes_sta_ac_pending(struct adapter *padapter, struct pkt_attrib *pattrib)
827 struct sta_info *psta;
828 struct tx_servq *ptxservq;
829 int priority = pattrib->priority;
831 psta = pattrib->psta;
836 ptxservq = &psta->sta_xmitpriv.bk_q;
840 ptxservq = &psta->sta_xmitpriv.vi_q;
844 ptxservq = &psta->sta_xmitpriv.vo_q;
849 ptxservq = &psta->sta_xmitpriv.be_q;
854 return ptxservq->qcnt;
859 * This sub-routine will perform all the following:
861 * 1. remove 802.3 header.
862 * 2. create wlan_header, based on the info in pxmitframe
863 * 3. append sta's iv/ext-iv
865 * 5. move frag chunk from pframe to pxmitframe->mem
866 * 6. apply sw-encrypt, if necessary.
868 s32 rtw_xmitframe_coalesce(struct adapter *padapter, struct sk_buff *pkt, struct xmit_frame *pxmitframe)
870 struct pkt_file pktfile;
871 s32 frg_inx, frg_len, mpdu_len, llc_sz, mem_sz;
872 u8 *pframe, *mem_start;
874 struct sta_info *psta;
875 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
876 struct pkt_attrib *pattrib = &pxmitframe->attrib;
878 bool bmcst = is_multicast_ether_addr(pattrib->ra);
884 psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
889 if (!pxmitframe->buf_addr)
892 pbuf_start = pxmitframe->buf_addr;
894 hw_hdr_offset = TXDESC_SIZE + (pxmitframe->pkt_offset * PACKET_OFFSET_SZ);
896 mem_start = pbuf_start + hw_hdr_offset;
898 if (rtw_make_wlanhdr(padapter, mem_start, pattrib) == _FAIL) {
903 _rtw_open_pktfile(pkt, &pktfile);
904 _rtw_pktfile_read(&pktfile, NULL, pattrib->pkt_hdrlen);
907 frg_len = pxmitpriv->frag_len - 4;/* 2346-4 = 2342 */
918 pframe += pattrib->hdrlen;
919 mpdu_len -= pattrib->hdrlen;
921 /* adding icv, if necessary... */
922 if (pattrib->iv_len) {
924 switch (pattrib->encrypt) {
927 WEP_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
931 TKIP_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
933 TKIP_IV(pattrib->iv, psta->dot11txpn, 0);
937 AES_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
939 AES_IV(pattrib->iv, psta->dot11txpn, 0);
944 memcpy(pframe, pattrib->iv, pattrib->iv_len);
946 pframe += pattrib->iv_len;
948 mpdu_len -= pattrib->iv_len;
952 llc_sz = rtw_put_snap(pframe, pattrib->ether_type);
957 if ((pattrib->icv_len > 0) && (pattrib->bswenc)) {
958 mpdu_len -= pattrib->icv_len;
962 /* don't do fragment to broadcat/multicast packets */
963 mem_sz = _rtw_pktfile_read(&pktfile, pframe, pattrib->pktlen);
965 mem_sz = _rtw_pktfile_read(&pktfile, pframe, mpdu_len);
970 if ((pattrib->icv_len > 0) && (pattrib->bswenc)) {
971 memcpy(pframe, pattrib->icv, pattrib->icv_len);
972 pframe += pattrib->icv_len;
977 if (bmcst || rtw_endofpktfile(&pktfile)) {
978 pattrib->nr_frags = frg_inx;
980 pattrib->last_txcmdsz = pattrib->hdrlen + pattrib->iv_len + ((pattrib->nr_frags == 1) ? llc_sz : 0) +
981 ((pattrib->bswenc) ? pattrib->icv_len : 0) + mem_sz;
983 ClearMFrag(mem_start);
988 mem_start = PTR_ALIGN(pframe, 4) + hw_hdr_offset;
989 memcpy(mem_start, pbuf_start + hw_hdr_offset, pattrib->hdrlen);
992 if (xmitframe_addmic(padapter, pxmitframe) == _FAIL) {
997 xmitframe_swencrypt(padapter, pxmitframe);
1000 update_attrib_vcs_info(padapter, pxmitframe);
1002 pattrib->vcs_mode = NONE_VCS;
1009 /* Logical Link Control(LLC) SubNetwork Attachment Point(SNAP) header
1010 * IEEE LLC/SNAP header contains 8 octets
1011 * First 3 octets comprise the LLC portion
1012 * SNAP portion, 5 octets, is divided into two fields:
1013 * Organizationally Unique Identifier(OUI), 3 octets,
1014 * type, defined by that organization, 2 octets.
1016 s32 rtw_put_snap(u8 *data, u16 h_proto)
1018 struct ieee80211_snap_hdr *snap;
1021 snap = (struct ieee80211_snap_hdr *)data;
1026 if (h_proto == 0x8137 || h_proto == 0x80f3)
1031 snap->oui[0] = oui[0];
1032 snap->oui[1] = oui[1];
1033 snap->oui[2] = oui[2];
1035 *(__be16 *)(data + SNAP_SIZE) = htons(h_proto);
1037 return SNAP_SIZE + sizeof(u16);
1040 void rtw_update_protection(struct adapter *padapter, u8 *ie, uint ie_len)
1045 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1046 struct registry_priv *pregistrypriv = &padapter->registrypriv;
1048 switch (pxmitpriv->vcs_setting) {
1050 pxmitpriv->vcs = NONE_VCS;
1056 perp = rtw_get_ie(ie, _ERPINFO_IE_, &erp_len, ie_len);
1058 pxmitpriv->vcs = NONE_VCS;
1060 protection = (*(perp + 2)) & BIT(1);
1062 if (pregistrypriv->vcs_type == RTS_CTS)
1063 pxmitpriv->vcs = RTS_CTS;
1065 pxmitpriv->vcs = CTS_TO_SELF;
1067 pxmitpriv->vcs = NONE_VCS;
1075 void rtw_count_tx_stats(struct adapter *padapter, struct xmit_frame *pxmitframe, int sz)
1077 struct sta_info *psta = NULL;
1078 struct stainfo_stats *pstats = NULL;
1079 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1080 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1082 if ((pxmitframe->frame_tag & 0x0f) == DATA_FRAMETAG) {
1083 pxmitpriv->tx_bytes += sz;
1084 pmlmepriv->LinkDetectInfo.NumTxOkInPeriod += pxmitframe->agg_num;
1086 psta = pxmitframe->attrib.psta;
1088 pstats = &psta->sta_stats;
1089 pstats->tx_pkts += pxmitframe->agg_num;
1090 pstats->tx_bytes += sz;
1095 struct xmit_buf *rtw_alloc_xmitbuf_ext(struct xmit_priv *pxmitpriv)
1097 struct xmit_buf *pxmitbuf = NULL;
1098 struct list_head *plist, *phead;
1099 struct __queue *pfree_queue = &pxmitpriv->free_xmit_extbuf_queue;
1100 unsigned long flags;
1102 spin_lock_irqsave(&pfree_queue->lock, flags);
1104 if (list_empty(&pfree_queue->queue)) {
1107 phead = get_list_head(pfree_queue);
1109 plist = phead->next;
1111 pxmitbuf = container_of(plist, struct xmit_buf, list);
1113 list_del_init(&pxmitbuf->list);
1117 pxmitpriv->free_xmit_extbuf_cnt--;
1119 pxmitbuf->priv_data = NULL;
1120 /* pxmitbuf->ext_tag = true; */
1123 rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_ALLOC);
1126 spin_unlock_irqrestore(&pfree_queue->lock, flags);
1131 s32 rtw_free_xmitbuf_ext(struct xmit_priv *pxmitpriv, struct xmit_buf *pxmitbuf)
1133 struct __queue *pfree_queue = &pxmitpriv->free_xmit_extbuf_queue;
1134 unsigned long flags;
1139 spin_lock_irqsave(&pfree_queue->lock, flags);
1141 list_del_init(&pxmitbuf->list);
1143 list_add_tail(&pxmitbuf->list, get_list_head(pfree_queue));
1144 pxmitpriv->free_xmit_extbuf_cnt++;
1146 spin_unlock_irqrestore(&pfree_queue->lock, flags);
1151 struct xmit_buf *rtw_alloc_xmitbuf(struct xmit_priv *pxmitpriv)
1153 struct xmit_buf *pxmitbuf = NULL;
1154 struct list_head *plist, *phead;
1155 struct __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
1156 unsigned long flags;
1158 spin_lock_irqsave(&pfree_xmitbuf_queue->lock, flags);
1160 if (list_empty(&pfree_xmitbuf_queue->queue)) {
1163 phead = get_list_head(pfree_xmitbuf_queue);
1165 plist = phead->next;
1167 pxmitbuf = container_of(plist, struct xmit_buf, list);
1169 list_del_init(&pxmitbuf->list);
1173 pxmitpriv->free_xmitbuf_cnt--;
1174 pxmitbuf->priv_data = NULL;
1176 rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_ALLOC);
1178 spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, flags);
1183 s32 rtw_free_xmitbuf(struct xmit_priv *pxmitpriv, struct xmit_buf *pxmitbuf)
1185 struct __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
1186 unsigned long flags;
1192 rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_FREE);
1194 if (pxmitbuf->ext_tag) {
1195 rtw_free_xmitbuf_ext(pxmitpriv, pxmitbuf);
1197 spin_lock_irqsave(&pfree_xmitbuf_queue->lock, flags);
1199 list_del_init(&pxmitbuf->list);
1201 list_add_tail(&pxmitbuf->list, get_list_head(pfree_xmitbuf_queue));
1203 pxmitpriv->free_xmitbuf_cnt++;
1204 spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, flags);
1213 * 2. RXENTRY (rx_thread or RX_ISR/RX_CallBack)
1215 * If we turn on USE_RXTHREAD, then, no need for critical section.
1216 * Otherwise, we must use _enter/_exit critical to protect free_xmit_queue...
1218 * Must be very very cautious...
1220 struct xmit_frame *rtw_alloc_xmitframe(struct xmit_priv *pxmitpriv)/* _queue *pfree_xmit_queue) */
1223 * Please remember to use all the osdep_service api,
1224 * and lock/unlock or _enter/_exit critical to protect
1228 struct xmit_frame *pxframe = NULL;
1229 struct list_head *plist, *phead;
1230 struct __queue *pfree_xmit_queue = &pxmitpriv->free_xmit_queue;
1232 spin_lock_bh(&pfree_xmit_queue->lock);
1234 if (list_empty(&pfree_xmit_queue->queue)) {
1237 phead = get_list_head(pfree_xmit_queue);
1239 plist = phead->next;
1241 pxframe = container_of(plist, struct xmit_frame, list);
1243 list_del_init(&pxframe->list);
1246 if (pxframe) { /* default value setting */
1247 pxmitpriv->free_xmitframe_cnt--;
1249 pxframe->buf_addr = NULL;
1250 pxframe->pxmitbuf = NULL;
1252 memset(&pxframe->attrib, 0, sizeof(struct pkt_attrib));
1253 /* pxframe->attrib.psta = NULL; */
1255 pxframe->frame_tag = DATA_FRAMETAG;
1257 pxframe->pkt = NULL;
1258 pxframe->pkt_offset = 1;/* default use pkt_offset to fill tx desc */
1260 pxframe->agg_num = 1;
1261 pxframe->ack_report = 0;
1264 spin_unlock_bh(&pfree_xmit_queue->lock);
1269 s32 rtw_free_xmitframe(struct xmit_priv *pxmitpriv, struct xmit_frame *pxmitframe)
1271 struct __queue *pfree_xmit_queue = &pxmitpriv->free_xmit_queue;
1272 struct adapter *padapter = pxmitpriv->adapter;
1273 struct sk_buff *pndis_pkt = NULL;
1278 spin_lock_bh(&pfree_xmit_queue->lock);
1280 list_del_init(&pxmitframe->list);
1282 if (pxmitframe->pkt) {
1283 pndis_pkt = pxmitframe->pkt;
1284 pxmitframe->pkt = NULL;
1287 list_add_tail(&pxmitframe->list, get_list_head(pfree_xmit_queue));
1289 pxmitpriv->free_xmitframe_cnt++;
1291 spin_unlock_bh(&pfree_xmit_queue->lock);
1294 rtw_os_pkt_complete(padapter, pndis_pkt);
1301 void rtw_free_xmitframe_queue(struct xmit_priv *pxmitpriv, struct __queue *pframequeue)
1303 struct list_head *plist, *phead;
1304 struct xmit_frame *pxmitframe;
1306 spin_lock_bh(&pframequeue->lock);
1308 phead = get_list_head(pframequeue);
1309 plist = phead->next;
1311 while (phead != plist) {
1312 pxmitframe = container_of(plist, struct xmit_frame, list);
1314 plist = plist->next;
1316 rtw_free_xmitframe(pxmitpriv, pxmitframe);
1318 spin_unlock_bh(&pframequeue->lock);
1322 s32 rtw_xmitframe_enqueue(struct adapter *padapter, struct xmit_frame *pxmitframe)
1324 if (rtw_xmit_classifier(padapter, pxmitframe) == _FAIL) {
1325 /* pxmitframe->pkt = NULL; */
1332 static struct xmit_frame *dequeue_one_xmitframe(struct xmit_priv *pxmitpriv, struct hw_xmit *phwxmit, struct tx_servq *ptxservq, struct __queue *pframe_queue)
1334 struct list_head *xmitframe_plist, *xmitframe_phead;
1335 struct xmit_frame *pxmitframe = NULL;
1337 xmitframe_phead = get_list_head(pframe_queue);
1338 xmitframe_plist = xmitframe_phead->next;
1340 if (xmitframe_phead != xmitframe_plist) {
1341 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
1343 xmitframe_plist = xmitframe_plist->next;
1345 list_del_init(&pxmitframe->list);
1352 struct xmit_frame *rtw_dequeue_xframe(struct xmit_priv *pxmitpriv, struct hw_xmit *phwxmit_i, int entry)
1354 struct list_head *sta_plist, *sta_phead;
1355 struct hw_xmit *phwxmit;
1356 struct tx_servq *ptxservq = NULL;
1357 struct __queue *pframe_queue = NULL;
1358 struct xmit_frame *pxmitframe = NULL;
1359 struct adapter *padapter = pxmitpriv->adapter;
1360 struct registry_priv *pregpriv = &padapter->registrypriv;
1363 inx[0] = 0; inx[1] = 1; inx[2] = 2; inx[3] = 3;
1365 if (pregpriv->wifi_spec == 1) {
1368 for (j = 0; j < 4; j++)
1369 inx[j] = pxmitpriv->wmm_para_seq[j];
1372 spin_lock_bh(&pxmitpriv->lock);
1374 for (i = 0; i < entry; i++) {
1375 phwxmit = phwxmit_i + inx[i];
1377 sta_phead = get_list_head(phwxmit->sta_queue);
1378 sta_plist = sta_phead->next;
1380 while (sta_phead != sta_plist) {
1381 ptxservq = container_of(sta_plist, struct tx_servq, tx_pending);
1383 pframe_queue = &ptxservq->sta_pending;
1385 pxmitframe = dequeue_one_xmitframe(pxmitpriv, phwxmit, ptxservq, pframe_queue);
1390 /* Remove sta node when there are no pending packets. */
1391 if (list_empty(&pframe_queue->queue)) /* must be done after get_next and before break */
1392 list_del_init(&ptxservq->tx_pending);
1396 sta_plist = sta_plist->next;
1400 spin_unlock_bh(&pxmitpriv->lock);
1405 struct tx_servq *rtw_get_sta_pending(struct adapter *padapter, struct sta_info *psta, int up, u8 *ac)
1407 struct tx_servq *ptxservq;
1412 ptxservq = &psta->sta_xmitpriv.bk_q;
1417 ptxservq = &psta->sta_xmitpriv.vi_q;
1422 ptxservq = &psta->sta_xmitpriv.vo_q;
1428 ptxservq = &psta->sta_xmitpriv.be_q;
1437 * Will enqueue pxmitframe to the proper queue,
1438 * and indicate it to xx_pending list.....
1440 s32 rtw_xmit_classifier(struct adapter *padapter, struct xmit_frame *pxmitframe)
1443 struct sta_info *psta;
1444 struct tx_servq *ptxservq;
1445 struct pkt_attrib *pattrib = &pxmitframe->attrib;
1446 struct sta_priv *pstapriv = &padapter->stapriv;
1447 struct hw_xmit *phwxmits = padapter->xmitpriv.hwxmits;
1451 psta = pattrib->psta;
1453 psta = rtw_get_stainfo(pstapriv, pattrib->ra);
1460 ptxservq = rtw_get_sta_pending(padapter, psta, pattrib->priority, (u8 *)(&ac_index));
1462 if (list_empty(&ptxservq->tx_pending))
1463 list_add_tail(&ptxservq->tx_pending, get_list_head(phwxmits[ac_index].sta_queue));
1465 list_add_tail(&pxmitframe->list, get_list_head(&ptxservq->sta_pending));
1467 phwxmits[ac_index].accnt++;
1473 int rtw_alloc_hwxmits(struct adapter *padapter)
1475 struct hw_xmit *hwxmits;
1476 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1478 pxmitpriv->hwxmit_entry = HWXMIT_ENTRY;
1480 pxmitpriv->hwxmits = kzalloc(sizeof(struct hw_xmit) * pxmitpriv->hwxmit_entry, GFP_KERNEL);
1481 if (!pxmitpriv->hwxmits)
1484 hwxmits = pxmitpriv->hwxmits;
1486 if (pxmitpriv->hwxmit_entry == 5) {
1487 hwxmits[0] .sta_queue = &pxmitpriv->bm_pending;
1488 hwxmits[1] .sta_queue = &pxmitpriv->vo_pending;
1489 hwxmits[2] .sta_queue = &pxmitpriv->vi_pending;
1490 hwxmits[3] .sta_queue = &pxmitpriv->bk_pending;
1491 hwxmits[4] .sta_queue = &pxmitpriv->be_pending;
1492 } else if (pxmitpriv->hwxmit_entry == 4) {
1493 hwxmits[0] .sta_queue = &pxmitpriv->vo_pending;
1494 hwxmits[1] .sta_queue = &pxmitpriv->vi_pending;
1495 hwxmits[2] .sta_queue = &pxmitpriv->be_pending;
1496 hwxmits[3] .sta_queue = &pxmitpriv->bk_pending;
1503 void rtw_free_hwxmits(struct adapter *padapter)
1505 struct hw_xmit *hwxmits;
1506 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1508 hwxmits = pxmitpriv->hwxmits;
1512 void rtw_init_hwxmits(struct hw_xmit *phwxmit, int entry)
1516 for (i = 0; i < entry; i++, phwxmit++)
1521 static int rtw_br_client_tx(struct adapter *padapter, struct sk_buff **pskb)
1523 struct sk_buff *skb = *pskb;
1524 int res, is_vlan_tag = 0, i, do_nat25 = 1;
1525 unsigned short vlan_hdr = 0;
1526 void *br_port = NULL;
1529 br_port = rcu_dereference(padapter->pnetdev->rx_handler_data);
1531 spin_lock_bh(&padapter->br_ext_lock);
1532 if (!(skb->data[0] & 1) && br_port &&
1533 memcmp(skb->data + ETH_ALEN, padapter->br_mac, ETH_ALEN) &&
1534 *((__be16 *)(skb->data + ETH_ALEN * 2)) != __constant_htons(ETH_P_8021Q) &&
1535 *((__be16 *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_IP) &&
1536 !memcmp(padapter->scdb_mac, skb->data + ETH_ALEN, ETH_ALEN) && padapter->scdb_entry) {
1537 memcpy(skb->data + ETH_ALEN, GET_MY_HWADDR(padapter), ETH_ALEN);
1538 padapter->scdb_entry->ageing_timer = jiffies;
1539 spin_unlock_bh(&padapter->br_ext_lock);
1541 if (*((__be16 *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_8021Q)) {
1543 vlan_hdr = *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2));
1544 for (i = 0; i < 6; i++)
1545 *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2 - i * 2)) = *((unsigned short *)(skb->data + ETH_ALEN * 2 - 2 - i * 2));
1548 if (!memcmp(skb->data + ETH_ALEN, padapter->br_mac, ETH_ALEN) &&
1549 (*((__be16 *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_IP)))
1550 memcpy(padapter->br_ip, skb->data + WLAN_ETHHDR_LEN + 12, 4);
1552 if (*((__be16 *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_IP)) {
1553 if (memcmp(padapter->scdb_mac, skb->data + ETH_ALEN, ETH_ALEN)) {
1554 padapter->scdb_entry = (struct nat25_network_db_entry *)scdb_findEntry(padapter,
1555 skb->data + WLAN_ETHHDR_LEN + 12);
1556 if (padapter->scdb_entry) {
1557 memcpy(padapter->scdb_mac, skb->data + ETH_ALEN, ETH_ALEN);
1558 memcpy(padapter->scdb_ip, skb->data + WLAN_ETHHDR_LEN + 12, 4);
1559 padapter->scdb_entry->ageing_timer = jiffies;
1563 if (padapter->scdb_entry) {
1564 padapter->scdb_entry->ageing_timer = jiffies;
1567 memset(padapter->scdb_mac, 0, ETH_ALEN);
1568 memset(padapter->scdb_ip, 0, 4);
1572 spin_unlock_bh(&padapter->br_ext_lock);
1574 if (nat25_db_handle(padapter, skb, NAT25_CHECK) == 0) {
1575 struct sk_buff *newskb;
1579 for (i = 0; i < 6; i++)
1580 *((unsigned short *)(skb->data + i * 2)) = *((unsigned short *)(skb->data + 4 + i * 2));
1581 *((__be16 *)(skb->data + ETH_ALEN * 2)) = __constant_htons(ETH_P_8021Q);
1582 *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2)) = vlan_hdr;
1585 newskb = skb_copy(skb, GFP_ATOMIC);
1588 dev_kfree_skb_any(skb);
1590 *pskb = skb = newskb;
1592 vlan_hdr = *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2));
1593 for (i = 0; i < 6; i++)
1594 *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2 - i * 2)) = *((unsigned short *)(skb->data + ETH_ALEN * 2 - 2 - i * 2));
1599 res = skb_linearize(skb);
1603 res = nat25_db_handle(padapter, skb, NAT25_INSERT);
1612 memcpy(skb->data + ETH_ALEN, GET_MY_HWADDR(padapter), ETH_ALEN);
1614 dhcp_flag_bcast(padapter, skb);
1618 for (i = 0; i < 6; i++)
1619 *((unsigned short *)(skb->data + i * 2)) = *((unsigned short *)(skb->data + 4 + i * 2));
1620 *((__be16 *)(skb->data + ETH_ALEN * 2)) = __constant_htons(ETH_P_8021Q);
1621 *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2)) = vlan_hdr;
1625 /* check if SA is equal to our MAC */
1626 if (memcmp(skb->data + ETH_ALEN, GET_MY_HWADDR(padapter), ETH_ALEN))
1632 u32 rtw_get_ff_hwaddr(struct xmit_frame *pxmitframe)
1635 struct pkt_attrib *pattrib = &pxmitframe->attrib;
1637 switch (pattrib->qsel) {
1640 addr = BE_QUEUE_INX;
1644 addr = BK_QUEUE_INX;
1648 addr = VI_QUEUE_INX;
1652 addr = VO_QUEUE_INX;
1655 addr = BCN_QUEUE_INX;
1657 case 0x11:/* BC/MC in PS (HIQ) */
1658 addr = HIGH_QUEUE_INX;
1662 addr = MGT_QUEUE_INX;
1669 static void do_queue_select(struct adapter *padapter, struct pkt_attrib *pattrib)
1673 qsel = pattrib->priority;
1675 pattrib->qsel = qsel;
1679 * The main transmit(tx) entry
1683 * 0 success, hardware will handle this xmit frame(packet)
1686 s32 rtw_xmit(struct adapter *padapter, struct sk_buff **ppkt)
1688 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1689 struct xmit_frame *pxmitframe = NULL;
1690 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1691 void *br_port = NULL;
1694 pxmitframe = rtw_alloc_xmitframe(pxmitpriv);
1699 br_port = rcu_dereference(padapter->pnetdev->rx_handler_data);
1702 if (br_port && check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_ADHOC_STATE)) {
1703 res = rtw_br_client_tx(padapter, ppkt);
1705 rtw_free_xmitframe(pxmitpriv, pxmitframe);
1710 res = update_attrib(padapter, *ppkt, &pxmitframe->attrib);
1713 rtw_free_xmitframe(pxmitpriv, pxmitframe);
1716 pxmitframe->pkt = *ppkt;
1718 rtw_led_control(padapter, LED_CTL_TX);
1720 do_queue_select(padapter, &pxmitframe->attrib);
1722 spin_lock_bh(&pxmitpriv->lock);
1723 if (xmitframe_enqueue_for_sleeping_sta(padapter, pxmitframe)) {
1724 spin_unlock_bh(&pxmitpriv->lock);
1727 spin_unlock_bh(&pxmitpriv->lock);
1729 if (!rtl8188eu_hal_xmit(padapter, pxmitframe))
1735 int xmitframe_enqueue_for_sleeping_sta(struct adapter *padapter, struct xmit_frame *pxmitframe)
1738 struct sta_info *psta = NULL;
1739 struct sta_priv *pstapriv = &padapter->stapriv;
1740 struct pkt_attrib *pattrib = &pxmitframe->attrib;
1741 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1742 bool bmcst = is_multicast_ether_addr(pattrib->ra);
1744 if (!check_fwstate(pmlmepriv, WIFI_AP_STATE))
1748 psta = pattrib->psta;
1750 psta = rtw_get_stainfo(pstapriv, pattrib->ra);
1755 if (pattrib->triggered == 1) {
1757 pattrib->qsel = 0x11;/* HIQ */
1762 spin_lock_bh(&psta->sleep_q.lock);
1764 if (pstapriv->sta_dz_bitmap) {/* if any one sta is in ps mode */
1765 list_del_init(&pxmitframe->list);
1767 list_add_tail(&pxmitframe->list, get_list_head(&psta->sleep_q));
1771 pstapriv->tim_bitmap |= BIT(0);/* */
1772 pstapriv->sta_dz_bitmap |= BIT(0);
1774 update_beacon(padapter, _TIM_IE_, NULL, false);/* tx bc/mc packets after upate bcn */
1779 spin_unlock_bh(&psta->sleep_q.lock);
1784 spin_lock_bh(&psta->sleep_q.lock);
1786 if (psta->state & WIFI_SLEEP_STATE) {
1789 if (pstapriv->sta_dz_bitmap & BIT(psta->aid)) {
1790 list_del_init(&pxmitframe->list);
1792 list_add_tail(&pxmitframe->list, get_list_head(&psta->sleep_q));
1796 switch (pattrib->priority) {
1799 wmmps_ac = psta->uapsd_bk & BIT(0);
1803 wmmps_ac = psta->uapsd_vi & BIT(0);
1807 wmmps_ac = psta->uapsd_vo & BIT(0);
1812 wmmps_ac = psta->uapsd_be & BIT(0);
1817 psta->sleepq_ac_len++;
1819 if (((psta->has_legacy_ac) && (!wmmps_ac)) ||
1820 ((!psta->has_legacy_ac) && (wmmps_ac))) {
1821 pstapriv->tim_bitmap |= BIT(psta->aid);
1823 if (psta->sleepq_len == 1) {
1824 /* upate BCN for TIM IE */
1825 update_beacon(padapter, _TIM_IE_, NULL, false);
1832 spin_unlock_bh(&psta->sleep_q.lock);
1837 static void dequeue_xmitframes_to_sleeping_queue(struct adapter *padapter, struct sta_info *psta, struct __queue *pframequeue)
1839 struct list_head *plist, *phead;
1841 struct tx_servq *ptxservq;
1842 struct pkt_attrib *pattrib;
1843 struct xmit_frame *pxmitframe;
1844 struct hw_xmit *phwxmits = padapter->xmitpriv.hwxmits;
1846 phead = get_list_head(pframequeue);
1847 plist = phead->next;
1849 while (phead != plist) {
1850 pxmitframe = container_of(plist, struct xmit_frame, list);
1852 plist = plist->next;
1854 xmitframe_enqueue_for_sleeping_sta(padapter, pxmitframe);
1856 pattrib = &pxmitframe->attrib;
1858 ptxservq = rtw_get_sta_pending(padapter, psta, pattrib->priority, (u8 *)(&ac_index));
1861 phwxmits[ac_index].accnt--;
1865 void stop_sta_xmit(struct adapter *padapter, struct sta_info *psta)
1867 struct sta_info *psta_bmc;
1868 struct sta_xmit_priv *pstaxmitpriv;
1869 struct sta_priv *pstapriv = &padapter->stapriv;
1870 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1872 pstaxmitpriv = &psta->sta_xmitpriv;
1874 /* for BC/MC Frames */
1875 psta_bmc = rtw_get_bcmc_stainfo(padapter);
1877 spin_lock_bh(&pxmitpriv->lock);
1879 psta->state |= WIFI_SLEEP_STATE;
1881 pstapriv->sta_dz_bitmap |= BIT(psta->aid);
1883 dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->vo_q.sta_pending);
1884 list_del_init(&pstaxmitpriv->vo_q.tx_pending);
1886 dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->vi_q.sta_pending);
1887 list_del_init(&pstaxmitpriv->vi_q.tx_pending);
1889 dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->be_q.sta_pending);
1890 list_del_init(&pstaxmitpriv->be_q.tx_pending);
1892 dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->bk_q.sta_pending);
1893 list_del_init(&pstaxmitpriv->bk_q.tx_pending);
1895 /* for BC/MC Frames */
1896 pstaxmitpriv = &psta_bmc->sta_xmitpriv;
1897 dequeue_xmitframes_to_sleeping_queue(padapter, psta_bmc, &pstaxmitpriv->be_q.sta_pending);
1898 list_del_init(&pstaxmitpriv->be_q.tx_pending);
1900 spin_unlock_bh(&pxmitpriv->lock);
1903 void wakeup_sta_to_xmit(struct adapter *padapter, struct sta_info *psta)
1905 u8 update_mask = 0, wmmps_ac = 0;
1906 struct sta_info *psta_bmc;
1907 struct list_head *xmitframe_plist, *xmitframe_phead;
1908 struct xmit_frame *pxmitframe = NULL;
1909 struct sta_priv *pstapriv = &padapter->stapriv;
1911 spin_lock_bh(&psta->sleep_q.lock);
1913 xmitframe_phead = get_list_head(&psta->sleep_q);
1914 xmitframe_plist = xmitframe_phead->next;
1916 while (xmitframe_phead != xmitframe_plist) {
1917 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
1919 xmitframe_plist = xmitframe_plist->next;
1921 list_del_init(&pxmitframe->list);
1923 switch (pxmitframe->attrib.priority) {
1926 wmmps_ac = psta->uapsd_bk & BIT(1);
1930 wmmps_ac = psta->uapsd_vi & BIT(1);
1934 wmmps_ac = psta->uapsd_vo & BIT(1);
1939 wmmps_ac = psta->uapsd_be & BIT(1);
1944 if (psta->sleepq_len > 0)
1945 pxmitframe->attrib.mdata = 1;
1947 pxmitframe->attrib.mdata = 0;
1950 psta->sleepq_ac_len--;
1951 if (psta->sleepq_ac_len > 0) {
1952 pxmitframe->attrib.mdata = 1;
1953 pxmitframe->attrib.eosp = 0;
1955 pxmitframe->attrib.mdata = 0;
1956 pxmitframe->attrib.eosp = 1;
1960 pxmitframe->attrib.triggered = 1;
1962 spin_unlock_bh(&psta->sleep_q.lock);
1963 if (rtl8188eu_hal_xmit(padapter, pxmitframe))
1964 rtw_os_xmit_complete(padapter, pxmitframe);
1965 spin_lock_bh(&psta->sleep_q.lock);
1968 if (psta->sleepq_len == 0) {
1969 pstapriv->tim_bitmap &= ~BIT(psta->aid);
1971 update_mask = BIT(0);
1973 if (psta->state & WIFI_SLEEP_STATE)
1974 psta->state ^= WIFI_SLEEP_STATE;
1976 if (psta->state & WIFI_STA_ALIVE_CHK_STATE) {
1977 psta->expire_to = pstapriv->expire_to;
1978 psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
1981 pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
1984 spin_unlock_bh(&psta->sleep_q.lock);
1986 /* for BC/MC Frames */
1987 psta_bmc = rtw_get_bcmc_stainfo(padapter);
1991 if ((pstapriv->sta_dz_bitmap & 0xfffe) == 0x0) { /* no any sta in ps mode */
1992 spin_lock_bh(&psta_bmc->sleep_q.lock);
1994 xmitframe_phead = get_list_head(&psta_bmc->sleep_q);
1995 xmitframe_plist = xmitframe_phead->next;
1997 while (xmitframe_phead != xmitframe_plist) {
1998 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
2000 xmitframe_plist = xmitframe_plist->next;
2002 list_del_init(&pxmitframe->list);
2004 psta_bmc->sleepq_len--;
2005 if (psta_bmc->sleepq_len > 0)
2006 pxmitframe->attrib.mdata = 1;
2008 pxmitframe->attrib.mdata = 0;
2010 pxmitframe->attrib.triggered = 1;
2012 spin_unlock_bh(&psta_bmc->sleep_q.lock);
2013 if (rtl8188eu_hal_xmit(padapter, pxmitframe))
2014 rtw_os_xmit_complete(padapter, pxmitframe);
2015 spin_lock_bh(&psta_bmc->sleep_q.lock);
2018 if (psta_bmc->sleepq_len == 0) {
2019 pstapriv->tim_bitmap &= ~BIT(0);
2020 pstapriv->sta_dz_bitmap &= ~BIT(0);
2022 update_mask |= BIT(1);
2025 spin_unlock_bh(&psta_bmc->sleep_q.lock);
2029 update_beacon(padapter, _TIM_IE_, NULL, false);
2032 void xmit_delivery_enabled_frames(struct adapter *padapter, struct sta_info *psta)
2035 struct list_head *xmitframe_plist, *xmitframe_phead;
2036 struct xmit_frame *pxmitframe = NULL;
2037 struct sta_priv *pstapriv = &padapter->stapriv;
2039 spin_lock_bh(&psta->sleep_q.lock);
2041 xmitframe_phead = get_list_head(&psta->sleep_q);
2042 xmitframe_plist = xmitframe_phead->next;
2044 while (xmitframe_phead != xmitframe_plist) {
2045 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
2047 xmitframe_plist = xmitframe_plist->next;
2049 switch (pxmitframe->attrib.priority) {
2052 wmmps_ac = psta->uapsd_bk & BIT(1);
2056 wmmps_ac = psta->uapsd_vi & BIT(1);
2060 wmmps_ac = psta->uapsd_vo & BIT(1);
2065 wmmps_ac = psta->uapsd_be & BIT(1);
2072 list_del_init(&pxmitframe->list);
2075 psta->sleepq_ac_len--;
2077 if (psta->sleepq_ac_len > 0) {
2078 pxmitframe->attrib.mdata = 1;
2079 pxmitframe->attrib.eosp = 0;
2081 pxmitframe->attrib.mdata = 0;
2082 pxmitframe->attrib.eosp = 1;
2085 pxmitframe->attrib.triggered = 1;
2087 if (rtl8188eu_hal_xmit(padapter, pxmitframe))
2088 rtw_os_xmit_complete(padapter, pxmitframe);
2090 if ((psta->sleepq_ac_len == 0) && (!psta->has_legacy_ac) && (wmmps_ac)) {
2091 pstapriv->tim_bitmap &= ~BIT(psta->aid);
2093 /* upate BCN for TIM IE */
2094 update_beacon(padapter, _TIM_IE_, NULL, false);
2098 spin_unlock_bh(&psta->sleep_q.lock);
2101 void rtw_sctx_init(struct submit_ctx *sctx, int timeout_ms)
2103 sctx->timeout_ms = timeout_ms;
2104 sctx->submit_time = jiffies;
2105 init_completion(&sctx->done);
2106 sctx->status = RTW_SCTX_SUBMITTED;
2109 int rtw_sctx_wait(struct submit_ctx *sctx)
2112 unsigned long expire;
2115 expire = sctx->timeout_ms ? msecs_to_jiffies(sctx->timeout_ms) : MAX_SCHEDULE_TIMEOUT;
2116 if (!wait_for_completion_timeout(&sctx->done, expire))
2117 /* timeout, do something?? */
2118 status = RTW_SCTX_DONE_TIMEOUT;
2120 status = sctx->status;
2122 if (status == RTW_SCTX_DONE_SUCCESS)
2128 void rtw_sctx_done_err(struct submit_ctx **sctx, int status)
2131 (*sctx)->status = status;
2132 complete(&((*sctx)->done));
2137 int rtw_ack_tx_wait(struct xmit_priv *pxmitpriv, u32 timeout_ms)
2139 struct submit_ctx *pack_tx_ops = &pxmitpriv->ack_tx_ops;
2141 pack_tx_ops->submit_time = jiffies;
2142 pack_tx_ops->timeout_ms = timeout_ms;
2143 pack_tx_ops->status = RTW_SCTX_SUBMITTED;
2145 return rtw_sctx_wait(pack_tx_ops);
2148 void rtw_ack_tx_done(struct xmit_priv *pxmitpriv, int status)
2150 struct submit_ctx *pack_tx_ops = &pxmitpriv->ack_tx_ops;
2152 if (pxmitpriv->ack_tx)
2153 rtw_sctx_done_err(&pack_tx_ops, status);