18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: ISC
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
48c2ecf20Sopenharmony_ci * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include "mt76x02.h"
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_civoid mt76x02_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
128c2ecf20Sopenharmony_ci		struct sk_buff *skb)
138c2ecf20Sopenharmony_ci{
148c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
158c2ecf20Sopenharmony_ci	struct mt76x02_dev *dev = hw->priv;
168c2ecf20Sopenharmony_ci	struct ieee80211_vif *vif = info->control.vif;
178c2ecf20Sopenharmony_ci	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci	if (control->sta) {
208c2ecf20Sopenharmony_ci		struct mt76x02_sta *msta;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci		msta = (struct mt76x02_sta *)control->sta->drv_priv;
238c2ecf20Sopenharmony_ci		wcid = &msta->wcid;
248c2ecf20Sopenharmony_ci	} else if (vif) {
258c2ecf20Sopenharmony_ci		struct mt76x02_vif *mvif;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci		mvif = (struct mt76x02_vif *)vif->drv_priv;
288c2ecf20Sopenharmony_ci		wcid = &mvif->group_wcid;
298c2ecf20Sopenharmony_ci	}
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	mt76_tx(&dev->mphy, control->sta, wcid, skb);
328c2ecf20Sopenharmony_ci}
338c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x02_tx);
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_civoid mt76x02_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
368c2ecf20Sopenharmony_ci			  struct sk_buff *skb)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
398c2ecf20Sopenharmony_ci	void *rxwi = skb->data;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	if (q == MT_RXQ_MCU) {
428c2ecf20Sopenharmony_ci		mt76_mcu_rx_event(&dev->mt76, skb);
438c2ecf20Sopenharmony_ci		return;
448c2ecf20Sopenharmony_ci	}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	skb_pull(skb, sizeof(struct mt76x02_rxwi));
478c2ecf20Sopenharmony_ci	if (mt76x02_mac_process_rx(dev, skb, rxwi)) {
488c2ecf20Sopenharmony_ci		dev_kfree_skb(skb);
498c2ecf20Sopenharmony_ci		return;
508c2ecf20Sopenharmony_ci	}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	mt76_rx(mdev, q, skb);
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x02_queue_rx_skb);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cis8 mt76x02_tx_get_max_txpwr_adj(struct mt76x02_dev *dev,
578c2ecf20Sopenharmony_ci				const struct ieee80211_tx_rate *rate)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	s8 max_txpwr;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
628c2ecf20Sopenharmony_ci		u8 mcs = ieee80211_rate_get_vht_mcs(rate);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci		if (mcs == 8 || mcs == 9) {
658c2ecf20Sopenharmony_ci			max_txpwr = dev->mt76.rate_power.vht[8];
668c2ecf20Sopenharmony_ci		} else {
678c2ecf20Sopenharmony_ci			u8 nss, idx;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci			nss = ieee80211_rate_get_vht_nss(rate);
708c2ecf20Sopenharmony_ci			idx = ((nss - 1) << 3) + mcs;
718c2ecf20Sopenharmony_ci			max_txpwr = dev->mt76.rate_power.ht[idx & 0xf];
728c2ecf20Sopenharmony_ci		}
738c2ecf20Sopenharmony_ci	} else if (rate->flags & IEEE80211_TX_RC_MCS) {
748c2ecf20Sopenharmony_ci		max_txpwr = dev->mt76.rate_power.ht[rate->idx & 0xf];
758c2ecf20Sopenharmony_ci	} else {
768c2ecf20Sopenharmony_ci		enum nl80211_band band = dev->mphy.chandef.chan->band;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci		if (band == NL80211_BAND_2GHZ) {
798c2ecf20Sopenharmony_ci			const struct ieee80211_rate *r;
808c2ecf20Sopenharmony_ci			struct wiphy *wiphy = dev->mt76.hw->wiphy;
818c2ecf20Sopenharmony_ci			struct mt76_rate_power *rp = &dev->mt76.rate_power;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci			r = &wiphy->bands[band]->bitrates[rate->idx];
848c2ecf20Sopenharmony_ci			if (r->flags & IEEE80211_RATE_SHORT_PREAMBLE)
858c2ecf20Sopenharmony_ci				max_txpwr = rp->cck[r->hw_value & 0x3];
868c2ecf20Sopenharmony_ci			else
878c2ecf20Sopenharmony_ci				max_txpwr = rp->ofdm[r->hw_value & 0x7];
888c2ecf20Sopenharmony_ci		} else {
898c2ecf20Sopenharmony_ci			max_txpwr = dev->mt76.rate_power.ofdm[rate->idx & 0x7];
908c2ecf20Sopenharmony_ci		}
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	return max_txpwr;
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cis8 mt76x02_tx_get_txpwr_adj(struct mt76x02_dev *dev, s8 txpwr, s8 max_txpwr_adj)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	txpwr = min_t(s8, txpwr, dev->txpower_conf);
998c2ecf20Sopenharmony_ci	txpwr -= (dev->target_power + dev->target_power_delta[0]);
1008c2ecf20Sopenharmony_ci	txpwr = min_t(s8, txpwr, max_txpwr_adj);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	if (!dev->enable_tpc)
1038c2ecf20Sopenharmony_ci		return 0;
1048c2ecf20Sopenharmony_ci	else if (txpwr >= 0)
1058c2ecf20Sopenharmony_ci		return min_t(s8, txpwr, 7);
1068c2ecf20Sopenharmony_ci	else
1078c2ecf20Sopenharmony_ci		return (txpwr < -16) ? 8 : (txpwr + 32) / 2;
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_civoid mt76x02_tx_set_txpwr_auto(struct mt76x02_dev *dev, s8 txpwr)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	s8 txpwr_adj;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	txpwr_adj = mt76x02_tx_get_txpwr_adj(dev, txpwr,
1158c2ecf20Sopenharmony_ci					     dev->mt76.rate_power.ofdm[4]);
1168c2ecf20Sopenharmony_ci	mt76_rmw_field(dev, MT_PROT_AUTO_TX_CFG,
1178c2ecf20Sopenharmony_ci		       MT_PROT_AUTO_TX_CFG_PROT_PADJ, txpwr_adj);
1188c2ecf20Sopenharmony_ci	mt76_rmw_field(dev, MT_PROT_AUTO_TX_CFG,
1198c2ecf20Sopenharmony_ci		       MT_PROT_AUTO_TX_CFG_AUTO_PADJ, txpwr_adj);
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x02_tx_set_txpwr_auto);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cibool mt76x02_tx_status_data(struct mt76_dev *mdev, u8 *update)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
1268c2ecf20Sopenharmony_ci	struct mt76x02_tx_status stat;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	if (!mt76x02_mac_load_tx_status(dev, &stat))
1298c2ecf20Sopenharmony_ci		return false;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	mt76x02_send_tx_status(dev, &stat, update);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	return true;
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x02_tx_status_data);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ciint mt76x02_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
1388c2ecf20Sopenharmony_ci			   enum mt76_txq_id qid, struct mt76_wcid *wcid,
1398c2ecf20Sopenharmony_ci			   struct ieee80211_sta *sta,
1408c2ecf20Sopenharmony_ci			   struct mt76_tx_info *tx_info)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
1438c2ecf20Sopenharmony_ci	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx_info->skb->data;
1448c2ecf20Sopenharmony_ci	struct mt76x02_txwi *txwi = txwi_ptr;
1458c2ecf20Sopenharmony_ci	bool ampdu = IEEE80211_SKB_CB(tx_info->skb)->flags & IEEE80211_TX_CTL_AMPDU;
1468c2ecf20Sopenharmony_ci	int hdrlen, len, pid, qsel = MT_QSEL_EDCA;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	if (qid == MT_TXQ_PSD && wcid && wcid->idx < 128)
1498c2ecf20Sopenharmony_ci		mt76x02_mac_wcid_set_drop(dev, wcid->idx, false);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	hdrlen = ieee80211_hdrlen(hdr->frame_control);
1528c2ecf20Sopenharmony_ci	len = tx_info->skb->len - (hdrlen & 2);
1538c2ecf20Sopenharmony_ci	mt76x02_mac_write_txwi(dev, txwi, tx_info->skb, wcid, sta, len);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	pid = mt76_tx_status_skb_add(mdev, wcid, tx_info->skb);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	/* encode packet rate for no-skb packet id to fix up status reporting */
1588c2ecf20Sopenharmony_ci	if (pid == MT_PACKET_ID_NO_SKB)
1598c2ecf20Sopenharmony_ci		pid = MT_PACKET_ID_HAS_RATE |
1608c2ecf20Sopenharmony_ci		      (le16_to_cpu(txwi->rate) & MT_RXWI_RATE_INDEX) |
1618c2ecf20Sopenharmony_ci		      FIELD_PREP(MT_PKTID_AC,
1628c2ecf20Sopenharmony_ci				 skb_get_queue_mapping(tx_info->skb));
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	txwi->pktid = pid;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	if (mt76_is_skb_pktid(pid) && ampdu)
1678c2ecf20Sopenharmony_ci		qsel = MT_QSEL_MGMT;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	tx_info->info = FIELD_PREP(MT_TXD_INFO_QSEL, qsel) |
1708c2ecf20Sopenharmony_ci			MT_TXD_INFO_80211;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	if (!wcid || wcid->hw_key_idx == 0xff || wcid->sw_iv)
1738c2ecf20Sopenharmony_ci		tx_info->info |= MT_TXD_INFO_WIV;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	if (sta) {
1768c2ecf20Sopenharmony_ci		struct mt76x02_sta *msta = (struct mt76x02_sta *)sta->drv_priv;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci		ewma_pktlen_add(&msta->pktlen, tx_info->skb->len);
1798c2ecf20Sopenharmony_ci	}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	return 0;
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x02_tx_prepare_skb);
184