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