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