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