xref: /openbmc/linux/net/mac80211/sta_info.c (revision 4df864c1d9afb46e2461a9f808d9f11a42d31bad)
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
6f0706e82SJiri Benc  *
7f0706e82SJiri Benc  * This program is free software; you can redistribute it and/or modify
8f0706e82SJiri Benc  * it under the terms of the GNU General Public License version 2 as
9f0706e82SJiri Benc  * published by the Free Software Foundation.
10f0706e82SJiri Benc  */
11f0706e82SJiri Benc 
12f0706e82SJiri Benc #include <linux/module.h>
13f0706e82SJiri Benc #include <linux/init.h>
14888d04dfSFelix Fietkau #include <linux/etherdevice.h>
15f0706e82SJiri Benc #include <linux/netdevice.h>
16f0706e82SJiri Benc #include <linux/types.h>
17f0706e82SJiri Benc #include <linux/slab.h>
18f0706e82SJiri Benc #include <linux/skbuff.h>
19f0706e82SJiri Benc #include <linux/if_arp.h>
200d174406SJohannes Berg #include <linux/timer.h>
21d0709a65SJohannes Berg #include <linux/rtnetlink.h>
22f0706e82SJiri Benc 
23484a54c2SToke Høiland-Jørgensen #include <net/codel.h>
24f0706e82SJiri Benc #include <net/mac80211.h>
25f0706e82SJiri Benc #include "ieee80211_i.h"
2624487981SJohannes Berg #include "driver-ops.h"
272c8dccc7SJohannes Berg #include "rate.h"
28f0706e82SJiri Benc #include "sta_info.h"
29e9f207f0SJiri Benc #include "debugfs_sta.h"
30ee385855SLuis Carlos Cobo #include "mesh.h"
31ce662b44SJohannes Berg #include "wme.h"
32f0706e82SJiri Benc 
33d0709a65SJohannes Berg /**
34d0709a65SJohannes Berg  * DOC: STA information lifetime rules
35d0709a65SJohannes Berg  *
36d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
37d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
38d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
39d0709a65SJohannes Berg  *
4034e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
4134e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4234e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4334e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4434e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4534e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4634e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4734e89507SJohannes Berg  * encryption keys.
4893e5deb1SJohannes Berg  *
4993e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
5093e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
51d0709a65SJohannes Berg  *
5234e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
537e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
547e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5525985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5634e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5725985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5834e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
597e189a12SLuis R. Rodriguez  *
6034e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
6134e89507SJohannes Berg  * calls are available.
62d0709a65SJohannes Berg  *
6334e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6434e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6534e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6634e89507SJohannes Berg  * freed before they are done using it.
67d0709a65SJohannes Berg  */
68f0706e82SJiri Benc 
697bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
707bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
71caf22d31SJohannes Berg 	.automatic_shrinking = true,
727bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
73ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
747bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
75ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
767bedd0cfSJohannes Berg };
777bedd0cfSJohannes Berg 
784d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
79be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
80f0706e82SJiri Benc 			     struct sta_info *sta)
81f0706e82SJiri Benc {
8283e7e4ceSHerbert Xu 	return rhltable_remove(&local->sta_hash, &sta->hash_node,
837bedd0cfSJohannes Berg 			       sta_rht_params);
84f0706e82SJiri Benc }
85f0706e82SJiri Benc 
865108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
87b22cfcfcSEliad Peller {
88b22cfcfcSEliad Peller 	int ac, i;
89b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
90b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
91b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
92fa962b92SMichal Kazior 	struct fq *fq = &local->fq;
93d012a605SMarco Porsch 	struct ps_data *ps;
94b22cfcfcSEliad Peller 
95e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
965ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
975ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
98d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
99d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
100d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1013f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1023f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
103d012a605SMarco Porsch 		else
104d012a605SMarco Porsch 			return;
105b22cfcfcSEliad Peller 
106b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
107e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1085ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
109b22cfcfcSEliad Peller 
110d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
111b22cfcfcSEliad Peller 	}
112b22cfcfcSEliad Peller 
113ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
114ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
115ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
116ba8c3d6fSFelix Fietkau 
117fa962b92SMichal Kazior 			spin_lock_bh(&fq->lock);
118fa962b92SMichal Kazior 			ieee80211_txq_purge(local, txqi);
119fa962b92SMichal Kazior 			spin_unlock_bh(&fq->lock);
120ba8c3d6fSFelix Fietkau 		}
121ba8c3d6fSFelix Fietkau 	}
122ba8c3d6fSFelix Fietkau 
123b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
124b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1251f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1261f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
127b22cfcfcSEliad Peller 	}
128b22cfcfcSEliad Peller 
12945b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
13045b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
131b22cfcfcSEliad Peller 
1325ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
133b22cfcfcSEliad Peller 
134b22cfcfcSEliad Peller 	/*
135b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
136b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
137b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
138b22cfcfcSEliad Peller 	 * directly by station destruction.
139b22cfcfcSEliad Peller 	 */
1405a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
141661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
142b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
143b22cfcfcSEliad Peller 		if (!tid_tx)
144b22cfcfcSEliad Peller 			continue;
1451f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
146b22cfcfcSEliad Peller 		kfree(tid_tx);
147b22cfcfcSEliad Peller 	}
1485108ca82SJohannes Berg }
149b22cfcfcSEliad Peller 
1505108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1515108ca82SJohannes Berg {
1525108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1535108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1545108ca82SJohannes Berg 
1555108ca82SJohannes Berg 	__cleanup_single_sta(sta);
156b22cfcfcSEliad Peller 	sta_info_free(local, sta);
157b22cfcfcSEliad Peller }
158b22cfcfcSEliad Peller 
15983e7e4ceSHerbert Xu struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
16083e7e4ceSHerbert Xu 					 const u8 *addr)
16183e7e4ceSHerbert Xu {
16283e7e4ceSHerbert Xu 	return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
16383e7e4ceSHerbert Xu }
16483e7e4ceSHerbert Xu 
165d0709a65SJohannes Berg /* protected by RCU */
166abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
167abe60632SJohannes Berg 			      const u8 *addr)
16843ba7e95SJohannes Berg {
169abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
17083e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
17160f4b626SJohannes Berg 	struct sta_info *sta;
17243ba7e95SJohannes Berg 
17360f4b626SJohannes Berg 	rcu_read_lock();
17483e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
17560f4b626SJohannes Berg 		if (sta->sdata == sdata) {
17660f4b626SJohannes Berg 			rcu_read_unlock();
17760f4b626SJohannes Berg 			/* this is safe as the caller must already hold
17860f4b626SJohannes Berg 			 * another rcu read section or the mutex
17960f4b626SJohannes Berg 			 */
18060f4b626SJohannes Berg 			return sta;
18160f4b626SJohannes Berg 		}
18260f4b626SJohannes Berg 	}
18360f4b626SJohannes Berg 	rcu_read_unlock();
18460f4b626SJohannes Berg 	return NULL;
18543ba7e95SJohannes Berg }
18643ba7e95SJohannes Berg 
1870e5ded5aSFelix Fietkau /*
1880e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1890e5ded5aSFelix Fietkau  * or from one of its vlans
1900e5ded5aSFelix Fietkau  */
1910e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1920e5ded5aSFelix Fietkau 				  const u8 *addr)
1930e5ded5aSFelix Fietkau {
1940e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
19583e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
1960e5ded5aSFelix Fietkau 	struct sta_info *sta;
1970e5ded5aSFelix Fietkau 
1987bedd0cfSJohannes Berg 	rcu_read_lock();
19983e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
2007bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
2017bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
2027bedd0cfSJohannes Berg 			rcu_read_unlock();
2037bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
2047bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
2057bedd0cfSJohannes Berg 			 */
2060e5ded5aSFelix Fietkau 			return sta;
2070e5ded5aSFelix Fietkau 		}
2087bedd0cfSJohannes Berg 	}
2097bedd0cfSJohannes Berg 	rcu_read_unlock();
2107bedd0cfSJohannes Berg 	return NULL;
2117bedd0cfSJohannes Berg }
2120e5ded5aSFelix Fietkau 
2133b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2143b53fde8SJohannes Berg 				     int idx)
215ee385855SLuis Carlos Cobo {
2163b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
217ee385855SLuis Carlos Cobo 	struct sta_info *sta;
218ee385855SLuis Carlos Cobo 	int i = 0;
219ee385855SLuis Carlos Cobo 
220d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
2213b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2222a8ca29aSLuis Carlos Cobo 			continue;
223ee385855SLuis Carlos Cobo 		if (i < idx) {
224ee385855SLuis Carlos Cobo 			++i;
225ee385855SLuis Carlos Cobo 			continue;
226ee385855SLuis Carlos Cobo 		}
2272a8ca29aSLuis Carlos Cobo 		return sta;
228ee385855SLuis Carlos Cobo 	}
229ee385855SLuis Carlos Cobo 
230ee385855SLuis Carlos Cobo 	return NULL;
231ee385855SLuis Carlos Cobo }
232f0706e82SJiri Benc 
23393e5deb1SJohannes Berg /**
234d9a7ddb0SJohannes Berg  * sta_info_free - free STA
23593e5deb1SJohannes Berg  *
2366ef307bcSRandy Dunlap  * @local: pointer to the global information
23793e5deb1SJohannes Berg  * @sta: STA info to free
23893e5deb1SJohannes Berg  *
23993e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
240d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
241d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
242d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
24393e5deb1SJohannes Berg  */
244d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
24593e5deb1SJohannes Berg {
246889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2474b7679a5SJohannes Berg 		rate_control_free_sta(sta);
24893e5deb1SJohannes Berg 
249bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
25093e5deb1SJohannes Berg 
251ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
252ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
25353d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
254433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
255433f5bc1SJohannes Berg 	kfree(sta->mesh);
256433f5bc1SJohannes Berg #endif
257c9c5962bSJohannes Berg 	free_percpu(sta->pcpu_rx_stats);
25893e5deb1SJohannes Berg 	kfree(sta);
25993e5deb1SJohannes Berg }
26093e5deb1SJohannes Berg 
2614d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
26262b14b24SJohannes Berg static int sta_info_hash_add(struct ieee80211_local *local,
263d0709a65SJohannes Berg 			     struct sta_info *sta)
264f0706e82SJiri Benc {
26583e7e4ceSHerbert Xu 	return rhltable_insert(&local->sta_hash, &sta->hash_node,
2667bedd0cfSJohannes Berg 			       sta_rht_params);
267f0706e82SJiri Benc }
268f0706e82SJiri Benc 
2695ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
270af818581SJohannes Berg {
271af818581SJohannes Berg 	struct sta_info *sta;
272af818581SJohannes Berg 
2735ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
274af818581SJohannes Berg 
275af818581SJohannes Berg 	if (sta->dead)
276af818581SJohannes Berg 		return;
277af818581SJohannes Berg 
27854420473SHelmut Schaa 	local_bh_disable();
2795ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
280af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2815ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
282af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2835ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
28447086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
285ce662b44SJohannes Berg 	local_bh_enable();
286af818581SJohannes Berg }
287af818581SJohannes Berg 
288af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
289af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
290af65cd96SJohannes Berg {
29130686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
292af65cd96SJohannes Berg 		return 0;
293af65cd96SJohannes Berg 
294889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
295af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
29635c347acSJohannes Berg 						     sta, gfp);
297889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
298af65cd96SJohannes Berg 		return -ENOMEM;
299af65cd96SJohannes Berg 
300af65cd96SJohannes Berg 	return 0;
301af65cd96SJohannes Berg }
302af65cd96SJohannes Berg 
30373651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
30456544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
305f0706e82SJiri Benc {
306d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
307ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
308f0706e82SJiri Benc 	struct sta_info *sta;
30916c5f15cSRon Rindjunsky 	int i;
310f0706e82SJiri Benc 
311ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
312f0706e82SJiri Benc 	if (!sta)
31373651ee6SJohannes Berg 		return NULL;
314f0706e82SJiri Benc 
315c9c5962bSJohannes Berg 	if (ieee80211_hw_check(hw, USES_RSS)) {
316c9c5962bSJohannes Berg 		sta->pcpu_rx_stats =
317c9c5962bSJohannes Berg 			alloc_percpu(struct ieee80211_sta_rx_stats);
318c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats)
319c9c5962bSJohannes Berg 			goto free;
320c9c5962bSJohannes Berg 	}
321c9c5962bSJohannes Berg 
32207346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3231d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
3245ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
32567c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
326a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
32787f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
328433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
329433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
330433f5bc1SJohannes Berg 		if (!sta->mesh)
331433f5bc1SJohannes Berg 			goto free;
332433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
33387f59c70SThomas Pedersen 		if (ieee80211_vif_is_mesh(&sdata->vif) &&
33487f59c70SThomas Pedersen 		    !sdata->u.mesh.user_mpm)
335433f5bc1SJohannes Berg 			init_timer(&sta->mesh->plink_timer);
336433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
337433f5bc1SJohannes Berg 	}
33887f59c70SThomas Pedersen #endif
33907346f81SJohannes Berg 
340ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
34117741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
342480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
343480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
344480dd46bSMaxim Altshul 
345d0709a65SJohannes Berg 	sta->local = local;
346d0709a65SJohannes Berg 	sta->sdata = sdata;
347e5a9f8d0SJohannes Berg 	sta->rx_stats.last_rx = jiffies;
348f0706e82SJiri Benc 
3490f9c5a61SJohannes Berg 	u64_stats_init(&sta->rx_stats.syncp);
3500f9c5a61SJohannes Berg 
35171ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
35271ec375cSJohannes Berg 
353b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
354b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
355b6da911bSLiad Kaufman 
35684b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
3570be6ed13SJohannes Berg 	ewma_signal_init(&sta->rx_stats_avg.signal);
3580be6ed13SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
3590be6ed13SJohannes Berg 		ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
360541a45a1SBruno Randolf 
361ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
362ba8c3d6fSFelix Fietkau 		void *txq_data;
363ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
364ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
365ba8c3d6fSFelix Fietkau 
366ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
367ba8c3d6fSFelix Fietkau 		if (!txq_data)
368ba8c3d6fSFelix Fietkau 			goto free;
369ba8c3d6fSFelix Fietkau 
370ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
371ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
372ba8c3d6fSFelix Fietkau 
373fa962b92SMichal Kazior 			ieee80211_txq_init(sdata, sta, txq, i);
374abfbc3afSJohannes Berg 		}
375ba8c3d6fSFelix Fietkau 	}
376ba8c3d6fSFelix Fietkau 
377ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
378ba8c3d6fSFelix Fietkau 		goto free_txq;
379f0706e82SJiri Benc 
3805a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
381a622ab72SJohannes Berg 		/*
382a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
383a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
384a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
385a622ab72SJohannes Berg 		 */
38616c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
38716c5f15cSRon Rindjunsky 	}
388948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
389948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
390948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
391948d887dSJohannes Berg 	}
39273651ee6SJohannes Berg 
3935a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3944be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
395cccaec98SSenthil Balasubramanian 
396af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
397687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
398687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
39921a8e9ddSMohammed Shafi Shajakhan 		struct ieee80211_supported_band *sband;
40021a8e9ddSMohammed Shafi Shajakhan 		u8 smps;
40121a8e9ddSMohammed Shafi Shajakhan 
40221a8e9ddSMohammed Shafi Shajakhan 		sband = ieee80211_get_sband(sdata);
40321a8e9ddSMohammed Shafi Shajakhan 		if (!sband)
40421a8e9ddSMohammed Shafi Shajakhan 			goto free_txq;
40521a8e9ddSMohammed Shafi Shajakhan 
40621a8e9ddSMohammed Shafi Shajakhan 		smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
407687da132SEmmanuel Grumbach 			IEEE80211_HT_CAP_SM_PS_SHIFT;
408687da132SEmmanuel Grumbach 		/*
409687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
410687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
411687da132SEmmanuel Grumbach 		 */
412687da132SEmmanuel Grumbach 		switch (smps) {
413687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
414687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
415687da132SEmmanuel Grumbach 			break;
416687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
417687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
418687da132SEmmanuel Grumbach 			break;
419687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
420687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
421687da132SEmmanuel Grumbach 			break;
422687da132SEmmanuel Grumbach 		default:
423687da132SEmmanuel Grumbach 			WARN_ON(1);
424687da132SEmmanuel Grumbach 		}
425687da132SEmmanuel Grumbach 	}
426af0ed69bSJohannes Berg 
4276e0456b5SFelix Fietkau 	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
4286e0456b5SFelix Fietkau 
429484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
430484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
431484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
432484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
433484a54c2SToke Høiland-Jørgensen 
434bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
435ef04a297SJohannes Berg 
436abfbc3afSJohannes Berg 	return sta;
437ba8c3d6fSFelix Fietkau 
438ba8c3d6fSFelix Fietkau free_txq:
439ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
440ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
441ba8c3d6fSFelix Fietkau free:
442433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
443433f5bc1SJohannes Berg 	kfree(sta->mesh);
444433f5bc1SJohannes Berg #endif
445ba8c3d6fSFelix Fietkau 	kfree(sta);
446ba8c3d6fSFelix Fietkau 	return NULL;
44773651ee6SJohannes Berg }
44873651ee6SJohannes Berg 
4498c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
45034e89507SJohannes Berg {
45134e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
45234e89507SJohannes Berg 
45303e4497eSJohannes Berg 	/*
45403e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
45503e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
45603e4497eSJohannes Berg 	 * and another CPU turns off the net device.
45703e4497eSJohannes Berg 	 */
4588c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4598c71df7aSGuy Eilam 		return -ENETDOWN;
46003e4497eSJohannes Berg 
461b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4628c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4638c71df7aSGuy Eilam 		return -EINVAL;
4648c71df7aSGuy Eilam 
46583e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
46683e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
46783e7e4ceSHerbert Xu 	 * for correctness.
46831104891SJohannes Berg 	 */
46931104891SJohannes Berg 	rcu_read_lock();
47031104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
47131104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
47231104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
47331104891SJohannes Berg 		rcu_read_unlock();
47431104891SJohannes Berg 		return -ENOTUNIQ;
47531104891SJohannes Berg 	}
47631104891SJohannes Berg 	rcu_read_unlock();
47731104891SJohannes Berg 
4788c71df7aSGuy Eilam 	return 0;
47993e5deb1SJohannes Berg }
48044213b5eSJohannes Berg 
481f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
482f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
483f09603a2SJohannes Berg 				     struct sta_info *sta)
484f09603a2SJohannes Berg {
485f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
486f09603a2SJohannes Berg 	int err = 0;
487f09603a2SJohannes Berg 
488f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
489f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
490f09603a2SJohannes Berg 		if (err)
491f09603a2SJohannes Berg 			break;
492f09603a2SJohannes Berg 	}
493f09603a2SJohannes Berg 
494f09603a2SJohannes Berg 	if (!err) {
495a4ec45a4SJohannes Berg 		/*
496a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
497a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
498a4ec45a4SJohannes Berg 		 */
499a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
500f09603a2SJohannes Berg 			sta->uploaded = true;
501f09603a2SJohannes Berg 		return 0;
502f09603a2SJohannes Berg 	}
503f09603a2SJohannes Berg 
504f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
505bdcbd8e0SJohannes Berg 		sdata_info(sdata,
506bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
507bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
508f09603a2SJohannes Berg 		err = 0;
509f09603a2SJohannes Berg 	}
510f09603a2SJohannes Berg 
511f09603a2SJohannes Berg 	/* unwind on error */
512f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
513f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
514f09603a2SJohannes Berg 
515f09603a2SJohannes Berg 	return err;
516f09603a2SJohannes Berg }
517f09603a2SJohannes Berg 
51834e89507SJohannes Berg /*
5198c71df7aSGuy Eilam  * should be called with sta_mtx locked
5208c71df7aSGuy Eilam  * this function replaces the mutex lock
5218c71df7aSGuy Eilam  * with a RCU lock
5228c71df7aSGuy Eilam  */
5234d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
5248c71df7aSGuy Eilam {
5258c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
5268c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
5270c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
5288c71df7aSGuy Eilam 	int err = 0;
5298c71df7aSGuy Eilam 
5308c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
5318c71df7aSGuy Eilam 
5327852e361SJohannes Berg 	/* check if STA exists already */
5337852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
5344d33960bSJohannes Berg 		err = -EEXIST;
5354d33960bSJohannes Berg 		goto out_err;
53634e89507SJohannes Berg 	}
53734e89507SJohannes Berg 
5380c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
5390c2e3842SKoen Vandeputte 	if (!sinfo) {
5400c2e3842SKoen Vandeputte 		err = -ENOMEM;
5410c2e3842SKoen Vandeputte 		goto out_err;
5420c2e3842SKoen Vandeputte 	}
5430c2e3842SKoen Vandeputte 
5444d33960bSJohannes Berg 	local->num_sta++;
5454d33960bSJohannes Berg 	local->sta_generation++;
5464d33960bSJohannes Berg 	smp_mb();
5474d33960bSJohannes Berg 
5485108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
5495108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5505108ca82SJohannes Berg 
5514d33960bSJohannes Berg 	/* make the station visible */
55262b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
55362b14b24SJohannes Berg 	if (err)
55462b14b24SJohannes Berg 		goto out_drop_sta;
5554d33960bSJohannes Berg 
5562bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
55783d5cc01SJohannes Berg 
5585108ca82SJohannes Berg 	/* notify driver */
5595108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
5605108ca82SJohannes Berg 	if (err)
5615108ca82SJohannes Berg 		goto out_remove;
5625108ca82SJohannes Berg 
56383d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
5645108ca82SJohannes Berg 	/* accept BA sessions now */
5655108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5664d33960bSJohannes Berg 
5674d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
5684d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
5694d33960bSJohannes Berg 
5700ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
5710ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
5720ef049dcSArnd Bergmann 	kfree(sinfo);
573d0709a65SJohannes Berg 
574bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
575f0706e82SJiri Benc 
57634e89507SJohannes Berg 	/* move reference to rcu-protected */
57734e89507SJohannes Berg 	rcu_read_lock();
57834e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
579e9f207f0SJiri Benc 
58073651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
58173651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
58273651ee6SJohannes Berg 
58373651ee6SJohannes Berg 	return 0;
5845108ca82SJohannes Berg  out_remove:
5855108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
5865108ca82SJohannes Berg 	list_del_rcu(&sta->list);
58762b14b24SJohannes Berg  out_drop_sta:
5885108ca82SJohannes Berg 	local->num_sta--;
5895108ca82SJohannes Berg 	synchronize_net();
5905108ca82SJohannes Berg 	__cleanup_single_sta(sta);
5914d33960bSJohannes Berg  out_err:
5924d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
593ea32f065SSudip Mukherjee 	kfree(sinfo);
5944d33960bSJohannes Berg 	rcu_read_lock();
5954d33960bSJohannes Berg 	return err;
5968c71df7aSGuy Eilam }
5978c71df7aSGuy Eilam 
5988c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5998c71df7aSGuy Eilam {
6008c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
601308f7fcfSZhao, Gang 	int err;
6028c71df7aSGuy Eilam 
6034d33960bSJohannes Berg 	might_sleep();
6044d33960bSJohannes Berg 
60531104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
60631104891SJohannes Berg 
6078c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
6088c71df7aSGuy Eilam 	if (err) {
60931104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
6108c71df7aSGuy Eilam 		rcu_read_lock();
6118c71df7aSGuy Eilam 		goto out_free;
6128c71df7aSGuy Eilam 	}
6138c71df7aSGuy Eilam 
6144d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
6158c71df7aSGuy Eilam 	if (err)
616004c872eSJohannes Berg 		goto out_free;
617d0709a65SJohannes Berg 
618004c872eSJohannes Berg 	return 0;
61993e5deb1SJohannes Berg  out_free:
620d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
62193e5deb1SJohannes Berg 	return err;
622f0706e82SJiri Benc }
623f0706e82SJiri Benc 
62434e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
62534e89507SJohannes Berg {
62634e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
62734e89507SJohannes Berg 
62834e89507SJohannes Berg 	rcu_read_unlock();
62934e89507SJohannes Berg 
63034e89507SJohannes Berg 	return err;
63134e89507SJohannes Berg }
63234e89507SJohannes Berg 
633d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
634004c872eSJohannes Berg {
635004c872eSJohannes Berg 	/*
636004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
637004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
638004c872eSJohannes Berg 	 */
639d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
640004c872eSJohannes Berg }
641004c872eSJohannes Berg 
642d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
643004c872eSJohannes Berg {
644004c872eSJohannes Berg 	/*
645004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
646004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
647004c872eSJohannes Berg 	 */
648d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
649004c872eSJohannes Berg }
650004c872eSJohannes Berg 
6513d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
6523d5839b6SIlan Peer {
6533d5839b6SIlan Peer 	/*
6543d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
6553d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
6563d5839b6SIlan Peer 	 */
6573d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
6583d5839b6SIlan Peer }
6593d5839b6SIlan Peer 
660948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
661004c872eSJohannes Berg {
662948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
663948d887dSJohannes Berg 	switch (ac) {
664948d887dSJohannes Berg 	case IEEE80211_AC_VO:
665948d887dSJohannes Berg 		return BIT(6) | BIT(7);
666948d887dSJohannes Berg 	case IEEE80211_AC_VI:
667948d887dSJohannes Berg 		return BIT(4) | BIT(5);
668948d887dSJohannes Berg 	case IEEE80211_AC_BE:
669948d887dSJohannes Berg 		return BIT(0) | BIT(3);
670948d887dSJohannes Berg 	case IEEE80211_AC_BK:
671948d887dSJohannes Berg 		return BIT(1) | BIT(2);
672948d887dSJohannes Berg 	default:
673948d887dSJohannes Berg 		WARN_ON(1);
674948d887dSJohannes Berg 		return 0;
675d0709a65SJohannes Berg 	}
676004c872eSJohannes Berg }
677004c872eSJohannes Berg 
6789b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
679004c872eSJohannes Berg {
680c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
681d012a605SMarco Porsch 	struct ps_data *ps;
682948d887dSJohannes Berg 	bool indicate_tim = false;
683948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
684948d887dSJohannes Berg 	int ac;
685a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
686004c872eSJohannes Berg 
687d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
688d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
689c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
690c868cb35SJohannes Berg 			return;
6913e122be0SJohannes Berg 
692d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
6933f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6943f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6953f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
6963f52b7e3SMarco Porsch #endif
697d012a605SMarco Porsch 	} else {
698d012a605SMarco Porsch 		return;
699d012a605SMarco Porsch 	}
700d012a605SMarco Porsch 
701c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
702d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
703c868cb35SJohannes Berg 		return;
704004c872eSJohannes Berg 
705c868cb35SJohannes Berg 	if (sta->dead)
706c868cb35SJohannes Berg 		goto done;
7073e122be0SJohannes Berg 
708948d887dSJohannes Berg 	/*
709948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
710948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
711948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
712948d887dSJohannes Berg 	 * non-enabled ones.
713948d887dSJohannes Berg 	 */
714948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
715948d887dSJohannes Berg 		ignore_for_tim = 0;
716948d887dSJohannes Berg 
7179b7a86f3SJohannes Berg 	if (ignore_pending)
7189b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
7199b7a86f3SJohannes Berg 
720948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
721948d887dSJohannes Berg 		unsigned long tids;
722948d887dSJohannes Berg 
723f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
724948d887dSJohannes Berg 			continue;
725948d887dSJohannes Berg 
726948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
727948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
728948d887dSJohannes Berg 		if (indicate_tim)
729948d887dSJohannes Berg 			break;
730948d887dSJohannes Berg 
731948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
732948d887dSJohannes Berg 
733948d887dSJohannes Berg 		indicate_tim |=
734948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
735ba8c3d6fSFelix Fietkau 		indicate_tim |=
736ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
737004c872eSJohannes Berg 	}
738004c872eSJohannes Berg 
739c868cb35SJohannes Berg  done:
74065f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
741004c872eSJohannes Berg 
7423d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
7433d5839b6SIlan Peer 		goto out_unlock;
7443d5839b6SIlan Peer 
745948d887dSJohannes Berg 	if (indicate_tim)
746d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
747c868cb35SJohannes Berg 	else
748d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
7493e122be0SJohannes Berg 
7509b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
751c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
752948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
753c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
754004c872eSJohannes Berg 	}
755004c872eSJohannes Berg 
7563d5839b6SIlan Peer out_unlock:
75765f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
758004c872eSJohannes Berg }
759004c872eSJohannes Berg 
7609b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
7619b7a86f3SJohannes Berg {
7629b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
7639b7a86f3SJohannes Berg }
7649b7a86f3SJohannes Berg 
765cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
766f0706e82SJiri Benc {
767e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
768f0706e82SJiri Benc 	int timeout;
769f0706e82SJiri Benc 
770f0706e82SJiri Benc 	if (!skb)
771cd0b8d89SJohannes Berg 		return false;
772f0706e82SJiri Benc 
773e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
774f0706e82SJiri Benc 
775f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
77657c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
77757c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
77857c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
779f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
780f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
781e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
782f0706e82SJiri Benc }
783f0706e82SJiri Benc 
784f0706e82SJiri Benc 
785948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
786948d887dSJohannes Berg 						struct sta_info *sta, int ac)
787f0706e82SJiri Benc {
788f0706e82SJiri Benc 	unsigned long flags;
789f0706e82SJiri Benc 	struct sk_buff *skb;
790f0706e82SJiri Benc 
79160750397SJohannes Berg 	/*
79260750397SJohannes Berg 	 * First check for frames that should expire on the filtered
79360750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
79460750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
79560750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
79660750397SJohannes Berg 	 * total_ps_buffered counter.
79760750397SJohannes Berg 	 */
798f0706e82SJiri Benc 	for (;;) {
799948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
800948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
80157c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
802948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
803836341a7SJohannes Berg 		else
804f0706e82SJiri Benc 			skb = NULL;
805948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
806f0706e82SJiri Benc 
80760750397SJohannes Berg 		/*
80860750397SJohannes Berg 		 * Frames are queued in order, so if this one
80960750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
81060750397SJohannes Berg 		 * we actually reached the end of the queue we
81160750397SJohannes Berg 		 * also need to stop, of course.
81260750397SJohannes Berg 		 */
81360750397SJohannes Berg 		if (!skb)
81460750397SJohannes Berg 			break;
815d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
81660750397SJohannes Berg 	}
81760750397SJohannes Berg 
81860750397SJohannes Berg 	/*
81960750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
82060750397SJohannes Berg 	 * only find something if the filtered queue was emptied
82160750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
82260750397SJohannes Berg 	 * buffered frames.
82360750397SJohannes Berg 	 */
824f0706e82SJiri Benc 	for (;;) {
825948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
826948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
827f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
828948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
829f0706e82SJiri Benc 		else
830f0706e82SJiri Benc 			skb = NULL;
831948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
832f0706e82SJiri Benc 
83360750397SJohannes Berg 		/*
83460750397SJohannes Berg 		 * frames are queued in order, so if this one
83560750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
83660750397SJohannes Berg 		 * the queue) we can stop testing
83760750397SJohannes Berg 		 */
838836341a7SJohannes Berg 		if (!skb)
839836341a7SJohannes Berg 			break;
840836341a7SJohannes Berg 
841f0706e82SJiri Benc 		local->total_ps_buffered--;
842bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
843bdcbd8e0SJohannes Berg 		       sta->sta.addr);
844d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
845f0706e82SJiri Benc 	}
8463393a608SJuuso Oikarinen 
84760750397SJohannes Berg 	/*
84860750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
84960750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
85060750397SJohannes Berg 	 * frames.
85160750397SJohannes Berg 	 */
85260750397SJohannes Berg 	sta_info_recalc_tim(sta);
85360750397SJohannes Berg 
85460750397SJohannes Berg 	/*
85560750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
85660750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
85760750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
85860750397SJohannes Berg 	 */
859948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
860948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
861948d887dSJohannes Berg }
862948d887dSJohannes Berg 
863948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
864948d887dSJohannes Berg 					     struct sta_info *sta)
865948d887dSJohannes Berg {
866948d887dSJohannes Berg 	bool have_buffered = false;
867948d887dSJohannes Berg 	int ac;
868948d887dSJohannes Berg 
8693f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
8703f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
8713f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
872948d887dSJohannes Berg 		return false;
873948d887dSJohannes Berg 
874948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
875948d887dSJohannes Berg 		have_buffered |=
876948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
877948d887dSJohannes Berg 
878948d887dSJohannes Berg 	return have_buffered;
879f0706e82SJiri Benc }
880f0706e82SJiri Benc 
881d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
88234e89507SJohannes Berg {
88334e89507SJohannes Berg 	struct ieee80211_local *local;
88434e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8856d10e46bSJohannes Berg 	int ret;
88634e89507SJohannes Berg 
88734e89507SJohannes Berg 	might_sleep();
88834e89507SJohannes Berg 
88934e89507SJohannes Berg 	if (!sta)
89034e89507SJohannes Berg 		return -ENOENT;
89134e89507SJohannes Berg 
89234e89507SJohannes Berg 	local = sta->local;
89334e89507SJohannes Berg 	sdata = sta->sdata;
89434e89507SJohannes Berg 
89583d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
89683d5cc01SJohannes Berg 
897098a6070SJohannes Berg 	/*
898098a6070SJohannes Berg 	 * Before removing the station from the driver and
899098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
900098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
901098a6070SJohannes Berg 	 * will be sufficient.
902098a6070SJohannes Berg 	 */
903c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
904c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
905098a6070SJohannes Berg 
906f59374ebSSara Sharon 	/*
907f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
908f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
909f59374ebSSara Sharon 	 * all such frames to be processed.
910f59374ebSSara Sharon 	 */
911f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
912f59374ebSSara Sharon 
91334e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
914b01711beSJohannes Berg 	if (WARN_ON(ret))
91534e89507SJohannes Berg 		return ret;
91634e89507SJohannes Berg 
917a7a6bdd0SArik Nemtsov 	/*
918a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
919a7a6bdd0SArik Nemtsov 	 * removal.
920a7a6bdd0SArik Nemtsov 	 */
921a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
922a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
923a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
924a7a6bdd0SArik Nemtsov 	}
925a7a6bdd0SArik Nemtsov 
926794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
927ef044763SEliad Peller 	sta->removed = true;
9284d33960bSJohannes Berg 
9296a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
9306a9d1b91SJohannes Berg 
931a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
932a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
933a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
934a710c816SJohannes Berg 
935d778207bSJohannes Berg 	return 0;
936d778207bSJohannes Berg }
937d778207bSJohannes Berg 
938d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
939d778207bSJohannes Berg {
940d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
941d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
9420ef049dcSArnd Bergmann 	struct station_info *sinfo;
943d778207bSJohannes Berg 	int ret;
944d778207bSJohannes Berg 
945d778207bSJohannes Berg 	/*
946d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
947d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
948d778207bSJohannes Berg 	 */
949d778207bSJohannes Berg 
950d778207bSJohannes Berg 	might_sleep();
951d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
952d778207bSJohannes Berg 
953c8782078SJohannes Berg 	/* now keys can no longer be reached */
9546d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
95534e89507SJohannes Berg 
9569b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
9579b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
9589b7a86f3SJohannes Berg 
95934e89507SJohannes Berg 	sta->dead = true;
96034e89507SJohannes Berg 
96134e89507SJohannes Berg 	local->num_sta--;
96234e89507SJohannes Berg 	local->sta_generation++;
96334e89507SJohannes Berg 
96483d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
965f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
966f09603a2SJohannes Berg 		if (ret) {
96783d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
96883d5cc01SJohannes Berg 			break;
96983d5cc01SJohannes Berg 		}
97083d5cc01SJohannes Berg 	}
971d9a7ddb0SJohannes Berg 
972f09603a2SJohannes Berg 	if (sta->uploaded) {
973f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
974f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
975f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
976f09603a2SJohannes Berg 	}
97734e89507SJohannes Berg 
978bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
979bdcbd8e0SJohannes Berg 
9800ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
9810ef049dcSArnd Bergmann 	if (sinfo)
9820ef049dcSArnd Bergmann 		sta_set_sinfo(sta, sinfo);
9830ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
9840ef049dcSArnd Bergmann 	kfree(sinfo);
985ec15e68bSJouni Malinen 
98634e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
98734e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
98834e89507SJohannes Berg 
989d34ba216SJohannes Berg 	cleanup_single_sta(sta);
990d778207bSJohannes Berg }
991d778207bSJohannes Berg 
992d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
993d778207bSJohannes Berg {
994d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
995d778207bSJohannes Berg 
996d778207bSJohannes Berg 	if (err)
997d778207bSJohannes Berg 		return err;
998d778207bSJohannes Berg 
999d778207bSJohannes Berg 	synchronize_net();
1000d778207bSJohannes Berg 
1001d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
100234e89507SJohannes Berg 
100334e89507SJohannes Berg 	return 0;
100434e89507SJohannes Berg }
100534e89507SJohannes Berg 
100634e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
100734e89507SJohannes Berg {
100834e89507SJohannes Berg 	struct sta_info *sta;
100934e89507SJohannes Berg 	int ret;
101034e89507SJohannes Berg 
101134e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
10127852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
101334e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
101434e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
101534e89507SJohannes Berg 
101634e89507SJohannes Berg 	return ret;
101734e89507SJohannes Berg }
101834e89507SJohannes Berg 
101934e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
102034e89507SJohannes Berg 			      const u8 *addr)
102134e89507SJohannes Berg {
102234e89507SJohannes Berg 	struct sta_info *sta;
102334e89507SJohannes Berg 	int ret;
102434e89507SJohannes Berg 
102534e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
10267852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
102734e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
102834e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
102934e89507SJohannes Berg 
103034e89507SJohannes Berg 	return ret;
103134e89507SJohannes Berg }
1032f0706e82SJiri Benc 
1033f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
1034f0706e82SJiri Benc {
1035f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
1036f0706e82SJiri Benc 	struct sta_info *sta;
10373393a608SJuuso Oikarinen 	bool timer_needed = false;
1038f0706e82SJiri Benc 
1039d0709a65SJohannes Berg 	rcu_read_lock();
1040d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
10413393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
10423393a608SJuuso Oikarinen 			timer_needed = true;
1043d0709a65SJohannes Berg 	rcu_read_unlock();
1044f0706e82SJiri Benc 
10455bb644a0SJohannes Berg 	if (local->quiescing)
10465bb644a0SJohannes Berg 		return;
10475bb644a0SJohannes Berg 
10483393a608SJuuso Oikarinen 	if (!timer_needed)
10493393a608SJuuso Oikarinen 		return;
10503393a608SJuuso Oikarinen 
105126d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
105226d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1053f0706e82SJiri Benc }
1054f0706e82SJiri Benc 
10557bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
10567bedd0cfSJohannes Berg {
10577bedd0cfSJohannes Berg 	int err;
10587bedd0cfSJohannes Berg 
105983e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
10607bedd0cfSJohannes Berg 	if (err)
10617bedd0cfSJohannes Berg 		return err;
10627bedd0cfSJohannes Berg 
10634d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
106434e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1065f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1066f0706e82SJiri Benc 
1067b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
1068b24b8a24SPavel Emelyanov 		    (unsigned long)local);
10697bedd0cfSJohannes Berg 	return 0;
1070f0706e82SJiri Benc }
1071f0706e82SJiri Benc 
1072f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1073f0706e82SJiri Benc {
1074a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
107583e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1076f0706e82SJiri Benc }
1077f0706e82SJiri Benc 
1078051007d9SJohannes Berg 
1079e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1080f0706e82SJiri Benc {
1081b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1082f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1083d778207bSJohannes Berg 	LIST_HEAD(free_list);
108444213b5eSJohannes Berg 	int ret = 0;
1085f0706e82SJiri Benc 
1086d0709a65SJohannes Berg 	might_sleep();
1087d0709a65SJohannes Berg 
1088e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1089e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1090e716251dSJohannes Berg 
109134e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
109234e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1093e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1094e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1095d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1096d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
109734316837SJohannes Berg 			ret++;
109834316837SJohannes Berg 		}
109934e89507SJohannes Berg 	}
1100d778207bSJohannes Berg 
1101d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1102d778207bSJohannes Berg 		synchronize_net();
1103d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1104d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1105d778207bSJohannes Berg 	}
110634e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
110744213b5eSJohannes Berg 
1108051007d9SJohannes Berg 	return ret;
1109051007d9SJohannes Berg }
1110051007d9SJohannes Berg 
111124723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
111224723d1bSJohannes Berg 			  unsigned long exp_time)
111324723d1bSJohannes Berg {
111424723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
111524723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
111624723d1bSJohannes Berg 
111734e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1118e46a2cf9SMohammed Shafi Shajakhan 
1119e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1120b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1121b8da6b6aSJohannes Berg 
1122ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1123ec2b774eSMarek Lindner 			continue;
1124ec2b774eSMarek Lindner 
1125b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1126eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1127bdcbd8e0SJohannes Berg 				sta->sta.addr);
11283f52b7e3SMarco Porsch 
11293f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
11303f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
11313f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
11323f52b7e3SMarco Porsch 
113334e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
113424723d1bSJohannes Berg 		}
1135e46a2cf9SMohammed Shafi Shajakhan 	}
1136e46a2cf9SMohammed Shafi Shajakhan 
113734e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
113824723d1bSJohannes Berg }
113917741cdcSJohannes Berg 
1140686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1141686b9cb9SBen Greear 						   const u8 *addr,
1142686b9cb9SBen Greear 						   const u8 *localaddr)
114317741cdcSJohannes Berg {
11447bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
114583e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
11467bedd0cfSJohannes Berg 	struct sta_info *sta;
114717741cdcSJohannes Berg 
1148686b9cb9SBen Greear 	/*
1149686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1150686b9cb9SBen Greear 	 * ... first in list.
1151686b9cb9SBen Greear 	 */
115283e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1153686b9cb9SBen Greear 		if (localaddr &&
1154b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1155686b9cb9SBen Greear 			continue;
1156f7c65594SJohannes Berg 		if (!sta->uploaded)
1157f7c65594SJohannes Berg 			return NULL;
115817741cdcSJohannes Berg 		return &sta->sta;
1159f7c65594SJohannes Berg 	}
1160f7c65594SJohannes Berg 
1161abe60632SJohannes Berg 	return NULL;
116217741cdcSJohannes Berg }
1163686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
11645ed176e1SJohannes Berg 
11655ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
11665ed176e1SJohannes Berg 					 const u8 *addr)
11675ed176e1SJohannes Berg {
1168f7c65594SJohannes Berg 	struct sta_info *sta;
11695ed176e1SJohannes Berg 
11705ed176e1SJohannes Berg 	if (!vif)
11715ed176e1SJohannes Berg 		return NULL;
11725ed176e1SJohannes Berg 
1173f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1174f7c65594SJohannes Berg 	if (!sta)
1175f7c65594SJohannes Berg 		return NULL;
11765ed176e1SJohannes Berg 
1177f7c65594SJohannes Berg 	if (!sta->uploaded)
1178f7c65594SJohannes Berg 		return NULL;
1179f7c65594SJohannes Berg 
1180f7c65594SJohannes Berg 	return &sta->sta;
11815ed176e1SJohannes Berg }
118217741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1183af818581SJohannes Berg 
1184e3685e03SJohannes Berg /* powersave support code */
1185e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
118650a9432dSJohannes Berg {
1187608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1188e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1189e3685e03SJohannes Berg 	struct sk_buff_head pending;
1190ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1191e3685e03SJohannes Berg 	unsigned long flags;
1192d012a605SMarco Porsch 	struct ps_data *ps;
1193d012a605SMarco Porsch 
11943918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
11953918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
11963918edb0SFelix Fietkau 				     u.ap);
11973918edb0SFelix Fietkau 
11983918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1199d012a605SMarco Porsch 		ps = &sdata->bss->ps;
12003f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
12013f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1202d012a605SMarco Porsch 	else
1203d012a605SMarco Porsch 		return;
120450a9432dSJohannes Berg 
1205c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
120647086fc5SJohannes Berg 
12075a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1208948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1209ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1210948d887dSJohannes Berg 
121130686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
121212375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1213af818581SJohannes Berg 
1214ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
1215ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1216bb42f2d1SToke Høiland-Jørgensen 			if (!txq_has_queue(sta->sta.txq[i]))
1217ba8c3d6fSFelix Fietkau 				continue;
1218ba8c3d6fSFelix Fietkau 
1219bb42f2d1SToke Høiland-Jørgensen 			drv_wake_tx_queue(local, to_txq_info(sta->sta.txq[i]));
1220ba8c3d6fSFelix Fietkau 		}
1221ba8c3d6fSFelix Fietkau 	}
1222ba8c3d6fSFelix Fietkau 
1223948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1224af818581SJohannes Berg 
12251d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
12261d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1227af818581SJohannes Berg 	/* Send all buffered frames to the station */
1228948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1229948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1230948d887dSJohannes Berg 
1231987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1232948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1233987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1234948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1235948d887dSJohannes Berg 		filtered += tmp - count;
1236948d887dSJohannes Berg 		count = tmp;
1237948d887dSJohannes Berg 
1238987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1239948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1240987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1241948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1242948d887dSJohannes Berg 		buffered += tmp - count;
1243948d887dSJohannes Berg 	}
1244948d887dSJohannes Berg 
1245e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
12465ac2e350SJohannes Berg 
12475ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
12485ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
12495ac2e350SJohannes Berg 
12505ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
12515ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
12525ac2e350SJohannes Berg 	 */
12535ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
12545ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
12551d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1256948d887dSJohannes Berg 
1257e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1258e3685e03SJohannes Berg 
1259687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1260062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1261062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1262687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1263687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1264687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1265687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1266687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1267687da132SEmmanuel Grumbach 		       sta->sta.addr);
1268687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1269687da132SEmmanuel Grumbach 					   sta->sta.addr,
1270687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1271687da132SEmmanuel Grumbach 	}
1272687da132SEmmanuel Grumbach 
1273af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1274af818581SJohannes Berg 
1275c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1276c868cb35SJohannes Berg 
1277bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
12782595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1279bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
128017c18bf8SJohannes Berg 
128117c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1282af818581SJohannes Berg }
1283af818581SJohannes Berg 
12840ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1285b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
12860ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1287ce662b44SJohannes Berg {
12880ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1289ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1290ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1291ce662b44SJohannes Berg 	struct sk_buff *skb;
1292ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1293ce662b44SJohannes Berg 	__le16 fc;
1294a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1295ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
129655de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1297ce662b44SJohannes Berg 
1298ce662b44SJohannes Berg 	if (qos) {
1299ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1300ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1301ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1302ce662b44SJohannes Berg 	} else {
1303ce662b44SJohannes Berg 		size -= 2;
1304ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1305ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1306ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1307ce662b44SJohannes Berg 	}
1308ce662b44SJohannes Berg 
1309ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1310ce662b44SJohannes Berg 	if (!skb)
1311ce662b44SJohannes Berg 		return;
1312ce662b44SJohannes Berg 
1313ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1314ce662b44SJohannes Berg 
1315*4df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1316ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1317ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1318ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1319ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1320ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1321864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1322ce662b44SJohannes Berg 
1323ce662b44SJohannes Berg 	skb->priority = tid;
1324ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
132559b66255SJohannes Berg 	if (qos) {
1326ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1327ce662b44SJohannes Berg 
13280ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1329ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1330ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
13310ead2510SEmmanuel Grumbach 			if (more_data)
13320ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
13330ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
13340ead2510SEmmanuel Grumbach 		}
1335ce662b44SJohannes Berg 	}
1336ce662b44SJohannes Berg 
1337ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1338ce662b44SJohannes Berg 
1339ce662b44SJohannes Berg 	/*
1340ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1341ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1342deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1343deeaee19SJohannes Berg 	 * ends the poll/service period.
1344ce662b44SJohannes Berg 	 */
134502f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1346deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1347ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1348ce662b44SJohannes Berg 
13496b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
13506b127c71SSujith Manoharan 
1351b77cf4f8SJohannes Berg 	if (call_driver)
1352b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1353b77cf4f8SJohannes Berg 					  reason, false);
135440b96408SJohannes Berg 
135589afe614SJohannes Berg 	skb->dev = sdata->dev;
135689afe614SJohannes Berg 
135755de908aSJohannes Berg 	rcu_read_lock();
135855de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
135955de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
136055de908aSJohannes Berg 		rcu_read_unlock();
136155de908aSJohannes Berg 		kfree_skb(skb);
136255de908aSJohannes Berg 		return;
136355de908aSJohannes Berg 	}
136455de908aSJohannes Berg 
136573c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
13667c10770fSJohannes Berg 	ieee80211_xmit(sdata, sta, skb);
136755de908aSJohannes Berg 	rcu_read_unlock();
1368ce662b44SJohannes Berg }
1369ce662b44SJohannes Berg 
13700a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
13710a1cb809SJohannes Berg {
13720a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
13730a1cb809SJohannes Berg 	if (tids & 0xF8)
13740a1cb809SJohannes Berg 		return fls(tids) - 1;
13750a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
13760a1cb809SJohannes Berg 	if (tids & BIT(0))
13770a1cb809SJohannes Berg 		return 0;
13780a1cb809SJohannes Berg 	return fls(tids) - 1;
13790a1cb809SJohannes Berg }
13800a1cb809SJohannes Berg 
13810ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
13820ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
13830ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
13840ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
13850ead2510SEmmanuel Grumbach  */
13860ead2510SEmmanuel Grumbach static bool
13870ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
13880ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
13890ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
13900ead2510SEmmanuel Grumbach {
13910ead2510SEmmanuel Grumbach 	int ac;
13920ead2510SEmmanuel Grumbach 
13930ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
13940ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
13950ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
13960ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
13970ead2510SEmmanuel Grumbach 	 */
13980ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
13990ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
14000ead2510SEmmanuel Grumbach 		return true;
14010ead2510SEmmanuel Grumbach 
14020ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1403f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
14040ead2510SEmmanuel Grumbach 			continue;
14050ead2510SEmmanuel Grumbach 
14060ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
14070ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
14080ead2510SEmmanuel Grumbach 			return true;
14090ead2510SEmmanuel Grumbach 	}
14100ead2510SEmmanuel Grumbach 
14110ead2510SEmmanuel Grumbach 	return false;
14120ead2510SEmmanuel Grumbach }
14130ead2510SEmmanuel Grumbach 
141447086fc5SJohannes Berg static void
14150ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
14160ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
14170ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
14180ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1419af818581SJohannes Berg {
1420af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1421af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1422948d887dSJohannes Berg 	int ac;
1423af818581SJohannes Berg 
1424f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1425948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
14264049e09aSJohannes Berg 		unsigned long tids;
14274049e09aSJohannes Berg 
1428f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1429948d887dSJohannes Berg 			continue;
1430948d887dSJohannes Berg 
14314049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
14324049e09aSJohannes Berg 
1433f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1434f9f760b4SJohannes Berg 		 * release from hardware queues
1435f9f760b4SJohannes Berg 		 */
14360ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
14370ead2510SEmmanuel Grumbach 			*driver_release_tids |=
14380ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
14390ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1440ba8c3d6fSFelix Fietkau 		}
1441f9f760b4SJohannes Berg 
14420ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
144347086fc5SJohannes Berg 			struct sk_buff *skb;
144447086fc5SJohannes Berg 
144547086fc5SJohannes Berg 			while (n_frames > 0) {
1446948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1447948d887dSJohannes Berg 				if (!skb) {
144847086fc5SJohannes Berg 					skb = skb_dequeue(
144947086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1450af818581SJohannes Berg 					if (skb)
1451af818581SJohannes Berg 						local->total_ps_buffered--;
1452af818581SJohannes Berg 				}
145347086fc5SJohannes Berg 				if (!skb)
145447086fc5SJohannes Berg 					break;
145547086fc5SJohannes Berg 				n_frames--;
14560ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
145747086fc5SJohannes Berg 			}
1458948d887dSJohannes Berg 		}
1459948d887dSJohannes Berg 
14600ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
14610ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
14620ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
14634049e09aSJohannes Berg 		 */
1464948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
14650ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1466948d887dSJohannes Berg 			break;
1467948d887dSJohannes Berg 	}
1468948d887dSJohannes Berg }
1469af818581SJohannes Berg 
14700ead2510SEmmanuel Grumbach static void
14710ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
14720ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
14730ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
14740ead2510SEmmanuel Grumbach {
14750ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
14760ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
14770ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
14780ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
14790ead2510SEmmanuel Grumbach 	bool more_data;
14800ead2510SEmmanuel Grumbach 
14810ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
14820ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
14830ead2510SEmmanuel Grumbach 
14840ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
14850ead2510SEmmanuel Grumbach 
14860ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
14870ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
14880ead2510SEmmanuel Grumbach 
14890ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
14900ead2510SEmmanuel Grumbach 
14911a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
14920ead2510SEmmanuel Grumbach 		driver_release_tids =
14930ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
14940ead2510SEmmanuel Grumbach 
1495f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1496f438ceb8SEmmanuel Grumbach 		int tid, ac;
14974049e09aSJohannes Berg 
1498ce662b44SJohannes Berg 		/*
1499ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1500ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1501ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1502ce662b44SJohannes Berg 		 *
1503ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1504ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1505ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1506ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1507ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1508ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1509ce662b44SJohannes Berg 		 *
1510ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1511ce662b44SJohannes Berg 		 */
1512ce662b44SJohannes Berg 
1513ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1514f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1515d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1516d7f84244SEmmanuel Grumbach 				break;
1517f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1518ce662b44SJohannes Berg 
15190ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1520f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
152147086fc5SJohannes Berg 		struct sk_buff_head pending;
152247086fc5SJohannes Berg 		struct sk_buff *skb;
152340b96408SJohannes Berg 		int num = 0;
152440b96408SJohannes Berg 		u16 tids = 0;
1525b77cf4f8SJohannes Berg 		bool need_null = false;
152647086fc5SJohannes Berg 
152747086fc5SJohannes Berg 		skb_queue_head_init(&pending);
152847086fc5SJohannes Berg 
152947086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1530af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
153147086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
153240b96408SJohannes Berg 			u8 *qoshdr = NULL;
153340b96408SJohannes Berg 
153440b96408SJohannes Berg 			num++;
1535af818581SJohannes Berg 
1536af818581SJohannes Berg 			/*
153747086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
153847086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
153947086fc5SJohannes Berg 			 * exchange.
1540af818581SJohannes Berg 			 */
15416b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
15426b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1543af818581SJohannes Berg 
154447086fc5SJohannes Berg 			/*
154547086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
154647086fc5SJohannes Berg 			 * more buffered frames for this STA
154747086fc5SJohannes Berg 			 */
154824b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
154947086fc5SJohannes Berg 				hdr->frame_control |=
155047086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
155124b9c373SJanusz.Dziedzic@tieto.com 			else
155224b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
155324b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1554af818581SJohannes Berg 
155540b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
155640b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
155740b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
155840b96408SJohannes Berg 
1559b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1560b77cf4f8SJohannes Berg 
1561b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1562b77cf4f8SJohannes Berg 
1563b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1564b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1565b77cf4f8SJohannes Berg 				continue;
1566b77cf4f8SJohannes Berg 
1567b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1568b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1569b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1570b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1571b77cf4f8SJohannes Berg 				break;
1572b77cf4f8SJohannes Berg 			}
1573b77cf4f8SJohannes Berg 
1574b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1575b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1576b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1577b77cf4f8SJohannes Berg 			 * and be done.
1578b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1579b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1580b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1581b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1582b77cf4f8SJohannes Berg 			 *
1583b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1584b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1585b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1586b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1587b77cf4f8SJohannes Berg 			 *
1588b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1589b77cf4f8SJohannes Berg 			 */
1590b77cf4f8SJohannes Berg 			if (qoshdr) {
159140b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1592deeaee19SJohannes Berg 
159347086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
159447086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1595b77cf4f8SJohannes Berg 			} else {
1596b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1597b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1598b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1599b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1600b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1601b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1602b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1603b77cf4f8SJohannes Berg 				 */
1604b77cf4f8SJohannes Berg 				hdr->frame_control |=
1605b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1606b77cf4f8SJohannes Berg 				need_null = true;
1607b77cf4f8SJohannes Berg 				num++;
160852a3f20cSMarco Porsch 			}
1609b77cf4f8SJohannes Berg 			break;
161047086fc5SJohannes Berg 		}
161147086fc5SJohannes Berg 
161240b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
161340b96408SJohannes Berg 					  reason, more_data);
161440b96408SJohannes Berg 
161547086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1616af818581SJohannes Berg 
1617b77cf4f8SJohannes Berg 		if (need_null)
1618b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
16190ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
16200ead2510SEmmanuel Grumbach 				reason, false, false);
1621b77cf4f8SJohannes Berg 
1622c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1623af818581SJohannes Berg 	} else {
1624ba8c3d6fSFelix Fietkau 		int tid;
1625ba8c3d6fSFelix Fietkau 
1626af818581SJohannes Berg 		/*
16274049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
16284049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1629f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1630f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1631f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1632f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1633f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1634f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1635af818581SJohannes Berg 		 */
16364049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
163747086fc5SJohannes Berg 					    n_frames, reason, more_data);
16384049e09aSJohannes Berg 
16394049e09aSJohannes Berg 		/*
16404049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
16414049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1642f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
16434049e09aSJohannes Berg 		 * release function.
1644f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1645ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1646ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
16474049e09aSJohannes Berg 		 */
1648ba8c3d6fSFelix Fietkau 
1649ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1650ba8c3d6fSFelix Fietkau 			return;
1651ba8c3d6fSFelix Fietkau 
1652ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
165383843c80SFelix Fietkau 			if (!(driver_release_tids & BIT(tid)) ||
16541e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1655ba8c3d6fSFelix Fietkau 				continue;
1656ba8c3d6fSFelix Fietkau 
1657ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1658ba8c3d6fSFelix Fietkau 			break;
1659ba8c3d6fSFelix Fietkau 		}
1660af818581SJohannes Berg 	}
1661af818581SJohannes Berg }
1662af818581SJohannes Berg 
1663af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1664af818581SJohannes Berg {
166547086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1666af818581SJohannes Berg 
1667af818581SJohannes Berg 	/*
166847086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
166947086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
167047086fc5SJohannes Berg 	 * only from the non-enabled ones.
1671af818581SJohannes Berg 	 */
167247086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
167347086fc5SJohannes Berg 		ignore_for_response = 0;
1674af818581SJohannes Berg 
167547086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
167647086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1677af818581SJohannes Berg }
167847086fc5SJohannes Berg 
167947086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
168047086fc5SJohannes Berg {
168147086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
168247086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
168347086fc5SJohannes Berg 
168447086fc5SJohannes Berg 	/*
168547086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
168647086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
168747086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
168847086fc5SJohannes Berg 	 * actually getting called.
168947086fc5SJohannes Berg 	 */
169047086fc5SJohannes Berg 	if (!delivery_enabled)
169147086fc5SJohannes Berg 		return;
169247086fc5SJohannes Berg 
169347086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
169447086fc5SJohannes Berg 	case 1:
169547086fc5SJohannes Berg 		n_frames = 2;
169647086fc5SJohannes Berg 		break;
169747086fc5SJohannes Berg 	case 2:
169847086fc5SJohannes Berg 		n_frames = 4;
169947086fc5SJohannes Berg 		break;
170047086fc5SJohannes Berg 	case 3:
170147086fc5SJohannes Berg 		n_frames = 6;
170247086fc5SJohannes Berg 		break;
170347086fc5SJohannes Berg 	case 0:
170447086fc5SJohannes Berg 		/* XXX: what is a good value? */
170513a8098aSAndrei Otcheretianski 		n_frames = 128;
170647086fc5SJohannes Berg 		break;
170747086fc5SJohannes Berg 	}
170847086fc5SJohannes Berg 
170947086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
171047086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1711af818581SJohannes Berg }
1712af818581SJohannes Berg 
1713af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1714af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1715af818581SJohannes Berg {
1716af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1717af818581SJohannes Berg 
1718b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1719b5878a2dSJohannes Berg 
17205ac2e350SJohannes Berg 	if (block) {
1721c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
172217c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
17235ac2e350SJohannes Berg 		return;
17245ac2e350SJohannes Berg 	}
17255ac2e350SJohannes Berg 
17265ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
17275ac2e350SJohannes Berg 		return;
17285ac2e350SJohannes Berg 
17295ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
17305ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
17315ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
17325ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
17335ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
17345ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
17355ac2e350SJohannes Berg 		/* must be asleep in this case */
17365ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
17375ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
17385ac2e350SJohannes Berg 	} else {
17395ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
174017c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
17415ac2e350SJohannes Berg 	}
1742af818581SJohannes Berg }
1743af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1744dcf55fb5SFelix Fietkau 
1745e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
174637fbd908SJohannes Berg {
174737fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
174837fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
174937fbd908SJohannes Berg 
175037fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
175137fbd908SJohannes Berg 
175237fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
175337fbd908SJohannes Berg }
1754e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
175537fbd908SJohannes Berg 
17560ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
17570ead2510SEmmanuel Grumbach {
17580ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
17590ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
17600ead2510SEmmanuel Grumbach 	bool more_data;
17610ead2510SEmmanuel Grumbach 
17620ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
17630ead2510SEmmanuel Grumbach 
17640ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
17650ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
17660ead2510SEmmanuel Grumbach 					       reason, 0);
17670ead2510SEmmanuel Grumbach 
17680ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
17690ead2510SEmmanuel Grumbach }
17700ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
17710ead2510SEmmanuel Grumbach 
1772042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1773042ec453SJohannes Berg 				u8 tid, bool buffered)
1774dcf55fb5SFelix Fietkau {
1775dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1776dcf55fb5SFelix Fietkau 
17775a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1778042ec453SJohannes Berg 		return;
1779042ec453SJohannes Berg 
17801b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
17811b000789SJohannes Berg 
1782948d887dSJohannes Berg 	if (buffered)
1783948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1784948d887dSJohannes Berg 	else
1785948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1786948d887dSJohannes Berg 
1787c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1788dcf55fb5SFelix Fietkau }
1789042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1790d9a7ddb0SJohannes Berg 
179152cfa1d6SAyala Beker static void
179252cfa1d6SAyala Beker ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
179352cfa1d6SAyala Beker {
179452cfa1d6SAyala Beker 	struct ieee80211_local *local = sdata->local;
179552cfa1d6SAyala Beker 	bool allow_p2p_go_ps = sdata->vif.p2p;
179652cfa1d6SAyala Beker 	struct sta_info *sta;
179752cfa1d6SAyala Beker 
179852cfa1d6SAyala Beker 	rcu_read_lock();
179952cfa1d6SAyala Beker 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
180052cfa1d6SAyala Beker 		if (sdata != sta->sdata ||
180152cfa1d6SAyala Beker 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
180252cfa1d6SAyala Beker 			continue;
180352cfa1d6SAyala Beker 		if (!sta->sta.support_p2p_ps) {
180452cfa1d6SAyala Beker 			allow_p2p_go_ps = false;
180552cfa1d6SAyala Beker 			break;
180652cfa1d6SAyala Beker 		}
180752cfa1d6SAyala Beker 	}
180852cfa1d6SAyala Beker 	rcu_read_unlock();
180952cfa1d6SAyala Beker 
181052cfa1d6SAyala Beker 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
181152cfa1d6SAyala Beker 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
181252cfa1d6SAyala Beker 		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
181352cfa1d6SAyala Beker 	}
181452cfa1d6SAyala Beker }
181552cfa1d6SAyala Beker 
181683d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1817d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1818d9a7ddb0SJohannes Berg {
18198bf11d8dSJohannes Berg 	might_sleep();
1820d9a7ddb0SJohannes Berg 
1821d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1822d9a7ddb0SJohannes Berg 		return 0;
1823d9a7ddb0SJohannes Berg 
1824f09603a2SJohannes Berg 	/* check allowed transitions first */
1825f09603a2SJohannes Berg 
1826d9a7ddb0SJohannes Berg 	switch (new_state) {
1827d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1828f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1829d9a7ddb0SJohannes Berg 			return -EINVAL;
1830d9a7ddb0SJohannes Berg 		break;
1831d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1832f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1833f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1834d9a7ddb0SJohannes Berg 			return -EINVAL;
1835d9a7ddb0SJohannes Berg 		break;
1836d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1837f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1838f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1839d9a7ddb0SJohannes Berg 			return -EINVAL;
1840d9a7ddb0SJohannes Berg 		break;
1841d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1842f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1843d9a7ddb0SJohannes Berg 			return -EINVAL;
1844d9a7ddb0SJohannes Berg 		break;
1845d9a7ddb0SJohannes Berg 	default:
1846d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1847d9a7ddb0SJohannes Berg 		return -EINVAL;
1848d9a7ddb0SJohannes Berg 	}
1849d9a7ddb0SJohannes Berg 
1850bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1851bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1852f09603a2SJohannes Berg 
1853f09603a2SJohannes Berg 	/*
1854f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1855f09603a2SJohannes Berg 	 * fail the transition
1856f09603a2SJohannes Berg 	 */
1857f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1858f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1859f09603a2SJohannes Berg 					sta->sta_state, new_state);
1860f09603a2SJohannes Berg 		if (err)
1861f09603a2SJohannes Berg 			return err;
1862f09603a2SJohannes Berg 	}
1863f09603a2SJohannes Berg 
1864f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1865f09603a2SJohannes Berg 
1866f09603a2SJohannes Berg 	switch (new_state) {
1867f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1868f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1869f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1870f09603a2SJohannes Berg 		break;
1871f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1872a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
1873f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1874a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1875f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1876a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
187752cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
187852cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1879a7201a6cSIlan Peer 		}
1880f09603a2SJohannes Berg 		break;
1881f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1882f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1883f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1884a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
188552cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
188652cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1887f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
188872f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
1889f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
189017c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
189149ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
1892f09603a2SJohannes Berg 		}
1893f09603a2SJohannes Berg 		break;
1894f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1895f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
189672f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
1897f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
189817c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
189949ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
1900f09603a2SJohannes Berg 		}
1901f09603a2SJohannes Berg 		break;
1902f09603a2SJohannes Berg 	default:
1903f09603a2SJohannes Berg 		break;
1904f09603a2SJohannes Berg 	}
1905f09603a2SJohannes Berg 
1906d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1907d9a7ddb0SJohannes Berg 
1908d9a7ddb0SJohannes Berg 	return 0;
1909d9a7ddb0SJohannes Berg }
1910687da132SEmmanuel Grumbach 
1911687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1912687da132SEmmanuel Grumbach {
1913687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1914687da132SEmmanuel Grumbach 	u8 rx_streams;
1915687da132SEmmanuel Grumbach 
1916687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1917687da132SEmmanuel Grumbach 		return 1;
1918687da132SEmmanuel Grumbach 
1919687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1920687da132SEmmanuel Grumbach 		int i;
1921687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1922687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1923687da132SEmmanuel Grumbach 
1924687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1925687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1926687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1927687da132SEmmanuel Grumbach 				return i + 1;
1928687da132SEmmanuel Grumbach 	}
1929687da132SEmmanuel Grumbach 
1930687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1931687da132SEmmanuel Grumbach 		rx_streams = 4;
1932687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1933687da132SEmmanuel Grumbach 		rx_streams = 3;
1934687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1935687da132SEmmanuel Grumbach 		rx_streams = 2;
1936687da132SEmmanuel Grumbach 	else
1937687da132SEmmanuel Grumbach 		rx_streams = 1;
1938687da132SEmmanuel Grumbach 
1939687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1940687da132SEmmanuel Grumbach 		return rx_streams;
1941687da132SEmmanuel Grumbach 
1942687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1943687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1944687da132SEmmanuel Grumbach }
1945b7ffbd7eSJohannes Berg 
1946c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
1947c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
1948c9c5962bSJohannes Berg {
1949c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
1950c9c5962bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1951c9c5962bSJohannes Berg 	int cpu;
1952c9c5962bSJohannes Berg 
1953c9c5962bSJohannes Berg 	if (!ieee80211_hw_check(&local->hw, USES_RSS))
1954c9c5962bSJohannes Berg 		return stats;
1955c9c5962bSJohannes Berg 
1956c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
1957c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
1958c9c5962bSJohannes Berg 
1959c9c5962bSJohannes Berg 		cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
1960c9c5962bSJohannes Berg 
1961c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
1962c9c5962bSJohannes Berg 			stats = cpustats;
1963c9c5962bSJohannes Berg 	}
1964c9c5962bSJohannes Berg 
1965c9c5962bSJohannes Berg 	return stats;
1966c9c5962bSJohannes Berg }
1967c9c5962bSJohannes Berg 
19684f6b1b3dSJohannes Berg static void sta_stats_decode_rate(struct ieee80211_local *local, u16 rate,
19694f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
1970fbd6ff5cSJohannes Berg {
1971dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
1972fbd6ff5cSJohannes Berg 
1973dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
19747f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
19754f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
1976dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
1977dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
1978dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
1979dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
19807f406cd1SJohannes Berg 		break;
19817f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
19824f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
1983dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
1984dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
1985dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
19867f406cd1SJohannes Berg 		break;
19877f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
1988fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
1989fbd6ff5cSJohannes Berg 		u16 brate;
19904f6b1b3dSJohannes Berg 		unsigned int shift;
1991dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
1992dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
1993fbd6ff5cSJohannes Berg 
1994a17d93ffSBen Greear 		rinfo->flags = 0;
1995dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
1996dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
19974f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
19984f6b1b3dSJohannes Berg 			shift = 2;
19994f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
20004f6b1b3dSJohannes Berg 			shift = 1;
20014f6b1b3dSJohannes Berg 		else
20024f6b1b3dSJohannes Berg 			shift = 0;
2003fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
20047f406cd1SJohannes Berg 		break;
20057f406cd1SJohannes Berg 		}
2006fbd6ff5cSJohannes Berg 	}
20074f6b1b3dSJohannes Berg }
2008fbd6ff5cSJohannes Berg 
2009a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
20104f6b1b3dSJohannes Berg {
2011c9c5962bSJohannes Berg 	u16 rate = ACCESS_ONCE(sta_get_last_rx_stats(sta)->last_rate);
20124f6b1b3dSJohannes Berg 
20134f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2014a17d93ffSBen Greear 		return -EINVAL;
2015a17d93ffSBen Greear 
20164f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2017a17d93ffSBen Greear 	return 0;
2018fbd6ff5cSJohannes Berg }
2019fbd6ff5cSJohannes Berg 
20200f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
20210f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
20220f9c5a61SJohannes Berg 			     int tid)
20230f9c5a61SJohannes Berg {
20240f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
20250f9c5a61SJohannes Berg 
20260f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
20270f9c5a61SJohannes Berg 		unsigned int start;
20280f9c5a61SJohannes Berg 
20290f9c5a61SJohannes Berg 		do {
20300f9c5a61SJohannes Berg 			start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
20310f9c5a61SJohannes Berg 			tidstats->rx_msdu = sta->rx_stats.msdu[tid];
20320f9c5a61SJohannes Berg 		} while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
20330f9c5a61SJohannes Berg 
20340f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
20350f9c5a61SJohannes Berg 	}
20360f9c5a61SJohannes Berg 
20370f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
20380f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
20390f9c5a61SJohannes Berg 		tidstats->tx_msdu = sta->tx_stats.msdu[tid];
20400f9c5a61SJohannes Berg 	}
20410f9c5a61SJohannes Berg 
20420f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
20430f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
20440f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
20450f9c5a61SJohannes Berg 		tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
20460f9c5a61SJohannes Berg 	}
20470f9c5a61SJohannes Berg 
20480f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
20490f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
20500f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
20510f9c5a61SJohannes Berg 		tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
20520f9c5a61SJohannes Berg 	}
20530f9c5a61SJohannes Berg }
20540f9c5a61SJohannes Berg 
2055c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2056c9c5962bSJohannes Berg {
2057c9c5962bSJohannes Berg 	unsigned int start;
2058c9c5962bSJohannes Berg 	u64 value;
2059c9c5962bSJohannes Berg 
2060c9c5962bSJohannes Berg 	do {
2061c9c5962bSJohannes Berg 		start = u64_stats_fetch_begin(&rxstats->syncp);
2062c9c5962bSJohannes Berg 		value = rxstats->bytes;
2063c9c5962bSJohannes Berg 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2064c9c5962bSJohannes Berg 
2065c9c5962bSJohannes Berg 	return value;
2066c9c5962bSJohannes Berg }
2067c9c5962bSJohannes Berg 
2068b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
2069b7ffbd7eSJohannes Berg {
2070b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2071b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2072b7ffbd7eSJohannes Berg 	u32 thr = 0;
2073c9c5962bSJohannes Berg 	int i, ac, cpu;
2074c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2075c9c5962bSJohannes Berg 
2076c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2077b7ffbd7eSJohannes Berg 
2078b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2079b7ffbd7eSJohannes Berg 
2080225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2081225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2082225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2083225b8189SJohannes Berg 	 */
2084225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2085225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2086225b8189SJohannes Berg 
20872b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
20882b9a7e1bSJohannes Berg 
2089319090bfSJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
2090319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_STA_FLAGS) |
2091319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BSS_PARAM) |
2092319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
2093976bd9efSJohannes Berg 			 BIT(NL80211_STA_INFO_RX_DROP_MISC);
2094976bd9efSJohannes Berg 
2095976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2096976bd9efSJohannes Berg 		sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2097976bd9efSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
2098976bd9efSJohannes Berg 	}
2099b7ffbd7eSJohannes Berg 
210084b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2101e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2102b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
21032b9a7e1bSJohannes Berg 
2104319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
2105319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_TX_BYTES)))) {
2106b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
21072b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2108e5a9f8d0SJohannes Berg 			sinfo->tx_bytes += sta->tx_stats.bytes[ac];
2109319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
2110b7ffbd7eSJohannes Berg 	}
21112b9a7e1bSJohannes Berg 
2112319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
21132b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
21142b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2115e5a9f8d0SJohannes Berg 			sinfo->tx_packets += sta->tx_stats.packets[ac];
2116319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
21172b9a7e1bSJohannes Berg 	}
21182b9a7e1bSJohannes Berg 
2119319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
2120319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_RX_BYTES)))) {
2121c9c5962bSJohannes Berg 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
21220f9c5a61SJohannes Berg 
2123c9c5962bSJohannes Berg 		if (sta->pcpu_rx_stats) {
2124c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2125c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2126c9c5962bSJohannes Berg 
2127c9c5962bSJohannes Berg 				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2128c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2129c9c5962bSJohannes Berg 			}
2130c9c5962bSJohannes Berg 		}
2131c9c5962bSJohannes Berg 
2132319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
21332b9a7e1bSJohannes Berg 	}
21342b9a7e1bSJohannes Berg 
2135319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
2136e5a9f8d0SJohannes Berg 		sinfo->rx_packets = sta->rx_stats.packets;
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_packets += cpurxs->packets;
2143c9c5962bSJohannes Berg 			}
2144c9c5962bSJohannes Berg 		}
2145319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
21462b9a7e1bSJohannes Berg 	}
21472b9a7e1bSJohannes Berg 
2148319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
2149e5a9f8d0SJohannes Berg 		sinfo->tx_retries = sta->status_stats.retry_count;
2150319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
21512b9a7e1bSJohannes Berg 	}
21522b9a7e1bSJohannes Berg 
2153319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
2154e5a9f8d0SJohannes Berg 		sinfo->tx_failed = sta->status_stats.retry_failed;
2155319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
21562b9a7e1bSJohannes Berg 	}
21572b9a7e1bSJohannes Berg 
2158e5a9f8d0SJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_stats.dropped;
2159c9c5962bSJohannes Berg 	if (sta->pcpu_rx_stats) {
2160c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2161c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2162c9c5962bSJohannes Berg 
2163c9c5962bSJohannes Berg 			cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2164e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2165c9c5962bSJohannes Berg 		}
2166c9c5962bSJohannes Berg 	}
2167b7ffbd7eSJohannes Berg 
2168225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2169225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2170225b8189SJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
2171225b8189SJohannes Berg 				 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2172225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2173225b8189SJohannes Berg 	}
2174225b8189SJohannes Berg 
217530686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
217630686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2177319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
2178c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2179319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
2180b7ffbd7eSJohannes Berg 		}
21812b9a7e1bSJohannes Berg 
2182c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats &&
2183c9c5962bSJohannes Berg 		    !(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
218440d9a38aSJohannes Berg 			sinfo->signal_avg =
21850be6ed13SJohannes Berg 				-ewma_signal_read(&sta->rx_stats_avg.signal);
2186319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
21872b9a7e1bSJohannes Berg 		}
21882b9a7e1bSJohannes Berg 	}
21892b9a7e1bSJohannes Berg 
2190c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2191c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2192c9c5962bSJohannes Berg 	 * pcpu statistics
2193c9c5962bSJohannes Berg 	 */
2194c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2195319090bfSJohannes Berg 	    !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2196319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2197c9c5962bSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL);
2198c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats)
2199c9c5962bSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2200b7ffbd7eSJohannes Berg 
2201c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2202c9c5962bSJohannes Berg 
2203b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2204e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2205c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2206b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
22070be6ed13SJohannes Berg 				-ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
2208b7ffbd7eSJohannes Berg 		}
2209b7ffbd7eSJohannes Berg 	}
2210b7ffbd7eSJohannes Berg 
2211319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
2212e5a9f8d0SJohannes Berg 		sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2213e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2214319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
22152b9a7e1bSJohannes Berg 	}
22162b9a7e1bSJohannes Berg 
2217319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
2218a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2219319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
22202b9a7e1bSJohannes Berg 	}
2221b7ffbd7eSJohannes Berg 
222279c892b8SJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
222379c892b8SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
222479c892b8SJohannes Berg 		struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
222579c892b8SJohannes Berg 
22260f9c5a61SJohannes Berg 		sta_set_tidstats(sta, tidstats, i);
222779c892b8SJohannes Berg 	}
222879c892b8SJohannes Berg 
2229b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2230b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2231319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2232319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLID) |
2233319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLINK_STATE) |
2234319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_LOCAL_PM) |
2235319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PEER_PM) |
2236319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_NONPEER_PM);
2237b7ffbd7eSJohannes Berg 
2238433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2239433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2240433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2241b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2242319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
2243433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2244b7ffbd7eSJohannes Berg 		}
2245433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2246433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2247433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2248b7ffbd7eSJohannes Berg #endif
2249b7ffbd7eSJohannes Berg 	}
2250b7ffbd7eSJohannes Berg 
2251b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2252b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2253b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2254b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2255b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2256b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2257b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2258785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2259b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2260b7ffbd7eSJohannes Berg 
2261b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2262b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2263b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2264b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2265b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2266b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2267b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2268b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2269b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2270b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2271b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2272b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2273a74a8c84SJohannes Berg 	if (sta->sta.wme)
2274b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2275b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2276b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2277b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2278b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2279b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2280b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2281b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2282b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2283b7ffbd7eSJohannes Berg 
22843b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
22853b17fbf8SMaxim Altshul 
22863b17fbf8SMaxim Altshul 	if (thr != 0) {
22873b17fbf8SMaxim Altshul 		sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
22883b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
22893b17fbf8SMaxim Altshul 	}
22903b17fbf8SMaxim Altshul }
22913b17fbf8SMaxim Altshul 
22923b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
22933b17fbf8SMaxim Altshul {
22943b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
22953b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
22963b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
22973b17fbf8SMaxim Altshul 	u32 thr = 0;
22983b17fbf8SMaxim Altshul 
22993b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
23003b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
23013b17fbf8SMaxim Altshul 
2302b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2303b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2304b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2305b7ffbd7eSJohannes Berg 	else
23064fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2307b7ffbd7eSJohannes Berg 
23083b17fbf8SMaxim Altshul 	return thr;
2309b7ffbd7eSJohannes Berg }
2310b8da6b6aSJohannes Berg 
2311b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2312b8da6b6aSJohannes Berg {
2313c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2314c9c5962bSJohannes Berg 
2315c9c5962bSJohannes Berg 	if (time_after(stats->last_rx, sta->status_stats.last_ack))
2316c9c5962bSJohannes Berg 		return stats->last_rx;
2317b8da6b6aSJohannes Berg 	return sta->status_stats.last_ack;
2318b8da6b6aSJohannes Berg }
2319484a54c2SToke Høiland-Jørgensen 
2320484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2321484a54c2SToke Høiland-Jørgensen {
2322484a54c2SToke Høiland-Jørgensen 	if (!sta->sdata->local->ops->wake_tx_queue)
2323484a54c2SToke Høiland-Jørgensen 		return;
2324484a54c2SToke Høiland-Jørgensen 
2325484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2326484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2327484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2328484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2329484a54c2SToke Høiland-Jørgensen 	} else {
2330484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2331484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2332484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2333484a54c2SToke Høiland-Jørgensen 	}
2334484a54c2SToke Høiland-Jørgensen }
2335484a54c2SToke Høiland-Jørgensen 
2336484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2337484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2338484a54c2SToke Høiland-Jørgensen {
2339484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2340484a54c2SToke Høiland-Jørgensen 
2341484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2342484a54c2SToke Høiland-Jørgensen }
2343