linux/drivers/staging/rtl8723bs/core/rtw_xmit.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/******************************************************************************
   3 *
   4 * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
   5 *
   6 ******************************************************************************/
   7#define _RTW_XMIT_C_
   8
   9#include <drv_types.h>
  10#include <rtw_debug.h>
  11
  12static u8 P802_1H_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0xf8 };
  13static u8 RFC1042_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0x00 };
  14
  15static void _init_txservq(struct tx_servq *ptxservq)
  16{
  17        INIT_LIST_HEAD(&ptxservq->tx_pending);
  18        _rtw_init_queue(&ptxservq->sta_pending);
  19        ptxservq->qcnt = 0;
  20}
  21
  22void _rtw_init_sta_xmit_priv(struct sta_xmit_priv *psta_xmitpriv)
  23{
  24        memset((unsigned char *)psta_xmitpriv, 0, sizeof(struct sta_xmit_priv));
  25
  26        spin_lock_init(&psta_xmitpriv->lock);
  27
  28        /* for (i = 0 ; i < MAX_NUMBLKS; i++) */
  29        /*      _init_txservq(&(psta_xmitpriv->blk_q[i])); */
  30
  31        _init_txservq(&psta_xmitpriv->be_q);
  32        _init_txservq(&psta_xmitpriv->bk_q);
  33        _init_txservq(&psta_xmitpriv->vi_q);
  34        _init_txservq(&psta_xmitpriv->vo_q);
  35        INIT_LIST_HEAD(&psta_xmitpriv->legacy_dz);
  36        INIT_LIST_HEAD(&psta_xmitpriv->apsd);
  37}
  38
  39s32 _rtw_init_xmit_priv(struct xmit_priv *pxmitpriv, struct adapter *padapter)
  40{
  41        int i;
  42        struct xmit_buf *pxmitbuf;
  43        struct xmit_frame *pxframe;
  44        sint    res = _SUCCESS;
  45
  46        spin_lock_init(&pxmitpriv->lock);
  47        spin_lock_init(&pxmitpriv->lock_sctx);
  48        init_completion(&pxmitpriv->xmit_comp);
  49        init_completion(&pxmitpriv->terminate_xmitthread_comp);
  50
  51        /*
  52        Please insert all the queue initializaiton using _rtw_init_queue below
  53        */
  54
  55        pxmitpriv->adapter = padapter;
  56
  57        /* for (i = 0 ; i < MAX_NUMBLKS; i++) */
  58        /*      _rtw_init_queue(&pxmitpriv->blk_strms[i]); */
  59
  60        _rtw_init_queue(&pxmitpriv->be_pending);
  61        _rtw_init_queue(&pxmitpriv->bk_pending);
  62        _rtw_init_queue(&pxmitpriv->vi_pending);
  63        _rtw_init_queue(&pxmitpriv->vo_pending);
  64        _rtw_init_queue(&pxmitpriv->bm_pending);
  65
  66        /* _rtw_init_queue(&pxmitpriv->legacy_dz_queue); */
  67        /* _rtw_init_queue(&pxmitpriv->apsd_queue); */
  68
  69        _rtw_init_queue(&pxmitpriv->free_xmit_queue);
  70
  71        /*
  72        Please allocate memory with the sz = (struct xmit_frame) * NR_XMITFRAME,
  73        and initialize free_xmit_frame below.
  74        Please also apply  free_txobj to link_up all the xmit_frames...
  75        */
  76
  77        pxmitpriv->pallocated_frame_buf = vzalloc(NR_XMITFRAME * sizeof(struct xmit_frame) + 4);
  78
  79        if (pxmitpriv->pallocated_frame_buf  == NULL) {
  80                pxmitpriv->pxmit_frame_buf = NULL;
  81                RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("alloc xmit_frame fail!\n"));
  82                res = _FAIL;
  83                goto exit;
  84        }
  85        pxmitpriv->pxmit_frame_buf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitpriv->pallocated_frame_buf), 4);
  86        /* pxmitpriv->pxmit_frame_buf = pxmitpriv->pallocated_frame_buf + 4 - */
  87        /*                                              ((SIZE_PTR) (pxmitpriv->pallocated_frame_buf) &3); */
  88
  89        pxframe = (struct xmit_frame *) pxmitpriv->pxmit_frame_buf;
  90
  91        for (i = 0; i < NR_XMITFRAME; i++) {
  92                INIT_LIST_HEAD(&(pxframe->list));
  93
  94                pxframe->padapter = padapter;
  95                pxframe->frame_tag = NULL_FRAMETAG;
  96
  97                pxframe->pkt = NULL;
  98
  99                pxframe->buf_addr = NULL;
 100                pxframe->pxmitbuf = NULL;
 101
 102                list_add_tail(&(pxframe->list), &(pxmitpriv->free_xmit_queue.queue));
 103
 104                pxframe++;
 105        }
 106
 107        pxmitpriv->free_xmitframe_cnt = NR_XMITFRAME;
 108
 109        pxmitpriv->frag_len = MAX_FRAG_THRESHOLD;
 110
 111
 112        /* init xmit_buf */
 113        _rtw_init_queue(&pxmitpriv->free_xmitbuf_queue);
 114        _rtw_init_queue(&pxmitpriv->pending_xmitbuf_queue);
 115
 116        pxmitpriv->pallocated_xmitbuf = vzalloc(NR_XMITBUFF * sizeof(struct xmit_buf) + 4);
 117
 118        if (pxmitpriv->pallocated_xmitbuf  == NULL) {
 119                RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("alloc xmit_buf fail!\n"));
 120                res = _FAIL;
 121                goto exit;
 122        }
 123
 124        pxmitpriv->pxmitbuf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitpriv->pallocated_xmitbuf), 4);
 125        /* pxmitpriv->pxmitbuf = pxmitpriv->pallocated_xmitbuf + 4 - */
 126        /*                                              ((SIZE_PTR) (pxmitpriv->pallocated_xmitbuf) &3); */
 127
 128        pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
 129
 130        for (i = 0; i < NR_XMITBUFF; i++) {
 131                INIT_LIST_HEAD(&pxmitbuf->list);
 132
 133                pxmitbuf->priv_data = NULL;
 134                pxmitbuf->padapter = padapter;
 135                pxmitbuf->buf_tag = XMITBUF_DATA;
 136
 137                /* Tx buf allocation may fail sometimes, so sleep and retry. */
 138                res = rtw_os_xmit_resource_alloc(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ), true);
 139                if (res == _FAIL) {
 140                        msleep(10);
 141                        res = rtw_os_xmit_resource_alloc(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ), true);
 142                        if (res == _FAIL)
 143                                goto exit;
 144                }
 145
 146                pxmitbuf->phead = pxmitbuf->pbuf;
 147                pxmitbuf->pend = pxmitbuf->pbuf + MAX_XMITBUF_SZ;
 148                pxmitbuf->len = 0;
 149                pxmitbuf->pdata = pxmitbuf->ptail = pxmitbuf->phead;
 150
 151                pxmitbuf->flags = XMIT_VO_QUEUE;
 152
 153                list_add_tail(&pxmitbuf->list, &(pxmitpriv->free_xmitbuf_queue.queue));
 154                #ifdef DBG_XMIT_BUF
 155                pxmitbuf->no = i;
 156                #endif
 157
 158                pxmitbuf++;
 159
 160        }
 161
 162        pxmitpriv->free_xmitbuf_cnt = NR_XMITBUFF;
 163
 164        /* init xframe_ext queue,  the same count as extbuf  */
 165        _rtw_init_queue(&pxmitpriv->free_xframe_ext_queue);
 166
 167        pxmitpriv->xframe_ext_alloc_addr = vzalloc(NR_XMIT_EXTBUFF * sizeof(struct xmit_frame) + 4);
 168
 169        if (pxmitpriv->xframe_ext_alloc_addr  == NULL) {
 170                pxmitpriv->xframe_ext = NULL;
 171                RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("alloc xframe_ext fail!\n"));
 172                res = _FAIL;
 173                goto exit;
 174        }
 175        pxmitpriv->xframe_ext = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitpriv->xframe_ext_alloc_addr), 4);
 176        pxframe = (struct xmit_frame *)pxmitpriv->xframe_ext;
 177
 178        for (i = 0; i < NR_XMIT_EXTBUFF; i++) {
 179                INIT_LIST_HEAD(&(pxframe->list));
 180
 181                pxframe->padapter = padapter;
 182                pxframe->frame_tag = NULL_FRAMETAG;
 183
 184                pxframe->pkt = NULL;
 185
 186                pxframe->buf_addr = NULL;
 187                pxframe->pxmitbuf = NULL;
 188
 189                pxframe->ext_tag = 1;
 190
 191                list_add_tail(&(pxframe->list), &(pxmitpriv->free_xframe_ext_queue.queue));
 192
 193                pxframe++;
 194        }
 195        pxmitpriv->free_xframe_ext_cnt = NR_XMIT_EXTBUFF;
 196
 197        /*  Init xmit extension buff */
 198        _rtw_init_queue(&pxmitpriv->free_xmit_extbuf_queue);
 199
 200        pxmitpriv->pallocated_xmit_extbuf = vzalloc(NR_XMIT_EXTBUFF * sizeof(struct xmit_buf) + 4);
 201
 202        if (pxmitpriv->pallocated_xmit_extbuf  == NULL) {
 203                RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("alloc xmit_extbuf fail!\n"));
 204                res = _FAIL;
 205                goto exit;
 206        }
 207
 208        pxmitpriv->pxmit_extbuf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitpriv->pallocated_xmit_extbuf), 4);
 209
 210        pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmit_extbuf;
 211
 212        for (i = 0; i < NR_XMIT_EXTBUFF; i++) {
 213                INIT_LIST_HEAD(&pxmitbuf->list);
 214
 215                pxmitbuf->priv_data = NULL;
 216                pxmitbuf->padapter = padapter;
 217                pxmitbuf->buf_tag = XMITBUF_MGNT;
 218
 219                res = rtw_os_xmit_resource_alloc(padapter, pxmitbuf, MAX_XMIT_EXTBUF_SZ + XMITBUF_ALIGN_SZ, true);
 220                if (res == _FAIL) {
 221                        res = _FAIL;
 222                        goto exit;
 223                }
 224
 225                pxmitbuf->phead = pxmitbuf->pbuf;
 226                pxmitbuf->pend = pxmitbuf->pbuf + MAX_XMIT_EXTBUF_SZ;
 227                pxmitbuf->len = 0;
 228                pxmitbuf->pdata = pxmitbuf->ptail = pxmitbuf->phead;
 229
 230                list_add_tail(&pxmitbuf->list, &(pxmitpriv->free_xmit_extbuf_queue.queue));
 231                #ifdef DBG_XMIT_BUF_EXT
 232                pxmitbuf->no = i;
 233                #endif
 234                pxmitbuf++;
 235
 236        }
 237
 238        pxmitpriv->free_xmit_extbuf_cnt = NR_XMIT_EXTBUFF;
 239
 240        for (i = 0; i < CMDBUF_MAX; i++) {
 241                pxmitbuf = &pxmitpriv->pcmd_xmitbuf[i];
 242                if (pxmitbuf) {
 243                        INIT_LIST_HEAD(&pxmitbuf->list);
 244
 245                        pxmitbuf->priv_data = NULL;
 246                        pxmitbuf->padapter = padapter;
 247                        pxmitbuf->buf_tag = XMITBUF_CMD;
 248
 249                        res = rtw_os_xmit_resource_alloc(padapter, pxmitbuf, MAX_CMDBUF_SZ+XMITBUF_ALIGN_SZ, true);
 250                        if (res == _FAIL) {
 251                                res = _FAIL;
 252                                goto exit;
 253                        }
 254
 255                        pxmitbuf->phead = pxmitbuf->pbuf;
 256                        pxmitbuf->pend = pxmitbuf->pbuf + MAX_CMDBUF_SZ;
 257                        pxmitbuf->len = 0;
 258                        pxmitbuf->pdata = pxmitbuf->ptail = pxmitbuf->phead;
 259                        pxmitbuf->alloc_sz = MAX_CMDBUF_SZ+XMITBUF_ALIGN_SZ;
 260                }
 261        }
 262
 263        rtw_alloc_hwxmits(padapter);
 264        rtw_init_hwxmits(pxmitpriv->hwxmits, pxmitpriv->hwxmit_entry);
 265
 266        for (i = 0; i < 4; i++) {
 267                pxmitpriv->wmm_para_seq[i] = i;
 268        }
 269
 270        pxmitpriv->ack_tx = false;
 271        mutex_init(&pxmitpriv->ack_tx_mutex);
 272        rtw_sctx_init(&pxmitpriv->ack_tx_ops, 0);
 273
 274        rtw_hal_init_xmit_priv(padapter);
 275
 276exit:
 277        return res;
 278}
 279
 280void _rtw_free_xmit_priv(struct xmit_priv *pxmitpriv)
 281{
 282        int i;
 283        struct adapter *padapter = pxmitpriv->adapter;
 284        struct xmit_frame       *pxmitframe = (struct xmit_frame *) pxmitpriv->pxmit_frame_buf;
 285        struct xmit_buf *pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
 286
 287        rtw_hal_free_xmit_priv(padapter);
 288
 289        if (pxmitpriv->pxmit_frame_buf == NULL)
 290                return;
 291
 292        for (i = 0; i < NR_XMITFRAME; i++) {
 293                rtw_os_xmit_complete(padapter, pxmitframe);
 294
 295                pxmitframe++;
 296        }
 297
 298        for (i = 0; i < NR_XMITBUFF; i++) {
 299                rtw_os_xmit_resource_free(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ), true);
 300
 301                pxmitbuf++;
 302        }
 303
 304        if (pxmitpriv->pallocated_frame_buf)
 305                vfree(pxmitpriv->pallocated_frame_buf);
 306
 307
 308        if (pxmitpriv->pallocated_xmitbuf)
 309                vfree(pxmitpriv->pallocated_xmitbuf);
 310
 311        /* free xframe_ext queue,  the same count as extbuf  */
 312        pxmitframe = (struct xmit_frame *)pxmitpriv->xframe_ext;
 313        if (pxmitframe) {
 314                for (i = 0; i < NR_XMIT_EXTBUFF; i++) {
 315                        rtw_os_xmit_complete(padapter, pxmitframe);
 316                        pxmitframe++;
 317                }
 318        }
 319        if (pxmitpriv->xframe_ext_alloc_addr)
 320                vfree(pxmitpriv->xframe_ext_alloc_addr);
 321
 322        /*  free xmit extension buff */
 323        pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmit_extbuf;
 324        for (i = 0; i < NR_XMIT_EXTBUFF; i++) {
 325                rtw_os_xmit_resource_free(padapter, pxmitbuf, (MAX_XMIT_EXTBUF_SZ + XMITBUF_ALIGN_SZ), true);
 326
 327                pxmitbuf++;
 328        }
 329
 330        if (pxmitpriv->pallocated_xmit_extbuf) {
 331                vfree(pxmitpriv->pallocated_xmit_extbuf);
 332        }
 333
 334        for (i = 0; i < CMDBUF_MAX; i++) {
 335                pxmitbuf = &pxmitpriv->pcmd_xmitbuf[i];
 336                if (pxmitbuf != NULL)
 337                        rtw_os_xmit_resource_free(padapter, pxmitbuf, MAX_CMDBUF_SZ+XMITBUF_ALIGN_SZ, true);
 338        }
 339
 340        rtw_free_hwxmits(padapter);
 341
 342        mutex_destroy(&pxmitpriv->ack_tx_mutex);
 343}
 344
 345u8 query_ra_short_GI(struct sta_info *psta)
 346{
 347        u8 sgi = false, sgi_20m = false, sgi_40m = false, sgi_80m = false;
 348
 349        sgi_20m = psta->htpriv.sgi_20m;
 350        sgi_40m = psta->htpriv.sgi_40m;
 351
 352        switch (psta->bw_mode) {
 353        case CHANNEL_WIDTH_80:
 354                sgi = sgi_80m;
 355                break;
 356        case CHANNEL_WIDTH_40:
 357                sgi = sgi_40m;
 358                break;
 359        case CHANNEL_WIDTH_20:
 360        default:
 361                sgi = sgi_20m;
 362                break;
 363        }
 364
 365        return sgi;
 366}
 367
 368static void update_attrib_vcs_info(struct adapter *padapter, struct xmit_frame *pxmitframe)
 369{
 370        u32 sz;
 371        struct pkt_attrib       *pattrib = &pxmitframe->attrib;
 372        /* struct sta_info *psta = pattrib->psta; */
 373        struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
 374        struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
 375
 376        if (pattrib->nr_frags != 1)
 377                sz = padapter->xmitpriv.frag_len;
 378        else /* no frag */
 379                sz = pattrib->last_txcmdsz;
 380
 381        /*  (1) RTS_Threshold is compared to the MPDU, not MSDU. */
 382        /*  (2) If there are more than one frag in  this MSDU, only the first frag uses protection frame. */
 383        /*              Other fragments are protected by previous fragment. */
 384        /*              So we only need to check the length of first fragment. */
 385        if (pmlmeext->cur_wireless_mode < WIRELESS_11_24N  || padapter->registrypriv.wifi_spec) {
 386                if (sz > padapter->registrypriv.rts_thresh)
 387                        pattrib->vcs_mode = RTS_CTS;
 388                else {
 389                        if (pattrib->rtsen)
 390                                pattrib->vcs_mode = RTS_CTS;
 391                        else if (pattrib->cts2self)
 392                                pattrib->vcs_mode = CTS_TO_SELF;
 393                        else
 394                                pattrib->vcs_mode = NONE_VCS;
 395                }
 396        } else {
 397                while (true) {
 398                        /* IOT action */
 399                        if ((pmlmeinfo->assoc_AP_vendor == HT_IOT_PEER_ATHEROS) && (pattrib->ampdu_en == true) &&
 400                                (padapter->securitypriv.dot11PrivacyAlgrthm == _AES_)) {
 401                                pattrib->vcs_mode = CTS_TO_SELF;
 402                                break;
 403                        }
 404
 405
 406                        /* check ERP protection */
 407                        if (pattrib->rtsen || pattrib->cts2self) {
 408                                if (pattrib->rtsen)
 409                                        pattrib->vcs_mode = RTS_CTS;
 410                                else if (pattrib->cts2self)
 411                                        pattrib->vcs_mode = CTS_TO_SELF;
 412
 413                                break;
 414                        }
 415
 416                        /* check HT op mode */
 417                        if (pattrib->ht_en) {
 418                                u8 HTOpMode = pmlmeinfo->HT_protection;
 419                                if ((pmlmeext->cur_bwmode && (HTOpMode == 2 || HTOpMode == 3)) ||
 420                                        (!pmlmeext->cur_bwmode && HTOpMode == 3)) {
 421                                        pattrib->vcs_mode = RTS_CTS;
 422                                        break;
 423                                }
 424                        }
 425
 426                        /* check rts */
 427                        if (sz > padapter->registrypriv.rts_thresh) {
 428                                pattrib->vcs_mode = RTS_CTS;
 429                                break;
 430                        }
 431
 432                        /* to do list: check MIMO power save condition. */
 433
 434                        /* check AMPDU aggregation for TXOP */
 435                        if (pattrib->ampdu_en == true) {
 436                                pattrib->vcs_mode = RTS_CTS;
 437                                break;
 438                        }
 439
 440                        pattrib->vcs_mode = NONE_VCS;
 441                        break;
 442                }
 443        }
 444
 445        /* for debug : force driver control vrtl_carrier_sense. */
 446        if (padapter->driver_vcs_en == 1)
 447                pattrib->vcs_mode = padapter->driver_vcs_type;
 448}
 449
 450static void update_attrib_phy_info(struct adapter *padapter, struct pkt_attrib *pattrib, struct sta_info *psta)
 451{
 452        struct mlme_ext_priv *mlmeext = &padapter->mlmeextpriv;
 453
 454        pattrib->rtsen = psta->rtsen;
 455        pattrib->cts2self = psta->cts2self;
 456
 457        pattrib->mdata = 0;
 458        pattrib->eosp = 0;
 459        pattrib->triggered = 0;
 460        pattrib->ampdu_spacing = 0;
 461
 462        /* qos_en, ht_en, init rate, , bw, ch_offset, sgi */
 463        pattrib->qos_en = psta->qos_option;
 464
 465        pattrib->raid = psta->raid;
 466
 467        if (mlmeext->cur_bwmode < psta->bw_mode)
 468                pattrib->bwmode = mlmeext->cur_bwmode;
 469        else
 470                pattrib->bwmode = psta->bw_mode;
 471
 472        pattrib->sgi = query_ra_short_GI(psta);
 473
 474        pattrib->ldpc = psta->ldpc;
 475        pattrib->stbc = psta->stbc;
 476
 477        pattrib->ht_en = psta->htpriv.ht_option;
 478        pattrib->ch_offset = psta->htpriv.ch_offset;
 479        pattrib->ampdu_en = false;
 480
 481        if (padapter->driver_ampdu_spacing != 0xFF) /* driver control AMPDU Density for peer sta's rx */
 482                pattrib->ampdu_spacing = padapter->driver_ampdu_spacing;
 483        else
 484                pattrib->ampdu_spacing = psta->htpriv.rx_ampdu_min_spacing;
 485
 486        /* if (pattrib->ht_en && psta->htpriv.ampdu_enable) */
 487        /*  */
 488        /*      if (psta->htpriv.agg_enable_bitmap & BIT(pattrib->priority)) */
 489        /*              pattrib->ampdu_en = true; */
 490        /*  */
 491
 492
 493        pattrib->retry_ctrl = false;
 494
 495#ifdef CONFIG_AUTO_AP_MODE
 496        if (psta->isrc && psta->pid > 0)
 497                pattrib->pctrl = true;
 498#endif
 499
 500}
 501
 502static s32 update_attrib_sec_info(struct adapter *padapter, struct pkt_attrib *pattrib, struct sta_info *psta)
 503{
 504        sint res = _SUCCESS;
 505        struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
 506        struct security_priv *psecuritypriv = &padapter->securitypriv;
 507        sint bmcast = IS_MCAST(pattrib->ra);
 508
 509        memset(pattrib->dot118021x_UncstKey.skey,  0, 16);
 510        memset(pattrib->dot11tkiptxmickey.skey,  0, 16);
 511        pattrib->mac_id = psta->mac_id;
 512
 513        if (psta->ieee8021x_blocked == true) {
 514                RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("\n psta->ieee8021x_blocked == true\n"));
 515
 516                pattrib->encrypt = 0;
 517
 518                if ((pattrib->ether_type != 0x888e) && (check_fwstate(pmlmepriv, WIFI_MP_STATE) == false)) {
 519                        RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("\npsta->ieee8021x_blocked == true,  pattrib->ether_type(%.4x) != 0x888e\n", pattrib->ether_type));
 520                        #ifdef DBG_TX_DROP_FRAME
 521                        DBG_871X("DBG_TX_DROP_FRAME %s psta->ieee8021x_blocked == true,  pattrib->ether_type(%04x) != 0x888e\n", __func__, pattrib->ether_type);
 522                        #endif
 523                        res = _FAIL;
 524                        goto exit;
 525                }
 526        } else {
 527                GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt, bmcast);
 528
 529                switch (psecuritypriv->dot11AuthAlgrthm) {
 530                case dot11AuthAlgrthm_Open:
 531                case dot11AuthAlgrthm_Shared:
 532                case dot11AuthAlgrthm_Auto:
 533                        pattrib->key_idx = (u8)psecuritypriv->dot11PrivacyKeyIndex;
 534                        break;
 535                case dot11AuthAlgrthm_8021X:
 536                        if (bmcast)
 537                                pattrib->key_idx = (u8)psecuritypriv->dot118021XGrpKeyid;
 538                        else
 539                                pattrib->key_idx = 0;
 540                        break;
 541                default:
 542                        pattrib->key_idx = 0;
 543                        break;
 544                }
 545
 546                /* For WPS 1.0 WEP, driver should not encrypt EAPOL Packet for WPS handshake. */
 547                if (((pattrib->encrypt == _WEP40_) || (pattrib->encrypt == _WEP104_)) && (pattrib->ether_type == 0x888e))
 548                        pattrib->encrypt = _NO_PRIVACY_;
 549
 550        }
 551
 552        switch (pattrib->encrypt) {
 553        case _WEP40_:
 554        case _WEP104_:
 555                pattrib->iv_len = 4;
 556                pattrib->icv_len = 4;
 557                WEP_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
 558                break;
 559
 560        case _TKIP_:
 561                pattrib->iv_len = 8;
 562                pattrib->icv_len = 4;
 563
 564                if (psecuritypriv->busetkipkey == _FAIL) {
 565                        #ifdef DBG_TX_DROP_FRAME
 566                        DBG_871X("DBG_TX_DROP_FRAME %s psecuritypriv->busetkipkey(%d) == _FAIL drop packet\n", __func__, psecuritypriv->busetkipkey);
 567                        #endif
 568                        res = _FAIL;
 569                        goto exit;
 570                }
 571
 572                if (bmcast)
 573                        TKIP_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
 574                else
 575                        TKIP_IV(pattrib->iv, psta->dot11txpn, 0);
 576
 577
 578                memcpy(pattrib->dot11tkiptxmickey.skey, psta->dot11tkiptxmickey.skey, 16);
 579
 580                break;
 581
 582        case _AES_:
 583
 584                pattrib->iv_len = 8;
 585                pattrib->icv_len = 8;
 586
 587                if (bmcast)
 588                        AES_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
 589                else
 590                        AES_IV(pattrib->iv, psta->dot11txpn, 0);
 591
 592                break;
 593
 594        default:
 595                pattrib->iv_len = 0;
 596                pattrib->icv_len = 0;
 597                break;
 598        }
 599
 600        if (pattrib->encrypt > 0)
 601                memcpy(pattrib->dot118021x_UncstKey.skey, psta->dot118021x_UncstKey.skey, 16);
 602
 603        RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_,
 604                ("update_attrib: encrypt =%d  securitypriv.sw_encrypt =%d\n",
 605                pattrib->encrypt, padapter->securitypriv.sw_encrypt));
 606
 607        if (pattrib->encrypt &&
 608                ((padapter->securitypriv.sw_encrypt == true) || (psecuritypriv->hw_decrypted == false))) {
 609                pattrib->bswenc = true;
 610                RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_,
 611                        ("update_attrib: encrypt =%d securitypriv.hw_decrypted =%d bswenc =true\n",
 612                        pattrib->encrypt, padapter->securitypriv.sw_encrypt));
 613        } else {
 614                pattrib->bswenc = false;
 615                RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("update_attrib: bswenc =false\n"));
 616        }
 617
 618exit:
 619
 620        return res;
 621
 622}
 623
 624u8 qos_acm(u8 acm_mask, u8 priority)
 625{
 626        u8 change_priority = priority;
 627
 628        switch (priority) {
 629        case 0:
 630        case 3:
 631                if (acm_mask & BIT(1))
 632                        change_priority = 1;
 633                break;
 634        case 1:
 635        case 2:
 636                break;
 637        case 4:
 638        case 5:
 639                if (acm_mask & BIT(2))
 640                        change_priority = 0;
 641                break;
 642        case 6:
 643        case 7:
 644                if (acm_mask & BIT(3))
 645                        change_priority = 5;
 646                break;
 647        default:
 648                DBG_871X("qos_acm(): invalid pattrib->priority: %d!!!\n", priority);
 649                break;
 650        }
 651
 652        return change_priority;
 653}
 654
 655static void set_qos(struct pkt_file *ppktfile, struct pkt_attrib *pattrib)
 656{
 657        struct ethhdr etherhdr;
 658        struct iphdr ip_hdr;
 659        s32 UserPriority = 0;
 660
 661
 662        _rtw_open_pktfile(ppktfile->pkt, ppktfile);
 663        _rtw_pktfile_read(ppktfile, (unsigned char *)&etherhdr, ETH_HLEN);
 664
 665        /*  get UserPriority from IP hdr */
 666        if (pattrib->ether_type == 0x0800) {
 667                _rtw_pktfile_read(ppktfile, (u8 *)&ip_hdr, sizeof(ip_hdr));
 668/*              UserPriority = (ntohs(ip_hdr.tos) >> 5) & 0x3; */
 669                UserPriority = ip_hdr.tos >> 5;
 670        }
 671        pattrib->priority = UserPriority;
 672        pattrib->hdrlen = WLAN_HDR_A3_QOS_LEN;
 673        pattrib->subtype = WIFI_QOS_DATA_TYPE;
 674}
 675
 676static s32 update_attrib(struct adapter *padapter, _pkt *pkt, struct pkt_attrib *pattrib)
 677{
 678        uint i;
 679        struct pkt_file pktfile;
 680        struct sta_info *psta = NULL;
 681        struct ethhdr etherhdr;
 682
 683        sint bmcast;
 684        struct sta_priv         *pstapriv = &padapter->stapriv;
 685        struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
 686        struct qos_priv         *pqospriv = &pmlmepriv->qospriv;
 687        sint res = _SUCCESS;
 688
 689        DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib);
 690
 691        _rtw_open_pktfile(pkt, &pktfile);
 692        i = _rtw_pktfile_read(&pktfile, (u8 *)&etherhdr, ETH_HLEN);
 693
 694        pattrib->ether_type = ntohs(etherhdr.h_proto);
 695
 696
 697        memcpy(pattrib->dst, &etherhdr.h_dest, ETH_ALEN);
 698        memcpy(pattrib->src, &etherhdr.h_source, ETH_ALEN);
 699
 700
 701        if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true) ||
 702                (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true)) {
 703                memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
 704                memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
 705                DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_adhoc);
 706        } else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
 707                memcpy(pattrib->ra, get_bssid(pmlmepriv), ETH_ALEN);
 708                memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
 709                DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_sta);
 710        } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
 711                memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
 712                memcpy(pattrib->ta, get_bssid(pmlmepriv), ETH_ALEN);
 713                DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_ap);
 714        } else
 715                DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_unknown);
 716
 717        pattrib->pktlen = pktfile.pkt_len;
 718
 719        if (ETH_P_IP == pattrib->ether_type) {
 720                /*  The following is for DHCP and ARP packet, we use cck1M to tx these packets and let LPS awake some time */
 721                /*  to prevent DHCP protocol fail */
 722
 723                u8 tmp[24];
 724
 725                _rtw_pktfile_read(&pktfile, &tmp[0], 24);
 726
 727                pattrib->dhcp_pkt = 0;
 728                if (pktfile.pkt_len > 282) {/* MINIMUM_DHCP_PACKET_SIZE) { */
 729                        if (ETH_P_IP == pattrib->ether_type) {/*  IP header */
 730                                if (((tmp[21] == 68) && (tmp[23] == 67)) ||
 731                                        ((tmp[21] == 67) && (tmp[23] == 68))) {
 732                                        /*  68 : UDP BOOTP client */
 733                                        /*  67 : UDP BOOTP server */
 734                                        RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("======================update_attrib: get DHCP Packet\n"));
 735                                        pattrib->dhcp_pkt = 1;
 736                                        DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_dhcp);
 737                                }
 738                        }
 739                }
 740
 741                /* for parsing ICMP pakcets */
 742                {
 743                        struct iphdr *piphdr = (struct iphdr *)tmp;
 744
 745                        pattrib->icmp_pkt = 0;
 746                        if (piphdr->protocol == 0x1) { /*  protocol type in ip header 0x1 is ICMP */
 747                                pattrib->icmp_pkt = 1;
 748                                DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_icmp);
 749                        }
 750                }
 751
 752
 753        } else if (0x888e == pattrib->ether_type) {
 754                DBG_871X_LEVEL(_drv_always_, "send eapol packet\n");
 755        }
 756
 757        if ((pattrib->ether_type == 0x888e) || (pattrib->dhcp_pkt == 1))
 758                rtw_set_scan_deny(padapter, 3000);
 759
 760        /*  If EAPOL , ARP , OR DHCP packet, driver must be in active mode. */
 761        if (pattrib->icmp_pkt == 1)
 762                rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_LEAVE, 1);
 763        else if (pattrib->dhcp_pkt == 1) {
 764                DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_active);
 765                rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_SPECIAL_PACKET, 1);
 766        }
 767
 768        bmcast = IS_MCAST(pattrib->ra);
 769
 770        /*  get sta_info */
 771        if (bmcast) {
 772                psta = rtw_get_bcmc_stainfo(padapter);
 773        } else {
 774                psta = rtw_get_stainfo(pstapriv, pattrib->ra);
 775                if (psta == NULL)       { /*  if we cannot get psta => drop the pkt */
 776                        DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_err_ucast_sta);
 777                        RT_TRACE(_module_rtl871x_xmit_c_, _drv_alert_, ("\nupdate_attrib => get sta_info fail, ra:" MAC_FMT"\n", MAC_ARG(pattrib->ra)));
 778                        #ifdef DBG_TX_DROP_FRAME
 779                        DBG_871X("DBG_TX_DROP_FRAME %s get sta_info fail, ra:" MAC_FMT"\n", __func__, MAC_ARG(pattrib->ra));
 780                        #endif
 781                        res = _FAIL;
 782                        goto exit;
 783                } else if ((check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) && (!(psta->state & _FW_LINKED))) {
 784                        DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_err_ucast_ap_link);
 785                        res = _FAIL;
 786                        goto exit;
 787                }
 788        }
 789
 790        if (psta == NULL) {
 791                /*  if we cannot get psta => drop the pkt */
 792                DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_err_sta);
 793                RT_TRACE(_module_rtl871x_xmit_c_, _drv_alert_, ("\nupdate_attrib => get sta_info fail, ra:" MAC_FMT "\n", MAC_ARG(pattrib->ra)));
 794                #ifdef DBG_TX_DROP_FRAME
 795                DBG_871X("DBG_TX_DROP_FRAME %s get sta_info fail, ra:" MAC_FMT"\n", __func__, MAC_ARG(pattrib->ra));
 796                #endif
 797                res = _FAIL;
 798                goto exit;
 799        }
 800
 801        if (!(psta->state & _FW_LINKED)) {
 802                DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_err_link);
 803                DBG_871X("%s, psta("MAC_FMT")->state(0x%x) != _FW_LINKED\n", __func__, MAC_ARG(psta->hwaddr), psta->state);
 804                return _FAIL;
 805        }
 806
 807
 808
 809        /* TODO:_lock */
 810        if (update_attrib_sec_info(padapter, pattrib, psta) == _FAIL) {
 811                DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_err_sec);
 812                res = _FAIL;
 813                goto exit;
 814        }
 815
 816        update_attrib_phy_info(padapter, pattrib, psta);
 817
 818        /* DBG_8192C("%s ==> mac_id(%d)\n", __func__, pattrib->mac_id); */
 819
 820        pattrib->psta = psta;
 821        /* TODO:_unlock */
 822
 823        pattrib->pctrl = 0;
 824
 825        pattrib->ack_policy = 0;
 826        /*  get ether_hdr_len */
 827        pattrib->pkt_hdrlen = ETH_HLEN;/* pattrib->ether_type == 0x8100) ? (14 + 4): 14; vlan tag */
 828
 829        pattrib->hdrlen = WLAN_HDR_A3_LEN;
 830        pattrib->subtype = WIFI_DATA_TYPE;
 831        pattrib->priority = 0;
 832
 833        if (check_fwstate(pmlmepriv, WIFI_AP_STATE|WIFI_ADHOC_STATE|WIFI_ADHOC_MASTER_STATE)) {
 834                if (pattrib->qos_en)
 835                        set_qos(&pktfile, pattrib);
 836        } else {
 837                if (pqospriv->qos_option) {
 838                        set_qos(&pktfile, pattrib);
 839
 840                        if (pmlmepriv->acm_mask != 0)
 841                                pattrib->priority = qos_acm(pmlmepriv->acm_mask, pattrib->priority);
 842
 843                }
 844        }
 845
 846        /* pattrib->priority = 5; force to used VI queue, for testing */
 847
 848exit:
 849        return res;
 850}
 851
 852static s32 xmitframe_addmic(struct adapter *padapter, struct xmit_frame *pxmitframe)
 853{
 854        sint                    curfragnum, length;
 855        u8 *pframe, *payload, mic[8];
 856        struct  mic_data                micdata;
 857        /* struct       sta_info        *stainfo; */
 858        struct  pkt_attrib       *pattrib = &pxmitframe->attrib;
 859        struct  security_priv *psecuritypriv = &padapter->securitypriv;
 860        struct  xmit_priv       *pxmitpriv = &padapter->xmitpriv;
 861        u8 priority[4] = {0x0, 0x0, 0x0, 0x0};
 862        u8 hw_hdr_offset = 0;
 863        sint bmcst = IS_MCAST(pattrib->ra);
 864
 865/*
 866        if (pattrib->psta)
 867        {
 868                stainfo = pattrib->psta;
 869        }
 870        else
 871        {
 872                DBG_871X("%s, call rtw_get_stainfo()\n", __func__);
 873                stainfo =rtw_get_stainfo(&padapter->stapriv ,&pattrib->ra[0]);
 874        }
 875
 876        if (stainfo == NULL)
 877        {
 878                DBG_871X("%s, psta ==NUL\n", __func__);
 879                return _FAIL;
 880        }
 881
 882        if (!(stainfo->state &_FW_LINKED))
 883        {
 884                DBG_871X("%s, psta->state(0x%x) != _FW_LINKED\n", __func__, stainfo->state);
 885                return _FAIL;
 886        }
 887*/
 888
 889        hw_hdr_offset = TXDESC_OFFSET;
 890
 891        if (pattrib->encrypt == _TKIP_) { /* if (psecuritypriv->dot11PrivacyAlgrthm == _TKIP_PRIVACY_) */
 892                /* encode mic code */
 893                /* if (stainfo!= NULL) */
 894                {
 895                        u8 null_key[16] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
 896
 897                        pframe = pxmitframe->buf_addr + hw_hdr_offset;
 898
 899                        if (bmcst) {
 900                                if (!memcmp(psecuritypriv->dot118021XGrptxmickey[psecuritypriv->dot118021XGrpKeyid].skey, null_key, 16)) {
 901                                        /* DbgPrint("\nxmitframe_addmic:stainfo->dot11tkiptxmickey == 0\n"); */
 902                                        /* msleep(10); */
 903                                        return _FAIL;
 904                                }
 905                                /* start to calculate the mic code */
 906                                rtw_secmicsetkey(&micdata, psecuritypriv->dot118021XGrptxmickey[psecuritypriv->dot118021XGrpKeyid].skey);
 907                        } else {
 908                                if (!memcmp(&pattrib->dot11tkiptxmickey.skey[0], null_key, 16)) {
 909                                        /* DbgPrint("\nxmitframe_addmic:stainfo->dot11tkiptxmickey == 0\n"); */
 910                                        /* msleep(10); */
 911                                        return _FAIL;
 912                                }
 913                                /* start to calculate the mic code */
 914                                rtw_secmicsetkey(&micdata, &pattrib->dot11tkiptxmickey.skey[0]);
 915                        }
 916
 917                        if (pframe[1]&1) {   /* ToDS == 1 */
 918                                rtw_secmicappend(&micdata, &pframe[16], 6);  /* DA */
 919                                if (pframe[1]&2)  /* From Ds == 1 */
 920                                        rtw_secmicappend(&micdata, &pframe[24], 6);
 921                                else
 922                                rtw_secmicappend(&micdata, &pframe[10], 6);
 923                        } else {        /* ToDS == 0 */
 924                                rtw_secmicappend(&micdata, &pframe[4], 6);   /* DA */
 925                                if (pframe[1]&2)  /* From Ds == 1 */
 926                                        rtw_secmicappend(&micdata, &pframe[16], 6);
 927                                else
 928                                        rtw_secmicappend(&micdata, &pframe[10], 6);
 929
 930                        }
 931
 932                        /* if (pqospriv->qos_option == 1) */
 933                        if (pattrib->qos_en)
 934                                priority[0] = (u8)pxmitframe->attrib.priority;
 935
 936
 937                        rtw_secmicappend(&micdata, &priority[0], 4);
 938
 939                        payload = pframe;
 940
 941                        for (curfragnum = 0; curfragnum < pattrib->nr_frags; curfragnum++) {
 942                                payload = (u8 *)RND4((SIZE_PTR)(payload));
 943                                RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("===curfragnum =%d, pframe = 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x,!!!\n",
 944                                        curfragnum, *payload, *(payload+1), *(payload+2), *(payload+3), *(payload+4), *(payload+5), *(payload+6), *(payload+7)));
 945
 946                                payload = payload+pattrib->hdrlen+pattrib->iv_len;
 947                                RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("curfragnum =%d pattrib->hdrlen =%d pattrib->iv_len =%d", curfragnum, pattrib->hdrlen, pattrib->iv_len));
 948                                if ((curfragnum+1) == pattrib->nr_frags) {
 949                                        length = pattrib->last_txcmdsz-pattrib->hdrlen-pattrib->iv_len-((pattrib->bswenc) ? pattrib->icv_len : 0);
 950                                        rtw_secmicappend(&micdata, payload, length);
 951                                        payload = payload+length;
 952                                } else {
 953                                        length = pxmitpriv->frag_len-pattrib->hdrlen-pattrib->iv_len-((pattrib->bswenc) ? pattrib->icv_len : 0);
 954                                        rtw_secmicappend(&micdata, payload, length);
 955                                        payload = payload+length+pattrib->icv_len;
 956                                        RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("curfragnum =%d length =%d pattrib->icv_len =%d", curfragnum, length, pattrib->icv_len));
 957                                }
 958                        }
 959                        rtw_secgetmic(&micdata, &(mic[0]));
 960                        RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("xmitframe_addmic: before add mic code!!!\n"));
 961                        RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("xmitframe_addmic: pattrib->last_txcmdsz =%d!!!\n", pattrib->last_txcmdsz));
 962                        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\
 963  mic[4]= 0x%.2x , mic[5]= 0x%.2x , mic[6]= 0x%.2x , mic[7]= 0x%.2x !!!!\n",
 964                                mic[0], mic[1], mic[2], mic[3], mic[4], mic[5], mic[6], mic[7]));
 965                        /* add mic code  and add the mic code length in last_txcmdsz */
 966
 967                        memcpy(payload, &(mic[0]), 8);
 968                        pattrib->last_txcmdsz += 8;
 969
 970                        RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("\n ========last pkt ========\n"));
 971                        payload = payload-pattrib->last_txcmdsz+8;
 972                        for (curfragnum = 0; curfragnum < pattrib->last_txcmdsz; curfragnum = curfragnum+8)
 973                                        RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, (" %.2x,  %.2x,  %.2x,  %.2x,  %.2x,  %.2x,  %.2x,  %.2x ",
 974                                        *(payload+curfragnum), *(payload+curfragnum+1), *(payload+curfragnum+2), *(payload+curfragnum+3),
 975                                        *(payload+curfragnum+4), *(payload+curfragnum+5), *(payload+curfragnum+6), *(payload+curfragnum+7)));
 976                        }
 977/*
 978                        else {
 979                                RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("xmitframe_addmic: rtw_get_stainfo == NULL!!!\n"));
 980                        }
 981*/
 982        }
 983        return _SUCCESS;
 984}
 985
 986static s32 xmitframe_swencrypt(struct adapter *padapter, struct xmit_frame *pxmitframe)
 987{
 988
 989        struct  pkt_attrib       *pattrib = &pxmitframe->attrib;
 990        /* struct       security_priv *psecuritypriv =&padapter->securitypriv; */
 991
 992        /* if ((psecuritypriv->sw_encrypt)||(pattrib->bswenc)) */
 993        if (pattrib->bswenc) {
 994                /* DBG_871X("start xmitframe_swencrypt\n"); */
 995                RT_TRACE(_module_rtl871x_xmit_c_, _drv_alert_, ("### xmitframe_swencrypt\n"));
 996                switch (pattrib->encrypt) {
 997                case _WEP40_:
 998                case _WEP104_:
 999                        rtw_wep_encrypt(padapter, (u8 *)pxmitframe);
1000                        break;
1001                case _TKIP_:
1002                        rtw_tkip_encrypt(padapter, (u8 *)pxmitframe);
1003                        break;
1004                case _AES_:
1005                        rtw_aes_encrypt(padapter, (u8 *)pxmitframe);
1006                        break;
1007                default:
1008                                break;
1009                }
1010
1011        } else
1012                RT_TRACE(_module_rtl871x_xmit_c_, _drv_notice_, ("### xmitframe_hwencrypt\n"));
1013
1014        return _SUCCESS;
1015}
1016
1017s32 rtw_make_wlanhdr(struct adapter *padapter, u8 *hdr, struct pkt_attrib *pattrib)
1018{
1019        u16 *qc;
1020
1021        struct ieee80211_hdr *pwlanhdr = (struct ieee80211_hdr *)hdr;
1022        struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1023        struct qos_priv *pqospriv = &pmlmepriv->qospriv;
1024        u8 qos_option = false;
1025        sint res = _SUCCESS;
1026        __le16 *fctrl = &pwlanhdr->frame_control;
1027
1028        memset(hdr, 0, WLANHDR_OFFSET);
1029
1030        SetFrameSubType(fctrl, pattrib->subtype);
1031
1032        if (pattrib->subtype & WIFI_DATA_TYPE) {
1033                if ((check_fwstate(pmlmepriv,  WIFI_STATION_STATE) == true)) {
1034                        /* to_ds = 1, fr_ds = 0; */
1035
1036                        {
1037                                /*  1.Data transfer to AP */
1038                                /*  2.Arp pkt will relayed by AP */
1039                                SetToDs(fctrl);
1040                                memcpy(pwlanhdr->addr1, get_bssid(pmlmepriv), ETH_ALEN);
1041                                memcpy(pwlanhdr->addr2, pattrib->src, ETH_ALEN);
1042                                memcpy(pwlanhdr->addr3, pattrib->dst, ETH_ALEN);
1043                        }
1044
1045                        if (pqospriv->qos_option)
1046                                qos_option = true;
1047
1048                } else if ((check_fwstate(pmlmepriv,  WIFI_AP_STATE) == true)) {
1049                        /* to_ds = 0, fr_ds = 1; */
1050                        SetFrDs(fctrl);
1051                        memcpy(pwlanhdr->addr1, pattrib->dst, ETH_ALEN);
1052                        memcpy(pwlanhdr->addr2, get_bssid(pmlmepriv), ETH_ALEN);
1053                        memcpy(pwlanhdr->addr3, pattrib->src, ETH_ALEN);
1054
1055                        if (pattrib->qos_en)
1056                                qos_option = true;
1057                } else if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true) ||
1058                (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true)) {
1059                        memcpy(pwlanhdr->addr1, pattrib->dst, ETH_ALEN);
1060                        memcpy(pwlanhdr->addr2, pattrib->src, ETH_ALEN);
1061                        memcpy(pwlanhdr->addr3, get_bssid(pmlmepriv), ETH_ALEN);
1062
1063                        if (pattrib->qos_en)
1064                                qos_option = true;
1065                } else {
1066                        RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("fw_state:%x is not allowed to xmit frame\n", get_fwstate(pmlmepriv)));
1067                        res = _FAIL;
1068                        goto exit;
1069                }
1070
1071                if (pattrib->mdata)
1072                        SetMData(fctrl);
1073
1074                if (pattrib->encrypt)
1075                        SetPrivacy(fctrl);
1076
1077                if (qos_option) {
1078                        qc = (unsigned short *)(hdr + pattrib->hdrlen - 2);
1079
1080                        if (pattrib->priority)
1081                                SetPriority(qc, pattrib->priority);
1082
1083                        SetEOSP(qc, pattrib->eosp);
1084
1085                        SetAckpolicy(qc, pattrib->ack_policy);
1086                }
1087
1088                /* TODO: fill HT Control Field */
1089
1090                /* Update Seq Num will be handled by f/w */
1091                {
1092                        struct sta_info *psta;
1093                        psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
1094                        if (pattrib->psta != psta) {
1095                                DBG_871X("%s, pattrib->psta(%p) != psta(%p)\n", __func__, pattrib->psta, psta);
1096                                return _FAIL;
1097                        }
1098
1099                        if (psta == NULL) {
1100                                DBG_871X("%s, psta ==NUL\n", __func__);
1101                                return _FAIL;
1102                        }
1103
1104                        if (!(psta->state & _FW_LINKED)) {
1105                                DBG_871X("%s, psta->state(0x%x) != _FW_LINKED\n", __func__, psta->state);
1106                                return _FAIL;
1107                        }
1108
1109
1110                        if (psta) {
1111                                psta->sta_xmitpriv.txseq_tid[pattrib->priority]++;
1112                                psta->sta_xmitpriv.txseq_tid[pattrib->priority] &= 0xFFF;
1113                                pattrib->seqnum = psta->sta_xmitpriv.txseq_tid[pattrib->priority];
1114
1115                                SetSeqNum(hdr, pattrib->seqnum);
1116
1117                                /* check if enable ampdu */
1118                                if (pattrib->ht_en && psta->htpriv.ampdu_enable)
1119                                        if (psta->htpriv.agg_enable_bitmap & BIT(pattrib->priority))
1120                                                pattrib->ampdu_en = true;
1121
1122
1123                                /* re-check if enable ampdu by BA_starting_seqctrl */
1124                                if (pattrib->ampdu_en == true) {
1125                                        u16 tx_seq;
1126
1127                                        tx_seq = psta->BA_starting_seqctrl[pattrib->priority & 0x0f];
1128
1129                                        /* check BA_starting_seqctrl */
1130                                        if (SN_LESS(pattrib->seqnum, tx_seq)) {
1131                                                /* DBG_871X("tx ampdu seqnum(%d) < tx_seq(%d)\n", pattrib->seqnum, tx_seq); */
1132                                                pattrib->ampdu_en = false;/* AGG BK */
1133                                        } else if (SN_EQUAL(pattrib->seqnum, tx_seq)) {
1134                                                psta->BA_starting_seqctrl[pattrib->priority & 0x0f] = (tx_seq+1)&0xfff;
1135
1136                                                pattrib->ampdu_en = true;/* AGG EN */
1137                                        } else {
1138                                                /* DBG_871X("tx ampdu over run\n"); */
1139                                                psta->BA_starting_seqctrl[pattrib->priority & 0x0f] = (pattrib->seqnum+1)&0xfff;
1140                                                pattrib->ampdu_en = true;/* AGG EN */
1141                                        }
1142
1143                                }
1144                        }
1145                }
1146
1147        } else {
1148
1149        }
1150
1151exit:
1152        return res;
1153}
1154
1155s32 rtw_txframes_pending(struct adapter *padapter)
1156{
1157        struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1158
1159        return ((!list_empty(&pxmitpriv->be_pending.queue)) ||
1160                         (!list_empty(&pxmitpriv->bk_pending.queue)) ||
1161                         (!list_empty(&pxmitpriv->vi_pending.queue)) ||
1162                         (!list_empty(&pxmitpriv->vo_pending.queue)));
1163}
1164
1165/*
1166 * Calculate wlan 802.11 packet MAX size from pkt_attrib
1167 * This function doesn't consider fragment case
1168 */
1169u32 rtw_calculate_wlan_pkt_size_by_attribue(struct pkt_attrib *pattrib)
1170{
1171        u32 len = 0;
1172
1173        len = pattrib->hdrlen + pattrib->iv_len; /*  WLAN Header and IV */
1174        len += SNAP_SIZE + sizeof(u16); /*  LLC */
1175        len += pattrib->pktlen;
1176        if (pattrib->encrypt == _TKIP_)
1177                len += 8; /*  MIC */
1178        len += ((pattrib->bswenc) ? pattrib->icv_len : 0); /*  ICV */
1179
1180        return len;
1181}
1182
1183/*
1184
1185This sub-routine will perform all the following:
1186
11871. remove 802.3 header.
11882. create wlan_header, based on the info in pxmitframe
11893. append sta's iv/ext-iv
11904. append LLC
11915. move frag chunk from pframe to pxmitframe->mem
11926. apply sw-encrypt, if necessary.
1193
1194*/
1195s32 rtw_xmitframe_coalesce(struct adapter *padapter, _pkt *pkt, struct xmit_frame *pxmitframe)
1196{
1197        struct pkt_file pktfile;
1198
1199        s32 frg_inx, frg_len, mpdu_len, llc_sz, mem_sz;
1200
1201        SIZE_PTR addr;
1202
1203        u8 *pframe, *mem_start;
1204        u8 hw_hdr_offset;
1205
1206        /* struct sta_info      *psta; */
1207        /* struct sta_priv      *pstapriv = &padapter->stapriv; */
1208        /* struct mlme_priv *pmlmepriv = &padapter->mlmepriv; */
1209        struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1210
1211        struct pkt_attrib       *pattrib = &pxmitframe->attrib;
1212
1213        u8 *pbuf_start;
1214
1215        s32 bmcst = IS_MCAST(pattrib->ra);
1216        s32 res = _SUCCESS;
1217
1218/*
1219        if (pattrib->psta)
1220        {
1221                psta = pattrib->psta;
1222        } else
1223        {
1224                DBG_871X("%s, call rtw_get_stainfo()\n", __func__);
1225                psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
1226        }
1227
1228        if (psta == NULL)
1229  {
1230
1231                DBG_871X("%s, psta ==NUL\n", __func__);
1232                return _FAIL;
1233        }
1234
1235
1236        if (!(psta->state &_FW_LINKED))
1237        {
1238                DBG_871X("%s, psta->state(0x%x) != _FW_LINKED\n", __func__, psta->state);
1239                return _FAIL;
1240        }
1241*/
1242        if (pxmitframe->buf_addr == NULL) {
1243                DBG_8192C("==> %s buf_addr == NULL\n", __func__);
1244                return _FAIL;
1245        }
1246
1247        pbuf_start = pxmitframe->buf_addr;
1248
1249        hw_hdr_offset = TXDESC_OFFSET;
1250        mem_start = pbuf_start +        hw_hdr_offset;
1251
1252        if (rtw_make_wlanhdr(padapter, mem_start, pattrib) == _FAIL) {
1253                RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("rtw_xmitframe_coalesce: rtw_make_wlanhdr fail; drop pkt\n"));
1254                DBG_8192C("rtw_xmitframe_coalesce: rtw_make_wlanhdr fail; drop pkt\n");
1255                res = _FAIL;
1256                goto exit;
1257        }
1258
1259        _rtw_open_pktfile(pkt, &pktfile);
1260        _rtw_pktfile_read(&pktfile, NULL, pattrib->pkt_hdrlen);
1261
1262        frg_inx = 0;
1263        frg_len = pxmitpriv->frag_len - 4;/* 2346-4 = 2342 */
1264
1265        while (1) {
1266                llc_sz = 0;
1267
1268                mpdu_len = frg_len;
1269
1270                pframe = mem_start;
1271
1272                SetMFrag(mem_start);
1273
1274                pframe += pattrib->hdrlen;
1275                mpdu_len -= pattrib->hdrlen;
1276
1277                /* adding icv, if necessary... */
1278                if (pattrib->iv_len) {
1279                        memcpy(pframe, pattrib->iv, pattrib->iv_len);
1280
1281                        RT_TRACE(_module_rtl871x_xmit_c_, _drv_notice_,
1282                                 ("rtw_xmitframe_coalesce: keyid =%d pattrib->iv[3]=%.2x pframe =%.2x %.2x %.2x %.2x\n",
1283                                  padapter->securitypriv.dot11PrivacyKeyIndex, pattrib->iv[3], *pframe, *(pframe+1), *(pframe+2), *(pframe+3)));
1284
1285                        pframe += pattrib->iv_len;
1286
1287                        mpdu_len -= pattrib->iv_len;
1288                }
1289
1290                if (frg_inx == 0) {
1291                        llc_sz = rtw_put_snap(pframe, pattrib->ether_type);
1292                        pframe += llc_sz;
1293                        mpdu_len -= llc_sz;
1294                }
1295
1296                if ((pattrib->icv_len > 0) && (pattrib->bswenc)) {
1297                        mpdu_len -= pattrib->icv_len;
1298                }
1299
1300
1301                if (bmcst) {
1302                        /*  don't do fragment to broadcat/multicast packets */
1303                        mem_sz = _rtw_pktfile_read(&pktfile, pframe, pattrib->pktlen);
1304                } else {
1305                        mem_sz = _rtw_pktfile_read(&pktfile, pframe, mpdu_len);
1306                }
1307
1308                pframe += mem_sz;
1309
1310                if ((pattrib->icv_len > 0) && (pattrib->bswenc)) {
1311                        memcpy(pframe, pattrib->icv, pattrib->icv_len);
1312                        pframe += pattrib->icv_len;
1313                }
1314
1315                frg_inx++;
1316
1317                if (bmcst || (rtw_endofpktfile(&pktfile) == true)) {
1318                        pattrib->nr_frags = frg_inx;
1319
1320                        pattrib->last_txcmdsz = pattrib->hdrlen + pattrib->iv_len + ((pattrib->nr_frags == 1) ? llc_sz:0) +
1321                                        ((pattrib->bswenc) ? pattrib->icv_len : 0) + mem_sz;
1322
1323                        ClearMFrag(mem_start);
1324
1325                        break;
1326                } else
1327                        RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("%s: There're still something in packet!\n", __func__));
1328
1329                addr = (SIZE_PTR)(pframe);
1330
1331                mem_start = (unsigned char *)RND4(addr) + hw_hdr_offset;
1332                memcpy(mem_start, pbuf_start + hw_hdr_offset, pattrib->hdrlen);
1333
1334        }
1335
1336        if (xmitframe_addmic(padapter, pxmitframe) == _FAIL) {
1337                RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("xmitframe_addmic(padapter, pxmitframe) == _FAIL\n"));
1338                DBG_8192C("xmitframe_addmic(padapter, pxmitframe) == _FAIL\n");
1339                res = _FAIL;
1340                goto exit;
1341        }
1342
1343        xmitframe_swencrypt(padapter, pxmitframe);
1344
1345        if (bmcst == false)
1346                update_attrib_vcs_info(padapter, pxmitframe);
1347        else
1348                pattrib->vcs_mode = NONE_VCS;
1349
1350exit:
1351        return res;
1352}
1353
1354/* broadcast or multicast management pkt use BIP, unicast management pkt use CCMP encryption */
1355s32 rtw_mgmt_xmitframe_coalesce(struct adapter *padapter, _pkt *pkt, struct xmit_frame *pxmitframe)
1356{
1357        u8 *pframe, *mem_start = NULL, *tmp_buf = NULL;
1358        u8 subtype;
1359        struct sta_info         *psta = NULL;
1360        struct pkt_attrib       *pattrib = &pxmitframe->attrib;
1361        s32 bmcst = IS_MCAST(pattrib->ra);
1362        u8 *BIP_AAD = NULL;
1363        u8 *MGMT_body = NULL;
1364
1365        struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1366        struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1367        struct ieee80211_hdr    *pwlanhdr;
1368        u8 MME[_MME_IE_LENGTH_];
1369        u32 ori_len;
1370        mem_start = pframe = (u8 *)(pxmitframe->buf_addr) + TXDESC_OFFSET;
1371        pwlanhdr = (struct ieee80211_hdr *)pframe;
1372
1373        ori_len = BIP_AAD_SIZE+pattrib->pktlen;
1374        tmp_buf = BIP_AAD = rtw_zmalloc(ori_len);
1375        subtype = GetFrameSubType(pframe); /* bit(7)~bit(2) */
1376
1377        if (BIP_AAD == NULL)
1378                return _FAIL;
1379
1380        spin_lock_bh(&padapter->security_key_mutex);
1381
1382        /* only support station mode */
1383        if (!check_fwstate(pmlmepriv, WIFI_STATION_STATE) || !check_fwstate(pmlmepriv, _FW_LINKED))
1384                goto xmitframe_coalesce_success;
1385
1386        /* IGTK key is not install, it may not support 802.11w */
1387        if (padapter->securitypriv.binstallBIPkey != true) {
1388                DBG_871X("no instll BIP key\n");
1389                goto xmitframe_coalesce_success;
1390        }
1391        /* station mode doesn't need TX BIP, just ready the code */
1392        if (bmcst) {
1393                int frame_body_len;
1394                u8 mic[16];
1395
1396                memset(MME, 0, 18);
1397
1398                /* other types doesn't need the BIP */
1399                if (GetFrameSubType(pframe) != WIFI_DEAUTH && GetFrameSubType(pframe) != WIFI_DISASSOC)
1400                        goto xmitframe_coalesce_fail;
1401
1402                MGMT_body = pframe + sizeof(struct ieee80211_hdr_3addr);
1403                pframe += pattrib->pktlen;
1404
1405                /* octent 0 and 1 is key index , BIP keyid is 4 or 5, LSB only need octent 0 */
1406                MME[0] = padapter->securitypriv.dot11wBIPKeyid;
1407                /* copy packet number */
1408                memcpy(&MME[2], &pmlmeext->mgnt_80211w_IPN, 6);
1409                /* increase the packet number */
1410                pmlmeext->mgnt_80211w_IPN++;
1411
1412                /* add MME IE with MIC all zero, MME string doesn't include element id and length */
1413                pframe = rtw_set_ie(pframe, _MME_IE_, 16, MME, &(pattrib->pktlen));
1414                pattrib->last_txcmdsz = pattrib->pktlen;
1415                /*  total frame length - header length */
1416                frame_body_len = pattrib->pktlen - sizeof(struct ieee80211_hdr_3addr);
1417
1418                /* conscruct AAD, copy frame control field */
1419                memcpy(BIP_AAD, &pwlanhdr->frame_control, 2);
1420                ClearRetry(BIP_AAD);
1421                ClearPwrMgt(BIP_AAD);
1422                ClearMData(BIP_AAD);
1423                /* conscruct AAD, copy address 1 to address 3 */
1424                memcpy(BIP_AAD+2, pwlanhdr->addr1, 18);
1425                /* copy management fram body */
1426                memcpy(BIP_AAD+BIP_AAD_SIZE, MGMT_body, frame_body_len);
1427                /* calculate mic */
1428                if (omac1_aes_128(padapter->securitypriv.dot11wBIPKey[padapter->securitypriv.dot11wBIPKeyid].skey
1429                        , BIP_AAD, BIP_AAD_SIZE+frame_body_len, mic))
1430                        goto xmitframe_coalesce_fail;
1431
1432                /* copy right BIP mic value, total is 128bits, we use the 0~63 bits */
1433                memcpy(pframe-8, mic, 8);
1434        } else { /* unicast mgmt frame TX */
1435                /* start to encrypt mgmt frame */
1436                if (subtype == WIFI_DEAUTH || subtype == WIFI_DISASSOC ||
1437                        subtype == WIFI_REASSOCREQ || subtype == WIFI_ACTION) {
1438                        if (pattrib->psta)
1439                                psta = pattrib->psta;
1440                        else
1441                                psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
1442
1443                        if (psta == NULL) {
1444
1445                                DBG_871X("%s, psta ==NUL\n", __func__);
1446                                goto xmitframe_coalesce_fail;
1447                        }
1448
1449                        if (!(psta->state & _FW_LINKED) || pxmitframe->buf_addr == NULL) {
1450                                DBG_871X("%s, not _FW_LINKED or addr null\n", __func__);
1451                                goto xmitframe_coalesce_fail;
1452                        }
1453
1454                        /* DBG_871X("%s, action frame category =%d\n", __func__, pframe[WLAN_HDR_A3_LEN]); */
1455                        /* according 802.11-2012 standard, these five types are not robust types */
1456                        if (subtype == WIFI_ACTION &&
1457                        (pframe[WLAN_HDR_A3_LEN] == RTW_WLAN_CATEGORY_PUBLIC ||
1458                        pframe[WLAN_HDR_A3_LEN] == RTW_WLAN_CATEGORY_HT ||
1459                        pframe[WLAN_HDR_A3_LEN] == RTW_WLAN_CATEGORY_UNPROTECTED_WNM ||
1460                        pframe[WLAN_HDR_A3_LEN] == RTW_WLAN_CATEGORY_SELF_PROTECTED  ||
1461                        pframe[WLAN_HDR_A3_LEN] == RTW_WLAN_CATEGORY_P2P))
1462                                goto xmitframe_coalesce_fail;
1463                        /* before encrypt dump the management packet content */
1464                        if (pattrib->encrypt > 0)
1465                                memcpy(pattrib->dot118021x_UncstKey.skey, psta->dot118021x_UncstKey.skey, 16);
1466                        /* bakeup original management packet */
1467                        memcpy(tmp_buf, pframe, pattrib->pktlen);
1468                        /* move to data portion */
1469                        pframe += pattrib->hdrlen;
1470
1471                        /* 802.11w unicast management packet must be _AES_ */
1472                        pattrib->iv_len = 8;
1473                        /* it's MIC of AES */
1474                        pattrib->icv_len = 8;
1475
1476                        switch (pattrib->encrypt) {
1477                        case _AES_:
1478                                        /* set AES IV header */
1479                                        AES_IV(pattrib->iv, psta->dot11wtxpn, 0);
1480                                break;
1481                        default:
1482                                goto xmitframe_coalesce_fail;
1483                        }
1484                        /* insert iv header into management frame */
1485                        memcpy(pframe, pattrib->iv, pattrib->iv_len);
1486                        pframe += pattrib->iv_len;
1487                        /* copy mgmt data portion after CCMP header */
1488                        memcpy(pframe, tmp_buf+pattrib->hdrlen, pattrib->pktlen-pattrib->hdrlen);
1489                        /* move pframe to end of mgmt pkt */
1490                        pframe += pattrib->pktlen-pattrib->hdrlen;
1491                        /* add 8 bytes CCMP IV header to length */
1492                        pattrib->pktlen += pattrib->iv_len;
1493                        if ((pattrib->icv_len > 0) && (pattrib->bswenc)) {
1494                                memcpy(pframe, pattrib->icv, pattrib->icv_len);
1495                                pframe += pattrib->icv_len;
1496                        }
1497                        /* add 8 bytes MIC */
1498                        pattrib->pktlen += pattrib->icv_len;
1499                        /* set final tx command size */
1500                        pattrib->last_txcmdsz = pattrib->pktlen;
1501
1502                        /* set protected bit must be beofre SW encrypt */
1503                        SetPrivacy(mem_start);
1504                        /* software encrypt */
1505                        xmitframe_swencrypt(padapter, pxmitframe);
1506                }
1507        }
1508
1509xmitframe_coalesce_success:
1510        spin_unlock_bh(&padapter->security_key_mutex);
1511        kfree(BIP_AAD);
1512        return _SUCCESS;
1513
1514xmitframe_coalesce_fail:
1515        spin_unlock_bh(&padapter->security_key_mutex);
1516        kfree(BIP_AAD);
1517        return _FAIL;
1518}
1519
1520/* Logical Link Control(LLC) SubNetwork Attachment Point(SNAP) header
1521 * IEEE LLC/SNAP header contains 8 octets
1522 * First 3 octets comprise the LLC portion
1523 * SNAP portion, 5 octets, is divided into two fields:
1524 *Organizationally Unique Identifier(OUI), 3 octets,
1525 *type, defined by that organization, 2 octets.
1526 */
1527s32 rtw_put_snap(u8 *data, u16 h_proto)
1528{
1529        struct ieee80211_snap_hdr *snap;
1530        u8 *oui;
1531
1532        snap = (struct ieee80211_snap_hdr *)data;
1533        snap->dsap = 0xaa;
1534        snap->ssap = 0xaa;
1535        snap->ctrl = 0x03;
1536
1537        if (h_proto == 0x8137 || h_proto == 0x80f3)
1538                oui = P802_1H_OUI;
1539        else
1540                oui = RFC1042_OUI;
1541
1542        snap->oui[0] = oui[0];
1543        snap->oui[1] = oui[1];
1544        snap->oui[2] = oui[2];
1545
1546        *(__be16 *)(data + SNAP_SIZE) = htons(h_proto);
1547
1548        return SNAP_SIZE + sizeof(u16);
1549}
1550
1551void rtw_update_protection(struct adapter *padapter, u8 *ie, uint ie_len)
1552{
1553
1554        uint    protection;
1555        u8 *perp;
1556        sint     erp_len;
1557        struct  xmit_priv *pxmitpriv = &padapter->xmitpriv;
1558        struct  registry_priv *pregistrypriv = &padapter->registrypriv;
1559
1560        switch (pxmitpriv->vcs_setting) {
1561        case DISABLE_VCS:
1562                pxmitpriv->vcs = NONE_VCS;
1563                break;
1564
1565        case ENABLE_VCS:
1566                break;
1567
1568        case AUTO_VCS:
1569        default:
1570                perp = rtw_get_ie(ie, _ERPINFO_IE_, &erp_len, ie_len);
1571                if (perp == NULL)
1572                        pxmitpriv->vcs = NONE_VCS;
1573                else {
1574                        protection = (*(perp + 2)) & BIT(1);
1575                        if (protection) {
1576                                if (pregistrypriv->vcs_type == RTS_CTS)
1577                                        pxmitpriv->vcs = RTS_CTS;
1578                                else
1579                                        pxmitpriv->vcs = CTS_TO_SELF;
1580                        } else
1581                                pxmitpriv->vcs = NONE_VCS;
1582                }
1583
1584                break;
1585
1586        }
1587}
1588
1589void rtw_count_tx_stats(struct adapter *padapter, struct xmit_frame *pxmitframe, int sz)
1590{
1591        struct sta_info *psta = NULL;
1592        struct stainfo_stats *pstats = NULL;
1593        struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1594        struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1595        u8 pkt_num = 1;
1596
1597        if ((pxmitframe->frame_tag&0x0f) == DATA_FRAMETAG) {
1598                pkt_num = pxmitframe->agg_num;
1599
1600                pmlmepriv->LinkDetectInfo.NumTxOkInPeriod += pkt_num;
1601
1602                pxmitpriv->tx_pkts += pkt_num;
1603
1604                pxmitpriv->tx_bytes += sz;
1605
1606                psta = pxmitframe->attrib.psta;
1607                if (psta) {
1608                        pstats = &psta->sta_stats;
1609
1610                        pstats->tx_pkts += pkt_num;
1611
1612                        pstats->tx_bytes += sz;
1613                }
1614        }
1615}
1616
1617static struct xmit_buf *__rtw_alloc_cmd_xmitbuf(struct xmit_priv *pxmitpriv,
1618                enum cmdbuf_type buf_type)
1619{
1620        struct xmit_buf *pxmitbuf =  NULL;
1621
1622        pxmitbuf = &pxmitpriv->pcmd_xmitbuf[buf_type];
1623        if (pxmitbuf !=  NULL) {
1624                pxmitbuf->priv_data = NULL;
1625
1626                pxmitbuf->len = 0;
1627                pxmitbuf->pdata = pxmitbuf->ptail = pxmitbuf->phead;
1628                pxmitbuf->agg_num = 0;
1629                pxmitbuf->pg_num = 0;
1630
1631                if (pxmitbuf->sctx) {
1632                        DBG_871X("%s pxmitbuf->sctx is not NULL\n", __func__);
1633                        rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_ALLOC);
1634                }
1635        } else
1636                DBG_871X("%s fail, no xmitbuf available !!!\n", __func__);
1637
1638        return pxmitbuf;
1639}
1640
1641struct xmit_frame *__rtw_alloc_cmdxmitframe(struct xmit_priv *pxmitpriv,
1642                enum cmdbuf_type buf_type)
1643{
1644        struct xmit_frame               *pcmdframe;
1645        struct xmit_buf         *pxmitbuf;
1646
1647        pcmdframe = rtw_alloc_xmitframe(pxmitpriv);
1648        if (pcmdframe == NULL) {
1649                DBG_871X("%s, alloc xmitframe fail\n", __func__);
1650                return NULL;
1651        }
1652
1653        pxmitbuf = __rtw_alloc_cmd_xmitbuf(pxmitpriv, buf_type);
1654        if (pxmitbuf == NULL) {
1655                DBG_871X("%s, alloc xmitbuf fail\n", __func__);
1656                rtw_free_xmitframe(pxmitpriv, pcmdframe);
1657                return NULL;
1658        }
1659
1660        pcmdframe->frame_tag = MGNT_FRAMETAG;
1661
1662        pcmdframe->pxmitbuf = pxmitbuf;
1663
1664        pcmdframe->buf_addr = pxmitbuf->pbuf;
1665
1666        pxmitbuf->priv_data = pcmdframe;
1667
1668        return pcmdframe;
1669
1670}
1671
1672struct xmit_buf *rtw_alloc_xmitbuf_ext(struct xmit_priv *pxmitpriv)
1673{
1674        _irqL irqL;
1675        struct xmit_buf *pxmitbuf =  NULL;
1676        struct list_head *plist, *phead;
1677        struct __queue *pfree_queue = &pxmitpriv->free_xmit_extbuf_queue;
1678
1679        spin_lock_irqsave(&pfree_queue->lock, irqL);
1680
1681        if (list_empty(&pfree_queue->queue)) {
1682                pxmitbuf = NULL;
1683        } else {
1684
1685                phead = get_list_head(pfree_queue);
1686
1687                plist = get_next(phead);
1688
1689                pxmitbuf = LIST_CONTAINOR(plist, struct xmit_buf, list);
1690
1691                list_del_init(&(pxmitbuf->list));
1692        }
1693
1694        if (pxmitbuf !=  NULL) {
1695                pxmitpriv->free_xmit_extbuf_cnt--;
1696                #ifdef DBG_XMIT_BUF_EXT
1697                DBG_871X("DBG_XMIT_BUF_EXT ALLOC no =%d,  free_xmit_extbuf_cnt =%d\n", pxmitbuf->no, pxmitpriv->free_xmit_extbuf_cnt);
1698                #endif
1699
1700
1701                pxmitbuf->priv_data = NULL;
1702
1703                pxmitbuf->len = 0;
1704                pxmitbuf->pdata = pxmitbuf->ptail = pxmitbuf->phead;
1705                pxmitbuf->agg_num = 1;
1706
1707                if (pxmitbuf->sctx) {
1708                        DBG_871X("%s pxmitbuf->sctx is not NULL\n", __func__);
1709                        rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_ALLOC);
1710                }
1711
1712        }
1713
1714        spin_unlock_irqrestore(&pfree_queue->lock, irqL);
1715
1716        return pxmitbuf;
1717}
1718
1719s32 rtw_free_xmitbuf_ext(struct xmit_priv *pxmitpriv, struct xmit_buf *pxmitbuf)
1720{
1721        _irqL irqL;
1722        struct __queue *pfree_queue = &pxmitpriv->free_xmit_extbuf_queue;
1723
1724        if (pxmitbuf == NULL)
1725                return _FAIL;
1726
1727        spin_lock_irqsave(&pfree_queue->lock, irqL);
1728
1729        list_del_init(&pxmitbuf->list);
1730
1731        list_add_tail(&(pxmitbuf->list), get_list_head(pfree_queue));
1732        pxmitpriv->free_xmit_extbuf_cnt++;
1733        #ifdef DBG_XMIT_BUF_EXT
1734        DBG_871X("DBG_XMIT_BUF_EXT FREE no =%d, free_xmit_extbuf_cnt =%d\n", pxmitbuf->no, pxmitpriv->free_xmit_extbuf_cnt);
1735        #endif
1736
1737        spin_unlock_irqrestore(&pfree_queue->lock, irqL);
1738
1739        return _SUCCESS;
1740}
1741
1742struct xmit_buf *rtw_alloc_xmitbuf(struct xmit_priv *pxmitpriv)
1743{
1744        _irqL irqL;
1745        struct xmit_buf *pxmitbuf =  NULL;
1746        struct list_head *plist, *phead;
1747        struct __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
1748
1749        /* DBG_871X("+rtw_alloc_xmitbuf\n"); */
1750
1751        spin_lock_irqsave(&pfree_xmitbuf_queue->lock, irqL);
1752
1753        if (list_empty(&pfree_xmitbuf_queue->queue)) {
1754                pxmitbuf = NULL;
1755        } else {
1756
1757                phead = get_list_head(pfree_xmitbuf_queue);
1758
1759                plist = get_next(phead);
1760
1761                pxmitbuf = LIST_CONTAINOR(plist, struct xmit_buf, list);
1762
1763                list_del_init(&(pxmitbuf->list));
1764        }
1765
1766        if (pxmitbuf !=  NULL) {
1767                pxmitpriv->free_xmitbuf_cnt--;
1768                #ifdef DBG_XMIT_BUF
1769                DBG_871X("DBG_XMIT_BUF ALLOC no =%d,  free_xmitbuf_cnt =%d\n", pxmitbuf->no, pxmitpriv->free_xmitbuf_cnt);
1770                #endif
1771                /* DBG_871X("alloc, free_xmitbuf_cnt =%d\n", pxmitpriv->free_xmitbuf_cnt); */
1772
1773                pxmitbuf->priv_data = NULL;
1774
1775                pxmitbuf->len = 0;
1776                pxmitbuf->pdata = pxmitbuf->ptail = pxmitbuf->phead;
1777                pxmitbuf->agg_num = 0;
1778                pxmitbuf->pg_num = 0;
1779
1780                if (pxmitbuf->sctx) {
1781                        DBG_871X("%s pxmitbuf->sctx is not NULL\n", __func__);
1782                        rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_ALLOC);
1783                }
1784        }
1785        #ifdef DBG_XMIT_BUF
1786        else
1787                DBG_871X("DBG_XMIT_BUF rtw_alloc_xmitbuf return NULL\n");
1788        #endif
1789
1790        spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, irqL);
1791
1792        return pxmitbuf;
1793}
1794
1795s32 rtw_free_xmitbuf(struct xmit_priv *pxmitpriv, struct xmit_buf *pxmitbuf)
1796{
1797        _irqL irqL;
1798        struct __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
1799
1800        /* DBG_871X("+rtw_free_xmitbuf\n"); */
1801
1802        if (pxmitbuf == NULL)
1803                return _FAIL;
1804
1805        if (pxmitbuf->sctx) {
1806                DBG_871X("%s pxmitbuf->sctx is not NULL\n", __func__);
1807                rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_FREE);
1808        }
1809
1810        if (pxmitbuf->buf_tag == XMITBUF_CMD) {
1811        } else if (pxmitbuf->buf_tag == XMITBUF_MGNT) {
1812                rtw_free_xmitbuf_ext(pxmitpriv, pxmitbuf);
1813        } else {
1814                spin_lock_irqsave(&pfree_xmitbuf_queue->lock, irqL);
1815
1816                list_del_init(&pxmitbuf->list);
1817
1818                list_add_tail(&(pxmitbuf->list), get_list_head(pfree_xmitbuf_queue));
1819
1820                pxmitpriv->free_xmitbuf_cnt++;
1821                /* DBG_871X("FREE, free_xmitbuf_cnt =%d\n", pxmitpriv->free_xmitbuf_cnt); */
1822                #ifdef DBG_XMIT_BUF
1823                DBG_871X("DBG_XMIT_BUF FREE no =%d, free_xmitbuf_cnt =%d\n", pxmitbuf->no, pxmitpriv->free_xmitbuf_cnt);
1824                #endif
1825                spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, irqL);
1826        }
1827        return _SUCCESS;
1828}
1829
1830static void rtw_init_xmitframe(struct xmit_frame *pxframe)
1831{
1832        if (pxframe !=  NULL) { /* default value setting */
1833                pxframe->buf_addr = NULL;
1834                pxframe->pxmitbuf = NULL;
1835
1836                memset(&pxframe->attrib, 0, sizeof(struct pkt_attrib));
1837                /* pxframe->attrib.psta = NULL; */
1838
1839                pxframe->frame_tag = DATA_FRAMETAG;
1840
1841                pxframe->pg_num = 1;
1842                pxframe->agg_num = 1;
1843                pxframe->ack_report = 0;
1844        }
1845}
1846
1847/*
1848Calling context:
18491. OS_TXENTRY
18502. RXENTRY (rx_thread or RX_ISR/RX_CallBack)
1851
1852If we turn on USE_RXTHREAD, then, no need for critical section.
1853Otherwise, we must use _enter/_exit critical to protect free_xmit_queue...
1854
1855Must be very very cautious...
1856
1857*/
1858struct xmit_frame *rtw_alloc_xmitframe(struct xmit_priv *pxmitpriv)/* _queue *pfree_xmit_queue) */
1859{
1860        /*
1861                Please remember to use all the osdep_service api,
1862                and lock/unlock or _enter/_exit critical to protect
1863                pfree_xmit_queue
1864        */
1865
1866        struct xmit_frame *pxframe = NULL;
1867        struct list_head *plist, *phead;
1868        struct __queue *pfree_xmit_queue = &pxmitpriv->free_xmit_queue;
1869
1870        spin_lock_bh(&pfree_xmit_queue->lock);
1871
1872        if (list_empty(&pfree_xmit_queue->queue)) {
1873                RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_alloc_xmitframe:%d\n", pxmitpriv->free_xmitframe_cnt));
1874                pxframe =  NULL;
1875        } else {
1876                phead = get_list_head(pfree_xmit_queue);
1877
1878                plist = get_next(phead);
1879
1880                pxframe = LIST_CONTAINOR(plist, struct xmit_frame, list);
1881
1882                list_del_init(&(pxframe->list));
1883                pxmitpriv->free_xmitframe_cnt--;
1884                RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_alloc_xmitframe():free_xmitframe_cnt =%d\n", pxmitpriv->free_xmitframe_cnt));
1885        }
1886
1887        spin_unlock_bh(&pfree_xmit_queue->lock);
1888
1889        rtw_init_xmitframe(pxframe);
1890        return pxframe;
1891}
1892
1893struct xmit_frame *rtw_alloc_xmitframe_ext(struct xmit_priv *pxmitpriv)
1894{
1895        struct xmit_frame *pxframe = NULL;
1896        struct list_head *plist, *phead;
1897        struct __queue *queue = &pxmitpriv->free_xframe_ext_queue;
1898
1899        spin_lock_bh(&queue->lock);
1900
1901        if (list_empty(&queue->queue)) {
1902                RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_alloc_xmitframe_ext:%d\n", pxmitpriv->free_xframe_ext_cnt));
1903                pxframe =  NULL;
1904        } else {
1905                phead = get_list_head(queue);
1906                plist = get_next(phead);
1907                pxframe = LIST_CONTAINOR(plist, struct xmit_frame, list);
1908
1909                list_del_init(&(pxframe->list));
1910                pxmitpriv->free_xframe_ext_cnt--;
1911                RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_alloc_xmitframe_ext():free_xmitframe_cnt =%d\n", pxmitpriv->free_xframe_ext_cnt));
1912        }
1913
1914        spin_unlock_bh(&queue->lock);
1915
1916        rtw_init_xmitframe(pxframe);
1917
1918        return pxframe;
1919}
1920
1921struct xmit_frame *rtw_alloc_xmitframe_once(struct xmit_priv *pxmitpriv)
1922{
1923        struct xmit_frame *pxframe = NULL;
1924        u8 *alloc_addr;
1925
1926        alloc_addr = rtw_zmalloc(sizeof(struct xmit_frame) + 4);
1927
1928        if (alloc_addr == NULL)
1929                goto exit;
1930
1931        pxframe = (struct xmit_frame *)N_BYTE_ALIGMENT((SIZE_PTR)(alloc_addr), 4);
1932        pxframe->alloc_addr = alloc_addr;
1933
1934        pxframe->padapter = pxmitpriv->adapter;
1935        pxframe->frame_tag = NULL_FRAMETAG;
1936
1937        pxframe->pkt = NULL;
1938
1939        pxframe->buf_addr = NULL;
1940        pxframe->pxmitbuf = NULL;
1941
1942        rtw_init_xmitframe(pxframe);
1943
1944        DBG_871X("################## %s ##################\n", __func__);
1945
1946exit:
1947        return pxframe;
1948}
1949
1950s32 rtw_free_xmitframe(struct xmit_priv *pxmitpriv, struct xmit_frame *pxmitframe)
1951{
1952        struct __queue *queue = NULL;
1953        struct adapter *padapter = pxmitpriv->adapter;
1954        _pkt *pndis_pkt = NULL;
1955
1956        if (pxmitframe == NULL) {
1957                RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("======rtw_free_xmitframe():pxmitframe == NULL!!!!!!!!!!\n"));
1958                goto exit;
1959        }
1960
1961        if (pxmitframe->pkt) {
1962                pndis_pkt = pxmitframe->pkt;
1963                pxmitframe->pkt = NULL;
1964        }
1965
1966        if (pxmitframe->alloc_addr) {
1967                DBG_871X("################## %s with alloc_addr ##################\n", __func__);
1968                kfree(pxmitframe->alloc_addr);
1969                goto check_pkt_complete;
1970        }
1971
1972        if (pxmitframe->ext_tag == 0)
1973                queue = &pxmitpriv->free_xmit_queue;
1974        else if (pxmitframe->ext_tag == 1)
1975                queue = &pxmitpriv->free_xframe_ext_queue;
1976        else {
1977
1978        }
1979
1980        spin_lock_bh(&queue->lock);
1981
1982        list_del_init(&pxmitframe->list);
1983        list_add_tail(&pxmitframe->list, get_list_head(queue));
1984        if (pxmitframe->ext_tag == 0) {
1985                pxmitpriv->free_xmitframe_cnt++;
1986                RT_TRACE(_module_rtl871x_xmit_c_, _drv_debug_, ("rtw_free_xmitframe():free_xmitframe_cnt =%d\n", pxmitpriv->free_xmitframe_cnt));
1987        } else if (pxmitframe->ext_tag == 1) {
1988                pxmitpriv->free_xframe_ext_cnt++;
1989                RT_TRACE(_module_rtl871x_xmit_c_, _drv_debug_, ("rtw_free_xmitframe():free_xframe_ext_cnt =%d\n", pxmitpriv->free_xframe_ext_cnt));
1990        } else {
1991        }
1992
1993        spin_unlock_bh(&queue->lock);
1994
1995check_pkt_complete:
1996
1997        if (pndis_pkt)
1998                rtw_os_pkt_complete(padapter, pndis_pkt);
1999
2000exit:
2001        return _SUCCESS;
2002}
2003
2004void rtw_free_xmitframe_queue(struct xmit_priv *pxmitpriv, struct __queue *pframequeue)
2005{
2006        struct list_head        *plist, *phead;
2007        struct  xmit_frame      *pxmitframe;
2008
2009        spin_lock_bh(&(pframequeue->lock));
2010
2011        phead = get_list_head(pframequeue);
2012        plist = get_next(phead);
2013
2014        while (phead != plist) {
2015
2016                pxmitframe = LIST_CONTAINOR(plist, struct xmit_frame, list);
2017
2018                plist = get_next(plist);
2019
2020                rtw_free_xmitframe(pxmitpriv, pxmitframe);
2021
2022        }
2023        spin_unlock_bh(&(pframequeue->lock));
2024}
2025
2026s32 rtw_xmitframe_enqueue(struct adapter *padapter, struct xmit_frame *pxmitframe)
2027{
2028        DBG_COUNTER(padapter->tx_logs.core_tx_enqueue);
2029        if (rtw_xmit_classifier(padapter, pxmitframe) == _FAIL) {
2030                RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_,
2031                         ("rtw_xmitframe_enqueue: drop xmit pkt for classifier fail\n"));
2032/*              pxmitframe->pkt = NULL; */
2033                return _FAIL;
2034        }
2035
2036        return _SUCCESS;
2037}
2038
2039struct tx_servq *rtw_get_sta_pending(struct adapter *padapter, struct sta_info *psta, sint up, u8 *ac)
2040{
2041        struct tx_servq *ptxservq = NULL;
2042
2043        switch (up) {
2044        case 1:
2045        case 2:
2046                ptxservq = &(psta->sta_xmitpriv.bk_q);
2047                *(ac) = 3;
2048                RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_get_sta_pending : BK\n"));
2049                break;
2050
2051        case 4:
2052        case 5:
2053                ptxservq = &(psta->sta_xmitpriv.vi_q);
2054                *(ac) = 1;
2055                RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_get_sta_pending : VI\n"));
2056                break;
2057
2058        case 6:
2059        case 7:
2060                ptxservq = &(psta->sta_xmitpriv.vo_q);
2061                *(ac) = 0;
2062                RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_get_sta_pending : VO\n"));
2063                break;
2064
2065        case 0:
2066        case 3:
2067        default:
2068                ptxservq = &(psta->sta_xmitpriv.be_q);
2069                *(ac) = 2;
2070                RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_get_sta_pending : BE\n"));
2071        break;
2072
2073        }
2074
2075        return ptxservq;
2076}
2077
2078/*
2079 * Will enqueue pxmitframe to the proper queue,
2080 * and indicate it to xx_pending list.....
2081 */
2082s32 rtw_xmit_classifier(struct adapter *padapter, struct xmit_frame *pxmitframe)
2083{
2084        /* _irqL irqL0; */
2085        u8 ac_index;
2086        struct sta_info *psta;
2087        struct tx_servq *ptxservq;
2088        struct pkt_attrib       *pattrib = &pxmitframe->attrib;
2089        struct hw_xmit  *phwxmits =  padapter->xmitpriv.hwxmits;
2090        sint res = _SUCCESS;
2091
2092        DBG_COUNTER(padapter->tx_logs.core_tx_enqueue_class);
2093
2094/*
2095        if (pattrib->psta) {
2096                psta = pattrib->psta;
2097        } else {
2098                DBG_871X("%s, call rtw_get_stainfo()\n", __func__);
2099                psta = rtw_get_stainfo(pstapriv, pattrib->ra);
2100        }
2101*/
2102
2103        psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
2104        if (pattrib->psta != psta) {
2105                DBG_COUNTER(padapter->tx_logs.core_tx_enqueue_class_err_sta);
2106                DBG_871X("%s, pattrib->psta(%p) != psta(%p)\n", __func__, pattrib->psta, psta);
2107                return _FAIL;
2108        }
2109
2110        if (psta == NULL) {
2111                DBG_COUNTER(padapter->tx_logs.core_tx_enqueue_class_err_nosta);
2112                res = _FAIL;
2113                DBG_8192C("rtw_xmit_classifier: psta == NULL\n");
2114                RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("rtw_xmit_classifier: psta == NULL\n"));
2115                goto exit;
2116        }
2117
2118        if (!(psta->state & _FW_LINKED)) {
2119                DBG_COUNTER(padapter->tx_logs.core_tx_enqueue_class_err_fwlink);
2120                DBG_871X("%s, psta->state(0x%x) != _FW_LINKED\n", __func__, psta->state);
2121                return _FAIL;
2122        }
2123
2124        ptxservq = rtw_get_sta_pending(padapter, psta, pattrib->priority, (u8 *)(&ac_index));
2125
2126        /* spin_lock_irqsave(&pstapending->lock, irqL0); */
2127
2128        if (list_empty(&ptxservq->tx_pending)) {
2129                list_add_tail(&ptxservq->tx_pending, get_list_head(phwxmits[ac_index].sta_queue));
2130        }
2131
2132        /* spin_lock_irqsave(&ptxservq->sta_pending.lock, irqL1); */
2133
2134        list_add_tail(&pxmitframe->list, get_list_head(&ptxservq->sta_pending));
2135        ptxservq->qcnt++;
2136        phwxmits[ac_index].accnt++;
2137
2138        /* spin_unlock_irqrestore(&ptxservq->sta_pending.lock, irqL1); */
2139
2140        /* spin_unlock_irqrestore(&pstapending->lock, irqL0); */
2141
2142exit:
2143
2144        return res;
2145}
2146
2147void rtw_alloc_hwxmits(struct adapter *padapter)
2148{
2149        struct hw_xmit *hwxmits;
2150        struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2151
2152        pxmitpriv->hwxmit_entry = HWXMIT_ENTRY;
2153
2154        pxmitpriv->hwxmits = NULL;
2155
2156        pxmitpriv->hwxmits = rtw_zmalloc(sizeof(struct hw_xmit) * pxmitpriv->hwxmit_entry);
2157
2158        if (pxmitpriv->hwxmits == NULL) {
2159                DBG_871X("alloc hwxmits fail!...\n");
2160                return;
2161        }
2162
2163        hwxmits = pxmitpriv->hwxmits;
2164
2165        if (pxmitpriv->hwxmit_entry == 5) {
2166                /* pxmitpriv->bmc_txqueue.head = 0; */
2167                /* hwxmits[0] .phwtxqueue = &pxmitpriv->bmc_txqueue; */
2168                hwxmits[0] .sta_queue = &pxmitpriv->bm_pending;
2169
2170                /* pxmitpriv->vo_txqueue.head = 0; */
2171                /* hwxmits[1] .phwtxqueue = &pxmitpriv->vo_txqueue; */
2172                hwxmits[1] .sta_queue = &pxmitpriv->vo_pending;
2173
2174                /* pxmitpriv->vi_txqueue.head = 0; */
2175                /* hwxmits[2] .phwtxqueue = &pxmitpriv->vi_txqueue; */
2176                hwxmits[2] .sta_queue = &pxmitpriv->vi_pending;
2177
2178                /* pxmitpriv->bk_txqueue.head = 0; */
2179                /* hwxmits[3] .phwtxqueue = &pxmitpriv->bk_txqueue; */
2180                hwxmits[3] .sta_queue = &pxmitpriv->bk_pending;
2181
2182                /* pxmitpriv->be_txqueue.head = 0; */
2183                /* hwxmits[4] .phwtxqueue = &pxmitpriv->be_txqueue; */
2184                hwxmits[4] .sta_queue = &pxmitpriv->be_pending;
2185
2186        } else if (pxmitpriv->hwxmit_entry == 4) {
2187
2188                /* pxmitpriv->vo_txqueue.head = 0; */
2189                /* hwxmits[0] .phwtxqueue = &pxmitpriv->vo_txqueue; */
2190                hwxmits[0] .sta_queue = &pxmitpriv->vo_pending;
2191
2192                /* pxmitpriv->vi_txqueue.head = 0; */
2193                /* hwxmits[1] .phwtxqueue = &pxmitpriv->vi_txqueue; */
2194                hwxmits[1] .sta_queue = &pxmitpriv->vi_pending;
2195
2196                /* pxmitpriv->be_txqueue.head = 0; */
2197                /* hwxmits[2] .phwtxqueue = &pxmitpriv->be_txqueue; */
2198                hwxmits[2] .sta_queue = &pxmitpriv->be_pending;
2199
2200                /* pxmitpriv->bk_txqueue.head = 0; */
2201                /* hwxmits[3] .phwtxqueue = &pxmitpriv->bk_txqueue; */
2202                hwxmits[3] .sta_queue = &pxmitpriv->bk_pending;
2203        } else {
2204
2205        }
2206
2207
2208}
2209
2210void rtw_free_hwxmits(struct adapter *padapter)
2211{
2212        struct hw_xmit *hwxmits;
2213        struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2214
2215        hwxmits = pxmitpriv->hwxmits;
2216        if (hwxmits)
2217                kfree((u8 *)hwxmits);
2218}
2219
2220void rtw_init_hwxmits(struct hw_xmit *phwxmit, sint entry)
2221{
2222        sint i;
2223
2224        for (i = 0; i < entry; i++, phwxmit++) {
2225                /* spin_lock_init(&phwxmit->xmit_lock); */
2226                /* INIT_LIST_HEAD(&phwxmit->pending); */
2227                /* phwxmit->txcmdcnt = 0; */
2228                phwxmit->accnt = 0;
2229        }
2230}
2231
2232u32 rtw_get_ff_hwaddr(struct xmit_frame *pxmitframe)
2233{
2234        u32 addr;
2235        struct pkt_attrib *pattrib = &pxmitframe->attrib;
2236
2237        switch (pattrib->qsel) {
2238        case 0:
2239        case 3:
2240                addr = BE_QUEUE_INX;
2241                break;
2242        case 1:
2243        case 2:
2244                addr = BK_QUEUE_INX;
2245                break;
2246        case 4:
2247        case 5:
2248                addr = VI_QUEUE_INX;
2249                break;
2250        case 6:
2251        case 7:
2252                addr = VO_QUEUE_INX;
2253                break;
2254        case 0x10:
2255                addr = BCN_QUEUE_INX;
2256                break;
2257        case 0x11:/* BC/MC in PS (HIQ) */
2258                addr = HIGH_QUEUE_INX;
2259                break;
2260        case 0x12:
2261        default:
2262                addr = MGT_QUEUE_INX;
2263                break;
2264
2265        }
2266
2267        return addr;
2268
2269}
2270
2271static void do_queue_select(struct adapter      *padapter, struct pkt_attrib *pattrib)
2272{
2273        u8 qsel;
2274
2275        qsel = pattrib->priority;
2276        RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("### do_queue_select priority =%d , qsel = %d\n", pattrib->priority, qsel));
2277
2278        pattrib->qsel = qsel;
2279}
2280
2281/*
2282 * The main transmit(tx) entry
2283 *
2284 * Return
2285 *1     enqueue
2286 *0     success, hardware will handle this xmit frame(packet)
2287 *<0    fail
2288 */
2289s32 rtw_xmit(struct adapter *padapter, _pkt **ppkt)
2290{
2291        static unsigned long start;
2292        static u32 drop_cnt;
2293
2294        struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2295        struct xmit_frame *pxmitframe = NULL;
2296
2297        s32 res;
2298
2299        DBG_COUNTER(padapter->tx_logs.core_tx);
2300
2301        if (start == 0)
2302                start = jiffies;
2303
2304        pxmitframe = rtw_alloc_xmitframe(pxmitpriv);
2305
2306        if (jiffies_to_msecs(jiffies - start) > 2000) {
2307                if (drop_cnt)
2308                        DBG_871X("DBG_TX_DROP_FRAME %s no more pxmitframe, drop_cnt:%u\n", __func__, drop_cnt);
2309                start = jiffies;
2310                drop_cnt = 0;
2311        }
2312
2313        if (pxmitframe == NULL) {
2314                drop_cnt++;
2315                RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit: no more pxmitframe\n"));
2316                DBG_COUNTER(padapter->tx_logs.core_tx_err_pxmitframe);
2317                return -1;
2318        }
2319
2320        res = update_attrib(padapter, *ppkt, &pxmitframe->attrib);
2321
2322        if (res == _FAIL) {
2323                RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit: update attrib fail\n"));
2324                #ifdef DBG_TX_DROP_FRAME
2325                DBG_871X("DBG_TX_DROP_FRAME %s update attrib fail\n", __func__);
2326                #endif
2327                rtw_free_xmitframe(pxmitpriv, pxmitframe);
2328                return -1;
2329        }
2330        pxmitframe->pkt = *ppkt;
2331
2332        do_queue_select(padapter, &pxmitframe->attrib);
2333
2334        spin_lock_bh(&pxmitpriv->lock);
2335        if (xmitframe_enqueue_for_sleeping_sta(padapter, pxmitframe) == true) {
2336                spin_unlock_bh(&pxmitpriv->lock);
2337                DBG_COUNTER(padapter->tx_logs.core_tx_ap_enqueue);
2338                return 1;
2339        }
2340        spin_unlock_bh(&pxmitpriv->lock);
2341
2342        /* pre_xmitframe */
2343        if (rtw_hal_xmit(padapter, pxmitframe) == false)
2344                return 1;
2345
2346        return 0;
2347}
2348
2349#define RTW_HIQ_FILTER_ALLOW_ALL 0
2350#define RTW_HIQ_FILTER_ALLOW_SPECIAL 1
2351#define RTW_HIQ_FILTER_DENY_ALL 2
2352
2353inline bool xmitframe_hiq_filter(struct xmit_frame *xmitframe)
2354{
2355        bool allow = false;
2356        struct adapter *adapter = xmitframe->padapter;
2357        struct registry_priv *registry = &adapter->registrypriv;
2358
2359        if (registry->hiq_filter == RTW_HIQ_FILTER_ALLOW_SPECIAL) {
2360
2361                struct pkt_attrib *attrib = &xmitframe->attrib;
2362
2363                if (attrib->ether_type == 0x0806
2364                        || attrib->ether_type == 0x888e
2365                        || attrib->dhcp_pkt
2366                ) {
2367                        DBG_871X(FUNC_ADPT_FMT" ether_type:0x%04x%s\n", FUNC_ADPT_ARG(xmitframe->padapter)
2368                                , attrib->ether_type, attrib->dhcp_pkt?" DHCP":"");
2369                        allow = true;
2370                }
2371        } else if (registry->hiq_filter == RTW_HIQ_FILTER_ALLOW_ALL)
2372                allow = true;
2373        else if (registry->hiq_filter == RTW_HIQ_FILTER_DENY_ALL) {
2374        } else
2375                rtw_warn_on(1);
2376
2377        return allow;
2378}
2379
2380sint xmitframe_enqueue_for_sleeping_sta(struct adapter *padapter, struct xmit_frame *pxmitframe)
2381{
2382        sint ret = false;
2383        struct sta_info *psta = NULL;
2384        struct sta_priv *pstapriv = &padapter->stapriv;
2385        struct pkt_attrib *pattrib = &pxmitframe->attrib;
2386        struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2387        sint bmcst = IS_MCAST(pattrib->ra);
2388        bool update_tim = false;
2389
2390        if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == false) {
2391                DBG_COUNTER(padapter->tx_logs.core_tx_ap_enqueue_warn_fwstate);
2392            return ret;
2393        }
2394/*
2395        if (pattrib->psta)
2396        {
2397                psta = pattrib->psta;
2398        }
2399        else
2400        {
2401                DBG_871X("%s, call rtw_get_stainfo()\n", __func__);
2402                psta =rtw_get_stainfo(pstapriv, pattrib->ra);
2403        }
2404*/
2405        psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
2406        if (pattrib->psta != psta) {
2407                DBG_COUNTER(padapter->tx_logs.core_tx_ap_enqueue_warn_sta);
2408                DBG_871X("%s, pattrib->psta(%p) != psta(%p)\n", __func__, pattrib->psta, psta);
2409                return false;
2410        }
2411
2412        if (psta == NULL) {
2413                DBG_COUNTER(padapter->tx_logs.core_tx_ap_enqueue_warn_nosta);
2414                DBG_871X("%s, psta ==NUL\n", __func__);
2415                return false;
2416        }
2417
2418        if (!(psta->state & _FW_LINKED)) {
2419                DBG_COUNTER(padapter->tx_logs.core_tx_ap_enqueue_warn_link);
2420                DBG_871X("%s, psta->state(0x%x) != _FW_LINKED\n", __func__, psta->state);
2421                return false;
2422        }
2423
2424        if (pattrib->triggered == 1) {
2425                DBG_COUNTER(padapter->tx_logs.core_tx_ap_enqueue_warn_trigger);
2426                /* DBG_871X("directly xmit pspoll_triggered packet\n"); */
2427
2428                /* pattrib->triggered = 0; */
2429                if (bmcst && xmitframe_hiq_filter(pxmitframe) == true)
2430                        pattrib->qsel = 0x11;/* HIQ */
2431
2432                return ret;
2433        }
2434
2435
2436        if (bmcst) {
2437                spin_lock_bh(&psta->sleep_q.lock);
2438
2439                if (pstapriv->sta_dz_bitmap) { /* if anyone sta is in ps mode */
2440                        /* pattrib->qsel = 0x11;HIQ */
2441
2442                        list_del_init(&pxmitframe->list);
2443
2444                        /* spin_lock_bh(&psta->sleep_q.lock); */
2445
2446                        list_add_tail(&pxmitframe->list, get_list_head(&psta->sleep_q));
2447
2448                        psta->sleepq_len++;
2449
2450                        if (!(pstapriv->tim_bitmap & BIT(0)))
2451                                update_tim = true;
2452
2453                        pstapriv->tim_bitmap |= BIT(0);/*  */
2454                        pstapriv->sta_dz_bitmap |= BIT(0);
2455
2456                        /* DBG_871X("enqueue, sq_len =%d, tim =%x\n", psta->sleepq_len, pstapriv->tim_bitmap); */
2457
2458                        if (update_tim == true) {
2459                                update_beacon(padapter, _TIM_IE_, NULL, true);
2460                        } else {
2461                                chk_bmc_sleepq_cmd(padapter);
2462                        }
2463
2464                        /* spin_unlock_bh(&psta->sleep_q.lock); */
2465
2466                        ret = true;
2467
2468                        DBG_COUNTER(padapter->tx_logs.core_tx_ap_enqueue_mcast);
2469
2470                }
2471
2472                spin_unlock_bh(&psta->sleep_q.lock);
2473
2474                return ret;
2475
2476        }
2477
2478
2479        spin_lock_bh(&psta->sleep_q.lock);
2480
2481        if (psta->state&WIFI_SLEEP_STATE) {
2482                u8 wmmps_ac = 0;
2483
2484                if (pstapriv->sta_dz_bitmap & BIT(psta->aid)) {
2485                        list_del_init(&pxmitframe->list);
2486
2487                        /* spin_lock_bh(&psta->sleep_q.lock); */
2488
2489                        list_add_tail(&pxmitframe->list, get_list_head(&psta->sleep_q));
2490
2491                        psta->sleepq_len++;
2492
2493                        switch (pattrib->priority) {
2494                        case 1:
2495                        case 2:
2496                                wmmps_ac = psta->uapsd_bk&BIT(0);
2497                                break;
2498                        case 4:
2499                        case 5:
2500                                wmmps_ac = psta->uapsd_vi&BIT(0);
2501                                break;
2502                        case 6:
2503                        case 7:
2504                                wmmps_ac = psta->uapsd_vo&BIT(0);
2505                                break;
2506                        case 0:
2507                        case 3:
2508                        default:
2509                                wmmps_ac = psta->uapsd_be&BIT(0);
2510                                break;
2511                        }
2512
2513                        if (wmmps_ac)
2514                                psta->sleepq_ac_len++;
2515
2516                        if (((psta->has_legacy_ac) && (!wmmps_ac)) || ((!psta->has_legacy_ac) && (wmmps_ac))) {
2517                                if (!(pstapriv->tim_bitmap & BIT(psta->aid)))
2518                                        update_tim = true;
2519
2520                                pstapriv->tim_bitmap |= BIT(psta->aid);
2521
2522                                /* DBG_871X("enqueue, sq_len =%d, tim =%x\n", psta->sleepq_len, pstapriv->tim_bitmap); */
2523
2524                                if (update_tim == true)
2525                                        /* DBG_871X("sleepq_len == 1, update BCNTIM\n"); */
2526                                        /* upate BCN for TIM IE */
2527                                        update_beacon(padapter, _TIM_IE_, NULL, true);
2528                        }
2529
2530                        /* spin_unlock_bh(&psta->sleep_q.lock); */
2531
2532                        /* if (psta->sleepq_len > (NR_XMITFRAME>>3)) */
2533                        /*  */
2534                        /*      wakeup_sta_to_xmit(padapter, psta); */
2535                        /*  */
2536
2537                        ret = true;
2538
2539                        DBG_COUNTER(padapter->tx_logs.core_tx_ap_enqueue_ucast);
2540                }
2541
2542        }
2543
2544        spin_unlock_bh(&psta->sleep_q.lock);
2545
2546        return ret;
2547
2548}
2549
2550static void dequeue_xmitframes_to_sleeping_queue(struct adapter *padapter, struct sta_info *psta, struct __queue *pframequeue)
2551{
2552        sint ret;
2553        struct list_head        *plist, *phead;
2554        u8 ac_index;
2555        struct tx_servq *ptxservq;
2556        struct pkt_attrib       *pattrib;
2557        struct xmit_frame       *pxmitframe;
2558        struct hw_xmit *phwxmits =  padapter->xmitpriv.hwxmits;
2559
2560        phead = get_list_head(pframequeue);
2561        plist = get_next(phead);
2562
2563        while (phead != plist) {
2564                pxmitframe = LIST_CONTAINOR(plist, struct xmit_frame, list);
2565
2566                plist = get_next(plist);
2567
2568                pattrib = &pxmitframe->attrib;
2569
2570                pattrib->triggered = 0;
2571
2572                ret = xmitframe_enqueue_for_sleeping_sta(padapter, pxmitframe);
2573
2574                if (true == ret) {
2575                        ptxservq = rtw_get_sta_pending(padapter, psta, pattrib->priority, (u8 *)(&ac_index));
2576
2577                        ptxservq->qcnt--;
2578                        phwxmits[ac_index].accnt--;
2579                } else {
2580                        /* DBG_871X("xmitframe_enqueue_for_sleeping_sta return false\n"); */
2581                }
2582
2583        }
2584
2585}
2586
2587void stop_sta_xmit(struct adapter *padapter, struct sta_info *psta)
2588{
2589        struct sta_info *psta_bmc;
2590        struct sta_xmit_priv *pstaxmitpriv;
2591        struct sta_priv *pstapriv = &padapter->stapriv;
2592        struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2593
2594        pstaxmitpriv = &psta->sta_xmitpriv;
2595
2596        /* for BC/MC Frames */
2597        psta_bmc = rtw_get_bcmc_stainfo(padapter);
2598
2599
2600        spin_lock_bh(&pxmitpriv->lock);
2601
2602        psta->state |= WIFI_SLEEP_STATE;
2603
2604        pstapriv->sta_dz_bitmap |= BIT(psta->aid);
2605
2606
2607
2608        dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->vo_q.sta_pending);
2609        list_del_init(&(pstaxmitpriv->vo_q.tx_pending));
2610
2611
2612        dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->vi_q.sta_pending);
2613        list_del_init(&(pstaxmitpriv->vi_q.tx_pending));
2614
2615
2616        dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->be_q.sta_pending);
2617        list_del_init(&(pstaxmitpriv->be_q.tx_pending));
2618
2619
2620        dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->bk_q.sta_pending);
2621        list_del_init(&(pstaxmitpriv->bk_q.tx_pending));
2622
2623        /* for BC/MC Frames */
2624        pstaxmitpriv = &psta_bmc->sta_xmitpriv;
2625        dequeue_xmitframes_to_sleeping_queue(padapter, psta_bmc, &pstaxmitpriv->be_q.sta_pending);
2626        list_del_init(&(pstaxmitpriv->be_q.tx_pending));
2627
2628        spin_unlock_bh(&pxmitpriv->lock);
2629}
2630
2631void wakeup_sta_to_xmit(struct adapter *padapter, struct sta_info *psta)
2632{
2633        u8 update_mask = 0, wmmps_ac = 0;
2634        struct sta_info *psta_bmc;
2635        struct list_head        *xmitframe_plist, *xmitframe_phead;
2636        struct xmit_frame *pxmitframe = NULL;
2637        struct sta_priv *pstapriv = &padapter->stapriv;
2638        struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2639
2640        psta_bmc = rtw_get_bcmc_stainfo(padapter);
2641
2642
2643        /* spin_lock_bh(&psta->sleep_q.lock); */
2644        spin_lock_bh(&pxmitpriv->lock);
2645
2646        xmitframe_phead = get_list_head(&psta->sleep_q);
2647        xmitframe_plist = get_next(xmitframe_phead);
2648
2649        while (xmitframe_phead != xmitframe_plist) {
2650                pxmitframe = LIST_CONTAINOR(xmitframe_plist, struct xmit_frame, list);
2651
2652                xmitframe_plist = get_next(xmitframe_plist);
2653
2654                list_del_init(&pxmitframe->list);
2655
2656                switch (pxmitframe->attrib.priority) {
2657                case 1:
2658                case 2:
2659                        wmmps_ac = psta->uapsd_bk&BIT(1);
2660                        break;
2661                case 4:
2662                case 5:
2663                        wmmps_ac = psta->uapsd_vi&BIT(1);
2664                        break;
2665                case 6:
2666                case 7:
2667                        wmmps_ac = psta->uapsd_vo&BIT(1);
2668                        break;
2669                case 0:
2670                case 3:
2671                default:
2672                        wmmps_ac = psta->uapsd_be&BIT(1);
2673                        break;
2674                }
2675
2676                psta->sleepq_len--;
2677                if (psta->sleepq_len > 0)
2678                        pxmitframe->attrib.mdata = 1;
2679                else
2680                        pxmitframe->attrib.mdata = 0;
2681
2682                if (wmmps_ac) {
2683                        psta->sleepq_ac_len--;
2684                        if (psta->sleepq_ac_len > 0) {
2685                                pxmitframe->attrib.mdata = 1;
2686                                pxmitframe->attrib.eosp = 0;
2687                        } else {
2688                                pxmitframe->attrib.mdata = 0;
2689                                pxmitframe->attrib.eosp = 1;
2690                        }
2691                }
2692
2693                pxmitframe->attrib.triggered = 1;
2694
2695/*
2696                spin_unlock_bh(&psta->sleep_q.lock);
2697                if (rtw_hal_xmit(padapter, pxmitframe) == true)
2698                {
2699                        rtw_os_xmit_complete(padapter, pxmitframe);
2700                }
2701                spin_lock_bh(&psta->sleep_q.lock);
2702*/
2703                rtw_hal_xmitframe_enqueue(padapter, pxmitframe);
2704
2705
2706        }
2707
2708        if (psta->sleepq_len == 0) {
2709                if (pstapriv->tim_bitmap & BIT(psta->aid)) {
2710                        /* DBG_871X("wakeup to xmit, qlen == 0, update_BCNTIM, tim =%x\n", pstapriv->tim_bitmap); */
2711                        /* upate BCN for TIM IE */
2712                        /* update_BCNTIM(padapter); */
2713                        update_mask = BIT(0);
2714                }
2715
2716                pstapriv->tim_bitmap &= ~BIT(psta->aid);
2717
2718                if (psta->state&WIFI_SLEEP_STATE)
2719                        psta->state ^= WIFI_SLEEP_STATE;
2720
2721                if (psta->state & WIFI_STA_ALIVE_CHK_STATE) {
2722                        DBG_871X("%s alive check\n", __func__);
2723                        psta->expire_to = pstapriv->expire_to;
2724                        psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
2725                }
2726
2727                pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
2728        }
2729
2730        /* for BC/MC Frames */
2731        if (!psta_bmc)
2732                goto _exit;
2733
2734        if ((pstapriv->sta_dz_bitmap&0xfffe) == 0x0) { /* no any sta in ps mode */
2735                xmitframe_phead = get_list_head(&psta_bmc->sleep_q);
2736                xmitframe_plist = get_next(xmitframe_phead);
2737
2738                while (xmitframe_phead != xmitframe_plist) {
2739                        pxmitframe = LIST_CONTAINOR(xmitframe_plist, struct xmit_frame, list);
2740
2741                        xmitframe_plist = get_next(xmitframe_plist);
2742
2743                        list_del_init(&pxmitframe->list);
2744
2745                        psta_bmc->sleepq_len--;
2746                        if (psta_bmc->sleepq_len > 0)
2747                                pxmitframe->attrib.mdata = 1;
2748                        else
2749                                pxmitframe->attrib.mdata = 0;
2750
2751
2752                        pxmitframe->attrib.triggered = 1;
2753/*
2754                        spin_unlock_bh(&psta_bmc->sleep_q.lock);
2755                        if (rtw_hal_xmit(padapter, pxmitframe) == true)
2756                        {
2757                                rtw_os_xmit_complete(padapter, pxmitframe);
2758                        }
2759                        spin_lock_bh(&psta_bmc->sleep_q.lock);
2760
2761*/
2762                        rtw_hal_xmitframe_enqueue(padapter, pxmitframe);
2763
2764                }
2765
2766                if (psta_bmc->sleepq_len == 0) {
2767                        if (pstapriv->tim_bitmap & BIT(0)) {
2768                                /* DBG_871X("wakeup to xmit, qlen == 0, update_BCNTIM, tim =%x\n", pstapriv->tim_bitmap); */
2769                                /* upate BCN for TIM IE */
2770                                /* update_BCNTIM(padapter); */
2771                                update_mask |= BIT(1);
2772                        }
2773                        pstapriv->tim_bitmap &= ~BIT(0);
2774                        pstapriv->sta_dz_bitmap &= ~BIT(0);
2775                }
2776
2777        }
2778
2779_exit:
2780
2781        /* spin_unlock_bh(&psta_bmc->sleep_q.lock); */
2782        spin_unlock_bh(&pxmitpriv->lock);
2783
2784        if (update_mask)
2785                /* update_BCNTIM(padapter); */
2786                /* printk("%s => call update_beacon\n", __func__); */
2787                update_beacon(padapter, _TIM_IE_, NULL, true);
2788
2789}
2790
2791void xmit_delivery_enabled_frames(struct adapter *padapter, struct sta_info *psta)
2792{
2793        u8 wmmps_ac = 0;
2794        struct list_head        *xmitframe_plist, *xmitframe_phead;
2795        struct xmit_frame *pxmitframe = NULL;
2796        struct sta_priv *pstapriv = &padapter->stapriv;
2797        struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2798
2799
2800        /* spin_lock_bh(&psta->sleep_q.lock); */
2801        spin_lock_bh(&pxmitpriv->lock);
2802
2803        xmitframe_phead = get_list_head(&psta->sleep_q);
2804        xmitframe_plist = get_next(xmitframe_phead);
2805
2806        while (xmitframe_phead != xmitframe_plist) {
2807                pxmitframe = LIST_CONTAINOR(xmitframe_plist, struct xmit_frame, list);
2808
2809                xmitframe_plist = get_next(xmitframe_plist);
2810
2811                switch (pxmitframe->attrib.priority) {
2812                case 1:
2813                case 2:
2814                        wmmps_ac = psta->uapsd_bk&BIT(1);
2815                        break;
2816                case 4:
2817                case 5:
2818                        wmmps_ac = psta->uapsd_vi&BIT(1);
2819                        break;
2820                case 6:
2821                case 7:
2822                        wmmps_ac = psta->uapsd_vo&BIT(1);
2823                        break;
2824                case 0:
2825                case 3:
2826                default:
2827                        wmmps_ac = psta->uapsd_be&BIT(1);
2828                        break;
2829                }
2830
2831                if (!wmmps_ac)
2832                        continue;
2833
2834                list_del_init(&pxmitframe->list);
2835
2836                psta->sleepq_len--;
2837                psta->sleepq_ac_len--;
2838
2839                if (psta->sleepq_ac_len > 0) {
2840                        pxmitframe->attrib.mdata = 1;
2841                        pxmitframe->attrib.eosp = 0;
2842                } else {
2843                        pxmitframe->attrib.mdata = 0;
2844                        pxmitframe->attrib.eosp = 1;
2845                }
2846
2847                pxmitframe->attrib.triggered = 1;
2848                rtw_hal_xmitframe_enqueue(padapter, pxmitframe);
2849
2850                if ((psta->sleepq_ac_len == 0) && (!psta->has_legacy_ac) && (wmmps_ac)) {
2851                        pstapriv->tim_bitmap &= ~BIT(psta->aid);
2852
2853                        /* DBG_871X("wakeup to xmit, qlen == 0, update_BCNTIM, tim =%x\n", pstapriv->tim_bitmap); */
2854                        /* upate BCN for TIM IE */
2855                        /* update_BCNTIM(padapter); */
2856                        update_beacon(padapter, _TIM_IE_, NULL, true);
2857                        /* update_mask = BIT(0); */
2858                }
2859
2860        }
2861
2862        /* spin_unlock_bh(&psta->sleep_q.lock); */
2863        spin_unlock_bh(&pxmitpriv->lock);
2864
2865        return;
2866}
2867
2868void enqueue_pending_xmitbuf(
2869        struct xmit_priv *pxmitpriv,
2870        struct xmit_buf *pxmitbuf)
2871{
2872        struct __queue *pqueue;
2873        struct adapter *pri_adapter = pxmitpriv->adapter;
2874
2875        pqueue = &pxmitpriv->pending_xmitbuf_queue;
2876
2877        spin_lock_bh(&pqueue->lock);
2878        list_del_init(&pxmitbuf->list);
2879        list_add_tail(&pxmitbuf->list, get_list_head(pqueue));
2880        spin_unlock_bh(&pqueue->lock);
2881
2882        complete(&(pri_adapter->xmitpriv.xmit_comp));
2883}
2884
2885void enqueue_pending_xmitbuf_to_head(
2886        struct xmit_priv *pxmitpriv,
2887        struct xmit_buf *pxmitbuf)
2888{
2889        struct __queue *pqueue;
2890
2891        pqueue = &pxmitpriv->pending_xmitbuf_queue;
2892
2893        spin_lock_bh(&pqueue->lock);
2894        list_del_init(&pxmitbuf->list);
2895        list_add(&pxmitbuf->list, get_list_head(pqueue));
2896        spin_unlock_bh(&pqueue->lock);
2897}
2898
2899struct xmit_buf *dequeue_pending_xmitbuf(
2900        struct xmit_priv *pxmitpriv)
2901{
2902        struct xmit_buf *pxmitbuf;
2903        struct __queue *pqueue;
2904
2905
2906        pxmitbuf = NULL;
2907        pqueue = &pxmitpriv->pending_xmitbuf_queue;
2908
2909        spin_lock_bh(&pqueue->lock);
2910
2911        if (!list_empty(&pqueue->queue)) {
2912                struct list_head *plist, *phead;
2913
2914                phead = get_list_head(pqueue);
2915                plist = get_next(phead);
2916                pxmitbuf = LIST_CONTAINOR(plist, struct xmit_buf, list);
2917                list_del_init(&pxmitbuf->list);
2918        }
2919
2920        spin_unlock_bh(&pqueue->lock);
2921
2922        return pxmitbuf;
2923}
2924
2925struct xmit_buf *dequeue_pending_xmitbuf_under_survey(
2926        struct xmit_priv *pxmitpriv)
2927{
2928        struct xmit_buf *pxmitbuf;
2929        struct __queue *pqueue;
2930
2931
2932        pxmitbuf = NULL;
2933        pqueue = &pxmitpriv->pending_xmitbuf_queue;
2934
2935        spin_lock_bh(&pqueue->lock);
2936
2937        if (!list_empty(&pqueue->queue)) {
2938                struct list_head *plist, *phead;
2939                u8 type;
2940
2941                phead = get_list_head(pqueue);
2942                plist = phead;
2943                do {
2944                        plist = get_next(plist);
2945                        if (plist == phead)
2946                                break;
2947
2948                        pxmitbuf = LIST_CONTAINOR(plist, struct xmit_buf, list);
2949
2950                        type = GetFrameSubType(pxmitbuf->pbuf + TXDESC_OFFSET);
2951
2952                        if ((type == WIFI_PROBEREQ) ||
2953                                (type == WIFI_DATA_NULL) ||
2954                                (type == WIFI_QOS_DATA_NULL)) {
2955                                list_del_init(&pxmitbuf->list);
2956                                break;
2957                        }
2958                        pxmitbuf = NULL;
2959                } while (1);
2960        }
2961
2962        spin_unlock_bh(&pqueue->lock);
2963
2964        return pxmitbuf;
2965}
2966
2967sint check_pending_xmitbuf(
2968        struct xmit_priv *pxmitpriv)
2969{
2970        struct __queue *pqueue;
2971        sint    ret = false;
2972
2973        pqueue = &pxmitpriv->pending_xmitbuf_queue;
2974
2975        spin_lock_bh(&pqueue->lock);
2976
2977        if (!list_empty(&pqueue->queue))
2978                ret = true;
2979
2980        spin_unlock_bh(&pqueue->lock);
2981
2982        return ret;
2983}
2984
2985int rtw_xmit_thread(void *context)
2986{
2987        s32 err;
2988        struct adapter *padapter;
2989
2990
2991        err = _SUCCESS;
2992        padapter = context;
2993
2994        thread_enter("RTW_XMIT_THREAD");
2995
2996        do {
2997                err = rtw_hal_xmit_thread_handler(padapter);
2998                flush_signals_thread();
2999        } while (_SUCCESS == err);
3000
3001        complete(&padapter->xmitpriv.terminate_xmitthread_comp);
3002
3003        thread_exit();
3004}
3005
3006void rtw_sctx_init(struct submit_ctx *sctx, int timeout_ms)
3007{
3008        sctx->timeout_ms = timeout_ms;
3009        sctx->submit_time = jiffies;
3010        init_completion(&sctx->done);
3011        sctx->status = RTW_SCTX_SUBMITTED;
3012}
3013
3014int rtw_sctx_wait(struct submit_ctx *sctx, const char *msg)
3015{
3016        int ret = _FAIL;
3017        unsigned long expire;
3018        int status = 0;
3019
3020        expire = sctx->timeout_ms ? msecs_to_jiffies(sctx->timeout_ms) : MAX_SCHEDULE_TIMEOUT;
3021        if (!wait_for_completion_timeout(&sctx->done, expire)) {
3022                /* timeout, do something?? */
3023                status = RTW_SCTX_DONE_TIMEOUT;
3024                DBG_871X("%s timeout: %s\n", __func__, msg);
3025        } else {
3026                status = sctx->status;
3027        }
3028
3029        if (status == RTW_SCTX_DONE_SUCCESS) {
3030                ret = _SUCCESS;
3031        }
3032
3033        return ret;
3034}
3035
3036static bool rtw_sctx_chk_warning_status(int status)
3037{
3038        switch (status) {
3039        case RTW_SCTX_DONE_UNKNOWN:
3040        case RTW_SCTX_DONE_BUF_ALLOC:
3041        case RTW_SCTX_DONE_BUF_FREE:
3042
3043        case RTW_SCTX_DONE_DRV_STOP:
3044        case RTW_SCTX_DONE_DEV_REMOVE:
3045                return true;
3046        default:
3047                return false;
3048        }
3049}
3050
3051void rtw_sctx_done_err(struct submit_ctx **sctx, int status)
3052{
3053        if (*sctx) {
3054                if (rtw_sctx_chk_warning_status(status))
3055                        DBG_871X("%s status:%d\n", __func__, status);
3056                (*sctx)->status = status;
3057                complete(&((*sctx)->done));
3058                *sctx = NULL;
3059        }
3060}
3061
3062void rtw_sctx_done(struct submit_ctx **sctx)
3063{
3064        rtw_sctx_done_err(sctx, RTW_SCTX_DONE_SUCCESS);
3065}
3066
3067int rtw_ack_tx_wait(struct xmit_priv *pxmitpriv, u32 timeout_ms)
3068{
3069        struct submit_ctx *pack_tx_ops = &pxmitpriv->ack_tx_ops;
3070
3071        pack_tx_ops->submit_time = jiffies;
3072        pack_tx_ops->timeout_ms = timeout_ms;
3073        pack_tx_ops->status = RTW_SCTX_SUBMITTED;
3074
3075        return rtw_sctx_wait(pack_tx_ops, __func__);
3076}
3077
3078void rtw_ack_tx_done(struct xmit_priv *pxmitpriv, int status)
3079{
3080        struct submit_ctx *pack_tx_ops = &pxmitpriv->ack_tx_ops;
3081
3082        if (pxmitpriv->ack_tx) {
3083                rtw_sctx_done_err(&pack_tx_ops, status);
3084        } else {
3085                DBG_871X("%s ack_tx not set\n", __func__);
3086        }
3087}
3088