blob: 666ddac3c87c67a63ed685efe83221cf7d52428d [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
Johannes Bergd98ad832014-09-03 15:24:57 +03004 * Copyright 2013-2014 Intel Mobile Communications GmbH
Jiri Bencf0706e82007-05-05 11:45:53 -07005 *
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>
Felix Fietkau888d04d2012-03-01 15:22:09 +010013#include <linux/etherdevice.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070014#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>
Johannes Berg0d174402007-12-17 15:07:43 +010019#include <linux/timer.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010020#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070021
22#include <net/mac80211.h>
23#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020024#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040025#include "rate.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070026#include "sta_info.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070027#include "debugfs_sta.h"
Luis Carlos Coboee385852008-02-23 15:17:11 +010028#include "mesh.h"
Johannes Bergce662b442011-09-29 16:04:34 +020029#include "wme.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070030
Johannes Bergd0709a62008-02-25 16:27:46 +010031/**
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 *
Johannes Berg34e89502010-02-03 13:59:58 +010038 * 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.
Johannes Berg93e5deb2008-04-01 15:21:00 +020046 *
47 * When the insertion fails (sta_info_insert()) returns non-zero), the
48 * structure will have been freed by sta_info_insert()!
Johannes Bergd0709a62008-02-25 16:27:46 +010049 *
Johannes Berg34e89502010-02-03 13:59:58 +010050 * Station entries are added by mac80211 when you establish a link with a
Luis R. Rodriguez7e189a12009-06-02 18:38:14 -040051 * 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
Lucas De Marchi25985ed2011-03-30 22:57:33 -030053 * receive an association response from the AP. For IBSS this occurs when
Johannes Berg34e89502010-02-03 13:59:58 +010054 * get to know about a peer on the same IBSS. For WDS we add the sta for
Lucas De Marchi25985ed2011-03-30 22:57:33 -030055 * the peer immediately upon device open. When using AP mode we add stations
Johannes Berg34e89502010-02-03 13:59:58 +010056 * for each respective station upon request from userspace through nl80211.
Luis R. Rodriguez7e189a12009-06-02 18:38:14 -040057 *
Johannes Berg34e89502010-02-03 13:59:58 +010058 * In order to remove a STA info structure, various sta_info_destroy_*()
59 * calls are available.
Johannes Bergd0709a62008-02-25 16:27:46 +010060 *
Johannes Berg34e89502010-02-03 13:59:58 +010061 * 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.
Johannes Bergd0709a62008-02-25 16:27:46 +010065 */
Jiri Bencf0706e82007-05-05 11:45:53 -070066
Johannes Berg7bedd0c2015-02-13 21:55:15 +010067static const struct rhashtable_params sta_rht_params = {
68 .nelem_hint = 3, /* start small */
Johannes Bergcaf22d32015-04-24 11:10:10 +020069 .automatic_shrinking = true,
Johannes Berg7bedd0c2015-02-13 21:55:15 +010070 .head_offset = offsetof(struct sta_info, hash_node),
71 .key_offset = offsetof(struct sta_info, sta.addr),
72 .key_len = ETH_ALEN,
73 .hashfn = sta_addr_hash,
Johannes Bergebd82b32015-04-23 17:26:06 +020074 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
Johannes Berg7bedd0c2015-02-13 21:55:15 +010075};
76
Johannes Berg4d339602011-12-15 11:24:20 +010077/* Caller must hold local->sta_mtx */
Michael Wube8755e2007-07-27 15:43:23 +020078static int sta_info_hash_del(struct ieee80211_local *local,
79 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070080{
Johannes Berg7bedd0c2015-02-13 21:55:15 +010081 return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
82 sta_rht_params);
Jiri Bencf0706e82007-05-05 11:45:53 -070083}
84
Johannes Berg5108ca82014-02-17 20:49:03 +010085static void __cleanup_single_sta(struct sta_info *sta)
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030086{
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030087 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;
Marco Porschd012a602012-10-10 12:39:50 -070091 struct ps_data *ps;
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030092
Johannes Berge3685e02014-02-20 11:19:58 +010093 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
Johannes Berg5ac2e352014-05-27 16:32:27 +020094 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
95 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
Marco Porschd012a602012-10-10 12:39:50 -070096 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
97 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
98 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +010099 else if (ieee80211_vif_is_mesh(&sdata->vif))
100 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700101 else
102 return;
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300103
104 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Berge3685e02014-02-20 11:19:58 +0100105 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200106 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300107
Marco Porschd012a602012-10-10 12:39:50 -0700108 atomic_dec(&ps->num_sta_ps);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300109 }
110
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100111 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
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300121 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
122 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
Felix Fietkau1f98ab72012-11-10 03:44:14 +0100123 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
124 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300125 }
126
Thomas Pedersen45b50282013-02-06 10:17:21 -0800127 if (ieee80211_vif_is_mesh(&sdata->vif))
128 mesh_sta_cleanup(sta);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300129
Johannes Berg5ac2e352014-05-27 16:32:27 +0200130 cancel_work_sync(&sta->drv_deliver_wk);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300131
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 */
Johannes Berg5a306f52012-11-14 23:22:21 +0100138 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berg661eb382013-06-12 22:47:56 +0200139 kfree(sta->ampdu_mlme.tid_start_tx[i]);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300140 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
141 if (!tid_tx)
142 continue;
Felix Fietkau1f98ab72012-11-10 03:44:14 +0100143 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300144 kfree(tid_tx);
145 }
Johannes Berg5108ca82014-02-17 20:49:03 +0100146}
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300147
Johannes Berg5108ca82014-02-17 20:49:03 +0100148static 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);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300154 sta_info_free(local, sta);
155}
156
Johannes Bergd0709a62008-02-25 16:27:46 +0100157/* protected by RCU */
Johannes Bergabe60632009-11-25 17:46:18 +0100158struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
159 const u8 *addr)
Johannes Berg43ba7e92008-02-21 14:09:30 +0100160{
Johannes Bergabe60632009-11-25 17:46:18 +0100161 struct ieee80211_local *local = sdata->local;
Johannes Berg60f4b622015-04-23 14:02:30 +0200162 struct sta_info *sta;
163 struct rhash_head *tmp;
164 const struct bucket_table *tbl;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100165
Johannes Berg60f4b622015-04-23 14:02:30 +0200166 rcu_read_lock();
167 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
168
169 for_each_sta_info(local, tbl, addr, sta, tmp) {
170 if (sta->sdata == sdata) {
171 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;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100180}
181
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100182/*
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;
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100191 struct rhash_head *tmp;
192 const struct bucket_table *tbl;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100193
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100194 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 }
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100206 }
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100207 rcu_read_unlock();
208 return NULL;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100209}
210
Johannes Berg3b53fde82009-11-16 12:00:37 +0100211struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
212 int idx)
Luis Carlos Coboee385852008-02-23 15:17:11 +0100213{
Johannes Berg3b53fde82009-11-16 12:00:37 +0100214 struct ieee80211_local *local = sdata->local;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100215 struct sta_info *sta;
216 int i = 0;
217
Johannes Bergd0709a62008-02-25 16:27:46 +0100218 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg3b53fde82009-11-16 12:00:37 +0100219 if (sdata != sta->sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800220 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100221 if (i < idx) {
222 ++i;
223 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100224 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800225 return sta;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100226 }
Luis Carlos Coboee385852008-02-23 15:17:11 +0100227
228 return NULL;
229}
Jiri Bencf0706e82007-05-05 11:45:53 -0700230
Johannes Berg93e5deb2008-04-01 15:21:00 +0200231/**
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100232 * sta_info_free - free STA
Johannes Berg93e5deb2008-04-01 15:21:00 +0200233 *
Randy Dunlap6ef307b2008-07-03 13:52:18 -0700234 * @local: pointer to the global information
Johannes Berg93e5deb2008-04-01 15:21:00 +0200235 * @sta: STA info to free
236 *
237 * This function must undo everything done by sta_info_alloc()
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100238 * 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.)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200241 */
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100242void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200243{
Johannes Berg889cbb92012-01-17 10:33:29 +0100244 if (sta->rate_ctrl)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100245 rate_control_free_sta(sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200246
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200247 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200248
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100249 if (sta->sta.txq[0])
250 kfree(to_txq_info(sta->sta.txq[0]));
Felix Fietkau53d04522014-05-27 22:33:57 +0200251 kfree(rcu_dereference_raw(sta->sta.rates));
Johannes Berg93e5deb2008-04-01 15:21:00 +0200252 kfree(sta);
253}
254
Johannes Berg4d339602011-12-15 11:24:20 +0100255/* Caller must hold local->sta_mtx */
Johannes Bergd0709a62008-02-25 16:27:46 +0100256static void sta_info_hash_add(struct ieee80211_local *local,
257 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700258{
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100259 rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
260 sta_rht_params);
Jiri Bencf0706e82007-05-05 11:45:53 -0700261}
Jiri Bencf0706e82007-05-05 11:45:53 -0700262
Johannes Berg5ac2e352014-05-27 16:32:27 +0200263static void sta_deliver_ps_frames(struct work_struct *wk)
Johannes Bergaf818582009-11-06 11:35:50 +0100264{
265 struct sta_info *sta;
266
Johannes Berg5ac2e352014-05-27 16:32:27 +0200267 sta = container_of(wk, struct sta_info, drv_deliver_wk);
Johannes Bergaf818582009-11-06 11:35:50 +0100268
269 if (sta->dead)
270 return;
271
Johannes Berg5ac2e352014-05-27 16:32:27 +0200272 local_bh_disable();
273 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
Johannes Bergaf818582009-11-06 11:35:50 +0100274 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200275 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
Johannes Bergaf818582009-11-06 11:35:50 +0100276 ieee80211_sta_ps_deliver_poll_response(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200277 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
Johannes Berg47086fc2011-09-29 16:04:33 +0200278 ieee80211_sta_ps_deliver_uapsd(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200279 local_bh_enable();
Johannes Bergaf818582009-11-06 11:35:50 +0100280}
281
Johannes Bergaf65cd962009-11-17 18:18:36 +0100282static int sta_prepare_rate_control(struct ieee80211_local *local,
283 struct sta_info *sta, gfp_t gfp)
284{
Johannes Berg30686bf2015-06-02 21:39:54 +0200285 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
Johannes Bergaf65cd962009-11-17 18:18:36 +0100286 return 0;
287
Johannes Berg889cbb92012-01-17 10:33:29 +0100288 sta->rate_ctrl = local->rate_ctrl;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100289 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
Johannes Berg35c347a2015-03-05 16:10:08 +0100290 sta, gfp);
Johannes Berg889cbb92012-01-17 10:33:29 +0100291 if (!sta->rate_ctrl_priv)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100292 return -ENOMEM;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100293
294 return 0;
295}
296
Johannes Berg73651ee2008-02-25 16:27:47 +0100297struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
Johannes Berg56544162011-12-14 13:28:46 +0100298 const u8 *addr, gfp_t gfp)
Jiri Bencf0706e82007-05-05 11:45:53 -0700299{
Johannes Bergd0709a62008-02-25 16:27:46 +0100300 struct ieee80211_local *local = sdata->local;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100301 struct ieee80211_hw *hw = &local->hw;
Jiri Bencf0706e82007-05-05 11:45:53 -0700302 struct sta_info *sta;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +0530303 struct timespec uptime;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200304 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700305
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100306 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
Jiri Bencf0706e82007-05-05 11:45:53 -0700307 if (!sta)
Johannes Berg73651ee2008-02-25 16:27:47 +0100308 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700309
Johannes Berg07346f812008-05-03 01:02:02 +0200310 spin_lock_init(&sta->lock);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200311 spin_lock_init(&sta->ps_lock);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200312 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
Johannes Berg67c282c2010-06-10 10:21:43 +0200313 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
Johannes Berga93e3642010-06-10 10:21:46 +0200314 mutex_init(&sta->ampdu_mlme.mtx);
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800315#ifdef CONFIG_MAC80211_MESH
Bob Copeland48bf6be2015-04-13 17:26:28 -0400316 spin_lock_init(&sta->plink_lock);
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800317 if (ieee80211_vif_is_mesh(&sdata->vif) &&
318 !sdata->u.mesh.user_mpm)
319 init_timer(&sta->plink_timer);
Thomas Pedersen6c7c4cb2013-06-20 23:50:59 -0700320 sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800321#endif
Johannes Berg07346f812008-05-03 01:02:02 +0200322
Johannes Berg17741cd2008-09-11 00:02:02 +0200323 memcpy(sta->sta.addr, addr, ETH_ALEN);
Johannes Bergd0709a62008-02-25 16:27:46 +0100324 sta->local = local;
325 sta->sdata = sdata;
Felix Fietkau8bc8aec2011-03-21 20:01:00 +0100326 sta->last_rx = jiffies;
Jiri Bencf0706e82007-05-05 11:45:53 -0700327
Johannes Berg71ec3752012-01-20 13:55:20 +0100328 sta->sta_state = IEEE80211_STA_NONE;
329
Liad Kaufmanb6da9112014-11-19 13:47:38 +0200330 /* Mark TID as unreserved */
331 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
332
Thomas Gleixner18171522014-06-11 23:59:14 +0000333 ktime_get_ts(&uptime);
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +0530334 sta->last_connected = uptime.tv_sec;
Bruno Randolf541a45a2010-12-02 19:12:43 +0900335 ewma_init(&sta->avg_signal, 1024, 8);
Felix Fietkauef0621e2013-04-22 16:29:31 +0200336 for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
337 ewma_init(&sta->chain_signal_avg[i], 1024, 8);
Bruno Randolf541a45a2010-12-02 19:12:43 +0900338
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100339 if (local->ops->wake_tx_queue) {
340 void *txq_data;
341 int size = sizeof(struct txq_info) +
342 ALIGN(hw->txq_data_size, sizeof(void *));
343
344 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
345 if (!txq_data)
346 goto free;
347
348 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
349 struct txq_info *txq = txq_data + i * size;
350
351 ieee80211_init_tx_queue(sdata, sta, txq, i);
352 }
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100353 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700354
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100355 if (sta_prepare_rate_control(local, sta, gfp))
356 goto free_txq;
357
Johannes Berg5a306f52012-11-14 23:22:21 +0100358 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berga622ab72010-06-10 10:21:39 +0200359 /*
360 * timer_to_tid must be initialized with identity mapping
361 * to enable session_timer's data differentiation. See
362 * sta_rx_agg_session_timer_expired for usage.
363 */
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200364 sta->timer_to_tid[i] = i;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200365 }
Johannes Berg948d8872011-09-29 16:04:29 +0200366 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
367 skb_queue_head_init(&sta->ps_tx_buf[i]);
368 skb_queue_head_init(&sta->tx_filtered[i]);
369 }
Johannes Berg73651ee2008-02-25 16:27:47 +0100370
Johannes Berg5a306f52012-11-14 23:22:21 +0100371 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700372 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
Senthil Balasubramaniancccaec92009-05-14 18:42:08 +0530373
Johannes Bergaf0ed692013-02-12 14:21:00 +0100374 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300375 if (sdata->vif.type == NL80211_IFTYPE_AP ||
376 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
377 struct ieee80211_supported_band *sband =
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100378 hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300379 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
380 IEEE80211_HT_CAP_SM_PS_SHIFT;
381 /*
382 * Assume that hostapd advertises our caps in the beacon and
383 * this is the known_smps_mode for a station that just assciated
384 */
385 switch (smps) {
386 case WLAN_HT_SMPS_CONTROL_DISABLED:
387 sta->known_smps_mode = IEEE80211_SMPS_OFF;
388 break;
389 case WLAN_HT_SMPS_CONTROL_STATIC:
390 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
391 break;
392 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
393 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
394 break;
395 default:
396 WARN_ON(1);
397 }
398 }
Johannes Bergaf0ed692013-02-12 14:21:00 +0100399
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200400 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
Johannes Bergef04a292014-01-06 15:56:59 +0100401
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100402 return sta;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100403
404free_txq:
405 if (sta->sta.txq[0])
406 kfree(to_txq_info(sta->sta.txq[0]));
407free:
408 kfree(sta);
409 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +0100410}
411
Guy Eilam8c71df72011-08-17 15:18:14 +0300412static int sta_info_insert_check(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100413{
Johannes Berg34e89502010-02-03 13:59:58 +0100414 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg34e89502010-02-03 13:59:58 +0100415
Johannes Berg03e44972008-02-27 09:56:40 +0100416 /*
417 * Can't be a WARN_ON because it can be triggered through a race:
418 * something inserts a STA (on one CPU) without holding the RTNL
419 * and another CPU turns off the net device.
420 */
Guy Eilam8c71df72011-08-17 15:18:14 +0300421 if (unlikely(!ieee80211_sdata_running(sdata)))
422 return -ENETDOWN;
Johannes Berg03e44972008-02-27 09:56:40 +0100423
Joe Perchesb203ca32012-05-08 18:56:52 +0000424 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
Guy Eilam8c71df72011-08-17 15:18:14 +0300425 is_multicast_ether_addr(sta->sta.addr)))
426 return -EINVAL;
427
428 return 0;
429}
430
Johannes Bergf09603a2012-01-20 13:55:21 +0100431static int sta_info_insert_drv_state(struct ieee80211_local *local,
432 struct ieee80211_sub_if_data *sdata,
433 struct sta_info *sta)
434{
435 enum ieee80211_sta_state state;
436 int err = 0;
437
438 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
439 err = drv_sta_state(local, sdata, sta, state, state + 1);
440 if (err)
441 break;
442 }
443
444 if (!err) {
Johannes Berga4ec45a2012-01-20 13:55:22 +0100445 /*
446 * Drivers using legacy sta_add/sta_remove callbacks only
447 * get uploaded set to true after sta_add is called.
448 */
449 if (!local->ops->sta_add)
450 sta->uploaded = true;
Johannes Bergf09603a2012-01-20 13:55:21 +0100451 return 0;
452 }
453
454 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200455 sdata_info(sdata,
456 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
457 sta->sta.addr, state + 1, err);
Johannes Bergf09603a2012-01-20 13:55:21 +0100458 err = 0;
459 }
460
461 /* unwind on error */
462 for (; state > IEEE80211_STA_NOTEXIST; state--)
463 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
464
465 return err;
466}
467
Guy Eilam8c71df72011-08-17 15:18:14 +0300468/*
469 * should be called with sta_mtx locked
470 * this function replaces the mutex lock
471 * with a RCU lock
472 */
Johannes Berg4d339602011-12-15 11:24:20 +0100473static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
Guy Eilam8c71df72011-08-17 15:18:14 +0300474{
475 struct ieee80211_local *local = sta->local;
476 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg7852e362012-01-20 13:55:24 +0100477 struct station_info sinfo;
Guy Eilam8c71df72011-08-17 15:18:14 +0300478 int err = 0;
479
480 lockdep_assert_held(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +0100481
Johannes Berg7852e362012-01-20 13:55:24 +0100482 /* check if STA exists already */
483 if (sta_info_get_bss(sdata, sta->sta.addr)) {
484 err = -EEXIST;
485 goto out_err;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100486 }
Johannes Berg32bfd352007-12-19 01:31:26 +0100487
Johannes Berg7852e362012-01-20 13:55:24 +0100488 local->num_sta++;
489 local->sta_generation++;
490 smp_mb();
Johannes Berg4d339602011-12-15 11:24:20 +0100491
Johannes Berg5108ca82014-02-17 20:49:03 +0100492 /* simplify things and don't accept BA sessions yet */
493 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
494
Johannes Berg7852e362012-01-20 13:55:24 +0100495 /* make the station visible */
496 sta_info_hash_add(local, sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100497
Arik Nemtsov2bad77482014-10-22 12:32:16 +0300498 list_add_tail_rcu(&sta->list, &local->sta_list);
Johannes Berg83d5cc02012-01-12 09:31:10 +0100499
Johannes Berg5108ca82014-02-17 20:49:03 +0100500 /* notify driver */
501 err = sta_info_insert_drv_state(local, sdata, sta);
502 if (err)
503 goto out_remove;
504
Johannes Berg7852e362012-01-20 13:55:24 +0100505 set_sta_flag(sta, WLAN_STA_INSERTED);
Johannes Berg5108ca82014-02-17 20:49:03 +0100506 /* accept BA sessions now */
507 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg4d339602011-12-15 11:24:20 +0100508
Eliad Peller21f659b2013-11-11 20:14:01 +0200509 ieee80211_recalc_min_chandef(sdata);
Johannes Berg7852e362012-01-20 13:55:24 +0100510 ieee80211_sta_debugfs_add(sta);
511 rate_control_add_sta_debugfs(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100512
Johannes Berg7852e362012-01-20 13:55:24 +0100513 memset(&sinfo, 0, sizeof(sinfo));
514 sinfo.filled = 0;
515 sinfo.generation = local->sta_generation;
516 cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
Johannes Bergd0709a62008-02-25 16:27:46 +0100517
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200518 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700519
Johannes Berg34e89502010-02-03 13:59:58 +0100520 /* move reference to rcu-protected */
521 rcu_read_lock();
522 mutex_unlock(&local->sta_mtx);
Jiri Bence9f207f2007-05-05 11:46:38 -0700523
Johannes Berg73651ee2008-02-25 16:27:47 +0100524 if (ieee80211_vif_is_mesh(&sdata->vif))
525 mesh_accept_plinks_update(sdata);
526
527 return 0;
Johannes Berg5108ca82014-02-17 20:49:03 +0100528 out_remove:
529 sta_info_hash_del(local, sta);
530 list_del_rcu(&sta->list);
531 local->num_sta--;
532 synchronize_net();
533 __cleanup_single_sta(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100534 out_err:
535 mutex_unlock(&local->sta_mtx);
536 rcu_read_lock();
537 return err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300538}
539
540int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
541{
542 struct ieee80211_local *local = sta->local;
Zhao, Gang308f7fc2014-04-21 12:53:00 +0800543 int err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300544
Johannes Berg4d339602011-12-15 11:24:20 +0100545 might_sleep();
546
Guy Eilam8c71df72011-08-17 15:18:14 +0300547 err = sta_info_insert_check(sta);
548 if (err) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700549 rcu_read_lock();
550 goto out_free;
551 }
552
Johannes Berg004c8722008-02-20 11:21:35 +0100553 mutex_lock(&local->sta_mtx);
554
Johannes Berg4d339602011-12-15 11:24:20 +0100555 err = sta_info_insert_finish(sta);
Guy Eilam8c71df72011-08-17 15:18:14 +0300556 if (err)
Johannes Berg004c8722008-02-20 11:21:35 +0100557 goto out_free;
Johannes Bergd0709a62008-02-25 16:27:46 +0100558
Johannes Berg004c8722008-02-20 11:21:35 +0100559 return 0;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200560 out_free:
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100561 sta_info_free(local, sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200562 return err;
Jiri Bencf0706e82007-05-05 11:45:53 -0700563}
564
Johannes Berg34e89502010-02-03 13:59:58 +0100565int sta_info_insert(struct sta_info *sta)
566{
567 int err = sta_info_insert_rcu(sta);
568
569 rcu_read_unlock();
570
571 return err;
572}
573
Marco Porschd012a602012-10-10 12:39:50 -0700574static inline void __bss_tim_set(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100575{
576 /*
577 * This format has been mandated by the IEEE specifications,
578 * so this line may not be changed to use the __set_bit() format.
579 */
Marco Porschd012a602012-10-10 12:39:50 -0700580 tim[id / 8] |= (1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100581}
582
Marco Porschd012a602012-10-10 12:39:50 -0700583static inline void __bss_tim_clear(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100584{
585 /*
586 * This format has been mandated by the IEEE specifications,
587 * so this line may not be changed to use the __clear_bit() format.
588 */
Marco Porschd012a602012-10-10 12:39:50 -0700589 tim[id / 8] &= ~(1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100590}
591
Ilan Peer3d5839b2013-03-05 15:27:20 +0200592static inline bool __bss_tim_get(u8 *tim, u16 id)
593{
594 /*
595 * This format has been mandated by the IEEE specifications,
596 * so this line may not be changed to use the test_bit() format.
597 */
598 return tim[id / 8] & (1 << (id % 8));
599}
600
Johannes Berg948d8872011-09-29 16:04:29 +0200601static unsigned long ieee80211_tids_for_ac(int ac)
Johannes Berg004c8722008-02-20 11:21:35 +0100602{
Johannes Berg948d8872011-09-29 16:04:29 +0200603 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
604 switch (ac) {
605 case IEEE80211_AC_VO:
606 return BIT(6) | BIT(7);
607 case IEEE80211_AC_VI:
608 return BIT(4) | BIT(5);
609 case IEEE80211_AC_BE:
610 return BIT(0) | BIT(3);
611 case IEEE80211_AC_BK:
612 return BIT(1) | BIT(2);
613 default:
614 WARN_ON(1);
615 return 0;
Johannes Bergd0709a62008-02-25 16:27:46 +0100616 }
Johannes Berg004c8722008-02-20 11:21:35 +0100617}
618
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100619static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
Johannes Berg004c8722008-02-20 11:21:35 +0100620{
Johannes Bergc868cb352011-09-29 16:04:27 +0200621 struct ieee80211_local *local = sta->local;
Marco Porschd012a602012-10-10 12:39:50 -0700622 struct ps_data *ps;
Johannes Berg948d8872011-09-29 16:04:29 +0200623 bool indicate_tim = false;
624 u8 ignore_for_tim = sta->sta.uapsd_queues;
625 int ac;
Marco Porschd012a602012-10-10 12:39:50 -0700626 u16 id;
Johannes Berg004c8722008-02-20 11:21:35 +0100627
Marco Porschd012a602012-10-10 12:39:50 -0700628 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
629 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
630 if (WARN_ON_ONCE(!sta->sdata->bss))
631 return;
632
633 ps = &sta->sdata->bss->ps;
634 id = sta->sta.aid;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100635#ifdef CONFIG_MAC80211_MESH
636 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
637 ps = &sta->sdata->u.mesh.ps;
Thomas Pedersen204d1302013-11-05 11:17:05 -0800638 /* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800639 id = sta->plid % (IEEE80211_MAX_AID + 1);
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100640#endif
Marco Porschd012a602012-10-10 12:39:50 -0700641 } else {
Johannes Bergc868cb352011-09-29 16:04:27 +0200642 return;
Marco Porschd012a602012-10-10 12:39:50 -0700643 }
Johannes Berg3e122be2008-07-09 14:40:34 +0200644
Johannes Bergc868cb352011-09-29 16:04:27 +0200645 /* No need to do anything if the driver does all */
Johannes Berg30686bf2015-06-02 21:39:54 +0200646 if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
Johannes Bergc868cb352011-09-29 16:04:27 +0200647 return;
Johannes Berg004c8722008-02-20 11:21:35 +0100648
Johannes Bergc868cb352011-09-29 16:04:27 +0200649 if (sta->dead)
650 goto done;
Johannes Berg3e122be2008-07-09 14:40:34 +0200651
Johannes Berg948d8872011-09-29 16:04:29 +0200652 /*
653 * If all ACs are delivery-enabled then we should build
654 * the TIM bit for all ACs anyway; if only some are then
655 * we ignore those and build the TIM bit using only the
656 * non-enabled ones.
657 */
658 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
659 ignore_for_tim = 0;
Johannes Berg3e122be2008-07-09 14:40:34 +0200660
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100661 if (ignore_pending)
662 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
663
Johannes Berg948d8872011-09-29 16:04:29 +0200664 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
665 unsigned long tids;
666
667 if (ignore_for_tim & BIT(ac))
668 continue;
669
670 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
671 !skb_queue_empty(&sta->ps_tx_buf[ac]);
672 if (indicate_tim)
673 break;
674
675 tids = ieee80211_tids_for_ac(ac);
676
677 indicate_tim |=
678 sta->driver_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100679 indicate_tim |=
680 sta->txq_buffered_tids & tids;
Johannes Bergd0709a62008-02-25 16:27:46 +0100681 }
Johannes Berg004c8722008-02-20 11:21:35 +0100682
Johannes Bergc868cb352011-09-29 16:04:27 +0200683 done:
Johannes Berg65f704a2013-02-13 17:39:53 +0100684 spin_lock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100685
Ilan Peer3d5839b2013-03-05 15:27:20 +0200686 if (indicate_tim == __bss_tim_get(ps->tim, id))
687 goto out_unlock;
688
Johannes Berg948d8872011-09-29 16:04:29 +0200689 if (indicate_tim)
Marco Porschd012a602012-10-10 12:39:50 -0700690 __bss_tim_set(ps->tim, id);
Johannes Bergc868cb352011-09-29 16:04:27 +0200691 else
Marco Porschd012a602012-10-10 12:39:50 -0700692 __bss_tim_clear(ps->tim, id);
Johannes Berg3e122be2008-07-09 14:40:34 +0200693
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100694 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
Johannes Bergc868cb352011-09-29 16:04:27 +0200695 local->tim_in_locked_section = true;
Johannes Berg948d8872011-09-29 16:04:29 +0200696 drv_set_tim(local, &sta->sta, indicate_tim);
Johannes Bergc868cb352011-09-29 16:04:27 +0200697 local->tim_in_locked_section = false;
Johannes Berg004c8722008-02-20 11:21:35 +0100698 }
Johannes Berg004c8722008-02-20 11:21:35 +0100699
Ilan Peer3d5839b2013-03-05 15:27:20 +0200700out_unlock:
Johannes Berg65f704a2013-02-13 17:39:53 +0100701 spin_unlock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100702}
703
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100704void sta_info_recalc_tim(struct sta_info *sta)
705{
706 __sta_info_recalc_tim(sta, false);
707}
708
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200709static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700710{
Johannes Berge039fa42008-05-15 12:55:29 +0200711 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700712 int timeout;
713
714 if (!skb)
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200715 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700716
Johannes Berge039fa42008-05-15 12:55:29 +0200717 info = IEEE80211_SKB_CB(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700718
719 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200720 timeout = (sta->listen_interval *
721 sta->sdata->vif.bss_conf.beacon_int *
722 32 / 15625) * HZ;
Jiri Bencf0706e82007-05-05 11:45:53 -0700723 if (timeout < STA_TX_BUFFER_EXPIRE)
724 timeout = STA_TX_BUFFER_EXPIRE;
Johannes Berge039fa42008-05-15 12:55:29 +0200725 return time_after(jiffies, info->control.jiffies + timeout);
Jiri Bencf0706e82007-05-05 11:45:53 -0700726}
727
728
Johannes Berg948d8872011-09-29 16:04:29 +0200729static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
730 struct sta_info *sta, int ac)
Jiri Bencf0706e82007-05-05 11:45:53 -0700731{
732 unsigned long flags;
733 struct sk_buff *skb;
734
Johannes Berg60750392011-09-29 16:04:28 +0200735 /*
736 * First check for frames that should expire on the filtered
737 * queue. Frames here were rejected by the driver and are on
738 * a separate queue to avoid reordering with normal PS-buffered
739 * frames. They also aren't accounted for right now in the
740 * total_ps_buffered counter.
741 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700742 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200743 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
744 skb = skb_peek(&sta->tx_filtered[ac]);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200745 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200746 skb = __skb_dequeue(&sta->tx_filtered[ac]);
Johannes Berg836341a2008-02-20 02:07:21 +0100747 else
Jiri Bencf0706e82007-05-05 11:45:53 -0700748 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200749 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700750
Johannes Berg60750392011-09-29 16:04:28 +0200751 /*
752 * Frames are queued in order, so if this one
753 * hasn't expired yet we can stop testing. If
754 * we actually reached the end of the queue we
755 * also need to stop, of course.
756 */
757 if (!skb)
758 break;
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200759 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg60750392011-09-29 16:04:28 +0200760 }
761
762 /*
763 * Now also check the normal PS-buffered queue, this will
764 * only find something if the filtered queue was emptied
765 * since the filtered frames are all before the normal PS
766 * buffered frames.
767 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700768 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200769 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
770 skb = skb_peek(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700771 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200772 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700773 else
774 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200775 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700776
Johannes Berg60750392011-09-29 16:04:28 +0200777 /*
778 * frames are queued in order, so if this one
779 * hasn't expired yet (or we reached the end of
780 * the queue) we can stop testing
781 */
Johannes Berg836341a2008-02-20 02:07:21 +0100782 if (!skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700783 break;
Johannes Berg836341a2008-02-20 02:07:21 +0100784
Johannes Berg836341a2008-02-20 02:07:21 +0100785 local->total_ps_buffered--;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200786 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
787 sta->sta.addr);
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200788 ieee80211_free_txskb(&local->hw, skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700789 }
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300790
Johannes Berg60750392011-09-29 16:04:28 +0200791 /*
792 * Finally, recalculate the TIM bit for this station -- it might
793 * now be clear because the station was too slow to retrieve its
794 * frames.
795 */
796 sta_info_recalc_tim(sta);
797
798 /*
799 * Return whether there are any frames still buffered, this is
800 * used to check whether the cleanup timer still needs to run,
801 * if there are no frames we don't need to rearm the timer.
802 */
Johannes Berg948d8872011-09-29 16:04:29 +0200803 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
804 skb_queue_empty(&sta->tx_filtered[ac]));
805}
806
807static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
808 struct sta_info *sta)
809{
810 bool have_buffered = false;
811 int ac;
812
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100813 /* This is only necessary for stations on BSS/MBSS interfaces */
814 if (!sta->sdata->bss &&
815 !ieee80211_vif_is_mesh(&sta->sdata->vif))
Johannes Berg948d8872011-09-29 16:04:29 +0200816 return false;
817
818 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
819 have_buffered |=
820 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
821
822 return have_buffered;
Jiri Bencf0706e82007-05-05 11:45:53 -0700823}
824
Johannes Bergd7782072013-12-04 23:12:31 +0100825static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100826{
827 struct ieee80211_local *local;
828 struct ieee80211_sub_if_data *sdata;
Johannes Berg6d10e462013-03-06 23:09:11 +0100829 int ret;
Johannes Berg34e89502010-02-03 13:59:58 +0100830
831 might_sleep();
832
833 if (!sta)
834 return -ENOENT;
835
836 local = sta->local;
837 sdata = sta->sdata;
838
Johannes Berg83d5cc02012-01-12 09:31:10 +0100839 lockdep_assert_held(&local->sta_mtx);
840
Johannes Berg098a6072010-04-06 11:18:47 +0200841 /*
842 * Before removing the station from the driver and
843 * rate control, it might still start new aggregation
844 * sessions -- block that to make sure the tear-down
845 * will be sufficient.
846 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200847 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Bergc82c4a82012-07-18 13:31:31 +0200848 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
Johannes Berg098a6072010-04-06 11:18:47 +0200849
Johannes Berg34e89502010-02-03 13:59:58 +0100850 ret = sta_info_hash_del(local, sta);
Johannes Bergb01711b2013-12-04 20:25:27 +0100851 if (WARN_ON(ret))
Johannes Berg34e89502010-02-03 13:59:58 +0100852 return ret;
853
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200854 /*
855 * for TDLS peers, make sure to return to the base channel before
856 * removal.
857 */
858 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
859 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
860 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
861 }
862
Arik Nemtsov794454c2012-06-03 23:32:32 +0300863 list_del_rcu(&sta->list);
Johannes Berg4d339602011-12-15 11:24:20 +0100864
Johannes Berg6a9d1b92013-12-04 22:39:17 +0100865 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
866
Johannes Berga710c812013-12-04 20:11:06 +0100867 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
868 rcu_access_pointer(sdata->u.vlan.sta) == sta)
869 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
870
Johannes Bergd7782072013-12-04 23:12:31 +0100871 return 0;
872}
873
874static void __sta_info_destroy_part2(struct sta_info *sta)
875{
876 struct ieee80211_local *local = sta->local;
877 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg6f7a8d22014-11-14 20:32:57 +0100878 struct station_info sinfo = {};
Johannes Bergd7782072013-12-04 23:12:31 +0100879 int ret;
880
881 /*
882 * NOTE: This assumes at least synchronize_net() was done
883 * after _part1 and before _part2!
884 */
885
886 might_sleep();
887 lockdep_assert_held(&local->sta_mtx);
888
Johannes Bergc8782072013-12-04 23:05:45 +0100889 /* now keys can no longer be reached */
Johannes Berg6d10e462013-03-06 23:09:11 +0100890 ieee80211_free_sta_keys(local, sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100891
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100892 /* disable TIM bit - last chance to tell driver */
893 __sta_info_recalc_tim(sta, true);
894
Johannes Berg34e89502010-02-03 13:59:58 +0100895 sta->dead = true;
896
Johannes Berg34e89502010-02-03 13:59:58 +0100897 local->num_sta--;
898 local->sta_generation++;
899
Johannes Berg83d5cc02012-01-12 09:31:10 +0100900 while (sta->sta_state > IEEE80211_STA_NONE) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100901 ret = sta_info_move_state(sta, sta->sta_state - 1);
902 if (ret) {
Johannes Berg83d5cc02012-01-12 09:31:10 +0100903 WARN_ON_ONCE(1);
904 break;
905 }
906 }
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100907
Johannes Bergf09603a2012-01-20 13:55:21 +0100908 if (sta->uploaded) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100909 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
910 IEEE80211_STA_NOTEXIST);
911 WARN_ON_ONCE(ret != 0);
912 }
Johannes Berg34e89502010-02-03 13:59:58 +0100913
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200914 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
915
Johannes Berg6f7a8d22014-11-14 20:32:57 +0100916 sta_set_sinfo(sta, &sinfo);
917 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
Jouni Malinenec15e682011-03-23 15:29:52 +0200918
Johannes Berg34e89502010-02-03 13:59:58 +0100919 rate_control_remove_sta_debugfs(sta);
920 ieee80211_sta_debugfs_remove(sta);
Eliad Peller21f659b2013-11-11 20:14:01 +0200921 ieee80211_recalc_min_chandef(sdata);
Johannes Berg34e89502010-02-03 13:59:58 +0100922
Johannes Bergd34ba212013-12-04 22:46:11 +0100923 cleanup_single_sta(sta);
Johannes Bergd7782072013-12-04 23:12:31 +0100924}
925
926int __must_check __sta_info_destroy(struct sta_info *sta)
927{
928 int err = __sta_info_destroy_part1(sta);
929
930 if (err)
931 return err;
932
933 synchronize_net();
934
935 __sta_info_destroy_part2(sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100936
937 return 0;
938}
939
940int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
941{
942 struct sta_info *sta;
943 int ret;
944
945 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100946 sta = sta_info_get(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100947 ret = __sta_info_destroy(sta);
948 mutex_unlock(&sdata->local->sta_mtx);
949
950 return ret;
951}
952
953int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
954 const u8 *addr)
955{
956 struct sta_info *sta;
957 int ret;
958
959 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100960 sta = sta_info_get_bss(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100961 ret = __sta_info_destroy(sta);
962 mutex_unlock(&sdata->local->sta_mtx);
963
964 return ret;
965}
Jiri Bencf0706e82007-05-05 11:45:53 -0700966
967static void sta_info_cleanup(unsigned long data)
968{
969 struct ieee80211_local *local = (struct ieee80211_local *) data;
970 struct sta_info *sta;
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300971 bool timer_needed = false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700972
Johannes Bergd0709a62008-02-25 16:27:46 +0100973 rcu_read_lock();
974 list_for_each_entry_rcu(sta, &local->sta_list, list)
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300975 if (sta_info_cleanup_expire_buffered(local, sta))
976 timer_needed = true;
Johannes Bergd0709a62008-02-25 16:27:46 +0100977 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -0700978
Johannes Berg5bb644a2009-05-17 11:40:42 +0200979 if (local->quiescing)
980 return;
981
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300982 if (!timer_needed)
983 return;
984
Johannes Berg26d59532011-04-01 13:52:48 +0200985 mod_timer(&local->sta_cleanup,
986 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
Jiri Bencf0706e82007-05-05 11:45:53 -0700987}
988
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100989u32 sta_addr_hash(const void *key, u32 length, u32 seed)
Jiri Bencf0706e82007-05-05 11:45:53 -0700990{
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100991 return jhash(key, ETH_ALEN, seed);
992}
993
994int sta_info_init(struct ieee80211_local *local)
995{
996 int err;
997
998 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
999 if (err)
1000 return err;
1001
Johannes Berg4d339602011-12-15 11:24:20 +01001002 spin_lock_init(&local->tim_lock);
Johannes Berg34e89502010-02-03 13:59:58 +01001003 mutex_init(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07001004 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001005
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001006 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1007 (unsigned long)local);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001008 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001009}
1010
1011void sta_info_stop(struct ieee80211_local *local)
1012{
Johannes Berga56f9922012-12-13 23:08:52 +01001013 del_timer_sync(&local->sta_cleanup);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001014 rhashtable_destroy(&local->sta_hash);
Jiri Bencf0706e82007-05-05 11:45:53 -07001015}
1016
Johannes Berg051007d2012-12-13 23:49:02 +01001017
Johannes Berge7162512013-12-04 23:18:37 +01001018int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
Jiri Bencf0706e82007-05-05 11:45:53 -07001019{
Johannes Bergb998e8b2012-12-13 23:07:46 +01001020 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001021 struct sta_info *sta, *tmp;
Johannes Bergd7782072013-12-04 23:12:31 +01001022 LIST_HEAD(free_list);
Johannes Berg44213b52008-02-25 16:27:49 +01001023 int ret = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001024
Johannes Bergd0709a62008-02-25 16:27:46 +01001025 might_sleep();
1026
Johannes Berge7162512013-12-04 23:18:37 +01001027 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1028 WARN_ON(vlans && !sdata->bss);
1029
Johannes Berg34e89502010-02-03 13:59:58 +01001030 mutex_lock(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +01001031 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Johannes Berge7162512013-12-04 23:18:37 +01001032 if (sdata == sta->sdata ||
1033 (vlans && sdata->bss == sta->sdata->bss)) {
Johannes Bergd7782072013-12-04 23:12:31 +01001034 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1035 list_add(&sta->free_list, &free_list);
Johannes Berg34316832012-02-25 21:40:46 +01001036 ret++;
1037 }
Johannes Berg34e89502010-02-03 13:59:58 +01001038 }
Johannes Bergd7782072013-12-04 23:12:31 +01001039
1040 if (!list_empty(&free_list)) {
1041 synchronize_net();
1042 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1043 __sta_info_destroy_part2(sta);
1044 }
Johannes Berg34e89502010-02-03 13:59:58 +01001045 mutex_unlock(&local->sta_mtx);
Johannes Berg44213b52008-02-25 16:27:49 +01001046
Johannes Berg051007d2012-12-13 23:49:02 +01001047 return ret;
1048}
1049
Johannes Berg24723d12008-09-11 00:01:46 +02001050void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1051 unsigned long exp_time)
1052{
1053 struct ieee80211_local *local = sdata->local;
1054 struct sta_info *sta, *tmp;
Johannes Berg24723d12008-09-11 00:01:46 +02001055
Johannes Berg34e89502010-02-03 13:59:58 +01001056 mutex_lock(&local->sta_mtx);
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301057
1058 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Marek Lindnerec2b7742011-12-20 23:16:52 +08001059 if (sdata != sta->sdata)
1060 continue;
1061
Johannes Berg24723d12008-09-11 00:01:46 +02001062 if (time_after(jiffies, sta->last_rx + exp_time)) {
Mohammed Shafi Shajakhaneea57d42012-10-08 21:33:47 +05301063 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1064 sta->sta.addr);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001065
1066 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1067 test_sta_flag(sta, WLAN_STA_PS_STA))
1068 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1069
Johannes Berg34e89502010-02-03 13:59:58 +01001070 WARN_ON(__sta_info_destroy(sta));
Johannes Berg24723d12008-09-11 00:01:46 +02001071 }
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301072 }
1073
Johannes Berg34e89502010-02-03 13:59:58 +01001074 mutex_unlock(&local->sta_mtx);
Johannes Berg24723d12008-09-11 00:01:46 +02001075}
Johannes Berg17741cd2008-09-11 00:02:02 +02001076
Ben Greear686b9cb2010-09-23 09:44:36 -07001077struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001078 const u8 *addr,
1079 const u8 *localaddr)
Johannes Berg17741cd2008-09-11 00:02:02 +02001080{
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001081 struct ieee80211_local *local = hw_to_local(hw);
1082 struct sta_info *sta;
1083 struct rhash_head *tmp;
1084 const struct bucket_table *tbl;
1085
1086 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
Johannes Berg17741cd2008-09-11 00:02:02 +02001087
Ben Greear686b9cb2010-09-23 09:44:36 -07001088 /*
1089 * Just return a random station if localaddr is NULL
1090 * ... first in list.
1091 */
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001092 for_each_sta_info(local, tbl, addr, sta, tmp) {
Ben Greear686b9cb2010-09-23 09:44:36 -07001093 if (localaddr &&
Joe Perchesb203ca32012-05-08 18:56:52 +00001094 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
Ben Greear686b9cb2010-09-23 09:44:36 -07001095 continue;
Johannes Bergf7c65592010-04-30 13:48:36 +02001096 if (!sta->uploaded)
1097 return NULL;
Johannes Bergabe60632009-11-25 17:46:18 +01001098 return &sta->sta;
Johannes Bergf7c65592010-04-30 13:48:36 +02001099 }
1100
Johannes Bergabe60632009-11-25 17:46:18 +01001101 return NULL;
Johannes Berg17741cd2008-09-11 00:02:02 +02001102}
Ben Greear686b9cb2010-09-23 09:44:36 -07001103EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
Johannes Berg5ed176e2009-11-04 14:42:28 +01001104
1105struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1106 const u8 *addr)
1107{
Johannes Bergf7c65592010-04-30 13:48:36 +02001108 struct sta_info *sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001109
1110 if (!vif)
1111 return NULL;
1112
Johannes Bergf7c65592010-04-30 13:48:36 +02001113 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1114 if (!sta)
1115 return NULL;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001116
Johannes Bergf7c65592010-04-30 13:48:36 +02001117 if (!sta->uploaded)
1118 return NULL;
1119
1120 return &sta->sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001121}
Johannes Berg17741cd2008-09-11 00:02:02 +02001122EXPORT_SYMBOL(ieee80211_find_sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001123
Johannes Berge3685e02014-02-20 11:19:58 +01001124/* powersave support code */
1125void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
Johannes Berg50a94322010-11-16 11:50:28 -08001126{
Helmut Schaa608383b2012-01-30 15:18:00 +01001127 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berge3685e02014-02-20 11:19:58 +01001128 struct ieee80211_local *local = sdata->local;
1129 struct sk_buff_head pending;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001130 int filtered = 0, buffered = 0, ac, i;
Johannes Berge3685e02014-02-20 11:19:58 +01001131 unsigned long flags;
Marco Porschd012a602012-10-10 12:39:50 -07001132 struct ps_data *ps;
1133
Felix Fietkau3918edb2014-07-25 16:20:23 +02001134 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1135 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1136 u.ap);
1137
1138 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porschd012a602012-10-10 12:39:50 -07001139 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001140 else if (ieee80211_vif_is_mesh(&sdata->vif))
1141 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07001142 else
1143 return;
Johannes Berg50a94322010-11-16 11:50:28 -08001144
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001145 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg47086fc2011-09-29 16:04:33 +02001146
Johannes Berg5a306f52012-11-14 23:22:21 +01001147 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
Johannes Berg948d8872011-09-29 16:04:29 +02001148 sta->driver_buffered_tids = 0;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001149 sta->txq_buffered_tids = 0;
Johannes Berg948d8872011-09-29 16:04:29 +02001150
Johannes Berg30686bf2015-06-02 21:39:54 +02001151 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001152 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001153
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001154 if (sta->sta.txq[0]) {
1155 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1156 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1157
1158 if (!skb_queue_len(&txqi->queue))
1159 continue;
1160
1161 drv_wake_tx_queue(local, txqi);
1162 }
1163 }
1164
Johannes Berg948d8872011-09-29 16:04:29 +02001165 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001166
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001167 /* sync with ieee80211_tx_h_unicast_ps_buf */
1168 spin_lock(&sta->ps_lock);
Johannes Bergaf818582009-11-06 11:35:50 +01001169 /* Send all buffered frames to the station */
Johannes Berg948d8872011-09-29 16:04:29 +02001170 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1171 int count = skb_queue_len(&pending), tmp;
1172
Arik Nemtsov987c2852012-11-05 10:27:52 +02001173 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001174 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001175 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001176 tmp = skb_queue_len(&pending);
1177 filtered += tmp - count;
1178 count = tmp;
1179
Arik Nemtsov987c2852012-11-05 10:27:52 +02001180 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001181 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001182 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001183 tmp = skb_queue_len(&pending);
1184 buffered += tmp - count;
1185 }
1186
Johannes Berge3685e02014-02-20 11:19:58 +01001187 ieee80211_add_pending_skbs(local, &pending);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001188
1189 /* now we're no longer in the deliver code */
1190 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1191
1192 /* The station might have polled and then woken up before we responded,
1193 * so clear these flags now to avoid them sticking around.
1194 */
1195 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1196 clear_sta_flag(sta, WLAN_STA_UAPSD);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001197 spin_unlock(&sta->ps_lock);
Johannes Berg948d8872011-09-29 16:04:29 +02001198
Johannes Berge3685e02014-02-20 11:19:58 +01001199 atomic_dec(&ps->num_sta_ps);
1200
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001201 /* This station just woke up and isn't aware of our SMPS state */
Chun-Yeow Yeoh062f1d62014-04-22 18:19:25 +08001202 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1203 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001204 sdata->smps_mode) &&
1205 sta->known_smps_mode != sdata->bss->req_smps &&
1206 sta_info_tx_streams(sta) != 1) {
1207 ht_dbg(sdata,
1208 "%pM just woke up and MIMO capable - update SMPS\n",
1209 sta->sta.addr);
1210 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1211 sta->sta.addr,
1212 sdata->vif.bss_conf.bssid);
1213 }
1214
Johannes Bergaf818582009-11-06 11:35:50 +01001215 local->total_ps_buffered -= buffered;
1216
Johannes Bergc868cb352011-09-29 16:04:27 +02001217 sta_info_recalc_tim(sta);
1218
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001219 ps_dbg(sdata,
1220 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1221 sta->sta.addr, sta->sta.aid, filtered, buffered);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001222
1223 ieee80211_check_fast_xmit(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001224}
1225
Johannes Bergce662b442011-09-29 16:04:34 +02001226static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1227 struct sta_info *sta, int tid,
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001228 enum ieee80211_frame_release_type reason,
1229 bool call_driver)
Johannes Bergce662b442011-09-29 16:04:34 +02001230{
1231 struct ieee80211_local *local = sdata->local;
1232 struct ieee80211_qos_hdr *nullfunc;
1233 struct sk_buff *skb;
1234 int size = sizeof(*nullfunc);
1235 __le16 fc;
Johannes Berga74a8c82014-07-22 14:50:47 +02001236 bool qos = sta->sta.wme;
Johannes Bergce662b442011-09-29 16:04:34 +02001237 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02001238 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergce662b442011-09-29 16:04:34 +02001239
1240 if (qos) {
1241 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1242 IEEE80211_STYPE_QOS_NULLFUNC |
1243 IEEE80211_FCTL_FROMDS);
1244 } else {
1245 size -= 2;
1246 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1247 IEEE80211_STYPE_NULLFUNC |
1248 IEEE80211_FCTL_FROMDS);
1249 }
1250
1251 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1252 if (!skb)
1253 return;
1254
1255 skb_reserve(skb, local->hw.extra_tx_headroom);
1256
1257 nullfunc = (void *) skb_put(skb, size);
1258 nullfunc->frame_control = fc;
1259 nullfunc->duration_id = 0;
1260 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1261 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1262 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
Johannes Berg864a6042014-03-04 13:46:53 +01001263 nullfunc->seq_ctrl = 0;
Johannes Bergce662b442011-09-29 16:04:34 +02001264
Johannes Berg59b66252011-10-13 13:19:19 +02001265 skb->priority = tid;
1266 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
Johannes Bergce662b442011-09-29 16:04:34 +02001267 if (qos) {
Johannes Bergce662b442011-09-29 16:04:34 +02001268 nullfunc->qos_ctrl = cpu_to_le16(tid);
1269
Johannes Berg40b96402011-09-29 16:04:38 +02001270 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
Johannes Bergce662b442011-09-29 16:04:34 +02001271 nullfunc->qos_ctrl |=
1272 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1273 }
1274
1275 info = IEEE80211_SKB_CB(skb);
1276
1277 /*
1278 * Tell TX path to send this frame even though the
1279 * STA may still remain is PS mode after this frame
Johannes Bergdeeaee12011-09-29 16:04:35 +02001280 * exchange. Also set EOSP to indicate this packet
1281 * ends the poll/service period.
Johannes Bergce662b442011-09-29 16:04:34 +02001282 */
Johannes Berg02f2f1a2012-02-27 12:18:30 +01001283 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
Johannes Bergdeeaee12011-09-29 16:04:35 +02001284 IEEE80211_TX_STATUS_EOSP |
1285 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergce662b442011-09-29 16:04:34 +02001286
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301287 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1288
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001289 if (call_driver)
1290 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1291 reason, false);
Johannes Berg40b96402011-09-29 16:04:38 +02001292
Johannes Berg89afe612013-02-13 15:39:57 +01001293 skb->dev = sdata->dev;
1294
Johannes Berg55de9082012-07-26 17:24:39 +02001295 rcu_read_lock();
1296 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1297 if (WARN_ON(!chanctx_conf)) {
1298 rcu_read_unlock();
1299 kfree_skb(skb);
1300 return;
1301 }
1302
Johannes Berg73c4e192014-11-09 18:50:09 +02001303 info->band = chanctx_conf->def.chan->band;
Johannes Berg7c107702015-03-20 14:18:27 +01001304 ieee80211_xmit(sdata, sta, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001305 rcu_read_unlock();
Johannes Bergce662b442011-09-29 16:04:34 +02001306}
1307
Johannes Berg0a1cb802014-01-09 11:05:31 +01001308static int find_highest_prio_tid(unsigned long tids)
1309{
1310 /* lower 3 TIDs aren't ordered perfectly */
1311 if (tids & 0xF8)
1312 return fls(tids) - 1;
1313 /* TID 0 is BE just like TID 3 */
1314 if (tids & BIT(0))
1315 return 0;
1316 return fls(tids) - 1;
1317}
1318
Johannes Berg47086fc2011-09-29 16:04:33 +02001319static void
1320ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1321 int n_frames, u8 ignored_acs,
1322 enum ieee80211_frame_release_type reason)
Johannes Bergaf818582009-11-06 11:35:50 +01001323{
1324 struct ieee80211_sub_if_data *sdata = sta->sdata;
1325 struct ieee80211_local *local = sdata->local;
Johannes Berg948d8872011-09-29 16:04:29 +02001326 bool more_data = false;
1327 int ac;
Johannes Berg4049e092011-09-29 16:04:32 +02001328 unsigned long driver_release_tids = 0;
Johannes Berg47086fc2011-09-29 16:04:33 +02001329 struct sk_buff_head frames;
1330
Johannes Bergdeeaee12011-09-29 16:04:35 +02001331 /* Service or PS-Poll period starts */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001332 set_sta_flag(sta, WLAN_STA_SP);
Johannes Bergdeeaee12011-09-29 16:04:35 +02001333
Johannes Berg47086fc2011-09-29 16:04:33 +02001334 __skb_queue_head_init(&frames);
Johannes Bergaf818582009-11-06 11:35:50 +01001335
Johannes Bergf9f760b2014-01-08 17:45:07 +01001336 /* Get response frame(s) and more data bit for the last one. */
Johannes Berg948d8872011-09-29 16:04:29 +02001337 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg4049e092011-09-29 16:04:32 +02001338 unsigned long tids;
1339
Johannes Berg47086fc2011-09-29 16:04:33 +02001340 if (ignored_acs & BIT(ac))
Johannes Berg948d8872011-09-29 16:04:29 +02001341 continue;
1342
Johannes Berg4049e092011-09-29 16:04:32 +02001343 tids = ieee80211_tids_for_ac(ac);
1344
Johannes Bergf9f760b2014-01-08 17:45:07 +01001345 /* if we already have frames from software, then we can't also
1346 * release from hardware queues
1347 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001348 if (skb_queue_empty(&frames)) {
Johannes Bergf9f760b2014-01-08 17:45:07 +01001349 driver_release_tids |= sta->driver_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001350 driver_release_tids |= sta->txq_buffered_tids & tids;
1351 }
Johannes Berg47086fc2011-09-29 16:04:33 +02001352
Johannes Bergf9f760b2014-01-08 17:45:07 +01001353 if (driver_release_tids) {
1354 /* If the driver has data on more than one TID then
Johannes Berg4049e092011-09-29 16:04:32 +02001355 * certainly there's more data if we release just a
Johannes Bergf9f760b2014-01-08 17:45:07 +01001356 * single frame now (from a single TID). This will
1357 * only happen for PS-Poll.
Johannes Berg4049e092011-09-29 16:04:32 +02001358 */
Johannes Berg47086fc2011-09-29 16:04:33 +02001359 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1360 hweight16(driver_release_tids) > 1) {
Johannes Berg4049e092011-09-29 16:04:32 +02001361 more_data = true;
1362 driver_release_tids =
Johannes Berg0a1cb802014-01-09 11:05:31 +01001363 BIT(find_highest_prio_tid(
1364 driver_release_tids));
Johannes Berg4049e092011-09-29 16:04:32 +02001365 break;
Johannes Berg948d8872011-09-29 16:04:29 +02001366 }
Johannes Bergf9f760b2014-01-08 17:45:07 +01001367 } else {
1368 struct sk_buff *skb;
1369
1370 while (n_frames > 0) {
1371 skb = skb_dequeue(&sta->tx_filtered[ac]);
1372 if (!skb) {
1373 skb = skb_dequeue(
1374 &sta->ps_tx_buf[ac]);
1375 if (skb)
1376 local->total_ps_buffered--;
1377 }
1378 if (!skb)
1379 break;
1380 n_frames--;
1381 __skb_queue_tail(&frames, skb);
1382 }
Johannes Berg948d8872011-09-29 16:04:29 +02001383 }
1384
Johannes Bergf9f760b2014-01-08 17:45:07 +01001385 /* If we have more frames buffered on this AC, then set the
1386 * more-data bit and abort the loop since we can't send more
1387 * data from other ACs before the buffered frames from this.
1388 */
Johannes Berg948d8872011-09-29 16:04:29 +02001389 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1390 !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1391 more_data = true;
1392 break;
1393 }
Johannes Bergaf818582009-11-06 11:35:50 +01001394 }
Johannes Bergaf818582009-11-06 11:35:50 +01001395
Johannes Bergf9f760b2014-01-08 17:45:07 +01001396 if (skb_queue_empty(&frames) && !driver_release_tids) {
Johannes Bergce662b442011-09-29 16:04:34 +02001397 int tid;
Johannes Berg4049e092011-09-29 16:04:32 +02001398
Johannes Bergce662b442011-09-29 16:04:34 +02001399 /*
1400 * For PS-Poll, this can only happen due to a race condition
1401 * when we set the TIM bit and the station notices it, but
1402 * before it can poll for the frame we expire it.
1403 *
1404 * For uAPSD, this is said in the standard (11.2.1.5 h):
1405 * At each unscheduled SP for a non-AP STA, the AP shall
1406 * attempt to transmit at least one MSDU or MMPDU, but no
1407 * more than the value specified in the Max SP Length field
1408 * in the QoS Capability element from delivery-enabled ACs,
1409 * that are destined for the non-AP STA.
1410 *
1411 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1412 */
1413
1414 /* This will evaluate to 1, 3, 5 or 7. */
1415 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1416
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001417 ieee80211_send_null_response(sdata, sta, tid, reason, true);
Johannes Bergf9f760b2014-01-08 17:45:07 +01001418 } else if (!driver_release_tids) {
Johannes Berg47086fc2011-09-29 16:04:33 +02001419 struct sk_buff_head pending;
1420 struct sk_buff *skb;
Johannes Berg40b96402011-09-29 16:04:38 +02001421 int num = 0;
1422 u16 tids = 0;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001423 bool need_null = false;
Johannes Bergaf818582009-11-06 11:35:50 +01001424
Johannes Berg47086fc2011-09-29 16:04:33 +02001425 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001426
Johannes Berg47086fc2011-09-29 16:04:33 +02001427 while ((skb = __skb_dequeue(&frames))) {
1428 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1429 struct ieee80211_hdr *hdr = (void *) skb->data;
Johannes Berg40b96402011-09-29 16:04:38 +02001430 u8 *qoshdr = NULL;
1431
1432 num++;
Johannes Bergaf818582009-11-06 11:35:50 +01001433
Johannes Berg47086fc2011-09-29 16:04:33 +02001434 /*
1435 * Tell TX path to send this frame even though the
1436 * STA may still remain is PS mode after this frame
1437 * exchange.
1438 */
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301439 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1440 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
Johannes Bergaf818582009-11-06 11:35:50 +01001441
Johannes Berg47086fc2011-09-29 16:04:33 +02001442 /*
1443 * Use MoreData flag to indicate whether there are
1444 * more buffered frames for this STA
1445 */
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001446 if (more_data || !skb_queue_empty(&frames))
Johannes Berg47086fc2011-09-29 16:04:33 +02001447 hdr->frame_control |=
1448 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001449 else
1450 hdr->frame_control &=
1451 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
Johannes Berg47086fc2011-09-29 16:04:33 +02001452
Johannes Berg40b96402011-09-29 16:04:38 +02001453 if (ieee80211_is_data_qos(hdr->frame_control) ||
1454 ieee80211_is_qos_nullfunc(hdr->frame_control))
1455 qoshdr = ieee80211_get_qos_ctl(hdr);
1456
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001457 tids |= BIT(skb->priority);
1458
1459 __skb_queue_tail(&pending, skb);
1460
1461 /* end service period after last frame or add one */
1462 if (!skb_queue_empty(&frames))
1463 continue;
1464
1465 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1466 /* for PS-Poll, there's only one frame */
1467 info->flags |= IEEE80211_TX_STATUS_EOSP |
1468 IEEE80211_TX_CTL_REQ_TX_STATUS;
1469 break;
1470 }
1471
1472 /* For uAPSD, things are a bit more complicated. If the
1473 * last frame has a QoS header (i.e. is a QoS-data or
1474 * QoS-nulldata frame) then just set the EOSP bit there
1475 * and be done.
1476 * If the frame doesn't have a QoS header (which means
1477 * it should be a bufferable MMPDU) then we can't set
1478 * the EOSP bit in the QoS header; add a QoS-nulldata
1479 * frame to the list to send it after the MMPDU.
1480 *
1481 * Note that this code is only in the mac80211-release
1482 * code path, we assume that the driver will not buffer
1483 * anything but QoS-data frames, or if it does, will
1484 * create the QoS-nulldata frame by itself if needed.
1485 *
1486 * Cf. 802.11-2012 10.2.1.10 (c).
1487 */
1488 if (qoshdr) {
1489 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
Johannes Berg47086fc2011-09-29 16:04:33 +02001490
Marco Porsch52a3f202012-03-16 15:30:26 +01001491 info->flags |= IEEE80211_TX_STATUS_EOSP |
1492 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001493 } else {
1494 /* The standard isn't completely clear on this
1495 * as it says the more-data bit should be set
1496 * if there are more BUs. The QoS-Null frame
1497 * we're about to send isn't buffered yet, we
1498 * only create it below, but let's pretend it
1499 * was buffered just in case some clients only
1500 * expect more-data=0 when eosp=1.
1501 */
1502 hdr->frame_control |=
1503 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1504 need_null = true;
1505 num++;
Marco Porsch52a3f202012-03-16 15:30:26 +01001506 }
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001507 break;
Johannes Berg47086fc2011-09-29 16:04:33 +02001508 }
1509
Johannes Berg40b96402011-09-29 16:04:38 +02001510 drv_allow_buffered_frames(local, sta, tids, num,
1511 reason, more_data);
1512
Johannes Berg47086fc2011-09-29 16:04:33 +02001513 ieee80211_add_pending_skbs(local, &pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001514
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001515 if (need_null)
1516 ieee80211_send_null_response(
1517 sdata, sta, find_highest_prio_tid(tids),
1518 reason, false);
1519
Johannes Bergc868cb352011-09-29 16:04:27 +02001520 sta_info_recalc_tim(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001521 } else {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001522 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1523 int tid;
1524
Johannes Bergaf818582009-11-06 11:35:50 +01001525 /*
Johannes Berg4049e092011-09-29 16:04:32 +02001526 * We need to release a frame that is buffered somewhere in the
1527 * driver ... it'll have to handle that.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001528 * Note that the driver also has to check the number of frames
1529 * on the TIDs we're releasing from - if there are more than
1530 * n_frames it has to set the more-data bit (if we didn't ask
1531 * it to set it anyway due to other buffered frames); if there
1532 * are fewer than n_frames it has to make sure to adjust that
1533 * to allow the service period to end properly.
Johannes Bergaf818582009-11-06 11:35:50 +01001534 */
Johannes Berg4049e092011-09-29 16:04:32 +02001535 drv_release_buffered_frames(local, sta, driver_release_tids,
Johannes Berg47086fc2011-09-29 16:04:33 +02001536 n_frames, reason, more_data);
Johannes Berg4049e092011-09-29 16:04:32 +02001537
1538 /*
1539 * Note that we don't recalculate the TIM bit here as it would
1540 * most likely have no effect at all unless the driver told us
Johannes Bergf9f760b2014-01-08 17:45:07 +01001541 * that the TID(s) became empty before returning here from the
Johannes Berg4049e092011-09-29 16:04:32 +02001542 * release function.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001543 * Either way, however, when the driver tells us that the TID(s)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001544 * became empty or we find that a txq became empty, we'll do the
1545 * TIM recalculation.
Johannes Berg4049e092011-09-29 16:04:32 +02001546 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001547
1548 if (!sta->sta.txq[0])
1549 return;
1550
1551 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1552 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1553
1554 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1555 continue;
1556
1557 sta_info_recalc_tim(sta);
1558 break;
1559 }
Johannes Bergaf818582009-11-06 11:35:50 +01001560 }
1561}
1562
Johannes Bergaf818582009-11-06 11:35:50 +01001563void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1564{
Johannes Berg47086fc2011-09-29 16:04:33 +02001565 u8 ignore_for_response = sta->sta.uapsd_queues;
Johannes Bergaf818582009-11-06 11:35:50 +01001566
Johannes Berg47086fc2011-09-29 16:04:33 +02001567 /*
1568 * If all ACs are delivery-enabled then we should reply
1569 * from any of them, if only some are enabled we reply
1570 * only from the non-enabled ones.
1571 */
1572 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1573 ignore_for_response = 0;
1574
1575 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1576 IEEE80211_FRAME_RELEASE_PSPOLL);
1577}
1578
1579void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1580{
1581 int n_frames = sta->sta.max_sp;
1582 u8 delivery_enabled = sta->sta.uapsd_queues;
1583
1584 /*
1585 * If we ever grow support for TSPEC this might happen if
1586 * the TSPEC update from hostapd comes in between a trigger
1587 * frame setting WLAN_STA_UAPSD in the RX path and this
1588 * actually getting called.
1589 */
1590 if (!delivery_enabled)
1591 return;
1592
Johannes Berg47086fc2011-09-29 16:04:33 +02001593 switch (sta->sta.max_sp) {
1594 case 1:
1595 n_frames = 2;
1596 break;
1597 case 2:
1598 n_frames = 4;
1599 break;
1600 case 3:
1601 n_frames = 6;
1602 break;
1603 case 0:
1604 /* XXX: what is a good value? */
Andrei Otcheretianski13a80982014-11-04 11:33:04 +02001605 n_frames = 128;
Johannes Berg47086fc2011-09-29 16:04:33 +02001606 break;
Johannes Bergaf818582009-11-06 11:35:50 +01001607 }
Johannes Bergaf818582009-11-06 11:35:50 +01001608
Johannes Berg47086fc2011-09-29 16:04:33 +02001609 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1610 IEEE80211_FRAME_RELEASE_UAPSD);
Johannes Bergaf818582009-11-06 11:35:50 +01001611}
1612
1613void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1614 struct ieee80211_sta *pubsta, bool block)
1615{
1616 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1617
Johannes Bergb5878a22010-04-07 16:48:40 +02001618 trace_api_sta_block_awake(sta->local, pubsta, block);
1619
Johannes Berg5ac2e352014-05-27 16:32:27 +02001620 if (block) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001621 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001622 ieee80211_clear_fast_xmit(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001623 return;
1624 }
1625
1626 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1627 return;
1628
1629 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1630 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1631 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1632 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1633 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1634 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1635 /* must be asleep in this case */
1636 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1637 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1638 } else {
1639 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001640 ieee80211_check_fast_xmit(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001641 }
Johannes Bergaf818582009-11-06 11:35:50 +01001642}
1643EXPORT_SYMBOL(ieee80211_sta_block_awake);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001644
Johannes Berge9437892013-02-15 21:38:08 +01001645void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
Johannes Berg37fbd902011-09-29 16:04:39 +02001646{
1647 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1648 struct ieee80211_local *local = sta->local;
Johannes Berg37fbd902011-09-29 16:04:39 +02001649
1650 trace_api_eosp(local, pubsta);
1651
Johannes Berge9437892013-02-15 21:38:08 +01001652 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg37fbd902011-09-29 16:04:39 +02001653}
Johannes Berge9437892013-02-15 21:38:08 +01001654EXPORT_SYMBOL(ieee80211_sta_eosp);
Johannes Berg37fbd902011-09-29 16:04:39 +02001655
Johannes Berg042ec452011-09-29 16:04:26 +02001656void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1657 u8 tid, bool buffered)
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001658{
1659 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1660
Johannes Berg5a306f52012-11-14 23:22:21 +01001661 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
Johannes Berg042ec452011-09-29 16:04:26 +02001662 return;
1663
Johannes Berg1b000782013-12-19 10:47:48 +01001664 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1665
Johannes Berg948d8872011-09-29 16:04:29 +02001666 if (buffered)
1667 set_bit(tid, &sta->driver_buffered_tids);
1668 else
1669 clear_bit(tid, &sta->driver_buffered_tids);
1670
Johannes Bergc868cb352011-09-29 16:04:27 +02001671 sta_info_recalc_tim(sta);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001672}
Johannes Berg042ec452011-09-29 16:04:26 +02001673EXPORT_SYMBOL(ieee80211_sta_set_buffered);
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001674
Johannes Berg83d5cc02012-01-12 09:31:10 +01001675int sta_info_move_state(struct sta_info *sta,
1676 enum ieee80211_sta_state new_state)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001677{
Johannes Berg8bf11d82011-12-15 11:17:37 +01001678 might_sleep();
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001679
1680 if (sta->sta_state == new_state)
1681 return 0;
1682
Johannes Bergf09603a2012-01-20 13:55:21 +01001683 /* check allowed transitions first */
1684
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001685 switch (new_state) {
1686 case IEEE80211_STA_NONE:
Johannes Bergf09603a2012-01-20 13:55:21 +01001687 if (sta->sta_state != IEEE80211_STA_AUTH)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001688 return -EINVAL;
1689 break;
1690 case IEEE80211_STA_AUTH:
Johannes Bergf09603a2012-01-20 13:55:21 +01001691 if (sta->sta_state != IEEE80211_STA_NONE &&
1692 sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001693 return -EINVAL;
1694 break;
1695 case IEEE80211_STA_ASSOC:
Johannes Bergf09603a2012-01-20 13:55:21 +01001696 if (sta->sta_state != IEEE80211_STA_AUTH &&
1697 sta->sta_state != IEEE80211_STA_AUTHORIZED)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001698 return -EINVAL;
1699 break;
1700 case IEEE80211_STA_AUTHORIZED:
Johannes Bergf09603a2012-01-20 13:55:21 +01001701 if (sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001702 return -EINVAL;
1703 break;
1704 default:
1705 WARN(1, "invalid state %d", new_state);
1706 return -EINVAL;
1707 }
1708
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001709 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1710 sta->sta.addr, new_state);
Johannes Bergf09603a2012-01-20 13:55:21 +01001711
1712 /*
1713 * notify the driver before the actual changes so it can
1714 * fail the transition
1715 */
1716 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1717 int err = drv_sta_state(sta->local, sta->sdata, sta,
1718 sta->sta_state, new_state);
1719 if (err)
1720 return err;
1721 }
1722
1723 /* reflect the change in all state variables */
1724
1725 switch (new_state) {
1726 case IEEE80211_STA_NONE:
1727 if (sta->sta_state == IEEE80211_STA_AUTH)
1728 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1729 break;
1730 case IEEE80211_STA_AUTH:
1731 if (sta->sta_state == IEEE80211_STA_NONE)
1732 set_bit(WLAN_STA_AUTH, &sta->_flags);
1733 else if (sta->sta_state == IEEE80211_STA_ASSOC)
1734 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1735 break;
1736 case IEEE80211_STA_ASSOC:
1737 if (sta->sta_state == IEEE80211_STA_AUTH) {
1738 set_bit(WLAN_STA_ASSOC, &sta->_flags);
1739 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001740 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1741 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1742 !sta->sdata->u.vlan.sta))
1743 atomic_dec(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001744 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001745 ieee80211_clear_fast_xmit(sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001746 }
1747 break;
1748 case IEEE80211_STA_AUTHORIZED:
1749 if (sta->sta_state == IEEE80211_STA_ASSOC) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001750 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1751 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1752 !sta->sdata->u.vlan.sta))
1753 atomic_inc(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001754 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001755 ieee80211_check_fast_xmit(sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001756 }
1757 break;
1758 default:
1759 break;
1760 }
1761
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001762 sta->sta_state = new_state;
1763
1764 return 0;
1765}
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001766
1767u8 sta_info_tx_streams(struct sta_info *sta)
1768{
1769 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1770 u8 rx_streams;
1771
1772 if (!sta->sta.ht_cap.ht_supported)
1773 return 1;
1774
1775 if (sta->sta.vht_cap.vht_supported) {
1776 int i;
1777 u16 tx_mcs_map =
1778 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1779
1780 for (i = 7; i >= 0; i--)
1781 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1782 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1783 return i + 1;
1784 }
1785
1786 if (ht_cap->mcs.rx_mask[3])
1787 rx_streams = 4;
1788 else if (ht_cap->mcs.rx_mask[2])
1789 rx_streams = 3;
1790 else if (ht_cap->mcs.rx_mask[1])
1791 rx_streams = 2;
1792 else
1793 rx_streams = 1;
1794
1795 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1796 return rx_streams;
1797
1798 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1799 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1800}
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001801
1802void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1803{
1804 struct ieee80211_sub_if_data *sdata = sta->sdata;
1805 struct ieee80211_local *local = sdata->local;
John W. Linville9a244402014-07-25 10:22:36 -04001806 struct rate_control_ref *ref = NULL;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001807 struct timespec uptime;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001808 u32 thr = 0;
1809 int i, ac;
1810
John W. Linville9a244402014-07-25 10:22:36 -04001811 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1812 ref = local->rate_ctrl;
1813
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001814 sinfo->generation = sdata->local->sta_generation;
1815
Johannes Berg225b8182015-01-21 21:09:02 +01001816 /* do before driver, so beacon filtering drivers have a
1817 * chance to e.g. just add the number of filtered beacons
1818 * (or just modify the value entirely, of course)
1819 */
1820 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1821 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1822
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001823 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1824
Johannes Berg319090b2014-11-17 14:08:11 +01001825 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1826 BIT(NL80211_STA_INFO_STA_FLAGS) |
1827 BIT(NL80211_STA_INFO_BSS_PARAM) |
1828 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1829 BIT(NL80211_STA_INFO_RX_DROP_MISC) |
1830 BIT(NL80211_STA_INFO_BEACON_LOSS);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001831
Thomas Gleixner18171522014-06-11 23:59:14 +00001832 ktime_get_ts(&uptime);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001833 sinfo->connected_time = uptime.tv_sec - sta->last_connected;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001834 sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001835
Johannes Berg319090b2014-11-17 14:08:11 +01001836 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1837 BIT(NL80211_STA_INFO_TX_BYTES)))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001838 sinfo->tx_bytes = 0;
1839 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1840 sinfo->tx_bytes += sta->tx_bytes[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01001841 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001842 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001843
Johannes Berg319090b2014-11-17 14:08:11 +01001844 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001845 sinfo->tx_packets = 0;
1846 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1847 sinfo->tx_packets += sta->tx_packets[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01001848 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001849 }
1850
Johannes Berg319090b2014-11-17 14:08:11 +01001851 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1852 BIT(NL80211_STA_INFO_RX_BYTES)))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001853 sinfo->rx_bytes = sta->rx_bytes;
Johannes Berg319090b2014-11-17 14:08:11 +01001854 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001855 }
1856
Johannes Berg319090b2014-11-17 14:08:11 +01001857 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001858 sinfo->rx_packets = sta->rx_packets;
Johannes Berg319090b2014-11-17 14:08:11 +01001859 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001860 }
1861
Johannes Berg319090b2014-11-17 14:08:11 +01001862 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001863 sinfo->tx_retries = sta->tx_retry_count;
Johannes Berg319090b2014-11-17 14:08:11 +01001864 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001865 }
1866
Johannes Berg319090b2014-11-17 14:08:11 +01001867 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001868 sinfo->tx_failed = sta->tx_retry_failed;
Johannes Berg319090b2014-11-17 14:08:11 +01001869 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001870 }
1871
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001872 sinfo->rx_dropped_misc = sta->rx_dropped;
1873 sinfo->beacon_loss_count = sta->beacon_loss_count;
1874
Johannes Berg225b8182015-01-21 21:09:02 +01001875 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1876 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1877 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1878 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1879 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1880 }
1881
Johannes Berg30686bf2015-06-02 21:39:54 +02001882 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
1883 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
Johannes Berg319090b2014-11-17 14:08:11 +01001884 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001885 sinfo->signal = (s8)sta->last_signal;
Johannes Berg319090b2014-11-17 14:08:11 +01001886 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001887 }
1888
Johannes Berg319090b2014-11-17 14:08:11 +01001889 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001890 sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
Johannes Berg319090b2014-11-17 14:08:11 +01001891 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001892 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001893 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001894
1895 if (sta->chains &&
Johannes Berg319090b2014-11-17 14:08:11 +01001896 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1897 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1898 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1899 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001900
1901 sinfo->chains = sta->chains;
1902 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1903 sinfo->chain_signal[i] = sta->chain_signal_last[i];
1904 sinfo->chain_signal_avg[i] =
1905 (s8) -ewma_read(&sta->chain_signal_avg[i]);
1906 }
1907 }
1908
Johannes Berg319090b2014-11-17 14:08:11 +01001909 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001910 sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
Johannes Berg319090b2014-11-17 14:08:11 +01001911 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001912 }
1913
Johannes Berg319090b2014-11-17 14:08:11 +01001914 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001915 sta_set_rate_info_rx(sta, &sinfo->rxrate);
Johannes Berg319090b2014-11-17 14:08:11 +01001916 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001917 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001918
Johannes Berg79c892b2014-11-21 14:26:31 +01001919 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
1920 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
1921 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
1922
1923 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
1924 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
1925 tidstats->rx_msdu = sta->rx_msdu[i];
1926 }
1927
1928 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
1929 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
1930 tidstats->tx_msdu = sta->tx_msdu[i];
1931 }
1932
1933 if (!(tidstats->filled &
1934 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001935 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Berg79c892b2014-11-21 14:26:31 +01001936 tidstats->filled |=
1937 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
1938 tidstats->tx_msdu_retries = sta->tx_msdu_retries[i];
1939 }
1940
1941 if (!(tidstats->filled &
1942 BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001943 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Berg79c892b2014-11-21 14:26:31 +01001944 tidstats->filled |=
1945 BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
1946 tidstats->tx_msdu_failed = sta->tx_msdu_failed[i];
1947 }
1948 }
1949
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001950 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1951#ifdef CONFIG_MAC80211_MESH
Johannes Berg319090b2014-11-17 14:08:11 +01001952 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
1953 BIT(NL80211_STA_INFO_PLID) |
1954 BIT(NL80211_STA_INFO_PLINK_STATE) |
1955 BIT(NL80211_STA_INFO_LOCAL_PM) |
1956 BIT(NL80211_STA_INFO_PEER_PM) |
1957 BIT(NL80211_STA_INFO_NONPEER_PM);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001958
1959 sinfo->llid = sta->llid;
1960 sinfo->plid = sta->plid;
1961 sinfo->plink_state = sta->plink_state;
1962 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
Johannes Berg319090b2014-11-17 14:08:11 +01001963 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001964 sinfo->t_offset = sta->t_offset;
1965 }
1966 sinfo->local_pm = sta->local_pm;
1967 sinfo->peer_pm = sta->peer_pm;
1968 sinfo->nonpeer_pm = sta->nonpeer_pm;
1969#endif
1970 }
1971
1972 sinfo->bss_param.flags = 0;
1973 if (sdata->vif.bss_conf.use_cts_prot)
1974 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1975 if (sdata->vif.bss_conf.use_short_preamble)
1976 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1977 if (sdata->vif.bss_conf.use_short_slot)
1978 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
Emmanuel Grumbach785e21a2014-09-03 15:25:04 +03001979 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001980 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1981
1982 sinfo->sta_flags.set = 0;
1983 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1984 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1985 BIT(NL80211_STA_FLAG_WME) |
1986 BIT(NL80211_STA_FLAG_MFP) |
1987 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1988 BIT(NL80211_STA_FLAG_ASSOCIATED) |
1989 BIT(NL80211_STA_FLAG_TDLS_PEER);
1990 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1991 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
1992 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
1993 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
Johannes Berga74a8c82014-07-22 14:50:47 +02001994 if (sta->sta.wme)
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001995 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
1996 if (test_sta_flag(sta, WLAN_STA_MFP))
1997 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
1998 if (test_sta_flag(sta, WLAN_STA_AUTH))
1999 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2000 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2001 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2002 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2003 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2004
2005 /* check if the driver has a SW RC implementation */
2006 if (ref && ref->ops->get_expected_throughput)
2007 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2008 else
2009 thr = drv_get_expected_throughput(local, &sta->sta);
2010
2011 if (thr != 0) {
Johannes Berg319090b2014-11-17 14:08:11 +01002012 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002013 sinfo->expected_throughput = thr;
2014 }
2015}