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