xref: /openbmc/linux/net/mac80211/sta_info.c (revision ab60633c7136c300f15a390f3469d7c4be15a055)
1f0706e82SJiri Benc /*
2f0706e82SJiri Benc  * Copyright 2002-2005, Instant802 Networks, Inc.
3f0706e82SJiri Benc  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
4d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
5dcba665bSJohannes Berg  * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
6a403f3bfSJohannes Berg  * Copyright (C) 2018 Intel Corporation
7f0706e82SJiri Benc  *
8f0706e82SJiri Benc  * This program is free software; you can redistribute it and/or modify
9f0706e82SJiri Benc  * it under the terms of the GNU General Public License version 2 as
10f0706e82SJiri Benc  * published by the Free Software Foundation.
11f0706e82SJiri Benc  */
12f0706e82SJiri Benc 
13f0706e82SJiri Benc #include <linux/module.h>
14f0706e82SJiri Benc #include <linux/init.h>
15888d04dfSFelix Fietkau #include <linux/etherdevice.h>
16f0706e82SJiri Benc #include <linux/netdevice.h>
17f0706e82SJiri Benc #include <linux/types.h>
18f0706e82SJiri Benc #include <linux/slab.h>
19f0706e82SJiri Benc #include <linux/skbuff.h>
20f0706e82SJiri Benc #include <linux/if_arp.h>
210d174406SJohannes Berg #include <linux/timer.h>
22d0709a65SJohannes Berg #include <linux/rtnetlink.h>
23f0706e82SJiri Benc 
24484a54c2SToke Høiland-Jørgensen #include <net/codel.h>
25f0706e82SJiri Benc #include <net/mac80211.h>
26f0706e82SJiri Benc #include "ieee80211_i.h"
2724487981SJohannes Berg #include "driver-ops.h"
282c8dccc7SJohannes Berg #include "rate.h"
29f0706e82SJiri Benc #include "sta_info.h"
30e9f207f0SJiri Benc #include "debugfs_sta.h"
31ee385855SLuis Carlos Cobo #include "mesh.h"
32ce662b44SJohannes Berg #include "wme.h"
33f0706e82SJiri Benc 
34d0709a65SJohannes Berg /**
35d0709a65SJohannes Berg  * DOC: STA information lifetime rules
36d0709a65SJohannes Berg  *
37d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
38d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
39d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
40d0709a65SJohannes Berg  *
4134e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
4234e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4334e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4434e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4534e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4634e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4734e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4834e89507SJohannes Berg  * encryption keys.
4993e5deb1SJohannes Berg  *
5093e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
5193e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
52d0709a65SJohannes Berg  *
5334e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
547e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
557e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5625985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5734e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5825985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5934e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
607e189a12SLuis R. Rodriguez  *
6134e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
6234e89507SJohannes Berg  * calls are available.
63d0709a65SJohannes Berg  *
6434e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6534e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6634e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6734e89507SJohannes Berg  * freed before they are done using it.
68d0709a65SJohannes Berg  */
69f0706e82SJiri Benc 
707bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
717bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
72caf22d31SJohannes Berg 	.automatic_shrinking = true,
737bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
74ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
757bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
76ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
777bedd0cfSJohannes Berg };
787bedd0cfSJohannes Berg 
794d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
80be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
81f0706e82SJiri Benc 			     struct sta_info *sta)
82f0706e82SJiri Benc {
8383e7e4ceSHerbert Xu 	return rhltable_remove(&local->sta_hash, &sta->hash_node,
847bedd0cfSJohannes Berg 			       sta_rht_params);
85f0706e82SJiri Benc }
86f0706e82SJiri Benc 
875108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
88b22cfcfcSEliad Peller {
89b22cfcfcSEliad Peller 	int ac, i;
90b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
91b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
92b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
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 
350d0709a65SJohannes Berg 	sta->local = local;
351d0709a65SJohannes Berg 	sta->sdata = sdata;
352e5a9f8d0SJohannes Berg 	sta->rx_stats.last_rx = jiffies;
353f0706e82SJiri Benc 
3540f9c5a61SJohannes Berg 	u64_stats_init(&sta->rx_stats.syncp);
3550f9c5a61SJohannes Berg 
35671ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
35771ec375cSJohannes Berg 
358b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
359b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
360b6da911bSLiad Kaufman 
36184b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
3620be6ed13SJohannes Berg 	ewma_signal_init(&sta->rx_stats_avg.signal);
363cc60dbbfSBalaji Pothunoori 	ewma_avg_signal_init(&sta->status_stats.avg_ack_signal);
3640be6ed13SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
3650be6ed13SJohannes Berg 		ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
366541a45a1SBruno Randolf 
367ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
368ba8c3d6fSFelix Fietkau 		void *txq_data;
369ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
370ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
371ba8c3d6fSFelix Fietkau 
372ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
373ba8c3d6fSFelix Fietkau 		if (!txq_data)
374ba8c3d6fSFelix Fietkau 			goto free;
375ba8c3d6fSFelix Fietkau 
376ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
377ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
378ba8c3d6fSFelix Fietkau 
379adf8ed01SJohannes Berg 			/* might not do anything for the bufferable MMPDU TXQ */
380fa962b92SMichal Kazior 			ieee80211_txq_init(sdata, sta, txq, i);
381abfbc3afSJohannes Berg 		}
382ba8c3d6fSFelix Fietkau 	}
383ba8c3d6fSFelix Fietkau 
384ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
385ba8c3d6fSFelix Fietkau 		goto free_txq;
386f0706e82SJiri Benc 
387b4809e94SToke Høiland-Jørgensen 	sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
388b4809e94SToke Høiland-Jørgensen 
389948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
390948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
391948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
392b4809e94SToke Høiland-Jørgensen 		sta->airtime[i].deficit = sta->airtime_weight;
393948d887dSJohannes Berg 	}
39473651ee6SJohannes Berg 
3955a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3964be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
397cccaec98SSenthil Balasubramanian 
398af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
399687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
400687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
40121a8e9ddSMohammed Shafi Shajakhan 		struct ieee80211_supported_band *sband;
40221a8e9ddSMohammed Shafi Shajakhan 		u8 smps;
40321a8e9ddSMohammed Shafi Shajakhan 
40421a8e9ddSMohammed Shafi Shajakhan 		sband = ieee80211_get_sband(sdata);
40521a8e9ddSMohammed Shafi Shajakhan 		if (!sband)
40621a8e9ddSMohammed Shafi Shajakhan 			goto free_txq;
40721a8e9ddSMohammed Shafi Shajakhan 
40821a8e9ddSMohammed Shafi Shajakhan 		smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
409687da132SEmmanuel Grumbach 			IEEE80211_HT_CAP_SM_PS_SHIFT;
410687da132SEmmanuel Grumbach 		/*
411687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
412687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
413687da132SEmmanuel Grumbach 		 */
414687da132SEmmanuel Grumbach 		switch (smps) {
415687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
416687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
417687da132SEmmanuel Grumbach 			break;
418687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
419687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
420687da132SEmmanuel Grumbach 			break;
421687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
422687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
423687da132SEmmanuel Grumbach 			break;
424687da132SEmmanuel Grumbach 		default:
425687da132SEmmanuel Grumbach 			WARN_ON(1);
426687da132SEmmanuel Grumbach 		}
427687da132SEmmanuel Grumbach 	}
428af0ed69bSJohannes Berg 
4296e0456b5SFelix Fietkau 	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
4306e0456b5SFelix Fietkau 
431484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
432484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
433484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
434484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
435484a54c2SToke Høiland-Jørgensen 
436bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
437ef04a297SJohannes Berg 
438abfbc3afSJohannes Berg 	return sta;
439ba8c3d6fSFelix Fietkau 
440ba8c3d6fSFelix Fietkau free_txq:
441ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
442ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
443ba8c3d6fSFelix Fietkau free:
444d78d9ee9SSara Sharon 	free_percpu(sta->pcpu_rx_stats);
445433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
446433f5bc1SJohannes Berg 	kfree(sta->mesh);
447433f5bc1SJohannes Berg #endif
448ba8c3d6fSFelix Fietkau 	kfree(sta);
449ba8c3d6fSFelix Fietkau 	return NULL;
45073651ee6SJohannes Berg }
45173651ee6SJohannes Berg 
4528c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
45334e89507SJohannes Berg {
45434e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
45534e89507SJohannes Berg 
45603e4497eSJohannes Berg 	/*
45703e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
45803e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
45903e4497eSJohannes Berg 	 * and another CPU turns off the net device.
46003e4497eSJohannes Berg 	 */
4618c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4628c71df7aSGuy Eilam 		return -ENETDOWN;
46303e4497eSJohannes Berg 
464b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4658c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4668c71df7aSGuy Eilam 		return -EINVAL;
4678c71df7aSGuy Eilam 
46883e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
46983e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
47083e7e4ceSHerbert Xu 	 * for correctness.
47131104891SJohannes Berg 	 */
47231104891SJohannes Berg 	rcu_read_lock();
47331104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
47431104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
47531104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
47631104891SJohannes Berg 		rcu_read_unlock();
47731104891SJohannes Berg 		return -ENOTUNIQ;
47831104891SJohannes Berg 	}
47931104891SJohannes Berg 	rcu_read_unlock();
48031104891SJohannes Berg 
4818c71df7aSGuy Eilam 	return 0;
48293e5deb1SJohannes Berg }
48344213b5eSJohannes Berg 
484f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
485f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
486f09603a2SJohannes Berg 				     struct sta_info *sta)
487f09603a2SJohannes Berg {
488f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
489f09603a2SJohannes Berg 	int err = 0;
490f09603a2SJohannes Berg 
491f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
492f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
493f09603a2SJohannes Berg 		if (err)
494f09603a2SJohannes Berg 			break;
495f09603a2SJohannes Berg 	}
496f09603a2SJohannes Berg 
497f09603a2SJohannes Berg 	if (!err) {
498a4ec45a4SJohannes Berg 		/*
499a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
500a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
501a4ec45a4SJohannes Berg 		 */
502a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
503f09603a2SJohannes Berg 			sta->uploaded = true;
504f09603a2SJohannes Berg 		return 0;
505f09603a2SJohannes Berg 	}
506f09603a2SJohannes Berg 
507f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
508bdcbd8e0SJohannes Berg 		sdata_info(sdata,
509bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
510bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
511f09603a2SJohannes Berg 		err = 0;
512f09603a2SJohannes Berg 	}
513f09603a2SJohannes Berg 
514f09603a2SJohannes Berg 	/* unwind on error */
515f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
516f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
517f09603a2SJohannes Berg 
518f09603a2SJohannes Berg 	return err;
519f09603a2SJohannes Berg }
520f09603a2SJohannes Berg 
521d405fd8cSGregory Greenman static void
522d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
523d405fd8cSGregory Greenman {
524d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
525d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
526d405fd8cSGregory Greenman 	struct sta_info *sta;
527d405fd8cSGregory Greenman 
528d405fd8cSGregory Greenman 	rcu_read_lock();
529d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
530d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
531d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
532d405fd8cSGregory Greenman 			continue;
533d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
534d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
535d405fd8cSGregory Greenman 			break;
536d405fd8cSGregory Greenman 		}
537d405fd8cSGregory Greenman 	}
538d405fd8cSGregory Greenman 	rcu_read_unlock();
539d405fd8cSGregory Greenman 
540d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
541d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
542d405fd8cSGregory Greenman 		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
543d405fd8cSGregory Greenman 	}
544d405fd8cSGregory Greenman }
545d405fd8cSGregory Greenman 
54634e89507SJohannes Berg /*
5478c71df7aSGuy Eilam  * should be called with sta_mtx locked
5488c71df7aSGuy Eilam  * this function replaces the mutex lock
5498c71df7aSGuy Eilam  * with a RCU lock
5508c71df7aSGuy Eilam  */
5514d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
5528c71df7aSGuy Eilam {
5538c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
5548c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
5550c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
5568c71df7aSGuy Eilam 	int err = 0;
5578c71df7aSGuy Eilam 
5588c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
5598c71df7aSGuy Eilam 
5607852e361SJohannes Berg 	/* check if STA exists already */
5617852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
5624d33960bSJohannes Berg 		err = -EEXIST;
5634d33960bSJohannes Berg 		goto out_err;
56434e89507SJohannes Berg 	}
56534e89507SJohannes Berg 
5660c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
5670c2e3842SKoen Vandeputte 	if (!sinfo) {
5680c2e3842SKoen Vandeputte 		err = -ENOMEM;
5690c2e3842SKoen Vandeputte 		goto out_err;
5700c2e3842SKoen Vandeputte 	}
5710c2e3842SKoen Vandeputte 
5724d33960bSJohannes Berg 	local->num_sta++;
5734d33960bSJohannes Berg 	local->sta_generation++;
5744d33960bSJohannes Berg 	smp_mb();
5754d33960bSJohannes Berg 
5765108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
5775108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5785108ca82SJohannes Berg 
5794d33960bSJohannes Berg 	/* make the station visible */
58062b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
58162b14b24SJohannes Berg 	if (err)
58262b14b24SJohannes Berg 		goto out_drop_sta;
5834d33960bSJohannes Berg 
5842bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
58583d5cc01SJohannes Berg 
5865108ca82SJohannes Berg 	/* notify driver */
5875108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
5885108ca82SJohannes Berg 	if (err)
5895108ca82SJohannes Berg 		goto out_remove;
5905108ca82SJohannes Berg 
59183d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
592d405fd8cSGregory Greenman 
593d405fd8cSGregory Greenman 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
594d405fd8cSGregory Greenman 		ieee80211_recalc_min_chandef(sta->sdata);
595d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps)
596d405fd8cSGregory Greenman 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
597d405fd8cSGregory Greenman 	}
598d405fd8cSGregory Greenman 
5995108ca82SJohannes Berg 	/* accept BA sessions now */
6005108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
6014d33960bSJohannes Berg 
6024d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
6034d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
6044d33960bSJohannes Berg 
6050ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
6060ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
6070ef049dcSArnd Bergmann 	kfree(sinfo);
608d0709a65SJohannes Berg 
609bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
610f0706e82SJiri Benc 
61134e89507SJohannes Berg 	/* move reference to rcu-protected */
61234e89507SJohannes Berg 	rcu_read_lock();
61334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
614e9f207f0SJiri Benc 
61573651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
61673651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
61773651ee6SJohannes Berg 
61873651ee6SJohannes Berg 	return 0;
6195108ca82SJohannes Berg  out_remove:
6205108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
6215108ca82SJohannes Berg 	list_del_rcu(&sta->list);
62262b14b24SJohannes Berg  out_drop_sta:
6235108ca82SJohannes Berg 	local->num_sta--;
6245108ca82SJohannes Berg 	synchronize_net();
6255108ca82SJohannes Berg 	__cleanup_single_sta(sta);
6264d33960bSJohannes Berg  out_err:
6274d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
628ea32f065SSudip Mukherjee 	kfree(sinfo);
6294d33960bSJohannes Berg 	rcu_read_lock();
6304d33960bSJohannes Berg 	return err;
6318c71df7aSGuy Eilam }
6328c71df7aSGuy Eilam 
6338c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
6348c71df7aSGuy Eilam {
6358c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
636308f7fcfSZhao, Gang 	int err;
6378c71df7aSGuy Eilam 
6384d33960bSJohannes Berg 	might_sleep();
6394d33960bSJohannes Berg 
64031104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
64131104891SJohannes Berg 
6428c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
6438c71df7aSGuy Eilam 	if (err) {
64431104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
6458c71df7aSGuy Eilam 		rcu_read_lock();
6468c71df7aSGuy Eilam 		goto out_free;
6478c71df7aSGuy Eilam 	}
6488c71df7aSGuy Eilam 
6494d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
6508c71df7aSGuy Eilam 	if (err)
651004c872eSJohannes Berg 		goto out_free;
652d0709a65SJohannes Berg 
653004c872eSJohannes Berg 	return 0;
65493e5deb1SJohannes Berg  out_free:
655d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
65693e5deb1SJohannes Berg 	return err;
657f0706e82SJiri Benc }
658f0706e82SJiri Benc 
65934e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
66034e89507SJohannes Berg {
66134e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
66234e89507SJohannes Berg 
66334e89507SJohannes Berg 	rcu_read_unlock();
66434e89507SJohannes Berg 
66534e89507SJohannes Berg 	return err;
66634e89507SJohannes Berg }
66734e89507SJohannes Berg 
668d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
669004c872eSJohannes Berg {
670004c872eSJohannes Berg 	/*
671004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
672004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
673004c872eSJohannes Berg 	 */
674d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
675004c872eSJohannes Berg }
676004c872eSJohannes Berg 
677d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
678004c872eSJohannes Berg {
679004c872eSJohannes Berg 	/*
680004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
681004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
682004c872eSJohannes Berg 	 */
683d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
684004c872eSJohannes Berg }
685004c872eSJohannes Berg 
6863d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
6873d5839b6SIlan Peer {
6883d5839b6SIlan Peer 	/*
6893d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
6903d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
6913d5839b6SIlan Peer 	 */
6923d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
6933d5839b6SIlan Peer }
6943d5839b6SIlan Peer 
695948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
696004c872eSJohannes Berg {
697948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
698948d887dSJohannes Berg 	switch (ac) {
699948d887dSJohannes Berg 	case IEEE80211_AC_VO:
700948d887dSJohannes Berg 		return BIT(6) | BIT(7);
701948d887dSJohannes Berg 	case IEEE80211_AC_VI:
702948d887dSJohannes Berg 		return BIT(4) | BIT(5);
703948d887dSJohannes Berg 	case IEEE80211_AC_BE:
704948d887dSJohannes Berg 		return BIT(0) | BIT(3);
705948d887dSJohannes Berg 	case IEEE80211_AC_BK:
706948d887dSJohannes Berg 		return BIT(1) | BIT(2);
707948d887dSJohannes Berg 	default:
708948d887dSJohannes Berg 		WARN_ON(1);
709948d887dSJohannes Berg 		return 0;
710d0709a65SJohannes Berg 	}
711004c872eSJohannes Berg }
712004c872eSJohannes Berg 
7139b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
714004c872eSJohannes Berg {
715c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
716d012a605SMarco Porsch 	struct ps_data *ps;
717948d887dSJohannes Berg 	bool indicate_tim = false;
718948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
719948d887dSJohannes Berg 	int ac;
720a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
721004c872eSJohannes Berg 
722d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
723d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
724c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
725c868cb35SJohannes Berg 			return;
7263e122be0SJohannes Berg 
727d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
7283f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
7293f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
7303f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
7313f52b7e3SMarco Porsch #endif
732d012a605SMarco Porsch 	} else {
733d012a605SMarco Porsch 		return;
734d012a605SMarco Porsch 	}
735d012a605SMarco Porsch 
736c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
737d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
738c868cb35SJohannes Berg 		return;
739004c872eSJohannes Berg 
740c868cb35SJohannes Berg 	if (sta->dead)
741c868cb35SJohannes Berg 		goto done;
7423e122be0SJohannes Berg 
743948d887dSJohannes Berg 	/*
744948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
745948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
746948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
747948d887dSJohannes Berg 	 * non-enabled ones.
748948d887dSJohannes Berg 	 */
749948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
750948d887dSJohannes Berg 		ignore_for_tim = 0;
751948d887dSJohannes Berg 
7529b7a86f3SJohannes Berg 	if (ignore_pending)
7539b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
7549b7a86f3SJohannes Berg 
755948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
756948d887dSJohannes Berg 		unsigned long tids;
757948d887dSJohannes Berg 
758f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
759948d887dSJohannes Berg 			continue;
760948d887dSJohannes Berg 
761948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
762948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
763948d887dSJohannes Berg 		if (indicate_tim)
764948d887dSJohannes Berg 			break;
765948d887dSJohannes Berg 
766948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
767948d887dSJohannes Berg 
768948d887dSJohannes Berg 		indicate_tim |=
769948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
770ba8c3d6fSFelix Fietkau 		indicate_tim |=
771ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
772004c872eSJohannes Berg 	}
773004c872eSJohannes Berg 
774c868cb35SJohannes Berg  done:
77565f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
776004c872eSJohannes Berg 
7773d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
7783d5839b6SIlan Peer 		goto out_unlock;
7793d5839b6SIlan Peer 
780948d887dSJohannes Berg 	if (indicate_tim)
781d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
782c868cb35SJohannes Berg 	else
783d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
7843e122be0SJohannes Berg 
7859b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
786c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
787948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
788c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
789004c872eSJohannes Berg 	}
790004c872eSJohannes Berg 
7913d5839b6SIlan Peer out_unlock:
79265f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
793004c872eSJohannes Berg }
794004c872eSJohannes Berg 
7959b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
7969b7a86f3SJohannes Berg {
7979b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
7989b7a86f3SJohannes Berg }
7999b7a86f3SJohannes Berg 
800cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
801f0706e82SJiri Benc {
802e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
803f0706e82SJiri Benc 	int timeout;
804f0706e82SJiri Benc 
805f0706e82SJiri Benc 	if (!skb)
806cd0b8d89SJohannes Berg 		return false;
807f0706e82SJiri Benc 
808e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
809f0706e82SJiri Benc 
810f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
81157c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
81257c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
81357c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
814f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
815f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
816e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
817f0706e82SJiri Benc }
818f0706e82SJiri Benc 
819f0706e82SJiri Benc 
820948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
821948d887dSJohannes Berg 						struct sta_info *sta, int ac)
822f0706e82SJiri Benc {
823f0706e82SJiri Benc 	unsigned long flags;
824f0706e82SJiri Benc 	struct sk_buff *skb;
825f0706e82SJiri Benc 
82660750397SJohannes Berg 	/*
82760750397SJohannes Berg 	 * First check for frames that should expire on the filtered
82860750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
82960750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
83060750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
83160750397SJohannes Berg 	 * total_ps_buffered counter.
83260750397SJohannes Berg 	 */
833f0706e82SJiri Benc 	for (;;) {
834948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
835948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
83657c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
837948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
838836341a7SJohannes Berg 		else
839f0706e82SJiri Benc 			skb = NULL;
840948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
841f0706e82SJiri Benc 
84260750397SJohannes Berg 		/*
84360750397SJohannes Berg 		 * Frames are queued in order, so if this one
84460750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
84560750397SJohannes Berg 		 * we actually reached the end of the queue we
84660750397SJohannes Berg 		 * also need to stop, of course.
84760750397SJohannes Berg 		 */
84860750397SJohannes Berg 		if (!skb)
84960750397SJohannes Berg 			break;
850d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
85160750397SJohannes Berg 	}
85260750397SJohannes Berg 
85360750397SJohannes Berg 	/*
85460750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
85560750397SJohannes Berg 	 * only find something if the filtered queue was emptied
85660750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
85760750397SJohannes Berg 	 * buffered frames.
85860750397SJohannes Berg 	 */
859f0706e82SJiri Benc 	for (;;) {
860948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
861948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
862f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
863948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
864f0706e82SJiri Benc 		else
865f0706e82SJiri Benc 			skb = NULL;
866948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
867f0706e82SJiri Benc 
86860750397SJohannes Berg 		/*
86960750397SJohannes Berg 		 * frames are queued in order, so if this one
87060750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
87160750397SJohannes Berg 		 * the queue) we can stop testing
87260750397SJohannes Berg 		 */
873836341a7SJohannes Berg 		if (!skb)
874836341a7SJohannes Berg 			break;
875836341a7SJohannes Berg 
876f0706e82SJiri Benc 		local->total_ps_buffered--;
877bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
878bdcbd8e0SJohannes Berg 		       sta->sta.addr);
879d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
880f0706e82SJiri Benc 	}
8813393a608SJuuso Oikarinen 
88260750397SJohannes Berg 	/*
88360750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
88460750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
88560750397SJohannes Berg 	 * frames.
88660750397SJohannes Berg 	 */
88760750397SJohannes Berg 	sta_info_recalc_tim(sta);
88860750397SJohannes Berg 
88960750397SJohannes Berg 	/*
89060750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
89160750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
89260750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
89360750397SJohannes Berg 	 */
894948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
895948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
896948d887dSJohannes Berg }
897948d887dSJohannes Berg 
898948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
899948d887dSJohannes Berg 					     struct sta_info *sta)
900948d887dSJohannes Berg {
901948d887dSJohannes Berg 	bool have_buffered = false;
902948d887dSJohannes Berg 	int ac;
903948d887dSJohannes Berg 
9043f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
9053f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
9063f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
907948d887dSJohannes Berg 		return false;
908948d887dSJohannes Berg 
909948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
910948d887dSJohannes Berg 		have_buffered |=
911948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
912948d887dSJohannes Berg 
913948d887dSJohannes Berg 	return have_buffered;
914f0706e82SJiri Benc }
915f0706e82SJiri Benc 
916d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
91734e89507SJohannes Berg {
91834e89507SJohannes Berg 	struct ieee80211_local *local;
91934e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
9206d10e46bSJohannes Berg 	int ret;
92134e89507SJohannes Berg 
92234e89507SJohannes Berg 	might_sleep();
92334e89507SJohannes Berg 
92434e89507SJohannes Berg 	if (!sta)
92534e89507SJohannes Berg 		return -ENOENT;
92634e89507SJohannes Berg 
92734e89507SJohannes Berg 	local = sta->local;
92834e89507SJohannes Berg 	sdata = sta->sdata;
92934e89507SJohannes Berg 
93083d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
93183d5cc01SJohannes Berg 
932098a6070SJohannes Berg 	/*
933098a6070SJohannes Berg 	 * Before removing the station from the driver and
934098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
935098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
936098a6070SJohannes Berg 	 * will be sufficient.
937098a6070SJohannes Berg 	 */
938c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
939c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
940098a6070SJohannes Berg 
941f59374ebSSara Sharon 	/*
942f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
943f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
944f59374ebSSara Sharon 	 * all such frames to be processed.
945f59374ebSSara Sharon 	 */
946f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
947f59374ebSSara Sharon 
94834e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
949b01711beSJohannes Berg 	if (WARN_ON(ret))
95034e89507SJohannes Berg 		return ret;
95134e89507SJohannes Berg 
952a7a6bdd0SArik Nemtsov 	/*
953a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
954a7a6bdd0SArik Nemtsov 	 * removal.
955a7a6bdd0SArik Nemtsov 	 */
956a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
957a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
958a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
959a7a6bdd0SArik Nemtsov 	}
960a7a6bdd0SArik Nemtsov 
961794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
962ef044763SEliad Peller 	sta->removed = true;
9634d33960bSJohannes Berg 
9646a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
9656a9d1b91SJohannes Berg 
966a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
967a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
968a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
969a710c816SJohannes Berg 
970d778207bSJohannes Berg 	return 0;
971d778207bSJohannes Berg }
972d778207bSJohannes Berg 
973d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
974d778207bSJohannes Berg {
975d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
976d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
9770ef049dcSArnd Bergmann 	struct station_info *sinfo;
978d778207bSJohannes Berg 	int ret;
979d778207bSJohannes Berg 
980d778207bSJohannes Berg 	/*
981d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
982d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
983d778207bSJohannes Berg 	 */
984d778207bSJohannes Berg 
985d778207bSJohannes Berg 	might_sleep();
986d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
987d778207bSJohannes Berg 
988c8782078SJohannes Berg 	/* now keys can no longer be reached */
9896d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
99034e89507SJohannes Berg 
9919b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
9929b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
9939b7a86f3SJohannes Berg 
99434e89507SJohannes Berg 	sta->dead = true;
99534e89507SJohannes Berg 
99634e89507SJohannes Berg 	local->num_sta--;
99734e89507SJohannes Berg 	local->sta_generation++;
99834e89507SJohannes Berg 
99983d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
1000f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
1001f09603a2SJohannes Berg 		if (ret) {
100283d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
100383d5cc01SJohannes Berg 			break;
100483d5cc01SJohannes Berg 		}
100583d5cc01SJohannes Berg 	}
1006d9a7ddb0SJohannes Berg 
1007f09603a2SJohannes Berg 	if (sta->uploaded) {
1008f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1009f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1010f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1011f09603a2SJohannes Berg 	}
101234e89507SJohannes Berg 
1013bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1014bdcbd8e0SJohannes Berg 
10150ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
10160ef049dcSArnd Bergmann 	if (sinfo)
10170fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
10180ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
10190ef049dcSArnd Bergmann 	kfree(sinfo);
1020ec15e68bSJouni Malinen 
102134e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
102234e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
102334e89507SJohannes Berg 
1024d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1025d778207bSJohannes Berg }
1026d778207bSJohannes Berg 
1027d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1028d778207bSJohannes Berg {
1029d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1030d778207bSJohannes Berg 
1031d778207bSJohannes Berg 	if (err)
1032d778207bSJohannes Berg 		return err;
1033d778207bSJohannes Berg 
1034d778207bSJohannes Berg 	synchronize_net();
1035d778207bSJohannes Berg 
1036d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
103734e89507SJohannes Berg 
103834e89507SJohannes Berg 	return 0;
103934e89507SJohannes Berg }
104034e89507SJohannes Berg 
104134e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
104234e89507SJohannes Berg {
104334e89507SJohannes Berg 	struct sta_info *sta;
104434e89507SJohannes Berg 	int ret;
104534e89507SJohannes Berg 
104634e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
10477852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
104834e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
104934e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
105034e89507SJohannes Berg 
105134e89507SJohannes Berg 	return ret;
105234e89507SJohannes Berg }
105334e89507SJohannes Berg 
105434e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
105534e89507SJohannes Berg 			      const u8 *addr)
105634e89507SJohannes Berg {
105734e89507SJohannes Berg 	struct sta_info *sta;
105834e89507SJohannes Berg 	int ret;
105934e89507SJohannes Berg 
106034e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
10617852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
106234e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
106334e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
106434e89507SJohannes Berg 
106534e89507SJohannes Berg 	return ret;
106634e89507SJohannes Berg }
1067f0706e82SJiri Benc 
106834f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1069f0706e82SJiri Benc {
107034f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1071f0706e82SJiri Benc 	struct sta_info *sta;
10723393a608SJuuso Oikarinen 	bool timer_needed = false;
1073f0706e82SJiri Benc 
1074d0709a65SJohannes Berg 	rcu_read_lock();
1075d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
10763393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
10773393a608SJuuso Oikarinen 			timer_needed = true;
1078d0709a65SJohannes Berg 	rcu_read_unlock();
1079f0706e82SJiri Benc 
10805bb644a0SJohannes Berg 	if (local->quiescing)
10815bb644a0SJohannes Berg 		return;
10825bb644a0SJohannes Berg 
10833393a608SJuuso Oikarinen 	if (!timer_needed)
10843393a608SJuuso Oikarinen 		return;
10853393a608SJuuso Oikarinen 
108626d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
108726d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1088f0706e82SJiri Benc }
1089f0706e82SJiri Benc 
10907bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
10917bedd0cfSJohannes Berg {
10927bedd0cfSJohannes Berg 	int err;
10937bedd0cfSJohannes Berg 
109483e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
10957bedd0cfSJohannes Berg 	if (err)
10967bedd0cfSJohannes Berg 		return err;
10977bedd0cfSJohannes Berg 
10984d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
109934e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1100f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1101f0706e82SJiri Benc 
110234f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
11037bedd0cfSJohannes Berg 	return 0;
1104f0706e82SJiri Benc }
1105f0706e82SJiri Benc 
1106f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1107f0706e82SJiri Benc {
1108a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
110983e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1110f0706e82SJiri Benc }
1111f0706e82SJiri Benc 
1112051007d9SJohannes Berg 
1113e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1114f0706e82SJiri Benc {
1115b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1116f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1117d778207bSJohannes Berg 	LIST_HEAD(free_list);
111844213b5eSJohannes Berg 	int ret = 0;
1119f0706e82SJiri Benc 
1120d0709a65SJohannes Berg 	might_sleep();
1121d0709a65SJohannes Berg 
1122e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1123e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1124e716251dSJohannes Berg 
112534e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
112634e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1127e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1128e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1129d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1130d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
113134316837SJohannes Berg 			ret++;
113234316837SJohannes Berg 		}
113334e89507SJohannes Berg 	}
1134d778207bSJohannes Berg 
1135d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1136d778207bSJohannes Berg 		synchronize_net();
1137d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1138d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1139d778207bSJohannes Berg 	}
114034e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
114144213b5eSJohannes Berg 
1142051007d9SJohannes Berg 	return ret;
1143051007d9SJohannes Berg }
1144051007d9SJohannes Berg 
114524723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
114624723d1bSJohannes Berg 			  unsigned long exp_time)
114724723d1bSJohannes Berg {
114824723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
114924723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
115024723d1bSJohannes Berg 
115134e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1152e46a2cf9SMohammed Shafi Shajakhan 
1153e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1154b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1155b8da6b6aSJohannes Berg 
1156ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1157ec2b774eSMarek Lindner 			continue;
1158ec2b774eSMarek Lindner 
1159b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1160eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1161bdcbd8e0SJohannes Berg 				sta->sta.addr);
11623f52b7e3SMarco Porsch 
11633f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
11643f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
11653f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
11663f52b7e3SMarco Porsch 
116734e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
116824723d1bSJohannes Berg 		}
1169e46a2cf9SMohammed Shafi Shajakhan 	}
1170e46a2cf9SMohammed Shafi Shajakhan 
117134e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
117224723d1bSJohannes Berg }
117317741cdcSJohannes Berg 
1174686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1175686b9cb9SBen Greear 						   const u8 *addr,
1176686b9cb9SBen Greear 						   const u8 *localaddr)
117717741cdcSJohannes Berg {
11787bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
117983e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
11807bedd0cfSJohannes Berg 	struct sta_info *sta;
118117741cdcSJohannes Berg 
1182686b9cb9SBen Greear 	/*
1183686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1184686b9cb9SBen Greear 	 * ... first in list.
1185686b9cb9SBen Greear 	 */
118683e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1187686b9cb9SBen Greear 		if (localaddr &&
1188b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1189686b9cb9SBen Greear 			continue;
1190f7c65594SJohannes Berg 		if (!sta->uploaded)
1191f7c65594SJohannes Berg 			return NULL;
119217741cdcSJohannes Berg 		return &sta->sta;
1193f7c65594SJohannes Berg 	}
1194f7c65594SJohannes Berg 
1195abe60632SJohannes Berg 	return NULL;
119617741cdcSJohannes Berg }
1197686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
11985ed176e1SJohannes Berg 
11995ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
12005ed176e1SJohannes Berg 					 const u8 *addr)
12015ed176e1SJohannes Berg {
1202f7c65594SJohannes Berg 	struct sta_info *sta;
12035ed176e1SJohannes Berg 
12045ed176e1SJohannes Berg 	if (!vif)
12055ed176e1SJohannes Berg 		return NULL;
12065ed176e1SJohannes Berg 
1207f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1208f7c65594SJohannes Berg 	if (!sta)
1209f7c65594SJohannes Berg 		return NULL;
12105ed176e1SJohannes Berg 
1211f7c65594SJohannes Berg 	if (!sta->uploaded)
1212f7c65594SJohannes Berg 		return NULL;
1213f7c65594SJohannes Berg 
1214f7c65594SJohannes Berg 	return &sta->sta;
12155ed176e1SJohannes Berg }
121617741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1217af818581SJohannes Berg 
1218e3685e03SJohannes Berg /* powersave support code */
1219e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
122050a9432dSJohannes Berg {
1221608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1222e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1223e3685e03SJohannes Berg 	struct sk_buff_head pending;
1224ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1225e3685e03SJohannes Berg 	unsigned long flags;
1226d012a605SMarco Porsch 	struct ps_data *ps;
1227d012a605SMarco Porsch 
12283918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
12293918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
12303918edb0SFelix Fietkau 				     u.ap);
12313918edb0SFelix Fietkau 
12323918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1233d012a605SMarco Porsch 		ps = &sdata->bss->ps;
12343f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
12353f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1236d012a605SMarco Porsch 	else
1237d012a605SMarco Porsch 		return;
123850a9432dSJohannes Berg 
1239c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
124047086fc5SJohannes Berg 
12415a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1242948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1243ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1244948d887dSJohannes Berg 
124530686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
124612375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1247af818581SJohannes Berg 
1248ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1249adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1250ba8c3d6fSFelix Fietkau 			continue;
1251ba8c3d6fSFelix Fietkau 
125218667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1253ba8c3d6fSFelix Fietkau 	}
1254ba8c3d6fSFelix Fietkau 
1255948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1256af818581SJohannes Berg 
12571d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
12581d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1259af818581SJohannes Berg 	/* Send all buffered frames to the station */
1260948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1261948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1262948d887dSJohannes Berg 
1263987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1264948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1265987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1266948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1267948d887dSJohannes Berg 		filtered += tmp - count;
1268948d887dSJohannes Berg 		count = tmp;
1269948d887dSJohannes Berg 
1270987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1271948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1272987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1273948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1274948d887dSJohannes Berg 		buffered += tmp - count;
1275948d887dSJohannes Berg 	}
1276948d887dSJohannes Berg 
1277e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
12785ac2e350SJohannes Berg 
12795ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
12805ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
12815ac2e350SJohannes Berg 
12825ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
12835ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
12845ac2e350SJohannes Berg 	 */
12855ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
12865ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
12871d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1288948d887dSJohannes Berg 
1289e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1290e3685e03SJohannes Berg 
1291687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1292062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1293062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1294687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1295687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1296687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1297687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1298687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1299687da132SEmmanuel Grumbach 		       sta->sta.addr);
1300687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1301687da132SEmmanuel Grumbach 					   sta->sta.addr,
1302687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1303687da132SEmmanuel Grumbach 	}
1304687da132SEmmanuel Grumbach 
1305af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1306af818581SJohannes Berg 
1307c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1308c868cb35SJohannes Berg 
1309bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
13102595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1311bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
131217c18bf8SJohannes Berg 
131317c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1314af818581SJohannes Berg }
1315af818581SJohannes Berg 
13160ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1317b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
13180ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1319ce662b44SJohannes Berg {
13200ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1321ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1322ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1323ce662b44SJohannes Berg 	struct sk_buff *skb;
1324ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1325ce662b44SJohannes Berg 	__le16 fc;
1326a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1327ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
132855de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1329ce662b44SJohannes Berg 
133041cbb0f5SLuca Coelho 	/* Don't send NDPs when STA is connected HE */
133141cbb0f5SLuca Coelho 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
133241cbb0f5SLuca Coelho 	    !(sdata->u.mgd.flags & IEEE80211_STA_DISABLE_HE))
133341cbb0f5SLuca Coelho 		return;
133441cbb0f5SLuca Coelho 
1335ce662b44SJohannes Berg 	if (qos) {
1336ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1337ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1338ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1339ce662b44SJohannes Berg 	} else {
1340ce662b44SJohannes Berg 		size -= 2;
1341ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1342ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1343ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1344ce662b44SJohannes Berg 	}
1345ce662b44SJohannes Berg 
1346ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1347ce662b44SJohannes Berg 	if (!skb)
1348ce662b44SJohannes Berg 		return;
1349ce662b44SJohannes Berg 
1350ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1351ce662b44SJohannes Berg 
13524df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1353ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1354ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1355ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1356ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1357ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1358864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1359ce662b44SJohannes Berg 
1360ce662b44SJohannes Berg 	skb->priority = tid;
1361ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
136259b66255SJohannes Berg 	if (qos) {
1363ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1364ce662b44SJohannes Berg 
13650ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1366ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1367ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
13680ead2510SEmmanuel Grumbach 			if (more_data)
13690ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
13700ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
13710ead2510SEmmanuel Grumbach 		}
1372ce662b44SJohannes Berg 	}
1373ce662b44SJohannes Berg 
1374ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1375ce662b44SJohannes Berg 
1376ce662b44SJohannes Berg 	/*
1377ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1378ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1379deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1380deeaee19SJohannes Berg 	 * ends the poll/service period.
1381ce662b44SJohannes Berg 	 */
138202f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1383deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1384ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1385ce662b44SJohannes Berg 
13866b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
13876b127c71SSujith Manoharan 
1388b77cf4f8SJohannes Berg 	if (call_driver)
1389b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1390b77cf4f8SJohannes Berg 					  reason, false);
139140b96408SJohannes Berg 
139289afe614SJohannes Berg 	skb->dev = sdata->dev;
139389afe614SJohannes Berg 
139455de908aSJohannes Berg 	rcu_read_lock();
139555de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
139655de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
139755de908aSJohannes Berg 		rcu_read_unlock();
139855de908aSJohannes Berg 		kfree_skb(skb);
139955de908aSJohannes Berg 		return;
140055de908aSJohannes Berg 	}
140155de908aSJohannes Berg 
140273c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
1403b9771d41SJohannes Berg 	ieee80211_xmit(sdata, sta, skb, 0);
140455de908aSJohannes Berg 	rcu_read_unlock();
1405ce662b44SJohannes Berg }
1406ce662b44SJohannes Berg 
14070a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
14080a1cb809SJohannes Berg {
14090a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
14100a1cb809SJohannes Berg 	if (tids & 0xF8)
14110a1cb809SJohannes Berg 		return fls(tids) - 1;
14120a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
14130a1cb809SJohannes Berg 	if (tids & BIT(0))
14140a1cb809SJohannes Berg 		return 0;
14150a1cb809SJohannes Berg 	return fls(tids) - 1;
14160a1cb809SJohannes Berg }
14170a1cb809SJohannes Berg 
14180ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
14190ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
14200ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
14210ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
14220ead2510SEmmanuel Grumbach  */
14230ead2510SEmmanuel Grumbach static bool
14240ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
14250ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
14260ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
14270ead2510SEmmanuel Grumbach {
14280ead2510SEmmanuel Grumbach 	int ac;
14290ead2510SEmmanuel Grumbach 
14300ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
14310ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
14320ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
14330ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
14340ead2510SEmmanuel Grumbach 	 */
14350ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
14360ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
14370ead2510SEmmanuel Grumbach 		return true;
14380ead2510SEmmanuel Grumbach 
14390ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1440f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
14410ead2510SEmmanuel Grumbach 			continue;
14420ead2510SEmmanuel Grumbach 
14430ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
14440ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
14450ead2510SEmmanuel Grumbach 			return true;
14460ead2510SEmmanuel Grumbach 	}
14470ead2510SEmmanuel Grumbach 
14480ead2510SEmmanuel Grumbach 	return false;
14490ead2510SEmmanuel Grumbach }
14500ead2510SEmmanuel Grumbach 
145147086fc5SJohannes Berg static void
14520ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
14530ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
14540ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
14550ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1456af818581SJohannes Berg {
1457af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1458af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1459948d887dSJohannes Berg 	int ac;
1460af818581SJohannes Berg 
1461f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1462948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
14634049e09aSJohannes Berg 		unsigned long tids;
14644049e09aSJohannes Berg 
1465f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1466948d887dSJohannes Berg 			continue;
1467948d887dSJohannes Berg 
14684049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
14694049e09aSJohannes Berg 
1470f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1471f9f760b4SJohannes Berg 		 * release from hardware queues
1472f9f760b4SJohannes Berg 		 */
14730ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
14740ead2510SEmmanuel Grumbach 			*driver_release_tids |=
14750ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
14760ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1477ba8c3d6fSFelix Fietkau 		}
1478f9f760b4SJohannes Berg 
14790ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
148047086fc5SJohannes Berg 			struct sk_buff *skb;
148147086fc5SJohannes Berg 
148247086fc5SJohannes Berg 			while (n_frames > 0) {
1483948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1484948d887dSJohannes Berg 				if (!skb) {
148547086fc5SJohannes Berg 					skb = skb_dequeue(
148647086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1487af818581SJohannes Berg 					if (skb)
1488af818581SJohannes Berg 						local->total_ps_buffered--;
1489af818581SJohannes Berg 				}
149047086fc5SJohannes Berg 				if (!skb)
149147086fc5SJohannes Berg 					break;
149247086fc5SJohannes Berg 				n_frames--;
14930ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
149447086fc5SJohannes Berg 			}
1495948d887dSJohannes Berg 		}
1496948d887dSJohannes Berg 
14970ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
14980ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
14990ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
15004049e09aSJohannes Berg 		 */
1501948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
15020ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1503948d887dSJohannes Berg 			break;
1504948d887dSJohannes Berg 	}
1505948d887dSJohannes Berg }
1506af818581SJohannes Berg 
15070ead2510SEmmanuel Grumbach static void
15080ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
15090ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
15100ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
15110ead2510SEmmanuel Grumbach {
15120ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
15130ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
15140ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
15150ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
15160ead2510SEmmanuel Grumbach 	bool more_data;
15170ead2510SEmmanuel Grumbach 
15180ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
15190ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
15200ead2510SEmmanuel Grumbach 
15210ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
15220ead2510SEmmanuel Grumbach 
15230ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
15240ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
15250ead2510SEmmanuel Grumbach 
15260ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
15270ead2510SEmmanuel Grumbach 
15281a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
15290ead2510SEmmanuel Grumbach 		driver_release_tids =
15300ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
15310ead2510SEmmanuel Grumbach 
1532f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1533f438ceb8SEmmanuel Grumbach 		int tid, ac;
15344049e09aSJohannes Berg 
1535ce662b44SJohannes Berg 		/*
1536ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1537ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1538ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1539ce662b44SJohannes Berg 		 *
1540ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1541ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1542ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1543ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1544ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1545ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1546ce662b44SJohannes Berg 		 *
1547ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1548ce662b44SJohannes Berg 		 */
1549ce662b44SJohannes Berg 
1550ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1551f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1552d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1553d7f84244SEmmanuel Grumbach 				break;
1554f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1555ce662b44SJohannes Berg 
15560ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1557f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
155847086fc5SJohannes Berg 		struct sk_buff_head pending;
155947086fc5SJohannes Berg 		struct sk_buff *skb;
156040b96408SJohannes Berg 		int num = 0;
156140b96408SJohannes Berg 		u16 tids = 0;
1562b77cf4f8SJohannes Berg 		bool need_null = false;
156347086fc5SJohannes Berg 
156447086fc5SJohannes Berg 		skb_queue_head_init(&pending);
156547086fc5SJohannes Berg 
156647086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1567af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
156847086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
156940b96408SJohannes Berg 			u8 *qoshdr = NULL;
157040b96408SJohannes Berg 
157140b96408SJohannes Berg 			num++;
1572af818581SJohannes Berg 
1573af818581SJohannes Berg 			/*
157447086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
157547086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
157647086fc5SJohannes Berg 			 * exchange.
1577af818581SJohannes Berg 			 */
15786b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
15796b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1580af818581SJohannes Berg 
158147086fc5SJohannes Berg 			/*
158247086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
158347086fc5SJohannes Berg 			 * more buffered frames for this STA
158447086fc5SJohannes Berg 			 */
158524b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
158647086fc5SJohannes Berg 				hdr->frame_control |=
158747086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
158824b9c373SJanusz.Dziedzic@tieto.com 			else
158924b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
159024b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1591af818581SJohannes Berg 
159240b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
159340b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
159440b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
159540b96408SJohannes Berg 
1596b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1597b77cf4f8SJohannes Berg 
1598b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1599b77cf4f8SJohannes Berg 
1600b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1601b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1602b77cf4f8SJohannes Berg 				continue;
1603b77cf4f8SJohannes Berg 
1604b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1605b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1606b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1607b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1608b77cf4f8SJohannes Berg 				break;
1609b77cf4f8SJohannes Berg 			}
1610b77cf4f8SJohannes Berg 
1611b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1612b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1613b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1614b77cf4f8SJohannes Berg 			 * and be done.
1615b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1616b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1617b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1618b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1619b77cf4f8SJohannes Berg 			 *
1620b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1621b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1622b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1623b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1624b77cf4f8SJohannes Berg 			 *
1625b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1626b77cf4f8SJohannes Berg 			 */
1627b77cf4f8SJohannes Berg 			if (qoshdr) {
162840b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1629deeaee19SJohannes Berg 
163047086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
163147086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1632b77cf4f8SJohannes Berg 			} else {
1633b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1634b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1635b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1636b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1637b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1638b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1639b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1640b77cf4f8SJohannes Berg 				 */
1641b77cf4f8SJohannes Berg 				hdr->frame_control |=
1642b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1643b77cf4f8SJohannes Berg 				need_null = true;
1644b77cf4f8SJohannes Berg 				num++;
164552a3f20cSMarco Porsch 			}
1646b77cf4f8SJohannes Berg 			break;
164747086fc5SJohannes Berg 		}
164847086fc5SJohannes Berg 
164940b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
165040b96408SJohannes Berg 					  reason, more_data);
165140b96408SJohannes Berg 
165247086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1653af818581SJohannes Berg 
1654b77cf4f8SJohannes Berg 		if (need_null)
1655b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
16560ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
16570ead2510SEmmanuel Grumbach 				reason, false, false);
1658b77cf4f8SJohannes Berg 
1659c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1660af818581SJohannes Berg 	} else {
1661ba8c3d6fSFelix Fietkau 		int tid;
1662ba8c3d6fSFelix Fietkau 
1663af818581SJohannes Berg 		/*
16644049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
16654049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1666f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1667f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1668f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1669f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1670f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1671f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1672af818581SJohannes Berg 		 */
16734049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
167447086fc5SJohannes Berg 					    n_frames, reason, more_data);
16754049e09aSJohannes Berg 
16764049e09aSJohannes Berg 		/*
16774049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
16784049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1679f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
16804049e09aSJohannes Berg 		 * release function.
1681f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1682ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1683ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
16844049e09aSJohannes Berg 		 */
1685ba8c3d6fSFelix Fietkau 
1686ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1687ba8c3d6fSFelix Fietkau 			return;
1688ba8c3d6fSFelix Fietkau 
1689ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1690adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
1691adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
16921e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1693ba8c3d6fSFelix Fietkau 				continue;
1694ba8c3d6fSFelix Fietkau 
1695ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1696ba8c3d6fSFelix Fietkau 			break;
1697ba8c3d6fSFelix Fietkau 		}
1698af818581SJohannes Berg 	}
1699af818581SJohannes Berg }
1700af818581SJohannes Berg 
1701af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1702af818581SJohannes Berg {
170347086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1704af818581SJohannes Berg 
1705af818581SJohannes Berg 	/*
170647086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
170747086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
170847086fc5SJohannes Berg 	 * only from the non-enabled ones.
1709af818581SJohannes Berg 	 */
171047086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
171147086fc5SJohannes Berg 		ignore_for_response = 0;
1712af818581SJohannes Berg 
171347086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
171447086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1715af818581SJohannes Berg }
171647086fc5SJohannes Berg 
171747086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
171847086fc5SJohannes Berg {
171947086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
172047086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
172147086fc5SJohannes Berg 
172247086fc5SJohannes Berg 	/*
172347086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
172447086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
172547086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
172647086fc5SJohannes Berg 	 * actually getting called.
172747086fc5SJohannes Berg 	 */
172847086fc5SJohannes Berg 	if (!delivery_enabled)
172947086fc5SJohannes Berg 		return;
173047086fc5SJohannes Berg 
173147086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
173247086fc5SJohannes Berg 	case 1:
173347086fc5SJohannes Berg 		n_frames = 2;
173447086fc5SJohannes Berg 		break;
173547086fc5SJohannes Berg 	case 2:
173647086fc5SJohannes Berg 		n_frames = 4;
173747086fc5SJohannes Berg 		break;
173847086fc5SJohannes Berg 	case 3:
173947086fc5SJohannes Berg 		n_frames = 6;
174047086fc5SJohannes Berg 		break;
174147086fc5SJohannes Berg 	case 0:
174247086fc5SJohannes Berg 		/* XXX: what is a good value? */
174313a8098aSAndrei Otcheretianski 		n_frames = 128;
174447086fc5SJohannes Berg 		break;
174547086fc5SJohannes Berg 	}
174647086fc5SJohannes Berg 
174747086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
174847086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1749af818581SJohannes Berg }
1750af818581SJohannes Berg 
1751af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1752af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1753af818581SJohannes Berg {
1754af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1755af818581SJohannes Berg 
1756b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1757b5878a2dSJohannes Berg 
17585ac2e350SJohannes Berg 	if (block) {
1759c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
176017c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
17615ac2e350SJohannes Berg 		return;
17625ac2e350SJohannes Berg 	}
17635ac2e350SJohannes Berg 
17645ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
17655ac2e350SJohannes Berg 		return;
17665ac2e350SJohannes Berg 
17675ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
17685ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
17695ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
17705ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
17715ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
17725ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
17735ac2e350SJohannes Berg 		/* must be asleep in this case */
17745ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
17755ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
17765ac2e350SJohannes Berg 	} else {
17775ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
177817c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
17795ac2e350SJohannes Berg 	}
1780af818581SJohannes Berg }
1781af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1782dcf55fb5SFelix Fietkau 
1783e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
178437fbd908SJohannes Berg {
178537fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
178637fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
178737fbd908SJohannes Berg 
178837fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
178937fbd908SJohannes Berg 
179037fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
179137fbd908SJohannes Berg }
1792e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
179337fbd908SJohannes Berg 
17940ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
17950ead2510SEmmanuel Grumbach {
17960ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
17970ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
17980ead2510SEmmanuel Grumbach 	bool more_data;
17990ead2510SEmmanuel Grumbach 
18000ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
18010ead2510SEmmanuel Grumbach 
18020ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
18030ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
18040ead2510SEmmanuel Grumbach 					       reason, 0);
18050ead2510SEmmanuel Grumbach 
18060ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
18070ead2510SEmmanuel Grumbach }
18080ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
18090ead2510SEmmanuel Grumbach 
1810042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1811042ec453SJohannes Berg 				u8 tid, bool buffered)
1812dcf55fb5SFelix Fietkau {
1813dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1814dcf55fb5SFelix Fietkau 
18155a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1816042ec453SJohannes Berg 		return;
1817042ec453SJohannes Berg 
18181b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
18191b000789SJohannes Berg 
1820948d887dSJohannes Berg 	if (buffered)
1821948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1822948d887dSJohannes Berg 	else
1823948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1824948d887dSJohannes Berg 
1825c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1826dcf55fb5SFelix Fietkau }
1827042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1828d9a7ddb0SJohannes Berg 
1829b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
1830b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
1831b4809e94SToke Høiland-Jørgensen {
1832b4809e94SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1833b4809e94SToke Høiland-Jørgensen 	struct ieee80211_local *local = sta->sdata->local;
1834b4809e94SToke Høiland-Jørgensen 	u8 ac = ieee80211_ac_from_tid(tid);
1835b4809e94SToke Høiland-Jørgensen 	u32 airtime = 0;
1836b4809e94SToke Høiland-Jørgensen 
1837b4809e94SToke Høiland-Jørgensen 	if (sta->local->airtime_flags & AIRTIME_USE_TX)
1838b4809e94SToke Høiland-Jørgensen 		airtime += tx_airtime;
1839b4809e94SToke Høiland-Jørgensen 	if (sta->local->airtime_flags & AIRTIME_USE_RX)
1840b4809e94SToke Høiland-Jørgensen 		airtime += rx_airtime;
1841b4809e94SToke Høiland-Jørgensen 
1842b4809e94SToke Høiland-Jørgensen 	spin_lock_bh(&local->active_txq_lock[ac]);
1843b4809e94SToke Høiland-Jørgensen 	sta->airtime[ac].tx_airtime += tx_airtime;
1844b4809e94SToke Høiland-Jørgensen 	sta->airtime[ac].rx_airtime += rx_airtime;
1845b4809e94SToke Høiland-Jørgensen 	sta->airtime[ac].deficit -= airtime;
1846b4809e94SToke Høiland-Jørgensen 	spin_unlock_bh(&local->active_txq_lock[ac]);
1847b4809e94SToke Høiland-Jørgensen }
1848b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
1849b4809e94SToke Høiland-Jørgensen 
185083d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1851d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1852d9a7ddb0SJohannes Berg {
18538bf11d8dSJohannes Berg 	might_sleep();
1854d9a7ddb0SJohannes Berg 
1855d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1856d9a7ddb0SJohannes Berg 		return 0;
1857d9a7ddb0SJohannes Berg 
1858f09603a2SJohannes Berg 	/* check allowed transitions first */
1859f09603a2SJohannes Berg 
1860d9a7ddb0SJohannes Berg 	switch (new_state) {
1861d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1862f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1863d9a7ddb0SJohannes Berg 			return -EINVAL;
1864d9a7ddb0SJohannes Berg 		break;
1865d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1866f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1867f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1868d9a7ddb0SJohannes Berg 			return -EINVAL;
1869d9a7ddb0SJohannes Berg 		break;
1870d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1871f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1872f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1873d9a7ddb0SJohannes Berg 			return -EINVAL;
1874d9a7ddb0SJohannes Berg 		break;
1875d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1876f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1877d9a7ddb0SJohannes Berg 			return -EINVAL;
1878d9a7ddb0SJohannes Berg 		break;
1879d9a7ddb0SJohannes Berg 	default:
1880d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1881d9a7ddb0SJohannes Berg 		return -EINVAL;
1882d9a7ddb0SJohannes Berg 	}
1883d9a7ddb0SJohannes Berg 
1884bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1885bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1886f09603a2SJohannes Berg 
1887f09603a2SJohannes Berg 	/*
1888f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1889f09603a2SJohannes Berg 	 * fail the transition
1890f09603a2SJohannes Berg 	 */
1891f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1892f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1893f09603a2SJohannes Berg 					sta->sta_state, new_state);
1894f09603a2SJohannes Berg 		if (err)
1895f09603a2SJohannes Berg 			return err;
1896f09603a2SJohannes Berg 	}
1897f09603a2SJohannes Berg 
1898f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1899f09603a2SJohannes Berg 
1900f09603a2SJohannes Berg 	switch (new_state) {
1901f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1902f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1903f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1904f09603a2SJohannes Berg 		break;
1905f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1906a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
1907f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1908a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1909f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1910a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
191152cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
191252cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1913a7201a6cSIlan Peer 		}
1914f09603a2SJohannes Berg 		break;
1915f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1916f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1917f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1918a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
191952cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
192052cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1921f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
192272f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
1923f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
192417c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
192549ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
1926f09603a2SJohannes Berg 		}
1927f09603a2SJohannes Berg 		break;
1928f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1929f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
193072f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
1931f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
193217c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
193349ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
1934f09603a2SJohannes Berg 		}
1935f09603a2SJohannes Berg 		break;
1936f09603a2SJohannes Berg 	default:
1937f09603a2SJohannes Berg 		break;
1938f09603a2SJohannes Berg 	}
1939f09603a2SJohannes Berg 
1940d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1941d9a7ddb0SJohannes Berg 
1942d9a7ddb0SJohannes Berg 	return 0;
1943d9a7ddb0SJohannes Berg }
1944687da132SEmmanuel Grumbach 
1945687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1946687da132SEmmanuel Grumbach {
1947687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1948687da132SEmmanuel Grumbach 	u8 rx_streams;
1949687da132SEmmanuel Grumbach 
1950687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1951687da132SEmmanuel Grumbach 		return 1;
1952687da132SEmmanuel Grumbach 
1953687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1954687da132SEmmanuel Grumbach 		int i;
1955687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1956687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1957687da132SEmmanuel Grumbach 
1958687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1959687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1960687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1961687da132SEmmanuel Grumbach 				return i + 1;
1962687da132SEmmanuel Grumbach 	}
1963687da132SEmmanuel Grumbach 
1964687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1965687da132SEmmanuel Grumbach 		rx_streams = 4;
1966687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1967687da132SEmmanuel Grumbach 		rx_streams = 3;
1968687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1969687da132SEmmanuel Grumbach 		rx_streams = 2;
1970687da132SEmmanuel Grumbach 	else
1971687da132SEmmanuel Grumbach 		rx_streams = 1;
1972687da132SEmmanuel Grumbach 
1973687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1974687da132SEmmanuel Grumbach 		return rx_streams;
1975687da132SEmmanuel Grumbach 
1976687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1977687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1978687da132SEmmanuel Grumbach }
1979b7ffbd7eSJohannes Berg 
1980c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
1981c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
1982c9c5962bSJohannes Berg {
1983c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
1984c9c5962bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1985c9c5962bSJohannes Berg 	int cpu;
1986c9c5962bSJohannes Berg 
1987c9c5962bSJohannes Berg 	if (!ieee80211_hw_check(&local->hw, USES_RSS))
1988c9c5962bSJohannes Berg 		return stats;
1989c9c5962bSJohannes Berg 
1990c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
1991c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
1992c9c5962bSJohannes Berg 
1993c9c5962bSJohannes Berg 		cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
1994c9c5962bSJohannes Berg 
1995c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
1996c9c5962bSJohannes Berg 			stats = cpustats;
1997c9c5962bSJohannes Berg 	}
1998c9c5962bSJohannes Berg 
1999c9c5962bSJohannes Berg 	return stats;
2000c9c5962bSJohannes Berg }
2001c9c5962bSJohannes Berg 
200241cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
20034f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2004fbd6ff5cSJohannes Berg {
2005dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2006fbd6ff5cSJohannes Berg 
2007dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
20087f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
20094f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2010dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2011dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2012dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2013dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
20147f406cd1SJohannes Berg 		break;
20157f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
20164f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2017dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2018dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2019dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
20207f406cd1SJohannes Berg 		break;
20217f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2022fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2023fbd6ff5cSJohannes Berg 		u16 brate;
20244f6b1b3dSJohannes Berg 		unsigned int shift;
2025dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2026dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2027fbd6ff5cSJohannes Berg 
2028dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
2029dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
20304f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
20314f6b1b3dSJohannes Berg 			shift = 2;
20324f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
20334f6b1b3dSJohannes Berg 			shift = 1;
20344f6b1b3dSJohannes Berg 		else
20354f6b1b3dSJohannes Berg 			shift = 0;
2036fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
20377f406cd1SJohannes Berg 		break;
20387f406cd1SJohannes Berg 		}
203941cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
204041cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
204141cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
204241cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
204341cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
204441cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
204541cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
204641cbb0f5SLuca Coelho 		break;
2047fbd6ff5cSJohannes Berg 	}
20484f6b1b3dSJohannes Berg }
2049fbd6ff5cSJohannes Berg 
2050a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
20514f6b1b3dSJohannes Berg {
20526aa7de05SMark Rutland 	u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
20534f6b1b3dSJohannes Berg 
20544f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2055a17d93ffSBen Greear 		return -EINVAL;
2056a17d93ffSBen Greear 
20574f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2058a17d93ffSBen Greear 	return 0;
2059fbd6ff5cSJohannes Berg }
2060fbd6ff5cSJohannes Berg 
20610f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
20620f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
20630f9c5a61SJohannes Berg 			     int tid)
20640f9c5a61SJohannes Berg {
20650f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
20660f9c5a61SJohannes Berg 
20670f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
20680f9c5a61SJohannes Berg 		unsigned int start;
20690f9c5a61SJohannes Berg 
20700f9c5a61SJohannes Berg 		do {
20710f9c5a61SJohannes Berg 			start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
20720f9c5a61SJohannes Berg 			tidstats->rx_msdu = sta->rx_stats.msdu[tid];
20730f9c5a61SJohannes Berg 		} while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
20740f9c5a61SJohannes Berg 
20750f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
20760f9c5a61SJohannes Berg 	}
20770f9c5a61SJohannes Berg 
20780f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
20790f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
20800f9c5a61SJohannes Berg 		tidstats->tx_msdu = sta->tx_stats.msdu[tid];
20810f9c5a61SJohannes Berg 	}
20820f9c5a61SJohannes Berg 
20830f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
20840f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
20850f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
20860f9c5a61SJohannes Berg 		tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
20870f9c5a61SJohannes Berg 	}
20880f9c5a61SJohannes Berg 
20890f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
20900f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
20910f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
20920f9c5a61SJohannes Berg 		tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
20930f9c5a61SJohannes Berg 	}
20942fe4a29aSToke Høiland-Jørgensen 
20952fe4a29aSToke Høiland-Jørgensen 	if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
20962fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
20972fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
20982fe4a29aSToke Høiland-Jørgensen 
20992fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
21002fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
21012fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
21022fe4a29aSToke Høiland-Jørgensen 
21032fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
21042fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
21052fe4a29aSToke Høiland-Jørgensen 	}
21060f9c5a61SJohannes Berg }
21070f9c5a61SJohannes Berg 
2108c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2109c9c5962bSJohannes Berg {
2110c9c5962bSJohannes Berg 	unsigned int start;
2111c9c5962bSJohannes Berg 	u64 value;
2112c9c5962bSJohannes Berg 
2113c9c5962bSJohannes Berg 	do {
2114c9c5962bSJohannes Berg 		start = u64_stats_fetch_begin(&rxstats->syncp);
2115c9c5962bSJohannes Berg 		value = rxstats->bytes;
2116c9c5962bSJohannes Berg 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2117c9c5962bSJohannes Berg 
2118c9c5962bSJohannes Berg 	return value;
2119c9c5962bSJohannes Berg }
2120c9c5962bSJohannes Berg 
21210fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
21220fdf1493SJohannes Berg 		   bool tidstats)
2123b7ffbd7eSJohannes Berg {
2124b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2125b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2126b7ffbd7eSJohannes Berg 	u32 thr = 0;
2127c9c5962bSJohannes Berg 	int i, ac, cpu;
2128c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2129c9c5962bSJohannes Berg 
2130c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2131b7ffbd7eSJohannes Berg 
2132b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2133b7ffbd7eSJohannes Berg 
2134225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2135225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2136225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2137225b8189SJohannes Berg 	 */
2138225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2139225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2140225b8189SJohannes Berg 
21412b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
21422b9a7e1bSJohannes Berg 
2143a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2144a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2145a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2146a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
2147a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2148976bd9efSJohannes Berg 
2149976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2150976bd9efSJohannes Berg 		sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2151a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2152976bd9efSJohannes Berg 	}
2153b7ffbd7eSJohannes Berg 
215484b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2155e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2156b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
21572b9a7e1bSJohannes Berg 
2158a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2159a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2160b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
21612b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2162e5a9f8d0SJohannes Berg 			sinfo->tx_bytes += sta->tx_stats.bytes[ac];
2163a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2164b7ffbd7eSJohannes Berg 	}
21652b9a7e1bSJohannes Berg 
2166a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
21672b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
21682b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2169e5a9f8d0SJohannes Berg 			sinfo->tx_packets += sta->tx_stats.packets[ac];
2170a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
21712b9a7e1bSJohannes Berg 	}
21722b9a7e1bSJohannes Berg 
2173a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2174a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2175c9c5962bSJohannes Berg 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
21760f9c5a61SJohannes Berg 
2177c9c5962bSJohannes Berg 		if (sta->pcpu_rx_stats) {
2178c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2179c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2180c9c5962bSJohannes Berg 
2181c9c5962bSJohannes Berg 				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2182c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2183c9c5962bSJohannes Berg 			}
2184c9c5962bSJohannes Berg 		}
2185c9c5962bSJohannes Berg 
2186a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
21872b9a7e1bSJohannes Berg 	}
21882b9a7e1bSJohannes Berg 
2189a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2190e5a9f8d0SJohannes Berg 		sinfo->rx_packets = sta->rx_stats.packets;
2191c9c5962bSJohannes Berg 		if (sta->pcpu_rx_stats) {
2192c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2193c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2194c9c5962bSJohannes Berg 
2195c9c5962bSJohannes Berg 				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2196c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2197c9c5962bSJohannes Berg 			}
2198c9c5962bSJohannes Berg 		}
2199a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
22002b9a7e1bSJohannes Berg 	}
22012b9a7e1bSJohannes Berg 
2202a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2203e5a9f8d0SJohannes Berg 		sinfo->tx_retries = sta->status_stats.retry_count;
2204a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
22052b9a7e1bSJohannes Berg 	}
22062b9a7e1bSJohannes Berg 
2207a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2208e5a9f8d0SJohannes Berg 		sinfo->tx_failed = sta->status_stats.retry_failed;
2209a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
22102b9a7e1bSJohannes Berg 	}
22112b9a7e1bSJohannes Berg 
2212b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2213b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2214b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2215b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2216b4809e94SToke Høiland-Jørgensen 	}
2217b4809e94SToke Høiland-Jørgensen 
2218b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2219b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2220b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2221b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2222b4809e94SToke Høiland-Jørgensen 	}
2223b4809e94SToke Høiland-Jørgensen 
2224b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2225b4809e94SToke Høiland-Jørgensen 		sinfo->airtime_weight = sta->airtime_weight;
2226b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2227b4809e94SToke Høiland-Jørgensen 	}
2228b4809e94SToke Høiland-Jørgensen 
2229e5a9f8d0SJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_stats.dropped;
2230c9c5962bSJohannes Berg 	if (sta->pcpu_rx_stats) {
2231c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2232c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2233c9c5962bSJohannes Berg 
2234c9c5962bSJohannes Berg 			cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2235e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2236c9c5962bSJohannes Berg 		}
2237c9c5962bSJohannes Berg 	}
2238b7ffbd7eSJohannes Berg 
2239225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2240225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2241a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2242a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2243225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2244225b8189SJohannes Berg 	}
2245225b8189SJohannes Berg 
224630686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
224730686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2248a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2249c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2250a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2251b7ffbd7eSJohannes Berg 		}
22522b9a7e1bSJohannes Berg 
2253c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats &&
2254a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
225540d9a38aSJohannes Berg 			sinfo->signal_avg =
22560be6ed13SJohannes Berg 				-ewma_signal_read(&sta->rx_stats_avg.signal);
2257a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
22582b9a7e1bSJohannes Berg 		}
22592b9a7e1bSJohannes Berg 	}
22602b9a7e1bSJohannes Berg 
2261c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2262c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2263c9c5962bSJohannes Berg 	 * pcpu statistics
2264c9c5962bSJohannes Berg 	 */
2265c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2266a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2267a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2268a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2269c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats)
2270a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2271b7ffbd7eSJohannes Berg 
2272c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2273c9c5962bSJohannes Berg 
2274b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2275e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2276c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2277b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
22780be6ed13SJohannes Berg 				-ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
2279b7ffbd7eSJohannes Berg 		}
2280b7ffbd7eSJohannes Berg 	}
2281b7ffbd7eSJohannes Berg 
2282a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
2283e5a9f8d0SJohannes Berg 		sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2284e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2285a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
22862b9a7e1bSJohannes Berg 	}
22872b9a7e1bSJohannes Berg 
2288a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
2289a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2290a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
22912b9a7e1bSJohannes Berg 	}
2292b7ffbd7eSJohannes Berg 
22930fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
22946af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
22956af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
22968689c051SArend van Spriel 	}
229779c892b8SJohannes Berg 
2298b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2299b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2300a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2301a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2302a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2303a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2304a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2305dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
2306dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE);
2307b7ffbd7eSJohannes Berg 
2308433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2309433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2310433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2311b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2312a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2313433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2314b7ffbd7eSJohannes Berg 		}
2315433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2316433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2317433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2318dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
2319b7ffbd7eSJohannes Berg #endif
2320b7ffbd7eSJohannes Berg 	}
2321b7ffbd7eSJohannes Berg 
2322b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2323b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2324b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2325b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2326b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2327b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2328b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2329785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2330b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2331b7ffbd7eSJohannes Berg 
2332b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2333b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2334b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2335b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2336b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2337b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2338b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2339b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2340b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2341b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2342b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2343b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2344a74a8c84SJohannes Berg 	if (sta->sta.wme)
2345b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2346b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2347b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2348b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2349b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2350b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2351b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2352b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2353b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2354b7ffbd7eSJohannes Berg 
23553b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
23563b17fbf8SMaxim Altshul 
23573b17fbf8SMaxim Altshul 	if (thr != 0) {
2358a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
23593b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
23603b17fbf8SMaxim Altshul 	}
2361a78b26ffSVenkateswara Naralasetty 
2362a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2363a78b26ffSVenkateswara Naralasetty 	    sta->status_stats.ack_signal_filled) {
2364a78b26ffSVenkateswara Naralasetty 		sinfo->ack_signal = sta->status_stats.last_ack_signal;
2365a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2366a78b26ffSVenkateswara Naralasetty 	}
2367cc60dbbfSBalaji Pothunoori 
23689c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
23699c06602bSBalaji Pothunoori 	    sta->status_stats.ack_signal_filled) {
2370cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2371cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2372cc60dbbfSBalaji Pothunoori 				&sta->status_stats.avg_ack_signal);
2373cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
23749c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2375cc60dbbfSBalaji Pothunoori 	}
2376*ab60633cSNarayanraddi Masti 
2377*ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2378*ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2379*ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2380*ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2381*ab60633cSNarayanraddi Masti 	}
23823b17fbf8SMaxim Altshul }
23833b17fbf8SMaxim Altshul 
23843b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
23853b17fbf8SMaxim Altshul {
23863b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
23873b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
23883b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
23893b17fbf8SMaxim Altshul 	u32 thr = 0;
23903b17fbf8SMaxim Altshul 
23913b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
23923b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
23933b17fbf8SMaxim Altshul 
2394b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2395b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2396b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2397b7ffbd7eSJohannes Berg 	else
23984fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2399b7ffbd7eSJohannes Berg 
24003b17fbf8SMaxim Altshul 	return thr;
2401b7ffbd7eSJohannes Berg }
2402b8da6b6aSJohannes Berg 
2403b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2404b8da6b6aSJohannes Berg {
2405c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2406c9c5962bSJohannes Berg 
2407c9c5962bSJohannes Berg 	if (time_after(stats->last_rx, sta->status_stats.last_ack))
2408c9c5962bSJohannes Berg 		return stats->last_rx;
2409b8da6b6aSJohannes Berg 	return sta->status_stats.last_ack;
2410b8da6b6aSJohannes Berg }
2411484a54c2SToke Høiland-Jørgensen 
2412484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2413484a54c2SToke Høiland-Jørgensen {
2414484a54c2SToke Høiland-Jørgensen 	if (!sta->sdata->local->ops->wake_tx_queue)
2415484a54c2SToke Høiland-Jørgensen 		return;
2416484a54c2SToke Høiland-Jørgensen 
2417484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2418484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2419484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2420484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2421484a54c2SToke Høiland-Jørgensen 	} else {
2422484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2423484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2424484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2425484a54c2SToke Høiland-Jørgensen 	}
2426484a54c2SToke Høiland-Jørgensen }
2427484a54c2SToke Høiland-Jørgensen 
2428484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2429484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2430484a54c2SToke Høiland-Jørgensen {
2431484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2432484a54c2SToke Høiland-Jørgensen 
2433484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2434484a54c2SToke Høiland-Jørgensen }
2435