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
mt76x02_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)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->mt76, control->sta, wcid, skb);
32 }
33 EXPORT_SYMBOL_GPL(mt76x02_tx);
34
mt76x02_queue_rx_skb(struct mt76_dev * mdev,enum mt76_rxq_id q,struct sk_buff * skb)35 void mt76x02_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
36 struct sk_buff *skb)
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 /* this is used just by mmio code */
43 mt76_mcu_rx_event(&dev->mt76, skb);
44 return;
45 }
46
47 skb_pull(skb, sizeof(struct mt76x02_rxwi));
48 if (mt76x02_mac_process_rx(dev, skb, rxwi)) {
49 dev_kfree_skb(skb);
50 return;
51 }
52
53 mt76_rx(mdev, q, skb);
54 }
55 EXPORT_SYMBOL_GPL(mt76x02_queue_rx_skb);
56
mt76x02_tx_get_max_txpwr_adj(struct mt76x02_dev * dev,const struct ieee80211_tx_rate * rate)57 s8 mt76x02_tx_get_max_txpwr_adj(struct mt76x02_dev *dev,
58 const struct ieee80211_tx_rate *rate)
59 {
60 s8 max_txpwr;
61
62 if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
63 u8 mcs = ieee80211_rate_get_vht_mcs(rate);
64
65 if (mcs == 8 || mcs == 9) {
66 max_txpwr = dev->mt76.rate_power.vht[8];
67 } else {
68 u8 nss, idx;
69
70 nss = ieee80211_rate_get_vht_nss(rate);
71 idx = ((nss - 1) << 3) + mcs;
72 max_txpwr = dev->mt76.rate_power.ht[idx & 0xf];
73 }
74 } else if (rate->flags & IEEE80211_TX_RC_MCS) {
75 max_txpwr = dev->mt76.rate_power.ht[rate->idx & 0xf];
76 } else {
77 enum nl80211_band band = dev->mt76.chandef.chan->band;
78
79 if (band == NL80211_BAND_2GHZ) {
80 const struct ieee80211_rate *r;
81 struct wiphy *wiphy = dev->mt76.hw->wiphy;
82 struct mt76_rate_power *rp = &dev->mt76.rate_power;
83
84 r = &wiphy->bands[band]->bitrates[rate->idx];
85 if (r->flags & IEEE80211_RATE_SHORT_PREAMBLE)
86 max_txpwr = rp->cck[r->hw_value & 0x3];
87 else
88 max_txpwr = rp->ofdm[r->hw_value & 0x7];
89 } else {
90 max_txpwr = dev->mt76.rate_power.ofdm[rate->idx & 0x7];
91 }
92 }
93
94 return max_txpwr;
95 }
96
mt76x02_tx_get_txpwr_adj(struct mt76x02_dev * dev,s8 txpwr,s8 max_txpwr_adj)97 s8 mt76x02_tx_get_txpwr_adj(struct mt76x02_dev *dev, s8 txpwr, s8 max_txpwr_adj)
98 {
99 txpwr = min_t(s8, txpwr, dev->mt76.txpower_conf);
100 txpwr -= (dev->target_power + dev->target_power_delta[0]);
101 txpwr = min_t(s8, txpwr, max_txpwr_adj);
102
103 if (!dev->enable_tpc)
104 return 0;
105 else if (txpwr >= 0)
106 return min_t(s8, txpwr, 7);
107 else
108 return (txpwr < -16) ? 8 : (txpwr + 32) / 2;
109 }
110
mt76x02_tx_set_txpwr_auto(struct mt76x02_dev * dev,s8 txpwr)111 void mt76x02_tx_set_txpwr_auto(struct mt76x02_dev *dev, s8 txpwr)
112 {
113 s8 txpwr_adj;
114
115 txpwr_adj = mt76x02_tx_get_txpwr_adj(dev, txpwr,
116 dev->mt76.rate_power.ofdm[4]);
117 mt76_rmw_field(dev, MT_PROT_AUTO_TX_CFG,
118 MT_PROT_AUTO_TX_CFG_PROT_PADJ, txpwr_adj);
119 mt76_rmw_field(dev, MT_PROT_AUTO_TX_CFG,
120 MT_PROT_AUTO_TX_CFG_AUTO_PADJ, txpwr_adj);
121 }
122 EXPORT_SYMBOL_GPL(mt76x02_tx_set_txpwr_auto);
123
mt76x02_tx_status_data(struct mt76_dev * mdev,u8 * update)124 bool mt76x02_tx_status_data(struct mt76_dev *mdev, u8 *update)
125 {
126 struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
127 struct mt76x02_tx_status stat;
128
129 if (!mt76x02_mac_load_tx_status(dev, &stat))
130 return false;
131
132 mt76x02_send_tx_status(dev, &stat, update);
133
134 return true;
135 }
136 EXPORT_SYMBOL_GPL(mt76x02_tx_status_data);
137
mt76x02_tx_prepare_skb(struct mt76_dev * mdev,void * txwi_ptr,enum mt76_txq_id qid,struct mt76_wcid * wcid,struct ieee80211_sta * sta,struct mt76_tx_info * tx_info)138 int mt76x02_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
139 enum mt76_txq_id qid, struct mt76_wcid *wcid,
140 struct ieee80211_sta *sta,
141 struct mt76_tx_info *tx_info)
142 {
143 struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
144 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx_info->skb->data;
145 struct mt76x02_txwi *txwi = txwi_ptr;
146 bool ampdu = IEEE80211_SKB_CB(tx_info->skb)->flags & IEEE80211_TX_CTL_AMPDU;
147 int hdrlen, len, pid, qsel = MT_QSEL_EDCA;
148
149 if (qid == MT_TXQ_PSD && wcid && wcid->idx < 128)
150 mt76x02_mac_wcid_set_drop(dev, wcid->idx, false);
151
152 hdrlen = ieee80211_hdrlen(hdr->frame_control);
153 len = tx_info->skb->len - (hdrlen & 2);
154 mt76x02_mac_write_txwi(dev, txwi, tx_info->skb, wcid, sta, len);
155
156 pid = mt76_tx_status_skb_add(mdev, wcid, tx_info->skb);
157
158 /* encode packet rate for no-skb packet id to fix up status reporting */
159 if (pid == MT_PACKET_ID_NO_SKB)
160 pid = MT_PACKET_ID_HAS_RATE |
161 (le16_to_cpu(txwi->rate) & MT_RXWI_RATE_INDEX);
162
163 txwi->pktid = pid;
164
165 if (mt76_is_skb_pktid(pid) && ampdu)
166 qsel = MT_QSEL_MGMT;
167
168 tx_info->info = FIELD_PREP(MT_TXD_INFO_QSEL, qsel) |
169 MT_TXD_INFO_80211;
170
171 if (!wcid || wcid->hw_key_idx == 0xff || wcid->sw_iv)
172 tx_info->info |= MT_TXD_INFO_WIV;
173
174 return 0;
175 }
176 EXPORT_SYMBOL_GPL(mt76x02_tx_prepare_skb);
177