1 // SPDX-License-Identifier: ISC
3 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4 * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
7 #include <linux/kernel.h>
11 void mt76x02_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
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;
20 struct mt76x02_sta *msta;
22 msta = (struct mt76x02_sta *)control->sta->drv_priv;
25 struct mt76x02_vif *mvif;
27 mvif = (struct mt76x02_vif *)vif->drv_priv;
28 wcid = &mvif->group_wcid;
31 mt76_tx(&dev->mt76, control->sta, wcid, skb);
33 EXPORT_SYMBOL_GPL(mt76x02_tx);
35 void mt76x02_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
38 struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
39 void *rxwi = skb->data;
41 if (q == MT_RXQ_MCU) {
42 /* this is used just by mmio code */
43 mt76_mcu_rx_event(&dev->mt76, skb);
47 skb_pull(skb, sizeof(struct mt76x02_rxwi));
48 if (mt76x02_mac_process_rx(dev, skb, rxwi)) {
53 mt76_rx(mdev, q, skb);
55 EXPORT_SYMBOL_GPL(mt76x02_queue_rx_skb);
57 s8 mt76x02_tx_get_max_txpwr_adj(struct mt76x02_dev *dev,
58 const struct ieee80211_tx_rate *rate)
62 if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
63 u8 mcs = ieee80211_rate_get_vht_mcs(rate);
65 if (mcs == 8 || mcs == 9) {
66 max_txpwr = dev->mt76.rate_power.vht[8];
70 nss = ieee80211_rate_get_vht_nss(rate);
71 idx = ((nss - 1) << 3) + mcs;
72 max_txpwr = dev->mt76.rate_power.ht[idx & 0xf];
74 } else if (rate->flags & IEEE80211_TX_RC_MCS) {
75 max_txpwr = dev->mt76.rate_power.ht[rate->idx & 0xf];
77 enum nl80211_band band = dev->mt76.chandef.chan->band;
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;
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];
88 max_txpwr = rp->ofdm[r->hw_value & 0x7];
90 max_txpwr = dev->mt76.rate_power.ofdm[rate->idx & 0x7];
97 s8 mt76x02_tx_get_txpwr_adj(struct mt76x02_dev *dev, s8 txpwr, s8 max_txpwr_adj)
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);
103 if (!dev->enable_tpc)
106 return min_t(s8, txpwr, 7);
108 return (txpwr < -16) ? 8 : (txpwr + 32) / 2;
111 void mt76x02_tx_set_txpwr_auto(struct mt76x02_dev *dev, s8 txpwr)
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);
122 EXPORT_SYMBOL_GPL(mt76x02_tx_set_txpwr_auto);
124 bool mt76x02_tx_status_data(struct mt76_dev *mdev, u8 *update)
126 struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
127 struct mt76x02_tx_status stat;
129 if (!mt76x02_mac_load_tx_status(dev, &stat))
132 mt76x02_send_tx_status(dev, &stat, update);
136 EXPORT_SYMBOL_GPL(mt76x02_tx_status_data);
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)
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;
149 if (qid == MT_TXQ_PSD && wcid && wcid->idx < 128)
150 mt76x02_mac_wcid_set_drop(dev, wcid->idx, false);
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);
156 pid = mt76_tx_status_skb_add(mdev, wcid, tx_info->skb);
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);
165 if (mt76_is_skb_pktid(pid) && ampdu)
168 tx_info->info = FIELD_PREP(MT_TXD_INFO_QSEL, qsel) |
171 if (!wcid || wcid->hw_key_idx == 0xff || wcid->sw_iv)
172 tx_info->info |= MT_TXD_INFO_WIV;
176 EXPORT_SYMBOL_GPL(mt76x02_tx_prepare_skb);