aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/rtl8723au/hal/rtl8723au_xmit.c
blob: 1759487329ab70905561d2aee96d37319b916431 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/******************************************************************************
 *
 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 ******************************************************************************/
#define _RTL8192C_XMIT_C_
#include <osdep_service.h>
#include <drv_types.h>
#include <wifi.h>
#include <osdep_intf.h>
#include <usb_ops.h>
/* include <rtl8192c_hal.h> */
#include <rtl8723a_hal.h>

static int urb_zero_packet_chk(struct rtw_adapter *padapter, int sz)
{
	int blnSetTxDescOffset;
	struct dvobj_priv	*pdvobj = adapter_to_dvobj(padapter);

	if (pdvobj->ishighspeed) {
		if (((sz + TXDESC_SIZE) % 512) == 0)
			blnSetTxDescOffset = 1;
		else
			blnSetTxDescOffset = 0;
	} else {
		if (((sz + TXDESC_SIZE) % 64) == 0)
			blnSetTxDescOffset = 1;
		else
			blnSetTxDescOffset = 0;
	}
	return blnSetTxDescOffset;
}

static void rtl8192cu_cal_txdesc_chksum(struct tx_desc	*ptxdesc)
{
		u16	*usPtr = (u16 *)ptxdesc;
		u32 count = 16;		/*  (32 bytes / 2 bytes per XOR) => 16 times */
		u32 index;
		u16 checksum = 0;

		/* Clear first */
		ptxdesc->txdw7 &= cpu_to_le32(0xffff0000);

		for (index = 0 ; index < count ; index++)
			checksum = checksum ^ le16_to_cpu(*(usPtr + index));

		ptxdesc->txdw7 |= cpu_to_le32(0x0000ffff&checksum);
}

static void fill_txdesc_sectype(struct pkt_attrib *pattrib, struct tx_desc *ptxdesc)
{
	if ((pattrib->encrypt > 0) && !pattrib->bswenc) {
		switch (pattrib->encrypt) {
		/* SEC_TYPE */
		case WLAN_CIPHER_SUITE_WEP40:
		case WLAN_CIPHER_SUITE_WEP104:
			ptxdesc->txdw1 |= cpu_to_le32((0x01<<22)&0x00c00000);
			break;
		case WLAN_CIPHER_SUITE_TKIP:
			/* ptxdesc->txdw1 |= cpu_to_le32((0x02<<22)&0x00c00000); */
			ptxdesc->txdw1 |= cpu_to_le32((0x01<<22)&0x00c00000);
			break;
		case WLAN_CIPHER_SUITE_CCMP:
			ptxdesc->txdw1 |= cpu_to_le32((0x03<<22)&0x00c00000);
			break;
		case 0:
		default:
			break;
		}
	}
}

static void fill_txdesc_vcs(struct pkt_attrib *pattrib, __le32 *pdw)
{
	/* DBG_8723A("cvs_mode =%d\n", pattrib->vcs_mode); */

	switch (pattrib->vcs_mode) {
	case RTS_CTS:
		*pdw |= cpu_to_le32(BIT(12));
		break;
	case CTS_TO_SELF:
		*pdw |= cpu_to_le32(BIT(11));
		break;
	case NONE_VCS:
	default:
		break;
	}

	if (pattrib->vcs_mode) {
		*pdw |= cpu_to_le32(BIT(13));

		/*  Set RTS BW */
		if (pattrib->ht_en) {
			*pdw |= (pattrib->bwmode&HT_CHANNEL_WIDTH_40) ?	cpu_to_le32(BIT(27)) : 0;

			if (pattrib->ch_offset == HAL_PRIME_CHNL_OFFSET_LOWER)
				*pdw |= cpu_to_le32((0x01<<28)&0x30000000);
			else if (pattrib->ch_offset == HAL_PRIME_CHNL_OFFSET_UPPER)
				*pdw |= cpu_to_le32((0x02<<28)&0x30000000);
			else if (pattrib->ch_offset == HAL_PRIME_CHNL_OFFSET_DONT_CARE)
				*pdw |= 0;
			else
				*pdw |= cpu_to_le32((0x03<<28)&0x30000000);
		}
	}
}

