2 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
18 /* Take mac80211 Q id from the skb and translate it to hardware Q id */
19 static u8 skb2q(struct sk_buff *skb)
21 int qid = skb_get_queue_mapping(skb);
23 if (WARN_ON(qid >= MT_TXQ_PSD)) {
25 skb_set_queue_mapping(skb, qid);
31 static void mt76x0_tx_skb_remove_dma_overhead(struct sk_buff *skb,
32 struct ieee80211_tx_info *info)
34 int pkt_len = (unsigned long)info->status.status_driver_data[0];
36 skb_pull(skb, sizeof(struct mt76_txwi) + 4);
37 if (ieee80211_get_hdrlen_from_skb(skb) % 4)
38 mt76x0_remove_hdr_pad(skb);
40 skb_trim(skb, pkt_len);
43 void mt76x0_tx_status(struct mt76x0_dev *dev, struct sk_buff *skb)
45 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
47 mt76x0_tx_skb_remove_dma_overhead(skb, info);
49 ieee80211_tx_info_clear_status(info);
50 info->status.rates[0].idx = -1;
51 info->flags |= IEEE80211_TX_STAT_ACK;
53 spin_lock(&dev->mac_lock);
54 ieee80211_tx_status(dev->mt76.hw, skb);
55 spin_unlock(&dev->mac_lock);
58 static int mt76x0_skb_rooms(struct mt76x0_dev *dev, struct sk_buff *skb)
60 int hdr_len = ieee80211_get_hdrlen_from_skb(skb);
63 need_head = sizeof(struct mt76_txwi) + 4;
67 return skb_cow(skb, need_head);
70 static struct mt76_txwi *
71 mt76x0_push_txwi(struct mt76x0_dev *dev, struct sk_buff *skb,
72 struct ieee80211_sta *sta, struct mt76_wcid *wcid,
75 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
76 struct ieee80211_tx_rate *rate = &info->control.rates[0];
77 struct mt76_txwi *txwi;
84 txwi = (struct mt76_txwi *)skb_push(skb, sizeof(struct mt76_txwi));
85 memset(txwi, 0, sizeof(*txwi));
87 if (!wcid->tx_rate_set)
88 ieee80211_get_tx_rates(info->control.vif, sta, skb,
89 info->control.rates, 1);
91 spin_lock_irqsave(&dev->mt76.lock, flags);
92 if (rate->idx < 0 || !rate->count) {
93 rate_ctl = wcid->tx_rate;
94 nss = wcid->tx_rate_nss;
96 rate_ctl = mt76x0_mac_tx_rate_val(dev, rate, &nss);
98 spin_unlock_irqrestore(&dev->mt76.lock, flags);
100 txwi->rate_ctl = cpu_to_le16(rate_ctl);
102 if (info->flags & IEEE80211_TX_CTL_LDPC)
103 txwi->rate_ctl |= cpu_to_le16(MT_RXWI_RATE_LDPC);
104 if ((info->flags & IEEE80211_TX_CTL_STBC) && nss == 1)
105 txwi->rate_ctl |= cpu_to_le16(MT_RXWI_RATE_STBC);
106 if (nss > 1 && sta && sta->smps_mode == IEEE80211_SMPS_DYNAMIC)
107 txwi_flags |= MT_TXWI_FLAGS_MMPS;
109 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
110 txwi->ack_ctl |= MT_TXWI_ACK_CTL_REQ;
116 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
117 pkt_id |= MT_TXWI_PKTID_PROBE;
119 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
120 txwi->ack_ctl |= MT_TXWI_ACK_CTL_NSEQ;
122 if ((info->flags & IEEE80211_TX_CTL_AMPDU) && sta) {
123 u8 ba_size = IEEE80211_MIN_AMPDU_BUF;
125 ba_size <<= sta->ht_cap.ampdu_factor;
126 ba_size = min_t(int, 7, ba_size - 1);
127 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) {
130 txwi_flags |= MT_TXWI_FLAGS_AMPDU;
131 txwi_flags |= FIELD_PREP(MT_TXWI_FLAGS_MPDU_DENSITY,
132 sta->ht_cap.ampdu_density);
134 txwi->ack_ctl |= FIELD_PREP(MT_TXWI_ACK_CTL_BA_WINDOW, ba_size);
137 txwi->wcid = wcid->idx;
138 txwi->flags |= cpu_to_le16(txwi_flags);
139 txwi->len_ctl = cpu_to_le16(pkt_len);
140 txwi->pktid = pkt_id;
145 void mt76x0_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
148 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
149 struct mt76x0_dev *dev = hw->priv;
150 struct ieee80211_vif *vif = info->control.vif;
151 struct ieee80211_sta *sta = control->sta;
152 struct mt76_sta *msta = NULL;
153 struct mt76_wcid *wcid = dev->mon_wcid;
154 struct mt76_txwi *txwi;
155 int pkt_len = skb->len;
156 int hw_q = skb2q(skb);
158 BUILD_BUG_ON(ARRAY_SIZE(info->status.status_driver_data) < 1);
159 info->status.status_driver_data[0] = (void *)(unsigned long)pkt_len;
161 if (mt76x0_skb_rooms(dev, skb) || mt76x0_insert_hdr_pad(skb)) {
162 ieee80211_free_txskb(dev->mt76.hw, skb);
167 msta = (struct mt76_sta *) sta->drv_priv;
169 } else if (vif && (!info->control.hw_key && wcid->hw_key_idx != 0xff)) {
170 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
172 wcid = &mvif->group_wcid;
175 txwi = mt76x0_push_txwi(dev, skb, sta, wcid, pkt_len);
177 if (mt76x0_dma_enqueue_tx(dev, skb, wcid, hw_q))
180 trace_mt76x0_tx(&dev->mt76, skb, msta, txwi);
183 void mt76x0_tx_stat(struct work_struct *work)
185 struct mt76x0_dev *dev = container_of(work, struct mt76x0_dev,
187 struct mt76_tx_status stat;
192 while (!test_bit(MT76_REMOVED, &dev->mt76.state)) {
193 stat = mt76x0_mac_fetch_tx_status(dev);
197 mt76x0_send_tx_status(dev, &stat, &update);
201 trace_mt76x0_tx_status_cleaned(&dev->mt76, cleaned);
203 spin_lock_irqsave(&dev->tx_lock, flags);
205 queue_delayed_work(dev->stat_wq, &dev->stat_work,
206 msecs_to_jiffies(10));
207 else if (test_and_clear_bit(MT76_MORE_STATS, &dev->mt76.state))
208 queue_delayed_work(dev->stat_wq, &dev->stat_work,
209 msecs_to_jiffies(20));
211 clear_bit(MT76_READING_STATS, &dev->mt76.state);
212 spin_unlock_irqrestore(&dev->tx_lock, flags);
215 int mt76x0_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
216 u16 queue, const struct ieee80211_tx_queue_params *params)
218 struct mt76x0_dev *dev = hw->priv;
219 u8 cw_min = 5, cw_max = 10, hw_q = q2hwq(queue);
222 /* TODO: should we do funny things with the parameters?
223 * See what mt76x0_set_default_edca() used to do in init.c.
227 cw_min = fls(params->cw_min);
229 cw_max = fls(params->cw_max);
231 WARN_ON(params->txop > 0xff);
232 WARN_ON(params->aifs > 0xf);
233 WARN_ON(cw_min > 0xf);
234 WARN_ON(cw_max > 0xf);
236 val = FIELD_PREP(MT_EDCA_CFG_AIFSN, params->aifs) |
237 FIELD_PREP(MT_EDCA_CFG_CWMIN, cw_min) |
238 FIELD_PREP(MT_EDCA_CFG_CWMAX, cw_max);
239 /* TODO: based on user-controlled EnableTxBurst var vendor drv sets
240 * a really long txop on AC0 (see connect.c:2009) but only on
241 * connect? When not connected should be 0.
246 val |= FIELD_PREP(MT_EDCA_CFG_TXOP, params->txop);
247 mt76_wr(dev, MT_EDCA_CFG_AC(hw_q), val);
249 val = mt76_rr(dev, MT_WMM_TXOP(hw_q));
250 val &= ~(MT_WMM_TXOP_MASK << MT_WMM_TXOP_SHIFT(hw_q));
251 val |= params->txop << MT_WMM_TXOP_SHIFT(hw_q);
252 mt76_wr(dev, MT_WMM_TXOP(hw_q), val);
254 val = mt76_rr(dev, MT_WMM_AIFSN);
255 val &= ~(MT_WMM_AIFSN_MASK << MT_WMM_AIFSN_SHIFT(hw_q));
256 val |= params->aifs << MT_WMM_AIFSN_SHIFT(hw_q);
257 mt76_wr(dev, MT_WMM_AIFSN, val);
259 val = mt76_rr(dev, MT_WMM_CWMIN);
260 val &= ~(MT_WMM_CWMIN_MASK << MT_WMM_CWMIN_SHIFT(hw_q));
261 val |= cw_min << MT_WMM_CWMIN_SHIFT(hw_q);
262 mt76_wr(dev, MT_WMM_CWMIN, val);
264 val = mt76_rr(dev, MT_WMM_CWMAX);
265 val &= ~(MT_WMM_CWMAX_MASK << MT_WMM_CWMAX_SHIFT(hw_q));
266 val |= cw_max << MT_WMM_CWMAX_SHIFT(hw_q);
267 mt76_wr(dev, MT_WMM_CWMAX, val);