GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / net / wireless / mediatek / mt76 / tx.c
1 /*
2  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include "mt76.h"
18
19 static struct mt76_txwi_cache *
20 mt76_alloc_txwi(struct mt76_dev *dev)
21 {
22         struct mt76_txwi_cache *t;
23         dma_addr_t addr;
24         int size;
25
26         size = (sizeof(*t) + L1_CACHE_BYTES - 1) & ~(L1_CACHE_BYTES - 1);
27         t = devm_kzalloc(dev->dev, size, GFP_ATOMIC);
28         if (!t)
29                 return NULL;
30
31         addr = dma_map_single(dev->dev, &t->txwi, sizeof(t->txwi),
32                               DMA_TO_DEVICE);
33         t->dma_addr = addr;
34
35         return t;
36 }
37
38 static struct mt76_txwi_cache *
39 __mt76_get_txwi(struct mt76_dev *dev)
40 {
41         struct mt76_txwi_cache *t = NULL;
42
43         spin_lock_bh(&dev->lock);
44         if (!list_empty(&dev->txwi_cache)) {
45                 t = list_first_entry(&dev->txwi_cache, struct mt76_txwi_cache,
46                                      list);
47                 list_del(&t->list);
48         }
49         spin_unlock_bh(&dev->lock);
50
51         return t;
52 }
53
54 struct mt76_txwi_cache *
55 mt76_get_txwi(struct mt76_dev *dev)
56 {
57         struct mt76_txwi_cache *t = __mt76_get_txwi(dev);
58
59         if (t)
60                 return t;
61
62         return mt76_alloc_txwi(dev);
63 }
64
65 void
66 mt76_put_txwi(struct mt76_dev *dev, struct mt76_txwi_cache *t)
67 {
68         if (!t)
69                 return;
70
71         spin_lock_bh(&dev->lock);
72         list_add(&t->list, &dev->txwi_cache);
73         spin_unlock_bh(&dev->lock);
74 }
75
76 void mt76_tx_free(struct mt76_dev *dev)
77 {
78         struct mt76_txwi_cache *t;
79
80         while ((t = __mt76_get_txwi(dev)) != NULL)
81                 dma_unmap_single(dev->dev, t->dma_addr, sizeof(t->txwi),
82                                  DMA_TO_DEVICE);
83 }
84
85 static int
86 mt76_txq_get_qid(struct ieee80211_txq *txq)
87 {
88         if (!txq->sta)
89                 return MT_TXQ_BE;
90
91         return txq->ac;
92 }
93
94 void
95 mt76_tx(struct mt76_dev *dev, struct ieee80211_sta *sta,
96         struct mt76_wcid *wcid, struct sk_buff *skb)
97 {
98         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
99         struct mt76_queue *q;
100         int qid = skb_get_queue_mapping(skb);
101
102         if (WARN_ON(qid >= MT_TXQ_PSD)) {
103                 qid = MT_TXQ_BE;
104                 skb_set_queue_mapping(skb, qid);
105         }
106
107         if (!wcid->tx_rate_set)
108                 ieee80211_get_tx_rates(info->control.vif, sta, skb,
109                                        info->control.rates, 1);
110
111         q = &dev->q_tx[qid];
112
113         spin_lock_bh(&q->lock);
114         dev->queue_ops->tx_queue_skb(dev, q, skb, wcid, sta);
115         dev->queue_ops->kick(dev, q);
116
117         if (q->queued > q->ndesc - 8)
118                 ieee80211_stop_queue(dev->hw, skb_get_queue_mapping(skb));
119         spin_unlock_bh(&q->lock);
120 }
121 EXPORT_SYMBOL_GPL(mt76_tx);
122
123 static struct sk_buff *
124 mt76_txq_dequeue(struct mt76_dev *dev, struct mt76_txq *mtxq, bool ps)
125 {
126         struct ieee80211_txq *txq = mtxq_to_txq(mtxq);
127         struct sk_buff *skb;
128
129         skb = skb_dequeue(&mtxq->retry_q);
130         if (skb) {
131                 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
132
133                 if (ps && skb_queue_empty(&mtxq->retry_q))
134                         ieee80211_sta_set_buffered(txq->sta, tid, false);
135
136                 return skb;
137         }
138
139         skb = ieee80211_tx_dequeue(dev->hw, txq);
140         if (!skb)
141                 return NULL;
142
143         return skb;
144 }
145
146 static void
147 mt76_check_agg_ssn(struct mt76_txq *mtxq, struct sk_buff *skb)
148 {
149         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
150
151         if (!ieee80211_is_data_qos(hdr->frame_control) ||
152             !ieee80211_is_data_present(hdr->frame_control))
153                 return;
154
155         mtxq->agg_ssn = le16_to_cpu(hdr->seq_ctrl) + 0x10;
156 }
157
158 static void
159 mt76_queue_ps_skb(struct mt76_dev *dev, struct ieee80211_sta *sta,
160                   struct sk_buff *skb, bool last)
161 {
162         struct mt76_wcid *wcid = (struct mt76_wcid *) sta->drv_priv;
163         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
164         struct mt76_queue *hwq = &dev->q_tx[MT_TXQ_PSD];
165
166         info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
167         if (last)
168                 info->flags |= IEEE80211_TX_STATUS_EOSP;
169
170         mt76_skb_set_moredata(skb, !last);
171         dev->queue_ops->tx_queue_skb(dev, hwq, skb, wcid, sta);
172 }
173
174 void
175 mt76_release_buffered_frames(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
176                              u16 tids, int nframes,
177                              enum ieee80211_frame_release_type reason,
178                              bool more_data)
179 {
180         struct mt76_dev *dev = hw->priv;
181         struct sk_buff *last_skb = NULL;
182         struct mt76_queue *hwq = &dev->q_tx[MT_TXQ_PSD];
183         int i;
184
185         spin_lock_bh(&hwq->lock);
186         for (i = 0; tids && nframes; i++, tids >>= 1) {
187                 struct ieee80211_txq *txq = sta->txq[i];
188                 struct mt76_txq *mtxq = (struct mt76_txq *) txq->drv_priv;
189                 struct sk_buff *skb;
190
191                 if (!(tids & 1))
192                         continue;
193
194                 do {
195                         skb = mt76_txq_dequeue(dev, mtxq, true);
196                         if (!skb)
197                                 break;
198
199                         if (mtxq->aggr)
200                                 mt76_check_agg_ssn(mtxq, skb);
201
202                         nframes--;
203                         if (last_skb)
204                                 mt76_queue_ps_skb(dev, sta, last_skb, false);
205
206                         last_skb = skb;
207                 } while (nframes);
208         }
209
210         if (last_skb) {
211                 mt76_queue_ps_skb(dev, sta, last_skb, true);
212                 dev->queue_ops->kick(dev, hwq);
213         }
214         spin_unlock_bh(&hwq->lock);
215 }
216 EXPORT_SYMBOL_GPL(mt76_release_buffered_frames);
217
218 static int
219 mt76_txq_send_burst(struct mt76_dev *dev, struct mt76_queue *hwq,
220                     struct mt76_txq *mtxq, bool *empty)
221 {
222         struct ieee80211_txq *txq = mtxq_to_txq(mtxq);
223         struct ieee80211_tx_info *info;
224         struct mt76_wcid *wcid = mtxq->wcid;
225         struct sk_buff *skb;
226         int n_frames = 1, limit;
227         struct ieee80211_tx_rate tx_rate;
228         bool ampdu;
229         bool probe;
230         int idx;
231
232         skb = mt76_txq_dequeue(dev, mtxq, false);
233         if (!skb) {
234                 *empty = true;
235                 return 0;
236         }
237
238         info = IEEE80211_SKB_CB(skb);
239         if (!wcid->tx_rate_set)
240                 ieee80211_get_tx_rates(txq->vif, txq->sta, skb,
241                                        info->control.rates, 1);
242         tx_rate = info->control.rates[0];
243
244         probe = (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE);
245         ampdu = IEEE80211_SKB_CB(skb)->flags & IEEE80211_TX_CTL_AMPDU;
246         limit = ampdu ? 16 : 3;
247
248         if (ampdu)
249                 mt76_check_agg_ssn(mtxq, skb);
250
251         idx = dev->queue_ops->tx_queue_skb(dev, hwq, skb, wcid, txq->sta);
252
253         if (idx < 0)
254                 return idx;
255
256         do {
257                 bool cur_ampdu;
258
259                 if (probe)
260                         break;
261
262                 if (test_bit(MT76_OFFCHANNEL, &dev->state) ||
263                     test_bit(MT76_RESET, &dev->state))
264                         return -EBUSY;
265
266                 skb = mt76_txq_dequeue(dev, mtxq, false);
267                 if (!skb) {
268                         *empty = true;
269                         break;
270                 }
271
272                 info = IEEE80211_SKB_CB(skb);
273                 cur_ampdu = info->flags & IEEE80211_TX_CTL_AMPDU;
274
275                 if (ampdu != cur_ampdu ||
276                     (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)) {
277                         skb_queue_tail(&mtxq->retry_q, skb);
278                         break;
279                 }
280
281                 info->control.rates[0] = tx_rate;
282
283                 if (cur_ampdu)
284                         mt76_check_agg_ssn(mtxq, skb);
285
286                 idx = dev->queue_ops->tx_queue_skb(dev, hwq, skb, wcid,
287                                                    txq->sta);
288                 if (idx < 0)
289                         return idx;
290
291                 n_frames++;
292         } while (n_frames < limit);
293
294         if (!probe) {
295                 hwq->swq_queued++;
296                 hwq->entry[idx].schedule = true;
297         }
298
299         dev->queue_ops->kick(dev, hwq);
300
301         return n_frames;
302 }
303
304 static int
305 mt76_txq_schedule_list(struct mt76_dev *dev, struct mt76_queue *hwq)
306 {
307         struct mt76_txq *mtxq, *mtxq_last;
308         int len = 0;
309
310 restart:
311         mtxq_last = list_last_entry(&hwq->swq, struct mt76_txq, list);
312         while (!list_empty(&hwq->swq)) {
313                 bool empty = false;
314                 int cur;
315
316                 if (test_bit(MT76_OFFCHANNEL, &dev->state) ||
317                     test_bit(MT76_RESET, &dev->state))
318                         return -EBUSY;
319
320                 mtxq = list_first_entry(&hwq->swq, struct mt76_txq, list);
321                 if (mtxq->send_bar && mtxq->aggr) {
322                         struct ieee80211_txq *txq = mtxq_to_txq(mtxq);
323                         struct ieee80211_sta *sta = txq->sta;
324                         struct ieee80211_vif *vif = txq->vif;
325                         u16 agg_ssn = mtxq->agg_ssn;
326                         u8 tid = txq->tid;
327
328                         mtxq->send_bar = false;
329                         spin_unlock_bh(&hwq->lock);
330                         ieee80211_send_bar(vif, sta->addr, tid, agg_ssn);
331                         spin_lock_bh(&hwq->lock);
332                         goto restart;
333                 }
334
335                 list_del_init(&mtxq->list);
336
337                 cur = mt76_txq_send_burst(dev, hwq, mtxq, &empty);
338                 if (!empty)
339                         list_add_tail(&mtxq->list, &hwq->swq);
340
341                 if (cur < 0)
342                         return cur;
343
344                 len += cur;
345
346                 if (mtxq == mtxq_last)
347                         break;
348         }
349
350         return len;
351 }
352
353 void mt76_txq_schedule(struct mt76_dev *dev, struct mt76_queue *hwq)
354 {
355         int len;
356
357         rcu_read_lock();
358         do {
359                 if (hwq->swq_queued >= 4 || list_empty(&hwq->swq))
360                         break;
361
362                 len = mt76_txq_schedule_list(dev, hwq);
363         } while (len > 0);
364         rcu_read_unlock();
365 }
366 EXPORT_SYMBOL_GPL(mt76_txq_schedule);
367
368 void mt76_txq_schedule_all(struct mt76_dev *dev)
369 {
370         int i;
371
372         for (i = 0; i <= MT_TXQ_BK; i++) {
373                 struct mt76_queue *q = &dev->q_tx[i];
374
375                 spin_lock_bh(&q->lock);
376                 mt76_txq_schedule(dev, q);
377                 spin_unlock_bh(&q->lock);
378         }
379 }
380 EXPORT_SYMBOL_GPL(mt76_txq_schedule_all);
381
382 void mt76_stop_tx_queues(struct mt76_dev *dev, struct ieee80211_sta *sta,
383                          bool send_bar)
384 {
385         int i;
386
387         for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
388                 struct ieee80211_txq *txq = sta->txq[i];
389                 struct mt76_txq *mtxq;
390
391                 if (!txq)
392                         continue;
393
394                 mtxq = (struct mt76_txq *)txq->drv_priv;
395
396                 spin_lock_bh(&mtxq->hwq->lock);
397                 mtxq->send_bar = mtxq->aggr && send_bar;
398                 if (!list_empty(&mtxq->list))
399                         list_del_init(&mtxq->list);
400                 spin_unlock_bh(&mtxq->hwq->lock);
401         }
402 }
403 EXPORT_SYMBOL_GPL(mt76_stop_tx_queues);
404
405 void mt76_wake_tx_queue(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
406 {
407         struct mt76_dev *dev = hw->priv;
408         struct mt76_txq *mtxq = (struct mt76_txq *) txq->drv_priv;
409         struct mt76_queue *hwq = mtxq->hwq;
410
411         spin_lock_bh(&hwq->lock);
412         if (list_empty(&mtxq->list))
413                 list_add_tail(&mtxq->list, &hwq->swq);
414         mt76_txq_schedule(dev, hwq);
415         spin_unlock_bh(&hwq->lock);
416 }
417 EXPORT_SYMBOL_GPL(mt76_wake_tx_queue);
418
419 void mt76_txq_remove(struct mt76_dev *dev, struct ieee80211_txq *txq)
420 {
421         struct mt76_txq *mtxq;
422         struct mt76_queue *hwq;
423         struct sk_buff *skb;
424
425         if (!txq)
426                 return;
427
428         mtxq = (struct mt76_txq *) txq->drv_priv;
429         hwq = mtxq->hwq;
430
431         spin_lock_bh(&hwq->lock);
432         if (!list_empty(&mtxq->list))
433                 list_del(&mtxq->list);
434         spin_unlock_bh(&hwq->lock);
435
436         while ((skb = skb_dequeue(&mtxq->retry_q)) != NULL)
437                 ieee80211_free_txskb(dev->hw, skb);
438 }
439 EXPORT_SYMBOL_GPL(mt76_txq_remove);
440
441 void mt76_txq_init(struct mt76_dev *dev, struct ieee80211_txq *txq)
442 {
443         struct mt76_txq *mtxq = (struct mt76_txq *) txq->drv_priv;
444
445         INIT_LIST_HEAD(&mtxq->list);
446         skb_queue_head_init(&mtxq->retry_q);
447
448         mtxq->hwq = &dev->q_tx[mt76_txq_get_qid(txq)];
449 }
450 EXPORT_SYMBOL_GPL(mt76_txq_init);