GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / staging / rtl8723bs / core / rtw_xmit.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  ******************************************************************************/
15 #define _RTW_XMIT_C_
16
17 #include <drv_types.h>
18 #include <rtw_debug.h>
19
20 static u8 P802_1H_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0xf8 };
21 static u8 RFC1042_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0x00 };
22
23 static void _init_txservq(struct tx_servq *ptxservq)
24 {
25         INIT_LIST_HEAD(&ptxservq->tx_pending);
26         _rtw_init_queue(&ptxservq->sta_pending);
27         ptxservq->qcnt = 0;
28 }
29
30 void _rtw_init_sta_xmit_priv(struct sta_xmit_priv *psta_xmitpriv)
31 {
32         memset((unsigned char *)psta_xmitpriv, 0, sizeof(struct sta_xmit_priv));
33
34         spin_lock_init(&psta_xmitpriv->lock);
35
36         /* for (i = 0 ; i < MAX_NUMBLKS; i++) */
37         /*      _init_txservq(&(psta_xmitpriv->blk_q[i])); */
38
39         _init_txservq(&psta_xmitpriv->be_q);
40         _init_txservq(&psta_xmitpriv->bk_q);
41         _init_txservq(&psta_xmitpriv->vi_q);
42         _init_txservq(&psta_xmitpriv->vo_q);
43         INIT_LIST_HEAD(&psta_xmitpriv->legacy_dz);
44         INIT_LIST_HEAD(&psta_xmitpriv->apsd);
45 }
46
47 s32     _rtw_init_xmit_priv(struct xmit_priv *pxmitpriv, struct adapter *padapter)
48 {
49         int i;
50         struct xmit_buf *pxmitbuf;
51         struct xmit_frame *pxframe;
52         sint    res = _SUCCESS;
53
54         /*  We don't need to memset padapter->XXX to zero, because adapter is allocated by vzalloc(). */
55         /* memset((unsigned char *)pxmitpriv, 0, sizeof(struct xmit_priv)); */
56
57         spin_lock_init(&pxmitpriv->lock);
58         spin_lock_init(&pxmitpriv->lock_sctx);
59         sema_init(&pxmitpriv->xmit_sema, 0);
60         sema_init(&pxmitpriv->terminate_xmitthread_sema, 0);
61
62         /*
63         Please insert all the queue initializaiton using _rtw_init_queue below
64         */
65
66         pxmitpriv->adapter = padapter;
67
68         /* for (i = 0 ; i < MAX_NUMBLKS; i++) */
69         /*      _rtw_init_queue(&pxmitpriv->blk_strms[i]); */
70
71         _rtw_init_queue(&pxmitpriv->be_pending);
72         _rtw_init_queue(&pxmitpriv->bk_pending);
73         _rtw_init_queue(&pxmitpriv->vi_pending);
74         _rtw_init_queue(&pxmitpriv->vo_pending);
75         _rtw_init_queue(&pxmitpriv->bm_pending);
76
77         /* _rtw_init_queue(&pxmitpriv->legacy_dz_queue); */
78         /* _rtw_init_queue(&pxmitpriv->apsd_queue); */
79
80         _rtw_init_queue(&pxmitpriv->free_xmit_queue);
81
82         /*
83         Please allocate memory with the sz = (struct xmit_frame) * NR_XMITFRAME,
84         and initialize free_xmit_frame below.
85         Please also apply  free_txobj to link_up all the xmit_frames...
86         */
87
88         pxmitpriv->pallocated_frame_buf = vzalloc(NR_XMITFRAME * sizeof(struct xmit_frame) + 4);
89
90         if (pxmitpriv->pallocated_frame_buf  == NULL) {
91                 pxmitpriv->pxmit_frame_buf = NULL;
92                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("alloc xmit_frame fail!\n"));
93                 res = _FAIL;
94                 goto exit;
95         }
96         pxmitpriv->pxmit_frame_buf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitpriv->pallocated_frame_buf), 4);
97         /* pxmitpriv->pxmit_frame_buf = pxmitpriv->pallocated_frame_buf + 4 - */
98         /*                                              ((SIZE_PTR) (pxmitpriv->pallocated_frame_buf) &3); */
99
100         pxframe = (struct xmit_frame *) pxmitpriv->pxmit_frame_buf;
101
102         for (i = 0; i < NR_XMITFRAME; i++) {
103                 INIT_LIST_HEAD(&(pxframe->list));
104
105                 pxframe->padapter = padapter;
106                 pxframe->frame_tag = NULL_FRAMETAG;
107
108                 pxframe->pkt = NULL;
109
110                 pxframe->buf_addr = NULL;
111                 pxframe->pxmitbuf = NULL;
112
113                 list_add_tail(&(pxframe->list), &(pxmitpriv->free_xmit_queue.queue));
114
115                 pxframe++;
116         }
117
118         pxmitpriv->free_xmitframe_cnt = NR_XMITFRAME;
119
120         pxmitpriv->frag_len = MAX_FRAG_THRESHOLD;
121
122
123         /* init xmit_buf */
124         _rtw_init_queue(&pxmitpriv->free_xmitbuf_queue);
125         _rtw_init_queue(&pxmitpriv->pending_xmitbuf_queue);
126
127         pxmitpriv->pallocated_xmitbuf = vzalloc(NR_XMITBUFF * sizeof(struct xmit_buf) + 4);
128
129         if (pxmitpriv->pallocated_xmitbuf  == NULL) {
130                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("alloc xmit_buf fail!\n"));
131                 res = _FAIL;
132                 goto exit;
133         }
134
135         pxmitpriv->pxmitbuf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitpriv->pallocated_xmitbuf), 4);
136         /* pxmitpriv->pxmitbuf = pxmitpriv->pallocated_xmitbuf + 4 - */
137         /*                                              ((SIZE_PTR) (pxmitpriv->pallocated_xmitbuf) &3); */
138
139         pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
140
141         for (i = 0; i < NR_XMITBUFF; i++) {
142                 INIT_LIST_HEAD(&pxmitbuf->list);
143
144                 pxmitbuf->priv_data = NULL;
145                 pxmitbuf->padapter = padapter;
146                 pxmitbuf->buf_tag = XMITBUF_DATA;
147
148                 /* Tx buf allocation may fail sometimes, so sleep and retry. */
149                 res = rtw_os_xmit_resource_alloc(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ), true);
150                 if (res == _FAIL) {
151                         msleep(10);
152                         res = rtw_os_xmit_resource_alloc(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ), true);
153                         if (res == _FAIL)
154                                 goto exit;
155                 }
156
157                 pxmitbuf->phead = pxmitbuf->pbuf;
158                 pxmitbuf->pend = pxmitbuf->pbuf + MAX_XMITBUF_SZ;
159                 pxmitbuf->len = 0;
160                 pxmitbuf->pdata = pxmitbuf->ptail = pxmitbuf->phead;
161
162                 pxmitbuf->flags = XMIT_VO_QUEUE;
163
164                 list_add_tail(&pxmitbuf->list, &(pxmitpriv->free_xmitbuf_queue.queue));
165                 #ifdef DBG_XMIT_BUF
166                 pxmitbuf->no = i;
167                 #endif
168
169                 pxmitbuf++;
170
171         }
172
173         pxmitpriv->free_xmitbuf_cnt = NR_XMITBUFF;
174
175         /* init xframe_ext queue,  the same count as extbuf  */
176         _rtw_init_queue(&pxmitpriv->free_xframe_ext_queue);
177
178         pxmitpriv->xframe_ext_alloc_addr = vzalloc(NR_XMIT_EXTBUFF * sizeof(struct xmit_frame) + 4);
179
180         if (pxmitpriv->xframe_ext_alloc_addr  == NULL) {
181                 pxmitpriv->xframe_ext = NULL;
182                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("alloc xframe_ext fail!\n"));
183                 res = _FAIL;
184                 goto exit;
185         }
186         pxmitpriv->xframe_ext = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitpriv->xframe_ext_alloc_addr), 4);
187         pxframe = (struct xmit_frame *)pxmitpriv->xframe_ext;
188
189         for (i = 0; i < NR_XMIT_EXTBUFF; i++) {
190                 INIT_LIST_HEAD(&(pxframe->list));
191
192                 pxframe->padapter = padapter;
193                 pxframe->frame_tag = NULL_FRAMETAG;
194
195                 pxframe->pkt = NULL;
196
197                 pxframe->buf_addr = NULL;
198                 pxframe->pxmitbuf = NULL;
199
200                 pxframe->ext_tag = 1;
201
202                 list_add_tail(&(pxframe->list), &(pxmitpriv->free_xframe_ext_queue.queue));
203
204                 pxframe++;
205         }
206         pxmitpriv->free_xframe_ext_cnt = NR_XMIT_EXTBUFF;
207
208         /*  Init xmit extension buff */
209         _rtw_init_queue(&pxmitpriv->free_xmit_extbuf_queue);
210
211         pxmitpriv->pallocated_xmit_extbuf = vzalloc(NR_XMIT_EXTBUFF * sizeof(struct xmit_buf) + 4);
212
213         if (pxmitpriv->pallocated_xmit_extbuf  == NULL) {
214                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("alloc xmit_extbuf fail!\n"));
215                 res = _FAIL;
216                 goto exit;
217         }
218
219         pxmitpriv->pxmit_extbuf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitpriv->pallocated_xmit_extbuf), 4);
220
221         pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmit_extbuf;
222
223         for (i = 0; i < NR_XMIT_EXTBUFF; i++) {
224                 INIT_LIST_HEAD(&pxmitbuf->list);
225
226                 pxmitbuf->priv_data = NULL;
227                 pxmitbuf->padapter = padapter;
228                 pxmitbuf->buf_tag = XMITBUF_MGNT;
229
230                 res = rtw_os_xmit_resource_alloc(padapter, pxmitbuf, MAX_XMIT_EXTBUF_SZ + XMITBUF_ALIGN_SZ, true);
231                 if (res == _FAIL) {
232                         res = _FAIL;
233                         goto exit;
234                 }
235
236                 pxmitbuf->phead = pxmitbuf->pbuf;
237                 pxmitbuf->pend = pxmitbuf->pbuf + MAX_XMIT_EXTBUF_SZ;
238                 pxmitbuf->len = 0;
239                 pxmitbuf->pdata = pxmitbuf->ptail = pxmitbuf->phead;
240
241                 list_add_tail(&pxmitbuf->list, &(pxmitpriv->free_xmit_extbuf_queue.queue));
242                 #ifdef DBG_XMIT_BUF_EXT
243                 pxmitbuf->no = i;
244                 #endif
245                 pxmitbuf++;
246
247         }
248
249         pxmitpriv->free_xmit_extbuf_cnt = NR_XMIT_EXTBUFF;
250
251         for (i = 0; i < CMDBUF_MAX; i++) {
252                 pxmitbuf = &pxmitpriv->pcmd_xmitbuf[i];
253                 if (pxmitbuf) {
254                         INIT_LIST_HEAD(&pxmitbuf->list);
255
256                         pxmitbuf->priv_data = NULL;
257                         pxmitbuf->padapter = padapter;
258                         pxmitbuf->buf_tag = XMITBUF_CMD;
259
260                         res = rtw_os_xmit_resource_alloc(padapter, pxmitbuf, MAX_CMDBUF_SZ+XMITBUF_ALIGN_SZ, true);
261                         if (res == _FAIL) {
262                                 res = _FAIL;
263                                 goto exit;
264                         }
265
266                         pxmitbuf->phead = pxmitbuf->pbuf;
267                         pxmitbuf->pend = pxmitbuf->pbuf + MAX_CMDBUF_SZ;
268                         pxmitbuf->len = 0;
269                         pxmitbuf->pdata = pxmitbuf->ptail = pxmitbuf->phead;
270                         pxmitbuf->alloc_sz = MAX_CMDBUF_SZ+XMITBUF_ALIGN_SZ;
271                 }
272         }
273
274         res = rtw_alloc_hwxmits(padapter);
275         if (res == _FAIL)
276                 goto exit;
277         rtw_init_hwxmits(pxmitpriv->hwxmits, pxmitpriv->hwxmit_entry);
278
279         for (i = 0; i < 4; i++) {
280                 pxmitpriv->wmm_para_seq[i] = i;
281         }
282
283         pxmitpriv->ack_tx = false;
284         mutex_init(&pxmitpriv->ack_tx_mutex);
285         rtw_sctx_init(&pxmitpriv->ack_tx_ops, 0);
286
287         rtw_hal_init_xmit_priv(padapter);
288
289 exit:
290         return res;
291 }
292
293 void _rtw_free_xmit_priv(struct xmit_priv *pxmitpriv)
294 {
295         int i;
296         struct adapter *padapter = pxmitpriv->adapter;
297         struct xmit_frame       *pxmitframe = (struct xmit_frame *) pxmitpriv->pxmit_frame_buf;
298         struct xmit_buf *pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
299
300         rtw_hal_free_xmit_priv(padapter);
301
302         if (pxmitpriv->pxmit_frame_buf == NULL)
303                 return;
304
305         for (i = 0; i < NR_XMITFRAME; i++) {
306                 rtw_os_xmit_complete(padapter, pxmitframe);
307
308                 pxmitframe++;
309         }
310
311         for (i = 0; i < NR_XMITBUFF; i++) {
312                 rtw_os_xmit_resource_free(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ), true);
313
314                 pxmitbuf++;
315         }
316
317         if (pxmitpriv->pallocated_frame_buf)
318                 vfree(pxmitpriv->pallocated_frame_buf);
319
320
321         if (pxmitpriv->pallocated_xmitbuf)
322                 vfree(pxmitpriv->pallocated_xmitbuf);
323
324         /* free xframe_ext queue,  the same count as extbuf  */
325         pxmitframe = (struct xmit_frame *)pxmitpriv->xframe_ext;
326         if (pxmitframe) {
327                 for (i = 0; i < NR_XMIT_EXTBUFF; i++) {
328                         rtw_os_xmit_complete(padapter, pxmitframe);
329                         pxmitframe++;
330                 }
331         }
332         if (pxmitpriv->xframe_ext_alloc_addr)
333                 vfree(pxmitpriv->xframe_ext_alloc_addr);
334
335         /*  free xmit extension buff */
336         pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmit_extbuf;
337         for (i = 0; i < NR_XMIT_EXTBUFF; i++) {
338                 rtw_os_xmit_resource_free(padapter, pxmitbuf, (MAX_XMIT_EXTBUF_SZ + XMITBUF_ALIGN_SZ), true);
339
340                 pxmitbuf++;
341         }
342
343         if (pxmitpriv->pallocated_xmit_extbuf) {
344                 vfree(pxmitpriv->pallocated_xmit_extbuf);
345         }
346
347         for (i = 0; i < CMDBUF_MAX; i++) {
348                 pxmitbuf = &pxmitpriv->pcmd_xmitbuf[i];
349                 if (pxmitbuf != NULL)
350                         rtw_os_xmit_resource_free(padapter, pxmitbuf, MAX_CMDBUF_SZ+XMITBUF_ALIGN_SZ, true);
351         }
352
353         rtw_free_hwxmits(padapter);
354
355         mutex_destroy(&pxmitpriv->ack_tx_mutex);
356 }
357
358 u8 query_ra_short_GI(struct sta_info *psta)
359 {
360         u8 sgi = false, sgi_20m = false, sgi_40m = false, sgi_80m = false;
361
362         sgi_20m = psta->htpriv.sgi_20m;
363         sgi_40m = psta->htpriv.sgi_40m;
364
365         switch (psta->bw_mode) {
366         case CHANNEL_WIDTH_80:
367                 sgi = sgi_80m;
368                 break;
369         case CHANNEL_WIDTH_40:
370                 sgi = sgi_40m;
371                 break;
372         case CHANNEL_WIDTH_20:
373         default:
374                 sgi = sgi_20m;
375                 break;
376         }
377
378         return sgi;
379 }
380
381 static void update_attrib_vcs_info(struct adapter *padapter, struct xmit_frame *pxmitframe)
382 {
383         u32 sz;
384         struct pkt_attrib       *pattrib = &pxmitframe->attrib;
385         /* struct sta_info *psta = pattrib->psta; */
386         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
387         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
388
389         if (pattrib->nr_frags != 1)
390                 sz = padapter->xmitpriv.frag_len;
391         else /* no frag */
392                 sz = pattrib->last_txcmdsz;
393
394         /*  (1) RTS_Threshold is compared to the MPDU, not MSDU. */
395         /*  (2) If there are more than one frag in  this MSDU, only the first frag uses protection frame. */
396         /*              Other fragments are protected by previous fragment. */
397         /*              So we only need to check the length of first fragment. */
398         if (pmlmeext->cur_wireless_mode < WIRELESS_11_24N  || padapter->registrypriv.wifi_spec) {
399                 if (sz > padapter->registrypriv.rts_thresh)
400                         pattrib->vcs_mode = RTS_CTS;
401                 else{
402                         if (pattrib->rtsen)
403                                 pattrib->vcs_mode = RTS_CTS;
404                         else if (pattrib->cts2self)
405                                 pattrib->vcs_mode = CTS_TO_SELF;
406                         else
407                                 pattrib->vcs_mode = NONE_VCS;
408                 }
409         } else{
410                 while (true) {
411                         /* IOT action */
412                         if ((pmlmeinfo->assoc_AP_vendor == HT_IOT_PEER_ATHEROS) && (pattrib->ampdu_en == true) &&
413                                 (padapter->securitypriv.dot11PrivacyAlgrthm == _AES_)) {
414                                 pattrib->vcs_mode = CTS_TO_SELF;
415                                 break;
416                         }
417
418
419                         /* check ERP protection */
420                         if (pattrib->rtsen || pattrib->cts2self) {
421                                 if (pattrib->rtsen)
422                                         pattrib->vcs_mode = RTS_CTS;
423                                 else if (pattrib->cts2self)
424                                         pattrib->vcs_mode = CTS_TO_SELF;
425
426                                 break;
427                         }
428
429                         /* check HT op mode */
430                         if (pattrib->ht_en) {
431                                 u8 HTOpMode = pmlmeinfo->HT_protection;
432                                 if ((pmlmeext->cur_bwmode && (HTOpMode == 2 || HTOpMode == 3)) ||
433                                         (!pmlmeext->cur_bwmode && HTOpMode == 3)) {
434                                         pattrib->vcs_mode = RTS_CTS;
435                                         break;
436                                 }
437                         }
438
439                         /* check rts */
440                         if (sz > padapter->registrypriv.rts_thresh) {
441                                 pattrib->vcs_mode = RTS_CTS;
442                                 break;
443                         }
444
445                         /* to do list: check MIMO power save condition. */
446
447                         /* check AMPDU aggregation for TXOP */
448                         if (pattrib->ampdu_en == true) {
449                                 pattrib->vcs_mode = RTS_CTS;
450                                 break;
451                         }
452
453                         pattrib->vcs_mode = NONE_VCS;
454                         break;
455                 }
456         }
457
458         /* for debug : force driver control vrtl_carrier_sense. */
459         if (padapter->driver_vcs_en == 1)
460                 pattrib->vcs_mode = padapter->driver_vcs_type;
461 }
462
463 static void update_attrib_phy_info(struct adapter *padapter, struct pkt_attrib *pattrib, struct sta_info *psta)
464 {
465         struct mlme_ext_priv *mlmeext = &padapter->mlmeextpriv;
466
467         pattrib->rtsen = psta->rtsen;
468         pattrib->cts2self = psta->cts2self;
469
470         pattrib->mdata = 0;
471         pattrib->eosp = 0;
472         pattrib->triggered = 0;
473         pattrib->ampdu_spacing = 0;
474
475         /* qos_en, ht_en, init rate, , bw, ch_offset, sgi */
476         pattrib->qos_en = psta->qos_option;
477
478         pattrib->raid = psta->raid;
479
480         if (mlmeext->cur_bwmode < psta->bw_mode)
481                 pattrib->bwmode = mlmeext->cur_bwmode;
482         else
483                 pattrib->bwmode = psta->bw_mode;
484
485         pattrib->sgi = query_ra_short_GI(psta);
486
487         pattrib->ldpc = psta->ldpc;
488         pattrib->stbc = psta->stbc;
489
490         pattrib->ht_en = psta->htpriv.ht_option;
491         pattrib->ch_offset = psta->htpriv.ch_offset;
492         pattrib->ampdu_en = false;
493
494         if (padapter->driver_ampdu_spacing != 0xFF) /* driver control AMPDU Density for peer sta's rx */
495                 pattrib->ampdu_spacing = padapter->driver_ampdu_spacing;
496         else
497                 pattrib->ampdu_spacing = psta->htpriv.rx_ampdu_min_spacing;
498
499         /* if (pattrib->ht_en && psta->htpriv.ampdu_enable) */
500         /*  */
501         /*      if (psta->htpriv.agg_enable_bitmap & BIT(pattrib->priority)) */
502         /*              pattrib->ampdu_en = true; */
503         /*  */
504
505
506         pattrib->retry_ctrl = false;
507
508 #ifdef CONFIG_AUTO_AP_MODE
509         if (psta->isrc && psta->pid > 0)
510                 pattrib->pctrl = true;
511 #endif
512
513 }
514
515 static s32 update_attrib_sec_info(struct adapter *padapter, struct pkt_attrib *pattrib, struct sta_info *psta)
516 {
517         sint res = _SUCCESS;
518         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
519         struct security_priv *psecuritypriv = &padapter->securitypriv;
520         sint bmcast = IS_MCAST(pattrib->ra);
521
522         memset(pattrib->dot118021x_UncstKey.skey,  0, 16);
523         memset(pattrib->dot11tkiptxmickey.skey,  0, 16);
524         pattrib->mac_id = psta->mac_id;
525
526         if (psta->ieee8021x_blocked == true) {
527                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("\n psta->ieee8021x_blocked == true\n"));
528
529                 pattrib->encrypt = 0;
530
531                 if ((pattrib->ether_type != 0x888e) && (check_fwstate(pmlmepriv, WIFI_MP_STATE) == false)) {
532                         RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("\npsta->ieee8021x_blocked == true,  pattrib->ether_type(%.4x) != 0x888e\n", pattrib->ether_type));
533                         #ifdef DBG_TX_DROP_FRAME
534                         DBG_871X("DBG_TX_DROP_FRAME %s psta->ieee8021x_blocked == true,  pattrib->ether_type(%04x) != 0x888e\n", __func__, pattrib->ether_type);
535                         #endif
536                         res = _FAIL;
537                         goto exit;
538                 }
539         } else{
540                 GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt, bmcast);
541
542                 switch (psecuritypriv->dot11AuthAlgrthm) {
543                 case dot11AuthAlgrthm_Open:
544                 case dot11AuthAlgrthm_Shared:
545                 case dot11AuthAlgrthm_Auto:
546                         pattrib->key_idx = (u8)psecuritypriv->dot11PrivacyKeyIndex;
547                         break;
548                 case dot11AuthAlgrthm_8021X:
549                         if (bmcast)
550                                 pattrib->key_idx = (u8)psecuritypriv->dot118021XGrpKeyid;
551                         else
552                                 pattrib->key_idx = 0;
553                         break;
554                 default:
555                         pattrib->key_idx = 0;
556                         break;
557                 }
558
559                 /* For WPS 1.0 WEP, driver should not encrypt EAPOL Packet for WPS handshake. */
560                 if (((pattrib->encrypt == _WEP40_) || (pattrib->encrypt == _WEP104_)) && (pattrib->ether_type == 0x888e))
561                         pattrib->encrypt = _NO_PRIVACY_;
562
563         }
564
565         switch (pattrib->encrypt) {
566         case _WEP40_:
567         case _WEP104_:
568                 pattrib->iv_len = 4;
569                 pattrib->icv_len = 4;
570                 WEP_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
571                 break;
572
573         case _TKIP_:
574                 pattrib->iv_len = 8;
575                 pattrib->icv_len = 4;
576
577                 if (psecuritypriv->busetkipkey == _FAIL) {
578                         #ifdef DBG_TX_DROP_FRAME
579                         DBG_871X("DBG_TX_DROP_FRAME %s psecuritypriv->busetkipkey(%d) == _FAIL drop packet\n", __func__, psecuritypriv->busetkipkey);
580                         #endif
581                         res = _FAIL;
582                         goto exit;
583                 }
584
585                 if (bmcast)
586                         TKIP_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
587                 else
588                         TKIP_IV(pattrib->iv, psta->dot11txpn, 0);
589
590
591                 memcpy(pattrib->dot11tkiptxmickey.skey, psta->dot11tkiptxmickey.skey, 16);
592
593                 break;
594
595         case _AES_:
596
597                 pattrib->iv_len = 8;
598                 pattrib->icv_len = 8;
599
600                 if (bmcast)
601                         AES_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
602                 else
603                         AES_IV(pattrib->iv, psta->dot11txpn, 0);
604
605                 break;
606
607         default:
608                 pattrib->iv_len = 0;
609                 pattrib->icv_len = 0;
610                 break;
611         }
612
613         if (pattrib->encrypt > 0)
614                 memcpy(pattrib->dot118021x_UncstKey.skey, psta->dot118021x_UncstKey.skey, 16);
615
616         RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_,
617                 ("update_attrib: encrypt =%d  securitypriv.sw_encrypt =%d\n",
618                 pattrib->encrypt, padapter->securitypriv.sw_encrypt));
619
620         if (pattrib->encrypt &&
621                 ((padapter->securitypriv.sw_encrypt == true) || (psecuritypriv->hw_decrypted == false))) {
622                 pattrib->bswenc = true;
623                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_,
624                         ("update_attrib: encrypt =%d securitypriv.hw_decrypted =%d bswenc =true\n",
625                         pattrib->encrypt, padapter->securitypriv.sw_encrypt));
626         } else {
627                 pattrib->bswenc = false;
628                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("update_attrib: bswenc =false\n"));
629         }
630
631 exit:
632
633         return res;
634
635 }
636
637 u8 qos_acm(u8 acm_mask, u8 priority)
638 {
639         u8 change_priority = priority;
640
641         switch (priority) {
642         case 0:
643         case 3:
644                 if (acm_mask & BIT(1))
645                         change_priority = 1;
646                 break;
647         case 1:
648         case 2:
649                 break;
650         case 4:
651         case 5:
652                 if (acm_mask & BIT(2))
653                         change_priority = 0;
654                 break;
655         case 6:
656         case 7:
657                 if (acm_mask & BIT(3))
658                         change_priority = 5;
659                 break;
660         default:
661                 DBG_871X("qos_acm(): invalid pattrib->priority: %d!!!\n", priority);
662                 break;
663         }
664
665         return change_priority;
666 }
667
668 static void set_qos(struct pkt_file *ppktfile, struct pkt_attrib *pattrib)
669 {
670         struct ethhdr etherhdr;
671         struct iphdr ip_hdr;
672         s32 UserPriority = 0;
673
674
675         _rtw_open_pktfile(ppktfile->pkt, ppktfile);
676         _rtw_pktfile_read(ppktfile, (unsigned char *)&etherhdr, ETH_HLEN);
677
678         /*  get UserPriority from IP hdr */
679         if (pattrib->ether_type == 0x0800) {
680                 _rtw_pktfile_read(ppktfile, (u8 *)&ip_hdr, sizeof(ip_hdr));
681 /*              UserPriority = (ntohs(ip_hdr.tos) >> 5) & 0x3; */
682                 UserPriority = ip_hdr.tos >> 5;
683         }
684         pattrib->priority = UserPriority;
685         pattrib->hdrlen = WLAN_HDR_A3_QOS_LEN;
686         pattrib->subtype = WIFI_QOS_DATA_TYPE;
687 }
688
689 static s32 update_attrib(struct adapter *padapter, _pkt *pkt, struct pkt_attrib *pattrib)
690 {
691         uint i;
692         struct pkt_file pktfile;
693         struct sta_info *psta = NULL;
694         struct ethhdr etherhdr;
695
696         sint bmcast;
697         struct sta_priv         *pstapriv = &padapter->stapriv;
698         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
699         struct qos_priv         *pqospriv = &pmlmepriv->qospriv;
700         sint res = _SUCCESS;
701
702         DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib);
703
704         _rtw_open_pktfile(pkt, &pktfile);
705         i = _rtw_pktfile_read(&pktfile, (u8 *)&etherhdr, ETH_HLEN);
706
707         pattrib->ether_type = ntohs(etherhdr.h_proto);
708
709
710         memcpy(pattrib->dst, &etherhdr.h_dest, ETH_ALEN);
711         memcpy(pattrib->src, &etherhdr.h_source, ETH_ALEN);
712
713
714         if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true) ||
715                 (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true)) {
716                 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
717                 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
718                 DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_adhoc);
719         } else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
720                 memcpy(pattrib->ra, get_bssid(pmlmepriv), ETH_ALEN);
721                 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
722                 DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_sta);
723         } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
724                 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
725                 memcpy(pattrib->ta, get_bssid(pmlmepriv), ETH_ALEN);
726                 DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_ap);
727         } else
728                 DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_unknown);
729
730         pattrib->pktlen = pktfile.pkt_len;
731
732         if (ETH_P_IP == pattrib->ether_type) {
733                 /*  The following is for DHCP and ARP packet, we use cck1M to tx these packets and let LPS awake some time */
734                 /*  to prevent DHCP protocol fail */
735
736                 u8 tmp[24];
737
738                 _rtw_pktfile_read(&pktfile, &tmp[0], 24);
739
740                 pattrib->dhcp_pkt = 0;
741                 if (pktfile.pkt_len > 282) {/* MINIMUM_DHCP_PACKET_SIZE) { */
742                         if (ETH_P_IP == pattrib->ether_type) {/*  IP header */
743                                 if (((tmp[21] == 68) && (tmp[23] == 67)) ||
744                                         ((tmp[21] == 67) && (tmp[23] == 68))) {
745                                         /*  68 : UDP BOOTP client */
746                                         /*  67 : UDP BOOTP server */
747                                         RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("======================update_attrib: get DHCP Packet\n"));
748                                         pattrib->dhcp_pkt = 1;
749                                         DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_dhcp);
750                                 }
751                         }
752                 }
753
754                 /* for parsing ICMP pakcets */
755                 {
756                         struct iphdr *piphdr = (struct iphdr *)tmp;
757
758                         pattrib->icmp_pkt = 0;
759                         if (piphdr->protocol == 0x1) { /*  protocol type in ip header 0x1 is ICMP */
760                                 pattrib->icmp_pkt = 1;
761                                 DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_icmp);
762                         }
763                 }
764
765
766         } else if (0x888e == pattrib->ether_type) {
767                 DBG_871X_LEVEL(_drv_always_, "send eapol packet\n");
768         }
769
770         if ((pattrib->ether_type == 0x888e) || (pattrib->dhcp_pkt == 1))
771                 rtw_set_scan_deny(padapter, 3000);
772
773         /*  If EAPOL , ARP , OR DHCP packet, driver must be in active mode. */
774         if (pattrib->icmp_pkt == 1)
775                 rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_LEAVE, 1);
776         else if (pattrib->dhcp_pkt == 1) {
777                 DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_active);
778                 rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_SPECIAL_PACKET, 1);
779         }
780
781         bmcast = IS_MCAST(pattrib->ra);
782
783         /*  get sta_info */
784         if (bmcast) {
785                 psta = rtw_get_bcmc_stainfo(padapter);
786         } else {
787                 psta = rtw_get_stainfo(pstapriv, pattrib->ra);
788                 if (psta == NULL)       { /*  if we cannot get psta => drop the pkt */
789                         DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_err_ucast_sta);
790                         RT_TRACE(_module_rtl871x_xmit_c_, _drv_alert_, ("\nupdate_attrib => get sta_info fail, ra:" MAC_FMT"\n", MAC_ARG(pattrib->ra)));
791                         #ifdef DBG_TX_DROP_FRAME
792                         DBG_871X("DBG_TX_DROP_FRAME %s get sta_info fail, ra:" MAC_FMT"\n", __func__, MAC_ARG(pattrib->ra));
793                         #endif
794                         res = _FAIL;
795                         goto exit;
796                 } else if ((check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) && (!(psta->state & _FW_LINKED))) {
797                         DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_err_ucast_ap_link);
798                         res = _FAIL;
799                         goto exit;
800                 }
801         }
802
803         if (psta == NULL) {
804                 /*  if we cannot get psta => drop the pkt */
805                 DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_err_sta);
806                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_alert_, ("\nupdate_attrib => get sta_info fail, ra:" MAC_FMT "\n", MAC_ARG(pattrib->ra)));
807                 #ifdef DBG_TX_DROP_FRAME
808                 DBG_871X("DBG_TX_DROP_FRAME %s get sta_info fail, ra:" MAC_FMT"\n", __func__, MAC_ARG(pattrib->ra));
809                 #endif
810                 res = _FAIL;
811                 goto exit;
812         }
813
814         if (!(psta->state & _FW_LINKED)) {
815                 DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_err_link);
816                 DBG_871X("%s, psta("MAC_FMT")->state(0x%x) != _FW_LINKED\n", __func__, MAC_ARG(psta->hwaddr), psta->state);
817                 return _FAIL;
818         }
819
820
821
822         /* TODO:_lock */
823         if (update_attrib_sec_info(padapter, pattrib, psta) == _FAIL) {
824                 DBG_COUNTER(padapter->tx_logs.core_tx_upd_attrib_err_sec);
825                 res = _FAIL;
826                 goto exit;
827         }
828
829         update_attrib_phy_info(padapter, pattrib, psta);
830
831         /* DBG_8192C("%s ==> mac_id(%d)\n", __func__, pattrib->mac_id); */
832
833         pattrib->psta = psta;
834         /* TODO:_unlock */
835
836         pattrib->pctrl = 0;
837
838         pattrib->ack_policy = 0;
839         /*  get ether_hdr_len */
840         pattrib->pkt_hdrlen = ETH_HLEN;/* pattrib->ether_type == 0x8100) ? (14 + 4): 14; vlan tag */
841
842         pattrib->hdrlen = WLAN_HDR_A3_LEN;
843         pattrib->subtype = WIFI_DATA_TYPE;
844         pattrib->priority = 0;
845
846         if (check_fwstate(pmlmepriv, WIFI_AP_STATE|WIFI_ADHOC_STATE|WIFI_ADHOC_MASTER_STATE)) {
847                 if (pattrib->qos_en)
848                         set_qos(&pktfile, pattrib);
849         } else{
850                 if (pqospriv->qos_option) {
851                         set_qos(&pktfile, pattrib);
852
853                         if (pmlmepriv->acm_mask != 0)
854                                 pattrib->priority = qos_acm(pmlmepriv->acm_mask, pattrib->priority);
855
856                 }
857         }
858
859         /* pattrib->priority = 5; force to used VI queue, for testing */
860
861         rtw_set_tx_chksum_offload(pkt, pattrib);
862
863 exit:
864         return res;
865 }
866
867 static s32 xmitframe_addmic(struct adapter *padapter, struct xmit_frame *pxmitframe)
868 {
869         sint                    curfragnum, length;
870         u8 *pframe, *payload, mic[8];
871         struct  mic_data                micdata;
872         /* struct       sta_info        *stainfo; */
873         struct  pkt_attrib       *pattrib = &pxmitframe->attrib;
874         struct  security_priv *psecuritypriv = &padapter->securitypriv;
875         struct  xmit_priv       *pxmitpriv = &padapter->xmitpriv;
876         u8 priority[4] = {0x0, 0x0, 0x0, 0x0};
877         u8 hw_hdr_offset = 0;
878         sint bmcst = IS_MCAST(pattrib->ra);
879
880 /*
881         if (pattrib->psta)
882         {
883                 stainfo = pattrib->psta;
884         }
885         else
886         {
887                 DBG_871X("%s, call rtw_get_stainfo()\n", __func__);
888                 stainfo =rtw_get_stainfo(&padapter->stapriv ,&pattrib->ra[0]);
889         }
890
891         if (stainfo == NULL)
892         {
893                 DBG_871X("%s, psta ==NUL\n", __func__);
894                 return _FAIL;
895         }
896
897         if (!(stainfo->state &_FW_LINKED))
898         {
899                 DBG_871X("%s, psta->state(0x%x) != _FW_LINKED\n", __func__, stainfo->state);
900                 return _FAIL;
901         }
902 */
903
904         hw_hdr_offset = TXDESC_OFFSET;
905
906         if (pattrib->encrypt == _TKIP_) { /* if (psecuritypriv->dot11PrivacyAlgrthm == _TKIP_PRIVACY_) */
907                 /* encode mic code */
908                 /* if (stainfo!= NULL) */
909                 {
910                         u8 null_key[16] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
911
912                         pframe = pxmitframe->buf_addr + hw_hdr_offset;
913
914                         if (bmcst) {
915                                 if (!memcmp(psecuritypriv->dot118021XGrptxmickey[psecuritypriv->dot118021XGrpKeyid].skey, null_key, 16)) {
916                                         /* DbgPrint("\nxmitframe_addmic:stainfo->dot11tkiptxmickey == 0\n"); */
917                                         /* msleep(10); */
918                                         return _FAIL;
919                                 }
920                                 /* start to calculate the mic code */
921                                 rtw_secmicsetkey(&micdata, psecuritypriv->dot118021XGrptxmickey[psecuritypriv->dot118021XGrpKeyid].skey);
922                         } else {
923                                 if (!memcmp(&pattrib->dot11tkiptxmickey.skey[0], null_key, 16)) {
924                                         /* DbgPrint("\nxmitframe_addmic:stainfo->dot11tkiptxmickey == 0\n"); */
925                                         /* msleep(10); */
926                                         return _FAIL;
927                                 }
928                                 /* start to calculate the mic code */
929                                 rtw_secmicsetkey(&micdata, &pattrib->dot11tkiptxmickey.skey[0]);
930                         }
931
932                         if (pframe[1]&1) {   /* ToDS == 1 */
933                                 rtw_secmicappend(&micdata, &pframe[16], 6);  /* DA */
934                                 if (pframe[1]&2)  /* From Ds == 1 */
935                                         rtw_secmicappend(&micdata, &pframe[24], 6);
936                                 else
937                                 rtw_secmicappend(&micdata, &pframe[10], 6);
938                         } else {        /* ToDS == 0 */
939                                 rtw_secmicappend(&micdata, &pframe[4], 6);   /* DA */
940                                 if (pframe[1]&2)  /* From Ds == 1 */
941                                         rtw_secmicappend(&micdata, &pframe[16], 6);
942                                 else
943                                         rtw_secmicappend(&micdata, &pframe[10], 6);
944
945                         }
946
947                         /* if (pqospriv->qos_option == 1) */
948                         if (pattrib->qos_en)
949                                 priority[0] = (u8)pxmitframe->attrib.priority;
950
951
952                         rtw_secmicappend(&micdata, &priority[0], 4);
953
954                         payload = pframe;
955
956                         for (curfragnum = 0; curfragnum < pattrib->nr_frags; curfragnum++) {
957                                 payload = (u8 *)RND4((SIZE_PTR)(payload));
958                                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("===curfragnum =%d, pframe = 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x,!!!\n",
959                                         curfragnum, *payload, *(payload+1), *(payload+2), *(payload+3), *(payload+4), *(payload+5), *(payload+6), *(payload+7)));
960
961                                 payload = payload+pattrib->hdrlen+pattrib->iv_len;
962                                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("curfragnum =%d pattrib->hdrlen =%d pattrib->iv_len =%d", curfragnum, pattrib->hdrlen, pattrib->iv_len));
963                                 if ((curfragnum+1) == pattrib->nr_frags) {
964                                         length = pattrib->last_txcmdsz-pattrib->hdrlen-pattrib->iv_len-((pattrib->bswenc) ? pattrib->icv_len : 0);
965                                         rtw_secmicappend(&micdata, payload, length);
966                                         payload = payload+length;
967                                 } else{
968                                         length = pxmitpriv->frag_len-pattrib->hdrlen-pattrib->iv_len-((pattrib->bswenc) ? pattrib->icv_len : 0);
969                                         rtw_secmicappend(&micdata, payload, length);
970                                         payload = payload+length+pattrib->icv_len;
971                                         RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("curfragnum =%d length =%d pattrib->icv_len =%d", curfragnum, length, pattrib->icv_len));
972                                 }
973                         }
974                         rtw_secgetmic(&micdata, &(mic[0]));
975                         RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("xmitframe_addmic: before add mic code!!!\n"));
976                         RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("xmitframe_addmic: pattrib->last_txcmdsz =%d!!!\n", pattrib->last_txcmdsz));
977                         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\
978   mic[4]= 0x%.2x , mic[5]= 0x%.2x , mic[6]= 0x%.2x , mic[7]= 0x%.2x !!!!\n",
979                                 mic[0], mic[1], mic[2], mic[3], mic[4], mic[5], mic[6], mic[7]));
980                         /* add mic code  and add the mic code length in last_txcmdsz */
981
982                         memcpy(payload, &(mic[0]), 8);
983                         pattrib->last_txcmdsz += 8;
984
985                         RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("\n ========last pkt ========\n"));
986                         payload = payload-pattrib->last_txcmdsz+8;
987                         for (curfragnum = 0; curfragnum < pattrib->last_txcmdsz; curfragnum = curfragnum+8)
988                                         RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, (" %.2x,  %.2x,  %.2x,  %.2x,  %.2x,  %.2x,  %.2x,  %.2x ",
989                                         *(payload+curfragnum), *(payload+curfragnum+1), *(payload+curfragnum+2), *(payload+curfragnum+3),
990                                         *(payload+curfragnum+4), *(payload+curfragnum+5), *(payload+curfragnum+6), *(payload+curfragnum+7)));
991                         }
992 /*
993                         else {
994                                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("xmitframe_addmic: rtw_get_stainfo == NULL!!!\n"));
995                         }
996 */
997         }
998         return _SUCCESS;
999 }
1000
1001 static s32 xmitframe_swencrypt(struct adapter *padapter, struct xmit_frame *pxmitframe)
1002 {
1003
1004         struct  pkt_attrib       *pattrib = &pxmitframe->attrib;
1005         /* struct       security_priv *psecuritypriv =&padapter->securitypriv; */
1006
1007         /* if ((psecuritypriv->sw_encrypt)||(pattrib->bswenc)) */
1008         if (pattrib->bswenc) {
1009                 /* DBG_871X("start xmitframe_swencrypt\n"); */
1010                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_alert_, ("### xmitframe_swencrypt\n"));
1011                 switch (pattrib->encrypt) {
1012                 case _WEP40_:
1013                 case _WEP104_:
1014                         rtw_wep_encrypt(padapter, (u8 *)pxmitframe);
1015                         break;
1016                 case _TKIP_:
1017                         rtw_tkip_encrypt(padapter, (u8 *)pxmitframe);
1018                         break;
1019                 case _AES_:
1020                         rtw_aes_encrypt(padapter, (u8 *)pxmitframe);
1021                         break;
1022                 default:
1023                                 break;
1024                 }
1025
1026         } else
1027                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_notice_, ("### xmitframe_hwencrypt\n"));
1028
1029         return _SUCCESS;
1030 }
1031
1032 s32 rtw_make_wlanhdr(struct adapter *padapter, u8 *hdr, struct pkt_attrib *pattrib)
1033 {
1034         u16 *qc;
1035
1036         struct ieee80211_hdr *pwlanhdr = (struct ieee80211_hdr *)hdr;
1037         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1038         struct qos_priv *pqospriv = &pmlmepriv->qospriv;
1039         u8 qos_option = false;
1040         sint res = _SUCCESS;
1041         __le16 *fctrl = &pwlanhdr->frame_control;
1042
1043         memset(hdr, 0, WLANHDR_OFFSET);
1044
1045         SetFrameSubType(fctrl, pattrib->subtype);
1046
1047         if (pattrib->subtype & WIFI_DATA_TYPE) {
1048                 if ((check_fwstate(pmlmepriv,  WIFI_STATION_STATE) == true)) {
1049                         /* to_ds = 1, fr_ds = 0; */
1050
1051                         {
1052                                 /*  1.Data transfer to AP */
1053                                 /*  2.Arp pkt will relayed by AP */
1054                                 SetToDs(fctrl);
1055                                 memcpy(pwlanhdr->addr1, get_bssid(pmlmepriv), ETH_ALEN);
1056                                 memcpy(pwlanhdr->addr2, pattrib->src, ETH_ALEN);
1057                                 memcpy(pwlanhdr->addr3, pattrib->dst, ETH_ALEN);
1058                         }
1059
1060                         if (pqospriv->qos_option)
1061                                 qos_option = true;
1062
1063                 } else if ((check_fwstate(pmlmepriv,  WIFI_AP_STATE) == true)) {
1064                         /* to_ds = 0, fr_ds = 1; */
1065                         SetFrDs(fctrl);
1066                         memcpy(pwlanhdr->addr1, pattrib->dst, ETH_ALEN);
1067                         memcpy(pwlanhdr->addr2, get_bssid(pmlmepriv), ETH_ALEN);
1068                         memcpy(pwlanhdr->addr3, pattrib->src, ETH_ALEN);
1069
1070                         if (pattrib->qos_en)
1071                                 qos_option = true;
1072                 } else if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true) ||
1073                 (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true)) {
1074                         memcpy(pwlanhdr->addr1, pattrib->dst, ETH_ALEN);
1075                         memcpy(pwlanhdr->addr2, pattrib->src, ETH_ALEN);
1076                         memcpy(pwlanhdr->addr3, get_bssid(pmlmepriv), ETH_ALEN);
1077
1078                         if (pattrib->qos_en)
1079                                 qos_option = true;
1080                 } else {
1081                         RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("fw_state:%x is not allowed to xmit frame\n", get_fwstate(pmlmepriv)));
1082                         res = _FAIL;
1083                         goto exit;
1084                 }
1085
1086                 if (pattrib->mdata)
1087                         SetMData(fctrl);
1088
1089                 if (pattrib->encrypt)
1090                         SetPrivacy(fctrl);
1091
1092                 if (qos_option) {
1093                         qc = (unsigned short *)(hdr + pattrib->hdrlen - 2);
1094
1095                         if (pattrib->priority)
1096                                 SetPriority(qc, pattrib->priority);
1097
1098                         SetEOSP(qc, pattrib->eosp);
1099
1100                         SetAckpolicy(qc, pattrib->ack_policy);
1101                 }
1102
1103                 /* TODO: fill HT Control Field */
1104
1105                 /* Update Seq Num will be handled by f/w */
1106                 {
1107                         struct sta_info *psta;
1108                         psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
1109                         if (pattrib->psta != psta) {
1110                                 DBG_871X("%s, pattrib->psta(%p) != psta(%p)\n", __func__, pattrib->psta, psta);
1111                                 return _FAIL;
1112                         }
1113
1114                         if (psta == NULL) {
1115                                 DBG_871X("%s, psta ==NUL\n", __func__);
1116                                 return _FAIL;
1117                         }
1118
1119                         if (!(psta->state & _FW_LINKED)) {
1120                                 DBG_871X("%s, psta->state(0x%x) != _FW_LINKED\n", __func__, psta->state);
1121                                 return _FAIL;
1122                         }
1123
1124
1125                         if (psta) {
1126                                 psta->sta_xmitpriv.txseq_tid[pattrib->priority]++;
1127                                 psta->sta_xmitpriv.txseq_tid[pattrib->priority] &= 0xFFF;
1128                                 pattrib->seqnum = psta->sta_xmitpriv.txseq_tid[pattrib->priority];
1129
1130                                 SetSeqNum(hdr, pattrib->seqnum);
1131
1132                                 /* check if enable ampdu */
1133                                 if (pattrib->ht_en && psta->htpriv.ampdu_enable)
1134                                         if (psta->htpriv.agg_enable_bitmap & BIT(pattrib->priority))
1135                                                 pattrib->ampdu_en = true;
1136
1137
1138                                 /* re-check if enable ampdu by BA_starting_seqctrl */
1139                                 if (pattrib->ampdu_en == true) {
1140                                         u16 tx_seq;
1141
1142                                         tx_seq = psta->BA_starting_seqctrl[pattrib->priority & 0x0f];
1143
1144                                         /* check BA_starting_seqctrl */
1145                                         if (SN_LESS(pattrib->seqnum, tx_seq)) {
1146                                                 /* DBG_871X("tx ampdu seqnum(%d) < tx_seq(%d)\n", pattrib->seqnum, tx_seq); */
1147                                                 pattrib->ampdu_en = false;/* AGG BK */
1148                                         } else if (SN_EQUAL(pattrib->seqnum, tx_seq)) {
1149                                                 psta->BA_starting_seqctrl[pattrib->priority & 0x0f] = (tx_seq+1)&0xfff;
1150
1151                                                 pattrib->ampdu_en = true;/* AGG EN */
1152                                         } else{
1153                                                 /* DBG_871X("tx ampdu over run\n"); */
1154                                                 psta->BA_starting_seqctrl[pattrib->priority & 0x0f] = (pattrib->seqnum+1)&0xfff;
1155                                                 pattrib->ampdu_en = true;/* AGG EN */
1156                                         }
1157
1158                                 }
1159                         }
1160                 }
1161
1162         } else{
1163
1164         }
1165
1166 exit:
1167         return res;
1168 }
1169
1170 s32 rtw_txframes_pending(struct adapter *padapter)
1171 {
1172         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1173
1174         return ((!list_empty(&pxmitpriv->be_pending.queue)) ||
1175                          (!list_empty(&pxmitpriv->bk_pending.queue)) ||
1176                          (!list_empty(&pxmitpriv->vi_pending.queue)) ||
1177                          (!list_empty(&pxmitpriv->vo_pending.queue)));
1178 }
1179
1180 /*
1181  * Calculate wlan 802.11 packet MAX size from pkt_attrib
1182  * This function doesn't consider fragment case
1183  */
1184 u32 rtw_calculate_wlan_pkt_size_by_attribue(struct pkt_attrib *pattrib)
1185 {
1186         u32 len = 0;
1187
1188         len = pattrib->hdrlen + pattrib->iv_len; /*  WLAN Header and IV */
1189         len += SNAP_SIZE + sizeof(u16); /*  LLC */
1190         len += pattrib->pktlen;
1191         if (pattrib->encrypt == _TKIP_)
1192                 len += 8; /*  MIC */
1193         len += ((pattrib->bswenc) ? pattrib->icv_len : 0); /*  ICV */
1194
1195         return len;
1196 }
1197
1198 /*
1199
1200 This sub-routine will perform all the following:
1201
1202 1. remove 802.3 header.
1203 2. create wlan_header, based on the info in pxmitframe
1204 3. append sta's iv/ext-iv
1205 4. append LLC
1206 5. move frag chunk from pframe to pxmitframe->mem
1207 6. apply sw-encrypt, if necessary.
1208
1209 */
1210 s32 rtw_xmitframe_coalesce(struct adapter *padapter, _pkt *pkt, struct xmit_frame *pxmitframe)
1211 {
1212         struct pkt_file pktfile;
1213
1214         s32 frg_inx, frg_len, mpdu_len, llc_sz, mem_sz;
1215
1216         SIZE_PTR addr;
1217
1218         u8 *pframe, *mem_start;
1219         u8 hw_hdr_offset;
1220
1221         /* struct sta_info      *psta; */
1222         /* struct sta_priv      *pstapriv = &padapter->stapriv; */
1223         /* struct mlme_priv *pmlmepriv = &padapter->mlmepriv; */
1224         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1225
1226         struct pkt_attrib       *pattrib = &pxmitframe->attrib;
1227
1228         u8 *pbuf_start;
1229
1230         s32 bmcst = IS_MCAST(pattrib->ra);
1231         s32 res = _SUCCESS;
1232
1233 /*
1234         if (pattrib->psta)
1235         {
1236                 psta = pattrib->psta;
1237         } else
1238         {
1239                 DBG_871X("%s, call rtw_get_stainfo()\n", __func__);
1240                 psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
1241         }
1242
1243         if (psta == NULL)
1244   {
1245
1246                 DBG_871X("%s, psta ==NUL\n", __func__);
1247                 return _FAIL;
1248         }
1249
1250
1251         if (!(psta->state &_FW_LINKED))
1252         {
1253                 DBG_871X("%s, psta->state(0x%x) != _FW_LINKED\n", __func__, psta->state);
1254                 return _FAIL;
1255         }
1256 */
1257         if (pxmitframe->buf_addr == NULL) {
1258                 DBG_8192C("==> %s buf_addr == NULL\n", __func__);
1259                 return _FAIL;
1260         }
1261
1262         pbuf_start = pxmitframe->buf_addr;
1263
1264         hw_hdr_offset = TXDESC_OFFSET;
1265         mem_start = pbuf_start +        hw_hdr_offset;
1266
1267         if (rtw_make_wlanhdr(padapter, mem_start, pattrib) == _FAIL) {
1268                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("rtw_xmitframe_coalesce: rtw_make_wlanhdr fail; drop pkt\n"));
1269                 DBG_8192C("rtw_xmitframe_coalesce: rtw_make_wlanhdr fail; drop pkt\n");
1270                 res = _FAIL;
1271                 goto exit;
1272         }
1273
1274         _rtw_open_pktfile(pkt, &pktfile);
1275         _rtw_pktfile_read(&pktfile, NULL, pattrib->pkt_hdrlen);
1276
1277         frg_inx = 0;
1278         frg_len = pxmitpriv->frag_len - 4;/* 2346-4 = 2342 */
1279
1280         while (1) {
1281                 llc_sz = 0;
1282
1283                 mpdu_len = frg_len;
1284
1285                 pframe = mem_start;
1286
1287                 SetMFrag(mem_start);
1288
1289                 pframe += pattrib->hdrlen;
1290                 mpdu_len -= pattrib->hdrlen;
1291
1292                 /* adding icv, if necessary... */
1293                 if (pattrib->iv_len) {
1294                         memcpy(pframe, pattrib->iv, pattrib->iv_len);
1295
1296                         RT_TRACE(_module_rtl871x_xmit_c_, _drv_notice_,
1297                                  ("rtw_xmitframe_coalesce: keyid =%d pattrib->iv[3]=%.2x pframe =%.2x %.2x %.2x %.2x\n",
1298                                   padapter->securitypriv.dot11PrivacyKeyIndex, pattrib->iv[3], *pframe, *(pframe+1), *(pframe+2), *(pframe+3)));
1299
1300                         pframe += pattrib->iv_len;
1301
1302                         mpdu_len -= pattrib->iv_len;
1303                 }
1304
1305                 if (frg_inx == 0) {
1306                         llc_sz = rtw_put_snap(pframe, pattrib->ether_type);
1307                         pframe += llc_sz;
1308                         mpdu_len -= llc_sz;
1309                 }
1310
1311                 if ((pattrib->icv_len > 0) && (pattrib->bswenc)) {
1312                         mpdu_len -= pattrib->icv_len;
1313                 }
1314
1315
1316                 if (bmcst) {
1317                         /*  don't do fragment to broadcat/multicast packets */
1318                         mem_sz = _rtw_pktfile_read(&pktfile, pframe, pattrib->pktlen);
1319                 } else {
1320                         mem_sz = _rtw_pktfile_read(&pktfile, pframe, mpdu_len);
1321                 }
1322
1323                 pframe += mem_sz;
1324
1325                 if ((pattrib->icv_len > 0) && (pattrib->bswenc)) {
1326                         memcpy(pframe, pattrib->icv, pattrib->icv_len);
1327                         pframe += pattrib->icv_len;
1328                 }
1329
1330                 frg_inx++;
1331
1332                 if (bmcst || (rtw_endofpktfile(&pktfile) == true)) {
1333                         pattrib->nr_frags = frg_inx;
1334
1335                         pattrib->last_txcmdsz = pattrib->hdrlen + pattrib->iv_len + ((pattrib->nr_frags == 1) ? llc_sz:0) +
1336                                         ((pattrib->bswenc) ? pattrib->icv_len : 0) + mem_sz;
1337
1338                         ClearMFrag(mem_start);
1339
1340                         break;
1341                 } else
1342                         RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("%s: There're still something in packet!\n", __func__));
1343
1344                 addr = (SIZE_PTR)(pframe);
1345
1346                 mem_start = (unsigned char *)RND4(addr) + hw_hdr_offset;
1347                 memcpy(mem_start, pbuf_start + hw_hdr_offset, pattrib->hdrlen);
1348
1349         }
1350
1351         if (xmitframe_addmic(padapter, pxmitframe) == _FAIL) {
1352                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("xmitframe_addmic(padapter, pxmitframe) == _FAIL\n"));
1353                 DBG_8192C("xmitframe_addmic(padapter, pxmitframe) == _FAIL\n");
1354                 res = _FAIL;
1355                 goto exit;
1356         }
1357
1358         xmitframe_swencrypt(padapter, pxmitframe);
1359
1360         if (bmcst == false)
1361                 update_attrib_vcs_info(padapter, pxmitframe);
1362         else
1363                 pattrib->vcs_mode = NONE_VCS;
1364
1365 exit:
1366         return res;
1367 }
1368
1369 /* broadcast or multicast management pkt use BIP, unicast management pkt use CCMP encryption */
1370 s32 rtw_mgmt_xmitframe_coalesce(struct adapter *padapter, _pkt *pkt, struct xmit_frame *pxmitframe)
1371 {
1372         u8 *pframe, *mem_start = NULL, *tmp_buf = NULL;
1373         u8 subtype;
1374         struct sta_info         *psta = NULL;
1375         struct pkt_attrib       *pattrib = &pxmitframe->attrib;
1376         s32 bmcst = IS_MCAST(pattrib->ra);
1377         u8 *BIP_AAD = NULL;
1378         u8 *MGMT_body = NULL;
1379
1380         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1381         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1382         struct ieee80211_hdr    *pwlanhdr;
1383         u8 MME[_MME_IE_LENGTH_];
1384         u32 ori_len;
1385         mem_start = pframe = (u8 *)(pxmitframe->buf_addr) + TXDESC_OFFSET;
1386         pwlanhdr = (struct ieee80211_hdr *)pframe;
1387
1388         ori_len = BIP_AAD_SIZE+pattrib->pktlen;
1389         tmp_buf = BIP_AAD = rtw_zmalloc(ori_len);
1390         subtype = GetFrameSubType(pframe); /* bit(7)~bit(2) */
1391
1392         if (BIP_AAD == NULL)
1393                 return _FAIL;
1394
1395         spin_lock_bh(&padapter->security_key_mutex);
1396
1397         /* only support station mode */
1398         if (!check_fwstate(pmlmepriv, WIFI_STATION_STATE) || !check_fwstate(pmlmepriv, _FW_LINKED))
1399                 goto xmitframe_coalesce_success;
1400
1401         /* IGTK key is not install, it may not support 802.11w */
1402         if (padapter->securitypriv.binstallBIPkey != true) {
1403                 DBG_871X("no instll BIP key\n");
1404                 goto xmitframe_coalesce_success;
1405         }
1406         /* station mode doesn't need TX BIP, just ready the code */
1407         if (bmcst) {
1408                 int frame_body_len;
1409                 u8 mic[16];
1410
1411                 memset(MME, 0, 18);
1412
1413                 /* other types doesn't need the BIP */
1414                 if (GetFrameSubType(pframe) != WIFI_DEAUTH && GetFrameSubType(pframe) != WIFI_DISASSOC)
1415                         goto xmitframe_coalesce_fail;
1416
1417                 MGMT_body = pframe + sizeof(struct ieee80211_hdr_3addr);
1418                 pframe += pattrib->pktlen;
1419
1420                 /* octent 0 and 1 is key index , BIP keyid is 4 or 5, LSB only need octent 0 */
1421                 MME[0] = padapter->securitypriv.dot11wBIPKeyid;
1422                 /* copy packet number */
1423                 memcpy(&MME[2], &pmlmeext->mgnt_80211w_IPN, 6);
1424                 /* increase the packet number */
1425                 pmlmeext->mgnt_80211w_IPN++;
1426
1427                 /* add MME IE with MIC all zero, MME string doesn't include element id and length */
1428                 pframe = rtw_set_ie(pframe, _MME_IE_, 16, MME, &(pattrib->pktlen));
1429                 pattrib->last_txcmdsz = pattrib->pktlen;
1430                 /*  total frame length - header length */
1431                 frame_body_len = pattrib->pktlen - sizeof(struct ieee80211_hdr_3addr);
1432
1433                 /* conscruct AAD, copy frame control field */
1434                 memcpy(BIP_AAD, &pwlanhdr->frame_control, 2);
1435                 ClearRetry(BIP_AAD);
1436                 ClearPwrMgt(BIP_AAD);
1437                 ClearMData(BIP_AAD);
1438                 /* conscruct AAD, copy address 1 to address 3 */
1439                 memcpy(BIP_AAD+2, pwlanhdr->addr1, 18);
1440                 /* copy management fram body */
1441                 memcpy(BIP_AAD+BIP_AAD_SIZE, MGMT_body, frame_body_len);
1442                 /* calculate mic */
1443                 if (omac1_aes_128(padapter->securitypriv.dot11wBIPKey[padapter->securitypriv.dot11wBIPKeyid].skey
1444                         , BIP_AAD, BIP_AAD_SIZE+frame_body_len, mic))
1445                         goto xmitframe_coalesce_fail;
1446
1447                 /* copy right BIP mic value, total is 128bits, we use the 0~63 bits */
1448                 memcpy(pframe-8, mic, 8);
1449         } else { /* unicast mgmt frame TX */
1450                 /* start to encrypt mgmt frame */
1451                 if (subtype == WIFI_DEAUTH || subtype == WIFI_DISASSOC ||
1452                         subtype == WIFI_REASSOCREQ || subtype == WIFI_ACTION) {
1453                         if (pattrib->psta)
1454                                 psta = pattrib->psta;
1455                         else
1456                                 psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
1457
1458                         if (psta == NULL) {
1459
1460                                 DBG_871X("%s, psta ==NUL\n", __func__);
1461                                 goto xmitframe_coalesce_fail;
1462                         }
1463
1464                         if (!(psta->state & _FW_LINKED) || pxmitframe->buf_addr == NULL) {
1465                                 DBG_871X("%s, not _FW_LINKED or addr null\n", __func__);
1466                                 goto xmitframe_coalesce_fail;
1467                         }
1468
1469                         /* DBG_871X("%s, action frame category =%d\n", __func__, pframe[WLAN_HDR_A3_LEN]); */
1470                         /* according 802.11-2012 standard, these five types are not robust types */
1471                         if (subtype == WIFI_ACTION &&
1472                         (pframe[WLAN_HDR_A3_LEN] == RTW_WLAN_CATEGORY_PUBLIC ||
1473                         pframe[WLAN_HDR_A3_LEN] == RTW_WLAN_CATEGORY_HT ||
1474                         pframe[WLAN_HDR_A3_LEN] == RTW_WLAN_CATEGORY_UNPROTECTED_WNM ||
1475                         pframe[WLAN_HDR_A3_LEN] == RTW_WLAN_CATEGORY_SELF_PROTECTED  ||
1476                         pframe[WLAN_HDR_A3_LEN] == RTW_WLAN_CATEGORY_P2P))
1477                                 goto xmitframe_coalesce_fail;
1478                         /* before encrypt dump the management packet content */
1479                         if (pattrib->encrypt > 0)
1480                                 memcpy(pattrib->dot118021x_UncstKey.skey, psta->dot118021x_UncstKey.skey, 16);
1481                         /* bakeup original management packet */
1482                         memcpy(tmp_buf, pframe, pattrib->pktlen);
1483                         /* move to data portion */
1484                         pframe += pattrib->hdrlen;
1485
1486                         /* 802.11w unicast management packet must be _AES_ */
1487                         pattrib->iv_len = 8;
1488                         /* it's MIC of AES */
1489                         pattrib->icv_len = 8;
1490
1491                         switch (pattrib->encrypt) {
1492                         case _AES_:
1493                                         /* set AES IV header */
1494                                         AES_IV(pattrib->iv, psta->dot11wtxpn, 0);
1495                                 break;
1496                         default:
1497                                 goto xmitframe_coalesce_fail;
1498                         }
1499                         /* insert iv header into management frame */
1500                         memcpy(pframe, pattrib->iv, pattrib->iv_len);
1501                         pframe += pattrib->iv_len;
1502                         /* copy mgmt data portion after CCMP header */
1503                         memcpy(pframe, tmp_buf+pattrib->hdrlen, pattrib->pktlen-pattrib->hdrlen);
1504                         /* move pframe to end of mgmt pkt */
1505                         pframe += pattrib->pktlen-pattrib->hdrlen;
1506                         /* add 8 bytes CCMP IV header to length */
1507                         pattrib->pktlen += pattrib->iv_len;
1508                         if ((pattrib->icv_len > 0) && (pattrib->bswenc)) {
1509                                 memcpy(pframe, pattrib->icv, pattrib->icv_len);
1510                                 pframe += pattrib->icv_len;
1511                         }
1512                         /* add 8 bytes MIC */
1513                         pattrib->pktlen += pattrib->icv_len;
1514                         /* set final tx command size */
1515                         pattrib->last_txcmdsz = pattrib->pktlen;
1516
1517                         /* set protected bit must be beofre SW encrypt */
1518                         SetPrivacy(mem_start);
1519                         /* software encrypt */
1520                         xmitframe_swencrypt(padapter, pxmitframe);
1521                 }
1522         }
1523
1524 xmitframe_coalesce_success:
1525         spin_unlock_bh(&padapter->security_key_mutex);
1526         kfree(BIP_AAD);
1527         return _SUCCESS;
1528
1529 xmitframe_coalesce_fail:
1530         spin_unlock_bh(&padapter->security_key_mutex);
1531         kfree(BIP_AAD);
1532         return _FAIL;
1533 }
1534
1535 /* Logical Link Control(LLC) SubNetwork Attachment Point(SNAP) header
1536  * IEEE LLC/SNAP header contains 8 octets
1537  * First 3 octets comprise the LLC portion
1538  * SNAP portion, 5 octets, is divided into two fields:
1539  *Organizationally Unique Identifier(OUI), 3 octets,
1540  *type, defined by that organization, 2 octets.
1541  */
1542 s32 rtw_put_snap(u8 *data, u16 h_proto)
1543 {
1544         struct ieee80211_snap_hdr *snap;
1545         u8 *oui;
1546
1547         snap = (struct ieee80211_snap_hdr *)data;
1548         snap->dsap = 0xaa;
1549         snap->ssap = 0xaa;
1550         snap->ctrl = 0x03;
1551
1552         if (h_proto == 0x8137 || h_proto == 0x80f3)
1553                 oui = P802_1H_OUI;
1554         else
1555                 oui = RFC1042_OUI;
1556
1557         snap->oui[0] = oui[0];
1558         snap->oui[1] = oui[1];
1559         snap->oui[2] = oui[2];
1560
1561         *(__be16 *)(data + SNAP_SIZE) = htons(h_proto);
1562
1563         return SNAP_SIZE + sizeof(u16);
1564 }
1565
1566 void rtw_update_protection(struct adapter *padapter, u8 *ie, uint ie_len)
1567 {
1568
1569         uint    protection;
1570         u8 *perp;
1571         sint     erp_len;
1572         struct  xmit_priv *pxmitpriv = &padapter->xmitpriv;
1573         struct  registry_priv *pregistrypriv = &padapter->registrypriv;
1574
1575         switch (pxmitpriv->vcs_setting) {
1576         case DISABLE_VCS:
1577                 pxmitpriv->vcs = NONE_VCS;
1578                 break;
1579
1580         case ENABLE_VCS:
1581                 break;
1582
1583         case AUTO_VCS:
1584         default:
1585                 perp = rtw_get_ie(ie, _ERPINFO_IE_, &erp_len, ie_len);
1586                 if (perp == NULL)
1587                         pxmitpriv->vcs = NONE_VCS;
1588                 else{
1589                         protection = (*(perp + 2)) & BIT(1);
1590                         if (protection) {
1591                                 if (pregistrypriv->vcs_type == RTS_CTS)
1592                                         pxmitpriv->vcs = RTS_CTS;
1593                                 else
1594                                         pxmitpriv->vcs = CTS_TO_SELF;
1595                         } else
1596                                 pxmitpriv->vcs = NONE_VCS;
1597                 }
1598
1599                 break;
1600
1601         }
1602 }
1603
1604 void rtw_count_tx_stats(struct adapter *padapter, struct xmit_frame *pxmitframe, int sz)
1605 {
1606         struct sta_info *psta = NULL;
1607         struct stainfo_stats *pstats = NULL;
1608         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1609         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1610         u8 pkt_num = 1;
1611
1612         if ((pxmitframe->frame_tag&0x0f) == DATA_FRAMETAG) {
1613                 pkt_num = pxmitframe->agg_num;
1614
1615                 pmlmepriv->LinkDetectInfo.NumTxOkInPeriod += pkt_num;
1616
1617                 pxmitpriv->tx_pkts += pkt_num;
1618
1619                 pxmitpriv->tx_bytes += sz;
1620
1621                 psta = pxmitframe->attrib.psta;
1622                 if (psta) {
1623                         pstats = &psta->sta_stats;
1624
1625                         pstats->tx_pkts += pkt_num;
1626
1627                         pstats->tx_bytes += sz;
1628                 }
1629         }
1630 }
1631
1632 static struct xmit_buf *__rtw_alloc_cmd_xmitbuf(struct xmit_priv *pxmitpriv,
1633                 enum cmdbuf_type buf_type)
1634 {
1635         struct xmit_buf *pxmitbuf =  NULL;
1636
1637         pxmitbuf = &pxmitpriv->pcmd_xmitbuf[buf_type];
1638         if (pxmitbuf !=  NULL) {
1639                 pxmitbuf->priv_data = NULL;
1640
1641                 pxmitbuf->len = 0;
1642                 pxmitbuf->pdata = pxmitbuf->ptail = pxmitbuf->phead;
1643                 pxmitbuf->agg_num = 0;
1644                 pxmitbuf->pg_num = 0;
1645
1646                 if (pxmitbuf->sctx) {
1647                         DBG_871X("%s pxmitbuf->sctx is not NULL\n", __func__);
1648                         rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_ALLOC);
1649                 }
1650         } else
1651                 DBG_871X("%s fail, no xmitbuf available !!!\n", __func__);
1652
1653         return pxmitbuf;
1654 }
1655
1656 struct xmit_frame *__rtw_alloc_cmdxmitframe(struct xmit_priv *pxmitpriv,
1657                 enum cmdbuf_type buf_type)
1658 {
1659         struct xmit_frame               *pcmdframe;
1660         struct xmit_buf         *pxmitbuf;
1661
1662         pcmdframe = rtw_alloc_xmitframe(pxmitpriv);
1663         if (pcmdframe == NULL) {
1664                 DBG_871X("%s, alloc xmitframe fail\n", __func__);
1665                 return NULL;
1666         }
1667
1668         pxmitbuf = __rtw_alloc_cmd_xmitbuf(pxmitpriv, buf_type);
1669         if (pxmitbuf == NULL) {
1670                 DBG_871X("%s, alloc xmitbuf fail\n", __func__);
1671                 rtw_free_xmitframe(pxmitpriv, pcmdframe);
1672                 return NULL;
1673         }
1674
1675         pcmdframe->frame_tag = MGNT_FRAMETAG;
1676
1677         pcmdframe->pxmitbuf = pxmitbuf;
1678
1679         pcmdframe->buf_addr = pxmitbuf->pbuf;
1680
1681         pxmitbuf->priv_data = pcmdframe;
1682
1683         return pcmdframe;
1684
1685 }
1686
1687 struct xmit_buf *rtw_alloc_xmitbuf_ext(struct xmit_priv *pxmitpriv)
1688 {
1689         _irqL irqL;
1690         struct xmit_buf *pxmitbuf =  NULL;
1691         struct list_head *plist, *phead;
1692         struct __queue *pfree_queue = &pxmitpriv->free_xmit_extbuf_queue;
1693
1694         spin_lock_irqsave(&pfree_queue->lock, irqL);
1695
1696         if (list_empty(&pfree_queue->queue)) {
1697                 pxmitbuf = NULL;
1698         } else {
1699
1700                 phead = get_list_head(pfree_queue);
1701
1702                 plist = get_next(phead);
1703
1704                 pxmitbuf = LIST_CONTAINOR(plist, struct xmit_buf, list);
1705
1706                 list_del_init(&(pxmitbuf->list));
1707         }
1708
1709         if (pxmitbuf !=  NULL) {
1710                 pxmitpriv->free_xmit_extbuf_cnt--;
1711                 #ifdef DBG_XMIT_BUF_EXT
1712                 DBG_871X("DBG_XMIT_BUF_EXT ALLOC no =%d,  free_xmit_extbuf_cnt =%d\n", pxmitbuf->no, pxmitpriv->free_xmit_extbuf_cnt);
1713                 #endif
1714
1715
1716                 pxmitbuf->priv_data = NULL;
1717
1718                 pxmitbuf->len = 0;
1719                 pxmitbuf->pdata = pxmitbuf->ptail = pxmitbuf->phead;
1720                 pxmitbuf->agg_num = 1;
1721
1722                 if (pxmitbuf->sctx) {
1723                         DBG_871X("%s pxmitbuf->sctx is not NULL\n", __func__);
1724                         rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_ALLOC);
1725                 }
1726
1727         }
1728
1729         spin_unlock_irqrestore(&pfree_queue->lock, irqL);
1730
1731         return pxmitbuf;
1732 }
1733
1734 s32 rtw_free_xmitbuf_ext(struct xmit_priv *pxmitpriv, struct xmit_buf *pxmitbuf)
1735 {
1736         _irqL irqL;
1737         struct __queue *pfree_queue = &pxmitpriv->free_xmit_extbuf_queue;
1738
1739         if (pxmitbuf == NULL)
1740                 return _FAIL;
1741
1742         spin_lock_irqsave(&pfree_queue->lock, irqL);
1743
1744         list_del_init(&pxmitbuf->list);
1745
1746         list_add_tail(&(pxmitbuf->list), get_list_head(pfree_queue));
1747         pxmitpriv->free_xmit_extbuf_cnt++;
1748         #ifdef DBG_XMIT_BUF_EXT
1749         DBG_871X("DBG_XMIT_BUF_EXT FREE no =%d, free_xmit_extbuf_cnt =%d\n", pxmitbuf->no, pxmitpriv->free_xmit_extbuf_cnt);
1750         #endif
1751
1752         spin_unlock_irqrestore(&pfree_queue->lock, irqL);
1753
1754         return _SUCCESS;
1755 }
1756
1757 struct xmit_buf *rtw_alloc_xmitbuf(struct xmit_priv *pxmitpriv)
1758 {
1759         _irqL irqL;
1760         struct xmit_buf *pxmitbuf =  NULL;
1761         struct list_head *plist, *phead;
1762         struct __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
1763
1764         /* DBG_871X("+rtw_alloc_xmitbuf\n"); */
1765
1766         spin_lock_irqsave(&pfree_xmitbuf_queue->lock, irqL);
1767
1768         if (list_empty(&pfree_xmitbuf_queue->queue)) {
1769                 pxmitbuf = NULL;
1770         } else {
1771
1772                 phead = get_list_head(pfree_xmitbuf_queue);
1773
1774                 plist = get_next(phead);
1775
1776                 pxmitbuf = LIST_CONTAINOR(plist, struct xmit_buf, list);
1777
1778                 list_del_init(&(pxmitbuf->list));
1779         }
1780
1781         if (pxmitbuf !=  NULL) {
1782                 pxmitpriv->free_xmitbuf_cnt--;
1783                 #ifdef DBG_XMIT_BUF
1784                 DBG_871X("DBG_XMIT_BUF ALLOC no =%d,  free_xmitbuf_cnt =%d\n", pxmitbuf->no, pxmitpriv->free_xmitbuf_cnt);
1785                 #endif
1786                 /* DBG_871X("alloc, free_xmitbuf_cnt =%d\n", pxmitpriv->free_xmitbuf_cnt); */
1787
1788                 pxmitbuf->priv_data = NULL;
1789
1790                 pxmitbuf->len = 0;
1791                 pxmitbuf->pdata = pxmitbuf->ptail = pxmitbuf->phead;
1792                 pxmitbuf->agg_num = 0;
1793                 pxmitbuf->pg_num = 0;
1794
1795                 if (pxmitbuf->sctx) {
1796                         DBG_871X("%s pxmitbuf->sctx is not NULL\n", __func__);
1797                         rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_ALLOC);
1798                 }
1799         }
1800         #ifdef DBG_XMIT_BUF
1801         else
1802                 DBG_871X("DBG_XMIT_BUF rtw_alloc_xmitbuf return NULL\n");
1803         #endif
1804
1805         spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, irqL);
1806
1807         return pxmitbuf;
1808 }
1809
1810 s32 rtw_free_xmitbuf(struct xmit_priv *pxmitpriv, struct xmit_buf *pxmitbuf)
1811 {
1812         _irqL irqL;
1813         struct __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
1814
1815         /* DBG_871X("+rtw_free_xmitbuf\n"); */
1816
1817         if (pxmitbuf == NULL)
1818                 return _FAIL;
1819
1820         if (pxmitbuf->sctx) {
1821                 DBG_871X("%s pxmitbuf->sctx is not NULL\n", __func__);
1822                 rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_FREE);
1823         }
1824
1825         if (pxmitbuf->buf_tag == XMITBUF_CMD) {
1826         } else if (pxmitbuf->buf_tag == XMITBUF_MGNT) {
1827                 rtw_free_xmitbuf_ext(pxmitpriv, pxmitbuf);
1828         } else{
1829                 spin_lock_irqsave(&pfree_xmitbuf_queue->lock, irqL);
1830
1831                 list_del_init(&pxmitbuf->list);
1832
1833                 list_add_tail(&(pxmitbuf->list), get_list_head(pfree_xmitbuf_queue));
1834
1835                 pxmitpriv->free_xmitbuf_cnt++;
1836                 /* DBG_871X("FREE, free_xmitbuf_cnt =%d\n", pxmitpriv->free_xmitbuf_cnt); */
1837                 #ifdef DBG_XMIT_BUF
1838                 DBG_871X("DBG_XMIT_BUF FREE no =%d, free_xmitbuf_cnt =%d\n", pxmitbuf->no, pxmitpriv->free_xmitbuf_cnt);
1839                 #endif
1840                 spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, irqL);
1841         }
1842         return _SUCCESS;
1843 }
1844
1845 static void rtw_init_xmitframe(struct xmit_frame *pxframe)
1846 {
1847         if (pxframe !=  NULL) { /* default value setting */
1848                 pxframe->buf_addr = NULL;
1849                 pxframe->pxmitbuf = NULL;
1850
1851                 memset(&pxframe->attrib, 0, sizeof(struct pkt_attrib));
1852                 /* pxframe->attrib.psta = NULL; */
1853
1854                 pxframe->frame_tag = DATA_FRAMETAG;
1855
1856                 pxframe->pg_num = 1;
1857                 pxframe->agg_num = 1;
1858                 pxframe->ack_report = 0;
1859         }
1860 }
1861
1862 /*
1863 Calling context:
1864 1. OS_TXENTRY
1865 2. RXENTRY (rx_thread or RX_ISR/RX_CallBack)
1866
1867 If we turn on USE_RXTHREAD, then, no need for critical section.
1868 Otherwise, we must use _enter/_exit critical to protect free_xmit_queue...
1869
1870 Must be very very cautious...
1871
1872 */
1873 struct xmit_frame *rtw_alloc_xmitframe(struct xmit_priv *pxmitpriv)/* _queue *pfree_xmit_queue) */
1874 {
1875         /*
1876                 Please remember to use all the osdep_service api,
1877                 and lock/unlock or _enter/_exit critical to protect
1878                 pfree_xmit_queue
1879         */
1880
1881         struct xmit_frame *pxframe = NULL;
1882         struct list_head *plist, *phead;
1883         struct __queue *pfree_xmit_queue = &pxmitpriv->free_xmit_queue;
1884
1885         spin_lock_bh(&pfree_xmit_queue->lock);
1886
1887         if (list_empty(&pfree_xmit_queue->queue)) {
1888                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_alloc_xmitframe:%d\n", pxmitpriv->free_xmitframe_cnt));
1889                 pxframe =  NULL;
1890         } else {
1891                 phead = get_list_head(pfree_xmit_queue);
1892
1893                 plist = get_next(phead);
1894
1895                 pxframe = LIST_CONTAINOR(plist, struct xmit_frame, list);
1896
1897                 list_del_init(&(pxframe->list));
1898                 pxmitpriv->free_xmitframe_cnt--;
1899                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_alloc_xmitframe():free_xmitframe_cnt =%d\n", pxmitpriv->free_xmitframe_cnt));
1900         }
1901
1902         spin_unlock_bh(&pfree_xmit_queue->lock);
1903
1904         rtw_init_xmitframe(pxframe);
1905         return pxframe;
1906 }
1907
1908 struct xmit_frame *rtw_alloc_xmitframe_ext(struct xmit_priv *pxmitpriv)
1909 {
1910         struct xmit_frame *pxframe = NULL;
1911         struct list_head *plist, *phead;
1912         struct __queue *queue = &pxmitpriv->free_xframe_ext_queue;
1913
1914         spin_lock_bh(&queue->lock);
1915
1916         if (list_empty(&queue->queue)) {
1917                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_alloc_xmitframe_ext:%d\n", pxmitpriv->free_xframe_ext_cnt));
1918                 pxframe =  NULL;
1919         } else {
1920                 phead = get_list_head(queue);
1921                 plist = get_next(phead);
1922                 pxframe = LIST_CONTAINOR(plist, struct xmit_frame, list);
1923
1924                 list_del_init(&(pxframe->list));
1925                 pxmitpriv->free_xframe_ext_cnt--;
1926                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_alloc_xmitframe_ext():free_xmitframe_cnt =%d\n", pxmitpriv->free_xframe_ext_cnt));
1927         }
1928
1929         spin_unlock_bh(&queue->lock);
1930
1931         rtw_init_xmitframe(pxframe);
1932
1933         return pxframe;
1934 }
1935
1936 struct xmit_frame *rtw_alloc_xmitframe_once(struct xmit_priv *pxmitpriv)
1937 {
1938         struct xmit_frame *pxframe = NULL;
1939         u8 *alloc_addr;
1940
1941         alloc_addr = rtw_zmalloc(sizeof(struct xmit_frame) + 4);
1942
1943         if (alloc_addr == NULL)
1944                 goto exit;
1945
1946         pxframe = (struct xmit_frame *)N_BYTE_ALIGMENT((SIZE_PTR)(alloc_addr), 4);
1947         pxframe->alloc_addr = alloc_addr;
1948
1949         pxframe->padapter = pxmitpriv->adapter;
1950         pxframe->frame_tag = NULL_FRAMETAG;
1951
1952         pxframe->pkt = NULL;
1953
1954         pxframe->buf_addr = NULL;
1955         pxframe->pxmitbuf = NULL;
1956
1957         rtw_init_xmitframe(pxframe);
1958
1959         DBG_871X("################## %s ##################\n", __func__);
1960
1961 exit:
1962         return pxframe;
1963 }
1964
1965 s32 rtw_free_xmitframe(struct xmit_priv *pxmitpriv, struct xmit_frame *pxmitframe)
1966 {
1967         struct __queue *queue = NULL;
1968         struct adapter *padapter = pxmitpriv->adapter;
1969         _pkt *pndis_pkt = NULL;
1970
1971         if (pxmitframe == NULL) {
1972                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("======rtw_free_xmitframe():pxmitframe == NULL!!!!!!!!!!\n"));
1973                 goto exit;
1974         }
1975
1976         if (pxmitframe->pkt) {
1977                 pndis_pkt = pxmitframe->pkt;
1978                 pxmitframe->pkt = NULL;
1979         }
1980
1981         if (pxmitframe->alloc_addr) {
1982                 DBG_871X("################## %s with alloc_addr ##################\n", __func__);
1983                 kfree(pxmitframe->alloc_addr);
1984                 goto check_pkt_complete;
1985         }
1986
1987         if (pxmitframe->ext_tag == 0)
1988                 queue = &pxmitpriv->free_xmit_queue;
1989         else if (pxmitframe->ext_tag == 1)
1990                 queue = &pxmitpriv->free_xframe_ext_queue;
1991         else {
1992
1993         }
1994
1995         spin_lock_bh(&queue->lock);
1996
1997         list_del_init(&pxmitframe->list);
1998         list_add_tail(&pxmitframe->list, get_list_head(queue));
1999         if (pxmitframe->ext_tag == 0) {
2000                 pxmitpriv->free_xmitframe_cnt++;
2001                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_debug_, ("rtw_free_xmitframe():free_xmitframe_cnt =%d\n", pxmitpriv->free_xmitframe_cnt));
2002         } else if (pxmitframe->ext_tag == 1) {
2003                 pxmitpriv->free_xframe_ext_cnt++;
2004                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_debug_, ("rtw_free_xmitframe():free_xframe_ext_cnt =%d\n", pxmitpriv->free_xframe_ext_cnt));
2005         } else {
2006         }
2007
2008         spin_unlock_bh(&queue->lock);
2009
2010 check_pkt_complete:
2011
2012         if (pndis_pkt)
2013                 rtw_os_pkt_complete(padapter, pndis_pkt);
2014
2015 exit:
2016         return _SUCCESS;
2017 }
2018
2019 void rtw_free_xmitframe_queue(struct xmit_priv *pxmitpriv, struct __queue *pframequeue)
2020 {
2021         struct list_head        *plist, *phead;
2022         struct  xmit_frame      *pxmitframe;
2023
2024         spin_lock_bh(&(pframequeue->lock));
2025
2026         phead = get_list_head(pframequeue);
2027         plist = get_next(phead);
2028
2029         while (phead != plist) {
2030
2031                 pxmitframe = LIST_CONTAINOR(plist, struct xmit_frame, list);
2032
2033                 plist = get_next(plist);
2034
2035                 rtw_free_xmitframe(pxmitpriv, pxmitframe);
2036
2037         }
2038         spin_unlock_bh(&(pframequeue->lock));
2039 }
2040
2041 s32 rtw_xmitframe_enqueue(struct adapter *padapter, struct xmit_frame *pxmitframe)
2042 {
2043         DBG_COUNTER(padapter->tx_logs.core_tx_enqueue);
2044         if (rtw_xmit_classifier(padapter, pxmitframe) == _FAIL) {
2045                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_,
2046                          ("rtw_xmitframe_enqueue: drop xmit pkt for classifier fail\n"));
2047 /*              pxmitframe->pkt = NULL; */
2048                 return _FAIL;
2049         }
2050
2051         return _SUCCESS;
2052 }
2053
2054 struct tx_servq *rtw_get_sta_pending(struct adapter *padapter, struct sta_info *psta, sint up, u8 *ac)
2055 {
2056         struct tx_servq *ptxservq = NULL;
2057
2058         switch (up) {
2059         case 1:
2060         case 2:
2061                 ptxservq = &(psta->sta_xmitpriv.bk_q);
2062                 *(ac) = 3;
2063                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_get_sta_pending : BK\n"));
2064                 break;
2065
2066         case 4:
2067         case 5:
2068                 ptxservq = &(psta->sta_xmitpriv.vi_q);
2069                 *(ac) = 1;
2070                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_get_sta_pending : VI\n"));
2071                 break;
2072
2073         case 6:
2074         case 7:
2075                 ptxservq = &(psta->sta_xmitpriv.vo_q);
2076                 *(ac) = 0;
2077                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_get_sta_pending : VO\n"));
2078                 break;
2079
2080         case 0:
2081         case 3:
2082         default:
2083                 ptxservq = &(psta->sta_xmitpriv.be_q);
2084                 *(ac) = 2;
2085                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_get_sta_pending : BE\n"));
2086         break;
2087
2088         }
2089
2090         return ptxservq;
2091 }
2092
2093 /*
2094  * Will enqueue pxmitframe to the proper queue,
2095  * and indicate it to xx_pending list.....
2096  */
2097 s32 rtw_xmit_classifier(struct adapter *padapter, struct xmit_frame *pxmitframe)
2098 {
2099         /* _irqL irqL0; */
2100         u8 ac_index;
2101         struct sta_info *psta;
2102         struct tx_servq *ptxservq;
2103         struct pkt_attrib       *pattrib = &pxmitframe->attrib;
2104         struct hw_xmit  *phwxmits =  padapter->xmitpriv.hwxmits;
2105         sint res = _SUCCESS;
2106
2107         DBG_COUNTER(padapter->tx_logs.core_tx_enqueue_class);
2108
2109 /*
2110         if (pattrib->psta) {
2111                 psta = pattrib->psta;
2112         } else {
2113                 DBG_871X("%s, call rtw_get_stainfo()\n", __func__);
2114                 psta = rtw_get_stainfo(pstapriv, pattrib->ra);
2115         }
2116 */
2117
2118         psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
2119         if (pattrib->psta != psta) {
2120                 DBG_COUNTER(padapter->tx_logs.core_tx_enqueue_class_err_sta);
2121                 DBG_871X("%s, pattrib->psta(%p) != psta(%p)\n", __func__, pattrib->psta, psta);
2122                 return _FAIL;
2123         }
2124
2125         if (psta == NULL) {
2126                 DBG_COUNTER(padapter->tx_logs.core_tx_enqueue_class_err_nosta);
2127                 res = _FAIL;
2128                 DBG_8192C("rtw_xmit_classifier: psta == NULL\n");
2129                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("rtw_xmit_classifier: psta == NULL\n"));
2130                 goto exit;
2131         }
2132
2133         if (!(psta->state & _FW_LINKED)) {
2134                 DBG_COUNTER(padapter->tx_logs.core_tx_enqueue_class_err_fwlink);
2135                 DBG_871X("%s, psta->state(0x%x) != _FW_LINKED\n", __func__, psta->state);
2136                 return _FAIL;
2137         }
2138
2139         ptxservq = rtw_get_sta_pending(padapter, psta, pattrib->priority, (u8 *)(&ac_index));
2140
2141         /* spin_lock_irqsave(&pstapending->lock, irqL0); */
2142
2143         if (list_empty(&ptxservq->tx_pending)) {
2144                 list_add_tail(&ptxservq->tx_pending, get_list_head(phwxmits[ac_index].sta_queue));
2145         }
2146
2147         /* spin_lock_irqsave(&ptxservq->sta_pending.lock, irqL1); */
2148
2149         list_add_tail(&pxmitframe->list, get_list_head(&ptxservq->sta_pending));
2150         ptxservq->qcnt++;
2151         phwxmits[ac_index].accnt++;
2152
2153         /* spin_unlock_irqrestore(&ptxservq->sta_pending.lock, irqL1); */
2154
2155         /* spin_unlock_irqrestore(&pstapending->lock, irqL0); */
2156
2157 exit:
2158
2159         return res;
2160 }
2161
2162 s32 rtw_alloc_hwxmits(struct adapter *padapter)
2163 {
2164         struct hw_xmit *hwxmits;
2165         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2166
2167         pxmitpriv->hwxmit_entry = HWXMIT_ENTRY;
2168
2169         pxmitpriv->hwxmits = NULL;
2170
2171         pxmitpriv->hwxmits = (struct hw_xmit *)rtw_zmalloc(sizeof(struct hw_xmit) * pxmitpriv->hwxmit_entry);
2172
2173         if (!pxmitpriv->hwxmits)
2174                 return _FAIL;
2175
2176         hwxmits = pxmitpriv->hwxmits;
2177
2178         if (pxmitpriv->hwxmit_entry == 5) {
2179                 /* pxmitpriv->bmc_txqueue.head = 0; */
2180                 /* hwxmits[0] .phwtxqueue = &pxmitpriv->bmc_txqueue; */
2181                 hwxmits[0] .sta_queue = &pxmitpriv->bm_pending;
2182
2183                 /* pxmitpriv->vo_txqueue.head = 0; */
2184                 /* hwxmits[1] .phwtxqueue = &pxmitpriv->vo_txqueue; */
2185                 hwxmits[1] .sta_queue = &pxmitpriv->vo_pending;
2186
2187                 /* pxmitpriv->vi_txqueue.head = 0; */
2188                 /* hwxmits[2] .phwtxqueue = &pxmitpriv->vi_txqueue; */
2189                 hwxmits[2] .sta_queue = &pxmitpriv->vi_pending;
2190
2191                 /* pxmitpriv->bk_txqueue.head = 0; */
2192                 /* hwxmits[3] .phwtxqueue = &pxmitpriv->bk_txqueue; */
2193                 hwxmits[3] .sta_queue = &pxmitpriv->bk_pending;
2194
2195                 /* pxmitpriv->be_txqueue.head = 0; */
2196                 /* hwxmits[4] .phwtxqueue = &pxmitpriv->be_txqueue; */
2197                 hwxmits[4] .sta_queue = &pxmitpriv->be_pending;
2198
2199         } else if (pxmitpriv->hwxmit_entry == 4) {
2200
2201                 /* pxmitpriv->vo_txqueue.head = 0; */
2202                 /* hwxmits[0] .phwtxqueue = &pxmitpriv->vo_txqueue; */
2203                 hwxmits[0] .sta_queue = &pxmitpriv->vo_pending;
2204
2205                 /* pxmitpriv->vi_txqueue.head = 0; */
2206                 /* hwxmits[1] .phwtxqueue = &pxmitpriv->vi_txqueue; */
2207                 hwxmits[1] .sta_queue = &pxmitpriv->vi_pending;
2208
2209                 /* pxmitpriv->be_txqueue.head = 0; */
2210                 /* hwxmits[2] .phwtxqueue = &pxmitpriv->be_txqueue; */
2211                 hwxmits[2] .sta_queue = &pxmitpriv->be_pending;
2212
2213                 /* pxmitpriv->bk_txqueue.head = 0; */
2214                 /* hwxmits[3] .phwtxqueue = &pxmitpriv->bk_txqueue; */
2215                 hwxmits[3] .sta_queue = &pxmitpriv->bk_pending;
2216         } else {
2217
2218         }
2219
2220         return _SUCCESS;
2221 }
2222
2223 void rtw_free_hwxmits(struct adapter *padapter)
2224 {
2225         struct hw_xmit *hwxmits;
2226         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2227
2228         hwxmits = pxmitpriv->hwxmits;
2229         if (hwxmits)
2230                 kfree((u8 *)hwxmits);
2231 }
2232
2233 void rtw_init_hwxmits(struct hw_xmit *phwxmit, sint entry)
2234 {
2235         sint i;
2236
2237         for (i = 0; i < entry; i++, phwxmit++) {
2238                 /* spin_lock_init(&phwxmit->xmit_lock); */
2239                 /* INIT_LIST_HEAD(&phwxmit->pending); */
2240                 /* phwxmit->txcmdcnt = 0; */
2241                 phwxmit->accnt = 0;
2242         }
2243 }
2244
2245 u32 rtw_get_ff_hwaddr(struct xmit_frame *pxmitframe)
2246 {
2247         u32 addr;
2248         struct pkt_attrib *pattrib = &pxmitframe->attrib;
2249
2250         switch (pattrib->qsel) {
2251         case 0:
2252         case 3:
2253                 addr = BE_QUEUE_INX;
2254                 break;
2255         case 1:
2256         case 2:
2257                 addr = BK_QUEUE_INX;
2258                 break;
2259         case 4:
2260         case 5:
2261                 addr = VI_QUEUE_INX;
2262                 break;
2263         case 6:
2264         case 7:
2265                 addr = VO_QUEUE_INX;
2266                 break;
2267         case 0x10:
2268                 addr = BCN_QUEUE_INX;
2269                 break;
2270         case 0x11:/* BC/MC in PS (HIQ) */
2271                 addr = HIGH_QUEUE_INX;
2272                 break;
2273         case 0x12:
2274         default:
2275                 addr = MGT_QUEUE_INX;
2276                 break;
2277
2278         }
2279
2280         return addr;
2281
2282 }
2283
2284 static void do_queue_select(struct adapter      *padapter, struct pkt_attrib *pattrib)
2285 {
2286         u8 qsel;
2287
2288         qsel = pattrib->priority;
2289         RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("### do_queue_select priority =%d , qsel = %d\n", pattrib->priority, qsel));
2290
2291         pattrib->qsel = qsel;
2292 }
2293
2294 /*
2295  * The main transmit(tx) entry
2296  *
2297  * Return
2298  *1     enqueue
2299  *0     success, hardware will handle this xmit frame(packet)
2300  *<0    fail
2301  */
2302 s32 rtw_xmit(struct adapter *padapter, _pkt **ppkt)
2303 {
2304         static unsigned long start;
2305         static u32 drop_cnt;
2306
2307         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2308         struct xmit_frame *pxmitframe = NULL;
2309
2310         s32 res;
2311
2312         DBG_COUNTER(padapter->tx_logs.core_tx);
2313
2314         if (start == 0)
2315                 start = jiffies;
2316
2317         pxmitframe = rtw_alloc_xmitframe(pxmitpriv);
2318
2319         if (jiffies_to_msecs(jiffies - start) > 2000) {
2320                 if (drop_cnt)
2321                         DBG_871X("DBG_TX_DROP_FRAME %s no more pxmitframe, drop_cnt:%u\n", __func__, drop_cnt);
2322                 start = jiffies;
2323                 drop_cnt = 0;
2324         }
2325
2326         if (pxmitframe == NULL) {
2327                 drop_cnt++;
2328                 RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit: no more pxmitframe\n"));
2329                 DBG_COUNTER(padapter->tx_logs.core_tx_err_pxmitframe);
2330                 return -1;
2331         }
2332
2333         res = update_attrib(padapter, *ppkt, &pxmitframe->attrib);
2334
2335         if (res == _FAIL) {
2336                 RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit: update attrib fail\n"));
2337                 #ifdef DBG_TX_DROP_FRAME
2338                 DBG_871X("DBG_TX_DROP_FRAME %s update attrib fail\n", __func__);
2339                 #endif
2340                 rtw_free_xmitframe(pxmitpriv, pxmitframe);
2341                 return -1;
2342         }
2343         pxmitframe->pkt = *ppkt;
2344
2345         do_queue_select(padapter, &pxmitframe->attrib);
2346
2347         spin_lock_bh(&pxmitpriv->lock);
2348         if (xmitframe_enqueue_for_sleeping_sta(padapter, pxmitframe) == true) {
2349                 spin_unlock_bh(&pxmitpriv->lock);
2350                 DBG_COUNTER(padapter->tx_logs.core_tx_ap_enqueue);
2351                 return 1;
2352         }
2353         spin_unlock_bh(&pxmitpriv->lock);
2354
2355         /* pre_xmitframe */
2356         if (rtw_hal_xmit(padapter, pxmitframe) == false)
2357                 return 1;
2358
2359         return 0;
2360 }
2361
2362 #define RTW_HIQ_FILTER_ALLOW_ALL 0
2363 #define RTW_HIQ_FILTER_ALLOW_SPECIAL 1
2364 #define RTW_HIQ_FILTER_DENY_ALL 2
2365
2366 inline bool xmitframe_hiq_filter(struct xmit_frame *xmitframe)
2367 {
2368         bool allow = false;
2369         struct adapter *adapter = xmitframe->padapter;
2370         struct registry_priv *registry = &adapter->registrypriv;
2371
2372         if (registry->hiq_filter == RTW_HIQ_FILTER_ALLOW_SPECIAL) {
2373
2374                 struct pkt_attrib *attrib = &xmitframe->attrib;
2375
2376                 if (attrib->ether_type == 0x0806
2377                         || attrib->ether_type == 0x888e
2378                         || attrib->dhcp_pkt
2379                 ) {
2380                         DBG_871X(FUNC_ADPT_FMT" ether_type:0x%04x%s\n", FUNC_ADPT_ARG(xmitframe->padapter)
2381                                 , attrib->ether_type, attrib->dhcp_pkt?" DHCP":"");
2382                         allow = true;
2383                 }
2384         } else if (registry->hiq_filter == RTW_HIQ_FILTER_ALLOW_ALL)
2385                 allow = true;
2386         else if (registry->hiq_filter == RTW_HIQ_FILTER_DENY_ALL) {
2387         } else
2388                 rtw_warn_on(1);
2389
2390         return allow;
2391 }
2392
2393 sint xmitframe_enqueue_for_sleeping_sta(struct adapter *padapter, struct xmit_frame *pxmitframe)
2394 {
2395         sint ret = false;
2396         struct sta_info *psta = NULL;
2397         struct sta_priv *pstapriv = &padapter->stapriv;
2398         struct pkt_attrib *pattrib = &pxmitframe->attrib;
2399         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2400         sint bmcst = IS_MCAST(pattrib->ra);
2401         bool update_tim = false;
2402
2403         if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == false) {
2404                 DBG_COUNTER(padapter->tx_logs.core_tx_ap_enqueue_warn_fwstate);
2405             return ret;
2406         }
2407 /*
2408         if (pattrib->psta)
2409         {
2410                 psta = pattrib->psta;
2411         }
2412         else
2413         {
2414                 DBG_871X("%s, call rtw_get_stainfo()\n", __func__);
2415                 psta =rtw_get_stainfo(pstapriv, pattrib->ra);
2416         }
2417 */
2418         psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
2419         if (pattrib->psta != psta) {
2420                 DBG_COUNTER(padapter->tx_logs.core_tx_ap_enqueue_warn_sta);
2421                 DBG_871X("%s, pattrib->psta(%p) != psta(%p)\n", __func__, pattrib->psta, psta);
2422                 return false;
2423         }
2424
2425         if (psta == NULL) {
2426                 DBG_COUNTER(padapter->tx_logs.core_tx_ap_enqueue_warn_nosta);
2427                 DBG_871X("%s, psta ==NUL\n", __func__);
2428                 return false;
2429         }
2430
2431         if (!(psta->state & _FW_LINKED)) {
2432                 DBG_COUNTER(padapter->tx_logs.core_tx_ap_enqueue_warn_link);
2433                 DBG_871X("%s, psta->state(0x%x) != _FW_LINKED\n", __func__, psta->state);
2434                 return false;
2435         }
2436
2437         if (pattrib->triggered == 1) {
2438                 DBG_COUNTER(padapter->tx_logs.core_tx_ap_enqueue_warn_trigger);
2439                 /* DBG_871X("directly xmit pspoll_triggered packet\n"); */
2440
2441                 /* pattrib->triggered = 0; */
2442                 if (bmcst && xmitframe_hiq_filter(pxmitframe) == true)
2443                         pattrib->qsel = 0x11;/* HIQ */
2444
2445                 return ret;
2446         }
2447
2448
2449         if (bmcst) {
2450                 spin_lock_bh(&psta->sleep_q.lock);
2451
2452                 if (pstapriv->sta_dz_bitmap) { /* if anyone sta is in ps mode */
2453                         /* pattrib->qsel = 0x11;HIQ */
2454
2455                         list_del_init(&pxmitframe->list);
2456
2457                         /* spin_lock_bh(&psta->sleep_q.lock); */
2458
2459                         list_add_tail(&pxmitframe->list, get_list_head(&psta->sleep_q));
2460
2461                         psta->sleepq_len++;
2462
2463                         if (!(pstapriv->tim_bitmap & BIT(0)))
2464                                 update_tim = true;
2465
2466                         pstapriv->tim_bitmap |= BIT(0);/*  */
2467                         pstapriv->sta_dz_bitmap |= BIT(0);
2468
2469                         /* DBG_871X("enqueue, sq_len =%d, tim =%x\n", psta->sleepq_len, pstapriv->tim_bitmap); */
2470
2471                         if (update_tim == true) {
2472                                 update_beacon(padapter, _TIM_IE_, NULL, true);
2473                         } else {
2474                                 chk_bmc_sleepq_cmd(padapter);
2475                         }
2476
2477                         /* spin_unlock_bh(&psta->sleep_q.lock); */
2478
2479                         ret = true;
2480
2481                         DBG_COUNTER(padapter->tx_logs.core_tx_ap_enqueue_mcast);
2482
2483                 }
2484
2485                 spin_unlock_bh(&psta->sleep_q.lock);
2486
2487                 return ret;
2488
2489         }
2490
2491
2492         spin_lock_bh(&psta->sleep_q.lock);
2493
2494         if (psta->state&WIFI_SLEEP_STATE) {
2495                 u8 wmmps_ac = 0;
2496
2497                 if (pstapriv->sta_dz_bitmap & BIT(psta->aid)) {
2498                         list_del_init(&pxmitframe->list);
2499
2500                         /* spin_lock_bh(&psta->sleep_q.lock); */
2501
2502                         list_add_tail(&pxmitframe->list, get_list_head(&psta->sleep_q));
2503
2504                         psta->sleepq_len++;
2505
2506                         switch (pattrib->priority) {
2507                         case 1:
2508                         case 2:
2509                                 wmmps_ac = psta->uapsd_bk&BIT(0);
2510                                 break;
2511                         case 4:
2512                         case 5:
2513                                 wmmps_ac = psta->uapsd_vi&BIT(0);
2514                                 break;
2515                         case 6:
2516                         case 7:
2517                                 wmmps_ac = psta->uapsd_vo&BIT(0);
2518                                 break;
2519                         case 0:
2520                         case 3:
2521                         default:
2522                                 wmmps_ac = psta->uapsd_be&BIT(0);
2523                                 break;
2524                         }
2525
2526                         if (wmmps_ac)
2527                                 psta->sleepq_ac_len++;
2528
2529                         if (((psta->has_legacy_ac) && (!wmmps_ac)) || ((!psta->has_legacy_ac) && (wmmps_ac))) {
2530                                 if (!(pstapriv->tim_bitmap & BIT(psta->aid)))
2531                                         update_tim = true;
2532
2533                                 pstapriv->tim_bitmap |= BIT(psta->aid);
2534
2535                                 /* DBG_871X("enqueue, sq_len =%d, tim =%x\n", psta->sleepq_len, pstapriv->tim_bitmap); */
2536
2537                                 if (update_tim == true)
2538                                         /* DBG_871X("sleepq_len == 1, update BCNTIM\n"); */
2539                                         /* upate BCN for TIM IE */
2540                                         update_beacon(padapter, _TIM_IE_, NULL, true);
2541                         }
2542
2543                         /* spin_unlock_bh(&psta->sleep_q.lock); */
2544
2545                         /* if (psta->sleepq_len > (NR_XMITFRAME>>3)) */
2546                         /*  */
2547                         /*      wakeup_sta_to_xmit(padapter, psta); */
2548                         /*  */
2549
2550                         ret = true;
2551
2552                         DBG_COUNTER(padapter->tx_logs.core_tx_ap_enqueue_ucast);
2553                 }
2554
2555         }
2556
2557         spin_unlock_bh(&psta->sleep_q.lock);
2558
2559         return ret;
2560
2561 }
2562
2563 static void dequeue_xmitframes_to_sleeping_queue(struct adapter *padapter, struct sta_info *psta, struct __queue *pframequeue)
2564 {
2565         sint ret;
2566         struct list_head        *plist, *phead;
2567         u8 ac_index;
2568         struct tx_servq *ptxservq;
2569         struct pkt_attrib       *pattrib;
2570         struct xmit_frame       *pxmitframe;
2571         struct hw_xmit *phwxmits =  padapter->xmitpriv.hwxmits;
2572
2573         phead = get_list_head(pframequeue);
2574         plist = get_next(phead);
2575
2576         while (phead != plist) {
2577                 pxmitframe = LIST_CONTAINOR(plist, struct xmit_frame, list);
2578
2579                 plist = get_next(plist);
2580
2581                 pattrib = &pxmitframe->attrib;
2582
2583                 pattrib->triggered = 0;
2584
2585                 ret = xmitframe_enqueue_for_sleeping_sta(padapter, pxmitframe);
2586
2587                 if (true == ret) {
2588                         ptxservq = rtw_get_sta_pending(padapter, psta, pattrib->priority, (u8 *)(&ac_index));
2589
2590                         ptxservq->qcnt--;
2591                         phwxmits[ac_index].accnt--;
2592                 } else {
2593                         /* DBG_871X("xmitframe_enqueue_for_sleeping_sta return false\n"); */
2594                 }
2595
2596         }
2597
2598 }
2599
2600 void stop_sta_xmit(struct adapter *padapter, struct sta_info *psta)
2601 {
2602         struct sta_info *psta_bmc;
2603         struct sta_xmit_priv *pstaxmitpriv;
2604         struct sta_priv *pstapriv = &padapter->stapriv;
2605         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2606
2607         pstaxmitpriv = &psta->sta_xmitpriv;
2608
2609         /* for BC/MC Frames */
2610         psta_bmc = rtw_get_bcmc_stainfo(padapter);
2611
2612
2613         spin_lock_bh(&pxmitpriv->lock);
2614
2615         psta->state |= WIFI_SLEEP_STATE;
2616
2617         pstapriv->sta_dz_bitmap |= BIT(psta->aid);
2618
2619
2620
2621         dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->vo_q.sta_pending);
2622         list_del_init(&(pstaxmitpriv->vo_q.tx_pending));
2623
2624
2625         dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->vi_q.sta_pending);
2626         list_del_init(&(pstaxmitpriv->vi_q.tx_pending));
2627
2628
2629         dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->be_q.sta_pending);
2630         list_del_init(&(pstaxmitpriv->be_q.tx_pending));
2631
2632
2633         dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->bk_q.sta_pending);
2634         list_del_init(&(pstaxmitpriv->bk_q.tx_pending));
2635
2636         /* for BC/MC Frames */
2637         pstaxmitpriv = &psta_bmc->sta_xmitpriv;
2638         dequeue_xmitframes_to_sleeping_queue(padapter, psta_bmc, &pstaxmitpriv->be_q.sta_pending);
2639         list_del_init(&(pstaxmitpriv->be_q.tx_pending));
2640
2641         spin_unlock_bh(&pxmitpriv->lock);
2642 }
2643
2644 void wakeup_sta_to_xmit(struct adapter *padapter, struct sta_info *psta)
2645 {
2646         u8 update_mask = 0, wmmps_ac = 0;
2647         struct sta_info *psta_bmc;
2648         struct list_head        *xmitframe_plist, *xmitframe_phead;
2649         struct xmit_frame *pxmitframe = NULL;
2650         struct sta_priv *pstapriv = &padapter->stapriv;
2651         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2652
2653         psta_bmc = rtw_get_bcmc_stainfo(padapter);
2654
2655
2656         /* spin_lock_bh(&psta->sleep_q.lock); */
2657         spin_lock_bh(&pxmitpriv->lock);
2658
2659         xmitframe_phead = get_list_head(&psta->sleep_q);
2660         xmitframe_plist = get_next(xmitframe_phead);
2661
2662         while (xmitframe_phead != xmitframe_plist) {
2663                 pxmitframe = LIST_CONTAINOR(xmitframe_plist, struct xmit_frame, list);
2664
2665                 xmitframe_plist = get_next(xmitframe_plist);
2666
2667                 list_del_init(&pxmitframe->list);
2668
2669                 switch (pxmitframe->attrib.priority) {
2670                 case 1:
2671                 case 2:
2672                         wmmps_ac = psta->uapsd_bk&BIT(1);
2673                         break;
2674                 case 4:
2675                 case 5:
2676                         wmmps_ac = psta->uapsd_vi&BIT(1);
2677                         break;
2678                 case 6:
2679                 case 7:
2680                         wmmps_ac = psta->uapsd_vo&BIT(1);
2681                         break;
2682                 case 0:
2683                 case 3:
2684                 default:
2685                         wmmps_ac = psta->uapsd_be&BIT(1);
2686                         break;
2687                 }
2688
2689                 psta->sleepq_len--;
2690                 if (psta->sleepq_len > 0)
2691                         pxmitframe->attrib.mdata = 1;
2692                 else
2693                         pxmitframe->attrib.mdata = 0;
2694
2695                 if (wmmps_ac) {
2696                         psta->sleepq_ac_len--;
2697                         if (psta->sleepq_ac_len > 0) {
2698                                 pxmitframe->attrib.mdata = 1;
2699                                 pxmitframe->attrib.eosp = 0;
2700                         } else{
2701                                 pxmitframe->attrib.mdata = 0;
2702                                 pxmitframe->attrib.eosp = 1;
2703                         }
2704                 }
2705
2706                 pxmitframe->attrib.triggered = 1;
2707
2708 /*
2709                 spin_unlock_bh(&psta->sleep_q.lock);
2710                 if (rtw_hal_xmit(padapter, pxmitframe) == true)
2711                 {
2712                         rtw_os_xmit_complete(padapter, pxmitframe);
2713                 }
2714                 spin_lock_bh(&psta->sleep_q.lock);
2715 */
2716                 rtw_hal_xmitframe_enqueue(padapter, pxmitframe);
2717
2718
2719         }
2720
2721         if (psta->sleepq_len == 0) {
2722                 if (pstapriv->tim_bitmap & BIT(psta->aid)) {
2723                         /* DBG_871X("wakeup to xmit, qlen == 0, update_BCNTIM, tim =%x\n", pstapriv->tim_bitmap); */
2724                         /* upate BCN for TIM IE */
2725                         /* update_BCNTIM(padapter); */
2726                         update_mask = BIT(0);
2727                 }
2728
2729                 pstapriv->tim_bitmap &= ~BIT(psta->aid);
2730
2731                 if (psta->state&WIFI_SLEEP_STATE)
2732                         psta->state ^= WIFI_SLEEP_STATE;
2733
2734                 if (psta->state & WIFI_STA_ALIVE_CHK_STATE) {
2735                         DBG_871X("%s alive check\n", __func__);
2736                         psta->expire_to = pstapriv->expire_to;
2737                         psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
2738                 }
2739
2740                 pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
2741         }
2742
2743         /* for BC/MC Frames */
2744         if (!psta_bmc)
2745                 goto _exit;
2746
2747         if ((pstapriv->sta_dz_bitmap&0xfffe) == 0x0) { /* no any sta in ps mode */
2748                 xmitframe_phead = get_list_head(&psta_bmc->sleep_q);
2749                 xmitframe_plist = get_next(xmitframe_phead);
2750
2751                 while (xmitframe_phead != xmitframe_plist) {
2752                         pxmitframe = LIST_CONTAINOR(xmitframe_plist, struct xmit_frame, list);
2753
2754                         xmitframe_plist = get_next(xmitframe_plist);
2755
2756                         list_del_init(&pxmitframe->list);
2757
2758                         psta_bmc->sleepq_len--;
2759                         if (psta_bmc->sleepq_len > 0)
2760                                 pxmitframe->attrib.mdata = 1;
2761                         else
2762                                 pxmitframe->attrib.mdata = 0;
2763
2764
2765                         pxmitframe->attrib.triggered = 1;
2766 /*
2767                         spin_unlock_bh(&psta_bmc->sleep_q.lock);
2768                         if (rtw_hal_xmit(padapter, pxmitframe) == true)
2769                         {
2770                                 rtw_os_xmit_complete(padapter, pxmitframe);
2771                         }
2772                         spin_lock_bh(&psta_bmc->sleep_q.lock);
2773
2774 */
2775                         rtw_hal_xmitframe_enqueue(padapter, pxmitframe);
2776
2777                 }
2778
2779                 if (psta_bmc->sleepq_len == 0) {
2780                         if (pstapriv->tim_bitmap & BIT(0)) {
2781                                 /* DBG_871X("wakeup to xmit, qlen == 0, update_BCNTIM, tim =%x\n", pstapriv->tim_bitmap); */
2782                                 /* upate BCN for TIM IE */
2783                                 /* update_BCNTIM(padapter); */
2784                                 update_mask |= BIT(1);
2785                         }
2786                         pstapriv->tim_bitmap &= ~BIT(0);
2787                         pstapriv->sta_dz_bitmap &= ~BIT(0);
2788                 }
2789
2790         }
2791
2792 _exit:
2793
2794         /* spin_unlock_bh(&psta_bmc->sleep_q.lock); */
2795         spin_unlock_bh(&pxmitpriv->lock);
2796
2797         if (update_mask)
2798                 /* update_BCNTIM(padapter); */
2799                 /* printk("%s => call update_beacon\n", __func__); */
2800                 update_beacon(padapter, _TIM_IE_, NULL, true);
2801
2802 }
2803
2804 void xmit_delivery_enabled_frames(struct adapter *padapter, struct sta_info *psta)
2805 {
2806         u8 wmmps_ac = 0;
2807         struct list_head        *xmitframe_plist, *xmitframe_phead;
2808         struct xmit_frame *pxmitframe = NULL;
2809         struct sta_priv *pstapriv = &padapter->stapriv;
2810         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2811
2812
2813         /* spin_lock_bh(&psta->sleep_q.lock); */
2814         spin_lock_bh(&pxmitpriv->lock);
2815
2816         xmitframe_phead = get_list_head(&psta->sleep_q);
2817         xmitframe_plist = get_next(xmitframe_phead);
2818
2819         while (xmitframe_phead != xmitframe_plist) {
2820                 pxmitframe = LIST_CONTAINOR(xmitframe_plist, struct xmit_frame, list);
2821
2822                 xmitframe_plist = get_next(xmitframe_plist);
2823
2824                 switch (pxmitframe->attrib.priority) {
2825                 case 1:
2826                 case 2:
2827                         wmmps_ac = psta->uapsd_bk&BIT(1);
2828                         break;
2829                 case 4:
2830                 case 5:
2831                         wmmps_ac = psta->uapsd_vi&BIT(1);
2832                         break;
2833                 case 6:
2834                 case 7:
2835                         wmmps_ac = psta->uapsd_vo&BIT(1);
2836                         break;
2837                 case 0:
2838                 case 3:
2839                 default:
2840                         wmmps_ac = psta->uapsd_be&BIT(1);
2841                         break;
2842                 }
2843
2844                 if (!wmmps_ac)
2845                         continue;
2846
2847                 list_del_init(&pxmitframe->list);
2848
2849                 psta->sleepq_len--;
2850                 psta->sleepq_ac_len--;
2851
2852                 if (psta->sleepq_ac_len > 0) {
2853                         pxmitframe->attrib.mdata = 1;
2854                         pxmitframe->attrib.eosp = 0;
2855                 } else{
2856                         pxmitframe->attrib.mdata = 0;
2857                         pxmitframe->attrib.eosp = 1;
2858                 }
2859
2860                 pxmitframe->attrib.triggered = 1;
2861                 rtw_hal_xmitframe_enqueue(padapter, pxmitframe);
2862
2863                 if ((psta->sleepq_ac_len == 0) && (!psta->has_legacy_ac) && (wmmps_ac)) {
2864                         pstapriv->tim_bitmap &= ~BIT(psta->aid);
2865
2866                         /* DBG_871X("wakeup to xmit, qlen == 0, update_BCNTIM, tim =%x\n", pstapriv->tim_bitmap); */
2867                         /* upate BCN for TIM IE */
2868                         /* update_BCNTIM(padapter); */
2869                         update_beacon(padapter, _TIM_IE_, NULL, true);
2870                         /* update_mask = BIT(0); */
2871                 }
2872
2873         }
2874
2875         /* spin_unlock_bh(&psta->sleep_q.lock); */
2876         spin_unlock_bh(&pxmitpriv->lock);
2877
2878         return;
2879 }
2880
2881 void enqueue_pending_xmitbuf(
2882         struct xmit_priv *pxmitpriv,
2883         struct xmit_buf *pxmitbuf)
2884 {
2885         struct __queue *pqueue;
2886         struct adapter *pri_adapter = pxmitpriv->adapter;
2887
2888         pqueue = &pxmitpriv->pending_xmitbuf_queue;
2889
2890         spin_lock_bh(&pqueue->lock);
2891         list_del_init(&pxmitbuf->list);
2892         list_add_tail(&pxmitbuf->list, get_list_head(pqueue));
2893         spin_unlock_bh(&pqueue->lock);
2894
2895         up(&(pri_adapter->xmitpriv.xmit_sema));
2896 }
2897
2898 void enqueue_pending_xmitbuf_to_head(
2899         struct xmit_priv *pxmitpriv,
2900         struct xmit_buf *pxmitbuf)
2901 {
2902         struct __queue *pqueue;
2903
2904         pqueue = &pxmitpriv->pending_xmitbuf_queue;
2905
2906         spin_lock_bh(&pqueue->lock);
2907         list_del_init(&pxmitbuf->list);
2908         list_add(&pxmitbuf->list, get_list_head(pqueue));
2909         spin_unlock_bh(&pqueue->lock);
2910 }
2911
2912 struct xmit_buf *dequeue_pending_xmitbuf(
2913         struct xmit_priv *pxmitpriv)
2914 {
2915         struct xmit_buf *pxmitbuf;
2916         struct __queue *pqueue;
2917
2918
2919         pxmitbuf = NULL;
2920         pqueue = &pxmitpriv->pending_xmitbuf_queue;
2921
2922         spin_lock_bh(&pqueue->lock);
2923
2924         if (!list_empty(&pqueue->queue)) {
2925                 struct list_head *plist, *phead;
2926
2927                 phead = get_list_head(pqueue);
2928                 plist = get_next(phead);
2929                 pxmitbuf = LIST_CONTAINOR(plist, struct xmit_buf, list);
2930                 list_del_init(&pxmitbuf->list);
2931         }
2932
2933         spin_unlock_bh(&pqueue->lock);
2934
2935         return pxmitbuf;
2936 }
2937
2938 struct xmit_buf *dequeue_pending_xmitbuf_under_survey(
2939         struct xmit_priv *pxmitpriv)
2940 {
2941         struct xmit_buf *pxmitbuf;
2942         struct __queue *pqueue;
2943
2944
2945         pxmitbuf = NULL;
2946         pqueue = &pxmitpriv->pending_xmitbuf_queue;
2947
2948         spin_lock_bh(&pqueue->lock);
2949
2950         if (!list_empty(&pqueue->queue)) {
2951                 struct list_head *plist, *phead;
2952                 u8 type;
2953
2954                 phead = get_list_head(pqueue);
2955                 plist = phead;
2956                 do {
2957                         plist = get_next(plist);
2958                         if (plist == phead)
2959                                 break;
2960
2961                         pxmitbuf = LIST_CONTAINOR(plist, struct xmit_buf, list);
2962
2963                         type = GetFrameSubType(pxmitbuf->pbuf + TXDESC_OFFSET);
2964
2965                         if ((type == WIFI_PROBEREQ) ||
2966                                 (type == WIFI_DATA_NULL) ||
2967                                 (type == WIFI_QOS_DATA_NULL)) {
2968                                 list_del_init(&pxmitbuf->list);
2969                                 break;
2970                         }
2971                         pxmitbuf = NULL;
2972                 } while (1);
2973         }
2974
2975         spin_unlock_bh(&pqueue->lock);
2976
2977         return pxmitbuf;
2978 }
2979
2980 sint check_pending_xmitbuf(
2981         struct xmit_priv *pxmitpriv)
2982 {
2983         struct __queue *pqueue;
2984         sint    ret = false;
2985
2986         pqueue = &pxmitpriv->pending_xmitbuf_queue;
2987
2988         spin_lock_bh(&pqueue->lock);
2989
2990         if (!list_empty(&pqueue->queue))
2991                 ret = true;
2992
2993         spin_unlock_bh(&pqueue->lock);
2994
2995         return ret;
2996 }
2997
2998 int rtw_xmit_thread(void *context)
2999 {
3000         s32 err;
3001         struct adapter *padapter;
3002
3003
3004         err = _SUCCESS;
3005         padapter = context;
3006
3007         thread_enter("RTW_XMIT_THREAD");
3008
3009         do {
3010                 err = rtw_hal_xmit_thread_handler(padapter);
3011                 flush_signals_thread();
3012         } while (_SUCCESS == err);
3013
3014         up(&padapter->xmitpriv.terminate_xmitthread_sema);
3015
3016         thread_exit();
3017 }
3018
3019 void rtw_sctx_init(struct submit_ctx *sctx, int timeout_ms)
3020 {
3021         sctx->timeout_ms = timeout_ms;
3022         sctx->submit_time = jiffies;
3023         init_completion(&sctx->done);
3024         sctx->status = RTW_SCTX_SUBMITTED;
3025 }
3026
3027 int rtw_sctx_wait(struct submit_ctx *sctx, const char *msg)
3028 {
3029         int ret = _FAIL;
3030         unsigned long expire;
3031         int status = 0;
3032
3033         expire = sctx->timeout_ms ? msecs_to_jiffies(sctx->timeout_ms) : MAX_SCHEDULE_TIMEOUT;
3034         if (!wait_for_completion_timeout(&sctx->done, expire)) {
3035                 /* timeout, do something?? */
3036                 status = RTW_SCTX_DONE_TIMEOUT;
3037                 DBG_871X("%s timeout: %s\n", __func__, msg);
3038         } else {
3039                 status = sctx->status;
3040         }
3041
3042         if (status == RTW_SCTX_DONE_SUCCESS) {
3043                 ret = _SUCCESS;
3044         }
3045
3046         return ret;
3047 }
3048
3049 static bool rtw_sctx_chk_waring_status(int status)
3050 {
3051         switch (status) {
3052         case RTW_SCTX_DONE_UNKNOWN:
3053         case RTW_SCTX_DONE_BUF_ALLOC:
3054         case RTW_SCTX_DONE_BUF_FREE:
3055
3056         case RTW_SCTX_DONE_DRV_STOP:
3057         case RTW_SCTX_DONE_DEV_REMOVE:
3058                 return true;
3059         default:
3060                 return false;
3061         }
3062 }
3063
3064 void rtw_sctx_done_err(struct submit_ctx **sctx, int status)
3065 {
3066         if (*sctx) {
3067                 if (rtw_sctx_chk_waring_status(status))
3068                         DBG_871X("%s status:%d\n", __func__, status);
3069                 (*sctx)->status = status;
3070                 complete(&((*sctx)->done));
3071                 *sctx = NULL;
3072         }
3073 }
3074
3075 void rtw_sctx_done(struct submit_ctx **sctx)
3076 {
3077         rtw_sctx_done_err(sctx, RTW_SCTX_DONE_SUCCESS);
3078 }
3079
3080 int rtw_ack_tx_wait(struct xmit_priv *pxmitpriv, u32 timeout_ms)
3081 {
3082         struct submit_ctx *pack_tx_ops = &pxmitpriv->ack_tx_ops;
3083
3084         pack_tx_ops->submit_time = jiffies;
3085         pack_tx_ops->timeout_ms = timeout_ms;
3086         pack_tx_ops->status = RTW_SCTX_SUBMITTED;
3087
3088         return rtw_sctx_wait(pack_tx_ops, __func__);
3089 }
3090
3091 void rtw_ack_tx_done(struct xmit_priv *pxmitpriv, int status)
3092 {
3093         struct submit_ctx *pack_tx_ops = &pxmitpriv->ack_tx_ops;
3094
3095         if (pxmitpriv->ack_tx) {
3096                 rtw_sctx_done_err(&pack_tx_ops, status);
3097         } else {
3098                 DBG_871X("%s ack_tx not set\n", __func__);
3099         }
3100 }