net: Remove the obsolte u64_stats_fetch_*_irq() users (drivers).
[linux-block.git] / net / mac80211 / sta_info.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
f0706e82
JB
2/*
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
d98ad83e 5 * Copyright 2013-2014 Intel Mobile Communications GmbH
dcba665b 6 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
3a11ce08 7 * Copyright (C) 2018-2021 Intel Corporation
f0706e82
JB
8 */
9
10#include <linux/module.h>
11#include <linux/init.h>
888d04df 12#include <linux/etherdevice.h>
f0706e82
JB
13#include <linux/netdevice.h>
14#include <linux/types.h>
15#include <linux/slab.h>
16#include <linux/skbuff.h>
17#include <linux/if_arp.h>
0d174406 18#include <linux/timer.h>
d0709a65 19#include <linux/rtnetlink.h>
f0706e82 20
484a54c2 21#include <net/codel.h>
f0706e82
JB
22#include <net/mac80211.h>
23#include "ieee80211_i.h"
24487981 24#include "driver-ops.h"
2c8dccc7 25#include "rate.h"
f0706e82 26#include "sta_info.h"
e9f207f0 27#include "debugfs_sta.h"
ee385855 28#include "mesh.h"
ce662b44 29#include "wme.h"
f0706e82 30
d0709a65
JB
31/**
32 * DOC: STA information lifetime rules
33 *
34 * STA info structures (&struct sta_info) are managed in a hash table
35 * for faster lookup and a list for iteration. They are managed using
36 * RCU, i.e. access to the list and hash table is protected by RCU.
37 *
34e89507
JB
38 * Upon allocating a STA info structure with sta_info_alloc(), the caller
39 * owns that structure. It must then insert it into the hash table using
40 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
41 * case (which acquires an rcu read section but must not be called from
42 * within one) will the pointer still be valid after the call. Note that
43 * the caller may not do much with the STA info before inserting it, in
44 * particular, it may not start any mesh peer link management or add
45 * encryption keys.
93e5deb1
JB
46 *
47 * When the insertion fails (sta_info_insert()) returns non-zero), the
48 * structure will have been freed by sta_info_insert()!
d0709a65 49 *
34e89507 50 * Station entries are added by mac80211 when you establish a link with a
7e189a12
LR
51 * peer. This means different things for the different type of interfaces
52 * we support. For a regular station this mean we add the AP sta when we
25985edc 53 * receive an association response from the AP. For IBSS this occurs when
34e89507 54 * get to know about a peer on the same IBSS. For WDS we add the sta for
25985edc 55 * the peer immediately upon device open. When using AP mode we add stations
34e89507 56 * for each respective station upon request from userspace through nl80211.
7e189a12 57 *
34e89507
JB
58 * In order to remove a STA info structure, various sta_info_destroy_*()
59 * calls are available.
d0709a65 60 *
34e89507
JB
61 * There is no concept of ownership on a STA entry, each structure is
62 * owned by the global hash table/list until it is removed. All users of
63 * the structure need to be RCU protected so that the structure won't be
64 * freed before they are done using it.
d0709a65 65 */
f0706e82 66
cb71f1d1
JB
67struct sta_link_alloc {
68 struct link_sta_info info;
69 struct ieee80211_link_sta sta;
c71420db 70 struct rcu_head rcu_head;
cb71f1d1
JB
71};
72
7bedd0cf
JB
73static const struct rhashtable_params sta_rht_params = {
74 .nelem_hint = 3, /* start small */
caf22d31 75 .automatic_shrinking = true,
7bedd0cf 76 .head_offset = offsetof(struct sta_info, hash_node),
ac100ce5 77 .key_offset = offsetof(struct sta_info, addr),
7bedd0cf 78 .key_len = ETH_ALEN,
ebd82b39 79 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
7bedd0cf
JB
80};
81
ba6ddab9
JB
82static const struct rhashtable_params link_sta_rht_params = {
83 .nelem_hint = 3, /* start small */
84 .automatic_shrinking = true,
85 .head_offset = offsetof(struct link_sta_info, link_hash_node),
86 .key_offset = offsetof(struct link_sta_info, addr),
87 .key_len = ETH_ALEN,
88 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
89};
90
4d33960b 91/* Caller must hold local->sta_mtx */
be8755e1
MW
92static int sta_info_hash_del(struct ieee80211_local *local,
93 struct sta_info *sta)
f0706e82 94{
83e7e4ce
HX
95 return rhltable_remove(&local->sta_hash, &sta->hash_node,
96 sta_rht_params);
f0706e82
JB
97}
98
f36fe0a2
JB
99static int link_sta_info_hash_add(struct ieee80211_local *local,
100 struct link_sta_info *link_sta)
101{
177577db 102 lockdep_assert_held(&local->sta_mtx);
f36fe0a2
JB
103 return rhltable_insert(&local->link_sta_hash,
104 &link_sta->link_hash_node,
105 link_sta_rht_params);
106}
107
ba6ddab9
JB
108static int link_sta_info_hash_del(struct ieee80211_local *local,
109 struct link_sta_info *link_sta)
110{
177577db 111 lockdep_assert_held(&local->sta_mtx);
ba6ddab9
JB
112 return rhltable_remove(&local->link_sta_hash,
113 &link_sta->link_hash_node,
114 link_sta_rht_params);
115}
116
5108ca82 117static void __cleanup_single_sta(struct sta_info *sta)
b22cfcfc 118{
b22cfcfc
EP
119 int ac, i;
120 struct tid_ampdu_tx *tid_tx;
121 struct ieee80211_sub_if_data *sdata = sta->sdata;
122 struct ieee80211_local *local = sdata->local;
d012a605 123 struct ps_data *ps;
b22cfcfc 124
e3685e03 125 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
5ac2e350
JB
126 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
127 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
d012a605
MP
128 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
129 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
130 ps = &sdata->bss->ps;
3f52b7e3
MP
131 else if (ieee80211_vif_is_mesh(&sdata->vif))
132 ps = &sdata->u.mesh.ps;
d012a605
MP
133 else
134 return;
b22cfcfc
EP
135
136 clear_sta_flag(sta, WLAN_STA_PS_STA);
e3685e03 137 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
5ac2e350 138 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
b22cfcfc 139
d012a605 140 atomic_dec(&ps->num_sta_ps);
b22cfcfc
EP
141 }
142
107395f9
AW
143 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
144 struct txq_info *txqi;
adf8ed01 145
107395f9
AW
146 if (!sta->sta.txq[i])
147 continue;
adf8ed01 148
107395f9 149 txqi = to_txq_info(sta->sta.txq[i]);
ba8c3d6f 150
107395f9 151 ieee80211_txq_purge(local, txqi);
ba8c3d6f
FF
152 }
153
b22cfcfc
EP
154 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
155 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1f98ab7f
FF
156 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
157 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
b22cfcfc
EP
158 }
159
45b5028e
TP
160 if (ieee80211_vif_is_mesh(&sdata->vif))
161 mesh_sta_cleanup(sta);
b22cfcfc 162
5ac2e350 163 cancel_work_sync(&sta->drv_deliver_wk);
b22cfcfc
EP
164
165 /*
166 * Destroy aggregation state here. It would be nice to wait for the
167 * driver to finish aggregation stop and then clean up, but for now
168 * drivers have to handle aggregation stop being requested, followed
169 * directly by station destruction.
170 */
5a306f58 171 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
661eb381 172 kfree(sta->ampdu_mlme.tid_start_tx[i]);
b22cfcfc
EP
173 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
174 if (!tid_tx)
175 continue;
1f98ab7f 176 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
b22cfcfc
EP
177 kfree(tid_tx);
178 }
5108ca82 179}
b22cfcfc 180
5108ca82
JB
181static void cleanup_single_sta(struct sta_info *sta)
182{
183 struct ieee80211_sub_if_data *sdata = sta->sdata;
184 struct ieee80211_local *local = sdata->local;
185
186 __cleanup_single_sta(sta);
b22cfcfc
EP
187 sta_info_free(local, sta);
188}
189
83e7e4ce
HX
190struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
191 const u8 *addr)
192{
193 return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
194}
195
d0709a65 196/* protected by RCU */
abe60632
JB
197struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
198 const u8 *addr)
f0706e82 199{
abe60632 200 struct ieee80211_local *local = sdata->local;
83e7e4ce 201 struct rhlist_head *tmp;
60f4b626 202 struct sta_info *sta;
f0706e82 203
60f4b626 204 rcu_read_lock();
83e7e4ce 205 for_each_sta_info(local, addr, sta, tmp) {
60f4b626
JB
206 if (sta->sdata == sdata) {
207 rcu_read_unlock();
208 /* this is safe as the caller must already hold
209 * another rcu read section or the mutex
210 */
211 return sta;
212 }
213 }
214 rcu_read_unlock();
215 return NULL;
43ba7e95
JB
216}
217
0e5ded5a
FF
218/*
219 * Get sta info either from the specified interface
220 * or from one of its vlans
221 */
222struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
223 const u8 *addr)
224{
225 struct ieee80211_local *local = sdata->local;
83e7e4ce 226 struct rhlist_head *tmp;
0e5ded5a
FF
227 struct sta_info *sta;
228
7bedd0cf 229 rcu_read_lock();
83e7e4ce 230 for_each_sta_info(local, addr, sta, tmp) {
7bedd0cf
JB
231 if (sta->sdata == sdata ||
232 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
233 rcu_read_unlock();
234 /* this is safe as the caller must already hold
235 * another rcu read section or the mutex
236 */
237 return sta;
238 }
0e5ded5a 239 }
7bedd0cf
JB
240 rcu_read_unlock();
241 return NULL;
0e5ded5a
FF
242}
243
ba6ddab9
JB
244struct rhlist_head *link_sta_info_hash_lookup(struct ieee80211_local *local,
245 const u8 *addr)
246{
247 return rhltable_lookup(&local->link_sta_hash, addr,
248 link_sta_rht_params);
249}
250
251struct link_sta_info *
252link_sta_info_get_bss(struct ieee80211_sub_if_data *sdata, const u8 *addr)
253{
254 struct ieee80211_local *local = sdata->local;
255 struct rhlist_head *tmp;
256 struct link_sta_info *link_sta;
257
258 rcu_read_lock();
259 for_each_link_sta_info(local, addr, link_sta, tmp) {
260 struct sta_info *sta = link_sta->sta;
261
262 if (sta->sdata == sdata ||
263 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
264 rcu_read_unlock();
265 /* this is safe as the caller must already hold
266 * another rcu read section or the mutex
267 */
268 return link_sta;
269 }
270 }
271 rcu_read_unlock();
272 return NULL;
273}
274
ffa9598e
JB
275struct ieee80211_sta *
276ieee80211_find_sta_by_link_addrs(struct ieee80211_hw *hw,
277 const u8 *addr,
278 const u8 *localaddr,
279 unsigned int *link_id)
280{
281 struct ieee80211_local *local = hw_to_local(hw);
282 struct link_sta_info *link_sta;
283 struct rhlist_head *tmp;
284
285 for_each_link_sta_info(local, addr, link_sta, tmp) {
286 struct sta_info *sta = link_sta->sta;
287 struct ieee80211_link_data *link;
288 u8 _link_id = link_sta->link_id;
289
290 if (!localaddr) {
291 if (link_id)
292 *link_id = _link_id;
293 return &sta->sta;
294 }
295
296 link = rcu_dereference(sta->sdata->link[_link_id]);
297 if (!link)
298 continue;
299
300 if (memcmp(link->conf->addr, localaddr, ETH_ALEN))
301 continue;
302
303 if (link_id)
304 *link_id = _link_id;
305 return &sta->sta;
306 }
307
308 return NULL;
309}
310EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_link_addrs);
311
5072f73c
THJ
312struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
313 const u8 *sta_addr, const u8 *vif_addr)
314{
315 struct rhlist_head *tmp;
316 struct sta_info *sta;
317
318 for_each_sta_info(local, sta_addr, sta, tmp) {
319 if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
320 return sta;
321 }
322
323 return NULL;
324}
325
3b53fde8
JB
326struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
327 int idx)
ee385855 328{
3b53fde8 329 struct ieee80211_local *local = sdata->local;
ee385855
LCC
330 struct sta_info *sta;
331 int i = 0;
332
8ca47eb9
MB
333 list_for_each_entry_rcu(sta, &local->sta_list, list,
334 lockdep_is_held(&local->sta_mtx)) {
3b53fde8 335 if (sdata != sta->sdata)
2a8ca29a 336 continue;
ee385855
LCC
337 if (i < idx) {
338 ++i;
339 continue;
ee385855 340 }
2a8ca29a 341 return sta;
ee385855 342 }
ee385855
LCC
343
344 return NULL;
345}
f0706e82 346
cb71f1d1 347static void sta_info_free_link(struct link_sta_info *link_sta)
246b39e4 348{
cb71f1d1
JB
349 free_percpu(link_sta->pcpu_rx_stats);
350}
246b39e4 351
ba6ddab9
JB
352static void sta_remove_link(struct sta_info *sta, unsigned int link_id,
353 bool unhash)
cb71f1d1
JB
354{
355 struct sta_link_alloc *alloc = NULL;
c71420db 356 struct link_sta_info *link_sta;
246b39e4 357
c71420db
JB
358 link_sta = rcu_dereference_protected(sta->link[link_id],
359 lockdep_is_held(&sta->local->sta_mtx));
360
361 if (WARN_ON(!link_sta))
cb71f1d1
JB
362 return;
363
ba6ddab9
JB
364 if (unhash)
365 link_sta_info_hash_del(sta->local, link_sta);
366
d2caad52
BB
367 if (test_sta_flag(sta, WLAN_STA_INSERTED))
368 ieee80211_link_sta_debugfs_remove(link_sta);
369
c71420db
JB
370 if (link_sta != &sta->deflink)
371 alloc = container_of(link_sta, typeof(*alloc), info);
cb71f1d1
JB
372
373 sta->sta.valid_links &= ~BIT(link_id);
c71420db
JB
374 RCU_INIT_POINTER(sta->link[link_id], NULL);
375 RCU_INIT_POINTER(sta->sta.link[link_id], NULL);
cb71f1d1
JB
376 if (alloc) {
377 sta_info_free_link(&alloc->info);
c71420db 378 kfree_rcu(alloc, rcu_head);
246b39e4 379 }
4c51541d
BB
380
381 ieee80211_sta_recalc_aggregates(&sta->sta);
246b39e4
JB
382}
383
93e5deb1 384/**
d9a7ddb0 385 * sta_info_free - free STA
93e5deb1 386 *
6ef307bc 387 * @local: pointer to the global information
93e5deb1
JB
388 * @sta: STA info to free
389 *
390 * This function must undo everything done by sta_info_alloc()
d9a7ddb0
JB
391 * that may happen before sta_info_insert(). It may only be
392 * called when sta_info_insert() has not been attempted (and
393 * if that fails, the station is freed anyway.)
93e5deb1 394 */
d9a7ddb0 395void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
93e5deb1 396{
cb71f1d1
JB
397 int i;
398
399 for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
400 if (!(sta->sta.valid_links & BIT(i)))
401 continue;
402
0ad49045 403 sta_remove_link(sta, i, false);
cb71f1d1
JB
404 }
405
dcd479e1
JB
406 /*
407 * If we had used sta_info_pre_move_state() then we might not
408 * have gone through the state transitions down again, so do
409 * it here now (and warn if it's inserted).
410 *
411 * This will clear state such as fast TX/RX that may have been
412 * allocated during state transitions.
413 */
414 while (sta->sta_state > IEEE80211_STA_NONE) {
415 int ret;
416
417 WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
418
419 ret = sta_info_move_state(sta, sta->sta_state - 1);
420 if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
421 break;
422 }
423
889cbb91 424 if (sta->rate_ctrl)
af65cd96 425 rate_control_free_sta(sta);
93e5deb1 426
bdcbd8e0 427 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
93e5deb1 428
107395f9 429 kfree(to_txq_info(sta->sta.txq[0]));
53d04525 430 kfree(rcu_dereference_raw(sta->sta.rates));
433f5bc1
JB
431#ifdef CONFIG_MAC80211_MESH
432 kfree(sta->mesh);
433#endif
246b39e4 434
cb71f1d1 435 sta_info_free_link(&sta->deflink);
93e5deb1
JB
436 kfree(sta);
437}
438
4d33960b 439/* Caller must hold local->sta_mtx */
62b14b24
JB
440static int sta_info_hash_add(struct ieee80211_local *local,
441 struct sta_info *sta)
f0706e82 442{
83e7e4ce
HX
443 return rhltable_insert(&local->sta_hash, &sta->hash_node,
444 sta_rht_params);
f0706e82 445}
f0706e82 446
5ac2e350 447static void sta_deliver_ps_frames(struct work_struct *wk)
af818581
JB
448{
449 struct sta_info *sta;
450
5ac2e350 451 sta = container_of(wk, struct sta_info, drv_deliver_wk);
af818581
JB
452
453 if (sta->dead)
454 return;
455
5ac2e350
JB
456 local_bh_disable();
457 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
af818581 458 ieee80211_sta_ps_deliver_wakeup(sta);
5ac2e350 459 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
af818581 460 ieee80211_sta_ps_deliver_poll_response(sta);
5ac2e350 461 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
47086fc5 462 ieee80211_sta_ps_deliver_uapsd(sta);
5ac2e350 463 local_bh_enable();
af818581
JB
464}
465
af65cd96
JB
466static int sta_prepare_rate_control(struct ieee80211_local *local,
467 struct sta_info *sta, gfp_t gfp)
468{
30686bf7 469 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
af65cd96
JB
470 return 0;
471
889cbb91 472 sta->rate_ctrl = local->rate_ctrl;
af65cd96 473 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
35c347ac 474 sta, gfp);
889cbb91 475 if (!sta->rate_ctrl_priv)
af65cd96 476 return -ENOMEM;
af65cd96
JB
477
478 return 0;
479}
480
cb71f1d1
JB
481static int sta_info_alloc_link(struct ieee80211_local *local,
482 struct link_sta_info *link_info,
483 gfp_t gfp)
246b39e4 484{
246b39e4
JB
485 struct ieee80211_hw *hw = &local->hw;
486 int i;
487
246b39e4
JB
488 if (ieee80211_hw_check(hw, USES_RSS)) {
489 link_info->pcpu_rx_stats =
490 alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
491 if (!link_info->pcpu_rx_stats)
492 return -ENOMEM;
493 }
494
246b39e4
JB
495 link_info->rx_stats.last_rx = jiffies;
496 u64_stats_init(&link_info->rx_stats.syncp);
497
498 ewma_signal_init(&link_info->rx_stats_avg.signal);
499 ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
500 for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
501 ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
502
503 return 0;
504}
505
cb71f1d1
JB
506static void sta_info_add_link(struct sta_info *sta,
507 unsigned int link_id,
508 struct link_sta_info *link_info,
509 struct ieee80211_link_sta *link_sta)
510{
511 link_info->sta = sta;
512 link_info->link_id = link_id;
c71420db 513 link_info->pub = link_sta;
1d9e4c91 514 link_info->pub->sta = &sta->sta;
c73993b8 515 link_sta->link_id = link_id;
c71420db
JB
516 rcu_assign_pointer(sta->link[link_id], link_info);
517 rcu_assign_pointer(sta->sta.link[link_id], link_sta);
261ce887
BB
518
519 link_sta->smps_mode = IEEE80211_SMPS_OFF;
4c51541d 520 link_sta->agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
cb71f1d1
JB
521}
522
f36fe0a2
JB
523static struct sta_info *
524__sta_info_alloc(struct ieee80211_sub_if_data *sdata,
525 const u8 *addr, int link_id, const u8 *link_addr,
526 gfp_t gfp)
f0706e82 527{
d0709a65 528 struct ieee80211_local *local = sdata->local;
ba8c3d6f 529 struct ieee80211_hw *hw = &local->hw;
f0706e82 530 struct sta_info *sta;
107395f9
AW
531 void *txq_data;
532 int size;
16c5f15c 533 int i;
f0706e82 534
ba8c3d6f 535 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
f0706e82 536 if (!sta)
73651ee6 537 return NULL;
f0706e82 538
246b39e4
JB
539 sta->local = local;
540 sta->sdata = sdata;
541
cb71f1d1 542 if (sta_info_alloc_link(local, &sta->deflink, gfp))
36fe8e4e 543 goto free;
c9c5962b 544
cb71f1d1
JB
545 if (link_id >= 0) {
546 sta_info_add_link(sta, link_id, &sta->deflink,
547 &sta->sta.deflink);
548 sta->sta.valid_links = BIT(link_id);
549 } else {
550 sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
551 }
552
4c51541d
BB
553 sta->sta.cur = &sta->sta.deflink.agg;
554
07346f81 555 spin_lock_init(&sta->lock);
1d147bfa 556 spin_lock_init(&sta->ps_lock);
5ac2e350 557 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
67c282c0 558 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
a93e3644 559 mutex_init(&sta->ampdu_mlme.mtx);
87f59c70 560#ifdef CONFIG_MAC80211_MESH
433f5bc1
JB
561 if (ieee80211_vif_is_mesh(&sdata->vif)) {
562 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
563 if (!sta->mesh)
564 goto free;
4c02d62f 565 sta->mesh->plink_sta = sta;
433f5bc1 566 spin_lock_init(&sta->mesh->plink_lock);
45d33746 567 if (!sdata->u.mesh.user_mpm)
4c02d62f
KC
568 timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
569 0);
433f5bc1
JB
570 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
571 }
87f59c70 572#endif
07346f81 573
ac100ce5 574 memcpy(sta->addr, addr, ETH_ALEN);
17741cdc 575 memcpy(sta->sta.addr, addr, ETH_ALEN);
f36fe0a2
JB
576 memcpy(sta->deflink.addr, link_addr, ETH_ALEN);
577 memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN);
480dd46b
MA
578 sta->sta.max_rx_aggregation_subframes =
579 local->hw.max_rx_aggregation_subframes;
580
046d2e7c
S
581 /* TODO link specific alloc and assignments for MLO Link STA */
582
96fc6efb
AW
583 /* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
584 * The Tx path starts to use a key as soon as the key slot ptk_idx
585 * references to is not NULL. To not use the initial Rx-only key
586 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
587 * which always will refer to a NULL key.
588 */
589 BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
590 sta->ptk_idx = INVALID_PTK_KEYIDX;
591
0f9c5a61 592
3a11ce08
JB
593 ieee80211_init_frag_cache(&sta->frags);
594
71ec375c
JB
595 sta->sta_state = IEEE80211_STA_NONE;
596
b6da911b
LK
597 /* Mark TID as unreserved */
598 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
599
84b00607 600 sta->last_connected = ktime_get_seconds();
541a45a1 601
107395f9
AW
602 size = sizeof(struct txq_info) +
603 ALIGN(hw->txq_data_size, sizeof(void *));
ba8c3d6f 604
107395f9
AW
605 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
606 if (!txq_data)
607 goto free;
ba8c3d6f 608
107395f9
AW
609 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
610 struct txq_info *txq = txq_data + i * size;
ba8c3d6f 611
107395f9
AW
612 /* might not do anything for the (bufferable) MMPDU TXQ */
613 ieee80211_txq_init(sdata, sta, txq, i);
abfbc3af 614 }
f0706e82 615
ba8c3d6f
FF
616 if (sta_prepare_rate_control(local, sta, gfp))
617 goto free_txq;
618
942741da 619 sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
b4809e94 620
948d887d
JB
621 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
622 skb_queue_head_init(&sta->ps_tx_buf[i]);
623 skb_queue_head_init(&sta->tx_filtered[i]);
942741da
FF
624 sta->airtime[i].deficit = sta->airtime_weight;
625 atomic_set(&sta->airtime[i].aql_tx_pending, 0);
626 sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
627 sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
948d887d 628 }
73651ee6 629
5a306f58 630 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
4be929be 631 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
cccaec98 632
bd718fc1
JB
633 for (i = 0; i < NUM_NL80211_BANDS; i++) {
634 u32 mandatory = 0;
635 int r;
636
637 if (!hw->wiphy->bands[i])
638 continue;
639
640 switch (i) {
641 case NL80211_BAND_2GHZ:
63fa0426 642 case NL80211_BAND_LC:
bd718fc1
JB
643 /*
644 * We use both here, even if we cannot really know for
645 * sure the station will support both, but the only use
646 * for this is when we don't know anything yet and send
647 * management frames, and then we'll pick the lowest
648 * possible rate anyway.
649 * If we don't include _G here, we cannot find a rate
650 * in P2P, and thus trigger the WARN_ONCE() in rate.c
651 */
652 mandatory = IEEE80211_RATE_MANDATORY_B |
653 IEEE80211_RATE_MANDATORY_G;
654 break;
655 case NL80211_BAND_5GHZ:
656 mandatory = IEEE80211_RATE_MANDATORY_A;
657 break;
658 case NL80211_BAND_60GHZ:
659 WARN_ON(1);
660 mandatory = 0;
661 break;
662 }
663
664 for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
665 struct ieee80211_rate *rate;
666
667 rate = &hw->wiphy->bands[i]->bitrates[r];
668
669 if (!(rate->flags & mandatory))
670 continue;
046d2e7c 671 sta->sta.deflink.supp_rates[i] |= BIT(r);
bd718fc1
JB
672 }
673 }
674
484a54c2
THJ
675 sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
676 sta->cparams.target = MS2TIME(20);
677 sta->cparams.interval = MS2TIME(100);
678 sta->cparams.ecn = true;
dfcb63ce
THJ
679 sta->cparams.ce_threshold_selector = 0;
680 sta->cparams.ce_threshold_mask = 0;
484a54c2 681
bdcbd8e0 682 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
ef04a297 683
abfbc3af 684 return sta;
ba8c3d6f
FF
685
686free_txq:
107395f9 687 kfree(to_txq_info(sta->sta.txq[0]));
ba8c3d6f 688free:
cb71f1d1 689 sta_info_free_link(&sta->deflink);
433f5bc1
JB
690#ifdef CONFIG_MAC80211_MESH
691 kfree(sta->mesh);
692#endif
ba8c3d6f
FF
693 kfree(sta);
694 return NULL;
73651ee6
JB
695}
696
f36fe0a2
JB
697struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
698 const u8 *addr, gfp_t gfp)
699{
700 return __sta_info_alloc(sdata, addr, -1, addr, gfp);
701}
702
703struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
704 const u8 *mld_addr,
705 unsigned int link_id,
706 const u8 *link_addr,
707 gfp_t gfp)
708{
709 return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
710}
711
8c71df7a 712static int sta_info_insert_check(struct sta_info *sta)
34e89507 713{
34e89507 714 struct ieee80211_sub_if_data *sdata = sta->sdata;
34e89507 715
03e4497e
JB
716 /*
717 * Can't be a WARN_ON because it can be triggered through a race:
718 * something inserts a STA (on one CPU) without holding the RTNL
719 * and another CPU turns off the net device.
720 */
8c71df7a
GE
721 if (unlikely(!ieee80211_sdata_running(sdata)))
722 return -ENETDOWN;
03e4497e 723
b203ca39 724 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
deebea0a 725 !is_valid_ether_addr(sta->sta.addr)))
8c71df7a
GE
726 return -EINVAL;
727
83e7e4ce
HX
728 /* The RCU read lock is required by rhashtable due to
729 * asynchronous resize/rehash. We also require the mutex
730 * for correctness.
31104891
JB
731 */
732 rcu_read_lock();
733 lockdep_assert_held(&sdata->local->sta_mtx);
734 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
735 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
736 rcu_read_unlock();
737 return -ENOTUNIQ;
738 }
739 rcu_read_unlock();
740
8c71df7a
GE
741 return 0;
742}
743
f09603a2
JB
744static int sta_info_insert_drv_state(struct ieee80211_local *local,
745 struct ieee80211_sub_if_data *sdata,
746 struct sta_info *sta)
747{
748 enum ieee80211_sta_state state;
749 int err = 0;
750
751 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
752 err = drv_sta_state(local, sdata, sta, state, state + 1);
753 if (err)
754 break;
755 }
756
757 if (!err) {
a4ec45a4
JB
758 /*
759 * Drivers using legacy sta_add/sta_remove callbacks only
760 * get uploaded set to true after sta_add is called.
761 */
762 if (!local->ops->sta_add)
763 sta->uploaded = true;
f09603a2
JB
764 return 0;
765 }
766
767 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
bdcbd8e0
JB
768 sdata_info(sdata,
769 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
770 sta->sta.addr, state + 1, err);
f09603a2
JB
771 err = 0;
772 }
773
774 /* unwind on error */
775 for (; state > IEEE80211_STA_NOTEXIST; state--)
776 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
777
778 return err;
779}
780
d405fd8c
GG
781static void
782ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
783{
784 struct ieee80211_local *local = sdata->local;
785 bool allow_p2p_go_ps = sdata->vif.p2p;
786 struct sta_info *sta;
787
788 rcu_read_lock();
789 list_for_each_entry_rcu(sta, &local->sta_list, list) {
790 if (sdata != sta->sdata ||
791 !test_sta_flag(sta, WLAN_STA_ASSOC))
792 continue;
793 if (!sta->sta.support_p2p_ps) {
794 allow_p2p_go_ps = false;
795 break;
796 }
797 }
798 rcu_read_unlock();
799
800 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
801 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
d8675a63
JB
802 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
803 BSS_CHANGED_P2P_PS);
d405fd8c
GG
804 }
805}
806
8c71df7a
GE
807/*
808 * should be called with sta_mtx locked
809 * this function replaces the mutex lock
810 * with a RCU lock
811 */
4d33960b 812static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
8c71df7a
GE
813{
814 struct ieee80211_local *local = sta->local;
815 struct ieee80211_sub_if_data *sdata = sta->sdata;
0c2e3842 816 struct station_info *sinfo = NULL;
8c71df7a
GE
817 int err = 0;
818
819 lockdep_assert_held(&local->sta_mtx);
34e89507 820
7852e361
JB
821 /* check if STA exists already */
822 if (sta_info_get_bss(sdata, sta->sta.addr)) {
823 err = -EEXIST;
8f9dcc29 824 goto out_cleanup;
4d33960b 825 }
32bfd35d 826
0c2e3842
KV
827 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
828 if (!sinfo) {
829 err = -ENOMEM;
8f9dcc29 830 goto out_cleanup;
0c2e3842
KV
831 }
832
7852e361
JB
833 local->num_sta++;
834 local->sta_generation++;
835 smp_mb();
4d33960b 836
5108ca82
JB
837 /* simplify things and don't accept BA sessions yet */
838 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
839
7852e361 840 /* make the station visible */
62b14b24
JB
841 err = sta_info_hash_add(local, sta);
842 if (err)
843 goto out_drop_sta;
83d5cc01 844
f36fe0a2
JB
845 if (sta->sta.valid_links) {
846 err = link_sta_info_hash_add(local, &sta->deflink);
847 if (err) {
848 sta_info_hash_del(local, sta);
849 goto out_drop_sta;
850 }
851 }
852
2bad7748 853 list_add_tail_rcu(&sta->list, &local->sta_list);
4d33960b 854
4dde3c36
MG
855 /* update channel context before notifying the driver about state
856 * change, this enables driver using the updated channel context right away.
857 */
858 if (sta->sta_state >= IEEE80211_STA_ASSOC) {
0cbf348a 859 ieee80211_recalc_min_chandef(sta->sdata, -1);
4dde3c36
MG
860 if (!sta->sta.support_p2p_ps)
861 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
862 }
863
5108ca82
JB
864 /* notify driver */
865 err = sta_info_insert_drv_state(local, sdata, sta);
866 if (err)
867 goto out_remove;
868
7852e361 869 set_sta_flag(sta, WLAN_STA_INSERTED);
d405fd8c 870
5108ca82
JB
871 /* accept BA sessions now */
872 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
4d33960b 873
7852e361
JB
874 ieee80211_sta_debugfs_add(sta);
875 rate_control_add_sta_debugfs(sta);
d2caad52
BB
876 if (sta->sta.valid_links) {
877 int i;
878
879 for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
880 struct link_sta_info *link_sta;
881
882 link_sta = rcu_dereference_protected(sta->link[i],
883 lockdep_is_held(&local->sta_mtx));
884
885 if (!link_sta)
886 continue;
887
888 ieee80211_link_sta_debugfs_add(link_sta);
889 if (sdata->vif.active_links & BIT(i))
890 ieee80211_link_sta_debugfs_drv_add(link_sta);
891 }
892 } else {
893 ieee80211_link_sta_debugfs_add(&sta->deflink);
894 ieee80211_link_sta_debugfs_drv_add(&sta->deflink);
895 }
4d33960b 896
0ef049dc
AB
897 sinfo->generation = local->sta_generation;
898 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
899 kfree(sinfo);
d0709a65 900
bdcbd8e0 901 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
f0706e82 902
34e89507
JB
903 /* move reference to rcu-protected */
904 rcu_read_lock();
905 mutex_unlock(&local->sta_mtx);
e9f207f0 906
73651ee6
JB
907 if (ieee80211_vif_is_mesh(&sdata->vif))
908 mesh_accept_plinks_update(sdata);
909
8c71df7a 910 return 0;
5108ca82 911 out_remove:
0ad49045
JB
912 if (sta->sta.valid_links)
913 link_sta_info_hash_del(local, &sta->deflink);
5108ca82
JB
914 sta_info_hash_del(local, sta);
915 list_del_rcu(&sta->list);
62b14b24 916 out_drop_sta:
5108ca82
JB
917 local->num_sta--;
918 synchronize_net();
8f9dcc29 919 out_cleanup:
7bc40aed 920 cleanup_single_sta(sta);
4d33960b 921 mutex_unlock(&local->sta_mtx);
ea32f065 922 kfree(sinfo);
4d33960b
JB
923 rcu_read_lock();
924 return err;
8c71df7a
GE
925}
926
927int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
928{
929 struct ieee80211_local *local = sta->local;
308f7fcf 930 int err;
8c71df7a 931
4d33960b
JB
932 might_sleep();
933
31104891
JB
934 mutex_lock(&local->sta_mtx);
935
8c71df7a
GE
936 err = sta_info_insert_check(sta);
937 if (err) {
7bc40aed 938 sta_info_free(local, sta);
31104891 939 mutex_unlock(&local->sta_mtx);
8c71df7a 940 rcu_read_lock();
7bc40aed 941 return err;
8c71df7a
GE
942 }
943
7bc40aed 944 return sta_info_insert_finish(sta);
f0706e82
JB
945}
946
34e89507
JB
947int sta_info_insert(struct sta_info *sta)
948{
949 int err = sta_info_insert_rcu(sta);
950
951 rcu_read_unlock();
952
953 return err;
954}
955
d012a605 956static inline void __bss_tim_set(u8 *tim, u16 id)
004c872e
JB
957{
958 /*
959 * This format has been mandated by the IEEE specifications,
960 * so this line may not be changed to use the __set_bit() format.
961 */
d012a605 962 tim[id / 8] |= (1 << (id % 8));
004c872e
JB
963}
964
d012a605 965static inline void __bss_tim_clear(u8 *tim, u16 id)
004c872e
JB
966{
967 /*
968 * This format has been mandated by the IEEE specifications,
969 * so this line may not be changed to use the __clear_bit() format.
970 */
d012a605 971 tim[id / 8] &= ~(1 << (id % 8));
004c872e
JB
972}
973
3d5839b6
IP
974static inline bool __bss_tim_get(u8 *tim, u16 id)
975{
976 /*
977 * This format has been mandated by the IEEE specifications,
978 * so this line may not be changed to use the test_bit() format.
979 */
980 return tim[id / 8] & (1 << (id % 8));
981}
982
948d887d 983static unsigned long ieee80211_tids_for_ac(int ac)
004c872e 984{
948d887d
JB
985 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
986 switch (ac) {
987 case IEEE80211_AC_VO:
988 return BIT(6) | BIT(7);
989 case IEEE80211_AC_VI:
990 return BIT(4) | BIT(5);
991 case IEEE80211_AC_BE:
992 return BIT(0) | BIT(3);
993 case IEEE80211_AC_BK:
994 return BIT(1) | BIT(2);
995 default:
996 WARN_ON(1);
997 return 0;
d0709a65 998 }
004c872e
JB
999}
1000
9b7a86f3 1001static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
004c872e 1002{
c868cb35 1003 struct ieee80211_local *local = sta->local;
d012a605 1004 struct ps_data *ps;
948d887d
JB
1005 bool indicate_tim = false;
1006 u8 ignore_for_tim = sta->sta.uapsd_queues;
1007 int ac;
a69bd8e6 1008 u16 id = sta->sta.aid;
d012a605
MP
1009
1010 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1011 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1012 if (WARN_ON_ONCE(!sta->sdata->bss))
1013 return;
004c872e 1014
d012a605 1015 ps = &sta->sdata->bss->ps;
3f52b7e3
MP
1016#ifdef CONFIG_MAC80211_MESH
1017 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
1018 ps = &sta->sdata->u.mesh.ps;
3f52b7e3 1019#endif
d012a605 1020 } else {
c868cb35 1021 return;
d012a605 1022 }
3e122be0 1023
c868cb35 1024 /* No need to do anything if the driver does all */
d98937f4 1025 if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
c868cb35 1026 return;
004c872e 1027
c868cb35
JB
1028 if (sta->dead)
1029 goto done;
3e122be0 1030
948d887d
JB
1031 /*
1032 * If all ACs are delivery-enabled then we should build
1033 * the TIM bit for all ACs anyway; if only some are then
1034 * we ignore those and build the TIM bit using only the
1035 * non-enabled ones.
1036 */
1037 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
1038 ignore_for_tim = 0;
1039
9b7a86f3
JB
1040 if (ignore_pending)
1041 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
1042
948d887d
JB
1043 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1044 unsigned long tids;
3e122be0 1045
f438ceb8 1046 if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
948d887d
JB
1047 continue;
1048
1049 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
1050 !skb_queue_empty(&sta->ps_tx_buf[ac]);
1051 if (indicate_tim)
1052 break;
3e122be0 1053
948d887d
JB
1054 tids = ieee80211_tids_for_ac(ac);
1055
1056 indicate_tim |=
1057 sta->driver_buffered_tids & tids;
ba8c3d6f
FF
1058 indicate_tim |=
1059 sta->txq_buffered_tids & tids;
d0709a65 1060 }
004c872e 1061
c868cb35 1062 done:
65f704a5 1063 spin_lock_bh(&local->tim_lock);
004c872e 1064
3d5839b6
IP
1065 if (indicate_tim == __bss_tim_get(ps->tim, id))
1066 goto out_unlock;
1067
948d887d 1068 if (indicate_tim)
d012a605 1069 __bss_tim_set(ps->tim, id);
c868cb35 1070 else
d012a605 1071 __bss_tim_clear(ps->tim, id);
004c872e 1072
9b7a86f3 1073 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
c868cb35 1074 local->tim_in_locked_section = true;
948d887d 1075 drv_set_tim(local, &sta->sta, indicate_tim);
c868cb35
JB
1076 local->tim_in_locked_section = false;
1077 }
3e122be0 1078
3d5839b6 1079out_unlock:
65f704a5 1080 spin_unlock_bh(&local->tim_lock);
004c872e
JB
1081}
1082
9b7a86f3
JB
1083void sta_info_recalc_tim(struct sta_info *sta)
1084{
1085 __sta_info_recalc_tim(sta, false);
1086}
1087
cd0b8d89 1088static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
f0706e82 1089{
e039fa4a 1090 struct ieee80211_tx_info *info;
f0706e82
JB
1091 int timeout;
1092
1093 if (!skb)
cd0b8d89 1094 return false;
f0706e82 1095
e039fa4a 1096 info = IEEE80211_SKB_CB(skb);
f0706e82
JB
1097
1098 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
57c4d7b4
JB
1099 timeout = (sta->listen_interval *
1100 sta->sdata->vif.bss_conf.beacon_int *
1101 32 / 15625) * HZ;
f0706e82
JB
1102 if (timeout < STA_TX_BUFFER_EXPIRE)
1103 timeout = STA_TX_BUFFER_EXPIRE;
e039fa4a 1104 return time_after(jiffies, info->control.jiffies + timeout);
f0706e82
JB
1105}
1106
1107
948d887d
JB
1108static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1109 struct sta_info *sta, int ac)
f0706e82
JB
1110{
1111 unsigned long flags;
1112 struct sk_buff *skb;
1113
60750397
JB
1114 /*
1115 * First check for frames that should expire on the filtered
1116 * queue. Frames here were rejected by the driver and are on
1117 * a separate queue to avoid reordering with normal PS-buffered
1118 * frames. They also aren't accounted for right now in the
1119 * total_ps_buffered counter.
1120 */
1121 for (;;) {
948d887d
JB
1122 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1123 skb = skb_peek(&sta->tx_filtered[ac]);
60750397 1124 if (sta_info_buffer_expired(sta, skb))
948d887d 1125 skb = __skb_dequeue(&sta->tx_filtered[ac]);
60750397
JB
1126 else
1127 skb = NULL;
948d887d 1128 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
60750397
JB
1129
1130 /*
1131 * Frames are queued in order, so if this one
1132 * hasn't expired yet we can stop testing. If
1133 * we actually reached the end of the queue we
1134 * also need to stop, of course.
1135 */
1136 if (!skb)
1137 break;
d4fa14cd 1138 ieee80211_free_txskb(&local->hw, skb);
60750397
JB
1139 }
1140
1141 /*
1142 * Now also check the normal PS-buffered queue, this will
1143 * only find something if the filtered queue was emptied
1144 * since the filtered frames are all before the normal PS
1145 * buffered frames.
1146 */
f0706e82 1147 for (;;) {
948d887d
JB
1148 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1149 skb = skb_peek(&sta->ps_tx_buf[ac]);
57c4d7b4 1150 if (sta_info_buffer_expired(sta, skb))
948d887d 1151 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
836341a7 1152 else
f0706e82 1153 skb = NULL;
948d887d 1154 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
f0706e82 1155
60750397
JB
1156 /*
1157 * frames are queued in order, so if this one
1158 * hasn't expired yet (or we reached the end of
1159 * the queue) we can stop testing
1160 */
836341a7 1161 if (!skb)
f0706e82 1162 break;
836341a7 1163
836341a7 1164 local->total_ps_buffered--;
bdcbd8e0
JB
1165 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1166 sta->sta.addr);
d4fa14cd 1167 ieee80211_free_txskb(&local->hw, skb);
f0706e82 1168 }
3393a608 1169
60750397
JB
1170 /*
1171 * Finally, recalculate the TIM bit for this station -- it might
1172 * now be clear because the station was too slow to retrieve its
1173 * frames.
1174 */
1175 sta_info_recalc_tim(sta);
1176
1177 /*
1178 * Return whether there are any frames still buffered, this is
1179 * used to check whether the cleanup timer still needs to run,
1180 * if there are no frames we don't need to rearm the timer.
1181 */
948d887d
JB
1182 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1183 skb_queue_empty(&sta->tx_filtered[ac]));
1184}
1185
1186static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1187 struct sta_info *sta)
1188{
1189 bool have_buffered = false;
1190 int ac;
1191
3f52b7e3
MP
1192 /* This is only necessary for stations on BSS/MBSS interfaces */
1193 if (!sta->sdata->bss &&
1194 !ieee80211_vif_is_mesh(&sta->sdata->vif))
948d887d
JB
1195 return false;
1196
1197 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1198 have_buffered |=
1199 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1200
1201 return have_buffered;
f0706e82
JB
1202}
1203
d778207b 1204static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
f0706e82 1205{
34e89507
JB
1206 struct ieee80211_local *local;
1207 struct ieee80211_sub_if_data *sdata;
0ad49045 1208 int ret, i;
f0706e82 1209
34e89507 1210 might_sleep();
f0706e82 1211
34e89507
JB
1212 if (!sta)
1213 return -ENOENT;
5bb644a0 1214
34e89507
JB
1215 local = sta->local;
1216 sdata = sta->sdata;
f0706e82 1217
83d5cc01
JB
1218 lockdep_assert_held(&local->sta_mtx);
1219
098a6070
JB
1220 /*
1221 * Before removing the station from the driver and
1222 * rate control, it might still start new aggregation
1223 * sessions -- block that to make sure the tear-down
1224 * will be sufficient.
1225 */
c2c98fde 1226 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
c82c4a80 1227 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
098a6070 1228
f59374eb
SS
1229 /*
1230 * Before removing the station from the driver there might be pending
1231 * rx frames on RSS queues sent prior to the disassociation - wait for
1232 * all such frames to be processed.
1233 */
1234 drv_sync_rx_queues(local, sta);
1235
0ad49045
JB
1236 for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
1237 struct link_sta_info *link_sta;
1238
1239 if (!(sta->sta.valid_links & BIT(i)))
1240 continue;
1241
1242 link_sta = rcu_dereference_protected(sta->link[i],
1243 lockdep_is_held(&local->sta_mtx));
1244
1245 link_sta_info_hash_del(local, link_sta);
1246 }
1247
34e89507 1248 ret = sta_info_hash_del(local, sta);
b01711be 1249 if (WARN_ON(ret))
34e89507
JB
1250 return ret;
1251
a7a6bdd0
AN
1252 /*
1253 * for TDLS peers, make sure to return to the base channel before
1254 * removal.
1255 */
1256 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1257 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1258 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1259 }
1260
794454ce 1261 list_del_rcu(&sta->list);
ef044763 1262 sta->removed = true;
4d33960b 1263
6a9d1b91
JB
1264 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
1265
a710c816
JB
1266 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1267 rcu_access_pointer(sdata->u.vlan.sta) == sta)
1268 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1269
d778207b
JB
1270 return 0;
1271}
1272
1273static void __sta_info_destroy_part2(struct sta_info *sta)
1274{
1275 struct ieee80211_local *local = sta->local;
1276 struct ieee80211_sub_if_data *sdata = sta->sdata;
0ef049dc 1277 struct station_info *sinfo;
d778207b
JB
1278 int ret;
1279
1280 /*
1281 * NOTE: This assumes at least synchronize_net() was done
1282 * after _part1 and before _part2!
1283 */
1284
1285 might_sleep();
1286 lockdep_assert_held(&local->sta_mtx);
1287
5981fe5b 1288 if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
b16798f5
JB
1289 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1290 WARN_ON_ONCE(ret);
1291 }
1292
c8782078 1293 /* now keys can no longer be reached */
6d10e46b 1294 ieee80211_free_sta_keys(local, sta);
34e89507 1295
9b7a86f3
JB
1296 /* disable TIM bit - last chance to tell driver */
1297 __sta_info_recalc_tim(sta, true);
1298
34e89507
JB
1299 sta->dead = true;
1300
34e89507
JB
1301 local->num_sta--;
1302 local->sta_generation++;
1303
83d5cc01 1304 while (sta->sta_state > IEEE80211_STA_NONE) {
f09603a2
JB
1305 ret = sta_info_move_state(sta, sta->sta_state - 1);
1306 if (ret) {
83d5cc01
JB
1307 WARN_ON_ONCE(1);
1308 break;
1309 }
1310 }
d9a7ddb0 1311
f09603a2 1312 if (sta->uploaded) {
f09603a2
JB
1313 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1314 IEEE80211_STA_NOTEXIST);
1315 WARN_ON_ONCE(ret != 0);
1316 }
34e89507 1317
bdcbd8e0
JB
1318 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1319
0ef049dc
AB
1320 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1321 if (sinfo)
0fdf1493 1322 sta_set_sinfo(sta, sinfo, true);
0ef049dc
AB
1323 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1324 kfree(sinfo);
ec15e68b 1325
34e89507
JB
1326 ieee80211_sta_debugfs_remove(sta);
1327
3a11ce08
JB
1328 ieee80211_destroy_frag_cache(&sta->frags);
1329
d34ba216 1330 cleanup_single_sta(sta);
d778207b
JB
1331}
1332
1333int __must_check __sta_info_destroy(struct sta_info *sta)
1334{
1335 int err = __sta_info_destroy_part1(sta);
1336
1337 if (err)
1338 return err;
1339
1340 synchronize_net();
1341
1342 __sta_info_destroy_part2(sta);
34e89507
JB
1343
1344 return 0;
4d6141c3
JS
1345}
1346
34e89507 1347int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
4d6141c3 1348{
34e89507
JB
1349 struct sta_info *sta;
1350 int ret;
4d6141c3 1351
34e89507 1352 mutex_lock(&sdata->local->sta_mtx);
7852e361 1353 sta = sta_info_get(sdata, addr);
34e89507
JB
1354 ret = __sta_info_destroy(sta);
1355 mutex_unlock(&sdata->local->sta_mtx);
4d6141c3
JS
1356
1357 return ret;
1358}
1359
34e89507
JB
1360int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1361 const u8 *addr)
e9f207f0 1362{
34e89507
JB
1363 struct sta_info *sta;
1364 int ret;
e9f207f0 1365
34e89507 1366 mutex_lock(&sdata->local->sta_mtx);
7852e361 1367 sta = sta_info_get_bss(sdata, addr);
34e89507
JB
1368 ret = __sta_info_destroy(sta);
1369 mutex_unlock(&sdata->local->sta_mtx);
d0709a65 1370
34e89507
JB
1371 return ret;
1372}
e9f207f0 1373
34f11cd3 1374static void sta_info_cleanup(struct timer_list *t)
34e89507 1375{
34f11cd3 1376 struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
34e89507 1377 struct sta_info *sta;
3393a608 1378 bool timer_needed = false;
34e89507
JB
1379
1380 rcu_read_lock();
1381 list_for_each_entry_rcu(sta, &local->sta_list, list)
3393a608
JO
1382 if (sta_info_cleanup_expire_buffered(local, sta))
1383 timer_needed = true;
34e89507 1384 rcu_read_unlock();
e9f207f0 1385
34e89507
JB
1386 if (local->quiescing)
1387 return;
d0709a65 1388
3393a608
JO
1389 if (!timer_needed)
1390 return;
1391
26d59535
JB
1392 mod_timer(&local->sta_cleanup,
1393 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
e9f207f0 1394}
e9f207f0 1395
7bedd0cf
JB
1396int sta_info_init(struct ieee80211_local *local)
1397{
1398 int err;
1399
83e7e4ce 1400 err = rhltable_init(&local->sta_hash, &sta_rht_params);
7bedd0cf
JB
1401 if (err)
1402 return err;
1403
ba6ddab9
JB
1404 err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1405 if (err) {
1406 rhltable_destroy(&local->sta_hash);
1407 return err;
1408 }
1409
4d33960b 1410 spin_lock_init(&local->tim_lock);
34e89507 1411 mutex_init(&local->sta_mtx);
f0706e82 1412 INIT_LIST_HEAD(&local->sta_list);
f0706e82 1413
34f11cd3 1414 timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
7bedd0cf 1415 return 0;
f0706e82
JB
1416}
1417
1418void sta_info_stop(struct ieee80211_local *local)
1419{
a56f992c 1420 del_timer_sync(&local->sta_cleanup);
83e7e4ce 1421 rhltable_destroy(&local->sta_hash);
ba6ddab9 1422 rhltable_destroy(&local->link_sta_hash);
f0706e82
JB
1423}
1424
051007d9 1425
e716251d 1426int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
f0706e82 1427{
b998e8bb 1428 struct ieee80211_local *local = sdata->local;
f0706e82 1429 struct sta_info *sta, *tmp;
d778207b 1430 LIST_HEAD(free_list);
44213b5e 1431 int ret = 0;
f0706e82 1432
d0709a65 1433 might_sleep();
be8755e1 1434
e716251d
JB
1435 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1436 WARN_ON(vlans && !sdata->bss);
1437
34e89507 1438 mutex_lock(&local->sta_mtx);
d0709a65 1439 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
e716251d
JB
1440 if (sdata == sta->sdata ||
1441 (vlans && sdata->bss == sta->sdata->bss)) {
d778207b
JB
1442 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1443 list_add(&sta->free_list, &free_list);
34316837
JB
1444 ret++;
1445 }
be8755e1 1446 }
d778207b
JB
1447
1448 if (!list_empty(&free_list)) {
1449 synchronize_net();
1450 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1451 __sta_info_destroy_part2(sta);
1452 }
34e89507 1453 mutex_unlock(&local->sta_mtx);
44213b5e 1454
051007d9
JB
1455 return ret;
1456}
1457
24723d1b
JB
1458void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1459 unsigned long exp_time)
1460{
1461 struct ieee80211_local *local = sdata->local;
1462 struct sta_info *sta, *tmp;
24723d1b 1463
34e89507 1464 mutex_lock(&local->sta_mtx);
e46a2cf9
MSS
1465
1466 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
b8da6b6a
JB
1467 unsigned long last_active = ieee80211_sta_last_active(sta);
1468
ec2b774e
ML
1469 if (sdata != sta->sdata)
1470 continue;
1471
b8da6b6a 1472 if (time_is_before_jiffies(last_active + exp_time)) {
eea57d42
MSS
1473 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1474 sta->sta.addr);
3f52b7e3
MP
1475
1476 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1477 test_sta_flag(sta, WLAN_STA_PS_STA))
1478 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1479
34e89507 1480 WARN_ON(__sta_info_destroy(sta));
24723d1b 1481 }
e46a2cf9
MSS
1482 }
1483
34e89507 1484 mutex_unlock(&local->sta_mtx);
24723d1b 1485}
17741cdc 1486
686b9cb9 1487struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
7bedd0cf
JB
1488 const u8 *addr,
1489 const u8 *localaddr)
17741cdc 1490{
7bedd0cf 1491 struct ieee80211_local *local = hw_to_local(hw);
83e7e4ce 1492 struct rhlist_head *tmp;
7bedd0cf 1493 struct sta_info *sta;
17741cdc 1494
686b9cb9
BG
1495 /*
1496 * Just return a random station if localaddr is NULL
1497 * ... first in list.
1498 */
83e7e4ce 1499 for_each_sta_info(local, addr, sta, tmp) {
686b9cb9 1500 if (localaddr &&
b203ca39 1501 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
686b9cb9 1502 continue;
f7c65594
JB
1503 if (!sta->uploaded)
1504 return NULL;
abe60632 1505 return &sta->sta;
f7c65594
JB
1506 }
1507
abe60632 1508 return NULL;
17741cdc 1509}
686b9cb9 1510EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
5ed176e1
JB
1511
1512struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1513 const u8 *addr)
1514{
f7c65594 1515 struct sta_info *sta;
5ed176e1
JB
1516
1517 if (!vif)
1518 return NULL;
1519
f7c65594
JB
1520 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1521 if (!sta)
1522 return NULL;
1523
1524 if (!sta->uploaded)
1525 return NULL;
5ed176e1 1526
f7c65594 1527 return &sta->sta;
5ed176e1 1528}
17741cdc 1529EXPORT_SYMBOL(ieee80211_find_sta);
af818581 1530
e3685e03
JB
1531/* powersave support code */
1532void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
50a9432d 1533{
608383bf 1534 struct ieee80211_sub_if_data *sdata = sta->sdata;
e3685e03
JB
1535 struct ieee80211_local *local = sdata->local;
1536 struct sk_buff_head pending;
ba8c3d6f 1537 int filtered = 0, buffered = 0, ac, i;
e3685e03 1538 unsigned long flags;
d012a605
MP
1539 struct ps_data *ps;
1540
3918edb0
FF
1541 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1542 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1543 u.ap);
1544
1545 if (sdata->vif.type == NL80211_IFTYPE_AP)
d012a605 1546 ps = &sdata->bss->ps;
3f52b7e3
MP
1547 else if (ieee80211_vif_is_mesh(&sdata->vif))
1548 ps = &sdata->u.mesh.ps;
d012a605
MP
1549 else
1550 return;
50a9432d 1551
c2c98fde 1552 clear_sta_flag(sta, WLAN_STA_SP);
47086fc5 1553
5a306f58 1554 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
948d887d 1555 sta->driver_buffered_tids = 0;
ba8c3d6f 1556 sta->txq_buffered_tids = 0;
af818581 1557
30686bf7 1558 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
d057e5a3 1559 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
af818581 1560
adf8ed01
JB
1561 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1562 if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1563 continue;
ba8c3d6f 1564
18667600 1565 schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
ba8c3d6f
FF
1566 }
1567
948d887d 1568 skb_queue_head_init(&pending);
af818581 1569
1d147bfa
EG
1570 /* sync with ieee80211_tx_h_unicast_ps_buf */
1571 spin_lock(&sta->ps_lock);
af818581 1572 /* Send all buffered frames to the station */
948d887d
JB
1573 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1574 int count = skb_queue_len(&pending), tmp;
1575
987c285c 1576 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
948d887d 1577 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
987c285c 1578 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
948d887d
JB
1579 tmp = skb_queue_len(&pending);
1580 filtered += tmp - count;
1581 count = tmp;
1582
987c285c 1583 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
948d887d 1584 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
987c285c 1585 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
948d887d
JB
1586 tmp = skb_queue_len(&pending);
1587 buffered += tmp - count;
1588 }
1589
e3685e03 1590 ieee80211_add_pending_skbs(local, &pending);
5ac2e350
JB
1591
1592 /* now we're no longer in the deliver code */
1593 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1594
1595 /* The station might have polled and then woken up before we responded,
1596 * so clear these flags now to avoid them sticking around.
1597 */
1598 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1599 clear_sta_flag(sta, WLAN_STA_UAPSD);
1d147bfa 1600 spin_unlock(&sta->ps_lock);
948d887d 1601
e3685e03
JB
1602 atomic_dec(&ps->num_sta_ps);
1603
af818581
JB
1604 local->total_ps_buffered -= buffered;
1605
c868cb35
JB
1606 sta_info_recalc_tim(sta);
1607
bdcbd8e0 1608 ps_dbg(sdata,
2595d259 1609 "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
bdcbd8e0 1610 sta->sta.addr, sta->sta.aid, filtered, buffered);
17c18bf8
JB
1611
1612 ieee80211_check_fast_xmit(sta);
af818581
JB
1613}
1614
0ead2510 1615static void ieee80211_send_null_response(struct sta_info *sta, int tid,
b77cf4f8 1616 enum ieee80211_frame_release_type reason,
0ead2510 1617 bool call_driver, bool more_data)
af818581 1618{
0ead2510 1619 struct ieee80211_sub_if_data *sdata = sta->sdata;
af818581 1620 struct ieee80211_local *local = sdata->local;
ce662b44 1621 struct ieee80211_qos_hdr *nullfunc;
af818581 1622 struct sk_buff *skb;
ce662b44
JB
1623 int size = sizeof(*nullfunc);
1624 __le16 fc;
a74a8c84 1625 bool qos = sta->sta.wme;
ce662b44 1626 struct ieee80211_tx_info *info;
55de908a 1627 struct ieee80211_chanctx_conf *chanctx_conf;
af818581 1628
ce662b44
JB
1629 if (qos) {
1630 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1631 IEEE80211_STYPE_QOS_NULLFUNC |
1632 IEEE80211_FCTL_FROMDS);
1633 } else {
1634 size -= 2;
1635 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1636 IEEE80211_STYPE_NULLFUNC |
1637 IEEE80211_FCTL_FROMDS);
af818581 1638 }
af818581 1639
ce662b44
JB
1640 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1641 if (!skb)
1642 return;
1643
1644 skb_reserve(skb, local->hw.extra_tx_headroom);
1645
4df864c1 1646 nullfunc = skb_put(skb, size);
ce662b44
JB
1647 nullfunc->frame_control = fc;
1648 nullfunc->duration_id = 0;
1649 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1650 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1651 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
864a6040 1652 nullfunc->seq_ctrl = 0;
ce662b44 1653
59b66255
JB
1654 skb->priority = tid;
1655 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
ce662b44 1656 if (qos) {
ce662b44
JB
1657 nullfunc->qos_ctrl = cpu_to_le16(tid);
1658
0ead2510 1659 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
ce662b44
JB
1660 nullfunc->qos_ctrl |=
1661 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
0ead2510
EG
1662 if (more_data)
1663 nullfunc->frame_control |=
1664 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1665 }
ce662b44
JB
1666 }
1667
1668 info = IEEE80211_SKB_CB(skb);
1669
1670 /*
1671 * Tell TX path to send this frame even though the
1672 * STA may still remain is PS mode after this frame
deeaee19
JB
1673 * exchange. Also set EOSP to indicate this packet
1674 * ends the poll/service period.
ce662b44 1675 */
02f2f1a9 1676 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
deeaee19
JB
1677 IEEE80211_TX_STATUS_EOSP |
1678 IEEE80211_TX_CTL_REQ_TX_STATUS;
ce662b44 1679
6b127c71
SM
1680 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1681
b77cf4f8
JB
1682 if (call_driver)
1683 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1684 reason, false);
40b96408 1685
89afe614
JB
1686 skb->dev = sdata->dev;
1687
55de908a 1688 rcu_read_lock();
d0a9123e 1689 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
55de908a
JB
1690 if (WARN_ON(!chanctx_conf)) {
1691 rcu_read_unlock();
1692 kfree_skb(skb);
1693 return;
1694 }
1695
73c4e195 1696 info->band = chanctx_conf->def.chan->band;
08aca29a 1697 ieee80211_xmit(sdata, sta, skb);
55de908a 1698 rcu_read_unlock();
ce662b44
JB
1699}
1700
0a1cb809
JB
1701static int find_highest_prio_tid(unsigned long tids)
1702{
1703 /* lower 3 TIDs aren't ordered perfectly */
1704 if (tids & 0xF8)
1705 return fls(tids) - 1;
1706 /* TID 0 is BE just like TID 3 */
1707 if (tids & BIT(0))
1708 return 0;
1709 return fls(tids) - 1;
1710}
1711
0ead2510
EG
1712/* Indicates if the MORE_DATA bit should be set in the last
1713 * frame obtained by ieee80211_sta_ps_get_frames.
1714 * Note that driver_release_tids is relevant only if
1715 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1716 */
1717static bool
1718ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1719 enum ieee80211_frame_release_type reason,
1720 unsigned long driver_release_tids)
1721{
1722 int ac;
1723
1724 /* If the driver has data on more than one TID then
1725 * certainly there's more data if we release just a
1726 * single frame now (from a single TID). This will
1727 * only happen for PS-Poll.
1728 */
1729 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1730 hweight16(driver_release_tids) > 1)
1731 return true;
1732
1733 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
f438ceb8 1734 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
0ead2510
EG
1735 continue;
1736
1737 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1738 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1739 return true;
1740 }
1741
1742 return false;
1743}
1744
47086fc5 1745static void
0ead2510
EG
1746ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1747 enum ieee80211_frame_release_type reason,
1748 struct sk_buff_head *frames,
1749 unsigned long *driver_release_tids)
af818581
JB
1750{
1751 struct ieee80211_sub_if_data *sdata = sta->sdata;
1752 struct ieee80211_local *local = sdata->local;
948d887d 1753 int ac;
948d887d 1754
f9f760b4 1755 /* Get response frame(s) and more data bit for the last one. */
948d887d 1756 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4049e09a
JB
1757 unsigned long tids;
1758
f438ceb8 1759 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
948d887d
JB
1760 continue;
1761
4049e09a
JB
1762 tids = ieee80211_tids_for_ac(ac);
1763
f9f760b4
JB
1764 /* if we already have frames from software, then we can't also
1765 * release from hardware queues
1766 */
0ead2510
EG
1767 if (skb_queue_empty(frames)) {
1768 *driver_release_tids |=
1769 sta->driver_buffered_tids & tids;
1770 *driver_release_tids |= sta->txq_buffered_tids & tids;
ba8c3d6f 1771 }
948d887d 1772
0ead2510 1773 if (!*driver_release_tids) {
f9f760b4
JB
1774 struct sk_buff *skb;
1775
1776 while (n_frames > 0) {
1777 skb = skb_dequeue(&sta->tx_filtered[ac]);
1778 if (!skb) {
1779 skb = skb_dequeue(
1780 &sta->ps_tx_buf[ac]);
1781 if (skb)
1782 local->total_ps_buffered--;
1783 }
1784 if (!skb)
1785 break;
1786 n_frames--;
0ead2510 1787 __skb_queue_tail(frames, skb);
f9f760b4 1788 }
4049e09a 1789 }
948d887d 1790
0ead2510
EG
1791 /* If we have more frames buffered on this AC, then abort the
1792 * loop since we can't send more data from other ACs before
1793 * the buffered frames from this.
f9f760b4 1794 */
948d887d 1795 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
0ead2510 1796 !skb_queue_empty(&sta->ps_tx_buf[ac]))
948d887d 1797 break;
af818581 1798 }
0ead2510
EG
1799}
1800
1801static void
1802ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1803 int n_frames, u8 ignored_acs,
1804 enum ieee80211_frame_release_type reason)
1805{
1806 struct ieee80211_sub_if_data *sdata = sta->sdata;
1807 struct ieee80211_local *local = sdata->local;
1808 unsigned long driver_release_tids = 0;
1809 struct sk_buff_head frames;
1810 bool more_data;
1811
1812 /* Service or PS-Poll period starts */
1813 set_sta_flag(sta, WLAN_STA_SP);
1814
1815 __skb_queue_head_init(&frames);
1816
1817 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1818 &frames, &driver_release_tids);
1819
1820 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1821
1a57081a 1822 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
0ead2510
EG
1823 driver_release_tids =
1824 BIT(find_highest_prio_tid(driver_release_tids));
af818581 1825
f9f760b4 1826 if (skb_queue_empty(&frames) && !driver_release_tids) {
f438ceb8 1827 int tid, ac;
af818581
JB
1828
1829 /*
ce662b44
JB
1830 * For PS-Poll, this can only happen due to a race condition
1831 * when we set the TIM bit and the station notices it, but
1832 * before it can poll for the frame we expire it.
1833 *
1834 * For uAPSD, this is said in the standard (11.2.1.5 h):
1835 * At each unscheduled SP for a non-AP STA, the AP shall
1836 * attempt to transmit at least one MSDU or MMPDU, but no
1837 * more than the value specified in the Max SP Length field
1838 * in the QoS Capability element from delivery-enabled ACs,
1839 * that are destined for the non-AP STA.
1840 *
1841 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
af818581 1842 */
af818581 1843
ce662b44 1844 /* This will evaluate to 1, 3, 5 or 7. */
f438ceb8 1845 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
d7f84244
EG
1846 if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1847 break;
f438ceb8 1848 tid = 7 - 2 * ac;
af818581 1849
0ead2510 1850 ieee80211_send_null_response(sta, tid, reason, true, false);
f9f760b4 1851 } else if (!driver_release_tids) {
47086fc5
JB
1852 struct sk_buff_head pending;
1853 struct sk_buff *skb;
40b96408
JB
1854 int num = 0;
1855 u16 tids = 0;
b77cf4f8 1856 bool need_null = false;
af818581 1857
47086fc5 1858 skb_queue_head_init(&pending);
af818581 1859
47086fc5
JB
1860 while ((skb = __skb_dequeue(&frames))) {
1861 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1862 struct ieee80211_hdr *hdr = (void *) skb->data;
40b96408
JB
1863 u8 *qoshdr = NULL;
1864
1865 num++;
af818581 1866
47086fc5
JB
1867 /*
1868 * Tell TX path to send this frame even though the
1869 * STA may still remain is PS mode after this frame
1870 * exchange.
1871 */
6b127c71
SM
1872 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1873 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
47086fc5
JB
1874
1875 /*
1876 * Use MoreData flag to indicate whether there are
1877 * more buffered frames for this STA
1878 */
24b9c373 1879 if (more_data || !skb_queue_empty(&frames))
47086fc5
JB
1880 hdr->frame_control |=
1881 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
24b9c373
JD
1882 else
1883 hdr->frame_control &=
1884 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
47086fc5 1885
40b96408
JB
1886 if (ieee80211_is_data_qos(hdr->frame_control) ||
1887 ieee80211_is_qos_nullfunc(hdr->frame_control))
1888 qoshdr = ieee80211_get_qos_ctl(hdr);
1889
b77cf4f8 1890 tids |= BIT(skb->priority);
52a3f20c 1891
b77cf4f8
JB
1892 __skb_queue_tail(&pending, skb);
1893
1894 /* end service period after last frame or add one */
1895 if (!skb_queue_empty(&frames))
1896 continue;
1897
1898 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1899 /* for PS-Poll, there's only one frame */
52a3f20c
MP
1900 info->flags |= IEEE80211_TX_STATUS_EOSP |
1901 IEEE80211_TX_CTL_REQ_TX_STATUS;
b77cf4f8 1902 break;
52a3f20c 1903 }
deeaee19 1904
b77cf4f8
JB
1905 /* For uAPSD, things are a bit more complicated. If the
1906 * last frame has a QoS header (i.e. is a QoS-data or
1907 * QoS-nulldata frame) then just set the EOSP bit there
1908 * and be done.
1909 * If the frame doesn't have a QoS header (which means
1910 * it should be a bufferable MMPDU) then we can't set
1911 * the EOSP bit in the QoS header; add a QoS-nulldata
1912 * frame to the list to send it after the MMPDU.
1913 *
1914 * Note that this code is only in the mac80211-release
1915 * code path, we assume that the driver will not buffer
1916 * anything but QoS-data frames, or if it does, will
1917 * create the QoS-nulldata frame by itself if needed.
1918 *
1919 * Cf. 802.11-2012 10.2.1.10 (c).
1920 */
1921 if (qoshdr) {
1922 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
40b96408 1923
b77cf4f8
JB
1924 info->flags |= IEEE80211_TX_STATUS_EOSP |
1925 IEEE80211_TX_CTL_REQ_TX_STATUS;
1926 } else {
1927 /* The standard isn't completely clear on this
1928 * as it says the more-data bit should be set
1929 * if there are more BUs. The QoS-Null frame
1930 * we're about to send isn't buffered yet, we
1931 * only create it below, but let's pretend it
1932 * was buffered just in case some clients only
1933 * expect more-data=0 when eosp=1.
1934 */
1935 hdr->frame_control |=
1936 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1937 need_null = true;
1938 num++;
1939 }
1940 break;
47086fc5 1941 }
af818581 1942
40b96408
JB
1943 drv_allow_buffered_frames(local, sta, tids, num,
1944 reason, more_data);
1945
47086fc5 1946 ieee80211_add_pending_skbs(local, &pending);
af818581 1947
b77cf4f8
JB
1948 if (need_null)
1949 ieee80211_send_null_response(
0ead2510
EG
1950 sta, find_highest_prio_tid(tids),
1951 reason, false, false);
b77cf4f8 1952
c868cb35 1953 sta_info_recalc_tim(sta);
af818581 1954 } else {
ba8c3d6f
FF
1955 int tid;
1956
af818581 1957 /*
4049e09a
JB
1958 * We need to release a frame that is buffered somewhere in the
1959 * driver ... it'll have to handle that.
f9f760b4
JB
1960 * Note that the driver also has to check the number of frames
1961 * on the TIDs we're releasing from - if there are more than
1962 * n_frames it has to set the more-data bit (if we didn't ask
1963 * it to set it anyway due to other buffered frames); if there
1964 * are fewer than n_frames it has to make sure to adjust that
1965 * to allow the service period to end properly.
4049e09a
JB
1966 */
1967 drv_release_buffered_frames(local, sta, driver_release_tids,
47086fc5 1968 n_frames, reason, more_data);
4049e09a
JB
1969
1970 /*
1971 * Note that we don't recalculate the TIM bit here as it would
1972 * most likely have no effect at all unless the driver told us
f9f760b4 1973 * that the TID(s) became empty before returning here from the
4049e09a 1974 * release function.
f9f760b4 1975 * Either way, however, when the driver tells us that the TID(s)
ba8c3d6f
FF
1976 * became empty or we find that a txq became empty, we'll do the
1977 * TIM recalculation.
af818581 1978 */
ba8c3d6f 1979
ba8c3d6f 1980 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
adf8ed01
JB
1981 if (!sta->sta.txq[tid] ||
1982 !(driver_release_tids & BIT(tid)) ||
1e1430d5 1983 txq_has_queue(sta->sta.txq[tid]))
ba8c3d6f
FF
1984 continue;
1985
1986 sta_info_recalc_tim(sta);
1987 break;
1988 }
af818581
JB
1989 }
1990}
1991
47086fc5
JB
1992void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1993{
1994 u8 ignore_for_response = sta->sta.uapsd_queues;
1995
1996 /*
1997 * If all ACs are delivery-enabled then we should reply
1998 * from any of them, if only some are enabled we reply
1999 * only from the non-enabled ones.
2000 */
2001 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
2002 ignore_for_response = 0;
2003
2004 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
2005 IEEE80211_FRAME_RELEASE_PSPOLL);
2006}
2007
2008void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
2009{
2010 int n_frames = sta->sta.max_sp;
2011 u8 delivery_enabled = sta->sta.uapsd_queues;
2012
2013 /*
2014 * If we ever grow support for TSPEC this might happen if
2015 * the TSPEC update from hostapd comes in between a trigger
2016 * frame setting WLAN_STA_UAPSD in the RX path and this
2017 * actually getting called.
2018 */
2019 if (!delivery_enabled)
2020 return;
2021
47086fc5
JB
2022 switch (sta->sta.max_sp) {
2023 case 1:
2024 n_frames = 2;
2025 break;
2026 case 2:
2027 n_frames = 4;
2028 break;
2029 case 3:
2030 n_frames = 6;
2031 break;
2032 case 0:
2033 /* XXX: what is a good value? */
13a8098a 2034 n_frames = 128;
47086fc5
JB
2035 break;
2036 }
2037
2038 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
2039 IEEE80211_FRAME_RELEASE_UAPSD);
2040}
2041
af818581
JB
2042void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
2043 struct ieee80211_sta *pubsta, bool block)
2044{
2045 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2046
b5878a2d
JB
2047 trace_api_sta_block_awake(sta->local, pubsta, block);
2048
5ac2e350 2049 if (block) {
c2c98fde 2050 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
17c18bf8 2051 ieee80211_clear_fast_xmit(sta);
5ac2e350
JB
2052 return;
2053 }
2054
2055 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
2056 return;
2057
2058 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
2059 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
2060 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2061 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2062 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
2063 test_sta_flag(sta, WLAN_STA_UAPSD)) {
2064 /* must be asleep in this case */
2065 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2066 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2067 } else {
2068 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
17c18bf8 2069 ieee80211_check_fast_xmit(sta);
5ac2e350 2070 }
af818581
JB
2071}
2072EXPORT_SYMBOL(ieee80211_sta_block_awake);
dcf55fb5 2073
e943789e 2074void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
37fbd908
JB
2075{
2076 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2077 struct ieee80211_local *local = sta->local;
37fbd908
JB
2078
2079 trace_api_eosp(local, pubsta);
2080
e943789e 2081 clear_sta_flag(sta, WLAN_STA_SP);
37fbd908 2082}
e943789e 2083EXPORT_SYMBOL(ieee80211_sta_eosp);
37fbd908 2084
0ead2510
EG
2085void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
2086{
2087 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2088 enum ieee80211_frame_release_type reason;
2089 bool more_data;
2090
2091 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
2092
2093 reason = IEEE80211_FRAME_RELEASE_UAPSD;
2094 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
2095 reason, 0);
2096
2097 ieee80211_send_null_response(sta, tid, reason, false, more_data);
2098}
2099EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
2100
042ec453
JB
2101void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2102 u8 tid, bool buffered)
dcf55fb5
FF
2103{
2104 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2105
5a306f58 2106 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
042ec453
JB
2107 return;
2108
1b000789
JB
2109 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
2110
948d887d
JB
2111 if (buffered)
2112 set_bit(tid, &sta->driver_buffered_tids);
2113 else
2114 clear_bit(tid, &sta->driver_buffered_tids);
2115
c868cb35 2116 sta_info_recalc_tim(sta);
dcf55fb5 2117}
042ec453 2118EXPORT_SYMBOL(ieee80211_sta_set_buffered);
d9a7ddb0 2119
942741da
FF
2120void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2121 u32 tx_airtime, u32 rx_airtime)
b4809e94 2122{
942741da
FF
2123 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2124 struct ieee80211_local *local = sta->sdata->local;
2125 u8 ac = ieee80211_ac_from_tid(tid);
b4809e94 2126 u32 airtime = 0;
c77bfab9 2127 u32 diff;
b4809e94 2128
942741da 2129 if (sta->local->airtime_flags & AIRTIME_USE_TX)
b4809e94 2130 airtime += tx_airtime;
942741da 2131 if (sta->local->airtime_flags & AIRTIME_USE_RX)
b4809e94
THJ
2132 airtime += rx_airtime;
2133
942741da
FF
2134 spin_lock_bh(&local->active_txq_lock[ac]);
2135 sta->airtime[ac].tx_airtime += tx_airtime;
2136 sta->airtime[ac].rx_airtime += rx_airtime;
c77bfab9
FF
2137
2138 diff = (u32)jiffies - sta->airtime[ac].last_active;
2139 if (diff <= AIRTIME_ACTIVE_DURATION)
2140 sta->airtime[ac].deficit -= airtime;
2141
942741da 2142 spin_unlock_bh(&local->active_txq_lock[ac]);
b4809e94
THJ
2143}
2144EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2145
9b41a9d7 2146void __ieee80211_sta_recalc_aggregates(struct sta_info *sta, u16 active_links)
4c51541d 2147{
4c51541d 2148 bool first = true;
9b41a9d7 2149 int link_id;
4c51541d 2150
9b41a9d7
JB
2151 if (!sta->sta.valid_links || !sta->sta.mlo) {
2152 sta->sta.cur = &sta->sta.deflink.agg;
4c51541d
BB
2153 return;
2154 }
2155
2156 rcu_read_lock();
9b41a9d7
JB
2157 for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) {
2158 struct ieee80211_link_sta *link_sta;
2159 int i;
2160
2161 if (!(active_links & BIT(link_id)))
2162 continue;
2163
2164 link_sta = rcu_dereference(sta->sta.link[link_id]);
2165 if (!link_sta)
2166 continue;
2167
4c51541d 2168 if (first) {
9b41a9d7 2169 sta->cur = sta->sta.deflink.agg;
4c51541d
BB
2170 first = false;
2171 continue;
2172 }
2173
2174 sta->cur.max_amsdu_len =
2175 min(sta->cur.max_amsdu_len,
2176 link_sta->agg.max_amsdu_len);
2177 sta->cur.max_rc_amsdu_len =
2178 min(sta->cur.max_rc_amsdu_len,
2179 link_sta->agg.max_rc_amsdu_len);
2180
2181 for (i = 0; i < ARRAY_SIZE(sta->cur.max_tid_amsdu_len); i++)
2182 sta->cur.max_tid_amsdu_len[i] =
2183 min(sta->cur.max_tid_amsdu_len[i],
2184 link_sta->agg.max_tid_amsdu_len[i]);
2185 }
2186 rcu_read_unlock();
2187
9b41a9d7
JB
2188 sta->sta.cur = &sta->cur;
2189}
2190
2191void ieee80211_sta_recalc_aggregates(struct ieee80211_sta *pubsta)
2192{
2193 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2194
2195 __ieee80211_sta_recalc_aggregates(sta, sta->sdata->vif.active_links);
4c51541d
BB
2196}
2197EXPORT_SYMBOL(ieee80211_sta_recalc_aggregates);
2198
3ace10f5
KY
2199void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
2200 struct sta_info *sta, u8 ac,
2201 u16 tx_airtime, bool tx_completed)
2202{
2203 int tx_pending;
2204
911bde0f
THJ
2205 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2206 return;
2207
3ace10f5
KY
2208 if (!tx_completed) {
2209 if (sta)
2210 atomic_add(tx_airtime,
2211 &sta->airtime[ac].aql_tx_pending);
2212
2213 atomic_add(tx_airtime, &local->aql_total_pending_airtime);
8e4bac06 2214 atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
3ace10f5
KY
2215 return;
2216 }
2217
2218 if (sta) {
2219 tx_pending = atomic_sub_return(tx_airtime,
2220 &sta->airtime[ac].aql_tx_pending);
04e35caa 2221 if (tx_pending < 0)
3ace10f5
KY
2222 atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
2223 tx_pending, 0);
2224 }
2225
8e4bac06 2226 atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
3ace10f5 2227 tx_pending = atomic_sub_return(tx_airtime,
8e4bac06 2228 &local->aql_ac_pending_airtime[ac]);
3ace10f5
KY
2229 if (WARN_ONCE(tx_pending < 0,
2230 "Device %s AC %d pending airtime underflow: %u, %u",
2231 wiphy_name(local->hw.wiphy), ac, tx_pending,
8e4bac06
FF
2232 tx_airtime)) {
2233 atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
3ace10f5 2234 tx_pending, 0);
8e4bac06
FF
2235 atomic_sub(tx_pending, &local->aql_total_pending_airtime);
2236 }
3ace10f5
KY
2237}
2238
83d5cc01
JB
2239int sta_info_move_state(struct sta_info *sta,
2240 enum ieee80211_sta_state new_state)
d9a7ddb0 2241{
8bf11d8d 2242 might_sleep();
d9a7ddb0
JB
2243
2244 if (sta->sta_state == new_state)
2245 return 0;
2246
f09603a2
JB
2247 /* check allowed transitions first */
2248
2249 switch (new_state) {
2250 case IEEE80211_STA_NONE:
2251 if (sta->sta_state != IEEE80211_STA_AUTH)
2252 return -EINVAL;
2253 break;
2254 case IEEE80211_STA_AUTH:
2255 if (sta->sta_state != IEEE80211_STA_NONE &&
2256 sta->sta_state != IEEE80211_STA_ASSOC)
2257 return -EINVAL;
2258 break;
2259 case IEEE80211_STA_ASSOC:
2260 if (sta->sta_state != IEEE80211_STA_AUTH &&
2261 sta->sta_state != IEEE80211_STA_AUTHORIZED)
2262 return -EINVAL;
2263 break;
2264 case IEEE80211_STA_AUTHORIZED:
2265 if (sta->sta_state != IEEE80211_STA_ASSOC)
2266 return -EINVAL;
2267 break;
2268 default:
2269 WARN(1, "invalid state %d", new_state);
2270 return -EINVAL;
2271 }
2272
bdcbd8e0
JB
2273 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2274 sta->sta.addr, new_state);
f09603a2
JB
2275
2276 /*
2277 * notify the driver before the actual changes so it can
2278 * fail the transition
2279 */
2280 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2281 int err = drv_sta_state(sta->local, sta->sdata, sta,
2282 sta->sta_state, new_state);
2283 if (err)
2284 return err;
2285 }
2286
2287 /* reflect the change in all state variables */
2288
d9a7ddb0
JB
2289 switch (new_state) {
2290 case IEEE80211_STA_NONE:
2291 if (sta->sta_state == IEEE80211_STA_AUTH)
2292 clear_bit(WLAN_STA_AUTH, &sta->_flags);
d9a7ddb0
JB
2293 break;
2294 case IEEE80211_STA_AUTH:
a7201a6c 2295 if (sta->sta_state == IEEE80211_STA_NONE) {
d9a7ddb0 2296 set_bit(WLAN_STA_AUTH, &sta->_flags);
a7201a6c 2297 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
d9a7ddb0 2298 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
0cbf348a 2299 ieee80211_recalc_min_chandef(sta->sdata, -1);
52cfa1d6
AB
2300 if (!sta->sta.support_p2p_ps)
2301 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
a7201a6c 2302 }
d9a7ddb0
JB
2303 break;
2304 case IEEE80211_STA_ASSOC:
29623892 2305 if (sta->sta_state == IEEE80211_STA_AUTH) {
d9a7ddb0 2306 set_bit(WLAN_STA_ASSOC, &sta->_flags);
9cf02338 2307 sta->assoc_at = ktime_get_boottime_ns();
0cbf348a 2308 ieee80211_recalc_min_chandef(sta->sdata, -1);
52cfa1d6
AB
2309 if (!sta->sta.support_p2p_ps)
2310 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
29623892 2311 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
72f15d53 2312 ieee80211_vif_dec_num_mcast(sta->sdata);
d9a7ddb0 2313 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
17c18bf8 2314 ieee80211_clear_fast_xmit(sta);
49ddf8e6 2315 ieee80211_clear_fast_rx(sta);
f09603a2 2316 }
d9a7ddb0
JB
2317 break;
2318 case IEEE80211_STA_AUTHORIZED:
29623892 2319 if (sta->sta_state == IEEE80211_STA_ASSOC) {
72f15d53 2320 ieee80211_vif_inc_num_mcast(sta->sdata);
d9a7ddb0 2321 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
17c18bf8 2322 ieee80211_check_fast_xmit(sta);
49ddf8e6 2323 ieee80211_check_fast_rx(sta);
f09603a2 2324 }
3e493173
JM
2325 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2326 sta->sdata->vif.type == NL80211_IFTYPE_AP)
2327 cfg80211_send_layer2_update(sta->sdata->dev,
2328 sta->sta.addr);
d9a7ddb0
JB
2329 break;
2330 default:
f09603a2 2331 break;
d9a7ddb0
JB
2332 }
2333
d9a7ddb0
JB
2334 sta->sta_state = new_state;
2335
2336 return 0;
2337}
687da132 2338
c9c5962b
JB
2339static struct ieee80211_sta_rx_stats *
2340sta_get_last_rx_stats(struct sta_info *sta)
2341{
046d2e7c 2342 struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
c9c5962b
JB
2343 int cpu;
2344
046d2e7c 2345 if (!sta->deflink.pcpu_rx_stats)
c9c5962b
JB
2346 return stats;
2347
2348 for_each_possible_cpu(cpu) {
2349 struct ieee80211_sta_rx_stats *cpustats;
2350
046d2e7c 2351 cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
c9c5962b
JB
2352
2353 if (time_after(cpustats->last_rx, stats->last_rx))
2354 stats = cpustats;
2355 }
2356
2357 return stats;
2358}
2359
41cbb0f5 2360static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
4f6b1b3d 2361 struct rate_info *rinfo)
fbd6ff5c 2362{
dcba665b 2363 rinfo->bw = STA_STATS_GET(BW, rate);
4f6b1b3d 2364
dcba665b 2365 switch (STA_STATS_GET(TYPE, rate)) {
7f406cd1 2366 case STA_STATS_RATE_TYPE_VHT:
4f6b1b3d 2367 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
dcba665b
JB
2368 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2369 rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2370 if (STA_STATS_GET(SGI, rate))
2371 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
7f406cd1
JB
2372 break;
2373 case STA_STATS_RATE_TYPE_HT:
4f6b1b3d 2374 rinfo->flags = RATE_INFO_FLAGS_MCS;
dcba665b
JB
2375 rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2376 if (STA_STATS_GET(SGI, rate))
2377 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
7f406cd1
JB
2378 break;
2379 case STA_STATS_RATE_TYPE_LEGACY: {
fbd6ff5c 2380 struct ieee80211_supported_band *sband;
fbd6ff5c 2381 u16 brate;
4f6b1b3d 2382 unsigned int shift;
dcba665b
JB
2383 int band = STA_STATS_GET(LEGACY_BAND, rate);
2384 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
4f6b1b3d 2385
dcba665b 2386 sband = local->hw.wiphy->bands[band];
8b783d10
TP
2387
2388 if (WARN_ON_ONCE(!sband->bitrates))
2389 break;
2390
dcba665b 2391 brate = sband->bitrates[rate_idx].bitrate;
4f6b1b3d
JB
2392 if (rinfo->bw == RATE_INFO_BW_5)
2393 shift = 2;
2394 else if (rinfo->bw == RATE_INFO_BW_10)
2395 shift = 1;
2396 else
2397 shift = 0;
fbd6ff5c 2398 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
7f406cd1
JB
2399 break;
2400 }
41cbb0f5
LC
2401 case STA_STATS_RATE_TYPE_HE:
2402 rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
2403 rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
2404 rinfo->nss = STA_STATS_GET(HE_NSS, rate);
2405 rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
2406 rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
2407 rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
2408 break;
fbd6ff5c 2409 }
4f6b1b3d
JB
2410}
2411
a17d93ff 2412static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
4f6b1b3d 2413{
6aa7de05 2414 u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
fbd6ff5c 2415
4f6b1b3d 2416 if (rate == STA_STATS_RATE_INVALID)
a17d93ff
BG
2417 return -EINVAL;
2418
2419 sta_stats_decode_rate(sta->local, rate, rinfo);
2420 return 0;
fbd6ff5c
JB
2421}
2422
b255b72b
SM
2423static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2424 int tid)
2425{
2426 unsigned int start;
2427 u64 value;
2428
2429 do {
278d3ba6 2430 start = u64_stats_fetch_begin_irq(&rxstats->syncp);
b255b72b 2431 value = rxstats->msdu[tid];
278d3ba6 2432 } while (u64_stats_fetch_retry_irq(&rxstats->syncp, start));
b255b72b
SM
2433
2434 return value;
2435}
2436
0f9c5a61
JB
2437static void sta_set_tidstats(struct sta_info *sta,
2438 struct cfg80211_tid_stats *tidstats,
2439 int tid)
2440{
2441 struct ieee80211_local *local = sta->local;
b255b72b 2442 int cpu;
0f9c5a61
JB
2443
2444 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
046d2e7c
S
2445 tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2446 tid);
0f9c5a61 2447
046d2e7c 2448 if (sta->deflink.pcpu_rx_stats) {
b255b72b
SM
2449 for_each_possible_cpu(cpu) {
2450 struct ieee80211_sta_rx_stats *cpurxs;
2451
046d2e7c
S
2452 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2453 cpu);
b255b72b
SM
2454 tidstats->rx_msdu +=
2455 sta_get_tidstats_msdu(cpurxs, tid);
2456 }
2457 }
0f9c5a61
JB
2458
2459 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2460 }
2461
2462 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2463 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
046d2e7c 2464 tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
0f9c5a61
JB
2465 }
2466
2467 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2468 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2469 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
046d2e7c 2470 tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
0f9c5a61
JB
2471 }
2472
2473 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2474 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2475 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
046d2e7c 2476 tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
0f9c5a61 2477 }
2fe4a29a 2478
107395f9 2479 if (tid < IEEE80211_NUM_TIDS) {
2fe4a29a
THJ
2480 spin_lock_bh(&local->fq.lock);
2481 rcu_read_lock();
2482
2483 tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2484 ieee80211_fill_txq_stats(&tidstats->txq_stats,
2485 to_txq_info(sta->sta.txq[tid]));
2486
2487 rcu_read_unlock();
2488 spin_unlock_bh(&local->fq.lock);
2489 }
0f9c5a61
JB
2490}
2491
c9c5962b
JB
2492static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2493{
2494 unsigned int start;
2495 u64 value;
2496
2497 do {
278d3ba6 2498 start = u64_stats_fetch_begin_irq(&rxstats->syncp);
c9c5962b 2499 value = rxstats->bytes;
278d3ba6 2500 } while (u64_stats_fetch_retry_irq(&rxstats->syncp, start));
c9c5962b
JB
2501
2502 return value;
2503}
2504
0fdf1493
JB
2505void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
2506 bool tidstats)
b7ffbd7e
JB
2507{
2508 struct ieee80211_sub_if_data *sdata = sta->sdata;
2509 struct ieee80211_local *local = sdata->local;
b7ffbd7e 2510 u32 thr = 0;
c9c5962b
JB
2511 int i, ac, cpu;
2512 struct ieee80211_sta_rx_stats *last_rxstats;
2513
2514 last_rxstats = sta_get_last_rx_stats(sta);
b7ffbd7e
JB
2515
2516 sinfo->generation = sdata->local->sta_generation;
2517
225b8189
JB
2518 /* do before driver, so beacon filtering drivers have a
2519 * chance to e.g. just add the number of filtered beacons
2520 * (or just modify the value entirely, of course)
2521 */
2522 if (sdata->vif.type == NL80211_IFTYPE_STATION)
bfd8403a 2523 sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
225b8189 2524
2b9a7e1b 2525 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
a4217750
OE
2526 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2527 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2528 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2529 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
9cf02338 2530 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
a4217750 2531 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
976bd9ef
JB
2532
2533 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
bfd8403a
JB
2534 sinfo->beacon_loss_count =
2535 sdata->deflink.u.mgd.beacon_loss_count;
a4217750 2536 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
976bd9ef 2537 }
b7ffbd7e 2538
84b00607 2539 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
9cf02338 2540 sinfo->assoc_at = sta->assoc_at;
e5a9f8d0 2541 sinfo->inactive_time =
b8da6b6a 2542 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2b9a7e1b 2543
a4217750
OE
2544 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2545 BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2b9a7e1b
JB
2546 sinfo->tx_bytes = 0;
2547 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
046d2e7c 2548 sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
a4217750 2549 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2b9a7e1b
JB
2550 }
2551
a4217750 2552 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
2b9a7e1b
JB
2553 sinfo->tx_packets = 0;
2554 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
046d2e7c 2555 sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
a4217750 2556 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2b9a7e1b
JB
2557 }
2558
a4217750
OE
2559 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2560 BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
046d2e7c 2561 sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
c9c5962b 2562
046d2e7c 2563 if (sta->deflink.pcpu_rx_stats) {
c9c5962b
JB
2564 for_each_possible_cpu(cpu) {
2565 struct ieee80211_sta_rx_stats *cpurxs;
2566
046d2e7c
S
2567 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2568 cpu);
c9c5962b
JB
2569 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2570 }
2571 }
0f9c5a61 2572
a4217750 2573 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
2b9a7e1b
JB
2574 }
2575
a4217750 2576 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
046d2e7c
S
2577 sinfo->rx_packets = sta->deflink.rx_stats.packets;
2578 if (sta->deflink.pcpu_rx_stats) {
c9c5962b
JB
2579 for_each_possible_cpu(cpu) {
2580 struct ieee80211_sta_rx_stats *cpurxs;
2581
046d2e7c
S
2582 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2583 cpu);
c9c5962b
JB
2584 sinfo->rx_packets += cpurxs->packets;
2585 }
2586 }
a4217750 2587 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2b9a7e1b
JB
2588 }
2589
a4217750 2590 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
046d2e7c 2591 sinfo->tx_retries = sta->deflink.status_stats.retry_count;
a4217750 2592 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
2b9a7e1b
JB
2593 }
2594
a4217750 2595 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
046d2e7c 2596 sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
a4217750 2597 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
b7ffbd7e 2598 }
2b9a7e1b 2599
b4809e94
THJ
2600 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2601 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2602 sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2603 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2604 }
2605
2606 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2607 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2608 sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2609 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2610 }
2611
2612 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
942741da 2613 sinfo->airtime_weight = sta->airtime_weight;
b4809e94
THJ
2614 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2615 }
2616
046d2e7c
S
2617 sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2618 if (sta->deflink.pcpu_rx_stats) {
c9c5962b
JB
2619 for_each_possible_cpu(cpu) {
2620 struct ieee80211_sta_rx_stats *cpurxs;
2621
046d2e7c 2622 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
e165bc02 2623 sinfo->rx_dropped_misc += cpurxs->dropped;
c9c5962b
JB
2624 }
2625 }
b7ffbd7e 2626
225b8189
JB
2627 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2628 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
a4217750
OE
2629 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2630 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
225b8189
JB
2631 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2632 }
2633
30686bf7
JB
2634 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2635 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
a4217750 2636 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
c9c5962b 2637 sinfo->signal = (s8)last_rxstats->last_signal;
a4217750 2638 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2b9a7e1b
JB
2639 }
2640
046d2e7c 2641 if (!sta->deflink.pcpu_rx_stats &&
a4217750 2642 !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
40d9a38a 2643 sinfo->signal_avg =
046d2e7c 2644 -ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
a4217750 2645 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
2b9a7e1b 2646 }
b7ffbd7e 2647 }
2b9a7e1b 2648
c9c5962b
JB
2649 /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2650 * the sta->rx_stats struct, so the check here is fine with and without
2651 * pcpu statistics
2652 */
2653 if (last_rxstats->chains &&
a4217750
OE
2654 !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2655 BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2656 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
046d2e7c 2657 if (!sta->deflink.pcpu_rx_stats)
a4217750 2658 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
c9c5962b
JB
2659
2660 sinfo->chains = last_rxstats->chains;
b7ffbd7e 2661
b7ffbd7e 2662 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
e5a9f8d0 2663 sinfo->chain_signal[i] =
c9c5962b 2664 last_rxstats->chain_signal_last[i];
b7ffbd7e 2665 sinfo->chain_signal_avg[i] =
046d2e7c 2666 -ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
b7ffbd7e
JB
2667 }
2668 }
2669
8a263dcb
JB
2670 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
2671 !sta->sta.valid_links) {
046d2e7c 2672 sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
e5a9f8d0 2673 &sinfo->txrate);
a4217750 2674 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2b9a7e1b
JB
2675 }
2676
8a263dcb
JB
2677 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
2678 !sta->sta.valid_links) {
a17d93ff 2679 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
a4217750 2680 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
2b9a7e1b 2681 }
b7ffbd7e 2682
0fdf1493 2683 if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
6af8354f
JB
2684 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
2685 sta_set_tidstats(sta, &sinfo->pertid[i], i);
79c892b8
JB
2686 }
2687
b7ffbd7e
JB
2688 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2689#ifdef CONFIG_MAC80211_MESH
a4217750
OE
2690 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2691 BIT_ULL(NL80211_STA_INFO_PLID) |
2692 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2693 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2694 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
dbdaee7a 2695 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
1303a51c
MT
2696 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
2697 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
b7ffbd7e 2698
433f5bc1
JB
2699 sinfo->llid = sta->mesh->llid;
2700 sinfo->plid = sta->mesh->plid;
2701 sinfo->plink_state = sta->mesh->plink_state;
b7ffbd7e 2702 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
a4217750 2703 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
433f5bc1 2704 sinfo->t_offset = sta->mesh->t_offset;
b7ffbd7e 2705 }
433f5bc1
JB
2706 sinfo->local_pm = sta->mesh->local_pm;
2707 sinfo->peer_pm = sta->mesh->peer_pm;
2708 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
dbdaee7a 2709 sinfo->connected_to_gate = sta->mesh->connected_to_gate;
1303a51c 2710 sinfo->connected_to_as = sta->mesh->connected_to_as;
b7ffbd7e
JB
2711#endif
2712 }
2713
2714 sinfo->bss_param.flags = 0;
2715 if (sdata->vif.bss_conf.use_cts_prot)
2716 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2717 if (sdata->vif.bss_conf.use_short_preamble)
2718 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2719 if (sdata->vif.bss_conf.use_short_slot)
2720 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
785e21a8 2721 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
b7ffbd7e
JB
2722 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2723
2724 sinfo->sta_flags.set = 0;
2725 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2726 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2727 BIT(NL80211_STA_FLAG_WME) |
2728 BIT(NL80211_STA_FLAG_MFP) |
2729 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2730 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2731 BIT(NL80211_STA_FLAG_TDLS_PEER);
2732 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2733 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2734 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2735 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
a74a8c84 2736 if (sta->sta.wme)
b7ffbd7e
JB
2737 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2738 if (test_sta_flag(sta, WLAN_STA_MFP))
2739 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2740 if (test_sta_flag(sta, WLAN_STA_AUTH))
2741 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2742 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2743 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2744 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2745 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2746
3b17fbf8
MA
2747 thr = sta_get_expected_throughput(sta);
2748
2749 if (thr != 0) {
a4217750 2750 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
3b17fbf8
MA
2751 sinfo->expected_throughput = thr;
2752 }
a78b26ff
VN
2753
2754 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
046d2e7c
S
2755 sta->deflink.status_stats.ack_signal_filled) {
2756 sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
a78b26ff
VN
2757 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2758 }
cc60dbbf 2759
9c06602b 2760 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
046d2e7c 2761 sta->deflink.status_stats.ack_signal_filled) {
cc60dbbf
BP
2762 sinfo->avg_ack_signal =
2763 -(s8)ewma_avg_signal_read(
046d2e7c 2764 &sta->deflink.status_stats.avg_ack_signal);
cc60dbbf 2765 sinfo->filled |=
9c06602b 2766 BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
cc60dbbf 2767 }
ab60633c
NM
2768
2769 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2770 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2771 sinfo->airtime_link_metric =
2772 airtime_link_metric_get(local, sta);
2773 }
3b17fbf8
MA
2774}
2775
2776u32 sta_get_expected_throughput(struct sta_info *sta)
2777{
2778 struct ieee80211_sub_if_data *sdata = sta->sdata;
2779 struct ieee80211_local *local = sdata->local;
2780 struct rate_control_ref *ref = NULL;
2781 u32 thr = 0;
2782
2783 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2784 ref = local->rate_ctrl;
2785
b7ffbd7e
JB
2786 /* check if the driver has a SW RC implementation */
2787 if (ref && ref->ops->get_expected_throughput)
2788 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2789 else
4fdbc67a 2790 thr = drv_get_expected_throughput(local, sta);
b7ffbd7e 2791
3b17fbf8 2792 return thr;
b7ffbd7e 2793}
b8da6b6a
JB
2794
2795unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2796{
c9c5962b
JB
2797 struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2798
046d2e7c
S
2799 if (!sta->deflink.status_stats.last_ack ||
2800 time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
c9c5962b 2801 return stats->last_rx;
046d2e7c 2802 return sta->deflink.status_stats.last_ack;
b8da6b6a 2803}
484a54c2
THJ
2804
2805static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2806{
484a54c2
THJ
2807 if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2808 sta->cparams.target = MS2TIME(50);
2809 sta->cparams.interval = MS2TIME(300);
2810 sta->cparams.ecn = false;
2811 } else {
2812 sta->cparams.target = MS2TIME(20);
2813 sta->cparams.interval = MS2TIME(100);
2814 sta->cparams.ecn = true;
2815 }
2816}
2817
2818void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2819 u32 thr)
2820{
2821 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2822
2823 sta_update_codel_params(sta, thr);
2824}
cb71f1d1
JB
2825
2826int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2827{
2828 struct ieee80211_sub_if_data *sdata = sta->sdata;
2829 struct sta_link_alloc *alloc;
2830 int ret;
2831
2832 lockdep_assert_held(&sdata->local->sta_mtx);
2833
2834 /* must represent an MLD from the start */
2835 if (WARN_ON(!sta->sta.valid_links))
2836 return -EINVAL;
2837
2838 if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2839 sta->link[link_id]))
2840 return -EBUSY;
2841
2842 alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2843 if (!alloc)
2844 return -ENOMEM;
2845
2846 ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2847 if (ret) {
2848 kfree(alloc);
2849 return ret;
2850 }
2851
2852 sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2853
d2caad52
BB
2854 ieee80211_link_sta_debugfs_add(&alloc->info);
2855
cb71f1d1
JB
2856 return 0;
2857}
2858
21476ad1
ST
2859void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
2860{
2861 lockdep_assert_held(&sta->sdata->local->sta_mtx);
2862
2863 sta_remove_link(sta, link_id, false);
2864}
2865
cb71f1d1
JB
2866int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2867{
2868 struct ieee80211_sub_if_data *sdata = sta->sdata;
c71420db 2869 struct link_sta_info *link_sta;
cb71f1d1
JB
2870 u16 old_links = sta->sta.valid_links;
2871 u16 new_links = old_links | BIT(link_id);
2872 int ret;
2873
c71420db
JB
2874 link_sta = rcu_dereference_protected(sta->link[link_id],
2875 lockdep_is_held(&sdata->local->sta_mtx));
cb71f1d1 2876
c71420db 2877 if (WARN_ON(old_links == new_links || !link_sta))
cb71f1d1
JB
2878 return -EINVAL;
2879
177577db
JB
2880 rcu_read_lock();
2881 if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
2882 rcu_read_unlock();
2883 return -EALREADY;
2884 }
2885 /* we only modify under the mutex so this is fine */
2886 rcu_read_unlock();
2887
cb71f1d1
JB
2888 sta->sta.valid_links = new_links;
2889
80e2b1fa 2890 if (!test_sta_flag(sta, WLAN_STA_INSERTED))
ba6ddab9 2891 goto hash;
cb71f1d1 2892
4c51541d
BB
2893 /* Ensure the values are updated for the driver,
2894 * redone by sta_remove_link on failure.
2895 */
2896 ieee80211_sta_recalc_aggregates(&sta->sta);
2897
cb71f1d1
JB
2898 ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2899 old_links, new_links);
2900 if (ret) {
2901 sta->sta.valid_links = old_links;
ba6ddab9 2902 sta_remove_link(sta, link_id, false);
177577db 2903 return ret;
cb71f1d1
JB
2904 }
2905
ba6ddab9 2906hash:
177577db
JB
2907 ret = link_sta_info_hash_add(sdata->local, link_sta);
2908 WARN_ON(ret);
2909 return 0;
cb71f1d1
JB
2910}
2911
2912void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2913{
2914 struct ieee80211_sub_if_data *sdata = sta->sdata;
a8f62399 2915 u16 old_links = sta->sta.valid_links;
cb71f1d1
JB
2916
2917 lockdep_assert_held(&sdata->local->sta_mtx);
2918
2919 sta->sta.valid_links &= ~BIT(link_id);
2920
2921 if (test_sta_flag(sta, WLAN_STA_INSERTED))
2922 drv_change_sta_links(sdata->local, sdata, &sta->sta,
a8f62399 2923 old_links, sta->sta.valid_links);
cb71f1d1 2924
ba6ddab9 2925 sta_remove_link(sta, link_id, true);
cb71f1d1 2926}
175ad2ec
JB
2927
2928void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
2929 const u8 *ext_capab,
2930 unsigned int ext_capab_len)
2931{
2932 u8 val;
2933
2934 sta->sta.max_amsdu_subframes = 0;
2935
2936 if (ext_capab_len < 8)
2937 return;
2938
2939 /* The sender might not have sent the last bit, consider it to be 0 */
2940 val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
2941
2942 /* we did get all the bits, take the MSB as well */
2943 if (ext_capab_len >= 9)
2944 val |= u8_get_bits(ext_capab[8],
2945 WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
2946
2947 if (val)
2948 sta->sta.max_amsdu_subframes = 4 << val;
2949}
65fd846c
JB
2950
2951#ifdef CONFIG_LOCKDEP
2952bool lockdep_sta_mutex_held(struct ieee80211_sta *pubsta)
2953{
2954 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2955
2956 return lockdep_is_held(&sta->local->sta_mtx);
2957}
2958EXPORT_SYMBOL(lockdep_sta_mutex_held);
2959#endif