xref: /openbmc/linux/net/mac80211/sta_info.c (revision 49ddf8e6e2347cffdcf83d1ca2d04ff929820178)
1f0706e82SJiri Benc /*
2f0706e82SJiri Benc  * Copyright 2002-2005, Instant802 Networks, Inc.
3f0706e82SJiri Benc  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
4d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
5f59374ebSSara Sharon  * Copyright (C) 2015 - 2016 Intel Deutschland GmbH
6f0706e82SJiri Benc  *
7f0706e82SJiri Benc  * This program is free software; you can redistribute it and/or modify
8f0706e82SJiri Benc  * it under the terms of the GNU General Public License version 2 as
9f0706e82SJiri Benc  * published by the Free Software Foundation.
10f0706e82SJiri Benc  */
11f0706e82SJiri Benc 
12f0706e82SJiri Benc #include <linux/module.h>
13f0706e82SJiri Benc #include <linux/init.h>
14888d04dfSFelix Fietkau #include <linux/etherdevice.h>
15f0706e82SJiri Benc #include <linux/netdevice.h>
16f0706e82SJiri Benc #include <linux/types.h>
17f0706e82SJiri Benc #include <linux/slab.h>
18f0706e82SJiri Benc #include <linux/skbuff.h>
19f0706e82SJiri Benc #include <linux/if_arp.h>
200d174406SJohannes Berg #include <linux/timer.h>
21d0709a65SJohannes Berg #include <linux/rtnetlink.h>
22f0706e82SJiri Benc 
23f0706e82SJiri Benc #include <net/mac80211.h>
24f0706e82SJiri Benc #include "ieee80211_i.h"
2524487981SJohannes Berg #include "driver-ops.h"
262c8dccc7SJohannes Berg #include "rate.h"
27f0706e82SJiri Benc #include "sta_info.h"
28e9f207f0SJiri Benc #include "debugfs_sta.h"
29ee385855SLuis Carlos Cobo #include "mesh.h"
30ce662b44SJohannes Berg #include "wme.h"
31f0706e82SJiri Benc 
32d0709a65SJohannes Berg /**
33d0709a65SJohannes Berg  * DOC: STA information lifetime rules
34d0709a65SJohannes Berg  *
35d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
36d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
37d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
38d0709a65SJohannes Berg  *
3934e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
4034e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4134e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4234e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4334e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4434e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4534e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4634e89507SJohannes Berg  * encryption keys.
4793e5deb1SJohannes Berg  *
4893e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4993e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
50d0709a65SJohannes Berg  *
5134e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
527e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
537e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5425985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5534e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5625985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5734e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
587e189a12SLuis R. Rodriguez  *
5934e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
6034e89507SJohannes Berg  * calls are available.
61d0709a65SJohannes Berg  *
6234e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6334e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6434e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6534e89507SJohannes Berg  * freed before they are done using it.
66d0709a65SJohannes Berg  */
67f0706e82SJiri Benc 
687bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
697bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
70caf22d31SJohannes Berg 	.automatic_shrinking = true,
717bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
72ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
737bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
747bedd0cfSJohannes Berg 	.hashfn = sta_addr_hash,
75ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
767bedd0cfSJohannes Berg };
777bedd0cfSJohannes Berg 
784d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
79be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
80f0706e82SJiri Benc 			     struct sta_info *sta)
81f0706e82SJiri Benc {
827bedd0cfSJohannes Berg 	return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
837bedd0cfSJohannes Berg 				      sta_rht_params);
84f0706e82SJiri Benc }
85f0706e82SJiri Benc 
865108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
87b22cfcfcSEliad Peller {
88b22cfcfcSEliad Peller 	int ac, i;
89b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
90b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
91b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
92d012a605SMarco Porsch 	struct ps_data *ps;
93b22cfcfcSEliad Peller 
94e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
955ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
965ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
97d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
98d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
99d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1003f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1013f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
102d012a605SMarco Porsch 		else
103d012a605SMarco Porsch 			return;
104b22cfcfcSEliad Peller 
105b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
106e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1075ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
108b22cfcfcSEliad Peller 
109d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
110b22cfcfcSEliad Peller 	}
111b22cfcfcSEliad Peller 
112ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
113ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
114ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
115ba8c3d6fSFelix Fietkau 			int n = skb_queue_len(&txqi->queue);
116ba8c3d6fSFelix Fietkau 
117ba8c3d6fSFelix Fietkau 			ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
118ba8c3d6fSFelix Fietkau 			atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
119f2ac7e30SMichal Kazior 			txqi->byte_cnt = 0;
120ba8c3d6fSFelix Fietkau 		}
121ba8c3d6fSFelix Fietkau 	}
122ba8c3d6fSFelix Fietkau 
123b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
124b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1251f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1261f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
127b22cfcfcSEliad Peller 	}
128b22cfcfcSEliad Peller 
12945b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
13045b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
131b22cfcfcSEliad Peller 
1325ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
133b22cfcfcSEliad Peller 
134b22cfcfcSEliad Peller 	/*
135b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
136b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
137b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
138b22cfcfcSEliad Peller 	 * directly by station destruction.
139b22cfcfcSEliad Peller 	 */
1405a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
141661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
142b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
143b22cfcfcSEliad Peller 		if (!tid_tx)
144b22cfcfcSEliad Peller 			continue;
1451f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
146b22cfcfcSEliad Peller 		kfree(tid_tx);
147b22cfcfcSEliad Peller 	}
1485108ca82SJohannes Berg }
149b22cfcfcSEliad Peller 
1505108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1515108ca82SJohannes Berg {
1525108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1535108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1545108ca82SJohannes Berg 
1555108ca82SJohannes Berg 	__cleanup_single_sta(sta);
156b22cfcfcSEliad Peller 	sta_info_free(local, sta);
157b22cfcfcSEliad Peller }
158b22cfcfcSEliad Peller 
159d0709a65SJohannes Berg /* protected by RCU */
160abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
161abe60632SJohannes Berg 			      const u8 *addr)
16243ba7e95SJohannes Berg {
163abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
16460f4b626SJohannes Berg 	struct sta_info *sta;
16560f4b626SJohannes Berg 	struct rhash_head *tmp;
16660f4b626SJohannes Berg 	const struct bucket_table *tbl;
16743ba7e95SJohannes Berg 
16860f4b626SJohannes Berg 	rcu_read_lock();
16960f4b626SJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
17060f4b626SJohannes Berg 
17160f4b626SJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
17260f4b626SJohannes Berg 		if (sta->sdata == sdata) {
17360f4b626SJohannes Berg 			rcu_read_unlock();
17460f4b626SJohannes Berg 			/* this is safe as the caller must already hold
17560f4b626SJohannes Berg 			 * another rcu read section or the mutex
17660f4b626SJohannes Berg 			 */
17760f4b626SJohannes Berg 			return sta;
17860f4b626SJohannes Berg 		}
17960f4b626SJohannes Berg 	}
18060f4b626SJohannes Berg 	rcu_read_unlock();
18160f4b626SJohannes Berg 	return NULL;
18243ba7e95SJohannes Berg }
18343ba7e95SJohannes Berg 
1840e5ded5aSFelix Fietkau /*
1850e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1860e5ded5aSFelix Fietkau  * or from one of its vlans
1870e5ded5aSFelix Fietkau  */
1880e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1890e5ded5aSFelix Fietkau 				  const u8 *addr)
1900e5ded5aSFelix Fietkau {
1910e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1920e5ded5aSFelix Fietkau 	struct sta_info *sta;
1937bedd0cfSJohannes Berg 	struct rhash_head *tmp;
1947bedd0cfSJohannes Berg 	const struct bucket_table *tbl;
1950e5ded5aSFelix Fietkau 
1967bedd0cfSJohannes Berg 	rcu_read_lock();
1977bedd0cfSJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
1987bedd0cfSJohannes Berg 
1997bedd0cfSJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
2007bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
2017bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
2027bedd0cfSJohannes Berg 			rcu_read_unlock();
2037bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
2047bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
2057bedd0cfSJohannes Berg 			 */
2060e5ded5aSFelix Fietkau 			return sta;
2070e5ded5aSFelix Fietkau 		}
2087bedd0cfSJohannes Berg 	}
2097bedd0cfSJohannes Berg 	rcu_read_unlock();
2107bedd0cfSJohannes Berg 	return NULL;
2117bedd0cfSJohannes Berg }
2120e5ded5aSFelix Fietkau 
2133b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2143b53fde8SJohannes Berg 				     int idx)
215ee385855SLuis Carlos Cobo {
2163b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
217ee385855SLuis Carlos Cobo 	struct sta_info *sta;
218ee385855SLuis Carlos Cobo 	int i = 0;
219ee385855SLuis Carlos Cobo 
220d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
2213b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2222a8ca29aSLuis Carlos Cobo 			continue;
223ee385855SLuis Carlos Cobo 		if (i < idx) {
224ee385855SLuis Carlos Cobo 			++i;
225ee385855SLuis Carlos Cobo 			continue;
226ee385855SLuis Carlos Cobo 		}
2272a8ca29aSLuis Carlos Cobo 		return sta;
228ee385855SLuis Carlos Cobo 	}
229ee385855SLuis Carlos Cobo 
230ee385855SLuis Carlos Cobo 	return NULL;
231ee385855SLuis Carlos Cobo }
232f0706e82SJiri Benc 
23393e5deb1SJohannes Berg /**
234d9a7ddb0SJohannes Berg  * sta_info_free - free STA
23593e5deb1SJohannes Berg  *
2366ef307bcSRandy Dunlap  * @local: pointer to the global information
23793e5deb1SJohannes Berg  * @sta: STA info to free
23893e5deb1SJohannes Berg  *
23993e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
240d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
241d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
242d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
24393e5deb1SJohannes Berg  */
244d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
24593e5deb1SJohannes Berg {
246889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2474b7679a5SJohannes Berg 		rate_control_free_sta(sta);
24893e5deb1SJohannes Berg 
249bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
25093e5deb1SJohannes Berg 
251ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
252ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
25353d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
254433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
255433f5bc1SJohannes Berg 	kfree(sta->mesh);
256433f5bc1SJohannes Berg #endif
25793e5deb1SJohannes Berg 	kfree(sta);
25893e5deb1SJohannes Berg }
25993e5deb1SJohannes Berg 
2604d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
261d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
262d0709a65SJohannes Berg 			      struct sta_info *sta)
263f0706e82SJiri Benc {
2647bedd0cfSJohannes Berg 	rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
2657bedd0cfSJohannes Berg 			       sta_rht_params);
266f0706e82SJiri Benc }
267f0706e82SJiri Benc 
2685ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
269af818581SJohannes Berg {
270af818581SJohannes Berg 	struct sta_info *sta;
271af818581SJohannes Berg 
2725ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
273af818581SJohannes Berg 
274af818581SJohannes Berg 	if (sta->dead)
275af818581SJohannes Berg 		return;
276af818581SJohannes Berg 
27754420473SHelmut Schaa 	local_bh_disable();
2785ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
279af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2805ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
281af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2825ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
28347086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
284ce662b44SJohannes Berg 	local_bh_enable();
285af818581SJohannes Berg }
286af818581SJohannes Berg 
287af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
288af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
289af65cd96SJohannes Berg {
29030686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
291af65cd96SJohannes Berg 		return 0;
292af65cd96SJohannes Berg 
293889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
294af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
29535c347acSJohannes Berg 						     sta, gfp);
296889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
297af65cd96SJohannes Berg 		return -ENOMEM;
298af65cd96SJohannes Berg 
299af65cd96SJohannes Berg 	return 0;
300af65cd96SJohannes Berg }
301af65cd96SJohannes Berg 
30273651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
30356544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
304f0706e82SJiri Benc {
305d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
306ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
307f0706e82SJiri Benc 	struct sta_info *sta;
30816c5f15cSRon Rindjunsky 	int i;
309f0706e82SJiri Benc 
310ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
311f0706e82SJiri Benc 	if (!sta)
31273651ee6SJohannes Berg 		return NULL;
313f0706e82SJiri Benc 
31407346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3151d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
3165ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
31767c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
318a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
31987f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
320433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
321433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
322433f5bc1SJohannes Berg 		if (!sta->mesh)
323433f5bc1SJohannes Berg 			goto free;
324433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
32587f59c70SThomas Pedersen 		if (ieee80211_vif_is_mesh(&sdata->vif) &&
32687f59c70SThomas Pedersen 		    !sdata->u.mesh.user_mpm)
327433f5bc1SJohannes Berg 			init_timer(&sta->mesh->plink_timer);
328433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
329433f5bc1SJohannes Berg 	}
33087f59c70SThomas Pedersen #endif
33107346f81SJohannes Berg 
332ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
33317741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
334d0709a65SJohannes Berg 	sta->local = local;
335d0709a65SJohannes Berg 	sta->sdata = sdata;
336e5a9f8d0SJohannes Berg 	sta->rx_stats.last_rx = jiffies;
337f0706e82SJiri Benc 
3380f9c5a61SJohannes Berg 	u64_stats_init(&sta->rx_stats.syncp);
3390f9c5a61SJohannes Berg 
34071ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
34171ec375cSJohannes Berg 
342b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
343b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
344b6da911bSLiad Kaufman 
34584b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
3460be6ed13SJohannes Berg 	ewma_signal_init(&sta->rx_stats_avg.signal);
3470be6ed13SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
3480be6ed13SJohannes Berg 		ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
349541a45a1SBruno Randolf 
350ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
351ba8c3d6fSFelix Fietkau 		void *txq_data;
352ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
353ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
354ba8c3d6fSFelix Fietkau 
355ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
356ba8c3d6fSFelix Fietkau 		if (!txq_data)
357ba8c3d6fSFelix Fietkau 			goto free;
358ba8c3d6fSFelix Fietkau 
359ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
360ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
361ba8c3d6fSFelix Fietkau 
362ba8c3d6fSFelix Fietkau 			ieee80211_init_tx_queue(sdata, sta, txq, i);
363abfbc3afSJohannes Berg 		}
364ba8c3d6fSFelix Fietkau 	}
365ba8c3d6fSFelix Fietkau 
366ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
367ba8c3d6fSFelix Fietkau 		goto free_txq;
368f0706e82SJiri Benc 
3695a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
370a622ab72SJohannes Berg 		/*
371a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
372a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
373a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
374a622ab72SJohannes Berg 		 */
37516c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
37616c5f15cSRon Rindjunsky 	}
377948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
378948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
379948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
380948d887dSJohannes Berg 	}
38173651ee6SJohannes Berg 
3825a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3834be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
384cccaec98SSenthil Balasubramanian 
385af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
386687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
387687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
388687da132SEmmanuel Grumbach 		struct ieee80211_supported_band *sband =
389ba8c3d6fSFelix Fietkau 			hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
390687da132SEmmanuel Grumbach 		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
391687da132SEmmanuel Grumbach 				IEEE80211_HT_CAP_SM_PS_SHIFT;
392687da132SEmmanuel Grumbach 		/*
393687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
394687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
395687da132SEmmanuel Grumbach 		 */
396687da132SEmmanuel Grumbach 		switch (smps) {
397687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
398687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
399687da132SEmmanuel Grumbach 			break;
400687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
401687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
402687da132SEmmanuel Grumbach 			break;
403687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
404687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
405687da132SEmmanuel Grumbach 			break;
406687da132SEmmanuel Grumbach 		default:
407687da132SEmmanuel Grumbach 			WARN_ON(1);
408687da132SEmmanuel Grumbach 		}
409687da132SEmmanuel Grumbach 	}
410af0ed69bSJohannes Berg 
411bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
412ef04a297SJohannes Berg 
413abfbc3afSJohannes Berg 	return sta;
414ba8c3d6fSFelix Fietkau 
415ba8c3d6fSFelix Fietkau free_txq:
416ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
417ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
418ba8c3d6fSFelix Fietkau free:
419433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
420433f5bc1SJohannes Berg 	kfree(sta->mesh);
421433f5bc1SJohannes Berg #endif
422ba8c3d6fSFelix Fietkau 	kfree(sta);
423ba8c3d6fSFelix Fietkau 	return NULL;
42473651ee6SJohannes Berg }
42573651ee6SJohannes Berg 
4268c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
42734e89507SJohannes Berg {
42834e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
42934e89507SJohannes Berg 
43003e4497eSJohannes Berg 	/*
43103e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
43203e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
43303e4497eSJohannes Berg 	 * and another CPU turns off the net device.
43403e4497eSJohannes Berg 	 */
4358c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4368c71df7aSGuy Eilam 		return -ENETDOWN;
43703e4497eSJohannes Berg 
438b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4398c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4408c71df7aSGuy Eilam 		return -EINVAL;
4418c71df7aSGuy Eilam 
44231104891SJohannes Berg 	/* Strictly speaking this isn't necessary as we hold the mutex, but
44331104891SJohannes Berg 	 * the rhashtable code can't really deal with that distinction. We
44431104891SJohannes Berg 	 * do require the mutex for correctness though.
44531104891SJohannes Berg 	 */
44631104891SJohannes Berg 	rcu_read_lock();
44731104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
44831104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
44931104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
45031104891SJohannes Berg 		rcu_read_unlock();
45131104891SJohannes Berg 		return -ENOTUNIQ;
45231104891SJohannes Berg 	}
45331104891SJohannes Berg 	rcu_read_unlock();
45431104891SJohannes Berg 
4558c71df7aSGuy Eilam 	return 0;
45693e5deb1SJohannes Berg }
45744213b5eSJohannes Berg 
458f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
459f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
460f09603a2SJohannes Berg 				     struct sta_info *sta)
461f09603a2SJohannes Berg {
462f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
463f09603a2SJohannes Berg 	int err = 0;
464f09603a2SJohannes Berg 
465f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
466f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
467f09603a2SJohannes Berg 		if (err)
468f09603a2SJohannes Berg 			break;
469f09603a2SJohannes Berg 	}
470f09603a2SJohannes Berg 
471f09603a2SJohannes Berg 	if (!err) {
472a4ec45a4SJohannes Berg 		/*
473a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
474a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
475a4ec45a4SJohannes Berg 		 */
476a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
477f09603a2SJohannes Berg 			sta->uploaded = true;
478f09603a2SJohannes Berg 		return 0;
479f09603a2SJohannes Berg 	}
480f09603a2SJohannes Berg 
481f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
482bdcbd8e0SJohannes Berg 		sdata_info(sdata,
483bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
484bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
485f09603a2SJohannes Berg 		err = 0;
486f09603a2SJohannes Berg 	}
487f09603a2SJohannes Berg 
488f09603a2SJohannes Berg 	/* unwind on error */
489f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
490f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
491f09603a2SJohannes Berg 
492f09603a2SJohannes Berg 	return err;
493f09603a2SJohannes Berg }
494f09603a2SJohannes Berg 
49534e89507SJohannes Berg /*
4968c71df7aSGuy Eilam  * should be called with sta_mtx locked
4978c71df7aSGuy Eilam  * this function replaces the mutex lock
4988c71df7aSGuy Eilam  * with a RCU lock
4998c71df7aSGuy Eilam  */
5004d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
5018c71df7aSGuy Eilam {
5028c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
5038c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
5040ef049dcSArnd Bergmann 	struct station_info *sinfo;
5058c71df7aSGuy Eilam 	int err = 0;
5068c71df7aSGuy Eilam 
5078c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
5088c71df7aSGuy Eilam 
5090ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
5100ef049dcSArnd Bergmann 	if (!sinfo) {
5110ef049dcSArnd Bergmann 		err = -ENOMEM;
5120ef049dcSArnd Bergmann 		goto out_err;
5130ef049dcSArnd Bergmann 	}
5140ef049dcSArnd Bergmann 
5157852e361SJohannes Berg 	/* check if STA exists already */
5167852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
5174d33960bSJohannes Berg 		err = -EEXIST;
5184d33960bSJohannes Berg 		goto out_err;
51934e89507SJohannes Berg 	}
52034e89507SJohannes Berg 
5214d33960bSJohannes Berg 	local->num_sta++;
5224d33960bSJohannes Berg 	local->sta_generation++;
5234d33960bSJohannes Berg 	smp_mb();
5244d33960bSJohannes Berg 
5255108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
5265108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5275108ca82SJohannes Berg 
5284d33960bSJohannes Berg 	/* make the station visible */
5294d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
5304d33960bSJohannes Berg 
5312bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
53283d5cc01SJohannes Berg 
5335108ca82SJohannes Berg 	/* notify driver */
5345108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
5355108ca82SJohannes Berg 	if (err)
5365108ca82SJohannes Berg 		goto out_remove;
5375108ca82SJohannes Berg 
53883d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
5395108ca82SJohannes Berg 	/* accept BA sessions now */
5405108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5414d33960bSJohannes Berg 
5424d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
5434d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
5444d33960bSJohannes Berg 
5450ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
5460ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
5470ef049dcSArnd Bergmann 	kfree(sinfo);
548d0709a65SJohannes Berg 
549bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
550f0706e82SJiri Benc 
55134e89507SJohannes Berg 	/* move reference to rcu-protected */
55234e89507SJohannes Berg 	rcu_read_lock();
55334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
554e9f207f0SJiri Benc 
55573651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
55673651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
55773651ee6SJohannes Berg 
55873651ee6SJohannes Berg 	return 0;
5595108ca82SJohannes Berg  out_remove:
5605108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
5615108ca82SJohannes Berg 	list_del_rcu(&sta->list);
5625108ca82SJohannes Berg 	local->num_sta--;
5635108ca82SJohannes Berg 	synchronize_net();
5645108ca82SJohannes Berg 	__cleanup_single_sta(sta);
5654d33960bSJohannes Berg  out_err:
5664d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
567ea32f065SSudip Mukherjee 	kfree(sinfo);
5684d33960bSJohannes Berg 	rcu_read_lock();
5694d33960bSJohannes Berg 	return err;
5708c71df7aSGuy Eilam }
5718c71df7aSGuy Eilam 
5728c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5738c71df7aSGuy Eilam {
5748c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
575308f7fcfSZhao, Gang 	int err;
5768c71df7aSGuy Eilam 
5774d33960bSJohannes Berg 	might_sleep();
5784d33960bSJohannes Berg 
57931104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
58031104891SJohannes Berg 
5818c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5828c71df7aSGuy Eilam 	if (err) {
58331104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
5848c71df7aSGuy Eilam 		rcu_read_lock();
5858c71df7aSGuy Eilam 		goto out_free;
5868c71df7aSGuy Eilam 	}
5878c71df7aSGuy Eilam 
5884d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5898c71df7aSGuy Eilam 	if (err)
590004c872eSJohannes Berg 		goto out_free;
591d0709a65SJohannes Berg 
592004c872eSJohannes Berg 	return 0;
59393e5deb1SJohannes Berg  out_free:
594d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
59593e5deb1SJohannes Berg 	return err;
596f0706e82SJiri Benc }
597f0706e82SJiri Benc 
59834e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
59934e89507SJohannes Berg {
60034e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
60134e89507SJohannes Berg 
60234e89507SJohannes Berg 	rcu_read_unlock();
60334e89507SJohannes Berg 
60434e89507SJohannes Berg 	return err;
60534e89507SJohannes Berg }
60634e89507SJohannes Berg 
607d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
608004c872eSJohannes Berg {
609004c872eSJohannes Berg 	/*
610004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
611004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
612004c872eSJohannes Berg 	 */
613d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
614004c872eSJohannes Berg }
615004c872eSJohannes Berg 
616d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
617004c872eSJohannes Berg {
618004c872eSJohannes Berg 	/*
619004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
620004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
621004c872eSJohannes Berg 	 */
622d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
623004c872eSJohannes Berg }
624004c872eSJohannes Berg 
6253d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
6263d5839b6SIlan Peer {
6273d5839b6SIlan Peer 	/*
6283d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
6293d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
6303d5839b6SIlan Peer 	 */
6313d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
6323d5839b6SIlan Peer }
6333d5839b6SIlan Peer 
634948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
635004c872eSJohannes Berg {
636948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
637948d887dSJohannes Berg 	switch (ac) {
638948d887dSJohannes Berg 	case IEEE80211_AC_VO:
639948d887dSJohannes Berg 		return BIT(6) | BIT(7);
640948d887dSJohannes Berg 	case IEEE80211_AC_VI:
641948d887dSJohannes Berg 		return BIT(4) | BIT(5);
642948d887dSJohannes Berg 	case IEEE80211_AC_BE:
643948d887dSJohannes Berg 		return BIT(0) | BIT(3);
644948d887dSJohannes Berg 	case IEEE80211_AC_BK:
645948d887dSJohannes Berg 		return BIT(1) | BIT(2);
646948d887dSJohannes Berg 	default:
647948d887dSJohannes Berg 		WARN_ON(1);
648948d887dSJohannes Berg 		return 0;
649d0709a65SJohannes Berg 	}
650004c872eSJohannes Berg }
651004c872eSJohannes Berg 
6529b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
653004c872eSJohannes Berg {
654c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
655d012a605SMarco Porsch 	struct ps_data *ps;
656948d887dSJohannes Berg 	bool indicate_tim = false;
657948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
658948d887dSJohannes Berg 	int ac;
659a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
660004c872eSJohannes Berg 
661d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
662d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
663c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
664c868cb35SJohannes Berg 			return;
6653e122be0SJohannes Berg 
666d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
6673f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6683f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6693f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
6703f52b7e3SMarco Porsch #endif
671d012a605SMarco Porsch 	} else {
672d012a605SMarco Porsch 		return;
673d012a605SMarco Porsch 	}
674d012a605SMarco Porsch 
675c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
67630686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
677c868cb35SJohannes Berg 		return;
678004c872eSJohannes Berg 
679c868cb35SJohannes Berg 	if (sta->dead)
680c868cb35SJohannes Berg 		goto done;
6813e122be0SJohannes Berg 
682948d887dSJohannes Berg 	/*
683948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
684948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
685948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
686948d887dSJohannes Berg 	 * non-enabled ones.
687948d887dSJohannes Berg 	 */
688948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
689948d887dSJohannes Berg 		ignore_for_tim = 0;
690948d887dSJohannes Berg 
6919b7a86f3SJohannes Berg 	if (ignore_pending)
6929b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
6939b7a86f3SJohannes Berg 
694948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
695948d887dSJohannes Berg 		unsigned long tids;
696948d887dSJohannes Berg 
697948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
698948d887dSJohannes Berg 			continue;
699948d887dSJohannes Berg 
700948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
701948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
702948d887dSJohannes Berg 		if (indicate_tim)
703948d887dSJohannes Berg 			break;
704948d887dSJohannes Berg 
705948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
706948d887dSJohannes Berg 
707948d887dSJohannes Berg 		indicate_tim |=
708948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
709ba8c3d6fSFelix Fietkau 		indicate_tim |=
710ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
711004c872eSJohannes Berg 	}
712004c872eSJohannes Berg 
713c868cb35SJohannes Berg  done:
71465f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
715004c872eSJohannes Berg 
7163d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
7173d5839b6SIlan Peer 		goto out_unlock;
7183d5839b6SIlan Peer 
719948d887dSJohannes Berg 	if (indicate_tim)
720d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
721c868cb35SJohannes Berg 	else
722d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
7233e122be0SJohannes Berg 
7249b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
725c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
726948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
727c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
728004c872eSJohannes Berg 	}
729004c872eSJohannes Berg 
7303d5839b6SIlan Peer out_unlock:
73165f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
732004c872eSJohannes Berg }
733004c872eSJohannes Berg 
7349b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
7359b7a86f3SJohannes Berg {
7369b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
7379b7a86f3SJohannes Berg }
7389b7a86f3SJohannes Berg 
739cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
740f0706e82SJiri Benc {
741e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
742f0706e82SJiri Benc 	int timeout;
743f0706e82SJiri Benc 
744f0706e82SJiri Benc 	if (!skb)
745cd0b8d89SJohannes Berg 		return false;
746f0706e82SJiri Benc 
747e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
748f0706e82SJiri Benc 
749f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
75057c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
75157c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
75257c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
753f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
754f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
755e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
756f0706e82SJiri Benc }
757f0706e82SJiri Benc 
758f0706e82SJiri Benc 
759948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
760948d887dSJohannes Berg 						struct sta_info *sta, int ac)
761f0706e82SJiri Benc {
762f0706e82SJiri Benc 	unsigned long flags;
763f0706e82SJiri Benc 	struct sk_buff *skb;
764f0706e82SJiri Benc 
76560750397SJohannes Berg 	/*
76660750397SJohannes Berg 	 * First check for frames that should expire on the filtered
76760750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
76860750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
76960750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
77060750397SJohannes Berg 	 * total_ps_buffered counter.
77160750397SJohannes Berg 	 */
772f0706e82SJiri Benc 	for (;;) {
773948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
774948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
77557c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
776948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
777836341a7SJohannes Berg 		else
778f0706e82SJiri Benc 			skb = NULL;
779948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
780f0706e82SJiri Benc 
78160750397SJohannes Berg 		/*
78260750397SJohannes Berg 		 * Frames are queued in order, so if this one
78360750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
78460750397SJohannes Berg 		 * we actually reached the end of the queue we
78560750397SJohannes Berg 		 * also need to stop, of course.
78660750397SJohannes Berg 		 */
78760750397SJohannes Berg 		if (!skb)
78860750397SJohannes Berg 			break;
789d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
79060750397SJohannes Berg 	}
79160750397SJohannes Berg 
79260750397SJohannes Berg 	/*
79360750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
79460750397SJohannes Berg 	 * only find something if the filtered queue was emptied
79560750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
79660750397SJohannes Berg 	 * buffered frames.
79760750397SJohannes Berg 	 */
798f0706e82SJiri Benc 	for (;;) {
799948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
800948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
801f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
802948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
803f0706e82SJiri Benc 		else
804f0706e82SJiri Benc 			skb = NULL;
805948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
806f0706e82SJiri Benc 
80760750397SJohannes Berg 		/*
80860750397SJohannes Berg 		 * frames are queued in order, so if this one
80960750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
81060750397SJohannes Berg 		 * the queue) we can stop testing
81160750397SJohannes Berg 		 */
812836341a7SJohannes Berg 		if (!skb)
813836341a7SJohannes Berg 			break;
814836341a7SJohannes Berg 
815f0706e82SJiri Benc 		local->total_ps_buffered--;
816bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
817bdcbd8e0SJohannes Berg 		       sta->sta.addr);
818d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
819f0706e82SJiri Benc 	}
8203393a608SJuuso Oikarinen 
82160750397SJohannes Berg 	/*
82260750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
82360750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
82460750397SJohannes Berg 	 * frames.
82560750397SJohannes Berg 	 */
82660750397SJohannes Berg 	sta_info_recalc_tim(sta);
82760750397SJohannes Berg 
82860750397SJohannes Berg 	/*
82960750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
83060750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
83160750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
83260750397SJohannes Berg 	 */
833948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
834948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
835948d887dSJohannes Berg }
836948d887dSJohannes Berg 
837948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
838948d887dSJohannes Berg 					     struct sta_info *sta)
839948d887dSJohannes Berg {
840948d887dSJohannes Berg 	bool have_buffered = false;
841948d887dSJohannes Berg 	int ac;
842948d887dSJohannes Berg 
8433f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
8443f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
8453f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
846948d887dSJohannes Berg 		return false;
847948d887dSJohannes Berg 
848948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
849948d887dSJohannes Berg 		have_buffered |=
850948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
851948d887dSJohannes Berg 
852948d887dSJohannes Berg 	return have_buffered;
853f0706e82SJiri Benc }
854f0706e82SJiri Benc 
855d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
85634e89507SJohannes Berg {
85734e89507SJohannes Berg 	struct ieee80211_local *local;
85834e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8596d10e46bSJohannes Berg 	int ret;
86034e89507SJohannes Berg 
86134e89507SJohannes Berg 	might_sleep();
86234e89507SJohannes Berg 
86334e89507SJohannes Berg 	if (!sta)
86434e89507SJohannes Berg 		return -ENOENT;
86534e89507SJohannes Berg 
86634e89507SJohannes Berg 	local = sta->local;
86734e89507SJohannes Berg 	sdata = sta->sdata;
86834e89507SJohannes Berg 
86983d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
87083d5cc01SJohannes Berg 
871098a6070SJohannes Berg 	/*
872098a6070SJohannes Berg 	 * Before removing the station from the driver and
873098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
874098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
875098a6070SJohannes Berg 	 * will be sufficient.
876098a6070SJohannes Berg 	 */
877c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
878c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
879098a6070SJohannes Berg 
880f59374ebSSara Sharon 	/*
881f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
882f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
883f59374ebSSara Sharon 	 * all such frames to be processed.
884f59374ebSSara Sharon 	 */
885f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
886f59374ebSSara Sharon 
88734e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
888b01711beSJohannes Berg 	if (WARN_ON(ret))
88934e89507SJohannes Berg 		return ret;
89034e89507SJohannes Berg 
891a7a6bdd0SArik Nemtsov 	/*
892a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
893a7a6bdd0SArik Nemtsov 	 * removal.
894a7a6bdd0SArik Nemtsov 	 */
895a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
896a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
897a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
898a7a6bdd0SArik Nemtsov 	}
899a7a6bdd0SArik Nemtsov 
900794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
901ef044763SEliad Peller 	sta->removed = true;
9024d33960bSJohannes Berg 
9036a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
9046a9d1b91SJohannes Berg 
905a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
906a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
907a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
908a710c816SJohannes Berg 
909d778207bSJohannes Berg 	return 0;
910d778207bSJohannes Berg }
911d778207bSJohannes Berg 
912d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
913d778207bSJohannes Berg {
914d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
915d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
9160ef049dcSArnd Bergmann 	struct station_info *sinfo;
917d778207bSJohannes Berg 	int ret;
918d778207bSJohannes Berg 
919d778207bSJohannes Berg 	/*
920d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
921d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
922d778207bSJohannes Berg 	 */
923d778207bSJohannes Berg 
924d778207bSJohannes Berg 	might_sleep();
925d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
926d778207bSJohannes Berg 
927c8782078SJohannes Berg 	/* now keys can no longer be reached */
9286d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
92934e89507SJohannes Berg 
9309b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
9319b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
9329b7a86f3SJohannes Berg 
93334e89507SJohannes Berg 	sta->dead = true;
93434e89507SJohannes Berg 
93534e89507SJohannes Berg 	local->num_sta--;
93634e89507SJohannes Berg 	local->sta_generation++;
93734e89507SJohannes Berg 
93883d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
939f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
940f09603a2SJohannes Berg 		if (ret) {
94183d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
94283d5cc01SJohannes Berg 			break;
94383d5cc01SJohannes Berg 		}
94483d5cc01SJohannes Berg 	}
945d9a7ddb0SJohannes Berg 
946f09603a2SJohannes Berg 	if (sta->uploaded) {
947f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
948f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
949f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
950f09603a2SJohannes Berg 	}
95134e89507SJohannes Berg 
952bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
953bdcbd8e0SJohannes Berg 
9540ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
9550ef049dcSArnd Bergmann 	if (sinfo)
9560ef049dcSArnd Bergmann 		sta_set_sinfo(sta, sinfo);
9570ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
9580ef049dcSArnd Bergmann 	kfree(sinfo);
959ec15e68bSJouni Malinen 
96034e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
96134e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
96234e89507SJohannes Berg 
963d34ba216SJohannes Berg 	cleanup_single_sta(sta);
964d778207bSJohannes Berg }
965d778207bSJohannes Berg 
966d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
967d778207bSJohannes Berg {
968d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
969d778207bSJohannes Berg 
970d778207bSJohannes Berg 	if (err)
971d778207bSJohannes Berg 		return err;
972d778207bSJohannes Berg 
973d778207bSJohannes Berg 	synchronize_net();
974d778207bSJohannes Berg 
975d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
97634e89507SJohannes Berg 
97734e89507SJohannes Berg 	return 0;
97834e89507SJohannes Berg }
97934e89507SJohannes Berg 
98034e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
98134e89507SJohannes Berg {
98234e89507SJohannes Berg 	struct sta_info *sta;
98334e89507SJohannes Berg 	int ret;
98434e89507SJohannes Berg 
98534e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9867852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
98734e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
98834e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
98934e89507SJohannes Berg 
99034e89507SJohannes Berg 	return ret;
99134e89507SJohannes Berg }
99234e89507SJohannes Berg 
99334e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
99434e89507SJohannes Berg 			      const u8 *addr)
99534e89507SJohannes Berg {
99634e89507SJohannes Berg 	struct sta_info *sta;
99734e89507SJohannes Berg 	int ret;
99834e89507SJohannes Berg 
99934e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
10007852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
100134e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
100234e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
100334e89507SJohannes Berg 
100434e89507SJohannes Berg 	return ret;
100534e89507SJohannes Berg }
1006f0706e82SJiri Benc 
1007f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
1008f0706e82SJiri Benc {
1009f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
1010f0706e82SJiri Benc 	struct sta_info *sta;
10113393a608SJuuso Oikarinen 	bool timer_needed = false;
1012f0706e82SJiri Benc 
1013d0709a65SJohannes Berg 	rcu_read_lock();
1014d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
10153393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
10163393a608SJuuso Oikarinen 			timer_needed = true;
1017d0709a65SJohannes Berg 	rcu_read_unlock();
1018f0706e82SJiri Benc 
10195bb644a0SJohannes Berg 	if (local->quiescing)
10205bb644a0SJohannes Berg 		return;
10215bb644a0SJohannes Berg 
10223393a608SJuuso Oikarinen 	if (!timer_needed)
10233393a608SJuuso Oikarinen 		return;
10243393a608SJuuso Oikarinen 
102526d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
102626d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1027f0706e82SJiri Benc }
1028f0706e82SJiri Benc 
10297bedd0cfSJohannes Berg u32 sta_addr_hash(const void *key, u32 length, u32 seed)
1030f0706e82SJiri Benc {
10317bedd0cfSJohannes Berg 	return jhash(key, ETH_ALEN, seed);
10327bedd0cfSJohannes Berg }
10337bedd0cfSJohannes Berg 
10347bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
10357bedd0cfSJohannes Berg {
10367bedd0cfSJohannes Berg 	int err;
10377bedd0cfSJohannes Berg 
10387bedd0cfSJohannes Berg 	err = rhashtable_init(&local->sta_hash, &sta_rht_params);
10397bedd0cfSJohannes Berg 	if (err)
10407bedd0cfSJohannes Berg 		return err;
10417bedd0cfSJohannes Berg 
10424d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
104334e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1044f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1045f0706e82SJiri Benc 
1046b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
1047b24b8a24SPavel Emelyanov 		    (unsigned long)local);
10487bedd0cfSJohannes Berg 	return 0;
1049f0706e82SJiri Benc }
1050f0706e82SJiri Benc 
1051f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1052f0706e82SJiri Benc {
1053a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
10547bedd0cfSJohannes Berg 	rhashtable_destroy(&local->sta_hash);
1055f0706e82SJiri Benc }
1056f0706e82SJiri Benc 
1057051007d9SJohannes Berg 
1058e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1059f0706e82SJiri Benc {
1060b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1061f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1062d778207bSJohannes Berg 	LIST_HEAD(free_list);
106344213b5eSJohannes Berg 	int ret = 0;
1064f0706e82SJiri Benc 
1065d0709a65SJohannes Berg 	might_sleep();
1066d0709a65SJohannes Berg 
1067e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1068e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1069e716251dSJohannes Berg 
107034e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
107134e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1072e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1073e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1074d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1075d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
107634316837SJohannes Berg 			ret++;
107734316837SJohannes Berg 		}
107834e89507SJohannes Berg 	}
1079d778207bSJohannes Berg 
1080d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1081d778207bSJohannes Berg 		synchronize_net();
1082d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1083d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1084d778207bSJohannes Berg 	}
108534e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
108644213b5eSJohannes Berg 
1087051007d9SJohannes Berg 	return ret;
1088051007d9SJohannes Berg }
1089051007d9SJohannes Berg 
109024723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
109124723d1bSJohannes Berg 			  unsigned long exp_time)
109224723d1bSJohannes Berg {
109324723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
109424723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
109524723d1bSJohannes Berg 
109634e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1097e46a2cf9SMohammed Shafi Shajakhan 
1098e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1099b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1100b8da6b6aSJohannes Berg 
1101ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1102ec2b774eSMarek Lindner 			continue;
1103ec2b774eSMarek Lindner 
1104b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1105eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1106bdcbd8e0SJohannes Berg 				sta->sta.addr);
11073f52b7e3SMarco Porsch 
11083f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
11093f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
11103f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
11113f52b7e3SMarco Porsch 
111234e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
111324723d1bSJohannes Berg 		}
1114e46a2cf9SMohammed Shafi Shajakhan 	}
1115e46a2cf9SMohammed Shafi Shajakhan 
111634e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
111724723d1bSJohannes Berg }
111817741cdcSJohannes Berg 
1119686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1120686b9cb9SBen Greear 						   const u8 *addr,
1121686b9cb9SBen Greear 						   const u8 *localaddr)
112217741cdcSJohannes Berg {
11237bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
11247bedd0cfSJohannes Berg 	struct sta_info *sta;
11257bedd0cfSJohannes Berg 	struct rhash_head *tmp;
11267bedd0cfSJohannes Berg 	const struct bucket_table *tbl;
11277bedd0cfSJohannes Berg 
11287bedd0cfSJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
112917741cdcSJohannes Berg 
1130686b9cb9SBen Greear 	/*
1131686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1132686b9cb9SBen Greear 	 * ... first in list.
1133686b9cb9SBen Greear 	 */
11347bedd0cfSJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
1135686b9cb9SBen Greear 		if (localaddr &&
1136b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1137686b9cb9SBen Greear 			continue;
1138f7c65594SJohannes Berg 		if (!sta->uploaded)
1139f7c65594SJohannes Berg 			return NULL;
114017741cdcSJohannes Berg 		return &sta->sta;
1141f7c65594SJohannes Berg 	}
1142f7c65594SJohannes Berg 
1143abe60632SJohannes Berg 	return NULL;
114417741cdcSJohannes Berg }
1145686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
11465ed176e1SJohannes Berg 
11475ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
11485ed176e1SJohannes Berg 					 const u8 *addr)
11495ed176e1SJohannes Berg {
1150f7c65594SJohannes Berg 	struct sta_info *sta;
11515ed176e1SJohannes Berg 
11525ed176e1SJohannes Berg 	if (!vif)
11535ed176e1SJohannes Berg 		return NULL;
11545ed176e1SJohannes Berg 
1155f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1156f7c65594SJohannes Berg 	if (!sta)
1157f7c65594SJohannes Berg 		return NULL;
11585ed176e1SJohannes Berg 
1159f7c65594SJohannes Berg 	if (!sta->uploaded)
1160f7c65594SJohannes Berg 		return NULL;
1161f7c65594SJohannes Berg 
1162f7c65594SJohannes Berg 	return &sta->sta;
11635ed176e1SJohannes Berg }
116417741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1165af818581SJohannes Berg 
1166e3685e03SJohannes Berg /* powersave support code */
1167e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
116850a9432dSJohannes Berg {
1169608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1170e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1171e3685e03SJohannes Berg 	struct sk_buff_head pending;
1172ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1173e3685e03SJohannes Berg 	unsigned long flags;
1174d012a605SMarco Porsch 	struct ps_data *ps;
1175d012a605SMarco Porsch 
11763918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
11773918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
11783918edb0SFelix Fietkau 				     u.ap);
11793918edb0SFelix Fietkau 
11803918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1181d012a605SMarco Porsch 		ps = &sdata->bss->ps;
11823f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
11833f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1184d012a605SMarco Porsch 	else
1185d012a605SMarco Porsch 		return;
118650a9432dSJohannes Berg 
1187c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
118847086fc5SJohannes Berg 
11895a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1190948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1191ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1192948d887dSJohannes Berg 
119330686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
119412375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1195af818581SJohannes Berg 
1196ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
1197ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1198ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1199ba8c3d6fSFelix Fietkau 
1200ba8c3d6fSFelix Fietkau 			if (!skb_queue_len(&txqi->queue))
1201ba8c3d6fSFelix Fietkau 				continue;
1202ba8c3d6fSFelix Fietkau 
1203ba8c3d6fSFelix Fietkau 			drv_wake_tx_queue(local, txqi);
1204ba8c3d6fSFelix Fietkau 		}
1205ba8c3d6fSFelix Fietkau 	}
1206ba8c3d6fSFelix Fietkau 
1207948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1208af818581SJohannes Berg 
12091d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
12101d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1211af818581SJohannes Berg 	/* Send all buffered frames to the station */
1212948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1213948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1214948d887dSJohannes Berg 
1215987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1216948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1217987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1218948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1219948d887dSJohannes Berg 		filtered += tmp - count;
1220948d887dSJohannes Berg 		count = tmp;
1221948d887dSJohannes Berg 
1222987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1223948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1224987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1225948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1226948d887dSJohannes Berg 		buffered += tmp - count;
1227948d887dSJohannes Berg 	}
1228948d887dSJohannes Berg 
1229e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
12305ac2e350SJohannes Berg 
12315ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
12325ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
12335ac2e350SJohannes Berg 
12345ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
12355ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
12365ac2e350SJohannes Berg 	 */
12375ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
12385ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
12391d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1240948d887dSJohannes Berg 
1241e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1242e3685e03SJohannes Berg 
1243687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1244062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1245062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1246687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1247687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1248687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1249687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1250687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1251687da132SEmmanuel Grumbach 		       sta->sta.addr);
1252687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1253687da132SEmmanuel Grumbach 					   sta->sta.addr,
1254687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1255687da132SEmmanuel Grumbach 	}
1256687da132SEmmanuel Grumbach 
1257af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1258af818581SJohannes Berg 
1259c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1260c868cb35SJohannes Berg 
1261bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1262bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1263bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
126417c18bf8SJohannes Berg 
126517c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1266af818581SJohannes Berg }
1267af818581SJohannes Berg 
12680ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1269b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
12700ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1271ce662b44SJohannes Berg {
12720ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1273ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1274ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1275ce662b44SJohannes Berg 	struct sk_buff *skb;
1276ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1277ce662b44SJohannes Berg 	__le16 fc;
1278a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1279ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
128055de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1281ce662b44SJohannes Berg 
1282ce662b44SJohannes Berg 	if (qos) {
1283ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1284ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1285ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1286ce662b44SJohannes Berg 	} else {
1287ce662b44SJohannes Berg 		size -= 2;
1288ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1289ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1290ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1291ce662b44SJohannes Berg 	}
1292ce662b44SJohannes Berg 
1293ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1294ce662b44SJohannes Berg 	if (!skb)
1295ce662b44SJohannes Berg 		return;
1296ce662b44SJohannes Berg 
1297ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1298ce662b44SJohannes Berg 
1299ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1300ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1301ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1302ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1303ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1304ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1305864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1306ce662b44SJohannes Berg 
1307ce662b44SJohannes Berg 	skb->priority = tid;
1308ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
130959b66255SJohannes Berg 	if (qos) {
1310ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1311ce662b44SJohannes Berg 
13120ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1313ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1314ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
13150ead2510SEmmanuel Grumbach 			if (more_data)
13160ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
13170ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
13180ead2510SEmmanuel Grumbach 		}
1319ce662b44SJohannes Berg 	}
1320ce662b44SJohannes Berg 
1321ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1322ce662b44SJohannes Berg 
1323ce662b44SJohannes Berg 	/*
1324ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1325ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1326deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1327deeaee19SJohannes Berg 	 * ends the poll/service period.
1328ce662b44SJohannes Berg 	 */
132902f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1330deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1331ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1332ce662b44SJohannes Berg 
13336b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
13346b127c71SSujith Manoharan 
1335b77cf4f8SJohannes Berg 	if (call_driver)
1336b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1337b77cf4f8SJohannes Berg 					  reason, false);
133840b96408SJohannes Berg 
133989afe614SJohannes Berg 	skb->dev = sdata->dev;
134089afe614SJohannes Berg 
134155de908aSJohannes Berg 	rcu_read_lock();
134255de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
134355de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
134455de908aSJohannes Berg 		rcu_read_unlock();
134555de908aSJohannes Berg 		kfree_skb(skb);
134655de908aSJohannes Berg 		return;
134755de908aSJohannes Berg 	}
134855de908aSJohannes Berg 
134973c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
13507c10770fSJohannes Berg 	ieee80211_xmit(sdata, sta, skb);
135155de908aSJohannes Berg 	rcu_read_unlock();
1352ce662b44SJohannes Berg }
1353ce662b44SJohannes Berg 
13540a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
13550a1cb809SJohannes Berg {
13560a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
13570a1cb809SJohannes Berg 	if (tids & 0xF8)
13580a1cb809SJohannes Berg 		return fls(tids) - 1;
13590a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
13600a1cb809SJohannes Berg 	if (tids & BIT(0))
13610a1cb809SJohannes Berg 		return 0;
13620a1cb809SJohannes Berg 	return fls(tids) - 1;
13630a1cb809SJohannes Berg }
13640a1cb809SJohannes Berg 
13650ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
13660ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
13670ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
13680ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
13690ead2510SEmmanuel Grumbach  */
13700ead2510SEmmanuel Grumbach static bool
13710ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
13720ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
13730ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
13740ead2510SEmmanuel Grumbach {
13750ead2510SEmmanuel Grumbach 	int ac;
13760ead2510SEmmanuel Grumbach 
13770ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
13780ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
13790ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
13800ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
13810ead2510SEmmanuel Grumbach 	 */
13820ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
13830ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
13840ead2510SEmmanuel Grumbach 		return true;
13850ead2510SEmmanuel Grumbach 
13860ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
13870ead2510SEmmanuel Grumbach 		if (ignored_acs & BIT(ac))
13880ead2510SEmmanuel Grumbach 			continue;
13890ead2510SEmmanuel Grumbach 
13900ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
13910ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
13920ead2510SEmmanuel Grumbach 			return true;
13930ead2510SEmmanuel Grumbach 	}
13940ead2510SEmmanuel Grumbach 
13950ead2510SEmmanuel Grumbach 	return false;
13960ead2510SEmmanuel Grumbach }
13970ead2510SEmmanuel Grumbach 
139847086fc5SJohannes Berg static void
13990ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
14000ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
14010ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
14020ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1403af818581SJohannes Berg {
1404af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1405af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1406948d887dSJohannes Berg 	int ac;
1407af818581SJohannes Berg 
1408f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1409948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
14104049e09aSJohannes Berg 		unsigned long tids;
14114049e09aSJohannes Berg 
141247086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1413948d887dSJohannes Berg 			continue;
1414948d887dSJohannes Berg 
14154049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
14164049e09aSJohannes Berg 
1417f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1418f9f760b4SJohannes Berg 		 * release from hardware queues
1419f9f760b4SJohannes Berg 		 */
14200ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
14210ead2510SEmmanuel Grumbach 			*driver_release_tids |=
14220ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
14230ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1424ba8c3d6fSFelix Fietkau 		}
1425f9f760b4SJohannes Berg 
14260ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
142747086fc5SJohannes Berg 			struct sk_buff *skb;
142847086fc5SJohannes Berg 
142947086fc5SJohannes Berg 			while (n_frames > 0) {
1430948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1431948d887dSJohannes Berg 				if (!skb) {
143247086fc5SJohannes Berg 					skb = skb_dequeue(
143347086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1434af818581SJohannes Berg 					if (skb)
1435af818581SJohannes Berg 						local->total_ps_buffered--;
1436af818581SJohannes Berg 				}
143747086fc5SJohannes Berg 				if (!skb)
143847086fc5SJohannes Berg 					break;
143947086fc5SJohannes Berg 				n_frames--;
14400ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
144147086fc5SJohannes Berg 			}
1442948d887dSJohannes Berg 		}
1443948d887dSJohannes Berg 
14440ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
14450ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
14460ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
14474049e09aSJohannes Berg 		 */
1448948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
14490ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1450948d887dSJohannes Berg 			break;
1451948d887dSJohannes Berg 	}
1452948d887dSJohannes Berg }
1453af818581SJohannes Berg 
14540ead2510SEmmanuel Grumbach static void
14550ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
14560ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
14570ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
14580ead2510SEmmanuel Grumbach {
14590ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
14600ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
14610ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
14620ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
14630ead2510SEmmanuel Grumbach 	bool more_data;
14640ead2510SEmmanuel Grumbach 
14650ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
14660ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
14670ead2510SEmmanuel Grumbach 
14680ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
14690ead2510SEmmanuel Grumbach 
14700ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
14710ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
14720ead2510SEmmanuel Grumbach 
14730ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
14740ead2510SEmmanuel Grumbach 
14751a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
14760ead2510SEmmanuel Grumbach 		driver_release_tids =
14770ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
14780ead2510SEmmanuel Grumbach 
1479f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1480ce662b44SJohannes Berg 		int tid;
14814049e09aSJohannes Berg 
1482ce662b44SJohannes Berg 		/*
1483ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1484ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1485ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1486ce662b44SJohannes Berg 		 *
1487ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1488ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1489ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1490ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1491ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1492ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1493ce662b44SJohannes Berg 		 *
1494ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1495ce662b44SJohannes Berg 		 */
1496ce662b44SJohannes Berg 
1497ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1498ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1499ce662b44SJohannes Berg 
15000ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1501f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
150247086fc5SJohannes Berg 		struct sk_buff_head pending;
150347086fc5SJohannes Berg 		struct sk_buff *skb;
150440b96408SJohannes Berg 		int num = 0;
150540b96408SJohannes Berg 		u16 tids = 0;
1506b77cf4f8SJohannes Berg 		bool need_null = false;
150747086fc5SJohannes Berg 
150847086fc5SJohannes Berg 		skb_queue_head_init(&pending);
150947086fc5SJohannes Berg 
151047086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1511af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
151247086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
151340b96408SJohannes Berg 			u8 *qoshdr = NULL;
151440b96408SJohannes Berg 
151540b96408SJohannes Berg 			num++;
1516af818581SJohannes Berg 
1517af818581SJohannes Berg 			/*
151847086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
151947086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
152047086fc5SJohannes Berg 			 * exchange.
1521af818581SJohannes Berg 			 */
15226b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
15236b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1524af818581SJohannes Berg 
152547086fc5SJohannes Berg 			/*
152647086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
152747086fc5SJohannes Berg 			 * more buffered frames for this STA
152847086fc5SJohannes Berg 			 */
152924b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
153047086fc5SJohannes Berg 				hdr->frame_control |=
153147086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
153224b9c373SJanusz.Dziedzic@tieto.com 			else
153324b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
153424b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1535af818581SJohannes Berg 
153640b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
153740b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
153840b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
153940b96408SJohannes Berg 
1540b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1541b77cf4f8SJohannes Berg 
1542b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1543b77cf4f8SJohannes Berg 
1544b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1545b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1546b77cf4f8SJohannes Berg 				continue;
1547b77cf4f8SJohannes Berg 
1548b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1549b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1550b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1551b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1552b77cf4f8SJohannes Berg 				break;
1553b77cf4f8SJohannes Berg 			}
1554b77cf4f8SJohannes Berg 
1555b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1556b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1557b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1558b77cf4f8SJohannes Berg 			 * and be done.
1559b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1560b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1561b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1562b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1563b77cf4f8SJohannes Berg 			 *
1564b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1565b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1566b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1567b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1568b77cf4f8SJohannes Berg 			 *
1569b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1570b77cf4f8SJohannes Berg 			 */
1571b77cf4f8SJohannes Berg 			if (qoshdr) {
157240b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1573deeaee19SJohannes Berg 
157447086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
157547086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1576b77cf4f8SJohannes Berg 			} else {
1577b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1578b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1579b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1580b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1581b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1582b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1583b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1584b77cf4f8SJohannes Berg 				 */
1585b77cf4f8SJohannes Berg 				hdr->frame_control |=
1586b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1587b77cf4f8SJohannes Berg 				need_null = true;
1588b77cf4f8SJohannes Berg 				num++;
158952a3f20cSMarco Porsch 			}
1590b77cf4f8SJohannes Berg 			break;
159147086fc5SJohannes Berg 		}
159247086fc5SJohannes Berg 
159340b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
159440b96408SJohannes Berg 					  reason, more_data);
159540b96408SJohannes Berg 
159647086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1597af818581SJohannes Berg 
1598b77cf4f8SJohannes Berg 		if (need_null)
1599b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
16000ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
16010ead2510SEmmanuel Grumbach 				reason, false, false);
1602b77cf4f8SJohannes Berg 
1603c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1604af818581SJohannes Berg 	} else {
1605ba8c3d6fSFelix Fietkau 		unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1606ba8c3d6fSFelix Fietkau 		int tid;
1607ba8c3d6fSFelix Fietkau 
1608af818581SJohannes Berg 		/*
16094049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
16104049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1611f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1612f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1613f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1614f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1615f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1616f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1617af818581SJohannes Berg 		 */
16184049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
161947086fc5SJohannes Berg 					    n_frames, reason, more_data);
16204049e09aSJohannes Berg 
16214049e09aSJohannes Berg 		/*
16224049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
16234049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1624f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
16254049e09aSJohannes Berg 		 * release function.
1626f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1627ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1628ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
16294049e09aSJohannes Berg 		 */
1630ba8c3d6fSFelix Fietkau 
1631ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1632ba8c3d6fSFelix Fietkau 			return;
1633ba8c3d6fSFelix Fietkau 
1634ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1635ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1636ba8c3d6fSFelix Fietkau 
1637ba8c3d6fSFelix Fietkau 			if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1638ba8c3d6fSFelix Fietkau 				continue;
1639ba8c3d6fSFelix Fietkau 
1640ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1641ba8c3d6fSFelix Fietkau 			break;
1642ba8c3d6fSFelix Fietkau 		}
1643af818581SJohannes Berg 	}
1644af818581SJohannes Berg }
1645af818581SJohannes Berg 
1646af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1647af818581SJohannes Berg {
164847086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1649af818581SJohannes Berg 
1650af818581SJohannes Berg 	/*
165147086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
165247086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
165347086fc5SJohannes Berg 	 * only from the non-enabled ones.
1654af818581SJohannes Berg 	 */
165547086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
165647086fc5SJohannes Berg 		ignore_for_response = 0;
1657af818581SJohannes Berg 
165847086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
165947086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1660af818581SJohannes Berg }
166147086fc5SJohannes Berg 
166247086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
166347086fc5SJohannes Berg {
166447086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
166547086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
166647086fc5SJohannes Berg 
166747086fc5SJohannes Berg 	/*
166847086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
166947086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
167047086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
167147086fc5SJohannes Berg 	 * actually getting called.
167247086fc5SJohannes Berg 	 */
167347086fc5SJohannes Berg 	if (!delivery_enabled)
167447086fc5SJohannes Berg 		return;
167547086fc5SJohannes Berg 
167647086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
167747086fc5SJohannes Berg 	case 1:
167847086fc5SJohannes Berg 		n_frames = 2;
167947086fc5SJohannes Berg 		break;
168047086fc5SJohannes Berg 	case 2:
168147086fc5SJohannes Berg 		n_frames = 4;
168247086fc5SJohannes Berg 		break;
168347086fc5SJohannes Berg 	case 3:
168447086fc5SJohannes Berg 		n_frames = 6;
168547086fc5SJohannes Berg 		break;
168647086fc5SJohannes Berg 	case 0:
168747086fc5SJohannes Berg 		/* XXX: what is a good value? */
168813a8098aSAndrei Otcheretianski 		n_frames = 128;
168947086fc5SJohannes Berg 		break;
169047086fc5SJohannes Berg 	}
169147086fc5SJohannes Berg 
169247086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
169347086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1694af818581SJohannes Berg }
1695af818581SJohannes Berg 
1696af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1697af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1698af818581SJohannes Berg {
1699af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1700af818581SJohannes Berg 
1701b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1702b5878a2dSJohannes Berg 
17035ac2e350SJohannes Berg 	if (block) {
1704c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
170517c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
17065ac2e350SJohannes Berg 		return;
17075ac2e350SJohannes Berg 	}
17085ac2e350SJohannes Berg 
17095ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
17105ac2e350SJohannes Berg 		return;
17115ac2e350SJohannes Berg 
17125ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
17135ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
17145ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
17155ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
17165ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
17175ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
17185ac2e350SJohannes Berg 		/* must be asleep in this case */
17195ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
17205ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
17215ac2e350SJohannes Berg 	} else {
17225ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
172317c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
17245ac2e350SJohannes Berg 	}
1725af818581SJohannes Berg }
1726af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1727dcf55fb5SFelix Fietkau 
1728e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
172937fbd908SJohannes Berg {
173037fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
173137fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
173237fbd908SJohannes Berg 
173337fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
173437fbd908SJohannes Berg 
173537fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
173637fbd908SJohannes Berg }
1737e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
173837fbd908SJohannes Berg 
17390ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
17400ead2510SEmmanuel Grumbach {
17410ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
17420ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
17430ead2510SEmmanuel Grumbach 	bool more_data;
17440ead2510SEmmanuel Grumbach 
17450ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
17460ead2510SEmmanuel Grumbach 
17470ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
17480ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
17490ead2510SEmmanuel Grumbach 					       reason, 0);
17500ead2510SEmmanuel Grumbach 
17510ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
17520ead2510SEmmanuel Grumbach }
17530ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
17540ead2510SEmmanuel Grumbach 
1755042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1756042ec453SJohannes Berg 				u8 tid, bool buffered)
1757dcf55fb5SFelix Fietkau {
1758dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1759dcf55fb5SFelix Fietkau 
17605a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1761042ec453SJohannes Berg 		return;
1762042ec453SJohannes Berg 
17631b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
17641b000789SJohannes Berg 
1765948d887dSJohannes Berg 	if (buffered)
1766948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1767948d887dSJohannes Berg 	else
1768948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1769948d887dSJohannes Berg 
1770c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1771dcf55fb5SFelix Fietkau }
1772042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1773d9a7ddb0SJohannes Berg 
177452cfa1d6SAyala Beker static void
177552cfa1d6SAyala Beker ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
177652cfa1d6SAyala Beker {
177752cfa1d6SAyala Beker 	struct ieee80211_local *local = sdata->local;
177852cfa1d6SAyala Beker 	bool allow_p2p_go_ps = sdata->vif.p2p;
177952cfa1d6SAyala Beker 	struct sta_info *sta;
178052cfa1d6SAyala Beker 
178152cfa1d6SAyala Beker 	rcu_read_lock();
178252cfa1d6SAyala Beker 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
178352cfa1d6SAyala Beker 		if (sdata != sta->sdata ||
178452cfa1d6SAyala Beker 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
178552cfa1d6SAyala Beker 			continue;
178652cfa1d6SAyala Beker 		if (!sta->sta.support_p2p_ps) {
178752cfa1d6SAyala Beker 			allow_p2p_go_ps = false;
178852cfa1d6SAyala Beker 			break;
178952cfa1d6SAyala Beker 		}
179052cfa1d6SAyala Beker 	}
179152cfa1d6SAyala Beker 	rcu_read_unlock();
179252cfa1d6SAyala Beker 
179352cfa1d6SAyala Beker 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
179452cfa1d6SAyala Beker 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
179552cfa1d6SAyala Beker 		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
179652cfa1d6SAyala Beker 	}
179752cfa1d6SAyala Beker }
179852cfa1d6SAyala Beker 
179983d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1800d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1801d9a7ddb0SJohannes Berg {
18028bf11d8dSJohannes Berg 	might_sleep();
1803d9a7ddb0SJohannes Berg 
1804d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1805d9a7ddb0SJohannes Berg 		return 0;
1806d9a7ddb0SJohannes Berg 
1807f09603a2SJohannes Berg 	/* check allowed transitions first */
1808f09603a2SJohannes Berg 
1809d9a7ddb0SJohannes Berg 	switch (new_state) {
1810d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1811f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1812d9a7ddb0SJohannes Berg 			return -EINVAL;
1813d9a7ddb0SJohannes Berg 		break;
1814d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1815f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1816f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1817d9a7ddb0SJohannes Berg 			return -EINVAL;
1818d9a7ddb0SJohannes Berg 		break;
1819d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1820f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1821f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1822d9a7ddb0SJohannes Berg 			return -EINVAL;
1823d9a7ddb0SJohannes Berg 		break;
1824d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1825f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1826d9a7ddb0SJohannes Berg 			return -EINVAL;
1827d9a7ddb0SJohannes Berg 		break;
1828d9a7ddb0SJohannes Berg 	default:
1829d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1830d9a7ddb0SJohannes Berg 		return -EINVAL;
1831d9a7ddb0SJohannes Berg 	}
1832d9a7ddb0SJohannes Berg 
1833bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1834bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1835f09603a2SJohannes Berg 
1836f09603a2SJohannes Berg 	/*
1837f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1838f09603a2SJohannes Berg 	 * fail the transition
1839f09603a2SJohannes Berg 	 */
1840f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1841f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1842f09603a2SJohannes Berg 					sta->sta_state, new_state);
1843f09603a2SJohannes Berg 		if (err)
1844f09603a2SJohannes Berg 			return err;
1845f09603a2SJohannes Berg 	}
1846f09603a2SJohannes Berg 
1847f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1848f09603a2SJohannes Berg 
1849f09603a2SJohannes Berg 	switch (new_state) {
1850f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1851f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1852f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1853f09603a2SJohannes Berg 		break;
1854f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1855a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
1856f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1857a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1858f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1859a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
186052cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
186152cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1862a7201a6cSIlan Peer 		}
1863f09603a2SJohannes Berg 		break;
1864f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1865f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1866f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1867a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
186852cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
186952cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1870f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
18717e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
18727e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
18737e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
18747e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1875f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
187617c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
1877*49ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
1878f09603a2SJohannes Berg 		}
1879f09603a2SJohannes Berg 		break;
1880f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1881f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
18827e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
18837e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
18847e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
18857e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1886f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
188717c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
1888*49ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
1889f09603a2SJohannes Berg 		}
1890f09603a2SJohannes Berg 		break;
1891f09603a2SJohannes Berg 	default:
1892f09603a2SJohannes Berg 		break;
1893f09603a2SJohannes Berg 	}
1894f09603a2SJohannes Berg 
1895d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1896d9a7ddb0SJohannes Berg 
1897d9a7ddb0SJohannes Berg 	return 0;
1898d9a7ddb0SJohannes Berg }
1899687da132SEmmanuel Grumbach 
1900687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1901687da132SEmmanuel Grumbach {
1902687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1903687da132SEmmanuel Grumbach 	u8 rx_streams;
1904687da132SEmmanuel Grumbach 
1905687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1906687da132SEmmanuel Grumbach 		return 1;
1907687da132SEmmanuel Grumbach 
1908687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1909687da132SEmmanuel Grumbach 		int i;
1910687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1911687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1912687da132SEmmanuel Grumbach 
1913687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1914687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1915687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1916687da132SEmmanuel Grumbach 				return i + 1;
1917687da132SEmmanuel Grumbach 	}
1918687da132SEmmanuel Grumbach 
1919687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1920687da132SEmmanuel Grumbach 		rx_streams = 4;
1921687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1922687da132SEmmanuel Grumbach 		rx_streams = 3;
1923687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1924687da132SEmmanuel Grumbach 		rx_streams = 2;
1925687da132SEmmanuel Grumbach 	else
1926687da132SEmmanuel Grumbach 		rx_streams = 1;
1927687da132SEmmanuel Grumbach 
1928687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1929687da132SEmmanuel Grumbach 		return rx_streams;
1930687da132SEmmanuel Grumbach 
1931687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1932687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1933687da132SEmmanuel Grumbach }
1934b7ffbd7eSJohannes Berg 
19354f6b1b3dSJohannes Berg static void sta_stats_decode_rate(struct ieee80211_local *local, u16 rate,
19364f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
1937fbd6ff5cSJohannes Berg {
19384f6b1b3dSJohannes Berg 	rinfo->bw = (rate & STA_STATS_RATE_BW_MASK) >>
19394f6b1b3dSJohannes Berg 		STA_STATS_RATE_BW_SHIFT;
1940fbd6ff5cSJohannes Berg 
19414f6b1b3dSJohannes Berg 	if (rate & STA_STATS_RATE_VHT) {
19424f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
19434f6b1b3dSJohannes Berg 		rinfo->mcs = rate & 0xf;
19444f6b1b3dSJohannes Berg 		rinfo->nss = (rate & 0xf0) >> 4;
19454f6b1b3dSJohannes Berg 	} else if (rate & STA_STATS_RATE_HT) {
19464f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
19474f6b1b3dSJohannes Berg 		rinfo->mcs = rate & 0xff;
19484f6b1b3dSJohannes Berg 	} else if (rate & STA_STATS_RATE_LEGACY) {
1949fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
1950fbd6ff5cSJohannes Berg 		u16 brate;
19514f6b1b3dSJohannes Berg 		unsigned int shift;
1952fbd6ff5cSJohannes Berg 
19534f6b1b3dSJohannes Berg 		sband = local->hw.wiphy->bands[(rate >> 4) & 0xf];
19544f6b1b3dSJohannes Berg 		brate = sband->bitrates[rate & 0xf].bitrate;
19554f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
19564f6b1b3dSJohannes Berg 			shift = 2;
19574f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
19584f6b1b3dSJohannes Berg 			shift = 1;
19594f6b1b3dSJohannes Berg 		else
19604f6b1b3dSJohannes Berg 			shift = 0;
1961fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
1962fbd6ff5cSJohannes Berg 	}
1963fbd6ff5cSJohannes Berg 
19644f6b1b3dSJohannes Berg 	if (rate & STA_STATS_RATE_SGI)
1965fbd6ff5cSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
19664f6b1b3dSJohannes Berg }
1967fbd6ff5cSJohannes Berg 
19684f6b1b3dSJohannes Berg static void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
19694f6b1b3dSJohannes Berg {
19704f6b1b3dSJohannes Berg 	u16 rate = ACCESS_ONCE(sta->rx_stats.last_rate);
19714f6b1b3dSJohannes Berg 
19724f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
19734f6b1b3dSJohannes Berg 		rinfo->flags = 0;
1974fbd6ff5cSJohannes Berg 	else
19754f6b1b3dSJohannes Berg 		sta_stats_decode_rate(sta->local, rate, rinfo);
1976fbd6ff5cSJohannes Berg }
1977fbd6ff5cSJohannes Berg 
19780f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
19790f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
19800f9c5a61SJohannes Berg 			     int tid)
19810f9c5a61SJohannes Berg {
19820f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
19830f9c5a61SJohannes Berg 
19840f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
19850f9c5a61SJohannes Berg 		unsigned int start;
19860f9c5a61SJohannes Berg 
19870f9c5a61SJohannes Berg 		do {
19880f9c5a61SJohannes Berg 			start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
19890f9c5a61SJohannes Berg 			tidstats->rx_msdu = sta->rx_stats.msdu[tid];
19900f9c5a61SJohannes Berg 		} while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
19910f9c5a61SJohannes Berg 
19920f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
19930f9c5a61SJohannes Berg 	}
19940f9c5a61SJohannes Berg 
19950f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
19960f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
19970f9c5a61SJohannes Berg 		tidstats->tx_msdu = sta->tx_stats.msdu[tid];
19980f9c5a61SJohannes Berg 	}
19990f9c5a61SJohannes Berg 
20000f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
20010f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
20020f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
20030f9c5a61SJohannes Berg 		tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
20040f9c5a61SJohannes Berg 	}
20050f9c5a61SJohannes Berg 
20060f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
20070f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
20080f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
20090f9c5a61SJohannes Berg 		tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
20100f9c5a61SJohannes Berg 	}
20110f9c5a61SJohannes Berg }
20120f9c5a61SJohannes Berg 
2013b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
2014b7ffbd7eSJohannes Berg {
2015b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2016b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
20179a244409SJohn W. Linville 	struct rate_control_ref *ref = NULL;
2018b7ffbd7eSJohannes Berg 	u32 thr = 0;
2019b7ffbd7eSJohannes Berg 	int i, ac;
2020b7ffbd7eSJohannes Berg 
20219a244409SJohn W. Linville 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
20229a244409SJohn W. Linville 		ref = local->rate_ctrl;
20239a244409SJohn W. Linville 
2024b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2025b7ffbd7eSJohannes Berg 
2026225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2027225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2028225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2029225b8189SJohannes Berg 	 */
2030225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2031225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2032225b8189SJohannes Berg 
20332b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
20342b9a7e1bSJohannes Berg 
2035319090bfSJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
2036319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_STA_FLAGS) |
2037319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BSS_PARAM) |
2038319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
2039976bd9efSJohannes Berg 			 BIT(NL80211_STA_INFO_RX_DROP_MISC);
2040976bd9efSJohannes Berg 
2041976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2042976bd9efSJohannes Berg 		sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2043976bd9efSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
2044976bd9efSJohannes Berg 	}
2045b7ffbd7eSJohannes Berg 
204684b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2047e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2048b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
20492b9a7e1bSJohannes Berg 
2050319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
2051319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_TX_BYTES)))) {
2052b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
20532b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2054e5a9f8d0SJohannes Berg 			sinfo->tx_bytes += sta->tx_stats.bytes[ac];
2055319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
2056b7ffbd7eSJohannes Berg 	}
20572b9a7e1bSJohannes Berg 
2058319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
20592b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
20602b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2061e5a9f8d0SJohannes Berg 			sinfo->tx_packets += sta->tx_stats.packets[ac];
2062319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
20632b9a7e1bSJohannes Berg 	}
20642b9a7e1bSJohannes Berg 
2065319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
2066319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_RX_BYTES)))) {
20670f9c5a61SJohannes Berg 		unsigned int start;
20680f9c5a61SJohannes Berg 
20690f9c5a61SJohannes Berg 		do {
20700f9c5a61SJohannes Berg 			start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
2071e5a9f8d0SJohannes Berg 			sinfo->rx_bytes = sta->rx_stats.bytes;
20720f9c5a61SJohannes Berg 		} while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
2073319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
20742b9a7e1bSJohannes Berg 	}
20752b9a7e1bSJohannes Berg 
2076319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
2077e5a9f8d0SJohannes Berg 		sinfo->rx_packets = sta->rx_stats.packets;
2078319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
20792b9a7e1bSJohannes Berg 	}
20802b9a7e1bSJohannes Berg 
2081319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
2082e5a9f8d0SJohannes Berg 		sinfo->tx_retries = sta->status_stats.retry_count;
2083319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
20842b9a7e1bSJohannes Berg 	}
20852b9a7e1bSJohannes Berg 
2086319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
2087e5a9f8d0SJohannes Berg 		sinfo->tx_failed = sta->status_stats.retry_failed;
2088319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
20892b9a7e1bSJohannes Berg 	}
20902b9a7e1bSJohannes Berg 
2091e5a9f8d0SJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_stats.dropped;
2092b7ffbd7eSJohannes Berg 
2093225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2094225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2095225b8189SJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
2096225b8189SJohannes Berg 				 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2097225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2098225b8189SJohannes Berg 	}
2099225b8189SJohannes Berg 
210030686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
210130686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2102319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
2103e5a9f8d0SJohannes Berg 			sinfo->signal = (s8)sta->rx_stats.last_signal;
2104319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
2105b7ffbd7eSJohannes Berg 		}
21062b9a7e1bSJohannes Berg 
2107319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
210840d9a38aSJohannes Berg 			sinfo->signal_avg =
21090be6ed13SJohannes Berg 				-ewma_signal_read(&sta->rx_stats_avg.signal);
2110319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
21112b9a7e1bSJohannes Berg 		}
21122b9a7e1bSJohannes Berg 	}
21132b9a7e1bSJohannes Berg 
2114e5a9f8d0SJohannes Berg 	if (sta->rx_stats.chains &&
2115319090bfSJohannes Berg 	    !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2116319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2117319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2118319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2119b7ffbd7eSJohannes Berg 
2120e5a9f8d0SJohannes Berg 		sinfo->chains = sta->rx_stats.chains;
2121b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2122e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2123e5a9f8d0SJohannes Berg 				sta->rx_stats.chain_signal_last[i];
2124b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
21250be6ed13SJohannes Berg 				-ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
2126b7ffbd7eSJohannes Berg 		}
2127b7ffbd7eSJohannes Berg 	}
2128b7ffbd7eSJohannes Berg 
2129319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
2130e5a9f8d0SJohannes Berg 		sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2131e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2132319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
21332b9a7e1bSJohannes Berg 	}
21342b9a7e1bSJohannes Berg 
2135319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
2136b7ffbd7eSJohannes Berg 		sta_set_rate_info_rx(sta, &sinfo->rxrate);
2137319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
21382b9a7e1bSJohannes Berg 	}
2139b7ffbd7eSJohannes Berg 
214079c892b8SJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
214179c892b8SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
214279c892b8SJohannes Berg 		struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
214379c892b8SJohannes Berg 
21440f9c5a61SJohannes Berg 		sta_set_tidstats(sta, tidstats, i);
214579c892b8SJohannes Berg 	}
214679c892b8SJohannes Berg 
2147b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2148b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2149319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2150319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLID) |
2151319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLINK_STATE) |
2152319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_LOCAL_PM) |
2153319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PEER_PM) |
2154319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_NONPEER_PM);
2155b7ffbd7eSJohannes Berg 
2156433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2157433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2158433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2159b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2160319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
2161433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2162b7ffbd7eSJohannes Berg 		}
2163433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2164433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2165433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2166b7ffbd7eSJohannes Berg #endif
2167b7ffbd7eSJohannes Berg 	}
2168b7ffbd7eSJohannes Berg 
2169b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2170b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2171b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2172b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2173b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2174b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2175b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2176785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2177b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2178b7ffbd7eSJohannes Berg 
2179b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2180b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2181b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2182b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2183b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2184b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2185b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2186b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2187b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2188b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2189b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2190b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2191a74a8c84SJohannes Berg 	if (sta->sta.wme)
2192b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2193b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2194b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2195b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2196b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2197b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2198b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2199b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2200b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2201b7ffbd7eSJohannes Berg 
2202b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2203b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2204b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2205b7ffbd7eSJohannes Berg 	else
2206b7ffbd7eSJohannes Berg 		thr = drv_get_expected_throughput(local, &sta->sta);
2207b7ffbd7eSJohannes Berg 
2208b7ffbd7eSJohannes Berg 	if (thr != 0) {
2209319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2210b7ffbd7eSJohannes Berg 		sinfo->expected_throughput = thr;
2211b7ffbd7eSJohannes Berg 	}
2212b7ffbd7eSJohannes Berg }
2213b8da6b6aSJohannes Berg 
2214b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2215b8da6b6aSJohannes Berg {
2216b8da6b6aSJohannes Berg 	if (time_after(sta->rx_stats.last_rx, sta->status_stats.last_ack))
2217b8da6b6aSJohannes Berg 		return sta->rx_stats.last_rx;
2218b8da6b6aSJohannes Berg 	return sta->status_stats.last_ack;
2219b8da6b6aSJohannes Berg }
2220