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 mt76x02_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->mt76.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->mt76.rate_power.ht[idx & 0xf]; 90 } 91 } else if (rate->flags & IEEE80211_TX_RC_MCS) { 92 max_txpwr = dev->mt76.rate_power.ht[rate->idx & 0xf]; 93 } else { 94 enum nl80211_band band = dev->mt76.chandef.chan->band; 95 96 if (band == NL80211_BAND_2GHZ) { 97 const struct ieee80211_rate *r; 98 struct wiphy *wiphy = dev->mt76.hw->wiphy; 99 struct mt76_rate_power *rp = &dev->mt76.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->mt76.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 mt76x02_dev *dev, s8 txpwr, s8 max_txpwr_adj) 116 { 117 txpwr = min_t(s8, txpwr, dev->mt76.txpower_conf); 118 txpwr -= (dev->target_power + dev->target_power_delta[0]); 119 txpwr = min_t(s8, txpwr, max_txpwr_adj); 120 121 if (!dev->enable_tpc) 122 return 0; 123 else if (txpwr >= 0) 124 return min_t(s8, txpwr, 7); 125 else 126 return (txpwr < -16) ? 8 : (txpwr + 32) / 2; 127 } 128 EXPORT_SYMBOL_GPL(mt76x02_tx_get_txpwr_adj); 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 void mt76x02_tx_complete(struct mt76_dev *dev, struct sk_buff *skb) 144 { 145 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 146 147 if (info->flags & IEEE80211_TX_CTL_AMPDU) { 148 ieee80211_free_txskb(dev->hw, skb); 149 } else { 150 ieee80211_tx_info_clear_status(info); 151 info->status.rates[0].idx = -1; 152 info->flags |= IEEE80211_TX_STAT_ACK; 153 ieee80211_tx_status(dev->hw, skb); 154 } 155 } 156 EXPORT_SYMBOL_GPL(mt76x02_tx_complete); 157 158 bool mt76x02_tx_status_data(struct mt76_dev *mdev, u8 *update) 159 { 160 struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76); 161 struct mt76x02_tx_status stat; 162 163 if (!mt76x02_mac_load_tx_status(dev, &stat)) 164 return false; 165 166 mt76x02_send_tx_status(dev, &stat, update); 167 168 return true; 169 } 170 EXPORT_SYMBOL_GPL(mt76x02_tx_status_data); 171 172 int mt76x02_tx_prepare_skb(struct mt76_dev *mdev, void *txwi, 173 struct sk_buff *skb, struct mt76_queue *q, 174 struct mt76_wcid *wcid, struct ieee80211_sta *sta, 175 u32 *tx_info) 176 { 177 struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76); 178 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 179 int qsel = MT_QSEL_EDCA; 180 int ret; 181 182 if (q == &dev->mt76.q_tx[MT_TXQ_PSD] && wcid && wcid->idx < 128) 183 mt76x02_mac_wcid_set_drop(dev, wcid->idx, false); 184 185 mt76x02_mac_write_txwi(dev, txwi, skb, wcid, sta, skb->len); 186 187 ret = mt76x02_insert_hdr_pad(skb); 188 if (ret < 0) 189 return ret; 190 191 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) 192 qsel = MT_QSEL_MGMT; 193 194 *tx_info = FIELD_PREP(MT_TXD_INFO_QSEL, qsel) | 195 MT_TXD_INFO_80211; 196 197 if (!wcid || wcid->hw_key_idx == 0xff || wcid->sw_iv) 198 *tx_info |= MT_TXD_INFO_WIV; 199 200 return 0; 201 } 202 EXPORT_SYMBOL_GPL(mt76x02_tx_prepare_skb); 203