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