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, __le32 *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, __le32 *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 				       __le32 *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, __le32 *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, __le32 *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, __le32 *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 *pdesc8, 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 	__le32 *pdesc = (__le32 *)pdesc8;
441 	u32 phystatus = get_rx_desc_physt(pdesc);
442 
443 	status->length = (u16)get_rx_desc_pkt_len(pdesc);
444 	status->rx_drvinfo_size = (u8)get_rx_desc_drv_info_size(pdesc) *
445 	    RX_DRV_INFO_SIZE_UNIT;
446 	status->rx_bufshift = (u8)(get_rx_desc_shift(pdesc) & 0x03);
447 	status->icv = (u16)get_rx_desc_icv(pdesc);
448 	status->crc = (u16)get_rx_desc_crc32(pdesc);
449 	status->hwerror = (status->crc | status->icv);
450 	status->decrypted = !get_rx_desc_swdec(pdesc);
451 	status->rate = (u8)get_rx_desc_rxmcs(pdesc);
452 	status->shortpreamble = (u16)get_rx_desc_splcp(pdesc);
453 	status->isampdu = (bool)(get_rx_desc_paggr(pdesc) == 1);
454 	status->isfirst_ampdu = (bool)(get_rx_desc_paggr(pdesc) == 1);
455 	status->timestamp_low = get_rx_desc_tsfl(pdesc);
456 	status->rx_packet_bw = get_rx_desc_bw(pdesc);
457 	status->macid = get_rx_desc_macid(pdesc);
458 	status->is_short_gi = !(bool)get_rx_desc_splcp(pdesc);
459 	status->is_ht = rtl8821ae_get_rxdesc_is_ht(hw, pdesc);
460 	status->is_vht = rtl8821ae_get_rxdesc_is_vht(hw, pdesc);
461 	status->vht_nss = rtl8821ae_get_rx_vht_nss(hw, pdesc);
462 	status->is_cck = RTL8821AE_RX_HAL_IS_CCK_RATE(status->rate);
463 
464 	RT_TRACE(rtlpriv, COMP_RXDESC, DBG_LOUD,
465 		 "rx_packet_bw=%s,is_ht %d, is_vht %d, vht_nss=%d,is_short_gi %d.\n",
466 		 (status->rx_packet_bw == 2) ? "80M" :
467 		 (status->rx_packet_bw == 1) ? "40M" : "20M",
468 		 status->is_ht, status->is_vht, status->vht_nss,
469 		 status->is_short_gi);
470 
471 	if (get_rx_status_desc_rpt_sel(pdesc))
472 		status->packet_report_type = C2H_PACKET;
473 	else
474 		status->packet_report_type = NORMAL_RX;
475 
476 	if (get_rx_status_desc_pattern_match(pdesc))
477 		wake_match = BIT(2);
478 	else if (get_rx_status_desc_magic_match(pdesc))
479 		wake_match = BIT(1);
480 	else if (get_rx_status_desc_unicast_match(pdesc))
481 		wake_match = BIT(0);
482 	else
483 		wake_match = 0;
484 
485 	if (wake_match)
486 		RT_TRACE(rtlpriv, COMP_RXDESC, DBG_LOUD,
487 			 "GGGGGGGGGGGGGet Wakeup Packet!! WakeMatch=%d\n",
488 			 wake_match);
489 	rx_status->freq = hw->conf.chandef.chan->center_freq;
490 	rx_status->band = hw->conf.chandef.chan->band;
491 
492 	hdr = (struct ieee80211_hdr *)(skb->data +
493 	      status->rx_drvinfo_size + status->rx_bufshift);
494 
495 	if (status->crc)
496 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
497 
498 	if (status->rx_packet_bw == HT_CHANNEL_WIDTH_20_40)
499 		rx_status->bw = RATE_INFO_BW_40;
500 	else if (status->rx_packet_bw == HT_CHANNEL_WIDTH_80)
501 		rx_status->bw = RATE_INFO_BW_80;
502 	if (status->is_ht)
503 		rx_status->encoding = RX_ENC_HT;
504 	if (status->is_vht)
505 		rx_status->encoding = RX_ENC_VHT;
506 
507 	if (status->is_short_gi)
508 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
509 
510 	rx_status->nss = status->vht_nss;
511 	rx_status->flag |= RX_FLAG_MACTIME_START;
512 
513 	/* hw will set status->decrypted true, if it finds the
514 	 * frame is open data frame or mgmt frame.
515 	 * So hw will not decryption robust managment frame
516 	 * for IEEE80211w but still set status->decrypted
517 	 * true, so here we should set it back to undecrypted
518 	 * for IEEE80211w frame, and mac80211 sw will help
519 	 * to decrypt it
520 	 */
521 	if (status->decrypted) {
522 		if ((!_ieee80211_is_robust_mgmt_frame(hdr)) &&
523 		    (ieee80211_has_protected(hdr->frame_control)))
524 			rx_status->flag |= RX_FLAG_DECRYPTED;
525 		else
526 			rx_status->flag &= ~RX_FLAG_DECRYPTED;
527 	}
528 
529 	/* rate_idx: index of data rate into band's
530 	 * supported rates or MCS index if HT rates
531 	 * are use (RX_FLAG_HT)
532 	 */
533 	rx_status->rate_idx = rtlwifi_rate_mapping(hw, status->is_ht,
534 						   status->is_vht,
535 						   status->rate);
536 
537 	rx_status->mactime = status->timestamp_low;
538 	if (phystatus) {
539 		p_drvinfo = (struct rx_fwinfo_8821ae *)(skb->data +
540 			    status->rx_bufshift);
541 
542 		translate_rx_signal_stuff(hw, skb, status, pdesc, p_drvinfo);
543 	}
544 	rx_status->signal = status->recvsignalpower + 10;
545 	if (status->packet_report_type == TX_REPORT2) {
546 		status->macid_valid_entry[0] =
547 		  get_rx_rpt2_desc_macid_valid_1(pdesc);
548 		status->macid_valid_entry[1] =
549 		  get_rx_rpt2_desc_macid_valid_2(pdesc);
550 	}
551 	return true;
552 }
553 
554 static u8 rtl8821ae_bw_mapping(struct ieee80211_hw *hw,
555 			       struct rtl_tcb_desc *ptcb_desc)
556 {
557 	struct rtl_priv *rtlpriv = rtl_priv(hw);
558 	struct rtl_phy *rtlphy = &rtlpriv->phy;
559 	u8 bw_setting_of_desc = 0;
560 
561 	RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
562 		 "rtl8821ae_bw_mapping, current_chan_bw %d, packet_bw %d\n",
563 		 rtlphy->current_chan_bw, ptcb_desc->packet_bw);
564 
565 	if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_80) {
566 		if (ptcb_desc->packet_bw == HT_CHANNEL_WIDTH_80)
567 			bw_setting_of_desc = 2;
568 		else if (ptcb_desc->packet_bw == HT_CHANNEL_WIDTH_20_40)
569 			bw_setting_of_desc = 1;
570 		else
571 			bw_setting_of_desc = 0;
572 	} else if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40) {
573 		if ((ptcb_desc->packet_bw == HT_CHANNEL_WIDTH_20_40) ||
574 		    (ptcb_desc->packet_bw == HT_CHANNEL_WIDTH_80))
575 			bw_setting_of_desc = 1;
576 		else
577 			bw_setting_of_desc = 0;
578 	} else {
579 		bw_setting_of_desc = 0;
580 	}
581 	return bw_setting_of_desc;
582 }
583 
584 static u8 rtl8821ae_sc_mapping(struct ieee80211_hw *hw,
585 			       struct rtl_tcb_desc *ptcb_desc)
586 {
587 	struct rtl_priv *rtlpriv = rtl_priv(hw);
588 	struct rtl_phy *rtlphy = &rtlpriv->phy;
589 	struct rtl_mac *mac = rtl_mac(rtlpriv);
590 	u8 sc_setting_of_desc = 0;
591 
592 	if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_80) {
593 		if (ptcb_desc->packet_bw == HT_CHANNEL_WIDTH_80) {
594 			sc_setting_of_desc = VHT_DATA_SC_DONOT_CARE;
595 		} else if (ptcb_desc->packet_bw == HT_CHANNEL_WIDTH_20_40) {
596 			if (mac->cur_80_prime_sc ==
597 			    HAL_PRIME_CHNL_OFFSET_LOWER)
598 				sc_setting_of_desc =
599 					VHT_DATA_SC_40_LOWER_OF_80MHZ;
600 			else if (mac->cur_80_prime_sc ==
601 				 HAL_PRIME_CHNL_OFFSET_UPPER)
602 				sc_setting_of_desc =
603 					VHT_DATA_SC_40_UPPER_OF_80MHZ;
604 			else
605 				RT_TRACE(rtlpriv, COMP_SEND, DBG_LOUD,
606 					 "rtl8821ae_sc_mapping: Not Correct Primary40MHz Setting\n");
607 		} else {
608 			if ((mac->cur_40_prime_sc ==
609 			     HAL_PRIME_CHNL_OFFSET_LOWER) &&
610 			    (mac->cur_80_prime_sc  ==
611 			     HAL_PRIME_CHNL_OFFSET_LOWER))
612 				sc_setting_of_desc =
613 					VHT_DATA_SC_20_LOWEST_OF_80MHZ;
614 			else if ((mac->cur_40_prime_sc ==
615 				  HAL_PRIME_CHNL_OFFSET_UPPER) &&
616 				 (mac->cur_80_prime_sc ==
617 				  HAL_PRIME_CHNL_OFFSET_LOWER))
618 				sc_setting_of_desc =
619 					VHT_DATA_SC_20_LOWER_OF_80MHZ;
620 			else if ((mac->cur_40_prime_sc ==
621 				  HAL_PRIME_CHNL_OFFSET_LOWER) &&
622 				 (mac->cur_80_prime_sc ==
623 				  HAL_PRIME_CHNL_OFFSET_UPPER))
624 				sc_setting_of_desc =
625 					VHT_DATA_SC_20_UPPER_OF_80MHZ;
626 			else if ((mac->cur_40_prime_sc ==
627 				  HAL_PRIME_CHNL_OFFSET_UPPER) &&
628 				 (mac->cur_80_prime_sc ==
629 				  HAL_PRIME_CHNL_OFFSET_UPPER))
630 				sc_setting_of_desc =
631 					VHT_DATA_SC_20_UPPERST_OF_80MHZ;
632 			else
633 				RT_TRACE(rtlpriv, COMP_SEND, DBG_LOUD,
634 					 "rtl8821ae_sc_mapping: Not Correct Primary40MHz Setting\n");
635 		}
636 	} else if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40) {
637 		if (ptcb_desc->packet_bw == HT_CHANNEL_WIDTH_20_40) {
638 			sc_setting_of_desc = VHT_DATA_SC_DONOT_CARE;
639 		} else if (ptcb_desc->packet_bw == HT_CHANNEL_WIDTH_20) {
640 			if (mac->cur_40_prime_sc ==
641 				HAL_PRIME_CHNL_OFFSET_UPPER) {
642 				sc_setting_of_desc =
643 					VHT_DATA_SC_20_UPPER_OF_80MHZ;
644 			} else if (mac->cur_40_prime_sc ==
645 				HAL_PRIME_CHNL_OFFSET_LOWER){
646 				sc_setting_of_desc =
647 					VHT_DATA_SC_20_LOWER_OF_80MHZ;
648 			} else {
649 				sc_setting_of_desc = VHT_DATA_SC_DONOT_CARE;
650 			}
651 		}
652 	} else {
653 		sc_setting_of_desc = VHT_DATA_SC_DONOT_CARE;
654 	}
655 
656 	return sc_setting_of_desc;
657 }
658 
659 void rtl8821ae_tx_fill_desc(struct ieee80211_hw *hw,
660 			    struct ieee80211_hdr *hdr, u8 *pdesc8, u8 *txbd,
661 			    struct ieee80211_tx_info *info,
662 			    struct ieee80211_sta *sta,
663 			    struct sk_buff *skb,
664 			    u8 hw_queue, struct rtl_tcb_desc *ptcb_desc)
665 {
666 	struct rtl_priv *rtlpriv = rtl_priv(hw);
667 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
668 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
669 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
670 	struct rtlwifi_tx_info *tx_info = rtl_tx_skb_cb_info(skb);
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 	bool tmp_bool;
683 	__le32 *pdesc = (__le32 *)pdesc8;
684 
685 	seq_number = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4;
686 	rtl_get_tcb_desc(hw, info, sta, skb, ptcb_desc);
687 	/* reserve 8 byte for AMPDU early mode */
688 	if (rtlhal->earlymode_enable) {
689 		skb_push(skb, EM_HDR_LEN);
690 		memset(skb->data, 0, EM_HDR_LEN);
691 	}
692 	buf_len = skb->len;
693 	mapping = pci_map_single(rtlpci->pdev, skb->data, skb->len,
694 				 PCI_DMA_TODEVICE);
695 	if (pci_dma_mapping_error(rtlpci->pdev, mapping)) {
696 		RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
697 			 "DMA mapping error\n");
698 		return;
699 	}
700 	clear_pci_tx_desc_content(pdesc, sizeof(struct tx_desc_8821ae));
701 	if (ieee80211_is_nullfunc(fc) || ieee80211_is_ctl(fc)) {
702 		firstseg = true;
703 		lastseg = true;
704 	}
705 	if (firstseg) {
706 		if (rtlhal->earlymode_enable) {
707 			set_tx_desc_pkt_offset(pdesc, 1);
708 			set_tx_desc_offset(pdesc, USB_HWDESC_HEADER_LEN +
709 					   EM_HDR_LEN);
710 			if (ptcb_desc->empkt_num) {
711 				RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
712 					 "Insert 8 byte.pTcb->EMPktNum:%d\n",
713 					  ptcb_desc->empkt_num);
714 				rtl8821ae_insert_emcontent(ptcb_desc,
715 							   (__le32 *)skb->data);
716 			}
717 		} else {
718 			set_tx_desc_offset(pdesc, USB_HWDESC_HEADER_LEN);
719 		}
720 
721 
722 		/* ptcb_desc->use_driver_rate = true; */
723 		set_tx_desc_tx_rate(pdesc, ptcb_desc->hw_rate);
724 		if (ptcb_desc->hw_rate > DESC_RATEMCS0)
725 			short_gi = (ptcb_desc->use_shortgi) ? 1 : 0;
726 		else
727 			short_gi = (ptcb_desc->use_shortpreamble) ? 1 : 0;
728 
729 		set_tx_desc_data_shortgi(pdesc, short_gi);
730 
731 		if (info->flags & IEEE80211_TX_CTL_AMPDU) {
732 			set_tx_desc_agg_enable(pdesc, 1);
733 			set_tx_desc_max_agg_num(pdesc, 0x1f);
734 		}
735 		set_tx_desc_seq(pdesc, seq_number);
736 		set_tx_desc_rts_enable(pdesc,
737 				       ((ptcb_desc->rts_enable &&
738 					!ptcb_desc->cts_enable) ? 1 : 0));
739 		set_tx_desc_hw_rts_enable(pdesc, 0);
740 		set_tx_desc_cts2self(pdesc, ((ptcb_desc->cts_enable) ? 1 : 0));
741 
742 		set_tx_desc_rts_rate(pdesc, ptcb_desc->rts_rate);
743 		set_tx_desc_rts_sc(pdesc, ptcb_desc->rts_sc);
744 		tmp_bool = ((ptcb_desc->rts_rate <= DESC_RATE54M) ?
745 			    (ptcb_desc->rts_use_shortpreamble ? 1 : 0) :
746 			    (ptcb_desc->rts_use_shortgi ? 1 : 0));
747 		set_tx_desc_rts_short(pdesc, tmp_bool);
748 
749 		if (ptcb_desc->tx_enable_sw_calc_duration)
750 			set_tx_desc_nav_use_hdr(pdesc, 1);
751 
752 		set_tx_desc_data_bw(pdesc,
753 				    rtl8821ae_bw_mapping(hw, ptcb_desc));
754 
755 		set_tx_desc_tx_sub_carrier(pdesc,
756 					   rtl8821ae_sc_mapping(hw, ptcb_desc));
757 
758 		set_tx_desc_linip(pdesc, 0);
759 		set_tx_desc_pkt_size(pdesc, (u16)skb_len);
760 		if (sta) {
761 			u8 ampdu_density = sta->ht_cap.ampdu_density;
762 
763 			set_tx_desc_ampdu_density(pdesc, ampdu_density);
764 		}
765 		if (info->control.hw_key) {
766 			struct ieee80211_key_conf *keyconf =
767 				info->control.hw_key;
768 			switch (keyconf->cipher) {
769 			case WLAN_CIPHER_SUITE_WEP40:
770 			case WLAN_CIPHER_SUITE_WEP104:
771 			case WLAN_CIPHER_SUITE_TKIP:
772 				set_tx_desc_sec_type(pdesc, 0x1);
773 				break;
774 			case WLAN_CIPHER_SUITE_CCMP:
775 				set_tx_desc_sec_type(pdesc, 0x3);
776 				break;
777 			default:
778 				set_tx_desc_sec_type(pdesc, 0x0);
779 				break;
780 			}
781 		}
782 
783 		set_tx_desc_queue_sel(pdesc, fw_qsel);
784 		set_tx_desc_data_rate_fb_limit(pdesc, 0x1F);
785 		set_tx_desc_rts_rate_fb_limit(pdesc, 0xF);
786 		set_tx_desc_disable_fb(pdesc, ptcb_desc->disable_ratefallback ?
787 				       1 : 0);
788 		set_tx_desc_use_rate(pdesc, ptcb_desc->use_driver_rate ? 1 : 0);
789 
790 		if (ieee80211_is_data_qos(fc)) {
791 			if (mac->rdg_en) {
792 				RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
793 					 "Enable RDG function.\n");
794 				set_tx_desc_rdg_enable(pdesc, 1);
795 				set_tx_desc_htc(pdesc, 1);
796 			}
797 		}
798 		/* tx report */
799 		rtl_set_tx_report(ptcb_desc, pdesc8, hw, tx_info);
800 	}
801 
802 	set_tx_desc_first_seg(pdesc, (firstseg ? 1 : 0));
803 	set_tx_desc_last_seg(pdesc, (lastseg ? 1 : 0));
804 	set_tx_desc_tx_buffer_size(pdesc, buf_len);
805 	set_tx_desc_tx_buffer_address(pdesc, mapping);
806 	/* if (rtlpriv->dm.useramask) { */
807 	if (1) {
808 		set_tx_desc_rate_id(pdesc, ptcb_desc->ratr_index);
809 		set_tx_desc_macid(pdesc, ptcb_desc->mac_id);
810 	} else {
811 		set_tx_desc_rate_id(pdesc, 0xC + ptcb_desc->ratr_index);
812 		set_tx_desc_macid(pdesc, ptcb_desc->mac_id);
813 	}
814 	if (!ieee80211_is_data_qos(fc))  {
815 		set_tx_desc_hwseq_en(pdesc, 1);
816 		set_tx_desc_hwseq_sel(pdesc, 0);
817 	}
818 	set_tx_desc_more_frag(pdesc, (lastseg ? 0 : 1));
819 	if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
820 	    is_broadcast_ether_addr(ieee80211_get_DA(hdr))) {
821 		set_tx_desc_bmc(pdesc, 1);
822 	}
823 
824 	rtl8821ae_dm_set_tx_ant_by_tx_info(hw, pdesc8, ptcb_desc->mac_id);
825 	RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE, "\n");
826 }
827 
828 void rtl8821ae_tx_fill_cmddesc(struct ieee80211_hw *hw,
829 			       u8 *pdesc8, bool firstseg,
830 			       bool lastseg, struct sk_buff *skb)
831 {
832 	struct rtl_priv *rtlpriv = rtl_priv(hw);
833 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
834 	u8 fw_queue = QSLT_BEACON;
835 	__le32 *pdesc = (__le32 *)pdesc8;
836 
837 	dma_addr_t mapping = pci_map_single(rtlpci->pdev,
838 					    skb->data, skb->len,
839 					    PCI_DMA_TODEVICE);
840 
841 	if (pci_dma_mapping_error(rtlpci->pdev, mapping)) {
842 		RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
843 			 "DMA mapping error\n");
844 		return;
845 	}
846 	clear_pci_tx_desc_content(pdesc, TX_DESC_SIZE);
847 
848 	set_tx_desc_first_seg(pdesc, 1);
849 	set_tx_desc_last_seg(pdesc, 1);
850 
851 	set_tx_desc_pkt_size(pdesc, (u16)(skb->len));
852 
853 	set_tx_desc_offset(pdesc, USB_HWDESC_HEADER_LEN);
854 
855 	set_tx_desc_use_rate(pdesc, 1);
856 	set_tx_desc_tx_rate(pdesc, DESC_RATE1M);
857 	set_tx_desc_disable_fb(pdesc, 1);
858 
859 	set_tx_desc_data_bw(pdesc, 0);
860 
861 	set_tx_desc_hwseq_en(pdesc, 1);
862 
863 	set_tx_desc_queue_sel(pdesc, fw_queue);
864 
865 	set_tx_desc_tx_buffer_size(pdesc, skb->len);
866 
867 	set_tx_desc_tx_buffer_address(pdesc, mapping);
868 
869 	set_tx_desc_macid(pdesc, 0);
870 
871 	set_tx_desc_own(pdesc, 1);
872 
873 	RT_PRINT_DATA(rtlpriv, COMP_CMD, DBG_LOUD,
874 		      "H2C Tx Cmd Content\n",
875 		      pdesc8, TX_DESC_SIZE);
876 }
877 
878 void rtl8821ae_set_desc(struct ieee80211_hw *hw, u8 *pdesc8,
879 			bool istx, u8 desc_name, u8 *val)
880 {
881 	__le32 *pdesc = (__le32 *)pdesc8;
882 
883 	if (istx) {
884 		switch (desc_name) {
885 		case HW_DESC_OWN:
886 			set_tx_desc_own(pdesc, 1);
887 			break;
888 		case HW_DESC_TX_NEXTDESC_ADDR:
889 			set_tx_desc_next_desc_address(pdesc, *(u32 *)val);
890 			break;
891 		default:
892 			WARN_ONCE(true,
893 				  "rtl8821ae: ERR txdesc :%d not processed\n",
894 				  desc_name);
895 			break;
896 		}
897 	} else {
898 		switch (desc_name) {
899 		case HW_DESC_RXOWN:
900 			set_rx_desc_own(pdesc, 1);
901 			break;
902 		case HW_DESC_RXBUFF_ADDR:
903 			set_rx_desc_buff_addr(pdesc, *(u32 *)val);
904 			break;
905 		case HW_DESC_RXPKT_LEN:
906 			set_rx_desc_pkt_len(pdesc, *(u32 *)val);
907 			break;
908 		case HW_DESC_RXERO:
909 			set_rx_desc_eor(pdesc, 1);
910 			break;
911 		default:
912 			WARN_ONCE(true,
913 				  "rtl8821ae: ERR rxdesc :%d not processed\n",
914 				  desc_name);
915 			break;
916 		}
917 	}
918 }
919 
920 u64 rtl8821ae_get_desc(struct ieee80211_hw *hw,
921 		       u8 *pdesc8, bool istx, u8 desc_name)
922 {
923 	u32 ret = 0;
924 	__le32 *pdesc = (__le32 *)pdesc8;
925 
926 	if (istx) {
927 		switch (desc_name) {
928 		case HW_DESC_OWN:
929 			ret = get_tx_desc_own(pdesc);
930 			break;
931 		case HW_DESC_TXBUFF_ADDR:
932 			ret = get_tx_desc_tx_buffer_address(pdesc);
933 			break;
934 		default:
935 			WARN_ONCE(true,
936 				  "rtl8821ae: ERR txdesc :%d not processed\n",
937 				  desc_name);
938 			break;
939 		}
940 	} else {
941 		switch (desc_name) {
942 		case HW_DESC_OWN:
943 			ret = get_rx_desc_own(pdesc);
944 			break;
945 		case HW_DESC_RXPKT_LEN:
946 			ret = get_rx_desc_pkt_len(pdesc);
947 			break;
948 		case HW_DESC_RXBUFF_ADDR:
949 			ret = get_rx_desc_buff_addr(pdesc);
950 			break;
951 		default:
952 			WARN_ONCE(true,
953 				  "rtl8821ae: ERR rxdesc :%d not processed\n",
954 				  desc_name);
955 			break;
956 		}
957 	}
958 	return ret;
959 }
960 
961 bool rtl8821ae_is_tx_desc_closed(struct ieee80211_hw *hw,
962 				 u8 hw_queue, u16 index)
963 {
964 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
965 	struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[hw_queue];
966 	u8 *entry = (u8 *)(&ring->desc[ring->idx]);
967 	u8 own = (u8)rtl8821ae_get_desc(hw, entry, true, HW_DESC_OWN);
968 
969 	/**
970 	 *beacon packet will only use the first
971 	 *descriptor defautly,and the own may not
972 	 *be cleared by the hardware
973 	 */
974 	if (own)
975 		return false;
976 	return true;
977 }
978 
979 void rtl8821ae_tx_polling(struct ieee80211_hw *hw, u8 hw_queue)
980 {
981 	struct rtl_priv *rtlpriv = rtl_priv(hw);
982 
983 	if (hw_queue == BEACON_QUEUE) {
984 		rtl_write_word(rtlpriv, REG_PCIE_CTRL_REG, BIT(4));
985 	} else {
986 		rtl_write_word(rtlpriv, REG_PCIE_CTRL_REG,
987 			       BIT(0) << (hw_queue));
988 	}
989 }
990