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