static void fill_txdesc_phy(struct pkt_attrib *pattrib, __le32 *pdw)
{
	if (pattrib->ht_en) {
		*pdw |= (pattrib->bwmode&HT_CHANNEL_WIDTH_40) ? cpu_to_le32(BIT(25)) : 0;

		if (pattrib->ch_offset == HAL_PRIME_CHNL_OFFSET_LOWER)
			*pdw |= cpu_to_le32((0x01<<20)&0x003f0000);
		else if (pattrib->ch_offset == HAL_PRIME_CHNL_OFFSET_UPPER)
			*pdw |= cpu_to_le32((0x02<<20)&0x003f0000);
		else if (pattrib->ch_offset == HAL_PRIME_CHNL_OFFSET_DONT_CARE)
			*pdw |= 0;
		else
			*pdw |= cpu_to_le32((0x03<<20)&0x003f0000);
	}
}

static s32 update_txdesc(struct xmit_frame *pxmitframe, u8 *pmem, s32 sz, u8 bagg_pkt)
{
	int	pull = 0;
	uint	qsel;
	struct rtw_adapter	*padapter = pxmitframe->padapter;
	struct pkt_attrib	*pattrib = &pxmitframe->attrib;
	struct hal_data_8723a	*pHalData = GET_HAL_DATA(padapter);
	struct dm_priv	*pdmpriv = &pHalData->dmpriv;
	struct tx_desc	*ptxdesc = (struct tx_desc *)pmem;
	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
	int	bmcst = is_multicast_ether_addr(pattrib->ra);

	if ((!bagg_pkt) && (urb_zero_packet_chk(padapter, sz) == 0)) {
		ptxdesc = (struct tx_desc *)(pmem+PACKET_OFFSET_SZ);
		pull = 1;
		pxmitframe->pkt_offset--;
	}

	memset(ptxdesc, 0, sizeof(struct tx_desc));

	if (pxmitframe->frame_tag == DATA_FRAMETAG) {
		/* offset 4 */
		ptxdesc->txdw1 |= cpu_to_le32(pattrib->mac_id&0x1f);

		qsel = (uint)(pattrib->qsel & 0x0000001f);
		ptxdesc->txdw1 |= cpu_to_le32((qsel << QSEL_SHT) & 0x00001f00);

		ptxdesc->txdw1 |= cpu_to_le32((pattrib->raid<<16) & 0x000f0000);

		fill_txdesc_sectype(pattrib, ptxdesc);

		if (pattrib->ampdu_en)
			ptxdesc->txdw1 |= cpu_to_le32(BIT(5));/* AGG EN */
		else
			ptxdesc->txdw1 |= cpu_to_le32(BIT(6));/* AGG BK */

		/* offset 8 */

		/* offset 12 */
		ptxdesc->txdw3 |= cpu_to_le32((pattrib->seqnum<<16)&0xffff0000);

		/* offset 16 , offset 20 */
		if (pattrib->qos_en)
			ptxdesc->txdw4 |= cpu_to_le32(BIT(6));/* QoS */

		if ((pattrib->ether_type != 0x888e) &&
		    (pattrib->ether_type != 0x0806) &&
		    (pattrib->dhcp_pkt != 1)) {
			/* Non EAP & ARP & DHCP type data packet */

			fill_txdesc_vcs(pattrib, &ptxdesc->txdw4);
			fill_txdesc_phy(pattrib, &ptxdesc->txdw4);

			ptxdesc->txdw4 |= cpu_to_le32(0x00000008);/* RTS Rate = 24M */
			ptxdesc->txdw5 |= cpu_to_le32(0x0001ff00);/*  */

			/* use REG_INIDATA_RATE_SEL value */
			ptxdesc->txdw5 |= cpu_to_le32(pdmpriv->INIDATA_RATE[pattrib->mac_id]);
		} else {
			/*  EAP data packet and ARP packet. */
			/*  Use the 1M data rate to send the EAP/ARP packet. */
			/*  This will maybe make the handshake smooth. */

			ptxdesc->txdw1 |= cpu_to_le32(BIT(6));/* AGG BK */

			ptxdesc->txdw4 |= cpu_to_le32(BIT(8));/* driver uses rate */

			if (pmlmeinfo->preamble_mode == PREAMBLE_SHORT)
				ptxdesc->txdw4 |= cpu_to_le32(BIT(24));/*  DATA_SHORT */

			ptxdesc->txdw5 |= cpu_to_le32(MRateToHwRate23a(pmlmeext->tx_rate));
		}
	} else if (pxmitframe->frame_tag == MGNT_FRAMETAG) {
		/* offset 4 */
		ptxdesc->txdw1 |= cpu_to_le32(pattrib->mac_id&0x1f);

		qsel = (uint)(pattrib->qsel&0x0000001f);
		ptxdesc->txdw1 |= cpu_to_le32((qsel<<QSEL_SHT)&0x00001f00);

		ptxdesc->txdw1 |= cpu_to_le32((pattrib->raid<<16) & 0x000f0000);

		/* offset 8 */
		/* CCX-TXRPT ack for xmit mgmt frames. */
		if (pxmitframe->ack_report)
			ptxdesc->txdw2 |= cpu_to_le32(BIT(19));

		/* offset 12 */
		ptxdesc->txdw3 |= cpu_to_le32((pattrib->seqnum<<16)&0xffff0000);

		/* offset 16 */
		ptxdesc->txdw4 |= cpu_to_le32(BIT(8));/* driver uses rate */

		/* offset 20 */
		ptxdesc->txdw5 |= cpu_to_le32(BIT(17));/* retry limit enable */
		ptxdesc->txdw5 |= cpu_to_le32(0x00180000);/* retry limit = 6 */

		ptxdesc->txdw5 |= cpu_to_le32(MRateToHwRate23a(pmlmeext->tx_rate));
	} else if (pxmitframe->frame_tag == TXAGG_FRAMETAG) {
		DBG_8723A("pxmitframe->frame_tag == TXAGG_FRAMETAG\n");
	} else {
		DBG_8723A("pxmitframe->frame_tag = %d\n",
			  pxmitframe->frame_tag);

		/* offset 4 */
		ptxdesc->txdw1 |= cpu_to_le32((4)&0x1f);/* CAM_ID(MAC_ID) */

		ptxdesc->txdw1 |= cpu_to_le32((6<<16) & 0x000f0000);/* raid */

		/* offset 8 */

		/* offset 12 */
		ptxdesc->txdw3 |= cpu_to_le32((pattrib->seqnum<<16)&0xffff0000);

		/* offset 16 */
		ptxdesc->txdw4 |= cpu_to_le32(BIT(8));/* driver uses rate */

		/* offset 20 */
		ptxdesc->txdw5 |= cpu_to_le32(MRateToHwRate23a(pmlmeext->tx_rate));
	}

	/*  (1) The sequence number of each non-Qos frame / broadcast / multicast / */
	/*  mgnt frame should be controled by Hw because Fw will also send null data */
	/*  which we cannot control when Fw LPS enable. */
	/*  --> default enable non-Qos data sequense number. 2010.06.23. by tynli. */
	/*  (2) Enable HW SEQ control for beacon packet, because we use Hw beacon. */
	/*  (3) Use HW Qos SEQ to control the seq num of Ext port non-Qos packets. */
	if (!pattrib->qos_en) {
		/*  Hw set sequence number */
		ptxdesc->txdw4 |= cpu_to_le32(BIT(7));
		/* set bit3 to 1. */
		ptxdesc->txdw3 |= cpu_to_le32((8 << 28));
	}

	/* offset 0 */
	ptxdesc->txdw0 |= cpu_to_le32(sz&0x0000ffff);
	ptxdesc->txdw0 |= cpu_to_le32(OWN | FSG | LSG);
	ptxdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE+OFFSET_SZ)<<OFFSET_SHT)&0x00ff0000);/* 32 bytes for TX Desc */

	if (bmcst)
		ptxdesc->txdw0 |= cpu_to_le32(BIT(24));

	RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("offset0-txdesc = 0x%x\n", ptxdesc->txdw0));

	/* offset 4 */
	/*  pkt_offset, unit:8 bytes padding */
	if (pxmitframe->pkt_offset > 0)
		ptxdesc->txdw1 |= cpu_to_le32((pxmitframe->pkt_offset << 26) & 0x7c000000);

	rtl8192cu_cal_txdesc_chksum(ptxdesc);
	return pull;
}

