xref: /linux/drivers/net/wireless/realtek/rtlwifi/rtl8192de/trx.c (revision 55d0969c451159cff86949b38c39171cab962069)
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 "../rtl8192d/reg.h"
9 #include "../rtl8192d/def.h"
10 #include "../rtl8192d/phy_common.h"
11 #include "../rtl8192d/trx_common.h"
12 #include "phy.h"
13 #include "trx.h"
14 #include "led.h"
15 
16 static u8 _rtl92de_map_hwqueue_to_fwqueue(struct sk_buff *skb, u8 hw_queue)
17 {
18 	__le16 fc = rtl_get_fc(skb);
19 
20 	if (unlikely(ieee80211_is_beacon(fc)))
21 		return QSLT_BEACON;
22 	if (ieee80211_is_mgmt(fc))
23 		return QSLT_MGNT;
24 
25 	return skb->priority;
26 }
27 
28 static void _rtl92de_insert_emcontent(struct rtl_tcb_desc *ptcb_desc,
29 				      u8 *virtualaddress8)
30 {
31 	__le32 *virtualaddress = (__le32 *)virtualaddress8;
32 
33 	memset(virtualaddress, 0, 8);
34 
35 	set_earlymode_pktnum(virtualaddress, ptcb_desc->empkt_num);
36 	set_earlymode_len0(virtualaddress, ptcb_desc->empkt_len[0]);
37 	set_earlymode_len1(virtualaddress, ptcb_desc->empkt_len[1]);
38 	set_earlymode_len2_1(virtualaddress, ptcb_desc->empkt_len[2] & 0xF);
39 	set_earlymode_len2_2(virtualaddress, ptcb_desc->empkt_len[2] >> 4);
40 	set_earlymode_len3(virtualaddress, ptcb_desc->empkt_len[3]);
41 	set_earlymode_len4(virtualaddress, ptcb_desc->empkt_len[4]);
42 }
43 
44 void rtl92de_tx_fill_desc(struct ieee80211_hw *hw,
45 			  struct ieee80211_hdr *hdr, u8 *pdesc8,
46 			  u8 *pbd_desc_tx, struct ieee80211_tx_info *info,
47 			  struct ieee80211_sta *sta,
48 			  struct sk_buff *skb,
49 			  u8 hw_queue, struct rtl_tcb_desc *ptcb_desc)
50 {
51 	struct rtl_priv *rtlpriv = rtl_priv(hw);
52 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
53 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
54 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
55 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
56 	__le32 *pdesc = (__le32 *)pdesc8;
57 	u16 seq_number;
58 	__le16 fc = hdr->frame_control;
59 	unsigned int buf_len = 0;
60 	unsigned int skb_len = skb->len;
61 	u8 fw_qsel = _rtl92de_map_hwqueue_to_fwqueue(skb, hw_queue);
62 	bool firstseg = ((hdr->seq_ctrl &
63 			cpu_to_le16(IEEE80211_SCTL_FRAG)) == 0);
64 	bool lastseg = ((hdr->frame_control &
65 			cpu_to_le16(IEEE80211_FCTL_MOREFRAGS)) == 0);
66 	dma_addr_t mapping;
67 	u8 bw_40 = 0;
68 
69 	if (mac->opmode == NL80211_IFTYPE_STATION) {
70 		bw_40 = mac->bw_40;
71 	} else if (mac->opmode == NL80211_IFTYPE_AP ||
72 		mac->opmode == NL80211_IFTYPE_ADHOC) {
73 		if (sta)
74 			bw_40 = sta->deflink.bandwidth >= IEEE80211_STA_RX_BW_40;
75 	}
76 	seq_number = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4;
77 	rtl_get_tcb_desc(hw, info, sta, skb, ptcb_desc);
78 	/* reserve 8 byte for AMPDU early mode */
79 	if (rtlhal->earlymode_enable) {
80 		skb_push(skb, EM_HDR_LEN);
81 		memset(skb->data, 0, EM_HDR_LEN);
82 	}
83 	buf_len = skb->len;
84 	mapping = dma_map_single(&rtlpci->pdev->dev, skb->data, skb->len,
85 				 DMA_TO_DEVICE);
86 	if (dma_mapping_error(&rtlpci->pdev->dev, mapping)) {
87 		rtl_dbg(rtlpriv, COMP_SEND, DBG_TRACE,
88 			"DMA mapping error\n");
89 		return;
90 	}
91 	clear_pci_tx_desc_content(pdesc, sizeof(struct tx_desc_92d));
92 	if (ieee80211_is_nullfunc(fc) || ieee80211_is_ctl(fc)) {
93 		firstseg = true;
94 		lastseg = true;
95 	}
96 	if (firstseg) {
97 		if (rtlhal->earlymode_enable) {
98 			set_tx_desc_pkt_offset(pdesc, 1);
99 			set_tx_desc_offset(pdesc, USB_HWDESC_HEADER_LEN +
100 					   EM_HDR_LEN);
101 			if (ptcb_desc->empkt_num) {
102 				rtl_dbg(rtlpriv, COMP_SEND, DBG_LOUD,
103 					"Insert 8 byte.pTcb->EMPktNum:%d\n",
104 					ptcb_desc->empkt_num);
105 				_rtl92de_insert_emcontent(ptcb_desc,
106 							  (u8 *)(skb->data));
107 			}
108 		} else {
109 			set_tx_desc_offset(pdesc, USB_HWDESC_HEADER_LEN);
110 		}
111 		/* 5G have no CCK rate */
112 		if (rtlhal->current_bandtype == BAND_ON_5G)
113 			if (ptcb_desc->hw_rate < DESC_RATE6M)
114 				ptcb_desc->hw_rate = DESC_RATE6M;
115 		set_tx_desc_tx_rate(pdesc, ptcb_desc->hw_rate);
116 		if (ptcb_desc->use_shortgi || ptcb_desc->use_shortpreamble)
117 			set_tx_desc_data_shortgi(pdesc, 1);
118 
119 		if (rtlhal->macphymode == DUALMAC_DUALPHY &&
120 			ptcb_desc->hw_rate == DESC_RATEMCS7)
121 			set_tx_desc_data_shortgi(pdesc, 1);
122 
123 		if (info->flags & IEEE80211_TX_CTL_AMPDU) {
124 			set_tx_desc_agg_enable(pdesc, 1);
125 			set_tx_desc_max_agg_num(pdesc, 0x14);
126 		}
127 		set_tx_desc_seq(pdesc, seq_number);
128 		set_tx_desc_rts_enable(pdesc,
129 				       ((ptcb_desc->rts_enable &&
130 					!ptcb_desc->cts_enable) ? 1 : 0));
131 		set_tx_desc_hw_rts_enable(pdesc, ((ptcb_desc->rts_enable
132 					  || ptcb_desc->cts_enable) ? 1 : 0));
133 		set_tx_desc_cts2self(pdesc, ((ptcb_desc->cts_enable) ? 1 : 0));
134 		set_tx_desc_rts_stbc(pdesc, ((ptcb_desc->rts_stbc) ? 1 : 0));
135 		/* 5G have no CCK rate */
136 		if (rtlhal->current_bandtype == BAND_ON_5G)
137 			if (ptcb_desc->rts_rate < DESC_RATE6M)
138 				ptcb_desc->rts_rate = DESC_RATE6M;
139 		set_tx_desc_rts_rate(pdesc, ptcb_desc->rts_rate);
140 		set_tx_desc_rts_bw(pdesc, 0);
141 		set_tx_desc_rts_sc(pdesc, ptcb_desc->rts_sc);
142 		set_tx_desc_rts_short(pdesc, ((ptcb_desc->rts_rate <=
143 			DESC_RATE54M) ?
144 			(ptcb_desc->rts_use_shortpreamble ? 1 : 0) :
145 			(ptcb_desc->rts_use_shortgi ? 1 : 0)));
146 		if (bw_40) {
147 			if (ptcb_desc->packet_bw) {
148 				set_tx_desc_data_bw(pdesc, 1);
149 				set_tx_desc_tx_sub_carrier(pdesc, 3);
150 			} else {
151 				set_tx_desc_data_bw(pdesc, 0);
152 				set_tx_desc_tx_sub_carrier(pdesc,
153 							mac->cur_40_prime_sc);
154 			}
155 		} else {
156 			set_tx_desc_data_bw(pdesc, 0);
157 			set_tx_desc_tx_sub_carrier(pdesc, 0);
158 		}
159 		set_tx_desc_linip(pdesc, 0);
160 		set_tx_desc_pkt_size(pdesc, (u16)skb_len);
161 		if (sta) {
162 			u8 ampdu_density = sta->deflink.ht_cap.ampdu_density;
163 			set_tx_desc_ampdu_density(pdesc, ampdu_density);
164 		}
165 		if (info->control.hw_key) {
166 			struct ieee80211_key_conf *keyconf;
167 
168 			keyconf = info->control.hw_key;
169 			switch (keyconf->cipher) {
170 			case WLAN_CIPHER_SUITE_WEP40:
171 			case WLAN_CIPHER_SUITE_WEP104:
172 			case WLAN_CIPHER_SUITE_TKIP:
173 				set_tx_desc_sec_type(pdesc, 0x1);
174 				break;
175 			case WLAN_CIPHER_SUITE_CCMP:
176 				set_tx_desc_sec_type(pdesc, 0x3);
177 				break;
178 			default:
179 				set_tx_desc_sec_type(pdesc, 0x0);
180 				break;
181 
182 			}
183 		}
184 		set_tx_desc_pkt_id(pdesc, 0);
185 		set_tx_desc_queue_sel(pdesc, fw_qsel);
186 		set_tx_desc_data_rate_fb_limit(pdesc, 0x1F);
187 		set_tx_desc_rts_rate_fb_limit(pdesc, 0xF);
188 		set_tx_desc_disable_fb(pdesc, ptcb_desc->disable_ratefallback ?
189 				       1 : 0);
190 		set_tx_desc_use_rate(pdesc, ptcb_desc->use_driver_rate ? 1 : 0);
191 
192 		/* Set TxRate and RTSRate in TxDesc  */
193 		/* This prevent Tx initial rate of new-coming packets */
194 		/* from being overwritten by retried  packet rate.*/
195 		if (!ptcb_desc->use_driver_rate) {
196 			set_tx_desc_rts_rate(pdesc, 0x08);
197 			/* set_tx_desc_tx_rate(pdesc, 0x0b); */
198 		}
199 		if (ieee80211_is_data_qos(fc)) {
200 			if (mac->rdg_en) {
201 				rtl_dbg(rtlpriv, COMP_SEND, DBG_TRACE,
202 					"Enable RDG function\n");
203 				set_tx_desc_rdg_enable(pdesc, 1);
204 				set_tx_desc_htc(pdesc, 1);
205 			}
206 		}
207 	}
208 
209 	set_tx_desc_first_seg(pdesc, (firstseg ? 1 : 0));
210 	set_tx_desc_last_seg(pdesc, (lastseg ? 1 : 0));
211 	set_tx_desc_tx_buffer_size(pdesc, (u16)buf_len);
212 	set_tx_desc_tx_buffer_address(pdesc, mapping);
213 	if (rtlpriv->dm.useramask) {
214 		set_tx_desc_rate_id(pdesc, ptcb_desc->ratr_index);
215 		set_tx_desc_macid(pdesc, ptcb_desc->mac_id);
216 	} else {
217 		set_tx_desc_rate_id(pdesc, 0xC + ptcb_desc->ratr_index);
218 		set_tx_desc_macid(pdesc, ptcb_desc->ratr_index);
219 	}
220 	if (ieee80211_is_data_qos(fc))
221 		set_tx_desc_qos(pdesc, 1);
222 
223 	if ((!ieee80211_is_data_qos(fc)) && ppsc->fwctrl_lps) {
224 		set_tx_desc_hwseq_en(pdesc, 1);
225 		set_tx_desc_pkt_id(pdesc, 8);
226 	}
227 	set_tx_desc_more_frag(pdesc, (lastseg ? 0 : 1));
228 	rtl_dbg(rtlpriv, COMP_SEND, DBG_TRACE, "\n");
229 }
230 
231 void rtl92de_tx_fill_cmddesc(struct ieee80211_hw *hw, u8 *pdesc8,
232 			     struct sk_buff *skb)
233 {
234 	struct rtl_priv *rtlpriv = rtl_priv(hw);
235 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
236 	struct rtl_ps_ctl *ppsc = rtl_psc(rtlpriv);
237 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
238 	u8 fw_queue = QSLT_BEACON;
239 
240 	struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
241 	__le16 fc = hdr->frame_control;
242 	__le32 *pdesc = (__le32 *)pdesc8;
243 
244 	dma_addr_t mapping = dma_map_single(&rtlpci->pdev->dev, skb->data,
245 					    skb->len, DMA_TO_DEVICE);
246 
247 	if (dma_mapping_error(&rtlpci->pdev->dev, mapping)) {
248 		rtl_dbg(rtlpriv, COMP_SEND, DBG_TRACE,
249 			"DMA mapping error\n");
250 		return;
251 	}
252 	clear_pci_tx_desc_content(pdesc, TX_DESC_SIZE);
253 	set_tx_desc_offset(pdesc, USB_HWDESC_HEADER_LEN);
254 	/* 5G have no CCK rate
255 	 * Caution: The macros below are multi-line expansions.
256 	 * The braces are needed no matter what checkpatch says
257 	 */
258 	if (rtlhal->current_bandtype == BAND_ON_5G) {
259 		set_tx_desc_tx_rate(pdesc, DESC_RATE6M);
260 	} else {
261 		set_tx_desc_tx_rate(pdesc, DESC_RATE1M);
262 	}
263 	set_tx_desc_seq(pdesc, 0);
264 	set_tx_desc_linip(pdesc, 0);
265 	set_tx_desc_queue_sel(pdesc, fw_queue);
266 	set_tx_desc_first_seg(pdesc, 1);
267 	set_tx_desc_last_seg(pdesc, 1);
268 	set_tx_desc_tx_buffer_size(pdesc, (u16)skb->len);
269 	set_tx_desc_tx_buffer_address(pdesc, mapping);
270 	set_tx_desc_rate_id(pdesc, 7);
271 	set_tx_desc_macid(pdesc, 0);
272 	set_tx_desc_pkt_size(pdesc, (u16)(skb->len));
273 	set_tx_desc_first_seg(pdesc, 1);
274 	set_tx_desc_last_seg(pdesc, 1);
275 	set_tx_desc_offset(pdesc, 0x20);
276 	set_tx_desc_use_rate(pdesc, 1);
277 
278 	if (!ieee80211_is_data_qos(fc) && ppsc->fwctrl_lps) {
279 		set_tx_desc_hwseq_en(pdesc, 1);
280 		set_tx_desc_pkt_id(pdesc, 8);
281 	}
282 
283 	RT_PRINT_DATA(rtlpriv, COMP_CMD, DBG_LOUD,
284 		      "H2C Tx Cmd Content", pdesc, TX_DESC_SIZE);
285 	wmb();
286 	set_tx_desc_own(pdesc, 1);
287 }
288 
289 bool rtl92de_is_tx_desc_closed(struct ieee80211_hw *hw,
290 			       u8 hw_queue, u16 index)
291 {
292 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
293 	struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[hw_queue];
294 	u8 *entry = (u8 *)(&ring->desc[ring->idx]);
295 	u8 own = (u8)rtl92d_get_desc(hw, entry, true, HW_DESC_OWN);
296 
297 	/* a beacon packet will only use the first
298 	 * descriptor by defaut, and the own bit may not
299 	 * be cleared by the hardware
300 	 */
301 	if (own)
302 		return false;
303 	return true;
304 }
305 
306 void rtl92de_tx_polling(struct ieee80211_hw *hw, u8 hw_queue)
307 {
308 	struct rtl_priv *rtlpriv = rtl_priv(hw);
309 	if (hw_queue == BEACON_QUEUE)
310 		rtl_write_word(rtlpriv, REG_PCIE_CTRL_REG, BIT(4));
311 	else
312 		rtl_write_word(rtlpriv, REG_PCIE_CTRL_REG,
313 			       BIT(0) << (hw_queue));
314 }
315