blob: 2880f2ae99abe3a05b6421f53340a42e11a9ca30 [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,
74};
75
Johannes Berg4d339602011-12-15 11:24:20 +010076/* Caller must hold local->sta_mtx */
Michael Wube8755e2007-07-27 15:43:23 +020077static int sta_info_hash_del(struct ieee80211_local *local,
78 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070079{
Johannes Berg7bedd0c2015-02-13 21:55:15 +010080 return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
81 sta_rht_params);
Jiri Bencf0706e82007-05-05 11:45:53 -070082}
83
Johannes Berg5108ca82014-02-17 20:49:03 +010084static void __cleanup_single_sta(struct sta_info *sta)
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030085{
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030086 int ac, i;
87 struct tid_ampdu_tx *tid_tx;
88 struct ieee80211_sub_if_data *sdata = sta->sdata;
89 struct ieee80211_local *local = sdata->local;
Marco Porschd012a602012-10-10 12:39:50 -070090 struct ps_data *ps;
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030091
Johannes Berge3685e02014-02-20 11:19:58 +010092 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
Johannes Berg5ac2e352014-05-27 16:32:27 +020093 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
94 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
Marco Porschd012a602012-10-10 12:39:50 -070095 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
96 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
97 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +010098 else if (ieee80211_vif_is_mesh(&sdata->vif))
99 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700100 else
101 return;
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300102
103 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Berge3685e02014-02-20 11:19:58 +0100104 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200105 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300106
Marco Porschd012a602012-10-10 12:39:50 -0700107 atomic_dec(&ps->num_sta_ps);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300108 }
109
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100110 if (sta->sta.txq[0]) {
111 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
112 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
113 int n = skb_queue_len(&txqi->queue);
114
115 ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
116 atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
117 }
118 }
119
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300120 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
121 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
Felix Fietkau1f98ab72012-11-10 03:44:14 +0100122 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
123 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300124 }
125
Thomas Pedersen45b50282013-02-06 10:17:21 -0800126 if (ieee80211_vif_is_mesh(&sdata->vif))
127 mesh_sta_cleanup(sta);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300128
Johannes Berg5ac2e352014-05-27 16:32:27 +0200129 cancel_work_sync(&sta->drv_deliver_wk);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300130
131 /*
132 * Destroy aggregation state here. It would be nice to wait for the
133 * driver to finish aggregation stop and then clean up, but for now
134 * drivers have to handle aggregation stop being requested, followed
135 * directly by station destruction.
136 */
Johannes Berg5a306f52012-11-14 23:22:21 +0100137 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berg661eb382013-06-12 22:47:56 +0200138 kfree(sta->ampdu_mlme.tid_start_tx[i]);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300139 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
140 if (!tid_tx)
141 continue;
Felix Fietkau1f98ab72012-11-10 03:44:14 +0100142 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300143 kfree(tid_tx);
144 }
Johannes Berg5108ca82014-02-17 20:49:03 +0100145}
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300146
Johannes Berg5108ca82014-02-17 20:49:03 +0100147static void cleanup_single_sta(struct sta_info *sta)
148{
149 struct ieee80211_sub_if_data *sdata = sta->sdata;
150 struct ieee80211_local *local = sdata->local;
151
152 __cleanup_single_sta(sta);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300153 sta_info_free(local, sta);
154}
155
Johannes Bergd0709a62008-02-25 16:27:46 +0100156/* protected by RCU */
Johannes Bergabe60632009-11-25 17:46:18 +0100157struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
158 const u8 *addr)
Johannes Berg43ba7e92008-02-21 14:09:30 +0100159{
Johannes Bergabe60632009-11-25 17:46:18 +0100160 struct ieee80211_local *local = sdata->local;
Johannes Berg60f4b622015-04-23 14:02:30 +0200161 struct sta_info *sta;
162 struct rhash_head *tmp;
163 const struct bucket_table *tbl;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100164
Johannes Berg60f4b622015-04-23 14:02:30 +0200165 rcu_read_lock();
166 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
167
168 for_each_sta_info(local, tbl, addr, sta, tmp) {
169 if (sta->sdata == sdata) {
170 rcu_read_unlock();
171 /* this is safe as the caller must already hold
172 * another rcu read section or the mutex
173 */
174 return sta;
175 }
176 }
177 rcu_read_unlock();
178 return NULL;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100179}
180
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100181/*
182 * Get sta info either from the specified interface
183 * or from one of its vlans
184 */
185struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
186 const u8 *addr)
187{
188 struct ieee80211_local *local = sdata->local;
189 struct sta_info *sta;
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100190 struct rhash_head *tmp;
191 const struct bucket_table *tbl;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100192
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100193 rcu_read_lock();
194 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
195
196 for_each_sta_info(local, tbl, addr, sta, tmp) {
197 if (sta->sdata == sdata ||
198 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
199 rcu_read_unlock();
200 /* this is safe as the caller must already hold
201 * another rcu read section or the mutex
202 */
203 return sta;
204 }
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100205 }
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100206 rcu_read_unlock();
207 return NULL;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100208}
209
Johannes Berg3b53fde82009-11-16 12:00:37 +0100210struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
211 int idx)
Luis Carlos Coboee385852008-02-23 15:17:11 +0100212{
Johannes Berg3b53fde82009-11-16 12:00:37 +0100213 struct ieee80211_local *local = sdata->local;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100214 struct sta_info *sta;
215 int i = 0;
216
Johannes Bergd0709a62008-02-25 16:27:46 +0100217 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg3b53fde82009-11-16 12:00:37 +0100218 if (sdata != sta->sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800219 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100220 if (i < idx) {
221 ++i;
222 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100223 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800224 return sta;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100225 }
Luis Carlos Coboee385852008-02-23 15:17:11 +0100226
227 return NULL;
228}
Jiri Bencf0706e82007-05-05 11:45:53 -0700229
Johannes Berg93e5deb2008-04-01 15:21:00 +0200230/**
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100231 * sta_info_free - free STA
Johannes Berg93e5deb2008-04-01 15:21:00 +0200232 *
Randy Dunlap6ef307b2008-07-03 13:52:18 -0700233 * @local: pointer to the global information
Johannes Berg93e5deb2008-04-01 15:21:00 +0200234 * @sta: STA info to free
235 *
236 * This function must undo everything done by sta_info_alloc()
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100237 * that may happen before sta_info_insert(). It may only be
238 * called when sta_info_insert() has not been attempted (and
239 * if that fails, the station is freed anyway.)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200240 */
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100241void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200242{
Johannes Berg889cbb92012-01-17 10:33:29 +0100243 if (sta->rate_ctrl)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100244 rate_control_free_sta(sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200245
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200246 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200247
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100248 if (sta->sta.txq[0])
249 kfree(to_txq_info(sta->sta.txq[0]));
Felix Fietkau53d04522014-05-27 22:33:57 +0200250 kfree(rcu_dereference_raw(sta->sta.rates));
Johannes Berg93e5deb2008-04-01 15:21:00 +0200251 kfree(sta);
252}
253
Johannes Berg4d339602011-12-15 11:24:20 +0100254/* Caller must hold local->sta_mtx */
Johannes Bergd0709a62008-02-25 16:27:46 +0100255static void sta_info_hash_add(struct ieee80211_local *local,
256 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700257{
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100258 rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
259 sta_rht_params);
Jiri Bencf0706e82007-05-05 11:45:53 -0700260}
Jiri Bencf0706e82007-05-05 11:45:53 -0700261
Johannes Berg5ac2e352014-05-27 16:32:27 +0200262static void sta_deliver_ps_frames(struct work_struct *wk)
Johannes Bergaf818582009-11-06 11:35:50 +0100263{
264 struct sta_info *sta;
265
Johannes Berg5ac2e352014-05-27 16:32:27 +0200266 sta = container_of(wk, struct sta_info, drv_deliver_wk);
Johannes Bergaf818582009-11-06 11:35:50 +0100267
268 if (sta->dead)
269 return;
270
Johannes Berg5ac2e352014-05-27 16:32:27 +0200271 local_bh_disable();
272 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
Johannes Bergaf818582009-11-06 11:35:50 +0100273 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200274 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
Johannes Bergaf818582009-11-06 11:35:50 +0100275 ieee80211_sta_ps_deliver_poll_response(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200276 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
Johannes Berg47086fc2011-09-29 16:04:33 +0200277 ieee80211_sta_ps_deliver_uapsd(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200278 local_bh_enable();
Johannes Bergaf818582009-11-06 11:35:50 +0100279}
280
Johannes Bergaf65cd962009-11-17 18:18:36 +0100281static int sta_prepare_rate_control(struct ieee80211_local *local,
282 struct sta_info *sta, gfp_t gfp)
283{
284 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
285 return 0;
286
Johannes Berg889cbb92012-01-17 10:33:29 +0100287 sta->rate_ctrl = local->rate_ctrl;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100288 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
289 &sta->sta, gfp);
Johannes Berg889cbb92012-01-17 10:33:29 +0100290 if (!sta->rate_ctrl_priv)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100291 return -ENOMEM;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100292
293 return 0;
294}
295
Johannes Berg73651ee2008-02-25 16:27:47 +0100296struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
Johannes Berg56544162011-12-14 13:28:46 +0100297 const u8 *addr, gfp_t gfp)
Jiri Bencf0706e82007-05-05 11:45:53 -0700298{
Johannes Bergd0709a62008-02-25 16:27:46 +0100299 struct ieee80211_local *local = sdata->local;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100300 struct ieee80211_hw *hw = &local->hw;
Jiri Bencf0706e82007-05-05 11:45:53 -0700301 struct sta_info *sta;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +0530302 struct timespec uptime;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200303 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700304
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100305 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
Jiri Bencf0706e82007-05-05 11:45:53 -0700306 if (!sta)
Johannes Berg73651ee2008-02-25 16:27:47 +0100307 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700308
Johannes Berg07346f812008-05-03 01:02:02 +0200309 spin_lock_init(&sta->lock);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200310 spin_lock_init(&sta->ps_lock);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200311 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
Johannes Berg67c282c2010-06-10 10:21:43 +0200312 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
Johannes Berga93e3642010-06-10 10:21:46 +0200313 mutex_init(&sta->ampdu_mlme.mtx);
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800314#ifdef CONFIG_MAC80211_MESH
315 if (ieee80211_vif_is_mesh(&sdata->vif) &&
316 !sdata->u.mesh.user_mpm)
317 init_timer(&sta->plink_timer);
Thomas Pedersen6c7c4cb2013-06-20 23:50:59 -0700318 sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800319#endif
Johannes Berg07346f812008-05-03 01:02:02 +0200320
Johannes Berg17741cd2008-09-11 00:02:02 +0200321 memcpy(sta->sta.addr, addr, ETH_ALEN);
Johannes Bergd0709a62008-02-25 16:27:46 +0100322 sta->local = local;
323 sta->sdata = sdata;
Felix Fietkau8bc8aec2011-03-21 20:01:00 +0100324 sta->last_rx = jiffies;
Jiri Bencf0706e82007-05-05 11:45:53 -0700325
Johannes Berg71ec3752012-01-20 13:55:20 +0100326 sta->sta_state = IEEE80211_STA_NONE;
327
Liad Kaufmanb6da9112014-11-19 13:47:38 +0200328 /* Mark TID as unreserved */
329 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
330
Thomas Gleixner18171522014-06-11 23:59:14 +0000331 ktime_get_ts(&uptime);
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +0530332 sta->last_connected = uptime.tv_sec;
Bruno Randolf541a45a2010-12-02 19:12:43 +0900333 ewma_init(&sta->avg_signal, 1024, 8);
Felix Fietkauef0621e2013-04-22 16:29:31 +0200334 for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
335 ewma_init(&sta->chain_signal_avg[i], 1024, 8);
Bruno Randolf541a45a2010-12-02 19:12:43 +0900336
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100337 if (local->ops->wake_tx_queue) {
338 void *txq_data;
339 int size = sizeof(struct txq_info) +
340 ALIGN(hw->txq_data_size, sizeof(void *));
341
342 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
343 if (!txq_data)
344 goto free;
345
346 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
347 struct txq_info *txq = txq_data + i * size;
348
349 ieee80211_init_tx_queue(sdata, sta, txq, i);
350 }
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100351 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700352
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100353 if (sta_prepare_rate_control(local, sta, gfp))
354 goto free_txq;
355
Johannes Berg5a306f52012-11-14 23:22:21 +0100356 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berga622ab72010-06-10 10:21:39 +0200357 /*
358 * timer_to_tid must be initialized with identity mapping
359 * to enable session_timer's data differentiation. See
360 * sta_rx_agg_session_timer_expired for usage.
361 */
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200362 sta->timer_to_tid[i] = i;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200363 }
Johannes Berg948d8872011-09-29 16:04:29 +0200364 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
365 skb_queue_head_init(&sta->ps_tx_buf[i]);
366 skb_queue_head_init(&sta->tx_filtered[i]);
367 }
Johannes Berg73651ee2008-02-25 16:27:47 +0100368
Johannes Berg5a306f52012-11-14 23:22:21 +0100369 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700370 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
Senthil Balasubramaniancccaec92009-05-14 18:42:08 +0530371
Johannes Bergaf0ed692013-02-12 14:21:00 +0100372 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300373 if (sdata->vif.type == NL80211_IFTYPE_AP ||
374 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
375 struct ieee80211_supported_band *sband =
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100376 hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300377 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
378 IEEE80211_HT_CAP_SM_PS_SHIFT;
379 /*
380 * Assume that hostapd advertises our caps in the beacon and
381 * this is the known_smps_mode for a station that just assciated
382 */
383 switch (smps) {
384 case WLAN_HT_SMPS_CONTROL_DISABLED:
385 sta->known_smps_mode = IEEE80211_SMPS_OFF;
386 break;
387 case WLAN_HT_SMPS_CONTROL_STATIC:
388 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
389 break;
390 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
391 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
392 break;
393 default:
394 WARN_ON(1);
395 }
396 }
Johannes Bergaf0ed692013-02-12 14:21:00 +0100397
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200398 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
Johannes Bergef04a292014-01-06 15:56:59 +0100399
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100400 return sta;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100401
402free_txq:
403 if (sta->sta.txq[0])
404 kfree(to_txq_info(sta->sta.txq[0]));
405free:
406 kfree(sta);
407 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +0100408}
409
Guy Eilam8c71df72011-08-17 15:18:14 +0300410static int sta_info_insert_check(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100411{
Johannes Berg34e89502010-02-03 13:59:58 +0100412 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg34e89502010-02-03 13:59:58 +0100413
Johannes Berg03e44972008-02-27 09:56:40 +0100414 /*
415 * Can't be a WARN_ON because it can be triggered through a race:
416 * something inserts a STA (on one CPU) without holding the RTNL
417 * and another CPU turns off the net device.
418 */
Guy Eilam8c71df72011-08-17 15:18:14 +0300419 if (unlikely(!ieee80211_sdata_running(sdata)))
420 return -ENETDOWN;
Johannes Berg03e44972008-02-27 09:56:40 +0100421
Joe Perchesb203ca32012-05-08 18:56:52 +0000422 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
Guy Eilam8c71df72011-08-17 15:18:14 +0300423 is_multicast_ether_addr(sta->sta.addr)))
424 return -EINVAL;
425
426 return 0;
427}
428
Johannes Bergf09603a2012-01-20 13:55:21 +0100429static int sta_info_insert_drv_state(struct ieee80211_local *local,
430 struct ieee80211_sub_if_data *sdata,
431 struct sta_info *sta)
432{
433 enum ieee80211_sta_state state;
434 int err = 0;
435
436 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
437 err = drv_sta_state(local, sdata, sta, state, state + 1);
438 if (err)
439 break;
440 }
441
442 if (!err) {
Johannes Berga4ec45a2012-01-20 13:55:22 +0100443 /*
444 * Drivers using legacy sta_add/sta_remove callbacks only
445 * get uploaded set to true after sta_add is called.
446 */
447 if (!local->ops->sta_add)
448 sta->uploaded = true;
Johannes Bergf09603a2012-01-20 13:55:21 +0100449 return 0;
450 }
451
452 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200453 sdata_info(sdata,
454 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
455 sta->sta.addr, state + 1, err);
Johannes Bergf09603a2012-01-20 13:55:21 +0100456 err = 0;
457 }
458
459 /* unwind on error */
460 for (; state > IEEE80211_STA_NOTEXIST; state--)
461 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
462
463 return err;
464}
465
Guy Eilam8c71df72011-08-17 15:18:14 +0300466/*
467 * should be called with sta_mtx locked
468 * this function replaces the mutex lock
469 * with a RCU lock
470 */
Johannes Berg4d339602011-12-15 11:24:20 +0100471static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
Guy Eilam8c71df72011-08-17 15:18:14 +0300472{
473 struct ieee80211_local *local = sta->local;
474 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg7852e362012-01-20 13:55:24 +0100475 struct station_info sinfo;
Guy Eilam8c71df72011-08-17 15:18:14 +0300476 int err = 0;
477
478 lockdep_assert_held(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +0100479
Johannes Berg7852e362012-01-20 13:55:24 +0100480 /* check if STA exists already */
481 if (sta_info_get_bss(sdata, sta->sta.addr)) {
482 err = -EEXIST;
483 goto out_err;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100484 }
Johannes Berg32bfd352007-12-19 01:31:26 +0100485
Johannes Berg7852e362012-01-20 13:55:24 +0100486 local->num_sta++;
487 local->sta_generation++;
488 smp_mb();
Johannes Berg4d339602011-12-15 11:24:20 +0100489
Johannes Berg5108ca82014-02-17 20:49:03 +0100490 /* simplify things and don't accept BA sessions yet */
491 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
492
Johannes Berg7852e362012-01-20 13:55:24 +0100493 /* make the station visible */
494 sta_info_hash_add(local, sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100495
Arik Nemtsov2bad77482014-10-22 12:32:16 +0300496 list_add_tail_rcu(&sta->list, &local->sta_list);
Johannes Berg83d5cc02012-01-12 09:31:10 +0100497
Johannes Berg5108ca82014-02-17 20:49:03 +0100498 /* notify driver */
499 err = sta_info_insert_drv_state(local, sdata, sta);
500 if (err)
501 goto out_remove;
502
Johannes Berg7852e362012-01-20 13:55:24 +0100503 set_sta_flag(sta, WLAN_STA_INSERTED);
Johannes Berg5108ca82014-02-17 20:49:03 +0100504 /* accept BA sessions now */
505 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg4d339602011-12-15 11:24:20 +0100506
Eliad Peller21f659b2013-11-11 20:14:01 +0200507 ieee80211_recalc_min_chandef(sdata);
Johannes Berg7852e362012-01-20 13:55:24 +0100508 ieee80211_sta_debugfs_add(sta);
509 rate_control_add_sta_debugfs(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100510
Johannes Berg7852e362012-01-20 13:55:24 +0100511 memset(&sinfo, 0, sizeof(sinfo));
512 sinfo.filled = 0;
513 sinfo.generation = local->sta_generation;
514 cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
Johannes Bergd0709a62008-02-25 16:27:46 +0100515
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200516 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700517
Johannes Berg34e89502010-02-03 13:59:58 +0100518 /* move reference to rcu-protected */
519 rcu_read_lock();
520 mutex_unlock(&local->sta_mtx);
Jiri Bence9f207f2007-05-05 11:46:38 -0700521
Johannes Berg73651ee2008-02-25 16:27:47 +0100522 if (ieee80211_vif_is_mesh(&sdata->vif))
523 mesh_accept_plinks_update(sdata);
524
525 return 0;
Johannes Berg5108ca82014-02-17 20:49:03 +0100526 out_remove:
527 sta_info_hash_del(local, sta);
528 list_del_rcu(&sta->list);
529 local->num_sta--;
530 synchronize_net();
531 __cleanup_single_sta(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100532 out_err:
533 mutex_unlock(&local->sta_mtx);
534 rcu_read_lock();
535 return err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300536}
537
538int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
539{
540 struct ieee80211_local *local = sta->local;
Zhao, Gang308f7fc2014-04-21 12:53:00 +0800541 int err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300542
Johannes Berg4d339602011-12-15 11:24:20 +0100543 might_sleep();
544
Guy Eilam8c71df72011-08-17 15:18:14 +0300545 err = sta_info_insert_check(sta);
546 if (err) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700547 rcu_read_lock();
548 goto out_free;
549 }
550
Johannes Berg004c8722008-02-20 11:21:35 +0100551 mutex_lock(&local->sta_mtx);
552
Johannes Berg4d339602011-12-15 11:24:20 +0100553 err = sta_info_insert_finish(sta);
Guy Eilam8c71df72011-08-17 15:18:14 +0300554 if (err)
Johannes Berg004c8722008-02-20 11:21:35 +0100555 goto out_free;
Johannes Bergd0709a62008-02-25 16:27:46 +0100556
Johannes Berg004c8722008-02-20 11:21:35 +0100557 return 0;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200558 out_free:
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100559 sta_info_free(local, sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200560 return err;
Jiri Bencf0706e82007-05-05 11:45:53 -0700561}
562
Johannes Berg34e89502010-02-03 13:59:58 +0100563int sta_info_insert(struct sta_info *sta)
564{
565 int err = sta_info_insert_rcu(sta);
566
567 rcu_read_unlock();
568
569 return err;
570}
571
Marco Porschd012a602012-10-10 12:39:50 -0700572static inline void __bss_tim_set(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100573{
574 /*
575 * This format has been mandated by the IEEE specifications,
576 * so this line may not be changed to use the __set_bit() format.
577 */
Marco Porschd012a602012-10-10 12:39:50 -0700578 tim[id / 8] |= (1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100579}
580
Marco Porschd012a602012-10-10 12:39:50 -0700581static inline void __bss_tim_clear(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100582{
583 /*
584 * This format has been mandated by the IEEE specifications,
585 * so this line may not be changed to use the __clear_bit() format.
586 */
Marco Porschd012a602012-10-10 12:39:50 -0700587 tim[id / 8] &= ~(1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100588}
589
Ilan Peer3d5839b2013-03-05 15:27:20 +0200590static inline bool __bss_tim_get(u8 *tim, u16 id)
591{
592 /*
593 * This format has been mandated by the IEEE specifications,
594 * so this line may not be changed to use the test_bit() format.
595 */
596 return tim[id / 8] & (1 << (id % 8));
597}
598
Johannes Berg948d8872011-09-29 16:04:29 +0200599static unsigned long ieee80211_tids_for_ac(int ac)
Johannes Berg004c8722008-02-20 11:21:35 +0100600{
Johannes Berg948d8872011-09-29 16:04:29 +0200601 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
602 switch (ac) {
603 case IEEE80211_AC_VO:
604 return BIT(6) | BIT(7);
605 case IEEE80211_AC_VI:
606 return BIT(4) | BIT(5);
607 case IEEE80211_AC_BE:
608 return BIT(0) | BIT(3);
609 case IEEE80211_AC_BK:
610 return BIT(1) | BIT(2);
611 default:
612 WARN_ON(1);
613 return 0;
Johannes Bergd0709a62008-02-25 16:27:46 +0100614 }
Johannes Berg004c8722008-02-20 11:21:35 +0100615}
616
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100617static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
Johannes Berg004c8722008-02-20 11:21:35 +0100618{
Johannes Bergc868cb352011-09-29 16:04:27 +0200619 struct ieee80211_local *local = sta->local;
Marco Porschd012a602012-10-10 12:39:50 -0700620 struct ps_data *ps;
Johannes Berg948d8872011-09-29 16:04:29 +0200621 bool indicate_tim = false;
622 u8 ignore_for_tim = sta->sta.uapsd_queues;
623 int ac;
Marco Porschd012a602012-10-10 12:39:50 -0700624 u16 id;
Johannes Berg004c8722008-02-20 11:21:35 +0100625
Marco Porschd012a602012-10-10 12:39:50 -0700626 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
627 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
628 if (WARN_ON_ONCE(!sta->sdata->bss))
629 return;
630
631 ps = &sta->sdata->bss->ps;
632 id = sta->sta.aid;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100633#ifdef CONFIG_MAC80211_MESH
634 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
635 ps = &sta->sdata->u.mesh.ps;
Thomas Pedersen204d1302013-11-05 11:17:05 -0800636 /* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800637 id = sta->plid % (IEEE80211_MAX_AID + 1);
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100638#endif
Marco Porschd012a602012-10-10 12:39:50 -0700639 } else {
Johannes Bergc868cb352011-09-29 16:04:27 +0200640 return;
Marco Porschd012a602012-10-10 12:39:50 -0700641 }
Johannes Berg3e122be2008-07-09 14:40:34 +0200642
Johannes Bergc868cb352011-09-29 16:04:27 +0200643 /* No need to do anything if the driver does all */
644 if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
645 return;
Johannes Berg004c8722008-02-20 11:21:35 +0100646
Johannes Bergc868cb352011-09-29 16:04:27 +0200647 if (sta->dead)
648 goto done;
Johannes Berg3e122be2008-07-09 14:40:34 +0200649
Johannes Berg948d8872011-09-29 16:04:29 +0200650 /*
651 * If all ACs are delivery-enabled then we should build
652 * the TIM bit for all ACs anyway; if only some are then
653 * we ignore those and build the TIM bit using only the
654 * non-enabled ones.
655 */
656 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
657 ignore_for_tim = 0;
Johannes Berg3e122be2008-07-09 14:40:34 +0200658
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100659 if (ignore_pending)
660 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
661
Johannes Berg948d8872011-09-29 16:04:29 +0200662 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
663 unsigned long tids;
664
665 if (ignore_for_tim & BIT(ac))
666 continue;
667
668 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
669 !skb_queue_empty(&sta->ps_tx_buf[ac]);
670 if (indicate_tim)
671 break;
672
673 tids = ieee80211_tids_for_ac(ac);
674
675 indicate_tim |=
676 sta->driver_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100677 indicate_tim |=
678 sta->txq_buffered_tids & tids;
Johannes Bergd0709a62008-02-25 16:27:46 +0100679 }
Johannes Berg004c8722008-02-20 11:21:35 +0100680
Johannes Bergc868cb352011-09-29 16:04:27 +0200681 done:
Johannes Berg65f704a2013-02-13 17:39:53 +0100682 spin_lock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100683
Ilan Peer3d5839b2013-03-05 15:27:20 +0200684 if (indicate_tim == __bss_tim_get(ps->tim, id))
685 goto out_unlock;
686
Johannes Berg948d8872011-09-29 16:04:29 +0200687 if (indicate_tim)
Marco Porschd012a602012-10-10 12:39:50 -0700688 __bss_tim_set(ps->tim, id);
Johannes Bergc868cb352011-09-29 16:04:27 +0200689 else
Marco Porschd012a602012-10-10 12:39:50 -0700690 __bss_tim_clear(ps->tim, id);
Johannes Berg3e122be2008-07-09 14:40:34 +0200691
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100692 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
Johannes Bergc868cb352011-09-29 16:04:27 +0200693 local->tim_in_locked_section = true;
Johannes Berg948d8872011-09-29 16:04:29 +0200694 drv_set_tim(local, &sta->sta, indicate_tim);
Johannes Bergc868cb352011-09-29 16:04:27 +0200695 local->tim_in_locked_section = false;
Johannes Berg004c8722008-02-20 11:21:35 +0100696 }
Johannes Berg004c8722008-02-20 11:21:35 +0100697
Ilan Peer3d5839b2013-03-05 15:27:20 +0200698out_unlock:
Johannes Berg65f704a2013-02-13 17:39:53 +0100699 spin_unlock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100700}
701
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100702void sta_info_recalc_tim(struct sta_info *sta)
703{
704 __sta_info_recalc_tim(sta, false);
705}
706
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200707static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700708{
Johannes Berge039fa42008-05-15 12:55:29 +0200709 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700710 int timeout;
711
712 if (!skb)
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200713 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700714
Johannes Berge039fa42008-05-15 12:55:29 +0200715 info = IEEE80211_SKB_CB(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700716
717 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200718 timeout = (sta->listen_interval *
719 sta->sdata->vif.bss_conf.beacon_int *
720 32 / 15625) * HZ;
Jiri Bencf0706e82007-05-05 11:45:53 -0700721 if (timeout < STA_TX_BUFFER_EXPIRE)
722 timeout = STA_TX_BUFFER_EXPIRE;
Johannes Berge039fa42008-05-15 12:55:29 +0200723 return time_after(jiffies, info->control.jiffies + timeout);
Jiri Bencf0706e82007-05-05 11:45:53 -0700724}
725
726
Johannes Berg948d8872011-09-29 16:04:29 +0200727static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
728 struct sta_info *sta, int ac)
Jiri Bencf0706e82007-05-05 11:45:53 -0700729{
730 unsigned long flags;
731 struct sk_buff *skb;
732
Johannes Berg60750392011-09-29 16:04:28 +0200733 /*
734 * First check for frames that should expire on the filtered
735 * queue. Frames here were rejected by the driver and are on
736 * a separate queue to avoid reordering with normal PS-buffered
737 * frames. They also aren't accounted for right now in the
738 * total_ps_buffered counter.
739 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700740 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200741 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
742 skb = skb_peek(&sta->tx_filtered[ac]);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200743 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200744 skb = __skb_dequeue(&sta->tx_filtered[ac]);
Johannes Berg836341a2008-02-20 02:07:21 +0100745 else
Jiri Bencf0706e82007-05-05 11:45:53 -0700746 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200747 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700748
Johannes Berg60750392011-09-29 16:04:28 +0200749 /*
750 * Frames are queued in order, so if this one
751 * hasn't expired yet we can stop testing. If
752 * we actually reached the end of the queue we
753 * also need to stop, of course.
754 */
755 if (!skb)
756 break;
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200757 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg60750392011-09-29 16:04:28 +0200758 }
759
760 /*
761 * Now also check the normal PS-buffered queue, this will
762 * only find something if the filtered queue was emptied
763 * since the filtered frames are all before the normal PS
764 * buffered frames.
765 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700766 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200767 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
768 skb = skb_peek(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700769 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200770 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700771 else
772 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200773 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700774
Johannes Berg60750392011-09-29 16:04:28 +0200775 /*
776 * frames are queued in order, so if this one
777 * hasn't expired yet (or we reached the end of
778 * the queue) we can stop testing
779 */
Johannes Berg836341a2008-02-20 02:07:21 +0100780 if (!skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700781 break;
Johannes Berg836341a2008-02-20 02:07:21 +0100782
Johannes Berg836341a2008-02-20 02:07:21 +0100783 local->total_ps_buffered--;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200784 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
785 sta->sta.addr);
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200786 ieee80211_free_txskb(&local->hw, skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700787 }
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300788
Johannes Berg60750392011-09-29 16:04:28 +0200789 /*
790 * Finally, recalculate the TIM bit for this station -- it might
791 * now be clear because the station was too slow to retrieve its
792 * frames.
793 */
794 sta_info_recalc_tim(sta);
795
796 /*
797 * Return whether there are any frames still buffered, this is
798 * used to check whether the cleanup timer still needs to run,
799 * if there are no frames we don't need to rearm the timer.
800 */
Johannes Berg948d8872011-09-29 16:04:29 +0200801 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
802 skb_queue_empty(&sta->tx_filtered[ac]));
803}
804
805static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
806 struct sta_info *sta)
807{
808 bool have_buffered = false;
809 int ac;
810
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100811 /* This is only necessary for stations on BSS/MBSS interfaces */
812 if (!sta->sdata->bss &&
813 !ieee80211_vif_is_mesh(&sta->sdata->vif))
Johannes Berg948d8872011-09-29 16:04:29 +0200814 return false;
815
816 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
817 have_buffered |=
818 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
819
820 return have_buffered;
Jiri Bencf0706e82007-05-05 11:45:53 -0700821}
822
Johannes Bergd7782072013-12-04 23:12:31 +0100823static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100824{
825 struct ieee80211_local *local;
826 struct ieee80211_sub_if_data *sdata;
Johannes Berg6d10e462013-03-06 23:09:11 +0100827 int ret;
Johannes Berg34e89502010-02-03 13:59:58 +0100828
829 might_sleep();
830
831 if (!sta)
832 return -ENOENT;
833
834 local = sta->local;
835 sdata = sta->sdata;
836
Johannes Berg83d5cc02012-01-12 09:31:10 +0100837 lockdep_assert_held(&local->sta_mtx);
838
Johannes Berg098a6072010-04-06 11:18:47 +0200839 /*
840 * Before removing the station from the driver and
841 * rate control, it might still start new aggregation
842 * sessions -- block that to make sure the tear-down
843 * will be sufficient.
844 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200845 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Bergc82c4a82012-07-18 13:31:31 +0200846 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
Johannes Berg098a6072010-04-06 11:18:47 +0200847
Johannes Berg34e89502010-02-03 13:59:58 +0100848 ret = sta_info_hash_del(local, sta);
Johannes Bergb01711b2013-12-04 20:25:27 +0100849 if (WARN_ON(ret))
Johannes Berg34e89502010-02-03 13:59:58 +0100850 return ret;
851
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200852 /*
853 * for TDLS peers, make sure to return to the base channel before
854 * removal.
855 */
856 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
857 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
858 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
859 }
860
Arik Nemtsov794454c2012-06-03 23:32:32 +0300861 list_del_rcu(&sta->list);
Johannes Berg4d339602011-12-15 11:24:20 +0100862
Johannes Berg6a9d1b92013-12-04 22:39:17 +0100863 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
864
Johannes Berga710c812013-12-04 20:11:06 +0100865 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
866 rcu_access_pointer(sdata->u.vlan.sta) == sta)
867 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
868
Johannes Bergd7782072013-12-04 23:12:31 +0100869 return 0;
870}
871
872static void __sta_info_destroy_part2(struct sta_info *sta)
873{
874 struct ieee80211_local *local = sta->local;
875 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg6f7a8d22014-11-14 20:32:57 +0100876 struct station_info sinfo = {};
Johannes Bergd7782072013-12-04 23:12:31 +0100877 int ret;
878
879 /*
880 * NOTE: This assumes at least synchronize_net() was done
881 * after _part1 and before _part2!
882 */
883
884 might_sleep();
885 lockdep_assert_held(&local->sta_mtx);
886
Johannes Bergc8782072013-12-04 23:05:45 +0100887 /* now keys can no longer be reached */
Johannes Berg6d10e462013-03-06 23:09:11 +0100888 ieee80211_free_sta_keys(local, sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100889
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100890 /* disable TIM bit - last chance to tell driver */
891 __sta_info_recalc_tim(sta, true);
892
Johannes Berg34e89502010-02-03 13:59:58 +0100893 sta->dead = true;
894
Johannes Berg34e89502010-02-03 13:59:58 +0100895 local->num_sta--;
896 local->sta_generation++;
897
Johannes Berg83d5cc02012-01-12 09:31:10 +0100898 while (sta->sta_state > IEEE80211_STA_NONE) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100899 ret = sta_info_move_state(sta, sta->sta_state - 1);
900 if (ret) {
Johannes Berg83d5cc02012-01-12 09:31:10 +0100901 WARN_ON_ONCE(1);
902 break;
903 }
904 }
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100905
Johannes Bergf09603a2012-01-20 13:55:21 +0100906 if (sta->uploaded) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100907 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
908 IEEE80211_STA_NOTEXIST);
909 WARN_ON_ONCE(ret != 0);
910 }
Johannes Berg34e89502010-02-03 13:59:58 +0100911
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200912 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
913
Johannes Berg6f7a8d22014-11-14 20:32:57 +0100914 sta_set_sinfo(sta, &sinfo);
915 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
Jouni Malinenec15e682011-03-23 15:29:52 +0200916
Johannes Berg34e89502010-02-03 13:59:58 +0100917 rate_control_remove_sta_debugfs(sta);
918 ieee80211_sta_debugfs_remove(sta);
Eliad Peller21f659b2013-11-11 20:14:01 +0200919 ieee80211_recalc_min_chandef(sdata);
Johannes Berg34e89502010-02-03 13:59:58 +0100920
Johannes Bergd34ba212013-12-04 22:46:11 +0100921 cleanup_single_sta(sta);
Johannes Bergd7782072013-12-04 23:12:31 +0100922}
923
924int __must_check __sta_info_destroy(struct sta_info *sta)
925{
926 int err = __sta_info_destroy_part1(sta);
927
928 if (err)
929 return err;
930
931 synchronize_net();
932
933 __sta_info_destroy_part2(sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100934
935 return 0;
936}
937
938int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
939{
940 struct sta_info *sta;
941 int ret;
942
943 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100944 sta = sta_info_get(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100945 ret = __sta_info_destroy(sta);
946 mutex_unlock(&sdata->local->sta_mtx);
947
948 return ret;
949}
950
951int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
952 const u8 *addr)
953{
954 struct sta_info *sta;
955 int ret;
956
957 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100958 sta = sta_info_get_bss(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100959 ret = __sta_info_destroy(sta);
960 mutex_unlock(&sdata->local->sta_mtx);
961
962 return ret;
963}
Jiri Bencf0706e82007-05-05 11:45:53 -0700964
965static void sta_info_cleanup(unsigned long data)
966{
967 struct ieee80211_local *local = (struct ieee80211_local *) data;
968 struct sta_info *sta;
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300969 bool timer_needed = false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700970
Johannes Bergd0709a62008-02-25 16:27:46 +0100971 rcu_read_lock();
972 list_for_each_entry_rcu(sta, &local->sta_list, list)
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300973 if (sta_info_cleanup_expire_buffered(local, sta))
974 timer_needed = true;
Johannes Bergd0709a62008-02-25 16:27:46 +0100975 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -0700976
Johannes Berg5bb644a2009-05-17 11:40:42 +0200977 if (local->quiescing)
978 return;
979
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300980 if (!timer_needed)
981 return;
982
Johannes Berg26d59532011-04-01 13:52:48 +0200983 mod_timer(&local->sta_cleanup,
984 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
Jiri Bencf0706e82007-05-05 11:45:53 -0700985}
986
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100987u32 sta_addr_hash(const void *key, u32 length, u32 seed)
Jiri Bencf0706e82007-05-05 11:45:53 -0700988{
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100989 return jhash(key, ETH_ALEN, seed);
990}
991
992int sta_info_init(struct ieee80211_local *local)
993{
994 int err;
995
996 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
997 if (err)
998 return err;
999
Johannes Berg4d339602011-12-15 11:24:20 +01001000 spin_lock_init(&local->tim_lock);
Johannes Berg34e89502010-02-03 13:59:58 +01001001 mutex_init(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07001002 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001003
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001004 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1005 (unsigned long)local);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001006 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001007}
1008
1009void sta_info_stop(struct ieee80211_local *local)
1010{
Johannes Berga56f9922012-12-13 23:08:52 +01001011 del_timer_sync(&local->sta_cleanup);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001012 rhashtable_destroy(&local->sta_hash);
Jiri Bencf0706e82007-05-05 11:45:53 -07001013}
1014
Johannes Berg051007d2012-12-13 23:49:02 +01001015
Johannes Berge7162512013-12-04 23:18:37 +01001016int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
Jiri Bencf0706e82007-05-05 11:45:53 -07001017{
Johannes Bergb998e8b2012-12-13 23:07:46 +01001018 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001019 struct sta_info *sta, *tmp;
Johannes Bergd7782072013-12-04 23:12:31 +01001020 LIST_HEAD(free_list);
Johannes Berg44213b52008-02-25 16:27:49 +01001021 int ret = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001022
Johannes Bergd0709a62008-02-25 16:27:46 +01001023 might_sleep();
1024
Johannes Berge7162512013-12-04 23:18:37 +01001025 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1026 WARN_ON(vlans && !sdata->bss);
1027
Johannes Berg34e89502010-02-03 13:59:58 +01001028 mutex_lock(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +01001029 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Johannes Berge7162512013-12-04 23:18:37 +01001030 if (sdata == sta->sdata ||
1031 (vlans && sdata->bss == sta->sdata->bss)) {
Johannes Bergd7782072013-12-04 23:12:31 +01001032 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1033 list_add(&sta->free_list, &free_list);
Johannes Berg34316832012-02-25 21:40:46 +01001034 ret++;
1035 }
Johannes Berg34e89502010-02-03 13:59:58 +01001036 }
Johannes Bergd7782072013-12-04 23:12:31 +01001037
1038 if (!list_empty(&free_list)) {
1039 synchronize_net();
1040 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1041 __sta_info_destroy_part2(sta);
1042 }
Johannes Berg34e89502010-02-03 13:59:58 +01001043 mutex_unlock(&local->sta_mtx);
Johannes Berg44213b52008-02-25 16:27:49 +01001044
Johannes Berg051007d2012-12-13 23:49:02 +01001045 return ret;
1046}
1047
Johannes Berg24723d12008-09-11 00:01:46 +02001048void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1049 unsigned long exp_time)
1050{
1051 struct ieee80211_local *local = sdata->local;
1052 struct sta_info *sta, *tmp;
Johannes Berg24723d12008-09-11 00:01:46 +02001053
Johannes Berg34e89502010-02-03 13:59:58 +01001054 mutex_lock(&local->sta_mtx);
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301055
1056 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Marek Lindnerec2b7742011-12-20 23:16:52 +08001057 if (sdata != sta->sdata)
1058 continue;
1059
Johannes Berg24723d12008-09-11 00:01:46 +02001060 if (time_after(jiffies, sta->last_rx + exp_time)) {
Mohammed Shafi Shajakhaneea57d42012-10-08 21:33:47 +05301061 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1062 sta->sta.addr);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001063
1064 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1065 test_sta_flag(sta, WLAN_STA_PS_STA))
1066 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1067
Johannes Berg34e89502010-02-03 13:59:58 +01001068 WARN_ON(__sta_info_destroy(sta));
Johannes Berg24723d12008-09-11 00:01:46 +02001069 }
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301070 }
1071
Johannes Berg34e89502010-02-03 13:59:58 +01001072 mutex_unlock(&local->sta_mtx);
Johannes Berg24723d12008-09-11 00:01:46 +02001073}
Johannes Berg17741cd2008-09-11 00:02:02 +02001074
Ben Greear686b9cb2010-09-23 09:44:36 -07001075struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001076 const u8 *addr,
1077 const u8 *localaddr)
Johannes Berg17741cd2008-09-11 00:02:02 +02001078{
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001079 struct ieee80211_local *local = hw_to_local(hw);
1080 struct sta_info *sta;
1081 struct rhash_head *tmp;
1082 const struct bucket_table *tbl;
1083
1084 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
Johannes Berg17741cd2008-09-11 00:02:02 +02001085
Ben Greear686b9cb2010-09-23 09:44:36 -07001086 /*
1087 * Just return a random station if localaddr is NULL
1088 * ... first in list.
1089 */
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001090 for_each_sta_info(local, tbl, addr, sta, tmp) {
Ben Greear686b9cb2010-09-23 09:44:36 -07001091 if (localaddr &&
Joe Perchesb203ca32012-05-08 18:56:52 +00001092 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
Ben Greear686b9cb2010-09-23 09:44:36 -07001093 continue;
Johannes Bergf7c65592010-04-30 13:48:36 +02001094 if (!sta->uploaded)
1095 return NULL;
Johannes Bergabe60632009-11-25 17:46:18 +01001096 return &sta->sta;
Johannes Bergf7c65592010-04-30 13:48:36 +02001097 }
1098
Johannes Bergabe60632009-11-25 17:46:18 +01001099 return NULL;
Johannes Berg17741cd2008-09-11 00:02:02 +02001100}
Ben Greear686b9cb2010-09-23 09:44:36 -07001101EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
Johannes Berg5ed176e2009-11-04 14:42:28 +01001102
1103struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1104 const u8 *addr)
1105{
Johannes Bergf7c65592010-04-30 13:48:36 +02001106 struct sta_info *sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001107
1108 if (!vif)
1109 return NULL;
1110
Johannes Bergf7c65592010-04-30 13:48:36 +02001111 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1112 if (!sta)
1113 return NULL;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001114
Johannes Bergf7c65592010-04-30 13:48:36 +02001115 if (!sta->uploaded)
1116 return NULL;
1117
1118 return &sta->sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001119}
Johannes Berg17741cd2008-09-11 00:02:02 +02001120EXPORT_SYMBOL(ieee80211_find_sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001121
Johannes Berge3685e02014-02-20 11:19:58 +01001122/* powersave support code */
1123void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
Johannes Berg50a94322010-11-16 11:50:28 -08001124{
Helmut Schaa608383b2012-01-30 15:18:00 +01001125 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berge3685e02014-02-20 11:19:58 +01001126 struct ieee80211_local *local = sdata->local;
1127 struct sk_buff_head pending;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001128 int filtered = 0, buffered = 0, ac, i;
Johannes Berge3685e02014-02-20 11:19:58 +01001129 unsigned long flags;
Marco Porschd012a602012-10-10 12:39:50 -07001130 struct ps_data *ps;
1131
Felix Fietkau3918edb2014-07-25 16:20:23 +02001132 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1133 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1134 u.ap);
1135
1136 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porschd012a602012-10-10 12:39:50 -07001137 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001138 else if (ieee80211_vif_is_mesh(&sdata->vif))
1139 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07001140 else
1141 return;
Johannes Berg50a94322010-11-16 11:50:28 -08001142
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001143 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg47086fc2011-09-29 16:04:33 +02001144
Johannes Berg5a306f52012-11-14 23:22:21 +01001145 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
Johannes Berg948d8872011-09-29 16:04:29 +02001146 sta->driver_buffered_tids = 0;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001147 sta->txq_buffered_tids = 0;
Johannes Berg948d8872011-09-29 16:04:29 +02001148
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001149 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1150 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001151
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001152 if (sta->sta.txq[0]) {
1153 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1154 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1155
1156 if (!skb_queue_len(&txqi->queue))
1157 continue;
1158
1159 drv_wake_tx_queue(local, txqi);
1160 }
1161 }
1162
Johannes Berg948d8872011-09-29 16:04:29 +02001163 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001164
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001165 /* sync with ieee80211_tx_h_unicast_ps_buf */
1166 spin_lock(&sta->ps_lock);
Johannes Bergaf818582009-11-06 11:35:50 +01001167 /* Send all buffered frames to the station */
Johannes Berg948d8872011-09-29 16:04:29 +02001168 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1169 int count = skb_queue_len(&pending), tmp;
1170
Arik Nemtsov987c2852012-11-05 10:27:52 +02001171 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001172 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001173 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001174 tmp = skb_queue_len(&pending);
1175 filtered += tmp - count;
1176 count = tmp;
1177
Arik Nemtsov987c2852012-11-05 10:27:52 +02001178 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001179 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001180 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001181 tmp = skb_queue_len(&pending);
1182 buffered += tmp - count;
1183 }
1184
Johannes Berge3685e02014-02-20 11:19:58 +01001185 ieee80211_add_pending_skbs(local, &pending);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001186
1187 /* now we're no longer in the deliver code */
1188 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1189
1190 /* The station might have polled and then woken up before we responded,
1191 * so clear these flags now to avoid them sticking around.
1192 */
1193 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1194 clear_sta_flag(sta, WLAN_STA_UAPSD);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001195 spin_unlock(&sta->ps_lock);
Johannes Berg948d8872011-09-29 16:04:29 +02001196
Johannes Berge3685e02014-02-20 11:19:58 +01001197 atomic_dec(&ps->num_sta_ps);
1198
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001199 /* This station just woke up and isn't aware of our SMPS state */
Chun-Yeow Yeoh062f1d62014-04-22 18:19:25 +08001200 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1201 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001202 sdata->smps_mode) &&
1203 sta->known_smps_mode != sdata->bss->req_smps &&
1204 sta_info_tx_streams(sta) != 1) {
1205 ht_dbg(sdata,
1206 "%pM just woke up and MIMO capable - update SMPS\n",
1207 sta->sta.addr);
1208 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1209 sta->sta.addr,
1210 sdata->vif.bss_conf.bssid);
1211 }
1212
Johannes Bergaf818582009-11-06 11:35:50 +01001213 local->total_ps_buffered -= buffered;
1214
Johannes Bergc868cb352011-09-29 16:04:27 +02001215 sta_info_recalc_tim(sta);
1216
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001217 ps_dbg(sdata,
1218 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1219 sta->sta.addr, sta->sta.aid, filtered, buffered);
Johannes Bergaf818582009-11-06 11:35:50 +01001220}
1221
Johannes Bergce662b442011-09-29 16:04:34 +02001222static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1223 struct sta_info *sta, int tid,
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001224 enum ieee80211_frame_release_type reason,
1225 bool call_driver)
Johannes Bergce662b442011-09-29 16:04:34 +02001226{
1227 struct ieee80211_local *local = sdata->local;
1228 struct ieee80211_qos_hdr *nullfunc;
1229 struct sk_buff *skb;
1230 int size = sizeof(*nullfunc);
1231 __le16 fc;
Johannes Berga74a8c82014-07-22 14:50:47 +02001232 bool qos = sta->sta.wme;
Johannes Bergce662b442011-09-29 16:04:34 +02001233 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02001234 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergce662b442011-09-29 16:04:34 +02001235
1236 if (qos) {
1237 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1238 IEEE80211_STYPE_QOS_NULLFUNC |
1239 IEEE80211_FCTL_FROMDS);
1240 } else {
1241 size -= 2;
1242 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1243 IEEE80211_STYPE_NULLFUNC |
1244 IEEE80211_FCTL_FROMDS);
1245 }
1246
1247 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1248 if (!skb)
1249 return;
1250
1251 skb_reserve(skb, local->hw.extra_tx_headroom);
1252
1253 nullfunc = (void *) skb_put(skb, size);
1254 nullfunc->frame_control = fc;
1255 nullfunc->duration_id = 0;
1256 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1257 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1258 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
Johannes Berg864a6042014-03-04 13:46:53 +01001259 nullfunc->seq_ctrl = 0;
Johannes Bergce662b442011-09-29 16:04:34 +02001260
Johannes Berg59b66252011-10-13 13:19:19 +02001261 skb->priority = tid;
1262 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
Johannes Bergce662b442011-09-29 16:04:34 +02001263 if (qos) {
Johannes Bergce662b442011-09-29 16:04:34 +02001264 nullfunc->qos_ctrl = cpu_to_le16(tid);
1265
Johannes Berg40b96402011-09-29 16:04:38 +02001266 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
Johannes Bergce662b442011-09-29 16:04:34 +02001267 nullfunc->qos_ctrl |=
1268 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1269 }
1270
1271 info = IEEE80211_SKB_CB(skb);
1272
1273 /*
1274 * Tell TX path to send this frame even though the
1275 * STA may still remain is PS mode after this frame
Johannes Bergdeeaee12011-09-29 16:04:35 +02001276 * exchange. Also set EOSP to indicate this packet
1277 * ends the poll/service period.
Johannes Bergce662b442011-09-29 16:04:34 +02001278 */
Johannes Berg02f2f1a2012-02-27 12:18:30 +01001279 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
Johannes Bergdeeaee12011-09-29 16:04:35 +02001280 IEEE80211_TX_STATUS_EOSP |
1281 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergce662b442011-09-29 16:04:34 +02001282
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301283 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1284
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001285 if (call_driver)
1286 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1287 reason, false);
Johannes Berg40b96402011-09-29 16:04:38 +02001288
Johannes Berg89afe612013-02-13 15:39:57 +01001289 skb->dev = sdata->dev;
1290
Johannes Berg55de9082012-07-26 17:24:39 +02001291 rcu_read_lock();
1292 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1293 if (WARN_ON(!chanctx_conf)) {
1294 rcu_read_unlock();
1295 kfree_skb(skb);
1296 return;
1297 }
1298
Johannes Berg73c4e192014-11-09 18:50:09 +02001299 info->band = chanctx_conf->def.chan->band;
Johannes Berg7c107702015-03-20 14:18:27 +01001300 ieee80211_xmit(sdata, sta, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001301 rcu_read_unlock();
Johannes Bergce662b442011-09-29 16:04:34 +02001302}
1303
Johannes Berg0a1cb802014-01-09 11:05:31 +01001304static int find_highest_prio_tid(unsigned long tids)
1305{
1306 /* lower 3 TIDs aren't ordered perfectly */
1307 if (tids & 0xF8)
1308 return fls(tids) - 1;
1309 /* TID 0 is BE just like TID 3 */
1310 if (tids & BIT(0))
1311 return 0;
1312 return fls(tids) - 1;
1313}
1314
Johannes Berg47086fc2011-09-29 16:04:33 +02001315static void
1316ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1317 int n_frames, u8 ignored_acs,
1318 enum ieee80211_frame_release_type reason)
Johannes Bergaf818582009-11-06 11:35:50 +01001319{
1320 struct ieee80211_sub_if_data *sdata = sta->sdata;
1321 struct ieee80211_local *local = sdata->local;
Johannes Berg948d8872011-09-29 16:04:29 +02001322 bool more_data = false;
1323 int ac;
Johannes Berg4049e092011-09-29 16:04:32 +02001324 unsigned long driver_release_tids = 0;
Johannes Berg47086fc2011-09-29 16:04:33 +02001325 struct sk_buff_head frames;
1326
Johannes Bergdeeaee12011-09-29 16:04:35 +02001327 /* Service or PS-Poll period starts */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001328 set_sta_flag(sta, WLAN_STA_SP);
Johannes Bergdeeaee12011-09-29 16:04:35 +02001329
Johannes Berg47086fc2011-09-29 16:04:33 +02001330 __skb_queue_head_init(&frames);
Johannes Bergaf818582009-11-06 11:35:50 +01001331
Johannes Bergf9f760b2014-01-08 17:45:07 +01001332 /* Get response frame(s) and more data bit for the last one. */
Johannes Berg948d8872011-09-29 16:04:29 +02001333 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg4049e092011-09-29 16:04:32 +02001334 unsigned long tids;
1335
Johannes Berg47086fc2011-09-29 16:04:33 +02001336 if (ignored_acs & BIT(ac))
Johannes Berg948d8872011-09-29 16:04:29 +02001337 continue;
1338
Johannes Berg4049e092011-09-29 16:04:32 +02001339 tids = ieee80211_tids_for_ac(ac);
1340
Johannes Bergf9f760b2014-01-08 17:45:07 +01001341 /* if we already have frames from software, then we can't also
1342 * release from hardware queues
1343 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001344 if (skb_queue_empty(&frames)) {
Johannes Bergf9f760b2014-01-08 17:45:07 +01001345 driver_release_tids |= sta->driver_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001346 driver_release_tids |= sta->txq_buffered_tids & tids;
1347 }
Johannes Berg47086fc2011-09-29 16:04:33 +02001348
Johannes Bergf9f760b2014-01-08 17:45:07 +01001349 if (driver_release_tids) {
1350 /* If the driver has data on more than one TID then
Johannes Berg4049e092011-09-29 16:04:32 +02001351 * certainly there's more data if we release just a
Johannes Bergf9f760b2014-01-08 17:45:07 +01001352 * single frame now (from a single TID). This will
1353 * only happen for PS-Poll.
Johannes Berg4049e092011-09-29 16:04:32 +02001354 */
Johannes Berg47086fc2011-09-29 16:04:33 +02001355 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1356 hweight16(driver_release_tids) > 1) {
Johannes Berg4049e092011-09-29 16:04:32 +02001357 more_data = true;
1358 driver_release_tids =
Johannes Berg0a1cb802014-01-09 11:05:31 +01001359 BIT(find_highest_prio_tid(
1360 driver_release_tids));
Johannes Berg4049e092011-09-29 16:04:32 +02001361 break;
Johannes Berg948d8872011-09-29 16:04:29 +02001362 }
Johannes Bergf9f760b2014-01-08 17:45:07 +01001363 } else {
1364 struct sk_buff *skb;
1365
1366 while (n_frames > 0) {
1367 skb = skb_dequeue(&sta->tx_filtered[ac]);
1368 if (!skb) {
1369 skb = skb_dequeue(
1370 &sta->ps_tx_buf[ac]);
1371 if (skb)
1372 local->total_ps_buffered--;
1373 }
1374 if (!skb)
1375 break;
1376 n_frames--;
1377 __skb_queue_tail(&frames, skb);
1378 }
Johannes Berg948d8872011-09-29 16:04:29 +02001379 }
1380
Johannes Bergf9f760b2014-01-08 17:45:07 +01001381 /* If we have more frames buffered on this AC, then set the
1382 * more-data bit and abort the loop since we can't send more
1383 * data from other ACs before the buffered frames from this.
1384 */
Johannes Berg948d8872011-09-29 16:04:29 +02001385 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1386 !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1387 more_data = true;
1388 break;
1389 }
Johannes Bergaf818582009-11-06 11:35:50 +01001390 }
Johannes Bergaf818582009-11-06 11:35:50 +01001391
Johannes Bergf9f760b2014-01-08 17:45:07 +01001392 if (skb_queue_empty(&frames) && !driver_release_tids) {
Johannes Bergce662b442011-09-29 16:04:34 +02001393 int tid;
Johannes Berg4049e092011-09-29 16:04:32 +02001394
Johannes Bergce662b442011-09-29 16:04:34 +02001395 /*
1396 * For PS-Poll, this can only happen due to a race condition
1397 * when we set the TIM bit and the station notices it, but
1398 * before it can poll for the frame we expire it.
1399 *
1400 * For uAPSD, this is said in the standard (11.2.1.5 h):
1401 * At each unscheduled SP for a non-AP STA, the AP shall
1402 * attempt to transmit at least one MSDU or MMPDU, but no
1403 * more than the value specified in the Max SP Length field
1404 * in the QoS Capability element from delivery-enabled ACs,
1405 * that are destined for the non-AP STA.
1406 *
1407 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1408 */
1409
1410 /* This will evaluate to 1, 3, 5 or 7. */
1411 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1412
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001413 ieee80211_send_null_response(sdata, sta, tid, reason, true);
Johannes Bergf9f760b2014-01-08 17:45:07 +01001414 } else if (!driver_release_tids) {
Johannes Berg47086fc2011-09-29 16:04:33 +02001415 struct sk_buff_head pending;
1416 struct sk_buff *skb;
Johannes Berg40b96402011-09-29 16:04:38 +02001417 int num = 0;
1418 u16 tids = 0;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001419 bool need_null = false;
Johannes Bergaf818582009-11-06 11:35:50 +01001420
Johannes Berg47086fc2011-09-29 16:04:33 +02001421 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001422
Johannes Berg47086fc2011-09-29 16:04:33 +02001423 while ((skb = __skb_dequeue(&frames))) {
1424 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1425 struct ieee80211_hdr *hdr = (void *) skb->data;
Johannes Berg40b96402011-09-29 16:04:38 +02001426 u8 *qoshdr = NULL;
1427
1428 num++;
Johannes Bergaf818582009-11-06 11:35:50 +01001429
Johannes Berg47086fc2011-09-29 16:04:33 +02001430 /*
1431 * Tell TX path to send this frame even though the
1432 * STA may still remain is PS mode after this frame
1433 * exchange.
1434 */
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301435 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1436 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
Johannes Bergaf818582009-11-06 11:35:50 +01001437
Johannes Berg47086fc2011-09-29 16:04:33 +02001438 /*
1439 * Use MoreData flag to indicate whether there are
1440 * more buffered frames for this STA
1441 */
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001442 if (more_data || !skb_queue_empty(&frames))
Johannes Berg47086fc2011-09-29 16:04:33 +02001443 hdr->frame_control |=
1444 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001445 else
1446 hdr->frame_control &=
1447 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
Johannes Berg47086fc2011-09-29 16:04:33 +02001448
Johannes Berg40b96402011-09-29 16:04:38 +02001449 if (ieee80211_is_data_qos(hdr->frame_control) ||
1450 ieee80211_is_qos_nullfunc(hdr->frame_control))
1451 qoshdr = ieee80211_get_qos_ctl(hdr);
1452
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001453 tids |= BIT(skb->priority);
1454
1455 __skb_queue_tail(&pending, skb);
1456
1457 /* end service period after last frame or add one */
1458 if (!skb_queue_empty(&frames))
1459 continue;
1460
1461 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1462 /* for PS-Poll, there's only one frame */
1463 info->flags |= IEEE80211_TX_STATUS_EOSP |
1464 IEEE80211_TX_CTL_REQ_TX_STATUS;
1465 break;
1466 }
1467
1468 /* For uAPSD, things are a bit more complicated. If the
1469 * last frame has a QoS header (i.e. is a QoS-data or
1470 * QoS-nulldata frame) then just set the EOSP bit there
1471 * and be done.
1472 * If the frame doesn't have a QoS header (which means
1473 * it should be a bufferable MMPDU) then we can't set
1474 * the EOSP bit in the QoS header; add a QoS-nulldata
1475 * frame to the list to send it after the MMPDU.
1476 *
1477 * Note that this code is only in the mac80211-release
1478 * code path, we assume that the driver will not buffer
1479 * anything but QoS-data frames, or if it does, will
1480 * create the QoS-nulldata frame by itself if needed.
1481 *
1482 * Cf. 802.11-2012 10.2.1.10 (c).
1483 */
1484 if (qoshdr) {
1485 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
Johannes Berg47086fc2011-09-29 16:04:33 +02001486
Marco Porsch52a3f202012-03-16 15:30:26 +01001487 info->flags |= IEEE80211_TX_STATUS_EOSP |
1488 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001489 } else {
1490 /* The standard isn't completely clear on this
1491 * as it says the more-data bit should be set
1492 * if there are more BUs. The QoS-Null frame
1493 * we're about to send isn't buffered yet, we
1494 * only create it below, but let's pretend it
1495 * was buffered just in case some clients only
1496 * expect more-data=0 when eosp=1.
1497 */
1498 hdr->frame_control |=
1499 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1500 need_null = true;
1501 num++;
Marco Porsch52a3f202012-03-16 15:30:26 +01001502 }
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001503 break;
Johannes Berg47086fc2011-09-29 16:04:33 +02001504 }
1505
Johannes Berg40b96402011-09-29 16:04:38 +02001506 drv_allow_buffered_frames(local, sta, tids, num,
1507 reason, more_data);
1508
Johannes Berg47086fc2011-09-29 16:04:33 +02001509 ieee80211_add_pending_skbs(local, &pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001510
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001511 if (need_null)
1512 ieee80211_send_null_response(
1513 sdata, sta, find_highest_prio_tid(tids),
1514 reason, false);
1515
Johannes Bergc868cb352011-09-29 16:04:27 +02001516 sta_info_recalc_tim(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001517 } else {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001518 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1519 int tid;
1520
Johannes Bergaf818582009-11-06 11:35:50 +01001521 /*
Johannes Berg4049e092011-09-29 16:04:32 +02001522 * We need to release a frame that is buffered somewhere in the
1523 * driver ... it'll have to handle that.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001524 * Note that the driver also has to check the number of frames
1525 * on the TIDs we're releasing from - if there are more than
1526 * n_frames it has to set the more-data bit (if we didn't ask
1527 * it to set it anyway due to other buffered frames); if there
1528 * are fewer than n_frames it has to make sure to adjust that
1529 * to allow the service period to end properly.
Johannes Bergaf818582009-11-06 11:35:50 +01001530 */
Johannes Berg4049e092011-09-29 16:04:32 +02001531 drv_release_buffered_frames(local, sta, driver_release_tids,
Johannes Berg47086fc2011-09-29 16:04:33 +02001532 n_frames, reason, more_data);
Johannes Berg4049e092011-09-29 16:04:32 +02001533
1534 /*
1535 * Note that we don't recalculate the TIM bit here as it would
1536 * most likely have no effect at all unless the driver told us
Johannes Bergf9f760b2014-01-08 17:45:07 +01001537 * that the TID(s) became empty before returning here from the
Johannes Berg4049e092011-09-29 16:04:32 +02001538 * release function.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001539 * Either way, however, when the driver tells us that the TID(s)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001540 * became empty or we find that a txq became empty, we'll do the
1541 * TIM recalculation.
Johannes Berg4049e092011-09-29 16:04:32 +02001542 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001543
1544 if (!sta->sta.txq[0])
1545 return;
1546
1547 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1548 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1549
1550 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1551 continue;
1552
1553 sta_info_recalc_tim(sta);
1554 break;
1555 }
Johannes Bergaf818582009-11-06 11:35:50 +01001556 }
1557}
1558
Johannes Bergaf818582009-11-06 11:35:50 +01001559void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1560{
Johannes Berg47086fc2011-09-29 16:04:33 +02001561 u8 ignore_for_response = sta->sta.uapsd_queues;
Johannes Bergaf818582009-11-06 11:35:50 +01001562
Johannes Berg47086fc2011-09-29 16:04:33 +02001563 /*
1564 * If all ACs are delivery-enabled then we should reply
1565 * from any of them, if only some are enabled we reply
1566 * only from the non-enabled ones.
1567 */
1568 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1569 ignore_for_response = 0;
1570
1571 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1572 IEEE80211_FRAME_RELEASE_PSPOLL);
1573}
1574
1575void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1576{
1577 int n_frames = sta->sta.max_sp;
1578 u8 delivery_enabled = sta->sta.uapsd_queues;
1579
1580 /*
1581 * If we ever grow support for TSPEC this might happen if
1582 * the TSPEC update from hostapd comes in between a trigger
1583 * frame setting WLAN_STA_UAPSD in the RX path and this
1584 * actually getting called.
1585 */
1586 if (!delivery_enabled)
1587 return;
1588
Johannes Berg47086fc2011-09-29 16:04:33 +02001589 switch (sta->sta.max_sp) {
1590 case 1:
1591 n_frames = 2;
1592 break;
1593 case 2:
1594 n_frames = 4;
1595 break;
1596 case 3:
1597 n_frames = 6;
1598 break;
1599 case 0:
1600 /* XXX: what is a good value? */
Andrei Otcheretianski13a80982014-11-04 11:33:04 +02001601 n_frames = 128;
Johannes Berg47086fc2011-09-29 16:04:33 +02001602 break;
Johannes Bergaf818582009-11-06 11:35:50 +01001603 }
Johannes Bergaf818582009-11-06 11:35:50 +01001604
Johannes Berg47086fc2011-09-29 16:04:33 +02001605 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1606 IEEE80211_FRAME_RELEASE_UAPSD);
Johannes Bergaf818582009-11-06 11:35:50 +01001607}
1608
1609void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1610 struct ieee80211_sta *pubsta, bool block)
1611{
1612 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1613
Johannes Bergb5878a22010-04-07 16:48:40 +02001614 trace_api_sta_block_awake(sta->local, pubsta, block);
1615
Johannes Berg5ac2e352014-05-27 16:32:27 +02001616 if (block) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001617 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001618 return;
1619 }
1620
1621 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1622 return;
1623
1624 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1625 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1626 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1627 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1628 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1629 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1630 /* must be asleep in this case */
1631 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1632 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1633 } else {
1634 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1635 }
Johannes Bergaf818582009-11-06 11:35:50 +01001636}
1637EXPORT_SYMBOL(ieee80211_sta_block_awake);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001638
Johannes Berge9437892013-02-15 21:38:08 +01001639void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
Johannes Berg37fbd902011-09-29 16:04:39 +02001640{
1641 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1642 struct ieee80211_local *local = sta->local;
Johannes Berg37fbd902011-09-29 16:04:39 +02001643
1644 trace_api_eosp(local, pubsta);
1645
Johannes Berge9437892013-02-15 21:38:08 +01001646 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg37fbd902011-09-29 16:04:39 +02001647}
Johannes Berge9437892013-02-15 21:38:08 +01001648EXPORT_SYMBOL(ieee80211_sta_eosp);
Johannes Berg37fbd902011-09-29 16:04:39 +02001649
Johannes Berg042ec452011-09-29 16:04:26 +02001650void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1651 u8 tid, bool buffered)
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001652{
1653 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1654
Johannes Berg5a306f52012-11-14 23:22:21 +01001655 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
Johannes Berg042ec452011-09-29 16:04:26 +02001656 return;
1657
Johannes Berg1b000782013-12-19 10:47:48 +01001658 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1659
Johannes Berg948d8872011-09-29 16:04:29 +02001660 if (buffered)
1661 set_bit(tid, &sta->driver_buffered_tids);
1662 else
1663 clear_bit(tid, &sta->driver_buffered_tids);
1664
Johannes Bergc868cb352011-09-29 16:04:27 +02001665 sta_info_recalc_tim(sta);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001666}
Johannes Berg042ec452011-09-29 16:04:26 +02001667EXPORT_SYMBOL(ieee80211_sta_set_buffered);
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001668
Johannes Berg83d5cc02012-01-12 09:31:10 +01001669int sta_info_move_state(struct sta_info *sta,
1670 enum ieee80211_sta_state new_state)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001671{
Johannes Berg8bf11d82011-12-15 11:17:37 +01001672 might_sleep();
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001673
1674 if (sta->sta_state == new_state)
1675 return 0;
1676
Johannes Bergf09603a2012-01-20 13:55:21 +01001677 /* check allowed transitions first */
1678
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001679 switch (new_state) {
1680 case IEEE80211_STA_NONE:
Johannes Bergf09603a2012-01-20 13:55:21 +01001681 if (sta->sta_state != IEEE80211_STA_AUTH)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001682 return -EINVAL;
1683 break;
1684 case IEEE80211_STA_AUTH:
Johannes Bergf09603a2012-01-20 13:55:21 +01001685 if (sta->sta_state != IEEE80211_STA_NONE &&
1686 sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001687 return -EINVAL;
1688 break;
1689 case IEEE80211_STA_ASSOC:
Johannes Bergf09603a2012-01-20 13:55:21 +01001690 if (sta->sta_state != IEEE80211_STA_AUTH &&
1691 sta->sta_state != IEEE80211_STA_AUTHORIZED)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001692 return -EINVAL;
1693 break;
1694 case IEEE80211_STA_AUTHORIZED:
Johannes Bergf09603a2012-01-20 13:55:21 +01001695 if (sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001696 return -EINVAL;
1697 break;
1698 default:
1699 WARN(1, "invalid state %d", new_state);
1700 return -EINVAL;
1701 }
1702
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001703 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1704 sta->sta.addr, new_state);
Johannes Bergf09603a2012-01-20 13:55:21 +01001705
1706 /*
1707 * notify the driver before the actual changes so it can
1708 * fail the transition
1709 */
1710 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1711 int err = drv_sta_state(sta->local, sta->sdata, sta,
1712 sta->sta_state, new_state);
1713 if (err)
1714 return err;
1715 }
1716
1717 /* reflect the change in all state variables */
1718
1719 switch (new_state) {
1720 case IEEE80211_STA_NONE:
1721 if (sta->sta_state == IEEE80211_STA_AUTH)
1722 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1723 break;
1724 case IEEE80211_STA_AUTH:
1725 if (sta->sta_state == IEEE80211_STA_NONE)
1726 set_bit(WLAN_STA_AUTH, &sta->_flags);
1727 else if (sta->sta_state == IEEE80211_STA_ASSOC)
1728 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1729 break;
1730 case IEEE80211_STA_ASSOC:
1731 if (sta->sta_state == IEEE80211_STA_AUTH) {
1732 set_bit(WLAN_STA_ASSOC, &sta->_flags);
1733 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001734 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1735 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1736 !sta->sdata->u.vlan.sta))
1737 atomic_dec(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001738 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1739 }
1740 break;
1741 case IEEE80211_STA_AUTHORIZED:
1742 if (sta->sta_state == IEEE80211_STA_ASSOC) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001743 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1744 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1745 !sta->sdata->u.vlan.sta))
1746 atomic_inc(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001747 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1748 }
1749 break;
1750 default:
1751 break;
1752 }
1753
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001754 sta->sta_state = new_state;
1755
1756 return 0;
1757}
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001758
1759u8 sta_info_tx_streams(struct sta_info *sta)
1760{
1761 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1762 u8 rx_streams;
1763
1764 if (!sta->sta.ht_cap.ht_supported)
1765 return 1;
1766
1767 if (sta->sta.vht_cap.vht_supported) {
1768 int i;
1769 u16 tx_mcs_map =
1770 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1771
1772 for (i = 7; i >= 0; i--)
1773 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1774 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1775 return i + 1;
1776 }
1777
1778 if (ht_cap->mcs.rx_mask[3])
1779 rx_streams = 4;
1780 else if (ht_cap->mcs.rx_mask[2])
1781 rx_streams = 3;
1782 else if (ht_cap->mcs.rx_mask[1])
1783 rx_streams = 2;
1784 else
1785 rx_streams = 1;
1786
1787 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1788 return rx_streams;
1789
1790 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1791 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1792}
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001793
1794void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1795{
1796 struct ieee80211_sub_if_data *sdata = sta->sdata;
1797 struct ieee80211_local *local = sdata->local;
John W. Linville9a244402014-07-25 10:22:36 -04001798 struct rate_control_ref *ref = NULL;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001799 struct timespec uptime;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001800 u32 thr = 0;
1801 int i, ac;
1802
John W. Linville9a244402014-07-25 10:22:36 -04001803 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1804 ref = local->rate_ctrl;
1805
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001806 sinfo->generation = sdata->local->sta_generation;
1807
Johannes Berg225b8182015-01-21 21:09:02 +01001808 /* do before driver, so beacon filtering drivers have a
1809 * chance to e.g. just add the number of filtered beacons
1810 * (or just modify the value entirely, of course)
1811 */
1812 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1813 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1814
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001815 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1816
Johannes Berg319090b2014-11-17 14:08:11 +01001817 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1818 BIT(NL80211_STA_INFO_STA_FLAGS) |
1819 BIT(NL80211_STA_INFO_BSS_PARAM) |
1820 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1821 BIT(NL80211_STA_INFO_RX_DROP_MISC) |
1822 BIT(NL80211_STA_INFO_BEACON_LOSS);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001823
Thomas Gleixner18171522014-06-11 23:59:14 +00001824 ktime_get_ts(&uptime);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001825 sinfo->connected_time = uptime.tv_sec - sta->last_connected;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001826 sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001827
Johannes Berg319090b2014-11-17 14:08:11 +01001828 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1829 BIT(NL80211_STA_INFO_TX_BYTES)))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001830 sinfo->tx_bytes = 0;
1831 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1832 sinfo->tx_bytes += sta->tx_bytes[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01001833 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001834 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001835
Johannes Berg319090b2014-11-17 14:08:11 +01001836 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001837 sinfo->tx_packets = 0;
1838 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1839 sinfo->tx_packets += sta->tx_packets[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01001840 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001841 }
1842
Johannes Berg319090b2014-11-17 14:08:11 +01001843 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1844 BIT(NL80211_STA_INFO_RX_BYTES)))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001845 sinfo->rx_bytes = sta->rx_bytes;
Johannes Berg319090b2014-11-17 14:08:11 +01001846 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001847 }
1848
Johannes Berg319090b2014-11-17 14:08:11 +01001849 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001850 sinfo->rx_packets = sta->rx_packets;
Johannes Berg319090b2014-11-17 14:08:11 +01001851 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001852 }
1853
Johannes Berg319090b2014-11-17 14:08:11 +01001854 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001855 sinfo->tx_retries = sta->tx_retry_count;
Johannes Berg319090b2014-11-17 14:08:11 +01001856 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001857 }
1858
Johannes Berg319090b2014-11-17 14:08:11 +01001859 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001860 sinfo->tx_failed = sta->tx_retry_failed;
Johannes Berg319090b2014-11-17 14:08:11 +01001861 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001862 }
1863
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001864 sinfo->rx_dropped_misc = sta->rx_dropped;
1865 sinfo->beacon_loss_count = sta->beacon_loss_count;
1866
Johannes Berg225b8182015-01-21 21:09:02 +01001867 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1868 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1869 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1870 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1871 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1872 }
1873
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001874 if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
1875 (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
Johannes Berg319090b2014-11-17 14:08:11 +01001876 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001877 sinfo->signal = (s8)sta->last_signal;
Johannes Berg319090b2014-11-17 14:08:11 +01001878 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001879 }
1880
Johannes Berg319090b2014-11-17 14:08:11 +01001881 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001882 sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
Johannes Berg319090b2014-11-17 14:08:11 +01001883 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001884 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001885 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001886
1887 if (sta->chains &&
Johannes Berg319090b2014-11-17 14:08:11 +01001888 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1889 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1890 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1891 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001892
1893 sinfo->chains = sta->chains;
1894 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1895 sinfo->chain_signal[i] = sta->chain_signal_last[i];
1896 sinfo->chain_signal_avg[i] =
1897 (s8) -ewma_read(&sta->chain_signal_avg[i]);
1898 }
1899 }
1900
Johannes Berg319090b2014-11-17 14:08:11 +01001901 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001902 sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
Johannes Berg319090b2014-11-17 14:08:11 +01001903 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001904 }
1905
Johannes Berg319090b2014-11-17 14:08:11 +01001906 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001907 sta_set_rate_info_rx(sta, &sinfo->rxrate);
Johannes Berg319090b2014-11-17 14:08:11 +01001908 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001909 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001910
Johannes Berg79c892b2014-11-21 14:26:31 +01001911 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
1912 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
1913 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
1914
1915 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
1916 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
1917 tidstats->rx_msdu = sta->rx_msdu[i];
1918 }
1919
1920 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
1921 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
1922 tidstats->tx_msdu = sta->tx_msdu[i];
1923 }
1924
1925 if (!(tidstats->filled &
1926 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
1927 local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1928 tidstats->filled |=
1929 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
1930 tidstats->tx_msdu_retries = sta->tx_msdu_retries[i];
1931 }
1932
1933 if (!(tidstats->filled &
1934 BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
1935 local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1936 tidstats->filled |=
1937 BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
1938 tidstats->tx_msdu_failed = sta->tx_msdu_failed[i];
1939 }
1940 }
1941
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001942 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1943#ifdef CONFIG_MAC80211_MESH
Johannes Berg319090b2014-11-17 14:08:11 +01001944 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
1945 BIT(NL80211_STA_INFO_PLID) |
1946 BIT(NL80211_STA_INFO_PLINK_STATE) |
1947 BIT(NL80211_STA_INFO_LOCAL_PM) |
1948 BIT(NL80211_STA_INFO_PEER_PM) |
1949 BIT(NL80211_STA_INFO_NONPEER_PM);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001950
1951 sinfo->llid = sta->llid;
1952 sinfo->plid = sta->plid;
1953 sinfo->plink_state = sta->plink_state;
1954 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
Johannes Berg319090b2014-11-17 14:08:11 +01001955 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001956 sinfo->t_offset = sta->t_offset;
1957 }
1958 sinfo->local_pm = sta->local_pm;
1959 sinfo->peer_pm = sta->peer_pm;
1960 sinfo->nonpeer_pm = sta->nonpeer_pm;
1961#endif
1962 }
1963
1964 sinfo->bss_param.flags = 0;
1965 if (sdata->vif.bss_conf.use_cts_prot)
1966 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1967 if (sdata->vif.bss_conf.use_short_preamble)
1968 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1969 if (sdata->vif.bss_conf.use_short_slot)
1970 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
Emmanuel Grumbach785e21a2014-09-03 15:25:04 +03001971 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001972 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1973
1974 sinfo->sta_flags.set = 0;
1975 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1976 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1977 BIT(NL80211_STA_FLAG_WME) |
1978 BIT(NL80211_STA_FLAG_MFP) |
1979 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1980 BIT(NL80211_STA_FLAG_ASSOCIATED) |
1981 BIT(NL80211_STA_FLAG_TDLS_PEER);
1982 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1983 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
1984 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
1985 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
Johannes Berga74a8c82014-07-22 14:50:47 +02001986 if (sta->sta.wme)
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001987 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
1988 if (test_sta_flag(sta, WLAN_STA_MFP))
1989 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
1990 if (test_sta_flag(sta, WLAN_STA_AUTH))
1991 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
1992 if (test_sta_flag(sta, WLAN_STA_ASSOC))
1993 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1994 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1995 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
1996
1997 /* check if the driver has a SW RC implementation */
1998 if (ref && ref->ops->get_expected_throughput)
1999 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2000 else
2001 thr = drv_get_expected_throughput(local, &sta->sta);
2002
2003 if (thr != 0) {
Johannes Berg319090b2014-11-17 14:08:11 +01002004 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002005 sinfo->expected_throughput = thr;
2006 }
2007}