static int rtw_dump_xframe(struct rtw_adapter *padapter,
			   struct xmit_frame *pxmitframe)
{
	int ret = _SUCCESS;
	int inner_ret = _SUCCESS;
	int t, sz, w_sz, pull = 0;
	u8 *mem_addr;
	u32 ff_hwaddr;
	struct xmit_buf *pxmitbuf = pxmitframe->pxmitbuf;
	struct pkt_attrib *pattrib = &pxmitframe->attrib;
	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;

	if (pxmitframe->frame_tag == DATA_FRAMETAG &&
	    pxmitframe->attrib.ether_type != ETH_P_ARP &&
	    pxmitframe->attrib.ether_type != ETH_P_PAE &&
	    pxmitframe->attrib.dhcp_pkt != 1)
		rtw_issue_addbareq_cmd23a(padapter, pxmitframe);

	mem_addr = pxmitframe->buf_addr;

	RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_dump_xframe()\n"));

	for (t = 0; t < pattrib->nr_frags; t++) {
		if (inner_ret != _SUCCESS && ret == _SUCCESS)
			ret = _FAIL;

		if (t != (pattrib->nr_frags - 1)) {
			RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_,
				 ("pattrib->nr_frags =%d\n", pattrib->nr_frags));

			sz = pxmitpriv->frag_len;
			sz = sz - 4 - pattrib->icv_len;
		} else {
			/* no frag */
			sz = pattrib->last_txcmdsz;
		}

