]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/mac80211/sta_info.c
m68k/macboing: Fix missed timer callback assignment
[mirror_ubuntu-bionic-kernel.git] / net / mac80211 / sta_info.c
CommitLineData
f0706e82
JB
1/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
d98ad83e 4 * Copyright 2013-2014 Intel Mobile Communications GmbH
dcba665b 5 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
f0706e82
JB
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
888d04df 14#include <linux/etherdevice.h>
f0706e82
JB
15#include <linux/netdevice.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/if_arp.h>
0d174406 20#include <linux/timer.h>
d0709a65 21#include <linux/rtnetlink.h>
f0706e82 22
484a54c2 23#include <net/codel.h>
f0706e82
JB
24#include <net/mac80211.h>
25#include "ieee80211_i.h"
24487981 26#include "driver-ops.h"
2c8dccc7 27#include "rate.h"
f0706e82 28#include "sta_info.h"
e9f207f0 29#include "debugfs_sta.h"
ee385855 30#include "mesh.h"
ce662b44 31#include "wme.h"
f0706e82 32
d0709a65
JB
33/**
34 * DOC: STA information lifetime rules
35 *
36 * STA info structures (&struct sta_info) are managed in a hash table
37 * for faster lookup and a list for iteration. They are managed using
38 * RCU, i.e. access to the list and hash table is protected by RCU.
39 *
34e89507
JB
40 * Upon allocating a STA info structure with sta_info_alloc(), the caller
41 * owns that structure. It must then insert it into the hash table using
42 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
43 * case (which acquires an rcu read section but must not be called from
44 * within one) will the pointer still be valid after the call. Note that
45 * the caller may not do much with the STA info before inserting it, in
46 * particular, it may not start any mesh peer link management or add
47 * encryption keys.
93e5deb1
JB
48 *
49 * When the insertion fails (sta_info_insert()) returns non-zero), the
50 * structure will have been freed by sta_info_insert()!
d0709a65 51 *
34e89507 52 * Station entries are added by mac80211 when you establish a link with a
7e189a12
LR
53 * peer. This means different things for the different type of interfaces
54 * we support. For a regular station this mean we add the AP sta when we
25985edc 55 * receive an association response from the AP. For IBSS this occurs when
34e89507 56 * get to know about a peer on the same IBSS. For WDS we add the sta for
25985edc 57 * the peer immediately upon device open. When using AP mode we add stations
34e89507 58 * for each respective station upon request from userspace through nl80211.
7e189a12 59 *
34e89507
JB
60 * In order to remove a STA info structure, various sta_info_destroy_*()
61 * calls are available.
d0709a65 62 *
34e89507
JB
63 * There is no concept of ownership on a STA entry, each structure is
64 * owned by the global hash table/list until it is removed. All users of
65 * the structure need to be RCU protected so that the structure won't be
66 * freed before they are done using it.
d0709a65 67 */
f0706e82 68
7bedd0cf
JB
69static const struct rhashtable_params sta_rht_params = {
70 .nelem_hint = 3, /* start small */
caf22d31 71 .automatic_shrinking = true,
7bedd0cf 72 .head_offset = offsetof(struct sta_info, hash_node),
ac100ce5 73 .key_offset = offsetof(struct sta_info, addr),
7bedd0cf 74 .key_len = ETH_ALEN,
ebd82b39 75 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
7bedd0cf
JB
76};
77
4d33960b 78/* Caller must hold local->sta_mtx */
be8755e1
MW
79static int sta_info_hash_del(struct ieee80211_local *local,
80 struct sta_info *sta)
f0706e82 81{
83e7e4ce
HX
82 return rhltable_remove(&local->sta_hash, &sta->hash_node,
83 sta_rht_params);
f0706e82
JB
84}
85
5108ca82 86static void __cleanup_single_sta(struct sta_info *sta)
b22cfcfc 87{
b22cfcfc
EP
88 int ac, i;
89 struct tid_ampdu_tx *tid_tx;
90 struct ieee80211_sub_if_data *sdata = sta->sdata;
91 struct ieee80211_local *local = sdata->local;
fa962b92 92 struct fq *fq = &local->fq;
d012a605 93 struct ps_data *ps;
b22cfcfc 94
e3685e03 95 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
5ac2e350
JB
96 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
97 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
d012a605
MP
98 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
99 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
100 ps = &sdata->bss->ps;
3f52b7e3
MP
101 else if (ieee80211_vif_is_mesh(&sdata->vif))
102 ps = &sdata->u.mesh.ps;
d012a605
MP
103 else
104 return;
b22cfcfc
EP
105
106 clear_sta_flag(sta, WLAN_STA_PS_STA);
e3685e03 107 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
5ac2e350 108 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
b22cfcfc 109
d012a605 110 atomic_dec(&ps->num_sta_ps);
b22cfcfc
EP
111 }
112
ba8c3d6f
FF
113 if (sta->sta.txq[0]) {
114 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
115 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
ba8c3d6f 116
fa962b92
MK
117 spin_lock_bh(&fq->lock);
118 ieee80211_txq_purge(local, txqi);
119 spin_unlock_bh(&fq->lock);
ba8c3d6f
FF
120 }
121 }
122
b22cfcfc
EP
123 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
124 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1f98ab7f
FF
125 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
126 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
b22cfcfc
EP
127 }
128
45b5028e
TP
129 if (ieee80211_vif_is_mesh(&sdata->vif))
130 mesh_sta_cleanup(sta);
b22cfcfc 131
5ac2e350 132 cancel_work_sync(&sta->drv_deliver_wk);
b22cfcfc
EP
133
134 /*
135 * Destroy aggregation state here. It would be nice to wait for the
136 * driver to finish aggregation stop and then clean up, but for now
137 * drivers have to handle aggregation stop being requested, followed
138 * directly by station destruction.
139 */
5a306f58 140 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
661eb381 141 kfree(sta->ampdu_mlme.tid_start_tx[i]);
b22cfcfc
EP
142 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
143 if (!tid_tx)
144 continue;
1f98ab7f 145 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
b22cfcfc
EP
146 kfree(tid_tx);
147 }
5108ca82 148}
b22cfcfc 149
5108ca82
JB
150static void cleanup_single_sta(struct sta_info *sta)
151{
152 struct ieee80211_sub_if_data *sdata = sta->sdata;
153 struct ieee80211_local *local = sdata->local;
154
155 __cleanup_single_sta(sta);
b22cfcfc
EP
156 sta_info_free(local, sta);
157}
158
83e7e4ce
HX
159struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
160 const u8 *addr)
161{
162 return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
163}
164
d0709a65 165/* protected by RCU */
abe60632
JB
166struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
167 const u8 *addr)
f0706e82 168{
abe60632 169 struct ieee80211_local *local = sdata->local;
83e7e4ce 170 struct rhlist_head *tmp;
60f4b626 171 struct sta_info *sta;
f0706e82 172
60f4b626 173 rcu_read_lock();
83e7e4ce 174 for_each_sta_info(local, addr, sta, tmp) {
60f4b626
JB
175 if (sta->sdata == sdata) {
176 rcu_read_unlock();
177 /* this is safe as the caller must already hold
178 * another rcu read section or the mutex
179 */
180 return sta;
181 }
182 }
183 rcu_read_unlock();
184 return NULL;
43ba7e95
JB
185}
186
0e5ded5a
FF
187/*
188 * Get sta info either from the specified interface
189 * or from one of its vlans
190 */
191struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
192 const u8 *addr)
193{
194 struct ieee80211_local *local = sdata->local;
83e7e4ce 195 struct rhlist_head *tmp;
0e5ded5a
FF
196 struct sta_info *sta;
197
7bedd0cf 198 rcu_read_lock();
83e7e4ce 199 for_each_sta_info(local, addr, sta, tmp) {
7bedd0cf
JB
200 if (sta->sdata == sdata ||
201 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
202 rcu_read_unlock();
203 /* this is safe as the caller must already hold
204 * another rcu read section or the mutex
205 */
206 return sta;
207 }
0e5ded5a 208 }
7bedd0cf
JB
209 rcu_read_unlock();
210 return NULL;
0e5ded5a
FF
211}
212
3b53fde8
JB
213struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
214 int idx)
ee385855 215{
3b53fde8 216 struct ieee80211_local *local = sdata->local;
ee385855
LCC
217 struct sta_info *sta;
218 int i = 0;
219
d0709a65 220 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3b53fde8 221 if (sdata != sta->sdata)
2a8ca29a 222 continue;
ee385855
LCC
223 if (i < idx) {
224 ++i;
225 continue;
ee385855 226 }
2a8ca29a 227 return sta;
ee385855 228 }
ee385855
LCC
229
230 return NULL;
231}
f0706e82 232
93e5deb1 233/**
d9a7ddb0 234 * sta_info_free - free STA
93e5deb1 235 *
6ef307bc 236 * @local: pointer to the global information
93e5deb1
JB
237 * @sta: STA info to free
238 *
239 * This function must undo everything done by sta_info_alloc()
d9a7ddb0
JB
240 * that may happen before sta_info_insert(). It may only be
241 * called when sta_info_insert() has not been attempted (and
242 * if that fails, the station is freed anyway.)
93e5deb1 243 */
d9a7ddb0 244void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
93e5deb1 245{
889cbb91 246 if (sta->rate_ctrl)
af65cd96 247 rate_control_free_sta(sta);
93e5deb1 248
bdcbd8e0 249 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
93e5deb1 250
ba8c3d6f
FF
251 if (sta->sta.txq[0])
252 kfree(to_txq_info(sta->sta.txq[0]));
53d04525 253 kfree(rcu_dereference_raw(sta->sta.rates));
433f5bc1
JB
254#ifdef CONFIG_MAC80211_MESH
255 kfree(sta->mesh);
256#endif
c9c5962b 257 free_percpu(sta->pcpu_rx_stats);
93e5deb1
JB
258 kfree(sta);
259}
260
4d33960b 261/* Caller must hold local->sta_mtx */
62b14b24
JB
262static int sta_info_hash_add(struct ieee80211_local *local,
263 struct sta_info *sta)
f0706e82 264{
83e7e4ce
HX
265 return rhltable_insert(&local->sta_hash, &sta->hash_node,
266 sta_rht_params);
f0706e82 267}
f0706e82 268
5ac2e350 269static void sta_deliver_ps_frames(struct work_struct *wk)
af818581
JB
270{
271 struct sta_info *sta;
272
5ac2e350 273 sta = container_of(wk, struct sta_info, drv_deliver_wk);
af818581
JB
274
275 if (sta->dead)
276 return;
277
5ac2e350
JB
278 local_bh_disable();
279 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
af818581 280 ieee80211_sta_ps_deliver_wakeup(sta);
5ac2e350 281 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
af818581 282 ieee80211_sta_ps_deliver_poll_response(sta);
5ac2e350 283 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
47086fc5 284 ieee80211_sta_ps_deliver_uapsd(sta);
5ac2e350 285 local_bh_enable();
af818581
JB
286}
287
af65cd96
JB
288static int sta_prepare_rate_control(struct ieee80211_local *local,
289 struct sta_info *sta, gfp_t gfp)
290{
30686bf7 291 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
af65cd96
JB
292 return 0;
293
889cbb91 294 sta->rate_ctrl = local->rate_ctrl;
af65cd96 295 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
35c347ac 296 sta, gfp);
889cbb91 297 if (!sta->rate_ctrl_priv)
af65cd96 298 return -ENOMEM;
af65cd96
JB
299
300 return 0;
301}
302
73651ee6 303struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
56544160 304 const u8 *addr, gfp_t gfp)
f0706e82 305{
d0709a65 306 struct ieee80211_local *local = sdata->local;
ba8c3d6f 307 struct ieee80211_hw *hw = &local->hw;
f0706e82 308 struct sta_info *sta;
16c5f15c 309 int i;
f0706e82 310
ba8c3d6f 311 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
f0706e82 312 if (!sta)
73651ee6 313 return NULL;
f0706e82 314
c9c5962b
JB
315 if (ieee80211_hw_check(hw, USES_RSS)) {
316 sta->pcpu_rx_stats =
317 alloc_percpu(struct ieee80211_sta_rx_stats);
318 if (!sta->pcpu_rx_stats)
319 goto free;
320 }
321
07346f81 322 spin_lock_init(&sta->lock);
1d147bfa 323 spin_lock_init(&sta->ps_lock);
5ac2e350 324 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
67c282c0 325 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
a93e3644 326 mutex_init(&sta->ampdu_mlme.mtx);
87f59c70 327#ifdef CONFIG_MAC80211_MESH
433f5bc1
JB
328 if (ieee80211_vif_is_mesh(&sdata->vif)) {
329 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
330 if (!sta->mesh)
331 goto free;
4c02d62f 332 sta->mesh->plink_sta = sta;
433f5bc1
JB
333 spin_lock_init(&sta->mesh->plink_lock);
334 if (ieee80211_vif_is_mesh(&sdata->vif) &&
335 !sdata->u.mesh.user_mpm)
4c02d62f
KC
336 timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
337 0);
433f5bc1
JB
338 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
339 }
87f59c70 340#endif
07346f81 341
ac100ce5 342 memcpy(sta->addr, addr, ETH_ALEN);
17741cdc 343 memcpy(sta->sta.addr, addr, ETH_ALEN);
480dd46b
MA
344 sta->sta.max_rx_aggregation_subframes =
345 local->hw.max_rx_aggregation_subframes;
346
d0709a65
JB
347 sta->local = local;
348 sta->sdata = sdata;
e5a9f8d0 349 sta->rx_stats.last_rx = jiffies;
f0706e82 350
0f9c5a61
JB
351 u64_stats_init(&sta->rx_stats.syncp);
352
71ec375c
JB
353 sta->sta_state = IEEE80211_STA_NONE;
354
b6da911b
LK
355 /* Mark TID as unreserved */
356 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
357
84b00607 358 sta->last_connected = ktime_get_seconds();
0be6ed13
JB
359 ewma_signal_init(&sta->rx_stats_avg.signal);
360 for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
361 ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
541a45a1 362
ba8c3d6f
FF
363 if (local->ops->wake_tx_queue) {
364 void *txq_data;
365 int size = sizeof(struct txq_info) +
366 ALIGN(hw->txq_data_size, sizeof(void *));
367
368 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
369 if (!txq_data)
370 goto free;
371
372 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
373 struct txq_info *txq = txq_data + i * size;
374
fa962b92 375 ieee80211_txq_init(sdata, sta, txq, i);
ba8c3d6f 376 }
abfbc3af 377 }
f0706e82 378
ba8c3d6f
FF
379 if (sta_prepare_rate_control(local, sta, gfp))
380 goto free_txq;
381
948d887d
JB
382 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
383 skb_queue_head_init(&sta->ps_tx_buf[i]);
384 skb_queue_head_init(&sta->tx_filtered[i]);
385 }
73651ee6 386
5a306f58 387 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
4be929be 388 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
cccaec98 389
af0ed69b 390 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
687da132
EG
391 if (sdata->vif.type == NL80211_IFTYPE_AP ||
392 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
21a8e9dd
MSS
393 struct ieee80211_supported_band *sband;
394 u8 smps;
395
396 sband = ieee80211_get_sband(sdata);
397 if (!sband)
398 goto free_txq;
399
400 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
401 IEEE80211_HT_CAP_SM_PS_SHIFT;
687da132
EG
402 /*
403 * Assume that hostapd advertises our caps in the beacon and
404 * this is the known_smps_mode for a station that just assciated
405 */
406 switch (smps) {
407 case WLAN_HT_SMPS_CONTROL_DISABLED:
408 sta->known_smps_mode = IEEE80211_SMPS_OFF;
409 break;
410 case WLAN_HT_SMPS_CONTROL_STATIC:
411 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
412 break;
413 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
414 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
415 break;
416 default:
417 WARN_ON(1);
418 }
419 }
af0ed69b 420
6e0456b5
FF
421 sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
422
484a54c2
THJ
423 sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
424 sta->cparams.target = MS2TIME(20);
425 sta->cparams.interval = MS2TIME(100);
426 sta->cparams.ecn = true;
427
bdcbd8e0 428 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
ef04a297 429
abfbc3af 430 return sta;
ba8c3d6f
FF
431
432free_txq:
433 if (sta->sta.txq[0])
434 kfree(to_txq_info(sta->sta.txq[0]));
435free:
433f5bc1
JB
436#ifdef CONFIG_MAC80211_MESH
437 kfree(sta->mesh);
438#endif
ba8c3d6f
FF
439 kfree(sta);
440 return NULL;
73651ee6
JB
441}
442
8c71df7a 443static int sta_info_insert_check(struct sta_info *sta)
34e89507 444{
34e89507 445 struct ieee80211_sub_if_data *sdata = sta->sdata;
34e89507 446
03e4497e
JB
447 /*
448 * Can't be a WARN_ON because it can be triggered through a race:
449 * something inserts a STA (on one CPU) without holding the RTNL
450 * and another CPU turns off the net device.
451 */
8c71df7a
GE
452 if (unlikely(!ieee80211_sdata_running(sdata)))
453 return -ENETDOWN;
03e4497e 454
b203ca39 455 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
8c71df7a
GE
456 is_multicast_ether_addr(sta->sta.addr)))
457 return -EINVAL;
458
83e7e4ce
HX
459 /* The RCU read lock is required by rhashtable due to
460 * asynchronous resize/rehash. We also require the mutex
461 * for correctness.
31104891
JB
462 */
463 rcu_read_lock();
464 lockdep_assert_held(&sdata->local->sta_mtx);
465 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
466 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
467 rcu_read_unlock();
468 return -ENOTUNIQ;
469 }
470 rcu_read_unlock();
471
8c71df7a
GE
472 return 0;
473}
474
f09603a2
JB
475static int sta_info_insert_drv_state(struct ieee80211_local *local,
476 struct ieee80211_sub_if_data *sdata,
477 struct sta_info *sta)
478{
479 enum ieee80211_sta_state state;
480 int err = 0;
481
482 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
483 err = drv_sta_state(local, sdata, sta, state, state + 1);
484 if (err)
485 break;
486 }
487
488 if (!err) {
a4ec45a4
JB
489 /*
490 * Drivers using legacy sta_add/sta_remove callbacks only
491 * get uploaded set to true after sta_add is called.
492 */
493 if (!local->ops->sta_add)
494 sta->uploaded = true;
f09603a2
JB
495 return 0;
496 }
497
498 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
bdcbd8e0
JB
499 sdata_info(sdata,
500 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
501 sta->sta.addr, state + 1, err);
f09603a2
JB
502 err = 0;
503 }
504
505 /* unwind on error */
506 for (; state > IEEE80211_STA_NOTEXIST; state--)
507 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
508
509 return err;
510}
511
d405fd8c
GG
512static void
513ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
514{
515 struct ieee80211_local *local = sdata->local;
516 bool allow_p2p_go_ps = sdata->vif.p2p;
517 struct sta_info *sta;
518
519 rcu_read_lock();
520 list_for_each_entry_rcu(sta, &local->sta_list, list) {
521 if (sdata != sta->sdata ||
522 !test_sta_flag(sta, WLAN_STA_ASSOC))
523 continue;
524 if (!sta->sta.support_p2p_ps) {
525 allow_p2p_go_ps = false;
526 break;
527 }
528 }
529 rcu_read_unlock();
530
531 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
532 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
533 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
534 }
535}
536
8c71df7a
GE
537/*
538 * should be called with sta_mtx locked
539 * this function replaces the mutex lock
540 * with a RCU lock
541 */
4d33960b 542static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
8c71df7a
GE
543{
544 struct ieee80211_local *local = sta->local;
545 struct ieee80211_sub_if_data *sdata = sta->sdata;
0c2e3842 546 struct station_info *sinfo = NULL;
8c71df7a
GE
547 int err = 0;
548
549 lockdep_assert_held(&local->sta_mtx);
34e89507 550
7852e361
JB
551 /* check if STA exists already */
552 if (sta_info_get_bss(sdata, sta->sta.addr)) {
553 err = -EEXIST;
554 goto out_err;
4d33960b 555 }
32bfd35d 556
0c2e3842
KV
557 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
558 if (!sinfo) {
559 err = -ENOMEM;
560 goto out_err;
561 }
562
7852e361
JB
563 local->num_sta++;
564 local->sta_generation++;
565 smp_mb();
4d33960b 566
5108ca82
JB
567 /* simplify things and don't accept BA sessions yet */
568 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
569
7852e361 570 /* make the station visible */
62b14b24
JB
571 err = sta_info_hash_add(local, sta);
572 if (err)
573 goto out_drop_sta;
83d5cc01 574
2bad7748 575 list_add_tail_rcu(&sta->list, &local->sta_list);
4d33960b 576
5108ca82
JB
577 /* notify driver */
578 err = sta_info_insert_drv_state(local, sdata, sta);
579 if (err)
580 goto out_remove;
581
7852e361 582 set_sta_flag(sta, WLAN_STA_INSERTED);
d405fd8c
GG
583
584 if (sta->sta_state >= IEEE80211_STA_ASSOC) {
585 ieee80211_recalc_min_chandef(sta->sdata);
586 if (!sta->sta.support_p2p_ps)
587 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
588 }
589
5108ca82
JB
590 /* accept BA sessions now */
591 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
4d33960b 592
7852e361
JB
593 ieee80211_sta_debugfs_add(sta);
594 rate_control_add_sta_debugfs(sta);
4d33960b 595
0ef049dc
AB
596 sinfo->generation = local->sta_generation;
597 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
598 kfree(sinfo);
d0709a65 599
bdcbd8e0 600 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
f0706e82 601
34e89507
JB
602 /* move reference to rcu-protected */
603 rcu_read_lock();
604 mutex_unlock(&local->sta_mtx);
e9f207f0 605
73651ee6
JB
606 if (ieee80211_vif_is_mesh(&sdata->vif))
607 mesh_accept_plinks_update(sdata);
608
8c71df7a 609 return 0;
5108ca82
JB
610 out_remove:
611 sta_info_hash_del(local, sta);
612 list_del_rcu(&sta->list);
62b14b24 613 out_drop_sta:
5108ca82
JB
614 local->num_sta--;
615 synchronize_net();
616 __cleanup_single_sta(sta);
4d33960b
JB
617 out_err:
618 mutex_unlock(&local->sta_mtx);
ea32f065 619 kfree(sinfo);
4d33960b
JB
620 rcu_read_lock();
621 return err;
8c71df7a
GE
622}
623
624int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
625{
626 struct ieee80211_local *local = sta->local;
308f7fcf 627 int err;
8c71df7a 628
4d33960b
JB
629 might_sleep();
630
31104891
JB
631 mutex_lock(&local->sta_mtx);
632
8c71df7a
GE
633 err = sta_info_insert_check(sta);
634 if (err) {
31104891 635 mutex_unlock(&local->sta_mtx);
8c71df7a
GE
636 rcu_read_lock();
637 goto out_free;
638 }
639
4d33960b 640 err = sta_info_insert_finish(sta);
8c71df7a
GE
641 if (err)
642 goto out_free;
643
73651ee6 644 return 0;
93e5deb1 645 out_free:
d9a7ddb0 646 sta_info_free(local, sta);
93e5deb1 647 return err;
f0706e82
JB
648}
649
34e89507
JB
650int sta_info_insert(struct sta_info *sta)
651{
652 int err = sta_info_insert_rcu(sta);
653
654 rcu_read_unlock();
655
656 return err;
657}
658
d012a605 659static inline void __bss_tim_set(u8 *tim, u16 id)
004c872e
JB
660{
661 /*
662 * This format has been mandated by the IEEE specifications,
663 * so this line may not be changed to use the __set_bit() format.
664 */
d012a605 665 tim[id / 8] |= (1 << (id % 8));
004c872e
JB
666}
667
d012a605 668static inline void __bss_tim_clear(u8 *tim, u16 id)
004c872e
JB
669{
670 /*
671 * This format has been mandated by the IEEE specifications,
672 * so this line may not be changed to use the __clear_bit() format.
673 */
d012a605 674 tim[id / 8] &= ~(1 << (id % 8));
004c872e
JB
675}
676
3d5839b6
IP
677static inline bool __bss_tim_get(u8 *tim, u16 id)
678{
679 /*
680 * This format has been mandated by the IEEE specifications,
681 * so this line may not be changed to use the test_bit() format.
682 */
683 return tim[id / 8] & (1 << (id % 8));
684}
685
948d887d 686static unsigned long ieee80211_tids_for_ac(int ac)
004c872e 687{
948d887d
JB
688 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
689 switch (ac) {
690 case IEEE80211_AC_VO:
691 return BIT(6) | BIT(7);
692 case IEEE80211_AC_VI:
693 return BIT(4) | BIT(5);
694 case IEEE80211_AC_BE:
695 return BIT(0) | BIT(3);
696 case IEEE80211_AC_BK:
697 return BIT(1) | BIT(2);
698 default:
699 WARN_ON(1);
700 return 0;
d0709a65 701 }
004c872e
JB
702}
703
9b7a86f3 704static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
004c872e 705{
c868cb35 706 struct ieee80211_local *local = sta->local;
d012a605 707 struct ps_data *ps;
948d887d
JB
708 bool indicate_tim = false;
709 u8 ignore_for_tim = sta->sta.uapsd_queues;
710 int ac;
a69bd8e6 711 u16 id = sta->sta.aid;
d012a605
MP
712
713 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
714 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
715 if (WARN_ON_ONCE(!sta->sdata->bss))
716 return;
004c872e 717
d012a605 718 ps = &sta->sdata->bss->ps;
3f52b7e3
MP
719#ifdef CONFIG_MAC80211_MESH
720 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
721 ps = &sta->sdata->u.mesh.ps;
3f52b7e3 722#endif
d012a605 723 } else {
c868cb35 724 return;
d012a605 725 }
3e122be0 726
c868cb35 727 /* No need to do anything if the driver does all */
d98937f4 728 if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
c868cb35 729 return;
004c872e 730
c868cb35
JB
731 if (sta->dead)
732 goto done;
3e122be0 733
948d887d
JB
734 /*
735 * If all ACs are delivery-enabled then we should build
736 * the TIM bit for all ACs anyway; if only some are then
737 * we ignore those and build the TIM bit using only the
738 * non-enabled ones.
739 */
740 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
741 ignore_for_tim = 0;
742
9b7a86f3
JB
743 if (ignore_pending)
744 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
745
948d887d
JB
746 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
747 unsigned long tids;
3e122be0 748
f438ceb8 749 if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
948d887d
JB
750 continue;
751
752 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
753 !skb_queue_empty(&sta->ps_tx_buf[ac]);
754 if (indicate_tim)
755 break;
3e122be0 756
948d887d
JB
757 tids = ieee80211_tids_for_ac(ac);
758
759 indicate_tim |=
760 sta->driver_buffered_tids & tids;
ba8c3d6f
FF
761 indicate_tim |=
762 sta->txq_buffered_tids & tids;
d0709a65 763 }
004c872e 764
c868cb35 765 done:
65f704a5 766 spin_lock_bh(&local->tim_lock);
004c872e 767
3d5839b6
IP
768 if (indicate_tim == __bss_tim_get(ps->tim, id))
769 goto out_unlock;
770
948d887d 771 if (indicate_tim)
d012a605 772 __bss_tim_set(ps->tim, id);
c868cb35 773 else
d012a605 774 __bss_tim_clear(ps->tim, id);
004c872e 775
9b7a86f3 776 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
c868cb35 777 local->tim_in_locked_section = true;
948d887d 778 drv_set_tim(local, &sta->sta, indicate_tim);
c868cb35
JB
779 local->tim_in_locked_section = false;
780 }
3e122be0 781
3d5839b6 782out_unlock:
65f704a5 783 spin_unlock_bh(&local->tim_lock);
004c872e
JB
784}
785
9b7a86f3
JB
786void sta_info_recalc_tim(struct sta_info *sta)
787{
788 __sta_info_recalc_tim(sta, false);
789}
790
cd0b8d89 791static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
f0706e82 792{
e039fa4a 793 struct ieee80211_tx_info *info;
f0706e82
JB
794 int timeout;
795
796 if (!skb)
cd0b8d89 797 return false;
f0706e82 798
e039fa4a 799 info = IEEE80211_SKB_CB(skb);
f0706e82
JB
800
801 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
57c4d7b4
JB
802 timeout = (sta->listen_interval *
803 sta->sdata->vif.bss_conf.beacon_int *
804 32 / 15625) * HZ;
f0706e82
JB
805 if (timeout < STA_TX_BUFFER_EXPIRE)
806 timeout = STA_TX_BUFFER_EXPIRE;
e039fa4a 807 return time_after(jiffies, info->control.jiffies + timeout);
f0706e82
JB
808}
809
810
948d887d
JB
811static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
812 struct sta_info *sta, int ac)
f0706e82
JB
813{
814 unsigned long flags;
815 struct sk_buff *skb;
816
60750397
JB
817 /*
818 * First check for frames that should expire on the filtered
819 * queue. Frames here were rejected by the driver and are on
820 * a separate queue to avoid reordering with normal PS-buffered
821 * frames. They also aren't accounted for right now in the
822 * total_ps_buffered counter.
823 */
824 for (;;) {
948d887d
JB
825 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
826 skb = skb_peek(&sta->tx_filtered[ac]);
60750397 827 if (sta_info_buffer_expired(sta, skb))
948d887d 828 skb = __skb_dequeue(&sta->tx_filtered[ac]);
60750397
JB
829 else
830 skb = NULL;
948d887d 831 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
60750397
JB
832
833 /*
834 * Frames are queued in order, so if this one
835 * hasn't expired yet we can stop testing. If
836 * we actually reached the end of the queue we
837 * also need to stop, of course.
838 */
839 if (!skb)
840 break;
d4fa14cd 841 ieee80211_free_txskb(&local->hw, skb);
60750397
JB
842 }
843
844 /*
845 * Now also check the normal PS-buffered queue, this will
846 * only find something if the filtered queue was emptied
847 * since the filtered frames are all before the normal PS
848 * buffered frames.
849 */
f0706e82 850 for (;;) {
948d887d
JB
851 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
852 skb = skb_peek(&sta->ps_tx_buf[ac]);
57c4d7b4 853 if (sta_info_buffer_expired(sta, skb))
948d887d 854 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
836341a7 855 else
f0706e82 856 skb = NULL;
948d887d 857 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
f0706e82 858
60750397
JB
859 /*
860 * frames are queued in order, so if this one
861 * hasn't expired yet (or we reached the end of
862 * the queue) we can stop testing
863 */
836341a7 864 if (!skb)
f0706e82 865 break;
836341a7 866
836341a7 867 local->total_ps_buffered--;
bdcbd8e0
JB
868 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
869 sta->sta.addr);
d4fa14cd 870 ieee80211_free_txskb(&local->hw, skb);
f0706e82 871 }
3393a608 872
60750397
JB
873 /*
874 * Finally, recalculate the TIM bit for this station -- it might
875 * now be clear because the station was too slow to retrieve its
876 * frames.
877 */
878 sta_info_recalc_tim(sta);
879
880 /*
881 * Return whether there are any frames still buffered, this is
882 * used to check whether the cleanup timer still needs to run,
883 * if there are no frames we don't need to rearm the timer.
884 */
948d887d
JB
885 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
886 skb_queue_empty(&sta->tx_filtered[ac]));
887}
888
889static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
890 struct sta_info *sta)
891{
892 bool have_buffered = false;
893 int ac;
894
3f52b7e3
MP
895 /* This is only necessary for stations on BSS/MBSS interfaces */
896 if (!sta->sdata->bss &&
897 !ieee80211_vif_is_mesh(&sta->sdata->vif))
948d887d
JB
898 return false;
899
900 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
901 have_buffered |=
902 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
903
904 return have_buffered;
f0706e82
JB
905}
906
d778207b 907static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
f0706e82 908{
34e89507
JB
909 struct ieee80211_local *local;
910 struct ieee80211_sub_if_data *sdata;
6d10e46b 911 int ret;
f0706e82 912
34e89507 913 might_sleep();
f0706e82 914
34e89507
JB
915 if (!sta)
916 return -ENOENT;
5bb644a0 917
34e89507
JB
918 local = sta->local;
919 sdata = sta->sdata;
f0706e82 920
83d5cc01
JB
921 lockdep_assert_held(&local->sta_mtx);
922
098a6070
JB
923 /*
924 * Before removing the station from the driver and
925 * rate control, it might still start new aggregation
926 * sessions -- block that to make sure the tear-down
927 * will be sufficient.
928 */
c2c98fde 929 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
c82c4a80 930 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
098a6070 931
f59374eb
SS
932 /*
933 * Before removing the station from the driver there might be pending
934 * rx frames on RSS queues sent prior to the disassociation - wait for
935 * all such frames to be processed.
936 */
937 drv_sync_rx_queues(local, sta);
938
34e89507 939 ret = sta_info_hash_del(local, sta);
b01711be 940 if (WARN_ON(ret))
34e89507
JB
941 return ret;
942
a7a6bdd0
AN
943 /*
944 * for TDLS peers, make sure to return to the base channel before
945 * removal.
946 */
947 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
948 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
949 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
950 }
951
794454ce 952 list_del_rcu(&sta->list);
ef044763 953 sta->removed = true;
4d33960b 954
6a9d1b91
JB
955 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
956
a710c816
JB
957 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
958 rcu_access_pointer(sdata->u.vlan.sta) == sta)
959 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
960
d778207b
JB
961 return 0;
962}
963
964static void __sta_info_destroy_part2(struct sta_info *sta)
965{
966 struct ieee80211_local *local = sta->local;
967 struct ieee80211_sub_if_data *sdata = sta->sdata;
0ef049dc 968 struct station_info *sinfo;
d778207b
JB
969 int ret;
970
971 /*
972 * NOTE: This assumes at least synchronize_net() was done
973 * after _part1 and before _part2!
974 */
975
976 might_sleep();
977 lockdep_assert_held(&local->sta_mtx);
978
c8782078 979 /* now keys can no longer be reached */
6d10e46b 980 ieee80211_free_sta_keys(local, sta);
34e89507 981
9b7a86f3
JB
982 /* disable TIM bit - last chance to tell driver */
983 __sta_info_recalc_tim(sta, true);
984
34e89507
JB
985 sta->dead = true;
986
34e89507
JB
987 local->num_sta--;
988 local->sta_generation++;
989
83d5cc01 990 while (sta->sta_state > IEEE80211_STA_NONE) {
f09603a2
JB
991 ret = sta_info_move_state(sta, sta->sta_state - 1);
992 if (ret) {
83d5cc01
JB
993 WARN_ON_ONCE(1);
994 break;
995 }
996 }
d9a7ddb0 997
f09603a2 998 if (sta->uploaded) {
f09603a2
JB
999 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1000 IEEE80211_STA_NOTEXIST);
1001 WARN_ON_ONCE(ret != 0);
1002 }
34e89507 1003
bdcbd8e0
JB
1004 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1005
0ef049dc
AB
1006 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1007 if (sinfo)
1008 sta_set_sinfo(sta, sinfo);
1009 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1010 kfree(sinfo);
ec15e68b 1011
34e89507
JB
1012 rate_control_remove_sta_debugfs(sta);
1013 ieee80211_sta_debugfs_remove(sta);
1014
d34ba216 1015 cleanup_single_sta(sta);
d778207b
JB
1016}
1017
1018int __must_check __sta_info_destroy(struct sta_info *sta)
1019{
1020 int err = __sta_info_destroy_part1(sta);
1021
1022 if (err)
1023 return err;
1024
1025 synchronize_net();
1026
1027 __sta_info_destroy_part2(sta);
34e89507
JB
1028
1029 return 0;
4d6141c3
JS
1030}
1031
34e89507 1032int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
4d6141c3 1033{
34e89507
JB
1034 struct sta_info *sta;
1035 int ret;
4d6141c3 1036
34e89507 1037 mutex_lock(&sdata->local->sta_mtx);
7852e361 1038 sta = sta_info_get(sdata, addr);
34e89507
JB
1039 ret = __sta_info_destroy(sta);
1040 mutex_unlock(&sdata->local->sta_mtx);
4d6141c3
JS
1041
1042 return ret;
1043}
1044
34e89507
JB
1045int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1046 const u8 *addr)
e9f207f0 1047{
34e89507
JB
1048 struct sta_info *sta;
1049 int ret;
e9f207f0 1050
34e89507 1051 mutex_lock(&sdata->local->sta_mtx);
7852e361 1052 sta = sta_info_get_bss(sdata, addr);
34e89507
JB
1053 ret = __sta_info_destroy(sta);
1054 mutex_unlock(&sdata->local->sta_mtx);
d0709a65 1055
34e89507
JB
1056 return ret;
1057}
e9f207f0 1058
34f11cd3 1059static void sta_info_cleanup(struct timer_list *t)
34e89507 1060{
34f11cd3 1061 struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
34e89507 1062 struct sta_info *sta;
3393a608 1063 bool timer_needed = false;
34e89507
JB
1064
1065 rcu_read_lock();
1066 list_for_each_entry_rcu(sta, &local->sta_list, list)
3393a608
JO
1067 if (sta_info_cleanup_expire_buffered(local, sta))
1068 timer_needed = true;
34e89507 1069 rcu_read_unlock();
e9f207f0 1070
34e89507
JB
1071 if (local->quiescing)
1072 return;
d0709a65 1073
3393a608
JO
1074 if (!timer_needed)
1075 return;
1076
26d59535
JB
1077 mod_timer(&local->sta_cleanup,
1078 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
e9f207f0 1079}
e9f207f0 1080
7bedd0cf
JB
1081int sta_info_init(struct ieee80211_local *local)
1082{
1083 int err;
1084
83e7e4ce 1085 err = rhltable_init(&local->sta_hash, &sta_rht_params);
7bedd0cf
JB
1086 if (err)
1087 return err;
1088
4d33960b 1089 spin_lock_init(&local->tim_lock);
34e89507 1090 mutex_init(&local->sta_mtx);
f0706e82 1091 INIT_LIST_HEAD(&local->sta_list);
f0706e82 1092
34f11cd3 1093 timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
7bedd0cf 1094 return 0;
f0706e82
JB
1095}
1096
1097void sta_info_stop(struct ieee80211_local *local)
1098{
a56f992c 1099 del_timer_sync(&local->sta_cleanup);
83e7e4ce 1100 rhltable_destroy(&local->sta_hash);
f0706e82
JB
1101}
1102
051007d9 1103
e716251d 1104int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
f0706e82 1105{
b998e8bb 1106 struct ieee80211_local *local = sdata->local;
f0706e82 1107 struct sta_info *sta, *tmp;
d778207b 1108 LIST_HEAD(free_list);
44213b5e 1109 int ret = 0;
f0706e82 1110
d0709a65 1111 might_sleep();
be8755e1 1112
e716251d
JB
1113 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1114 WARN_ON(vlans && !sdata->bss);
1115
34e89507 1116 mutex_lock(&local->sta_mtx);
d0709a65 1117 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
e716251d
JB
1118 if (sdata == sta->sdata ||
1119 (vlans && sdata->bss == sta->sdata->bss)) {
d778207b
JB
1120 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1121 list_add(&sta->free_list, &free_list);
34316837
JB
1122 ret++;
1123 }
be8755e1 1124 }
d778207b
JB
1125
1126 if (!list_empty(&free_list)) {
1127 synchronize_net();
1128 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1129 __sta_info_destroy_part2(sta);
1130 }
34e89507 1131 mutex_unlock(&local->sta_mtx);
44213b5e 1132
051007d9
JB
1133 return ret;
1134}
1135
24723d1b
JB
1136void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1137 unsigned long exp_time)
1138{
1139 struct ieee80211_local *local = sdata->local;
1140 struct sta_info *sta, *tmp;
24723d1b 1141
34e89507 1142 mutex_lock(&local->sta_mtx);
e46a2cf9
MSS
1143
1144 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
b8da6b6a
JB
1145 unsigned long last_active = ieee80211_sta_last_active(sta);
1146
ec2b774e
ML
1147 if (sdata != sta->sdata)
1148 continue;
1149
b8da6b6a 1150 if (time_is_before_jiffies(last_active + exp_time)) {
eea57d42
MSS
1151 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1152 sta->sta.addr);
3f52b7e3
MP
1153
1154 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1155 test_sta_flag(sta, WLAN_STA_PS_STA))
1156 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1157
34e89507 1158 WARN_ON(__sta_info_destroy(sta));
24723d1b 1159 }
e46a2cf9
MSS
1160 }
1161
34e89507 1162 mutex_unlock(&local->sta_mtx);
24723d1b 1163}
17741cdc 1164
686b9cb9 1165struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
7bedd0cf
JB
1166 const u8 *addr,
1167 const u8 *localaddr)
17741cdc 1168{
7bedd0cf 1169 struct ieee80211_local *local = hw_to_local(hw);
83e7e4ce 1170 struct rhlist_head *tmp;
7bedd0cf 1171 struct sta_info *sta;
17741cdc 1172
686b9cb9
BG
1173 /*
1174 * Just return a random station if localaddr is NULL
1175 * ... first in list.
1176 */
83e7e4ce 1177 for_each_sta_info(local, addr, sta, tmp) {
686b9cb9 1178 if (localaddr &&
b203ca39 1179 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
686b9cb9 1180 continue;
f7c65594
JB
1181 if (!sta->uploaded)
1182 return NULL;
abe60632 1183 return &sta->sta;
f7c65594
JB
1184 }
1185
abe60632 1186 return NULL;
17741cdc 1187}
686b9cb9 1188EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
5ed176e1
JB
1189
1190struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1191 const u8 *addr)
1192{
f7c65594 1193 struct sta_info *sta;
5ed176e1
JB
1194
1195 if (!vif)
1196 return NULL;
1197
f7c65594
JB
1198 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1199 if (!sta)
1200 return NULL;
1201
1202 if (!sta->uploaded)
1203 return NULL;
5ed176e1 1204
f7c65594 1205 return &sta->sta;
5ed176e1 1206}
17741cdc 1207EXPORT_SYMBOL(ieee80211_find_sta);
af818581 1208
e3685e03
JB
1209/* powersave support code */
1210void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
50a9432d 1211{
608383bf 1212 struct ieee80211_sub_if_data *sdata = sta->sdata;
e3685e03
JB
1213 struct ieee80211_local *local = sdata->local;
1214 struct sk_buff_head pending;
ba8c3d6f 1215 int filtered = 0, buffered = 0, ac, i;
e3685e03 1216 unsigned long flags;
d012a605
MP
1217 struct ps_data *ps;
1218
3918edb0
FF
1219 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1220 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1221 u.ap);
1222
1223 if (sdata->vif.type == NL80211_IFTYPE_AP)
d012a605 1224 ps = &sdata->bss->ps;
3f52b7e3
MP
1225 else if (ieee80211_vif_is_mesh(&sdata->vif))
1226 ps = &sdata->u.mesh.ps;
d012a605
MP
1227 else
1228 return;
50a9432d 1229
c2c98fde 1230 clear_sta_flag(sta, WLAN_STA_SP);
47086fc5 1231
5a306f58 1232 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
948d887d 1233 sta->driver_buffered_tids = 0;
ba8c3d6f 1234 sta->txq_buffered_tids = 0;
af818581 1235
30686bf7 1236 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
d057e5a3 1237 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
af818581 1238
ba8c3d6f
FF
1239 if (sta->sta.txq[0]) {
1240 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
bb42f2d1 1241 if (!txq_has_queue(sta->sta.txq[i]))
ba8c3d6f
FF
1242 continue;
1243
bb42f2d1 1244 drv_wake_tx_queue(local, to_txq_info(sta->sta.txq[i]));
ba8c3d6f
FF
1245 }
1246 }
1247
948d887d 1248 skb_queue_head_init(&pending);
af818581 1249
1d147bfa
EG
1250 /* sync with ieee80211_tx_h_unicast_ps_buf */
1251 spin_lock(&sta->ps_lock);
af818581 1252 /* Send all buffered frames to the station */
948d887d
JB
1253 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1254 int count = skb_queue_len(&pending), tmp;
1255
987c285c 1256 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
948d887d 1257 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
987c285c 1258 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
948d887d
JB
1259 tmp = skb_queue_len(&pending);
1260 filtered += tmp - count;
1261 count = tmp;
1262
987c285c 1263 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
948d887d 1264 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
987c285c 1265 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
948d887d
JB
1266 tmp = skb_queue_len(&pending);
1267 buffered += tmp - count;
1268 }
1269
e3685e03 1270 ieee80211_add_pending_skbs(local, &pending);
5ac2e350
JB
1271
1272 /* now we're no longer in the deliver code */
1273 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1274
1275 /* The station might have polled and then woken up before we responded,
1276 * so clear these flags now to avoid them sticking around.
1277 */
1278 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1279 clear_sta_flag(sta, WLAN_STA_UAPSD);
1d147bfa 1280 spin_unlock(&sta->ps_lock);
948d887d 1281
e3685e03
JB
1282 atomic_dec(&ps->num_sta_ps);
1283
687da132 1284 /* This station just woke up and isn't aware of our SMPS state */
062f1d6d
CYY
1285 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1286 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
687da132
EG
1287 sdata->smps_mode) &&
1288 sta->known_smps_mode != sdata->bss->req_smps &&
1289 sta_info_tx_streams(sta) != 1) {
1290 ht_dbg(sdata,
1291 "%pM just woke up and MIMO capable - update SMPS\n",
1292 sta->sta.addr);
1293 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1294 sta->sta.addr,
1295 sdata->vif.bss_conf.bssid);
1296 }
1297
af818581
JB
1298 local->total_ps_buffered -= buffered;
1299
c868cb35
JB
1300 sta_info_recalc_tim(sta);
1301
bdcbd8e0 1302 ps_dbg(sdata,
2595d259 1303 "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
bdcbd8e0 1304 sta->sta.addr, sta->sta.aid, filtered, buffered);
17c18bf8
JB
1305
1306 ieee80211_check_fast_xmit(sta);
af818581
JB
1307}
1308
0ead2510 1309static void ieee80211_send_null_response(struct sta_info *sta, int tid,
b77cf4f8 1310 enum ieee80211_frame_release_type reason,
0ead2510 1311 bool call_driver, bool more_data)
af818581 1312{
0ead2510 1313 struct ieee80211_sub_if_data *sdata = sta->sdata;
af818581 1314 struct ieee80211_local *local = sdata->local;
ce662b44 1315 struct ieee80211_qos_hdr *nullfunc;
af818581 1316 struct sk_buff *skb;
ce662b44
JB
1317 int size = sizeof(*nullfunc);
1318 __le16 fc;
a74a8c84 1319 bool qos = sta->sta.wme;
ce662b44 1320 struct ieee80211_tx_info *info;
55de908a 1321 struct ieee80211_chanctx_conf *chanctx_conf;
af818581 1322
ce662b44
JB
1323 if (qos) {
1324 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1325 IEEE80211_STYPE_QOS_NULLFUNC |
1326 IEEE80211_FCTL_FROMDS);
1327 } else {
1328 size -= 2;
1329 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1330 IEEE80211_STYPE_NULLFUNC |
1331 IEEE80211_FCTL_FROMDS);
af818581 1332 }
af818581 1333
ce662b44
JB
1334 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1335 if (!skb)
1336 return;
1337
1338 skb_reserve(skb, local->hw.extra_tx_headroom);
1339
4df864c1 1340 nullfunc = skb_put(skb, size);
ce662b44
JB
1341 nullfunc->frame_control = fc;
1342 nullfunc->duration_id = 0;
1343 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1344 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1345 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
864a6040 1346 nullfunc->seq_ctrl = 0;
ce662b44 1347
59b66255
JB
1348 skb->priority = tid;
1349 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
ce662b44 1350 if (qos) {
ce662b44
JB
1351 nullfunc->qos_ctrl = cpu_to_le16(tid);
1352
0ead2510 1353 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
ce662b44
JB
1354 nullfunc->qos_ctrl |=
1355 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
0ead2510
EG
1356 if (more_data)
1357 nullfunc->frame_control |=
1358 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1359 }
ce662b44
JB
1360 }
1361
1362 info = IEEE80211_SKB_CB(skb);
1363
1364 /*
1365 * Tell TX path to send this frame even though the
1366 * STA may still remain is PS mode after this frame
deeaee19
JB
1367 * exchange. Also set EOSP to indicate this packet
1368 * ends the poll/service period.
ce662b44 1369 */
02f2f1a9 1370 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
deeaee19
JB
1371 IEEE80211_TX_STATUS_EOSP |
1372 IEEE80211_TX_CTL_REQ_TX_STATUS;
ce662b44 1373
6b127c71
SM
1374 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1375
b77cf4f8
JB
1376 if (call_driver)
1377 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1378 reason, false);
40b96408 1379
89afe614
JB
1380 skb->dev = sdata->dev;
1381
55de908a
JB
1382 rcu_read_lock();
1383 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1384 if (WARN_ON(!chanctx_conf)) {
1385 rcu_read_unlock();
1386 kfree_skb(skb);
1387 return;
1388 }
1389
73c4e195 1390 info->band = chanctx_conf->def.chan->band;
7c10770f 1391 ieee80211_xmit(sdata, sta, skb);
55de908a 1392 rcu_read_unlock();
ce662b44
JB
1393}
1394
0a1cb809
JB
1395static int find_highest_prio_tid(unsigned long tids)
1396{
1397 /* lower 3 TIDs aren't ordered perfectly */
1398 if (tids & 0xF8)
1399 return fls(tids) - 1;
1400 /* TID 0 is BE just like TID 3 */
1401 if (tids & BIT(0))
1402 return 0;
1403 return fls(tids) - 1;
1404}
1405
0ead2510
EG
1406/* Indicates if the MORE_DATA bit should be set in the last
1407 * frame obtained by ieee80211_sta_ps_get_frames.
1408 * Note that driver_release_tids is relevant only if
1409 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1410 */
1411static bool
1412ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1413 enum ieee80211_frame_release_type reason,
1414 unsigned long driver_release_tids)
1415{
1416 int ac;
1417
1418 /* If the driver has data on more than one TID then
1419 * certainly there's more data if we release just a
1420 * single frame now (from a single TID). This will
1421 * only happen for PS-Poll.
1422 */
1423 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1424 hweight16(driver_release_tids) > 1)
1425 return true;
1426
1427 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
f438ceb8 1428 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
0ead2510
EG
1429 continue;
1430
1431 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1432 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1433 return true;
1434 }
1435
1436 return false;
1437}
1438
47086fc5 1439static void
0ead2510
EG
1440ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1441 enum ieee80211_frame_release_type reason,
1442 struct sk_buff_head *frames,
1443 unsigned long *driver_release_tids)
af818581
JB
1444{
1445 struct ieee80211_sub_if_data *sdata = sta->sdata;
1446 struct ieee80211_local *local = sdata->local;
948d887d 1447 int ac;
948d887d 1448
f9f760b4 1449 /* Get response frame(s) and more data bit for the last one. */
948d887d 1450 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4049e09a
JB
1451 unsigned long tids;
1452
f438ceb8 1453 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
948d887d
JB
1454 continue;
1455
4049e09a
JB
1456 tids = ieee80211_tids_for_ac(ac);
1457
f9f760b4
JB
1458 /* if we already have frames from software, then we can't also
1459 * release from hardware queues
1460 */
0ead2510
EG
1461 if (skb_queue_empty(frames)) {
1462 *driver_release_tids |=
1463 sta->driver_buffered_tids & tids;
1464 *driver_release_tids |= sta->txq_buffered_tids & tids;
ba8c3d6f 1465 }
948d887d 1466
0ead2510 1467 if (!*driver_release_tids) {
f9f760b4
JB
1468 struct sk_buff *skb;
1469
1470 while (n_frames > 0) {
1471 skb = skb_dequeue(&sta->tx_filtered[ac]);
1472 if (!skb) {
1473 skb = skb_dequeue(
1474 &sta->ps_tx_buf[ac]);
1475 if (skb)
1476 local->total_ps_buffered--;
1477 }
1478 if (!skb)
1479 break;
1480 n_frames--;
0ead2510 1481 __skb_queue_tail(frames, skb);
f9f760b4 1482 }
4049e09a 1483 }
948d887d 1484
0ead2510
EG
1485 /* If we have more frames buffered on this AC, then abort the
1486 * loop since we can't send more data from other ACs before
1487 * the buffered frames from this.
f9f760b4 1488 */
948d887d 1489 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
0ead2510 1490 !skb_queue_empty(&sta->ps_tx_buf[ac]))
948d887d 1491 break;
af818581 1492 }
0ead2510
EG
1493}
1494
1495static void
1496ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1497 int n_frames, u8 ignored_acs,
1498 enum ieee80211_frame_release_type reason)
1499{
1500 struct ieee80211_sub_if_data *sdata = sta->sdata;
1501 struct ieee80211_local *local = sdata->local;
1502 unsigned long driver_release_tids = 0;
1503 struct sk_buff_head frames;
1504 bool more_data;
1505
1506 /* Service or PS-Poll period starts */
1507 set_sta_flag(sta, WLAN_STA_SP);
1508
1509 __skb_queue_head_init(&frames);
1510
1511 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1512 &frames, &driver_release_tids);
1513
1514 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1515
1a57081a 1516 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
0ead2510
EG
1517 driver_release_tids =
1518 BIT(find_highest_prio_tid(driver_release_tids));
af818581 1519
f9f760b4 1520 if (skb_queue_empty(&frames) && !driver_release_tids) {
f438ceb8 1521 int tid, ac;
af818581
JB
1522
1523 /*
ce662b44
JB
1524 * For PS-Poll, this can only happen due to a race condition
1525 * when we set the TIM bit and the station notices it, but
1526 * before it can poll for the frame we expire it.
1527 *
1528 * For uAPSD, this is said in the standard (11.2.1.5 h):
1529 * At each unscheduled SP for a non-AP STA, the AP shall
1530 * attempt to transmit at least one MSDU or MMPDU, but no
1531 * more than the value specified in the Max SP Length field
1532 * in the QoS Capability element from delivery-enabled ACs,
1533 * that are destined for the non-AP STA.
1534 *
1535 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
af818581 1536 */
af818581 1537
ce662b44 1538 /* This will evaluate to 1, 3, 5 or 7. */
f438ceb8 1539 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
d7f84244
EG
1540 if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1541 break;
f438ceb8 1542 tid = 7 - 2 * ac;
af818581 1543
0ead2510 1544 ieee80211_send_null_response(sta, tid, reason, true, false);
f9f760b4 1545 } else if (!driver_release_tids) {
47086fc5
JB
1546 struct sk_buff_head pending;
1547 struct sk_buff *skb;
40b96408
JB
1548 int num = 0;
1549 u16 tids = 0;
b77cf4f8 1550 bool need_null = false;
af818581 1551
47086fc5 1552 skb_queue_head_init(&pending);
af818581 1553
47086fc5
JB
1554 while ((skb = __skb_dequeue(&frames))) {
1555 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1556 struct ieee80211_hdr *hdr = (void *) skb->data;
40b96408
JB
1557 u8 *qoshdr = NULL;
1558
1559 num++;
af818581 1560
47086fc5
JB
1561 /*
1562 * Tell TX path to send this frame even though the
1563 * STA may still remain is PS mode after this frame
1564 * exchange.
1565 */
6b127c71
SM
1566 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1567 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
47086fc5
JB
1568
1569 /*
1570 * Use MoreData flag to indicate whether there are
1571 * more buffered frames for this STA
1572 */
24b9c373 1573 if (more_data || !skb_queue_empty(&frames))
47086fc5
JB
1574 hdr->frame_control |=
1575 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
24b9c373
JD
1576 else
1577 hdr->frame_control &=
1578 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
47086fc5 1579
40b96408
JB
1580 if (ieee80211_is_data_qos(hdr->frame_control) ||
1581 ieee80211_is_qos_nullfunc(hdr->frame_control))
1582 qoshdr = ieee80211_get_qos_ctl(hdr);
1583
b77cf4f8 1584 tids |= BIT(skb->priority);
52a3f20c 1585
b77cf4f8
JB
1586 __skb_queue_tail(&pending, skb);
1587
1588 /* end service period after last frame or add one */
1589 if (!skb_queue_empty(&frames))
1590 continue;
1591
1592 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1593 /* for PS-Poll, there's only one frame */
52a3f20c
MP
1594 info->flags |= IEEE80211_TX_STATUS_EOSP |
1595 IEEE80211_TX_CTL_REQ_TX_STATUS;
b77cf4f8 1596 break;
52a3f20c 1597 }
deeaee19 1598
b77cf4f8
JB
1599 /* For uAPSD, things are a bit more complicated. If the
1600 * last frame has a QoS header (i.e. is a QoS-data or
1601 * QoS-nulldata frame) then just set the EOSP bit there
1602 * and be done.
1603 * If the frame doesn't have a QoS header (which means
1604 * it should be a bufferable MMPDU) then we can't set
1605 * the EOSP bit in the QoS header; add a QoS-nulldata
1606 * frame to the list to send it after the MMPDU.
1607 *
1608 * Note that this code is only in the mac80211-release
1609 * code path, we assume that the driver will not buffer
1610 * anything but QoS-data frames, or if it does, will
1611 * create the QoS-nulldata frame by itself if needed.
1612 *
1613 * Cf. 802.11-2012 10.2.1.10 (c).
1614 */
1615 if (qoshdr) {
1616 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
40b96408 1617
b77cf4f8
JB
1618 info->flags |= IEEE80211_TX_STATUS_EOSP |
1619 IEEE80211_TX_CTL_REQ_TX_STATUS;
1620 } else {
1621 /* The standard isn't completely clear on this
1622 * as it says the more-data bit should be set
1623 * if there are more BUs. The QoS-Null frame
1624 * we're about to send isn't buffered yet, we
1625 * only create it below, but let's pretend it
1626 * was buffered just in case some clients only
1627 * expect more-data=0 when eosp=1.
1628 */
1629 hdr->frame_control |=
1630 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1631 need_null = true;
1632 num++;
1633 }
1634 break;
47086fc5 1635 }
af818581 1636
40b96408
JB
1637 drv_allow_buffered_frames(local, sta, tids, num,
1638 reason, more_data);
1639
47086fc5 1640 ieee80211_add_pending_skbs(local, &pending);
af818581 1641
b77cf4f8
JB
1642 if (need_null)
1643 ieee80211_send_null_response(
0ead2510
EG
1644 sta, find_highest_prio_tid(tids),
1645 reason, false, false);
b77cf4f8 1646
c868cb35 1647 sta_info_recalc_tim(sta);
af818581 1648 } else {
ba8c3d6f
FF
1649 int tid;
1650
af818581 1651 /*
4049e09a
JB
1652 * We need to release a frame that is buffered somewhere in the
1653 * driver ... it'll have to handle that.
f9f760b4
JB
1654 * Note that the driver also has to check the number of frames
1655 * on the TIDs we're releasing from - if there are more than
1656 * n_frames it has to set the more-data bit (if we didn't ask
1657 * it to set it anyway due to other buffered frames); if there
1658 * are fewer than n_frames it has to make sure to adjust that
1659 * to allow the service period to end properly.
4049e09a
JB
1660 */
1661 drv_release_buffered_frames(local, sta, driver_release_tids,
47086fc5 1662 n_frames, reason, more_data);
4049e09a
JB
1663
1664 /*
1665 * Note that we don't recalculate the TIM bit here as it would
1666 * most likely have no effect at all unless the driver told us
f9f760b4 1667 * that the TID(s) became empty before returning here from the
4049e09a 1668 * release function.
f9f760b4 1669 * Either way, however, when the driver tells us that the TID(s)
ba8c3d6f
FF
1670 * became empty or we find that a txq became empty, we'll do the
1671 * TIM recalculation.
af818581 1672 */
ba8c3d6f
FF
1673
1674 if (!sta->sta.txq[0])
1675 return;
1676
1677 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
83843c80 1678 if (!(driver_release_tids & BIT(tid)) ||
1e1430d5 1679 txq_has_queue(sta->sta.txq[tid]))
ba8c3d6f
FF
1680 continue;
1681
1682 sta_info_recalc_tim(sta);
1683 break;
1684 }
af818581
JB
1685 }
1686}
1687
47086fc5
JB
1688void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1689{
1690 u8 ignore_for_response = sta->sta.uapsd_queues;
1691
1692 /*
1693 * If all ACs are delivery-enabled then we should reply
1694 * from any of them, if only some are enabled we reply
1695 * only from the non-enabled ones.
1696 */
1697 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1698 ignore_for_response = 0;
1699
1700 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1701 IEEE80211_FRAME_RELEASE_PSPOLL);
1702}
1703
1704void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1705{
1706 int n_frames = sta->sta.max_sp;
1707 u8 delivery_enabled = sta->sta.uapsd_queues;
1708
1709 /*
1710 * If we ever grow support for TSPEC this might happen if
1711 * the TSPEC update from hostapd comes in between a trigger
1712 * frame setting WLAN_STA_UAPSD in the RX path and this
1713 * actually getting called.
1714 */
1715 if (!delivery_enabled)
1716 return;
1717
47086fc5
JB
1718 switch (sta->sta.max_sp) {
1719 case 1:
1720 n_frames = 2;
1721 break;
1722 case 2:
1723 n_frames = 4;
1724 break;
1725 case 3:
1726 n_frames = 6;
1727 break;
1728 case 0:
1729 /* XXX: what is a good value? */
13a8098a 1730 n_frames = 128;
47086fc5
JB
1731 break;
1732 }
1733
1734 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1735 IEEE80211_FRAME_RELEASE_UAPSD);
1736}
1737
af818581
JB
1738void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1739 struct ieee80211_sta *pubsta, bool block)
1740{
1741 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1742
b5878a2d
JB
1743 trace_api_sta_block_awake(sta->local, pubsta, block);
1744
5ac2e350 1745 if (block) {
c2c98fde 1746 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
17c18bf8 1747 ieee80211_clear_fast_xmit(sta);
5ac2e350
JB
1748 return;
1749 }
1750
1751 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1752 return;
1753
1754 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1755 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1756 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1757 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1758 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1759 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1760 /* must be asleep in this case */
1761 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1762 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1763 } else {
1764 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
17c18bf8 1765 ieee80211_check_fast_xmit(sta);
5ac2e350 1766 }
af818581
JB
1767}
1768EXPORT_SYMBOL(ieee80211_sta_block_awake);
dcf55fb5 1769
e943789e 1770void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
37fbd908
JB
1771{
1772 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1773 struct ieee80211_local *local = sta->local;
37fbd908
JB
1774
1775 trace_api_eosp(local, pubsta);
1776
e943789e 1777 clear_sta_flag(sta, WLAN_STA_SP);
37fbd908 1778}
e943789e 1779EXPORT_SYMBOL(ieee80211_sta_eosp);
37fbd908 1780
0ead2510
EG
1781void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1782{
1783 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1784 enum ieee80211_frame_release_type reason;
1785 bool more_data;
1786
1787 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1788
1789 reason = IEEE80211_FRAME_RELEASE_UAPSD;
1790 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1791 reason, 0);
1792
1793 ieee80211_send_null_response(sta, tid, reason, false, more_data);
1794}
1795EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1796
042ec453
JB
1797void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1798 u8 tid, bool buffered)
dcf55fb5
FF
1799{
1800 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1801
5a306f58 1802 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
042ec453
JB
1803 return;
1804
1b000789
JB
1805 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1806
948d887d
JB
1807 if (buffered)
1808 set_bit(tid, &sta->driver_buffered_tids);
1809 else
1810 clear_bit(tid, &sta->driver_buffered_tids);
1811
c868cb35 1812 sta_info_recalc_tim(sta);
dcf55fb5 1813}
042ec453 1814EXPORT_SYMBOL(ieee80211_sta_set_buffered);
d9a7ddb0 1815
83d5cc01
JB
1816int sta_info_move_state(struct sta_info *sta,
1817 enum ieee80211_sta_state new_state)
d9a7ddb0 1818{
8bf11d8d 1819 might_sleep();
d9a7ddb0
JB
1820
1821 if (sta->sta_state == new_state)
1822 return 0;
1823
f09603a2
JB
1824 /* check allowed transitions first */
1825
1826 switch (new_state) {
1827 case IEEE80211_STA_NONE:
1828 if (sta->sta_state != IEEE80211_STA_AUTH)
1829 return -EINVAL;
1830 break;
1831 case IEEE80211_STA_AUTH:
1832 if (sta->sta_state != IEEE80211_STA_NONE &&
1833 sta->sta_state != IEEE80211_STA_ASSOC)
1834 return -EINVAL;
1835 break;
1836 case IEEE80211_STA_ASSOC:
1837 if (sta->sta_state != IEEE80211_STA_AUTH &&
1838 sta->sta_state != IEEE80211_STA_AUTHORIZED)
1839 return -EINVAL;
1840 break;
1841 case IEEE80211_STA_AUTHORIZED:
1842 if (sta->sta_state != IEEE80211_STA_ASSOC)
1843 return -EINVAL;
1844 break;
1845 default:
1846 WARN(1, "invalid state %d", new_state);
1847 return -EINVAL;
1848 }
1849
bdcbd8e0
JB
1850 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1851 sta->sta.addr, new_state);
f09603a2
JB
1852
1853 /*
1854 * notify the driver before the actual changes so it can
1855 * fail the transition
1856 */
1857 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1858 int err = drv_sta_state(sta->local, sta->sdata, sta,
1859 sta->sta_state, new_state);
1860 if (err)
1861 return err;
1862 }
1863
1864 /* reflect the change in all state variables */
1865
d9a7ddb0
JB
1866 switch (new_state) {
1867 case IEEE80211_STA_NONE:
1868 if (sta->sta_state == IEEE80211_STA_AUTH)
1869 clear_bit(WLAN_STA_AUTH, &sta->_flags);
d9a7ddb0
JB
1870 break;
1871 case IEEE80211_STA_AUTH:
a7201a6c 1872 if (sta->sta_state == IEEE80211_STA_NONE) {
d9a7ddb0 1873 set_bit(WLAN_STA_AUTH, &sta->_flags);
a7201a6c 1874 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
d9a7ddb0 1875 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
a7201a6c 1876 ieee80211_recalc_min_chandef(sta->sdata);
52cfa1d6
AB
1877 if (!sta->sta.support_p2p_ps)
1878 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
a7201a6c 1879 }
d9a7ddb0
JB
1880 break;
1881 case IEEE80211_STA_ASSOC:
29623892 1882 if (sta->sta_state == IEEE80211_STA_AUTH) {
d9a7ddb0 1883 set_bit(WLAN_STA_ASSOC, &sta->_flags);
a7201a6c 1884 ieee80211_recalc_min_chandef(sta->sdata);
52cfa1d6
AB
1885 if (!sta->sta.support_p2p_ps)
1886 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
29623892 1887 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
72f15d53 1888 ieee80211_vif_dec_num_mcast(sta->sdata);
d9a7ddb0 1889 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
17c18bf8 1890 ieee80211_clear_fast_xmit(sta);
49ddf8e6 1891 ieee80211_clear_fast_rx(sta);
f09603a2 1892 }
d9a7ddb0
JB
1893 break;
1894 case IEEE80211_STA_AUTHORIZED:
29623892 1895 if (sta->sta_state == IEEE80211_STA_ASSOC) {
72f15d53 1896 ieee80211_vif_inc_num_mcast(sta->sdata);
d9a7ddb0 1897 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
17c18bf8 1898 ieee80211_check_fast_xmit(sta);
49ddf8e6 1899 ieee80211_check_fast_rx(sta);
f09603a2 1900 }
d9a7ddb0
JB
1901 break;
1902 default:
f09603a2 1903 break;
d9a7ddb0
JB
1904 }
1905
d9a7ddb0
JB
1906 sta->sta_state = new_state;
1907
1908 return 0;
1909}
687da132
EG
1910
1911u8 sta_info_tx_streams(struct sta_info *sta)
1912{
1913 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1914 u8 rx_streams;
1915
1916 if (!sta->sta.ht_cap.ht_supported)
1917 return 1;
1918
1919 if (sta->sta.vht_cap.vht_supported) {
1920 int i;
1921 u16 tx_mcs_map =
1922 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1923
1924 for (i = 7; i >= 0; i--)
1925 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1926 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1927 return i + 1;
1928 }
1929
1930 if (ht_cap->mcs.rx_mask[3])
1931 rx_streams = 4;
1932 else if (ht_cap->mcs.rx_mask[2])
1933 rx_streams = 3;
1934 else if (ht_cap->mcs.rx_mask[1])
1935 rx_streams = 2;
1936 else
1937 rx_streams = 1;
1938
1939 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1940 return rx_streams;
1941
1942 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1943 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1944}
b7ffbd7e 1945
c9c5962b
JB
1946static struct ieee80211_sta_rx_stats *
1947sta_get_last_rx_stats(struct sta_info *sta)
1948{
1949 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
1950 struct ieee80211_local *local = sta->local;
1951 int cpu;
1952
1953 if (!ieee80211_hw_check(&local->hw, USES_RSS))
1954 return stats;
1955
1956 for_each_possible_cpu(cpu) {
1957 struct ieee80211_sta_rx_stats *cpustats;
1958
1959 cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
1960
1961 if (time_after(cpustats->last_rx, stats->last_rx))
1962 stats = cpustats;
1963 }
1964
1965 return stats;
1966}
1967
4f6b1b3d
JB
1968static void sta_stats_decode_rate(struct ieee80211_local *local, u16 rate,
1969 struct rate_info *rinfo)
fbd6ff5c 1970{
dcba665b 1971 rinfo->bw = STA_STATS_GET(BW, rate);
4f6b1b3d 1972
dcba665b 1973 switch (STA_STATS_GET(TYPE, rate)) {
7f406cd1 1974 case STA_STATS_RATE_TYPE_VHT:
4f6b1b3d 1975 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
dcba665b
JB
1976 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
1977 rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
1978 if (STA_STATS_GET(SGI, rate))
1979 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
7f406cd1
JB
1980 break;
1981 case STA_STATS_RATE_TYPE_HT:
4f6b1b3d 1982 rinfo->flags = RATE_INFO_FLAGS_MCS;
dcba665b
JB
1983 rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
1984 if (STA_STATS_GET(SGI, rate))
1985 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
7f406cd1
JB
1986 break;
1987 case STA_STATS_RATE_TYPE_LEGACY: {
fbd6ff5c 1988 struct ieee80211_supported_band *sband;
fbd6ff5c 1989 u16 brate;
4f6b1b3d 1990 unsigned int shift;
dcba665b
JB
1991 int band = STA_STATS_GET(LEGACY_BAND, rate);
1992 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
4f6b1b3d 1993
a17d93ff 1994 rinfo->flags = 0;
dcba665b
JB
1995 sband = local->hw.wiphy->bands[band];
1996 brate = sband->bitrates[rate_idx].bitrate;
4f6b1b3d
JB
1997 if (rinfo->bw == RATE_INFO_BW_5)
1998 shift = 2;
1999 else if (rinfo->bw == RATE_INFO_BW_10)
2000 shift = 1;
2001 else
2002 shift = 0;
fbd6ff5c 2003 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
7f406cd1
JB
2004 break;
2005 }
fbd6ff5c 2006 }
4f6b1b3d
JB
2007}
2008
a17d93ff 2009static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
4f6b1b3d 2010{
6aa7de05 2011 u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
fbd6ff5c 2012
4f6b1b3d 2013 if (rate == STA_STATS_RATE_INVALID)
a17d93ff
BG
2014 return -EINVAL;
2015
2016 sta_stats_decode_rate(sta->local, rate, rinfo);
2017 return 0;
fbd6ff5c
JB
2018}
2019
0f9c5a61
JB
2020static void sta_set_tidstats(struct sta_info *sta,
2021 struct cfg80211_tid_stats *tidstats,
2022 int tid)
2023{
2024 struct ieee80211_local *local = sta->local;
2025
2026 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2027 unsigned int start;
2028
2029 do {
2030 start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
2031 tidstats->rx_msdu = sta->rx_stats.msdu[tid];
2032 } while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
2033
2034 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2035 }
2036
2037 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2038 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2039 tidstats->tx_msdu = sta->tx_stats.msdu[tid];
2040 }
2041
2042 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2043 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2044 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2045 tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
2046 }
2047
2048 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2049 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2050 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2051 tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
2052 }
2053}
2054
c9c5962b
JB
2055static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2056{
2057 unsigned int start;
2058 u64 value;
2059
2060 do {
2061 start = u64_stats_fetch_begin(&rxstats->syncp);
2062 value = rxstats->bytes;
2063 } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2064
2065 return value;
2066}
2067
b7ffbd7e
JB
2068void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
2069{
2070 struct ieee80211_sub_if_data *sdata = sta->sdata;
2071 struct ieee80211_local *local = sdata->local;
b7ffbd7e 2072 u32 thr = 0;
c9c5962b
JB
2073 int i, ac, cpu;
2074 struct ieee80211_sta_rx_stats *last_rxstats;
2075
2076 last_rxstats = sta_get_last_rx_stats(sta);
b7ffbd7e
JB
2077
2078 sinfo->generation = sdata->local->sta_generation;
2079
225b8189
JB
2080 /* do before driver, so beacon filtering drivers have a
2081 * chance to e.g. just add the number of filtered beacons
2082 * (or just modify the value entirely, of course)
2083 */
2084 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2085 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2086
2b9a7e1b
JB
2087 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2088
319090bf
JB
2089 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
2090 BIT(NL80211_STA_INFO_STA_FLAGS) |
2091 BIT(NL80211_STA_INFO_BSS_PARAM) |
2092 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
976bd9ef
JB
2093 BIT(NL80211_STA_INFO_RX_DROP_MISC);
2094
2095 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2096 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2097 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
2098 }
b7ffbd7e 2099
84b00607 2100 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
e5a9f8d0 2101 sinfo->inactive_time =
b8da6b6a 2102 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2b9a7e1b 2103
319090bf
JB
2104 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
2105 BIT(NL80211_STA_INFO_TX_BYTES)))) {
2b9a7e1b
JB
2106 sinfo->tx_bytes = 0;
2107 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
e5a9f8d0 2108 sinfo->tx_bytes += sta->tx_stats.bytes[ac];
319090bf 2109 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
2b9a7e1b
JB
2110 }
2111
319090bf 2112 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
2b9a7e1b
JB
2113 sinfo->tx_packets = 0;
2114 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
e5a9f8d0 2115 sinfo->tx_packets += sta->tx_stats.packets[ac];
319090bf 2116 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
2b9a7e1b
JB
2117 }
2118
319090bf
JB
2119 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
2120 BIT(NL80211_STA_INFO_RX_BYTES)))) {
c9c5962b
JB
2121 sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
2122
2123 if (sta->pcpu_rx_stats) {
2124 for_each_possible_cpu(cpu) {
2125 struct ieee80211_sta_rx_stats *cpurxs;
2126
2127 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2128 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2129 }
2130 }
0f9c5a61 2131
319090bf 2132 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
2b9a7e1b
JB
2133 }
2134
319090bf 2135 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
e5a9f8d0 2136 sinfo->rx_packets = sta->rx_stats.packets;
c9c5962b
JB
2137 if (sta->pcpu_rx_stats) {
2138 for_each_possible_cpu(cpu) {
2139 struct ieee80211_sta_rx_stats *cpurxs;
2140
2141 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2142 sinfo->rx_packets += cpurxs->packets;
2143 }
2144 }
319090bf 2145 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
2b9a7e1b
JB
2146 }
2147
319090bf 2148 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
e5a9f8d0 2149 sinfo->tx_retries = sta->status_stats.retry_count;
319090bf 2150 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
2b9a7e1b
JB
2151 }
2152
319090bf 2153 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
e5a9f8d0 2154 sinfo->tx_failed = sta->status_stats.retry_failed;
319090bf 2155 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
b7ffbd7e 2156 }
2b9a7e1b 2157
e5a9f8d0 2158 sinfo->rx_dropped_misc = sta->rx_stats.dropped;
c9c5962b
JB
2159 if (sta->pcpu_rx_stats) {
2160 for_each_possible_cpu(cpu) {
2161 struct ieee80211_sta_rx_stats *cpurxs;
2162
2163 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
e165bc02 2164 sinfo->rx_dropped_misc += cpurxs->dropped;
c9c5962b
JB
2165 }
2166 }
b7ffbd7e 2167
225b8189
JB
2168 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2169 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2170 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
2171 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2172 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2173 }
2174
30686bf7
JB
2175 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2176 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
319090bf 2177 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
c9c5962b 2178 sinfo->signal = (s8)last_rxstats->last_signal;
319090bf 2179 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
2b9a7e1b
JB
2180 }
2181
c9c5962b
JB
2182 if (!sta->pcpu_rx_stats &&
2183 !(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
40d9a38a 2184 sinfo->signal_avg =
0be6ed13 2185 -ewma_signal_read(&sta->rx_stats_avg.signal);
319090bf 2186 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
2b9a7e1b 2187 }
b7ffbd7e 2188 }
2b9a7e1b 2189
c9c5962b
JB
2190 /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2191 * the sta->rx_stats struct, so the check here is fine with and without
2192 * pcpu statistics
2193 */
2194 if (last_rxstats->chains &&
319090bf
JB
2195 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2196 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
c9c5962b
JB
2197 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL);
2198 if (!sta->pcpu_rx_stats)
2199 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2200
2201 sinfo->chains = last_rxstats->chains;
b7ffbd7e 2202
b7ffbd7e 2203 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
e5a9f8d0 2204 sinfo->chain_signal[i] =
c9c5962b 2205 last_rxstats->chain_signal_last[i];
b7ffbd7e 2206 sinfo->chain_signal_avg[i] =
0be6ed13 2207 -ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
b7ffbd7e
JB
2208 }
2209 }
2210
319090bf 2211 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
e5a9f8d0
JB
2212 sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2213 &sinfo->txrate);
319090bf 2214 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
2b9a7e1b
JB
2215 }
2216
319090bf 2217 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
a17d93ff
BG
2218 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2219 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
2b9a7e1b 2220 }
b7ffbd7e 2221
79c892b8
JB
2222 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
2223 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
2224 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
2225
0f9c5a61 2226 sta_set_tidstats(sta, tidstats, i);
79c892b8
JB
2227 }
2228
b7ffbd7e
JB
2229 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2230#ifdef CONFIG_MAC80211_MESH
319090bf
JB
2231 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2232 BIT(NL80211_STA_INFO_PLID) |
2233 BIT(NL80211_STA_INFO_PLINK_STATE) |
2234 BIT(NL80211_STA_INFO_LOCAL_PM) |
2235 BIT(NL80211_STA_INFO_PEER_PM) |
2236 BIT(NL80211_STA_INFO_NONPEER_PM);
b7ffbd7e 2237
433f5bc1
JB
2238 sinfo->llid = sta->mesh->llid;
2239 sinfo->plid = sta->mesh->plid;
2240 sinfo->plink_state = sta->mesh->plink_state;
b7ffbd7e 2241 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
319090bf 2242 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
433f5bc1 2243 sinfo->t_offset = sta->mesh->t_offset;
b7ffbd7e 2244 }
433f5bc1
JB
2245 sinfo->local_pm = sta->mesh->local_pm;
2246 sinfo->peer_pm = sta->mesh->peer_pm;
2247 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
b7ffbd7e
JB
2248#endif
2249 }
2250
2251 sinfo->bss_param.flags = 0;
2252 if (sdata->vif.bss_conf.use_cts_prot)
2253 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2254 if (sdata->vif.bss_conf.use_short_preamble)
2255 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2256 if (sdata->vif.bss_conf.use_short_slot)
2257 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
785e21a8 2258 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
b7ffbd7e
JB
2259 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2260
2261 sinfo->sta_flags.set = 0;
2262 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2263 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2264 BIT(NL80211_STA_FLAG_WME) |
2265 BIT(NL80211_STA_FLAG_MFP) |
2266 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2267 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2268 BIT(NL80211_STA_FLAG_TDLS_PEER);
2269 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2270 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2271 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2272 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
a74a8c84 2273 if (sta->sta.wme)
b7ffbd7e
JB
2274 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2275 if (test_sta_flag(sta, WLAN_STA_MFP))
2276 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2277 if (test_sta_flag(sta, WLAN_STA_AUTH))
2278 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2279 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2280 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2281 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2282 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2283
3b17fbf8
MA
2284 thr = sta_get_expected_throughput(sta);
2285
2286 if (thr != 0) {
2287 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2288 sinfo->expected_throughput = thr;
2289 }
2290}
2291
2292u32 sta_get_expected_throughput(struct sta_info *sta)
2293{
2294 struct ieee80211_sub_if_data *sdata = sta->sdata;
2295 struct ieee80211_local *local = sdata->local;
2296 struct rate_control_ref *ref = NULL;
2297 u32 thr = 0;
2298
2299 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2300 ref = local->rate_ctrl;
2301
b7ffbd7e
JB
2302 /* check if the driver has a SW RC implementation */
2303 if (ref && ref->ops->get_expected_throughput)
2304 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2305 else
4fdbc67a 2306 thr = drv_get_expected_throughput(local, sta);
b7ffbd7e 2307
3b17fbf8 2308 return thr;
b7ffbd7e 2309}
b8da6b6a
JB
2310
2311unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2312{
c9c5962b
JB
2313 struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2314
2315 if (time_after(stats->last_rx, sta->status_stats.last_ack))
2316 return stats->last_rx;
b8da6b6a
JB
2317 return sta->status_stats.last_ack;
2318}
484a54c2
THJ
2319
2320static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2321{
2322 if (!sta->sdata->local->ops->wake_tx_queue)
2323 return;
2324
2325 if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2326 sta->cparams.target = MS2TIME(50);
2327 sta->cparams.interval = MS2TIME(300);
2328 sta->cparams.ecn = false;
2329 } else {
2330 sta->cparams.target = MS2TIME(20);
2331 sta->cparams.interval = MS2TIME(100);
2332 sta->cparams.ecn = true;
2333 }
2334}
2335
2336void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2337 u32 thr)
2338{
2339 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2340
2341 sta_update_codel_params(sta, thr);
2342}