xref: /linux/drivers/net/wireless/realtek/rtlwifi/rtl8192de/trx.c (revision eb01fe7abbe2d0b38824d2a93fdb4cc3eaf2ccc1)
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 				       struct rx_desc_92d *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_phy *rtlphy = &(rtlpriv->phy);
46 	struct rtl_ps_ctl *ppsc = rtl_psc(rtlpriv);
47 	struct phy_sts_cck_8192d *cck_buf;
48 	s8 rx_pwr_all, rx_pwr[4];
49 	u8 rf_rx_num = 0, evm, pwdb_all;
50 	u8 i, max_spatial_stream;
51 	u32 rssi, total_rssi = 0;
52 	bool is_cck_rate;
53 
54 	is_cck_rate = RX_HAL_IS_CCK_RATE(pdesc->rxmcs);
55 	pstats->packet_matchbssid = packet_match_bssid;
56 	pstats->packet_toself = packet_toself;
57 	pstats->packet_beacon = packet_beacon;
58 	pstats->is_cck = is_cck_rate;
59 	pstats->rx_mimo_sig_qual[0] = -1;
60 	pstats->rx_mimo_sig_qual[1] = -1;
61 
62 	if (is_cck_rate) {
63 		u8 report, cck_highpwr;
64 		cck_buf = (struct phy_sts_cck_8192d *)p_drvinfo;
65 		if (ppsc->rfpwr_state == ERFON)
66 			cck_highpwr = rtlphy->cck_high_power;
67 		else
68 			cck_highpwr = false;
69 		if (!cck_highpwr) {
70 			u8 cck_agc_rpt = cck_buf->cck_agc_rpt;
71 			report = cck_buf->cck_agc_rpt & 0xc0;
72 			report = report >> 6;
73 			switch (report) {
74 			case 0x3:
75 				rx_pwr_all = -46 - (cck_agc_rpt & 0x3e);
76 				break;
77 			case 0x2:
78 				rx_pwr_all = -26 - (cck_agc_rpt & 0x3e);
79 				break;
80 			case 0x1:
81 				rx_pwr_all = -12 - (cck_agc_rpt & 0x3e);
82 				break;
83 			case 0x0:
84 				rx_pwr_all = 16 - (cck_agc_rpt & 0x3e);
85 				break;
86 			}
87 		} else {
88 			u8 cck_agc_rpt = cck_buf->cck_agc_rpt;
89 			report = p_drvinfo->cfosho[0] & 0x60;
90 			report = report >> 5;
91 			switch (report) {
92 			case 0x3:
93 				rx_pwr_all = -46 - ((cck_agc_rpt & 0x1f) << 1);
94 				break;
95 			case 0x2:
96 				rx_pwr_all = -26 - ((cck_agc_rpt & 0x1f) << 1);
97 				break;
98 			case 0x1:
99 				rx_pwr_all = -12 - ((cck_agc_rpt & 0x1f) << 1);
100 				break;
101 			case 0x0:
102 				rx_pwr_all = 16 - ((cck_agc_rpt & 0x1f) << 1);
103 				break;
104 			}
105 		}
106 		pwdb_all = rtl_query_rxpwrpercentage(rx_pwr_all);
107 		/* CCK gain is smaller than OFDM/MCS gain,  */
108 		/* so we add gain diff by experiences, the val is 6 */
109 		pwdb_all += 6;
110 		if (pwdb_all > 100)
111 			pwdb_all = 100;
112 		/* modify the offset to make the same gain index with OFDM. */
113 		if (pwdb_all > 34 && pwdb_all <= 42)
114 			pwdb_all -= 2;
115 		else if (pwdb_all > 26 && pwdb_all <= 34)
116 			pwdb_all -= 6;
117 		else if (pwdb_all > 14 && pwdb_all <= 26)
118 			pwdb_all -= 8;
119 		else if (pwdb_all > 4 && pwdb_all <= 14)
120 			pwdb_all -= 4;
121 		pstats->rx_pwdb_all = pwdb_all;
122 		pstats->recvsignalpower = rx_pwr_all;
123 		if (packet_match_bssid) {
124 			u8 sq;
125 			if (pstats->rx_pwdb_all > 40) {
126 				sq = 100;
127 			} else {
128 				sq = cck_buf->sq_rpt;
129 				if (sq > 64)
130 					sq = 0;
131 				else if (sq < 20)
132 					sq = 100;
133 				else
134 					sq = ((64 - sq) * 100) / 44;
135 			}
136 			pstats->signalquality = sq;
137 			pstats->rx_mimo_sig_qual[0] = sq;
138 			pstats->rx_mimo_sig_qual[1] = -1;
139 		}
140 	} else {
141 		rtlpriv->dm.rfpath_rxenable[0] = true;
142 		rtlpriv->dm.rfpath_rxenable[1] = true;
143 		for (i = RF90_PATH_A; i < RF6052_MAX_PATH; i++) {
144 			if (rtlpriv->dm.rfpath_rxenable[i])
145 				rf_rx_num++;
146 			rx_pwr[i] = ((p_drvinfo->gain_trsw[i] & 0x3f) * 2)
147 				    - 110;
148 			rssi = rtl_query_rxpwrpercentage(rx_pwr[i]);
149 			total_rssi += rssi;
150 			rtlpriv->stats.rx_snr_db[i] =
151 					 (long)(p_drvinfo->rxsnr[i] / 2);
152 			if (packet_match_bssid)
153 				pstats->rx_mimo_signalstrength[i] = (u8) rssi;
154 		}
155 		rx_pwr_all = ((p_drvinfo->pwdb_all >> 1) & 0x7f) - 106;
156 		pwdb_all = rtl_query_rxpwrpercentage(rx_pwr_all);
157 		pstats->rx_pwdb_all = pwdb_all;
158 		pstats->rxpower = rx_pwr_all;
159 		pstats->recvsignalpower = rx_pwr_all;
160 		if (pdesc->rxht && pdesc->rxmcs >= DESC_RATEMCS8 &&
161 		    pdesc->rxmcs <= DESC_RATEMCS15)
162 			max_spatial_stream = 2;
163 		else
164 			max_spatial_stream = 1;
165 		for (i = 0; i < max_spatial_stream; i++) {
166 			evm = rtl_evm_db_to_percentage(p_drvinfo->rxevm[i]);
167 			if (packet_match_bssid) {
168 				if (i == 0)
169 					pstats->signalquality =
170 						 (u8)(evm & 0xff);
171 				pstats->rx_mimo_sig_qual[i] =
172 						 (u8)(evm & 0xff);
173 			}
174 		}
175 	}
176 	if (is_cck_rate)
177 		pstats->signalstrength = (u8)(rtl_signal_scale_mapping(hw,
178 				pwdb_all));
179 	else if (rf_rx_num != 0)
180 		pstats->signalstrength = (u8)(rtl_signal_scale_mapping(hw,
181 				total_rssi /= rf_rx_num));
182 }
183 
184 static void rtl92d_loop_over_paths(struct ieee80211_hw *hw,
185 				   struct rtl_stats *pstats)
186 {
187 	struct rtl_priv *rtlpriv = rtl_priv(hw);
188 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
189 	u8 rfpath;
190 
191 	for (rfpath = RF90_PATH_A; rfpath < rtlphy->num_total_rfpath;
192 	     rfpath++) {
193 		if (rtlpriv->stats.rx_rssi_percentage[rfpath] == 0) {
194 			rtlpriv->stats.rx_rssi_percentage[rfpath] =
195 			    pstats->rx_mimo_signalstrength[rfpath];
196 
197 		}
198 		if (pstats->rx_mimo_signalstrength[rfpath] >
199 		    rtlpriv->stats.rx_rssi_percentage[rfpath]) {
200 			rtlpriv->stats.rx_rssi_percentage[rfpath] =
201 			    ((rtlpriv->stats.rx_rssi_percentage[rfpath] *
202 			      (RX_SMOOTH_FACTOR - 1)) +
203 			     (pstats->rx_mimo_signalstrength[rfpath])) /
204 			    (RX_SMOOTH_FACTOR);
205 			rtlpriv->stats.rx_rssi_percentage[rfpath] =
206 			    rtlpriv->stats.rx_rssi_percentage[rfpath] + 1;
207 		} else {
208 			rtlpriv->stats.rx_rssi_percentage[rfpath] =
209 			    ((rtlpriv->stats.rx_rssi_percentage[rfpath] *
210 			      (RX_SMOOTH_FACTOR - 1)) +
211 			     (pstats->rx_mimo_signalstrength[rfpath])) /
212 			    (RX_SMOOTH_FACTOR);
213 		}
214 	}
215 }
216 
217 static void _rtl92de_process_ui_rssi(struct ieee80211_hw *hw,
218 				     struct rtl_stats *pstats)
219 {
220 	struct rtl_priv *rtlpriv = rtl_priv(hw);
221 	u32 last_rssi, tmpval;
222 
223 	if (pstats->packet_toself || pstats->packet_beacon) {
224 		rtlpriv->stats.rssi_calculate_cnt++;
225 		if (rtlpriv->stats.ui_rssi.total_num++ >=
226 		    PHY_RSSI_SLID_WIN_MAX) {
227 			rtlpriv->stats.ui_rssi.total_num =
228 						 PHY_RSSI_SLID_WIN_MAX;
229 			last_rssi = rtlpriv->stats.ui_rssi.elements[
230 				rtlpriv->stats.ui_rssi.index];
231 			rtlpriv->stats.ui_rssi.total_val -= last_rssi;
232 		}
233 		rtlpriv->stats.ui_rssi.total_val += pstats->signalstrength;
234 		rtlpriv->stats.ui_rssi.elements
235 			[rtlpriv->stats.ui_rssi.index++] =
236 			pstats->signalstrength;
237 		if (rtlpriv->stats.ui_rssi.index >= PHY_RSSI_SLID_WIN_MAX)
238 			rtlpriv->stats.ui_rssi.index = 0;
239 		tmpval = rtlpriv->stats.ui_rssi.total_val /
240 			rtlpriv->stats.ui_rssi.total_num;
241 		rtlpriv->stats.signal_strength = _rtl92de_translate_todbm(hw,
242 			(u8) tmpval);
243 		pstats->rssi = rtlpriv->stats.signal_strength;
244 	}
245 	if (!pstats->is_cck && pstats->packet_toself)
246 		rtl92d_loop_over_paths(hw, pstats);
247 }
248 
249 static void _rtl92de_update_rxsignalstatistics(struct ieee80211_hw *hw,
250 					       struct rtl_stats *pstats)
251 {
252 	struct rtl_priv *rtlpriv = rtl_priv(hw);
253 	int weighting = 0;
254 
255 	if (rtlpriv->stats.recv_signal_power == 0)
256 		rtlpriv->stats.recv_signal_power = pstats->recvsignalpower;
257 	if (pstats->recvsignalpower > rtlpriv->stats.recv_signal_power)
258 		weighting = 5;
259 	else if (pstats->recvsignalpower < rtlpriv->stats.recv_signal_power)
260 		weighting = (-5);
261 	rtlpriv->stats.recv_signal_power = (rtlpriv->stats.recv_signal_power *
262 		5 + pstats->recvsignalpower + weighting) / 6;
263 }
264 
265 static void _rtl92de_process_pwdb(struct ieee80211_hw *hw,
266 				  struct rtl_stats *pstats)
267 {
268 	struct rtl_priv *rtlpriv = rtl_priv(hw);
269 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
270 	long undec_sm_pwdb;
271 
272 	if (mac->opmode == NL80211_IFTYPE_ADHOC	||
273 		mac->opmode == NL80211_IFTYPE_AP)
274 		return;
275 	else
276 		undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb;
277 
278 	if (pstats->packet_toself || pstats->packet_beacon) {
279 		if (undec_sm_pwdb < 0)
280 			undec_sm_pwdb = pstats->rx_pwdb_all;
281 		if (pstats->rx_pwdb_all > (u32) undec_sm_pwdb) {
282 			undec_sm_pwdb = (((undec_sm_pwdb) *
283 			      (RX_SMOOTH_FACTOR - 1)) +
284 			      (pstats->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
285 			undec_sm_pwdb = undec_sm_pwdb + 1;
286 		} else {
287 			undec_sm_pwdb = (((undec_sm_pwdb) *
288 			      (RX_SMOOTH_FACTOR - 1)) +
289 			      (pstats->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
290 		}
291 		rtlpriv->dm.undec_sm_pwdb = undec_sm_pwdb;
292 		_rtl92de_update_rxsignalstatistics(hw, pstats);
293 	}
294 }
295 
296 static void rtl92d_loop_over_streams(struct ieee80211_hw *hw,
297 				     struct rtl_stats *pstats)
298 {
299 	struct rtl_priv *rtlpriv = rtl_priv(hw);
300 	int stream;
301 
302 	for (stream = 0; stream < 2; stream++) {
303 		if (pstats->rx_mimo_sig_qual[stream] != -1) {
304 			if (rtlpriv->stats.rx_evm_percentage[stream] == 0) {
305 				rtlpriv->stats.rx_evm_percentage[stream] =
306 				    pstats->rx_mimo_sig_qual[stream];
307 			}
308 			rtlpriv->stats.rx_evm_percentage[stream] =
309 			    ((rtlpriv->stats.rx_evm_percentage[stream]
310 			      * (RX_SMOOTH_FACTOR - 1)) +
311 			     (pstats->rx_mimo_sig_qual[stream] * 1)) /
312 			    (RX_SMOOTH_FACTOR);
313 		}
314 	}
315 }
316 
317 static void _rtl92de_process_ui_link_quality(struct ieee80211_hw *hw,
318 					     struct rtl_stats *pstats)
319 {
320 	struct rtl_priv *rtlpriv = rtl_priv(hw);
321 	u32 last_evm, tmpval;
322 
323 	if (pstats->signalquality == 0)
324 		return;
325 	if (pstats->packet_toself || pstats->packet_beacon) {
326 		if (rtlpriv->stats.ui_link_quality.total_num++ >=
327 		    PHY_LINKQUALITY_SLID_WIN_MAX) {
328 			rtlpriv->stats.ui_link_quality.total_num =
329 			    PHY_LINKQUALITY_SLID_WIN_MAX;
330 			last_evm = rtlpriv->stats.ui_link_quality.elements[
331 				rtlpriv->stats.ui_link_quality.index];
332 			rtlpriv->stats.ui_link_quality.total_val -= last_evm;
333 		}
334 		rtlpriv->stats.ui_link_quality.total_val +=
335 						 pstats->signalquality;
336 		rtlpriv->stats.ui_link_quality.elements[
337 			rtlpriv->stats.ui_link_quality.index++] =
338 						 pstats->signalquality;
339 		if (rtlpriv->stats.ui_link_quality.index >=
340 		    PHY_LINKQUALITY_SLID_WIN_MAX)
341 			rtlpriv->stats.ui_link_quality.index = 0;
342 		tmpval = rtlpriv->stats.ui_link_quality.total_val /
343 		    rtlpriv->stats.ui_link_quality.total_num;
344 		rtlpriv->stats.signal_quality = tmpval;
345 		rtlpriv->stats.last_sigstrength_inpercent = tmpval;
346 		rtl92d_loop_over_streams(hw, pstats);
347 	}
348 }
349 
350 static void _rtl92de_process_phyinfo(struct ieee80211_hw *hw,
351 				     u8 *buffer,
352 				     struct rtl_stats *pcurrent_stats)
353 {
354 
355 	if (!pcurrent_stats->packet_matchbssid &&
356 	    !pcurrent_stats->packet_beacon)
357 		return;
358 
359 	_rtl92de_process_ui_rssi(hw, pcurrent_stats);
360 	_rtl92de_process_pwdb(hw, pcurrent_stats);
361 	_rtl92de_process_ui_link_quality(hw, pcurrent_stats);
362 }
363 
364 static void _rtl92de_translate_rx_signal_stuff(struct ieee80211_hw *hw,
365 					       struct sk_buff *skb,
366 					       struct rtl_stats *pstats,
367 					       struct rx_desc_92d *pdesc,
368 					       struct rx_fwinfo_92d *p_drvinfo)
369 {
370 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
371 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
372 	struct ieee80211_hdr *hdr;
373 	u8 *tmp_buf;
374 	u8 *praddr;
375 	u16 type, cfc;
376 	__le16 fc;
377 	bool packet_matchbssid, packet_toself, packet_beacon = false;
378 
379 	tmp_buf = skb->data + pstats->rx_drvinfo_size + pstats->rx_bufshift;
380 	hdr = (struct ieee80211_hdr *)tmp_buf;
381 	fc = hdr->frame_control;
382 	cfc = le16_to_cpu(fc);
383 	type = WLAN_FC_GET_TYPE(fc);
384 	praddr = hdr->addr1;
385 	packet_matchbssid = ((IEEE80211_FTYPE_CTL != type) &&
386 	     ether_addr_equal(mac->bssid,
387 			      (cfc & IEEE80211_FCTL_TODS) ? hdr->addr1 :
388 			      (cfc & IEEE80211_FCTL_FROMDS) ? hdr->addr2 :
389 			      hdr->addr3) &&
390 	     (!pstats->hwerror) && (!pstats->crc) && (!pstats->icv));
391 	packet_toself = packet_matchbssid &&
392 			ether_addr_equal(praddr, rtlefuse->dev_addr);
393 	if (ieee80211_is_beacon(fc))
394 		packet_beacon = true;
395 	_rtl92de_query_rxphystatus(hw, pstats, pdesc, p_drvinfo,
396 				   packet_matchbssid, packet_toself,
397 				   packet_beacon);
398 	_rtl92de_process_phyinfo(hw, tmp_buf, pstats);
399 }
400 
401 bool rtl92de_rx_query_desc(struct ieee80211_hw *hw,	struct rtl_stats *stats,
402 		struct ieee80211_rx_status *rx_status,
403 		u8 *pdesc8, struct sk_buff *skb)
404 {
405 	__le32 *pdesc = (__le32 *)pdesc8;
406 	struct rx_fwinfo_92d *p_drvinfo;
407 	u32 phystatus = get_rx_desc_physt(pdesc);
408 
409 	stats->length = (u16)get_rx_desc_pkt_len(pdesc);
410 	stats->rx_drvinfo_size = (u8)get_rx_desc_drv_info_size(pdesc) *
411 				 RX_DRV_INFO_SIZE_UNIT;
412 	stats->rx_bufshift = (u8)(get_rx_desc_shift(pdesc) & 0x03);
413 	stats->icv = (u16)get_rx_desc_icv(pdesc);
414 	stats->crc = (u16)get_rx_desc_crc32(pdesc);
415 	stats->hwerror = (stats->crc | stats->icv);
416 	stats->decrypted = !get_rx_desc_swdec(pdesc);
417 	stats->rate = (u8)get_rx_desc_rxmcs(pdesc);
418 	stats->shortpreamble = (u16)get_rx_desc_splcp(pdesc);
419 	stats->isampdu = (bool)(get_rx_desc_paggr(pdesc) == 1);
420 	stats->isfirst_ampdu = (bool)((get_rx_desc_paggr(pdesc) == 1) &&
421 				      (get_rx_desc_faggr(pdesc) == 1));
422 	stats->timestamp_low = get_rx_desc_tsfl(pdesc);
423 	stats->rx_is40mhzpacket = (bool)get_rx_desc_bw(pdesc);
424 	stats->is_ht = (bool)get_rx_desc_rxht(pdesc);
425 	rx_status->freq = hw->conf.chandef.chan->center_freq;
426 	rx_status->band = hw->conf.chandef.chan->band;
427 	if (get_rx_desc_crc32(pdesc))
428 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
429 	if (!get_rx_desc_swdec(pdesc))
430 		rx_status->flag |= RX_FLAG_DECRYPTED;
431 	if (get_rx_desc_bw(pdesc))
432 		rx_status->bw = RATE_INFO_BW_40;
433 	if (get_rx_desc_rxht(pdesc))
434 		rx_status->encoding = RX_ENC_HT;
435 	rx_status->flag |= RX_FLAG_MACTIME_START;
436 	if (stats->decrypted)
437 		rx_status->flag |= RX_FLAG_DECRYPTED;
438 	rx_status->rate_idx = rtlwifi_rate_mapping(hw, stats->is_ht,
439 						   false, stats->rate);
440 	rx_status->mactime = get_rx_desc_tsfl(pdesc);
441 	if (phystatus) {
442 		p_drvinfo = (struct rx_fwinfo_92d *)(skb->data +
443 						     stats->rx_bufshift);
444 		_rtl92de_translate_rx_signal_stuff(hw,
445 						   skb, stats,
446 						   (struct rx_desc_92d *)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, u8 *pdesc8,
658 			     struct sk_buff *skb)
659 {
660 	struct rtl_priv *rtlpriv = rtl_priv(hw);
661 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
662 	struct rtl_ps_ctl *ppsc = rtl_psc(rtlpriv);
663 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
664 	u8 fw_queue = QSLT_BEACON;
665 
666 	struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
667 	__le16 fc = hdr->frame_control;
668 	__le32 *pdesc = (__le32 *)pdesc8;
669 
670 	dma_addr_t mapping = dma_map_single(&rtlpci->pdev->dev, skb->data,
671 					    skb->len, DMA_TO_DEVICE);
672 
673 	if (dma_mapping_error(&rtlpci->pdev->dev, mapping)) {
674 		rtl_dbg(rtlpriv, COMP_SEND, DBG_TRACE,
675 			"DMA mapping error\n");
676 		return;
677 	}
678 	clear_pci_tx_desc_content(pdesc, TX_DESC_SIZE);
679 	set_tx_desc_offset(pdesc, USB_HWDESC_HEADER_LEN);
680 	/* 5G have no CCK rate
681 	 * Caution: The macros below are multi-line expansions.
682 	 * The braces are needed no matter what checkpatch says
683 	 */
684 	if (rtlhal->current_bandtype == BAND_ON_5G) {
685 		set_tx_desc_tx_rate(pdesc, DESC_RATE6M);
686 	} else {
687 		set_tx_desc_tx_rate(pdesc, DESC_RATE1M);
688 	}
689 	set_tx_desc_seq(pdesc, 0);
690 	set_tx_desc_linip(pdesc, 0);
691 	set_tx_desc_queue_sel(pdesc, fw_queue);
692 	set_tx_desc_first_seg(pdesc, 1);
693 	set_tx_desc_last_seg(pdesc, 1);
694 	set_tx_desc_tx_buffer_size(pdesc, (u16)skb->len);
695 	set_tx_desc_tx_buffer_address(pdesc, mapping);
696 	set_tx_desc_rate_id(pdesc, 7);
697 	set_tx_desc_macid(pdesc, 0);
698 	set_tx_desc_pkt_size(pdesc, (u16)(skb->len));
699 	set_tx_desc_first_seg(pdesc, 1);
700 	set_tx_desc_last_seg(pdesc, 1);
701 	set_tx_desc_offset(pdesc, 0x20);
702 	set_tx_desc_use_rate(pdesc, 1);
703 
704 	if (!ieee80211_is_data_qos(fc) && ppsc->fwctrl_lps) {
705 		set_tx_desc_hwseq_en(pdesc, 1);
706 		set_tx_desc_pkt_id(pdesc, 8);
707 	}
708 
709 	RT_PRINT_DATA(rtlpriv, COMP_CMD, DBG_LOUD,
710 		      "H2C Tx Cmd Content", pdesc, TX_DESC_SIZE);
711 	wmb();
712 	set_tx_desc_own(pdesc, 1);
713 }
714 
715 void rtl92de_set_desc(struct ieee80211_hw *hw, u8 *pdesc8, bool istx,
716 		      u8 desc_name, u8 *val)
717 {
718 	__le32  *pdesc = (__le32 *)pdesc8;
719 
720 	if (istx) {
721 		switch (desc_name) {
722 		case HW_DESC_OWN:
723 			wmb();
724 			set_tx_desc_own(pdesc, 1);
725 			break;
726 		case HW_DESC_TX_NEXTDESC_ADDR:
727 			set_tx_desc_next_desc_address(pdesc, *(u32 *)val);
728 			break;
729 		default:
730 			WARN_ONCE(true, "rtl8192de: ERR txdesc :%d not processed\n",
731 				  desc_name);
732 			break;
733 		}
734 	} else {
735 		switch (desc_name) {
736 		case HW_DESC_RXOWN:
737 			wmb();
738 			set_rx_desc_own(pdesc, 1);
739 			break;
740 		case HW_DESC_RXBUFF_ADDR:
741 			set_rx_desc_buff_addr(pdesc, *(u32 *)val);
742 			break;
743 		case HW_DESC_RXPKT_LEN:
744 			set_rx_desc_pkt_len(pdesc, *(u32 *)val);
745 			break;
746 		case HW_DESC_RXERO:
747 			set_rx_desc_eor(pdesc, 1);
748 			break;
749 		default:
750 			WARN_ONCE(true, "rtl8192de: ERR rxdesc :%d not processed\n",
751 				  desc_name);
752 			break;
753 		}
754 	}
755 }
756 
757 u64 rtl92de_get_desc(struct ieee80211_hw *hw,
758 		     u8 *p_desc8, bool istx, u8 desc_name)
759 {
760 	__le32 *p_desc = (__le32 *)p_desc8;
761 	u32 ret = 0;
762 
763 	if (istx) {
764 		switch (desc_name) {
765 		case HW_DESC_OWN:
766 			ret = get_tx_desc_own(p_desc);
767 			break;
768 		case HW_DESC_TXBUFF_ADDR:
769 			ret = get_tx_desc_tx_buffer_address(p_desc);
770 			break;
771 		default:
772 			WARN_ONCE(true, "rtl8192de: ERR txdesc :%d not processed\n",
773 				  desc_name);
774 			break;
775 		}
776 	} else {
777 		switch (desc_name) {
778 		case HW_DESC_OWN:
779 			ret = get_rx_desc_own(p_desc);
780 			break;
781 		case HW_DESC_RXPKT_LEN:
782 			ret = get_rx_desc_pkt_len(p_desc);
783 		break;
784 		case HW_DESC_RXBUFF_ADDR:
785 			ret = get_rx_desc_buff_addr(p_desc);
786 			break;
787 		default:
788 			WARN_ONCE(true, "rtl8192de: ERR rxdesc :%d not processed\n",
789 				  desc_name);
790 			break;
791 		}
792 	}
793 	return ret;
794 }
795 
796 bool rtl92de_is_tx_desc_closed(struct ieee80211_hw *hw,
797 			       u8 hw_queue, u16 index)
798 {
799 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
800 	struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[hw_queue];
801 	u8 *entry = (u8 *)(&ring->desc[ring->idx]);
802 	u8 own = (u8)rtl92de_get_desc(hw, entry, true, HW_DESC_OWN);
803 
804 	/* a beacon packet will only use the first
805 	 * descriptor by defaut, and the own bit may not
806 	 * be cleared by the hardware
807 	 */
808 	if (own)
809 		return false;
810 	return true;
811 }
812 
813 void rtl92de_tx_polling(struct ieee80211_hw *hw, u8 hw_queue)
814 {
815 	struct rtl_priv *rtlpriv = rtl_priv(hw);
816 	if (hw_queue == BEACON_QUEUE)
817 		rtl_write_word(rtlpriv, REG_PCIE_CTRL_REG, BIT(4));
818 	else
819 		rtl_write_word(rtlpriv, REG_PCIE_CTRL_REG,
820 			       BIT(0) << (hw_queue));
821 }
822