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