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