1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * HT handling
4 *
5 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
6 * Copyright 2002-2005, Instant802 Networks, Inc.
7 * Copyright 2005-2006, Devicescape Software, Inc.
8 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
9 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
10 * Copyright 2007-2010, Intel Corporation
11 * Copyright(c) 2015-2017 Intel Deutschland GmbH
12 * Copyright (C) 2018-2026 Intel Corporation
13 */
14
15 #include <linux/ieee80211.h>
16 #include <linux/slab.h>
17 #include <linux/export.h>
18 #include <net/mac80211.h>
19 #include "ieee80211_i.h"
20 #include "driver-ops.h"
21 #include "wme.h"
22
23 /**
24 * DOC: TX A-MPDU aggregation
25 *
26 * Aggregation on the TX side requires setting the hardware flag
27 * %IEEE80211_HW_AMPDU_AGGREGATION. The driver will then be handed
28 * packets with a flag indicating A-MPDU aggregation. The driver
29 * or device is responsible for actually aggregating the frames,
30 * as well as deciding how many and which to aggregate.
31 *
32 * When TX aggregation is started by some subsystem (usually the rate
33 * control algorithm would be appropriate) by calling the
34 * ieee80211_start_tx_ba_session() function, the driver will be
35 * notified via its @ampdu_action function, with the
36 * %IEEE80211_AMPDU_TX_START action.
37 *
38 * In response to that, the driver is later required to call the
39 * ieee80211_start_tx_ba_cb_irqsafe() function, which will really
40 * start the aggregation session after the peer has also responded.
41 * If the peer responds negatively, the session will be stopped
42 * again right away. Note that it is possible for the aggregation
43 * session to be stopped before the driver has indicated that it
44 * is done setting it up, in which case it must not indicate the
45 * setup completion.
46 *
47 * Also note that, since we also need to wait for a response from
48 * the peer, the driver is notified of the completion of the
49 * handshake by the %IEEE80211_AMPDU_TX_OPERATIONAL action to the
50 * @ampdu_action callback.
51 *
52 * Similarly, when the aggregation session is stopped by the peer
53 * or something calling ieee80211_stop_tx_ba_session(), the driver's
54 * @ampdu_action function will be called with the action
55 * %IEEE80211_AMPDU_TX_STOP. In this case, the call must not fail,
56 * and the driver must later call ieee80211_stop_tx_ba_cb_irqsafe().
57 * Note that the sta can get destroyed before the BA tear down is
58 * complete.
59 */
60
ieee80211_send_addba_request(struct sta_info * sta,u16 tid,u8 dialog_token,u16 start_seq_num,u16 agg_size,u16 timeout,bool ndp)61 static void ieee80211_send_addba_request(struct sta_info *sta, u16 tid,
62 u8 dialog_token, u16 start_seq_num,
63 u16 agg_size, u16 timeout, bool ndp)
64 {
65 struct ieee80211_sub_if_data *sdata = sta->sdata;
66 struct ieee80211_local *local = sdata->local;
67 struct sk_buff *skb;
68 struct ieee80211_mgmt *mgmt;
69 u16 capab;
70
71 skb = dev_alloc_skb(IEEE80211_MIN_ACTION_SIZE(addba_req) +
72 2 + sizeof(struct ieee80211_addba_ext_ie) +
73 local->hw.extra_tx_headroom);
74 if (!skb)
75 return;
76
77 skb_reserve(skb, local->hw.extra_tx_headroom);
78 mgmt = ieee80211_mgmt_ba(skb, sta->sta.addr, sdata);
79
80 skb_put(skb, 2 + sizeof(mgmt->u.action.addba_req));
81
82 mgmt->u.action.category = WLAN_CATEGORY_BACK;
83 mgmt->u.action.action_code = ndp ?
84 WLAN_ACTION_NDP_ADDBA_REQ : WLAN_ACTION_ADDBA_REQ;
85
86 mgmt->u.action.addba_req.dialog_token = dialog_token;
87 capab = IEEE80211_ADDBA_PARAM_AMSDU_MASK;
88 capab |= IEEE80211_ADDBA_PARAM_POLICY_MASK;
89 capab |= u16_encode_bits(tid, IEEE80211_ADDBA_PARAM_TID_MASK);
90 capab |= u16_encode_bits(agg_size, IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK);
91
92 mgmt->u.action.addba_req.capab = cpu_to_le16(capab);
93
94 mgmt->u.action.addba_req.timeout = cpu_to_le16(timeout);
95 mgmt->u.action.addba_req.start_seq_num =
96 cpu_to_le16(start_seq_num << 4);
97
98 if (sta->sta.deflink.he_cap.has_he)
99 ieee80211_add_addbaext(skb, 0, agg_size);
100
101 ieee80211_tx_skb_tid(sdata, skb, tid, -1);
102 }
103
ieee80211_send_bar(struct ieee80211_vif * vif,u8 * ra,u16 tid,u16 ssn)104 void ieee80211_send_bar(struct ieee80211_vif *vif, u8 *ra, u16 tid, u16 ssn)
105 {
106 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
107 struct ieee80211_local *local = sdata->local;
108 struct sk_buff *skb;
109 struct ieee80211_bar *bar;
110 u16 bar_control = 0;
111
112 skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
113 if (!skb)
114 return;
115
116 skb_reserve(skb, local->hw.extra_tx_headroom);
117 bar = skb_put_zero(skb, sizeof(*bar));
118 bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
119 IEEE80211_STYPE_BACK_REQ);
120 memcpy(bar->ra, ra, ETH_ALEN);
121 memcpy(bar->ta, sdata->vif.addr, ETH_ALEN);
122 bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
123 bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
124 bar_control |= (u16)(tid << IEEE80211_BAR_CTRL_TID_INFO_SHIFT);
125 bar->control = cpu_to_le16(bar_control);
126 bar->start_seq_num = cpu_to_le16(ssn);
127
128 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
129 IEEE80211_TX_CTL_REQ_TX_STATUS;
130 ieee80211_tx_skb_tid(sdata, skb, tid, -1);
131 }
132 EXPORT_SYMBOL(ieee80211_send_bar);
133
ieee80211_assign_tid_tx(struct sta_info * sta,int tid,struct tid_ampdu_tx * tid_tx)134 void ieee80211_assign_tid_tx(struct sta_info *sta, int tid,
135 struct tid_ampdu_tx *tid_tx)
136 {
137 lockdep_assert_wiphy(sta->local->hw.wiphy);
138 lockdep_assert_held(&sta->lock);
139 rcu_assign_pointer(sta->ampdu_mlme.tid_tx[tid], tid_tx);
140 }
141
142 /*
143 * When multiple aggregation sessions on multiple stations
144 * are being created/destroyed simultaneously, we need to
145 * refcount the global queue stop caused by that in order
146 * to not get into a situation where one of the aggregation
147 * setup or teardown re-enables queues before the other is
148 * ready to handle that.
149 *
150 * These two functions take care of this issue by keeping
151 * a global "agg_queue_stop" refcount.
152 */
__acquires(agg_queue)153 static void __acquires(agg_queue)
154 ieee80211_stop_queue_agg(struct ieee80211_sub_if_data *sdata, int tid)
155 {
156 int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
157
158 /* we do refcounting here, so don't use the queue reason refcounting */
159
160 if (atomic_inc_return(&sdata->local->agg_queue_stop[queue]) == 1)
161 ieee80211_stop_queue_by_reason(
162 &sdata->local->hw, queue,
163 IEEE80211_QUEUE_STOP_REASON_AGGREGATION,
164 false);
165 __acquire(agg_queue);
166 }
167
__releases(agg_queue)168 static void __releases(agg_queue)
169 ieee80211_wake_queue_agg(struct ieee80211_sub_if_data *sdata, int tid)
170 {
171 int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
172
173 if (atomic_dec_return(&sdata->local->agg_queue_stop[queue]) == 0)
174 ieee80211_wake_queue_by_reason(
175 &sdata->local->hw, queue,
176 IEEE80211_QUEUE_STOP_REASON_AGGREGATION,
177 false);
178 __release(agg_queue);
179 }
180
181 static void
ieee80211_agg_stop_txq(struct sta_info * sta,int tid)182 ieee80211_agg_stop_txq(struct sta_info *sta, int tid)
183 {
184 struct ieee80211_txq *txq = sta->sta.txq[tid];
185 struct ieee80211_sub_if_data *sdata;
186 struct fq *fq;
187 struct txq_info *txqi;
188
189 if (!txq)
190 return;
191
192 txqi = to_txq_info(txq);
193 sdata = vif_to_sdata(txq->vif);
194 fq = &sdata->local->fq;
195
196 /* Lock here to protect against further seqno updates on dequeue */
197 spin_lock_bh(&fq->lock);
198 set_bit(IEEE80211_TXQ_STOP, &txqi->flags);
199 spin_unlock_bh(&fq->lock);
200 }
201
202 static void
ieee80211_agg_start_txq(struct sta_info * sta,int tid,bool enable)203 ieee80211_agg_start_txq(struct sta_info *sta, int tid, bool enable)
204 {
205 struct ieee80211_txq *txq = sta->sta.txq[tid];
206 struct txq_info *txqi;
207
208 lockdep_assert_wiphy(sta->local->hw.wiphy);
209
210 if (!txq)
211 return;
212
213 txqi = to_txq_info(txq);
214
215 if (enable)
216 set_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
217 else
218 clear_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
219
220 clear_bit(IEEE80211_TXQ_STOP, &txqi->flags);
221 local_bh_disable();
222 rcu_read_lock();
223 schedule_and_wake_txq(sta->sdata->local, txqi);
224 rcu_read_unlock();
225 local_bh_enable();
226 }
227
228 /*
229 * splice packets from the STA's pending to the local pending,
230 * requires a call to ieee80211_agg_splice_finish later
231 */
__acquires(agg_queue)232 static void __acquires(agg_queue)
233 ieee80211_agg_splice_packets(struct ieee80211_sub_if_data *sdata,
234 struct tid_ampdu_tx *tid_tx, u16 tid)
235 {
236 struct ieee80211_local *local = sdata->local;
237 int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
238 unsigned long flags;
239
240 ieee80211_stop_queue_agg(sdata, tid);
241
242 if (WARN(!tid_tx,
243 "TID %d gone but expected when splicing aggregates from the pending queue\n",
244 tid))
245 return;
246
247 if (!skb_queue_empty(&tid_tx->pending)) {
248 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
249 /* copy over remaining packets */
250 skb_queue_splice_tail_init(&tid_tx->pending,
251 &local->pending[queue]);
252 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
253 }
254 }
255
__releases(agg_queue)256 static void __releases(agg_queue)
257 ieee80211_agg_splice_finish(struct ieee80211_sub_if_data *sdata, u16 tid)
258 {
259 ieee80211_wake_queue_agg(sdata, tid);
260 }
261
ieee80211_remove_tid_tx(struct sta_info * sta,int tid)262 static void ieee80211_remove_tid_tx(struct sta_info *sta, int tid)
263 {
264 struct tid_ampdu_tx *tid_tx;
265
266 lockdep_assert_wiphy(sta->local->hw.wiphy);
267 lockdep_assert_held(&sta->lock);
268
269 tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
270
271 /*
272 * When we get here, the TX path will not be lockless any more wrt.
273 * aggregation, since the OPERATIONAL bit has long been cleared.
274 * Thus it will block on getting the lock, if it occurs. So if we
275 * stop the queue now, we will not get any more packets, and any
276 * that might be being processed will wait for us here, thereby
277 * guaranteeing that no packets go to the tid_tx pending queue any
278 * more.
279 */
280
281 ieee80211_agg_splice_packets(sta->sdata, tid_tx, tid);
282
283 /* future packets must not find the tid_tx struct any more */
284 ieee80211_assign_tid_tx(sta, tid, NULL);
285
286 ieee80211_agg_splice_finish(sta->sdata, tid);
287
288 kfree_rcu(tid_tx, rcu_head);
289 }
290
__ieee80211_stop_tx_ba_session(struct sta_info * sta,u16 tid,enum ieee80211_agg_stop_reason reason)291 int __ieee80211_stop_tx_ba_session(struct sta_info *sta, u16 tid,
292 enum ieee80211_agg_stop_reason reason)
293 {
294 struct ieee80211_local *local = sta->local;
295 struct tid_ampdu_tx *tid_tx;
296 struct ieee80211_ampdu_params params = {
297 .sta = &sta->sta,
298 .tid = tid,
299 .buf_size = 0,
300 .amsdu = false,
301 .timeout = 0,
302 .ssn = 0,
303 };
304 int ret;
305
306 lockdep_assert_wiphy(sta->local->hw.wiphy);
307
308 switch (reason) {
309 case AGG_STOP_DECLINED:
310 case AGG_STOP_LOCAL_REQUEST:
311 case AGG_STOP_PEER_REQUEST:
312 params.action = IEEE80211_AMPDU_TX_STOP_CONT;
313 break;
314 case AGG_STOP_DESTROY_STA:
315 params.action = IEEE80211_AMPDU_TX_STOP_FLUSH;
316 break;
317 default:
318 WARN_ON_ONCE(1);
319 return -EINVAL;
320 }
321
322 spin_lock_bh(&sta->lock);
323
324 /* free struct pending for start, if present */
325 tid_tx = sta->ampdu_mlme.tid_start_tx[tid];
326 kfree(tid_tx);
327 sta->ampdu_mlme.tid_start_tx[tid] = NULL;
328
329 tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
330 if (!tid_tx) {
331 spin_unlock_bh(&sta->lock);
332 return -ENOENT;
333 }
334
335 /*
336 * if we're already stopping ignore any new requests to stop
337 * unless we're destroying it in which case notify the driver
338 */
339 if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
340 spin_unlock_bh(&sta->lock);
341 if (reason != AGG_STOP_DESTROY_STA)
342 return -EALREADY;
343 params.action = IEEE80211_AMPDU_TX_STOP_FLUSH_CONT;
344 ret = drv_ampdu_action(local, sta->sdata, ¶ms);
345 WARN_ON_ONCE(ret);
346 return 0;
347 }
348
349 if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
350 /* not even started yet! */
351 ieee80211_assign_tid_tx(sta, tid, NULL);
352 spin_unlock_bh(&sta->lock);
353 kfree_rcu(tid_tx, rcu_head);
354 return 0;
355 }
356
357 set_bit(HT_AGG_STATE_STOPPING, &tid_tx->state);
358
359 ieee80211_agg_stop_txq(sta, tid);
360
361 spin_unlock_bh(&sta->lock);
362
363 ht_dbg(sta->sdata, "Tx BA session stop requested for %pM tid %u\n",
364 sta->sta.addr, tid);
365
366 timer_delete_sync(&tid_tx->addba_resp_timer);
367 timer_delete_sync(&tid_tx->session_timer);
368
369 /*
370 * After this packets are no longer handed right through
371 * to the driver but are put onto tid_tx->pending instead,
372 * with locking to ensure proper access.
373 */
374 clear_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state);
375
376 /*
377 * There might be a few packets being processed right now (on
378 * another CPU) that have already gotten past the aggregation
379 * check when it was still OPERATIONAL and consequently have
380 * IEEE80211_TX_CTL_AMPDU set. In that case, this code might
381 * call into the driver at the same time or even before the
382 * TX paths calls into it, which could confuse the driver.
383 *
384 * Wait for all currently running TX paths to finish before
385 * telling the driver. New packets will not go through since
386 * the aggregation session is no longer OPERATIONAL.
387 */
388 if (!local->in_reconfig)
389 synchronize_net();
390
391 tid_tx->stop_initiator = reason == AGG_STOP_PEER_REQUEST ?
392 WLAN_BACK_RECIPIENT :
393 WLAN_BACK_INITIATOR;
394 tid_tx->tx_stop = reason == AGG_STOP_LOCAL_REQUEST;
395
396 ret = drv_ampdu_action(local, sta->sdata, ¶ms);
397
398 /* HW shall not deny going back to legacy */
399 if (WARN_ON(ret)) {
400 /*
401 * We may have pending packets get stuck in this case...
402 * Not bothering with a workaround for now.
403 */
404 }
405
406 /*
407 * In the case of AGG_STOP_DESTROY_STA, the driver won't
408 * necessarily call ieee80211_stop_tx_ba_cb(), so this may
409 * seem like we can leave the tid_tx data pending forever.
410 * This is true, in a way, but "forever" is only until the
411 * station struct is actually destroyed. In the meantime,
412 * leaving it around ensures that we don't transmit packets
413 * to the driver on this TID which might confuse it.
414 */
415
416 return 0;
417 }
418
419 /*
420 * After sending add Block Ack request we activated a timer until
421 * add Block Ack response will arrive from the recipient.
422 * If this timer expires sta_addba_resp_timer_expired will be executed.
423 */
sta_addba_resp_timer_expired(struct timer_list * t)424 static void sta_addba_resp_timer_expired(struct timer_list *t)
425 {
426 struct tid_ampdu_tx *tid_tx = timer_container_of(tid_tx, t,
427 addba_resp_timer);
428 struct sta_info *sta = tid_tx->sta;
429 u8 tid = tid_tx->tid;
430
431 /* check if the TID waits for addBA response */
432 if (test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state)) {
433 ht_dbg(sta->sdata,
434 "timer expired on %pM tid %d not expecting addBA response\n",
435 sta->sta.addr, tid);
436 return;
437 }
438
439 ht_dbg(sta->sdata, "addBA response timer expired on %pM tid %d\n",
440 sta->sta.addr, tid);
441
442 ieee80211_stop_tx_ba_session(&sta->sta, tid);
443 }
444
ieee80211_send_addba_with_timeout(struct sta_info * sta,struct tid_ampdu_tx * tid_tx)445 static void ieee80211_send_addba_with_timeout(struct sta_info *sta,
446 struct tid_ampdu_tx *tid_tx)
447 {
448 struct ieee80211_sub_if_data *sdata = sta->sdata;
449 struct ieee80211_local *local = sta->local;
450 u8 tid = tid_tx->tid;
451 u16 buf_size;
452
453 if (WARN_ON_ONCE(test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state) ||
454 test_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state)))
455 return;
456
457 lockdep_assert_wiphy(sta->local->hw.wiphy);
458
459 /* activate the timer for the recipient's addBA response */
460 mod_timer(&tid_tx->addba_resp_timer, jiffies + ADDBA_RESP_INTERVAL);
461 ht_dbg(sdata, "activated addBA response timer on %pM tid %d\n",
462 sta->sta.addr, tid);
463
464 spin_lock_bh(&sta->lock);
465 sta->ampdu_mlme.last_addba_req_time[tid] = jiffies;
466 sta->ampdu_mlme.addba_req_num[tid]++;
467 spin_unlock_bh(&sta->lock);
468
469 if (sta->sta.valid_links ||
470 sta->sta.deflink.eht_cap.has_eht ||
471 ieee80211_hw_check(&local->hw, STRICT)) {
472 buf_size = local->hw.max_tx_aggregation_subframes;
473 } else if (sta->sta.deflink.he_cap.has_he) {
474 buf_size = min_t(u16, local->hw.max_tx_aggregation_subframes,
475 IEEE80211_MAX_AMPDU_BUF_HE);
476 } else {
477 /*
478 * We really should use what the driver told us it will
479 * transmit as the maximum, but certain APs (e.g. the
480 * LinkSys WRT120N with FW v1.0.07 build 002 Jun 18 2012)
481 * will crash when we use a lower number.
482 */
483 buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
484 }
485
486 /* send AddBA request */
487 ieee80211_send_addba_request(sta, tid, tid_tx->dialog_token,
488 tid_tx->ssn, buf_size, tid_tx->timeout,
489 tid_tx->ndp);
490
491 WARN_ON(test_and_set_bit(HT_AGG_STATE_SENT_ADDBA, &tid_tx->state));
492 }
493
ieee80211_tx_ba_session_handle_start(struct sta_info * sta,int tid)494 void ieee80211_tx_ba_session_handle_start(struct sta_info *sta, int tid)
495 {
496 struct tid_ampdu_tx *tid_tx;
497 struct ieee80211_local *local = sta->local;
498 struct ieee80211_sub_if_data *sdata = sta->sdata;
499 struct ieee80211_ampdu_params params = {
500 .sta = &sta->sta,
501 .action = IEEE80211_AMPDU_TX_START,
502 .tid = tid,
503 .buf_size = 0,
504 .amsdu = false,
505 .timeout = 0,
506 };
507 int ret;
508
509 tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
510
511 /*
512 * Start queuing up packets for this aggregation session.
513 * We're going to release them once the driver is OK with
514 * that.
515 */
516 clear_bit(HT_AGG_STATE_WANT_START, &tid_tx->state);
517
518 /*
519 * Make sure no packets are being processed. This ensures that
520 * we have a valid starting sequence number and that in-flight
521 * packets have been flushed out and no packets for this TID
522 * will go into the driver during the ampdu_action call.
523 */
524 synchronize_net();
525
526 tid_tx->ndp = ieee80211_s1g_use_ndp_ba(sdata, sta);
527 params.ssn = sta->tid_seq[tid] >> 4;
528 ret = drv_ampdu_action(local, sdata, ¶ms);
529 tid_tx->ssn = params.ssn;
530 if (ret == IEEE80211_AMPDU_TX_START_DELAY_ADDBA) {
531 return;
532 } else if (ret == IEEE80211_AMPDU_TX_START_IMMEDIATE) {
533 /*
534 * We didn't send the request yet, so don't need to check
535 * here if we already got a response, just mark as driver
536 * ready immediately.
537 */
538 set_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state);
539 } else if (ret) {
540 ht_dbg(sdata,
541 "BA request denied - HW unavailable for %pM tid %d\n",
542 sta->sta.addr, tid);
543 spin_lock_bh(&sta->lock);
544 ieee80211_agg_splice_packets(sdata, tid_tx, tid);
545 ieee80211_assign_tid_tx(sta, tid, NULL);
546 ieee80211_agg_splice_finish(sdata, tid);
547 spin_unlock_bh(&sta->lock);
548
549 ieee80211_agg_start_txq(sta, tid, false);
550
551 kfree_rcu(tid_tx, rcu_head);
552 return;
553 }
554
555 ieee80211_send_addba_with_timeout(sta, tid_tx);
556 }
557
ieee80211_refresh_tx_agg_session_timer(struct ieee80211_sta * pubsta,u16 tid)558 void ieee80211_refresh_tx_agg_session_timer(struct ieee80211_sta *pubsta,
559 u16 tid)
560 {
561 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
562 struct tid_ampdu_tx *tid_tx;
563
564 if (WARN_ON_ONCE(tid >= IEEE80211_NUM_TIDS))
565 return;
566
567 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
568 if (!tid_tx)
569 return;
570
571 tid_tx->last_tx = jiffies;
572 }
573 EXPORT_SYMBOL(ieee80211_refresh_tx_agg_session_timer);
574
575 /*
576 * After accepting the AddBA Response we activated a timer,
577 * resetting it after each frame that we send.
578 */
sta_tx_agg_session_timer_expired(struct timer_list * t)579 static void sta_tx_agg_session_timer_expired(struct timer_list *t)
580 {
581 struct tid_ampdu_tx *tid_tx = timer_container_of(tid_tx, t,
582 session_timer);
583 struct sta_info *sta = tid_tx->sta;
584 u8 tid = tid_tx->tid;
585 unsigned long timeout;
586
587 if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
588 return;
589 }
590
591 timeout = tid_tx->last_tx + TU_TO_JIFFIES(tid_tx->timeout);
592 if (time_is_after_jiffies(timeout)) {
593 mod_timer(&tid_tx->session_timer, timeout);
594 return;
595 }
596
597 ht_dbg(sta->sdata, "tx session timer expired on %pM tid %d\n",
598 sta->sta.addr, tid);
599
600 ieee80211_stop_tx_ba_session(&sta->sta, tid);
601 }
602
ieee80211_start_tx_ba_session(struct ieee80211_sta * pubsta,u16 tid,u16 timeout)603 int ieee80211_start_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid,
604 u16 timeout)
605 {
606 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
607 struct ieee80211_sub_if_data *sdata = sta->sdata;
608 struct ieee80211_local *local = sdata->local;
609 struct tid_ampdu_tx *tid_tx;
610 int ret = 0;
611
612 trace_api_start_tx_ba_session(pubsta, tid);
613
614 if (WARN(sta->reserved_tid == tid,
615 "Requested to start BA session on reserved tid=%d", tid))
616 return -EINVAL;
617
618 if (!pubsta->valid_links &&
619 !pubsta->deflink.ht_cap.ht_supported &&
620 !pubsta->deflink.vht_cap.vht_supported &&
621 !pubsta->deflink.he_cap.has_he &&
622 !pubsta->deflink.eht_cap.has_eht &&
623 !pubsta->deflink.s1g_cap.s1g)
624 return -EINVAL;
625
626 if (WARN_ON_ONCE(!local->ops->ampdu_action))
627 return -EINVAL;
628
629 if ((tid >= IEEE80211_NUM_TIDS) ||
630 !ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) ||
631 ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW))
632 return -EINVAL;
633
634 if (WARN_ON(tid >= IEEE80211_FIRST_TSPEC_TSID))
635 return -EINVAL;
636
637 ht_dbg(sdata, "Open BA session requested for %pM tid %u\n",
638 pubsta->addr, tid);
639
640 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
641 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
642 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
643 sdata->vif.type != NL80211_IFTYPE_AP &&
644 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
645 sdata->vif.type != NL80211_IFTYPE_NAN_DATA)
646 return -EINVAL;
647
648 if (test_sta_flag(sta, WLAN_STA_BLOCK_BA)) {
649 ht_dbg(sdata,
650 "BA sessions blocked - Denying BA session request %pM tid %d\n",
651 sta->sta.addr, tid);
652 return -EINVAL;
653 }
654
655 if (test_sta_flag(sta, WLAN_STA_MFP) &&
656 !test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
657 ht_dbg(sdata,
658 "MFP STA not authorized - deny BA session request %pM tid %d\n",
659 sta->sta.addr, tid);
660 return -EINVAL;
661 }
662
663 /*
664 * 802.11n-2009 11.5.1.1: If the initiating STA is an HT STA, is a
665 * member of an IBSS, and has no other existing Block Ack agreement
666 * with the recipient STA, then the initiating STA shall transmit a
667 * Probe Request frame to the recipient STA and shall not transmit an
668 * ADDBA Request frame unless it receives a Probe Response frame
669 * from the recipient within dot11ADDBAFailureTimeout.
670 *
671 * The probe request mechanism for ADDBA is currently not implemented,
672 * but we only build up Block Ack session with HT STAs. This information
673 * is set when we receive a bss info from a probe response or a beacon.
674 */
675 if (sta->sdata->vif.type == NL80211_IFTYPE_ADHOC &&
676 !sta->sta.deflink.ht_cap.ht_supported) {
677 ht_dbg(sdata,
678 "BA request denied - IBSS STA %pM does not advertise HT support\n",
679 pubsta->addr);
680 return -EINVAL;
681 }
682
683 spin_lock_bh(&sta->lock);
684
685 /* we have tried too many times, receiver does not want A-MPDU */
686 if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_MAX_RETRIES) {
687 ret = -EBUSY;
688 goto err_unlock_sta;
689 }
690
691 /*
692 * if we have tried more than HT_AGG_BURST_RETRIES times we
693 * will spread our requests in time to avoid stalling connection
694 * for too long
695 */
696 if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_BURST_RETRIES &&
697 time_before(jiffies, sta->ampdu_mlme.last_addba_req_time[tid] +
698 HT_AGG_RETRIES_PERIOD)) {
699 ht_dbg(sdata,
700 "BA request denied - %d failed requests on %pM tid %u\n",
701 sta->ampdu_mlme.addba_req_num[tid], sta->sta.addr, tid);
702 ret = -EBUSY;
703 goto err_unlock_sta;
704 }
705
706 tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
707 /* check if the TID is not in aggregation flow already */
708 if (tid_tx || sta->ampdu_mlme.tid_start_tx[tid]) {
709 ht_dbg(sdata,
710 "BA request denied - session is not idle on %pM tid %u\n",
711 sta->sta.addr, tid);
712 ret = -EAGAIN;
713 goto err_unlock_sta;
714 }
715
716 /* prepare A-MPDU MLME for Tx aggregation */
717 tid_tx = kzalloc_obj(struct tid_ampdu_tx, GFP_ATOMIC);
718 if (!tid_tx) {
719 ret = -ENOMEM;
720 goto err_unlock_sta;
721 }
722
723 skb_queue_head_init(&tid_tx->pending);
724 __set_bit(HT_AGG_STATE_WANT_START, &tid_tx->state);
725
726 tid_tx->timeout = timeout;
727 tid_tx->sta = sta;
728 tid_tx->tid = tid;
729
730 /* response timer */
731 timer_setup(&tid_tx->addba_resp_timer, sta_addba_resp_timer_expired, 0);
732
733 /* tx timer */
734 timer_setup(&tid_tx->session_timer,
735 sta_tx_agg_session_timer_expired, TIMER_DEFERRABLE);
736
737 /* assign a dialog token */
738 sta->ampdu_mlme.dialog_token_allocator++;
739 tid_tx->dialog_token = sta->ampdu_mlme.dialog_token_allocator;
740
741 /*
742 * Finally, assign it to the start array; the work item will
743 * collect it and move it to the normal array.
744 */
745 sta->ampdu_mlme.tid_start_tx[tid] = tid_tx;
746
747 wiphy_work_queue(local->hw.wiphy, &sta->ampdu_mlme.work);
748
749 /* this flow continues off the work */
750 err_unlock_sta:
751 spin_unlock_bh(&sta->lock);
752 return ret;
753 }
754 EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
755
ieee80211_agg_tx_operational(struct ieee80211_local * local,struct sta_info * sta,u16 tid)756 static void ieee80211_agg_tx_operational(struct ieee80211_local *local,
757 struct sta_info *sta, u16 tid)
758 {
759 struct tid_ampdu_tx *tid_tx;
760 struct ieee80211_ampdu_params params = {
761 .sta = &sta->sta,
762 .action = IEEE80211_AMPDU_TX_OPERATIONAL,
763 .tid = tid,
764 .timeout = 0,
765 .ssn = 0,
766 };
767
768 lockdep_assert_wiphy(sta->local->hw.wiphy);
769
770 tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
771 params.buf_size = tid_tx->buf_size;
772 params.amsdu = tid_tx->amsdu;
773
774 ht_dbg(sta->sdata, "Aggregation is on for %pM tid %d\n",
775 sta->sta.addr, tid);
776
777 drv_ampdu_action(local, sta->sdata, ¶ms);
778
779 /*
780 * synchronize with TX path, while splicing the TX path
781 * should block so it won't put more packets onto pending.
782 */
783 spin_lock_bh(&sta->lock);
784
785 ieee80211_agg_splice_packets(sta->sdata, tid_tx, tid);
786 /*
787 * Now mark as operational. This will be visible
788 * in the TX path, and lets it go lock-free in
789 * the common case.
790 */
791 set_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state);
792 ieee80211_agg_splice_finish(sta->sdata, tid);
793
794 spin_unlock_bh(&sta->lock);
795
796 ieee80211_agg_start_txq(sta, tid, true);
797 }
798
ieee80211_start_tx_ba_cb(struct sta_info * sta,int tid,struct tid_ampdu_tx * tid_tx)799 void ieee80211_start_tx_ba_cb(struct sta_info *sta, int tid,
800 struct tid_ampdu_tx *tid_tx)
801 {
802 struct ieee80211_sub_if_data *sdata = sta->sdata;
803 struct ieee80211_local *local = sdata->local;
804
805 lockdep_assert_wiphy(sta->local->hw.wiphy);
806
807 if (WARN_ON(test_and_set_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state)))
808 return;
809
810 if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state) ||
811 test_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state))
812 return;
813
814 if (!test_bit(HT_AGG_STATE_SENT_ADDBA, &tid_tx->state)) {
815 ieee80211_send_addba_with_timeout(sta, tid_tx);
816 /* RESPONSE_RECEIVED state would trigger the flow again */
817 return;
818 }
819
820 if (test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state))
821 ieee80211_agg_tx_operational(local, sta, tid);
822 }
823
824 static struct tid_ampdu_tx *
ieee80211_lookup_tid_tx(struct ieee80211_sub_if_data * sdata,const u8 * ra,u16 tid,struct sta_info ** sta)825 ieee80211_lookup_tid_tx(struct ieee80211_sub_if_data *sdata,
826 const u8 *ra, u16 tid, struct sta_info **sta)
827 {
828 struct tid_ampdu_tx *tid_tx;
829
830 if (tid >= IEEE80211_NUM_TIDS) {
831 ht_dbg(sdata, "Bad TID value: tid = %d (>= %d)\n",
832 tid, IEEE80211_NUM_TIDS);
833 return NULL;
834 }
835
836 *sta = sta_info_get_bss(sdata, ra);
837 if (!*sta) {
838 ht_dbg(sdata, "Could not find station: %pM\n", ra);
839 return NULL;
840 }
841
842 tid_tx = rcu_dereference((*sta)->ampdu_mlme.tid_tx[tid]);
843
844 if (WARN_ON(!tid_tx))
845 ht_dbg(sdata, "addBA was not requested!\n");
846
847 return tid_tx;
848 }
849
ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_vif * vif,const u8 * ra,u16 tid)850 void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
851 const u8 *ra, u16 tid)
852 {
853 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
854 struct ieee80211_local *local = sdata->local;
855 struct sta_info *sta;
856 struct tid_ampdu_tx *tid_tx;
857
858 trace_api_start_tx_ba_cb(sdata, ra, tid);
859
860 rcu_read_lock();
861 tid_tx = ieee80211_lookup_tid_tx(sdata, ra, tid, &sta);
862 if (!tid_tx)
863 goto out;
864
865 set_bit(HT_AGG_STATE_START_CB, &tid_tx->state);
866 wiphy_work_queue(local->hw.wiphy, &sta->ampdu_mlme.work);
867 out:
868 rcu_read_unlock();
869 }
870 EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
871
ieee80211_stop_tx_ba_session(struct ieee80211_sta * pubsta,u16 tid)872 int ieee80211_stop_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid)
873 {
874 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
875 struct ieee80211_sub_if_data *sdata = sta->sdata;
876 struct ieee80211_local *local = sdata->local;
877 struct tid_ampdu_tx *tid_tx;
878 int ret = 0;
879
880 trace_api_stop_tx_ba_session(pubsta, tid);
881
882 if (!local->ops->ampdu_action)
883 return -EINVAL;
884
885 if (tid >= IEEE80211_NUM_TIDS)
886 return -EINVAL;
887
888 spin_lock_bh(&sta->lock);
889 tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
890
891 if (!tid_tx) {
892 ret = -ENOENT;
893 goto unlock;
894 }
895
896 WARN(sta->reserved_tid == tid,
897 "Requested to stop BA session on reserved tid=%d", tid);
898
899 if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
900 /* already in progress stopping it */
901 ret = 0;
902 goto unlock;
903 }
904
905 set_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state);
906 wiphy_work_queue(local->hw.wiphy, &sta->ampdu_mlme.work);
907
908 unlock:
909 spin_unlock_bh(&sta->lock);
910 return ret;
911 }
912 EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
913
ieee80211_stop_tx_ba_cb(struct sta_info * sta,int tid,struct tid_ampdu_tx * tid_tx)914 void ieee80211_stop_tx_ba_cb(struct sta_info *sta, int tid,
915 struct tid_ampdu_tx *tid_tx)
916 {
917 struct ieee80211_sub_if_data *sdata = sta->sdata;
918 bool ndp = ndp = tid_tx->ndp;
919 bool send_delba = false;
920 bool start_txq = false;
921
922 ht_dbg(sdata, "Stopping Tx BA session for %pM tid %d\n",
923 sta->sta.addr, tid);
924
925 spin_lock_bh(&sta->lock);
926
927 if (!test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
928 ht_dbg(sdata,
929 "unexpected callback to A-MPDU stop for %pM tid %d\n",
930 sta->sta.addr, tid);
931 goto unlock_sta;
932 }
933
934 if (tid_tx->stop_initiator == WLAN_BACK_INITIATOR && tid_tx->tx_stop)
935 send_delba = true;
936
937 ieee80211_remove_tid_tx(sta, tid);
938 /* tid_tx is now invalid since ieee80211_remove_tid_tx() frees it */
939 start_txq = true;
940
941 unlock_sta:
942 spin_unlock_bh(&sta->lock);
943
944 if (start_txq)
945 ieee80211_agg_start_txq(sta, tid, false);
946
947 if (send_delba)
948 ieee80211_send_delba(sdata, sta->sta.addr, tid,
949 WLAN_BACK_INITIATOR,
950 WLAN_REASON_QSTA_NOT_USE,
951 ndp);
952 }
953
ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_vif * vif,const u8 * ra,u16 tid)954 void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
955 const u8 *ra, u16 tid)
956 {
957 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
958 struct ieee80211_local *local = sdata->local;
959 struct sta_info *sta;
960 struct tid_ampdu_tx *tid_tx;
961
962 trace_api_stop_tx_ba_cb(sdata, ra, tid);
963
964 rcu_read_lock();
965 tid_tx = ieee80211_lookup_tid_tx(sdata, ra, tid, &sta);
966 if (!tid_tx)
967 goto out;
968
969 set_bit(HT_AGG_STATE_STOP_CB, &tid_tx->state);
970 wiphy_work_queue(local->hw.wiphy, &sta->ampdu_mlme.work);
971 out:
972 rcu_read_unlock();
973 }
974 EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
975
976
ieee80211_process_addba_resp(struct ieee80211_local * local,struct sta_info * sta,struct ieee80211_mgmt * mgmt,size_t len)977 void ieee80211_process_addba_resp(struct ieee80211_local *local,
978 struct sta_info *sta,
979 struct ieee80211_mgmt *mgmt,
980 size_t len)
981 {
982 struct tid_ampdu_tx *tid_tx;
983 struct ieee80211_txq *txq;
984 u16 capab, tid, buf_size;
985 bool amsdu;
986
987 lockdep_assert_wiphy(sta->local->hw.wiphy);
988
989 capab = le16_to_cpu(mgmt->u.action.addba_resp.capab);
990 amsdu = capab & IEEE80211_ADDBA_PARAM_AMSDU_MASK;
991 tid = u16_get_bits(capab, IEEE80211_ADDBA_PARAM_TID_MASK);
992 buf_size = u16_get_bits(capab, IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK);
993
994 ieee80211_retrieve_addba_ext_data(sta,
995 mgmt->u.action.addba_resp.variable,
996 len - offsetof(typeof(*mgmt),
997 u.action.addba_resp.variable),
998 &buf_size);
999
1000 buf_size = min(buf_size, local->hw.max_tx_aggregation_subframes);
1001
1002 txq = sta->sta.txq[tid];
1003 if (!amsdu && txq)
1004 set_bit(IEEE80211_TXQ_NO_AMSDU, &to_txq_info(txq)->flags);
1005
1006 tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
1007 if (!tid_tx)
1008 return;
1009
1010 if (mgmt->u.action.addba_resp.dialog_token != tid_tx->dialog_token) {
1011 ht_dbg(sta->sdata, "wrong addBA response token, %pM tid %d\n",
1012 sta->sta.addr, tid);
1013 return;
1014 }
1015
1016 timer_delete_sync(&tid_tx->addba_resp_timer);
1017
1018 ht_dbg(sta->sdata, "switched off addBA timer for %pM tid %d\n",
1019 sta->sta.addr, tid);
1020
1021 /*
1022 * addba_resp_timer may have fired before we got here, and
1023 * caused WANT_STOP to be set. If the stop then was already
1024 * processed further, STOPPING might be set.
1025 */
1026 if (test_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state) ||
1027 test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
1028 ht_dbg(sta->sdata,
1029 "got addBA resp for %pM tid %d but we already gave up\n",
1030 sta->sta.addr, tid);
1031 return;
1032 }
1033
1034 /*
1035 * IEEE 802.11-2007 7.3.1.14:
1036 * In an ADDBA Response frame, when the Status Code field
1037 * is set to 0, the Buffer Size subfield is set to a value
1038 * of at least 1.
1039 */
1040 if (le16_to_cpu(mgmt->u.action.addba_resp.status)
1041 == WLAN_STATUS_SUCCESS && buf_size) {
1042 if (test_and_set_bit(HT_AGG_STATE_RESPONSE_RECEIVED,
1043 &tid_tx->state)) {
1044 /* ignore duplicate response */
1045 return;
1046 }
1047
1048 tid_tx->buf_size = buf_size;
1049 tid_tx->amsdu = amsdu;
1050
1051 if (test_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state))
1052 ieee80211_agg_tx_operational(local, sta, tid);
1053
1054 sta->ampdu_mlme.addba_req_num[tid] = 0;
1055
1056 tid_tx->timeout =
1057 le16_to_cpu(mgmt->u.action.addba_resp.timeout);
1058
1059 if (tid_tx->timeout) {
1060 mod_timer(&tid_tx->session_timer,
1061 TU_TO_EXP_TIME(tid_tx->timeout));
1062 tid_tx->last_tx = jiffies;
1063 }
1064
1065 } else {
1066 __ieee80211_stop_tx_ba_session(sta, tid, AGG_STOP_DECLINED);
1067 }
1068 }
1069