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 "mt76x2.h"
19 #include "mt76x2_dma.h"
20 
mt76x2_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)21 void mt76x2_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
22 	       struct sk_buff *skb)
23 {
24 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
25 	struct mt76x2_dev *dev = hw->priv;
26 	struct ieee80211_vif *vif = info->control.vif;
27 	struct mt76_wcid *wcid = &dev->global_wcid;
28 
29 	if (control->sta) {
30 		struct mt76x2_sta *msta;
31 
32 		msta = (struct mt76x2_sta *)control->sta->drv_priv;
33 		wcid = &msta->wcid;
34 		/* sw encrypted frames */
35 		if (!info->control.hw_key && wcid->hw_key_idx != -1)
36 			control->sta = NULL;
37 	}
38 
39 	if (vif && !control->sta) {
40 		struct mt76x2_vif *mvif;
41 
42 		mvif = (struct mt76x2_vif *)vif->drv_priv;
43 		wcid = &mvif->group_wcid;
44 	}
45 
46 	mt76_tx(&dev->mt76, control->sta, wcid, skb);
47 }
48 EXPORT_SYMBOL_GPL(mt76x2_tx);
49 
mt76x2_insert_hdr_pad(struct sk_buff * skb)50 int mt76x2_insert_hdr_pad(struct sk_buff *skb)
51 {
52 	int len = ieee80211_get_hdrlen_from_skb(skb);
53 
54 	if (len % 4 == 0)
55 		return 0;
56 
57 	skb_push(skb, 2);
58 	memmove(skb->data, skb->data + 2, len);
59 
60 	skb->data[len] = 0;
61 	skb->data[len + 1] = 0;
62 	return 2;
63 }
64 EXPORT_SYMBOL_GPL(mt76x2_insert_hdr_pad);
65 
mt76x2_tx_get_max_txpwr_adj(struct mt76x2_dev * dev,const struct ieee80211_tx_rate * rate)66 s8 mt76x2_tx_get_max_txpwr_adj(struct mt76x2_dev *dev,
67 			       const struct ieee80211_tx_rate *rate)
68 {
69 	s8 max_txpwr;
70 
71 	if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
72 		u8 mcs = ieee80211_rate_get_vht_mcs(rate);
73 
74 		if (mcs == 8 || mcs == 9) {
75 			max_txpwr = dev->rate_power.vht[8];
76 		} else {
77 			u8 nss, idx;
78 
79 			nss = ieee80211_rate_get_vht_nss(rate);
80 			idx = ((nss - 1) << 3) + mcs;
81 			max_txpwr = dev->rate_power.ht[idx & 0xf];
82 		}
83 	} else if (rate->flags & IEEE80211_TX_RC_MCS) {
84 		max_txpwr = dev->rate_power.ht[rate->idx & 0xf];
85 	} else {
86 		enum nl80211_band band = dev->mt76.chandef.chan->band;
87 
88 		if (band == NL80211_BAND_2GHZ) {
89 			const struct ieee80211_rate *r;
90 			struct wiphy *wiphy = mt76_hw(dev)->wiphy;
91 			struct mt76_rate_power *rp = &dev->rate_power;
92 
93 			r = &wiphy->bands[band]->bitrates[rate->idx];
94 			if (r->flags & IEEE80211_RATE_SHORT_PREAMBLE)
95 				max_txpwr = rp->cck[r->hw_value & 0x3];
96 			else
97 				max_txpwr = rp->ofdm[r->hw_value & 0x7];
98 		} else {
99 			max_txpwr = dev->rate_power.ofdm[rate->idx & 0x7];
100 		}
101 	}
102 
103 	return max_txpwr;
104 }
105 EXPORT_SYMBOL_GPL(mt76x2_tx_get_max_txpwr_adj);
106 
mt76x2_tx_get_txpwr_adj(struct mt76x2_dev * dev,s8 txpwr,s8 max_txpwr_adj)107 s8 mt76x2_tx_get_txpwr_adj(struct mt76x2_dev *dev, s8 txpwr, s8 max_txpwr_adj)
108 {
109 	txpwr = min_t(s8, txpwr, dev->txpower_conf);
110 	txpwr -= (dev->target_power + dev->target_power_delta[0]);
111 	txpwr = min_t(s8, txpwr, max_txpwr_adj);
112 
113 	if (!dev->enable_tpc)
114 		return 0;
115 	else if (txpwr >= 0)
116 		return min_t(s8, txpwr, 7);
117 	else
118 		return (txpwr < -16) ? 8 : (txpwr + 32) / 2;
119 }
120 EXPORT_SYMBOL_GPL(mt76x2_tx_get_txpwr_adj);
121 
mt76x2_tx_set_txpwr_auto(struct mt76x2_dev * dev,s8 txpwr)122 void mt76x2_tx_set_txpwr_auto(struct mt76x2_dev *dev, s8 txpwr)
123 {
124 	s8 txpwr_adj;
125 
126 	txpwr_adj = mt76x2_tx_get_txpwr_adj(dev, txpwr,
127 					    dev->rate_power.ofdm[4]);
128 	mt76_rmw_field(dev, MT_PROT_AUTO_TX_CFG,
129 		       MT_PROT_AUTO_TX_CFG_PROT_PADJ, txpwr_adj);
130 	mt76_rmw_field(dev, MT_PROT_AUTO_TX_CFG,
131 		       MT_PROT_AUTO_TX_CFG_AUTO_PADJ, txpwr_adj);
132 }
133 EXPORT_SYMBOL_GPL(mt76x2_tx_set_txpwr_auto);
134 
mt76x2_tx_complete(struct mt76x2_dev * dev,struct sk_buff * skb)135 void mt76x2_tx_complete(struct mt76x2_dev *dev, struct sk_buff *skb)
136 {
137 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
138 
139 	if (info->flags & IEEE80211_TX_CTL_AMPDU) {
140 		ieee80211_free_txskb(mt76_hw(dev), skb);
141 	} else {
142 		ieee80211_tx_info_clear_status(info);
143 		info->status.rates[0].idx = -1;
144 		info->flags |= IEEE80211_TX_STAT_ACK;
145 		ieee80211_tx_status(mt76_hw(dev), skb);
146 	}
147 }
148 EXPORT_SYMBOL_GPL(mt76x2_tx_complete);
149 
150