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