staging: rtl8188eu: Remove rtw_is_list_empty(), wrapper for list_emty()
[linux-block.git] / drivers / staging / rtl8188eu / core / rtw_xmit.c
CommitLineData
d6846af6
LF
1/******************************************************************************
2 *
3 * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 *
19 ******************************************************************************/
20#define _RTW_XMIT_C_
21
22#include <osdep_service.h>
23#include <drv_types.h>
24#include <wifi.h>
25#include <osdep_intf.h>
d6846af6 26#include <usb_osintf.h>
d249db9e 27#include <linux/vmalloc.h>
d6846af6
LF
28
29static u8 P802_1H_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0xf8 };
30static u8 RFC1042_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0x00 };
31
32static void _init_txservq(struct tx_servq *ptxservq)
33{
d6846af6
LF
34 _rtw_init_listhead(&ptxservq->tx_pending);
35 _rtw_init_queue(&ptxservq->sta_pending);
36 ptxservq->qcnt = 0;
d6846af6
LF
37}
38
39void _rtw_init_sta_xmit_priv(struct sta_xmit_priv *psta_xmitpriv)
40{
1ce39848 41 memset((unsigned char *)psta_xmitpriv, 0, sizeof (struct sta_xmit_priv));
f214e521 42 spin_lock_init(&psta_xmitpriv->lock);
d6846af6
LF
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 _rtw_init_listhead(&psta_xmitpriv->legacy_dz);
48 _rtw_init_listhead(&psta_xmitpriv->apsd);
49
d6846af6
LF
50}
51
52s32 _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
d6846af6 61
2397c6e0 62 /* We don't need to memset padapter->XXX to zero, because adapter is allocated by vzalloc(). */
d6846af6 63
f214e521 64 spin_lock_init(&pxmitpriv->lock);
efbef100
LF
65 sema_init(&pxmitpriv->xmit_sema, 0);
66 sema_init(&pxmitpriv->terminate_xmitthread_sema, 0);
d6846af6
LF
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
2397c6e0 88 pxmitpriv->pallocated_frame_buf = vzalloc(NR_XMITFRAME * sizeof(struct xmit_frame) + 4);
d6846af6
LF
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 _rtw_init_listhead(&(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
ae6787ad 113 list_add_tail(&(pxframe->list), &(pxmitpriv->free_xmit_queue.queue));
d6846af6
LF
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
2397c6e0 126 pxmitpriv->pallocated_xmitbuf = vzalloc(NR_XMITBUFF * sizeof(struct xmit_buf) + 4);
d6846af6
LF
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 _rtw_init_listhead(&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) {
0da46e6b 150 msleep(10);
d6846af6
LF
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
ae6787ad 159 list_add_tail(&pxmitbuf->list, &(pxmitpriv->free_xmitbuf_queue.queue));
d6846af6
LF
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
2397c6e0 168 pxmitpriv->pallocated_xmit_extbuf = vzalloc(num_xmit_extbuf * sizeof(struct xmit_buf) + 4);
d6846af6
LF
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 _rtw_init_listhead(&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
ae6787ad 193 list_add_tail(&pxmitbuf->list, &(pxmitpriv->free_xmit_extbuf_queue.queue));
d6846af6
LF
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
efbef100 207 sema_init(&(pxmitpriv->tx_retevt), 0);
d6846af6
LF
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;
2ca4ab53 216 mutex_init(&pxmitpriv->ack_tx_mutex);
d6846af6
LF
217 rtw_sctx_init(&pxmitpriv->ack_tx_ops, 0);
218
219 rtw_hal_init_xmit_priv(padapter);
220
221exit:
222
d6846af6
LF
223
224 return res;
225}
226
d6846af6
LF
227void _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
d6846af6 236 if (pxmitpriv->pxmit_frame_buf == NULL)
f578b5d3 237 return;
d6846af6
LF
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)
03bd6aea 251 vfree(pxmitpriv->pallocated_frame_buf);
d6846af6
LF
252
253 if (pxmitpriv->pallocated_xmitbuf)
03bd6aea 254 vfree(pxmitpriv->pallocated_xmitbuf);
d6846af6
LF
255
256 /* free xmit extension buff */
d6846af6
LF
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) {
03bd6aea 264 vfree(pxmitpriv->pallocated_xmit_extbuf);
d6846af6
LF
265 }
266
267 rtw_free_hwxmits(padapter);
268
4b33d52a 269 mutex_destroy(&pxmitpriv->ack_tx_mutex);
d6846af6
LF
270}
271
272static 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
349static 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
374u8 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
405static 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
430static 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
d6846af6
LF
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
d6846af6
LF
620 update_attrib_phy_info(pattrib, psta);
621
622exit:
623
d6846af6
LF
624
625 return res;
626}
627
628static 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
d6846af6 646
74772fcf 647 hw_hdr_offset = TXDESC_SIZE + (pxmitframe->pkt_offset * PACKET_OFFSET_SZ);
d6846af6
LF
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) {
f42f52aa 659 if (!memcmp(psecuritypriv->dot118021XGrptxmickey[psecuritypriv->dot118021XGrpKeyid].skey, null_key, 16))
d6846af6
LF
660 return _FAIL;
661 /* start to calculate the mic code */
662 rtw_secmicsetkey(&micdata, psecuritypriv->dot118021XGrptxmickey[psecuritypriv->dot118021XGrpKeyid].skey);
663 } else {
05c9bc1f 664 if (!memcmp(&stainfo->dot11tkiptxmickey.skey[0], null_key, 16)) {
d6846af6 665 /* DbgPrint("\nxmitframe_addmic:stainfo->dot11tkiptxmickey == 0\n"); */
0da46e6b 666 /* msleep(10); */
d6846af6
LF
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 *)RND4((size_t)(payload));
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
d6846af6
LF
743
744 return _SUCCESS;
745}
746
747static s32 xmitframe_swencrypt(struct adapter *padapter, struct xmit_frame *pxmitframe)
748{
749 struct pkt_attrib *pattrib = &pxmitframe->attrib;
750
d6846af6
LF
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
d6846af6
LF
772
773 return _SUCCESS;
774}
775
776s32 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;
7e83fd6d 786 __le16 *fctrl = &pwlanhdr->frame_ctl;
d6846af6
LF
787
788 struct sta_info *psta;
789
790 int bmcst = IS_MCAST(pattrib->ra);
791
d6846af6
LF
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
1ce39848 803 memset(hdr, 0, WLANHDR_OFFSET);
d6846af6
LF
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 }
895exit:
896
d6846af6
LF
897 return res;
898}
899
900s32 rtw_txframes_pending(struct adapter *padapter)
901{
902 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
903
904 return ((_rtw_queue_empty(&pxmitpriv->be_pending) == false) ||
905 (_rtw_queue_empty(&pxmitpriv->bk_pending) == false) ||
906 (_rtw_queue_empty(&pxmitpriv->vi_pending) == false) ||
907 (_rtw_queue_empty(&pxmitpriv->vo_pending) == false));
908}
909
910s32 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 */
945u32 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
961This sub-routine will perform all the following:
962
9631. remove 802.3 header.
9642. create wlan_header, based on the info in pxmitframe
9653. append sta's iv/ext-iv
9664. append LLC
9675. move frag chunk from pframe to pxmitframe->mem
9686. apply sw-encrypt, if necessary.
969
970*/
971s32 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
d6846af6
LF
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 if (psta != NULL) {
1030 switch (pattrib->encrypt) {
1031 case _WEP40_:
1032 case _WEP104_:
1033 WEP_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
1034 break;
1035 case _TKIP_:
1036 if (bmcst)
1037 TKIP_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
1038 else
1039 TKIP_IV(pattrib->iv, psta->dot11txpn, 0);
1040 break;
1041 case _AES_:
1042 if (bmcst)
1043 AES_IV(pattrib->iv, psta->dot11txpn, pattrib->key_idx);
1044 else
1045 AES_IV(pattrib->iv, psta->dot11txpn, 0);
1046 break;
1047 }
1048 }
1049
1050 memcpy(pframe, pattrib->iv, pattrib->iv_len);
1051
1052 RT_TRACE(_module_rtl871x_xmit_c_, _drv_notice_,
1053 ("rtw_xmitframe_coalesce: keyid=%d pattrib->iv[3]=%.2x pframe=%.2x %.2x %.2x %.2x\n",
1054 padapter->securitypriv.dot11PrivacyKeyIndex, pattrib->iv[3], *pframe, *(pframe+1), *(pframe+2), *(pframe+3)));
1055
1056 pframe += pattrib->iv_len;
1057
1058 mpdu_len -= pattrib->iv_len;
1059 }
1060
1061 if (frg_inx == 0) {
1062 llc_sz = rtw_put_snap(pframe, pattrib->ether_type);
1063 pframe += llc_sz;
1064 mpdu_len -= llc_sz;
1065 }
1066
1067 if ((pattrib->icv_len > 0) && (pattrib->bswenc)) {
1068 mpdu_len -= pattrib->icv_len;
1069 }
1070
1071 if (bmcst) {
1072 /* don't do fragment to broadcat/multicast packets */
1073 mem_sz = _rtw_pktfile_read(&pktfile, pframe, pattrib->pktlen);
1074 } else {
1075 mem_sz = _rtw_pktfile_read(&pktfile, pframe, mpdu_len);
1076 }
1077
1078 pframe += mem_sz;
1079
1080 if ((pattrib->icv_len > 0) && (pattrib->bswenc)) {
1081 memcpy(pframe, pattrib->icv, pattrib->icv_len);
1082 pframe += pattrib->icv_len;
1083 }
1084
1085 frg_inx++;
1086
1087 if (bmcst || rtw_endofpktfile(&pktfile)) {
1088 pattrib->nr_frags = frg_inx;
1089
1090 pattrib->last_txcmdsz = pattrib->hdrlen + pattrib->iv_len + ((pattrib->nr_frags == 1) ? llc_sz : 0) +
1091 ((pattrib->bswenc) ? pattrib->icv_len : 0) + mem_sz;
1092
1093 ClearMFrag(mem_start);
1094
1095 break;
1096 } else {
1097 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("%s: There're still something in packet!\n", __func__));
1098 }
1099
1100 addr = (size_t)(pframe);
1101
1102 mem_start = (unsigned char *)RND4(addr) + hw_hdr_offset;
1103 memcpy(mem_start, pbuf_start + hw_hdr_offset, pattrib->hdrlen);
1104 }
1105
1106 if (xmitframe_addmic(padapter, pxmitframe) == _FAIL) {
1107 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("xmitframe_addmic(padapter, pxmitframe) == _FAIL\n"));
1108 DBG_88E("xmitframe_addmic(padapter, pxmitframe) == _FAIL\n");
1109 res = _FAIL;
1110 goto exit;
1111 }
1112
1113 xmitframe_swencrypt(padapter, pxmitframe);
1114
1115 if (!bmcst)
1116 update_attrib_vcs_info(padapter, pxmitframe);
1117 else
1118 pattrib->vcs_mode = NONE_VCS;
1119
1120exit:
1121
d6846af6
LF
1122
1123 return res;
1124}
1125
1126/* Logical Link Control(LLC) SubNetwork Attachment Point(SNAP) header
1127 * IEEE LLC/SNAP header contains 8 octets
1128 * First 3 octets comprise the LLC portion
1129 * SNAP portion, 5 octets, is divided into two fields:
1130 * Organizationally Unique Identifier(OUI), 3 octets,
1131 * type, defined by that organization, 2 octets.
1132 */
1133s32 rtw_put_snap(u8 *data, u16 h_proto)
1134{
1135 struct ieee80211_snap_hdr *snap;
1136 u8 *oui;
1137
d6846af6
LF
1138
1139 snap = (struct ieee80211_snap_hdr *)data;
1140 snap->dsap = 0xaa;
1141 snap->ssap = 0xaa;
1142 snap->ctrl = 0x03;
1143
1144 if (h_proto == 0x8137 || h_proto == 0x80f3)
1145 oui = P802_1H_OUI;
1146 else
1147 oui = RFC1042_OUI;
1148
1149 snap->oui[0] = oui[0];
1150 snap->oui[1] = oui[1];
1151 snap->oui[2] = oui[2];
1152
1153 *(__be16 *)(data + SNAP_SIZE) = htons(h_proto);
1154
d6846af6
LF
1155
1156 return SNAP_SIZE + sizeof(u16);
1157}
1158
1159void rtw_update_protection(struct adapter *padapter, u8 *ie, uint ie_len)
1160{
1161 uint protection;
1162 u8 *perp;
1163 int erp_len;
1164 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1165 struct registry_priv *pregistrypriv = &padapter->registrypriv;
1166
d6846af6
LF
1167
1168 switch (pxmitpriv->vcs_setting) {
1169 case DISABLE_VCS:
1170 pxmitpriv->vcs = NONE_VCS;
1171 break;
1172 case ENABLE_VCS:
1173 break;
1174 case AUTO_VCS:
1175 default:
1176 perp = rtw_get_ie(ie, _ERPINFO_IE_, &erp_len, ie_len);
1177 if (perp == NULL) {
1178 pxmitpriv->vcs = NONE_VCS;
1179 } else {
1180 protection = (*(perp + 2)) & BIT(1);
1181 if (protection) {
1182 if (pregistrypriv->vcs_type == RTS_CTS)
1183 pxmitpriv->vcs = RTS_CTS;
1184 else
1185 pxmitpriv->vcs = CTS_TO_SELF;
1186 } else {
1187 pxmitpriv->vcs = NONE_VCS;
1188 }
1189 }
1190 break;
1191 }
1192
d6846af6
LF
1193}
1194
1195void rtw_count_tx_stats(struct adapter *padapter, struct xmit_frame *pxmitframe, int sz)
1196{
1197 struct sta_info *psta = NULL;
1198 struct stainfo_stats *pstats = NULL;
1199 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1200 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1201
1202 if ((pxmitframe->frame_tag&0x0f) == DATA_FRAMETAG) {
1203 pxmitpriv->tx_bytes += sz;
1204 pmlmepriv->LinkDetectInfo.NumTxOkInPeriod += pxmitframe->agg_num;
1205
1206 psta = pxmitframe->attrib.psta;
1207 if (psta) {
1208 pstats = &psta->sta_stats;
1209 pstats->tx_pkts += pxmitframe->agg_num;
1210 pstats->tx_bytes += sz;
1211 }
1212 }
1213}
1214
1215struct xmit_buf *rtw_alloc_xmitbuf_ext(struct xmit_priv *pxmitpriv)
1216{
1217 unsigned long irql;
1218 struct xmit_buf *pxmitbuf = NULL;
1219 struct list_head *plist, *phead;
1220 struct __queue *pfree_queue = &pxmitpriv->free_xmit_extbuf_queue;
1221
d6846af6 1222
f937886b 1223 spin_lock_irqsave(&pfree_queue->lock, irql);
d6846af6
LF
1224
1225 if (_rtw_queue_empty(pfree_queue) == true) {
1226 pxmitbuf = NULL;
1227 } else {
1228 phead = get_list_head(pfree_queue);
1229
c44e5e39 1230 plist = phead->next;
d6846af6 1231
bea88100 1232 pxmitbuf = container_of(plist, struct xmit_buf, list);
d6846af6
LF
1233
1234 rtw_list_delete(&(pxmitbuf->list));
1235 }
1236
1237 if (pxmitbuf != NULL) {
1238 pxmitpriv->free_xmit_extbuf_cnt--;
1239
1240 pxmitbuf->priv_data = NULL;
1241 /* pxmitbuf->ext_tag = true; */
1242
1243 if (pxmitbuf->sctx) {
1244 DBG_88E("%s pxmitbuf->sctx is not NULL\n", __func__);
1245 rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_ALLOC);
1246 }
1247 }
1248
597794f5 1249 spin_unlock_irqrestore(&pfree_queue->lock, irql);
d6846af6 1250
d6846af6
LF
1251
1252 return pxmitbuf;
1253}
1254
1255s32 rtw_free_xmitbuf_ext(struct xmit_priv *pxmitpriv, struct xmit_buf *pxmitbuf)
1256{
1257 unsigned long irql;
1258 struct __queue *pfree_queue = &pxmitpriv->free_xmit_extbuf_queue;
1259
d6846af6
LF
1260
1261 if (pxmitbuf == NULL)
1262 return _FAIL;
1263
f937886b 1264 spin_lock_irqsave(&pfree_queue->lock, irql);
d6846af6
LF
1265
1266 rtw_list_delete(&pxmitbuf->list);
1267
ae6787ad 1268 list_add_tail(&(pxmitbuf->list), get_list_head(pfree_queue));
d6846af6
LF
1269 pxmitpriv->free_xmit_extbuf_cnt++;
1270
597794f5 1271 spin_unlock_irqrestore(&pfree_queue->lock, irql);
d6846af6 1272
d6846af6
LF
1273
1274 return _SUCCESS;
1275}
1276
1277struct xmit_buf *rtw_alloc_xmitbuf(struct xmit_priv *pxmitpriv)
1278{
1279 unsigned long irql;
1280 struct xmit_buf *pxmitbuf = NULL;
1281 struct list_head *plist, *phead;
1282 struct __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
1283
d6846af6
LF
1284
1285 /* DBG_88E("+rtw_alloc_xmitbuf\n"); */
1286
f937886b 1287 spin_lock_irqsave(&pfree_xmitbuf_queue->lock, irql);
d6846af6
LF
1288
1289 if (_rtw_queue_empty(pfree_xmitbuf_queue) == true) {
1290 pxmitbuf = NULL;
1291 } else {
1292 phead = get_list_head(pfree_xmitbuf_queue);
1293
c44e5e39 1294 plist = phead->next;
d6846af6 1295
bea88100 1296 pxmitbuf = container_of(plist, struct xmit_buf, list);
d6846af6
LF
1297
1298 rtw_list_delete(&(pxmitbuf->list));
1299 }
1300
1301 if (pxmitbuf != NULL) {
1302 pxmitpriv->free_xmitbuf_cnt--;
1303 pxmitbuf->priv_data = NULL;
1304 if (pxmitbuf->sctx) {
1305 DBG_88E("%s pxmitbuf->sctx is not NULL\n", __func__);
1306 rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_ALLOC);
1307 }
1308 }
597794f5 1309 spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, irql);
d6846af6 1310
d6846af6
LF
1311
1312 return pxmitbuf;
1313}
1314
1315s32 rtw_free_xmitbuf(struct xmit_priv *pxmitpriv, struct xmit_buf *pxmitbuf)
1316{
1317 unsigned long irql;
1318 struct __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
1319
d6846af6
LF
1320 if (pxmitbuf == NULL)
1321 return _FAIL;
1322
1323 if (pxmitbuf->sctx) {
1324 DBG_88E("%s pxmitbuf->sctx is not NULL\n", __func__);
1325 rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_BUF_FREE);
1326 }
1327
1328 if (pxmitbuf->ext_tag) {
1329 rtw_free_xmitbuf_ext(pxmitpriv, pxmitbuf);
1330 } else {
f937886b 1331 spin_lock_irqsave(&pfree_xmitbuf_queue->lock, irql);
d6846af6
LF
1332
1333 rtw_list_delete(&pxmitbuf->list);
1334
ae6787ad 1335 list_add_tail(&(pxmitbuf->list), get_list_head(pfree_xmitbuf_queue));
d6846af6
LF
1336
1337 pxmitpriv->free_xmitbuf_cnt++;
597794f5 1338 spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, irql);
d6846af6
LF
1339 }
1340
d6846af6
LF
1341
1342 return _SUCCESS;
1343}
1344
1345/*
1346Calling context:
13471. OS_TXENTRY
13482. RXENTRY (rx_thread or RX_ISR/RX_CallBack)
1349
1350If we turn on USE_RXTHREAD, then, no need for critical section.
1351Otherwise, we must use _enter/_exit critical to protect free_xmit_queue...
1352
1353Must be very very cautious...
1354
1355*/
1356
1357struct xmit_frame *rtw_alloc_xmitframe(struct xmit_priv *pxmitpriv)/* _queue *pfree_xmit_queue) */
1358{
1359 /*
1360 Please remember to use all the osdep_service api,
1361 and lock/unlock or _enter/_exit critical to protect
1362 pfree_xmit_queue
1363 */
1364
d6846af6
LF
1365 struct xmit_frame *pxframe = NULL;
1366 struct list_head *plist, *phead;
1367 struct __queue *pfree_xmit_queue = &pxmitpriv->free_xmit_queue;
1368
d6846af6 1369
7057dcb3 1370 spin_lock_bh(&pfree_xmit_queue->lock);
d6846af6
LF
1371
1372 if (_rtw_queue_empty(pfree_xmit_queue) == true) {
1373 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_alloc_xmitframe:%d\n", pxmitpriv->free_xmitframe_cnt));
1374 pxframe = NULL;
1375 } else {
1376 phead = get_list_head(pfree_xmit_queue);
1377
c44e5e39 1378 plist = phead->next;
d6846af6 1379
bea88100 1380 pxframe = container_of(plist, struct xmit_frame, list);
d6846af6
LF
1381
1382 rtw_list_delete(&(pxframe->list));
1383 }
1384
1385 if (pxframe != NULL) { /* default value setting */
1386 pxmitpriv->free_xmitframe_cnt--;
1387
1388 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_alloc_xmitframe():free_xmitframe_cnt=%d\n", pxmitpriv->free_xmitframe_cnt));
1389
1390 pxframe->buf_addr = NULL;
1391 pxframe->pxmitbuf = NULL;
1392
1ce39848 1393 memset(&pxframe->attrib, 0, sizeof(struct pkt_attrib));
d6846af6
LF
1394 /* pxframe->attrib.psta = NULL; */
1395
1396 pxframe->frame_tag = DATA_FRAMETAG;
1397
1398 pxframe->pkt = NULL;
1399 pxframe->pkt_offset = 1;/* default use pkt_offset to fill tx desc */
1400
1401 pxframe->agg_num = 1;
1402 pxframe->ack_report = 0;
1403 }
1404
e02bcf61 1405 spin_unlock_bh(&pfree_xmit_queue->lock);
d6846af6 1406
d6846af6
LF
1407
1408 return pxframe;
1409}
1410
1411s32 rtw_free_xmitframe(struct xmit_priv *pxmitpriv, struct xmit_frame *pxmitframe)
1412{
d6846af6
LF
1413 struct __queue *pfree_xmit_queue = &pxmitpriv->free_xmit_queue;
1414 struct adapter *padapter = pxmitpriv->adapter;
1415 struct sk_buff *pndis_pkt = NULL;
1416
d6846af6
LF
1417
1418 if (pxmitframe == NULL) {
1419 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("====== rtw_free_xmitframe():pxmitframe == NULL!!!!!!!!!!\n"));
1420 goto exit;
1421 }
1422
7057dcb3 1423 spin_lock_bh(&pfree_xmit_queue->lock);
d6846af6
LF
1424
1425 rtw_list_delete(&pxmitframe->list);
1426
1427 if (pxmitframe->pkt) {
1428 pndis_pkt = pxmitframe->pkt;
1429 pxmitframe->pkt = NULL;
1430 }
1431
ae6787ad 1432 list_add_tail(&pxmitframe->list, get_list_head(pfree_xmit_queue));
d6846af6
LF
1433
1434 pxmitpriv->free_xmitframe_cnt++;
1435 RT_TRACE(_module_rtl871x_xmit_c_, _drv_debug_, ("rtw_free_xmitframe():free_xmitframe_cnt=%d\n", pxmitpriv->free_xmitframe_cnt));
1436
e02bcf61 1437 spin_unlock_bh(&pfree_xmit_queue->lock);
d6846af6
LF
1438
1439 if (pndis_pkt)
1440 rtw_os_pkt_complete(padapter, pndis_pkt);
1441
1442exit:
1443
d6846af6
LF
1444
1445 return _SUCCESS;
1446}
1447
1448void rtw_free_xmitframe_queue(struct xmit_priv *pxmitpriv, struct __queue *pframequeue)
1449{
d6846af6
LF
1450 struct list_head *plist, *phead;
1451 struct xmit_frame *pxmitframe;
1452
d6846af6 1453
7057dcb3 1454 spin_lock_bh(&(pframequeue->lock));
d6846af6
LF
1455
1456 phead = get_list_head(pframequeue);
c44e5e39 1457 plist = phead->next;
d6846af6 1458
84660700 1459 while (phead != plist) {
bea88100 1460 pxmitframe = container_of(plist, struct xmit_frame, list);
d6846af6 1461
c44e5e39 1462 plist = plist->next;
d6846af6
LF
1463
1464 rtw_free_xmitframe(pxmitpriv, pxmitframe);
1465 }
e02bcf61 1466 spin_unlock_bh(&(pframequeue->lock));
d6846af6 1467
d6846af6
LF
1468}
1469
1470s32 rtw_xmitframe_enqueue(struct adapter *padapter, struct xmit_frame *pxmitframe)
1471{
1472 if (rtw_xmit_classifier(padapter, pxmitframe) == _FAIL) {
1473 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_,
1474 ("rtw_xmitframe_enqueue: drop xmit pkt for classifier fail\n"));
1475/* pxmitframe->pkt = NULL; */
1476 return _FAIL;
1477 }
1478
1479 return _SUCCESS;
1480}
1481
1482static struct xmit_frame *dequeue_one_xmitframe(struct xmit_priv *pxmitpriv, struct hw_xmit *phwxmit, struct tx_servq *ptxservq, struct __queue *pframe_queue)
1483{
1484 struct list_head *xmitframe_plist, *xmitframe_phead;
1485 struct xmit_frame *pxmitframe = NULL;
1486
1487 xmitframe_phead = get_list_head(pframe_queue);
c44e5e39 1488 xmitframe_plist = xmitframe_phead->next;
d6846af6 1489
84660700 1490 if (xmitframe_phead != xmitframe_plist) {
bea88100 1491 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
d6846af6 1492
c44e5e39 1493 xmitframe_plist = xmitframe_plist->next;
d6846af6
LF
1494
1495 rtw_list_delete(&pxmitframe->list);
1496
1497 ptxservq->qcnt--;
d6846af6 1498 }
d6846af6
LF
1499 return pxmitframe;
1500}
1501
1502struct xmit_frame *rtw_dequeue_xframe(struct xmit_priv *pxmitpriv, struct hw_xmit *phwxmit_i, int entry)
1503{
d6846af6
LF
1504 struct list_head *sta_plist, *sta_phead;
1505 struct hw_xmit *phwxmit;
1506 struct tx_servq *ptxservq = NULL;
1507 struct __queue *pframe_queue = NULL;
1508 struct xmit_frame *pxmitframe = NULL;
1509 struct adapter *padapter = pxmitpriv->adapter;
1510 struct registry_priv *pregpriv = &padapter->registrypriv;
1511 int i, inx[4];
1512
d6846af6
LF
1513
1514 inx[0] = 0; inx[1] = 1; inx[2] = 2; inx[3] = 3;
1515
1516 if (pregpriv->wifi_spec == 1) {
1517 int j;
1518
1519 for (j = 0; j < 4; j++)
1520 inx[j] = pxmitpriv->wmm_para_seq[j];
1521 }
1522
7057dcb3 1523 spin_lock_bh(&pxmitpriv->lock);
d6846af6
LF
1524
1525 for (i = 0; i < entry; i++) {
1526 phwxmit = phwxmit_i + inx[i];
1527
1528 sta_phead = get_list_head(phwxmit->sta_queue);
c44e5e39 1529 sta_plist = sta_phead->next;
d6846af6 1530
84660700 1531 while (sta_phead != sta_plist) {
bea88100 1532 ptxservq = container_of(sta_plist, struct tx_servq, tx_pending);
d6846af6
LF
1533
1534 pframe_queue = &ptxservq->sta_pending;
1535
1536 pxmitframe = dequeue_one_xmitframe(pxmitpriv, phwxmit, ptxservq, pframe_queue);
1537
1538 if (pxmitframe) {
1539 phwxmit->accnt--;
1540
1541 /* Remove sta node when there are no pending packets. */
1542 if (_rtw_queue_empty(pframe_queue)) /* must be done after get_next and before break */
1543 rtw_list_delete(&ptxservq->tx_pending);
1544 goto exit;
1545 }
1546
c44e5e39 1547 sta_plist = sta_plist->next;
d6846af6
LF
1548 }
1549 }
1550exit:
e02bcf61 1551 spin_unlock_bh(&pxmitpriv->lock);
d6846af6
LF
1552 return pxmitframe;
1553}
1554
1555struct tx_servq *rtw_get_sta_pending(struct adapter *padapter, struct sta_info *psta, int up, u8 *ac)
1556{
1557 struct tx_servq *ptxservq;
1558
d6846af6
LF
1559 switch (up) {
1560 case 1:
1561 case 2:
1562 ptxservq = &(psta->sta_xmitpriv.bk_q);
1563 *(ac) = 3;
1564 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_get_sta_pending : BK\n"));
1565 break;
1566 case 4:
1567 case 5:
1568 ptxservq = &(psta->sta_xmitpriv.vi_q);
1569 *(ac) = 1;
1570 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_get_sta_pending : VI\n"));
1571 break;
1572 case 6:
1573 case 7:
1574 ptxservq = &(psta->sta_xmitpriv.vo_q);
1575 *(ac) = 0;
1576 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_get_sta_pending : VO\n"));
1577 break;
1578 case 0:
1579 case 3:
1580 default:
1581 ptxservq = &(psta->sta_xmitpriv.be_q);
1582 *(ac) = 2;
1583 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_get_sta_pending : BE\n"));
1584 break;
1585 }
1586
d6846af6
LF
1587
1588 return ptxservq;
1589}
1590
1591/*
1592 * Will enqueue pxmitframe to the proper queue,
1593 * and indicate it to xx_pending list.....
1594 */
1595s32 rtw_xmit_classifier(struct adapter *padapter, struct xmit_frame *pxmitframe)
1596{
d6846af6
LF
1597 u8 ac_index;
1598 struct sta_info *psta;
1599 struct tx_servq *ptxservq;
1600 struct pkt_attrib *pattrib = &pxmitframe->attrib;
1601 struct sta_priv *pstapriv = &padapter->stapriv;
1602 struct hw_xmit *phwxmits = padapter->xmitpriv.hwxmits;
1603 int res = _SUCCESS;
1604
d6846af6
LF
1605
1606 if (pattrib->psta) {
1607 psta = pattrib->psta;
1608 } else {
1609 psta = rtw_get_stainfo(pstapriv, pattrib->ra);
1610 }
1611
1612 if (psta == NULL) {
1613 res = _FAIL;
1614 DBG_88E("rtw_xmit_classifier: psta == NULL\n");
1615 RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("rtw_xmit_classifier: psta == NULL\n"));
1616 goto exit;
1617 }
1618
1619 ptxservq = rtw_get_sta_pending(padapter, psta, pattrib->priority, (u8 *)(&ac_index));
1620
9c4b0e70 1621 if (list_empty(&ptxservq->tx_pending))
ae6787ad 1622 list_add_tail(&ptxservq->tx_pending, get_list_head(phwxmits[ac_index].sta_queue));
d6846af6 1623
ae6787ad 1624 list_add_tail(&pxmitframe->list, get_list_head(&ptxservq->sta_pending));
d6846af6
LF
1625 ptxservq->qcnt++;
1626 phwxmits[ac_index].accnt++;
1627exit:
1628
d6846af6
LF
1629
1630 return res;
1631}
1632
1633void rtw_alloc_hwxmits(struct adapter *padapter)
1634{
1635 struct hw_xmit *hwxmits;
1636 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1637
1638 pxmitpriv->hwxmit_entry = HWXMIT_ENTRY;
1639
1640 pxmitpriv->hwxmits = (struct hw_xmit *)rtw_zmalloc(sizeof(struct hw_xmit) * pxmitpriv->hwxmit_entry);
1641
1642 hwxmits = pxmitpriv->hwxmits;
1643
1644 if (pxmitpriv->hwxmit_entry == 5) {
1645 hwxmits[0] .sta_queue = &pxmitpriv->bm_pending;
1646 hwxmits[1] .sta_queue = &pxmitpriv->vo_pending;
1647 hwxmits[2] .sta_queue = &pxmitpriv->vi_pending;
1648 hwxmits[3] .sta_queue = &pxmitpriv->bk_pending;
1649 hwxmits[4] .sta_queue = &pxmitpriv->be_pending;
1650 } else if (pxmitpriv->hwxmit_entry == 4) {
1651 hwxmits[0] .sta_queue = &pxmitpriv->vo_pending;
1652 hwxmits[1] .sta_queue = &pxmitpriv->vi_pending;
1653 hwxmits[2] .sta_queue = &pxmitpriv->be_pending;
1654 hwxmits[3] .sta_queue = &pxmitpriv->bk_pending;
1655 } else {
1656 }
1657}
1658
1659void rtw_free_hwxmits(struct adapter *padapter)
1660{
1661 struct hw_xmit *hwxmits;
1662 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1663
1664 hwxmits = pxmitpriv->hwxmits;
1665 kfree(hwxmits);
1666}
1667
1668void rtw_init_hwxmits(struct hw_xmit *phwxmit, int entry)
1669{
1670 int i;
d6846af6
LF
1671 for (i = 0; i < entry; i++, phwxmit++)
1672 phwxmit->accnt = 0;
d6846af6
LF
1673}
1674
d6846af6
LF
1675u32 rtw_get_ff_hwaddr(struct xmit_frame *pxmitframe)
1676{
1677 u32 addr;
1678 struct pkt_attrib *pattrib = &pxmitframe->attrib;
1679
1680 switch (pattrib->qsel) {
1681 case 0:
1682 case 3:
1683 addr = BE_QUEUE_INX;
1684 break;
1685 case 1:
1686 case 2:
1687 addr = BK_QUEUE_INX;
1688 break;
1689 case 4:
1690 case 5:
1691 addr = VI_QUEUE_INX;
1692 break;
1693 case 6:
1694 case 7:
1695 addr = VO_QUEUE_INX;
1696 break;
1697 case 0x10:
1698 addr = BCN_QUEUE_INX;
1699 break;
1700 case 0x11:/* BC/MC in PS (HIQ) */
1701 addr = HIGH_QUEUE_INX;
1702 break;
1703 case 0x12:
1704 default:
1705 addr = MGT_QUEUE_INX;
1706 break;
1707 }
1708
1709 return addr;
1710}
1711
1712static void do_queue_select(struct adapter *padapter, struct pkt_attrib *pattrib)
1713{
1714 u8 qsel;
1715
1716 qsel = pattrib->priority;
1717 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("### do_queue_select priority=%d , qsel = %d\n", pattrib->priority , qsel));
1718
1719 pattrib->qsel = qsel;
1720}
1721
1722/*
1723 * The main transmit(tx) entry
1724 *
1725 * Return
1726 * 1 enqueue
1727 * 0 success, hardware will handle this xmit frame(packet)
1728 * <0 fail
1729 */
1730s32 rtw_xmit(struct adapter *padapter, struct sk_buff **ppkt)
1731{
d6846af6
LF
1732 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1733 struct xmit_frame *pxmitframe = NULL;
d6846af6
LF
1734 s32 res;
1735
1736 pxmitframe = rtw_alloc_xmitframe(pxmitpriv);
1737 if (pxmitframe == NULL) {
1738 RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit: no more pxmitframe\n"));
1739 DBG_88E("DBG_TX_DROP_FRAME %s no more pxmitframe\n", __func__);
1740 return -1;
1741 }
1742
d6846af6
LF
1743 res = update_attrib(padapter, *ppkt, &pxmitframe->attrib);
1744
1745 if (res == _FAIL) {
1746 RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit: update attrib fail\n"));
1747 rtw_free_xmitframe(pxmitpriv, pxmitframe);
1748 return -1;
1749 }
1750 pxmitframe->pkt = *ppkt;
1751
1752 rtw_led_control(padapter, LED_CTL_TX);
1753
1754 do_queue_select(padapter, &pxmitframe->attrib);
1755
1756#ifdef CONFIG_88EU_AP_MODE
7057dcb3 1757 spin_lock_bh(&pxmitpriv->lock);
d6846af6 1758 if (xmitframe_enqueue_for_sleeping_sta(padapter, pxmitframe)) {
e02bcf61 1759 spin_unlock_bh(&pxmitpriv->lock);
d6846af6
LF
1760 return 1;
1761 }
e02bcf61 1762 spin_unlock_bh(&pxmitpriv->lock);
d6846af6
LF
1763#endif
1764
1765 if (rtw_hal_xmit(padapter, pxmitframe) == false)
1766 return 1;
1767
1768 return 0;
1769}
1770
1771#if defined(CONFIG_88EU_AP_MODE)
1772
1773int xmitframe_enqueue_for_sleeping_sta(struct adapter *padapter, struct xmit_frame *pxmitframe)
1774{
d6846af6
LF
1775 int ret = false;
1776 struct sta_info *psta = NULL;
1777 struct sta_priv *pstapriv = &padapter->stapriv;
1778 struct pkt_attrib *pattrib = &pxmitframe->attrib;
1779 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1780 int bmcst = IS_MCAST(pattrib->ra);
1781
1782 if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == false)
1783 return ret;
1784
1785 if (pattrib->psta)
1786 psta = pattrib->psta;
1787 else
1788 psta = rtw_get_stainfo(pstapriv, pattrib->ra);
1789
1790 if (psta == NULL)
1791 return ret;
1792
1793 if (pattrib->triggered == 1) {
1794 if (bmcst)
1795 pattrib->qsel = 0x11;/* HIQ */
1796 return ret;
1797 }
1798
1799 if (bmcst) {
7057dcb3 1800 spin_lock_bh(&psta->sleep_q.lock);
d6846af6
LF
1801
1802 if (pstapriv->sta_dz_bitmap) {/* if any one sta is in ps mode */
1803 rtw_list_delete(&pxmitframe->list);
1804
ae6787ad 1805 list_add_tail(&pxmitframe->list, get_list_head(&psta->sleep_q));
d6846af6
LF
1806
1807 psta->sleepq_len++;
1808
1809 pstapriv->tim_bitmap |= BIT(0);/* */
1810 pstapriv->sta_dz_bitmap |= BIT(0);
1811
40a46d8b 1812 update_beacon(padapter, _TIM_IE_, NULL, false);/* tx bc/mc packets after update bcn */
d6846af6
LF
1813
1814 ret = true;
1815 }
1816
e02bcf61 1817 spin_unlock_bh(&psta->sleep_q.lock);
d6846af6
LF
1818
1819 return ret;
1820 }
1821
7057dcb3 1822 spin_lock_bh(&psta->sleep_q.lock);
d6846af6
LF
1823
1824 if (psta->state&WIFI_SLEEP_STATE) {
1825 u8 wmmps_ac = 0;
1826
1827 if (pstapriv->sta_dz_bitmap&BIT(psta->aid)) {
1828 rtw_list_delete(&pxmitframe->list);
1829
ae6787ad 1830 list_add_tail(&pxmitframe->list, get_list_head(&psta->sleep_q));
d6846af6
LF
1831
1832 psta->sleepq_len++;
1833
1834 switch (pattrib->priority) {
1835 case 1:
1836 case 2:
1837 wmmps_ac = psta->uapsd_bk&BIT(0);
1838 break;
1839 case 4:
1840 case 5:
1841 wmmps_ac = psta->uapsd_vi&BIT(0);
1842 break;
1843 case 6:
1844 case 7:
1845 wmmps_ac = psta->uapsd_vo&BIT(0);
1846 break;
1847 case 0:
1848 case 3:
1849 default:
1850 wmmps_ac = psta->uapsd_be&BIT(0);
1851 break;
1852 }
1853
1854 if (wmmps_ac)
1855 psta->sleepq_ac_len++;
1856
1857 if (((psta->has_legacy_ac) && (!wmmps_ac)) ||
1858 ((!psta->has_legacy_ac) && (wmmps_ac))) {
1859 pstapriv->tim_bitmap |= BIT(psta->aid);
1860
1861 if (psta->sleepq_len == 1) {
40a46d8b 1862 /* update BCN for TIM IE */
d6846af6
LF
1863 update_beacon(padapter, _TIM_IE_, NULL, false);
1864 }
1865 }
1866 ret = true;
1867 }
1868 }
1869
e02bcf61 1870 spin_unlock_bh(&psta->sleep_q.lock);
d6846af6
LF
1871
1872 return ret;
1873}
1874
1875static void dequeue_xmitframes_to_sleeping_queue(struct adapter *padapter, struct sta_info *psta, struct __queue *pframequeue)
1876{
1877 struct list_head *plist, *phead;
1878 u8 ac_index;
1879 struct tx_servq *ptxservq;
1880 struct pkt_attrib *pattrib;
1881 struct xmit_frame *pxmitframe;
1882 struct hw_xmit *phwxmits = padapter->xmitpriv.hwxmits;
1883
1884 phead = get_list_head(pframequeue);
c44e5e39 1885 plist = phead->next;
d6846af6 1886
84660700 1887 while (phead != plist) {
bea88100 1888 pxmitframe = container_of(plist, struct xmit_frame, list);
d6846af6 1889
c44e5e39 1890 plist = plist->next;
d6846af6
LF
1891
1892 xmitframe_enqueue_for_sleeping_sta(padapter, pxmitframe);
1893
1894 pattrib = &pxmitframe->attrib;
1895
1896 ptxservq = rtw_get_sta_pending(padapter, psta, pattrib->priority, (u8 *)(&ac_index));
1897
1898 ptxservq->qcnt--;
1899 phwxmits[ac_index].accnt--;
1900 }
1901}
1902
1903void stop_sta_xmit(struct adapter *padapter, struct sta_info *psta)
1904{
d6846af6
LF
1905 struct sta_info *psta_bmc;
1906 struct sta_xmit_priv *pstaxmitpriv;
1907 struct sta_priv *pstapriv = &padapter->stapriv;
1908 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1909
1910 pstaxmitpriv = &psta->sta_xmitpriv;
1911
1912 /* for BC/MC Frames */
1913 psta_bmc = rtw_get_bcmc_stainfo(padapter);
1914
7057dcb3 1915 spin_lock_bh(&pxmitpriv->lock);
d6846af6
LF
1916
1917 psta->state |= WIFI_SLEEP_STATE;
1918
1919 pstapriv->sta_dz_bitmap |= BIT(psta->aid);
1920
1921 dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->vo_q.sta_pending);
1922 rtw_list_delete(&(pstaxmitpriv->vo_q.tx_pending));
1923
1924 dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->vi_q.sta_pending);
1925 rtw_list_delete(&(pstaxmitpriv->vi_q.tx_pending));
1926
1927 dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->be_q.sta_pending);
1928 rtw_list_delete(&(pstaxmitpriv->be_q.tx_pending));
1929
1930 dequeue_xmitframes_to_sleeping_queue(padapter, psta, &pstaxmitpriv->bk_q.sta_pending);
1931 rtw_list_delete(&(pstaxmitpriv->bk_q.tx_pending));
1932
1933 /* for BC/MC Frames */
1934 pstaxmitpriv = &psta_bmc->sta_xmitpriv;
1935 dequeue_xmitframes_to_sleeping_queue(padapter, psta_bmc, &pstaxmitpriv->be_q.sta_pending);
1936 rtw_list_delete(&(pstaxmitpriv->be_q.tx_pending));
1937
e02bcf61 1938 spin_unlock_bh(&pxmitpriv->lock);
d6846af6
LF
1939}
1940
1941void wakeup_sta_to_xmit(struct adapter *padapter, struct sta_info *psta)
1942{
d6846af6
LF
1943 u8 update_mask = 0, wmmps_ac = 0;
1944 struct sta_info *psta_bmc;
1945 struct list_head *xmitframe_plist, *xmitframe_phead;
1946 struct xmit_frame *pxmitframe = NULL;
1947 struct sta_priv *pstapriv = &padapter->stapriv;
1948
7057dcb3 1949 spin_lock_bh(&psta->sleep_q.lock);
d6846af6
LF
1950
1951 xmitframe_phead = get_list_head(&psta->sleep_q);
c44e5e39 1952 xmitframe_plist = xmitframe_phead->next;
d6846af6 1953
84660700 1954 while (xmitframe_phead != xmitframe_plist) {
bea88100 1955 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
d6846af6 1956
c44e5e39 1957 xmitframe_plist = xmitframe_plist->next;
d6846af6
LF
1958
1959 rtw_list_delete(&pxmitframe->list);
1960
1961 switch (pxmitframe->attrib.priority) {
1962 case 1:
1963 case 2:
1964 wmmps_ac = psta->uapsd_bk&BIT(1);
1965 break;
1966 case 4:
1967 case 5:
1968 wmmps_ac = psta->uapsd_vi&BIT(1);
1969 break;
1970 case 6:
1971 case 7:
1972 wmmps_ac = psta->uapsd_vo&BIT(1);
1973 break;
1974 case 0:
1975 case 3:
1976 default:
1977 wmmps_ac = psta->uapsd_be&BIT(1);
1978 break;
1979 }
1980
1981 psta->sleepq_len--;
1982 if (psta->sleepq_len > 0)
1983 pxmitframe->attrib.mdata = 1;
1984 else
1985 pxmitframe->attrib.mdata = 0;
1986
1987 if (wmmps_ac) {
1988 psta->sleepq_ac_len--;
1989 if (psta->sleepq_ac_len > 0) {
1990 pxmitframe->attrib.mdata = 1;
1991 pxmitframe->attrib.eosp = 0;
1992 } else {
1993 pxmitframe->attrib.mdata = 0;
1994 pxmitframe->attrib.eosp = 1;
1995 }
1996 }
1997
1998 pxmitframe->attrib.triggered = 1;
1999
e02bcf61 2000 spin_unlock_bh(&psta->sleep_q.lock);
d6846af6
LF
2001 if (rtw_hal_xmit(padapter, pxmitframe))
2002 rtw_os_xmit_complete(padapter, pxmitframe);
7057dcb3 2003 spin_lock_bh(&psta->sleep_q.lock);
d6846af6
LF
2004 }
2005
2006 if (psta->sleepq_len == 0) {
2007 pstapriv->tim_bitmap &= ~BIT(psta->aid);
2008
2009 update_mask = BIT(0);
2010
2011 if (psta->state&WIFI_SLEEP_STATE)
2012 psta->state ^= WIFI_SLEEP_STATE;
2013
2014 if (psta->state & WIFI_STA_ALIVE_CHK_STATE) {
2015 psta->expire_to = pstapriv->expire_to;
2016 psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
2017 }
2018
2019 pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
2020 }
2021
e02bcf61 2022 spin_unlock_bh(&psta->sleep_q.lock);
d6846af6
LF
2023
2024 /* for BC/MC Frames */
2025 psta_bmc = rtw_get_bcmc_stainfo(padapter);
2026 if (!psta_bmc)
2027 return;
2028
2029 if ((pstapriv->sta_dz_bitmap&0xfffe) == 0x0) { /* no any sta in ps mode */
7057dcb3 2030 spin_lock_bh(&psta_bmc->sleep_q.lock);
d6846af6
LF
2031
2032 xmitframe_phead = get_list_head(&psta_bmc->sleep_q);
c44e5e39 2033 xmitframe_plist = xmitframe_phead->next;
d6846af6 2034
84660700 2035 while (xmitframe_phead != xmitframe_plist) {
bea88100 2036 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
d6846af6 2037
c44e5e39 2038 xmitframe_plist = xmitframe_plist->next;
d6846af6
LF
2039
2040 rtw_list_delete(&pxmitframe->list);
2041
2042 psta_bmc->sleepq_len--;
2043 if (psta_bmc->sleepq_len > 0)
2044 pxmitframe->attrib.mdata = 1;
2045 else
2046 pxmitframe->attrib.mdata = 0;
2047
2048 pxmitframe->attrib.triggered = 1;
2049
e02bcf61 2050 spin_unlock_bh(&psta_bmc->sleep_q.lock);
d6846af6
LF
2051 if (rtw_hal_xmit(padapter, pxmitframe))
2052 rtw_os_xmit_complete(padapter, pxmitframe);
7057dcb3 2053 spin_lock_bh(&psta_bmc->sleep_q.lock);
d6846af6
LF
2054 }
2055
2056 if (psta_bmc->sleepq_len == 0) {
2057 pstapriv->tim_bitmap &= ~BIT(0);
2058 pstapriv->sta_dz_bitmap &= ~BIT(0);
2059
2060 update_mask |= BIT(1);
2061 }
2062
e02bcf61 2063 spin_unlock_bh(&psta_bmc->sleep_q.lock);
d6846af6
LF
2064 }
2065
2066 if (update_mask)
2067 update_beacon(padapter, _TIM_IE_, NULL, false);
2068}
2069
2070void xmit_delivery_enabled_frames(struct adapter *padapter, struct sta_info *psta)
2071{
d6846af6
LF
2072 u8 wmmps_ac = 0;
2073 struct list_head *xmitframe_plist, *xmitframe_phead;
2074 struct xmit_frame *pxmitframe = NULL;
2075 struct sta_priv *pstapriv = &padapter->stapriv;
2076
7057dcb3 2077 spin_lock_bh(&psta->sleep_q.lock);
d6846af6
LF
2078
2079 xmitframe_phead = get_list_head(&psta->sleep_q);
c44e5e39 2080 xmitframe_plist = xmitframe_phead->next;
d6846af6 2081
84660700 2082 while (xmitframe_phead != xmitframe_plist) {
bea88100 2083 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
d6846af6 2084
c44e5e39 2085 xmitframe_plist = xmitframe_plist->next;
d6846af6
LF
2086
2087 switch (pxmitframe->attrib.priority) {
2088 case 1:
2089 case 2:
2090 wmmps_ac = psta->uapsd_bk&BIT(1);
2091 break;
2092 case 4:
2093 case 5:
2094 wmmps_ac = psta->uapsd_vi&BIT(1);
2095 break;
2096 case 6:
2097 case 7:
2098 wmmps_ac = psta->uapsd_vo&BIT(1);
2099 break;
2100 case 0:
2101 case 3:
2102 default:
2103 wmmps_ac = psta->uapsd_be&BIT(1);
2104 break;
2105 }
2106
2107 if (!wmmps_ac)
2108 continue;
2109
2110 rtw_list_delete(&pxmitframe->list);
2111
2112 psta->sleepq_len--;
2113 psta->sleepq_ac_len--;
2114
2115 if (psta->sleepq_ac_len > 0) {
2116 pxmitframe->attrib.mdata = 1;
2117 pxmitframe->attrib.eosp = 0;
2118 } else {
2119 pxmitframe->attrib.mdata = 0;
2120 pxmitframe->attrib.eosp = 1;
2121 }
2122
2123 pxmitframe->attrib.triggered = 1;
2124
2125 if (rtw_hal_xmit(padapter, pxmitframe) == true)
2126 rtw_os_xmit_complete(padapter, pxmitframe);
2127
2128 if ((psta->sleepq_ac_len == 0) && (!psta->has_legacy_ac) && (wmmps_ac)) {
2129 pstapriv->tim_bitmap &= ~BIT(psta->aid);
2130
40a46d8b 2131 /* update BCN for TIM IE */
d6846af6
LF
2132 update_beacon(padapter, _TIM_IE_, NULL, false);
2133 }
2134 }
2135
e02bcf61 2136 spin_unlock_bh(&psta->sleep_q.lock);
d6846af6
LF
2137}
2138
2139#endif
2140
2141void rtw_sctx_init(struct submit_ctx *sctx, int timeout_ms)
2142{
2143 sctx->timeout_ms = timeout_ms;
c01fb496 2144 sctx->submit_time = jiffies;
d6846af6
LF
2145 init_completion(&sctx->done);
2146 sctx->status = RTW_SCTX_SUBMITTED;
2147}
2148
2149int rtw_sctx_wait(struct submit_ctx *sctx)
2150{
2151 int ret = _FAIL;
2152 unsigned long expire;
2153 int status = 0;
2154
2155 expire = sctx->timeout_ms ? msecs_to_jiffies(sctx->timeout_ms) : MAX_SCHEDULE_TIMEOUT;
2156 if (!wait_for_completion_timeout(&sctx->done, expire)) {
2157 /* timeout, do something?? */
2158 status = RTW_SCTX_DONE_TIMEOUT;
2159 DBG_88E("%s timeout\n", __func__);
2160 } else {
2161 status = sctx->status;
2162 }
2163
2164 if (status == RTW_SCTX_DONE_SUCCESS)
2165 ret = _SUCCESS;
2166
2167 return ret;
2168}
2169
2170static bool rtw_sctx_chk_waring_status(int status)
2171{
2172 switch (status) {
2173 case RTW_SCTX_DONE_UNKNOWN:
2174 case RTW_SCTX_DONE_BUF_ALLOC:
2175 case RTW_SCTX_DONE_BUF_FREE:
2176
2177 case RTW_SCTX_DONE_DRV_STOP:
2178 case RTW_SCTX_DONE_DEV_REMOVE:
2179 return true;
2180 default:
2181 return false;
2182 }
2183}
2184
2185void rtw_sctx_done_err(struct submit_ctx **sctx, int status)
2186{
2187 if (*sctx) {
2188 if (rtw_sctx_chk_waring_status(status))
2189 DBG_88E("%s status:%d\n", __func__, status);
2190 (*sctx)->status = status;
2191 complete(&((*sctx)->done));
2192 *sctx = NULL;
2193 }
2194}
2195
2196void rtw_sctx_done(struct submit_ctx **sctx)
2197{
2198 rtw_sctx_done_err(sctx, RTW_SCTX_DONE_SUCCESS);
2199}
2200
2201int rtw_ack_tx_wait(struct xmit_priv *pxmitpriv, u32 timeout_ms)
2202{
2203 struct submit_ctx *pack_tx_ops = &pxmitpriv->ack_tx_ops;
2204
c01fb496 2205 pack_tx_ops->submit_time = jiffies;
d6846af6
LF
2206 pack_tx_ops->timeout_ms = timeout_ms;
2207 pack_tx_ops->status = RTW_SCTX_SUBMITTED;
2208
2209 return rtw_sctx_wait(pack_tx_ops);
2210}
2211
2212void rtw_ack_tx_done(struct xmit_priv *pxmitpriv, int status)
2213{
2214 struct submit_ctx *pack_tx_ops = &pxmitpriv->ack_tx_ops;
2215
2216 if (pxmitpriv->ack_tx)
2217 rtw_sctx_done_err(&pack_tx_ops, status);
2218 else
2219 DBG_88E("%s ack_tx not set\n", __func__);
2220}