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