xref: /linux/drivers/net/wireless/realtek/rtlwifi/rtl8723ae/trx.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
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 
_rtl8723e_map_hwqueue_to_fwqueue(struct sk_buff * skb,u8 hw_queue)14 static u8 _rtl8723e_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) || ieee80211_is_ctl(fc))
21 		return QSLT_MGNT;
22 
23 	return skb->priority;
24 }
25 
_rtl8723e_query_rxphystatus(struct ieee80211_hw * hw,struct rtl_stats * pstatus,u8 * pdesc,struct rx_fwinfo_8723e * p_drvinfo,bool bpacket_match_bssid,bool bpacket_toself,bool packet_beacon)26 static void _rtl8723e_query_rxphystatus(struct ieee80211_hw *hw,
27 					struct rtl_stats *pstatus, u8 *pdesc,
28 					struct rx_fwinfo_8723e *p_drvinfo,
29 					bool bpacket_match_bssid,
30 					bool bpacket_toself, bool packet_beacon)
31 {
32 	struct rtl_priv *rtlpriv = rtl_priv(hw);
33 	struct rtl_ps_ctl *ppsc = rtl_psc(rtlpriv);
34 	struct phy_sts_cck_8723e_t *cck_buf;
35 	s8 rx_pwr_all = 0, rx_pwr[4];
36 	u8 rf_rx_num = 0, evm, pwdb_all;
37 	u8 i, max_spatial_stream;
38 	u32 rssi, total_rssi = 0;
39 	bool is_cck = pstatus->is_cck;
40 
41 	/* Record it for next packet processing */
42 	pstatus->packet_matchbssid = bpacket_match_bssid;
43 	pstatus->packet_toself = bpacket_toself;
44 	pstatus->packet_beacon = packet_beacon;
45 	pstatus->rx_mimo_signalquality[0] = -1;
46 	pstatus->rx_mimo_signalquality[1] = -1;
47 
48 	if (is_cck) {
49 		u8 report, cck_highpwr;
50 
51 		/* CCK Driver info Structure is not the same as OFDM packet. */
52 		cck_buf = (struct phy_sts_cck_8723e_t *)p_drvinfo;
53 
54 		/* (1)Hardware does not provide RSSI for CCK */
55 		/* (2)PWDB, Average PWDB calculated by
56 		 * hardware (for rate adaptive)
57 		 */
58 		if (ppsc->rfpwr_state == ERFON)
59 			cck_highpwr = (u8)rtl_get_bbreg(hw,
60 					RFPGA0_XA_HSSIPARAMETER2,
61 					BIT(9));
62 		else
63 			cck_highpwr = false;
64 
65 		if (!cck_highpwr) {
66 			u8 cck_agc_rpt = cck_buf->cck_agc_rpt;
67 			report = cck_buf->cck_agc_rpt & 0xc0;
68 			report = report >> 6;
69 			switch (report) {
70 			case 0x3:
71 				rx_pwr_all = -46 - (cck_agc_rpt & 0x3e);
72 				break;
73 			case 0x2:
74 				rx_pwr_all = -26 - (cck_agc_rpt & 0x3e);
75 				break;
76 			case 0x1:
77 				rx_pwr_all = -12 - (cck_agc_rpt & 0x3e);
78 				break;
79 			case 0x0:
80 				rx_pwr_all = 16 - (cck_agc_rpt & 0x3e);
81 				break;
82 			}
83 		} else {
84 			u8 cck_agc_rpt = cck_buf->cck_agc_rpt;
85 			report = p_drvinfo->cfosho[0] & 0x60;
86 			report = report >> 5;
87 			switch (report) {
88 			case 0x3:
89 				rx_pwr_all = -46 - ((cck_agc_rpt & 0x1f) << 1);
90 				break;
91 			case 0x2:
92 				rx_pwr_all = -26 - ((cck_agc_rpt & 0x1f) << 1);
93 				break;
94 			case 0x1:
95 				rx_pwr_all = -12 - ((cck_agc_rpt & 0x1f) << 1);
96 				break;
97 			case 0x0:
98 				rx_pwr_all = 16 - ((cck_agc_rpt & 0x1f) << 1);
99 				break;
100 			}
101 		}
102 
103 		pwdb_all = rtl_query_rxpwrpercentage(rx_pwr_all);
104 		/* CCK gain is smaller than OFDM/MCS gain,  */
105 		/* so we add gain diff by experiences,
106 		 * the val is 6
107 		 */
108 		pwdb_all += 6;
109 		if (pwdb_all > 100)
110 			pwdb_all = 100;
111 		/* modify the offset to make the same
112 		 * gain index with OFDM.
113 		 */
114 		if (pwdb_all > 34 && pwdb_all <= 42)
115 			pwdb_all -= 2;
116 		else if (pwdb_all > 26 && pwdb_all <= 34)
117 			pwdb_all -= 6;
118 		else if (pwdb_all > 14 && pwdb_all <= 26)
119 			pwdb_all -= 8;
120 		else if (pwdb_all > 4 && pwdb_all <= 14)
121 			pwdb_all -= 4;
122 
123 		pstatus->rx_pwdb_all = pwdb_all;
124 		pstatus->recvsignalpower = rx_pwr_all;
125 
126 		/* (3) Get Signal Quality (EVM) */
127 		if (bpacket_match_bssid) {
128 			u8 sq;
129 
130 			if (pstatus->rx_pwdb_all > 40)
131 				sq = 100;
132 			else {
133 				sq = cck_buf->sq_rpt;
134 				if (sq > 64)
135 					sq = 0;
136 				else if (sq < 20)
137 					sq = 100;
138 				else
139 					sq = ((64 - sq) * 100) / 44;
140 			}
141 
142 			pstatus->signalquality = sq;
143 			pstatus->rx_mimo_signalquality[0] = sq;
144 			pstatus->rx_mimo_signalquality[1] = -1;
145 		}
146 	} else {
147 		rtlpriv->dm.rfpath_rxenable[0] =
148 		    rtlpriv->dm.rfpath_rxenable[1] = true;
149 
150 		/* (1)Get RSSI for HT rate */
151 		for (i = RF90_PATH_A; i < RF6052_MAX_PATH; i++) {
152 
153 			/* we will judge RF RX path now. */
154 			if (rtlpriv->dm.rfpath_rxenable[i])
155 				rf_rx_num++;
156 
157 			rx_pwr[i] = ((p_drvinfo->gain_trsw[i] &
158 				      0x3f) * 2) - 110;
159 
160 			/* Translate DBM to percentage. */
161 			rssi = rtl_query_rxpwrpercentage(rx_pwr[i]);
162 			total_rssi += rssi;
163 
164 			/* Get Rx snr value in DB */
165 			rtlpriv->stats.rx_snr_db[i] =
166 				(long)(p_drvinfo->rxsnr[i] / 2);
167 
168 			/* Record Signal Strength for next packet */
169 			if (bpacket_match_bssid)
170 				pstatus->rx_mimo_signalstrength[i] = (u8)rssi;
171 		}
172 
173 		/* (2)PWDB, Average PWDB calculated by
174 		 * hardware (for rate adaptive)
175 		 */
176 		rx_pwr_all = ((p_drvinfo->pwdb_all >> 1) & 0x7f) - 110;
177 
178 		pwdb_all = rtl_query_rxpwrpercentage(rx_pwr_all);
179 		pstatus->rx_pwdb_all = pwdb_all;
180 		pstatus->rxpower = rx_pwr_all;
181 		pstatus->recvsignalpower = rx_pwr_all;
182 
183 		/* (3)EVM of HT rate */
184 		if (pstatus->is_ht && pstatus->rate >= DESC92C_RATEMCS8 &&
185 		    pstatus->rate <= DESC92C_RATEMCS15)
186 			max_spatial_stream = 2;
187 		else
188 			max_spatial_stream = 1;
189 
190 		for (i = 0; i < max_spatial_stream; i++) {
191 			evm = rtl_evm_db_to_percentage(p_drvinfo->rxevm[i]);
192 
193 			if (bpacket_match_bssid) {
194 				/* Fill value in RFD, Get the first
195 				 * spatial stream only
196 				 */
197 				if (i == 0)
198 					pstatus->signalquality =
199 						(u8)(evm & 0xff);
200 				pstatus->rx_mimo_signalquality[i] =
201 					(u8)(evm & 0xff);
202 			}
203 		}
204 	}
205 
206 	/* UI BSS List signal strength(in percentage),
207 	 * make it good looking, from 0~100.
208 	 */
209 	if (is_cck)
210 		pstatus->signalstrength = (u8)(rtl_signal_scale_mapping(hw,
211 			pwdb_all));
212 	else if (rf_rx_num != 0)
213 		pstatus->signalstrength = (u8)(rtl_signal_scale_mapping(hw,
214 			total_rssi /= rf_rx_num));
215 }
216 
translate_rx_signal_stuff(struct ieee80211_hw * hw,struct sk_buff * skb,struct rtl_stats * pstatus,u8 * pdesc,struct rx_fwinfo_8723e * p_drvinfo)217 static void translate_rx_signal_stuff(struct ieee80211_hw *hw,
218 				      struct sk_buff *skb,
219 				      struct rtl_stats *pstatus, u8 *pdesc,
220 				      struct rx_fwinfo_8723e *p_drvinfo)
221 {
222 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
223 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
224 	struct ieee80211_hdr *hdr;
225 	u8 *tmp_buf;
226 	u8 *praddr;
227 	/*u8 *psaddr;*/
228 	u16 fc, type;
229 	bool packet_matchbssid, packet_toself, packet_beacon;
230 
231 	tmp_buf = skb->data + pstatus->rx_drvinfo_size + pstatus->rx_bufshift;
232 
233 	hdr = (struct ieee80211_hdr *)tmp_buf;
234 	fc = le16_to_cpu(hdr->frame_control);
235 	type = WLAN_FC_GET_TYPE(hdr->frame_control);
236 	praddr = hdr->addr1;
237 
238 	packet_matchbssid = ((IEEE80211_FTYPE_CTL != type) &&
239 		(ether_addr_equal(mac->bssid, (fc & IEEE80211_FCTL_TODS) ?
240 		 hdr->addr1 : (fc & IEEE80211_FCTL_FROMDS) ?
241 		 hdr->addr2 : hdr->addr3)) &&
242 		 (!pstatus->hwerror) &&
243 		 (!pstatus->crc) && (!pstatus->icv));
244 
245 	packet_toself = packet_matchbssid &&
246 	    (ether_addr_equal(praddr, rtlefuse->dev_addr));
247 
248 	if (ieee80211_is_beacon(hdr->frame_control))
249 		packet_beacon = true;
250 	else
251 		packet_beacon = false;
252 
253 	_rtl8723e_query_rxphystatus(hw, pstatus, pdesc, p_drvinfo,
254 				    packet_matchbssid, packet_toself,
255 				    packet_beacon);
256 
257 	rtl_process_phyinfo(hw, tmp_buf, pstatus);
258 }
259 
rtl8723e_rx_query_desc(struct ieee80211_hw * hw,struct rtl_stats * status,struct ieee80211_rx_status * rx_status,u8 * pdesc8,struct sk_buff * skb)260 bool rtl8723e_rx_query_desc(struct ieee80211_hw *hw,
261 			    struct rtl_stats *status,
262 			    struct ieee80211_rx_status *rx_status,
263 			    u8 *pdesc8, struct sk_buff *skb)
264 {
265 	struct rx_fwinfo_8723e *p_drvinfo;
266 	struct ieee80211_hdr *hdr;
267 	__le32 *pdesc = (__le32 *)pdesc8;
268 	u32 phystatus = get_rx_desc_physt(pdesc);
269 
270 	status->length = (u16)get_rx_desc_pkt_len(pdesc);
271 	status->rx_drvinfo_size = (u8)get_rx_desc_drv_info_size(pdesc) *
272 	    RX_DRV_INFO_SIZE_UNIT;
273 	status->rx_bufshift = (u8)(get_rx_desc_shift(pdesc) & 0x03);
274 	status->icv = (u16)get_rx_desc_icv(pdesc);
275 	status->crc = (u16)get_rx_desc_crc32(pdesc);
276 	status->hwerror = (status->crc | status->icv);
277 	status->decrypted = !get_rx_desc_swdec(pdesc);
278 	status->rate = (u8)get_rx_desc_rxmcs(pdesc);
279 	status->shortpreamble = (u16)get_rx_desc_splcp(pdesc);
280 	status->isampdu = (bool)(get_rx_desc_paggr(pdesc) == 1);
281 	status->isfirst_ampdu = (bool)((get_rx_desc_paggr(pdesc) == 1) &&
282 				       (get_rx_desc_faggr(pdesc) == 1));
283 	status->timestamp_low = get_rx_desc_tsfl(pdesc);
284 	status->rx_is40mhzpacket = (bool)get_rx_desc_bw(pdesc);
285 	status->is_ht = (bool)get_rx_desc_rxht(pdesc);
286 
287 	status->is_cck = RX_HAL_IS_CCK_RATE(status->rate);
288 
289 	rx_status->freq = hw->conf.chandef.chan->center_freq;
290 	rx_status->band = hw->conf.chandef.chan->band;
291 
292 	hdr = (struct ieee80211_hdr *)(skb->data + status->rx_drvinfo_size
293 			+ status->rx_bufshift);
294 
295 	if (status->crc)
296 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
297 
298 	if (status->rx_is40mhzpacket)
299 		rx_status->bw = RATE_INFO_BW_40;
300 
301 	if (status->is_ht)
302 		rx_status->encoding = RX_ENC_HT;
303 
304 	rx_status->flag |= RX_FLAG_MACTIME_START;
305 
306 	/* hw will set status->decrypted true, if it finds the
307 	 * frame is open data frame or mgmt frame.
308 	 * So hw will not decryption robust managment frame
309 	 * for IEEE80211w but still set status->decrypted
310 	 * true, so here we should set it back to undecrypted
311 	 * for IEEE80211w frame, and mac80211 sw will help
312 	 * to decrypt it
313 	 */
314 	if (status->decrypted) {
315 		if ((!_ieee80211_is_robust_mgmt_frame(hdr)) &&
316 		    (ieee80211_has_protected(hdr->frame_control)))
317 			rx_status->flag |= RX_FLAG_DECRYPTED;
318 		else
319 			rx_status->flag &= ~RX_FLAG_DECRYPTED;
320 	}
321 
322 	/* rate_idx: index of data rate into band's
323 	 * supported rates or MCS index if HT rates
324 	 * are use (RX_FLAG_HT)
325 	 * Notice: this is diff with windows define
326 	 */
327 	rx_status->rate_idx = rtlwifi_rate_mapping(hw, status->is_ht,
328 						   false, status->rate);
329 
330 	rx_status->mactime = status->timestamp_low;
331 	if (phystatus == true) {
332 		p_drvinfo = (struct rx_fwinfo_8723e *)(skb->data +
333 						     status->rx_bufshift);
334 
335 		translate_rx_signal_stuff(hw, skb, status, pdesc8, p_drvinfo);
336 	}
337 	rx_status->signal = status->recvsignalpower + 10;
338 	return true;
339 }
340 
rtl8723e_tx_fill_desc(struct ieee80211_hw * hw,struct ieee80211_hdr * hdr,u8 * pdesc_tx,u8 * txbd,struct ieee80211_tx_info * info,struct ieee80211_sta * sta,struct sk_buff * skb,u8 hw_queue,struct rtl_tcb_desc * ptcb_desc)341 void rtl8723e_tx_fill_desc(struct ieee80211_hw *hw,
342 			   struct ieee80211_hdr *hdr, u8 *pdesc_tx,
343 			   u8 *txbd, struct ieee80211_tx_info *info,
344 			   struct ieee80211_sta *sta,
345 			   struct sk_buff *skb,
346 			   u8 hw_queue, struct rtl_tcb_desc *ptcb_desc)
347 {
348 	struct rtl_priv *rtlpriv = rtl_priv(hw);
349 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
350 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
351 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
352 	/* bool b_trigger_ac = false; */
353 	u8 *pdesc8 = (u8 *)pdesc_tx;
354 	__le32 *pdesc = (__le32 *)pdesc8;
355 	u16 seq_number;
356 	__le16 fc = hdr->frame_control;
357 	u8 fw_qsel = _rtl8723e_map_hwqueue_to_fwqueue(skb, hw_queue);
358 	bool firstseg = ((hdr->seq_ctrl &
359 			  cpu_to_le16(IEEE80211_SCTL_FRAG)) == 0);
360 
361 	bool lastseg = ((hdr->frame_control &
362 			 cpu_to_le16(IEEE80211_FCTL_MOREFRAGS)) == 0);
363 
364 	dma_addr_t mapping = dma_map_single(&rtlpci->pdev->dev, skb->data,
365 					    skb->len, DMA_TO_DEVICE);
366 	u8 bw_40 = 0;
367 
368 	if (dma_mapping_error(&rtlpci->pdev->dev, mapping)) {
369 		rtl_dbg(rtlpriv, COMP_SEND, DBG_TRACE,
370 			"DMA mapping error\n");
371 		return;
372 	}
373 	if (mac->opmode == NL80211_IFTYPE_STATION) {
374 		bw_40 = mac->bw_40;
375 	} else if (mac->opmode == NL80211_IFTYPE_AP ||
376 		mac->opmode == NL80211_IFTYPE_ADHOC) {
377 		if (sta)
378 			bw_40 = sta->deflink.ht_cap.cap &
379 				IEEE80211_HT_CAP_SUP_WIDTH_20_40;
380 	}
381 
382 	seq_number = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4;
383 
384 	rtl_get_tcb_desc(hw, info, sta, skb, ptcb_desc);
385 
386 	clear_pci_tx_desc_content(pdesc, sizeof(struct tx_desc_8723e));
387 
388 	if (ieee80211_is_nullfunc(fc) || ieee80211_is_ctl(fc)) {
389 		firstseg = true;
390 		lastseg = true;
391 	}
392 
393 	if (firstseg) {
394 		set_tx_desc_offset(pdesc, USB_HWDESC_HEADER_LEN);
395 
396 		set_tx_desc_tx_rate(pdesc, ptcb_desc->hw_rate);
397 
398 		if (ptcb_desc->use_shortgi || ptcb_desc->use_shortpreamble)
399 			set_tx_desc_data_shortgi(pdesc, 1);
400 
401 		if (info->flags & IEEE80211_TX_CTL_AMPDU) {
402 			set_tx_desc_agg_break(pdesc, 1);
403 			set_tx_desc_max_agg_num(pdesc, 0x14);
404 		}
405 		set_tx_desc_seq(pdesc, seq_number);
406 
407 		set_tx_desc_rts_enable(pdesc,
408 				       ((ptcb_desc->rts_enable &&
409 					!ptcb_desc->cts_enable) ? 1 : 0));
410 		set_tx_desc_hw_rts_enable(pdesc,
411 					  ((ptcb_desc->rts_enable ||
412 					  ptcb_desc->cts_enable) ? 1 : 0));
413 		set_tx_desc_cts2self(pdesc,
414 				     ((ptcb_desc->cts_enable) ? 1 : 0));
415 		set_tx_desc_rts_stbc(pdesc,
416 				     ((ptcb_desc->rts_stbc) ? 1 : 0));
417 
418 		set_tx_desc_rts_rate(pdesc, ptcb_desc->rts_rate);
419 		set_tx_desc_rts_bw(pdesc, 0);
420 		set_tx_desc_rts_sc(pdesc, ptcb_desc->rts_sc);
421 		set_tx_desc_rts_short(pdesc,
422 				((ptcb_desc->rts_rate <= DESC92C_RATE54M) ?
423 				(ptcb_desc->rts_use_shortpreamble ? 1 : 0)
424 				: (ptcb_desc->rts_use_shortgi ? 1 : 0)));
425 
426 		if (bw_40) {
427 			if (ptcb_desc->packet_bw == HT_CHANNEL_WIDTH_20_40) {
428 				set_tx_desc_data_bw(pdesc, 1);
429 				set_tx_desc_tx_sub_carrier(pdesc, 3);
430 			} else {
431 				set_tx_desc_data_bw(pdesc, 0);
432 				set_tx_desc_tx_sub_carrier(pdesc,
433 					mac->cur_40_prime_sc);
434 			}
435 		} else {
436 			set_tx_desc_data_bw(pdesc, 0);
437 			set_tx_desc_tx_sub_carrier(pdesc, 0);
438 		}
439 
440 		set_tx_desc_linip(pdesc, 0);
441 		set_tx_desc_pkt_size(pdesc, (u16)skb->len);
442 
443 		if (sta) {
444 			u8 ampdu_density = sta->deflink.ht_cap.ampdu_density;
445 			set_tx_desc_ampdu_density(pdesc, ampdu_density);
446 		}
447 
448 		if (info->control.hw_key) {
449 			struct ieee80211_key_conf *keyconf =
450 			    info->control.hw_key;
451 
452 			switch (keyconf->cipher) {
453 			case WLAN_CIPHER_SUITE_WEP40:
454 			case WLAN_CIPHER_SUITE_WEP104:
455 			case WLAN_CIPHER_SUITE_TKIP:
456 				set_tx_desc_sec_type(pdesc, 0x1);
457 				break;
458 			case WLAN_CIPHER_SUITE_CCMP:
459 				set_tx_desc_sec_type(pdesc, 0x3);
460 				break;
461 			default:
462 				set_tx_desc_sec_type(pdesc, 0x0);
463 				break;
464 
465 			}
466 		}
467 
468 		set_tx_desc_pkt_id(pdesc, 0);
469 		set_tx_desc_queue_sel(pdesc, fw_qsel);
470 
471 		set_tx_desc_data_rate_fb_limit(pdesc, 0x1F);
472 		set_tx_desc_rts_rate_fb_limit(pdesc, 0xF);
473 		set_tx_desc_disable_fb(pdesc, 0);
474 		set_tx_desc_use_rate(pdesc, ptcb_desc->use_driver_rate ? 1 : 0);
475 
476 		if (ieee80211_is_data_qos(fc)) {
477 			if (mac->rdg_en) {
478 				rtl_dbg(rtlpriv, COMP_SEND, DBG_TRACE,
479 					"Enable RDG function.\n");
480 				set_tx_desc_rdg_enable(pdesc, 1);
481 				set_tx_desc_htc(pdesc, 1);
482 			}
483 		}
484 	}
485 
486 	set_tx_desc_first_seg(pdesc, (firstseg ? 1 : 0));
487 	set_tx_desc_last_seg(pdesc, (lastseg ? 1 : 0));
488 
489 	set_tx_desc_tx_buffer_size(pdesc, (u16)skb->len);
490 
491 	set_tx_desc_tx_buffer_address(pdesc, mapping);
492 
493 	if (rtlpriv->dm.useramask) {
494 		set_tx_desc_rate_id(pdesc, ptcb_desc->ratr_index);
495 		set_tx_desc_macid(pdesc, ptcb_desc->mac_id);
496 	} else {
497 		set_tx_desc_rate_id(pdesc, 0xC + ptcb_desc->ratr_index);
498 		set_tx_desc_macid(pdesc, ptcb_desc->ratr_index);
499 	}
500 
501 	if ((!ieee80211_is_data_qos(fc)) && ppsc->fwctrl_lps) {
502 		set_tx_desc_hwseq_en_8723(pdesc, 1);
503 		/* set_tx_desc_hwseq_en(pdesc, 1); */
504 		/* set_tx_desc_pkt_id(pdesc, 8); */
505 		/* set_tx_desc_qos(pdesc, 1); */
506 	}
507 
508 	set_tx_desc_more_frag(pdesc, (lastseg ? 0 : 1));
509 
510 	if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
511 	    is_broadcast_ether_addr(ieee80211_get_DA(hdr))) {
512 		set_tx_desc_bmc(pdesc, 1);
513 	}
514 
515 	rtl_dbg(rtlpriv, COMP_SEND, DBG_TRACE, "\n");
516 }
517 
rtl8723e_tx_fill_cmddesc(struct ieee80211_hw * hw,u8 * pdesc8,struct sk_buff * skb)518 void rtl8723e_tx_fill_cmddesc(struct ieee80211_hw *hw, u8 *pdesc8,
519 			      struct sk_buff *skb)
520 {
521 	struct rtl_priv *rtlpriv = rtl_priv(hw);
522 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
523 	u8 fw_queue = QSLT_BEACON;
524 	__le32 *pdesc = (__le32 *)pdesc8;
525 
526 	struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
527 	__le16 fc = hdr->frame_control;
528 
529 	dma_addr_t mapping = dma_map_single(&rtlpci->pdev->dev, skb->data,
530 					    skb->len, DMA_TO_DEVICE);
531 
532 	if (dma_mapping_error(&rtlpci->pdev->dev, mapping)) {
533 		rtl_dbg(rtlpriv, COMP_SEND, DBG_TRACE,
534 			"DMA mapping error\n");
535 		return;
536 	}
537 	clear_pci_tx_desc_content(pdesc, TX_DESC_SIZE);
538 
539 	set_tx_desc_offset(pdesc, USB_HWDESC_HEADER_LEN);
540 
541 	set_tx_desc_tx_rate(pdesc, DESC92C_RATE1M);
542 
543 	set_tx_desc_seq(pdesc, 0);
544 
545 	set_tx_desc_linip(pdesc, 0);
546 
547 	set_tx_desc_queue_sel(pdesc, fw_queue);
548 
549 	set_tx_desc_first_seg(pdesc, 1);
550 	set_tx_desc_last_seg(pdesc, 1);
551 
552 	set_tx_desc_tx_buffer_size(pdesc, (u16)(skb->len));
553 
554 	set_tx_desc_tx_buffer_address(pdesc, mapping);
555 
556 	set_tx_desc_rate_id(pdesc, 7);
557 	set_tx_desc_macid(pdesc, 0);
558 
559 	set_tx_desc_own(pdesc, 1);
560 
561 	set_tx_desc_pkt_size(pdesc, (u16)(skb->len));
562 
563 	set_tx_desc_first_seg(pdesc, 1);
564 	set_tx_desc_last_seg(pdesc, 1);
565 
566 	set_tx_desc_offset(pdesc, 0x20);
567 
568 	set_tx_desc_use_rate(pdesc, 1);
569 
570 	if (!ieee80211_is_data_qos(fc)) {
571 		set_tx_desc_hwseq_en_8723(pdesc, 1);
572 		/* set_tx_desc_hwseq_en(pdesc, 1); */
573 		/* set_tx_desc_pkt_id(pdesc, 8); */
574 	}
575 
576 	RT_PRINT_DATA(rtlpriv, COMP_CMD, DBG_LOUD,
577 		      "H2C Tx Cmd Content\n",
578 		      pdesc, TX_DESC_SIZE);
579 }
580 
rtl8723e_set_desc(struct ieee80211_hw * hw,u8 * pdesc8,bool istx,u8 desc_name,u8 * val)581 void rtl8723e_set_desc(struct ieee80211_hw *hw, u8 *pdesc8,
582 		       bool istx, u8 desc_name, u8 *val)
583 {
584 	__le32 *pdesc = (__le32 *)pdesc8;
585 
586 	if (istx) {
587 		switch (desc_name) {
588 		case HW_DESC_OWN:
589 			set_tx_desc_own(pdesc, 1);
590 			break;
591 		case HW_DESC_TX_NEXTDESC_ADDR:
592 			set_tx_desc_next_desc_address(pdesc, *(u32 *)val);
593 			break;
594 		default:
595 			WARN_ONCE(true, "rtl8723ae: ERR txdesc :%d not processed\n",
596 				  desc_name);
597 			break;
598 		}
599 	} else {
600 		switch (desc_name) {
601 		case HW_DESC_RXOWN:
602 			set_rx_desc_own(pdesc, 1);
603 			break;
604 		case HW_DESC_RXBUFF_ADDR:
605 			set_rx_desc_buff_addr(pdesc, *(u32 *)val);
606 			break;
607 		case HW_DESC_RXPKT_LEN:
608 			set_rx_desc_pkt_len(pdesc, *(u32 *)val);
609 			break;
610 		case HW_DESC_RXERO:
611 			set_rx_desc_eor(pdesc, 1);
612 			break;
613 		default:
614 			WARN_ONCE(true, "rtl8723ae: ERR rxdesc :%d not processed\n",
615 				  desc_name);
616 			break;
617 		}
618 	}
619 }
620 
rtl8723e_get_desc(struct ieee80211_hw * hw,u8 * pdesc8,bool istx,u8 desc_name)621 u64 rtl8723e_get_desc(struct ieee80211_hw *hw,
622 		      u8 *pdesc8, bool istx, u8 desc_name)
623 {
624 	u32 ret = 0;
625 	__le32 *pdesc = (__le32 *)pdesc8;
626 
627 	if (istx) {
628 		switch (desc_name) {
629 		case HW_DESC_OWN:
630 			ret = get_tx_desc_own(pdesc);
631 			break;
632 		case HW_DESC_TXBUFF_ADDR:
633 			ret = get_tx_desc_tx_buffer_address(pdesc);
634 			break;
635 		default:
636 			WARN_ONCE(true, "rtl8723ae: ERR txdesc :%d not processed\n",
637 				  desc_name);
638 			break;
639 		}
640 	} else {
641 		switch (desc_name) {
642 		case HW_DESC_OWN:
643 			ret = get_rx_desc_own(pdesc);
644 			break;
645 		case HW_DESC_RXPKT_LEN:
646 			ret = get_rx_desc_pkt_len(pdesc);
647 			break;
648 		case HW_DESC_RXBUFF_ADDR:
649 			ret = get_rx_desc_buff_addr(pdesc);
650 			break;
651 		default:
652 			WARN_ONCE(true, "rtl8723ae: ERR rxdesc :%d not processed\n",
653 				  desc_name);
654 			break;
655 		}
656 	}
657 	return ret;
658 }
659 
rtl8723e_is_tx_desc_closed(struct ieee80211_hw * hw,u8 hw_queue,u16 index)660 bool rtl8723e_is_tx_desc_closed(struct ieee80211_hw *hw,
661 				u8 hw_queue, u16 index)
662 {
663 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
664 	struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[hw_queue];
665 	u8 *entry = (u8 *)(&ring->desc[ring->idx]);
666 	u8 own = (u8)rtl8723e_get_desc(hw, entry, true, HW_DESC_OWN);
667 
668 	/**
669 	 *beacon packet will only use the first
670 	 *descriptor defautly,and the own may not
671 	 *be cleared by the hardware
672 	 */
673 	if (own)
674 		return false;
675 	return true;
676 }
677 
rtl8723e_tx_polling(struct ieee80211_hw * hw,u8 hw_queue)678 void rtl8723e_tx_polling(struct ieee80211_hw *hw, u8 hw_queue)
679 {
680 	struct rtl_priv *rtlpriv = rtl_priv(hw);
681 	if (hw_queue == BEACON_QUEUE) {
682 		rtl_write_word(rtlpriv, REG_PCIE_CTRL_REG, BIT(4));
683 	} else {
684 		rtl_write_word(rtlpriv, REG_PCIE_CTRL_REG,
685 			       BIT(0) << (hw_queue));
686 	}
687 }
688