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