xref: /linux/drivers/net/wireless/mediatek/mt76/mt76x02_txrx.c (revision b7d3826c2ed6c3e626e7ae796c5df2c0d2551c6a)
1 /*
2  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
3  * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <linux/kernel.h>
19 
20 #include "mt76x02.h"
21 
22 void mt76x02_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
23 		struct sk_buff *skb)
24 {
25 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
26 	struct mt76x02_dev *dev = hw->priv;
27 	struct ieee80211_vif *vif = info->control.vif;
28 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
29 
30 	if (control->sta) {
31 		struct mt76x02_sta *msta;
32 
33 		msta = (struct mt76x02_sta *)control->sta->drv_priv;
34 		wcid = &msta->wcid;
35 		/* sw encrypted frames */
36 		if (!info->control.hw_key && wcid->hw_key_idx != 0xff)
37 			control->sta = NULL;
38 	}
39 
40 	if (vif && !control->sta) {
41 		struct mt76x02_vif *mvif;
42 
43 		mvif = (struct mt76x02_vif *)vif->drv_priv;
44 		wcid = &mvif->group_wcid;
45 	}
46 
47 	mt76_tx(&dev->mt76, control->sta, wcid, skb);
48 }
49 EXPORT_SYMBOL_GPL(mt76x02_tx);
50 
51 void mt76x02_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
52 			  struct sk_buff *skb)
53 {
54 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
55 	void *rxwi = skb->data;
56 
57 	if (q == MT_RXQ_MCU) {
58 		/* this is used just by mmio code */
59 		skb_queue_tail(&mdev->mmio.mcu.res_q, skb);
60 		wake_up(&mdev->mmio.mcu.wait);
61 		return;
62 	}
63 
64 	skb_pull(skb, sizeof(struct mt76x02_rxwi));
65 	if (mt76x02_mac_process_rx(dev, skb, rxwi)) {
66 		dev_kfree_skb(skb);
67 		return;
68 	}
69 
70 	mt76_rx(mdev, q, skb);
71 }
72 EXPORT_SYMBOL_GPL(mt76x02_queue_rx_skb);
73 
74 s8 mt76x02_tx_get_max_txpwr_adj(struct mt76_dev *dev,
75 				const struct ieee80211_tx_rate *rate)
76 {
77 	s8 max_txpwr;
78 
79 	if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
80 		u8 mcs = ieee80211_rate_get_vht_mcs(rate);
81 
82 		if (mcs == 8 || mcs == 9) {
83 			max_txpwr = dev->rate_power.vht[8];
84 		} else {
85 			u8 nss, idx;
86 
87 			nss = ieee80211_rate_get_vht_nss(rate);
88 			idx = ((nss - 1) << 3) + mcs;
89 			max_txpwr = dev->rate_power.ht[idx & 0xf];
90 		}
91 	} else if (rate->flags & IEEE80211_TX_RC_MCS) {
92 		max_txpwr = dev->rate_power.ht[rate->idx & 0xf];
93 	} else {
94 		enum nl80211_band band = dev->chandef.chan->band;
95 
96 		if (band == NL80211_BAND_2GHZ) {
97 			const struct ieee80211_rate *r;
98 			struct wiphy *wiphy = dev->hw->wiphy;
99 			struct mt76_rate_power *rp = &dev->rate_power;
100 
101 			r = &wiphy->bands[band]->bitrates[rate->idx];
102 			if (r->flags & IEEE80211_RATE_SHORT_PREAMBLE)
103 				max_txpwr = rp->cck[r->hw_value & 0x3];
104 			else
105 				max_txpwr = rp->ofdm[r->hw_value & 0x7];
106 		} else {
107 			max_txpwr = dev->rate_power.ofdm[rate->idx & 0x7];
108 		}
109 	}
110 
111 	return max_txpwr;
112 }
113 EXPORT_SYMBOL_GPL(mt76x02_tx_get_max_txpwr_adj);
114 
115 s8 mt76x02_tx_get_txpwr_adj(struct mt76_dev *mdev, s8 txpwr, s8 max_txpwr_adj)
116 {
117 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
118 
119 	txpwr = min_t(s8, txpwr, dev->mt76.txpower_conf);
120 	txpwr -= (dev->target_power + dev->target_power_delta[0]);
121 	txpwr = min_t(s8, txpwr, max_txpwr_adj);
122 
123 	if (!dev->enable_tpc)
124 		return 0;
125 	else if (txpwr >= 0)
126 		return min_t(s8, txpwr, 7);
127 	else
128 		return (txpwr < -16) ? 8 : (txpwr + 32) / 2;
129 }
130 EXPORT_SYMBOL_GPL(mt76x02_tx_get_txpwr_adj);
131 
132 void mt76x02_tx_set_txpwr_auto(struct mt76x02_dev *dev, s8 txpwr)
133 {
134 	s8 txpwr_adj;
135 
136 	txpwr_adj = mt76x02_tx_get_txpwr_adj(&dev->mt76, txpwr,
137 					     dev->mt76.rate_power.ofdm[4]);
138 	mt76_rmw_field(dev, MT_PROT_AUTO_TX_CFG,
139 		       MT_PROT_AUTO_TX_CFG_PROT_PADJ, txpwr_adj);
140 	mt76_rmw_field(dev, MT_PROT_AUTO_TX_CFG,
141 		       MT_PROT_AUTO_TX_CFG_AUTO_PADJ, txpwr_adj);
142 }
143 EXPORT_SYMBOL_GPL(mt76x02_tx_set_txpwr_auto);
144 
145 void mt76x02_tx_complete(struct mt76_dev *dev, struct sk_buff *skb)
146 {
147 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
148 
149 	if (info->flags & IEEE80211_TX_CTL_AMPDU) {
150 		ieee80211_free_txskb(dev->hw, skb);
151 	} else {
152 		ieee80211_tx_info_clear_status(info);
153 		info->status.rates[0].idx = -1;
154 		info->flags |= IEEE80211_TX_STAT_ACK;
155 		ieee80211_tx_status(dev->hw, skb);
156 	}
157 }
158 EXPORT_SYMBOL_GPL(mt76x02_tx_complete);
159 
160 bool mt76x02_tx_status_data(struct mt76_dev *dev, u8 *update)
161 {
162 	struct mt76x02_tx_status stat;
163 
164 	if (!mt76x02_mac_load_tx_status(dev, &stat))
165 		return false;
166 
167 	mt76x02_send_tx_status(dev, &stat, update);
168 
169 	return true;
170 }
171 EXPORT_SYMBOL_GPL(mt76x02_tx_status_data);
172 
173 int mt76x02_tx_prepare_skb(struct mt76_dev *mdev, void *txwi,
174 			   struct sk_buff *skb, struct mt76_queue *q,
175 			   struct mt76_wcid *wcid, struct ieee80211_sta *sta,
176 			   u32 *tx_info)
177 {
178 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
179 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
180 	int qsel = MT_QSEL_EDCA;
181 	int ret;
182 
183 	if (q == &dev->mt76.q_tx[MT_TXQ_PSD] && wcid && wcid->idx < 128)
184 		mt76x02_mac_wcid_set_drop(&dev->mt76, wcid->idx, false);
185 
186 	mt76x02_mac_write_txwi(mdev, txwi, skb, wcid, sta, skb->len);
187 
188 	ret = mt76x02_insert_hdr_pad(skb);
189 	if (ret < 0)
190 		return ret;
191 
192 	if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
193 		qsel = MT_QSEL_MGMT;
194 
195 	*tx_info = FIELD_PREP(MT_TXD_INFO_QSEL, qsel) |
196 		   MT_TXD_INFO_80211;
197 
198 	if (!wcid || wcid->hw_key_idx == 0xff || wcid->sw_iv)
199 		*tx_info |= MT_TXD_INFO_WIV;
200 
201 	return 0;
202 }
203 EXPORT_SYMBOL_GPL(mt76x02_tx_prepare_skb);
204