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