		pull = update_txdesc(pxmitframe, mem_addr, sz, false);

		if (pull) {
			mem_addr += PACKET_OFFSET_SZ; /* pull txdesc head */

			pxmitframe->buf_addr = mem_addr;

			w_sz = sz + TXDESC_SIZE;
		} else {
			w_sz = sz + TXDESC_SIZE + PACKET_OFFSET_SZ;
		}

		ff_hwaddr = rtw_get_ff_hwaddr23a(pxmitframe);
		inner_ret = rtl8723au_write_port(padapter, ff_hwaddr,
						 w_sz, pxmitbuf);
		rtw_count_tx_stats23a(padapter, pxmitframe, sz);

		RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_,
			 ("rtw_write_port, w_sz =%d\n", w_sz));

		mem_addr += w_sz;

		mem_addr = PTR_ALIGN(mem_addr, 4);
	}

	rtw_free_xmitframe23a(pxmitpriv, pxmitframe);

	if  (ret != _SUCCESS)
		rtw23a_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_UNKNOWN);

	return ret;
}

bool rtl8723au_xmitframe_complete(struct rtw_adapter *padapter,
				  struct xmit_priv *pxmitpriv,
				  struct xmit_buf *pxmitbuf)
{
	struct hw_xmit *phwxmits;
	struct xmit_frame *pxmitframe;
	int hwentry;
	int res = _SUCCESS, xcnt = 0;

	phwxmits = pxmitpriv->hwxmits;
	hwentry = pxmitpriv->hwxmit_entry;

	RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("xmitframe_complete()\n"));

	if (pxmitbuf == NULL) {
		pxmitbuf = rtw_alloc_xmitbuf23a(pxmitpriv);
		if (!pxmitbuf)
			return false;
	}
	pxmitframe =  rtw_dequeue_xframe23a(pxmitpriv, phwxmits, hwentry);

	if (pxmitframe) {
		pxmitframe->pxmitbuf = pxmitbuf;

		pxmitframe->buf_addr = pxmitbuf->pbuf;

		pxmitbuf->priv_data = pxmitframe;

		if (pxmitframe->frame_tag == DATA_FRAMETAG) {
			if (pxmitframe->attrib.priority <= 15)/* TID0~15 */
				res = rtw_xmitframe_coalesce23a(padapter, pxmitframe->pkt, pxmitframe);

			rtw_os_xmit_complete23a(padapter, pxmitframe);/* always return ndis_packet after rtw_xmitframe_coalesce23a */
		}

		RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("xmitframe_complete(): rtw_dump_xframe\n"));

		if (res == _SUCCESS) {
			rtw_dump_xframe(padapter, pxmitframe);
		} else {
			rtw_free_xmitbuf23a(pxmitpriv, pxmitbuf);
			rtw_free_xmitframe23a(pxmitpriv, pxmitframe);
		}
		xcnt++;
	} else {
		rtw_free_xmitbuf23a(pxmitpriv, pxmitbuf);
		return false;
	}
	return true;
}

