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