xref: /openbmc/linux/drivers/net/wireless/realtek/rtlwifi/rtl8188ee/trx.c (revision 22fc4c4c9fd60427bcda00878cee94e7622cfa7a)
1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2013  Realtek Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * The full GNU General Public License is included in this distribution in the
15  * file called LICENSE.
16  *
17  * Contact Information:
18  * wlanfae <wlanfae@realtek.com>
19  * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20  * Hsinchu 300, Taiwan.
21  *
22  * Larry Finger <Larry.Finger@lwfinger.net>
23  *
24  *****************************************************************************/
25 
26 #include "../wifi.h"
27 #include "../pci.h"
28 #include "../base.h"
29 #include "../stats.h"
30 #include "reg.h"
31 #include "def.h"
32 #include "trx.h"
33 #include "led.h"
34 #include "dm.h"
35 #include "phy.h"
36 
37 static u8 _rtl88ee_map_hwqueue_to_fwqueue(struct sk_buff *skb, u8 hw_queue)
38 {
39 	__le16 fc = rtl_get_fc(skb);
40 
41 	if (unlikely(ieee80211_is_beacon(fc)))
42 		return QSLT_BEACON;
43 	if (ieee80211_is_mgmt(fc) || ieee80211_is_ctl(fc))
44 		return QSLT_MGNT;
45 
46 	return skb->priority;
47 }
48 
49 static void _rtl88ee_query_rxphystatus(struct ieee80211_hw *hw,
50 			struct rtl_stats *pstatus, u8 *pdesc,
51 			struct rx_fwinfo_88e *p_drvinfo,
52 			bool bpacket_match_bssid,
53 			bool bpacket_toself, bool packet_beacon)
54 {
55 	struct rtl_priv *rtlpriv = rtl_priv(hw);
56 	struct rtl_ps_ctl *ppsc = rtl_psc(rtlpriv);
57 	struct phy_sts_cck_8192s_t *cck_buf;
58 	struct phy_status_rpt *phystrpt =
59 		(struct phy_status_rpt *)p_drvinfo;
60 	struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
61 	s8 rx_pwr_all = 0, rx_pwr[4];
62 	u8 rf_rx_num = 0, evm, pwdb_all;
63 	u8 i, max_spatial_stream;
64 	u32 rssi, total_rssi = 0;
65 	bool is_cck = pstatus->is_cck;
66 	u8 lan_idx, vga_idx;
67 
68 	/* Record it for next packet processing */
69 	pstatus->packet_matchbssid = bpacket_match_bssid;
70 	pstatus->packet_toself = bpacket_toself;
71 	pstatus->packet_beacon = packet_beacon;
72 	pstatus->rx_mimo_signalquality[0] = -1;
73 	pstatus->rx_mimo_signalquality[1] = -1;
74 
75 	if (is_cck) {
76 		u8 cck_highpwr;
77 		u8 cck_agc_rpt;
78 		/* CCK Driver info Structure is not the same as OFDM packet. */
79 		cck_buf = (struct phy_sts_cck_8192s_t *)p_drvinfo;
80 		cck_agc_rpt = cck_buf->cck_agc_rpt;
81 
82 		/* (1)Hardware does not provide RSSI for CCK
83 		 * (2)PWDB, Average PWDB cacluated by
84 		 * hardware (for rate adaptive)
85 		 */
86 		if (ppsc->rfpwr_state == ERFON)
87 			cck_highpwr =
88 				(u8)rtl_get_bbreg(hw, RFPGA0_XA_HSSIPARAMETER2,
89 						  BIT(9));
90 		else
91 			cck_highpwr = false;
92 
93 		lan_idx = ((cck_agc_rpt & 0xE0) >> 5);
94 		vga_idx = (cck_agc_rpt & 0x1f);
95 		switch (lan_idx) {
96 		case 7:
97 			if (vga_idx <= 27)
98 				/*VGA_idx = 27~2*/
99 				rx_pwr_all = -100 + 2*(27-vga_idx);
100 			else
101 				rx_pwr_all = -100;
102 			break;
103 		case 6:
104 			/*VGA_idx = 2~0*/
105 			rx_pwr_all = -48 + 2*(2-vga_idx);
106 			break;
107 		case 5:
108 			/*VGA_idx = 7~5*/
109 			rx_pwr_all = -42 + 2*(7-vga_idx);
110 			break;
111 		case 4:
112 			/*VGA_idx = 7~4*/
113 			rx_pwr_all = -36 + 2*(7-vga_idx);
114 			break;
115 		case 3:
116 			/*VGA_idx = 7~0*/
117 			rx_pwr_all = -24 + 2*(7-vga_idx);
118 			break;
119 		case 2:
120 			if (cck_highpwr)
121 				/*VGA_idx = 5~0*/
122 				rx_pwr_all = -12 + 2*(5-vga_idx);
123 			else
124 				rx_pwr_all = -6 + 2*(5-vga_idx);
125 			break;
126 		case 1:
127 			rx_pwr_all = 8-2*vga_idx;
128 			break;
129 		case 0:
130 			rx_pwr_all = 14-2*vga_idx;
131 			break;
132 		default:
133 			break;
134 		}
135 		rx_pwr_all += 6;
136 		pwdb_all = rtl_query_rxpwrpercentage(rx_pwr_all);
137 		/* CCK gain is smaller than OFDM/MCS gain,  */
138 		/* so we add gain diff by experiences, the val is 6 */
139 		pwdb_all += 6;
140 		if (pwdb_all > 100)
141 			pwdb_all = 100;
142 		/* modify the offset to make the same
143 		 * gain index with OFDM.
144 		 */
145 		if (pwdb_all > 34 && pwdb_all <= 42)
146 			pwdb_all -= 2;
147 		else if (pwdb_all > 26 && pwdb_all <= 34)
148 			pwdb_all -= 6;
149 		else if (pwdb_all > 14 && pwdb_all <= 26)
150 			pwdb_all -= 8;
151 		else if (pwdb_all > 4 && pwdb_all <= 14)
152 			pwdb_all -= 4;
153 		if (!cck_highpwr) {
154 			if (pwdb_all >= 80)
155 				pwdb_all = ((pwdb_all-80)<<1) +
156 					   ((pwdb_all-80)>>1) + 80;
157 			else if ((pwdb_all <= 78) && (pwdb_all >= 20))
158 				pwdb_all += 3;
159 			if (pwdb_all > 100)
160 				pwdb_all = 100;
161 		}
162 
163 		pstatus->rx_pwdb_all = pwdb_all;
164 		pstatus->recvsignalpower = rx_pwr_all;
165 
166 		/* (3) Get Signal Quality (EVM) */
167 		if (bpacket_match_bssid) {
168 			u8 sq;
169 
170 			if (pstatus->rx_pwdb_all > 40)
171 				sq = 100;
172 			else {
173 				sq = cck_buf->sq_rpt;
174 				if (sq > 64)
175 					sq = 0;
176 				else if (sq < 20)
177 					sq = 100;
178 				else
179 					sq = ((64 - sq) * 100) / 44;
180 			}
181 
182 			pstatus->signalquality = sq;
183 			pstatus->rx_mimo_signalquality[0] = sq;
184 			pstatus->rx_mimo_signalquality[1] = -1;
185 		}
186 	} else {
187 		rtlpriv->dm.rfpath_rxenable[0] =
188 		    rtlpriv->dm.rfpath_rxenable[1] = true;
189 
190 		/* (1)Get RSSI for HT rate */
191 		for (i = RF90_PATH_A; i < RF6052_MAX_PATH; i++) {
192 			/* we will judge RF RX path now. */
193 			if (rtlpriv->dm.rfpath_rxenable[i])
194 				rf_rx_num++;
195 
196 			rx_pwr[i] = ((p_drvinfo->gain_trsw[i] &
197 				      0x3f) * 2) - 110;
198 
199 			/* Translate DBM to percentage. */
200 			rssi = rtl_query_rxpwrpercentage(rx_pwr[i]);
201 			total_rssi += rssi;
202 
203 			/* Get Rx snr value in DB */
204 			rtlpriv->stats.rx_snr_db[i] =
205 				(long)(p_drvinfo->rxsnr[i] / 2);
206 
207 			/* Record Signal Strength for next packet */
208 			if (bpacket_match_bssid)
209 				pstatus->rx_mimo_signalstrength[i] = (u8)rssi;
210 		}
211 
212 		/* (2)PWDB, Average PWDB cacluated by
213 		 * hardware (for rate adaptive)
214 		 */
215 		rx_pwr_all = ((p_drvinfo->pwdb_all >> 1) & 0x7f) - 110;
216 
217 		pwdb_all = rtl_query_rxpwrpercentage(rx_pwr_all);
218 		pstatus->rx_pwdb_all = pwdb_all;
219 		pstatus->rxpower = rx_pwr_all;
220 		pstatus->recvsignalpower = rx_pwr_all;
221 
222 		/* (3)EVM of HT rate */
223 		if (pstatus->is_ht && pstatus->rate >= DESC92C_RATEMCS8 &&
224 		    pstatus->rate <= DESC92C_RATEMCS15)
225 			max_spatial_stream = 2;
226 		else
227 			max_spatial_stream = 1;
228 
229 		for (i = 0; i < max_spatial_stream; i++) {
230 			evm = rtl_evm_db_to_percentage(p_drvinfo->rxevm[i]);
231 
232 			if (bpacket_match_bssid) {
233 				/* Fill value in RFD, Get the first
234 				 * spatial stream onlyi
235 				 */
236 				if (i == 0)
237 					pstatus->signalquality =
238 						(u8)(evm & 0xff);
239 				pstatus->rx_mimo_signalquality[i] =
240 					(u8)(evm & 0xff);
241 			}
242 		}
243 	}
244 
245 	/* UI BSS List signal strength(in percentage),
246 	 * make it good looking, from 0~100.
247 	 */
248 	if (is_cck)
249 		pstatus->signalstrength = (u8)(rtl_signal_scale_mapping(hw,
250 			pwdb_all));
251 	else if (rf_rx_num != 0)
252 		pstatus->signalstrength = (u8)(rtl_signal_scale_mapping(hw,
253 			total_rssi /= rf_rx_num));
254 	/*HW antenna diversity*/
255 	rtldm->fat_table.antsel_rx_keep_0 = phystrpt->ant_sel;
256 	rtldm->fat_table.antsel_rx_keep_1 = phystrpt->ant_sel_b;
257 	rtldm->fat_table.antsel_rx_keep_2 = phystrpt->antsel_rx_keep_2;
258 }
259 
260 static void _rtl88ee_smart_antenna(struct ieee80211_hw *hw,
261 	struct rtl_stats *pstatus)
262 {
263 	struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
264 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
265 	u8 antsel_tr_mux;
266 	struct fast_ant_training *pfat_table = &rtldm->fat_table;
267 
268 	if (rtlefuse->antenna_div_type == CG_TRX_SMART_ANTDIV) {
269 		if (pfat_table->fat_state == FAT_TRAINING_STATE) {
270 			if (pstatus->packet_toself) {
271 				antsel_tr_mux =
272 					(pfat_table->antsel_rx_keep_2 << 2) |
273 					(pfat_table->antsel_rx_keep_1 << 1) |
274 					pfat_table->antsel_rx_keep_0;
275 				pfat_table->ant_sum[antsel_tr_mux] +=
276 					pstatus->rx_pwdb_all;
277 				pfat_table->ant_cnt[antsel_tr_mux]++;
278 			}
279 		}
280 	} else if ((rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV) ||
281 	(rtlefuse->antenna_div_type == CGCS_RX_HW_ANTDIV)) {
282 		if (pstatus->packet_toself || pstatus->packet_matchbssid) {
283 			antsel_tr_mux = (pfat_table->antsel_rx_keep_2 << 2) |
284 					(pfat_table->antsel_rx_keep_1 << 1) |
285 					pfat_table->antsel_rx_keep_0;
286 			rtl88e_dm_ant_sel_statistics(hw, antsel_tr_mux, 0,
287 						     pstatus->rx_pwdb_all);
288 		}
289 
290 	}
291 }
292 
293 static void _rtl88ee_translate_rx_signal_stuff(struct ieee80211_hw *hw,
294 					       struct sk_buff *skb,
295 					       struct rtl_stats *pstatus,
296 					       u8 *pdesc,
297 					       struct rx_fwinfo_88e *p_drvinfo)
298 {
299 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
300 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
301 	struct ieee80211_hdr *hdr;
302 	u8 *tmp_buf;
303 	u8 *praddr;
304 	u8 *psaddr;
305 	__le16 fc;
306 	bool packet_matchbssid, packet_toself, packet_beacon;
307 
308 	tmp_buf = skb->data + pstatus->rx_drvinfo_size + pstatus->rx_bufshift;
309 
310 	hdr = (struct ieee80211_hdr *)tmp_buf;
311 	fc = hdr->frame_control;
312 	praddr = hdr->addr1;
313 	psaddr = ieee80211_get_SA(hdr);
314 	memcpy(pstatus->psaddr, psaddr, ETH_ALEN);
315 
316 	packet_matchbssid = ((!ieee80211_is_ctl(fc)) &&
317 	     (ether_addr_equal(mac->bssid, ieee80211_has_tods(fc) ?
318 			       hdr->addr1 : ieee80211_has_fromds(fc) ?
319 			       hdr->addr2 : hdr->addr3)) &&
320 			       (!pstatus->hwerror) &&
321 			       (!pstatus->crc) && (!pstatus->icv));
322 
323 	packet_toself = packet_matchbssid &&
324 	    (ether_addr_equal(praddr, rtlefuse->dev_addr));
325 
326 	if (ieee80211_is_beacon(hdr->frame_control))
327 		packet_beacon = true;
328 	else
329 		packet_beacon = false;
330 
331 	_rtl88ee_query_rxphystatus(hw, pstatus, pdesc, p_drvinfo,
332 				   packet_matchbssid, packet_toself,
333 				   packet_beacon);
334 	_rtl88ee_smart_antenna(hw, pstatus);
335 	rtl_process_phyinfo(hw, tmp_buf, pstatus);
336 }
337 
338 static void _rtl88ee_insert_emcontent(struct rtl_tcb_desc *ptcb_desc,
339 				      u8 *virtualaddress)
340 {
341 	u32 dwtmp = 0;
342 	memset(virtualaddress, 0, 8);
343 
344 	SET_EARLYMODE_PKTNUM(virtualaddress, ptcb_desc->empkt_num);
345 	if (ptcb_desc->empkt_num == 1) {
346 		dwtmp = ptcb_desc->empkt_len[0];
347 	} else {
348 		dwtmp = ptcb_desc->empkt_len[0];
349 		dwtmp += ((dwtmp%4) ? (4-dwtmp%4) : 0)+4;
350 		dwtmp += ptcb_desc->empkt_len[1];
351 	}
352 	SET_EARLYMODE_LEN0(virtualaddress, dwtmp);
353 
354 	if (ptcb_desc->empkt_num <= 3) {
355 		dwtmp = ptcb_desc->empkt_len[2];
356 	} else {
357 		dwtmp = ptcb_desc->empkt_len[2];
358 		dwtmp += ((dwtmp%4) ? (4-dwtmp%4) : 0)+4;
359 		dwtmp += ptcb_desc->empkt_len[3];
360 	}
361 	SET_EARLYMODE_LEN1(virtualaddress, dwtmp);
362 	if (ptcb_desc->empkt_num <= 5) {
363 		dwtmp = ptcb_desc->empkt_len[4];
364 	} else {
365 		dwtmp = ptcb_desc->empkt_len[4];
366 		dwtmp += ((dwtmp%4) ? (4-dwtmp%4) : 0)+4;
367 		dwtmp += ptcb_desc->empkt_len[5];
368 	}
369 	SET_EARLYMODE_LEN2_1(virtualaddress, dwtmp & 0xF);
370 	SET_EARLYMODE_LEN2_2(virtualaddress, dwtmp >> 4);
371 	if (ptcb_desc->empkt_num <= 7) {
372 		dwtmp = ptcb_desc->empkt_len[6];
373 	} else {
374 		dwtmp = ptcb_desc->empkt_len[6];
375 		dwtmp += ((dwtmp%4) ? (4-dwtmp%4) : 0)+4;
376 		dwtmp += ptcb_desc->empkt_len[7];
377 	}
378 	SET_EARLYMODE_LEN3(virtualaddress, dwtmp);
379 	if (ptcb_desc->empkt_num <= 9) {
380 		dwtmp = ptcb_desc->empkt_len[8];
381 	} else {
382 		dwtmp = ptcb_desc->empkt_len[8];
383 		dwtmp += ((dwtmp%4) ? (4-dwtmp%4) : 0)+4;
384 		dwtmp += ptcb_desc->empkt_len[9];
385 	}
386 	SET_EARLYMODE_LEN4(virtualaddress, dwtmp);
387 }
388 
389 bool rtl88ee_rx_query_desc(struct ieee80211_hw *hw,
390 			   struct rtl_stats *status,
391 			   struct ieee80211_rx_status *rx_status,
392 			   u8 *pdesc, struct sk_buff *skb)
393 {
394 	struct rtl_priv *rtlpriv = rtl_priv(hw);
395 	struct rx_fwinfo_88e *p_drvinfo;
396 	struct ieee80211_hdr *hdr;
397 
398 	u32 phystatus = GET_RX_DESC_PHYST(pdesc);
399 	status->packet_report_type = (u8)GET_RX_STATUS_DESC_RPT_SEL(pdesc);
400 	if (status->packet_report_type == TX_REPORT2)
401 		status->length = (u16)GET_RX_RPT2_DESC_PKT_LEN(pdesc);
402 	else
403 		status->length = (u16)GET_RX_DESC_PKT_LEN(pdesc);
404 	status->rx_drvinfo_size = (u8)GET_RX_DESC_DRV_INFO_SIZE(pdesc) *
405 	    RX_DRV_INFO_SIZE_UNIT;
406 	status->rx_bufshift = (u8)(GET_RX_DESC_SHIFT(pdesc) & 0x03);
407 	status->icv = (u16)GET_RX_DESC_ICV(pdesc);
408 	status->crc = (u16)GET_RX_DESC_CRC32(pdesc);
409 	status->hwerror = (status->crc | status->icv);
410 	status->decrypted = !GET_RX_DESC_SWDEC(pdesc);
411 	status->rate = (u8)GET_RX_DESC_RXMCS(pdesc);
412 	status->shortpreamble = (u16)GET_RX_DESC_SPLCP(pdesc);
413 	status->isampdu = (bool) (GET_RX_DESC_PAGGR(pdesc) == 1);
414 	status->isfirst_ampdu = (bool)((GET_RX_DESC_PAGGR(pdesc) == 1) &&
415 				(GET_RX_DESC_FAGGR(pdesc) == 1));
416 	if (status->packet_report_type == NORMAL_RX)
417 		status->timestamp_low = GET_RX_DESC_TSFL(pdesc);
418 	status->rx_is40Mhzpacket = (bool) GET_RX_DESC_BW(pdesc);
419 	status->is_ht = (bool)GET_RX_DESC_RXHT(pdesc);
420 
421 	status->is_cck = RTL8188_RX_HAL_IS_CCK_RATE(status->rate);
422 
423 	status->macid = GET_RX_DESC_MACID(pdesc);
424 	if (GET_RX_STATUS_DESC_MAGIC_MATCH(pdesc))
425 		status->wake_match = BIT(2);
426 	else if (GET_RX_STATUS_DESC_MAGIC_MATCH(pdesc))
427 		status->wake_match = BIT(1);
428 	else if (GET_RX_STATUS_DESC_UNICAST_MATCH(pdesc))
429 		status->wake_match = BIT(0);
430 	else
431 		status->wake_match = 0;
432 	if (status->wake_match)
433 		RT_TRACE(rtlpriv, COMP_RXDESC, DBG_LOUD,
434 		"GGGGGGGGGGGGGet Wakeup Packet!! WakeMatch=%d\n",
435 		status->wake_match);
436 	rx_status->freq = hw->conf.chandef.chan->center_freq;
437 	rx_status->band = hw->conf.chandef.chan->band;
438 
439 	hdr = (struct ieee80211_hdr *)(skb->data + status->rx_drvinfo_size
440 			+ status->rx_bufshift);
441 
442 	if (status->crc)
443 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
444 
445 	if (status->rx_is40Mhzpacket)
446 		rx_status->bw = RATE_INFO_BW_40;
447 
448 	if (status->is_ht)
449 		rx_status->encoding = RX_ENC_HT;
450 
451 	rx_status->flag |= RX_FLAG_MACTIME_START;
452 
453 	/* hw will set status->decrypted true, if it finds the
454 	 * frame is open data frame or mgmt frame.
455 	 * So hw will not decryption robust managment frame
456 	 * for IEEE80211w but still set status->decrypted
457 	 * true, so here we should set it back to undecrypted
458 	 * for IEEE80211w frame, and mac80211 sw will help
459 	 * to decrypt it
460 	 */
461 	if (status->decrypted) {
462 		if ((!_ieee80211_is_robust_mgmt_frame(hdr)) &&
463 		    (ieee80211_has_protected(hdr->frame_control)))
464 			rx_status->flag |= RX_FLAG_DECRYPTED;
465 		else
466 			rx_status->flag &= ~RX_FLAG_DECRYPTED;
467 	}
468 
469 	/* rate_idx: index of data rate into band's
470 	 * supported rates or MCS index if HT rates
471 	 * are use (RX_FLAG_HT)
472 	 * Notice: this is diff with windows define
473 	 */
474 	rx_status->rate_idx = rtlwifi_rate_mapping(hw, status->is_ht,
475 						   false, status->rate);
476 
477 	rx_status->mactime = status->timestamp_low;
478 	if (phystatus == true) {
479 		p_drvinfo = (struct rx_fwinfo_88e *)(skb->data +
480 						     status->rx_bufshift);
481 
482 		_rtl88ee_translate_rx_signal_stuff(hw,
483 						   skb, status, pdesc,
484 						   p_drvinfo);
485 	}
486 	rx_status->signal = status->recvsignalpower + 10;
487 	if (status->packet_report_type == TX_REPORT2) {
488 		status->macid_valid_entry[0] =
489 			 GET_RX_RPT2_DESC_MACID_VALID_1(pdesc);
490 		status->macid_valid_entry[1] =
491 			 GET_RX_RPT2_DESC_MACID_VALID_2(pdesc);
492 	}
493 	return true;
494 }
495 
496 void rtl88ee_tx_fill_desc(struct ieee80211_hw *hw,
497 			  struct ieee80211_hdr *hdr, u8 *pdesc_tx,
498 			  u8 *txbd, struct ieee80211_tx_info *info,
499 			  struct ieee80211_sta *sta,
500 			  struct sk_buff *skb,
501 			  u8 hw_queue, struct rtl_tcb_desc *ptcb_desc)
502 
503 {
504 	struct rtl_priv *rtlpriv = rtl_priv(hw);
505 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
506 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
507 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
508 	u8 *pdesc = (u8 *)pdesc_tx;
509 	u16 seq_number;
510 	__le16 fc = hdr->frame_control;
511 	unsigned int buf_len = 0;
512 	unsigned int skb_len = skb->len;
513 	u8 fw_qsel = _rtl88ee_map_hwqueue_to_fwqueue(skb, hw_queue);
514 	bool firstseg = ((hdr->seq_ctrl &
515 			    cpu_to_le16(IEEE80211_SCTL_FRAG)) == 0);
516 	bool lastseg = ((hdr->frame_control &
517 			   cpu_to_le16(IEEE80211_FCTL_MOREFRAGS)) == 0);
518 	dma_addr_t mapping;
519 	u8 bw_40 = 0;
520 	u8 short_gi = 0;
521 
522 	if (mac->opmode == NL80211_IFTYPE_STATION) {
523 		bw_40 = mac->bw_40;
524 	} else if (mac->opmode == NL80211_IFTYPE_AP ||
525 		mac->opmode == NL80211_IFTYPE_ADHOC) {
526 		if (sta)
527 			bw_40 = sta->ht_cap.cap &
528 				IEEE80211_HT_CAP_SUP_WIDTH_20_40;
529 	}
530 	seq_number = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4;
531 	rtl_get_tcb_desc(hw, info, sta, skb, ptcb_desc);
532 	/* reserve 8 byte for AMPDU early mode */
533 	if (rtlhal->earlymode_enable) {
534 		skb_push(skb, EM_HDR_LEN);
535 		memset(skb->data, 0, EM_HDR_LEN);
536 	}
537 	buf_len = skb->len;
538 	mapping = pci_map_single(rtlpci->pdev, skb->data, skb->len,
539 				 PCI_DMA_TODEVICE);
540 	if (pci_dma_mapping_error(rtlpci->pdev, mapping)) {
541 		RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
542 			 "DMA mapping error\n");
543 		return;
544 	}
545 	CLEAR_PCI_TX_DESC_CONTENT(pdesc, sizeof(struct tx_desc_88e));
546 	if (ieee80211_is_nullfunc(fc) || ieee80211_is_ctl(fc)) {
547 		firstseg = true;
548 		lastseg = true;
549 	}
550 	if (firstseg) {
551 		if (rtlhal->earlymode_enable) {
552 			SET_TX_DESC_PKT_OFFSET(pdesc, 1);
553 			SET_TX_DESC_OFFSET(pdesc, USB_HWDESC_HEADER_LEN +
554 					   EM_HDR_LEN);
555 			if (ptcb_desc->empkt_num) {
556 				RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
557 					 "Insert 8 byte.pTcb->EMPktNum:%d\n",
558 					  ptcb_desc->empkt_num);
559 				_rtl88ee_insert_emcontent(ptcb_desc,
560 							  (u8 *)(skb->data));
561 			}
562 		} else {
563 			SET_TX_DESC_OFFSET(pdesc, USB_HWDESC_HEADER_LEN);
564 		}
565 
566 		ptcb_desc->use_driver_rate = true;
567 		SET_TX_DESC_TX_RATE(pdesc, ptcb_desc->hw_rate);
568 		if (ptcb_desc->hw_rate > DESC92C_RATEMCS0)
569 			short_gi = (ptcb_desc->use_shortgi) ? 1 : 0;
570 		else
571 			short_gi = (ptcb_desc->use_shortpreamble) ? 1 : 0;
572 
573 		SET_TX_DESC_DATA_SHORTGI(pdesc, short_gi);
574 
575 		if (info->flags & IEEE80211_TX_CTL_AMPDU) {
576 			SET_TX_DESC_AGG_ENABLE(pdesc, 1);
577 			SET_TX_DESC_MAX_AGG_NUM(pdesc, 0x14);
578 		}
579 		SET_TX_DESC_SEQ(pdesc, seq_number);
580 		SET_TX_DESC_RTS_ENABLE(pdesc, ((ptcb_desc->rts_enable &&
581 						!ptcb_desc->cts_enable) ? 1 : 0));
582 		SET_TX_DESC_HW_RTS_ENABLE(pdesc, 0);
583 		SET_TX_DESC_CTS2SELF(pdesc, ((ptcb_desc->cts_enable) ? 1 : 0));
584 		SET_TX_DESC_RTS_STBC(pdesc, ((ptcb_desc->rts_stbc) ? 1 : 0));
585 
586 		SET_TX_DESC_RTS_RATE(pdesc, ptcb_desc->rts_rate);
587 		SET_TX_DESC_RTS_BW(pdesc, 0);
588 		SET_TX_DESC_RTS_SC(pdesc, ptcb_desc->rts_sc);
589 		SET_TX_DESC_RTS_SHORT(pdesc,
590 			((ptcb_desc->rts_rate <= DESC92C_RATE54M) ?
591 			(ptcb_desc->rts_use_shortpreamble ? 1 : 0) :
592 			(ptcb_desc->rts_use_shortgi ? 1 : 0)));
593 
594 		if (ptcb_desc->tx_enable_sw_calc_duration)
595 			SET_TX_DESC_NAV_USE_HDR(pdesc, 1);
596 
597 		if (bw_40) {
598 			if (ptcb_desc->packet_bw == HT_CHANNEL_WIDTH_20_40) {
599 				SET_TX_DESC_DATA_BW(pdesc, 1);
600 				SET_TX_DESC_TX_SUB_CARRIER(pdesc, 3);
601 			} else {
602 				SET_TX_DESC_DATA_BW(pdesc, 0);
603 				SET_TX_DESC_TX_SUB_CARRIER(pdesc,
604 							   mac->cur_40_prime_sc);
605 			}
606 		} else {
607 			SET_TX_DESC_DATA_BW(pdesc, 0);
608 			SET_TX_DESC_TX_SUB_CARRIER(pdesc, 0);
609 		}
610 
611 		SET_TX_DESC_LINIP(pdesc, 0);
612 		SET_TX_DESC_PKT_SIZE(pdesc, (u16)skb_len);
613 		if (sta) {
614 			u8 ampdu_density = sta->ht_cap.ampdu_density;
615 			SET_TX_DESC_AMPDU_DENSITY(pdesc, ampdu_density);
616 		}
617 		if (info->control.hw_key) {
618 			struct ieee80211_key_conf *keyconf;
619 
620 			keyconf = info->control.hw_key;
621 			switch (keyconf->cipher) {
622 			case WLAN_CIPHER_SUITE_WEP40:
623 			case WLAN_CIPHER_SUITE_WEP104:
624 			case WLAN_CIPHER_SUITE_TKIP:
625 				SET_TX_DESC_SEC_TYPE(pdesc, 0x1);
626 				break;
627 			case WLAN_CIPHER_SUITE_CCMP:
628 				SET_TX_DESC_SEC_TYPE(pdesc, 0x3);
629 				break;
630 			default:
631 				SET_TX_DESC_SEC_TYPE(pdesc, 0x0);
632 				break;
633 
634 			}
635 		}
636 
637 		SET_TX_DESC_QUEUE_SEL(pdesc, fw_qsel);
638 		SET_TX_DESC_DATA_RATE_FB_LIMIT(pdesc, 0x1F);
639 		SET_TX_DESC_RTS_RATE_FB_LIMIT(pdesc, 0xF);
640 		SET_TX_DESC_DISABLE_FB(pdesc, ptcb_desc->disable_ratefallback ?
641 				       1 : 0);
642 		SET_TX_DESC_USE_RATE(pdesc, ptcb_desc->use_driver_rate ? 1 : 0);
643 
644 		/*SET_TX_DESC_PWR_STATUS(pdesc, pwr_status);*/
645 		/* Set TxRate and RTSRate in TxDesc  */
646 		/* This prevent Tx initial rate of new-coming packets */
647 		/* from being overwritten by retried  packet rate.*/
648 		if (!ptcb_desc->use_driver_rate) {
649 			/*SET_TX_DESC_RTS_RATE(pdesc, 0x08); */
650 			/* SET_TX_DESC_TX_RATE(pdesc, 0x0b); */
651 		}
652 		if (ieee80211_is_data_qos(fc)) {
653 			if (mac->rdg_en) {
654 				RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
655 					"Enable RDG function.\n");
656 				SET_TX_DESC_RDG_ENABLE(pdesc, 1);
657 				SET_TX_DESC_HTC(pdesc, 1);
658 			}
659 		}
660 	}
661 
662 	SET_TX_DESC_FIRST_SEG(pdesc, (firstseg ? 1 : 0));
663 	SET_TX_DESC_LAST_SEG(pdesc, (lastseg ? 1 : 0));
664 	SET_TX_DESC_TX_BUFFER_SIZE(pdesc, (u16)buf_len);
665 	SET_TX_DESC_TX_BUFFER_ADDRESS(pdesc, mapping);
666 	if (rtlpriv->dm.useramask) {
667 		SET_TX_DESC_RATE_ID(pdesc, ptcb_desc->ratr_index);
668 		SET_TX_DESC_MACID(pdesc, ptcb_desc->mac_id);
669 	} else {
670 		SET_TX_DESC_RATE_ID(pdesc, 0xC + ptcb_desc->ratr_index);
671 		SET_TX_DESC_MACID(pdesc, ptcb_desc->ratr_index);
672 	}
673 	if (ieee80211_is_data_qos(fc))
674 		SET_TX_DESC_QOS(pdesc, 1);
675 
676 	if (!ieee80211_is_data_qos(fc))
677 		SET_TX_DESC_HWSEQ_EN(pdesc, 1);
678 	SET_TX_DESC_MORE_FRAG(pdesc, (lastseg ? 0 : 1));
679 	if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
680 	    is_broadcast_ether_addr(ieee80211_get_DA(hdr))) {
681 		SET_TX_DESC_BMC(pdesc, 1);
682 	}
683 
684 	rtl88e_dm_set_tx_ant_by_tx_info(hw, pdesc, ptcb_desc->mac_id);
685 	RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE, "\n");
686 }
687 
688 void rtl88ee_tx_fill_cmddesc(struct ieee80211_hw *hw,
689 			     u8 *pdesc, bool firstseg,
690 			     bool lastseg, struct sk_buff *skb)
691 {
692 	struct rtl_priv *rtlpriv = rtl_priv(hw);
693 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
694 	u8 fw_queue = QSLT_BEACON;
695 
696 	dma_addr_t mapping = pci_map_single(rtlpci->pdev,
697 					    skb->data, skb->len,
698 					    PCI_DMA_TODEVICE);
699 
700 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
701 	__le16 fc = hdr->frame_control;
702 
703 	if (pci_dma_mapping_error(rtlpci->pdev, mapping)) {
704 		RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
705 			 "DMA mapping error\n");
706 		return;
707 	}
708 	CLEAR_PCI_TX_DESC_CONTENT(pdesc, TX_DESC_SIZE);
709 
710 	if (firstseg)
711 		SET_TX_DESC_OFFSET(pdesc, USB_HWDESC_HEADER_LEN);
712 
713 	SET_TX_DESC_TX_RATE(pdesc, DESC92C_RATE1M);
714 
715 	SET_TX_DESC_SEQ(pdesc, 0);
716 
717 	SET_TX_DESC_LINIP(pdesc, 0);
718 
719 	SET_TX_DESC_QUEUE_SEL(pdesc, fw_queue);
720 
721 	SET_TX_DESC_FIRST_SEG(pdesc, 1);
722 	SET_TX_DESC_LAST_SEG(pdesc, 1);
723 
724 	SET_TX_DESC_TX_BUFFER_SIZE(pdesc, (u16)(skb->len));
725 
726 	SET_TX_DESC_TX_BUFFER_ADDRESS(pdesc, mapping);
727 
728 	SET_TX_DESC_RATE_ID(pdesc, 7);
729 	SET_TX_DESC_MACID(pdesc, 0);
730 
731 	SET_TX_DESC_OWN(pdesc, 1);
732 
733 	SET_TX_DESC_PKT_SIZE(pdesc, (u16)(skb->len));
734 
735 	SET_TX_DESC_FIRST_SEG(pdesc, 1);
736 	SET_TX_DESC_LAST_SEG(pdesc, 1);
737 
738 	SET_TX_DESC_OFFSET(pdesc, 0x20);
739 
740 	SET_TX_DESC_USE_RATE(pdesc, 1);
741 
742 	if (!ieee80211_is_data_qos(fc))
743 		SET_TX_DESC_HWSEQ_EN(pdesc, 1);
744 
745 	RT_PRINT_DATA(rtlpriv, COMP_CMD, DBG_LOUD,
746 		      "H2C Tx Cmd Content\n",
747 		      pdesc, TX_DESC_SIZE);
748 }
749 
750 void rtl88ee_set_desc(struct ieee80211_hw *hw, u8 *pdesc,
751 		      bool istx, u8 desc_name, u8 *val)
752 {
753 	if (istx == true) {
754 		switch (desc_name) {
755 		case HW_DESC_OWN:
756 			SET_TX_DESC_OWN(pdesc, 1);
757 			break;
758 		case HW_DESC_TX_NEXTDESC_ADDR:
759 			SET_TX_DESC_NEXT_DESC_ADDRESS(pdesc, *(u32 *)val);
760 			break;
761 		default:
762 			WARN_ONCE(true, "rtl8188ee: ERR txdesc :%d not processed\n",
763 				  desc_name);
764 			break;
765 		}
766 	} else {
767 		switch (desc_name) {
768 		case HW_DESC_RXOWN:
769 			SET_RX_DESC_OWN(pdesc, 1);
770 			break;
771 		case HW_DESC_RXBUFF_ADDR:
772 			SET_RX_DESC_BUFF_ADDR(pdesc, *(u32 *)val);
773 			break;
774 		case HW_DESC_RXPKT_LEN:
775 			SET_RX_DESC_PKT_LEN(pdesc, *(u32 *)val);
776 			break;
777 		case HW_DESC_RXERO:
778 			SET_RX_DESC_EOR(pdesc, 1);
779 			break;
780 		default:
781 			WARN_ONCE(true, "rtl8188ee: ERR rxdesc :%d not processed\n",
782 				  desc_name);
783 			break;
784 		}
785 	}
786 }
787 
788 u64 rtl88ee_get_desc(struct ieee80211_hw *hw,
789 		     u8 *pdesc, bool istx, u8 desc_name)
790 {
791 	u32 ret = 0;
792 
793 	if (istx == true) {
794 		switch (desc_name) {
795 		case HW_DESC_OWN:
796 			ret = GET_TX_DESC_OWN(pdesc);
797 			break;
798 		case HW_DESC_TXBUFF_ADDR:
799 			ret = GET_TX_DESC_TX_BUFFER_ADDRESS(pdesc);
800 			break;
801 		default:
802 			WARN_ONCE(true, "rtl8188ee: ERR txdesc :%d not processed\n",
803 				  desc_name);
804 			break;
805 		}
806 	} else {
807 		switch (desc_name) {
808 		case HW_DESC_OWN:
809 			ret = GET_RX_DESC_OWN(pdesc);
810 			break;
811 		case HW_DESC_RXPKT_LEN:
812 			ret = GET_RX_DESC_PKT_LEN(pdesc);
813 			break;
814 		case HW_DESC_RXBUFF_ADDR:
815 			ret = GET_RX_DESC_BUFF_ADDR(pdesc);
816 			break;
817 		default:
818 			WARN_ONCE(true, "rtl8188ee: ERR rxdesc :%d not processed\n",
819 				  desc_name);
820 			break;
821 		}
822 	}
823 	return ret;
824 }
825 
826 bool rtl88ee_is_tx_desc_closed(struct ieee80211_hw *hw, u8 hw_queue, u16 index)
827 {
828 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
829 	struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[hw_queue];
830 	u8 *entry = (u8 *)(&ring->desc[ring->idx]);
831 	u8 own = (u8)rtl88ee_get_desc(hw, entry, true, HW_DESC_OWN);
832 
833 	/*beacon packet will only use the first
834 	 *descriptor defautly,and the own may not
835 	 *be cleared by the hardware
836 	 */
837 	if (own)
838 		return false;
839 	return true;
840 }
841 
842 void rtl88ee_tx_polling(struct ieee80211_hw *hw, u8 hw_queue)
843 {
844 	struct rtl_priv *rtlpriv = rtl_priv(hw);
845 	if (hw_queue == BEACON_QUEUE) {
846 		rtl_write_word(rtlpriv, REG_PCIE_CTRL_REG, BIT(4));
847 	} else {
848 		rtl_write_word(rtlpriv, REG_PCIE_CTRL_REG,
849 			       BIT(0) << (hw_queue));
850 	}
851 }
852