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