1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4  * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
5  */
6 
7 #include <linux/kernel.h>
8 
9 #include "mt76x02.h"
10 
11 void mt76x02_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
12 		struct sk_buff *skb)
13 {
14 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
15 	struct mt76x02_dev *dev = hw->priv;
16 	struct ieee80211_vif *vif = info->control.vif;
17 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
18 
19 	if (control->sta) {
20 		struct mt76x02_sta *msta;
21 
22 		msta = (struct mt76x02_sta *)control->sta->drv_priv;
23 		wcid = &msta->wcid;
24 	} else if (vif) {
25 		struct mt76x02_vif *mvif;
26 
27 		mvif = (struct mt76x02_vif *)vif->drv_priv;
28 		wcid = &mvif->group_wcid;
29 	}
30 
31 	mt76_tx(&dev->mphy, control->sta, wcid, skb);
32 }
33 EXPORT_SYMBOL_GPL(mt76x02_tx);
34 
35 void mt76x02_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
36 			  struct sk_buff *skb, u32 *info)
37 {
38 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
39 	void *rxwi = skb->data;
40 
41 	if (q == MT_RXQ_MCU) {
42 		mt76_mcu_rx_event(&dev->mt76, skb);
43 		return;
44 	}
45 
46 	skb_pull(skb, sizeof(struct mt76x02_rxwi));
47 	if (mt76x02_mac_process_rx(dev, skb, rxwi)) {
48 		dev_kfree_skb(skb);
49 		return;
50 	}
51 
52 	mt76_rx(mdev, q, skb);
53 }
54 EXPORT_SYMBOL_GPL(mt76x02_queue_rx_skb);
55 
56 s8 mt76x02_tx_get_max_txpwr_adj(struct mt76x02_dev *dev,
57 				const struct ieee80211_tx_rate *rate)
58 {
59 	s8 max_txpwr;
60 
61 	if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
62 		u8 mcs = ieee80211_rate_get_vht_mcs(rate);
63 
64 		if (mcs == 8 || mcs == 9) {
65 			max_txpwr = dev->rate_power.vht[0];
66 		} else {
67 			u8 nss, idx;
68 
69 			nss = ieee80211_rate_get_vht_nss(rate);
70 			idx = ((nss - 1) << 3) + mcs;
71 			max_txpwr = dev->rate_power.ht[idx & 0xf];
72 		}
73 	} else if (rate->flags & IEEE80211_TX_RC_MCS) {
74 		max_txpwr = dev->rate_power.ht[rate->idx & 0xf];
75 	} else {
76 		enum nl80211_band band = dev->mphy.chandef.chan->band;
77 
78 		if (band == NL80211_BAND_2GHZ) {
79 			const struct ieee80211_rate *r;
80 			struct wiphy *wiphy = dev->mt76.hw->wiphy;
81 			struct mt76x02_rate_power *rp = &dev->rate_power;
82 
83 			r = &wiphy->bands[band]->bitrates[rate->idx];
84 			if (r->flags & IEEE80211_RATE_SHORT_PREAMBLE)
85 				max_txpwr = rp->cck[r->hw_value & 0x3];
86 			else
87 				max_txpwr = rp->ofdm[r->hw_value & 0x7];
88 		} else {
89 			max_txpwr = dev->rate_power.ofdm[rate->idx & 0x7];
90 		}
91 	}
92 
93 	return max_txpwr;
94 }
95 
96 s8 mt76x02_tx_get_txpwr_adj(struct mt76x02_dev *dev, s8 txpwr, s8 max_txpwr_adj)
97 {
98 	txpwr = min_t(s8, txpwr, dev->txpower_conf);
99 	txpwr -= (dev->target_power + dev->target_power_delta[0]);
100 	txpwr = min_t(s8, txpwr, max_txpwr_adj);
101 
102 	if (!dev->enable_tpc)
103 		return 0;
104 	else if (txpwr >= 0)
105 		return min_t(s8, txpwr, 7);
106 	else
107 		return (txpwr < -16) ? 8 : (txpwr + 32) / 2;
108 }
109 
110 void mt76x02_tx_set_txpwr_auto(struct mt76x02_dev *dev, s8 txpwr)
111 {
112 	s8 txpwr_adj;
113 
114 	txpwr_adj = mt76x02_tx_get_txpwr_adj(dev, txpwr,
115 					     dev->rate_power.ofdm[4]);
116 	mt76_rmw_field(dev, MT_PROT_AUTO_TX_CFG,
117 		       MT_PROT_AUTO_TX_CFG_PROT_PADJ, txpwr_adj);
118 	mt76_rmw_field(dev, MT_PROT_AUTO_TX_CFG,
119 		       MT_PROT_AUTO_TX_CFG_AUTO_PADJ, txpwr_adj);
120 }
121 EXPORT_SYMBOL_GPL(mt76x02_tx_set_txpwr_auto);
122 
123 bool mt76x02_tx_status_data(struct mt76_dev *mdev, u8 *update)
124 {
125 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
126 	struct mt76x02_tx_status stat;
127 
128 	if (!mt76x02_mac_load_tx_status(dev, &stat))
129 		return false;
130 
131 	mt76x02_send_tx_status(dev, &stat, update);
132 
133 	return true;
134 }
135 EXPORT_SYMBOL_GPL(mt76x02_tx_status_data);
136 
137 int mt76x02_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
138 			   enum mt76_txq_id qid, struct mt76_wcid *wcid,
139 			   struct ieee80211_sta *sta,
140 			   struct mt76_tx_info *tx_info)
141 {
142 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
143 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx_info->skb->data;
144 	struct mt76x02_txwi *txwi = txwi_ptr;
145 	bool ampdu = IEEE80211_SKB_CB(tx_info->skb)->flags & IEEE80211_TX_CTL_AMPDU;
146 	int hdrlen, len, pid, qsel = MT_QSEL_EDCA;
147 
148 	if (qid == MT_TXQ_PSD && wcid && wcid->idx < 128)
149 		mt76x02_mac_wcid_set_drop(dev, wcid->idx, false);
150 
151 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
152 	len = tx_info->skb->len - (hdrlen & 2);
153 	mt76x02_mac_write_txwi(dev, txwi, tx_info->skb, wcid, sta, len);
154 
155 	pid = mt76_tx_status_skb_add(mdev, wcid, tx_info->skb);
156 
157 	/* encode packet rate for no-skb packet id to fix up status reporting */
158 	if (pid == MT_PACKET_ID_NO_SKB)
159 		pid = MT_PACKET_ID_HAS_RATE |
160 		      (le16_to_cpu(txwi->rate) & MT_RXWI_RATE_INDEX) |
161 		      FIELD_PREP(MT_PKTID_AC,
162 				 skb_get_queue_mapping(tx_info->skb));
163 
164 	txwi->pktid = pid;
165 
166 	if (mt76_is_skb_pktid(pid) && ampdu)
167 		qsel = MT_QSEL_MGMT;
168 
169 	tx_info->info = FIELD_PREP(MT_TXD_INFO_QSEL, qsel) |
170 			MT_TXD_INFO_80211;
171 
172 	if (!wcid || wcid->hw_key_idx == 0xff || wcid->sw_iv)
173 		tx_info->info |= MT_TXD_INFO_WIV;
174 
175 	if (sta) {
176 		struct mt76x02_sta *msta = (struct mt76x02_sta *)sta->drv_priv;
177 
178 		ewma_pktlen_add(&msta->pktlen, tx_info->skb->len);
179 	}
180 
181 	return 0;
182 }
183 EXPORT_SYMBOL_GPL(mt76x02_tx_prepare_skb);
184