1/******************************************************************************
2 * rtl871x_xmit.c
3 *
4 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
5 * Linux device driver for RTL8192SU
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19 *
20 * Modifications for inclusion into the Linux staging tree are
21 * Copyright(c) 2010 Larry Finger. All rights reserved.
22 *
23 * Contact information:
24 * WLAN FAE <wlanfae@realtek.com>
25 * Larry Finger <Larry.Finger@lwfinger.net>
26 *
27 ******************************************************************************/
28
29#define _RTL871X_XMIT_C_
30
31#include "osdep_service.h"
32#include "drv_types.h"
33#include "wifi.h"
34#include "osdep_intf.h"
35#include "usb_ops.h"
36
37
38static const u8 P802_1H_OUI[P80211_OUI_LEN] = {0x00, 0x00, 0xf8};
39static const u8 RFC1042_OUI[P80211_OUI_LEN] = {0x00, 0x00, 0x00};
40static void init_hwxmits(struct hw_xmit *phwxmit, sint entry);
41static void alloc_hwxmits(struct _adapter *padapter);
42static void free_hwxmits(struct _adapter *padapter);
43
44static void _init_txservq(struct tx_servq *ptxservq)
45{
46	INIT_LIST_HEAD(&ptxservq->tx_pending);
47	_init_queue(&ptxservq->sta_pending);
48	ptxservq->qcnt = 0;
49}
50
51void _r8712_init_sta_xmit_priv(struct sta_xmit_priv *psta_xmitpriv)
52{
53	memset((unsigned char *)psta_xmitpriv, 0,
54		 sizeof(struct sta_xmit_priv));
55	spin_lock_init(&psta_xmitpriv->lock);
56	_init_txservq(&psta_xmitpriv->be_q);
57	_init_txservq(&psta_xmitpriv->bk_q);
58	_init_txservq(&psta_xmitpriv->vi_q);
59	_init_txservq(&psta_xmitpriv->vo_q);
60	INIT_LIST_HEAD(&psta_xmitpriv->legacy_dz);
61	INIT_LIST_HEAD(&psta_xmitpriv->apsd);
62}
63
64sint _r8712_init_xmit_priv(struct xmit_priv *pxmitpriv,
65			   struct _adapter *padapter)
66{
67	sint i;
68	struct xmit_buf *pxmitbuf;
69	struct xmit_frame *pxframe;
70
71	memset((unsigned char *)pxmitpriv, 0, sizeof(struct xmit_priv));
72	spin_lock_init(&pxmitpriv->lock);
73	/*
74	Please insert all the queue initialization using _init_queue below
75	*/
76	pxmitpriv->adapter = padapter;
77	_init_queue(&pxmitpriv->be_pending);
78	_init_queue(&pxmitpriv->bk_pending);
79	_init_queue(&pxmitpriv->vi_pending);
80	_init_queue(&pxmitpriv->vo_pending);
81	_init_queue(&pxmitpriv->bm_pending);
82	_init_queue(&pxmitpriv->legacy_dz_queue);
83	_init_queue(&pxmitpriv->apsd_queue);
84	_init_queue(&pxmitpriv->free_xmit_queue);
85	/*
86	Please allocate memory with the sz = (struct xmit_frame) * NR_XMITFRAME,
87	and initialize free_xmit_frame below.
88	Please also apply  free_txobj to link_up all the xmit_frames...
89	*/
90	pxmitpriv->pallocated_frame_buf = kmalloc(NR_XMITFRAME * sizeof(struct xmit_frame) + 4,
91						  GFP_ATOMIC);
92	if (pxmitpriv->pallocated_frame_buf == NULL) {
93		pxmitpriv->pxmit_frame_buf = NULL;
94		return _FAIL;
95	}
96	pxmitpriv->pxmit_frame_buf = pxmitpriv->pallocated_frame_buf + 4 -
97			((addr_t) (pxmitpriv->pallocated_frame_buf) & 3);
98	pxframe = (struct xmit_frame *) pxmitpriv->pxmit_frame_buf;
99	for (i = 0; i < NR_XMITFRAME; i++) {
100		INIT_LIST_HEAD(&(pxframe->list));
101		pxframe->padapter = padapter;
102		pxframe->frame_tag = DATA_FRAMETAG;
103		pxframe->pkt = NULL;
104		pxframe->buf_addr = NULL;
105		pxframe->pxmitbuf = NULL;
106		list_add_tail(&(pxframe->list),
107				 &(pxmitpriv->free_xmit_queue.queue));
108		pxframe++;
109	}
110	pxmitpriv->free_xmitframe_cnt = NR_XMITFRAME;
111	/*
112		init xmit hw_txqueue
113	*/
114	_r8712_init_hw_txqueue(&pxmitpriv->be_txqueue, BE_QUEUE_INX);
115	_r8712_init_hw_txqueue(&pxmitpriv->bk_txqueue, BK_QUEUE_INX);
116	_r8712_init_hw_txqueue(&pxmitpriv->vi_txqueue, VI_QUEUE_INX);
117	_r8712_init_hw_txqueue(&pxmitpriv->vo_txqueue, VO_QUEUE_INX);
118	_r8712_init_hw_txqueue(&pxmitpriv->bmc_txqueue, BMC_QUEUE_INX);
119	pxmitpriv->frag_len = MAX_FRAG_THRESHOLD;
120	pxmitpriv->txirp_cnt = 1;
121	/*per AC pending irp*/
122	pxmitpriv->beq_cnt = 0;
123	pxmitpriv->bkq_cnt = 0;
124	pxmitpriv->viq_cnt = 0;
125	pxmitpriv->voq_cnt = 0;
126	/*init xmit_buf*/
127	_init_queue(&pxmitpriv->free_xmitbuf_queue);
128	_init_queue(&pxmitpriv->pending_xmitbuf_queue);
129	pxmitpriv->pallocated_xmitbuf = kmalloc(NR_XMITBUFF * sizeof(struct xmit_buf) + 4,
130						GFP_ATOMIC);
131	if (pxmitpriv->pallocated_xmitbuf  == NULL)
132		return _FAIL;
133	pxmitpriv->pxmitbuf = pxmitpriv->pallocated_xmitbuf + 4 -
134			      ((addr_t)(pxmitpriv->pallocated_xmitbuf) & 3);
135	pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
136	for (i = 0; i < NR_XMITBUFF; i++) {
137		INIT_LIST_HEAD(&pxmitbuf->list);
138		pxmitbuf->pallocated_buf = kmalloc(MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ,
139						   GFP_ATOMIC);
140		if (pxmitbuf->pallocated_buf == NULL)
141			return _FAIL;
142		pxmitbuf->pbuf = pxmitbuf->pallocated_buf + XMITBUF_ALIGN_SZ -
143				 ((addr_t) (pxmitbuf->pallocated_buf) &
144				 (XMITBUF_ALIGN_SZ - 1));
145		r8712_xmit_resource_alloc(padapter, pxmitbuf);
146		list_add_tail(&pxmitbuf->list,
147				 &(pxmitpriv->free_xmitbuf_queue.queue));
148		pxmitbuf++;
149	}
150	pxmitpriv->free_xmitbuf_cnt = NR_XMITBUFF;
151	INIT_WORK(&padapter->wkFilterRxFF0, r8712_SetFilter);
152	alloc_hwxmits(padapter);
153	init_hwxmits(pxmitpriv->hwxmits, pxmitpriv->hwxmit_entry);
154	tasklet_init(&pxmitpriv->xmit_tasklet,
155		(void(*)(unsigned long))r8712_xmit_bh,
156		(unsigned long)padapter);
157	return _SUCCESS;
158}
159
160void _free_xmit_priv(struct xmit_priv *pxmitpriv)
161{
162	int i;
163	struct _adapter *padapter = pxmitpriv->adapter;
164	struct xmit_frame *pxmitframe = (struct xmit_frame *)
165					pxmitpriv->pxmit_frame_buf;
166	struct xmit_buf *pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
167
168	if (pxmitpriv->pxmit_frame_buf == NULL)
169		return;
170	for (i = 0; i < NR_XMITFRAME; i++) {
171		r8712_xmit_complete(padapter, pxmitframe);
172		pxmitframe++;
173	}
174	for (i = 0; i < NR_XMITBUFF; i++) {
175		r8712_xmit_resource_free(padapter, pxmitbuf);
176		kfree(pxmitbuf->pallocated_buf);
177		pxmitbuf++;
178	}
179	kfree(pxmitpriv->pallocated_frame_buf);
180	kfree(pxmitpriv->pallocated_xmitbuf);
181	free_hwxmits(padapter);
182}
183
184sint r8712_update_attrib(struct _adapter *padapter, _pkt *pkt,
185		   struct pkt_attrib *pattrib)
186{
187	struct pkt_file pktfile;
188	struct sta_info *psta = NULL;
189	struct ethhdr etherhdr;
190
191	struct tx_cmd txdesc;
192
193	sint bmcast;
194	struct sta_priv		*pstapriv = &padapter->stapriv;
195	struct security_priv	*psecuritypriv = &padapter->securitypriv;
196	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
197	struct qos_priv		*pqospriv = &pmlmepriv->qospriv;
198
199	_r8712_open_pktfile(pkt, &pktfile);
200
201	_r8712_pktfile_read(&pktfile, (unsigned char *)&etherhdr, ETH_HLEN);
202
203	pattrib->ether_type = ntohs(etherhdr.h_proto);
204
205{
206	/*If driver xmit ARP packet, driver can set ps mode to initial
207	 * setting. It stands for getting DHCP or fix IP.*/
208	if (pattrib->ether_type == 0x0806) {
209		if (padapter->pwrctrlpriv.pwr_mode !=
210		    padapter->registrypriv.power_mgnt) {
211			del_timer_sync(&pmlmepriv->dhcp_timer);
212			r8712_set_ps_mode(padapter, padapter->registrypriv.
213				power_mgnt, padapter->registrypriv.smart_ps);
214		}
215	}
216}
217	memcpy(pattrib->dst, &etherhdr.h_dest, ETH_ALEN);
218	memcpy(pattrib->src, &etherhdr.h_source, ETH_ALEN);
219	pattrib->pctrl = 0;
220	if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true) ||
221	    (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true)) {
222		memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
223		memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
224	} else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
225		memcpy(pattrib->ra, get_bssid(pmlmepriv), ETH_ALEN);
226		memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
227	} else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
228		memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
229		memcpy(pattrib->ta, get_bssid(pmlmepriv), ETH_ALEN);
230	} else if (check_fwstate(pmlmepriv, WIFI_MP_STATE) == true) {
231		/*firstly, filter packet not belongs to mp*/
232		if (pattrib->ether_type != 0x8712)
233			return _FAIL;
234		/* for mp storing the txcmd per packet,
235		 * according to the info of txcmd to update pattrib */
236		/*get MP_TXDESC_SIZE bytes txcmd per packet*/
237		_r8712_pktfile_read(&pktfile, (u8 *)&txdesc, TXDESC_SIZE);
238		memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
239		memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
240		pattrib->pctrl = 1;
241	}
242	/* r8712_xmitframe_coalesce() overwrite this!*/
243	pattrib->pktlen = pktfile.pkt_len;
244	if (ETH_P_IP == pattrib->ether_type) {
245		/* The following is for DHCP and ARP packet, we use cck1M to
246		 * tx these packets and let LPS awake some time
247		 * to prevent DHCP protocol fail */
248		u8 tmp[24];
249
250		_r8712_pktfile_read(&pktfile, &tmp[0], 24);
251		pattrib->dhcp_pkt = 0;
252		if (pktfile.pkt_len > 282) {/*MINIMUM_DHCP_PACKET_SIZE)*/
253			if (ETH_P_IP == pattrib->ether_type) {/* IP header*/
254				if (((tmp[21] == 68) && (tmp[23] == 67)) ||
255					((tmp[21] == 67) && (tmp[23] == 68))) {
256					/* 68 : UDP BOOTP client
257					 * 67 : UDP BOOTP server
258					 * Use low rate to send DHCP packet.*/
259					pattrib->dhcp_pkt = 1;
260				}
261			}
262		}
263	}
264	bmcast = IS_MCAST(pattrib->ra);
265	/* get sta_info*/
266	if (bmcast) {
267		psta = r8712_get_bcmc_stainfo(padapter);
268		pattrib->mac_id = 4;
269	} else {
270		if (check_fwstate(pmlmepriv, WIFI_MP_STATE) == true) {
271			psta = r8712_get_stainfo(pstapriv,
272						 get_bssid(pmlmepriv));
273			pattrib->mac_id = 5;
274		} else {
275			psta = r8712_get_stainfo(pstapriv, pattrib->ra);
276			if (psta == NULL)  /* drop the pkt */
277				return _FAIL;
278			if (check_fwstate(pmlmepriv, WIFI_STATION_STATE))
279				pattrib->mac_id = 5;
280			else
281				pattrib->mac_id = psta->mac_id;
282		}
283	}
284
285	if (psta) {
286		pattrib->psta = psta;
287	} else {
288		/* if we cannot get psta => drrp the pkt */
289		return _FAIL;
290	}
291
292	pattrib->ack_policy = 0;
293	/* get ether_hdr_len */
294	pattrib->pkt_hdrlen = ETH_HLEN;
295
296	if (pqospriv->qos_option)
297		r8712_set_qos(&pktfile, pattrib);
298	else {
299		pattrib->hdrlen = WLAN_HDR_A3_LEN;
300		pattrib->subtype = WIFI_DATA_TYPE;
301		pattrib->priority = 0;
302	}
303	if (psta->ieee8021x_blocked == true) {
304		pattrib->encrypt = 0;
305		if ((pattrib->ether_type != 0x888e) &&
306		    (check_fwstate(pmlmepriv, WIFI_MP_STATE) == false))
307			return _FAIL;
308	} else
309		GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt, bmcast);
310	switch (pattrib->encrypt) {
311	case _WEP40_:
312	case _WEP104_:
313		pattrib->iv_len = 4;
314		pattrib->icv_len = 4;
315		break;
316	case _TKIP_:
317		pattrib->iv_len = 8;
318		pattrib->icv_len = 4;
319		if (padapter->securitypriv.busetkipkey == _FAIL)
320			return _FAIL;
321		break;
322	case _AES_:
323		pattrib->iv_len = 8;
324		pattrib->icv_len = 8;
325		break;
326	default:
327		pattrib->iv_len = 0;
328		pattrib->icv_len = 0;
329		break;
330	}
331
332	if (pattrib->encrypt &&
333	    ((padapter->securitypriv.sw_encrypt == true) ||
334	     (psecuritypriv->hw_decrypted == false)))
335		pattrib->bswenc = true;
336	else
337		pattrib->bswenc = false;
338	/* if in MP_STATE, update pkt_attrib from mp_txcmd, and overwrite
339	 * some settings above.*/
340	if (check_fwstate(pmlmepriv, WIFI_MP_STATE) == true)
341		pattrib->priority = (txdesc.txdw1 >> QSEL_SHT) & 0x1f;
342	return _SUCCESS;
343}
344
345static sint xmitframe_addmic(struct _adapter *padapter,
346			     struct xmit_frame *pxmitframe)
347{
348	u32	curfragnum, length;
349	u8	*pframe, *payload, mic[8];
350	struct	mic_data micdata;
351	struct	sta_info *stainfo;
352	struct	qos_priv *pqospriv = &(padapter->mlmepriv.qospriv);
353	struct	pkt_attrib  *pattrib = &pxmitframe->attrib;
354	struct	security_priv *psecuritypriv = &padapter->securitypriv;
355	struct	xmit_priv *pxmitpriv = &padapter->xmitpriv;
356	u8 priority[4] = {0x0, 0x0, 0x0, 0x0};
357	sint bmcst = IS_MCAST(pattrib->ra);
358
359	if (pattrib->psta)
360		stainfo = pattrib->psta;
361	else
362		stainfo = r8712_get_stainfo(&padapter->stapriv,
363					    &pattrib->ra[0]);
364	if (pattrib->encrypt == _TKIP_) {
365		/*encode mic code*/
366		if (stainfo != NULL) {
367			u8 null_key[16] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
368					   0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
369					   0x0, 0x0};
370			pframe = pxmitframe->buf_addr + TXDESC_OFFSET;
371			if (bmcst) {
372				if (!memcmp(psecuritypriv->XGrptxmickey
373				   [psecuritypriv->XGrpKeyid].skey,
374				   null_key, 16))
375					return _FAIL;
376				/*start to calculate the mic code*/
377				r8712_secmicsetkey(&micdata,
378					 psecuritypriv->
379					 XGrptxmickey[psecuritypriv->
380					XGrpKeyid].skey);
381			} else {
382				if (!memcmp(&stainfo->tkiptxmickey.skey[0],
383					    null_key, 16))
384					return _FAIL;
385				/* start to calculate the mic code */
386				r8712_secmicsetkey(&micdata,
387					     &stainfo->tkiptxmickey.skey[0]);
388			}
389			if (pframe[1] & 1) {   /* ToDS==1 */
390				r8712_secmicappend(&micdata,
391						   &pframe[16], 6); /*DA*/
392				if (pframe[1]&2)  /* From Ds==1 */
393					r8712_secmicappend(&micdata,
394							   &pframe[24], 6);
395				else
396					r8712_secmicappend(&micdata,
397							   &pframe[10], 6);
398			} else {	/* ToDS==0 */
399				r8712_secmicappend(&micdata,
400						   &pframe[4], 6); /* DA */
401				if (pframe[1]&2)  /* From Ds==1 */
402					r8712_secmicappend(&micdata,
403							   &pframe[16], 6);
404				else
405					r8712_secmicappend(&micdata,
406							   &pframe[10], 6);
407			}
408			if (pqospriv->qos_option == 1)
409					priority[0] = (u8)pxmitframe->
410						      attrib.priority;
411			r8712_secmicappend(&micdata, &priority[0], 4);
412			payload = pframe;
413			for (curfragnum = 0; curfragnum < pattrib->nr_frags;
414			     curfragnum++) {
415				payload = (u8 *)RND4((addr_t)(payload));
416				payload = payload+pattrib->
417					  hdrlen+pattrib->iv_len;
418				if ((curfragnum + 1) == pattrib->nr_frags) {
419					length = pattrib->last_txcmdsz -
420						  pattrib->hdrlen -
421						  pattrib->iv_len -
422						  ((psecuritypriv->sw_encrypt)
423						  ? pattrib->icv_len : 0);
424					r8712_secmicappend(&micdata, payload,
425							   length);
426					payload = payload+length;
427				} else{
428					length = pxmitpriv->frag_len -
429					    pattrib->hdrlen-pattrib->iv_len -
430					    ((psecuritypriv->sw_encrypt) ?
431					    pattrib->icv_len : 0);
432					r8712_secmicappend(&micdata, payload,
433							   length);
434					payload = payload + length +
435						  pattrib->icv_len;
436				}
437			}
438			r8712_secgetmic(&micdata, &(mic[0]));
439			/* add mic code  and add the mic code length in
440			 * last_txcmdsz */
441			memcpy(payload, &(mic[0]), 8);
442			pattrib->last_txcmdsz += 8;
443			payload = payload-pattrib->last_txcmdsz + 8;
444		}
445	}
446	return _SUCCESS;
447}
448
449static sint xmitframe_swencrypt(struct _adapter *padapter,
450				struct xmit_frame *pxmitframe)
451{
452	struct pkt_attrib	*pattrib = &pxmitframe->attrib;
453
454	if (pattrib->bswenc) {
455		switch (pattrib->encrypt) {
456		case _WEP40_:
457		case _WEP104_:
458			r8712_wep_encrypt(padapter, (u8 *)pxmitframe);
459			break;
460		case _TKIP_:
461			r8712_tkip_encrypt(padapter, (u8 *)pxmitframe);
462			break;
463		case _AES_:
464			r8712_aes_encrypt(padapter, (u8 *)pxmitframe);
465			break;
466		default:
467				break;
468		}
469	}
470	return _SUCCESS;
471}
472
473static sint make_wlanhdr(struct _adapter *padapter, u8 *hdr,
474			 struct pkt_attrib *pattrib)
475{
476	u16 *qc;
477
478	struct ieee80211_hdr *pwlanhdr = (struct ieee80211_hdr *)hdr;
479	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
480	struct qos_priv *pqospriv = &pmlmepriv->qospriv;
481	u16 *fctrl = &pwlanhdr->frame_ctl;
482
483	memset(hdr, 0, WLANHDR_OFFSET);
484	SetFrameSubType(fctrl, pattrib->subtype);
485	if (pattrib->subtype & WIFI_DATA_TYPE) {
486		if (check_fwstate(pmlmepriv,  WIFI_STATION_STATE) == true) {
487			/* to_ds = 1, fr_ds = 0; */
488			SetToDs(fctrl);
489			memcpy(pwlanhdr->addr1, get_bssid(pmlmepriv),
490				ETH_ALEN);
491			memcpy(pwlanhdr->addr2, pattrib->src, ETH_ALEN);
492			memcpy(pwlanhdr->addr3, pattrib->dst, ETH_ALEN);
493		} else if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
494			/* to_ds = 0, fr_ds = 1; */
495			SetFrDs(fctrl);
496			memcpy(pwlanhdr->addr1, pattrib->dst, ETH_ALEN);
497			memcpy(pwlanhdr->addr2, get_bssid(pmlmepriv),
498				ETH_ALEN);
499			memcpy(pwlanhdr->addr3, pattrib->src, ETH_ALEN);
500		} else if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true)
501			   || (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)
502			   == true)) {
503			memcpy(pwlanhdr->addr1, pattrib->dst, ETH_ALEN);
504			memcpy(pwlanhdr->addr2, pattrib->src, ETH_ALEN);
505			memcpy(pwlanhdr->addr3, get_bssid(pmlmepriv),
506				ETH_ALEN);
507		} else if (check_fwstate(pmlmepriv, WIFI_MP_STATE) == true) {
508			memcpy(pwlanhdr->addr1, pattrib->dst, ETH_ALEN);
509			memcpy(pwlanhdr->addr2, pattrib->src, ETH_ALEN);
510			memcpy(pwlanhdr->addr3, get_bssid(pmlmepriv),
511				ETH_ALEN);
512		} else
513			return _FAIL;
514
515		if (pattrib->encrypt)
516			SetPrivacy(fctrl);
517		if (pqospriv->qos_option) {
518			qc = (unsigned short *)(hdr + pattrib->hdrlen - 2);
519			if (pattrib->priority)
520				SetPriority(qc, pattrib->priority);
521			SetAckpolicy(qc, pattrib->ack_policy);
522		}
523		/* TODO: fill HT Control Field */
524		/* Update Seq Num will be handled by f/w */
525		{
526			struct sta_info *psta;
527			sint bmcst = IS_MCAST(pattrib->ra);
528
529			if (pattrib->psta)
530				psta = pattrib->psta;
531			else {
532				if (bmcst)
533					psta = r8712_get_bcmc_stainfo(padapter);
534				else
535					psta =
536					 r8712_get_stainfo(&padapter->stapriv,
537					 pattrib->ra);
538			}
539			if (psta) {
540				psta->sta_xmitpriv.txseq_tid
541						  [pattrib->priority]++;
542				psta->sta_xmitpriv.txseq_tid[pattrib->priority]
543						   &= 0xFFF;
544				pattrib->seqnum = psta->sta_xmitpriv.
545						  txseq_tid[pattrib->priority];
546				SetSeqNum(hdr, pattrib->seqnum);
547			}
548		}
549	}
550	return _SUCCESS;
551}
552
553static sint r8712_put_snap(u8 *data, u16 h_proto)
554{
555	struct ieee80211_snap_hdr *snap;
556	const u8 *oui;
557
558	snap = (struct ieee80211_snap_hdr *)data;
559	snap->dsap = 0xaa;
560	snap->ssap = 0xaa;
561	snap->ctrl = 0x03;
562	if (h_proto == 0x8137 || h_proto == 0x80f3)
563		oui = P802_1H_OUI;
564	else
565		oui = RFC1042_OUI;
566	snap->oui[0] = oui[0];
567	snap->oui[1] = oui[1];
568	snap->oui[2] = oui[2];
569	*(u16 *)(data + SNAP_SIZE) = htons(h_proto);
570	return SNAP_SIZE + sizeof(u16);
571}
572
573/*
574 * This sub-routine will perform all the following:
575 * 1. remove 802.3 header.
576 * 2. create wlan_header, based on the info in pxmitframe
577 * 3. append sta's iv/ext-iv
578 * 4. append LLC
579 * 5. move frag chunk from pframe to pxmitframe->mem
580 * 6. apply sw-encrypt, if necessary.
581 */
582sint r8712_xmitframe_coalesce(struct _adapter *padapter, _pkt *pkt,
583			struct xmit_frame *pxmitframe)
584{
585	struct pkt_file pktfile;
586
587	sint	frg_len, mpdu_len, llc_sz;
588	u32	mem_sz;
589	u8	frg_inx;
590	addr_t addr;
591	u8 *pframe, *mem_start, *ptxdesc;
592	struct sta_info		*psta;
593	struct security_priv	*psecuritypriv = &padapter->securitypriv;
594	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
595	struct xmit_priv	*pxmitpriv = &padapter->xmitpriv;
596	struct pkt_attrib	*pattrib = &pxmitframe->attrib;
597	u8 *pbuf_start;
598	sint bmcst = IS_MCAST(pattrib->ra);
599
600	if (pattrib->psta == NULL)
601		return _FAIL;
602	psta = pattrib->psta;
603	if (pxmitframe->buf_addr == NULL)
604		return _FAIL;
605	pbuf_start = pxmitframe->buf_addr;
606	ptxdesc = pbuf_start;
607	mem_start = pbuf_start + TXDESC_OFFSET;
608	if (make_wlanhdr(padapter, mem_start, pattrib) == _FAIL)
609		return _FAIL;
610	_r8712_open_pktfile(pkt, &pktfile);
611	_r8712_pktfile_read(&pktfile, NULL, (uint) pattrib->pkt_hdrlen);
612	if (check_fwstate(pmlmepriv, WIFI_MP_STATE) == true) {
613		/* truncate TXDESC_SIZE bytes txcmd if at mp mode for 871x */
614		if (pattrib->ether_type == 0x8712) {
615			/* take care -  update_txdesc overwrite this */
616			_r8712_pktfile_read(&pktfile, ptxdesc, TXDESC_SIZE);
617		}
618	}
619	pattrib->pktlen = pktfile.pkt_len;
620	frg_inx = 0;
621	frg_len = pxmitpriv->frag_len - 4;
622	while (1) {
623		llc_sz = 0;
624		mpdu_len = frg_len;
625		pframe = mem_start;
626		SetMFrag(mem_start);
627		pframe += pattrib->hdrlen;
628		mpdu_len -= pattrib->hdrlen;
629		/* adding icv, if necessary...*/
630		if (pattrib->iv_len) {
631			if (psta != NULL) {
632				switch (pattrib->encrypt) {
633				case _WEP40_:
634				case _WEP104_:
635					WEP_IV(pattrib->iv, psta->txpn,
636					       (u8)psecuritypriv->
637					       PrivacyKeyIndex);
638					break;
639				case _TKIP_:
640					if (bmcst)
641						TKIP_IV(pattrib->iv,
642						    psta->txpn,
643						    (u8)psecuritypriv->
644						    XGrpKeyid);
645					else
646						TKIP_IV(pattrib->iv, psta->txpn,
647							0);
648					break;
649				case _AES_:
650					if (bmcst)
651						AES_IV(pattrib->iv, psta->txpn,
652						    (u8)psecuritypriv->
653						    XGrpKeyid);
654					else
655						AES_IV(pattrib->iv, psta->txpn,
656						       0);
657					break;
658				}
659			}
660			memcpy(pframe, pattrib->iv, pattrib->iv_len);
661			pframe += pattrib->iv_len;
662			mpdu_len -= pattrib->iv_len;
663		}
664		if (frg_inx == 0) {
665			llc_sz = r8712_put_snap(pframe, pattrib->ether_type);
666			pframe += llc_sz;
667			mpdu_len -= llc_sz;
668		}
669		if ((pattrib->icv_len > 0) && (pattrib->bswenc))
670			mpdu_len -= pattrib->icv_len;
671		if (bmcst)
672			mem_sz = _r8712_pktfile_read(&pktfile, pframe,
673				 pattrib->pktlen);
674		else
675			mem_sz = _r8712_pktfile_read(&pktfile, pframe,
676				 mpdu_len);
677		pframe += mem_sz;
678		if ((pattrib->icv_len > 0) && (pattrib->bswenc)) {
679			memcpy(pframe, pattrib->icv, pattrib->icv_len);
680			pframe += pattrib->icv_len;
681		}
682		frg_inx++;
683		if (bmcst || (r8712_endofpktfile(&pktfile) == true)) {
684			pattrib->nr_frags = frg_inx;
685			pattrib->last_txcmdsz = pattrib->hdrlen +
686						pattrib->iv_len +
687						((pattrib->nr_frags == 1) ?
688						llc_sz : 0) +
689						((pattrib->bswenc) ?
690						pattrib->icv_len : 0) + mem_sz;
691			ClearMFrag(mem_start);
692			break;
693		}
694		addr = (addr_t)(pframe);
695		mem_start = (unsigned char *)RND4(addr) + TXDESC_OFFSET;
696		memcpy(mem_start, pbuf_start + TXDESC_OFFSET, pattrib->hdrlen);
697	}
698
699	if (xmitframe_addmic(padapter, pxmitframe) == _FAIL)
700		return _FAIL;
701	xmitframe_swencrypt(padapter, pxmitframe);
702	return _SUCCESS;
703}
704
705void r8712_update_protection(struct _adapter *padapter, u8 *ie, uint ie_len)
706{
707	uint	protection;
708	u8	*perp;
709	sint	 erp_len;
710	struct	xmit_priv *pxmitpriv = &padapter->xmitpriv;
711	struct	registry_priv *pregistrypriv = &padapter->registrypriv;
712
713	switch (pxmitpriv->vcs_setting) {
714	case DISABLE_VCS:
715		pxmitpriv->vcs = NONE_VCS;
716		break;
717	case ENABLE_VCS:
718		break;
719	case AUTO_VCS:
720	default:
721		perp = r8712_get_ie(ie, _ERPINFO_IE_, &erp_len, ie_len);
722		if (perp == NULL)
723			pxmitpriv->vcs = NONE_VCS;
724		else {
725			protection = (*(perp + 2)) & BIT(1);
726			if (protection) {
727				if (pregistrypriv->vcs_type == RTS_CTS)
728					pxmitpriv->vcs = RTS_CTS;
729				else
730					pxmitpriv->vcs = CTS_TO_SELF;
731			} else
732				pxmitpriv->vcs = NONE_VCS;
733		}
734		break;
735	}
736}
737
738struct xmit_buf *r8712_alloc_xmitbuf(struct xmit_priv *pxmitpriv)
739{
740	unsigned long irqL;
741	struct xmit_buf *pxmitbuf =  NULL;
742	struct list_head *plist, *phead;
743	struct  __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
744
745	spin_lock_irqsave(&pfree_xmitbuf_queue->lock, irqL);
746	if (list_empty(&pfree_xmitbuf_queue->queue))
747		pxmitbuf = NULL;
748	else {
749		phead = &pfree_xmitbuf_queue->queue;
750		plist = phead->next;
751		pxmitbuf = LIST_CONTAINOR(plist, struct xmit_buf, list);
752		list_del_init(&(pxmitbuf->list));
753	}
754	if (pxmitbuf !=  NULL)
755		pxmitpriv->free_xmitbuf_cnt--;
756	spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, irqL);
757	return pxmitbuf;
758}
759
760int r8712_free_xmitbuf(struct xmit_priv *pxmitpriv, struct xmit_buf *pxmitbuf)
761{
762	unsigned long irqL;
763	struct  __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
764
765	if (pxmitbuf == NULL)
766		return _FAIL;
767	spin_lock_irqsave(&pfree_xmitbuf_queue->lock, irqL);
768	list_del_init(&pxmitbuf->list);
769	list_add_tail(&(pxmitbuf->list), &pfree_xmitbuf_queue->queue);
770	pxmitpriv->free_xmitbuf_cnt++;
771	spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, irqL);
772	return _SUCCESS;
773}
774
775/*
776Calling context:
7771. OS_TXENTRY
7782. RXENTRY (rx_thread or RX_ISR/RX_CallBack)
779
780If we turn on USE_RXTHREAD, then, no need for critical section.
781Otherwise, we must use _enter/_exit critical to protect free_xmit_queue...
782
783Must be very very cautious...
784
785*/
786
787struct xmit_frame *r8712_alloc_xmitframe(struct xmit_priv *pxmitpriv)
788{
789	/*
790		Please remember to use all the osdep_service api,
791		and lock/unlock or _enter/_exit critical to protect
792		pfree_xmit_queue
793	*/
794	unsigned long irqL;
795	struct xmit_frame *pxframe = NULL;
796	struct list_head *plist, *phead;
797	struct  __queue *pfree_xmit_queue = &pxmitpriv->free_xmit_queue;
798
799	spin_lock_irqsave(&pfree_xmit_queue->lock, irqL);
800	if (list_empty(&pfree_xmit_queue->queue))
801		pxframe =  NULL;
802	else {
803		phead = &pfree_xmit_queue->queue;
804		plist = phead->next;
805		pxframe = LIST_CONTAINOR(plist, struct xmit_frame, list);
806		list_del_init(&(pxframe->list));
807	}
808	if (pxframe !=  NULL) {
809		pxmitpriv->free_xmitframe_cnt--;
810		pxframe->buf_addr = NULL;
811		pxframe->pxmitbuf = NULL;
812		pxframe->attrib.psta = NULL;
813		pxframe->pkt = NULL;
814	}
815	spin_unlock_irqrestore(&pfree_xmit_queue->lock, irqL);
816	return pxframe;
817}
818
819void r8712_free_xmitframe(struct xmit_priv *pxmitpriv,
820			  struct xmit_frame *pxmitframe)
821{
822	unsigned long irqL;
823	struct  __queue *pfree_xmit_queue = &pxmitpriv->free_xmit_queue;
824	struct _adapter *padapter = pxmitpriv->adapter;
825
826	if (pxmitframe == NULL)
827		return;
828	spin_lock_irqsave(&pfree_xmit_queue->lock, irqL);
829	list_del_init(&pxmitframe->list);
830	if (pxmitframe->pkt)
831		pxmitframe->pkt = NULL;
832	list_add_tail(&pxmitframe->list, &pfree_xmit_queue->queue);
833	pxmitpriv->free_xmitframe_cnt++;
834	spin_unlock_irqrestore(&pfree_xmit_queue->lock, irqL);
835	if (netif_queue_stopped(padapter->pnetdev))
836		netif_wake_queue(padapter->pnetdev);
837}
838
839void r8712_free_xmitframe_ex(struct xmit_priv *pxmitpriv,
840		      struct xmit_frame *pxmitframe)
841{
842	if (pxmitframe == NULL)
843		return;
844	if (pxmitframe->frame_tag == DATA_FRAMETAG)
845		r8712_free_xmitframe(pxmitpriv, pxmitframe);
846}
847
848void r8712_free_xmitframe_queue(struct xmit_priv *pxmitpriv,
849				struct  __queue *pframequeue)
850{
851	unsigned long irqL;
852	struct list_head *plist, *phead;
853	struct	xmit_frame	*pxmitframe;
854
855	spin_lock_irqsave(&(pframequeue->lock), irqL);
856	phead = &pframequeue->queue;
857	plist = phead->next;
858	while (end_of_queue_search(phead, plist) == false) {
859		pxmitframe = LIST_CONTAINOR(plist, struct xmit_frame, list);
860		plist = plist->next;
861		r8712_free_xmitframe(pxmitpriv, pxmitframe);
862	}
863	spin_unlock_irqrestore(&(pframequeue->lock), irqL);
864}
865
866static inline struct tx_servq *get_sta_pending(struct _adapter *padapter,
867					       struct  __queue **ppstapending,
868					       struct sta_info *psta, sint up)
869{
870
871	struct tx_servq *ptxservq;
872	struct hw_xmit *phwxmits =  padapter->xmitpriv.hwxmits;
873
874	switch (up) {
875	case 1:
876	case 2:
877		ptxservq = &(psta->sta_xmitpriv.bk_q);
878		*ppstapending = &padapter->xmitpriv.bk_pending;
879		(phwxmits+3)->accnt++;
880		break;
881	case 4:
882	case 5:
883		ptxservq = &(psta->sta_xmitpriv.vi_q);
884		*ppstapending = &padapter->xmitpriv.vi_pending;
885		(phwxmits+1)->accnt++;
886		break;
887	case 6:
888	case 7:
889		ptxservq = &(psta->sta_xmitpriv.vo_q);
890		*ppstapending = &padapter->xmitpriv.vo_pending;
891		(phwxmits+0)->accnt++;
892		break;
893	case 0:
894	case 3:
895	default:
896		ptxservq = &(psta->sta_xmitpriv.be_q);
897		*ppstapending = &padapter->xmitpriv.be_pending;
898		(phwxmits + 2)->accnt++;
899		break;
900	}
901	return ptxservq;
902}
903
904/*
905 * Will enqueue pxmitframe to the proper queue, and indicate it
906 * to xx_pending list.....
907 */
908sint r8712_xmit_classifier(struct _adapter *padapter,
909			   struct xmit_frame *pxmitframe)
910{
911	unsigned long irqL0;
912	struct  __queue *pstapending;
913	struct sta_info	*psta;
914	struct tx_servq	*ptxservq;
915	struct pkt_attrib *pattrib = &pxmitframe->attrib;
916	struct sta_priv *pstapriv = &padapter->stapriv;
917	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
918	sint bmcst = IS_MCAST(pattrib->ra);
919
920	if (pattrib->psta)
921		psta = pattrib->psta;
922	else {
923		if (bmcst)
924			psta = r8712_get_bcmc_stainfo(padapter);
925		else {
926			if (check_fwstate(pmlmepriv, WIFI_MP_STATE) == true)
927				psta = r8712_get_stainfo(pstapriv,
928				       get_bssid(pmlmepriv));
929			else
930				psta = r8712_get_stainfo(pstapriv, pattrib->ra);
931		}
932	}
933	if (psta == NULL)
934		return _FAIL;
935	ptxservq = get_sta_pending(padapter, &pstapending,
936		   psta, pattrib->priority);
937	spin_lock_irqsave(&pstapending->lock, irqL0);
938	if (list_empty(&ptxservq->tx_pending))
939		list_add_tail(&ptxservq->tx_pending, &pstapending->queue);
940	list_add_tail(&pxmitframe->list, &ptxservq->sta_pending.queue);
941	ptxservq->qcnt++;
942	spin_unlock_irqrestore(&pstapending->lock, irqL0);
943	return _SUCCESS;
944}
945
946static void alloc_hwxmits(struct _adapter *padapter)
947{
948	struct hw_xmit *hwxmits;
949	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
950
951	pxmitpriv->hwxmit_entry = HWXMIT_ENTRY;
952	pxmitpriv->hwxmits = kmalloc_array(pxmitpriv->hwxmit_entry,
953				sizeof(struct hw_xmit), GFP_ATOMIC);
954	if (pxmitpriv->hwxmits == NULL)
955		return;
956	hwxmits = pxmitpriv->hwxmits;
957	if (pxmitpriv->hwxmit_entry == 5) {
958		pxmitpriv->bmc_txqueue.head = 0;
959		hwxmits[0] .phwtxqueue = &pxmitpriv->bmc_txqueue;
960		hwxmits[0] .sta_queue = &pxmitpriv->bm_pending;
961		pxmitpriv->vo_txqueue.head = 0;
962		hwxmits[1] .phwtxqueue = &pxmitpriv->vo_txqueue;
963		hwxmits[1] .sta_queue = &pxmitpriv->vo_pending;
964	pxmitpriv->vi_txqueue.head = 0;
965		hwxmits[2] .phwtxqueue = &pxmitpriv->vi_txqueue;
966		hwxmits[2] .sta_queue = &pxmitpriv->vi_pending;
967		pxmitpriv->bk_txqueue.head = 0;
968		hwxmits[3] .phwtxqueue = &pxmitpriv->bk_txqueue;
969		hwxmits[3] .sta_queue = &pxmitpriv->bk_pending;
970		pxmitpriv->be_txqueue.head = 0;
971		hwxmits[4] .phwtxqueue = &pxmitpriv->be_txqueue;
972		hwxmits[4] .sta_queue = &pxmitpriv->be_pending;
973	} else if (pxmitpriv->hwxmit_entry == 4) {
974		pxmitpriv->vo_txqueue.head = 0;
975		hwxmits[0] .phwtxqueue = &pxmitpriv->vo_txqueue;
976		hwxmits[0] .sta_queue = &pxmitpriv->vo_pending;
977		pxmitpriv->vi_txqueue.head = 0;
978		hwxmits[1] .phwtxqueue = &pxmitpriv->vi_txqueue;
979		hwxmits[1] .sta_queue = &pxmitpriv->vi_pending;
980		pxmitpriv->be_txqueue.head = 0;
981		hwxmits[2] .phwtxqueue = &pxmitpriv->be_txqueue;
982		hwxmits[2] .sta_queue = &pxmitpriv->be_pending;
983		pxmitpriv->bk_txqueue.head = 0;
984		hwxmits[3] .phwtxqueue = &pxmitpriv->bk_txqueue;
985		hwxmits[3] .sta_queue = &pxmitpriv->bk_pending;
986	}
987}
988
989static void free_hwxmits(struct _adapter *padapter)
990{
991	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
992
993	kfree(pxmitpriv->hwxmits);
994}
995
996static void init_hwxmits(struct hw_xmit *phwxmit, sint entry)
997{
998	sint i;
999
1000	for (i = 0; i < entry; i++, phwxmit++) {
1001		spin_lock_init(&phwxmit->xmit_lock);
1002		INIT_LIST_HEAD(&phwxmit->pending);
1003		phwxmit->txcmdcnt = 0;
1004		phwxmit->accnt = 0;
1005	}
1006}
1007
1008void xmitframe_xmitbuf_attach(struct xmit_frame *pxmitframe,
1009			struct xmit_buf *pxmitbuf)
1010{
1011	/* pxmitbuf attach to pxmitframe */
1012	pxmitframe->pxmitbuf = pxmitbuf;
1013	/* urb and irp connection */
1014	pxmitframe->pxmit_urb[0] = pxmitbuf->pxmit_urb[0];
1015	/* buffer addr assoc */
1016	pxmitframe->buf_addr = pxmitbuf->pbuf;
1017	/* pxmitframe attach to pxmitbuf */
1018	pxmitbuf->priv_data = pxmitframe;
1019}
1020
1021/*
1022 * tx_action == 0 == no frames to transmit
1023 * tx_action > 0 ==> we have frames to transmit
1024 * tx_action < 0 ==> we have frames to transmit, but TXFF is not even enough
1025 *						 to transmit 1 frame.
1026 */
1027
1028int r8712_pre_xmit(struct _adapter *padapter, struct xmit_frame *pxmitframe)
1029{
1030	unsigned long irqL;
1031	int ret;
1032	struct xmit_buf *pxmitbuf = NULL;
1033	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1034	struct pkt_attrib *pattrib = &pxmitframe->attrib;
1035
1036	r8712_do_queue_select(padapter, pattrib);
1037	spin_lock_irqsave(&pxmitpriv->lock, irqL);
1038	if (r8712_txframes_sta_ac_pending(padapter, pattrib) > 0) {
1039		ret = false;
1040		r8712_xmit_enqueue(padapter, pxmitframe);
1041		spin_unlock_irqrestore(&pxmitpriv->lock, irqL);
1042		return ret;
1043	}
1044	pxmitbuf = r8712_alloc_xmitbuf(pxmitpriv);
1045	if (pxmitbuf == NULL) { /*enqueue packet*/
1046		ret = false;
1047		r8712_xmit_enqueue(padapter, pxmitframe);
1048		spin_unlock_irqrestore(&pxmitpriv->lock, irqL);
1049	} else { /*dump packet directly*/
1050		spin_unlock_irqrestore(&pxmitpriv->lock, irqL);
1051		ret = true;
1052		xmitframe_xmitbuf_attach(pxmitframe, pxmitbuf);
1053		r8712_xmit_direct(padapter, pxmitframe);
1054	}
1055	return ret;
1056}
1057