[go: nahoru, domu]

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