[go: nahoru, domu]

1/******************************************************************************
2 *
3 * Copyright(c) 2009-2013  Realtek Corporation.
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 * The full GNU General Public License is included in this distribution in the
15 * file called LICENSE.
16 *
17 * Contact Information:
18 * wlanfae <wlanfae@realtek.com>
19 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20 * Hsinchu 300, Taiwan.
21 *
22 * Larry Finger <Larry.Finger@lwfinger.net>
23 *
24 *****************************************************************************/
25
26#include "../wifi.h"
27#include "../pci.h"
28#include "../base.h"
29#include "../stats.h"
30#include "reg.h"
31#include "def.h"
32#include "phy.h"
33#include "trx.h"
34#include "led.h"
35#include "dm.h"
36#include "phy.h"
37
38static u8 _rtl88ee_map_hwqueue_to_fwqueue(struct sk_buff *skb, u8 hw_queue)
39{
40	__le16 fc = rtl_get_fc(skb);
41
42	if (unlikely(ieee80211_is_beacon(fc)))
43		return QSLT_BEACON;
44	if (ieee80211_is_mgmt(fc) || ieee80211_is_ctl(fc))
45		return QSLT_MGNT;
46
47	return skb->priority;
48}
49
50/* mac80211's rate_idx is like this:
51 *
52 * 2.4G band:rx_status->band == IEEE80211_BAND_2GHZ
53 *
54 * B/G rate:
55 * (rx_status->flag & RX_FLAG_HT) = 0,
56 * DESC92C_RATE1M-->DESC92C_RATE54M ==> idx is 0-->11,
57 *
58 * N rate:
59 * (rx_status->flag & RX_FLAG_HT) = 1,
60 * DESC92C_RATEMCS0-->DESC92C_RATEMCS15 ==> idx is 0-->15
61 *
62 * 5G band:rx_status->band == IEEE80211_BAND_5GHZ
63 * A rate:
64 * (rx_status->flag & RX_FLAG_HT) = 0,
65 * DESC92C_RATE6M-->DESC92C_RATE54M ==> idx is 0-->7,
66 *
67 * N rate:
68 * (rx_status->flag & RX_FLAG_HT) = 1,
69 * DESC92C_RATEMCS0-->DESC92C_RATEMCS15 ==> idx is 0-->15
70 */
71static int _rtl88ee_rate_mapping(struct ieee80211_hw *hw,
72				 bool isht, u8 desc_rate)
73{
74	int rate_idx;
75
76	if (!isht) {
77		if (IEEE80211_BAND_2GHZ == hw->conf.chandef.chan->band) {
78			switch (desc_rate) {
79			case DESC92C_RATE1M:
80				rate_idx = 0;
81				break;
82			case DESC92C_RATE2M:
83				rate_idx = 1;
84				break;
85			case DESC92C_RATE5_5M:
86				rate_idx = 2;
87				break;
88			case DESC92C_RATE11M:
89				rate_idx = 3;
90				break;
91			case DESC92C_RATE6M:
92				rate_idx = 4;
93				break;
94			case DESC92C_RATE9M:
95				rate_idx = 5;
96				break;
97			case DESC92C_RATE12M:
98				rate_idx = 6;
99				break;
100			case DESC92C_RATE18M:
101				rate_idx = 7;
102				break;
103			case DESC92C_RATE24M:
104				rate_idx = 8;
105				break;
106			case DESC92C_RATE36M:
107				rate_idx = 9;
108				break;
109			case DESC92C_RATE48M:
110				rate_idx = 10;
111				break;
112			case DESC92C_RATE54M:
113				rate_idx = 11;
114				break;
115			default:
116				rate_idx = 0;
117				break;
118			}
119		} else {
120			switch (desc_rate) {
121			case DESC92C_RATE6M:
122				rate_idx = 0;
123				break;
124			case DESC92C_RATE9M:
125				rate_idx = 1;
126				break;
127			case DESC92C_RATE12M:
128				rate_idx = 2;
129				break;
130			case DESC92C_RATE18M:
131				rate_idx = 3;
132				break;
133			case DESC92C_RATE24M:
134				rate_idx = 4;
135				break;
136			case DESC92C_RATE36M:
137				rate_idx = 5;
138				break;
139			case DESC92C_RATE48M:
140				rate_idx = 6;
141				break;
142			case DESC92C_RATE54M:
143				rate_idx = 7;
144				break;
145			default:
146				rate_idx = 0;
147				break;
148			}
149		}
150	} else {
151		switch (desc_rate) {
152		case DESC92C_RATEMCS0:
153			rate_idx = 0;
154			break;
155		case DESC92C_RATEMCS1:
156			rate_idx = 1;
157			break;
158		case DESC92C_RATEMCS2:
159			rate_idx = 2;
160			break;
161		case DESC92C_RATEMCS3:
162			rate_idx = 3;
163			break;
164		case DESC92C_RATEMCS4:
165			rate_idx = 4;
166			break;
167		case DESC92C_RATEMCS5:
168			rate_idx = 5;
169			break;
170		case DESC92C_RATEMCS6:
171			rate_idx = 6;
172			break;
173		case DESC92C_RATEMCS7:
174			rate_idx = 7;
175			break;
176		case DESC92C_RATEMCS8:
177			rate_idx = 8;
178			break;
179		case DESC92C_RATEMCS9:
180			rate_idx = 9;
181			break;
182		case DESC92C_RATEMCS10:
183			rate_idx = 10;
184			break;
185		case DESC92C_RATEMCS11:
186			rate_idx = 11;
187			break;
188		case DESC92C_RATEMCS12:
189			rate_idx = 12;
190			break;
191		case DESC92C_RATEMCS13:
192			rate_idx = 13;
193			break;
194		case DESC92C_RATEMCS14:
195			rate_idx = 14;
196			break;
197		case DESC92C_RATEMCS15:
198			rate_idx = 15;
199			break;
200		default:
201			rate_idx = 0;
202			break;
203		}
204	}
205	return rate_idx;
206}
207
208static void _rtl88ee_query_rxphystatus(struct ieee80211_hw *hw,
209			struct rtl_stats *pstatus, u8 *pdesc,
210			struct rx_fwinfo_88e *p_drvinfo,
211			bool bpacket_match_bssid,
212			bool bpacket_toself, bool packet_beacon)
213{
214	struct rtl_priv *rtlpriv = rtl_priv(hw);
215	struct rtl_ps_ctl *ppsc = rtl_psc(rtlpriv);
216	struct phy_sts_cck_8192s_t *cck_buf;
217	struct phy_status_rpt *phystrpt =
218		(struct phy_status_rpt *)p_drvinfo;
219	struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
220	char rx_pwr_all = 0, rx_pwr[4];
221	u8 rf_rx_num = 0, evm, pwdb_all;
222	u8 i, max_spatial_stream;
223	u32 rssi, total_rssi = 0;
224	bool is_cck = pstatus->is_cck;
225	u8 lan_idx, vga_idx;
226
227	/* Record it for next packet processing */
228	pstatus->packet_matchbssid = bpacket_match_bssid;
229	pstatus->packet_toself = bpacket_toself;
230	pstatus->packet_beacon = packet_beacon;
231	pstatus->rx_mimo_signalquality[0] = -1;
232	pstatus->rx_mimo_signalquality[1] = -1;
233
234	if (is_cck) {
235		u8 cck_highpwr;
236		u8 cck_agc_rpt;
237		/* CCK Driver info Structure is not the same as OFDM packet. */
238		cck_buf = (struct phy_sts_cck_8192s_t *)p_drvinfo;
239		cck_agc_rpt = cck_buf->cck_agc_rpt;
240
241		/* (1)Hardware does not provide RSSI for CCK
242		 * (2)PWDB, Average PWDB cacluated by
243		 * hardware (for rate adaptive)
244		 */
245		if (ppsc->rfpwr_state == ERFON)
246			cck_highpwr =
247				(u8)rtl_get_bbreg(hw, RFPGA0_XA_HSSIPARAMETER2,
248						  BIT(9));
249		else
250			cck_highpwr = false;
251
252		lan_idx = ((cck_agc_rpt & 0xE0) >> 5);
253		vga_idx = (cck_agc_rpt & 0x1f);
254		switch (lan_idx) {
255		case 7:
256			if (vga_idx <= 27)
257				/*VGA_idx = 27~2*/
258				rx_pwr_all = -100 + 2*(27-vga_idx);
259			else
260				rx_pwr_all = -100;
261			break;
262		case 6:
263			/*VGA_idx = 2~0*/
264			rx_pwr_all = -48 + 2*(2-vga_idx);
265			break;
266		case 5:
267			/*VGA_idx = 7~5*/
268			rx_pwr_all = -42 + 2*(7-vga_idx);
269			break;
270		case 4:
271			/*VGA_idx = 7~4*/
272			rx_pwr_all = -36 + 2*(7-vga_idx);
273			break;
274		case 3:
275			/*VGA_idx = 7~0*/
276			rx_pwr_all = -24 + 2*(7-vga_idx);
277			break;
278		case 2:
279			if (cck_highpwr)
280				/*VGA_idx = 5~0*/
281				rx_pwr_all = -12 + 2*(5-vga_idx);
282			else
283				rx_pwr_all = -6 + 2*(5-vga_idx);
284			break;
285		case 1:
286			rx_pwr_all = 8-2*vga_idx;
287			break;
288		case 0:
289			rx_pwr_all = 14-2*vga_idx;
290			break;
291		default:
292			break;
293		}
294		rx_pwr_all += 6;
295		pwdb_all = rtl_query_rxpwrpercentage(rx_pwr_all);
296		/* CCK gain is smaller than OFDM/MCS gain,  */
297		/* so we add gain diff by experiences, the val is 6 */
298		pwdb_all += 6;
299		if (pwdb_all > 100)
300			pwdb_all = 100;
301		/* modify the offset to make the same
302		 * gain index with OFDM.
303		 */
304		if (pwdb_all > 34 && pwdb_all <= 42)
305			pwdb_all -= 2;
306		else if (pwdb_all > 26 && pwdb_all <= 34)
307			pwdb_all -= 6;
308		else if (pwdb_all > 14 && pwdb_all <= 26)
309			pwdb_all -= 8;
310		else if (pwdb_all > 4 && pwdb_all <= 14)
311			pwdb_all -= 4;
312		if (!cck_highpwr) {
313			if (pwdb_all >= 80)
314				pwdb_all = ((pwdb_all-80)<<1) +
315					   ((pwdb_all-80)>>1) + 80;
316			else if ((pwdb_all <= 78) && (pwdb_all >= 20))
317				pwdb_all += 3;
318			if (pwdb_all > 100)
319				pwdb_all = 100;
320		}
321
322		pstatus->rx_pwdb_all = pwdb_all;
323		pstatus->recvsignalpower = rx_pwr_all;
324
325		/* (3) Get Signal Quality (EVM) */
326		if (bpacket_match_bssid) {
327			u8 sq;
328
329			if (pstatus->rx_pwdb_all > 40)
330				sq = 100;
331			else {
332				sq = cck_buf->sq_rpt;
333				if (sq > 64)
334					sq = 0;
335				else if (sq < 20)
336					sq = 100;
337				else
338					sq = ((64 - sq) * 100) / 44;
339			}
340
341			pstatus->signalquality = sq;
342			pstatus->rx_mimo_signalquality[0] = sq;
343			pstatus->rx_mimo_signalquality[1] = -1;
344		}
345	} else {
346		rtlpriv->dm.rfpath_rxenable[0] =
347		    rtlpriv->dm.rfpath_rxenable[1] = true;
348
349		/* (1)Get RSSI for HT rate */
350		for (i = RF90_PATH_A; i < RF6052_MAX_PATH; i++) {
351			/* we will judge RF RX path now. */
352			if (rtlpriv->dm.rfpath_rxenable[i])
353				rf_rx_num++;
354
355			rx_pwr[i] = ((p_drvinfo->gain_trsw[i] &
356				      0x3f) * 2) - 110;
357
358			/* Translate DBM to percentage. */
359			rssi = rtl_query_rxpwrpercentage(rx_pwr[i]);
360			total_rssi += rssi;
361
362			/* Get Rx snr value in DB */
363			rtlpriv->stats.rx_snr_db[i] =
364				(long)(p_drvinfo->rxsnr[i] / 2);
365
366			/* Record Signal Strength for next packet */
367			if (bpacket_match_bssid)
368				pstatus->rx_mimo_signalstrength[i] = (u8)rssi;
369		}
370
371		/* (2)PWDB, Average PWDB cacluated by
372		 * hardware (for rate adaptive)
373		 */
374		rx_pwr_all = ((p_drvinfo->pwdb_all >> 1) & 0x7f) - 110;
375
376		pwdb_all = rtl_query_rxpwrpercentage(rx_pwr_all);
377		pstatus->rx_pwdb_all = pwdb_all;
378		pstatus->rxpower = rx_pwr_all;
379		pstatus->recvsignalpower = rx_pwr_all;
380
381		/* (3)EVM of HT rate */
382		if (pstatus->is_ht && pstatus->rate >= DESC92C_RATEMCS8 &&
383		    pstatus->rate <= DESC92C_RATEMCS15)
384			max_spatial_stream = 2;
385		else
386			max_spatial_stream = 1;
387
388		for (i = 0; i < max_spatial_stream; i++) {
389			evm = rtl_evm_db_to_percentage(p_drvinfo->rxevm[i]);
390
391			if (bpacket_match_bssid) {
392				/* Fill value in RFD, Get the first
393				 * spatial stream onlyi
394				 */
395				if (i == 0)
396					pstatus->signalquality =
397						(u8)(evm & 0xff);
398				pstatus->rx_mimo_signalquality[i] =
399					(u8)(evm & 0xff);
400			}
401		}
402	}
403
404	/* UI BSS List signal strength(in percentage),
405	 * make it good looking, from 0~100.
406	 */
407	if (is_cck)
408		pstatus->signalstrength = (u8)(rtl_signal_scale_mapping(hw,
409			pwdb_all));
410	else if (rf_rx_num != 0)
411		pstatus->signalstrength = (u8)(rtl_signal_scale_mapping(hw,
412			total_rssi /= rf_rx_num));
413	/*HW antenna diversity*/
414	rtldm->fat_table.antsel_rx_keep_0 = phystrpt->ant_sel;
415	rtldm->fat_table.antsel_rx_keep_1 = phystrpt->ant_sel_b;
416	rtldm->fat_table.antsel_rx_keep_2 = phystrpt->antsel_rx_keep_2;
417}
418
419static void _rtl88ee_smart_antenna(struct ieee80211_hw *hw,
420	struct rtl_stats *pstatus)
421{
422	struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
423	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
424	u8 antsel_tr_mux;
425	struct fast_ant_training *pfat_table = &rtldm->fat_table;
426
427	if (rtlefuse->antenna_div_type == CG_TRX_SMART_ANTDIV) {
428		if (pfat_table->fat_state == FAT_TRAINING_STATE) {
429			if (pstatus->packet_toself) {
430				antsel_tr_mux =
431					(pfat_table->antsel_rx_keep_2 << 2) |
432					(pfat_table->antsel_rx_keep_1 << 1) |
433					pfat_table->antsel_rx_keep_0;
434				pfat_table->ant_sum[antsel_tr_mux] +=
435					pstatus->rx_pwdb_all;
436				pfat_table->ant_cnt[antsel_tr_mux]++;
437			}
438		}
439	} else if ((rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV) ||
440	(rtlefuse->antenna_div_type == CGCS_RX_HW_ANTDIV)) {
441		if (pstatus->packet_toself || pstatus->packet_matchbssid) {
442			antsel_tr_mux = (pfat_table->antsel_rx_keep_2 << 2) |
443					(pfat_table->antsel_rx_keep_1 << 1) |
444					pfat_table->antsel_rx_keep_0;
445			rtl88e_dm_ant_sel_statistics(hw, antsel_tr_mux, 0,
446						     pstatus->rx_pwdb_all);
447		}
448
449	}
450}
451
452static void _rtl88ee_translate_rx_signal_stuff(struct ieee80211_hw *hw,
453					       struct sk_buff *skb,
454					       struct rtl_stats *pstatus,
455					       u8 *pdesc,
456					       struct rx_fwinfo_88e *p_drvinfo)
457{
458	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
459	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
460	struct ieee80211_hdr *hdr;
461	u8 *tmp_buf;
462	u8 *praddr;
463	u8 *psaddr;
464	__le16 fc;
465	bool packet_matchbssid, packet_toself, packet_beacon;
466
467	tmp_buf = skb->data + pstatus->rx_drvinfo_size + pstatus->rx_bufshift;
468
469	hdr = (struct ieee80211_hdr *)tmp_buf;
470	fc = hdr->frame_control;
471	praddr = hdr->addr1;
472	psaddr = ieee80211_get_SA(hdr);
473	memcpy(pstatus->psaddr, psaddr, ETH_ALEN);
474
475	packet_matchbssid = ((!ieee80211_is_ctl(fc)) &&
476	     (ether_addr_equal(mac->bssid, ieee80211_has_tods(fc) ?
477			       hdr->addr1 : ieee80211_has_fromds(fc) ?
478			       hdr->addr2 : hdr->addr3)) &&
479			       (!pstatus->hwerror) &&
480			       (!pstatus->crc) && (!pstatus->icv));
481
482	packet_toself = packet_matchbssid &&
483	    (ether_addr_equal(praddr, rtlefuse->dev_addr));
484
485	if (ieee80211_is_beacon(hdr->frame_control))
486		packet_beacon = true;
487	else
488		packet_beacon = false;
489
490	_rtl88ee_query_rxphystatus(hw, pstatus, pdesc, p_drvinfo,
491				   packet_matchbssid, packet_toself,
492				   packet_beacon);
493	_rtl88ee_smart_antenna(hw, pstatus);
494	rtl_process_phyinfo(hw, tmp_buf, pstatus);
495}
496
497static void _rtl88ee_insert_emcontent(struct rtl_tcb_desc *ptcb_desc,
498				      u8 *virtualaddress)
499{
500	u32 dwtmp = 0;
501	memset(virtualaddress, 0, 8);
502
503	SET_EARLYMODE_PKTNUM(virtualaddress, ptcb_desc->empkt_num);
504	if (ptcb_desc->empkt_num == 1) {
505		dwtmp = ptcb_desc->empkt_len[0];
506	} else {
507		dwtmp = ptcb_desc->empkt_len[0];
508		dwtmp += ((dwtmp%4) ? (4-dwtmp%4) : 0)+4;
509		dwtmp += ptcb_desc->empkt_len[1];
510	}
511	SET_EARLYMODE_LEN0(virtualaddress, dwtmp);
512
513	if (ptcb_desc->empkt_num <= 3) {
514		dwtmp = ptcb_desc->empkt_len[2];
515	} else {
516		dwtmp = ptcb_desc->empkt_len[2];
517		dwtmp += ((dwtmp%4) ? (4-dwtmp%4) : 0)+4;
518		dwtmp += ptcb_desc->empkt_len[3];
519	}
520	SET_EARLYMODE_LEN1(virtualaddress, dwtmp);
521	if (ptcb_desc->empkt_num <= 5) {
522		dwtmp = ptcb_desc->empkt_len[4];
523	} else {
524		dwtmp = ptcb_desc->empkt_len[4];
525		dwtmp += ((dwtmp%4) ? (4-dwtmp%4) : 0)+4;
526		dwtmp += ptcb_desc->empkt_len[5];
527	}
528	SET_EARLYMODE_LEN2_1(virtualaddress, dwtmp & 0xF);
529	SET_EARLYMODE_LEN2_2(virtualaddress, dwtmp >> 4);
530	if (ptcb_desc->empkt_num <= 7) {
531		dwtmp = ptcb_desc->empkt_len[6];
532	} else {
533		dwtmp = ptcb_desc->empkt_len[6];
534		dwtmp += ((dwtmp%4) ? (4-dwtmp%4) : 0)+4;
535		dwtmp += ptcb_desc->empkt_len[7];
536	}
537	SET_EARLYMODE_LEN3(virtualaddress, dwtmp);
538	if (ptcb_desc->empkt_num <= 9) {
539		dwtmp = ptcb_desc->empkt_len[8];
540	} else {
541		dwtmp = ptcb_desc->empkt_len[8];
542		dwtmp += ((dwtmp%4) ? (4-dwtmp%4) : 0)+4;
543		dwtmp += ptcb_desc->empkt_len[9];
544	}
545	SET_EARLYMODE_LEN4(virtualaddress, dwtmp);
546}
547
548bool rtl88ee_rx_query_desc(struct ieee80211_hw *hw,
549			   struct rtl_stats *status,
550			   struct ieee80211_rx_status *rx_status,
551			   u8 *pdesc, struct sk_buff *skb)
552{
553	struct rtl_priv *rtlpriv = rtl_priv(hw);
554	struct rx_fwinfo_88e *p_drvinfo;
555	struct ieee80211_hdr *hdr;
556
557	u32 phystatus = GET_RX_DESC_PHYST(pdesc);
558	status->packet_report_type = (u8)GET_RX_STATUS_DESC_RPT_SEL(pdesc);
559	if (status->packet_report_type == TX_REPORT2)
560		status->length = (u16)GET_RX_RPT2_DESC_PKT_LEN(pdesc);
561	else
562		status->length = (u16)GET_RX_DESC_PKT_LEN(pdesc);
563	status->rx_drvinfo_size = (u8)GET_RX_DESC_DRV_INFO_SIZE(pdesc) *
564	    RX_DRV_INFO_SIZE_UNIT;
565	status->rx_bufshift = (u8)(GET_RX_DESC_SHIFT(pdesc) & 0x03);
566	status->icv = (u16)GET_RX_DESC_ICV(pdesc);
567	status->crc = (u16)GET_RX_DESC_CRC32(pdesc);
568	status->hwerror = (status->crc | status->icv);
569	status->decrypted = !GET_RX_DESC_SWDEC(pdesc);
570	status->rate = (u8)GET_RX_DESC_RXMCS(pdesc);
571	status->shortpreamble = (u16)GET_RX_DESC_SPLCP(pdesc);
572	status->isampdu = (bool) (GET_RX_DESC_PAGGR(pdesc) == 1);
573	status->isfirst_ampdu = (bool)((GET_RX_DESC_PAGGR(pdesc) == 1) &&
574				(GET_RX_DESC_FAGGR(pdesc) == 1));
575	if (status->packet_report_type == NORMAL_RX)
576		status->timestamp_low = GET_RX_DESC_TSFL(pdesc);
577	status->rx_is40Mhzpacket = (bool) GET_RX_DESC_BW(pdesc);
578	status->is_ht = (bool)GET_RX_DESC_RXHT(pdesc);
579
580	status->is_cck = RTL8188_RX_HAL_IS_CCK_RATE(status->rate);
581
582	status->macid = GET_RX_DESC_MACID(pdesc);
583	if (GET_RX_STATUS_DESC_MAGIC_MATCH(pdesc))
584		status->wake_match = BIT(2);
585	else if (GET_RX_STATUS_DESC_MAGIC_MATCH(pdesc))
586		status->wake_match = BIT(1);
587	else if (GET_RX_STATUS_DESC_UNICAST_MATCH(pdesc))
588		status->wake_match = BIT(0);
589	else
590		status->wake_match = 0;
591	if (status->wake_match)
592		RT_TRACE(rtlpriv, COMP_RXDESC, DBG_LOUD,
593		"GGGGGGGGGGGGGet Wakeup Packet!! WakeMatch=%d\n",
594		status->wake_match);
595	rx_status->freq = hw->conf.chandef.chan->center_freq;
596	rx_status->band = hw->conf.chandef.chan->band;
597
598	hdr = (struct ieee80211_hdr *)(skb->data + status->rx_drvinfo_size
599			+ status->rx_bufshift);
600
601	if (status->crc)
602		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
603
604	if (status->rx_is40Mhzpacket)
605		rx_status->flag |= RX_FLAG_40MHZ;
606
607	if (status->is_ht)
608		rx_status->flag |= RX_FLAG_HT;
609
610	rx_status->flag |= RX_FLAG_MACTIME_START;
611
612	/* hw will set status->decrypted true, if it finds the
613	 * frame is open data frame or mgmt frame.
614	 * So hw will not decryption robust managment frame
615	 * for IEEE80211w but still set status->decrypted
616	 * true, so here we should set it back to undecrypted
617	 * for IEEE80211w frame, and mac80211 sw will help
618	 * to decrypt it
619	 */
620	if (status->decrypted) {
621		if ((!_ieee80211_is_robust_mgmt_frame(hdr)) &&
622		    (ieee80211_has_protected(hdr->frame_control)))
623			rx_status->flag |= RX_FLAG_DECRYPTED;
624		else
625			rx_status->flag &= ~RX_FLAG_DECRYPTED;
626	}
627
628	/* rate_idx: index of data rate into band's
629	 * supported rates or MCS index if HT rates
630	 * are use (RX_FLAG_HT)
631	 * Notice: this is diff with windows define
632	 */
633	rx_status->rate_idx = _rtl88ee_rate_mapping(hw,
634				status->is_ht, status->rate);
635
636	rx_status->mactime = status->timestamp_low;
637	if (phystatus == true) {
638		p_drvinfo = (struct rx_fwinfo_88e *)(skb->data +
639						     status->rx_bufshift);
640
641		_rtl88ee_translate_rx_signal_stuff(hw,
642						   skb, status, pdesc,
643						   p_drvinfo);
644	}
645	rx_status->signal = status->recvsignalpower + 10;
646	if (status->packet_report_type == TX_REPORT2) {
647		status->macid_valid_entry[0] =
648			 GET_RX_RPT2_DESC_MACID_VALID_1(pdesc);
649		status->macid_valid_entry[1] =
650			 GET_RX_RPT2_DESC_MACID_VALID_2(pdesc);
651	}
652	return true;
653}
654
655void rtl88ee_tx_fill_desc(struct ieee80211_hw *hw,
656			  struct ieee80211_hdr *hdr, u8 *pdesc_tx,
657			  u8 *txbd, struct ieee80211_tx_info *info,
658			  struct ieee80211_sta *sta,
659			  struct sk_buff *skb,
660			  u8 hw_queue, struct rtl_tcb_desc *ptcb_desc)
661
662{
663	struct rtl_priv *rtlpriv = rtl_priv(hw);
664	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
665	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
666	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
667	u8 *pdesc = (u8 *)pdesc_tx;
668	u16 seq_number;
669	__le16 fc = hdr->frame_control;
670	unsigned int buf_len = 0;
671	unsigned int skb_len = skb->len;
672	u8 fw_qsel = _rtl88ee_map_hwqueue_to_fwqueue(skb, hw_queue);
673	bool firstseg = ((hdr->seq_ctrl &
674			    cpu_to_le16(IEEE80211_SCTL_FRAG)) == 0);
675	bool lastseg = ((hdr->frame_control &
676			   cpu_to_le16(IEEE80211_FCTL_MOREFRAGS)) == 0);
677	dma_addr_t mapping;
678	u8 bw_40 = 0;
679	u8 short_gi = 0;
680
681	if (mac->opmode == NL80211_IFTYPE_STATION) {
682		bw_40 = mac->bw_40;
683	} else if (mac->opmode == NL80211_IFTYPE_AP ||
684		mac->opmode == NL80211_IFTYPE_ADHOC) {
685		if (sta)
686			bw_40 = sta->ht_cap.cap &
687				IEEE80211_HT_CAP_SUP_WIDTH_20_40;
688	}
689	seq_number = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4;
690	rtl_get_tcb_desc(hw, info, sta, skb, ptcb_desc);
691	/* reserve 8 byte for AMPDU early mode */
692	if (rtlhal->earlymode_enable) {
693		skb_push(skb, EM_HDR_LEN);
694		memset(skb->data, 0, EM_HDR_LEN);
695	}
696	buf_len = skb->len;
697	mapping = pci_map_single(rtlpci->pdev, skb->data, skb->len,
698				 PCI_DMA_TODEVICE);
699	if (pci_dma_mapping_error(rtlpci->pdev, mapping)) {
700		RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
701			 "DMA mapping error");
702		return;
703	}
704	CLEAR_PCI_TX_DESC_CONTENT(pdesc, sizeof(struct tx_desc_88e));
705	if (ieee80211_is_nullfunc(fc) || ieee80211_is_ctl(fc)) {
706		firstseg = true;
707		lastseg = true;
708	}
709	if (firstseg) {
710		if (rtlhal->earlymode_enable) {
711			SET_TX_DESC_PKT_OFFSET(pdesc, 1);
712			SET_TX_DESC_OFFSET(pdesc, USB_HWDESC_HEADER_LEN +
713					   EM_HDR_LEN);
714			if (ptcb_desc->empkt_num) {
715				RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
716					 "Insert 8 byte.pTcb->EMPktNum:%d\n",
717					  ptcb_desc->empkt_num);
718				_rtl88ee_insert_emcontent(ptcb_desc,
719							  (u8 *)(skb->data));
720			}
721		} else {
722			SET_TX_DESC_OFFSET(pdesc, USB_HWDESC_HEADER_LEN);
723		}
724
725		ptcb_desc->use_driver_rate = true;
726		SET_TX_DESC_TX_RATE(pdesc, ptcb_desc->hw_rate);
727		if (ptcb_desc->hw_rate > DESC92C_RATEMCS0)
728			short_gi = (ptcb_desc->use_shortgi) ? 1 : 0;
729		else
730			short_gi = (ptcb_desc->use_shortpreamble) ? 1 : 0;
731
732		SET_TX_DESC_DATA_SHORTGI(pdesc, short_gi);
733
734		if (info->flags & IEEE80211_TX_CTL_AMPDU) {
735			SET_TX_DESC_AGG_ENABLE(pdesc, 1);
736			SET_TX_DESC_MAX_AGG_NUM(pdesc, 0x14);
737		}
738		SET_TX_DESC_SEQ(pdesc, seq_number);
739		SET_TX_DESC_RTS_ENABLE(pdesc, ((ptcb_desc->rts_enable &&
740						!ptcb_desc->cts_enable) ? 1 : 0));
741		SET_TX_DESC_HW_RTS_ENABLE(pdesc, 0);
742		SET_TX_DESC_CTS2SELF(pdesc, ((ptcb_desc->cts_enable) ? 1 : 0));
743		SET_TX_DESC_RTS_STBC(pdesc, ((ptcb_desc->rts_stbc) ? 1 : 0));
744
745		SET_TX_DESC_RTS_RATE(pdesc, ptcb_desc->rts_rate);
746		SET_TX_DESC_RTS_BW(pdesc, 0);
747		SET_TX_DESC_RTS_SC(pdesc, ptcb_desc->rts_sc);
748		SET_TX_DESC_RTS_SHORT(pdesc,
749			((ptcb_desc->rts_rate <= DESC92C_RATE54M) ?
750			(ptcb_desc->rts_use_shortpreamble ? 1 : 0) :
751			(ptcb_desc->rts_use_shortgi ? 1 : 0)));
752
753		if (ptcb_desc->tx_enable_sw_calc_duration)
754			SET_TX_DESC_NAV_USE_HDR(pdesc, 1);
755
756		if (bw_40) {
757			if (ptcb_desc->packet_bw == HT_CHANNEL_WIDTH_20_40) {
758				SET_TX_DESC_DATA_BW(pdesc, 1);
759				SET_TX_DESC_TX_SUB_CARRIER(pdesc, 3);
760			} else {
761				SET_TX_DESC_DATA_BW(pdesc, 0);
762				SET_TX_DESC_TX_SUB_CARRIER(pdesc,
763							   mac->cur_40_prime_sc);
764			}
765		} else {
766			SET_TX_DESC_DATA_BW(pdesc, 0);
767			SET_TX_DESC_TX_SUB_CARRIER(pdesc, 0);
768		}
769
770		SET_TX_DESC_LINIP(pdesc, 0);
771		SET_TX_DESC_PKT_SIZE(pdesc, (u16)skb_len);
772		if (sta) {
773			u8 ampdu_density = sta->ht_cap.ampdu_density;
774			SET_TX_DESC_AMPDU_DENSITY(pdesc, ampdu_density);
775		}
776		if (info->control.hw_key) {
777			struct ieee80211_key_conf *keyconf;
778
779			keyconf = info->control.hw_key;
780			switch (keyconf->cipher) {
781			case WLAN_CIPHER_SUITE_WEP40:
782			case WLAN_CIPHER_SUITE_WEP104:
783			case WLAN_CIPHER_SUITE_TKIP:
784				SET_TX_DESC_SEC_TYPE(pdesc, 0x1);
785				break;
786			case WLAN_CIPHER_SUITE_CCMP:
787				SET_TX_DESC_SEC_TYPE(pdesc, 0x3);
788				break;
789			default:
790				SET_TX_DESC_SEC_TYPE(pdesc, 0x0);
791				break;
792
793			}
794		}
795
796		SET_TX_DESC_QUEUE_SEL(pdesc, fw_qsel);
797		SET_TX_DESC_DATA_RATE_FB_LIMIT(pdesc, 0x1F);
798		SET_TX_DESC_RTS_RATE_FB_LIMIT(pdesc, 0xF);
799		SET_TX_DESC_DISABLE_FB(pdesc, ptcb_desc->disable_ratefallback ?
800				       1 : 0);
801		SET_TX_DESC_USE_RATE(pdesc, ptcb_desc->use_driver_rate ? 1 : 0);
802
803		/*SET_TX_DESC_PWR_STATUS(pdesc, pwr_status);*/
804		/* Set TxRate and RTSRate in TxDesc  */
805		/* This prevent Tx initial rate of new-coming packets */
806		/* from being overwritten by retried  packet rate.*/
807		if (!ptcb_desc->use_driver_rate) {
808			/*SET_TX_DESC_RTS_RATE(pdesc, 0x08); */
809			/* SET_TX_DESC_TX_RATE(pdesc, 0x0b); */
810		}
811		if (ieee80211_is_data_qos(fc)) {
812			if (mac->rdg_en) {
813				RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
814					"Enable RDG function.\n");
815				SET_TX_DESC_RDG_ENABLE(pdesc, 1);
816				SET_TX_DESC_HTC(pdesc, 1);
817			}
818		}
819	}
820
821	SET_TX_DESC_FIRST_SEG(pdesc, (firstseg ? 1 : 0));
822	SET_TX_DESC_LAST_SEG(pdesc, (lastseg ? 1 : 0));
823	SET_TX_DESC_TX_BUFFER_SIZE(pdesc, (u16)buf_len);
824	SET_TX_DESC_TX_BUFFER_ADDRESS(pdesc, mapping);
825	if (rtlpriv->dm.useramask) {
826		SET_TX_DESC_RATE_ID(pdesc, ptcb_desc->ratr_index);
827		SET_TX_DESC_MACID(pdesc, ptcb_desc->mac_id);
828	} else {
829		SET_TX_DESC_RATE_ID(pdesc, 0xC + ptcb_desc->ratr_index);
830		SET_TX_DESC_MACID(pdesc, ptcb_desc->ratr_index);
831	}
832	if (ieee80211_is_data_qos(fc))
833		SET_TX_DESC_QOS(pdesc, 1);
834
835	if (!ieee80211_is_data_qos(fc))
836		SET_TX_DESC_HWSEQ_EN(pdesc, 1);
837	SET_TX_DESC_MORE_FRAG(pdesc, (lastseg ? 0 : 1));
838	if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
839	    is_broadcast_ether_addr(ieee80211_get_DA(hdr))) {
840		SET_TX_DESC_BMC(pdesc, 1);
841	}
842
843	rtl88e_dm_set_tx_ant_by_tx_info(hw, pdesc, ptcb_desc->mac_id);
844	RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE, "\n");
845}
846
847void rtl88ee_tx_fill_cmddesc(struct ieee80211_hw *hw,
848			     u8 *pdesc, bool firstseg,
849			     bool lastseg, struct sk_buff *skb)
850{
851	struct rtl_priv *rtlpriv = rtl_priv(hw);
852	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
853	u8 fw_queue = QSLT_BEACON;
854
855	dma_addr_t mapping = pci_map_single(rtlpci->pdev,
856					    skb->data, skb->len,
857					    PCI_DMA_TODEVICE);
858
859	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
860	__le16 fc = hdr->frame_control;
861
862	if (pci_dma_mapping_error(rtlpci->pdev, mapping)) {
863		RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
864			 "DMA mapping error");
865		return;
866	}
867	CLEAR_PCI_TX_DESC_CONTENT(pdesc, TX_DESC_SIZE);
868
869	if (firstseg)
870		SET_TX_DESC_OFFSET(pdesc, USB_HWDESC_HEADER_LEN);
871
872	SET_TX_DESC_TX_RATE(pdesc, DESC92C_RATE1M);
873
874	SET_TX_DESC_SEQ(pdesc, 0);
875
876	SET_TX_DESC_LINIP(pdesc, 0);
877
878	SET_TX_DESC_QUEUE_SEL(pdesc, fw_queue);
879
880	SET_TX_DESC_FIRST_SEG(pdesc, 1);
881	SET_TX_DESC_LAST_SEG(pdesc, 1);
882
883	SET_TX_DESC_TX_BUFFER_SIZE(pdesc, (u16)(skb->len));
884
885	SET_TX_DESC_TX_BUFFER_ADDRESS(pdesc, mapping);
886
887	SET_TX_DESC_RATE_ID(pdesc, 7);
888	SET_TX_DESC_MACID(pdesc, 0);
889
890	SET_TX_DESC_OWN(pdesc, 1);
891
892	SET_TX_DESC_PKT_SIZE(pdesc, (u16)(skb->len));
893
894	SET_TX_DESC_FIRST_SEG(pdesc, 1);
895	SET_TX_DESC_LAST_SEG(pdesc, 1);
896
897	SET_TX_DESC_OFFSET(pdesc, 0x20);
898
899	SET_TX_DESC_USE_RATE(pdesc, 1);
900
901	if (!ieee80211_is_data_qos(fc))
902		SET_TX_DESC_HWSEQ_EN(pdesc, 1);
903
904	RT_PRINT_DATA(rtlpriv, COMP_CMD, DBG_LOUD,
905		      "H2C Tx Cmd Content\n",
906		      pdesc, TX_DESC_SIZE);
907}
908
909void rtl88ee_set_desc(struct ieee80211_hw *hw, u8 *pdesc,
910		      bool istx, u8 desc_name, u8 *val)
911{
912	if (istx == true) {
913		switch (desc_name) {
914		case HW_DESC_OWN:
915			SET_TX_DESC_OWN(pdesc, 1);
916			break;
917		case HW_DESC_TX_NEXTDESC_ADDR:
918			SET_TX_DESC_NEXT_DESC_ADDRESS(pdesc, *(u32 *)val);
919			break;
920		default:
921			RT_ASSERT(false, "ERR txdesc :%d not process\n",
922				  desc_name);
923			break;
924		}
925	} else {
926		switch (desc_name) {
927		case HW_DESC_RXOWN:
928			SET_RX_DESC_OWN(pdesc, 1);
929			break;
930		case HW_DESC_RXBUFF_ADDR:
931			SET_RX_DESC_BUFF_ADDR(pdesc, *(u32 *)val);
932			break;
933		case HW_DESC_RXPKT_LEN:
934			SET_RX_DESC_PKT_LEN(pdesc, *(u32 *)val);
935			break;
936		case HW_DESC_RXERO:
937			SET_RX_DESC_EOR(pdesc, 1);
938			break;
939		default:
940			RT_ASSERT(false, "ERR rxdesc :%d not process\n",
941				  desc_name);
942			break;
943		}
944	}
945}
946
947u32 rtl88ee_get_desc(u8 *pdesc, bool istx, u8 desc_name)
948{
949	u32 ret = 0;
950
951	if (istx == true) {
952		switch (desc_name) {
953		case HW_DESC_OWN:
954			ret = GET_TX_DESC_OWN(pdesc);
955			break;
956		case HW_DESC_TXBUFF_ADDR:
957			ret = GET_TX_DESC_TX_BUFFER_ADDRESS(pdesc);
958			break;
959		default:
960			RT_ASSERT(false, "ERR txdesc :%d not process\n",
961				  desc_name);
962			break;
963		}
964	} else {
965		switch (desc_name) {
966		case HW_DESC_OWN:
967			ret = GET_RX_DESC_OWN(pdesc);
968			break;
969		case HW_DESC_RXPKT_LEN:
970			ret = GET_RX_DESC_PKT_LEN(pdesc);
971			break;
972		case HW_DESC_RXBUFF_ADDR:
973			ret = GET_RX_DESC_BUFF_ADDR(pdesc);
974			break;
975		default:
976			RT_ASSERT(false, "ERR rxdesc :%d not process\n",
977				  desc_name);
978			break;
979		}
980	}
981	return ret;
982}
983
984bool rtl88ee_is_tx_desc_closed(struct ieee80211_hw *hw, u8 hw_queue, u16 index)
985{
986	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
987	struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[hw_queue];
988	u8 *entry = (u8 *)(&ring->desc[ring->idx]);
989	u8 own = (u8)rtl88ee_get_desc(entry, true, HW_DESC_OWN);
990
991	/*beacon packet will only use the first
992	 *descriptor defautly,and the own may not
993	 *be cleared by the hardware
994	 */
995	if (own)
996		return false;
997	return true;
998}
999
1000void rtl88ee_tx_polling(struct ieee80211_hw *hw, u8 hw_queue)
1001{
1002	struct rtl_priv *rtlpriv = rtl_priv(hw);
1003	if (hw_queue == BEACON_QUEUE) {
1004		rtl_write_word(rtlpriv, REG_PCIE_CTRL_REG, BIT(4));
1005	} else {
1006		rtl_write_word(rtlpriv, REG_PCIE_CTRL_REG,
1007			       BIT(0) << (hw_queue));
1008	}
1009}
1010
1011u32 rtl88ee_rx_command_packet(struct ieee80211_hw *hw,
1012			      struct rtl_stats status,
1013			      struct sk_buff *skb)
1014{
1015	return 0;
1016}
1017