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_hdr *hdr = (struct ieee80211_hdr *) skb->data;
26 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
27 	struct mt76x02_dev *dev = hw->priv;
28 	struct ieee80211_vif *vif = info->control.vif;
29 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
30 
31 	if (control->sta) {
32 		struct mt76x02_sta *msta;
33 
34 		msta = (struct mt76x02_sta *)control->sta->drv_priv;
35 		wcid = &msta->wcid;
36 		/* sw encrypted frames */
37 		if (!info->control.hw_key && wcid->hw_key_idx != 0xff &&
38 		    ieee80211_has_protected(hdr->frame_control))
39 			control->sta = NULL;
40 	}
41 
42 	if (vif && !control->sta) {
43 		struct mt76x02_vif *mvif;
44 
45 		mvif = (struct mt76x02_vif *)vif->drv_priv;
46 		wcid = &mvif->group_wcid;
47 	}
48 
49 	mt76_tx(&dev->mt76, control->sta, wcid, skb);
50 }
51 EXPORT_SYMBOL_GPL(mt76x02_tx);
52 
53 void mt76x02_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
54 			  struct sk_buff *skb)
55 {
56 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
57 	void *rxwi = skb->data;
58 
59 	if (q == MT_RXQ_MCU) {
60 		/* this is used just by mmio code */
61 		skb_queue_tail(&mdev->mmio.mcu.res_q, skb);
62 		wake_up(&mdev->mmio.mcu.wait);
63 		return;
64 	}
65 
66 	skb_pull(skb, sizeof(struct mt76x02_rxwi));
67 	if (mt76x02_mac_process_rx(dev, skb, rxwi)) {
68 		dev_kfree_skb(skb);
69 		return;
70 	}
71 
72 	mt76_rx(mdev, q, skb);
73 }
74 EXPORT_SYMBOL_GPL(mt76x02_queue_rx_skb);
75 
76 s8 mt76x02_tx_get_max_txpwr_adj(struct mt76x02_dev *dev,
77 				const struct ieee80211_tx_rate *rate)
78 {
79 	s8 max_txpwr;
80 
81 	if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
82 		u8 mcs = ieee80211_rate_get_vht_mcs(rate);
83 
84 		if (mcs == 8 || mcs == 9) {
85 			max_txpwr = dev->mt76.rate_power.vht[8];
86 		} else {
87 			u8 nss, idx;
88 
89 			nss = ieee80211_rate_get_vht_nss(rate);
90 			idx = ((nss - 1) << 3) + mcs;
91 			max_txpwr = dev->mt76.rate_power.ht[idx & 0xf];
92 		}
93 	} else if (rate->flags & IEEE80211_TX_RC_MCS) {
94 		max_txpwr = dev->mt76.rate_power.ht[rate->idx & 0xf];
95 	} else {
96 		enum nl80211_band band = dev->mt76.chandef.chan->band;
97 
98 		if (band == NL80211_BAND_2GHZ) {
99 			const struct ieee80211_rate *r;
100 			struct wiphy *wiphy = dev->mt76.hw->wiphy;
101 			struct mt76_rate_power *rp = &dev->mt76.rate_power;
102 
103 			r = &wiphy->bands[band]->bitrates[rate->idx];
104 			if (r->flags & IEEE80211_RATE_SHORT_PREAMBLE)
105 				max_txpwr = rp->cck[r->hw_value & 0x3];
106 			else
107 				max_txpwr = rp->ofdm[r->hw_value & 0x7];
108 		} else {
109 			max_txpwr = dev->mt76.rate_power.ofdm[rate->idx & 0x7];
110 		}
111 	}
112 
113 	return max_txpwr;
114 }
115 
116 s8 mt76x02_tx_get_txpwr_adj(struct mt76x02_dev *dev, s8 txpwr, s8 max_txpwr_adj)
117 {
118 	txpwr = min_t(s8, txpwr, dev->mt76.txpower_conf);
119 	txpwr -= (dev->target_power + dev->target_power_delta[0]);
120 	txpwr = min_t(s8, txpwr, max_txpwr_adj);
121 
122 	if (!dev->enable_tpc)
123 		return 0;
124 	else if (txpwr >= 0)
125 		return min_t(s8, txpwr, 7);
126 	else
127 		return (txpwr < -16) ? 8 : (txpwr + 32) / 2;
128 }
129 
130 void mt76x02_tx_set_txpwr_auto(struct mt76x02_dev *dev, s8 txpwr)
131 {
132 	s8 txpwr_adj;
133 
134 	txpwr_adj = mt76x02_tx_get_txpwr_adj(dev, txpwr,
135 					     dev->mt76.rate_power.ofdm[4]);
136 	mt76_rmw_field(dev, MT_PROT_AUTO_TX_CFG,
137 		       MT_PROT_AUTO_TX_CFG_PROT_PADJ, txpwr_adj);
138 	mt76_rmw_field(dev, MT_PROT_AUTO_TX_CFG,
139 		       MT_PROT_AUTO_TX_CFG_AUTO_PADJ, txpwr_adj);
140 }
141 EXPORT_SYMBOL_GPL(mt76x02_tx_set_txpwr_auto);
142 
143 bool mt76x02_tx_status_data(struct mt76_dev *mdev, u8 *update)
144 {
145 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
146 	struct mt76x02_tx_status stat;
147 
148 	if (!mt76x02_mac_load_tx_status(dev, &stat))
149 		return false;
150 
151 	mt76x02_send_tx_status(dev, &stat, update);
152 
153 	return true;
154 }
155 EXPORT_SYMBOL_GPL(mt76x02_tx_status_data);
156 
157 int mt76x02_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
158 			   struct sk_buff *skb, struct mt76_queue *q,
159 			   struct mt76_wcid *wcid, struct ieee80211_sta *sta,
160 			   u32 *tx_info)
161 {
162 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
163 	struct mt76x02_txwi *txwi = txwi_ptr;
164 	int qsel = MT_QSEL_EDCA;
165 	int pid;
166 	int ret;
167 
168 	if (q == &dev->mt76.q_tx[MT_TXQ_PSD] && wcid && wcid->idx < 128)
169 		mt76x02_mac_wcid_set_drop(dev, wcid->idx, false);
170 
171 	mt76x02_mac_write_txwi(dev, txwi, skb, wcid, sta, skb->len);
172 
173 	pid = mt76_tx_status_skb_add(mdev, wcid, skb);
174 	txwi->pktid = pid;
175 
176 	ret = mt76x02_insert_hdr_pad(skb);
177 	if (ret < 0)
178 		return ret;
179 
180 	if (pid && pid != MT_PACKET_ID_NO_ACK)
181 		qsel = MT_QSEL_MGMT;
182 
183 	*tx_info = FIELD_PREP(MT_TXD_INFO_QSEL, qsel) |
184 		   MT_TXD_INFO_80211;
185 
186 	if (!wcid || wcid->hw_key_idx == 0xff || wcid->sw_iv)
187 		*tx_info |= MT_TXD_INFO_WIV;
188 
189 	return 0;
190 }
191 EXPORT_SYMBOL_GPL(mt76x02_tx_prepare_skb);
192