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