1 /******************************************************************************
3 * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 ******************************************************************************/
17 #include <osdep_service.h>
18 #include <drv_types.h>
21 #include <osdep_intf.h>
22 #include <linux/vmalloc.h>
24 static u8 P802_1H_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0xf8 };
25 static u8 RFC1042_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0x00 };
27 static void _init_txservq(struct tx_servq *ptxservq)
29 INIT_LIST_HEAD(&ptxservq->tx_pending);
30 _rtw_init_queue(&ptxservq->sta_pending);
34 void _rtw_init_sta_xmit_priv(struct sta_xmit_priv *psta_xmitpriv)
36 memset((unsigned char *)psta_xmitpriv, 0, sizeof(struct sta_xmit_priv));
37 spin_lock_init(&psta_xmitpriv->lock);
38 _init_txservq(&psta_xmitpriv->be_q);
39 _init_txservq(&psta_xmitpriv->bk_q);
40 _init_txservq(&psta_xmitpriv->vi_q);
41 _init_txservq(&psta_xmitpriv->vo_q);
42 INIT_LIST_HEAD(&psta_xmitpriv->legacy_dz);
43 INIT_LIST_HEAD(&psta_xmitpriv->apsd);
47 s32 _rtw_init_xmit_priv(struct xmit_priv *pxmitpriv, struct adapter *padapter)
50 struct xmit_buf *pxmitbuf;
51 struct xmit_frame *pxframe;
53 u32 max_xmit_extbuf_size = MAX_XMIT_EXTBUF_SZ;
54 u32 num_xmit_extbuf = NR_XMIT_EXTBUFF;
57 /* We don't need to memset padapter->XXX to zero, because adapter is allocated by vzalloc(). */
59 spin_lock_init(&pxmitpriv->lock);
62 Please insert all the queue initializaiton using _rtw_init_queue below
65 pxmitpriv->adapter = padapter;
67 _rtw_init_queue(&pxmitpriv->be_pending);
68 _rtw_init_queue(&pxmitpriv->bk_pending);
69 _rtw_init_queue(&pxmitpriv->vi_pending);
70 _rtw_init_queue(&pxmitpriv->vo_pending);
71 _rtw_init_queue(&pxmitpriv->bm_pending);
73 _rtw_init_queue(&pxmitpriv->free_xmit_queue);
76 Please allocate memory with the sz = (struct xmit_frame) * NR_XMITFRAME,
77 and initialize free_xmit_frame below.
78 Please also apply free_txobj to link_up all the xmit_frames...
81 pxmitpriv->pallocated_frame_buf = vzalloc(NR_XMITFRAME * sizeof(struct xmit_frame) + 4);
83 if (pxmitpriv->pallocated_frame_buf == NULL) {
84 pxmitpriv->pxmit_frame_buf = NULL;
85 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("alloc xmit_frame fail!\n"));
89 pxmitpriv->pxmit_frame_buf = PTR_ALIGN(pxmitpriv->pallocated_frame_buf, 4);
90 /* pxmitpriv->pxmit_frame_buf = pxmitpriv->pallocated_frame_buf + 4 - */
91 /* ((size_t) (pxmitpriv->pallocated_frame_buf) &3); */
93 pxframe = (struct xmit_frame *)pxmitpriv->pxmit_frame_buf;
95 for (i = 0; i < NR_XMITFRAME; i++) {
96 INIT_LIST_HEAD(&(pxframe->list));
98 pxframe->padapter = padapter;
99 pxframe->frame_tag = NULL_FRAMETAG;
103 pxframe->buf_addr = NULL;
104 pxframe->pxmitbuf = NULL;
106 list_add_tail(&(pxframe->list), &(pxmitpriv->free_xmit_queue.queue));
111 pxmitpriv->free_xmitframe_cnt = NR_XMITFRAME;
113 pxmitpriv->frag_len = MAX_FRAG_THRESHOLD;
116 _rtw_init_queue(&pxmitpriv->free_xmitbuf_queue);
117 _rtw_init_queue(&pxmitpriv->pending_xmitbuf_queue);
119 pxmitpriv->pallocated_xmitbuf = vzalloc(NR_XMITBUFF * sizeof(struct xmit_buf) + 4);
121 if (pxmitpriv->pallocated_xmitbuf == NULL) {
122 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("alloc xmit_buf fail!\n"));
127 pxmitpriv->pxmitbuf = PTR_ALIGN(pxmitpriv->pallocated_xmitbuf, 4);
128 /* pxmitpriv->pxmitbuf = pxmitpriv->pallocated_xmitbuf + 4 - */
129 /* ((size_t) (pxmitpriv->pallocated_xmitbuf) &3); */
131 pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
133 for (i = 0; i < NR_XMITBUFF; i++) {
134 INIT_LIST_HEAD(&pxmitbuf->list);
136 pxmitbuf->priv_data = NULL;
137 pxmitbuf->padapter = padapter;
138 pxmitbuf->ext_tag = false;
140 /* Tx buf allocation may fail sometimes, so sleep and retry. */
141 res = rtw_os_xmit_resource_alloc(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ));
144 res = rtw_os_xmit_resource_alloc(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ));
149 pxmitbuf->flags = XMIT_VO_QUEUE;
151 list_add_tail(&pxmitbuf->list, &(pxmitpriv->free_xmitbuf_queue.queue));
155 pxmitpriv->free_xmitbuf_cnt = NR_XMITBUFF;
157 /* Init xmit extension buff */
158 _rtw_init_queue(&pxmitpriv->free_xmit_extbuf_queue);
160 pxmitpriv->pallocated_xmit_extbuf = vzalloc(num_xmit_extbuf * sizeof(struct xmit_buf) + 4);
162 if (pxmitpriv->pallocated_xmit_extbuf == NULL) {
163 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("alloc xmit_extbuf fail!\n"));
168 pxmitpriv->pxmit_extbuf = PTR_ALIGN(pxmitpriv->pallocated_xmit_extbuf, 4);
170 pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmit_extbuf;
172 for (i = 0; i < num_xmit_extbuf; i++) {
173 INIT_LIST_HEAD(&pxmitbuf->list);
175 pxmitbuf->priv_data = NULL;
176 pxmitbuf->padapter = padapter;
177 pxmitbuf->ext_tag = true;
179 res = rtw_os_xmit_resource_alloc(padapter, pxmitbuf, max_xmit_extbuf_size + XMITBUF_ALIGN_SZ);
185 list_add_tail(&pxmitbuf->list, &(pxmitpriv->free_xmit_extbuf_queue.queue));
189 pxmitpriv->free_xmit_extbuf_cnt = num_xmit_extbuf;
191 rtw_alloc_hwxmits(padapter);
192 rtw_init_hwxmits(pxmitpriv->hwxmits, pxmitpriv->hwxmit_entry);
194 for (i = 0; i < 4; i++)
195 pxmitpriv->wmm_para_seq[i] = i;
197 pxmitpriv->txirp_cnt = 1;
199 /* per AC pending irp */
200 pxmitpriv->beq_cnt = 0;
201 pxmitpriv->bkq_cnt = 0;
202 pxmitpriv->viq_cnt = 0;
203 pxmitpriv->voq_cnt = 0;
205 pxmitpriv->ack_tx = false;
206 mutex_init(&pxmitpriv->ack_tx_mutex);
207 rtw_sctx_init(&pxmitpriv->ack_tx_ops, 0);
209 rtw_hal_init_xmit_priv(padapter);
217 void _rtw_free_xmit_priv(struct xmit_priv *pxmitpriv)
220 struct adapter *padapter = pxmitpriv->adapter;
221 struct xmit_frame *pxmitframe = (struct xmit_frame *)pxmitpriv->pxmit_frame_buf;
222 struct xmit_buf *pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
223 u32 num_xmit_extbuf = NR_XMIT_EXTBUFF;
225 if (pxmitpriv->pxmit_frame_buf == NULL)
228 for (i = 0; i < NR_XMITFRAME; i++) {
229 rtw_os_xmit_complete(padapter, pxmitframe);
234 for (i = 0; i < NR_XMITBUFF; i++) {
235 rtw_os_xmit_resource_free(pxmitbuf);
239 vfree(pxmitpriv->pallocated_frame_buf);
240 vfree(pxmitpriv->pallocated_xmitbuf);
242 /* free xmit extension buff */
243 pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmit_extbuf;
244 for (i = 0; i < num_xmit_extbuf; i++) {
245 rtw_os_xmit_resource_free(pxmitbuf);
249 if (pxmitpriv->pallocated_xmit_extbuf)
250 vfree(pxmitpriv->pallocated_xmit_extbuf);
252 rtw_free_hwxmits(padapter);
254 mutex_destroy(&pxmitpriv->ack_tx_mutex);
257 static void update_attrib_vcs_info(struct adapter *padapter, struct xmit_frame *pxmitframe)
260 struct pkt_attrib *pattrib = &pxmitframe->attrib;
261 struct sta_info *psta = pattrib->psta;
262 struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
263 struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
265 if (pattrib->nr_frags != 1)
266 sz = padapter->xmitpriv.frag_len;
268 sz = pattrib->last_txcmdsz;
270 /* (1) RTS_Threshold is compared to the MPDU, not MSDU. */
271 /* (2) If there are more than one frag in this MSDU, only the first frag uses protection frame. */
272 /* Other fragments are protected by previous fragment. */
273 /* So we only need to check the length of first fragment. */
274 if (pmlmeext->cur_wireless_mode < WIRELESS_11_24N || padapter->registrypriv.wifi_spec) {
275 if (sz > padapter->registrypriv.rts_thresh) {
276 pattrib->vcs_mode = RTS_CTS;
279 pattrib->vcs_mode = RTS_CTS;
280 else if (psta->cts2self)
281 pattrib->vcs_mode = CTS_TO_SELF;
283 pattrib->vcs_mode = NONE_VCS;
288 if ((pmlmeinfo->assoc_AP_vendor == HT_IOT_PEER_ATHEROS) && pattrib->ampdu_en &&
289 (padapter->securitypriv.dot11PrivacyAlgrthm == _AES_)) {
290 pattrib->vcs_mode = CTS_TO_SELF;
294 /* check ERP protection */
295 if (psta->rtsen || psta->cts2self) {
297 pattrib->vcs_mode = RTS_CTS;
298 else if (psta->cts2self)
299 pattrib->vcs_mode = CTS_TO_SELF;
304 /* check HT op mode */
305 if (pattrib->ht_en) {
306 u8 htopmode = pmlmeinfo->HT_protection;
308 if ((pmlmeext->cur_bwmode && (htopmode == 2 || htopmode == 3)) ||
309 (!pmlmeext->cur_bwmode && htopmode == 3)) {
310 pattrib->vcs_mode = RTS_CTS;
316 if (sz > padapter->registrypriv.rts_thresh) {
317 pattrib->vcs_mode = RTS_CTS;
321 /* to do list: check MIMO power save condition. */
323 /* check AMPDU aggregation for TXOP */
324 if (pattrib->ampdu_en) {
325 pattrib->vcs_mode = RTS_CTS;
329 pattrib->vcs_mode = NONE_VCS;
335 static void update_attrib_phy_info(struct pkt_attrib *pattrib, struct sta_info *psta)
338 pattrib->vcs_mode = RTS_CTS;
339 else if (psta->cts2self)
340 pattrib->vcs_mode = CTS_TO_SELF;
342 pattrib->vcs_mode = NONE_VCS;*/
346 pattrib->triggered = 0;
348 /* qos_en, ht_en, init rate, , bw, ch_offset, sgi */
349 pattrib->qos_en = psta->qos_option;
351 pattrib->raid = psta->raid;
352 pattrib->ht_en = psta->htpriv.ht_option;
353 pattrib->bwmode = psta->htpriv.bwmode;
354 pattrib->ch_offset = psta->htpriv.ch_offset;
355 pattrib->sgi = psta->htpriv.sgi;
356 pattrib->ampdu_en = false;
357 pattrib->retry_ctrl = false;
360 u8 qos_acm(u8 acm_mask, u8 priority)
362 u8 change_priority = priority;
367 if (acm_mask & BIT(1))
375 if (acm_mask & BIT(2))
380 if (acm_mask & BIT(3))
384 DBG_88E("qos_acm(): invalid pattrib->priority: %d!!!\n", priority);
388 return change_priority;
391 static void set_qos(struct pkt_file *ppktfile, struct pkt_attrib *pattrib)
393 struct ethhdr etherhdr;
397 _rtw_open_pktfile(ppktfile->pkt, ppktfile);
398 _rtw_pktfile_read(ppktfile, (unsigned char *)ðerhdr, ETH_HLEN);
400 /* get user_prio from IP hdr */
401 if (pattrib->ether_type == 0x0800) {
402 _rtw_pktfile_read(ppktfile, (u8 *)&ip_hdr, sizeof(ip_hdr));
403 /* user_prio = (ntohs(ip_hdr.tos) >> 5) & 0x3; */
404 user_prio = ip_hdr.tos >> 5;
405 } else if (pattrib->ether_type == ETH_P_PAE) {
406 /* "When priority processing of data frames is supported, */
407 /* a STA's SME should send EAPOL-Key frames at the highest priority." */
411 pattrib->priority = user_prio;
412 pattrib->hdrlen = WLAN_HDR_A3_QOS_LEN;
413 pattrib->subtype = WIFI_QOS_DATA_TYPE;
416 static s32 update_attrib(struct adapter *padapter, struct sk_buff *pkt, struct pkt_attrib *pattrib)
418 struct pkt_file pktfile;
419 struct sta_info *psta = NULL;
420 struct ethhdr etherhdr;
423 struct sta_priv *pstapriv = &padapter->stapriv;
424 struct security_priv *psecuritypriv = &padapter->securitypriv;
425 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
426 struct qos_priv *pqospriv = &pmlmepriv->qospriv;
430 _rtw_open_pktfile(pkt, &pktfile);
431 _rtw_pktfile_read(&pktfile, (u8 *)ðerhdr, ETH_HLEN);
433 pattrib->ether_type = ntohs(etherhdr.h_proto);
435 memcpy(pattrib->dst, ðerhdr.h_dest, ETH_ALEN);
436 memcpy(pattrib->src, ðerhdr.h_source, ETH_ALEN);
440 if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) ||
441 check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
442 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
443 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
444 } else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
445 memcpy(pattrib->ra, get_bssid(pmlmepriv), ETH_ALEN);
446 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
447 } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
448 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
449 memcpy(pattrib->ta, get_bssid(pmlmepriv), ETH_ALEN);
452 pattrib->pktlen = pktfile.pkt_len;
454 if (pattrib->ether_type == ETH_P_IP) {
455 /* The following is for DHCP and ARP packet, we use cck1M to tx these packets and let LPS awake some time */
456 /* to prevent DHCP protocol fail */
459 _rtw_pktfile_read(&pktfile, &tmp[0], 24);
460 pattrib->dhcp_pkt = 0;
461 if (pktfile.pkt_len > 282) {/* MINIMUM_DHCP_PACKET_SIZE) { */
462 if (pattrib->ether_type == ETH_P_IP) {/* IP header */
463 if (((tmp[21] == 68) && (tmp[23] == 67)) ||
464 ((tmp[21] == 67) && (tmp[23] == 68))) {
465 /* 68 : UDP BOOTP client */
466 /* 67 : UDP BOOTP server */
467 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("====================== update_attrib: get DHCP Packet\n"));
468 /* Use low rate to send DHCP packet. */
469 pattrib->dhcp_pkt = 1;
473 } else if (pattrib->ether_type == ETH_P_PAE) {
474 DBG_88E_LEVEL(_drv_info_, "send eapol packet\n");
477 if ((pattrib->ether_type == ETH_P_PAE) || (pattrib->dhcp_pkt == 1))
478 rtw_set_scan_deny(padapter, 3000);
480 /* If EAPOL , ARP , OR DHCP packet, driver must be in active mode. */
481 if ((pattrib->ether_type == ETH_P_ARP) || (pattrib->ether_type == ETH_P_PAE) || (pattrib->dhcp_pkt == 1))
482 rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_SPECIAL_PACKET, 1);
484 bmcast = IS_MCAST(pattrib->ra);
488 psta = rtw_get_bcmc_stainfo(padapter);
490 psta = rtw_get_stainfo(pstapriv, pattrib->ra);
491 if (psta == NULL) { /* if we cannot get psta => drrp the pkt */
492 RT_TRACE(_module_rtl871x_xmit_c_, _drv_alert_, ("\nupdate_attrib => get sta_info fail, ra: %pM\n", (pattrib->ra)));
495 } else if ((check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) && (!(psta->state & _FW_LINKED))) {
502 pattrib->mac_id = psta->mac_id;
503 /* DBG_88E("%s ==> mac_id(%d)\n", __func__, pattrib->mac_id); */
504 pattrib->psta = psta;
506 /* if we cannot get psta => drop the pkt */
507 RT_TRACE(_module_rtl871x_xmit_c_, _drv_alert_, ("\nupdate_attrib => get sta_info fail, ra:%pM\n", (pattrib->ra)));
512 pattrib->ack_policy = 0;
514 pattrib->hdrlen = WLAN_HDR_A3_LEN;
515 pattrib->subtype = WIFI_DATA_TYPE;
516 pattrib->priority = 0;
518 if (check_fwstate(pmlmepriv, WIFI_AP_STATE|WIFI_ADHOC_STATE|WIFI_ADHOC_MASTER_STATE)) {
519 if (psta->qos_option)
520 set_qos(&pktfile, pattrib);
522 if (pqospriv->qos_option) {
523 set_qos(&pktfile, pattrib);
525 if (pmlmepriv->acm_mask != 0)
526 pattrib->priority = qos_acm(pmlmepriv->acm_mask, pattrib->priority);
530 if (psta->ieee8021x_blocked) {
531 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("\n psta->ieee8021x_blocked == true\n"));
533 pattrib->encrypt = 0;
535 if ((pattrib->ether_type != ETH_P_PAE) && !check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
536 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("\npsta->ieee8021x_blocked == true, pattrib->ether_type(%.4x) != ETH_P_PAE\n", pattrib->ether_type));
541 GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt, bmcast);
543 switch (psecuritypriv->dot11AuthAlgrthm) {
544 case dot11AuthAlgrthm_Open:
545 case dot11AuthAlgrthm_Shared:
546 case dot11AuthAlgrthm_Auto:
547 pattrib->key_idx = (u8)psecuritypriv->dot11PrivacyKeyIndex;
549 case dot11AuthAlgrthm_8021X:
551 pattrib->key_idx = (u8)psecuritypriv->dot118021XGrpKeyid;
553 pattrib->key_idx = 0;
556 pattrib->key_idx = 0;
561 switch (pattrib->encrypt) {
565 pattrib->icv_len = 4;
569 pattrib->icv_len = 4;
571 if (padapter->securitypriv.busetkipkey == _FAIL) {
572 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_,
573 ("\npadapter->securitypriv.busetkipkey(%d) == _FAIL drop packet\n",
574 padapter->securitypriv.busetkipkey));
580 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("pattrib->encrypt=%d (_AES_)\n", pattrib->encrypt));
582 pattrib->icv_len = 8;
586 pattrib->icv_len = 0;
590 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_,
591 ("update_attrib: encrypt=%d securitypriv.sw_encrypt=%d\n",
592 pattrib->encrypt, padapter->securitypriv.sw_encrypt));
594 if (pattrib->encrypt &&
595 (padapter->securitypriv.sw_encrypt || !psecuritypriv->hw_decrypted)) {
596 pattrib->bswenc = true;
597 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_,
598 ("update_attrib: encrypt=%d securitypriv.hw_decrypted=%d bswenc = true\n",
599 pattrib->encrypt, padapter->securitypriv.sw_encrypt));
601 pattrib->bswenc = false;
602 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("update_attrib: bswenc = false\n"));
605 update_attrib_phy_info(pattrib, psta);
613 static s32 xmitframe_addmic(struct adapter *padapter, struct xmit_frame *pxmitframe)
615 int curfragnum, length;
616 u8 *pframe, *payload, mic[8];
617 struct mic_data micdata;
618 struct sta_info *stainfo;
619 struct pkt_attrib *pattrib = &pxmitframe->attrib;
620 struct security_priv *psecuritypriv = &padapter->securitypriv;
621 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
622 u8 priority[4] = {0x0, 0x0, 0x0, 0x0};
623 u8 hw_hdr_offset = 0;
624 int bmcst = IS_MCAST(pattrib->ra);
627 stainfo = pattrib->psta;
629 stainfo = rtw_get_stainfo(&padapter->stapriv, &pattrib->ra[0]);
632 hw_hdr_offset = TXDESC_SIZE + (pxmitframe->pkt_offset * PACKET_OFFSET_SZ);
634 if (pattrib->encrypt == _TKIP_) {/* if (psecuritypriv->dot11PrivacyAlgrthm == _TKIP_PRIVACY_) */
635 /* encode mic code */
636 if (stainfo != NULL) {
637 u8 null_key[16] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
638 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
641 pframe = pxmitframe->buf_addr + hw_hdr_offset;
644 if (!memcmp(psecuritypriv->dot118021XGrptxmickey[psecuritypriv->dot118021XGrpKeyid].skey, null_key, 16))
646 /* start to calculate the mic code */
647 rtw_secmicsetkey(&micdata, psecuritypriv->dot118021XGrptxmickey[psecuritypriv->dot118021XGrpKeyid].skey);
649 if (!memcmp(&stainfo->dot11tkiptxmickey.skey[0], null_key, 16)) {
650 /* DbgPrint("\nxmitframe_addmic:stainfo->dot11tkiptxmickey == 0\n"); */
654 /* start to calculate the mic code */
655 rtw_secmicsetkey(&micdata, &stainfo->dot11tkiptxmickey.skey[0]);
658 if (pframe[1]&1) { /* ToDS == 1 */
659 rtw_secmicappend(&micdata, &pframe[16], 6); /* DA */
660 if (pframe[1]&2) /* From Ds == 1 */
661 rtw_secmicappend(&micdata, &pframe[24], 6);
663 rtw_secmicappend(&micdata, &pframe[10], 6);
664 } else { /* ToDS == 0 */
665 rtw_secmicappend(&micdata, &pframe[4], 6); /* DA */
666 if (pframe[1]&2) /* From Ds == 1 */
667 rtw_secmicappend(&micdata, &pframe[16], 6);
669 rtw_secmicappend(&micdata, &pframe[10], 6);
673 priority[0] = (u8)pxmitframe->attrib.priority;
675 rtw_secmicappend(&micdata, &priority[0], 4);
679 for (curfragnum = 0; curfragnum < pattrib->nr_frags; curfragnum++) {
680 payload = (u8 *)round_up((size_t)(payload), 4);
681 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_,
682 ("=== curfragnum=%d, pframe = 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x,!!!\n",
683 curfragnum, *payload, *(payload+1),
684 *(payload+2), *(payload+3),
685 *(payload+4), *(payload+5),
686 *(payload+6), *(payload+7)));
688 payload = payload+pattrib->hdrlen+pattrib->iv_len;
689 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_,
690 ("curfragnum=%d pattrib->hdrlen=%d pattrib->iv_len=%d",
691 curfragnum, pattrib->hdrlen, pattrib->iv_len));
692 if ((curfragnum+1) == pattrib->nr_frags) {
693 length = pattrib->last_txcmdsz-pattrib->hdrlen-pattrib->iv_len-((pattrib->bswenc) ? pattrib->icv_len : 0);
694 rtw_secmicappend(&micdata, payload, length);
695 payload = payload+length;
697 length = pxmitpriv->frag_len-pattrib->hdrlen-pattrib->iv_len-((pattrib->bswenc) ? pattrib->icv_len : 0);
698 rtw_secmicappend(&micdata, payload, length);
699 payload = payload+length+pattrib->icv_len;
700 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("curfragnum=%d length=%d pattrib->icv_len=%d", curfragnum, length, pattrib->icv_len));
703 rtw_secgetmic(&micdata, &(mic[0]));
704 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("xmitframe_addmic: before add mic code!!!\n"));
705 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("xmitframe_addmic: pattrib->last_txcmdsz=%d!!!\n", pattrib->last_txcmdsz));
706 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("xmitframe_addmic: mic[0]=0x%.2x , mic[1]=0x%.2x , mic[2]= 0x%.2x, mic[3]=0x%.2x\n\
707 mic[4]= 0x%.2x , mic[5]= 0x%.2x , mic[6]= 0x%.2x , mic[7]= 0x%.2x !!!!\n",
708 mic[0], mic[1], mic[2], mic[3], mic[4], mic[5], mic[6], mic[7]));
709 /* add mic code and add the mic code length in last_txcmdsz */
711 memcpy(payload, &(mic[0]), 8);
712 pattrib->last_txcmdsz += 8;
714 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("\n ======== last pkt ========\n"));
715 payload = payload-pattrib->last_txcmdsz+8;
716 for (curfragnum = 0; curfragnum < pattrib->last_txcmdsz; curfragnum = curfragnum+8)
717 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_,
718 (" %.2x, %.2x, %.2x, %.2x, %.2x, %.2x, %.2x, %.2x ",
719 *(payload+curfragnum), *(payload+curfragnum+1),
720 *(payload+curfragnum+2), *(payload+curfragnum+3),
721 *(payload+curfragnum+4), *(payload+curfragnum+5),
722 *(payload+curfragnum+6), *(payload+curfragnum+7)));
724 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("xmitframe_addmic: rtw_get_stainfo==NULL!!!\n"));
732 static s32 xmitframe_swencrypt(struct adapter *padapter, struct xmit_frame *pxmitframe)
734 struct pkt_attrib *pattrib = &pxmitframe->attrib;
737 if (pattrib->bswenc) {
738 RT_TRACE(_module_rtl871x_xmit_c_, _drv_alert_, ("### xmitframe_swencrypt\n"));
739 switch (pattrib->encrypt) {
742 rtw_wep_encrypt(padapter, (u8 *)pxmitframe);
745 rtw_tkip_encrypt(padapter, (u8 *)pxmitframe);
748 rtw_aes_encrypt(padapter, (u8 *)pxmitframe);
754 RT_TRACE(_module_rtl871x_xmit_c_, _drv_notice_, ("### xmitframe_hwencrypt\n"));
761 s32 rtw_make_wlanhdr(struct adapter *padapter, u8 *hdr, struct pkt_attrib *pattrib)
765 struct ieee80211_hdr *pwlanhdr = (struct ieee80211_hdr *)hdr;
766 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
767 struct qos_priv *pqospriv = &pmlmepriv->qospriv;
768 u8 qos_option = false;
771 __le16 *fctrl = &pwlanhdr->frame_control;
773 struct sta_info *psta;
775 int bmcst = IS_MCAST(pattrib->ra);
779 psta = pattrib->psta;
782 psta = rtw_get_bcmc_stainfo(padapter);
784 psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
787 memset(hdr, 0, WLANHDR_OFFSET);
789 SetFrameSubType(fctrl, pattrib->subtype);
791 if (pattrib->subtype & WIFI_DATA_TYPE) {
792 if ((check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true)) {
793 /* to_ds = 1, fr_ds = 0; */
794 /* Data transfer to AP */
796 memcpy(pwlanhdr->addr1, get_bssid(pmlmepriv), ETH_ALEN);
797 memcpy(pwlanhdr->addr2, pattrib->src, ETH_ALEN);
798 memcpy(pwlanhdr->addr3, pattrib->dst, ETH_ALEN);
800 if (pqospriv->qos_option)
802 } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
803 /* to_ds = 0, fr_ds = 1; */
805 memcpy(pwlanhdr->addr1, pattrib->dst, ETH_ALEN);
806 memcpy(pwlanhdr->addr2, get_bssid(pmlmepriv), ETH_ALEN);
807 memcpy(pwlanhdr->addr3, pattrib->src, ETH_ALEN);
809 if (psta->qos_option)
811 } else if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) ||
812 check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
813 memcpy(pwlanhdr->addr1, pattrib->dst, ETH_ALEN);
814 memcpy(pwlanhdr->addr2, pattrib->src, ETH_ALEN);
815 memcpy(pwlanhdr->addr3, get_bssid(pmlmepriv), ETH_ALEN);
817 if (psta->qos_option)
820 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("fw_state:%x is not allowed to xmit frame\n", get_fwstate(pmlmepriv)));
828 if (pattrib->encrypt)
832 qc = (unsigned short *)(hdr + pattrib->hdrlen - 2);
834 if (pattrib->priority)
835 SetPriority(qc, pattrib->priority);
837 SetEOSP(qc, pattrib->eosp);
839 SetAckpolicy(qc, pattrib->ack_policy);
842 /* TODO: fill HT Control Field */
844 /* Update Seq Num will be handled by f/w */
846 psta->sta_xmitpriv.txseq_tid[pattrib->priority]++;
847 psta->sta_xmitpriv.txseq_tid[pattrib->priority] &= 0xFFF;
849 pattrib->seqnum = psta->sta_xmitpriv.txseq_tid[pattrib->priority];
851 SetSeqNum(hdr, pattrib->seqnum);
853 /* check if enable ampdu */
854 if (pattrib->ht_en && psta->htpriv.ampdu_enable) {
855 if (psta->htpriv.agg_enable_bitmap & BIT(pattrib->priority))
856 pattrib->ampdu_en = true;
859 /* re-check if enable ampdu by BA_starting_seqctrl */
860 if (pattrib->ampdu_en) {
863 tx_seq = psta->BA_starting_seqctrl[pattrib->priority & 0x0f];
865 /* check BA_starting_seqctrl */
866 if (SN_LESS(pattrib->seqnum, tx_seq)) {
867 pattrib->ampdu_en = false;/* AGG BK */
868 } else if (SN_EQUAL(pattrib->seqnum, tx_seq)) {
869 psta->BA_starting_seqctrl[pattrib->priority & 0x0f] = (tx_seq+1)&0xfff;
871 pattrib->ampdu_en = true;/* AGG EN */
873 psta->BA_starting_seqctrl[pattrib->priority & 0x0f] = (pattrib->seqnum+1)&0xfff;
874 pattrib->ampdu_en = true;/* AGG EN */
884 s32 rtw_txframes_pending(struct adapter *padapter)
886 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
888 return (!list_empty(&pxmitpriv->be_pending.queue) ||
889 !list_empty(&pxmitpriv->bk_pending.queue) ||
890 !list_empty(&pxmitpriv->vi_pending.queue) ||
891 !list_empty(&pxmitpriv->vo_pending.queue));
894 s32 rtw_txframes_sta_ac_pending(struct adapter *padapter, struct pkt_attrib *pattrib)
896 struct sta_info *psta;
897 struct tx_servq *ptxservq;
898 int priority = pattrib->priority;
900 psta = pattrib->psta;
905 ptxservq = &(psta->sta_xmitpriv.bk_q);
909 ptxservq = &(psta->sta_xmitpriv.vi_q);
913 ptxservq = &(psta->sta_xmitpriv.vo_q);
918 ptxservq = &(psta->sta_xmitpriv.be_q);
922 return ptxservq->qcnt;
926 * Calculate wlan 802.11 packet MAX size from pkt_attrib
927 * This function doesn't consider fragment case
929 u32 rtw_calculate_wlan_pkt_size_by_attribue(struct pkt_attrib *pattrib)
933 len = pattrib->hdrlen + pattrib->iv_len; /* WLAN Header and IV */
934 len += SNAP_SIZE + sizeof(u16); /* LLC */
935 len += pattrib->pktlen;
936 if (pattrib->encrypt == _TKIP_)
938 len += ((pattrib->bswenc) ? pattrib->icv_len : 0); /* ICV */
945 This sub-routine will perform all the following:
947 1. remove 802.3 header.
948 2. create wlan_header, based on the info in pxmitframe
949 3. append sta's iv/ext-iv
951 5. move frag chunk from pframe to pxmitframe->mem
952 6. apply sw-encrypt, if necessary.
955 s32 rtw_xmitframe_coalesce(struct adapter *padapter, struct sk_buff *pkt, struct xmit_frame *pxmitframe)
957 struct pkt_file pktfile;
958 s32 frg_inx, frg_len, mpdu_len, llc_sz, mem_sz;
960 u8 *pframe, *mem_start;
962 struct sta_info *psta;
963 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
964 struct pkt_attrib *pattrib = &pxmitframe->attrib;
966 s32 bmcst = IS_MCAST(pattrib->ra);
970 psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
975 if (pxmitframe->buf_addr == NULL) {
976 DBG_88E("==> %s buf_addr == NULL\n", __func__);
980 pbuf_start = pxmitframe->buf_addr;
982 hw_hdr_offset = TXDESC_SIZE + (pxmitframe->pkt_offset * PACKET_OFFSET_SZ);
984 mem_start = pbuf_start + hw_hdr_offset;
986 if (rtw_make_wlanhdr(padapter, mem_start, pattrib) == _FAIL) {
987 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("rtw_xmitframe_coalesce: rtw_make_wlanhdr fail; drop pkt\n"));
988 DBG_88E("rtw_xmitframe_coalesce: rtw_make_wlanhdr fail; drop pkt\n");
993 _rtw_open_pktfile(pkt, &pktfile);
994 _rtw_pktfile_read(&pktfile, NULL, ETH_HLEN);
997 frg_len = pxmitpriv->frag_len - 4;/* 2346-4 = 2342 */
1006 SetMFrag(mem_start);
1008 pframe += pattrib->hdrlen;
1009 mpdu_len -= pattrib->hdrlen;
1011 /* adding icv, if necessary... */
1012 if (pattrib->iv_len) {
1013 switch (pattrib->encrypt) {
1016 WEP_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
1020 TKIP_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
1022 TKIP_IV(pattrib->iv, psta->dot11txpn, 0);
1026 AES_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
1028 AES_IV(pattrib->iv, psta->dot11txpn, 0);
1032 memcpy(pframe, pattrib->iv, pattrib->iv_len);
1034 RT_TRACE(_module_rtl871x_xmit_c_, _drv_notice_,
1035 ("rtw_xmitframe_coalesce: keyid=%d pattrib->iv[3]=%.2x pframe=%.2x %.2x %.2x %.2x\n",
1036 padapter->securitypriv.dot11PrivacyKeyIndex, pattrib->iv[3], *pframe, *(pframe+1), *(pframe+2), *(pframe+3)));
1038 pframe += pattrib->iv_len;
1040 mpdu_len -= pattrib->iv_len;
1044 llc_sz = rtw_put_snap(pframe, pattrib->ether_type);
1049 if ((pattrib->icv_len > 0) && (pattrib->bswenc))
1050 mpdu_len -= pattrib->icv_len;
1053 /* don't do fragment to broadcat/multicast packets */
1054 mem_sz = _rtw_pktfile_read(&pktfile, pframe, pattrib->pktlen);
1056 mem_sz = _rtw_pktfile_read(&pktfile, pframe, mpdu_len);
1061 if ((pattrib->icv_len > 0) && (pattrib->bswenc)) {
1062 memcpy(pframe, pattrib->icv, pattrib->icv_len);
1063 pframe += pattrib->icv_len;
1068 if (bmcst || pktfile.pkt_len == 0) {
1069 pattrib->nr_frags = frg_inx;
1071 pattrib->last_txcmdsz = pattrib->hdrlen + pattrib->iv_len + ((pattrib->nr_frags == 1) ? llc_sz : 0) +
1072 ((pattrib->bswenc) ? pattrib->icv_len : 0) + mem_sz;
1074 ClearMFrag(mem_start);
1078 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("%s: There're still something in packet!\n", __func__));
1081 addr = (size_t)(pframe);
1083 mem_start = (unsigned char *)round_up(addr, 4) + hw_hdr_offset;
1084 memcpy(mem_start, pbuf_start + hw_hdr_offset, pattrib->hdrlen);
1087 /* Frame is about to be encrypted. Forward it to the monitor first. */
1088 rtl88eu_mon_xmit_hook(padapter->pmondev, pxmitframe, frg_len);
1090 if (xmitframe_addmic(padapter, pxmitframe) == _FAIL) {
1091 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("xmitframe_addmic(padapter, pxmitframe) == _FAIL\n"));
1092 DBG_88E("xmitframe_addmic(padapter, pxmitframe) == _FAIL\n");
1097 xmitframe_swencrypt(padapter, pxmitframe);
1100 update_attrib_vcs_info(padapter, pxmitframe);
1102 pattrib->vcs_mode = NONE_VCS;
1110 /* Logical Link Control(LLC) SubNetwork Attachment Point(SNAP) header
1111 * IEEE LLC/SNAP header contains 8 octets
1112 * First 3 octets comprise the LLC portion
1113 * SNAP portion, 5 octets, is divided into two fields:
1114 * Organizationally Unique Identifier(OUI), 3 octets,
1115 * type, defined by that organization, 2 octets.
1117 s32 rtw_put_snap(u8 *data, u16 h_proto)
1119 struct ieee80211_snap_hdr *snap;
1123 snap = (struct ieee80211_snap_hdr *)data;
1128 if (h_proto == 0x8137 || h_proto == 0x80f3)
1133 snap->oui[0] = oui[0];
1134 snap->oui[1] = oui[1];
1135 snap->oui[2] = oui[2];
1137 *(__be16 *)(data + SNAP_SIZE) = htons(h_proto);
1140 return SNAP_SIZE + sizeof(u16);
1143 void rtw_update_protection(struct adapter *padapter, u8 *ie, uint ie_len)
1148 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1149 struct registry_priv *pregistrypriv = &padapter->registrypriv;
1152 switch (pxmitpriv->vcs_setting) {
1154 pxmitpriv->vcs = NONE_VCS;
1160 perp = rtw_get_ie(ie, _ERPINFO_IE_, &erp_len, ie_len);
1162 pxmitpriv->vcs = NONE_VCS;
1164 protection = (*(perp + 2)) & BIT(1);
1166 if (pregistrypriv->vcs_type == RTS_CTS)
1167 pxmitpriv->vcs = RTS_CTS;
1169 pxmitpriv->vcs = CTS_TO_SELF;
1171 pxmitpriv->vcs = NONE_VCS;
1179 void rtw_count_tx_stats(struct adapter *padapter, struct xmit_frame *pxmitframe, int sz)
1181 struct sta_info *psta = NULL;
1182 struct stainfo_stats *pstats = NULL;
1183 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1184 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1186 if ((pxmitframe->frame_tag&0x0f) == DATA_FRAMETAG) {
1187 pxmitpriv->tx_bytes += sz;
1188 pmlmepriv->LinkDetectInfo.NumTxOkInPeriod += pxmitframe->agg_num;
1190 psta = pxmitframe->attrib.psta;
1192 pstats = &psta->sta_stats;
1193 pstats->tx_pkts += pxmitframe->agg_num;
1194 pstats->tx_bytes += sz;
1199 struct xmit_buf *rtw_alloc_xmitbuf_ext(struct xmit_priv *pxmitpriv)
1202 struct xmit_buf *pxmitbuf;
1203 struct __queue *pfree_queue = &pxmitpriv->free_xmit_extbuf_queue;
1205 spin_lock_irqsave(&pfree_queue->lock, irql);
1206 pxmitbuf = list_first_entry_or_null(&pfree_queue->queue,
1207 struct xmit_buf, list);
1209 list_del_init(&pxmitbuf->list);
1210 pxmitpriv->free_xmit_extbuf_cnt--;
1211 pxmitbuf->priv_data = NULL;
1212 /* pxmitbuf->ext_tag = true; */
1213 if (pxmitbuf->sctx) {
1214 DBG_88E("%s pxmitbuf->sctx is not NULL\n", __func__);
1215 rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_ALLOC);
1218 spin_unlock_irqrestore(&pfree_queue->lock, irql);
1223 s32 rtw_free_xmitbuf_ext(struct xmit_priv *pxmitpriv, struct xmit_buf *pxmitbuf)
1226 struct __queue *pfree_queue = &pxmitpriv->free_xmit_extbuf_queue;
1229 if (pxmitbuf == NULL)
1232 spin_lock_irqsave(&pfree_queue->lock, irql);
1234 list_del_init(&pxmitbuf->list);
1236 list_add_tail(&(pxmitbuf->list), get_list_head(pfree_queue));
1237 pxmitpriv->free_xmit_extbuf_cnt++;
1239 spin_unlock_irqrestore(&pfree_queue->lock, irql);
1245 struct xmit_buf *rtw_alloc_xmitbuf(struct xmit_priv *pxmitpriv)
1248 struct xmit_buf *pxmitbuf;
1249 struct __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
1251 /* DBG_88E("+rtw_alloc_xmitbuf\n"); */
1253 spin_lock_irqsave(&pfree_xmitbuf_queue->lock, irql);
1254 pxmitbuf = list_first_entry_or_null(&pfree_xmitbuf_queue->queue,
1255 struct xmit_buf, list);
1257 list_del_init(&pxmitbuf->list);
1258 pxmitpriv->free_xmitbuf_cnt--;
1259 pxmitbuf->priv_data = NULL;
1260 if (pxmitbuf->sctx) {
1261 DBG_88E("%s pxmitbuf->sctx is not NULL\n", __func__);
1262 rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_ALLOC);
1265 spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, irql);
1270 s32 rtw_free_xmitbuf(struct xmit_priv *pxmitpriv, struct xmit_buf *pxmitbuf)
1273 struct __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
1275 if (pxmitbuf == NULL)
1278 if (pxmitbuf->sctx) {
1279 DBG_88E("%s pxmitbuf->sctx is not NULL\n", __func__);
1280 rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_FREE);
1283 if (pxmitbuf->ext_tag) {
1284 rtw_free_xmitbuf_ext(pxmitpriv, pxmitbuf);
1286 spin_lock_irqsave(&pfree_xmitbuf_queue->lock, irql);
1288 list_del_init(&pxmitbuf->list);
1290 list_add_tail(&(pxmitbuf->list), get_list_head(pfree_xmitbuf_queue));
1292 pxmitpriv->free_xmitbuf_cnt++;
1293 spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, irql);
1303 2. RXENTRY (rx_thread or RX_ISR/RX_CallBack)
1305 If we turn on USE_RXTHREAD, then, no need for critical section.
1306 Otherwise, we must use _enter/_exit critical to protect free_xmit_queue...
1308 Must be very very cautious...
1312 struct xmit_frame *rtw_alloc_xmitframe(struct xmit_priv *pxmitpriv)
1313 /* _queue *pfree_xmit_queue) */
1316 Please remember to use all the osdep_service api,
1317 and lock/unlock or _enter/_exit critical to protect
1320 struct xmit_frame *pxframe;
1321 struct __queue *pfree_xmit_queue = &pxmitpriv->free_xmit_queue;
1323 spin_lock_bh(&pfree_xmit_queue->lock);
1324 pxframe = list_first_entry_or_null(&pfree_xmit_queue->queue,
1325 struct xmit_frame, list);
1327 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_,
1328 ("rtw_alloc_xmitframe:%d\n",
1329 pxmitpriv->free_xmitframe_cnt));
1331 list_del_init(&pxframe->list);
1333 /* default value setting */
1334 pxmitpriv->free_xmitframe_cnt--;
1336 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_,
1337 ("rtw_alloc_xmitframe():free_xmitframe_cnt=%d\n",
1338 pxmitpriv->free_xmitframe_cnt));
1340 pxframe->buf_addr = NULL;
1341 pxframe->pxmitbuf = NULL;
1343 memset(&pxframe->attrib, 0, sizeof(struct pkt_attrib));
1344 /* pxframe->attrib.psta = NULL; */
1346 pxframe->frame_tag = DATA_FRAMETAG;
1348 pxframe->pkt = NULL;
1349 pxframe->pkt_offset = 1;/* default use pkt_offset to fill tx desc */
1351 pxframe->agg_num = 1;
1352 pxframe->ack_report = 0;
1354 spin_unlock_bh(&pfree_xmit_queue->lock);
1359 s32 rtw_free_xmitframe(struct xmit_priv *pxmitpriv, struct xmit_frame *pxmitframe)
1361 struct __queue *pfree_xmit_queue = &pxmitpriv->free_xmit_queue;
1362 struct adapter *padapter = pxmitpriv->adapter;
1363 struct sk_buff *pndis_pkt = NULL;
1366 if (pxmitframe == NULL) {
1367 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("====== rtw_free_xmitframe():pxmitframe == NULL!!!!!!!!!!\n"));
1371 spin_lock_bh(&pfree_xmit_queue->lock);
1373 list_del_init(&pxmitframe->list);
1375 if (pxmitframe->pkt) {
1376 pndis_pkt = pxmitframe->pkt;
1377 pxmitframe->pkt = NULL;
1380 list_add_tail(&pxmitframe->list, get_list_head(pfree_xmit_queue));
1382 pxmitpriv->free_xmitframe_cnt++;
1383 RT_TRACE(_module_rtl871x_xmit_c_, _drv_debug_, ("rtw_free_xmitframe():free_xmitframe_cnt=%d\n", pxmitpriv->free_xmitframe_cnt));
1385 spin_unlock_bh(&pfree_xmit_queue->lock);
1388 rtw_os_pkt_complete(padapter, pndis_pkt);
1396 void rtw_free_xmitframe_queue(struct xmit_priv *pxmitpriv, struct __queue *pframequeue)
1398 struct list_head *plist, *phead;
1399 struct xmit_frame *pxmitframe;
1402 spin_lock_bh(&(pframequeue->lock));
1404 phead = get_list_head(pframequeue);
1405 plist = phead->next;
1407 while (phead != plist) {
1408 pxmitframe = container_of(plist, struct xmit_frame, list);
1410 plist = plist->next;
1412 rtw_free_xmitframe(pxmitpriv, pxmitframe);
1414 spin_unlock_bh(&(pframequeue->lock));
1418 s32 rtw_xmitframe_enqueue(struct adapter *padapter, struct xmit_frame *pxmitframe)
1420 if (rtw_xmit_classifier(padapter, pxmitframe) == _FAIL) {
1421 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_,
1422 ("rtw_xmitframe_enqueue: drop xmit pkt for classifier fail\n"));
1423 /* pxmitframe->pkt = NULL; */
1430 static struct xmit_frame *dequeue_one_xmitframe(struct xmit_priv *pxmitpriv, struct hw_xmit *phwxmit, struct tx_servq *ptxservq, struct __queue *pframe_queue)
1432 struct list_head *xmitframe_plist, *xmitframe_phead;
1433 struct xmit_frame *pxmitframe = NULL;
1435 xmitframe_phead = get_list_head(pframe_queue);
1436 xmitframe_plist = xmitframe_phead->next;
1438 if (xmitframe_phead != xmitframe_plist) {
1439 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
1441 xmitframe_plist = xmitframe_plist->next;
1443 list_del_init(&pxmitframe->list);
1450 struct xmit_frame *rtw_dequeue_xframe(struct xmit_priv *pxmitpriv, struct hw_xmit *phwxmit_i, int entry)
1452 struct list_head *sta_plist, *sta_phead;
1453 struct hw_xmit *phwxmit;
1454 struct tx_servq *ptxservq = NULL;
1455 struct __queue *pframe_queue = NULL;
1456 struct xmit_frame *pxmitframe = NULL;
1457 struct adapter *padapter = pxmitpriv->adapter;
1458 struct registry_priv *pregpriv = &padapter->registrypriv;
1462 inx[0] = 0; inx[1] = 1; inx[2] = 2; inx[3] = 3;
1464 if (pregpriv->wifi_spec == 1) {
1467 for (j = 0; j < 4; j++)
1468 inx[j] = pxmitpriv->wmm_para_seq[j];
1471 spin_lock_bh(&pxmitpriv->lock);
1473 for (i = 0; i < entry; i++) {
1474 phwxmit = phwxmit_i + inx[i];
1476 sta_phead = get_list_head(phwxmit->sta_queue);
1477 sta_plist = sta_phead->next;
1479 while (sta_phead != sta_plist) {
1480 ptxservq = container_of(sta_plist, struct tx_servq, tx_pending);
1482 pframe_queue = &ptxservq->sta_pending;
1484 pxmitframe = dequeue_one_xmitframe(pxmitpriv, phwxmit, ptxservq, pframe_queue);
1489 /* Remove sta node when there are no pending packets. */
1490 if (list_empty(&pframe_queue->queue)) /* must be done after get_next and before break */
1491 list_del_init(&ptxservq->tx_pending);
1495 sta_plist = sta_plist->next;
1499 spin_unlock_bh(&pxmitpriv->lock);
1503 struct tx_servq *rtw_get_sta_pending(struct adapter *padapter, struct sta_info *psta, int up, u8 *ac)
1505 struct tx_servq *ptxservq;
1510 ptxservq = &(psta->sta_xmitpriv.bk_q);
1512 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_get_sta_pending : BK\n"));
1516 ptxservq = &(psta->sta_xmitpriv.vi_q);
1518 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_get_sta_pending : VI\n"));
1522 ptxservq = &(psta->sta_xmitpriv.vo_q);
1524 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_get_sta_pending : VO\n"));
1529 ptxservq = &(psta->sta_xmitpriv.be_q);
1531 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_get_sta_pending : BE\n"));
1540 * Will enqueue pxmitframe to the proper queue,
1541 * and indicate it to xx_pending list.....
1543 s32 rtw_xmit_classifier(struct adapter *padapter, struct xmit_frame *pxmitframe)
1546 struct sta_info *psta;
1547 struct tx_servq *ptxservq;
1548 struct pkt_attrib *pattrib = &pxmitframe->attrib;
1549 struct sta_priv *pstapriv = &padapter->stapriv;
1550 struct hw_xmit *phwxmits = padapter->xmitpriv.hwxmits;
1555 psta = pattrib->psta;
1557 psta = rtw_get_stainfo(pstapriv, pattrib->ra);
1561 DBG_88E("rtw_xmit_classifier: psta == NULL\n");
1562 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("rtw_xmit_classifier: psta == NULL\n"));
1566 ptxservq = rtw_get_sta_pending(padapter, psta, pattrib->priority, (u8 *)(&ac_index));
1568 if (list_empty(&ptxservq->tx_pending))
1569 list_add_tail(&ptxservq->tx_pending, get_list_head(phwxmits[ac_index].sta_queue));
1571 list_add_tail(&pxmitframe->list, get_list_head(&ptxservq->sta_pending));
1573 phwxmits[ac_index].accnt++;
1580 void rtw_alloc_hwxmits(struct adapter *padapter)
1582 struct hw_xmit *hwxmits;
1583 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1585 pxmitpriv->hwxmit_entry = HWXMIT_ENTRY;
1587 pxmitpriv->hwxmits = kcalloc(pxmitpriv->hwxmit_entry,
1588 sizeof(struct hw_xmit), GFP_KERNEL);
1590 hwxmits = pxmitpriv->hwxmits;
1592 hwxmits[0] .sta_queue = &pxmitpriv->vo_pending;
1593 hwxmits[1] .sta_queue = &pxmitpriv->vi_pending;
1594 hwxmits[2] .sta_queue = &pxmitpriv->be_pending;
1595 hwxmits[3] .sta_queue = &pxmitpriv->bk_pending;
1598 void rtw_free_hwxmits(struct adapter *padapter)
1600 struct hw_xmit *hwxmits;
1601 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1603 hwxmits = pxmitpriv->hwxmits;
1607 void rtw_init_hwxmits(struct hw_xmit *phwxmit, int entry)
1611 for (i = 0; i < entry; i++, phwxmit++)
1615 u32 rtw_get_ff_hwaddr(struct xmit_frame *pxmitframe)
1618 struct pkt_attrib *pattrib = &pxmitframe->attrib;
1620 switch (pattrib->qsel) {
1623 addr = BE_QUEUE_INX;
1627 addr = BK_QUEUE_INX;
1631 addr = VI_QUEUE_INX;
1635 addr = VO_QUEUE_INX;
1638 addr = BCN_QUEUE_INX;
1640 case 0x11:/* BC/MC in PS (HIQ) */
1641 addr = HIGH_QUEUE_INX;
1645 addr = MGT_QUEUE_INX;
1653 * The main transmit(tx) entry
1657 * 0 success, hardware will handle this xmit frame(packet)
1660 s32 rtw_xmit(struct adapter *padapter, struct sk_buff **ppkt)
1662 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1663 struct xmit_frame *pxmitframe = NULL;
1666 pxmitframe = rtw_alloc_xmitframe(pxmitpriv);
1667 if (pxmitframe == NULL) {
1668 RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit: no more pxmitframe\n"));
1669 DBG_88E("DBG_TX_DROP_FRAME %s no more pxmitframe\n", __func__);
1673 res = update_attrib(padapter, *ppkt, &pxmitframe->attrib);
1676 RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit: update attrib fail\n"));
1677 rtw_free_xmitframe(pxmitpriv, pxmitframe);
1680 pxmitframe->pkt = *ppkt;
1682 LedControl8188eu(padapter, LED_CTL_TX);
1684 pxmitframe->attrib.qsel = pxmitframe->attrib.priority;
1686 #ifdef CONFIG_88EU_AP_MODE
1687 spin_lock_bh(&pxmitpriv->lock);
1688 if (xmitframe_enqueue_for_sleeping_sta(padapter, pxmitframe)) {
1689 spin_unlock_bh(&pxmitpriv->lock);
1692 spin_unlock_bh(&pxmitpriv->lock);
1695 if (rtw_hal_xmit(padapter, pxmitframe) == false)
1701 #if defined(CONFIG_88EU_AP_MODE)
1703 int xmitframe_enqueue_for_sleeping_sta(struct adapter *padapter, struct xmit_frame *pxmitframe)
1706 struct sta_info *psta = NULL;
1707 struct sta_priv *pstapriv = &padapter->stapriv;
1708 struct pkt_attrib *pattrib = &pxmitframe->attrib;
1709 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1710 int bmcst = IS_MCAST(pattrib->ra);
1712 if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == false)
1716 psta = pattrib->psta;
1718 psta = rtw_get_stainfo(pstapriv, pattrib->ra);
1723 if (pattrib->triggered == 1) {
1725 pattrib->qsel = 0x11;/* HIQ */
1730 spin_lock_bh(&psta->sleep_q.lock);
1732 if (pstapriv->sta_dz_bitmap) {/* if any one sta is in ps mode */
1733 list_del_init(&pxmitframe->list);
1735 list_add_tail(&pxmitframe->list, get_list_head(&psta->sleep_q));
1739 pstapriv->tim_bitmap |= BIT(0);/* */
1740 pstapriv->sta_dz_bitmap |= BIT(0);
1742 update_beacon(padapter, _TIM_IE_, NULL, false);/* tx bc/mc packets after update bcn */
1747 spin_unlock_bh(&psta->sleep_q.lock);
1752 spin_lock_bh(&psta->sleep_q.lock);
1754 if (psta->state&WIFI_SLEEP_STATE) {
1757 if (pstapriv->sta_dz_bitmap&BIT(psta->aid)) {
1758 list_del_init(&pxmitframe->list);
1760 list_add_tail(&pxmitframe->list, get_list_head(&psta->sleep_q));
1764 switch (pattrib->priority) {
1767 wmmps_ac = psta->uapsd_bk&BIT(0);
1771 wmmps_ac = psta->uapsd_vi&BIT(0);
1775 wmmps_ac = psta->uapsd_vo&BIT(0);
1780 wmmps_ac = psta->uapsd_be&BIT(0);
1785 psta->sleepq_ac_len++;
1787 if (((psta->has_legacy_ac) && (!wmmps_ac)) ||
1788 ((!psta->has_legacy_ac) && (wmmps_ac))) {
1789 pstapriv->tim_bitmap |= BIT(psta->aid);
1791 if (psta->sleepq_len == 1) {
1792 /* update BCN for TIM IE */
1793 update_beacon(padapter, _TIM_IE_, NULL, false);
1800 spin_unlock_bh(&psta->sleep_q.lock);
1805 static void dequeue_xmitframes_to_sleeping_queue(struct adapter *padapter, struct sta_info *psta, struct __queue *pframequeue)
1807 struct list_head *plist, *phead;
1809 struct tx_servq *ptxservq;
1810 struct pkt_attrib *pattrib;
1811 struct xmit_frame *pxmitframe;
1812 struct hw_xmit *phwxmits = padapter->xmitpriv.hwxmits;
1814 phead = get_list_head(pframequeue);
1815 plist = phead->next;
1817 while (phead != plist) {
1818 pxmitframe = container_of(plist, struct xmit_frame, list);
1820 plist = plist->next;
1822 xmitframe_enqueue_for_sleeping_sta(padapter, pxmitframe);
1824 pattrib = &pxmitframe->attrib;
1826 ptxservq = rtw_get_sta_pending(padapter, psta, pattrib->priority, (u8 *)(&ac_index));
1829 phwxmits[ac_index].accnt--;
1833 void stop_sta_xmit(struct adapter *padapter, struct sta_info *psta)
1835 struct sta_info *psta_bmc;
1836 struct sta_xmit_priv *pstaxmitpriv;
1837 struct sta_priv *pstapriv = &padapter->stapriv;
1838 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1840 pstaxmitpriv = &psta->sta_xmitpriv;
1842 /* for BC/MC Frames */
1843 psta_bmc = rtw_get_bcmc_stainfo(padapter);
1845 spin_lock_bh(&pxmitpriv->lock);
1847 psta->state |= WIFI_SLEEP_STATE;
1849 pstapriv->sta_dz_bitmap |= BIT(psta->aid);
1851 dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->vo_q.sta_pending);
1852 list_del_init(&(pstaxmitpriv->vo_q.tx_pending));
1854 dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->vi_q.sta_pending);
1855 list_del_init(&(pstaxmitpriv->vi_q.tx_pending));
1857 dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->be_q.sta_pending);
1858 list_del_init(&(pstaxmitpriv->be_q.tx_pending));
1860 dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->bk_q.sta_pending);
1861 list_del_init(&(pstaxmitpriv->bk_q.tx_pending));
1863 /* for BC/MC Frames */
1864 pstaxmitpriv = &psta_bmc->sta_xmitpriv;
1865 dequeue_xmitframes_to_sleeping_queue(padapter, psta_bmc, &pstaxmitpriv->be_q.sta_pending);
1866 list_del_init(&(pstaxmitpriv->be_q.tx_pending));
1868 spin_unlock_bh(&pxmitpriv->lock);
1871 void wakeup_sta_to_xmit(struct adapter *padapter, struct sta_info *psta)
1873 u8 update_mask = 0, wmmps_ac = 0;
1874 struct sta_info *psta_bmc;
1875 struct list_head *xmitframe_plist, *xmitframe_phead;
1876 struct xmit_frame *pxmitframe = NULL;
1877 struct sta_priv *pstapriv = &padapter->stapriv;
1879 spin_lock_bh(&psta->sleep_q.lock);
1881 xmitframe_phead = get_list_head(&psta->sleep_q);
1882 xmitframe_plist = xmitframe_phead->next;
1884 while (xmitframe_phead != xmitframe_plist) {
1885 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
1887 xmitframe_plist = xmitframe_plist->next;
1889 list_del_init(&pxmitframe->list);
1891 switch (pxmitframe->attrib.priority) {
1894 wmmps_ac = psta->uapsd_bk&BIT(1);
1898 wmmps_ac = psta->uapsd_vi&BIT(1);
1902 wmmps_ac = psta->uapsd_vo&BIT(1);
1907 wmmps_ac = psta->uapsd_be&BIT(1);
1912 if (psta->sleepq_len > 0)
1913 pxmitframe->attrib.mdata = 1;
1915 pxmitframe->attrib.mdata = 0;
1918 psta->sleepq_ac_len--;
1919 if (psta->sleepq_ac_len > 0) {
1920 pxmitframe->attrib.mdata = 1;
1921 pxmitframe->attrib.eosp = 0;
1923 pxmitframe->attrib.mdata = 0;
1924 pxmitframe->attrib.eosp = 1;
1928 pxmitframe->attrib.triggered = 1;
1930 spin_unlock_bh(&psta->sleep_q.lock);
1931 if (rtw_hal_xmit(padapter, pxmitframe))
1932 rtw_os_xmit_complete(padapter, pxmitframe);
1933 spin_lock_bh(&psta->sleep_q.lock);
1936 if (psta->sleepq_len == 0) {
1937 pstapriv->tim_bitmap &= ~BIT(psta->aid);
1939 update_mask = BIT(0);
1941 if (psta->state&WIFI_SLEEP_STATE)
1942 psta->state ^= WIFI_SLEEP_STATE;
1944 if (psta->state & WIFI_STA_ALIVE_CHK_STATE) {
1945 psta->expire_to = pstapriv->expire_to;
1946 psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
1949 pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
1952 spin_unlock_bh(&psta->sleep_q.lock);
1954 /* for BC/MC Frames */
1955 psta_bmc = rtw_get_bcmc_stainfo(padapter);
1959 if ((pstapriv->sta_dz_bitmap&0xfffe) == 0x0) { /* no any sta in ps mode */
1960 spin_lock_bh(&psta_bmc->sleep_q.lock);
1962 xmitframe_phead = get_list_head(&psta_bmc->sleep_q);
1963 xmitframe_plist = xmitframe_phead->next;
1965 while (xmitframe_phead != xmitframe_plist) {
1966 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
1968 xmitframe_plist = xmitframe_plist->next;
1970 list_del_init(&pxmitframe->list);
1972 psta_bmc->sleepq_len--;
1973 if (psta_bmc->sleepq_len > 0)
1974 pxmitframe->attrib.mdata = 1;
1976 pxmitframe->attrib.mdata = 0;
1978 pxmitframe->attrib.triggered = 1;
1980 spin_unlock_bh(&psta_bmc->sleep_q.lock);
1981 if (rtw_hal_xmit(padapter, pxmitframe))
1982 rtw_os_xmit_complete(padapter, pxmitframe);
1983 spin_lock_bh(&psta_bmc->sleep_q.lock);
1986 if (psta_bmc->sleepq_len == 0) {
1987 pstapriv->tim_bitmap &= ~BIT(0);
1988 pstapriv->sta_dz_bitmap &= ~BIT(0);
1990 update_mask |= BIT(1);
1993 spin_unlock_bh(&psta_bmc->sleep_q.lock);
1997 update_beacon(padapter, _TIM_IE_, NULL, false);
2000 void xmit_delivery_enabled_frames(struct adapter *padapter, struct sta_info *psta)
2003 struct list_head *xmitframe_plist, *xmitframe_phead;
2004 struct xmit_frame *pxmitframe = NULL;
2005 struct sta_priv *pstapriv = &padapter->stapriv;
2007 spin_lock_bh(&psta->sleep_q.lock);
2009 xmitframe_phead = get_list_head(&psta->sleep_q);
2010 xmitframe_plist = xmitframe_phead->next;
2012 while (xmitframe_phead != xmitframe_plist) {
2013 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
2015 xmitframe_plist = xmitframe_plist->next;
2017 switch (pxmitframe->attrib.priority) {
2020 wmmps_ac = psta->uapsd_bk&BIT(1);
2024 wmmps_ac = psta->uapsd_vi&BIT(1);
2028 wmmps_ac = psta->uapsd_vo&BIT(1);
2033 wmmps_ac = psta->uapsd_be&BIT(1);
2040 list_del_init(&pxmitframe->list);
2043 psta->sleepq_ac_len--;
2045 if (psta->sleepq_ac_len > 0) {
2046 pxmitframe->attrib.mdata = 1;
2047 pxmitframe->attrib.eosp = 0;
2049 pxmitframe->attrib.mdata = 0;
2050 pxmitframe->attrib.eosp = 1;
2053 pxmitframe->attrib.triggered = 1;
2055 if (rtw_hal_xmit(padapter, pxmitframe) == true)
2056 rtw_os_xmit_complete(padapter, pxmitframe);
2058 if ((psta->sleepq_ac_len == 0) && (!psta->has_legacy_ac) && (wmmps_ac)) {
2059 pstapriv->tim_bitmap &= ~BIT(psta->aid);
2061 /* update BCN for TIM IE */
2062 update_beacon(padapter, _TIM_IE_, NULL, false);
2066 spin_unlock_bh(&psta->sleep_q.lock);
2071 void rtw_sctx_init(struct submit_ctx *sctx, int timeout_ms)
2073 sctx->timeout_ms = timeout_ms;
2074 sctx->submit_time = jiffies;
2075 init_completion(&sctx->done);
2076 sctx->status = RTW_SCTX_SUBMITTED;
2079 int rtw_sctx_wait(struct submit_ctx *sctx)
2082 unsigned long expire;
2085 expire = sctx->timeout_ms ? msecs_to_jiffies(sctx->timeout_ms) : MAX_SCHEDULE_TIMEOUT;
2086 if (!wait_for_completion_timeout(&sctx->done, expire)) {
2087 /* timeout, do something?? */
2088 status = RTW_SCTX_DONE_TIMEOUT;
2089 DBG_88E("%s timeout\n", __func__);
2091 status = sctx->status;
2094 if (status == RTW_SCTX_DONE_SUCCESS)
2100 static bool rtw_sctx_chk_waring_status(int status)
2103 case RTW_SCTX_DONE_UNKNOWN:
2104 case RTW_SCTX_DONE_BUF_ALLOC:
2105 case RTW_SCTX_DONE_BUF_FREE:
2107 case RTW_SCTX_DONE_DRV_STOP:
2108 case RTW_SCTX_DONE_DEV_REMOVE:
2115 void rtw_sctx_done_err(struct submit_ctx **sctx, int status)
2118 if (rtw_sctx_chk_waring_status(status))
2119 DBG_88E("%s status:%d\n", __func__, status);
2120 (*sctx)->status = status;
2121 complete(&((*sctx)->done));
2126 int rtw_ack_tx_wait(struct xmit_priv *pxmitpriv, u32 timeout_ms)
2128 struct submit_ctx *pack_tx_ops = &pxmitpriv->ack_tx_ops;
2130 pack_tx_ops->submit_time = jiffies;
2131 pack_tx_ops->timeout_ms = timeout_ms;
2132 pack_tx_ops->status = RTW_SCTX_SUBMITTED;
2134 return rtw_sctx_wait(pack_tx_ops);
2137 void rtw_ack_tx_done(struct xmit_priv *pxmitpriv, int status)
2139 struct submit_ctx *pack_tx_ops = &pxmitpriv->ack_tx_ops;
2141 if (pxmitpriv->ack_tx)
2142 rtw_sctx_done_err(&pack_tx_ops, status);
2144 DBG_88E("%s ack_tx not set\n", __func__);