1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
11 MT_TXQ_VO = IEEE80211_AC_VO,
12 MT_TXQ_VI = IEEE80211_AC_VI,
13 MT_TXQ_BE = IEEE80211_AC_BE,
14 MT_TXQ_BK = IEEE80211_AC_BK,
20 /* Hardware uses mirrored order of queues with Q0 having the highest priority */
26 /* Take mac80211 Q id from the skb and translate it to hardware Q id */
27 static u8 skb2q(struct sk_buff *skb)
29 int qid = skb_get_queue_mapping(skb);
31 if (WARN_ON(qid >= MT_TXQ_PSD)) {
33 skb_set_queue_mapping(skb, qid);
39 /* Note: TX retry reporting is a bit broken.
40 * Retries are reported only once per AMPDU and often come a frame early
41 * i.e. they are reported in the last status preceding the AMPDU. Apart
42 * from the fact that it's hard to know the length of the AMPDU (which is
43 * required to know to how many consecutive frames retries should be
44 * applied), if status comes early on full FIFO it gets lost and retries
45 * of the whole AMPDU become invisible.
46 * As a work-around encode the desired rate in PKT_ID of TX descriptor
47 * and based on that guess the retries (every rate is tried once).
48 * Only downside here is that for MCS0 we have to rely solely on
49 * transmission failures as no retries can ever be reported.
50 * Not having to read EXT_FIFO has a nice effect of doubling the number
51 * of reports which can be fetched.
52 * Also the vendor driver never uses the EXT_FIFO register so it may be
55 static u8 mt7601u_tx_pktid_enc(struct mt7601u_dev *dev, u8 rate, bool is_probe)
57 u8 encoded = (rate + 1) + is_probe * 8;
59 /* Because PKT_ID 0 disables status reporting only 15 values are
60 * available but 16 are needed (8 MCS * 2 for encoding is_probe)
61 * - we need to cram together two rates. MCS0 and MCS7 with is_probe
64 if (is_probe && rate == 7)
71 mt7601u_tx_pktid_dec(struct mt7601u_dev *dev, struct mt76_tx_status *stat)
73 u8 req_rate = stat->pktid;
74 u8 eff_rate = stat->rate & 0x7;
79 stat->is_probe = true;
82 /* Decide between MCS0 and MCS7 which share pktid 9 */
83 if (!req_rate && eff_rate)
87 stat->retry = req_rate - eff_rate;
90 static void mt7601u_tx_skb_remove_dma_overhead(struct sk_buff *skb,
91 struct ieee80211_tx_info *info)
93 int pkt_len = (unsigned long)info->status.status_driver_data[0];
95 skb_pull(skb, sizeof(struct mt76_txwi) + 4);
96 if (ieee80211_get_hdrlen_from_skb(skb) % 4)
97 mt76_remove_hdr_pad(skb);
99 skb_trim(skb, pkt_len);
102 void mt7601u_tx_status(struct mt7601u_dev *dev, struct sk_buff *skb)
104 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
106 mt7601u_tx_skb_remove_dma_overhead(skb, info);
108 ieee80211_tx_info_clear_status(info);
109 info->status.rates[0].idx = -1;
110 info->flags |= IEEE80211_TX_STAT_ACK;
112 spin_lock_bh(&dev->mac_lock);
113 ieee80211_tx_status(dev->hw, skb);
114 spin_unlock_bh(&dev->mac_lock);
117 static int mt7601u_skb_rooms(struct mt7601u_dev *dev, struct sk_buff *skb)
119 int hdr_len = ieee80211_get_hdrlen_from_skb(skb);
122 need_head = sizeof(struct mt76_txwi) + 4;
126 return skb_cow(skb, need_head);
129 static struct mt76_txwi *
130 mt7601u_push_txwi(struct mt7601u_dev *dev, struct sk_buff *skb,
131 struct ieee80211_sta *sta, struct mt76_wcid *wcid,
134 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
135 struct ieee80211_tx_rate *rate = &info->control.rates[0];
136 struct mt76_txwi *txwi;
143 txwi = skb_push(skb, sizeof(struct mt76_txwi));
144 memset(txwi, 0, sizeof(*txwi));
146 if (!wcid->tx_rate_set)
147 ieee80211_get_tx_rates(info->control.vif, sta, skb,
148 info->control.rates, 1);
150 spin_lock_irqsave(&dev->lock, flags);
151 if (rate->idx < 0 || !rate->count)
152 rate_ctl = wcid->tx_rate;
154 rate_ctl = mt76_mac_tx_rate_val(dev, rate, &nss);
155 spin_unlock_irqrestore(&dev->lock, flags);
156 txwi->rate_ctl = cpu_to_le16(rate_ctl);
158 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
159 txwi->ack_ctl |= MT_TXWI_ACK_CTL_REQ;
160 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
161 txwi->ack_ctl |= MT_TXWI_ACK_CTL_NSEQ;
163 if ((info->flags & IEEE80211_TX_CTL_AMPDU) && sta) {
164 u8 ba_size = IEEE80211_MIN_AMPDU_BUF;
166 ba_size <<= sta->deflink.ht_cap.ampdu_factor;
167 ba_size = min_t(int, 63, ba_size);
168 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
170 txwi->ack_ctl |= FIELD_PREP(MT_TXWI_ACK_CTL_BA_WINDOW, ba_size);
173 cpu_to_le16(MT_TXWI_FLAGS_AMPDU |
174 FIELD_PREP(MT_TXWI_FLAGS_MPDU_DENSITY,
175 sta->deflink.ht_cap.ampdu_density));
176 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
180 txwi->wcid = wcid->idx;
182 is_probe = !!(info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE);
183 pkt_id = mt7601u_tx_pktid_enc(dev, rate_ctl & 0x7, is_probe);
184 pkt_len |= FIELD_PREP(MT_TXWI_LEN_PKTID, pkt_id);
185 txwi->len_ctl = cpu_to_le16(pkt_len);
190 void mt7601u_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
193 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
194 struct mt7601u_dev *dev = hw->priv;
195 struct ieee80211_vif *vif = info->control.vif;
196 struct ieee80211_sta *sta = control->sta;
197 struct mt76_sta *msta = NULL;
198 struct mt76_wcid *wcid = dev->mon_wcid;
199 struct mt76_txwi *txwi;
200 int pkt_len = skb->len;
201 int hw_q = skb2q(skb);
203 BUILD_BUG_ON(ARRAY_SIZE(info->status.status_driver_data) < 1);
204 info->status.status_driver_data[0] = (void *)(unsigned long)pkt_len;
206 if (mt7601u_skb_rooms(dev, skb) || mt76_insert_hdr_pad(skb)) {
207 ieee80211_free_txskb(dev->hw, skb);
212 msta = (struct mt76_sta *) sta->drv_priv;
215 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
217 wcid = &mvif->group_wcid;
220 txwi = mt7601u_push_txwi(dev, skb, sta, wcid, pkt_len);
222 if (mt7601u_dma_enqueue_tx(dev, skb, wcid, hw_q))
225 trace_mt_tx(dev, skb, msta, txwi);
228 void mt7601u_tx_stat(struct work_struct *work)
230 struct mt7601u_dev *dev = container_of(work, struct mt7601u_dev,
232 struct mt76_tx_status stat;
236 while (!test_bit(MT7601U_STATE_REMOVED, &dev->state)) {
237 stat = mt7601u_mac_fetch_tx_status(dev);
241 mt7601u_tx_pktid_dec(dev, &stat);
242 mt76_send_tx_status(dev, &stat);
246 trace_mt_tx_status_cleaned(dev, cleaned);
248 spin_lock_irqsave(&dev->tx_lock, flags);
250 queue_delayed_work(dev->stat_wq, &dev->stat_work,
251 msecs_to_jiffies(10));
252 else if (test_and_clear_bit(MT7601U_STATE_MORE_STATS, &dev->state))
253 queue_delayed_work(dev->stat_wq, &dev->stat_work,
254 msecs_to_jiffies(20));
256 clear_bit(MT7601U_STATE_READING_STATS, &dev->state);
257 spin_unlock_irqrestore(&dev->tx_lock, flags);
260 int mt7601u_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
261 unsigned int link_id, u16 queue,
262 const struct ieee80211_tx_queue_params *params)
264 struct mt7601u_dev *dev = hw->priv;
265 u8 cw_min = 5, cw_max = 10, hw_q = q2hwq(queue);
268 /* TODO: should we do funny things with the parameters?
269 * See what mt7601u_set_default_edca() used to do in init.c.
273 cw_min = fls(params->cw_min);
275 cw_max = fls(params->cw_max);
277 WARN_ON(params->txop > 0xff);
278 WARN_ON(params->aifs > 0xf);
279 WARN_ON(cw_min > 0xf);
280 WARN_ON(cw_max > 0xf);
282 val = FIELD_PREP(MT_EDCA_CFG_AIFSN, params->aifs) |
283 FIELD_PREP(MT_EDCA_CFG_CWMIN, cw_min) |
284 FIELD_PREP(MT_EDCA_CFG_CWMAX, cw_max);
285 /* TODO: based on user-controlled EnableTxBurst var vendor drv sets
286 * a really long txop on AC0 (see connect.c:2009) but only on
287 * connect? When not connected should be 0.
292 val |= FIELD_PREP(MT_EDCA_CFG_TXOP, params->txop);
293 mt76_wr(dev, MT_EDCA_CFG_AC(hw_q), val);
295 val = mt76_rr(dev, MT_WMM_TXOP(hw_q));
296 val &= ~(MT_WMM_TXOP_MASK << MT_WMM_TXOP_SHIFT(hw_q));
297 val |= params->txop << MT_WMM_TXOP_SHIFT(hw_q);
298 mt76_wr(dev, MT_WMM_TXOP(hw_q), val);
300 val = mt76_rr(dev, MT_WMM_AIFSN);
301 val &= ~(MT_WMM_AIFSN_MASK << MT_WMM_AIFSN_SHIFT(hw_q));
302 val |= params->aifs << MT_WMM_AIFSN_SHIFT(hw_q);
303 mt76_wr(dev, MT_WMM_AIFSN, val);
305 val = mt76_rr(dev, MT_WMM_CWMIN);
306 val &= ~(MT_WMM_CWMIN_MASK << MT_WMM_CWMIN_SHIFT(hw_q));
307 val |= cw_min << MT_WMM_CWMIN_SHIFT(hw_q);
308 mt76_wr(dev, MT_WMM_CWMIN, val);
310 val = mt76_rr(dev, MT_WMM_CWMAX);
311 val &= ~(MT_WMM_CWMAX_MASK << MT_WMM_CWMAX_SHIFT(hw_q));
312 val |= cw_max << MT_WMM_CWMAX_SHIFT(hw_q);
313 mt76_wr(dev, MT_WMM_CWMAX, val);