1 // SPDX-License-Identifier: ISC
2 /*
3 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4 */
5
6 #include "mt76.h"
7
8 static int
mt76_txq_get_qid(struct ieee80211_txq * txq)9 mt76_txq_get_qid(struct ieee80211_txq *txq)
10 {
11 if (!txq->sta)
12 return MT_TXQ_BE;
13
14 return txq->ac;
15 }
16
17 void
mt76_tx_check_agg_ssn(struct ieee80211_sta * sta,struct sk_buff * skb)18 mt76_tx_check_agg_ssn(struct ieee80211_sta *sta, struct sk_buff *skb)
19 {
20 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
21 struct ieee80211_txq *txq;
22 struct mt76_txq *mtxq;
23 u8 tid;
24
25 if (!sta || !ieee80211_is_data_qos(hdr->frame_control) ||
26 !ieee80211_is_data_present(hdr->frame_control))
27 return;
28
29 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
30 txq = sta->txq[tid];
31 mtxq = (struct mt76_txq *)txq->drv_priv;
32 if (!mtxq->aggr)
33 return;
34
35 mtxq->agg_ssn = le16_to_cpu(hdr->seq_ctrl) + 0x10;
36 }
37 EXPORT_SYMBOL_GPL(mt76_tx_check_agg_ssn);
38
39 void
mt76_tx_status_lock(struct mt76_dev * dev,struct sk_buff_head * list)40 mt76_tx_status_lock(struct mt76_dev *dev, struct sk_buff_head *list)
41 __acquires(&dev->status_lock)
42 {
43 __skb_queue_head_init(list);
44 spin_lock_bh(&dev->status_lock);
45 }
46 EXPORT_SYMBOL_GPL(mt76_tx_status_lock);
47
48 void
mt76_tx_status_unlock(struct mt76_dev * dev,struct sk_buff_head * list)49 mt76_tx_status_unlock(struct mt76_dev *dev, struct sk_buff_head *list)
50 __releases(&dev->status_lock)
51 {
52 struct ieee80211_hw *hw;
53 struct sk_buff *skb;
54
55 spin_unlock_bh(&dev->status_lock);
56
57 rcu_read_lock();
58 while ((skb = __skb_dequeue(list)) != NULL) {
59 struct ieee80211_tx_status status = {
60 .skb = skb,
61 .info = IEEE80211_SKB_CB(skb),
62 };
63 struct ieee80211_rate_status rs = {};
64 struct mt76_tx_cb *cb = mt76_tx_skb_cb(skb);
65 struct mt76_wcid *wcid;
66
67 wcid = rcu_dereference(dev->wcid[cb->wcid]);
68 if (wcid) {
69 status.sta = wcid_to_sta(wcid);
70 if (status.sta && (wcid->rate.flags || wcid->rate.legacy)) {
71 rs.rate_idx = wcid->rate;
72 status.rates = &rs;
73 status.n_rates = 1;
74 } else {
75 status.n_rates = 0;
76 }
77 }
78
79 hw = mt76_tx_status_get_hw(dev, skb);
80 spin_lock_bh(&dev->rx_lock);
81 ieee80211_tx_status_ext(hw, &status);
82 spin_unlock_bh(&dev->rx_lock);
83 }
84 rcu_read_unlock();
85 }
86 EXPORT_SYMBOL_GPL(mt76_tx_status_unlock);
87
88 static void
__mt76_tx_status_skb_done(struct mt76_dev * dev,struct sk_buff * skb,u8 flags,struct sk_buff_head * list)89 __mt76_tx_status_skb_done(struct mt76_dev *dev, struct sk_buff *skb, u8 flags,
90 struct sk_buff_head *list)
91 {
92 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
93 struct mt76_tx_cb *cb = mt76_tx_skb_cb(skb);
94 u8 done = MT_TX_CB_DMA_DONE | MT_TX_CB_TXS_DONE;
95
96 flags |= cb->flags;
97 cb->flags = flags;
98
99 if ((flags & done) != done)
100 return;
101
102 /* Tx status can be unreliable. if it fails, mark the frame as ACKed */
103 if (flags & MT_TX_CB_TXS_FAILED &&
104 (dev->drv->drv_flags & MT_DRV_IGNORE_TXS_FAILED)) {
105 info->status.rates[0].count = 0;
106 info->status.rates[0].idx = -1;
107 info->flags |= IEEE80211_TX_STAT_ACK;
108 }
109
110 __skb_queue_tail(list, skb);
111 }
112
113 void
mt76_tx_status_skb_done(struct mt76_dev * dev,struct sk_buff * skb,struct sk_buff_head * list)114 mt76_tx_status_skb_done(struct mt76_dev *dev, struct sk_buff *skb,
115 struct sk_buff_head *list)
116 {
117 __mt76_tx_status_skb_done(dev, skb, MT_TX_CB_TXS_DONE, list);
118 }
119 EXPORT_SYMBOL_GPL(mt76_tx_status_skb_done);
120
121 int
mt76_tx_status_skb_add(struct mt76_dev * dev,struct mt76_wcid * wcid,struct sk_buff * skb)122 mt76_tx_status_skb_add(struct mt76_dev *dev, struct mt76_wcid *wcid,
123 struct sk_buff *skb)
124 {
125 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
126 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
127 struct mt76_tx_cb *cb = mt76_tx_skb_cb(skb);
128 int pid;
129
130 memset(cb, 0, sizeof(*cb));
131
132 if (!wcid || !rcu_access_pointer(dev->wcid[wcid->idx]))
133 return MT_PACKET_ID_NO_ACK;
134
135 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
136 return MT_PACKET_ID_NO_ACK;
137
138 if (!(info->flags & (IEEE80211_TX_CTL_REQ_TX_STATUS |
139 IEEE80211_TX_CTL_RATE_CTRL_PROBE))) {
140 if (mtk_wed_device_active(&dev->mmio.wed) &&
141 ((info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) ||
142 ieee80211_is_data(hdr->frame_control)))
143 return MT_PACKET_ID_WED;
144
145 return MT_PACKET_ID_NO_SKB;
146 }
147
148 spin_lock_bh(&dev->status_lock);
149
150 pid = idr_alloc(&wcid->pktid, skb, MT_PACKET_ID_FIRST,
151 MT_PACKET_ID_MASK, GFP_ATOMIC);
152 if (pid < 0) {
153 pid = MT_PACKET_ID_NO_SKB;
154 goto out;
155 }
156
157 cb->wcid = wcid->idx;
158 cb->pktid = pid;
159
160 if (list_empty(&wcid->list))
161 list_add_tail(&wcid->list, &dev->wcid_list);
162
163 out:
164 spin_unlock_bh(&dev->status_lock);
165
166 return pid;
167 }
168 EXPORT_SYMBOL_GPL(mt76_tx_status_skb_add);
169
170 struct sk_buff *
mt76_tx_status_skb_get(struct mt76_dev * dev,struct mt76_wcid * wcid,int pktid,struct sk_buff_head * list)171 mt76_tx_status_skb_get(struct mt76_dev *dev, struct mt76_wcid *wcid, int pktid,
172 struct sk_buff_head *list)
173 {
174 struct sk_buff *skb;
175 int id;
176
177 lockdep_assert_held(&dev->status_lock);
178
179 skb = idr_remove(&wcid->pktid, pktid);
180 if (skb)
181 goto out;
182
183 /* look for stale entries in the wcid idr queue */
184 idr_for_each_entry(&wcid->pktid, skb, id) {
185 struct mt76_tx_cb *cb = mt76_tx_skb_cb(skb);
186
187 if (pktid >= 0) {
188 if (!(cb->flags & MT_TX_CB_DMA_DONE))
189 continue;
190
191 if (time_is_after_jiffies(cb->jiffies +
192 MT_TX_STATUS_SKB_TIMEOUT))
193 continue;
194 }
195
196 /* It has been too long since DMA_DONE, time out this packet
197 * and stop waiting for TXS callback.
198 */
199 idr_remove(&wcid->pktid, cb->pktid);
200 __mt76_tx_status_skb_done(dev, skb, MT_TX_CB_TXS_FAILED |
201 MT_TX_CB_TXS_DONE, list);
202 }
203
204 out:
205 if (idr_is_empty(&wcid->pktid))
206 list_del_init(&wcid->list);
207
208 return skb;
209 }
210 EXPORT_SYMBOL_GPL(mt76_tx_status_skb_get);
211
212 void
mt76_tx_status_check(struct mt76_dev * dev,bool flush)213 mt76_tx_status_check(struct mt76_dev *dev, bool flush)
214 {
215 struct mt76_wcid *wcid, *tmp;
216 struct sk_buff_head list;
217
218 mt76_tx_status_lock(dev, &list);
219 list_for_each_entry_safe(wcid, tmp, &dev->wcid_list, list)
220 mt76_tx_status_skb_get(dev, wcid, flush ? -1 : 0, &list);
221 mt76_tx_status_unlock(dev, &list);
222 }
223 EXPORT_SYMBOL_GPL(mt76_tx_status_check);
224
225 static void
mt76_tx_check_non_aql(struct mt76_dev * dev,struct mt76_wcid * wcid,struct sk_buff * skb)226 mt76_tx_check_non_aql(struct mt76_dev *dev, struct mt76_wcid *wcid,
227 struct sk_buff *skb)
228 {
229 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
230 int pending;
231
232 if (!wcid || info->tx_time_est)
233 return;
234
235 pending = atomic_dec_return(&wcid->non_aql_packets);
236 if (pending < 0)
237 atomic_cmpxchg(&wcid->non_aql_packets, pending, 0);
238 }
239
__mt76_tx_complete_skb(struct mt76_dev * dev,u16 wcid_idx,struct sk_buff * skb,struct list_head * free_list)240 void __mt76_tx_complete_skb(struct mt76_dev *dev, u16 wcid_idx, struct sk_buff *skb,
241 struct list_head *free_list)
242 {
243 struct mt76_tx_cb *cb = mt76_tx_skb_cb(skb);
244 struct ieee80211_tx_status status = {
245 .skb = skb,
246 .free_list = free_list,
247 };
248 struct mt76_wcid *wcid = NULL;
249 struct ieee80211_hw *hw;
250 struct sk_buff_head list;
251
252 rcu_read_lock();
253
254 if (wcid_idx < ARRAY_SIZE(dev->wcid))
255 wcid = rcu_dereference(dev->wcid[wcid_idx]);
256
257 mt76_tx_check_non_aql(dev, wcid, skb);
258
259 #ifdef CONFIG_NL80211_TESTMODE
260 if (mt76_is_testmode_skb(dev, skb, &hw)) {
261 struct mt76_phy *phy = hw->priv;
262
263 if (skb == phy->test.tx_skb)
264 phy->test.tx_done++;
265 if (phy->test.tx_queued == phy->test.tx_done)
266 wake_up(&dev->tx_wait);
267
268 dev_kfree_skb_any(skb);
269 goto out;
270 }
271 #endif
272
273 if (cb->pktid < MT_PACKET_ID_FIRST) {
274 struct ieee80211_rate_status rs = {};
275
276 hw = mt76_tx_status_get_hw(dev, skb);
277 status.sta = wcid_to_sta(wcid);
278 if (status.sta && (wcid->rate.flags || wcid->rate.legacy)) {
279 rs.rate_idx = wcid->rate;
280 status.rates = &rs;
281 status.n_rates = 1;
282 }
283 spin_lock_bh(&dev->rx_lock);
284 ieee80211_tx_status_ext(hw, &status);
285 spin_unlock_bh(&dev->rx_lock);
286 goto out;
287 }
288
289 mt76_tx_status_lock(dev, &list);
290 cb->jiffies = jiffies;
291 __mt76_tx_status_skb_done(dev, skb, MT_TX_CB_DMA_DONE, &list);
292 mt76_tx_status_unlock(dev, &list);
293
294 out:
295 rcu_read_unlock();
296 }
297 EXPORT_SYMBOL_GPL(__mt76_tx_complete_skb);
298
299 static int
__mt76_tx_queue_skb(struct mt76_phy * phy,int qid,struct sk_buff * skb,struct mt76_wcid * wcid,struct ieee80211_sta * sta,bool * stop)300 __mt76_tx_queue_skb(struct mt76_phy *phy, int qid, struct sk_buff *skb,
301 struct mt76_wcid *wcid, struct ieee80211_sta *sta,
302 bool *stop)
303 {
304 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
305 struct mt76_queue *q = phy->q_tx[qid];
306 struct mt76_dev *dev = phy->dev;
307 bool non_aql;
308 int pending;
309 int idx;
310
311 non_aql = !info->tx_time_est;
312 idx = dev->queue_ops->tx_queue_skb(phy, q, qid, skb, wcid, sta);
313 if (idx < 0 || !sta)
314 return idx;
315
316 wcid = (struct mt76_wcid *)sta->drv_priv;
317 if (!wcid->sta)
318 return idx;
319
320 q->entry[idx].wcid = wcid->idx;
321
322 if (!non_aql)
323 return idx;
324
325 pending = atomic_inc_return(&wcid->non_aql_packets);
326 if (stop && pending >= MT_MAX_NON_AQL_PKT)
327 *stop = true;
328
329 return idx;
330 }
331
332 void
mt76_tx(struct mt76_phy * phy,struct ieee80211_sta * sta,struct mt76_wcid * wcid,struct sk_buff * skb)333 mt76_tx(struct mt76_phy *phy, struct ieee80211_sta *sta,
334 struct mt76_wcid *wcid, struct sk_buff *skb)
335 {
336 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
337 struct ieee80211_hdr *hdr = (void *)skb->data;
338 struct sk_buff_head *head;
339
340 if (mt76_testmode_enabled(phy)) {
341 ieee80211_free_txskb(phy->hw, skb);
342 return;
343 }
344
345 if (WARN_ON(skb_get_queue_mapping(skb) >= MT_TXQ_PSD))
346 skb_set_queue_mapping(skb, MT_TXQ_BE);
347
348 if (wcid && !(wcid->tx_info & MT_WCID_TX_INFO_SET))
349 ieee80211_get_tx_rates(info->control.vif, sta, skb,
350 info->control.rates, 1);
351
352 info->hw_queue |= FIELD_PREP(MT_TX_HW_QUEUE_PHY, phy->band_idx);
353
354 if ((info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
355 ((info->control.flags & IEEE80211_TX_CTRL_DONT_USE_RATE_MASK) &&
356 ieee80211_is_probe_req(hdr->frame_control)))
357 head = &wcid->tx_offchannel;
358 else
359 head = &wcid->tx_pending;
360
361 spin_lock_bh(&head->lock);
362 __skb_queue_tail(head, skb);
363 spin_unlock_bh(&head->lock);
364
365 spin_lock_bh(&phy->tx_lock);
366 if (list_empty(&wcid->tx_list))
367 list_add_tail(&wcid->tx_list, &phy->tx_list);
368 spin_unlock_bh(&phy->tx_lock);
369
370 mt76_worker_schedule(&phy->dev->tx_worker);
371 }
372 EXPORT_SYMBOL_GPL(mt76_tx);
373
374 static struct sk_buff *
mt76_txq_dequeue(struct mt76_phy * phy,struct mt76_txq * mtxq)375 mt76_txq_dequeue(struct mt76_phy *phy, struct mt76_txq *mtxq)
376 {
377 struct ieee80211_txq *txq = mtxq_to_txq(mtxq);
378 struct ieee80211_tx_info *info;
379 struct sk_buff *skb;
380
381 skb = ieee80211_tx_dequeue(phy->hw, txq);
382 if (!skb)
383 return NULL;
384
385 info = IEEE80211_SKB_CB(skb);
386 info->hw_queue |= FIELD_PREP(MT_TX_HW_QUEUE_PHY, phy->band_idx);
387
388 return skb;
389 }
390
391 static void
mt76_queue_ps_skb(struct mt76_phy * phy,struct ieee80211_sta * sta,struct sk_buff * skb,bool last)392 mt76_queue_ps_skb(struct mt76_phy *phy, struct ieee80211_sta *sta,
393 struct sk_buff *skb, bool last)
394 {
395 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
396 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
397
398 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
399 if (last)
400 info->flags |= IEEE80211_TX_STATUS_EOSP |
401 IEEE80211_TX_CTL_REQ_TX_STATUS;
402
403 mt76_skb_set_moredata(skb, !last);
404 __mt76_tx_queue_skb(phy, MT_TXQ_PSD, skb, wcid, sta, NULL);
405 }
406
407 void
mt76_release_buffered_frames(struct ieee80211_hw * hw,struct ieee80211_sta * sta,u16 tids,int nframes,enum ieee80211_frame_release_type reason,bool more_data)408 mt76_release_buffered_frames(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
409 u16 tids, int nframes,
410 enum ieee80211_frame_release_type reason,
411 bool more_data)
412 {
413 struct mt76_phy *phy = hw->priv;
414 struct mt76_dev *dev = phy->dev;
415 struct sk_buff *last_skb = NULL;
416 struct mt76_queue *hwq = phy->q_tx[MT_TXQ_PSD];
417 int i;
418
419 spin_lock_bh(&hwq->lock);
420 for (i = 0; tids && nframes; i++, tids >>= 1) {
421 struct ieee80211_txq *txq = sta->txq[i];
422 struct mt76_txq *mtxq = (struct mt76_txq *)txq->drv_priv;
423 struct sk_buff *skb;
424
425 if (!(tids & 1))
426 continue;
427
428 do {
429 skb = mt76_txq_dequeue(phy, mtxq);
430 if (!skb)
431 break;
432
433 nframes--;
434 if (last_skb)
435 mt76_queue_ps_skb(phy, sta, last_skb, false);
436
437 last_skb = skb;
438 } while (nframes);
439 }
440
441 if (last_skb) {
442 mt76_queue_ps_skb(phy, sta, last_skb, true);
443 dev->queue_ops->kick(dev, hwq);
444 } else {
445 ieee80211_sta_eosp(sta);
446 }
447
448 spin_unlock_bh(&hwq->lock);
449 }
450 EXPORT_SYMBOL_GPL(mt76_release_buffered_frames);
451
452 static bool
mt76_txq_stopped(struct mt76_queue * q)453 mt76_txq_stopped(struct mt76_queue *q)
454 {
455 return q->stopped || q->blocked ||
456 q->queued + MT_TXQ_FREE_THR >= q->ndesc;
457 }
458
459 static int
mt76_txq_send_burst(struct mt76_phy * phy,struct mt76_queue * q,struct mt76_txq * mtxq,struct mt76_wcid * wcid)460 mt76_txq_send_burst(struct mt76_phy *phy, struct mt76_queue *q,
461 struct mt76_txq *mtxq, struct mt76_wcid *wcid)
462 {
463 struct mt76_dev *dev = phy->dev;
464 struct ieee80211_txq *txq = mtxq_to_txq(mtxq);
465 enum mt76_txq_id qid = mt76_txq_get_qid(txq);
466 struct ieee80211_tx_info *info;
467 struct sk_buff *skb;
468 int n_frames = 1;
469 bool stop = false;
470 int idx;
471
472 if (test_bit(MT_WCID_FLAG_PS, &wcid->flags))
473 return 0;
474
475 if (atomic_read(&wcid->non_aql_packets) >= MT_MAX_NON_AQL_PKT)
476 return 0;
477
478 skb = mt76_txq_dequeue(phy, mtxq);
479 if (!skb)
480 return 0;
481
482 info = IEEE80211_SKB_CB(skb);
483 if (!(wcid->tx_info & MT_WCID_TX_INFO_SET))
484 ieee80211_get_tx_rates(txq->vif, txq->sta, skb,
485 info->control.rates, 1);
486
487 spin_lock(&q->lock);
488 idx = __mt76_tx_queue_skb(phy, qid, skb, wcid, txq->sta, &stop);
489 spin_unlock(&q->lock);
490 if (idx < 0)
491 return idx;
492
493 do {
494 if (test_bit(MT76_RESET, &phy->state) || phy->offchannel)
495 return -EBUSY;
496
497 if (stop || mt76_txq_stopped(q))
498 break;
499
500 skb = mt76_txq_dequeue(phy, mtxq);
501 if (!skb)
502 break;
503
504 info = IEEE80211_SKB_CB(skb);
505 if (!(wcid->tx_info & MT_WCID_TX_INFO_SET))
506 ieee80211_get_tx_rates(txq->vif, txq->sta, skb,
507 info->control.rates, 1);
508
509 spin_lock(&q->lock);
510 idx = __mt76_tx_queue_skb(phy, qid, skb, wcid, txq->sta, &stop);
511 spin_unlock(&q->lock);
512 if (idx < 0)
513 break;
514
515 n_frames++;
516 } while (1);
517
518 spin_lock(&q->lock);
519 dev->queue_ops->kick(dev, q);
520 spin_unlock(&q->lock);
521
522 return n_frames;
523 }
524
525 static int
mt76_txq_schedule_list(struct mt76_phy * phy,enum mt76_txq_id qid)526 mt76_txq_schedule_list(struct mt76_phy *phy, enum mt76_txq_id qid)
527 {
528 struct mt76_queue *q = phy->q_tx[qid];
529 struct mt76_dev *dev = phy->dev;
530 struct ieee80211_txq *txq;
531 struct mt76_txq *mtxq;
532 struct mt76_wcid *wcid;
533 int ret = 0;
534
535 while (1) {
536 int n_frames = 0;
537
538 if (test_bit(MT76_RESET, &phy->state) || phy->offchannel)
539 return -EBUSY;
540
541 if (dev->queue_ops->tx_cleanup &&
542 q->queued + 2 * MT_TXQ_FREE_THR >= q->ndesc) {
543 dev->queue_ops->tx_cleanup(dev, q, false);
544 }
545
546 txq = ieee80211_next_txq(phy->hw, qid);
547 if (!txq)
548 break;
549
550 mtxq = (struct mt76_txq *)txq->drv_priv;
551 wcid = rcu_dereference(dev->wcid[mtxq->wcid]);
552 if (!wcid || test_bit(MT_WCID_FLAG_PS, &wcid->flags))
553 continue;
554
555 if (mtxq->send_bar && mtxq->aggr) {
556 struct ieee80211_txq *txq = mtxq_to_txq(mtxq);
557 struct ieee80211_sta *sta = txq->sta;
558 struct ieee80211_vif *vif = txq->vif;
559 u16 agg_ssn = mtxq->agg_ssn;
560 u8 tid = txq->tid;
561
562 mtxq->send_bar = false;
563 ieee80211_send_bar(vif, sta->addr, tid, agg_ssn);
564 }
565
566 if (!mt76_txq_stopped(q))
567 n_frames = mt76_txq_send_burst(phy, q, mtxq, wcid);
568
569 ieee80211_return_txq(phy->hw, txq, false);
570
571 if (unlikely(n_frames < 0))
572 return n_frames;
573
574 ret += n_frames;
575 }
576
577 return ret;
578 }
579
mt76_txq_schedule(struct mt76_phy * phy,enum mt76_txq_id qid)580 void mt76_txq_schedule(struct mt76_phy *phy, enum mt76_txq_id qid)
581 {
582 int len;
583
584 if (qid >= 4 || phy->offchannel)
585 return;
586
587 local_bh_disable();
588 rcu_read_lock();
589
590 do {
591 ieee80211_txq_schedule_start(phy->hw, qid);
592 len = mt76_txq_schedule_list(phy, qid);
593 ieee80211_txq_schedule_end(phy->hw, qid);
594 } while (len > 0);
595
596 rcu_read_unlock();
597 local_bh_enable();
598 }
599 EXPORT_SYMBOL_GPL(mt76_txq_schedule);
600
601 static int
mt76_txq_schedule_pending_wcid(struct mt76_phy * phy,struct mt76_wcid * wcid,struct sk_buff_head * head)602 mt76_txq_schedule_pending_wcid(struct mt76_phy *phy, struct mt76_wcid *wcid,
603 struct sk_buff_head *head)
604 {
605 struct mt76_dev *dev = phy->dev;
606 struct ieee80211_sta *sta;
607 struct mt76_queue *q;
608 struct sk_buff *skb;
609 int ret = 0;
610
611 spin_lock(&head->lock);
612 while ((skb = skb_peek(head)) != NULL) {
613 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
614 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
615 int qid = skb_get_queue_mapping(skb);
616
617 if ((dev->drv->drv_flags & MT_DRV_HW_MGMT_TXQ) &&
618 !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) &&
619 !ieee80211_is_data(hdr->frame_control) &&
620 !ieee80211_is_bufferable_mmpdu(skb))
621 qid = MT_TXQ_PSD;
622
623 q = phy->q_tx[qid];
624 if (mt76_txq_stopped(q) || test_bit(MT76_RESET, &phy->state)) {
625 ret = -1;
626 break;
627 }
628
629 __skb_unlink(skb, head);
630 spin_unlock(&head->lock);
631
632 sta = wcid_to_sta(wcid);
633 spin_lock(&q->lock);
634 __mt76_tx_queue_skb(phy, qid, skb, wcid, sta, NULL);
635 dev->queue_ops->kick(dev, q);
636 spin_unlock(&q->lock);
637
638 spin_lock(&head->lock);
639 }
640 spin_unlock(&head->lock);
641
642 return ret;
643 }
644
mt76_txq_schedule_pending(struct mt76_phy * phy)645 static void mt76_txq_schedule_pending(struct mt76_phy *phy)
646 {
647 LIST_HEAD(tx_list);
648 int ret = 0;
649
650 if (list_empty(&phy->tx_list))
651 return;
652
653 local_bh_disable();
654 rcu_read_lock();
655
656 spin_lock(&phy->tx_lock);
657 list_splice_init(&phy->tx_list, &tx_list);
658 while (!list_empty(&tx_list)) {
659 struct mt76_wcid *wcid;
660
661 wcid = list_first_entry(&tx_list, struct mt76_wcid, tx_list);
662 list_del_init(&wcid->tx_list);
663
664 spin_unlock(&phy->tx_lock);
665 if (ret >= 0)
666 ret = mt76_txq_schedule_pending_wcid(phy, wcid, &wcid->tx_offchannel);
667 if (ret >= 0 && !phy->offchannel)
668 ret = mt76_txq_schedule_pending_wcid(phy, wcid, &wcid->tx_pending);
669 spin_lock(&phy->tx_lock);
670
671 if (!skb_queue_empty(&wcid->tx_pending) &&
672 !skb_queue_empty(&wcid->tx_offchannel) &&
673 list_empty(&wcid->tx_list))
674 list_add_tail(&wcid->tx_list, &phy->tx_list);
675 }
676 spin_unlock(&phy->tx_lock);
677
678 rcu_read_unlock();
679 local_bh_enable();
680 }
681
mt76_txq_schedule_all(struct mt76_phy * phy)682 void mt76_txq_schedule_all(struct mt76_phy *phy)
683 {
684 int i;
685
686 mt76_txq_schedule_pending(phy);
687 for (i = 0; i <= MT_TXQ_BK; i++)
688 mt76_txq_schedule(phy, i);
689 }
690 EXPORT_SYMBOL_GPL(mt76_txq_schedule_all);
691
mt76_tx_worker_run(struct mt76_dev * dev)692 void mt76_tx_worker_run(struct mt76_dev *dev)
693 {
694 struct mt76_phy *phy;
695 int i;
696
697 for (i = 0; i < ARRAY_SIZE(dev->phys); i++) {
698 phy = dev->phys[i];
699 if (!phy)
700 continue;
701
702 mt76_txq_schedule_all(phy);
703 }
704
705 #ifdef CONFIG_NL80211_TESTMODE
706 for (i = 0; i < ARRAY_SIZE(dev->phys); i++) {
707 phy = dev->phys[i];
708 if (!phy || !phy->test.tx_pending)
709 continue;
710
711 mt76_testmode_tx_pending(phy);
712 }
713 #endif
714 }
715 EXPORT_SYMBOL_GPL(mt76_tx_worker_run);
716
mt76_tx_worker(struct mt76_worker * w)717 void mt76_tx_worker(struct mt76_worker *w)
718 {
719 struct mt76_dev *dev = container_of(w, struct mt76_dev, tx_worker);
720
721 mt76_tx_worker_run(dev);
722 }
723
mt76_stop_tx_queues(struct mt76_phy * phy,struct ieee80211_sta * sta,bool send_bar)724 void mt76_stop_tx_queues(struct mt76_phy *phy, struct ieee80211_sta *sta,
725 bool send_bar)
726 {
727 int i;
728
729 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
730 struct ieee80211_txq *txq = sta->txq[i];
731 struct mt76_queue *hwq;
732 struct mt76_txq *mtxq;
733
734 if (!txq)
735 continue;
736
737 hwq = phy->q_tx[mt76_txq_get_qid(txq)];
738 mtxq = (struct mt76_txq *)txq->drv_priv;
739
740 spin_lock_bh(&hwq->lock);
741 mtxq->send_bar = mtxq->aggr && send_bar;
742 spin_unlock_bh(&hwq->lock);
743 }
744 }
745 EXPORT_SYMBOL_GPL(mt76_stop_tx_queues);
746
mt76_wake_tx_queue(struct ieee80211_hw * hw,struct ieee80211_txq * txq)747 void mt76_wake_tx_queue(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
748 {
749 struct mt76_phy *phy = hw->priv;
750 struct mt76_dev *dev = phy->dev;
751
752 if (!test_bit(MT76_STATE_RUNNING, &phy->state))
753 return;
754
755 mt76_worker_schedule(&dev->tx_worker);
756 }
757 EXPORT_SYMBOL_GPL(mt76_wake_tx_queue);
758
mt76_ac_to_hwq(u8 ac)759 u8 mt76_ac_to_hwq(u8 ac)
760 {
761 static const u8 wmm_queue_map[] = {
762 [IEEE80211_AC_BE] = 0,
763 [IEEE80211_AC_BK] = 1,
764 [IEEE80211_AC_VI] = 2,
765 [IEEE80211_AC_VO] = 3,
766 };
767
768 if (WARN_ON(ac >= IEEE80211_NUM_ACS))
769 return 0;
770
771 return wmm_queue_map[ac];
772 }
773 EXPORT_SYMBOL_GPL(mt76_ac_to_hwq);
774
mt76_skb_adjust_pad(struct sk_buff * skb,int pad)775 int mt76_skb_adjust_pad(struct sk_buff *skb, int pad)
776 {
777 struct sk_buff *iter, *last = skb;
778
779 /* First packet of a A-MSDU burst keeps track of the whole burst
780 * length, need to update length of it and the last packet.
781 */
782 skb_walk_frags(skb, iter) {
783 last = iter;
784 if (!iter->next) {
785 skb->data_len += pad;
786 skb->len += pad;
787 break;
788 }
789 }
790
791 if (skb_pad(last, pad))
792 return -ENOMEM;
793
794 __skb_put(last, pad);
795
796 return 0;
797 }
798 EXPORT_SYMBOL_GPL(mt76_skb_adjust_pad);
799
mt76_queue_tx_complete(struct mt76_dev * dev,struct mt76_queue * q,struct mt76_queue_entry * e)800 void mt76_queue_tx_complete(struct mt76_dev *dev, struct mt76_queue *q,
801 struct mt76_queue_entry *e)
802 {
803 if (e->skb)
804 dev->drv->tx_complete_skb(dev, e);
805
806 spin_lock_bh(&q->lock);
807 q->tail = (q->tail + 1) % q->ndesc;
808 q->queued--;
809 spin_unlock_bh(&q->lock);
810 }
811 EXPORT_SYMBOL_GPL(mt76_queue_tx_complete);
812
__mt76_set_tx_blocked(struct mt76_dev * dev,bool blocked)813 void __mt76_set_tx_blocked(struct mt76_dev *dev, bool blocked)
814 {
815 struct mt76_phy *phy = &dev->phy;
816 struct mt76_queue *q = phy->q_tx[0];
817
818 if (blocked == q->blocked)
819 return;
820
821 q->blocked = blocked;
822
823 phy = dev->phys[MT_BAND1];
824 if (phy) {
825 q = phy->q_tx[0];
826 q->blocked = blocked;
827 }
828 phy = dev->phys[MT_BAND2];
829 if (phy) {
830 q = phy->q_tx[0];
831 q->blocked = blocked;
832 }
833
834 if (!blocked)
835 mt76_worker_schedule(&dev->tx_worker);
836 }
837 EXPORT_SYMBOL_GPL(__mt76_set_tx_blocked);
838
mt76_token_consume(struct mt76_dev * dev,struct mt76_txwi_cache ** ptxwi)839 int mt76_token_consume(struct mt76_dev *dev, struct mt76_txwi_cache **ptxwi)
840 {
841 int token;
842
843 spin_lock_bh(&dev->token_lock);
844
845 token = idr_alloc(&dev->token, *ptxwi, 0, dev->token_size, GFP_ATOMIC);
846 if (token >= 0)
847 dev->token_count++;
848
849 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
850 if (mtk_wed_device_active(&dev->mmio.wed) &&
851 token >= dev->mmio.wed.wlan.token_start)
852 dev->wed_token_count++;
853 #endif
854
855 if (dev->token_count >= dev->token_size - MT76_TOKEN_FREE_THR)
856 __mt76_set_tx_blocked(dev, true);
857
858 spin_unlock_bh(&dev->token_lock);
859
860 return token;
861 }
862 EXPORT_SYMBOL_GPL(mt76_token_consume);
863
mt76_rx_token_consume(struct mt76_dev * dev,void * ptr,struct mt76_txwi_cache * t,dma_addr_t phys)864 int mt76_rx_token_consume(struct mt76_dev *dev, void *ptr,
865 struct mt76_txwi_cache *t, dma_addr_t phys)
866 {
867 int token;
868
869 spin_lock_bh(&dev->rx_token_lock);
870 token = idr_alloc(&dev->rx_token, t, 0, dev->rx_token_size,
871 GFP_ATOMIC);
872 if (token >= 0) {
873 t->ptr = ptr;
874 t->dma_addr = phys;
875 }
876 spin_unlock_bh(&dev->rx_token_lock);
877
878 return token;
879 }
880 EXPORT_SYMBOL_GPL(mt76_rx_token_consume);
881
882 struct mt76_txwi_cache *
mt76_token_release(struct mt76_dev * dev,int token,bool * wake)883 mt76_token_release(struct mt76_dev *dev, int token, bool *wake)
884 {
885 struct mt76_txwi_cache *txwi;
886
887 spin_lock_bh(&dev->token_lock);
888
889 txwi = idr_remove(&dev->token, token);
890 if (txwi) {
891 dev->token_count--;
892
893 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
894 if (mtk_wed_device_active(&dev->mmio.wed) &&
895 token >= dev->mmio.wed.wlan.token_start &&
896 --dev->wed_token_count == 0)
897 wake_up(&dev->tx_wait);
898 #endif
899 }
900
901 if (dev->token_count < dev->token_size - MT76_TOKEN_FREE_THR &&
902 dev->phy.q_tx[0]->blocked)
903 *wake = true;
904
905 spin_unlock_bh(&dev->token_lock);
906
907 return txwi;
908 }
909 EXPORT_SYMBOL_GPL(mt76_token_release);
910
911 struct mt76_txwi_cache *
mt76_rx_token_release(struct mt76_dev * dev,int token)912 mt76_rx_token_release(struct mt76_dev *dev, int token)
913 {
914 struct mt76_txwi_cache *t;
915
916 spin_lock_bh(&dev->rx_token_lock);
917 t = idr_remove(&dev->rx_token, token);
918 spin_unlock_bh(&dev->rx_token_lock);
919
920 return t;
921 }
922 EXPORT_SYMBOL_GPL(mt76_rx_token_release);
923