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