xref: /openbmc/linux/drivers/net/wireless/realtek/rtw88/tx.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1e3037485SYan-Hsuan Chuang // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2e3037485SYan-Hsuan Chuang /* Copyright(c) 2018-2019  Realtek Corporation
3e3037485SYan-Hsuan Chuang  */
4e3037485SYan-Hsuan Chuang 
5e3037485SYan-Hsuan Chuang #include "main.h"
6e3037485SYan-Hsuan Chuang #include "tx.h"
7e3037485SYan-Hsuan Chuang #include "fw.h"
8e3037485SYan-Hsuan Chuang #include "ps.h"
9da14a040SYan-Hsuan Chuang #include "debug.h"
10e3037485SYan-Hsuan Chuang 
11e3037485SYan-Hsuan Chuang static
rtw_tx_stats(struct rtw_dev * rtwdev,struct ieee80211_vif * vif,struct sk_buff * skb)12e3037485SYan-Hsuan Chuang void rtw_tx_stats(struct rtw_dev *rtwdev, struct ieee80211_vif *vif,
13e3037485SYan-Hsuan Chuang 		  struct sk_buff *skb)
14e3037485SYan-Hsuan Chuang {
15e3037485SYan-Hsuan Chuang 	struct ieee80211_hdr *hdr;
16e3037485SYan-Hsuan Chuang 	struct rtw_vif *rtwvif;
17e3037485SYan-Hsuan Chuang 
18e3037485SYan-Hsuan Chuang 	hdr = (struct ieee80211_hdr *)skb->data;
19e3037485SYan-Hsuan Chuang 
20e3037485SYan-Hsuan Chuang 	if (!ieee80211_is_data(hdr->frame_control))
21e3037485SYan-Hsuan Chuang 		return;
22e3037485SYan-Hsuan Chuang 
23e3037485SYan-Hsuan Chuang 	if (!is_broadcast_ether_addr(hdr->addr1) &&
24e3037485SYan-Hsuan Chuang 	    !is_multicast_ether_addr(hdr->addr1)) {
25e3037485SYan-Hsuan Chuang 		rtwdev->stats.tx_unicast += skb->len;
26e3037485SYan-Hsuan Chuang 		rtwdev->stats.tx_cnt++;
27e3037485SYan-Hsuan Chuang 		if (vif) {
28e3037485SYan-Hsuan Chuang 			rtwvif = (struct rtw_vif *)vif->drv_priv;
29e3037485SYan-Hsuan Chuang 			rtwvif->stats.tx_unicast += skb->len;
30e3037485SYan-Hsuan Chuang 			rtwvif->stats.tx_cnt++;
31e3037485SYan-Hsuan Chuang 		}
32e3037485SYan-Hsuan Chuang 	}
33e3037485SYan-Hsuan Chuang }
34e3037485SYan-Hsuan Chuang 
rtw_tx_fill_tx_desc(struct rtw_tx_pkt_info * pkt_info,struct sk_buff * skb)35e3037485SYan-Hsuan Chuang void rtw_tx_fill_tx_desc(struct rtw_tx_pkt_info *pkt_info, struct sk_buff *skb)
36e3037485SYan-Hsuan Chuang {
3788b9d8e6SPo-Hao Huang 	struct rtw_tx_desc *tx_desc = (struct rtw_tx_desc *)skb->data;
38*076f786aSPo-Hao Huang 	bool more_data = false;
39*076f786aSPo-Hao Huang 
40*076f786aSPo-Hao Huang 	if (pkt_info->qsel == TX_DESC_QSEL_HIGH)
41*076f786aSPo-Hao Huang 		more_data = true;
42e3037485SYan-Hsuan Chuang 
4388b9d8e6SPo-Hao Huang 	tx_desc->w0 = le32_encode_bits(pkt_info->tx_pkt_size, RTW_TX_DESC_W0_TXPKTSIZE) |
4488b9d8e6SPo-Hao Huang 		      le32_encode_bits(pkt_info->offset, RTW_TX_DESC_W0_OFFSET) |
4588b9d8e6SPo-Hao Huang 		      le32_encode_bits(pkt_info->bmc, RTW_TX_DESC_W0_BMC) |
4688b9d8e6SPo-Hao Huang 		      le32_encode_bits(pkt_info->ls, RTW_TX_DESC_W0_LS) |
4788b9d8e6SPo-Hao Huang 		      le32_encode_bits(pkt_info->dis_qselseq, RTW_TX_DESC_W0_DISQSELSEQ);
4888b9d8e6SPo-Hao Huang 
4988b9d8e6SPo-Hao Huang 	tx_desc->w1 = le32_encode_bits(pkt_info->qsel, RTW_TX_DESC_W1_QSEL) |
5088b9d8e6SPo-Hao Huang 		      le32_encode_bits(pkt_info->rate_id, RTW_TX_DESC_W1_RATE_ID) |
5188b9d8e6SPo-Hao Huang 		      le32_encode_bits(pkt_info->sec_type, RTW_TX_DESC_W1_SEC_TYPE) |
52*076f786aSPo-Hao Huang 		      le32_encode_bits(pkt_info->pkt_offset, RTW_TX_DESC_W1_PKT_OFFSET) |
53*076f786aSPo-Hao Huang 		      le32_encode_bits(more_data, RTW_TX_DESC_W1_MORE_DATA);
5488b9d8e6SPo-Hao Huang 
5588b9d8e6SPo-Hao Huang 	tx_desc->w2 = le32_encode_bits(pkt_info->ampdu_en, RTW_TX_DESC_W2_AGG_EN) |
5688b9d8e6SPo-Hao Huang 		      le32_encode_bits(pkt_info->report, RTW_TX_DESC_W2_SPE_RPT) |
5788b9d8e6SPo-Hao Huang 		      le32_encode_bits(pkt_info->ampdu_density, RTW_TX_DESC_W2_AMPDU_DEN) |
5888b9d8e6SPo-Hao Huang 		      le32_encode_bits(pkt_info->bt_null, RTW_TX_DESC_W2_BT_NULL);
5988b9d8e6SPo-Hao Huang 
6088b9d8e6SPo-Hao Huang 	tx_desc->w3 = le32_encode_bits(pkt_info->hw_ssn_sel, RTW_TX_DESC_W3_HW_SSN_SEL) |
6188b9d8e6SPo-Hao Huang 		      le32_encode_bits(pkt_info->use_rate, RTW_TX_DESC_W3_USE_RATE) |
6288b9d8e6SPo-Hao Huang 		      le32_encode_bits(pkt_info->dis_rate_fallback, RTW_TX_DESC_W3_DISDATAFB) |
6388b9d8e6SPo-Hao Huang 		      le32_encode_bits(pkt_info->rts, RTW_TX_DESC_W3_USE_RTS) |
6488b9d8e6SPo-Hao Huang 		      le32_encode_bits(pkt_info->nav_use_hdr, RTW_TX_DESC_W3_NAVUSEHDR) |
6588b9d8e6SPo-Hao Huang 		      le32_encode_bits(pkt_info->ampdu_factor, RTW_TX_DESC_W3_MAX_AGG_NUM);
6688b9d8e6SPo-Hao Huang 
6788b9d8e6SPo-Hao Huang 	tx_desc->w4 = le32_encode_bits(pkt_info->rate, RTW_TX_DESC_W4_DATARATE);
6888b9d8e6SPo-Hao Huang 
6988b9d8e6SPo-Hao Huang 	tx_desc->w5 = le32_encode_bits(pkt_info->short_gi, RTW_TX_DESC_W5_DATA_SHORT) |
7088b9d8e6SPo-Hao Huang 		      le32_encode_bits(pkt_info->bw, RTW_TX_DESC_W5_DATA_BW) |
7188b9d8e6SPo-Hao Huang 		      le32_encode_bits(pkt_info->ldpc, RTW_TX_DESC_W5_DATA_LDPC) |
7288b9d8e6SPo-Hao Huang 		      le32_encode_bits(pkt_info->stbc, RTW_TX_DESC_W5_DATA_STBC);
7388b9d8e6SPo-Hao Huang 
7488b9d8e6SPo-Hao Huang 	tx_desc->w6 = le32_encode_bits(pkt_info->sn, RTW_TX_DESC_W6_SW_DEFINE);
7588b9d8e6SPo-Hao Huang 
7688b9d8e6SPo-Hao Huang 	tx_desc->w8 = le32_encode_bits(pkt_info->en_hwseq, RTW_TX_DESC_W8_EN_HWSEQ);
7788b9d8e6SPo-Hao Huang 
7888b9d8e6SPo-Hao Huang 	tx_desc->w9 = le32_encode_bits(pkt_info->seq, RTW_TX_DESC_W9_SW_SEQ);
7988b9d8e6SPo-Hao Huang 
80d77ddc34SPo-Hao Huang 	if (pkt_info->rts) {
8188b9d8e6SPo-Hao Huang 		tx_desc->w4 |= le32_encode_bits(DESC_RATE24M, RTW_TX_DESC_W4_RTSRATE);
8288b9d8e6SPo-Hao Huang 		tx_desc->w5 |= le32_encode_bits(1, RTW_TX_DESC_W5_DATA_RTS_SHORT);
83d77ddc34SPo-Hao Huang 	}
8488b9d8e6SPo-Hao Huang 
8588b9d8e6SPo-Hao Huang 	if (pkt_info->tim_offset)
8688b9d8e6SPo-Hao Huang 		tx_desc->w9 |= le32_encode_bits(1, RTW_TX_DESC_W9_TIM_EN) |
8788b9d8e6SPo-Hao Huang 			       le32_encode_bits(pkt_info->tim_offset, RTW_TX_DESC_W9_TIM_OFFSET);
88e3037485SYan-Hsuan Chuang }
89e3037485SYan-Hsuan Chuang EXPORT_SYMBOL(rtw_tx_fill_tx_desc);
90e3037485SYan-Hsuan Chuang 
get_tx_ampdu_factor(struct ieee80211_sta * sta)91e3037485SYan-Hsuan Chuang static u8 get_tx_ampdu_factor(struct ieee80211_sta *sta)
92e3037485SYan-Hsuan Chuang {
93046d2e7cSSriram R 	u8 exp = sta->deflink.ht_cap.ampdu_factor;
94e3037485SYan-Hsuan Chuang 
95e3037485SYan-Hsuan Chuang 	/* the least ampdu factor is 8K, and the value in the tx desc is the
96e3037485SYan-Hsuan Chuang 	 * max aggregation num, which represents val * 2 packets can be
97e3037485SYan-Hsuan Chuang 	 * aggregated in an AMPDU, so here we should use 8/2=4 as the base
98e3037485SYan-Hsuan Chuang 	 */
99e3037485SYan-Hsuan Chuang 	return (BIT(2) << exp) - 1;
100e3037485SYan-Hsuan Chuang }
101e3037485SYan-Hsuan Chuang 
get_tx_ampdu_density(struct ieee80211_sta * sta)102e3037485SYan-Hsuan Chuang static u8 get_tx_ampdu_density(struct ieee80211_sta *sta)
103e3037485SYan-Hsuan Chuang {
104046d2e7cSSriram R 	return sta->deflink.ht_cap.ampdu_density;
105e3037485SYan-Hsuan Chuang }
106e3037485SYan-Hsuan Chuang 
get_highest_ht_tx_rate(struct rtw_dev * rtwdev,struct ieee80211_sta * sta)107e3037485SYan-Hsuan Chuang static u8 get_highest_ht_tx_rate(struct rtw_dev *rtwdev,
108e3037485SYan-Hsuan Chuang 				 struct ieee80211_sta *sta)
109e3037485SYan-Hsuan Chuang {
110e3037485SYan-Hsuan Chuang 	u8 rate;
111e3037485SYan-Hsuan Chuang 
112046d2e7cSSriram R 	if (rtwdev->hal.rf_type == RF_2T2R && sta->deflink.ht_cap.mcs.rx_mask[1] != 0)
113e3037485SYan-Hsuan Chuang 		rate = DESC_RATEMCS15;
114e3037485SYan-Hsuan Chuang 	else
115e3037485SYan-Hsuan Chuang 		rate = DESC_RATEMCS7;
116e3037485SYan-Hsuan Chuang 
117e3037485SYan-Hsuan Chuang 	return rate;
118e3037485SYan-Hsuan Chuang }
119e3037485SYan-Hsuan Chuang 
get_highest_vht_tx_rate(struct rtw_dev * rtwdev,struct ieee80211_sta * sta)120e3037485SYan-Hsuan Chuang static u8 get_highest_vht_tx_rate(struct rtw_dev *rtwdev,
121e3037485SYan-Hsuan Chuang 				  struct ieee80211_sta *sta)
122e3037485SYan-Hsuan Chuang {
123e3037485SYan-Hsuan Chuang 	struct rtw_efuse *efuse = &rtwdev->efuse;
124e3037485SYan-Hsuan Chuang 	u8 rate;
125e3037485SYan-Hsuan Chuang 	u16 tx_mcs_map;
126e3037485SYan-Hsuan Chuang 
127046d2e7cSSriram R 	tx_mcs_map = le16_to_cpu(sta->deflink.vht_cap.vht_mcs.tx_mcs_map);
128e3037485SYan-Hsuan Chuang 	if (efuse->hw_cap.nss == 1) {
129e3037485SYan-Hsuan Chuang 		switch (tx_mcs_map & 0x3) {
130e3037485SYan-Hsuan Chuang 		case IEEE80211_VHT_MCS_SUPPORT_0_7:
131e3037485SYan-Hsuan Chuang 			rate = DESC_RATEVHT1SS_MCS7;
132e3037485SYan-Hsuan Chuang 			break;
133e3037485SYan-Hsuan Chuang 		case IEEE80211_VHT_MCS_SUPPORT_0_8:
134e3037485SYan-Hsuan Chuang 			rate = DESC_RATEVHT1SS_MCS8;
135e3037485SYan-Hsuan Chuang 			break;
136e3037485SYan-Hsuan Chuang 		default:
137e3037485SYan-Hsuan Chuang 		case IEEE80211_VHT_MCS_SUPPORT_0_9:
138e3037485SYan-Hsuan Chuang 			rate = DESC_RATEVHT1SS_MCS9;
139e3037485SYan-Hsuan Chuang 			break;
140e3037485SYan-Hsuan Chuang 		}
141e3037485SYan-Hsuan Chuang 	} else if (efuse->hw_cap.nss >= 2) {
142e3037485SYan-Hsuan Chuang 		switch ((tx_mcs_map & 0xc) >> 2) {
143e3037485SYan-Hsuan Chuang 		case IEEE80211_VHT_MCS_SUPPORT_0_7:
144e3037485SYan-Hsuan Chuang 			rate = DESC_RATEVHT2SS_MCS7;
145e3037485SYan-Hsuan Chuang 			break;
146e3037485SYan-Hsuan Chuang 		case IEEE80211_VHT_MCS_SUPPORT_0_8:
147e3037485SYan-Hsuan Chuang 			rate = DESC_RATEVHT2SS_MCS8;
148e3037485SYan-Hsuan Chuang 			break;
149e3037485SYan-Hsuan Chuang 		default:
150e3037485SYan-Hsuan Chuang 		case IEEE80211_VHT_MCS_SUPPORT_0_9:
151e3037485SYan-Hsuan Chuang 			rate = DESC_RATEVHT2SS_MCS9;
152e3037485SYan-Hsuan Chuang 			break;
153e3037485SYan-Hsuan Chuang 		}
154e3037485SYan-Hsuan Chuang 	} else {
155e3037485SYan-Hsuan Chuang 		rate = DESC_RATEVHT1SS_MCS9;
156e3037485SYan-Hsuan Chuang 	}
157e3037485SYan-Hsuan Chuang 
158e3037485SYan-Hsuan Chuang 	return rate;
159e3037485SYan-Hsuan Chuang }
160e3037485SYan-Hsuan Chuang 
rtw_tx_report_enable(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info)161e3037485SYan-Hsuan Chuang static void rtw_tx_report_enable(struct rtw_dev *rtwdev,
162e3037485SYan-Hsuan Chuang 				 struct rtw_tx_pkt_info *pkt_info)
163e3037485SYan-Hsuan Chuang {
164e3037485SYan-Hsuan Chuang 	struct rtw_tx_report *tx_report = &rtwdev->tx_report;
165e3037485SYan-Hsuan Chuang 
166e3037485SYan-Hsuan Chuang 	/* [11:8], reserved, fills with zero
167e3037485SYan-Hsuan Chuang 	 * [7:2],  tx report sequence number
168e3037485SYan-Hsuan Chuang 	 * [1:0],  firmware use, fills with zero
169e3037485SYan-Hsuan Chuang 	 */
170e3037485SYan-Hsuan Chuang 	pkt_info->sn = (atomic_inc_return(&tx_report->sn) << 2) & 0xfc;
171e3037485SYan-Hsuan Chuang 	pkt_info->report = true;
172e3037485SYan-Hsuan Chuang }
173e3037485SYan-Hsuan Chuang 
rtw_tx_report_purge_timer(struct timer_list * t)174e3037485SYan-Hsuan Chuang void rtw_tx_report_purge_timer(struct timer_list *t)
175e3037485SYan-Hsuan Chuang {
176e3037485SYan-Hsuan Chuang 	struct rtw_dev *rtwdev = from_timer(rtwdev, t, tx_report.purge_timer);
177e3037485SYan-Hsuan Chuang 	struct rtw_tx_report *tx_report = &rtwdev->tx_report;
178e3037485SYan-Hsuan Chuang 	unsigned long flags;
179e3037485SYan-Hsuan Chuang 
180e3037485SYan-Hsuan Chuang 	if (skb_queue_len(&tx_report->queue) == 0)
181e3037485SYan-Hsuan Chuang 		return;
182e3037485SYan-Hsuan Chuang 
183584dce17SChin-Yen Lee 	rtw_warn(rtwdev, "failed to get tx report from firmware\n");
184e3037485SYan-Hsuan Chuang 
185e3037485SYan-Hsuan Chuang 	spin_lock_irqsave(&tx_report->q_lock, flags);
186e3037485SYan-Hsuan Chuang 	skb_queue_purge(&tx_report->queue);
187e3037485SYan-Hsuan Chuang 	spin_unlock_irqrestore(&tx_report->q_lock, flags);
188e3037485SYan-Hsuan Chuang }
189e3037485SYan-Hsuan Chuang 
rtw_tx_report_enqueue(struct rtw_dev * rtwdev,struct sk_buff * skb,u8 sn)190e3037485SYan-Hsuan Chuang void rtw_tx_report_enqueue(struct rtw_dev *rtwdev, struct sk_buff *skb, u8 sn)
191e3037485SYan-Hsuan Chuang {
192e3037485SYan-Hsuan Chuang 	struct rtw_tx_report *tx_report = &rtwdev->tx_report;
193e3037485SYan-Hsuan Chuang 	unsigned long flags;
194e3037485SYan-Hsuan Chuang 	u8 *drv_data;
195e3037485SYan-Hsuan Chuang 
196e3037485SYan-Hsuan Chuang 	/* pass sn to tx report handler through driver data */
197e3037485SYan-Hsuan Chuang 	drv_data = (u8 *)IEEE80211_SKB_CB(skb)->status.status_driver_data;
198e3037485SYan-Hsuan Chuang 	*drv_data = sn;
199e3037485SYan-Hsuan Chuang 
200e3037485SYan-Hsuan Chuang 	spin_lock_irqsave(&tx_report->q_lock, flags);
201e3037485SYan-Hsuan Chuang 	__skb_queue_tail(&tx_report->queue, skb);
202e3037485SYan-Hsuan Chuang 	spin_unlock_irqrestore(&tx_report->q_lock, flags);
203e3037485SYan-Hsuan Chuang 
204e3037485SYan-Hsuan Chuang 	mod_timer(&tx_report->purge_timer, jiffies + RTW_TX_PROBE_TIMEOUT);
205e3037485SYan-Hsuan Chuang }
206e3037485SYan-Hsuan Chuang EXPORT_SYMBOL(rtw_tx_report_enqueue);
207e3037485SYan-Hsuan Chuang 
rtw_tx_report_tx_status(struct rtw_dev * rtwdev,struct sk_buff * skb,bool acked)208e3037485SYan-Hsuan Chuang static void rtw_tx_report_tx_status(struct rtw_dev *rtwdev,
209e3037485SYan-Hsuan Chuang 				    struct sk_buff *skb, bool acked)
210e3037485SYan-Hsuan Chuang {
211e3037485SYan-Hsuan Chuang 	struct ieee80211_tx_info *info;
212e3037485SYan-Hsuan Chuang 
213e3037485SYan-Hsuan Chuang 	info = IEEE80211_SKB_CB(skb);
214e3037485SYan-Hsuan Chuang 	ieee80211_tx_info_clear_status(info);
215e3037485SYan-Hsuan Chuang 	if (acked)
216e3037485SYan-Hsuan Chuang 		info->flags |= IEEE80211_TX_STAT_ACK;
217e3037485SYan-Hsuan Chuang 	else
218e3037485SYan-Hsuan Chuang 		info->flags &= ~IEEE80211_TX_STAT_ACK;
219e3037485SYan-Hsuan Chuang 
220e3037485SYan-Hsuan Chuang 	ieee80211_tx_status_irqsafe(rtwdev->hw, skb);
221e3037485SYan-Hsuan Chuang }
222e3037485SYan-Hsuan Chuang 
rtw_tx_report_handle(struct rtw_dev * rtwdev,struct sk_buff * skb,int src)223614b1f87SPing-Ke Shih void rtw_tx_report_handle(struct rtw_dev *rtwdev, struct sk_buff *skb, int src)
224e3037485SYan-Hsuan Chuang {
225e3037485SYan-Hsuan Chuang 	struct rtw_tx_report *tx_report = &rtwdev->tx_report;
226e3037485SYan-Hsuan Chuang 	struct rtw_c2h_cmd *c2h;
227e3037485SYan-Hsuan Chuang 	struct sk_buff *cur, *tmp;
228e3037485SYan-Hsuan Chuang 	unsigned long flags;
229e3037485SYan-Hsuan Chuang 	u8 sn, st;
230e3037485SYan-Hsuan Chuang 	u8 *n;
231e3037485SYan-Hsuan Chuang 
232e3037485SYan-Hsuan Chuang 	c2h = get_c2h_from_skb(skb);
233e3037485SYan-Hsuan Chuang 
234614b1f87SPing-Ke Shih 	if (src == C2H_CCX_TX_RPT) {
235614b1f87SPing-Ke Shih 		sn = GET_CCX_REPORT_SEQNUM_V0(c2h->payload);
236614b1f87SPing-Ke Shih 		st = GET_CCX_REPORT_STATUS_V0(c2h->payload);
237614b1f87SPing-Ke Shih 	} else {
238614b1f87SPing-Ke Shih 		sn = GET_CCX_REPORT_SEQNUM_V1(c2h->payload);
239614b1f87SPing-Ke Shih 		st = GET_CCX_REPORT_STATUS_V1(c2h->payload);
240614b1f87SPing-Ke Shih 	}
241e3037485SYan-Hsuan Chuang 
242e3037485SYan-Hsuan Chuang 	spin_lock_irqsave(&tx_report->q_lock, flags);
243e3037485SYan-Hsuan Chuang 	skb_queue_walk_safe(&tx_report->queue, cur, tmp) {
244e3037485SYan-Hsuan Chuang 		n = (u8 *)IEEE80211_SKB_CB(cur)->status.status_driver_data;
245e3037485SYan-Hsuan Chuang 		if (*n == sn) {
246e3037485SYan-Hsuan Chuang 			__skb_unlink(cur, &tx_report->queue);
247e3037485SYan-Hsuan Chuang 			rtw_tx_report_tx_status(rtwdev, cur, st == 0);
248e3037485SYan-Hsuan Chuang 			break;
249e3037485SYan-Hsuan Chuang 		}
250e3037485SYan-Hsuan Chuang 	}
251e3037485SYan-Hsuan Chuang 	spin_unlock_irqrestore(&tx_report->q_lock, flags);
252e3037485SYan-Hsuan Chuang }
253e3037485SYan-Hsuan Chuang 
rtw_get_mgmt_rate(struct rtw_dev * rtwdev,struct sk_buff * skb,u8 lowest_rate,bool ignore_rate)2542f1367b5SYu-Yen Ting static u8 rtw_get_mgmt_rate(struct rtw_dev *rtwdev, struct sk_buff *skb,
2552f1367b5SYu-Yen Ting 			    u8 lowest_rate, bool ignore_rate)
2562f1367b5SYu-Yen Ting {
2572f1367b5SYu-Yen Ting 	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
2582f1367b5SYu-Yen Ting 	struct ieee80211_vif *vif = tx_info->control.vif;
259272cda71SYu-Yen Ting 	bool force_lowest = test_bit(RTW_FLAG_FORCE_LOWEST_RATE, rtwdev->flags);
2602f1367b5SYu-Yen Ting 
261272cda71SYu-Yen Ting 	if (!vif || !vif->bss_conf.basic_rates || ignore_rate || force_lowest)
2622f1367b5SYu-Yen Ting 		return lowest_rate;
2632f1367b5SYu-Yen Ting 
2642f1367b5SYu-Yen Ting 	return __ffs(vif->bss_conf.basic_rates) + lowest_rate;
2652f1367b5SYu-Yen Ting }
2662f1367b5SYu-Yen Ting 
rtw_tx_pkt_info_update_rate(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct sk_buff * skb,bool ignore_rate)267d8e030c7STzu-En Huang static void rtw_tx_pkt_info_update_rate(struct rtw_dev *rtwdev,
268d8e030c7STzu-En Huang 					struct rtw_tx_pkt_info *pkt_info,
2692f1367b5SYu-Yen Ting 					struct sk_buff *skb,
2702f1367b5SYu-Yen Ting 					bool ignore_rate)
271d8e030c7STzu-En Huang {
272d8e030c7STzu-En Huang 	if (rtwdev->hal.current_band_type == RTW_BAND_2G) {
273d8e030c7STzu-En Huang 		pkt_info->rate_id = RTW_RATEID_B_20M;
2742f1367b5SYu-Yen Ting 		pkt_info->rate = rtw_get_mgmt_rate(rtwdev, skb, DESC_RATE1M,
2752f1367b5SYu-Yen Ting 						   ignore_rate);
276d8e030c7STzu-En Huang 	} else {
277d8e030c7STzu-En Huang 		pkt_info->rate_id = RTW_RATEID_G;
2782f1367b5SYu-Yen Ting 		pkt_info->rate = rtw_get_mgmt_rate(rtwdev, skb, DESC_RATE6M,
2792f1367b5SYu-Yen Ting 						   ignore_rate);
280d8e030c7STzu-En Huang 	}
2812f1367b5SYu-Yen Ting 
282d8e030c7STzu-En Huang 	pkt_info->use_rate = true;
283d8e030c7STzu-En Huang 	pkt_info->dis_rate_fallback = true;
284d8e030c7STzu-En Huang }
285d8e030c7STzu-En Huang 
rtw_tx_pkt_info_update_sec(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct sk_buff * skb)286d8e030c7STzu-En Huang static void rtw_tx_pkt_info_update_sec(struct rtw_dev *rtwdev,
287d8e030c7STzu-En Huang 				       struct rtw_tx_pkt_info *pkt_info,
288d8e030c7STzu-En Huang 				       struct sk_buff *skb)
289d8e030c7STzu-En Huang {
290d8e030c7STzu-En Huang 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
291d8e030c7STzu-En Huang 	u8 sec_type = 0;
292d8e030c7STzu-En Huang 
293d8e030c7STzu-En Huang 	if (info && info->control.hw_key) {
294d8e030c7STzu-En Huang 		struct ieee80211_key_conf *key = info->control.hw_key;
295d8e030c7STzu-En Huang 
296d8e030c7STzu-En Huang 		switch (key->cipher) {
297d8e030c7STzu-En Huang 		case WLAN_CIPHER_SUITE_WEP40:
298d8e030c7STzu-En Huang 		case WLAN_CIPHER_SUITE_WEP104:
299d8e030c7STzu-En Huang 		case WLAN_CIPHER_SUITE_TKIP:
300d8e030c7STzu-En Huang 			sec_type = 0x01;
301d8e030c7STzu-En Huang 			break;
302d8e030c7STzu-En Huang 		case WLAN_CIPHER_SUITE_CCMP:
303d8e030c7STzu-En Huang 			sec_type = 0x03;
304d8e030c7STzu-En Huang 			break;
305d8e030c7STzu-En Huang 		default:
306d8e030c7STzu-En Huang 			break;
307d8e030c7STzu-En Huang 		}
308d8e030c7STzu-En Huang 	}
309d8e030c7STzu-En Huang 
310d8e030c7STzu-En Huang 	pkt_info->sec_type = sec_type;
311d8e030c7STzu-En Huang }
312d8e030c7STzu-En Huang 
rtw_tx_mgmt_pkt_info_update(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct ieee80211_sta * sta,struct sk_buff * skb)313e3037485SYan-Hsuan Chuang static void rtw_tx_mgmt_pkt_info_update(struct rtw_dev *rtwdev,
314e3037485SYan-Hsuan Chuang 					struct rtw_tx_pkt_info *pkt_info,
315aaab5d0eSYan-Hsuan Chuang 					struct ieee80211_sta *sta,
316e3037485SYan-Hsuan Chuang 					struct sk_buff *skb)
317e3037485SYan-Hsuan Chuang {
3182f1367b5SYu-Yen Ting 	rtw_tx_pkt_info_update_rate(rtwdev, pkt_info, skb, false);
3192542469dSTzu-En Huang 	pkt_info->dis_qselseq = true;
3202542469dSTzu-En Huang 	pkt_info->en_hwseq = true;
3212542469dSTzu-En Huang 	pkt_info->hw_ssn_sel = 0;
322d8e030c7STzu-En Huang 	/* TODO: need to change hw port and hw ssn sel for multiple vifs */
323e3037485SYan-Hsuan Chuang }
324e3037485SYan-Hsuan Chuang 
rtw_tx_data_pkt_info_update(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct ieee80211_sta * sta,struct sk_buff * skb)325e3037485SYan-Hsuan Chuang static void rtw_tx_data_pkt_info_update(struct rtw_dev *rtwdev,
326e3037485SYan-Hsuan Chuang 					struct rtw_tx_pkt_info *pkt_info,
327aaab5d0eSYan-Hsuan Chuang 					struct ieee80211_sta *sta,
328e3037485SYan-Hsuan Chuang 					struct sk_buff *skb)
329e3037485SYan-Hsuan Chuang {
330e3037485SYan-Hsuan Chuang 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
331e3037485SYan-Hsuan Chuang 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
332d77ddc34SPo-Hao Huang 	struct ieee80211_hw *hw = rtwdev->hw;
3331379e620SYan-Hsuan Chuang 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
334e3037485SYan-Hsuan Chuang 	struct rtw_sta_info *si;
3351379e620SYan-Hsuan Chuang 	u8 fix_rate;
336e3037485SYan-Hsuan Chuang 	u16 seq;
337e3037485SYan-Hsuan Chuang 	u8 ampdu_factor = 0;
338e3037485SYan-Hsuan Chuang 	u8 ampdu_density = 0;
339e3037485SYan-Hsuan Chuang 	bool ampdu_en = false;
340e3037485SYan-Hsuan Chuang 	u8 rate = DESC_RATE6M;
341e3037485SYan-Hsuan Chuang 	u8 rate_id = 6;
342e3037485SYan-Hsuan Chuang 	u8 bw = RTW_CHANNEL_WIDTH_20;
343e3037485SYan-Hsuan Chuang 	bool stbc = false;
344e3037485SYan-Hsuan Chuang 	bool ldpc = false;
345e3037485SYan-Hsuan Chuang 
346e3037485SYan-Hsuan Chuang 	seq = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4;
347e3037485SYan-Hsuan Chuang 
348e3037485SYan-Hsuan Chuang 	/* for broadcast/multicast, use default values */
349e3037485SYan-Hsuan Chuang 	if (!sta)
350e3037485SYan-Hsuan Chuang 		goto out;
351e3037485SYan-Hsuan Chuang 
352e3037485SYan-Hsuan Chuang 	if (info->flags & IEEE80211_TX_CTL_AMPDU) {
353e3037485SYan-Hsuan Chuang 		ampdu_en = true;
354e3037485SYan-Hsuan Chuang 		ampdu_factor = get_tx_ampdu_factor(sta);
355e3037485SYan-Hsuan Chuang 		ampdu_density = get_tx_ampdu_density(sta);
356e3037485SYan-Hsuan Chuang 	}
357e3037485SYan-Hsuan Chuang 
358d77ddc34SPo-Hao Huang 	if (info->control.use_rts || skb->len > hw->wiphy->rts_threshold)
359942e2a5dSYan-Hsuan Chuang 		pkt_info->rts = true;
360942e2a5dSYan-Hsuan Chuang 
361046d2e7cSSriram R 	if (sta->deflink.vht_cap.vht_supported)
362e3037485SYan-Hsuan Chuang 		rate = get_highest_vht_tx_rate(rtwdev, sta);
363046d2e7cSSriram R 	else if (sta->deflink.ht_cap.ht_supported)
364e3037485SYan-Hsuan Chuang 		rate = get_highest_ht_tx_rate(rtwdev, sta);
365046d2e7cSSriram R 	else if (sta->deflink.supp_rates[0] <= 0xf)
366e3037485SYan-Hsuan Chuang 		rate = DESC_RATE11M;
367e3037485SYan-Hsuan Chuang 	else
368e3037485SYan-Hsuan Chuang 		rate = DESC_RATE54M;
369e3037485SYan-Hsuan Chuang 
370e3037485SYan-Hsuan Chuang 	si = (struct rtw_sta_info *)sta->drv_priv;
371e3037485SYan-Hsuan Chuang 
372e3037485SYan-Hsuan Chuang 	bw = si->bw_mode;
373e3037485SYan-Hsuan Chuang 	rate_id = si->rate_id;
37404e00ac9SChin-Yen Lee 	stbc = rtwdev->hal.txrx_1ss ? false : si->stbc_en;
375e3037485SYan-Hsuan Chuang 	ldpc = si->ldpc_en;
376e3037485SYan-Hsuan Chuang 
377e3037485SYan-Hsuan Chuang out:
378e3037485SYan-Hsuan Chuang 	pkt_info->seq = seq;
379e3037485SYan-Hsuan Chuang 	pkt_info->ampdu_factor = ampdu_factor;
380e3037485SYan-Hsuan Chuang 	pkt_info->ampdu_density = ampdu_density;
381e3037485SYan-Hsuan Chuang 	pkt_info->ampdu_en = ampdu_en;
382e3037485SYan-Hsuan Chuang 	pkt_info->rate = rate;
383e3037485SYan-Hsuan Chuang 	pkt_info->rate_id = rate_id;
384e3037485SYan-Hsuan Chuang 	pkt_info->bw = bw;
385e3037485SYan-Hsuan Chuang 	pkt_info->stbc = stbc;
386e3037485SYan-Hsuan Chuang 	pkt_info->ldpc = ldpc;
3871379e620SYan-Hsuan Chuang 
3881379e620SYan-Hsuan Chuang 	fix_rate = dm_info->fix_rate;
3891379e620SYan-Hsuan Chuang 	if (fix_rate < DESC_RATE_MAX) {
3901379e620SYan-Hsuan Chuang 		pkt_info->rate = fix_rate;
3911379e620SYan-Hsuan Chuang 		pkt_info->dis_rate_fallback = true;
3921379e620SYan-Hsuan Chuang 		pkt_info->use_rate = true;
3931379e620SYan-Hsuan Chuang 	}
394e3037485SYan-Hsuan Chuang }
395e3037485SYan-Hsuan Chuang 
rtw_tx_pkt_info_update(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct ieee80211_sta * sta,struct sk_buff * skb)396e3037485SYan-Hsuan Chuang void rtw_tx_pkt_info_update(struct rtw_dev *rtwdev,
397e3037485SYan-Hsuan Chuang 			    struct rtw_tx_pkt_info *pkt_info,
398aaab5d0eSYan-Hsuan Chuang 			    struct ieee80211_sta *sta,
399e3037485SYan-Hsuan Chuang 			    struct sk_buff *skb)
400e3037485SYan-Hsuan Chuang {
401dcbf179cSPing-Ke Shih 	const struct rtw_chip_info *chip = rtwdev->chip;
402e3037485SYan-Hsuan Chuang 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
403e3037485SYan-Hsuan Chuang 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
404e3037485SYan-Hsuan Chuang 	struct rtw_sta_info *si;
405e3037485SYan-Hsuan Chuang 	struct ieee80211_vif *vif = NULL;
406e3037485SYan-Hsuan Chuang 	__le16 fc = hdr->frame_control;
407e3037485SYan-Hsuan Chuang 	bool bmc;
408e3037485SYan-Hsuan Chuang 
409aaab5d0eSYan-Hsuan Chuang 	if (sta) {
410aaab5d0eSYan-Hsuan Chuang 		si = (struct rtw_sta_info *)sta->drv_priv;
411e3037485SYan-Hsuan Chuang 		vif = si->vif;
412e3037485SYan-Hsuan Chuang 	}
413e3037485SYan-Hsuan Chuang 
414e3037485SYan-Hsuan Chuang 	if (ieee80211_is_mgmt(fc) || ieee80211_is_nullfunc(fc))
415aaab5d0eSYan-Hsuan Chuang 		rtw_tx_mgmt_pkt_info_update(rtwdev, pkt_info, sta, skb);
416e3037485SYan-Hsuan Chuang 	else if (ieee80211_is_data(fc))
417aaab5d0eSYan-Hsuan Chuang 		rtw_tx_data_pkt_info_update(rtwdev, pkt_info, sta, skb);
418e3037485SYan-Hsuan Chuang 
419e3037485SYan-Hsuan Chuang 	bmc = is_broadcast_ether_addr(hdr->addr1) ||
420e3037485SYan-Hsuan Chuang 	      is_multicast_ether_addr(hdr->addr1);
421e3037485SYan-Hsuan Chuang 
422e3037485SYan-Hsuan Chuang 	if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
423e3037485SYan-Hsuan Chuang 		rtw_tx_report_enable(rtwdev, pkt_info);
424e3037485SYan-Hsuan Chuang 
425e3037485SYan-Hsuan Chuang 	pkt_info->bmc = bmc;
426d8e030c7STzu-En Huang 	rtw_tx_pkt_info_update_sec(rtwdev, pkt_info, skb);
427e3037485SYan-Hsuan Chuang 	pkt_info->tx_pkt_size = skb->len;
428e3037485SYan-Hsuan Chuang 	pkt_info->offset = chip->tx_pkt_desc_sz;
429e3037485SYan-Hsuan Chuang 	pkt_info->qsel = skb->priority;
430e3037485SYan-Hsuan Chuang 	pkt_info->ls = true;
431e3037485SYan-Hsuan Chuang 
432e3037485SYan-Hsuan Chuang 	/* maybe merge with tx status ? */
433e3037485SYan-Hsuan Chuang 	rtw_tx_stats(rtwdev, vif, skb);
434e3037485SYan-Hsuan Chuang }
435e3037485SYan-Hsuan Chuang 
rtw_tx_rsvd_page_pkt_info_update(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct sk_buff * skb,enum rtw_rsvd_packet_type type)436d8e030c7STzu-En Huang void rtw_tx_rsvd_page_pkt_info_update(struct rtw_dev *rtwdev,
437e3037485SYan-Hsuan Chuang 				      struct rtw_tx_pkt_info *pkt_info,
438d8e030c7STzu-En Huang 				      struct sk_buff *skb,
439d8e030c7STzu-En Huang 				      enum rtw_rsvd_packet_type type)
440e3037485SYan-Hsuan Chuang {
441dcbf179cSPing-Ke Shih 	const struct rtw_chip_info *chip = rtwdev->chip;
442e3037485SYan-Hsuan Chuang 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
443e3037485SYan-Hsuan Chuang 	bool bmc;
444e3037485SYan-Hsuan Chuang 
445d8e030c7STzu-En Huang 	/* A beacon or dummy reserved page packet indicates that it is the first
446d8e030c7STzu-En Huang 	 * reserved page, and the qsel of it will be set in each hci.
447d8e030c7STzu-En Huang 	 */
448d8e030c7STzu-En Huang 	if (type != RSVD_BEACON && type != RSVD_DUMMY)
449d8e030c7STzu-En Huang 		pkt_info->qsel = TX_DESC_QSEL_MGMT;
450d8e030c7STzu-En Huang 
4512f1367b5SYu-Yen Ting 	rtw_tx_pkt_info_update_rate(rtwdev, pkt_info, skb, true);
452d8e030c7STzu-En Huang 
453e3037485SYan-Hsuan Chuang 	bmc = is_broadcast_ether_addr(hdr->addr1) ||
454e3037485SYan-Hsuan Chuang 	      is_multicast_ether_addr(hdr->addr1);
455e3037485SYan-Hsuan Chuang 	pkt_info->bmc = bmc;
456e3037485SYan-Hsuan Chuang 	pkt_info->tx_pkt_size = skb->len;
457e3037485SYan-Hsuan Chuang 	pkt_info->offset = chip->tx_pkt_desc_sz;
458e3037485SYan-Hsuan Chuang 	pkt_info->ls = true;
459d8e030c7STzu-En Huang 	if (type == RSVD_PS_POLL) {
460d8e030c7STzu-En Huang 		pkt_info->nav_use_hdr = true;
461d8e030c7STzu-En Huang 	} else {
462d8e030c7STzu-En Huang 		pkt_info->dis_qselseq = true;
463d8e030c7STzu-En Huang 		pkt_info->en_hwseq = true;
464d8e030c7STzu-En Huang 		pkt_info->hw_ssn_sel = 0;
465d8e030c7STzu-En Huang 	}
466d8e030c7STzu-En Huang 	if (type == RSVD_QOS_NULL)
467d8e030c7STzu-En Huang 		pkt_info->bt_null = true;
468d8e030c7STzu-En Huang 
469f2217968SPo-Hao Huang 	if (type == RSVD_BEACON) {
470f2217968SPo-Hao Huang 		struct rtw_rsvd_page *rsvd_pkt;
471f2217968SPo-Hao Huang 		int hdr_len;
472f2217968SPo-Hao Huang 
473f2217968SPo-Hao Huang 		rsvd_pkt = list_first_entry_or_null(&rtwdev->rsvd_page_list,
474f2217968SPo-Hao Huang 						    struct rtw_rsvd_page,
475f2217968SPo-Hao Huang 						    build_list);
476f2217968SPo-Hao Huang 		if (rsvd_pkt && rsvd_pkt->tim_offset != 0) {
477f2217968SPo-Hao Huang 			hdr_len = sizeof(struct ieee80211_hdr_3addr);
478f2217968SPo-Hao Huang 			pkt_info->tim_offset = rsvd_pkt->tim_offset - hdr_len;
479f2217968SPo-Hao Huang 		}
480f2217968SPo-Hao Huang 	}
481f2217968SPo-Hao Huang 
482d8e030c7STzu-En Huang 	rtw_tx_pkt_info_update_sec(rtwdev, pkt_info, skb);
483d8e030c7STzu-En Huang 
484d8e030c7STzu-En Huang 	/* TODO: need to change hw port and hw ssn sel for multiple vifs */
485e3037485SYan-Hsuan Chuang }
4863745d3e5SYan-Hsuan Chuang 
487da14a040SYan-Hsuan Chuang struct sk_buff *
rtw_tx_write_data_rsvd_page_get(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,u8 * buf,u32 size)488da14a040SYan-Hsuan Chuang rtw_tx_write_data_rsvd_page_get(struct rtw_dev *rtwdev,
489da14a040SYan-Hsuan Chuang 				struct rtw_tx_pkt_info *pkt_info,
490da14a040SYan-Hsuan Chuang 				u8 *buf, u32 size)
491da14a040SYan-Hsuan Chuang {
492dcbf179cSPing-Ke Shih 	const struct rtw_chip_info *chip = rtwdev->chip;
493da14a040SYan-Hsuan Chuang 	struct sk_buff *skb;
494da14a040SYan-Hsuan Chuang 	u32 tx_pkt_desc_sz;
495da14a040SYan-Hsuan Chuang 	u32 length;
496da14a040SYan-Hsuan Chuang 
497da14a040SYan-Hsuan Chuang 	tx_pkt_desc_sz = chip->tx_pkt_desc_sz;
498da14a040SYan-Hsuan Chuang 	length = size + tx_pkt_desc_sz;
499da14a040SYan-Hsuan Chuang 	skb = dev_alloc_skb(length);
500da14a040SYan-Hsuan Chuang 	if (!skb) {
501da14a040SYan-Hsuan Chuang 		rtw_err(rtwdev, "failed to alloc write data rsvd page skb\n");
502da14a040SYan-Hsuan Chuang 		return NULL;
503da14a040SYan-Hsuan Chuang 	}
504da14a040SYan-Hsuan Chuang 
505da14a040SYan-Hsuan Chuang 	skb_reserve(skb, tx_pkt_desc_sz);
506da14a040SYan-Hsuan Chuang 	skb_put_data(skb, buf, size);
507d8e030c7STzu-En Huang 	rtw_tx_rsvd_page_pkt_info_update(rtwdev, pkt_info, skb, RSVD_BEACON);
508da14a040SYan-Hsuan Chuang 
509da14a040SYan-Hsuan Chuang 	return skb;
510da14a040SYan-Hsuan Chuang }
511da14a040SYan-Hsuan Chuang EXPORT_SYMBOL(rtw_tx_write_data_rsvd_page_get);
512da14a040SYan-Hsuan Chuang 
513da14a040SYan-Hsuan Chuang struct sk_buff *
rtw_tx_write_data_h2c_get(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,u8 * buf,u32 size)514da14a040SYan-Hsuan Chuang rtw_tx_write_data_h2c_get(struct rtw_dev *rtwdev,
515da14a040SYan-Hsuan Chuang 			  struct rtw_tx_pkt_info *pkt_info,
516da14a040SYan-Hsuan Chuang 			  u8 *buf, u32 size)
517da14a040SYan-Hsuan Chuang {
518dcbf179cSPing-Ke Shih 	const struct rtw_chip_info *chip = rtwdev->chip;
519da14a040SYan-Hsuan Chuang 	struct sk_buff *skb;
520da14a040SYan-Hsuan Chuang 	u32 tx_pkt_desc_sz;
521da14a040SYan-Hsuan Chuang 	u32 length;
522da14a040SYan-Hsuan Chuang 
523da14a040SYan-Hsuan Chuang 	tx_pkt_desc_sz = chip->tx_pkt_desc_sz;
524da14a040SYan-Hsuan Chuang 	length = size + tx_pkt_desc_sz;
525da14a040SYan-Hsuan Chuang 	skb = dev_alloc_skb(length);
526da14a040SYan-Hsuan Chuang 	if (!skb) {
527da14a040SYan-Hsuan Chuang 		rtw_err(rtwdev, "failed to alloc write data h2c skb\n");
528da14a040SYan-Hsuan Chuang 		return NULL;
529da14a040SYan-Hsuan Chuang 	}
530da14a040SYan-Hsuan Chuang 
531da14a040SYan-Hsuan Chuang 	skb_reserve(skb, tx_pkt_desc_sz);
532da14a040SYan-Hsuan Chuang 	skb_put_data(skb, buf, size);
533da14a040SYan-Hsuan Chuang 	pkt_info->tx_pkt_size = size;
534da14a040SYan-Hsuan Chuang 
535da14a040SYan-Hsuan Chuang 	return skb;
536da14a040SYan-Hsuan Chuang }
537da14a040SYan-Hsuan Chuang EXPORT_SYMBOL(rtw_tx_write_data_h2c_get);
538da14a040SYan-Hsuan Chuang 
rtw_tx(struct rtw_dev * rtwdev,struct ieee80211_tx_control * control,struct sk_buff * skb)5393745d3e5SYan-Hsuan Chuang void rtw_tx(struct rtw_dev *rtwdev,
5403745d3e5SYan-Hsuan Chuang 	    struct ieee80211_tx_control *control,
5413745d3e5SYan-Hsuan Chuang 	    struct sk_buff *skb)
5423745d3e5SYan-Hsuan Chuang {
5433745d3e5SYan-Hsuan Chuang 	struct rtw_tx_pkt_info pkt_info = {0};
544aaab5d0eSYan-Hsuan Chuang 	int ret;
5453745d3e5SYan-Hsuan Chuang 
546aaab5d0eSYan-Hsuan Chuang 	rtw_tx_pkt_info_update(rtwdev, &pkt_info, control->sta, skb);
547aaab5d0eSYan-Hsuan Chuang 	ret = rtw_hci_tx_write(rtwdev, &pkt_info, skb);
548aaab5d0eSYan-Hsuan Chuang 	if (ret) {
549aaab5d0eSYan-Hsuan Chuang 		rtw_err(rtwdev, "failed to write TX skb to HCI\n");
5503745d3e5SYan-Hsuan Chuang 		goto out;
551aaab5d0eSYan-Hsuan Chuang 	}
552aaab5d0eSYan-Hsuan Chuang 
553aaab5d0eSYan-Hsuan Chuang 	rtw_hci_tx_kick_off(rtwdev);
5543745d3e5SYan-Hsuan Chuang 
5553745d3e5SYan-Hsuan Chuang 	return;
5563745d3e5SYan-Hsuan Chuang 
5573745d3e5SYan-Hsuan Chuang out:
5583745d3e5SYan-Hsuan Chuang 	ieee80211_free_txskb(rtwdev->hw, skb);
5593745d3e5SYan-Hsuan Chuang }
5603745d3e5SYan-Hsuan Chuang 
rtw_txq_check_agg(struct rtw_dev * rtwdev,struct rtw_txq * rtwtxq,struct sk_buff * skb)56146ebb174SYan-Hsuan Chuang static void rtw_txq_check_agg(struct rtw_dev *rtwdev,
56246ebb174SYan-Hsuan Chuang 			      struct rtw_txq *rtwtxq,
56346ebb174SYan-Hsuan Chuang 			      struct sk_buff *skb)
56446ebb174SYan-Hsuan Chuang {
56546ebb174SYan-Hsuan Chuang 	struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq);
56646ebb174SYan-Hsuan Chuang 	struct ieee80211_tx_info *info;
56746ebb174SYan-Hsuan Chuang 	struct rtw_sta_info *si;
56846ebb174SYan-Hsuan Chuang 
56946ebb174SYan-Hsuan Chuang 	if (test_bit(RTW_TXQ_AMPDU, &rtwtxq->flags)) {
57046ebb174SYan-Hsuan Chuang 		info = IEEE80211_SKB_CB(skb);
57146ebb174SYan-Hsuan Chuang 		info->flags |= IEEE80211_TX_CTL_AMPDU;
57246ebb174SYan-Hsuan Chuang 		return;
57346ebb174SYan-Hsuan Chuang 	}
57446ebb174SYan-Hsuan Chuang 
57546ebb174SYan-Hsuan Chuang 	if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
57646ebb174SYan-Hsuan Chuang 		return;
57746ebb174SYan-Hsuan Chuang 
57846ebb174SYan-Hsuan Chuang 	if (test_bit(RTW_TXQ_BLOCK_BA, &rtwtxq->flags))
57946ebb174SYan-Hsuan Chuang 		return;
58046ebb174SYan-Hsuan Chuang 
58146ebb174SYan-Hsuan Chuang 	if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
58246ebb174SYan-Hsuan Chuang 		return;
58346ebb174SYan-Hsuan Chuang 
58446ebb174SYan-Hsuan Chuang 	if (!txq->sta)
58546ebb174SYan-Hsuan Chuang 		return;
58646ebb174SYan-Hsuan Chuang 
58746ebb174SYan-Hsuan Chuang 	si = (struct rtw_sta_info *)txq->sta->drv_priv;
58846ebb174SYan-Hsuan Chuang 	set_bit(txq->tid, si->tid_ba);
58946ebb174SYan-Hsuan Chuang 
59046ebb174SYan-Hsuan Chuang 	ieee80211_queue_work(rtwdev->hw, &rtwdev->ba_work);
59146ebb174SYan-Hsuan Chuang }
59246ebb174SYan-Hsuan Chuang 
rtw_txq_push_skb(struct rtw_dev * rtwdev,struct rtw_txq * rtwtxq,struct sk_buff * skb)593aaab5d0eSYan-Hsuan Chuang static int rtw_txq_push_skb(struct rtw_dev *rtwdev,
594aaab5d0eSYan-Hsuan Chuang 			    struct rtw_txq *rtwtxq,
595aaab5d0eSYan-Hsuan Chuang 			    struct sk_buff *skb)
596aaab5d0eSYan-Hsuan Chuang {
597aaab5d0eSYan-Hsuan Chuang 	struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq);
598aaab5d0eSYan-Hsuan Chuang 	struct rtw_tx_pkt_info pkt_info = {0};
599aaab5d0eSYan-Hsuan Chuang 	int ret;
600aaab5d0eSYan-Hsuan Chuang 
601aaab5d0eSYan-Hsuan Chuang 	rtw_txq_check_agg(rtwdev, rtwtxq, skb);
602aaab5d0eSYan-Hsuan Chuang 
603aaab5d0eSYan-Hsuan Chuang 	rtw_tx_pkt_info_update(rtwdev, &pkt_info, txq->sta, skb);
604aaab5d0eSYan-Hsuan Chuang 	ret = rtw_hci_tx_write(rtwdev, &pkt_info, skb);
605aaab5d0eSYan-Hsuan Chuang 	if (ret) {
606aaab5d0eSYan-Hsuan Chuang 		rtw_err(rtwdev, "failed to write TX skb to HCI\n");
607aaab5d0eSYan-Hsuan Chuang 		return ret;
608aaab5d0eSYan-Hsuan Chuang 	}
609aaab5d0eSYan-Hsuan Chuang 	return 0;
610aaab5d0eSYan-Hsuan Chuang }
611aaab5d0eSYan-Hsuan Chuang 
rtw_txq_dequeue(struct rtw_dev * rtwdev,struct rtw_txq * rtwtxq)612aaab5d0eSYan-Hsuan Chuang static struct sk_buff *rtw_txq_dequeue(struct rtw_dev *rtwdev,
6133745d3e5SYan-Hsuan Chuang 				       struct rtw_txq *rtwtxq)
6143745d3e5SYan-Hsuan Chuang {
6153745d3e5SYan-Hsuan Chuang 	struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq);
6163745d3e5SYan-Hsuan Chuang 	struct sk_buff *skb;
6173745d3e5SYan-Hsuan Chuang 
6183745d3e5SYan-Hsuan Chuang 	skb = ieee80211_tx_dequeue(rtwdev->hw, txq);
6193745d3e5SYan-Hsuan Chuang 	if (!skb)
620aaab5d0eSYan-Hsuan Chuang 		return NULL;
6213745d3e5SYan-Hsuan Chuang 
622aaab5d0eSYan-Hsuan Chuang 	return skb;
6233745d3e5SYan-Hsuan Chuang }
6243745d3e5SYan-Hsuan Chuang 
rtw_txq_push(struct rtw_dev * rtwdev,struct rtw_txq * rtwtxq,unsigned long frames)6253745d3e5SYan-Hsuan Chuang static void rtw_txq_push(struct rtw_dev *rtwdev,
6263745d3e5SYan-Hsuan Chuang 			 struct rtw_txq *rtwtxq,
6273745d3e5SYan-Hsuan Chuang 			 unsigned long frames)
6283745d3e5SYan-Hsuan Chuang {
629aaab5d0eSYan-Hsuan Chuang 	struct sk_buff *skb;
630aaab5d0eSYan-Hsuan Chuang 	int ret;
6313745d3e5SYan-Hsuan Chuang 	int i;
6323745d3e5SYan-Hsuan Chuang 
6333745d3e5SYan-Hsuan Chuang 	rcu_read_lock();
6343745d3e5SYan-Hsuan Chuang 
635aaab5d0eSYan-Hsuan Chuang 	for (i = 0; i < frames; i++) {
636aaab5d0eSYan-Hsuan Chuang 		skb = rtw_txq_dequeue(rtwdev, rtwtxq);
637aaab5d0eSYan-Hsuan Chuang 		if (!skb)
6383745d3e5SYan-Hsuan Chuang 			break;
6393745d3e5SYan-Hsuan Chuang 
640aaab5d0eSYan-Hsuan Chuang 		ret = rtw_txq_push_skb(rtwdev, rtwtxq, skb);
641aaab5d0eSYan-Hsuan Chuang 		if (ret) {
642aaab5d0eSYan-Hsuan Chuang 			rtw_err(rtwdev, "failed to pusk skb, ret %d\n", ret);
643aaab5d0eSYan-Hsuan Chuang 			break;
644aaab5d0eSYan-Hsuan Chuang 		}
645aaab5d0eSYan-Hsuan Chuang 	}
646aaab5d0eSYan-Hsuan Chuang 
6473745d3e5SYan-Hsuan Chuang 	rcu_read_unlock();
6483745d3e5SYan-Hsuan Chuang }
6493745d3e5SYan-Hsuan Chuang 
__rtw_tx_work(struct rtw_dev * rtwdev)65067d7f24bSChih-Kang Chang void __rtw_tx_work(struct rtw_dev *rtwdev)
6513745d3e5SYan-Hsuan Chuang {
6523745d3e5SYan-Hsuan Chuang 	struct rtw_txq *rtwtxq, *tmp;
6533745d3e5SYan-Hsuan Chuang 
6543745d3e5SYan-Hsuan Chuang 	spin_lock_bh(&rtwdev->txq_lock);
6553745d3e5SYan-Hsuan Chuang 
6563745d3e5SYan-Hsuan Chuang 	list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->txqs, list) {
6573745d3e5SYan-Hsuan Chuang 		struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq);
6583745d3e5SYan-Hsuan Chuang 		unsigned long frame_cnt;
6593745d3e5SYan-Hsuan Chuang 		unsigned long byte_cnt;
6603745d3e5SYan-Hsuan Chuang 
6613745d3e5SYan-Hsuan Chuang 		ieee80211_txq_get_depth(txq, &frame_cnt, &byte_cnt);
6623745d3e5SYan-Hsuan Chuang 		rtw_txq_push(rtwdev, rtwtxq, frame_cnt);
6633745d3e5SYan-Hsuan Chuang 
6643745d3e5SYan-Hsuan Chuang 		list_del_init(&rtwtxq->list);
6653745d3e5SYan-Hsuan Chuang 	}
6663745d3e5SYan-Hsuan Chuang 
667aaab5d0eSYan-Hsuan Chuang 	rtw_hci_tx_kick_off(rtwdev);
668aaab5d0eSYan-Hsuan Chuang 
6693745d3e5SYan-Hsuan Chuang 	spin_unlock_bh(&rtwdev->txq_lock);
6703745d3e5SYan-Hsuan Chuang }
6713745d3e5SYan-Hsuan Chuang 
rtw_tx_work(struct work_struct * w)67267d7f24bSChih-Kang Chang void rtw_tx_work(struct work_struct *w)
67367d7f24bSChih-Kang Chang {
67467d7f24bSChih-Kang Chang 	struct rtw_dev *rtwdev = container_of(w, struct rtw_dev, tx_work);
67567d7f24bSChih-Kang Chang 
67667d7f24bSChih-Kang Chang 	__rtw_tx_work(rtwdev);
67767d7f24bSChih-Kang Chang }
67867d7f24bSChih-Kang Chang 
rtw_txq_init(struct rtw_dev * rtwdev,struct ieee80211_txq * txq)6793745d3e5SYan-Hsuan Chuang void rtw_txq_init(struct rtw_dev *rtwdev, struct ieee80211_txq *txq)
6803745d3e5SYan-Hsuan Chuang {
6813745d3e5SYan-Hsuan Chuang 	struct rtw_txq *rtwtxq;
6823745d3e5SYan-Hsuan Chuang 
6833745d3e5SYan-Hsuan Chuang 	if (!txq)
6843745d3e5SYan-Hsuan Chuang 		return;
6853745d3e5SYan-Hsuan Chuang 
6863745d3e5SYan-Hsuan Chuang 	rtwtxq = (struct rtw_txq *)txq->drv_priv;
6873745d3e5SYan-Hsuan Chuang 	INIT_LIST_HEAD(&rtwtxq->list);
6883745d3e5SYan-Hsuan Chuang }
6893745d3e5SYan-Hsuan Chuang 
rtw_txq_cleanup(struct rtw_dev * rtwdev,struct ieee80211_txq * txq)6903745d3e5SYan-Hsuan Chuang void rtw_txq_cleanup(struct rtw_dev *rtwdev, struct ieee80211_txq *txq)
6913745d3e5SYan-Hsuan Chuang {
6923745d3e5SYan-Hsuan Chuang 	struct rtw_txq *rtwtxq;
6933745d3e5SYan-Hsuan Chuang 
6943745d3e5SYan-Hsuan Chuang 	if (!txq)
6953745d3e5SYan-Hsuan Chuang 		return;
6963745d3e5SYan-Hsuan Chuang 
6973745d3e5SYan-Hsuan Chuang 	rtwtxq = (struct rtw_txq *)txq->drv_priv;
6983745d3e5SYan-Hsuan Chuang 	spin_lock_bh(&rtwdev->txq_lock);
6993745d3e5SYan-Hsuan Chuang 	if (!list_empty(&rtwtxq->list))
7003745d3e5SYan-Hsuan Chuang 		list_del_init(&rtwtxq->list);
7013745d3e5SYan-Hsuan Chuang 	spin_unlock_bh(&rtwdev->txq_lock);
7023745d3e5SYan-Hsuan Chuang }
7037b6e9df9SMartin Blumenstingl 
7047b6e9df9SMartin Blumenstingl static const enum rtw_tx_queue_type ac_to_hwq[] = {
7057b6e9df9SMartin Blumenstingl 	[IEEE80211_AC_VO] = RTW_TX_QUEUE_VO,
7067b6e9df9SMartin Blumenstingl 	[IEEE80211_AC_VI] = RTW_TX_QUEUE_VI,
7077b6e9df9SMartin Blumenstingl 	[IEEE80211_AC_BE] = RTW_TX_QUEUE_BE,
7087b6e9df9SMartin Blumenstingl 	[IEEE80211_AC_BK] = RTW_TX_QUEUE_BK,
7097b6e9df9SMartin Blumenstingl };
7107b6e9df9SMartin Blumenstingl 
7117b6e9df9SMartin Blumenstingl static_assert(ARRAY_SIZE(ac_to_hwq) == IEEE80211_NUM_ACS);
7127b6e9df9SMartin Blumenstingl 
rtw_tx_ac_to_hwq(enum ieee80211_ac_numbers ac)7137b6e9df9SMartin Blumenstingl enum rtw_tx_queue_type rtw_tx_ac_to_hwq(enum ieee80211_ac_numbers ac)
7147b6e9df9SMartin Blumenstingl {
7157b6e9df9SMartin Blumenstingl 	if (WARN_ON(unlikely(ac >= IEEE80211_NUM_ACS)))
7167b6e9df9SMartin Blumenstingl 		return RTW_TX_QUEUE_BE;
7177b6e9df9SMartin Blumenstingl 
7187b6e9df9SMartin Blumenstingl 	return ac_to_hwq[ac];
7197b6e9df9SMartin Blumenstingl }
7207b6e9df9SMartin Blumenstingl EXPORT_SYMBOL(rtw_tx_ac_to_hwq);
7217b6e9df9SMartin Blumenstingl 
rtw_tx_queue_mapping(struct sk_buff * skb)7227b6e9df9SMartin Blumenstingl enum rtw_tx_queue_type rtw_tx_queue_mapping(struct sk_buff *skb)
7237b6e9df9SMartin Blumenstingl {
7247b6e9df9SMartin Blumenstingl 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
7257b6e9df9SMartin Blumenstingl 	__le16 fc = hdr->frame_control;
7267b6e9df9SMartin Blumenstingl 	u8 q_mapping = skb_get_queue_mapping(skb);
7277b6e9df9SMartin Blumenstingl 	enum rtw_tx_queue_type queue;
7287b6e9df9SMartin Blumenstingl 
7297b6e9df9SMartin Blumenstingl 	if (unlikely(ieee80211_is_beacon(fc)))
7307b6e9df9SMartin Blumenstingl 		queue = RTW_TX_QUEUE_BCN;
7317b6e9df9SMartin Blumenstingl 	else if (unlikely(ieee80211_is_mgmt(fc) || ieee80211_is_ctl(fc)))
7327b6e9df9SMartin Blumenstingl 		queue = RTW_TX_QUEUE_MGMT;
7337b6e9df9SMartin Blumenstingl 	else if (is_broadcast_ether_addr(hdr->addr1) ||
7347b6e9df9SMartin Blumenstingl 		 is_multicast_ether_addr(hdr->addr1))
7357b6e9df9SMartin Blumenstingl 		queue = RTW_TX_QUEUE_HI0;
7367b6e9df9SMartin Blumenstingl 	else if (WARN_ON_ONCE(q_mapping >= ARRAY_SIZE(ac_to_hwq)))
7377b6e9df9SMartin Blumenstingl 		queue = ac_to_hwq[IEEE80211_AC_BE];
7387b6e9df9SMartin Blumenstingl 	else
7397b6e9df9SMartin Blumenstingl 		queue = ac_to_hwq[q_mapping];
7407b6e9df9SMartin Blumenstingl 
7417b6e9df9SMartin Blumenstingl 	return queue;
7427b6e9df9SMartin Blumenstingl }
7437b6e9df9SMartin Blumenstingl EXPORT_SYMBOL(rtw_tx_queue_mapping);
744