xref: /openbmc/linux/net/mac80211/sta_info.c (revision 2fe4a29a452a68ffa8a501000d0ef8095c242eba)
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
5dcba665bSJohannes Berg  * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
6a403f3bfSJohannes Berg  * Copyright (C) 2018 Intel Corporation
7f0706e82SJiri Benc  *
8f0706e82SJiri Benc  * This program is free software; you can redistribute it and/or modify
9f0706e82SJiri Benc  * it under the terms of the GNU General Public License version 2 as
10f0706e82SJiri Benc  * published by the Free Software Foundation.
11f0706e82SJiri Benc  */
12f0706e82SJiri Benc 
13f0706e82SJiri Benc #include <linux/module.h>
14f0706e82SJiri Benc #include <linux/init.h>
15888d04dfSFelix Fietkau #include <linux/etherdevice.h>
16f0706e82SJiri Benc #include <linux/netdevice.h>
17f0706e82SJiri Benc #include <linux/types.h>
18f0706e82SJiri Benc #include <linux/slab.h>
19f0706e82SJiri Benc #include <linux/skbuff.h>
20f0706e82SJiri Benc #include <linux/if_arp.h>
210d174406SJohannes Berg #include <linux/timer.h>
22d0709a65SJohannes Berg #include <linux/rtnetlink.h>
23f0706e82SJiri Benc 
24484a54c2SToke Høiland-Jørgensen #include <net/codel.h>
25f0706e82SJiri Benc #include <net/mac80211.h>
26f0706e82SJiri Benc #include "ieee80211_i.h"
2724487981SJohannes Berg #include "driver-ops.h"
282c8dccc7SJohannes Berg #include "rate.h"
29f0706e82SJiri Benc #include "sta_info.h"
30e9f207f0SJiri Benc #include "debugfs_sta.h"
31ee385855SLuis Carlos Cobo #include "mesh.h"
32ce662b44SJohannes Berg #include "wme.h"
33f0706e82SJiri Benc 
34d0709a65SJohannes Berg /**
35d0709a65SJohannes Berg  * DOC: STA information lifetime rules
36d0709a65SJohannes Berg  *
37d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
38d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
39d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
40d0709a65SJohannes Berg  *
4134e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
4234e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4334e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4434e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4534e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4634e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4734e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4834e89507SJohannes Berg  * encryption keys.
4993e5deb1SJohannes Berg  *
5093e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
5193e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
52d0709a65SJohannes Berg  *
5334e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
547e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
557e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5625985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5734e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5825985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5934e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
607e189a12SLuis R. Rodriguez  *
6134e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
6234e89507SJohannes Berg  * calls are available.
63d0709a65SJohannes Berg  *
6434e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6534e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6634e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6734e89507SJohannes Berg  * freed before they are done using it.
68d0709a65SJohannes Berg  */
69f0706e82SJiri Benc 
707bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
717bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
72caf22d31SJohannes Berg 	.automatic_shrinking = true,
737bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
74ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
757bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
76ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
777bedd0cfSJohannes Berg };
787bedd0cfSJohannes Berg 
794d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
80be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
81f0706e82SJiri Benc 			     struct sta_info *sta)
82f0706e82SJiri Benc {
8383e7e4ceSHerbert Xu 	return rhltable_remove(&local->sta_hash, &sta->hash_node,
847bedd0cfSJohannes Berg 			       sta_rht_params);
85f0706e82SJiri Benc }
86f0706e82SJiri Benc 
875108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
88b22cfcfcSEliad Peller {
89b22cfcfcSEliad Peller 	int ac, i;
90b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
91b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
92b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
93fa962b92SMichal Kazior 	struct fq *fq = &local->fq;
94d012a605SMarco Porsch 	struct ps_data *ps;
95b22cfcfcSEliad Peller 
96e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
975ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
985ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
99d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
100d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
101d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1023f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1033f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
104d012a605SMarco Porsch 		else
105d012a605SMarco Porsch 			return;
106b22cfcfcSEliad Peller 
107b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
108e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1095ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
110b22cfcfcSEliad Peller 
111d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
112b22cfcfcSEliad Peller 	}
113b22cfcfcSEliad Peller 
114ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
115ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
116ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
117ba8c3d6fSFelix Fietkau 
118fa962b92SMichal Kazior 			spin_lock_bh(&fq->lock);
119fa962b92SMichal Kazior 			ieee80211_txq_purge(local, txqi);
120fa962b92SMichal Kazior 			spin_unlock_bh(&fq->lock);
121ba8c3d6fSFelix Fietkau 		}
122ba8c3d6fSFelix Fietkau 	}
123ba8c3d6fSFelix Fietkau 
124b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
125b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1261f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1271f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
128b22cfcfcSEliad Peller 	}
129b22cfcfcSEliad Peller 
13045b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
13145b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
132b22cfcfcSEliad Peller 
1335ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
134b22cfcfcSEliad Peller 
135b22cfcfcSEliad Peller 	/*
136b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
137b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
138b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
139b22cfcfcSEliad Peller 	 * directly by station destruction.
140b22cfcfcSEliad Peller 	 */
1415a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
142661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
143b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
144b22cfcfcSEliad Peller 		if (!tid_tx)
145b22cfcfcSEliad Peller 			continue;
1461f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
147b22cfcfcSEliad Peller 		kfree(tid_tx);
148b22cfcfcSEliad Peller 	}
1495108ca82SJohannes Berg }
150b22cfcfcSEliad Peller 
1515108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1525108ca82SJohannes Berg {
1535108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1545108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1555108ca82SJohannes Berg 
1565108ca82SJohannes Berg 	__cleanup_single_sta(sta);
157b22cfcfcSEliad Peller 	sta_info_free(local, sta);
158b22cfcfcSEliad Peller }
159b22cfcfcSEliad Peller 
16083e7e4ceSHerbert Xu struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
16183e7e4ceSHerbert Xu 					 const u8 *addr)
16283e7e4ceSHerbert Xu {
16383e7e4ceSHerbert Xu 	return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
16483e7e4ceSHerbert Xu }
16583e7e4ceSHerbert Xu 
166d0709a65SJohannes Berg /* protected by RCU */
167abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
168abe60632SJohannes Berg 			      const u8 *addr)
16943ba7e95SJohannes Berg {
170abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
17183e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
17260f4b626SJohannes Berg 	struct sta_info *sta;
17343ba7e95SJohannes Berg 
17460f4b626SJohannes Berg 	rcu_read_lock();
17583e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
17660f4b626SJohannes Berg 		if (sta->sdata == sdata) {
17760f4b626SJohannes Berg 			rcu_read_unlock();
17860f4b626SJohannes Berg 			/* this is safe as the caller must already hold
17960f4b626SJohannes Berg 			 * another rcu read section or the mutex
18060f4b626SJohannes Berg 			 */
18160f4b626SJohannes Berg 			return sta;
18260f4b626SJohannes Berg 		}
18360f4b626SJohannes Berg 	}
18460f4b626SJohannes Berg 	rcu_read_unlock();
18560f4b626SJohannes Berg 	return NULL;
18643ba7e95SJohannes Berg }
18743ba7e95SJohannes Berg 
1880e5ded5aSFelix Fietkau /*
1890e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1900e5ded5aSFelix Fietkau  * or from one of its vlans
1910e5ded5aSFelix Fietkau  */
1920e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1930e5ded5aSFelix Fietkau 				  const u8 *addr)
1940e5ded5aSFelix Fietkau {
1950e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
19683e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
1970e5ded5aSFelix Fietkau 	struct sta_info *sta;
1980e5ded5aSFelix Fietkau 
1997bedd0cfSJohannes Berg 	rcu_read_lock();
20083e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
2017bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
2027bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
2037bedd0cfSJohannes Berg 			rcu_read_unlock();
2047bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
2057bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
2067bedd0cfSJohannes Berg 			 */
2070e5ded5aSFelix Fietkau 			return sta;
2080e5ded5aSFelix Fietkau 		}
2097bedd0cfSJohannes Berg 	}
2107bedd0cfSJohannes Berg 	rcu_read_unlock();
2117bedd0cfSJohannes Berg 	return NULL;
2127bedd0cfSJohannes Berg }
2130e5ded5aSFelix Fietkau 
2143b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2153b53fde8SJohannes Berg 				     int idx)
216ee385855SLuis Carlos Cobo {
2173b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
218ee385855SLuis Carlos Cobo 	struct sta_info *sta;
219ee385855SLuis Carlos Cobo 	int i = 0;
220ee385855SLuis Carlos Cobo 
221d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
2223b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2232a8ca29aSLuis Carlos Cobo 			continue;
224ee385855SLuis Carlos Cobo 		if (i < idx) {
225ee385855SLuis Carlos Cobo 			++i;
226ee385855SLuis Carlos Cobo 			continue;
227ee385855SLuis Carlos Cobo 		}
2282a8ca29aSLuis Carlos Cobo 		return sta;
229ee385855SLuis Carlos Cobo 	}
230ee385855SLuis Carlos Cobo 
231ee385855SLuis Carlos Cobo 	return NULL;
232ee385855SLuis Carlos Cobo }
233f0706e82SJiri Benc 
23493e5deb1SJohannes Berg /**
235d9a7ddb0SJohannes Berg  * sta_info_free - free STA
23693e5deb1SJohannes Berg  *
2376ef307bcSRandy Dunlap  * @local: pointer to the global information
23893e5deb1SJohannes Berg  * @sta: STA info to free
23993e5deb1SJohannes Berg  *
24093e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
241d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
242d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
243d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
24493e5deb1SJohannes Berg  */
245d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
24693e5deb1SJohannes Berg {
247889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2484b7679a5SJohannes Berg 		rate_control_free_sta(sta);
24993e5deb1SJohannes Berg 
250bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
25193e5deb1SJohannes Berg 
252ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
253ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
25453d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
255433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
256433f5bc1SJohannes Berg 	kfree(sta->mesh);
257433f5bc1SJohannes Berg #endif
258c9c5962bSJohannes Berg 	free_percpu(sta->pcpu_rx_stats);
25993e5deb1SJohannes Berg 	kfree(sta);
26093e5deb1SJohannes Berg }
26193e5deb1SJohannes Berg 
2624d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
26362b14b24SJohannes Berg static int sta_info_hash_add(struct ieee80211_local *local,
264d0709a65SJohannes Berg 			     struct sta_info *sta)
265f0706e82SJiri Benc {
26683e7e4ceSHerbert Xu 	return rhltable_insert(&local->sta_hash, &sta->hash_node,
2677bedd0cfSJohannes Berg 			       sta_rht_params);
268f0706e82SJiri Benc }
269f0706e82SJiri Benc 
2705ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
271af818581SJohannes Berg {
272af818581SJohannes Berg 	struct sta_info *sta;
273af818581SJohannes Berg 
2745ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
275af818581SJohannes Berg 
276af818581SJohannes Berg 	if (sta->dead)
277af818581SJohannes Berg 		return;
278af818581SJohannes Berg 
27954420473SHelmut Schaa 	local_bh_disable();
2805ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
281af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2825ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
283af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2845ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
28547086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
286ce662b44SJohannes Berg 	local_bh_enable();
287af818581SJohannes Berg }
288af818581SJohannes Berg 
289af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
290af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
291af65cd96SJohannes Berg {
29230686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
293af65cd96SJohannes Berg 		return 0;
294af65cd96SJohannes Berg 
295889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
296af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
29735c347acSJohannes Berg 						     sta, gfp);
298889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
299af65cd96SJohannes Berg 		return -ENOMEM;
300af65cd96SJohannes Berg 
301af65cd96SJohannes Berg 	return 0;
302af65cd96SJohannes Berg }
303af65cd96SJohannes Berg 
30473651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
30556544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
306f0706e82SJiri Benc {
307d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
308ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
309f0706e82SJiri Benc 	struct sta_info *sta;
31016c5f15cSRon Rindjunsky 	int i;
311f0706e82SJiri Benc 
312ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
313f0706e82SJiri Benc 	if (!sta)
31473651ee6SJohannes Berg 		return NULL;
315f0706e82SJiri Benc 
316c9c5962bSJohannes Berg 	if (ieee80211_hw_check(hw, USES_RSS)) {
317c9c5962bSJohannes Berg 		sta->pcpu_rx_stats =
31895f3ce6aSSara Sharon 			alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
319c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats)
320c9c5962bSJohannes Berg 			goto free;
321c9c5962bSJohannes Berg 	}
322c9c5962bSJohannes Berg 
32307346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3241d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
3255ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
32667c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
327a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
32887f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
329433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
330433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
331433f5bc1SJohannes Berg 		if (!sta->mesh)
332433f5bc1SJohannes Berg 			goto free;
3334c02d62fSKees Cook 		sta->mesh->plink_sta = sta;
334433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
33587f59c70SThomas Pedersen 		if (ieee80211_vif_is_mesh(&sdata->vif) &&
33687f59c70SThomas Pedersen 		    !sdata->u.mesh.user_mpm)
3374c02d62fSKees Cook 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
3384c02d62fSKees Cook 				    0);
339433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
340433f5bc1SJohannes Berg 	}
34187f59c70SThomas Pedersen #endif
34207346f81SJohannes Berg 
343ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
34417741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
345480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
346480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
347480dd46bSMaxim Altshul 
348d0709a65SJohannes Berg 	sta->local = local;
349d0709a65SJohannes Berg 	sta->sdata = sdata;
350e5a9f8d0SJohannes Berg 	sta->rx_stats.last_rx = jiffies;
351f0706e82SJiri Benc 
3520f9c5a61SJohannes Berg 	u64_stats_init(&sta->rx_stats.syncp);
3530f9c5a61SJohannes Berg 
35471ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
35571ec375cSJohannes Berg 
356b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
357b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
358b6da911bSLiad Kaufman 
35984b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
3600be6ed13SJohannes Berg 	ewma_signal_init(&sta->rx_stats_avg.signal);
361cc60dbbfSBalaji Pothunoori 	ewma_avg_signal_init(&sta->status_stats.avg_ack_signal);
3620be6ed13SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
3630be6ed13SJohannes Berg 		ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
364541a45a1SBruno Randolf 
365ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
366ba8c3d6fSFelix Fietkau 		void *txq_data;
367ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
368ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
369ba8c3d6fSFelix Fietkau 
370ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
371ba8c3d6fSFelix Fietkau 		if (!txq_data)
372ba8c3d6fSFelix Fietkau 			goto free;
373ba8c3d6fSFelix Fietkau 
374ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
375ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
376ba8c3d6fSFelix Fietkau 
377fa962b92SMichal Kazior 			ieee80211_txq_init(sdata, sta, txq, i);
378abfbc3afSJohannes Berg 		}
379ba8c3d6fSFelix Fietkau 	}
380ba8c3d6fSFelix Fietkau 
381ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
382ba8c3d6fSFelix Fietkau 		goto free_txq;
383f0706e82SJiri Benc 
384948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
385948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
386948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
387948d887dSJohannes Berg 	}
38873651ee6SJohannes Berg 
3895a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3904be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
391cccaec98SSenthil Balasubramanian 
392af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
393687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
394687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
39521a8e9ddSMohammed Shafi Shajakhan 		struct ieee80211_supported_band *sband;
39621a8e9ddSMohammed Shafi Shajakhan 		u8 smps;
39721a8e9ddSMohammed Shafi Shajakhan 
39821a8e9ddSMohammed Shafi Shajakhan 		sband = ieee80211_get_sband(sdata);
39921a8e9ddSMohammed Shafi Shajakhan 		if (!sband)
40021a8e9ddSMohammed Shafi Shajakhan 			goto free_txq;
40121a8e9ddSMohammed Shafi Shajakhan 
40221a8e9ddSMohammed Shafi Shajakhan 		smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
403687da132SEmmanuel Grumbach 			IEEE80211_HT_CAP_SM_PS_SHIFT;
404687da132SEmmanuel Grumbach 		/*
405687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
406687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
407687da132SEmmanuel Grumbach 		 */
408687da132SEmmanuel Grumbach 		switch (smps) {
409687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
410687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
411687da132SEmmanuel Grumbach 			break;
412687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
413687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
414687da132SEmmanuel Grumbach 			break;
415687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
416687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
417687da132SEmmanuel Grumbach 			break;
418687da132SEmmanuel Grumbach 		default:
419687da132SEmmanuel Grumbach 			WARN_ON(1);
420687da132SEmmanuel Grumbach 		}
421687da132SEmmanuel Grumbach 	}
422af0ed69bSJohannes Berg 
4236e0456b5SFelix Fietkau 	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
4246e0456b5SFelix Fietkau 
425484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
426484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
427484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
428484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
429484a54c2SToke Høiland-Jørgensen 
430bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
431ef04a297SJohannes Berg 
432abfbc3afSJohannes Berg 	return sta;
433ba8c3d6fSFelix Fietkau 
434ba8c3d6fSFelix Fietkau free_txq:
435ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
436ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
437ba8c3d6fSFelix Fietkau free:
438d78d9ee9SSara Sharon 	free_percpu(sta->pcpu_rx_stats);
439433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
440433f5bc1SJohannes Berg 	kfree(sta->mesh);
441433f5bc1SJohannes Berg #endif
442ba8c3d6fSFelix Fietkau 	kfree(sta);
443ba8c3d6fSFelix Fietkau 	return NULL;
44473651ee6SJohannes Berg }
44573651ee6SJohannes Berg 
4468c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
44734e89507SJohannes Berg {
44834e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
44934e89507SJohannes Berg 
45003e4497eSJohannes Berg 	/*
45103e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
45203e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
45303e4497eSJohannes Berg 	 * and another CPU turns off the net device.
45403e4497eSJohannes Berg 	 */
4558c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4568c71df7aSGuy Eilam 		return -ENETDOWN;
45703e4497eSJohannes Berg 
458b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4598c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4608c71df7aSGuy Eilam 		return -EINVAL;
4618c71df7aSGuy Eilam 
46283e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
46383e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
46483e7e4ceSHerbert Xu 	 * for correctness.
46531104891SJohannes Berg 	 */
46631104891SJohannes Berg 	rcu_read_lock();
46731104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
46831104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
46931104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
47031104891SJohannes Berg 		rcu_read_unlock();
47131104891SJohannes Berg 		return -ENOTUNIQ;
47231104891SJohannes Berg 	}
47331104891SJohannes Berg 	rcu_read_unlock();
47431104891SJohannes Berg 
4758c71df7aSGuy Eilam 	return 0;
47693e5deb1SJohannes Berg }
47744213b5eSJohannes Berg 
478f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
479f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
480f09603a2SJohannes Berg 				     struct sta_info *sta)
481f09603a2SJohannes Berg {
482f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
483f09603a2SJohannes Berg 	int err = 0;
484f09603a2SJohannes Berg 
485f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
486f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
487f09603a2SJohannes Berg 		if (err)
488f09603a2SJohannes Berg 			break;
489f09603a2SJohannes Berg 	}
490f09603a2SJohannes Berg 
491f09603a2SJohannes Berg 	if (!err) {
492a4ec45a4SJohannes Berg 		/*
493a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
494a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
495a4ec45a4SJohannes Berg 		 */
496a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
497f09603a2SJohannes Berg 			sta->uploaded = true;
498f09603a2SJohannes Berg 		return 0;
499f09603a2SJohannes Berg 	}
500f09603a2SJohannes Berg 
501f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
502bdcbd8e0SJohannes Berg 		sdata_info(sdata,
503bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
504bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
505f09603a2SJohannes Berg 		err = 0;
506f09603a2SJohannes Berg 	}
507f09603a2SJohannes Berg 
508f09603a2SJohannes Berg 	/* unwind on error */
509f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
510f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
511f09603a2SJohannes Berg 
512f09603a2SJohannes Berg 	return err;
513f09603a2SJohannes Berg }
514f09603a2SJohannes Berg 
515d405fd8cSGregory Greenman static void
516d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
517d405fd8cSGregory Greenman {
518d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
519d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
520d405fd8cSGregory Greenman 	struct sta_info *sta;
521d405fd8cSGregory Greenman 
522d405fd8cSGregory Greenman 	rcu_read_lock();
523d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
524d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
525d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
526d405fd8cSGregory Greenman 			continue;
527d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
528d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
529d405fd8cSGregory Greenman 			break;
530d405fd8cSGregory Greenman 		}
531d405fd8cSGregory Greenman 	}
532d405fd8cSGregory Greenman 	rcu_read_unlock();
533d405fd8cSGregory Greenman 
534d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
535d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
536d405fd8cSGregory Greenman 		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
537d405fd8cSGregory Greenman 	}
538d405fd8cSGregory Greenman }
539d405fd8cSGregory Greenman 
54034e89507SJohannes Berg /*
5418c71df7aSGuy Eilam  * should be called with sta_mtx locked
5428c71df7aSGuy Eilam  * this function replaces the mutex lock
5438c71df7aSGuy Eilam  * with a RCU lock
5448c71df7aSGuy Eilam  */
5454d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
5468c71df7aSGuy Eilam {
5478c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
5488c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
5490c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
5508c71df7aSGuy Eilam 	int err = 0;
5518c71df7aSGuy Eilam 
5528c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
5538c71df7aSGuy Eilam 
5547852e361SJohannes Berg 	/* check if STA exists already */
5557852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
5564d33960bSJohannes Berg 		err = -EEXIST;
5574d33960bSJohannes Berg 		goto out_err;
55834e89507SJohannes Berg 	}
55934e89507SJohannes Berg 
5600c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
5610c2e3842SKoen Vandeputte 	if (!sinfo) {
5620c2e3842SKoen Vandeputte 		err = -ENOMEM;
5630c2e3842SKoen Vandeputte 		goto out_err;
5640c2e3842SKoen Vandeputte 	}
5650c2e3842SKoen Vandeputte 
5664d33960bSJohannes Berg 	local->num_sta++;
5674d33960bSJohannes Berg 	local->sta_generation++;
5684d33960bSJohannes Berg 	smp_mb();
5694d33960bSJohannes Berg 
5705108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
5715108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5725108ca82SJohannes Berg 
5734d33960bSJohannes Berg 	/* make the station visible */
57462b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
57562b14b24SJohannes Berg 	if (err)
57662b14b24SJohannes Berg 		goto out_drop_sta;
5774d33960bSJohannes Berg 
5782bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
57983d5cc01SJohannes Berg 
5805108ca82SJohannes Berg 	/* notify driver */
5815108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
5825108ca82SJohannes Berg 	if (err)
5835108ca82SJohannes Berg 		goto out_remove;
5845108ca82SJohannes Berg 
58583d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
586d405fd8cSGregory Greenman 
587d405fd8cSGregory Greenman 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
588d405fd8cSGregory Greenman 		ieee80211_recalc_min_chandef(sta->sdata);
589d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps)
590d405fd8cSGregory Greenman 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
591d405fd8cSGregory Greenman 	}
592d405fd8cSGregory Greenman 
5935108ca82SJohannes Berg 	/* accept BA sessions now */
5945108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5954d33960bSJohannes Berg 
5964d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
5974d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
5984d33960bSJohannes Berg 
5990ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
6000ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
6010ef049dcSArnd Bergmann 	kfree(sinfo);
602d0709a65SJohannes Berg 
603bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
604f0706e82SJiri Benc 
60534e89507SJohannes Berg 	/* move reference to rcu-protected */
60634e89507SJohannes Berg 	rcu_read_lock();
60734e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
608e9f207f0SJiri Benc 
60973651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
61073651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
61173651ee6SJohannes Berg 
61273651ee6SJohannes Berg 	return 0;
6135108ca82SJohannes Berg  out_remove:
6145108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
6155108ca82SJohannes Berg 	list_del_rcu(&sta->list);
61662b14b24SJohannes Berg  out_drop_sta:
6175108ca82SJohannes Berg 	local->num_sta--;
6185108ca82SJohannes Berg 	synchronize_net();
6195108ca82SJohannes Berg 	__cleanup_single_sta(sta);
6204d33960bSJohannes Berg  out_err:
6214d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
622ea32f065SSudip Mukherjee 	kfree(sinfo);
6234d33960bSJohannes Berg 	rcu_read_lock();
6244d33960bSJohannes Berg 	return err;
6258c71df7aSGuy Eilam }
6268c71df7aSGuy Eilam 
6278c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
6288c71df7aSGuy Eilam {
6298c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
630308f7fcfSZhao, Gang 	int err;
6318c71df7aSGuy Eilam 
6324d33960bSJohannes Berg 	might_sleep();
6334d33960bSJohannes Berg 
63431104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
63531104891SJohannes Berg 
6368c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
6378c71df7aSGuy Eilam 	if (err) {
63831104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
6398c71df7aSGuy Eilam 		rcu_read_lock();
6408c71df7aSGuy Eilam 		goto out_free;
6418c71df7aSGuy Eilam 	}
6428c71df7aSGuy Eilam 
6434d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
6448c71df7aSGuy Eilam 	if (err)
645004c872eSJohannes Berg 		goto out_free;
646d0709a65SJohannes Berg 
647004c872eSJohannes Berg 	return 0;
64893e5deb1SJohannes Berg  out_free:
649d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
65093e5deb1SJohannes Berg 	return err;
651f0706e82SJiri Benc }
652f0706e82SJiri Benc 
65334e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
65434e89507SJohannes Berg {
65534e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
65634e89507SJohannes Berg 
65734e89507SJohannes Berg 	rcu_read_unlock();
65834e89507SJohannes Berg 
65934e89507SJohannes Berg 	return err;
66034e89507SJohannes Berg }
66134e89507SJohannes Berg 
662d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
663004c872eSJohannes Berg {
664004c872eSJohannes Berg 	/*
665004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
666004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
667004c872eSJohannes Berg 	 */
668d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
669004c872eSJohannes Berg }
670004c872eSJohannes Berg 
671d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
672004c872eSJohannes Berg {
673004c872eSJohannes Berg 	/*
674004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
675004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
676004c872eSJohannes Berg 	 */
677d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
678004c872eSJohannes Berg }
679004c872eSJohannes Berg 
6803d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
6813d5839b6SIlan Peer {
6823d5839b6SIlan Peer 	/*
6833d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
6843d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
6853d5839b6SIlan Peer 	 */
6863d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
6873d5839b6SIlan Peer }
6883d5839b6SIlan Peer 
689948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
690004c872eSJohannes Berg {
691948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
692948d887dSJohannes Berg 	switch (ac) {
693948d887dSJohannes Berg 	case IEEE80211_AC_VO:
694948d887dSJohannes Berg 		return BIT(6) | BIT(7);
695948d887dSJohannes Berg 	case IEEE80211_AC_VI:
696948d887dSJohannes Berg 		return BIT(4) | BIT(5);
697948d887dSJohannes Berg 	case IEEE80211_AC_BE:
698948d887dSJohannes Berg 		return BIT(0) | BIT(3);
699948d887dSJohannes Berg 	case IEEE80211_AC_BK:
700948d887dSJohannes Berg 		return BIT(1) | BIT(2);
701948d887dSJohannes Berg 	default:
702948d887dSJohannes Berg 		WARN_ON(1);
703948d887dSJohannes Berg 		return 0;
704d0709a65SJohannes Berg 	}
705004c872eSJohannes Berg }
706004c872eSJohannes Berg 
7079b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
708004c872eSJohannes Berg {
709c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
710d012a605SMarco Porsch 	struct ps_data *ps;
711948d887dSJohannes Berg 	bool indicate_tim = false;
712948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
713948d887dSJohannes Berg 	int ac;
714a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
715004c872eSJohannes Berg 
716d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
717d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
718c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
719c868cb35SJohannes Berg 			return;
7203e122be0SJohannes Berg 
721d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
7223f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
7233f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
7243f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
7253f52b7e3SMarco Porsch #endif
726d012a605SMarco Porsch 	} else {
727d012a605SMarco Porsch 		return;
728d012a605SMarco Porsch 	}
729d012a605SMarco Porsch 
730c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
731d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
732c868cb35SJohannes Berg 		return;
733004c872eSJohannes Berg 
734c868cb35SJohannes Berg 	if (sta->dead)
735c868cb35SJohannes Berg 		goto done;
7363e122be0SJohannes Berg 
737948d887dSJohannes Berg 	/*
738948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
739948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
740948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
741948d887dSJohannes Berg 	 * non-enabled ones.
742948d887dSJohannes Berg 	 */
743948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
744948d887dSJohannes Berg 		ignore_for_tim = 0;
745948d887dSJohannes Berg 
7469b7a86f3SJohannes Berg 	if (ignore_pending)
7479b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
7489b7a86f3SJohannes Berg 
749948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
750948d887dSJohannes Berg 		unsigned long tids;
751948d887dSJohannes Berg 
752f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
753948d887dSJohannes Berg 			continue;
754948d887dSJohannes Berg 
755948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
756948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
757948d887dSJohannes Berg 		if (indicate_tim)
758948d887dSJohannes Berg 			break;
759948d887dSJohannes Berg 
760948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
761948d887dSJohannes Berg 
762948d887dSJohannes Berg 		indicate_tim |=
763948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
764ba8c3d6fSFelix Fietkau 		indicate_tim |=
765ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
766004c872eSJohannes Berg 	}
767004c872eSJohannes Berg 
768c868cb35SJohannes Berg  done:
76965f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
770004c872eSJohannes Berg 
7713d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
7723d5839b6SIlan Peer 		goto out_unlock;
7733d5839b6SIlan Peer 
774948d887dSJohannes Berg 	if (indicate_tim)
775d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
776c868cb35SJohannes Berg 	else
777d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
7783e122be0SJohannes Berg 
7799b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
780c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
781948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
782c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
783004c872eSJohannes Berg 	}
784004c872eSJohannes Berg 
7853d5839b6SIlan Peer out_unlock:
78665f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
787004c872eSJohannes Berg }
788004c872eSJohannes Berg 
7899b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
7909b7a86f3SJohannes Berg {
7919b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
7929b7a86f3SJohannes Berg }
7939b7a86f3SJohannes Berg 
794cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
795f0706e82SJiri Benc {
796e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
797f0706e82SJiri Benc 	int timeout;
798f0706e82SJiri Benc 
799f0706e82SJiri Benc 	if (!skb)
800cd0b8d89SJohannes Berg 		return false;
801f0706e82SJiri Benc 
802e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
803f0706e82SJiri Benc 
804f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
80557c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
80657c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
80757c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
808f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
809f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
810e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
811f0706e82SJiri Benc }
812f0706e82SJiri Benc 
813f0706e82SJiri Benc 
814948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
815948d887dSJohannes Berg 						struct sta_info *sta, int ac)
816f0706e82SJiri Benc {
817f0706e82SJiri Benc 	unsigned long flags;
818f0706e82SJiri Benc 	struct sk_buff *skb;
819f0706e82SJiri Benc 
82060750397SJohannes Berg 	/*
82160750397SJohannes Berg 	 * First check for frames that should expire on the filtered
82260750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
82360750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
82460750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
82560750397SJohannes Berg 	 * total_ps_buffered counter.
82660750397SJohannes Berg 	 */
827f0706e82SJiri Benc 	for (;;) {
828948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
829948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
83057c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
831948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
832836341a7SJohannes Berg 		else
833f0706e82SJiri Benc 			skb = NULL;
834948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
835f0706e82SJiri Benc 
83660750397SJohannes Berg 		/*
83760750397SJohannes Berg 		 * Frames are queued in order, so if this one
83860750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
83960750397SJohannes Berg 		 * we actually reached the end of the queue we
84060750397SJohannes Berg 		 * also need to stop, of course.
84160750397SJohannes Berg 		 */
84260750397SJohannes Berg 		if (!skb)
84360750397SJohannes Berg 			break;
844d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
84560750397SJohannes Berg 	}
84660750397SJohannes Berg 
84760750397SJohannes Berg 	/*
84860750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
84960750397SJohannes Berg 	 * only find something if the filtered queue was emptied
85060750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
85160750397SJohannes Berg 	 * buffered frames.
85260750397SJohannes Berg 	 */
853f0706e82SJiri Benc 	for (;;) {
854948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
855948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
856f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
857948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
858f0706e82SJiri Benc 		else
859f0706e82SJiri Benc 			skb = NULL;
860948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
861f0706e82SJiri Benc 
86260750397SJohannes Berg 		/*
86360750397SJohannes Berg 		 * frames are queued in order, so if this one
86460750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
86560750397SJohannes Berg 		 * the queue) we can stop testing
86660750397SJohannes Berg 		 */
867836341a7SJohannes Berg 		if (!skb)
868836341a7SJohannes Berg 			break;
869836341a7SJohannes Berg 
870f0706e82SJiri Benc 		local->total_ps_buffered--;
871bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
872bdcbd8e0SJohannes Berg 		       sta->sta.addr);
873d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
874f0706e82SJiri Benc 	}
8753393a608SJuuso Oikarinen 
87660750397SJohannes Berg 	/*
87760750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
87860750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
87960750397SJohannes Berg 	 * frames.
88060750397SJohannes Berg 	 */
88160750397SJohannes Berg 	sta_info_recalc_tim(sta);
88260750397SJohannes Berg 
88360750397SJohannes Berg 	/*
88460750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
88560750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
88660750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
88760750397SJohannes Berg 	 */
888948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
889948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
890948d887dSJohannes Berg }
891948d887dSJohannes Berg 
892948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
893948d887dSJohannes Berg 					     struct sta_info *sta)
894948d887dSJohannes Berg {
895948d887dSJohannes Berg 	bool have_buffered = false;
896948d887dSJohannes Berg 	int ac;
897948d887dSJohannes Berg 
8983f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
8993f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
9003f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
901948d887dSJohannes Berg 		return false;
902948d887dSJohannes Berg 
903948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
904948d887dSJohannes Berg 		have_buffered |=
905948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
906948d887dSJohannes Berg 
907948d887dSJohannes Berg 	return have_buffered;
908f0706e82SJiri Benc }
909f0706e82SJiri Benc 
910d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
91134e89507SJohannes Berg {
91234e89507SJohannes Berg 	struct ieee80211_local *local;
91334e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
9146d10e46bSJohannes Berg 	int ret;
91534e89507SJohannes Berg 
91634e89507SJohannes Berg 	might_sleep();
91734e89507SJohannes Berg 
91834e89507SJohannes Berg 	if (!sta)
91934e89507SJohannes Berg 		return -ENOENT;
92034e89507SJohannes Berg 
92134e89507SJohannes Berg 	local = sta->local;
92234e89507SJohannes Berg 	sdata = sta->sdata;
92334e89507SJohannes Berg 
92483d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
92583d5cc01SJohannes Berg 
926098a6070SJohannes Berg 	/*
927098a6070SJohannes Berg 	 * Before removing the station from the driver and
928098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
929098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
930098a6070SJohannes Berg 	 * will be sufficient.
931098a6070SJohannes Berg 	 */
932c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
933c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
934098a6070SJohannes Berg 
935f59374ebSSara Sharon 	/*
936f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
937f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
938f59374ebSSara Sharon 	 * all such frames to be processed.
939f59374ebSSara Sharon 	 */
940f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
941f59374ebSSara Sharon 
94234e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
943b01711beSJohannes Berg 	if (WARN_ON(ret))
94434e89507SJohannes Berg 		return ret;
94534e89507SJohannes Berg 
946a7a6bdd0SArik Nemtsov 	/*
947a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
948a7a6bdd0SArik Nemtsov 	 * removal.
949a7a6bdd0SArik Nemtsov 	 */
950a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
951a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
952a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
953a7a6bdd0SArik Nemtsov 	}
954a7a6bdd0SArik Nemtsov 
955794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
956ef044763SEliad Peller 	sta->removed = true;
9574d33960bSJohannes Berg 
9586a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
9596a9d1b91SJohannes Berg 
960a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
961a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
962a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
963a710c816SJohannes Berg 
964d778207bSJohannes Berg 	return 0;
965d778207bSJohannes Berg }
966d778207bSJohannes Berg 
967d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
968d778207bSJohannes Berg {
969d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
970d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
9710ef049dcSArnd Bergmann 	struct station_info *sinfo;
972d778207bSJohannes Berg 	int ret;
973d778207bSJohannes Berg 
974d778207bSJohannes Berg 	/*
975d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
976d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
977d778207bSJohannes Berg 	 */
978d778207bSJohannes Berg 
979d778207bSJohannes Berg 	might_sleep();
980d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
981d778207bSJohannes Berg 
982c8782078SJohannes Berg 	/* now keys can no longer be reached */
9836d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
98434e89507SJohannes Berg 
9859b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
9869b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
9879b7a86f3SJohannes Berg 
98834e89507SJohannes Berg 	sta->dead = true;
98934e89507SJohannes Berg 
99034e89507SJohannes Berg 	local->num_sta--;
99134e89507SJohannes Berg 	local->sta_generation++;
99234e89507SJohannes Berg 
99383d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
994f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
995f09603a2SJohannes Berg 		if (ret) {
99683d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
99783d5cc01SJohannes Berg 			break;
99883d5cc01SJohannes Berg 		}
99983d5cc01SJohannes Berg 	}
1000d9a7ddb0SJohannes Berg 
1001f09603a2SJohannes Berg 	if (sta->uploaded) {
1002f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1003f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1004f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1005f09603a2SJohannes Berg 	}
100634e89507SJohannes Berg 
1007bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1008bdcbd8e0SJohannes Berg 
10090ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
10100ef049dcSArnd Bergmann 	if (sinfo)
10110ef049dcSArnd Bergmann 		sta_set_sinfo(sta, sinfo);
10120ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
10130ef049dcSArnd Bergmann 	kfree(sinfo);
1014ec15e68bSJouni Malinen 
101534e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
101634e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
101734e89507SJohannes Berg 
1018d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1019d778207bSJohannes Berg }
1020d778207bSJohannes Berg 
1021d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1022d778207bSJohannes Berg {
1023d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1024d778207bSJohannes Berg 
1025d778207bSJohannes Berg 	if (err)
1026d778207bSJohannes Berg 		return err;
1027d778207bSJohannes Berg 
1028d778207bSJohannes Berg 	synchronize_net();
1029d778207bSJohannes Berg 
1030d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
103134e89507SJohannes Berg 
103234e89507SJohannes Berg 	return 0;
103334e89507SJohannes Berg }
103434e89507SJohannes Berg 
103534e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
103634e89507SJohannes Berg {
103734e89507SJohannes Berg 	struct sta_info *sta;
103834e89507SJohannes Berg 	int ret;
103934e89507SJohannes Berg 
104034e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
10417852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
104234e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
104334e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
104434e89507SJohannes Berg 
104534e89507SJohannes Berg 	return ret;
104634e89507SJohannes Berg }
104734e89507SJohannes Berg 
104834e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
104934e89507SJohannes Berg 			      const u8 *addr)
105034e89507SJohannes Berg {
105134e89507SJohannes Berg 	struct sta_info *sta;
105234e89507SJohannes Berg 	int ret;
105334e89507SJohannes Berg 
105434e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
10557852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
105634e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
105734e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
105834e89507SJohannes Berg 
105934e89507SJohannes Berg 	return ret;
106034e89507SJohannes Berg }
1061f0706e82SJiri Benc 
106234f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1063f0706e82SJiri Benc {
106434f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1065f0706e82SJiri Benc 	struct sta_info *sta;
10663393a608SJuuso Oikarinen 	bool timer_needed = false;
1067f0706e82SJiri Benc 
1068d0709a65SJohannes Berg 	rcu_read_lock();
1069d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
10703393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
10713393a608SJuuso Oikarinen 			timer_needed = true;
1072d0709a65SJohannes Berg 	rcu_read_unlock();
1073f0706e82SJiri Benc 
10745bb644a0SJohannes Berg 	if (local->quiescing)
10755bb644a0SJohannes Berg 		return;
10765bb644a0SJohannes Berg 
10773393a608SJuuso Oikarinen 	if (!timer_needed)
10783393a608SJuuso Oikarinen 		return;
10793393a608SJuuso Oikarinen 
108026d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
108126d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1082f0706e82SJiri Benc }
1083f0706e82SJiri Benc 
10847bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
10857bedd0cfSJohannes Berg {
10867bedd0cfSJohannes Berg 	int err;
10877bedd0cfSJohannes Berg 
108883e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
10897bedd0cfSJohannes Berg 	if (err)
10907bedd0cfSJohannes Berg 		return err;
10917bedd0cfSJohannes Berg 
10924d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
109334e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1094f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1095f0706e82SJiri Benc 
109634f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
10977bedd0cfSJohannes Berg 	return 0;
1098f0706e82SJiri Benc }
1099f0706e82SJiri Benc 
1100f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1101f0706e82SJiri Benc {
1102a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
110383e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1104f0706e82SJiri Benc }
1105f0706e82SJiri Benc 
1106051007d9SJohannes Berg 
1107e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1108f0706e82SJiri Benc {
1109b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1110f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1111d778207bSJohannes Berg 	LIST_HEAD(free_list);
111244213b5eSJohannes Berg 	int ret = 0;
1113f0706e82SJiri Benc 
1114d0709a65SJohannes Berg 	might_sleep();
1115d0709a65SJohannes Berg 
1116e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1117e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1118e716251dSJohannes Berg 
111934e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
112034e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1121e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1122e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1123d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1124d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
112534316837SJohannes Berg 			ret++;
112634316837SJohannes Berg 		}
112734e89507SJohannes Berg 	}
1128d778207bSJohannes Berg 
1129d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1130d778207bSJohannes Berg 		synchronize_net();
1131d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1132d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1133d778207bSJohannes Berg 	}
113434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
113544213b5eSJohannes Berg 
1136051007d9SJohannes Berg 	return ret;
1137051007d9SJohannes Berg }
1138051007d9SJohannes Berg 
113924723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
114024723d1bSJohannes Berg 			  unsigned long exp_time)
114124723d1bSJohannes Berg {
114224723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
114324723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
114424723d1bSJohannes Berg 
114534e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1146e46a2cf9SMohammed Shafi Shajakhan 
1147e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1148b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1149b8da6b6aSJohannes Berg 
1150ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1151ec2b774eSMarek Lindner 			continue;
1152ec2b774eSMarek Lindner 
1153b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1154eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1155bdcbd8e0SJohannes Berg 				sta->sta.addr);
11563f52b7e3SMarco Porsch 
11573f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
11583f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
11593f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
11603f52b7e3SMarco Porsch 
116134e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
116224723d1bSJohannes Berg 		}
1163e46a2cf9SMohammed Shafi Shajakhan 	}
1164e46a2cf9SMohammed Shafi Shajakhan 
116534e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
116624723d1bSJohannes Berg }
116717741cdcSJohannes Berg 
1168686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1169686b9cb9SBen Greear 						   const u8 *addr,
1170686b9cb9SBen Greear 						   const u8 *localaddr)
117117741cdcSJohannes Berg {
11727bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
117383e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
11747bedd0cfSJohannes Berg 	struct sta_info *sta;
117517741cdcSJohannes Berg 
1176686b9cb9SBen Greear 	/*
1177686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1178686b9cb9SBen Greear 	 * ... first in list.
1179686b9cb9SBen Greear 	 */
118083e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1181686b9cb9SBen Greear 		if (localaddr &&
1182b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1183686b9cb9SBen Greear 			continue;
1184f7c65594SJohannes Berg 		if (!sta->uploaded)
1185f7c65594SJohannes Berg 			return NULL;
118617741cdcSJohannes Berg 		return &sta->sta;
1187f7c65594SJohannes Berg 	}
1188f7c65594SJohannes Berg 
1189abe60632SJohannes Berg 	return NULL;
119017741cdcSJohannes Berg }
1191686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
11925ed176e1SJohannes Berg 
11935ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
11945ed176e1SJohannes Berg 					 const u8 *addr)
11955ed176e1SJohannes Berg {
1196f7c65594SJohannes Berg 	struct sta_info *sta;
11975ed176e1SJohannes Berg 
11985ed176e1SJohannes Berg 	if (!vif)
11995ed176e1SJohannes Berg 		return NULL;
12005ed176e1SJohannes Berg 
1201f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1202f7c65594SJohannes Berg 	if (!sta)
1203f7c65594SJohannes Berg 		return NULL;
12045ed176e1SJohannes Berg 
1205f7c65594SJohannes Berg 	if (!sta->uploaded)
1206f7c65594SJohannes Berg 		return NULL;
1207f7c65594SJohannes Berg 
1208f7c65594SJohannes Berg 	return &sta->sta;
12095ed176e1SJohannes Berg }
121017741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1211af818581SJohannes Berg 
1212e3685e03SJohannes Berg /* powersave support code */
1213e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
121450a9432dSJohannes Berg {
1215608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1216e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1217e3685e03SJohannes Berg 	struct sk_buff_head pending;
1218ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1219e3685e03SJohannes Berg 	unsigned long flags;
1220d012a605SMarco Porsch 	struct ps_data *ps;
1221d012a605SMarco Porsch 
12223918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
12233918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
12243918edb0SFelix Fietkau 				     u.ap);
12253918edb0SFelix Fietkau 
12263918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1227d012a605SMarco Porsch 		ps = &sdata->bss->ps;
12283f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
12293f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1230d012a605SMarco Porsch 	else
1231d012a605SMarco Porsch 		return;
123250a9432dSJohannes Berg 
1233c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
123447086fc5SJohannes Berg 
12355a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1236948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1237ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1238948d887dSJohannes Berg 
123930686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
124012375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1241af818581SJohannes Berg 
1242ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
1243ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1244bb42f2d1SToke Høiland-Jørgensen 			if (!txq_has_queue(sta->sta.txq[i]))
1245ba8c3d6fSFelix Fietkau 				continue;
1246ba8c3d6fSFelix Fietkau 
1247bb42f2d1SToke Høiland-Jørgensen 			drv_wake_tx_queue(local, to_txq_info(sta->sta.txq[i]));
1248ba8c3d6fSFelix Fietkau 		}
1249ba8c3d6fSFelix Fietkau 	}
1250ba8c3d6fSFelix Fietkau 
1251948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1252af818581SJohannes Berg 
12531d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
12541d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1255af818581SJohannes Berg 	/* Send all buffered frames to the station */
1256948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1257948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1258948d887dSJohannes Berg 
1259987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1260948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1261987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1262948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1263948d887dSJohannes Berg 		filtered += tmp - count;
1264948d887dSJohannes Berg 		count = tmp;
1265948d887dSJohannes Berg 
1266987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1267948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1268987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1269948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1270948d887dSJohannes Berg 		buffered += tmp - count;
1271948d887dSJohannes Berg 	}
1272948d887dSJohannes Berg 
1273e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
12745ac2e350SJohannes Berg 
12755ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
12765ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
12775ac2e350SJohannes Berg 
12785ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
12795ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
12805ac2e350SJohannes Berg 	 */
12815ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
12825ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
12831d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1284948d887dSJohannes Berg 
1285e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1286e3685e03SJohannes Berg 
1287687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1288062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1289062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1290687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1291687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1292687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1293687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1294687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1295687da132SEmmanuel Grumbach 		       sta->sta.addr);
1296687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1297687da132SEmmanuel Grumbach 					   sta->sta.addr,
1298687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1299687da132SEmmanuel Grumbach 	}
1300687da132SEmmanuel Grumbach 
1301af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1302af818581SJohannes Berg 
1303c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1304c868cb35SJohannes Berg 
1305bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
13062595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1307bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
130817c18bf8SJohannes Berg 
130917c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1310af818581SJohannes Berg }
1311af818581SJohannes Berg 
13120ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1313b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
13140ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1315ce662b44SJohannes Berg {
13160ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1317ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1318ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1319ce662b44SJohannes Berg 	struct sk_buff *skb;
1320ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1321ce662b44SJohannes Berg 	__le16 fc;
1322a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1323ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
132455de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1325ce662b44SJohannes Berg 
1326ce662b44SJohannes Berg 	if (qos) {
1327ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1328ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1329ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1330ce662b44SJohannes Berg 	} else {
1331ce662b44SJohannes Berg 		size -= 2;
1332ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1333ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1334ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1335ce662b44SJohannes Berg 	}
1336ce662b44SJohannes Berg 
1337ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1338ce662b44SJohannes Berg 	if (!skb)
1339ce662b44SJohannes Berg 		return;
1340ce662b44SJohannes Berg 
1341ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1342ce662b44SJohannes Berg 
13434df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1344ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1345ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1346ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1347ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1348ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1349864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1350ce662b44SJohannes Berg 
1351ce662b44SJohannes Berg 	skb->priority = tid;
1352ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
135359b66255SJohannes Berg 	if (qos) {
1354ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1355ce662b44SJohannes Berg 
13560ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1357ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1358ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
13590ead2510SEmmanuel Grumbach 			if (more_data)
13600ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
13610ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
13620ead2510SEmmanuel Grumbach 		}
1363ce662b44SJohannes Berg 	}
1364ce662b44SJohannes Berg 
1365ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1366ce662b44SJohannes Berg 
1367ce662b44SJohannes Berg 	/*
1368ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1369ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1370deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1371deeaee19SJohannes Berg 	 * ends the poll/service period.
1372ce662b44SJohannes Berg 	 */
137302f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1374deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1375ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1376ce662b44SJohannes Berg 
13776b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
13786b127c71SSujith Manoharan 
1379b77cf4f8SJohannes Berg 	if (call_driver)
1380b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1381b77cf4f8SJohannes Berg 					  reason, false);
138240b96408SJohannes Berg 
138389afe614SJohannes Berg 	skb->dev = sdata->dev;
138489afe614SJohannes Berg 
138555de908aSJohannes Berg 	rcu_read_lock();
138655de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
138755de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
138855de908aSJohannes Berg 		rcu_read_unlock();
138955de908aSJohannes Berg 		kfree_skb(skb);
139055de908aSJohannes Berg 		return;
139155de908aSJohannes Berg 	}
139255de908aSJohannes Berg 
139373c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
13947c10770fSJohannes Berg 	ieee80211_xmit(sdata, sta, skb);
139555de908aSJohannes Berg 	rcu_read_unlock();
1396ce662b44SJohannes Berg }
1397ce662b44SJohannes Berg 
13980a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
13990a1cb809SJohannes Berg {
14000a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
14010a1cb809SJohannes Berg 	if (tids & 0xF8)
14020a1cb809SJohannes Berg 		return fls(tids) - 1;
14030a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
14040a1cb809SJohannes Berg 	if (tids & BIT(0))
14050a1cb809SJohannes Berg 		return 0;
14060a1cb809SJohannes Berg 	return fls(tids) - 1;
14070a1cb809SJohannes Berg }
14080a1cb809SJohannes Berg 
14090ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
14100ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
14110ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
14120ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
14130ead2510SEmmanuel Grumbach  */
14140ead2510SEmmanuel Grumbach static bool
14150ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
14160ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
14170ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
14180ead2510SEmmanuel Grumbach {
14190ead2510SEmmanuel Grumbach 	int ac;
14200ead2510SEmmanuel Grumbach 
14210ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
14220ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
14230ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
14240ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
14250ead2510SEmmanuel Grumbach 	 */
14260ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
14270ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
14280ead2510SEmmanuel Grumbach 		return true;
14290ead2510SEmmanuel Grumbach 
14300ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1431f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
14320ead2510SEmmanuel Grumbach 			continue;
14330ead2510SEmmanuel Grumbach 
14340ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
14350ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
14360ead2510SEmmanuel Grumbach 			return true;
14370ead2510SEmmanuel Grumbach 	}
14380ead2510SEmmanuel Grumbach 
14390ead2510SEmmanuel Grumbach 	return false;
14400ead2510SEmmanuel Grumbach }
14410ead2510SEmmanuel Grumbach 
144247086fc5SJohannes Berg static void
14430ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
14440ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
14450ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
14460ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1447af818581SJohannes Berg {
1448af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1449af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1450948d887dSJohannes Berg 	int ac;
1451af818581SJohannes Berg 
1452f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1453948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
14544049e09aSJohannes Berg 		unsigned long tids;
14554049e09aSJohannes Berg 
1456f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1457948d887dSJohannes Berg 			continue;
1458948d887dSJohannes Berg 
14594049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
14604049e09aSJohannes Berg 
1461f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1462f9f760b4SJohannes Berg 		 * release from hardware queues
1463f9f760b4SJohannes Berg 		 */
14640ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
14650ead2510SEmmanuel Grumbach 			*driver_release_tids |=
14660ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
14670ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1468ba8c3d6fSFelix Fietkau 		}
1469f9f760b4SJohannes Berg 
14700ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
147147086fc5SJohannes Berg 			struct sk_buff *skb;
147247086fc5SJohannes Berg 
147347086fc5SJohannes Berg 			while (n_frames > 0) {
1474948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1475948d887dSJohannes Berg 				if (!skb) {
147647086fc5SJohannes Berg 					skb = skb_dequeue(
147747086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1478af818581SJohannes Berg 					if (skb)
1479af818581SJohannes Berg 						local->total_ps_buffered--;
1480af818581SJohannes Berg 				}
148147086fc5SJohannes Berg 				if (!skb)
148247086fc5SJohannes Berg 					break;
148347086fc5SJohannes Berg 				n_frames--;
14840ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
148547086fc5SJohannes Berg 			}
1486948d887dSJohannes Berg 		}
1487948d887dSJohannes Berg 
14880ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
14890ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
14900ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
14914049e09aSJohannes Berg 		 */
1492948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
14930ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1494948d887dSJohannes Berg 			break;
1495948d887dSJohannes Berg 	}
1496948d887dSJohannes Berg }
1497af818581SJohannes Berg 
14980ead2510SEmmanuel Grumbach static void
14990ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
15000ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
15010ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
15020ead2510SEmmanuel Grumbach {
15030ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
15040ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
15050ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
15060ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
15070ead2510SEmmanuel Grumbach 	bool more_data;
15080ead2510SEmmanuel Grumbach 
15090ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
15100ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
15110ead2510SEmmanuel Grumbach 
15120ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
15130ead2510SEmmanuel Grumbach 
15140ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
15150ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
15160ead2510SEmmanuel Grumbach 
15170ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
15180ead2510SEmmanuel Grumbach 
15191a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
15200ead2510SEmmanuel Grumbach 		driver_release_tids =
15210ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
15220ead2510SEmmanuel Grumbach 
1523f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1524f438ceb8SEmmanuel Grumbach 		int tid, ac;
15254049e09aSJohannes Berg 
1526ce662b44SJohannes Berg 		/*
1527ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1528ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1529ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1530ce662b44SJohannes Berg 		 *
1531ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1532ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1533ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1534ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1535ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1536ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1537ce662b44SJohannes Berg 		 *
1538ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1539ce662b44SJohannes Berg 		 */
1540ce662b44SJohannes Berg 
1541ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1542f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1543d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1544d7f84244SEmmanuel Grumbach 				break;
1545f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1546ce662b44SJohannes Berg 
15470ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1548f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
154947086fc5SJohannes Berg 		struct sk_buff_head pending;
155047086fc5SJohannes Berg 		struct sk_buff *skb;
155140b96408SJohannes Berg 		int num = 0;
155240b96408SJohannes Berg 		u16 tids = 0;
1553b77cf4f8SJohannes Berg 		bool need_null = false;
155447086fc5SJohannes Berg 
155547086fc5SJohannes Berg 		skb_queue_head_init(&pending);
155647086fc5SJohannes Berg 
155747086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1558af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
155947086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
156040b96408SJohannes Berg 			u8 *qoshdr = NULL;
156140b96408SJohannes Berg 
156240b96408SJohannes Berg 			num++;
1563af818581SJohannes Berg 
1564af818581SJohannes Berg 			/*
156547086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
156647086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
156747086fc5SJohannes Berg 			 * exchange.
1568af818581SJohannes Berg 			 */
15696b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
15706b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1571af818581SJohannes Berg 
157247086fc5SJohannes Berg 			/*
157347086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
157447086fc5SJohannes Berg 			 * more buffered frames for this STA
157547086fc5SJohannes Berg 			 */
157624b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
157747086fc5SJohannes Berg 				hdr->frame_control |=
157847086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
157924b9c373SJanusz.Dziedzic@tieto.com 			else
158024b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
158124b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1582af818581SJohannes Berg 
158340b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
158440b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
158540b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
158640b96408SJohannes Berg 
1587b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1588b77cf4f8SJohannes Berg 
1589b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1590b77cf4f8SJohannes Berg 
1591b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1592b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1593b77cf4f8SJohannes Berg 				continue;
1594b77cf4f8SJohannes Berg 
1595b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1596b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1597b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1598b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1599b77cf4f8SJohannes Berg 				break;
1600b77cf4f8SJohannes Berg 			}
1601b77cf4f8SJohannes Berg 
1602b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1603b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1604b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1605b77cf4f8SJohannes Berg 			 * and be done.
1606b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1607b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1608b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1609b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1610b77cf4f8SJohannes Berg 			 *
1611b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1612b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1613b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1614b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1615b77cf4f8SJohannes Berg 			 *
1616b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1617b77cf4f8SJohannes Berg 			 */
1618b77cf4f8SJohannes Berg 			if (qoshdr) {
161940b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1620deeaee19SJohannes Berg 
162147086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
162247086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1623b77cf4f8SJohannes Berg 			} else {
1624b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1625b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1626b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1627b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1628b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1629b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1630b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1631b77cf4f8SJohannes Berg 				 */
1632b77cf4f8SJohannes Berg 				hdr->frame_control |=
1633b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1634b77cf4f8SJohannes Berg 				need_null = true;
1635b77cf4f8SJohannes Berg 				num++;
163652a3f20cSMarco Porsch 			}
1637b77cf4f8SJohannes Berg 			break;
163847086fc5SJohannes Berg 		}
163947086fc5SJohannes Berg 
164040b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
164140b96408SJohannes Berg 					  reason, more_data);
164240b96408SJohannes Berg 
164347086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1644af818581SJohannes Berg 
1645b77cf4f8SJohannes Berg 		if (need_null)
1646b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
16470ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
16480ead2510SEmmanuel Grumbach 				reason, false, false);
1649b77cf4f8SJohannes Berg 
1650c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1651af818581SJohannes Berg 	} else {
1652ba8c3d6fSFelix Fietkau 		int tid;
1653ba8c3d6fSFelix Fietkau 
1654af818581SJohannes Berg 		/*
16554049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
16564049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1657f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1658f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1659f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1660f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1661f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1662f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1663af818581SJohannes Berg 		 */
16644049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
166547086fc5SJohannes Berg 					    n_frames, reason, more_data);
16664049e09aSJohannes Berg 
16674049e09aSJohannes Berg 		/*
16684049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
16694049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1670f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
16714049e09aSJohannes Berg 		 * release function.
1672f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1673ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1674ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
16754049e09aSJohannes Berg 		 */
1676ba8c3d6fSFelix Fietkau 
1677ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1678ba8c3d6fSFelix Fietkau 			return;
1679ba8c3d6fSFelix Fietkau 
1680ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
168183843c80SFelix Fietkau 			if (!(driver_release_tids & BIT(tid)) ||
16821e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1683ba8c3d6fSFelix Fietkau 				continue;
1684ba8c3d6fSFelix Fietkau 
1685ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1686ba8c3d6fSFelix Fietkau 			break;
1687ba8c3d6fSFelix Fietkau 		}
1688af818581SJohannes Berg 	}
1689af818581SJohannes Berg }
1690af818581SJohannes Berg 
1691af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1692af818581SJohannes Berg {
169347086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1694af818581SJohannes Berg 
1695af818581SJohannes Berg 	/*
169647086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
169747086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
169847086fc5SJohannes Berg 	 * only from the non-enabled ones.
1699af818581SJohannes Berg 	 */
170047086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
170147086fc5SJohannes Berg 		ignore_for_response = 0;
1702af818581SJohannes Berg 
170347086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
170447086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1705af818581SJohannes Berg }
170647086fc5SJohannes Berg 
170747086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
170847086fc5SJohannes Berg {
170947086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
171047086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
171147086fc5SJohannes Berg 
171247086fc5SJohannes Berg 	/*
171347086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
171447086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
171547086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
171647086fc5SJohannes Berg 	 * actually getting called.
171747086fc5SJohannes Berg 	 */
171847086fc5SJohannes Berg 	if (!delivery_enabled)
171947086fc5SJohannes Berg 		return;
172047086fc5SJohannes Berg 
172147086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
172247086fc5SJohannes Berg 	case 1:
172347086fc5SJohannes Berg 		n_frames = 2;
172447086fc5SJohannes Berg 		break;
172547086fc5SJohannes Berg 	case 2:
172647086fc5SJohannes Berg 		n_frames = 4;
172747086fc5SJohannes Berg 		break;
172847086fc5SJohannes Berg 	case 3:
172947086fc5SJohannes Berg 		n_frames = 6;
173047086fc5SJohannes Berg 		break;
173147086fc5SJohannes Berg 	case 0:
173247086fc5SJohannes Berg 		/* XXX: what is a good value? */
173313a8098aSAndrei Otcheretianski 		n_frames = 128;
173447086fc5SJohannes Berg 		break;
173547086fc5SJohannes Berg 	}
173647086fc5SJohannes Berg 
173747086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
173847086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1739af818581SJohannes Berg }
1740af818581SJohannes Berg 
1741af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1742af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1743af818581SJohannes Berg {
1744af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1745af818581SJohannes Berg 
1746b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1747b5878a2dSJohannes Berg 
17485ac2e350SJohannes Berg 	if (block) {
1749c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
175017c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
17515ac2e350SJohannes Berg 		return;
17525ac2e350SJohannes Berg 	}
17535ac2e350SJohannes Berg 
17545ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
17555ac2e350SJohannes Berg 		return;
17565ac2e350SJohannes Berg 
17575ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
17585ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
17595ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
17605ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
17615ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
17625ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
17635ac2e350SJohannes Berg 		/* must be asleep in this case */
17645ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
17655ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
17665ac2e350SJohannes Berg 	} else {
17675ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
176817c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
17695ac2e350SJohannes Berg 	}
1770af818581SJohannes Berg }
1771af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1772dcf55fb5SFelix Fietkau 
1773e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
177437fbd908SJohannes Berg {
177537fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
177637fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
177737fbd908SJohannes Berg 
177837fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
177937fbd908SJohannes Berg 
178037fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
178137fbd908SJohannes Berg }
1782e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
178337fbd908SJohannes Berg 
17840ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
17850ead2510SEmmanuel Grumbach {
17860ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
17870ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
17880ead2510SEmmanuel Grumbach 	bool more_data;
17890ead2510SEmmanuel Grumbach 
17900ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
17910ead2510SEmmanuel Grumbach 
17920ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
17930ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
17940ead2510SEmmanuel Grumbach 					       reason, 0);
17950ead2510SEmmanuel Grumbach 
17960ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
17970ead2510SEmmanuel Grumbach }
17980ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
17990ead2510SEmmanuel Grumbach 
1800042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1801042ec453SJohannes Berg 				u8 tid, bool buffered)
1802dcf55fb5SFelix Fietkau {
1803dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1804dcf55fb5SFelix Fietkau 
18055a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1806042ec453SJohannes Berg 		return;
1807042ec453SJohannes Berg 
18081b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
18091b000789SJohannes Berg 
1810948d887dSJohannes Berg 	if (buffered)
1811948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1812948d887dSJohannes Berg 	else
1813948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1814948d887dSJohannes Berg 
1815c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1816dcf55fb5SFelix Fietkau }
1817042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1818d9a7ddb0SJohannes Berg 
181983d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1820d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1821d9a7ddb0SJohannes Berg {
18228bf11d8dSJohannes Berg 	might_sleep();
1823d9a7ddb0SJohannes Berg 
1824d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1825d9a7ddb0SJohannes Berg 		return 0;
1826d9a7ddb0SJohannes Berg 
1827f09603a2SJohannes Berg 	/* check allowed transitions first */
1828f09603a2SJohannes Berg 
1829d9a7ddb0SJohannes Berg 	switch (new_state) {
1830d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1831f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1832d9a7ddb0SJohannes Berg 			return -EINVAL;
1833d9a7ddb0SJohannes Berg 		break;
1834d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1835f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1836f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1837d9a7ddb0SJohannes Berg 			return -EINVAL;
1838d9a7ddb0SJohannes Berg 		break;
1839d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1840f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1841f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1842d9a7ddb0SJohannes Berg 			return -EINVAL;
1843d9a7ddb0SJohannes Berg 		break;
1844d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1845f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1846d9a7ddb0SJohannes Berg 			return -EINVAL;
1847d9a7ddb0SJohannes Berg 		break;
1848d9a7ddb0SJohannes Berg 	default:
1849d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1850d9a7ddb0SJohannes Berg 		return -EINVAL;
1851d9a7ddb0SJohannes Berg 	}
1852d9a7ddb0SJohannes Berg 
1853bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1854bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1855f09603a2SJohannes Berg 
1856f09603a2SJohannes Berg 	/*
1857f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1858f09603a2SJohannes Berg 	 * fail the transition
1859f09603a2SJohannes Berg 	 */
1860f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1861f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1862f09603a2SJohannes Berg 					sta->sta_state, new_state);
1863f09603a2SJohannes Berg 		if (err)
1864f09603a2SJohannes Berg 			return err;
1865f09603a2SJohannes Berg 	}
1866f09603a2SJohannes Berg 
1867f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1868f09603a2SJohannes Berg 
1869f09603a2SJohannes Berg 	switch (new_state) {
1870f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1871f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1872f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1873f09603a2SJohannes Berg 		break;
1874f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1875a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
1876f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1877a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1878f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1879a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
188052cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
188152cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1882a7201a6cSIlan Peer 		}
1883f09603a2SJohannes Berg 		break;
1884f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1885f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1886f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1887a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
188852cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
188952cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1890f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
189172f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
1892f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
189317c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
189449ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
1895f09603a2SJohannes Berg 		}
1896f09603a2SJohannes Berg 		break;
1897f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1898f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
189972f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
1900f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
190117c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
190249ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
1903f09603a2SJohannes Berg 		}
1904f09603a2SJohannes Berg 		break;
1905f09603a2SJohannes Berg 	default:
1906f09603a2SJohannes Berg 		break;
1907f09603a2SJohannes Berg 	}
1908f09603a2SJohannes Berg 
1909d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1910d9a7ddb0SJohannes Berg 
1911d9a7ddb0SJohannes Berg 	return 0;
1912d9a7ddb0SJohannes Berg }
1913687da132SEmmanuel Grumbach 
1914687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1915687da132SEmmanuel Grumbach {
1916687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1917687da132SEmmanuel Grumbach 	u8 rx_streams;
1918687da132SEmmanuel Grumbach 
1919687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1920687da132SEmmanuel Grumbach 		return 1;
1921687da132SEmmanuel Grumbach 
1922687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1923687da132SEmmanuel Grumbach 		int i;
1924687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1925687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1926687da132SEmmanuel Grumbach 
1927687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1928687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1929687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1930687da132SEmmanuel Grumbach 				return i + 1;
1931687da132SEmmanuel Grumbach 	}
1932687da132SEmmanuel Grumbach 
1933687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1934687da132SEmmanuel Grumbach 		rx_streams = 4;
1935687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1936687da132SEmmanuel Grumbach 		rx_streams = 3;
1937687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1938687da132SEmmanuel Grumbach 		rx_streams = 2;
1939687da132SEmmanuel Grumbach 	else
1940687da132SEmmanuel Grumbach 		rx_streams = 1;
1941687da132SEmmanuel Grumbach 
1942687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1943687da132SEmmanuel Grumbach 		return rx_streams;
1944687da132SEmmanuel Grumbach 
1945687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1946687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1947687da132SEmmanuel Grumbach }
1948b7ffbd7eSJohannes Berg 
1949c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
1950c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
1951c9c5962bSJohannes Berg {
1952c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
1953c9c5962bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1954c9c5962bSJohannes Berg 	int cpu;
1955c9c5962bSJohannes Berg 
1956c9c5962bSJohannes Berg 	if (!ieee80211_hw_check(&local->hw, USES_RSS))
1957c9c5962bSJohannes Berg 		return stats;
1958c9c5962bSJohannes Berg 
1959c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
1960c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
1961c9c5962bSJohannes Berg 
1962c9c5962bSJohannes Berg 		cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
1963c9c5962bSJohannes Berg 
1964c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
1965c9c5962bSJohannes Berg 			stats = cpustats;
1966c9c5962bSJohannes Berg 	}
1967c9c5962bSJohannes Berg 
1968c9c5962bSJohannes Berg 	return stats;
1969c9c5962bSJohannes Berg }
1970c9c5962bSJohannes Berg 
19714f6b1b3dSJohannes Berg static void sta_stats_decode_rate(struct ieee80211_local *local, u16 rate,
19724f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
1973fbd6ff5cSJohannes Berg {
1974dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
1975fbd6ff5cSJohannes Berg 
1976dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
19777f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
19784f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
1979dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
1980dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
1981dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
1982dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
19837f406cd1SJohannes Berg 		break;
19847f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
19854f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
1986dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
1987dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
1988dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
19897f406cd1SJohannes Berg 		break;
19907f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
1991fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
1992fbd6ff5cSJohannes Berg 		u16 brate;
19934f6b1b3dSJohannes Berg 		unsigned int shift;
1994dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
1995dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
1996fbd6ff5cSJohannes Berg 
1997dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
1998dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
19994f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
20004f6b1b3dSJohannes Berg 			shift = 2;
20014f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
20024f6b1b3dSJohannes Berg 			shift = 1;
20034f6b1b3dSJohannes Berg 		else
20044f6b1b3dSJohannes Berg 			shift = 0;
2005fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
20067f406cd1SJohannes Berg 		break;
20077f406cd1SJohannes Berg 		}
2008fbd6ff5cSJohannes Berg 	}
20094f6b1b3dSJohannes Berg }
2010fbd6ff5cSJohannes Berg 
2011a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
20124f6b1b3dSJohannes Berg {
20136aa7de05SMark Rutland 	u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
20144f6b1b3dSJohannes Berg 
20154f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2016a17d93ffSBen Greear 		return -EINVAL;
2017a17d93ffSBen Greear 
20184f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2019a17d93ffSBen Greear 	return 0;
2020fbd6ff5cSJohannes Berg }
2021fbd6ff5cSJohannes Berg 
20220f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
20230f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
20240f9c5a61SJohannes Berg 			     int tid)
20250f9c5a61SJohannes Berg {
20260f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
20270f9c5a61SJohannes Berg 
20280f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
20290f9c5a61SJohannes Berg 		unsigned int start;
20300f9c5a61SJohannes Berg 
20310f9c5a61SJohannes Berg 		do {
20320f9c5a61SJohannes Berg 			start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
20330f9c5a61SJohannes Berg 			tidstats->rx_msdu = sta->rx_stats.msdu[tid];
20340f9c5a61SJohannes Berg 		} while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
20350f9c5a61SJohannes Berg 
20360f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
20370f9c5a61SJohannes Berg 	}
20380f9c5a61SJohannes Berg 
20390f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
20400f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
20410f9c5a61SJohannes Berg 		tidstats->tx_msdu = sta->tx_stats.msdu[tid];
20420f9c5a61SJohannes Berg 	}
20430f9c5a61SJohannes Berg 
20440f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
20450f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
20460f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
20470f9c5a61SJohannes Berg 		tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
20480f9c5a61SJohannes Berg 	}
20490f9c5a61SJohannes Berg 
20500f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
20510f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
20520f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
20530f9c5a61SJohannes Berg 		tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
20540f9c5a61SJohannes Berg 	}
2055*2fe4a29aSToke Høiland-Jørgensen 
2056*2fe4a29aSToke Høiland-Jørgensen 	if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
2057*2fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
2058*2fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
2059*2fe4a29aSToke Høiland-Jørgensen 
2060*2fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2061*2fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
2062*2fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
2063*2fe4a29aSToke Høiland-Jørgensen 
2064*2fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
2065*2fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
2066*2fe4a29aSToke Høiland-Jørgensen 	}
20670f9c5a61SJohannes Berg }
20680f9c5a61SJohannes Berg 
2069c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2070c9c5962bSJohannes Berg {
2071c9c5962bSJohannes Berg 	unsigned int start;
2072c9c5962bSJohannes Berg 	u64 value;
2073c9c5962bSJohannes Berg 
2074c9c5962bSJohannes Berg 	do {
2075c9c5962bSJohannes Berg 		start = u64_stats_fetch_begin(&rxstats->syncp);
2076c9c5962bSJohannes Berg 		value = rxstats->bytes;
2077c9c5962bSJohannes Berg 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2078c9c5962bSJohannes Berg 
2079c9c5962bSJohannes Berg 	return value;
2080c9c5962bSJohannes Berg }
2081c9c5962bSJohannes Berg 
2082b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
2083b7ffbd7eSJohannes Berg {
2084b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2085b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2086b7ffbd7eSJohannes Berg 	u32 thr = 0;
2087c9c5962bSJohannes Berg 	int i, ac, cpu;
2088c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2089c9c5962bSJohannes Berg 
2090c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2091b7ffbd7eSJohannes Berg 
2092b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2093b7ffbd7eSJohannes Berg 
2094225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2095225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2096225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2097225b8189SJohannes Berg 	 */
2098225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2099225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2100225b8189SJohannes Berg 
21012b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
21022b9a7e1bSJohannes Berg 
2103319090bfSJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
2104319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_STA_FLAGS) |
2105319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BSS_PARAM) |
2106319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
2107976bd9efSJohannes Berg 			 BIT(NL80211_STA_INFO_RX_DROP_MISC);
2108976bd9efSJohannes Berg 
2109976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2110976bd9efSJohannes Berg 		sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2111976bd9efSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
2112976bd9efSJohannes Berg 	}
2113b7ffbd7eSJohannes Berg 
211484b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2115e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2116b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
21172b9a7e1bSJohannes Berg 
2118319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
2119319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_TX_BYTES)))) {
2120b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
21212b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2122e5a9f8d0SJohannes Berg 			sinfo->tx_bytes += sta->tx_stats.bytes[ac];
2123319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
2124b7ffbd7eSJohannes Berg 	}
21252b9a7e1bSJohannes Berg 
2126319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
21272b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
21282b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2129e5a9f8d0SJohannes Berg 			sinfo->tx_packets += sta->tx_stats.packets[ac];
2130319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
21312b9a7e1bSJohannes Berg 	}
21322b9a7e1bSJohannes Berg 
2133319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
2134319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_RX_BYTES)))) {
2135c9c5962bSJohannes Berg 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
21360f9c5a61SJohannes Berg 
2137c9c5962bSJohannes Berg 		if (sta->pcpu_rx_stats) {
2138c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2139c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2140c9c5962bSJohannes Berg 
2141c9c5962bSJohannes Berg 				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2142c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2143c9c5962bSJohannes Berg 			}
2144c9c5962bSJohannes Berg 		}
2145c9c5962bSJohannes Berg 
2146319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
21472b9a7e1bSJohannes Berg 	}
21482b9a7e1bSJohannes Berg 
2149319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
2150e5a9f8d0SJohannes Berg 		sinfo->rx_packets = sta->rx_stats.packets;
2151c9c5962bSJohannes Berg 		if (sta->pcpu_rx_stats) {
2152c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2153c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2154c9c5962bSJohannes Berg 
2155c9c5962bSJohannes Berg 				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2156c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2157c9c5962bSJohannes Berg 			}
2158c9c5962bSJohannes Berg 		}
2159319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
21602b9a7e1bSJohannes Berg 	}
21612b9a7e1bSJohannes Berg 
2162319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
2163e5a9f8d0SJohannes Berg 		sinfo->tx_retries = sta->status_stats.retry_count;
2164319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
21652b9a7e1bSJohannes Berg 	}
21662b9a7e1bSJohannes Berg 
2167319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
2168e5a9f8d0SJohannes Berg 		sinfo->tx_failed = sta->status_stats.retry_failed;
2169319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
21702b9a7e1bSJohannes Berg 	}
21712b9a7e1bSJohannes Berg 
2172e5a9f8d0SJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_stats.dropped;
2173c9c5962bSJohannes Berg 	if (sta->pcpu_rx_stats) {
2174c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2175c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2176c9c5962bSJohannes Berg 
2177c9c5962bSJohannes Berg 			cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2178e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2179c9c5962bSJohannes Berg 		}
2180c9c5962bSJohannes Berg 	}
2181b7ffbd7eSJohannes Berg 
2182225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2183225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2184225b8189SJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
2185225b8189SJohannes Berg 				 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2186225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2187225b8189SJohannes Berg 	}
2188225b8189SJohannes Berg 
218930686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
219030686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2191319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
2192c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2193319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
2194b7ffbd7eSJohannes Berg 		}
21952b9a7e1bSJohannes Berg 
2196c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats &&
2197c9c5962bSJohannes Berg 		    !(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
219840d9a38aSJohannes Berg 			sinfo->signal_avg =
21990be6ed13SJohannes Berg 				-ewma_signal_read(&sta->rx_stats_avg.signal);
2200319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
22012b9a7e1bSJohannes Berg 		}
22022b9a7e1bSJohannes Berg 	}
22032b9a7e1bSJohannes Berg 
2204c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2205c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2206c9c5962bSJohannes Berg 	 * pcpu statistics
2207c9c5962bSJohannes Berg 	 */
2208c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2209319090bfSJohannes Berg 	    !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2210319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2211c9c5962bSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL);
2212c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats)
2213c9c5962bSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2214b7ffbd7eSJohannes Berg 
2215c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2216c9c5962bSJohannes Berg 
2217b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2218e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2219c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2220b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
22210be6ed13SJohannes Berg 				-ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
2222b7ffbd7eSJohannes Berg 		}
2223b7ffbd7eSJohannes Berg 	}
2224b7ffbd7eSJohannes Berg 
2225319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
2226e5a9f8d0SJohannes Berg 		sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2227e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2228319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
22292b9a7e1bSJohannes Berg 	}
22302b9a7e1bSJohannes Berg 
2231319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
2232a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2233319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
22342b9a7e1bSJohannes Berg 	}
2235b7ffbd7eSJohannes Berg 
223679c892b8SJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
223779c892b8SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
223879c892b8SJohannes Berg 		struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
223979c892b8SJohannes Berg 
22400f9c5a61SJohannes Berg 		sta_set_tidstats(sta, tidstats, i);
224179c892b8SJohannes Berg 	}
224279c892b8SJohannes Berg 
2243b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2244b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2245319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2246319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLID) |
2247319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLINK_STATE) |
2248319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_LOCAL_PM) |
2249319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PEER_PM) |
2250319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_NONPEER_PM);
2251b7ffbd7eSJohannes Berg 
2252433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2253433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2254433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2255b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2256319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
2257433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2258b7ffbd7eSJohannes Berg 		}
2259433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2260433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2261433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2262b7ffbd7eSJohannes Berg #endif
2263b7ffbd7eSJohannes Berg 	}
2264b7ffbd7eSJohannes Berg 
2265b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2266b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2267b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2268b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2269b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2270b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2271b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2272785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2273b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2274b7ffbd7eSJohannes Berg 
2275b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2276b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2277b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2278b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2279b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2280b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2281b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2282b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2283b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2284b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2285b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2286b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2287a74a8c84SJohannes Berg 	if (sta->sta.wme)
2288b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2289b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2290b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2291b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2292b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2293b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2294b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2295b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2296b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2297b7ffbd7eSJohannes Berg 
22983b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
22993b17fbf8SMaxim Altshul 
23003b17fbf8SMaxim Altshul 	if (thr != 0) {
23013b17fbf8SMaxim Altshul 		sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
23023b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
23033b17fbf8SMaxim Altshul 	}
2304a78b26ffSVenkateswara Naralasetty 
2305a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2306a78b26ffSVenkateswara Naralasetty 	    sta->status_stats.ack_signal_filled) {
2307a78b26ffSVenkateswara Naralasetty 		sinfo->ack_signal = sta->status_stats.last_ack_signal;
2308a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2309a78b26ffSVenkateswara Naralasetty 	}
2310cc60dbbfSBalaji Pothunoori 
2311cc60dbbfSBalaji Pothunoori 	if (ieee80211_hw_check(&sta->local->hw, REPORTS_TX_ACK_STATUS) &&
2312cc60dbbfSBalaji Pothunoori 	    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_DATA_ACK_SIGNAL_AVG))) {
2313cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2314cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2315cc60dbbfSBalaji Pothunoori 				&sta->status_stats.avg_ack_signal);
2316cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
2317cc60dbbfSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_DATA_ACK_SIGNAL_AVG);
2318cc60dbbfSBalaji Pothunoori 	}
23193b17fbf8SMaxim Altshul }
23203b17fbf8SMaxim Altshul 
23213b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
23223b17fbf8SMaxim Altshul {
23233b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
23243b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
23253b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
23263b17fbf8SMaxim Altshul 	u32 thr = 0;
23273b17fbf8SMaxim Altshul 
23283b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
23293b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
23303b17fbf8SMaxim Altshul 
2331b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2332b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2333b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2334b7ffbd7eSJohannes Berg 	else
23354fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2336b7ffbd7eSJohannes Berg 
23373b17fbf8SMaxim Altshul 	return thr;
2338b7ffbd7eSJohannes Berg }
2339b8da6b6aSJohannes Berg 
2340b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2341b8da6b6aSJohannes Berg {
2342c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2343c9c5962bSJohannes Berg 
2344c9c5962bSJohannes Berg 	if (time_after(stats->last_rx, sta->status_stats.last_ack))
2345c9c5962bSJohannes Berg 		return stats->last_rx;
2346b8da6b6aSJohannes Berg 	return sta->status_stats.last_ack;
2347b8da6b6aSJohannes Berg }
2348484a54c2SToke Høiland-Jørgensen 
2349484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2350484a54c2SToke Høiland-Jørgensen {
2351484a54c2SToke Høiland-Jørgensen 	if (!sta->sdata->local->ops->wake_tx_queue)
2352484a54c2SToke Høiland-Jørgensen 		return;
2353484a54c2SToke Høiland-Jørgensen 
2354484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2355484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2356484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2357484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2358484a54c2SToke Høiland-Jørgensen 	} else {
2359484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2360484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2361484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2362484a54c2SToke Høiland-Jørgensen 	}
2363484a54c2SToke Høiland-Jørgensen }
2364484a54c2SToke Høiland-Jørgensen 
2365484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2366484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2367484a54c2SToke Høiland-Jørgensen {
2368484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2369484a54c2SToke Høiland-Jørgensen 
2370484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2371484a54c2SToke Høiland-Jørgensen }
2372