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