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 = __mt76_wcid_ptr(dev, 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 wcid = __mt76_wcid_ptr(dev, wcid_idx);
255 mt76_tx_check_non_aql(dev, wcid, skb);
256
257 #ifdef CONFIG_NL80211_TESTMODE
258 if (mt76_is_testmode_skb(dev, skb, &hw)) {
259 struct mt76_phy *phy = hw->priv;
260
261 if (skb == phy->test.tx_skb)
262 phy->test.tx_done++;
263 if (phy->test.tx_queued == phy->test.tx_done)
264 wake_up(&dev->tx_wait);
265
266 dev_kfree_skb_any(skb);
267 goto out;
268 }
269 #endif
270
271 if (cb->pktid < MT_PACKET_ID_FIRST) {
272 struct ieee80211_rate_status rs = {};
273
274 hw = mt76_tx_status_get_hw(dev, skb);
275 status.sta = wcid_to_sta(wcid);
276 if (status.sta && (wcid->rate.flags || wcid->rate.legacy)) {
277 rs.rate_idx = wcid->rate;
278 status.rates = &rs;
279 status.n_rates = 1;
280 }
281 spin_lock_bh(&dev->rx_lock);
282 ieee80211_tx_status_ext(hw, &status);
283 spin_unlock_bh(&dev->rx_lock);
284 goto out;
285 }
286
287 mt76_tx_status_lock(dev, &list);
288 cb->jiffies = jiffies;
289 __mt76_tx_status_skb_done(dev, skb, MT_TX_CB_DMA_DONE, &list);
290 mt76_tx_status_unlock(dev, &list);
291
292 out:
293 rcu_read_unlock();
294 }
295 EXPORT_SYMBOL_GPL(__mt76_tx_complete_skb);
296
297 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)298 __mt76_tx_queue_skb(struct mt76_phy *phy, int qid, struct sk_buff *skb,
299 struct mt76_wcid *wcid, struct ieee80211_sta *sta,
300 bool *stop)
301 {
302 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
303 struct mt76_queue *q = phy->q_tx[qid];
304 struct mt76_dev *dev = phy->dev;
305 bool non_aql;
306 int pending;
307 int idx;
308
309 non_aql = !info->tx_time_est;
310 idx = dev->queue_ops->tx_queue_skb(phy, q, qid, skb, wcid, sta);
311 if (idx < 0 || !sta)
312 return idx;
313
314 wcid = (struct mt76_wcid *)sta->drv_priv;
315 if (!wcid->sta)
316 return idx;
317
318 q->entry[idx].wcid = wcid->idx;
319
320 if (!non_aql)
321 return idx;
322
323 pending = atomic_inc_return(&wcid->non_aql_packets);
324 if (stop && pending >= MT_MAX_NON_AQL_PKT)
325 *stop = true;
326
327 return idx;
328 }
329
330 void
mt76_tx(struct mt76_phy * phy,struct ieee80211_sta * sta,struct mt76_wcid * wcid,struct sk_buff * skb)331 mt76_tx(struct mt76_phy *phy, struct ieee80211_sta *sta,
332 struct mt76_wcid *wcid, struct sk_buff *skb)
333 {
334 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
335 struct ieee80211_hdr *hdr = (void *)skb->data;
336 struct sk_buff_head *head;
337
338 if (mt76_testmode_enabled(phy)) {
339 ieee80211_free_txskb(phy->hw, skb);
340 return;
341 }
342
343 if (WARN_ON(skb_get_queue_mapping(skb) >= MT_TXQ_PSD))
344 skb_set_queue_mapping(skb, MT_TXQ_BE);
345
346 if (wcid && !(wcid->tx_info & MT_WCID_TX_INFO_SET))
347 ieee80211_get_tx_rates(info->control.vif, sta, skb,
348 info->control.rates, 1);
349
350 info->hw_queue |= FIELD_PREP(MT_TX_HW_QUEUE_PHY, phy->band_idx);
351
352 if ((info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
353 ((info->control.flags & IEEE80211_TX_CTRL_DONT_USE_RATE_MASK) &&
354 ieee80211_is_probe_req(hdr->frame_control)))
355 head = &wcid->tx_offchannel;
356 else
357 head = &wcid->tx_pending;
358
359 spin_lock_bh(&head->lock);
360 __skb_queue_tail(head, skb);
361 spin_unlock_bh(&head->lock);
362
363 spin_lock_bh(&phy->tx_lock);
364 if (list_empty(&wcid->tx_list))
365 list_add_tail(&wcid->tx_list, &phy->tx_list);
366 spin_unlock_bh(&phy->tx_lock);
367
368 mt76_worker_schedule(&phy->dev->tx_worker);
369 }
370 EXPORT_SYMBOL_GPL(mt76_tx);
371
372 static struct sk_buff *
mt76_txq_dequeue(struct mt76_phy * phy,struct mt76_txq * mtxq)373 mt76_txq_dequeue(struct mt76_phy *phy, struct mt76_txq *mtxq)
374 {
375 struct ieee80211_txq *txq = mtxq_to_txq(mtxq);
376 struct ieee80211_tx_info *info;
377 struct sk_buff *skb;
378
379 skb = ieee80211_tx_dequeue(phy->hw, txq);
380 if (!skb)
381 return NULL;
382
383 info = IEEE80211_SKB_CB(skb);
384 info->hw_queue |= FIELD_PREP(MT_TX_HW_QUEUE_PHY, phy->band_idx);
385
386 return skb;
387 }
388
389 static void
mt76_queue_ps_skb(struct mt76_phy * phy,struct ieee80211_sta * sta,struct sk_buff * skb,bool last)390 mt76_queue_ps_skb(struct mt76_phy *phy, struct ieee80211_sta *sta,
391 struct sk_buff *skb, bool last)
392 {
393 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
394 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
395
396 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
397 if (last)
398 info->flags |= IEEE80211_TX_STATUS_EOSP |
399 IEEE80211_TX_CTL_REQ_TX_STATUS;
400
401 mt76_skb_set_moredata(skb, !last);
402 __mt76_tx_queue_skb(phy, MT_TXQ_PSD, skb, wcid, sta, NULL);
403 }
404
405 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)406 mt76_release_buffered_frames(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
407 u16 tids, int nframes,
408 enum ieee80211_frame_release_type reason,
409 bool more_data)
410 {
411 struct mt76_phy *phy = hw->priv;
412 struct mt76_dev *dev = phy->dev;
413 struct sk_buff *last_skb = NULL;
414 struct mt76_queue *hwq = phy->q_tx[MT_TXQ_PSD];
415 int i;
416
417 spin_lock_bh(&hwq->lock);
418 for (i = 0; tids && nframes; i++, tids >>= 1) {
419 struct ieee80211_txq *txq = sta->txq[i];
420 struct mt76_txq *mtxq = (struct mt76_txq *)txq->drv_priv;
421 struct sk_buff *skb;
422
423 if (!(tids & 1))
424 continue;
425
426 do {
427 skb = mt76_txq_dequeue(phy, mtxq);
428 if (!skb)
429 break;
430
431 nframes--;
432 if (last_skb)
433 mt76_queue_ps_skb(phy, sta, last_skb, false);
434
435 last_skb = skb;
436 } while (nframes);
437 }
438
439 if (last_skb) {
440 mt76_queue_ps_skb(phy, sta, last_skb, true);
441 dev->queue_ops->kick(dev, hwq);
442 } else {
443 ieee80211_sta_eosp(sta);
444 }
445
446 spin_unlock_bh(&hwq->lock);
447 }
448 EXPORT_SYMBOL_GPL(mt76_release_buffered_frames);
449
450 static bool
mt76_txq_stopped(struct mt76_queue * q)451 mt76_txq_stopped(struct mt76_queue *q)
452 {
453 return q->stopped || q->blocked ||
454 q->queued + MT_TXQ_FREE_THR >= q->ndesc;
455 }
456
457 static int
mt76_txq_send_burst(struct mt76_phy * phy,struct mt76_queue * q,struct mt76_txq * mtxq,struct mt76_wcid * wcid)458 mt76_txq_send_burst(struct mt76_phy *phy, struct mt76_queue *q,
459 struct mt76_txq *mtxq, struct mt76_wcid *wcid)
460 {
461 struct mt76_dev *dev = phy->dev;
462 struct ieee80211_txq *txq = mtxq_to_txq(mtxq);
463 enum mt76_txq_id qid = mt76_txq_get_qid(txq);
464 struct ieee80211_tx_info *info;
465 struct sk_buff *skb;
466 int n_frames = 1;
467 bool stop = false;
468 int idx;
469
470 if (test_bit(MT_WCID_FLAG_PS, &wcid->flags))
471 return 0;
472
473 if (atomic_read(&wcid->non_aql_packets) >= MT_MAX_NON_AQL_PKT)
474 return 0;
475
476 skb = mt76_txq_dequeue(phy, mtxq);
477 if (!skb)
478 return 0;
479
480 info = IEEE80211_SKB_CB(skb);
481 if (!(wcid->tx_info & MT_WCID_TX_INFO_SET))
482 ieee80211_get_tx_rates(txq->vif, txq->sta, skb,
483 info->control.rates, 1);
484
485 spin_lock(&q->lock);
486 idx = __mt76_tx_queue_skb(phy, qid, skb, wcid, txq->sta, &stop);
487 spin_unlock(&q->lock);
488 if (idx < 0)
489 return idx;
490
491 do {
492 if (test_bit(MT76_RESET, &phy->state) || phy->offchannel)
493 break;
494
495 if (stop || mt76_txq_stopped(q))
496 break;
497
498 skb = mt76_txq_dequeue(phy, mtxq);
499 if (!skb)
500 break;
501
502 info = IEEE80211_SKB_CB(skb);
503 if (!(wcid->tx_info & MT_WCID_TX_INFO_SET))
504 ieee80211_get_tx_rates(txq->vif, txq->sta, skb,
505 info->control.rates, 1);
506
507 spin_lock(&q->lock);
508 idx = __mt76_tx_queue_skb(phy, qid, skb, wcid, txq->sta, &stop);
509 spin_unlock(&q->lock);
510 if (idx < 0)
511 break;
512
513 n_frames++;
514 } while (1);
515
516 spin_lock(&q->lock);
517 dev->queue_ops->kick(dev, q);
518 spin_unlock(&q->lock);
519
520 return n_frames;
521 }
522
523 static int
mt76_txq_schedule_list(struct mt76_phy * phy,enum mt76_txq_id qid)524 mt76_txq_schedule_list(struct mt76_phy *phy, enum mt76_txq_id qid)
525 {
526 struct mt76_dev *dev = phy->dev;
527 struct ieee80211_txq *txq;
528 struct mt76_txq *mtxq;
529 struct mt76_wcid *wcid;
530 struct mt76_queue *q;
531 int ret = 0;
532
533 while (1) {
534 int n_frames = 0;
535
536 txq = ieee80211_next_txq(phy->hw, qid);
537 if (!txq)
538 break;
539
540 mtxq = (struct mt76_txq *)txq->drv_priv;
541 wcid = __mt76_wcid_ptr(dev, mtxq->wcid);
542 if (!wcid || test_bit(MT_WCID_FLAG_PS, &wcid->flags))
543 continue;
544
545 phy = mt76_dev_phy(dev, wcid->phy_idx);
546 if (test_bit(MT76_RESET, &phy->state) || phy->offchannel)
547 continue;
548
549 q = phy->q_tx[qid];
550 if (dev->queue_ops->tx_cleanup &&
551 q->queued + 2 * MT_TXQ_FREE_THR >= q->ndesc) {
552 dev->queue_ops->tx_cleanup(dev, q, false);
553 }
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)
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 ieee80211_is_deauth(hdr->frame_control)))
622 qid = MT_TXQ_PSD;
623
624 q = phy->q_tx[qid];
625 if (mt76_txq_stopped(q) || test_bit(MT76_RESET, &phy->state)) {
626 ret = -1;
627 break;
628 }
629
630 __skb_unlink(skb, head);
631 spin_unlock(&head->lock);
632
633 sta = wcid_to_sta(wcid);
634 spin_lock(&q->lock);
635 __mt76_tx_queue_skb(phy, qid, skb, wcid, sta, NULL);
636 dev->queue_ops->kick(dev, q);
637 spin_unlock(&q->lock);
638
639 spin_lock(&head->lock);
640 }
641 spin_unlock(&head->lock);
642
643 return ret;
644 }
645
mt76_txq_schedule_pending(struct mt76_phy * phy)646 static void mt76_txq_schedule_pending(struct mt76_phy *phy)
647 {
648 LIST_HEAD(tx_list);
649 int ret = 0;
650
651 if (list_empty(&phy->tx_list))
652 return;
653
654 local_bh_disable();
655 rcu_read_lock();
656
657 spin_lock(&phy->tx_lock);
658 list_splice_init(&phy->tx_list, &tx_list);
659 while (!list_empty(&tx_list)) {
660 struct mt76_wcid *wcid;
661
662 wcid = list_first_entry(&tx_list, struct mt76_wcid, tx_list);
663 list_del_init(&wcid->tx_list);
664
665 spin_unlock(&phy->tx_lock);
666 if (ret >= 0)
667 ret = mt76_txq_schedule_pending_wcid(phy, wcid, &wcid->tx_offchannel);
668 if (ret >= 0 && !phy->offchannel)
669 ret = mt76_txq_schedule_pending_wcid(phy, wcid, &wcid->tx_pending);
670 spin_lock(&phy->tx_lock);
671
672 if (!skb_queue_empty(&wcid->tx_pending) &&
673 !skb_queue_empty(&wcid->tx_offchannel) &&
674 list_empty(&wcid->tx_list))
675 list_add_tail(&wcid->tx_list, &phy->tx_list);
676 }
677 spin_unlock(&phy->tx_lock);
678
679 rcu_read_unlock();
680 local_bh_enable();
681 }
682
mt76_txq_schedule_all(struct mt76_phy * phy)683 void mt76_txq_schedule_all(struct mt76_phy *phy)
684 {
685 struct mt76_phy *main_phy = &phy->dev->phy;
686 int i;
687
688 mt76_txq_schedule_pending(phy);
689
690 if (phy != main_phy && phy->hw == main_phy->hw)
691 return;
692
693 for (i = 0; i <= MT_TXQ_BK; i++)
694 mt76_txq_schedule(phy, i);
695 }
696 EXPORT_SYMBOL_GPL(mt76_txq_schedule_all);
697
mt76_tx_worker_run(struct mt76_dev * dev)698 void mt76_tx_worker_run(struct mt76_dev *dev)
699 {
700 struct mt76_phy *phy;
701 int i;
702
703 mt76_txq_schedule_all(&dev->phy);
704 for (i = 0; i < ARRAY_SIZE(dev->phys); i++) {
705 phy = dev->phys[i];
706 if (!phy)
707 continue;
708
709 mt76_txq_schedule_all(phy);
710 }
711
712 #ifdef CONFIG_NL80211_TESTMODE
713 for (i = 0; i < ARRAY_SIZE(dev->phys); i++) {
714 phy = dev->phys[i];
715 if (!phy || !phy->test.tx_pending)
716 continue;
717
718 mt76_testmode_tx_pending(phy);
719 }
720 #endif
721 }
722 EXPORT_SYMBOL_GPL(mt76_tx_worker_run);
723
mt76_tx_worker(struct mt76_worker * w)724 void mt76_tx_worker(struct mt76_worker *w)
725 {
726 struct mt76_dev *dev = container_of(w, struct mt76_dev, tx_worker);
727
728 mt76_tx_worker_run(dev);
729 }
730
mt76_stop_tx_queues(struct mt76_phy * phy,struct ieee80211_sta * sta,bool send_bar)731 void mt76_stop_tx_queues(struct mt76_phy *phy, struct ieee80211_sta *sta,
732 bool send_bar)
733 {
734 int i;
735
736 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
737 struct ieee80211_txq *txq = sta->txq[i];
738 struct mt76_queue *hwq;
739 struct mt76_txq *mtxq;
740
741 if (!txq)
742 continue;
743
744 hwq = phy->q_tx[mt76_txq_get_qid(txq)];
745 mtxq = (struct mt76_txq *)txq->drv_priv;
746
747 spin_lock_bh(&hwq->lock);
748 mtxq->send_bar = mtxq->aggr && send_bar;
749 spin_unlock_bh(&hwq->lock);
750 }
751 }
752 EXPORT_SYMBOL_GPL(mt76_stop_tx_queues);
753
mt76_wake_tx_queue(struct ieee80211_hw * hw,struct ieee80211_txq * txq)754 void mt76_wake_tx_queue(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
755 {
756 struct mt76_phy *phy = hw->priv;
757 struct mt76_dev *dev = phy->dev;
758
759 mt76_worker_schedule(&dev->tx_worker);
760 }
761 EXPORT_SYMBOL_GPL(mt76_wake_tx_queue);
762
mt76_ac_to_hwq(u8 ac)763 u8 mt76_ac_to_hwq(u8 ac)
764 {
765 static const u8 wmm_queue_map[] = {
766 [IEEE80211_AC_BE] = 0,
767 [IEEE80211_AC_BK] = 1,
768 [IEEE80211_AC_VI] = 2,
769 [IEEE80211_AC_VO] = 3,
770 };
771
772 if (WARN_ON(ac >= IEEE80211_NUM_ACS))
773 return 0;
774
775 return wmm_queue_map[ac];
776 }
777 EXPORT_SYMBOL_GPL(mt76_ac_to_hwq);
778
mt76_skb_adjust_pad(struct sk_buff * skb,int pad)779 int mt76_skb_adjust_pad(struct sk_buff *skb, int pad)
780 {
781 struct sk_buff *iter, *last = skb;
782
783 /* First packet of a A-MSDU burst keeps track of the whole burst
784 * length, need to update length of it and the last packet.
785 */
786 skb_walk_frags(skb, iter) {
787 last = iter;
788 if (!iter->next) {
789 skb->data_len += pad;
790 skb->len += pad;
791 break;
792 }
793 }
794
795 if (skb_pad(last, pad))
796 return -ENOMEM;
797
798 __skb_put(last, pad);
799
800 return 0;
801 }
802 EXPORT_SYMBOL_GPL(mt76_skb_adjust_pad);
803
mt76_queue_tx_complete(struct mt76_dev * dev,struct mt76_queue * q,struct mt76_queue_entry * e)804 void mt76_queue_tx_complete(struct mt76_dev *dev, struct mt76_queue *q,
805 struct mt76_queue_entry *e)
806 {
807 if (e->skb)
808 dev->drv->tx_complete_skb(dev, e);
809
810 spin_lock_bh(&q->lock);
811 q->tail = (q->tail + 1) % q->ndesc;
812 q->queued--;
813 spin_unlock_bh(&q->lock);
814 }
815 EXPORT_SYMBOL_GPL(mt76_queue_tx_complete);
816
__mt76_set_tx_blocked(struct mt76_dev * dev,bool blocked)817 void __mt76_set_tx_blocked(struct mt76_dev *dev, bool blocked)
818 {
819 struct mt76_phy *phy = &dev->phy;
820 struct mt76_queue *q = phy->q_tx[0];
821
822 if (blocked == q->blocked)
823 return;
824
825 q->blocked = blocked;
826
827 phy = dev->phys[MT_BAND1];
828 if (phy) {
829 q = phy->q_tx[0];
830 q->blocked = blocked;
831 }
832 phy = dev->phys[MT_BAND2];
833 if (phy) {
834 q = phy->q_tx[0];
835 q->blocked = blocked;
836 }
837
838 if (!blocked)
839 mt76_worker_schedule(&dev->tx_worker);
840 }
841 EXPORT_SYMBOL_GPL(__mt76_set_tx_blocked);
842
mt76_token_consume(struct mt76_dev * dev,struct mt76_txwi_cache ** ptxwi)843 int mt76_token_consume(struct mt76_dev *dev, struct mt76_txwi_cache **ptxwi)
844 {
845 int token;
846
847 spin_lock_bh(&dev->token_lock);
848
849 token = idr_alloc(&dev->token, *ptxwi, 0, dev->token_size, GFP_ATOMIC);
850 if (token >= 0)
851 dev->token_count++;
852
853 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
854 if (mtk_wed_device_active(&dev->mmio.wed) &&
855 token >= dev->mmio.wed.wlan.token_start)
856 dev->wed_token_count++;
857 #endif
858
859 if (dev->token_count >= dev->token_size - MT76_TOKEN_FREE_THR)
860 __mt76_set_tx_blocked(dev, true);
861
862 spin_unlock_bh(&dev->token_lock);
863
864 return token;
865 }
866 EXPORT_SYMBOL_GPL(mt76_token_consume);
867
mt76_rx_token_consume(struct mt76_dev * dev,void * ptr,struct mt76_txwi_cache * t,dma_addr_t phys)868 int mt76_rx_token_consume(struct mt76_dev *dev, void *ptr,
869 struct mt76_txwi_cache *t, dma_addr_t phys)
870 {
871 int token;
872
873 spin_lock_bh(&dev->rx_token_lock);
874 token = idr_alloc(&dev->rx_token, t, 0, dev->rx_token_size,
875 GFP_ATOMIC);
876 if (token >= 0) {
877 t->ptr = ptr;
878 t->dma_addr = phys;
879 }
880 spin_unlock_bh(&dev->rx_token_lock);
881
882 return token;
883 }
884 EXPORT_SYMBOL_GPL(mt76_rx_token_consume);
885
886 struct mt76_txwi_cache *
mt76_token_release(struct mt76_dev * dev,int token,bool * wake)887 mt76_token_release(struct mt76_dev *dev, int token, bool *wake)
888 {
889 struct mt76_txwi_cache *txwi;
890
891 spin_lock_bh(&dev->token_lock);
892
893 txwi = idr_remove(&dev->token, token);
894 if (txwi) {
895 dev->token_count--;
896
897 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
898 if (mtk_wed_device_active(&dev->mmio.wed) &&
899 token >= dev->mmio.wed.wlan.token_start &&
900 --dev->wed_token_count == 0)
901 wake_up(&dev->tx_wait);
902 #endif
903 }
904
905 if (dev->token_count < dev->token_size - MT76_TOKEN_FREE_THR &&
906 dev->phy.q_tx[0]->blocked)
907 *wake = true;
908
909 spin_unlock_bh(&dev->token_lock);
910
911 return txwi;
912 }
913 EXPORT_SYMBOL_GPL(mt76_token_release);
914
915 struct mt76_txwi_cache *
mt76_rx_token_release(struct mt76_dev * dev,int token)916 mt76_rx_token_release(struct mt76_dev *dev, int token)
917 {
918 struct mt76_txwi_cache *t;
919
920 spin_lock_bh(&dev->rx_token_lock);
921 t = idr_remove(&dev->rx_token, token);
922 spin_unlock_bh(&dev->rx_token_lock);
923
924 return t;
925 }
926 EXPORT_SYMBOL_GPL(mt76_rx_token_release);
927