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