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