cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

tx.c (17840B)


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