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