static int xmitframe_direct(struct rtw_adapter *padapter,
			    struct xmit_frame *pxmitframe)
{
	int res;

	res = rtw_xmitframe_coalesce23a(padapter, pxmitframe->pkt, pxmitframe);
	if (res == _SUCCESS)
		rtw_dump_xframe(padapter, pxmitframe);
	return res;
}

/*
 * Return
 *	true	dump packet directly
 *	false	enqueue packet
 */
bool rtl8723au_hal_xmit(struct rtw_adapter *padapter,
			struct xmit_frame *pxmitframe)
{
	int res;
	struct xmit_buf *pxmitbuf = NULL;
	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
	struct pkt_attrib *pattrib = &pxmitframe->attrib;
	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;

	pattrib->qsel = pattrib->priority;
	spin_lock_bh(&pxmitpriv->lock);

#ifdef CONFIG_8723AU_AP_MODE
	if (xmitframe_enqueue_for_sleeping_sta23a(padapter, pxmitframe)) {
		struct sta_info *psta;
		struct sta_priv *pstapriv = &padapter->stapriv;

		spin_unlock_bh(&pxmitpriv->lock);

		if (pattrib->psta)
			psta = pattrib->psta;
		else
			psta = rtw_get_stainfo23a(pstapriv, pattrib->ra);

		if (psta) {
			if (psta->sleepq_len > (NR_XMITFRAME>>3))
				wakeup_sta_to_xmit23a(padapter, psta);
		}

		return false;
	}
#endif

	if (rtw_txframes_sta_ac_pending23a(padapter, pattrib) > 0)
		goto enqueue;

	if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY|_FW_UNDER_LINKING) == true)
		goto enqueue;

	pxmitbuf = rtw_alloc_xmitbuf23a(pxmitpriv);
	if (pxmitbuf == NULL)
		goto enqueue;

	spin_unlock_bh(&pxmitpriv->lock);

	pxmitframe->pxmitbuf = pxmitbuf;
	pxmitframe->buf_addr = pxmitbuf->pbuf;
	pxmitbuf->priv_data = pxmitframe;

	if (xmitframe_direct(padapter, pxmitframe) != _SUCCESS) {
		rtw_free_xmitbuf23a(pxmitpriv, pxmitbuf);
		rtw_free_xmitframe23a(pxmitpriv, pxmitframe);
	}
	return true;

enqueue:
	res = rtw_xmitframe_enqueue23a(padapter, pxmitframe);
	spin_unlock_bh(&pxmitpriv->lock);

	if (res != _SUCCESS) {
		RT_TRACE(_module_xmit_osdep_c_, _drv_err_,
			 ("pre_xmitframe: enqueue xmitframe fail\n"));
		rtw_free_xmitframe23a(pxmitpriv, pxmitframe);

		/*  Trick, make the statistics correct */
		pxmitpriv->tx_pkts--;
		pxmitpriv->tx_drop++;
		return true;
	}
	return false;
}

int rtl8723au_mgnt_xmit(struct rtw_adapter *padapter,
			struct xmit_frame *pmgntframe)
{
	return rtw_dump_xframe(padapter, pmgntframe);
}

int rtl8723au_hal_xmitframe_enqueue(struct rtw_adapter *padapter,
				    struct xmit_frame *pxmitframe)
{
	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
	int err;

	err = rtw_xmitframe_enqueue23a(padapter, pxmitframe);
	if (err != _SUCCESS) {
		rtw_free_xmitframe23a(pxmitpriv, pxmitframe);

		/*  Trick, make the statistics correct */
		pxmitpriv->tx_pkts--;
		pxmitpriv->tx_drop++;
	} else {
		tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
	}
	return err;
}