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