xref: /linux/drivers/net/wireless/realtek/rtw88/tx.c (revision 3fd6c59042dbba50391e30862beac979491145fe)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2018-2019  Realtek Corporation
3  */
4 
5 #include "main.h"
6 #include "tx.h"
7 #include "fw.h"
8 #include "ps.h"
9 #include "debug.h"
10 
11 static
rtw_tx_stats(struct rtw_dev * rtwdev,struct ieee80211_vif * vif,struct sk_buff * skb)12 void rtw_tx_stats(struct rtw_dev *rtwdev, struct ieee80211_vif *vif,
13 		  struct sk_buff *skb)
14 {
15 	struct ieee80211_hdr *hdr;
16 	struct rtw_vif *rtwvif;
17 
18 	hdr = (struct ieee80211_hdr *)skb->data;
19 
20 	if (!ieee80211_is_data(hdr->frame_control))
21 		return;
22 
23 	if (!is_broadcast_ether_addr(hdr->addr1) &&
24 	    !is_multicast_ether_addr(hdr->addr1)) {
25 		rtwdev->stats.tx_unicast += skb->len;
26 		rtwdev->stats.tx_cnt++;
27 		if (vif) {
28 			rtwvif = (struct rtw_vif *)vif->drv_priv;
29 			rtwvif->stats.tx_unicast += skb->len;
30 			rtwvif->stats.tx_cnt++;
31 		}
32 	}
33 }
34 
rtw_tx_fill_tx_desc(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct sk_buff * skb)35 void rtw_tx_fill_tx_desc(struct rtw_dev *rtwdev,
36 			 struct rtw_tx_pkt_info *pkt_info, struct sk_buff *skb)
37 {
38 	struct rtw_tx_desc *tx_desc = (struct rtw_tx_desc *)skb->data;
39 	bool more_data = false;
40 
41 	if (pkt_info->qsel == TX_DESC_QSEL_HIGH)
42 		more_data = true;
43 
44 	tx_desc->w0 = le32_encode_bits(pkt_info->tx_pkt_size, RTW_TX_DESC_W0_TXPKTSIZE) |
45 		      le32_encode_bits(pkt_info->offset, RTW_TX_DESC_W0_OFFSET) |
46 		      le32_encode_bits(pkt_info->bmc, RTW_TX_DESC_W0_BMC) |
47 		      le32_encode_bits(pkt_info->ls, RTW_TX_DESC_W0_LS) |
48 		      le32_encode_bits(pkt_info->dis_qselseq, RTW_TX_DESC_W0_DISQSELSEQ);
49 
50 	tx_desc->w1 = le32_encode_bits(pkt_info->mac_id, RTW_TX_DESC_W1_MACID) |
51 		      le32_encode_bits(pkt_info->qsel, RTW_TX_DESC_W1_QSEL) |
52 		      le32_encode_bits(pkt_info->rate_id, RTW_TX_DESC_W1_RATE_ID) |
53 		      le32_encode_bits(pkt_info->sec_type, RTW_TX_DESC_W1_SEC_TYPE) |
54 		      le32_encode_bits(pkt_info->pkt_offset, RTW_TX_DESC_W1_PKT_OFFSET) |
55 		      le32_encode_bits(more_data, RTW_TX_DESC_W1_MORE_DATA);
56 
57 	tx_desc->w2 = le32_encode_bits(pkt_info->ampdu_en, RTW_TX_DESC_W2_AGG_EN) |
58 		      le32_encode_bits(pkt_info->report, RTW_TX_DESC_W2_SPE_RPT) |
59 		      le32_encode_bits(pkt_info->ampdu_density, RTW_TX_DESC_W2_AMPDU_DEN) |
60 		      le32_encode_bits(pkt_info->bt_null, RTW_TX_DESC_W2_BT_NULL);
61 
62 	tx_desc->w3 = le32_encode_bits(pkt_info->hw_ssn_sel, RTW_TX_DESC_W3_HW_SSN_SEL) |
63 		      le32_encode_bits(pkt_info->use_rate, RTW_TX_DESC_W3_USE_RATE) |
64 		      le32_encode_bits(pkt_info->dis_rate_fallback, RTW_TX_DESC_W3_DISDATAFB) |
65 		      le32_encode_bits(pkt_info->rts, RTW_TX_DESC_W3_USE_RTS) |
66 		      le32_encode_bits(pkt_info->nav_use_hdr, RTW_TX_DESC_W3_NAVUSEHDR) |
67 		      le32_encode_bits(pkt_info->ampdu_factor, RTW_TX_DESC_W3_MAX_AGG_NUM);
68 
69 	tx_desc->w4 = le32_encode_bits(pkt_info->rate, RTW_TX_DESC_W4_DATARATE);
70 
71 	if (rtwdev->chip->old_datarate_fb_limit)
72 		tx_desc->w4 |= le32_encode_bits(0x1f, RTW_TX_DESC_W4_DATARATE_FB_LIMIT);
73 
74 	tx_desc->w5 = le32_encode_bits(pkt_info->short_gi, RTW_TX_DESC_W5_DATA_SHORT) |
75 		      le32_encode_bits(pkt_info->bw, RTW_TX_DESC_W5_DATA_BW) |
76 		      le32_encode_bits(pkt_info->ldpc, RTW_TX_DESC_W5_DATA_LDPC) |
77 		      le32_encode_bits(pkt_info->stbc, RTW_TX_DESC_W5_DATA_STBC);
78 
79 	tx_desc->w6 = le32_encode_bits(pkt_info->sn, RTW_TX_DESC_W6_SW_DEFINE);
80 
81 	tx_desc->w8 = le32_encode_bits(pkt_info->en_hwseq, RTW_TX_DESC_W8_EN_HWSEQ);
82 
83 	tx_desc->w9 = le32_encode_bits(pkt_info->seq, RTW_TX_DESC_W9_SW_SEQ);
84 
85 	if (pkt_info->rts) {
86 		tx_desc->w4 |= le32_encode_bits(DESC_RATE24M, RTW_TX_DESC_W4_RTSRATE);
87 		tx_desc->w5 |= le32_encode_bits(1, RTW_TX_DESC_W5_DATA_RTS_SHORT);
88 	}
89 
90 	if (pkt_info->tim_offset)
91 		tx_desc->w9 |= le32_encode_bits(1, RTW_TX_DESC_W9_TIM_EN) |
92 			       le32_encode_bits(pkt_info->tim_offset, RTW_TX_DESC_W9_TIM_OFFSET);
93 }
94 EXPORT_SYMBOL(rtw_tx_fill_tx_desc);
95 
get_tx_ampdu_factor(struct ieee80211_sta * sta)96 static u8 get_tx_ampdu_factor(struct ieee80211_sta *sta)
97 {
98 	u8 exp = sta->deflink.ht_cap.ampdu_factor;
99 
100 	/* the least ampdu factor is 8K, and the value in the tx desc is the
101 	 * max aggregation num, which represents val * 2 packets can be
102 	 * aggregated in an AMPDU, so here we should use 8/2=4 as the base
103 	 */
104 	return (BIT(2) << exp) - 1;
105 }
106 
get_tx_ampdu_density(struct ieee80211_sta * sta)107 static u8 get_tx_ampdu_density(struct ieee80211_sta *sta)
108 {
109 	return sta->deflink.ht_cap.ampdu_density;
110 }
111 
get_highest_ht_tx_rate(struct rtw_dev * rtwdev,struct ieee80211_sta * sta)112 static u8 get_highest_ht_tx_rate(struct rtw_dev *rtwdev,
113 				 struct ieee80211_sta *sta)
114 {
115 	u8 rate;
116 
117 	if (rtwdev->hal.rf_type == RF_2T2R && sta->deflink.ht_cap.mcs.rx_mask[1] != 0)
118 		rate = DESC_RATEMCS15;
119 	else
120 		rate = DESC_RATEMCS7;
121 
122 	return rate;
123 }
124 
get_highest_vht_tx_rate(struct rtw_dev * rtwdev,struct ieee80211_sta * sta)125 static u8 get_highest_vht_tx_rate(struct rtw_dev *rtwdev,
126 				  struct ieee80211_sta *sta)
127 {
128 	struct rtw_efuse *efuse = &rtwdev->efuse;
129 	u8 rate;
130 	u16 tx_mcs_map;
131 
132 	tx_mcs_map = le16_to_cpu(sta->deflink.vht_cap.vht_mcs.tx_mcs_map);
133 	if (efuse->hw_cap.nss == 1) {
134 		switch (tx_mcs_map & 0x3) {
135 		case IEEE80211_VHT_MCS_SUPPORT_0_7:
136 			rate = DESC_RATEVHT1SS_MCS7;
137 			break;
138 		case IEEE80211_VHT_MCS_SUPPORT_0_8:
139 			rate = DESC_RATEVHT1SS_MCS8;
140 			break;
141 		default:
142 		case IEEE80211_VHT_MCS_SUPPORT_0_9:
143 			rate = DESC_RATEVHT1SS_MCS9;
144 			break;
145 		}
146 	} else if (efuse->hw_cap.nss >= 2) {
147 		switch ((tx_mcs_map & 0xc) >> 2) {
148 		case IEEE80211_VHT_MCS_SUPPORT_0_7:
149 			rate = DESC_RATEVHT2SS_MCS7;
150 			break;
151 		case IEEE80211_VHT_MCS_SUPPORT_0_8:
152 			rate = DESC_RATEVHT2SS_MCS8;
153 			break;
154 		default:
155 		case IEEE80211_VHT_MCS_SUPPORT_0_9:
156 			rate = DESC_RATEVHT2SS_MCS9;
157 			break;
158 		}
159 	} else {
160 		rate = DESC_RATEVHT1SS_MCS9;
161 	}
162 
163 	return rate;
164 }
165 
rtw_tx_report_enable(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info)166 static void rtw_tx_report_enable(struct rtw_dev *rtwdev,
167 				 struct rtw_tx_pkt_info *pkt_info)
168 {
169 	struct rtw_tx_report *tx_report = &rtwdev->tx_report;
170 
171 	/* [11:8], reserved, fills with zero
172 	 * [7:2],  tx report sequence number
173 	 * [1:0],  firmware use, fills with zero
174 	 */
175 	pkt_info->sn = (atomic_inc_return(&tx_report->sn) << 2) & 0xfc;
176 	pkt_info->report = true;
177 }
178 
rtw_tx_report_purge_timer(struct timer_list * t)179 void rtw_tx_report_purge_timer(struct timer_list *t)
180 {
181 	struct rtw_dev *rtwdev = from_timer(rtwdev, t, tx_report.purge_timer);
182 	struct rtw_tx_report *tx_report = &rtwdev->tx_report;
183 	unsigned long flags;
184 
185 	if (skb_queue_len(&tx_report->queue) == 0)
186 		return;
187 
188 	rtw_warn(rtwdev, "failed to get tx report from firmware\n");
189 
190 	spin_lock_irqsave(&tx_report->q_lock, flags);
191 	skb_queue_purge(&tx_report->queue);
192 	spin_unlock_irqrestore(&tx_report->q_lock, flags);
193 }
194 
rtw_tx_report_enqueue(struct rtw_dev * rtwdev,struct sk_buff * skb,u8 sn)195 void rtw_tx_report_enqueue(struct rtw_dev *rtwdev, struct sk_buff *skb, u8 sn)
196 {
197 	struct rtw_tx_report *tx_report = &rtwdev->tx_report;
198 	unsigned long flags;
199 	u8 *drv_data;
200 
201 	/* pass sn to tx report handler through driver data */
202 	drv_data = (u8 *)IEEE80211_SKB_CB(skb)->status.status_driver_data;
203 	*drv_data = sn;
204 
205 	spin_lock_irqsave(&tx_report->q_lock, flags);
206 	__skb_queue_tail(&tx_report->queue, skb);
207 	spin_unlock_irqrestore(&tx_report->q_lock, flags);
208 
209 	mod_timer(&tx_report->purge_timer, jiffies + RTW_TX_PROBE_TIMEOUT);
210 }
211 EXPORT_SYMBOL(rtw_tx_report_enqueue);
212 
rtw_tx_report_tx_status(struct rtw_dev * rtwdev,struct sk_buff * skb,bool acked)213 static void rtw_tx_report_tx_status(struct rtw_dev *rtwdev,
214 				    struct sk_buff *skb, bool acked)
215 {
216 	struct ieee80211_tx_info *info;
217 
218 	info = IEEE80211_SKB_CB(skb);
219 	ieee80211_tx_info_clear_status(info);
220 	if (acked)
221 		info->flags |= IEEE80211_TX_STAT_ACK;
222 	else
223 		info->flags &= ~IEEE80211_TX_STAT_ACK;
224 
225 	ieee80211_tx_status_irqsafe(rtwdev->hw, skb);
226 }
227 
rtw_tx_report_handle(struct rtw_dev * rtwdev,struct sk_buff * skb,int src)228 void rtw_tx_report_handle(struct rtw_dev *rtwdev, struct sk_buff *skb, int src)
229 {
230 	struct rtw_tx_report *tx_report = &rtwdev->tx_report;
231 	struct rtw_c2h_cmd *c2h;
232 	struct sk_buff *cur, *tmp;
233 	unsigned long flags;
234 	u8 sn, st;
235 	u8 *n;
236 
237 	c2h = get_c2h_from_skb(skb);
238 
239 	if (src == C2H_CCX_TX_RPT) {
240 		sn = GET_CCX_REPORT_SEQNUM_V0(c2h->payload);
241 		st = GET_CCX_REPORT_STATUS_V0(c2h->payload);
242 	} else {
243 		sn = GET_CCX_REPORT_SEQNUM_V1(c2h->payload);
244 		st = GET_CCX_REPORT_STATUS_V1(c2h->payload);
245 	}
246 
247 	spin_lock_irqsave(&tx_report->q_lock, flags);
248 	skb_queue_walk_safe(&tx_report->queue, cur, tmp) {
249 		n = (u8 *)IEEE80211_SKB_CB(cur)->status.status_driver_data;
250 		if (*n == sn) {
251 			__skb_unlink(cur, &tx_report->queue);
252 			rtw_tx_report_tx_status(rtwdev, cur, st == 0);
253 			break;
254 		}
255 	}
256 	spin_unlock_irqrestore(&tx_report->q_lock, flags);
257 }
258 
rtw_get_mgmt_rate(struct rtw_dev * rtwdev,struct sk_buff * skb,u8 lowest_rate,bool ignore_rate)259 static u8 rtw_get_mgmt_rate(struct rtw_dev *rtwdev, struct sk_buff *skb,
260 			    u8 lowest_rate, bool ignore_rate)
261 {
262 	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
263 	struct ieee80211_vif *vif = tx_info->control.vif;
264 	bool force_lowest = test_bit(RTW_FLAG_FORCE_LOWEST_RATE, rtwdev->flags);
265 
266 	if (!vif || !vif->bss_conf.basic_rates || ignore_rate || force_lowest)
267 		return lowest_rate;
268 
269 	return __ffs(vif->bss_conf.basic_rates) + lowest_rate;
270 }
271 
rtw_tx_pkt_info_update_rate(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct sk_buff * skb,bool ignore_rate)272 static void rtw_tx_pkt_info_update_rate(struct rtw_dev *rtwdev,
273 					struct rtw_tx_pkt_info *pkt_info,
274 					struct sk_buff *skb,
275 					bool ignore_rate)
276 {
277 	if (rtwdev->hal.current_band_type == RTW_BAND_2G) {
278 		pkt_info->rate_id = RTW_RATEID_B_20M;
279 		pkt_info->rate = rtw_get_mgmt_rate(rtwdev, skb, DESC_RATE1M,
280 						   ignore_rate);
281 	} else {
282 		pkt_info->rate_id = RTW_RATEID_G;
283 		pkt_info->rate = rtw_get_mgmt_rate(rtwdev, skb, DESC_RATE6M,
284 						   ignore_rate);
285 	}
286 
287 	pkt_info->use_rate = true;
288 	pkt_info->dis_rate_fallback = true;
289 }
290 
rtw_tx_pkt_info_update_sec(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct sk_buff * skb)291 static void rtw_tx_pkt_info_update_sec(struct rtw_dev *rtwdev,
292 				       struct rtw_tx_pkt_info *pkt_info,
293 				       struct sk_buff *skb)
294 {
295 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
296 	u8 sec_type = 0;
297 
298 	if (info && info->control.hw_key) {
299 		struct ieee80211_key_conf *key = info->control.hw_key;
300 
301 		switch (key->cipher) {
302 		case WLAN_CIPHER_SUITE_WEP40:
303 		case WLAN_CIPHER_SUITE_WEP104:
304 		case WLAN_CIPHER_SUITE_TKIP:
305 			sec_type = 0x01;
306 			break;
307 		case WLAN_CIPHER_SUITE_CCMP:
308 			sec_type = 0x03;
309 			break;
310 		default:
311 			break;
312 		}
313 	}
314 
315 	pkt_info->sec_type = sec_type;
316 }
317 
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)318 static void rtw_tx_mgmt_pkt_info_update(struct rtw_dev *rtwdev,
319 					struct rtw_tx_pkt_info *pkt_info,
320 					struct ieee80211_sta *sta,
321 					struct sk_buff *skb)
322 {
323 	rtw_tx_pkt_info_update_rate(rtwdev, pkt_info, skb, false);
324 	pkt_info->dis_qselseq = true;
325 	pkt_info->en_hwseq = true;
326 	pkt_info->hw_ssn_sel = 0;
327 	/* TODO: need to change hw port and hw ssn sel for multiple vifs */
328 }
329 
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)330 static void rtw_tx_data_pkt_info_update(struct rtw_dev *rtwdev,
331 					struct rtw_tx_pkt_info *pkt_info,
332 					struct ieee80211_sta *sta,
333 					struct sk_buff *skb)
334 {
335 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
336 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
337 	struct ieee80211_hw *hw = rtwdev->hw;
338 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
339 	struct rtw_sta_info *si;
340 	u8 fix_rate;
341 	u16 seq;
342 	u8 ampdu_factor = 0;
343 	u8 ampdu_density = 0;
344 	bool ampdu_en = false;
345 	u8 rate = DESC_RATE6M;
346 	u8 rate_id = 6;
347 	u8 bw = RTW_CHANNEL_WIDTH_20;
348 	bool stbc = false;
349 	bool ldpc = false;
350 
351 	seq = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4;
352 
353 	/* for broadcast/multicast, use default values */
354 	if (!sta)
355 		goto out;
356 
357 	if (info->flags & IEEE80211_TX_CTL_AMPDU) {
358 		ampdu_en = true;
359 		ampdu_factor = get_tx_ampdu_factor(sta);
360 		ampdu_density = get_tx_ampdu_density(sta);
361 	}
362 
363 	if (info->control.use_rts || skb->len > hw->wiphy->rts_threshold)
364 		pkt_info->rts = true;
365 
366 	if (sta->deflink.vht_cap.vht_supported)
367 		rate = get_highest_vht_tx_rate(rtwdev, sta);
368 	else if (sta->deflink.ht_cap.ht_supported)
369 		rate = get_highest_ht_tx_rate(rtwdev, sta);
370 	else if (sta->deflink.supp_rates[0] <= 0xf)
371 		rate = DESC_RATE11M;
372 	else
373 		rate = DESC_RATE54M;
374 
375 	si = (struct rtw_sta_info *)sta->drv_priv;
376 
377 	bw = si->bw_mode;
378 	rate_id = si->rate_id;
379 	stbc = rtwdev->hal.txrx_1ss ? false : si->stbc_en;
380 	ldpc = si->ldpc_en;
381 
382 out:
383 	pkt_info->seq = seq;
384 	pkt_info->ampdu_factor = ampdu_factor;
385 	pkt_info->ampdu_density = ampdu_density;
386 	pkt_info->ampdu_en = ampdu_en;
387 	pkt_info->rate = rate;
388 	pkt_info->rate_id = rate_id;
389 	pkt_info->bw = bw;
390 	pkt_info->stbc = stbc;
391 	pkt_info->ldpc = ldpc;
392 
393 	fix_rate = dm_info->fix_rate;
394 	if (fix_rate < DESC_RATE_MAX) {
395 		pkt_info->rate = fix_rate;
396 		pkt_info->dis_rate_fallback = true;
397 		pkt_info->use_rate = true;
398 	}
399 }
400 
rtw_tx_pkt_info_update(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct ieee80211_sta * sta,struct sk_buff * skb)401 void rtw_tx_pkt_info_update(struct rtw_dev *rtwdev,
402 			    struct rtw_tx_pkt_info *pkt_info,
403 			    struct ieee80211_sta *sta,
404 			    struct sk_buff *skb)
405 {
406 	const struct rtw_chip_info *chip = rtwdev->chip;
407 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
408 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
409 	struct ieee80211_vif *vif = info->control.vif;
410 	struct rtw_sta_info *si;
411 	struct rtw_vif *rtwvif;
412 	__le16 fc = hdr->frame_control;
413 	bool bmc;
414 
415 	if (sta) {
416 		si = (struct rtw_sta_info *)sta->drv_priv;
417 		pkt_info->mac_id = si->mac_id;
418 	} else if (vif) {
419 		rtwvif = (struct rtw_vif *)vif->drv_priv;
420 		pkt_info->mac_id = rtwvif->mac_id;
421 	}
422 
423 	if (ieee80211_is_mgmt(fc) || ieee80211_is_nullfunc(fc))
424 		rtw_tx_mgmt_pkt_info_update(rtwdev, pkt_info, sta, skb);
425 	else if (ieee80211_is_data(fc))
426 		rtw_tx_data_pkt_info_update(rtwdev, pkt_info, sta, skb);
427 
428 	bmc = is_broadcast_ether_addr(hdr->addr1) ||
429 	      is_multicast_ether_addr(hdr->addr1);
430 
431 	if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
432 		rtw_tx_report_enable(rtwdev, pkt_info);
433 
434 	pkt_info->bmc = bmc;
435 	rtw_tx_pkt_info_update_sec(rtwdev, pkt_info, skb);
436 	pkt_info->tx_pkt_size = skb->len;
437 	pkt_info->offset = chip->tx_pkt_desc_sz;
438 	pkt_info->qsel = skb->priority;
439 	pkt_info->ls = true;
440 
441 	/* maybe merge with tx status ? */
442 	rtw_tx_stats(rtwdev, vif, skb);
443 }
444 
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)445 void rtw_tx_rsvd_page_pkt_info_update(struct rtw_dev *rtwdev,
446 				      struct rtw_tx_pkt_info *pkt_info,
447 				      struct sk_buff *skb,
448 				      enum rtw_rsvd_packet_type type)
449 {
450 	const struct rtw_chip_info *chip = rtwdev->chip;
451 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
452 	bool bmc;
453 
454 	/* A beacon or dummy reserved page packet indicates that it is the first
455 	 * reserved page, and the qsel of it will be set in each hci.
456 	 */
457 	if (type != RSVD_BEACON && type != RSVD_DUMMY)
458 		pkt_info->qsel = TX_DESC_QSEL_MGMT;
459 
460 	rtw_tx_pkt_info_update_rate(rtwdev, pkt_info, skb, true);
461 
462 	bmc = is_broadcast_ether_addr(hdr->addr1) ||
463 	      is_multicast_ether_addr(hdr->addr1);
464 	pkt_info->bmc = bmc;
465 	pkt_info->tx_pkt_size = skb->len;
466 	pkt_info->offset = chip->tx_pkt_desc_sz;
467 	pkt_info->ls = true;
468 	if (type == RSVD_PS_POLL) {
469 		pkt_info->nav_use_hdr = true;
470 	} else {
471 		pkt_info->dis_qselseq = true;
472 		pkt_info->en_hwseq = true;
473 		pkt_info->hw_ssn_sel = 0;
474 	}
475 	if (type == RSVD_QOS_NULL)
476 		pkt_info->bt_null = true;
477 
478 	if (type == RSVD_BEACON) {
479 		struct rtw_rsvd_page *rsvd_pkt;
480 		int hdr_len;
481 
482 		rsvd_pkt = list_first_entry_or_null(&rtwdev->rsvd_page_list,
483 						    struct rtw_rsvd_page,
484 						    build_list);
485 		if (rsvd_pkt && rsvd_pkt->tim_offset != 0) {
486 			hdr_len = sizeof(struct ieee80211_hdr_3addr);
487 			pkt_info->tim_offset = rsvd_pkt->tim_offset - hdr_len;
488 		}
489 	}
490 
491 	rtw_tx_pkt_info_update_sec(rtwdev, pkt_info, skb);
492 
493 	/* TODO: need to change hw port and hw ssn sel for multiple vifs */
494 }
495 
496 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)497 rtw_tx_write_data_rsvd_page_get(struct rtw_dev *rtwdev,
498 				struct rtw_tx_pkt_info *pkt_info,
499 				u8 *buf, u32 size)
500 {
501 	const struct rtw_chip_info *chip = rtwdev->chip;
502 	struct sk_buff *skb;
503 	u32 tx_pkt_desc_sz;
504 	u32 length;
505 
506 	tx_pkt_desc_sz = chip->tx_pkt_desc_sz;
507 	length = size + tx_pkt_desc_sz;
508 	skb = dev_alloc_skb(length);
509 	if (!skb) {
510 		rtw_err(rtwdev, "failed to alloc write data rsvd page skb\n");
511 		return NULL;
512 	}
513 
514 	skb_reserve(skb, tx_pkt_desc_sz);
515 	skb_put_data(skb, buf, size);
516 	rtw_tx_rsvd_page_pkt_info_update(rtwdev, pkt_info, skb, RSVD_BEACON);
517 
518 	return skb;
519 }
520 EXPORT_SYMBOL(rtw_tx_write_data_rsvd_page_get);
521 
522 struct sk_buff *
rtw_tx_write_data_h2c_get(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,u8 * buf,u32 size)523 rtw_tx_write_data_h2c_get(struct rtw_dev *rtwdev,
524 			  struct rtw_tx_pkt_info *pkt_info,
525 			  u8 *buf, u32 size)
526 {
527 	const struct rtw_chip_info *chip = rtwdev->chip;
528 	struct sk_buff *skb;
529 	u32 tx_pkt_desc_sz;
530 	u32 length;
531 
532 	tx_pkt_desc_sz = chip->tx_pkt_desc_sz;
533 	length = size + tx_pkt_desc_sz;
534 	skb = dev_alloc_skb(length);
535 	if (!skb) {
536 		rtw_err(rtwdev, "failed to alloc write data h2c skb\n");
537 		return NULL;
538 	}
539 
540 	skb_reserve(skb, tx_pkt_desc_sz);
541 	skb_put_data(skb, buf, size);
542 	pkt_info->tx_pkt_size = size;
543 
544 	return skb;
545 }
546 EXPORT_SYMBOL(rtw_tx_write_data_h2c_get);
547 
rtw_tx(struct rtw_dev * rtwdev,struct ieee80211_tx_control * control,struct sk_buff * skb)548 void rtw_tx(struct rtw_dev *rtwdev,
549 	    struct ieee80211_tx_control *control,
550 	    struct sk_buff *skb)
551 {
552 	struct rtw_tx_pkt_info pkt_info = {0};
553 	int ret;
554 
555 	rtw_tx_pkt_info_update(rtwdev, &pkt_info, control->sta, skb);
556 	ret = rtw_hci_tx_write(rtwdev, &pkt_info, skb);
557 	if (ret) {
558 		rtw_err(rtwdev, "failed to write TX skb to HCI\n");
559 		goto out;
560 	}
561 
562 	rtw_hci_tx_kick_off(rtwdev);
563 
564 	return;
565 
566 out:
567 	ieee80211_free_txskb(rtwdev->hw, skb);
568 }
569 
rtw_txq_check_agg(struct rtw_dev * rtwdev,struct rtw_txq * rtwtxq,struct sk_buff * skb)570 static void rtw_txq_check_agg(struct rtw_dev *rtwdev,
571 			      struct rtw_txq *rtwtxq,
572 			      struct sk_buff *skb)
573 {
574 	struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq);
575 	struct ieee80211_tx_info *info;
576 	struct rtw_sta_info *si;
577 
578 	if (test_bit(RTW_TXQ_AMPDU, &rtwtxq->flags)) {
579 		info = IEEE80211_SKB_CB(skb);
580 		info->flags |= IEEE80211_TX_CTL_AMPDU;
581 		return;
582 	}
583 
584 	if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
585 		return;
586 
587 	if (test_bit(RTW_TXQ_BLOCK_BA, &rtwtxq->flags))
588 		return;
589 
590 	if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
591 		return;
592 
593 	if (!txq->sta)
594 		return;
595 
596 	si = (struct rtw_sta_info *)txq->sta->drv_priv;
597 	set_bit(txq->tid, si->tid_ba);
598 
599 	ieee80211_queue_work(rtwdev->hw, &rtwdev->ba_work);
600 }
601 
rtw_txq_push_skb(struct rtw_dev * rtwdev,struct rtw_txq * rtwtxq,struct sk_buff * skb)602 static int rtw_txq_push_skb(struct rtw_dev *rtwdev,
603 			    struct rtw_txq *rtwtxq,
604 			    struct sk_buff *skb)
605 {
606 	struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq);
607 	struct rtw_tx_pkt_info pkt_info = {0};
608 	int ret;
609 
610 	rtw_txq_check_agg(rtwdev, rtwtxq, skb);
611 
612 	rtw_tx_pkt_info_update(rtwdev, &pkt_info, txq->sta, skb);
613 	ret = rtw_hci_tx_write(rtwdev, &pkt_info, skb);
614 	if (ret) {
615 		rtw_err(rtwdev, "failed to write TX skb to HCI\n");
616 		return ret;
617 	}
618 	return 0;
619 }
620 
rtw_txq_dequeue(struct rtw_dev * rtwdev,struct rtw_txq * rtwtxq)621 static struct sk_buff *rtw_txq_dequeue(struct rtw_dev *rtwdev,
622 				       struct rtw_txq *rtwtxq)
623 {
624 	struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq);
625 	struct sk_buff *skb;
626 
627 	skb = ieee80211_tx_dequeue(rtwdev->hw, txq);
628 	if (!skb)
629 		return NULL;
630 
631 	return skb;
632 }
633 
rtw_txq_push(struct rtw_dev * rtwdev,struct rtw_txq * rtwtxq,unsigned long frames)634 static void rtw_txq_push(struct rtw_dev *rtwdev,
635 			 struct rtw_txq *rtwtxq,
636 			 unsigned long frames)
637 {
638 	struct sk_buff *skb;
639 	int ret;
640 	int i;
641 
642 	rcu_read_lock();
643 
644 	for (i = 0; i < frames; i++) {
645 		skb = rtw_txq_dequeue(rtwdev, rtwtxq);
646 		if (!skb)
647 			break;
648 
649 		ret = rtw_txq_push_skb(rtwdev, rtwtxq, skb);
650 		if (ret) {
651 			rtw_err(rtwdev, "failed to pusk skb, ret %d\n", ret);
652 			break;
653 		}
654 	}
655 
656 	rcu_read_unlock();
657 }
658 
__rtw_tx_work(struct rtw_dev * rtwdev)659 void __rtw_tx_work(struct rtw_dev *rtwdev)
660 {
661 	struct rtw_txq *rtwtxq, *tmp;
662 
663 	spin_lock_bh(&rtwdev->txq_lock);
664 
665 	list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->txqs, list) {
666 		struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq);
667 		unsigned long frame_cnt;
668 
669 		ieee80211_txq_get_depth(txq, &frame_cnt, NULL);
670 		rtw_txq_push(rtwdev, rtwtxq, frame_cnt);
671 
672 		list_del_init(&rtwtxq->list);
673 	}
674 
675 	rtw_hci_tx_kick_off(rtwdev);
676 
677 	spin_unlock_bh(&rtwdev->txq_lock);
678 }
679 
rtw_tx_work(struct work_struct * w)680 void rtw_tx_work(struct work_struct *w)
681 {
682 	struct rtw_dev *rtwdev = container_of(w, struct rtw_dev, tx_work);
683 
684 	__rtw_tx_work(rtwdev);
685 }
686 
rtw_txq_init(struct rtw_dev * rtwdev,struct ieee80211_txq * txq)687 void rtw_txq_init(struct rtw_dev *rtwdev, struct ieee80211_txq *txq)
688 {
689 	struct rtw_txq *rtwtxq;
690 
691 	if (!txq)
692 		return;
693 
694 	rtwtxq = (struct rtw_txq *)txq->drv_priv;
695 	INIT_LIST_HEAD(&rtwtxq->list);
696 }
697 
rtw_txq_cleanup(struct rtw_dev * rtwdev,struct ieee80211_txq * txq)698 void rtw_txq_cleanup(struct rtw_dev *rtwdev, struct ieee80211_txq *txq)
699 {
700 	struct rtw_txq *rtwtxq;
701 
702 	if (!txq)
703 		return;
704 
705 	rtwtxq = (struct rtw_txq *)txq->drv_priv;
706 	spin_lock_bh(&rtwdev->txq_lock);
707 	if (!list_empty(&rtwtxq->list))
708 		list_del_init(&rtwtxq->list);
709 	spin_unlock_bh(&rtwdev->txq_lock);
710 }
711 
712 static const enum rtw_tx_queue_type ac_to_hwq[] = {
713 	[IEEE80211_AC_VO] = RTW_TX_QUEUE_VO,
714 	[IEEE80211_AC_VI] = RTW_TX_QUEUE_VI,
715 	[IEEE80211_AC_BE] = RTW_TX_QUEUE_BE,
716 	[IEEE80211_AC_BK] = RTW_TX_QUEUE_BK,
717 };
718 
719 static_assert(ARRAY_SIZE(ac_to_hwq) == IEEE80211_NUM_ACS);
720 
rtw_tx_ac_to_hwq(enum ieee80211_ac_numbers ac)721 enum rtw_tx_queue_type rtw_tx_ac_to_hwq(enum ieee80211_ac_numbers ac)
722 {
723 	if (WARN_ON(unlikely(ac >= IEEE80211_NUM_ACS)))
724 		return RTW_TX_QUEUE_BE;
725 
726 	return ac_to_hwq[ac];
727 }
728 EXPORT_SYMBOL(rtw_tx_ac_to_hwq);
729 
rtw_tx_queue_mapping(struct sk_buff * skb)730 enum rtw_tx_queue_type rtw_tx_queue_mapping(struct sk_buff *skb)
731 {
732 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
733 	__le16 fc = hdr->frame_control;
734 	u8 q_mapping = skb_get_queue_mapping(skb);
735 	enum rtw_tx_queue_type queue;
736 
737 	if (unlikely(ieee80211_is_beacon(fc)))
738 		queue = RTW_TX_QUEUE_BCN;
739 	else if (unlikely(ieee80211_is_mgmt(fc) || ieee80211_is_ctl(fc)))
740 		queue = RTW_TX_QUEUE_MGMT;
741 	else if (is_broadcast_ether_addr(hdr->addr1) ||
742 		 is_multicast_ether_addr(hdr->addr1))
743 		queue = RTW_TX_QUEUE_HI0;
744 	else if (WARN_ON_ONCE(q_mapping >= ARRAY_SIZE(ac_to_hwq)))
745 		queue = ac_to_hwq[IEEE80211_AC_BE];
746 	else
747 		queue = ac_to_hwq[q_mapping];
748 
749 	return queue;
750 }
751 EXPORT_SYMBOL(rtw_tx_queue_mapping);
752