xref: /openbmc/linux/net/mac80211/sta_info.c (revision bd718fc11d5b184701e7fd8302033e31a3a03ba8)
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
6*bd718fc1SJohannes Berg  * Copyright (C) 2018-2019 Intel Corporation
7f0706e82SJiri Benc  *
8f0706e82SJiri Benc  * This program is free software; you can redistribute it and/or modify
9f0706e82SJiri Benc  * it under the terms of the GNU General Public License version 2 as
10f0706e82SJiri Benc  * published by the Free Software Foundation.
11f0706e82SJiri Benc  */
12f0706e82SJiri Benc 
13f0706e82SJiri Benc #include <linux/module.h>
14f0706e82SJiri Benc #include <linux/init.h>
15888d04dfSFelix Fietkau #include <linux/etherdevice.h>
16f0706e82SJiri Benc #include <linux/netdevice.h>
17f0706e82SJiri Benc #include <linux/types.h>
18f0706e82SJiri Benc #include <linux/slab.h>
19f0706e82SJiri Benc #include <linux/skbuff.h>
20f0706e82SJiri Benc #include <linux/if_arp.h>
210d174406SJohannes Berg #include <linux/timer.h>
22d0709a65SJohannes Berg #include <linux/rtnetlink.h>
23f0706e82SJiri Benc 
24484a54c2SToke Høiland-Jørgensen #include <net/codel.h>
25f0706e82SJiri Benc #include <net/mac80211.h>
26f0706e82SJiri Benc #include "ieee80211_i.h"
2724487981SJohannes Berg #include "driver-ops.h"
282c8dccc7SJohannes Berg #include "rate.h"
29f0706e82SJiri Benc #include "sta_info.h"
30e9f207f0SJiri Benc #include "debugfs_sta.h"
31ee385855SLuis Carlos Cobo #include "mesh.h"
32ce662b44SJohannes Berg #include "wme.h"
33f0706e82SJiri Benc 
34d0709a65SJohannes Berg /**
35d0709a65SJohannes Berg  * DOC: STA information lifetime rules
36d0709a65SJohannes Berg  *
37d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
38d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
39d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
40d0709a65SJohannes Berg  *
4134e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
4234e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4334e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4434e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4534e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4634e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4734e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4834e89507SJohannes Berg  * encryption keys.
4993e5deb1SJohannes Berg  *
5093e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
5193e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
52d0709a65SJohannes Berg  *
5334e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
547e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
557e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5625985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5734e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5825985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5934e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
607e189a12SLuis R. Rodriguez  *
6134e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
6234e89507SJohannes Berg  * calls are available.
63d0709a65SJohannes Berg  *
6434e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6534e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6634e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6734e89507SJohannes Berg  * freed before they are done using it.
68d0709a65SJohannes Berg  */
69f0706e82SJiri Benc 
707bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
717bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
72caf22d31SJohannes Berg 	.automatic_shrinking = true,
737bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
74ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
757bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
76ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
777bedd0cfSJohannes Berg };
787bedd0cfSJohannes Berg 
794d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
80be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
81f0706e82SJiri Benc 			     struct sta_info *sta)
82f0706e82SJiri Benc {
8383e7e4ceSHerbert Xu 	return rhltable_remove(&local->sta_hash, &sta->hash_node,
847bedd0cfSJohannes Berg 			       sta_rht_params);
85f0706e82SJiri Benc }
86f0706e82SJiri Benc 
875108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
88b22cfcfcSEliad Peller {
89b22cfcfcSEliad Peller 	int ac, i;
90b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
91b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
92b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
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++) {
115adf8ed01SJohannes Berg 			struct txq_info *txqi;
116adf8ed01SJohannes Berg 
117adf8ed01SJohannes Berg 			if (!sta->sta.txq[i])
118adf8ed01SJohannes Berg 				continue;
119adf8ed01SJohannes Berg 
120adf8ed01SJohannes Berg 			txqi = to_txq_info(sta->sta.txq[i]);
121ba8c3d6fSFelix Fietkau 
122fa962b92SMichal Kazior 			ieee80211_txq_purge(local, txqi);
123ba8c3d6fSFelix Fietkau 		}
124ba8c3d6fSFelix Fietkau 	}
125ba8c3d6fSFelix Fietkau 
126b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
127b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1281f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1291f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
130b22cfcfcSEliad Peller 	}
131b22cfcfcSEliad Peller 
13245b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
13345b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
134b22cfcfcSEliad Peller 
1355ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
136b22cfcfcSEliad Peller 
137b22cfcfcSEliad Peller 	/*
138b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
139b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
140b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
141b22cfcfcSEliad Peller 	 * directly by station destruction.
142b22cfcfcSEliad Peller 	 */
1435a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
144661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
145b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
146b22cfcfcSEliad Peller 		if (!tid_tx)
147b22cfcfcSEliad Peller 			continue;
1481f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
149b22cfcfcSEliad Peller 		kfree(tid_tx);
150b22cfcfcSEliad Peller 	}
1515108ca82SJohannes Berg }
152b22cfcfcSEliad Peller 
1535108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1545108ca82SJohannes Berg {
1555108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1565108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1575108ca82SJohannes Berg 
1585108ca82SJohannes Berg 	__cleanup_single_sta(sta);
159b22cfcfcSEliad Peller 	sta_info_free(local, sta);
160b22cfcfcSEliad Peller }
161b22cfcfcSEliad Peller 
16283e7e4ceSHerbert Xu struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
16383e7e4ceSHerbert Xu 					 const u8 *addr)
16483e7e4ceSHerbert Xu {
16583e7e4ceSHerbert Xu 	return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
16683e7e4ceSHerbert Xu }
16783e7e4ceSHerbert Xu 
168d0709a65SJohannes Berg /* protected by RCU */
169abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
170abe60632SJohannes Berg 			      const u8 *addr)
17143ba7e95SJohannes Berg {
172abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
17383e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
17460f4b626SJohannes Berg 	struct sta_info *sta;
17543ba7e95SJohannes Berg 
17660f4b626SJohannes Berg 	rcu_read_lock();
17783e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
17860f4b626SJohannes Berg 		if (sta->sdata == sdata) {
17960f4b626SJohannes Berg 			rcu_read_unlock();
18060f4b626SJohannes Berg 			/* this is safe as the caller must already hold
18160f4b626SJohannes Berg 			 * another rcu read section or the mutex
18260f4b626SJohannes Berg 			 */
18360f4b626SJohannes Berg 			return sta;
18460f4b626SJohannes Berg 		}
18560f4b626SJohannes Berg 	}
18660f4b626SJohannes Berg 	rcu_read_unlock();
18760f4b626SJohannes Berg 	return NULL;
18843ba7e95SJohannes Berg }
18943ba7e95SJohannes Berg 
1900e5ded5aSFelix Fietkau /*
1910e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1920e5ded5aSFelix Fietkau  * or from one of its vlans
1930e5ded5aSFelix Fietkau  */
1940e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1950e5ded5aSFelix Fietkau 				  const u8 *addr)
1960e5ded5aSFelix Fietkau {
1970e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
19883e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
1990e5ded5aSFelix Fietkau 	struct sta_info *sta;
2000e5ded5aSFelix Fietkau 
2017bedd0cfSJohannes Berg 	rcu_read_lock();
20283e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
2037bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
2047bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
2057bedd0cfSJohannes Berg 			rcu_read_unlock();
2067bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
2077bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
2087bedd0cfSJohannes Berg 			 */
2090e5ded5aSFelix Fietkau 			return sta;
2100e5ded5aSFelix Fietkau 		}
2117bedd0cfSJohannes Berg 	}
2127bedd0cfSJohannes Berg 	rcu_read_unlock();
2137bedd0cfSJohannes Berg 	return NULL;
2147bedd0cfSJohannes Berg }
2150e5ded5aSFelix Fietkau 
2163b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2173b53fde8SJohannes Berg 				     int idx)
218ee385855SLuis Carlos Cobo {
2193b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
220ee385855SLuis Carlos Cobo 	struct sta_info *sta;
221ee385855SLuis Carlos Cobo 	int i = 0;
222ee385855SLuis Carlos Cobo 
223d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
2243b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2252a8ca29aSLuis Carlos Cobo 			continue;
226ee385855SLuis Carlos Cobo 		if (i < idx) {
227ee385855SLuis Carlos Cobo 			++i;
228ee385855SLuis Carlos Cobo 			continue;
229ee385855SLuis Carlos Cobo 		}
2302a8ca29aSLuis Carlos Cobo 		return sta;
231ee385855SLuis Carlos Cobo 	}
232ee385855SLuis Carlos Cobo 
233ee385855SLuis Carlos Cobo 	return NULL;
234ee385855SLuis Carlos Cobo }
235f0706e82SJiri Benc 
23693e5deb1SJohannes Berg /**
237d9a7ddb0SJohannes Berg  * sta_info_free - free STA
23893e5deb1SJohannes Berg  *
2396ef307bcSRandy Dunlap  * @local: pointer to the global information
24093e5deb1SJohannes Berg  * @sta: STA info to free
24193e5deb1SJohannes Berg  *
24293e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
243d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
244d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
245d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
24693e5deb1SJohannes Berg  */
247d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
24893e5deb1SJohannes Berg {
249889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2504b7679a5SJohannes Berg 		rate_control_free_sta(sta);
25193e5deb1SJohannes Berg 
252bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
25393e5deb1SJohannes Berg 
254ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
255ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
25653d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
257433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
258433f5bc1SJohannes Berg 	kfree(sta->mesh);
259433f5bc1SJohannes Berg #endif
260c9c5962bSJohannes Berg 	free_percpu(sta->pcpu_rx_stats);
26193e5deb1SJohannes Berg 	kfree(sta);
26293e5deb1SJohannes Berg }
26393e5deb1SJohannes Berg 
2644d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
26562b14b24SJohannes Berg static int sta_info_hash_add(struct ieee80211_local *local,
266d0709a65SJohannes Berg 			     struct sta_info *sta)
267f0706e82SJiri Benc {
26883e7e4ceSHerbert Xu 	return rhltable_insert(&local->sta_hash, &sta->hash_node,
2697bedd0cfSJohannes Berg 			       sta_rht_params);
270f0706e82SJiri Benc }
271f0706e82SJiri Benc 
2725ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
273af818581SJohannes Berg {
274af818581SJohannes Berg 	struct sta_info *sta;
275af818581SJohannes Berg 
2765ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
277af818581SJohannes Berg 
278af818581SJohannes Berg 	if (sta->dead)
279af818581SJohannes Berg 		return;
280af818581SJohannes Berg 
28154420473SHelmut Schaa 	local_bh_disable();
2825ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
283af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2845ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
285af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2865ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
28747086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
288ce662b44SJohannes Berg 	local_bh_enable();
289af818581SJohannes Berg }
290af818581SJohannes Berg 
291af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
292af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
293af65cd96SJohannes Berg {
29430686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
295af65cd96SJohannes Berg 		return 0;
296af65cd96SJohannes Berg 
297889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
298af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
29935c347acSJohannes Berg 						     sta, gfp);
300889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
301af65cd96SJohannes Berg 		return -ENOMEM;
302af65cd96SJohannes Berg 
303af65cd96SJohannes Berg 	return 0;
304af65cd96SJohannes Berg }
305af65cd96SJohannes Berg 
30673651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
30756544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
308f0706e82SJiri Benc {
309d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
310ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
311f0706e82SJiri Benc 	struct sta_info *sta;
31216c5f15cSRon Rindjunsky 	int i;
313f0706e82SJiri Benc 
314ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
315f0706e82SJiri Benc 	if (!sta)
31673651ee6SJohannes Berg 		return NULL;
317f0706e82SJiri Benc 
318c9c5962bSJohannes Berg 	if (ieee80211_hw_check(hw, USES_RSS)) {
319c9c5962bSJohannes Berg 		sta->pcpu_rx_stats =
32095f3ce6aSSara Sharon 			alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
321c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats)
322c9c5962bSJohannes Berg 			goto free;
323c9c5962bSJohannes Berg 	}
324c9c5962bSJohannes Berg 
32507346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3261d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
3275ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
32867c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
329a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
33087f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
331433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
332433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
333433f5bc1SJohannes Berg 		if (!sta->mesh)
334433f5bc1SJohannes Berg 			goto free;
3354c02d62fSKees Cook 		sta->mesh->plink_sta = sta;
336433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
33787f59c70SThomas Pedersen 		if (ieee80211_vif_is_mesh(&sdata->vif) &&
33887f59c70SThomas Pedersen 		    !sdata->u.mesh.user_mpm)
3394c02d62fSKees Cook 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
3404c02d62fSKees Cook 				    0);
341433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
342433f5bc1SJohannes Berg 	}
34387f59c70SThomas Pedersen #endif
34407346f81SJohannes Berg 
345ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
34617741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
347480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
348480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
349480dd46bSMaxim Altshul 
35096fc6efbSAlexander Wetzel 	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
35196fc6efbSAlexander Wetzel 	 * The Tx path starts to use a key as soon as the key slot ptk_idx
35296fc6efbSAlexander Wetzel 	 * references to is not NULL. To not use the initial Rx-only key
35396fc6efbSAlexander Wetzel 	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
35496fc6efbSAlexander Wetzel 	 * which always will refer to a NULL key.
35596fc6efbSAlexander Wetzel 	 */
35696fc6efbSAlexander Wetzel 	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
35796fc6efbSAlexander Wetzel 	sta->ptk_idx = INVALID_PTK_KEYIDX;
35896fc6efbSAlexander Wetzel 
359d0709a65SJohannes Berg 	sta->local = local;
360d0709a65SJohannes Berg 	sta->sdata = sdata;
361e5a9f8d0SJohannes Berg 	sta->rx_stats.last_rx = jiffies;
362f0706e82SJiri Benc 
3630f9c5a61SJohannes Berg 	u64_stats_init(&sta->rx_stats.syncp);
3640f9c5a61SJohannes Berg 
36571ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
36671ec375cSJohannes Berg 
367b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
368b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
369b6da911bSLiad Kaufman 
37084b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
3710be6ed13SJohannes Berg 	ewma_signal_init(&sta->rx_stats_avg.signal);
372cc60dbbfSBalaji Pothunoori 	ewma_avg_signal_init(&sta->status_stats.avg_ack_signal);
3730be6ed13SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
3740be6ed13SJohannes Berg 		ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
375541a45a1SBruno Randolf 
376ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
377ba8c3d6fSFelix Fietkau 		void *txq_data;
378ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
379ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
380ba8c3d6fSFelix Fietkau 
381ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
382ba8c3d6fSFelix Fietkau 		if (!txq_data)
383ba8c3d6fSFelix Fietkau 			goto free;
384ba8c3d6fSFelix Fietkau 
385ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
386ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
387ba8c3d6fSFelix Fietkau 
388adf8ed01SJohannes Berg 			/* might not do anything for the bufferable MMPDU TXQ */
389fa962b92SMichal Kazior 			ieee80211_txq_init(sdata, sta, txq, i);
390abfbc3afSJohannes Berg 		}
391ba8c3d6fSFelix Fietkau 	}
392ba8c3d6fSFelix Fietkau 
393ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
394ba8c3d6fSFelix Fietkau 		goto free_txq;
395f0706e82SJiri Benc 
396b4809e94SToke Høiland-Jørgensen 	sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
397b4809e94SToke Høiland-Jørgensen 
398948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
399948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
400948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
401b4809e94SToke Høiland-Jørgensen 		sta->airtime[i].deficit = sta->airtime_weight;
402948d887dSJohannes Berg 	}
40373651ee6SJohannes Berg 
4045a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
4054be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
406cccaec98SSenthil Balasubramanian 
407*bd718fc1SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
408*bd718fc1SJohannes Berg 		u32 mandatory = 0;
409*bd718fc1SJohannes Berg 		int r;
410*bd718fc1SJohannes Berg 
411*bd718fc1SJohannes Berg 		if (!hw->wiphy->bands[i])
412*bd718fc1SJohannes Berg 			continue;
413*bd718fc1SJohannes Berg 
414*bd718fc1SJohannes Berg 		switch (i) {
415*bd718fc1SJohannes Berg 		case NL80211_BAND_2GHZ:
416*bd718fc1SJohannes Berg 			/*
417*bd718fc1SJohannes Berg 			 * We use both here, even if we cannot really know for
418*bd718fc1SJohannes Berg 			 * sure the station will support both, but the only use
419*bd718fc1SJohannes Berg 			 * for this is when we don't know anything yet and send
420*bd718fc1SJohannes Berg 			 * management frames, and then we'll pick the lowest
421*bd718fc1SJohannes Berg 			 * possible rate anyway.
422*bd718fc1SJohannes Berg 			 * If we don't include _G here, we cannot find a rate
423*bd718fc1SJohannes Berg 			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
424*bd718fc1SJohannes Berg 			 */
425*bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_B |
426*bd718fc1SJohannes Berg 				    IEEE80211_RATE_MANDATORY_G;
427*bd718fc1SJohannes Berg 			break;
428*bd718fc1SJohannes Berg 		case NL80211_BAND_5GHZ:
429*bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_A;
430*bd718fc1SJohannes Berg 			break;
431*bd718fc1SJohannes Berg 		case NL80211_BAND_60GHZ:
432*bd718fc1SJohannes Berg 			WARN_ON(1);
433*bd718fc1SJohannes Berg 			mandatory = 0;
434*bd718fc1SJohannes Berg 			break;
435*bd718fc1SJohannes Berg 		}
436*bd718fc1SJohannes Berg 
437*bd718fc1SJohannes Berg 		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
438*bd718fc1SJohannes Berg 			struct ieee80211_rate *rate;
439*bd718fc1SJohannes Berg 
440*bd718fc1SJohannes Berg 			rate = &hw->wiphy->bands[i]->bitrates[r];
441*bd718fc1SJohannes Berg 
442*bd718fc1SJohannes Berg 			if (!(rate->flags & mandatory))
443*bd718fc1SJohannes Berg 				continue;
444*bd718fc1SJohannes Berg 			sta->sta.supp_rates[i] |= BIT(r);
445*bd718fc1SJohannes Berg 		}
446*bd718fc1SJohannes Berg 	}
447*bd718fc1SJohannes Berg 
448af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
449687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
450687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
45121a8e9ddSMohammed Shafi Shajakhan 		struct ieee80211_supported_band *sband;
45221a8e9ddSMohammed Shafi Shajakhan 		u8 smps;
45321a8e9ddSMohammed Shafi Shajakhan 
45421a8e9ddSMohammed Shafi Shajakhan 		sband = ieee80211_get_sband(sdata);
45521a8e9ddSMohammed Shafi Shajakhan 		if (!sband)
45621a8e9ddSMohammed Shafi Shajakhan 			goto free_txq;
45721a8e9ddSMohammed Shafi Shajakhan 
45821a8e9ddSMohammed Shafi Shajakhan 		smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
459687da132SEmmanuel Grumbach 			IEEE80211_HT_CAP_SM_PS_SHIFT;
460687da132SEmmanuel Grumbach 		/*
461687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
462687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
463687da132SEmmanuel Grumbach 		 */
464687da132SEmmanuel Grumbach 		switch (smps) {
465687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
466687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
467687da132SEmmanuel Grumbach 			break;
468687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
469687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
470687da132SEmmanuel Grumbach 			break;
471687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
472687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
473687da132SEmmanuel Grumbach 			break;
474687da132SEmmanuel Grumbach 		default:
475687da132SEmmanuel Grumbach 			WARN_ON(1);
476687da132SEmmanuel Grumbach 		}
477687da132SEmmanuel Grumbach 	}
478af0ed69bSJohannes Berg 
4796e0456b5SFelix Fietkau 	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
4806e0456b5SFelix Fietkau 
481484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
482484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
483484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
484484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
485484a54c2SToke Høiland-Jørgensen 
486bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
487ef04a297SJohannes Berg 
488abfbc3afSJohannes Berg 	return sta;
489ba8c3d6fSFelix Fietkau 
490ba8c3d6fSFelix Fietkau free_txq:
491ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
492ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
493ba8c3d6fSFelix Fietkau free:
494d78d9ee9SSara Sharon 	free_percpu(sta->pcpu_rx_stats);
495433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
496433f5bc1SJohannes Berg 	kfree(sta->mesh);
497433f5bc1SJohannes Berg #endif
498ba8c3d6fSFelix Fietkau 	kfree(sta);
499ba8c3d6fSFelix Fietkau 	return NULL;
50073651ee6SJohannes Berg }
50173651ee6SJohannes Berg 
5028c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
50334e89507SJohannes Berg {
50434e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
50534e89507SJohannes Berg 
50603e4497eSJohannes Berg 	/*
50703e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
50803e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
50903e4497eSJohannes Berg 	 * and another CPU turns off the net device.
51003e4497eSJohannes Berg 	 */
5118c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
5128c71df7aSGuy Eilam 		return -ENETDOWN;
51303e4497eSJohannes Berg 
514b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
5158c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
5168c71df7aSGuy Eilam 		return -EINVAL;
5178c71df7aSGuy Eilam 
51883e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
51983e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
52083e7e4ceSHerbert Xu 	 * for correctness.
52131104891SJohannes Berg 	 */
52231104891SJohannes Berg 	rcu_read_lock();
52331104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
52431104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
52531104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
52631104891SJohannes Berg 		rcu_read_unlock();
52731104891SJohannes Berg 		return -ENOTUNIQ;
52831104891SJohannes Berg 	}
52931104891SJohannes Berg 	rcu_read_unlock();
53031104891SJohannes Berg 
5318c71df7aSGuy Eilam 	return 0;
53293e5deb1SJohannes Berg }
53344213b5eSJohannes Berg 
534f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
535f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
536f09603a2SJohannes Berg 				     struct sta_info *sta)
537f09603a2SJohannes Berg {
538f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
539f09603a2SJohannes Berg 	int err = 0;
540f09603a2SJohannes Berg 
541f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
542f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
543f09603a2SJohannes Berg 		if (err)
544f09603a2SJohannes Berg 			break;
545f09603a2SJohannes Berg 	}
546f09603a2SJohannes Berg 
547f09603a2SJohannes Berg 	if (!err) {
548a4ec45a4SJohannes Berg 		/*
549a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
550a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
551a4ec45a4SJohannes Berg 		 */
552a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
553f09603a2SJohannes Berg 			sta->uploaded = true;
554f09603a2SJohannes Berg 		return 0;
555f09603a2SJohannes Berg 	}
556f09603a2SJohannes Berg 
557f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
558bdcbd8e0SJohannes Berg 		sdata_info(sdata,
559bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
560bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
561f09603a2SJohannes Berg 		err = 0;
562f09603a2SJohannes Berg 	}
563f09603a2SJohannes Berg 
564f09603a2SJohannes Berg 	/* unwind on error */
565f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
566f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
567f09603a2SJohannes Berg 
568f09603a2SJohannes Berg 	return err;
569f09603a2SJohannes Berg }
570f09603a2SJohannes Berg 
571d405fd8cSGregory Greenman static void
572d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
573d405fd8cSGregory Greenman {
574d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
575d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
576d405fd8cSGregory Greenman 	struct sta_info *sta;
577d405fd8cSGregory Greenman 
578d405fd8cSGregory Greenman 	rcu_read_lock();
579d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
580d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
581d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
582d405fd8cSGregory Greenman 			continue;
583d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
584d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
585d405fd8cSGregory Greenman 			break;
586d405fd8cSGregory Greenman 		}
587d405fd8cSGregory Greenman 	}
588d405fd8cSGregory Greenman 	rcu_read_unlock();
589d405fd8cSGregory Greenman 
590d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
591d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
592d405fd8cSGregory Greenman 		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
593d405fd8cSGregory Greenman 	}
594d405fd8cSGregory Greenman }
595d405fd8cSGregory Greenman 
59634e89507SJohannes Berg /*
5978c71df7aSGuy Eilam  * should be called with sta_mtx locked
5988c71df7aSGuy Eilam  * this function replaces the mutex lock
5998c71df7aSGuy Eilam  * with a RCU lock
6008c71df7aSGuy Eilam  */
6014d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
6028c71df7aSGuy Eilam {
6038c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
6048c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
6050c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
6068c71df7aSGuy Eilam 	int err = 0;
6078c71df7aSGuy Eilam 
6088c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
6098c71df7aSGuy Eilam 
6107852e361SJohannes Berg 	/* check if STA exists already */
6117852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
6124d33960bSJohannes Berg 		err = -EEXIST;
6134d33960bSJohannes Berg 		goto out_err;
61434e89507SJohannes Berg 	}
61534e89507SJohannes Berg 
6160c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
6170c2e3842SKoen Vandeputte 	if (!sinfo) {
6180c2e3842SKoen Vandeputte 		err = -ENOMEM;
6190c2e3842SKoen Vandeputte 		goto out_err;
6200c2e3842SKoen Vandeputte 	}
6210c2e3842SKoen Vandeputte 
6224d33960bSJohannes Berg 	local->num_sta++;
6234d33960bSJohannes Berg 	local->sta_generation++;
6244d33960bSJohannes Berg 	smp_mb();
6254d33960bSJohannes Berg 
6265108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
6275108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
6285108ca82SJohannes Berg 
6294d33960bSJohannes Berg 	/* make the station visible */
63062b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
63162b14b24SJohannes Berg 	if (err)
63262b14b24SJohannes Berg 		goto out_drop_sta;
6334d33960bSJohannes Berg 
6342bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
63583d5cc01SJohannes Berg 
6365108ca82SJohannes Berg 	/* notify driver */
6375108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
6385108ca82SJohannes Berg 	if (err)
6395108ca82SJohannes Berg 		goto out_remove;
6405108ca82SJohannes Berg 
64183d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
642d405fd8cSGregory Greenman 
643d405fd8cSGregory Greenman 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
644d405fd8cSGregory Greenman 		ieee80211_recalc_min_chandef(sta->sdata);
645d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps)
646d405fd8cSGregory Greenman 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
647d405fd8cSGregory Greenman 	}
648d405fd8cSGregory Greenman 
6495108ca82SJohannes Berg 	/* accept BA sessions now */
6505108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
6514d33960bSJohannes Berg 
6524d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
6534d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
6544d33960bSJohannes Berg 
6550ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
6560ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
6570ef049dcSArnd Bergmann 	kfree(sinfo);
658d0709a65SJohannes Berg 
659bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
660f0706e82SJiri Benc 
66134e89507SJohannes Berg 	/* move reference to rcu-protected */
66234e89507SJohannes Berg 	rcu_read_lock();
66334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
664e9f207f0SJiri Benc 
66573651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
66673651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
66773651ee6SJohannes Berg 
66873651ee6SJohannes Berg 	return 0;
6695108ca82SJohannes Berg  out_remove:
6705108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
6715108ca82SJohannes Berg 	list_del_rcu(&sta->list);
67262b14b24SJohannes Berg  out_drop_sta:
6735108ca82SJohannes Berg 	local->num_sta--;
6745108ca82SJohannes Berg 	synchronize_net();
6755108ca82SJohannes Berg 	__cleanup_single_sta(sta);
6764d33960bSJohannes Berg  out_err:
6774d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
678ea32f065SSudip Mukherjee 	kfree(sinfo);
6794d33960bSJohannes Berg 	rcu_read_lock();
6804d33960bSJohannes Berg 	return err;
6818c71df7aSGuy Eilam }
6828c71df7aSGuy Eilam 
6838c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
6848c71df7aSGuy Eilam {
6858c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
686308f7fcfSZhao, Gang 	int err;
6878c71df7aSGuy Eilam 
6884d33960bSJohannes Berg 	might_sleep();
6894d33960bSJohannes Berg 
69031104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
69131104891SJohannes Berg 
6928c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
6938c71df7aSGuy Eilam 	if (err) {
69431104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
6958c71df7aSGuy Eilam 		rcu_read_lock();
6968c71df7aSGuy Eilam 		goto out_free;
6978c71df7aSGuy Eilam 	}
6988c71df7aSGuy Eilam 
6994d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
7008c71df7aSGuy Eilam 	if (err)
701004c872eSJohannes Berg 		goto out_free;
702d0709a65SJohannes Berg 
703004c872eSJohannes Berg 	return 0;
70493e5deb1SJohannes Berg  out_free:
705d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
70693e5deb1SJohannes Berg 	return err;
707f0706e82SJiri Benc }
708f0706e82SJiri Benc 
70934e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
71034e89507SJohannes Berg {
71134e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
71234e89507SJohannes Berg 
71334e89507SJohannes Berg 	rcu_read_unlock();
71434e89507SJohannes Berg 
71534e89507SJohannes Berg 	return err;
71634e89507SJohannes Berg }
71734e89507SJohannes Berg 
718d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
719004c872eSJohannes Berg {
720004c872eSJohannes Berg 	/*
721004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
722004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
723004c872eSJohannes Berg 	 */
724d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
725004c872eSJohannes Berg }
726004c872eSJohannes Berg 
727d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
728004c872eSJohannes Berg {
729004c872eSJohannes Berg 	/*
730004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
731004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
732004c872eSJohannes Berg 	 */
733d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
734004c872eSJohannes Berg }
735004c872eSJohannes Berg 
7363d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
7373d5839b6SIlan Peer {
7383d5839b6SIlan Peer 	/*
7393d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
7403d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
7413d5839b6SIlan Peer 	 */
7423d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
7433d5839b6SIlan Peer }
7443d5839b6SIlan Peer 
745948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
746004c872eSJohannes Berg {
747948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
748948d887dSJohannes Berg 	switch (ac) {
749948d887dSJohannes Berg 	case IEEE80211_AC_VO:
750948d887dSJohannes Berg 		return BIT(6) | BIT(7);
751948d887dSJohannes Berg 	case IEEE80211_AC_VI:
752948d887dSJohannes Berg 		return BIT(4) | BIT(5);
753948d887dSJohannes Berg 	case IEEE80211_AC_BE:
754948d887dSJohannes Berg 		return BIT(0) | BIT(3);
755948d887dSJohannes Berg 	case IEEE80211_AC_BK:
756948d887dSJohannes Berg 		return BIT(1) | BIT(2);
757948d887dSJohannes Berg 	default:
758948d887dSJohannes Berg 		WARN_ON(1);
759948d887dSJohannes Berg 		return 0;
760d0709a65SJohannes Berg 	}
761004c872eSJohannes Berg }
762004c872eSJohannes Berg 
7639b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
764004c872eSJohannes Berg {
765c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
766d012a605SMarco Porsch 	struct ps_data *ps;
767948d887dSJohannes Berg 	bool indicate_tim = false;
768948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
769948d887dSJohannes Berg 	int ac;
770a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
771004c872eSJohannes Berg 
772d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
773d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
774c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
775c868cb35SJohannes Berg 			return;
7763e122be0SJohannes Berg 
777d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
7783f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
7793f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
7803f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
7813f52b7e3SMarco Porsch #endif
782d012a605SMarco Porsch 	} else {
783d012a605SMarco Porsch 		return;
784d012a605SMarco Porsch 	}
785d012a605SMarco Porsch 
786c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
787d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
788c868cb35SJohannes Berg 		return;
789004c872eSJohannes Berg 
790c868cb35SJohannes Berg 	if (sta->dead)
791c868cb35SJohannes Berg 		goto done;
7923e122be0SJohannes Berg 
793948d887dSJohannes Berg 	/*
794948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
795948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
796948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
797948d887dSJohannes Berg 	 * non-enabled ones.
798948d887dSJohannes Berg 	 */
799948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
800948d887dSJohannes Berg 		ignore_for_tim = 0;
801948d887dSJohannes Berg 
8029b7a86f3SJohannes Berg 	if (ignore_pending)
8039b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
8049b7a86f3SJohannes Berg 
805948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
806948d887dSJohannes Berg 		unsigned long tids;
807948d887dSJohannes Berg 
808f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
809948d887dSJohannes Berg 			continue;
810948d887dSJohannes Berg 
811948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
812948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
813948d887dSJohannes Berg 		if (indicate_tim)
814948d887dSJohannes Berg 			break;
815948d887dSJohannes Berg 
816948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
817948d887dSJohannes Berg 
818948d887dSJohannes Berg 		indicate_tim |=
819948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
820ba8c3d6fSFelix Fietkau 		indicate_tim |=
821ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
822004c872eSJohannes Berg 	}
823004c872eSJohannes Berg 
824c868cb35SJohannes Berg  done:
82565f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
826004c872eSJohannes Berg 
8273d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
8283d5839b6SIlan Peer 		goto out_unlock;
8293d5839b6SIlan Peer 
830948d887dSJohannes Berg 	if (indicate_tim)
831d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
832c868cb35SJohannes Berg 	else
833d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
8343e122be0SJohannes Berg 
8359b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
836c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
837948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
838c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
839004c872eSJohannes Berg 	}
840004c872eSJohannes Berg 
8413d5839b6SIlan Peer out_unlock:
84265f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
843004c872eSJohannes Berg }
844004c872eSJohannes Berg 
8459b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
8469b7a86f3SJohannes Berg {
8479b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
8489b7a86f3SJohannes Berg }
8499b7a86f3SJohannes Berg 
850cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
851f0706e82SJiri Benc {
852e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
853f0706e82SJiri Benc 	int timeout;
854f0706e82SJiri Benc 
855f0706e82SJiri Benc 	if (!skb)
856cd0b8d89SJohannes Berg 		return false;
857f0706e82SJiri Benc 
858e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
859f0706e82SJiri Benc 
860f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
86157c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
86257c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
86357c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
864f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
865f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
866e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
867f0706e82SJiri Benc }
868f0706e82SJiri Benc 
869f0706e82SJiri Benc 
870948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
871948d887dSJohannes Berg 						struct sta_info *sta, int ac)
872f0706e82SJiri Benc {
873f0706e82SJiri Benc 	unsigned long flags;
874f0706e82SJiri Benc 	struct sk_buff *skb;
875f0706e82SJiri Benc 
87660750397SJohannes Berg 	/*
87760750397SJohannes Berg 	 * First check for frames that should expire on the filtered
87860750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
87960750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
88060750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
88160750397SJohannes Berg 	 * total_ps_buffered counter.
88260750397SJohannes Berg 	 */
883f0706e82SJiri Benc 	for (;;) {
884948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
885948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
88657c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
887948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
888836341a7SJohannes Berg 		else
889f0706e82SJiri Benc 			skb = NULL;
890948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
891f0706e82SJiri Benc 
89260750397SJohannes Berg 		/*
89360750397SJohannes Berg 		 * Frames are queued in order, so if this one
89460750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
89560750397SJohannes Berg 		 * we actually reached the end of the queue we
89660750397SJohannes Berg 		 * also need to stop, of course.
89760750397SJohannes Berg 		 */
89860750397SJohannes Berg 		if (!skb)
89960750397SJohannes Berg 			break;
900d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
90160750397SJohannes Berg 	}
90260750397SJohannes Berg 
90360750397SJohannes Berg 	/*
90460750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
90560750397SJohannes Berg 	 * only find something if the filtered queue was emptied
90660750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
90760750397SJohannes Berg 	 * buffered frames.
90860750397SJohannes Berg 	 */
909f0706e82SJiri Benc 	for (;;) {
910948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
911948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
912f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
913948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
914f0706e82SJiri Benc 		else
915f0706e82SJiri Benc 			skb = NULL;
916948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
917f0706e82SJiri Benc 
91860750397SJohannes Berg 		/*
91960750397SJohannes Berg 		 * frames are queued in order, so if this one
92060750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
92160750397SJohannes Berg 		 * the queue) we can stop testing
92260750397SJohannes Berg 		 */
923836341a7SJohannes Berg 		if (!skb)
924836341a7SJohannes Berg 			break;
925836341a7SJohannes Berg 
926f0706e82SJiri Benc 		local->total_ps_buffered--;
927bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
928bdcbd8e0SJohannes Berg 		       sta->sta.addr);
929d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
930f0706e82SJiri Benc 	}
9313393a608SJuuso Oikarinen 
93260750397SJohannes Berg 	/*
93360750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
93460750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
93560750397SJohannes Berg 	 * frames.
93660750397SJohannes Berg 	 */
93760750397SJohannes Berg 	sta_info_recalc_tim(sta);
93860750397SJohannes Berg 
93960750397SJohannes Berg 	/*
94060750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
94160750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
94260750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
94360750397SJohannes Berg 	 */
944948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
945948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
946948d887dSJohannes Berg }
947948d887dSJohannes Berg 
948948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
949948d887dSJohannes Berg 					     struct sta_info *sta)
950948d887dSJohannes Berg {
951948d887dSJohannes Berg 	bool have_buffered = false;
952948d887dSJohannes Berg 	int ac;
953948d887dSJohannes Berg 
9543f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
9553f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
9563f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
957948d887dSJohannes Berg 		return false;
958948d887dSJohannes Berg 
959948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
960948d887dSJohannes Berg 		have_buffered |=
961948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
962948d887dSJohannes Berg 
963948d887dSJohannes Berg 	return have_buffered;
964f0706e82SJiri Benc }
965f0706e82SJiri Benc 
966d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
96734e89507SJohannes Berg {
96834e89507SJohannes Berg 	struct ieee80211_local *local;
96934e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
9706d10e46bSJohannes Berg 	int ret;
97134e89507SJohannes Berg 
97234e89507SJohannes Berg 	might_sleep();
97334e89507SJohannes Berg 
97434e89507SJohannes Berg 	if (!sta)
97534e89507SJohannes Berg 		return -ENOENT;
97634e89507SJohannes Berg 
97734e89507SJohannes Berg 	local = sta->local;
97834e89507SJohannes Berg 	sdata = sta->sdata;
97934e89507SJohannes Berg 
98083d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
98183d5cc01SJohannes Berg 
982098a6070SJohannes Berg 	/*
983098a6070SJohannes Berg 	 * Before removing the station from the driver and
984098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
985098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
986098a6070SJohannes Berg 	 * will be sufficient.
987098a6070SJohannes Berg 	 */
988c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
989c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
990098a6070SJohannes Berg 
991f59374ebSSara Sharon 	/*
992f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
993f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
994f59374ebSSara Sharon 	 * all such frames to be processed.
995f59374ebSSara Sharon 	 */
996f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
997f59374ebSSara Sharon 
99834e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
999b01711beSJohannes Berg 	if (WARN_ON(ret))
100034e89507SJohannes Berg 		return ret;
100134e89507SJohannes Berg 
1002a7a6bdd0SArik Nemtsov 	/*
1003a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
1004a7a6bdd0SArik Nemtsov 	 * removal.
1005a7a6bdd0SArik Nemtsov 	 */
1006a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1007a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1008a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1009a7a6bdd0SArik Nemtsov 	}
1010a7a6bdd0SArik Nemtsov 
1011794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
1012ef044763SEliad Peller 	sta->removed = true;
10134d33960bSJohannes Berg 
10146a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
10156a9d1b91SJohannes Berg 
1016a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1017a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1018a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1019a710c816SJohannes Berg 
1020d778207bSJohannes Berg 	return 0;
1021d778207bSJohannes Berg }
1022d778207bSJohannes Berg 
1023d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
1024d778207bSJohannes Berg {
1025d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1026d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
10270ef049dcSArnd Bergmann 	struct station_info *sinfo;
1028d778207bSJohannes Berg 	int ret;
1029d778207bSJohannes Berg 
1030d778207bSJohannes Berg 	/*
1031d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
1032d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
1033d778207bSJohannes Berg 	 */
1034d778207bSJohannes Berg 
1035d778207bSJohannes Berg 	might_sleep();
1036d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
1037d778207bSJohannes Berg 
1038c8782078SJohannes Berg 	/* now keys can no longer be reached */
10396d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
104034e89507SJohannes Berg 
10419b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
10429b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
10439b7a86f3SJohannes Berg 
104434e89507SJohannes Berg 	sta->dead = true;
104534e89507SJohannes Berg 
104634e89507SJohannes Berg 	local->num_sta--;
104734e89507SJohannes Berg 	local->sta_generation++;
104834e89507SJohannes Berg 
104983d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
1050f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
1051f09603a2SJohannes Berg 		if (ret) {
105283d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
105383d5cc01SJohannes Berg 			break;
105483d5cc01SJohannes Berg 		}
105583d5cc01SJohannes Berg 	}
1056d9a7ddb0SJohannes Berg 
1057f09603a2SJohannes Berg 	if (sta->uploaded) {
1058f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1059f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1060f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1061f09603a2SJohannes Berg 	}
106234e89507SJohannes Berg 
1063bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1064bdcbd8e0SJohannes Berg 
10650ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
10660ef049dcSArnd Bergmann 	if (sinfo)
10670fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
10680ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
10690ef049dcSArnd Bergmann 	kfree(sinfo);
1070ec15e68bSJouni Malinen 
107134e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
107234e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
107334e89507SJohannes Berg 
1074d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1075d778207bSJohannes Berg }
1076d778207bSJohannes Berg 
1077d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1078d778207bSJohannes Berg {
1079d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1080d778207bSJohannes Berg 
1081d778207bSJohannes Berg 	if (err)
1082d778207bSJohannes Berg 		return err;
1083d778207bSJohannes Berg 
1084d778207bSJohannes Berg 	synchronize_net();
1085d778207bSJohannes Berg 
1086d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
108734e89507SJohannes Berg 
108834e89507SJohannes Berg 	return 0;
108934e89507SJohannes Berg }
109034e89507SJohannes Berg 
109134e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
109234e89507SJohannes Berg {
109334e89507SJohannes Berg 	struct sta_info *sta;
109434e89507SJohannes Berg 	int ret;
109534e89507SJohannes Berg 
109634e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
10977852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
109834e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
109934e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
110034e89507SJohannes Berg 
110134e89507SJohannes Berg 	return ret;
110234e89507SJohannes Berg }
110334e89507SJohannes Berg 
110434e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
110534e89507SJohannes Berg 			      const u8 *addr)
110634e89507SJohannes Berg {
110734e89507SJohannes Berg 	struct sta_info *sta;
110834e89507SJohannes Berg 	int ret;
110934e89507SJohannes Berg 
111034e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
11117852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
111234e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
111334e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
111434e89507SJohannes Berg 
111534e89507SJohannes Berg 	return ret;
111634e89507SJohannes Berg }
1117f0706e82SJiri Benc 
111834f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1119f0706e82SJiri Benc {
112034f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1121f0706e82SJiri Benc 	struct sta_info *sta;
11223393a608SJuuso Oikarinen 	bool timer_needed = false;
1123f0706e82SJiri Benc 
1124d0709a65SJohannes Berg 	rcu_read_lock();
1125d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
11263393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
11273393a608SJuuso Oikarinen 			timer_needed = true;
1128d0709a65SJohannes Berg 	rcu_read_unlock();
1129f0706e82SJiri Benc 
11305bb644a0SJohannes Berg 	if (local->quiescing)
11315bb644a0SJohannes Berg 		return;
11325bb644a0SJohannes Berg 
11333393a608SJuuso Oikarinen 	if (!timer_needed)
11343393a608SJuuso Oikarinen 		return;
11353393a608SJuuso Oikarinen 
113626d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
113726d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1138f0706e82SJiri Benc }
1139f0706e82SJiri Benc 
11407bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
11417bedd0cfSJohannes Berg {
11427bedd0cfSJohannes Berg 	int err;
11437bedd0cfSJohannes Berg 
114483e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
11457bedd0cfSJohannes Berg 	if (err)
11467bedd0cfSJohannes Berg 		return err;
11477bedd0cfSJohannes Berg 
11484d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
114934e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1150f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1151f0706e82SJiri Benc 
115234f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
11537bedd0cfSJohannes Berg 	return 0;
1154f0706e82SJiri Benc }
1155f0706e82SJiri Benc 
1156f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1157f0706e82SJiri Benc {
1158a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
115983e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1160f0706e82SJiri Benc }
1161f0706e82SJiri Benc 
1162051007d9SJohannes Berg 
1163e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1164f0706e82SJiri Benc {
1165b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1166f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1167d778207bSJohannes Berg 	LIST_HEAD(free_list);
116844213b5eSJohannes Berg 	int ret = 0;
1169f0706e82SJiri Benc 
1170d0709a65SJohannes Berg 	might_sleep();
1171d0709a65SJohannes Berg 
1172e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1173e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1174e716251dSJohannes Berg 
117534e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
117634e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1177e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1178e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1179d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1180d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
118134316837SJohannes Berg 			ret++;
118234316837SJohannes Berg 		}
118334e89507SJohannes Berg 	}
1184d778207bSJohannes Berg 
1185d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1186d778207bSJohannes Berg 		synchronize_net();
1187d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1188d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1189d778207bSJohannes Berg 	}
119034e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
119144213b5eSJohannes Berg 
1192051007d9SJohannes Berg 	return ret;
1193051007d9SJohannes Berg }
1194051007d9SJohannes Berg 
119524723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
119624723d1bSJohannes Berg 			  unsigned long exp_time)
119724723d1bSJohannes Berg {
119824723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
119924723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
120024723d1bSJohannes Berg 
120134e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1202e46a2cf9SMohammed Shafi Shajakhan 
1203e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1204b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1205b8da6b6aSJohannes Berg 
1206ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1207ec2b774eSMarek Lindner 			continue;
1208ec2b774eSMarek Lindner 
1209b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1210eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1211bdcbd8e0SJohannes Berg 				sta->sta.addr);
12123f52b7e3SMarco Porsch 
12133f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
12143f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
12153f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
12163f52b7e3SMarco Porsch 
121734e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
121824723d1bSJohannes Berg 		}
1219e46a2cf9SMohammed Shafi Shajakhan 	}
1220e46a2cf9SMohammed Shafi Shajakhan 
122134e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
122224723d1bSJohannes Berg }
122317741cdcSJohannes Berg 
1224686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1225686b9cb9SBen Greear 						   const u8 *addr,
1226686b9cb9SBen Greear 						   const u8 *localaddr)
122717741cdcSJohannes Berg {
12287bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
122983e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
12307bedd0cfSJohannes Berg 	struct sta_info *sta;
123117741cdcSJohannes Berg 
1232686b9cb9SBen Greear 	/*
1233686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1234686b9cb9SBen Greear 	 * ... first in list.
1235686b9cb9SBen Greear 	 */
123683e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1237686b9cb9SBen Greear 		if (localaddr &&
1238b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1239686b9cb9SBen Greear 			continue;
1240f7c65594SJohannes Berg 		if (!sta->uploaded)
1241f7c65594SJohannes Berg 			return NULL;
124217741cdcSJohannes Berg 		return &sta->sta;
1243f7c65594SJohannes Berg 	}
1244f7c65594SJohannes Berg 
1245abe60632SJohannes Berg 	return NULL;
124617741cdcSJohannes Berg }
1247686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
12485ed176e1SJohannes Berg 
12495ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
12505ed176e1SJohannes Berg 					 const u8 *addr)
12515ed176e1SJohannes Berg {
1252f7c65594SJohannes Berg 	struct sta_info *sta;
12535ed176e1SJohannes Berg 
12545ed176e1SJohannes Berg 	if (!vif)
12555ed176e1SJohannes Berg 		return NULL;
12565ed176e1SJohannes Berg 
1257f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1258f7c65594SJohannes Berg 	if (!sta)
1259f7c65594SJohannes Berg 		return NULL;
12605ed176e1SJohannes Berg 
1261f7c65594SJohannes Berg 	if (!sta->uploaded)
1262f7c65594SJohannes Berg 		return NULL;
1263f7c65594SJohannes Berg 
1264f7c65594SJohannes Berg 	return &sta->sta;
12655ed176e1SJohannes Berg }
126617741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1267af818581SJohannes Berg 
1268e3685e03SJohannes Berg /* powersave support code */
1269e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
127050a9432dSJohannes Berg {
1271608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1272e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1273e3685e03SJohannes Berg 	struct sk_buff_head pending;
1274ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1275e3685e03SJohannes Berg 	unsigned long flags;
1276d012a605SMarco Porsch 	struct ps_data *ps;
1277d012a605SMarco Porsch 
12783918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
12793918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
12803918edb0SFelix Fietkau 				     u.ap);
12813918edb0SFelix Fietkau 
12823918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1283d012a605SMarco Porsch 		ps = &sdata->bss->ps;
12843f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
12853f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1286d012a605SMarco Porsch 	else
1287d012a605SMarco Porsch 		return;
128850a9432dSJohannes Berg 
1289c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
129047086fc5SJohannes Berg 
12915a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1292948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1293ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1294948d887dSJohannes Berg 
129530686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
129612375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1297af818581SJohannes Berg 
1298ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1299adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1300ba8c3d6fSFelix Fietkau 			continue;
1301ba8c3d6fSFelix Fietkau 
130218667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1303ba8c3d6fSFelix Fietkau 	}
1304ba8c3d6fSFelix Fietkau 
1305948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1306af818581SJohannes Berg 
13071d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
13081d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1309af818581SJohannes Berg 	/* Send all buffered frames to the station */
1310948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1311948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1312948d887dSJohannes Berg 
1313987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1314948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1315987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1316948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1317948d887dSJohannes Berg 		filtered += tmp - count;
1318948d887dSJohannes Berg 		count = tmp;
1319948d887dSJohannes Berg 
1320987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1321948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1322987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1323948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1324948d887dSJohannes Berg 		buffered += tmp - count;
1325948d887dSJohannes Berg 	}
1326948d887dSJohannes Berg 
1327e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
13285ac2e350SJohannes Berg 
13295ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
13305ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
13315ac2e350SJohannes Berg 
13325ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
13335ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
13345ac2e350SJohannes Berg 	 */
13355ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
13365ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
13371d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1338948d887dSJohannes Berg 
1339e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1340e3685e03SJohannes Berg 
1341687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1342062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1343062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1344687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1345687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1346687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1347687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1348687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1349687da132SEmmanuel Grumbach 		       sta->sta.addr);
1350687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1351687da132SEmmanuel Grumbach 					   sta->sta.addr,
1352687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1353687da132SEmmanuel Grumbach 	}
1354687da132SEmmanuel Grumbach 
1355af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1356af818581SJohannes Berg 
1357c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1358c868cb35SJohannes Berg 
1359bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
13602595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1361bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
136217c18bf8SJohannes Berg 
136317c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1364af818581SJohannes Berg }
1365af818581SJohannes Berg 
13660ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1367b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
13680ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1369ce662b44SJohannes Berg {
13700ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1371ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1372ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1373ce662b44SJohannes Berg 	struct sk_buff *skb;
1374ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1375ce662b44SJohannes Berg 	__le16 fc;
1376a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1377ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
137855de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1379ce662b44SJohannes Berg 
138041cbb0f5SLuca Coelho 	/* Don't send NDPs when STA is connected HE */
138141cbb0f5SLuca Coelho 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
138241cbb0f5SLuca Coelho 	    !(sdata->u.mgd.flags & IEEE80211_STA_DISABLE_HE))
138341cbb0f5SLuca Coelho 		return;
138441cbb0f5SLuca Coelho 
1385ce662b44SJohannes Berg 	if (qos) {
1386ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1387ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1388ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1389ce662b44SJohannes Berg 	} else {
1390ce662b44SJohannes Berg 		size -= 2;
1391ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1392ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1393ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1394ce662b44SJohannes Berg 	}
1395ce662b44SJohannes Berg 
1396ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1397ce662b44SJohannes Berg 	if (!skb)
1398ce662b44SJohannes Berg 		return;
1399ce662b44SJohannes Berg 
1400ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1401ce662b44SJohannes Berg 
14024df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1403ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1404ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1405ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1406ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1407ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1408864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1409ce662b44SJohannes Berg 
1410ce662b44SJohannes Berg 	skb->priority = tid;
1411ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
141259b66255SJohannes Berg 	if (qos) {
1413ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1414ce662b44SJohannes Berg 
14150ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1416ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1417ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
14180ead2510SEmmanuel Grumbach 			if (more_data)
14190ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
14200ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
14210ead2510SEmmanuel Grumbach 		}
1422ce662b44SJohannes Berg 	}
1423ce662b44SJohannes Berg 
1424ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1425ce662b44SJohannes Berg 
1426ce662b44SJohannes Berg 	/*
1427ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1428ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1429deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1430deeaee19SJohannes Berg 	 * ends the poll/service period.
1431ce662b44SJohannes Berg 	 */
143202f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1433deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1434ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1435ce662b44SJohannes Berg 
14366b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
14376b127c71SSujith Manoharan 
1438b77cf4f8SJohannes Berg 	if (call_driver)
1439b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1440b77cf4f8SJohannes Berg 					  reason, false);
144140b96408SJohannes Berg 
144289afe614SJohannes Berg 	skb->dev = sdata->dev;
144389afe614SJohannes Berg 
144455de908aSJohannes Berg 	rcu_read_lock();
144555de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
144655de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
144755de908aSJohannes Berg 		rcu_read_unlock();
144855de908aSJohannes Berg 		kfree_skb(skb);
144955de908aSJohannes Berg 		return;
145055de908aSJohannes Berg 	}
145155de908aSJohannes Berg 
145273c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
1453b9771d41SJohannes Berg 	ieee80211_xmit(sdata, sta, skb, 0);
145455de908aSJohannes Berg 	rcu_read_unlock();
1455ce662b44SJohannes Berg }
1456ce662b44SJohannes Berg 
14570a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
14580a1cb809SJohannes Berg {
14590a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
14600a1cb809SJohannes Berg 	if (tids & 0xF8)
14610a1cb809SJohannes Berg 		return fls(tids) - 1;
14620a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
14630a1cb809SJohannes Berg 	if (tids & BIT(0))
14640a1cb809SJohannes Berg 		return 0;
14650a1cb809SJohannes Berg 	return fls(tids) - 1;
14660a1cb809SJohannes Berg }
14670a1cb809SJohannes Berg 
14680ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
14690ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
14700ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
14710ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
14720ead2510SEmmanuel Grumbach  */
14730ead2510SEmmanuel Grumbach static bool
14740ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
14750ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
14760ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
14770ead2510SEmmanuel Grumbach {
14780ead2510SEmmanuel Grumbach 	int ac;
14790ead2510SEmmanuel Grumbach 
14800ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
14810ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
14820ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
14830ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
14840ead2510SEmmanuel Grumbach 	 */
14850ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
14860ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
14870ead2510SEmmanuel Grumbach 		return true;
14880ead2510SEmmanuel Grumbach 
14890ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1490f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
14910ead2510SEmmanuel Grumbach 			continue;
14920ead2510SEmmanuel Grumbach 
14930ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
14940ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
14950ead2510SEmmanuel Grumbach 			return true;
14960ead2510SEmmanuel Grumbach 	}
14970ead2510SEmmanuel Grumbach 
14980ead2510SEmmanuel Grumbach 	return false;
14990ead2510SEmmanuel Grumbach }
15000ead2510SEmmanuel Grumbach 
150147086fc5SJohannes Berg static void
15020ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
15030ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
15040ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
15050ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1506af818581SJohannes Berg {
1507af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1508af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1509948d887dSJohannes Berg 	int ac;
1510af818581SJohannes Berg 
1511f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1512948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
15134049e09aSJohannes Berg 		unsigned long tids;
15144049e09aSJohannes Berg 
1515f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1516948d887dSJohannes Berg 			continue;
1517948d887dSJohannes Berg 
15184049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
15194049e09aSJohannes Berg 
1520f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1521f9f760b4SJohannes Berg 		 * release from hardware queues
1522f9f760b4SJohannes Berg 		 */
15230ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
15240ead2510SEmmanuel Grumbach 			*driver_release_tids |=
15250ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
15260ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1527ba8c3d6fSFelix Fietkau 		}
1528f9f760b4SJohannes Berg 
15290ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
153047086fc5SJohannes Berg 			struct sk_buff *skb;
153147086fc5SJohannes Berg 
153247086fc5SJohannes Berg 			while (n_frames > 0) {
1533948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1534948d887dSJohannes Berg 				if (!skb) {
153547086fc5SJohannes Berg 					skb = skb_dequeue(
153647086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1537af818581SJohannes Berg 					if (skb)
1538af818581SJohannes Berg 						local->total_ps_buffered--;
1539af818581SJohannes Berg 				}
154047086fc5SJohannes Berg 				if (!skb)
154147086fc5SJohannes Berg 					break;
154247086fc5SJohannes Berg 				n_frames--;
15430ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
154447086fc5SJohannes Berg 			}
1545948d887dSJohannes Berg 		}
1546948d887dSJohannes Berg 
15470ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
15480ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
15490ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
15504049e09aSJohannes Berg 		 */
1551948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
15520ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1553948d887dSJohannes Berg 			break;
1554948d887dSJohannes Berg 	}
1555948d887dSJohannes Berg }
1556af818581SJohannes Berg 
15570ead2510SEmmanuel Grumbach static void
15580ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
15590ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
15600ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
15610ead2510SEmmanuel Grumbach {
15620ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
15630ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
15640ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
15650ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
15660ead2510SEmmanuel Grumbach 	bool more_data;
15670ead2510SEmmanuel Grumbach 
15680ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
15690ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
15700ead2510SEmmanuel Grumbach 
15710ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
15720ead2510SEmmanuel Grumbach 
15730ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
15740ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
15750ead2510SEmmanuel Grumbach 
15760ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
15770ead2510SEmmanuel Grumbach 
15781a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
15790ead2510SEmmanuel Grumbach 		driver_release_tids =
15800ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
15810ead2510SEmmanuel Grumbach 
1582f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1583f438ceb8SEmmanuel Grumbach 		int tid, ac;
15844049e09aSJohannes Berg 
1585ce662b44SJohannes Berg 		/*
1586ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1587ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1588ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1589ce662b44SJohannes Berg 		 *
1590ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1591ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1592ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1593ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1594ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1595ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1596ce662b44SJohannes Berg 		 *
1597ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1598ce662b44SJohannes Berg 		 */
1599ce662b44SJohannes Berg 
1600ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1601f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1602d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1603d7f84244SEmmanuel Grumbach 				break;
1604f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1605ce662b44SJohannes Berg 
16060ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1607f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
160847086fc5SJohannes Berg 		struct sk_buff_head pending;
160947086fc5SJohannes Berg 		struct sk_buff *skb;
161040b96408SJohannes Berg 		int num = 0;
161140b96408SJohannes Berg 		u16 tids = 0;
1612b77cf4f8SJohannes Berg 		bool need_null = false;
161347086fc5SJohannes Berg 
161447086fc5SJohannes Berg 		skb_queue_head_init(&pending);
161547086fc5SJohannes Berg 
161647086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1617af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
161847086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
161940b96408SJohannes Berg 			u8 *qoshdr = NULL;
162040b96408SJohannes Berg 
162140b96408SJohannes Berg 			num++;
1622af818581SJohannes Berg 
1623af818581SJohannes Berg 			/*
162447086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
162547086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
162647086fc5SJohannes Berg 			 * exchange.
1627af818581SJohannes Berg 			 */
16286b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
16296b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1630af818581SJohannes Berg 
163147086fc5SJohannes Berg 			/*
163247086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
163347086fc5SJohannes Berg 			 * more buffered frames for this STA
163447086fc5SJohannes Berg 			 */
163524b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
163647086fc5SJohannes Berg 				hdr->frame_control |=
163747086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
163824b9c373SJanusz.Dziedzic@tieto.com 			else
163924b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
164024b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1641af818581SJohannes Berg 
164240b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
164340b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
164440b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
164540b96408SJohannes Berg 
1646b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1647b77cf4f8SJohannes Berg 
1648b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1649b77cf4f8SJohannes Berg 
1650b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1651b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1652b77cf4f8SJohannes Berg 				continue;
1653b77cf4f8SJohannes Berg 
1654b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1655b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1656b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1657b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1658b77cf4f8SJohannes Berg 				break;
1659b77cf4f8SJohannes Berg 			}
1660b77cf4f8SJohannes Berg 
1661b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1662b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1663b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1664b77cf4f8SJohannes Berg 			 * and be done.
1665b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1666b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1667b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1668b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1669b77cf4f8SJohannes Berg 			 *
1670b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1671b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1672b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1673b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1674b77cf4f8SJohannes Berg 			 *
1675b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1676b77cf4f8SJohannes Berg 			 */
1677b77cf4f8SJohannes Berg 			if (qoshdr) {
167840b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1679deeaee19SJohannes Berg 
168047086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
168147086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1682b77cf4f8SJohannes Berg 			} else {
1683b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1684b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1685b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1686b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1687b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1688b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1689b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1690b77cf4f8SJohannes Berg 				 */
1691b77cf4f8SJohannes Berg 				hdr->frame_control |=
1692b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1693b77cf4f8SJohannes Berg 				need_null = true;
1694b77cf4f8SJohannes Berg 				num++;
169552a3f20cSMarco Porsch 			}
1696b77cf4f8SJohannes Berg 			break;
169747086fc5SJohannes Berg 		}
169847086fc5SJohannes Berg 
169940b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
170040b96408SJohannes Berg 					  reason, more_data);
170140b96408SJohannes Berg 
170247086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1703af818581SJohannes Berg 
1704b77cf4f8SJohannes Berg 		if (need_null)
1705b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
17060ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
17070ead2510SEmmanuel Grumbach 				reason, false, false);
1708b77cf4f8SJohannes Berg 
1709c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1710af818581SJohannes Berg 	} else {
1711ba8c3d6fSFelix Fietkau 		int tid;
1712ba8c3d6fSFelix Fietkau 
1713af818581SJohannes Berg 		/*
17144049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
17154049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1716f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1717f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1718f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1719f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1720f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1721f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1722af818581SJohannes Berg 		 */
17234049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
172447086fc5SJohannes Berg 					    n_frames, reason, more_data);
17254049e09aSJohannes Berg 
17264049e09aSJohannes Berg 		/*
17274049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
17284049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1729f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
17304049e09aSJohannes Berg 		 * release function.
1731f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1732ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1733ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
17344049e09aSJohannes Berg 		 */
1735ba8c3d6fSFelix Fietkau 
1736ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1737ba8c3d6fSFelix Fietkau 			return;
1738ba8c3d6fSFelix Fietkau 
1739ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1740adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
1741adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
17421e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1743ba8c3d6fSFelix Fietkau 				continue;
1744ba8c3d6fSFelix Fietkau 
1745ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1746ba8c3d6fSFelix Fietkau 			break;
1747ba8c3d6fSFelix Fietkau 		}
1748af818581SJohannes Berg 	}
1749af818581SJohannes Berg }
1750af818581SJohannes Berg 
1751af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1752af818581SJohannes Berg {
175347086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1754af818581SJohannes Berg 
1755af818581SJohannes Berg 	/*
175647086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
175747086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
175847086fc5SJohannes Berg 	 * only from the non-enabled ones.
1759af818581SJohannes Berg 	 */
176047086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
176147086fc5SJohannes Berg 		ignore_for_response = 0;
1762af818581SJohannes Berg 
176347086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
176447086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1765af818581SJohannes Berg }
176647086fc5SJohannes Berg 
176747086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
176847086fc5SJohannes Berg {
176947086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
177047086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
177147086fc5SJohannes Berg 
177247086fc5SJohannes Berg 	/*
177347086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
177447086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
177547086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
177647086fc5SJohannes Berg 	 * actually getting called.
177747086fc5SJohannes Berg 	 */
177847086fc5SJohannes Berg 	if (!delivery_enabled)
177947086fc5SJohannes Berg 		return;
178047086fc5SJohannes Berg 
178147086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
178247086fc5SJohannes Berg 	case 1:
178347086fc5SJohannes Berg 		n_frames = 2;
178447086fc5SJohannes Berg 		break;
178547086fc5SJohannes Berg 	case 2:
178647086fc5SJohannes Berg 		n_frames = 4;
178747086fc5SJohannes Berg 		break;
178847086fc5SJohannes Berg 	case 3:
178947086fc5SJohannes Berg 		n_frames = 6;
179047086fc5SJohannes Berg 		break;
179147086fc5SJohannes Berg 	case 0:
179247086fc5SJohannes Berg 		/* XXX: what is a good value? */
179313a8098aSAndrei Otcheretianski 		n_frames = 128;
179447086fc5SJohannes Berg 		break;
179547086fc5SJohannes Berg 	}
179647086fc5SJohannes Berg 
179747086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
179847086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1799af818581SJohannes Berg }
1800af818581SJohannes Berg 
1801af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1802af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1803af818581SJohannes Berg {
1804af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1805af818581SJohannes Berg 
1806b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1807b5878a2dSJohannes Berg 
18085ac2e350SJohannes Berg 	if (block) {
1809c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
181017c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
18115ac2e350SJohannes Berg 		return;
18125ac2e350SJohannes Berg 	}
18135ac2e350SJohannes Berg 
18145ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
18155ac2e350SJohannes Berg 		return;
18165ac2e350SJohannes Berg 
18175ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
18185ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
18195ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
18205ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
18215ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
18225ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
18235ac2e350SJohannes Berg 		/* must be asleep in this case */
18245ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
18255ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
18265ac2e350SJohannes Berg 	} else {
18275ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
182817c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
18295ac2e350SJohannes Berg 	}
1830af818581SJohannes Berg }
1831af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1832dcf55fb5SFelix Fietkau 
1833e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
183437fbd908SJohannes Berg {
183537fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
183637fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
183737fbd908SJohannes Berg 
183837fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
183937fbd908SJohannes Berg 
184037fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
184137fbd908SJohannes Berg }
1842e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
184337fbd908SJohannes Berg 
18440ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
18450ead2510SEmmanuel Grumbach {
18460ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
18470ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
18480ead2510SEmmanuel Grumbach 	bool more_data;
18490ead2510SEmmanuel Grumbach 
18500ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
18510ead2510SEmmanuel Grumbach 
18520ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
18530ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
18540ead2510SEmmanuel Grumbach 					       reason, 0);
18550ead2510SEmmanuel Grumbach 
18560ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
18570ead2510SEmmanuel Grumbach }
18580ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
18590ead2510SEmmanuel Grumbach 
1860042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1861042ec453SJohannes Berg 				u8 tid, bool buffered)
1862dcf55fb5SFelix Fietkau {
1863dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1864dcf55fb5SFelix Fietkau 
18655a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1866042ec453SJohannes Berg 		return;
1867042ec453SJohannes Berg 
18681b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
18691b000789SJohannes Berg 
1870948d887dSJohannes Berg 	if (buffered)
1871948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1872948d887dSJohannes Berg 	else
1873948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1874948d887dSJohannes Berg 
1875c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1876dcf55fb5SFelix Fietkau }
1877042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1878d9a7ddb0SJohannes Berg 
1879b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
1880b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
1881b4809e94SToke Høiland-Jørgensen {
1882b4809e94SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1883b4809e94SToke Høiland-Jørgensen 	struct ieee80211_local *local = sta->sdata->local;
1884b4809e94SToke Høiland-Jørgensen 	u8 ac = ieee80211_ac_from_tid(tid);
1885b4809e94SToke Høiland-Jørgensen 	u32 airtime = 0;
1886b4809e94SToke Høiland-Jørgensen 
1887b4809e94SToke Høiland-Jørgensen 	if (sta->local->airtime_flags & AIRTIME_USE_TX)
1888b4809e94SToke Høiland-Jørgensen 		airtime += tx_airtime;
1889b4809e94SToke Høiland-Jørgensen 	if (sta->local->airtime_flags & AIRTIME_USE_RX)
1890b4809e94SToke Høiland-Jørgensen 		airtime += rx_airtime;
1891b4809e94SToke Høiland-Jørgensen 
1892b4809e94SToke Høiland-Jørgensen 	spin_lock_bh(&local->active_txq_lock[ac]);
1893b4809e94SToke Høiland-Jørgensen 	sta->airtime[ac].tx_airtime += tx_airtime;
1894b4809e94SToke Høiland-Jørgensen 	sta->airtime[ac].rx_airtime += rx_airtime;
1895b4809e94SToke Høiland-Jørgensen 	sta->airtime[ac].deficit -= airtime;
1896b4809e94SToke Høiland-Jørgensen 	spin_unlock_bh(&local->active_txq_lock[ac]);
1897b4809e94SToke Høiland-Jørgensen }
1898b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
1899b4809e94SToke Høiland-Jørgensen 
190083d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1901d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1902d9a7ddb0SJohannes Berg {
19038bf11d8dSJohannes Berg 	might_sleep();
1904d9a7ddb0SJohannes Berg 
1905d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1906d9a7ddb0SJohannes Berg 		return 0;
1907d9a7ddb0SJohannes Berg 
1908f09603a2SJohannes Berg 	/* check allowed transitions first */
1909f09603a2SJohannes Berg 
1910d9a7ddb0SJohannes Berg 	switch (new_state) {
1911d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1912f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1913d9a7ddb0SJohannes Berg 			return -EINVAL;
1914d9a7ddb0SJohannes Berg 		break;
1915d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1916f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1917f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1918d9a7ddb0SJohannes Berg 			return -EINVAL;
1919d9a7ddb0SJohannes Berg 		break;
1920d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1921f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1922f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1923d9a7ddb0SJohannes Berg 			return -EINVAL;
1924d9a7ddb0SJohannes Berg 		break;
1925d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1926f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1927d9a7ddb0SJohannes Berg 			return -EINVAL;
1928d9a7ddb0SJohannes Berg 		break;
1929d9a7ddb0SJohannes Berg 	default:
1930d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1931d9a7ddb0SJohannes Berg 		return -EINVAL;
1932d9a7ddb0SJohannes Berg 	}
1933d9a7ddb0SJohannes Berg 
1934bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1935bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1936f09603a2SJohannes Berg 
1937f09603a2SJohannes Berg 	/*
1938f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1939f09603a2SJohannes Berg 	 * fail the transition
1940f09603a2SJohannes Berg 	 */
1941f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1942f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1943f09603a2SJohannes Berg 					sta->sta_state, new_state);
1944f09603a2SJohannes Berg 		if (err)
1945f09603a2SJohannes Berg 			return err;
1946f09603a2SJohannes Berg 	}
1947f09603a2SJohannes Berg 
1948f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1949f09603a2SJohannes Berg 
1950f09603a2SJohannes Berg 	switch (new_state) {
1951f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1952f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1953f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1954f09603a2SJohannes Berg 		break;
1955f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1956a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
1957f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1958a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1959f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1960a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
196152cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
196252cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1963a7201a6cSIlan Peer 		}
1964f09603a2SJohannes Berg 		break;
1965f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1966f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1967f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1968a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
196952cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
197052cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1971f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
197272f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
1973f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
197417c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
197549ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
1976f09603a2SJohannes Berg 		}
1977f09603a2SJohannes Berg 		break;
1978f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1979f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
198072f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
1981f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
198217c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
198349ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
1984f09603a2SJohannes Berg 		}
1985f09603a2SJohannes Berg 		break;
1986f09603a2SJohannes Berg 	default:
1987f09603a2SJohannes Berg 		break;
1988f09603a2SJohannes Berg 	}
1989f09603a2SJohannes Berg 
1990d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1991d9a7ddb0SJohannes Berg 
1992d9a7ddb0SJohannes Berg 	return 0;
1993d9a7ddb0SJohannes Berg }
1994687da132SEmmanuel Grumbach 
1995687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1996687da132SEmmanuel Grumbach {
1997687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1998687da132SEmmanuel Grumbach 	u8 rx_streams;
1999687da132SEmmanuel Grumbach 
2000687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
2001687da132SEmmanuel Grumbach 		return 1;
2002687da132SEmmanuel Grumbach 
2003687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
2004687da132SEmmanuel Grumbach 		int i;
2005687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
2006687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
2007687da132SEmmanuel Grumbach 
2008687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
2009687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
2010687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
2011687da132SEmmanuel Grumbach 				return i + 1;
2012687da132SEmmanuel Grumbach 	}
2013687da132SEmmanuel Grumbach 
2014687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
2015687da132SEmmanuel Grumbach 		rx_streams = 4;
2016687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
2017687da132SEmmanuel Grumbach 		rx_streams = 3;
2018687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
2019687da132SEmmanuel Grumbach 		rx_streams = 2;
2020687da132SEmmanuel Grumbach 	else
2021687da132SEmmanuel Grumbach 		rx_streams = 1;
2022687da132SEmmanuel Grumbach 
2023687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
2024687da132SEmmanuel Grumbach 		return rx_streams;
2025687da132SEmmanuel Grumbach 
2026687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
2027687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
2028687da132SEmmanuel Grumbach }
2029b7ffbd7eSJohannes Berg 
2030c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
2031c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
2032c9c5962bSJohannes Berg {
2033c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
2034c9c5962bSJohannes Berg 	struct ieee80211_local *local = sta->local;
2035c9c5962bSJohannes Berg 	int cpu;
2036c9c5962bSJohannes Berg 
2037c9c5962bSJohannes Berg 	if (!ieee80211_hw_check(&local->hw, USES_RSS))
2038c9c5962bSJohannes Berg 		return stats;
2039c9c5962bSJohannes Berg 
2040c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
2041c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
2042c9c5962bSJohannes Berg 
2043c9c5962bSJohannes Berg 		cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2044c9c5962bSJohannes Berg 
2045c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
2046c9c5962bSJohannes Berg 			stats = cpustats;
2047c9c5962bSJohannes Berg 	}
2048c9c5962bSJohannes Berg 
2049c9c5962bSJohannes Berg 	return stats;
2050c9c5962bSJohannes Berg }
2051c9c5962bSJohannes Berg 
205241cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
20534f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2054fbd6ff5cSJohannes Berg {
2055dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2056fbd6ff5cSJohannes Berg 
2057dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
20587f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
20594f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2060dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2061dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2062dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2063dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
20647f406cd1SJohannes Berg 		break;
20657f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
20664f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2067dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2068dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2069dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
20707f406cd1SJohannes Berg 		break;
20717f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2072fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2073fbd6ff5cSJohannes Berg 		u16 brate;
20744f6b1b3dSJohannes Berg 		unsigned int shift;
2075dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2076dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2077fbd6ff5cSJohannes Berg 
2078dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
2079dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
20804f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
20814f6b1b3dSJohannes Berg 			shift = 2;
20824f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
20834f6b1b3dSJohannes Berg 			shift = 1;
20844f6b1b3dSJohannes Berg 		else
20854f6b1b3dSJohannes Berg 			shift = 0;
2086fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
20877f406cd1SJohannes Berg 		break;
20887f406cd1SJohannes Berg 		}
208941cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
209041cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
209141cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
209241cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
209341cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
209441cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
209541cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
209641cbb0f5SLuca Coelho 		break;
2097fbd6ff5cSJohannes Berg 	}
20984f6b1b3dSJohannes Berg }
2099fbd6ff5cSJohannes Berg 
2100a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
21014f6b1b3dSJohannes Berg {
21026aa7de05SMark Rutland 	u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
21034f6b1b3dSJohannes Berg 
21044f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2105a17d93ffSBen Greear 		return -EINVAL;
2106a17d93ffSBen Greear 
21074f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2108a17d93ffSBen Greear 	return 0;
2109fbd6ff5cSJohannes Berg }
2110fbd6ff5cSJohannes Berg 
21110f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
21120f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
21130f9c5a61SJohannes Berg 			     int tid)
21140f9c5a61SJohannes Berg {
21150f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
21160f9c5a61SJohannes Berg 
21170f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
21180f9c5a61SJohannes Berg 		unsigned int start;
21190f9c5a61SJohannes Berg 
21200f9c5a61SJohannes Berg 		do {
21210f9c5a61SJohannes Berg 			start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
21220f9c5a61SJohannes Berg 			tidstats->rx_msdu = sta->rx_stats.msdu[tid];
21230f9c5a61SJohannes Berg 		} while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
21240f9c5a61SJohannes Berg 
21250f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
21260f9c5a61SJohannes Berg 	}
21270f9c5a61SJohannes Berg 
21280f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
21290f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
21300f9c5a61SJohannes Berg 		tidstats->tx_msdu = sta->tx_stats.msdu[tid];
21310f9c5a61SJohannes Berg 	}
21320f9c5a61SJohannes Berg 
21330f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
21340f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
21350f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
21360f9c5a61SJohannes Berg 		tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
21370f9c5a61SJohannes Berg 	}
21380f9c5a61SJohannes Berg 
21390f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
21400f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
21410f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
21420f9c5a61SJohannes Berg 		tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
21430f9c5a61SJohannes Berg 	}
21442fe4a29aSToke Høiland-Jørgensen 
21452fe4a29aSToke Høiland-Jørgensen 	if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
21462fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
21472fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
21482fe4a29aSToke Høiland-Jørgensen 
21492fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
21502fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
21512fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
21522fe4a29aSToke Høiland-Jørgensen 
21532fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
21542fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
21552fe4a29aSToke Høiland-Jørgensen 	}
21560f9c5a61SJohannes Berg }
21570f9c5a61SJohannes Berg 
2158c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2159c9c5962bSJohannes Berg {
2160c9c5962bSJohannes Berg 	unsigned int start;
2161c9c5962bSJohannes Berg 	u64 value;
2162c9c5962bSJohannes Berg 
2163c9c5962bSJohannes Berg 	do {
2164c9c5962bSJohannes Berg 		start = u64_stats_fetch_begin(&rxstats->syncp);
2165c9c5962bSJohannes Berg 		value = rxstats->bytes;
2166c9c5962bSJohannes Berg 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2167c9c5962bSJohannes Berg 
2168c9c5962bSJohannes Berg 	return value;
2169c9c5962bSJohannes Berg }
2170c9c5962bSJohannes Berg 
21710fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
21720fdf1493SJohannes Berg 		   bool tidstats)
2173b7ffbd7eSJohannes Berg {
2174b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2175b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2176b7ffbd7eSJohannes Berg 	u32 thr = 0;
2177c9c5962bSJohannes Berg 	int i, ac, cpu;
2178c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2179c9c5962bSJohannes Berg 
2180c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2181b7ffbd7eSJohannes Berg 
2182b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2183b7ffbd7eSJohannes Berg 
2184225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2185225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2186225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2187225b8189SJohannes Berg 	 */
2188225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2189225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2190225b8189SJohannes Berg 
21912b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
21922b9a7e1bSJohannes Berg 
2193a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2194a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2195a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2196a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
2197a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2198976bd9efSJohannes Berg 
2199976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2200976bd9efSJohannes Berg 		sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2201a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2202976bd9efSJohannes Berg 	}
2203b7ffbd7eSJohannes Berg 
220484b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2205e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2206b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
22072b9a7e1bSJohannes Berg 
2208a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2209a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2210b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
22112b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2212e5a9f8d0SJohannes Berg 			sinfo->tx_bytes += sta->tx_stats.bytes[ac];
2213a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2214b7ffbd7eSJohannes Berg 	}
22152b9a7e1bSJohannes Berg 
2216a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
22172b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
22182b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2219e5a9f8d0SJohannes Berg 			sinfo->tx_packets += sta->tx_stats.packets[ac];
2220a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
22212b9a7e1bSJohannes Berg 	}
22222b9a7e1bSJohannes Berg 
2223a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2224a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2225c9c5962bSJohannes Berg 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
22260f9c5a61SJohannes Berg 
2227c9c5962bSJohannes Berg 		if (sta->pcpu_rx_stats) {
2228c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2229c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2230c9c5962bSJohannes Berg 
2231c9c5962bSJohannes Berg 				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2232c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2233c9c5962bSJohannes Berg 			}
2234c9c5962bSJohannes Berg 		}
2235c9c5962bSJohannes Berg 
2236a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
22372b9a7e1bSJohannes Berg 	}
22382b9a7e1bSJohannes Berg 
2239a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2240e5a9f8d0SJohannes Berg 		sinfo->rx_packets = sta->rx_stats.packets;
2241c9c5962bSJohannes Berg 		if (sta->pcpu_rx_stats) {
2242c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2243c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2244c9c5962bSJohannes Berg 
2245c9c5962bSJohannes Berg 				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2246c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2247c9c5962bSJohannes Berg 			}
2248c9c5962bSJohannes Berg 		}
2249a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
22502b9a7e1bSJohannes Berg 	}
22512b9a7e1bSJohannes Berg 
2252a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2253e5a9f8d0SJohannes Berg 		sinfo->tx_retries = sta->status_stats.retry_count;
2254a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
22552b9a7e1bSJohannes Berg 	}
22562b9a7e1bSJohannes Berg 
2257a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2258e5a9f8d0SJohannes Berg 		sinfo->tx_failed = sta->status_stats.retry_failed;
2259a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
22602b9a7e1bSJohannes Berg 	}
22612b9a7e1bSJohannes Berg 
2262b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2263b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2264b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2265b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2266b4809e94SToke Høiland-Jørgensen 	}
2267b4809e94SToke Høiland-Jørgensen 
2268b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2269b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2270b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2271b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2272b4809e94SToke Høiland-Jørgensen 	}
2273b4809e94SToke Høiland-Jørgensen 
2274b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2275b4809e94SToke Høiland-Jørgensen 		sinfo->airtime_weight = sta->airtime_weight;
2276b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2277b4809e94SToke Høiland-Jørgensen 	}
2278b4809e94SToke Høiland-Jørgensen 
2279e5a9f8d0SJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_stats.dropped;
2280c9c5962bSJohannes Berg 	if (sta->pcpu_rx_stats) {
2281c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2282c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2283c9c5962bSJohannes Berg 
2284c9c5962bSJohannes Berg 			cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2285e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2286c9c5962bSJohannes Berg 		}
2287c9c5962bSJohannes Berg 	}
2288b7ffbd7eSJohannes Berg 
2289225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2290225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2291a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2292a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2293225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2294225b8189SJohannes Berg 	}
2295225b8189SJohannes Berg 
229630686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
229730686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2298a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2299c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2300a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2301b7ffbd7eSJohannes Berg 		}
23022b9a7e1bSJohannes Berg 
2303c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats &&
2304a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
230540d9a38aSJohannes Berg 			sinfo->signal_avg =
23060be6ed13SJohannes Berg 				-ewma_signal_read(&sta->rx_stats_avg.signal);
2307a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
23082b9a7e1bSJohannes Berg 		}
23092b9a7e1bSJohannes Berg 	}
23102b9a7e1bSJohannes Berg 
2311c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2312c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2313c9c5962bSJohannes Berg 	 * pcpu statistics
2314c9c5962bSJohannes Berg 	 */
2315c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2316a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2317a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2318a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2319c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats)
2320a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2321b7ffbd7eSJohannes Berg 
2322c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2323c9c5962bSJohannes Berg 
2324b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2325e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2326c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2327b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
23280be6ed13SJohannes Berg 				-ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
2329b7ffbd7eSJohannes Berg 		}
2330b7ffbd7eSJohannes Berg 	}
2331b7ffbd7eSJohannes Berg 
2332a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
2333e5a9f8d0SJohannes Berg 		sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2334e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2335a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
23362b9a7e1bSJohannes Berg 	}
23372b9a7e1bSJohannes Berg 
2338a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
2339a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2340a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
23412b9a7e1bSJohannes Berg 	}
2342b7ffbd7eSJohannes Berg 
23430fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
23446af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
23456af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
23468689c051SArend van Spriel 	}
234779c892b8SJohannes Berg 
2348b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2349b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2350a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2351a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2352a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2353a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2354a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2355dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
2356dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE);
2357b7ffbd7eSJohannes Berg 
2358433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2359433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2360433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2361b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2362a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2363433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2364b7ffbd7eSJohannes Berg 		}
2365433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2366433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2367433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2368dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
2369b7ffbd7eSJohannes Berg #endif
2370b7ffbd7eSJohannes Berg 	}
2371b7ffbd7eSJohannes Berg 
2372b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2373b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2374b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2375b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2376b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2377b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2378b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2379785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2380b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2381b7ffbd7eSJohannes Berg 
2382b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2383b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2384b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2385b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2386b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2387b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2388b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2389b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2390b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2391b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2392b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2393b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2394a74a8c84SJohannes Berg 	if (sta->sta.wme)
2395b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2396b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2397b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2398b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2399b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2400b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2401b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2402b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2403b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2404b7ffbd7eSJohannes Berg 
24053b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
24063b17fbf8SMaxim Altshul 
24073b17fbf8SMaxim Altshul 	if (thr != 0) {
2408a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
24093b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
24103b17fbf8SMaxim Altshul 	}
2411a78b26ffSVenkateswara Naralasetty 
2412a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2413a78b26ffSVenkateswara Naralasetty 	    sta->status_stats.ack_signal_filled) {
2414a78b26ffSVenkateswara Naralasetty 		sinfo->ack_signal = sta->status_stats.last_ack_signal;
2415a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2416a78b26ffSVenkateswara Naralasetty 	}
2417cc60dbbfSBalaji Pothunoori 
24189c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
24199c06602bSBalaji Pothunoori 	    sta->status_stats.ack_signal_filled) {
2420cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2421cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2422cc60dbbfSBalaji Pothunoori 				&sta->status_stats.avg_ack_signal);
2423cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
24249c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2425cc60dbbfSBalaji Pothunoori 	}
2426ab60633cSNarayanraddi Masti 
2427ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2428ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2429ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2430ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2431ab60633cSNarayanraddi Masti 	}
24323b17fbf8SMaxim Altshul }
24333b17fbf8SMaxim Altshul 
24343b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
24353b17fbf8SMaxim Altshul {
24363b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
24373b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
24383b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
24393b17fbf8SMaxim Altshul 	u32 thr = 0;
24403b17fbf8SMaxim Altshul 
24413b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
24423b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
24433b17fbf8SMaxim Altshul 
2444b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2445b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2446b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2447b7ffbd7eSJohannes Berg 	else
24484fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2449b7ffbd7eSJohannes Berg 
24503b17fbf8SMaxim Altshul 	return thr;
2451b7ffbd7eSJohannes Berg }
2452b8da6b6aSJohannes Berg 
2453b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2454b8da6b6aSJohannes Berg {
2455c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2456c9c5962bSJohannes Berg 
2457c9c5962bSJohannes Berg 	if (time_after(stats->last_rx, sta->status_stats.last_ack))
2458c9c5962bSJohannes Berg 		return stats->last_rx;
2459b8da6b6aSJohannes Berg 	return sta->status_stats.last_ack;
2460b8da6b6aSJohannes Berg }
2461484a54c2SToke Høiland-Jørgensen 
2462484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2463484a54c2SToke Høiland-Jørgensen {
2464484a54c2SToke Høiland-Jørgensen 	if (!sta->sdata->local->ops->wake_tx_queue)
2465484a54c2SToke Høiland-Jørgensen 		return;
2466484a54c2SToke Høiland-Jørgensen 
2467484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2468484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2469484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2470484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2471484a54c2SToke Høiland-Jørgensen 	} else {
2472484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2473484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2474484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2475484a54c2SToke Høiland-Jørgensen 	}
2476484a54c2SToke Høiland-Jørgensen }
2477484a54c2SToke Høiland-Jørgensen 
2478484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2479484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2480484a54c2SToke Høiland-Jørgensen {
2481484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2482484a54c2SToke Høiland-Jørgensen 
2483484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2484484a54c2SToke Høiland-Jørgensen }
2485