1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009-2012  Realtek Corporation.*/
3 
4 #include "../wifi.h"
5 #include "../pci.h"
6 #include "../base.h"
7 #include "reg.h"
8 #include "def.h"
9 #include "phy.h"
10 #include "trx.h"
11 #include "led.h"
12 
13 static u8 _rtl92de_map_hwqueue_to_fwqueue(struct sk_buff *skb, u8 hw_queue)
14 {
15 	__le16 fc = rtl_get_fc(skb);
16 
17 	if (unlikely(ieee80211_is_beacon(fc)))
18 		return QSLT_BEACON;
19 	if (ieee80211_is_mgmt(fc))
20 		return QSLT_MGNT;
21 
22 	return skb->priority;
23 }
24 
25 static u8 _rtl92d_query_rxpwrpercentage(s8 antpower)
26 {
27 	if ((antpower <= -100) || (antpower >= 20))
28 		return 0;
29 	else if (antpower >= 0)
30 		return 100;
31 	else
32 		return 100 + antpower;
33 }
34 
35 static u8 _rtl92d_evm_db_to_percentage(s8 value)
36 {
37 	s8 ret_val = value;
38 
39 	if (ret_val >= 0)
40 		ret_val = 0;
41 	if (ret_val <= -33)
42 		ret_val = -33;
43 	ret_val = 0 - ret_val;
44 	ret_val *= 3;
45 	if (ret_val == 99)
46 		ret_val = 100;
47 	return ret_val;
48 }
49 
50 static long _rtl92de_translate_todbm(struct ieee80211_hw *hw,
51 				     u8 signal_strength_index)
52 {
53 	long signal_power;
54 
55 	signal_power = (long)((signal_strength_index + 1) >> 1);
56 	signal_power -= 95;
57 	return signal_power;
58 }
59 
60 static long _rtl92de_signal_scale_mapping(struct ieee80211_hw *hw, long currsig)
61 {
62 	long retsig;
63 
64 	if (currsig >= 61 && currsig <= 100)
65 		retsig = 90 + ((currsig - 60) / 4);
66 	else if (currsig >= 41 && currsig <= 60)
67 		retsig = 78 + ((currsig - 40) / 2);
68 	else if (currsig >= 31 && currsig <= 40)
69 		retsig = 66 + (currsig - 30);
70 	else if (currsig >= 21 && currsig <= 30)
71 		retsig = 54 + (currsig - 20);
72 	else if (currsig >= 5 && currsig <= 20)
73 		retsig = 42 + (((currsig - 5) * 2) / 3);
74 	else if (currsig == 4)
75 		retsig = 36;
76 	else if (currsig == 3)
77 		retsig = 27;
78 	else if (currsig == 2)
79 		retsig = 18;
80 	else if (currsig == 1)
81 		retsig = 9;
82 	else
83 		retsig = currsig;
84 	return retsig;
85 }
86 
87 static void _rtl92de_query_rxphystatus(struct ieee80211_hw *hw,
88 				       struct rtl_stats *pstats,
89 				       struct rx_desc_92d *pdesc,
90 				       struct rx_fwinfo_92d *p_drvinfo,
91 				       bool packet_match_bssid,
92 				       bool packet_toself,
93 				       bool packet_beacon)
94 {
95 	struct rtl_priv *rtlpriv = rtl_priv(hw);
96 	struct rtl_ps_ctl *ppsc = rtl_psc(rtlpriv);
97 	struct phy_sts_cck_8192d *cck_buf;
98 	s8 rx_pwr_all, rx_pwr[4];
99 	u8 rf_rx_num = 0, evm, pwdb_all;
100 	u8 i, max_spatial_stream;
101 	u32 rssi, total_rssi = 0;
102 	bool is_cck_rate;
103 
104 	is_cck_rate = RX_HAL_IS_CCK_RATE(pdesc->rxmcs);
105 	pstats->packet_matchbssid = packet_match_bssid;
106 	pstats->packet_toself = packet_toself;
107 	pstats->packet_beacon = packet_beacon;
108 	pstats->is_cck = is_cck_rate;
109 	pstats->rx_mimo_sig_qual[0] = -1;
110 	pstats->rx_mimo_sig_qual[1] = -1;
111 
112 	if (is_cck_rate) {
113 		u8 report, cck_highpwr;
114 		cck_buf = (struct phy_sts_cck_8192d *)p_drvinfo;
115 		if (ppsc->rfpwr_state == ERFON)
116 			cck_highpwr = (u8) rtl_get_bbreg(hw,
117 						 RFPGA0_XA_HSSIPARAMETER2,
118 						 BIT(9));
119 		else
120 			cck_highpwr = false;
121 		if (!cck_highpwr) {
122 			u8 cck_agc_rpt = cck_buf->cck_agc_rpt;
123 			report = cck_buf->cck_agc_rpt & 0xc0;
124 			report = report >> 6;
125 			switch (report) {
126 			case 0x3:
127 				rx_pwr_all = -46 - (cck_agc_rpt & 0x3e);
128 				break;
129 			case 0x2:
130 				rx_pwr_all = -26 - (cck_agc_rpt & 0x3e);
131 				break;
132 			case 0x1:
133 				rx_pwr_all = -12 - (cck_agc_rpt & 0x3e);
134 				break;
135 			case 0x0:
136 				rx_pwr_all = 16 - (cck_agc_rpt & 0x3e);
137 				break;
138 			}
139 		} else {
140 			u8 cck_agc_rpt = cck_buf->cck_agc_rpt;
141 			report = p_drvinfo->cfosho[0] & 0x60;
142 			report = report >> 5;
143 			switch (report) {
144 			case 0x3:
145 				rx_pwr_all = -46 - ((cck_agc_rpt & 0x1f) << 1);
146 				break;
147 			case 0x2:
148 				rx_pwr_all = -26 - ((cck_agc_rpt & 0x1f) << 1);
149 				break;
150 			case 0x1:
151 				rx_pwr_all = -12 - ((cck_agc_rpt & 0x1f) << 1);
152 				break;
153 			case 0x0:
154 				rx_pwr_all = 16 - ((cck_agc_rpt & 0x1f) << 1);
155 				break;
156 			}
157 		}
158 		pwdb_all = _rtl92d_query_rxpwrpercentage(rx_pwr_all);
159 		/* CCK gain is smaller than OFDM/MCS gain,  */
160 		/* so we add gain diff by experiences, the val is 6 */
161 		pwdb_all += 6;
162 		if (pwdb_all > 100)
163 			pwdb_all = 100;
164 		/* modify the offset to make the same gain index with OFDM. */
165 		if (pwdb_all > 34 && pwdb_all <= 42)
166 			pwdb_all -= 2;
167 		else if (pwdb_all > 26 && pwdb_all <= 34)
168 			pwdb_all -= 6;
169 		else if (pwdb_all > 14 && pwdb_all <= 26)
170 			pwdb_all -= 8;
171 		else if (pwdb_all > 4 && pwdb_all <= 14)
172 			pwdb_all -= 4;
173 		pstats->rx_pwdb_all = pwdb_all;
174 		pstats->recvsignalpower = rx_pwr_all;
175 		if (packet_match_bssid) {
176 			u8 sq;
177 			if (pstats->rx_pwdb_all > 40) {
178 				sq = 100;
179 			} else {
180 				sq = cck_buf->sq_rpt;
181 				if (sq > 64)
182 					sq = 0;
183 				else if (sq < 20)
184 					sq = 100;
185 				else
186 					sq = ((64 - sq) * 100) / 44;
187 			}
188 			pstats->signalquality = sq;
189 			pstats->rx_mimo_sig_qual[0] = sq;
190 			pstats->rx_mimo_sig_qual[1] = -1;
191 		}
192 	} else {
193 		rtlpriv->dm.rfpath_rxenable[0] = true;
194 		rtlpriv->dm.rfpath_rxenable[1] = true;
195 		for (i = RF90_PATH_A; i < RF6052_MAX_PATH; i++) {
196 			if (rtlpriv->dm.rfpath_rxenable[i])
197 				rf_rx_num++;
198 			rx_pwr[i] = ((p_drvinfo->gain_trsw[i] & 0x3f) * 2)
199 				    - 110;
200 			rssi = _rtl92d_query_rxpwrpercentage(rx_pwr[i]);
201 			total_rssi += rssi;
202 			rtlpriv->stats.rx_snr_db[i] =
203 					 (long)(p_drvinfo->rxsnr[i] / 2);
204 			if (packet_match_bssid)
205 				pstats->rx_mimo_signalstrength[i] = (u8) rssi;
206 		}
207 		rx_pwr_all = ((p_drvinfo->pwdb_all >> 1) & 0x7f) - 106;
208 		pwdb_all = _rtl92d_query_rxpwrpercentage(rx_pwr_all);
209 		pstats->rx_pwdb_all = pwdb_all;
210 		pstats->rxpower = rx_pwr_all;
211 		pstats->recvsignalpower = rx_pwr_all;
212 		if (pdesc->rxht && pdesc->rxmcs >= DESC_RATEMCS8 &&
213 		    pdesc->rxmcs <= DESC_RATEMCS15)
214 			max_spatial_stream = 2;
215 		else
216 			max_spatial_stream = 1;
217 		for (i = 0; i < max_spatial_stream; i++) {
218 			evm = _rtl92d_evm_db_to_percentage(p_drvinfo->rxevm[i]);
219 			if (packet_match_bssid) {
220 				if (i == 0)
221 					pstats->signalquality =
222 						 (u8)(evm & 0xff);
223 				pstats->rx_mimo_sig_qual[i] =
224 						 (u8)(evm & 0xff);
225 			}
226 		}
227 	}
228 	if (is_cck_rate)
229 		pstats->signalstrength = (u8)(_rtl92de_signal_scale_mapping(hw,
230 				pwdb_all));
231 	else if (rf_rx_num != 0)
232 		pstats->signalstrength = (u8)(_rtl92de_signal_scale_mapping(hw,
233 				total_rssi /= rf_rx_num));
234 }
235 
236 static void rtl92d_loop_over_paths(struct ieee80211_hw *hw,
237 				   struct rtl_stats *pstats)
238 {
239 	struct rtl_priv *rtlpriv = rtl_priv(hw);
240 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
241 	u8 rfpath;
242 
243 	for (rfpath = RF90_PATH_A; rfpath < rtlphy->num_total_rfpath;
244 	     rfpath++) {
245 		if (rtlpriv->stats.rx_rssi_percentage[rfpath] == 0) {
246 			rtlpriv->stats.rx_rssi_percentage[rfpath] =
247 			    pstats->rx_mimo_signalstrength[rfpath];
248 
249 		}
250 		if (pstats->rx_mimo_signalstrength[rfpath] >
251 		    rtlpriv->stats.rx_rssi_percentage[rfpath]) {
252 			rtlpriv->stats.rx_rssi_percentage[rfpath] =
253 			    ((rtlpriv->stats.rx_rssi_percentage[rfpath] *
254 			      (RX_SMOOTH_FACTOR - 1)) +
255 			     (pstats->rx_mimo_signalstrength[rfpath])) /
256 			    (RX_SMOOTH_FACTOR);
257 			rtlpriv->stats.rx_rssi_percentage[rfpath] =
258 			    rtlpriv->stats.rx_rssi_percentage[rfpath] + 1;
259 		} else {
260 			rtlpriv->stats.rx_rssi_percentage[rfpath] =
261 			    ((rtlpriv->stats.rx_rssi_percentage[rfpath] *
262 			      (RX_SMOOTH_FACTOR - 1)) +
263 			     (pstats->rx_mimo_signalstrength[rfpath])) /
264 			    (RX_SMOOTH_FACTOR);
265 		}
266 	}
267 }
268 
269 static void _rtl92de_process_ui_rssi(struct ieee80211_hw *hw,
270 				     struct rtl_stats *pstats)
271 {
272 	struct rtl_priv *rtlpriv = rtl_priv(hw);
273 	u32 last_rssi, tmpval;
274 
275 	if (pstats->packet_toself || pstats->packet_beacon) {
276 		rtlpriv->stats.rssi_calculate_cnt++;
277 		if (rtlpriv->stats.ui_rssi.total_num++ >=
278 		    PHY_RSSI_SLID_WIN_MAX) {
279 			rtlpriv->stats.ui_rssi.total_num =
280 						 PHY_RSSI_SLID_WIN_MAX;
281 			last_rssi = rtlpriv->stats.ui_rssi.elements[
282 				rtlpriv->stats.ui_rssi.index];
283 			rtlpriv->stats.ui_rssi.total_val -= last_rssi;
284 		}
285 		rtlpriv->stats.ui_rssi.total_val += pstats->signalstrength;
286 		rtlpriv->stats.ui_rssi.elements
287 			[rtlpriv->stats.ui_rssi.index++] =
288 			pstats->signalstrength;
289 		if (rtlpriv->stats.ui_rssi.index >= PHY_RSSI_SLID_WIN_MAX)
290 			rtlpriv->stats.ui_rssi.index = 0;
291 		tmpval = rtlpriv->stats.ui_rssi.total_val /
292 			rtlpriv->stats.ui_rssi.total_num;
293 		rtlpriv->stats.signal_strength = _rtl92de_translate_todbm(hw,
294 			(u8) tmpval);
295 		pstats->rssi = rtlpriv->stats.signal_strength;
296 	}
297 	if (!pstats->is_cck && pstats->packet_toself)
298 		rtl92d_loop_over_paths(hw, pstats);
299 }
300 
301 static void _rtl92de_update_rxsignalstatistics(struct ieee80211_hw *hw,
302 					       struct rtl_stats *pstats)
303 {
304 	struct rtl_priv *rtlpriv = rtl_priv(hw);
305 	int weighting = 0;
306 
307 	if (rtlpriv->stats.recv_signal_power == 0)
308 		rtlpriv->stats.recv_signal_power = pstats->recvsignalpower;
309 	if (pstats->recvsignalpower > rtlpriv->stats.recv_signal_power)
310 		weighting = 5;
311 	else if (pstats->recvsignalpower < rtlpriv->stats.recv_signal_power)
312 		weighting = (-5);
313 	rtlpriv->stats.recv_signal_power = (rtlpriv->stats.recv_signal_power *
314 		5 + pstats->recvsignalpower + weighting) / 6;
315 }
316 
317 static void _rtl92de_process_pwdb(struct ieee80211_hw *hw,
318 				  struct rtl_stats *pstats)
319 {
320 	struct rtl_priv *rtlpriv = rtl_priv(hw);
321 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
322 	long undec_sm_pwdb;
323 
324 	if (mac->opmode == NL80211_IFTYPE_ADHOC	||
325 		mac->opmode == NL80211_IFTYPE_AP)
326 		return;
327 	else
328 		undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb;
329 
330 	if (pstats->packet_toself || pstats->packet_beacon) {
331 		if (undec_sm_pwdb < 0)
332 			undec_sm_pwdb = pstats->rx_pwdb_all;
333 		if (pstats->rx_pwdb_all > (u32) undec_sm_pwdb) {
334 			undec_sm_pwdb = (((undec_sm_pwdb) *
335 			      (RX_SMOOTH_FACTOR - 1)) +
336 			      (pstats->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
337 			undec_sm_pwdb = undec_sm_pwdb + 1;
338 		} else {
339 			undec_sm_pwdb = (((undec_sm_pwdb) *
340 			      (RX_SMOOTH_FACTOR - 1)) +
341 			      (pstats->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
342 		}
343 		rtlpriv->dm.undec_sm_pwdb = undec_sm_pwdb;
344 		_rtl92de_update_rxsignalstatistics(hw, pstats);
345 	}
346 }
347 
348 static void rtl92d_loop_over_streams(struct ieee80211_hw *hw,
349 				     struct rtl_stats *pstats)
350 {
351 	struct rtl_priv *rtlpriv = rtl_priv(hw);
352 	int stream;
353 
354 	for (stream = 0; stream < 2; stream++) {
355 		if (pstats->rx_mimo_sig_qual[stream] != -1) {
356 			if (rtlpriv->stats.rx_evm_percentage[stream] == 0) {
357 				rtlpriv->stats.rx_evm_percentage[stream] =
358 				    pstats->rx_mimo_sig_qual[stream];
359 			}
360 			rtlpriv->stats.rx_evm_percentage[stream] =
361 			    ((rtlpriv->stats.rx_evm_percentage[stream]
362 			      * (RX_SMOOTH_FACTOR - 1)) +
363 			     (pstats->rx_mimo_sig_qual[stream] * 1)) /
364 			    (RX_SMOOTH_FACTOR);
365 		}
366 	}
367 }
368 
369 static void _rtl92de_process_ui_link_quality(struct ieee80211_hw *hw,
370 					     struct rtl_stats *pstats)
371 {
372 	struct rtl_priv *rtlpriv = rtl_priv(hw);
373 	u32 last_evm, tmpval;
374 
375 	if (pstats->signalquality == 0)
376 		return;
377 	if (pstats->packet_toself || pstats->packet_beacon) {
378 		if (rtlpriv->stats.ui_link_quality.total_num++ >=
379 		    PHY_LINKQUALITY_SLID_WIN_MAX) {
380 			rtlpriv->stats.ui_link_quality.total_num =
381 			    PHY_LINKQUALITY_SLID_WIN_MAX;
382 			last_evm = rtlpriv->stats.ui_link_quality.elements[
383 				rtlpriv->stats.ui_link_quality.index];
384 			rtlpriv->stats.ui_link_quality.total_val -= last_evm;
385 		}
386 		rtlpriv->stats.ui_link_quality.total_val +=
387 						 pstats->signalquality;
388 		rtlpriv->stats.ui_link_quality.elements[
389 			rtlpriv->stats.ui_link_quality.index++] =
390 						 pstats->signalquality;
391 		if (rtlpriv->stats.ui_link_quality.index >=
392 		    PHY_LINKQUALITY_SLID_WIN_MAX)
393 			rtlpriv->stats.ui_link_quality.index = 0;
394 		tmpval = rtlpriv->stats.ui_link_quality.total_val /
395 		    rtlpriv->stats.ui_link_quality.total_num;
396 		rtlpriv->stats.signal_quality = tmpval;
397 		rtlpriv->stats.last_sigstrength_inpercent = tmpval;
398 		rtl92d_loop_over_streams(hw, pstats);
399 	}
400 }
401 
402 static void _rtl92de_process_phyinfo(struct ieee80211_hw *hw,
403 				     u8 *buffer,
404 				     struct rtl_stats *pcurrent_stats)
405 {
406 
407 	if (!pcurrent_stats->packet_matchbssid &&
408 	    !pcurrent_stats->packet_beacon)
409 		return;
410 
411 	_rtl92de_process_ui_rssi(hw, pcurrent_stats);
412 	_rtl92de_process_pwdb(hw, pcurrent_stats);
413 	_rtl92de_process_ui_link_quality(hw, pcurrent_stats);
414 }
415 
416 static void _rtl92de_translate_rx_signal_stuff(struct ieee80211_hw *hw,
417 					       struct sk_buff *skb,
418 					       struct rtl_stats *pstats,
419 					       struct rx_desc_92d *pdesc,
420 					       struct rx_fwinfo_92d *p_drvinfo)
421 {
422 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
423 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
424 	struct ieee80211_hdr *hdr;
425 	u8 *tmp_buf;
426 	u8 *praddr;
427 	u16 type, cfc;
428 	__le16 fc;
429 	bool packet_matchbssid, packet_toself, packet_beacon = false;
430 
431 	tmp_buf = skb->data + pstats->rx_drvinfo_size + pstats->rx_bufshift;
432 	hdr = (struct ieee80211_hdr *)tmp_buf;
433 	fc = hdr->frame_control;
434 	cfc = le16_to_cpu(fc);
435 	type = WLAN_FC_GET_TYPE(fc);
436 	praddr = hdr->addr1;
437 	packet_matchbssid = ((IEEE80211_FTYPE_CTL != type) &&
438 	     ether_addr_equal(mac->bssid,
439 			      (cfc & IEEE80211_FCTL_TODS) ? hdr->addr1 :
440 			      (cfc & IEEE80211_FCTL_FROMDS) ? hdr->addr2 :
441 			      hdr->addr3) &&
442 	     (!pstats->hwerror) && (!pstats->crc) && (!pstats->icv));
443 	packet_toself = packet_matchbssid &&
444 			ether_addr_equal(praddr, rtlefuse->dev_addr);
445 	if (ieee80211_is_beacon(fc))
446 		packet_beacon = true;
447 	_rtl92de_query_rxphystatus(hw, pstats, pdesc, p_drvinfo,
448 				   packet_matchbssid, packet_toself,
449 				   packet_beacon);
450 	_rtl92de_process_phyinfo(hw, tmp_buf, pstats);
451 }
452 
453 bool rtl92de_rx_query_desc(struct ieee80211_hw *hw,	struct rtl_stats *stats,
454 		struct ieee80211_rx_status *rx_status,
455 		u8 *p_desc, struct sk_buff *skb)
456 {
457 	struct rx_fwinfo_92d *p_drvinfo;
458 	struct rx_desc_92d *pdesc = (struct rx_desc_92d *)p_desc;
459 	u32 phystatus = GET_RX_DESC_PHYST(pdesc);
460 
461 	stats->length = (u16) GET_RX_DESC_PKT_LEN(pdesc);
462 	stats->rx_drvinfo_size = (u8) GET_RX_DESC_DRV_INFO_SIZE(pdesc) *
463 				 RX_DRV_INFO_SIZE_UNIT;
464 	stats->rx_bufshift = (u8) (GET_RX_DESC_SHIFT(pdesc) & 0x03);
465 	stats->icv = (u16) GET_RX_DESC_ICV(pdesc);
466 	stats->crc = (u16) GET_RX_DESC_CRC32(pdesc);
467 	stats->hwerror = (stats->crc | stats->icv);
468 	stats->decrypted = !GET_RX_DESC_SWDEC(pdesc);
469 	stats->rate = (u8) GET_RX_DESC_RXMCS(pdesc);
470 	stats->shortpreamble = (u16) GET_RX_DESC_SPLCP(pdesc);
471 	stats->isampdu = (bool) (GET_RX_DESC_PAGGR(pdesc) == 1);
472 	stats->isfirst_ampdu = (bool) ((GET_RX_DESC_PAGGR(pdesc) == 1)
473 					 && (GET_RX_DESC_FAGGR(pdesc) == 1));
474 	stats->timestamp_low = GET_RX_DESC_TSFL(pdesc);
475 	stats->rx_is40mhzpacket = (bool)GET_RX_DESC_BW(pdesc);
476 	stats->is_ht = (bool)GET_RX_DESC_RXHT(pdesc);
477 	rx_status->freq = hw->conf.chandef.chan->center_freq;
478 	rx_status->band = hw->conf.chandef.chan->band;
479 	if (GET_RX_DESC_CRC32(pdesc))
480 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
481 	if (!GET_RX_DESC_SWDEC(pdesc))
482 		rx_status->flag |= RX_FLAG_DECRYPTED;
483 	if (GET_RX_DESC_BW(pdesc))
484 		rx_status->bw = RATE_INFO_BW_40;
485 	if (GET_RX_DESC_RXHT(pdesc))
486 		rx_status->encoding = RX_ENC_HT;
487 	rx_status->flag |= RX_FLAG_MACTIME_START;
488 	if (stats->decrypted)
489 		rx_status->flag |= RX_FLAG_DECRYPTED;
490 	rx_status->rate_idx = rtlwifi_rate_mapping(hw, stats->is_ht,
491 						   false, stats->rate);
492 	rx_status->mactime = GET_RX_DESC_TSFL(pdesc);
493 	if (phystatus) {
494 		p_drvinfo = (struct rx_fwinfo_92d *)(skb->data +
495 						     stats->rx_bufshift);
496 		_rtl92de_translate_rx_signal_stuff(hw,
497 						   skb, stats, pdesc,
498 						   p_drvinfo);
499 	}
500 	/*rx_status->qual = stats->signal; */
501 	rx_status->signal = stats->recvsignalpower + 10;
502 	return true;
503 }
504 
505 static void _rtl92de_insert_emcontent(struct rtl_tcb_desc *ptcb_desc,
506 				      u8 *virtualaddress)
507 {
508 	memset(virtualaddress, 0, 8);
509 
510 	SET_EARLYMODE_PKTNUM(virtualaddress, ptcb_desc->empkt_num);
511 	SET_EARLYMODE_LEN0(virtualaddress, ptcb_desc->empkt_len[0]);
512 	SET_EARLYMODE_LEN1(virtualaddress, ptcb_desc->empkt_len[1]);
513 	SET_EARLYMODE_LEN2_1(virtualaddress, ptcb_desc->empkt_len[2] & 0xF);
514 	SET_EARLYMODE_LEN2_2(virtualaddress, ptcb_desc->empkt_len[2] >> 4);
515 	SET_EARLYMODE_LEN3(virtualaddress, ptcb_desc->empkt_len[3]);
516 	SET_EARLYMODE_LEN4(virtualaddress, ptcb_desc->empkt_len[4]);
517 }
518 
519 void rtl92de_tx_fill_desc(struct ieee80211_hw *hw,
520 			  struct ieee80211_hdr *hdr, u8 *pdesc_tx,
521 			  u8 *pbd_desc_tx, struct ieee80211_tx_info *info,
522 			  struct ieee80211_sta *sta,
523 			  struct sk_buff *skb,
524 			  u8 hw_queue, struct rtl_tcb_desc *ptcb_desc)
525 {
526 	struct rtl_priv *rtlpriv = rtl_priv(hw);
527 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
528 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
529 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
530 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
531 	u8 *pdesc = pdesc_tx;
532 	u16 seq_number;
533 	__le16 fc = hdr->frame_control;
534 	unsigned int buf_len = 0;
535 	unsigned int skb_len = skb->len;
536 	u8 fw_qsel = _rtl92de_map_hwqueue_to_fwqueue(skb, hw_queue);
537 	bool firstseg = ((hdr->seq_ctrl &
538 			cpu_to_le16(IEEE80211_SCTL_FRAG)) == 0);
539 	bool lastseg = ((hdr->frame_control &
540 			cpu_to_le16(IEEE80211_FCTL_MOREFRAGS)) == 0);
541 	dma_addr_t mapping;
542 	u8 bw_40 = 0;
543 
544 	if (mac->opmode == NL80211_IFTYPE_STATION) {
545 		bw_40 = mac->bw_40;
546 	} else if (mac->opmode == NL80211_IFTYPE_AP ||
547 		mac->opmode == NL80211_IFTYPE_ADHOC) {
548 		if (sta)
549 			bw_40 = sta->bandwidth >= IEEE80211_STA_RX_BW_40;
550 	}
551 	seq_number = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4;
552 	rtl_get_tcb_desc(hw, info, sta, skb, ptcb_desc);
553 	/* reserve 8 byte for AMPDU early mode */
554 	if (rtlhal->earlymode_enable) {
555 		skb_push(skb, EM_HDR_LEN);
556 		memset(skb->data, 0, EM_HDR_LEN);
557 	}
558 	buf_len = skb->len;
559 	mapping = pci_map_single(rtlpci->pdev, skb->data, skb->len,
560 				 PCI_DMA_TODEVICE);
561 	if (pci_dma_mapping_error(rtlpci->pdev, mapping)) {
562 		RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
563 			 "DMA mapping error\n");
564 		return;
565 	}
566 	CLEAR_PCI_TX_DESC_CONTENT(pdesc, sizeof(struct tx_desc_92d));
567 	if (ieee80211_is_nullfunc(fc) || ieee80211_is_ctl(fc)) {
568 		firstseg = true;
569 		lastseg = true;
570 	}
571 	if (firstseg) {
572 		if (rtlhal->earlymode_enable) {
573 			SET_TX_DESC_PKT_OFFSET(pdesc, 1);
574 			SET_TX_DESC_OFFSET(pdesc, USB_HWDESC_HEADER_LEN +
575 					   EM_HDR_LEN);
576 			if (ptcb_desc->empkt_num) {
577 				RT_TRACE(rtlpriv, COMP_SEND, DBG_LOUD,
578 					 "Insert 8 byte.pTcb->EMPktNum:%d\n",
579 					 ptcb_desc->empkt_num);
580 				_rtl92de_insert_emcontent(ptcb_desc,
581 							  (u8 *)(skb->data));
582 			}
583 		} else {
584 			SET_TX_DESC_OFFSET(pdesc, USB_HWDESC_HEADER_LEN);
585 		}
586 		/* 5G have no CCK rate */
587 		if (rtlhal->current_bandtype == BAND_ON_5G)
588 			if (ptcb_desc->hw_rate < DESC_RATE6M)
589 				ptcb_desc->hw_rate = DESC_RATE6M;
590 		SET_TX_DESC_TX_RATE(pdesc, ptcb_desc->hw_rate);
591 		if (ptcb_desc->use_shortgi || ptcb_desc->use_shortpreamble)
592 			SET_TX_DESC_DATA_SHORTGI(pdesc, 1);
593 
594 		if (rtlhal->macphymode == DUALMAC_DUALPHY &&
595 			ptcb_desc->hw_rate == DESC_RATEMCS7)
596 			SET_TX_DESC_DATA_SHORTGI(pdesc, 1);
597 
598 		if (info->flags & IEEE80211_TX_CTL_AMPDU) {
599 			SET_TX_DESC_AGG_ENABLE(pdesc, 1);
600 			SET_TX_DESC_MAX_AGG_NUM(pdesc, 0x14);
601 		}
602 		SET_TX_DESC_SEQ(pdesc, seq_number);
603 		SET_TX_DESC_RTS_ENABLE(pdesc, ((ptcb_desc->rts_enable &&
604 				       !ptcb_desc->cts_enable) ? 1 : 0));
605 		SET_TX_DESC_HW_RTS_ENABLE(pdesc, ((ptcb_desc->rts_enable
606 					  || ptcb_desc->cts_enable) ? 1 : 0));
607 		SET_TX_DESC_CTS2SELF(pdesc, ((ptcb_desc->cts_enable) ? 1 : 0));
608 		SET_TX_DESC_RTS_STBC(pdesc, ((ptcb_desc->rts_stbc) ? 1 : 0));
609 		/* 5G have no CCK rate */
610 		if (rtlhal->current_bandtype == BAND_ON_5G)
611 			if (ptcb_desc->rts_rate < DESC_RATE6M)
612 				ptcb_desc->rts_rate = DESC_RATE6M;
613 		SET_TX_DESC_RTS_RATE(pdesc, ptcb_desc->rts_rate);
614 		SET_TX_DESC_RTS_BW(pdesc, 0);
615 		SET_TX_DESC_RTS_SC(pdesc, ptcb_desc->rts_sc);
616 		SET_TX_DESC_RTS_SHORT(pdesc, ((ptcb_desc->rts_rate <=
617 			DESC_RATE54M) ?
618 			(ptcb_desc->rts_use_shortpreamble ? 1 : 0) :
619 			(ptcb_desc->rts_use_shortgi ? 1 : 0)));
620 		if (bw_40) {
621 			if (ptcb_desc->packet_bw) {
622 				SET_TX_DESC_DATA_BW(pdesc, 1);
623 				SET_TX_DESC_TX_SUB_CARRIER(pdesc, 3);
624 			} else {
625 				SET_TX_DESC_DATA_BW(pdesc, 0);
626 				SET_TX_DESC_TX_SUB_CARRIER(pdesc,
627 							mac->cur_40_prime_sc);
628 			}
629 		} else {
630 			SET_TX_DESC_DATA_BW(pdesc, 0);
631 			SET_TX_DESC_TX_SUB_CARRIER(pdesc, 0);
632 		}
633 		SET_TX_DESC_LINIP(pdesc, 0);
634 		SET_TX_DESC_PKT_SIZE(pdesc, (u16) skb_len);
635 		if (sta) {
636 			u8 ampdu_density = sta->ht_cap.ampdu_density;
637 			SET_TX_DESC_AMPDU_DENSITY(pdesc, ampdu_density);
638 		}
639 		if (info->control.hw_key) {
640 			struct ieee80211_key_conf *keyconf;
641 
642 			keyconf = info->control.hw_key;
643 			switch (keyconf->cipher) {
644 			case WLAN_CIPHER_SUITE_WEP40:
645 			case WLAN_CIPHER_SUITE_WEP104:
646 			case WLAN_CIPHER_SUITE_TKIP:
647 				SET_TX_DESC_SEC_TYPE(pdesc, 0x1);
648 				break;
649 			case WLAN_CIPHER_SUITE_CCMP:
650 				SET_TX_DESC_SEC_TYPE(pdesc, 0x3);
651 				break;
652 			default:
653 				SET_TX_DESC_SEC_TYPE(pdesc, 0x0);
654 				break;
655 
656 			}
657 		}
658 		SET_TX_DESC_PKT_ID(pdesc, 0);
659 		SET_TX_DESC_QUEUE_SEL(pdesc, fw_qsel);
660 		SET_TX_DESC_DATA_RATE_FB_LIMIT(pdesc, 0x1F);
661 		SET_TX_DESC_RTS_RATE_FB_LIMIT(pdesc, 0xF);
662 		SET_TX_DESC_DISABLE_FB(pdesc, ptcb_desc->disable_ratefallback ?
663 				       1 : 0);
664 		SET_TX_DESC_USE_RATE(pdesc, ptcb_desc->use_driver_rate ? 1 : 0);
665 
666 		/* Set TxRate and RTSRate in TxDesc  */
667 		/* This prevent Tx initial rate of new-coming packets */
668 		/* from being overwritten by retried  packet rate.*/
669 		if (!ptcb_desc->use_driver_rate) {
670 			SET_TX_DESC_RTS_RATE(pdesc, 0x08);
671 			/* SET_TX_DESC_TX_RATE(pdesc, 0x0b); */
672 		}
673 		if (ieee80211_is_data_qos(fc)) {
674 			if (mac->rdg_en) {
675 				RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
676 					 "Enable RDG function\n");
677 				SET_TX_DESC_RDG_ENABLE(pdesc, 1);
678 				SET_TX_DESC_HTC(pdesc, 1);
679 			}
680 		}
681 	}
682 
683 	SET_TX_DESC_FIRST_SEG(pdesc, (firstseg ? 1 : 0));
684 	SET_TX_DESC_LAST_SEG(pdesc, (lastseg ? 1 : 0));
685 	SET_TX_DESC_TX_BUFFER_SIZE(pdesc, (u16) buf_len);
686 	SET_TX_DESC_TX_BUFFER_ADDRESS(pdesc, mapping);
687 	if (rtlpriv->dm.useramask) {
688 		SET_TX_DESC_RATE_ID(pdesc, ptcb_desc->ratr_index);
689 		SET_TX_DESC_MACID(pdesc, ptcb_desc->mac_id);
690 	} else {
691 		SET_TX_DESC_RATE_ID(pdesc, 0xC + ptcb_desc->ratr_index);
692 		SET_TX_DESC_MACID(pdesc, ptcb_desc->ratr_index);
693 	}
694 	if (ieee80211_is_data_qos(fc))
695 		SET_TX_DESC_QOS(pdesc, 1);
696 
697 	if ((!ieee80211_is_data_qos(fc)) && ppsc->fwctrl_lps) {
698 		SET_TX_DESC_HWSEQ_EN(pdesc, 1);
699 		SET_TX_DESC_PKT_ID(pdesc, 8);
700 	}
701 	SET_TX_DESC_MORE_FRAG(pdesc, (lastseg ? 0 : 1));
702 	RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE, "\n");
703 }
704 
705 void rtl92de_tx_fill_cmddesc(struct ieee80211_hw *hw,
706 			     u8 *pdesc, bool firstseg,
707 			     bool lastseg, struct sk_buff *skb)
708 {
709 	struct rtl_priv *rtlpriv = rtl_priv(hw);
710 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
711 	struct rtl_ps_ctl *ppsc = rtl_psc(rtlpriv);
712 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
713 	u8 fw_queue = QSLT_BEACON;
714 	dma_addr_t mapping = pci_map_single(rtlpci->pdev,
715 		    skb->data, skb->len, PCI_DMA_TODEVICE);
716 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
717 	__le16 fc = hdr->frame_control;
718 
719 	if (pci_dma_mapping_error(rtlpci->pdev, mapping)) {
720 		RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
721 			 "DMA mapping error\n");
722 		return;
723 	}
724 	CLEAR_PCI_TX_DESC_CONTENT(pdesc, TX_DESC_SIZE);
725 	if (firstseg)
726 		SET_TX_DESC_OFFSET(pdesc, USB_HWDESC_HEADER_LEN);
727 	/* 5G have no CCK rate
728 	 * Caution: The macros below are multi-line expansions.
729 	 * The braces are needed no matter what checkpatch says
730 	 */
731 	if (rtlhal->current_bandtype == BAND_ON_5G) {
732 		SET_TX_DESC_TX_RATE(pdesc, DESC_RATE6M);
733 	} else {
734 		SET_TX_DESC_TX_RATE(pdesc, DESC_RATE1M);
735 	}
736 	SET_TX_DESC_SEQ(pdesc, 0);
737 	SET_TX_DESC_LINIP(pdesc, 0);
738 	SET_TX_DESC_QUEUE_SEL(pdesc, fw_queue);
739 	SET_TX_DESC_FIRST_SEG(pdesc, 1);
740 	SET_TX_DESC_LAST_SEG(pdesc, 1);
741 	SET_TX_DESC_TX_BUFFER_SIZE(pdesc, (u16)skb->len);
742 	SET_TX_DESC_TX_BUFFER_ADDRESS(pdesc, mapping);
743 	SET_TX_DESC_RATE_ID(pdesc, 7);
744 	SET_TX_DESC_MACID(pdesc, 0);
745 	SET_TX_DESC_PKT_SIZE(pdesc, (u16) (skb->len));
746 	SET_TX_DESC_FIRST_SEG(pdesc, 1);
747 	SET_TX_DESC_LAST_SEG(pdesc, 1);
748 	SET_TX_DESC_OFFSET(pdesc, 0x20);
749 	SET_TX_DESC_USE_RATE(pdesc, 1);
750 
751 	if (!ieee80211_is_data_qos(fc) && ppsc->fwctrl_lps) {
752 		SET_TX_DESC_HWSEQ_EN(pdesc, 1);
753 		SET_TX_DESC_PKT_ID(pdesc, 8);
754 	}
755 
756 	RT_PRINT_DATA(rtlpriv, COMP_CMD, DBG_LOUD,
757 		      "H2C Tx Cmd Content", pdesc, TX_DESC_SIZE);
758 	wmb();
759 	SET_TX_DESC_OWN(pdesc, 1);
760 }
761 
762 void rtl92de_set_desc(struct ieee80211_hw *hw, u8 *pdesc, bool istx,
763 		      u8 desc_name, u8 *val)
764 {
765 	if (istx) {
766 		switch (desc_name) {
767 		case HW_DESC_OWN:
768 			wmb();
769 			SET_TX_DESC_OWN(pdesc, 1);
770 			break;
771 		case HW_DESC_TX_NEXTDESC_ADDR:
772 			SET_TX_DESC_NEXT_DESC_ADDRESS(pdesc, *(u32 *) val);
773 			break;
774 		default:
775 			WARN_ONCE(true, "rtl8192de: ERR txdesc :%d not processed\n",
776 				  desc_name);
777 			break;
778 		}
779 	} else {
780 		switch (desc_name) {
781 		case HW_DESC_RXOWN:
782 			wmb();
783 			SET_RX_DESC_OWN(pdesc, 1);
784 			break;
785 		case HW_DESC_RXBUFF_ADDR:
786 			SET_RX_DESC_BUFF_ADDR(pdesc, *(u32 *) val);
787 			break;
788 		case HW_DESC_RXPKT_LEN:
789 			SET_RX_DESC_PKT_LEN(pdesc, *(u32 *) val);
790 			break;
791 		case HW_DESC_RXERO:
792 			SET_RX_DESC_EOR(pdesc, 1);
793 			break;
794 		default:
795 			WARN_ONCE(true, "rtl8192de: ERR rxdesc :%d not processed\n",
796 				  desc_name);
797 			break;
798 		}
799 	}
800 }
801 
802 u64 rtl92de_get_desc(struct ieee80211_hw *hw,
803 		     u8 *p_desc, bool istx, u8 desc_name)
804 {
805 	u32 ret = 0;
806 
807 	if (istx) {
808 		switch (desc_name) {
809 		case HW_DESC_OWN:
810 			ret = GET_TX_DESC_OWN(p_desc);
811 			break;
812 		case HW_DESC_TXBUFF_ADDR:
813 			ret = GET_TX_DESC_TX_BUFFER_ADDRESS(p_desc);
814 			break;
815 		default:
816 			WARN_ONCE(true, "rtl8192de: ERR txdesc :%d not processed\n",
817 				  desc_name);
818 			break;
819 		}
820 	} else {
821 		struct rx_desc_92c *pdesc = (struct rx_desc_92c *)p_desc;
822 		switch (desc_name) {
823 		case HW_DESC_OWN:
824 			ret = GET_RX_DESC_OWN(pdesc);
825 			break;
826 		case HW_DESC_RXPKT_LEN:
827 			ret = GET_RX_DESC_PKT_LEN(pdesc);
828 			break;
829 		default:
830 			WARN_ONCE(true, "rtl8192de: ERR rxdesc :%d not processed\n",
831 				  desc_name);
832 			break;
833 		}
834 	}
835 	return ret;
836 }
837 
838 void rtl92de_tx_polling(struct ieee80211_hw *hw, u8 hw_queue)
839 {
840 	struct rtl_priv *rtlpriv = rtl_priv(hw);
841 	if (hw_queue == BEACON_QUEUE)
842 		rtl_write_word(rtlpriv, REG_PCIE_CTRL_REG, BIT(4));
843 	else
844 		rtl_write_word(rtlpriv, REG_PCIE_CTRL_REG,
845 			       BIT(0) << (hw_queue));
846 }
847