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
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
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
40 mt76_tx_status_lock(struct mt76_dev *dev, struct sk_buff_head *list)
41 		   __acquires(&dev->status_list.lock)
42 {
43 	__skb_queue_head_init(list);
44 	spin_lock_bh(&dev->status_list.lock);
45 }
46 EXPORT_SYMBOL_GPL(mt76_tx_status_lock);
47 
48 void
49 mt76_tx_status_unlock(struct mt76_dev *dev, struct sk_buff_head *list)
50 		      __releases(&dev->status_list.lock)
51 {
52 	struct ieee80211_hw *hw;
53 	struct sk_buff *skb;
54 
55 	spin_unlock_bh(&dev->status_list.lock);
56 
57 	while ((skb = __skb_dequeue(list)) != NULL) {
58 		hw = mt76_tx_status_get_hw(dev, skb);
59 		ieee80211_tx_status(hw, skb);
60 	}
61 
62 }
63 EXPORT_SYMBOL_GPL(mt76_tx_status_unlock);
64 
65 static void
66 __mt76_tx_status_skb_done(struct mt76_dev *dev, struct sk_buff *skb, u8 flags,
67 			  struct sk_buff_head *list)
68 {
69 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
70 	struct mt76_tx_cb *cb = mt76_tx_skb_cb(skb);
71 	u8 done = MT_TX_CB_DMA_DONE | MT_TX_CB_TXS_DONE;
72 
73 	flags |= cb->flags;
74 	cb->flags = flags;
75 
76 	if ((flags & done) != done)
77 		return;
78 
79 	__skb_unlink(skb, &dev->status_list);
80 
81 	/* Tx status can be unreliable. if it fails, mark the frame as ACKed */
82 	if (flags & MT_TX_CB_TXS_FAILED) {
83 		ieee80211_tx_info_clear_status(info);
84 		info->status.rates[0].idx = -1;
85 		info->flags |= IEEE80211_TX_STAT_ACK;
86 	}
87 
88 	__skb_queue_tail(list, skb);
89 }
90 
91 void
92 mt76_tx_status_skb_done(struct mt76_dev *dev, struct sk_buff *skb,
93 			struct sk_buff_head *list)
94 {
95 	__mt76_tx_status_skb_done(dev, skb, MT_TX_CB_TXS_DONE, list);
96 }
97 EXPORT_SYMBOL_GPL(mt76_tx_status_skb_done);
98 
99 int
100 mt76_tx_status_skb_add(struct mt76_dev *dev, struct mt76_wcid *wcid,
101 		       struct sk_buff *skb)
102 {
103 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
104 	struct mt76_tx_cb *cb = mt76_tx_skb_cb(skb);
105 	int pid;
106 
107 	if (!wcid)
108 		return MT_PACKET_ID_NO_ACK;
109 
110 	if (info->flags & IEEE80211_TX_CTL_NO_ACK)
111 		return MT_PACKET_ID_NO_ACK;
112 
113 	if (!(info->flags & (IEEE80211_TX_CTL_REQ_TX_STATUS |
114 			     IEEE80211_TX_CTL_RATE_CTRL_PROBE)))
115 		return MT_PACKET_ID_NO_SKB;
116 
117 	spin_lock_bh(&dev->status_list.lock);
118 
119 	memset(cb, 0, sizeof(*cb));
120 	wcid->packet_id = (wcid->packet_id + 1) & MT_PACKET_ID_MASK;
121 	if (wcid->packet_id == MT_PACKET_ID_NO_ACK ||
122 	    wcid->packet_id == MT_PACKET_ID_NO_SKB)
123 		wcid->packet_id = MT_PACKET_ID_FIRST;
124 
125 	pid = wcid->packet_id;
126 	cb->wcid = wcid->idx;
127 	cb->pktid = pid;
128 	cb->jiffies = jiffies;
129 
130 	__skb_queue_tail(&dev->status_list, skb);
131 	spin_unlock_bh(&dev->status_list.lock);
132 
133 	return pid;
134 }
135 EXPORT_SYMBOL_GPL(mt76_tx_status_skb_add);
136 
137 struct sk_buff *
138 mt76_tx_status_skb_get(struct mt76_dev *dev, struct mt76_wcid *wcid, int pktid,
139 		       struct sk_buff_head *list)
140 {
141 	struct sk_buff *skb, *tmp;
142 
143 	skb_queue_walk_safe(&dev->status_list, skb, tmp) {
144 		struct mt76_tx_cb *cb = mt76_tx_skb_cb(skb);
145 
146 		if (wcid && cb->wcid != wcid->idx)
147 			continue;
148 
149 		if (cb->pktid == pktid)
150 			return skb;
151 
152 		if (pktid >= 0 && !time_after(jiffies, cb->jiffies +
153 					      MT_TX_STATUS_SKB_TIMEOUT))
154 			continue;
155 
156 		__mt76_tx_status_skb_done(dev, skb, MT_TX_CB_TXS_FAILED |
157 						    MT_TX_CB_TXS_DONE, list);
158 	}
159 
160 	return NULL;
161 }
162 EXPORT_SYMBOL_GPL(mt76_tx_status_skb_get);
163 
164 void
165 mt76_tx_status_check(struct mt76_dev *dev, struct mt76_wcid *wcid, bool flush)
166 {
167 	struct sk_buff_head list;
168 
169 	mt76_tx_status_lock(dev, &list);
170 	mt76_tx_status_skb_get(dev, wcid, flush ? -1 : 0, &list);
171 	mt76_tx_status_unlock(dev, &list);
172 }
173 EXPORT_SYMBOL_GPL(mt76_tx_status_check);
174 
175 static void
176 mt76_tx_check_non_aql(struct mt76_dev *dev, u16 wcid_idx, struct sk_buff *skb)
177 {
178 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
179 	struct mt76_wcid *wcid;
180 	int pending;
181 
182 	if (info->tx_time_est)
183 		return;
184 
185 	if (wcid_idx >= ARRAY_SIZE(dev->wcid))
186 		return;
187 
188 	rcu_read_lock();
189 
190 	wcid = rcu_dereference(dev->wcid[wcid_idx]);
191 	if (wcid) {
192 		pending = atomic_dec_return(&wcid->non_aql_packets);
193 		if (pending < 0)
194 			atomic_cmpxchg(&wcid->non_aql_packets, pending, 0);
195 	}
196 
197 	rcu_read_unlock();
198 }
199 
200 void mt76_tx_complete_skb(struct mt76_dev *dev, u16 wcid_idx, struct sk_buff *skb)
201 {
202 	struct ieee80211_hw *hw;
203 	struct sk_buff_head list;
204 
205 #ifdef CONFIG_NL80211_TESTMODE
206 	if (skb == dev->test.tx_skb) {
207 		dev->test.tx_done++;
208 		if (dev->test.tx_queued == dev->test.tx_done)
209 			wake_up(&dev->tx_wait);
210 	}
211 #endif
212 
213 	mt76_tx_check_non_aql(dev, wcid_idx, skb);
214 
215 	if (!skb->prev) {
216 		hw = mt76_tx_status_get_hw(dev, skb);
217 		ieee80211_free_txskb(hw, skb);
218 		return;
219 	}
220 
221 	mt76_tx_status_lock(dev, &list);
222 	__mt76_tx_status_skb_done(dev, skb, MT_TX_CB_DMA_DONE, &list);
223 	mt76_tx_status_unlock(dev, &list);
224 }
225 EXPORT_SYMBOL_GPL(mt76_tx_complete_skb);
226 
227 static int
228 __mt76_tx_queue_skb(struct mt76_dev *dev, int qid, struct sk_buff *skb,
229 		    struct mt76_wcid *wcid, struct ieee80211_sta *sta,
230 		    bool *stop)
231 {
232 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
233 	struct mt76_queue *q;
234 	bool non_aql;
235 	int pending;
236 	int idx;
237 
238 	non_aql = !info->tx_time_est;
239 	idx = dev->queue_ops->tx_queue_skb(dev, qid, skb, wcid, sta);
240 	if (idx < 0 || !sta || !non_aql)
241 		return idx;
242 
243 	wcid = (struct mt76_wcid *)sta->drv_priv;
244 	q = dev->q_tx[qid];
245 	q->entry[idx].wcid = wcid->idx;
246 	pending = atomic_inc_return(&wcid->non_aql_packets);
247 	if (stop && pending >= MT_MAX_NON_AQL_PKT)
248 		*stop = true;
249 
250 	return idx;
251 }
252 
253 void
254 mt76_tx(struct mt76_phy *phy, struct ieee80211_sta *sta,
255 	struct mt76_wcid *wcid, struct sk_buff *skb)
256 {
257 	struct mt76_dev *dev = phy->dev;
258 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
259 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
260 	struct mt76_queue *q;
261 	int qid = skb_get_queue_mapping(skb);
262 	bool ext_phy = phy != &dev->phy;
263 
264 	if (mt76_testmode_enabled(dev)) {
265 		ieee80211_free_txskb(phy->hw, skb);
266 		return;
267 	}
268 
269 	if (WARN_ON(qid >= MT_TXQ_PSD)) {
270 		qid = MT_TXQ_BE;
271 		skb_set_queue_mapping(skb, qid);
272 	}
273 
274 	if ((dev->drv->drv_flags & MT_DRV_HW_MGMT_TXQ) &&
275 	    !ieee80211_is_data(hdr->frame_control) &&
276 	    !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
277 		qid = MT_TXQ_PSD;
278 		skb_set_queue_mapping(skb, qid);
279 	}
280 
281 	if (!(wcid->tx_info & MT_WCID_TX_INFO_SET))
282 		ieee80211_get_tx_rates(info->control.vif, sta, skb,
283 				       info->control.rates, 1);
284 
285 	if (ext_phy)
286 		info->hw_queue |= MT_TX_HW_QUEUE_EXT_PHY;
287 
288 	q = dev->q_tx[qid];
289 
290 	spin_lock_bh(&q->lock);
291 	__mt76_tx_queue_skb(dev, qid, skb, wcid, sta, NULL);
292 	dev->queue_ops->kick(dev, q);
293 
294 	if (q->queued > q->ndesc - 8 && !q->stopped) {
295 		ieee80211_stop_queue(phy->hw, skb_get_queue_mapping(skb));
296 		q->stopped = true;
297 	}
298 
299 	spin_unlock_bh(&q->lock);
300 }
301 EXPORT_SYMBOL_GPL(mt76_tx);
302 
303 static struct sk_buff *
304 mt76_txq_dequeue(struct mt76_phy *phy, struct mt76_txq *mtxq)
305 {
306 	struct ieee80211_txq *txq = mtxq_to_txq(mtxq);
307 	struct ieee80211_tx_info *info;
308 	bool ext_phy = phy != &phy->dev->phy;
309 	struct sk_buff *skb;
310 
311 	skb = ieee80211_tx_dequeue(phy->hw, txq);
312 	if (!skb)
313 		return NULL;
314 
315 	info = IEEE80211_SKB_CB(skb);
316 	if (ext_phy)
317 		info->hw_queue |= MT_TX_HW_QUEUE_EXT_PHY;
318 
319 	return skb;
320 }
321 
322 static void
323 mt76_queue_ps_skb(struct mt76_dev *dev, struct ieee80211_sta *sta,
324 		  struct sk_buff *skb, bool last)
325 {
326 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
327 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
328 
329 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
330 	if (last)
331 		info->flags |= IEEE80211_TX_STATUS_EOSP |
332 			       IEEE80211_TX_CTL_REQ_TX_STATUS;
333 
334 	mt76_skb_set_moredata(skb, !last);
335 	__mt76_tx_queue_skb(dev, MT_TXQ_PSD, skb, wcid, sta, NULL);
336 }
337 
338 void
339 mt76_release_buffered_frames(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
340 			     u16 tids, int nframes,
341 			     enum ieee80211_frame_release_type reason,
342 			     bool more_data)
343 {
344 	struct mt76_phy *phy = hw->priv;
345 	struct mt76_dev *dev = phy->dev;
346 	struct sk_buff *last_skb = NULL;
347 	struct mt76_queue *hwq = dev->q_tx[MT_TXQ_PSD];
348 	int i;
349 
350 	spin_lock_bh(&hwq->lock);
351 	for (i = 0; tids && nframes; i++, tids >>= 1) {
352 		struct ieee80211_txq *txq = sta->txq[i];
353 		struct mt76_txq *mtxq = (struct mt76_txq *)txq->drv_priv;
354 		struct sk_buff *skb;
355 
356 		if (!(tids & 1))
357 			continue;
358 
359 		do {
360 			skb = mt76_txq_dequeue(phy, mtxq);
361 			if (!skb)
362 				break;
363 
364 			nframes--;
365 			if (last_skb)
366 				mt76_queue_ps_skb(dev, sta, last_skb, false);
367 
368 			last_skb = skb;
369 		} while (nframes);
370 	}
371 
372 	if (last_skb) {
373 		mt76_queue_ps_skb(dev, sta, last_skb, true);
374 		dev->queue_ops->kick(dev, hwq);
375 	} else {
376 		ieee80211_sta_eosp(sta);
377 	}
378 
379 	spin_unlock_bh(&hwq->lock);
380 }
381 EXPORT_SYMBOL_GPL(mt76_release_buffered_frames);
382 
383 static int
384 mt76_txq_send_burst(struct mt76_phy *phy, struct mt76_queue *q,
385 		    struct mt76_txq *mtxq)
386 {
387 	struct mt76_dev *dev = phy->dev;
388 	struct ieee80211_txq *txq = mtxq_to_txq(mtxq);
389 	enum mt76_txq_id qid = mt76_txq_get_qid(txq);
390 	struct mt76_wcid *wcid = mtxq->wcid;
391 	struct ieee80211_tx_info *info;
392 	struct sk_buff *skb;
393 	int n_frames = 1;
394 	bool stop = false;
395 	int idx;
396 
397 	if (test_bit(MT_WCID_FLAG_PS, &wcid->flags))
398 		return 0;
399 
400 	if (atomic_read(&wcid->non_aql_packets) >= MT_MAX_NON_AQL_PKT)
401 		return 0;
402 
403 	skb = mt76_txq_dequeue(phy, mtxq);
404 	if (!skb)
405 		return 0;
406 
407 	info = IEEE80211_SKB_CB(skb);
408 	if (!(wcid->tx_info & MT_WCID_TX_INFO_SET))
409 		ieee80211_get_tx_rates(txq->vif, txq->sta, skb,
410 				       info->control.rates, 1);
411 
412 	idx = __mt76_tx_queue_skb(dev, qid, skb, wcid, txq->sta, &stop);
413 	if (idx < 0)
414 		return idx;
415 
416 	do {
417 		if (test_bit(MT76_STATE_PM, &phy->state) ||
418 		    test_bit(MT76_RESET, &phy->state))
419 			return -EBUSY;
420 
421 		if (stop)
422 			break;
423 
424 		if (q->queued + MT_TXQ_FREE_THR >= q->ndesc)
425 			break;
426 
427 		skb = mt76_txq_dequeue(phy, mtxq);
428 		if (!skb)
429 			break;
430 
431 		info = IEEE80211_SKB_CB(skb);
432 		if (!(wcid->tx_info & MT_WCID_TX_INFO_SET))
433 			ieee80211_get_tx_rates(txq->vif, txq->sta, skb,
434 					       info->control.rates, 1);
435 
436 		idx = __mt76_tx_queue_skb(dev, qid, skb, wcid, txq->sta, &stop);
437 		if (idx < 0)
438 			break;
439 
440 		n_frames++;
441 	} while (1);
442 
443 	dev->queue_ops->kick(dev, q);
444 
445 	return n_frames;
446 }
447 
448 static int
449 mt76_txq_schedule_list(struct mt76_phy *phy, enum mt76_txq_id qid)
450 {
451 	struct mt76_dev *dev = phy->dev;
452 	struct mt76_queue *q = dev->q_tx[qid];
453 	struct ieee80211_txq *txq;
454 	struct mt76_txq *mtxq;
455 	struct mt76_wcid *wcid;
456 	int ret = 0;
457 
458 	spin_lock_bh(&q->lock);
459 	while (1) {
460 		if (test_bit(MT76_STATE_PM, &phy->state) ||
461 		    test_bit(MT76_RESET, &phy->state)) {
462 			ret = -EBUSY;
463 			break;
464 		}
465 
466 		if (q->queued + MT_TXQ_FREE_THR >= q->ndesc)
467 			break;
468 
469 		txq = ieee80211_next_txq(phy->hw, qid);
470 		if (!txq)
471 			break;
472 
473 		mtxq = (struct mt76_txq *)txq->drv_priv;
474 		wcid = mtxq->wcid;
475 		if (wcid && test_bit(MT_WCID_FLAG_PS, &wcid->flags))
476 			continue;
477 
478 		if (mtxq->send_bar && mtxq->aggr) {
479 			struct ieee80211_txq *txq = mtxq_to_txq(mtxq);
480 			struct ieee80211_sta *sta = txq->sta;
481 			struct ieee80211_vif *vif = txq->vif;
482 			u16 agg_ssn = mtxq->agg_ssn;
483 			u8 tid = txq->tid;
484 
485 			mtxq->send_bar = false;
486 			spin_unlock_bh(&q->lock);
487 			ieee80211_send_bar(vif, sta->addr, tid, agg_ssn);
488 			spin_lock_bh(&q->lock);
489 		}
490 
491 		ret += mt76_txq_send_burst(phy, q, mtxq);
492 		ieee80211_return_txq(phy->hw, txq, false);
493 	}
494 	spin_unlock_bh(&q->lock);
495 
496 	return ret;
497 }
498 
499 void mt76_txq_schedule(struct mt76_phy *phy, enum mt76_txq_id qid)
500 {
501 	int len;
502 
503 	if (qid >= 4)
504 		return;
505 
506 	rcu_read_lock();
507 
508 	do {
509 		ieee80211_txq_schedule_start(phy->hw, qid);
510 		len = mt76_txq_schedule_list(phy, qid);
511 		ieee80211_txq_schedule_end(phy->hw, qid);
512 	} while (len > 0);
513 
514 	rcu_read_unlock();
515 }
516 EXPORT_SYMBOL_GPL(mt76_txq_schedule);
517 
518 void mt76_txq_schedule_all(struct mt76_phy *phy)
519 {
520 	int i;
521 
522 	for (i = 0; i <= MT_TXQ_BK; i++)
523 		mt76_txq_schedule(phy, i);
524 }
525 EXPORT_SYMBOL_GPL(mt76_txq_schedule_all);
526 
527 void mt76_tx_worker(struct mt76_worker *w)
528 {
529 	struct mt76_dev *dev = container_of(w, struct mt76_dev, tx_worker);
530 
531 	mt76_txq_schedule_all(&dev->phy);
532 	if (dev->phy2)
533 		mt76_txq_schedule_all(dev->phy2);
534 
535 #ifdef CONFIG_NL80211_TESTMODE
536 	if (dev->test.tx_pending)
537 		mt76_testmode_tx_pending(dev);
538 #endif
539 }
540 
541 void mt76_stop_tx_queues(struct mt76_dev *dev, struct ieee80211_sta *sta,
542 			 bool send_bar)
543 {
544 	int i;
545 
546 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
547 		struct ieee80211_txq *txq = sta->txq[i];
548 		struct mt76_queue *hwq;
549 		struct mt76_txq *mtxq;
550 
551 		if (!txq)
552 			continue;
553 
554 		hwq = dev->q_tx[mt76_txq_get_qid(txq)];
555 		mtxq = (struct mt76_txq *)txq->drv_priv;
556 
557 		spin_lock_bh(&hwq->lock);
558 		mtxq->send_bar = mtxq->aggr && send_bar;
559 		spin_unlock_bh(&hwq->lock);
560 	}
561 }
562 EXPORT_SYMBOL_GPL(mt76_stop_tx_queues);
563 
564 void mt76_wake_tx_queue(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
565 {
566 	struct mt76_phy *phy = hw->priv;
567 	struct mt76_dev *dev = phy->dev;
568 
569 	if (!test_bit(MT76_STATE_RUNNING, &phy->state))
570 		return;
571 
572 	mt76_worker_schedule(&dev->tx_worker);
573 }
574 EXPORT_SYMBOL_GPL(mt76_wake_tx_queue);
575 
576 u8 mt76_ac_to_hwq(u8 ac)
577 {
578 	static const u8 wmm_queue_map[] = {
579 		[IEEE80211_AC_BE] = 0,
580 		[IEEE80211_AC_BK] = 1,
581 		[IEEE80211_AC_VI] = 2,
582 		[IEEE80211_AC_VO] = 3,
583 	};
584 
585 	if (WARN_ON(ac >= IEEE80211_NUM_ACS))
586 		return 0;
587 
588 	return wmm_queue_map[ac];
589 }
590 EXPORT_SYMBOL_GPL(mt76_ac_to_hwq);
591 
592 int mt76_skb_adjust_pad(struct sk_buff *skb, int pad)
593 {
594 	struct sk_buff *iter, *last = skb;
595 
596 	/* First packet of a A-MSDU burst keeps track of the whole burst
597 	 * length, need to update length of it and the last packet.
598 	 */
599 	skb_walk_frags(skb, iter) {
600 		last = iter;
601 		if (!iter->next) {
602 			skb->data_len += pad;
603 			skb->len += pad;
604 			break;
605 		}
606 	}
607 
608 	if (skb_pad(last, pad))
609 		return -ENOMEM;
610 
611 	__skb_put(last, pad);
612 
613 	return 0;
614 }
615 EXPORT_SYMBOL_GPL(mt76_skb_adjust_pad);
616 
617 void mt76_queue_tx_complete(struct mt76_dev *dev, struct mt76_queue *q,
618 			    struct mt76_queue_entry *e)
619 {
620 	if (e->skb)
621 		dev->drv->tx_complete_skb(dev, e);
622 
623 	spin_lock_bh(&q->lock);
624 	q->tail = (q->tail + 1) % q->ndesc;
625 	q->queued--;
626 	spin_unlock_bh(&q->lock);
627 }
628 EXPORT_SYMBOL_GPL(mt76_queue_tx_complete);
629